summaryrefslogtreecommitdiff
path: root/src/connectivity.c
blob: 61b08939f357f0e8013c776a47782ab3cce8d5c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *
 * Contact: Jin Yoon <jinny.yoon@samsung.com>
 *          Geunsun Lee <gs86.lee@samsung.com>
 *          Eunyoung Lee <ey928.lee@samsung.com>
 *          Junkyu Han <junkyu.han@samsung.com>
 *
 * Licensed under the Flora License, Version 1.1 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://floralicense.org/license/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <glib.h>

#include <tizen.h>
#include <service_app.h>
#include <iotcon.h>
#include <Eina.h>

#include "log.h"
#include "connectivity.h"

#define DOOR_RESOURCE_URI_PREFIX "/door"
#define MAXIMUM_LIVE_INTERVAL 10l
#define BUFSIZE 1024
#define ULTRASONIC_RESOURCE_TYPE "org.tizen.door"

struct _connectivity_observe_resource_cb_s {
	connectivity_observe_resource_cb cb;
	void *user_data;
};
typedef struct _connectivity_observe_resource_cb_s connectivity_observe_resource_cb_s;

struct _connectivity_resource_s {
	char *device_id;
	char *host_address;
	char *device_name;
	char *type;
	char *uri_path;
	iotcon_remote_resource_h resource;
	time_t last_time;
	connectivity_observe_resource_cb_s *cb_info;
	int total_value;
};

static struct {
	Eina_List *list;
} connectivity_info = {
	.list = NULL,
};

/* FIXME : Now we don't need the key, but in the near future I'll add routines for this key */
int connectivity_resource_add_int(connectivity_resource_s *resource_info, const char *key, int value)
{
	retv_if(!resource_info, -1);

	resource_info->total_value += value;

	return 0;
}

int connectivity_resource_pop_int(connectivity_resource_s *resource_info, const char *key, int *value)
{
	retv_if(!resource_info, -1);
	retv_if(!value, -1);

	*value = resource_info->total_value;
	resource_info->total_value = 0;

	return 0;
}

int connectivity_resource_list_foreach(connectivity_foreach_cb cb, void *data)
{
	Eina_List *l = NULL, *ln = NULL;
	connectivity_resource_s *temp = NULL;

	retv_if(!cb, -1);

	EINA_LIST_FOREACH_SAFE(connectivity_info.list, l, ln, temp) {
		cb(temp, temp->uri_path, data);
	}

	return 0;
}

static void _observe_cb(iotcon_remote_resource_h resource, iotcon_error_e err, int sequence_number, iotcon_response_h response, void *user_data)
{
	int ret = -1;
	bool opened = false;
	iotcon_attributes_h attributes = NULL;
	iotcon_representation_h repr = NULL;
	iotcon_response_result_e response_result;
	connectivity_resource_s *resource_info = user_data;

	ret_if(!resource_info);
	ret_if(!resource_info->cb_info);
	ret_if(!resource_info->cb_info->cb);
	ret_if(IOTCON_ERROR_NONE != err);

	ret = iotcon_response_get_result(response, &response_result);
	if (IOTCON_ERROR_NONE != ret) {
		_E("iotcon_response_get_result() Fail(%d)", ret);
		return;
	}

	if (IOTCON_RESPONSE_OK != response_result) {
		_E("_on_response_observe Response error(%d)", response_result);
		return;
	}

	ret = iotcon_response_get_representation(response, &repr);
	ret_if(IOTCON_ERROR_NONE != ret);

	ret = iotcon_representation_get_attributes(repr, &attributes);
	ret_if(IOTCON_ERROR_NONE != ret);

	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);
}

/* return values : -1 error, 0 not exist, 1 exist */
static int _exist_uri_path_in_list(iotcon_remote_resource_h resource)
{
	Eina_List *l = NULL, *ln = NULL;
	connectivity_resource_s *temp = NULL;
	char *uri_path = NULL;
	int ret = -1;

	retv_if(!resource, -1);

	ret = iotcon_remote_resource_get_uri_path(resource, &uri_path);
	retv_if(IOTCON_ERROR_NONE != ret, -1);

	EINA_LIST_FOREACH_SAFE(connectivity_info.list, l, ln, temp) {
		continue_if(!temp->uri_path);
		if (!strncmp(temp->uri_path, uri_path, strlen(uri_path))) {
			_D("\"%s\" already found. skip !", uri_path);
			return 1;
		}
	}

	_I("Resource[%s] is not in the list", uri_path);

	return 0;
}

static int _add_resource_info_into_list(connectivity_resource_s *info)
{
	retv_if(!info, -1);
	connectivity_info.list = eina_list_append(connectivity_info.list, info);
	return 0;
}

#if 0
static connectivity_resource_s *_pop_device_id_from_list(const char *device_id)
{
	Eina_List *l = NULL, *ln = NULL;
	connectivity_resource_s *temp = NULL;

	retv_if(!device_id, NULL);

	EINA_LIST_FOREACH_SAFE(connectivity_info.list, l, ln, temp) {
		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);
			return temp;
		}
	}

	return NULL;
}
#endif

static bool _get_res_iface_cb(const char *string, void *user_data)
{
	char *resource_uri_path = user_data;
	_I("[%s] resource interface : %s", resource_uri_path, string);
	return IOTCON_FUNC_CONTINUE;
}

static bool _get_res_type_cb(const char *string, void *user_data)
{
	char *resource_uri_path = user_data;
	_I("[%s] resource type : %s", resource_uri_path, string);
	return IOTCON_FUNC_CONTINUE;
}

static int _retrieve_resource_info(iotcon_remote_resource_h resource, connectivity_resource_s **info)
{
	int ret = -1;
	char *device_id = NULL;
	char *host_address = NULL;
	char *device_name = NULL;
	char *uri_path = NULL;
	iotcon_resource_interfaces_h resource_interfaces;
	iotcon_resource_types_h resource_types;
	iotcon_connectivity_type_e connectivity_type;

	retv_if(!info, -1);

	ret = iotcon_remote_resource_get_device_id(resource, &device_id);
	retv_if(IOTCON_ERROR_NONE != ret, -1);

	ret = iotcon_remote_resource_get_host_address(resource, &host_address);
	retv_if(IOTCON_ERROR_NONE != ret, -1);

	ret = iotcon_remote_resource_get_device_name(resource, &device_name);
	retv_if(IOTCON_ERROR_NONE != ret, -1);

	ret = iotcon_remote_resource_get_uri_path(resource, &uri_path);
	retv_if(IOTCON_ERROR_NONE != ret, -1);

	ret = iotcon_remote_resource_get_connectivity_type(resource, &connectivity_type);
	retv_if(IOTCON_ERROR_NONE != ret, -1);

	ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
	retv_if(IOTCON_ERROR_NONE != ret, -1);

	ret = iotcon_resource_interfaces_foreach(resource_interfaces, _get_res_iface_cb, uri_path);
	retv_if(IOTCON_ERROR_NONE != ret, -1);

	ret = iotcon_remote_resource_get_types(resource, &resource_types);
	retv_if(IOTCON_ERROR_NONE != ret, -1);

	ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb, uri_path);
	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_id);

	(*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)->device_name = strdup(device_name);
	goto_if(!(*info)->device_name, error);

	(*info)->uri_path = strdup(uri_path);
	goto_if(!(*info)->uri_path, error);

	return 0;

error:
	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);
		*info = NULL;
	}

	return -1;
}

static void _free_resource_info(connectivity_resource_s *info)
{
	ret_if(!info);

	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);

	if (info->device_name)
		free(info->device_name);

	if (info->host_address)
		free(info->host_address);

	if (info->device_id)
		free(info->device_id);

	free(info);
}

static void _free_resource_info_list(void)
{
	connectivity_resource_s *info = NULL;

	EINA_LIST_FREE(connectivity_info.list, info) {
		_free_resource_info(info);
	}
}

static int _register_observe(connectivity_resource_s *info)
{
	int ret = -1;

	retv_if(!info, -1);

	/* resource is cloned data */
	ret = iotcon_remote_resource_observe_register(info->resource,
			IOTCON_OBSERVE_IGNORE_OUT_OF_ORDER,
			NULL,
			_observe_cb,
			info);
	retv_if(IOTCON_ERROR_NONE != ret, -1);

	return 0;
}

static void _unregister_observe(connectivity_resource_s *info)
{
	ret_if(!info);
	iotcon_remote_resource_observe_deregister(info->resource);
}

static bool _found_resource_cb(iotcon_remote_resource_h resource, iotcon_error_e result, void *user_data)
{
	int ret = -1;
	connectivity_resource_s *info = NULL;

	/* We should know where we get wrong results */
	/* Can attackers send wrong messages to stop this? */
	retv_if(IOTCON_ERROR_NONE != result, IOTCON_FUNC_CONTINUE);
	if (!resource) return IOTCON_FUNC_CONTINUE;

	_I("Resource is found");

	ret = _exist_uri_path_in_list(resource);
	retv_if(-1 == ret, IOTCON_FUNC_CONTINUE);
	if (1 == ret) return IOTCON_FUNC_CONTINUE;

	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);
	}

	ret = _add_resource_info_into_list(info);
	goto_if(-1 == ret, error);

	return IOTCON_FUNC_CONTINUE;

error:
	if (info) _free_resource_info(info);
	return IOTCON_FUNC_CONTINUE;
}

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;

	_copy_file(CBOR_FILE_IN_RES, CBOR_FILE_IN_DATA);

	ret = iotcon_initialize(CBOR_FILE_IN_DATA);
	if (IOTCON_ERROR_NONE != ret) {
		_E("iotcon_initialize() Fail(%d)", ret);
		return -1;
	}

	return 0;
}

int connectivity_fini(void)
{
	/* deinitialize iotcon */
	_free_resource_info_list();
	iotcon_deinitialize();
	return 0;
}

int connectivity_observe_resource(const char *type, connectivity_observe_resource_cb cb, void *user_data)
{
	int ret = -1;
	iotcon_query_h query = NULL;
	connectivity_observe_resource_cb_s *cb_info = NULL;

	cb_info = calloc(1, sizeof(connectivity_observe_resource_cb_s));
	retv_if(!cb_info, -1);
	cb_info->cb = cb;
	cb_info->user_data = user_data;

	ret = iotcon_query_create(&query);
	goto_if(IOTCON_ERROR_NONE != ret, error);

	ret = iotcon_query_set_resource_type(query, type);
	goto_if(IOTCON_ERROR_NONE != ret, error);

	ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS,
			IOTCON_CONNECTIVITY_IP | IOTCON_CONNECTIVITY_PREFER_UDP,
			query,
			_found_resource_cb,
			cb_info);
	goto_if(IOTCON_ERROR_NONE != ret, error);

	iotcon_query_destroy(query);
	return 0;

error:
	iotcon_query_destroy(query);
	if (cb_info) {
		free(cb_info);
	}
	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);
		}
	}
}