forked from edrosten/bluez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandsfree-client.c
2204 lines (1725 loc) · 48.9 KB
/
handsfree-client.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2014 Intel Corporation. All rights reserved.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>
#include <unistd.h>
#include <glib.h>
#include "lib/bluetooth.h"
#include "lib/sdp.h"
#include "lib/sdp_lib.h"
#include "src/sdp-client.h"
#include "src/shared/hfp.h"
#include "src/shared/queue.h"
#include "src/shared/util.h"
#include "btio/btio.h"
#include "ipc.h"
#include "ipc-common.h"
#include "src/log.h"
#include "utils.h"
#include "bluetooth.h"
#include "hal-msg.h"
#include "handsfree-client.h"
#include "sco.h"
#define HFP_HF_CHANNEL 7
#define HFP_HF_FEAT_ECNR 0x00000001
#define HFP_HF_FEAT_3WAY 0x00000002
#define HFP_HF_FEAT_CLI 0x00000004
#define HFP_HF_FEAT_VR 0x00000008
#define HFP_HF_FEAT_RVC 0x00000010
#define HFP_HF_FEAT_ECS 0x00000020
#define HFP_HF_FEAT_ECC 0x00000040
#define HFP_HF_FEAT_CODEC 0x00000080
#define HFP_HF_FEAT_HF_IND 0x00000100
#define HFP_HF_FEAT_ESCO_S4_T2 0x00000200
#define HFP_AG_FEAT_3WAY 0x00000001
#define HFP_AG_FEAT_ECNR 0x00000002
#define HFP_AG_FEAT_VR 0x00000004
#define HFP_AG_FEAT_INBAND 0x00000008
#define HFP_AG_FEAT_VTAG 0x00000010
#define HFP_AG_FEAT_REJ_CALL 0x00000020
#define HFP_AG_FEAT_ECS 0x00000040
#define HFP_AG_FEAT_ECC 0x00000080
#define HFP_AG_FEAT_EXT_ERR 0x00000100
#define HFP_AG_FEAT_CODEC 0x00000200
#define HFP_HF_FEATURES (HFP_HF_FEAT_ECNR | HFP_HF_FEAT_3WAY |\
HFP_HF_FEAT_CLI | HFP_HF_FEAT_VR |\
HFP_HF_FEAT_RVC | HFP_HF_FEAT_ECS |\
HFP_HF_FEAT_ECC)
#define CVSD_OFFSET 0
#define MSBC_OFFSET 1
#define CODECS_COUNT (MSBC_OFFSET + 1)
#define CODEC_ID_CVSD 0x01
#define CODEC_ID_MSBC 0x02
#define MAX_NUMBER_LEN 33
#define MAX_OPERATOR_NAME_LEN 17
enum hfp_indicator {
HFP_INDICATOR_SERVICE = 0,
HFP_INDICATOR_CALL,
HFP_INDICATOR_CALLSETUP,
HFP_INDICATOR_CALLHELD,
HFP_INDICATOR_SIGNAL,
HFP_INDICATOR_ROAM,
HFP_INDICATOR_BATTCHG,
HFP_INDICATOR_LAST
};
typedef void (*ciev_func_t)(uint8_t val);
struct indicator {
uint8_t index;
uint32_t min;
uint32_t max;
uint32_t val;
ciev_func_t cb;
};
struct hfp_codec {
uint8_t type;
bool local_supported;
bool remote_supported;
};
struct device {
bdaddr_t bdaddr;
struct hfp_hf *hf;
uint8_t state;
uint8_t audio_state;
uint8_t negotiated_codec;
uint32_t features;
struct hfp_codec codecs[2];
struct indicator ag_ind[HFP_INDICATOR_LAST];
uint32_t chld_features;
};
static const struct hfp_codec codecs_defaults[] = {
{ CODEC_ID_CVSD, true, false},
{ CODEC_ID_MSBC, false, false},
};
static bdaddr_t adapter_addr;
static struct ipc *hal_ipc = NULL;
static uint32_t hfp_hf_features = 0;
static uint32_t hfp_hf_record_id = 0;
static struct queue *devices = NULL;
static GIOChannel *hfp_hf_server = NULL;
static struct bt_sco *sco = NULL;
static struct device *find_default_device(void)
{
return queue_peek_head(devices);
}
static bool match_by_bdaddr(const void *data, const void *user_data)
{
const bdaddr_t *addr1 = data;
const bdaddr_t *addr2 = user_data;
return !bacmp(addr1, addr2);
}
static struct device *find_device(const bdaddr_t *addr)
{
return queue_find(devices, match_by_bdaddr, addr);
}
static void init_codecs(struct device *dev)
{
memcpy(&dev->codecs, codecs_defaults, sizeof(dev->codecs));
if (hfp_hf_features & HFP_HF_FEAT_CODEC)
dev->codecs[MSBC_OFFSET].local_supported = true;
}
static struct device *device_create(const bdaddr_t *bdaddr)
{
struct device *dev;
dev = new0(struct device, 1);
bacpy(&dev->bdaddr, bdaddr);
dev->state = HAL_HF_CLIENT_CONN_STATE_DISCONNECTED;
dev->audio_state = HAL_HF_CLIENT_AUDIO_STATE_DISCONNECTED;
init_codecs(dev);
queue_push_tail(devices, dev);
return dev;
}
static struct device *get_device(const bdaddr_t *addr)
{
struct device *dev;
dev = find_device(addr);
if (dev)
return dev;
/* We do support only one device as for now */
if (queue_isempty(devices))
return device_create(addr);
return NULL;
}
static void device_set_state(struct device *dev, uint8_t state)
{
struct hal_ev_hf_client_conn_state ev;
char address[18];
if (dev->state == state)
return;
memset(&ev, 0, sizeof(ev));
dev->state = state;
ba2str(&dev->bdaddr, address);
DBG("device %s state %u", address, state);
bdaddr2android(&dev->bdaddr, ev.bdaddr);
ev.state = state;
ev.chld_feat = dev->chld_features;
ev.peer_feat = dev->features;
ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_EV_HF_CLIENT_CONN_STATE, sizeof(ev), &ev);
}
static void device_destroy(struct device *dev)
{
device_set_state(dev, HAL_HF_CLIENT_CONN_STATE_DISCONNECTED);
queue_remove(devices, dev);
if (dev->hf)
hfp_hf_unref(dev->hf);
free(dev);
}
static void handle_disconnect(const void *buf, uint16_t len)
{
const struct hal_cmd_hf_client_disconnect *cmd = buf;
struct device *dev;
uint32_t status;
bdaddr_t bdaddr;
char addr[18];
DBG("");
android2bdaddr(&cmd->bdaddr, &bdaddr);
ba2str(&bdaddr, addr);
DBG("Disconnect %s", addr);
dev = get_device(&bdaddr);
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
if (dev->state == HAL_HF_CLIENT_CONN_STATE_DISCONNECTED) {
status = HAL_STATUS_FAILED;
goto done;
}
if (dev->state == HAL_HF_CLIENT_CONN_STATE_DISCONNECTING) {
status = HAL_STATUS_SUCCESS;
goto done;
}
if (dev->state == HAL_HF_CLIENT_CONN_STATE_CONNECTING) {
device_destroy(dev);
status = HAL_STATUS_SUCCESS;
goto done;
}
status = hfp_hf_disconnect(dev->hf) ? HAL_STATUS_SUCCESS :
HAL_STATUS_FAILED;
if (status)
device_set_state(dev, HAL_HF_CLIENT_CONN_STATE_DISCONNECTING);
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_DISCONNECT, status);
}
static void set_audio_state(struct device *dev, uint8_t state)
{
struct hal_ev_hf_client_audio_state ev;
char address[18];
if (dev->audio_state == state)
return;
dev->audio_state = state;
ba2str(&dev->bdaddr, address);
DBG("device %s audio state %u", address, state);
bdaddr2android(&dev->bdaddr, ev.bdaddr);
ev.state = state;
ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_EV_HF_CLIENT_AUDIO_STATE, sizeof(ev), &ev);
}
static void bcc_cb(enum hfp_result result, enum hfp_error cme_err,
void *user_data)
{
struct device *dev = user_data;
if (result != HFP_RESULT_OK)
set_audio_state(dev, HAL_HF_CLIENT_AUDIO_STATE_DISCONNECTED);
}
static bool codec_negotiation_supported(struct device *dev)
{
return (dev->features & HFP_AG_FEAT_CODEC) &&
(hfp_hf_features & HFP_HF_FEAT_CODEC);
}
static bool connect_sco(struct device *dev)
{
if (codec_negotiation_supported(dev))
return hfp_hf_send_command(dev->hf, bcc_cb, dev,
"AT+BCC");
return bt_sco_connect(sco, &dev->bdaddr, BT_VOICE_CVSD_16BIT);
}
static void handle_connect_audio(const void *buf, uint16_t len)
{
const struct hal_cmd_hf_client_connect_audio *cmd = (void *) buf;
struct device *dev;
uint8_t status;
bdaddr_t bdaddr;
DBG("");
android2bdaddr(&cmd->bdaddr, &bdaddr);
dev = find_device(&bdaddr);
if (!dev || dev->state != HAL_HF_CLIENT_CONN_STATE_SLC_CONNECTED ||
dev->audio_state != HAL_HF_CLIENT_AUDIO_STATE_DISCONNECTED) {
error("hf-client: Cannot create SCO, check SLC or audio state");
status = HAL_STATUS_FAILED;
goto done;
}
if (connect_sco(dev)) {
status = HAL_STATUS_SUCCESS;
set_audio_state(dev, HAL_HF_CLIENT_AUDIO_STATE_CONNECTING);
} else {
status = HAL_STATUS_FAILED;
}
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_CONNECT_AUDIO, status);
}
static void handle_disconnect_audio(const void *buf, uint16_t len)
{
const struct hal_cmd_hf_client_disconnect_audio *cmd = (void *) buf;
struct device *dev;
uint8_t status;
bdaddr_t bdaddr;
DBG("");
android2bdaddr(&cmd->bdaddr, &bdaddr);
dev = find_device(&bdaddr);
if (!dev ||
dev->audio_state == HAL_HF_CLIENT_AUDIO_STATE_DISCONNECTED) {
error("hf-client: Device not found or audio not connected");
status = HAL_STATUS_FAILED;
goto done;
}
bt_sco_disconnect(sco);
status = HAL_STATUS_SUCCESS;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_DISCONNECT_AUDIO, status);
}
static void cmd_complete_cb(enum hfp_result result, enum hfp_error cme_err,
void *user_data)
{
struct hal_ev_hf_client_command_complete ev;
DBG("");
memset(&ev, 0, sizeof(ev));
switch (result) {
case HFP_RESULT_OK:
ev.type = HAL_HF_CLIENT_CMD_COMP_OK;
break;
case HFP_RESULT_NO_CARRIER:
ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_NO_CARRIER;
break;
case HFP_RESULT_ERROR:
ev.type = HAL_HF_CLIENT_CMD_COMP_ERR;
break;
case HFP_RESULT_BUSY:
ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_BUSY;
break;
case HFP_RESULT_NO_ANSWER:
ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_NO_ANSWER;
break;
case HFP_RESULT_DELAYED:
ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_DELAYED;
break;
case HFP_RESULT_BLACKLISTED:
ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_BACKLISTED;
break;
case HFP_RESULT_CME_ERROR:
ev.type = HAL_HF_CLIENT_CMD_COMP_ERR_CME;
ev.cme = cme_err;
break;
case HFP_RESULT_CONNECT:
case HFP_RESULT_RING:
case HFP_RESULT_NO_DIALTONE:
default:
error("hf-client: Unknown error code %d", result);
ev.type = HAL_HF_CLIENT_CMD_COMP_ERR;
break;
}
ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_EV_CLIENT_COMMAND_COMPLETE, sizeof(ev), &ev);
}
static void handle_start_vr(const void *buf, uint16_t len)
{
struct device *dev;
uint8_t status;
DBG("");
dev = find_default_device();
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+BVRA=1"))
status = HAL_STATUS_SUCCESS;
else
status = HAL_STATUS_FAILED;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_START_VR, status);
}
static void handle_stop_vr(const void *buf, uint16_t len)
{
struct device *dev;
uint8_t status;
DBG("");
dev = find_default_device();
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+BVRA=0"))
status = HAL_STATUS_SUCCESS;
else
status = HAL_STATUS_FAILED;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_STOP_VR, status);
}
static void handle_volume_control(const void *buf, uint16_t len)
{
const struct hal_cmd_hf_client_volume_control *cmd = buf;
struct device *dev;
uint8_t status;
uint8_t vol;
bool ret;
DBG("");
dev = find_default_device();
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
/*
* Volume is in the range 0-15. Make sure we send correct value
* to remote device
*/
vol = cmd->volume > 15 ? 15 : cmd->volume;
switch (cmd->type) {
case HF_CLIENT_VOLUME_TYPE_SPEAKER:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"AT+VGS=%u", vol);
break;
case HF_CLIENT_VOLUME_TYPE_MIC:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"AT+VGM=%u", vol);
break;
default:
ret = false;
break;
}
status = ret ? HAL_STATUS_SUCCESS : HAL_STATUS_FAILED;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_VOLUME_CONTROL,
status);
}
static void handle_dial(const void *buf, uint16_t len)
{
const struct hal_cmd_hf_client_dial *cmd = buf;
struct device *dev;
uint8_t status;
bool ret;
DBG("");
if (len != sizeof(*cmd) + cmd->number_len)
goto failed;
dev = find_default_device();
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
if (cmd->number_len > 0) {
if (cmd->number[cmd->number_len - 1] != '\0')
goto failed;
DBG("Dialing %s", cmd->number);
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"ATD%s;", cmd->number);
} else {
DBG("Redialing");
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"AT+BLDN");
}
status = ret ? HAL_STATUS_SUCCESS : HAL_STATUS_FAILED;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_DIAL, status);
return;
failed:
error("Malformed number data, size (%u bytes), terminating", len);
raise(SIGTERM);
}
static void handle_dial_memory(const void *buf, uint16_t len)
{
const struct hal_cmd_hf_client_dial_memory *cmd = buf;
struct device *dev;
uint8_t status;
DBG("");
dev = find_default_device();
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
/* For some reason location in BT HAL is int. Therefore that check */
if (cmd->location < 0) {
status = HAL_STATUS_FAILED;
goto done;
}
if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL , "ATD>%d;",
cmd->location))
status = HAL_STATUS_SUCCESS;
else
status = HAL_STATUS_FAILED;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_DIAL_MEMORY, status);
}
static void handle_call_action(const void *buf, uint16_t len)
{
const struct hal_cmd_hf_client_call_action *cmd = buf;
struct device *dev;
uint8_t status;
bool ret;
DBG("");
dev = find_default_device();
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
switch (cmd->action) {
case HAL_HF_CLIENT_ACTION_CHLD_0:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"AT+CHLD=0");
break;
case HAL_HF_CLIENT_ACTION_CHLD_1:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"AT+CHLD=1");
break;
case HAL_HF_CLIENT_ACTION_CHLD_2:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb,
NULL, "AT+CHLD=2");
break;
case HAL_HF_CLIENT_ACTION_CHLD_3:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"AT+CHLD=3");
break;
case HAL_HF_CLIENT_ACTION_CHLD_4:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"AT+CHLD=4");
break;
case HAL_HF_CLIENT_ACTION_CHLD_1x:
/* Index is int in BT HAL. Let's be paranoid here */
if (cmd->index <= 0)
ret = false;
else
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb,
NULL, "AT+CHLD=1%d", cmd->index);
break;
case HAL_HF_CLIENT_ACTION_CHLD_2x:
/* Index is int in BT HAL. Let's be paranoid here */
if (cmd->index <= 0)
ret = false;
else
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb,
NULL, "AT+CHLD=2%d", cmd->index);
break;
case HAL_HF_CLIENT_ACTION_ATA:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"ATA");
break;
case HAL_HF_CLIENT_ACTION_CHUP:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"AT+CHUP");
break;
case HAL_HF_CLIENT_ACTION_BRTH_0:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"AT+BTRH=0");
break;
case HAL_HF_CLIENT_ACTION_BRTH_1:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"AT+BTRH=1");
break;
case HAL_HF_CLIENT_ACTION_BRTH_2:
ret = hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL,
"AT+BTRH=2");
break;
default:
error("hf-client: Unknown action %d", cmd->action);
ret = false;
break;
}
status = ret ? HAL_STATUS_SUCCESS : HAL_STATUS_FAILED;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_CALL_ACTION, status);
}
static void handle_query_current_calls(const void *buf, uint16_t len)
{
struct device *dev;
uint8_t status;
DBG("");
dev = find_default_device();
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+CLCC"))
status = HAL_STATUS_SUCCESS;
else
status = HAL_STATUS_FAILED;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_QUERY_CURRENT_CALLS,
status);
}
static void handle_query_operator_name(const void *buf, uint16_t len)
{
struct device *dev;
uint8_t status;
DBG("");
dev = find_default_device();
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+COPS?"))
status = HAL_STATUS_SUCCESS;
else
status = HAL_STATUS_FAILED;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_QUERY_OPERATOR_NAME,
status);
}
static void handle_retrieve_subscr_info(const void *buf, uint16_t len)
{
struct device *dev;
uint8_t status;
DBG("");
dev = find_default_device();
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+CNUM"))
status = HAL_STATUS_SUCCESS;
else
status = HAL_STATUS_FAILED;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_RETRIEVE_SUBSCR_INFO,
status);
}
static void handle_send_dtmf(const void *buf, uint16_t len)
{
const struct hal_cmd_hf_client_send_dtmf *cmd = buf;
struct device *dev;
uint8_t status;
dev = find_default_device();
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+VTS=%c",
(char) cmd->tone))
status = HAL_STATUS_SUCCESS;
else
status = HAL_STATUS_FAILED;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_SEND_DTMF, status);
}
static void handle_get_last_vc_tag_num(const void *buf, uint16_t len)
{
struct device *dev;
uint8_t status;
dev = find_default_device();
if (!dev) {
status = HAL_STATUS_FAILED;
goto done;
}
if (hfp_hf_send_command(dev->hf, cmd_complete_cb, NULL, "AT+BINP=1"))
status = HAL_STATUS_SUCCESS;
else
status = HAL_STATUS_FAILED;
done:
ipc_send_rsp(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_OP_HF_CLIENT_GET_LAST_VOICE_TAG_NUM, status);
}
static void disconnect_watch(void *user_data)
{
DBG("");
device_destroy(user_data);
}
static void slc_error(struct device *dev)
{
error("hf-client: Could not create SLC - dropping connection");
hfp_hf_disconnect(dev->hf);
}
static void set_chld_feat(struct device *dev, char *feat)
{
DBG(" %s", feat);
if (strcmp(feat, "0") == 0)
dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_REL;
else if (strcmp(feat, "1") == 0)
dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_REL_ACC;
else if (strcmp(feat, "1x") == 0)
dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_REL_X;
else if (strcmp(feat, "2") == 0)
dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_HOLD_ACC;
else if (strcmp(feat, "2x") == 0)
dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_PRIV_X;
else if (strcmp(feat, "3") == 0)
dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_MERGE;
else if (strcmp(feat, "4") == 0)
dev->chld_features |= HAL_HF_CLIENT_CHLD_FEAT_MERGE_DETACH;
}
static void get_local_codecs_string(struct device *dev, char *buf,
uint8_t len)
{
int i;
uint8_t offset;
memset(buf, 0, len);
offset = 0;
for (i = 0; i < CODECS_COUNT; i++) {
char c[8];
int l;
if (!dev->codecs[i].local_supported)
continue;
memset(c, 0, sizeof(c));
l = sprintf(c, "%d,", dev->codecs[i].type);
if (l > (len - offset - 1)) {
error("hf-client: Codecs cannot fit into buffer");
return;
}
strcat(&buf[offset], c);
offset += l;
}
}
static void bvra_cb(struct hfp_context *context, void *user_data)
{
struct hal_ev_hf_client_vr_state ev;
unsigned int val;
if (!hfp_context_get_number(context, &val) || val > 1)
return;
ev.state = val ? HAL_HF_CLIENT_VR_STARTED : HAL_HF_CLIENT_VR_STOPPED;
ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_EV_HF_CLIENT_VR_STATE, sizeof(ev), &ev);
}
static void vgm_cb(struct hfp_context *context, void *user_data)
{
struct hal_ev_hf_client_volume_changed ev;
unsigned int val;
if (!hfp_context_get_number(context, &val) || val > 15)
return;
ev.type = HF_CLIENT_VOLUME_TYPE_MIC;
ev.volume = val;
ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_EV_HF_CLIENT_VR_STATE, sizeof(ev), &ev);
}
static void vgs_cb(struct hfp_context *context, void *user_data)
{
struct hal_ev_hf_client_volume_changed ev;
unsigned int val;
if (!hfp_context_get_number(context, &val) || val > 15)
return;
ev.type = HF_CLIENT_VOLUME_TYPE_SPEAKER;
ev.volume = val;
ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_EV_CLIENT_VOLUME_CHANGED, sizeof(ev), &ev);
}
static void brth_cb(struct hfp_context *context, void *user_data)
{
struct hal_ev_hf_client_response_and_hold_status ev;
unsigned int val;
DBG("");
if (!hfp_context_get_number(context, &val) ||
val > HAL_HF_CLIENT_RESP_AND_HOLD_STATUS_REJECT) {
error("hf-client: incorrect BTRH response ");
return;
}
ev.status = val;
ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_EV_HF_CLIENT_RESPONSE_AND_HOLD_STATUS,
sizeof(ev), &ev);
}
static void clcc_cb(struct hfp_context *context, void *user_data)
{
uint8_t buf[IPC_MTU];
struct hal_ev_hf_client_current_call *ev = (void *) buf;
unsigned int val;
DBG("");
memset(buf, 0, sizeof(buf));
if (!hfp_context_get_number(context, &val)) {
error("hf-client: Could not get index");
return;
}
ev->index = val;
if (!hfp_context_get_number(context, &val) ||
val > HAL_HF_CLIENT_DIRECTION_INCOMING) {
error("hf-client: Could not get direction");
return;
}
ev->direction = val;
if (!hfp_context_get_number(context, &val) ||
val > HAL_HF_CLIENT_CALL_STATE_HELD_BY_RESP_AND_HOLD) {
error("hf-client: Could not get callstate");
return;
}
ev->call_state = val;
/* Next field is MODE but Android is not interested in this. Skip it */
if (!hfp_context_get_number(context, &val)) {
error("hf-client: Could not get mode");
return;
}
if (!hfp_context_get_number(context, &val) || val > 1) {
error("hf-client: Could not get multiparty");
return;
}
ev->multiparty = val;
if (hfp_context_get_string(context, (char *) &ev->number[0],
MAX_NUMBER_LEN))
ev->number_len = strlen((char *) ev->number) + 1;
ipc_send_notif(hal_ipc, HAL_SERVICE_ID_HANDSFREE_CLIENT,
HAL_EV_HF_CLIENT_CURRENT_CALL,
sizeof(*ev) + ev->number_len, ev);
}
static void ciev_cb(struct hfp_context *context, void *user_data)
{
struct device *dev = user_data;
unsigned int index, val;
int i;
DBG("");
if (!hfp_context_get_number(context, &index))
return;