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);
99 while (g_variant_iter_loop(query, "(&s&s)", &key, &value))
100 iotcon_query_insert(request.query, key, value);
102 g_variant_iter_free(query);
104 if (g_variant_iter_loop(repr_iter, "v", &repr_gvar)) {
105 request.repr = icl_representation_from_gvariant(repr_gvar);
106 if (NULL == request.repr) {
107 ERR("icl_representation_from_gvariant() Fail");
109 iotcon_query_destroy(request.query);
110 if (request.header_options)
111 iotcon_options_destroy(request.header_options);
115 g_variant_iter_free(repr_iter);
117 /* TODO remove request.uri */
118 request.uri_path = "temp_uri_path";
121 cb(resource, &request, resource->user_data);
123 /* To avoid unnecessary ERR log (representation could be NULL) */
125 iotcon_representation_destroy(request.repr);
127 iotcon_query_destroy(request.query);
128 if (request.header_options)
129 iotcon_options_destroy(request.header_options);
133 static void _icl_resource_conn_cleanup(iotcon_resource_h resource)
135 resource->sub_id = 0;
137 if (resource->handle) {
138 resource->handle = 0;
142 iotcon_resource_types_destroy(resource->types);
143 free(resource->uri_path);
148 /* The length of uri_path should be less than or equal to 36. */
149 API int iotcon_register_resource(const char *uri_path,
150 iotcon_resource_types_h res_types,
153 iotcon_request_handler_cb cb,
155 iotcon_resource_h *resource_handle)
159 GError *error = NULL;
161 char sig_name[IC_DBUS_SIGNAL_LENGTH];
162 iotcon_resource_h resource;
164 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_INVALID_PARAMETER);
165 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
166 RETVM_IF(ICL_URI_PATH_LENGTH_MAX < strlen(uri_path),
167 IOTCON_ERROR_INVALID_PARAMETER, "Invalid uri_path(%s)", uri_path);
168 RETV_IF(NULL == res_types, IOTCON_ERROR_INVALID_PARAMETER);
169 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
171 resource = calloc(1, sizeof(struct icl_resource));
172 if (NULL == resource) {
173 ERR("calloc() Fail(%d)", errno);
174 return IOTCON_ERROR_OUT_OF_MEMORY;
177 types = icl_dbus_resource_types_to_array(res_types);
179 ERR("icl_dbus_resource_types_to_array() Fail");
181 return IOTCON_ERROR_INVALID_PARAMETER;
184 signal_number = icl_dbus_generate_signal_number();
186 ic_dbus_call_register_resource_sync(icl_dbus_get_object(), uri_path, types, ifaces,
187 properties, signal_number, &(resource->handle), NULL, &error);
189 ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message);
193 return IOTCON_ERROR_DBUS;
197 if (0 == resource->handle) {
198 ERR("iotcon-daemon Fail");
200 return IOTCON_ERROR_IOTIVITY;
204 resource->user_data = user_data;
206 resource->types = icl_resource_types_ref(res_types);
207 resource->uri_path = ic_utils_strdup(uri_path);
208 resource->ifaces = ifaces;
209 resource->is_observable = properties & IOTCON_OBSERVABLE;
211 snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER,
214 sub_id = icl_dbus_subscribe_signal(sig_name, resource, _icl_resource_conn_cleanup,
215 _icl_request_handler);
217 ERR("icl_dbus_subscribe_signal() Fail");
219 return IOTCON_ERROR_DBUS;
222 resource->sub_id = sub_id;
224 *resource_handle = resource;
226 return IOTCON_ERROR_NONE;
230 API int iotcon_unregister_resource(iotcon_resource_h resource)
234 GError *error = NULL;
236 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
237 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
239 if (0 == resource->sub_id) {
240 WARN("Invalid Resource handle");
241 iotcon_resource_types_destroy(resource->types);
242 free(resource->uri_path);
244 return IOTCON_ERROR_NONE;
247 ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle,
250 ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message);
252 return IOTCON_ERROR_DBUS;
255 if (IOTCON_ERROR_NONE != ret) {
256 ERR("iotcon-daemon Fail(%d)", ret);
257 return icl_dbus_convert_daemon_error(ret);
259 resource->handle = 0;
261 icl_dbus_unsubscribe_signal(resource->sub_id);
263 return IOTCON_ERROR_NONE;
267 API int iotcon_resource_bind_interface(iotcon_resource_h resource, int iface)
271 GError *error = NULL;
273 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
274 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
275 if (0 == resource->sub_id) {
276 ERR("Invalid Resource handle");
277 return IOTCON_ERROR_INVALID_PARAMETER;
280 ic_dbus_call_bind_interface_sync(icl_dbus_get_object(), resource->handle,
281 iface, &ret, NULL, &error);
283 ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message);
285 return IOTCON_ERROR_DBUS;
288 if (IOTCON_ERROR_NONE != ret) {
289 ERR("iotcon-daemon Fail(%d)", ret);
290 return icl_dbus_convert_daemon_error(ret);
297 API int iotcon_resource_bind_type(iotcon_resource_h resource, const char *resource_type)
301 GError *error = NULL;
303 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
304 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
305 RETV_IF(NULL == resource_type, IOTCON_ERROR_INVALID_PARAMETER);
306 if (ICL_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type)) {
307 ERR("Invalid resource_type(%s)", resource_type);
308 return IOTCON_ERROR_INVALID_PARAMETER;
311 if (0 == resource->sub_id) {
312 ERR("Invalid Resource handle");
313 return IOTCON_ERROR_INVALID_PARAMETER;
316 ic_dbus_call_bind_type_sync(icl_dbus_get_object(), resource->handle, resource_type,
319 ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message);
321 return IOTCON_ERROR_DBUS;
324 if (IOTCON_ERROR_NONE != ret) {
325 ERR("iotcon-daemon Fail(%d)", ret);
326 return icl_dbus_convert_daemon_error(ret);
333 API int iotcon_resource_bind_request_handler(iotcon_resource_h resource,
334 iotcon_request_handler_cb cb)
336 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
337 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
339 WARN("Request handler is changed");
342 return IOTCON_ERROR_NONE;
346 API int iotcon_resource_bind_child_resource(iotcon_resource_h parent,
347 iotcon_resource_h child)
352 GError *error = NULL;
354 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
355 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
356 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
357 RETV_IF(parent == child, IOTCON_ERROR_INVALID_PARAMETER);
359 if (0 == parent->sub_id) {
360 ERR("Invalid Resource handle(parent)");
361 return IOTCON_ERROR_INVALID_PARAMETER;
363 if (0 == child->sub_id) {
364 ERR("Invalid Resource handle(child)");
365 return IOTCON_ERROR_INVALID_PARAMETER;
368 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
369 if (child == parent->children[i]) {
370 ERR("Child resource was already bound to parent resource.");
371 return IOTCON_ERROR_ALREADY;
375 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
376 if (NULL == parent->children[i]) {
377 ic_dbus_call_bind_resource_sync(icl_dbus_get_object(), parent->handle,
378 child->handle, &ret, NULL, &error);
380 ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message);
382 return IOTCON_ERROR_DBUS;
385 if (IOTCON_ERROR_NONE != ret) {
386 ERR("iotcon-daemon Fail(%d)", ret);
387 return icl_dbus_convert_daemon_error(ret);
390 parent->children[i] = child;
392 return IOTCON_ERROR_NONE;
396 ERR("There is no slot to bind a child resource");
397 return IOTCON_ERROR_OUT_OF_MEMORY;
401 API int iotcon_resource_unbind_child_resource(iotcon_resource_h parent,
402 iotcon_resource_h child)
406 GError *error = NULL;
408 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
409 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
410 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
412 if (0 == parent->sub_id) {
413 ERR("Invalid Resource handle(parent)");
414 return IOTCON_ERROR_INVALID_PARAMETER;
416 if (0 == child->sub_id) {
417 ERR("Invalid Resource handle(child)");
418 return IOTCON_ERROR_INVALID_PARAMETER;
421 ic_dbus_call_unbind_resource_sync(icl_dbus_get_object(), parent->handle,
422 child->handle, &ret, NULL, &error);
424 ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message);
426 return IOTCON_ERROR_DBUS;
429 if (IOTCON_ERROR_NONE != ret) {
430 ERR("iotcon-daemon Fail(%d)", ret);
431 return icl_dbus_convert_daemon_error(ret);
434 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
435 if (child == parent->children[i])
436 parent->children[i] = NULL;
439 return IOTCON_ERROR_NONE;
443 API int iotcon_resource_get_number_of_children(iotcon_resource_h resource, int *number)
447 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
448 RETV_IF(NULL == number, IOTCON_ERROR_INVALID_PARAMETER);
451 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
452 if (resource->children[i])
456 return IOTCON_ERROR_NONE;
460 API int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index,
461 iotcon_resource_h *child)
463 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
464 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
465 if ((index < 0) || (ICL_CONTAINED_RESOURCES_MAX <= index)) {
466 ERR("Invalid index(%d)", index);
467 return IOTCON_ERROR_INVALID_PARAMETER;
470 *child = parent->children[index];
472 return IOTCON_ERROR_NONE;
476 /* The content of the resource should not be freed by user. */
477 API int iotcon_resource_get_uri_path(iotcon_resource_h resource, char **uri_path)
479 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
480 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
482 *uri_path = resource->uri_path;
484 return IOTCON_ERROR_NONE;
488 /* The content of the resource should not be freed by user. */
489 API int iotcon_resource_get_types(iotcon_resource_h resource,
490 iotcon_resource_types_h *types)
492 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
493 RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
495 *types = resource->types;
497 return IOTCON_ERROR_NONE;
501 API int iotcon_resource_get_interfaces(iotcon_resource_h resource, int *ifaces)
503 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
504 RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER);
506 *ifaces = resource->ifaces;
508 return IOTCON_ERROR_NONE;
512 API int iotcon_resource_is_observable(iotcon_resource_h resource, bool *observable)
514 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
515 RETV_IF(NULL == observable, IOTCON_ERROR_INVALID_PARAMETER);
517 *observable = resource->is_observable;
519 return IOTCON_ERROR_NONE;
523 API int iotcon_notimsg_create(iotcon_representation_h repr, int iface,
524 iotcon_notimsg_h *notimsg_handle)
526 iotcon_notimsg_h msg;
528 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
530 msg = calloc(1, sizeof(struct icl_notify_msg));
532 ERR("calloc() Fail(%d)", errno);
533 return IOTCON_ERROR_OUT_OF_MEMORY;
537 icl_representation_inc_ref_count(msg->repr);
539 msg->error_code = 200;
541 *notimsg_handle = msg;
543 return IOTCON_ERROR_NONE;
547 API void iotcon_notimsg_destroy(iotcon_notimsg_h msg)
551 iotcon_representation_destroy(msg->repr);
556 API int iotcon_notify_list_of_observers(iotcon_resource_h resource, iotcon_notimsg_h msg,
557 iotcon_observers_h observers)
560 GError *error = NULL;
564 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
565 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
566 RETV_IF(NULL == observers, IOTCON_ERROR_INVALID_PARAMETER);
568 if (0 == resource->sub_id) {
569 ERR("Invalid Resource handle");
570 return IOTCON_ERROR_INVALID_PARAMETER;
573 noti_msg = icl_dbus_notimsg_to_gvariant(msg);
574 if (NULL == noti_msg) {
575 ERR("icl_dbus_notimsg_to_gvariant() Fail");
576 return IOTCON_ERROR_REPRESENTATION;
578 obs = icl_dbus_observers_to_gvariant(observers);
580 ic_dbus_call_notify_list_of_observers_sync(icl_dbus_get_object(), resource->handle,
581 noti_msg, obs, &ret, NULL, &error);
583 ERR("ic_dbus_call_notify_list_of_observers_sync() Fail(%s)", error->message);
585 g_variant_unref(obs);
586 g_variant_unref(noti_msg);
587 return IOTCON_ERROR_DBUS;
590 if (IOTCON_ERROR_NONE != ret) {
591 ERR("iotcon-daemon Fail(%d)", ret);
592 return icl_dbus_convert_daemon_error(ret);
595 return IOTCON_ERROR_NONE;
599 API int iotcon_resource_notify_all(iotcon_resource_h resource)
602 GError *error = NULL;
604 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
605 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
606 if (0 == resource->sub_id) {
607 ERR("Invalid Resource handle");
608 return IOTCON_ERROR_INVALID_PARAMETER;
611 ic_dbus_call_notify_all_sync(icl_dbus_get_object(), resource->handle, &ret, NULL,
614 ERR("ic_dbus_call_notify_all_sync() Fail(%s)", error->message);
616 return IOTCON_ERROR_DBUS;
619 if (IOTCON_ERROR_NONE != ret) {
620 ERR("iotcon-daemon Fail(%d)", ret);
621 return icl_dbus_convert_daemon_error(ret);
624 return IOTCON_ERROR_NONE;