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