简介
samgr组件是OpenHarmony的核心组件,提供OpenHarmony系统服务启动、注册、查询等功能。
系统架构
图 1 系统服务管理系统架构图
目录
/foundation/distributedschedule
├── samgr
│ ├── bundle.json # 部件描述及编译文件
│ ├── frameworks # 框架实现存在目录
│ ├── interfaces # 接口目录
│ ├── services # 组件服务端目录
│ ├── test # 测试代码存放目录
│ ├── utils # 工具类目录
- samgr服务接收到sa框架层发送的注册消息,会在本地缓存中存入系统服务相关信息。
int32_t SystemAbilityManager::AddSystemAbility(int32_t systemAbilityId, const sptr& ability,
const SAExtraProp& extraProp)
{
if (!CheckInputSysAbilityId(systemAbilityId) || ability == nullptr) {
HILOGE("AddSystemAbilityExtra input params is invalid.");
return ERR_INVALID_VALUE;
}
{
unique_lock writeLock(abilityMapLock_);
auto saSize = abilityMap_.size();
if (saSize >= MAX_SERVICES) {
HILOGE("map size error, (Has been greater than %zu)", saSize);
return ERR_INVALID_VALUE;
}
SAInfo saInfo;
saInfo.remoteObj = ability;
saInfo.isDistributed = extraProp.isDistributed;
saInfo.capability = extraProp.capability;
saInfo.permission = Str16ToStr8(extraProp.permission);
abilityMap_[systemAbilityId] = std::move(saInfo);
HILOGI("insert %{public}d. size : %{public}zu", systemAbilityId, abilityMap_.size());
}
RemoveCheckLoadedMsg(systemAbilityId);
if (abilityDeath_ != nullptr) {
ability->AddDeathRecipient(abilityDeath_);
}
u16string strName = Str8ToStr16(to_string(systemAbilityId));
if (extraProp.isDistributed && dBinderService_ != nullptr) {
dBinderService_->RegisterRemoteProxy(strName, systemAbilityId);
HILOGD("AddSystemAbility RegisterRemoteProxy, serviceId is %{public}d", systemAbilityId);
}
if (systemAbilityId == SOFTBUS_SERVER_SA_ID && !isDbinderStart_) {
if (dBinderService_ != nullptr && rpcCallbackImp_ != nullptr) {
bool ret = dBinderService_->StartDBinderService(rpcCallbackImp_);
HILOGI("start result is %{public}s", ret ? "succeed" : "fail");
isDbinderStart_ = true;
}
}
SendSystemAbilityAddedMsg(systemAbilityId, ability);
return ERR_OK;
}
- 对于本地服务而言,samgr服务接收到sa框架层发送的获取消息,会通过服务id,查找到对应服务的代理对象,然后返回给sa框架。
sptr SystemAbilityManager::CheckSystemAbility(int32_t systemAbilityId)
{
if (!CheckInputSysAbilityId(systemAbilityId)) {
HILOGW("CheckSystemAbility CheckSystemAbility invalid!");
return nullptr;
}
shared_lock readLock(abilityMapLock_);
auto iter = abilityMap_.find(systemAbilityId);
if (iter != abilityMap_.end()) {
HILOGI("found service : %{public}d.", systemAbilityId);
return iter->second.remoteObj;
}
HILOGI("NOT found service : %{public}d", systemAbilityId);
return nullptr;
}
- 动态加载系统服务进程及SystemAbility, 系统进程无需开机启动,而是在SystemAbility被访问的时候按需拉起,并加载指定SystemAbility。
3.1 继承SystemAbilityLoadCallbackStub类,并覆写OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject)、OnLoadSystemAbilityFail(int32_t systemAbilityId)方法。
class OnDemandLoadCallback : public SystemAbilityLoadCallbackStub {
public:
void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) override;
void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
};
void OnDemandLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
const sptr& remoteObject) // systemAbilityId为指定加载的SAID,remoteObject为指定systemAbility的代理对象
{
cout << "OnLoadSystemAbilitySuccess systemAbilityId:" << systemAbilityId << " IRemoteObject result:" <<
((remoteObject != nullptr) ? "succeed" : "failed") << endl;
}
void OnDemandLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) // systemAbilityId为指定加载的SAID
{
cout << "OnLoadSystemAbilityFail systemAbilityId:" << systemAbilityId << endl;
}
3.2 调用samgr提供的动态加载接口LoadSystemAbility(int32_t systemAbilityId, const sptr& callback)。
// 构造步骤1的SystemAbilityLoadCallbackStub子类的实例
sptr loadCallback_ = new OnDemandLoadCallback();
// 调用LoadSystemAbility方法
sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (sm == nullptr) {
cout << "GetSystemAbilityManager samgr object null!" << endl;
return;
}
int32_t result = sm->LoadSystemAbility(systemAbilityId, loadCallback_);
if (result != ERR_OK) {
cout << "systemAbilityId:" << systemAbilityId << " load failed, result code:" << result << endl;
return;
}
说明:
1.LoadSystemAbility方法调用成功后,指定SystemAbility加载成功后会触发回调OnLoadSystemAbilitySuccess,加载失败触发回调OnLoadSystemAbilityFail。
2.动态加载的进程cfg文件不能配置为开机启动,需指定”ondemand” : true, 示例如下:
{
"services" : [{
"name" : "listen_test",
"path" : ["/system/bin/sa_main", "/system/profile/listen_test.xml"],
"ondemand" : true,
"uid" : "system",
"gid" : ["system", "shell"]
}
]
}
阅读全文
下载说明:
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.1024c.cn/archives/21270,转载请注明出处。
1、本站所有资源均从互联网上收集整理而来,仅供学习交流之用,因此不包含技术服务请大家谅解!
2、本站不提供任何实质性的付费和支付资源,所有需要积分下载的资源均为网站运营赞助费用或者线下劳务费用!
3、本站所有资源仅用于学习及研究使用,您必须在下载后的24小时内删除所下载资源,切勿用于商业用途,否则由此引发的法律纠纷及连带责任本站和发布者概不承担!
4、本站站内提供的所有可下载资源,本站保证未做任何负面改动(不包含修复bug和完善功能等正面优化或二次开发),但本站不保证资源的准确性、安全性和完整性,用户下载后自行斟酌,我们以交流学习为目的,并不是所有的源码都100%无错或无bug!如有链接无法下载、失效或广告,请联系客服处理!
5、本站资源除标明原创外均来自网络整理,版权归原作者或本站特约原创作者所有,如侵犯到您的合法权益,请立即告知本站,本站将及时予与删除并致以最深的歉意!
6、如果您也有好的资源或教程,您可以投稿发布,成功分享后有站币奖励和额外收入!
7、如果您喜欢该资源,请支持官方正版资源,以得到更好的正版服务!
8、请您认真阅读上述内容,注册本站用户或下载本站资源即您同意上述内容!
原文链接:https://www.1024c.cn/archives/21270,转载请注明出处。
评论0