• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, uint32_t devType, 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     uint32_t devType;
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->devType);
80     FREE(queueData);
81 }
82 
ProcessDeviceStatusReceiver(const DeviceIdentify * devId,uint32_t status,uint32_t devType)83 static void ProcessDeviceStatusReceiver(const DeviceIdentify *devId, uint32_t status, uint32_t devType)
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->devType = devType;
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     char udid[UDID_BUF_LEN] = {0};
131     if (GetNodeKeyInfo(instance->pkgName, info->networkId, NODE_KEY_UDID, (uint8_t *)udid, UDID_BUF_LEN) != 0) {
132         SECURITY_LOG_ERROR("MessengerOnNodeStateChange process get device identity error.");
133         return;
134     }
135 
136     DeviceIdentify identity = {0, {0}};
137     identity.length = UDID_BUF_LEN - 1;
138     if (memcpy_s(identity.identity, DEVICE_ID_MAX_LEN, udid, UDID_BUF_LEN - 1) != EOK) {
139         SECURITY_LOG_ERROR("MessengerOnNodeStateChange copy device error");
140     }
141     uint32_t maskId = MaskDeviceIdentity(udid, UDID_BUF_LEN);
142     SECURITY_LOG_INFO("MessengerOnNodeStateChange device(%{public}x*** change to %{public}s, deviceType is %{public}d)",
143         maskId, (state == EVENT_NODE_STATE_ONLINE) ? "online" : "offline", info->deviceTypeId);
144 
145     ProcessDeviceStatusReceiver(&identity, state, info->deviceTypeId);
146 }
147 
MessengerOnNodeOnline(NodeBasicInfo * info)148 static void MessengerOnNodeOnline(NodeBasicInfo *info)
149 {
150     return MessengerOnNodeStateChange(info, EVENT_NODE_STATE_ONLINE);
151 }
152 
MessengerOnNodeOffline(NodeBasicInfo * info)153 static void MessengerOnNodeOffline(NodeBasicInfo *info)
154 {
155     return MessengerOnNodeStateChange(info, EVENT_NODE_STATE_OFFLINE);
156 }
157 
MessengerOnNodeBasicInfoChanged(NodeBasicInfoType type,NodeBasicInfo * info)158 static void MessengerOnNodeBasicInfoChanged(NodeBasicInfoType type, NodeBasicInfo *info)
159 {
160     (void)type;
161     (void)info;
162 }
163 
InitDeviceOnlineProcessor(const DeviceIdentify * devId,uint32_t devType,void * para)164 static int32_t InitDeviceOnlineProcessor(const DeviceIdentify *devId, uint32_t devType, void *para)
165 {
166     (void)para;
167     ProcessDeviceStatusReceiver(devId, EVENT_NODE_STATE_ONLINE, devType);
168     return 0;
169 }
170 
InitDeviceStatusManager(WorkQueue * queue,const char * pkgName,DeviceStatusReceiver deviceStatusReceiver)171 bool InitDeviceStatusManager(WorkQueue *queue, const char *pkgName, DeviceStatusReceiver deviceStatusReceiver)
172 {
173     if (deviceStatusReceiver == NULL) {
174         return false;
175     }
176 
177     DeviceStatusManager *instance = GetDeviceManagerInstance();
178     instance->pkgName = pkgName;
179     instance->deviceStatusReceiver = deviceStatusReceiver;
180     instance->queue = queue;
181 
182     int try = 0;
183     int32_t ret = RegNodeDeviceStateCb(pkgName, (INodeStateCb *)&instance->nodeStateCb);
184     while (ret != 0 && try < MAX_TRY_TIMES) {
185         MessengerSleep(1); // sleep 1 second and try again
186         ret = RegNodeDeviceStateCb(pkgName, (INodeStateCb *)&instance->nodeStateCb);
187         try++;
188     }
189 
190     if (ret != 0) {
191         SECURITY_LOG_ERROR("InitDeviceManager RegNodeDeviceStateCb failed = %{public}d", ret);
192         return false;
193     }
194 
195     MessengerForEachDeviceProcess(InitDeviceOnlineProcessor, NULL);
196     SECURITY_LOG_INFO("InitDeviceManager RegNodeDeviceStateCb success");
197     return true;
198 }
199 
DeInitDeviceStatusManager(void)200 bool DeInitDeviceStatusManager(void)
201 {
202     DeviceStatusManager *instance = GetDeviceManagerInstance();
203 
204     int32_t ret = UnregNodeDeviceStateCb((INodeStateCb *)&instance->nodeStateCb);
205     if (ret != 0) {
206         SECURITY_LOG_ERROR("DeInitDeviceManager UnregNodeDeviceStateCb failed = %{public}d", ret);
207         return false;
208     }
209     instance->pkgName = NULL;
210     instance->deviceStatusReceiver = NULL;
211     instance->queue = NULL;
212     DestroyWorkQueue(instance->queue);
213 
214     SECURITY_LOG_INFO("DeInitDeviceManager UnregNodeDeviceStateCb success");
215     return true;
216 }
217 
MessengerConvertNodeToIdentity(const NodeBasicInfo * node,DeviceIdentify * devId)218 static bool MessengerConvertNodeToIdentity(const NodeBasicInfo *node, DeviceIdentify *devId)
219 {
220     if ((node == NULL) || (devId == NULL)) {
221         return false;
222     }
223     char udid[UDID_BUF_LEN] = {0};
224 
225     DeviceStatusManager *instance = GetDeviceManagerInstance();
226     if (GetNodeKeyInfo(instance->pkgName, node->networkId, NODE_KEY_UDID, (uint8_t *)udid, UDID_BUF_LEN) != 0) {
227         SECURITY_LOG_ERROR("MessengerGetSelfDeviceIdentify GetNodeKeyInfo error.");
228         return false;
229     }
230 
231     if (memcpy_s(devId->identity, DEVICE_ID_MAX_LEN, udid, DEVICE_ID_MAX_LEN) != EOK) {
232         SECURITY_LOG_ERROR("MessengerGetSelfDeviceIdentify memcpy error");
233         return false;
234     }
235     devId->length = DEVICE_ID_MAX_LEN;
236     return true;
237 }
238 
MessengerGetDeviceNodeBasicInfo(const DeviceIdentify * devId,NodeBasicInfo * info)239 bool MessengerGetDeviceNodeBasicInfo(const DeviceIdentify *devId, NodeBasicInfo *info)
240 {
241     if (devId == NULL || info == NULL) {
242         return false;
243     }
244     DeviceStatusManager *instance = GetDeviceManagerInstance();
245 
246     NodeBasicInfo *infoList = NULL;
247 
248     int infoListLen = 0;
249     int32_t ret = GetAllNodeDeviceInfo(instance->pkgName, &infoList, &infoListLen);
250     if (ret != 0) {
251         SECURITY_LOG_ERROR("MessengerGetDeviceOnlineStatus GetAllNodeDeviceInfo failed = %{public}d", ret);
252         return false;
253     }
254 
255     bool find = false;
256     for (int loop = 0; loop < infoListLen; loop++) {
257         const NodeBasicInfo *node = infoList + loop;
258         DeviceIdentify curr = {DEVICE_ID_MAX_LEN, {0}};
259         bool convert = MessengerConvertNodeToIdentity(node, &curr);
260         if (convert != true) {
261             continue;
262         }
263 
264         if (IsSameDevice(devId, &curr)) {
265             find = true;
266             (void)memcpy_s(info, sizeof(NodeBasicInfo), node, sizeof(NodeBasicInfo));
267             break;
268         }
269     }
270 
271     if (infoList != NULL) {
272         FreeNodeInfo(infoList);
273     }
274     return find;
275 }
276 
MessengerGetDeviceOnlineStatus(const DeviceIdentify * devId,uint32_t * devType)277 bool MessengerGetDeviceOnlineStatus(const DeviceIdentify *devId, uint32_t *devType)
278 {
279     if (devId == NULL) {
280         return false;
281     }
282     NodeBasicInfo info = {{0}, {0}, 0};
283     bool result = MessengerGetDeviceNodeBasicInfo(devId, &info);
284     if (result == true && devType != NULL) {
285         *devType = info.deviceTypeId;
286     }
287     return result;
288 }
289 
MessengerGetNetworkIdByDeviceIdentify(const DeviceIdentify * devId,char * networkId,uint32_t len)290 bool MessengerGetNetworkIdByDeviceIdentify(const DeviceIdentify *devId, char *networkId, uint32_t len)
291 {
292     if (devId == NULL || networkId == NULL || len == 0) {
293         return false;
294     }
295     NodeBasicInfo info = {{0}, {0}, 0};
296     bool result = MessengerGetDeviceNodeBasicInfo(devId, &info);
297     if (result != true) {
298         return false;
299     }
300 
301     int32_t ret = memcpy_s(networkId, len, info.networkId, NETWORK_ID_BUF_LEN);
302     if (ret != EOK) {
303         SECURITY_LOG_ERROR("MessengerGetDeviceNetworkId memcpy error");
304         return false;
305     }
306     return true;
307 }
308 
MessengerGetSelfDeviceIdentify(DeviceIdentify * devId,uint32_t * devType)309 bool MessengerGetSelfDeviceIdentify(DeviceIdentify *devId, uint32_t *devType)
310 {
311     if (devId == NULL || devType == NULL) {
312         return false;
313     }
314 
315     DeviceStatusManager *instance = GetDeviceManagerInstance();
316 
317     NodeBasicInfo info;
318     int32_t ret = GetLocalNodeDeviceInfo(instance->pkgName, &info);
319     if (ret != 0) {
320         SECURITY_LOG_ERROR("MessengerGetSelfDeviceIdentify GetLocalNodeDeviceInfo failed = %{public}d", ret);
321         return false;
322     }
323 
324     bool convert = MessengerConvertNodeToIdentity(&info, devId);
325     if (convert == false) {
326         return false;
327     }
328     *devType = info.deviceTypeId;
329 
330     uint32_t maskId = MaskDeviceIdentity((const char *)&devId->identity[0], UDID_BUF_LEN);
331     SECURITY_LOG_DEBUG("MessengerGetSelfDeviceIdentify device %{public}x***, deviceType is %{public}d", maskId,
332         info.deviceTypeId);
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 ret = GetAllNodeDeviceInfo(instance->pkgName, &infoList, &infoListLen);
347     if (ret != 0) {
348         SECURITY_LOG_ERROR("MessengerForEachDeviceProcess GetAllNodeDeviceInfo failed = %{public}d", ret);
349         return;
350     }
351 
352     for (int loop = 0; loop < infoListLen; loop++) {
353         const NodeBasicInfo *node = infoList + loop;
354         DeviceIdentify devId = {DEVICE_ID_MAX_LEN, {0}};
355         bool convert = MessengerConvertNodeToIdentity(node, &devId);
356         if (convert == true) {
357             processor(&devId, node->deviceTypeId, para);
358         }
359     }
360 
361     if (infoList != NULL) {
362         FreeNodeInfo(infoList);
363     }
364 }
365 
MessengerGetDeviceIdentifyByNetworkId(const char * networkId,DeviceIdentify * devId)366 bool MessengerGetDeviceIdentifyByNetworkId(const char *networkId, DeviceIdentify *devId)
367 {
368     if (networkId == NULL || devId == NULL) {
369         return false;
370     }
371     return MessengerConvertNodeToIdentity(networkId, devId);
372 }
373