Test version for sensor-pis(Same URI, path from file, No sensor, Always notify true) 67/146067/1
authorJin Yoon <jinny.yoon@samsung.com>
Thu, 24 Aug 2017 22:51:30 +0000 (07:51 +0900)
committerJin Yoon <jinny.yoon@samsung.com>
Thu, 24 Aug 2017 22:51:30 +0000 (07:51 +0900)
Change-Id: If2490ce320936f63daa636f8e3d241fc187fa980

inc/connectivity.h
src/connectivity.c
src/controller.c

index fc7962f..ca1aa2f 100644 (file)
 struct connectivity_resource {
        iotcon_resource_h res;
        iotcon_observers_h observers;
+       char *path;
 };
 
 typedef struct connectivity_resource connectivity_resource_s;
 
 /**
  * @brief Create connectivity resource and registers the resource in server.
- * @param[in] uri_path The URI path of the resource
+ * @param[in] path The path of the resource
  * @param[in] type The string data to insert into the resource types (e.g. "org.tizen.light")
  * @param[out] out_resource_info A structure containing information about connectivity resource
  * @return 0 on success, otherwise a negative error value
  * @see uri_path length must be less than 128.
  * @see You must destroy resource by calling connectivity_unset_resource() if resource is no longer needed.
  */
-extern int connectivity_set_resource(const char *uri_path, const char *type, connectivity_resource_s **out_resource_info);
+extern int connectivity_set_resource(const char *path, const char *type, connectivity_resource_s **out_resource_info);
 
 /**
  * @brief Releases all resource about connectivity.
index 0ec3195..1554737 100644 (file)
@@ -31,6 +31,8 @@
 #define ULTRASONIC_RESOURCE_TYPE "org.tizen.door"
 #define BUFSIZE 1024
 #define URI_PATH_LEN 64
+#define URI_PATH "/door/1"
+#define PATH "path"
 
 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data);
 
@@ -66,14 +68,14 @@ static void _destroy_representation(iotcon_representation_h representation)
        iotcon_representation_destroy(representation);
 }
 
-static iotcon_representation_h _create_representation_with_bool(iotcon_resource_h res, const char *key, bool value)
+static iotcon_representation_h _create_representation_with_bool(connectivity_resource_s *resource_info, const char *key, bool value)
 {
        iotcon_attributes_h attributes = NULL;
        iotcon_representation_h representation = NULL;
        char *uri_path = NULL;
        int ret = -1;
 
-       ret = iotcon_resource_get_uri_path(res, &uri_path);
+       ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
        retv_if(IOTCON_ERROR_NONE != ret, NULL);
 
        ret = iotcon_representation_create(&representation);
@@ -85,6 +87,9 @@ static iotcon_representation_h _create_representation_with_bool(iotcon_resource_
        ret = iotcon_representation_set_uri_path(representation, uri_path);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
+       ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
        ret = iotcon_attributes_add_bool(attributes, key, value);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
@@ -102,14 +107,14 @@ error:
        return NULL;
 }
 
-static iotcon_representation_h _create_representation_with_int(iotcon_resource_h res, const char *key, int value)
+static iotcon_representation_h _create_representation_with_int(connectivity_resource_s *resource_info, const char *key, int value)
 {
        iotcon_attributes_h attributes = NULL;
        iotcon_representation_h representation = NULL;
        char *uri_path = NULL;
        int ret = -1;
 
-       ret = iotcon_resource_get_uri_path(res, &uri_path);
+       ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
        retv_if(IOTCON_ERROR_NONE != ret, NULL);
 
        ret = iotcon_representation_create(&representation);
@@ -121,7 +126,10 @@ static iotcon_representation_h _create_representation_with_int(iotcon_resource_h
        ret = iotcon_representation_set_uri_path(representation, uri_path);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
-       ret = iotcon_attributes_add_int(attributes, "opened", value);
+       ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_attributes_add_int(attributes, key, value);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
        ret = iotcon_representation_set_attributes(representation, attributes);
@@ -138,14 +146,14 @@ error:
        return NULL;
 }
 
-static iotcon_representation_h _create_representation_with_double(iotcon_resource_h res, const char *key, double value)
+static iotcon_representation_h _create_representation_with_double(connectivity_resource_s *resource_info, const char *key, double value)
 {
        iotcon_attributes_h attributes = NULL;
        iotcon_representation_h representation = NULL;
        char *uri_path = NULL;
        int ret = -1;
 
-       ret = iotcon_resource_get_uri_path(res, &uri_path);
+       ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
        retv_if(IOTCON_ERROR_NONE != ret, NULL);
 
        ret = iotcon_representation_create(&representation);
@@ -157,7 +165,10 @@ static iotcon_representation_h _create_representation_with_double(iotcon_resourc
        ret = iotcon_representation_set_uri_path(representation, uri_path);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
-       ret = iotcon_attributes_add_double(attributes, "opened", value);
+       ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_attributes_add_double(attributes, key, value);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
        ret = iotcon_representation_set_attributes(representation, attributes);
@@ -184,7 +195,7 @@ int connectivity_notify_bool(connectivity_resource_s *resource_info, const char
 
        _D("Notify the value[%d]", value);
 
-       representation = _create_representation_with_bool(resource_info->res, key, value);
+       representation = _create_representation_with_bool(resource_info, key, value);
        retv_if(!representation, -1);
 
        ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
@@ -205,7 +216,7 @@ int connectivity_notify_int(connectivity_resource_s *resource_info, const char *
 
        _D("Notify the value[%d]", value);
 
-       representation = _create_representation_with_int(resource_info->res, key, value);
+       representation = _create_representation_with_int(resource_info, key, value);
        retv_if(!representation, -1);
 
        ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
@@ -226,7 +237,7 @@ int connectivity_notify_double(connectivity_resource_s *resource_info, const cha
 
        _D("Notify the value[%f]", value);
 
-       representation = _create_representation_with_double(resource_info->res, key, value);
+       representation = _create_representation_with_double(resource_info, key, value);
        retv_if(!representation, -1);
 
        ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
@@ -409,10 +420,11 @@ void connectivity_unset_resource(connectivity_resource_s *resource_info)
        ret_if(!resource_info);
        if (resource_info->observers) iotcon_observers_destroy(resource_info->observers);
        if (resource_info->res) iotcon_resource_destroy(resource_info->res);
+       if (resource_info->path) free(resource_info->path);
        free(resource_info);
 }
 
-static int _get_default_uri_path_in_conf(char *buf, int size)
+static int _get_default_path_in_conf(char *buf, int size)
 {
        FILE *in = NULL;
        size_t nread = 0;
@@ -434,29 +446,28 @@ static int _get_default_uri_path_in_conf(char *buf, int size)
        return 0;
 }
 
-int connectivity_set_resource(const char *uri_path, const char *type, connectivity_resource_s **out_resource_info)
+int connectivity_set_resource(const char *path, const char *type, connectivity_resource_s **out_resource_info)
 {
        iotcon_resource_types_h resource_types = NULL;
        iotcon_resource_interfaces_h ifaces = NULL;
        connectivity_resource_s *resource_info = NULL;
        uint8_t policies;
        int ret = -1;
-       const char *final_uri_path = NULL;
-       char default_uri_path[URI_PATH_LEN] = { 0, };
+       char default_path[URI_PATH_LEN] = { 0, };
+
+       resource_info = calloc(1, sizeof(connectivity_resource_s));
+       retv_if(!resource_info, -1);
 
-       if (uri_path) {
-               final_uri_path = uri_path;
+       if (path) {
+               resource_info->path = strdup(path);
        } else {
-               ret = _get_default_uri_path_in_conf(default_uri_path, URI_PATH_LEN);
+               ret = _get_default_path_in_conf(default_path, URI_PATH_LEN);
                retv_if(ret < 0, -1);
-               final_uri_path = default_uri_path;
+               resource_info->path = strdup(default_path);
        }
+       goto_if(!resource_info->path, error);
 
-       _D("uri path : [%s]", final_uri_path);
-
-       resource_info = calloc(1, sizeof(connectivity_resource_s));
-       retv_if(!resource_info, -1);
-       *out_resource_info = resource_info;
+       _D("Path : [%s]", resource_info->path);
 
        ret = iotcon_resource_types_create(&resource_types);
        goto_if(IOTCON_ERROR_NONE != ret, error);
@@ -478,7 +489,7 @@ int connectivity_set_resource(const char *uri_path, const char *type, connectivi
                IOTCON_RESOURCE_OBSERVABLE |
                IOTCON_RESOURCE_SECURE;
 
-       ret = iotcon_resource_create(final_uri_path,
+       ret = iotcon_resource_create(URI_PATH,
                        resource_types,
                        ifaces,
                        policies,
@@ -492,6 +503,7 @@ int connectivity_set_resource(const char *uri_path, const char *type, connectivi
 
        iotcon_resource_types_destroy(resource_types);
        iotcon_resource_interfaces_destroy(ifaces);
+       *out_resource_info = resource_info;
 
        return 0;
 
@@ -499,6 +511,7 @@ error:
        if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
        if (resource_types) iotcon_resource_types_destroy(resource_types);
        if (resource_info->res) iotcon_resource_destroy(resource_info->res);
+       if (resource_info->path) free(resource_info->path);
        if (resource_info) free(resource_info);
 
        return -1;
index f533159..b00b4c9 100644 (file)
 #include "connectivity.h"
 #include "controller.h"
 
-#define GPIO_ULTRASONIC_TRIG_NUM_1 20
-#define GPIO_ULTRASONIC_ECHO_NUM_1 21
-#define MULTIPLE_SENSOR_NUMBER 5
 #define CONNECTIVITY_KEY "opened"
+#define SENSORING_TIME_INTERVAL 1.0f
 
 typedef struct app_data_s {
        Ecore_Timer *getter_timer;
@@ -42,36 +40,9 @@ typedef struct app_data_s {
 
 static Eina_Bool control_sensors_cb(void *data)
 {
-       int value[MULTIPLE_SENSOR_NUMBER] = { 0, };
-       int total = 0;
-       int gpio_num[MULTIPLE_SENSOR_NUMBER] = { 5, 6, 13, 19, 26 };
-       int i = 0;
        app_data *ad = data;
 
-       /**
-        * This is the case when a number of the same sensors are installed.
-        * Each of the five infrared motion sensors will receive the value.
-        */
-       for (i = 0; i < MULTIPLE_SENSOR_NUMBER; i++) {
-               /**
-                * Infrared motion sensor outputs 1 if motion is detected, or 0 if motion is not detected.
-                */
-               if (resource_read_infrared_motion_sensor(gpio_num[i], &value[i]) == -1) {
-                       _E("Failed to get Infrared Motion value [GPIO:%d]", gpio_num[i]);
-                       continue;
-               }
-               /**
-                * If at least one of the five infrared motion sensors detects motion (1),
-                * it is judged that there is a person (total == 1).
-                */
-               total |= value[i];
-       }
-       _I("[5:%d] | [6:%d] | [13:%d] | [19:%d] | [26:%d] = [Total:%d]", value[0], value[1], value[2], value[3], value[4], total);
-
-       /**
-        * Notifies specific clients that resource's attributes have changed.
-        */
-       if (connectivity_notify_bool(ad->resource_info, CONNECTIVITY_KEY, total) == -1)
+       if (connectivity_notify_bool(ad->resource_info, CONNECTIVITY_KEY, true) == -1)
                _E("Cannot notify message");
 
        return ECORE_CALLBACK_RENEW;
@@ -91,14 +62,14 @@ static bool service_app_create(void *data)
        /**
         * Create a connectivity resource and registers the resource in server.
         */
-       ret = connectivity_set_resource("/door/1", "org.tizen.door", &ad->resource_info);
+       ret = connectivity_set_resource(NULL, "org.tizen.door", &ad->resource_info);
        if (ret == -1) _E("Cannot broadcast resource");
 
        /**
         * Creates a timer to call the given function in the given period of time.
         * In the control_sensors_cb(), each sensor reads the measured value or writes a specific value to the sensor.
         */
-       ad->getter_timer = ecore_timer_add(0.5f, control_sensors_cb, ad);
+       ad->getter_timer = ecore_timer_add(SENSORING_TIME_INTERVAL, control_sensors_cb, ad);
        if (!ad->getter_timer) {
                _E("Failed to add infrared motion getter timer");
                return false;