2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include "ua-plugin.h"
22 #include "ua-internal.h"
23 #include "ua-manager-common.h"
24 #include "ua-plugin-manager.h"
25 #include "ua-manager-database.h"
26 #include "ua-cloud-plugin-handler.h"
27 #include "ua-vendor-plugin-manager.h"
29 #define UAM_MAX_USERS 255
30 #define USER_ACCOUNT_DEFAULT "default@default.com"
31 #define USER_ACCOUNT_DEFAULT_SUFFIX "@default.com"
32 #define USER_NAME_DEFAULT "default"
33 #define UAM_APP_NUM_MAX_LEN 50
38 uam_pm_detection_mode_e mode;
39 uam_db_service_info_t *service;
48 static GSList *users; /* List of users - uam_db_user_info_t */
49 static GSList *devices; /* List of devices - uam_db_device_info_t */
50 static GSList *services; /* List of services - uam_db_service_info_t */
51 static GSList *svc_devs; /* List of service device mapping - uam_svc_dev_info_t */
52 static GSList *svc_users; /* List of service user mapping - uam_svc_user_info_t */
54 static GSList *payloads; /* List of payloads - uam_db_payload_info_t */
55 static GSList *temp_devices; /* List of devices whichi will be temporary and \
56 will be cleared in the end of addition \
57 uam_db_device_info_t */
59 static GSList *monitors; /* List of monitoring apps - uam_monitor_info_t */
60 static GSList *scanners; /* List of scanning apps - uam_scanner_info_t */
62 static guint detection_timer = 0;
63 static unsigned int detection_window;
64 static unsigned int detecting_sensors = 0;
66 /* Utility functions to print */
67 static void __print_service(gpointer data, gpointer user_data)
69 uam_db_service_info_t *service = data;
70 uam_db_tech_info_t *tech = user_data;
73 ret_if(NULL == service);
75 UAM_DBG("DevId: %s, Type: %d, Svc: %s, Cycle: %d",
76 tech->device->device_id, tech->tech_type,
77 service->name, service->cycle);
80 static void __print_svc_dev(gpointer data, gpointer user_data)
82 uam_svc_dev_info_t *svc_dev = data;
84 ret_if(NULL == svc_dev);
86 UAM_DBG("DevId: %s, Svc: %s, payload primary key: %d",
87 svc_dev->device_id, svc_dev->service,
88 svc_dev->payload->primary_key);
91 void __print_payload(const uam_ble_payload_s *payload)
93 ret_if(NULL == payload);
94 int user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - payload->device_uid_len;
96 UAM_DBG("Payload primary key: [0x%2.2X], secondary_key: [0x%2.2X], " \
97 "device_type: [0x%2.2X] device uid len: [%d]",
98 payload->primary_key, payload->secondary_key, payload->device_type,
99 payload->device_uid_len);
101 for (int i = 0; i < payload->device_uid_len; i++)
102 UAM_DBG("Device uid [0x%2.2X]", payload->device_uid[i]);
104 for (int i = 0; i < user_data_len; i++)
105 UAM_DBG("User data [0x%2.2X]", payload->user_data[i]);
108 void __print_db_payload(const uam_db_payload_info_t *payload)
110 ret_if(NULL == payload);
112 int user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - payload->device_uid_len;
114 UAM_DBG("Payload primary key: [0x%2.2X], secondary_key: [0x%2.2X], " \
115 "device type: [0x%2.2X] device uid len: [%d]",
116 payload->primary_key, payload->secondary_key, payload->device_type,
117 payload->device_uid_len);
119 for (int i = 0; i < payload->device_uid_len; i++)
120 UAM_DBG("Device uid [0x%2.2X]", payload->device_uid[i]);
122 for (int i = 0; i < user_data_len; i++)
123 UAM_DBG("User data [0x%2.2X]", payload->user_data[i]);
126 /* Utility functions to compare */
127 static gint __compare_user_account(gconstpointer data, gconstpointer user_data)
129 const uam_db_user_info_t *user = data;
130 const char *account = user_data;
132 retv_if(NULL == user, -1);
133 retv_if(NULL == user->account, -1);
134 retv_if(NULL == account, -1);
136 return g_strcmp0(user->account, account);
139 static gint __compare_user(gconstpointer data, gconstpointer user_data)
141 const uam_db_user_info_t *user = data;
142 const uam_db_user_info_t *temp_user = user_data;
144 retv_if(NULL == user, -1);
145 retv_if(NULL == user->account, -1);
146 retv_if(NULL == temp_user, -1);
147 retv_if(NULL == temp_user->account, -1);
149 UAM_DBG("user list app_num %d account %s " \
150 "temp user app_num %d account %s",
151 user->app_num, user->account,
152 temp_user->app_num, temp_user->account);
154 if (user->app_num != temp_user->app_num)
157 return g_strcmp0(user->account, temp_user->account);
160 static gint __compare_user_id(gconstpointer data, gconstpointer user_data)
162 const uam_db_user_info_t *user = data;
163 const int *id = user_data;
165 retv_if(NULL == user, -1);
166 retv_if(NULL == user->account, -1);
167 retv_if(NULL == id, -1);
169 UAM_INFO("[%d][%d]", *id, user->user_id);
171 if (*id != user->user_id)
177 static gint __compare_service(gconstpointer data, gconstpointer user_data)
179 const uam_db_service_info_t *service = data;
180 const uam_db_service_info_t *tmp = user_data;
182 retv_if(NULL == service, -1);
183 retv_if(NULL == service->name, -1);
184 retv_if(NULL == tmp, -1);
185 retv_if(NULL == tmp->name, -1);
187 if (service->app_num == tmp->app_num)
188 return g_strcmp0(service->name, tmp->name);
193 static gint __compare_svc_user(gconstpointer data, gconstpointer user_data)
195 const uam_svc_user_info_t *svc_user = data;
196 const uam_svc_user_info_t *tmp_svc_user = user_data;
198 retv_if(NULL == svc_user, -1);
199 retv_if(NULL == tmp_svc_user, -1);
200 retv_if(NULL == svc_user->svc_name, -1);
201 retv_if(NULL == tmp_svc_user->svc_name, -1);
202 retv_if(NULL == svc_user->account, -1);
203 retv_if(NULL == tmp_svc_user->account, -1);
205 if (g_strcmp0(svc_user->svc_name, tmp_svc_user->svc_name) == 0)
206 if (g_strcmp0(svc_user->account, tmp_svc_user->account) == 0)
207 return svc_user->app_num == tmp_svc_user->app_num ? 0 : -1;
214 static gint __compare_svc_user_account(gconstpointer data, gconstpointer user_data)
216 const uam_svc_user_info_t *svc_user = data;
217 const uam_db_user_info_t *user = user_data;
219 retv_if(NULL == svc_user, -1);
220 retv_if(NULL == user, -1);
222 if (g_strcmp0(svc_user->account, user->account) == 0)
223 return svc_user->app_num == user->app_num ? 0 : -1;
228 static gint __compare_payload(gconstpointer data, gconstpointer user_data)
230 const uam_db_payload_info_t *db_payload = data;
231 const uam_ble_payload_s *payload = user_data;
233 __print_payload(payload);
234 __print_db_payload(db_payload);
236 retv_if(NULL == db_payload, -1);
237 retv_if(NULL == payload, -1);
239 if ((db_payload->primary_key == payload->primary_key) &&
240 (db_payload->secondary_key == payload->secondary_key) &&
241 (db_payload->device_uid_len == payload->device_uid_len) &&
242 (0 == memcmp(db_payload->device_uid, payload->device_uid,
243 payload->device_uid_len)))
249 static gint __compare_device(gconstpointer data, gconstpointer user_data)
252 const uam_db_device_info_t *device = data;
253 const uam_db_device_info_t *temp_device = user_data;
255 retv_if(NULL == device, -1);
256 retv_if(NULL == device->device_id, -1);
257 retv_if(NULL == temp_device, -1);
258 retv_if(device->app_num != temp_device->app_num, -1);
261 return g_strcmp0(device->device_id, temp_device->device_id);
264 static gint __compare_device_id(gconstpointer data, gconstpointer user_data)
267 const uam_db_device_info_t *device = data;
268 const char *dev_id = user_data;
270 retv_if(NULL == device, -1);
271 retv_if(NULL == device->device_id, -1);
272 retv_if(NULL == dev_id, -1);
275 return g_strcmp0(device->device_id, dev_id);
278 static gint __compare_device_id_and_appnum(gconstpointer data, gconstpointer user_data)
281 const uam_db_device_info_t *device = data;
282 const uam_device_info_s *dev_info = user_data;
284 retv_if(NULL == device, -1);
285 retv_if(NULL == device->device_id, -1);
286 retv_if(NULL == dev_info->device_id, -1);
287 retv_if(device->app_num != dev_info->app_num, -1);
290 return g_strcmp0(device->device_id, dev_info->device_id);
293 static gint __compare_tech_type(gconstpointer data, gconstpointer user_data)
296 const uam_db_tech_info_t *tech = data;
297 const int *type = user_data;
299 retv_if(NULL == tech, -1);
300 retv_if(NULL == type, -1);
303 return ((*type == tech->tech_type) ? 0 : 1);
306 static gint __compare_app_key(gconstpointer data, gconstpointer user_data)
308 const uam_sender_app_t *app_data = data;
309 const char *app_key = user_data;
311 retv_if(NULL == app_data, -1);
312 retv_if(NULL == app_data->app_key, -1);
313 retv_if(NULL == app_key, -1);
315 return g_strcmp0(app_data->app_key, app_key);
318 /* Utility functions to get pm info */
319 unsigned int _uam_core_get_env_sensors()
321 return _uam_pm_get_env_sensors();
324 unsigned int _uam_core_get_user_sensors()
326 return _uam_pm_get_user_sensors();
329 /* Utility functions to free memory */
330 static void __free_address_info(gpointer data)
333 uam_db_address_info_t *addr = data;
335 ret_if(NULL == addr);
337 g_free(addr->address);
343 static void __free_dev_tech_info(gpointer data)
346 uam_db_tech_info_t *tech_info = data;
349 ret_if(NULL == tech_info);
351 /* Delete the tech information from the service list that includes tech. */
352 for (l = tech_info->svc_list; NULL != l; l = g_slist_next(l)) {
353 uam_db_service_info_t *svc_info = l->data;
355 if (!svc_info || !svc_info->dev_techs)
358 svc_info->dev_techs = g_slist_remove(svc_info->dev_techs, tech_info);
361 g_slist_free_full(tech_info->addresses, __free_address_info);
362 tech_info->addresses = NULL;
369 static void __free_user_device(gpointer data)
372 uam_db_device_info_t *device = data;
373 ret_if(NULL == device);
374 /* Remove device data from global device list */
375 devices = g_slist_remove(devices, device);
377 /* Free allocated memory */
378 g_free(device->device_id);
380 g_slist_free_full(device->tech_list, __free_dev_tech_info);
387 static void __free_user_info(gpointer data)
390 uam_db_user_info_t *user = data;
392 ret_if(NULL == user);
395 g_free(user->account);
400 static void __free_service_info(gpointer data)
403 uam_db_service_info_t *svc = data;
412 static void __free_monitor_info(gpointer data)
415 uam_monitor_info_t *monitor = data;
417 ret_if(NULL == monitor);
419 g_free(monitor->name);
424 static void __free_scanner_info(gpointer data)
427 uam_scanner_info_t *scanner = data;
429 ret_if(NULL == scanner);
431 g_free(scanner->name);
436 /* Utility functions to copy */
437 static void __uam_core_copy_addr(uam_device_info_s *device, uam_db_address_info_t *addr)
439 switch (addr->addr_type) {
440 case UAM_ADDR_TYPE_BLE:
441 case UAM_ADDR_TYPE_BT:
442 case UAM_ADDR_TYPE_P2P:
443 case UAM_ADDR_TYPE_WIFI:
444 case UAM_ADDR_TYPE_UWB:
445 g_strlcpy(device->mac, addr->address,
446 UAM_MAC_ADDRESS_STRING_LEN);
448 case UAM_ADDR_TYPE_IPv4:
449 g_strlcpy(device->ipv4_addr,
451 UAM_IP_ADDRESS_MAX_STRING_LEN);
454 UAM_WARN("Unknown address type %d", addr->addr_type);
458 static int __copy_tech_info_to_device_info(uam_db_tech_info_t *tech, uam_device_info_s *device)
463 retv_if(NULL == tech, UAM_ERROR_INVALID_PARAMETER);
465 memset(device, 0x00, sizeof(uam_device_info_s));
466 for (l = tech->addresses; NULL != l; l = g_slist_next(l)) {
467 uam_db_address_info_t *addr = l->data;
472 __uam_core_copy_addr(device, addr);
475 device->operating_system = tech->os; // make it display the tech->os as the device->os
476 g_strlcpy(device->device_id, tech->device->device_id,
477 UAM_DEVICE_ID_MAX_STRING_LEN);
478 device->type = tech->tech_type;
479 device->discriminant = tech->discriminant;
480 device->last_seen = tech->last_seen;
481 device->app_num = tech->device->app_num;
484 return UAM_ERROR_NONE;
487 static void __uam_copy_db_payload_info(uam_ble_payload_s *dst_payload,
488 uam_db_payload_info_t *src_payload)
492 int user_data_len = 0;
493 ret_if(NULL == src_payload);
495 dst_payload->primary_key = src_payload->primary_key;
496 dst_payload->device_type = src_payload->device_type;
497 dst_payload->secondary_key = src_payload->secondary_key;
498 dst_payload->ext_val1 = src_payload->ext_val1;
499 dst_payload->ext_val2 = src_payload->ext_val2;
500 dst_payload->device_uid_len = src_payload->device_uid_len;
502 memset(dst_payload->device_uid, 0, src_payload->device_uid_len);
503 if (src_payload->device_uid)
504 memcpy(dst_payload->device_uid,
505 src_payload->device_uid, src_payload->device_uid_len);
507 user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - src_payload->device_uid_len;
508 ret_if(0 == user_data_len);
509 memset(dst_payload->user_data, 0, user_data_len);
510 if (src_payload->user_data)
511 memcpy(dst_payload->user_data,
512 src_payload->user_data, user_data_len);
518 static void __uam_copy_uam_payload_info(
519 uam_db_payload_info_t *dst_payload, uam_ble_payload_s *src_payload)
523 int user_data_len = 0;
524 ret_if(NULL == src_payload);
526 dst_payload->primary_key = src_payload->primary_key;
527 dst_payload->device_type = src_payload->device_type;
528 dst_payload->secondary_key = src_payload->secondary_key;
529 dst_payload->ext_val1 = src_payload->ext_val1;
530 dst_payload->ext_val2 = src_payload->ext_val2;
531 dst_payload->device_uid_len = src_payload->device_uid_len;
532 dst_payload->device_uid = g_memdup2(&(src_payload->device_uid),
533 src_payload->device_uid_len);
535 user_data_len = UAM_BLE_PAYLOAD_DEVICE_UID_MAX_LEN - 1 - src_payload->device_uid_len;
536 ret_if(0 == user_data_len);
537 dst_payload->user_data = g_memdup2(&(src_payload->user_data),
544 static void __uam_copy_uam_db_service_info(
545 uam_service_info_s *dst, uam_db_service_info_t *src)
551 memset(dst, 0x00, sizeof(uam_service_info_s));
552 g_strlcpy(dst->name, src->name, UAM_SERVICE_MAX_STRING_LEN);
553 dst->presence_threshold = src->presence_threshold;
554 dst->absence_threshold = src->absence_threshold;
555 dst->app_num = src->app_num;
560 static void __uam_copy_uam_service_info(
561 uam_db_service_info_t *dst, uam_service_info_s *src)
567 dst->name = g_strdup(src->name);
568 dst->presence_threshold = src->presence_threshold;
569 dst->absence_threshold = src->absence_threshold;
570 dst->app_num = src->app_num;
575 static void __uam_copy_db_service_info(
576 uam_db_service_info_t *dst, db_service_info_t *src)
582 dst->name = g_strdup(src->service_name);
583 dst->presence_threshold = src->presence_threshold;
584 dst->absence_threshold = src->absence_threshold;
585 dst->app_num = src->app_num;
586 dst->cycle = src->cycle;
591 /* Utility functions to search */
592 GSList* __search_service(GSList* list, const char *svc_name, int app_num)
594 /* Search user in existing user list */
596 uam_db_service_info_t *temp_svc;
598 temp_svc = g_new0(uam_db_service_info_t, 1);
599 temp_svc->name = g_strdup(svc_name);
600 temp_svc->app_num = app_num;
602 l = g_slist_find_custom(list, temp_svc, __compare_service);
604 g_free(temp_svc->name);
610 GSList* __search_user(const char *account, int app_num)
612 /* Search user in existing user list */
614 uam_db_user_info_t *temp_user;
616 temp_user = g_new0(uam_db_user_info_t, 1);
617 temp_user->account = g_strdup(account);
618 temp_user->app_num = app_num;
620 l = g_slist_find_custom(users, temp_user, __compare_user);
622 free_n_null(&(temp_user->account));
623 free_n_null(&temp_user);
628 GSList* __search_svc_user(const char* svc_name, const char* account, int app_num)
632 uam_svc_user_info_t *svc_user;
634 svc_user = g_new0(uam_svc_user_info_t, 1);
635 svc_user->svc_name = g_strdup(svc_name);
636 svc_user->account = g_strdup(account);
637 svc_user->app_num = app_num;
639 l = g_slist_find_custom(svc_users, svc_user, __compare_svc_user);
641 free_n_null(&(svc_user->svc_name));
642 free_n_null(&(svc_user->account));
643 free_n_null(&svc_user);
648 GSList* __search_device(const char *device_id, int app_num)
650 /* Search device in existing device list */
652 uam_db_device_info_t *temp_dev;
654 temp_dev = g_new0(uam_db_device_info_t, 1);
655 temp_dev->device_id = g_strdup(device_id);
656 temp_dev->app_num = app_num;
658 l = g_slist_find_custom(devices, temp_dev, __compare_device);
659 free_n_null(&(temp_dev->device_id));
660 free_n_null(&temp_dev);
665 static gboolean __is_mac_addr(uam_addr_type_e addr_type)
667 if (addr_type == UAM_ADDR_TYPE_BT ||
668 addr_type == UAM_ADDR_TYPE_BLE ||
669 addr_type == UAM_ADDR_TYPE_WIFI ||
670 addr_type == UAM_ADDR_TYPE_UWB ||
671 addr_type == UAM_ADDR_TYPE_P2P)
676 static char *__get_mac_addr(uam_db_tech_info_t *tech)
681 retv_if(NULL == tech, NULL);
683 for (l = tech->addresses; NULL != l; l = g_slist_next(l)) {
684 uam_db_address_info_t *addr = l->data;
689 if (__is_mac_addr(addr->addr_type))
690 return addr->address;
697 static uam_monitor_info_t *__uam_find_monitor(GSList *monitor_list,
698 const char *name, const char *svc_name, uam_pm_detection_mode_e mode, const int app_num)
703 retv_if(NULL == name, NULL);
704 retv_if(NULL == monitor_list, NULL);
706 for (l = monitor_list; NULL != l; l = g_slist_next(l)) {
707 uam_monitor_info_t *monitor = l->data;
709 if (!monitor || !monitor->name ||
710 !monitor->service || !monitor->service->name)
713 if ((mode == monitor->mode) &&
714 (0 == g_strcmp0(monitor->name, name)) &&
715 (0 == g_strcmp0(monitor->service->name, svc_name)) &&
716 (monitor->service->app_num == app_num)) {
717 UAM_DBG("Monitoring application found in list");
726 /* Utility functions to convert info */
727 static unsigned int __uam_core_tech_type_to_addr_type(uam_tech_type_e tech_type)
730 case UAM_TECH_TYPE_BLE:
731 return UAM_ADDR_TYPE_BLE;
732 case UAM_TECH_TYPE_BT:
733 return UAM_ADDR_TYPE_BT;
734 case UAM_TECH_TYPE_P2P:
735 return UAM_ADDR_TYPE_P2P;
736 case UAM_TECH_TYPE_WIFI:
737 return UAM_ADDR_TYPE_WIFI;
738 case UAM_TECH_TYPE_WIFI_LOCATION:
739 return UAM_ADDR_TYPE_WIFI;
740 case UAM_TECH_TYPE_UWB:
741 return UAM_ADDR_TYPE_UWB;
743 UAM_ERR("Unknown tech type: %d", tech_type);
748 static unsigned int __uam_core_sensor_to_tech_type(unsigned int sensor)
751 case UAM_SENSOR_BITMASK_BLE:
752 return UAM_TECH_TYPE_BLE;
753 case UAM_SENSOR_BITMASK_WIFI:
754 return UAM_TECH_TYPE_WIFI;
755 case UAM_SENSOR_BITMASK_WIFI_LOCATION:
756 return UAM_TECH_TYPE_WIFI_LOCATION;
757 case UAM_SENSOR_BITMASK_UWB:
758 return UAM_TECH_TYPE_UWB;
760 UAM_ERR("Unknown sensor: %d", sensor);
765 static unsigned int __uam_core_tech_type_to_sensor(unsigned int tech_type)
768 case UAM_TECH_TYPE_BLE:
769 return UAM_SENSOR_BITMASK_BLE;
770 case UAM_TECH_TYPE_WIFI:
771 return UAM_SENSOR_BITMASK_WIFI;
772 case UAM_TECH_TYPE_WIFI_LOCATION:
773 return UAM_SENSOR_BITMASK_WIFI_LOCATION;
774 case UAM_TECH_TYPE_UWB:
775 return UAM_SENSOR_BITMASK_UWB;
777 UAM_ERR("Unknown tech type: %d", tech_type);
782 /* Other Utility functions */
783 static void __send_device_event(int err, int event, const uam_device_info_s *dev_info)
789 UAM_INFO_C("Send %s to applications", _uam_manager_event_to_str(event));
790 /* Send device event to application */
791 param = g_variant_new("(iiisss)",
793 dev_info->operating_system,
797 dev_info->device_id);
799 _uam_get_sender_from_app_num(dev_info->app_num, &dest);
801 if (UAM_ERROR_NONE != _uam_manager_send_event(dest, event, param))
802 UAM_ERR("Failed to send %s", _uam_manager_event_to_str(event));
807 static void __add_service_to_dev_tech_mapping(
808 uam_db_tech_info_t *tech, uam_db_service_info_t *service)
812 ret_if(NULL == tech);
813 ret_if(NULL == service);
815 tech->svc_list = g_slist_append(tech->svc_list, service);
816 service->dev_techs = g_slist_append(service->dev_techs, tech);
817 g_slist_foreach(tech->svc_list, __print_service, tech);
822 static void __add_svc_dev_to_dev_tech_mapping(
823 uam_db_tech_info_t *tech, uam_svc_dev_info_t *svc_dev)
827 ret_if(NULL == tech);
828 ret_if(NULL == svc_dev);
830 tech->svc_dev_list = g_slist_append(tech->svc_dev_list, svc_dev);
831 g_slist_foreach(tech->svc_dev_list, __print_svc_dev, NULL);
836 static void __remove_service_to_dev_tech_mapping(
837 uam_db_tech_info_t *tech, uam_db_service_info_t *service)
841 ret_if(NULL == tech);
842 ret_if(NULL == service);
844 tech->svc_list = g_slist_remove(tech->svc_list, service);
845 service->dev_techs = g_slist_remove(service->dev_techs, tech);
846 g_slist_foreach(tech->svc_list, __print_service, tech);
851 static int _uam_remove_user_device(uam_db_device_info_t *device)
854 uam_device_info_s dev_info;
857 int ret = UAM_ERROR_NONE;
859 retv_if(NULL == device, UAM_ERROR_INVALID_PARAMETER);
860 user_id = device->user->user_id;
862 retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED);
863 for (l = device->tech_list; NULL != l; l = g_slist_next(l)) {
864 uam_db_tech_info_t *tech = l->data;
869 /* Copy tech info to device info */
870 ret = __copy_tech_info_to_device_info(tech, &dev_info);
871 if (UAM_ERROR_NONE != ret) {
872 UAM_ERR("__copy_tech_info_to_device_info failed");
873 __uam_db_end_transaction(0);
877 /* Unregister device from plugin */
878 ret = _uam_pm_unregister_device(user_id, &dev_info);
879 if (UAM_ERROR_NONE != ret)
880 UAM_ERR("_uam_pm_unregister_device failed with %s",
881 _uam_manager_error_to_str(ret));
883 /* Send device removed event to application */
884 __send_device_event(UAM_ERROR_NONE, UAM_EVENT_DEVICE_REMOVED, &dev_info);
886 /* Remove device from database */
887 ret = _uam_device_db_delete_device_info(
888 dev_info.device_id, dev_info.type, dev_info.mac, dev_info.app_num);
889 if (UAM_ERROR_NONE != ret) {
890 UAM_ERR("_uam_device_db_delete_device_info failed");
891 __uam_db_end_transaction(0);
895 __uam_db_end_transaction(1);
901 static int __remove_uam_db_user_info(gpointer data)
904 uam_db_user_info_t **ptr = data;
905 uam_db_user_info_t *user = *ptr;
907 int ret = UAM_ERROR_NONE;
908 unsigned int bitmask;
910 for (l = user->devices; NULL != l; l = g_slist_next(l)) {
911 uam_db_device_info_t *device = l->data;
912 ret = _uam_remove_user_device(device);
913 if (UAM_ERROR_NONE != ret) {
914 UAM_ERR("_uam_remove_user_device failed");
919 g_slist_free_full(user->devices, __free_user_device);
921 bitmask = _uam_core_get_user_sensors();
923 /* Set/update registered device list to plugins */
924 if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices, bitmask))
925 UAM_ERR("_uam_pm_set_registered_devices failed");
927 /* Set/update registered device list to cloud plugin */
928 _uam_cloud_update_registered_devices();
930 __free_user_info(user);
937 static uam_db_tech_info_t *__get_tech_info_by_mac(const char *mac,
943 for (l = devices; NULL != l; l = g_slist_next(l)) {
944 uam_db_device_info_t *dev = l->data;
947 if (!dev || dev->app_num != app_num)
950 for (l1 = dev->tech_list; NULL != l1; l1 = g_slist_next(l1)) {
951 uam_db_tech_info_t *tech = l1->data;
957 for (l2 = tech->addresses; NULL != l2; l2 = g_slist_next(l2)) {
958 uam_db_address_info_t *addr = l2->data;
963 if (__is_mac_addr(addr->addr_type) &&
964 !strcasecmp(addr->address, mac)) {
965 UAM_DBG("Device found Mac: %s, type: %d",
966 addr->address, addr->addr_type);
977 static void __get_default_service_name(int app_num, gchar **dest)
979 gchar buf[UAM_APP_NUM_MAX_LEN];
980 memset(buf, 0x00, sizeof(buf));
981 g_snprintf(buf, UAM_APP_NUM_MAX_LEN, "%d", app_num);
982 *dest = g_strjoin(".", UAM_SERVICE_DEFAULT, buf, NULL);
985 static void __get_default_user(int app_num, gchar **account, gchar **user_name)
987 gchar buf[UAM_APP_NUM_MAX_LEN];
988 memset(buf, 0x00, sizeof(buf));
989 g_snprintf(buf, UAM_APP_NUM_MAX_LEN, "%d", app_num);
991 if (user_name != NULL) {
992 *user_name = g_strjoin(".", USER_NAME_DEFAULT, buf, NULL);
993 UAM_INFO("Default username: [%s]", *user_name);
994 if (account != NULL) {
995 *account = g_strjoin(NULL, *user_name, USER_ACCOUNT_DEFAULT_SUFFIX, NULL);
996 UAM_INFO("Default account: [%s]", *account);
1001 /* Give count of devices matching given details. Mac would be compared if
1002 * non-null, otherwise upto tech_type would be matched.
1004 static void _uam_device_match_count(const char *device_id, int tech_type,
1005 const char *mac, int *count)
1009 uam_db_device_info_t *device;
1010 uam_db_tech_info_t *tech;
1012 ret_if(NULL == count);
1014 ret_if(NULL == device_id);
1016 for (l = devices; l; l = g_slist_next(l)) {
1018 if (!device || !(device->supported_techs & tech_type) ||
1019 g_strcmp0(device->device_id, device_id))
1028 for (l1 = device->tech_list; l1; l1 = g_slist_next(l1)) {
1034 for (l2 = tech->addresses; l2; l2 = g_slist_next(l2)) {
1035 uam_db_address_info_t *addr = l2->data;
1039 if (__is_mac_addr(addr->addr_type) &&
1040 !strcasecmp(addr->address, mac))
1045 UAM_DBG("device match count [%d]", *count);
1049 /* Initial service_user mapping. Here, using user's device list information
1050 * and the service_device mapping, service and user are mapped.
1052 static void __init_svc_user_mapping()
1055 GSList *l, *ll, *tmp;
1057 for (l = svc_devs; NULL != l; l = g_slist_next(l)) {
1059 uam_svc_dev_info_t *svc_dev = l->data;
1060 char *device_id = svc_dev->device_id;
1062 /* Search for devices */
1063 ll = __search_device(device_id, svc_dev->app_num);
1067 uam_db_device_info_t *device = ll->data;
1068 uam_db_user_info_t *user_info = device->user;
1073 tmp = __search_svc_user(svc_dev->service, user_info->account, svc_dev->app_num);
1076 uam_svc_user_info_t *svc_user = g_new0(uam_svc_user_info_t, 1);
1077 svc_user->svc_name = g_strdup(svc_dev->service);
1078 svc_user->account = g_strdup(user_info->account);
1079 svc_user->app_num = svc_dev->app_num;
1081 svc_users = g_slist_prepend(svc_users, svc_user);
1086 static int __get_uam_db_dev_list_to_uam_dev_list(
1087 GSList *db_dev_list, uam_device_info_s **device_list,
1088 int *count, const int app_num)
1093 int ret = UAM_ERROR_NONE;
1097 /* Calculate num devices first */
1098 for (l = db_dev_list; NULL != l; l = g_slist_next(l)) {
1099 uam_db_device_info_t *db_info = l->data;
1102 if (!db_info || !db_info->device_id || db_info->app_num != app_num)
1105 for (l1 = db_info->tech_list; NULL != l1; l1 = g_slist_next(l1)) {
1106 uam_db_tech_info_t *tech = l1->data;
1108 if (!tech || !tech->addresses)
1115 UAM_INFO("Count = %d", *count);
1117 *device_list = g_new0(uam_device_info_s, *count);
1120 for (l = db_dev_list; NULL != l; l = g_slist_next(l)) {
1121 uam_db_device_info_t *db_info = l->data;
1124 if (!db_info || !db_info->device_id || db_info->app_num != app_num)
1127 for (l1 = db_info->tech_list; NULL != l1; l1 = g_slist_next(l1)) {
1128 uam_db_tech_info_t *tech = l1->data;
1130 if (!tech || !tech->addresses)
1133 /* Copy tech info to device info */
1134 ret = __copy_tech_info_to_device_info(tech, &((*device_list)[indx]));
1135 if (UAM_ERROR_NONE != ret) {
1136 UAM_ERR("__copy_tech_info_to_device_info failed");
1143 UAM_INFO("Count = %d, indx = %d", *count, indx);
1148 unsigned int _uam_core_get_active_sensors(int detection_mode)
1151 unsigned int sensors = 0;
1154 retv_if((UAM_DETECT_PRESENCE != detection_mode) &&
1155 (UAM_DETECT_ABSENCE != detection_mode) &&
1156 (UAM_DETECT_LOCATION != detection_mode), 0);
1158 for (l = monitors; NULL != l; l = g_slist_next(l)) {
1159 uam_monitor_info_t *monitor = l->data;
1161 if (!monitor || !monitor->name || (detection_mode != monitor->mode))
1164 sensors |= monitor->sensors;
1171 unsigned int _uam_core_get_active_env_sensors(int detection_mode)
1174 unsigned int sensors = 0;
1177 retv_if((UAM_DETECT_PRESENCE != detection_mode) &&
1178 (UAM_DETECT_ABSENCE != detection_mode), 0);
1180 for (l = monitors; NULL != l; l = g_slist_next(l)) {
1181 uam_monitor_info_t *monitor = l->data;
1183 if (!monitor || !monitor->name || (detection_mode != monitor->mode))
1186 sensors |= monitor->sensors & _uam_core_get_env_sensors();
1193 static GSList *_uam_core_find_svc_dev_list(uam_device_info_s *dev_info)
1196 uam_svc_dev_info_t *svc_dev = NULL;
1198 GSList *svc_dev_list = NULL;
1200 retv_if(NULL == dev_info, NULL);
1203 * Iterate over the svc_devs and find each service device information
1205 for (l = svc_devs; NULL != l; l = g_slist_next(l)) {
1208 if (!svc_dev || !svc_dev->device_id || !svc_dev->service)
1211 if ((0 == g_strcmp0(svc_dev->device_id, dev_info->device_id)) &&
1212 (svc_dev->tech_type == dev_info->type)) {
1213 svc_dev_list = g_slist_append(svc_dev_list, svc_dev);
1214 UAM_DBG("Service %s found for device in list",
1220 return svc_dev_list;
1223 void _uam_core_get_app_num_from_app_key(const char* app_key, int *app_num)
1226 uam_sender_app_t *sender_app = NULL;
1227 GSList *sender_apps = *(_uam_manager_get_sender_apps());
1229 l = g_slist_find_custom(sender_apps, app_key, __compare_app_key);
1232 UAM_DBG("App key not found in the list");
1235 sender_app = l->data;
1236 *app_num = sender_app->app_num;
1237 UAM_DBG("app_num is [%d]", *app_num);
1240 int __uam_core_get_app_num_from_sec_list_devices(const uam_device_info_s *dev_info)
1243 uam_db_device_info_t *temp_device;
1246 retv_if(NULL == dev_info, 0);
1248 l = g_slist_find_custom(temp_devices, dev_info->device_id, __compare_device_id);
1250 temp_device = l->data;
1251 return temp_device->app_num;
1258 static GSList *__convert_db_svc_list_to_uam_svc_list(GSList *db_svc_list)
1262 GSList *svc_list = NULL;
1264 retv_if(NULL == db_svc_list, NULL);
1267 * Iterate over the db_svc_list and add each service to the global
1268 * service list "services" if its not already inserted. Also append this
1269 * uam_db_service_info_t to svc_list.
1271 for (l = db_svc_list; NULL != l; l = g_slist_next(l)) {
1272 db_service_info_t *db_svc = l->data;
1273 uam_db_service_info_t *service;
1279 ll = __search_service(services, db_svc->service_name, db_svc->app_num);
1282 service = g_new0(uam_db_service_info_t, 1);
1283 __uam_copy_db_service_info(service, db_svc);
1284 services = g_slist_append(services, service);
1288 svc_list = g_slist_append(svc_list, service);
1295 static void __uam_core_add_dev_to_sec_list_devices(const uam_device_info_s *dev_info)
1298 uam_db_device_info_t *temp_device;
1301 ret_if(NULL == dev_info);
1303 /* create a new entry in list if the app_num is different */
1304 l = g_slist_find_custom(temp_devices, dev_info, __compare_device_id_and_appnum);
1306 temp_device = l->data;
1308 temp_device = g_new0(uam_db_device_info_t, 1);
1309 temp_device->device_id = g_strdup(dev_info->device_id);
1310 temp_device->os = dev_info->operating_system;
1311 temp_device->discriminant = dev_info->discriminant;
1312 temp_device->app_num = dev_info->app_num;
1314 /* Add device to global temp_devices list */
1315 temp_devices = g_slist_prepend(temp_devices, temp_device);
1317 temp_device->supported_techs |= dev_info->type;
1322 static void __uam_core_remove_dev_from_sec_list_devices(
1323 const uam_device_info_s *dev_info)
1326 uam_db_device_info_t *temp_device;
1329 ret_if(NULL == dev_info);
1331 l = g_slist_find_custom(temp_devices, dev_info->device_id,
1332 __compare_device_id);
1334 temp_device = l->data;
1337 temp_device->supported_techs &= ~(dev_info->type);
1339 if (temp_device->supported_techs == 0) {
1340 /* Remove device from global temp_devices list */
1341 temp_devices = g_slist_remove(temp_devices, temp_device);
1343 /* free memory of temp_device */
1344 g_free(temp_device->device_id);
1345 g_free(temp_device);
1352 static void __uam_core_add_dev_to_list(
1353 uam_db_user_info_t *user, const uam_device_info_s *dev_info,
1354 int presence_state, unsigned long long last_seen, GSList *svc_list,
1355 GSList *svc_dev_list)
1358 uam_db_tech_info_t *tech;
1359 uam_db_device_info_t *device;
1362 ret_if(NULL == dev_info);
1363 ret_if(NULL == user);
1365 UAM_DBG("device-id %s", dev_info->device_id);
1367 l = g_slist_find_custom(devices, dev_info->device_id, __compare_device_id);
1369 UAM_DBG("The device already exists");
1371 ret_if(user != device->user);
1372 ret_if(device->supported_techs & dev_info->type);
1374 if (device->os != dev_info->operating_system) {
1375 UAM_INFO("device->os: %d, dev_info->operating_system: %d",
1376 device->os, dev_info->operating_system);
1378 /* Update device OS type */
1379 if (UAM_OS_TYPE_INVALID == device->os)
1380 device->os = dev_info->operating_system;
1382 UAM_WARN("Strange - OS types did not match, need to check");
1385 device = g_new0(uam_db_device_info_t, 1);
1386 device->device_id = g_strdup(dev_info->device_id);
1387 device->os = dev_info->operating_system;
1388 device->discriminant = dev_info->discriminant;
1389 device->user = user;
1390 device->app_num = dev_info->app_num;
1392 /* Add device to global device list */
1393 devices = g_slist_append(devices, device);
1395 /* Add same device to user's device list */
1396 user->devices = g_slist_append(user->devices, device);
1399 tech = g_new0(uam_db_tech_info_t, 1);
1400 tech->tech_type = dev_info->type;
1401 tech->os = dev_info->operating_system;
1402 tech->presence_state = presence_state;
1403 tech->last_seen = last_seen;
1404 tech->device = device;
1405 tech->discriminant = dev_info->discriminant;
1407 tech->svc_list = svc_list;
1408 g_slist_foreach(tech->svc_list, __print_service, tech);
1409 for (l = svc_list; NULL != l; l = g_slist_next(l)) {
1410 uam_db_service_info_t *service = l->data;
1415 service->dev_techs = g_slist_prepend(service->dev_techs, tech);
1417 tech->svc_dev_list = svc_dev_list;
1418 g_slist_foreach(tech->svc_dev_list, __print_svc_dev, NULL);
1420 /* Add tech info to tech list */
1421 device->tech_list = g_slist_append(device->tech_list, tech);
1422 device->supported_techs |= tech->tech_type;
1423 UAM_INFO("device->supported_techs: 0x%8.8X", device->supported_techs);
1425 if (0 < strlen(dev_info->mac)) {
1426 uam_db_address_info_t *addr;
1428 addr = g_new0(uam_db_address_info_t, 1);
1430 addr->address = g_strdup(dev_info->mac);
1431 addr->addr_type = __uam_core_tech_type_to_addr_type(dev_info->type);
1433 UAM_DBG("MAC address %s added for tech type: %d",
1434 dev_info->mac, dev_info->type);
1435 tech->addresses = g_slist_append(tech->addresses, addr);
1439 if (0 < strlen(dev_info->ipv4_addr)) {
1440 uam_db_address_info_t *addr;
1442 UAM_DBG("IPv4 address %s added for tech type: %d",
1443 dev_info->ipv4_addr, dev_info->type);
1445 addr = g_new0(uam_db_address_info_t, 1);
1446 addr->addr_type = UAM_ADDR_TYPE_IPv4;
1447 addr->address = g_strdup(dev_info->ipv4_addr);
1449 tech->addresses = g_slist_append(tech->addresses, addr);
1455 int _uam_core_add_user(int *user_id, const char *account, const char *name,
1460 uam_db_user_info_t *user;
1462 int ret = UAM_ERROR_NONE;
1464 retv_if(!user_id, UAM_ERROR_INVALID_PARAMETER);
1465 retv_if(!account, UAM_ERROR_INVALID_PARAMETER);
1466 retv_if(!name, UAM_ERROR_INVALID_PARAMETER);
1467 retv_if(app_num <= 0, UAM_ERROR_INVALID_PARAMETER);
1469 l = __search_user(account, app_num);
1470 retv_if((NULL != l) && (l->data != NULL), UAM_ERROR_ALREADY_REGISTERED);
1472 user = g_new0(uam_db_user_info_t, 1);
1473 /* Add user to database */
1474 ret = _uam_db_insert_user_info(&(user->user_id), name, account, app_num);
1475 if (UAM_ERROR_NONE != ret) {
1476 __free_user_info(user);
1477 UAM_ERR("_uam_db_insert_user_info failed [%d]", ret);
1481 user->name = g_strdup(name);
1482 user->account = g_strdup(account);
1483 user->app_num = app_num;
1484 user->devices = NULL;
1485 users = g_slist_append(users, user);
1487 *user_id = user->user_id;
1488 _uam_get_sender_from_app_num(app_num, &dest);
1490 /* Send user added event to application */
1491 if (UAM_ERROR_NONE != _uam_manager_send_event(dest,
1492 UAM_EVENT_USER_ADDED, g_variant_new("(iss)",
1493 UAM_ERROR_NONE, user->account, user->name)))
1494 UAM_ERR("Failed to send UAM_EVENT_USER_ADDED");
1499 return UAM_ERROR_NONE;
1502 int _uam_core_remove_user(const char *account, const int app_num)
1506 uam_db_user_info_t *user;
1508 int ret = UAM_ERROR_NONE;
1510 l = __search_user(account, app_num);
1511 retv_if((NULL == l), UAM_ERROR_NOT_REGISTERED);
1514 /* Remove user from database */
1515 ret = _uam_db_delete_by_user_id(user->user_id);
1516 if (UAM_ERROR_NONE != ret) {
1517 UAM_ERR("_uam_db_delete_by_user_id failed");
1521 users = g_slist_remove(users, user);
1522 _uam_get_sender_from_app_num(app_num, &dest);
1524 /* Send user removed event to application */
1525 if (UAM_ERROR_NONE != _uam_manager_send_event(dest,
1526 UAM_EVENT_USER_REMOVED, g_variant_new("(iss)",
1527 UAM_ERROR_NONE, user->account, user->name)))
1528 UAM_ERR("Failed to send UAM_EVENT_USER_REMOVED");
1530 ret = __remove_uam_db_user_info((gpointer)&user);
1531 if (UAM_ERROR_NONE != ret)
1532 UAM_WARN("_free_uam_db_user_info failed");
1535 return UAM_ERROR_NONE;
1538 int _uam_core_update_user(uam_user_info_s *user)
1542 uam_db_user_info_t *user_info;
1544 retv_if(NULL == user, UAM_ERROR_INVALID_PARAMETER);
1546 /* Retrieve user from list */
1547 l = __search_user(user->account, user->app_num);
1548 retv_if((NULL == l) || (l->data == NULL), UAM_ERROR_NOT_REGISTERED);
1550 user_info = l->data;
1551 g_strlcpy(user_info->name, user->name, UAM_USER_NAME_MAX_STRING_LEN);
1553 /* Update user to database */
1554 if (UAM_ERROR_NONE != _uam_db_update_user_info(user_info)) {
1555 UAM_ERR("_uam_db_update_user_info failed");
1556 return UAM_ERROR_DB_FAILED;
1560 return UAM_ERROR_NONE;
1563 int _uam_core_add_device(const char *account, uam_device_info_s *dev_info)
1568 uam_db_device_info_t *device;
1569 uam_db_user_info_t *user;
1571 retv_if(NULL == account, UAM_ERROR_INVALID_PARAMETER);
1572 retv_if(NULL == dev_info, UAM_ERROR_INVALID_PARAMETER);
1574 /* Search user to link with device */
1575 l = __search_user(account, dev_info->app_num);
1576 retv_if(l == NULL, UAM_ERROR_INVALID_PARAMETER);
1579 /* Search if device already exist */
1580 l = __search_device(dev_info->device_id, dev_info->app_num);
1582 UAM_DBG("The device id was found in the current list");
1584 retv_if(user != device->user, UAM_ERROR_ALREADY_REGISTERED);
1585 retv_if((dev_info->type & device->supported_techs),
1586 UAM_ERROR_ALREADY_REGISTERED);
1587 retv_if(dev_info->app_num != device->app_num, UAM_ERROR_INVALID_PARAMETER);
1590 UAM_INFO("device to be added for user_id [%d]", user->user_id);
1591 __uam_core_add_dev_to_sec_list_devices(dev_info);
1593 ret = _uam_pm_register_device(user->user_id, dev_info);
1594 if (UAM_ERROR_NONE != ret) {
1595 UAM_ERR("_uam_pm_register_device failed with %s",
1596 _uam_manager_error_to_str(ret));
1601 return UAM_ERROR_NONE;
1604 int _uam_core_is_device_added(uam_device_info_s *dev, gboolean *is_added)
1607 uam_db_device_info_t *device;
1610 retv_if(NULL == dev, UAM_ERROR_INVALID_PARAMETER);
1611 retv_if(NULL == is_added, UAM_ERROR_INVALID_PARAMETER);
1614 l = __search_device(dev->device_id, dev->app_num);
1619 if (!(device->supported_techs & dev->type))
1622 for (l = device->tech_list; NULL != l; l = g_slist_next(l)) {
1623 uam_db_tech_info_t *tech = l->data;
1629 if (dev->type != tech->tech_type)
1632 for (l1 = tech->addresses; NULL != l1; l1 = g_slist_next(l1)) {
1633 uam_db_address_info_t *addr = l1->data;
1638 if (__is_mac_addr(addr->addr_type) &&
1639 !strcasecmp(addr->address, dev->mac)) {
1648 UAM_INFO("Device %s", (*is_added ? "Added" : "Not Added"));
1650 return UAM_ERROR_NONE;
1653 static int __uam_remove_device(int user_id, uam_db_device_info_t *device,
1654 const uam_device_info_s *dev_info, uam_db_tech_info_t *tech, const int app_num)
1659 unsigned int bitmask;
1661 ret = _uam_db_get_device_services_count(dev_info->device_id,
1662 dev_info->type, dev_info->mac, dev_info->app_num, &count);
1663 if (UAM_ERROR_NONE != ret) {
1664 UAM_ERR("_uam_db_get_device_services_count failed with %s",
1665 _uam_manager_error_to_str(ret));
1670 ret = UAM_ERROR_RESOURCE_BUSY;
1671 UAM_WARN("other service uses this device ref:[%d]", count);
1675 _uam_device_match_count(dev_info->device_id, dev_info->type,
1676 dev_info->mac, &count);
1677 /* If no copies present then remove from plugin too.
1678 * Note: Discriminant is not considered for current logic.
1681 ret = _uam_pm_unregister_device(user_id, dev_info);
1682 if (UAM_ERROR_NONE != ret) {
1683 UAM_ERR("_uam_pm_unregister_device failed with %s",
1684 _uam_manager_error_to_str(ret));
1689 /* Send device removed event to application */
1690 __send_device_event(ret, UAM_EVENT_DEVICE_REMOVED, dev_info);
1692 retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED);
1693 /* Remove device from database */
1694 ret = _uam_device_db_delete_device_info(dev_info->device_id, dev_info->type,
1695 dev_info->mac, dev_info->app_num);
1696 if (UAM_ERROR_NONE != ret) {
1697 UAM_ERR("_uam_device_db_delete_device_info failed");
1698 __uam_db_end_transaction(0);
1702 /* Remove device from service */
1703 for (l = tech->svc_list; l; l = g_slist_next(l)) {
1704 uam_db_service_info_t *svc = l->data;
1706 if (!svc || !svc->name)
1708 //TODO: check _uam_core_service_remove_device for app_num updates
1709 ret = _uam_core_service_remove_device(svc->name,
1710 dev_info->device_id, dev_info->type, app_num);
1711 if (UAM_ERROR_NONE != ret)
1712 UAM_ERR("_uam_device_db_delete_device_info failed");
1714 __uam_db_end_transaction(1);
1716 bitmask = __uam_core_tech_type_to_sensor(tech->tech_type);
1717 /* Remove tech info from device's tech list */
1718 device->tech_list = g_slist_remove(device->tech_list, tech);
1719 device->supported_techs &= ~(tech->tech_type);
1720 UAM_INFO("device->supported_techs: %8.8X", device->supported_techs);
1721 __free_dev_tech_info(tech);
1723 if (UAM_TECH_TYPE_NONE == device->supported_techs) {
1724 /* Remove device from global device list */
1725 devices = g_slist_remove(devices, device);
1727 /* Remove device from user's device list */
1728 device->user->devices = g_slist_remove(device->user->devices, device);
1729 __free_user_device(device);
1733 /* Set/update registered device list to plugins */
1734 if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices, bitmask))
1735 UAM_ERR("_uam_pm_set_registered_devices failed");
1737 /* Set/update registered device list to cloud plugin */
1738 _uam_cloud_update_registered_devices();
1741 return UAM_ERROR_NONE;
1744 int _uam_core_remove_device(const char *account,
1745 const uam_device_info_s *dev_info, const int app_num)
1750 uam_db_tech_info_t *tech;
1751 uam_db_device_info_t *device;
1752 uam_db_user_info_t *user;
1754 retv_if(NULL == account, UAM_ERROR_INVALID_PARAMETER);
1755 retv_if(NULL == dev_info, UAM_ERROR_INVALID_PARAMETER);
1757 /* Retrieve user from list */
1758 l = __search_user(account, dev_info->app_num);
1759 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
1762 /* Retrieve device from list */
1763 l = __search_device(dev_info->device_id, dev_info->app_num);
1764 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
1767 retv_if(user != device->user, UAM_ERROR_INVALID_PARAMETER);
1768 retv_if(!(device->supported_techs & dev_info->type), UAM_ERROR_INVALID_PARAMETER);
1770 /* Retrieve tech info from list */
1771 l = g_slist_find_custom(device->tech_list,
1772 &(dev_info->type), __compare_tech_type);
1773 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
1776 ret = __uam_remove_device(user->user_id, device, dev_info, tech, app_num);
1782 int _uam_core_remove_device_by_device_id(const char *device_id,
1783 int tech_type, const int app_num)
1788 uam_device_info_s dev_info;
1789 uam_db_tech_info_t *tech;
1790 uam_db_device_info_t *device;
1792 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
1793 retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
1794 retv_if(UAM_TECH_TYPE_P2P < tech_type, UAM_ERROR_INVALID_PARAMETER);
1796 /* Retrieve device from list */
1797 l = __search_device(device_id, app_num);
1798 retv_if(NULL == l, UAM_ERROR_NOT_REGISTERED);
1800 retv_if(!(device->supported_techs & tech_type), UAM_ERROR_NOT_REGISTERED);
1802 /* Retrieve tech info from list */
1803 l = g_slist_find_custom(device->tech_list,
1804 &tech_type, __compare_tech_type);
1805 retv_if(NULL == l, UAM_ERROR_INTERNAL);
1809 ret = _uam_core_get_devcie_by_device_id(device_id, tech_type, &dev_info, app_num);
1810 retv_if(UAM_ERROR_NONE != ret, UAM_ERROR_NOT_REGISTERED);
1812 ret = __uam_remove_device(device->user->user_id, device, &dev_info, tech, dev_info.app_num);
1818 int _uam_core_remove_device_by_mac(const char *mac, const int app_num)
1821 uam_db_tech_info_t *tech;
1822 uam_db_device_info_t *device;
1823 uam_device_info_s dev_info;
1826 retv_if(NULL == mac, UAM_ERROR_INVALID_PARAMETER);
1828 tech = __get_tech_info_by_mac(mac, app_num);
1829 retv_if(NULL == tech, UAM_ERROR_NOT_FOUND);
1830 device = tech->device;
1832 ret = __copy_tech_info_to_device_info(tech, &dev_info);
1833 if (UAM_ERROR_NONE != ret) {
1834 UAM_ERR("__copy_tech_info_to_device_info failed");
1838 ret = __uam_remove_device(device->user->user_id, device, &dev_info, tech, app_num);
1844 int _uam_core_update_device(uam_device_info_s *a_device)
1848 int ret = UAM_ERROR_NONE;
1849 GSList *l, *l1, *l2;
1851 uam_device_info_s temp;
1852 uam_db_user_info_t *user = NULL;
1853 uam_db_device_info_t *device = NULL;
1854 uam_db_device_info_t *temp_dev = NULL;
1855 uam_db_tech_info_t *tech = NULL;
1857 temp_dev = g_new0(uam_db_device_info_t, 1);
1858 temp_dev->device_id = a_device->device_id;
1859 temp_dev->app_num = a_device->app_num;
1860 UAM_DBG("temp_device device id %s", temp_dev->device_id);
1861 UAM_DBG("temp_device app_num is %d", temp_dev->app_num);
1863 /* Find all tech-devices in users' devices */
1864 for (l = users; NULL != l; l = g_slist_next(l)) {
1867 UAM_DBG("user->app_num %d a_device->app_num %d", user->app_num , a_device->app_num);
1868 if (user->app_num != a_device->app_num)
1871 l1 = g_slist_find_custom(user->devices,
1872 temp_dev, __compare_device);
1874 UAM_DBG("Valid user_id [%d] but Invalid device_id [%s]",
1875 user->user_id, a_device->device_id);
1880 if (!(device->supported_techs & a_device->type)) {
1881 UAM_DBG("Valid device_id [%s] but Invalid tech type [%d]",
1882 device->device_id, a_device->type);
1886 /* Update discriminant for devices */
1887 device->discriminant = a_device->discriminant;
1889 l2 = g_slist_find_custom(device->tech_list,
1890 &(a_device->type), __compare_tech_type);
1892 UAM_DBG("device->tech_list, tech type [%d] not found", a_device->type);
1898 UAM_DBG("Device tech is NULL");
1902 /* Update discriminant for device-tech */
1903 tech->discriminant = a_device->discriminant;
1904 tech->os = a_device->operating_system;
1906 /* Update device's updated information to database */
1907 ret = __copy_tech_info_to_device_info(tech, &temp);
1909 if (UAM_ERROR_NONE != ret) {
1910 UAM_ERR("__copy_tech_info_to_device_info failed [%d]", ret);
1914 ret = _uam_device_db_update_device(temp.device_id, temp.type,
1915 temp.mac, temp.ipv4_addr, temp.operating_system, temp.discriminant, temp.app_num);
1916 if (UAM_ERROR_NONE != ret) {
1917 UAM_ERR("_uam_device_db_update_device failed [%d]", ret);
1922 /* Set/update registered device list to plugins */
1923 ret = _uam_pm_set_registered_devices(devices,
1924 __uam_core_tech_type_to_sensor(a_device->type));
1925 if (UAM_ERROR_NONE != ret) {
1926 UAM_ERR("_uam_pm_set_registered_devices failed [%d]", ret);
1930 /* Set/update registered device list to cloud plugin */
1931 _uam_cloud_update_registered_devices();
1934 free_n_null(&temp_dev);
1939 int _uam_core_get_default_user(uam_user_info_s *user_info, const int app_num)
1942 int ret = UAM_ERROR_NONE;
1945 uam_db_user_info_t *user;
1946 gchar *default_user_name = NULL;
1947 gchar *default_account_name = NULL;
1949 retv_if(NULL == user_info, UAM_ERROR_INVALID_PARAMETER);
1950 retv_if(app_num <= 0, UAM_ERROR_INVALID_PARAMETER);
1952 __get_default_user(app_num, &default_account_name, &default_user_name);
1953 UAM_DBG("default user account: [%s] name: [%s]",
1954 default_account_name, default_user_name);
1956 l = __search_user(default_account_name, app_num);
1958 ret = _uam_core_add_user(&user_id, default_account_name,
1959 default_user_name, app_num);
1961 if ((UAM_ERROR_NONE != ret) && (UAM_ERROR_ALREADY_REGISTERED != ret)) {
1962 UAM_ERR("_uam_core_add_user failed with %s", _uam_manager_error_to_str(ret));
1967 memset(user_info, 0x00, sizeof(uam_user_info_s));
1968 g_strlcpy(user_info->account, user->account, UAM_USER_ACCOUNT_MAX_STRING_LEN);
1969 g_strlcpy(user_info->name, user->name, UAM_USER_NAME_MAX_STRING_LEN);
1970 user_info->app_num = user->app_num;
1974 free_n_null(&default_user_name);
1975 free_n_null(&default_account_name);
1981 int _uam_core_get_users(int *count, uam_user_info_s **user_list,
1988 for (l = users; l; l = g_slist_next(l)) {
1989 uam_db_user_info_t *db_info = l->data;
1990 if (db_info && db_info->app_num == app_num)
1994 *user_list = g_new0(uam_user_info_s, size);
1997 /* fetch users list from DB */
1998 for (l = users; l; l = g_slist_next(l)) {
1999 uam_db_user_info_t *db_info = l->data;
2001 if (!db_info || !db_info->account || db_info->app_num != app_num)
2004 g_strlcpy((*user_list)[*count].account,
2005 db_info->account, UAM_USER_ACCOUNT_MAX_STRING_LEN);
2007 g_strlcpy((*user_list)[*count].name,
2008 db_info->name, UAM_USER_NAME_MAX_STRING_LEN);
2009 (*user_list)[*count].app_num = db_info->app_num;
2014 UAM_INFO("Count: %d", *count);
2016 return UAM_ERROR_NONE;
2019 int _uam_core_get_app_num_from_userid(int user_id, int *app_num)
2022 for (l = users; l; l = g_slist_next(l)) {
2023 uam_db_user_info_t *db_info = l->data;
2025 if (!db_info || !db_info->account)
2027 if (db_info->user_id == user_id) {
2028 *app_num = db_info->app_num;
2032 return UAM_ERROR_NONE;
2035 int _uam_core_get_user_by_account(const char *account, uam_user_info_s *user,
2039 uam_db_user_info_t *db_info;
2042 retv_if(NULL == account, UAM_ERROR_INVALID_PARAMETER);
2043 retv_if(!user, UAM_ERROR_INVALID_PARAMETER);
2045 l = __search_user(account, app_num);
2046 retv_if(NULL == l, UAM_ERROR_NOT_FOUND);
2049 g_strlcpy(user->account, db_info->account, UAM_USER_ACCOUNT_MAX_STRING_LEN);
2051 g_strlcpy(user->name, db_info->name, UAM_USER_NAME_MAX_STRING_LEN);
2052 user->app_num = db_info->app_num;
2055 return UAM_ERROR_NONE;
2058 int _uam_core_get_devcie_by_device_id(
2059 const char *device_id, int tech_type, uam_device_info_s *device, const int app_num)
2062 uam_db_device_info_t *db_info;
2066 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
2067 retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
2068 retv_if(UAM_TECH_TYPE_P2P < tech_type, UAM_ERROR_INVALID_PARAMETER);
2070 l = __search_device(device_id, app_num);
2071 retv_if(NULL == l, UAM_ERROR_NOT_FOUND);
2073 retv_if(!(tech_type & db_info->supported_techs), UAM_ERROR_NOT_FOUND);
2075 memset(device, 0x00, sizeof(uam_device_info_s));
2077 for (l1 = db_info->tech_list; NULL != l1; l1 = g_slist_next(l1)) {
2078 uam_db_tech_info_t *tech = l1->data;
2081 if (!tech || !tech->addresses || (tech->tech_type != tech_type))
2084 for (l2 = tech->addresses; NULL != l2; l2 = g_slist_next(l2)) {
2085 uam_db_address_info_t *addr = l2->data;
2090 __uam_core_copy_addr(device, addr);
2093 device->operating_system = db_info->os;
2094 g_strlcpy(device->device_id, db_info->device_id,
2095 UAM_DEVICE_ID_MAX_STRING_LEN);
2096 device->type = tech->tech_type;
2097 device->discriminant = tech->discriminant;
2098 device->app_num = db_info->app_num;
2101 retv_if(UAM_TECH_TYPE_NONE == device->type, UAM_ERROR_NOT_FOUND);
2104 return UAM_ERROR_NONE;
2107 int _uam_core_get_device_by_mac(const char *mac, uam_device_info_s *device,
2112 uam_db_tech_info_t *tech;
2114 retv_if(NULL == mac, UAM_ERROR_INVALID_PARAMETER);
2116 tech = __get_tech_info_by_mac(mac, app_num);
2117 retv_if(NULL == tech, UAM_ERROR_NOT_FOUND);
2119 ret = __copy_tech_info_to_device_info(tech, device);
2120 if (UAM_ERROR_NONE != ret) {
2121 UAM_ERR("__copy_tech_info_to_device_info failed");
2129 int _uam_core_get_user_by_device_id(const char *device_id,
2130 uam_user_info_s *user, const int app_num)
2133 uam_db_device_info_t *dev;
2136 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
2137 retv_if(!user, UAM_ERROR_INVALID_PARAMETER);
2139 l = __search_device(device_id, app_num);
2140 retv_if(NULL == l, UAM_ERROR_NOT_FOUND);
2143 g_strlcpy(user->account, dev->user->account, UAM_USER_ACCOUNT_MAX_STRING_LEN);
2144 if (dev->user->name)
2145 g_strlcpy(user->name, dev->user->name, UAM_USER_NAME_MAX_STRING_LEN);
2146 user->app_num = dev->user->app_num;
2149 return UAM_ERROR_NONE;
2152 int _uam_core_get_user_by_mac(const char *mac, uam_user_info_s *user,
2156 uam_db_tech_info_t *tech;
2158 retv_if(NULL == mac, UAM_ERROR_INVALID_PARAMETER);
2160 tech = __get_tech_info_by_mac(mac, app_num);
2161 retv_if(NULL == tech, UAM_ERROR_NOT_FOUND);
2163 g_strlcpy(user->account, tech->device->user->account, UAM_USER_ACCOUNT_MAX_STRING_LEN);
2164 if (tech->device->user->name)
2165 g_strlcpy(user->name, tech->device->user->name, UAM_USER_NAME_MAX_STRING_LEN);
2166 user->app_num = tech->device->user->app_num;
2169 return UAM_ERROR_NONE;
2172 int _uam_core_get_devices(int *count, uam_device_info_s **device_list, const int app_num)
2175 int ret = UAM_ERROR_NONE;
2177 UAM_DBG("app_num %d", app_num);
2178 ret = __get_uam_db_dev_list_to_uam_dev_list(devices, device_list, count, app_num);
2184 int _uam_core_get_user_devices(const char *account,
2185 int *count, uam_device_info_s **device_list, const int app_num)
2188 uam_db_user_info_t *user;
2190 int ret = UAM_ERROR_NONE;
2192 l = __search_user(account, app_num);
2193 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
2196 ret = __get_uam_db_dev_list_to_uam_dev_list(user->devices, device_list, count, app_num);
2202 int _uam_core_get_available_sensors(unsigned int *sensor_bitmask)
2206 *sensor_bitmask = _uam_pm_get_avaliable_sensors();
2209 return UAM_ERROR_NONE;
2212 gboolean _uam_core_is_sensor_ready(unsigned int sensor)
2217 is_ready = _uam_pm_is_sensor_ready(sensor);
2218 UAM_DBG("%8.8X is %s", sensor, (is_ready ? "Ready" : "NOT Ready"));
2224 static uam_svc_dev_info_t *_uam_core_find_svc_dev_info(const char *device_id,
2225 uam_tech_type_e tech_type, const char *svc_name, const int app_num)
2228 uam_svc_dev_info_t *svc_dev = NULL;
2231 for (l = svc_devs; NULL != l; l = g_slist_next(l)) {
2234 if (!svc_dev || !svc_dev->device_id || !svc_dev->service)
2237 if ((0 == g_strcmp0(svc_dev->device_id, device_id)) &&
2238 (0 == g_strcmp0(svc_dev->service, svc_name)) &&
2239 (svc_dev->app_num == app_num) &&
2240 (svc_dev->tech_type == tech_type)) {
2241 UAM_DBG("Service device found in list");
2250 static int _uam_core_update_svc_dev_info(const char *device_id, uam_tech_type_e tech_type,
2251 const char *svc_name, gboolean discriminant, unsigned long long last_seen,
2252 uam_ble_payload_s *payload_info, const int app_num)
2255 uam_svc_dev_info_t *svc = NULL;
2257 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
2258 retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
2259 retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
2260 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2262 svc = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name, app_num);
2264 svc = g_new0(uam_svc_dev_info_t, 1);
2265 svc->device_id = g_strdup(device_id);
2266 svc->app_num = app_num;
2267 svc->tech_type = tech_type;
2268 svc->service = g_strdup(svc_name);
2270 svc->payload = g_new0(uam_db_payload_info_t, 1);
2272 __uam_copy_uam_payload_info(svc->payload, payload_info);
2274 svc_devs = g_slist_append(svc_devs, svc);
2277 if (discriminant >= 0)
2278 svc->discriminant = discriminant;
2280 svc->last_seen = last_seen;
2282 /* TODO free existing payload structure */
2284 __uam_copy_uam_payload_info(svc->payload, payload_info);
2286 UAM_DBG("Service [%s] device [%s] tech_type [%d] discriminant [%d] " \
2287 "last_seen [%llu] payload primary key [%d] app number [%d]",
2288 svc->service, svc->device_id, svc->tech_type, svc->discriminant,
2289 svc->last_seen, svc->payload->primary_key, svc->app_num);
2292 return UAM_ERROR_NONE;
2295 static int _uam_core_update_svc_dev_info_discriminant(const char *device_id,
2296 uam_tech_type_e tech_type, const char *svc_name, gboolean discriminant, const int app_num)
2299 uam_svc_dev_info_t *svc = NULL;
2301 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
2302 retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
2303 retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
2304 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2306 svc = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name, app_num);
2308 svc = g_new0(uam_svc_dev_info_t, 1);
2309 svc->device_id = g_strdup(device_id);
2310 svc->tech_type = tech_type;
2311 svc->app_num = app_num;
2312 svc->service = g_strdup(svc_name);
2313 svc_devs = g_slist_append(svc_devs, svc);
2316 svc->discriminant = discriminant;
2318 UAM_DBG("Service [%s] device [%s] tech_type [%d] discriminant [%d] app_num [%d]",
2319 svc->service, svc->device_id, svc->tech_type, svc->discriminant, svc->app_num);
2322 return UAM_ERROR_NONE;
2325 int _uam_core_service_add_user(const char *svc_name, const char *account, const int app_num)
2329 int ret = UAM_ERROR_NONE;
2330 uam_db_user_info_t *user;
2331 uam_db_service_info_t *service;
2333 retv_if(NULL == account, UAM_ERROR_INVALID_PARAMETER);
2334 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2336 /* Retrieve user from list */
2337 l = __search_user(account, app_num);
2338 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
2341 /* Retrieve service from list */
2342 l = __search_service(services, svc_name, app_num);
2343 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
2347 retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED);
2348 for (l = user->devices; NULL != l; l = g_slist_next(l)) {
2349 uam_db_device_info_t *device = l->data;
2355 for (l1 = device->tech_list; NULL != l1; l1 = g_slist_next(l1)) {
2356 uam_db_tech_info_t *tech = l1->data;
2363 l2 = __search_service(tech->svc_list, svc_name, app_num);
2367 __add_service_to_dev_tech_mapping(tech, service);
2369 mac = __get_mac_addr(tech);
2371 /* Insert device service info to db */
2372 ret = _uam_db_insert_device_service_info(device->device_id,
2373 tech->tech_type, mac, service->name, service->cycle,
2374 device->discriminant, 0, app_num);
2375 if (UAM_ERROR_NONE != ret) {
2376 UAM_WARN("Device service addition to persistent DB failed");
2377 __uam_db_end_transaction(0);
2381 ret = _uam_core_update_svc_dev_info(device->device_id, tech->tech_type,
2382 service->name, device->discriminant, 0, NULL, app_num);
2383 if (UAM_ERROR_NONE != ret) {
2384 UAM_WARN("Device service addition to service device mapping failed");
2385 __uam_db_end_transaction(0);
2391 __uam_db_end_transaction(1);
2393 /* Add service to user mapping */
2394 l = __search_svc_user(svc_name, account, app_num);
2397 uam_svc_user_info_t *svc_user = g_new0(uam_svc_user_info_t, 1);
2398 svc_user->svc_name = g_strdup(svc_name);
2399 svc_user->account = g_strdup(account);
2400 svc_user->app_num = app_num;
2402 svc_users = g_slist_prepend(svc_users, svc_user);
2405 UAM_INFO("USER SERVICE MAPPING ALLREADY PRESENT.");
2411 int _uam_core_service_remove_user(const char *svc_name, const char *account, const int app_num)
2415 uam_db_user_info_t *user;
2416 int ret = UAM_ERROR_NONE;
2418 retv_if(NULL == account, UAM_ERROR_INVALID_PARAMETER);
2419 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2420 retv_if(app_num <= 0, UAM_ERROR_INVALID_PARAMETER);
2422 /* Retrieve user from list */
2423 l = __search_user(account, app_num);
2424 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
2428 /* Retrieve service from list */
2429 l = __search_service(services, svc_name, app_num);
2430 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
2432 retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_DB_FAILED);
2433 for (l = user->devices; NULL != l; l = g_slist_next(l)) {
2434 uam_db_device_info_t *device = l->data;
2440 for (l1 = device->tech_list; NULL != l1; l1 = g_slist_next(l1)) {
2441 uam_db_tech_info_t *tech = l1->data;
2448 l2 = __search_service(tech->svc_list, svc_name, app_num);
2452 UAM_DBG("Service %s found, remove it from list", svc_name);
2453 __remove_service_to_dev_tech_mapping(
2454 tech, (uam_db_service_info_t *)l2->data);
2456 mac = __get_mac_addr(tech);
2457 /* Remove service-device from DB */
2458 ret = _uam_db_delete_device_service_info(device->device_id,
2459 tech->tech_type, mac, svc_name, app_num);
2460 if (UAM_ERROR_NONE != ret) {
2461 UAM_WARN("Device service removal from persistent DB failed");
2462 __uam_db_end_transaction(0);
2468 __uam_db_end_transaction(1);
2470 /* Removne service-user mapping from svc_users */
2472 l = __search_svc_user(svc_name, account, app_num);
2473 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
2475 uam_svc_user_info_t *svc_user = l->data;
2476 svc_users = g_slist_remove_all(svc_users, svc_user);
2478 g_free(svc_user->svc_name);
2479 g_free(svc_user->account);
2483 return UAM_ERROR_NONE;
2486 static uam_db_tech_info_t *__uam_core_get_dev_tech_info(const char *device_id, int tech_type)
2489 uam_db_device_info_t *device;
2492 retv_if(NULL == device_id, NULL);
2493 retv_if(UAM_TECH_TYPE_NONE >= tech_type, NULL);
2494 retv_if(UAM_TECH_TYPE_MAX <= tech_type, NULL);
2496 l = g_slist_find_custom(devices, device_id, __compare_device_id);
2498 UAM_DBG("DeviceId [%s] is not in the list", device_id);
2503 if (!device || !(device->supported_techs & tech_type)) {
2504 UAM_DBG("Device type [0x%2.2X] for deviceId [%s] not found",
2505 tech_type, device_id);
2509 for (l = device->tech_list; NULL != l; l = g_slist_next(l)) {
2510 uam_db_tech_info_t *tech = l->data;
2515 if (tech_type == tech->tech_type) {
2516 UAM_DBG("DeviceId [%s], Device type [0x%2.2X] found",
2517 device_id, tech_type);
2522 UAM_DBG("DeviceId [%s], Device type [0x%2.2X] not found", device_id, tech_type);
2527 int _uam_core_service_add_device(const char *svc_name, const char *device_id,
2528 int tech_type, const int app_num)
2533 int ret = UAM_ERROR_NONE;
2534 uam_db_tech_info_t *tech_info;
2535 uam_db_service_info_t *service;
2536 uam_svc_dev_info_t *svc_dev = NULL;
2538 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2539 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
2540 retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
2541 retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
2543 tech_info = __uam_core_get_dev_tech_info(device_id, tech_type);
2544 retv_if(NULL == tech_info, UAM_ERROR_INVALID_PARAMETER);
2546 l = __search_service(tech_info->svc_list, svc_name, app_num);
2547 retv_if(NULL != l, UAM_ERROR_ALREADY_REGISTERED);
2549 /* Retrieve service from list */
2550 l = __search_service(services, svc_name, app_num);
2551 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
2555 __add_service_to_dev_tech_mapping(tech_info, service);
2557 _uam_core_update_svc_dev_info(device_id, tech_type,
2558 service->name, -1, -1, NULL, app_num);
2559 svc_dev = _uam_core_find_svc_dev_info(device_id, tech_type, service->name, app_num);
2560 __add_svc_dev_to_dev_tech_mapping(tech_info, svc_dev);
2562 mac = __get_mac_addr(tech_info);
2563 /* Insert device service info to db */
2564 ret = _uam_db_insert_device_service_info(device_id, tech_type, mac,
2565 service->name, service->cycle, tech_info->discriminant, 0, app_num);
2566 if (UAM_ERROR_NONE != ret) {
2567 UAM_WARN("Device service addition to persistent DB failed");
2570 ret = _uam_core_update_svc_dev_info(device_id, tech_type, service->name,
2571 tech_info->discriminant, 0, NULL, app_num);
2572 if (UAM_ERROR_NONE != ret) {
2573 UAM_WARN("Device service addition to svc dev mapping failed");
2578 return UAM_ERROR_NONE;
2581 int _uam_core_service_remove_device(const char *svc_name,
2582 const char *device_id, int tech_type, const int app_num)
2587 uam_db_tech_info_t *tech_info;
2588 int ret = UAM_ERROR_NONE;
2590 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2591 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
2592 retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
2593 retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
2595 /* Retrieve service from list */
2596 l = __search_service(services, svc_name, app_num);
2597 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
2599 tech_info = __uam_core_get_dev_tech_info(device_id, tech_type);
2600 retv_if(NULL == tech_info, UAM_ERROR_INVALID_PARAMETER);
2602 l = __search_service(tech_info->svc_list, svc_name, app_num);
2603 retv_if(NULL == l, UAM_ERROR_NOT_REGISTERED);
2605 __remove_service_to_dev_tech_mapping(tech_info, (uam_db_service_info_t *)l->data);
2607 mac = __get_mac_addr(tech_info);
2608 /* Remove service-device from DB */
2609 ret = _uam_db_delete_device_service_info(
2610 device_id, tech_type, mac, svc_name, app_num);
2611 if (UAM_ERROR_NONE != ret) {
2612 UAM_ERR("Device service removal from persistent DB failed");
2617 return UAM_ERROR_NONE;
2620 int _uam_core_service_set_device_discriminant(const char *svc_name,
2621 const char *device_id, int tech_type, gboolean discriminant, const int app_num)
2625 int ret = UAM_ERROR_NONE;
2627 uam_db_tech_info_t *tech_info;
2628 uam_db_service_info_t *service;
2630 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2631 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
2632 retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
2633 retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
2635 tech_info = __uam_core_get_dev_tech_info(device_id, tech_type);
2636 retv_if(NULL == tech_info, UAM_ERROR_INVALID_PARAMETER);
2638 /* Retrieve service from list */
2639 l = __search_service(services, svc_name, app_num);
2640 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
2643 mac = __get_mac_addr(tech_info);
2644 /* Insert device service info to db */
2645 ret = _uam_db_update_device_service_discriminant(device_id,
2646 tech_type, mac, service->name, discriminant, app_num);
2647 if (UAM_ERROR_NONE != ret) {
2648 UAM_WARN("Device service discriminant update to persistent DB failed");
2651 ret = _uam_core_update_svc_dev_info_discriminant(device_id,
2652 tech_type, service->name, discriminant, app_num);
2653 if (UAM_ERROR_NONE != ret) {
2654 UAM_WARN("Device service discriminant mapping update failed");
2659 return UAM_ERROR_NONE;
2662 int _uam_core_service_get_device_discriminant(const char *svc_name,
2663 const char *device_id, int tech_type, gboolean *discriminant, const int app_num)
2666 uam_svc_dev_info_t *svc_dev;
2668 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2669 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
2670 retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
2671 retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
2673 svc_dev = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name, app_num);
2674 retv_if(NULL == svc_dev, UAM_ERROR_INVALID_PARAMETER);
2675 *discriminant = (gboolean)svc_dev->discriminant;
2678 return UAM_ERROR_NONE;
2681 int _uam_core_service_get_device_last_seen(const char *svc_name,
2682 const char *device_id, int tech_type, unsigned long long *last_seen, const int app_num)
2685 uam_svc_dev_info_t *svc_dev;
2687 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2688 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
2689 retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
2690 retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
2692 svc_dev = _uam_core_find_svc_dev_info(device_id, tech_type, svc_name, app_num);
2693 retv_if(NULL == svc_dev, UAM_ERROR_INVALID_PARAMETER);
2694 *last_seen = svc_dev->last_seen;
2697 return UAM_ERROR_NONE;
2700 int _uam_core_set_service_detection_cycle(const char *svc_name, unsigned int new_cycle, const int app_num)
2704 uam_db_service_info_t *service;
2705 unsigned int elapsed_time;
2707 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2708 retv_if(UAM_DETECTION_CYCLE_MIN > new_cycle, UAM_ERROR_INVALID_PARAMETER);
2709 retv_if(0 != (new_cycle % UAM_DETECTION_CYCLE_MIN), UAM_ERROR_INVALID_PARAMETER);
2711 l = __search_service(services, svc_name, app_num);
2712 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
2715 elapsed_time = service->cycle - service->remaining_time;
2716 service->cycle = new_cycle;
2718 if (service->monitors) {
2719 UAM_DBG("Detection ongoing, Update service remaining time");
2720 if (new_cycle < elapsed_time)
2721 service->remaining_time = 0;
2723 service->remaining_time = new_cycle - elapsed_time;
2726 /* Update service detection cycle in DB */
2727 if (UAM_ERROR_NONE != _uam_db_update_service_cycle(svc_name, new_cycle, app_num))
2728 UAM_WARN("Service cycle updation to persistent DB failed");
2731 return UAM_ERROR_NONE;
2734 int _uam_core_get_service_detection_cycle(const char *svc_name, unsigned int *cycle, const int app_num)
2738 uam_db_service_info_t *service;
2740 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2741 retv_if(NULL == cycle, UAM_ERROR_INVALID_PARAMETER);
2743 l = __search_service(services, svc_name, app_num);
2744 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
2747 *cycle = service->cycle;
2750 return UAM_ERROR_NONE;
2753 int _uam_core_set_detection_threshold(unsigned int sensor,
2754 int presence_threshold, int absence_threshold)
2759 ret = _uam_pm_set_detection_threshold(sensor,
2760 presence_threshold, absence_threshold);
2761 if (UAM_ERROR_NONE != ret) {
2762 UAM_ERR("_uam_pm_set_detection_threshold failed with %s",
2763 _uam_manager_error_to_str(ret));
2768 return UAM_ERROR_NONE;
2771 static gboolean __start_detection(gpointer data)
2775 uam_db_service_info_t *service = NULL;
2776 unsigned int sensors;
2777 gboolean start_detection = FALSE;
2779 unsigned int env_sensors;
2780 unsigned int loc_sensors;
2782 env_sensors = _uam_core_get_active_env_sensors(UAM_DETECT_PRESENCE) |
2783 _uam_core_get_active_env_sensors(UAM_DETECT_ABSENCE);
2784 UAM_DBG("Active envionmental sensors: 0x%8.8X", env_sensors);
2786 for (l = services; NULL != l; l = g_slist_next(l)) {
2788 if (!service || !service->monitors)
2791 UAM_DBG("service: %s, monitors: %p", service->name, service->monitors);
2792 UAM_DBG("service->remaining_time: %d", service->remaining_time);
2793 service->remaining_time -= UAM_DETECTION_CYCLE_MIN;
2794 if (0 >= service->remaining_time) {
2795 start_detection = TRUE;
2796 service->remaining_time = service->cycle;
2800 if (!start_detection)
2803 /* Get sensors on which detection to be started */
2804 sensors = _uam_core_get_active_sensors(UAM_DETECT_PRESENCE) |
2805 _uam_core_get_active_sensors(UAM_DETECT_ABSENCE);
2806 UAM_DBG("Active sensors: 0x%8.8X", sensors);
2808 /* Remove env sensors from active sensors */
2809 sensors &= ~env_sensors;
2810 UAM_DBG("Active connectivity sensors: 0x%8.8X", sensors);
2813 /* Start detection */
2814 ret = _uam_pm_start_detection(UAM_DETECT_PRESENCE | UAM_DETECT_ABSENCE,
2816 if (UAM_ERROR_NONE != ret)
2817 UAM_ERR("Failed with error: %s (0x%4.4X)",
2818 _uam_manager_error_to_str(ret), ret);
2822 if (0 != env_sensors) {
2823 /* continuous monitoring
2824 * always start detection on active envionmental sensors */
2825 ret = _uam_pm_start_detection(UAM_DETECT_PRESENCE | UAM_DETECT_ABSENCE,
2827 if (UAM_ERROR_NONE != ret)
2828 UAM_ERR("Failed with error: %s (0x%4.4X)",
2829 _uam_manager_error_to_str(ret), ret);
2832 /* location detection */
2833 loc_sensors = _uam_core_get_active_sensors(UAM_DETECT_LOCATION);
2834 UAM_DBG("Active sensors for location detection: 0x%8.8X", loc_sensors);
2836 if (0 != loc_sensors) {
2837 /* Start location detection */
2838 ret = _uam_pm_start_detection(UAM_DETECT_LOCATION, loc_sensors);
2839 if (UAM_ERROR_NONE != ret)
2840 UAM_ERR("Failed with error: %s (0x%4.4X)",
2841 _uam_manager_error_to_str(ret), ret);
2848 static int __uam_core_start_detection(int detection_type,
2849 const char *svc_name, char *sender, unsigned int sensors, int app_num)
2852 uam_monitor_info_t *monitor;
2853 uam_db_service_info_t *service;
2854 gboolean is_monitor_added = TRUE;
2857 retv_if(NULL == sender, UAM_ERROR_INVALID_PARAMETER);
2858 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2860 l = __search_service(services, svc_name, app_num);
2862 UAM_ERR("Service not found");
2864 retv_if(NULL == l, UAM_ERROR_NOT_FOUND);
2865 retv_if(NULL == l->data, UAM_ERROR_INTERNAL);
2868 monitor = __uam_find_monitor(monitors, sender, svc_name, detection_type, app_num);
2870 monitor = g_malloc0(sizeof(uam_monitor_info_t));
2872 monitor->name = g_strdup(sender);
2873 monitor->mode = detection_type;
2874 monitor->service = service;
2875 is_monitor_added = FALSE;
2877 UAM_ERR("Memory allocation error");
2878 return UAM_ERROR_OUT_OF_MEMORY;
2882 UAM_DBG("Name: %s, Service: %s, Mode: %d, App_num: %d", monitor->name, svc_name,
2883 monitor->mode, app_num);
2885 monitor->sensors |= sensors;
2886 if (!is_monitor_added) {
2887 monitors = g_slist_append(monitors, monitor);
2888 service->monitors = g_slist_append(service->monitors, monitor);
2891 /* Start detection */
2892 if (0 == detection_timer) {
2893 __start_detection(NULL);
2894 UAM_INFO("Monitor started detection, start timer");
2895 detection_timer = g_timeout_add_seconds(
2896 UAM_DETECTION_CYCLE_MIN, __start_detection, NULL);
2900 return UAM_ERROR_NONE;
2903 static int __uam_core_stop_detection(int detection_type,
2904 const char *svc_name, char *sender, unsigned int sensors, int app_num)
2907 int ret = UAM_ERROR_NONE;
2908 unsigned int remaining_sensors;
2909 unsigned int active_sensors;
2910 unsigned int remaining_sensors_loc;
2911 unsigned int active_sensors_loc;
2912 uam_monitor_info_t *monitor;
2913 uam_db_service_info_t *service;
2915 retv_if(NULL == sender, UAM_ERROR_INVALID_PARAMETER);
2916 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
2918 monitor = __uam_find_monitor(monitors, sender, svc_name, detection_type, app_num);
2919 retv_if(NULL == monitor, UAM_ERROR_NOT_IN_OPERATION);
2920 service = monitor->service;
2921 retv_if(0 != g_strcmp0(service->name, svc_name), UAM_ERROR_NOT_IN_OPERATION);
2923 /* Find sensors which are already monitoring */
2924 active_sensors = _uam_core_get_active_sensors(UAM_DETECT_PRESENCE) |
2925 _uam_core_get_active_sensors(UAM_DETECT_ABSENCE);
2926 active_sensors_loc = _uam_core_get_active_sensors(UAM_DETECT_LOCATION);
2928 UAM_DBG("sensors: 0x%8.8X, Active sensors: 0x%8.8X " \
2929 "Active sensors location 0x%8.8X",
2930 sensors, active_sensors, active_sensors_loc);
2932 /* Update monitor info for application */
2933 monitor->sensors &= ~sensors;
2934 if (0 == monitor->sensors) {
2936 * App requested to stop monitoring for all of its active sensors,
2937 * remove its monitor info from list.
2939 monitors = g_slist_remove(monitors, monitor);
2940 if ((NULL == monitors) && (0 != detection_timer)) {
2941 UAM_INFO("All monitors stopped detection, stop timer");
2942 g_source_remove(detection_timer);
2943 detection_timer = 0;
2946 service->monitors = g_slist_remove(service->monitors, monitor);
2947 if (NULL == service->monitors)
2948 service->remaining_time = 0;
2950 g_free(monitor->name);
2954 /* Find sensors which are already monitoring */
2955 remaining_sensors = _uam_core_get_active_sensors(UAM_DETECT_PRESENCE) |
2956 _uam_core_get_active_sensors(UAM_DETECT_ABSENCE);
2957 remaining_sensors_loc = _uam_core_get_active_sensors(UAM_DETECT_LOCATION);
2959 UAM_DBG("Remaining sensors: 0x%8.8X Remaining sensors location: 0x%8.8X",
2960 remaining_sensors, remaining_sensors_loc);
2962 /* Stop location detection */
2963 if (detection_type == UAM_DETECT_LOCATION &&
2964 (active_sensors_loc != remaining_sensors_loc)) {
2966 ret = _uam_pm_stop_detection(UAM_DETECT_LOCATION,
2967 active_sensors_loc & ~remaining_sensors_loc);
2968 if (UAM_ERROR_NONE != ret)
2969 UAM_ERR("Failed with error: %s (0x%4.4X)",
2970 _uam_manager_error_to_str(ret), ret);
2974 if (active_sensors == remaining_sensors) {
2975 UAM_INFO("No need to stop monitoring");
2976 return UAM_ERROR_NONE;
2979 /* Stop monitoring only for sensors which aren't required anymore */
2980 sensors = (active_sensors & ~remaining_sensors);
2981 ret = _uam_pm_stop_detection(UAM_DETECT_PRESENCE | UAM_DETECT_ABSENCE, sensors);
2982 if (UAM_ERROR_NONE != ret)
2983 UAM_ERR("Failed with error: %s (0x%4.4X)",
2984 _uam_manager_error_to_str(ret), ret);
2990 int _uam_core_start_presence_detection(const char *svc_name, char *sender,
2991 unsigned int sensors, int app_num)
2996 ret = __uam_core_start_detection(UAM_DETECT_PRESENCE, svc_name,
2997 sender, sensors, app_num);
3003 int _uam_core_stop_presence_detection(const char *svc_name, char *sender,
3004 unsigned int sensors, int app_num)
3009 ret = __uam_core_stop_detection(UAM_DETECT_PRESENCE, svc_name,
3010 sender, sensors, app_num);
3016 int _uam_core_start_absence_detection(const char *svc_name, char *sender,
3017 unsigned int sensors, int app_num)
3022 ret = __uam_core_start_detection(UAM_DETECT_ABSENCE, svc_name, sender,
3029 int _uam_core_stop_absence_detection(const char *svc_name, char *sender,
3030 unsigned int sensors, int app_num)
3035 ret = __uam_core_stop_detection(UAM_DETECT_ABSENCE, svc_name, sender,
3042 int _uam_core_start_location_detection(const char *svc_name, char *sender,
3043 unsigned int sensors, int app_num)
3048 ret = __uam_core_start_detection(UAM_DETECT_LOCATION,
3049 svc_name, sender, sensors, app_num);
3055 int _uam_core_stop_location_detection(const char *svc_name, char *sender,
3056 unsigned int sensors, int app_num)
3061 ret = __uam_core_stop_detection(UAM_DETECT_LOCATION,
3062 svc_name, sender, sensors, app_num);
3068 int _uam_core_set_low_power_mode(unsigned int bitmask, gboolean mode)
3073 UAM_INFO("bitmask [%x] mode [%d]", bitmask, mode);
3074 ret = _uam_pm_set_low_power_mode(bitmask, mode);
3075 if (UAM_ERROR_NONE != ret) {
3076 UAM_ERR("_uam_pm_set_low_power_mode failed with [%x] %s",
3077 bitmask, _uam_manager_error_to_str(ret));
3082 return UAM_ERROR_NONE;
3085 int _uam_core_get_detection_window(unsigned int *window)
3089 retv_if(NULL == window, UAM_ERROR_INVALID_PARAMETER);
3091 *window = detection_window;
3094 return UAM_ERROR_NONE;
3097 int _uam_core_set_detection_window(unsigned int window)
3101 retv_if(0 >= window, UAM_ERROR_INVALID_PARAMETER);
3102 retv_if(UAM_DETECTION_WINDOW_MAX < window, UAM_ERROR_INVALID_PARAMETER);
3103 retv_if(0 != (window % UAM_DETECTION_WINDOW_STEP), UAM_ERROR_INVALID_PARAMETER);
3105 if (UAM_ERROR_NONE != _uam_pm_set_detection_window(window)) {
3106 UAM_ERR("_uam_pm_set_detection_window(%d) failed", window);
3107 return UAM_ERROR_INTERNAL;
3110 detection_window = window;
3113 return UAM_ERROR_NONE;
3116 int _uam_core_init(void)
3121 GSList *db_svc_list;
3122 GSList *db_adv_list;
3123 GSList *db_svc_dev_list;
3124 GSList *db_payload_list;
3125 GSList *db_sender_apps;
3129 _uam_db_initialize();
3131 /* Reset detecton window to default */
3132 detection_window = UAM_DETECTION_WINDOW_DEFAULT;
3133 if (UAM_ERROR_NONE != _uam_pm_set_detection_window(detection_window))
3134 UAM_ERR("_uam_pm_set_detection_window(%d) failed", detection_window);
3136 /* Fetch user list */
3137 db_users = _uam_db_get_all_users(-1);
3139 UAM_INFO_C("No users in database");
3141 for (l = db_users; NULL != l; l = g_slist_next(l)) {
3142 db_user_info_t *info = l->data;
3143 uam_db_user_info_t *user;
3148 user = g_new0(uam_db_user_info_t, 1);
3149 user->user_id = info->user_id;
3150 user->name = g_strdup(info->name);
3151 user->account = g_strdup(info->account);
3152 user->app_num = info->app_num;
3153 user->devices = NULL;
3155 users = g_slist_prepend(users, user);
3159 /* Fetch service list */
3160 db_svc_list = _uam_service_db_get_all_services(-1);
3162 UAM_INFO_C("No services in database");
3164 for (l = db_svc_list; NULL != l; l = g_slist_next(l)) {
3165 db_service_info_t *db_svc = l->data;
3166 uam_db_service_info_t *service;
3170 service = g_new0(uam_db_service_info_t, 1);
3171 __uam_copy_db_service_info(service, db_svc);
3172 services = g_slist_append(services, service);
3176 /* Fetch payload list */
3177 db_payload_list = _uam_db_get_all_payloads(-1);
3178 if (!db_payload_list) {
3179 UAM_INFO_C("No device payload in database");
3181 for (l = db_payload_list; NULL != l; l = g_slist_next(l)) {
3182 db_payload_info_t *db_payload = l->data;
3183 uam_db_payload_info_t *payload;
3189 l1 = g_slist_find_custom(payloads, &(db_payload->payload_info),
3192 payload = g_new0(uam_db_payload_info_t, 1);
3193 __uam_copy_uam_payload_info(payload, &(db_payload->payload_info));
3194 payload->device_id = g_strdup(db_payload->device_id);
3195 payload->tech_type = db_payload->type;
3196 payloads = g_slist_append(payloads, payload);
3201 /* Fetch svc dev list */
3202 db_svc_dev_list = _uam_db_get_service_devices_info(-1);
3203 if (!db_svc_dev_list) {
3204 UAM_INFO_C("No service devices in database");
3206 for (l = db_svc_dev_list; NULL != l; l = g_slist_next(l)) {
3207 db_svc_dev_info_t *db_svc = l->data;
3209 _uam_core_update_svc_dev_info(db_svc->device_id, db_svc->type,
3210 db_svc->svc, db_svc->discriminant, db_svc->last_seen,
3211 &(db_svc->payload_info), db_svc->app_num);
3214 g_slist_free_full(db_svc_dev_list, g_free);
3216 db_svc_dev_list = _uam_db_get_service_devices_payloads_info(-1);
3217 if (!db_svc_dev_list) {
3218 UAM_INFO_C("No service devices payloads in database");
3220 for (l = db_svc_dev_list; NULL != l; l = g_slist_next(l)) {
3221 db_svc_dev_info_t *db_svc = l->data;
3223 _uam_core_update_svc_dev_info(db_svc->device_id, db_svc->type,
3224 db_svc->svc, db_svc->discriminant, db_svc->last_seen,
3225 &(db_svc->payload_info), db_svc->app_num);
3229 /* Fetch device list */
3230 db_devices = _uam_device_db_get_all_devices(-1);
3232 UAM_INFO_C("No Devices registered in database");
3234 for (l = db_devices; NULL != l; l = g_slist_next(l)) {
3235 db_device_info_t *db_info = l->data;
3236 uam_db_user_info_t *user;
3237 GSList *svc_list = NULL;
3238 GSList *svc_dev_list = NULL;
3244 l1 = g_slist_find_custom(users,
3245 &(db_info->user_id), __compare_user_id);
3247 UAM_ERR("Invalid user Id: %d", db_info->user_id);
3251 UAM_DBG("db_info->app_num %d", db_info->app_num);
3252 UAM_DBG("dev_info.app_num %d", db_info->dev_info.app_num);
3253 db_info->dev_info.app_num = db_info->app_num;
3255 /* Fetch device services from DB */
3256 l1 = _uam_db_get_device_services(
3257 db_info->dev_info.device_id,
3258 db_info->dev_info.type,
3259 db_info->dev_info.mac,
3262 svc_list = __convert_db_svc_list_to_uam_svc_list(l1);
3264 svc_dev_list = _uam_core_find_svc_dev_list(&(db_info->dev_info));
3265 __uam_core_add_dev_to_list(user, &(db_info->dev_info),
3266 db_info->presence_state, db_info->last_seen,
3267 svc_list, svc_dev_list);
3271 /* Update user_service_mapping */
3272 __init_svc_user_mapping();
3274 /* Fetch iBeacon adv list */
3275 db_adv_list = _uam_db_get_all_advs(-1);
3277 UAM_INFO_C("No iBeacon adv in database");
3279 for (l = db_adv_list; NULL != l; l = g_slist_next(l)) {
3280 db_adv_info_t *db_adv = l->data;
3281 _uam_pm_add_ibeacon_adv(db_adv->adv_len, db_adv->iadv);
3285 /* Fetch sender_apps list */
3286 db_sender_apps = _uam_db_get_all_apps();
3287 if (!db_sender_apps) {
3288 UAM_INFO_C("No sender_apps in database");
3290 GSList **sender_apps = _uam_manager_get_sender_apps();
3291 uam_sender_app_t *sender_app = NULL;
3292 for (l = db_sender_apps; l; l = g_slist_next(l)) {
3293 db_app_info_t *db_sender_app = l->data;
3294 if (!db_sender_app) {
3295 UAM_WARN("db_sender_apps list has NULL data element");
3299 sender_app = g_malloc0(sizeof(uam_sender_app_t));
3301 sender_app->app_num = db_sender_app->app_num;
3302 sender_app->app_key = g_strdup(db_sender_app->app_key);
3303 *sender_apps = g_slist_prepend(*sender_apps, sender_app);
3305 UAM_ERR("Memory Allocation error");
3306 return UAM_ERROR_OUT_OF_MEMORY;
3312 g_slist_free_full(db_devices, g_free);
3313 g_slist_free_full(db_users, g_free);
3314 g_slist_free_full(db_svc_list, g_free);
3315 g_slist_free_full(db_adv_list, g_free);
3316 g_slist_free_full(db_svc_dev_list, g_free);
3317 g_slist_free_full(db_payload_list, g_free);
3318 g_slist_free_full(db_sender_apps, g_free);
3321 unsigned int bitmask;
3322 bitmask = _uam_core_get_user_sensors();
3324 /* Set/update registered device list to plugins */
3325 if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices, bitmask))
3326 UAM_ERR("_uam_pm_set_registered_devices failed");
3328 /* Set/update registered device list to cloud plugin */
3329 _uam_cloud_update_registered_devices();
3333 return UAM_ERROR_NONE;
3336 void _uam_core_deinit(void)
3341 /* de-init database */
3342 _uam_db_deinitialize();
3344 /* Reset detecton window to default */
3345 detection_window = UAM_DETECTION_WINDOW_DEFAULT;
3347 /* Release allocated memory for devices */
3348 g_slist_free_full(devices, __free_user_device);
3351 /* Release allocated memory for users */
3352 for (l = users; NULL != l; l = g_slist_next(l)) {
3353 uam_db_user_info_t *user = l->data;
3359 g_free(user->account);
3363 g_slist_free(users);
3366 /* Release allocated memory for services */
3367 for (l = services; NULL != l; l = g_slist_next(l)) {
3368 uam_db_service_info_t *service = l->data;
3372 __free_service_info(service);
3374 g_slist_free(services);
3377 /* Release allocated memory for service devices */
3378 for (l = svc_devs; NULL != l; l = g_slist_next(l)) {
3379 uam_svc_dev_info_t *svc_dev = l->data;
3384 g_free(svc_dev->device_id);
3385 g_free(svc_dev->service);
3386 g_free(svc_dev->payload);
3389 g_slist_free(svc_devs);
3392 /* Release allocated memory for service users */
3393 for (l = svc_users; NULL != l; l = g_slist_next(l)) {
3394 uam_svc_user_info_t *svc_user = l->data;
3399 g_free(svc_user->svc_name);
3400 g_free(svc_user->account);
3403 g_slist_free(svc_users);
3406 /* Release allocated memory for payloads */
3407 for (l = payloads; NULL != l; l = g_slist_next(l)) {
3408 uam_db_payload_info_t *payload = l->data;
3412 g_free(payload->device_uid);
3415 g_slist_free(payloads);
3418 /* Release allocated memory for sender_apps*/
3419 GSList *sender_apps = *(_uam_manager_get_sender_apps());
3420 for (l = sender_apps; l; l = g_slist_next(l)) {
3421 uam_sender_app_t *sender_app = l->data;
3425 g_free(sender_app->sender);
3426 g_free(sender_app->app_key);
3429 g_slist_free(sender_apps);
3434 void _uam_core_handle_sensor_ready(unsigned int sensor, gboolean is_ready)
3438 /* Send sensor state changed event over dbus */
3440 _uam_manager_send_event(NULL, UAM_EVENT_SENSOR_STATE_READY,
3441 g_variant_new("(iu)", UAM_ERROR_NONE, sensor));
3443 _uam_manager_send_event(NULL, UAM_EVENT_SENSOR_STATE_NOT_READY,
3444 g_variant_new("(iu)", UAM_ERROR_NONE, sensor));
3449 int _uam_core_handle_device_added(int status,
3450 int user_id, uam_device_info_s *dev_info)
3455 int ret = UAM_ERROR_NONE;
3456 GSList *svc_list = NULL;
3457 GSList *svc_dev_list = NULL;
3458 uam_db_user_info_t *user = NULL;
3459 uam_svc_dev_info_t *svc_dev = NULL;
3460 uam_db_service_info_t *service = NULL;
3461 gchar *default_service_name = NULL;
3464 UAM_INFO("user_id is [%d]", user_id);
3466 value = __uam_core_get_app_num_from_sec_list_devices(dev_info);
3467 UAM_DBG("The app_num is %d", value);
3469 dev_info->app_num = value;
3471 UAM_DBG("could not find app_num");
3473 __uam_core_remove_dev_from_sec_list_devices(dev_info);
3475 /* Send reply over dbus for add device API */
3476 l = _uam_manager_get_request_list();
3477 for (; NULL != l; l = g_slist_next(l)) {
3478 uam_request_context_t *info = l->data;
3479 uam_device_info_s *dev;
3482 if (!info || (UAM_REQUEST_ADD_DEVICE != info->function))
3487 UAM_WARN("info->data is NULL");
3491 if (dev->type != dev_info->type ||
3492 strcasecmp(dev->device_id, dev_info->device_id)) {
3493 UAM_WARN("[%d != %d] || [%s != %s]", dev->type, dev_info->type,
3494 dev->device_id, dev_info->device_id);
3497 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
3498 g_array_append_vals(out_param, dev_info, sizeof(uam_device_info_s));
3499 _uam_manager_method_return(info->context, out_param, status);
3501 _uam_remove_timer(info->tid);
3503 _uam_manager_remove_req_ctxt_from_list(info);
3508 if (UAM_ERROR_NONE != status) {
3509 __send_device_event(status, UAM_EVENT_DEVICE_ADDED, dev_info);
3514 gchar *default_user_account = NULL;
3515 __get_default_user(dev_info->app_num, &default_user_account, NULL);
3517 l = g_slist_find_custom(users,
3518 default_user_account, __compare_user_account);
3520 UAM_ERR("default user not found for account %s",
3521 default_user_account ? default_user_account : "");
3527 l = g_slist_find_custom(users, &user_id, __compare_user_id);
3528 if (NULL == l || NULL == l->data) {
3529 UAM_ERR("Invalid user Id: %d", user_id);
3530 ret = UAM_ERROR_NOT_FOUND;
3531 __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info);
3536 /* Get default service */
3537 __get_default_service_name(dev_info->app_num, &default_service_name);
3539 l = __search_service(services, default_service_name, dev_info->app_num);
3541 UAM_WARN("Default service not found");
3545 /** updates for svc dev*/
3546 ret = _uam_core_update_svc_dev_info(dev_info->device_id, dev_info->type,
3547 default_service_name, dev_info->discriminant,
3548 0, NULL, dev_info->app_num);
3549 if (UAM_ERROR_NONE != ret) {
3550 UAM_WARN("Device service mapping update failed");
3551 __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info);
3555 svc_list = g_slist_append(svc_list, service);
3556 svc_dev = _uam_core_find_svc_dev_info(dev_info->device_id,
3557 dev_info->type, default_service_name, dev_info->app_num);
3558 svc_dev_list = g_slist_append(svc_dev_list, svc_dev);
3559 __uam_core_add_dev_to_list(user, dev_info, UAM_PRESENCE_STATE_PRESENT,
3560 dev_info->last_seen, svc_list, svc_dev_list);
3562 /** Start database transaction */
3563 ret = __uam_db_begin_transaction();
3564 if (UAM_ERROR_NONE != ret) {
3565 UAM_WARN("__uam_db_begin_transaction failed");
3569 /* Add device to database */
3570 ret = _uam_device_db_insert_device_info(user->user_id, dev_info,
3571 UAM_PRESENCE_STATE_PRESENT, dev_info->last_seen, dev_info->app_num);
3572 if (UAM_ERROR_NONE != ret) {
3573 UAM_WARN("Device addition to persistent DB failed");
3574 __uam_db_end_transaction(0);
3578 /* Insert device service info to db */
3579 ret = _uam_db_insert_device_service_info(
3580 dev_info->device_id, dev_info->type, dev_info->mac,
3581 default_service_name, UAM_DETECTION_CYCLE_DEFAULT,
3582 dev_info->discriminant, 0, dev_info->app_num);
3583 if (UAM_ERROR_NONE != ret) {
3584 UAM_WARN("Device service addition to persistent DB failed");
3585 __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info);
3586 __uam_db_end_transaction(0);
3590 /* Add device to user mapped service */
3591 uam_svc_user_info_t* svc_user = NULL;
3593 l = g_slist_find_custom(svc_users, user, __compare_svc_user_account);
3595 UAM_WARN("User service mapping not found.");
3599 if (svc_user != NULL && svc_user->svc_name)
3600 _uam_core_service_add_device(svc_user->svc_name, dev_info->device_id,
3601 dev_info->type, dev_info->app_num);
3603 __uam_db_end_transaction(1);
3604 /** End database transaction */
3606 /* Send device added event to application */
3607 __send_device_event(ret, UAM_EVENT_DEVICE_ADDED, dev_info);
3609 /* Set/update registered device list to plugins */
3610 ret = _uam_pm_set_registered_devices(devices,
3611 __uam_core_tech_type_to_sensor(dev_info->type));
3612 if (UAM_ERROR_NONE != ret) {
3613 UAM_ERR("_uam_pm_set_registered_devices failed");
3617 _uam_cloud_send_device_added(status, user->account, dev_info);
3619 /* Set/update registered device list to cloud plugin */
3620 if (UAM_ERROR_NONE == status)
3621 _uam_cloud_update_registered_devices();
3623 free_n_null(&default_service_name);
3628 void __send_sensor_presence_event(uam_sensor_info_s *sensor_info, unsigned int sensor)
3632 unsigned long long timestamp;
3634 UAM_INFO("sensor 0x[%8.8X]", sensor);
3636 if (NULL == sensor_info) {
3637 _uam_manager_send_event(NULL, UAM_EVENT_PRESENCE_DETECTED,
3638 g_variant_new("(utiidddd)", sensor, 0, 0, 0, 0, 0, 0, 0));
3639 UAM_DBG("Sent UAM_EVENT_PRESENCE_DETECTED for 0x%8.8X", sensor);
3644 timestamp = _uam_get_timestamp();
3646 if (UAM_SENSOR_BITMASK_LIGHT != sensor) {
3647 _uam_manager_send_event(NULL, UAM_EVENT_PRESENCE_DETECTED,
3648 g_variant_new("(utiidddd)", sensor, timestamp,
3649 sensor_info->accuracy, sensor_info->count, sensor_info->values[0],
3650 sensor_info->values[1], sensor_info->values[2], sensor_info->values[3]));
3651 UAM_DBG("Sent UAM_EVENT_PRESENCE_DETECTED for 0x%8.8X", sensor);
3656 // service specific light detection threshold
3657 for (l = services; NULL != l; l = g_slist_next(l)) {
3658 uam_db_service_info_t *svc = l->data;
3661 if (!svc || !svc->monitors)
3664 UAM_INFO("service [%s] sensor value [%f] threshold [%u]",
3665 svc->name, sensor_info->values[0], svc->presence_threshold);
3667 if (sensor_info->values[0] < svc->presence_threshold)
3670 for (l1 = svc->monitors; NULL != l1; l1 = g_slist_next(l1)) {
3671 uam_monitor_info_t *mon = l1->data;
3676 UAM_INFO("monitor [%s] sensors 0x[%8.8X]",
3677 mon->name, mon->sensors);
3679 if (!(mon->sensors & sensor))
3682 if (UAM_DETECT_PRESENCE != mon->mode)
3685 _uam_manager_send_event(mon->name, UAM_EVENT_PRESENCE_DETECTED,
3686 g_variant_new("(utiidddd)", sensor, timestamp,
3687 sensor_info->accuracy, sensor_info->count, sensor_info->values[0],
3688 sensor_info->values[1], sensor_info->values[2], sensor_info->values[3]));
3689 UAM_DBG("Sent UAM_EVENT_PRESENCE_DETECTED to %s for 0x%8.8X",
3697 static void __send_sensor_absence_event(uam_sensor_info_s *sensor_info,
3698 unsigned int sensor)
3703 UAM_INFO("sensor 0x[%8.8X]", sensor);
3705 if (NULL == sensor_info) {
3706 _uam_manager_send_event(NULL, UAM_EVENT_ABSENCE_DETECTED,
3707 g_variant_new("(utiidddd)", sensor, 0, 0, 0, 0, 0, 0, 0));
3708 UAM_DBG("Sent UAM_EVENT_ABSENCE_DETECTED for 0x%8.8X", sensor);
3713 if (UAM_SENSOR_BITMASK_LIGHT != sensor) {
3714 _uam_manager_send_event(NULL, UAM_EVENT_ABSENCE_DETECTED,
3715 g_variant_new("(utiidddd)", sensor, sensor_info->timestamp,
3716 sensor_info->accuracy, sensor_info->count, sensor_info->values[0],
3717 sensor_info->values[1], sensor_info->values[2], sensor_info->values[3]));
3718 UAM_DBG("Sent UAM_EVENT_ABSENCE_DETECTED for 0x%8.8X", sensor);
3723 // service specific light detection threshold
3724 for (l = services; NULL != l; l = g_slist_next(l)) {
3725 uam_db_service_info_t *svc = l->data;
3728 if (!svc || !svc->monitors)
3731 UAM_INFO("service [%s] sensor value [%f] threshold [%u]",
3732 svc->name, sensor_info->values[0], svc->absence_threshold);
3734 if (sensor_info->values[0] > svc->absence_threshold)
3737 for (l1 = svc->monitors; NULL != l1; l1 = g_slist_next(l1)) {
3738 uam_monitor_info_t *mon = l1->data;
3743 if (!(mon->sensors & sensor))
3746 if (UAM_DETECT_PRESENCE != mon->mode)
3749 _uam_manager_send_event(mon->name, UAM_EVENT_ABSENCE_DETECTED,
3750 g_variant_new("(utiidddd)", sensor, sensor_info->timestamp,
3751 sensor_info->accuracy, sensor_info->count, sensor_info->values[0],
3752 sensor_info->values[1], sensor_info->values[2], sensor_info->values[3]));
3753 UAM_DBG("Sent UAM_EVENT_ABSENCE_DETECTED for 0x%8.8X", sensor);
3760 void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor,
3761 uam_device_info_s *dev_info, uam_ble_payload_s *payload)
3766 uam_db_user_info_t *user;
3767 uam_svc_dev_info_t *svc_dev = NULL;
3768 gboolean live_monitoring = FALSE;
3769 unsigned long long timestamp;
3772 ret_if(NULL == tech);
3773 ret_if(NULL == tech->svc_list);
3774 ret_if(NULL == dev_info);
3776 user = tech->device->user;
3777 user->last_seen = tech->last_seen;
3779 for (l = tech->svc_list; NULL != l; l = g_slist_next(l)) {
3780 uam_db_service_info_t *svc = l->data;
3782 live_monitoring = FALSE;
3784 if (!svc || !svc->monitors)
3787 UAM_INFO("service [%s] remaining time [%d] cycle [%d]",
3788 svc->name, svc->remaining_time, svc->cycle);
3790 if (!(svc->remaining_time == svc->cycle))
3793 UAM_DBG("Check service device discriminant");
3794 svc_dev = _uam_core_find_svc_dev_info(dev_info->device_id,
3795 dev_info->type, svc->name, dev_info->app_num);
3796 if (!svc_dev || !svc_dev->discriminant)
3799 /** Compare device payload*/
3800 /** If payload is NULL, send event to all services */
3802 UAM_DBG("Compare payload - primary_key detected: [%d], svc_dev: [%d] ",
3803 payload->primary_key, svc_dev->payload->primary_key);
3805 if (0 != __compare_payload(svc_dev->payload, payload))
3808 UAM_DBG("Detected payload is NULL");
3811 UAM_DBG("Send event");
3812 for (l1 = svc->monitors; NULL != l1; l1 = g_slist_next(l1)) {
3813 uam_monitor_info_t *mon = l1->data;
3818 if (!(mon->sensors & sensor))
3821 if (UAM_DETECT_PRESENCE != mon->mode)
3825 * For current service, if any of its running monitor has same sensor
3826 * and detection_mode
3828 live_monitoring = TRUE;
3830 timestamp = _uam_get_timestamp();
3831 UAM_INFO("sensor [%d]", sensor);
3832 _uam_manager_send_event(mon->name,
3833 UAM_EVENT_USER_PRESENCE_DETECTED,
3834 g_variant_new("(utsss)", sensor, timestamp,
3835 user->account, svc->name, dev_info->device_id));
3836 UAM_DBG("Sent UAM_EVENT_USER_PRESENCE_DETECTED to %s"
3838 " for 0x%8.8X, User: %s Service: %s Timestamp: %llu",
3839 mon->name, dev_info->device_id,
3840 sensor, user->account,
3845 /* Update service specific device last_seen in svc_dev list and DB */
3846 if (live_monitoring) {
3847 svc_dev->last_seen = tech->last_seen;
3848 ret = _uam_db_update_device_service_last_seen(dev_info->device_id,
3849 dev_info->type, dev_info->mac, svc->name, tech->last_seen, dev_info->app_num);
3850 if (UAM_ERROR_NONE != ret)
3851 UAM_WARN("_uam_db_update_device_service_last_seen failed");
3858 static int __uam_core_update_device_addr(uam_db_address_info_t *addr,
3859 uam_device_info_s *dev_info)
3862 int ret = UAM_ERROR_NONE;
3864 retv_if(!addr, UAM_ERROR_INVALID_PARAMETER);
3865 retv_if(!dev_info, UAM_ERROR_INVALID_PARAMETER);
3867 switch (addr->addr_type) {
3868 case UAM_ADDR_TYPE_IPv4:
3869 if (strcasecmp(addr->address, dev_info->ipv4_addr)) {
3870 UAM_DBG("Old IPv4: %s, New IPv4: %s",
3871 addr->address, dev_info->ipv4_addr);
3873 g_free(addr->address);
3874 addr->address = g_strdup(dev_info->ipv4_addr);
3876 /* Update IP address in DB */
3877 ret = _uam_device_db_update_device_ip_address(dev_info->device_id,
3878 dev_info->type, dev_info->ipv4_addr, dev_info->app_num);
3879 if (UAM_ERROR_NONE != ret) {
3880 UAM_WARN("_uam_device_db_update_device_ip_address failed");
3885 case UAM_ADDR_TYPE_WIFI:
3886 if (strcasecmp(addr->address, dev_info->mac)) {
3887 UAM_DBG("Old MAC: %s, New MAC: %s",
3888 addr->address, dev_info->mac);
3890 g_free(addr->address);
3891 addr->address = g_strdup(dev_info->mac);
3893 /* Update address in DB */
3894 ret = _uam_device_db_update_device_mac_address(dev_info->device_id,
3895 dev_info->type, dev_info->mac, dev_info->app_num);
3896 if (UAM_ERROR_NONE != ret) {
3897 UAM_WARN("_uam_device_db_update_device_mac_address failed");
3898 __uam_db_end_transaction(0);
3904 UAM_DBG("Unknown addr type: %d", addr->addr_type);
3911 int _uam_core_handle_presence_detected(unsigned int sensor,
3912 int user_id, void *info, uam_ble_payload_s *payload)
3915 int ret = UAM_ERROR_NONE;
3918 uam_db_user_info_t *user;
3919 uam_db_device_info_t *device;
3920 uam_db_tech_info_t *tech;
3922 uam_device_info_s *dev_info = NULL;
3923 uam_sensor_info_s *sensor_info = NULL;
3925 UAM_INFO("sensor: 0x%8.8X, user_id: %d", sensor, user_id);
3927 if (info && (sensor & _uam_core_get_env_sensors())) {
3928 /* Received info is sensor information */
3932 if (info && (sensor & _uam_core_get_user_sensors())) {
3933 /* Received info is device information */
3937 _uam_vpm_send_presence_detection_event(sensor);
3939 if (NULL == dev_info) {
3940 __send_sensor_presence_event(sensor_info, sensor);
3944 retv_if(0 > user_id, UAM_ERROR_INVALID_PARAMETER);
3946 /* fetch the app_num info for the corresponding user_id */
3947 _uam_core_get_app_num_from_userid(user_id, &dev_info->app_num);
3949 l = g_slist_find_custom(users, &user_id, __compare_user_id);
3951 UAM_ERR("Invalid user_id [%d]", user_id);
3952 return UAM_ERROR_INVALID_PARAMETER;
3956 l = g_slist_find_custom(user->devices,
3957 dev_info->device_id, __compare_device_id);
3959 UAM_ERR("Valid user_id [%d] but Invalid device_id [%s]",
3960 user_id, dev_info->device_id);
3961 return UAM_ERROR_INVALID_PARAMETER;
3965 if (!(device->supported_techs & dev_info->type)) {
3966 UAM_ERR("Valid device_id [%s] but Invalid tech type [%d]",
3967 dev_info->device_id, dev_info->type);
3968 return UAM_ERROR_INVALID_PARAMETER;
3971 l = g_slist_find_custom(device->tech_list,
3972 &(dev_info->type), __compare_tech_type);
3974 UAM_ERR("Strange, tech type [%d] not found", dev_info->type);
3975 return UAM_ERROR_INVALID_PARAMETER;
3979 tech->presence_state = UAM_PRESENCE_STATE_PRESENT;
3980 tech->last_seen = dev_info->last_seen;
3982 /** Update payload info in list with user data */
3984 l = g_slist_find_custom(payloads, payload, __compare_payload);
3986 __uam_copy_uam_payload_info(l->data, payload);
3989 retv_if(UAM_ERROR_NONE != __uam_db_begin_transaction(), UAM_ERROR_INVALID_PARAMETER);
3991 /* Check if IP/MAC address was updated then update in DB */
3992 if (UAM_TECH_TYPE_WIFI == dev_info->type) {
3993 for (l = tech->addresses; NULL != l; l = g_slist_next(l)) {
3994 uam_db_address_info_t *addr = l->data;
3999 ret = __uam_core_update_device_addr(addr, dev_info);
4000 if (UAM_ERROR_NONE != ret) {
4001 UAM_WARN("_uam_device_db_update_device_ip_address failed");
4002 __uam_db_end_transaction(0);
4008 /* Update database (presence state & timestamp) */
4009 ret = _uam_device_db_update_device_last_seen(dev_info->device_id,
4010 dev_info->type, dev_info->mac, dev_info->last_seen, dev_info->app_num);
4011 if (UAM_ERROR_NONE != ret) {
4012 UAM_WARN("_uam_device_db_update_device_last_seen failed");
4013 __uam_db_end_transaction(0);
4017 ret = _uam_device_db_update_device_presence(dev_info->device_id,
4018 dev_info->type, dev_info->mac, tech->presence_state, dev_info->app_num);
4019 if (UAM_ERROR_NONE != ret) {
4020 UAM_WARN("_uam_device_db_update_device_presence failed");
4021 __uam_db_end_transaction(0);
4025 /* Update database (payload user data) */
4027 ret = _uam_db_update_payload_info(dev_info->device_id,
4028 dev_info->type, dev_info->mac, payload, dev_info->app_num);
4029 if (UAM_ERROR_NONE != ret) {
4030 UAM_WARN("_uam_db_update_payload_user_data failed");
4031 __uam_db_end_transaction(0);
4036 __uam_db_end_transaction(1);
4038 /* Send user presence event and update service_device timestamp */
4039 __send_user_presence_event(tech, sensor, dev_info, payload);
4045 void __send_user_location_event(uam_db_tech_info_t *tech,
4046 unsigned int sensor, uam_device_info_s *dev_info)
4051 uam_db_user_info_t *user;
4052 uam_svc_dev_info_t *svc_dev = NULL;
4053 unsigned long long timestamp;
4055 ret_if(NULL == tech);
4056 ret_if(NULL == tech->svc_list);
4057 ret_if(NULL == dev_info);
4059 user = tech->device->user;
4061 for (l = tech->svc_list; NULL != l; l = g_slist_next(l)) {
4062 uam_db_service_info_t *svc = l->data;
4065 if (!svc || !svc->monitors)
4068 UAM_DBG("Check service device discriminant");
4069 svc_dev = _uam_core_find_svc_dev_info(dev_info->device_id,
4070 dev_info->type, svc->name, dev_info->app_num);
4071 if (!svc_dev || !svc_dev->discriminant)
4074 UAM_DBG("Send event");
4075 for (l1 = svc->monitors; NULL != l1; l1 = g_slist_next(l1)) {
4076 uam_monitor_info_t *mon = l1->data;
4081 if (!(mon->sensors & sensor))
4084 if (UAM_DETECT_LOCATION != mon->mode)
4088 * For current service, if any of its running monitor has same sensor
4089 * and detection_mode
4092 timestamp = _uam_get_timestamp();
4093 UAM_INFO("sensor [%d]", sensor);
4094 _uam_manager_send_event(mon->name,
4095 UAM_EVENT_USER_LOCATION_DETECTED,
4096 g_variant_new("(utsssiiii)", sensor, timestamp,
4097 user->account, svc->name, dev_info->device_id,
4098 dev_info->distance_mm, dev_info->x, dev_info->y,
4100 UAM_DBG("Sent UAM_EVENT_USER_LOCATION_DETECTED to %s"
4101 " on device %s for 0x%8.8X, User: %s"
4102 " Service: %s Timestamp: %llu distance: %d"
4103 " coordinates x:%d y:%d z:%d",
4104 mon->name, dev_info->device_id,
4105 sensor, user->account, svc->name,
4106 timestamp, dev_info->distance_mm,
4107 dev_info->x, dev_info->y, dev_info->z);
4114 int _uam_core_handle_location_detected(unsigned int sensor,
4115 int user_id, void *info)
4118 int ret = UAM_ERROR_NONE;
4121 uam_db_user_info_t *user;
4122 uam_db_device_info_t *device;
4123 uam_db_tech_info_t *tech;
4124 uam_device_info_s *dev_info = NULL;
4126 UAM_INFO("sensor: 0x%8.8X, user_id: %d", sensor, user_id);
4128 if (info && (sensor & _uam_core_get_user_sensors())) {
4129 /* Received info is device information */
4133 if (NULL == dev_info) {
4134 /* To-Do: Handle location detection if no dev info available */
4138 retv_if(0 > user_id, UAM_ERROR_INVALID_PARAMETER);
4140 /* fetch the app_num info for the corresponding user_id */
4141 _uam_core_get_app_num_from_userid(user_id, &dev_info->app_num);
4144 l = g_slist_find_custom(users, &user_id, __compare_user_id);
4146 UAM_ERR("Invalid user_id [%d]", user_id);
4147 return UAM_ERROR_INVALID_PARAMETER;
4152 l = g_slist_find_custom(user->devices,
4153 dev_info->device_id, __compare_device_id);
4155 UAM_ERR("Valid user_id [%d] but Invalid device_id [%s]",
4156 user_id, dev_info->device_id);
4157 return UAM_ERROR_INVALID_PARAMETER;
4161 if (!(device->supported_techs & dev_info->type)) {
4162 UAM_ERR("Valid device_id [%s] but Invalid tech type [%d]",
4163 dev_info->device_id, dev_info->type);
4164 return UAM_ERROR_INVALID_PARAMETER;
4167 l = g_slist_find_custom(device->tech_list,
4168 &(dev_info->type), __compare_tech_type);
4170 UAM_ERR("Strange, tech type [%d] not found", dev_info->type);
4171 return UAM_ERROR_INVALID_PARAMETER;
4175 /* Send user location event */
4176 __send_user_location_event(tech, sensor, dev_info);
4182 static void __send_user_absence_event(uam_tech_type_e type, unsigned int sensor)
4188 * For each service, find users absent on given sensor. Then for each
4189 * monitor in serivce's monitor list, if it is monitoring ABSENCE on
4190 * given sensor, send user ABSENCE event.
4192 for (l = services; NULL != l; l = g_slist_next(l)) {
4193 uam_db_service_info_t *svc = l->data;
4194 GSList *absent_users = NULL;
4195 GSList *present_users = NULL;
4199 if (!svc || !svc->monitors || !svc->dev_techs)
4202 UAM_INFO("service [%s] remaining time [%d] cycle [%d]",
4203 svc->name, svc->remaining_time, svc->cycle);
4205 if (!(svc->remaining_time == svc->cycle))
4208 for (l1 = svc->dev_techs; NULL != l1; l1 = g_slist_next(l1)) {
4209 uam_db_tech_info_t *tech = l1->data;
4211 if (!tech || (tech->tech_type != type)) {
4212 UAM_WARN("tech is NULL or tech->tech_type != type [%d]", type);
4216 if (!tech->device || !tech->device->user) {
4217 UAM_WARN("tech->device is NULL or tech->device->user is NULL");
4221 l2 = g_slist_find_custom(present_users,
4222 &(tech->device->user->user_id), __compare_user_id);
4224 if (UAM_PRESENCE_STATE_PRESENT == tech->presence_state && tech->discriminant) {
4225 UAM_DBG("tech->discriminant [%d] device_id [%s] account [%s]",
4227 tech->device->device_id,
4228 tech->device->user->account);
4229 /* Remove user from absent list */
4230 absent_users = g_slist_remove(absent_users, tech->device->user);
4232 /* If user not in present list, add user to the list */
4234 UAM_DBG("added present user [%s]", tech->device->user->account);
4235 present_users = g_slist_prepend(present_users, tech->device->user);
4238 /* If user not in the present list then only add it to absent list */
4239 if ((NULL == l2) && (NULL == g_slist_find_custom(
4240 absent_users, &(tech->device->user->user_id),
4241 __compare_user_id))) {
4242 UAM_DBG("added absent user [%s]", tech->device->user->account);
4243 absent_users = g_slist_prepend(absent_users, tech->device->user);
4248 g_slist_free(present_users);
4252 for (l2 = svc->monitors; NULL != l2; l2 = g_slist_next(l2)) {
4253 uam_monitor_info_t *mon = l2->data;
4258 if (!(mon->sensors & sensor))
4261 if (UAM_DETECT_ABSENCE != mon->mode)
4264 for (l1 = absent_users; NULL != l1; l1 = g_slist_next(l1)) {
4265 uam_db_user_info_t *user = l1->data;
4270 user->last_seen = 0;
4272 _uam_manager_send_event(mon->name,
4273 UAM_EVENT_USER_ABSENCE_DETECTED,
4274 g_variant_new("(utss)", sensor, user->last_seen,
4275 user->account, svc->name));
4276 UAM_DBG("Sent UAM_EVENT_USER_ABSENCE_DETECTED to %s"
4277 " for 0x%8.8X, User: %s Service: %s",
4278 mon->name, sensor, user->account, svc->name);
4282 g_slist_free(absent_users);
4288 void _uam_core_handle_absence_detected(unsigned int sensor,
4289 int user_id, void *info)
4292 uam_db_user_info_t *user;
4293 uam_db_device_info_t *device;
4294 uam_db_tech_info_t *tech;
4296 uam_device_info_s *dev_info = NULL;
4297 uam_sensor_info_s *sensor_info = NULL;
4299 UAM_INFO("sensor: 0x%8.8X, user_id: %d", sensor, user_id);
4301 if (info && (sensor & _uam_core_get_env_sensors())) {
4302 /* received info is sensor information */
4306 if (info && (sensor & _uam_core_get_user_sensors())) {
4307 /* received info is device information */
4311 if (NULL == dev_info) {
4312 __send_sensor_absence_event(sensor_info, sensor);
4317 ret_if(0 > user_id);
4319 l = g_slist_find_custom(users, &user_id, __compare_user_id);
4321 UAM_ERR("Invalid user_id [%d]", user_id);
4326 l = g_slist_find_custom(user->devices,
4327 dev_info->device_id, __compare_device_id);
4329 UAM_ERR("Valid user_id [%d] but Invalid device_id [%s]",
4330 user_id, dev_info->device_id);
4334 if (!(device->supported_techs & dev_info->type)) {
4335 UAM_ERR("Valid device_id [%s] but Invalid tech type [%d]",
4336 dev_info->device_id, dev_info->type);
4340 l = g_slist_find_custom(device->tech_list,
4341 &(dev_info->type), __compare_tech_type);
4343 UAM_ERR("Strange, tech type [%d] not found", dev_info->type);
4348 tech->presence_state = UAM_PRESENCE_STATE_ABSENT;
4350 /* Update database (presence state) */
4351 if (UAM_ERROR_NONE != _uam_device_db_update_device_presence(dev_info->device_id,
4352 dev_info->type, dev_info->mac, tech->presence_state, dev_info->app_num))
4353 UAM_WARN("_uam_device_db_update_device_presence failed");
4358 void _uam_core_cleanup_monitor(char *name)
4362 for (l = monitors; NULL != l; l = g_slist_next(l)) {
4363 uam_monitor_info_t *monitor = l->data;
4365 if (!monitor || !monitor->name || !monitor->service)
4368 if (0 != g_strcmp0(name, monitor->name))
4371 /* If there is a monitor that is not freed, stop detection
4372 * and free the monitor structure in the memory. */
4373 UAM_INFO("clear %s's monitor info.", monitor->name);
4374 __uam_core_stop_detection(monitor->mode,
4375 monitor->service->name, name, monitor->sensors, monitor->service->app_num);
4379 void _uam_core_reset_database(void)
4383 unsigned int bitmask;
4385 ret = _uam_db_clear();
4386 if (UAM_ERROR_NONE != ret) {
4387 UAM_ERR("_uam_db_clear failed with %s",
4388 _uam_manager_error_to_str(ret));
4389 unlink(DATABASE_FULL_PATH);
4393 g_slist_free_full(devices, __free_user_device);
4396 g_slist_free_full(users, __free_user_info);
4399 g_slist_free_full(services, __free_service_info);
4402 g_slist_free_full(monitors, __free_monitor_info);
4405 g_slist_free_full(scanners, __free_scanner_info);
4408 bitmask = _uam_core_get_user_sensors();
4409 /* Set/update registered device list to plugins */
4410 ret = _uam_pm_set_registered_devices(devices, bitmask);
4411 if (UAM_ERROR_NONE != ret)
4412 UAM_ERR("_uam_pm_set_registered_devices failed with %s",
4413 _uam_manager_error_to_str(ret));
4415 /* Set/update registered device list to cloud plugin */
4416 _uam_cloud_update_registered_devices();
4421 void _uam_core_handle_detection_started(unsigned int sensor)
4424 unsigned int active_sensors = 0;
4426 UAM_DBG("Sensor: 0x%8.8X, detecting_sensors: 0x%8.8X",
4427 sensor, detecting_sensors);
4429 detecting_sensors |= sensor;
4430 active_sensors |= _uam_core_get_active_sensors(UAM_DETECT_PRESENCE);
4431 active_sensors |= _uam_core_get_active_sensors(UAM_DETECT_ABSENCE);
4433 if (active_sensors == detecting_sensors) {
4434 /* Send detection started event */
4435 _uam_manager_send_event(NULL, UAM_EVENT_DETECTION_STARTED, NULL);
4441 void _uam_core_handle_detection_stopped(unsigned int sensor)
4445 uam_tech_type_e type = UAM_TECH_TYPE_NONE;
4446 uam_cycle_state_e cycle_state;
4449 ret_if((detecting_sensors & sensor) == 0);
4450 UAM_DBG("Sensor: 0x%8.8X, detecting_sensors: 0x%8.8X",
4451 sensor, detecting_sensors);
4452 detecting_sensors &= ~sensor;
4454 type = __uam_core_sensor_to_tech_type(sensor);
4456 if (UAM_TECH_TYPE_NONE != type)
4457 __send_user_absence_event(type, sensor);
4459 if (0 == detecting_sensors) {
4460 /* Send detection stopped event */
4461 for (l = monitors; l; l = g_slist_next(l)) {
4462 uam_monitor_info_t *mon = l->data;
4464 if (!mon || !mon->name || !mon->service)
4466 uam_db_service_info_t *service = mon->service;
4467 cycle_state = UAM_DETECTION_CYCLE_END;
4469 UAM_DBG("mon->sensors[0x%X], [%s]->remaining_time: %d",
4470 mon->sensors, service->name, service->remaining_time);
4471 if (!(mon->sensors & _uam_core_get_env_sensors())) {
4472 if (service->remaining_time < service->cycle) {
4473 UAM_DBG("service->remaining_time < service->cycle, return");
4476 } else if (service->remaining_time > UAM_DETECTION_CYCLE_MIN) {
4477 cycle_state = UAM_DETECTION_CYCLE_MID;
4480 _uam_manager_send_event(mon->name, UAM_EVENT_DETECTION_STOPPED,
4481 g_variant_new("(si)", service->name, cycle_state));
4482 UAM_DBG("Sent UAM_EVENT_DETECTION_STOPPED to %s, Service: %s,"
4483 " cycle state:[%s]",
4484 mon->name, service->name, cycle_state ? "end" : "mid");
4490 static uam_scanner_info_t *__uam_find_scanner(const char *name)
4494 retv_if(NULL == name, NULL);
4496 for (l = scanners; NULL != l; l = g_slist_next(l)) {
4497 uam_scanner_info_t *scanner = l->data;
4499 if (!scanner || !scanner->name)
4502 if (0 == g_strcmp0(scanner->name, name)) {
4503 UAM_DBG("Scanning application found in list");
4511 static gboolean __scan_completed_cb(gpointer data)
4514 uam_scanner_info_t *scanner = data;
4516 retv_if(NULL == scanner, FALSE);
4518 if (UAM_ERROR_NONE != _uam_manager_send_event(
4519 scanner->name, UAM_EVENT_SCAN_COMPLETED, NULL))
4520 UAM_ERR("Failed to send UAM_EVENT_SCAN_COMPLETED");
4522 UAM_INFO_C("Sent UAM_EVENT_SCAN_COMPLETED to [%s]", scanner->name);
4525 scanners = g_slist_remove(scanners, scanner);
4526 g_free(scanner->name);
4533 int _uam_core_start_active_device_scan(char *sender, unsigned int sensors, int detection_period)
4537 uam_scanner_info_t *scanner;
4539 retv_if(NULL == sender, UAM_ERROR_INVALID_PARAMETER);
4540 retv_if(0 == sensors, UAM_ERROR_INVALID_PARAMETER);
4542 scanner = __uam_find_scanner(sender);
4543 retv_if(NULL != scanner, UAM_ERROR_NOW_IN_PROGRESS);
4545 ret = _uam_pm_start_active_device_scan(&sensors, detection_period);
4546 if (UAM_ERROR_NONE != ret) {
4547 UAM_ERR("Failed with error: %s (0x%4.4X)",
4548 _uam_manager_error_to_str(ret), ret);
4552 scanner = g_malloc0(sizeof(uam_scanner_info_t));
4554 UAM_ERR("Failed to allocate memory");
4555 return UAM_ERROR_OUT_OF_MEMORY;
4557 scanner->name = g_strdup(sender);
4558 scanner->sensors |= sensors;
4559 scanner->timer = g_timeout_add_seconds(detection_period,
4560 __scan_completed_cb, scanner);
4561 scanners = g_slist_append(scanners, scanner);
4562 UAM_DBG("sensors = 0x%8.8X - 0x%8.8X", scanner->sensors, sensors);
4565 return UAM_ERROR_NONE;
4568 int _uam_core_stop_active_device_scan(char *sender, unsigned int sensors)
4572 uam_scanner_info_t *scanner;
4575 retv_if(NULL == sender, UAM_ERROR_INVALID_PARAMETER);
4576 retv_if(0 == sensors, UAM_ERROR_INVALID_PARAMETER);
4578 scanner = __uam_find_scanner(sender);
4579 retv_if(NULL == scanner, UAM_ERROR_NOT_IN_OPERATION);
4581 /* Trim sensors to a subset of active sensors for the scanner */
4582 sensors &= scanner->sensors;
4585 * modify scanner's active sensors and if active sensors are NULL
4586 * remove scanner from scanners list
4588 scanner->sensors &= ~sensors;
4589 if (0 == scanner->sensors) {
4590 scanners = g_slist_remove(scanners, scanner);
4591 g_source_remove(scanner->timer);
4592 g_free(scanner->name);
4596 for (l = scanners; NULL != l; l = g_slist_next(l)) {
4597 uam_scanner_info_t *scanner_data = l->data;
4599 if (!scanner_data || !scanner_data->name)
4602 sensors &= ~(scanner_data->sensors);
4606 ret = _uam_pm_stop_active_device_scan(sensors);
4607 if (UAM_ERROR_NONE != ret) {
4608 UAM_ERR("Failed with error: %s (0x%4.4X)",
4609 _uam_manager_error_to_str(ret), ret);
4615 return UAM_ERROR_NONE;
4618 void _uam_core_handle_active_device(uam_active_scan_event_e event,
4619 unsigned int sensor, const uam_device_info_s *dev_info)
4624 ret_if((UAM_ACTIVE_SCAN_COMPLETED != event) && (NULL == dev_info));
4626 for (l = scanners; NULL != l;) {
4627 uam_scanner_info_t *scanner = l->data;
4629 if (!scanner || !scanner->name) {
4630 l = g_slist_next(l);
4634 if (0 == (scanner->sensors & sensor)) {
4635 l = g_slist_next(l);
4639 if (event == UAM_ACTIVE_SCAN_COMPLETED) {
4640 scanner->sensors &= ~(sensor);
4641 UAM_DBG("sensors = 0x%8.8X", scanner->sensors);
4642 if (0 != scanner->sensors) {
4643 l = g_slist_next(l);
4647 if (UAM_ERROR_NONE != _uam_manager_send_event(
4648 scanner->name, UAM_EVENT_SCAN_COMPLETED, NULL))
4649 UAM_ERR("Failed to send UAM_EVENT_SCAN_COMPLETED");
4651 UAM_INFO_C("Sent UAM_EVENT_SCAN_COMPLETED to [%s]", scanner->name);
4654 l = g_slist_next(l);
4655 scanners = g_slist_remove(scanners, scanner);
4656 g_source_remove(scanner->timer);
4657 g_free(scanner->name);
4660 GVariant *param = g_variant_new("(iiisss)",
4662 dev_info->operating_system,
4665 dev_info->ipv4_addr,
4666 dev_info->device_id);
4667 if (UAM_ERROR_NONE != _uam_manager_send_event(
4668 scanner->name, UAM_EVENT_DEVICE_FOUND, param))
4669 UAM_ERR("Failed to send %s", _uam_manager_event_to_str(event));
4671 UAM_INFO_C("Sent UAM_EVENT_DEVICE_FOUND to [%s]", scanner->name);
4673 l = g_slist_next(l);
4680 int _uam_core_register_service(uam_service_info_s *svc, const int app_num)
4684 uam_db_service_info_t *service;
4685 int service_number = 0;
4687 retv_if(NULL == svc->name, UAM_ERROR_INVALID_PARAMETER);
4689 /* Retrieve service from list */
4690 UAM_INFO("Registering for : service name->[%s] and app_num->[%d]",
4691 svc->name, app_num);
4693 l = __search_service(services, svc->name, app_num);
4694 retv_if((NULL != l) && (l->data != NULL), UAM_ERROR_ALREADY_REGISTERED);
4696 service = g_new0(uam_db_service_info_t, 1);
4697 __uam_copy_uam_service_info(service, svc);
4698 service->cycle = UAM_DETECTION_CYCLE_DEFAULT;
4700 /* Add service to database */
4701 if (UAM_ERROR_NONE != _uam_db_insert_service_info(&service_number,
4702 svc, service->cycle, app_num)) {
4703 UAM_ERR("_uam_db_insert_service_info failed");
4704 __free_service_info(service);
4705 return UAM_ERROR_DB_FAILED;
4708 services = g_slist_append(services, service);
4710 /* Send service registered event to application */
4711 /* <TO-DO> send event only to particular sender */
4712 if (UAM_ERROR_NONE != _uam_manager_send_event(NULL,
4713 UAM_EVENT_SERVICE_REGISTERED, g_variant_new("(is)",
4714 UAM_ERROR_NONE, service->name)))
4715 UAM_ERR("Failed to send UAM_EVENT_SERVICE_REGISTERED");
4718 return UAM_ERROR_NONE;
4721 int _uam_core_update_service(uam_service_info_s *svc)
4725 uam_db_service_info_t *service;
4727 retv_if(NULL == svc->name, UAM_ERROR_INVALID_PARAMETER);
4729 /* Retrieve service from list */
4730 l = __search_service(services, svc->name, svc->app_num);
4731 retv_if((NULL == l) || (l->data == NULL), UAM_ERROR_NOT_REGISTERED);
4734 service->presence_threshold = svc->presence_threshold;
4735 service->absence_threshold = svc->absence_threshold;
4737 /* Update service to database */
4738 if (UAM_ERROR_NONE != _uam_db_update_service_info(service, svc->app_num)) {
4739 UAM_ERR("_uam_db_update_service_info failed");
4740 return UAM_ERROR_DB_FAILED;
4744 return UAM_ERROR_NONE;
4747 int _uam_core_get_default_service(uam_service_info_s *service_info,
4751 int ret = UAM_ERROR_NONE;
4753 uam_db_service_info_t *service;
4754 gchar *default_service_name = NULL;
4756 retv_if(NULL == service_info, UAM_ERROR_INVALID_PARAMETER);
4757 retv_if(app_num <= 0, UAM_ERROR_INVALID_PARAMETER);
4759 /* Get the default service name specific to app */
4760 __get_default_service_name(app_num, &default_service_name);
4761 UAM_DBG("default_service_name is [%s]", default_service_name);
4763 l = __search_service(services, default_service_name, app_num);
4765 /* Register default service */
4766 memset(service_info, 0x00, sizeof(uam_service_info_s));
4768 service_info->app_num = app_num;
4769 g_strlcpy(service_info->name, default_service_name, UAM_SERVICE_MAX_STRING_LEN);
4770 service_info->presence_threshold = UAM_PRESENCE_THRESHOLD_DEFAULT;
4771 service_info->absence_threshold = UAM_ABSENCE_THRESHOLD_DEFAULT;
4773 ret = _uam_core_register_service(service_info, service_info->app_num);
4774 if ((UAM_ERROR_NONE != ret) && (UAM_ERROR_ALREADY_REGISTERED != ret)) {
4775 UAM_ERR("_uam_core_register_service failed with %s", _uam_manager_error_to_str(ret));
4780 __uam_copy_uam_db_service_info(service_info, service);
4784 free_n_null(&default_service_name);
4789 int _uam_core_unregister_service(const char *svc_name, const int app_num)
4793 uam_db_service_info_t *service;
4795 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
4797 /* Retrieve service from list */
4798 l = __search_service(services, svc_name, app_num);
4799 retv_if((NULL == l), UAM_ERROR_NOT_REGISTERED);
4802 /* Check if service is being used*/
4803 if (service->monitors) {
4804 UAM_ERR("service monitoring in progress");
4805 return UAM_ERROR_PERMISSION_DENIED;
4808 /* Remove service from database */
4809 if (UAM_ERROR_NONE != _uam_db_delete_service_info(service->name, app_num)) {
4810 UAM_ERR("_uam_db_delete_service_info failed");
4811 return UAM_ERROR_DB_FAILED;
4814 /* Remove service mapping from devices*/
4815 for (l = service->dev_techs; NULL != l; l = g_slist_next(l)) {
4816 uam_db_tech_info_t *tech = l->data;
4817 if (!tech || !tech->addresses)
4819 tech->svc_list = g_slist_remove(tech->svc_list, service);
4821 services = g_slist_remove(services, service);
4823 /* Send service unregistered event to application */
4824 if (UAM_ERROR_NONE != _uam_manager_send_event(NULL,
4825 UAM_EVENT_SERVICE_UNREGISTERED, g_variant_new("(is)",
4826 UAM_ERROR_NONE, service->name)))
4827 UAM_ERR("Failed to send UAM_EVENT_SERVICE_UNREGISTERED");
4830 return UAM_ERROR_NONE;
4833 static int __get_service_dev_list(
4834 uam_db_service_info_t* service, uam_device_info_s **device_list, int *count)
4840 int ret = UAM_ERROR_NONE;
4841 uam_svc_dev_info_t *svc_dev = NULL;
4842 uam_device_info_s *dev = NULL;
4845 /* Calculate number of devices */
4846 for (l1 = service->dev_techs; NULL != l1; l1 = g_slist_next(l1)) {
4847 uam_db_tech_info_t *tech = l1->data;
4848 if (!tech || !tech->addresses)
4854 *device_list = g_new0(uam_device_info_s, *count);
4857 for (l1 = service->dev_techs; NULL != l1; l1 = g_slist_next(l1)) {
4858 uam_db_tech_info_t *tech = l1->data;
4860 if (!tech || !tech->addresses)
4863 /* Copy tech info to device info */
4864 ret = __copy_tech_info_to_device_info(tech, &((*device_list)[indx]));
4865 if (UAM_ERROR_NONE != ret) {
4866 UAM_ERR("__copy_tech_info_to_device_info failed");
4870 (*device_list)[indx].last_seen = 0;
4874 UAM_INFO("Count = %d, indx = %d", *count, indx);
4876 /* Update service specific device last time */
4877 for (s = svc_devs; s; s = g_slist_next(s)) {
4880 if (!svc_dev || !svc_dev->device_id || !svc_dev->service)
4882 if (g_strcmp0(svc_dev->service, service->name))
4885 for (indx = 0; indx < *count; indx++) {
4886 dev = &((*device_list)[indx]);
4888 if (svc_dev->tech_type != dev->type ||
4889 g_strcmp0(svc_dev->device_id, dev->device_id))
4892 dev->last_seen = svc_dev->last_seen;
4900 int _uam_core_get_service_devices(const char *svc_name,
4901 int *count, uam_device_info_s **device_list, const int app_num)
4904 uam_db_service_info_t *service;
4906 int ret = UAM_ERROR_NONE;
4908 l = __search_service(services, svc_name, app_num);
4909 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
4912 ret = __get_service_dev_list(service, device_list, count);
4918 static void __get_service_user_list(
4919 const char* svc_name, uam_user_info_s **user_list, int *count, const int app_num)
4924 GSList *svc_user_list = NULL;
4927 /* Calculate number of users */
4928 for (l = svc_users; NULL != l; l = g_slist_next(l)) {
4929 uam_svc_user_info_t *svc_user = l->data;
4930 if (!svc_user || !svc_user->svc_name || !svc_user->account)
4933 if (g_strcmp0(svc_user->svc_name, svc_name) == 0)
4934 if (svc_user->app_num == app_num)
4935 svc_user_list = g_slist_append(svc_user_list, svc_user);
4939 *count = g_slist_length(svc_user_list);
4940 *user_list = g_new0(uam_user_info_s, *count);
4943 for (l = svc_user_list; l; l = g_slist_next(l)) {
4944 uam_svc_user_info_t *svc_user = l->data;
4945 uam_db_user_info_t *user;
4947 if (!svc_user || !svc_user->svc_name || !svc_user->account)
4950 /* Retrieve user from list */
4951 ll = __search_user(svc_user->account, app_num);
4957 g_strlcpy((*user_list)[indx].account,
4958 svc_user->account, UAM_USER_ACCOUNT_MAX_STRING_LEN);
4960 g_strlcpy((*user_list)[indx].name,
4961 user->name, UAM_USER_NAME_MAX_STRING_LEN);
4963 (*user_list)[indx].app_num = app_num;
4968 UAM_INFO("Count = %d, indx = %d", *count, indx);
4972 int _uam_core_get_service_users(const char *svc_name,
4973 int *count, uam_user_info_s **user_list, const int app_num)
4977 __get_service_user_list(svc_name, user_list, count, app_num);
4980 return UAM_ERROR_NONE;
4983 int _uam_core_get_services(int *count, uam_service_info_s **service_list, const int app_num)
4989 size = g_slist_length(services);
4990 *service_list = g_new0(uam_service_info_s, size);
4993 /* fetch services list from DB */
4994 for (l = services; l; l = g_slist_next(l)) {
4995 uam_db_service_info_t *db_info = l->data;
4997 if (!db_info || !db_info->name || db_info->app_num != app_num)
5000 g_strlcpy((*service_list)[*count].name,
5001 db_info->name, UAM_SERVICE_MAX_STRING_LEN);
5002 (*service_list)[*count].presence_threshold = db_info->presence_threshold;
5003 (*service_list)[*count].absence_threshold = db_info->absence_threshold;
5007 UAM_INFO("Count: %d", *count);
5009 return UAM_ERROR_NONE;
5012 int _uam_core_add_ibeacon_adv(unsigned int adv_len, const char *iadv, const int app_num)
5017 UAM_INFO("adv_len = %u, iadv = 0x%0x:0x%0x:0x%0x", adv_len,
5018 iadv[0], iadv[1], iadv[2]);
5020 ret = _uam_db_insert_adv_info(adv_len, iadv, app_num);
5021 if (UAM_ERROR_NONE != ret) {
5022 UAM_ERR("_uam_db_insert_adv_info failed");
5026 ret = _uam_pm_add_ibeacon_adv(adv_len, iadv);
5027 if (UAM_ERROR_NONE != ret) {
5028 UAM_ERR("Failed with error: %s (0x%4.4X)",
5029 _uam_manager_error_to_str(ret), ret);
5034 return UAM_ERROR_NONE;
5037 void _uam_core_handle_status_changed(unsigned int sensor, void *info)
5041 uam_sensor_info_s *sensor_info = info;
5043 ret_if(NULL == info);
5045 UAM_INFO("sensor: 0x%8.8X %s", sensor, sensor_info->status == UAS_ABSENCE ?
5046 "UAM_EVENT_ABSENCE_DETECTED" : "UAM_EVENT_PRESENCE_DETECTED");
5047 UAM_DBG("%llu %d %d", sensor_info->timestamp,
5048 sensor_info->accuracy, sensor_info->count);
5050 _uam_manager_send_event(NULL, UAM_EVENT_SENSOR_STATUS_CHANGED,
5051 g_variant_new("(uutiidddd)", sensor, sensor_info->status, sensor_info->timestamp,
5052 sensor_info->accuracy, sensor_info->count, sensor_info->values[0],
5053 sensor_info->values[1], sensor_info->values[2], sensor_info->values[3]));
5058 int _uam_core_add_payload(uam_ble_payload_s *payload,
5059 const char *device_id, int tech_type, const int app_num)
5062 int ret = UAM_ERROR_NONE;
5063 uam_db_tech_info_t *tech_info;
5064 uam_db_payload_info_t *db_payload;
5067 retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER);
5068 retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAMETER);
5069 retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAMETER);
5070 retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAMETER);
5072 /* <TO-DO> check if payload already exist */
5073 __print_payload(payload);
5075 tech_info = __uam_core_get_dev_tech_info(device_id, tech_type);
5076 retv_if(NULL == tech_info, UAM_ERROR_INVALID_PARAMETER);
5077 mac = __get_mac_addr(tech_info);
5079 db_payload = g_new0(uam_db_payload_info_t, 1);
5080 __uam_copy_uam_payload_info(db_payload, payload);
5081 db_payload->device_id = g_strdup(device_id);
5082 db_payload->tech_type = tech_type;
5083 db_payload->app_num = app_num;
5084 payloads = g_slist_append(payloads, db_payload);
5086 /*** Add payload to database ***/
5087 ret = _uam_db_insert_payload_info(device_id, tech_type, mac, payload, app_num);
5088 if (UAM_ERROR_NONE != ret) {
5089 UAM_ERR("_uam_db_insert_payload_info failed");
5094 return UAM_ERROR_NONE;
5097 int _uam_core_service_add_payload(uam_ble_payload_s *payload,
5098 const char *svc_name, const int app_num)
5101 int ret = UAM_ERROR_NONE;
5102 uam_db_service_info_t *service;
5103 uam_db_payload_info_t *db_payload;
5104 uam_svc_dev_info_t *svc_dev;
5107 retv_if(NULL == payload, UAM_ERROR_INVALID_PARAMETER);
5108 retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAMETER);
5110 /* Retrieve service from list */
5111 l = __search_service(services, svc_name, app_num);
5112 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
5115 /* Retrieve payload from list */
5116 l = g_slist_find_custom(payloads, payload, __compare_payload);
5117 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
5118 db_payload = l->data;
5120 /* Retrieve device from list */
5121 l = g_slist_find_custom(devices, db_payload->device_id, __compare_device_id);
5122 retv_if(NULL == l, UAM_ERROR_INVALID_PARAMETER);
5124 svc_dev = _uam_core_find_svc_dev_info(db_payload->device_id,
5125 db_payload->tech_type, service->name, app_num);
5126 retv_if(NULL == svc_dev, UAM_ERROR_INVALID_PARAMETER);
5129 _uam_core_update_svc_dev_info(db_payload->device_id, db_payload->tech_type,
5130 service->name, -1, -1, payload, app_num);
5132 /* Set/update registered device list to plugins */
5133 if (UAM_ERROR_NONE != _uam_pm_set_registered_devices(devices,
5134 __uam_core_tech_type_to_sensor(db_payload->tech_type)))
5135 UAM_ERR("_uam_pm_set_registered_devices failed");
5137 /*** Add payload service mapping to database ***/
5138 ret = _uam_db_update_device_service_payload_info(payload, service->name, app_num);
5139 if (UAM_ERROR_NONE != ret) {
5140 UAM_ERR("_uam_db_update_device_service_payload_info failed");
5145 return UAM_ERROR_NONE;
5148 int _uam_core_get_payloads(int *count, uam_ble_payload_s **payload_list, const int app_num)
5154 size = g_slist_length(payloads);
5155 *payload_list = g_new0(uam_ble_payload_s, size);
5158 /* fetch payloads list from DB */
5159 for (l = payloads; l; l = g_slist_next(l)) {
5160 uam_db_payload_info_t *db_info = l->data;
5164 UAM_DBG("db_info->app_num is [%d]", db_info->app_num);
5165 if (db_info->app_num != app_num)
5167 __uam_copy_db_payload_info(&((*payload_list)[*count]), db_info);
5168 __print_db_payload(db_info);
5172 UAM_INFO("Count: %d", *count);
5174 return UAM_ERROR_NONE;