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