3f72f6eb1d79e982d97c7c0d00ab86a35f0d6435
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-device.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "bluetooth-api.h"
19 #include "bt-internal-types.h"
20
21 #include "bt-common.h"
22 #include "bt-request-sender.h"
23 #include "bt-event-handler.h"
24
25 BT_EXPORT_API int bluetooth_bond_device(const bluetooth_device_address_t *device_address)
26 {
27         int result;
28         bt_user_info_t *user_info;
29
30         BT_CHECK_PARAMETER(device_address, return);
31         BT_CHECK_ENABLED(return);
32
33         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_BOND_DEVICE)
34              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
35                 BT_ERR("Don't have a privilege to use this API");
36                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
37         }
38
39         user_info = _bt_get_user_data(BT_COMMON);
40         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
41
42         BT_INIT_PARAMS();
43         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
44
45         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
46
47         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_BOND_DEVICE,
48                 in_param1, in_param2, in_param3, in_param4,
49                 user_info->cb, user_info->user_data);
50
51         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
52
53         return result;
54 }
55
56 BT_EXPORT_API int bluetooth_bond_device_by_type(
57         const bluetooth_device_address_t *device_address,
58         bluetooth_conn_type_t conn_type)
59 {
60         int result;
61         bt_user_info_t *user_info;
62
63         BT_CHECK_PARAMETER(device_address, return);
64         if(conn_type == BLUETOOTH_DEV_CONN_BREDR)
65                 BT_CHECK_ENABLED(return);
66         else if(conn_type == BLUETOOTH_DEV_CONN_LE)
67                 BT_CHECK_ENABLED_LE(return);
68         else if(conn_type == BLUETOOTH_DEV_CONN_DEFAULT) {
69                 BT_CHECK_ENABLED(return);
70                 BT_CHECK_ENABLED_LE(return);
71         }
72
73         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_BOND_DEVICE_BY_TYPE)
74              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
75                 BT_ERR("Don't have a privilege to use this API");
76                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
77         }
78
79         BT_INIT_PARAMS();
80         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
81
82         user_info = _bt_get_user_data(BT_COMMON);
83         retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
84
85         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
86         g_array_append_vals(in_param2, &conn_type, sizeof(unsigned short));
87
88         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_BOND_DEVICE_BY_TYPE,
89                 in_param1, in_param2, in_param3, in_param4,
90                 user_info->cb, user_info->user_data);
91
92         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
93
94         return result;
95 }
96
97 BT_EXPORT_API int bluetooth_cancel_bonding(void)
98 {
99         int result;
100
101         BT_CHECK_ENABLED_ANY(return);
102
103         BT_INIT_PARAMS();
104         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
105
106         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CANCEL_BONDING,
107                 in_param1, in_param2, in_param3, in_param4, &out_param);
108
109         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
110
111         return result;
112 }
113
114 BT_EXPORT_API int bluetooth_unbond_device(const bluetooth_device_address_t *device_address)
115 {
116         int result;
117         bt_user_info_t *user_info;
118
119         BT_CHECK_PARAMETER(device_address, return);
120         BT_CHECK_ENABLED_ANY(return);
121
122         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_UNBOND_DEVICE)
123              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
124                 BT_ERR("Don't have a privilege to use this API");
125                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
126         }
127
128         user_info = _bt_get_user_data(BT_COMMON);
129         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
130
131         BT_INIT_PARAMS();
132         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
133
134         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
135
136         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_UNBOND_DEVICE,
137                 in_param1, in_param2, in_param3, in_param4,
138                 user_info->cb, user_info->user_data);
139
140         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
141
142         return result;
143 }
144
145 BT_EXPORT_API int bluetooth_get_bonded_device(const bluetooth_device_address_t *device_address,
146                                               bluetooth_device_info_t *dev_info)
147 {
148         int result;
149
150         BT_CHECK_PARAMETER(device_address, return);
151         BT_CHECK_PARAMETER(dev_info, return);
152         BT_CHECK_ENABLED_ANY(return);
153
154         BT_INIT_PARAMS();
155         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
156
157         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
158
159         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_BONDED_DEVICE,
160                 in_param1, in_param2, in_param3, in_param4, &out_param);
161
162         if (result == BLUETOOTH_ERROR_NONE) {
163                 if (out_param->len > 0) {
164                         *dev_info = g_array_index(out_param,
165                                         bluetooth_device_info_t, 0);
166                 } else {
167                         BT_ERR("out_param length is 0!!");
168                 }
169         }
170
171         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
172
173         return result;
174 }
175
176 BT_EXPORT_API int bluetooth_get_remote_device(const bluetooth_device_address_t *device_address)
177 {
178         return BLUETOOTH_ERROR_NONE;
179 }
180
181 BT_EXPORT_API int bluetooth_search_service(const bluetooth_device_address_t *device_address)
182 {
183         int result;
184         bt_user_info_t *user_info;
185
186         BT_CHECK_PARAMETER(device_address, return);
187         BT_CHECK_ENABLED_ANY(return);
188
189         if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_SEARCH_SERVICE)
190              == BLUETOOTH_ERROR_PERMISSION_DEINED) {
191                 BT_ERR("Don't have a privilege to use this API");
192                 return BLUETOOTH_ERROR_PERMISSION_DEINED;
193         }
194
195         user_info = _bt_get_user_data(BT_COMMON);
196         retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
197
198         BT_INIT_PARAMS();
199         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
200
201         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
202
203         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_SEARCH_SERVICE,
204                 in_param1, in_param2, in_param3, in_param4,
205                 user_info->cb, user_info->user_data);
206
207         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
208
209         return result;
210 }
211
212 BT_EXPORT_API int bluetooth_cancel_service_search(void)
213 {
214         int result;
215
216         BT_CHECK_ENABLED_ANY(return);
217
218         BT_INIT_PARAMS();
219         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
220
221         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_CANCEL_SEARCH_SERVICE,
222                 in_param1, in_param2, in_param3, in_param4, &out_param);
223
224         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
225
226         return result;
227 }
228
229 BT_EXPORT_API int bluetooth_set_alias(const bluetooth_device_address_t *device_address,
230                                       const char *alias)
231 {
232         int result;
233         char alias_name[BLUETOOTH_DEVICE_NAME_LENGTH_MAX];
234
235         BT_CHECK_PARAMETER(device_address, return);
236         BT_CHECK_PARAMETER(alias, return);
237         BT_CHECK_ENABLED_ANY(return);
238
239         BT_INIT_PARAMS();
240         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
241
242         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
243         g_strlcpy(alias_name, alias, sizeof(alias_name));
244         g_array_append_vals(in_param2, alias_name, BLUETOOTH_DEVICE_NAME_LENGTH_MAX);
245
246         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_ALIAS,
247                 in_param1, in_param2, in_param3, in_param4, &out_param);
248
249         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
250
251         return result;
252 }
253
254 BT_EXPORT_API int bluetooth_authorize_device(const bluetooth_device_address_t *device_address,
255                                              gboolean authorized)
256 {
257         int result;
258
259         BT_CHECK_PARAMETER(device_address, return);
260         BT_CHECK_ENABLED_ANY(return);
261
262         BT_INIT_PARAMS();
263         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
264
265         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
266         g_array_append_vals(in_param2, &authorized, sizeof(gboolean));
267
268         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_AUTHORIZATION,
269                 in_param1, in_param2, in_param3, in_param4, &out_param);
270
271         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
272
273         return result;
274 }
275
276 BT_EXPORT_API int bluetooth_is_device_connected(const bluetooth_device_address_t *device_address,
277                                 bluetooth_service_type_t type,
278                                 gboolean *is_connected)
279 {
280         int result;
281
282         BT_CHECK_PARAMETER(device_address, return);
283         BT_CHECK_PARAMETER(is_connected, return);
284         BT_CHECK_ENABLED_ANY(return);
285
286 #ifdef RFCOMM_DIRECT
287         if (type & BLUETOOTH_RFCOMM_SERVICE) {
288                 result = bluetooth_rfcomm_client_is_connected(device_address, is_connected);
289                 if (*is_connected == FALSE)
290                         result = bluetooth_rfcomm_server_is_connected(device_address, is_connected);
291
292                 return result;
293         }
294 #endif
295
296         BT_INIT_PARAMS();
297         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
298
299         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
300         g_array_append_vals(in_param2, &type, sizeof(int));
301
302         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_IS_DEVICE_CONNECTED,
303                 in_param1, in_param2, in_param3, in_param4, &out_param);
304
305         if (result == BLUETOOTH_ERROR_NONE) {
306                 *is_connected = g_array_index(out_param, gboolean, 0);
307         }
308
309         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
310
311         return result;
312 }
313
314 BT_EXPORT_API int bluetooth_connect_le(const bluetooth_device_address_t *device_address, gboolean auto_connect)
315 {
316         int result;
317         bt_user_info_t *user_info;
318
319         BT_CHECK_PARAMETER(device_address, return);
320         BT_CHECK_ENABLED_ANY(return);
321
322         BT_INIT_PARAMS();
323         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
324
325         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
326         g_array_append_vals(in_param2, &auto_connect, sizeof(gboolean));
327
328         user_info = _bt_get_user_data(BT_COMMON);
329         retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
330
331         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_CONNECT_LE,
332                 in_param1, in_param2, in_param3, in_param4,
333                 user_info->cb, user_info->user_data);
334
335         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
336
337         return result;
338 }
339
340 BT_EXPORT_API int bluetooth_disconnect_le(const bluetooth_device_address_t *device_address)
341 {
342         int result;
343         bt_user_info_t *user_info;
344
345         BT_CHECK_PARAMETER(device_address, return);
346         BT_CHECK_ENABLED_ANY(return);
347
348         BT_INIT_PARAMS();
349         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
350
351         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
352
353         user_info = _bt_get_user_data(BT_COMMON);
354         retv_if(user_info == NULL, BLUETOOTH_ERROR_INTERNAL);
355
356         result = _bt_send_request_async(BT_BLUEZ_SERVICE, BT_DISCONNECT_LE,
357                 in_param1, in_param2, in_param3, in_param4,
358                 user_info->cb, user_info->user_data);
359
360         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
361
362         return result;
363 }
364
365 BT_EXPORT_API int bluetooth_enable_rssi(const bluetooth_device_address_t *remote_address,
366                 int link_type, bt_rssi_threshold_t *rssi_threshold)
367 {
368         int result;
369
370         BT_CHECK_ENABLED_ANY(return);
371
372         BT_INIT_PARAMS();
373         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
374         g_array_append_vals(in_param1, remote_address, sizeof(bluetooth_device_address_t));
375         g_array_append_vals(in_param2, &link_type, sizeof(int));
376         g_array_append_vals(in_param3, rssi_threshold, sizeof(bt_rssi_threshold_t));
377
378         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_ENABLE_RSSI,
379                 in_param1, in_param2, in_param3, in_param4, &out_param);
380
381         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
382
383         return result;
384 }
385
386 BT_EXPORT_API int bluetooth_get_rssi_strength(const bluetooth_device_address_t *remote_address, int link_type)
387 {
388         int result;
389
390         BT_CHECK_ENABLED_ANY(return);
391
392         BT_INIT_PARAMS();
393         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
394         g_array_append_vals(in_param1, remote_address, sizeof(bluetooth_device_address_t));
395         g_array_append_vals(in_param2, &link_type, sizeof(int));
396
397         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_RSSI,
398                 in_param1, in_param2, in_param3, in_param4, &out_param);
399
400         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
401
402         return result;
403 }
404
405 BT_EXPORT_API int bluetooth_le_conn_update(const bluetooth_device_address_t *address,
406                                         const bluetooth_le_connection_param_t *parameters)
407 {
408         int result;
409
410         BT_CHECK_ENABLED_ANY(return);
411         BT_CHECK_PARAMETER(address, return);
412         BT_CHECK_PARAMETER(parameters, return);
413
414         BT_INIT_PARAMS();
415         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
416
417         g_array_append_vals(in_param1, address,
418                         sizeof(bluetooth_device_address_t));
419         g_array_append_vals(in_param2, parameters,
420                         sizeof(bluetooth_le_connection_param_t));
421
422         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_LE_CONN_UPDATE,
423                         in_param1, in_param2, in_param3, in_param4, &out_param);
424
425         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
426
427         return result;
428 }
429
430 BT_EXPORT_API int bluetooth_get_connected_link_type(
431                 const bluetooth_device_address_t *device_address,
432                 bluetooth_connected_link_t *connected)
433 {
434         int result;
435
436         BT_CHECK_PARAMETER(device_address, return);
437         BT_CHECK_ENABLED_ANY(return);
438
439         BT_INIT_PARAMS();
440         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
441
442         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
443
444         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_GET_CONNECTED_LINK_TYPE,
445                         in_param1, in_param2, in_param3, in_param4, &out_param);
446
447         if (result == BLUETOOTH_ERROR_NONE) {
448                 *connected = g_array_index(out_param, guint, 0);
449         }
450
451         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
452
453         return result;
454 }
455
456 BT_EXPORT_API int bluetooth_set_pin_code(
457                 const bluetooth_device_address_t *device_address,
458                 const bluetooth_device_pin_code_t *pin_code)
459 {
460         int result;
461
462         BT_CHECK_PARAMETER(device_address, return);
463         BT_CHECK_PARAMETER(pin_code, return);
464         BT_CHECK_ENABLED(return);
465
466         BT_INIT_PARAMS();
467         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
468
469         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
470         g_array_append_vals(in_param2, pin_code, sizeof(bluetooth_device_pin_code_t));
471
472         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_SET_PIN_CODE,
473                 in_param1, in_param2, in_param3, in_param4, &out_param);
474
475         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
476
477         return result;
478 }
479
480 BT_EXPORT_API int bluetooth_unset_pin_code(
481                 const bluetooth_device_address_t *device_address)
482 {
483         int result;
484
485         BT_CHECK_PARAMETER(device_address, return);
486         BT_CHECK_ENABLED(return);
487
488         BT_INIT_PARAMS();
489         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
490
491         g_array_append_vals(in_param1, device_address, sizeof(bluetooth_device_address_t));
492
493         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UNSET_PIN_CODE,
494                 in_param1, in_param2, in_param3, in_param4, &out_param);
495
496         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
497
498         return result;
499 }
500
501 BT_EXPORT_API int bluetooth_update_le_connection_mode(const bluetooth_device_address_t *address,
502                 const bluetooth_le_connection_mode_t mode)
503 {
504         int result;
505
506         BT_CHECK_ENABLED(return);
507         BT_CHECK_PARAMETER(address, return);
508
509         BT_INIT_PARAMS();
510         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
511
512         g_array_append_vals(in_param1, address,
513                         sizeof(bluetooth_device_address_t));
514         g_array_append_vals(in_param2, &mode,
515                         sizeof(bluetooth_le_connection_mode_t));
516
517         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_UPDATE_LE_CONNECTION_MODE,
518                         in_param1, in_param2, in_param3, in_param4, &out_param);
519
520         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
521
522         return result;
523 }
524
525 BT_EXPORT_API int bluetooth_passkey_reply(char *passkey, gboolean reply)
526 {
527         int result;
528
529         char str_passkey[BLUETOOTH_DEVICE_PASSKEY_LENGTH_MAX];
530
531         BT_CHECK_PARAMETER(passkey, return);
532         BT_CHECK_ENABLED(return);
533
534         BT_INIT_PARAMS();
535         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
536
537         g_strlcpy(str_passkey, passkey, sizeof(str_passkey));
538         g_array_append_vals(in_param1, str_passkey, BLUETOOTH_DEVICE_PASSKEY_LENGTH_MAX);
539         g_array_append_vals(in_param2, &reply, sizeof(gboolean));
540
541         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_PASSKEY_REPLY,
542                 in_param1, in_param2, in_param3, in_param4, &out_param);
543
544         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
545
546         return result;
547 }
548
549 BT_EXPORT_API int bluetooth_passkey_confirmation_reply(gboolean reply)
550 {
551         int result;
552
553         BT_CHECK_ENABLED(return);
554         BT_INIT_PARAMS();
555
556         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
557         g_array_append_vals(in_param1, &reply, sizeof(gboolean));
558         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_PASSKEY_CONFIRMATION_REPLY,
559                         in_param1, in_param2, in_param3, in_param4, &out_param);
560         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
561
562         return result;
563 }