• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
31 namespace OHOS {
32 namespace DistributedHardware {
33 namespace {
34 struct CompHandler {
35     DHType type;
36     void *sourceHandler;
37     int32_t sourceSaId;
38     void *sinkHandler;
39     int32_t sinkSaId;
40     void *hardwareHandler;
41 };
42 }
43 
44 struct CompConfig {
45     std::string name;
46     DHType type;
47     std::string compHandlerLoc;
48     std::string compHandlerVersion;
49     std::string compSourceLoc;
50     std::string compSourceVersion;
51     int32_t compSourceSaId;
52     std::string compSinkLoc;
53     std::string compSinkVersion;
54     int32_t compSinkSaId;
55 };
56 
57 class ComponentLoader {
58     DECLARE_SINGLE_INSTANCE_BASE(ComponentLoader);
59 
60 public:
ComponentLoader()61     ComponentLoader() : isLocalVersionInit_(false) {}
~ComponentLoader()62     ~ComponentLoader() {}
63 
64 public:
65     int32_t Init();
66     int32_t GetHardwareHandler(const DHType dhType, IHardwareHandler *&hardwareHandlerPtr);
67     int32_t GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr);
68     int32_t GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr);
69     int32_t UnInit();
70     int32_t ReleaseHardwareHandler(const DHType dhType);
71     int32_t ReleaseSource(const DHType dhType);
72     int32_t ReleaseSink(const DHType dhType);
73     std::vector<DHType> GetAllCompTypes();
74     int32_t GetLocalDHVersion(DHVersion &dhVersion);
75     int32_t GetSourceSaId(const DHType dhType);
76     DHType GetDHTypeBySrcSaId(const int32_t saId);
77 
78 private:
79     void *GetHandler(const std::string &soName);
80     void GetAllHandler(std::map<DHType, CompConfig> &dhtypeMap);
81     int32_t ReleaseHandler(void *&handler);
82     int32_t GetCompPathAndVersion(const std::string &jsonStr, std::map<DHType, CompConfig> &dhtypeMap);
83     CompVersion GetCompVersionFromComConfig(const CompConfig& cCfg);
84     int32_t ParseConfig();
85     void StoreLocalDHVersionInDB();
86     bool IsDHTypeExist(DHType dhType);
87     std::string Readfile(const std::string &filePath);
88 
89 private:
90     DHVersion localDHVersion_;
91     std::map<DHType, CompHandler> compHandlerMap_;
92     std::atomic<bool> isLocalVersionInit_;
93 };
94 } // namespace DistributedHardware
95 } // namespace OHOS
96 #endif
97