1 /*
2  * Copyright (c) 2022-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_LISTENER_H
17 #define OHOS_DM_SOFTBUS_LISTENER_H
18 
19 #include <map>
20 #include <memory>
21 #include <mutex>
22 #include <set>
23 #include <string>
24 #include <sstream>
25 #include <type_traits>
26 #include <vector>
27 
28 #include "softbus_bus_center.h"
29 #include "dm_device_info.h"
30 #include "dm_publish_info.h"
31 #include "dm_radar_helper.h"
32 #include "dm_subscribe_info.h"
33 #include "i_softbus_discovering_callback.h"
34 #include "inner_session.h"
35 #include "session.h"
36 #include "socket.h"
37 
38 namespace OHOS {
39 namespace DistributedHardware {
40 
41 class SoftbusListener {
42 public:
43     SoftbusListener();
44     ~SoftbusListener();
45 
46     static void OnSoftbusDeviceFound(const DeviceInfo *device);
47     static void OnSoftbusDiscoveryResult(int subscribeId, RefreshResult result);
48     static void OnSoftbusDeviceOnline(NodeBasicInfo *info);
49     static void OnSoftbusDeviceOffline(NodeBasicInfo *info);
50     static void OnSoftbusDeviceInfoChanged(NodeBasicInfoType type, NodeBasicInfo *info);
51     static void OnSoftbusPublishResult(int publishId, PublishResult result);
52     static void DeviceOnLine(DmDeviceInfo deviceInfo);
53     static void DeviceOffLine(DmDeviceInfo deviceInfo);
54     static void DeviceNameChange(DmDeviceInfo deviceInfo);
55     static void OnLocalDevInfoChange();
56     static void OnDeviceNotTrusted(const char *msg);
57     static void DeviceNotTrust(const std::string &msg);
58     static void OnDeviceScreenStatusChanged(NodeStatusType type, NodeStatus *status);
59     static void DeviceScreenStatusChange(DmDeviceInfo deviceInfo);
60     static void OnCredentialAuthStatus(const char *proofInfo, uint32_t proofLen, uint16_t deviceTypeId,
61                                        int32_t errcode);
62     static void CredentialAuthStatusProcess(std::string proofInfo, uint16_t deviceTypeId, int32_t errcode);
63 
64     static void CacheDiscoveredDevice(const DeviceInfo *device);
65     static void ClearDiscoveredDevice();
66     static void ConvertDeviceInfoToDmDevice(const DeviceInfo &device, DmDeviceInfo &dmDevice);
67     static int32_t GetUdidByNetworkId(const char *networkId, std::string &udid);
68     static int32_t GetTargetInfoFromCache(const std::string &deviceId, PeerTargetId &targetId,
69         ConnectionAddrType &addrType);
70     static int32_t ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeInfo, DmDeviceInfo &devInfo);
71     static int32_t ConvertNodeBasicInfoToDmDevice(const NodeBasicInfo &nodeInfo, DmDeviceBasicInfo &devInfo);
72     static std::string ConvertBytesToUpperCaseHexString(const uint8_t arr[], const size_t size);
73     static int32_t ConvertScreenStatusToDmDevice(const NodeBasicInfo &nodeInfo, const int32_t devScreenStatus,
74         DmDeviceInfo &devInfo);
75 
76     int32_t InitSoftbusListener();
77     int32_t GetTrustedDeviceList(std::vector<DmDeviceInfo> &deviceInfoList);
78     int32_t GetLocalDeviceInfo(DmDeviceInfo &deviceInfo);
79     int32_t GetDeviceInfo(const std::string &networkId, DmDeviceInfo &info);
80     int32_t ShiftLNNGear(bool isWakeUp, const std::string &callerId);
81     static int32_t GetUuidByNetworkId(const char *networkId, std::string &uuid);
82     int32_t GetDeviceSecurityLevel(const char *networkId, int32_t &securityLevel);
83     int32_t GetNetworkTypeByNetworkId(const char *networkId, int32_t &networkType);
84     int32_t RefreshSoftbusLNN(const char *pkgName, const DmSubscribeInfo &dmSubInfo, const std::string &customData);
85     int32_t StopRefreshSoftbusLNN(uint16_t subscribeId);
86     int32_t PublishSoftbusLNN(const DmPublishInfo &dmPubInfo, const std::string &capability,
87         const std::string &customData);
88     int32_t StopPublishSoftbusLNN(int32_t publishId);
89     int32_t RegisterSoftbusLnnOpsCbk(const std::string &pkgName,
90         const std::shared_ptr<ISoftbusDiscoveringCallback> callback);
91     int32_t UnRegisterSoftbusLnnOpsCbk(const std::string &pkgName);
92     static IDmRadarHelper* GetDmRadarHelperObj();
93     static bool IsDmRadarHelperReady();
94     static bool CloseDmRadarHelperObj(std::string name);
95     static void CacheDeviceInfo(const std::string deviceId, std::shared_ptr<DeviceInfo> infoPtr);
96     static int32_t GetIPAddrTypeFromCache(const std::string &deviceId, const std::string &ip,
97         ConnectionAddrType &addrType);
98     static IRefreshCallback &GetSoftbusRefreshCb();
99     static void SetHostPkgName(const std::string hostName);
100     static std::string GetHostPkgName();
101     static int32_t GetNetworkIdByUdid(const std::string &udid, std::string &networkId);
102 
103     int32_t GetDeviceScreenStatus(const char *networkId, int32_t &screenStatus);
104 private:
105     int32_t InitSoftPublishLNN();
106 
107 private:
108     static std::string hostName_;
109     static bool isRadarSoLoad_;
110     static IDmRadarHelper *dmRadarHelper_;
111     static void *radarHandle_;
112 };
113 } // namespace DistributedHardware
114 } // namespace OHOS
115 #endif // OHOS_DM_SOFTBUS_LISTENER_H
116