summaryrefslogtreecommitdiff
path: root/src/user-awareness-event-handler.c
blob: b968b47c2846936d654bb1a16fb05cebfdd6dfbf (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
/*
 * 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 <glib.h>
#include <ua-api.h>

#include <user-awareness.h>
#include <user-awareness-log.h>
#include <user-awareness-private.h>
#include <user-awareness-util.h>

static gboolean is_initialized = FALSE;

/* LCOV_EXCL_START */
static void __ua_event_handler(int event, uam_event_data_t *event_param, void *user_data)
{
	FUNC_ENTRY;

	ret_if(NULL == event_param);

	switch (event) {
	case UAM_EVENT_USER_PRESENCE_DETECTED: {
		uam_detection_event_data_t *event_data = NULL;
		ua_user_info_s* user_info;
		ua_user_h user_handle = NULL;

		event_data = event_param->data;
		ret_if(NULL == event_data);

		user_handle = _ua_get_user_handle_by_account(event_data->account);

		if (user_handle) {
			user_info = (ua_user_info_s*) user_handle;
			_ua_mark_user_info_state_db(user_info->account,
					UA_PRESENCE_STATE_ACTIVATE, event_data->sensor_bitmask);

			_ua_monitor_handle_user_presence_detected(
					event_data->sensor_bitmask, user_info);
		}

		break;
	}
	case UAM_EVENT_USER_ABSENCE_DETECTED: {
		uam_detection_event_data_t *event_data = NULL;
		ua_user_info_s* user_info;
		ua_user_h user_handle = NULL;

		event_data = event_param->data;
		ret_if(NULL == event_data);

		user_handle = _ua_get_user_handle_by_account(event_data->account);

		if (user_handle) {
			user_info = (ua_user_info_s*) user_handle;
			_ua_mark_user_info_state_db(user_info->account,
					UA_PRESENCE_STATE_INACTIVATE, event_data->sensor_bitmask);

			_ua_monitor_handle_user_absence_detected(
					event_data->sensor_bitmask, user_info);
		}

		break;
	}
	case UAM_EVENT_PRESENCE_DETECTED: {
		unsigned int *sensor_bitmask = event_param->data;

		ret_if(NULL == sensor_bitmask);

		_ua_monitor_handle_user_presence_detected(*sensor_bitmask, NULL);
		break;
	}
	case UAM_EVENT_ABSENCE_DETECTED: {
		unsigned int *sensor_bitmask = event_param->data;

		ret_if(NULL == sensor_bitmask);

		_ua_monitor_handle_user_absence_detected(*sensor_bitmask, NULL);
		break;
	}
	case UAM_EVENT_DETECTION_STARTED: {
		UA_INFO("Received DETECTION STARTED event !!");
		break;
	}
	case UAM_EVENT_DETECTION_STOPPED: {
		_ua_monitor_handle_detection_stopped();
		break;
	}
	case UAM_EVENT_SENSOR_STATE_READY: {
		unsigned int *sensor_bitmask = event_param->data;

		ret_if(NULL == sensor_bitmask);

		_ua_monitor_handle_sensor_state(*sensor_bitmask, TRUE);
		break;
	}
	case UAM_EVENT_SENSOR_STATE_NOT_READY: {
		unsigned int *sensor_bitmask = event_param->data;

		ret_if(NULL == sensor_bitmask);
		ret_if(0 == *sensor_bitmask);

		_ua_monitor_handle_sensor_state(*sensor_bitmask, FALSE);
		break;
	}
	case UAM_EVENT_USER_ADDED: {
		uam_user_info_t *event_data = NULL;

		event_data = event_param->data;
		ret_if(NULL == event_data);

		if (_ua_user_add_info_to_list_from_uapi_data(event_data) != UA_ERROR_NONE)
			UA_ERR("Fail to add user");
		else
			UA_INFO("User Added");
		break;
	}
	case UAM_EVENT_USER_REMOVED: {
		uam_user_info_t *event_data = NULL;

		event_data = event_param->data;
		ret_if(NULL == event_data);

		if (_ua_remove_user_info_from_list(event_data->account) != UA_ERROR_NONE)
			UA_ERR("Fail to remove user");
		else
			UA_INFO("Removed user");
		break;
	}
	case UAM_EVENT_DEVICE_ADDED: {
		uam_device_info_t *dev_info = NULL;

		dev_info = event_param->data;
		ret_if(NULL == dev_info);

		_ua_handle_device_added(event_param->result, dev_info);
		break;
	}
	case UAM_EVENT_DEVICE_REMOVED: {
		uam_device_info_t *dev_info = NULL;

		dev_info = event_param->data;
		ret_if(NULL == dev_info);

		_ua_handle_device_removed(event_param->result, dev_info);
		break;
	}

	case UAM_EVENT_DEVICE_FOUND: {
		uam_device_info_t *dev_info = NULL;

		dev_info = event_param->data;
		ret_if(NULL == dev_info);

		_ua_monitor_handle_scanned_device(event_param->result, dev_info);
		break;
	}

	case UAM_EVENT_SCAN_COMPLETED: {
		_ua_monitor_handle_scan_complete(event_param->result);
		break;
	}

	default:
		UA_WARN("Unknown Event received");
		break;
	}

	FUNC_EXIT;
}
/* LCOV_EXCL_STOP */

static bool __foreach_intr_registered_user_info(
		ua_user_h handle, void *user_data)
{
	int ret;
	ua_user_info_s* user_info;
	retv_if(NULL == handle, false);

	user_info = (ua_user_info_s*)handle;
	/* Initial state need to be set ACTIVATE to handle absence case */
	ret = _ua_user_add_info_to_list(user_info);

	if (ret != UA_ERROR_NONE)
		return false;
	else
		return true;
}

int ua_initialize(void)
{
	FUNC_ENTRY;
	int ret;

	retv_if(TRUE == is_initialized, UA_ERROR_ALREADY_DONE);

	ret = _ua_get_error_code(_uam_init(__ua_event_handler, NULL));
	if (UA_ERROR_NONE != ret) {
		UA_ERR("Failed with error: %s(0x%X)",
				_ua_get_error_string(ret), ret);
		return ret;
	}

	ret = _ua_intr_foreach_registered_users(__foreach_intr_registered_user_info, NULL);
	if (UA_ERROR_NONE != ret) {
		/* LCOV_EXCL_START */
		UA_ERR("Failed with error: %s(0x%X)",
				_ua_get_error_string(ret), ret);
		return ret;
		/* LCOV_EXCL_STOP */
	}

	ret = _ua_intr_get_default_user();
	if (UA_ERROR_NONE != ret) {
		/* LCOV_EXCL_START */
		UA_ERR("Failed with error: %s(0x%X)",
				_ua_get_error_string(ret), ret);
		return ret;
		/* LCOV_EXCL_STOP */
	}

	is_initialized = TRUE;

	FUNC_EXIT;
	return UA_ERROR_NONE;
}

int ua_deinitialize(void)
{
	FUNC_ENTRY;
	int ret;

	retv_if(TRUE != is_initialized, UA_ERROR_NOT_INITIALIZED);

	ret = _ua_get_error_code(_uam_deinit());
	if (UA_ERROR_NONE != ret) {
		/* LCOV_EXCL_START */
		UA_ERR("Failed with error: %s(0x%X)",
				_ua_get_error_string(ret), ret);
		return ret;
		/* LCOV_EXCL_STOP */
	}

	is_initialized = FALSE;

	FUNC_EXIT;
	return UA_ERROR_NONE;
}