* limitations under the License.
*/
+#include <stdio.h>
#include <stdlib.h>
#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_1_URI "/door/1"
-#define ULTRASONIC_RESOURCE_2_URI "/door/2"
-#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);
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(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_bool(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 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);
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_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);
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_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);
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_double(attributes, key, value);
goto_if(IOTCON_ERROR_NONE != ret, error);
ret = iotcon_representation_set_attributes(representation, attributes);
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_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(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);
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_str(attributes, key, value);
goto_if(IOTCON_ERROR_NONE != ret, error);
ret = iotcon_representation_set_attributes(representation, attributes);
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;
_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);
_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);
_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);
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);
}
_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);
+ ret = iotcon_set_device_name(DEVICE_NAME);
goto_if(IOTCON_ERROR_NONE != ret, error);
return 0;
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;
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);
IOTCON_RESOURCE_OBSERVABLE |
IOTCON_RESOURCE_SECURE;
- ret = iotcon_resource_create(uri_path,
+ ret = iotcon_resource_create(URI_PATH,
resource_types,
ifaces,
policies,
iotcon_resource_types_destroy(resource_types);
iotcon_resource_interfaces_destroy(ifaces);
+ *out_resource_info = resource_info;
return 0;
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;