*/
#include <stdlib.h>
+#include <time.h>
#include <glib.h>
#include <tizen.h>
#define CBOR_FILE "/home/owner/apps_rw/org.tizen.position-finder-client/data/iotcon-test-svr-db-client.dat"
#define DOOR_RESOURCE_URI_PREFIX "/door"
+#define MAXIMUM_LIVE_INTERVAL 10l
struct _connectivity_observe_resource_cb_s {
connectivity_observe_resource_cb cb;
char *type;
char *uri_path;
iotcon_remote_resource_h resource;
+ time_t last_time;
connectivity_observe_resource_cb_s *cb_info;
};
ret = iotcon_attributes_get_bool(attributes, "opened", &opened);
ret_if(IOTCON_ERROR_NONE != ret);
+ resource_info->last_time = time(NULL);
resource_info->cb_info->cb(resource_info, (void *)(int) opened, resource_info->cb_info->user_data);
}
{
Eina_List *l = NULL, *ln = NULL;
connectivity_resource_s *temp = NULL;
- connectivity_resource_s *info = NULL;
retv_if(!device_id, NULL);
continue_if(!temp->device_id);
if (!strncmp(temp->device_id, device_id, strlen(device_id))) {
connectivity_info.list = eina_list_remove(connectivity_info.list, temp);
- info = temp;
- break;
+ return temp;
}
}
- return info;
+ return NULL;
}
#endif
return IOTCON_FUNC_CONTINUE;
}
-static int _retrieve_resource_info(iotcon_remote_resource_h resource, connectivity_resource_s *info)
+static int _retrieve_resource_info(iotcon_remote_resource_h resource, connectivity_resource_s **info)
{
int ret = -1;
char *device_id = NULL;
ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb, uri_path);
retv_if(IOTCON_ERROR_NONE != ret, -1);
- ret = iotcon_remote_resource_clone(resource, &info->resource);
- retv_if(IOTCON_ERROR_NONE != ret, -1);
+ *info = calloc(1, sizeof(connectivity_resource_s));
+ retv_if(!*info, -1);
+
+ ret = iotcon_remote_resource_clone(resource, &(*info)->resource);
+ goto_if(IOTCON_ERROR_NONE != ret, error);
_I("Resource Addr[%s/%s] is in Device[%s]", host_address, uri_path, device_name);
- info->device_id = strdup(device_id);
- goto_if(!info->device_id, error);
+ (*info)->device_id = strdup(device_id);
+ goto_if(!(*info)->device_id, error);
- info->host_address = strdup(host_address);
- goto_if(!info->host_address, error);
+ (*info)->host_address = strdup(host_address);
+ goto_if(!(*info)->host_address, error);
- info->device_name = strdup(device_name);
- goto_if(!info->device_name, error);
+ (*info)->device_name = strdup(device_name);
+ goto_if(!(*info)->device_name, error);
- info->uri_path = strdup(uri_path);
- goto_if(!info->uri_path, error);
+ (*info)->uri_path = strdup(uri_path);
+ goto_if(!(*info)->uri_path, error);
return 0;
error:
- if (info->resource) {
- iotcon_remote_resource_destroy(info->resource);
- info->resource = NULL;
- }
- if (info->uri_path) {
- free(info->uri_path);
- info->uri_path = NULL;
- }
- if (info->device_name) {
- free(info->device_name);
- info->device_name = NULL;
- }
- if (info->host_address) {
- free(info->host_address);
- info->host_address = NULL;
- }
- if (info->device_id) {
- free(info->device_id);
- info->device_id = NULL;
+ if (*info) {
+ if ((*info)->resource) {
+ iotcon_remote_resource_destroy((*info)->resource);
+ (*info)->resource = NULL;
+ }
+ if ((*info)->uri_path) {
+ free((*info)->uri_path);
+ (*info)->uri_path = NULL;
+ }
+ if ((*info)->device_name) {
+ free((*info)->device_name);
+ (*info)->device_name = NULL;
+ }
+ if ((*info)->host_address) {
+ free((*info)->host_address);
+ (*info)->host_address = NULL;
+ }
+ if ((*info)->device_id) {
+ free((*info)->device_id);
+ (*info)->device_id = NULL;
+ }
+ free(*info);
}
return -1;
if (info->resource)
iotcon_remote_resource_destroy(info->resource);
+ if (info->cb_info)
+ free(info->cb_info);
+
if (info->uri_path)
free(info->uri_path);
return 0;
}
-#if 0
static void _unregister_observe(connectivity_resource_s *info)
{
ret_if(!info);
iotcon_remote_resource_observe_deregister(info->resource);
}
-#endif
static bool _found_resource_cb(iotcon_remote_resource_h resource, iotcon_error_e result, void *user_data)
{
retv_if(-1 == ret, IOTCON_FUNC_CONTINUE);
if (1 == ret) return IOTCON_FUNC_CONTINUE;
- info = calloc(1, sizeof(connectivity_resource_s));
- retv_if(!info, -1);
- info->cb_info = user_data;
-
- ret = _retrieve_resource_info(resource, info);
+ ret = _retrieve_resource_info(resource, &info);
goto_if(-1 == ret, error);
+ info->cb_info = user_data;
if (-1 == _register_observe(info)) {
_E("Cannot observe [%s:%s:%s:%s]", info->device_id, info->host_address, info->device_name, info->uri_path);
}
return -1;
}
+
+void connectivity_free_abnormal_resource(void)
+{
+ Eina_List *l = NULL, *ln = NULL;
+ connectivity_resource_s *temp = NULL;
+ time_t cur_time = time(NULL);
+
+ EINA_LIST_FOREACH_SAFE(connectivity_info.list, l, ln, temp) {
+ if (cur_time > temp->last_time + MAXIMUM_LIVE_INTERVAL) {
+ _E("Lost signal [%s]", temp->device_id);
+ connectivity_info.list = eina_list_remove(connectivity_info.list, temp);
+ _unregister_observe(temp);
+ _free_resource_info(temp);
+ }
+ }
+}
#include <service_app.h>
#include <iotcon.h>
#include <Eina.h>
+#include <Ecore.h>
#include "log.h"
#include "connectivity.h"
#define MAX_QUEUE_ELEMENTS 120
#define DOOR_RESOURCE_TYPE "org.tizen.door"
+#define FINDING_SERVER_INTERVAL 5.0f
static void _start_internal_function(void);
static void _stop_internal_function(void);
_I("Result value is [%d]\n", result);
}
+static Eina_Bool _timer_cb(void *data)
+{
+ int ret = -1;
+
+ connectivity_free_abnormal_resource();
+ ret = connectivity_observe_resource(DOOR_RESOURCE_TYPE, _observe_resource_cb, NULL);
+ retv_if(ret == -1, ECORE_CALLBACK_RENEW);
+
+ return ECORE_CALLBACK_RENEW;
+}
+
bool service_app_create(void *data)
{
+ Ecore_Timer *timer = NULL;
int ret = -1;
_start_internal_function();
ret = connectivity_observe_resource(DOOR_RESOURCE_TYPE, _observe_resource_cb, NULL);
retv_if(ret == -1, false);
+ timer = ecore_timer_add(FINDING_SERVER_INTERVAL, _timer_cb, NULL);
+ retv_if(!timer, false);
+
return true;
}