Add functionality to report instant sensor status change
authorsaerome.kim <saerome.kim@samsung.com>
Mon, 21 Oct 2019 11:55:53 +0000 (20:55 +0900)
committersaerome.kim <saerome.kim@samsung.com>
Wed, 23 Oct 2019 00:32:01 +0000 (09:32 +0900)
- Problem: in ABSENCE case, even the motion sensor detects ABSENCE
ua-manager reports this ABSENCE state after the detection window complete.
- Cause: If ABSENCE case, the ABSENCE can be jurged only when the
detction window complete.
- Solution: Whenever the sensor status is changed, plugin reports status
changed event immediately so that the user can recognize ABSENCE event
takes place during uncompleted ABSENCE detection window.

Change-Id: I49254770cbab1c55aac7b9b893c5eec8d54139cf
Signed-off-by: saerome.kim <saerome.kim@samsung.com>
CMakeLists.txt
include/user-awareness-private.h
include/user-awareness.h
packaging/capi-network-ua.spec
src/user-awareness-event-handler.c
src/user-awareness-monitors.c
src/user-awareness-util.c
test/uat-init.c

index 7fbb441d784b9b8eea526a8ecd558bcb4647d024..4300258035a944ac92c6665bd1d4d98bd5d99b81 100644 (file)
@@ -18,14 +18,11 @@ IF (${SUSPEND_RESUME_TEST})
            deviced
            power-defs
     )
+    ADD_DEFINITIONS(-DSUSPEND_RESUME_TEST)
 ELSE (${SUSPEND_RESUME_TEST})
     pkg_check_modules (PKGS REQUIRED ${PKG_MODULES})
 ENDIF (${SUSPEND_RESUME_TEST})
 
-IF(${SUSPEND_RESUME_TEST})
-    ADD_DEFINITIONS(-DSUSPEND_RESUME_TEST)
-ENDIF(${SUPPORT_BLE_ADV})
-
 INCLUDE_DIRECTORIES(${PKGS_INCLUDE_DIRS})
 LINK_DIRECTORIES(${PKGS_LIBRARY_DIRS})
 
index d1ccf1820b5e0cdbc0b2639d05845e1e5398fa8c..7e6478e5e855b0fe5410cd593ccda45cf2cf0f8c 100644 (file)
@@ -189,6 +189,7 @@ typedef struct {
        ua_scan_completed_cb scan_device_cb; /**< Callback to let apps know scanned registered devices */
        ua_presence_detected_cb presence_cb; /**< User presence detection callback */
        ua_absence_detected_cb absence_cb; /**< Absence detection callback */
+       ua_callback_s sensor_status_cb; /**< Senser status changed callback */
        char *service; /**< Service name */
        GSList *user_state; /**< User current state i.e presence or absence */
        void *user_data; /**< User data */
@@ -200,6 +201,7 @@ typedef struct {
  * @since_tizen 5.5
  */
 typedef struct {
+       unsigned  int status; /**< Sensor status 1:PRESENCE 2:ABSENCE */
        ua_sensor_h handle;
        ua_sensor_e bitmask;
        long int timestamp;
@@ -385,6 +387,18 @@ void _ua_monitor_handle_sensor_state(unsigned int bitmask, gboolean ready);
 void _ua_monitor_handle_user_absence_detected(
                uam_sensor_info_s *sensor_info, char *service, char *account);
 
+/**
+ * @brief Calls the sensor status changed callback when notified of sensor status changed.
+ * @since_tizen 5.5
+ *
+ * @param[in] info Sensor information
+ *
+ * @exception
+ * @pre
+ * @post
+ */
+void _ua_monitor_handle_sensor_status_changed(uam_sensor_info_s *info);
+
 /**
  * @brief Destroys a monitor handle.
  * @since_tizen 5.5
index 8ee579aa6105f5ab7af5a2c5d8c8c0fe5d5ba22a..839f7d82f7b57f96cd213667ea6d37609e19d86b 100644 (file)
@@ -245,6 +245,16 @@ typedef enum {
        UA_AND_OPERATION = 0x01 /**< AND & OR */
 } ua_condition_conjunction_e;
 
+/**
+ * @ingroup CAPI_NETWORK_UA_MODULE
+ * @brief Enumerations for sensor status
+ * @since_tizen 5.5
+ */
+typedef enum {
+       UA_SENSOR_PRESENCE = 0x01, /**< Sensor reports PRESENCE status */
+       UA_SENSOR_ABSENCE = 0x02 /**< Sensor reports ABSENCE status */
+} ua_sensor_status_e;
+
 /**
  * @ingroup CAPI_NETWORK_UA_MODULE
  * @brief The handle of sensor information.
@@ -598,6 +608,35 @@ typedef bool (*ua_service_added_device_cb)(
                ua_device_h device_handle,
                void *user_data);
 
+/**
+ * @ingroup CAPI_NETWORK_UA_MODULE
+ * @brief Callback to be invoked if a sensor status changed (presence <-> absence)
+ *        immediately during detection operation.
+ * @since_tizen 5.5
+ *
+ * @remarks The @a handle should not be released.
+ * @remarks The @a handle can be used only in the callback.
+ * @remarks The @a sensor_handles should not be released.
+ * @remarks The @a sensor_handles can be used only in the callback.
+ *
+ * @param[in] status #UA_SENSOR_PRESENCE is sensor detects presence. \n
+ *                   #UA_SENSOR_ABSENCE is sensor detects presence.
+ * @param[in] sensor Sensor type for which status was changed.
+ * @param[in] handle The monitor handle for which sensor type was added.
+ * @param[in] sensor_handles The list of sensor handles on which user is not detected.
+ * @param[in] user_data The user data passed in ua_monitor_set_sensor_status_cb().
+ *
+ * @exception
+ * @pre
+ * @post
+ *
+ * @see ua_monitor_set_sensor_state_cb()
+ */
+typedef void (*ua_sensor_status_changed_cb)(
+               ua_sensor_status_e status,
+               ua_monitor_h handle,
+               ua_sensor_h sensor_handle,
+               void *user_data);
 /**
  * @ingroup CAPI_NETWORK_UA_MODULE
  * @brief Initializes an user awareness framework.
@@ -864,6 +903,50 @@ int ua_monitor_set_sensor_state_cb(
 int ua_monitor_unset_sensor_state_cb(
                ua_monitor_h handle);
 
+/**
+ * @ingroup CAPI_NETWORK_UA_MODULE
+ * @brief Sets sensor status changed callback.
+ * @since_tizen 5.5
+ *
+ * @param[in] handle The monitor handle
+ * @param[in] callback Sensor status changed callback
+ * @param[in] user_data The user data to be passed in sensor state changed callback.
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #UA_ERROR_NONE Successful
+ * @retval #UA_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @exception
+ * @pre
+ * @post
+ *
+ * @see ua_monitor_unset_sensor_state_cb()
+ */
+int ua_monitor_set_sensor_status_cb(
+               ua_monitor_h handle,
+               ua_sensor_status_changed_cb callback,
+               void *user_data);
+
+/**
+ * @ingroup CAPI_NETWORK_UA_MODULE
+ * @brief Unsets sensor status changed callback.
+ * @since_tizen 5.5
+ *
+ * @param[in] handle The monitor handle
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #UA_ERROR_NONE Successful
+ * @retval #UA_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ * @exception
+ * @pre
+ * @post
+ *
+ * @see ua_monitor_set_sensor_state_cb()
+ */
+int ua_monitor_unset_sensor_status_cb(
+               ua_monitor_h handle);
+
 /**
  * @ingroup CAPI_NETWORK_UA_MODULE
  * @brief Sets absence detected callback for all users.
index 39ad397f13aa7c4cbdd0b76babb24e48e22790c9..5e4cc8df8b2d84f140c09ef8843bddd50337f8e5 100644 (file)
@@ -1,6 +1,6 @@
 Name: capi-network-ua
 Summary: User Awareness Framework CAPI
-Version: 0.11.11
+Version: 0.12.0
 Release: 1
 License: Apache-2.0
 Source0: %{name}-%{version}.tar.gz
index 254bed59702a44804387896b916dcf351d8c7eef..3223f1d3693780a6c63103a4c07d73499f047a6d 100644 (file)
@@ -112,6 +112,20 @@ static void __ua_event_handler(int event, uam_event_data_s *event_param,
 
                break;
        }
+       case UAM_EVENT_SENSOR_STATUS_CHANGED: {
+               uam_sensor_info_s *sensor_info = NULL;
+
+               sensor_info = event_param->data;
+               ret_if(NULL == sensor_info);
+
+               UA_INFO("status [%d] sensor: [%u], timestamp [%ld], accuracy [%d], value [%f]",
+                               sensor_info->status, sensor_info->sensor_bitmask, sensor_info->timestamp,
+                               sensor_info->accuracy, sensor_info->values[0]);
+
+               _ua_monitor_handle_sensor_status_changed(sensor_info);
+
+               break;
+       }
        case UAM_EVENT_DETECTION_STARTED: {
                UA_INFO("Received DETECTION STARTED event !!");
                break;
index debbc0b2908d0fd07deaa718f9876d316755f15a..b2a0bd7d3c6aca8c012a57910442aac869bf855a 100644 (file)
@@ -363,12 +363,14 @@ static void __ua_monitor_send_env_user_presence_cb(ua_monitor_s *monitor)
        sensors = ua_sensor_get_sensor_handle_list(monitor->presence_detected_bitmask
                & filter_bitmask);
 
-       ((ua_presence_user_detected_cb)monitor->presence_user_cb.callback)(
-                                       UA_ERROR_NONE, monitor, NULL,
-                                       NULL, sensors,
-                                       monitor->presence_user_cb.user_data);
+       if (monitor->presence_user_cb.callback) {
+                       ((ua_presence_user_detected_cb)monitor->presence_user_cb.callback)(
+                                                       UA_ERROR_NONE, monitor, NULL,
+                                                       NULL, sensors,
+                                                       monitor->presence_user_cb.user_data);
 
-       monitor->env_user_cb_sent = TRUE;
+                       monitor->env_user_cb_sent = TRUE;
+       }
        g_slist_free(sensors);
 
        FUNC_EXIT;
@@ -987,6 +989,37 @@ void _ua_monitor_handle_sensor_state(unsigned int bitmask, gboolean ready)
        FUNC_EXIT;
 }
 
+void _ua_monitor_handle_sensor_status_changed(uam_sensor_info_s *info)
+{
+       FUNC_ENTRY;
+       GSList *l;
+       ua_sensor_info_s *sensor_info;
+       ret_if(NULL == info);
+
+       sensor_info = _uam_to_ua_sensor_info(info);
+       ret_if(NULL == sensor_info);
+
+       for (l = ua_monitor_list; l; l = g_slist_next(l)) {
+               ua_monitor_s *monitor = l->data;
+
+               if (!monitor || (!monitor->presence_detection_started
+                               && !monitor->internal_presence_started)
+                               || (!monitor->absence_detection_started))
+                       continue;
+
+               if (0 == (sensor_info->bitmask & monitor->sensor_bitmask))
+                       continue;
+
+               if (monitor->sensor_status_cb.callback)
+                       ((ua_sensor_status_changed_cb)monitor->sensor_status_cb.callback)(
+                               sensor_info->status, (ua_monitor_h)monitor, (ua_sensor_h)sensor_info,
+                               monitor->sensor_state_cb.user_data);
+       }
+       _ua_free_sensor_info(sensor_info);
+
+       FUNC_EXIT;
+}
+
 void _ua_free_ua_monitor_t(gpointer data)
 {
        FUNC_ENTRY;
@@ -1350,6 +1383,38 @@ int ua_monitor_set_user_absence_condition(
        return UA_ERROR_NONE;
 }
 
+int ua_monitor_set_sensor_status_cb(ua_monitor_h handle,
+               ua_sensor_status_changed_cb callback, void *user_data)
+{
+       FUNC_ENTRY;
+       ua_monitor_s *monitor = (ua_monitor_s *)handle;
+
+       UA_VALIDATE_INPUT_PARAMETER(callback);
+       UA_VALIDATE_INPUT_PARAMETER(handle);
+       UA_VALIDATE_HANDLE(handle, ua_monitor_list);
+
+       monitor->sensor_status_cb.callback = callback;
+       monitor->sensor_status_cb.user_data = user_data;
+
+       FUNC_EXIT;
+       return UA_ERROR_NONE;
+}
+
+int ua_monitor_unset_sensor_status_cb(ua_monitor_h handle)
+{
+       FUNC_ENTRY;
+       ua_monitor_s *monitor = (ua_monitor_s *)handle;
+
+       UA_VALIDATE_INPUT_PARAMETER(handle);
+       UA_VALIDATE_HANDLE(handle, ua_monitor_list);
+
+       monitor->sensor_status_cb.callback = NULL;
+       monitor->sensor_status_cb.user_data = NULL;
+
+       FUNC_EXIT;
+       return UA_ERROR_NONE;
+}
+
 int ua_monitor_add_sensor(
                ua_monitor_h handle,
                ua_sensor_e sensor_type)
index 5e113f39b01cc80307cff2369c704121319a0591..d066063e24c03b8657f130d88237d2d5d0080236 100644 (file)
@@ -291,6 +291,7 @@ ua_sensor_info_s* _uam_to_ua_sensor_info(uam_sensor_info_s *info)
        if (!sensor_info)
                return NULL;
 
+       sensor_info->status = info->status;
        sensor_info->timestamp = info->timestamp;
        sensor_info->count = info->count;
        sensor_info->bitmask = _uam_to_ua_sensor(info->sensor_bitmask);
index c91c617f4d465f692890709825822839bb083a89..10a0fad7022d006260b5b9e2df22ba97ac5489d1 100644 (file)
@@ -39,7 +39,7 @@ static char g_ibeacon_adv_len[MENU_DATA_SIZE + 1] = {0,}; /**< iBeacon advertisi
 static char g_ibeacon_adv_str[MENU_DATA_SIZE + 1] = {0,}; /**< iBeacon advertising data */
 
 static void __user_detected_foreach_sensors(gpointer data,
-                                                    gpointer user_data)
+       gpointer user_data)
 {
        int ret;
        char buf[MENU_DATA_SIZE] = {0, };
@@ -130,7 +130,7 @@ const char * __convert_device_mac_type_to_txt(ua_mac_type_e mac_type)
 }
 
 static void __user_presence_detected_foreach_devices(gpointer data,
-                                                    gpointer user_data)
+       gpointer user_data)
 {
        int ret;
        ua_device_h device_handle = (ua_device_h)data;
@@ -155,8 +155,8 @@ static void __user_presence_detected_foreach_devices(gpointer data,
 }
 
 static void __user_presence_detected_cb(int result, ua_monitor_h monitor,
-               ua_user_h user_handle, GSList *device_handles, GSList *sensor_handles,
-               void *user_data)
+       ua_user_h user_handle, GSList *device_handles, GSList *sensor_handles,
+       void *user_data)
 {
        int ret;
        char *account = NULL;
@@ -193,6 +193,58 @@ static void __sensor_state_changed_cb(bool ready,
        msgb("sensor: %s is %s",
                        uat_get_sensor_bitmask_str(sensor), (ready ? "Ready" : "Not Ready"));
 }
+static void __sensor_status_changed_cb(ua_sensor_status_e status,
+       ua_monitor_h handle, ua_sensor_h sensor_handle, void *user_data)
+{
+       int ret;
+       double *value;
+       GSList *l = 0;
+       long int timestamp;
+       int info_count = 0;
+       ua_sensor_e sensor;
+       GSList *values = NULL;
+       char buf[MENU_DATA_SIZE] = {0, };
+       char final_buf[MENU_DATA_SIZE * 4] = {0, };
+       char *pbuf = uat_get_time();
+
+       msgr("\n[%s] Sensor Status Changed -> %s",
+               pbuf, status == UA_SENSOR_PRESENCE ? "PRESENCE" : "ABSENCE");
+       free(pbuf);
+
+       ret = ua_sensor_get_bitmask(sensor_handle, &sensor);
+       if (UA_ERROR_NONE != ret) {
+               msg(" - ua_sensor_get_bitmask() ret: [0x%X] [%s]",
+                       ret, uat_get_error_str(ret));
+       }
+
+       ret = ua_sensor_get_timestamp(sensor_handle, &timestamp);
+       if (UA_ERROR_NONE != ret) {
+               msg(" - ua_sensor_get_timestamp() ret: [0x%X] [%s]",
+                       ret, uat_get_error_str(ret));
+       }
+
+       ret = ua_sensor_get_info_count(sensor_handle, &info_count);
+       if (UA_ERROR_NONE != ret) {
+               msg(" - ua_sensor_get_info_count() ret: [0x%X] [%s]",
+                       ret, uat_get_error_str(ret));
+       }
+
+       values = ua_sensor_get_info_values(sensor_handle);
+       if (NULL == values) {
+               msg(" - ua_sensor_get_info_values() failed");
+       }
+
+       for (l = values; l; l = g_slist_next(l)) {
+               value = (double *)l->data;
+               snprintf(buf, MENU_DATA_SIZE, "%lF ", *value);
+               strncat(final_buf, buf, sizeof(buf) - strlen(buf) - 1);
+               memset(buf, 0, MENU_DATA_SIZE);
+       }
+       msgr("[%s] information detected at timestamp [%ld] value [%s]",
+               uat_get_sensor_bitmask_str(sensor), timestamp, final_buf);
+
+       g_slist_free(values);
+}
 
 static int run_ua_monitor_create(MManager *mm, struct menu_data *menu)
 {
@@ -295,6 +347,22 @@ static int run_ua_monitor_set_sensor_state_cb(
        return RET_SUCCESS;
 }
 
+static int run_ua_monitor_set_sensor_status_cb(
+               MManager *mm, struct menu_data *menu)
+{
+       int ret = UA_ERROR_NONE;
+
+       msg("ua_monitor_set_sensor_status_cb");
+
+       ret = ua_monitor_set_sensor_status_cb(g_ua_mon_h,
+                       __sensor_status_changed_cb, g_ua_mon_h);
+
+       msg(" - ua_monitor_set_sensor_status_cb() ret: [0x%X] [%s]",
+                       ret, uat_get_error_str(ret));
+
+       return RET_SUCCESS;
+}
+
 static int run_ua_monitor_unset_user_absence_detected_cb(
                MManager *mm, struct menu_data *menu)
 {
@@ -340,6 +408,21 @@ static int run_ua_monitor_unset_sensor_state_cb(
        return RET_SUCCESS;
 }
 
+static int run_ua_monitor_unset_sensor_status_cb(
+               MManager *mm, struct menu_data *menu)
+{
+       int ret = UA_ERROR_NONE;
+
+       msg("ua_monitor_unset_sensor_status_cb");
+
+       ret = ua_monitor_unset_user_absence_detected_cb(g_ua_mon_h);
+
+       msg(" - ua_monitor_unset_sensor_status_cb() ret: [0x%X] [%s]",
+                       ret, uat_get_error_str(ret));
+
+       return RET_SUCCESS;
+}
+
 static int run_ua_set_app_info(MManager *mm, struct menu_data *menu)
 {
        int ret = UA_ERROR_NONE;
@@ -472,25 +555,29 @@ struct menu_data menu_ua_init[] = {
                NULL, run_ua_monitor_create, NULL },
        { "2", "ua_monitor_destroy",
                NULL, run_ua_monitor_destroy, NULL },
-       { "3", "ua_monitor_set_user_sensor_state_cb",
+       { "3", "ua_monitor_set_sensor_state_cb",
                NULL, run_ua_monitor_set_sensor_state_cb, NULL },
-       { "4", "ua_monitor_unset_user_sensor_state_cb",
+       { "4", "ua_monitor_unset_sensor_state_cb",
                NULL, run_ua_monitor_unset_sensor_state_cb, NULL },
-       { "5", "ua_monitor_set_user_absence_detected_cb",
+       { "5", "ua_monitor_set_sensor_status_cb",
+               NULL, run_ua_monitor_set_sensor_status_cb, NULL },
+       { "6", "ua_monitor_unset_sensor_status_cb",
+               NULL, run_ua_monitor_unset_sensor_status_cb, NULL },
+       { "7", "ua_monitor_set_user_absence_detected_cb",
                NULL, run_ua_monitor_set_user_absence_detected_cb, NULL },
-       { "6", "ua_monitor_unset_absence_detected_cb",
+       { "8", "ua_monitor_unset_absence_detected_cb",
                NULL, run_ua_monitor_unset_user_absence_detected_cb, NULL },
-       { "7", "ua_monitor_set_user_presence_detected_cb",
+       { "9", "ua_monitor_set_user_presence_detected_cb",
                NULL, run_ua_monitor_set_user_presence_detected_cb, NULL },
-       { "8", "ua_monitor_unset_presence_detected_cb",
+       { "10", "ua_monitor_unset_presence_detected_cb",
                NULL, run_ua_monitor_unset_user_presence_detected_cb, NULL },
-       { "9", "ua_set_app_info",
+       { "11", "ua_set_app_info",
                menu_ua_set_app_info, NULL, NULL},
-       { "10", "ua_unset_app_info",
+       { "12", "ua_unset_app_info",
                menu_ua_unset_app_info, NULL, NULL},
-       { "11", "ua_reset_database",
+       { "13", "ua_reset_database",
                NULL, run_ua_reset_database, NULL},
-       { "12", "ua_add_ibeacon_adv_data",
+       { "14", "ua_add_ibeacon_adv_data",
                menu_ua_add_ibeacon_adv_data, NULL, NULL},
        { NULL, NULL, },
 };