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