簡介
samgr組件是OpenHarmony的核心組件,提供OpenHarmony系統服務啟動、注冊、查詢等功能。
系統架構
圖 1 系統服務管理系統架構圖
目錄
/foundation/systemabilitymgr
├── samgr
│ ├── bundle.json # 部件描述及編譯文件
│ ├── frameworks # 框架實現存在目錄
│ ├── interfaces # 接口目錄
│ ├── services # 組件服務端目錄
│ ├── test # 測試代碼存放目錄
│ ├── utils # 工具類目錄
說明
samgr服務接收到sa框架層發送的注冊消息,會在本地緩存中存入系統服務相關信息。
int32_t SystemAbilityManager::AddSystemAbility(int32_t systemAbilityId, const sptr< IRemoteObject >& ability, const SAExtraProp& extraProp) { if (!CheckInputSysAbilityId(systemAbilityId) || ability == nullptr) { HILOGE("AddSystemAbilityExtra input params is invalid."); return ERR_INVALID_VALUE; } { unique_lock< shared_mutex > 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< IRemoteObject > SystemAbilityManager::CheckSystemAbility(int32_t systemAbilityId) { if (!CheckInputSysAbilityId(systemAbilityId)) { HILOGW("CheckSystemAbility CheckSystemAbility invalid!"); return nullptr; } shared_lock< shared_mutex > 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< IRemoteObject >& remoteObject) override; void OnLoadSystemAbilityFail(int32_t systemAbilityId) override; }; void OnDemandLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr< IRemoteObject >& 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< OnDemandLoadCallback > loadCallback_ = new OnDemandLoadCallback(); // 調用LoadSystemAbility方法 sptr< ISystemAbilityManager > 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.json"],
> "ondemand" : true,
> "uid" : "system",
> "gid" : ["system", "shell"]
> }
> ]
> }
3.LoadSystemAbility方法適用于動態加載場景,其他獲取SystemAbility場景建議使用CheckSystemAbility方法。
4.cfg里進程名稱需要與SA的配置json文件里進程名保持一致
審核編輯 黃宇
-
鴻蒙
+關注
關注
57文章
2310瀏覽量
42745 -
OpenHarmony
+關注
關注
25文章
3660瀏覽量
16158
發布評論請先 登錄
相關推薦
評論