2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
36 * @brief The maximum length of uri_path path which can be held in a resource.
40 #define ICL_URI_PATH_LENGTH_MAX 36
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,
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;
65 g_variant_get(parameters, "(ia(qs)a(ss)iiavxx)",
69 &request.observation_info.action,
70 &request.observation_info.observer_id,
72 &request.oic_request_h,
73 &request.oic_resource_h);
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);
85 while (g_variant_iter_loop(options, "(q&s)", &option_id, &option_data))
86 iotcon_options_insert(request.header_options, option_id, option_data);
88 g_variant_iter_free(options);
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);
101 while (g_variant_iter_loop(query, "(&s&s)", &key, &value))
102 iotcon_query_insert(request.query, key, value);
104 g_variant_iter_free(query);
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");
111 iotcon_query_destroy(request.query);
112 if (request.header_options)
113 iotcon_options_destroy(request.header_options);
117 g_variant_iter_free(repr_iter);
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);
132 cb(resource, &request, resource->user_data);
134 /* To avoid unnecessary ERR log (representation could be NULL) */
136 iotcon_representation_destroy(request.repr);
138 iotcon_query_destroy(request.query);
139 if (request.header_options)
140 iotcon_options_destroy(request.header_options);
144 static void _icl_resource_conn_cleanup(iotcon_resource_h resource)
146 resource->sub_id = 0;
148 if (resource->handle) {
149 resource->handle = 0;
153 iotcon_resource_types_destroy(resource->types);
154 if (resource->observers)
155 iotcon_observers_destroy(resource->observers);
156 free(resource->uri_path);
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,
166 iotcon_request_handler_cb cb,
168 iotcon_resource_h *resource_handle)
171 GError *error = NULL;
173 iotcon_resource_h resource;
174 int signal_number, ret;
175 char sig_name[IC_DBUS_SIGNAL_LENGTH];
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);
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;
190 types = icl_dbus_resource_types_to_array(res_types);
192 ERR("icl_dbus_resource_types_to_array() Fail");
194 return IOTCON_ERROR_INVALID_PARAMETER;
197 signal_number = icl_dbus_generate_signal_number();
199 ic_dbus_call_register_resource_sync(icl_dbus_get_object(), uri_path, types, ifaces,
200 properties, signal_number, &(resource->handle), NULL, &error);
202 ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message);
203 ret = icl_dbus_convert_dbus_error(error->code);
211 if (0 == resource->handle) {
212 ERR("iotcon-daemon Fail");
214 return IOTCON_ERROR_IOTIVITY;
218 resource->user_data = user_data;
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;
225 snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER,
228 sub_id = icl_dbus_subscribe_signal(sig_name, resource, _icl_resource_conn_cleanup,
229 _icl_request_handler);
231 ERR("icl_dbus_subscribe_signal() Fail");
232 iotcon_resource_types_destroy(res_types);
233 free(resource->uri_path);
235 return IOTCON_ERROR_DBUS;
238 resource->sub_id = sub_id;
240 *resource_handle = resource;
242 return IOTCON_ERROR_NONE;
246 API int iotcon_resource_destroy(iotcon_resource_h resource)
250 GError *error = NULL;
252 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
253 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
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);
262 return IOTCON_ERROR_NONE;
265 ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle, NULL,
268 ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message);
269 ret = icl_dbus_convert_dbus_error(error->code);
274 resource->handle = 0;
276 icl_dbus_unsubscribe_signal(resource->sub_id);
278 return IOTCON_ERROR_NONE;
282 API int iotcon_resource_bind_interface(iotcon_resource_h resource, int iface)
286 GError *error = NULL;
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;
295 ic_dbus_call_bind_interface_sync(icl_dbus_get_object(), resource->handle,
296 iface, &ret, NULL, &error);
298 ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message);
299 ret = icl_dbus_convert_dbus_error(error->code);
304 if (IOTCON_ERROR_NONE != ret) {
305 ERR("iotcon-daemon Fail(%d)", ret);
306 return icl_dbus_convert_daemon_error(ret);
313 API int iotcon_resource_bind_type(iotcon_resource_h resource, const char *resource_type)
317 GError *error = NULL;
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;
327 if (0 == resource->sub_id) {
328 ERR("Invalid Resource handle");
329 return IOTCON_ERROR_INVALID_PARAMETER;
332 ic_dbus_call_bind_type_sync(icl_dbus_get_object(), resource->handle, resource_type,
335 ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message);
336 ret = icl_dbus_convert_dbus_error(error->code);
341 if (IOTCON_ERROR_NONE != ret) {
342 ERR("iotcon-daemon Fail(%d)", ret);
343 return icl_dbus_convert_daemon_error(ret);
350 API int iotcon_resource_set_request_handler(iotcon_resource_h resource,
351 iotcon_request_handler_cb cb)
353 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
354 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
356 WARN("Request handler is changed");
359 return IOTCON_ERROR_NONE;
363 API int iotcon_resource_bind_child_resource(iotcon_resource_h parent,
364 iotcon_resource_h child)
366 GError *error = NULL;
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);
374 if (0 == parent->sub_id) {
375 ERR("Invalid Resource handle(parent)");
376 return IOTCON_ERROR_INVALID_PARAMETER;
378 if (0 == child->sub_id) {
379 ERR("Invalid Resource handle(child)");
380 return IOTCON_ERROR_INVALID_PARAMETER;
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;
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);
395 ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message);
396 ret = icl_dbus_convert_dbus_error(error->code);
401 if (IOTCON_ERROR_NONE != ret) {
402 ERR("iotcon-daemon Fail(%d)", ret);
403 return icl_dbus_convert_daemon_error(ret);
406 parent->children[i] = child;
408 return IOTCON_ERROR_NONE;
412 ERR("There is no slot to bind a child resource");
413 return IOTCON_ERROR_OUT_OF_MEMORY;
417 API int iotcon_resource_unbind_child_resource(iotcon_resource_h parent,
418 iotcon_resource_h child)
420 GError *error = NULL;
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);
427 if (0 == parent->sub_id) {
428 ERR("Invalid Resource handle(parent)");
429 return IOTCON_ERROR_INVALID_PARAMETER;
431 if (0 == child->sub_id) {
432 ERR("Invalid Resource handle(child)");
433 return IOTCON_ERROR_INVALID_PARAMETER;
436 ic_dbus_call_unbind_resource_sync(icl_dbus_get_object(), parent->handle,
437 child->handle, &ret, NULL, &error);
439 ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message);
440 ret = icl_dbus_convert_dbus_error(error->code);
445 if (IOTCON_ERROR_NONE != ret) {
446 ERR("iotcon-daemon Fail(%d)", ret);
447 return icl_dbus_convert_daemon_error(ret);
450 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
451 if (child == parent->children[i])
452 parent->children[i] = NULL;
455 return IOTCON_ERROR_NONE;
459 API int iotcon_resource_get_number_of_children(iotcon_resource_h resource, int *number)
463 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
464 RETV_IF(NULL == number, IOTCON_ERROR_INVALID_PARAMETER);
467 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
468 if (resource->children[i])
472 return IOTCON_ERROR_NONE;
476 API int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index,
477 iotcon_resource_h *child)
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;
486 *child = parent->children[index];
488 return IOTCON_ERROR_NONE;
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)
495 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
496 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
498 *uri_path = resource->uri_path;
500 return IOTCON_ERROR_NONE;
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)
508 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
509 RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
511 *types = resource->types;
513 return IOTCON_ERROR_NONE;
517 API int iotcon_resource_get_interfaces(iotcon_resource_h resource, int *ifaces)
519 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
520 RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER);
522 *ifaces = resource->ifaces;
524 return IOTCON_ERROR_NONE;
528 API int iotcon_resource_is_observable(iotcon_resource_h resource, bool *observable)
530 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
531 RETV_IF(NULL == observable, IOTCON_ERROR_INVALID_PARAMETER);
533 *observable = resource->is_observable;
535 return IOTCON_ERROR_NONE;
539 API int iotcon_notimsg_create(iotcon_representation_h repr, int iface,
540 iotcon_notimsg_h *notimsg_handle)
542 iotcon_notimsg_h msg;
544 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
545 RETV_IF(NULL == notimsg_handle, IOTCON_ERROR_INVALID_PARAMETER);
547 msg = calloc(1, sizeof(struct icl_notify_msg));
549 ERR("calloc() Fail(%d)", errno);
550 return IOTCON_ERROR_OUT_OF_MEMORY;
554 icl_representation_inc_ref_count(msg->repr);
556 msg->error_code = 200;
558 *notimsg_handle = msg;
560 return IOTCON_ERROR_NONE;
564 API void iotcon_notimsg_destroy(iotcon_notimsg_h msg)
568 iotcon_representation_destroy(msg->repr);
572 API int iotcon_resource_notify(iotcon_resource_h resource, iotcon_notimsg_h msg,
573 iotcon_observers_h observers)
576 GError *error = NULL;
580 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
581 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
583 if (0 == resource->sub_id) {
584 ERR("Invalid Resource handle");
585 return IOTCON_ERROR_INVALID_PARAMETER;
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;
596 obs = icl_dbus_observers_to_gvariant(observers);
598 obs = icl_dbus_observers_to_gvariant(resource->observers);
600 ic_dbus_call_notify_sync(icl_dbus_get_object(), resource->handle, noti_msg, obs, &ret,
603 ERR("ic_dbus_call_notify_sync() Fail(%s)", error->message);
604 ret = icl_dbus_convert_dbus_error(error->code);
606 g_variant_unref(obs);
607 g_variant_unref(noti_msg);
611 if (IOTCON_ERROR_NONE != ret) {
612 ERR("iotcon-daemon Fail(%d)", ret);
613 return icl_dbus_convert_daemon_error(ret);
616 return IOTCON_ERROR_NONE;