Add iotcon_lite_resource_state_changed_cb
authorsung.goo.kim <sung.goo.kim@samsung.com>
Wed, 13 Jan 2016 05:53:12 +0000 (14:53 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 14 Jan 2016 22:10:58 +0000 (07:10 +0900)
Change-Id: I6aebdd4134c478428564aec14e84aa8330f2fc89

lib/icl-lite-resource.c
lib/include/iotcon-lite-resource.h
test/iotcon-test-encap-server.c

index 0c62f6e..1043d15 100644 (file)
@@ -39,6 +39,8 @@ struct icl_lite_resource {
        int64_t handle;
        unsigned int sub_id;
        int properties;
+       iotcon_lite_resource_put_request_cb cb;
+       void *cb_data;
 };
 
 
@@ -206,6 +208,16 @@ static void _icl_lite_resource_request_handler(GDBusConnection *connection,
 
                icl_state_from_gvariant(recv_state, state_iter);
 
+               if (resource->cb) {
+                       if (false == resource->cb(resource, recv_state, resource->cb_data)) {
+                               _icl_lite_resource_response_send(repr, oic_request_h, oic_resource_h,
+                                               IOTCON_RESPONSE_ERROR);
+                               iotcon_state_destroy(recv_state);
+                               iotcon_representation_destroy(repr);
+                               return;
+                       }
+               }
+
                ret = _icl_lite_resource_set_state(recv_state, resource->state);
                if (IOTCON_ERROR_NONE != ret) {
                        ERR("_icl_lite_resource_set_state() Fail(%d)", ret);
@@ -252,7 +264,6 @@ static void _icl_lite_resource_request_handler(GDBusConnection *connection,
                if (IOTCON_ERROR_NONE != ret)
                        WARN("_icl_lite_resource_notify() Fail(%d)", ret);
        }
-
        iotcon_representation_destroy(repr);
 }
 
@@ -278,6 +289,8 @@ API int iotcon_lite_resource_create(const char *uri_path,
                iotcon_resource_types_h res_types,
                int properties,
                iotcon_state_h state,
+               iotcon_lite_resource_put_request_cb cb,
+               void *user_data,
                iotcon_lite_resource_h *resource_handle)
 {
        int ret, iface;
@@ -332,6 +345,8 @@ API int iotcon_lite_resource_create(const char *uri_path,
        resource->uri_path = ic_utils_strdup(uri_path);
        if (state)
                resource->state = icl_state_ref(state);
+       resource->cb = cb;
+       resource->cb_data = user_data;
 
        snprintf(signal_name, sizeof(signal_name), "%s_%llx", IC_DBUS_SIGNAL_REQUEST_HANDLER,
                        signal_number);
index 0f61932..8b37ee5 100644 (file)
 ...
 static iotcon_lite_resource_h _resource;
 
+static bool _state_changed_cb(iotcon_lite_resource_h resource, iotcon_state_h state, void *user_data)
+{
+       return true;
+}
+
 static void _create_light_resource()
 {
        int ret;
@@ -81,7 +86,8 @@ static void _create_light_resource()
        }
 
        ret = iotcon_lite_resource_create("/light/1", resource_types,
-                       IOTCON_RESOURCE_DISCOVERABLE | IOTCON_RESOURCE_OBSERVABLE, state, &resource);
+                       IOTCON_RESOURCE_DISCOVERABLE | IOTCON_RESOURCE_OBSERVABLE, state,
+                       _state_changed_cb, NULL, &resource);
        if (IOTCON_ERROR_NONE != ret) {
                iotcon_state_destroy(state);
                iotcon_resource_types_destroy(resource_types);
@@ -129,6 +135,25 @@ static void _update_brightness(int brightness)
  */
 
 /**
+ * @brief Specifies the type of function passed to iotcon_lite_resource_create().
+ *
+ * @since_tizen 3.0
+ *
+ * @param[in] resource The handle of the lite resource
+ * @param[in] state The state of the lite resource
+ * @param[in] user_data The user data to pass to the function
+ *
+ * @pre The callback must be registered using iotcon_lite_resource_create()
+ *
+ * @return true to accept put request, otherwise false to reject it.
+ *
+ * @see iotcon_lite_resource_create()
+ */
+typedef bool (*iotcon_lite_resource_put_request_cb)(iotcon_lite_resource_h resource,
+               iotcon_state_h state, void *user_data);
+
+
+/**
  * @brief Creates a lite resource handle and registers the resource in server
  * @details Registers a resource specified by @a uri_path, @a res_types, @a state which have
  * @a properties in Iotcon server.\n
@@ -149,6 +174,8 @@ static void _update_brightness(int brightness)
  * @param[in] res_types The list of type of the resource
  * @param[in] properties The property of the resource\n Set of #iotcon_resource_property_e
  * @param[in] state The state handle to set
+ * @param[in] cb The callback function to add into callback list
+ * @param[in] user_data The user data to pass to the callback function
  * @param[out] resource_handle The handle of the resource
  *
  * @return 0 on success, otherwise a negative error value.
@@ -159,14 +186,14 @@ static void _update_brightness(int brightness)
  * @retval #IOTCON_ERROR_OUT_OF_MEMORY  Out of memory
  * @retval #IOTCON_ERROR_PERMISSION_DENIED Permission denied
  *
- * @post When the resource receive CRUD request, iotcon_request_handler_cb() will be called.
- *
  * @see iotcon_lite_resource_destroy()
  */
 int iotcon_lite_resource_create(const char *uri_path,
                iotcon_resource_types_h res_types,
                int properties,
                iotcon_state_h state,
+               iotcon_lite_resource_put_request_cb cb,
+               void *user_data,
                iotcon_lite_resource_h *resource_handle);
 
 /**
index d53bbf2..ad79f4b 100644 (file)
@@ -119,6 +119,23 @@ static gboolean _door_state_changer(gpointer user_data)
        return G_SOURCE_CONTINUE;
 }
 
+static bool _door_state_changed(iotcon_lite_resource_h resource,
+               iotcon_state_h state, void *user_data)
+{
+       FN_CALL;
+       bool opened;
+       int ret;
+
+       ret = iotcon_state_get_bool(state, "opened", &opened);
+       if (IOTCON_ERROR_NONE != ret) {
+               ERR("iotcon_state_get_bool() Fail(%d)", ret);
+               return false;
+       }
+       DBG("opened: %d", opened);
+
+       return true;
+}
+
 static iotcon_lite_resource_h _create_door_resource(char *uri_path, char *type,
                int properties, void *user_data)
 {
@@ -157,7 +174,7 @@ static iotcon_lite_resource_h _create_door_resource(char *uri_path, char *type,
 
        /* register door resource */
        ret = iotcon_lite_resource_create(uri_path, resource_types, properties, state,
-                       &handle);
+                       _door_state_changed, NULL, &handle);
        if (IOTCON_ERROR_NONE != ret) {
                ERR("iotcon_resource_create() Fail");
                iotcon_state_destroy(state);