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