1 /*
2  * Copyright (c) 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_LOCAL_CAPABILITY_INFO_MANAGER_H
17 #define OHOS_DISTRIBUTED_HARDWARE_LOCAL_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 
29 class DBAdapter;
30 namespace OHOS {
31 namespace DistributedHardware {
32 class LocalCapabilityInfoManager : public std::enable_shared_from_this<LocalCapabilityInfoManager>,
33     public DistributedKv::KvStoreObserver {
34 public:
35     LocalCapabilityInfoManager(const LocalCapabilityInfoManager &) = delete;
36     LocalCapabilityInfoManager &operator = (const LocalCapabilityInfoManager &) = delete;
37     LocalCapabilityInfoManager(LocalCapabilityInfoManager &&) = delete;
38     LocalCapabilityInfoManager &operator = (LocalCapabilityInfoManager &&) = delete;
39     static std::shared_ptr<LocalCapabilityInfoManager> GetInstance();
40     LocalCapabilityInfoManager();
41     virtual ~LocalCapabilityInfoManager();
42     int32_t Init();
43     int32_t UnInit();
44     /* update the database record to memory */
45     int32_t SyncDeviceInfoFromDB(const std::string &deviceId);
46     /* Add Distributed hardware information, Save in memory and database */
47     int32_t AddCapability(const std::vector<std::shared_ptr<CapabilityInfo>> &resInfos);
48     /* Deleting Database Records by key */
49     int32_t RemoveCapabilityInfoByKey(const std::string &key);
50     void GetCapabilitiesByDeviceId(const std::string &deviceId,
51         std::vector<std::shared_ptr<CapabilityInfo>> &resInfos);
52     /* Queries capability information based on deviceId and dhId. */
53     int32_t GetCapability(const std::string &deviceId, const std::string &dhId,
54         std::shared_ptr<CapabilityInfo> &capPtr);
55     int32_t GetDataByKey(const std::string &key, std::shared_ptr<CapabilityInfo> &capInfoPtr);
56     /* Query batch records by dhtype */
57     int32_t GetDataByDHType(const DHType dhType, CapabilityInfoMap &capabilityMap);
58     /* Queries batch records in the database based on the prefix of the key. */
59     int32_t GetDataByKeyPrefix(const std::string &keyPrefix, CapabilityInfoMap &capabilityMap);
60 
61 private:
62     mutable std::mutex capInfoMgrMutex_;
63     std::shared_ptr<DBAdapter> dbAdapterPtr_;
64     CapabilityInfoMap globalCapInfoMap_;
65 };
66 } // namespace DistributedHardware
67 } // namespace OHOS
68 #endif
69