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.
22 #include <tizen_type.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"
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,
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;
57 g_variant_get(parameters, "(siia(qs)a(ss)iiavxx)",
58 &request.host_address,
59 &request.connectivity_type,
63 &request.observation_info.action,
64 &request.observation_info.observer_id,
66 &request.oic_request_h,
67 &request.oic_resource_h);
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);
79 while (g_variant_iter_loop(options, "(q&s)", &option_id, &option_data))
80 iotcon_options_add(request.header_options, option_id, option_data);
82 g_variant_iter_free(options);
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);
95 while (g_variant_iter_loop(query, "(&s&s)", &key, &value))
96 iotcon_query_add(request.query, key, value);
98 g_variant_iter_free(query);
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");
105 iotcon_query_destroy(request.query);
106 if (request.header_options)
107 iotcon_options_destroy(request.header_options);
111 g_variant_iter_free(repr_iter);
113 /* for iotcon_resource_notify */
114 if (IOTCON_REQUEST_OBSERVE & request.types) {
115 int observer_id = request.observation_info.observer_id;
116 if (IOTCON_OBSERVE_REGISTER == request.observation_info.action) {
117 if (NULL == resource->observers)
118 iotcon_observers_create(&resource->observers);
119 iotcon_observers_add(resource->observers, observer_id);
120 } else if (IOTCON_OBSERVE_DEREGISTER == request.observation_info.action) {
121 iotcon_observers_remove(resource->observers, observer_id);
126 cb(resource, &request, resource->user_data);
128 /* To avoid unnecessary ERR log (representation could be NULL) */
130 iotcon_representation_destroy(request.repr);
132 iotcon_query_destroy(request.query);
133 if (request.header_options)
134 iotcon_options_destroy(request.header_options);
138 static void _icl_resource_conn_cleanup(iotcon_resource_h resource)
140 resource->sub_id = 0;
142 if (resource->handle) {
143 resource->handle = 0;
147 iotcon_resource_types_destroy(resource->types);
148 if (resource->observers)
149 iotcon_observers_destroy(resource->observers);
150 free(resource->uri_path);
155 /* The length of uri_path should be less than or equal to 36. */
156 API int iotcon_resource_create(const char *uri_path,
157 iotcon_resource_types_h res_types,
160 iotcon_request_handler_cb cb,
162 iotcon_resource_h *resource_handle)
165 GError *error = NULL;
167 iotcon_resource_h resource;
168 int signal_number, ret;
169 char sig_name[IC_DBUS_SIGNAL_LENGTH];
171 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
172 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
173 RETVM_IF(ICL_URI_PATH_LENGTH_MAX < strlen(uri_path),
174 IOTCON_ERROR_INVALID_PARAMETER, "Invalid uri_path(%s)", uri_path);
175 RETV_IF(NULL == res_types, IOTCON_ERROR_INVALID_PARAMETER);
176 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
178 resource = calloc(1, sizeof(struct icl_resource));
179 if (NULL == resource) {
180 ERR("calloc() Fail(%d)", errno);
181 return IOTCON_ERROR_OUT_OF_MEMORY;
184 types = icl_dbus_resource_types_to_array(res_types);
186 ERR("icl_dbus_resource_types_to_array() Fail");
188 return IOTCON_ERROR_OUT_OF_MEMORY;
191 signal_number = icl_dbus_generate_signal_number();
193 ic_dbus_call_register_resource_sync(icl_dbus_get_object(), uri_path, types, ifaces,
194 properties, signal_number, &(resource->handle), NULL, &error);
196 ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message);
197 ret = icl_dbus_convert_dbus_error(error->code);
205 if (0 == resource->handle) {
206 ERR("iotcon-daemon Fail");
208 return IOTCON_ERROR_IOTIVITY;
212 resource->user_data = user_data;
214 resource->types = icl_resource_types_ref(res_types);
215 resource->uri_path = ic_utils_strdup(uri_path);
216 resource->ifaces = ifaces;
217 resource->properties = properties;
219 snprintf(sig_name, sizeof(sig_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER,
222 sub_id = icl_dbus_subscribe_signal(sig_name, resource, _icl_resource_conn_cleanup,
223 _icl_request_handler);
225 ERR("icl_dbus_subscribe_signal() Fail");
226 iotcon_resource_types_destroy(res_types);
227 free(resource->uri_path);
229 return IOTCON_ERROR_DBUS;
232 resource->sub_id = sub_id;
234 *resource_handle = resource;
236 return IOTCON_ERROR_NONE;
240 API int iotcon_resource_destroy(iotcon_resource_h resource)
244 GError *error = NULL;
246 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
248 if (0 == resource->handle) { /* iotcon dbus disconnected */
249 WARN("Invalid Resource handle");
250 iotcon_resource_types_destroy(resource->types);
251 if (resource->observers)
252 iotcon_observers_destroy(resource->observers);
253 free(resource->uri_path);
255 return IOTCON_ERROR_NONE;
258 if (NULL == icl_dbus_get_object()) {
259 ERR("icl_dbus_get_object() return NULL");
260 return IOTCON_ERROR_DBUS;
263 ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle, NULL,
266 ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message);
267 ret = icl_dbus_convert_dbus_error(error->code);
271 resource->handle = 0;
272 icl_dbus_unsubscribe_signal(resource->sub_id);
274 return IOTCON_ERROR_NONE;
278 API int iotcon_resource_bind_interface(iotcon_resource_h resource,
279 iotcon_interface_e iface)
283 GError *error = NULL;
285 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
286 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
287 if (0 == resource->sub_id) {
288 ERR("Invalid Resource handle");
289 return IOTCON_ERROR_INVALID_PARAMETER;
292 ic_dbus_call_bind_interface_sync(icl_dbus_get_object(), resource->handle,
293 iface, &ret, NULL, &error);
295 ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message);
296 ret = icl_dbus_convert_dbus_error(error->code);
301 if (IOTCON_ERROR_NONE != ret) {
302 ERR("iotcon-daemon Fail(%d)", ret);
303 return icl_dbus_convert_daemon_error(ret);
310 API int iotcon_resource_bind_type(iotcon_resource_h resource, const char *resource_type)
314 GError *error = NULL;
316 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
317 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
318 RETV_IF(NULL == resource_type, IOTCON_ERROR_INVALID_PARAMETER);
319 if (ICL_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type)) {
320 ERR("Invalid resource_type(%s)", resource_type);
321 return IOTCON_ERROR_INVALID_PARAMETER;
324 if (0 == resource->sub_id) {
325 ERR("Invalid Resource handle");
326 return IOTCON_ERROR_INVALID_PARAMETER;
329 ic_dbus_call_bind_type_sync(icl_dbus_get_object(), resource->handle, resource_type,
332 ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message);
333 ret = icl_dbus_convert_dbus_error(error->code);
338 if (IOTCON_ERROR_NONE != ret) {
339 ERR("iotcon-daemon Fail(%d)", ret);
340 return icl_dbus_convert_daemon_error(ret);
347 API int iotcon_resource_set_request_handler(iotcon_resource_h resource,
348 iotcon_request_handler_cb cb)
350 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
351 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
353 WARN("Request handler is changed");
356 return IOTCON_ERROR_NONE;
360 API int iotcon_resource_bind_child_resource(iotcon_resource_h parent,
361 iotcon_resource_h child)
363 GError *error = NULL;
366 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
367 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
368 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
369 RETV_IF(parent == child, IOTCON_ERROR_INVALID_PARAMETER);
371 if (0 == parent->sub_id) {
372 ERR("Invalid Resource handle(parent)");
373 return IOTCON_ERROR_INVALID_PARAMETER;
375 if (0 == child->sub_id) {
376 ERR("Invalid Resource handle(child)");
377 return IOTCON_ERROR_INVALID_PARAMETER;
380 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
381 if (child == parent->children[i]) {
382 ERR("Child resource was already bound to parent resource.");
383 return IOTCON_ERROR_ALREADY;
387 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
388 if (NULL == parent->children[i]) {
389 ic_dbus_call_bind_resource_sync(icl_dbus_get_object(), parent->handle,
390 child->handle, &ret, NULL, &error);
392 ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message);
393 ret = icl_dbus_convert_dbus_error(error->code);
398 if (IOTCON_ERROR_NONE != ret) {
399 ERR("iotcon-daemon Fail(%d)", ret);
400 return icl_dbus_convert_daemon_error(ret);
403 parent->children[i] = child;
405 return IOTCON_ERROR_NONE;
409 ERR("There is no slot to bind a child resource");
410 return IOTCON_ERROR_OUT_OF_MEMORY;
414 API int iotcon_resource_unbind_child_resource(iotcon_resource_h parent,
415 iotcon_resource_h child)
417 GError *error = NULL;
420 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
421 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
422 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
424 if (0 == parent->sub_id) {
425 ERR("Invalid Resource handle(parent)");
426 return IOTCON_ERROR_INVALID_PARAMETER;
428 if (0 == child->sub_id) {
429 ERR("Invalid Resource handle(child)");
430 return IOTCON_ERROR_INVALID_PARAMETER;
433 ic_dbus_call_unbind_resource_sync(icl_dbus_get_object(), parent->handle,
434 child->handle, &ret, NULL, &error);
436 ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message);
437 ret = icl_dbus_convert_dbus_error(error->code);
442 if (IOTCON_ERROR_NONE != ret) {
443 ERR("iotcon-daemon Fail(%d)", ret);
444 return icl_dbus_convert_daemon_error(ret);
447 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
448 if (child == parent->children[i])
449 parent->children[i] = NULL;
452 return IOTCON_ERROR_NONE;
456 API int iotcon_resource_get_number_of_children(iotcon_resource_h resource, int *number)
460 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
461 RETV_IF(NULL == number, IOTCON_ERROR_INVALID_PARAMETER);
464 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
465 if (resource->children[i])
469 return IOTCON_ERROR_NONE;
473 API int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index,
474 iotcon_resource_h *child)
476 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
477 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
478 if ((index < 0) || (ICL_CONTAINED_RESOURCES_MAX <= index)) {
479 ERR("Invalid index(%d)", index);
480 return IOTCON_ERROR_INVALID_PARAMETER;
483 *child = parent->children[index];
485 return IOTCON_ERROR_NONE;
489 /* The content of the resource should not be freed by user. */
490 API int iotcon_resource_get_uri_path(iotcon_resource_h resource, char **uri_path)
492 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
493 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
495 *uri_path = resource->uri_path;
497 return IOTCON_ERROR_NONE;
501 /* The content of the resource should not be freed by user. */
502 API int iotcon_resource_get_types(iotcon_resource_h resource,
503 iotcon_resource_types_h *types)
505 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
506 RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
508 *types = resource->types;
510 return IOTCON_ERROR_NONE;
514 API int iotcon_resource_get_interfaces(iotcon_resource_h resource, int *ifaces)
516 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
517 RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER);
519 *ifaces = resource->ifaces;
521 return IOTCON_ERROR_NONE;
525 API int iotcon_resource_get_properties(iotcon_resource_h resource, int *properties)
527 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
528 RETV_IF(NULL == properties, IOTCON_ERROR_INVALID_PARAMETER);
530 *properties = resource->properties;
532 return IOTCON_ERROR_NONE;
535 API int iotcon_resource_notify(iotcon_resource_h resource,
536 iotcon_representation_h repr, iotcon_observers_h observers)
539 GError *error = NULL;
543 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
544 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
545 RETV_IF(NULL == repr, IOTCON_ERROR_INVALID_PARAMETER);
547 if (0 == resource->sub_id) {
548 ERR("Invalid Resource handle");
549 return IOTCON_ERROR_INVALID_PARAMETER;
552 repr_gvar = icl_dbus_representation_to_gvariant(repr);
553 if (NULL == repr_gvar) {
554 ERR("icl_representation_to_gvariant() Fail");
555 return IOTCON_ERROR_SYSTEM;
559 obs = icl_dbus_observers_to_gvariant(observers);
561 obs = icl_dbus_observers_to_gvariant(resource->observers);
563 ic_dbus_call_notify_sync(icl_dbus_get_object(), resource->handle, repr_gvar, obs, &ret,
566 ERR("ic_dbus_call_notify_sync() Fail(%s)", error->message);
567 ret = icl_dbus_convert_dbus_error(error->code);
569 g_variant_unref(obs);
570 g_variant_unref(repr_gvar);
574 if (IOTCON_ERROR_NONE != ret) {
575 ERR("iotcon-daemon Fail(%d)", ret);
576 return icl_dbus_convert_daemon_error(ret);
579 return IOTCON_ERROR_NONE;