2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
21 #include <iotcon-internal.h>
24 #define DOOR_RESOURCE_URI "/door/1"
25 #define DOOR_RESOURCE_TYPE "org.tizen.door"
28 typedef struct _door_resource_s {
33 iotcon_lite_resource_h handle;
36 static int _set_door_resource(door_resource_s *door)
40 door->uri_path = strdup(DOOR_RESOURCE_URI);
41 if (NULL == door->uri_path) {
42 ERR("strdup(%s) Fail", DOOR_RESOURCE_URI);
46 door->type = strdup(DOOR_RESOURCE_TYPE);
47 if (NULL == door->type) {
48 ERR("strdup(%s) Fail", DOOR_RESOURCE_TYPE);
53 door->properties = IOTCON_RESOURCE_DISCOVERABLE;
58 static void _free_door_resource(door_resource_s *door)
64 static void _check_door_state(door_resource_s door)
66 if (false == door.state)
67 INFO("[Door] closed.");
69 INFO("[Door] opened.");
72 static gboolean _door_state_changer(gpointer user_data)
75 door_resource_s *door = user_data;
76 iotcon_state_h recv_state, send_state;
78 ret = iotcon_lite_resource_get_state(door->handle, &recv_state);
79 if (IOTCON_ERROR_NONE != ret) {
80 ERR("iotcon_lite_resource_get_state() Fail(%d)", ret);
81 return G_SOURCE_CONTINUE;
84 ret = iotcon_state_get_bool(recv_state, "opened", &(door->state));
85 if (IOTCON_ERROR_NONE != ret) {
86 ERR("iotcon_state_get_bool() Fail(%d)", ret);
87 return G_SOURCE_CONTINUE;
90 if (true == door->state)
95 ret = iotcon_state_create(&send_state);
96 if (IOTCON_ERROR_NONE != ret) {
97 ERR("iotcon_state_create() Fail(%d)", ret);
98 return G_SOURCE_CONTINUE;
101 ret = iotcon_state_add_bool(send_state, "opened", door->state);
102 if (IOTCON_ERROR_NONE != ret) {
103 ERR("iotcon_state_add_bool() Fail(%d)", ret);
104 iotcon_state_destroy(send_state);
105 return G_SOURCE_CONTINUE;
108 ret = iotcon_lite_resource_update_state(door->handle, send_state);
109 if (IOTCON_ERROR_NONE != ret) {
110 ERR("iotcon_lite_resource_update_state() Fail(%d)", ret);
111 iotcon_state_destroy(send_state);
112 return G_SOURCE_CONTINUE;
115 iotcon_state_destroy(send_state);
117 _check_door_state(*door);
119 return G_SOURCE_CONTINUE;
122 static iotcon_lite_resource_h _create_door_resource(char *uri_path, char *type,
123 int properties, void *user_data)
126 iotcon_state_h state;
127 iotcon_lite_resource_h handle;
128 iotcon_resource_types_h resource_types;
130 ret = iotcon_resource_types_create(&resource_types);
131 if (IOTCON_ERROR_NONE != ret) {
132 ERR("iotcon_resource_types_create() Fail(%d)", ret);
136 ret = iotcon_resource_types_add(resource_types, type);
137 if (IOTCON_ERROR_NONE != ret) {
138 ERR("iotcon_resource_types_add() Fail(%d)", ret);
139 iotcon_resource_types_destroy(resource_types);
143 ret = iotcon_state_create(&state);
144 if (IOTCON_ERROR_NONE != ret) {
145 ERR("iotcon_state_create() Fail(%d)", ret);
146 iotcon_resource_types_destroy(resource_types);
150 ret = iotcon_state_add_bool(state, "opened", false);
151 if (IOTCON_ERROR_NONE != ret) {
152 ERR("iotcon_state_add_bool() Fail(%d)", ret);
153 iotcon_state_destroy(state);
154 iotcon_resource_types_destroy(resource_types);
158 /* register door resource */
159 ret = iotcon_lite_resource_create(uri_path, resource_types, properties, state,
161 if (IOTCON_ERROR_NONE != ret) {
162 ERR("iotcon_resource_create() Fail");
163 iotcon_state_destroy(state);
164 iotcon_resource_types_destroy(resource_types);
168 iotcon_state_destroy(state);
169 iotcon_resource_types_destroy(resource_types);
174 int main(int argc, char **argv)
179 door_resource_s my_door = {0};
181 loop = g_main_loop_new(NULL, FALSE);
184 ret = iotcon_connect();
185 if (IOTCON_ERROR_NONE != ret) {
186 ERR("iotcon_connect() Fail(%d)", ret);
191 ret = iotcon_start_presence(10);
192 if (IOTCON_ERROR_NONE != ret) {
193 ERR("iotcon_start_presence() Fail(%d)", ret);
197 /* set local door resource */
198 ret = _set_door_resource(&my_door);
200 ERR("_set_door_resource() Fail");
205 /* add resource options */
206 my_door.properties |= IOTCON_RESOURCE_OBSERVABLE;
208 /* create new door resource */
209 my_door.handle = _create_door_resource(my_door.uri_path, my_door.type,
210 my_door.properties, &my_door);
211 if (NULL == my_door.handle) {
212 ERR("_create_door_resource() Fail");
213 _free_door_resource(&my_door);
214 iotcon_stop_presence();
219 _check_door_state(my_door);
221 g_timeout_add_seconds(7, _door_state_changer, &my_door);
223 g_main_loop_run(loop);
224 g_main_loop_unref(loop);
226 iotcon_lite_resource_destroy(my_door.handle);
228 _free_door_resource(&my_door);
230 iotcon_stop_presence();
232 /* disconnect iotcon */