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 #ifndef OHOS_DISTRIBUTED_HARDWARE_COMPONENT_LOADER_H
17 #define OHOS_DISTRIBUTED_HARDWARE_COMPONENT_LOADER_H
18 
19 #include <atomic>
20 #include <map>
21 #include <vector>
22 
23 #include "single_instance.h"
24 #include "distributed_hardware_errno.h"
25 #include "device_type.h"
26 #include "ihardware_handler.h"
27 #include "idistributed_hardware_sink.h"
28 #include "idistributed_hardware_source.h"
29 #include "utils/impl_utils.h"
30 #include "cJSON.h"
31 
32 namespace OHOS {
33 namespace DistributedHardware {
34 struct ResourceDesc {
35     std::string subtype;
36     bool sensitiveValue;
37 };
38 
39 namespace {
40 struct CompHandler {
41     DHType type;
42     void *sourceHandler;
43     int32_t sourceSaId;
44     void *sinkHandler;
45     int32_t sinkSaId;
46     void *hardwareHandler;
47     std::vector<ResourceDesc> resourceDesc;
48 };
49 }
50 
51 struct CompConfig {
52     std::string name;
53     DHType type;
54     std::string compHandlerLoc;
55     std::string compHandlerVersion;
56     std::string compSourceLoc;
57     std::string compSourceVersion;
58     int32_t compSourceSaId;
59     std::string compSinkLoc;
60     std::string compSinkVersion;
61     int32_t compSinkSaId;
62     std::vector<ResourceDesc> compResourceDesc;
63 };
64 
65 class ComponentLoader {
66     DECLARE_SINGLE_INSTANCE_BASE(ComponentLoader);
67 
68 public:
ComponentLoader()69     ComponentLoader() : isLocalVersionInit_(false) {}
~ComponentLoader()70     ~ComponentLoader() {}
71 
72 public:
73     int32_t Init();
74     int32_t GetHardwareHandler(const DHType dhType, IHardwareHandler *&hardwareHandlerPtr);
75     int32_t GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr);
76     int32_t GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr);
77     int32_t UnInit();
78     int32_t ReleaseHardwareHandler(const DHType dhType);
79     int32_t ReleaseSource(const DHType dhType);
80     int32_t ReleaseSink(const DHType dhType);
81     std::vector<DHType> GetAllCompTypes();
82     int32_t GetLocalDHVersion(DHVersion &dhVersion);
83     int32_t GetSourceSaId(const DHType dhType);
84     DHType GetDHTypeBySrcSaId(const int32_t saId);
85     std::map<std::string, bool> GetCompResourceDesc();
86 
87 private:
88     void *GetHandler(const std::string &soName);
89     void GetAllHandler(std::map<DHType, CompConfig> &dhtypeMap);
90     int32_t ReleaseHandler(void *&handler);
91     int32_t GetCompPathAndVersion(const std::string &jsonStr, std::map<DHType, CompConfig> &dhtypeMap);
92     CompVersion GetCompVersionFromComConfig(const CompConfig& cCfg);
93     int32_t ParseConfig();
94     void StoreLocalDHVersionInDB();
95     bool IsDHTypeExist(DHType dhType);
96     std::string Readfile(const std::string &filePath);
97     void ParseCompConfigFromJson(cJSON *component, CompConfig &config);
98     void ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig &config);
99     bool CheckComponentEnable(const CompConfig &config);
100 
101 private:
102     DHVersion localDHVersion_;
103     std::map<DHType, CompHandler> compHandlerMap_;
104     std::atomic<bool> isLocalVersionInit_;
105     std::map<std::string, bool> resDescMap_;
106 };
107 } // namespace DistributedHardware
108 } // namespace OHOS
109 #endif
110