1 /* 2 * Copyright (c) 2021-2025 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 CompConfig { 42 std::string name; 43 DHType type; 44 std::string compHandlerLoc; 45 std::string compHandlerVersion; 46 std::string compSourceLoc; 47 std::string compSourceVersion; 48 int32_t compSourceSaId; 49 std::string compSinkLoc; 50 std::string compSinkVersion; 51 int32_t compSinkSaId; 52 std::vector<ResourceDesc> compResourceDesc; 53 bool haveFeature; 54 std::vector<std::string> sourceFeatureFilters; 55 std::vector<std::string> sinkSupportedFeatures; 56 }; 57 58 struct CompHandler { 59 DHType type; 60 void *sourceHandler; 61 int32_t sourceSaId; 62 void *sinkHandler; 63 int32_t sinkSaId; 64 void *hardwareHandler; 65 CompConfig compConfig; 66 }; 67 } 68 69 class ComponentLoader { 70 DECLARE_SINGLE_INSTANCE_BASE(ComponentLoader); 71 72 public: ComponentLoader()73 ComponentLoader() : isLocalVersionInit_(false) {} ~ComponentLoader()74 ~ComponentLoader() {} 75 76 public: 77 int32_t Init(); 78 int32_t GetHardwareHandler(const DHType dhType, IHardwareHandler *&hardwareHandlerPtr); 79 int32_t GetSource(const DHType dhType, IDistributedHardwareSource *&sourcePtr); 80 int32_t GetSink(const DHType dhType, IDistributedHardwareSink *&sinkPtr); 81 int32_t UnInit(); 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 int32_t GetSource(const DHType dhType); 89 int32_t ReleaseSource(const DHType dhType); 90 int32_t GetSink(const DHType dhType); 91 int32_t ReleaseSink(const DHType dhType); 92 int32_t GetHardwareHandler(const DHType dhType); 93 int32_t ReleaseHardwareHandler(const DHType dhType); 94 bool IsDHTypeSupport(DHType dhType); 95 96 private: 97 void *GetHandler(const std::string &soName); 98 void GetAllHandler(std::map<DHType, CompConfig> &dhtypeMap); 99 int32_t ReleaseHandler(void *&handler); 100 int32_t GetCompPathAndVersion(const std::string &jsonStr, std::map<DHType, CompConfig> &dhtypeMap); 101 CompVersion GetCompVersionFromComConfig(const CompConfig& cCfg); 102 int32_t ParseConfig(); 103 void StoreLocalDHVersionInDB(); 104 bool IsDHTypeExist(DHType dhType); 105 bool IsDHTypeSinkLoaded(DHType dhType); 106 bool IsDHTypeSourceLoaded(DHType dhType); 107 bool IsDHTypeHandlerLoaded(DHType dhType); 108 std::string Readfile(const std::string &filePath); 109 void ParseCompConfigFromJson(cJSON *component, CompConfig &config); 110 void ParseResourceDescFromJson(cJSON *resourceDescs, CompConfig &config); 111 void ParseSourceFeatureFiltersFromJson(cJSON *sourceFeatureFilters, CompConfig &config); 112 void ParseSinkSupportedFeaturesFromJson(cJSON *sinkSupportedFeatures, CompConfig &config); 113 void CheckAndParseFeatures(cJSON *component, CompConfig &config); 114 bool CheckComponentEnable(const CompConfig &config); 115 116 private: 117 DHVersion localDHVersion_; 118 std::map<DHType, CompHandler> compHandlerMap_; 119 std::mutex compHandlerMapMutex_; 120 std::atomic<bool> isLocalVersionInit_; 121 std::map<std::string, bool> resDescMap_; 122 }; 123 } // namespace DistributedHardware 124 } // namespace OHOS 125 #endif 126