1 /*
2  * Copyright (c) 2023-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_RADAR_HELPER_H
17 #define OHOS_DM_RADAR_HELPER_H
18 
19 #include <cstdint>
20 #include <chrono>
21 #include <string>
22 #include <vector>
23 
24 #include "dm_single_instance.h"
25 
26 #include "dm_device_info.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 const std::string ORGPKGNAME = "deviceManager";
31 const std::string SOFTBUSNAME = "dsoftbus";
32 const std::string HICHAINNAME = "hichain";
33 const std::string DM_DISCOVER_BEHAVIOR = "DM_DISCOVER_BEHAVIOR";
34 const std::string DM_AUTHCATION_BEHAVIOR = "DM_AUTHCATION_BEHAVIOR";
35 constexpr int32_t SUBSYS_DISTRIBUTEDHARDWARE_DM = 204;
36 constexpr int32_t INVALID_UDID_LENGTH = 10;
37 constexpr int32_t SUBSTR_UDID_LENGTH = 5;
38 enum class BizScene : int32_t {
39     DM_DISCOVER = 0x1,
40     DM_AUTHCATION = 0x2,
41     DM_NETWORK = 0x3,
42     DM_DELET_TRUST_RELATION = 0x4,
43     DM_PIN_HOLDER = 0x5,
44 };
45 
46 enum class StageRes : int32_t {
47     STAGE_IDLE = 0x0,
48     STAGE_SUCC = 0x1,
49     STAGE_FAIL = 0x2,
50     STAGE_CANCEL = 0x3,
51     STAGE_UNKNOW = 0x4,
52 };
53 
54 enum class BizState : int32_t {
55     BIZ_STATE_START = 0x1,
56     BIZ_STATE_END = 0x2,
57     BIZ_STATE_CANCEL = 0x3,
58 };
59 
60 enum class DisCoverStage : int32_t {
61     DISCOVER_REGISTER_CALLBACK = 0x1,
62     DISCOVER_USER_DEAL_RES = 0x2,
63     DISCOVER_GET_TRUST_DEVICE_LIST = 0x3,
64 };
65 
66 enum class AuthStage : int32_t {
67     AUTH_START = 0x1,
68     AUTH_OPEN_SESSION = 0x2,
69     AUTH_SEND_REQUEST = 0x3,
70     AUTH_PULL_AUTH_BOX = 0x4,
71     AUTH_CREATE_HICHAIN_GROUP = 0x5,
72     AUTH_PULL_PIN_BOX_START = 0x6,
73     AUTH_PULL_PIN_INPUT_BOX_END = 0x7,
74     AUTH_ADD_HICHAIN_GROUP = 0x8,
75 };
76 
77 enum class NetworkStage : int32_t {
78     NETWORK_ONLINE = 0x1,
79     NETWORK_OFFLINE = 0x2,
80 };
81 
82 enum class DeleteTrust : int32_t {
83     DELETE_TRUST = 0x1,
84 };
85 
86 enum class PinHolderStage : int32_t {
87     CREATE_PIN_HOLDER = 0x1,
88     SESSION_OPENED = 0x2,
89     SEND_CREATE_PIN_HOLDER_MSG = 0x3,
90     RECEIVE_CREATE_PIN_HOLDER_MSG = 0x4,
91     DESTROY_PIN_HOLDER = 0x5,
92     RECEIVE_DESTROY_PIN_HOLDER_MSG = 0x6,
93 };
94 
95 enum class GetTrustDeviceList : int32_t {
96     GET_TRUST_DEVICE_LIST = 0x1,
97 };
98 
99 enum class TrustStatus : int32_t {
100     NOT_TRUST = 0x0,
101     IS_TRUST = 0x1,
102 };
103 
104 enum class CommServ : int32_t {
105     NOT_USE_SOFTBUS = 0x0,
106     USE_SOFTBUS = 0x1,
107 };
108 
109 enum class Module : int32_t {
110     DEVICE_MANAGER = 0x0,
111     HICHAIN = 0x1,
112     SOFTBUS = 0x2,
113     USER = 0x3
114 };
115 
116 struct RadarInfo {
117     std::string funcName;
118     std::string toCallPkg;
119     std::string hostName;
120     int32_t stageRes;
121     int32_t bizState;
122     std::string localSessName;
123     std::string peerSessName;
124     int32_t isTrust;
125     int32_t commServ;
126     std::string peerNetId;
127     std::string localUdid;
128     std::string peerUdid;
129     std::string discoverDevList;
130     int32_t channelId;
131     int32_t errCode;
132 };
133 
134 class IDmRadarHelper {
135 public:
~IDmRadarHelper()136     virtual ~IDmRadarHelper() {}
137     /**
138      * @tc.name: ReportDiscoverRegCallback
139      * @tc.desc: report discover regsit callback
140      * @tc.type: FUNC
141      */
142     virtual bool ReportDiscoverRegCallback(struct RadarInfo &info) = 0;
143     virtual bool ReportDiscoverResCallback(struct RadarInfo &info) = 0;
144     virtual bool ReportDiscoverUserRes(struct RadarInfo &info) = 0;
145     virtual bool ReportAuthStart(const std::string &peerUdid, const std::string &pkgName) = 0;
146     virtual bool ReportAuthOpenSession(struct RadarInfo &info) = 0;
147     virtual bool ReportAuthSessionOpenCb(struct RadarInfo &info) = 0;
148     virtual bool ReportAuthSendRequest(struct RadarInfo &info) = 0;
149     virtual bool ReportAuthPullAuthBox(struct RadarInfo &info) = 0;
150     virtual bool ReportAuthConfirmBox(struct RadarInfo &info) = 0;
151     virtual bool ReportAuthCreateGroup(struct RadarInfo &info) = 0;
152     virtual bool ReportAuthCreateGroupCb(std::string funcName, int32_t stageRes) = 0;
153     virtual bool ReportAuthPullPinBox(struct RadarInfo &info) = 0;
154     virtual bool ReportAuthInputPinBox(struct RadarInfo &info) = 0;
155     virtual bool ReportAuthAddGroup(struct RadarInfo &info) = 0;
156     virtual bool ReportAuthAddGroupCb(std::string funcName, int32_t stageRes) = 0;
157     virtual bool ReportNetworkOnline(struct RadarInfo &info) = 0;
158     virtual bool ReportNetworkOffline(struct RadarInfo &info) = 0;
159     virtual bool ReportDeleteTrustRelation(struct RadarInfo &info) = 0;
160     virtual bool ReportGetTrustDeviceList(struct RadarInfo &info) = 0;
161     virtual void ReportCreatePinHolder(std::string hostName,
162         int32_t channelId, std::string peerUdid, int32_t errCode, int32_t stageRes) = 0;
163     virtual void ReportDestroyPinHolder(std::string hostName,
164         std::string peerUdid, int32_t errCode, int32_t stageRes) = 0;
165     virtual void ReportSendOrReceiveHolderMsg(int32_t bizStage, std::string funcName, std::string peerUdid) = 0;
166     virtual std::string GetDeviceInfoList(std::vector<DmDeviceInfo> &deviceInfoList) = 0;
167     virtual std::string GetUdidHashByUdid(std::string udid) = 0;
168 };
169 
170 class DmRadarHelper : public IDmRadarHelper {
171     DM_DECLARE_SINGLE_INSTANCE(DmRadarHelper);
172 public:
173     bool ReportDiscoverRegCallback(struct RadarInfo &info) override;
174     bool ReportDiscoverResCallback(struct RadarInfo &info) override;
175     bool ReportDiscoverUserRes(struct RadarInfo &info) override;
176     bool ReportAuthStart(const std::string &peerUdid, const std::string &pkgName) override;
177     bool ReportAuthOpenSession(struct RadarInfo &info) override;
178     bool ReportAuthSessionOpenCb(struct RadarInfo &info) override;
179     bool ReportAuthSendRequest(struct RadarInfo &info) override;
180     bool ReportAuthPullAuthBox(struct RadarInfo &info) override;
181     bool ReportAuthConfirmBox(struct RadarInfo &info) override;
182     bool ReportAuthCreateGroup(struct RadarInfo &info) override;
183     bool ReportAuthCreateGroupCb(std::string funcName, int32_t stageRes) override;
184     bool ReportAuthPullPinBox(struct RadarInfo &info) override;
185     bool ReportAuthInputPinBox(struct RadarInfo &info) override;
186     bool ReportAuthAddGroup(struct RadarInfo &info) override;
187     bool ReportAuthAddGroupCb(std::string funcName, int32_t stageRes) override;
188     bool ReportNetworkOnline(struct RadarInfo &info) override;
189     bool ReportNetworkOffline(struct RadarInfo &info) override;
190     bool ReportDeleteTrustRelation(struct RadarInfo &info) override;
191     bool ReportGetTrustDeviceList(struct RadarInfo &info) override;
192     void ReportCreatePinHolder(std::string hostName,
193         int32_t channelId, std::string peerUdid, int32_t errCode, int32_t stageRes) override;
194     void ReportDestroyPinHolder(std::string hostName,
195         std::string peerUdid, int32_t errCode, int32_t stageRes) override;
196     void ReportSendOrReceiveHolderMsg(int32_t bizStage, std::string funcName, std::string peerUdid) override;
197     std::string GetDeviceInfoList(std::vector<DmDeviceInfo> &deviceInfoList) override;
198     std::string GetUdidHashByUdid(std::string udid) override;
199     std::string ConvertHexToString(uint16_t hex);
200     int32_t GetErrCode(int32_t errCode);
201 private:
202     std::string GetAnonyUdid(std::string udid);
203     std::string GetAnonyLocalUdid();
204 };
205 
206 extern "C" IDmRadarHelper *CreateDmRadarInstance();
207 using CreateDmRadarFuncPtr = IDmRadarHelper *(*)(void);
208 } // namespace DistributedHardware
209 } // namespace OHOS
210 #endif // OHOS_DM_RADAR_HELPER_H