Connectivity : Initial Codes
[apps/native/position-finder-server.git] / src / connectivity.c
1 /*
2  * Copyright (c) 2015 - 2016 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 <Eina.h>
20
21 #include <iotcon.h>
22
23 #include "log.h"
24 #include "connectivity.h"
25
26 #define ULTRASONIC_RESOURCE_1_URI "/door/1"
27 #define ULTRASONIC_RESOURCE_2_URI "/door/2"
28 #define ULTRASONIC_RESOURCE_TYPE "org.tizen.door"
29
30 struct connectivity_resource {
31         iotcon_resource_h res;
32         iotcon_observers_h observers;
33 };
34 static bool _resource_created;
35 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data);
36
37 static int _send_response(iotcon_request_h request, iotcon_representation_h representation, iotcon_response_result_e result)
38 {
39         int ret = -1;
40         iotcon_response_h response;
41
42         ret = iotcon_response_create(request, &response);
43         retv_if(IOTCON_ERROR_NONE != ret, -1);
44
45         ret = iotcon_response_set_result(response, result);
46         goto_if(IOTCON_ERROR_NONE != ret, error);
47
48         ret = iotcon_response_set_representation(response, representation);
49         goto_if(IOTCON_ERROR_NONE != ret, error);
50
51         ret = iotcon_response_send(response);
52         goto_if(IOTCON_ERROR_NONE != ret, error);
53
54         iotcon_response_destroy(response);
55
56         return 0;
57
58 error:
59         iotcon_response_destroy(response);
60         return -1;
61 }
62
63 static void _destroy_representation(iotcon_representation_h representation)
64 {
65         ret_if(!representation);
66         iotcon_representation_destroy(representation);
67 }
68
69 static iotcon_representation_h _create_representation_with_attribute(iotcon_resource_h res, bool value)
70 {
71         iotcon_attributes_h attributes = NULL;
72         iotcon_representation_h representation = NULL;
73         char *uri_path = NULL;
74         int ret = -1;
75
76         ret = iotcon_resource_get_uri_path(res, &uri_path);
77         retv_if(IOTCON_ERROR_NONE != ret, NULL);
78
79         ret = iotcon_representation_create(&representation);
80         retv_if(IOTCON_ERROR_NONE != ret, NULL);
81
82         ret = iotcon_attributes_create(&attributes);
83         goto_if(IOTCON_ERROR_NONE != ret, error);
84
85         ret = iotcon_representation_set_uri_path(representation, uri_path);
86         goto_if(IOTCON_ERROR_NONE != ret, error);
87
88         ret = iotcon_attributes_add_bool(attributes, "opened", value);
89         goto_if(IOTCON_ERROR_NONE != ret, error);
90
91         ret = iotcon_representation_set_attributes(representation, attributes);
92         goto_if(IOTCON_ERROR_NONE != ret, error);
93
94         iotcon_attributes_destroy(attributes);
95
96         return representation;
97
98 error:
99         if (attributes) iotcon_attributes_destroy(attributes);
100         if (representation) iotcon_representation_destroy(representation);
101
102         return NULL;
103 }
104
105 static int _handle_get_request(iotcon_resource_h res, iotcon_request_h request)
106 {
107         iotcon_representation_h representation;
108         int ret = -1;
109         int value = 1;
110
111         /* FIXME : We need to check the value of sensors */
112         representation = _create_representation_with_attribute(res, (bool)value);
113         retv_if(!representation, -1);
114
115         ret = _send_response(request, representation, IOTCON_RESPONSE_OK);
116         goto_if(0 != ret, error);
117
118         _destroy_representation(representation);
119
120         return 0;
121
122 error:
123         _destroy_representation(representation);
124         return -1;
125 }
126
127 static int _get_value_from_representation(iotcon_representation_h representation, bool *value)
128 {
129         iotcon_attributes_h attributes;
130         int ret = -1;
131
132         ret = iotcon_representation_get_attributes(representation, &attributes);
133         retv_if(IOTCON_ERROR_NONE != ret, -1);
134
135         ret = iotcon_attributes_get_bool(attributes, "opened", value);
136         retv_if(IOTCON_ERROR_NONE != ret, -1);
137
138         return 0;
139 }
140
141 static int _set_value_into_thing(iotcon_representation_h representation, bool value)
142 {
143         /* FIXME : We need to set the value into the thing */
144         return 0;
145 }
146
147 static int _handle_put_request(connectivity_resource_s *resource_info, iotcon_request_h request)
148 {
149         iotcon_representation_h req_repr, resp_repr;
150         int ret = -1;
151         bool value = false;
152
153         _D("PUT request");
154
155         ret = iotcon_request_get_representation(request, &req_repr);
156         retv_if(IOTCON_ERROR_NONE != ret, -1);
157
158         ret = _get_value_from_representation(req_repr, &value);
159         retv_if(0 != ret, -1);
160
161         ret = _set_value_into_thing(req_repr, value);
162         retv_if(0 != ret, -1);
163
164         resp_repr = _create_representation_with_attribute(resource_info->res, (bool)value);
165         retv_if(NULL == resp_repr, -1);
166
167         ret = _send_response(request, resp_repr, IOTCON_RESPONSE_OK);
168         goto_if(0 != ret, error);
169
170         ret = iotcon_resource_notify(resource_info->res, resp_repr, resource_info->observers, IOTCON_QOS_HIGH);
171         goto_if(IOTCON_ERROR_NONE != ret, error);
172
173         _destroy_representation(resp_repr);
174
175         return 0;
176
177 error:
178         _destroy_representation(resp_repr);
179         return -1;
180 }
181
182 int connectivity_notify(connectivity_resource_s *user_data, int value)
183 {
184         iotcon_representation_h representation;
185         connectivity_resource_s *resource_info = user_data;
186         int ret = -1;
187
188         retv_if(!resource_info, -1);
189         retv_if(!resource_info->observers, -1);
190
191         _D("Notify the value[%d]", value);
192
193         representation = _create_representation_with_attribute(resource_info->res, (bool)value);
194         retv_if(!representation, -1);
195
196         ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
197         retv_if(IOTCON_ERROR_NONE != ret, -1);
198
199         _destroy_representation(representation);
200
201         return 0;
202 }
203
204 static int _handle_post_request(connectivity_resource_s *resource_info, iotcon_request_h request)
205 {
206         iotcon_attributes_h resp_attributes = NULL;
207         iotcon_representation_h resp_repr = NULL;
208         connectivity_resource_s *new_resource_info = NULL;
209         int ret = -1;
210
211         _D("POST request");
212
213         if (_resource_created) {
214                 _E("Resource(%s) is already created", ULTRASONIC_RESOURCE_2_URI);
215                 return -1;
216         }
217
218         new_resource_info = calloc(1, sizeof(connectivity_resource_s));
219         retv_if(!new_resource_info, -1);
220
221         ret = connectivity_create_resource(ULTRASONIC_RESOURCE_2_URI, ULTRASONIC_RESOURCE_TYPE, &new_resource_info);
222         retv_if(0 != ret, -1);
223
224         _resource_created = true;
225
226         ret = iotcon_representation_create(&resp_repr);
227         retv_if(IOTCON_ERROR_NONE != ret, -1);
228
229         ret = iotcon_attributes_create(&resp_attributes);
230         goto_if(IOTCON_ERROR_NONE != ret, error);
231
232         ret = iotcon_attributes_add_str(resp_attributes, "createduripath", ULTRASONIC_RESOURCE_2_URI);
233         goto_if(IOTCON_ERROR_NONE != ret, error);
234
235         ret = iotcon_representation_set_attributes(resp_repr, resp_attributes);
236         goto_if(IOTCON_ERROR_NONE != ret, error);
237
238         iotcon_attributes_destroy(resp_attributes);
239
240         ret = _send_response(request, resp_repr, IOTCON_RESPONSE_RESOURCE_CREATED);
241         goto_if(0 != ret, error);
242
243         iotcon_representation_destroy(resp_repr);
244
245         return 0;
246
247 error:
248         if (resp_attributes) iotcon_attributes_destroy(resp_attributes);
249         iotcon_representation_destroy(resp_repr);
250         return -1;
251 }
252
253 static int _handle_delete_request(iotcon_resource_h resource, iotcon_request_h request)
254 {
255         int ret = -1;
256
257         _D("DELETE request");
258
259         ret = iotcon_resource_destroy(resource);
260         retv_if(IOTCON_ERROR_NONE != ret, -1);
261
262         ret = _send_response(request, NULL, IOTCON_RESPONSE_RESOURCE_DELETED);
263         retv_if(0 != ret, -1);
264
265         return 0;
266 }
267
268 static bool _query_cb(const char *key, const char *value, void *user_data)
269 {
270         _D("Key : [%s], Value : [%s]", key, value);
271
272         return IOTCON_FUNC_CONTINUE;
273 }
274
275 static int _handle_query(iotcon_request_h request)
276 {
277         iotcon_query_h query = NULL;
278         int ret = -1;
279
280         ret = iotcon_request_get_query(request, &query);
281         retv_if(IOTCON_ERROR_NONE != ret, -1);
282
283         if (query) iotcon_query_foreach(query, _query_cb, NULL);
284
285         return 0;
286 }
287
288 static int _handle_request_by_crud_type(iotcon_request_h request, connectivity_resource_s *resource_info)
289 {
290         iotcon_request_type_e type;
291         int ret = -1;
292
293         ret = iotcon_request_get_request_type(request, &type);
294         retv_if(IOTCON_ERROR_NONE != ret, -1);
295
296         switch (type) {
297         case IOTCON_REQUEST_GET:
298                 ret = _handle_get_request(resource_info->res, request);
299                 break;
300         case IOTCON_REQUEST_PUT:
301                 ret = _handle_put_request(resource_info, request);
302                 break;
303         case IOTCON_REQUEST_POST:
304                 ret = _handle_post_request(resource_info, request);
305                 break;
306         case IOTCON_REQUEST_DELETE:
307                 ret = _handle_delete_request(resource_info->res, request);
308                 break;
309         default:
310                 _E("Cannot reach here");
311                 ret = -1;
312                 break;
313         }
314         retv_if(0 != ret, -1);
315
316         return 0;
317 }
318
319 static int _handle_observer(iotcon_request_h request, iotcon_observers_h observers)
320 {
321         iotcon_observe_type_e observe_type;
322         int ret = -1;
323         int observe_id = -1;
324
325         ret = iotcon_request_get_observe_type(request, &observe_type);
326         retv_if(IOTCON_ERROR_NONE != ret, -1);
327
328         if (IOTCON_OBSERVE_REGISTER == observe_type) {
329                 ret = iotcon_request_get_observe_id(request, &observe_id);
330                 retv_if(IOTCON_ERROR_NONE != ret, -1);
331
332                 ret = iotcon_observers_add(observers, observe_id);
333                 retv_if(IOTCON_ERROR_NONE != ret, -1);
334         } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
335                 ret = iotcon_request_get_observe_id(request, &observe_id);
336                 retv_if(IOTCON_ERROR_NONE != ret, -1);
337
338                 ret = iotcon_observers_remove(observers, observe_id);
339                 retv_if(IOTCON_ERROR_NONE != ret, -1);
340         }
341
342         return 0;
343 }
344
345 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
346 {
347         connectivity_resource_s *resource_info = user_data;
348         int ret = -1;
349         char *host_address = NULL;
350
351         ret_if(!request);
352
353         ret = iotcon_request_get_host_address(request, &host_address);
354         goto_if(IOTCON_ERROR_NONE != ret, error);
355
356         _D("Host address : %s", host_address);
357
358         ret = _handle_query(request);
359         goto_if(IOTCON_ERROR_NONE != ret, error);
360
361         ret = _handle_request_by_crud_type(request, resource_info);
362         goto_if(0 != ret, error);
363
364         ret = _handle_observer(request, resource_info->observers);
365         goto_if(0 != ret, error);
366
367         return;
368
369 error:
370         _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
371 }
372
373 /* device_name : "iotcon-test-basic-server" */
374 int connectivity_init(const char *device_name)
375 {
376         int ret = -1;
377
378         ret = iotcon_initialize("../res/iotcon-test-svr-db-server.dat");
379         retv_if(IOTCON_ERROR_NONE != ret, -1);
380
381         ret = iotcon_set_device_name(device_name);
382         goto_if(IOTCON_ERROR_NONE != ret, error);
383
384         ret = iotcon_start_presence(10);
385         goto_if(IOTCON_ERROR_NONE != ret, error);
386
387         return 0;
388
389 error:
390         iotcon_deinitialize();
391         return -1;
392 }
393
394 int connectivity_fini(void)
395 {
396         iotcon_deinitialize();
397         return 0;
398 }
399
400 void connectivity_destroy_resource(connectivity_resource_s *resource_info)
401 {
402         ret_if(!resource_info);
403         if (resource_info->observers) iotcon_observers_destroy(resource_info->observers);
404         if (resource_info->res) iotcon_resource_destroy(resource_info->res);
405         free(resource_info);
406 }
407
408 int connectivity_create_resource(const char *uri_path, const char *type, connectivity_resource_s **out_resource_info)
409 {
410         iotcon_resource_types_h resource_types = NULL;
411         iotcon_resource_interfaces_h ifaces = NULL;
412         connectivity_resource_s *resource_info = NULL;
413         uint8_t policies;
414         int ret = -1;
415
416         resource_info = calloc(1, sizeof(connectivity_resource_s));
417         retv_if(!resource_info, -1);
418         *out_resource_info = resource_info;
419
420         ret = iotcon_resource_types_create(&resource_types);
421         goto_if(IOTCON_ERROR_NONE != ret, error);
422
423         ret = iotcon_resource_types_add(resource_types, type);
424         goto_if(IOTCON_ERROR_NONE != ret, error);
425
426         ret = iotcon_resource_interfaces_create(&ifaces);
427         goto_if(IOTCON_ERROR_NONE != ret, error);
428
429         ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_DEFAULT);
430         goto_if(IOTCON_ERROR_NONE != ret, error);
431
432         ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_BATCH);
433         goto_if(IOTCON_ERROR_NONE != ret, error);
434
435         policies =
436                 IOTCON_RESOURCE_DISCOVERABLE |
437                 IOTCON_RESOURCE_OBSERVABLE |
438                 IOTCON_RESOURCE_SECURE;
439
440         ret = iotcon_resource_create(uri_path,
441                         resource_types,
442                         ifaces,
443                         policies,
444                         _request_resource_handler,
445                         resource_info,
446                         &resource_info->res);
447         goto_if(IOTCON_ERROR_NONE != ret, error);
448
449         ret = iotcon_observers_create(&resource_info->observers);
450         goto_if(IOTCON_ERROR_NONE != ret, error);
451
452         iotcon_resource_types_destroy(resource_types);
453         iotcon_resource_interfaces_destroy(ifaces);
454
455         return 0;
456
457 error:
458         if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
459         if (resource_types) iotcon_resource_types_destroy(resource_types);
460         if (resource_info->res) iotcon_resource_destroy(resource_info->res);
461         if (resource_info) free(resource_info);
462
463         return -1;
464 }