Add example on doxygen
[platform/core/iot/iotcon.git] / test / iotcon-test-repr-client.c
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <stdlib.h>
18 #include <glib.h>
19 #include <iotcon.h>
20 #include "test.h"
21
22 static char *room_resource_device_id;
23 static GList *device_id_list;
24
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"
29
30 static bool _get_int_list_cb(int pos, const int value, void *user_data)
31 {
32         DBG("%d", value);
33
34         return IOTCON_FUNC_CONTINUE;
35 }
36
37 static void _print_repr(iotcon_representation_h recv_repr)
38 {
39         int i, ret, int_val;
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;
46
47         INFO("GET request was successful");
48
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);
53                 return;
54         }
55         DBG("uri_path : %s", uri_path);
56
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);
60                 return;
61         }
62
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);
66                 return;
67         }
68
69         if (key_count) {
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);
73                         return;
74                 }
75                 DBG("name : %s", str_val);
76
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);
80                         return;
81                 }
82
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);
87                         return;
88                 }
89
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);
93                         return;
94                 }
95
96                 if (is_null)
97                         DBG("null value is null");
98         }
99
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);
103                 return;
104         }
105
106         for (i = 0; i < children_count; i++) {
107                 DBG("[ child representation ]");
108
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);
112                         continue;
113                 }
114
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);
118                         continue;
119                 }
120                 DBG("uri_path : %s", uri_path);
121
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);
125                         continue;
126                 }
127
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);
133                                 continue;
134                         }
135
136                         if (key_count) {
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);
140                                         continue;
141                                 }
142                                 DBG("brightness : %d", int_val);
143                         }
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);
149                                 continue;
150                         }
151                         if (key_count) {
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);
155                                         continue;
156                                 }
157                                 DBG("state : %d", bool_val);
158                         }
159                 }
160         }
161 }
162
163 static void _on_get_2nd(iotcon_remote_resource_h resource,
164                 iotcon_error_e err,
165                 iotcon_request_type_e request_type,
166                 iotcon_response_h response,
167                 void *user_data)
168 {
169         int ret;
170         iotcon_response_result_e response_result;
171         iotcon_representation_h recv_repr = NULL;
172
173         RETM_IF(IOTCON_ERROR_NONE != err, "Invalid err(%d)", err);
174
175         ret = iotcon_response_get_result(response, &response_result);
176         if (IOTCON_ERROR_NONE != ret) {
177                 ERR("iotcon_response_get_result() Fail(%d)", ret);
178                 return;
179         }
180
181         ret = iotcon_response_get_representation(response, &recv_repr);
182         if (IOTCON_ERROR_NONE != ret) {
183                 ERR("iotcon_response_get_representation() Fail(%d)", ret);
184                 return;
185         }
186
187         if (IOTCON_RESPONSE_RESULT_OK == response_result)
188                 _print_repr(recv_repr);
189         else
190                 ERR("Invalid result(%d)", response_result);
191
192         iotcon_remote_resource_destroy(resource);
193 }
194
195 static void _on_response_1st(iotcon_remote_resource_h resource,
196                 iotcon_error_e err,
197                 iotcon_request_type_e request_type,
198                 iotcon_response_h response,
199                 void *user_data)
200 {
201         int ret;
202         iotcon_response_result_e response_result;
203         iotcon_query_h query_params;
204         iotcon_representation_h recv_repr = NULL;
205
206         RETM_IF(IOTCON_ERROR_NONE != err, "Invalid err(%d)", err);
207
208         ret = iotcon_response_get_result(response, &response_result);
209         if (IOTCON_ERROR_NONE != ret) {
210                 ERR("iotcon_response_get_result() Fail(%d)", ret);
211                 return;
212         }
213
214         ret = iotcon_response_get_representation(response, &recv_repr);
215         if (IOTCON_ERROR_NONE != ret) {
216                 ERR("iotcon_response_get_representation() Fail(%d)", ret);
217                 return;
218         }
219
220         if (IOTCON_RESPONSE_RESULT_OK == response_result)
221                 _print_repr(recv_repr);
222         else
223                 ERR("Invalid result(%d)", response_result);
224
225         ret = iotcon_query_create(&query_params);
226         if (IOTCON_ERROR_NONE != ret) {
227                 ERR("iotcon_query_create() Fail(%d)", ret);
228                 return;
229         }
230
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);
235                 return;
236         }
237
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);
243                 return;
244         }
245
246         iotcon_query_destroy(query_params);
247 }
248
249 static bool _get_res_type_fn(const char *string, void *user_data)
250 {
251         char *resource_uri_path = user_data;
252
253         DBG("[%s] resource type : %s", resource_uri_path, string);
254
255         return IOTCON_FUNC_CONTINUE;
256 }
257
258 static int _device_id_compare(const void *a, const void *b)
259 {
260         return strcmp(a, b);
261 }
262
263 static void _found_resource(iotcon_remote_resource_h resource, iotcon_error_e result,
264                 void *user_data)
265 {
266         GList *node;
267         char *resource_host;
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;
273
274         RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result(%d)", result);
275
276         if (NULL == resource)
277                 return;
278
279         INFO("===== resource found =====");
280
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);
285                 return;
286         }
287
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);
292                 return;
293         }
294         DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
295
296         node = g_list_find_custom(device_id_list, resource_device_id, _device_id_compare);
297
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);
301                 return;
302         }
303
304         room_resource_device_id = strdup(resource_device_id);
305         if (NULL == room_resource_device_id) {
306                 ERR("strdup(room_resource_device_id) Fail");
307                 return;
308         }
309
310         device_id_list = g_list_append(device_id_list, room_resource_device_id);
311
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);
318                 return;
319         }
320         DBG("[%s] resource host : %s", resource_uri_path, resource_host);
321
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);
328                 return;
329         }
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);
338
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);
345                 return;
346         }
347         ret = iotcon_resource_types_foreach(resource_types, _get_res_type_fn,
348                         resource_uri_path);
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);
353                 return;
354         }
355
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);
363                         return;
364                 }
365
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);
370         }
371
372         device_id_list = g_list_remove(device_id_list, room_resource_device_id);
373         free(room_resource_device_id);
374 }
375
376 int main(int argc, char **argv)
377 {
378         FN_CALL;
379         int ret;
380         GMainLoop *loop;
381
382         loop = g_main_loop_new(NULL, FALSE);
383
384         /* connect iotcon */
385         ret = iotcon_connect();
386         if (IOTCON_ERROR_NONE != ret) {
387                 ERR("iotcon_connect() Fail(%d)", ret);
388                 return -1;
389         }
390
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);
396                 iotcon_disconnect();
397                 return -1;
398         }
399
400         g_main_loop_run(loop);
401         g_main_loop_unref(loop);
402
403         g_list_free_full(device_id_list, free);
404
405         /* disconnect iotcon */
406         iotcon_disconnect();
407
408         return 0;
409 }