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"
34 #define DEFAULT_RESOURCE_TYPE "org.tizen.door"
36 #define URI_PATH_LEN 64
37 #define URI_PATH "/door/1"
39 #define IOTCON_DB_FILENAME "iotcon-test-svr-db-server.dat"
41 struct _connectivity_resource {
44 connectivity_protocol_e protocol_type;
45 GHashTable *value_hash;
48 iotcon_resource_h res;
49 iotcon_observers_h observers;
59 DATA_VAL_TYPE_BOOL = 0,
63 } conn_data_val_type_e;
65 typedef struct _conn_data_value_s {
66 conn_data_val_type_e type;
75 static connectivity_protocol_e ProtocolType = CONNECTIVITY_PROTOCOL_DEFAULT;
76 static int connectivity_iotcon_intialized = 0;
77 static int connectivity_http_intialized = 0;
79 static void _print_iotcon_error(int err_no)
82 case IOTCON_ERROR_NOT_SUPPORTED:
83 _E("IOTCON_ERROR_NOT_SUPPORTED");
85 case IOTCON_ERROR_PERMISSION_DENIED:
86 _E("IOTCON_ERROR_PERMISSION_DENIED");
88 case IOTCON_ERROR_INVALID_PARAMETER:
89 _E("IOTCON_ERROR_INVALID_PARAMETER");
92 _E("Error : [%d]", err_no);
99 static void _copy_file(const char *in_filename, const char *out_filename)
101 char buf[BUFSIZE] = { 0, };
106 ret_if(!in_filename);
107 ret_if(!out_filename);
109 in = fopen(in_filename, "r");
112 out = fopen(out_filename, "w");
113 goto_if(!out, error);
116 while ((nread = fread(buf, 1, sizeof(buf), in)) > 0) {
117 if (fwrite(buf, 1, nread, out) < nread) {
118 _E("critical error to copy a file");
133 static bool _query_cb(const char *key, const char *value, void *user_data)
135 _D("Key : [%s], Value : [%s]", key, value);
137 return IOTCON_FUNC_CONTINUE;
140 static int _handle_query(iotcon_request_h request)
142 iotcon_query_h query = NULL;
145 ret = iotcon_request_get_query(request, &query);
146 retv_if(IOTCON_ERROR_NONE != ret, -1);
148 if (query) iotcon_query_foreach(query, _query_cb, NULL);
153 static int _handle_request_by_crud_type(iotcon_request_h request, connectivity_resource_s *resource_info)
155 iotcon_request_type_e type;
158 ret = iotcon_request_get_request_type(request, &type);
159 retv_if(IOTCON_ERROR_NONE != ret, -1);
162 case IOTCON_REQUEST_GET:
163 _I("Do not support 'get' query");
165 case IOTCON_REQUEST_PUT:
166 _I("Do not support 'put' query");
168 case IOTCON_REQUEST_POST:
169 _I("Do not support 'post' query");
171 case IOTCON_REQUEST_DELETE:
172 _I("Do not support 'delete' query");
175 _E("Cannot reach here");
179 retv_if(0 != ret, -1);
184 static int _handle_observer(iotcon_request_h request, iotcon_observers_h observers)
186 iotcon_observe_type_e observe_type;
190 ret = iotcon_request_get_observe_type(request, &observe_type);
191 retv_if(IOTCON_ERROR_NONE != ret, -1);
193 if (IOTCON_OBSERVE_REGISTER == observe_type) {
194 ret = iotcon_request_get_observe_id(request, &observe_id);
195 retv_if(IOTCON_ERROR_NONE != ret, -1);
197 _I("Add an observer : %d", observe_id);
199 ret = iotcon_observers_add(observers, observe_id);
200 retv_if(IOTCON_ERROR_NONE != ret, -1);
201 } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
202 ret = iotcon_request_get_observe_id(request, &observe_id);
203 retv_if(IOTCON_ERROR_NONE != ret, -1);
205 _I("Remove an observer : %d", observe_id);
207 ret = iotcon_observers_remove(observers, observe_id);
208 retv_if(IOTCON_ERROR_NONE != ret, -1);
214 static int _send_response(iotcon_request_h request, iotcon_representation_h representation, iotcon_response_result_e result)
217 iotcon_response_h response;
219 ret = iotcon_response_create(request, &response);
220 retv_if(IOTCON_ERROR_NONE != ret, -1);
222 ret = iotcon_response_set_result(response, result);
223 goto_if(IOTCON_ERROR_NONE != ret, error);
225 ret = iotcon_response_set_representation(response, representation);
226 goto_if(IOTCON_ERROR_NONE != ret, error);
228 ret = iotcon_response_send(response);
229 goto_if(IOTCON_ERROR_NONE != ret, error);
231 iotcon_response_destroy(response);
236 iotcon_response_destroy(response);
240 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data)
242 connectivity_resource_s *resource_info = user_data;
244 char *host_address = NULL;
248 ret = iotcon_request_get_host_address(request, &host_address);
249 goto_if(IOTCON_ERROR_NONE != ret, error);
251 _D("Host address : %s", host_address);
253 ret = _handle_query(request);
254 goto_if(IOTCON_ERROR_NONE != ret, error);
256 ret = _handle_request_by_crud_type(request, resource_info);
257 goto_if(0 != ret, error);
259 ret = _handle_observer(request, resource_info->conn_data.iotcon_data.observers);
260 goto_if(0 != ret, error);
265 _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
269 static int __init_iotcon(connectivity_resource_s *resource_info)
272 iotcon_resource_types_h resource_types = NULL;
273 iotcon_resource_interfaces_h ifaces = NULL;
274 uint8_t policies = IOTCON_RESOURCE_NO_POLICY;
275 char res_path[PATH_MAX] = {0,};
276 char data_path[PATH_MAX] = {0,};
279 retv_if(!resource_info, -1);
280 retv_if(resource_info->protocol_type != CONNECTIVITY_PROTOCOL_IOTIVITY, -1);
282 prefix = app_get_resource_path();
283 retv_if(!prefix, -1);
284 snprintf(res_path, sizeof(res_path)-1, "%s%s", prefix, IOTCON_DB_FILENAME);
288 prefix = app_get_data_path();
289 retv_if(!prefix, -1);
290 snprintf(data_path, sizeof(data_path)-1, "%s%s", prefix, IOTCON_DB_FILENAME);
293 _copy_file(res_path, data_path);
295 ret = iotcon_initialize(data_path);
296 retv_if(IOTCON_ERROR_NONE != ret, -1);
298 /* TODO : If we have to set device name, naming it more gorgeous one */
299 ret = iotcon_set_device_name(DEFAULT_RESOURCE_TYPE);
300 goto_if(IOTCON_ERROR_NONE != ret, error);
302 ret = iotcon_resource_types_create(&resource_types);
303 goto_if(IOTCON_ERROR_NONE != ret, error);
305 ret = iotcon_resource_types_add(resource_types, resource_info->type);
306 goto_if(IOTCON_ERROR_NONE != ret, error);
308 ret = iotcon_resource_interfaces_create(&ifaces);
309 goto_if(IOTCON_ERROR_NONE != ret, error);
311 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_DEFAULT);
312 goto_if(IOTCON_ERROR_NONE != ret, error);
314 ret = iotcon_resource_interfaces_add(ifaces, IOTCON_INTERFACE_BATCH);
315 goto_if(IOTCON_ERROR_NONE != ret, error);
318 IOTCON_RESOURCE_DISCOVERABLE |
319 IOTCON_RESOURCE_OBSERVABLE |
320 IOTCON_RESOURCE_SECURE;
322 ret = iotcon_resource_create(URI_PATH,
326 _request_resource_handler,
328 &resource_info->conn_data.iotcon_data.res);
329 goto_if(IOTCON_ERROR_NONE != ret, error);
331 ret = iotcon_observers_create(&resource_info->conn_data.iotcon_data.observers);
332 goto_if(IOTCON_ERROR_NONE != ret, error);
334 iotcon_resource_types_destroy(resource_types);
335 iotcon_resource_interfaces_destroy(ifaces);
336 connectivity_iotcon_intialized = 1;
341 if (resource_types) iotcon_resource_types_destroy(resource_types);
342 if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
343 if (resource_info->conn_data.iotcon_data.res) iotcon_resource_destroy(resource_info->conn_data.iotcon_data.res);
344 iotcon_deinitialize();
348 static int __init_http(connectivity_resource_s *resource_info)
351 ret = web_util_noti_init();
353 connectivity_http_intialized = 1;
358 #ifdef PRINT_DEBUG_DETAIL
359 static bool __print_attributes_cb(iotcon_attributes_h attributes, const char *key, void *user_data)
361 iotcon_type_e type = IOTCON_TYPE_NONE;
363 iotcon_attributes_get_type(attributes, key, &type);
366 case IOTCON_TYPE_INT: {
368 iotcon_attributes_get_int(attributes, key, &value);
369 _D("key[%s] - int value [%d]", key, value);
372 case IOTCON_TYPE_BOOL: {
374 iotcon_attributes_get_bool(attributes, key, &value);
375 _D("key[%s] - bool value [%d]", key, value);
378 case IOTCON_TYPE_DOUBLE: {
380 iotcon_attributes_get_double(attributes, key, &value);
381 _D("key[%s] - double value [%lf]", key, value);
384 case IOTCON_TYPE_STR: {
386 iotcon_attributes_get_str(attributes, key, &value);
387 _D("key[%s] - string value [%s]", key, value);
390 case IOTCON_TYPE_NONE:
391 case IOTCON_TYPE_BYTE_STR:
392 case IOTCON_TYPE_NULL:
393 case IOTCON_TYPE_LIST:
394 case IOTCON_TYPE_ATTRIBUTES:
396 _W("unhandled key[%s] type[%d]", key, type);
400 return IOTCON_FUNC_CONTINUE;
404 static void __print_attribute(iotcon_attributes_h attributes)
406 #ifdef PRINT_DEBUG_DETAIL
409 iotcon_attributes_foreach(attributes, __print_attributes_cb, NULL);
414 static void _destroy_representation(iotcon_representation_h representation)
416 ret_if(!representation);
417 iotcon_representation_destroy(representation);
421 static iotcon_representation_h _create_representation_with_bool(connectivity_resource_s *resource_info, const char *key, bool value)
423 iotcon_attributes_h attributes = NULL;
424 iotcon_representation_h representation = NULL;
425 char *uri_path = NULL;
428 ret = iotcon_resource_get_uri_path(resource_info->conn_data.iotcon_data.res, &uri_path);
429 retv_if(IOTCON_ERROR_NONE != ret, NULL);
431 ret = iotcon_representation_create(&representation);
432 retv_if(IOTCON_ERROR_NONE != ret, NULL);
434 ret = iotcon_attributes_create(&attributes);
435 goto_if(IOTCON_ERROR_NONE != ret, error);
437 ret = iotcon_representation_set_uri_path(representation, uri_path);
438 goto_if(IOTCON_ERROR_NONE != ret, error);
440 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
441 goto_if(IOTCON_ERROR_NONE != ret, error);
443 ret = iotcon_attributes_add_bool(attributes, key, value);
444 goto_if(IOTCON_ERROR_NONE != ret, error);
446 ret = iotcon_representation_set_attributes(representation, attributes);
447 goto_if(IOTCON_ERROR_NONE != ret, error);
449 iotcon_attributes_destroy(attributes);
451 return representation;
454 if (attributes) iotcon_attributes_destroy(attributes);
455 if (representation) iotcon_representation_destroy(representation);
460 static iotcon_representation_h _create_representation_with_int(connectivity_resource_s *resource_info, const char *key, int value)
462 iotcon_attributes_h attributes = NULL;
463 iotcon_representation_h representation = NULL;
464 char *uri_path = NULL;
467 ret = iotcon_resource_get_uri_path(resource_info->conn_data.iotcon_data.res, &uri_path);
468 retv_if(IOTCON_ERROR_NONE != ret, NULL);
470 ret = iotcon_representation_create(&representation);
471 retv_if(IOTCON_ERROR_NONE != ret, NULL);
473 ret = iotcon_attributes_create(&attributes);
474 goto_if(IOTCON_ERROR_NONE != ret, error);
476 ret = iotcon_representation_set_uri_path(representation, uri_path);
477 goto_if(IOTCON_ERROR_NONE != ret, error);
479 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
480 goto_if(IOTCON_ERROR_NONE != ret, error);
482 ret = iotcon_attributes_add_int(attributes, key, value);
483 goto_if(IOTCON_ERROR_NONE != ret, error);
485 ret = iotcon_representation_set_attributes(representation, attributes);
486 goto_if(IOTCON_ERROR_NONE != ret, error);
488 iotcon_attributes_destroy(attributes);
490 return representation;
493 if (attributes) iotcon_attributes_destroy(attributes);
494 if (representation) iotcon_representation_destroy(representation);
499 static iotcon_representation_h _create_representation_with_double(connectivity_resource_s *resource_info, const char *key, double value)
501 iotcon_attributes_h attributes = NULL;
502 iotcon_representation_h representation = NULL;
503 char *uri_path = NULL;
506 ret = iotcon_resource_get_uri_path(resource_info->conn_data.iotcon_data.res, &uri_path);
507 retv_if(IOTCON_ERROR_NONE != ret, NULL);
509 ret = iotcon_representation_create(&representation);
510 retv_if(IOTCON_ERROR_NONE != ret, NULL);
512 ret = iotcon_attributes_create(&attributes);
513 goto_if(IOTCON_ERROR_NONE != ret, error);
515 ret = iotcon_representation_set_uri_path(representation, uri_path);
516 goto_if(IOTCON_ERROR_NONE != ret, error);
518 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
519 goto_if(IOTCON_ERROR_NONE != ret, error);
521 ret = iotcon_attributes_add_double(attributes, key, value);
522 goto_if(IOTCON_ERROR_NONE != ret, error);
524 ret = iotcon_representation_set_attributes(representation, attributes);
525 goto_if(IOTCON_ERROR_NONE != ret, error);
527 iotcon_attributes_destroy(attributes);
529 return representation;
532 if (attributes) iotcon_attributes_destroy(attributes);
533 if (representation) iotcon_representation_destroy(representation);
538 static iotcon_representation_h _create_representation_with_string(connectivity_resource_s *resource_info, const char *key, const char *value)
540 iotcon_attributes_h attributes = NULL;
541 iotcon_representation_h representation = NULL;
542 char *uri_path = NULL;
545 ret = iotcon_resource_get_uri_path(resource_info->conn_data.iotcon_data.res, &uri_path);
546 retv_if(IOTCON_ERROR_NONE != ret, NULL);
548 ret = iotcon_representation_create(&representation);
549 retv_if(IOTCON_ERROR_NONE != ret, NULL);
551 ret = iotcon_attributes_create(&attributes);
552 goto_if(IOTCON_ERROR_NONE != ret, error);
554 ret = iotcon_representation_set_uri_path(representation, uri_path);
555 goto_if(IOTCON_ERROR_NONE != ret, error);
557 ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
558 goto_if(IOTCON_ERROR_NONE != ret, error);
560 ret = iotcon_attributes_add_str(attributes, key, (char *)value);
561 goto_if(IOTCON_ERROR_NONE != ret, error);
563 ret = iotcon_representation_set_attributes(representation, attributes);
564 goto_if(IOTCON_ERROR_NONE != ret, error);
566 iotcon_attributes_destroy(attributes);
568 return representation;
571 if (attributes) iotcon_attributes_destroy(attributes);
572 if (representation) iotcon_representation_destroy(representation);
577 static inline void __noti_by_http(void)
579 char *json_data = NULL;
581 json_data = web_util_get_json_string();
583 const char *url = NULL;
584 controller_util_get_address(&url);
586 web_util_noti_post(url, json_data);
588 _E("fail to get url");
591 _E("fail to get json_data");
596 int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value)
600 retv_if(!resource_info, -1);
603 _D("Notify key[%s], value[%d]", key, value);
605 switch (resource_info->protocol_type) {
606 case CONNECTIVITY_PROTOCOL_IOTIVITY:
607 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
608 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
610 iotcon_representation_h representation;
611 representation = _create_representation_with_bool(resource_info, key, value);
612 retv_if(!representation, -1);
613 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
614 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
615 if (IOTCON_ERROR_NONE != ret) {
616 _W("There are some troubles for notifying value[%d]", ret);
617 _print_iotcon_error(ret);
620 _destroy_representation(representation);
623 case CONNECTIVITY_PROTOCOL_HTTP:
624 ret = web_util_json_init();
627 ret = web_util_json_begin();
630 web_util_json_add_string("SensorPiID", resource_info->path);
631 web_util_json_add_string("SensorPiType", resource_info->type);
632 web_util_json_add_boolean(key, value);
637 web_util_json_fini();
640 _E("Unknown protocol type[%d]", resource_info->protocol_type);
648 int connectivity_notify_int(connectivity_resource_s *resource_info, const char *key, int value)
652 retv_if(!resource_info, -1);
655 _D("Notify key[%s], value[%d]", key, value);
657 switch (resource_info->protocol_type) {
658 case CONNECTIVITY_PROTOCOL_IOTIVITY:
659 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
660 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
662 iotcon_representation_h representation;
663 representation = _create_representation_with_int(resource_info, key, value);
664 retv_if(!representation, -1);
665 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
666 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
667 if (IOTCON_ERROR_NONE != ret) {
668 _W("There are some troubles for notifying value[%d]", ret);
669 _print_iotcon_error(ret);
672 _destroy_representation(representation);
675 case CONNECTIVITY_PROTOCOL_HTTP:
676 ret = web_util_json_init();
679 ret = web_util_json_begin();
682 web_util_json_add_string("SensorPiID", resource_info->path);
683 web_util_json_add_string("SensorPiType", resource_info->type);
684 web_util_json_add_int(key, value);
689 web_util_json_fini();
692 _E("Unknown protocol type[%d]", resource_info->protocol_type);
699 int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value)
703 retv_if(!resource_info, -1);
706 _D("Notify key[%s], value[%lf]", key, value);
708 switch (resource_info->protocol_type) {
709 case CONNECTIVITY_PROTOCOL_IOTIVITY:
710 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
711 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
713 iotcon_representation_h representation;
714 representation = _create_representation_with_double(resource_info, key, value);
715 retv_if(!representation, -1);
716 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
717 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
718 if (IOTCON_ERROR_NONE != ret) {
719 _W("There are some troubles for notifying value[%d]", ret);
720 _print_iotcon_error(ret);
723 _destroy_representation(representation);
726 case CONNECTIVITY_PROTOCOL_HTTP:
727 ret = web_util_json_init();
730 ret = web_util_json_begin();
733 web_util_json_add_string("SensorPiID", resource_info->path);
734 web_util_json_add_string("SensorPiType", resource_info->type);
735 web_util_json_add_double(key, value);
740 web_util_json_fini();
743 _E("Unknown protocol type[%d]", resource_info->protocol_type);
750 int connectivity_notify_string(connectivity_resource_s *resource_info, const char *key, const char *value)
754 retv_if(!resource_info, -1);
758 _D("Notify key[%s], value[%s]", key, value);
760 switch (resource_info->protocol_type) {
761 case CONNECTIVITY_PROTOCOL_IOTIVITY:
762 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
763 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
765 iotcon_representation_h representation;
766 representation = _create_representation_with_string(resource_info, key, value);
767 retv_if(!representation, -1);
768 ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res,
769 representation, resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
770 if (IOTCON_ERROR_NONE != ret) {
771 _W("There are some troubles for notifying value[%d]", ret);
772 _print_iotcon_error(ret);
775 _destroy_representation(representation);
778 case CONNECTIVITY_PROTOCOL_HTTP:
779 ret = web_util_json_init();
782 ret = web_util_json_begin();
785 web_util_json_add_string("SensorPiID", resource_info->path);
786 web_util_json_add_string("SensorPiType", resource_info->type);
787 web_util_json_add_string(key, value);
792 web_util_json_fini();
795 _E("Unknown protocol type[%d]", resource_info->protocol_type);
802 static void __hash_key_free(gpointer data)
808 static void __hash_value_free(gpointer data)
810 conn_data_value_s *value = data;
811 if (value && (value->type == DATA_VAL_TYPE_STRING))
818 static int __add_value_to_hash(connectivity_resource_s *resource_info, const char *key, conn_data_value_s *value)
820 retv_if(!resource_info, -1);
824 if (!resource_info->value_hash) {
825 resource_info->value_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
826 __hash_key_free, __hash_value_free);
827 retv_if(!resource_info->value_hash, -1);
830 g_hash_table_insert(resource_info->value_hash, strdup(key), value);
835 int connectivity_attributes_add_bool(connectivity_resource_s *resource_info, const char *key, bool value)
837 conn_data_value_s *data_value = NULL;
839 retv_if(!resource_info, -1);
842 _D("adding key[%s] - value[%d]", key, value);
844 data_value = malloc(sizeof(conn_data_value_s));
845 retv_if(!data_value, -1);
847 data_value->type = DATA_VAL_TYPE_BOOL;
848 data_value->b_val = value;
850 return __add_value_to_hash(resource_info, key, data_value);
853 int connectivity_attributes_add_int(connectivity_resource_s *resource_info, const char *key, int value)
855 conn_data_value_s *data_value = NULL;
857 retv_if(!resource_info, -1);
860 _D("adding key[%s] - value[%d]", key, value);
862 data_value = malloc(sizeof(conn_data_value_s));
863 retv_if(!data_value, -1);
865 data_value->type = DATA_VAL_TYPE_INT;
866 data_value->i_val = value;
868 return __add_value_to_hash(resource_info, key, data_value);
871 int connectivity_attributes_add_double(connectivity_resource_s *resource_info, const char *key, double value)
873 conn_data_value_s *data_value = NULL;
875 retv_if(!resource_info, -1);
878 _D("adding key[%s] - value[%lf]", key, value);
880 data_value = malloc(sizeof(conn_data_value_s));
881 retv_if(!data_value, -1);
883 data_value->type = DATA_VAL_TYPE_DOUBLE;
884 data_value->d_val = value;
886 return __add_value_to_hash(resource_info, key, data_value);
889 int connectivity_attributes_add_string(connectivity_resource_s *resource_info, const char *key, const char *value)
891 conn_data_value_s *data_value = NULL;
893 retv_if(!resource_info, -1);
897 _D("adding key[%s] - value[%s]", key, value);
899 data_value = malloc(sizeof(conn_data_value_s));
900 retv_if(!data_value, -1);
902 data_value->type = DATA_VAL_TYPE_STRING;
903 data_value->s_val = strdup(value);
905 return __add_value_to_hash(resource_info, key, data_value);
908 int connectivity_attributes_remove_value_by_key(connectivity_resource_s *resource_info, const char *key)
910 retv_if(!resource_info, -1);
913 if (resource_info->value_hash)
914 g_hash_table_remove(resource_info->value_hash, key);
916 if (g_hash_table_size(resource_info->value_hash) == 0) {
917 g_hash_table_unref(resource_info->value_hash);
918 resource_info->value_hash = NULL;
924 int connectivity_attributes_remove_all(connectivity_resource_s *resource_info)
926 retv_if(!resource_info, -1);
928 if (resource_info->value_hash) {
929 g_hash_table_destroy(resource_info->value_hash);
930 resource_info->value_hash = NULL;
936 static void __json_add_data_iter_cb(gpointer key, gpointer value, gpointer user_data)
939 conn_data_value_s *data = value;
945 switch (data->type) {
946 case DATA_VAL_TYPE_BOOL:
947 ret = web_util_json_add_boolean(name, data->b_val);
948 if (IOTCON_ERROR_NONE != ret)
949 _E("failed to add key[%s], value[%d]", name, data->b_val);
951 case DATA_VAL_TYPE_INT:
952 ret = web_util_json_add_int(name, data->i_val);
953 if (IOTCON_ERROR_NONE != ret)
954 _E("failed to add key[%s], value[%d]", name, data->i_val);
956 case DATA_VAL_TYPE_DOUBLE:
957 ret = web_util_json_add_double(name, data->d_val);
958 if (IOTCON_ERROR_NONE != ret)
959 _E("failed to add key[%s], value[%lf]", name, data->d_val);
961 case DATA_VAL_TYPE_STRING:
962 ret = web_util_json_add_string(name, data->s_val);
963 if (IOTCON_ERROR_NONE != ret)
964 _E("failed to add key[%s], value[%s]", name, data->s_val);
967 _E("Unknown data type [%d]", data->type);
974 static void __attr_add_data_iter_cb(gpointer key, gpointer value, gpointer user_data)
977 conn_data_value_s *data = value;
978 iotcon_attributes_h attr = user_data;
985 switch (data->type) {
986 case DATA_VAL_TYPE_BOOL:
987 ret = iotcon_attributes_add_bool(attr, name, data->b_val);
988 if (IOTCON_ERROR_NONE != ret)
989 _E("failed to add key[%s], value[%d]", name, data->b_val);
991 case DATA_VAL_TYPE_INT:
992 ret = iotcon_attributes_add_int(attr, name, data->i_val);
993 if (IOTCON_ERROR_NONE != ret)
994 _E("failed to add key[%s], value[%d]", name, data->i_val);
996 case DATA_VAL_TYPE_DOUBLE:
997 ret = iotcon_attributes_add_double(attr, name, data->d_val);
998 if (IOTCON_ERROR_NONE != ret)
999 _E("failed to add key[%s], value[%lf]", name, data->d_val);
1001 case DATA_VAL_TYPE_STRING:
1002 ret = iotcon_attributes_add_str(attr, name, data->s_val);
1003 if (IOTCON_ERROR_NONE != ret)
1004 _E("failed to add key[%s], value[%s]", name, data->s_val);
1007 _E("Unknown data type [%d]", data->type);
1014 static int __create_attributes(connectivity_resource_s *resource_info, iotcon_attributes_h *attributes)
1017 iotcon_attributes_h attr = NULL;
1019 ret = iotcon_attributes_create(&attr);
1020 if (IOTCON_ERROR_NONE != ret) {
1021 _print_iotcon_error(ret);
1025 ret = iotcon_attributes_add_str(attr, PATH, resource_info->path);
1026 if (IOTCON_ERROR_NONE != ret) {
1027 _print_iotcon_error(ret);
1028 iotcon_attributes_destroy(attr);
1031 if (resource_info->value_hash)
1032 g_hash_table_foreach(resource_info->value_hash, __attr_add_data_iter_cb, attr);
1039 int connectivity_attributes_notify_all(connectivity_resource_s *resource_info)
1043 retv_if(!resource_info, -1);
1045 if (resource_info->value_hash == NULL) {
1046 _W("You have nothing to notify now");
1051 switch (resource_info->protocol_type) {
1052 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1053 retv_if(!resource_info->conn_data.iotcon_data.res, -1);
1054 retv_if(!resource_info->conn_data.iotcon_data.observers, -1);
1056 char *uri_path = NULL;
1057 iotcon_representation_h representation = NULL;
1058 iotcon_attributes_h attributes = NULL;
1061 iotcon_ret = iotcon_resource_get_uri_path(resource_info->conn_data.iotcon_data.res, &uri_path);
1062 retv_if(IOTCON_ERROR_NONE != iotcon_ret, -1);
1064 iotcon_ret = iotcon_representation_create(&representation);
1065 retv_if(IOTCON_ERROR_NONE != iotcon_ret, -1);
1067 iotcon_ret = iotcon_representation_set_uri_path(representation, uri_path);
1068 if (IOTCON_ERROR_NONE != iotcon_ret) {
1069 _print_iotcon_error(iotcon_ret);
1073 iotcon_ret = __create_attributes(resource_info, &attributes);
1075 _E("failed to create attributes");
1078 __print_attribute(attributes);
1080 iotcon_ret = iotcon_representation_set_attributes(representation, attributes);
1081 if (IOTCON_ERROR_NONE != iotcon_ret) {
1082 _print_iotcon_error(iotcon_ret);
1086 iotcon_ret = iotcon_resource_notify(resource_info->conn_data.iotcon_data.res, representation,
1087 resource_info->conn_data.iotcon_data.observers, IOTCON_QOS_LOW);
1088 if (IOTCON_ERROR_NONE != iotcon_ret) {
1089 _print_iotcon_error(iotcon_ret);
1093 iotcon_representation_destroy(representation);
1094 iotcon_attributes_destroy(attributes);
1097 case CONNECTIVITY_PROTOCOL_HTTP:
1098 ret = web_util_json_init();
1101 ret = web_util_json_begin();
1104 web_util_json_add_string("SensorPiID", resource_info->path);
1105 web_util_json_add_string("SensorPiType", resource_info->type);
1106 g_hash_table_foreach(resource_info->value_hash, __json_add_data_iter_cb, NULL);
1107 web_util_json_end();
1111 web_util_json_fini();
1117 g_hash_table_destroy(resource_info->value_hash);
1118 resource_info->value_hash = NULL;
1123 void connectivity_unset_resource(connectivity_resource_s *resource_info)
1125 ret_if(!resource_info);
1127 switch (resource_info->protocol_type) {
1128 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1129 if (resource_info->conn_data.iotcon_data.observers) iotcon_observers_destroy(resource_info->conn_data.iotcon_data.observers);
1130 if (resource_info->conn_data.iotcon_data.res) iotcon_resource_destroy(resource_info->conn_data.iotcon_data.res);
1131 iotcon_deinitialize();
1132 connectivity_iotcon_intialized = 0;
1134 case CONNECTIVITY_PROTOCOL_HTTP:
1135 web_util_noti_fini();
1136 connectivity_http_intialized = 0;
1142 if (resource_info->path) free(resource_info->path);
1143 if (resource_info->type) free(resource_info->type);
1144 free(resource_info);
1149 int connectivity_set_resource(const char *path, const char *type, connectivity_resource_s **out_resource_info)
1151 connectivity_resource_s *resource_info = NULL;
1156 retv_if(!out_resource_info, -1);
1158 resource_info = calloc(1, sizeof(connectivity_resource_s));
1159 retv_if(!resource_info, -1);
1161 resource_info->path = strdup(path);
1162 goto_if(!resource_info->path, error);
1164 resource_info->type = strdup(type);
1165 goto_if(!resource_info->type, error);
1167 resource_info->protocol_type = ProtocolType;
1169 _D("Path[%s], Type[%s], protocol_type[%d]" , resource_info->path, resource_info->type, resource_info->protocol_type);
1171 switch (resource_info->protocol_type) {
1172 case CONNECTIVITY_PROTOCOL_DEFAULT:
1173 _D("default protocol type is iotivity\n \
1174 To set connectivity use connectivity_set_protocol_type() function");
1175 resource_info->protocol_type = CONNECTIVITY_PROTOCOL_IOTIVITY;
1176 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1177 ret = __init_iotcon(resource_info);
1178 goto_if(ret, error);
1180 case CONNECTIVITY_PROTOCOL_HTTP:
1181 ret = __init_http(resource_info);
1182 goto_if(ret, error);
1189 *out_resource_info = resource_info;
1194 if (resource_info->path) free(resource_info->path);
1195 if (resource_info->type) free(resource_info->type);
1196 if (resource_info) free(resource_info);
1201 int connectivity_set_protocol(connectivity_protocol_e protocol_type)
1204 retv_if(protocol_type >= CONNECTIVITY_PROTOCOL_MAX, -1);
1206 switch (protocol_type) {
1207 case CONNECTIVITY_PROTOCOL_DEFAULT:
1208 case CONNECTIVITY_PROTOCOL_IOTIVITY:
1209 if (connectivity_iotcon_intialized) {
1210 _E("protocol type[%d] already initialized", protocol_type);
1213 ProtocolType = CONNECTIVITY_PROTOCOL_IOTIVITY;
1215 case CONNECTIVITY_PROTOCOL_HTTP:
1216 if (connectivity_http_intialized) {
1217 _E("protocol type[%d] already initialized", protocol_type);
1220 ProtocolType = CONNECTIVITY_PROTOCOL_HTTP;
1223 _E("unknown protocol type[%d]", protocol_type);