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