Fix FC when cloning representation
[platform/core/iot/iotcon.git] / test / iotcon-test-encap-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 #include <stdlib.h>
17 #include <glib.h>
18 #include <tizen_type.h>
19
20 #include <iotcon.h>
21 #include "test.h"
22
23 static char *door_resource_device_id;
24 static GList *device_id_list;
25
26 #define DOOR_RESOURCE_TYPE "org.tizen.door"
27
28 static bool _get_res_type_cb(const char *string, void *user_data)
29 {
30         char *resource_uri_path = user_data;
31
32         DBG("[%s] resource type : %s", resource_uri_path, string);
33
34         return IOTCON_FUNC_CONTINUE;
35 }
36
37 static int _device_id_compare(const void *a, const void *b)
38 {
39         return strcmp(a, b);
40 }
41
42 static void _state_changed_cb(iotcon_remote_resource_h resource,
43                 iotcon_remote_resource_state_e state, void *user_data)
44 {
45         INFO("Resource State is Changed");
46
47         switch (state) {
48         case IOTCON_REMOTE_RESOURCE_ALIVE:
49                 INFO(" --- ALIVE");
50                 break;
51         case IOTCON_REMOTE_RESOURCE_LOST_SIGNAL:
52                 INFO(" --- LOST_SIGNAL");
53                 break;
54         default:
55                 break;
56         }
57 }
58
59 static void _representation_changed_cb(iotcon_remote_resource_h resource,
60                 iotcon_representation_h representation, void *user_data)
61 {
62         int ret;
63         bool opened;
64         iotcon_state_h state;
65
66         INFO("Resource is cached");
67
68         ret = iotcon_representation_get_state(representation, &state);
69         if (IOTCON_ERROR_NONE != ret) {
70                 ERR("iotcon_representation_get_state() Fail(%d)", ret);
71                 return;
72         }
73
74         ret = iotcon_state_get_bool(state, "opened", &opened);
75         if (IOTCON_ERROR_NONE != ret) {
76                 ERR("iotcon_state_get_bool() Fail(%d)", ret);
77                 return;
78         }
79
80         switch (opened) {
81         case true:
82                 INFO("door is opened");
83                 break;
84         case false:
85                 INFO("door is closed");
86                 break;
87         default:
88                 break;
89         }
90 }
91
92 static void _found_resource(iotcon_remote_resource_h resource, iotcon_error_e result,
93                 void *user_data)
94 {
95         GList *node;
96         char *resource_host;
97         char *resource_uri_path;
98         char *resource_device_id;
99         int ret, resource_interfaces;
100         iotcon_resource_types_h resource_types;
101         iotcon_remote_resource_h cloned_resource;
102
103         RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result(%d)", result);
104
105         if (NULL == resource)
106                 return;
107
108         INFO("===== resource found =====");
109
110         /* get the resource URI */
111         ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
112         if (IOTCON_ERROR_NONE != ret) {
113                 ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret);
114                 return;
115         }
116
117         /* get the device unique id.
118          * this is unique per-server independent on how it was discovered. */
119         ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
120         if (IOTCON_ERROR_NONE != ret) {
121                 ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret);
122                 return;
123         }
124         DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
125
126         node = g_list_find_custom(device_id_list, resource_device_id, _device_id_compare);
127
128         door_resource_device_id = strdup(resource_device_id);
129         if (NULL == door_resource_device_id) {
130                 ERR("strdup(door_resource_device_id) Fail");
131                 return;
132         }
133
134         device_id_list = g_list_append(device_id_list, door_resource_device_id);
135
136         if (node) {
137                 DBG("This device(%s) is already found. skip !", door_resource_device_id);
138                 return;
139         }
140
141         /* get the resource host address */
142         ret = iotcon_remote_resource_get_host_address(resource, &resource_host);
143         if (IOTCON_ERROR_NONE != ret) {
144                 ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret);
145                 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
146                 free(door_resource_device_id);
147                 return;
148         }
149         DBG("[%s] resource host : %s", resource_uri_path, resource_host);
150
151         /* get the resource interfaces */
152         ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
153         if (IOTCON_ERROR_NONE != ret) {
154                 ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
155                 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
156                 free(door_resource_device_id);
157                 return;
158         }
159         if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
160                 DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
161         if (IOTCON_INTERFACE_LINK & resource_interfaces)
162                 DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
163         if (IOTCON_INTERFACE_BATCH & resource_interfaces)
164                 DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
165         if (IOTCON_INTERFACE_GROUP & resource_interfaces)
166                 DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
167
168         /* get the resource types */
169         ret = iotcon_remote_resource_get_types(resource, &resource_types);
170         if (IOTCON_ERROR_NONE != ret) {
171                 ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
172                 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
173                 free(door_resource_device_id);
174                 return;
175         }
176
177         ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb,
178                         resource_uri_path);
179         if (IOTCON_ERROR_NONE != ret) {
180                 ERR("iotcon_resource_types_foreach() Fail(%d)", ret);
181                 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
182                 free(door_resource_device_id);
183                 return;
184         }
185
186         ret = iotcon_remote_resource_clone(resource, &cloned_resource);
187         if (IOTCON_ERROR_NONE != ret) {
188                 ERR("iotcon_remote_resource_clone() Fail(%d)", ret);
189                 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
190                 free(door_resource_device_id);
191                 return;
192         }
193
194         /* Start Monitoring */
195         ret = iotcon_remote_resource_start_monitoring(cloned_resource, _state_changed_cb,
196                         NULL);
197         if (IOTCON_ERROR_NONE != ret) {
198                 ERR("iotcon_remote_resource_start_monitoring() Fail(%d)", ret);
199                 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
200                 free(door_resource_device_id);
201                 return;
202         }
203
204         /* Start Caching */
205         ret = iotcon_remote_resource_start_caching(cloned_resource,
206                         _representation_changed_cb, NULL);
207         if (IOTCON_ERROR_NONE != ret) {
208                 ERR("iotcon_remote_resource_start_caching() Fail(%d)", ret);
209                 iotcon_remote_resource_stop_monitoring(resource);
210                 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
211                 free(door_resource_device_id);
212                 return;
213         }
214
215         device_id_list = g_list_remove(device_id_list, door_resource_device_id);
216         free(door_resource_device_id);
217 }
218
219 int main(int argc, char **argv)
220 {
221         FN_CALL;
222         int ret;
223         GMainLoop *loop;
224         iotcon_remote_resource_h resource;
225
226         loop = g_main_loop_new(NULL, FALSE);
227
228         /* connect iotcon */
229         ret = iotcon_connect();
230         if (IOTCON_ERROR_NONE != ret) {
231                 ERR("iotcon_connect() Fail(%d)", ret);
232                 return -1;
233         }
234
235         /* find door typed resources */
236         ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, IOTCON_CONNECTIVITY_IPV4,
237                         DOOR_RESOURCE_TYPE, false, _found_resource, &resource);
238         if (IOTCON_ERROR_NONE != ret) {
239                 ERR("iotcon_find_resource() Fail(%d)", ret);
240                 iotcon_disconnect();
241                 return -1;
242         }
243
244         g_main_loop_run(loop);
245         g_main_loop_unref(loop);
246
247         g_list_free_full(device_id_list, free);
248
249         /* disconnect iotcon */
250         iotcon_disconnect();
251
252         return 0;
253 }