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");
577 object_id = g_dbus_connection_register_object(conn, path,
578 node_info->interfaces[0],
581 if (object_id == 0) {
582 ERR("Failed to register: %s", error->message);
584 g_dbus_node_info_unref(node_info);
589 g_dbus_node_info_unref(node_info);
591 node_info = __bt_hf_create_method_node_info(hf_agent_introspection_xml);
592 if (node_info == NULL)
595 path = g_strdup(BT_HF_AGENT_OBJECT_PATH);
596 DBG("path is [%s]", path);
598 object_id = g_dbus_connection_register_object(conn, path,
599 node_info->interfaces[0],
602 if (object_id == 0) {
604 ERR("Failed to register: %s", error->message);
607 g_dbus_node_info_unref(node_info);
612 g_dbus_node_info_unref(node_info);
618 static GDBusProxy *__bt_hf_gdbus_init_service_proxy(const gchar *service,
619 const gchar *path, const gchar *interface)
625 GDBusConnection *conn;
627 conn = __bt_hf_get_gdbus_connection();
629 ERR("Unable to get connection");
633 proxy = g_dbus_proxy_new_sync(conn,
634 G_DBUS_PROXY_FLAGS_NONE, NULL,
636 interface, NULL, &err);
640 ERR("Unable to create proxy: %s", err->message);
650 static GDBusProxy *__bt_hf_gdbus_get_service_proxy(const gchar *service,
651 const gchar *path, const gchar *interface)
653 return (service_gproxy) ? service_gproxy :
654 __bt_hf_gdbus_init_service_proxy(service,
658 static char __bt_hf_agent_get_tx_power(char *address)
662 GError *error = NULL;
663 char result = READ_TX_POWER_MIN; /* default minimum */
665 proxy = __bt_hf_gdbus_get_service_proxy(BLUEZ_SERVICE_NAME, g_obj_path,
666 BLUEZ_HF_INTERFACE_NAME);
668 ERR("Proxy is NULL");
672 ret = g_dbus_proxy_call_sync(proxy,
673 "GetTxPowerLevel", g_variant_new("(s)", address),
674 G_DBUS_CALL_FLAGS_NONE, -1,
677 ERR("DBus is failed");
679 /* Dbus gives error cause */
680 ERR("D-Bus API failure: errCode[%x], message[%s]",
681 error->code, error->message);
682 g_clear_error(&error);
686 g_variant_get(ret, "(y)", &result);
687 DBG("TX power level = %d", result);
688 g_variant_unref(ret);
692 static int __bt_hf_agent_gdbus_method_send(const char *service,
693 GVariant *path, const char *interface,
700 GError *error = NULL;
702 proxy = __bt_hf_gdbus_get_service_proxy(service, g_obj_path, interface);
704 return BT_HF_AGENT_ERROR_INTERNAL;
706 ret = g_dbus_proxy_call_sync(proxy,
708 G_DBUS_CALL_FLAGS_NONE, -1,
711 /* dBUS-RPC is failed */
712 ERR("dBUS-RPC is failed");
714 /* dBUS gives error cause */
715 ERR("D-Bus API failure: errCode[%x], message[%s]",
716 error->code, error->message);
718 g_clear_error(&error);
720 return BT_HF_AGENT_ERROR_INTERNAL;
723 g_variant_unref(ret);
725 return BT_HF_AGENT_ERROR_NONE;
728 static void __bt_hf_agent_release_queue(void)
731 bt_hf_agent_send_at_info *cmd;
734 len = g_slist_length(bt_hf_info.cmd_send_queue);
735 for (i = 0; i < len; ++i) {
736 cmd = g_slist_nth_data(bt_hf_info.cmd_send_queue, i);
737 if (cmd && cmd->context) {
738 DBG("Pending context found for %.6s[%d]",
739 cmd->at_cmd, cmd->id);
740 err = __bt_hf_agent_set_error(BT_HF_AGENT_ERROR_INTERNAL);
741 g_dbus_method_invocation_return_gerror(cmd->context, err);
744 if (cmd && cmd->timer_id)
745 g_source_remove(cmd->timer_id);
747 g_slist_free(bt_hf_info.cmd_send_queue);
748 bt_hf_info.cmd_send_queue = NULL;
752 static gboolean __bt_hf_monitor_timer_cb(gpointer data)
755 bt_hf_agent_send_at_info *cmd = data;
756 ERR_C("Monitor timer came becuase of timeout for sendflag %d, %s",
757 send_flag, cmd->at_cmd);
758 /* In the case of ATD, we have to inform the remote to end the call */
759 if (strstr(cmd->at_cmd, "ATD") || strstr(cmd->at_cmd, "BLDN")) {
760 INFO_C("Sending CHUP for remote call termination");
761 __bt_hf_send_only_without_queue(&bt_hf_info, BT_HF_END_CALL,
762 strlen(BT_HF_END_CALL));
763 /* Here there is a high posisbility that we do not get response
764 * for CHUP. Hence we need to decrement send_flag to process further
765 * incomming packets because we already incremented it in the CHUP case. */
769 /* In the case of ATD, prev_cmd will be always ATD, because we will not
770 * allow further commands. For safer side again set prev_cmd as ATD */
771 strncpy(prev_cmd, "ATD\0", BT_HF_CMD_BUF_SIZE - 1);
773 hf_handle_rx_at_cmd(&bt_hf_info, BT_HF_ERROR_RESP);
781 gboolean __bt_hf_agent_add_queue(GDBusMethodInvocation *context, char *at,
782 int count, gboolean pending_flag)
785 if (bt_hf_info.slc == FALSE)
789 DBG("*** Add Pending queue request for = %s **** ", at);
791 DBG("Add Pending queue respnse for = %s ", at);
793 bt_hf_agent_send_at_info *cmd = g_new0(bt_hf_agent_send_at_info, 1);
795 memcpy(cmd->at_cmd, at, count);
797 cmd->context = context;
798 cmd->pending = pending_flag;
799 bt_hf_info.cmd_send_queue = g_slist_append(bt_hf_info.cmd_send_queue,
801 len = g_slist_length(bt_hf_info.cmd_send_queue);
802 for (i = 0; i < len; ++i) {
803 cmd = g_slist_nth_data(bt_hf_info.cmd_send_queue, i);
804 DBG("Q> %.6s[%d]", cmd->at_cmd, cmd->id);
807 /* We need to have base timeout + tolerance value to process other request */
808 if (strstr(at, "ATD") || strstr(at, "BLDN")) {
809 /* Android 15 seconds timeout in case of ATD timeout in flight mode */
810 cmd->timer_id = g_timeout_add_seconds(BT_HF_COMMAND_TIMEOUT * 5 + len,
811 __bt_hf_monitor_timer_cb, cmd);
813 cmd->timer_id = g_timeout_add_seconds(BT_HF_COMMAND_TIMEOUT + len,
814 __bt_hf_monitor_timer_cb, cmd);
820 Below methods exposed to Applicatoins
822 static gboolean __bt_hf_agent_emit_signal(GDBusConnection *connection,
823 const char *path, const char *interface,
824 const char *signal_name, GVariant *param)
826 GError *error = NULL;
828 ret = g_dbus_connection_emit_signal(connection,
830 interface, signal_name,
834 /* dBUS gives error cause */
835 ERR("D-Bus API failure: errCode[%x], message[%s]",
836 error->code, error->message);
837 g_clear_error(&error);
840 INFO_C("Emit Signal [%s]", signal_name);
845 static gboolean __bt_hf_agent_emit_property_changed(
846 GDBusConnection *connection,
848 const char *interface,
854 GError *error = NULL;
856 ret = g_dbus_connection_emit_signal(connection,
857 NULL, path, interface,
859 g_variant_new("s(v)", name, property),
863 /* dBUS gives error cause */
864 ERR("D-Bus API failure: errCode[%x], message[%s]",
865 error->code, error->message);
866 g_clear_error(&error);
874 Below methods exposed to Bluez
877 static void __bt_hf_agent_handle_ind_change(bt_hf_agent_info_t *bt_hf_info,
878 guint index, gint value)
880 GDBusConnection *conn;
882 struct indicator *ind = g_slist_nth_data(bt_hf_info->indies, index - 1);
884 ERR("Indicator is NULL");
891 conn = __bt_hf_get_gdbus_connection();
893 ERR("Unable to get connection");
897 INFO("Indicator name is %s, value = [%d]", name, value);
898 if (!strcmp(name, "\"call\"")) {
899 bt_hf_info->ciev_call_status = value;
901 __bt_hf_agent_emit_signal(conn,
902 BT_HF_AGENT_OBJECT_PATH,
903 BT_HF_SERVICE_INTERFACE,
904 "CallStarted", NULL);
905 bt_hf_info->is_dialing = FALSE;
906 bt_hf_info->call_active = TRUE;
907 } else if (bt_hf_info->call_active) {
908 __bt_hf_agent_emit_signal(conn,
909 BT_HF_AGENT_OBJECT_PATH,
910 BT_HF_SERVICE_INTERFACE,
912 bt_hf_info->call_active = FALSE;
915 } else if (!strcmp(name, "\"callsetup\"")) {
916 bt_hf_info->ciev_call_setup_status = value;
917 if (value == 0 && bt_hf_info->is_dialing) {
918 __bt_hf_agent_emit_signal(conn,
919 BT_HF_AGENT_OBJECT_PATH,
920 BT_HF_SERVICE_INTERFACE,
923 bt_hf_info->is_dialing = FALSE;
924 } else if (!bt_hf_info->is_dialing && value > 0)
925 bt_hf_info->is_dialing = TRUE;
927 if (bt_hf_info->ciev_call_status == 0 &&
928 bt_hf_info->ciev_call_setup_status == 0)
929 __bt_hf_agent_emit_signal(gdbus_conn,
930 BT_HF_AGENT_OBJECT_PATH,
931 BT_HF_SERVICE_INTERFACE,
934 } else if (!strcmp(name, "\"callheld\"")) {
935 if (value == 0) { /* No calls held*/
936 __bt_hf_agent_emit_signal(conn,
937 BT_HF_AGENT_OBJECT_PATH,
938 BT_HF_SERVICE_INTERFACE,
941 } else if (value == 1) {
942 /*Call is placed on hold or active/held calls swapped */
943 __bt_hf_agent_emit_signal(conn,
944 BT_HF_AGENT_OBJECT_PATH,
945 BT_HF_SERVICE_INTERFACE,
946 "CallsSwapped", NULL);
947 bt_hf_info->is_dialing = FALSE;
949 /*Call on hold, no active call*/
950 __bt_hf_agent_emit_signal(conn,
951 BT_HF_AGENT_OBJECT_PATH,
952 BT_HF_SERVICE_INTERFACE,
954 bt_hf_info->is_dialing = FALSE;
956 } else if (!strcmp(name, "\"service\""))
957 __bt_hf_agent_emit_property_changed(conn,
958 BT_HF_AGENT_OBJECT_PATH,
959 BT_HF_SERVICE_INTERFACE,
960 "RegistrationStatus",
961 g_variant_new("(q)", value));
962 else if (!strcmp(name, "\"signal\""))
963 __bt_hf_agent_emit_property_changed(conn,
964 BT_HF_AGENT_OBJECT_PATH,
965 BT_HF_SERVICE_INTERFACE, "SignalStrength",
966 g_variant_new("(q)", value));
967 else if (!strcmp(name, "\"roam\""))
968 __bt_hf_agent_emit_property_changed(conn,
969 BT_HF_AGENT_OBJECT_PATH,
970 BT_HF_SERVICE_INTERFACE, "RoamingStatus",
971 g_variant_new("(q)", value));
972 else if (!strcmp(name, "\"battchg\""))
973 __bt_hf_agent_emit_property_changed(conn,
974 BT_HF_AGENT_OBJECT_PATH,
975 BT_HF_SERVICE_INTERFACE, "BatteryCharge",
976 g_variant_new("(q)", value));
980 static gboolean __bt_hf_agent_launch_call_app(const char *launch_type,
987 app_manager_is_running(CALL_APP_ID, &is_running);
991 DBG_SECURE("Launch type = %s, number(%s)", launch_type, number);
995 ERR("bundle_create() Failed");
999 bundle_add(b, "launch-type", launch_type);
1001 if (strlen(number) != 0)
1002 bundle_add(b, "number", number);
1004 bundle_add(b, "carrier-type", "BT");
1005 DBG("For 3G, carrier-type: BT has been added");
1007 aul_launch_app(CALL_APP_ID, b);
1015 static void __bt_hf_agent_handle_voice_activation(gint value)
1017 GDBusConnection *conn;
1019 conn = __bt_hf_get_gdbus_connection();
1021 ERR("Unable to get connection");
1025 __bt_hf_agent_emit_signal(conn, BT_HF_AGENT_OBJECT_PATH,
1026 BT_HF_SERVICE_INTERFACE,
1028 g_variant_new("(i)", value));
1033 static void __bt_hf_agent_handle_speaker_gain(gint value)
1035 GDBusConnection *conn;
1037 conn = __bt_hf_get_gdbus_connection();
1039 ERR("Unable to get connection");
1043 __bt_hf_agent_emit_signal(conn, BT_HF_AGENT_OBJECT_PATH,
1044 BT_HF_SERVICE_INTERFACE, "VolumeSpeaker",
1045 g_variant_new("(i)", value));
1050 static int __bt_hf_agent_handle_ccwa(bt_hf_agent_info_t *bt_hf_info,
1053 GDBusConnection *conn;
1055 gchar number[BT_HF_CALLER_NUM_SIZE];
1057 char fmt_str[BT_HF_FMT_STR_SIZE];
1058 int len = strlen(buf);
1060 DBG("__bt_hf_agent_handle_ccwa +");
1061 if (len > BT_HF_CALLER_NUM_SIZE + 10) {
1062 ERR("buf len %d is too long", len);
1066 if ((ccwa = strstr(buf, "\r\n+CCWA"))) {
1067 snprintf(fmt_str, sizeof(fmt_str), "\r\n+CCWA: \"%%%ds", sizeof(number) - 1);
1068 if (sscanf(ccwa, fmt_str, number) == 1) {
1069 sep = strchr(number, '"');
1074 conn = __bt_hf_get_gdbus_connection();
1076 ERR("Unable to get connection");
1080 __bt_hf_agent_emit_signal(conn,
1081 BT_HF_AGENT_OBJECT_PATH,
1082 BT_HF_SERVICE_INTERFACE, "CallWaiting",
1083 g_variant_new("(s)", ccwa));
1085 ERR_SECURE("CCWA '%s' is Call Wating", buf);
1089 DBG("__bt_hf_agent_handle_ccwa -");
1093 static GSList *__bt_hf_prepare_call_list(const char *buf)
1095 GSList *call_list = NULL;
1100 char delim_sep[] = "\r\n";
1101 char temp_buf[BT_HF_DATA_BUF_SIZE] = {0,};
1103 hf_call_list_info_t *call_info;
1106 strncpy(temp_buf, buf, BT_HF_DATA_BUF_SIZE - 1);
1108 str = strtok_r(temp_buf, delim_sep, &sp);
1109 while (str != NULL) {
1110 if (!(strstr(str, "+CLCC:"))) {
1111 str = strtok_r(NULL, delim_sep, &sp);
1115 call_info = g_new0(hf_call_list_info_t, 1);
1117 sscanf(str, "+CLCC: %1d,%1d, %1d, %1d, %1d",
1118 &call_info->idx, &call_info->dir,
1119 &call_info->status, &call_info->mode,
1120 &call_info->multi_party);
1121 DBG("Index = [%d], Direction = [%d], Status = [%d], Mode = [%d], Multi_party = [%d]\n",
1122 call_info->idx, call_info->dir, call_info->status,
1123 call_info->mode, call_info->multi_party);
1125 ptr = strstr(str, "\"");
1127 temp = strstr(ptr + 1, "\"");
1130 DBG_SECURE("\tPhone Number = [%s]\n", ptr + 1);
1131 call_info->number = g_strdup(ptr + 1);
1133 if (strstr(temp + 1, ",")) {
1135 DBG("\tType = [%s]\n", temp);
1136 call_info->type = atoi(temp);
1140 /*In case of no phone no. in CLCC respnse, we should launch call application
1141 * with NULL string. By doing so "unknown" shall be displayed*/
1142 DBG("Phone number does not exist\n");
1143 call_info->number = g_strdup("");
1146 call_list = g_slist_append(call_list, call_info);
1147 str = strtok_r(NULL, delim_sep, &sp);
1153 static GSList *__bt_hf_get_call_list(bt_hf_agent_info_t *bt_hf_info)
1155 char buf[BT_HF_DATA_BUF_SIZE] = {0,};
1156 GSList *call_list = NULL;
1160 /* Send CLCC when the callsetup */
1161 __bt_hf_send_and_read(bt_hf_info, BT_HF_CALLLIST, buf,
1162 sizeof(BT_HF_CALLLIST) - 1);
1163 DBG_SECURE("Receive CLCC response buffer = '%s'", buf);
1165 call_list = __bt_hf_prepare_call_list(buf);
1170 static void __bt_hf_call_info_free(void *data)
1174 hf_call_list_info_t *call_info = data;
1175 g_free(call_info->number);
1181 static void __bt_hf_free_call_list(GSList *call_list)
1185 g_slist_free_full(call_list, __bt_hf_call_info_free);
1190 static void __bt_hf_launch_call_using_call_list(GSList *call_list,
1191 bt_hf_agent_info_t *bt_hf_info)
1194 const char *launch_type_str;
1195 hf_call_list_info_t *call_info;
1198 if (call_list == NULL)
1201 len = g_slist_length(call_list);
1204 call_info = g_slist_nth_data(call_list, len);
1206 /* Launch based on below conditions
1207 * DC - Active call which is initiated from H
1208 * MR - Alerting call which is initiated from H
1209 * MT - Incoming call */
1210 if (call_info->status == BT_HF_CALL_STAT_ACTIVE) {
1211 launch_type_str = "DC";
1213 if (call_info->dir == BT_HF_CALL_DIR_INCOMING)
1214 launch_type_str = "MT";
1216 launch_type_str = "MR";
1219 if (__bt_hf_agent_launch_call_app(launch_type_str,
1220 call_info->number) == FALSE)
1221 DBG("call app launching failed");
1226 static GVariant *__bt_hf_agent_get_call_status_info(GSList *call_list)
1232 hf_call_list_info_t *call_info;
1234 GVariantBuilder *builder;
1237 builder = g_variant_builder_new(G_VARIANT_TYPE("a(siiii)"));
1239 call_count = g_slist_length(call_list);
1240 DBG("Total call count = '%d'", call_count);
1242 while (call_count--) {
1243 call_info = g_slist_nth_data(call_list, call_count);
1244 INFO("Idx=%d, Dir=%d, status=%d, mode=%d, mparty=%d",
1245 call_info->idx, call_info->dir, call_info->status,
1246 call_info->mode, call_info->multi_party);
1247 caller = call_info->number;
1249 g_variant_builder_add(builder, "(siiii)",
1250 caller, call_info->dir, call_info->status,
1251 call_info->multi_party, call_info->idx);
1253 var_data = g_variant_new("(ia(siiii))",
1254 g_slist_length(call_list), builder);
1256 g_variant_builder_unref(builder);
1261 static void __bt_hf_clear_prev_sent_cmd(void)
1263 if (prev_cmd[0] != 0)
1264 ERR("No sent command");
1266 memset(prev_cmd, 0, BT_HF_CMD_BUF_SIZE);
1271 static void __bt_hf_agent_send_call_status_info(GSList *call_list)
1273 GDBusConnection *conn;
1276 var_data = __bt_hf_agent_get_call_status_info(call_list);
1277 conn = __bt_hf_get_gdbus_connection();
1279 ERR("Unable to get connection");
1284 __bt_hf_agent_emit_signal(conn,
1285 BT_HF_AGENT_OBJECT_PATH,
1286 BT_HF_SERVICE_INTERFACE,
1291 static void __bt_hf_agent_handle_call_list(bt_hf_agent_info_t *bt_hf_info)
1295 __bt_hf_lock_display(0);
1297 bt_hf_info->context = NULL;
1299 /* Send CLCC. The response will be handled in the handler */
1300 ret = __bt_hf_send_only(bt_hf_info, BT_HF_CALLLIST,
1301 sizeof(BT_HF_CALLLIST) - 1);
1303 ERR("Failed to send CLCC");
1305 __bt_hf_unlock_display();
1308 static void __bt_hf_agent_request_call_list_info(bt_hf_agent_info_t *bt_hf_info,
1312 struct indicator *ind = g_slist_nth_data(bt_hf_info->indies, index - 1);
1314 ERR("Indicator is NULL");
1318 DBG("name : %s", name);
1320 if ((strcmp(name, "\"callsetup\"") != 0) &&
1321 (strcmp(name, "\"call\"") != 0) &&
1322 (strcmp(name, "\"callheld\"") != 0))
1325 __bt_hf_lock_display(0);
1327 __bt_hf_agent_handle_call_list(bt_hf_info);
1329 __bt_hf_unlock_display();
1333 static gboolean __bt_hf_send_available_codec(bt_hf_agent_info_t *bt_hf_info, int send_only)
1335 gchar buf[BT_HF_DATA_BUF_SIZE];
1336 gchar cmd_buf[BT_HF_CMD_BUF_SIZE] = {0};
1339 snprintf(cmd_buf, sizeof(cmd_buf), BT_HF_AVAILABLE_CODEC,
1340 BT_HF_CODEC_ID_CVSD, BT_HF_CODEC_ID_MSBC);
1342 bt_hf_info->context = NULL;
1344 ret = __bt_hf_send_only(bt_hf_info, cmd_buf, strlen(cmd_buf));
1347 ret = __bt_hf_send_and_read(bt_hf_info, cmd_buf, buf,
1350 if (!ret || !strstr(buf, "OK"))
1356 static int _hf_agent_codec_setup(const char *addr, guint codec_id)
1361 ERR("g_obj_path is NULL\n");
1362 return BT_HF_AGENT_ERROR_INTERNAL;
1366 case BT_HF_CODEC_ID_CVSD:
1367 INFO("Set NB parameters");
1368 ret = __bt_hf_agent_gdbus_method_send(BLUEZ_SERVICE_NAME,
1369 g_variant_new("(ss)", "Handsfree", addr),
1370 BT_ADAPTER_INTERFACE,
1373 case BT_HF_CODEC_ID_MSBC:
1374 INFO("Set WBS parameters");
1375 ret = __bt_hf_agent_gdbus_method_send(BLUEZ_SERVICE_NAME,
1376 g_variant_new("(ss)", "Handsfree", addr),
1377 BT_ADAPTER_INTERFACE,
1378 "SetWbsParameters");
1381 ret = BT_HF_AGENT_ERROR_INTERNAL;
1382 ERR("Invalid Codec\n");
1387 ERR("Failed to setup the Codec\n");
1389 current_codec_id = codec_id;
1394 static void __bt_hf_agent_handle_codec_select(bt_hf_agent_info_t *bt_hf_info,
1397 gchar cmd_buf[BT_HF_CMD_BUF_SIZE] = {0};
1400 if (codec_id != BT_HF_CODEC_ID_CVSD && codec_id != BT_HF_CODEC_ID_MSBC) {
1401 INFO("Codec id doesn't match, so send available codec again");
1402 ret = __bt_hf_send_available_codec(bt_hf_info, 1);
1404 ERR("Failed to send avalable codec");
1408 /* HF should be ready accpet SCO connection before sending theresponse for
1409 "\r\n+BCS=>Codec ID\r\n", Keep the BT chip ready to recevie encoded SCO data */
1410 ret = _hf_agent_codec_setup(bt_hf_info->remote_addr, codec_id);
1412 snprintf(cmd_buf, sizeof(cmd_buf), BT_HF_CODEC_SELECT, codec_id);
1414 bt_hf_info->context = NULL;
1416 ret = __bt_hf_send_only(bt_hf_info, cmd_buf, strlen(cmd_buf));
1418 ERR("Failed to select the Codec");
1421 void __bt_hf_agent_print_at_buffer(char *message, const char *buf)
1425 char s[BT_HF_DATA_BUF_SIZE] = {0, };
1426 gboolean hide = FALSE;
1428 gboolean has_clcc = FALSE;
1429 gboolean has_clip = FALSE;
1430 gboolean has_ccwa = FALSE;
1433 strncpy(s, buf, BT_HF_DATA_BUF_SIZE - 1);
1435 has_clcc = strstr(buf, "CLCC:") ? TRUE : FALSE;
1436 if (has_clcc == TRUE)
1438 has_clip = strstr(buf, "+CLIP:") ? TRUE : FALSE;
1439 if (has_clip == TRUE)
1441 has_ccwa = strstr(buf, "+CCWA:") ? TRUE : FALSE;
1444 /* +XSAT: 11,DISC */
1445 xsat_ptr = strstr(s, "11,DISC,");
1447 xsat_ptr = xsat_ptr + 8;
1449 while (xsat_ptr[x] != '\0' && xsat_ptr[x] != '\r' && xsat_ptr[x] != '\n') {
1455 /* AT+XSAT=11,Q_CT,X,XXXX */
1456 xsat_ptr = strstr(s, "11,Q_CT,");
1458 xsat_ptr = xsat_ptr + 8;
1460 while (xsat_ptr[x] != '\0' && xsat_ptr[x] != '\r' && xsat_ptr[x] != '\n') {
1461 if (x > 1) /* ignore 0 and 1 position */
1468 while (s[i] != '\0') {
1469 if (s[i] == '\r' || s[i] == '\n') {
1473 hide = hide ? FALSE : TRUE;
1474 else if ((has_clcc || has_clip || has_ccwa) && hide) {
1482 INFO("%s Buffer = %s, Length = %d ", message, s, strlen(s));
1487 static int __bt_hf_agent_handler_ciev(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1489 gchar indicator[BT_HF_INDICATOR_DESCR_SIZE + 4];
1493 char fmt_str[BT_HF_FMT_STR_SIZE];
1495 DBG("++++++++ __bt_hf_agent_handler_ciev +++++++++");
1497 snprintf(fmt_str, sizeof(fmt_str), "\r\n+CIEV:%%%ds\r\n", sizeof(indicator) - 1);
1498 if (sscanf(buf, fmt_str, indicator) == 1) {
1499 sep = strchr(indicator, ',');
1502 index = atoi(indicator);
1504 __bt_hf_agent_handle_ind_change(bt_hf_info, index, value);
1506 if (bt_hf_info->ciev_call_status == 0 &&
1507 bt_hf_info->ciev_call_setup_status == 0)
1508 INFO("No active call");
1510 /* Request CLCC based on indicator change for call/callsetup/callHeld */
1511 __bt_hf_agent_request_call_list_info(bt_hf_info, index);
1513 DBG("--------- __bt_hf_agent_handler_ciev ------------");
1517 static void __bt_hf_agent_handle_ven_samsung(bt_hf_agent_info_t *bt_hf_info,
1518 gint app_id, const char *msg)
1520 /* Whomesoever wants need to handle it */
1521 char *sig_name = "SamsungXSAT";
1522 GDBusConnection *conn;
1524 conn = __bt_hf_get_gdbus_connection();
1526 ERR("Unable to get connection");
1530 __bt_hf_agent_emit_signal(conn,
1531 BT_HF_AGENT_OBJECT_PATH,
1532 BT_HF_SERVICE_INTERFACE,
1534 g_variant_new("(is)", app_id, msg));
1537 static int __bt_hf_agent_handler_ring(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1539 DBG("++++++++ __bt_hf_agent_handler_ring ++++++++");
1540 DBG("---------__bt_hf_agent_handler_ring --------");
1545 static int __bt_hf_agent_handler_clip(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1547 DBG("+++++++++ __bt_hf_agent_handler_clip ++++++++");
1548 DBG("---------__bt_hf_agent_handler_clip --------");
1553 static int __bt_hf_agent_handler_bvra(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1555 DBG("+++++++++ __bt_hf_agent_handler_bvra +++++++++");
1557 if (sscanf(buf, "\r\n+BVRA:%1d\r\n", &value) == 1)
1558 __bt_hf_agent_handle_voice_activation(value);
1560 DBG("---------__bt_hf_agent_handler_bvra --------");
1564 static int __bt_hf_agent_handler_bcs(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1567 DBG("+++++++++ __bt_hf_agent_handler_bcs +++++++++-");
1568 if (sscanf(buf, "\r\n+BCS:%3d\r\n", &codec_id))
1569 __bt_hf_agent_handle_codec_select(bt_hf_info, codec_id);
1571 DBG("---------__bt_hf_agent_handler_bcs --------");
1575 static int __bt_hf_agent_handler_vgs(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1578 DBG("+++++++++ __bt_hf_agent_handler_vgs +++++++++");
1579 if (sscanf(buf, "\r\n+VGS:%2d\r\n", &value))
1580 __bt_hf_agent_handle_speaker_gain(value);
1582 DBG("---------__bt_hf_agent_handler_vgs --------");
1587 static int __bt_hf_agent_handler_ccwa(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1589 DBG("+++++++++ __bt_hf_agent_handler_ccwa ++++++++");
1590 __bt_hf_agent_handle_ccwa(bt_hf_info, buf);
1591 DBG("---------__bt_hf_agent_handler_ccwa --------");
1597 static int __bt_hf_agent_handler_xsat(bt_hf_agent_info_t *bt_hf_info,
1601 char msg[BT_HF_DATA_BUF_SIZE];
1602 char fmt_str[BT_HF_CMD_BUF_SIZE];
1604 DBG("+++++++++ __bt_hf_agent_handler_xsat +++++++++");
1605 snprintf(fmt_str, sizeof(fmt_str), "\r\n+XSAT:%%d,%%%ds\r\n", sizeof(msg) - 1);
1606 if (sscanf(buf, fmt_str, &app_id, msg)) {
1607 if (app_id == 2 && strstr(msg, "READTXPOWER")) {
1608 char cmd_buf[BT_HF_CMD_BUF_SIZE * 2] = {0, };
1609 char power = __bt_hf_agent_get_tx_power(bt_hf_info->remote_addr);
1610 snprintf(cmd_buf, sizeof(cmd_buf), "AT+XSAT: 2,%d", power);
1611 bt_hf_agent_send_at_cmd(NULL, cmd_buf);
1613 __bt_hf_agent_handle_ven_samsung(bt_hf_info, app_id, msg);
1617 DBG("---------__bt_hf_agent_handler_xsat --------");
1622 static int __bt_hf_agent_handler_cme_error(bt_hf_agent_info_t *bt_hf_info,
1625 DBG("+++++++++ __bt_hf_agent_handler_cme_error ++++++++");
1627 GDBusConnection *conn;
1629 if (strstr(prev_cmd, "ATD") || strstr(prev_cmd, BT_HF_REDIAL)) {
1630 conn = __bt_hf_get_gdbus_connection();
1632 ERR("Unable to get connection");
1636 __bt_hf_agent_emit_signal(conn,
1637 BT_HF_AGENT_OBJECT_PATH,
1638 BT_HF_SERVICE_INTERFACE,
1643 __bt_hf_clear_prev_sent_cmd();
1648 static int __bt_hf_agent_handler_response_ok(bt_hf_agent_info_t *bt_hf_info,
1651 DBG("+++++++++ __bt_hf_agent_handler_response_ok ++++++++");
1653 __bt_hf_clear_prev_sent_cmd();
1658 static int __bt_hf_agent_handler_response_err(bt_hf_agent_info_t *bt_hf_info,
1661 DBG("+++++++++ __bt_hf_agent_handler_response_err ++++++++");
1662 GDBusConnection *conn;
1664 if (strstr(prev_cmd, "ATD") || strstr(prev_cmd, BT_HF_REDIAL)) {
1665 conn = __bt_hf_get_gdbus_connection();
1667 ERR("Unable to get connection");
1671 __bt_hf_agent_emit_signal(conn, BT_HF_AGENT_OBJECT_PATH,
1672 BT_HF_SERVICE_INTERFACE,
1676 __bt_hf_clear_prev_sent_cmd();
1681 static int __bt_hf_agent_handler_response_serr(bt_hf_agent_info_t *bt_hf_info,
1685 GDBusConnection *conn;
1687 if (strstr(prev_cmd, "ATD") || strstr(prev_cmd, BT_HF_REDIAL)) {
1688 conn = __bt_hf_get_gdbus_connection();
1690 ERR("Unable to get connection");
1694 __bt_hf_agent_emit_signal(conn, BT_HF_AGENT_OBJECT_PATH,
1695 BT_HF_SERVICE_INTERFACE,
1700 __bt_hf_clear_prev_sent_cmd();
1706 static int __bt_hf_agent_handler_clcc(bt_hf_agent_info_t *bt_hf_info, const char *buffer)
1709 GSList *call_list = NULL;
1710 DBG("+++++++++ __bt_hf_agent_handler_clcc ++++++++");
1711 DBG_SECURE("Receive CLCC response buffer = '%s'", buffer);
1713 __bt_hf_lock_display(0);
1715 call_list = __bt_hf_prepare_call_list(buffer);
1717 if (call_list == NULL)
1720 __bt_hf_launch_call_using_call_list(call_list, bt_hf_info);
1722 __bt_hf_agent_send_call_status_info(call_list);
1724 __bt_hf_free_call_list(call_list);
1727 __bt_hf_unlock_display();
1728 DBG("---------__bt_hf_agent_handler_clcc --------");
1732 static struct hf_event hf_event_callbacks[] = {
1733 { "\r\n+CIEV:", __bt_hf_agent_handler_ciev },
1734 { "\r\nRING", __bt_hf_agent_handler_ring },
1735 { "\r\n+CLIP:", __bt_hf_agent_handler_clip },
1736 { "\r\n+BVRA:", __bt_hf_agent_handler_bvra },
1737 { "\r\n+BCS:", __bt_hf_agent_handler_bcs },
1738 { "\r\n+VGS:", __bt_hf_agent_handler_vgs },
1739 { "\r\n+CCWA:", __bt_hf_agent_handler_ccwa },
1740 { "\r\n+XSAT:", __bt_hf_agent_handler_xsat },
1741 {"\r\n+CLCC:", __bt_hf_agent_handler_clcc },
1745 static struct hf_event hf_event_resp_callbacks[] = {
1746 { "\r\n+CME ERROR:", __bt_hf_agent_handler_cme_error },
1747 { "\r\nOK\r\n", __bt_hf_agent_handler_response_ok },
1748 { "ERROR", __bt_hf_agent_handler_response_err },
1749 { "SERR", __bt_hf_agent_handler_response_serr },
1753 bt_hf_agent_send_at_info *__bt_hf_agent_find_next(bt_hf_agent_info_t *bt_hf_info)
1757 bt_hf_agent_send_at_info *cmd;
1758 len = g_slist_length(bt_hf_info->cmd_send_queue);
1759 for (i = 0; i < len; ++i) {
1760 cmd = g_slist_nth_data(bt_hf_info->cmd_send_queue, i);
1761 DBG("F> %.6s[%d]", cmd->at_cmd, cmd->id);
1763 len = g_slist_length(bt_hf_info->cmd_send_queue);
1764 DBG("Context queue length = %d", len);
1768 cmd = g_slist_nth_data(bt_hf_info->cmd_send_queue, 0);
1770 bt_hf_info->cmd_send_queue = g_slist_remove(bt_hf_info->cmd_send_queue, cmd);
1771 DBG("NEXT[%d] Found %s, context = 0x%x, pending = %d", cmd->id,
1772 cmd->at_cmd, cmd->context, cmd->pending);
1776 DBG("**** Not found any pending command on list length %d ****", len);
1781 static int hf_handle_rx_at_cmd(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1783 struct hf_event *ev;
1785 bt_hf_agent_send_at_info *cmd = NULL;
1787 __bt_hf_agent_print_at_buffer("Processing [Rx cmd]:", buf);
1789 for (ev = hf_event_resp_callbacks; ev->cmd; ev++) {
1790 if (strstr(buf, ev->cmd)) {
1793 DBG("Send flag value = %d(after)", send_flag);
1794 ret = ev->callback(bt_hf_info, buf);
1801 for (ev = hf_event_callbacks; ev->cmd; ev++) {
1802 if (!strncmp(buf, ev->cmd, strlen(ev->cmd))) {
1803 ret = ev->callback(bt_hf_info, buf);
1811 cmd = __bt_hf_agent_find_next(bt_hf_info);
1813 if (cmd && cmd->context) {
1814 DBG("Pending context found for %.6s[%d]", cmd->at_cmd, cmd->id);
1815 g_dbus_method_invocation_return_value(cmd->context, NULL);
1817 g_source_remove(cmd->timer_id);
1823 cmd = __bt_hf_agent_find_next(bt_hf_info);
1825 if (cmd && cmd->pending && send_flag == 0) {
1826 DBG("Pending only found of %.6s[%d]", cmd->at_cmd, cmd->id);
1827 __bt_hf_send_only_without_queue(bt_hf_info,
1828 cmd->at_cmd, cmd->count);
1829 cmd->pending = FALSE;
1830 bt_hf_info->cmd_send_queue = g_slist_prepend(bt_hf_info->cmd_send_queue,
1832 DBG("Prepend %.6s[%d]", cmd->at_cmd, cmd->id);
1835 DBG("Pending free for %.6s[%d] - send_flag = %d",
1836 cmd->at_cmd, cmd->id, send_flag);
1838 g_source_remove(cmd->timer_id);
1844 /* Need to process further pending */
1845 cmd = __bt_hf_agent_find_next(bt_hf_info);
1847 if (cmd->pending && send_flag == 0) {
1848 DBG("2nd Pending only found of %.6s[%d]",
1849 cmd->at_cmd, cmd->id);
1850 __bt_hf_send_only_without_queue(bt_hf_info,
1851 cmd->at_cmd, cmd->count);
1852 cmd->pending = FALSE;
1854 bt_hf_info->cmd_send_queue = g_slist_prepend(bt_hf_info->cmd_send_queue,
1856 DBG("2nd Prepend %.6s[%d]", cmd->at_cmd, cmd->id);
1862 static int hf_handle_append_clcc_buff(char *cmd_buf, const char *buf)
1866 int cmd_buf_len = 0;
1867 char *pos_start, *pos_end;
1868 const char *datap = buf;
1870 cmd_buf_len = strlen(cmd_buf);
1871 buf_length = strlen(buf);
1872 DBG("buf_length = %d, cmd_buf_len = %d", buf_length, cmd_buf_len);
1874 if (buf_length > 0 && strstr(datap, "+CLCC")) {
1875 pos_start = strstr(datap, "\r\n");
1876 if (pos_start == NULL) {
1877 ERR("Invalid AT command signature..\n");
1881 pos_end = g_strrstr(datap, "+CLCC");
1882 if (pos_end == NULL) {
1883 ERR("Invalid AT command signature..\n");
1886 pos_end = strstr(pos_end, "\r\n");
1887 cmd_length = (pos_end - pos_start) + 2;
1888 INFO("CLCC balance Cmd Length = %d\n", cmd_length);
1889 memcpy(cmd_buf + cmd_buf_len, pos_start, cmd_length);
1890 cmd_buf[cmd_buf_len + cmd_length] = '\0';
1892 if (strstr(cmd_buf, "\r\nOK\r\n")) {
1893 pos_end = strstr(datap, "\r\nOK\r\n");
1894 cmd_length = (pos_end - pos_start);
1895 memcpy(cmd_buf + cmd_buf_len, pos_start, cmd_length);
1896 cmd_buf[cmd_buf_len + cmd_length] = '\0';
1897 INFO("New CLCC balance Cmd Length = %d", cmd_length);
1904 static int hf_handle_rx_at_buff(bt_hf_agent_info_t *bt_hf_info, const char *buf)
1908 char *pos_start, *pos_end;
1910 gchar cmd_buf[BT_HF_DATA_BUF_SIZE] = {0,};
1911 const char *datap = buf;
1913 __bt_hf_agent_print_at_buffer("[HF AT CMD] Recv >>>>>:", buf);
1915 buf_length = strlen(buf);
1917 while (buf_length > 0) {
1918 pos_start = strstr(datap, "\r\n");
1919 if (pos_start == NULL) {
1920 ERR("Invalid AT command start signature..\n");
1925 pos_end = strstr(datap, "\r\n");
1926 if (pos_end == NULL) {
1927 ERR("Invalid AT command end signature..\n");
1930 cmd_length = (pos_end - pos_start) + 2;
1931 DBG("Cmd Length = %d\n", cmd_length);
1933 memcpy(cmd_buf, pos_start, cmd_length);
1934 cmd_buf[cmd_length] = '\0';
1936 buf_length = buf_length - cmd_length;
1937 datap = datap + cmd_length - 2;
1939 /* We need to pass all the CLCC's together to its handler */
1940 if (strstr(cmd_buf, "+CLCC")) {
1941 tmp = hf_handle_append_clcc_buff(cmd_buf, datap);
1943 buf_length = buf_length - tmp;
1945 hf_handle_rx_at_cmd(bt_hf_info, cmd_buf);
1946 DBG("Pending buf_length = %d\n", buf_length);
1951 static gboolean __bt_hf_agent_data_cb(GIOChannel *chan, GIOCondition cond,
1952 bt_hf_agent_info_t *bt_hf_info)
1954 gchar buf[BT_HF_DATA_BUF_SIZE] = {0,};
1956 GError *gerr = NULL;
1957 gboolean recvd_ok = FALSE;
1958 gboolean recvd_error = FALSE;
1959 gboolean recvd_sec_error = FALSE;
1961 if (cond & (G_IO_ERR | G_IO_HUP)) {
1962 ERR("ERR or HUP on RFCOMM socket");
1963 INFO_C("Disconnected [HF role] [Terminated by remote dev]");
1964 is_hf_connected = FALSE;
1965 bt_hf_info->slc = FALSE;
1966 __bt_hf_agent_release();
1970 if (g_io_channel_read_chars(chan, buf, sizeof(buf) - 1, &read, &gerr)
1971 != G_IO_STATUS_NORMAL) {
1973 ERR("Read failed, cond = [%d], Err msg = [%s]",
1974 cond, gerr->message);
1980 recvd_ok = NULL != strstr(buf, BT_HF_OK_RESPONSE);
1981 recvd_error = NULL != strstr(buf, BT_HF_ERROR_RESPONSE);
1982 recvd_sec_error = NULL != strstr(buf, BT_HF_SEC_ERROR_RESPONSE);
1983 DBG("<-------Received data --send flag status = %d ----->", send_flag);
1985 /* Once service level connection is established we need to handle
1986 * all the intermediate AT commands */
1987 if (bt_hf_info->state != BT_HF_STATE_CONNECTED)
1991 strncat(global_buff, buf,
1992 (BT_AT_COMMAND_BUFFER_MAX - 1) - strlen(global_buff));
1993 if (!(recvd_ok || recvd_error || recvd_sec_error)) {
1994 __bt_hf_agent_print_at_buffer("Concat ()", global_buff);
1996 DBG("*** Received terminator.. process Rx buffer ***");
1997 hf_handle_rx_at_buff(bt_hf_info, global_buff);
1998 memset(global_buff, 0, sizeof(global_buff));
2001 INFO("*** Received Direct AT buffer packet handling ****");
2002 hf_handle_rx_at_buff(bt_hf_info, buf);
2007 static void __bt_hf_agent_start_watch(bt_hf_agent_info_t *bt_hf_info)
2009 bt_hf_info->watch_id = g_io_add_watch(bt_hf_info->io_chan,
2010 G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
2011 (GIOFunc) __bt_hf_agent_data_cb, bt_hf_info);
2014 static void __bt_hf_agent_stop_watch(bt_hf_agent_info_t *bt_hf_info)
2016 if (bt_hf_info->watch_id > 0) {
2017 g_source_remove(bt_hf_info->watch_id);
2018 bt_hf_info->watch_id = 0;
2022 static gboolean __bt_hf_channel_write(GIOChannel *io, gchar *data,
2029 status = g_io_channel_write_chars(io, data, count, &written,
2031 if (status != G_IO_STATUS_NORMAL)
2040 static gboolean __bt_hf_send_only_without_queue(bt_hf_agent_info_t *bt_hf_info,
2041 gchar *data, gsize count)
2043 GIOChannel *io_chan = bt_hf_info->io_chan;
2044 if (!__bt_hf_channel_write(io_chan, data, count))
2047 g_io_channel_flush(io_chan, NULL);
2049 if (count > 2 && data[2] == 'D') { /* ATDXXXXX */
2050 INFO("Send only buffer size =[%d] No len = %d - Send <<<<<| %s",
2051 count, count - 6, "ATDXXXXXXX");
2052 snprintf(prev_cmd, BT_HF_CMD_BUF_SIZE, "%s", data);
2054 INFO("No queue....Send only buffer size =[%d] - Send <<<<<| %s",
2059 /* DBG("Ref %d(after) on Send only buffer size =[%d] - Send <<<<<| %s",
2060 * send_flag, count, data); */
2065 static gboolean __bt_hf_send_only(bt_hf_agent_info_t *bt_hf_info, gchar *data,
2068 gboolean pending = FALSE;
2069 GIOChannel *io_chan = bt_hf_info->io_chan;
2074 __bt_hf_agent_add_queue(bt_hf_info->context, data, count, pending);
2079 if (!__bt_hf_channel_write(io_chan, data, count))
2082 g_io_channel_flush(io_chan, NULL);
2084 if (count > 2 && data[2] == 'D') /* ATDXXXXX */
2085 INFO("Send only buffer size =[%d] No len = %d - Send <<<<<| %s",
2086 count, count - 6, "ATDXXXXXXX");
2088 INFO("Send only buffer size =[%d] - Send <<<<<| %s", count, data);
2091 DBG("Ref %d(after) on Send only buffer size =[%d] - Send <<<<<| %s",
2092 send_flag, count, data);
2096 static gboolean __bt_hf_send_and_read(bt_hf_agent_info_t *bt_hf_info,
2097 gchar *data, gchar *response, gsize count)
2099 GIOChannel *io_chan = bt_hf_info->io_chan;
2101 gboolean recvd_ok = FALSE;
2102 gboolean recvd_error = FALSE;
2103 gboolean recvd_sec_error = FALSE;
2104 gchar *resp_buf = response;
2105 gsize toread = BT_HF_DATA_BUF_SIZE - 1;
2110 GDBusConnection *conn;
2112 /* Should not send cmds if DUT send a command and wait the response */
2113 if (prev_cmd[0] != 0) {
2114 INFO("DUT is waiting a respond for previous TX cmd. Skip sending.");
2118 memset(resp_buf, 0, BT_HF_DATA_BUF_SIZE);
2120 if (!__bt_hf_channel_write(io_chan, data, count))
2123 g_io_channel_flush(io_chan, NULL);
2125 __bt_hf_agent_print_at_buffer("[HF AT CMD] Send <<<<<:", data);
2127 fd = g_io_channel_unix_get_fd(io_chan);
2129 p.events = POLLIN | POLLERR | POLLHUP | POLLNVAL;
2131 /* Maximun 8 seconds of poll or 8 minus no of cmd received */
2132 for (i = 1; i <= MAX_WAITING_DELAY; i++) {
2133 DBG("Loop Counter = %d", i);
2135 err = poll(&p, 1, 1000);
2137 ERR("Loop Counter = %d, >>>> Poll error happen", i);
2139 } else if (err == 0) {
2140 INFO("Loop Counter = %d, >>>> Poll Timeout", i);
2143 if (p.revents & (POLLERR | POLLHUP | POLLNVAL)) {
2144 ERR("Loop Counter = %d, >> Poll ERR/HUP/INV (%d)",
2147 conn = __bt_hf_get_gdbus_connection();
2149 ERR("Unable to get connection");
2153 __bt_hf_agent_emit_signal(conn,
2154 BT_HF_AGENT_OBJECT_PATH,
2155 BT_HF_SERVICE_INTERFACE,
2157 g_variant_new("(s)",
2158 bt_hf_info->remote_addr));
2160 bt_hf_info->state = BT_HF_STATE_DISCONNECTED;
2164 if (p.revents & POLLIN) {
2165 rd_size = read(fd, resp_buf, toread);
2166 resp_buf[rd_size] = '\0';
2167 DBG_SECURE("size = %d, Buffer=[%s]", rd_size, resp_buf);
2168 recvd_ok = NULL != strstr(resp_buf, BT_HF_OK_RESPONSE);
2169 recvd_error = NULL != strstr(resp_buf, BT_HF_ERROR_RESPONSE);
2170 recvd_sec_error = NULL != strstr(resp_buf, BT_HF_SEC_ERROR_RESPONSE);
2172 resp_buf += rd_size;
2175 if (recvd_ok || recvd_error || recvd_sec_error) {
2176 DBG("Break Loop Counter = %d", i);
2182 /* Once service level connection is established we need to handle
2183 * all the intermediate AT commands */
2184 if (bt_hf_info->state == BT_HF_STATE_CONNECTED)
2185 hf_handle_rx_at_buff(bt_hf_info, response);
2189 static GSList *__bt_hf_parse_indicator_names(gchar *names, GSList *indices)
2191 struct indicator *ind;
2192 gchar *cur = names - 1;
2193 GSList *list = indices;
2196 DBG("Indicator buffer = %s", names);
2197 __bt_hf_agent_print_at_buffer("Indicator names :", names);
2198 while (cur != NULL) {
2200 next = strstr(cur, ",(");
2201 ind = g_new0(struct indicator, 1);
2202 g_strlcpy(ind->descr, cur, BT_HF_INDICATOR_DESCR_SIZE);
2203 ind->descr[(intptr_t) next - (intptr_t) cur] = '\0';
2204 list = g_slist_append(list, (gpointer) ind);
2205 cur = strstr(next + 1, ",(");
2210 static GSList *__bt_hf_parse_indicator_values(gchar *values, GSList *indices)
2213 struct indicator *ind;
2214 GSList *runner = indices;
2216 gchar *cur = values - 1;
2217 DBG("Indicator string = %s", values);
2218 __bt_hf_agent_print_at_buffer("Indicator values :", values);
2219 while (cur != NULL) {
2221 sscanf(cur, "%1d", &val);
2222 cur = strchr(cur, ',');
2223 ind = g_slist_nth_data(runner, 0);
2225 runner = g_slist_next(runner);
2230 static guint __bt_hf_get_hold_mpty_features(gchar *features)
2234 if (strstr(features, "0"))
2235 result |= BT_HF_CHLD_0;
2237 if (strstr(features, "1"))
2238 result |= BT_HF_CHLD_1;
2240 if (strstr(features, "1x"))
2241 result |= BT_HF_CHLD_1x;
2243 if (strstr(features, "2"))
2244 result |= BT_HF_CHLD_2;
2246 if (strstr(features, "2x"))
2247 result |= BT_HF_CHLD_2x;
2249 if (strstr(features, "3"))
2250 result |= BT_HF_CHLD_3;
2252 if (strstr(features, "4"))
2253 result |= BT_HF_CHLD_4;
2258 static gboolean __bt_hf_agent_sco_conn_cb(GIOChannel *chan, GIOCondition cond, gpointer user_data)
2260 bt_hf_agent_info_t *bt_hf_info = user_data;
2261 GDBusConnection *conn;
2264 if (cond & G_IO_NVAL)
2267 if (cond & (G_IO_HUP | G_IO_ERR)) {
2268 g_io_channel_shutdown(chan, TRUE, NULL);
2269 close(bt_hf_info->cli_sco_fd);
2270 g_io_channel_unref(chan);
2271 DBG("Emit AudioDisconnected Signal");
2273 sco_audio_connected = BT_HF_AUDIO_DISCONNECTED;
2275 conn = __bt_hf_get_gdbus_connection();
2277 ERR("Unable to get connection");
2280 __bt_hf_agent_emit_signal(conn,
2281 BT_HF_AGENT_OBJECT_PATH,
2282 BT_HF_SERVICE_INTERFACE,
2283 "AudioDisconnected", NULL);
2291 static gboolean __bt_agent_query_and_update_call_list(gpointer data)
2294 bt_hf_agent_info_t *bt_hf_info = data;
2296 if (bt_hf_info->cli_sco_fd >= 0)
2297 __bt_hf_agent_handle_call_list(bt_hf_info);
2299 INFO("SCO Audio is already disconnected");
2306 static gboolean __bt_hf_agent_sco_accept_cb(GIOChannel *chan, GIOCondition cond, gpointer user_data)
2308 bt_hf_agent_info_t *bt_hf_info = user_data;
2312 GDBusConnection *conn;
2314 INFO("Incoming SCO....");
2316 if (cond & G_IO_NVAL)
2319 sco_skt = g_io_channel_unix_get_fd(chan);
2321 if (cond & (G_IO_HUP | G_IO_ERR)) {
2326 cli_sco_sock = accept(sco_skt, NULL, NULL);
2327 if (cli_sco_sock < 0)
2330 bt_hf_info->cli_sco_fd = cli_sco_sock;
2332 sco_io = g_io_channel_unix_new(cli_sco_sock);
2333 g_io_channel_set_close_on_unref(sco_io, TRUE);
2334 g_io_channel_set_encoding(sco_io, NULL, NULL);
2335 g_io_channel_set_flags(sco_io, G_IO_FLAG_NONBLOCK, NULL);
2336 g_io_channel_set_buffered(sco_io, FALSE);
2338 g_io_add_watch(sco_io, G_IO_HUP | G_IO_ERR | G_IO_NVAL,
2339 __bt_hf_agent_sco_conn_cb, bt_hf_info);
2341 /* S-Voice app requires the AudioConnected signal earlier */
2342 DBG("Emit AudioConnected Signal");
2344 sco_audio_connected = BT_HF_AUDIO_CONNECTED;
2346 conn = __bt_hf_get_gdbus_connection();
2348 ERR("Unable to get connection");
2352 __bt_hf_agent_emit_signal(conn,
2353 BT_HF_AGENT_OBJECT_PATH,
2354 BT_HF_SERVICE_INTERFACE,
2355 "AudioConnected", NULL);
2357 /* In the case of incoming call, the call app is already launched,
2358 * hence AudioConnected signal is enough to update the call status.
2359 * In the case of outgoing call we need to lauch the callapp.
2362 g_idle_add(__bt_agent_query_and_update_call_list, bt_hf_info);
2367 void _bt_convert_addr_string_to_type_rev(unsigned char *addr,
2368 const char *address)
2373 ret_if(address == NULL);
2374 ret_if(addr == NULL);
2376 for (i = 0; i < 6; i++) {
2377 addr[5 - i] = strtol(address, &ptr, 16);
2378 if (ptr[0] != '\0') {
2387 static gboolean __bt_hf_agent_sco_accept(bt_hf_agent_info_t *bt_hf_info)
2389 struct sockaddr_sco addr;
2391 bdaddr_t bd_addr = {{0},};
2394 if (bt_hf_info->state != BT_HF_STATE_CONNECTED)
2398 sco_skt = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
2400 ERR("Can't create socket:\n");
2404 /* Bind to local address */
2405 memset(&addr, 0, sizeof(addr));
2406 addr.sco_family = AF_BLUETOOTH;
2408 DBG("Bind to address %s", bt_hf_info->remote_addr);
2410 _bt_convert_addr_string_to_type_rev(bd_addr.b, bt_hf_info->remote_addr);
2411 memcpy(&addr.sco_bdaddr, &bd_addr, sizeof(bdaddr_t));
2413 if (bind(sco_skt, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
2414 ERR("Can't bind socket:\n");
2418 if (listen(sco_skt, 1)) {
2419 ERR("Can not listen on the socket:\n");
2423 sco_io = g_io_channel_unix_new(sco_skt);
2424 g_io_channel_set_close_on_unref(sco_io, TRUE);
2425 g_io_channel_set_encoding(sco_io, NULL, NULL);
2426 g_io_channel_set_flags(sco_io, G_IO_FLAG_NONBLOCK, NULL);
2427 g_io_channel_set_buffered(sco_io, FALSE);
2429 bt_hf_info->sco_fd = sco_skt;
2430 bt_hf_info->sco_io_chan = sco_io;
2432 bt_hf_info->sco_watch_id = g_io_add_watch(sco_io,
2433 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL, __bt_hf_agent_sco_accept_cb, bt_hf_info);
2435 g_io_channel_unref(sco_io);
2444 static gboolean __bt_get_supported_indicators(bt_hf_agent_info_t *bt_hf_info)
2446 gchar buf[BT_HF_DATA_BUF_SIZE] = {0,};
2449 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_INDICATORS_SUPP, buf,
2450 sizeof(BT_HF_INDICATORS_SUPP) - 1);
2451 if (!ret || !strstr(buf, "+CIND:"))
2454 bt_hf_info->indies = __bt_hf_parse_indicator_names(strchr(buf, '('), NULL);
2459 static gboolean __bt_get_bia_cmd(bt_hf_agent_info_t *bt_hf_info, gchar *cmd, gsize cmd_size)
2464 if (bt_hf_info == NULL || cmd == NULL) {
2465 ERR("Invalid parameter");
2469 ret = g_strlcpy(cmd, BT_HF_INDICATORS_ACTIVATION, cmd_size);
2471 for (l = bt_hf_info->indies; l != NULL; l = g_slist_next(l)) {
2472 ret = g_strlcat(cmd, "0,", cmd_size);
2473 if (ret >= cmd_size) {
2474 ERR("Too many indices");
2480 cmd[ret - 1] = '\0';
2481 DBG("BIA Str : %s", cmd);
2483 ret = g_strlcat(cmd, "\r", cmd_size);
2484 if (ret >= cmd_size) {
2485 ERR("Too many indices");
2492 static gboolean __bt_get_current_indicators(bt_hf_agent_info_t *bt_hf_info)
2494 gchar buf[BT_HF_DATA_BUF_SIZE] = {0,};
2500 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_INDICATORS_VAL, buf,
2501 sizeof(BT_HF_INDICATORS_VAL) - 1);
2502 if (!ret || !strstr(buf, "+CIND:"))
2505 /* if buf has other command prefix, skip it */
2506 str = strstr(buf, "+CIND");
2510 bt_hf_info->indies = __bt_hf_parse_indicator_values(str + 6, bt_hf_info->indies);
2512 /* Parse the updated value */
2513 for (l = bt_hf_info->indies; l != NULL; l = g_slist_next(l), ++index) {
2514 struct indicator *ind = l->data;
2516 DBG("Index is NULL");
2520 if (0 == g_strcmp0(ind->descr, "\"call\"")) {
2521 DBG("CIND Match found index = %d, %s, value = %d",
2522 index, ind->descr, ind->value);
2523 bt_hf_info->ciev_call_status = ind->value;
2524 if (ind->value > 0) {
2525 bt_hf_info->is_dialing = FALSE;
2526 bt_hf_info->call_active = TRUE;
2528 } else if (0 == g_strcmp0(ind->descr, "\"callsetup\"")) {
2529 DBG("CIND Match found index = %d, %s, value = %d",
2530 index, ind->descr, ind->value);
2531 bt_hf_info->ciev_call_setup_status = ind->value;
2532 if (!bt_hf_info->is_dialing && ind->value > 0)
2533 bt_hf_info->is_dialing = TRUE;
2540 static gboolean __bt_establish_service_level_conn(bt_hf_agent_info_t *bt_hf_info)
2542 gchar buf[BT_HF_DATA_BUF_SIZE];
2543 gchar cmd_buf[BT_HF_CMD_BUF_SIZE] = {0};
2546 guint feature = BT_HF_FEATURE_EC_ANDOR_NR |
2547 BT_HF_FEATURE_CALL_WAITING_AND_3WAY |
2548 BT_HF_FEATURE_CLI_PRESENTATION |
2549 BT_HF_FEATURE_VOICE_RECOGNITION |
2550 BT_HF_FEATURE_REMOTE_VOLUME_CONTROL |
2551 BT_HF_FEATURE_ENHANCED_CALL_STATUS |
2552 BT_HF_FEATURE_CODEC_NEGOTIATION;
2554 snprintf(cmd_buf, sizeof(cmd_buf), BT_HF_FEATURES, feature);
2555 ret = __bt_hf_send_and_read(bt_hf_info, cmd_buf, buf,
2560 buf_ptr = strstr(buf, "\r\n+BRSF:");
2561 if (buf_ptr == NULL)
2564 if (!ret || sscanf(buf_ptr, "\r\n+BRSF:%5d", &bt_hf_info->ag_features) != 1)
2566 INFO("Gateway supported features are 0x%X", bt_hf_info->ag_features);
2568 if (bt_hf_info->ag_features & BT_AG_FEATURE_CODEC_NEGOTIATION) {
2569 ret = _hf_agent_codec_setup(bt_hf_info->remote_addr, BT_HF_CODEC_ID_MSBC);
2570 if (ret != BT_HF_AGENT_ERROR_NONE)
2571 ERR("Unable to set the default WBC codec");
2573 ret = __bt_hf_send_available_codec(bt_hf_info, 0);
2577 /* Default codec is NB */
2578 ret = _hf_agent_codec_setup(bt_hf_info->remote_addr, BT_HF_CODEC_ID_CVSD);
2579 if (ret != BT_HF_AGENT_ERROR_NONE)
2580 ERR("Unable to set the default NBC codec");
2583 ret = __bt_get_supported_indicators(bt_hf_info);
2588 ret = __bt_get_current_indicators(bt_hf_info);
2592 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_INDICATORS_ENABLE, buf,
2593 sizeof(BT_HF_INDICATORS_ENABLE) - 1);
2594 if (!ret || !strstr(buf, "OK"))
2597 if ((bt_hf_info->ag_features & BT_AG_FEATURE_3WAY) != 0) {
2598 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_HOLD_MPTY_SUPP,
2599 buf, sizeof(BT_HF_HOLD_MPTY_SUPP) - 1);
2600 if (!ret || !strstr(buf, "+CHLD:")) {
2601 ERR("Unable to get the CHLD Supported info");
2604 bt_hf_info->hold_multiparty_features = __bt_hf_get_hold_mpty_features(
2607 bt_hf_info->hold_multiparty_features = 0;
2609 INFO("Service layer connection successfully established...!");
2611 __bt_hf_send_and_read(bt_hf_info, BT_HF_CALLER_IDENT_ENABLE, buf,
2612 sizeof(BT_HF_CALLER_IDENT_ENABLE) - 1);
2613 __bt_hf_send_and_read(bt_hf_info, BT_HF_CARRIER_FORMAT, buf,
2614 sizeof(BT_HF_CARRIER_FORMAT) - 1);
2615 __bt_hf_send_and_read(bt_hf_info, BT_HF_CALLWAIT_NOTI_ENABLE, buf,
2616 sizeof(BT_HF_CALLWAIT_NOTI_ENABLE) - 1);
2618 if ((bt_hf_info->ag_features & BT_AG_FEATURE_NREC) != 0)
2619 __bt_hf_send_and_read(bt_hf_info, BT_HF_NREC, buf,
2620 sizeof(BT_HF_NREC) - 1);
2622 if ((bt_hf_info->ag_features & BT_AG_FEATURE_EXTENDED_RES_CODE) != 0)
2623 __bt_hf_send_and_read(bt_hf_info, BT_HF_EXTENDED_RESULT_CODE,
2624 buf, sizeof(BT_HF_EXTENDED_RESULT_CODE) - 1);
2626 if (__bt_get_bia_cmd(bt_hf_info, cmd_buf, sizeof(cmd_buf)) == TRUE)
2627 __bt_hf_send_and_read(bt_hf_info, cmd_buf, buf, strlen(cmd_buf));
2629 ERR("__bt_get_bia_cmd is failed");
2631 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_XSAT, buf,
2632 sizeof(BT_HF_XSAT) - 1);
2634 DBG("sent BT_HF_XSAT");
2636 ERR("BT_HF_XSAT sending failed");
2638 /* send Bluetooth Samsung Support Feature cmd */
2639 ret = __bt_hf_send_and_read(bt_hf_info, BT_HF_BSSF, buf,
2640 sizeof(BT_HF_BSSF) - 1);
2642 INFO("SLC completed with all commands");
2644 ERR("BT_HF_BSSF sending failed");
2646 bt_hf_info->slc = TRUE;
2649 memset(global_buff, 0, sizeof(global_buff));
2653 static void __bt_hf_agent_sigterm_handler(int signo)
2655 ERR_C("***** Signal handler came with signal %d *****", signo);
2656 GDBusConnection *conn;
2658 conn = __bt_hf_get_gdbus_connection();
2660 ERR("Unable to get connection");
2664 __bt_hf_agent_emit_signal(conn,
2665 BT_HF_AGENT_OBJECT_PATH,
2666 BT_HF_SERVICE_INTERFACE,
2668 DBG("CallEnded Signal done");
2670 g_main_loop_quit(gmain_loop);
2674 INFO_C("Terminating HF agent");
2679 static void __bt_convert_addr_type_to_rev_string(char *address,
2680 unsigned char *addr)
2682 ret_if(address == NULL);
2683 ret_if(addr == NULL);
2685 g_snprintf(address, BT_ADDRESS_STRING_SIZE,
2686 "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
2687 addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
2691 static gboolean __bt_hf_agent_release_after(gpointer user_data)
2693 if (__bt_hf_agent_release() == FALSE)
2694 ERR("Unable to release hf connection");
2699 static gboolean __bt_agent_request_service_level_conn(gpointer data)
2702 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
2703 GDBusConnection *conn;
2706 memset(prev_cmd, 0, BT_HF_CMD_BUF_SIZE);
2708 if (!__bt_establish_service_level_conn(&bt_hf_info)) {
2709 ERR("Service Level Connection is fail");
2711 conn = __bt_hf_get_gdbus_connection();
2713 remote_addr = bt_hf_info.remote_addr;
2714 __bt_hf_agent_emit_signal(conn,
2715 BT_HF_AGENT_OBJECT_PATH,
2716 BT_HF_SERVICE_INTERFACE,
2718 g_variant_new("(s)", remote_addr));
2720 bt_hf_info.state = BT_HF_STATE_CONNECTED;
2722 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) == 0) {
2723 DBG("BT device state is : 0x%X", bt_device_state);
2724 bt_device_state |= VCONFKEY_BT_DEVICE_AG_CONNECTED;
2725 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
2726 ERR("vconf_set_int failed");
2729 ERR("vconf_get_int failed");
2732 g_idle_add(__bt_hf_agent_release_after, NULL);
2737 bt_hf_info.state = BT_HF_STATE_CONNECTED;
2739 __bt_hf_agent_sco_accept(&bt_hf_info);
2741 __bt_hf_agent_start_watch(&bt_hf_info);
2743 remote_addr = bt_hf_info.remote_addr;
2745 INFO_SECURE("Address is : %s", remote_addr);
2746 INFO_C("Connected [HF role]");
2748 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) == 0) {
2749 DBG("BT device state is : 0x%X", bt_device_state);
2750 bt_device_state |= VCONFKEY_BT_DEVICE_AG_CONNECTED;
2751 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
2752 ERR("vconf_set_int failed");
2755 ERR("vconf_get_int failed");
2758 conn = __bt_hf_get_gdbus_connection();
2760 ERR("Unable to get connection");
2764 __bt_hf_agent_emit_signal(conn,
2765 BT_HF_AGENT_OBJECT_PATH,
2766 BT_HF_SERVICE_INTERFACE,
2768 g_variant_new("(s)", remote_addr));
2770 /* Request the call list and launch call app if required */
2771 __bt_hf_agent_handle_call_list(&bt_hf_info);
2778 static gboolean __bt_hf_agent_connection(gint32 fd, const gchar *obj_path)
2782 struct sockaddr_remote address;
2783 socklen_t address_len;
2784 bt_hf_info.path = g_strdup(obj_path);
2786 INFO_C("**** New HFP connection ****");
2788 is_hf_connected = TRUE;
2790 address_len = sizeof(address);
2791 if (getpeername(fd, (struct sockaddr *) &address, &address_len) != 0)
2792 ERR("BD_ADDR is NULL");
2794 DBG("RFCOMM connection for HFP is completed. Fd = [%d]\n", fd);
2796 bt_hf_info.io_chan = g_io_channel_unix_new(bt_hf_info.fd);
2797 flags = g_io_channel_get_flags(bt_hf_info.io_chan);
2799 flags &= ~G_IO_FLAG_NONBLOCK;
2800 flags &= G_IO_FLAG_MASK;
2801 g_io_channel_set_flags(bt_hf_info.io_chan, flags, NULL);
2802 g_io_channel_set_encoding(bt_hf_info.io_chan, NULL, NULL);
2803 g_io_channel_set_buffered(bt_hf_info.io_chan, FALSE);
2805 bt_hf_info.remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
2806 __bt_convert_addr_type_to_rev_string(bt_hf_info.remote_addr,
2807 address.remote_bdaddr.b);
2809 g_idle_add(__bt_agent_request_service_level_conn, NULL);
2814 static void __bt_hf_agent_indicator_free(gpointer mem)
2819 static gboolean __bt_hf_agent_release(void)
2821 int bt_device_state = VCONFKEY_BT_DEVICE_NONE;
2822 GDBusConnection *conn;
2824 if (bt_hf_info.state == BT_HF_STATE_DISCONNECTED) {
2825 ERR("hf is already disconnected");
2829 if (bt_hf_info.indies) {
2830 g_slist_free_full(bt_hf_info.indies, __bt_hf_agent_indicator_free);
2831 bt_hf_info.indies = NULL;
2834 if (bt_hf_info.io_chan) {
2835 g_io_channel_shutdown(bt_hf_info.io_chan, TRUE, NULL);
2836 g_io_channel_unref(bt_hf_info.io_chan);
2837 bt_hf_info.io_chan = NULL;
2840 if (bt_hf_info.sco_watch_id > 0) {
2841 g_source_remove(bt_hf_info.sco_watch_id);
2842 bt_hf_info.sco_watch_id = 0;
2845 bt_hf_info.state = BT_HF_STATE_DISCONNECTED;
2847 __bt_hf_agent_release_queue();
2849 if (vconf_get_int(VCONFKEY_BT_DEVICE, &bt_device_state) == 0) {
2850 DBG("BT device state is : 0x%X", bt_device_state);
2851 bt_device_state ^= VCONFKEY_BT_DEVICE_AG_CONNECTED;
2852 if (vconf_set_int(VCONFKEY_BT_DEVICE, bt_device_state) != 0) {
2853 ERR("vconf_set_int failed");
2856 ERR("vconf_get_int failed");
2859 __bt_hf_agent_stop_watch(&bt_hf_info);
2860 conn = __bt_hf_get_gdbus_connection();
2862 ERR("Unable to get connection");
2866 __bt_hf_agent_emit_signal(conn,
2867 BT_HF_AGENT_OBJECT_PATH,
2868 BT_HF_SERVICE_INTERFACE,
2870 g_variant_new("(s)", bt_hf_info.remote_addr));
2872 g_free(bt_hf_info.path);
2873 bt_hf_info.path = NULL;
2875 g_free(bt_hf_info.remote_addr);
2876 bt_hf_info.remote_addr = NULL;
2878 is_hf_connected = FALSE;
2883 static gboolean __bt_hf_agent_connection_release(void)
2885 return __bt_hf_agent_release();
2888 static int __bt_hf_register_profile(const char *uuid, uint16_t version,
2889 const char *name, const char *object, uint16_t features)
2894 GError *error = NULL;
2895 GVariantBuilder *builder;
2898 proxy = __bt_hf_gdbus_get_service_proxy(BLUEZ_SERVICE_NAME,
2899 "/org/bluez", BLUEZ_PROFILE_MGMT_INTERFACE);
2902 return BT_HF_AGENT_ERROR_INTERNAL;
2904 path = g_strdup(BT_HF_BLUEZ_OBJECT_PATH);
2906 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
2908 g_variant_builder_add(builder, "{sv}",
2909 "Name", g_variant_new("s",
2911 g_variant_builder_add(builder, "{sv}",
2912 "Version", g_variant_new("q", version));
2914 g_variant_builder_add(builder, "{sv}",
2915 "features", g_variant_new("q", features));
2917 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
2918 g_variant_new("(osa{sv})", path,
2919 HFP_HF_UUID, builder),
2920 G_DBUS_CALL_FLAGS_NONE, -1,
2923 g_variant_builder_unref(builder);
2926 /* dBUS-RPC is failed */
2927 ERR("dBUS-RPC is failed");
2928 if (error != NULL) {
2929 /* dBUS gives error cause */
2930 ERR("D-Bus API failure: errCode[%x], message[%s]",
2931 error->code, error->message);
2932 g_clear_error(&error);
2935 return BT_HF_AGENT_ERROR_INTERNAL;
2937 g_variant_unref(ret);
2941 return BT_HF_AGENT_ERROR_NONE;
2944 static void __bt_hf_agent_register(void)
2949 uint16_t version = hf_ver;
2950 uint16_t features = bt_hf_info.feature;
2952 gchar *path = g_strdup(BT_HF_BLUEZ_OBJECT_PATH);
2953 name = g_strdup("Hands-Free");
2955 ret = __bt_hf_register_profile(HFP_HF_UUID, version, name, path,
2958 ERR("Error in register");
2967 static void __bt_hf_agent_unregister(void)
2971 gchar *path = g_strdup(BT_HF_BLUEZ_OBJECT_PATH);
2974 __bt_hf_agent_gdbus_method_send(BLUEZ_SERVICE_NAME,
2975 g_variant_new("(o)", path),
2976 BLUEZ_HF_INTERFACE_NAME,
2988 static void __bt_hf_agent_filter_cb(GDBusConnection *connection,
2989 const gchar *sender_name,
2990 const gchar *object_path,
2991 const gchar *interface_name,
2992 const gchar *signal_name,
2993 GVariant *parameters,
2999 GVariant *optional_param;
3001 if (strcasecmp(signal_name, "InterfacesAdded") == 0) {
3003 g_variant_get(parameters, "(&o@a{sa{sv}})",
3004 &path, &optional_param);
3006 ERR("Invalid adapter path");
3010 if (strcasecmp(path, DEFAULT_ADAPTER_OBJECT_PATH) == 0) {
3011 g_obj_path = g_strdup(path);
3012 INFO("Adapter Path = [%s]", path);
3013 __bt_hf_agent_register();
3015 } else if (strcasecmp(signal_name, "InterfacesRemoved") == 0) {
3016 g_variant_get(parameters, "(&o@as)", &path, &optional_param);
3018 __bt_hf_agent_unregister();
3023 static void __bt_hf_agent_dbus_init(void)
3025 GDBusConnection *conn;
3029 conn = __bt_hf_get_gdbus_connection();
3031 ERR("Error in creating the gdbus connection\n");
3034 if (!__bt_hf_register_profile_methods()) {
3035 ERR("Error in register_profile_methods\n");
3039 owner_sig_id = g_dbus_connection_signal_subscribe(conn,
3040 NULL, BT_MANAGER_INTERFACE, NULL, NULL, NULL, 0,
3041 __bt_hf_agent_filter_cb, NULL, NULL);
3046 static void __bt_hf_agent_dbus_deinit(void)
3049 if (service_gproxy) {
3050 g_object_unref(service_gproxy);
3051 service_gproxy = NULL;
3055 if (owner_sig_id != -1)
3056 g_dbus_connection_signal_unsubscribe(gdbus_conn,
3059 g_object_unref(gdbus_conn);
3065 static int _hf_agent_answer_call(GDBusMethodInvocation *context)
3070 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3071 ERR("HF not Connected");
3072 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3074 bt_hf_info.context = context;
3076 ret = __bt_hf_send_only(&bt_hf_info, BT_HF_ANSWER_CALL,
3077 sizeof(BT_HF_ANSWER_CALL) - 1);
3079 return BT_HF_AGENT_ERROR_INTERNAL;
3082 return BT_HF_AGENT_ERROR_NONE;
3086 static int _hf_agent_terminate_call(GDBusMethodInvocation *context)
3091 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3092 ERR("HF not Connected");
3093 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3096 bt_hf_info.context = context;
3098 ret = __bt_hf_send_only(&bt_hf_info, BT_HF_END_CALL,
3099 sizeof(BT_HF_END_CALL) - 1);
3101 return BT_HF_AGENT_ERROR_INTERNAL;
3104 return BT_HF_AGENT_ERROR_NONE;
3107 static int _hf_agent_dial_no(GDBusMethodInvocation *context, char *no)
3110 char buf[BT_MAX_TEL_NUM_STR + 6] = {0};
3112 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3113 ERR("HF not Connected");
3114 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3117 bt_hf_info.context = context;
3119 if (strlen(no) > 0) {
3120 snprintf(buf, sizeof(buf), BT_HF_DIAL_NO, no);
3122 if (strstr(prev_cmd, "ATD") && bt_hf_info.ciev_call_status == 0
3123 && bt_hf_info.ciev_call_setup_status == 0) {
3124 INFO("RAD POPUP CANCEL CASE. send ATD w/o response - KOR REQUEST");
3125 ret = __bt_hf_send_only_without_queue(&bt_hf_info, buf, strlen(buf));
3129 ret = __bt_hf_send_only(&bt_hf_info, buf, strlen(buf));
3132 /* prev_cmd is meant for only meant for ATD & AT+BLDN Error handling */
3133 snprintf(prev_cmd, BT_HF_CMD_BUF_SIZE, "%s", buf);
3136 return BT_HF_AGENT_ERROR_INTERNAL;
3138 return BT_HF_AGENT_ERROR_NONE;
3141 /* prev_cmd is meant for only meant for ATD & AT+BLDN Error handling */
3142 snprintf(prev_cmd, BT_HF_CMD_BUF_SIZE, "%s", BT_HF_REDIAL);
3144 ret = __bt_hf_send_only(&bt_hf_info, BT_HF_REDIAL,
3145 sizeof(BT_HF_REDIAL) - 1);
3147 return BT_HF_AGENT_ERROR_INTERNAL;
3149 return BT_HF_AGENT_ERROR_NONE;
3152 static int _hf_agent_voice_recognition(GDBusMethodInvocation *context, unsigned int status)
3157 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3158 ERR("HF not Connected");
3159 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3162 snprintf(buf, sizeof(buf), BT_HF_VOICE_RECOGNITION, status);
3164 bt_hf_info.context = context;
3166 ret = __bt_hf_send_only(&bt_hf_info, buf, strlen(buf));
3168 return BT_HF_AGENT_ERROR_INTERNAL;
3171 return BT_HF_AGENT_ERROR_NONE;
3174 static int _hf_agent_set_speaker_gain(GDBusMethodInvocation *context, unsigned int gain)
3179 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3180 ERR("HF not Connected");
3181 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3184 if (gain > BT_HF_MAX_SPEAKER_GAIN)
3185 return BT_HF_AGENT_ERROR_INVALID_PARAM;
3187 snprintf(buf, sizeof(buf), BT_HF_SPEAKER_GAIN, gain);
3189 bt_hf_info.context = context;
3191 ret = __bt_hf_send_only(&bt_hf_info, buf,
3194 return BT_HF_AGENT_ERROR_INTERNAL;
3196 return BT_HF_AGENT_ERROR_NONE;
3200 static int _hf_agent_send_dtmf(GDBusMethodInvocation *context, char *dtmf)
3205 if (strlen(dtmf) <= 0)
3206 return BT_HF_AGENT_ERROR_INVALID_PARAM;
3208 if (bt_hf_info.state != BT_HF_STATE_CONNECTED) {
3209 ERR("HF not Connected");
3210 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3213 snprintf(buf, sizeof(buf), BT_HF_DTMF, dtmf);
3215 bt_hf_info.context = context;
3217 ret = __bt_hf_send_only(&bt_hf_info, buf, strlen(buf));
3219 return BT_HF_AGENT_ERROR_INTERNAL;
3222 return BT_HF_AGENT_ERROR_NONE;
3226 static int _hf_agent_send_3way_cmd(GDBusMethodInvocation *context, char *cmd)
3230 if (strlen(cmd) <= 0)
3231 return BT_HF_AGENT_ERROR_INVALID_PARAM;
3233 bt_hf_info.context = context;
3235 ret = __bt_hf_send_only(&bt_hf_info, cmd,
3238 return BT_HF_AGENT_ERROR_INTERNAL;
3240 return BT_HF_AGENT_ERROR_NONE;
3243 static gboolean bt_hf_agent_sco_disconnect(void)
3246 GDBusConnection *conn;
3248 close(bt_hf_info.cli_sco_fd);
3249 bt_hf_info.cli_sco_fd = -1;
3251 DBG("Emit AudioDisconnected Signal");
3252 conn = __bt_hf_get_gdbus_connection();
3254 ERR("Unable to get connection");
3258 sco_audio_connected = BT_HF_AUDIO_DISCONNECTED;
3260 __bt_hf_agent_emit_signal(conn,
3261 BT_HF_AGENT_OBJECT_PATH,
3262 BT_HF_SERVICE_INTERFACE,
3263 "AudioDisconnected", NULL);
3268 static GVariant *bt_hf_agent_request_call_list(void)
3270 GSList *call_list = NULL;
3274 call_list = __bt_hf_get_call_list(&bt_hf_info);
3276 INFO("call list is NULL");
3280 var_data = __bt_hf_agent_get_call_status_info(call_list);
3281 __bt_hf_free_call_list(call_list);
3287 static int bt_hf_agent_send_at_cmd(GDBusMethodInvocation *context, char *atcmd)
3291 char cmd_buf[BT_MAX_TEL_NUM_STR + 20] = {0, };
3296 return BT_HF_AGENT_ERROR_INVALID_PARAM;
3298 if (bt_hf_info.state != BT_HF_STATE_CONNECTED)
3299 return BT_HF_AGENT_ERROR_NOT_CONNECTED;
3301 /* Should not send cmds if DUT has sent a command and waiting for response */
3302 if (prev_cmd[0] != 0) {
3303 INFO("DUT is waiting a respond for previous TX cmd. Skip sending.");
3304 return BT_HF_AGENT_ERROR_INTERNAL;
3307 strncpy(cmd_buf, atcmd, sizeof(cmd_buf) - 2);
3308 strncat(cmd_buf, "\r", (sizeof(cmd_buf) - 1) - strlen(cmd_buf));
3310 bt_hf_info.context = context;
3312 ret = __bt_hf_send_only(&bt_hf_info, cmd_buf, strlen(cmd_buf));
3314 return BT_HF_AGENT_ERROR_INTERNAL;
3317 return BT_HF_AGENT_ERROR_NONE;
3320 static uint32_t __bt_hf_agent_get_hf_features(void)
3323 uint32_t hf_features = BT_HF_FEATURE_EC_ANDOR_NR |
3324 BT_HF_FEATURE_CALL_WAITING_AND_3WAY |
3325 BT_HF_FEATURE_CLI_PRESENTATION |
3326 BT_HF_FEATURE_VOICE_RECOGNITION |
3327 BT_HF_FEATURE_REMOTE_VOLUME_CONTROL |
3328 BT_HF_FEATURE_ENHANCED_CALL_STATUS |
3329 BT_HF_FEATURE_CODEC_NEGOTIATION;
3331 hf_ver = HFP_VERSION_1_6;
3338 struct sigaction sa;
3339 uint32_t hf_features;
3341 INFO("Starting Bluetooth HF agent");
3343 hf_features = __bt_hf_agent_get_hf_features();
3344 bt_hf_info.feature = (uint16_t) hf_features & 0x3F;
3346 memset(&sa, 0, sizeof(sa));
3347 sa.sa_flags = SA_NOCLDSTOP;
3348 sa.sa_handler = __bt_hf_agent_sigterm_handler;
3349 sigaction(SIGTERM, &sa, NULL);
3351 /* Temporarily, block the below signal for debugging */
3352 // sigaction(SIGSEGV, &sa, NULL);
3353 // sigaction(SIGABRT, &sa, NULL);
3354 gmain_loop = g_main_loop_new(NULL, FALSE);
3356 if (gmain_loop == NULL) {
3357 ERR("GMainLoop create failed\n");
3358 return EXIT_FAILURE;
3361 __bt_hf_agent_dbus_init();
3362 g_main_loop_run(gmain_loop);
3364 __bt_hf_agent_dbus_deinit();
3367 g_main_loop_unref(gmain_loop);
3369 INFO("Terminating Bluetooth HF agent");