1 /*
2 * Copyright (c) 2022 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 #include "messenger_device_status_manager.h"
17
18 #include <stdlib.h>
19
20 #include "securec.h"
21 #include "softbus_bus_center.h"
22 #include "softbus_common.h"
23
24 #include "messenger_utils.h"
25 #include "utils_log.h"
26 #include "utils_mem.h"
27
28 static void MessengerOnNodeOnline(NodeBasicInfo *info);
29 static void MessengerOnNodeOffline(NodeBasicInfo *info);
30 static void MessengerOnNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo *info);
31 static int32_t InitDeviceOnlineProcessor(const DeviceIdentify *devId, int32_t level, void *para);
32
33 typedef struct DeviceStatusManager {
34 const INodeStateCb nodeStateCb;
35 DeviceStatusReceiver deviceStatusReceiver;
36 const char *pkgName;
37 WorkQueue *queue;
38 } DeviceStatusManager;
39
40 typedef struct QueueStatusData {
41 DeviceIdentify srcIdentity;
42 uint32_t status;
43 int32_t level;
44 } QueueStatusData;
45
GetDeviceManagerInstance(void)46 static DeviceStatusManager *GetDeviceManagerInstance(void)
47 {
48 static DeviceStatusManager manager = {
49 {
50 .events = EVENT_NODE_STATE_ONLINE | EVENT_NODE_STATE_OFFLINE,
51 .onNodeOnline = MessengerOnNodeOnline,
52 .onNodeOffline = MessengerOnNodeOffline,
53 .onNodeBasicInfoChanged = MessengerOnNodeBasicInfoChanged,
54 },
55 .deviceStatusReceiver = NULL,
56 .pkgName = NULL,
57 .queue = NULL,
58 };
59 return &manager;
60 }
61
ProcessDeviceStatusReceived(const uint8_t * data,uint32_t len)62 static void ProcessDeviceStatusReceived(const uint8_t *data, uint32_t len)
63 {
64 if (data == NULL || len == 0) {
65 return;
66 }
67 QueueStatusData *queueData = (QueueStatusData *)data;
68 if (sizeof(QueueStatusData) != len) {
69 SECURITY_LOG_ERROR("ProcessDeviceStatusReceived, invalid input");
70 return;
71 }
72
73 DeviceStatusManager *instance = GetDeviceManagerInstance();
74 DeviceStatusReceiver deviceStatusReceiver = instance->deviceStatusReceiver;
75 if (deviceStatusReceiver == NULL) {
76 SECURITY_LOG_ERROR("ProcessSessionMessageReceived, messageReceiver is null");
77 return;
78 }
79 deviceStatusReceiver(&queueData->srcIdentity, queueData->status, queueData->level);
80 FREE(queueData);
81 }
82
ProcessDeviceStatusReceiver(const DeviceIdentify * devId,uint32_t status,int32_t level)83 static void ProcessDeviceStatusReceiver(const DeviceIdentify *devId, uint32_t status, int32_t level)
84 {
85 DeviceStatusManager *instance = GetDeviceManagerInstance();
86
87 WorkQueue *queue = instance->queue;
88 if (queue == NULL) {
89 SECURITY_LOG_ERROR("ProcessDeviceStatusReceiver, queue is null");
90 return;
91 }
92
93 DeviceStatusReceiver deviceStatusReceiver = instance->deviceStatusReceiver;
94 if (deviceStatusReceiver == NULL) {
95 SECURITY_LOG_ERROR("ProcessDeviceStatusReceiver, messageReceiver is null");
96 return;
97 }
98
99 QueueStatusData *data = MALLOC(sizeof(QueueStatusData));
100 if (data == NULL) {
101 SECURITY_LOG_ERROR("ProcessDeviceStatusReceiver, malloc result null");
102 return;
103 }
104
105 uint32_t ret = (uint32_t)memcpy_s(&data->srcIdentity, sizeof(DeviceIdentify), devId, sizeof(DeviceIdentify));
106 if (ret != EOK) {
107 SECURITY_LOG_ERROR("ProcessDeviceStatusReceiver, memcpy failed");
108 FREE(data);
109 return;
110 }
111 data->level = level;
112 data->status = status;
113
114 ret = QueueWork(queue, ProcessDeviceStatusReceived, (uint8_t *)data, sizeof(QueueStatusData));
115 if (ret != WORK_QUEUE_OK) {
116 SECURITY_LOG_ERROR("ProcessDeviceStatusReceiver, QueueWork failed, ret is %{public}u", ret);
117 FREE(data);
118 return;
119 }
120 }
121
MessengerOnNodeStateChange(NodeBasicInfo * info,uint32_t state)122 static void MessengerOnNodeStateChange(NodeBasicInfo *info, uint32_t state)
123 {
124 if (info == NULL) {
125 SECURITY_LOG_ERROR("MessengerOnNodeStateChange process input is null.");
126 return;
127 }
128 DeviceStatusManager *instance = GetDeviceManagerInstance();
129
130 int32_t level = 0;
131 char udid[UDID_BUF_LEN] = {0};
132 if (GetNodeKeyInfo(instance->pkgName, info->networkId, NODE_KEY_UDID, (uint8_t *)udid, UDID_BUF_LEN) != 0) {
133 SECURITY_LOG_ERROR("MessengerOnNodeStateChange process get device identity error.");
134 return;
135 }
136
137 DeviceIdentify identity = {0, {0}};
138 identity.length = UDID_BUF_LEN - 1;
139 if (memcpy_s(identity.identity, DEVICE_ID_MAX_LEN, udid, UDID_BUF_LEN - 1) != EOK) {
140 SECURITY_LOG_ERROR("MessengerOnNodeStateChange copy device error");
141 }
142 uint32_t maskId = MaskDeviceIdentity(udid, UDID_BUF_LEN);
143 SECURITY_LOG_INFO("MessengerOnNodeStateChange device(%{public}x*** change to %{public}s)", maskId,
144 (state == EVENT_NODE_STATE_ONLINE) ? "online" : "offline");
145
146 ProcessDeviceStatusReceiver(&identity, state, level);
147 }
148
MessengerOnNodeOnline(NodeBasicInfo * info)149 static void MessengerOnNodeOnline(NodeBasicInfo *info)
150 {
151 return MessengerOnNodeStateChange(info, EVENT_NODE_STATE_ONLINE);
152 }
153
MessengerOnNodeOffline(NodeBasicInfo * info)154 static void MessengerOnNodeOffline(NodeBasicInfo *info)
155 {
156 return MessengerOnNodeStateChange(info, EVENT_NODE_STATE_OFFLINE);
157 }
158
MessengerOnNodeBasicInfoChanged(NodeBasicInfoType type,NodeBasicInfo * info)159 static void MessengerOnNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo *info)
160 {
161 (void)type;
162 (void)info;
163 }
164
InitDeviceOnlineProcessor(const DeviceIdentify * devId,int32_t level,void * para)165 static int32_t InitDeviceOnlineProcessor(const DeviceIdentify *devId, int32_t level, void *para)
166 {
167 (void)para;
168 ProcessDeviceStatusReceiver(devId, EVENT_NODE_STATE_ONLINE, level);
169 return 0;
170 }
171
InitDeviceStatusManager(WorkQueue * queue,const char * pkgName,DeviceStatusReceiver deviceStatusReceiver)172 bool InitDeviceStatusManager(WorkQueue *queue, const char *pkgName, DeviceStatusReceiver deviceStatusReceiver)
173 {
174 if (deviceStatusReceiver == NULL) {
175 return false;
176 }
177
178 DeviceStatusManager *instance = GetDeviceManagerInstance();
179 instance->pkgName = pkgName;
180 instance->deviceStatusReceiver = deviceStatusReceiver;
181 instance->queue = queue;
182
183 int try = 0;
184 int32_t ret = RegNodeDeviceStateCb(pkgName, (INodeStateCb *)&instance->nodeStateCb);
185 while (ret != 0 && try < MAX_TRY_TIMES) {
186 MessengerSleep(1); // sleep 1 second and try again
187 ret = RegNodeDeviceStateCb(pkgName, (INodeStateCb *)&instance->nodeStateCb);
188 try++;
189 }
190
191 if (ret != 0) {
192 SECURITY_LOG_ERROR("InitDeviceManager RegNodeDeviceStateCb failed = %{public}d", ret);
193 return false;
194 }
195
196 MessengerForEachDeviceProcess(InitDeviceOnlineProcessor, NULL);
197 SECURITY_LOG_INFO("InitDeviceManager RegNodeDeviceStateCb success");
198 return true;
199 }
200
DeInitDeviceStatusManager(void)201 bool DeInitDeviceStatusManager(void)
202 {
203 DeviceStatusManager *instance = GetDeviceManagerInstance();
204
205 int32_t ret = UnregNodeDeviceStateCb((INodeStateCb *)&instance->nodeStateCb);
206 if (ret != 0) {
207 SECURITY_LOG_ERROR("DeInitDeviceManager UnregNodeDeviceStateCb failed = %{public}d", ret);
208 return false;
209 }
210 instance->pkgName = NULL;
211 instance->deviceStatusReceiver = NULL;
212 instance->queue = NULL;
213 DestroyWorkQueue(instance->queue);
214
215 SECURITY_LOG_INFO("DeInitDeviceManager UnregNodeDeviceStateCb success");
216 return true;
217 }
218
MessengerConvertNodeToIdentity(const NodeBasicInfo * node,DeviceIdentify * devId)219 static bool MessengerConvertNodeToIdentity(const NodeBasicInfo *node, DeviceIdentify *devId)
220 {
221 if ((node == NULL) || (devId == NULL)) {
222 return false;
223 }
224 char udid[UDID_BUF_LEN] = {0};
225
226 DeviceStatusManager *instance = GetDeviceManagerInstance();
227 if (GetNodeKeyInfo(instance->pkgName, node->networkId, NODE_KEY_UDID, (uint8_t *)udid, UDID_BUF_LEN) != 0) {
228 SECURITY_LOG_ERROR("MessengerGetSelfDeviceIdentify GetNodeKeyInfo error.");
229 return false;
230 }
231
232 if (memcpy_s(devId->identity, DEVICE_ID_MAX_LEN, udid, DEVICE_ID_MAX_LEN) != EOK) {
233 SECURITY_LOG_ERROR("MessengerGetSelfDeviceIdentify memcpy error");
234 return false;
235 }
236 devId->length = DEVICE_ID_MAX_LEN;
237 return true;
238 }
239
MessengerGetDeviceNodeBasicInfo(const DeviceIdentify * devId,NodeBasicInfo * info)240 bool MessengerGetDeviceNodeBasicInfo(const DeviceIdentify *devId, NodeBasicInfo *info)
241 {
242 if (devId == NULL || info == NULL) {
243 return false;
244 }
245 DeviceStatusManager *instance = GetDeviceManagerInstance();
246
247 NodeBasicInfo *infoList = NULL;
248
249 int infoListLen = 0;
250 int32_t ret = GetAllNodeDeviceInfo(instance->pkgName, &infoList, &infoListLen);
251 if (ret != 0) {
252 SECURITY_LOG_ERROR("MessengerGetDeviceOnlineStatus GetAllNodeDeviceInfo failed = %{public}d", ret);
253 return false;
254 }
255
256 bool find = false;
257 for (int loop = 0; loop < infoListLen; loop++) {
258 const NodeBasicInfo *node = infoList + loop;
259 DeviceIdentify curr = {DEVICE_ID_MAX_LEN, {0}};
260 bool convert = MessengerConvertNodeToIdentity(node, &curr);
261 if (convert != true) {
262 continue;
263 }
264
265 if (IsSameDevice(devId, &curr)) {
266 find = true;
267 (void)memcpy_s(info, sizeof(NodeBasicInfo), node, sizeof(NodeBasicInfo));
268 break;
269 }
270 }
271
272 if (infoList != NULL) {
273 FreeNodeInfo(infoList);
274 }
275 return find;
276 }
277
MessengerGetDeviceOnlineStatus(const DeviceIdentify * devId,int32_t * level)278 bool MessengerGetDeviceOnlineStatus(const DeviceIdentify *devId, int32_t *level)
279 {
280 if (devId == NULL) {
281 return false;
282 }
283 NodeBasicInfo info = {{0}, {0}, 0};
284 bool result = MessengerGetDeviceNodeBasicInfo(devId, &info);
285 if (result == true && level != NULL) {
286 *level = -1;
287 }
288 return result;
289 }
290
MessengerGetNetworkIdByDeviceIdentify(const DeviceIdentify * devId,char * networkId,uint32_t len)291 bool MessengerGetNetworkIdByDeviceIdentify(const DeviceIdentify *devId, char *networkId, uint32_t len)
292 {
293 if (devId == NULL || networkId == NULL || len == 0) {
294 return false;
295 }
296 NodeBasicInfo info = {{0}, {0}, 0};
297 bool result = MessengerGetDeviceNodeBasicInfo(devId, &info);
298 if (result != true) {
299 return false;
300 }
301
302 int32_t ret = memcpy_s(networkId, len, info.networkId, NETWORK_ID_BUF_LEN);
303 if (ret != EOK) {
304 SECURITY_LOG_ERROR("MessengerGetDeviceNetworkId memcpy error");
305 return false;
306 }
307 return true;
308 }
309
MessengerGetSelfDeviceIdentify(DeviceIdentify * devId,int32_t * level)310 bool MessengerGetSelfDeviceIdentify(DeviceIdentify *devId, int32_t *level)
311 {
312 if (devId == NULL || level == NULL) {
313 return false;
314 }
315
316 DeviceStatusManager *instance = GetDeviceManagerInstance();
317
318 NodeBasicInfo info;
319 int32_t ret = GetLocalNodeDeviceInfo(instance->pkgName, &info);
320 if (ret != 0) {
321 SECURITY_LOG_ERROR("MessengerGetSelfDeviceIdentify GetLocalNodeDeviceInfo failed = %{public}d", ret);
322 return false;
323 }
324
325 bool convert = MessengerConvertNodeToIdentity(&info, devId);
326 if (convert == false) {
327 return false;
328 }
329 *level = -1;
330
331 uint32_t maskId = MaskDeviceIdentity((const char *)&devId->identity[0], UDID_BUF_LEN);
332 SECURITY_LOG_DEBUG("MessengerGetSelfDeviceIdentify device %{public}x***", maskId);
333 return true;
334 }
335
MessengerForEachDeviceProcess(const DeviceProcessor processor,void * para)336 void MessengerForEachDeviceProcess(const DeviceProcessor processor, void *para)
337 {
338 if (processor == NULL) {
339 return;
340 }
341 DeviceStatusManager *instance = GetDeviceManagerInstance();
342
343 NodeBasicInfo *infoList = NULL;
344
345 int infoListLen = 0;
346 int32_t level = -1;
347 int32_t ret = GetAllNodeDeviceInfo(instance->pkgName, &infoList, &infoListLen);
348 if (ret != 0) {
349 SECURITY_LOG_ERROR("MessengerForEachDeviceProcess GetAllNodeDeviceInfo failed = %{public}d", ret);
350 return;
351 }
352
353 for (int loop = 0; loop < infoListLen; loop++) {
354 const NodeBasicInfo *node = infoList + loop;
355 DeviceIdentify devId = {DEVICE_ID_MAX_LEN, {0}};
356 bool convert = MessengerConvertNodeToIdentity(node, &devId);
357 if (convert == true) {
358 processor(&devId, level, para);
359 }
360 }
361
362 if (infoList != NULL) {
363 FreeNodeInfo(infoList);
364 }
365 }
366
MessengerGetDeviceIdentifyByNetworkId(const char * networkId,DeviceIdentify * devId)367 bool MessengerGetDeviceIdentifyByNetworkId(const char *networkId, DeviceIdentify *devId)
368 {
369 if (networkId == NULL || devId == NULL) {
370 return false;
371 }
372 return MessengerConvertNodeToIdentity(networkId, devId);
373 }
374