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