summaryrefslogtreecommitdiff
path: root/test/uat-sensors.c
blob: be82beea62534250ae4680b75d24b39a9285bdbf (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
/*
 * 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;

static char g_sensor_type[MENU_DATA_SIZE + 1] = {0,}; /**< Selected Sensor type */
static char g_sensor_type_str[MENU_DATA_SIZE + 1] = {0,}; /**< Selected Sensor type string */
static char g_presence_threshold[MENU_DATA_SIZE + 1] = {0,}; /**< PRESENCE threashold for litght sensor */
static char g_absence_threshold[MENU_DATA_SIZE + 1] = {0,}; /**< ABSENCE threashold for litght sensor  */

unsigned short g_sensor_list; /**< Added sensor list */
char g_sensor_list_str[MENU_DATA_SIZE + 1] = {0,}; /**< Added sensor list string */

static unsigned int __sensor_type_to_bitmask(int idx)
{
	unsigned int bitmask;
	switch (idx) {
	case UAT_SENSOR_TYPE_BT:
		bitmask = UA_SENSOR_BT;
		break;
	case UAT_SENSOR_TYPE_BLE:
		bitmask = UA_SENSOR_BLE;
		break;
	case UAT_SENSOR_TYPE_WIFI:
		bitmask = UA_SENSOR_WIFI;
		break;
	case UAT_SENSOR_TYPE_MOTION:
		bitmask = UA_SENSOR_MOTION;
		break;
	case UAT_SENSOR_TYPE_LIGHT:
		bitmask = UA_SENSOR_LIGHT;
		break;
	case UAT_SENSOR_TYPE_AUDIO:
		bitmask = UA_SENSOR_AUDIO;
		break;
	default:
		msgr("Unsupported sensor [%d]", idx);
		bitmask = 0x00;
	}

	return bitmask;
}

static bool __ua_monitor_foreach_sensor_cb(ua_sensor_e sensor, void *user_data)
{
	const char *buf = uat_get_sensor_bitmask_str(sensor);
	strncat(g_sensor_list_str, buf,
		sizeof(g_sensor_list_str) - strlen(g_sensor_list_str) - 1);
	strncat(g_sensor_list_str, " ", 2);
	return TRUE;
}

static void __update_sensor_list(void)
{
	memset(g_sensor_list_str, 0, MENU_DATA_SIZE + 1);
	ua_monitor_foreach_sensor(g_ua_mon_h,
				 __ua_monitor_foreach_sensor_cb,
				 NULL);
}

static int run_ua_monitor_add_sensor(
		MManager *mm, struct menu_data *menu)
{
	int sensortype = -1;
	int ret = UA_ERROR_NONE;
	msg("ua_monitor_add_sensor");

	check_if(NULL == g_ua_mon_h);

	if (strlen(g_sensor_type)) {
		sensortype = (unsigned char)strtol(g_sensor_type, NULL, 10);
		snprintf(g_sensor_type_str, MENU_DATA_SIZE, "%s",
			uat_get_sensor_str(sensortype));
	}

	ret = ua_monitor_add_sensor(g_ua_mon_h,
			__sensor_type_to_bitmask(sensortype));

	if (UA_ERROR_NONE == ret)
		__update_sensor_list();

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

	return RET_SUCCESS;
}

static int run_ua_monitor_remove_sensor(
	MManager *mm, struct menu_data *menu)
{
	int sensortype = -1;
	int ret = UA_ERROR_NONE;

	msg("ua_monitor_remove_sensor");

	check_if(NULL == g_ua_mon_h);

	if (strlen(g_sensor_type)) {
		sensortype = (unsigned char)strtol(g_sensor_type, NULL, 10);
		snprintf(g_sensor_type_str, MENU_DATA_SIZE, "%s",
			uat_get_sensor_str(sensortype));
	}

	ret = ua_monitor_remove_sensor(g_ua_mon_h,
			__sensor_type_to_bitmask(sensortype));

	if (UA_ERROR_NONE == ret)
		__update_sensor_list();

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

	return RET_SUCCESS;
}

static int run_ua_monitor_is_sensor_available(
	MManager *mm, struct menu_data *menu)
{
	int sensortype = -1;
	bool is_available = false;
	int ret = UA_ERROR_NONE;

	msg("ua_monitor_is_sensor_available");

	check_if(NULL == g_ua_mon_h);

	if (strlen(g_sensor_type)) {
		sensortype = (unsigned char)strtol(g_sensor_type, NULL, 10);
		snprintf(g_sensor_type_str, MENU_DATA_SIZE, "%s",
			uat_get_sensor_str(sensortype));
	}

	ret = ua_monitor_is_sensor_available(__sensor_type_to_bitmask(sensortype),
			&is_available);

	msg(" - ua_monitor_is_sensor_available() ret: [0x%X] [%s]",
				ret, uat_get_error_str(ret));
	if (UA_ERROR_NONE == ret) {
		if (is_available) {
			msg("%s sensor is available",
				uat_get_sensor_str(sensortype));
		} else {
			msg("%s sensor is not available",
				uat_get_sensor_str(sensortype));
		}
	}

	return RET_SUCCESS;
}

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

	msg("ua_monitor_foreach_sensor");

	check_if(NULL == g_ua_mon_h);

	memset(g_sensor_list_str, 0, MENU_DATA_SIZE + 1);

	ret = ua_monitor_foreach_sensor(g_ua_mon_h, __ua_monitor_foreach_sensor_cb, NULL);

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

	return RET_SUCCESS;
}

static int run_ua_monitor_set_brightness_threshold(
	MManager *mm, struct menu_data *menu)
{
	int ret = UA_ERROR_NONE;
	unsigned int presencethreshold = 0;
	unsigned int absencethreshold = 0;

	msg("ua_monitor_set_brightness_threshold");

	check_if(NULL == g_ua_mon_h);

	if (strlen(g_presence_threshold))
		presencethreshold = (unsigned int)strtol(g_presence_threshold, NULL, 10);

	if (strlen(g_absence_threshold))
		absencethreshold = (unsigned int)strtol(g_absence_threshold, NULL, 10);

	ret = ua_monitor_set_brightness_threshold(g_ua_mon_h,
			presencethreshold, absencethreshold);

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

	return RET_SUCCESS;
}

static struct menu_data menu_ua_add_sensor[] = {
	{ "1", "type (1:BT 2:BLE 3:Wi-Fi 4:Motion 5:Light 6:Audio)",
		NULL, NULL, g_sensor_type },
	{ "2", "run", NULL,
		run_ua_monitor_add_sensor, NULL },
	{ NULL, NULL, },
};

static struct menu_data menu_ua_remove_sensor[] = {
	{ "1", "type (1:BT 2:BLE 3:Wi-Fi 4:Motion 5:Light 6:Audio)",
		NULL, NULL, g_sensor_type },
	{ "2", "run", NULL,
		run_ua_monitor_remove_sensor, NULL },
	{ NULL, NULL, },
};

static struct menu_data menu_ua_is_available[] = {
	{ "1", "type (1:BT 2:BLE 3:Wi-Fi 4:Motion 5:Light 6:Audio)",
		NULL, NULL, g_sensor_type },
	{ "2", "run", NULL,
		run_ua_monitor_is_sensor_available, NULL },
	{ NULL, NULL, },
};

static struct menu_data menu_ua_set_brightness_threshold[] = {
	{ "1", "Presence Threshold",
		NULL, NULL, g_presence_threshold},
	{ "2", "Absence Threshold",
		NULL, NULL, g_absence_threshold},
	{ "3", "run", NULL,
		run_ua_monitor_set_brightness_threshold, NULL },
	{ NULL, NULL, },
};

struct menu_data menu_ua_sensors[] = {
	{ "1", "ua_monitor_add_sensor",
		menu_ua_add_sensor, NULL, NULL },
	{ "2", "ua_monitor_remove_sensor",
		menu_ua_remove_sensor, NULL, NULL },
	{ "3", "ua_monitor_is_sensor_available",
		menu_ua_is_available, NULL, NULL },
	{ "4", "ua_monitor_is_sensor_available",
		NULL, run_ua_monitor_foreach_sensor, NULL },
	{ "5", "ua_monitor_set_brightness_threshold",
		menu_ua_set_brightness_threshold, NULL, NULL },
	{ NULL, NULL, },
};