summaryrefslogtreecommitdiff
path: root/src/bluetooth-hid.c
blob: 0b8a11fb4308d4f86e442908ba4c7c400e6d3e0e (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
/*
 * Copyright (c) 2011 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 <dlog.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <bluetooth-api.h>
#include <bluetooth-hid-api.h>

#include "bluetooth.h"
#include "bluetooth_private.h"
#include "bluetooth_internal.h"

static bool is_hid_host_initialized = false;

#define BT_CHECK_HID_HOST_SUPPORT() \
{ \
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON); \
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_HID_HOST); \
}

#define BT_CHECK_HID_DEVICE_SUPPORT() \
{ \
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_COMMON); \
	BT_CHECK_SUPPORTED_FEATURE(BT_FEATURE_HID_DEVICE); \
}

#define BT_CHECK_HID_HOST_INIT_STATUS() \
	if (__bt_check_hid_host_init_status() == BT_ERROR_NOT_INITIALIZED) { \
		LOGE("[%s] NOT_INITIALIZED(0x%08x)", __FUNCTION__, BT_ERROR_NOT_INITIALIZED); \
		return BT_ERROR_NOT_INITIALIZED; \
	}

int __bt_check_hid_host_init_status(void)
{
	if (is_hid_host_initialized != true) {
		BT_ERR("NOT_INITIALIZED(0x%08x)", BT_ERROR_NOT_INITIALIZED);
		return BT_ERROR_NOT_INITIALIZED;
	}

	return BT_ERROR_NONE;
}

int bt_hid_host_initialize(bt_hid_host_connection_state_changed_cb connection_cb,
								void *user_data)
{
	int error;

	BT_CHECK_HID_HOST_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(connection_cb);

	error = bluetooth_hid_init(_bt_hid_event_proxy, user_data);
	error = _bt_get_error_code(error);
	if (BT_ERROR_NONE != error) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error); /* LCOV_EXCL_LINE */
		return error; /* LCOV_EXCL_LINE */
	}

	_bt_set_cb(BT_EVENT_HID_CONNECTION_STATUS, connection_cb, user_data);

	is_hid_host_initialized = true;
	return BT_ERROR_NONE;
}

int bt_hid_host_deinitialize()
{
	int error;

	BT_CHECK_HID_HOST_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_HID_HOST_INIT_STATUS();

	error = bluetooth_hid_deinit();
	error = _bt_get_error_code(error);
	if (BT_ERROR_NONE != error) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error); /* LCOV_EXCL_LINE */
		return error; /* LCOV_EXCL_LINE */
	}

	_bt_unset_cb(BT_EVENT_HID_CONNECTION_STATUS);

	is_hid_host_initialized = false;
	return BT_ERROR_NONE;
}

int bt_hid_host_connect(const char *remote_address)
{
	int error;
	bluetooth_device_address_t addr_hex = { {0,} };

	BT_CHECK_HID_HOST_SUPPORT();
	BT_CHECK_INIT_STATUS(); /* LCOV_EXCL_START */
	BT_CHECK_HID_HOST_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);

	_bt_convert_address_to_hex(&addr_hex, remote_address);

	error = bluetooth_hid_connect((hid_device_address_t *)&addr_hex);
	error = _bt_get_error_code(error);
	if (error != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);

	return error; /* LCOV_EXCL_STOP */
}

int bt_hid_host_disconnect(const char *remote_address)
{
	int error;
	bluetooth_device_address_t addr_hex = { {0,} };

	BT_CHECK_HID_HOST_SUPPORT();
	BT_CHECK_INIT_STATUS(); /* LCOV_EXCL_START */
	BT_CHECK_HID_HOST_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);

	_bt_convert_address_to_hex(&addr_hex, remote_address);

	error = bluetooth_hid_disconnect((hid_device_address_t *)&addr_hex);
	error = _bt_get_error_code(error);
	if (error != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);

	return error; /* LCOV_EXCL_STOP */
}
/* LCOV_EXCL_START */
int bt_hid_device_activate(bt_hid_device_connection_state_changed_cb callback, void *user_data)
{
	int error;
	BT_CHECK_HID_DEVICE_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(callback);

	error = bluetooth_hid_device_init(_bt_hid_event_proxy, user_data);
	error = _bt_get_error_code(error);
	if (BT_ERROR_NONE != error) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
		return error;
	}

	error = bluetooth_hid_device_activate();
	error = _bt_get_error_code(error);
	if (error != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
		return error;
	}

	_bt_set_cb(BT_EVENT_HID_DEVICE_CONNECTION_STATUS, callback, user_data);

	return BT_ERROR_NONE;
}

int bt_hid_device_deactivate(void)
{
	int error;
	BT_CHECK_HID_DEVICE_SUPPORT();
	BT_CHECK_INIT_STATUS();

	error = bluetooth_hid_device_deinit();
	error = _bt_get_error_code(error);
	if (BT_ERROR_NONE != error) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
		return error;
	}

	error = bluetooth_hid_device_deactivate();
	error = _bt_get_error_code(error);
	if (error != BT_ERROR_NONE) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
		return error;
	}

	_bt_unset_cb(BT_EVENT_HID_DEVICE_CONNECTION_STATUS);

	return BT_ERROR_NONE;
}

int bt_hid_device_connect(const char *remote_address)
{
	int error;
	BT_CHECK_HID_DEVICE_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);
	error = bluetooth_hid_device_connect(remote_address);
	error = _bt_get_error_code(error);
	if (BT_ERROR_NONE != error) {
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
		return error;
	}
	return BT_ERROR_NONE;
}
int bt_hid_device_disconnect(const char *remote_address)
{
	int error;
	BT_CHECK_HID_DEVICE_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);
	error = bluetooth_hid_device_disconnect(remote_address);
	error = _bt_get_error_code(error);
	if (error != BT_ERROR_NONE)
		BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
	return error;
}

int bt_hid_device_send_mouse_event(const char *remote_address,
		const bt_hid_mouse_data_s *mouse_data)
{
	int ret;
	BT_CHECK_HID_DEVICE_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);
	BT_CHECK_INPUT_PARAMETER(mouse_data);

	if (mouse_data->buttons != BT_HID_MOUSE_BUTTON_LEFT ||
		mouse_data->buttons != BT_HID_MOUSE_BUTTON_RIGHT ||
		mouse_data->buttons != BT_HID_MOUSE_BUTTON_MIDDLE) {
		return BT_ERROR_INVALID_PARAMETER;
	}

	if (mouse_data->axis_x > 127 || mouse_data->axis_x < -128) {
		BT_ERR("Exceed range of axis_x");
		return -1;
	} else if (mouse_data->axis_y > 127 || mouse_data->axis_y < -128) {
		BT_ERR("Exceed range of axis_y");
		return -1;
	} else if (mouse_data->padding > 127 || mouse_data->padding < -128) {
		BT_ERR("Exceed range of padding");
		return -1;
	}

	ret = bluetooth_hid_device_send_mouse_event(remote_address,
			*(hid_send_mouse_event_t *)mouse_data);
	if (ret <= 0) {
		if (ret == -1) {
			/* write fail case */
			if (errno == EACCES || errno == EPERM)
				set_last_result(BT_ERROR_PERMISSION_DENIED);
			else if (errno == EAGAIN || errno == EWOULDBLOCK)
				set_last_result(BT_ERROR_AGAIN);
			else
				set_last_result(BT_ERROR_OPERATION_FAILED);
		} else {
			ret = _bt_get_error_code(ret);
			set_last_result(ret);
		}

		BT_ERR("Write failed, ret = %d", ret);
	}

	return ret;
}

int bt_hid_device_send_key_event(const char *remote_address,
		const bt_hid_key_data_s *key_data)
{
	int ret;
	BT_CHECK_HID_DEVICE_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);
	BT_CHECK_INPUT_PARAMETER(key_data);
	hid_send_key_event_t send_event;

	send_event.modify = key_data->modifier;
	memcpy(send_event.key, key_data->key, sizeof(send_event.key));

	ret = bluetooth_hid_device_send_key_event(remote_address, send_event);
	if (ret <= 0) {
		if (ret == -1) {
			/* write fail case */
			if (errno == EACCES || errno == EPERM)
				set_last_result(BT_ERROR_PERMISSION_DENIED);
			else if (errno == EAGAIN || errno == EWOULDBLOCK)
				set_last_result(BT_ERROR_AGAIN);
			else
				set_last_result(BT_ERROR_OPERATION_FAILED);
		} else {
			ret = _bt_get_error_code(ret);
			set_last_result(ret);
		}

		BT_ERR("Write failed, ret = %d", ret);
	}

	return ret;
}

#ifdef TIZEN_WEARABLE
int bt_hid_device_send_rc_key_event(const char *remote_address,
		const bt_hid_rc_key_data_s *key_data)
{
	int ret;
	BT_CHECK_HID_DEVICE_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);
	BT_CHECK_INPUT_PARAMETER(key_data);
	hid_send_rc_key_event_t send_event;

	send_event.btcode = 0xA1;
	send_event.rep_id = 0xF7;
	memcpy(send_event.key, key_data->key, sizeof(send_event.key));

	ret = bluetooth_hid_device_send_rc_key_event(remote_address, send_event);
	if (ret <= 0) {
		if (ret == -1) {
			/* write fail case */
			if (errno == EACCES || errno == EPERM) {
				set_last_result(BT_ERROR_PERMISSION_DENIED);
				BT_ERR("PERMISSION_DENIED, errno = %d", errno);
			} else if (errno == EAGAIN || errno == EWOULDBLOCK) {
				set_last_result(BT_ERROR_AGAIN);
				BT_ERR("BT_ERROR_AGAIN, errno = %d", errno);
			} else {
				set_last_result(BT_ERROR_OPERATION_FAILED);
				BT_ERR("BT_ERROR_OPERATION_FAILED, errno = %d", errno);
			}
		} else {
			ret = _bt_get_error_code(ret);
			set_last_result(ret);
			BT_ERR("Write failed, ret = %d", ret);
		}
	}

	return ret;
}
#endif

int bt_hid_device_reply_to_report(const char *remote_address,
		bt_hid_header_type_e header_type,
		bt_hid_param_type_e param_type,
		const char *data, unsigned int data_len)
{
	int ret;
	BT_CHECK_HID_DEVICE_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(remote_address);
	BT_CHECK_INPUT_PARAMETER(data);

	ret = bluetooth_hid_device_reply_to_report(remote_address, header_type,
				param_type, data, data_len);
	if (ret <= 0) {
		if (ret == -1) {
			/* write fail case */
			if (errno == EACCES || errno == EPERM) {
				set_last_result(BT_ERROR_PERMISSION_DENIED);
				BT_ERR("PERMISSION_DENIED, errno = %d", errno);
			} else if (errno == EAGAIN || errno == EWOULDBLOCK) {
				set_last_result(BT_ERROR_AGAIN);
				BT_ERR("BT_ERROR_AGAIN, errno = %d", errno);
			} else {
				set_last_result(BT_ERROR_OPERATION_FAILED);
				BT_ERR("BT_ERROR_OPERATION_FAILED, errno = %d", errno);
			}
		} else {
			ret = _bt_get_error_code(ret);
			set_last_result(ret);
			BT_ERR("Write failed, ret = %d", ret);
		}
	}

	return ret;
}

int bt_hid_device_set_data_received_cb(bt_hid_device_data_received_cb callback, void *user_data)
{
	BT_CHECK_HID_DEVICE_SUPPORT();
	BT_CHECK_INIT_STATUS();
	BT_CHECK_INPUT_PARAMETER(callback);
	_bt_set_cb(BT_EVENT_HID_DEVICE_DATA_RECEIVED, callback, user_data);
	return BT_ERROR_NONE;
}

int bt_hid_device_unset_data_received_cb(void)
{
	BT_CHECK_HID_DEVICE_SUPPORT();
	BT_CHECK_INIT_STATUS();
	_bt_unset_cb(BT_EVENT_HID_DEVICE_DATA_RECEIVED);
	return BT_ERROR_NONE;
}
/* LCOV_EXCL_STOP */