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_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 bool HasCapability(const std::string &deviceId, const std::string &dhId); 60 void GetCapabilitiesByDeviceId(const std::string &deviceId, 61 std::vector<std::shared_ptr<CapabilityInfo>> &resInfos); 62 63 /* Queries capability information based on deviceId and dhId. */ 64 int32_t GetCapability(const std::string &deviceId, const std::string &dhId, 65 std::shared_ptr<CapabilityInfo> &capPtr); 66 int32_t GetDataByKey(const std::string &key, std::shared_ptr<CapabilityInfo> &capInfoPtr); 67 /* Query batch records by dhtype */ 68 int32_t GetDataByDHType(const DHType dhType, CapabilityInfoMap &capabilityMap); 69 /* Queries batch records in the database based on the prefix of the key. */ 70 int32_t GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap); 71 /* Database data changes callback */ 72 virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; 73 /* Cloud data changes callback */ 74 void OnChange(const DistributedKv::DataOrigin &origin, Keys &&keys) override; 75 76 class CapabilityInfoManagerEventHandler : public AppExecFwk::EventHandler { 77 public: 78 CapabilityInfoManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner, 79 std::shared_ptr<CapabilityInfoManager> capabilityInfoMgrPtr); 80 ~CapabilityInfoManagerEventHandler() override = default; 81 void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override; 82 private: 83 std::weak_ptr<CapabilityInfoManager> capabilityInfoMgrWPtr_; 84 }; 85 std::shared_ptr<CapabilityInfoManager::CapabilityInfoManagerEventHandler> GetEventHandler(); 86 87 void DumpCapabilityInfos(std::vector<CapabilityInfo> &capInfos); 88 89 private: 90 void HandleCapabilityAddChange(const std::vector<DistributedKv::Entry> &insertRecords); 91 void HandleCapabilityUpdateChange(const std::vector<DistributedKv::Entry> &updateRecords); 92 void HandleCapabilityDeleteChange(const std::vector<DistributedKv::Entry> &deleteRecords); 93 std::vector<DistributedKv::Entry> GetEntriesByKeys(const std::vector<std::string> &keys); 94 95 private: 96 mutable std::mutex capInfoMgrMutex_; 97 std::shared_ptr<DBAdapter> dbAdapterPtr_; 98 CapabilityInfoMap globalCapInfoMap_; 99 100 std::shared_ptr<CapabilityInfoManager::CapabilityInfoManagerEventHandler> eventHandler_; 101 }; 102 } // namespace DistributedHardware 103 } // namespace OHOS 104 #endif 105