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