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_CAPABILITY_INFO_MANAGER_H 17 #define OHOS_DISTRIBUTED_HARDWARE_CAPABILITY_INFO_MANAGER_H 18 19 #include <condition_variable> 20 #include <map> 21 #include <set> 22 23 #include "kvstore_observer.h" 24 25 #include "capability_info.h" 26 #include "capability_info_event.h" 27 #include "capability_utils.h" 28 #include "db_adapter.h" 29 #include "event_handler.h" 30 #include "single_instance.h" 31 32 class DBAdapter; 33 namespace OHOS { 34 namespace DistributedHardware { 35 class CapabilityInfoManager : public std::enable_shared_from_this<CapabilityInfoManager>, 36 public DistributedKv::KvStoreObserver { 37 public: 38 CapabilityInfoManager(const CapabilityInfoManager &) = delete; 39 CapabilityInfoManager &operator = (const CapabilityInfoManager &) = delete; 40 CapabilityInfoManager(CapabilityInfoManager &&) = delete; 41 CapabilityInfoManager &operator = (CapabilityInfoManager &&) = delete; 42 static std::shared_ptr<CapabilityInfoManager> GetInstance(); 43 virtual ~CapabilityInfoManager(); 44 int32_t Init(); 45 int32_t UnInit(); 46 /* update the database record to memory */ 47 int32_t SyncDeviceInfoFromDB(const std::string &deviceId); 48 /* update the database record to memory in abnormal scene */ 49 int32_t SyncRemoteCapabilityInfos(); 50 /* Add Distributed hardware information, Save in memory and database */ 51 int32_t AddCapability(const std::vector<std::shared_ptr<CapabilityInfo>> &resInfos); 52 /* Save CapabilityInfo in memory */ 53 int32_t AddCapabilityInMem(const std::vector<std::shared_ptr<CapabilityInfo>> &resInfos); 54 /* Deleting Database Records */ 55 int32_t RemoveCapabilityInfoInDB(const std::string &deviceId); 56 /* Deleting Database Records by key */ 57 int32_t RemoveCapabilityInfoByKey(const std::string &key); 58 /* Delete data from memory cache */ 59 int32_t RemoveCapabilityInfoInMem(const std::string &deviceId); 60 /* Queries distributed hardware information based on filter criteria. */ 61 std::map<std::string, std::shared_ptr<CapabilityInfo>> QueryCapabilityByFilters( 62 const std::map<CapabilityInfoFilter, std::string> &filters); 63 bool IsCapabilityMatchFilter(const std::shared_ptr<CapabilityInfo> &cap, const CapabilityInfoFilter &filter, 64 const std::string &value); 65 bool HasCapability(const std::string &deviceId, const std::string &dhId); 66 void GetCapabilitiesByDeviceId(const std::string &deviceId, 67 std::vector<std::shared_ptr<CapabilityInfo>> &resInfos); 68 69 /* Queries capability information based on deviceId and dhId. */ 70 int32_t GetCapability(const std::string &deviceId, const std::string &dhId, 71 std::shared_ptr<CapabilityInfo> &capPtr); 72 int32_t GetDataByKey(const std::string &key, std::shared_ptr<CapabilityInfo>& capInfoPtr); 73 /* Query batch records by dhtype */ 74 int32_t GetDataByDHType(const DHType dhType, CapabilityInfoMap &capabilityMap); 75 /* Queries batch records in the database based on the prefix of the key. */ 76 int32_t GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap); 77 /* Database data changes callback */ 78 virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; 79 80 class CapabilityInfoManagerEventHandler : public AppExecFwk::EventHandler { 81 public: 82 CapabilityInfoManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> &runner, 83 std::shared_ptr<CapabilityInfoManager> versionInfoMgrPtr); 84 ~CapabilityInfoManagerEventHandler() override = default; 85 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 86 private: 87 std::weak_ptr<CapabilityInfoManager> capabilityInfoMgrWPtr_; 88 }; 89 std::shared_ptr<CapabilityInfoManager::CapabilityInfoManagerEventHandler> GetEventHandler(); 90 91 void DumpCapabilityInfos(std::vector<CapabilityInfo> &capInfos); 92 93 private: 94 CapabilityInfoManager(); 95 void HandleCapabilityAddChange(const std::vector<DistributedKv::Entry> &insertRecords); 96 void HandleCapabilityUpdateChange(const std::vector<DistributedKv::Entry> &updateRecords); 97 void HandleCapabilityDeleteChange(const std::vector<DistributedKv::Entry> &deleteRecords); 98 99 private: 100 mutable std::mutex capInfoMgrMutex_; 101 std::shared_ptr<DBAdapter> dbAdapterPtr_; 102 CapabilityInfoMap globalCapInfoMap_; 103 104 std::shared_ptr<CapabilityInfoManager::CapabilityInfoManagerEventHandler> eventHandler_; 105 }; 106 } // namespace DistributedHardware 107 } // namespace OHOS 108 #endif 109