4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Chethan T N <chethan.tn@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
31 #include <sys/socket.h>
35 #include <gio/gunixfdlist.h>
36 #include <device/power.h>
37 #include <app_manager.h>
39 #include <vconf-keys.h>
40 #include <bundle_internal.h>
42 #include "bluetooth-hf-agent.h"
44 #define BT_AGENT_SYSPOPUP_MAX_ATTEMPT 3
45 #define CALL_APP_ID "org.tizen.call-ui"
47 #define MAX_WAITING_DELAY 8
48 #define READ_TX_POWER_MIN -30
50 #define BT_ADDRESS_STRING_SIZE 18
51 #define BT_AT_COMMAND_BUFFER_MAX 4000
52 #define BT_HF_ERROR_RESP "\r\nERROR\r\n"
53 #define BT_HF_COMMAND_TIMEOUT 3
55 #define ret_if(expr) \
58 ERR("(%s) return", #expr); \
63 static GMainLoop *gmain_loop = NULL;
64 static char *g_obj_path;
66 static GDBusConnection *gdbus_conn;
67 static GDBusProxy *service_gproxy;
68 static int owner_sig_id = -1;
72 #define HFP_HF_UUID "0000111e-0000-1000-8000-00805f9b34fb"
73 #define DEFAULT_ADAPTER_OBJECT_PATH "/org/bluez/hci0"
75 /*Below Inrospection data is exposed to bluez from agent*/
76 static const gchar hf_agent_bluez_introspection_xml[] =
78 " <interface name='org.bluez.Profile1'>"
79 " <method name='NewConnection'>"
80 " <arg type='o' name='device' direction='in'/>"
81 " <arg type='h' name='fd' direction='in'/>"
82 " <arg type='a{sv}' name='options' direction='in'/>"
84 " <method name='RequestDisconnection'>"
85 " <arg type='o' name='device' direction='in'/>"
90 /*Below Inrospection data is exposed to application from agent*/
91 static const gchar hf_agent_introspection_xml[] =
93 " <interface name='org.tizen.HfApp'>"
94 " <method name='AnswerCall'>"
96 " <method name='TerminateCall'>"
98 " <method name='InitiateCall'>"
99 " <arg type='s' name='phoneno' direction='in'/>"
101 " <method name='VoiceRecognition'>"
102 " <arg type='i' name='status' direction='in'/>"
104 " <method name='ScoDisconnect'>"
106 " <method name='SpeakerGain'>"
107 " <arg type='u' name='gain' direction='in'/>"
109 " <method name='SendDtmf'>"
110 " <arg type='s' name='dtmf' direction='in'/>"
112 " <method name='SendAtCmd'>"
113 " <arg type='s' name='atcmd' direction='in'/>"
115 " <method name='ReleaseAndAccept'>"
117 " <method name='CallSwap'>"
119 " <method name='ReleaseAllCall'>"
121 " <method name='JoinCall'>"
123 " <method name='GetCurrentCodec'>"
124 " <arg type='i' name='codec' direction='out'/>"
126 " <method name='RequestCallList'>"
127 " <arg type='i' name='count' direction='out'/>"
128 " <arg type='a(siiii)' name='callList' direction='out'/>"
130 " <method name='GetAudioConnected'>"
131 " <arg type='i' name='status' direction='out'/>"
133 " <method name='IsHfConnected'>"
134 " <arg type='b' name='status' direction='out'/>"
139 static bt_hf_agent_info_t bt_hf_info;
140 static gboolean is_hf_connected = FALSE;
141 static int32_t current_codec_id = BT_HF_CODEC_ID_CVSD;
142 static int32_t sco_audio_connected = BT_HF_AUDIO_DISCONNECTED;
144 static char global_buff[BT_AT_COMMAND_BUFFER_MAX] = {0,};
147 static char prev_cmd[BT_HF_CMD_BUF_SIZE];
157 } hf_call_list_info_t;
159 static GError *__bt_hf_agent_set_error(bt_hf_agent_error_t error);
161 static gboolean __bt_hf_agent_emit_property_changed(
162 GDBusConnection *connection,
164 const char *interface,
168 static gboolean __bt_hf_agent_data_cb(GIOChannel *chan, GIOCondition cond,
169 bt_hf_agent_info_t *bt_hf_info);
170 static void __bt_hf_agent_stop_watch(bt_hf_agent_info_t *bt_hf_info);
171 static void __bt_hf_agent_start_watch(bt_hf_agent_info_t *bt_hf_info);
172 static gboolean __bt_hf_channel_write(GIOChannel *io, gchar *data,
174 static gboolean __bt_hf_send_only_without_queue(bt_hf_agent_info_t *bt_hf_info,
175 gchar *data, gsize count);
177 static gboolean __bt_hf_send_only(bt_hf_agent_info_t *bt_hf_info, gchar *data,
179 static gboolean __bt_hf_send_and_read(bt_hf_agent_info_t *bt_hf_info,
180 gchar *data, gchar *response, gsize count);
181 static GSList *__bt_hf_parse_indicator_names(gchar *names, GSList *indies);
182 static GSList *__bt_hf_parse_indicator_values(gchar *values, GSList *indies);
183 static guint __bt_hf_get_hold_mpty_features(gchar *features);
184 static gboolean __bt_establish_service_level_conn(bt_hf_agent_info_t *bt_hf_info);
185 static void __bt_hf_agent_sigterm_handler(int signo);
186 static gboolean __bt_hf_agent_release(void);
188 static gboolean __bt_get_current_indicators(bt_hf_agent_info_t *bt_hf_info);
189 static gboolean __bt_get_supported_indicators(bt_hf_agent_info_t *bt_hf_info);
190 static gboolean __bt_hf_agent_connection(gint32 fd, const gchar * object_path);
191 static gboolean __bt_hf_agent_connection_release(void);
194 gchar descr[BT_HF_INDICATOR_DESCR_SIZE];
198 static int _hf_agent_answer_call(GDBusMethodInvocation *context);
200 static int _hf_agent_terminate_call(GDBusMethodInvocation *context);
202 static int _hf_agent_dial_no(GDBusMethodInvocation *context, char *no);
204 static int _hf_agent_set_speaker_gain(GDBusMethodInvocation *context,
207 static int _hf_agent_send_3way_cmd(GDBusMethodInvocation *context, char *cmd);
209 static int _hf_agent_voice_recognition(GDBusMethodInvocation *context,
210 unsigned int status);
212 static gboolean bt_hf_agent_sco_disconnect(void);
214 static int _hf_agent_send_dtmf(GDBusMethodInvocation *context, char *dtmf);
216 static GVariant *bt_hf_agent_request_call_list(void);
218 static int bt_hf_agent_send_at_cmd(GDBusMethodInvocation *context, char *atcmd);
220 static int hf_handle_rx_at_cmd(bt_hf_agent_info_t *bt_hf_info, const char *buf);
221 static GQuark __bt_hf_agent_error_quark(void)
225 static GQuark quark = 0;
227 quark = g_quark_from_static_string("hf-agent");
232 static GError *__bt_hf_agent_set_error(bt_hf_agent_error_t error)
234 ERR("error[%d]", error);
237 case BT_HF_AGENT_ERROR_NOT_AVAILABLE:
238 return g_error_new(BT_HF_AGENT_ERROR, error,
239 BT_ERROR_NOT_AVAILABLE);
240 case BT_HF_AGENT_ERROR_NOT_CONNECTED:
241 return g_error_new(BT_HF_AGENT_ERROR, error,
242 BT_ERROR_NOT_CONNECTED);
243 case BT_HF_AGENT_ERROR_CONNECTION_FAILED:
244 return g_error_new(BT_HF_AGENT_ERROR, error,
245 BT_ERROR_NOT_CONNECTION_FAILED);
246 case BT_HF_AGENT_ERROR_BUSY:
247 return g_error_new(BT_HF_AGENT_ERROR, error,
249 case BT_HF_AGENT_ERROR_INVALID_PARAM:
250 return g_error_new(BT_HF_AGENT_ERROR, error,
251 BT_ERROR_INVALID_PARAM);
252 case BT_HF_AGENT_ERROR_ALREADY_EXIST:
253 return g_error_new(BT_HF_AGENT_ERROR, error,
254 BT_ERROR_ALREADY_EXIST);
255 case BT_HF_AGENT_ERROR_ALREADY_CONNECTED:
256 return g_error_new(BT_HF_AGENT_ERROR, error,
257 BT_ERROR_ALREADY_CONNECTED);
258 case BT_HF_AGENT_ERROR_NO_MEMORY:
259 return g_error_new(BT_HF_AGENT_ERROR, error,
261 case BT_HF_AGENT_ERROR_I_O_ERROR:
262 return g_error_new(BT_HF_AGENT_ERROR, error,
264 case BT_HF_AGENT_ERROR_APPLICATION:
265 return g_error_new(BT_HF_AGENT_ERROR, error,
266 BT_ERROR_OPERATION_NOT_AVAILABLE);
267 case BT_HF_AGENT_ERROR_NOT_ALLOWED:
268 return g_error_new(BT_HF_AGENT_ERROR, error,
269 BT_ERROR_OPERATION_NOT_ALLOWED);
270 case BT_HF_AGENT_ERROR_NOT_SUPPORTED:
271 return g_error_new(BT_HF_AGENT_ERROR, error,
272 BT_ERROR_OPERATION_NOT_SUPPORTED);
273 case BT_HF_AGENT_ERROR_INVALID_FILE_DESCRIPTOR:
274 return g_error_new(BT_HF_AGENT_ERROR, error,
275 BT_ERROR_INVALID_FILE_DESCRIPTOR);
276 case BT_HF_AGENT_ERROR_INTERNAL:
278 return g_error_new(BT_HF_AGENT_ERROR, error,
283 static void __bt_hf_lock_display(int timeout)
287 ret = device_power_request_lock(POWER_LOCK_DISPLAY, timeout);
289 DBG("Lock PM state as current state!");
291 ERR("deviced error!");
294 static void __bt_hf_unlock_display()
298 ret = device_power_release_lock(POWER_LOCK_DISPLAY);
300 DBG("UnLock PM state");
302 ERR("deviced error!");
305 static void __hf_agent_method(GDBusConnection *connection,
307 const gchar *object_path,
308 const gchar *interface_name,
309 const gchar *method_name,
310 GVariant *parameters,
311 GDBusMethodInvocation *context,
316 INFO("method %s", method_name);
320 if (g_strcmp0(method_name, "NewConnection") == 0) {
324 GUnixFDList *fd_list;
325 const gchar *object_path;
328 g_variant_get(parameters, "(oha{sv})",
329 &object_path, &index, &options);
331 msg = g_dbus_method_invocation_get_message(context);
332 fd_list = g_dbus_message_get_unix_fd_list(msg);
333 if (fd_list == NULL) {
334 ret = BT_HF_AGENT_ERROR_INVALID_FILE_DESCRIPTOR;
338 fd = g_unix_fd_list_get(fd_list, index, NULL);
340 ret = BT_HF_AGENT_ERROR_INVALID_FILE_DESCRIPTOR;
344 DBG("FD is = [%d], Object path = [%s]", fd, object_path);
346 if (!__bt_hf_agent_connection(fd, object_path)) {
347 ret = BT_HF_AGENT_ERROR_INTERNAL;
351 g_dbus_method_invocation_return_value(context, NULL);
352 } else if (g_strcmp0(method_name, "RequestDisconnection") == 0) {
353 if (!__bt_hf_agent_connection_release()) {
354 ret = BT_HF_AGENT_ERROR_INTERNAL;
357 INFO_C("Disconnected [HF role] [Terminated by local host]");
358 g_dbus_method_invocation_return_value(context, NULL);
359 } else if (g_strcmp0(method_name, "Release") == 0) {
360 if (!__bt_hf_agent_connection_release()) {
361 ret = BT_HF_AGENT_ERROR_INTERNAL;
365 g_dbus_method_invocation_return_value(context, NULL);
366 } else if (g_strcmp0(method_name, "AnswerCall") == 0) {
367 DBG("Going to call AnswerCall");
368 ret = _hf_agent_answer_call(context);
372 } else if (g_strcmp0(method_name, "TerminateCall") == 0) {
373 DBG("Going to call TerminateCall");
374 ret = _hf_agent_terminate_call(context);
378 } else if (g_strcmp0(method_name, "InitiateCall") == 0) {
379 char *phoneno = NULL;
381 g_variant_get(parameters, "(&s)", &phoneno);
383 DBG_SECURE("Going to call InitiateCall, Number is = [%s]\n", phoneno);
384 ret = _hf_agent_dial_no(NULL, phoneno);
388 g_dbus_method_invocation_return_value(context, NULL);
390 } else if (g_strcmp0(method_name, "VoiceRecognition") == 0) {
393 g_variant_get(parameters, "(i)", &status);
395 DBG("Going to call VoiceRecognition, Status [%d]", status);
396 ret = _hf_agent_voice_recognition(context, status);
400 } else if (g_strcmp0(method_name, "ScoDisconnect") == 0) {
401 DBG("Going to call ScoDisconnect");
402 if (!bt_hf_agent_sco_disconnect()) {
403 ret = BT_HF_AGENT_ERROR_INTERNAL;
407 g_dbus_method_invocation_return_value(context, NULL);
408 } else if (g_strcmp0(method_name, "SpeakerGain") == 0) {
409 unsigned int gain = 0;
411 g_variant_get(parameters, "(u)", &gain);
413 DBG("Going to call SpeakerGain, gain is = [%d]\n", gain);
414 ret = _hf_agent_set_speaker_gain(context, gain);
418 } else if (g_strcmp0(method_name, "SendDtmf") == 0) {
421 g_variant_get(parameters, "(&s)", &dtmf);
423 DBG("Going to call SendDtmf, dtmf is = [%s]\n", dtmf);
424 ret = _hf_agent_send_dtmf(NULL, dtmf);
427 g_dbus_method_invocation_return_value(context, NULL);
429 } else if (g_strcmp0(method_name, "SendAtCmd") == 0) {
432 g_variant_get(parameters, "(&s)", &cmd);
434 DBG("Going to call SendAtCmd, cmd is = [%s]\n", cmd);
435 ret = bt_hf_agent_send_at_cmd(context, cmd);
439 } else if (g_strcmp0(method_name, "ReleaseAndAccept") == 0) {
440 DBG("Going to call ReleaseAndAccept");
441 ret = _hf_agent_send_3way_cmd(context,
442 BT_HF_RELEASE_AND_ACCEPT);
446 } else if (g_strcmp0(method_name, "CallSwap") == 0) {
447 DBG("Going to call CallSwap");
448 ret = _hf_agent_send_3way_cmd(context,
449 BT_HF_ACCEPT_AND_HOLD);
453 } else if (g_strcmp0(method_name, "ReleaseAllCall") == 0) {
454 DBG("Going to call ReleaseAllCall");
455 ret = _hf_agent_send_3way_cmd(context, BT_HF_RELEASE_ALL);
459 } else if (g_strcmp0(method_name, "JoinCall") == 0) {
460 DBG("Going to call JoinCall");
461 ret = _hf_agent_send_3way_cmd(context, BT_HF_JOIN_CALL);
465 } else if (g_strcmp0(method_name, "GetCurrentCodec") == 0) {
466 DBG("Going to call GetCurrentCodec");
467 INFO("Current codec : %d", current_codec_id);
468 g_dbus_method_invocation_return_value(context,
469 g_variant_new("(i)", current_codec_id));
470 } else if (g_strcmp0(method_name, "RequestCallList") == 0) {
473 DBG("Going to call RequestCallList");
474 call_var = bt_hf_agent_request_call_list();
476 ret = BT_HF_AGENT_ERROR_NOT_AVAILABLE;
479 g_dbus_method_invocation_return_value(context, call_var);
480 } else if (g_strcmp0(method_name, "GetAudioConnected") == 0) {
481 DBG("Going to call GetAudioConnected");
482 g_dbus_method_invocation_return_value(context,
483 g_variant_new("(i)", sco_audio_connected));
484 } else if (g_strcmp0(method_name, "IsHfConnected") == 0) {
485 DBG("Going to call IsHfConnected");
486 INFO("is_hf_connected : %s", is_hf_connected ? "Connected":"Disconnected");
488 g_dbus_method_invocation_return_value(context,
489 g_variant_new("(b)", is_hf_connected));
495 err = __bt_hf_agent_set_error(ret);
496 g_dbus_method_invocation_return_gerror(context, err);
501 static const GDBusInterfaceVTable method_table = {
507 static GDBusNodeInfo *__bt_hf_create_method_node_info
508 (const gchar *introspection_data)
510 if (introspection_data == NULL)
513 return g_dbus_node_info_new_for_xml(introspection_data, NULL);
516 static GDBusConnection *__bt_hf_get_gdbus_connection(void)
518 GDBusConnection *local_system_gconn = NULL;
521 if (gdbus_conn == NULL) {
522 gdbus_conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
525 ERR("Unable to connect to dbus: %s", err->message);
530 } else if (g_dbus_connection_is_closed(gdbus_conn)) {
531 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
533 if (!local_system_gconn) {
534 ERR("Unable to connect to dbus: %s", err->message);
538 gdbus_conn = local_system_gconn;
544 static gboolean __bt_hf_register_profile_methods(void)
547 GError *error = NULL;
550 GDBusNodeInfo *node_info;
552 GDBusConnection *conn;
554 owner_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
556 G_BUS_NAME_OWNER_FLAGS_NONE,
560 DBG("owner_id is [%d]", owner_id);
562 node_info = __bt_hf_create_method_node_info(
563 hf_agent_bluez_introspection_xml);
564 if (node_info == NULL)
567 path = g_strdup(BT_HF_BLUEZ_OBJECT_PATH);
568 DBG("path is [%s]", path);
570 conn = __bt_hf_get_gdbus_connection();
572 ERR("Unable to get connection");
576 object_id = g_dbus_connection_register_object(conn, path,
577 node_info->interfaces[0],
580 if (object_id == 0) {
581 ERR("Failed to register: %s", error->message);
583 g_dbus_node_info_unref(node_info);
588 g_dbus_node_info_unref(node_info);
590 node_info = __bt_hf_create_method_node_info(hf_agent_introspection_xml);
591 if (node_info == NULL)
594 path = g_strdup(BT_HF_AGENT_OBJECT_PATH);
595 DBG("path is [%s]", path);
597 object_id = g_dbus_connection_register_object(conn, path,
598 node_info->interfaces[0],
601 if (object_id == 0) {
602 ERR("Failed to register: %s", error->message);
604 g_dbus_node_info_unref(node_info);
609 g_dbus_node_info_unref(node_info);
615 static GDBusProxy *__bt_hf_gdbus_init_service_proxy(const gchar *service,
616 const gchar *path, const gchar *interface)
622 GDBusConnection *conn;
624 conn = __bt_hf_get_gdbus_connection();
626 ERR("Unable to get connection");
630 proxy = g_dbus_proxy_new_sync(conn,
631 G_DBUS_PROXY_FLAGS_NONE, NULL,
633 interface, NULL, &err);
637 ERR("Unable to create proxy: %s", err->message);
647 static GDBusProxy *__bt_hf_gdbus_get_service_proxy(const gchar *service,
648 const gchar *path, const gchar *interface)
650 return (service_gproxy) ? service_gproxy :
651 __bt_hf_gdbus_init_service_proxy(service,
655 static char __bt_hf_agent_get_tx_power(char *address)
659 GError *error = NULL;
660 char result = READ_TX_POWER_MIN; /* default minimum */
662 proxy = __bt_hf_gdbus_get_service_proxy(BLUEZ_SERVICE_NAME, g_obj_path,
663 BLUEZ_HF_INTERFACE_NAME);
665 ERR("Proxy is NULL");
669 ret = g_dbus_proxy_call_sync(proxy,
670 "GetTxPowerLevel", g_variant_new("(s)", address),
671 G_DBUS_CALL_FLAGS_NONE, -1,
674 ERR("DBus is failed");
676 /* Dbus gives error cause */
677 ERR("D-Bus API failure: errCode[%x], message[%s]",
678 error->code, error->message);
679 g_clear_error(&error);
683 g_variant_get(ret, "(y)", &result);
684 DBG("TX power level = %d", result);
685 g_variant_unref(ret);
689 static int __bt_hf_agent_gdbus_method_send(const char *service,
690 GVariant *path, const char *interface,
697 GError *error = NULL;
699 proxy = __bt_hf_gdbus_get_service_proxy(service, g_obj_path, interface);
701 return BT_HF_AGENT_ERROR_INTERNAL;
703 ret = g_dbus_proxy_call_sync(proxy,
705 G_DBUS_CALL_FLAGS_NONE, -1,
708 /* dBUS-RPC is failed */
709 ERR("dBUS-RPC is failed");
711 /* dBUS gives error cause */
712 ERR("D-Bus API failure: errCode[%x], message[%s]",
713 error->code, error->message);
715 g_clear_error(&error);
717 return BT_HF_AGENT_ERROR_INTERNAL;
720 g_variant_unref(ret);
722 return BT_HF_AGENT_ERROR_NONE;
725 static void __bt_hf_agent_release_queue(void)
728 bt_hf_agent_send_at_info *cmd;
731 len = g_slist_length(bt_hf_info.cmd_send_queue);
732 for (i = 0; i < len; ++i) {
733 cmd = g_slist_nth_data(bt_hf_info.cmd_send_queue, i);
734 if (cmd && cmd->context) {
735 DBG("Pending context found for %.6s[%d]",
736 cmd->at_cmd, cmd->id);
737 err = __bt_hf_agent_set_error(BT_HF_AGENT_ERROR_INTERNAL);
738 g_dbus_method_invocation_return_gerror(cmd->context, err);
741 if (cmd && cmd->timer_id)
742 g_source_remove(cmd->timer_id);
744 g_slist_free(bt_hf_info.cmd_send_queue);
745 bt_hf_info.cmd_send_queue = NULL;
749 static gboolean __bt_hf_monitor_timer_cb(gpointer data)
752 bt_hf_agent_send_at_info *cmd = data;
753 ERR_C("Monitor timer came becuase of timeout for sendflag %d, %s",
754 send_flag, cmd->at_cmd);
755 /* In the case of ATD, we have to inform the remote to end the call */
756 if (strstr(cmd->at_cmd, "ATD") || strstr(cmd->at_cmd, "BLDN")) {
757 INFO_C("Sending CHUP for remote call termination");
758 __bt_hf_send_only_without_queue(&bt_hf_info, BT_HF_END_CALL,
759 strlen(BT_HF_END_CALL));
760 /* Here there is a high posisbility that we do not get response
761 * for CHUP. Hence we need to decrement send_flag to process further
762 * incomming packets because we already incremented it in the CHUP case. */
766 /* In the case of ATD, prev_cmd will be always ATD, because we will not
767 * allow further commands. For safer side again set prev_cmd as ATD */
768 strncpy(prev_cmd, "ATD\0", BT_HF_CMD_BUF_SIZE - 1);
770 hf_handle_rx_at_cmd(&bt_hf_info, BT_HF_ERROR_RESP);
778 gboolean __bt_hf_agent_add_queue(GDBusMethodInvocation *context, char *at,
779 int count, gboolean pending_flag)
782 if (bt_hf_info.slc == FALSE)
786 DBG("*** Add Pending queue request for = %s **** ", at);
788 DBG("Add Pending queue respnse for = %s ", at);
790 bt_hf_agent_send_at_info *cmd = g_new0(bt_hf_agent_send_at_info, 1);
792 memcpy(cmd->at_cmd, at, count);
794 cmd->context = context;
795 cmd->pending = pending_flag;
796 bt_hf_info.cmd_send_queue = g_slist_append(bt_hf_info.cmd_send_queue,
798 len = g_slist_length(bt_hf_info.cmd_send_queue);
799 for (i = 0; i < len; ++i) {
800 cmd = g_slist_nth_data(bt_hf_info.cmd_send_queue, i);
801 DBG("Q> %.6s[%d]", cmd->at_cmd, cmd->id);
804 /* We need to have base timeout + tolerance value to process other request */
805 if (strstr(at, "ATD") || strstr(at, "BLDN")) {
806 /* Android 15 seconds timeout in case of ATD timeout in flight mode */
807 cmd->timer_id = g_timeout_add_seconds(BT_HF_COMMAND_TIMEOUT * 5 + len,
808 __bt_hf_monitor_timer_cb, cmd);
810 cmd->timer_id = g_timeout_add_seconds(BT_HF_COMMAND_TIMEOUT + len,
811 __bt_hf_monitor_timer_cb, cmd);
817 Below methods exposed to Applicatoins
819 static gboolean __bt_hf_agent_emit_signal(GDBusConnection *connection,
820 const char *path, const char *interface,
821 const char *signal_name, GVariant *param)
823 GError *error = NULL;
825 ret = g_dbus_connection_emit_signal(connection,
827 interface, signal_name,
831 /* dBUS gives error cause */
832 ERR("D-Bus API failure: errCode[%x], message[%s]",
833 error->code, error->message);
834 g_clear_error(&error);
837 INFO_C("Emit Signal [%s]", signal_name);
842 static gboolean __bt_hf_agent_emit_property_changed(
843 GDBusConnection *connection,
845 const char *interface,
851 GError *error = NULL;
853 ret = g_dbus_connection_emit_signal(connection,
854 NULL, path, interface,
856 g_variant_new("s(v)", name, property),
860 /* dBUS gives error cause */
861 ERR("D-Bus API failure: errCode[%x], message[%s]",
862 error->code, error->message);
863 g_clear_error(&error);
871 Below methods exposed to Bluez
874 static void __bt_hf_agent_handle_ind_change(bt_hf_agent_info_t *bt_hf_info,
875 guint index, gint value)
877 GDBusConnection *conn;
879 struct indicator *ind = g_slist_nth_data(bt_hf_info->indies, index - 1);
881 ERR("Indicator is NULL");
888 conn = __bt_hf_get_gdbus_connection();
890 ERR("Unable to get connection");
894 INFO("Indicator name is %s, value = [%d]", name, value);
895 if (!strcmp(name, "\"call\"")) {
896 bt_hf_info->ciev_call_status = value;
898 __bt_hf_agent_emit_signal(conn,
899 BT_HF_AGENT_OBJECT_PATH,
900 BT_HF_SERVICE_INTERFACE,
901 "CallStarted", NULL);
902 bt_hf_info->is_dialing = FALSE;
903 bt_hf_info->call_active = TRUE;
904 } else if (bt_hf_info->call_active) {
905 __bt_hf_agent_emit_signal(conn,
906 BT_HF_AGENT_OBJECT_PATH,
907 BT_HF_SERVICE_INTERFACE,
909 bt_hf_info->call_active = FALSE;
912 } else if (!strcmp(name, "\"callsetup\"")) {
913 bt_hf_info->ciev_call_setup_status = value;
914 if (value == 0 && bt_hf_info->is_dialing) {
915 __bt_hf_agent_emit_signal(conn,
916 BT_HF_AGENT_OBJECT_PATH,
917 BT_HF_SERVICE_INTERFACE,
920 bt_hf_info->is_dialing = FALSE;
921 } else if (!bt_hf_info->is_dialing && value > 0)
922 bt_hf_info->is_dialing = TRUE;
924 if (bt_hf_info->ciev_call_status == 0 &&
925 bt_hf_info->ciev_call_setup_status == 0)
926 __bt_hf_agent_emit_signal(gdbus_conn,
927 BT_HF_AGENT_OBJECT_PATH,
928 BT_HF_SERVICE_INTERFACE,
931 } else if (!strcmp(name, "\"callheld\"")) {
932 if (value == 0) { /* No calls held*/
933 __bt_hf_agent_emit_signal(conn,
934 BT_HF_AGENT_OBJECT_PATH,
935 BT_HF_SERVICE_INTERFACE,
938 } else if (value == 1) {
939 /*Call is placed on hold or active/held calls swapped */
940 __bt_hf_agent_emit_signal(conn,
941 BT_HF_AGENT_OBJECT_PATH,
942 BT_HF_SERVICE_INTERFACE,
943 "CallsSwapped", NULL);
944 bt_hf_info->is_dialing = FALSE;
946 /*Call on hold, no active call*/
947 __bt_hf_agent_emit_signal(conn,
948 BT_HF_AGENT_OBJECT_PATH,
949 BT_HF_SERVICE_INTERFACE,
951 bt_hf_info->is_dialing = FALSE;
953 } else if (!strcmp(name, "\"service\""))
954 __bt_hf_agent_emit_property_changed(conn,
955 BT_HF_AGENT_OBJECT_PATH,
956 BT_HF_SERVICE_INTERFACE,
957 "RegistrationStatus",
958 g_variant_new("(q)", value));
959 else if (!strcmp(name, "\"signal\""))
960 __bt_hf_agent_emit_property_changed(conn,
961 BT_HF_AGENT_OBJECT_PATH,
962 BT_HF_SERVICE_INTERFACE, "SignalStrength",
963 g_variant_new("(q)", value));
964 else if (!strcmp(name, "\"roam\""))
965 __bt_hf_agent_emit_property_changed(conn,
966 BT_HF_AGENT_OBJECT_PATH,
967 BT_HF_SERVICE_INTERFACE, "RoamingStatus",
968 g_variant_new("(q)", value));
969 else if (!strcmp(name, "\"battchg\""))
970 __bt_hf_agent_emit_property_changed(conn,
971 BT_HF_AGENT_OBJECT_PATH,
972 BT_HF_SERVICE_INTERFACE, "BatteryCharge",
973 g_variant_new("(q)", value));
977 static gboolean __bt_hf_agent_launch_call_app(const char *launch_type,
984 app_manager_is_running(CALL_APP_ID, &is_running);
988 DBG_SECURE("Launch type = %s, number(%s)", launch_type, number);
992 ERR("bundle_create() Failed");
996 bundle_add(b, "launch-type", launch_type);
998 if (strlen(number) != 0)
999 bundle_add(b, "number", number);
1001 bundle_add(b, "carrier-type", "BT");
1002 DBG("For 3G, carrier-type: BT has been added");
1004 aul_launch_app(CALL_APP_ID, b);
1012 static void __bt_hf_agent_handle_voice_activation(gint value)
1014 GDBusConnection *conn;
1016 conn = __bt_hf_get_gdbus_connection();
1018 ERR("Unable to get connection");
1022 __bt_hf_agent_emit_signal(conn, BT_HF_AGENT_OBJECT_PATH,
1023 BT_HF_SERVICE_INTERFACE,
1025 g_variant_new("(i)", value));
1030 static void __bt_hf_agent_handle_speaker_gain(gint value)
1032 GDBusConnection *conn;
1034 conn = __bt_hf_get_gdbus_connection();
1036 ERR("Unable to get connection");
1040 __bt_hf_agent_emit_signal(conn, BT_HF_AGENT_OBJECT_PATH,
1041 BT_HF_SERVICE_INTERFACE, "VolumeSpeaker",
1042 g_variant_new("(i)", value));
1047 static int __bt_hf_agent_handle_ccwa(bt_hf_agent_info_t *bt_hf_info,
1050 GDBusConnection *conn;
1052 gchar number[BT_HF_CALLER_NUM_SIZE];
1054 char fmt_str[BT_HF_FMT_STR_SIZE];
1055 int len = strlen(buf);
1057 DBG("__bt_hf_agent_handle_ccwa +");
1058 if (len > BT_HF_CALLER_NUM_SIZE + 10) {
1059 ERR("buf len %d is too long", len);
1063 if ((ccwa = strstr(buf, "\r\n+CCWA"))) {
1064 snprintf(fmt_str, sizeof(fmt_str), "\r\n+CCWA: \"%%%ds", sizeof(number) - 1);
1065 if (sscanf(ccwa, fmt_str, number) == 1) {
1066 sep = strchr(number, '"');
1071 conn = __bt_hf_get_gdbus_connection();
1073 ERR("Unable to get connection");
1077 __bt_hf_agent_emit_signal(conn,
1078 BT_HF_AGENT_OBJECT_PATH,
1079 BT_HF_SERVICE_INTERFACE, "CallWaiting",
1080 g_variant_new("(s)", ccwa));
1082 ERR_SECURE("CCWA '%s' is Call Wating", buf);
1086 DBG("__bt_hf_agent_handle_ccwa -");
1090 static GSList *__bt_hf_prepare_call_list(const char *buf) {
1091 GSList *call_list = NULL;
1096 char delim_sep[] = "\r\n";
1097 char temp_buf[BT_HF_DATA_BUF_SIZE] = {0,};
1099 hf_call_list_info_t *call_info;
1102 strncpy(temp_buf, buf, BT_HF_DATA_BUF_SIZE - 1);
1104 str = strtok_r(temp_buf, delim_sep, &sp);
1105 while (str != NULL) {
1106 if (!(strstr(str, "+CLCC:"))) {
1107 str = strtok_r(NULL, delim_sep, &sp);
1111 call_info = g_new0(hf_call_list_info_t, 1);
1113 sscanf(str, "+CLCC: %1d,%1d, %1d, %1d, %1d",
1114 &call_info->idx, &call_info->dir,
1115 &call_info->status, &call_info->mode,
1116 &call_info->multi_party);
1117 DBG("Index = [%d], Direction = [%d], Status = [%d], Mode = [%d], Multi_party = [%d]\n",
1118 call_info->idx, call_info->dir, call_info->status,
1119 call_info->mode, call_info->multi_party);
1121 ptr = strstr(str, "\"");
1123 temp = strstr(ptr + 1, "\"");
1126 DBG_SECURE("\tPhone Number = [%s]\n", ptr + 1);
1127 call_info->number = g_strdup(ptr + 1);
1129 if (strstr(temp + 1, ",")) {
1131 DBG("\tType = [%s]\n", temp);
1132 call_info->type = atoi(temp);
1136 /*In case of no phone no. in CLCC respnse, we should launch call application
1137 * with NULL string. By doing so "unknown" shall be displayed*/
1138 DBG("Phone number does not exist\n");
1139 call_info->number = g_strdup("");
1142 call_list = g_slist_append(call_list, call_info);
1143 str = strtok_r(NULL, delim_sep, &sp);
1149 static GSList *__bt_hf_get_call_list(bt_hf_agent_info_t *bt_hf_info)
1151 char buf[BT_HF_DATA_BUF_SIZE] = {0,};
1152 GSList *call_list = NULL;
1156 /* Send CLCC when the callsetup */
1157 __bt_hf_send_and_read(bt_hf_info, BT_HF_CALLLIST, buf,
1158 sizeof(BT_HF_CALLLIST) - 1);
1159 DBG_SECURE("Receive CLCC response buffer = '%s'", buf);
1161 call_list = __bt_hf_prepare_call_list(buf);
1166 static void __bt_hf_call_info_free(void *data)
1170 hf_call_list_info_t *call_info = data;
1171 g_free(call_info->number);
1177 static void __bt_hf_free_call_list(GSList *call_list)
1181 g_slist_free_full(call_list, __bt_hf_call_info_free);
1186 static void __bt_hf_launch_call_using_call_list(GSList *call_list,
1187 bt_hf_agent_info_t *bt_hf_info)
1190 const char *launch_type_str;
1191 hf_call_list_info_t *call_info;
1194 if (call_list == NULL)
1197 len = g_slist_length(call_list);
1200 call_info = g_slist_nth_data(call_list, len);
1202 /* Launch based on below conditions
1203 * DC - Active call which is initiated from H
1204 * MR - Alerting call which is initiated from H
1205 * MT - Incoming call */
1206 if (call_info->status == BT_HF_CALL_STAT_ACTIVE) {
1207 launch_type_str = "DC";
1209 if (call_info->dir == BT_HF_CALL_DIR_INCOMING)
1210 launch_type_str = "MT";
1212 launch_type_str = "MR";
1215 if (__bt_hf_agent_launch_call_app(launch_type_str,
1216 call_info->number) == FALSE)
1217 DBG("call app launching failed");
1222 static GVariant *__bt_hf_agent_get_call_status_info(GSList *call_list)
1228 hf_call_list_info_t *call_info;
1230 GVariantBuilder *builder;
1233 builder = g_variant_builder_new(G_VARIANT_TYPE("a(siiii)"));
1235 call_count = g_slist_length(call_list);
1236 DBG("Total call count = '%d'", call_count);
1238 while (call_count--) {
1239 call_info = g_slist_nth_data(call_list, call_count);
1240 INFO("Idx=%d, Dir=%d, status=%d, mode=%d, mparty=%d",
1241 call_info->idx, call_info->dir, call_info->status,
1242 call_info->mode, call_info->multi_party);
1243 caller = call_info->number;
1245 g_variant_builder_add(builder, "(siiii)",
1246 caller, call_info->dir, call_info->status,
1247 call_info->multi_party, call_info->idx);
1249 var_data = g_variant_new("(ia(siiii))",
1250 g_slist_length(call_list), builder);
1252 g_variant_builder_unref(builder);
1257 static void __bt_hf_clear_prev_sent_cmd(void)
1259 if (prev_cmd[0] != 0)
1260 ERR("No sent command");
1262 memset(prev_cmd, 0, BT_HF_CMD_BUF_SIZE);
1267 static void __bt_hf_agent_send_call_status_info(GSList *call_list)
1269 GDBusConnection *conn;
1272 var_data = __bt_hf_agent_get_call_status_info(call_list);
1273 conn = __bt_hf_get_gdbus_connection();
1275 ERR("Unable to get connection");
1280 __bt_hf_agent_emit_signal(conn,
1281 BT_HF_AGENT_OBJECT_PATH,
1282 BT_HF_SERVICE_INTERFACE,
1287 static void __bt_hf_agent_handle_call_list(bt_hf_agent_info_t *bt_hf_info)
1291 __bt_hf_lock_display(0);
1293 bt_hf_info->context = NULL;
1295 /* Send CLCC. The response will be handled in the handler */
1296 ret = __bt_hf_send_only(bt_hf_info, BT_HF_CALLLIST,
1297 sizeof(BT_HF_CALLLIST) - 1);
1299 ERR("Failed to send CLCC");
1301 __bt_hf_unlock_display();
1304 static void __bt_hf_agent_request_call_list_info(bt_hf_agent_info_t *bt_hf_info,
1308 struct indicator *ind = g_slist_nth_data(bt_hf_info->indies, index - 1);
1310 ERR("Indicator is NULL");
1314 DBG("name : %s", name);
1316 if ((strcmp(name, "\"callsetup\"") != 0) &&
1317 (strcmp(name, "\"call\"") != 0) &&
1318 (strcmp(name, "\"callheld\"") != 0))
1321 __bt_hf_lock_display(0);
1323 __bt_hf_agent_handle_call_list(bt_hf_info);
1325 __bt_hf_unlock_display();
1329 static gboolean __bt_hf_send_available_codec(bt_hf_agent_info_t *bt_hf_info, int send_only)
1331 gchar buf[BT_HF_DATA_BUF_SIZE];
1332 gchar cmd_buf[BT_HF_CMD_BUF_SIZE] = {0};
1335 snprintf(cmd_buf, sizeof(cmd_buf), BT_HF_AVAILABLE_CODEC,
1336 BT_HF_CODEC_ID_CVSD, BT_HF_CODEC_ID_MSBC);
1338 bt_hf_info->context = NULL;
1340 ret = __bt_hf_send_only(bt_hf_info, cmd_buf, strlen(cmd_buf));
1343 ret = __bt_hf_send_and_read(bt_hf_info, cmd_buf, buf,
1346 if (!ret || !strstr(buf, "OK"))
1352 static int _hf_agent_codec_setup(const char *addr, guint codec_id)
1357 ERR("g_obj_path is NULL\n");
1358 return BT_HF_AGENT_ERROR_INTERNAL;
1362 case BT_HF_CODEC_ID_CVSD:
1363 INFO("Set NB parameters");
1364 ret = __bt_hf_agent_gdbus_method_send(BLUEZ_SERVICE_NAME,
1365 g_variant_new("(ss)", "Handsfree", addr),
1366 BT_ADAPTER_INTERFACE,
1369 case BT_HF_CODEC_ID_MSBC:
1370 INFO("Set WBS parameters");
1371 ret = __bt_hf_agent_gdbus_method_send(BLUEZ_SERVICE_NAME,
1372 g_variant_new("(ss)", "Handsfree", addr),
1373 BT_ADAPTER_INTERFACE,
1374 "SetWbsParameters");
1377 ret = BT_HF_AGENT_ERROR_INTERNAL;
1378 ERR("Invalid Codec\n");
1383 ERR("Failed to setup the Codec\n");
1385 current_codec_id = codec_id;
1390 static void __bt_hf_agent_handle_codec_select(bt_hf_agent_info_t *bt_hf_info,
1393 gchar cmd_buf[BT_HF_CMD_BUF_SIZE] = {0};
1396 if (codec_id != BT_HF_CODEC_ID_CVSD && codec_id != BT_HF_CODEC_ID_MSBC) {
1397 INFO("Codec id doesn't match, so send available codec again");
1398 ret = __bt_hf_send_available_codec(bt_hf_info, 1);
1400 ERR("Failed to send avalable codec");
1404 /* HF should be ready accpet SCO connection before sending theresponse for
1405 "\r\n+BCS=>Codec ID\r\n", Keep the BT chip ready to recevie encoded SCO data */
1406 ret = _hf_agent_codec_setup(bt_hf_info->remote_addr, codec_id);
1408 snprintf(cmd_buf, sizeof(cmd_buf), BT_HF_CODEC_SELECT, codec_id);
1410 bt_hf_info->context = NULL;
1412 ret = __bt_hf_send_only(bt_hf_info, cmd_buf, strlen(cmd_buf));
1414 ERR("Failed to select the Codec");
1417 void __bt_hf_agent_print_at_buffer(char *message, const char *buf)
1421 char s[BT_HF_DATA_BUF_SIZE] = {0, };
1422 gboolean hide = FALSE;
1424 gboolean has_clcc = FALSE;
1425 gboolean has_clip = FALSE;
1426 gboolean has_ccwa = FALSE;
1429 strncpy(s, buf, BT_HF_DATA_BUF_SIZE - 1);
1431 has_clcc = strstr(buf, "CLCC:") ? TRUE : FALSE;
1432 if (has_clcc == TRUE)
1434 has_clip = strstr(buf, "+CLIP:") ? TRUE : FALSE;
1435 if (has_clip == TRUE)
1437 has_ccwa = strstr(buf, "+CCWA:") ? TRUE : FALSE;
1440 /* +XSAT: 11,DISC */
1441 xsat_ptr = strstr(s, "11,DISC,");
1443 xsat_ptr = xsat_ptr + 8;
1445 while (xsat_ptr[x] != '\0' && xsat_ptr[x] != '\r' && xsat_ptr[x] != '\n') {
1451 /* AT+XSAT=11,Q_CT,X,XXXX */
1452 xsat_ptr = strstr(s, "11,Q_CT,");
1454 xsat_ptr = xsat_ptr + 8;
1456 while (xsat_ptr[x] != '\0' && xsat_ptr[x] != '\r' && xsat_ptr[x] != '\n') {
1457 if (x > 1) /* ignore 0 and 1 position */
1464 while (s[i] != '\0') {
1465 if (s[i] == '\r' || s[i] == '\n') {
1469 hide = hide ? FALSE : TRUE;
1470 else if ((has_clcc || has_clip || has_ccwa) && hide) {
1478 INFO("%s Buffer = %s, Length = %d ", message, s, strlen(s));
1483 static int __bt_hf_agent_handler_ciev(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1485 gchar indicator[BT_HF_INDICATOR_DESCR_SIZE + 4];
1489 char fmt_str[BT_HF_FMT_STR_SIZE];
1491 DBG("++++++++ __bt_hf_agent_handler_ciev +++++++++");
1493 snprintf(fmt_str, sizeof(fmt_str), "\r\n+CIEV:%%%ds\r\n", sizeof(indicator) - 1);
1494 if (sscanf(buf, fmt_str, indicator) == 1) {
1495 sep = strchr(indicator, ',');
1498 index = atoi(indicator);
1500 __bt_hf_agent_handle_ind_change(bt_hf_info, index, value);
1502 if (bt_hf_info->ciev_call_status == 0 &&
1503 bt_hf_info->ciev_call_setup_status == 0)
1504 INFO("No active call");
1506 /* Request CLCC based on indicator change for call/callsetup/callHeld */
1507 __bt_hf_agent_request_call_list_info(bt_hf_info, index);
1509 DBG("--------- __bt_hf_agent_handler_ciev ------------");
1513 static void __bt_hf_agent_handle_ven_samsung(bt_hf_agent_info_t *bt_hf_info,
1514 gint app_id, const char *msg)
1516 /* Whomesoever wants need to handle it */
1517 char *sig_name = "SamsungXSAT";
1518 GDBusConnection *conn;
1520 conn = __bt_hf_get_gdbus_connection();
1522 ERR("Unable to get connection");
1526 __bt_hf_agent_emit_signal(conn,
1527 BT_HF_AGENT_OBJECT_PATH,
1528 BT_HF_SERVICE_INTERFACE,
1530 g_variant_new("(is)", app_id, msg));
1533 static int __bt_hf_agent_handler_ring(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1535 DBG("++++++++ __bt_hf_agent_handler_ring ++++++++");
1536 DBG("---------__bt_hf_agent_handler_ring --------");
1541 static int __bt_hf_agent_handler_clip(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1543 DBG("+++++++++ __bt_hf_agent_handler_clip ++++++++");
1544 DBG("---------__bt_hf_agent_handler_clip --------");
1549 static int __bt_hf_agent_handler_bvra(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1551 DBG("+++++++++ __bt_hf_agent_handler_bvra +++++++++");
1553 if (sscanf(buf, "\r\n+BVRA:%1d\r\n", &value) == 1)
1554 __bt_hf_agent_handle_voice_activation(value);
1556 DBG("---------__bt_hf_agent_handler_bvra --------");
1560 static int __bt_hf_agent_handler_bcs(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1563 DBG("+++++++++ __bt_hf_agent_handler_bcs +++++++++-");
1564 if (sscanf(buf, "\r\n+BCS:%3d\r\n", &codec_id))
1565 __bt_hf_agent_handle_codec_select(bt_hf_info, codec_id);
1567 DBG("---------__bt_hf_agent_handler_bcs --------");
1571 static int __bt_hf_agent_handler_vgs(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1574 DBG("+++++++++ __bt_hf_agent_handler_vgs +++++++++");
1575 if (sscanf(buf, "\r\n+VGS:%2d\r\n", &value))
1576 __bt_hf_agent_handle_speaker_gain(value);
1578 DBG("---------__bt_hf_agent_handler_vgs --------");
1583 static int __bt_hf_agent_handler_ccwa(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1585 DBG("+++++++++ __bt_hf_agent_handler_ccwa ++++++++");
1586 __bt_hf_agent_handle_ccwa(bt_hf_info, buf);
1587 DBG("---------__bt_hf_agent_handler_ccwa --------");
1593 static int __bt_hf_agent_handler_xsat(bt_hf_agent_info_t *bt_hf_info,
1597 char msg[BT_HF_DATA_BUF_SIZE];
1598 char fmt_str[BT_HF_CMD_BUF_SIZE];
1600 DBG("+++++++++ __bt_hf_agent_handler_xsat +++++++++");
1601 snprintf(fmt_str, sizeof(fmt_str), "\r\n+XSAT:%%d,%%%ds\r\n", sizeof(msg) - 1);
1602 if (sscanf(buf, fmt_str, &app_id, msg)) {
1603 if (app_id == 2 && strstr(msg, "READTXPOWER")) {
1604 char cmd_buf[BT_HF_CMD_BUF_SIZE * 2] = {0, };
1605 char power = __bt_hf_agent_get_tx_power(bt_hf_info->remote_addr);
1606 snprintf(cmd_buf, sizeof(cmd_buf), "AT+XSAT: 2,%d", power);
1607 bt_hf_agent_send_at_cmd(NULL, cmd_buf);
1609 __bt_hf_agent_handle_ven_samsung(bt_hf_info, app_id, msg);
1613 DBG("---------__bt_hf_agent_handler_xsat --------");
1618 static int __bt_hf_agent_handler_cme_error(bt_hf_agent_info_t *bt_hf_info,
1621 DBG("+++++++++ __bt_hf_agent_handler_cme_error ++++++++");
1623 GDBusConnection *conn;
1625 if (strstr(prev_cmd, "ATD") || strstr(prev_cmd, BT_HF_REDIAL)) {
1626 conn = __bt_hf_get_gdbus_connection();
1628 ERR("Unable to get connection");
1632 __bt_hf_agent_emit_signal(conn,
1633 BT_HF_AGENT_OBJECT_PATH,
1634 BT_HF_SERVICE_INTERFACE,
1639 __bt_hf_clear_prev_sent_cmd();
1644 static int __bt_hf_agent_handler_response_ok(bt_hf_agent_info_t *bt_hf_info,
1647 DBG("+++++++++ __bt_hf_agent_handler_response_ok ++++++++");
1649 __bt_hf_clear_prev_sent_cmd();
1654 static int __bt_hf_agent_handler_response_err(bt_hf_agent_info_t *bt_hf_info,
1657 DBG("+++++++++ __bt_hf_agent_handler_response_err ++++++++");
1658 GDBusConnection *conn;
1660 if (strstr(prev_cmd, "ATD") || strstr(prev_cmd, BT_HF_REDIAL)) {
1661 conn = __bt_hf_get_gdbus_connection();
1663 ERR("Unable to get connection");
1667 __bt_hf_agent_emit_signal(conn, BT_HF_AGENT_OBJECT_PATH,
1668 BT_HF_SERVICE_INTERFACE,
1672 __bt_hf_clear_prev_sent_cmd();
1677 static int __bt_hf_agent_handler_response_serr(bt_hf_agent_info_t *bt_hf_info,
1681 GDBusConnection *conn;
1683 if (strstr(prev_cmd, "ATD") || strstr(prev_cmd, BT_HF_REDIAL)) {
1684 conn = __bt_hf_get_gdbus_connection();
1686 ERR("Unable to get connection");
1690 __bt_hf_agent_emit_signal(conn, BT_HF_AGENT_OBJECT_PATH,
1691 BT_HF_SERVICE_INTERFACE,
1696 __bt_hf_clear_prev_sent_cmd();
1702 static int __bt_hf_agent_handler_clcc(bt_hf_agent_info_t *bt_hf_info, const char *buffer)
1705 GSList *call_list = NULL;
1706 DBG("+++++++++ __bt_hf_agent_handler_clcc ++++++++");
1707 DBG_SECURE("Receive CLCC response buffer = '%s'", buffer);
1709 __bt_hf_lock_display(0);
1711 call_list = __bt_hf_prepare_call_list(buffer);
1713 if (call_list == NULL)
1716 __bt_hf_launch_call_using_call_list(call_list, bt_hf_info);
1718 __bt_hf_agent_send_call_status_info(call_list);
1720 __bt_hf_free_call_list(call_list);
1723 __bt_hf_unlock_display();
1724 DBG("---------__bt_hf_agent_handler_clcc --------");
1728 static struct hf_event hf_event_callbacks[] = {
1729 { "\r\n+CIEV:", __bt_hf_agent_handler_ciev },
1730 { "\r\nRING", __bt_hf_agent_handler_ring },
1731 { "\r\n+CLIP:", __bt_hf_agent_handler_clip },
1732 { "\r\n+BVRA:", __bt_hf_agent_handler_bvra },
1733 { "\r\n+BCS:", __bt_hf_agent_handler_bcs },
1734 { "\r\n+VGS:", __bt_hf_agent_handler_vgs },
1735 { "\r\n+CCWA:", __bt_hf_agent_handler_ccwa },
1736 { "\r\n+XSAT:", __bt_hf_agent_handler_xsat },
1737 {"\r\n+CLCC:", __bt_hf_agent_handler_clcc },
1741 static struct hf_event hf_event_resp_callbacks[] = {
1742 { "\r\n+CME ERROR:", __bt_hf_agent_handler_cme_error },
1743 { "\r\nOK\r\n", __bt_hf_agent_handler_response_ok },
1744 { "ERROR", __bt_hf_agent_handler_response_err },
1745 { "SERR", __bt_hf_agent_handler_response_serr },
1749 bt_hf_agent_send_at_info *__bt_hf_agent_find_next(bt_hf_agent_info_t *bt_hf_info)
1753 bt_hf_agent_send_at_info *cmd;
1754 len = g_slist_length(bt_hf_info->cmd_send_queue);
1755 for (i = 0; i < len; ++i) {
1756 cmd = g_slist_nth_data(bt_hf_info->cmd_send_queue, i);
1757 DBG("F> %.6s[%d]", cmd->at_cmd, cmd->id);
1759 len = g_slist_length(bt_hf_info->cmd_send_queue);
1760 DBG("Context queue length = %d", len);
1764 cmd = g_slist_nth_data(bt_hf_info->cmd_send_queue, 0);
1766 bt_hf_info->cmd_send_queue = g_slist_remove(bt_hf_info->cmd_send_queue, cmd);
1767 DBG("NEXT[%d] Found %s, context = 0x%x, pending = %d", cmd->id,
1768 cmd->at_cmd, cmd->context, cmd->pending);
1772 DBG("**** Not found any pending command on list length %d ****", len);
1777 static int hf_handle_rx_at_cmd(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1779 struct hf_event *ev;
1781 bt_hf_agent_send_at_info *cmd = NULL;
1783 __bt_hf_agent_print_at_buffer("Processing [Rx cmd]:", buf);
1785 for (ev = hf_event_resp_callbacks; ev->cmd; ev++) {
1786 if (strstr(buf, ev->cmd)) {
1789 DBG("Send flag value = %d(after)", send_flag);
1790 ret = ev->callback(bt_hf_info, buf);
1797 for (ev = hf_event_callbacks; ev->cmd; ev++) {
1798 if (!strncmp(buf, ev->cmd, strlen(ev->cmd))) {
1799 ret = ev->callback(bt_hf_info, buf);
1807 cmd = __bt_hf_agent_find_next(bt_hf_info);
1809 if (cmd && cmd->context) {
1810 DBG("Pending context found for %.6s[%d]", cmd->at_cmd, cmd->id);
1811 g_dbus_method_invocation_return_value(cmd->context, NULL);
1813 g_source_remove(cmd->timer_id);
1819 cmd = __bt_hf_agent_find_next(bt_hf_info);
1821 if (cmd && cmd->pending && send_flag == 0) {
1822 DBG("Pending only found of %.6s[%d]", cmd->at_cmd, cmd->id);
1823 __bt_hf_send_only_without_queue(bt_hf_info,
1824 cmd->at_cmd, cmd->count);
1825 cmd->pending = FALSE;
1826 bt_hf_info->cmd_send_queue = g_slist_prepend(bt_hf_info->cmd_send_queue,
1828 DBG("Prepend %.6s[%d]", cmd->at_cmd, cmd->id);
1831 DBG("Pending free for %.6s[%d] - send_flag = %d",
1832 cmd->at_cmd, cmd->id, send_flag);
1834 g_source_remove(cmd->timer_id);
1840 /* Need to process further pending */
1841 cmd = __bt_hf_agent_find_next(bt_hf_info);
1843 if (cmd->pending && send_flag == 0) {
1844 DBG("2nd Pending only found of %.6s[%d]",
1845 cmd->at_cmd, cmd->id);
1846 __bt_hf_send_only_without_queue(bt_hf_info,
1847 cmd->at_cmd, cmd->count);
1848 cmd->pending = FALSE;
1850 bt_hf_info->cmd_send_queue = g_slist_prepend(bt_hf_info->cmd_send_queue,
1852 DBG("2nd Prepend %.6s[%d]", cmd->at_cmd, cmd->id);
1858 static int hf_handle_append_clcc_buff(char *cmd_buf, const char *buf)
1862 int cmd_buf_len = 0;
1863 char *pos_start, *pos_end;
1864 const char *datap = buf;
1866 cmd_buf_len = strlen(cmd_buf);
1867 buf_length = strlen(buf);
1868 DBG("buf_length = %d, cmd_buf_len = %d", buf_length, cmd_buf_len);
1870 if (buf_length > 0 && strstr(datap, "+CLCC")) {
1871 pos_start = strstr(datap, "\r\n");
1872 if (pos_start == NULL) {
1873 ERR("Invalid AT command signature..\n");
1877 pos_end = g_strrstr(datap, "+CLCC");
1878 if (pos_end == NULL) {
1879 ERR("Invalid AT command signature..\n");
1882 pos_end = strstr(pos_end, "\r\n");
1883 cmd_length = (pos_end - pos_start) + 2;
1884 INFO("CLCC balance Cmd Length = %d\n", cmd_length);
1885 memcpy(cmd_buf + cmd_buf_len, pos_start, cmd_length);
1886 cmd_buf[cmd_buf_len + cmd_length] = '\0';
1888 if (strstr(cmd_buf, "\r\nOK\r\n")) {
1889 pos_end = strstr(datap, "\r\nOK\r\n");
1890 cmd_length = (pos_end - pos_start);
1891 memcpy(cmd_buf + cmd_buf_len, pos_start, cmd_length);
1892 cmd_buf[cmd_buf_len + cmd_length] = '\0';
1893 INFO("New CLCC balance Cmd Length = %d", cmd_length);
1900 static int hf_handle_rx_at_buff(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1904 char *pos_start, *pos_end;
1906 gchar cmd_buf[BT_HF_DATA_BUF_SIZE] = {0,};
1907 const char *datap = buf;
1909 __bt_hf_agent_print_at_buffer("[HF AT CMD] Recv >>>>>:", buf);
1911 buf_length = strlen(buf);
1913 while (buf_length > 0) {
1914 pos_start = strstr(datap, "\r\n");
1915 if (pos_start == NULL) {
1916 ERR("Invalid AT command start signature..\n");
1921 pos_end = strstr(datap, "\r\n");
1922 if (pos_end == NULL) {
1923 ERR("Invalid AT command end signature..\n");
1926 cmd_length = (pos_end - pos_start) + 2;
1927 DBG("Cmd Length = %d\n", cmd_length);
1929 memcpy(cmd_buf, pos_start, cmd_length);
1930 cmd_buf[cmd_length] = '\0';
1932 buf_length = buf_length - cmd_length;
1933 datap = datap + cmd_length - 2;
1935 /* We need to pass all the CLCC's together to its handler */
1936 if (strstr(cmd_buf, "+CLCC")) {
1937 tmp = hf_handle_append_clcc_buff(cmd_buf, datap);
1939 buf_length = buf_length - tmp;
1941 hf_handle_rx_at_cmd(bt_hf_info, cmd_buf);
1942 DBG("Pending buf_length = %d\n", buf_length);
1947 static gboolean __bt_hf_agent_data_cb(GIOChannel *chan, GIOCondition cond,
1948 bt_hf_agent_info_t *bt_hf_info)
1950 gchar buf[BT_HF_DATA_BUF_SIZE] = {0,};
1952 GError *gerr = NULL;
1953 gboolean recvd_ok = FALSE;
1954 gboolean recvd_error = FALSE;
1955 gboolean recvd_sec_error = FALSE;
1957 if (cond & (G_IO_ERR | G_IO_HUP)) {
1958 ERR("ERR or HUP on RFCOMM socket");
1959 INFO_C("Disconnected [HF role] [Terminated by remote dev]");
1960 is_hf_connected = FALSE;
1961 bt_hf_info->slc = FALSE;
1962 __bt_hf_agent_release();
1966 if (g_io_channel_read_chars(chan, buf, sizeof(buf) - 1, &read, &gerr)
1967 != G_IO_STATUS_NORMAL) {
1969 ERR("Read failed, cond = [%d], Err msg = [%s]",
1970 cond, gerr->message);
1976 recvd_ok = NULL != strstr(buf, BT_HF_OK_RESPONSE);
1977 recvd_error = NULL != strstr(buf, BT_HF_ERROR_RESPONSE);
1978 recvd_sec_error = NULL != strstr(buf, BT_HF_SEC_ERROR_RESPONSE);
1979 DBG("<-------Received data --send flag status = %d ----->", send_flag);
1981 /* Once service level connection is established we need to handle
1982 * all the intermediate AT commands */
1983 if (bt_hf_info->state != BT_HF_STATE_CONNECTED)
1987 strncat(global_buff, buf,
1988 (BT_AT_COMMAND_BUFFER_MAX - 1) - strlen(global_buff));
1989 if (!(recvd_ok || recvd_error || recvd_sec_error)) {
1990 __bt_hf_agent_print_at_buffer("Concat ()", global_buff);
1992 DBG("*** Received terminator.. process Rx buffer ***");
1993 hf_handle_rx_at_buff(bt_hf_info, global_buff);
1994 memset(global_buff, 0, sizeof(global_buff));
1997 INFO("*** Received Direct AT buffer packet handling ****");
1998 hf_handle_rx_at_buff(bt_hf_info, buf);
2003 static void __bt_hf_agent_start_watch(bt_hf_agent_info_t *bt_hf_info)
2005 bt_hf_info->watch_id = g_io_add_watch(bt_hf_info->io_chan,
2006 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
2007 (GIOFunc) __bt_hf_agent_data_cb, bt_hf_info);
2010 static void __bt_hf_agent_stop_watch(bt_hf_agent_info_t *bt_hf_info)
2012 if (bt_hf_info->watch_id > 0) {
2013 g_source_remove(bt_hf_info->watch_id);
2014 bt_hf_info->watch_id = 0;
2018 static gboolean __bt_hf_channel_write(GIOChannel *io, gchar *data,
2025 status = g_io_channel_write_chars(io, data, count, &written,
2027 if (status != G_IO_STATUS_NORMAL)
2036 static gboolean __bt_hf_send_only_without_queue(bt_hf_agent_info_t *bt_hf_info,
2037 gchar *data, gsize count)
2039 GIOChannel *io_chan = bt_hf_info->io_chan;
2040 if (!__bt_hf_channel_write(io_chan, data, count))
2043 g_io_channel_flush(io_chan, NULL);
2045 if (count > 2 && data[2] == 'D') { /* ATDXXXXX */
2046 INFO("Send only buffer size =[%d] No len = %d - Send <<<<<| %s",
2047 count, count - 6, "ATDXXXXXXX");
2048 snprintf(prev_cmd, BT_HF_CMD_BUF_SIZE, "%s", data);
2050 INFO("No queue....Send only buffer size =[%d] - Send <<<<<| %s",
2055 /* DBG("Ref %d(after) on Send only buffer size =[%d] - Send <<<<<| %s",
2056 * send_flag, count, data); */
2061 static gboolean __bt_hf_send_only(bt_hf_agent_info_t *bt_hf_info, gchar *data,
2064 gboolean pending = FALSE;
2065 GIOChannel *io_chan = bt_hf_info->io_chan;
2070 __bt_hf_agent_add_queue(bt_hf_info->context, data, count, pending);
2075 if (!__bt_hf_channel_write(io_chan, data, count))
2078 g_io_channel_flush(io_chan, NULL);
2080 if (count > 2 && data[2] == 'D') /* ATDXXXXX */
2081 INFO("Send only buffer size =[%d] No len = %d - Send <<<<<| %s",
2082 count, count - 6, "ATDXXXXXXX");
2084 INFO("Send only buffer size =[%d] - Send <<<<<| %s", count, data);
2087 DBG("Ref %d(after) on Send only buffer size =[%d] - Send <<<<<| %s",
2088 send_flag, count, data);
2092 static gboolean __bt_hf_send_and_read(bt_hf_agent_info_t *bt_hf_info,
2093 gchar *data, gchar *response, gsize count)
2095 GIOChannel *io_chan = bt_hf_info->io_chan;
2097 gboolean recvd_ok = FALSE;
2098 gboolean recvd_error = FALSE;
2099 gboolean recvd_sec_error = FALSE;
2100 gchar *resp_buf = response;
2101 gsize toread = BT_HF_DATA_BUF_SIZE - 1;
2106 GDBusConnection *conn;
2108 /* Should not send cmds if DUT send a command and wait the response */
2109 if (prev_cmd[0] != 0) {
2110 INFO("DUT is waiting a respond for previous TX cmd. Skip sending.");
2114 memset(resp_buf, 0, BT_HF_DATA_BUF_SIZE);
2116 if (!__bt_hf_channel_write(io_chan, data, count))
2119 g_io_channel_flush(io_chan, NULL);
2121 __bt_hf_agent_print_at_buffer("[HF AT CMD] Send <<<<<:", data);
2123 fd = g_io_channel_unix_get_fd(io_chan);
2125 p.events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
2127 /* Maximun 8 seconds of poll or 8 minus no of cmd received */
2128 for (i = 1; i <= MAX_WAITING_DELAY; i++) {
2129 DBG("Loop Counter = %d", i);
2131 err = poll(&p, 1, 1000);
2133 ERR("Loop Counter = %d, >>>> Poll error happen", i);
2135 } else if (err == 0) {
2136 INFO("Loop Counter = %d, >>>> Poll Timeout", i);
2139 if (p.revents & (POLLERR | POLLHUP | POLLNVAL)) {
2140 ERR("Loop Counter = %d, >> Poll ERR/HUP/INV (%d)",
2143 conn = __bt_hf_get_gdbus_connection();
2145 ERR("Unable to get connection");
2149 __bt_hf_agent_emit_signal(conn,
2150 BT_HF_AGENT_OBJECT_PATH,
2151 BT_HF_SERVICE_INTERFACE,
2153 g_variant_new("(s)",
2154 bt_hf_info->remote_addr));
2156 bt_hf_info->state = BT_HF_STATE_DISCONNECTED;
2160 if (p.revents & POLLIN) {
2161 rd_size = read(fd, resp_buf, toread);
2162 resp_buf[rd_size] = '\0';
2163 DBG_SECURE("size = %d, Buffer=[%s]", rd_size, resp_buf);
2164 recvd_ok = NULL != strstr(resp_buf, BT_HF_OK_RESPONSE);
2165 recvd_error = NULL != strstr(resp_buf, BT_HF_ERROR_RESPONSE);
2166 recvd_sec_error = NULL != strstr(resp_buf, BT_HF_SEC_ERROR_RESPONSE);
2168 resp_buf += rd_size;
2171 if (recvd_ok || recvd_error || recvd_sec_error) {
2172 DBG("Break Loop Counter = %d", i);
2178 /* Once service level connection is established we need to handle
2179 * all the intermediate AT commands */
2180 if (bt_hf_info->state == BT_HF_STATE_CONNECTED)
2181 hf_handle_rx_at_buff(bt_hf_info, response);
2185 static GSList *__bt_hf_parse_indicator_names(gchar *names, GSList *indices)
2187 struct indicator *ind;
2188 gchar *cur = names - 1;
2189 GSList *list = indices;
2192 DBG("Indicator buffer = %s", names);
2193 __bt_hf_agent_print_at_buffer("Indicator names :", names);
2194 while (cur != NULL) {
2196 next = strstr(cur, ",(");
2197 ind = g_new0(struct indicator, 1);
2198 g_strlcpy(ind->descr, cur, BT_HF_INDICATOR_DESCR_SIZE);
2199 ind->descr[(intptr_t) next - (intptr_t) cur] = '\0';
2200 list = g_slist_append(list, (gpointer) ind);
2201 cur = strstr(next + 1, ",(");
2206 static GSList *__bt_hf_parse_indicator_values(gchar *values, GSList *indices)
2209 struct indicator *ind;
2210 GSList *runner = indices;
2212 gchar *cur = values - 1;
2213 DBG("Indicator string = %s", values);
2214 __bt_hf_agent_print_at_buffer("Indicator values :", values);
2215 while (cur != NULL) {
2217 sscanf(cur, "%1d", &val);
2218 cur = strchr(cur, ',');
2219 ind = g_slist_nth_data(runner, 0);
2221 runner = g_slist_next(runner);
2226 static guint __bt_hf_get_hold_mpty_features(gchar *features)
2230 if (strstr(features, "0"))
2231 result |= BT_HF_CHLD_0;
2233 if (strstr(features, "1"))
2234 result |= BT_HF_CHLD_1;
2236 if (strstr(features, "1x"))
2237 result |= BT_HF_CHLD_1x;
2239 if (strstr(features, "2"))
2240 result |= BT_HF_CHLD_2;
2242 if (strstr(features, "2x"))
2243 result |= BT_HF_CHLD_2x;
2245 if (strstr(features, "3"))
2246 result |= BT_HF_CHLD_3;
2248 if (strstr(features, "4"))
2249 result |= BT_HF_CHLD_4;
2254 static gboolean __bt_hf_agent_sco_conn_cb(GIOChannel *chan, GIOCondition cond, gpointer user_data)
2256 bt_hf_agent_info_t *bt_hf_info = user_data;
2257 GDBusConnection *conn;
2260 if (cond & G_IO_NVAL)
2263 if (cond & (G_IO_HUP | G_IO_ERR)) {
2264 g_io_channel_shutdown(chan, TRUE, NULL);
2265 close(bt_hf_info->cli_sco_fd);
2266 g_io_channel_unref(chan);
2267 DBG("Emit AudioDisconnected Signal");
2269 sco_audio_connected = BT_HF_AUDIO_DISCONNECTED;
2271 conn = __bt_hf_get_gdbus_connection();
2273 ERR("Unable to get connection");
2276 __bt_hf_agent_emit_signal(conn,
2277 BT_HF_AGENT_OBJECT_PATH,
2278 BT_HF_SERVICE_INTERFACE,
2279 "AudioDisconnected", NULL);
2287 static gboolean __bt_agent_query_and_update_call_list(gpointer data)
2290 bt_hf_agent_info_t *bt_hf_info = data;
2292 if (bt_hf_info->cli_sco_fd >= 0)
2293 __bt_hf_agent_handle_call_list(bt_hf_info);
2295 INFO("SCO Audio is already disconnected");
2302 static gboolean __bt_hf_agent_sco_accept_cb(GIOChannel *chan, GIOCondition cond, gpointer user_data)
2304 bt_hf_agent_info_t *bt_hf_info = user_data;
2308 GDBusConnection *conn;
2310 INFO("Incoming SCO....");
2312 if (cond & G_IO_NVAL)
2315 sco_skt = g_io_channel_unix_get_fd(chan);
2317 if (cond & (G_IO_HUP | G_IO_ERR)) {
2322 cli_sco_sock = accept(sco_skt, NULL, NULL);
2323 if (cli_sco_sock < 0)
2326 bt_hf_info->cli_sco_fd = cli_sco_sock;
2328 sco_io = g_io_channel_unix_new(cli_sco_sock);
2329 g_io_channel_set_close_on_unref(sco_io, TRUE);
2330 g_io_channel_set_encoding(sco_io, NULL, NULL);
2331 g_io_channel_set_flags(sco_io, G_IO_FLAG_NONBLOCK, NULL);
2332 g_io_channel_set_buffered(sco_io, FALSE);
2334 g_io_add_watch(sco_io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
2335 __bt_hf_agent_sco_conn_cb, bt_hf_info);
2337 /* S-Voice app requires the AudioConnected signal earlier */
2338 DBG("Emit AudioConnected Signal");
2340 sco_audio_connected = BT_HF_AUDIO_CONNECTED;
2342 conn = __bt_hf_get_gdbus_connection();
2344 ERR("Unable to get connection");
2348 __bt_hf_agent_emit_signal(conn,
2349 BT_HF_AGENT_OBJECT_PATH,
2350 BT_HF_SERVICE_INTERFACE,
2351 "AudioConnected", NULL);
2353 /* In the case of incoming call, the call app is already launched,
2354 * hence AudioConnected signal is enough to update the call status.
2355 * In the case of outgoing call we need to lauch the callapp.
2358 g_idle_add(__bt_agent_query_and_update_call_list, bt_hf_info);
2363 void _bt_convert_addr_string_to_type_rev(unsigned char *addr,
2364 const char *address)
2369 ret_if(address == NULL);
2370 ret_if(addr == NULL);
2372 for (i = 0; i < 6; i++) {
2373 addr[5 - i] = strtol(address, &ptr, 16);
2374 if (ptr[0] != '\0') {
2383 static gboolean __bt_hf_agent_sco_accept(bt_hf_agent_info_t *bt_hf_info)
2385 struct sockaddr_sco addr;
2387 bdaddr_t bd_addr = {{0},};
2390 if (bt_hf_info->state != BT_HF_STATE_CONNECTED)
2394 sco_skt = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
2396 ERR("Can't create socket:\n");
2400 /* Bind to local address */
2401 memset(&addr, 0, sizeof(addr));
2402 addr.sco_family = AF_BLUETOOTH;
2404 DBG("Bind to address %s", bt_hf_info->remote_addr);
2406 _bt_convert_addr_string_to_type_rev(bd_addr.b, bt_hf_info->remote_addr);
2407 memcpy(&addr.sco_bdaddr, &bd_addr, sizeof(bdaddr_t));
2409 if (bind(sco_skt, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2410 ERR("Can't bind socket:\n");
2414 if (listen(sco_skt, 1)) {
2415 ERR("Can not listen on the socket:\n");
2419 sco_io = g_io_channel_unix_new(sco_skt);
2420 g_io_channel_set_close_on_unref(sco_io, TRUE);
2421 g_io_channel_set_encoding(sco_io, NULL, NULL);
2422 g_io_channel_set_flags(sco_io, G_IO_FLAG_NONBLOCK, NULL);
2423 g_io_channel_set_buffered(sco_io, FALSE);
2425 bt_hf_info->sco_fd = sco_skt;
2426 bt_hf_info->sco_io_chan = sco_io;
2428 bt_hf_info->sco_watch_id = g_io_add_watch(sco_io,
2429 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, __bt_hf_agent_sco_accept_cb, bt_hf_info);
2431 g_io_channel_unref(sco_io);
2440 static gboolean __bt_get_supported_indicators(bt_hf_agent_info_t *bt_hf_info)
2442 gchar buf[BT_HF_DATA_BUF_SIZE] = {0,};
2445 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_INDICATORS_SUPP, buf,
2446 sizeof(BT_HF_INDICATORS_SUPP) - 1);
2447 if (!ret || !strstr(buf, "+CIND:"))
2450 bt_hf_info->indies = __bt_hf_parse_indicator_names(strchr(buf, '('), NULL);
2455 static gboolean __bt_get_bia_cmd(bt_hf_agent_info_t *bt_hf_info, gchar *cmd, gsize cmd_size)
2460 if (bt_hf_info == NULL || cmd == NULL) {
2461 ERR("Invalid parameter");
2465 ret = g_strlcpy(cmd, BT_HF_INDICATORS_ACTIVATION, cmd_size);
2467 for (l = bt_hf_info->indies; l != NULL; l = g_slist_next(l)) {
2468 ret = g_strlcat(cmd, "0,", cmd_size);
2469 if (ret >= cmd_size) {
2470 ERR("Too many indices");
2476 cmd[ret - 1] = '\0';
2477 DBG("BIA Str : %s", cmd);
2479 ret = g_strlcat(cmd, "\r", cmd_size);
2480 if (ret >= cmd_size) {
2481 ERR("Too many indices");
2488 static gboolean __bt_get_current_indicators(bt_hf_agent_info_t *bt_hf_info)
2490 gchar buf[BT_HF_DATA_BUF_SIZE] = {0,};
2496 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_INDICATORS_VAL, buf,
2497 sizeof(BT_HF_INDICATORS_VAL) - 1);
2498 if (!ret || !strstr(buf, "+CIND:"))
2501 /* if buf has other command prefix, skip it */
2502 str = strstr(buf, "+CIND");
2506 bt_hf_info->indies = __bt_hf_parse_indicator_values(str + 6, bt_hf_info->indies);
2508 /* Parse the updated value */
2509 for (l = bt_hf_info->indies; l != NULL; l = g_slist_next(l), ++index) {
2510 struct indicator *ind = l->data;
2512 DBG("Index is NULL");
2516 if (0 == g_strcmp0(ind->descr, "\"call\"")) {
2517 DBG("CIND Match found index = %d, %s, value = %d",
2518 index, ind->descr, ind->value);
2519 bt_hf_info->ciev_call_status = ind->value;
2520 if (ind->value > 0) {
2521 bt_hf_info->is_dialing = FALSE;
2522 bt_hf_info->call_active = TRUE;
2524 } else if (0 == g_strcmp0(ind->descr, "\"callsetup\"")) {
2525 DBG("CIND Match found index = %d, %s, value = %d",
2526 index, ind->descr, ind->value);
2527 bt_hf_info->ciev_call_setup_status = ind->value;
2528 if (!bt_hf_info->is_dialing && ind->value > 0)
2529 bt_hf_info->is_dialing = TRUE;
2536 static gboolean __bt_establish_service_level_conn(bt_hf_agent_info_t *bt_hf_info)
2538 gchar buf[BT_HF_DATA_BUF_SIZE];
2539 gchar cmd_buf[BT_HF_CMD_BUF_SIZE] = {0};
2542 guint feature = BT_HF_FEATURE_EC_ANDOR_NR |
2543 BT_HF_FEATURE_CALL_WAITING_AND_3WAY |
2544 BT_HF_FEATURE_CLI_PRESENTATION |
2545 BT_HF_FEATURE_VOICE_RECOGNITION |
2546 BT_HF_FEATURE_REMOTE_VOLUME_CONTROL |
2547 BT_HF_FEATURE_ENHANCED_CALL_STATUS |
2548 BT_HF_FEATURE_CODEC_NEGOTIATION;
2550 snprintf(cmd_buf, sizeof(cmd_buf), BT_HF_FEATURES, feature);
2551 ret = __bt_hf_send_and_read(bt_hf_info, cmd_buf, buf,
2556 buf_ptr = strstr(buf, "\r\n+BRSF:");
2557 if (buf_ptr == NULL)
2560 if (!ret || sscanf(buf_ptr, "\r\n+BRSF:%5d", &bt_hf_info->ag_features) != 1)
2562 INFO("Gateway supported features are 0x%X", bt_hf_info->ag_features);
2564 if (bt_hf_info->ag_features & BT_AG_FEATURE_CODEC_NEGOTIATION) {
2565 ret = _hf_agent_codec_setup(bt_hf_info->remote_addr, BT_HF_CODEC_ID_MSBC);
2566 if (ret != BT_HF_AGENT_ERROR_NONE)
2567 ERR("Unable to set the default WBC codec");
2569 ret = __bt_hf_send_available_codec(bt_hf_info, 0);
2573 /* Default codec is NB */
2574 ret = _hf_agent_codec_setup(bt_hf_info->remote_addr, BT_HF_CODEC_ID_CVSD);
2575 if (ret != BT_HF_AGENT_ERROR_NONE)
2576 ERR("Unable to set the default NBC codec");
2579 ret = __bt_get_supported_indicators(bt_hf_info);
2584 ret = __bt_get_current_indicators(bt_hf_info);
2588 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_INDICATORS_ENABLE, buf,
2589 sizeof(BT_HF_INDICATORS_ENABLE) - 1);
2590 if (!ret || !strstr(buf, "OK"))
2593 if ((bt_hf_info->ag_features & BT_AG_FEATURE_3WAY) != 0) {
2594 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_HOLD_MPTY_SUPP,
2595 buf, sizeof(BT_HF_HOLD_MPTY_SUPP) - 1);
2596 if (!ret || !strstr(buf, "+CHLD:")) {
2597 ERR("Unable to get the CHLD Supported info");
2600 bt_hf_info->hold_multiparty_features = __bt_hf_get_hold_mpty_features(
2603 bt_hf_info->hold_multiparty_features = 0;
2605 INFO("Service layer connection successfully established...!");
2607 __bt_hf_send_and_read(bt_hf_info, BT_HF_CALLER_IDENT_ENABLE, buf,
2608 sizeof(BT_HF_CALLER_IDENT_ENABLE) - 1);
2609 __bt_hf_send_and_read(bt_hf_info, BT_HF_CARRIER_FORMAT, buf,
2610 sizeof(BT_HF_CARRIER_FORMAT) - 1);
2611 __bt_hf_send_and_read(bt_hf_info, BT_HF_CALLWAIT_NOTI_ENABLE, buf,
2612 sizeof(BT_HF_CALLWAIT_NOTI_ENABLE) - 1);
2614 if ((bt_hf_info->ag_features & BT_AG_FEATURE_NREC) != 0)
2615 __bt_hf_send_and_read(bt_hf_info, BT_HF_NREC, buf,
2616 sizeof(BT_HF_NREC) - 1);
2618 if ((bt_hf_info->ag_features & BT_AG_FEATURE_EXTENDED_RES_CODE) != 0)
2619 __bt_hf_send_and_read(bt_hf_info, BT_HF_EXTENDED_RESULT_CODE,
2620 buf, sizeof(BT_HF_EXTENDED_RESULT_CODE) - 1);
2622 if (__bt_get_bia_cmd(bt_hf_info, cmd_buf, sizeof(cmd_buf)) == TRUE)
2623 __bt_hf_send_and_read(bt_hf_info, cmd_buf, buf, strlen(cmd_buf));
2625 ERR("__bt_get_bia_cmd is failed");
2627 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_XSAT, buf,
2628 sizeof(BT_HF_XSAT) - 1);
2630 DBG("sent BT_HF_XSAT");
2632 ERR("BT_HF_XSAT sending failed");
2634 /* send Bluetooth Samsung Support Feature cmd */
2635 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_BSSF, buf,
2636 sizeof(BT_HF_BSSF) - 1);
2638 INFO("SLC completed with all commands");
2640 ERR("BT_HF_BSSF sending failed");
2642 bt_hf_info->slc = TRUE;
2645 memset(global_buff, 0, sizeof(global_buff));
2649 static void __bt_hf_agent_sigterm_handler(int signo)
2651 ERR_C("***** Signal handler came with signal %d *****", signo);
2652 GDBusConnection *conn;
2654 conn = __bt_hf_get_gdbus_connection();
2656 ERR("Unable to get connection");
2660 __bt_hf_agent_emit_signal(conn,
2661 BT_HF_AGENT_OBJECT_PATH,
2662 BT_HF_SERVICE_INTERFACE,
2664 DBG("CallEnded Signal done");
2666 g_main_loop_quit(gmain_loop);
2670 INFO_C("Terminating HF agent");
2675 static void __bt_convert_addr_type_to_rev_string(char *address,
2676 unsigned char *addr)
2678 ret_if(address == NULL);
2679 ret_if(addr == NULL);
2681 g_snprintf(address, BT_ADDRESS_STRING_SIZE,
2682 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
2683 addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
2687 static gboolean __bt_hf_agent_release_after(gpointer user_data)
2689 if (__bt_hf_agent_release() == FALSE)
2690 ERR("Unable to release hf connection");
2695 static gboolean __bt_agent_request_service_level_conn(gpointer data)
2698 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
2699 GDBusConnection *conn;
2702 memset(prev_cmd, 0, BT_HF_CMD_BUF_SIZE);
2704 if (!__bt_establish_service_level_conn(&bt_hf_info)) {
2705 ERR("Service Level Connection is fail");
2707 conn = __bt_hf_get_gdbus_connection();
2709 remote_addr = bt_hf_info.remote_addr;
2710 __bt_hf_agent_emit_signal(conn,
2711 BT_HF_AGENT_OBJECT_PATH,
2712 BT_HF_SERVICE_INTERFACE,
2714 g_variant_new("(s)", remote_addr));
2716 bt_hf_info.state = BT_HF_STATE_CONNECTED;
2718 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) == 0) {
2719 DBG("BT device state is : 0x%X", bt_device_state);
2720 bt_device_state |= VCONFKEY_BT_DEVICE_AG_CONNECTED;
2721 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
2722 ERR("vconf_set_int failed");
2725 ERR("vconf_get_int failed");
2728 g_idle_add(__bt_hf_agent_release_after, NULL);
2733 bt_hf_info.state = BT_HF_STATE_CONNECTED;
2735 __bt_hf_agent_sco_accept(&bt_hf_info);
2737 __bt_hf_agent_start_watch(&bt_hf_info);
2739 remote_addr = bt_hf_info.remote_addr;
2741 INFO_SECURE("Address is : %s", remote_addr);
2742 INFO_C("Connected [HF role]");
2744 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) == 0) {
2745 DBG("BT device state is : 0x%X", bt_device_state);
2746 bt_device_state |= VCONFKEY_BT_DEVICE_AG_CONNECTED;
2747 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
2748 ERR("vconf_set_int failed");
2751 ERR("vconf_get_int failed");
2754 conn = __bt_hf_get_gdbus_connection();
2756 ERR("Unable to get connection");
2760 __bt_hf_agent_emit_signal(conn,
2761 BT_HF_AGENT_OBJECT_PATH,
2762 BT_HF_SERVICE_INTERFACE,
2764 g_variant_new("(s)", remote_addr));
2766 /* Request the call list and launch call app if required */
2767 __bt_hf_agent_handle_call_list(&bt_hf_info);
2774 static gboolean __bt_hf_agent_connection(gint32 fd, const gchar *obj_path)
2778 struct sockaddr_remote address;
2779 socklen_t address_len;
2780 bt_hf_info.path = g_strdup(obj_path);
2782 INFO_C("**** New HFP connection ****");
2784 is_hf_connected = TRUE;
2786 address_len = sizeof(address);
2787 if (getpeername(fd, (struct sockaddr *) &address, &address_len) != 0)
2788 ERR("BD_ADDR is NULL");
2790 DBG("RFCOMM connection for HFP is completed. Fd = [%d]\n", fd);
2792 bt_hf_info.io_chan = g_io_channel_unix_new(bt_hf_info.fd);
2793 flags = g_io_channel_get_flags(bt_hf_info.io_chan);
2795 flags &= ~G_IO_FLAG_NONBLOCK;
2796 flags &= G_IO_FLAG_MASK;
2797 g_io_channel_set_flags(bt_hf_info.io_chan, flags, NULL);
2798 g_io_channel_set_encoding(bt_hf_info.io_chan, NULL, NULL);
2799 g_io_channel_set_buffered(bt_hf_info.io_chan, FALSE);
2801 bt_hf_info.remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2802 __bt_convert_addr_type_to_rev_string(bt_hf_info.remote_addr,
2803 address.remote_bdaddr.b);
2805 g_idle_add(__bt_agent_request_service_level_conn, NULL);
2810 static void __bt_hf_agent_indicator_free(gpointer mem)
2815 static gboolean __bt_hf_agent_release(void)
2817 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
2818 GDBusConnection *conn;
2820 if (bt_hf_info.state == BT_HF_STATE_DISCONNECTED) {
2821 ERR("hf is already disconnected");
2825 if (bt_hf_info.indies) {
2826 g_slist_free_full(bt_hf_info.indies, __bt_hf_agent_indicator_free);
2827 bt_hf_info.indies = NULL;
2830 if (bt_hf_info.io_chan) {
2831 g_io_channel_shutdown(bt_hf_info.io_chan, TRUE, NULL);
2832 g_io_channel_unref(bt_hf_info.io_chan);
2833 bt_hf_info.io_chan = NULL;
2836 if (bt_hf_info.sco_watch_id > 0) {
2837 g_source_remove(bt_hf_info.sco_watch_id);
2838 bt_hf_info.sco_watch_id = 0;
2841 bt_hf_info.state = BT_HF_STATE_DISCONNECTED;
2843 __bt_hf_agent_release_queue();
2845 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) == 0) {
2846 DBG("BT device state is : 0x%X", bt_device_state);
2847 bt_device_state ^= VCONFKEY_BT_DEVICE_AG_CONNECTED;
2848 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
2849 ERR("vconf_set_int failed");
2852 ERR("vconf_get_int failed");
2855 __bt_hf_agent_stop_watch(&bt_hf_info);
2856 conn = __bt_hf_get_gdbus_connection();
2858 ERR("Unable to get connection");
2862 __bt_hf_agent_emit_signal(conn,
2863 BT_HF_AGENT_OBJECT_PATH,
2864 BT_HF_SERVICE_INTERFACE,
2866 g_variant_new("(s)", bt_hf_info.remote_addr));
2868 g_free(bt_hf_info.path);
2869 bt_hf_info.path = NULL;
2871 g_free(bt_hf_info.remote_addr);
2872 bt_hf_info.remote_addr = NULL;
2874 is_hf_connected = FALSE;
2879 static gboolean __bt_hf_agent_connection_release(void)
2881 return __bt_hf_agent_release();
2884 static int __bt_hf_register_profile(const char *uuid, uint16_t version,
2885 const char *name, const char *object, uint16_t features)
2890 GError *error = NULL;
2891 GVariantBuilder *builder;
2894 proxy = __bt_hf_gdbus_get_service_proxy(BLUEZ_SERVICE_NAME,
2895 "/org/bluez", BLUEZ_PROFILE_MGMT_INTERFACE);
2898 return BT_HF_AGENT_ERROR_INTERNAL;
2900 path = g_strdup(BT_HF_BLUEZ_OBJECT_PATH);
2902 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2904 g_variant_builder_add(builder, "{sv}",
2905 "Name", g_variant_new("s",
2907 g_variant_builder_add(builder, "{sv}",
2908 "Version", g_variant_new("q", version));
2910 g_variant_builder_add(builder, "{sv}",
2911 "features", g_variant_new("q", features));
2913 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
2914 g_variant_new("(osa{sv})", path,
2915 HFP_HF_UUID, builder),
2916 G_DBUS_CALL_FLAGS_NONE, -1,
2919 g_variant_builder_unref(builder);
2922 /* dBUS-RPC is failed */
2923 ERR("dBUS-RPC is failed");
2924 if (error != NULL) {
2925 /* dBUS gives error cause */
2926 ERR("D-Bus API failure: errCode[%x], message[%s]",
2927 error->code, error->message);
2928 g_clear_error(&error);
2931 return BT_HF_AGENT_ERROR_INTERNAL;
2933 g_variant_unref(ret);
2937 return BT_HF_AGENT_ERROR_NONE;
2940 static void __bt_hf_agent_register(void)
2945 uint16_t version = hf_ver;
2946 uint16_t features = bt_hf_info.feature;
2948 gchar *path = g_strdup(BT_HF_BLUEZ_OBJECT_PATH);
2949 name = g_strdup("Hands-Free");
2951 ret = __bt_hf_register_profile(HFP_HF_UUID, version, name, path,
2954 ERR("Error in register");
2963 static void __bt_hf_agent_unregister(void)
2967 gchar *path = g_strdup(BT_HF_BLUEZ_OBJECT_PATH);
2970 __bt_hf_agent_gdbus_method_send(BLUEZ_SERVICE_NAME,
2971 g_variant_new("(o)", path),
2972 BLUEZ_HF_INTERFACE_NAME,
2984 static void __bt_hf_agent_filter_cb(GDBusConnection *connection,
2985 const gchar *sender_name,
2986 const gchar *object_path,
2987 const gchar *interface_name,
2988 const gchar *signal_name,
2989 GVariant *parameters,
2995 GVariant *optional_param;
2997 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
2999 g_variant_get(parameters, "(&o@a{sa{sv}})",
3000 &path, &optional_param);
3002 ERR("Invalid adapter path");
3006 if (strcasecmp(path, DEFAULT_ADAPTER_OBJECT_PATH) == 0) {
3007 g_obj_path = g_strdup(path);
3008 INFO("Adapter Path = [%s]", path);
3009 __bt_hf_agent_register();
3011 } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
3012 g_variant_get(parameters, "(&o@as)", &path, &optional_param);
3014 __bt_hf_agent_unregister();
3019 static void __bt_hf_agent_dbus_init(void)
3021 GDBusConnection *conn;
3025 conn = __bt_hf_get_gdbus_connection();
3027 ERR("Error in creating the gdbus connection\n");
3030 if (!__bt_hf_register_profile_methods()) {
3031 ERR("Error in register_profile_methods\n");
3035 owner_sig_id = g_dbus_connection_signal_subscribe(conn,
3036 NULL, BT_MANAGER_INTERFACE, NULL, NULL, NULL, 0,
3037 __bt_hf_agent_filter_cb, NULL, NULL);
3042 static void __bt_hf_agent_dbus_deinit(void)
3045 if (service_gproxy) {
3046 g_object_unref(service_gproxy);
3047 service_gproxy = NULL;
3051 if (owner_sig_id != -1)
3052 g_dbus_connection_signal_unsubscribe(gdbus_conn,
3055 g_object_unref(gdbus_conn);
3061 static int _hf_agent_answer_call(GDBusMethodInvocation *context)
3066 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3067 ERR("HF not Connected");
3068 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3070 bt_hf_info.context = context;
3072 ret = __bt_hf_send_only(&bt_hf_info, BT_HF_ANSWER_CALL,
3073 sizeof(BT_HF_ANSWER_CALL) - 1);
3075 return BT_HF_AGENT_ERROR_INTERNAL;
3078 return BT_HF_AGENT_ERROR_NONE;
3082 static int _hf_agent_terminate_call(GDBusMethodInvocation *context)
3087 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3088 ERR("HF not Connected");
3089 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3092 bt_hf_info.context = context;
3094 ret = __bt_hf_send_only(&bt_hf_info, BT_HF_END_CALL,
3095 sizeof(BT_HF_END_CALL) - 1);
3097 return BT_HF_AGENT_ERROR_INTERNAL;
3100 return BT_HF_AGENT_ERROR_NONE;
3103 static int _hf_agent_dial_no(GDBusMethodInvocation *context, char *no)
3106 char buf[BT_MAX_TEL_NUM_STR + 6] = {0};
3108 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3109 ERR("HF not Connected");
3110 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3113 bt_hf_info.context = context;
3115 if (strlen(no) > 0) {
3116 snprintf(buf, sizeof(buf), BT_HF_DIAL_NO, no);
3118 if (strstr(prev_cmd, "ATD") && bt_hf_info.ciev_call_status == 0
3119 && bt_hf_info.ciev_call_setup_status == 0) {
3120 INFO("RAD POPUP CANCEL CASE. send ATD w/o response - KOR REQUEST");
3121 ret = __bt_hf_send_only_without_queue(&bt_hf_info, buf, strlen(buf));
3125 ret = __bt_hf_send_only(&bt_hf_info, buf, strlen(buf));
3128 /* prev_cmd is meant for only meant for ATD & AT+BLDN Error handling */
3129 snprintf(prev_cmd, BT_HF_CMD_BUF_SIZE, "%s", buf);
3132 return BT_HF_AGENT_ERROR_INTERNAL;
3134 return BT_HF_AGENT_ERROR_NONE;
3137 /* prev_cmd is meant for only meant for ATD & AT+BLDN Error handling */
3138 snprintf(prev_cmd, BT_HF_CMD_BUF_SIZE, "%s", BT_HF_REDIAL);
3140 ret = __bt_hf_send_only(&bt_hf_info, BT_HF_REDIAL,
3141 sizeof(BT_HF_REDIAL) - 1);
3143 return BT_HF_AGENT_ERROR_INTERNAL;
3145 return BT_HF_AGENT_ERROR_NONE;
3148 static int _hf_agent_voice_recognition(GDBusMethodInvocation *context, unsigned int status)
3153 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3154 ERR("HF not Connected");
3155 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3158 snprintf(buf, sizeof(buf), BT_HF_VOICE_RECOGNITION, status);
3160 bt_hf_info.context = context;
3162 ret = __bt_hf_send_only(&bt_hf_info, buf, strlen(buf));
3164 return BT_HF_AGENT_ERROR_INTERNAL;
3167 return BT_HF_AGENT_ERROR_NONE;
3170 static int _hf_agent_set_speaker_gain(GDBusMethodInvocation *context, unsigned int gain)
3175 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3176 ERR("HF not Connected");
3177 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3180 if (gain > BT_HF_MAX_SPEAKER_GAIN)
3181 return BT_HF_AGENT_ERROR_INVALID_PARAM;
3183 snprintf(buf, sizeof(buf), BT_HF_SPEAKER_GAIN, gain);
3185 bt_hf_info.context = context;
3187 ret = __bt_hf_send_only(&bt_hf_info, buf,
3190 return BT_HF_AGENT_ERROR_INTERNAL;
3192 return BT_HF_AGENT_ERROR_NONE;
3196 static int _hf_agent_send_dtmf(GDBusMethodInvocation *context, char *dtmf)
3201 if (strlen(dtmf) <= 0)
3202 return BT_HF_AGENT_ERROR_INVALID_PARAM;
3204 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3205 ERR("HF not Connected");
3206 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3209 snprintf(buf, sizeof(buf), BT_HF_DTMF, dtmf);
3211 bt_hf_info.context = context;
3213 ret = __bt_hf_send_only(&bt_hf_info, buf, strlen(buf));
3215 return BT_HF_AGENT_ERROR_INTERNAL;
3218 return BT_HF_AGENT_ERROR_NONE;
3222 static int _hf_agent_send_3way_cmd(GDBusMethodInvocation *context, char *cmd)
3226 if (strlen(cmd) <= 0)
3227 return BT_HF_AGENT_ERROR_INVALID_PARAM;
3229 bt_hf_info.context = context;
3231 ret = __bt_hf_send_only(&bt_hf_info, cmd,
3234 return BT_HF_AGENT_ERROR_INTERNAL;
3236 return BT_HF_AGENT_ERROR_NONE;
3239 static gboolean bt_hf_agent_sco_disconnect(void)
3242 GDBusConnection *conn;
3244 close(bt_hf_info.cli_sco_fd);
3245 bt_hf_info.cli_sco_fd = -1;
3247 DBG("Emit AudioDisconnected Signal");
3248 conn = __bt_hf_get_gdbus_connection();
3250 ERR("Unable to get connection");
3254 sco_audio_connected = BT_HF_AUDIO_DISCONNECTED;
3256 __bt_hf_agent_emit_signal(conn,
3257 BT_HF_AGENT_OBJECT_PATH,
3258 BT_HF_SERVICE_INTERFACE,
3259 "AudioDisconnected", NULL);
3264 static GVariant *bt_hf_agent_request_call_list(void)
3266 GSList *call_list = NULL;
3270 call_list = __bt_hf_get_call_list(&bt_hf_info);
3272 INFO("call list is NULL");
3276 var_data = __bt_hf_agent_get_call_status_info(call_list);
3277 __bt_hf_free_call_list(call_list);
3283 static int bt_hf_agent_send_at_cmd(GDBusMethodInvocation *context, char *atcmd)
3287 char cmd_buf[BT_MAX_TEL_NUM_STR + 20] = {0, };
3292 return BT_HF_AGENT_ERROR_INVALID_PARAM;
3294 if (bt_hf_info.state != BT_HF_STATE_CONNECTED)
3295 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3297 /* Should not send cmds if DUT has sent a command and waiting for response */
3298 if (prev_cmd[0] != 0) {
3299 INFO("DUT is waiting a respond for previous TX cmd. Skip sending.");
3300 return BT_HF_AGENT_ERROR_INTERNAL;
3303 strncpy(cmd_buf, atcmd, sizeof(cmd_buf) - 2);
3304 strncat(cmd_buf, "\r", (sizeof(cmd_buf) - 1) - strlen(cmd_buf));
3306 bt_hf_info.context = context;
3308 ret = __bt_hf_send_only(&bt_hf_info, cmd_buf, strlen(cmd_buf));
3310 return BT_HF_AGENT_ERROR_INTERNAL;
3313 return BT_HF_AGENT_ERROR_NONE;
3316 static uint32_t __bt_hf_agent_get_hf_features(void)
3319 uint32_t hf_features = BT_HF_FEATURE_EC_ANDOR_NR |
3320 BT_HF_FEATURE_CALL_WAITING_AND_3WAY |
3321 BT_HF_FEATURE_CLI_PRESENTATION |
3322 BT_HF_FEATURE_VOICE_RECOGNITION |
3323 BT_HF_FEATURE_REMOTE_VOLUME_CONTROL |
3324 BT_HF_FEATURE_ENHANCED_CALL_STATUS |
3325 BT_HF_FEATURE_CODEC_NEGOTIATION;
3327 hf_ver = HFP_VERSION_1_6;
3334 struct sigaction sa;
3335 uint32_t hf_features;
3337 INFO("Starting Bluetooth HF agent");
3339 hf_features = __bt_hf_agent_get_hf_features();
3340 bt_hf_info.feature = (uint16_t) hf_features & 0x3F;
3342 memset(&sa, 0, sizeof(sa));
3343 sa.sa_flags = SA_NOCLDSTOP;
3344 sa.sa_handler = __bt_hf_agent_sigterm_handler;
3345 sigaction(SIGTERM, &sa, NULL);
3347 /* Temporarily, block the below signal for debugging */
3348 // sigaction(SIGSEGV, &sa, NULL);
3349 // sigaction(SIGABRT, &sa, NULL);
3350 gmain_loop = g_main_loop_new(NULL, FALSE);
3352 if (gmain_loop == NULL) {
3353 ERR("GMainLoop create failed\n");
3354 return EXIT_FAILURE;
3357 __bt_hf_agent_dbus_init();
3358 g_main_loop_run(gmain_loop);
3360 __bt_hf_agent_dbus_deinit();
3363 g_main_loop_unref(gmain_loop);
3365 INFO("Terminating Bluetooth HF agent");