use sensor handle to send sensor information to application
authorAbhay Agarwal <ay.agarwal@samsung.com>
Tue, 1 Oct 2019 09:19:22 +0000 (18:19 +0900)
committersaerome.kim <saerome.kim@samsung.com>
Tue, 1 Oct 2019 12:38:52 +0000 (21:38 +0900)
This commit provides the sensor information to application using a sensor handle
instead of providing directly using sensor struct, so that sensor struct need not
to be disclosed to the application.

Change-Id: Iab7ff5c6898404412f0fbb3d2d3586f2306d3237
Signed-off-by: Abhay Agarwal <ay.agarwal@samsung.com>
include/user-awareness-private.h
include/user-awareness-util.h
include/user-awareness.h
src/user-awareness-monitors.c
src/user-awareness-util.c
test/uat-detections.c

index f33b0b4c2990a1725782a5acc907642122979656..357e7cc9500513909542919780e0c13b1260ddb6 100644 (file)
@@ -194,6 +194,19 @@ typedef struct {
        void *user_data; /**< User data */
 } ua_monitor_s;
 
+/**
+ * @brief sensor info data structure.
+ * @since_tizen 5.5
+ */
+typedef struct {
+       ua_sensor_h handle;
+       ua_sensor_e bitmask;
+       long int timestamp;
+       int accuracy;
+       int count;
+       double *values;
+} ua_sensor_info_s;
+
 /**
  * @brief User state data structure.
  * @since_tizen 5.5
index 6a70257115c774b725ce6cc94c9eea462cf6178b..0fc4555617ab44aa3a46917ae41a3f69c06d9858 100644 (file)
@@ -216,6 +216,31 @@ ua_sensor_e _uam_to_ua_sensor(uam_sensor_bitmask_e bitmask);
  */
 ua_sensor_info_s* _uam_to_ua_sensor_info(uam_sensor_info_s *info);
 
+/**
+ * @ingroup CAPI_NETWORK_UA_MODULE
+ * @brief Gets sensor handle by sensor info.
+ * @since_tizen 5.5
+ *
+ * @remarks The @a sensor_handle should not be released.
+ * @remarks The @a sensor_handle can be used only in the function.
+ *
+ * @param[in] sensor_info The detected sensor information
+ * @param[out] sensor_handle The sensor handle.
+ *
+ * @return 0 on success, otherwise a negative error value
+ * @retval #UA_ERROR_NONE Successful
+ * @retval #UA_ERROR_INVALID_PARAMETER Invalid parameter
+ * @retval #UA_ERROR_OUT_OF_MEMORY Out of memory
+ *
+ * @exception
+ * @pre
+ * @post
+ *
+ */
+int ua_sensor_get_by_sensor_info(
+               ua_sensor_info_s *sensor_info,
+               ua_sensor_h * sensor_handle);
+
 #ifdef __cplusplus
 }
 #endif
index 1eaa5aa55b844c462fcc8d1df50ac42be062c704..ff276fe0b85cc2b9173a62c325256c8031678eeb 100644 (file)
@@ -245,16 +245,11 @@ typedef enum {
 } ua_condition_conjunction_e;
 
 /**
- * @brief sensor info data structure.
+ * @ingroup CAPI_NETWORK_UA_MODULE
+ * @brief The handle of sensor information.
  * @since_tizen 5.5
  */
-typedef struct {
-       ua_sensor_e bitmask;
-       long int timestamp;
-       int accuracy;
-       int count;
-       double *values;
-} ua_sensor_info_s;
+typedef void *ua_sensor_h;
 
 /**
  * @ingroup CAPI_NETWORK_UA_MODULE
@@ -383,7 +378,7 @@ typedef void (*ua_presence_detected_cb)(
                ua_monitor_h handle,
                ua_sensor_e sensor,
                ua_device_h device_handle,
-               ua_sensor_info_s *sensor_info,
+               ua_sensor_h sensor_handle,
                void *user_data);
 
 /**
@@ -413,7 +408,7 @@ typedef void (*ua_absence_detected_cb)(
                int result,
                ua_monitor_h handle,
                ua_sensor_e sensor,
-               ua_sensor_info_s *sensor_info,
+               ua_sensor_sensor_info,
                void *user_data);
 
 /**
index 305868610a6b644c24f79eb2aaec833be95f2b79..9ada0e19c0b1db82e5073093215509363ac7ea6c 100644 (file)
@@ -334,6 +334,10 @@ static void __ua_monitor_send_sensor_presence_cb(ua_monitor_s *monitor,
 {
        FUNC_ENTRY;
        ua_sensor_e bitmask = sensor_info->bitmask;
+       ua_sensor_h sensor_handle;
+       int ret = ua_sensor_get_by_sensor_info(sensor_info, &sensor_handle);
+       UA_INFO("ua_device_get_by_device_id returned %s",
+                       _ua_get_error_string(ret));
        switch (monitor->presence_mode) {
        case UA_DETECT_MODE_ALL_SENSOR:
                /*
index 6b479a602266387e4fb852bd4b96937ed8583e32..e64cf6616967614e1cf8cf11f713986867811089 100644 (file)
@@ -22,6 +22,8 @@
 #include <user-awareness-log.h>
 #include <user-awareness-private.h>
 
+GSList *ua_sensors_list = NULL; /* ua_sensor_info_s */
+
 ua_mac_type_e _to_ua_mac_type(uam_tech_type_e tech_type)
 {
        switch (tech_type) {
@@ -214,12 +216,9 @@ ua_sensor_e _uam_to_ua_sensor(uam_sensor_bitmask_e bitmask)
        FUNC_EXIT;
 }
 
-ua_sensor_info_s* _uam_to_ua_sensor_info(uam_sensor_info_s *info)
+static ua_sensor_info_s* __ua_allocate_ua_sensor_info()
 {
-       FUNC_ENTRY;
-       ua_sensor_info_s *sensor_info = NULL;
-       unsigned int i = 0;
-
+       ua_sensor_info_s *sensor_info;
        sensor_info = g_malloc0(sizeof(ua_sensor_info_s));
        if (!sensor_info) {
                UA_ERR("g_malloc0 failed");
@@ -232,6 +231,57 @@ ua_sensor_info_s* _uam_to_ua_sensor_info(uam_sensor_info_s *info)
                g_free(sensor_info);
                return NULL;
        }
+       return sensor_info;
+}
+
+static ua_sensor_info_s *__ua_get_sensor_info_from_list(ua_sensor_e bitmask)
+{
+       GSList *l;
+       ua_sensor_info_s *sensor;
+
+       for (l = ua_sensors_list; NULL != l; l = g_slist_next(l)) {
+               sensor = (ua_sensor_info_s *)l->data;
+
+               if (bitmask == sensor->bitmask) {
+                       UA_INFO("Sensor info found, sensor bitmask[%d]", sensor->bitmask);
+                       return sensor;
+               }
+       }
+
+       return NULL;
+}
+
+static int __ua_update_sensor_info(
+               ua_sensor_info_s *sensor,
+               ua_sensor_info_s *info)
+{
+       FUNC_ENTRY;
+       unsigned int i = 0;
+
+       UA_VALIDATE_INPUT_PARAMETER(sensor);
+       UA_VALIDATE_INPUT_PARAMETER(info);
+
+       sensor->timestamp = info->timestamp;
+       sensor->count = info->count;
+       sensor->bitmask = info->bitmask;
+       for(i = 0; i < UA_SENSOR_MAX_VALUES; i++) {
+               sensor->values[i] = info->values[i];
+       }
+
+       FUNC_EXIT;
+       return UA_ERROR_NONE;
+}
+
+ua_sensor_info_s* _uam_to_ua_sensor_info(uam_sensor_info_s *info)
+{
+       FUNC_ENTRY;
+       ua_sensor_info_s *sensor_info = NULL;
+       unsigned int i = 0;
+
+       sensor_info = __ua_allocate_ua_sensor_info();
+       if (!sensor_info)
+               return NULL;
+
        sensor_info->timestamp = info->timestamp;
        sensor_info->count = info->count;
        sensor_info->bitmask = _uam_to_ua_sensor(info->sensor_bitmask);
@@ -242,3 +292,39 @@ ua_sensor_info_s* _uam_to_ua_sensor_info(uam_sensor_info_s *info)
        return sensor_info;
        FUNC_EXIT;
 }
+
+int ua_sensor_get_by_sensor_info(
+               ua_sensor_info_s *info,
+               ua_sensor_h *sensor_handle)
+{
+       FUNC_ENTRY;
+       int ret = UA_ERROR_NONE;
+       ua_sensor_info_s *sensor;
+       *sensor_handle = NULL;
+
+       UA_VALIDATE_INPUT_PARAMETER(info);
+       UA_VALIDATE_INPUT_PARAMETER(sensor_handle);
+
+       sensor = __ua_get_sensor_info_from_list(info->bitmask);
+
+       if (NULL != sensor) {
+               ret = __ua_update_sensor_info(sensor, info);
+               *sensor_handle = (ua_sensor_h)sensor;
+               goto done;
+       }
+
+       sensor = __ua_allocate_ua_sensor_info();
+       if (!sensor)
+               return UA_ERROR_OUT_OF_MEMORY;
+
+       ret = __ua_update_sensor_info(sensor, info);
+
+       /* Add sensor to list of sensors */
+       *sensor_handle = (ua_sensor_h)sensor;
+       sensor->handle = *sensor_handle;
+       ua_sensors_list = g_slist_append(ua_sensors_list, sensor);
+
+done:
+       FUNC_EXIT;
+       return ret;
+}
index 54502de7ce67556d7679db7e9f0461ece0fb4c49..b6cb71e03f566b39f7664ee5c123098766b2e22b 100644 (file)
@@ -70,12 +70,28 @@ static void __sensor_presence_detected_device(ua_device_h device_handle)
        g_free(mac);
 }
 
-static void __sensor_presence_detected_cb(int result, ua_monitor_h monitor,
-               ua_sensor_e sensor, ua_device_h device_handle, ua_sensor_info_s *info,
-               void *user_data)
+static void __sensor_presence_detected_sensor_info(ua_sensor_h sensor_handle)
 {
+/*
        char buf[MENU_DATA_SIZE] = {0, };
        char final_buf[MENU_DATA_SIZE * 4] = {0, };
+       for (int i = 0; i < info->count ; i++) {
+               if (i >= 4)
+                       break;
+               snprintf(buf, MENU_DATA_SIZE, "%lF ", info->values[i]);
+               strncat(final_buf, buf, sizeof(buf) - strlen(buf) - 1);
+               memset(buf, 0, MENU_DATA_SIZE);
+       }
+       msgb("[%s] information detected at timestamp [%ld] value [%s]",
+               uat_get_sensor_bitmask_str(info->bitmask), info->timestamp,
+                       final_buf);
+*/
+}
+
+static void __sensor_presence_detected_cb(int result, ua_monitor_h monitor,
+               ua_sensor_e sensor, ua_device_h device_handle, ua_sensor_h sensor_handle,
+               void *user_data)
+{
        char *pbuf = uat_get_time();
        msg("\n[%s] Sensor PRESENCE [%d]", pbuf, sensor);
        free(pbuf);
@@ -101,25 +117,13 @@ static void __sensor_presence_detected_cb(int result, ua_monitor_h monitor,
        if (device_handle)
                __sensor_presence_detected_device(device_handle);
        /* For sensor information */
-       if (info && (sensor & (UA_SENSOR_LIGHT | UA_SENSOR_MOTION))) {
-               for (int i = 0; i < info->count ; i++) {
-                       if (i >= 4)
-                               break;
-                       snprintf(buf, MENU_DATA_SIZE, "%lF ", info->values[i]);
-                       strncat(final_buf, buf, sizeof(buf) - strlen(buf) - 1);
-                       memset(buf, 0, MENU_DATA_SIZE);
-               }
-               msgb("[%s] information detected at timestamp [%ld] value [%s]",
-                       uat_get_sensor_bitmask_str(info->bitmask), info->timestamp,
-                               final_buf);
-       }
+       if (sensor_handle && (sensor & (UA_SENSOR_LIGHT | UA_SENSOR_MOTION)))
+               __sensor_presence_detected_sensor_info(sensor_handle);
 }
 
 static void __sensor_absence_detected_cb(int result, ua_monitor_h monitor,
-       ua_sensor_e sensor, ua_sensor_info_s *info, void *user_data)
+       ua_sensor_e sensor, ua_sensor_h sensor_handle, void *user_data)
 {
-       char buf[MENU_DATA_SIZE] = {0, };
-       char final_buf[MENU_DATA_SIZE * 4] = {0, };
        char *pbuf = uat_get_time();
        msg("\n[%s] Sensor ABSENCE", pbuf);
        free(pbuf);
@@ -141,18 +145,8 @@ static void __sensor_absence_detected_cb(int result, ua_monitor_h monitor,
                        uat_get_sensor_bitmask_str(UA_SENSOR_MOTION), uat_get_error_str(result));
        }
        /* For sensor information */
-       if (info && (sensor & (UA_SENSOR_LIGHT | UA_SENSOR_MOTION))) {
-               for (int i = 0; i < info->count ; i++) {
-                       if (i >= 4)
-                               break;
-                       snprintf(buf, MENU_DATA_SIZE, "%lF ", info->values[i]);
-                       strncat(final_buf, buf, sizeof(buf) - strlen(buf) - 1);
-                       memset(buf, 0, MENU_DATA_SIZE);
-               }
-               msgb("[%s] information detected at timestamp [%ld] value [%s]",
-                       uat_get_sensor_bitmask_str(info->bitmask), info->timestamp,
-                               final_buf);
-       }
+       if (sensor_handle && (sensor & (UA_SENSOR_LIGHT | UA_SENSOR_MOTION)))
+               __sensor_presence_detected_sensor_info(sensor_handle);
 }
 
 void __ua_test_scan_completed_cb(ua_active_scan_type_e result, ua_monitor_h handle,