Challenge : Create server requried in Question#3
[apps/native/position-finder-server.git] / src / connectivity.c
index 1c33e5e..09b9c30 100644 (file)
@@ -29,7 +29,7 @@
 #include "log.h"
 #include "connectivity.h"
 
-#define ULTRASONIC_RESOURCE_TYPE "org.tizen.door"
+#define DEVICE_NAME "Dr.Evil's device"
 #define BUFSIZE 1024
 #define URI_PATH_LEN 64
 #define URI_PATH "/door/1"
@@ -186,21 +186,60 @@ error:
        return NULL;
 }
 
+static iotcon_representation_h _create_representation_with_str(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;
        }
 }
 
@@ -279,6 +318,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 all members [%s]", value);
+
+       representation = _create_representation_with_str(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 +470,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;
                }
@@ -430,7 +494,7 @@ int connectivity_init(void)
        ret = iotcon_initialize(CBOR_FILE_IN_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;
@@ -455,28 +519,6 @@ void connectivity_unset_resource(connectivity_resource_s *resource_info)
        free(resource_info);
 }
 
-static int _get_default_path_in_conf(char *buf, int size)
-{
-       FILE *in = NULL;
-       size_t nread = 0;
-
-       in = fopen(CONF_FILE, "r");
-       retv_if(!in, -1);
-
-       nread = fread(buf, 1, size, in);
-       if (nread <= 0) {
-               _I("No contents in the conf.");
-               return -1;
-       }
-
-       if (buf[nread - 1] == '\n')
-               buf[nread - 1] = '\0';
-
-       fclose(in);
-
-       return 0;
-}
-
 int connectivity_set_resource(const char *path, const char *type, connectivity_resource_s **out_resource_info)
 {
        iotcon_resource_types_h resource_types = NULL;
@@ -484,18 +526,15 @@ int connectivity_set_resource(const char *path, const char *type, connectivity_r
        connectivity_resource_s *resource_info = NULL;
        uint8_t policies;
        int ret = -1;
-       char default_path[URI_PATH_LEN] = { 0, };
+
+       retv_if(!path, -1);
+       retv_if(!type, -1);
+       retv_if(!out_resource_info, -1);
 
        resource_info = calloc(1, sizeof(connectivity_resource_s));
        retv_if(!resource_info, -1);
 
-       if (path) {
-               resource_info->path = strdup(path);
-       } else {
-               ret = _get_default_path_in_conf(default_path, URI_PATH_LEN);
-               retv_if(ret < 0, -1);
-               resource_info->path = strdup(default_path);
-       }
+       resource_info->path = strdup(path);
        goto_if(!resource_info->path, error);
 
        _D("Path : [%s]", resource_info->path);