2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
4 * Contact: Jin Yoon <jinny.yoon@samsung.com>
5 * Geunsun Lee <gs86.lee@samsung.com>
6 * Eunyoung Lee <ey928.lee@samsung.com>
7 * Junkyu Han <junkyu.han@samsung.com>
9 * Licensed under the Flora License, Version 1.1 (the License);
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://floralicense.org/license/
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an AS IS BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
26 #include <app_common.h>
30 #include "connectivity.h"
32 #include "controller_util.h"
33 #include "connection_manager.h"
35 #define DEFAULT_RESOURCE_TYPE "org.tizen.door"
37 #define URI_PATH_LEN 64
38 #define URI_PATH "/door/1"
40 #define IOTCON_DB_FILENAME "iotcon-test-svr-db-server.dat"
42 struct _connectivity_resource {
46 connectivity_protocol_e protocol_type;
47 GHashTable *value_hash;
50 iotcon_resource_h res;
51 iotcon_observers_h observers;
61 DATA_VAL_TYPE_BOOL = 0,
65 } conn_data_val_type_e;
67 typedef struct _conn_data_value_s {
68 conn_data_val_type_e type;
77 static connectivity_protocol_e ProtocolType = CONNECTIVITY_PROTOCOL_DEFAULT;
78 static int connectivity_iotcon_intialized = 0;
79 static int connectivity_http_intialized = 0;
81 static void _print_iotcon_error(int err_no)
84 case IOTCON_ERROR_NOT_SUPPORTED:
85 _E("IOTCON_ERROR_NOT_SUPPORTED");
87 case IOTCON_ERROR_PERMISSION_DENIED:
88 _E("IOTCON_ERROR_PERMISSION_DENIED");
90 case IOTCON_ERROR_INVALID_PARAMETER:
91 _E("IOTCON_ERROR_INVALID_PARAMETER");
94 _E("Error : [%d]", err_no);
101 static void _copy_file(const char *in_filename, const char *out_filename)
103 char buf[BUFSIZE] = { 0, };
108 ret_if(!in_filename);
109 ret_if(!out_filename);
111 in = fopen(in_filename, "r");
114 out = fopen(out_filename, "w");
115 goto_if(!out, error);
118 while ((nread = fread(buf, 1, sizeof(buf), in)) > 0) {
119 if (fwrite(buf, 1, nread, out) < nread) {
120 _E("critical error to copy a file");
135 static bool _query_cb(const char *key, const char *value, void *user_data)
137 _D("Key : [%s], Value : [%s]", key, value);
139 return IOTCON_FUNC_CONTINUE;
142 static int _handle_query(iotcon_request_h request)
144 iotcon_query_h query = NULL;
147 ret = iotcon_request_get_query(request, &query);
148 retv_if(IOTCON_ERROR_NONE != ret, -1);
150 if (query) iotcon_query_foreach(query, _query_cb, NULL);
155 static int _handle_request_by_crud_type(iotcon_request_h request, connectivity_resource_s *resource_info)
157 iotcon_request_type_e type;
160 ret = iotcon_request_get_request_type(request, &type);
161 retv_if(IOTCON_ERROR_NONE != ret, -1);
164 case IOTCON_REQUEST_GET:
165 _I("Do not support 'get' query");
167 case IOTCON_REQUEST_PUT:
168 _I("Do not support 'put' query");
170 case IOTCON_REQUEST_POST:
171 _I("Do not support 'post' query");
173 case IOTCON_REQUEST_DELETE:
174 _I("Do not support 'delete' query");
177 _E("Cannot reach here");
181 retv_if(0 != ret, -1);
186 static int _handle_observer(iotcon_request_h request, iotcon_observers_h observers)
188 iotcon_observe_type_e observe_type;
192 ret = iotcon_request_get_observe_type(request, &observe_type);
193 retv_if(IOTCON_ERROR_NONE != ret, -1);
195 if (IOTCON_OBSERVE_REGISTER == observe_type) {
196 ret = iotcon_request_get_observe_id(request, &observe_id);
197 retv_if(IOTCON_ERROR_NONE != ret, -1);
199 _I("Add an observer : %d", observe_id);
201 ret = iotcon_observers_add(observers, observe_id);
202 retv_if(IOTCON_ERROR_NONE != ret, -1);
203 } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
204 ret = iotcon_request_get_observe_id(request, &observe_id);
205 retv_if(IOTCON_ERROR_NONE != ret, -1);
207 _I("Remove an observer : %d", observe_id);
209 ret = iotcon_observers_remove(observers, observe_id);
210 retv_if(IOTCON_ERROR_NONE != ret, -1);
216 static int _send_response(iotcon_request_h request, iotcon_representation_h representation, iotcon_response_result_e result)
219 iotcon_response_h response;
221 ret = iotcon_response_create(request, &response);
222 retv_if(IOTCON_ERROR_NONE != ret, -1);
224 ret = iotcon_response_set_result(response, result);
225 goto_if(IOTCON_ERROR_NONE != ret, error);
227 ret = iotcon_response_set_representation(response, representation);
228 goto_if(IOTCON_ERROR_NONE != ret, error);
230 ret = iotcon_response_send(response);
231 goto_if(IOTCON_ERROR_NONE != ret, error);
233 iotcon_response_destroy(response);
238 iotcon_response_destroy(response);
242 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
244 connectivity_resource_s *resource_info = user_data;
246 char *host_address = NULL;
250 ret = iotcon_request_get_host_address(request, &host_address);
251 goto_if(IOTCON_ERROR_NONE != ret, error);
253 _D("Host address : %s", host_address);
255 ret = _handle_query(request);
256 goto_if(IOTCON_ERROR_NONE != ret, error);
258 ret = _handle_request_by_crud_type(request, resource_info);
259 goto_if(0 != ret, error);
261 ret = _handle_observer(request, resource_info->conn_data.iotcon_data.observers);
262 goto_if(0 != ret, error);
267 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
271 static int __init_iotcon(connectivity_resource_s *resource_info)
274 iotcon_resource_types_h resource_types = NULL;
275 iotcon_resource_interfaces_h ifaces = NULL;
276 uint8_t policies = IOTCON_RESOURCE_NO_POLICY;
277 char res_path[PATH_MAX] = {0,};
278 char data_path[PATH_MAX] = {0,};
281 retv_if(!resource_info, -1);
282 retv_if(resource_info->protocol_type != CONNECTIVITY_PROTOCOL_IOTIVITY, -1);
284 prefix = app_get_resource_path();
285 retv_if(!prefix, -1);
286 snprintf(res_path, sizeof(res_path)-1, "%s%s", prefix, IOTCON_DB_FILENAME);
290 prefix = app_get_data_path();
291 retv_if(!prefix, -1);
292 snprintf(data_path, sizeof(data_path)-1, "%s%s", prefix, IOTCON_DB_FILENAME);
295 _copy_file(res_path, data_path);
297 ret = iotcon_initialize(data_path);
298 retv_if(IOTCON_ERROR_NONE != ret, -1);
300 /* TODO : If we have to set device name, naming it more gorgeous one */
301 ret = iotcon_set_device_name(DEFAULT_RESOURCE_TYPE);
302 goto_if(IOTCON_ERROR_NONE != ret, error);
304 ret = iotcon_resource_types_create(&resource_types);
305 goto_if(IOTCON_ERROR_NONE != ret, error);
307 ret = iotcon_resource_types_add(resource_types, resource_info->type);
308 goto_if(IOTCON_ERROR_NONE != ret, error);
310 ret = iotcon_resource_interfaces_create(&ifaces);
311 goto_if(IOTCON_ERROR_NONE != ret, error);
313 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_DEFAULT);
314 goto_if(IOTCON_ERROR_NONE != ret, error);
316 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_BATCH);
317 goto_if(IOTCON_ERROR_NONE != ret, error);
320 IOTCON_RESOURCE_DISCOVERABLE |
321 IOTCON_RESOURCE_OBSERVABLE |
322 IOTCON_RESOURCE_SECURE;
324 ret = iotcon_resource_create(URI_PATH,
328 _request_resource_handler,
330 &resource_info->conn_data.iotcon_data.res);
331 goto_if(IOTCON_ERROR_NONE != ret, error);
333 ret = iotcon_observers_create(&resource_info->conn_data.iotcon_data.observers);
334 goto_if(IOTCON_ERROR_NONE != ret, error);
336 iotcon_resource_types_destroy(resource_types);
337 iotcon_resource_interfaces_destroy(ifaces);
338 connectivity_iotcon_intialized = 1;
343 if (resource_types) iotcon_resource_types_destroy(resource_types);
344 if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
345 if (resource_info->conn_data.iotcon_data.res) iotcon_resource_destroy(resource_info->conn_data.iotcon_data.res);
346 iotcon_deinitialize();
350 static int __init_http(connectivity_resource_s *resource_info)
353 ret = web_util_noti_init();
355 connectivity_http_intialized = 1;
360 #ifdef PRINT_DEBUG_DETAIL
361 static bool __print_attributes_cb(iotcon_attributes_h attributes, const char *key, void *user_data)
363 iotcon_type_e type = IOTCON_TYPE_NONE;
365 iotcon_attributes_get_type(attributes, key, &type);
368 case IOTCON_TYPE_INT: {
370 iotcon_attributes_get_int(attributes, key, &value);
371 _D("key[%s] - int value [%d]", key, value);
374 case IOTCON_TYPE_BOOL: {
376 iotcon_attributes_get_bool(attributes, key, &value);
377 _D("key[%s] - bool value [%d]", key, value);
380 case IOTCON_TYPE_DOUBLE: {
382 iotcon_attributes_get_double(attributes, key, &value);
383 _D("key[%s] - double value [%lf]", key, value);
386 case IOTCON_TYPE_STR: {
388 iotcon_attributes_get_str(attributes, key, &value);
389 _D("key[%s] - string value [%s]", key, value);
392 case IOTCON_TYPE_NONE:
393 case IOTCON_TYPE_BYTE_STR:
394 case IOTCON_TYPE_NULL:
395 case IOTCON_TYPE_LIST:
396 case IOTCON_TYPE_ATTRIBUTES:
398 _W("unhandled key[%s] type[%d]", key, type);
402 return IOTCON_FUNC_CONTINUE;
406 static void __print_attribute(iotcon_attributes_h attributes)
408 #ifdef PRINT_DEBUG_DETAIL
411 iotcon_attributes_foreach(attributes, __print_attributes_cb, NULL);
416 static void _destroy_representation(iotcon_representation_h representation)
418 ret_if(!representation);
419 iotcon_representation_destroy(representation);
423 static iotcon_representation_h _create_representation_with_bool(connectivity_resource_s *resource_info, const char *key, bool value)
425 iotcon_attributes_h attributes = NULL;
426 iotcon_representation_h representation = NULL;
427 char *uri_path = NULL;
430 ret = iotcon_resource_get_uri_path(resource_info->conn_data.iotcon_data.res, &uri_path);
431 retv_if(IOTCON_ERROR_NONE != ret, NULL);
433 ret = iotcon_representation_create(&representation);
434 retv_if(IOTCON_ERROR_NONE != ret, NULL);
436 ret = iotcon_attributes_create(&attributes);
437 goto_if(IOTCON_ERROR_NONE != ret, error);
439 ret = iotcon_representation_set_uri_path(representation, uri_path);
440 goto_if(IOTCON_ERROR_NONE != ret, error);
442 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
443 goto_if(IOTCON_ERROR_NONE != ret, error);
445 ret = iotcon_attributes_add_bool(attributes, key, value);
446 goto_if(IOTCON_ERROR_NONE != ret, error);
448 ret = iotcon_representation_set_attributes(representation, attributes);
449 goto_if(IOTCON_ERROR_NONE != ret, error);
451 iotcon_attributes_destroy(attributes);
453 return representation;
456 if (attributes) iotcon_attributes_destroy(attributes);
457 if (representation) iotcon_representation_destroy(representation);
462 static iotcon_representation_h _create_representation_with_int(connectivity_resource_s *resource_info, const char *key, int value)
464 iotcon_attributes_h attributes = NULL;
465 iotcon_representation_h representation = NULL;
466 char *uri_path = NULL;
469 ret = iotcon_resource_get_uri_path(resource_info->conn_data.iotcon_data.res, &uri_path);
470 retv_if(IOTCON_ERROR_NONE != ret, NULL);
472 ret = iotcon_representation_create(&representation);
473 retv_if(IOTCON_ERROR_NONE != ret, NULL);
475 ret = iotcon_attributes_create(&attributes);
476 goto_if(IOTCON_ERROR_NONE != ret, error);
478 ret = iotcon_representation_set_uri_path(representation, uri_path);
479 goto_if(IOTCON_ERROR_NONE != ret, error);
481 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
482 goto_if(IOTCON_ERROR_NONE != ret, error);
484 ret = iotcon_attributes_add_int(attributes, key, value);
485 goto_if(IOTCON_ERROR_NONE != ret, error);
487 ret = iotcon_representation_set_attributes(representation, attributes);
488 goto_if(IOTCON_ERROR_NONE != ret, error);
490 iotcon_attributes_destroy(attributes);
492 return representation;
495 if (attributes) iotcon_attributes_destroy(attributes);
496 if (representation) iotcon_representation_destroy(representation);
501 static iotcon_representation_h _create_representation_with_double(connectivity_resource_s *resource_info, const char *key, double value)
503 iotcon_attributes_h attributes = NULL;
504 iotcon_representation_h representation = NULL;
505 char *uri_path = NULL;
508 ret = iotcon_resource_get_uri_path(resource_info->conn_data.iotcon_data.res, &uri_path);
509 retv_if(IOTCON_ERROR_NONE != ret, NULL);
511 ret = iotcon_representation_create(&representation);
512 retv_if(IOTCON_ERROR_NONE != ret, NULL);
514 ret = iotcon_attributes_create(&attributes);
515 goto_if(IOTCON_ERROR_NONE != ret, error);
517 ret = iotcon_representation_set_uri_path(representation, uri_path);
518 goto_if(IOTCON_ERROR_NONE != ret, error);
520 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
521 goto_if(IOTCON_ERROR_NONE != ret, error);
523 ret = iotcon_attributes_add_double(attributes, key, value);
524 goto_if(IOTCON_ERROR_NONE != ret, error);
526 ret = iotcon_representation_set_attributes(representation, attributes);
527 goto_if(IOTCON_ERROR_NONE != ret, error);
529 iotcon_attributes_destroy(attributes);
531 return representation;
534 if (attributes) iotcon_attributes_destroy(attributes);
535 if (representation) iotcon_representation_destroy(representation);
540 static iotcon_representation_h _create_representation_with_string(connectivity_resource_s *resource_info, const char *key, const char *value)
542 iotcon_attributes_h attributes = NULL;
543 iotcon_representation_h representation = NULL;
544 char *uri_path = NULL;
547 ret = iotcon_resource_get_uri_path(resource_info->conn_data.iotcon_data.res, &uri_path);
548 retv_if(IOTCON_ERROR_NONE != ret, NULL);
550 ret = iotcon_representation_create(&representation);
551 retv_if(IOTCON_ERROR_NONE != ret, NULL);
553 ret = iotcon_attributes_create(&attributes);
554 goto_if(IOTCON_ERROR_NONE != ret, error);
556 ret = iotcon_representation_set_uri_path(representation, uri_path);
557 goto_if(IOTCON_ERROR_NONE != ret, error);
559 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
560 goto_if(IOTCON_ERROR_NONE != ret, error);
562 ret = iotcon_attributes_add_str(attributes, key, (char *)value);
563 goto_if(IOTCON_ERROR_NONE != ret, error);
565 ret = iotcon_representation_set_attributes(representation, attributes);
566 goto_if(IOTCON_ERROR_NONE != ret, error);
568 iotcon_attributes_destroy(attributes);
570 return representation;
573 if (attributes) iotcon_attributes_destroy(attributes);
574 if (representation) iotcon_representation_destroy(representation);
579 static inline void __noti_by_http(void)
581 char *json_data = NULL;
583 json_data = web_util_get_json_string();
585 const char *url = NULL;
586 controller_util_get_address(&url);
588 web_util_noti_post(url, json_data);
590 _E("fail to get url");
593 _E("fail to get json_data");
598 int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value)
602 retv_if(!resource_info, -1);
605 _D("Notify key[%s], value[%d]", key, value);
607 switch (resource_info->protocol_type) {
608 case CONNECTIVITY_PROTOCOL_IOTIVITY:
609 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
610 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
612 iotcon_representation_h representation;
613 representation = _create_representation_with_bool(resource_info, key, value);
614 retv_if(!representation, -1);
615 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
616 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
617 if (IOTCON_ERROR_NONE != ret) {
618 _W("There are some troubles for notifying value[%d]", ret);
619 _print_iotcon_error(ret);
622 _destroy_representation(representation);
625 case CONNECTIVITY_PROTOCOL_HTTP:
626 ret = web_util_json_init();
629 ret = web_util_json_begin();
632 web_util_json_add_string("SensorPiID", resource_info->path);
633 web_util_json_add_string("SensorPiType", resource_info->type);
634 web_util_json_add_string("SensorPiIP", resource_info->ip);
635 web_util_json_add_boolean(key, value);
640 web_util_json_fini();
643 _E("Unknown protocol type[%d]", resource_info->protocol_type);
651 int connectivity_notify_int(connectivity_resource_s *resource_info, const char *key, int value)
655 retv_if(!resource_info, -1);
658 _D("Notify key[%s], value[%d]", key, value);
660 switch (resource_info->protocol_type) {
661 case CONNECTIVITY_PROTOCOL_IOTIVITY:
662 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
663 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
665 iotcon_representation_h representation;
666 representation = _create_representation_with_int(resource_info, key, value);
667 retv_if(!representation, -1);
668 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
669 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
670 if (IOTCON_ERROR_NONE != ret) {
671 _W("There are some troubles for notifying value[%d]", ret);
672 _print_iotcon_error(ret);
675 _destroy_representation(representation);
678 case CONNECTIVITY_PROTOCOL_HTTP:
679 ret = web_util_json_init();
682 ret = web_util_json_begin();
685 web_util_json_add_string("SensorPiID", resource_info->path);
686 web_util_json_add_string("SensorPiType", resource_info->type);
687 web_util_json_add_string("SensorPiIP", resource_info->ip);
688 web_util_json_add_int(key, value);
693 web_util_json_fini();
696 _E("Unknown protocol type[%d]", resource_info->protocol_type);
703 int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value)
707 retv_if(!resource_info, -1);
710 _D("Notify key[%s], value[%lf]", key, value);
712 switch (resource_info->protocol_type) {
713 case CONNECTIVITY_PROTOCOL_IOTIVITY:
714 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
715 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
717 iotcon_representation_h representation;
718 representation = _create_representation_with_double(resource_info, key, value);
719 retv_if(!representation, -1);
720 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
721 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
722 if (IOTCON_ERROR_NONE != ret) {
723 _W("There are some troubles for notifying value[%d]", ret);
724 _print_iotcon_error(ret);
727 _destroy_representation(representation);
730 case CONNECTIVITY_PROTOCOL_HTTP:
731 ret = web_util_json_init();
734 ret = web_util_json_begin();
737 web_util_json_add_string("SensorPiID", resource_info->path);
738 web_util_json_add_string("SensorPiType", resource_info->type);
739 web_util_json_add_string("SensorPiIP", resource_info->ip);
740 web_util_json_add_double(key, value);
745 web_util_json_fini();
748 _E("Unknown protocol type[%d]", resource_info->protocol_type);
755 int connectivity_notify_string(connectivity_resource_s *resource_info, const char *key, const char *value)
759 retv_if(!resource_info, -1);
763 _D("Notify key[%s], value[%s]", key, value);
765 switch (resource_info->protocol_type) {
766 case CONNECTIVITY_PROTOCOL_IOTIVITY:
767 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
768 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
770 iotcon_representation_h representation;
771 representation = _create_representation_with_string(resource_info, key, value);
772 retv_if(!representation, -1);
773 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
774 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
775 if (IOTCON_ERROR_NONE != ret) {
776 _W("There are some troubles for notifying value[%d]", ret);
777 _print_iotcon_error(ret);
780 _destroy_representation(representation);
783 case CONNECTIVITY_PROTOCOL_HTTP:
784 ret = web_util_json_init();
787 ret = web_util_json_begin();
790 web_util_json_add_string("SensorPiID", resource_info->path);
791 web_util_json_add_string("SensorPiType", resource_info->type);
792 web_util_json_add_string("SensorPiIP", resource_info->ip);
793 web_util_json_add_string(key, value);
798 web_util_json_fini();
801 _E("Unknown protocol type[%d]", resource_info->protocol_type);
808 static void __hash_key_free(gpointer data)
814 static void __hash_value_free(gpointer data)
816 conn_data_value_s *value = data;
817 if (value && (value->type == DATA_VAL_TYPE_STRING))
824 static int __add_value_to_hash(connectivity_resource_s *resource_info, const char *key, conn_data_value_s *value)
826 retv_if(!resource_info, -1);
830 if (!resource_info->value_hash) {
831 resource_info->value_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
832 __hash_key_free, __hash_value_free);
833 retv_if(!resource_info->value_hash, -1);
836 g_hash_table_insert(resource_info->value_hash, strdup(key), value);
841 int connectivity_attributes_add_bool(connectivity_resource_s *resource_info, const char *key, bool value)
844 conn_data_value_s *data_value = NULL;
846 retv_if(!resource_info, -1);
849 _D("adding key[%s] - value[%d]", key, value);
851 data_value = malloc(sizeof(conn_data_value_s));
852 retv_if(!data_value, -1);
854 data_value->type = DATA_VAL_TYPE_BOOL;
855 data_value->b_val = value;
857 ret = __add_value_to_hash(resource_info, key, data_value);
866 int connectivity_attributes_add_int(connectivity_resource_s *resource_info, const char *key, int value)
869 conn_data_value_s *data_value = NULL;
871 retv_if(!resource_info, -1);
874 _D("adding key[%s] - value[%d]", key, value);
876 data_value = malloc(sizeof(conn_data_value_s));
877 retv_if(!data_value, -1);
879 data_value->type = DATA_VAL_TYPE_INT;
880 data_value->i_val = value;
882 ret = __add_value_to_hash(resource_info, key, data_value);
891 int connectivity_attributes_add_double(connectivity_resource_s *resource_info, const char *key, double value)
894 conn_data_value_s *data_value = NULL;
896 retv_if(!resource_info, -1);
899 _D("adding key[%s] - value[%lf]", key, value);
901 data_value = malloc(sizeof(conn_data_value_s));
902 retv_if(!data_value, -1);
904 data_value->type = DATA_VAL_TYPE_DOUBLE;
905 data_value->d_val = value;
907 ret = __add_value_to_hash(resource_info, key, data_value);
916 int connectivity_attributes_add_string(connectivity_resource_s *resource_info, const char *key, const char *value)
919 conn_data_value_s *data_value = NULL;
921 retv_if(!resource_info, -1);
925 _D("adding key[%s] - value[%s]", key, value);
927 data_value = malloc(sizeof(conn_data_value_s));
928 retv_if(!data_value, -1);
930 data_value->type = DATA_VAL_TYPE_STRING;
931 data_value->s_val = strdup(value);
933 ret = __add_value_to_hash(resource_info, key, data_value);
935 free(data_value->s_val);
943 int connectivity_attributes_remove_value_by_key(connectivity_resource_s *resource_info, const char *key)
945 retv_if(!resource_info, -1);
948 if (resource_info->value_hash)
949 g_hash_table_remove(resource_info->value_hash, key);
951 if (g_hash_table_size(resource_info->value_hash) == 0) {
952 g_hash_table_unref(resource_info->value_hash);
953 resource_info->value_hash = NULL;
959 int connectivity_attributes_remove_all(connectivity_resource_s *resource_info)
961 retv_if(!resource_info, -1);
963 if (resource_info->value_hash) {
964 g_hash_table_destroy(resource_info->value_hash);
965 resource_info->value_hash = NULL;
971 static void __json_add_data_iter_cb(gpointer key, gpointer value, gpointer user_data)
974 conn_data_value_s *data = value;
980 switch (data->type) {
981 case DATA_VAL_TYPE_BOOL:
982 ret = web_util_json_add_boolean(name, data->b_val);
983 if (IOTCON_ERROR_NONE != ret)
984 _E("failed to add key[%s], value[%d]", name, data->b_val);
986 case DATA_VAL_TYPE_INT:
987 ret = web_util_json_add_int(name, data->i_val);
988 if (IOTCON_ERROR_NONE != ret)
989 _E("failed to add key[%s], value[%d]", name, data->i_val);
991 case DATA_VAL_TYPE_DOUBLE:
992 ret = web_util_json_add_double(name, data->d_val);
993 if (IOTCON_ERROR_NONE != ret)
994 _E("failed to add key[%s], value[%lf]", name, data->d_val);
996 case DATA_VAL_TYPE_STRING:
997 ret = web_util_json_add_string(name, data->s_val);
998 if (IOTCON_ERROR_NONE != ret)
999 _E("failed to add key[%s], value[%s]", name, data->s_val);
1002 _E("Unknown data type [%d]", data->type);
1009 static void __attr_add_data_iter_cb(gpointer key, gpointer value, gpointer user_data)
1012 conn_data_value_s *data = value;
1013 iotcon_attributes_h attr = user_data;
1020 switch (data->type) {
1021 case DATA_VAL_TYPE_BOOL:
1022 ret = iotcon_attributes_add_bool(attr, name, data->b_val);
1023 if (IOTCON_ERROR_NONE != ret)
1024 _E("failed to add key[%s], value[%d]", name, data->b_val);
1026 case DATA_VAL_TYPE_INT:
1027 ret = iotcon_attributes_add_int(attr, name, data->i_val);
1028 if (IOTCON_ERROR_NONE != ret)
1029 _E("failed to add key[%s], value[%d]", name, data->i_val);
1031 case DATA_VAL_TYPE_DOUBLE:
1032 ret = iotcon_attributes_add_double(attr, name, data->d_val);
1033 if (IOTCON_ERROR_NONE != ret)
1034 _E("failed to add key[%s], value[%lf]", name, data->d_val);
1036 case DATA_VAL_TYPE_STRING:
1037 ret = iotcon_attributes_add_str(attr, name, data->s_val);
1038 if (IOTCON_ERROR_NONE != ret)
1039 _E("failed to add key[%s], value[%s]", name, data->s_val);
1042 _E("Unknown data type [%d]", data->type);
1049 static int __create_attributes(connectivity_resource_s *resource_info, iotcon_attributes_h *attributes)
1052 iotcon_attributes_h attr = NULL;
1054 ret = iotcon_attributes_create(&attr);
1055 if (IOTCON_ERROR_NONE != ret) {
1056 _print_iotcon_error(ret);
1060 ret = iotcon_attributes_add_str(attr, PATH, resource_info->path);
1061 if (IOTCON_ERROR_NONE != ret) {
1062 _print_iotcon_error(ret);
1063 iotcon_attributes_destroy(attr);
1066 if (resource_info->value_hash)
1067 g_hash_table_foreach(resource_info->value_hash, __attr_add_data_iter_cb, attr);
1074 int connectivity_attributes_notify_all(connectivity_resource_s *resource_info)
1078 retv_if(!resource_info, -1);
1080 if (resource_info->value_hash == NULL) {
1081 _W("You have nothing to notify now");
1086 switch (resource_info->protocol_type) {
1087 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1088 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
1089 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
1091 char *uri_path = NULL;
1092 iotcon_representation_h representation = NULL;
1093 iotcon_attributes_h attributes = NULL;
1096 iotcon_ret = iotcon_resource_get_uri_path(resource_info->conn_data.iotcon_data.res, &uri_path);
1097 retv_if(IOTCON_ERROR_NONE != iotcon_ret, -1);
1099 iotcon_ret = iotcon_representation_create(&representation);
1100 retv_if(IOTCON_ERROR_NONE != iotcon_ret, -1);
1102 iotcon_ret = iotcon_representation_set_uri_path(representation, uri_path);
1103 if (IOTCON_ERROR_NONE != iotcon_ret) {
1104 _print_iotcon_error(iotcon_ret);
1108 iotcon_ret = __create_attributes(resource_info, &attributes);
1110 _E("failed to create attributes");
1113 __print_attribute(attributes);
1115 iotcon_ret = iotcon_representation_set_attributes(representation, attributes);
1116 if (IOTCON_ERROR_NONE != iotcon_ret) {
1117 _print_iotcon_error(iotcon_ret);
1121 iotcon_ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res, representation,
1122 resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
1123 if (IOTCON_ERROR_NONE != iotcon_ret) {
1124 _print_iotcon_error(iotcon_ret);
1128 iotcon_representation_destroy(representation);
1129 iotcon_attributes_destroy(attributes);
1132 case CONNECTIVITY_PROTOCOL_HTTP:
1133 ret = web_util_json_init();
1136 ret = web_util_json_begin();
1139 web_util_json_add_string("SensorPiID", resource_info->path);
1140 web_util_json_add_string("SensorPiType", resource_info->type);
1141 web_util_json_add_string("SensorPiIP", resource_info->ip);
1142 g_hash_table_foreach(resource_info->value_hash, __json_add_data_iter_cb, NULL);
1143 web_util_json_end();
1147 web_util_json_fini();
1153 g_hash_table_destroy(resource_info->value_hash);
1154 resource_info->value_hash = NULL;
1159 void connectivity_unset_resource(connectivity_resource_s *resource_info)
1161 ret_if(!resource_info);
1163 switch (resource_info->protocol_type) {
1164 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1165 if (resource_info->conn_data.iotcon_data.observers) iotcon_observers_destroy(resource_info->conn_data.iotcon_data.observers);
1166 if (resource_info->conn_data.iotcon_data.res) iotcon_resource_destroy(resource_info->conn_data.iotcon_data.res);
1167 iotcon_deinitialize();
1168 connectivity_iotcon_intialized = 0;
1170 case CONNECTIVITY_PROTOCOL_HTTP:
1171 web_util_noti_fini();
1172 connectivity_http_intialized = 0;
1178 if (resource_info->path) free(resource_info->path);
1179 if (resource_info->type) free(resource_info->type);
1180 if (resource_info->ip) free(resource_info->ip);
1181 free(resource_info);
1186 int connectivity_set_resource(const char *path, const char *type, connectivity_resource_s **out_resource_info)
1188 connectivity_resource_s *resource_info = NULL;
1189 const char *ip = NULL;
1194 retv_if(!out_resource_info, -1);
1196 resource_info = calloc(1, sizeof(connectivity_resource_s));
1197 retv_if(!resource_info, -1);
1199 resource_info->path = strdup(path);
1200 goto_if(!resource_info->path, error);
1202 resource_info->type = strdup(type);
1203 goto_if(!resource_info->type, error);
1205 connection_manager_get_ip(&ip);
1206 resource_info->ip = strdup(ip);
1207 goto_if(!resource_info->ip, error);
1209 resource_info->protocol_type = ProtocolType;
1211 _D("Path[%s], Type[%s], protocol_type[%d]" , resource_info->path, resource_info->type, resource_info->protocol_type);
1213 switch (resource_info->protocol_type) {
1214 case CONNECTIVITY_PROTOCOL_DEFAULT:
1215 _D("default protocol type is iotivity\n \
1216 To set connectivity use connectivity_set_protocol_type() function");
1217 resource_info->protocol_type = CONNECTIVITY_PROTOCOL_IOTIVITY;
1218 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1219 ret = __init_iotcon(resource_info);
1220 goto_if(ret, error);
1222 case CONNECTIVITY_PROTOCOL_HTTP:
1223 ret = __init_http(resource_info);
1224 goto_if(ret, error);
1231 *out_resource_info = resource_info;
1236 if (resource_info->path) free(resource_info->path);
1237 if (resource_info->type) free(resource_info->type);
1238 if (resource_info->ip) free(resource_info->ip);
1239 if (resource_info) free(resource_info);
1244 int connectivity_set_protocol(connectivity_protocol_e protocol_type)
1247 retv_if(protocol_type >= CONNECTIVITY_PROTOCOL_MAX, -1);
1249 switch (protocol_type) {
1250 case CONNECTIVITY_PROTOCOL_DEFAULT:
1251 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1252 if (connectivity_iotcon_intialized) {
1253 _E("protocol type[%d] already initialized", protocol_type);
1256 ProtocolType = CONNECTIVITY_PROTOCOL_IOTIVITY;
1258 case CONNECTIVITY_PROTOCOL_HTTP:
1259 if (connectivity_http_intialized) {
1260 _E("protocol type[%d] already initialized", protocol_type);
1263 ProtocolType = CONNECTIVITY_PROTOCOL_HTTP;
1266 _E("unknown protocol type[%d]", protocol_type);