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_DM_SOFTBUS_CACHE_H
17#define OHOS_DM_SOFTBUS_CACHE_H
18
19#include <cstdint>
20#include <string>
21#include <mutex>
22#include "dm_device_info.h"
23#include "dm_single_instance.h"
24#include "softbus_bus_center.h"
25#include "softbus_common.h"
26
27namespace OHOS {
28namespace DistributedHardware {
29class SoftbusCache {
30    DM_DECLARE_SINGLE_INSTANCE(SoftbusCache);
31
32public:
33    void SaveDeviceInfo(DmDeviceInfo deviceInfo);
34    void DeleteDeviceInfo(const DmDeviceInfo &nodeInfo);
35    void ChangeDeviceInfo(const DmDeviceInfo deviceInfo);
36    void SaveDeviceSecurityLevel(const char *networkId);
37    void DeleteDeviceSecurityLevel(const char *networkId);
38    int32_t GetDeviceInfoFromCache(std::vector<DmDeviceInfo> &deviceInfoList);
39    int32_t GetUdidFromCache(const char *networkId, std::string &udid);
40    int32_t GetUuidFromCache(const char *networkId, std::string &uuid);
41    int32_t GetSecurityDeviceLevel(const char *networkId, int32_t &securityLevel);
42    void UpdateDeviceInfoCache();
43    void SaveLocalDeviceInfo();
44    void DeleteLocalDeviceInfo();
45    int32_t GetLocalDeviceInfo(DmDeviceInfo &nodeInfo);
46    int32_t GetDevInfoByNetworkId(const std::string &networkId, DmDeviceInfo &nodeInfo);
47    void UpDataLocalDevInfo();
48    int32_t GetUdidByUdidHash(const std::string &udidHash, std::string &udid);
49    int32_t GetUuidByUdid(const std::string &udid, std::string &uuid);
50    int32_t GetNetworkIdFromCache(const std::string &udid, std::string &networkId);
51    bool CheckIsOnline(const std::string &udidHash);
52private:
53    int32_t GetUdidByNetworkId(const char *networkId, std::string &udid);
54    int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid);
55    int32_t ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeInfo, DmDeviceInfo &devInfo);
56    int32_t GetDevInfoFromBus(const std::string &networkId, DmDeviceInfo &devInfo);
57    int32_t GetDevLevelFromBus(const char *networkId, int32_t &securityLevel);
58
59private:
60    std::mutex deviceInfosMutex_;
61    std::mutex deviceSecurityLevelMutex_;
62    std::unordered_map<std::string, std::pair<std::string, DmDeviceInfo>> deviceInfo_;
63    std::unordered_map<std::string, int32_t> deviceSecurityLevel_;
64};
65} // namespace DistributedHardware
66} // namespace OHOS
67#endif // OHOS_DM_PERMISSION_STANDARD_PERMISSION_MANAGER_H
68