Replace g_memdup to g_memdup2
[platform/core/connectivity/ua-manager.git] / ua-daemon / src / pm / ua-pm-util.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License")
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <dlfcn.h>
21
22 #include "ua-plugin.h"
23 #include <ua-plugin-manager.h>
24
25 static const char *status_string[] = {
26         FOREACH_STATUS(GENERATE_STATUS_STRING)
27 };
28
29 const char *_pm_util_uas_status_to_str(int status)
30 {
31         int arr_size = (sizeof(status_string)/sizeof(char*));
32         int id = -status;
33
34         UAM_DBG("arr_size: %d, id: %d", arr_size, id);
35         retv_if(arr_size <= id, NULL);
36
37         return status_string[id];
38 }
39
40 unsigned int _pm_util_uas_plugin_id_to_sensor_bitmask(uas_plugin_id_e id)
41 {
42         switch (id) {
43         case UAS_PLUGIN_ID_BLE:
44                 return UAM_SENSOR_BITMASK_BLE;
45         case UAS_PLUGIN_ID_WIFI:
46                 return UAM_SENSOR_BITMASK_WIFI;
47         case UAS_PLUGIN_ID_LIGHT:
48                 return UAM_SENSOR_BITMASK_LIGHT;
49         case UAS_PLUGIN_ID_MOTION:
50                 return UAM_SENSOR_BITMASK_MOTION;
51         case UAS_PLUGIN_ID_WIFI_LOCATION:
52                 return UAM_SENSOR_BITMASK_WIFI_LOCATION;
53         case UAS_PLUGIN_ID_UWB:
54                 return UAM_SENSOR_BITMASK_UWB;
55         default:
56                 UAM_WARN("Unknown Plugin id 0x%8.8X", id);
57                 return 0;
58         }
59 }
60
61 unsigned int _pm_util_uam_tech_type_to_plugin_id(uam_tech_type_e type)
62 {
63         switch (type) {
64         case UAM_TECH_TYPE_BLE:
65                 return UAS_PLUGIN_ID_BLE;
66         case UAM_TECH_TYPE_WIFI:
67                 return UAS_PLUGIN_ID_WIFI;
68         case UAM_TECH_TYPE_WIFI_LOCATION:
69                 return UAS_PLUGIN_ID_WIFI_LOCATION;
70         case UAM_TECH_TYPE_UWB:
71                 return UAS_PLUGIN_ID_UWB;
72         default:
73                 UAM_WARN("Unknown type 0x%8.8X", type);
74                 return UAS_PLUGIN_ID_MAX;
75         }
76 }
77
78 unsigned int _pm_util_uas_plugin_id_to_tech_type(uas_plugin_id_e id)
79 {
80         switch (id) {
81         case UAS_PLUGIN_ID_BLE:
82                 return UAM_TECH_TYPE_BLE;
83         case UAS_PLUGIN_ID_WIFI:
84                 return UAM_TECH_TYPE_WIFI;
85         case UAS_PLUGIN_ID_WIFI_LOCATION:
86                 return UAM_TECH_TYPE_WIFI_LOCATION;
87         case UAS_PLUGIN_ID_UWB:
88                 return UAM_TECH_TYPE_UWB;
89         default:
90                 UAM_WARN("Unknown Plugin id 0x%8.8X", id);
91                 return UAM_TECH_TYPE_NONE;
92         }
93 }
94
95 int _pm_util_sensor_bitmask_to_plugin_id(unsigned int bitmask)
96 {
97         switch (bitmask) {
98         case UAM_SENSOR_BITMASK_BLE:
99                 return UAS_PLUGIN_ID_BLE;
100         case UAM_SENSOR_BITMASK_WIFI:
101                 return UAS_PLUGIN_ID_WIFI;
102         case UAM_SENSOR_BITMASK_LIGHT:
103                 return UAS_PLUGIN_ID_LIGHT;
104         case UAM_SENSOR_BITMASK_MOTION:
105                 return UAS_PLUGIN_ID_MOTION;
106         default:
107                 UAM_WARN("Unknown sensor 0x%8.8X", bitmask);
108                 return UAS_PLUGIN_ID_MAX;
109         }
110 }
111
112 int _pm_util_sensor_bitmask_to_technology_type(unsigned int bitmask)
113 {
114         switch (bitmask) {
115         case UAM_SENSOR_BITMASK_BLE:
116                 return UAM_TECH_TYPE_BLE;
117         case UAM_SENSOR_BITMASK_WIFI:
118                 return UAM_TECH_TYPE_WIFI;
119         default:
120                 UAM_WARN("Unknown sensor 0x%8.8X", bitmask);
121                 return UAM_TECH_TYPE_NONE;
122         }
123 }
124
125 uas_address_type_e _pm_util_uam_addr_type_to_uas_addr_type(uam_addr_type_e type)
126 {
127         switch (type) {
128         case UAM_ADDR_TYPE_BT:
129                 return UAS_ADDR_TYPE_BT;
130         case UAM_ADDR_TYPE_BLE:
131                 return UAS_ADDR_TYPE_BLE;
132         case UAM_ADDR_TYPE_WIFI:
133                 return UAS_ADDR_TYPE_WIFI;
134         case UAM_ADDR_TYPE_P2P:
135                 return UAS_ADDR_TYPE_P2P;
136         case UAM_ADDR_TYPE_IPv4:
137                 return UAS_ADDR_TYPE_IPv4;
138         case UAM_ADDR_TYPE_IPv6:
139                 return UAS_ADDR_TYPE_IPv6;
140         default:
141                 UAM_ERR("Unknown address type: %d", type);
142                 return UAS_ADDR_TYPE_INVALID;
143         }
144 }
145
146 uas_address_type_e _pm_util_uam_tech_type_to_uas_addr_type(uam_tech_type_e type)
147 {
148         switch (type) {
149         case UAM_TECH_TYPE_BT:
150                 return UAS_ADDR_TYPE_BT;
151         case UAM_TECH_TYPE_BLE:
152                 return UAS_ADDR_TYPE_BLE;
153         case UAM_TECH_TYPE_P2P:
154                 return UAS_ADDR_TYPE_P2P;
155         case UAM_TECH_TYPE_WIFI:
156                 return UAS_ADDR_TYPE_WIFI;
157         case UAM_TECH_TYPE_WIFI_LOCATION:
158                 return UAS_ADDR_TYPE_WIFI;
159         case UAM_TECH_TYPE_UWB:
160                 return UAS_ADDR_TYPE_UWB;
161         default:
162                 UAM_WARN("Unknown type 0x%8.8X", type);
163                 return UAS_PLUGIN_ID_MAX;
164         }
165 }
166
167 void _pm_util_uam_db_payload_to_uas_payload(
168                 uas_payload_info_t *dst_payload, uam_db_payload_info_t *src_payload)
169 {
170         if (src_payload) {
171                 dst_payload->primary_key = src_payload->primary_key;
172                 dst_payload->device_type = src_payload->device_type;
173                 dst_payload->secondary_key = src_payload->secondary_key;
174                 dst_payload->ext_val1 = src_payload->ext_val1;
175                 dst_payload->ext_val2 = src_payload->ext_val2;
176                 dst_payload->device_uid_len = src_payload->device_uid_len;
177                 dst_payload->device_uid = g_memdup2((src_payload->device_uid),
178                                                 src_payload->device_uid_len);
179         }
180 }
181
182 void _pm_util_uas_payload_to_uam_payload(
183                 uam_ble_payload_s *dst_payload, uas_payload_info_t *src_payload)
184 {
185         if (src_payload) {
186                 int user_data_len;
187
188                 dst_payload->primary_key = src_payload->primary_key;
189                 dst_payload->device_type = src_payload->device_type;
190                 dst_payload->secondary_key = src_payload->secondary_key;
191                 dst_payload->ext_val1 = src_payload->ext_val1;
192                 dst_payload->ext_val2 = src_payload->ext_val2;
193                 dst_payload->device_uid_len = src_payload->device_uid_len;
194                 memset(dst_payload->device_uid, 0, src_payload->device_uid_len);
195                 if (src_payload->device_uid)
196                         memcpy(dst_payload->device_uid,
197                         src_payload->device_uid, src_payload->device_uid_len);
198
199                 user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN
200                                         - 1 - src_payload->device_uid_len;
201                 memset(dst_payload->user_data, 0, user_data_len);
202                 if (src_payload->user_data)
203                         memcpy(dst_payload->user_data,
204                         src_payload->user_data, user_data_len);
205         }
206 }
207
208 void _pm_util_uas_device_info_free(uas_device_info_t *device)
209 {
210         FUNC_ENTRY;
211         int i;
212
213         g_free(device->device_id);
214
215         for (i = 0; i < device->num_addr; i++)
216                 g_free(device->addr_list[i].address);
217
218         g_free(device->addr_list);
219         g_free(device);
220
221         FUNC_EXIT;
222 }
223
224 static gint __compare_payload(gconstpointer data, gconstpointer user_data)
225 {
226         const uam_db_payload_info_t *list_payload = data;
227         const uam_db_payload_info_t *payload = user_data;
228
229         retv_if(NULL == list_payload, -1);
230         retv_if(NULL == payload, -1);
231
232         if ((list_payload->primary_key == payload->primary_key) &&
233                 (list_payload->secondary_key == payload->secondary_key) &&
234                 (list_payload->device_uid_len == payload->device_uid_len) &&
235                 (0 == memcmp(list_payload->device_uid, payload->device_uid,
236                         payload->device_uid_len)))
237                 return 0;
238
239         return -1;
240 }
241
242 void _pm_util_uam_db_dev_to_uas_dev(unsigned int tech_type,
243                 uam_db_device_info_t *dev, uas_device_info_t **device)
244 {
245         FUNC_ENTRY;
246         int i = 0;
247         GSList *l;
248         int len = 0;
249
250         ret_if(NULL == dev);
251         ret_if(NULL == dev->user);
252         ret_if(NULL == dev->tech_list);
253         ret_if(NULL == device);
254
255         if (NULL == *device)
256                 *device = g_new0(uas_device_info_t, 1);
257         else
258                 memset(*device, 0x00, sizeof(uas_device_info_t));
259
260         (*device)->user_id = dev->user->user_id;
261         (*device)->device_id = g_strdup(dev->device_id);
262
263         for (l = dev->tech_list; NULL != l; l = g_slist_next(l)) {
264                 uam_db_tech_info_t *tech = l->data;
265                 GSList *unique_payloads = NULL;
266                 GSList *l1, *l2;
267
268                 if (!tech || !tech->addresses)
269                         continue;
270
271                 if (tech_type != tech->tech_type)
272                         continue;
273
274                 (*device)->os = tech->os;
275                 (*device)->discriminant = tech->discriminant;
276                 (*device)->num_addr = g_slist_length(tech->addresses);
277                 (*device)->addr_list = g_new0(uas_address_info_t, (*device)->num_addr);
278                 for (l1 = tech->addresses; NULL != l1; l1 = g_slist_next(l1)) {
279                         uam_db_address_info_t *addr = l1->data;
280
281                         if (!addr)
282                                 continue;
283
284                         (*device)->addr_list[i].type = _pm_util_uam_addr_type_to_uas_addr_type(addr->addr_type);
285                         (*device)->addr_list[i++].address = g_strdup(addr->address);
286                 }
287
288                 /* Find unique payload list for the device */
289                 for (l1 = tech->svc_dev_list; NULL != l1; l1 = g_slist_next(l1)) {
290                         uam_svc_dev_info_t *svc_dev = l1->data;
291                         uam_db_payload_info_t *payload = svc_dev->payload;
292
293                         if (!payload)
294                                 continue;
295                         l2 = g_slist_find_custom(unique_payloads, payload, __compare_payload);
296                         if (!l2)
297                                 unique_payloads = g_slist_prepend(unique_payloads, payload);
298                 }
299
300                 len = g_slist_length(unique_payloads);
301                 (*device)->num_payload = len;
302                 (*device)->payload = g_new0(uas_payload_info_t, len);
303                 i = 0;
304                 UAM_INFO("%d payload present for device %s tech %d", len, dev->device_id, tech->tech_type);
305                 for (l1 = unique_payloads; NULL != l1; l1 = g_slist_next(l1)) {
306                         uam_db_payload_info_t *payload = l1->data;
307
308                         if (!payload)
309                                 continue;
310
311                         _pm_util_uam_db_payload_to_uas_payload(&((*device)->payload[i]), payload);
312                         i++;
313                 }
314                 g_slist_free(unique_payloads);
315
316         }
317
318         FUNC_EXIT;
319 }
320
321 uas_device_info_t *_pm_util_uam_dev_info_to_uas_dev_info(const uam_device_info_s *dev)
322 {
323         FUNC_ENTRY;
324         uas_device_info_t *device;
325         int type = UAS_ADDR_TYPE_INVALID;
326         char *mac = NULL;
327         char *ipv4_addr = NULL;
328         int i = 0;
329
330         retv_if(NULL == dev, NULL);
331
332         device = g_new0(uas_device_info_t, 1);
333         retv_if(NULL == device, NULL);
334
335         device->os = dev->operating_system;
336         device->discriminant = dev->discriminant;
337
338         if (0 < strlen(dev->mac)) {
339                 type =  _pm_util_uam_tech_type_to_uas_addr_type(dev->type);
340
341                 if (UAS_ADDR_TYPE_INVALID != type) {
342                         mac = g_strdup(dev->mac);
343                         device->num_addr += 1;
344                 }
345         }
346
347         if (0 < strlen(dev->ipv4_addr)) {
348                 ipv4_addr = g_strdup(dev->ipv4_addr);
349                 device->num_addr += 1;
350         }
351
352         if (0 >= device->num_addr)
353                 UAM_WARN("device->num_addr = %d", device->num_addr);
354
355         device->device_id = g_strdup(dev->device_id);
356         device->addr_list = g_new0(uas_address_info_t, device->num_addr);
357         if (mac) {
358                 device->addr_list[i].type = type;
359                 device->addr_list[i++].address = mac;
360         }
361
362         if (ipv4_addr) {
363                 device->addr_list[i].type = UAS_ADDR_TYPE_IPv4;
364                 device->addr_list[i++].address = ipv4_addr;
365         }
366
367         device->payload = NULL;
368
369         FUNC_EXIT;
370         return device;
371 }
372
373 uam_device_info_s *_pm_util_uas_dev_info_to_uam_dev_info(const uas_device_info_t *dev)
374 {
375         FUNC_ENTRY;
376         uam_device_info_s *device;
377         int i = 0;
378
379         retv_if(NULL == dev, NULL);
380
381         device = g_new0(uam_device_info_s, 1);
382         retv_if(NULL == device, NULL);
383
384         device->operating_system = dev->os;
385         device->discriminant = dev->discriminant;
386         device->last_seen = dev->last_seen;
387         g_strlcpy(device->device_id, dev->device_id, UAM_DEVICE_ID_MAX_STRING_LEN);
388
389         for (i = 0; i < dev->num_addr; i++) {
390                 UAM_DBG("Address[%d]: %s", i, dev->addr_list[i].address);
391                 switch (dev->addr_list[i].type) {
392                 case UAS_ADDR_TYPE_BLE:
393                         device->type = UAM_TECH_TYPE_BLE;
394                         g_strlcpy(device->mac, dev->addr_list[i].address,
395                                         UAM_MAC_ADDRESS_STRING_LEN);
396                         break;
397                 case UAS_ADDR_TYPE_BT:
398                         device->type = UAM_TECH_TYPE_BT;
399                         g_strlcpy(device->mac, dev->addr_list[i].address,
400                                         UAM_MAC_ADDRESS_STRING_LEN);
401                         break;
402                 case UAS_ADDR_TYPE_P2P:
403                         device->type = UAM_TECH_TYPE_P2P;
404                         g_strlcpy(device->mac, dev->addr_list[i].address,
405                                         UAM_MAC_ADDRESS_STRING_LEN);
406                         break;
407                 case UAS_ADDR_TYPE_WIFI:
408                         device->type = UAM_TECH_TYPE_WIFI;
409                         g_strlcpy(device->mac, dev->addr_list[i].address,
410                                         UAM_MAC_ADDRESS_STRING_LEN);
411                         break;
412                 case UAS_ADDR_TYPE_IPv4:
413                         g_strlcpy(device->ipv4_addr, dev->addr_list[i].address,
414                                         UAM_IP_ADDRESS_MAX_STRING_LEN);
415                         break;
416                 case UAS_ADDR_TYPE_UWB:
417                         g_strlcpy(device->mac, dev->addr_list[i].address,
418                                         UAM_MAC_ADDRESS_STRING_LEN);
419                         break;
420                 case UAS_ADDR_TYPE_IPv6:
421                 default:
422                         UAM_ERR("Unsupported address type: %d", dev->addr_list[i].type);
423                 }
424         }
425
426         FUNC_EXIT;
427         return device;
428 }
429
430 uam_active_scan_event_e _pm_util_uas_scan_event_to_uam_scan_event(uas_active_scan_event_e event)
431 {
432         switch (event) {
433         case UAS_ACTIVE_DEVICE_FOUND:
434                 return UAM_ACTIVE_DEVICE_FOUND;
435         case UAS_ACTIVE_SCAN_COMPLETED:
436                 return UAM_ACTIVE_SCAN_COMPLETED;
437         default:
438                 UAM_WARN("Unknown event 0x%8.8X", event);
439                 return 0;
440         }
441 }
442
443 uam_sensor_info_s *_pm_util_uas_sensor_info_to_uam_sensor_info(
444         const uas_sensor_info_t *info)
445 {
446         FUNC_ENTRY;
447         uam_sensor_info_s *sensor_info;
448         unsigned int i = 0;
449         retv_if(NULL == info, NULL);
450
451         sensor_info = g_new0(uam_sensor_info_s, 1);
452         retv_if(NULL == sensor_info, NULL);
453
454         sensor_info->status = info->status;
455         sensor_info->timestamp = info->timestamp;
456         sensor_info->accuracy = info->accuracy;
457         sensor_info->count = info->count;
458
459         for (i = 0; i < info->count; i++)
460                 sensor_info->values[i] = info->values[i];
461
462         UAM_INFO("t [%llu] Accuaracy [%d] Count [%d] Lux [%f] CCT [%f] Lv[%f] Cv[%f]",
463                 sensor_info->timestamp, sensor_info->accuracy, sensor_info->count,
464                 sensor_info->values[0], sensor_info->values[1], sensor_info->values[2],
465                 sensor_info->values[3]);
466
467         FUNC_EXIT;
468         return sensor_info;
469 }