Update AG and HF agent codes from wearable product
[platform/core/connectivity/bluetooth-agent.git] / ag-agent / bluetooth-ag-handler.c
1 /*
2  * bluetooth-ag-handler.c
3  *
4  * Copyright (c) 2014 - 2015 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:     Hocheol Seo <hocheol.seo@samsung.com>
7  *              Chethan TN <chethan.tn@samsung.com>
8  *              Chanyeol Park <chanyeol.park@samsung.com>
9  *              Rakesh MK <rakesh.mk@samsung.com>
10  *
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *              http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  */
24 #include <stdbool.h>
25 #include <bundle_internal.h>
26 #include <bluetooth.h>
27 #include <bluetooth_internal.h>
28 #include "bluetooth-ag-agent.h"
29 #include "bluetooth-ag-handler.h"
30 #include "vconf.h"
31 #include "vconf-keys.h"
32 #include <syspopup_caller.h>
33
34 extern bt_ag_status_t ag;
35 extern GSList *active_devices;
36 extern GDBusConnection *ag_dbus_conn;
37 extern gchar *remote_dev_path;
38
39  /* AT+CSQ : Returns received signal strength indication.
40      Command response: +CSQ: <rssi>,<ber>
41     <ber> is not supported and has a constant value of 99, included for compatibility reasons.
42 */
43 #define BT_SIGNAL_QUALITY_BER 99
44
45 wbs_options wbs_opts = {
46         .wbs_enable = FALSE,
47         .i2s_enable = 0x00,
48         .is_master = 0x00,
49         .clock_rate = 0x02,
50         .pcm_interface_rate = 0x00,
51 };
52
53 /* AT+BRSF response */
54 int _bt_hfp_supported_features(bt_ag_info_t *hs, const char *buf)
55 {
56         bt_ag_slconn_t *slconn = hs->slc;
57         int err;
58 //      bt_hfp_agent_error_t ret = BT_HFP_AGENT_ERROR_NONE;
59
60         DBG("AT + BRSF");
61         if (strlen(buf) < 9)
62                 return -EINVAL;
63
64         slconn->hs_features = strtoul(&buf[8], NULL, 10);
65 #if 0 /* SCO is crashed if below is called when SCO is opened by hf-agent */
66         if (slconn->hs_features & BT_HF_FEATURE_CODEC_NEGOTIATION) {
67                 ret = _bt_ag_set_codec(hs, "SetWbsParameters");
68                 if (ret != BT_HFP_AGENT_ERROR_NONE)
69                         ERR("Unable to set the default WBC codec");
70         } else {
71                 /* Default codec is NB */
72                 ret = _bt_ag_set_codec(hs, "SetNbParameters");
73                 if (ret != BT_HFP_AGENT_ERROR_NONE)
74                         ERR("Unable to set the default NBC codec");
75         }
76 #endif
77         err = _bt_ag_send_at(hs, "\r\n+BRSF: %u\r\n", ag.features);
78         if (err < 0)
79                 return err;
80
81         return _bt_ag_send_at(hs, "\r\nOK\r\n");
82 }
83
84 static char *__bt_get_indicator_ranges(const bt_ag_indicators_t *indicators)
85 {
86         int i;
87         GString *gstr;
88
89         DBG("__bt_get_indicator_ranges");
90         gstr = g_string_new("\r\n+CIND: ");
91
92         for (i = 0; indicators[i].indicator_desc != NULL; i++) {
93                 if (i == 0)
94                         g_string_append_printf(gstr, "(\"%s\",(%s))",
95                                 indicators[i].indicator_desc,
96                                 indicators[i].indicator_range);
97                 else
98                         g_string_append_printf(gstr, ",(\"%s\",(%s))",
99                                 indicators[i].indicator_desc,
100                                 indicators[i].indicator_range);
101         }
102         g_string_append(gstr, "\r\n");
103         return g_string_free(gstr, FALSE);
104 }
105
106 static char *__bt_get_indicator_values(const bt_ag_indicators_t *indicators)
107 {
108         int i;
109         GString *gstr;
110
111         gstr = g_string_new("\r\n+CIND: ");
112         DBG("__bt_get_indicator_values");
113         for (i = 0; indicators[i].indicator_range != NULL; i++) {
114                 if (i == 0)
115                         g_string_append_printf(gstr, "%d",
116                                 indicators[i].hfp_value);
117                 else
118                         g_string_append_printf(gstr, ",%d",
119                                 indicators[i].hfp_value);
120         }
121         g_string_append(gstr, "\r\n");
122
123         return g_string_free(gstr, FALSE);
124 }
125
126 static int __bt_check_hdset(bt_ag_info_t *hdset)
127 {
128         bt_ag_slconn_t *slconn = hdset->slc;
129
130         if (!hdset->hfp_active)
131                 return -1;
132
133         if (slconn->is_client_active)
134                 return 0;
135         else
136                 return -1;
137 }
138
139 static int __bt_hfp_cmp(bt_ag_info_t *hs)
140 {
141         if (hs->hfp_active)
142                 return 0;
143         else
144                 return -1;
145 }
146
147 static int __bt_cwa_cmp(bt_ag_info_t *hs)
148 {
149         if (!hs->hfp_active)
150                 return -1;
151
152         if (hs->slc->is_cwa_enabled)
153                 return 0;
154         else
155                 return -1;
156 }
157
158 gboolean __bt_ring_timer_cb(gpointer data)
159 {
160         _bt_ag_send_foreach_headset(active_devices, NULL, "\r\nRING\r\n");
161
162         if (ag.number)
163                 _bt_ag_send_foreach_headset(active_devices, __bt_check_hdset,
164                                         "\r\n+CLIP: \"%s\",%d\r\n",
165                                         ag.number, ag.number_type);
166
167         return TRUE;
168 }
169
170 int _bt_incoming_call_indicator(const char *number, int type)
171 {
172         bt_ag_info_t *hs;
173         bt_ag_slconn_t *slconn;
174
175         if (!active_devices)
176                 return -ENODEV;
177
178         /* Get the updated list of connected devices */
179         hs = active_devices->data;
180         slconn = hs->slc;
181
182         if (ag.ring_timer) {
183                 DBG("incoming_call_indicator: already calling....");
184                 return -EBUSY;
185         }
186
187         /*If inband ring supported then no need to send RING alert to HF */
188         if (!hs->hfp_active && slconn->is_inband_ring) {
189                 DBG("Inband ring tone supported");
190                 return 0;
191         }
192
193         DBG("Inband ring tone not supported.. so send a RING to HF");
194         g_free(ag.number);
195         ag.number = g_strdup(number);
196         ag.number_type = type;
197
198         if (slconn->is_inband_ring &&
199                                         hs->state != HEADSET_STATE_ON_CALL) {
200                 slconn->is_pending_ring = TRUE;
201                 return 0;
202         }
203
204         __bt_ring_timer_cb(NULL);
205         ag.ring_timer = g_timeout_add(AG_RING_INTERVAL, __bt_ring_timer_cb, NULL);
206
207         return 0;
208 }
209
210 int _bt_calling_stopped_indicator(void)
211 {
212         bt_ag_info_t *dev;
213
214         if (ag.ring_timer) {
215                 g_source_remove(ag.ring_timer);
216                 ag.ring_timer = 0;
217         }
218
219         if (!active_devices)
220                 return 0;
221
222         /* In case SCO is in intermediate state to connect */
223         dev = active_devices->data;
224
225         if (!dev->slc->is_pending_ring && !ag.ring_timer)
226                 return -EINVAL;
227
228         dev->slc->is_pending_ring = FALSE;
229
230         return 0;
231 }
232
233 void _bt_hfp_set_ag_indicator(uint32_t ag_features,
234                         const bt_ag_indicators_t *ag_indicators, int rh,
235                         const char *chld)
236 {
237         DBG("Set Ag Features");
238         ag.telephony_ready = TRUE;
239         ag.features = ag_features;
240         ag.indicators = ag_indicators;
241         ag.rh = rh;
242         ag.chld = chld;
243 }
244
245 void _bt_hfp_deinitialize(void)
246 {
247         g_free(ag.number);
248         memset(&ag, 0, sizeof(ag));
249         ag.rh = BT_RSP_HOLD_NOT_SUPPORTED;
250         ag.er_mode = 3;
251 }
252
253 /* Send event indication call from Statemanager module */
254 bt_hfp_agent_error_t _bt_hfp_event_indicator(int index)
255 {
256         if (!active_devices) {
257                 DBG("No Active devices present");
258                 return BT_HFP_AGENT_ERROR_NOT_AVAILABLE;
259         }
260
261         if (!ag.er_ind) {
262                 DBG("Indicate event called but event reporting is disabled");
263                 return BT_HFP_AGENT_ERROR_INTERNAL;
264         }
265
266         DBG("Sending event notification to hf....");
267
268         _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
269                                 "\r\n+CIEV: %d,%d\r\n", index + 1,
270                                 ag.indicators[index].hfp_value);
271
272         return BT_HFP_AGENT_ERROR_NONE;
273 }
274
275 /* AT+CIND response */
276 int _bt_hfp_report_indicators(bt_ag_info_t *hs, const char *buf)
277 {
278         int err;
279         char *str;
280
281         if (strlen(buf) < 8)
282                 return -EINVAL;
283
284         if (buf[7] == '=')
285                 str = __bt_get_indicator_ranges(ag.indicators);
286         else
287                 str = __bt_get_indicator_values(ag.indicators);
288
289         err = _bt_ag_send_at(hs, "%s", str);
290
291         g_free(str);
292
293         if (err < 0)
294                 return err;
295
296         return _bt_ag_send_at(hs, "\r\nOK\r\n");
297 }
298
299 /* AT+CMER response */
300 int _bt_event_reporting_response(void *t_device,
301                                 bt_hfp_agent_error_t err)
302 {
303         bt_ag_info_t *hdset = t_device;
304         bt_ag_slconn_t *slconn = hdset->slc;
305         int ret_val;
306
307         if (err != (bt_hfp_agent_error_t) HFP_STATE_MNGR_ERR_NONE)
308                 return _bt_ag_send_response(t_device, err);
309
310         ret_val = _bt_ag_send_at(hdset, "\r\nOK\r\n");
311         if (ret_val < 0)
312                 return ret_val;
313
314         if (hdset->state != HEADSET_STATE_CONNECTING)
315                 return 0;
316
317         if (slconn->hs_features & HANDSFREE_FEATURE_CALL_WAITING_AND_3WAY &&
318                         ag.features & BT_AG_FEATURE_THREE_WAY_CALL)
319                 return 0;
320
321         _bt_ag_slconn_complete(hdset);
322
323         return 0;
324 }
325
326 int _bt_hfp_enable_indicators(bt_ag_info_t *hdset, const char *buffer)
327 {
328         if (strlen(buffer) < 13)
329                 return -EINVAL;
330
331         /* tokenks can be <mode>,<keyp>,<disp>,<ind>,<bfr>*/
332         char **ind_tokens = g_strsplit(&buffer[8], ",", 5);
333
334         if (g_strv_length(ind_tokens) < 4) {
335                 g_strfreev(ind_tokens);
336                 return -EINVAL;
337         }
338
339         ag.er_mode = atoi(ind_tokens[0]);
340         ag.er_ind = atoi(ind_tokens[3]);
341
342         DBG("hfp_enable_indicators (CMER): indicator=%d, mode=%d",
343                 ag.er_ind, ag.er_mode);
344
345         g_strfreev(ind_tokens);
346         ind_tokens = NULL;
347
348         switch (ag.er_ind) {
349         case 0:
350         case 1:
351                 _bt_hfp_update_event_request(ag.er_ind, hdset);
352                 break;
353         default:
354                 return -EINVAL;
355         }
356         return 0;
357 }
358
359         /* AT+CHLD response */
360 int _bt_hfp_call_hold(bt_ag_info_t *hs, const char *buf)
361 {
362         int err;
363
364         if (strlen(buf) < 9)
365                 return -EINVAL;
366
367         if (buf[8] != '?') {
368                 _bt_hfp_call_hold_request(&buf[8], hs);
369                 return 0;
370         }
371
372         err = _bt_ag_send_at(hs, "\r\n+CHLD: (%s)\r\n", ag.chld);
373         if (err < 0)
374                 return err;
375
376         err = _bt_ag_send_at(hs, "\r\nOK\r\n");
377         if (err < 0)
378                 return err;
379
380         _bt_ag_slconn_complete(hs);
381
382         return 0;
383 }
384
385 int _bt_key_press_response(void *t_device, bt_hfp_agent_error_t err)
386 {
387         return _bt_ag_send_response(t_device, err);
388 }
389
390 int _bt_hfp_key_press(bt_ag_info_t *hs, const char *buf)
391 {
392         if (strlen(buf) < 9)
393                 return -EINVAL;
394
395         if (ag.ring_timer) {
396                 g_source_remove(ag.ring_timer);
397                 ag.ring_timer = 0;
398         }
399
400         _bt_hfp_key_press_request(&buf[8], hs);
401
402         return 0;
403 }
404
405 int _bt_answer_call_response(void *hs, bt_hfp_agent_error_t err)
406 {
407         return _bt_ag_send_response(hs, err);
408 }
409
410 int _bt_hfp_answer_call(bt_ag_info_t *hs, const char *buf)
411 {
412         if (ag.ring_timer) {
413                 g_source_remove(ag.ring_timer);
414                 ag.ring_timer = 0;
415         }
416
417         if (ag.number) {
418                 g_free(ag.number);
419                 ag.number = NULL;
420         }
421
422         if (remote_dev_path)
423                 g_free(remote_dev_path);
424
425         remote_dev_path = g_strdup(hs->path);
426
427         _bt_hfp_answer_call_request(hs);
428
429         return 0;
430 }
431 int _bt_terminate_call_response(void *t_device,
432                                         hfp_state_manager_err_t err)
433 {
434         bt_ag_info_t *hs = t_device;
435
436         if (err != HFP_STATE_MNGR_ERR_NONE)
437                 return _bt_ag_send_response(hs, err);
438
439         return _bt_ag_send_at(hs, "\r\nOK\r\n");
440 }
441
442 int _bt_hfp_terminate_call(bt_ag_info_t *hs, const char *buf)
443 {
444         if (ag.number) {
445                 g_free(ag.number);
446                 ag.number = NULL;
447         }
448
449         if (ag.ring_timer) {
450                 g_source_remove(ag.ring_timer);
451                 ag.ring_timer = 0;
452         }
453
454         _bt_hfp_terminate_call_request(hs);
455
456         return 0;
457 }
458
459 int _bt_hfp_cli_notification(bt_ag_info_t *hs, const char *buf)
460 {
461         bt_ag_slconn_t *slconn = hs->slc;
462
463         if (strlen(buf) < 9)
464                 return -EINVAL;
465
466         slconn->is_client_active = buf[8] == '1' ? TRUE : FALSE;
467
468         return _bt_ag_send_at(hs, "\r\nOK\r\n");
469 }
470
471 int _bt_response_and_hold_response(void *t_device,
472                                         bt_hfp_agent_error_t err)
473 {
474         return _bt_ag_send_response(t_device, err);
475 }
476
477 int _bt_hfp_response_and_hold(bt_ag_info_t *hs, const char *buf)
478 {
479
480         if (strlen(buf) < 8)
481                 return -EINVAL;
482
483         if (ag.rh == BT_RSP_HOLD_NOT_SUPPORTED)
484                 return _bt_ag_send_response(hs,
485                         HFP_STATE_MNGR_ERR_NOT_SUPPORTED);
486
487         if (buf[7] == '=') {
488                 _bt_hfp_response_and_hold_request(hs);
489                 return 0;
490         }
491
492         if (ag.rh >= 0)
493                 _bt_ag_send_at(hs, "\r\n+BTRH: %d\r\n", ag.rh);
494
495         return _bt_ag_send_at(hs, "\r\nOK\r\n");
496 }
497
498 int _bt_hfp_last_dialed_number(bt_ag_info_t *hs, const char *buf)
499 {
500         if (remote_dev_path)
501                 g_free(remote_dev_path);
502
503         remote_dev_path = g_strdup(hs->path);
504         _bt_hfp_last_dialed_number_request(hs);
505
506         return 0;
507 }
508
509 int _bt_dial_number_response(void *t_device, bt_hfp_agent_error_t err)
510 {
511         return _bt_ag_send_response(t_device, err);
512 }
513
514 int _bt_hfp_dial_number(bt_ag_info_t *hs, const char *buf)
515 {
516         char number[MAX_BUFFER_SIZE];
517         size_t buf_len;
518
519         buf_len = strlen(buf);
520
521         if (buf[buf_len - 1] != ';') {
522                 DBG("Reject the non-voice call dial number request");
523                 return -EINVAL;
524         }
525
526         memset(number, 0, sizeof(number));
527         strncpy(number, &buf[3], buf_len - 4);
528
529         if (remote_dev_path)
530                 g_free(remote_dev_path);
531
532         remote_dev_path = g_strdup(hs->path);
533
534         _bt_hfp_dial_number_request(number, hs);
535
536         return 0;
537 }
538
539 static int __bt_headset_set_gain(bt_ag_info_t *hs, uint16_t gain, char type)
540 {
541         bt_ag_slconn_t *slconn = hs->slc;
542         const char *property;
543
544         if (gain > 15) {
545                 DBG("Invalid gain value: %u", gain);
546                 return -EINVAL;
547         }
548
549         switch (type) {
550         case BT_HFP_SPEAKER_GAIN:
551                 if (slconn->speaker_gain == gain) {
552                         DBG("Ignoring no-change in speaker gain");
553                         return -EALREADY;
554                 }
555                 property = "SpeakerGain";
556                 slconn->speaker_gain = gain;
557                 _bt_ag_agent_set_last_speaker_gain(gain);
558                 break;
559         case BT_HFP_MICROPHONE_GAIN:
560                 if (slconn->microphone_gain == gain) {
561                         DBG("Ignoring no-change in microphone gain");
562                         return -EALREADY;
563                 }
564                 property = "MicrophoneGain";
565                 slconn->microphone_gain = gain;
566                 break;
567         default:
568                 DBG("Unknown gain setting\n");
569                 return -EINVAL;
570         }
571
572         _bt_ag_agent_emit_property_changed(ag_dbus_conn, hs->path,
573                                 BT_HEADSET_INTERFACE, property,
574                                 g_variant_new("q", gain));
575         return 0;
576 }
577
578 int _bt_hfp_signal_gain_setting(bt_ag_info_t *hs, const char *buf)
579 {
580         uint16_t gain;
581         int err;
582
583         if (strlen(buf) < 8) {
584                 DBG("very short string to use for Gain setting\n");
585                 return -EINVAL;
586         }
587
588         gain = (uint16_t) strtol(&buf[7], NULL, 10);
589
590         err = __bt_headset_set_gain(hs, gain, buf[5]);
591         if (err < 0 && err != -EALREADY)
592                 return err;
593
594         return _bt_ag_send_at(hs, "\r\nOK\r\n");
595 }
596
597 int _bt_transmit_dtmf_response(void *t_device,
598                         bt_hfp_agent_error_t err)
599 {
600         return _bt_ag_send_response(t_device, err);
601 }
602
603 int _bt_hfp_dtmf_tone(bt_ag_info_t *hs, const char *buf)
604 {
605         char tone;
606
607         if (strlen(buf) < 8) {
608                 printf("Too short string for DTMF tone");
609                 return -EINVAL;
610         }
611
612         tone = buf[7];
613         if (tone >= '#' && tone <= 'D')
614                 _bt_hfp_channel_dtmf_request(tone, hs);
615         else
616                 return -EINVAL;
617
618         return 0;
619 }
620
621 int _bt_hfp_set_speaker_gain(bt_ag_info_t *hs,
622                 uint16_t gain_value)
623 {
624         int err;
625         char type = BT_HFP_SPEAKER_GAIN;
626
627         err = __bt_headset_set_gain(hs, gain_value, type);
628         if (err < 0)
629                 return BT_HFP_AGENT_ERROR_INTERNAL;
630
631         if (hs->state == HEADSET_STATE_ON_CALL) {
632                 err = _bt_ag_send_at(hs, "\r\n+VG%c=%u\r\n", type,
633                                 gain_value);
634                 if (err < 0)
635                         return BT_HFP_AGENT_ERROR_INTERNAL;
636         }
637         return BT_HFP_AGENT_ERROR_NONE;
638 }
639
640 int _bt_hfp_set_microphone_gain(bt_ag_info_t *hs,
641                 uint16_t gain_value)
642 {
643         int err;
644         char type = BT_HFP_MICROPHONE_GAIN;
645
646         if (hs == NULL) {
647                 DBG("hs is NULL");
648                 return BT_HFP_AGENT_ERROR_INVALID_PARAM;
649         }
650
651         err = __bt_headset_set_gain(hs, gain_value, type);
652         if (err < 0)
653                 return BT_HFP_AGENT_ERROR_INTERNAL;
654
655         if (hs->state == HEADSET_STATE_ON_CALL) {
656                 err = _bt_ag_send_at(hs, "\r\n+VG%c=%u\r\n", type,
657                                 gain_value);
658                 if (err < 0)
659                         return BT_HFP_AGENT_ERROR_INTERNAL;
660         }
661         return BT_HFP_AGENT_ERROR_NONE;
662 }
663
664
665 int _bt_hfp_set_voice_dial(bt_ag_info_t *hs,
666                 gboolean enable)
667 {
668         DBG("_bt_hfp_set_voice_dial = %d", enable);
669
670         if (_bt_ag_send_at(hs, "\r\n+BVRA: %d\r\n", enable) < 0)
671                 return BT_HFP_AGENT_ERROR_INTERNAL;
672
673         return BT_HFP_AGENT_ERROR_NONE;
674 }
675
676 int _bt_hfp_send_vendor_cmd(bt_ag_info_t *hs,
677                 const char *cmd)
678 {
679         DBG("_bt_hfp_send_vendor_cmd = %d", cmd);
680
681         if (_bt_ag_send_at(hs, "\r\n%s\r\n", cmd) < 0)
682                 return BT_HFP_AGENT_ERROR_INTERNAL;
683
684         return BT_HFP_AGENT_ERROR_NONE;
685 }
686
687 int _bt_vendor_cmd_response(void *t_device,
688                         bt_hfp_agent_error_t err)
689 {
690         return _bt_ag_send_response(t_device, err);
691 }
692
693 int _bt_hfp_vendor_cmd(bt_ag_info_t *hs, const char *buf)
694 {
695         DBG("XSAT vendor command");
696
697         _bt_hfp_vendor_cmd_request(buf, hs);
698
699         return 0;
700 }
701
702 int _bt_list_current_call_indicator(bt_ag_info_t *hs, int index, int direction,
703         int mode, int status, const char *call_num, int conference, int t_num)
704 {
705         if (call_num && strlen(call_num) > 0) {
706                 _bt_ag_send_at(hs,
707                         "\r\n+CLCC: %d,%d,%d,%d,%d,\"%s\",%d\r\n",
708                         index, direction, status, mode, conference,
709                                 call_num, t_num);
710         } else {
711                 _bt_ag_send_at(hs,
712                         "\r\n+CLCC: %d,%d,%d,%d,%d\r\n",
713                         index, direction, status, mode, conference);
714         }
715
716         return 0;
717 }
718 int _bt_subscriber_number_indicator(const char *call_num, int type, int service)
719 {
720         if (!active_devices)
721                 return -ENODEV;
722
723         _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
724                                 "\r\n+CNUM: ,%s,%d,,%d\r\n",
725                                 call_num, type, service);
726         return 0;
727 }
728
729 int _bt_subscriber_number_response(void *t_device,
730                                         bt_hfp_agent_error_t err)
731 {
732         return _bt_ag_send_response(t_device, err);
733 }
734
735 int _bt_hfp_subscriber_number(bt_ag_info_t *hs, const char *buf)
736 {
737         _bt_hfp_subscriber_number_request(hs);
738
739         return 0;
740 }
741
742 int _bt_call_waiting_indicator(const char *number, int type)
743 {
744         if (!active_devices)
745                 return -ENODEV;
746
747         DBG("Call waiting indicator to hf");
748         _bt_ag_send_foreach_headset(active_devices, __bt_cwa_cmp,
749                                 "\r\n+CCWA: \"%s\",%d\r\n",
750                                 number, type);
751         return 0;
752 }
753
754 int _bt_list_current_calls_response(void *t_device,
755                                         bt_hfp_agent_error_t err)
756 {
757         return _bt_ag_send_response(t_device, err);
758 }
759
760 int _bt_hfp_list_current_calls(bt_ag_info_t *hs, const char *buf)
761 {
762         _bt_list_current_calls(hs);
763
764         return 0;
765 }
766
767 int _bt_hfp_extended_errors(bt_ag_info_t *hs, const char *buf)
768 {
769         bt_ag_slconn_t *slconn = hs->slc;
770
771         if (strlen(buf) < 9)
772                 return -EINVAL;
773
774         if (buf[8] == '1') {
775                 slconn->is_cme_enabled = TRUE;
776                 DBG("CME errors enabled for headset %p", hs);
777         } else {
778                 slconn->is_cme_enabled = FALSE;
779                 DBG("CME errors disabled for headset %p", hs);
780         }
781
782         return _bt_ag_send_at(hs, "\r\nOK\r\n");
783 }
784
785 int _bt_hfp_call_waiting_notify(bt_ag_info_t *hs, const char *buf)
786 {
787         bt_ag_slconn_t *slconn = hs->slc;
788
789         if (strlen(buf) < 9)
790                 return -EINVAL;
791
792         if (buf[8] == '1') {
793                 slconn->is_cwa_enabled = TRUE;
794                 DBG("Call waiting notification enabled for headset %p", hs);
795         } else {
796                 slconn->is_cwa_enabled = FALSE;
797                 DBG("Call waiting notification disabled for headset %p", hs);
798         }
799
800         return _bt_ag_send_at(hs, "\r\nOK\r\n");
801 }
802
803 int _bt_operator_selection_response(void *t_device,
804                                         bt_hfp_agent_error_t err)
805 {
806         return _bt_ag_send_response(t_device, err);
807 }
808
809 int _bt_call_hold_response(void *t_device, bt_hfp_agent_error_t err)
810 {
811         return _bt_ag_send_response(t_device, err);
812 }
813
814 int _bt_nr_and_ec_response(void *t_device, bt_hfp_agent_error_t err)
815 {
816         bt_ag_info_t *hs = t_device;
817         bt_ag_slconn_t *slconn = hs->slc;
818
819         if (err == (bt_hfp_agent_error_t)HFP_STATE_MNGR_ERR_NONE) {
820                 GSList *l;
821
822                 for (l = hs->nrec_cbs; l; l = l->next) {
823                         struct hs_nrec_callback *nrec_cb = l->data;
824
825                         nrec_cb->cb(hs, slconn->is_nrec_req,
826                                 nrec_cb->user_data);
827                 }
828
829                 slconn->is_nrec = hs->slc->is_nrec_req;
830         }
831
832         return _bt_ag_send_response(t_device, err);
833 }
834
835 int _bt_voice_dial_response(void *t_device, bt_hfp_agent_error_t err)
836 {
837         return _bt_ag_send_response(t_device, err);
838 }
839
840 int _bt_operator_selection_indicator(int mode, const char *oper)
841 {
842         if (!active_devices)
843                 return -ENODEV;
844
845         _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
846                                 "\r\n+COPS: %d,0,\"%s\"\r\n",
847                                 mode, oper);
848         return 0;
849 }
850
851 int _bt_hfp_operator_selection(bt_ag_info_t *hs, const char *buf)
852 {
853         if (strlen(buf) < 8)
854                 return -EINVAL;
855
856         switch (buf[7]) {
857         case '?':
858                 _bt_hfp_get_operator_selection_request(hs);
859                 break;
860         case '=': {
861                 if (buf[8] == '?')
862                         return _bt_ag_send_at(hs, "\r\n+CME ERROR: %d\r\n",
863                                 HFP_STATE_MNGR_ERR_NOT_SUPPORTED);
864                 else
865                         return _bt_ag_send_at(hs, "\r\nOK\r\n");
866         }
867         default:
868                 return -EINVAL;
869         }
870
871         return 0;
872 }
873
874 int _bt_hfp_nr_and_ec(bt_ag_info_t *hs, const char *buf)
875 {
876         bt_ag_slconn_t *slconn = hs->slc;
877
878         if (strlen(buf) < 9)
879                 return -EINVAL;
880
881         if (buf[8] == '0')
882                 slconn->is_nrec_req = FALSE;
883         else
884                 slconn->is_nrec_req = TRUE;
885
886         _bt_hfp_noise_red_and_echo_cancel_request(slconn->is_nrec_req, hs);
887
888         return 0;
889 }
890
891 int _bt_hfp_voice_dial(bt_ag_info_t *hs, const char *buf)
892 {
893         bt_ag_slconn_t *slconn = hs->slc;
894         gboolean enable;
895
896         if (strlen(buf) < 9)
897                 return -EINVAL;
898
899         if (buf[8] == '0')
900                 enable = FALSE;
901         else
902                 enable = TRUE;
903
904         if (remote_dev_path)
905                 g_free(remote_dev_path);
906
907         remote_dev_path = g_strdup(hs->path);
908
909         _bt_hfp_voice_dial_request(enable, hs);
910
911         slconn->is_voice_recognition_running = enable;
912
913         return 0;
914 }
915
916 int _bt_hfp_indicators_activation(bt_ag_info_t *hs, const char *buf)
917 {
918         if (strlen(buf) < 7) {
919                 ERR("Invalid indicator activation request\n");
920                 return -EINVAL;
921         }
922
923         _bt_hfp_set_indicators(&buf[6], hs);
924         return 0;
925 }
926
927 int _bt_indicators_activation_response(void *t_device,
928                                         bt_hfp_agent_error_t err)
929 {
930         return _bt_ag_send_response(t_device, err);
931 }
932
933 int _bt_select_phonebook_memory_status_response(void *t_device,
934                                                 const char *path,
935                                                 uint32_t total, uint32_t used,
936                                                 bt_hfp_agent_error_t err)
937 {
938         bt_ag_info_t *hs = t_device;
939         bt_ag_slconn_t *slconn = hs->slc;
940
941         if (err != (bt_hfp_agent_error_t)HFP_STATE_MNGR_ERR_NONE) {
942                 if (slconn->is_cme_enabled)
943                         return _bt_ag_send_at(hs,
944                                         "\r\n+CME ERROR: %d\r\n", err);
945                 else
946                         return _bt_ag_send_at(hs, "\r\nERROR\r\n");
947         }
948
949         if (!active_devices)
950                 return -ENODEV;
951
952         _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
953                         "\r\n+CPBS: %s,%d,%d\r\n",
954                         path, used, total);
955
956         return _bt_ag_send_at(hs, "\r\nOK\r\n");
957 }
958
959 int _bt_select_phonebook_memory_list_response(void *t_device,
960                                                 const char *buf,
961                                                 bt_hfp_agent_error_t err)
962 {
963         bt_ag_info_t *hs = t_device;
964         bt_ag_slconn_t *slconn = hs->slc;
965
966         if ((err != (bt_hfp_agent_error_t)HFP_STATE_MNGR_ERR_NONE) ||
967                                 (NULL == buf)) {
968                 if (slconn->is_cme_enabled)
969                         return _bt_ag_send_at(hs,
970                                         "\r\n+CME ERROR: %d\r\n", err);
971                 else
972                         return _bt_ag_send_at(hs, "\r\nERROR\r\n");
973         }
974
975         if (NULL != buf) {
976                 if (!active_devices)
977                         return -ENODEV;
978
979                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
980                                         "\r\n+CPBS: %s\r\n", buf);
981
982         }
983         return _bt_ag_send_at(hs, "\r\nOK\r\n");
984 }
985
986 int _bt_select_phonebook_memory_response(void *t_device,
987                                                 bt_hfp_agent_error_t err)
988 {
989         return _bt_ag_send_response(t_device, err);
990 }
991
992 static char *__bt_get_remote_device_name(const char *device_path)
993 {
994         char *name = NULL;
995         GVariant *value;
996         GVariant *result = NULL;
997         GError *err = NULL;
998         GDBusProxy *device_proxy;
999         GDBusConnection *conn;
1000
1001         if (device_path == NULL)
1002                 return NULL;
1003
1004         conn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, NULL);
1005
1006         INFO_SECURE("Device_path %s", device_path);
1007         device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1008                                                 NULL, "org.bluez",
1009                                                 device_path,
1010                                                 BT_PROPERTIES_INTERFACE,
1011                                                 NULL, &err);
1012
1013         result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1014                         g_variant_new("(s)", "org.bluez.Device1"),
1015                         G_DBUS_CALL_FLAGS_NONE,
1016                         20, NULL,
1017                         &err);
1018         if (err) {
1019                 ERR("DBus Error : %s", err->message);
1020                 g_clear_error(&err);
1021                 return NULL;
1022         }
1023         if (result == NULL) {
1024                 ERR("g_dbus_proxy_call_sync function return NULL");
1025                 return NULL;
1026         }
1027         g_variant_get(result, "(@a{sv})", &value);
1028
1029         if (value) {
1030                 GVariant *temp_value = g_variant_lookup_value(value, "Alias",
1031                         G_VARIANT_TYPE_STRING);
1032                 g_variant_get(temp_value, "(s)", &name);
1033                 if (temp_value)
1034                         g_variant_unref(temp_value);
1035
1036                 if (name != NULL)
1037                         INFO_SECURE("Alias Name [%s]", name);
1038                 else {
1039                         temp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
1040                         g_variant_get(temp_value, "s", &name);
1041                         if (temp_value)
1042                                 g_variant_unref(temp_value);
1043                         INFO_SECURE("Name = %s", name);
1044                 }
1045         }
1046         g_variant_unref(result);
1047         g_object_unref(device_proxy);
1048         return name;
1049 }
1050
1051 int _bt_launch_pbap_popup(const char *device_name,
1052                                                         const char *agent_path)
1053 {
1054         int ret;
1055         bundle *b;
1056         char event_str[50 + 1];
1057
1058         b = bundle_create();
1059         if (!b) {
1060                 ERR("Launching system popup failed");
1061                 return -1;
1062         }
1063
1064         bundle_add(b, "device-name", device_name);
1065         bundle_add(b, "agent-path", agent_path);
1066
1067         g_strlcpy(event_str, "phonebook-request", sizeof(event_str));
1068
1069         bundle_add(b, "event-type", event_str);
1070
1071         ret = syspopup_launch("bt-syspopup", b);
1072         if (0 > ret) {
1073                 ERR("Popup launch failed... %d", ret);
1074         }
1075
1076         bundle_free(b);
1077
1078         INFO("_bt_agent_launch_system_popup");
1079         return 0;
1080 }
1081
1082 int _bt_hfp_select_phonebook_memory_status_reply(bt_ag_info_t *hs)
1083 {
1084         const char *buf = hs->at_pbap_buf;
1085         if (hs->pbap_trusted == BT_AG_FEATURE_PBAP_BLOCKED
1086                 || hs->pbap_trusted == BT_AG_FEATURE_PBAP_CANCEL) {
1087                 INFO("Doesnot have Access reply with error");
1088                 _bt_select_phonebook_memory_response(hs,
1089                         HFP_STATE_MNGR_ERR_NOT_ALLOWED);
1090                 return 0;
1091         }
1092
1093         if (buf[7] == '?') {
1094                 _bt_hfp_select_phonebook_memory_status(hs);
1095                 return 0;
1096         }
1097
1098         if (buf[7] == '=') {
1099                 if (buf[8] == '?') {
1100                         _bt_hfp_select_phonebook_memory_list(hs);
1101                         return 0;
1102                 }
1103                 _bt_hfp_select_phonebook_memory(hs, &buf[8]);
1104         }
1105
1106         return 0;
1107 }
1108
1109 int _bt_hfp_select_pb_memory(bt_ag_info_t *hs, const char *buf)
1110 {
1111         if (strlen(buf) < 8)
1112                 return -EINVAL;
1113         INFO("Pbap_trusted %d", hs->pbap_trusted);
1114         if (hs->pbap_trusted == BT_AG_FEATURE_PBAP_SHOW_AUTHORIZATION
1115                 || hs->pbap_trusted == BT_AG_FEATURE_PBAP_CANCEL) {
1116                 strncpy(hs->at_pbap_buf, buf, sizeof(hs->at_pbap_buf) - 1);
1117                 /* Adding Popup here */
1118                 int trust = 0;
1119                 bt_initialize();
1120                 int err = bt_device_get_profile_trusted(hs->remote_addr, 1, &trust);
1121                 if (err != TIZEN_ERROR_NONE) {
1122                         INFO("Show authorization popup %d Error %d", trust, err);
1123                         char *name = __bt_get_remote_device_name(hs->path);
1124                         INFO_SECURE("remote device name %s", name);
1125                         _bt_launch_pbap_popup(name,
1126                                 BT_AG_AGENT_OBJECT_PATH);
1127                         return 0;
1128                 }
1129                 if (trust == 0) {
1130                         INFO("This device has been marked as blocked");
1131                         hs->pbap_trusted = BT_AG_FEATURE_PBAP_BLOCKED;
1132                         return -EACCES;
1133                 } else
1134                         hs->pbap_trusted = BT_AG_FEATURE_PBAP_ALLOWED;
1135         }
1136         if (hs->pbap_trusted == BT_AG_FEATURE_PBAP_ALLOWED) {
1137                 INFO("Authorized from PBAP");
1138                 if (buf[7] == '?') {
1139                         _bt_hfp_select_phonebook_memory_status(hs);
1140                         return 0;
1141                 }
1142
1143                 if (buf[7] == '=') {
1144                         if (buf[8] == '?') {
1145                                 _bt_hfp_select_phonebook_memory_list(hs);
1146                                 return 0;
1147                         }
1148                         _bt_hfp_select_phonebook_memory(hs, &buf[8]);
1149                         return 0;
1150                 }
1151         } else if (hs->pbap_trusted == BT_AG_FEATURE_PBAP_BLOCKED) {
1152                 INFO("Device is blocked");
1153                 /* Reply with Error */
1154                 return -EACCES;
1155         }
1156
1157         return 0;
1158 }
1159
1160 int _bt_read_phonebook_entries_list_response(void *t_device,
1161                                                 uint32_t used,
1162                                                 uint32_t number_length,
1163                                                 uint32_t name_length,
1164                                                 bt_hfp_agent_error_t err)
1165 {
1166         bt_ag_info_t *hs = t_device;
1167         bt_ag_slconn_t *slconn = hs->slc;
1168
1169         int send_err = 0;
1170         int index = 1;
1171
1172         if (err != (bt_hfp_agent_error_t)HFP_STATE_MNGR_ERR_NONE) {
1173                 if (slconn->is_cme_enabled)
1174                         return _bt_ag_send_at(hs,
1175                                         "\r\n+CME ERROR: %d\r\n", err);
1176                 else
1177                         return _bt_ag_send_at(hs, "\r\nERROR\r\n");
1178         }
1179
1180         if (used < 1)
1181                 used = 1;
1182
1183         send_err = _bt_ag_send_at(hs, "\r\n+CPBR: (%d-%d),%d,%d\r\n",
1184                         index, used, number_length, name_length);
1185         if (send_err < 0)
1186                 return send_err;
1187
1188         return _bt_ag_send_at(hs, "\r\nOK\r\n");
1189 }
1190
1191 int _bt_read_phonebook_entries_response(void *t_device,
1192                                         bt_hfp_agent_error_t err)
1193 {
1194         return _bt_ag_send_response(t_device, err);
1195 }
1196
1197 int _bt_read_phonebook_entries_indicator(const char *name, const char *number,
1198                                         uint32_t handle)
1199 {
1200         int type = 129;
1201         const char *pos = NULL;
1202
1203         pos = number;
1204         while (*pos == ' ' || *pos == '\t')
1205                 pos++;
1206
1207         /* 145 means international access code, otherwise 129 is used */
1208         if (*pos == '+')
1209                 type = 145;
1210
1211         _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1212                         "\r\n+CPBR: %d,\"%s\",%d,\"%s\"\r\n",
1213                         handle, number, type, name);
1214         return 0;
1215 }
1216
1217 int _bt_read_phonebook_entries_indicator_by_name(const char *name,
1218                                         const char *number, uint32_t handle)
1219 {
1220         int type = 129;
1221         const char *pos = NULL;
1222
1223         pos = number;
1224         while (*pos == ' ' || *pos == '\t')
1225                 pos++;
1226
1227         /* 145 means international access code, otherwise 129 is used */
1228         if (*pos == '+')
1229                 type = 145;
1230
1231         _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1232                 "\r\n+CPBF: %d,\"%s\",%d,\"%s\"\r\n",
1233                 handle, number, type, name);
1234         return 0;
1235 }
1236
1237 int _bt_hfp_read_pb_entries(bt_ag_info_t *hs, const char *buf)
1238 {
1239         if (hs->pbap_trusted != BT_AG_FEATURE_PBAP_ALLOWED)
1240                 return -EACCES;
1241
1242         if (strlen(buf) < 8)
1243                 return -EINVAL;
1244
1245         if (buf[7] != '=')
1246                 return -EINVAL;
1247
1248         if (buf[8] == '?')
1249                 _bt_hfp_read_phonebook_entries_list(hs);
1250         else
1251                 _bt_hfp_read_phonebook_entries(hs, &buf[8]);
1252
1253         return 0;
1254 }
1255
1256 int _bt_find_phonebook_entries_status_response(void *t_device,
1257                                                 bt_hfp_agent_error_t err)
1258 {
1259         return _bt_ag_send_response(t_device, err);
1260 }
1261
1262 int _bt_find_phonebook_entries_response(void *t_device,
1263                                         bt_hfp_agent_error_t err)
1264 {
1265         return _bt_ag_send_response(t_device, err);
1266 }
1267
1268 int _bt_find_phonebook_entries_status_indicator(uint32_t number_length,
1269                                         uint32_t name_length)
1270 {
1271         _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1272                         "\r\n+CPBF: %d,%d\r\n",
1273                         number_length, name_length);
1274
1275         return 0;
1276 }
1277
1278 int _bt_hfp_find_pb_entires(bt_ag_info_t *hs, const char *buf)
1279 {
1280         /* Check if Contact access is permitted or not */
1281         if (hs->pbap_trusted != BT_AG_FEATURE_PBAP_ALLOWED)
1282                 return -EACCES;
1283
1284         if (strlen(buf) < 8)
1285                 return -EINVAL;
1286
1287         if (buf[7] != '=')
1288                 return -EINVAL;
1289
1290         if (buf[8] == '?')
1291                 _bt_hfp_find_phonebook_entries_status(hs);
1292         else
1293                 _bt_hfp_find_phonebook_entries(hs, &buf[8]);
1294
1295         return 0;
1296 }
1297
1298 int _bt_supported_character_generic_response(void *t_device,
1299                                                 char *character_set_list,
1300                                                 bt_hfp_agent_error_t err)
1301 {
1302         bt_ag_info_t *hs = t_device;
1303         bt_ag_slconn_t *slconn = hs->slc;
1304
1305         if (err != (bt_hfp_agent_error_t)HFP_STATE_MNGR_ERR_NONE) {
1306                 if (slconn->is_cme_enabled)
1307                         return _bt_ag_send_at(hs,
1308                                         "\r\n+CME ERROR: %d\r\n", err);
1309                 else
1310                         return _bt_ag_send_at(hs, "\r\nERROR\r\n");
1311         }
1312
1313         if (NULL != character_set_list) {
1314                 if (!active_devices)
1315                         return -ENODEV;
1316
1317                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1318                         "\r\n+CSCS: %s\r\n", character_set_list);
1319         }
1320         return _bt_ag_send_at(hs, "\r\nOK\r\n");
1321 }
1322
1323 int _bt_set_characterset_generic_response(void *t_device,
1324                                         bt_hfp_agent_error_t err)
1325 {
1326         return _bt_ag_send_response(t_device, err);
1327 }
1328
1329 int _bt_hfp_select_character_set(bt_ag_info_t *hs, const char *buf)
1330 {
1331         if (NULL != buf) {
1332                 if (strlen(buf) < 7)
1333                         return -EINVAL;
1334
1335                 if (buf[7] == '?') {
1336                         _bt_hfp_get_character_set(hs);
1337                         return 0;
1338                 }
1339
1340                 if (buf[7] == '=') {
1341                         if (buf[8] == '?')
1342                                 _bt_hfp_list_supported_character(hs);
1343                         else
1344                                 _bt_hfp_set_character_set(hs, &buf[8]);
1345                 }
1346         }
1347         return 0;
1348
1349 }
1350
1351 int _bt_battery_charge_status_response(void *t_device,
1352                                                 int32_t bcs,
1353                                                 int32_t bcl,
1354                                                 bt_hfp_agent_error_t err)
1355 {
1356         bt_ag_info_t *hs = t_device;
1357
1358         if (err == (bt_hfp_agent_error_t)HFP_STATE_MNGR_ERR_NONE) {
1359                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1360                                         "\r\n+CBC: %d,%d\r\n", bcs, bcl);
1361         }
1362
1363         return _bt_ag_send_response(hs, err);
1364 }
1365
1366 int _bt_hfp_get_battery_charge_status(bt_ag_info_t *hs, const char *buf)
1367 {
1368         if (strlen(buf) < 6)
1369                 return -EINVAL;
1370
1371         if (buf[6] == '=')
1372                 return _bt_ag_send_response(hs, HFP_STATE_MNGR_ERR_NONE);
1373
1374         _bt_hfp_get_battery_property(hs);
1375         return 0;
1376 }
1377
1378 int _bt_hfp_apl_command(bt_ag_info_t *hs, const char *buf)
1379 {
1380         DBG("Got Apple command: %s", buf);
1381
1382         /*we don't support XAPL commands*/
1383         return _bt_ag_send_response(hs, HFP_STATE_MNGR_ERR_NOT_SUPPORTED);
1384 }
1385
1386 /* convert signal strength to a RSSI level */
1387 static int __bt_telephony_convert_signal_to_rssi(int signal)
1388 {
1389         /* input  : BT signal strength (0~5) */
1390         /* output : RSSI strength (0~31) */
1391         switch (signal) {
1392         case 0: return 0;
1393         case 1: return 4;
1394         case 2: return 8;
1395         case 3: return 13;
1396         case 4: return 19;
1397         case 5: return 31;
1398         }
1399
1400         if (signal > 5)
1401                 return 31;
1402
1403         return 0;
1404 }
1405
1406 int _bt_signal_quality_response(void *t_device,
1407                                                 int32_t rssi,
1408                                                 int32_t ber,
1409                                                 bt_hfp_agent_error_t err)
1410 {
1411         bt_ag_info_t *hs = t_device;
1412
1413         if (err == (bt_hfp_agent_error_t)HFP_STATE_MNGR_ERR_NONE) {
1414                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1415                         "\r\n+CSQ: %d,%d\r\n",
1416                         __bt_telephony_convert_signal_to_rssi(rssi), ber);
1417         }
1418         return _bt_ag_send_response(hs, err);
1419 }
1420
1421 int _bt_telephony_signal_quality_list_supported_response(void *t_device,
1422                                                 bt_hfp_agent_error_t err)
1423 {
1424         bt_ag_info_t *device = t_device;
1425
1426         if (err == (bt_hfp_agent_error_t)HFP_STATE_MNGR_ERR_NONE) {
1427                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1428                                         "\r\n+CSQ: (0-31,99),(99)\r\n");
1429         }
1430         return _bt_ag_send_response(device, err);
1431 }
1432
1433 int _bt_hfp_get_signal_quality(bt_ag_info_t *hs, const char *buf)
1434 {
1435         if (strlen(buf) < 6)
1436                 return -EINVAL;
1437
1438         if (buf[6] == '=')
1439                 _bt_telephony_signal_quality_list_supported_response(hs,
1440                                         HFP_STATE_MNGR_ERR_NONE);
1441         else
1442                 _bt_ag_agent_get_signal_quality(hs);
1443
1444         return 0;
1445 }
1446
1447 int _bt_hfp_get_activity_status_rsp(void *t_device,
1448                                                 int status,
1449                                                 bt_hfp_agent_error_t err)
1450 {
1451         bt_ag_info_t *device = t_device;
1452
1453         if (err == (bt_hfp_agent_error_t)HFP_STATE_MNGR_ERR_NONE) {
1454                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1455                                         "\r\n+CPAS: %d\r\n", status);
1456         }
1457
1458         return _bt_ag_send_response(device, err);
1459 }
1460
1461 int _bt_hfp_get_activity_status(bt_ag_info_t *device, const char *buf)
1462 {
1463         if (strlen(buf) < 7)
1464                 return -EINVAL;
1465
1466         if (buf[7] == '?') {
1467                 return _bt_ag_send_response(device,
1468                                         HFP_STATE_MNGR_ERR_AG_FAILURE);
1469         } else if (buf[7] == '=') {
1470                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1471                                         "\r\n+CPAS: (0-4)\r\n");
1472                 return _bt_ag_send_response(device, HFP_STATE_MNGR_ERR_NONE);
1473         }
1474
1475         _bt_get_activity_status(device);
1476         return 0;
1477 }
1478
1479 int _bt_hfp_get_equipment_identity_rsp(void *t_device,
1480                                 char *identity, bt_hfp_agent_error_t err)
1481 {
1482         if (identity)
1483                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1484                 "\r\n+CGSN: %s\r\n", identity);
1485         return _bt_ag_send_response(t_device, err);
1486 }
1487
1488 int _bt_hfp_get_imsi_rsp(void *t_device,
1489                 char *mcc, char *mnc, char *msin, bt_hfp_agent_error_t err)
1490 {
1491         if (err == (bt_hfp_agent_error_t)HFP_STATE_MNGR_ERR_NONE)
1492                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1493                                 "\r\n%s%s%s\r\n", mcc, mnc, msin);
1494         return _bt_ag_send_response(t_device, err);
1495 }
1496
1497 int _bt_hfp_get_creg_status_rsp(void *t_device,
1498                 int n, int status, bt_hfp_agent_error_t err)
1499 {
1500         if (err == (bt_hfp_agent_error_t)HFP_STATE_MNGR_ERR_NONE)
1501                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1502                                 "\r\n+CREG: %d,%d\r\n", n, status);
1503         return _bt_ag_send_response(t_device, err);
1504 }
1505
1506 int _bt_hfp_get_equipment_identity(bt_ag_info_t *device, const char *buf)
1507 {
1508         int len = strlen(buf);
1509
1510         if (len == 9 && buf[7] == '=' && buf[8] == '?')  /* AT+CGSN=? */
1511                 return _bt_ag_send_response(device, HFP_STATE_MNGR_ERR_NONE);
1512
1513         else if (len > 7)
1514                 return -EINVAL;
1515
1516         _bt_hfp_get_equipment_identity_req(device); /* AT+CGSN */
1517         return 0;
1518 }
1519
1520 int _bt_hfp_get_model_info_rsp(void *t_device, char *model,
1521                                                 bt_hfp_agent_error_t err)
1522 {
1523         if (model)
1524                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1525                 "\r\n+CGMM: %s\r\n", model);
1526         return _bt_ag_send_response(t_device, err);
1527 }
1528
1529 int _bt_hfp_get_model_information(bt_ag_info_t *device, const char *buf)
1530 {
1531         int len = strlen(buf);
1532
1533         if (len == 9 && buf[7] == '=' && buf[8] == '?')  /* AT+CGMM=? */
1534                 return _bt_ag_send_response(device, HFP_STATE_MNGR_ERR_NONE);
1535
1536         else if (len > 7)
1537                 return -EINVAL;
1538
1539         _bt_hfp_get_model_info_req(device);/* AT+CGMM */
1540         return 0;
1541 }
1542
1543 int _bt_hfp_get_device_manufacturer_rsp(void *t_device,
1544                                 char *manufacturer, bt_hfp_agent_error_t err)
1545 {
1546         if (manufacturer)
1547                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1548                 "\r\n+CGMI: %s\r\n", manufacturer);
1549         return _bt_ag_send_response(t_device, err);
1550 }
1551
1552 int _bt_hfp_get_device_manufacturer(bt_ag_info_t *device, const char *buf)
1553 {
1554         int len = strlen(buf);
1555
1556         if (len == 9 && buf[7] == '=' && buf[8] == '?')  /* AT+CGMI=? */
1557                 return _bt_ag_send_response(device, HFP_STATE_MNGR_ERR_NONE);
1558
1559         else if (len > 7)
1560                 return -EINVAL;
1561
1562         _bt_hfp_get_device_manufacturer_req(device);
1563         return 0;
1564 }
1565
1566 int _bt_hfp_get_imsi(bt_ag_info_t *device, const char *buf)
1567 {
1568         int len = strlen(buf);
1569         DBG_SECURE("Buf %s", buf);
1570
1571         if (len == 7)
1572                 _bt_hfp_get_imsi_req(device);
1573         else
1574                 _bt_ag_send_response(device, HFP_STATE_MNGR_ERR_INVALID_INDEX);
1575
1576         return 0;
1577 }
1578
1579 int _bt_hfp_get_creg_status(bt_ag_info_t *device, const char *buf)
1580 {
1581         int len = strlen(buf);
1582         DBG_SECURE("buf %s", buf);
1583         if (len < 7 || len > 9)
1584                 return -EINVAL;
1585         else if (len == 7)
1586                 _bt_ag_send_response(device, HFP_STATE_MNGR_ERR_INVALID_INDEX);
1587         else if (buf[7] == '=')
1588                 _bt_ag_send_response(device, HFP_STATE_MNGR_ERR_INVALID_INDEX);
1589         else if (buf[7] == '?')
1590                 _bt_hfp_get_creg_status_req(device);
1591
1592         return 0;
1593 }
1594 int _bt_hfp_get_revision_info_rsp(void *t_device, char *revision,
1595                                                 bt_hfp_agent_error_t err)
1596 {
1597         if (revision)
1598                 _bt_ag_send_foreach_headset(active_devices, __bt_hfp_cmp,
1599                 "\r\n+CGMR: %s\r\n", revision);
1600         return _bt_ag_send_response(t_device, err);
1601 }
1602
1603 int _bt_hfp_get_revision_information(bt_ag_info_t *device, const char *buf)
1604 {
1605         int len = strlen(buf);
1606
1607         if (len == 9 && buf[7] == '=' && buf[8] == '?')  /* AT+CGMR=? */
1608                 return _bt_ag_send_response(device, HFP_STATE_MNGR_ERR_NONE);
1609
1610         else if (len > 7)
1611                 return -EINVAL;
1612
1613         _bt_hfp_get_revision_info_req(device);
1614         return 0;
1615 }