Challenge : Create server requried in Question#3
[apps/native/position-finder-server.git] / src / connectivity.c
index 7a4ae94..09b9c30 100644 (file)
 #include <stdbool.h>
 #include <glib.h>
 #include <Eina.h>
-
-#include <iotcon.h>
+#include <app_common.h>
 
 #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"
+#define PATH "path"
 
 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data);
 
@@ -67,14 +69,14 @@ static void _destroy_representation(iotcon_representation_h representation)
        iotcon_representation_destroy(representation);
 }
 
-static iotcon_representation_h _create_representation_with_bool(iotcon_resource_h res, const char *key, bool value)
+static iotcon_representation_h _create_representation_with_bool(connectivity_resource_s *resource_info, const char *key, bool value)
 {
        iotcon_attributes_h attributes = NULL;
        iotcon_representation_h representation = NULL;
        char *uri_path = NULL;
        int ret = -1;
 
-       ret = iotcon_resource_get_uri_path(res, &uri_path);
+       ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
        retv_if(IOTCON_ERROR_NONE != ret, NULL);
 
        ret = iotcon_representation_create(&representation);
@@ -86,6 +88,9 @@ static iotcon_representation_h _create_representation_with_bool(iotcon_resource_
        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_bool(attributes, key, value);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
@@ -103,14 +108,14 @@ error:
        return NULL;
 }
 
-static iotcon_representation_h _create_representation_with_int(iotcon_resource_h res, const char *key, int value)
+static iotcon_representation_h _create_representation_with_int(connectivity_resource_s *resource_info, const char *key, int value)
 {
        iotcon_attributes_h attributes = NULL;
        iotcon_representation_h representation = NULL;
        char *uri_path = NULL;
        int ret = -1;
 
-       ret = iotcon_resource_get_uri_path(res, &uri_path);
+       ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
        retv_if(IOTCON_ERROR_NONE != ret, NULL);
 
        ret = iotcon_representation_create(&representation);
@@ -122,7 +127,10 @@ static iotcon_representation_h _create_representation_with_int(iotcon_resource_h
        ret = iotcon_representation_set_uri_path(representation, uri_path);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
-       ret = iotcon_attributes_add_int(attributes, "opened", value);
+       ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_attributes_add_int(attributes, key, value);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
        ret = iotcon_representation_set_attributes(representation, attributes);
@@ -139,14 +147,14 @@ error:
        return NULL;
 }
 
-static iotcon_representation_h _create_representation_with_double(iotcon_resource_h res, const char *key, double value)
+static iotcon_representation_h _create_representation_with_double(connectivity_resource_s *resource_info, const char *key, double value)
 {
        iotcon_attributes_h attributes = NULL;
        iotcon_representation_h representation = NULL;
        char *uri_path = NULL;
        int ret = -1;
 
-       ret = iotcon_resource_get_uri_path(res, &uri_path);
+       ret = iotcon_resource_get_uri_path(resource_info->res, &uri_path);
        retv_if(IOTCON_ERROR_NONE != ret, NULL);
 
        ret = iotcon_representation_create(&representation);
@@ -158,7 +166,10 @@ static iotcon_representation_h _create_representation_with_double(iotcon_resourc
        ret = iotcon_representation_set_uri_path(representation, uri_path);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
-       ret = iotcon_attributes_add_double(attributes, "opened", value);
+       ret = iotcon_attributes_add_str(attributes, PATH, resource_info->path);
+       goto_if(IOTCON_ERROR_NONE != ret, error);
+
+       ret = iotcon_attributes_add_double(attributes, key, value);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
        ret = iotcon_representation_set_attributes(representation, attributes);
@@ -175,6 +186,63 @@ 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;
+       }
+}
+
 int connectivity_notify_bool(connectivity_resource_s *resource_info, const char *key, bool value)
 {
        iotcon_representation_h representation;
@@ -185,11 +253,15 @@ int connectivity_notify_bool(connectivity_resource_s *resource_info, const char
 
        _D("Notify the value[%d]", value);
 
-       representation = _create_representation_with_bool(resource_info->res, key, value);
+       representation = _create_representation_with_bool(resource_info, key, value);
        retv_if(!representation, -1);
 
-       ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
-       retv_if(IOTCON_ERROR_NONE != ret, -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);
 
@@ -206,11 +278,15 @@ int connectivity_notify_int(connectivity_resource_s *resource_info, const char *
 
        _D("Notify the value[%d]", value);
 
-       representation = _create_representation_with_int(resource_info->res, key, value);
+       representation = _create_representation_with_int(resource_info, key, value);
        retv_if(!representation, -1);
 
-       ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
-       retv_if(IOTCON_ERROR_NONE != ret, -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);
 
@@ -227,11 +303,40 @@ int connectivity_notify_double(connectivity_resource_s *resource_info, const cha
 
        _D("Notify the value[%f]", value);
 
-       representation = _create_representation_with_double(resource_info->res, key, value);
+       representation = _create_representation_with_double(resource_info, key, value);
        retv_if(!representation, -1);
 
-       ret = iotcon_resource_notify(resource_info->res, representation, resource_info->observers, IOTCON_QOS_HIGH);
-       retv_if(IOTCON_ERROR_NONE != ret, -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;
+}
+
+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);
 
@@ -365,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;
                }
@@ -389,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;
@@ -410,10 +515,11 @@ void connectivity_unset_resource(connectivity_resource_s *resource_info)
        ret_if(!resource_info);
        if (resource_info->observers) iotcon_observers_destroy(resource_info->observers);
        if (resource_info->res) iotcon_resource_destroy(resource_info->res);
+       if (resource_info->path) free(resource_info->path);
        free(resource_info);
 }
 
-int connectivity_set_resource(const char *uri_path, const char *type, connectivity_resource_s **out_resource_info)
+int connectivity_set_resource(const char *path, const char *type, connectivity_resource_s **out_resource_info)
 {
        iotcon_resource_types_h resource_types = NULL;
        iotcon_resource_interfaces_h ifaces = NULL;
@@ -421,9 +527,17 @@ int connectivity_set_resource(const char *uri_path, const char *type, connectivi
        uint8_t policies;
        int ret = -1;
 
+       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);
-       *out_resource_info = resource_info;
+
+       resource_info->path = strdup(path);
+       goto_if(!resource_info->path, error);
+
+       _D("Path : [%s]", resource_info->path);
 
        ret = iotcon_resource_types_create(&resource_types);
        goto_if(IOTCON_ERROR_NONE != ret, error);
@@ -445,7 +559,7 @@ int connectivity_set_resource(const char *uri_path, const char *type, connectivi
                IOTCON_RESOURCE_OBSERVABLE |
                IOTCON_RESOURCE_SECURE;
 
-       ret = iotcon_resource_create(uri_path,
+       ret = iotcon_resource_create(URI_PATH,
                        resource_types,
                        ifaces,
                        policies,
@@ -459,6 +573,7 @@ int connectivity_set_resource(const char *uri_path, const char *type, connectivi
 
        iotcon_resource_types_destroy(resource_types);
        iotcon_resource_interfaces_destroy(ifaces);
+       *out_resource_info = resource_info;
 
        return 0;
 
@@ -466,6 +581,7 @@ error:
        if (ifaces) iotcon_resource_interfaces_destroy(ifaces);
        if (resource_types) iotcon_resource_types_destroy(resource_types);
        if (resource_info->res) iotcon_resource_destroy(resource_info->res);
+       if (resource_info->path) free(resource_info->path);
        if (resource_info) free(resource_info);
 
        return -1;