summaryrefslogtreecommitdiff
path: root/test/uat-init.c
blob: 10a0fad7022d006260b5b9e2df22ba97ac5489d1 (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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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 <string.h>

#include <glib.h>
#include <gio/gio.h>

#include <user-awareness.h>
#include <user-awareness-internal.h>

#include "uat-menu.h"
#include "uat-common.h"

extern ua_monitor_h g_ua_mon_h; /**< Monitor handle */
extern ua_service_h g_service_h; /**< Service handle */
extern char g_service_str[MENU_DATA_SIZE + 1]; /**< Service name */
extern char g_sensor_list_str[MENU_DATA_SIZE + 1]; /**< Sendosr list string */
extern unsigned int g_sensor_list; /**< Added sensor list */

static char g_app_id_str[MENU_DATA_SIZE + 1] = {0,}; /**< User app id */
static char g_uid[MENU_DATA_SIZE + 1] = {0,}; /**< User id */
static char g_ibeacon_adv_len[MENU_DATA_SIZE + 1] = {0,}; /**< iBeacon advertising data length */
static char g_ibeacon_adv_str[MENU_DATA_SIZE + 1] = {0,}; /**< iBeacon advertising data */

static void __user_detected_foreach_sensors(gpointer data,
	gpointer user_data)
{
	int ret;
	char buf[MENU_DATA_SIZE] = {0, };
	char final_buf[MENU_DATA_SIZE * 4] = {0, };
	long int timestamp;
	int info_count = 0;
	GSList *values = NULL;
	double *value;
	GSList *l = 0;
	ua_sensor_e bitmask;
	ua_device_h sensor_handle = (ua_sensor_h)data;

	ret = ua_sensor_get_bitmask(sensor_handle, &bitmask);
	if (UA_ERROR_NONE != ret) {
		msg(" - ua_sensor_get_bitmask() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));
	}

	ret = ua_sensor_get_timestamp(sensor_handle, &timestamp);
	if (UA_ERROR_NONE != ret) {
		msg(" - ua_sensor_get_timestamp() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));
	}

	ret = ua_sensor_get_info_count(sensor_handle, &info_count);
	if (UA_ERROR_NONE != ret) {
		msg(" - ua_sensor_get_info_count() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));
	}

	values = ua_sensor_get_info_values(sensor_handle);
	if (NULL == values) {
		msg(" - ua_sensor_get_info_values() failed");
	}

	for (l = values; l; l = g_slist_next(l)) {
		value = (double *)l->data;
		snprintf(buf, MENU_DATA_SIZE, "%lF ", *value);
		strncat(final_buf, buf, sizeof(buf) - strlen(buf) - 1);
		memset(buf, 0, MENU_DATA_SIZE);
	}
	msgb("[%s] information detected at timestamp [%ld] value [%s]",
		uat_get_sensor_bitmask_str(bitmask), timestamp,
			final_buf);

	g_slist_free(values);
}

static void __user_absence_detected_cb(int result, ua_monitor_h monitor,
		ua_user_h user_handle, GSList *sensor_handles, void *user_data)
{
	int ret;
	char *account = NULL;
	char *pbuf = uat_get_time();

	msg("\n[%s]", pbuf);
	free(pbuf);

	ret = ua_user_get_account(user_handle, &account);
	if (UA_ERROR_NONE != ret) {
		msg(" - ua_user_get_account() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));
	}
	msgp("[%s] ABSENCE detected [%s]", account, uat_get_error_str(result));

	g_slist_foreach(sensor_handles,
			__user_detected_foreach_sensors, NULL);

	if (account) {
		free(account);
	}
}

const char * __convert_device_mac_type_to_txt(ua_mac_type_e mac_type)
{
	switch (mac_type) {
	case UA_MAC_TYPE_BT:
		return "BT";
	case UA_MAC_TYPE_BLE:
		return "BLE";
	case UA_MAC_TYPE_WIFI:
		return "WIFI";
	case UA_MAC_TYPE_P2P:
		return "P2P";
	default:
		return "UNKNOWN";
	}
}

static void __user_presence_detected_foreach_devices(gpointer data,
	gpointer user_data)
{
	int ret;
	ua_device_h device_handle = (ua_device_h)data;
	char *account = (char *)user_data;
	ua_mac_type_e mac_type;
	char *mac = NULL;

	ret = ua_device_get_mac_address(device_handle, &mac);
	if (UA_ERROR_NONE != ret) {
		msg(" - ua_device_get_mac_address() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));
	}
	ret = ua_device_get_mac_type(device_handle, &mac_type);
	if (UA_ERROR_NONE != ret) {
		msg(" - ua_device_get_mac_type() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));
	}
	msgb("[%s] detected on [%s][%s]", account,
	     __convert_device_mac_type_to_txt(mac_type), mac);

	g_free(mac);
}

static void __user_presence_detected_cb(int result, ua_monitor_h monitor,
	ua_user_h user_handle, GSList *device_handles, GSList *sensor_handles,
	void *user_data)
{
	int ret;
	char *account = NULL;
	char *pbuf = uat_get_time();

	msg("\n[%s]", pbuf);
	free(pbuf);

	if (user_handle) {
		ret = ua_user_get_account(user_handle, &account);
		if (UA_ERROR_NONE != ret) {
			msg(" - ua_user_get_account() ret: [0x%X] [%s]",
				ret, uat_get_error_str(ret));
		}
		msgb("[%s] PRESENCE detected [%s]", account, uat_get_error_str(result));
		g_slist_foreach(device_handles,
				__user_presence_detected_foreach_devices,
				account);
	}

	if (!account) {
		msgb("[No user information] PRESENCE detected [%s]", uat_get_error_str(result));
	}

	g_slist_foreach(sensor_handles,
			__user_detected_foreach_sensors, NULL);

	g_free(account);
}

static void __sensor_state_changed_cb(bool ready,
		ua_sensor_e sensor, ua_monitor_h monitor, void *user_data)
{
	msgb("sensor: %s is %s",
			uat_get_sensor_bitmask_str(sensor), (ready ? "Ready" : "Not Ready"));
}
static void __sensor_status_changed_cb(ua_sensor_status_e status,
	ua_monitor_h handle, ua_sensor_h sensor_handle, void *user_data)
{
	int ret;
	double *value;
	GSList *l = 0;
	long int timestamp;
	int info_count = 0;
	ua_sensor_e sensor;
	GSList *values = NULL;
	char buf[MENU_DATA_SIZE] = {0, };
	char final_buf[MENU_DATA_SIZE * 4] = {0, };
	char *pbuf = uat_get_time();

	msgr("\n[%s] Sensor Status Changed -> %s",
		pbuf, status == UA_SENSOR_PRESENCE ? "PRESENCE" : "ABSENCE");
	free(pbuf);

	ret = ua_sensor_get_bitmask(sensor_handle, &sensor);
	if (UA_ERROR_NONE != ret) {
		msg(" - ua_sensor_get_bitmask() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));
	}

	ret = ua_sensor_get_timestamp(sensor_handle, &timestamp);
	if (UA_ERROR_NONE != ret) {
		msg(" - ua_sensor_get_timestamp() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));
	}

	ret = ua_sensor_get_info_count(sensor_handle, &info_count);
	if (UA_ERROR_NONE != ret) {
		msg(" - ua_sensor_get_info_count() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));
	}

	values = ua_sensor_get_info_values(sensor_handle);
	if (NULL == values) {
		msg(" - ua_sensor_get_info_values() failed");
	}

	for (l = values; l; l = g_slist_next(l)) {
		value = (double *)l->data;
		snprintf(buf, MENU_DATA_SIZE, "%lF ", *value);
		strncat(final_buf, buf, sizeof(buf) - strlen(buf) - 1);
		memset(buf, 0, MENU_DATA_SIZE);
	}
	msgr("[%s] information detected at timestamp [%ld] value [%s]",
		uat_get_sensor_bitmask_str(sensor), timestamp, final_buf);

	g_slist_free(values);
}

static int run_ua_monitor_create(MManager *mm, struct menu_data *menu)
{
	const char *buf = NULL;
	int ret = UA_ERROR_NONE;
	bool is_available = false;

	msg("ua_monitor_create");
	if (g_ua_mon_h)
		ua_monitor_destroy(g_ua_mon_h);

	ret = ua_monitor_create(&g_ua_mon_h);

	msg(" - ua_monitor_create() ret: [0x%X] [%s]", ret, uat_get_error_str(ret));

	if (UA_ERROR_NONE != ret)
		return RET_SUCCESS;

	memset(g_sensor_list_str, 0, MENU_DATA_SIZE + 1);
	for (unsigned int i = UA_SENSOR_BT ; i < UA_SENSOR_MAX; i <<= 1) {
		ret = ua_monitor_is_sensor_available(i, &is_available);
		if (UA_ERROR_NONE == ret && is_available) {

			/* If sensor plugins are loaded, enable them */
			ua_monitor_add_sensor(g_ua_mon_h, i);
			/* Save loaded sensor */
			g_sensor_list |= i;

			buf = uat_get_sensor_bitmask_str(i);
			strncat(g_sensor_list_str, buf,
					sizeof(g_sensor_list_str) - strlen(g_sensor_list_str) - 1);
			strncat(g_sensor_list_str, " ",
					sizeof(g_sensor_list_str) - strlen(g_sensor_list_str) - 1);

		}
		is_available = false;
	}

	return RET_SUCCESS;
}

static int run_ua_monitor_destroy(MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;

	msg("ua_monitor_destroy");
	if (g_ua_mon_h)
		ret = ua_monitor_destroy(g_ua_mon_h);

	msg(" - ua_monitor_destroy() ret: [0x%X] [%s]", ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int run_ua_monitor_set_user_absence_detected_cb(
		MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;

	msg("ua_monitor_set_absence_detected_cb");

	ret = ua_monitor_set_user_absence_detected_cb(g_ua_mon_h,
			__user_absence_detected_cb, g_ua_mon_h);

	msg(" - ua_monitor_set_absence_detected_cb() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int run_ua_monitor_set_user_presence_detected_cb(
		MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;

	msg("ua_monitor_set_presence_detected_cb");

	ret = ua_monitor_set_user_presence_detected_cb(g_ua_mon_h,
			__user_presence_detected_cb, g_ua_mon_h);

	msg(" - ua_monitor_set_presence_detected_cb() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int run_ua_monitor_set_sensor_state_cb(
		MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;

	msg("ua_monitor_set_sensor_state_cb");

	ret = ua_monitor_set_sensor_state_cb(g_ua_mon_h,
			__sensor_state_changed_cb, g_ua_mon_h);

	msg(" - ua_monitor_set_sensor_state_cb() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int run_ua_monitor_set_sensor_status_cb(
		MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;

	msg("ua_monitor_set_sensor_status_cb");

	ret = ua_monitor_set_sensor_status_cb(g_ua_mon_h,
			__sensor_status_changed_cb, g_ua_mon_h);

	msg(" - ua_monitor_set_sensor_status_cb() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int run_ua_monitor_unset_user_absence_detected_cb(
		MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;

	msg("ua_monitor_unset_absence_detected_cb");

	ret = ua_monitor_unset_user_absence_detected_cb(g_ua_mon_h);

	msg(" - ua_monitor_unset_absence_detected_cb() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int run_ua_monitor_unset_user_presence_detected_cb(
		MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;

	msg("ua_monitor_unset_presence_detected_cb");

	ret = ua_monitor_unset_user_presence_detected_cb(g_ua_mon_h);

	msg(" - ua_monitor_unset_presence_detected_cb() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int run_ua_monitor_unset_sensor_state_cb(
		MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;

	msg("ua_monitor_unset_sensor_state_cb");

	ret = ua_monitor_unset_sensor_state_cb(g_ua_mon_h);

	msg(" - ua_monitor_unset_sensor_state_cb() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int run_ua_monitor_unset_sensor_status_cb(
		MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;

	msg("ua_monitor_unset_sensor_status_cb");

	ret = ua_monitor_unset_user_absence_detected_cb(g_ua_mon_h);

	msg(" - ua_monitor_unset_sensor_status_cb() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int run_ua_set_app_info(MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;
	unsigned short uid_value = 0;

	msg("ua_set_app_info");

	if (strlen(g_uid))
		uid_value = (unsigned short)strtol(g_uid, NULL, 10);

	ret = ua_set_app_info(g_app_id_str, uid_value);

	msg(" - ua_set_app_info() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int run_ua_unset_app_info(MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;
	unsigned short uid_value = 0;

	msg("ua_unset_app_info");

	if (strlen(g_uid))
		uid_value = (unsigned short)strtol(g_uid, NULL, 10);

	ret = ua_unset_app_info(g_app_id_str, uid_value);

	msg(" - ua_unset_app_info() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int run_ua_reset_database(
		MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;

	msg("ua_reset_database");

	ret = ua_reset_database();

	msg(" - ua_reset_database() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static int _scan_ibeacon_adv_str(
	MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;
	int temp = 0;
	int length = 0;

	memset(g_ibeacon_adv_str, 0, MENU_DATA_SIZE + 1);

	if (strlen(g_ibeacon_adv_len))
		length = (unsigned int)strtol(g_ibeacon_adv_len, NULL, 10);
	if (length > UA_IBEACON_ADV_MAX_LEN) {
		msg("Error - iBeacon adv length is greater than %d", UA_IBEACON_ADV_MAX_LEN);
		return RET_SUCCESS;
	}

	for (int i = 0; i < length; i++) {
		ret = fscanf(stdin, "%d", &temp);
		if(!ret)
			msg(" - fscanf ret: %d", ret);

		g_ibeacon_adv_str[i] = (unsigned char)temp;
	}

	return RET_SUCCESS;
}

static int run_ua_add_ibeacon_adv_data(
	MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;
	int length = 0;

	msg("ua_add_ibeacon_adv_data");

	if (strlen(g_ibeacon_adv_len))
		length = (unsigned int)strtol(g_ibeacon_adv_len, NULL, 10);

	ret = ua_add_ibeacon_adv_data(length, g_ibeacon_adv_str);

	msg(" - ua_add_ibeacon_adv_data() ret: [0x%X] [%s]",
			ret, uat_get_error_str(ret));

	return RET_SUCCESS;
}

static struct menu_data menu_ua_set_app_info[] = {
	{ "1", "app_id (e.g. 'org.tizen.factory')",
		NULL, NULL, g_app_id_str },
	{ "2", "uid (Uid found using 'ps -eo uid,gid,args | grep TheApp')",
		NULL, NULL, g_uid },
	{ "3", "run", NULL,
		run_ua_set_app_info, NULL },
	{ NULL, NULL, },
};

static struct menu_data menu_ua_unset_app_info[] = {
	{ "1", "app_id (e.g. 'org.tizen.factory')",
		NULL, NULL, g_app_id_str },
	{ "2", "uid (Uid found using 'ps -eo uid,gid,args | grep TheApp')",
		NULL, NULL, g_uid },
	{ "3", "run", NULL,
		run_ua_unset_app_info, NULL },
	{ NULL, NULL, },
};

static struct menu_data menu_ua_add_ibeacon_adv_data[] = {
	{ "1", "Length of data(Max is 31)",
		NULL, NULL, g_ibeacon_adv_len },
	{ "2", "iBeacon adv data (each byte is 0 to 255)",
		NULL, _scan_ibeacon_adv_str, NULL },
	{ "3", "run", NULL,
		run_ua_add_ibeacon_adv_data, NULL },
	{ NULL, NULL, },
};

struct menu_data menu_ua_init[] = {
	{ "1", "ua_monitor_create",
		NULL, run_ua_monitor_create, NULL },
	{ "2", "ua_monitor_destroy",
		NULL, run_ua_monitor_destroy, NULL },
	{ "3", "ua_monitor_set_sensor_state_cb",
		NULL, run_ua_monitor_set_sensor_state_cb, NULL },
	{ "4", "ua_monitor_unset_sensor_state_cb",
		NULL, run_ua_monitor_unset_sensor_state_cb, NULL },
	{ "5", "ua_monitor_set_sensor_status_cb",
		NULL, run_ua_monitor_set_sensor_status_cb, NULL },
	{ "6", "ua_monitor_unset_sensor_status_cb",
		NULL, run_ua_monitor_unset_sensor_status_cb, NULL },
	{ "7", "ua_monitor_set_user_absence_detected_cb",
		NULL, run_ua_monitor_set_user_absence_detected_cb, NULL },
	{ "8", "ua_monitor_unset_absence_detected_cb",
		NULL, run_ua_monitor_unset_user_absence_detected_cb, NULL },
	{ "9", "ua_monitor_set_user_presence_detected_cb",
		NULL, run_ua_monitor_set_user_presence_detected_cb, NULL },
	{ "10", "ua_monitor_unset_presence_detected_cb",
		NULL, run_ua_monitor_unset_user_presence_detected_cb, NULL },
	{ "11", "ua_set_app_info",
		menu_ua_set_app_info, NULL, NULL},
	{ "12", "ua_unset_app_info",
		menu_ua_unset_app_info, NULL, NULL},
	{ "13", "ua_reset_database",
		NULL, run_ua_reset_database, NULL},
	{ "14", "ua_add_ibeacon_adv_data",
		menu_ua_add_ibeacon_adv_data, NULL, NULL},
	{ NULL, NULL, },
};