0a2b1f8c25889c86cf1621e9d8cc7eafba9781cf
[platform/core/iot/iotcon.git] / test / iotcon-test-encap-server.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
20 #include <iotcon.h>
21 #include <iotcon-internal.h>
22 #include "test.h"
23
24 #define DOOR_RESOURCE_URI "/door/1"
25 #define DOOR_RESOURCE_TYPE "org.tizen.door"
26
27 /* Door Resource */
28 typedef struct _door_resource_s {
29         bool state;
30         char *uri_path;
31         char *type;
32         int properties;
33         iotcon_lite_resource_h handle;
34 } door_resource_s;
35
36 static int _set_door_resource(door_resource_s *door)
37 {
38         door->state = false;
39
40         door->uri_path = strdup(DOOR_RESOURCE_URI);
41         if (NULL == door->uri_path) {
42                 ERR("strdup(%s) Fail", DOOR_RESOURCE_URI);
43                 return -1;
44         }
45
46         door->type = strdup(DOOR_RESOURCE_TYPE);
47         if (NULL == door->type) {
48                 ERR("strdup(%s) Fail", DOOR_RESOURCE_TYPE);
49                 free(door->uri_path);
50                 return -1;
51         }
52
53         door->properties = IOTCON_RESOURCE_DISCOVERABLE;
54
55         return 0;
56 }
57
58 static void _free_door_resource(door_resource_s *door)
59 {
60         free(door->type);
61         free(door->uri_path);
62 }
63
64 static void _check_door_state(door_resource_s door)
65 {
66         if (false == door.state)
67                 INFO("[Door] closed.");
68         else
69                 INFO("[Door] opened.");
70 }
71
72 static gboolean _door_state_changer(gpointer user_data)
73 {
74         int ret;
75         door_resource_s *door = user_data;
76         iotcon_state_h recv_state, send_state;
77
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;
82         }
83
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;
88         }
89
90         if (true == door->state)
91                 door->state = false;
92         else
93                 door->state = true;
94
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;
99         }
100
101         ret = iotcon_state_set_bool(send_state, "opened", door->state);
102         if (IOTCON_ERROR_NONE != ret) {
103                 ERR("iotcon_state_set_bool() Fail(%d)", ret);
104                 iotcon_state_destroy(send_state);
105                 return G_SOURCE_CONTINUE;
106         }
107
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;
113         }
114
115         iotcon_state_destroy(send_state);
116
117         _check_door_state(*door);
118
119         return G_SOURCE_CONTINUE;
120 }
121
122 static iotcon_lite_resource_h _create_door_resource(char *uri_path, char *type,
123                 int properties, void *user_data)
124 {
125         int ret;
126         iotcon_state_h state;
127         iotcon_lite_resource_h handle;
128         iotcon_resource_types_h resource_types;
129
130         ret = iotcon_resource_types_create(&resource_types);
131         if (IOTCON_ERROR_NONE != ret) {
132                 ERR("iotcon_resource_types_create() Fail(%d)", ret);
133                 return NULL;
134         }
135
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);
140                 return NULL;
141         }
142
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);
147                 return NULL;
148         }
149
150         ret = iotcon_state_set_bool(state, "opened", false);
151         if (IOTCON_ERROR_NONE != ret) {
152                 ERR("iotcon_state_set_bool() Fail(%d)", ret);
153                 iotcon_state_destroy(state);
154                 iotcon_resource_types_destroy(resource_types);
155                 return NULL;
156         }
157
158         /* register door resource */
159         ret = iotcon_lite_resource_create(uri_path, resource_types, properties, state,
160                         &handle);
161         if (IOTCON_ERROR_NONE != ret) {
162                 ERR("iotcon_resource_create() Fail");
163                 iotcon_state_destroy(state);
164                 iotcon_resource_types_destroy(resource_types);
165                 return NULL;
166         }
167
168         iotcon_state_destroy(state);
169         iotcon_resource_types_destroy(resource_types);
170
171         return handle;
172 }
173
174 int main(int argc, char **argv)
175 {
176         FN_CALL;
177         int ret;
178         GMainLoop *loop;
179         door_resource_s my_door = {0};
180
181         loop = g_main_loop_new(NULL, FALSE);
182
183         /* connect iotcon */
184         ret = iotcon_connect();
185         if (IOTCON_ERROR_NONE != ret) {
186                 ERR("iotcon_connect() Fail(%d)", ret);
187                 return -1;
188         }
189
190         /* start presence */
191         ret = iotcon_start_presence(10);
192         if (IOTCON_ERROR_NONE != ret) {
193                 ERR("iotcon_start_presence() Fail(%d)", ret);
194                 return -1;
195         }
196
197         /* set local door resource */
198         ret = _set_door_resource(&my_door);
199         if (0 != ret) {
200                 ERR("_set_door_resource() Fail");
201                 iotcon_disconnect();
202                 return -1;
203         }
204
205         /* add resource options */
206         my_door.properties |= IOTCON_RESOURCE_OBSERVABLE;
207
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();
215                 iotcon_disconnect();
216                 return -1;
217         }
218
219         _check_door_state(my_door);
220
221         g_timeout_add_seconds(7, _door_state_changer, &my_door);
222
223         g_main_loop_run(loop);
224         g_main_loop_unref(loop);
225
226         iotcon_lite_resource_destroy(my_door.handle);
227
228         _free_door_resource(&my_door);
229
230         iotcon_stop_presence();
231
232         /* disconnect iotcon */
233         iotcon_disconnect();
234
235         return 0;
236 }