Apply security
[platform/core/iot/iotcon.git] / lib / icl-resource.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 <stdbool.h>
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <glib.h>
23
24 #include "iotcon.h"
25 #include "ic-utils.h"
26 #include "icl.h"
27 #include "icl-repr.h"
28 #include "icl-dbus.h"
29 #include "icl-request.h"
30 #include "icl-dbus-type.h"
31 #include "icl-resource-types.h"
32 #include "icl-resource.h"
33 #include "icl-payload.h"
34
35 /**
36  * @brief The maximum length of uri_path path which can be held in a resource.
37  *
38  * @since_tizen 3.0
39  */
40 #define ICL_URI_PATH_LENGTH_MAX 36
41
42
43 static void _icl_request_handler(GDBusConnection *connection,
44                 const gchar *sender_name,
45                 const gchar *object_path,
46                 const gchar *interface_name,
47                 const gchar *signal_name,
48                 GVariant *parameters,
49                 gpointer user_data)
50 {
51         FN_CALL;
52         int ret;
53         char *key = NULL;
54         char *option_data;
55         char *value = NULL;
56         GVariant *repr_gvar;
57         GVariantIter *query;
58         GVariantIter *options;
59         GVariantIter *repr_iter;
60         unsigned short option_id;
61         struct icl_resource_request request = {0};
62         iotcon_resource_h resource = user_data;
63         iotcon_request_handler_cb cb = resource->cb;
64
65         g_variant_get(parameters, "(ia(qs)a(ss)iiavxx)",
66                         &request.types,
67                         &options,
68                         &query,
69                         &request.observation_info.action,
70                         &request.observation_info.observer_id,
71                         &repr_iter,
72                         &request.oic_request_h,
73                         &request.oic_resource_h);
74
75         if (g_variant_iter_n_children(options)) {
76                 ret = iotcon_options_create(&request.header_options);
77                 if (IOTCON_ERROR_NONE != ret) {
78                         ERR("iotcon_options_create() Fail(%d)", ret);
79                         g_variant_iter_free(options);
80                         g_variant_iter_free(query);
81                         g_variant_iter_free(repr_iter);
82                         return;
83                 }
84
85                 while (g_variant_iter_loop(options, "(q&s)", &option_id, &option_data))
86                         iotcon_options_insert(request.header_options, option_id, option_data);
87         }
88         g_variant_iter_free(options);
89
90         if (g_variant_iter_n_children(query)) {
91                 ret = iotcon_query_create(&request.query);
92                 if (IOTCON_ERROR_NONE != ret) {
93                         ERR("iotcon_query_create() Fail(%d)", ret);
94                         g_variant_iter_free(query);
95                         g_variant_iter_free(repr_iter);
96                         return;
97                 }
98
99                 while (g_variant_iter_loop(query, "(&s&s)", &key, &value))
100                         iotcon_query_insert(request.query, key, value);
101         }
102         g_variant_iter_free(query);
103
104         if (g_variant_iter_loop(repr_iter, "v", &repr_gvar)) {
105                 request.repr = icl_representation_from_gvariant(repr_gvar);
106                 if (NULL == request.repr) {
107                         ERR("icl_representation_from_gvariant() Fail");
108                         if (request.query)
109                                 iotcon_query_destroy(request.query);
110                         if (request.header_options)
111                                 iotcon_options_destroy(request.header_options);
112                         return;
113                 }
114         }
115         g_variant_iter_free(repr_iter);
116
117         /* TODO remove request.uri */
118         request.uri_path = "temp_uri_path";
119
120         if (cb)
121                 cb(resource, &request, resource->user_data);
122
123         /* To avoid unnecessary ERR log (representation could be NULL) */
124         if (request.repr)
125                 iotcon_representation_destroy(request.repr);
126         if (request.query)
127                 iotcon_query_destroy(request.query);
128         if (request.header_options)
129                 iotcon_options_destroy(request.header_options);
130 }
131
132
133 static void _icl_resource_conn_cleanup(iotcon_resource_h resource)
134 {
135         resource->sub_id = 0;
136
137         if (resource->handle) {
138                 resource->handle = 0;
139                 return;
140         }
141
142         iotcon_resource_types_destroy(resource->types);
143         free(resource->uri_path);
144         free(resource);
145 }
146
147
148 /* The length of uri_path should be less than or equal to 36. */
149 API int iotcon_register_resource(const char *uri_path,
150                 iotcon_resource_types_h res_types,
151                 int ifaces,
152                 uint8_t properties,
153                 iotcon_request_handler_cb cb,
154                 void *user_data,
155                 iotcon_resource_h *resource_handle)
156 {
157         unsigned int sub_id;
158         GError *error = NULL;
159         const gchar **types;
160         iotcon_resource_h resource;
161         int signal_number, error_code;
162         char sig_name[IC_DBUS_SIGNAL_LENGTH];
163
164         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_INVALID_PARAMETER);
165         RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
166         RETVM_IF(ICL_URI_PATH_LENGTH_MAX < strlen(uri_path),
167                         IOTCON_ERROR_INVALID_PARAMETER, "Invalid uri_path(%s)", uri_path);
168         RETV_IF(NULL == res_types, IOTCON_ERROR_INVALID_PARAMETER);
169         RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
170
171         resource = calloc(1, sizeof(struct icl_resource));
172         if (NULL == resource) {
173                 ERR("calloc() Fail(%d)", errno);
174                 return IOTCON_ERROR_OUT_OF_MEMORY;
175         }
176
177         types = icl_dbus_resource_types_to_array(res_types);
178         if (NULL == types) {
179                 ERR("icl_dbus_resource_types_to_array() Fail");
180                 free(resource);
181                 return IOTCON_ERROR_INVALID_PARAMETER;
182         }
183
184         signal_number = icl_dbus_generate_signal_number();
185
186         ic_dbus_call_register_resource_sync(icl_dbus_get_object(), uri_path, types, ifaces,
187                         properties, signal_number, &(resource->handle), NULL, &error);
188         if (error) {
189                 ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message);
190                 error_code = icl_dbus_convert_dbus_error(error->code);
191                 g_error_free(error);
192                 free(types);
193                 free(resource);
194                 return error_code;
195         }
196         free(types);
197
198         if (0 == resource->handle) {
199                 ERR("iotcon-daemon Fail");
200                 free(resource);
201                 return IOTCON_ERROR_IOTIVITY;
202         }
203
204         resource->cb = cb;
205         resource->user_data = user_data;
206
207         resource->types = icl_resource_types_ref(res_types);
208         resource->uri_path = ic_utils_strdup(uri_path);
209         resource->ifaces = ifaces;
210         resource->is_observable = properties & IOTCON_OBSERVABLE;
211
212         snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER,
213                         signal_number);
214
215         sub_id = icl_dbus_subscribe_signal(sig_name, resource, _icl_resource_conn_cleanup,
216                         _icl_request_handler);
217         if (0 == sub_id) {
218                 ERR("icl_dbus_subscribe_signal() Fail");
219                 free(resource);
220                 return IOTCON_ERROR_DBUS;
221         }
222
223         resource->sub_id = sub_id;
224
225         *resource_handle = resource;
226
227         return IOTCON_ERROR_NONE;
228 }
229
230
231 API int iotcon_unregister_resource(iotcon_resource_h resource)
232 {
233         FN_CALL;
234         int ret, error_code;
235         GError *error = NULL;
236
237         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
238         RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
239
240         if (0 == resource->sub_id) {
241                 WARN("Invalid Resource handle");
242                 iotcon_resource_types_destroy(resource->types);
243                 free(resource->uri_path);
244                 free(resource);
245                 return IOTCON_ERROR_NONE;
246         }
247
248         ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle,
249                         &ret, NULL, &error);
250         if (error) {
251                 ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message);
252                 error_code = icl_dbus_convert_dbus_error(error->code);
253                 g_error_free(error);
254                 return error_code;
255         }
256
257         if (IOTCON_ERROR_NONE != ret) {
258                 ERR("iotcon-daemon Fail(%d)", ret);
259                 return icl_dbus_convert_daemon_error(ret);
260         }
261         resource->handle = 0;
262
263         icl_dbus_unsubscribe_signal(resource->sub_id);
264
265         return IOTCON_ERROR_NONE;
266 }
267
268
269 API int iotcon_resource_bind_interface(iotcon_resource_h resource, int iface)
270 {
271         FN_CALL;
272         int ret, error_code;
273         GError *error = NULL;
274
275         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
276         RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
277         if (0 == resource->sub_id) {
278                 ERR("Invalid Resource handle");
279                 return IOTCON_ERROR_INVALID_PARAMETER;
280         }
281
282         ic_dbus_call_bind_interface_sync(icl_dbus_get_object(), resource->handle,
283                         iface, &ret, NULL, &error);
284         if (error) {
285                 ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message);
286                 error_code = icl_dbus_convert_dbus_error(error->code);
287                 g_error_free(error);
288                 return error_code;
289         }
290
291         if (IOTCON_ERROR_NONE != ret) {
292                 ERR("iotcon-daemon Fail(%d)", ret);
293                 return icl_dbus_convert_daemon_error(ret);
294         }
295
296         return ret;
297 }
298
299
300 API int iotcon_resource_bind_type(iotcon_resource_h resource, const char *resource_type)
301 {
302         FN_CALL;
303         int ret, error_code;
304         GError *error = NULL;
305
306         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
307         RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
308         RETV_IF(NULL == resource_type, IOTCON_ERROR_INVALID_PARAMETER);
309         if (ICL_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type)) {
310                 ERR("Invalid resource_type(%s)", resource_type);
311                 return IOTCON_ERROR_INVALID_PARAMETER;
312         }
313
314         if (0 == resource->sub_id) {
315                 ERR("Invalid Resource handle");
316                 return IOTCON_ERROR_INVALID_PARAMETER;
317         }
318
319         ic_dbus_call_bind_type_sync(icl_dbus_get_object(), resource->handle, resource_type,
320                         &ret, NULL, &error);
321         if (error) {
322                 ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message);
323                 error_code = icl_dbus_convert_dbus_error(error->code);
324                 g_error_free(error);
325                 return error_code;
326         }
327
328         if (IOTCON_ERROR_NONE != ret) {
329                 ERR("iotcon-daemon Fail(%d)", ret);
330                 return icl_dbus_convert_daemon_error(ret);
331         }
332
333         return ret;
334 }
335
336
337 API int iotcon_resource_bind_request_handler(iotcon_resource_h resource,
338                 iotcon_request_handler_cb cb)
339 {
340         RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
341         RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
342
343         WARN("Request handler is changed");
344         resource->cb = cb;
345
346         return IOTCON_ERROR_NONE;
347 }
348
349
350 API int iotcon_resource_bind_child_resource(iotcon_resource_h parent,
351                 iotcon_resource_h child)
352 {
353         GError *error = NULL;
354         int i, ret, error_code;
355
356         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
357         RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
358         RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
359         RETV_IF(parent == child, IOTCON_ERROR_INVALID_PARAMETER);
360
361         if (0 == parent->sub_id) {
362                 ERR("Invalid Resource handle(parent)");
363                 return IOTCON_ERROR_INVALID_PARAMETER;
364         }
365         if (0 == child->sub_id) {
366                 ERR("Invalid Resource handle(child)");
367                 return IOTCON_ERROR_INVALID_PARAMETER;
368         }
369
370         for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
371                 if (child == parent->children[i]) {
372                         ERR("Child resource was already bound to parent resource.");
373                         return IOTCON_ERROR_ALREADY;
374                 }
375         }
376
377         for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
378                 if (NULL == parent->children[i]) {
379                         ic_dbus_call_bind_resource_sync(icl_dbus_get_object(), parent->handle,
380                                         child->handle, &ret, NULL, &error);
381                         if (error) {
382                                 ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message);
383                                 error_code = icl_dbus_convert_dbus_error(error->code);
384                                 g_error_free(error);
385                                 return error_code;
386                         }
387
388                         if (IOTCON_ERROR_NONE != ret) {
389                                 ERR("iotcon-daemon Fail(%d)", ret);
390                                 return icl_dbus_convert_daemon_error(ret);
391                         }
392
393                         parent->children[i] = child;
394
395                         return IOTCON_ERROR_NONE;
396                 }
397         }
398
399         ERR("There is no slot to bind a child resource");
400         return IOTCON_ERROR_OUT_OF_MEMORY;
401 }
402
403
404 API int iotcon_resource_unbind_child_resource(iotcon_resource_h parent,
405                 iotcon_resource_h child)
406 {
407         GError *error = NULL;
408         int i, ret, error_code;
409
410         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
411         RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
412         RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
413
414         if (0 == parent->sub_id) {
415                 ERR("Invalid Resource handle(parent)");
416                 return IOTCON_ERROR_INVALID_PARAMETER;
417         }
418         if (0 == child->sub_id) {
419                 ERR("Invalid Resource handle(child)");
420                 return IOTCON_ERROR_INVALID_PARAMETER;
421         }
422
423         ic_dbus_call_unbind_resource_sync(icl_dbus_get_object(), parent->handle,
424                         child->handle, &ret, NULL, &error);
425         if (error) {
426                 ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message);
427                 error_code = icl_dbus_convert_dbus_error(error->code);
428                 g_error_free(error);
429                 return error_code;
430         }
431
432         if (IOTCON_ERROR_NONE != ret) {
433                 ERR("iotcon-daemon Fail(%d)", ret);
434                 return icl_dbus_convert_daemon_error(ret);
435         }
436
437         for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
438                 if (child == parent->children[i])
439                         parent->children[i] = NULL;
440         }
441
442         return IOTCON_ERROR_NONE;
443 }
444
445
446 API int iotcon_resource_get_number_of_children(iotcon_resource_h resource, int *number)
447 {
448         int i;
449
450         RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
451         RETV_IF(NULL == number, IOTCON_ERROR_INVALID_PARAMETER);
452
453         *number = 0;
454         for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
455                 if (resource->children[i])
456                         *number += 1;
457         }
458
459         return IOTCON_ERROR_NONE;
460 }
461
462
463 API int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index,
464                 iotcon_resource_h *child)
465 {
466         RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
467         RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
468         if ((index < 0) || (ICL_CONTAINED_RESOURCES_MAX <= index)) {
469                 ERR("Invalid index(%d)", index);
470                 return IOTCON_ERROR_INVALID_PARAMETER;
471         }
472
473         *child = parent->children[index];
474
475         return IOTCON_ERROR_NONE;
476 }
477
478
479 /* The content of the resource should not be freed by user. */
480 API int iotcon_resource_get_uri_path(iotcon_resource_h resource, char **uri_path)
481 {
482         RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
483         RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
484
485         *uri_path = resource->uri_path;
486
487         return IOTCON_ERROR_NONE;
488 }
489
490
491 /* The content of the resource should not be freed by user. */
492 API int iotcon_resource_get_types(iotcon_resource_h resource,
493                 iotcon_resource_types_h *types)
494 {
495         RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
496         RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
497
498         *types = resource->types;
499
500         return IOTCON_ERROR_NONE;
501 }
502
503
504 API int iotcon_resource_get_interfaces(iotcon_resource_h resource, int *ifaces)
505 {
506         RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
507         RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER);
508
509         *ifaces = resource->ifaces;
510
511         return IOTCON_ERROR_NONE;
512 }
513
514
515 API int iotcon_resource_is_observable(iotcon_resource_h resource, bool *observable)
516 {
517         RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
518         RETV_IF(NULL == observable, IOTCON_ERROR_INVALID_PARAMETER);
519
520         *observable = resource->is_observable;
521
522         return IOTCON_ERROR_NONE;
523 }
524
525
526 API int iotcon_notimsg_create(iotcon_representation_h repr, int iface,
527                 iotcon_notimsg_h *notimsg_handle)
528 {
529         iotcon_notimsg_h msg;
530
531         RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
532
533         msg = calloc(1, sizeof(struct icl_notify_msg));
534         if (NULL == msg) {
535                 ERR("calloc() Fail(%d)", errno);
536                 return IOTCON_ERROR_OUT_OF_MEMORY;
537         }
538
539         msg->repr = repr;
540         icl_representation_inc_ref_count(msg->repr);
541         msg->iface = iface;
542         msg->error_code = 200;
543
544         *notimsg_handle = msg;
545
546         return IOTCON_ERROR_NONE;
547 }
548
549
550 API void iotcon_notimsg_destroy(iotcon_notimsg_h msg)
551 {
552         RET_IF(NULL == msg);
553
554         iotcon_representation_destroy(msg->repr);
555         free(msg);
556 }
557
558
559 API int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notimsg_h msg,
560                 iotcon_observers_h observers)
561 {
562         int ret, error_code;
563         GError *error = NULL;
564         GVariant *noti_msg;
565         GVariant *obs;
566
567         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
568         RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
569         RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER);
570
571         if (0 == resource->sub_id) {
572                 ERR("Invalid Resource handle");
573                 return IOTCON_ERROR_INVALID_PARAMETER;
574         }
575
576         noti_msg = icl_dbus_notimsg_to_gvariant(msg);
577         if (NULL == noti_msg) {
578                 ERR("icl_dbus_notimsg_to_gvariant() Fail");
579                 return IOTCON_ERROR_REPRESENTATION;
580         }
581         obs = icl_dbus_observers_to_gvariant(observers);
582
583         ic_dbus_call_notify_list_of_observers_sync(icl_dbus_get_object(), resource->handle,
584                         noti_msg, obs, &ret, NULL, &error);
585         if (error) {
586                 ERR("ic_dbus_call_notify_list_of_observers_sync() Fail(%s)", error->message);
587                 error_code = icl_dbus_convert_dbus_error(error->code);
588                 g_error_free(error);
589                 g_variant_unref(obs);
590                 g_variant_unref(noti_msg);
591                 return error_code;
592         }
593
594         if (IOTCON_ERROR_NONE != ret) {
595                 ERR("iotcon-daemon Fail(%d)", ret);
596                 return icl_dbus_convert_daemon_error(ret);
597         }
598
599         return IOTCON_ERROR_NONE;
600 }
601
602
603 API int iotcon_resource_notify_all(iotcon_resource_h resource)
604 {
605         int ret, error_code;
606         GError *error = NULL;
607
608         RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
609         RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
610         if (0 == resource->sub_id) {
611                 ERR("Invalid Resource handle");
612                 return IOTCON_ERROR_INVALID_PARAMETER;
613         }
614
615         ic_dbus_call_notify_all_sync(icl_dbus_get_object(), resource->handle, &ret, NULL,
616                         &error);
617         if (error) {
618                 ERR("ic_dbus_call_notify_all_sync() Fail(%s)", error->message);
619                 error_code = icl_dbus_convert_dbus_error(error->code);
620                 g_error_free(error);
621                 return error_code;
622         }
623
624         if (IOTCON_ERROR_NONE != ret) {
625                 ERR("iotcon-daemon Fail(%d)", ret);
626                 return icl_dbus_convert_daemon_error(ret);
627         }
628
629         return IOTCON_ERROR_NONE;
630 }