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