2 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <bluetooth-api.h>
21 #include "bluetooth.h"
22 #include "bluetooth_private.h"
23 #include "bluetooth-audio-api.h"
24 #include "bluetooth-telephony-api.h"
26 typedef struct _call_list_s {
30 /*The below API is just to convert the error from Telephony API's to CAPI error codes,
31 * this is temporary change and changes to proper error code will be done in
32 * subsequent check ins.*/
33 int _bt_convert_telephony_error_code(int error)
36 case BLUETOOTH_TELEPHONY_ERROR_NONE:
38 case BLUETOOTH_TELEPHONY_ERROR_INVALID_PARAM:
39 return BT_ERROR_INVALID_PARAMETER;
40 case BLUETOOTH_TELEPHONY_ERROR_NOT_INITIALIZED:
41 return BT_ERROR_NOT_INITIALIZED;
42 case BLUETOOTH_TELEPHONY_ERROR_NOT_ENABLED:
43 return BT_ERROR_NOT_ENABLED;
44 case BLUETOOTH_TELEPHONY_ERROR_AUDIO_NOT_CONNECTED:
45 case BLUETOOTH_TELEPHONY_ERROR_NOT_CONNECTED:
46 return BT_ERROR_REMOTE_DEVICE_NOT_CONNECTED;
47 case BLUETOOTH_TELEPHONY_ERROR_BUSY:
48 return BT_ERROR_RESOURCE_BUSY;
49 case BLUETOOTH_TELEPHONY_ERROR_NO_MEMORY:
50 return BT_ERROR_OUT_OF_MEMORY;
51 case BLUETOOTH_TELEPHONY_ERROR_ALREADY_INITIALIZED:
52 case BLUETOOTH_TELEPHONY_ERROR_ALREADY_EXSIST:
53 case BLUETOOTH_TELEPHONY_ERROR_ALREADY_CONNECTED:
54 return BT_ERROR_ALREADY_DONE;
55 case BLUETOOTH_TELEPHONY_ERROR_INTERNAL:
56 case BLUETOOTH_TELEPHONY_ERROR_NOT_AVAILABLE:
57 case BLUETOOTH_TELEPHONY_ERROR_I_O_ERROR:
58 case BLUETOOTH_TELEPHONY_ERROR_OPERATION_NOT_AVAILABLE:
59 return BT_ERROR_OPERATION_FAILED;
65 int bt_audio_initialize(void)
69 BT_CHECK_INIT_STATUS();
70 error = bluetooth_audio_init(_bt_audio_event_proxy, NULL);
71 error = _bt_get_error_code(error);
72 if (BT_ERROR_NONE != error) {
73 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
76 error = bluetooth_telephony_init((void *)_bt_telephony_event_proxy, NULL);
77 error = _bt_convert_telephony_error_code(error);
78 error = _bt_get_error_code(error);
79 if (BT_ERROR_NONE != error) {
81 _bt_convert_error_to_string(error), error);
86 int bt_audio_deinitialize(void)
90 BT_CHECK_INIT_STATUS();
91 error = bluetooth_audio_deinit();
92 error = _bt_get_error_code(error);
93 if (BT_ERROR_NONE != error) {
94 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
97 error = bluetooth_telephony_deinit();
98 error = _bt_convert_telephony_error_code(error);
99 error = _bt_get_error_code(error);
100 if (BT_ERROR_NONE != error) {
101 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
106 int bt_audio_connect(const char *remote_address, bt_audio_profile_type_e type)
109 bluetooth_device_address_t addr_hex = { {0,} };
111 BT_CHECK_INIT_STATUS();
112 BT_CHECK_INPUT_PARAMETER(remote_address);
113 _bt_convert_address_to_hex(&addr_hex, remote_address);
115 case BT_AUDIO_PROFILE_TYPE_HSP_HFP:
116 error = bluetooth_ag_connect(&addr_hex);
118 case BT_AUDIO_PROFILE_TYPE_A2DP:
119 error = bluetooth_av_connect(&addr_hex);
121 case BT_AUDIO_PROFILE_TYPE_ALL:
123 error = bluetooth_audio_connect(&addr_hex);
126 error = _bt_get_error_code(error);
127 if (error != BT_ERROR_NONE) {
128 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
133 int bt_audio_disconnect(const char *remote_address, bt_audio_profile_type_e type)
136 bluetooth_device_address_t addr_hex = { {0,} };
138 BT_CHECK_INIT_STATUS();
139 BT_CHECK_INPUT_PARAMETER(remote_address);
140 _bt_convert_address_to_hex(&addr_hex, remote_address);
142 case BT_AUDIO_PROFILE_TYPE_HSP_HFP:
143 error = bluetooth_ag_disconnect(&addr_hex);
145 case BT_AUDIO_PROFILE_TYPE_A2DP:
146 error = bluetooth_av_disconnect(&addr_hex);
148 case BT_AUDIO_PROFILE_TYPE_ALL:
150 error = bluetooth_audio_disconnect(&addr_hex);
153 error = _bt_get_error_code(error);
154 if (error != BT_ERROR_NONE) {
155 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
160 int bt_audio_set_connection_state_changed_cb(bt_audio_connection_state_changed_cb callback, void *user_data)
162 BT_CHECK_INIT_STATUS();
163 BT_CHECK_INPUT_PARAMETER(callback);
164 _bt_set_cb(BT_EVENT_AUDIO_CONNECTION_STATUS, callback, user_data);
165 return BT_ERROR_NONE;
168 int bt_audio_unset_connection_state_changed_cb(void)
170 BT_CHECK_INIT_STATUS();
171 if (_bt_check_cb(BT_EVENT_AUDIO_CONNECTION_STATUS) == true)
172 _bt_unset_cb(BT_EVENT_AUDIO_CONNECTION_STATUS);
173 return BT_ERROR_NONE;
176 int bt_ag_notify_speaker_gain(int gain)
180 BT_CHECK_INIT_STATUS();
181 error = bluetooth_telephony_set_speaker_gain((unsigned short)gain);
182 error = _bt_get_error_code(error);
183 if (BT_ERROR_NONE != error) {
184 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
189 int bt_ag_get_speaker_gain(int *gain)
193 BT_CHECK_INIT_STATUS();
194 BT_CHECK_INPUT_PARAMETER(gain);
195 error = bluetooth_telephony_get_headset_volume((unsigned int *)gain);
196 error = _bt_get_error_code(error);
197 if (BT_ERROR_NONE != error) {
198 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
203 int bt_ag_is_nrec_enabled(bool *enabled)
207 BT_CHECK_INIT_STATUS();
208 BT_CHECK_INPUT_PARAMETER(enabled);
210 error = bluetooth_telephony_is_nrec_enabled((gboolean *)enabled);
211 error = _bt_get_error_code(error);
212 if (BT_ERROR_NONE != error) {
213 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
219 int bt_ag_set_microphone_gain_changed_cb(bt_ag_microphone_gain_changed_cb callback, void *user_data)
221 BT_CHECK_INIT_STATUS();
222 BT_CHECK_INPUT_PARAMETER(callback);
223 _bt_set_cb(BT_EVENT_AG_MICROPHONE_GAIN_CHANGE, callback, user_data);
224 return BT_ERROR_NONE;
228 int bt_ag_unset_microphone_gain_changed_cb(void)
230 BT_CHECK_INIT_STATUS();
231 if (_bt_check_cb(BT_EVENT_AG_MICROPHONE_GAIN_CHANGE) == true)
232 _bt_unset_cb(BT_EVENT_AG_MICROPHONE_GAIN_CHANGE);
233 return BT_ERROR_NONE;
236 int bt_ag_set_speaker_gain_changed_cb(bt_ag_speaker_gain_changed_cb callback,
239 BT_CHECK_INIT_STATUS();
240 BT_CHECK_INPUT_PARAMETER(callback);
241 _bt_set_cb(BT_EVENT_AG_SPEAKER_GAIN_CHANGE, callback, user_data);
242 return BT_ERROR_NONE;
245 int bt_ag_unset_speaker_gain_changed_cb(void)
247 BT_CHECK_INIT_STATUS();
248 if (_bt_check_cb(BT_EVENT_AG_SPEAKER_GAIN_CHANGE) == true)
249 _bt_unset_cb(BT_EVENT_AG_SPEAKER_GAIN_CHANGE);
250 return BT_ERROR_NONE;
253 int bt_ag_open_sco(void)
257 BT_CHECK_INIT_STATUS();
258 error = bluetooth_telephony_audio_open();
259 error = _bt_convert_telephony_error_code(error);
260 error = _bt_get_error_code(error);
261 if (error != BT_ERROR_NONE) {
262 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
267 int bt_ag_close_sco(void)
271 BT_CHECK_INIT_STATUS();
272 error = bluetooth_telephony_audio_close();
273 error = _bt_convert_telephony_error_code(error);
274 error = _bt_get_error_code(error);
275 if (error != BT_ERROR_NONE) {
276 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
281 int bt_ag_is_sco_opened(bool *opened)
283 BT_CHECK_INIT_STATUS();
284 BT_CHECK_INPUT_PARAMETER(opened);
285 *opened = bluetooth_telephony_is_sco_connected();
286 return BT_ERROR_NONE;
289 int bt_ag_set_sco_state_changed_cb(bt_ag_sco_state_changed_cb callback,
292 BT_CHECK_INIT_STATUS();
293 BT_CHECK_INPUT_PARAMETER(callback);
294 _bt_set_cb(BT_EVENT_AG_SCO_CONNECTION_STATUS, callback, user_data);
295 return BT_ERROR_NONE;
298 int bt_ag_unset_sco_state_changed_cb(void)
300 BT_CHECK_INIT_STATUS();
301 if (_bt_check_cb(BT_EVENT_AG_SCO_CONNECTION_STATUS) == true)
302 _bt_unset_cb(BT_EVENT_AG_SCO_CONNECTION_STATUS);
303 return BT_ERROR_NONE;
306 int bt_ag_notify_call_event(bt_ag_call_event_e event, unsigned int call_id, const char *phone_number)
310 BT_CHECK_INIT_STATUS();
312 case BT_AG_CALL_EVENT_IDLE:
313 error = bluetooth_telephony_call_end(call_id);
315 case BT_AG_CALL_EVENT_ANSWERED:
316 error = bluetooth_telephony_call_answered(call_id, FALSE);
318 case BT_AG_CALL_EVENT_HELD:
319 error = bluetooth_telephony_call_held(call_id);
321 case BT_AG_CALL_EVENT_RETRIEVED:
322 error = bluetooth_telephony_call_retrieved(call_id);
324 case BT_AG_CALL_EVENT_DIALING:
325 BT_CHECK_INPUT_PARAMETER(phone_number);
326 error = bluetooth_telephony_indicate_outgoing_call(
327 phone_number, call_id, FALSE);
329 case BT_AG_CALL_EVENT_ALERTING:
330 error = bluetooth_telephony_call_remote_ringing(call_id);
332 case BT_AG_CALL_EVENT_INCOMING:
333 BT_CHECK_INPUT_PARAMETER(phone_number);
334 error = bluetooth_telephony_indicate_incoming_call(phone_number,
338 error = BT_ERROR_INVALID_PARAMETER;
340 error = _bt_convert_telephony_error_code(error);
341 error = _bt_get_error_code(error);
342 if (error != BT_ERROR_NONE) {
343 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
348 int bt_ag_notify_call_list(bt_call_list_h list)
351 unsigned int call_count;
354 BT_CHECK_INIT_STATUS();
355 BT_CHECK_INPUT_PARAMETER(list);
356 handle = (call_list_s *)list;
357 call_count = g_list_length(handle->list);
358 error = bluetooth_telephony_set_call_status((void *)handle->list, call_count);
359 error = _bt_convert_telephony_error_code(error);
360 error = _bt_get_error_code(error);
361 if (error != BT_ERROR_NONE) {
362 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
367 int bt_ag_notify_voice_recognition_state(bool state)
371 BT_CHECK_INIT_STATUS();
373 error = bluetooth_telephony_start_voice_recognition();
375 error = bluetooth_telephony_stop_voice_recognition();
376 error = _bt_convert_telephony_error_code(error);
377 error = _bt_get_error_code(error);
378 if (error != BT_ERROR_NONE) {
379 BT_ERR("%s(0x%08x)", _bt_convert_error_to_string(error), error);
384 int bt_ag_set_call_handling_event_cb(bt_ag_call_handling_event_cb callback,
387 BT_CHECK_INIT_STATUS();
388 BT_CHECK_INPUT_PARAMETER(callback);
389 _bt_set_cb(BT_EVENT_AG_CALL_HANDLING_EVENT, callback, user_data);
390 return BT_ERROR_NONE;
394 int bt_ag_unset_call_handling_event_cb(void)
396 BT_CHECK_INIT_STATUS();
397 if (_bt_check_cb(BT_EVENT_AG_CALL_HANDLING_EVENT) == true)
398 _bt_unset_cb(BT_EVENT_AG_CALL_HANDLING_EVENT);
399 return BT_ERROR_NONE;
402 int bt_ag_set_multi_call_handling_event_cb(
403 bt_ag_multi_call_handling_event_cb callback,
406 BT_CHECK_INIT_STATUS();
407 BT_CHECK_INPUT_PARAMETER(callback);
408 _bt_set_cb(BT_EVENT_AG_MULTI_CALL_HANDLING_EVENT, callback, user_data);
409 return BT_ERROR_NONE;
412 int bt_ag_unset_multi_call_handling_event_cb(void)
414 BT_CHECK_INIT_STATUS();
415 if (_bt_check_cb(BT_EVENT_AG_MULTI_CALL_HANDLING_EVENT) == true)
416 _bt_unset_cb(BT_EVENT_AG_MULTI_CALL_HANDLING_EVENT);
417 return BT_ERROR_NONE;
420 int bt_ag_set_dtmf_transmitted_cb(bt_ag_dtmf_transmitted_cb callback,
423 BT_CHECK_INIT_STATUS();
424 BT_CHECK_INPUT_PARAMETER(callback);
425 _bt_set_cb(BT_EVENT_AG_DTMF_TRANSMITTED, callback, user_data);
426 return BT_ERROR_NONE;
429 int bt_ag_unset_dtmf_transmitted_cb(void)
431 BT_CHECK_INIT_STATUS();
432 if (_bt_check_cb(BT_EVENT_AG_DTMF_TRANSMITTED) == true)
433 _bt_unset_cb(BT_EVENT_AG_DTMF_TRANSMITTED);
434 return BT_ERROR_NONE;
437 int bt_call_list_create(bt_call_list_h *list)
441 BT_CHECK_INIT_STATUS();
443 BT_ERR("BT_ERROR_ALREADY_DONE(0x%08x)", BT_ERROR_ALREADY_DONE);
444 return BT_ERROR_ALREADY_DONE;
446 handle = g_malloc0(sizeof(call_list_s));
448 return BT_ERROR_NONE;
451 int bt_call_list_destroy(bt_call_list_h list)
456 BT_CHECK_INIT_STATUS();
457 BT_CHECK_INPUT_PARAMETER(list);
458 handle = (call_list_s *)list;
459 result = bt_call_list_reset(list);
464 int bt_call_list_reset(bt_call_list_h list)
467 bt_telephony_call_status_info_t *call_status;
469 BT_CHECK_INIT_STATUS();
470 BT_CHECK_INPUT_PARAMETER(list);
471 handle = (call_list_s *)list;
473 call_status = (bt_telephony_call_status_info_t *)g_list_nth_data(handle->list, 0);
474 if (call_status == NULL)
476 handle->list = g_list_remove(handle->list, call_status);
479 return BT_ERROR_NONE;
482 int bt_call_list_add(bt_call_list_h list, unsigned int call_id, bt_ag_call_state_e state)
485 bt_telephony_call_status_info_t *call_status;
487 BT_CHECK_INIT_STATUS();
488 BT_CHECK_INPUT_PARAMETER(list);
489 handle = (call_list_s *)list;
490 call_status = g_malloc0(sizeof(bt_telephony_call_status_info_t));
491 call_status->call_id = call_id;
492 call_status->call_status = state;
493 handle->list = g_list_append(handle->list, (gpointer)call_status);
494 return BT_ERROR_NONE;