1 /*
2  * Copyright (c) 2021-2024 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 const std::string COMP_NAME = "name";
48 const std::string COMP_TYPE = "type";
49 const std::string COMP_HANDLER_LOC = "comp_handler_loc";
50 const std::string COMP_HANDLER_VERSION = "comp_handler_version";
51 const std::string COMP_SOURCE_LOC = "comp_source_loc";
52 const std::string COMP_SOURCE_VERSION = "comp_source_version";
53 const std::string COMP_SOURCE_SA_ID = "comp_source_sa_id";
54 const std::string COMP_SINK_LOC = "comp_sink_loc";
55 const std::string COMP_SINK_VERSION = "comp_sink_version";
56 const std::string COMP_SINK_SA_ID = "comp_sink_sa_id";
57 const std::string COMP_RESOURCE_DESC = "comp_resource_desc";
58 const std::string COMP_SUBTYPE = "subtype";
59 const std::string COMP_SENSITIVE = "sensitive";
60 
61 const std::string COMPONENTSLOAD_DISTRIBUTED_COMPONENTS = "distributed_components";
62 
63 const std::string DEFAULT_NAME = "";
64 const std::string DEFAULT_TYPE = "UNKNOWN";
65 const std::string DEFAULT_LOC = "";
66 const int32_t DEFAULT_SA_ID = -1;
67 const std::string DEFAULT_VERSION = "1.0";
68 
69 std::map<std::string, DHType> g_mapDhTypeName = {
70     { "UNKNOWN", DHType::UNKNOWN },
71     { "CAMERA", DHType::CAMERA },
72     { "AUDIO", DHType::AUDIO },
73     { "SCREEN", DHType::SCREEN },
74     { "GPS", DHType::GPS },
75     { "INPUT", DHType::INPUT },
76     { "HFP", DHType::HFP },
77     { "A2D", DHType::A2D },
78     { "VIRMODEM_AUDIO", DHType::VIRMODEM_AUDIO },
79     { "MODEM", DHType::MODEM },
80 };
81 
82 std::map<DHType, std::string> g_mapPartsParamName = {
83     { DHType::CAMERA, "sys.dhfwk.component.dcamera.enable" },
84     { DHType::AUDIO, "sys.dhfwk.component.daudio.enable" },
85     { DHType::SCREEN, "sys.dhfwk.component.dscreen.enable" },
86     { DHType::INPUT, "sys.dhfwk.component.dinput.enable" },
87 };
88 }
89 
Init()90 int32_t ComponentLoader::Init()
91 {
92     DHLOGI("start");
93     DHTraceStart(COMPONENT_LOAD_START);
94     int32_t ret = ParseConfig();
95     StoreLocalDHVersionInDB();
96     DHTraceEnd();
97 
98     return ret;
99 }
100 
GetAllCompTypes()101 std::vector<DHType> ComponentLoader::GetAllCompTypes()
102 {
103     std::vector<DHType> DHTypeALL;
104     for (std::map<DHType, CompHandler>::iterator it = compHandlerMap_.begin(); it != compHandlerMap_.end(); ++it) {
105         DHTypeALL.push_back(it->first);
106     }
107     return DHTypeALL;
108 }
109 
ParseComponent(const cJSON *json, CompConfig &cfg)110 int32_t ParseComponent(const cJSON *json, CompConfig &cfg)
111 {
112     if (!IsString(json, COMP_NAME)) {
113         DHLOGE("COMP_NAME is invalid!");
114         return ERR_DH_FWK_JSON_PARSE_FAILED;
115     }
116     cfg.name = cJSON_GetObjectItem(json, COMP_NAME.c_str())->valuestring;
117     if (!IsString(json, COMP_TYPE)) {
118         DHLOGE("COMP_TYPE is invalid!");
119         return ERR_DH_FWK_JSON_PARSE_FAILED;
120     }
121     cfg.type = g_mapDhTypeName[cJSON_GetObjectItem(json, COMP_TYPE.c_str())->valuestring];
122     if (!IsString(json, COMP_HANDLER_LOC)) {
123         DHLOGE("COMP_HANDLER_LOC is invalid!");
124         return ERR_DH_FWK_JSON_PARSE_FAILED;
125     }
126     cfg.compHandlerLoc = cJSON_GetObjectItem(json, COMP_HANDLER_LOC.c_str())->valuestring;
127     if (!IsString(json, COMP_HANDLER_VERSION)) {
128         DHLOGE("COMP_HANDLER_VERSION is invalid!");
129         return ERR_DH_FWK_JSON_PARSE_FAILED;
130     }
131     cfg.compHandlerVersion = cJSON_GetObjectItem(json, COMP_HANDLER_VERSION.c_str())->valuestring;
132     return DH_FWK_SUCCESS;
133 }
134 
ParseSource(const cJSON *json, CompConfig &cfg)135 int32_t ParseSource(const cJSON *json, CompConfig &cfg)
136 {
137     if (!IsString(json, COMP_SOURCE_LOC)) {
138         DHLOGE("COMP_SOURCE_LOC is invalid!");
139         return ERR_DH_FWK_JSON_PARSE_FAILED;
140     }
141     cfg.compSourceLoc = cJSON_GetObjectItem(json, COMP_SOURCE_LOC.c_str())->valuestring;
142     if (!IsString(json, COMP_SOURCE_VERSION)) {
143         DHLOGE("COMP_SOURCE_VERSION is invalid!");
144         return ERR_DH_FWK_JSON_PARSE_FAILED;
145     }
146     cfg.compSourceVersion = cJSON_GetObjectItem(json, COMP_SOURCE_VERSION.c_str())->valuestring;
147     if (!IsInt32(json, COMP_SOURCE_SA_ID)) {
148         DHLOGE("COMP_SOURCE_SA_ID is invalid!");
149         return ERR_DH_FWK_JSON_PARSE_FAILED;
150     }
151     cfg.compSourceSaId = static_cast<int32_t>(cJSON_GetObjectItem(json, COMP_SOURCE_SA_ID.c_str())->valuedouble);
152     return DH_FWK_SUCCESS;
153 }
154 
ParseSink(const cJSON *json, CompConfig &cfg)155 int32_t ParseSink(const cJSON *json, CompConfig &cfg)
156 {
157     if (!IsString(json, COMP_SINK_LOC)) {
158         DHLOGE("COMP_SINK_LOC is invalid!");
159         return ERR_DH_FWK_JSON_PARSE_FAILED;
160     }
161     cfg.compSinkLoc = cJSON_GetObjectItem(json, COMP_SINK_LOC.c_str())->valuestring;
162     if (!IsString(json, COMP_SINK_VERSION)) {
163         DHLOGE("COMP_SINK_VERSION is invalid!");
164         return ERR_DH_FWK_JSON_PARSE_FAILED;
165     }
166     cfg.compSinkVersion = cJSON_GetObjectItem(json, COMP_SINK_VERSION.c_str())->valuestring;
167     if (!IsInt32(json, COMP_SINK_SA_ID)) {
168         DHLOGE("COMP_SINK_SA_ID is invalid!");
169         return ERR_DH_FWK_JSON_PARSE_FAILED;
170     }
171     cfg.compSinkSaId = static_cast<int32_t>(cJSON_GetObjectItem(json, COMP_SINK_SA_ID.c_str())->valuedouble);
172     return DH_FWK_SUCCESS;
173 }
174 
ParseResourceDesc(const cJSON *json, CompConfig &cfg)175 int32_t ParseResourceDesc(const cJSON *json, CompConfig &cfg)
176 {
177     if (!IsArray(json, COMP_RESOURCE_DESC)) {
178         DHLOGE("COMP_RESOURCE_DESC is invalid!");
179         return ERR_DH_FWK_JSON_PARSE_FAILED;
180     }
181     cJSON *resourceDescArray = cJSON_GetObjectItem(json, COMP_RESOURCE_DESC.c_str());
182     cJSON *element = nullptr;
183     cJSON_ArrayForEach(element, resourceDescArray) {
184         ResourceDesc desc;
185         if (!IsString(element, COMP_SUBTYPE)) {
186             DHLOGE("COMP_SUBTYPE is invalid!");
187             return ERR_DH_FWK_JSON_PARSE_FAILED;
188         }
189         desc.subtype = cJSON_GetObjectItem(element, COMP_SUBTYPE.c_str())->valuestring;
190         cJSON *sensitive = cJSON_GetObjectItem(element, COMP_SENSITIVE.c_str());
191         if (cJSON_IsTrue(sensitive)) {
192             desc.sensitiveValue = true;
193         } else {
194             desc.sensitiveValue = false;
195         }
196         cfg.compResourceDesc.push_back(desc);
197     }
198     return DH_FWK_SUCCESS;
199 }
200 
from_json(const cJSON *json, CompConfig &cfg)201 void from_json(const cJSON *json, CompConfig &cfg)
202 {
203     if (ParseComponent(json, cfg) != DH_FWK_SUCCESS) {
204         DHLOGE("ParseComponent is failed");
205         return;
206     }
207     if (ParseSource(json, cfg) != DH_FWK_SUCCESS) {
208         DHLOGE("ParseSource is failed");
209         return;
210     }
211     if (ParseSink(json, cfg) != DH_FWK_SUCCESS) {
212         DHLOGE("ParseSink is failed");
213         return;
214     }
215     if (ParseResourceDesc(json, cfg) != DH_FWK_SUCCESS) {
216         DHLOGE("ParseResourceDesc is failed");
217         return;
218     }
219 }
220 
GetCompVersionFromComConfig(const CompConfig& cCfg)221 CompVersion ComponentLoader::GetCompVersionFromComConfig(const CompConfig& cCfg)
222 {
223     CompVersion compVersions;
224     compVersions.dhType = cCfg.type;
225     compVersions.name = cCfg.name;
226     compVersions.handlerVersion = cCfg.compHandlerVersion;
227     compVersions.sinkVersion = cCfg.compSinkVersion;
228     compVersions.sourceVersion = cCfg.compSourceVersion;
229     return compVersions;
230 }
231 
CheckComponentEnable(const CompConfig &config)232 bool ComponentLoader::CheckComponentEnable(const CompConfig &config)
233 {
234     auto item = g_mapPartsParamName.find(config.type);
235     if (item == g_mapPartsParamName.end()) {
236         DHLOGI("Crrent component is enabled by default.");
237         return true;
238     }
239     bool isEnable = false;
240     if (!GetSysPara((item->second).c_str(), isEnable)) {
241         DHLOGE("sys para: %{public}s get failed.", (item->second).c_str());
242         return false;
243     }
244     DHLOGI("Component type: %{public}u, enable flag: %{public}d.", config.type, isEnable);
245     return isEnable;
246 }
247 
GetCompPathAndVersion(const std::string &jsonStr, std::map<DHType, CompConfig> &dhtypeMap)248 int32_t ComponentLoader::GetCompPathAndVersion(const std::string &jsonStr, std::map<DHType, CompConfig> &dhtypeMap)
249 {
250     if (!IsJsonLengthValid(jsonStr)) {
251         return ERR_DH_FWK_PARA_INVALID;
252     }
253     cJSON *root = cJSON_Parse(jsonStr.c_str());
254     if (root == NULL) {
255         DHLOGE("jsonStr parse failed");
256         return ERR_DH_FWK_JSON_PARSE_FAILED;
257     }
258     if (!IsArray(root, COMPONENTSLOAD_DISTRIBUTED_COMPONENTS)) {
259         DHLOGE("distributed_components is not an array");
260         cJSON_Delete(root);
261         return ERR_DH_FWK_PARA_INVALID;
262     }
263     cJSON *components = cJSON_GetObjectItem(root, COMPONENTSLOAD_DISTRIBUTED_COMPONENTS.c_str());
264 
265     size_t compSize = static_cast<size_t>(cJSON_GetArraySize(components));
266     if (compSize == 0 || compSize > MAX_COMP_SIZE) {
267         DHLOGE("CompConfig size is invalid!");
268         cJSON_Delete(root);
269         return ERR_DH_FWK_PARA_INVALID;
270     }
271     cJSON *component = nullptr;
272     cJSON_ArrayForEach(component, components) {
273         CompConfig config;
274         ParseCompConfigFromJson(component, config);
275         dhtypeMap.insert(std::pair<DHType, CompConfig>(config.type, config));
276         localDHVersion_.compVersions.insert(
277             std::pair<DHType, CompVersion>(config.type, GetCompVersionFromComConfig(config)));
278     }
279     cJSON_Delete(root);
280     isLocalVersionInit_.store(true);
281     return DH_FWK_SUCCESS;
282 }
283 
ParseCompConfigFromJson(cJSON *component, CompConfig &config)284 void ComponentLoader::ParseCompConfigFromJson(cJSON *component, CompConfig &config)
285 {
286     if (IsString(component, COMP_NAME.c_str())) {
287         config.name = cJSON_GetObjectItem(component, COMP_NAME.c_str())->valuestring;
288     }
289     if (IsString(component, COMP_TYPE.c_str())) {
290         config.type = g_mapDhTypeName[cJSON_GetObjectItem(component, COMP_TYPE.c_str())->valuestring];
291     }
292     if (IsString(component, COMP_HANDLER_LOC.c_str())) {
293         config.compHandlerLoc = cJSON_GetObjectItem(component, COMP_HANDLER_LOC.c_str())->valuestring;
294     }
295     if (IsString(component, COMP_HANDLER_VERSION.c_str())) {
296         config.compHandlerVersion = cJSON_GetObjectItem(component, COMP_HANDLER_VERSION.c_str())->valuestring;
297     }
298     if (IsString(component, COMP_SOURCE_LOC.c_str())) {
299         config.compSourceLoc = cJSON_GetObjectItem(component, COMP_SOURCE_LOC.c_str())->valuestring;
300     }
301     if (IsString(component, COMP_SOURCE_VERSION.c_str())) {
302         config.compSourceVersion = cJSON_GetObjectItem(component, COMP_SOURCE_VERSION.c_str())->valuestring;
303     }
304     if (IsInt32(component, COMP_SOURCE_SA_ID.c_str())) {
305         config.compSourceSaId =
306             static_cast<int32_t>(cJSON_GetObjectItem(component, COMP_SOURCE_SA_ID.c_str())->valuedouble);
307     }
308     if (IsString(component, COMP_SINK_LOC.c_str())) {
309         config.compSinkLoc = cJSON_GetObjectItem(component, COMP_SINK_LOC.c_str())->valuestring;
310     }
311     if (IsString(component, COMP_SINK_VERSION.c_str())) {
312         config.compSinkVersion = cJSON_GetObjectItem(component, COMP_SINK_VERSION.c_str())->valuestring;
313     }
314     if (IsInt32(component, COMP_SINK_SA_ID.c_str())) {
315         config.compSinkSaId =
316             static_cast<int32_t>(cJSON_GetObjectItem(component, COMP_SINK_SA_ID.c_str())->valuedouble);
317     }
318     if (IsArray(component, COMP_RESOURCE_DESC.c_str())) {
319         cJSON *resourceDescs = cJSON_GetObjectItem(component, COMP_RESOURCE_DESC.c_str());
320         ParseResourceDescFromJson(resourceDescs, config);
321     }
322 }
323 
ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig &config)324 void ComponentLoader::ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig &config)
325 {
326     cJSON *resourceDesc = nullptr;
327     cJSON_ArrayForEach(resourceDesc, resourceDescs) {
328         bool sensitiveValue;
329         cJSON *sensitive = cJSON_GetObjectItem(resourceDesc, COMP_SENSITIVE.c_str());
330         if (cJSON_IsTrue(sensitive)) {
331             sensitiveValue = true;
332         } else {
333             sensitiveValue = false;
334         }
335         ResourceDesc resource;
336         if (!IsString(resourceDesc, COMP_SUBTYPE)) {
337             DHLOGE("COMP_SUBTYPE is invalid!");
338             return;
339         }
340         resource.subtype = cJSON_GetObjectItem(resourceDesc, COMP_SUBTYPE.c_str())->valuestring;
341         resource.sensitiveValue = sensitiveValue;
342         config.compResourceDesc.push_back(resource);
343     }
344 }
345 
GetLocalDHVersion(DHVersion &dhVersion)346 int32_t ComponentLoader::GetLocalDHVersion(DHVersion &dhVersion)
347 {
348     if (!isLocalVersionInit_.load()) {
349         DHLOGE("get local DHVersion fail");
350         return ERR_DH_FWK_LOADER_GET_LOCAL_VERSION_FAIL;
351     }
352     dhVersion = localDHVersion_;
353     return DH_FWK_SUCCESS;
354 }
355 
StoreLocalDHVersionInDB()356 void ComponentLoader::StoreLocalDHVersionInDB()
357 {
358     if (!isLocalVersionInit_.load()) {
359         DHLOGE("Store local DHVersion fail");
360         return;
361     }
362     VersionInfo versionInfo;
363     versionInfo.dhVersion = VersionManager::GetInstance().GetLocalDeviceVersion();
364     versionInfo.deviceId = DHContext::GetInstance().GetDeviceInfo().deviceId;
365     versionInfo.compVersions = localDHVersion_.compVersions;
366     VersionInfoManager::GetInstance()->AddVersion(versionInfo);
367 }
368 
GetHandler(const std::string &soName)369 void *ComponentLoader::GetHandler(const std::string &soName)
370 {
371     if (soName.length() == 0 || soName.length() > PATH_MAX) {
372         DHLOGE("File canonicalization failed, soName: %{public}s", soName.c_str());
373         return nullptr;
374     }
375     void *pHandler = dlopen(soName.c_str(), RTLD_LAZY | RTLD_NODELETE);
376     if (pHandler == nullptr) {
377         DHLOGE("so: %{public}s load failed, failed reason: %{public}s", soName.c_str(), dlerror());
378         HiSysEventWriteMsg(DHFWK_INIT_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
379             "dhfwk so open failed, soname : " + soName);
380         return nullptr;
381     }
382     return pHandler;
383 }
384 
GetAllHandler(std::map<DHType, CompConfig> &dhtypeMap)385 void ComponentLoader::GetAllHandler(std::map<DHType, CompConfig> &dhtypeMap)
386 {
387     std::map<DHType, CompConfig>::iterator itor;
388     for (itor = dhtypeMap.begin(); itor != dhtypeMap.end(); ++itor) {
389         CompHandler comHandler;
390         comHandler.type = itor->second.type;
391         comHandler.hardwareHandler = GetHandler(itor->second.compHandlerLoc);
392         comHandler.sourceHandler = GetHandler(itor->second.compSourceLoc);
393         comHandler.sourceSaId = itor->second.compSourceSaId;
394         comHandler.sinkHandler = GetHandler(itor->second.compSinkLoc);
395         comHandler.sinkSaId = itor->second.compSinkSaId;
396         std::vector<ResourceDesc> compResourceDesc = itor->second.compResourceDesc;
397         for (auto it = compResourceDesc.begin(); it != compResourceDesc.end(); it++) {
398             resDescMap_[it->subtype] = it->sensitiveValue;
399         }
400         comHandler.resourceDesc = itor->second.compResourceDesc;
401         compHandlerMap_[itor->second.type] = comHandler;
402     }
403 }
404 
GetHardwareHandler(const DHType dhType, IHardwareHandler *&hardwareHandlerPtr)405 int32_t ComponentLoader::GetHardwareHandler(const DHType dhType, IHardwareHandler *&hardwareHandlerPtr)
406 {
407     if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) {
408         DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType);
409         return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
410     }
411 
412     if (compHandlerMap_[dhType].hardwareHandler == nullptr) {
413         DHLOGE("hardwareHandler is null.");
414         return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
415     }
416 
417     GetHardwareClass getHardwareClassHandler = (GetHardwareClass)dlsym(compHandlerMap_[dhType].hardwareHandler,
418         COMPONENT_LOADER_GET_HARDWARE_HANDLER.c_str());
419     if (getHardwareClassHandler == nullptr) {
420         DHLOGE("get getHardwareClassHandler is null, failed reason : %{public}s", dlerror());
421         dlclose(compHandlerMap_[dhType].hardwareHandler);
422         compHandlerMap_[dhType].hardwareHandler = nullptr;
423         return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
424     }
425     hardwareHandlerPtr = getHardwareClassHandler();
426     return DH_FWK_SUCCESS;
427 }
428 
GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr)429 int32_t ComponentLoader::GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr)
430 {
431     if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) {
432         DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType);
433         return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
434     }
435 
436     if (compHandlerMap_[dhType].sourceHandler == nullptr) {
437         DHLOGE("sourceHandler is null.");
438         return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
439     }
440 
441     GetSourceHardwareClass getSourceHardClassHandler = (GetSourceHardwareClass)dlsym(
442         compHandlerMap_[dhType].sourceHandler, COMPONENT_LOADER_GET_SOURCE_HANDLER.c_str());
443     if (getSourceHardClassHandler == nullptr) {
444         DHLOGE("get getSourceHardClassHandler is null, failed reason : %{public}s", dlerror());
445         dlclose(compHandlerMap_[dhType].sourceHandler);
446         compHandlerMap_[dhType].sourceHandler = nullptr;
447         return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
448     }
449     sourcePtr = getSourceHardClassHandler();
450     return DH_FWK_SUCCESS;
451 }
452 
GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr)453 int32_t ComponentLoader::GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr)
454 {
455     if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) {
456         DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType);
457         return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
458     }
459 
460     if (compHandlerMap_[dhType].sinkHandler == nullptr) {
461         DHLOGE("sinkHandler is null.");
462         return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
463     }
464 
465     GetSinkHardwareClass getSinkHardwareClassHandler =
466         (GetSinkHardwareClass)dlsym(compHandlerMap_[dhType].sinkHandler, COMPONENT_LOADER_GET_SINK_HANDLER.c_str());
467     if (getSinkHardwareClassHandler == nullptr) {
468         DHLOGE("get getSinkHardwareClassHandler is null, failed reason : %{public}s", dlerror());
469         dlclose(compHandlerMap_[dhType].sinkHandler);
470         compHandlerMap_[dhType].sinkHandler = nullptr;
471         return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
472     }
473     sinkPtr = getSinkHardwareClassHandler();
474     return DH_FWK_SUCCESS;
475 }
476 
Readfile(const std::string &filePath)477 std::string ComponentLoader::Readfile(const std::string &filePath)
478 {
479     std::ifstream infile;
480     std::string sLine;
481     std::string sAll = "";
482     infile.open(filePath);
483     if (!infile.is_open()) {
484         DHLOGE("filePath: %{public}s Readfile fail", filePath.c_str());
485         return sAll;
486     }
487 
488     while (getline(infile, sLine)) {
489         sAll.append(sLine);
490     }
491     infile.close();
492     return sAll;
493 }
494 
ParseConfig()495 int32_t ComponentLoader::ParseConfig()
496 {
497     std::map<DHType, CompConfig> dhtypeMap;
498     int32_t ret;
499     DHLOGI("ParseConfig start");
500     char buf[MAX_PATH_LEN] = {0};
501     char path[PATH_MAX + 1] = {0x00};
502     char *profilePath = GetOneCfgFile(COMPONENTSLOAD_PROFILE_PATH, buf, MAX_PATH_LEN);
503     if (profilePath == nullptr) {
504         DHLOGE("profilePath is null.");
505         return ERR_DH_FWK_LOADER_PROFILE_PATH_IS_NULL;
506     }
507 
508     if (strlen(profilePath) == 0 || strlen(profilePath) > PATH_MAX || realpath(profilePath, path) == nullptr) {
509         std::string comProfilePath(profilePath);
510         DHLOGE("File connicailization failed, comProfilePath: %{public}s.", GetAnonyString(comProfilePath).c_str());
511         return ERR_DH_FWK_LOADER_PROFILE_PATH_IS_NULL;
512     }
513     std::string componentProfilePath(path);
514     std::string jsonStr = Readfile(componentProfilePath);
515     if (!IsMessageLengthValid(jsonStr)) {
516         return ERR_DH_FWK_LOADER_CONFIG_JSON_INVALID;
517     }
518     ret = GetCompPathAndVersion(jsonStr, dhtypeMap);
519     if (ret != DH_FWK_SUCCESS) {
520         return ret;
521     }
522     GetAllHandler(dhtypeMap);
523     return DH_FWK_SUCCESS;
524 }
525 
ReleaseHandler(void *&handler)526 int32_t ComponentLoader::ReleaseHandler(void *&handler)
527 {
528     if (handler == nullptr) {
529         DHLOGE("handler is null.");
530         return ERR_DH_FWK_LOADER_HANDLER_IS_NULL;
531     }
532 
533     if (dlclose(handler) != 0) {
534         DHLOGE("dlclose failed.");
535         return ERR_DH_FWK_LOADER_DLCLOSE_FAIL;
536     }
537     handler = nullptr;
538     return DH_FWK_SUCCESS;
539 }
540 
UnInit()541 int32_t ComponentLoader::UnInit()
542 {
543     DHLOGI("release all handler");
544     DHTraceStart(COMPONENT_RELEASE_START);
545     int32_t ret = DH_FWK_SUCCESS;
546     for (std::map<DHType, CompHandler>::iterator iter = compHandlerMap_.begin();
547         iter != compHandlerMap_.end(); ++iter) {
548         ret += ReleaseHardwareHandler(iter->first);
549         ret += ReleaseSource(iter->first);
550         ret += ReleaseSink(iter->first);
551     }
552     compHandlerMap_.clear();
553     resDescMap_.clear();
554     DHTraceEnd();
555     return ret;
556 }
557 
ReleaseHardwareHandler(const DHType dhType)558 int32_t ComponentLoader::ReleaseHardwareHandler(const DHType dhType)
559 {
560     if (!IsDHTypeExist(dhType)) {
561         return ERR_DH_FWK_TYPE_NOT_EXIST;
562     }
563     int32_t ret = ReleaseHandler(compHandlerMap_[dhType].hardwareHandler);
564     if (ret) {
565         DHLOGE("fail, dhType: %{public}#X", dhType);
566         HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
567             dhType, ret, "dhfwk release hardware handler failed.");
568     }
569     return ret;
570 }
571 
ReleaseSource(const DHType dhType)572 int32_t ComponentLoader::ReleaseSource(const DHType dhType)
573 {
574     if (!IsDHTypeExist(dhType)) {
575         return ERR_DH_FWK_TYPE_NOT_EXIST;
576     }
577     int32_t ret = ReleaseHandler(compHandlerMap_[dhType].sourceHandler);
578     if (ret) {
579         DHLOGE("fail, dhType: %{public}#X", dhType);
580         HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
581             dhType, ret, "dhfwk release source failed.");
582     }
583     return ret;
584 }
585 
ReleaseSink(const DHType dhType)586 int32_t ComponentLoader::ReleaseSink(const DHType dhType)
587 {
588     if (!IsDHTypeExist(dhType)) {
589         return ERR_DH_FWK_TYPE_NOT_EXIST;
590     }
591     int32_t ret = ReleaseHandler(compHandlerMap_[dhType].sinkHandler);
592     if (ret) {
593         DHLOGE("fail, dhType: %{public}#X", dhType);
594         HiSysEventWriteReleaseMsg(DHFWK_RELEASE_FAIL, OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
595             dhType, ret, "dhfwk release sink failed.");
596     }
597     return ret;
598 }
599 
IsDHTypeExist(DHType dhType)600 bool ComponentLoader::IsDHTypeExist(DHType dhType)
601 {
602     if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) {
603         DHLOGE("fail, dhType: %{public}#X not exist", dhType);
604         return false;
605     }
606     return true;
607 }
608 
GetSourceSaId(const DHType dhType)609 int32_t ComponentLoader::GetSourceSaId(const DHType dhType)
610 {
611     if (compHandlerMap_.find(dhType) == compHandlerMap_.end()) {
612         DHLOGE("DHType not exist, dhType: %{public}" PRIu32, (uint32_t)dhType);
613         return DEFAULT_SA_ID;
614     }
615     return compHandlerMap_[dhType].sourceSaId;
616 }
617 
GetDHTypeBySrcSaId(const int32_t saId)618 DHType ComponentLoader::GetDHTypeBySrcSaId(const int32_t saId)
619 {
620     DHType type = DHType::UNKNOWN;
621     for (const auto &handler : compHandlerMap_) {
622         if (handler.second.sourceSaId == saId) {
623             type = handler.second.type;
624             break;
625         }
626     }
627     return type;
628 }
629 
GetCompResourceDesc()630 std::map<std::string, bool> ComponentLoader::GetCompResourceDesc()
631 {
632     return resDescMap_;
633 }
634 } // namespace DistributedHardware
635 } // namespace OHOS
636