• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "iget_dh_descriptors_callback.h"
30 
31 class DBAdapter;
32 namespace OHOS {
33 namespace DistributedHardware {
34 class CapabilityInfoManager : public std::enable_shared_from_this<CapabilityInfoManager>,
35                               public DistributedKv::KvStoreObserver {
36 public:
37     CapabilityInfoManager(const CapabilityInfoManager &) = delete;
38     CapabilityInfoManager &operator = (const CapabilityInfoManager &) = delete;
39     CapabilityInfoManager(CapabilityInfoManager &&) = delete;
40     CapabilityInfoManager &operator = (CapabilityInfoManager &&) = delete;
41     static std::shared_ptr<CapabilityInfoManager> GetInstance();
42     CapabilityInfoManager();
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     bool HasCapability(const std::string &deviceId, const std::string &dhId);
61     void GetCapabilitiesByDeviceId(const std::string &deviceId,
62         std::vector<std::shared_ptr<CapabilityInfo>> &resInfos);
63 
64     /* Queries capability information based on deviceId and dhId. */
65     int32_t GetCapability(const std::string &deviceId, const std::string &dhId,
66         std::shared_ptr<CapabilityInfo> &capPtr);
67     int32_t GetDataByKey(const std::string &key, std::shared_ptr<CapabilityInfo> &capInfoPtr);
68     /* Query batch records by dhtype */
69     int32_t GetDataByDHType(const DHType dhType, CapabilityInfoMap &capabilityMap);
70     /* Queries batch records in the database based on the prefix of the key. */
71     int32_t GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap);
72     /* Database data changes callback */
73     virtual void OnChange(const DistributedKv::ChangeNotification &changeNotification) override;
74     /* Cloud data changes callback */
75     void OnChange(const DistributedKv::DataOrigin &origin, Keys &&keys) override;
76 
77     class CapabilityInfoManagerEventHandler : public AppExecFwk::EventHandler {
78         public:
79             CapabilityInfoManagerEventHandler(const std::shared_ptr<AppExecFwk::EventRunner> runner,
80                 std::shared_ptr<CapabilityInfoManager> capabilityInfoMgrPtr);
81             ~CapabilityInfoManagerEventHandler() override = default;
82             void ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event) override;
83         private:
84             std::weak_ptr<CapabilityInfoManager> capabilityInfoMgrWPtr_;
85     };
86     std::shared_ptr<CapabilityInfoManager::CapabilityInfoManagerEventHandler> GetEventHandler();
87 
88     void DumpCapabilityInfos(std::vector<CapabilityInfo> &capInfos);
89     void AsyncGetDistributedHardware(const std::string &networkId, EnableStep enableStep,
90         const sptr<IGetDhDescriptorsCallback> callback);
91     void DoAsyncGetDistributedHardware(const std::string &networkId, EnableStep enableStep,
92         const sptr<IGetDhDescriptorsCallback> callback);
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     std::mutex syncDataMutex_;
107     std::condition_variable syncDataCondVar_;
108 };
109 } // namespace DistributedHardware
110 } // namespace OHOS
111 #endif
112