#include <iotcon-internal.h>
#include "test.h"
+#define DOOR_RESOURCE_URI "/door/1"
+#define DOOR_RESOURCE_URI2 "/door/2"
+#define DOOR_RESOURCE_TYPE "org.tizen.door"
+
/* Door Resource */
typedef struct _door_resource_s {
bool state;
iotcon_representation_h repr;
} door_resource_s;
-static bool resource_created;
-
-#define DOOR_RESOURCE_URI "/door/1"
-#define DOOR_RESOURCE_URI2 "/door/2"
-#define DOOR_RESOURCE_TYPE "org.tizen.door"
+static bool _resource_created;
static void _request_handler(iotcon_resource_h resource, iotcon_request_h request,
void *user_data);
ret = iotcon_resource_types_add(resource_types, type);
if (IOTCON_ERROR_NONE != ret) {
- iotcon_resource_types_destroy(resource_types);
ERR("iotcon_resource_types_add() Fail(%d)", ret);
+ iotcon_resource_types_destroy(resource_types);
return NULL;
}
ret = iotcon_resource_create(uri_path, resource_types, ifaces, properties,
_request_handler, user_data, &handle);
if (IOTCON_ERROR_NONE != ret) {
- iotcon_resource_types_destroy(resource_types);
ERR("iotcon_resource_create() Fail");
+ iotcon_resource_types_destroy(resource_types);
return NULL;
}
return handle;
}
-static int _send_response(iotcon_response_h response, iotcon_representation_h repr,
+static int _send_response(iotcon_request_h request, iotcon_representation_h repr,
iotcon_response_result_e result)
{
int ret;
+ iotcon_response_h response;
+
+ ret = iotcon_response_create(request, &response);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_response_create() Fail(%d)", ret);
+ return -1;
+ }
ret = iotcon_response_set_result(response, result);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_response_set_result() Fail(%d)", ret);
+ iotcon_response_destroy(response);
return -1;
}
ret = iotcon_response_set_representation(response, IOTCON_INTERFACE_DEFAULT, repr);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_response_set_representation() Fail(%d)", ret);
+ iotcon_response_destroy(response);
return -1;
}
ret = iotcon_response_send(response);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_response_send() Fail(%d)", ret);
+ iotcon_response_destroy(response);
return -1;
}
+ iotcon_response_destroy(response);
+
return 0;
}
-static void _request_handler_get(door_resource_s *door, iotcon_response_h response)
+static iotcon_representation_h _get_door_representation(door_resource_s *door)
{
int ret;
- iotcon_representation_h resp_repr;
- iotcon_state_h resp_state;
- INFO("GET request");
+ iotcon_state_h state;
+ iotcon_representation_h repr;
/* create a door Representation */
- ret = iotcon_representation_create(&resp_repr);
+ ret = iotcon_representation_create(&repr);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_representation_create() Fail(%d)", ret);
- return;
+ return NULL;
}
/* create a door state */
- ret = iotcon_state_create(&resp_state);
+ ret = iotcon_state_create(&state);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_state_create() Fail(%d)", ret);
- iotcon_representation_destroy(resp_repr);
- return;
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- ret = iotcon_representation_set_uri_path(resp_repr, door->uri_path);
+ ret = iotcon_representation_set_uri_path(repr, door->uri_path);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
- iotcon_state_destroy(resp_state);
- iotcon_representation_destroy(resp_repr);
- return;
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- ret = iotcon_state_set_bool(resp_state, "opened", door->state);
+ ret = iotcon_state_set_bool(state, "opened", door->state);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_state_set_bool() Fail(%d)", ret);
- iotcon_state_destroy(resp_state);
- iotcon_representation_destroy(resp_repr);
- return;
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- ret = iotcon_representation_set_state(resp_repr, resp_state);
+ ret = iotcon_representation_set_state(repr, state);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_representation_set_state() Fail(%d)", ret);
- iotcon_state_destroy(resp_state);
- iotcon_representation_destroy(resp_repr);
- return;
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- iotcon_state_destroy(resp_state);
+ iotcon_state_destroy(state);
- ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK);
- if (0 != ret)
+ return repr;
+}
+
+static int _request_handler_get(door_resource_s *door, iotcon_request_h request)
+{
+ int ret;
+ iotcon_representation_h resp_repr;
+ INFO("GET request");
+
+ resp_repr = _get_door_representation(door);
+ if (NULL == resp_repr) {
+ ERR("_get_door_representation() Fail");
+ return -1;
+ }
+
+ ret = _send_response(request, resp_repr, IOTCON_RESPONSE_RESULT_OK);
+ if (0 != ret) {
ERR("_send_response() Fail(%d)", ret);
+ iotcon_representation_destroy(resp_repr);
+ return -1;
+ }
iotcon_representation_destroy(resp_repr);
+
+ return 0;
}
-static void _request_handler_put(door_resource_s *door, iotcon_request_h request,
- iotcon_response_h response)
+static int _set_door_representation(door_resource_s *door, iotcon_representation_h repr)
{
int ret;
bool bval;
- iotcon_representation_h req_repr, resp_repr;
- iotcon_state_h req_state, resp_state;;
- INFO("PUT request");
-
- ret = iotcon_request_get_representation(request, &req_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_request_get_representation() Fail(%d)", ret);
- return;
- }
+ iotcon_state_h state;
- ret = iotcon_representation_get_state(req_repr, &req_state);
+ ret = iotcon_representation_get_state(repr, &state);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_representation_get_state() Fail(%d)", ret);
- return;
+ return -1;
}
- ret = iotcon_state_get_bool(req_state, "opened", &bval);
+ ret = iotcon_state_get_bool(state, "opened", &bval);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_state_get_bool() Fail(%d)", ret);
- return;
+ return -1;
}
door->state = bval;
- _check_door_state(*door);
+ return 0;
+}
- ret = iotcon_representation_create(&resp_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_create() Fail(%d)", ret);
- return;
- }
+static int _request_handler_put(door_resource_s *door, iotcon_request_h request)
+{
+ int ret;
+ iotcon_representation_h req_repr, resp_repr;
+ INFO("PUT request");
- ret = iotcon_representation_set_uri_path(resp_repr, door->uri_path);
+ ret = iotcon_request_get_representation(request, &req_repr);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
- iotcon_representation_destroy(resp_repr);
- return;
+ ERR("iotcon_request_get_representation() Fail(%d)", ret);
+ return -1;
}
- ret = iotcon_state_create(&resp_state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_create() Fail(%d)", ret);
- iotcon_representation_destroy(resp_repr);
- return;
+ ret = _set_door_representation(door, req_repr);
+ if (0 != ret) {
+ ERR("_set_door_representation() Fail(%d)", ret);
+ return -1;
}
- ret = iotcon_state_set_bool(resp_state, "opened", door->state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_bool() Fail(%d)", ret);
- iotcon_state_destroy(resp_state);
- iotcon_representation_destroy(resp_repr);
- return;
- }
+ _check_door_state(*door);
- ret = iotcon_representation_set_state(resp_repr, resp_state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_state() Fail(%d)", ret);
- iotcon_state_destroy(resp_state);
- iotcon_representation_destroy(resp_repr);
- return;
+ resp_repr = _get_door_representation(door);
+ if (NULL == resp_repr) {
+ ERR("_get_door_representation() Fail");
+ return -1;
}
- iotcon_state_destroy(resp_state);
-
- ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_OK);
+ ret = _send_response(request, resp_repr, IOTCON_RESPONSE_RESULT_OK);
if (0 != ret) {
ERR("_send_response() Fail(%d)", ret);
iotcon_representation_destroy(resp_repr);
- return;
+ return -1;
}
/* notify */
ERR("iotcon_resource_notify() Fail(%d)", ret);
iotcon_representation_destroy(resp_repr);
+
+ return 0;
}
static gboolean _door_state_changer(gpointer user_data)
{
int ret;
static int i = 0;
- door_resource_s *door = user_data;
- iotcon_state_h state;
iotcon_representation_h repr;
+ door_resource_s *door = user_data;
if ((5 == i++) || NULL == door->observers)
return G_SOURCE_REMOVE;
INFO("[Door] opened -> closed");
}
- ret = iotcon_representation_create(&repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_create() Fail(%d)", ret);
- return G_SOURCE_REMOVE;
- }
-
- ret = iotcon_state_create(&state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_create() Fail(%d)", ret);
- iotcon_representation_destroy(repr);
- return G_SOURCE_REMOVE;
- }
-
- ret = iotcon_state_set_bool(state, "opened", door->state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_bool() Fail(%d)", ret);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
- return G_SOURCE_REMOVE;
- }
+ INFO("NOTIFY!");
- ret = iotcon_representation_set_state(repr, state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_state() Fail(%d)", ret);
- iotcon_state_destroy(state);
- iotcon_representation_destroy(repr);
+ repr = _get_door_representation(door);
+ if (NULL == repr) {
+ ERR("_get_door_representation() Fail");
return G_SOURCE_REMOVE;
}
- iotcon_state_destroy(state);
-
- INFO("NOTIFY!");
-
ret = iotcon_resource_notify(door->handle, repr, door->observers);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_resource_notify() Fail(%d)", ret);
return G_SOURCE_CONTINUE;
}
-static void _request_handler_post(door_resource_s *door, iotcon_response_h response)
+static int _request_handler_post(door_resource_s *door, iotcon_request_h request)
{
int ret;
iotcon_state_h resp_state;
iotcon_resource_h new_door_handle;
INFO("POST request");
- if (resource_created) {
- INFO("Resource is already created");
- return;
+ if (_resource_created) {
+ ERR("Resource(%s) is already created", DOOR_RESOURCE_URI2);
+ return -1;
}
new_door_handle = _create_door_resource(DOOR_RESOURCE_URI2, door->type,
IOTCON_INTERFACE_DEFAULT, (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), NULL);
if (NULL == new_door_handle) {
ERR("_create_door_resource() Fail");
- return;
+ return -1;
}
- resource_created = true;
+ _resource_created = true;
/* send information that new resource was created */
ret = iotcon_representation_create(&resp_repr);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_representation_create() Fail(%d)", ret);
- return;
+ return -1;
}
ret = iotcon_state_create(&resp_state);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_state_create() Fail(%d)", ret);
iotcon_representation_destroy(resp_repr);
- return;
+ return -1;
}
ret = iotcon_state_set_str(resp_state, "createduripath", DOOR_RESOURCE_URI2);
ERR("iotcon_state_set_str() Fail(%d)", ret);
iotcon_state_destroy(resp_state);
iotcon_representation_destroy(resp_repr);
- return;
+ return -1;
}
ret = iotcon_representation_set_state(resp_repr, resp_state);
ERR("iotcon_representation_set_state() Fail(%d)", ret);
iotcon_state_destroy(resp_state);
iotcon_representation_destroy(resp_repr);
- return;
+ return -1;
}
iotcon_state_destroy(resp_state);
- ret = _send_response(response, resp_repr, IOTCON_RESPONSE_RESULT_RESOURCE_CREATED);
+ ret = _send_response(request, resp_repr, IOTCON_RESPONSE_RESULT_RESOURCE_CREATED);
if (0 != ret) {
ERR("_send_response() Fail(%d)", ret);
iotcon_representation_destroy(resp_repr);
- return;
+ return -1;
}
iotcon_representation_destroy(resp_repr);
- /* add observe */
- g_timeout_add_seconds(5, _door_state_changer, door);
+ return 0;
}
-static void _request_handler_delete(iotcon_resource_h resource,
- iotcon_response_h response)
+static int _request_handler_delete(iotcon_resource_h resource, iotcon_request_h request)
{
int ret;
- iotcon_representation_h resp_repr = NULL;
- iotcon_response_result_e result = IOTCON_RESPONSE_RESULT_OK;
INFO("DELETE request");
ret = iotcon_resource_destroy(resource);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_unregiser_resource() Fail(%d)", ret);
- return;
+ return -1;
}
- result = IOTCON_RESPONSE_RESULT_RESOURCE_DELETED;
+ ret = _send_response(request, NULL, IOTCON_RESPONSE_RESULT_RESOURCE_DELETED);
+ if (0 != ret) {
+ ERR("_send_response() Fail(%d)", ret);
+ return -1;
+ }
- ret = iotcon_representation_create(&resp_repr);
+ return 0;
+}
+
+static int _request_handler_observe(door_resource_s *door, iotcon_request_h request)
+{
+ int ret;
+ int observe_id;
+ iotcon_observe_action_e observe_action;
+
+ ret = iotcon_request_get_observe_action(request, &observe_action);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_create() Fail(%d)", ret);
- return;
+ ERR("iotcon_request_get_observe_action() Fail(%d)", ret);
+ return -1;
}
- ret = _send_response(response, resp_repr, result);
- if (0 != ret)
- ERR("_send_response() Fail(%d)", ret);
+ if (IOTCON_OBSERVE_REGISTER == observe_action) {
+ ret = iotcon_request_get_observe_id(request, &observe_id);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_observe_id() Fail(%d)", ret);
+ return -1;
+ }
+ ret = iotcon_observers_add(door->observers, observe_id);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_observers_add() Fail(%d)", ret);
+ return -1;
+ }
+ } else if (IOTCON_OBSERVE_DEREGISTER == observe_action) {
+ ret = iotcon_request_get_observe_id(request, &observe_id);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_observe_id() Fail(%d)", ret);
+ return -1;
+ }
+ ret = iotcon_observers_remove(door->observers, observe_id);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_observers_remove() Fail(%d)", ret);
+ return -1;
+ }
+ }
- iotcon_representation_destroy(resp_repr);
+ return 0;
}
static bool _query_cb(const char *key, const char *value, void *user_data)
static void _request_handler(iotcon_resource_h resource, iotcon_request_h request,
void *user_data)
{
- door_resource_s *door;
- iotcon_query_h query;
- iotcon_response_h response = NULL;
- int ret, types, observe_id;
- iotcon_observe_action_e observe_action;
+ int ret, types;
char *host_address;
+ iotcon_query_h query;
+ door_resource_s *door;
RET_IF(NULL == request);
ret = iotcon_request_get_host_address(request, &host_address);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_request_get_host_address() Fail(%d)", ret);
+ _send_response(request, NULL, IOTCON_RESPONSE_RESULT_ERROR);
return;
}
INFO("host_address : %s", host_address);
ret = iotcon_request_get_query(request, &query);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_request_get_query() Fail(%d)", ret);
+ _send_response(request, NULL, IOTCON_RESPONSE_RESULT_ERROR);
return;
}
if (query)
ret = iotcon_request_get_types(request, &types);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_request_get_types() Fail(%d)", ret);
- return;
- }
-
- ret = iotcon_response_create(request, &response);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_create() Fail(%d)", ret);
+ _send_response(request, NULL, IOTCON_RESPONSE_RESULT_ERROR);
return;
}
door = user_data;
if (IOTCON_REQUEST_GET & types)
- _request_handler_get(door, response);
+ ret = _request_handler_get(door, request);
else if (IOTCON_REQUEST_PUT & types)
- _request_handler_put(door, request, response);
+ ret = _request_handler_put(door, request);
else if (IOTCON_REQUEST_POST & types)
- _request_handler_post(door, response);
+ ret = _request_handler_post(door, request);
else if (IOTCON_REQUEST_DELETE & types)
- _request_handler_delete(resource, response);
-
- iotcon_response_destroy(response);
-
- if (IOTCON_REQUEST_OBSERVE & types) {
- ret = iotcon_request_get_observe_action(request, &observe_action);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_request_get_observe_action() Fail(%d)", ret);
- return;
- }
+ ret = _request_handler_delete(resource, request);
- if (IOTCON_OBSERVE_REGISTER == observe_action) {
- ret = iotcon_request_get_observe_id(request, &observe_id);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_request_get_observe_id() Fail(%d)", ret);
- return;
- }
- ret = iotcon_observers_add(door->observers, observe_id);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_observers_add() Fail(%d)", ret);
- return;
- }
- } else if (IOTCON_OBSERVE_DEREGISTER == observe_action) {
- ret = iotcon_request_get_observe_id(request, &observe_id);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_request_get_observe_id() Fail(%d)", ret);
- return;
- }
- ret = iotcon_observers_remove(door->observers, observe_id);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_observers_remove() Fail(%d)", ret);
- return;
- }
- }
+ if (0 != ret) {
+ _send_response(request, NULL, IOTCON_RESPONSE_RESULT_ERROR);
+ return;
}
+
+ if (IOTCON_REQUEST_OBSERVE & types)
+ _request_handler_observe(door,request);
}
static gboolean _presence_timer(gpointer user_data)
_check_door_state(my_door);
+ /* add observe */
+ g_timeout_add_seconds(5, _door_state_changer, &my_door);
+
g_main_loop_run(loop);
g_main_loop_unref(loop);
#include <iotcon.h>
#include "test.h"
-#define LIGHT_RESOURCE_URI "/light/1"
-#define LIGHT_RESOURCE_TYPE "org.tizen.light"
-
#define ROOM_RESOURCE_URI "/room/1"
#define ROOM_RESOURCE_TYPE "org.tizen.room"
-
-#define SWITCH_RESOURCE_URI "/switch/1"
-
-static int _send_response(iotcon_response_h response, iotcon_representation_h repr,
- iotcon_interface_e iface)
+#define LIGHT_RESOURCE_URI "/light/1"
+#define LIGHT_RESOURCE_TYPE "org.tizen.light"
+#define FAN_RESOURCE_URI "/fan/1"
+#define FAN_RESOURCE_TYPE "org.tizen.fan"
+
+/* Light Resource */
+typedef struct _light_resource_s {
+ int brightness;
+ char *uri_path;
+ char *type;
+ int ifaces;
+ int properties;
+ iotcon_resource_h handle;
+} light_resource_s;
+
+/* Fan Resource */
+typedef struct _fan_resource_s {
+ bool state;
+ char *uri_path;
+ char *type;
+ int ifaces;
+ int properties;
+ iotcon_resource_h handle;
+} fan_resource_s;
+
+/* Room Resource */
+typedef struct _room_resource_s {
+ char *name;
+ int today_temp[5];
+ char *uri_path;
+ char *type;
+ int ifaces;
+ int properties;
+ iotcon_resource_h handle;
+ light_resource_s *child_light;
+ fan_resource_s *child_fan;
+} room_resource_s;
+
+static int _set_room_resource(room_resource_s *room)
{
- int ret;
-
- ret = iotcon_response_set_representation(response, iface, repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_set_representation() Fail(%d)", ret);
+ room->name = strdup("Michael's Room");
+ if (NULL == room->name) {
+ ERR("strdup() Fail");
return -1;
}
- ret = iotcon_response_set_result(response, IOTCON_RESPONSE_RESULT_OK);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_set_result() Fail(%d)", ret);
+ room->today_temp[0] = 13;
+ room->today_temp[1] = 19;
+ room->today_temp[2] = 24;
+ room->today_temp[3] = 21;
+ room->today_temp[4] = 14;
+
+ room->uri_path = strdup(ROOM_RESOURCE_URI);
+ if (NULL == room->uri_path) {
+ ERR("strdup(%s) Fail", ROOM_RESOURCE_URI);
+ free(room->name);
return -1;
}
- /* send Representation to the client */
- ret = iotcon_response_send(response);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_send() Fail(%d)", ret);
+ room->type = strdup(ROOM_RESOURCE_TYPE);
+ if (NULL == room->type) {
+ ERR("strdup(%s) Fail", ROOM_RESOURCE_TYPE);
+ free(room->uri_path);
+ free(room->name);
return -1;
}
+ room->ifaces = IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH;
+ room->properties = IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE;
+
return 0;
}
-static void _light_request_handler_get(iotcon_response_h response)
+static void _free_room_resource(room_resource_s *room)
{
- int ret;
- iotcon_representation_h resp_repr;
+ free(room->type);
+ free(room->uri_path);
+ free(room->name);
+}
- INFO("GET request - Light");
+static int _set_light_resource(light_resource_s *light)
+{
+ light->brightness = 50;
- /* create a light Representation */
- ret = iotcon_representation_create(&resp_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_create() Fail(%d)", ret);
- return;
+ light->uri_path = strdup(LIGHT_RESOURCE_URI);
+ if (NULL == light->uri_path) {
+ ERR("strdup(%s) Fail", LIGHT_RESOURCE_URI);
+ return -1;
}
- ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
- if (0 != ret)
- ERR("_send_response() Fail(%d)", ret);
+ light->type = strdup(LIGHT_RESOURCE_TYPE);
+ if (NULL == light->type) {
+ ERR("strdup(%s) Fail", LIGHT_RESOURCE_TYPE);
+ free(light->uri_path);
+ return -1;
+ }
+
+ light->ifaces = IOTCON_INTERFACE_DEFAULT;
+ light->properties = IOTCON_HIDDEN;
- iotcon_representation_destroy(resp_repr);
+ return 0;
}
-static void _room_request_handler_get(iotcon_request_h request,
- iotcon_response_h response)
+static void _free_light_resource(light_resource_s *light)
{
- int ret;
- iotcon_representation_h room_repr, light_repr, switch_repr;
- iotcon_state_h room_state, light_state, switch_state;
- iotcon_list_h temperature_list;
+ free(light->type);
+ free(light->uri_path);
+}
- iotcon_query_h query;
- char *query_str = NULL;
+static int _set_fan_resource(fan_resource_s *fan)
+{
+ fan->state = false;
- iotcon_interface_e iface;
+ fan->uri_path = strdup(FAN_RESOURCE_URI);
+ if (NULL == fan->uri_path) {
+ ERR("strdup(%s) Fail", FAN_RESOURCE_URI);
+ return -1;
+ }
- INFO("GET request - Room");
+ fan->type = strdup(FAN_RESOURCE_TYPE);
+ if (NULL == fan->type) {
+ ERR("strdup(%s) Fail", FAN_RESOURCE_TYPE);
+ free(fan->uri_path);
+ return -1;
+ }
- /* create a room Representation */
- ret = iotcon_representation_create(&room_repr);
+ fan->ifaces = IOTCON_INTERFACE_DEFAULT;
+ fan->properties = IOTCON_HIDDEN;
+
+ return 0;
+}
+
+static void _free_fan_resource(fan_resource_s *fan)
+{
+ free(fan->type);
+ free(fan->uri_path);
+}
+
+static iotcon_resource_h _create_resource(char *uri_path, char *type, int ifaces,
+ int properties, iotcon_request_handler_cb cb, void *user_data)
+{
+ int ret;
+ iotcon_resource_h handle;
+ iotcon_resource_types_h resource_types;
+
+ ret = iotcon_resource_types_create(&resource_types);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_create() Fail(%d)", ret);
- return;
+ ERR("iotcon_resource_types_create() Fail(%d)", ret);
+ return NULL;
}
- /* create a room state */
- ret = iotcon_state_create(&room_state);
+ ret = iotcon_resource_types_add(resource_types, type);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_create() Fail(%d)", ret);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_resource_types_add() Fail(%d)", ret);
+ iotcon_resource_types_destroy(resource_types);
+ return NULL;
}
- ret = iotcon_representation_set_uri_path(room_repr, ROOM_RESOURCE_URI);
+ ret = iotcon_resource_create(uri_path, resource_types, ifaces, properties, cb,
+ user_data, &handle);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
- iotcon_state_destroy(room_state);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_response_create() Fail(%d)", ret);
+ iotcon_resource_types_destroy(resource_types);
+ return NULL;
}
- ret = iotcon_state_set_str(room_state, "name", "Michael's Room");
+ iotcon_resource_types_destroy(resource_types);
+
+ return handle;
+}
+
+static int _send_response(iotcon_request_h request, iotcon_representation_h repr,
+ iotcon_interface_e iface, iotcon_response_result_e result)
+{
+ int ret;
+ iotcon_response_h response;
+
+ ret = iotcon_response_create(request, &response);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_str() Fail(%d)", ret);
- iotcon_state_destroy(room_state);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_response_create() Fail(%d)", ret);
+ return -1;
}
- /* set null */
- ret = iotcon_state_set_null(room_state, "null value");
+ ret = iotcon_response_set_representation(response, iface, repr);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_null() Fail(%d)", ret);
- iotcon_state_destroy(room_state);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_response_set_representation() Fail(%d)", ret);
+ iotcon_response_destroy(response);
+ return -1;
}
- ret = iotcon_list_create(IOTCON_TYPE_INT, &temperature_list);
+ ret = iotcon_response_set_result(response, result);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_create() Fail(%d)", ret);
- iotcon_state_destroy(room_state);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_response_set_result() Fail(%d)", ret);
+ iotcon_response_destroy(response);
+ return -1;
}
- ret = iotcon_list_add_int(temperature_list, 22, -1);
+ /* send Representation to the client */
+ ret = iotcon_response_send(response);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_add_int() Fail(%d)", ret);
- iotcon_state_destroy(room_state);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_response_send() Fail(%d)", ret);
+ iotcon_response_destroy(response);
+ return -1;
}
- ret = iotcon_list_add_int(temperature_list, 23, -1);
+ iotcon_response_destroy(response);
+
+ return 0;
+}
+
+static iotcon_representation_h _get_light_representation(light_resource_s *light)
+{
+ int ret;
+ iotcon_state_h state;
+ iotcon_representation_h repr;
+
+ /* create a light Representation */
+ ret = iotcon_representation_create(&repr);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_add_int() Fail(%d)", ret);
- iotcon_state_destroy(room_state);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_representation_create() Fail(%d)", ret);
+ return NULL;
}
- ret = iotcon_list_add_int(temperature_list, 24, -1);
+ ret = iotcon_representation_set_uri_path(repr, light->uri_path);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_add_int() Fail(%d)", ret);
- iotcon_state_destroy(room_state);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- ret = iotcon_list_add_int(temperature_list, 25, -1);
+ /* create a light state */
+ ret = iotcon_state_create(&state);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_add_int() Fail(%d)", ret);
- iotcon_state_destroy(room_state);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_state_create() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- ret = iotcon_list_add_int(temperature_list, 26, -1);
+ ret = iotcon_state_set_int(state, "brightness", light->brightness);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_list_add_int() Fail(%d)", ret);
- iotcon_state_destroy(room_state);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_state_set_int() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- ret = iotcon_state_set_list(room_state, "today_temp", temperature_list);
+ /* Set a light state into light Representation */
+ ret = iotcon_representation_set_state(repr, state);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_list() Fail(%d)", ret);
- iotcon_state_destroy(room_state);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_representation_set_state() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- iotcon_list_destroy(temperature_list);
+ iotcon_state_destroy(state);
- /* Set a room state into room Representation */
- ret = iotcon_representation_set_state(room_repr, room_state);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_state() Fail(%d)", ret);
- iotcon_state_destroy(room_state);
- iotcon_representation_destroy(room_repr);
- return;
+ return repr;
+}
+
+static int _light_request_handler_get(light_resource_s *light, iotcon_request_h request)
+{
+ int ret;
+ iotcon_representation_h repr;
+
+ INFO("GET request - Light");
+
+ repr = _get_light_representation(light);
+ if (NULL == repr) {
+ ERR("_get_light_representation() Fail");
+ return -1;
}
- iotcon_state_destroy(room_state);
+ ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT,
+ IOTCON_RESPONSE_RESULT_OK);
+ if (0 != ret) {
+ ERR("_send_response() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return -1;
+ }
- /* create a light Representation */
- ret = iotcon_representation_create(&light_repr);
+ iotcon_representation_destroy(repr);
+
+ return 0;
+}
+
+
+static iotcon_representation_h _get_fan_representation(fan_resource_s *fan)
+{
+ int ret;
+ iotcon_state_h state;
+ iotcon_representation_h repr;
+
+ /* create a fan Representation */
+ ret = iotcon_representation_create(&repr);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_representation_create() Fail(%d)", ret);
- iotcon_representation_destroy(room_repr);
- return;
+ return NULL;
}
- ret = iotcon_representation_set_uri_path(light_repr, LIGHT_RESOURCE_URI);
+ ret = iotcon_representation_set_uri_path(repr, fan->uri_path);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
- iotcon_representation_destroy(light_repr);
- iotcon_representation_destroy(room_repr);
- return;
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- /* create a light state */
- ret = iotcon_state_create(&light_state);
+ /* create a fan state */
+ ret = iotcon_state_create(&state);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_state_create() Fail(%d)", ret);
- iotcon_state_destroy(light_state);
- iotcon_representation_destroy(light_repr);
- iotcon_representation_destroy(room_repr);
- return;
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- ret = iotcon_state_set_int(light_state, "brightness", 50);
+ ret = iotcon_state_set_bool(state, "state", fan->state);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_int() Fail(%d)", ret);
- iotcon_state_destroy(light_state);
- iotcon_representation_destroy(light_repr);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_state_set_bool() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
/* Set a light state into light Representation */
- ret = iotcon_representation_set_state(light_repr, light_state);
+ ret = iotcon_representation_set_state(repr, state);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_representation_set_state() Fail(%d)", ret);
- iotcon_state_destroy(light_state);
- iotcon_representation_destroy(light_repr);
- iotcon_representation_destroy(room_repr);
- return;
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- iotcon_state_destroy(light_state);
+ iotcon_state_destroy(state);
- ret = iotcon_representation_append_child(room_repr, light_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_append_child() Fail(%d)", ret);
- iotcon_representation_destroy(light_repr);
- iotcon_representation_destroy(room_repr);
- return;
+ return repr;
+}
+
+static int _fan_request_handler_get(fan_resource_s *fan, iotcon_request_h request)
+{
+ int ret;
+ iotcon_representation_h repr;
+
+ INFO("GET request - Fan");
+
+ repr = _get_fan_representation(fan);
+ if (NULL == repr) {
+ ERR("_get_fan_representation() Fail");
+ return -1;
}
- iotcon_representation_destroy(light_repr);
+ ret = _send_response(request, repr, IOTCON_INTERFACE_DEFAULT,
+ IOTCON_RESPONSE_RESULT_OK);
+ if (0 != ret) {
+ ERR("_send_response() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return -1;
+ }
+
+ iotcon_representation_destroy(repr);
- /* create a switch Representation */
- ret = iotcon_representation_create(&switch_repr);
+ return 0;
+}
+
+static iotcon_representation_h _get_room_representation(room_resource_s *room)
+{
+ int ret;
+ iotcon_state_h state;
+ iotcon_list_h today_temp;
+ iotcon_representation_h repr, light_repr, fan_repr;
+
+ /* create a room Representation */
+ ret = iotcon_representation_create(&repr);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_representation_create() Fail(%d)", ret);
- iotcon_representation_destroy(room_repr);
- return;
+ return NULL;
}
- ret = iotcon_representation_set_uri_path(switch_repr, SWITCH_RESOURCE_URI);
+ ret = iotcon_representation_set_uri_path(repr, room->uri_path);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_representation_set_uri_path() Fail(%d)", ret);
- iotcon_representation_destroy(switch_repr);
- iotcon_representation_destroy(room_repr);
- return;
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- /* create a switch state */
- ret = iotcon_state_create(&switch_state);
+ /* create a room state */
+ ret = iotcon_state_create(&state);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_state_create() Fail(%d)", ret);
- iotcon_representation_destroy(switch_repr);
- iotcon_representation_destroy(room_repr);
- return;
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- ret = iotcon_state_set_bool(switch_state, "switch", false);
+ ret = iotcon_state_set_str(state, "name", room->name);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_state_set_bool() Fail(%d)", ret);
- iotcon_state_destroy(switch_state);
- iotcon_representation_destroy(switch_repr);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_state_set_str() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- /* Set a light state into light Representation */
- ret = iotcon_representation_set_state(switch_repr, switch_state);
+ /* set null */
+ ret = iotcon_state_set_null(state, "null value");
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_set_state() Fail(%d)", ret);
- iotcon_state_destroy(switch_state);
- iotcon_representation_destroy(switch_repr);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_state_set_null() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- iotcon_state_destroy(switch_state);
-
- ret = iotcon_representation_append_child(room_repr, switch_repr);
+ ret = iotcon_list_create(IOTCON_TYPE_INT, &today_temp);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_append_child() Fail(%d)", ret);
- iotcon_representation_destroy(switch_repr);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_list_create() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- iotcon_representation_destroy(switch_repr);
+ ret = iotcon_list_add_int(today_temp, room->today_temp[0], -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_add_int() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
- ret = iotcon_request_get_query(request, &query);
+ ret = iotcon_list_add_int(today_temp, room->today_temp[1], -1);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_request_get_query() Fail(%d)", ret);
- iotcon_representation_destroy(room_repr);
- return;
+ ERR("iotcon_list_add_int() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- if (query) {
- ret = iotcon_query_lookup(query, "if", &query_str);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_query_lookup() Fail(%d)", ret);
- iotcon_representation_destroy(room_repr);
- return;
- }
+ ret = iotcon_list_add_int(today_temp, room->today_temp[2], -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_add_int() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- if (query_str && (TEST_STR_EQUAL == strcmp("oic.if.b", query_str))) {
- DBG("operation for BATCH interface");
- iface = IOTCON_INTERFACE_BATCH;
- } else {
- DBG("operation for DEFAULT interface");
- iface = IOTCON_INTERFACE_DEFAULT;
+ ret = iotcon_list_add_int(today_temp, room->today_temp[3], -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_add_int() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- ret = _send_response(response, room_repr, iface);
- if (0 != ret)
- ERR("_send_response() Fail(%d)", ret);
+ ret = iotcon_list_add_int(today_temp, room->today_temp[4], -1);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_list_add_int() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
- iotcon_representation_destroy(room_repr);
-}
+ ret = iotcon_state_set_list(state, "today_temp", today_temp);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_state_set_list() Fail(%d)", ret);
+ iotcon_list_destroy(today_temp);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
-static void _request_handler_put(iotcon_request_h request, iotcon_response_h response)
-{
- int ret;
- iotcon_representation_h resp_repr;
+ iotcon_list_destroy(today_temp);
- ret = iotcon_representation_create(&resp_repr);
+ /* Set a room state into room Representation */
+ ret = iotcon_representation_set_state(repr, state);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_create() Fail(%d)", ret);
- return;
+ ERR("iotcon_representation_set_state() Fail(%d)", ret);
+ iotcon_state_destroy(state);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- INFO("PUT request");
+ iotcon_state_destroy(state);
- /* do PUT operation */
+ light_repr = _get_light_representation(room->child_light);
+ if (NULL == light_repr) {
+ ERR("_get_light_representation() fail");
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
- ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
- if (0 != ret)
- ERR("_send_response() Fail(%d)", ret);
+ ret = iotcon_representation_append_child(repr, light_repr);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_representation_append_child() Fail(%d)", ret);
+ iotcon_representation_destroy(light_repr);
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
- iotcon_representation_destroy(resp_repr);
-}
+ iotcon_representation_destroy(light_repr);
-static void _request_handler_post(iotcon_response_h response)
-{
- int ret;
- iotcon_representation_h resp_repr;
+ fan_repr = _get_fan_representation(room->child_fan);
+ if (NULL == fan_repr) {
+ ERR("_get_fan_representation() fail");
+ iotcon_representation_destroy(repr);
+ return NULL;
+ }
- ret = iotcon_representation_create(&resp_repr);
+ ret = iotcon_representation_append_child(repr, fan_repr);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_create() Fail(%d)", ret);
- return;
+ ERR("iotcon_representation_append_child() Fail(%d)", ret);
+ iotcon_representation_destroy(fan_repr);
+ iotcon_representation_destroy(repr);
+ return NULL;
}
- INFO("POST request");
-
- /* do POST operation */
-
- ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
- if (0 != ret)
- ERR("_send_response() Fail(%d)", ret);
+ iotcon_representation_destroy(fan_repr);
- iotcon_representation_destroy(resp_repr);
+ return repr;
}
-static void _request_handler_delete(iotcon_response_h response)
+static int _room_request_handler_get(room_resource_s *room, iotcon_request_h request)
{
int ret;
- iotcon_representation_h resp_repr;
+ iotcon_query_h query;
+ iotcon_representation_h repr;
+ iotcon_interface_e iface = IOTCON_INTERFACE_DEFAULT;
- ret = iotcon_representation_create(&resp_repr);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_representation_create() Fail(%d)", ret);
- return;
+ INFO("GET request - Room");
+
+ repr = _get_room_representation(room);
+ if (NULL == repr) {
+ ERR("_get_room_representation() Fail");
+ return -1;
}
- INFO("DELETE request");
+ ret = iotcon_request_get_query(request, &query);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_query() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return -1;
+ }
- /* do DELETE operation */
+ if (query) {
+ ret = iotcon_query_get_interface(query, &iface);
+ if (IOTCON_ERROR_NO_DATA == ret) {
+ iface = IOTCON_INTERFACE_DEFAULT;
+ } else if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_query_get_interface() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return -1;
+ }
+ }
- ret = _send_response(response, resp_repr, IOTCON_INTERFACE_DEFAULT);
- if (0 != ret)
+ ret = _send_response(request, repr, iface, IOTCON_RESPONSE_RESULT_OK);
+ if (0 != ret) {
ERR("_send_response() Fail(%d)", ret);
+ iotcon_representation_destroy(repr);
+ return -1;
+ }
- iotcon_representation_destroy(resp_repr);
+ iotcon_representation_destroy(repr);
+
+ return 0;
}
static void _light_request_handler(iotcon_resource_h resource, iotcon_request_h request,
void *user_data)
{
- int ret;
- int types;
- iotcon_response_h response;
+ int ret, types;
+ light_resource_s *light = user_data;
+ int iface = IOTCON_INTERFACE_DEFAULT;
RET_IF(NULL == request);
ret = iotcon_request_get_types(request, &types);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_request_get_types() Fail(%d)", ret);
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
return;
}
- ret = iotcon_response_create(request, &response);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_create() Fail");
- return;
+ if (IOTCON_REQUEST_GET & types) {
+ ret = _light_request_handler_get(light, request);
+ if (0 != ret)
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
+ } else {
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN);
}
+}
- if (IOTCON_REQUEST_GET & types)
- _light_request_handler_get(response);
-
- else if (IOTCON_REQUEST_PUT & types)
- _request_handler_put(request, response);
+static void _fan_request_handler(iotcon_resource_h resource, iotcon_request_h request,
+ void *user_data)
+{
+ int ret, types;
+ fan_resource_s *fan = user_data;
+ int iface = IOTCON_INTERFACE_DEFAULT;
- else if (IOTCON_REQUEST_POST & types)
- _request_handler_post(response);
+ RET_IF(NULL == request);
- else if (IOTCON_REQUEST_DELETE & types)
- _request_handler_delete(response);
+ ret = iotcon_request_get_types(request, &types);
+ if (IOTCON_ERROR_NONE != ret) {
+ ERR("iotcon_request_get_types() Fail(%d)", ret);
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
+ return;
+ }
- iotcon_response_destroy(response);
+ if (IOTCON_REQUEST_GET & types) {
+ ret = _fan_request_handler_get(fan, request);
+ if (0 != ret)
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
+ } else {
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN);
+ }
}
static void _room_request_handler(iotcon_resource_h resource, iotcon_request_h request,
{
FN_CALL;
int ret, types;
- iotcon_response_h response;
+ char *host_address;
+ room_resource_s *room = user_data;
+ int iface = IOTCON_INTERFACE_DEFAULT;
RET_IF(NULL == request);
- ret = iotcon_request_get_types(request, &types);
+ ret = iotcon_request_get_host_address(request, &host_address);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_request_get_types() Fail(%d)", ret);
+ ERR("iotcon_request_get_host_address() Fail(%d)", ret);
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
return;
}
+ INFO("host address : %s", host_address);
- ret = iotcon_response_create(request, &response);
+ ret = iotcon_request_get_types(request, &types);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_response_create() Fail(%d)", ret);
+ ERR("iotcon_request_get_types() Fail(%d)", ret);
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
return;
}
- if (IOTCON_REQUEST_GET & types)
- _room_request_handler_get(request, response);
-
- else if (IOTCON_REQUEST_PUT & types)
- _request_handler_put(request, response);
-
- else if (IOTCON_REQUEST_POST & types)
- _request_handler_post(response);
-
- else if (IOTCON_REQUEST_DELETE & types)
- _request_handler_delete(response);
-
- iotcon_response_destroy(response);
+ if (IOTCON_REQUEST_GET & types) {
+ ret = _room_request_handler_get(room, request);
+ if (0 != ret)
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_ERROR);
+ } else {
+ _send_response(request, NULL, iface, IOTCON_RESPONSE_RESULT_FORBIDDEN);
+ }
}
int main(int argc, char **argv)
FN_CALL;
int ret;
GMainLoop *loop;
- iotcon_resource_h room_handle, light_handle;
- iotcon_resource_types_h room_rtypes, light_rtypes;
+ room_resource_s room = {0};
+ light_resource_s light = {0};
+ fan_resource_s fan = {0};
loop = g_main_loop_new(NULL, FALSE);
return -1;
}
- /* register room resource */
- ret = iotcon_resource_types_create(&room_rtypes);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_resource_types_create() Fail(%d)", ret);
+ /* set resource */
+ ret = _set_room_resource(&room);
+ if (0 != ret) {
+ ERR("_set_room_resource() Fail(%d)", ret);
iotcon_disconnect();
return -1;
}
- ret = iotcon_resource_types_add(room_rtypes, ROOM_RESOURCE_TYPE);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_resource_types_add(%s) Fail(%d)", ROOM_RESOURCE_TYPE, ret);
- iotcon_resource_types_destroy(room_rtypes);
+
+ ret = _set_light_resource(&light);
+ if (0 != ret) {
+ ERR("_set_room_resource() Fail(%d)", ret);
+ _free_room_resource(&room);
iotcon_disconnect();
return -1;
}
- ret = iotcon_resource_create(ROOM_RESOURCE_URI, room_rtypes,
- (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH),
- (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _room_request_handler,
- NULL, &room_handle);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_resource_create() Fail(%d)", ret);
- iotcon_resource_types_destroy(room_rtypes);
+ ret = _set_fan_resource(&fan);
+ if (0 != ret) {
+ ERR("_set_room_resource() Fail(%d)", ret);
+ _free_room_resource(&room);
+ _free_light_resource(&light);
+ iotcon_disconnect();
+ return -1;
+ }
+
+ room.child_light = &light;
+ room.child_fan = &fan;
+
+ /* register room resource */
+ room.handle = _create_resource(room.uri_path, room.type, room.ifaces, room.properties,
+ _room_request_handler, &room);
+ if (NULL == room.handle) {
+ ERR("_create_resource() Fail");
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
iotcon_disconnect();
return -1;
}
- iotcon_resource_types_destroy(room_rtypes);
/* register light resource */
- ret = iotcon_resource_types_create(&light_rtypes);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_resource_types_create() Fail(%d)", ret);
- iotcon_resource_destroy(room_handle);
+ light.handle = _create_resource(light.uri_path, light.type, light.ifaces,
+ light.properties, _light_request_handler, &light);
+ if (NULL == light.handle) {
+ ERR("_create_resource() Fail");
+ iotcon_resource_destroy(room.handle);
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
iotcon_disconnect();
return -1;
}
- ret = iotcon_resource_types_add(light_rtypes, LIGHT_RESOURCE_TYPE);
+
+ ret = iotcon_resource_bind_child_resource(room.handle, light.handle);
if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_resource_types_add(%s) Fail(%d)", LIGHT_RESOURCE_TYPE, ret);
- iotcon_resource_types_destroy(light_rtypes);
- iotcon_resource_destroy(room_handle);
+ ERR("iotcon_resource_bind_child_resource() Fail");
+ iotcon_resource_destroy(light.handle);
+ iotcon_resource_destroy(room.handle);
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
iotcon_disconnect();
return -1;
}
- ret = iotcon_resource_create(LIGHT_RESOURCE_URI, light_rtypes,
- (IOTCON_INTERFACE_DEFAULT | IOTCON_INTERFACE_BATCH),
- (IOTCON_DISCOVERABLE | IOTCON_OBSERVABLE), _light_request_handler,
- NULL, &light_handle);
- if (IOTCON_ERROR_NONE != ret) {
- ERR("iotcon_resource_create() Fail");
- iotcon_resource_types_destroy(light_rtypes);
- iotcon_resource_destroy(room_handle);
+ /* register fan resource */
+ fan.handle = _create_resource(fan.uri_path, fan.type, fan.ifaces, fan.properties,
+ _fan_request_handler, &fan);
+ if (NULL == fan.handle) {
+ ERR("_create_resource() Fail");
+ iotcon_resource_destroy(light.handle);
+ iotcon_resource_destroy(room.handle);
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
iotcon_disconnect();
return -1;
}
- iotcon_resource_types_destroy(light_rtypes);
- ret = iotcon_resource_bind_child_resource(room_handle, light_handle);
+ ret = iotcon_resource_bind_child_resource(room.handle, fan.handle);
if (IOTCON_ERROR_NONE != ret) {
ERR("iotcon_resource_bind_child_resource() Fail");
- iotcon_resource_destroy(light_handle);
- iotcon_resource_destroy(room_handle);
+ iotcon_resource_destroy(fan.handle);
+ iotcon_resource_destroy(light.handle);
+ iotcon_resource_destroy(room.handle);
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
iotcon_disconnect();
return -1;
}
g_main_loop_run(loop);
g_main_loop_unref(loop);
- iotcon_resource_destroy(light_handle);
- iotcon_resource_destroy(room_handle);
+ iotcon_resource_destroy(fan.handle);
+ iotcon_resource_destroy(light.handle);
+ iotcon_resource_destroy(room.handle);
+ _free_fan_resource(&fan);
+ _free_light_resource(&light);
+ _free_room_resource(&room);
/* disconnect iotcon */
iotcon_disconnect();