From 53a3f531dada64bf0bdb26bb24f6925f9a30c972 Mon Sep 17 00:00:00 2001 From: "saerome.kim" Date: Thu, 18 Apr 2019 20:10:07 +0900 Subject: [PATCH 01/16] Fixed crash when UTC with Wi-Fi dummy plugin. Added error code UAM_ERROR_NOT_INITIALIZED Change-Id: I42542f5281a31a4d5f78b40e6b949cb90be4456d Signed-off-by: saerome.kim --- include/ua-api.h | 3 +- ua-api/src/ua-common.c | 1 + ua-api/src/ua-event-handler.c | 2 ++ ua-api/src/ua-request-sender.c | 19 ++++++------ ua-daemon/include/ua-manager-common.h | 2 ++ ua-daemon/src/ua-manager-request-handler.c | 46 +++++++++++++++++------------- 6 files changed, 44 insertions(+), 29 deletions(-) diff --git a/include/ua-api.h b/include/ua-api.h index 7b1245b..1f53432 100644 --- a/include/ua-api.h +++ b/include/ua-api.h @@ -72,13 +72,14 @@ typedef enum { ERROR(UAM_ERROR_TIMEOUT, TIZEN_ERROR_TIMED_OUT) \ ERROR(UAM_ERROR_NOW_IN_PROGRESS, TIZEN_ERROR_NOW_IN_PROGRESS) \ ERROR(UAM_ERROR_NOT_SUPPORTED, TIZEN_ERROR_NOT_SUPPORTED) \ - ERROR(UAM_ERROR_NOT_REGISTERED, TIZEN_ERROR_UA | 0x01) \ + ERROR(UAM_ERROR_NOT_INITIALIZED, TIZEN_ERROR_UA | 0x01) \ ERROR(UAM_ERROR_NOT_IN_OPERATION, TIZEN_ERROR_UA | 0x02) \ ERROR(UAM_ERROR_ALREADY_DONE, TIZEN_ERROR_UA | 0x03) \ ERROR(UAM_ERROR_INTERNAL, TIZEN_ERROR_UA | 0x04) \ ERROR(UAM_ERROR_NOT_FOUND, TIZEN_ERROR_UA | 0x10) \ ERROR(UAM_ERROR_ALREADY_REGISTERED, TIZEN_ERROR_UA | 0x11) \ ERROR(UAM_ERROR_DB_FAILED, TIZEN_ERROR_UA | 0x12) \ + ERROR(UAM_ERROR_NOT_REGISTERED, TIZEN_ERROR_UA | 0x13) \ #define GENERATE_ERROR_ENUM(ENUM, offset) ENUM = -offset, #define GENERATE_ERROR_STRING(STRING, offset) #STRING, diff --git a/ua-api/src/ua-common.c b/ua-api/src/ua-common.c index 65dd5be..d86197c 100644 --- a/ua-api/src/ua-common.c +++ b/ua-api/src/ua-common.c @@ -108,6 +108,7 @@ const char *_uam_error_to_str(int error) int err_id = -error; UAM_DBG("arr_size: %d, err_id: %d", arr_size, err_id); + retv_if(0 > err_id, NULL); retv_if(arr_size <= err_id, NULL); return error_string[err_id]; diff --git a/ua-api/src/ua-event-handler.c b/ua-api/src/ua-event-handler.c index 9a4af61..369d25a 100644 --- a/ua-api/src/ua-event-handler.c +++ b/ua-api/src/ua-event-handler.c @@ -274,7 +274,9 @@ int _uam_unregister_event_handler(void) retv_if(NULL == conn, UAM_ERROR_INTERNAL); g_dbus_connection_signal_unsubscribe(conn, event_handler_data->id); + memset(event_handler_data, 0, sizeof(uam_event_handler_data_t)); g_free(event_handler_data); + event_handler_data = NULL; FUNC_EXIT; return UAM_ERROR_NONE; diff --git a/ua-api/src/ua-request-sender.c b/ua-api/src/ua-request-sender.c index 940c1a8..312accf 100644 --- a/ua-api/src/ua-request-sender.c +++ b/ua-api/src/ua-request-sender.c @@ -285,24 +285,27 @@ static void __uam_async_request_cb( goto done; } - UAM_INFO("Event: %s [0x%4.4X], result= %s [0x%4.4X]", - _uam_event_to_str(event), event, - _uam_error_to_str(result), result); + UAM_INFO("result= %s [0x%4.4X]", _uam_error_to_str(result), result); event_data.result = result; - __uam_get_event_info(req_info->req_func, out_param, - &event, &event_data.data); - ((uam_event_callback)req_info->cb)( - event, &event_data, req_info->user_data); + if (req_info && req_info->req_func) { + __uam_get_event_info(req_info->req_func, out_param, + &event, &event_data.data); + + ((uam_event_callback)req_info->cb)( + event, &event_data, req_info->user_data); + } done: - if (out_param) + if (out_param) { g_array_free(out_param, TRUE); + } if (req_info) { pending_requests = g_slist_remove( pending_requests, (void *)req_info); g_free(req_info); + req_info = NULL; } FUNC_EXIT; } diff --git a/ua-daemon/include/ua-manager-common.h b/ua-daemon/include/ua-manager-common.h index 0f32d21..836e2a9 100644 --- a/ua-daemon/include/ua-manager-common.h +++ b/ua-daemon/include/ua-manager-common.h @@ -93,6 +93,8 @@ GSList *_uam_manager_get_request_list(void); void _uam_manager_remove_req_ctxt_from_list( uam_request_context_t *req_info); +void _uam_manager_cleanup_client(const char *name); + void _uam_manager_method_return( GDBusMethodInvocation *invocation, GArray *out_param, int result); diff --git a/ua-daemon/src/ua-manager-request-handler.c b/ua-daemon/src/ua-manager-request-handler.c index 5e775b1..538327c 100644 --- a/ua-daemon/src/ua-manager-request-handler.c +++ b/ua-daemon/src/ua-manager-request-handler.c @@ -802,32 +802,14 @@ static void __uam_manager_name_owner_changed_cb(GDBusConnection *connection, GVariant *parameters, gpointer user_data) { - int count = 1; const char *name = NULL; const char *old_owner = NULL; const char *new_owner = NULL; - GSList *list; g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner); - if ('\0' == *new_owner) { - /* Cleanup all pending request from this sender */ - __uam_manager_cleanup_requests_from_sender(name); - - g_mutex_lock(&uam_dbus_clients_mutex); - list = __uam_list_find_client(name); - if (list) { - /* found bus name in our bus list */ - __uamd_dbus_client_cleanup((gpointer)list->data); - uam_dbus_clients = g_slist_remove_link(uam_dbus_clients, list); - count = g_slist_length(uam_dbus_clients); - } - g_mutex_unlock(&uam_dbus_clients_mutex); - - if (0 == count) { - UAM_ERR("Quit ua-manager"); - g_main_loop_quit(main_loop); - } + /* Clean up client info */ + _uam_manager_cleanup_client(name); } } @@ -989,3 +971,27 @@ void _uam_manager_remove_req_ctxt_from_list(uam_request_context_t *req_info) } FUNC_EXIT; } + +void _uam_manager_cleanup_client(const char *name) +{ + int count = 1; + GSList *list = NULL; + + /* Cleanup all pending request from this sender */ + __uam_manager_cleanup_requests_from_sender(name); + + g_mutex_lock(&uam_dbus_clients_mutex); + list = __uam_list_find_client(name); + if (list) { + /* found bus name in our bus list */ + __uamd_dbus_client_cleanup((gpointer)list->data); + uam_dbus_clients = g_slist_remove_link(uam_dbus_clients, list); + count = g_slist_length(uam_dbus_clients); + } + g_mutex_unlock(&uam_dbus_clients_mutex); + + if (0 == count) { + UAM_ERR("Quit ua-manager"); + g_main_loop_quit(main_loop); + } +} -- 2.7.4 From 3f80fbaec8a687996c4e255792cdaa23bebc96ad Mon Sep 17 00:00:00 2001 From: Atul Rai Date: Wed, 24 Apr 2019 20:03:13 +0530 Subject: [PATCH 02/16] Added DETECTION STARTED/STOPPED event Change-Id: Idc03c7961c77cfc01d1b5bdf8fe724bc9a38ec4e Signed-off-by: Atul Rai --- include/ua-api.h | 2 ++ include/ua-internal.h | 2 ++ ua-api/src/ua-event-handler.c | 8 +++++ ua-daemon/include/ua-manager-core.h | 4 +++ ua-daemon/src/pm/ua-ble-plugin-handler.c | 13 +++++++ ua-daemon/src/pm/ua-light-plugin-handler.c | 13 +++++++ ua-daemon/src/pm/ua-motion-plugin-handler.c | 15 +++++++- ua-daemon/src/pm/ua-wifi-plugin-handler.c | 13 +++++++ ua-daemon/src/ua-manager-core.c | 55 ++++++++++++++++++++++++++++- ua-daemon/src/ua-manager-event-sender.c | 6 ++++ ua-plugins/include/ua-plugin.h | 7 ++++ 11 files changed, 136 insertions(+), 2 deletions(-) diff --git a/include/ua-api.h b/include/ua-api.h index 1f53432..f208e96 100644 --- a/include/ua-api.h +++ b/include/ua-api.h @@ -56,6 +56,8 @@ extern "C" { EVENT(UAM_EVENT_AMBIANT_MODE_DISABLED) \ EVENT(UAM_EVENT_SENSOR_STATE_READY) \ EVENT(UAM_EVENT_SENSOR_STATE_NOT_READY) \ + EVENT(UAM_EVENT_DETECTION_STARTED) \ + EVENT(UAM_EVENT_DETECTION_STOPPED) \ EVENT(UAM_EVENT_MAX) #define GENERATE_EVENT_ENUM(ENUM) ENUM, diff --git a/include/ua-internal.h b/include/ua-internal.h index fd6c8d9..cd508a7 100644 --- a/include/ua-internal.h +++ b/include/ua-internal.h @@ -78,6 +78,8 @@ typedef enum { #define UAM_SIGNAL_USER_ABSENCE_DETECTED "UserAbsenceDetected" #define UAM_SIGNAL_SENSOR_STATE_READY "SensorReady" #define UAM_SIGNAL_SENSOR_STATE_NOT_READY "SensorNotReady" +#define UAM_SIGNAL_DETECTION_STARTED "DetectionStarted" +#define UAM_SIGNAL_DETECTION_STOPPED "DetectionStopped" #ifdef __cplusplus } diff --git a/ua-api/src/ua-event-handler.c b/ua-api/src/ua-event-handler.c index 369d25a..5f2bae7 100644 --- a/ua-api/src/ua-event-handler.c +++ b/ua-api/src/ua-event-handler.c @@ -227,6 +227,14 @@ static void __uam_event_handler(GDBusConnection *connection, event = UAM_EVENT_DEVICE_REMOVED; __uam_send_event(event, result, &dev_info, event_info->cb, event_info->user_data); + } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_DETECTION_STARTED, + strlen(UAM_SIGNAL_DETECTION_STARTED))) { + __uam_send_event(UAM_EVENT_DETECTION_STARTED, result, NULL, + event_info->cb, event_info->user_data); + } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_DETECTION_STOPPED, + strlen(UAM_SIGNAL_DETECTION_STOPPED))) { + __uam_send_event(UAM_EVENT_DETECTION_STOPPED, result, NULL, + event_info->cb, event_info->user_data); } else { UAM_WARN("Unknown signal recieved: %s", signal_name); } diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index 1ee5e31..1ba8969 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -145,6 +145,10 @@ void _uam_core_reset_database(void); unsigned int _uam_core_get_active_sensors(int detection_mode); +void _uam_core_handle_detection_started(unsigned int sensor); + +void _uam_core_handle_detection_stopped(unsigned int sensor); + #ifdef __cplusplus } #endif diff --git a/ua-daemon/src/pm/ua-ble-plugin-handler.c b/ua-daemon/src/pm/ua-ble-plugin-handler.c index d2ffcdb..3e834d0 100644 --- a/ua-daemon/src/pm/ua-ble-plugin-handler.c +++ b/ua-daemon/src/pm/ua-ble-plugin-handler.c @@ -52,6 +52,18 @@ static void ble_lpm_detection_callback(uas_detection_type_e type) FUNC_EXIT; } +static void ble_detection_state_changed_cb(int state) +{ + FUNC_ENTRY; + + if (state) + _uam_core_handle_detection_started(UAM_SENSOR_BITMASK_BLE); + else + _uam_core_handle_detection_stopped(UAM_SENSOR_BITMASK_BLE); + + FUNC_EXIT; +} + static void ble_device_detection_callback(uas_detection_type_e type, uas_device_info_t *device) { FUNC_ENTRY; @@ -98,6 +110,7 @@ static void ble_device_added_callback(int status, uas_device_info_t *device) uas_callbacks_t ble_cbs = { .state_changed_cb = ble_state_changed_callback, + .detection_state_cb = ble_detection_state_changed_cb, .detected_cb = ble_lpm_detection_callback, .device_detected_cb = ble_device_detection_callback, .device_added_cb = ble_device_added_callback diff --git a/ua-daemon/src/pm/ua-light-plugin-handler.c b/ua-daemon/src/pm/ua-light-plugin-handler.c index 092f60d..ff962ac 100644 --- a/ua-daemon/src/pm/ua-light-plugin-handler.c +++ b/ua-daemon/src/pm/ua-light-plugin-handler.c @@ -30,6 +30,18 @@ void light_state_changed_callback(int state) FUNC_EXIT; } +static void light_detection_state_changed_cb(int state) +{ + FUNC_ENTRY; + + if (state) + _uam_core_handle_detection_started(UAM_SENSOR_BITMASK_LIGHT); + else + _uam_core_handle_detection_stopped(UAM_SENSOR_BITMASK_LIGHT); + + FUNC_EXIT; +} + void light_detection_callback(uas_detection_type_e type) { FUNC_ENTRY; @@ -49,6 +61,7 @@ void light_detection_callback(uas_detection_type_e type) uas_callbacks_t light_cbs = { .state_changed_cb = light_state_changed_callback, + .detection_state_cb = light_detection_state_changed_cb, .detected_cb = light_detection_callback, .device_detected_cb = NULL, .device_added_cb = NULL diff --git a/ua-daemon/src/pm/ua-motion-plugin-handler.c b/ua-daemon/src/pm/ua-motion-plugin-handler.c index 1dff18a..82bc6c6 100644 --- a/ua-daemon/src/pm/ua-motion-plugin-handler.c +++ b/ua-daemon/src/pm/ua-motion-plugin-handler.c @@ -30,6 +30,18 @@ void motion_state_changed_callback(int state) FUNC_EXIT; } +static void motion_detection_state_changed_cb(int state) +{ + FUNC_ENTRY; + + if (state) + _uam_core_handle_detection_started(UAM_SENSOR_BITMASK_MOTION); + else + _uam_core_handle_detection_stopped(UAM_SENSOR_BITMASK_MOTION); + + FUNC_EXIT; +} + void motion_detection_callback(uas_detection_type_e type) { FUNC_ENTRY; @@ -48,7 +60,8 @@ void motion_detection_callback(uas_detection_type_e type) } uas_callbacks_t motion_cbs = { - .state_changed_cb = motion_state_changed_callback, + .state_changed_cb = motion_detection_state_changed_cb, + .detection_state_cb = motion_state_changed_callback, .detected_cb = motion_detection_callback, .device_detected_cb = NULL, .device_added_cb = NULL diff --git a/ua-daemon/src/pm/ua-wifi-plugin-handler.c b/ua-daemon/src/pm/ua-wifi-plugin-handler.c index 1f6da6c..8630d80 100644 --- a/ua-daemon/src/pm/ua-wifi-plugin-handler.c +++ b/ua-daemon/src/pm/ua-wifi-plugin-handler.c @@ -52,6 +52,18 @@ static void wifi_lpm_detection_callback(uas_detection_type_e type) FUNC_EXIT; } +static void wifi_detection_state_changed_cb(int state) +{ + FUNC_ENTRY; + + if (state) + _uam_core_handle_detection_started(UAM_SENSOR_BITMASK_WIFI); + else + _uam_core_handle_detection_stopped(UAM_SENSOR_BITMASK_WIFI); + + FUNC_EXIT; +} + static void wifi_device_detection_callback(uas_detection_type_e type, uas_device_info_t *device) { FUNC_ENTRY; @@ -98,6 +110,7 @@ static void wifi_device_added_callback(int status, uas_device_info_t *device) uas_callbacks_t wifi_cbs = { .state_changed_cb = wifi_state_changed_callback, + .detection_state_cb = wifi_detection_state_changed_cb, .detected_cb = wifi_lpm_detection_callback, .device_detected_cb = wifi_device_detection_callback, .device_added_cb = wifi_device_added_callback diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index b343b6d..2f89739 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -44,6 +44,7 @@ GSList *devices; /* List of devices - uam_db_device_info_t */ static gboolean user_id_list[UAM_MAX_USERS]; static int next_user_id = 1; +static unsigned int detecting_sensors = 0; static int __uam_core_reserve_user_id(void) { @@ -1892,7 +1893,7 @@ void _uam_core_handle_absence_detected(unsigned int sensor, dev_info->type, dev_info->mac, tech->presence_state)) UAM_WARN("_ua_device_db_update_device_presence failed"); - __send_user_absence_event(user, dev_info->type, sensor); + //__send_user_absence_event(user, dev_info->type, sensor); FUNC_EXIT; } @@ -1949,3 +1950,55 @@ void _uam_core_reset_database(void) FUNC_EXIT; } + +void _uam_core_handle_detection_started(unsigned int sensor) +{ + FUNC_ENTRY; + unsigned int active_sensors = 0; + + UAM_DBG("Sensor: 0x%8.8X, detecting_sensors: 0x%8.8X", + sensor, detecting_sensors); + + detecting_sensors |= sensor; + active_sensors |= _uam_core_get_active_sensors(UAM_DETECT_PRESENCE); + active_sensors |= _uam_core_get_active_sensors(UAM_DETECT_ABSENCE); + if (active_sensors == detecting_sensors) { + /* Send detection started event */ + _uam_manager_send_event(NULL, UAM_EVENT_DETECTION_STARTED, NULL); + } + + FUNC_EXIT; +} + +void _uam_core_handle_detection_stopped(unsigned int sensor) +{ + FUNC_ENTRY; + uam_tech_type_e type = UAM_TECH_TYPE_NONE; + + UAM_DBG("Sensor: 0x%8.8X, detecting_sensors: 0x%8.8X", + sensor, detecting_sensors); + detecting_sensors &= ~sensor; + + if (UAM_SENSOR_BITMASK_BLE == sensor) + type = UAM_TECH_TYPE_BLE; + else if (UAM_SENSOR_BITMASK_WIFI == sensor) + type = UAM_TECH_TYPE_WIFI; + + if (UAM_TECH_TYPE_NONE != type) { + GSList *l; + + for (l = users; NULL != l; l = g_slist_next(l)) { + uam_db_user_info_t *user = l->data; + if (!user) + continue; + + __send_user_absence_event(user, type, sensor); + } + } + + if (0 == detecting_sensors) + /* Send detection stopped event */ + _uam_manager_send_event(NULL, UAM_EVENT_DETECTION_STOPPED, NULL); + + FUNC_EXIT; +} diff --git a/ua-daemon/src/ua-manager-event-sender.c b/ua-daemon/src/ua-manager-event-sender.c index 13db7e3..a709653 100644 --- a/ua-daemon/src/ua-manager-event-sender.c +++ b/ua-daemon/src/ua-manager-event-sender.c @@ -62,6 +62,12 @@ int _uam_manager_send_event( case UAM_EVENT_USER_ABSENCE_DETECTED: signal = UAM_SIGNAL_USER_ABSENCE_DETECTED; break; + case UAM_EVENT_DETECTION_STARTED: + signal = UAM_SIGNAL_DETECTION_STARTED; + break; + case UAM_EVENT_DETECTION_STOPPED: + signal = UAM_SIGNAL_DETECTION_STOPPED; + break; default: UAM_ERR("Unhandled event"); return UAM_ERROR_INTERNAL; diff --git a/ua-plugins/include/ua-plugin.h b/ua-plugins/include/ua-plugin.h index eb87268..8d2a836 100644 --- a/ua-plugins/include/ua-plugin.h +++ b/ua-plugins/include/ua-plugin.h @@ -113,6 +113,12 @@ typedef struct { typedef void (*uas_state_changed_callback)(int state); /* + * Callback to be invoked on Presen/Absence detection is started/stopped by plug-in's + * [Param] state - 0 = detection stopped, 1 = detection started. + */ +typedef void (*uas_detection_state_changed_callback)(int state); + +/* * Callback to be invoked on Presen/Absence detection by plug-in's which do * not support User identification. */ @@ -137,6 +143,7 @@ typedef void (*uas_device_added_callback)(int status, uas_device_info_t *device) /* UA plug-in callback structure */ typedef struct { uas_state_changed_callback state_changed_cb; + uas_detection_state_changed_callback detection_state_cb; uas_detection_callback detected_cb; uas_device_detection_callback device_detected_cb; uas_device_added_callback device_added_cb; -- 2.7.4 From 4569d0c0939c196783e2ee424b565abbf054fa28 Mon Sep 17 00:00:00 2001 From: Atul Rai Date: Tue, 7 May 2019 08:58:03 +0530 Subject: [PATCH 03/16] Added last seen timestamp in uam_device_info_t Change-Id: Id48102119eae0c2a536147eba3fea45e39d7a995 Signed-off-by: Atul Rai --- include/ua-api.h | 1 + ua-daemon/src/pm/ua-cloud-plugin-handler.c | 1 + ua-daemon/src/ua-manager-core.c | 1 + ua-plugins/include/ua-plugin-type.h | 1 + 4 files changed, 4 insertions(+) diff --git a/include/ua-api.h b/include/ua-api.h index f208e96..2669382 100644 --- a/include/ua-api.h +++ b/include/ua-api.h @@ -141,6 +141,7 @@ typedef struct { char mac[UAM_MAC_ADDRESS_STRING_LEN]; /* Device's MAC ADDRESS */ char ipv4_addr[UAM_IP_ADDRESS_MAX_STRING_LEN];/* Device's IPv4 address optional */ char device_id[UAM_DEVICE_ID_MAX_STRING_LEN]; /* Device's uniquie ID */ + long long int last_seen; /* Latest timestamp when device was discoverd */ } uam_device_info_t; typedef struct { diff --git a/ua-daemon/src/pm/ua-cloud-plugin-handler.c b/ua-daemon/src/pm/ua-cloud-plugin-handler.c index 1a98b54..8fd4b06 100644 --- a/ua-daemon/src/pm/ua-cloud-plugin-handler.c +++ b/ua-daemon/src/pm/ua-cloud-plugin-handler.c @@ -57,6 +57,7 @@ static void __uam_to_uap_device_info(uap_device_info_t *dest, dest->os = src->operating_system; dest->type = src->type; + dest->last_seen = src->last_seen; g_strlcpy(dest->device_id, src->device_id, UAP_DEVICE_ID_MAX_STRING_LEN); g_strlcpy(dest->mac, src->mac, UAP_MAC_ADDRESS_STRING_LEN); g_strlcpy(dest->ipv4_addr, src->ipv4_addr, UAP_IP_ADDRESS_MAX_STRING_LEN); diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 2f89739..7820f59 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -390,6 +390,7 @@ static void __get_uam_db_dev_list_to_uam_dev_list( (*device_list)[indx].operating_system = db_info->os; g_strlcpy((*device_list)[indx].device_id, db_info->device_id, UAM_DEVICE_ID_MAX_STRING_LEN); + (*device_list)[indx].last_seen = tech->timestamp; (*device_list)[indx++].type = tech->tech_type; } } diff --git a/ua-plugins/include/ua-plugin-type.h b/ua-plugins/include/ua-plugin-type.h index e91972b..cf46efa 100644 --- a/ua-plugins/include/ua-plugin-type.h +++ b/ua-plugins/include/ua-plugin-type.h @@ -50,6 +50,7 @@ typedef struct { char mac[UAP_MAC_ADDRESS_STRING_LEN]; /* Device's MAC ADDRESS */ char ipv4_addr[UAP_IP_ADDRESS_MAX_STRING_LEN];/* Device's IPv4 address optional */ char device_id[UAP_DEVICE_ID_MAX_STRING_LEN]; /* Device's uniquie ID */ + long long int last_seen; } uap_device_info_t; /* UA plugin return status */ -- 2.7.4 From 544f259fa10c8c3fe5d307f341ac7a081a83be34 Mon Sep 17 00:00:00 2001 From: saerome kim Date: Thu, 16 May 2019 15:58:40 +0900 Subject: [PATCH 04/16] Fixed securiti issue. - Security team requested apply the smack rule to the db files so that only ua-manager can get access to the db files. Change-Id: I454153a5167a66d61003f947cc93d53aaba1a915 Signed-off-by: saerome kim --- packaging/ua-manager.spec | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packaging/ua-manager.spec b/packaging/ua-manager.spec index 38c9cbc..91b41b2 100644 --- a/packaging/ua-manager.spec +++ b/packaging/ua-manager.spec @@ -77,7 +77,11 @@ cp net.uamd.service %{buildroot}%{_datadir}/dbus-1/system-services/net.uamd.serv mkdir -p %{buildroot}%{_unitdir} cp ua-manager.service %{buildroot}%{_unitdir}/ua-manager.service -%post -p /sbin/ldconfig +%post +/sbin/ldconfig + +/usr/bin/chsmack -a 'System' %{database_full_path} +/usr/bin/chsmack -a 'System' %{database_full_path}-journal %postun -p /sbin/ldconfig -- 2.7.4 From 62a7aac0fdc19f04a6a804560f714837bda8c4ee Mon Sep 17 00:00:00 2001 From: Atul Rai Date: Mon, 20 May 2019 17:11:35 +0530 Subject: [PATCH 05/16] Add plugin APIs and cb to support active device scan This patch adds APIs and callback on sensor plugin interface to support immediate scanning for active devcies in proximity. Change-Id: I803a920f1aff0a67ba288cf8a573e8b6b9ae179b Signed-off-by: Atul Rai --- ua-daemon/src/pm/ua-ble-plugin-handler.c | 3 ++- ua-daemon/src/pm/ua-light-plugin-handler.c | 3 ++- ua-daemon/src/pm/ua-motion-plugin-handler.c | 3 ++- ua-daemon/src/pm/ua-wifi-plugin-handler.c | 11 +++++++++- ua-plugins/include/ua-plugin.h | 34 +++++++++++++++++++++++++++++ 5 files changed, 50 insertions(+), 4 deletions(-) diff --git a/ua-daemon/src/pm/ua-ble-plugin-handler.c b/ua-daemon/src/pm/ua-ble-plugin-handler.c index 3e834d0..0755c31 100644 --- a/ua-daemon/src/pm/ua-ble-plugin-handler.c +++ b/ua-daemon/src/pm/ua-ble-plugin-handler.c @@ -113,5 +113,6 @@ uas_callbacks_t ble_cbs = { .detection_state_cb = ble_detection_state_changed_cb, .detected_cb = ble_lpm_detection_callback, .device_detected_cb = ble_device_detection_callback, - .device_added_cb = ble_device_added_callback + .device_added_cb = ble_device_added_callback, + .active_scan_cb = NULL }; diff --git a/ua-daemon/src/pm/ua-light-plugin-handler.c b/ua-daemon/src/pm/ua-light-plugin-handler.c index ff962ac..fbf0ed4 100644 --- a/ua-daemon/src/pm/ua-light-plugin-handler.c +++ b/ua-daemon/src/pm/ua-light-plugin-handler.c @@ -64,5 +64,6 @@ uas_callbacks_t light_cbs = { .detection_state_cb = light_detection_state_changed_cb, .detected_cb = light_detection_callback, .device_detected_cb = NULL, - .device_added_cb = NULL + .device_added_cb = NULL, + .active_scan_cb = NULL, }; diff --git a/ua-daemon/src/pm/ua-motion-plugin-handler.c b/ua-daemon/src/pm/ua-motion-plugin-handler.c index 82bc6c6..3efd1c5 100644 --- a/ua-daemon/src/pm/ua-motion-plugin-handler.c +++ b/ua-daemon/src/pm/ua-motion-plugin-handler.c @@ -64,5 +64,6 @@ uas_callbacks_t motion_cbs = { .detection_state_cb = motion_state_changed_callback, .detected_cb = motion_detection_callback, .device_detected_cb = NULL, - .device_added_cb = NULL + .device_added_cb = NULL, + .active_scan_cb = NULL, }; diff --git a/ua-daemon/src/pm/ua-wifi-plugin-handler.c b/ua-daemon/src/pm/ua-wifi-plugin-handler.c index 8630d80..cb82201 100644 --- a/ua-daemon/src/pm/ua-wifi-plugin-handler.c +++ b/ua-daemon/src/pm/ua-wifi-plugin-handler.c @@ -108,10 +108,19 @@ static void wifi_device_added_callback(int status, uas_device_info_t *device) FUNC_EXIT; } +static void wifi_active_scan_callback(uas_active_scan_event_e event, + const uas_device_info_t *device) +{ + FUNC_ENTRY; + + FUNC_EXIT; +} + uas_callbacks_t wifi_cbs = { .state_changed_cb = wifi_state_changed_callback, .detection_state_cb = wifi_detection_state_changed_cb, .detected_cb = wifi_lpm_detection_callback, .device_detected_cb = wifi_device_detection_callback, - .device_added_cb = wifi_device_added_callback + .device_added_cb = wifi_device_added_callback, + .active_scan_cb = wifi_active_scan_callback }; diff --git a/ua-plugins/include/ua-plugin.h b/ua-plugins/include/ua-plugin.h index 8d2a836..7f3746f 100644 --- a/ua-plugins/include/ua-plugin.h +++ b/ua-plugins/include/ua-plugin.h @@ -109,6 +109,12 @@ typedef struct { uas_address_info_t *addr_list; } uas_device_info_t; +/* Active scan event types */ +typedef enum { + UAS_ACTIVE_DEVICE_FOUND = 0x01, + UAS_ACTIVE_SCAN_COMPLETED = 0x02 +} uas_active_scan_event_e; + /* Callback to be invoked on plug-in's state changes (Ready <-> Not Ready) */ typedef void (*uas_state_changed_callback)(int state); @@ -140,6 +146,16 @@ typedef void (*uas_device_detection_callback)(uas_detection_type_e type, uas_dev */ typedef void (*uas_device_added_callback)(int status, uas_device_info_t *device); +/* + * Callback to be invoked in response to search_active_devices() API + * + * [Param] scan_event - UAS_ACTIVE_DEVICE_FOUND or, UAS_ACTIVE_SCAN_COMPLETED. + * [Param] device - Found registerd device info if event is UAS_ACTIVE_DEVICE_FOUND + * or, NULL if event is UAS_ACTIVE_SCAN_COMPLETED. + */ +typedef void (*uas_active_device_scan_callback)( + uas_active_scan_event_e event, const uas_device_info_t *device); + /* UA plug-in callback structure */ typedef struct { uas_state_changed_callback state_changed_cb; @@ -147,6 +163,7 @@ typedef struct { uas_detection_callback detected_cb; uas_device_detection_callback device_detected_cb; uas_device_added_callback device_added_cb; + uas_active_device_scan_callback active_scan_cb; } uas_callbacks_t; typedef struct { @@ -249,6 +266,23 @@ typedef struct { * detecting absence. */ int (*set_detection_threshold)(int presence_threshold, int absence_threshold); + + /* + * [Async API] To search for currently active devices in proximity. + * + * [Param] detection_period - Time in second for which User detection procedure is + * executed. + * + * Once detection_period is over, list of current active devices will be returned using + * uas_active_devices_callback(). + * + */ + int (*scan_active_devices)(int detection_period); + + /* + * [Async API] To cancel the ongoing active devices search. + */ + int (*cancel_active_device_scan)(void); } uas_api_t; typedef enum { -- 2.7.4 From 7fb41162e8b57dd2585303d32047d1303c702ff8 Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Fri, 24 May 2019 16:58:51 +0530 Subject: [PATCH 06/16] Added support for on_demand_scan Change-Id: I0f7c1fcceb920cbffabe864a1385c5b04612b1d0 Signed-off-by: Abhay agarwal --- include/ua-api.h | 6 ++ include/ua-internal.h | 4 + ua-api/src/ua-api.c | 37 +++++++ ua-api/src/ua-event-handler.c | 29 ++++++ ua-daemon/include/ua-manager-core.h | 12 +++ ua-daemon/include/ua-plugin-manager.h | 4 + ua-daemon/src/pm/ua-plugin-manager.c | 68 +++++++++++++ ua-daemon/src/pm/ua-pm-util.c | 13 +++ ua-daemon/src/pm/ua-pm-util.h | 2 + ua-daemon/src/pm/ua-wifi-plugin-handler.c | 16 +++ ua-daemon/src/ua-manager-core.c | 154 +++++++++++++++++++++++++++++ ua-daemon/src/ua-manager-event-sender.c | 6 ++ ua-daemon/src/ua-manager-request-handler.c | 24 +++++ 13 files changed, 375 insertions(+) diff --git a/include/ua-api.h b/include/ua-api.h index 2669382..cb85c46 100644 --- a/include/ua-api.h +++ b/include/ua-api.h @@ -58,6 +58,8 @@ extern "C" { EVENT(UAM_EVENT_SENSOR_STATE_NOT_READY) \ EVENT(UAM_EVENT_DETECTION_STARTED) \ EVENT(UAM_EVENT_DETECTION_STOPPED) \ + EVENT(UAM_EVENT_DEVICE_FOUND) \ + EVENT(UAM_EVENT_SCAN_COMPLETED) \ EVENT(UAM_EVENT_MAX) #define GENERATE_EVENT_ENUM(ENUM) ENUM, @@ -113,6 +115,10 @@ int _uam_start_absence_detection(unsigned int bitmask); int _uam_stop_absence_detection(unsigned int bitmask); +int _uam_start_search_active_devices(unsigned int bitmask, int detection_period); + +int _uam_stop_search_active_devices(unsigned int bitmask); + typedef enum { UAM_PRESENCE_STATE_INVALID = 0x00, UAM_PRESENCE_STATE_PRESENT, diff --git a/include/ua-internal.h b/include/ua-internal.h index cd508a7..f472921 100644 --- a/include/ua-internal.h +++ b/include/ua-internal.h @@ -59,6 +59,8 @@ extern "C" { REQUEST(UAM_REQUEST_SET_LOW_POWER_MODE) \ REQUEST(UAM_REQUEST_GET_DETECTION_PARAMS) \ REQUEST(UAM_REQUEST_SET_DETECTION_PARAMS) \ + REQUEST(UAM_REQUEST_START_SEARCH_ACTIVE_DEVICES) \ + REQUEST(UAM_REQUEST_STOP_SEARCH_ACTIVE_DEVICES) \ REQUEST(UAM_REQUEST_MAX) #define GENERATE_REQUEST_ENUM(ENUM) ENUM, @@ -80,6 +82,8 @@ typedef enum { #define UAM_SIGNAL_SENSOR_STATE_NOT_READY "SensorNotReady" #define UAM_SIGNAL_DETECTION_STARTED "DetectionStarted" #define UAM_SIGNAL_DETECTION_STOPPED "DetectionStopped" +#define UAM_SIGNAL_DEVICE_FOUND "DeviceFound" +#define UAM_SIGNAL_SCAN_COMPLETED "ScanCompleted" #ifdef __cplusplus } diff --git a/ua-api/src/ua-api.c b/ua-api/src/ua-api.c index d9b2621..4a7b354 100644 --- a/ua-api/src/ua-api.c +++ b/ua-api/src/ua-api.c @@ -772,3 +772,40 @@ UAM_EXPORT_API int _uam_set_detection_params(int cycle, int period, int retries) FUNC_EXIT; return ret; } + +UAM_EXPORT_API int _uam_start_search_active_devices(unsigned int bitmask, int detection_period) +{ + FUNC_ENTRY; + int ret; + + UAM_INIT_PARAMS(); + UAM_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + g_array_append_vals(in_param1, &bitmask, sizeof(unsigned int)); + g_array_append_vals(in_param2, &detection_period, sizeof(int)); + ret = _uam_sync_request(UAM_REQUEST_START_SEARCH_ACTIVE_DEVICES, + in_param1, in_param2, in_param3, in_param4, &out_param); + + UAM_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + FUNC_EXIT; + return ret; +} + +UAM_EXPORT_API int _uam_stop_search_active_devices(unsigned int bitmask) +{ + FUNC_ENTRY; + int ret; + + UAM_INIT_PARAMS(); + UAM_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + g_array_append_vals(in_param1, &bitmask, sizeof(unsigned int)); + ret = _uam_sync_request(UAM_REQUEST_STOP_SEARCH_ACTIVE_DEVICES, + in_param1, in_param2, in_param3, in_param4, &out_param); + + UAM_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + FUNC_EXIT; + return ret; +} diff --git a/ua-api/src/ua-event-handler.c b/ua-api/src/ua-event-handler.c index 5f2bae7..bf7df72 100644 --- a/ua-api/src/ua-event-handler.c +++ b/ua-api/src/ua-event-handler.c @@ -235,6 +235,35 @@ static void __uam_event_handler(GDBusConnection *connection, strlen(UAM_SIGNAL_DETECTION_STOPPED))) { __uam_send_event(UAM_EVENT_DETECTION_STOPPED, result, NULL, event_info->cb, event_info->user_data); + } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_DEVICE_FOUND, + strlen(UAM_SIGNAL_DEVICE_FOUND))) { + uam_device_info_t dev_info; + int os, type; + char *mac = NULL; + char *ipv4_addr = NULL; + char *device_id = NULL; + + g_variant_get(parameters, "(iii&s&s&s)", &result, + &os, &type, &mac, &ipv4_addr, &device_id); + + dev_info.operating_system = os; + dev_info.type = type; + g_strlcpy(dev_info.mac, + mac, UAM_MAC_ADDRESS_STRING_LEN); + g_strlcpy(dev_info.ipv4_addr, + ipv4_addr, UAM_IP_ADDRESS_MAX_STRING_LEN); + g_strlcpy(dev_info.device_id, + device_id, UAM_DEVICE_ID_MAX_STRING_LEN); + + event = UAM_EVENT_DEVICE_FOUND; + __uam_send_event(event, result, &dev_info, + event_info->cb, event_info->user_data); + } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_SCAN_COMPLETED, + strlen(UAM_SIGNAL_SCAN_COMPLETED))) { + + event = UAM_EVENT_SCAN_COMPLETED; + __uam_send_event(event, result, NULL, + event_info->cb, event_info->user_data); } else { UAM_WARN("Unknown signal recieved: %s", signal_name); } diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index 1ba8969..2d64e66 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -69,6 +69,12 @@ typedef struct uam_db_user_info { // long long int timestamp; } uam_db_user_info_t; + +typedef enum { + UAM_ACTIVE_DEVICE_FOUND = 0x01, + UAM_ACTIVE_SCAN_COMPLETED = 0x02 +} uam_active_scan_event_e; + int _uam_core_add_user(const char *account, const char *name); int _uam_core_remove_user(const char *account); @@ -149,6 +155,12 @@ void _uam_core_handle_detection_started(unsigned int sensor); void _uam_core_handle_detection_stopped(unsigned int sensor); +int _uam_core_start_search_active_devices(char *sender, unsigned int sensors, int detection_period); + +int _uam_core_stop_search_active_devices(char *sender, unsigned int sensors); + +void _uam_core_handle_active_device(uam_active_scan_event_e event, unsigned int sensor, const uam_device_info_t *dev_info); + #ifdef __cplusplus } #endif diff --git a/ua-daemon/include/ua-plugin-manager.h b/ua-daemon/include/ua-plugin-manager.h index e66eeb0..b26d354 100644 --- a/ua-daemon/include/ua-plugin-manager.h +++ b/ua-daemon/include/ua-plugin-manager.h @@ -58,6 +58,10 @@ int _uam_pm_set_detection_params(unsigned int sensor, int detection_cycle, int _uam_pm_set_detection_threshold(unsigned int sensor, int presence_threshold, int absence_threshold); +int _uam_pm_start_search_active_devices(unsigned int* bitmask, int detection_period); + +int _uam_pm_stop_search_active_devices(unsigned int bitmask); + #ifdef __cplusplus } #endif diff --git a/ua-daemon/src/pm/ua-plugin-manager.c b/ua-daemon/src/pm/ua-plugin-manager.c index 3a13745..a338bb3 100644 --- a/ua-daemon/src/pm/ua-plugin-manager.c +++ b/ua-daemon/src/pm/ua-plugin-manager.c @@ -668,3 +668,71 @@ int _uam_pm_set_detection_threshold(unsigned int sensor, FUNC_EXIT; return UAM_ERROR_NONE; } + +int _uam_pm_start_search_active_devices(unsigned int* bitmask, int detection_period) +{ + FUNC_ENTRY; + int id; + int status; + int ret = UAM_ERROR_INTERNAL; + + unsigned int scanning_sensor = 0; + + for (id = UAS_PLUGIN_ID_BLE; id < UAS_PLUGIN_ID_MAX; id++) { + uam_sensor_plugin_info_t *plugin = plugins[id]; + unsigned int sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id); + + if (!(sensor & (*bitmask))) + continue; + + if (!plugin || !plugin->api || !plugin->api->scan_active_devices) + continue; + + status = plugin->api->scan_active_devices(detection_period); + + if (UAS_STATUS_SUCCESS != status && UAS_STATUS_ALREADY_DONE != status) { + UAM_ERR("plugin->search_active_devices(%d) failed for %d", detection_period, id); + continue; + } + + scanning_sensor |= sensor; + /* Result success if scanning started for atleast 1 of the sensor */ + ret = UAM_ERROR_NONE; + } + + (*bitmask) &= scanning_sensor; + + FUNC_EXIT; + return ret; +} + +int _uam_pm_stop_search_active_devices(unsigned int bitmask) +{ + FUNC_ENTRY; + int id; + int status; + int ret = UAM_ERROR_INTERNAL; + + for (id = UAS_PLUGIN_ID_BLE; id < UAS_PLUGIN_ID_MAX; id++) { + uam_sensor_plugin_info_t *plugin = plugins[id]; + unsigned int sensor = _pm_util_uas_plugin_id_to_sensor_bitmask(id); + + if (!(sensor & bitmask)) + continue; + + if (!plugin || !plugin->api || !plugin->api->cancel_active_device_scan) + continue; + + status = plugin->api->cancel_active_device_scan(); + + if (UAS_STATUS_SUCCESS != status && UAS_STATUS_ALREADY_DONE != status) { + UAM_ERR("plugin->stop_search_active_devices() failed for %d", id); + continue; + } + + ret = UAM_ERROR_NONE; + } + + FUNC_EXIT; + return ret; +} diff --git a/ua-daemon/src/pm/ua-pm-util.c b/ua-daemon/src/pm/ua-pm-util.c index 58cf16e..08cc0fa 100644 --- a/ua-daemon/src/pm/ua-pm-util.c +++ b/ua-daemon/src/pm/ua-pm-util.c @@ -306,3 +306,16 @@ uam_device_info_t *_pm_util_uas_dev_info_to_uam_dev_info(const uas_device_info_t FUNC_EXIT; return device; } + +uam_active_scan_event_e _pm_util_uas_scan_event_to_uam_scan_event(uas_active_scan_event_e event) +{ + switch (event) { + case UAS_ACTIVE_DEVICE_FOUND : + return UAM_ACTIVE_DEVICE_FOUND; + case UAS_ACTIVE_SCAN_COMPLETED : + return UAM_ACTIVE_SCAN_COMPLETED; + default: + UAM_WARN("Unknown event 0x%8.8X", event); + return 0; + } +} diff --git a/ua-daemon/src/pm/ua-pm-util.h b/ua-daemon/src/pm/ua-pm-util.h index ec153df..7af480e 100644 --- a/ua-daemon/src/pm/ua-pm-util.h +++ b/ua-daemon/src/pm/ua-pm-util.h @@ -49,6 +49,8 @@ unsigned int _pm_util_uas_plugin_id_to_sensor_bitmask(uas_plugin_id_e id); const char *_pm_util_uas_status_to_str(int status); +uam_active_scan_event_e _pm_util_uas_scan_event_to_uam_scan_event(uas_active_scan_event_e event); + #ifdef __cplusplus } #endif diff --git a/ua-daemon/src/pm/ua-wifi-plugin-handler.c b/ua-daemon/src/pm/ua-wifi-plugin-handler.c index cb82201..2c95270 100644 --- a/ua-daemon/src/pm/ua-wifi-plugin-handler.c +++ b/ua-daemon/src/pm/ua-wifi-plugin-handler.c @@ -112,7 +112,23 @@ static void wifi_active_scan_callback(uas_active_scan_event_e event, const uas_device_info_t *device) { FUNC_ENTRY; + uam_device_info_t *dev_info = NULL; + uam_active_scan_event_e event_info; + if(UAS_ACTIVE_DEVICE_FOUND == event){ + ret_if(NULL == device); + + dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device); + ret_if(NULL == dev_info); + + dev_info->type = UAM_TECH_TYPE_WIFI; + } + + event_info = _pm_util_uas_scan_event_to_uam_scan_event(event); + + _uam_core_handle_active_device(event_info, UAM_SENSOR_BITMASK_WIFI, dev_info); + + g_free(dev_info); FUNC_EXIT; } diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 7820f59..9236abf 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -41,6 +41,7 @@ typedef struct { GSList *monitors; /* List of monitoring apps - uam_monitor_info_t */ GSList *users; /* List of users - uam_db_user_info_t */ GSList *devices; /* List of devices - uam_db_device_info_t */ +GSList *scanners; /* List of scanning apps - uam_monitor_info_t*/ static gboolean user_id_list[UAM_MAX_USERS]; static int next_user_id = 1; @@ -2003,3 +2004,156 @@ void _uam_core_handle_detection_stopped(unsigned int sensor) FUNC_EXIT; } + +static uam_monitor_info_t *__uam_find_scanner(const char *name) +{ +// FUNC_ENTRY; + GSList *l; + + retv_if(NULL == name, NULL); + + for (l = scanners; NULL != l; l = g_slist_next(l)) { + uam_monitor_info_t *scanner = l->data; + + if (!scanner || !scanner->name) + continue; + + if (0 == g_strcmp0(scanner->name, name)) { + UAM_DBG("Scanning application found in list"); + return scanner; + } + } + +// FUNC_EXIT; + return NULL; +} + +int _uam_core_start_search_active_devices(char *sender, unsigned int sensors, int detection_period) +{ + FUNC_ENTRY; + int ret; + uam_monitor_info_t *scanner; + GSList *l; + + retv_if(NULL == sender, UAM_ERROR_INVALID_PARAM); + + scanner = __uam_find_scanner(sender); + if (scanner) { + ret = UAM_ERROR_NOW_IN_PROGRESS; + UAM_ERR("Failed with error: %s (0x%4.4X)", + _uam_manager_error_to_str(ret), ret); + return ret; + } + + for (l = scanners; NULL != l; l = g_slist_next(l)) { + uam_monitor_info_t *scanner_data = l->data; + + if (!scanner_data || !scanner_data->name) + continue; + sensors &= ~(scanner_data->sensors); + } + + ret = _uam_pm_start_search_active_devices(&sensors, detection_period); + + if (UAM_ERROR_NONE != ret) { + UAM_ERR("Failed with error: %s (0x%4.4X)", + _uam_manager_error_to_str(ret), ret); + return ret; + } + + scanner = g_malloc0(sizeof(uam_monitor_info_t)); + scanner->name = g_strdup(sender); + scanner->sensors |= sensors; + scanners = g_slist_append(scanners, scanner); + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + +int _uam_core_stop_search_active_devices(char *sender, unsigned int sensors) +{ + FUNC_ENTRY; + int ret; + + GSList *l; + retv_if(NULL == sender, UAM_ERROR_INVALID_PARAM); + + for (l = scanners; NULL != l; l = g_slist_next(l)) { + uam_monitor_info_t *scanner = l->data; + + if (!scanner || !scanner->name) + continue; + + if (scanner->sensors & sensors){ + ret = _uam_pm_stop_search_active_devices(sensors); + if (UAM_ERROR_NONE != ret) { + UAM_ERR("Failed with error: %s (0x%4.4X)", + _uam_manager_error_to_str(ret), ret); + return ret; + } + scanners = g_slist_remove(scanners,scanner); + g_free(scanner->name); + g_free(scanner); + } + } + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + +void _uam_core_handle_active_device(uam_active_scan_event_e event, unsigned int sensor, const uam_device_info_t *dev_info) +{ + FUNC_ENTRY; + + GVariant *param = NULL; + GSList *l; + int done = 0; + char* name = NULL; + + for (l = scanners; NULL != l; l = g_slist_next(l)) { + uam_monitor_info_t *scanner = (uam_monitor_info_t *)l->data; + + if (!scanner || !scanner->name) + continue; + + if (scanner->sensors & sensor){ + + name = g_strdup(scanner->name); + if(event == UAM_ACTIVE_SCAN_COMPLETED){ + scanner->sensors &= ~(sensor); + if(scanner->sensors == 0){ + done = 1; + scanners = g_slist_remove(scanners, scanner); + g_free(scanner->name); + g_free(scanner); + } + } + break; + } + } + + if(event == UAM_ACTIVE_DEVICE_FOUND){ + param = g_variant_new("(iiisss)", + UAM_ERROR_NONE, + dev_info->operating_system, + dev_info->type, + dev_info->mac, + dev_info->ipv4_addr, + dev_info->device_id); + UAM_INFO_C("Send %s to applications", _uam_manager_event_to_str(UAM_EVENT_DEVICE_FOUND)); + /* Send device event to application */ + if (UAM_ERROR_NONE != _uam_manager_send_event(name, UAM_EVENT_DEVICE_FOUND, param)) + UAM_ERR("Failed to send %s", _uam_manager_event_to_str(event)); + } + else if(done == 1 && event == UAM_ACTIVE_SCAN_COMPLETED){ + UAM_INFO_C("Send %s to applications", _uam_manager_event_to_str(UAM_EVENT_SCAN_COMPLETED)); + /* Send device event to application */ + if (UAM_ERROR_NONE != _uam_manager_send_event(name, UAM_EVENT_SCAN_COMPLETED, param)) + UAM_ERR("Failed to send %s", _uam_manager_event_to_str(event)); + } + + if (name != NULL) + g_free(name); + + FUNC_EXIT; +} diff --git a/ua-daemon/src/ua-manager-event-sender.c b/ua-daemon/src/ua-manager-event-sender.c index a709653..2c064f6 100644 --- a/ua-daemon/src/ua-manager-event-sender.c +++ b/ua-daemon/src/ua-manager-event-sender.c @@ -68,6 +68,12 @@ int _uam_manager_send_event( case UAM_EVENT_DETECTION_STOPPED: signal = UAM_SIGNAL_DETECTION_STOPPED; break; + case UAM_EVENT_DEVICE_FOUND: + signal = UAM_SIGNAL_DEVICE_FOUND; + break; + case UAM_EVENT_SCAN_COMPLETED: + signal = UAM_SIGNAL_SCAN_COMPLETED; + break; default: UAM_ERR("Unhandled event"); return UAM_ERROR_INTERNAL; diff --git a/ua-daemon/src/ua-manager-request-handler.c b/ua-daemon/src/ua-manager-request-handler.c index 538327c..8435c78 100644 --- a/ua-daemon/src/ua-manager-request-handler.c +++ b/ua-daemon/src/ua-manager-request-handler.c @@ -390,6 +390,28 @@ static int __uam_manager_sync_request_handler( detection_param.period, detection_param.retries); break; } + case UAM_REQUEST_START_SEARCH_ACTIVE_DEVICES: { + unsigned int sensors; + int detection_period; + + __uam_manager_copy_params(in_param1, + &sensors, sizeof(unsigned int)); + + __uam_manager_copy_params(in_param2, + &detection_period, sizeof(int)); + + result = _uam_core_start_search_active_devices(sender, sensors, detection_period); + break; + } + case UAM_REQUEST_STOP_SEARCH_ACTIVE_DEVICES: { + unsigned int sensors; + + __uam_manager_copy_params(in_param1, + &sensors, sizeof(unsigned int)); + + result = _uam_core_stop_search_active_devices(sender, sensors); + break; + } default: UAM_WARN("UnSupported function [%s(0x%4.4X)]", _uam_manager_request_to_str(function), function); @@ -521,6 +543,8 @@ static gboolean __uam_manager_is_sync_function(int function) case UAM_REQUEST_SET_LOW_POWER_MODE: case UAM_REQUEST_GET_DETECTION_PARAMS: case UAM_REQUEST_SET_DETECTION_PARAMS: + case UAM_REQUEST_START_SEARCH_ACTIVE_DEVICES: + case UAM_REQUEST_STOP_SEARCH_ACTIVE_DEVICES: return TRUE; default: return FALSE; -- 2.7.4 From 4ebd5790ea36fd73e46046b3a2e358fd2f970ce5 Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Tue, 28 May 2019 19:30:26 +0530 Subject: [PATCH 07/16] add app launcher Change-Id: Ie82336dfd5bb8d86174d8805bd45336aec38e7fe Signed-off-by: Abhay agarwal --- include/ua-api.h | 11 ++ include/ua-internal.h | 2 + packaging/ua-manager.spec | 2 + ua-api/src/ua-api.c | 44 ++++++++ ua-api/src/ua-event-handler.c | 1 - ua-daemon/CMakeLists.txt | 2 + ua-daemon/include/ua-manager-common.h | 4 + ua-daemon/src/pm/ua-pm-util.c | 4 +- ua-daemon/src/pm/ua-wifi-plugin-handler.c | 2 +- ua-daemon/src/ua-manager-core.c | 15 ++- ua-daemon/src/ua-manager-event-sender.c | 155 ++++++++++++++++++++++++++++- ua-daemon/src/ua-manager-request-handler.c | 20 ++++ 12 files changed, 249 insertions(+), 13 deletions(-) diff --git a/include/ua-api.h b/include/ua-api.h index cb85c46..c8453fa 100644 --- a/include/ua-api.h +++ b/include/ua-api.h @@ -42,6 +42,7 @@ extern "C" { #define UAM_IP_ADDRESS_MAX_STRING_LEN 50 #define UAM_USER_NAME_MAX_STRING_LEN 254 #define UAM_USER_ACCOUNT_MAX_STRING_LEN 254 +#define UAM_APP_ID_MAX_STRING_LEN 100 #define FOREACH_EVENT(EVENT) \ EVENT(UAM_EVENT_USER_ADDED) \ @@ -119,6 +120,10 @@ int _uam_start_search_active_devices(unsigned int bitmask, int detection_period) int _uam_stop_search_active_devices(unsigned int bitmask); +int _uam_register_app(const char *app_id, unsigned short uid); + +int _uam_deregister_app(const char *app_id, unsigned short uid); + typedef enum { UAM_PRESENCE_STATE_INVALID = 0x00, UAM_PRESENCE_STATE_PRESENT, @@ -171,6 +176,12 @@ typedef struct { int retries; } uam_detection_params_t; +typedef struct { + char* sender; + unsigned short uid; + char app_id[UAM_APP_ID_MAX_STRING_LEN]; +} uam_app_info_t; + int _uam_get_registered_users(GPtrArray **user_list); int _uam_is_device_registered( diff --git a/include/ua-internal.h b/include/ua-internal.h index f472921..6ff05bc 100644 --- a/include/ua-internal.h +++ b/include/ua-internal.h @@ -61,6 +61,8 @@ extern "C" { REQUEST(UAM_REQUEST_SET_DETECTION_PARAMS) \ REQUEST(UAM_REQUEST_START_SEARCH_ACTIVE_DEVICES) \ REQUEST(UAM_REQUEST_STOP_SEARCH_ACTIVE_DEVICES) \ + REQUEST(UAM_REQUEST_REGISTER_APP) \ + REQUEST(UAM_REQUEST_DEREGISTER_APP) \ REQUEST(UAM_REQUEST_MAX) #define GENERATE_REQUEST_ENUM(ENUM) ENUM, diff --git a/packaging/ua-manager.spec b/packaging/ua-manager.spec index 91b41b2..2b8eb1d 100644 --- a/packaging/ua-manager.spec +++ b/packaging/ua-manager.spec @@ -20,6 +20,8 @@ BuildRequires: pkgconfig(gio-unix-2.0) BuildRequires: pkgconfig(libtzplatform-config) BuildRequires: pkgconfig(sqlite3) BuildRequires: pkgconfig(json-c) +BuildRequires: pkgconfig(bundle) +BuildRequires: pkgconfig(aul) Requires(post): /sbin/ldconfig Requires(postun): /sbin/ldconfig diff --git a/ua-api/src/ua-api.c b/ua-api/src/ua-api.c index 4a7b354..6b97199 100644 --- a/ua-api/src/ua-api.c +++ b/ua-api/src/ua-api.c @@ -809,3 +809,47 @@ UAM_EXPORT_API int _uam_stop_search_active_devices(unsigned int bitmask) FUNC_EXIT; return ret; } + +UAM_EXPORT_API int _uam_register_app(const char *app_id, unsigned short uid) +{ + FUNC_ENTRY; + int ret; + uam_app_info_t app_info; + + UAM_INIT_PARAMS(); + UAM_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + g_strlcpy(app_info.app_id, app_id, UAM_APP_ID_MAX_STRING_LEN); + app_info.uid = uid; + + g_array_append_vals(in_param1, &app_info, sizeof(uam_app_info_t)); + ret = _uam_sync_request(UAM_REQUEST_REGISTER_APP, + in_param1, in_param2, in_param3, in_param4, &out_param); + + UAM_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + FUNC_EXIT; + return ret; +} + +UAM_EXPORT_API int _uam_deregister_app(const char *app_id, unsigned short uid) +{ + FUNC_ENTRY; + int ret; + uam_app_info_t app_info; + + UAM_INIT_PARAMS(); + UAM_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + g_strlcpy(app_info.app_id, app_id, UAM_APP_ID_MAX_STRING_LEN); + app_info.uid = uid; + + g_array_append_vals(in_param1, &app_info, sizeof(uam_app_info_t)); + ret = _uam_sync_request(UAM_REQUEST_DEREGISTER_APP, + in_param1, in_param2, in_param3, in_param4, &out_param); + + UAM_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param); + + FUNC_EXIT; + return ret; +} diff --git a/ua-api/src/ua-event-handler.c b/ua-api/src/ua-event-handler.c index bf7df72..2e84f3e 100644 --- a/ua-api/src/ua-event-handler.c +++ b/ua-api/src/ua-event-handler.c @@ -17,7 +17,6 @@ #include "ua-api.h" #include "ua-internal.h" - #include "ua-common.h" typedef struct { diff --git a/ua-daemon/CMakeLists.txt b/ua-daemon/CMakeLists.txt index f957f4f..bb623bb 100644 --- a/ua-daemon/CMakeLists.txt +++ b/ua-daemon/CMakeLists.txt @@ -40,6 +40,8 @@ SET(PKG_MODULES libtzplatform-config sqlite3 json-c + bundle + aul ) INCLUDE(FindPkgConfig) diff --git a/ua-daemon/include/ua-manager-common.h b/ua-daemon/include/ua-manager-common.h index 836e2a9..c18e543 100644 --- a/ua-daemon/include/ua-manager-common.h +++ b/ua-daemon/include/ua-manager-common.h @@ -123,6 +123,10 @@ void _uam_remove_timer(guint id); const char* _uam_get_sensor_str(unsigned int ids); +int _uam_register_app_info(char* sender, uam_app_info_t app_info); + +int _uam_deregister_app_info(char* sender, uam_app_info_t app_info); + #ifdef __cplusplus } #endif diff --git a/ua-daemon/src/pm/ua-pm-util.c b/ua-daemon/src/pm/ua-pm-util.c index 08cc0fa..3467ea6 100644 --- a/ua-daemon/src/pm/ua-pm-util.c +++ b/ua-daemon/src/pm/ua-pm-util.c @@ -310,9 +310,9 @@ uam_device_info_t *_pm_util_uas_dev_info_to_uam_dev_info(const uas_device_info_t uam_active_scan_event_e _pm_util_uas_scan_event_to_uam_scan_event(uas_active_scan_event_e event) { switch (event) { - case UAS_ACTIVE_DEVICE_FOUND : + case UAS_ACTIVE_DEVICE_FOUND: return UAM_ACTIVE_DEVICE_FOUND; - case UAS_ACTIVE_SCAN_COMPLETED : + case UAS_ACTIVE_SCAN_COMPLETED: return UAM_ACTIVE_SCAN_COMPLETED; default: UAM_WARN("Unknown event 0x%8.8X", event); diff --git a/ua-daemon/src/pm/ua-wifi-plugin-handler.c b/ua-daemon/src/pm/ua-wifi-plugin-handler.c index 2c95270..d48c82d 100644 --- a/ua-daemon/src/pm/ua-wifi-plugin-handler.c +++ b/ua-daemon/src/pm/ua-wifi-plugin-handler.c @@ -115,7 +115,7 @@ static void wifi_active_scan_callback(uas_active_scan_event_e event, uam_device_info_t *dev_info = NULL; uam_active_scan_event_e event_info; - if(UAS_ACTIVE_DEVICE_FOUND == event){ + if (UAS_ACTIVE_DEVICE_FOUND == event) { ret_if(NULL == device); dev_info = _pm_util_uas_dev_info_to_uam_dev_info(device); diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 9236abf..73a0c1a 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -2084,14 +2084,14 @@ int _uam_core_stop_search_active_devices(char *sender, unsigned int sensors) if (!scanner || !scanner->name) continue; - if (scanner->sensors & sensors){ + if (scanner->sensors & sensors) { ret = _uam_pm_stop_search_active_devices(sensors); if (UAM_ERROR_NONE != ret) { UAM_ERR("Failed with error: %s (0x%4.4X)", _uam_manager_error_to_str(ret), ret); return ret; } - scanners = g_slist_remove(scanners,scanner); + scanners = g_slist_remove(scanners, scanner); g_free(scanner->name); g_free(scanner); } @@ -2116,12 +2116,12 @@ void _uam_core_handle_active_device(uam_active_scan_event_e event, unsigned int if (!scanner || !scanner->name) continue; - if (scanner->sensors & sensor){ + if (scanner->sensors & sensor) { name = g_strdup(scanner->name); - if(event == UAM_ACTIVE_SCAN_COMPLETED){ + if (event == UAM_ACTIVE_SCAN_COMPLETED) { scanner->sensors &= ~(sensor); - if(scanner->sensors == 0){ + if (scanner->sensors == 0) { done = 1; scanners = g_slist_remove(scanners, scanner); g_free(scanner->name); @@ -2132,7 +2132,7 @@ void _uam_core_handle_active_device(uam_active_scan_event_e event, unsigned int } } - if(event == UAM_ACTIVE_DEVICE_FOUND){ + if (event == UAM_ACTIVE_DEVICE_FOUND) { param = g_variant_new("(iiisss)", UAM_ERROR_NONE, dev_info->operating_system, @@ -2144,8 +2144,7 @@ void _uam_core_handle_active_device(uam_active_scan_event_e event, unsigned int /* Send device event to application */ if (UAM_ERROR_NONE != _uam_manager_send_event(name, UAM_EVENT_DEVICE_FOUND, param)) UAM_ERR("Failed to send %s", _uam_manager_event_to_str(event)); - } - else if(done == 1 && event == UAM_ACTIVE_SCAN_COMPLETED){ + } else if (done == 1 && event == UAM_ACTIVE_SCAN_COMPLETED) { UAM_INFO_C("Send %s to applications", _uam_manager_event_to_str(UAM_EVENT_SCAN_COMPLETED)); /* Send device event to application */ if (UAM_ERROR_NONE != _uam_manager_send_event(name, UAM_EVENT_SCAN_COMPLETED, param)) diff --git a/ua-daemon/src/ua-manager-event-sender.c b/ua-daemon/src/ua-manager-event-sender.c index 2c064f6..bc8e629 100644 --- a/ua-daemon/src/ua-manager-event-sender.c +++ b/ua-daemon/src/ua-manager-event-sender.c @@ -17,9 +17,81 @@ #include "ua-api.h" #include "ua-internal.h" - #include "ua-manager-common.h" +#include +#include +#include + +GSList *apps; /* List of apps to launch - uam_app_info_t*/ + +# define APP_CONTROL_DATA_PUSH_LAUNCH_TYPE "http://tizen.org/appcontrol/data/push/launch_type" +# define EXTRA_DATA_FROM_REGISTRATION_CHANGE "registration_change" + +static void __uam_app_launcher(uam_app_info_t *app_info) +{ + FUNC_ENTRY; + int r = 0; + + r = aul_app_get_status_for_uid((const char *)app_info->app_id, app_info->uid); + UAM_INFO("returned r: [%d] ", r); + if (r == -1) { + /* Send launch request to wake application */ + UAM_INFO("[%s] will be launched for receiving registration change callback", (const char*)app_info->app_id); + + bundle *bund = NULL; + bund = bundle_create(); + + bundle_add(bund, APP_CONTROL_DATA_PUSH_LAUNCH_TYPE, EXTRA_DATA_FROM_REGISTRATION_CHANGE); + + int result; + result = aul_svc_set_background_launch(bund, TRUE); + if (result < AUL_R_OK) + UAM_ERR("ERROR : aul_svc_set_background_launch failed. app_id [%s] bundle[%p] result[%d : %s]", app_info->app_id, bund, result, get_error_message(result)); + + result = aul_launch_app_async_for_uid(app_info->app_id, bund, app_info->uid); + if (result < AUL_R_OK) + UAM_ERR("ERROR : aul_launch_app failed. app_id [%s] bundle[%p] result[%d : %s]", app_info->app_id, bund, result, get_error_message(result)); + + bundle_free(bund); + } + FUNC_EXIT; +} + +static void __uam_resume_app(const char* dest) +{ + FUNC_ENTRY; + GSList *l; + + UAM_INFO("dest: [%s] ", dest); + if (NULL == dest) { + for (l = apps; NULL != l; l = g_slist_next(l)) { + uam_app_info_t *app_info = l->data; + if (NULL == app_info) + continue; + + UAM_INFO("app_id: [%s] ", (const char*)app_info->app_id); + UAM_INFO("uid: [%d] ", app_info->uid); + __uam_app_launcher(app_info); + } + } else { + for (l = apps; NULL != l; l = g_slist_next(l)) { + uam_app_info_t *app_info = l->data; + if (NULL == app_info) + continue; + + UAM_INFO("app_id: [%s] ", (const char*)app_info->app_id); + UAM_INFO("uid: [%d] ", app_info->uid); + if (!g_strcmp0(app_info->sender, dest)) { + __uam_app_launcher(app_info); + break; + } + } + } + + FUNC_EXIT; +} + int _uam_manager_send_event( const char* dest, int event, GVariant *param) { @@ -82,6 +154,9 @@ int _uam_manager_send_event( UAM_INFO_C("Destination: [%s], event: [%s], signal: [%s]", dest, _uam_manager_event_to_str(event), signal); + /* Check if application requires resume before sending event */ + __uam_resume_app(dest); + if (!g_dbus_connection_emit_signal(conn, dest, UAM_EVENT_PATH, UAM_EVENT_INTERFACE, signal, param, &error)) { UAM_ERR("Error while sending Signal: %s", signal); @@ -97,3 +172,81 @@ int _uam_manager_send_event( FUNC_EXIT; return UAM_ERROR_NONE; } + +static uam_app_info_t *__uam_find_app(const char *name) +{ +// FUNC_ENTRY; + GSList *l; + + retv_if(NULL == name, NULL); + + for (l = apps; NULL != l; l = g_slist_next(l)) { + uam_app_info_t *app_info = l->data; + + if (!app_info || !app_info->sender) + continue; + + if (0 == g_strcmp0(app_info->sender, name)) { + UAM_DBG("Monitoring application found in list"); + return app_info; + } + } + +// FUNC_EXIT; + return NULL; +} + +int _uam_register_app_info(char* sender, uam_app_info_t app_info) +{ + FUNC_ENTRY; + + int ret; + uam_app_info_t *app; + + app = __uam_find_app(sender); + + if (app) { + ret = UAM_ERROR_ALREADY_DONE; + UAM_ERR("Failed with error: %s (0x%4.4X)", + _uam_manager_error_to_str(ret), ret); + return ret; + } + + app = g_malloc0(sizeof(uam_app_info_t)); + app->sender = g_strdup(sender); + g_strlcpy(app->app_id, app_info.app_id, UAM_APP_ID_MAX_STRING_LEN); + app->uid = app_info.uid; + + apps = g_slist_append(apps, app); + ret = UAM_ERROR_NONE; + + FUNC_EXIT; + return ret; +} + +int _uam_deregister_app_info(char* sender, uam_app_info_t app_info) +{ + FUNC_ENTRY; + + int ret; + uam_app_info_t *app; + + app = __uam_find_app(sender); + + if (!app) { + ret = UAM_ERROR_ALREADY_DONE; + UAM_ERR("Failed with error: %s (0x%4.4X)", + _uam_manager_error_to_str(ret), ret); + return ret; + } + + UAM_DBG("Remove app from list"); + apps = g_slist_remove(apps, app); + g_free(app->sender); + g_free(app); + + ret = UAM_ERROR_NONE; + + FUNC_EXIT; + return ret; +} diff --git a/ua-daemon/src/ua-manager-request-handler.c b/ua-daemon/src/ua-manager-request-handler.c index 8435c78..9d515e9 100644 --- a/ua-daemon/src/ua-manager-request-handler.c +++ b/ua-daemon/src/ua-manager-request-handler.c @@ -412,6 +412,24 @@ static int __uam_manager_sync_request_handler( result = _uam_core_stop_search_active_devices(sender, sensors); break; } + case UAM_REQUEST_REGISTER_APP: { + uam_app_info_t app_info; + + __uam_manager_copy_params(in_param1, + &app_info, sizeof(uam_app_info_t)); + + result = _uam_register_app_info(sender, app_info); + break; + } + case UAM_REQUEST_DEREGISTER_APP: { + uam_app_info_t app_info; + + __uam_manager_copy_params(in_param1, + &app_info, sizeof(uam_app_info_t)); + + result = _uam_deregister_app_info(sender, app_info); + break; + } default: UAM_WARN("UnSupported function [%s(0x%4.4X)]", _uam_manager_request_to_str(function), function); @@ -545,6 +563,8 @@ static gboolean __uam_manager_is_sync_function(int function) case UAM_REQUEST_SET_DETECTION_PARAMS: case UAM_REQUEST_START_SEARCH_ACTIVE_DEVICES: case UAM_REQUEST_STOP_SEARCH_ACTIVE_DEVICES: + case UAM_REQUEST_REGISTER_APP: + case UAM_REQUEST_DEREGISTER_APP: return TRUE; default: return FALSE; -- 2.7.4 From 72bfa6529331a6f31d09e7852bf33c4e03fddae4 Mon Sep 17 00:00:00 2001 From: saerome kim Date: Mon, 3 Jun 2019 20:30:24 +0900 Subject: [PATCH 08/16] Fixed Tizen coding rule violations Change-Id: I86eeae7d65d58398e1d4be3c10e48180c8a1decd Signed-off-by: saerome kim --- include/ua-api.h | 28 ++++++++--------- ua-api/src/ua-api.c | 42 +++++++++++++++----------- ua-api/src/ua-event-handler.c | 48 ++++++++++-------------------- ua-daemon/include/ua-manager-common.h | 6 ++-- ua-daemon/src/pm/ua-cloud-plugin-handler.c | 2 +- ua-daemon/src/ua-manager-core.c | 16 ++++------ ua-daemon/src/ua-manager-event-sender.c | 8 ++--- 7 files changed, 68 insertions(+), 82 deletions(-) diff --git a/include/ua-api.h b/include/ua-api.h index c8453fa..ad4fb43 100644 --- a/include/ua-api.h +++ b/include/ua-api.h @@ -177,7 +177,7 @@ typedef struct { } uam_detection_params_t; typedef struct { - char* sender; + char *sender; unsigned short uid; char app_id[UAM_APP_ID_MAX_STRING_LEN]; } uam_app_info_t; @@ -195,33 +195,33 @@ int _uam_deinit(void); int _uam_get_available_sensors(unsigned int *bitmask); -int _uam_get_default_user(uam_user_info_t* uam_user); +int _uam_get_default_user(uam_user_info_t * uam_user); -int _uam_add_user(uam_user_info_t* user); +int _uam_add_user(uam_user_info_t * user); -int _uam_remove_user(char* account); +int _uam_remove_user(char *account); -int _uam_request_get_user_by_account(char* account, uam_user_info_t* user); +int _uam_request_get_user_by_account(char *account, uam_user_info_t * user); -int _uam_request_get_user_by_deviceid(char* device_id, uam_user_info_t* user); +int _uam_request_get_user_by_deviceid(char *device_id, uam_user_info_t * user); -int _uam_request_get_user_by_mac(char* mac, uam_user_info_t* user); +int _uam_request_get_user_by_mac(char *mac, uam_user_info_t * user); -int _uam_request_add_device(char* account, uam_device_info_t* device); +int _uam_request_add_device(char *account, uam_device_info_t *device); -int _uam_request_remove_device(char* account, uam_device_info_t* device); +int _uam_request_remove_device(char *account, uam_device_info_t *device); -int _uam_request_remove_device_by_deviceid(const char* device_id, uam_tech_type_e tech_type); +int _uam_request_remove_device_by_deviceid(const char *device_id, uam_tech_type_e tech_type); -int _uam_request_remove_device_by_mac(const char* mac); +int _uam_request_remove_device_by_mac(const char *mac); -int _uam_request_get_device_by_deviceid(const char* device_id, uam_tech_type_e tech_type, uam_device_info_t* device); +int _uam_request_get_device_by_deviceid(const char *device_id, uam_tech_type_e tech_type, uam_device_info_t *device); -int _uam_request_get_device_by_mac(const char* mac, uam_device_info_t* device); +int _uam_request_get_device_by_mac(const char *mac, uam_device_info_t *device); int _uam_request_get_devices(GPtrArray **devices_list); -int _uam_request_get_user_devices(char* account, GPtrArray **devices_list); +int _uam_request_get_user_devices(char *account, GPtrArray **devices_list); int _uam_request_set_detection_threshold(unsigned int sensor_type, int presence_threshold, int absence_threshold); diff --git a/ua-api/src/ua-api.c b/ua-api/src/ua-api.c index 6b97199..900b142 100644 --- a/ua-api/src/ua-api.c +++ b/ua-api/src/ua-api.c @@ -144,7 +144,7 @@ UAM_EXPORT_API int _uam_is_device_registered(uam_device_info_t *dev_info, gboole return ret; } -UAM_EXPORT_API int _uam_get_default_user(uam_user_info_t* user) +UAM_EXPORT_API int _uam_get_default_user(uam_user_info_t *user) { FUNC_ENTRY; int ret; @@ -159,7 +159,7 @@ UAM_EXPORT_API int _uam_get_default_user(uam_user_info_t* user) if (UAM_ERROR_NONE == ret) { if (out_param->len > 0) { - uam_user_info_t* info; + uam_user_info_t *info; info = &g_array_index(out_param, uam_user_info_t, 0); memcpy(user, info, sizeof(uam_user_info_t)); @@ -174,7 +174,7 @@ UAM_EXPORT_API int _uam_get_default_user(uam_user_info_t* user) return ret; } -UAM_EXPORT_API int _uam_add_user(uam_user_info_t* user) +UAM_EXPORT_API int _uam_add_user(uam_user_info_t *user) { FUNC_ENTRY; int ret; @@ -202,7 +202,7 @@ UAM_EXPORT_API int _uam_add_user(uam_user_info_t* user) return ret; } -UAM_EXPORT_API int _uam_remove_user(char* account) +UAM_EXPORT_API int _uam_remove_user(char *account) { FUNC_ENTRY; int ret; @@ -224,7 +224,8 @@ UAM_EXPORT_API int _uam_remove_user(char* account) return ret; } -UAM_EXPORT_API int _uam_request_get_user_by_account(char* account, uam_user_info_t* user) +UAM_EXPORT_API int _uam_request_get_user_by_account(char *account, + uam_user_info_t *user) { FUNC_ENTRY; int ret; @@ -243,7 +244,7 @@ UAM_EXPORT_API int _uam_request_get_user_by_account(char* account, uam_user_info if (UAM_ERROR_NONE == ret) { if (out_param->len > 0) { - uam_user_info_t* info; + uam_user_info_t *info; info = &g_array_index(out_param, uam_user_info_t, 0); memcpy(user, info, sizeof(uam_user_info_t)); @@ -258,7 +259,8 @@ UAM_EXPORT_API int _uam_request_get_user_by_account(char* account, uam_user_info return ret; } -UAM_EXPORT_API int _uam_request_get_user_by_deviceid(char* device_id, uam_user_info_t* user) +UAM_EXPORT_API int _uam_request_get_user_by_deviceid(char *device_id, + uam_user_info_t *user) { FUNC_ENTRY; int ret; @@ -277,7 +279,7 @@ UAM_EXPORT_API int _uam_request_get_user_by_deviceid(char* device_id, uam_user_i if (UAM_ERROR_NONE == ret) { if (out_param->len > 0) { - uam_user_info_t* info; + uam_user_info_t * info; info = &g_array_index(out_param, uam_user_info_t, 0); memcpy(user, info, sizeof(uam_user_info_t)); @@ -292,7 +294,8 @@ UAM_EXPORT_API int _uam_request_get_user_by_deviceid(char* device_id, uam_user_i return ret; } -UAM_EXPORT_API int _uam_request_get_user_by_mac(char* mac, uam_user_info_t* user) +UAM_EXPORT_API int _uam_request_get_user_by_mac(char *mac, + uam_user_info_t *user) { FUNC_ENTRY; int ret; @@ -311,7 +314,7 @@ UAM_EXPORT_API int _uam_request_get_user_by_mac(char* mac, uam_user_info_t* user if (UAM_ERROR_NONE == ret) { if (out_param->len > 0) { - uam_user_info_t* info; + uam_user_info_t * info; info = &g_array_index(out_param, uam_user_info_t, 0); memcpy(user, info, sizeof(uam_user_info_t)); @@ -326,7 +329,8 @@ UAM_EXPORT_API int _uam_request_get_user_by_mac(char* mac, uam_user_info_t* user return ret; } -UAM_EXPORT_API int _uam_request_add_device(char* account, uam_device_info_t* device) +UAM_EXPORT_API int _uam_request_add_device(char *account, + uam_device_info_t *device) { FUNC_ENTRY; int ret; @@ -350,7 +354,8 @@ UAM_EXPORT_API int _uam_request_add_device(char* account, uam_device_info_t* dev return ret; } -UAM_EXPORT_API int _uam_request_remove_device(char* account, uam_device_info_t* device) +UAM_EXPORT_API int _uam_request_remove_device(char *account, + uam_device_info_t *device) { FUNC_ENTRY; int ret; @@ -374,7 +379,8 @@ UAM_EXPORT_API int _uam_request_remove_device(char* account, uam_device_info_t* return ret; } -UAM_EXPORT_API int _uam_request_remove_device_by_deviceid(const char* device_id, uam_tech_type_e tech_type) +UAM_EXPORT_API int _uam_request_remove_device_by_deviceid( + const char *device_id, uam_tech_type_e tech_type) { FUNC_ENTRY; int ret; @@ -397,7 +403,7 @@ UAM_EXPORT_API int _uam_request_remove_device_by_deviceid(const char* device_id, return ret; } -UAM_EXPORT_API int _uam_request_remove_device_by_mac(const char* mac) +UAM_EXPORT_API int _uam_request_remove_device_by_mac(const char *mac) { FUNC_ENTRY; int ret; @@ -419,7 +425,8 @@ UAM_EXPORT_API int _uam_request_remove_device_by_mac(const char* mac) return ret; } -UAM_EXPORT_API int _uam_request_get_device_by_deviceid(const char* device_id, uam_tech_type_e tech_type, uam_device_info_t* device) +UAM_EXPORT_API int _uam_request_get_device_by_deviceid(const char *device_id, + uam_tech_type_e tech_type, uam_device_info_t *device) { FUNC_ENTRY; int ret; @@ -454,7 +461,8 @@ UAM_EXPORT_API int _uam_request_get_device_by_deviceid(const char* device_id, ua return ret; } -UAM_EXPORT_API int _uam_request_get_device_by_mac(const char* mac, uam_device_info_t* device) +UAM_EXPORT_API int _uam_request_get_device_by_mac(const char *mac, + uam_device_info_t *device) { FUNC_ENTRY; int ret; @@ -525,7 +533,7 @@ UAM_EXPORT_API int _uam_request_get_devices(GPtrArray **devices_list) return ret; } -UAM_EXPORT_API int _uam_request_get_user_devices(char* account, GPtrArray **devices_list) +UAM_EXPORT_API int _uam_request_get_user_devices(char *account, GPtrArray **devices_list) { FUNC_ENTRY; int ret; diff --git a/ua-api/src/ua-event-handler.c b/ua-api/src/ua-event-handler.c index 2e84f3e..42fae20 100644 --- a/ua-api/src/ua-event-handler.c +++ b/ua-api/src/ua-event-handler.c @@ -65,16 +65,13 @@ static void __uam_event_handler(GDBusConnection *connection, ret_if(NULL == event_info); - if (0 != strncasecmp(object_path, - UAM_EVENT_PATH, strlen(UAM_EVENT_PATH))) + if (0 != strcasecmp(object_path, UAM_EVENT_PATH)) return; - if (0 != strncasecmp(interface_name, - UAM_EVENT_INTERFACE, strlen(UAM_EVENT_INTERFACE))) + if (0 != strcasecmp(interface_name, UAM_EVENT_INTERFACE)) return; - if (0 == strncasecmp(signal_name, UAM_SIGNAL_USER_PRESENCE_DETECTED, - strlen(UAM_SIGNAL_USER_PRESENCE_DETECTED))) { + if (0 == strcasecmp(signal_name, UAM_SIGNAL_USER_PRESENCE_DETECTED)) { uam_detection_event_data_t event_data; unsigned int sensor_bitmask; char *account = NULL; @@ -89,8 +86,7 @@ static void __uam_event_handler(GDBusConnection *connection, __uam_send_event(event, result, &event_data, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_USER_ABSENCE_DETECTED, - strlen(UAM_SIGNAL_USER_ABSENCE_DETECTED))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_USER_ABSENCE_DETECTED)) { uam_detection_event_data_t event_data; unsigned int sensor_bitmask; char *account = NULL; @@ -105,8 +101,7 @@ static void __uam_event_handler(GDBusConnection *connection, __uam_send_event(event, result, &event_data, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_PRESENCE_DETECTED, - strlen(UAM_SIGNAL_PRESENCE_DETECTED))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_PRESENCE_DETECTED)) { unsigned int sensor_bitmask; g_variant_get(parameters, "(u)", &sensor_bitmask); @@ -115,8 +110,7 @@ static void __uam_event_handler(GDBusConnection *connection, __uam_send_event(event, result, &sensor_bitmask, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_ABSENCE_DETECTED, - strlen(UAM_SIGNAL_ABSENCE_DETECTED))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_ABSENCE_DETECTED)) { unsigned int sensor_bitmask; g_variant_get(parameters, "(u)", &sensor_bitmask); @@ -124,8 +118,7 @@ static void __uam_event_handler(GDBusConnection *connection, __uam_send_event(event, result, &sensor_bitmask, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_SENSOR_STATE_READY, - strlen(UAM_SIGNAL_SENSOR_STATE_READY))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_SENSOR_STATE_READY)) { unsigned int sensor; g_variant_get(parameters, "(iu)", &result, @@ -134,8 +127,7 @@ static void __uam_event_handler(GDBusConnection *connection, event = UAM_EVENT_SENSOR_STATE_READY; __uam_send_event(event, result, &sensor, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_SENSOR_STATE_NOT_READY, - strlen(UAM_SIGNAL_SENSOR_STATE_NOT_READY))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_SENSOR_STATE_NOT_READY)) { unsigned int sensor; g_variant_get(parameters, "(iu)", &result, @@ -144,8 +136,7 @@ static void __uam_event_handler(GDBusConnection *connection, event = UAM_EVENT_SENSOR_STATE_NOT_READY; __uam_send_event(event, result, &sensor, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_USER_ADDED, - strlen(UAM_SIGNAL_USER_ADDED))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_USER_ADDED)) { uam_user_info_t user_info; char *account = NULL; char *name = NULL; @@ -162,8 +153,7 @@ static void __uam_event_handler(GDBusConnection *connection, event = UAM_EVENT_USER_ADDED; __uam_send_event(event, result, &user_info, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_USER_REMOVED, - strlen(UAM_SIGNAL_USER_REMOVED))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_USER_REMOVED)) { uam_user_info_t user_info; char *account = NULL; char *name = NULL; @@ -180,8 +170,7 @@ static void __uam_event_handler(GDBusConnection *connection, event = UAM_EVENT_USER_REMOVED; __uam_send_event(event, result, &user_info, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_DEVICE_ADDED, - strlen(UAM_SIGNAL_DEVICE_ADDED))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_DEVICE_ADDED)) { uam_device_info_t dev_info; int os, type; char *mac = NULL; @@ -203,8 +192,7 @@ static void __uam_event_handler(GDBusConnection *connection, event = UAM_EVENT_DEVICE_ADDED; __uam_send_event(event, result, &dev_info, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_DEVICE_REMOVED, - strlen(UAM_SIGNAL_DEVICE_REMOVED))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_DEVICE_REMOVED)) { uam_device_info_t dev_info; int os, type; char *mac = NULL; @@ -226,16 +214,13 @@ static void __uam_event_handler(GDBusConnection *connection, event = UAM_EVENT_DEVICE_REMOVED; __uam_send_event(event, result, &dev_info, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_DETECTION_STARTED, - strlen(UAM_SIGNAL_DETECTION_STARTED))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_DETECTION_STARTED)) { __uam_send_event(UAM_EVENT_DETECTION_STARTED, result, NULL, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_DETECTION_STOPPED, - strlen(UAM_SIGNAL_DETECTION_STOPPED))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_DETECTION_STOPPED)) { __uam_send_event(UAM_EVENT_DETECTION_STOPPED, result, NULL, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_DEVICE_FOUND, - strlen(UAM_SIGNAL_DEVICE_FOUND))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_DEVICE_FOUND)) { uam_device_info_t dev_info; int os, type; char *mac = NULL; @@ -257,8 +242,7 @@ static void __uam_event_handler(GDBusConnection *connection, event = UAM_EVENT_DEVICE_FOUND; __uam_send_event(event, result, &dev_info, event_info->cb, event_info->user_data); - } else if (0 == strncasecmp(signal_name, UAM_SIGNAL_SCAN_COMPLETED, - strlen(UAM_SIGNAL_SCAN_COMPLETED))) { + } else if (0 == strcasecmp(signal_name, UAM_SIGNAL_SCAN_COMPLETED)) { event = UAM_EVENT_SCAN_COMPLETED; __uam_send_event(event, result, NULL, diff --git a/ua-daemon/include/ua-manager-common.h b/ua-daemon/include/ua-manager-common.h index c18e543..21941b3 100644 --- a/ua-daemon/include/ua-manager-common.h +++ b/ua-daemon/include/ua-manager-common.h @@ -100,7 +100,7 @@ void _uam_manager_method_return( GArray *out_param, int result); int _uam_manager_send_event( - const char* dest, int event, GVariant *param); + const char *dest, int event, GVariant *param); GDBusConnection *_uam_manager_get_gdbus_conn(void); @@ -123,9 +123,9 @@ void _uam_remove_timer(guint id); const char* _uam_get_sensor_str(unsigned int ids); -int _uam_register_app_info(char* sender, uam_app_info_t app_info); +int _uam_register_app_info(char *sender, uam_app_info_t app_info); -int _uam_deregister_app_info(char* sender, uam_app_info_t app_info); +int _uam_deregister_app_info(char *sender, uam_app_info_t app_info); #ifdef __cplusplus } diff --git a/ua-daemon/src/pm/ua-cloud-plugin-handler.c b/ua-daemon/src/pm/ua-cloud-plugin-handler.c index 8fd4b06..b61a0f9 100644 --- a/ua-daemon/src/pm/ua-cloud-plugin-handler.c +++ b/ua-daemon/src/pm/ua-cloud-plugin-handler.c @@ -123,7 +123,7 @@ static int add_device(const char *account, uap_device_info_t *dev_info) return UAP_STATUS_SUCCESS; } -static int remove_device(const char* device_id, int type) +static int remove_device(const char *device_id, int type) { FUNC_ENTRY; diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 73a0c1a..be48631 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -306,8 +306,7 @@ static uam_db_tech_info_t *__get_tech_info_by_mac(const char *mac) UAM_ADDR_TYPE_BT == addr->addr_type || UAM_ADDR_TYPE_P2P == addr->addr_type || UAM_ADDR_TYPE_WIFI == addr->addr_type) - if (!strncasecmp(addr->address, mac, - UAM_MAC_ADDRESS_STRING_LEN)) { + if (!strcasecmp(addr->address, mac)) { UAM_DBG("Device found Mac: %s, type: %d", addr->address, addr->addr_type); return tech; @@ -735,8 +734,7 @@ int _uam_core_is_device_added(uam_device_info_t *dev, gboolean *is_added) UAM_ADDR_TYPE_BT == addr->addr_type || UAM_ADDR_TYPE_P2P == addr->addr_type || UAM_ADDR_TYPE_WIFI == addr->addr_type) - if (!strncasecmp(addr->address, dev->mac, - UAM_MAC_ADDRESS_STRING_LEN)) { + if (!strcasecmp(addr->address, dev->mac)) { *is_added = TRUE; break; } @@ -1607,8 +1605,7 @@ void _uam_core_handle_device_added(int status, } if (dev->type != dev_info->type || - strncasecmp(dev->device_id, dev_info->device_id, - UAM_DEVICE_ID_MAX_STRING_LEN)) { + strcasecmp(dev->device_id, dev_info->device_id)) { UAM_WARN("[%d != %d] || [%s != %s]", dev->type, dev_info->type, dev->device_id, dev_info->device_id); _uam_manager_remove_req_ctxt_from_list(info); @@ -1749,8 +1746,7 @@ void _uam_core_handle_presence_detected(unsigned int sensor, if (!addr || (UAM_ADDR_TYPE_IPv4 != addr->addr_type)) continue; - if (!strncasecmp(addr->address, dev_info->ipv4_addr, - UAM_MAC_ADDRESS_STRING_LEN)) + if (!strcasecmp(addr->address, dev_info->ipv4_addr)) is_updated = FALSE; else UAM_DBG("Old IPv4: %s, New IPv4: %s", @@ -2054,7 +2050,6 @@ int _uam_core_start_search_active_devices(char *sender, unsigned int sensors, in } ret = _uam_pm_start_search_active_devices(&sensors, detection_period); - if (UAM_ERROR_NONE != ret) { UAM_ERR("Failed with error: %s (0x%4.4X)", _uam_manager_error_to_str(ret), ret); @@ -2108,7 +2103,7 @@ void _uam_core_handle_active_device(uam_active_scan_event_e event, unsigned int GVariant *param = NULL; GSList *l; int done = 0; - char* name = NULL; + char *name = NULL; for (l = scanners; NULL != l; l = g_slist_next(l)) { uam_monitor_info_t *scanner = (uam_monitor_info_t *)l->data; @@ -2117,7 +2112,6 @@ void _uam_core_handle_active_device(uam_active_scan_event_e event, unsigned int continue; if (scanner->sensors & sensor) { - name = g_strdup(scanner->name); if (event == UAM_ACTIVE_SCAN_COMPLETED) { scanner->sensors &= ~(sensor); diff --git a/ua-daemon/src/ua-manager-event-sender.c b/ua-daemon/src/ua-manager-event-sender.c index bc8e629..5f40dc5 100644 --- a/ua-daemon/src/ua-manager-event-sender.c +++ b/ua-daemon/src/ua-manager-event-sender.c @@ -58,7 +58,7 @@ static void __uam_app_launcher(uam_app_info_t *app_info) FUNC_EXIT; } -static void __uam_resume_app(const char* dest) +static void __uam_resume_app(const char *dest) { FUNC_ENTRY; GSList *l; @@ -93,7 +93,7 @@ static void __uam_resume_app(const char* dest) } int _uam_manager_send_event( - const char* dest, int event, GVariant *param) + const char *dest, int event, GVariant *param) { FUNC_ENTRY; char *signal; @@ -196,7 +196,7 @@ static uam_app_info_t *__uam_find_app(const char *name) return NULL; } -int _uam_register_app_info(char* sender, uam_app_info_t app_info) +int _uam_register_app_info(char *sender, uam_app_info_t app_info) { FUNC_ENTRY; @@ -224,7 +224,7 @@ int _uam_register_app_info(char* sender, uam_app_info_t app_info) return ret; } -int _uam_deregister_app_info(char* sender, uam_app_info_t app_info) +int _uam_deregister_app_info(char *sender, uam_app_info_t app_info) { FUNC_ENTRY; -- 2.7.4 From 0d3c7c94e8d2c8d1bd9ed0338e3a930402f5a538 Mon Sep 17 00:00:00 2001 From: Atul Rai Date: Tue, 4 Jun 2019 15:04:38 +0530 Subject: [PATCH 09/16] Fixed start/stop active scan functionality This patch adds support to handle start/stop active scan from multiple applications simultaneously. Change-Id: Ief32d9b9b743fc9d76731aee1829baf9b9536452 Signed-off-by: Atul Rai --- ua-api/src/ua-request-sender.c | 3 +- ua-daemon/include/ua-manager-core.h | 4 +- ua-daemon/include/ua-plugin-manager.h | 4 +- ua-daemon/src/pm/ua-plugin-manager.c | 4 +- ua-daemon/src/ua-manager-core.c | 196 +++++++++++++++++------------ ua-daemon/src/ua-manager-request-handler.c | 4 +- 6 files changed, 128 insertions(+), 87 deletions(-) diff --git a/ua-api/src/ua-request-sender.c b/ua-api/src/ua-request-sender.c index 312accf..dfe52da 100644 --- a/ua-api/src/ua-request-sender.c +++ b/ua-api/src/ua-request-sender.c @@ -297,9 +297,8 @@ static void __uam_async_request_cb( } done: - if (out_param) { + if (out_param) g_array_free(out_param, TRUE); - } if (req_info) { pending_requests = g_slist_remove( diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index 2d64e66..f56506a 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -155,9 +155,9 @@ void _uam_core_handle_detection_started(unsigned int sensor); void _uam_core_handle_detection_stopped(unsigned int sensor); -int _uam_core_start_search_active_devices(char *sender, unsigned int sensors, int detection_period); +int _uam_core_start_active_device_scan(char *sender, unsigned int sensors, int detection_period); -int _uam_core_stop_search_active_devices(char *sender, unsigned int sensors); +int _uam_core_stop_active_device_scan(char *sender, unsigned int sensors); void _uam_core_handle_active_device(uam_active_scan_event_e event, unsigned int sensor, const uam_device_info_t *dev_info); diff --git a/ua-daemon/include/ua-plugin-manager.h b/ua-daemon/include/ua-plugin-manager.h index b26d354..94b5080 100644 --- a/ua-daemon/include/ua-plugin-manager.h +++ b/ua-daemon/include/ua-plugin-manager.h @@ -58,9 +58,9 @@ int _uam_pm_set_detection_params(unsigned int sensor, int detection_cycle, int _uam_pm_set_detection_threshold(unsigned int sensor, int presence_threshold, int absence_threshold); -int _uam_pm_start_search_active_devices(unsigned int* bitmask, int detection_period); +int _uam_pm_start_active_device_scan(unsigned int* bitmask, int detection_period); -int _uam_pm_stop_search_active_devices(unsigned int bitmask); +int _uam_pm_stop_active_device_scan(unsigned int bitmask); #ifdef __cplusplus } diff --git a/ua-daemon/src/pm/ua-plugin-manager.c b/ua-daemon/src/pm/ua-plugin-manager.c index a338bb3..16da30d 100644 --- a/ua-daemon/src/pm/ua-plugin-manager.c +++ b/ua-daemon/src/pm/ua-plugin-manager.c @@ -669,7 +669,7 @@ int _uam_pm_set_detection_threshold(unsigned int sensor, return UAM_ERROR_NONE; } -int _uam_pm_start_search_active_devices(unsigned int* bitmask, int detection_period) +int _uam_pm_start_active_device_scan(unsigned int* bitmask, int detection_period) { FUNC_ENTRY; int id; @@ -706,7 +706,7 @@ int _uam_pm_start_search_active_devices(unsigned int* bitmask, int detection_per return ret; } -int _uam_pm_stop_search_active_devices(unsigned int bitmask) +int _uam_pm_stop_active_device_scan(unsigned int bitmask) { FUNC_ENTRY; int id; diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index be48631..355eb7e 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -38,6 +38,12 @@ typedef struct { uam_pm_detection_mode_e mode; } uam_monitor_info_t; +typedef struct { + char *name; + unsigned int sensors; + guint timer; +} uam_scanner_info_t; + GSList *monitors; /* List of monitoring apps - uam_monitor_info_t */ GSList *users; /* List of users - uam_db_user_info_t */ GSList *devices; /* List of devices - uam_db_device_info_t */ @@ -2001,15 +2007,14 @@ void _uam_core_handle_detection_stopped(unsigned int sensor) FUNC_EXIT; } -static uam_monitor_info_t *__uam_find_scanner(const char *name) +static uam_scanner_info_t *__uam_find_scanner(const char *name) { -// FUNC_ENTRY; GSList *l; retv_if(NULL == name, NULL); for (l = scanners; NULL != l; l = g_slist_next(l)) { - uam_monitor_info_t *scanner = l->data; + uam_scanner_info_t *scanner = l->data; if (!scanner || !scanner->name) continue; @@ -2020,75 +2025,105 @@ static uam_monitor_info_t *__uam_find_scanner(const char *name) } } -// FUNC_EXIT; return NULL; } -int _uam_core_start_search_active_devices(char *sender, unsigned int sensors, int detection_period) +static gboolean __scan_completed_cb(gpointer data) +{ + FUNC_ENTRY; + uam_scanner_info_t *scanner = data; + + retv_if(NULL == scanner, FALSE); + + if (UAM_ERROR_NONE != _uam_manager_send_event( + scanner->name, UAM_EVENT_SCAN_COMPLETED, NULL)) + UAM_ERR("Failed to send UAM_EVENT_SCAN_COMPLETED"); + else + UAM_INFO_C("Sent UAM_EVENT_SCAN_COMPLETED to [%s]", scanner->name); + + /* Free scanner */ + scanners = g_slist_remove(scanners, scanner); + g_free(scanner->name); + g_free(scanner); + + FUNC_EXIT; + return FALSE; +} + +int _uam_core_start_active_device_scan(char *sender, unsigned int sensors, int detection_period) { FUNC_ENTRY; int ret; - uam_monitor_info_t *scanner; - GSList *l; + uam_scanner_info_t *scanner; retv_if(NULL == sender, UAM_ERROR_INVALID_PARAM); + retv_if(0 == sensors, UAM_ERROR_INVALID_PARAM); scanner = __uam_find_scanner(sender); - if (scanner) { - ret = UAM_ERROR_NOW_IN_PROGRESS; - UAM_ERR("Failed with error: %s (0x%4.4X)", - _uam_manager_error_to_str(ret), ret); - return ret; - } + retv_if(NULL != scanner, UAM_ERROR_NOW_IN_PROGRESS); - for (l = scanners; NULL != l; l = g_slist_next(l)) { - uam_monitor_info_t *scanner_data = l->data; - - if (!scanner_data || !scanner_data->name) - continue; - sensors &= ~(scanner_data->sensors); - } - - ret = _uam_pm_start_search_active_devices(&sensors, detection_period); + ret = _uam_pm_start_active_device_scan(&sensors, detection_period); if (UAM_ERROR_NONE != ret) { UAM_ERR("Failed with error: %s (0x%4.4X)", _uam_manager_error_to_str(ret), ret); return ret; } - scanner = g_malloc0(sizeof(uam_monitor_info_t)); + scanner = g_malloc0(sizeof(uam_scanner_info_t)); scanner->name = g_strdup(sender); scanner->sensors |= sensors; + scanner->timer = g_timeout_add_seconds(detection_period, + __scan_completed_cb, scanner); scanners = g_slist_append(scanners, scanner); + UAM_DBG("sensors = 0x%8.8X - 0x%8.8X", scanner->sensors, sensors); FUNC_EXIT; return UAM_ERROR_NONE; } -int _uam_core_stop_search_active_devices(char *sender, unsigned int sensors) +int _uam_core_stop_active_device_scan(char *sender, unsigned int sensors) { FUNC_ENTRY; int ret; - + uam_scanner_info_t *scanner; GSList *l; + retv_if(NULL == sender, UAM_ERROR_INVALID_PARAM); + retv_if(0 == sensors, UAM_ERROR_INVALID_PARAM); + + scanner = __uam_find_scanner(sender); + retv_if(NULL == scanner, UAM_ERROR_NOT_IN_OPERATION); + + /* Trim sensors to a subset of active sensors for the scanner */ + sensors &= scanner->sensors; + + /* + * modify scanner's active sensors and if active sensors are NULL + * remove scanner from scanners list + */ + scanner->sensors &= ~sensors; + if (0 == scanner->sensors) { + scanners = g_slist_remove(scanners, scanner); + g_source_remove(scanner->timer); + g_free(scanner->name); + g_free(scanner); + } for (l = scanners; NULL != l; l = g_slist_next(l)) { - uam_monitor_info_t *scanner = l->data; + uam_scanner_info_t *scanner_data = l->data; - if (!scanner || !scanner->name) + if (!scanner_data || !scanner_data->name) continue; - if (scanner->sensors & sensors) { - ret = _uam_pm_stop_search_active_devices(sensors); - if (UAM_ERROR_NONE != ret) { - UAM_ERR("Failed with error: %s (0x%4.4X)", + sensors &= ~(scanner_data->sensors); + } + + if (0 != sensors) { + ret = _uam_pm_stop_active_device_scan(sensors); + if (UAM_ERROR_NONE != ret) { + UAM_ERR("Failed with error: %s (0x%4.4X)", _uam_manager_error_to_str(ret), ret); - return ret; - } - scanners = g_slist_remove(scanners, scanner); - g_free(scanner->name); - g_free(scanner); + return ret; } } @@ -2096,57 +2131,64 @@ int _uam_core_stop_search_active_devices(char *sender, unsigned int sensors) return UAM_ERROR_NONE; } -void _uam_core_handle_active_device(uam_active_scan_event_e event, unsigned int sensor, const uam_device_info_t *dev_info) +void _uam_core_handle_active_device(uam_active_scan_event_e event, + unsigned int sensor, const uam_device_info_t *dev_info) { FUNC_ENTRY; - - GVariant *param = NULL; GSList *l; - int done = 0; - char *name = NULL; - for (l = scanners; NULL != l; l = g_slist_next(l)) { - uam_monitor_info_t *scanner = (uam_monitor_info_t *)l->data; + ret_if((UAM_ACTIVE_SCAN_COMPLETED != event) && (NULL == dev_info)); - if (!scanner || !scanner->name) + for (l = scanners; NULL != l;) { + uam_scanner_info_t *scanner = l->data; + + if (!scanner || !scanner->name) { + l = g_slist_next(l); continue; + } - if (scanner->sensors & sensor) { - name = g_strdup(scanner->name); - if (event == UAM_ACTIVE_SCAN_COMPLETED) { - scanner->sensors &= ~(sensor); - if (scanner->sensors == 0) { - done = 1; - scanners = g_slist_remove(scanners, scanner); - g_free(scanner->name); - g_free(scanner); - } + if (0 == (scanner->sensors & sensor)) { + l = g_slist_next(l); + continue; + } + + if (event == UAM_ACTIVE_SCAN_COMPLETED) { + scanner->sensors &= ~(sensor); + UAM_DBG("sensors = 0x%8.8X", scanner->sensors); + if (0 != scanner->sensors) { + l = g_slist_next(l); + continue; } - break; + + if (UAM_ERROR_NONE != _uam_manager_send_event( + scanner->name, UAM_EVENT_SCAN_COMPLETED, NULL)) + UAM_ERR("Failed to send UAM_EVENT_SCAN_COMPLETED"); + else + UAM_INFO_C("Sent UAM_EVENT_SCAN_COMPLETED to [%s]", scanner->name); + + /* Free scanner */ + l = g_slist_next(l); + scanners = g_slist_remove(scanners, scanner); + g_source_remove(scanner->timer); + g_free(scanner->name); + g_free(scanner); + } else { + GVariant *param = g_variant_new("(iiisss)", + UAM_ERROR_NONE, + dev_info->operating_system, + dev_info->type, + dev_info->mac, + dev_info->ipv4_addr, + dev_info->device_id); + if (UAM_ERROR_NONE != _uam_manager_send_event( + scanner->name, UAM_EVENT_DEVICE_FOUND, param)) + UAM_ERR("Failed to send %s", _uam_manager_event_to_str(event)); + else + UAM_INFO_C("Sent UAM_EVENT_DEVICE_FOUND to [%s]", scanner->name); + + l = g_slist_next(l); } } - if (event == UAM_ACTIVE_DEVICE_FOUND) { - param = g_variant_new("(iiisss)", - UAM_ERROR_NONE, - dev_info->operating_system, - dev_info->type, - dev_info->mac, - dev_info->ipv4_addr, - dev_info->device_id); - UAM_INFO_C("Send %s to applications", _uam_manager_event_to_str(UAM_EVENT_DEVICE_FOUND)); - /* Send device event to application */ - if (UAM_ERROR_NONE != _uam_manager_send_event(name, UAM_EVENT_DEVICE_FOUND, param)) - UAM_ERR("Failed to send %s", _uam_manager_event_to_str(event)); - } else if (done == 1 && event == UAM_ACTIVE_SCAN_COMPLETED) { - UAM_INFO_C("Send %s to applications", _uam_manager_event_to_str(UAM_EVENT_SCAN_COMPLETED)); - /* Send device event to application */ - if (UAM_ERROR_NONE != _uam_manager_send_event(name, UAM_EVENT_SCAN_COMPLETED, param)) - UAM_ERR("Failed to send %s", _uam_manager_event_to_str(event)); - } - - if (name != NULL) - g_free(name); - FUNC_EXIT; } diff --git a/ua-daemon/src/ua-manager-request-handler.c b/ua-daemon/src/ua-manager-request-handler.c index 9d515e9..5f2fed3 100644 --- a/ua-daemon/src/ua-manager-request-handler.c +++ b/ua-daemon/src/ua-manager-request-handler.c @@ -400,7 +400,7 @@ static int __uam_manager_sync_request_handler( __uam_manager_copy_params(in_param2, &detection_period, sizeof(int)); - result = _uam_core_start_search_active_devices(sender, sensors, detection_period); + result = _uam_core_start_active_device_scan(sender, sensors, detection_period); break; } case UAM_REQUEST_STOP_SEARCH_ACTIVE_DEVICES: { @@ -409,7 +409,7 @@ static int __uam_manager_sync_request_handler( __uam_manager_copy_params(in_param1, &sensors, sizeof(unsigned int)); - result = _uam_core_stop_search_active_devices(sender, sensors); + result = _uam_core_stop_active_device_scan(sender, sensors); break; } case UAM_REQUEST_REGISTER_APP: { -- 2.7.4 From 13c855935ea84c6806b05a1e12f859c2d97b9ad4 Mon Sep 17 00:00:00 2001 From: saerome kim Date: Thu, 13 Jun 2019 17:01:26 +0900 Subject: [PATCH 10/16] Removed 'cap_net_admin' and 'ep' options in service file. Change-Id: I78ae7df02b1745186644045ed520fa8a318788fd Signed-off-by: saerome kim --- packaging/ua-manager.service | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/ua-manager.service b/packaging/ua-manager.service index ee5ed23..2b7d35a 100644 --- a/packaging/ua-manager.service +++ b/packaging/ua-manager.service @@ -10,5 +10,5 @@ Group=network_fw BusName=net.uamd.enabler SmackProcessLabel=System ExecStart=/usr/bin/ua-manager -Capabilities=cap_net_admin,cap_net_raw,cap_sys_rawio=eip +Capabilities=cap_net_raw,cap_sys_rawio=i SecureBits=keep-caps -- 2.7.4 From 085ce8266a45b5849fd9569653c286f7a3016a91 Mon Sep 17 00:00:00 2001 From: Atul Rai Date: Thu, 6 Jun 2019 18:37:58 +0530 Subject: [PATCH 11/16] Added APIs to add/remove user and device to service Change-Id: Ic2a96e869903068779265280105f2c6bf0c7c52b Signed-off-by: Atul Rai --- include/ua-api.h | 1 + include/ua-internal.h | 4 + ua-daemon/include/ua-manager-core.h | 15 ++ ua-daemon/src/ua-manager-core.c | 222 ++++++++++++++++++++++++++++- ua-daemon/src/ua-manager-request-handler.c | 54 +++++++ 5 files changed, 294 insertions(+), 2 deletions(-) diff --git a/include/ua-api.h b/include/ua-api.h index ad4fb43..8727231 100644 --- a/include/ua-api.h +++ b/include/ua-api.h @@ -144,6 +144,7 @@ typedef enum { UAM_TECH_TYPE_BLE = 0x02, UAM_TECH_TYPE_WIFI = 0x04, UAM_TECH_TYPE_P2P = 0x08, + UAM_TECH_TYPE_MAX } uam_tech_type_e; typedef struct { diff --git a/include/ua-internal.h b/include/ua-internal.h index 6ff05bc..da5de5e 100644 --- a/include/ua-internal.h +++ b/include/ua-internal.h @@ -63,6 +63,10 @@ extern "C" { REQUEST(UAM_REQUEST_STOP_SEARCH_ACTIVE_DEVICES) \ REQUEST(UAM_REQUEST_REGISTER_APP) \ REQUEST(UAM_REQUEST_DEREGISTER_APP) \ + REQUEST(UAM_REQUEST_ADD_USER_TO_SERVICE) \ + REQUEST(UAM_REQUEST_REMOVE_USER_FROM_SERVICE) \ + REQUEST(UAM_REQUEST_ADD_DEVICE_TO_SERVICE) \ + REQUEST(UAM_REQUEST_REMOVE_DEVICE_FROM_SERVICE) \ REQUEST(UAM_REQUEST_MAX) #define GENERATE_REQUEST_ENUM(ENUM) ENUM, diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index f56506a..aa07478 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -37,6 +37,12 @@ typedef enum { UAM_ADDR_TYPE_MAX } uam_addr_type_e; +#define UAM_DETECTION_CYCLE_DEFAULT 900 /* 900 seconds */ +typedef struct { + char *name; + unsigned int cycle; +} uam_db_service_info_t; + typedef struct { uam_addr_type_e addr_type; char *address; @@ -48,6 +54,7 @@ typedef struct { int presence_state; long long int timestamp; struct uam_db_device_info *device; + GSList *services; } uam_db_tech_info_t; typedef struct uam_db_device_info { @@ -161,6 +168,14 @@ int _uam_core_stop_active_device_scan(char *sender, unsigned int sensors); void _uam_core_handle_active_device(uam_active_scan_event_e event, unsigned int sensor, const uam_device_info_t *dev_info); +int _uam_core_service_add_user(const char *service_name, const char *account); + +int _uam_core_service_remove_user(const char *service, const char *account); + +int _uam_core_service_add_device(const char *service_name, const char *device_id, int tech_type); + +int _uam_core_service_remove_device(const char *service_name, const char *device_id, int tech_type); + #ifdef __cplusplus } #endif diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 355eb7e..9bd68aa 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -44,10 +44,12 @@ typedef struct { guint timer; } uam_scanner_info_t; -GSList *monitors; /* List of monitoring apps - uam_monitor_info_t */ GSList *users; /* List of users - uam_db_user_info_t */ GSList *devices; /* List of devices - uam_db_device_info_t */ -GSList *scanners; /* List of scanning apps - uam_monitor_info_t*/ +GSList *services; /* List of scanning apps - uam_db_service_info_t */ + +GSList *monitors; /* List of monitoring apps - uam_monitor_info_t */ +GSList *scanners; /* List of scanning apps - uam_scanner_info_t */ static gboolean user_id_list[UAM_MAX_USERS]; static int next_user_id = 1; @@ -106,6 +108,18 @@ static gint __compare_user_id(gconstpointer data, gconstpointer user_data) return 0; } +static gint __compare_svc_name(gconstpointer data, gconstpointer user_data) +{ + const uam_db_service_info_t *service = data; + const char *svc_name = user_data; + + retv_if(NULL == service, -1); + retv_if(NULL == service->name, -1); + retv_if(NULL == svc_name, -1); + + return g_strcmp0(service->name, svc_name); +} + static void __free_address_info(gpointer data) { FUNC_ENTRY; @@ -1207,6 +1221,209 @@ gboolean _uam_core_is_sensor_ready(unsigned int sensor) return is_ready; } +int _uam_core_service_add_user(const char *svc_name, const char *account) +{ + FUNC_ENTRY; + GSList *l; + uam_db_user_info_t *user; + uam_db_service_info_t *service; + + retv_if(NULL == account, UAM_ERROR_INVALID_PARAM); + retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAM); + + /* Retrieve user from list */ + l = g_slist_find_custom(users, account, __compare_user_account); + retv_if(NULL == l, UAM_ERROR_INVALID_PARAM); + user = l->data; + + /* Retrieve service from list */ + l = g_slist_find_custom(services, svc_name, __compare_svc_name); + if (NULL == l) { + service = g_new0(uam_db_service_info_t, 1); + service->name = g_strdup(svc_name); + service->cycle = UAM_DETECTION_CYCLE_DEFAULT; + } else + service = l->data; + + for (l = user->devices; NULL != l; l = g_slist_next(l)) { + uam_db_device_info_t *device = l->data; + GSList *l1; + + if (!device) + continue; + + for (l1 = device->tech_list; NULL != l1; l1 = g_slist_next(l1)) { + uam_db_tech_info_t *tech = l1->data; + GSList *l2; + + if (!tech) + continue; + + l2 = g_slist_find_custom(tech->services, svc_name, + __compare_svc_name); + if (NULL != l2) + continue; + + tech->services = g_slist_append(tech->services, service); + /* TODO: Add service-device to DB */ + } + } + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + +int _uam_core_service_remove_user(const char *svc_name, const char *account) +{ + FUNC_ENTRY; + GSList *l; + uam_db_user_info_t *user; + + retv_if(NULL == account, UAM_ERROR_INVALID_PARAM); + retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAM); + + /* Retrieve user from list */ + l = g_slist_find_custom(users, account, __compare_user_account); + retv_if(NULL == l, UAM_ERROR_INVALID_PARAM); + user = l->data; + + /* Retrieve service from list */ + l = g_slist_find_custom(services, svc_name, __compare_svc_name); + retv_if(NULL == l, UAM_ERROR_INVALID_PARAM); + + for (l = user->devices; NULL != l; l = g_slist_next(l)) { + uam_db_device_info_t *device = l->data; + GSList *l1; + + if (!device) + continue; + + for (l1 = device->tech_list; NULL != l1; l1 = g_slist_next(l1)) { + uam_db_tech_info_t *tech = l1->data; + GSList *l2; + + if (!tech) + continue; + + l2 = g_slist_find_custom(tech->services, svc_name, + __compare_svc_name); + if (NULL == l2) + continue; + + UAM_DBG("Service %s found, remove it from list", svc_name); + tech->services = g_slist_remove(tech->services, l2->data); + /* TODO: Remove service-device from DB */ + } + } + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + +static uam_db_tech_info_t *__uam_core_get_dev_tech_info(const char *device_id, int tech_type) +{ + FUNC_ENTRY; + uam_db_device_info_t *device; + GSList *l; + + retv_if(NULL == device_id, NULL); + retv_if(UAM_TECH_TYPE_NONE >= tech_type, NULL); + retv_if(UAM_TECH_TYPE_MAX <= tech_type, NULL); + + l = g_slist_find_custom(devices, device_id, __compare_device_id); + if (NULL == l) { + UAM_DBG("DeviceId [%s] is not in the list", device_id); + return NULL; + } + + device = l->data; + if (!(device->supported_techs & tech_type)) + if (NULL == l) { + UAM_DBG("Device type [0x%2.2X] for deviceId [%s] not found", + tech_type, device_id); + return NULL; + } + + for (l = device->tech_list; NULL != l; l = g_slist_next(l)) { + uam_db_tech_info_t *tech = l->data; + + if (!tech) + continue; + + if (tech_type == tech->tech_type) { + UAM_DBG("DeviceId [%s], Device type [0x%2.2X] found", + device_id, tech_type); + return tech; + } + } + + UAM_DBG("DeviceId [%s], Device type [0x%2.2X] not found", device_id, tech_type); + FUNC_EXIT; + return NULL; +} + +int _uam_core_service_add_device(const char *svc_name, const char *device_id, int tech_type) +{ + FUNC_ENTRY; + GSList *l; + uam_db_tech_info_t *tech_info; + uam_db_service_info_t *service; + + retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAM); + retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAM); + retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAM); + retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAM); + + tech_info = __uam_core_get_dev_tech_info(device_id, tech_type); + retv_if(NULL == tech_info, UAM_ERROR_INVALID_PARAM); + + /* Retrieve service from list */ + l = g_slist_find_custom(services, svc_name, __compare_svc_name); + if (NULL == l) { + service = g_new0(uam_db_service_info_t, 1); + service->name = g_strdup(svc_name); + service->cycle = UAM_DETECTION_CYCLE_DEFAULT; + } else + service = l->data; + + l = g_slist_find_custom(tech_info->services, svc_name, __compare_svc_name); + retv_if(NULL != l, UAM_ERROR_ALREADY_REGISTERED); + + tech_info->services = g_slist_append(tech_info->services, service); + /* TODO: Add service-device to DB */ + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + +int _uam_core_service_remove_device(const char *svc_name, const char *device_id, int tech_type) +{ + FUNC_ENTRY; + GSList *l; + uam_db_tech_info_t *tech_info; + + retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAM); + retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAM); + retv_if(UAM_TECH_TYPE_NONE >= tech_type, UAM_ERROR_INVALID_PARAM); + retv_if(UAM_TECH_TYPE_MAX <= tech_type, UAM_ERROR_INVALID_PARAM); + + /* Retrieve service from list */ + l = g_slist_find_custom(services, svc_name, __compare_svc_name); + retv_if(NULL == l, UAM_ERROR_INVALID_PARAM); + + tech_info = __uam_core_get_dev_tech_info(device_id, tech_type); + retv_if(NULL == tech_info, UAM_ERROR_INVALID_PARAM); + + l = g_slist_find_custom(tech_info->services, svc_name, __compare_svc_name); + retv_if(NULL == l, UAM_ERROR_NOT_REGISTERED); + + tech_info->services = g_slist_remove(tech_info->services, l->data); + /* TODO: Remove service-device from DB */ + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + int _uam_core_set_detection_threshold(unsigned int sensor, int presence_threshold, int absence_threshold) { @@ -1524,6 +1741,7 @@ int _uam_core_init(void) __uam_core_add_dev_to_list(user, &(db_info->dev_info), db_info->presence_state, db_info->timestamp); + /* TODO: Fetch and update device services from DB */ } g_slist_free_full(db_devices, g_free); diff --git a/ua-daemon/src/ua-manager-request-handler.c b/ua-daemon/src/ua-manager-request-handler.c index 5f2fed3..b6732ab 100644 --- a/ua-daemon/src/ua-manager-request-handler.c +++ b/ua-daemon/src/ua-manager-request-handler.c @@ -430,6 +430,56 @@ static int __uam_manager_sync_request_handler( result = _uam_deregister_app_info(sender, app_info); break; } + case UAM_REQUEST_ADD_USER_TO_SERVICE: { + const char *service_name; + const char *account; + + service_name = (char *)g_variant_get_data(in_param1); + account = (char *)g_variant_get_data(in_param2); + UAM_DBG("Account: [%s], Service: [%s]", account, service_name); + + result = _uam_core_service_add_user(service_name, account); + break; + } + case UAM_REQUEST_REMOVE_USER_FROM_SERVICE: { + const char *service_name; + const char *account; + + service_name = (char *)g_variant_get_data(in_param1); + account = (char *)g_variant_get_data(in_param2); + UAM_DBG("Account: [%s], Service: [%s]", account, service_name); + + result = _uam_core_service_remove_user(service_name, account); + break; + } + case UAM_REQUEST_ADD_DEVICE_TO_SERVICE: { + const char *service_name; + const char *device_id; + int tech_type; + + service_name = (char *)g_variant_get_data(in_param1); + device_id = (char *)g_variant_get_data(in_param2); + __uam_manager_copy_params(in_param3, &tech_type, sizeof(int)); + UAM_DBG("Service: [%s] device_id: [%s], tech_type: %d", + service_name, device_id, tech_type); + + result = _uam_core_service_add_device(service_name, device_id, tech_type); + break; + } + case UAM_REQUEST_REMOVE_DEVICE_FROM_SERVICE: { + const char *service_name; + const char *device_id; + int tech_type; + + service_name = (char *)g_variant_get_data(in_param1); + device_id = (char *)g_variant_get_data(in_param2); + __uam_manager_copy_params(in_param3, &tech_type, sizeof(int)); + UAM_DBG("Service: [%s] device_id: [%s], tech_type: %d", + service_name, device_id, tech_type); + + result = _uam_core_service_remove_device(service_name, device_id, tech_type); + break; + } default: UAM_WARN("UnSupported function [%s(0x%4.4X)]", _uam_manager_request_to_str(function), function); @@ -565,6 +615,10 @@ static gboolean __uam_manager_is_sync_function(int function) case UAM_REQUEST_STOP_SEARCH_ACTIVE_DEVICES: case UAM_REQUEST_REGISTER_APP: case UAM_REQUEST_DEREGISTER_APP: + case UAM_REQUEST_ADD_USER_TO_SERVICE: + case UAM_REQUEST_REMOVE_USER_FROM_SERVICE: + case UAM_REQUEST_ADD_DEVICE_TO_SERVICE: + case UAM_REQUEST_REMOVE_DEVICE_FROM_SERVICE: return TRUE; default: return FALSE; -- 2.7.4 From 04faebd84036d46b66603e3cd02b8c9d99378b9e Mon Sep 17 00:00:00 2001 From: Atul Rai Date: Mon, 10 Jun 2019 11:05:16 +0530 Subject: [PATCH 12/16] Implement GET/SET service detection cycle APIs Change-Id: I8115152142f5f53e3c84765e7f9815713a402dd7 Signed-off-by: Atul Rai --- include/ua-internal.h | 2 ++ ua-daemon/include/ua-manager-core.h | 9 +++++-- ua-daemon/src/ua-manager-core.c | 39 ++++++++++++++++++++++++++++++ ua-daemon/src/ua-manager-request-handler.c | 26 ++++++++++++++++++++ 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/include/ua-internal.h b/include/ua-internal.h index da5de5e..68829ca 100644 --- a/include/ua-internal.h +++ b/include/ua-internal.h @@ -67,6 +67,8 @@ extern "C" { REQUEST(UAM_REQUEST_REMOVE_USER_FROM_SERVICE) \ REQUEST(UAM_REQUEST_ADD_DEVICE_TO_SERVICE) \ REQUEST(UAM_REQUEST_REMOVE_DEVICE_FROM_SERVICE) \ + REQUEST(UAM_REQUEST_SET_SERVICE_DETECTION_CYCLE) \ + REQUEST(UAM_REQUEST_GET_SERVICE_DETECTION_CYCLE) \ REQUEST(UAM_REQUEST_MAX) #define GENERATE_REQUEST_ENUM(ENUM) ENUM, diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index aa07478..308220d 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -38,6 +38,7 @@ typedef enum { } uam_addr_type_e; #define UAM_DETECTION_CYCLE_DEFAULT 900 /* 900 seconds */ +#define UAM_DETECTION_CYCLE_MIN 300 /* 300 seconds */ typedef struct { char *name; unsigned int cycle; @@ -123,11 +124,11 @@ gboolean _uam_core_is_sensor_ready(unsigned int sensor); int _uam_core_set_detection_threshold(unsigned int sensor, int presence_threshold, int absence_threshold); -int _uam_core_start_presence_detection(char *sender, unsigned int sensors); +int _uam_core_start_presence_detection(const char *svc_name, char *sender, unsigned int sensors); int _uam_core_stop_presence_detection(char *sender, unsigned int sensors); -int _uam_core_start_absence_detection(char *sender, unsigned int sensors); +int _uam_core_start_absence_detection(const char *svc_name, char *sender, unsigned int sensors); int _uam_core_stop_absence_detection(char *sender, unsigned int sensors); @@ -176,6 +177,10 @@ int _uam_core_service_add_device(const char *service_name, const char *device_id int _uam_core_service_remove_device(const char *service_name, const char *device_id, int tech_type); +int _uam_core_set_service_detection_cycle(const char *svc_name, unsigned int cycle); + +int _uam_core_get_service_detection_cycle(const char *svc_name, unsigned int *cycle); + #ifdef __cplusplus } #endif diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 9bd68aa..d89c3d3 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -1424,6 +1424,45 @@ int _uam_core_service_remove_device(const char *svc_name, const char *device_id, return UAM_ERROR_NONE; } +int _uam_core_set_service_detection_cycle(const char *svc_name, unsigned int cycle) +{ + FUNC_ENTRY; + GSList *l; + uam_db_service_info_t *service; + + retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAM); + retv_if(UAM_DETECTION_CYCLE_MIN > cycle, UAM_ERROR_INVALID_PARAM); + retv_if(0 != (cycle % UAM_DETECTION_CYCLE_MIN), UAM_ERROR_INVALID_PARAM); + + l = g_slist_find_custom(services, svc_name, __compare_svc_name); + retv_if(NULL == l, UAM_ERROR_INVALID_PARAM); + service = l->data; + service->cycle = cycle; + + /* TODO: Update service detection cycle in DB */ + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + +int _uam_core_get_service_detection_cycle(const char *svc_name, unsigned int *cycle) +{ + FUNC_ENTRY; + GSList *l; + uam_db_service_info_t *service; + + retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAM); + retv_if(NULL == cycle, UAM_ERROR_INVALID_PARAM); + + l = g_slist_find_custom(services, svc_name, __compare_svc_name); + retv_if(NULL == l, UAM_ERROR_INVALID_PARAM); + service = l->data; + *cycle = service->cycle; + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + int _uam_core_set_detection_threshold(unsigned int sensor, int presence_threshold, int absence_threshold) { diff --git a/ua-daemon/src/ua-manager-request-handler.c b/ua-daemon/src/ua-manager-request-handler.c index b6732ab..b2f11a1 100644 --- a/ua-daemon/src/ua-manager-request-handler.c +++ b/ua-daemon/src/ua-manager-request-handler.c @@ -480,6 +480,30 @@ static int __uam_manager_sync_request_handler( result = _uam_core_service_remove_device(service_name, device_id, tech_type); break; } + case UAM_REQUEST_SET_SERVICE_DETECTION_CYCLE: { + const char *svc_name; + unsigned int cycle; + + svc_name = (char *)g_variant_get_data(in_param1); + __uam_manager_copy_params(in_param2, &cycle, sizeof(unsigned int)); + UAM_DBG("Service: [%s], Cycle [%d]", svc_name, cycle); + + result = _uam_core_set_service_detection_cycle(svc_name, cycle); + break; + } + case UAM_REQUEST_GET_SERVICE_DETECTION_CYCLE: { + const char *svc_name; + unsigned int cycle; + + svc_name = (char *)g_variant_get_data(in_param1); + UAM_DBG("Service: [%s]", svc_name); + + result = _uam_core_get_service_detection_cycle(svc_name, &cycle); + if (UAM_ERROR_NONE == result) + g_array_append_vals(*out_param1, &cycle, sizeof(unsigned int)); + + break; + } default: UAM_WARN("UnSupported function [%s(0x%4.4X)]", _uam_manager_request_to_str(function), function); @@ -619,6 +643,8 @@ static gboolean __uam_manager_is_sync_function(int function) case UAM_REQUEST_REMOVE_USER_FROM_SERVICE: case UAM_REQUEST_ADD_DEVICE_TO_SERVICE: case UAM_REQUEST_REMOVE_DEVICE_FROM_SERVICE: + case UAM_REQUEST_SET_SERVICE_DETECTION_CYCLE: + case UAM_REQUEST_GET_SERVICE_DETECTION_CYCLE: return TRUE; default: return FALSE; -- 2.7.4 From 387d4b8825007122ae8cfef9223ca65c2383d567 Mon Sep 17 00:00:00 2001 From: Atul Rai Date: Mon, 10 Jun 2019 20:46:41 +0530 Subject: [PATCH 13/16] Implement service based PRESENCE/ABSENCE detection Change-Id: I66f3c618c95399a99ac6d2d569ca00cce5869331 Signed-off-by: Atul Rai --- ua-daemon/include/ua-manager-core.h | 6 +- ua-daemon/src/ua-manager-core.c | 184 +++++++++++++++++++++-------- ua-daemon/src/ua-manager-request-handler.c | 48 ++++---- 3 files changed, 164 insertions(+), 74 deletions(-) diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index 308220d..a32d328 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -42,6 +42,8 @@ typedef enum { typedef struct { char *name; unsigned int cycle; + unsigned int remaining_time; + GSList *monitors; } uam_db_service_info_t; typedef struct { @@ -126,11 +128,11 @@ int _uam_core_set_detection_threshold(unsigned int sensor, int _uam_core_start_presence_detection(const char *svc_name, char *sender, unsigned int sensors); -int _uam_core_stop_presence_detection(char *sender, unsigned int sensors); +int _uam_core_stop_presence_detection(const char *svc_name, char *sender, unsigned int sensors); int _uam_core_start_absence_detection(const char *svc_name, char *sender, unsigned int sensors); -int _uam_core_stop_absence_detection(char *sender, unsigned int sensors); +int _uam_core_stop_absence_detection(const char *svc_name, char *sender, unsigned int sensors); int _uam_core_set_low_power_mode(gboolean mode); diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index d89c3d3..c243c4d 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -36,6 +36,7 @@ typedef struct { char *name; unsigned int sensors; uam_pm_detection_mode_e mode; + uam_db_service_info_t *service; } uam_monitor_info_t; typedef struct { @@ -51,6 +52,8 @@ GSList *services; /* List of scanning apps - uam_db_service_info_t */ GSList *monitors; /* List of monitoring apps - uam_monitor_info_t */ GSList *scanners; /* List of scanning apps - uam_scanner_info_t */ +static guint detection_timer = 0; + static gboolean user_id_list[UAM_MAX_USERS]; static int next_user_id = 1; static unsigned int detecting_sensors = 0; @@ -460,22 +463,25 @@ static int __copy_tech_info_to_device_info(uam_db_tech_info_t *tech, uam_device_ return UAM_ERROR_NONE; } -static uam_monitor_info_t *__uam_find_monitor( - const char *name, uam_pm_detection_mode_e mode) +static uam_monitor_info_t *__uam_find_monitor(GSList *monitor_list, + const char *name, const char *svc_name, uam_pm_detection_mode_e mode) { // FUNC_ENTRY; GSList *l; retv_if(NULL == name, NULL); + retv_if(NULL == monitor_list, NULL); - for (l = monitors; NULL != l; l = g_slist_next(l)) { + for (l = monitor_list; NULL != l; l = g_slist_next(l)) { uam_monitor_info_t *monitor = l->data; - if (!monitor || !monitor->name) + if (!monitor || !monitor->name || + !monitor->service || !monitor->service->name) continue; if ((mode == monitor->mode) && - (0 == g_strcmp0(monitor->name, name))) { + (0 == g_strcmp0(monitor->name, name)) && + (0 == g_strcmp0(monitor->service->name, svc_name))) { UAM_DBG("Monitoring application found in list"); return monitor; } @@ -1424,20 +1430,27 @@ int _uam_core_service_remove_device(const char *svc_name, const char *device_id, return UAM_ERROR_NONE; } -int _uam_core_set_service_detection_cycle(const char *svc_name, unsigned int cycle) +int _uam_core_set_service_detection_cycle(const char *svc_name, unsigned int new_cycle) { FUNC_ENTRY; GSList *l; uam_db_service_info_t *service; + unsigned int elapsed_time; retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAM); - retv_if(UAM_DETECTION_CYCLE_MIN > cycle, UAM_ERROR_INVALID_PARAM); - retv_if(0 != (cycle % UAM_DETECTION_CYCLE_MIN), UAM_ERROR_INVALID_PARAM); + retv_if(UAM_DETECTION_CYCLE_MIN > new_cycle, UAM_ERROR_INVALID_PARAM); + retv_if(0 != (new_cycle % UAM_DETECTION_CYCLE_MIN), UAM_ERROR_INVALID_PARAM); l = g_slist_find_custom(services, svc_name, __compare_svc_name); retv_if(NULL == l, UAM_ERROR_INVALID_PARAM); service = l->data; - service->cycle = cycle; + + elapsed_time = service->cycle - service->remaining_time; + service->cycle = new_cycle; + if (new_cycle < elapsed_time) + service->remaining_time = 0; + else + service->remaining_time = new_cycle - elapsed_time; /* TODO: Update service detection cycle in DB */ @@ -1481,69 +1494,117 @@ int _uam_core_set_detection_threshold(unsigned int sensor, return UAM_ERROR_NONE; } -static int __uam_core_start_detection(int detection_type, char *sender, unsigned int sensors) +static gboolean __start_detection(gpointer data) { FUNC_ENTRY; int ret; - unsigned int active_sensors; - unsigned int inactive_sensors; + uam_db_service_info_t *service = NULL; + unsigned int sensors; + gboolean start_detection = FALSE; + GSList *l; + + for (l = services; NULL != l; l = g_slist_next(l)) { + service = l->data; + + if (!service || !service->monitors) + continue; + + if (0 == service->remaining_time) { + start_detection = TRUE; + service->remaining_time = service->cycle; + } else + service->remaining_time -= UAM_DETECTION_CYCLE_MIN; + } + + if (!start_detection) + goto done; + + /* Get sensors on which PRESENCE detection to be started */ + sensors = _uam_core_get_active_sensors(UAM_DETECT_PRESENCE); + UAM_DBG("PRESENCE sensors: 0x%8.8X", sensors); + + /* Start PRESENCE detection */ + ret = _uam_pm_start_detection(UAM_DETECT_PRESENCE, sensors); + if (UAM_ERROR_NONE != ret) + UAM_ERR("Failed with error: %s (0x%4.4X)", + _uam_manager_error_to_str(ret), ret); + + /* Get sensors on which ABSENCE detection to be started */ + sensors = _uam_core_get_active_sensors(UAM_DETECT_ABSENCE); + UAM_DBG("ABSENCE sensors: 0x%8.8X", sensors); + + /* Start ABSENCE detection */ + ret = _uam_pm_start_detection(UAM_DETECT_ABSENCE, sensors); + if (UAM_ERROR_NONE != ret) + UAM_ERR("Failed with error: %s (0x%4.4X)", + _uam_manager_error_to_str(ret), ret); + +done: + FUNC_EXIT; + return TRUE; +} + +static int __uam_core_start_detection(int detection_type, + const char *svc_name, char *sender, unsigned int sensors) +{ + FUNC_ENTRY; uam_monitor_info_t *monitor; + uam_db_service_info_t *service; + gboolean is_monitor_added = TRUE; + GSList *l; retv_if(NULL == sender, UAM_ERROR_INVALID_PARAM); + retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAM); + + l = g_slist_find_custom(services, svc_name, __compare_svc_name); + retv_if(NULL == l, UAM_ERROR_INVALID_PARAM); + service = l->data; - monitor = __uam_find_monitor(sender, detection_type); + monitor = __uam_find_monitor(monitors, sender, svc_name, detection_type); if (!monitor) { monitor = g_malloc0(sizeof(uam_monitor_info_t)); monitor->name = g_strdup(sender); monitor->mode = detection_type; + monitor->service = service; + is_monitor_added = FALSE; } - UAM_DBG("Name: %s, Mode: %d", monitor->name, monitor->mode); + UAM_DBG("Name: %s, Service: %s, Mode: %d", monitor->name, svc_name, monitor->mode); - /* Find sensors which are already monitoring */ - active_sensors = _uam_core_get_active_sensors(detection_type); - UAM_DBG("sensors: 0x%8.8X, active_sensors: 0x%8.8X", sensors, active_sensors); - - inactive_sensors = (sensors & ~active_sensors); - /* - * If all requested sensors are already active, just update - * monitor info for current application and return success. - */ - if (0 == inactive_sensors) { - UAM_DBG("All sensors are already monitoring"); - goto done; + if (0 == detection_timer) { + __start_detection(NULL); + UAM_INFO("Monitor started detection, start timer"); + detection_timer = g_timeout_add_seconds( + UAM_DETECTION_CYCLE_MIN, __start_detection, NULL); } - /* Start monitoring only for sensors which aren't already active */ - ret = _uam_pm_start_detection(detection_type, inactive_sensors); - if (UAM_ERROR_NONE != ret) { - UAM_ERR("Failed with error: %s (0x%4.4X)", - _uam_manager_error_to_str(ret), ret); - g_free(monitor->name); - g_free(monitor); - return ret; - } - -done: - monitors = g_slist_append(monitors, monitor); monitor->sensors |= sensors; + if (!is_monitor_added) { + monitors = g_slist_append(monitors, monitor); + service->monitors = g_slist_append(service->monitors, monitor); + } FUNC_EXIT; return UAM_ERROR_NONE; } -static int __uam_core_stop_detection(int detection_type, char *sender, unsigned int sensors) +static int __uam_core_stop_detection(int detection_type, + const char *svc_name, char *sender, unsigned int sensors) { FUNC_ENTRY; int ret = UAM_ERROR_NONE; unsigned int remaining_sensors; unsigned int active_sensors; uam_monitor_info_t *monitor; + uam_db_service_info_t *service; retv_if(NULL == sender, UAM_ERROR_INVALID_PARAM); + retv_if(NULL == svc_name, UAM_ERROR_INVALID_PARAM); - monitor = __uam_find_monitor(sender, detection_type); + monitor = __uam_find_monitor(monitors, sender, svc_name, detection_type); retv_if(NULL == monitor, UAM_ERROR_NOT_IN_OPERATION); + service = monitor->service; + retv_if(0 != g_strcmp0(service->name, svc_name), UAM_ERROR_NOT_IN_OPERATION); /* Find sensors which are already monitoring */ active_sensors = _uam_core_get_active_sensors(detection_type); @@ -1558,6 +1619,16 @@ static int __uam_core_stop_detection(int detection_type, char *sender, unsigned * remove its monitor info from list. */ monitors = g_slist_remove(monitors, monitor); + if ((NULL == monitors) && (0 != detection_timer)) { + UAM_INFO("All monitors stopped detection, stop timer"); + g_source_remove(detection_timer); + detection_timer = 0; + } + + service->monitors = g_slist_remove(service->monitors, monitor); + if (NULL == service->monitors) + service->remaining_time = 0; + g_free(monitor->name); g_free(monitor); } @@ -1582,45 +1653,45 @@ static int __uam_core_stop_detection(int detection_type, char *sender, unsigned return ret; } -int _uam_core_start_presence_detection(char *sender, unsigned int sensors) +int _uam_core_start_presence_detection(const char *svc_name, char *sender, unsigned int sensors) { FUNC_ENTRY; int ret; - ret = __uam_core_start_detection(UAM_DETECT_PRESENCE, sender, sensors); + ret = __uam_core_start_detection(UAM_DETECT_PRESENCE, svc_name, sender, sensors); FUNC_EXIT; return ret; } -int _uam_core_stop_presence_detection(char *sender, unsigned int sensors) +int _uam_core_stop_presence_detection(const char *svc_name, char *sender, unsigned int sensors) { FUNC_ENTRY; int ret; - ret = __uam_core_stop_detection(UAM_DETECT_PRESENCE, sender, sensors); + ret = __uam_core_stop_detection(UAM_DETECT_PRESENCE, svc_name, sender, sensors); FUNC_EXIT; return ret; } -int _uam_core_start_absence_detection(char *sender, unsigned int sensors) +int _uam_core_start_absence_detection(const char *svc_name, char *sender, unsigned int sensors) { FUNC_ENTRY; int ret; - ret = __uam_core_start_detection(UAM_DETECT_ABSENCE, sender, sensors); + ret = __uam_core_start_detection(UAM_DETECT_ABSENCE, svc_name, sender, sensors); FUNC_EXIT; return ret; } -int _uam_core_stop_absence_detection(char *sender, unsigned int sensors) +int _uam_core_stop_absence_detection(const char *svc_name, char *sender, unsigned int sensors) { FUNC_ENTRY; int ret; - ret = __uam_core_stop_detection(UAM_DETECT_ABSENCE, sender, sensors); + ret = __uam_core_stop_detection(UAM_DETECT_ABSENCE, svc_name, sender, sensors); FUNC_EXIT; return ret; @@ -2161,11 +2232,20 @@ void _uam_core_handle_absence_detected(unsigned int sensor, void _uam_core_cleanup_monitor(char *name) { - if (__uam_find_monitor(name, UAM_DETECT_PRESENCE)) - __uam_core_stop_detection(UAM_DETECT_PRESENCE, name, UAM_SENSOR_ALL); + GSList *l; - if (__uam_find_monitor(name, UAM_DETECT_ABSENCE)) - __uam_core_stop_detection(UAM_DETECT_ABSENCE, name, UAM_SENSOR_ALL); + for (l = monitors; NULL != l; l = g_slist_next(l)) { + uam_monitor_info_t *monitor = l->data; + + if (!monitor || !monitor->name || !monitor->service) + continue; + + if (0 != g_strcmp0(name, monitor->name)) + continue; + + __uam_core_stop_detection(monitor->mode, + monitor->service->name, name, monitor->sensors); + } } static void __free_user_info(gpointer data) diff --git a/ua-daemon/src/ua-manager-request-handler.c b/ua-daemon/src/ua-manager-request-handler.c index b2f11a1..3847113 100644 --- a/ua-daemon/src/ua-manager-request-handler.c +++ b/ua-daemon/src/ua-manager-request-handler.c @@ -327,38 +327,46 @@ static int __uam_manager_sync_request_handler( } case UAM_REQUEST_START_PRESENCE_DETECTION: { unsigned int sensors; + const char *svc_name; __uam_manager_copy_params(in_param1, &sensors, sizeof(unsigned int)); + svc_name = (char *)g_variant_get_data(in_param2); - result = _uam_core_start_presence_detection(sender, sensors); + result = _uam_core_start_presence_detection(svc_name, sender, sensors); break; } case UAM_REQUEST_STOP_PRESENCE_DETECTION: { unsigned int sensors; + const char *svc_name; __uam_manager_copy_params(in_param1, &sensors, sizeof(unsigned int)); + svc_name = (char *)g_variant_get_data(in_param2); - result = _uam_core_stop_presence_detection(sender, sensors); + result = _uam_core_stop_presence_detection(svc_name, sender, sensors); break; } case UAM_REQUEST_START_ABSENCE_DETECTION: { unsigned int sensors; + const char *svc_name; __uam_manager_copy_params(in_param1, &sensors, sizeof(unsigned int)); + svc_name = (char *)g_variant_get_data(in_param2); - result = _uam_core_start_absence_detection(sender, sensors); + result = _uam_core_start_absence_detection(svc_name, sender, sensors); break; } case UAM_REQUEST_STOP_ABSENCE_DETECTION: { unsigned int sensors; + const char *svc_name; __uam_manager_copy_params(in_param1, &sensors, sizeof(unsigned int)); + svc_name = (char *)g_variant_get_data(in_param2); - result = _uam_core_stop_absence_detection(sender, sensors); + result = _uam_core_stop_absence_detection(svc_name, sender, sensors); break; } case UAM_REQUEST_SET_LOW_POWER_MODE: { @@ -431,53 +439,53 @@ static int __uam_manager_sync_request_handler( break; } case UAM_REQUEST_ADD_USER_TO_SERVICE: { - const char *service_name; + const char *svc_name; const char *account; - service_name = (char *)g_variant_get_data(in_param1); + svc_name = (char *)g_variant_get_data(in_param1); account = (char *)g_variant_get_data(in_param2); - UAM_DBG("Account: [%s], Service: [%s]", account, service_name); + UAM_DBG("Account: [%s], Service: [%s]", account, svc_name); - result = _uam_core_service_add_user(service_name, account); + result = _uam_core_service_add_user(svc_name, account); break; } case UAM_REQUEST_REMOVE_USER_FROM_SERVICE: { - const char *service_name; + const char *svc_name; const char *account; - service_name = (char *)g_variant_get_data(in_param1); + svc_name = (char *)g_variant_get_data(in_param1); account = (char *)g_variant_get_data(in_param2); - UAM_DBG("Account: [%s], Service: [%s]", account, service_name); + UAM_DBG("Account: [%s], Service: [%s]", account, svc_name); - result = _uam_core_service_remove_user(service_name, account); + result = _uam_core_service_remove_user(svc_name, account); break; } case UAM_REQUEST_ADD_DEVICE_TO_SERVICE: { - const char *service_name; + const char *svc_name; const char *device_id; int tech_type; - service_name = (char *)g_variant_get_data(in_param1); + svc_name = (char *)g_variant_get_data(in_param1); device_id = (char *)g_variant_get_data(in_param2); __uam_manager_copy_params(in_param3, &tech_type, sizeof(int)); UAM_DBG("Service: [%s] device_id: [%s], tech_type: %d", - service_name, device_id, tech_type); + svc_name, device_id, tech_type); - result = _uam_core_service_add_device(service_name, device_id, tech_type); + result = _uam_core_service_add_device(svc_name, device_id, tech_type); break; } case UAM_REQUEST_REMOVE_DEVICE_FROM_SERVICE: { - const char *service_name; + const char *svc_name; const char *device_id; int tech_type; - service_name = (char *)g_variant_get_data(in_param1); + svc_name = (char *)g_variant_get_data(in_param1); device_id = (char *)g_variant_get_data(in_param2); __uam_manager_copy_params(in_param3, &tech_type, sizeof(int)); UAM_DBG("Service: [%s] device_id: [%s], tech_type: %d", - service_name, device_id, tech_type); + svc_name, device_id, tech_type); - result = _uam_core_service_remove_device(service_name, device_id, tech_type); + result = _uam_core_service_remove_device(svc_name, device_id, tech_type); break; } case UAM_REQUEST_SET_SERVICE_DETECTION_CYCLE: { -- 2.7.4 From 8d10f8eab459c8057f4d22251a052a2c158ae11d Mon Sep 17 00:00:00 2001 From: Atul Rai Date: Tue, 11 Jun 2019 11:14:25 +0530 Subject: [PATCH 14/16] Auto add default service to each device This patch creates default service "ua.service.default" if not already present and adds it to the newly registered device's service list. Change-Id: Ied15fd3616ee87b636a9bec679094e5413c2b1ca Signed-off-by: Atul Rai --- include/ua-internal.h | 2 + ua-daemon/src/ua-manager-core.c | 99 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 89 insertions(+), 12 deletions(-) diff --git a/include/ua-internal.h b/include/ua-internal.h index 68829ca..054cfa2 100644 --- a/include/ua-internal.h +++ b/include/ua-internal.h @@ -93,6 +93,8 @@ typedef enum { #define UAM_SIGNAL_DEVICE_FOUND "DeviceFound" #define UAM_SIGNAL_SCAN_COMPLETED "ScanCompleted" +#define UAM_SERVICE_DEFAULT "ua.service.default" + #ifdef __cplusplus } #endif diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index c243c4d..9b02b61 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -513,9 +513,29 @@ unsigned int _uam_core_get_active_sensors(int detection_mode) return sensors; } +static GSList *__convert_db_svc_list_to_uam_svc_list(GSList *db_svc_list) +{ + FUNC_ENTRY; + GSList *l; + GSList *svc_list = NULL; + + retv_if(NULL == db_svc_list, NULL); + + for (l = db_svc_list; NULL != l; l = g_slist_next(l)) { + /* + * TODO: Iterate over the db_svc_list and add each service to the global + * service list "services" if its not already inserted. Also append this + * uam_db_service_info_t to svc_list. + * */ + } + + FUNC_EXIT; + return svc_list; +} + static void __uam_core_add_dev_to_list( uam_db_user_info_t *user, const uam_device_info_t *dev_info, - int presence_state, long long timestamp) + int presence_state, long long timestamp, GSList *svc_list) { FUNC_ENTRY; uam_db_tech_info_t *tech; @@ -1248,6 +1268,7 @@ int _uam_core_service_add_user(const char *svc_name, const char *account) service = g_new0(uam_db_service_info_t, 1); service->name = g_strdup(svc_name); service->cycle = UAM_DETECTION_CYCLE_DEFAULT; + services = g_slist_append(services, service); } else service = l->data; @@ -1271,7 +1292,12 @@ int _uam_core_service_add_user(const char *svc_name, const char *account) continue; tech->services = g_slist_append(tech->services, service); - /* TODO: Add service-device to DB */ + /* + * TODO: insert device service info to db + * if (UAM_ERROR_NONE != _uam_db_insert_device_service_info(device->device_id, + * tech->tech_type, ->mac, service->name, service->cycle)) + * UAM_WARN("Device service addition to persistent DB failed"); + */ } } @@ -1318,7 +1344,12 @@ int _uam_core_service_remove_user(const char *svc_name, const char *account) UAM_DBG("Service %s found, remove it from list", svc_name); tech->services = g_slist_remove(tech->services, l2->data); - /* TODO: Remove service-device from DB */ + /* + * TODO: Remove service-device from DB + * if (UAM_ERROR_NONE != _uam_db_delete_device_service_info( + * device->device_id, tech->tech_type, mac, svc_name)) + * UAM_WARN("Device service removal from persistent DB failed"); + */ } } @@ -1383,20 +1414,26 @@ int _uam_core_service_add_device(const char *svc_name, const char *device_id, in tech_info = __uam_core_get_dev_tech_info(device_id, tech_type); retv_if(NULL == tech_info, UAM_ERROR_INVALID_PARAM); + l = g_slist_find_custom(tech_info->services, svc_name, __compare_svc_name); + retv_if(NULL != l, UAM_ERROR_ALREADY_REGISTERED); + /* Retrieve service from list */ l = g_slist_find_custom(services, svc_name, __compare_svc_name); if (NULL == l) { service = g_new0(uam_db_service_info_t, 1); service->name = g_strdup(svc_name); service->cycle = UAM_DETECTION_CYCLE_DEFAULT; + services = g_slist_append(services, service); } else service = l->data; - l = g_slist_find_custom(tech_info->services, svc_name, __compare_svc_name); - retv_if(NULL != l, UAM_ERROR_ALREADY_REGISTERED); - tech_info->services = g_slist_append(tech_info->services, service); - /* TODO: Add service-device to DB */ + /* + * TODO: insert device service info to db + * if (UAM_ERROR_NONE != _uam_db_insert_device_service_info(device_id, + * tech_type, mac, service->name, service->cycle)) + * UAM_WARN("Device service addition to persistent DB failed"); + */ FUNC_EXIT; return UAM_ERROR_NONE; @@ -1424,7 +1461,12 @@ int _uam_core_service_remove_device(const char *svc_name, const char *device_id, retv_if(NULL == l, UAM_ERROR_NOT_REGISTERED); tech_info->services = g_slist_remove(tech_info->services, l->data); - /* TODO: Remove service-device from DB */ + /* + * TODO: Remove service-device from DB + * if (UAM_ERROR_NONE != _uam_db_delete_device_service_info( + * device_id, tech_type, mac, svc_name)) + * UAM_WARN("Device service removal from persistent DB failed"); + */ FUNC_EXIT; return UAM_ERROR_NONE; @@ -1452,7 +1494,11 @@ int _uam_core_set_service_detection_cycle(const char *svc_name, unsigned int new else service->remaining_time = new_cycle - elapsed_time; - /* TODO: Update service detection cycle in DB */ + /* + * TODO: Update service detection cycle in DB + * if (UAM_ERROR_NONE != _uam_db_update_service_cycle(svc_name, new_cycle)) + * UAM_WARN("Service cycle updation to persistent DB failed"); + */ FUNC_EXIT; return UAM_ERROR_NONE; @@ -1836,6 +1882,7 @@ int _uam_core_init(void) for (l = db_devices; NULL != l; l = g_slist_next(l)) { db_device_info_t *db_info = l->data; uam_db_user_info_t *user; + GSList *svc_list = NULL; GSList *l1; if (!db_info) @@ -1849,9 +1896,18 @@ int _uam_core_init(void) } user = l1->data; +#if 0 + /* TODO: Fetch device services from DB */ + l1 = _uam_db_get_device_services( + db_info->dev_info.device_id, + db_info->dev_info.type, + db_info->dev_info.mac); +#else + l1 = NULL; +#endif + svc_list = __convert_db_svc_list_to_uam_svc_list(l1); __uam_core_add_dev_to_list(user, &(db_info->dev_info), - db_info->presence_state, db_info->timestamp); - /* TODO: Fetch and update device services from DB */ + db_info->presence_state, db_info->timestamp, svc_list); } g_slist_free_full(db_devices, g_free); @@ -1955,6 +2011,7 @@ void _uam_core_handle_device_added(int status, } if (UAM_ERROR_NONE == status) { + GSList *svc_list; long long timestamp; if (0 > user_id) { @@ -1988,15 +2045,33 @@ void _uam_core_handle_device_added(int status, user = l->data; } + /* Get default service and add it to device's service list by default */ + svc_list = g_slist_find_custom(services, UAM_SERVICE_DEFAULT, __compare_svc_name); + if (NULL == svc_list) { + uam_db_service_info_t *service = g_new0(uam_db_service_info_t, 1); + service->name = g_strdup(UAM_SERVICE_DEFAULT); + service->cycle = UAM_DETECTION_CYCLE_DEFAULT; + services = g_slist_append(services, service); + svc_list = g_slist_append(svc_list, service); + } + timestamp = time(NULL); __uam_core_add_dev_to_list(user, dev_info, - UAM_PRESENCE_STATE_PRESENT, timestamp); + UAM_PRESENCE_STATE_PRESENT, timestamp, svc_list); /* Add device to database */ if (UAM_ERROR_NONE != _ua_device_db_insert_device_info(user->user_id, dev_info, UAM_PRESENCE_STATE_PRESENT, timestamp)) UAM_WARN("Device addition to persistent DB failed"); + /* + * TODO: insert device service info to db + * if (UAM_ERROR_NONE != _uam_db_insert_device_service_info( + * dev_info->device_id, dev_info->type, dev_info->mac, + * UAM_SERVICE_DEFAULT, UAM_DETECTION_CYCLE_DEFAULT)) + * UAM_WARN("Device service addition to persistent DB failed"); + */ + /* Send device added event to application */ __send_device_event(UAM_EVENT_DEVICE_ADDED, dev_info); -- 2.7.4 From e06ecd672ed2c268c3d15f3acb4444f318531ace Mon Sep 17 00:00:00 2001 From: Atul Rai Date: Tue, 11 Jun 2019 14:47:27 +0530 Subject: [PATCH 15/16] Modify PRESENCE/ANSENCE event handling This patch modifies user PRESENCE/ABSENCE event handling to incorporate service based event sending. Change-Id: I1e89373afdb99d1347b926262c76352d879dd1f6 Signed-off-by: Atul Rai --- ua-daemon/include/ua-manager-core.h | 1 + ua-daemon/src/ua-manager-core.c | 193 ++++++++++++++++++++++++++++-------- 2 files changed, 150 insertions(+), 44 deletions(-) diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index a32d328..c57f13b 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -44,6 +44,7 @@ typedef struct { unsigned int cycle; unsigned int remaining_time; GSList *monitors; + GSList *dev_techs; } uam_db_service_info_t; typedef struct { diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index 9b02b61..d9f0ee0 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -184,6 +184,34 @@ static void __send_device_event(int event, const uam_device_info_t *dev_info) FUNC_EXIT; } +static void __add_service_to_dev_tech_mapping( + uam_db_tech_info_t *tech, uam_db_service_info_t *service) +{ + FUNC_ENTRY; + + ret_if(NULL == tech); + ret_if(NULL == service); + + tech->services = g_slist_append(tech->services, service); + service->dev_techs = g_slist_append(service->dev_techs, tech); + + FUNC_EXIT; +} + +static void __remove_service_to_dev_tech_mapping( + uam_db_tech_info_t *tech, uam_db_service_info_t *service) +{ + FUNC_ENTRY; + + ret_if(NULL == tech); + ret_if(NULL == service); + + tech->services = g_slist_remove(tech->services, service); + service->dev_techs = g_slist_remove(service->dev_techs, tech); + + FUNC_EXIT; +} + static void __remove_user_device(gpointer data) { FUNC_ENTRY; @@ -579,6 +607,16 @@ static void __uam_core_add_dev_to_list( tech->timestamp = timestamp; tech->device = device; + tech->services = svc_list; + for (l = svc_list; NULL != l; l = g_slist_next(l)) { + uam_db_service_info_t *service = l->data; + + if (!service) + continue; + + service->dev_techs = g_slist_prepend(service->dev_techs, tech); + } + /* Add tech info to tech list */ device->tech_list = g_slist_append(device->tech_list, tech); device->supported_techs |= tech->tech_type; @@ -1291,7 +1329,8 @@ int _uam_core_service_add_user(const char *svc_name, const char *account) if (NULL != l2) continue; - tech->services = g_slist_append(tech->services, service); + __add_service_to_dev_tech_mapping(tech, service); + /* * TODO: insert device service info to db * if (UAM_ERROR_NONE != _uam_db_insert_device_service_info(device->device_id, @@ -1343,7 +1382,9 @@ int _uam_core_service_remove_user(const char *svc_name, const char *account) continue; UAM_DBG("Service %s found, remove it from list", svc_name); - tech->services = g_slist_remove(tech->services, l2->data); + __remove_service_to_dev_tech_mapping( + tech, (uam_db_service_info_t *)l2->data); + /* * TODO: Remove service-device from DB * if (UAM_ERROR_NONE != _uam_db_delete_device_service_info( @@ -1427,7 +1468,8 @@ int _uam_core_service_add_device(const char *svc_name, const char *device_id, in } else service = l->data; - tech_info->services = g_slist_append(tech_info->services, service); + __add_service_to_dev_tech_mapping(tech_info, service); + /* * TODO: insert device service info to db * if (UAM_ERROR_NONE != _uam_db_insert_device_service_info(device_id, @@ -1460,7 +1502,8 @@ int _uam_core_service_remove_device(const char *svc_name, const char *device_id, l = g_slist_find_custom(tech_info->services, svc_name, __compare_svc_name); retv_if(NULL == l, UAM_ERROR_NOT_REGISTERED); - tech_info->services = g_slist_remove(tech_info->services, l->data); + __remove_service_to_dev_tech_mapping(tech_info, (uam_db_service_info_t *)l->data); + /* * TODO: Remove service-device from DB * if (UAM_ERROR_NONE != _uam_db_delete_device_service_info( @@ -2089,6 +2132,48 @@ void _uam_core_handle_device_added(int status, FUNC_EXIT; } +void __send_user_presence_event(uam_db_tech_info_t *tech, unsigned int sensor) +{ + FUNC_ENTRY; + uam_db_user_info_t *user; + GSList *l; + + ret_if(NULL == tech); + ret_if(NULL == tech->services); + + user = tech->device->user; + for (l = tech->services; NULL != l; l = g_slist_next(l)) { + uam_db_service_info_t *svc = l->data; + GSList *l1; + + if (!svc || !svc->monitors) + continue; + + for (l1 = svc->monitors; NULL != l1; l1 = g_slist_next(l1)) { + uam_monitor_info_t *mon = l1->data; + + if (!mon) + continue; + + if (!(mon->sensors & sensor)) + continue; + + if (UAM_DETECT_PRESENCE != mon->mode) + continue; + + _uam_manager_send_event(mon->name, + UAM_EVENT_USER_PRESENCE_DETECTED, + g_variant_new("(uss)", sensor, + user->account, svc->name)); + UAM_DBG("Sent UAM_EVENT_USER_PRESENCE_DETECTED to %s" + " for 0x%8.8X, User: %s Service: %s", + mon->name, sensor, user->account, svc->name); + } + } + + FUNC_EXIT; +} + void _uam_core_handle_presence_detected(unsigned int sensor, int user_id, uam_device_info_t *dev_info) { @@ -2189,55 +2274,86 @@ void _uam_core_handle_presence_detected(unsigned int sensor, dev_info->type, dev_info->mac, tech->presence_state)) UAM_WARN("_ua_device_db_update_device_presence failed"); - _uam_manager_send_event(NULL, UAM_EVENT_USER_PRESENCE_DETECTED, - g_variant_new("(us)", sensor, user->account)); - UAM_DBG("Sent UAM_EVENT_USER_PRESENCE_DETECTED for 0x%8.8X, User: %s", - sensor, user->account); + __send_user_presence_event(tech, sensor); FUNC_EXIT; } -static void __send_user_absence_event(uam_db_user_info_t *user, - uam_tech_type_e type, unsigned int sensor) +static void __send_user_absence_event(uam_tech_type_e type, unsigned int sensor) { FUNC_ENTRY; - gboolean is_present = FALSE; GSList *l; /* - * If none of the user's devices which support the tech_type, is PRESENT, - * send user ABSENCE event (User absent for tech_type/sensor). + * For each service, find users absent on given sensor. Then for each + * monitor in serivce's monitor list, if it is monitoring ABSENCE on + * given sensor, send user ABSENCE event. */ - for (l = user->devices; NULL != l; l = g_slist_next(l)) { - uam_db_device_info_t *device = l->data; + for (l = services; NULL != l; l = g_slist_next(l)) { + uam_db_service_info_t *svc = l->data; + GSList *absent_users = NULL; + GSList *present_users = NULL; GSList *l1; + GSList *l2; - if (!device) - continue; - - if (!(device->supported_techs & type)) + if (!svc || !svc->monitors || !svc->dev_techs) continue; - for (l1 = device->tech_list; NULL != l1; l1 = g_slist_next(l1)) { - uam_db_tech_info_t *tech = l1->data; + for (l1 = svc->dev_techs; NULL != l1; l1 = g_slist_next(l1)) { + uam_db_tech_info_t *tech = l->data; - if (!tech || tech->tech_type != type) + if (!tech || (tech->tech_type != type)) continue; + l2 = g_slist_find_custom(present_users, + &(tech->device->user->user_id), __compare_user_id); if (UAM_PRESENCE_STATE_PRESENT == tech->presence_state) { - is_present = TRUE; - break; + /* Remove user from absent list */ + absent_users = g_slist_remove(absent_users, tech->device->user); + + /* If user not in present list, add user to the list */ + if (!l2) + present_users = g_slist_prepend(present_users, tech->device->user); + } else { + /* If user not in the present list then only add it to absent list */ + if (!l2) + absent_users = g_slist_prepend(absent_users, tech->device->user); } } - if (is_present) - break; - } + g_slist_free(present_users); + if (!absent_users) + continue; + + for (l2 = svc->monitors; NULL != l2; l2 = g_slist_next(l2)) { + uam_monitor_info_t *mon = l2->data; + + if (!mon) + continue; + + if (!(mon->sensors & sensor)) + continue; + + if (UAM_DETECT_ABSENCE != mon->mode) + continue; + + for (l1 = absent_users; NULL != l1; l1 = g_slist_next(l1)) { + uam_db_user_info_t *user = l1->data; + + if (!user) + continue; - if (!is_present) { - _uam_manager_send_event(NULL, UAM_EVENT_USER_ABSENCE_DETECTED, - g_variant_new("(us)", sensor, user->account)); - UAM_DBG("Sent UAM_EVENT_USER_ABSENCE_DETECTED for 0x%8.8X", sensor); + _uam_manager_send_event(mon->name, + UAM_EVENT_USER_PRESENCE_DETECTED, + g_variant_new("(uss)", sensor, + user->account, svc->name)); + UAM_DBG("Sent UAM_EVENT_USER_PRESENCE_DETECTED to %s" + " for 0x%8.8X, User: %s Service: %s", + mon->name, sensor, user->account, svc->name); + } + } + + g_slist_free(absent_users); } FUNC_EXIT; @@ -2300,8 +2416,6 @@ void _uam_core_handle_absence_detected(unsigned int sensor, dev_info->type, dev_info->mac, tech->presence_state)) UAM_WARN("_ua_device_db_update_device_presence failed"); - //__send_user_absence_event(user, dev_info->type, sensor); - FUNC_EXIT; } @@ -2400,17 +2514,8 @@ void _uam_core_handle_detection_stopped(unsigned int sensor) else if (UAM_SENSOR_BITMASK_WIFI == sensor) type = UAM_TECH_TYPE_WIFI; - if (UAM_TECH_TYPE_NONE != type) { - GSList *l; - - for (l = users; NULL != l; l = g_slist_next(l)) { - uam_db_user_info_t *user = l->data; - if (!user) - continue; - - __send_user_absence_event(user, type, sensor); - } - } + if (UAM_TECH_TYPE_NONE != type) + __send_user_absence_event(type, sensor); if (0 == detecting_sensors) /* Send detection stopped event */ -- 2.7.4 From 70c38e08ed72ea8035870cee9c874f8694889cd0 Mon Sep 17 00:00:00 2001 From: Abhay agarwal Date: Tue, 11 Jun 2019 19:17:06 +0530 Subject: [PATCH 16/16] Added service info into UA DB and provide access APIs Change-Id: I2da914a42bf24985d22c5b6dce5e968b51f993f0 Signed-off-by: Abhay agarwal --- ua-daemon/CMakeLists.txt | 2 + ua-daemon/data/ua_db.sql | 20 +- ua-daemon/include/ua-manager-core.h | 2 +- ua-daemon/include/ua-manager-database.h | 55 ++- ua-daemon/src/ua-manager-core.c | 37 +- ua-daemon/src/ua-manager-device-db.c | 37 ++ ua-daemon/src/ua-manager-device-service-db.c | 598 +++++++++++++++++++++++++++ ua-daemon/src/ua-manager-service-db.c | 535 ++++++++++++++++++++++++ ua-daemon/src/ua-manager-user-db.c | 40 +- 9 files changed, 1280 insertions(+), 46 deletions(-) create mode 100644 ua-daemon/src/ua-manager-device-service-db.c create mode 100644 ua-daemon/src/ua-manager-service-db.c diff --git a/ua-daemon/CMakeLists.txt b/ua-daemon/CMakeLists.txt index bb623bb..05a434b 100644 --- a/ua-daemon/CMakeLists.txt +++ b/ua-daemon/CMakeLists.txt @@ -12,6 +12,8 @@ SET(SRCS src/ua-manager-core.c src/ua-manager-device-db.c src/ua-manager-user-db.c + src/ua-manager-service-db.c + src/ua-manager-device-service-db.c src/ua-manager-config-parser.c src/pm/ua-plugin-manager.c src/pm/ua-pm-util.c diff --git a/ua-daemon/data/ua_db.sql b/ua-daemon/data/ua_db.sql index 7cbcd17..42c3778 100644 --- a/ua-daemon/data/ua_db.sql +++ b/ua-daemon/data/ua_db.sql @@ -5,11 +5,12 @@ PRAGMA foreign_keys = ON; CREATE TABLE IF NOT EXISTS userdata ( name TEXT, - user_id INTEGER PRIMARY KEY, + user_id INTEGER PRIMARY KEY AUTOINCREMENT, account TEXT ); CREATE TABLE IF NOT EXISTS devices ( + device_number INTEGER PRIMARY KEY AUTOINCREMENT, device_id TEXT, user_id INTEGER, tech_type INTEGER, @@ -18,6 +19,19 @@ CREATE TABLE IF NOT EXISTS devices ( timestamp LONG, presence_state INTEGER, os_type INTEGER, - FOREIGN KEY(user_id) REFERENCES userdata(user_id), - PRIMARY KEY(device_id, tech_type, address) + FOREIGN KEY(user_id) REFERENCES userdata(user_id) +); + +CREATE TABLE IF NOT EXISTS services ( + service_number INTEGER PRIMARY KEY AUTOINCREMENT, + service_name TEXT, + cycle INTEGER +); + +CREATE TABLE IF NOT EXISTS device_services ( + device_number INTEGER, + service_number INTEGER, + FOREIGN KEY(device_number) REFERENCES devices(device_number), + FOREIGN KEY(service_number) REFERENCES services(service_number), + PRIMARY KEY(device_number, service_number) ); diff --git a/ua-daemon/include/ua-manager-core.h b/ua-daemon/include/ua-manager-core.h index c57f13b..feafcbe 100644 --- a/ua-daemon/include/ua-manager-core.h +++ b/ua-daemon/include/ua-manager-core.h @@ -72,7 +72,7 @@ typedef struct uam_db_device_info { } uam_db_device_info_t; typedef struct uam_db_user_info { - unsigned short user_id; + int user_id; char *name; char *account; GSList *devices; diff --git a/ua-daemon/include/ua-manager-database.h b/ua-daemon/include/ua-manager-database.h index 15ee05f..ba2ddde 100644 --- a/ua-daemon/include/ua-manager-database.h +++ b/ua-daemon/include/ua-manager-database.h @@ -47,7 +47,7 @@ int _ua_db_delete_by_user_id(int user_id); int _ua_db_clear(void); /* insert */ -int _ua_db_insert_user_info(int user_id, const char *name, const char *account); +int _ua_db_insert_user_info(int* user_id, const char *name, const char *account); /* DEVICE QUERIES */ typedef struct { @@ -65,6 +65,7 @@ int _ua_device_db_deinitialize(void); GSList *_ua_device_db_get_all_devices(void); int _ua_device_db_get_device(char *device_id, int tech_type, char *address, db_device_info_t *info); +int _uam_db_get_device_number(const char *device_id, int tech_type, const char *address, int *device_number); /* delete operations */ int _ua_device_db_clear(void); @@ -82,6 +83,58 @@ int _ua_device_db_update_device_presence(char *device_id, int tech_type, char *a int _ua_device_db_update_device_ip_address(char *device_id, int tech_type, char *address, char *ip_address); +/* SERVICE QUERIES */ + +#define UA_MAX_SERVICE_LEN 100 + +typedef struct { + int service_number; + char service_name[UA_MAX_SERVICE_LEN]; + int cycle; +} db_service_info_t; + +/* db init/deinit */ +int _ua_service_db_initialize(void); +int _ua_service_db_deinitialize(void); + +/* select operations */ +int _uam_db_get_service_info(const char *service_name, db_service_info_t *info); +GSList *_ua_service_db_get_all_services(void); + +/* delete operations */ +int _uam_db_delete_service_info(const char *service_name); +int _ua_service_db_clear(void); + +/* insert */ +int _uam_db_insert_service_info(int *service_number, const char *service_name, int cycle); + +/* update */ +int _uam_db_update_service_cycle(const char *service_name, int cycle); + + +/* DEVICE_SERVICE QUERIES */ + +/* db init/deinit */ +int _ua_device_service_db_initialize(void); +int _ua_device_service_db_deinitialize(void); + +/* select operations */ +GSList *_uam_db_get_service(int device_number); +GSList *_uam_db_get_device_services(const char *device_id, int tech_type, const char *address); + +/* delete operations */ +int _ua_device_service_db_clear(void); +int _uam_db_delete_service_number(int service_number); +int _uam_db_delete_device_service_number(int device_number, int service_number); +int _ua_device_service_db_delete_by_service(const char *service_name); +int _uam_db_delete_device_service_info(const char *device_id, int tech_type, + const char *address, const char *service_name); + +/* insert */ +int _uam_db_insert_service(int device_number, int service_number); +int _uam_db_insert_device_service_info(const char *device_id, int tech_type, + const char *address, const char *service_name, int cycle); + #ifdef __cplusplus } diff --git a/ua-daemon/src/ua-manager-core.c b/ua-daemon/src/ua-manager-core.c index d9f0ee0..fdad64a 100644 --- a/ua-daemon/src/ua-manager-core.c +++ b/ua-daemon/src/ua-manager-core.c @@ -54,36 +54,8 @@ GSList *scanners; /* List of scanning apps - uam_scanner_info_t */ static guint detection_timer = 0; -static gboolean user_id_list[UAM_MAX_USERS]; -static int next_user_id = 1; static unsigned int detecting_sensors = 0; -static int __uam_core_reserve_user_id(void) -{ - FUNC_ENTRY; - int indx = next_user_id; - - while (user_id_list[indx]) { - indx = (indx + 1) % UAM_MAX_USERS; - - if (indx == next_user_id) - return -1; - } - - user_id_list[indx] = TRUE; - next_user_id = indx; - - FUNC_EXIT; - return next_user_id; -} - -static void __uam_core_release_user_id(int id) -{ - FUNC_ENTRY; - user_id_list[id] = FALSE; - FUNC_EXIT; -} - static gint __compare_user_account(gconstpointer data, gconstpointer user_data) { const uam_db_user_info_t *user = data; @@ -294,7 +266,6 @@ static void __free_uam_db_user_info(gpointer data) /* Set/update registered device list to cloud plugin */ _uam_cloud_update_registered_devices(); - __uam_core_release_user_id(user->user_id); g_free(user->name); g_free(user->account); g_free(user); @@ -686,15 +657,18 @@ int _uam_core_add_user(const char *account, const char *name) retv_if((NULL != l) && (l->data != NULL), UAM_ERROR_ALREADY_REGISTERED); user = g_new0(uam_db_user_info_t, 1); + + #if 0 user->user_id = __uam_core_reserve_user_id(); if (USHRT_MAX - 1 < user->user_id) { UAM_ERR("Max user limit reached"); g_free(user); return UAM_ERROR_INTERNAL; } + #endif /* Add user to database */ - if (UAM_ERROR_NONE != _ua_db_insert_user_info(user->user_id, name, account)) { + if (UAM_ERROR_NONE != _ua_db_insert_user_info(&(user->user_id), name, account)) { UAM_ERR("_ua_db_insert_user_info failed"); g_free(user); return UAM_ERROR_DB_FAILED; @@ -1907,7 +1881,6 @@ int _uam_core_init(void) user = g_new0(uam_db_user_info_t, 1); user->user_id = info->user_id; - user_id_list[user->user_id] = TRUE; user->name = g_strdup(info->name); user->account = g_strdup(info->account); user->devices = NULL; @@ -1986,7 +1959,6 @@ void _uam_core_deinit(void) if (!user) continue; - __uam_core_release_user_id(user->user_id); g_free(user->name); g_free(user->account); g_free(user); @@ -2444,7 +2416,6 @@ static void __free_user_info(gpointer data) ret_if(NULL == user); - __uam_core_release_user_id(user->user_id); g_free(user->name); g_free(user->account); g_free(user); diff --git a/ua-daemon/src/ua-manager-device-db.c b/ua-daemon/src/ua-manager-device-db.c index 241c9db..5767f0c 100644 --- a/ua-daemon/src/ua-manager-device-db.c +++ b/ua-daemon/src/ua-manager-device-db.c @@ -30,6 +30,8 @@ #define SELECT_DEVICE "SELECT device_id, tech_type, address, " \ "ip_address, timestamp, presence_state, os_type, " \ "user_id FROM devices WHERE device_id = ? AND tech_type = ? AND address = ?" +#define SELECT_DEVICE_NUMBER "SELECT device_number FROM devices " \ + "WHERE device_id = ? AND tech_type = ? AND address = ?" #define INSERT_DEVICE "insert into devices " \ "(device_id, tech_type, address, ip_address, timestamp, " \ @@ -135,6 +137,7 @@ static sqlite3_stmt *delete_device_info; /* SELECT statements */ static sqlite3_stmt *select_all_devices; static sqlite3_stmt *select_device; +static sqlite3_stmt *select_device_number; /* UPDATE statements */ static sqlite3_stmt *update_timestamp; @@ -230,6 +233,7 @@ static int __ua_device_prepare_select(sqlite3 *db) PREPARE_SELECT(select_all_devices, SELECT_ALL_DEVICES); PREPARE_SELECT(select_device, SELECT_DEVICE); + PREPARE_SELECT(select_device_number, SELECT_DEVICE_NUMBER); initialized = 1; FUNC_EXIT; @@ -312,6 +316,7 @@ static void __ua_device_finalize_select(void) FINALIZE(select_all_devices); FINALIZE(select_device); + FINALIZE(select_device_number); FUNC_EXIT; } @@ -576,6 +581,38 @@ handle_error: return error_code; } +int _uam_db_get_device_number(const char *device_id, int tech_type, const char *address, int *device_number) +{ + FUNC_ENTRY; + int error_code = UAM_ERROR_NONE; + sqlite3_stmt *stmt = select_device_number; + + retv_if(NULL == device_id, UAM_ERROR_INVALID_PARAM); + retv_if(NULL == address, UAM_ERROR_INVALID_PARAM); + + UAM_INFO("UserId: %s", device_id); + + DB_ACTION(sqlite3_bind_text(stmt, 1, device_id, -1, SQLITE_STATIC)); + DB_ACTION(sqlite3_bind_int(stmt, 2, tech_type)); + DB_ACTION(sqlite3_bind_text(stmt, 3, address, -1, SQLITE_STATIC)); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + UAM_ERR("Failed to select device info %s\n", + sqlite3_errmsg(__ua_device_db_get_database())); + error_code = UAM_ERROR_DB_FAILED; + } else { + UAM_DBG("Device number info found"); + *device_number = sqlite3_column_int(stmt, 0); + + UAM_INFO("%d", *device_number); + } + +handle_error: + sqlite3_reset(stmt); + FUNC_EXIT; + return error_code; +} + GSList *_ua_device_db_get_all_devices(void) { FUNC_ENTRY; diff --git a/ua-daemon/src/ua-manager-device-service-db.c b/ua-daemon/src/ua-manager-device-service-db.c new file mode 100644 index 0000000..fc8ee8a --- /dev/null +++ b/ua-daemon/src/ua-manager-device-service-db.c @@ -0,0 +1,598 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License") + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include +#include + +#include "ua-manager-common.h" +#include "ua-manager-database.h" + +#define SELECT_SERVICE "SELECT service_number, service_name, cycle" \ + "FROM device_services JOIN services where service_number = ?" + +#define INSERT_DEVICE_SERVICE "insert into device_services " \ + "(device_number, service_number, values (?, ?)" + +#define UPDATE "" + +#define DELETE_ALL_DEVICE_SERVICES "delete from devices " +#define DELETE_DEVICE_SERVICE "delete from device_services " \ + "WHERE device_number = ? and service_number = ?" +#define DELETE_SERVICE "delete from device_services " \ + "WHERE service_number = ?" + +#ifndef DATABASE_FULL_PATH +#define DATABASE_FULL_PATH "/opt/usr/dbspace/.ua-manager-data.db" +#endif + +#define SQLITE_BUSY_TIMEOUT 500000 + +static void __ua_device_service_finalize_delete(void); + +#define EXEC(error_code, command) do { \ + if (error_code != command) { \ + FUNC_EXIT; \ + goto handle_error; \ + } \ +} while (0) + +#define PREPARE_DELETE(stm, query) do { \ + rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \ + if (rc != SQLITE_OK) { \ + stm = NULL; \ + __ua_device_service_finalize_delete(); \ + UAM_ERR("Failed to prepare \"%s\" query" \ + , query); \ + return rc; \ + } \ +} while (0) + +static void __ua_device_service_finalize_select(void); + +#define PREPARE_SELECT(stm, query) do { \ + rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \ + if (rc != SQLITE_OK) { \ + stm = NULL; \ + __ua_device_service_finalize_select(); \ + UAM_ERR("Failed to prepare \"%s\" query" \ + , query); \ + return rc; \ + } \ +} while (0) + +static void __ua_device_service_finalize_update(void); + +#define PREPARE_UPDATE(stm, query) do { \ + rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \ + if (rc != SQLITE_OK) { \ + stm = NULL; \ + __ua_device_service_finalize_update(); \ + UAM_ERR("Failed to prepare \"%s\" query" \ + , query); \ + return rc; \ + } \ +} while (0) + +static void __ua_device_service_finalize_insert(void); + +#define PREPARE_INSERT(stm, query) do { \ + rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \ + if (rc != SQLITE_OK) { \ + stm = NULL; \ + __ua_device_service_finalize_insert(); \ + UAM_ERR("Failed to prepare \"%s\" query" \ + , query); \ + return rc; \ + } \ +} while (0) + +#define FINALIZE(stm) do { \ + if (stm) { \ + sqlite3_finalize(stm); \ + stm = NULL; \ + } \ +} while (0) + +#define DB_ACTION(command) do { \ + if ((command) != SQLITE_OK) { \ + error_code = UAM_ERROR_DB_FAILED; \ + FUNC_EXIT; \ + goto handle_error; \ + } \ +} while (0) + +/* DELETE statements */ +static sqlite3_stmt *delete_all_device_services; +static sqlite3_stmt *delete_service; +static sqlite3_stmt *delete_device_service; + +/* SELECT statements */ +static sqlite3_stmt *select_service; +static sqlite3_stmt *select_service_info; +static sqlite3_stmt *select_device_info; + +/* UPDATE statements */ +//TO-DO + +/* INSERT statements */ +static sqlite3_stmt *insert_device_service; + +static sqlite3 *database; + +static int __ua_device_service_db_busy(void *user, int attempts) +{ + FUNC_ENTRY; + UAM_ERR("DB locked by another process, attempts number %d", + attempts); + + usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/ + FUNC_EXIT; + return 1; +} + + +static int __ua_device_service_db_initialize_once(void) +{ + FUNC_ENTRY; + int res = 0; + + if (database != NULL) { + FUNC_EXIT; + return UAM_ERROR_NONE; + } + + res = sqlite3_open(DATABASE_FULL_PATH, &database); + + if (res != SQLITE_OK) { + // TODO : Can add logic of retry if not able to open + UAM_ERR("Error in opening database %s: %s\n", DATABASE_FULL_PATH, sqlite3_errmsg(database)); + return UAM_ERROR_DB_FAILED; + } + + UAM_DBG("Successfully opened database"); + + res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0); + + if (res != SQLITE_OK) { + UAM_ERR("Can't set locking mode %s, skip set busy handler.", + sqlite3_errmsg(database)); + sqlite3_close(database); + database = NULL; + FUNC_EXIT; + return UAM_ERROR_DB_FAILED; + } + + /* Set how many times we'll repeat our attempts for sqlite_step */ + if (sqlite3_busy_handler(database, __ua_device_service_db_busy, NULL) + != SQLITE_OK) + UAM_ERR("Couldn't set busy handler!"); + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + +static int __ua_device_service_prepare_delete(sqlite3 *db) +{ + FUNC_ENTRY; + int rc; + static int initialized; + + if (initialized) { + FUNC_EXIT; + return SQLITE_OK; + } + + PREPARE_DELETE(delete_all_device_services, DELETE_ALL_DEVICE_SERVICES); + PREPARE_DELETE(delete_service, DELETE_SERVICE); + PREPARE_DELETE(delete_device_service, DELETE_DEVICE_SERVICE); + + initialized = 1; + FUNC_EXIT; + return rc; +} + +static int __ua_device_service_prepare_select(sqlite3 *db) +{ + FUNC_ENTRY; + int rc; + static int initialized; + + if (initialized) { + FUNC_EXIT; + return SQLITE_OK; + } + + PREPARE_SELECT(select_service, SELECT_SERVICE); + //PREPARE_SELECT(select_service_info, SELECT_SERVICE_INFO); + + initialized = 1; + FUNC_EXIT; + return rc; +} + +static int __ua_device_service_prepare_update(sqlite3 *db) +{ + FUNC_ENTRY; +// TO-DO + FUNC_EXIT; + return 0; +} + +static int __ua_device_service_prepare_insert(sqlite3 *db) +{ + FUNC_ENTRY; + int rc; + static int initialized; + + if (initialized) { + FUNC_EXIT; + return SQLITE_OK; + } + + PREPARE_INSERT(insert_device_service, INSERT_DEVICE_SERVICE); + + initialized = 1; + FUNC_EXIT; + return rc; +} + +static int __ua_device_service_table_deviceservicesinfo_prepare(sqlite3 *db) +{ + FUNC_ENTRY; + + int error_code = UAM_ERROR_NONE; + + if (db == NULL) { + FUNC_EXIT; + return UAM_ERROR_DB_FAILED; + } + + DB_ACTION(__ua_device_service_prepare_delete(db)); + DB_ACTION(__ua_device_service_prepare_select(db)); + DB_ACTION(__ua_device_service_prepare_update(db)); + DB_ACTION(__ua_device_service_prepare_insert(db)); + +handle_error: + + FUNC_EXIT; + return error_code; +} + +static void __ua_device_service_finalize_delete(void) +{ + FUNC_ENTRY; + + FINALIZE(delete_all_device_services); + FINALIZE(delete_service); + FINALIZE(delete_device_service); + + FUNC_EXIT; +} + +static void __ua_device_service_finalize_select(void) +{ + FUNC_ENTRY; + + FINALIZE(select_service); + FINALIZE(select_service_info); + FINALIZE(select_device_info); + + FUNC_EXIT; +} + +static void __ua_device_service_finalize_insert(void) +{ + FUNC_ENTRY; + + FINALIZE(insert_device_service); + + FUNC_EXIT; +} + +static void __ua_device_service_finalize_update(void) +{ + FUNC_ENTRY; + +// TO-DO + + FUNC_EXIT; +} + +static void __ua_device_service_table_devicesinfo_finalize(void) +{ + FUNC_ENTRY; + __ua_device_service_finalize_delete(); + __ua_device_service_finalize_select(); + __ua_device_service_finalize_insert(); + __ua_device_service_finalize_update(); + FUNC_EXIT; +} + +int _ua_device_service_db_deinitialize(void) +{ + FUNC_ENTRY; + + retv_if(NULL == database, UAM_ERROR_NONE); + + __ua_device_service_table_devicesinfo_finalize(); + sqlite3_close(database); + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + +int _ua_device_service_db_initialize(void) +{ + FUNC_ENTRY; + database = NULL; + + __ua_device_service_db_initialize_once(); + + EXEC(UAM_ERROR_NONE, __ua_device_service_table_deviceservicesinfo_prepare(database)); + + FUNC_EXIT; + return UAM_ERROR_NONE; + +handle_error: + _ua_device_service_db_deinitialize(); + FUNC_EXIT; + return UAM_ERROR_DB_FAILED; +} + +static sqlite3 *__ua_device_service_db_get_database(void) +{ + if (database == NULL) + __ua_device_service_db_initialize_once(); + + return database; +} + +int _uam_db_insert_service(int device_number, int service_number) +{ + FUNC_ENTRY; + int error_code = UAM_ERROR_NONE; + sqlite3_stmt *stmt = insert_device_service; + + UAM_INFO("%d-%d", device_number, service_number); + + DB_ACTION(sqlite3_bind_int(stmt, 1, device_number)); + DB_ACTION(sqlite3_bind_int(stmt, 2, service_number)); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + UAM_ERR("Failed to insert device-service info: %s\n", + sqlite3_errmsg(__ua_device_service_db_get_database())); + error_code = UAM_ERROR_DB_FAILED; + goto handle_error; + } + + UAM_DBG("Device-service info inserted [%d] [%d]", device_number, service_number); + +handle_error: + sqlite3_reset(stmt); + FUNC_EXIT; + return error_code; +} + +int _uam_db_insert_device_service_info(const char *device_id, int tech_type, + const char *address, const char *service_name, int cycle) +{ + FUNC_ENTRY; + int error_code = UAM_ERROR_NONE; + UAM_INFO("%s-%d-%s-%s-%d", device_id, tech_type, address, service_name, cycle); + + int device_number = 0; + if (_uam_db_get_device_number(device_id, tech_type, address, &device_number) != UAM_ERROR_NONE) + goto handle_error; + + db_service_info_t *info = NULL; + int service_number = 0; + if (_uam_db_get_service_info(service_name, info) != UAM_ERROR_NONE) { + if (_uam_db_insert_service_info(&service_number, service_name, cycle) != UAM_ERROR_NONE) + goto handle_error; + } else + service_number = info->service_number; + + UAM_INFO("%d-%d", device_number, service_number); + + if (_uam_db_insert_service(device_number, service_number) != UAM_ERROR_NONE) + goto handle_error; + + UAM_DBG("Device-service info inserted [%d] [%d]", device_number, service_number); + +handle_error: + FUNC_EXIT; + return error_code; +} + +int _ua_device_service_db_clear(void) +{ + int error_code = UAM_ERROR_NONE; + sqlite3_stmt *stmt = delete_all_device_services; + + if (sqlite3_step(stmt) != SQLITE_DONE) { + UAM_ERR("Failed to delete device data %s\n", + sqlite3_errmsg(__ua_device_service_db_get_database())); + error_code = UAM_ERROR_DB_FAILED; + goto handle_error; + } + + UAM_DBG("User data deleted "); + +handle_error: + + sqlite3_reset(stmt); + return error_code; +} + +int _uam_db_delete_service_number(int service_number) +{ + FUNC_ENTRY; + int error_code = UAM_ERROR_NONE; + sqlite3_stmt *stmt = delete_service; + + UAM_INFO("Service Number: %d", service_number); + + DB_ACTION(sqlite3_bind_int(stmt, 1, service_number)); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + UAM_ERR("Failed to delete device-service info %s\n", + sqlite3_errmsg(__ua_device_service_db_get_database())); + error_code = UAM_ERROR_DB_FAILED; + } else + UAM_DBG("Device-service info deleted"); + +handle_error: + sqlite3_reset(stmt); + FUNC_EXIT; + return error_code; +} + +int _uam_db_delete_device_service_number(int device_number, int service_number) +{ + FUNC_ENTRY; + int error_code = UAM_ERROR_NONE; + sqlite3_stmt *stmt = delete_device_service; + + UAM_INFO("Device Number: %d Service Number: %d", device_number, service_number); + + DB_ACTION(sqlite3_bind_int(stmt, 1, device_number)); + DB_ACTION(sqlite3_bind_int(stmt, 2, service_number)); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + UAM_ERR("Failed to delete device-service info %s\n", + sqlite3_errmsg(__ua_device_service_db_get_database())); + error_code = UAM_ERROR_DB_FAILED; + } else + UAM_DBG("Device-service info deleted"); + +handle_error: + sqlite3_reset(stmt); + FUNC_EXIT; + return error_code; +} + +int _ua_device_service_db_delete_by_service(const char *service_name) +{ + FUNC_ENTRY; + int error_code = UAM_ERROR_NONE; + UAM_INFO("%s", service_name); + + db_service_info_t *info = NULL; + int service_number = 0; + if (_uam_db_get_service_info(service_name, info) != UAM_ERROR_NONE) + goto handle_error; + service_number = info->service_number; + + UAM_INFO("%d", service_number); + + if (_uam_db_delete_service_number(service_number) != UAM_ERROR_NONE) + goto handle_error; + + UAM_DBG("Service info deleted [%d]", service_number); + +handle_error: + FUNC_EXIT; + return error_code; +} + +int _uam_db_delete_device_service_info(const char *device_id, int tech_type, + const char *address, const char *service_name) +{ + FUNC_ENTRY; + int error_code = UAM_ERROR_NONE; + UAM_INFO("%s-%d-%s-%s", device_id, tech_type, address, service_name); + + int device_number = 0; + if (_uam_db_get_device_number(device_id, tech_type, address, &device_number) != UAM_ERROR_NONE) + goto handle_error; + + db_service_info_t *info = NULL; + int service_number = 0; + if (_uam_db_get_service_info(service_name, info) != UAM_ERROR_NONE) + goto handle_error; + service_number = info->service_number; + + UAM_INFO("%d-%d", device_number, service_number); + + if (_uam_db_delete_device_service_number(device_number, service_number) != UAM_ERROR_NONE) + goto handle_error; + + UAM_DBG("Device-service info deleted [%d] [%d]", device_number, service_number); + +handle_error: + FUNC_EXIT; + return error_code; +} + +GSList *_uam_db_get_service(int device_number) +{ + FUNC_ENTRY; + int rc; + sqlite3_stmt *stmt = select_service; + GSList *service_list = NULL; + db_service_info_t *info = NULL; + + if (sqlite3_bind_int(stmt, 1, device_number) != SQLITE_OK) + goto handle_error; + + do { + rc = sqlite3_step(stmt); + + switch (rc) { + case SQLITE_DONE: + break; + case SQLITE_ROW: + info = g_new0(db_service_info_t, 1); + info->service_number = sqlite3_column_int(stmt, 0); + g_strlcpy(info->service_name, (char *)sqlite3_column_text(stmt, 1), UA_MAX_SERVICE_LEN); + info->cycle = sqlite3_column_int(stmt, 2); + + UAM_INFO("%d-%s-%d", info->service_number, info->service_name, info->cycle); + service_list = g_slist_append(service_list, info); + break; + case SQLITE_ERROR: + default: + UAM_ERR("Failed to enumerate device info: %s\n", + sqlite3_errmsg(__ua_device_service_db_get_database())); + } + } while (rc == SQLITE_ROW); + +handle_error: + sqlite3_reset(stmt); + FUNC_EXIT; + return service_list; +} + +GSList *_uam_db_get_device_services(const char *device_id, int tech_type, const char *address) +{ + FUNC_ENTRY; + UAM_INFO("%s-%d-%s", device_id, tech_type, address); + + GSList *service_list = NULL; + int device_number = 0; + if (_uam_db_get_device_number(device_id, tech_type, address, &device_number) != UAM_ERROR_NONE) + goto handle_error; + + service_list = _uam_db_get_service(device_number); + +handle_error: + FUNC_EXIT; + return service_list; +} diff --git a/ua-daemon/src/ua-manager-service-db.c b/ua-daemon/src/ua-manager-service-db.c new file mode 100644 index 0000000..1180abc --- /dev/null +++ b/ua-daemon/src/ua-manager-service-db.c @@ -0,0 +1,535 @@ +/* + * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License") + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include +#include +#include +#include +#include + +#include "ua-manager-common.h" +#include "ua-manager-database.h" + +#define SELECT_ALL_SERVICES "SELECT service_number, service_name, " \ + "cycle FROM services" + +#define SELECT_SERVICE "SELECT service_number, service_name, cycle " \ + "FROM services where service_name = ?" + +#define INSERT_SERVICE "insert into services (service_name, cycle) values (?, ?)" + +#define UPDATE_CYCLE "UPDATE services SET cycle = ? WHERE service_name = ?" + +#define DELETE_ALL_SERVICES "delete from services" + +#define DELETE_SERVICE_INFO "delete from services WHERE service_name = ?" + +#ifndef DATABASE_FULL_PATH +#define DATABASE_FULL_PATH "/opt/usr/dbspace/.ua-manager-data.db" +#endif + +#define SQLITE_BUSY_TIMEOUT 500000 + +static void __ua_service_finalize_delete(void); + +#define EXEC(error_code, command) do { \ + if (error_code != command) { \ + FUNC_EXIT; \ + goto handle_error; \ + } \ +} while (0) + +#define PREPARE_DELETE(stm, query) do { \ + rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \ + if (rc != SQLITE_OK) { \ + stm = NULL; \ + __ua_service_finalize_delete(); \ + UAM_ERR("Failed to prepare \"%s\" query" \ + , query); \ + return rc; \ + } \ +} while (0) + +static void __ua_service_finalize_select(void); + +#define PREPARE_SELECT(stm, query) do { \ + rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \ + if (rc != SQLITE_OK) { \ + stm = NULL; \ + __ua_service_finalize_select(); \ + UAM_ERR("Failed to prepare \"%s\" query" \ + , query); \ + return rc; \ + } \ +} while (0) + +static void _ua_service_finalize_update(void); + +#define PREPARE_UPDATE(stm, query) do { \ + rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \ + if (rc != SQLITE_OK) { \ + stm = NULL; \ + _ua_service_finalize_update(); \ + UAM_ERR("Failed to prepare \"%s\" query" \ + , query); \ + return rc; \ + } \ +} while (0) + +static void __ua_service_finalize_insert(void); + +#define PREPARE_INSERT(stm, query) do { \ + rc = sqlite3_prepare_v2(db, query, -1, &stm, NULL); \ + if (rc != SQLITE_OK) { \ + stm = NULL; \ + __ua_service_finalize_insert(); \ + UAM_ERR("Failed to prepare \"%s\" query" \ + , query); \ + return rc; \ + } \ +} while (0) + +#define FINALIZE(stm) do { \ + if (stm) { \ + sqlite3_finalize(stm); \ + stm = NULL; \ + } \ +} while (0) + +#define DB_ACTION(command) do { \ + if ((command) != SQLITE_OK) { \ + error_code = UAM_ERROR_DB_FAILED; \ + FUNC_EXIT; \ + goto handle_error; \ + } \ +} while (0) + +/* DELETE statements */ +static sqlite3_stmt *delete_all_services; +static sqlite3_stmt *delete_service_info; + +/* SELECT statements */ +static sqlite3_stmt *select_all_services; +static sqlite3_stmt *select_service; + +/* UPDATE statements */ +static sqlite3_stmt *update_cycle; + +/* INSERT statements */ +static sqlite3_stmt *insert_service_info; + +static sqlite3 *database; + +static int __ua_service_db_busy(void *user, int attempts) +{ + FUNC_ENTRY; + UAM_ERR("DB locked by another process, attempts number %d", + attempts); + + usleep(SQLITE_BUSY_TIMEOUT); /* wait for a half second*/ + FUNC_EXIT; + return 1; +} + + +static int __ua_service_db_initialize_once(void) +{ + FUNC_ENTRY; + int res = 0; + + if (database != NULL) { + FUNC_EXIT; + return UAM_ERROR_NONE; + } + + res = sqlite3_open(DATABASE_FULL_PATH, &database); + + if (res != SQLITE_OK) { + // TODO : Can add logic of retry if not able to open + UAM_ERR("Error in opening database %s: %s\n", DATABASE_FULL_PATH, sqlite3_errmsg(database)); + return UAM_ERROR_DB_FAILED; + } + + UAM_DBG("Successfully opened database"); + + res = sqlite3_exec(database, "PRAGMA locking_mode = NORMAL", 0, 0, 0); + + if (res != SQLITE_OK) { + UAM_ERR("Can't set locking mode %s, skip set busy handler.", + sqlite3_errmsg(database)); + sqlite3_close(database); + database = NULL; + FUNC_EXIT; + return UAM_ERROR_DB_FAILED; + } + + /* Set how many times we'll repeat our attempts for sqlite_step */ + if (sqlite3_busy_handler(database, __ua_service_db_busy, NULL) + != SQLITE_OK) + UAM_ERR("Couldn't set busy handler!"); + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + +static int __ua_service_prepare_delete(sqlite3 *db) +{ + FUNC_ENTRY; + int rc; + static int initialized; + + if (initialized) { + FUNC_EXIT; + return SQLITE_OK; + } + + PREPARE_DELETE(delete_all_services, DELETE_ALL_SERVICES); + PREPARE_DELETE(delete_service_info, DELETE_SERVICE_INFO); + + initialized = 1; + FUNC_EXIT; + return rc; +} + +static int __ua_service_prepare_select(sqlite3 *db) +{ + FUNC_ENTRY; + int rc; + static int initialized; + + if (initialized) { + FUNC_EXIT; + return SQLITE_OK; + } + + PREPARE_SELECT(select_all_services, SELECT_ALL_SERVICES); + PREPARE_SELECT(select_service, SELECT_SERVICE); + + initialized = 1; + FUNC_EXIT; + return rc; +} + +static int __ua_service_prepare_update(sqlite3 *db) +{ + FUNC_ENTRY; + int rc; + static int initialized; + + if (initialized) { + FUNC_EXIT; + return SQLITE_OK; + } + + PREPARE_UPDATE(update_cycle, UPDATE_CYCLE); + + initialized = 1; + FUNC_EXIT; + return rc; +} + +static int __ua_service_prepare_insert(sqlite3 *db) +{ + FUNC_ENTRY; + int rc; + static int initialized; + + if (initialized) { + FUNC_EXIT; + return SQLITE_OK; + } + + PREPARE_INSERT(insert_service_info, INSERT_SERVICE); + + initialized = 1; + FUNC_EXIT; + return rc; +} + +static int __ua_service_table_servicesinfo_prepare(sqlite3 *db) +{ + FUNC_ENTRY; + + int error_code = UAM_ERROR_NONE; + + if (db == NULL) { + FUNC_EXIT; + return UAM_ERROR_DB_FAILED; + } + + DB_ACTION(__ua_service_prepare_delete(db)); + DB_ACTION(__ua_service_prepare_select(db)); + DB_ACTION(__ua_service_prepare_update(db)); + DB_ACTION(__ua_service_prepare_insert(db)); + +handle_error: + + FUNC_EXIT; + return error_code; +} + +static void __ua_service_finalize_delete(void) +{ + FUNC_ENTRY; + + FINALIZE(delete_all_services); + FINALIZE(delete_service_info); + + FUNC_EXIT; +} + +static void __ua_service_finalize_select(void) +{ + FUNC_ENTRY; + + FINALIZE(select_all_services); + FINALIZE(select_service); + + FUNC_EXIT; +} + +static void __ua_service_finalize_insert(void) +{ + FUNC_ENTRY; + + FINALIZE(insert_service_info); + + FUNC_EXIT; +} + +static void _ua_service_finalize_update(void) +{ + FUNC_ENTRY; + + FINALIZE(update_cycle); + + FUNC_EXIT; +} + +static void __ua_service_table_servicesinfo_finalize(void) +{ + FUNC_ENTRY; + __ua_service_finalize_delete(); + __ua_service_finalize_select(); + __ua_service_finalize_insert(); + FUNC_EXIT; +} + +int _ua_service_db_deinitialize(void) +{ + FUNC_ENTRY; + + retv_if(NULL == database, UAM_ERROR_NONE); + + __ua_service_table_servicesinfo_finalize(); + sqlite3_close(database); + + FUNC_EXIT; + return UAM_ERROR_NONE; +} + +int _ua_service_db_initialize(void) +{ + FUNC_ENTRY; + database = NULL; + + __ua_service_db_initialize_once(); + + EXEC(UAM_ERROR_NONE, __ua_service_table_servicesinfo_prepare(database)); + + FUNC_EXIT; + return UAM_ERROR_NONE; + +handle_error: + _ua_service_db_deinitialize(); + FUNC_EXIT; + return UAM_ERROR_DB_FAILED; +} + +static sqlite3 *__ua_service_db_get_database(void) +{ + if (database == NULL) + __ua_service_db_initialize_once(); + + return database; +} + +int _uam_db_insert_service_info(int *service_number, const char *service_name, int cycle) +{ + FUNC_ENTRY; + int error_code = UAM_ERROR_NONE; + sqlite3_stmt *stmt = insert_service_info; + + retv_if(NULL == service_name, UAM_ERROR_INVALID_PARAM); + + UAM_INFO("%s-%d", service_name, cycle); + + DB_ACTION(sqlite3_bind_text(stmt, 1, service_name, -1, SQLITE_TRANSIENT)); + DB_ACTION(sqlite3_bind_int(stmt, 2, cycle)); + + *service_number = sqlite3_last_insert_rowid(database); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + UAM_ERR("Failed to insert service info: %s\n", + sqlite3_errmsg(__ua_service_db_get_database())); + error_code = UAM_ERROR_DB_FAILED; + goto handle_error; + } + + UAM_DBG("Service info inserted service_number:[%d]", *service_number); + +handle_error: + sqlite3_reset(stmt); + FUNC_EXIT; + return error_code; +} + +int _uam_db_delete_service_info(const char *service_name) +{ + FUNC_ENTRY; + int error_code = UAM_ERROR_NONE; + sqlite3_stmt *stmt = delete_service_info; + + UAM_INFO("Service name: %s", service_name); + + DB_ACTION(sqlite3_bind_text(stmt, 1, service_name, -1, SQLITE_STATIC)); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + UAM_ERR("Failed to delete service info %s\n", + sqlite3_errmsg(__ua_service_db_get_database())); + error_code = UAM_ERROR_DB_FAILED; + } else + UAM_DBG("Service info deleted"); + +handle_error: + sqlite3_reset(stmt); + FUNC_EXIT; + return error_code; +} + +int _ua_service_db_clear(void) +{ + int error_code = UAM_ERROR_NONE; + sqlite3_stmt *stmt = delete_all_services; + + if (sqlite3_step(stmt) != SQLITE_DONE) { + UAM_ERR("Failed to delete service data %s\n", + sqlite3_errmsg(__ua_service_db_get_database())); + error_code = UAM_ERROR_DB_FAILED; + goto handle_error; + } + + UAM_DBG("Service data deleted "); + +handle_error: + + sqlite3_reset(stmt); + return error_code; +} + +int _uam_db_update_service_cycle(const char *service_name, int cycle) +{ + int error_code = UAM_ERROR_NONE; + sqlite3_stmt *stmt = update_cycle; + + retv_if(NULL == service_name, UAM_ERROR_INVALID_PARAM); + + DB_ACTION(sqlite3_bind_int64(stmt, 1, cycle)); + DB_ACTION(sqlite3_bind_text(stmt, 2, service_name, -1, SQLITE_TRANSIENT)); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + UAM_ERR("Failed to update service timestamp: %s\n", + sqlite3_errmsg(__ua_service_db_get_database())); + + error_code = UAM_ERROR_DB_FAILED; + goto handle_error; + } + + UAM_DBG("Service cycle updated [%d]", cycle); + +handle_error: + sqlite3_reset(stmt); + return error_code; +} + +int _uam_db_get_service_info(const char *service_name, db_service_info_t *info) +{ + FUNC_ENTRY; + int error_code = UAM_ERROR_NONE; + sqlite3_stmt *stmt = select_service; + + retv_if(!info, UAM_ERROR_INVALID_PARAM); + + UAM_INFO("Service Name: %s", service_name); + + DB_ACTION(sqlite3_bind_text(stmt, 1, service_name, -1, SQLITE_STATIC)); + + if (sqlite3_step(stmt) != SQLITE_DONE) { + UAM_ERR("Failed to select service info %s\n", + sqlite3_errmsg(__ua_service_db_get_database())); + error_code = UAM_ERROR_DB_FAILED; + } else { + UAM_DBG("Service info found"); + info = g_new0(db_service_info_t, 1); + info->service_number = sqlite3_column_int(stmt, 0); + g_strlcpy(info->service_name, (char *)sqlite3_column_text(stmt, 1), UA_MAX_SERVICE_LEN); + info->cycle = sqlite3_column_int(stmt, 2); + + UAM_INFO("%d-%s-%d", info->service_number, info->service_name, info->cycle); + } + +handle_error: + sqlite3_reset(stmt); + FUNC_EXIT; + return error_code; +} + +GSList *_ua_service_db_get_all_services(void) +{ + FUNC_ENTRY; + int rc; + sqlite3_stmt *stmt = select_all_services; + GSList *service_list = NULL; + db_service_info_t *info = NULL; + + do { + rc = sqlite3_step(stmt); + + switch (rc) { + case SQLITE_DONE: + break; + case SQLITE_ROW: + info = g_new0(db_service_info_t, 1); + info->service_number = sqlite3_column_int(stmt, 0); + g_strlcpy(info->service_name, (char *)sqlite3_column_text(stmt, 1), UA_MAX_SERVICE_LEN); + info->cycle = sqlite3_column_int(stmt, 2); + + UAM_INFO("%d-%s-%d", info->service_number, info->service_name, info->cycle); + + service_list = g_slist_append(service_list, info); + break; + case SQLITE_ERROR: + default: + UAM_ERR("Failed to enumerate service info: %s\n", + sqlite3_errmsg(__ua_service_db_get_database())); + } + } while (rc == SQLITE_ROW); + + sqlite3_reset(stmt); + FUNC_EXIT; + return service_list; +} diff --git a/ua-daemon/src/ua-manager-user-db.c b/ua-daemon/src/ua-manager-user-db.c index 2c4ee9d..d2b5512 100644 --- a/ua-daemon/src/ua-manager-user-db.c +++ b/ua-daemon/src/ua-manager-user-db.c @@ -31,8 +31,8 @@ "name, account FROM userdata WHERE user_id = ?" #define INSERT_USER_INFO "insert into userdata " \ - "(user_id, name, account)" \ - "values (?, ?, ?)" + "(name, account)" \ + "values (?, ?)" #define DELETE_ALL_USERS_DATA "delete from userdata " @@ -336,6 +336,10 @@ int _ua_db_initialize(void) if (_ua_device_db_initialize() != UAM_ERROR_NONE) goto handle_error; + if (_ua_service_db_initialize() != UAM_ERROR_NONE) + goto handle_error; + if (_ua_device_service_db_initialize() != UAM_ERROR_NONE) + goto handle_error; FUNC_EXIT; return UAM_ERROR_NONE; @@ -343,6 +347,8 @@ int _ua_db_initialize(void) handle_error: _ua_db_deinitialize(); _ua_device_db_deinitialize(); + _ua_service_db_deinitialize(); + _ua_device_service_db_deinitialize(); FUNC_EXIT; return UAM_ERROR_DB_FAILED; } @@ -357,6 +363,8 @@ int _ua_db_deinitialize(void) sqlite3_close(database); _ua_device_db_deinitialize(); + _ua_service_db_deinitialize(); + _ua_device_service_db_deinitialize(); FUNC_EXIT; return UAM_ERROR_NONE; @@ -370,7 +378,7 @@ static sqlite3 *__ua_db_get_database(void) return database; } -int _ua_db_insert_user_info(int user_id, const char *name, const char *account) +int _ua_db_insert_user_info(int *user_id, const char *name, const char *account) { FUNC_ENTRY; int error_code = UAM_ERROR_NONE; @@ -378,13 +386,13 @@ int _ua_db_insert_user_info(int user_id, const char *name, const char *account) retv_if(NULL == account, UAM_ERROR_INVALID_PARAM); - UAM_INFO("%d-%s-%s", user_id, name, account); + UAM_INFO("%s-%s", name, account); - DB_ACTION(sqlite3_bind_int(stmt, 1, user_id)); - DB_ACTION(sqlite3_bind_text(stmt, 2, name ? name : "", + DB_ACTION(sqlite3_bind_text(stmt, 1, name ? name : "", -1, SQLITE_TRANSIENT)); - DB_ACTION(sqlite3_bind_text(stmt, 3, account, -1, SQLITE_TRANSIENT)); + DB_ACTION(sqlite3_bind_text(stmt, 2, account, -1, SQLITE_TRANSIENT)); + *user_id = sqlite3_last_insert_rowid(database); if (sqlite3_step(stmt) != SQLITE_DONE) { UAM_ERR("Failed to insert user info: %s\n", sqlite3_errmsg(__ua_db_get_database())); @@ -392,7 +400,7 @@ int _ua_db_insert_user_info(int user_id, const char *name, const char *account) goto handle_error; } - UAM_DBG("User info inserted [%d]", user_id); + UAM_DBG("User info inserted [%d]", *user_id); handle_error: sqlite3_reset(stmt); @@ -518,6 +526,22 @@ int _ua_db_clear(void) UAM_DBG("Device data deleted "); + if (_ua_service_db_clear() != UAM_ERROR_NONE) { + UAM_ERR("Failed to delete servicee data \n"); + error_code = UAM_ERROR_DB_FAILED; + goto handle_error; + } + + UAM_DBG("Service data deleted "); + + if (_ua_device_service_db_clear() != UAM_ERROR_NONE) { + UAM_ERR("Failed to delete device-service data \n"); + error_code = UAM_ERROR_DB_FAILED; + goto handle_error; + } + + UAM_DBG("Device data deleted "); + handle_error: sqlite3_reset(stmt); -- 2.7.4