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.
22 static char *room_resource_device_id;
23 static GList *device_id_list;
25 #define ROOM_RESOURCE_TYPE "org.tizen.room"
26 #define ROOM_RESOURCE_URI_PREFIX "/room"
27 #define LIGHT_RESOURCE_URI_PREFIX "/light"
28 #define FAN_RESOURCE_URI_PREFIX "/fan"
30 static bool _get_int_list_cb(int pos, const int value, void *user_data)
34 return IOTCON_FUNC_CONTINUE;
37 static void _print_repr(iotcon_representation_h recv_repr)
40 bool is_null, bool_val;
41 char *uri_path, *str_val;
42 iotcon_list_h list_val;
43 iotcon_representation_h child_repr;
44 iotcon_state_h recv_state, child_state;
45 unsigned int key_count, children_count;
47 INFO("GET request was successful");
49 DBG("[ parent representation ]");
50 ret = iotcon_representation_get_uri_path(recv_repr, &uri_path);
51 if (IOTCON_ERROR_NONE != ret) {
52 ERR("iotcon_representation_get_uri_path() Fail(%d)", ret);
55 DBG("uri_path : %s", uri_path);
57 ret = iotcon_representation_get_state(recv_repr, &recv_state);
58 if (IOTCON_ERROR_NONE != ret) {
59 ERR("iotcon_representation_get_state() Fail(%d)", ret);
63 ret = iotcon_state_get_keys_count(recv_state, &key_count);
64 if (IOTCON_ERROR_NONE != ret) {
65 ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
70 ret = iotcon_state_get_str(recv_state, "name", &str_val);
71 if (IOTCON_ERROR_NONE != ret) {
72 ERR("iotcon_state_get_str() Fail(%d)", ret);
75 DBG("name : %s", str_val);
77 ret = iotcon_state_get_list(recv_state, "today_temp", &list_val);
78 if (IOTCON_ERROR_NONE != ret) {
79 ERR("iotcon_state_get_list() Fail(%d)", ret);
83 DBG("today's temperature :");
84 ret = iotcon_list_foreach_int(list_val, _get_int_list_cb, NULL);
85 if (IOTCON_ERROR_NONE != ret) {
86 ERR("iotcon_list_foreach_int() Fail(%d)", ret);
90 ret = iotcon_state_is_null(recv_state, "null value", &is_null);
91 if (IOTCON_ERROR_NONE != ret) {
92 ERR("iotcon_state_is_null() Fail(%d)", ret);
97 DBG("null value is null");
100 ret = iotcon_representation_get_children_count(recv_repr, &children_count);
101 if (IOTCON_ERROR_NONE != ret) {
102 ERR("iotcon_representation_get_children_count() Fail(%d)", ret);
106 for (i = 0; i < children_count; i++) {
107 DBG("[ child representation ]");
109 ret = iotcon_representation_get_nth_child(recv_repr, i, &child_repr);
110 if (IOTCON_ERROR_NONE != ret) {
111 ERR("iotcon_representation_get_nth_child(%d) Fail(%d)", i, ret);
115 ret = iotcon_representation_get_uri_path(child_repr, &uri_path);
116 if (IOTCON_ERROR_NONE != ret) {
117 ERR("iotcon_representation_get_uri_path() Fail(%d)", ret);
120 DBG("uri_path : %s", uri_path);
122 ret = iotcon_representation_get_state(child_repr, &child_state);
123 if (IOTCON_ERROR_NONE != ret) {
124 ERR("iotcon_representation_get_state() Fail(%d)", ret);
128 if (TEST_STR_EQUAL == strncmp(LIGHT_RESOURCE_URI_PREFIX, uri_path,
129 strlen(LIGHT_RESOURCE_URI_PREFIX))) {
130 ret = iotcon_state_get_keys_count(child_state, &key_count);
131 if (IOTCON_ERROR_NONE != ret) {
132 ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
137 ret = iotcon_state_get_int(child_state, "brightness", &int_val);
138 if (IOTCON_ERROR_NONE != ret) {
139 ERR("iotcon_state_get_int() Fail(%d)", ret);
142 DBG("brightness : %d", int_val);
144 } else if (TEST_STR_EQUAL == strncmp(FAN_RESOURCE_URI_PREFIX, uri_path,
145 strlen(FAN_RESOURCE_URI_PREFIX))) {
146 ret = iotcon_state_get_keys_count(child_state, &key_count);
147 if (IOTCON_ERROR_NONE != ret) {
148 ERR("iotcon_state_get_keys_count() Fail(%d)", ret);
152 ret = iotcon_state_get_bool(child_state, "state", &bool_val);
153 if (IOTCON_ERROR_NONE != ret) {
154 ERR("iotcon_state_get_bool() Fail(%d)", ret);
157 DBG("state : %d", bool_val);
163 static void _on_get_2nd(iotcon_remote_resource_h resource,
165 iotcon_request_type_e request_type,
166 iotcon_response_h response,
170 iotcon_response_result_e response_result;
171 iotcon_representation_h recv_repr = NULL;
173 RETM_IF(IOTCON_ERROR_NONE != err, "Invalid err(%d)", err);
175 ret = iotcon_response_get_result(response, &response_result);
176 if (IOTCON_ERROR_NONE != ret) {
177 ERR("iotcon_response_get_result() Fail(%d)", ret);
181 ret = iotcon_response_get_representation(response, &recv_repr);
182 if (IOTCON_ERROR_NONE != ret) {
183 ERR("iotcon_response_get_representation() Fail(%d)", ret);
187 if (IOTCON_RESPONSE_RESULT_OK == response_result)
188 _print_repr(recv_repr);
190 ERR("Invalid result(%d)", response_result);
192 iotcon_remote_resource_destroy(resource);
195 static void _on_response_1st(iotcon_remote_resource_h resource,
197 iotcon_request_type_e request_type,
198 iotcon_response_h response,
202 iotcon_response_result_e response_result;
203 iotcon_query_h query_params;
204 iotcon_representation_h recv_repr = NULL;
206 RETM_IF(IOTCON_ERROR_NONE != err, "Invalid err(%d)", err);
208 ret = iotcon_response_get_result(response, &response_result);
209 if (IOTCON_ERROR_NONE != ret) {
210 ERR("iotcon_response_get_result() Fail(%d)", ret);
214 ret = iotcon_response_get_representation(response, &recv_repr);
215 if (IOTCON_ERROR_NONE != ret) {
216 ERR("iotcon_response_get_representation() Fail(%d)", ret);
220 if (IOTCON_RESPONSE_RESULT_OK == response_result)
221 _print_repr(recv_repr);
223 ERR("Invalid result(%d)", response_result);
225 ret = iotcon_query_create(&query_params);
226 if (IOTCON_ERROR_NONE != ret) {
227 ERR("iotcon_query_create() Fail(%d)", ret);
231 ret = iotcon_query_set_interface(query_params, IOTCON_INTERFACE_BATCH);
232 if (IOTCON_ERROR_NONE != ret) {
233 ERR("iotcon_query_set_interface() Fail(%d)", ret);
234 iotcon_query_destroy(query_params);
238 /* send GET request again with BATCH interface */
239 ret = iotcon_remote_resource_get(resource, query_params, _on_get_2nd, NULL);
240 if (IOTCON_ERROR_NONE != ret) {
241 ERR("iotcon_remote_resource_get() Fail(%d)", ret);
242 iotcon_query_destroy(query_params);
246 iotcon_query_destroy(query_params);
249 static bool _get_res_type_fn(const char *string, void *user_data)
251 char *resource_uri_path = user_data;
253 DBG("[%s] resource type : %s", resource_uri_path, string);
255 return IOTCON_FUNC_CONTINUE;
258 static int _device_id_compare(const void *a, const void *b)
263 static void _found_resource(iotcon_remote_resource_h resource, iotcon_error_e result,
268 char *resource_uri_path;
269 char *resource_device_id;
270 int ret, resource_interfaces;
271 iotcon_resource_types_h resource_types;
272 iotcon_remote_resource_h cloned_resource;
274 RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result(%d)", result);
276 if (NULL == resource)
279 INFO("===== resource found =====");
281 /* get the resource URI */
282 ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
283 if (IOTCON_ERROR_NONE != ret) {
284 ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret);
288 /* get the resource device id */
289 ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
290 if (IOTCON_ERROR_NONE != ret) {
291 ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret);
294 DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
296 node = g_list_find_custom(device_id_list, resource_device_id, _device_id_compare);
298 if (node && TEST_STR_EQUAL == strncmp(ROOM_RESOURCE_URI_PREFIX, resource_uri_path,
299 strlen(ROOM_RESOURCE_URI_PREFIX))) {
300 DBG("uri_path \"%s\" already found. skip !", resource_uri_path);
304 room_resource_device_id = strdup(resource_device_id);
305 if (NULL == room_resource_device_id) {
306 ERR("strdup(room_resource_device_id) Fail");
310 device_id_list = g_list_append(device_id_list, room_resource_device_id);
312 /* get the resource host address */
313 ret = iotcon_remote_resource_get_host_address(resource, &resource_host);
314 if (IOTCON_ERROR_NONE != ret) {
315 ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret);
316 device_id_list = g_list_remove(device_id_list, room_resource_device_id);
317 free(room_resource_device_id);
320 DBG("[%s] resource host : %s", resource_uri_path, resource_host);
322 /* get the resource interfaces */
323 ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
324 if (IOTCON_ERROR_NONE != ret) {
325 ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
326 device_id_list = g_list_remove(device_id_list, room_resource_device_id);
327 free(room_resource_device_id);
330 if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
331 DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
332 if (IOTCON_INTERFACE_LINK & resource_interfaces)
333 DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
334 if (IOTCON_INTERFACE_BATCH & resource_interfaces)
335 DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
336 if (IOTCON_INTERFACE_GROUP & resource_interfaces)
337 DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
339 /* get the resource types */
340 ret = iotcon_remote_resource_get_types(resource, &resource_types);
341 if (IOTCON_ERROR_NONE != ret) {
342 ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
343 device_id_list = g_list_remove(device_id_list, room_resource_device_id);
344 free(room_resource_device_id);
347 ret = iotcon_resource_types_foreach(resource_types, _get_res_type_fn,
349 if (IOTCON_ERROR_NONE != ret) {
350 ERR("iotcon_resource_types_foreach() Fail(%d)", ret);
351 device_id_list = g_list_remove(device_id_list, room_resource_device_id);
352 free(room_resource_device_id);
356 if (TEST_STR_EQUAL == strncmp(ROOM_RESOURCE_URI_PREFIX, resource_uri_path,
357 strlen(ROOM_RESOURCE_URI_PREFIX))) {
358 ret = iotcon_remote_resource_clone(resource, &cloned_resource);
359 if (IOTCON_ERROR_NONE != ret) {
360 ERR("iotcon_remote_resource_clone() Fail(%d)", ret);
361 device_id_list = g_list_remove(device_id_list, room_resource_device_id);
362 free(room_resource_device_id);
366 /* send GET request */
367 ret = iotcon_remote_resource_get(cloned_resource, NULL, _on_response_1st, NULL);
368 if (IOTCON_ERROR_NONE != ret)
369 ERR("iotcon_remote_resource_get() Fail(%d)", ret);
372 device_id_list = g_list_remove(device_id_list, room_resource_device_id);
373 free(room_resource_device_id);
376 int main(int argc, char **argv)
382 loop = g_main_loop_new(NULL, FALSE);
385 ret = iotcon_connect();
386 if (IOTCON_ERROR_NONE != ret) {
387 ERR("iotcon_connect() Fail(%d)", ret);
391 /* find room typed resources */
392 ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, IOTCON_CONNECTIVITY_IPV4,
393 ROOM_RESOURCE_TYPE, _found_resource, NULL);
394 if (IOTCON_ERROR_NONE != ret) {
395 ERR("iotcon_find_resource() Fail(%d)", ret);
400 g_main_loop_run(loop);
401 g_main_loop_unref(loop);
403 g_list_free_full(device_id_list, free);
405 /* disconnect iotcon */