Change peripheral io APIs
[apps/native/position-finder-server.git] / src / connectivity.c
index eb281f1..5065735 100644 (file)
@@ -19,6 +19,7 @@
  * limitations under the License.
  */
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <stdbool.h>
 #include <glib.h>
@@ -29,9 +30,9 @@
 #include "log.h"
 #include "connectivity.h"
 
-#define ULTRASONIC_RESOURCE_1_URI "/door/1"
-#define ULTRASONIC_RESOURCE_2_URI "/door/2"
 #define ULTRASONIC_RESOURCE_TYPE "org.tizen.door"
+#define BUFSIZE 1024
+#define URI_PATH_LEN 64
 
 static void _request_resource_handler(iotcon_resource_h resource, iotcon_request_h request, void *user_data);
 
@@ -86,7 +87,7 @@ 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_bool(attributes, "opened", value);
+       ret = iotcon_attributes_add_bool(attributes, key, value);
        goto_if(IOTCON_ERROR_NONE != ret, error);
 
        ret = iotcon_representation_set_attributes(representation, attributes);
@@ -302,12 +303,16 @@ static int _handle_observer(iotcon_request_h request, iotcon_observers_h observe
                ret = iotcon_request_get_observe_id(request, &observe_id);
                retv_if(IOTCON_ERROR_NONE != ret, -1);
 
+               _I("Add an observer : %d", observe_id);
+
                ret = iotcon_observers_add(observers, observe_id);
                retv_if(IOTCON_ERROR_NONE != ret, -1);
        } else if (IOTCON_OBSERVE_DEREGISTER == observe_type) {
                ret = iotcon_request_get_observe_id(request, &observe_id);
                retv_if(IOTCON_ERROR_NONE != ret, -1);
 
+               _I("Remove an observer : %d", observe_id);
+
                ret = iotcon_observers_remove(observers, observe_id);
                retv_if(IOTCON_ERROR_NONE != ret, -1);
        }
@@ -343,11 +348,46 @@ error:
        _send_response(request, NULL, IOTCON_RESPONSE_ERROR);
 }
 
+static void _copy_file(const char *in_filename, const char *out_filename)
+{
+       char buf[BUFSIZE] = { 0, };
+       size_t nread = 0;
+       FILE *in = NULL;
+       FILE *out = NULL;
+
+       ret_if(!in_filename);
+       ret_if(!out_filename);
+
+       in = fopen(in_filename, "r");
+       ret_if(!in);
+
+       out = fopen(out_filename, "w");
+       goto_if(!out, error);
+
+       rewind(in);
+       while ((nread = fread(buf, 1, sizeof(buf), in)) > 0) {
+               if (fwrite (buf, 1, nread, out) < nread) {
+                       _E("critical error to copy a file");
+                       break;
+               }
+       }
+
+       fclose(in);
+       fclose(out);
+
+       return;
+
+error:
+       fclose(out);
+}
+
 int connectivity_init(void)
 {
        int ret = -1;
 
-       ret = iotcon_initialize("/home/owner/apps_rw/org.tizen.position-finder-server/data/iotcon-test-svr-db-server.dat");
+       _copy_file(CBOR_FILE_IN_RES, CBOR_FILE_IN_DATA);
+
+       ret = iotcon_initialize(CBOR_FILE_IN_DATA);
        retv_if(IOTCON_ERROR_NONE != ret, -1);
 
        ret = iotcon_set_device_name(ULTRASONIC_RESOURCE_TYPE);
@@ -374,6 +414,28 @@ void connectivity_unset_resource(connectivity_resource_s *resource_info)
        free(resource_info);
 }
 
+static int _get_default_uri_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 *uri_path, const char *type, connectivity_resource_s **out_resource_info)
 {
        iotcon_resource_types_h resource_types = NULL;
@@ -381,6 +443,18 @@ int connectivity_set_resource(const char *uri_path, const char *type, connectivi
        connectivity_resource_s *resource_info = NULL;
        uint8_t policies;
        int ret = -1;
+       const char *final_uri_path = NULL;
+       char default_uri_path[URI_PATH_LEN] = { 0, };
+
+       if (uri_path) {
+               final_uri_path = uri_path;
+       } else {
+               ret = _get_default_uri_path_in_conf(default_uri_path, URI_PATH_LEN);
+               retv_if(ret < 0, -1);
+               final_uri_path = default_uri_path;
+       }
+
+       _D("uri path : [%s]", final_uri_path);
 
        resource_info = calloc(1, sizeof(connectivity_resource_s));
        retv_if(!resource_info, -1);
@@ -406,7 +480,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(final_uri_path,
                        resource_types,
                        ifaces,
                        policies,