Adds APIs related to connectivity 20/157420/1
authorEunyoung Lee <ey928.lee@samsung.com>
Tue, 24 Oct 2017 11:03:30 +0000 (20:03 +0900)
committerEunyoung Lee <ey928.lee@samsung.com>
Tue, 24 Oct 2017 11:03:46 +0000 (20:03 +0900)
Change-Id: Iee9f19dbcb8759839f56b3f65b290a7e88e183bb

inc/connectivity.h
lib/libperipheralio.so [changed mode: 0644->0755]
src/connectivity.c

index b88f83a..3f9025a 100644 (file)
@@ -79,4 +79,14 @@ extern int connectivity_notify_int(connectivity_resource_s *resource_info, const
  */
 extern int connectivity_notify_double(connectivity_resource_s *resource_info, const char *key, double value);
 
+/**
+ * @brief Notifies specific clients that resource's attributes have changed with string value.
+ * @param[in] resource_info A structure containing information about connectivity resource
+ * @param[in] key A new key to be added into attributes
+ * @param[in] value A string value to be added into attributes
+ * @return 0 on success, otherwise a negative error value
+ * @see If key is already exists, current value will be replaced with new value.
+ */
+extern int connectivity_notify_string(connectivity_resource_s *resource_info, const char *key, char *value);
+
 #endif /* __POSITION_FINDER_CONNECTIVITY_H__ */
old mode 100644 (file)
new mode 100755 (executable)
index d02ca3b..618c38b
Binary files a/lib/libperipheralio.so and b/lib/libperipheralio.so differ
index 9410f3c..b2f0b78 100644 (file)
@@ -29,7 +29,7 @@
 #include "log.h"
 #include "connectivity.h"
 
-#define ULTRASONIC_RESOURCE_TYPE "org.tizen.door"
+#define DEVICE_NAME "org.tizen.device"
 #define BUFSIZE 1024
 #define URI_PATH_LEN 64
 #define URI_PATH "/door/1"
@@ -169,6 +169,9 @@ static iotcon_representation_h _create_representation_with_double(connectivity_r
        ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
+       ret = iotcon_attributes_add_str(attributes, "Hash", "0");
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
        ret = iotcon_attributes_add_double(attributes, key, value);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
@@ -186,21 +189,60 @@ error:
        return NULL;
 }
 
+static iotcon_representation_h _create_representation_with_string(connectivity_resource_s *resource_info, const char *key, char *value)
+{
+       iotcon_attributes_h attributes = NULL;
+       iotcon_representation_h representation = NULL;
+       char *uri_path = NULL;
+       int ret = -1;
+
+       ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
+       retv_if(IOTCON_ERROR_NONE != ret, NULL);
+
+       ret = iotcon_representation_create(&representation);
+       retv_if(IOTCON_ERROR_NONE != ret, NULL);
+
+       ret = iotcon_attributes_create(&attributes);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_representation_set_uri_path(representation, uri_path);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_attributes_add_str(attributes, key, value);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_representation_set_attributes(representation, attributes);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       iotcon_attributes_destroy(attributes);
+
+       return representation;
+
+error:
+       if (attributes) iotcon_attributes_destroy(attributes);
+       if (representation) iotcon_representation_destroy(representation);
+
+       return NULL;
+}
+
 static void _print_iotcon_error(int err_no)
 {
        switch (err_no) {
-               case IOTCON_ERROR_NOT_SUPPORTED:
-                       _E("IOTCON_ERROR_NOT_SUPPORTED");
-                       break;
-               case IOTCON_ERROR_PERMISSION_DENIED:
-                       _E("IOTCON_ERROR_PERMISSION_DENIED");
-                       break;
-               case IOTCON_ERROR_INVALID_PARAMETER:
-                       _E("IOTCON_ERROR_INVALID_PARAMETER");
-                       break;
-               default:
-                       _E("Error : [%d]", err_no);
-                       break;
+       case IOTCON_ERROR_NOT_SUPPORTED:
+               _E("IOTCON_ERROR_NOT_SUPPORTED");
+               break;
+       case IOTCON_ERROR_PERMISSION_DENIED:
+               _E("IOTCON_ERROR_PERMISSION_DENIED");
+               break;
+       case IOTCON_ERROR_INVALID_PARAMETER:
+               _E("IOTCON_ERROR_INVALID_PARAMETER");
+               break;
+       default:
+               _E("Error : [%d]", err_no);
+               break;
        }
 }
 
@@ -262,7 +304,7 @@ int connectivity_notify_double(connectivity_resource_s *resource_info, const cha
        retv_if(!resource_info, -1);
        retv_if(!resource_info->observers, -1);
 
-       _D("Notify the value[%f]", value);
+       _D("Notify the value [%.2lf]", value);
 
        representation = _create_representation_with_double(resource_info, key, value);
        retv_if(!representation, -1);
@@ -279,6 +321,31 @@ int connectivity_notify_double(connectivity_resource_s *resource_info, const cha
        return 0;
 }
 
+int connectivity_notify_string(connectivity_resource_s *resource_info, const char *key, char *value)
+{
+       iotcon_representation_h representation;
+       int ret = -1;
+
+       retv_if(!resource_info, -1);
+       retv_if(!resource_info->observers, -1);
+
+       _D("Notify the value [%s]", value);
+
+       representation = _create_representation_with_string(resource_info, key, value);
+       retv_if(!representation, -1);
+
+       ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_LOW);
+       if (IOTCON_ERROR_NONE != ret) {
+               _I("There are some troubles for notifying value[%d]", ret);
+               _print_iotcon_error(ret);
+               return -1;
+       }
+
+       _destroy_representation(representation);
+
+       return 0;
+}
+
 static bool _query_cb(const char *key, const char *value, void *user_data)
 {
        _D("Key : [%s], Value : [%s]", key, value);
@@ -406,7 +473,7 @@ static void _copy_file(const char *in_filename, const char *out_filename)
 
        rewind(in);
        while ((nread = fread(buf, 1, sizeof(buf), in)) > 0) {
-               if (fwrite (buf, 1, nread, out) < nread) {
+               if (fwrite(buf, 1, nread, out) < nread) {
                        _E("critical error to copy a file");
                        break;
                }
@@ -444,7 +511,7 @@ int connectivity_init(void)
        ret = iotcon_initialize(data);
        retv_if(IOTCON_ERROR_NONE != ret, -1);
 
-       ret = iotcon_set_device_name(ULTRASONIC_RESOURCE_TYPE);
+       ret = iotcon_set_device_name(DEVICE_NAME);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
        return 0;