1 /*
2 * Copyright (c) 2021-2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "componentloader/component_loader.h"
17
18 #include <cinttypes>
19 #include <dlfcn.h>
20 #include <fstream>
21 #include <string>
22
23 #include "config_policy_utils.h"
24
25 #include "anonymous_string.h"
26 #include "constants.h"
27 #include "dh_context.h"
28 #include "dh_utils_hitrace.h"
29 #include "dh_utils_hisysevent.h"
30 #include "dh_utils_tool.h"
31 #include "hidump_helper.h"
32 #include "distributed_hardware_log.h"
33 #include "version_info.h"
34 #include "version_info_manager.h"
35 #include "version_manager.h"
36
37 namespace OHOS {
38 namespace DistributedHardware {
39 #undef DH_LOG_TAG
40 #define DH_LOG_TAG "ComponentLoader"
41
42 IMPLEMENT_SINGLE_INSTANCE(ComponentLoader);
43 using GetHardwareClass = IHardwareHandler *(*)();
44 using GetSourceHardwareClass = IDistributedHardwareSource *(*)();
45 using GetSinkHardwareClass = IDistributedHardwareSink *(*)();
46 namespace {
47 constexpr const char *COMP_NAME = "name";
48 constexpr const char *COMP_TYPE = "type";
49 constexpr const char *COMP_HANDLER_LOC = "comp_handler_loc";
50 constexpr const char *COMP_HANDLER_VERSION = "comp_handler_version";
51 constexpr const char *COMP_SOURCE_LOC = "comp_source_loc";
52 constexpr const char *COMP_SOURCE_VERSION = "comp_source_version";
53 constexpr const char *COMP_SOURCE_SA_ID = "comp_source_sa_id";
54 constexpr const char *COMP_SINK_LOC = "comp_sink_loc";
55 constexpr const char *COMP_SINK_VERSION = "comp_sink_version";
56 constexpr const char *COMP_SINK_SA_ID = "comp_sink_sa_id";
57 constexpr const char *COMP_RESOURCE_DESC = "comp_resource_desc";
58 constexpr const char *COMP_SUBTYPE = "subtype";
59 constexpr const char *COMP_SENSITIVE = "sensitive";
60 constexpr const char *COMPONENTSLOAD_DISTRIBUTED_COMPONENTS = "distributed_components";
61
62 constexpr int32_t DEFAULT_SA_ID = -1;
63 constexpr uint32_t MAX_COMP_SIZE = 128;
64
65 std::map<std::string, DHType> g_mapDhTypeName = {
66 { "UNKNOWN", DHType::UNKNOWN },
67 { "CAMERA", DHType::CAMERA },
68 { "AUDIO", DHType::AUDIO },
69 { "SCREEN", DHType::SCREEN },
70 { "GPS", DHType::GPS },
71 { "INPUT", DHType::INPUT },
72 { "HFP", DHType::HFP },
73 { "A2D", DHType::A2D },
74 { "VIRMODEM_AUDIO", DHType::VIRMODEM_AUDIO },
75 { "MODEM", DHType::MODEM },
76 };
77
78 std::map<DHType, std::string> g_mapPartsParamName = {
79 { DHType::CAMERA, "sys.dhfwk.component.dcamera.enable" },
80 { DHType::AUDIO, "sys.dhfwk.component.daudio.enable" },
81 { DHType::SCREEN, "sys.dhfwk.component.dscreen.enable" },
82 { DHType::INPUT, "sys.dhfwk.component.dinput.enable" },
83 };
84 }
85
Init()86 int32_t ComponentLoader::Init()
87 {
88 DHLOGI("start");
89 DHTraceStart(COMPONENT_LOAD_START);
90 int32_t ret = ParseConfig();
91 StoreLocalDHVersionInDB();
92 DHTraceEnd();
93
94 return ret;
95 }
96
GetAllCompTypes(std::vector<DHType> & dhTypeVec)97 void ComponentLoader::GetAllCompTypes(std::vector<DHType> &dhTypeVec)
98 {
99 std::lock_guard<std::mutex> lock(compHandlerMapMutex_);
100 for (std::map<DHType, CompHandler>::iterator it = compHandlerMap_.begin(); it != compHandlerMap_.end(); ++it) {
101 dhTypeVec.push_back(it->first);
102 }
103 }
104
GetCompVersionFromComConfig(const CompConfig & comCfg,CompVersion & compVersion)105 void ComponentLoader::GetCompVersionFromComConfig(const CompConfig &comCfg, CompVersion &compVersion)
106 {
107 compVersion.dhType = comCfg.type;
108 compVersion.name = comCfg.name;
109 compVersion.handlerVersion = comCfg.compHandlerVersion;
110 compVersion.sinkVersion = comCfg.compSinkVersion;
111 compVersion.sourceVersion = comCfg.compSourceVersion;
112 compVersion.haveFeature = comCfg.haveFeature;
113 compVersion.sourceFeatureFilters = comCfg.sourceFeatureFilters;
114 compVersion.sinkSupportedFeatures = comCfg.sinkSupportedFeatures;
115 }
116
CheckComponentEnable(const CompConfig & config)117 bool ComponentLoader::CheckComponentEnable(const CompConfig &config)
118 {
119 auto item = g_mapPartsParamName.find(config.type);
120 if (item == g_mapPartsParamName.end()) {
121 DHLOGI("Crrent component is enabled by default.");
122 return true;
123 }
124 bool isEnable = false;
125 if (!GetSysPara((item->second).c_str(), isEnable)) {
126 DHLOGE("sys para: %{public}s get failed.", (item->second).c_str());
127 return false;
128 }
129 DHLOGI("Component type: %{public}u, enable flag: %{public}d.", config.type, isEnable);
130 return isEnable;
131 }
132
GetCompPathAndVersion(const std::string & jsonStr,std::map<DHType,CompConfig> & dhtypeMap)133 int32_t ComponentLoader::GetCompPathAndVersion(const std::string &jsonStr, std::map<DHType, CompConfig> &dhtypeMap)
134 {
135 if (!IsJsonLengthValid(jsonStr)) {
136 return ERR_DH_FWK_PARA_INVALID;
137 }
138 cJSON *root = cJSON_Parse(jsonStr.c_str());
139 if (root == NULL) {
140 DHLOGE("jsonStr parse failed");
141 return ERR_DH_FWK_JSON_PARSE_FAILED;
142 }
143 cJSON *components = cJSON_GetObjectItem(root, COMPONENTSLOAD_DISTRIBUTED_COMPONENTS);
144 if (!IsArray(components)) {
145 DHLOGE("distributed_components is not an array");
146 cJSON_Delete(root);
147 return ERR_DH_FWK_PARA_INVALID;
148 }
149
150 size_t compSize = static_cast<size_t>(cJSON_GetArraySize(components));
151 if (compSize == 0 || compSize > MAX_COMP_SIZE) {
152 DHLOGE("CompConfig size is invalid!");
153 cJSON_Delete(root);
154 return ERR_DH_FWK_PARA_INVALID;
155 }
156 cJSON *component = nullptr;
157 cJSON_ArrayForEach(component, components) {
158 CompConfig config;
159 ParseCompConfigFromJson(component, config);
160 dhtypeMap.insert(std::pair<DHType, CompConfig>(config.type, config));
161 CompVersion compVersion;
162 GetCompVersionFromComConfig(config, compVersion);
163 localDHVersion_.compVersions.insert(std::pair<DHType, CompVersion>(config.type, compVersion));
164 }
165 cJSON_Delete(root);
166 isLocalVersionInit_.store(true);
167 return DH_FWK_SUCCESS;
168 }
169
ParseCompConfigFromJson(cJSON * component,CompConfig & config)170 void ComponentLoader::ParseCompConfigFromJson(cJSON *component, CompConfig &config)
171 {
172 cJSON *nameJson = cJSON_GetObjectItem(component, COMP_NAME);
173 if (IsString(nameJson)) {
174 config.name = nameJson->valuestring;
175 }
176 cJSON *typeJson = cJSON_GetObjectItem(component, COMP_TYPE);
177 if (IsString(typeJson)) {
178 config.type = g_mapDhTypeName[typeJson->valuestring];
179 }
180 cJSON *handlerLocJson = cJSON_GetObjectItem(component, COMP_HANDLER_LOC);
181 if (IsString(handlerLocJson)) {
182 config.compHandlerLoc = handlerLocJson->valuestring;
183 }
184 cJSON *handlerVerJson = cJSON_GetObjectItem(component, COMP_HANDLER_VERSION);
185 if (IsString(handlerVerJson)) {
186 config.compHandlerVersion = handlerVerJson->valuestring;
187 }
188 cJSON *sourceLocJson = cJSON_GetObjectItem(component, COMP_SOURCE_LOC);
189 if (IsString(sourceLocJson)) {
190 config.compSourceLoc = sourceLocJson->valuestring;
191 }
192 cJSON *sourceVerJson = cJSON_GetObjectItem(component, COMP_SOURCE_VERSION);
193 if (IsString(sourceVerJson)) {
194 config.compSourceVersion = sourceVerJson->valuestring;
195 }
196 cJSON *sourceSaIdJson = cJSON_GetObjectItem(component, COMP_SOURCE_SA_ID);
197 if (IsInt32(sourceSaIdJson)) {
198 config.compSourceSaId = static_cast<int32_t>(sourceSaIdJson->valueint);
199 }
200 cJSON *sinkLocJson = cJSON_GetObjectItem(component, COMP_SINK_LOC);
201 if (IsString(sinkLocJson)) {
202 config.compSinkLoc = sinkLocJson->valuestring;
203 }
204 cJSON *sinkVerJson = cJSON_GetObjectItem(component, COMP_SINK_VERSION);
205 if (IsString(sinkVerJson)) {
206 config.compSinkVersion = sinkVerJson->valuestring;
207 }
208 cJSON *sinkSaIdJson = cJSON_GetObjectItem(component, COMP_SINK_SA_ID);
209 if (IsInt32(sinkSaIdJson)) {
210 config.compSinkSaId = static_cast<int32_t>(sinkSaIdJson->valueint);
211 }
212 cJSON *resourceDescs = cJSON_GetObjectItem(component, COMP_RESOURCE_DESC);
213 if (IsArray(resourceDescs)) {
214 ParseResourceDescFromJson(resourceDescs, config);
215 }
216 CheckAndParseFeatures(component, config);
217 }
218
ParseResourceDescFromJson(cJSON * resourceDescs,CompConfig & config)219 void ComponentLoader::ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig &config)
220 {
221 cJSON *resourceDesc = nullptr;
222 cJSON_ArrayForEach(resourceDesc, resourceDescs) {
223 bool sensitiveValue;
224 cJSON *sensitive = cJSON_GetObjectItem(resourceDesc, COMP_SENSITIVE);
225 if (!IsBool(sensitive)) {
226 DHLOGE("COMP_SUBTYPE is invalid!");
227 return;
228 }
229 if (cJSON_IsTrue(sensitive)) {
230 sensitiveValue = true;
231 } else {
232 sensitiveValue = false;
233 }
234 ResourceDesc resource;
235 cJSON *subtypeJson = cJSON_GetObjectItem(resourceDesc, COMP_SUBTYPE);
236 if (!IsString(subtypeJson)) {
237 DHLOGE("COMP_SUBTYPE is invalid!");
238 return;
239 }
240 resource.subtype = subtypeJson->valuestring;
241 resource.sensitiveValue = sensitiveValue;
242 config.compResourceDesc.push_back(resource);
243 }
244 }
245
ParseSourceFeatureFiltersFromJson(cJSON * sourceFeatureFilters,CompConfig & config)246 void ComponentLoader::ParseSourceFeatureFiltersFromJson(cJSON *sourceFeatureFilters, CompConfig &config)
247 {
248 cJSON *filter = nullptr;
249 config.sourceFeatureFilters.clear();
250 cJSON_ArrayForEach(filter, sourceFeatureFilters) {
251 if (filter != nullptr && filter->type == cJSON_String) {
252 config.sourceFeatureFilters.push_back(std::string(filter->valuestring));
253 }
254 }
255 }
256
ParseSinkSupportedFeaturesFromJson(cJSON * sinkSupportedFeatures,CompConfig & config)257 void ComponentLoader::ParseSinkSupportedFeaturesFromJson(cJSON *sinkSupportedFeatures, CompConfig &config)
258 {
259 cJSON *feature = nullptr;
260 config.sinkSupportedFeatures.clear();
261 cJSON_ArrayForEach(feature, sinkSupportedFeatures) {
262 if (feature != nullptr && feature->type == cJSON_String) {
263 config.sinkSupportedFeatures.push_back(std::string(feature->valuestring));
264 }
265 }
266 }
267
CheckAndParseFeatures(cJSON * component,CompConfig & config)268 void ComponentLoader::CheckAndParseFeatures(cJSON *component, CompConfig &config)
269 {
270 cJSON *sourceFeatureFilters = cJSON_GetObjectItem(component, SOURCE_FEATURE_FILTER);
271 cJSON *sinkSupportedFeatures = cJSON_GetObjectItem(component, SINK_SUPPORTED_FEATURE);
272 if (sourceFeatureFilters || sinkSupportedFeatures) {
273 config.haveFeature = true;
274 if (IsArray(sourceFeatureFilters)) {
275 ParseSourceFeatureFiltersFromJson(sourceFeatureFilters, config);
276 }
277 if (IsArray(sinkSupportedFeatures)) {
278 ParseSinkSupportedFeaturesFromJson(sinkSupportedFeatures, config);
279 }
280 } else {
281 config.haveFeature = false;
282 }
283 }
284
GetLocalDHVersion(DHVersion & dhVersion)285 int32_t ComponentLoader::GetLocalDHVersion(DHVersion &dhVersion)
286 {
287 if (!isLocalVersionInit_.load()) {
288 DHLOGE("get local DHVersion fail");
289 return ERR_DH_FWK_LOADER_GET_LOCAL_VERSION_FAIL;
290 }
291 dhVersion = localDHVersion_;
292 return DH_FWK_SUCCESS;
293 }
294
StoreLocalDHVersionInDB()295 void ComponentLoader::StoreLocalDHVersionInDB()
296 {
297 if (!isLocalVersionInit_.load()) {
298 DHLOGE("Store local DHVersion fail");
299 return;
300 }
301 VersionInfo versionInfo;
302 versionInfo.dhVersion = VersionManager::GetInstance().GetLocalDeviceVersion();
303 versionInfo.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId;
304 versionInfo.compVersions = localDHVersion_.compVersions;
305 VersionInfoManager::GetInstance()->AddVersion(versionInfo);
306 }
307
GetHandler(const std::string & soName)308 void *ComponentLoader::GetHandler(const std::string &soName)
309 {
310 if (soName.length() == 0 || soName.length() > PATH_MAX) {
311 DHLOGE("File canonicalization failed, soName: %{public}s", soName.c_str());
312 return nullptr;
313 }
314 void *pHandler = dlopen(soName.c_str(), RTLD_LAZY | RTLD_NODELETE);
315 if (pHandler == nullptr) {
316 DHLOGE("so: %{public}s load failed, failed reason: %{public}s", soName.c_str(), dlerror());
317 HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
318 "dhfwk so open failed, soname : " + soName);
319 return nullptr;
320 }
321 return pHandler;
322 }
323
GetAllHandler(std::map<DHType,CompConfig> & dhtypeMap)324 void ComponentLoader::GetAllHandler(std::map<DHType, CompConfig> &dhtypeMap)
325 {
326 std::lock_guard<std::mutex> lock(compHandlerMapMutex_);
327 std::map<DHType, CompConfig>::iterator itor;
328 for (itor = dhtypeMap.begin(); itor != dhtypeMap.end(); ++itor) {
329 CompHandler comHandler;
330 comHandler.type = itor->second.type;
331 comHandler.sourceSaId = itor->second.compSourceSaId;
332 comHandler.sinkSaId = itor->second.compSinkSaId;
333 std::vector<ResourceDesc> compResourceDesc = itor->second.compResourceDesc;
334 for (auto it = compResourceDesc.begin(); it != compResourceDesc.end(); it++) {
335 resDescMap_[it->subtype] = it->sensitiveValue;
336 }
337 comHandler.compConfig = itor->second;
338 compHandlerMap_[itor->second.type] = comHandler;
339 }
340 }
341
GetHardwareHandler(const DHType dhType,IHardwareHandler * & hardwareHandlerPtr)342 int32_t ComponentLoader::GetHardwareHandler(const DHType dhType, IHardwareHandler *&hardwareHandlerPtr)
343 {
344 std::lock_guard<std::mutex> lock(compHandlerMapMutex_);
345 auto iter = compHandlerMap_.find(dhType);
346 if (iter == compHandlerMap_.end()) {
347 DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType);
348 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
349 }
350 CompHandler &compHandler = iter->second;
351 if (compHandler.hardwareHandler == nullptr) {
352 compHandler.hardwareHandler = GetHandler(compHandler.compConfig.compHandlerLoc);
353 if (compHandler.hardwareHandler == nullptr) {
354 DHLOGE("get hardware handler is null, dhType: %{public}" PRIu32, (uint32_t)dhType);
355 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
356 }
357 }
358 GetHardwareClass getHardwareClassHandler = (GetHardwareClass)dlsym(compHandler.hardwareHandler,
359 COMPONENT_LOADER_GET_HARDWARE_HANDLER.c_str());
360 if (getHardwareClassHandler == nullptr) {
361 DHLOGE("get getHardwareClassHandler is null, failed reason : %{public}s", dlerror());
362 ReleaseHandler(compHandler.hardwareHandler);
363 compHandler.hardwareHandler = nullptr;
364 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
365 }
366 hardwareHandlerPtr = getHardwareClassHandler();
367 return DH_FWK_SUCCESS;
368 }
369
GetSource(const DHType dhType,IDistributedHardwareSource * & sourcePtr)370 int32_t ComponentLoader::GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr)
371 {
372 std::lock_guard<std::mutex> lock(compHandlerMapMutex_);
373 auto iter = compHandlerMap_.find(dhType);
374 if (iter == compHandlerMap_.end()) {
375 DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType);
376 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
377 }
378 CompHandler &compHandler = iter->second;
379 if (compHandler.sourceHandler == nullptr) {
380 compHandler.sourceHandler = GetHandler(compHandler.compConfig.compSourceLoc);
381 if (compHandler.sourceHandler == nullptr) {
382 DHLOGE("get source handler is null, dhType: %{public}" PRIu32, (uint32_t)dhType);
383 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
384 }
385 }
386 GetSourceHardwareClass getSourceHardClassHandler = (GetSourceHardwareClass)dlsym(
387 compHandler.sourceHandler, COMPONENT_LOADER_GET_SOURCE_HANDLER.c_str());
388 if (getSourceHardClassHandler == nullptr) {
389 DHLOGE("get getSourceHardClassHandler is null, failed reason : %{public}s", dlerror());
390 ReleaseHandler(compHandler.sourceHandler);
391 compHandler.sourceHandler = nullptr;
392 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
393 }
394 sourcePtr = getSourceHardClassHandler();
395 return DH_FWK_SUCCESS;
396 }
397
GetSink(const DHType dhType,IDistributedHardwareSink * & sinkPtr)398 int32_t ComponentLoader::GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr)
399 {
400 std::lock_guard<std::mutex> lock(compHandlerMapMutex_);
401 auto iter = compHandlerMap_.find(dhType);
402 if (iter == compHandlerMap_.end()) {
403 DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType);
404 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
405 }
406 CompHandler &compHandler = iter->second;
407 if (compHandler.sinkHandler == nullptr) {
408 compHandler.sinkHandler = GetHandler(compHandler.compConfig.compSinkLoc);
409 if (compHandler.sinkHandler == nullptr) {
410 DHLOGE("get sink handler is null, dhType: %{public}" PRIu32, (uint32_t)dhType);
411 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
412 }
413 }
414 GetSinkHardwareClass getSinkHardwareClassHandler =
415 (GetSinkHardwareClass)dlsym(compHandler.sinkHandler, COMPONENT_LOADER_GET_SINK_HANDLER.c_str());
416 if (getSinkHardwareClassHandler == nullptr) {
417 DHLOGE("get getSinkHardwareClassHandler is null, failed reason : %{public}s", dlerror());
418 ReleaseHandler(compHandler.sinkHandler);
419 compHandler.sinkHandler = nullptr;
420 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
421 }
422 sinkPtr = getSinkHardwareClassHandler();
423 return DH_FWK_SUCCESS;
424 }
425
Readfile(const std::string & filePath)426 std::string ComponentLoader::Readfile(const std::string &filePath)
427 {
428 std::ifstream infile;
429 std::string sLine;
430 std::string sAll = "";
431 infile.open(filePath);
432 if (!infile.is_open()) {
433 DHLOGE("filePath: %{public}s Readfile fail", filePath.c_str());
434 return sAll;
435 }
436
437 while (getline(infile, sLine)) {
438 sAll.append(sLine);
439 }
440 infile.close();
441 return sAll;
442 }
443
ParseConfig()444 int32_t ComponentLoader::ParseConfig()
445 {
446 std::map<DHType, CompConfig> dhtypeMap;
447 int32_t ret;
448 DHLOGI("ParseConfig start");
449 char buf[MAX_PATH_LEN] = {0};
450 char path[PATH_MAX + 1] = {0x00};
451 char *profilePath = GetOneCfgFile(COMPONENTSLOAD_PROFILE_PATH, buf, MAX_PATH_LEN);
452 if (profilePath == nullptr) {
453 DHLOGE("profilePath is null.");
454 return ERR_DH_FWK_LOADER_PROFILE_PATH_IS_NULL;
455 }
456
457 if (strlen(profilePath) == 0 || strlen(profilePath) > PATH_MAX || realpath(profilePath, path) == nullptr) {
458 std::string comProfilePath(profilePath);
459 DHLOGE("File connicailization failed, comProfilePath: %{public}s.", GetAnonyString(comProfilePath).c_str());
460 return ERR_DH_FWK_LOADER_PROFILE_PATH_IS_NULL;
461 }
462 std::string componentProfilePath(path);
463 std::string jsonStr = Readfile(componentProfilePath);
464 if (!IsMessageLengthValid(jsonStr)) {
465 return ERR_DH_FWK_LOADER_CONFIG_JSON_INVALID;
466 }
467 ret = GetCompPathAndVersion(jsonStr, dhtypeMap);
468 if (ret != DH_FWK_SUCCESS) {
469 return ret;
470 }
471 GetAllHandler(dhtypeMap);
472 return DH_FWK_SUCCESS;
473 }
474
ReleaseHandler(void * & handler)475 int32_t ComponentLoader::ReleaseHandler(void *&handler)
476 {
477 if (handler == nullptr) {
478 DHLOGE("handler is null.");
479 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
480 }
481
482 if (dlclose(handler) != 0) {
483 DHLOGE("dlclose failed.");
484 return ERR_DH_FWK_LOADER_DLCLOSE_FAIL;
485 }
486 handler = nullptr;
487 return DH_FWK_SUCCESS;
488 }
489
UnInit()490 int32_t ComponentLoader::UnInit()
491 {
492 DHLOGI("release all handler");
493 DHTraceStart(COMPONENT_RELEASE_START);
494 std::lock_guard<std::mutex> lock(compHandlerMapMutex_);
495 for (auto iter = compHandlerMap_.begin(); iter != compHandlerMap_.end(); ++iter) {
496 if (iter->second.sinkHandler) {
497 ReleaseHandler(iter->second.sinkHandler);
498 }
499 if (iter->second.sourceHandler) {
500 ReleaseHandler(iter->second.sourceHandler);
501 }
502 if (iter->second.hardwareHandler) {
503 ReleaseHandler(iter->second.hardwareHandler);
504 }
505 }
506 compHandlerMap_.clear();
507 resDescMap_.clear();
508 DHTraceEnd();
509 return DH_FWK_SUCCESS;
510 }
511
ReleaseHardwareHandler(const DHType dhType)512 int32_t ComponentLoader::ReleaseHardwareHandler(const DHType dhType)
513 {
514 if (!IsDHTypeExist(dhType)) {
515 return ERR_DH_FWK_TYPE_NOT_EXIST;
516 }
517 if (!IsDHTypeHandlerLoaded(dhType)) {
518 return ERR_DH_FWK_LOADER_HANDLER_UNLOAD;
519 }
520 int32_t ret = ReleaseHandler(compHandlerMap_[dhType].hardwareHandler);
521 if (ret) {
522 DHLOGE("fail, dhType: %{public}#X", dhType);
523 HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
524 dhType, ret, "dhfwk release hardware handler failed.");
525 }
526 compHandlerMap_[dhType].hardwareHandler = nullptr;
527 return ret;
528 }
529
ReleaseSource(const DHType dhType)530 int32_t ComponentLoader::ReleaseSource(const DHType dhType)
531 {
532 if (!IsDHTypeExist(dhType)) {
533 return ERR_DH_FWK_TYPE_NOT_EXIST;
534 }
535 if (!IsDHTypeSourceLoaded(dhType)) {
536 return ERR_DH_FWK_LOADER_SOURCE_UNLOAD;
537 }
538 int32_t ret = ReleaseHandler(compHandlerMap_[dhType].sourceHandler);
539 if (ret) {
540 DHLOGE("fail, dhType: %{public}#X", dhType);
541 HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
542 dhType, ret, "dhfwk release source failed.");
543 }
544 compHandlerMap_[dhType].sourceHandler = nullptr;
545 return ret;
546 }
547
ReleaseSink(const DHType dhType)548 int32_t ComponentLoader::ReleaseSink(const DHType dhType)
549 {
550 if (!IsDHTypeExist(dhType)) {
551 return ERR_DH_FWK_TYPE_NOT_EXIST;
552 }
553 if (!IsDHTypeSinkLoaded(dhType)) {
554 return ERR_DH_FWK_LOADER_SINK_UNLOAD;
555 }
556 int32_t ret = ReleaseHandler(compHandlerMap_[dhType].sinkHandler);
557 if (ret) {
558 DHLOGE("fail, dhType: %{public}#X", dhType);
559 HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
560 dhType, ret, "dhfwk release sink failed.");
561 }
562 compHandlerMap_[dhType].sinkHandler = nullptr;
563 return ret;
564 }
565
IsDHTypeExist(DHType dhType)566 bool ComponentLoader::IsDHTypeExist(DHType dhType)
567 {
568 if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) {
569 DHLOGE("fail, dhType: %{public}#X not exist", dhType);
570 return false;
571 }
572 return true;
573 }
574
IsDHTypeSinkLoaded(DHType dhType)575 bool ComponentLoader::IsDHTypeSinkLoaded(DHType dhType)
576 {
577 if (compHandlerMap_[dhType].sinkHandler == nullptr) {
578 DHLOGE("fail, dhType: %{public}#X sink not loaded", dhType);
579 return false;
580 }
581 return true;
582 }
583
IsDHTypeSourceLoaded(DHType dhType)584 bool ComponentLoader::IsDHTypeSourceLoaded(DHType dhType)
585 {
586 if (compHandlerMap_[dhType].sourceHandler == nullptr) {
587 DHLOGE("fail, dhType: %{public}#X source not loaded", dhType);
588 return false;
589 }
590 return true;
591 }
592
IsDHTypeHandlerLoaded(DHType dhType)593 bool ComponentLoader::IsDHTypeHandlerLoaded(DHType dhType)
594 {
595 if (compHandlerMap_[dhType].hardwareHandler == nullptr) {
596 DHLOGE("fail, dhType: %{public}#X handler not loaded", dhType);
597 return false;
598 }
599 return true;
600 }
601
GetSourceSaId(const DHType dhType)602 int32_t ComponentLoader::GetSourceSaId(const DHType dhType)
603 {
604 std::lock_guard<std::mutex> lock(compHandlerMapMutex_);
605 if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) {
606 DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType);
607 return DEFAULT_SA_ID;
608 }
609 return compHandlerMap_[dhType].sourceSaId;
610 }
611
GetDHTypeBySrcSaId(const int32_t saId)612 DHType ComponentLoader::GetDHTypeBySrcSaId(const int32_t saId)
613 {
614 std::lock_guard<std::mutex> lock(compHandlerMapMutex_);
615 DHType type = DHType::UNKNOWN;
616 for (const auto &handler : compHandlerMap_) {
617 if (handler.second.sourceSaId == saId) {
618 type = handler.second.type;
619 break;
620 }
621 }
622 return type;
623 }
624
GetCompResourceDesc()625 std::map<std::string, bool> ComponentLoader::GetCompResourceDesc()
626 {
627 return resDescMap_;
628 }
629
GetSource(const DHType dhType)630 int32_t ComponentLoader::GetSource(const DHType dhType)
631 {
632 std::lock_guard<std::mutex> lock(compHandlerMapMutex_);
633 auto iter = compHandlerMap_.find(dhType);
634 if (iter == compHandlerMap_.end()) {
635 DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType);
636 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
637 }
638 CompHandler &compHandler = iter->second;
639 if (compHandler.sourceHandler != nullptr) {
640 DHLOGE("sourceHandler is loaded.");
641 return ERR_DH_FWK_LOADER_SOURCE_LOAD;
642 }
643 compHandler.sourceHandler = GetHandler(compHandler.compConfig.compSourceLoc);
644 return DH_FWK_SUCCESS;
645 }
646
GetSink(const DHType dhType)647 int32_t ComponentLoader::GetSink(const DHType dhType)
648 {
649 std::lock_guard<std::mutex> lock(compHandlerMapMutex_);
650 auto iter = compHandlerMap_.find(dhType);
651 if (iter == compHandlerMap_.end()) {
652 DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType);
653 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
654 }
655 CompHandler &compHandler = iter->second;
656 if (compHandler.sinkHandler != nullptr) {
657 DHLOGE("sinkHandler is loaded.");
658 return ERR_DH_FWK_LOADER_SINK_LOAD;
659 }
660 compHandler.sinkHandler = GetHandler(compHandler.compConfig.compSinkLoc);
661 return DH_FWK_SUCCESS;
662 }
663
GetHardwareHandler(const DHType dhType)664 int32_t ComponentLoader::GetHardwareHandler(const DHType dhType)
665 {
666 std::lock_guard<std::mutex> lock(compHandlerMapMutex_);
667 auto iter = compHandlerMap_.find(dhType);
668 if (iter == compHandlerMap_.end()) {
669 DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType);
670 return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
671 }
672 CompHandler &compHandler = iter->second;
673 if (compHandler.hardwareHandler != nullptr) {
674 DHLOGE("hardwareHandler is loaded.");
675 return ERR_DH_FWK_LOADER_HANDLER_LOAD;
676 }
677 compHandler.hardwareHandler = GetHandler(compHandler.compConfig.compHandlerLoc);
678 return DH_FWK_SUCCESS;
679 }
680
IsDHTypeSupport(DHType dhType)681 bool ComponentLoader::IsDHTypeSupport(DHType dhType)
682 {
683 return IsDHTypeExist(dhType);
684 }
685 } // namespace DistributedHardware
686 } // namespace OHOS
687