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 /* TODO remove request.uri */
120 request.uri_path = "temp_uri_path";
123 cb(resource, &request, resource->user_data);
125 /* To avoid unnecessary ERR log (representation could be NULL) */
127 iotcon_representation_destroy(request.repr);
129 iotcon_query_destroy(request.query);
130 if (request.header_options)
131 iotcon_options_destroy(request.header_options);
135 static void _icl_resource_conn_cleanup(iotcon_resource_h resource)
137 resource->sub_id = 0;
139 if (resource->handle) {
140 resource->handle = 0;
144 iotcon_resource_types_destroy(resource->types);
145 free(resource->uri_path);
150 /* The length of uri_path should be less than or equal to 36. */
151 API int iotcon_resource_create(const char *uri_path,
152 iotcon_resource_types_h res_types,
155 iotcon_request_handler_cb cb,
157 iotcon_resource_h *resource_handle)
160 GError *error = NULL;
162 iotcon_resource_h resource;
163 int signal_number, ret;
164 char sig_name[IC_DBUS_SIGNAL_LENGTH];
166 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
167 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
168 RETVM_IF(ICL_URI_PATH_LENGTH_MAX < strlen(uri_path),
169 IOTCON_ERROR_INVALID_PARAMETER, "Invalid uri_path(%s)", uri_path);
170 RETV_IF(NULL == res_types, IOTCON_ERROR_INVALID_PARAMETER);
171 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
173 resource = calloc(1, sizeof(struct icl_resource));
174 if (NULL == resource) {
175 ERR("calloc() Fail(%d)", errno);
176 return IOTCON_ERROR_OUT_OF_MEMORY;
179 types = icl_dbus_resource_types_to_array(res_types);
181 ERR("icl_dbus_resource_types_to_array() Fail");
183 return IOTCON_ERROR_INVALID_PARAMETER;
186 signal_number = icl_dbus_generate_signal_number();
188 ic_dbus_call_register_resource_sync(icl_dbus_get_object(), uri_path, types, ifaces,
189 properties, signal_number, &(resource->handle), NULL, &error);
191 ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message);
192 ret = icl_dbus_convert_dbus_error(error->code);
200 if (0 == resource->handle) {
201 ERR("iotcon-daemon Fail");
203 return IOTCON_ERROR_IOTIVITY;
207 resource->user_data = user_data;
209 resource->types = icl_resource_types_ref(res_types);
210 resource->uri_path = ic_utils_strdup(uri_path);
211 resource->ifaces = ifaces;
212 resource->is_observable = properties & IOTCON_OBSERVABLE;
214 snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER,
217 sub_id = icl_dbus_subscribe_signal(sig_name, resource, _icl_resource_conn_cleanup,
218 _icl_request_handler);
220 ERR("icl_dbus_subscribe_signal() Fail");
221 iotcon_resource_types_destroy(res_types);
222 free(resource->uri_path);
224 return IOTCON_ERROR_DBUS;
227 resource->sub_id = sub_id;
229 *resource_handle = resource;
231 return IOTCON_ERROR_NONE;
235 API int iotcon_resource_destroy(iotcon_resource_h resource)
239 GError *error = NULL;
241 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
242 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
244 if (0 == resource->sub_id) {
245 WARN("Invalid Resource handle");
246 iotcon_resource_types_destroy(resource->types);
247 free(resource->uri_path);
249 return IOTCON_ERROR_NONE;
252 ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle, NULL,
255 ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message);
256 ret = icl_dbus_convert_dbus_error(error->code);
261 resource->handle = 0;
263 icl_dbus_unsubscribe_signal(resource->sub_id);
265 return IOTCON_ERROR_NONE;
269 API int iotcon_resource_bind_interface(iotcon_resource_h resource, int iface)
273 GError *error = NULL;
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;
282 ic_dbus_call_bind_interface_sync(icl_dbus_get_object(), resource->handle,
283 iface, &ret, NULL, &error);
285 ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message);
286 ret = icl_dbus_convert_dbus_error(error->code);
291 if (IOTCON_ERROR_NONE != ret) {
292 ERR("iotcon-daemon Fail(%d)", ret);
293 return icl_dbus_convert_daemon_error(ret);
300 API int iotcon_resource_bind_type(iotcon_resource_h resource, const char *resource_type)
304 GError *error = NULL;
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;
314 if (0 == resource->sub_id) {
315 ERR("Invalid Resource handle");
316 return IOTCON_ERROR_INVALID_PARAMETER;
319 ic_dbus_call_bind_type_sync(icl_dbus_get_object(), resource->handle, resource_type,
322 ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message);
323 ret = icl_dbus_convert_dbus_error(error->code);
328 if (IOTCON_ERROR_NONE != ret) {
329 ERR("iotcon-daemon Fail(%d)", ret);
330 return icl_dbus_convert_daemon_error(ret);
337 API int iotcon_resource_set_request_handler(iotcon_resource_h resource,
338 iotcon_request_handler_cb cb)
340 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
341 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
343 WARN("Request handler is changed");
346 return IOTCON_ERROR_NONE;
350 API int iotcon_resource_bind_child_resource(iotcon_resource_h parent,
351 iotcon_resource_h child)
353 GError *error = NULL;
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);
361 if (0 == parent->sub_id) {
362 ERR("Invalid Resource handle(parent)");
363 return IOTCON_ERROR_INVALID_PARAMETER;
365 if (0 == child->sub_id) {
366 ERR("Invalid Resource handle(child)");
367 return IOTCON_ERROR_INVALID_PARAMETER;
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;
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);
382 ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message);
383 ret = icl_dbus_convert_dbus_error(error->code);
388 if (IOTCON_ERROR_NONE != ret) {
389 ERR("iotcon-daemon Fail(%d)", ret);
390 return icl_dbus_convert_daemon_error(ret);
393 parent->children[i] = child;
395 return IOTCON_ERROR_NONE;
399 ERR("There is no slot to bind a child resource");
400 return IOTCON_ERROR_OUT_OF_MEMORY;
404 API int iotcon_resource_unbind_child_resource(iotcon_resource_h parent,
405 iotcon_resource_h child)
407 GError *error = NULL;
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);
414 if (0 == parent->sub_id) {
415 ERR("Invalid Resource handle(parent)");
416 return IOTCON_ERROR_INVALID_PARAMETER;
418 if (0 == child->sub_id) {
419 ERR("Invalid Resource handle(child)");
420 return IOTCON_ERROR_INVALID_PARAMETER;
423 ic_dbus_call_unbind_resource_sync(icl_dbus_get_object(), parent->handle,
424 child->handle, &ret, NULL, &error);
426 ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message);
427 ret = icl_dbus_convert_dbus_error(error->code);
432 if (IOTCON_ERROR_NONE != ret) {
433 ERR("iotcon-daemon Fail(%d)", ret);
434 return icl_dbus_convert_daemon_error(ret);
437 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
438 if (child == parent->children[i])
439 parent->children[i] = NULL;
442 return IOTCON_ERROR_NONE;
446 API int iotcon_resource_get_number_of_children(iotcon_resource_h resource, int *number)
450 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
451 RETV_IF(NULL == number, IOTCON_ERROR_INVALID_PARAMETER);
454 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
455 if (resource->children[i])
459 return IOTCON_ERROR_NONE;
463 API int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index,
464 iotcon_resource_h *child)
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;
473 *child = parent->children[index];
475 return IOTCON_ERROR_NONE;
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)
482 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
483 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
485 *uri_path = resource->uri_path;
487 return IOTCON_ERROR_NONE;
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)
495 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
496 RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
498 *types = resource->types;
500 return IOTCON_ERROR_NONE;
504 API int iotcon_resource_get_interfaces(iotcon_resource_h resource, int *ifaces)
506 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
507 RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER);
509 *ifaces = resource->ifaces;
511 return IOTCON_ERROR_NONE;
515 API int iotcon_resource_is_observable(iotcon_resource_h resource, bool *observable)
517 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
518 RETV_IF(NULL == observable, IOTCON_ERROR_INVALID_PARAMETER);
520 *observable = resource->is_observable;
522 return IOTCON_ERROR_NONE;
526 API int iotcon_notimsg_create(iotcon_representation_h repr, int iface,
527 iotcon_notimsg_h *notimsg_handle)
529 iotcon_notimsg_h msg;
531 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
532 RETV_IF(NULL == notimsg_handle, IOTCON_ERROR_INVALID_PARAMETER);
534 msg = calloc(1, sizeof(struct icl_notify_msg));
536 ERR("calloc() Fail(%d)", errno);
537 return IOTCON_ERROR_OUT_OF_MEMORY;
541 icl_representation_inc_ref_count(msg->repr);
543 msg->error_code = 200;
545 *notimsg_handle = msg;
547 return IOTCON_ERROR_NONE;
551 API void iotcon_notimsg_destroy(iotcon_notimsg_h msg)
555 iotcon_representation_destroy(msg->repr);
560 API int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notimsg_h msg,
561 iotcon_observers_h observers)
564 GError *error = NULL;
568 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
569 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
570 RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER);
572 if (0 == resource->sub_id) {
573 ERR("Invalid Resource handle");
574 return IOTCON_ERROR_INVALID_PARAMETER;
577 noti_msg = icl_dbus_notimsg_to_gvariant(msg);
578 if (NULL == noti_msg) {
579 ERR("icl_dbus_notimsg_to_gvariant() Fail");
580 return IOTCON_ERROR_REPRESENTATION;
582 obs = icl_dbus_observers_to_gvariant(observers);
584 ic_dbus_call_notify_list_of_observers_sync(icl_dbus_get_object(), resource->handle,
585 noti_msg, obs, &ret, NULL, &error);
587 ERR("ic_dbus_call_notify_list_of_observers_sync() Fail(%s)", error->message);
588 ret = icl_dbus_convert_dbus_error(error->code);
590 g_variant_unref(obs);
591 g_variant_unref(noti_msg);
595 if (IOTCON_ERROR_NONE != ret) {
596 ERR("iotcon-daemon Fail(%d)", ret);
597 return icl_dbus_convert_daemon_error(ret);
600 return IOTCON_ERROR_NONE;
604 API int iotcon_resource_notify_all(iotcon_resource_h resource)
607 GError *error = NULL;
609 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
610 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
611 if (0 == resource->sub_id) {
612 ERR("Invalid Resource handle");
613 return IOTCON_ERROR_INVALID_PARAMETER;
616 ic_dbus_call_notify_all_sync(icl_dbus_get_object(), resource->handle, &ret, NULL,
619 ERR("ic_dbus_call_notify_all_sync() Fail(%s)", error->message);
620 ret = icl_dbus_convert_dbus_error(error->code);
625 if (IOTCON_ERROR_NONE != ret) {
626 ERR("iotcon-daemon Fail(%d)", ret);
627 return icl_dbus_convert_daemon_error(ret);
630 return IOTCON_ERROR_NONE;