Added support for on_demand_scan 87/207487/1
authorAbhay agarwal <ay.agarwal@samsung.com>
Fri, 24 May 2019 11:28:51 +0000 (16:58 +0530)
committersaerome kim <saerome.kim@samsung.com>
Wed, 5 Jun 2019 08:40:32 +0000 (17:40 +0900)
Change-Id: I0f7c1fcceb920cbffabe864a1385c5b04612b1d0
Signed-off-by: Abhay agarwal <ay.agarwal@samsung.com>
13 files changed:
include/ua-api.h
include/ua-internal.h
ua-api/src/ua-api.c
ua-api/src/ua-event-handler.c
ua-daemon/include/ua-manager-core.h
ua-daemon/include/ua-plugin-manager.h
ua-daemon/src/pm/ua-plugin-manager.c
ua-daemon/src/pm/ua-pm-util.c
ua-daemon/src/pm/ua-pm-util.h
ua-daemon/src/pm/ua-wifi-plugin-handler.c
ua-daemon/src/ua-manager-core.c
ua-daemon/src/ua-manager-event-sender.c
ua-daemon/src/ua-manager-request-handler.c

index 2669382..cb85c46 100644 (file)
@@ -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,
index cd508a7..f472921 100644 (file)
@@ -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
 }
index d9b2621..4a7b354 100644 (file)
@@ -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;
+}
index 5f2bae7..bf7df72 100644 (file)
@@ -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);
        }
index 1ba8969..2d64e66 100644 (file)
@@ -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
index e66eeb0..b26d354 100644 (file)
@@ -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
index 3a13745..a338bb3 100644 (file)
@@ -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;
+}
index 58cf16e..08cc0fa 100644 (file)
@@ -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;
+       }
+}
index ec153df..7af480e 100644 (file)
@@ -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
index cb82201..2c95270 100644 (file)
@@ -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;
 }
 
index 7820f59..9236abf 100644 (file)
@@ -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;
+}
index a709653..2c064f6 100644 (file)
@@ -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;
index 538327c..8435c78 100644 (file)
@@ -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;