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.h" 30 #include "eventbus_handler.h" 31 #include "event_bus.h" 32 #include "event_sender.h" 33 #include "single_instance.h" 34 35 class DBAdapter; 36 namespace OHOS { 37 namespace DistributedHardware { 38 class CapabilityInfoManager : public std::enable_shared_from_this<CapabilityInfoManager>, 39 public EventSender, 40 public DistributedKv::KvStoreObserver, 41 public EventBusHandler<CapabilityInfoEvent> { 42 public: 43 CapabilityInfoManager(const CapabilityInfoManager &) = delete; 44 CapabilityInfoManager &operator = (const CapabilityInfoManager &) = delete; 45 CapabilityInfoManager(CapabilityInfoManager &&) = delete; 46 CapabilityInfoManager &operator = (CapabilityInfoManager &&) = delete; 47 static std::shared_ptr<CapabilityInfoManager> GetInstance(); 48 virtual ~CapabilityInfoManager(); 49 int32_t Init(); 50 int32_t UnInit(); 51 /* update the database record to memory */ 52 int32_t SyncDeviceInfoFromDB(const std::string &deviceId); 53 /* update the database record to memory in abnormal scene */ 54 int32_t SyncRemoteCapabilityInfos(); 55 /* Add Distributed hardware information, Save in memory and database */ 56 int32_t AddCapability(const std::vector<std::shared_ptr<CapabilityInfo>> &resInfos); 57 /* Save CapabilityInfo in memory */ 58 int32_t AddCapabilityInMem(const std::vector<std::shared_ptr<CapabilityInfo>> &resInfos); 59 /* Deleting Database Records */ 60 int32_t RemoveCapabilityInfoInDB(const std::string &deviceId); 61 /* Deleting Database Records by key */ 62 int32_t RemoveCapabilityInfoByKey(const std::string &key); 63 /* Delete data from memory cache */ 64 int32_t RemoveCapabilityInfoInMem(const std::string &deviceId); 65 /* Queries distributed hardware information based on filter criteria. */ 66 std::map<std::string, std::shared_ptr<CapabilityInfo>> QueryCapabilityByFilters( 67 const std::map<CapabilityInfoFilter, std::string> &filters); 68 bool IsCapabilityMatchFilter(const std::shared_ptr<CapabilityInfo> &cap, const CapabilityInfoFilter &filter, 69 const std::string &value); 70 bool HasCapability(const std::string &deviceId, const std::string &dhId); 71 void GetCapabilitiesByDeviceId(const std::string &deviceId, 72 std::vector<std::shared_ptr<CapabilityInfo>> &resInfos); 73 74 /* Queries capability information based on deviceId and dhId. */ 75 int32_t GetCapability(const std::string &deviceId, const std::string &dhId, 76 std::shared_ptr<CapabilityInfo> &capPtr); 77 int32_t GetDataByKey(const std::string &key, std::shared_ptr<CapabilityInfo>& capInfoPtr); 78 /* Query batch records by dhtype */ 79 int32_t GetDataByDHType(const DHType dhType, CapabilityInfoMap &capabilityMap); 80 /* Queries batch records in the database based on the prefix of the key. */ 81 int32_t GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap); 82 /* Init the count of manual sync times */ 83 void CreateManualSyncCount(const std::string &deviceId); 84 /* Clearing the count of manual sync times */ 85 void RemoveManualSyncCount(const std::string &deviceId); 86 /* Actively synchronizes data */ 87 int32_t ManualSync(const std::string &networkId); 88 /* Database data changes callback */ 89 virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override; 90 /* EventBus async processing callback */ 91 void OnEvent(CapabilityInfoEvent &e) override; 92 93 void DumpCapabilityInfos(std::vector<CapabilityInfo> &capInfos); 94 95 private: 96 CapabilityInfoManager(); 97 void HandleCapabilityAddChange(const std::vector<DistributedKv::Entry> &insertRecords); 98 void HandleCapabilityUpdateChange(const std::vector<DistributedKv::Entry> &updateRecords); 99 void HandleCapabilityDeleteChange(const std::vector<DistributedKv::Entry> &deleteRecords); 100 101 private: 102 mutable std::mutex capInfoMgrMutex_; 103 std::shared_ptr<DBAdapter> dbAdapterPtr_; 104 CapabilityInfoMap globalCapInfoMap_; 105 }; 106 } // namespace DistributedHardware 107 } // namespace OHOS 108 #endif 109