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