Add API to set location callback period 97/260397/2
authorAbhay Agarwal <ay.agarwal@samsung.com>
Fri, 21 May 2021 08:28:58 +0000 (13:58 +0530)
committerAbhay Agarwal <ay.agarwal@samsung.com>
Fri, 25 Jun 2021 12:03:12 +0000 (17:33 +0530)
Change-Id: I6bdfb7a2284188ba1b637e8c3adeccc1048ee710
Signed-off-by: Abhay Agarwal <ay.agarwal@samsung.com>
include/user-awareness-internal.h
include/user-awareness-private.h
include/user-awareness-util.h
src/user-awareness-monitors.c
src/user-awareness-util.c
tests/test/uat-scenarios.c

index dd4d6734a2d5b73a2c64448a031058a096f90ee1..2296f8d5de0fd4c17d6d044b9e53e3cca3536fd9 100755 (executable)
@@ -432,7 +432,25 @@ int ua_location_get_distance(ua_location_h location_handle, int *distance);
  * @retval #UA_ERROR_INVALID_PARAMETER Invalid parameter
  * @retval #UA_ERROR_NOT_INITIALIZED Not initialized yet
  */
-int ua_location_get_position(ua_location_h location_handle, int *x, int *y, int *z);
+int ua_location_get_position(ua_location_h location_handle,
+                                       int *x, int *y, int *z);
+
+/**
+ * @internal
+ * @ingroup CAPI_NETWORK_UA_MODULE
+ * @brief Sets the location period.
+ * @since_tizen 6.5
+ *
+ * @param[in] monitor handle
+ * @param[out] location period
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #UA_ERROR_NONE Successful
+ * @retval #UA_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #UA_ERROR_NOT_INITIALIZED Not initialized yet
+ */
+int ua_monitor_set_location_period(
+       ua_monitor_h monitor_handle, unsigned int location_period);
 
 /* Deprecated API's */
 
index 6bcc4862206f0de9f561f672741e6dbc3ed4f2d7..3a70f627705e3f46a7a865068bf1959dd7eb2866 100755 (executable)
@@ -203,6 +203,12 @@ do { \
  */
 #define UA_BLE_PAYLOAD_DEVICE_UID_MAX_LEN 17
 
+/**
+ * @brief Default location callback period (in msec).
+ * @since_tizen 6.5
+ */
+#define UA_LOCATION_PERIOD_DEFAULT 1000
+
 /**
  * @brief Wifi location sensor.
  * @since_tizen 6.5
@@ -299,6 +305,7 @@ typedef struct {
        unsigned int location_detected_bitmask; /**< Detected Location bitmask */
        gboolean location_detection_started; /**< Is location detection started */
        ua_location_detected_cb location_cb; /**< Location detection callback */
+       unsigned int location_period; /**< Location callback period */
 } ua_monitor_s;
 
 /**
@@ -320,6 +327,12 @@ typedef struct {
  * @since_tizen 6.5
  */
 typedef struct {
+       ua_monitor_h monitor_handle; /**< Monitor handle */
+       ua_service_h service_handle; /**< Service handle */
+       ua_monitor_h device_handle; /**< Device handle */
+       ua_monitor_h user_handle; /**< User handle */
+       ua_sensor_h sensor_handle; /**< Sensor handle */
+       ua_sensor_type_e bitmask; /**< Sensor bitmask */
        unsigned int distance; /**< Device distance in mm */
        unsigned int x; /**< Device x coordinate */
        unsigned int y; /**< Device y coordinate */
index 88b9871519e75e27ba658f2f71e6d70785486a36..04e695307fa0c264c6b7ab3511b00277879bdc3a 100755 (executable)
@@ -334,6 +334,39 @@ int _ua_free_sensor_info(ua_sensor_info_s *sensor_info);
  */
 void _ua_clear_sensor_list(void);
 
+/**
+ * @ingroup CAPI_NETWORK_UA_MODULE
+ * @brief Updates location info.
+ * @since_tizen 6.5
+ *
+ * @param[in] location_info The location information to be updated.
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #UA_ERROR_NONE Successful
+ * @retval #UA_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @exception
+ * @pre
+ * @post
+ *
+ */
+int _ua_update_location_info(ua_location_info_s *location_info);
+
+/**
+ * @ingroup CAPI_NETWORK_UA_MODULE
+ * @brief Sends location callback.
+ * @since_tizen 6.5
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #UA_ERROR_NONE Successful
+ *
+ * @exception
+ * @pre
+ * @post
+ *
+ */
+gboolean _ua_monitor_send_location_cb(gpointer data);
+
 #ifdef __cplusplus
 }
 #endif
index f74deaca0ade8d548876e57248b2fec32c949dad..a7d9d3f826bd060c55b1c28e4a4f548666abc725 100755 (executable)
@@ -28,6 +28,7 @@
 
 static GSList *ua_monitor_list;
 static ua_monitor_h scanning_monitor = NULL;
+static guint location_cb_timer = 0;
 
 static gboolean __ua_monitor_is_only_env_in_conditions(
                ua_monitor_s *monitor)
@@ -844,25 +845,33 @@ static void __ua_sensor_absence_detected(ua_monitor_s *monitor,
        FUNC_EXIT;
 }
 
-static void __ua_monitor_send_location_cb(ua_monitor_s *monitor,
-       ua_device_h device_handle, ua_sensor_info_s *sensor_info,
-               char* account, ua_location_info_s *location_info)
+static void __ua_sensor_location_detected(ua_monitor_s *monitor,
+               ua_sensor_info_s *sensor_info, char *account,
+               ua_location_info_s *location_info, char *device_id)
 {
        FUNC_ENTRY;
-       ua_sensor_type_e bitmask = sensor_info->bitmask;
+
+       int ret = UA_ERROR_NONE;
+       ua_device_h device_handle = NULL;
        ua_sensor_h sensor_handle;
        ua_service_info_s *service = NULL;
        ua_service_h service_handle = NULL;
        ua_service_h user_handle = NULL;
-       ua_location_h location_handle = NULL;
-       int ret;
+       ua_sensor_type_e bitmask = sensor_info->bitmask;
+
+       ret_if(NULL == monitor);
+
+       /* Get device handle */
+       ret = ua_device_get_by_device_id(device_id,
+               _ua_sensor_to_dev_type(bitmask), &device_handle);
+       UA_INFO("ua_device_get_by_device_id returned %s",
+                               _ua_get_error_string(ret));
 
        /* Get sensor handle */
        ret = _ua_sensor_get_by_sensor_info(sensor_info, &sensor_handle);
-       if (UA_ERROR_NONE != ret) {
+       if (UA_ERROR_NONE != ret)
                UA_INFO("_ua_sensor_get_by_sensor_info returned %s",
                                _ua_get_error_string(ret));
-       }
 
        /* Get service handle */
        service = __ua_get_service_from_list(monitor->service);
@@ -873,44 +882,20 @@ static void __ua_monitor_send_location_cb(ua_monitor_s *monitor,
        if (account)
                user_handle = _ua_get_user_handle_by_account(account);
 
-       /* Get location handle */
-       location_handle = (ua_location_h)location_info;
-
-       /* Send location callback */
-       monitor->location_cb(UA_ERROR_NONE, monitor,
-               service_handle, bitmask, sensor_handle,
-               device_handle, user_handle, location_handle,
-               monitor->user_data);
-
-       FUNC_EXIT;
-       return;
-}
-
-static void __ua_sensor_location_detected(ua_monitor_s *monitor,
-               ua_sensor_info_s *sensor_info, char *account,
-               ua_location_info_s *location_info, char *device_id)
-{
-       FUNC_ENTRY;
+       location_info->service_handle = service_handle;
+       location_info->bitmask = bitmask;
+       location_info->sensor_handle = sensor_handle;
+       location_info->device_handle = device_handle;
+       location_info->user_handle = user_handle;
+       location_info->monitor_handle = (ua_monitor_h *)monitor;
 
-       int ret = UA_ERROR_NONE;
-       ua_device_h device_handle = NULL;
-       ua_sensor_type_e bitmask = sensor_info->bitmask;
-
-       ret_if(NULL == monitor);
-
-       if (!monitor->location_detection_started) {
-               FUNC_EXIT;
-               return;
-       }
-
-       /* Get device */
-       ret = ua_device_get_by_device_id(device_id,
-               _ua_sensor_to_dev_type(bitmask), &device_handle);
-       UA_INFO("ua_device_get_by_device_id returned %s",
+       /* Update location */
+       ret = _ua_update_location_info(location_info);
+       if (UA_ERROR_NONE != ret) {
+               UA_INFO("_ua_update_location_info returned %s",
                                _ua_get_error_string(ret));
-
-       __ua_monitor_send_location_cb(monitor, device_handle,
-                               sensor_info, account, location_info);
+       }
+       UA_INFO("location updated");
 
        FUNC_EXIT;
 }
@@ -1266,6 +1251,7 @@ int ua_monitor_create(ua_monitor_h *handle)
        monitor->presence_bitmask_or = (available_sensors & UA_SENSOR_BLE) | (available_sensors & UA_SENSOR_WIFI);
        monitor->absence_bitmask_and = (available_sensors & UA_SENSOR_BLE) | (available_sensors & UA_SENSOR_WIFI);
        monitor->absence_bitmask_or = 0;
+       monitor->location_period = UA_LOCATION_PERIOD_DEFAULT;
 
        *handle = (ua_monitor_h)monitor;
 
@@ -1820,6 +1806,8 @@ int ua_monitor_start_location_detection(
                                _uam_get_available_sensors(&available_sensors)))
                UA_WARN("_uam_get_available_sensors failed");
 
+       UA_INFO("Starting location detection on %s service",
+                                               monitor->service);
        ret = __ua_start_monitoring(monitor->sensor_bitmask,
                                        monitor->service, detect);
        if (UA_ERROR_NONE != ret) {
@@ -1837,6 +1825,15 @@ int ua_monitor_start_location_detection(
 
        g_free(service);
 
+       /* Start timer */
+       if (0 == location_cb_timer) {
+               UA_INFO("Location detection started, start timer of %d seconds",
+                                               (monitor->location_period)/1000);
+               location_cb_timer = g_timeout_add_seconds(
+                       (monitor->location_period)/1000,
+                       _ua_monitor_send_location_cb, service_handle);
+       }
+
        FUNC_EXIT;
        return UA_ERROR_NONE;
 }
@@ -1969,6 +1966,9 @@ int ua_monitor_stop_location_detection(ua_monitor_h handle)
        monitor->location_detection_started = FALSE;
        monitor->location_detected_bitmask = 0;
 
+       if (location_cb_timer)
+               g_source_remove(location_cb_timer);
+
        FUNC_EXIT;
        return UA_ERROR_NONE;
 }
@@ -2014,6 +2014,26 @@ int ua_location_get_position(ua_location_h location_handle,
 
 }
 
+int ua_monitor_set_location_period(
+               ua_monitor_h handle,
+               unsigned int location_period)
+{
+       FUNC_ENTRY;
+       ua_monitor_s *monitor = (ua_monitor_s *)handle;
+
+       UA_CHECK_INIT_STATUS();
+       UA_VALIDATE_INPUT_PARAMETER(handle);
+       UA_VALIDATE_HANDLE(handle, ua_monitor_list);
+
+       monitor->location_period = location_period;
+       UA_INFO("monitor location period set to %d milliseconds",
+                               monitor->location_period);
+
+       FUNC_EXIT;
+       return UA_ERROR_NONE;
+}
+
+
 int ua_monitor_set_brightness_threshold(ua_monitor_h handle,
        int presence_threshold, int absence_threshold)
 {
index 8af37afa850d8349629f472fbf18e9c2bff0fba0..2f950d81b82f1c12172623bd1d371b795ea76348 100755 (executable)
@@ -24,6 +24,7 @@
 #include <user-awareness-private.h>
 
 static GSList *ua_sensors_list = NULL; /* ua_sensor_info_s */
+static GSList *ua_location_list = NULL; /* ua_location_info_s */
 
 static ua_feature_table_t feature_table[] = {
        {"tizen.org/feature/user_awareness", FALSE, FALSE}, /* UA_FEATURE_COMMON */
@@ -530,3 +531,115 @@ ua_location_info_s* _uam_to_ua_location_info(uam_device_info_s *info)
        return location_info;
 }
 
+static ua_location_info_s *__ua_get_location_info_from_list(
+                               ua_service_h svc_h, ua_device_h dev_h)
+{
+       GSList *l;
+       ua_location_info_s *location;
+
+       for (l = ua_location_list; NULL != l; l = g_slist_next(l)) {
+               location = (ua_location_info_s *)l->data;
+
+               if (svc_h == location->service_handle &&
+                               dev_h == location->device_handle) {
+                       UA_INFO("Location info found for service and device");
+                       return location;
+               }
+       }
+
+       return NULL;
+}
+
+static int __ua_update_location_info(
+               ua_location_info_s *location,
+               ua_location_info_s *info)
+{
+       FUNC_ENTRY;
+
+       UA_VALIDATE_INPUT_PARAMETER(location);
+       UA_VALIDATE_INPUT_PARAMETER(info);
+
+       location->distance = info->distance;
+       location->x = info->x;
+       location->y = info->y;
+       location->z = info->z;
+       location->bitmask = info->bitmask;
+       location->service_handle = info->service_handle;
+       location->device_handle = info->device_handle;
+       location->user_handle = info->user_handle;
+       location->monitor_handle = info->monitor_handle;
+       location->sensor_handle = info->sensor_handle;
+
+       FUNC_EXIT;
+       return UA_ERROR_NONE;
+}
+
+int _ua_update_location_info(
+               ua_location_info_s *info)
+{
+       FUNC_ENTRY;
+       int ret = UA_ERROR_NONE;
+       ua_location_info_s *location;
+
+       UA_VALIDATE_INPUT_PARAMETER(info);
+
+       location = __ua_get_location_info_from_list(
+               info->service_handle, info->device_handle);
+
+       if (location) {
+               ret = __ua_update_location_info(location, info);
+               goto done;
+       }
+
+       location = __ua_allocate_ua_location_info();
+       if (!location)
+               return UA_ERROR_OUT_OF_MEMORY;
+
+       ret = __ua_update_location_info(location, info);
+       ua_location_list = g_slist_append(ua_location_list, location);
+
+done:
+       FUNC_EXIT;
+       return ret;
+}
+
+static void __send_location_cb(gpointer data, gpointer user_data)
+{
+       FUNC_ENTRY;
+       ua_location_h location_handle = NULL;
+       ua_location_info_s *location_info = data;
+       ua_service_h service_handle = user_data;
+
+       ret_if(!location_info);
+       ret_if(!service_handle);
+
+       ua_monitor_s *monitor = (ua_monitor_s *)(location_info->monitor_handle);
+
+       if (!monitor || !(monitor->location_cb))
+               return;
+
+       if (!(location_info->service_handle == service_handle))
+               return;
+
+       /* Get location handle */
+       location_handle = (ua_location_h)location_info;
+
+       /* Send location callback */
+       monitor->location_cb(UA_ERROR_NONE, monitor,
+               location_info->service_handle, location_info->bitmask,
+               location_info->sensor_handle, location_info->device_handle,
+               location_info->user_handle, location_handle,
+               monitor->user_data);
+
+       return;
+}
+
+gboolean _ua_monitor_send_location_cb(gpointer data)
+{
+       FUNC_ENTRY;
+
+       g_slist_foreach(ua_location_list, __send_location_cb, data);
+
+       FUNC_EXIT;
+       return TRUE;
+}
index 23170d98e955eb94fdfe11bd3ced307542656d62..8397dfb73c86f5c85d82a6feab31007f5bbcce96 100755 (executable)
@@ -433,6 +433,10 @@ static int run_ua_monitor_start_location_detection(
 
        check_if(NULL == g_ua_mon_h);
 
+       ret = ua_monitor_set_location_period(g_ua_mon_h, 1000);
+       msg(" - ua_monitor_set_location_period() ret: [0x%X] [%s]",
+               ret, uat_get_error_str(ret));
+
        ret = ua_monitor_start_location_detection(g_ua_mon_h,
                        (g_service_str[0] == '\0' ? NULL : g_service_h),
                        _sensor_location_detected_cb, g_ua_mon_h);