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");
599 int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value)
603 retv_if(!resource_info, -1);
606 _D("Notify key[%s], value[%d]", key, value);
608 switch (resource_info->protocol_type) {
609 case CONNECTIVITY_PROTOCOL_IOTIVITY:
610 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
611 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
613 iotcon_representation_h representation;
614 representation = _create_representation_with_bool(resource_info, key, value);
615 retv_if(!representation, -1);
616 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
617 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
618 if (IOTCON_ERROR_NONE != ret) {
619 _W("There are some troubles for notifying value[%d]", ret);
620 _print_iotcon_error(ret);
623 _destroy_representation(representation);
626 case CONNECTIVITY_PROTOCOL_HTTP:
627 ret = web_util_json_init();
630 ret = web_util_json_begin();
633 web_util_json_add_string("SensorPiID", resource_info->path);
634 web_util_json_add_string("SensorPiType", resource_info->type);
635 web_util_json_add_string("SensorPiIP", resource_info->ip);
636 web_util_json_add_boolean(key, value);
641 web_util_json_fini();
644 _E("Unknown protocol type[%d]", resource_info->protocol_type);
652 int connectivity_notify_int(connectivity_resource_s *resource_info, const char *key, int value)
656 retv_if(!resource_info, -1);
659 _D("Notify key[%s], value[%d]", key, value);
661 switch (resource_info->protocol_type) {
662 case CONNECTIVITY_PROTOCOL_IOTIVITY:
663 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
664 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
666 iotcon_representation_h representation;
667 representation = _create_representation_with_int(resource_info, key, value);
668 retv_if(!representation, -1);
669 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
670 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
671 if (IOTCON_ERROR_NONE != ret) {
672 _W("There are some troubles for notifying value[%d]", ret);
673 _print_iotcon_error(ret);
676 _destroy_representation(representation);
679 case CONNECTIVITY_PROTOCOL_HTTP:
680 ret = web_util_json_init();
683 ret = web_util_json_begin();
686 web_util_json_add_string("SensorPiID", resource_info->path);
687 web_util_json_add_string("SensorPiType", resource_info->type);
688 web_util_json_add_string("SensorPiIP", resource_info->ip);
689 web_util_json_add_int(key, value);
694 web_util_json_fini();
697 _E("Unknown protocol type[%d]", resource_info->protocol_type);
704 int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value)
708 retv_if(!resource_info, -1);
711 _D("Notify key[%s], value[%lf]", key, value);
713 switch (resource_info->protocol_type) {
714 case CONNECTIVITY_PROTOCOL_IOTIVITY:
715 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
716 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
718 iotcon_representation_h representation;
719 representation = _create_representation_with_double(resource_info, key, value);
720 retv_if(!representation, -1);
721 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
722 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
723 if (IOTCON_ERROR_NONE != ret) {
724 _W("There are some troubles for notifying value[%d]", ret);
725 _print_iotcon_error(ret);
728 _destroy_representation(representation);
731 case CONNECTIVITY_PROTOCOL_HTTP:
732 ret = web_util_json_init();
735 ret = web_util_json_begin();
738 web_util_json_add_string("SensorPiID", resource_info->path);
739 web_util_json_add_string("SensorPiType", resource_info->type);
740 web_util_json_add_string("SensorPiIP", resource_info->ip);
741 web_util_json_add_double(key, value);
746 web_util_json_fini();
749 _E("Unknown protocol type[%d]", resource_info->protocol_type);
756 int connectivity_notify_string(connectivity_resource_s *resource_info, const char *key, const char *value)
760 retv_if(!resource_info, -1);
764 _D("Notify key[%s], value[%s]", key, value);
766 switch (resource_info->protocol_type) {
767 case CONNECTIVITY_PROTOCOL_IOTIVITY:
768 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
769 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
771 iotcon_representation_h representation;
772 representation = _create_representation_with_string(resource_info, key, value);
773 retv_if(!representation, -1);
774 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
775 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
776 if (IOTCON_ERROR_NONE != ret) {
777 _W("There are some troubles for notifying value[%d]", ret);
778 _print_iotcon_error(ret);
781 _destroy_representation(representation);
784 case CONNECTIVITY_PROTOCOL_HTTP:
785 ret = web_util_json_init();
788 ret = web_util_json_begin();
791 web_util_json_add_string("SensorPiID", resource_info->path);
792 web_util_json_add_string("SensorPiType", resource_info->type);
793 web_util_json_add_string("SensorPiIP", resource_info->ip);
794 web_util_json_add_string(key, value);
799 web_util_json_fini();
802 _E("Unknown protocol type[%d]", resource_info->protocol_type);
809 static void __hash_key_free(gpointer data)
815 static void __hash_value_free(gpointer data)
817 conn_data_value_s *value = data;
818 if (value && (value->type == DATA_VAL_TYPE_STRING))
825 static int __add_value_to_hash(connectivity_resource_s *resource_info, const char *key, conn_data_value_s *value)
827 retv_if(!resource_info, -1);
831 if (!resource_info->value_hash) {
832 resource_info->value_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
833 __hash_key_free, __hash_value_free);
834 retv_if(!resource_info->value_hash, -1);
837 g_hash_table_insert(resource_info->value_hash, strdup(key), value);
842 int connectivity_attributes_add_bool(connectivity_resource_s *resource_info, const char *key, bool value)
845 conn_data_value_s *data_value = NULL;
847 retv_if(!resource_info, -1);
850 _D("adding key[%s] - value[%d]", key, value);
852 data_value = malloc(sizeof(conn_data_value_s));
853 retv_if(!data_value, -1);
855 data_value->type = DATA_VAL_TYPE_BOOL;
856 data_value->b_val = value;
858 ret = __add_value_to_hash(resource_info, key, data_value);
867 int connectivity_attributes_add_int(connectivity_resource_s *resource_info, const char *key, int value)
870 conn_data_value_s *data_value = NULL;
872 retv_if(!resource_info, -1);
875 _D("adding key[%s] - value[%d]", key, value);
877 data_value = malloc(sizeof(conn_data_value_s));
878 retv_if(!data_value, -1);
880 data_value->type = DATA_VAL_TYPE_INT;
881 data_value->i_val = value;
883 ret = __add_value_to_hash(resource_info, key, data_value);
892 int connectivity_attributes_add_double(connectivity_resource_s *resource_info, const char *key, double value)
895 conn_data_value_s *data_value = NULL;
897 retv_if(!resource_info, -1);
900 _D("adding key[%s] - value[%lf]", key, value);
902 data_value = malloc(sizeof(conn_data_value_s));
903 retv_if(!data_value, -1);
905 data_value->type = DATA_VAL_TYPE_DOUBLE;
906 data_value->d_val = value;
908 ret = __add_value_to_hash(resource_info, key, data_value);
917 int connectivity_attributes_add_string(connectivity_resource_s *resource_info, const char *key, const char *value)
920 conn_data_value_s *data_value = NULL;
922 retv_if(!resource_info, -1);
926 _D("adding key[%s] - value[%s]", key, value);
928 data_value = malloc(sizeof(conn_data_value_s));
929 retv_if(!data_value, -1);
931 data_value->type = DATA_VAL_TYPE_STRING;
932 data_value->s_val = strdup(value);
934 ret = __add_value_to_hash(resource_info, key, data_value);
936 free(data_value->s_val);
944 int connectivity_attributes_remove_value_by_key(connectivity_resource_s *resource_info, const char *key)
946 retv_if(!resource_info, -1);
949 if (resource_info->value_hash)
950 g_hash_table_remove(resource_info->value_hash, key);
952 if (g_hash_table_size(resource_info->value_hash) == 0) {
953 g_hash_table_unref(resource_info->value_hash);
954 resource_info->value_hash = NULL;
960 int connectivity_attributes_remove_all(connectivity_resource_s *resource_info)
962 retv_if(!resource_info, -1);
964 if (resource_info->value_hash) {
965 g_hash_table_destroy(resource_info->value_hash);
966 resource_info->value_hash = NULL;
972 static void __json_add_data_iter_cb(gpointer key, gpointer value, gpointer user_data)
975 conn_data_value_s *data = value;
981 switch (data->type) {
982 case DATA_VAL_TYPE_BOOL:
983 ret = web_util_json_add_boolean(name, data->b_val);
984 if (IOTCON_ERROR_NONE != ret)
985 _E("failed to add key[%s], value[%d]", name, data->b_val);
987 case DATA_VAL_TYPE_INT:
988 ret = web_util_json_add_int(name, data->i_val);
989 if (IOTCON_ERROR_NONE != ret)
990 _E("failed to add key[%s], value[%d]", name, data->i_val);
992 case DATA_VAL_TYPE_DOUBLE:
993 ret = web_util_json_add_double(name, data->d_val);
994 if (IOTCON_ERROR_NONE != ret)
995 _E("failed to add key[%s], value[%lf]", name, data->d_val);
997 case DATA_VAL_TYPE_STRING:
998 ret = web_util_json_add_string(name, data->s_val);
999 if (IOTCON_ERROR_NONE != ret)
1000 _E("failed to add key[%s], value[%s]", name, data->s_val);
1003 _E("Unknown data type [%d]", data->type);
1010 static void __attr_add_data_iter_cb(gpointer key, gpointer value, gpointer user_data)
1013 conn_data_value_s *data = value;
1014 iotcon_attributes_h attr = user_data;
1021 switch (data->type) {
1022 case DATA_VAL_TYPE_BOOL:
1023 ret = iotcon_attributes_add_bool(attr, name, data->b_val);
1024 if (IOTCON_ERROR_NONE != ret)
1025 _E("failed to add key[%s], value[%d]", name, data->b_val);
1027 case DATA_VAL_TYPE_INT:
1028 ret = iotcon_attributes_add_int(attr, name, data->i_val);
1029 if (IOTCON_ERROR_NONE != ret)
1030 _E("failed to add key[%s], value[%d]", name, data->i_val);
1032 case DATA_VAL_TYPE_DOUBLE:
1033 ret = iotcon_attributes_add_double(attr, name, data->d_val);
1034 if (IOTCON_ERROR_NONE != ret)
1035 _E("failed to add key[%s], value[%lf]", name, data->d_val);
1037 case DATA_VAL_TYPE_STRING:
1038 ret = iotcon_attributes_add_str(attr, name, data->s_val);
1039 if (IOTCON_ERROR_NONE != ret)
1040 _E("failed to add key[%s], value[%s]", name, data->s_val);
1043 _E("Unknown data type [%d]", data->type);
1050 static int __create_attributes(connectivity_resource_s *resource_info, iotcon_attributes_h *attributes)
1053 iotcon_attributes_h attr = NULL;
1055 ret = iotcon_attributes_create(&attr);
1056 if (IOTCON_ERROR_NONE != ret) {
1057 _print_iotcon_error(ret);
1061 ret = iotcon_attributes_add_str(attr, PATH, resource_info->path);
1062 if (IOTCON_ERROR_NONE != ret) {
1063 _print_iotcon_error(ret);
1064 iotcon_attributes_destroy(attr);
1067 if (resource_info->value_hash)
1068 g_hash_table_foreach(resource_info->value_hash, __attr_add_data_iter_cb, attr);
1075 int connectivity_attributes_notify_all(connectivity_resource_s *resource_info)
1079 retv_if(!resource_info, -1);
1081 if (resource_info->value_hash == NULL) {
1082 _W("You have nothing to notify now");
1087 switch (resource_info->protocol_type) {
1088 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1089 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
1090 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
1092 char *uri_path = NULL;
1093 iotcon_representation_h representation = NULL;
1094 iotcon_attributes_h attributes = NULL;
1097 iotcon_ret = iotcon_resource_get_uri_path(resource_info->conn_data.iotcon_data.res, &uri_path);
1098 retv_if(IOTCON_ERROR_NONE != iotcon_ret, -1);
1100 iotcon_ret = iotcon_representation_create(&representation);
1101 retv_if(IOTCON_ERROR_NONE != iotcon_ret, -1);
1103 iotcon_ret = iotcon_representation_set_uri_path(representation, uri_path);
1104 if (IOTCON_ERROR_NONE != iotcon_ret) {
1105 _print_iotcon_error(iotcon_ret);
1109 iotcon_ret = __create_attributes(resource_info, &attributes);
1111 _E("failed to create attributes");
1114 __print_attribute(attributes);
1116 iotcon_ret = iotcon_representation_set_attributes(representation, attributes);
1117 if (IOTCON_ERROR_NONE != iotcon_ret) {
1118 _print_iotcon_error(iotcon_ret);
1122 iotcon_ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res, representation,
1123 resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
1124 if (IOTCON_ERROR_NONE != iotcon_ret) {
1125 _print_iotcon_error(iotcon_ret);
1129 iotcon_representation_destroy(representation);
1130 iotcon_attributes_destroy(attributes);
1133 case CONNECTIVITY_PROTOCOL_HTTP:
1134 ret = web_util_json_init();
1137 ret = web_util_json_begin();
1140 web_util_json_add_string("SensorPiID", resource_info->path);
1141 web_util_json_add_string("SensorPiType", resource_info->type);
1142 web_util_json_add_string("SensorPiIP", resource_info->ip);
1143 g_hash_table_foreach(resource_info->value_hash, __json_add_data_iter_cb, NULL);
1144 web_util_json_end();
1148 web_util_json_fini();
1154 g_hash_table_destroy(resource_info->value_hash);
1155 resource_info->value_hash = NULL;
1160 void connectivity_unset_resource(connectivity_resource_s *resource_info)
1162 ret_if(!resource_info);
1164 switch (resource_info->protocol_type) {
1165 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1166 if (resource_info->conn_data.iotcon_data.observers) iotcon_observers_destroy(resource_info->conn_data.iotcon_data.observers);
1167 if (resource_info->conn_data.iotcon_data.res) iotcon_resource_destroy(resource_info->conn_data.iotcon_data.res);
1168 iotcon_deinitialize();
1169 connectivity_iotcon_intialized = 0;
1171 case CONNECTIVITY_PROTOCOL_HTTP:
1172 web_util_noti_fini();
1173 connectivity_http_intialized = 0;
1179 if (resource_info->path) free(resource_info->path);
1180 if (resource_info->type) free(resource_info->type);
1181 if (resource_info->ip) free(resource_info->ip);
1182 free(resource_info);
1187 int connectivity_set_resource(const char *path, const char *type, connectivity_resource_s **out_resource_info)
1189 connectivity_resource_s *resource_info = NULL;
1190 const char *ip = NULL;
1195 retv_if(!out_resource_info, -1);
1197 resource_info = calloc(1, sizeof(connectivity_resource_s));
1198 retv_if(!resource_info, -1);
1200 resource_info->path = strdup(path);
1201 goto_if(!resource_info->path, error);
1203 resource_info->type = strdup(type);
1204 goto_if(!resource_info->type, error);
1206 connection_manager_get_ip(&ip);
1207 resource_info->ip = strdup(ip);
1208 goto_if(!resource_info->ip, error);
1210 resource_info->protocol_type = ProtocolType;
1212 _D("Path[%s], Type[%s], protocol_type[%d]" , resource_info->path, resource_info->type, resource_info->protocol_type);
1214 switch (resource_info->protocol_type) {
1215 case CONNECTIVITY_PROTOCOL_DEFAULT:
1216 _D("default protocol type is iotivity\n \
1217 To set connectivity use connectivity_set_protocol_type() function");
1218 resource_info->protocol_type = CONNECTIVITY_PROTOCOL_IOTIVITY;
1219 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1220 ret = __init_iotcon(resource_info);
1221 goto_if(ret, error);
1223 case CONNECTIVITY_PROTOCOL_HTTP:
1224 ret = __init_http(resource_info);
1225 goto_if(ret, error);
1232 *out_resource_info = resource_info;
1237 if (resource_info->path) free(resource_info->path);
1238 if (resource_info->type) free(resource_info->type);
1239 if (resource_info->ip) free(resource_info->ip);
1240 if (resource_info) free(resource_info);
1245 int connectivity_set_protocol(connectivity_protocol_e protocol_type)
1248 retv_if(protocol_type >= CONNECTIVITY_PROTOCOL_MAX, -1);
1250 switch (protocol_type) {
1251 case CONNECTIVITY_PROTOCOL_DEFAULT:
1252 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1253 if (connectivity_iotcon_intialized) {
1254 _E("protocol type[%d] already initialized", protocol_type);
1257 ProtocolType = CONNECTIVITY_PROTOCOL_IOTIVITY;
1259 case CONNECTIVITY_PROTOCOL_HTTP:
1260 if (connectivity_http_intialized) {
1261 _E("protocol type[%d] already initialized", protocol_type);
1264 ProtocolType = CONNECTIVITY_PROTOCOL_HTTP;
1267 _E("unknown protocol type[%d]", protocol_type);