Rename ble payload service_id to primary_key
[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         default:
52                 UAM_WARN("Unknown Plugin id 0x%8.8X", id);
53                 return 0;
54         }
55 }
56
57 unsigned int _pm_util_uam_tech_type_to_plugin_id(uam_tech_type_e type)
58 {
59         switch (type) {
60         case UAM_TECH_TYPE_BLE:
61                 return UAS_PLUGIN_ID_BLE;
62         case UAM_TECH_TYPE_WIFI:
63                 return UAS_PLUGIN_ID_WIFI;
64         default:
65                 UAM_WARN("Unknown type 0x%8.8X", type);
66                 return UAS_PLUGIN_ID_MAX;
67         }
68 }
69
70 unsigned int _pm_util_uas_plugin_id_to_tech_type(uas_plugin_id_e id)
71 {
72         switch (id) {
73         case UAS_PLUGIN_ID_BLE:
74                 return UAM_TECH_TYPE_BLE;
75         case UAS_PLUGIN_ID_WIFI:
76                 return UAM_TECH_TYPE_WIFI;
77         default:
78                 UAM_WARN("Unknown Plugin id 0x%8.8X", id);
79                 return UAM_TECH_TYPE_NONE;
80         }
81 }
82
83 int _pm_util_sensor_bitmask_to_plugin_id(unsigned int bitmask)
84 {
85         switch (bitmask) {
86         case UAM_SENSOR_BITMASK_BLE:
87                 return UAS_PLUGIN_ID_BLE;
88         case UAM_SENSOR_BITMASK_WIFI:
89                 return UAS_PLUGIN_ID_WIFI;
90         case UAM_SENSOR_BITMASK_LIGHT:
91                 return UAS_PLUGIN_ID_LIGHT;
92         case UAM_SENSOR_BITMASK_MOTION:
93                 return UAS_PLUGIN_ID_MOTION;
94         default:
95                 UAM_WARN("Unknown sensor 0x%8.8X", bitmask);
96                 return UAS_PLUGIN_ID_MAX;
97         }
98 }
99
100 int _pm_util_sensor_bitmask_to_technology_type(unsigned int bitmask)
101 {
102         switch (bitmask) {
103         case UAM_SENSOR_BITMASK_BLE:
104                 return UAM_TECH_TYPE_BLE;
105         case UAM_SENSOR_BITMASK_WIFI:
106                 return UAM_TECH_TYPE_WIFI;
107         default:
108                 UAM_WARN("Unknown sensor 0x%8.8X", bitmask);
109                 return UAM_TECH_TYPE_NONE;
110         }
111 }
112
113 uas_address_type_e _pm_util_uam_addr_type_to_uas_addr_type(uam_addr_type_e type)
114 {
115         switch (type) {
116         case UAM_ADDR_TYPE_BT:
117                 return UAS_ADDR_TYPE_BT;
118         case UAM_ADDR_TYPE_BLE:
119                 return UAS_ADDR_TYPE_BLE;
120         case UAM_ADDR_TYPE_WIFI:
121                 return UAS_ADDR_TYPE_WIFI;
122         case UAM_ADDR_TYPE_P2P:
123                 return UAS_ADDR_TYPE_P2P;
124         case UAM_ADDR_TYPE_IPv4:
125                 return UAS_ADDR_TYPE_IPv4;
126         case UAM_ADDR_TYPE_IPv6:
127                 return UAS_ADDR_TYPE_IPv6;
128         default:
129                 UAM_ERR("Unknown address type: %d", type);
130                 return UAS_ADDR_TYPE_INVALID;
131         }
132 }
133
134 void _pm_util_uas_device_info_free(uas_device_info_t *device)
135 {
136         FUNC_ENTRY;
137         int i;
138
139         g_free(device->device_id);
140
141         for (i = 0; i < device->num_addr; i++)
142                 g_free(device->addr_list[i].address);
143
144         g_free(device->addr_list);
145
146         if (device->payload) {
147                 g_free(device->payload->duid);
148                 g_free(device->payload->bt_mac);
149                 g_free(device->payload);
150         }
151
152         g_free(device);
153
154         FUNC_EXIT;
155 }
156
157 void _pm_util_uam_db_dev_to_uas_dev(unsigned int tech_type,
158                 uam_db_device_info_t *dev, uas_device_info_t **device)
159 {
160         FUNC_ENTRY;
161         int i = 0;
162         GSList *l;
163
164         ret_if(NULL == dev);
165         ret_if(NULL == dev->user);
166         ret_if(NULL == dev->tech_list);
167         ret_if(NULL == device);
168
169         if (NULL == *device)
170                 *device = g_new0(uas_device_info_t, 1);
171         else
172                 memset(*device, 0x00, sizeof(uas_device_info_t));
173
174         (*device)->user_id = dev->user->user_id;
175         (*device)->os = dev->os;
176         (*device)->device_id = g_strdup(dev->device_id);
177
178         for (l = dev->tech_list; NULL != l; l = g_slist_next(l)) {
179                 uam_db_tech_info_t *tech = l->data;
180                 GSList *l1;
181
182                 if (!tech || !tech->addresses)
183                         continue;
184
185                 if (tech_type != tech->tech_type)
186                         continue;
187
188                 (*device)->discriminant = tech->discriminant;
189                 (*device)->num_addr = g_slist_length(tech->addresses);
190                 (*device)->addr_list = g_new0(uas_address_info_t, (*device)->num_addr);
191                 for (l1 = tech->addresses; NULL != l1; l1 = g_slist_next(l1)) {
192                         uam_db_address_info_t *addr = l1->data;
193
194                         if (!addr)
195                                 continue;
196
197                         (*device)->addr_list[i].type = _pm_util_uam_addr_type_to_uas_addr_type(addr->addr_type);
198                         (*device)->addr_list[i++].address = g_strdup(addr->address);
199                 }
200                 (*device)->payload = g_new0(uas_ble_payload_t, 1);
201                 (*device)->payload->primary_key = tech->payload->primary_key;
202                 (*device)->payload->purpose = tech->payload->purpose;
203                 (*device)->payload->device_icon = tech->payload->device_icon;
204                 (*device)->payload->duid = g_memdup(tech->payload->duid, UAM_BLE_PAYLOAD_DUID_LEN);
205                 (*device)->payload->bt_mac = g_memdup(tech->payload->bt_mac, UAM_BT_MAC_ADDRESS_STRING_LEN);
206         }
207
208         FUNC_EXIT;
209 }
210
211 uas_device_info_t *_pm_util_uam_dev_info_to_uas_dev_info(const uam_device_info_s *dev)
212 {
213         FUNC_ENTRY;
214         uas_device_info_t *device;
215         int type = UAS_ADDR_TYPE_INVALID;
216         char *mac = NULL;
217         char *ipv4_addr = NULL;
218         int i = 0;
219
220         retv_if(NULL == dev, NULL);
221
222         device = g_new0(uas_device_info_t, 1);
223         retv_if(NULL == device, NULL);
224
225         device->os = dev->operating_system;
226         device->discriminant = dev->discriminant;
227
228         if (0 < strlen(dev->mac)) {
229                 switch (dev->type) {
230                 case UAM_TECH_TYPE_BLE:
231                         type = UAS_ADDR_TYPE_BLE;
232                         break;
233                 case UAM_TECH_TYPE_BT:
234                         type = UAS_ADDR_TYPE_BT;
235                         break;
236                 case UAM_TECH_TYPE_P2P:
237                         type = UAS_ADDR_TYPE_P2P;
238                         break;
239                 case UAM_TECH_TYPE_WIFI:
240                         type = UAS_ADDR_TYPE_WIFI;
241                         break;
242                 default:
243                         UAM_ERR("Unknown tech type: %d", dev->type);
244                 }
245
246                 if (UAS_ADDR_TYPE_INVALID != type) {
247                         mac = g_strdup(dev->mac);
248                         device->num_addr += 1;
249                 }
250         }
251
252         if (0 < strlen(dev->ipv4_addr)) {
253                 ipv4_addr = g_strdup(dev->ipv4_addr);
254                 device->num_addr += 1;
255         }
256
257         if (0 >= device->num_addr)
258                 UAM_WARN("device->num_addr = %d", device->num_addr);
259
260         device->device_id = g_strdup(dev->device_id);
261
262         device->payload = g_new0(uas_ble_payload_t, 1);
263         device->payload->primary_key = dev->payload.primary_key;
264         device->payload->purpose = dev->payload.purpose;
265         device->payload->device_icon = dev->payload.device_icon;
266         device->payload->duid = g_memdup(&(dev->payload.duid), UAM_BLE_PAYLOAD_DUID_LEN);
267         device->payload->bt_mac = g_memdup(&(dev->payload.bt_mac), UAM_BT_MAC_ADDRESS_STRING_LEN);
268
269         device->addr_list = g_new0(uas_address_info_t, device->num_addr);
270         if (mac) {
271                 device->addr_list[i].type = type;
272                 device->addr_list[i++].address = mac;
273         }
274
275         if (ipv4_addr) {
276                 device->addr_list[i].type = UAS_ADDR_TYPE_IPv4;
277                 device->addr_list[i++].address = ipv4_addr;
278         }
279
280         FUNC_EXIT;
281         return device;
282 }
283
284 uam_device_info_s *_pm_util_uas_dev_info_to_uam_dev_info(const uas_device_info_t *dev)
285 {
286         FUNC_ENTRY;
287         uam_device_info_s *device;
288         int i = 0;
289
290         retv_if(NULL == dev, NULL);
291
292         device = g_new0(uam_device_info_s, 1);
293         retv_if(NULL == device, NULL);
294
295         device->operating_system = dev->os;
296         device->discriminant = dev->discriminant;
297         device->last_seen = dev->last_seen;
298         g_strlcpy(device->device_id, dev->device_id, UAM_DEVICE_ID_MAX_STRING_LEN);
299
300         memset(device->payload.duid, 0, UAM_BLE_PAYLOAD_DUID_LEN + 1);
301         memset(device->payload.bt_mac, 0, UAM_BT_MAC_ADDRESS_STRING_LEN);
302         if (dev->payload) {
303                 device->payload.primary_key = dev->payload->primary_key;
304                 device->payload.device_icon = dev->payload->device_icon;
305                 device->payload.purpose = dev->payload->purpose;
306                 if (dev->payload->duid)
307                         memcpy(device->payload.duid, dev->payload->duid, UAM_BLE_PAYLOAD_DUID_LEN);
308                 if (dev->payload->bt_mac)
309                         g_strlcpy(device->payload.bt_mac,
310                         dev->payload->bt_mac, UAM_BT_MAC_ADDRESS_STRING_LEN);
311         }
312
313         for (i = 0; i < dev->num_addr; i++) {
314                 UAM_DBG("Address[%d]: %s", i, dev->addr_list[i].address);
315                 switch (dev->addr_list[i].type) {
316                 case UAS_ADDR_TYPE_BLE:
317                         device->type = UAM_TECH_TYPE_BLE;
318                         g_strlcpy(device->mac, dev->addr_list[i].address,
319                                         UAM_MAC_ADDRESS_STRING_LEN);
320                         break;
321                 case UAS_ADDR_TYPE_BT:
322                         device->type = UAM_TECH_TYPE_BT;
323                         g_strlcpy(device->mac, dev->addr_list[i].address,
324                                         UAM_MAC_ADDRESS_STRING_LEN);
325                         break;
326                 case UAS_ADDR_TYPE_P2P:
327                         device->type = UAM_TECH_TYPE_P2P;
328                         g_strlcpy(device->mac, dev->addr_list[i].address,
329                                         UAM_MAC_ADDRESS_STRING_LEN);
330                         break;
331                 case UAS_ADDR_TYPE_WIFI:
332                         device->type = UAM_TECH_TYPE_WIFI;
333                         g_strlcpy(device->mac, dev->addr_list[i].address,
334                                         UAM_MAC_ADDRESS_STRING_LEN);
335                         break;
336                 case UAS_ADDR_TYPE_IPv4:
337                         g_strlcpy(device->ipv4_addr, dev->addr_list[i].address,
338                                         UAM_IP_ADDRESS_MAX_STRING_LEN);
339                         break;
340                 case UAS_ADDR_TYPE_IPv6:
341                 default:
342                         UAM_ERR("Unsupported address type: %d", dev->addr_list[i].type);
343                 }
344         }
345
346         FUNC_EXIT;
347         return device;
348 }
349
350 uam_active_scan_event_e _pm_util_uas_scan_event_to_uam_scan_event(uas_active_scan_event_e event)
351 {
352         switch (event) {
353         case UAS_ACTIVE_DEVICE_FOUND:
354                 return UAM_ACTIVE_DEVICE_FOUND;
355         case UAS_ACTIVE_SCAN_COMPLETED:
356                 return UAM_ACTIVE_SCAN_COMPLETED;
357         default:
358                 UAM_WARN("Unknown event 0x%8.8X", event);
359                 return 0;
360         }
361 }
362
363 uam_sensor_info_s *_pm_util_uas_sensor_info_to_uam_sensor_info(
364         const uas_sensor_info_t *info)
365 {
366         FUNC_ENTRY;
367         uam_sensor_info_s *sensor_info;
368         unsigned int i = 0;
369         retv_if(NULL == info, NULL);
370
371         sensor_info = g_new0(uam_sensor_info_s, 1);
372         retv_if(NULL == sensor_info, NULL);
373
374         sensor_info->status = info->status;
375         sensor_info->timestamp = info->timestamp;
376         sensor_info->accuracy = info->accuracy;
377         sensor_info->count = info->count;
378         for (i = 0; i < info->count; i++) {
379                 sensor_info->values[i] = info->values[i];
380         }
381
382         UAM_INFO("t [%llu] Accuaracy [%d] Count [%d] Lux [%f] CCT [%f] Lv[%f] Cv[%f]",
383                 sensor_info->timestamp, sensor_info->accuracy, sensor_info->count,
384                 sensor_info->values[0], sensor_info->values[1], sensor_info->values[2],
385                 sensor_info->values[3]);
386
387         FUNC_EXIT;
388         return sensor_info;
389 }