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>
27 #include "icl-representation.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.observe_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_OBSERVE_REGISTER == request.observation_info.action) {
115 if (NULL == resource->observers)
116 iotcon_observers_create(&resource->observers);
117 iotcon_observers_add(resource->observers, request.observation_info.observe_id);
118 } else if (IOTCON_OBSERVE_DEREGISTER == request.observation_info.action) {
119 iotcon_observers_remove(resource->observers, request.observation_info.observe_id);
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 if (resource->observers)
146 iotcon_observers_destroy(resource->observers);
147 free(resource->uri_path);
152 /* The length of uri_path should be less than or equal to 36. */
153 API int iotcon_resource_create(const char *uri_path,
154 iotcon_resource_types_h res_types,
157 iotcon_request_handler_cb cb,
159 iotcon_resource_h *resource_handle)
163 GError *error = NULL;
164 iotcon_resource_h resource;
165 unsigned int sub_id, signal_number;
166 char signal_name[IC_DBUS_SIGNAL_LENGTH];
168 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
169 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
170 RETVM_IF(ICL_URI_PATH_LENGTH_MAX < strlen(uri_path),
171 IOTCON_ERROR_INVALID_PARAMETER, "Invalid uri_path(%s)", uri_path);
172 RETV_IF(NULL == res_types, IOTCON_ERROR_INVALID_PARAMETER);
173 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
175 resource = calloc(1, sizeof(struct icl_resource));
176 if (NULL == resource) {
177 ERR("calloc() Fail(%d)", errno);
178 return IOTCON_ERROR_OUT_OF_MEMORY;
181 types = icl_dbus_resource_types_to_array(res_types);
183 ERR("icl_dbus_resource_types_to_array() Fail");
185 return IOTCON_ERROR_OUT_OF_MEMORY;
188 signal_number = icl_dbus_generate_signal_number();
190 ic_dbus_call_register_resource_sync(icl_dbus_get_object(), uri_path, types, ifaces,
191 properties, signal_number, &(resource->handle), NULL, &error);
193 ERR("ic_dbus_call_register_resource_sync() Fail(%s)", error->message);
194 ret = icl_dbus_convert_dbus_error(error->code);
202 if (0 == resource->handle) {
203 ERR("iotcon-daemon Fail");
205 return IOTCON_ERROR_IOTIVITY;
209 resource->user_data = user_data;
211 resource->types = icl_resource_types_ref(res_types);
212 resource->uri_path = ic_utils_strdup(uri_path);
213 resource->ifaces = ifaces;
214 resource->properties = properties;
216 snprintf(signal_name, sizeof(signal_name), "%s_%u", IC_DBUS_SIGNAL_REQUEST_HANDLER,
219 sub_id = icl_dbus_subscribe_signal(signal_name, resource, _icl_resource_conn_cleanup,
220 _icl_request_handler);
222 ERR("icl_dbus_subscribe_signal() Fail");
223 iotcon_resource_types_destroy(res_types);
224 free(resource->uri_path);
226 return IOTCON_ERROR_DBUS;
229 resource->sub_id = sub_id;
231 *resource_handle = resource;
233 return IOTCON_ERROR_NONE;
237 API int iotcon_resource_destroy(iotcon_resource_h resource)
241 GError *error = NULL;
243 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
245 if (0 == resource->handle) { /* iotcon dbus disconnected */
246 WARN("Invalid Resource handle");
247 iotcon_resource_types_destroy(resource->types);
248 if (resource->observers)
249 iotcon_observers_destroy(resource->observers);
250 free(resource->uri_path);
252 return IOTCON_ERROR_NONE;
255 if (NULL == icl_dbus_get_object()) {
256 ERR("icl_dbus_get_object() return NULL");
257 return IOTCON_ERROR_DBUS;
260 ic_dbus_call_unregister_resource_sync(icl_dbus_get_object(), resource->handle, NULL,
263 ERR("ic_dbus_call_unregister_resource_sync() Fail(%s)", error->message);
264 ret = icl_dbus_convert_dbus_error(error->code);
268 resource->handle = 0;
269 icl_dbus_unsubscribe_signal(resource->sub_id);
271 return IOTCON_ERROR_NONE;
275 API int iotcon_resource_bind_interface(iotcon_resource_h resource,
276 iotcon_interface_e iface)
280 GError *error = NULL;
282 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
283 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
284 if (0 == resource->sub_id) {
285 ERR("Invalid Resource handle");
286 return IOTCON_ERROR_INVALID_PARAMETER;
289 ic_dbus_call_bind_interface_sync(icl_dbus_get_object(), resource->handle,
290 iface, &ret, NULL, &error);
292 ERR("ic_dbus_call_bind_interface_sync() Fail(%s)", error->message);
293 ret = icl_dbus_convert_dbus_error(error->code);
298 if (IOTCON_ERROR_NONE != ret) {
299 ERR("iotcon-daemon Fail(%d)", ret);
300 return icl_dbus_convert_daemon_error(ret);
307 API int iotcon_resource_bind_type(iotcon_resource_h resource, const char *resource_type)
311 GError *error = NULL;
313 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
314 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
315 RETV_IF(NULL == resource_type, IOTCON_ERROR_INVALID_PARAMETER);
316 if (ICL_RESOURCE_TYPE_LENGTH_MAX < strlen(resource_type)) {
317 ERR("Invalid resource_type(%s)", resource_type);
318 return IOTCON_ERROR_INVALID_PARAMETER;
321 if (0 == resource->sub_id) {
322 ERR("Invalid Resource handle");
323 return IOTCON_ERROR_INVALID_PARAMETER;
326 ic_dbus_call_bind_type_sync(icl_dbus_get_object(), resource->handle, resource_type,
329 ERR("ic_dbus_call_bind_type_sync() Fail(%s)", error->message);
330 ret = icl_dbus_convert_dbus_error(error->code);
335 if (IOTCON_ERROR_NONE != ret) {
336 ERR("iotcon-daemon Fail(%d)", ret);
337 return icl_dbus_convert_daemon_error(ret);
343 API int iotcon_resource_set_request_handler(iotcon_resource_h resource,
344 iotcon_request_handler_cb cb, void *user_data)
346 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
347 RETV_IF(NULL == cb, IOTCON_ERROR_INVALID_PARAMETER);
349 DBG("Request handler is changed");
351 resource->user_data = user_data;
353 return IOTCON_ERROR_NONE;
357 API int iotcon_resource_bind_child_resource(iotcon_resource_h parent,
358 iotcon_resource_h child)
360 GError *error = NULL;
363 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
364 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
365 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
366 RETV_IF(parent == child, IOTCON_ERROR_INVALID_PARAMETER);
368 if (0 == parent->sub_id) {
369 ERR("Invalid Resource handle(parent)");
370 return IOTCON_ERROR_INVALID_PARAMETER;
372 if (0 == child->sub_id) {
373 ERR("Invalid Resource handle(child)");
374 return IOTCON_ERROR_INVALID_PARAMETER;
377 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
378 if (child == parent->children[i]) {
379 ERR("Child resource was already bound to parent resource.");
380 return IOTCON_ERROR_ALREADY;
384 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
385 if (NULL == parent->children[i]) {
386 ic_dbus_call_bind_resource_sync(icl_dbus_get_object(), parent->handle,
387 child->handle, &ret, NULL, &error);
389 ERR("ic_dbus_call_bind_resource_sync() Fail(%s)", error->message);
390 ret = icl_dbus_convert_dbus_error(error->code);
395 if (IOTCON_ERROR_NONE != ret) {
396 ERR("iotcon-daemon Fail(%d)", ret);
397 return icl_dbus_convert_daemon_error(ret);
400 parent->children[i] = child;
402 return IOTCON_ERROR_NONE;
406 ERR("There is no slot to bind a child resource");
407 return IOTCON_ERROR_OUT_OF_MEMORY;
411 API int iotcon_resource_unbind_child_resource(iotcon_resource_h parent,
412 iotcon_resource_h child)
414 GError *error = NULL;
417 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
418 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
419 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
421 if (0 == parent->sub_id) {
422 ERR("Invalid Resource handle(parent)");
423 return IOTCON_ERROR_INVALID_PARAMETER;
425 if (0 == child->sub_id) {
426 ERR("Invalid Resource handle(child)");
427 return IOTCON_ERROR_INVALID_PARAMETER;
430 ic_dbus_call_unbind_resource_sync(icl_dbus_get_object(), parent->handle,
431 child->handle, &ret, NULL, &error);
433 ERR("ic_dbus_call_unbind_resource_sync() Fail(%s)", error->message);
434 ret = icl_dbus_convert_dbus_error(error->code);
439 if (IOTCON_ERROR_NONE != ret) {
440 ERR("iotcon-daemon Fail(%d)", ret);
441 return icl_dbus_convert_daemon_error(ret);
444 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
445 if (child == parent->children[i])
446 parent->children[i] = NULL;
449 return IOTCON_ERROR_NONE;
453 API int iotcon_resource_get_number_of_children(iotcon_resource_h resource, int *number)
457 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
458 RETV_IF(NULL == number, IOTCON_ERROR_INVALID_PARAMETER);
461 for (i = 0; i < ICL_CONTAINED_RESOURCES_MAX; i++) {
462 if (resource->children[i])
466 return IOTCON_ERROR_NONE;
470 API int iotcon_resource_get_nth_child(iotcon_resource_h parent, int index,
471 iotcon_resource_h *child)
473 RETV_IF(NULL == parent, IOTCON_ERROR_INVALID_PARAMETER);
474 RETV_IF(NULL == child, IOTCON_ERROR_INVALID_PARAMETER);
475 if ((index < 0) || (ICL_CONTAINED_RESOURCES_MAX <= index)) {
476 ERR("Invalid index(%d)", index);
477 return IOTCON_ERROR_INVALID_PARAMETER;
480 *child = parent->children[index];
482 return IOTCON_ERROR_NONE;
486 /* The content of the resource should not be freed by user. */
487 API int iotcon_resource_get_uri_path(iotcon_resource_h resource, char **uri_path)
489 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
490 RETV_IF(NULL == uri_path, IOTCON_ERROR_INVALID_PARAMETER);
492 *uri_path = resource->uri_path;
494 return IOTCON_ERROR_NONE;
498 /* The content of the resource should not be freed by user. */
499 API int iotcon_resource_get_types(iotcon_resource_h resource,
500 iotcon_resource_types_h *types)
502 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
503 RETV_IF(NULL == types, IOTCON_ERROR_INVALID_PARAMETER);
505 *types = resource->types;
507 return IOTCON_ERROR_NONE;
511 API int iotcon_resource_get_interfaces(iotcon_resource_h resource, int *ifaces)
513 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
514 RETV_IF(NULL == ifaces, IOTCON_ERROR_INVALID_PARAMETER);
516 *ifaces = resource->ifaces;
518 return IOTCON_ERROR_NONE;
522 API int iotcon_resource_get_properties(iotcon_resource_h resource, int *properties)
524 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
525 RETV_IF(NULL == properties, IOTCON_ERROR_INVALID_PARAMETER);
527 *properties = resource->properties;
529 return IOTCON_ERROR_NONE;
532 API int iotcon_resource_notify(iotcon_resource_h resource,
533 iotcon_representation_h repr, iotcon_observers_h observers)
536 GError *error = NULL;
540 RETV_IF(NULL == icl_dbus_get_object(), IOTCON_ERROR_DBUS);
541 RETV_IF(NULL == resource, IOTCON_ERROR_INVALID_PARAMETER);
543 if (0 == resource->sub_id) {
544 ERR("Invalid Resource handle");
545 return IOTCON_ERROR_INVALID_PARAMETER;
548 repr_gvar = icl_dbus_representation_to_gvariant(repr);
549 if (NULL == repr_gvar) {
550 ERR("icl_representation_to_gvariant() Fail");
551 return IOTCON_ERROR_SYSTEM;
555 obs = icl_dbus_observers_to_gvariant(observers);
557 obs = icl_dbus_observers_to_gvariant(resource->observers);
559 ic_dbus_call_notify_sync(icl_dbus_get_object(), resource->handle, repr_gvar, obs,
562 ERR("ic_dbus_call_notify_sync() Fail(%s)", error->message);
563 ret = icl_dbus_convert_dbus_error(error->code);
565 g_variant_unref(obs);
566 g_variant_unref(repr_gvar);
570 if (IOTCON_ERROR_NONE != ret) {
571 ERR("iotcon-daemon Fail(%d)", ret);
572 return icl_dbus_convert_daemon_error(ret);
575 return IOTCON_ERROR_NONE;