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
|
/*
* Copyright (C) 2014 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <cutils/properties.h>
#include "hal-log.h"
#include "hal.h"
#include "hal-msg.h"
#include "ipc-common.h"
#include "hal-ipc.h"
#include "hal-utils.h"
static const bthf_callbacks_t *cbs = NULL;
static bool interface_ready(void)
{
return cbs != NULL;
}
static void handle_conn_state(void *buf, uint16_t len, int fd)
{
struct hal_ev_handsfree_conn_state *ev = buf;
if (cbs->connection_state_cb)
cbs->connection_state_cb(ev->state,
(bt_bdaddr_t *) (ev->bdaddr));
}
static void handle_audio_state(void *buf, uint16_t len, int fd)
{
struct hal_ev_handsfree_audio_state *ev = buf;
if (cbs->audio_state_cb)
cbs->audio_state_cb(ev->state, (bt_bdaddr_t *) (ev->bdaddr));
}
static void handle_vr_state(void *buf, uint16_t len, int fd)
{
struct hal_ev_handsfree_vr_state *ev = buf;
if (cbs->vr_cmd_cb)
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
cbs->vr_cmd_cb(ev->state, (bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->vr_cmd_cb(ev->state);
#endif
}
static void handle_answer(void *buf, uint16_t len, int fd)
{
if (cbs->answer_call_cmd_cb) {
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
struct hal_ev_handsfree_answer *ev = buf;
cbs->answer_call_cmd_cb((bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->answer_call_cmd_cb();
#endif
}
}
static void handle_hangup(void *buf, uint16_t len, int fd)
{
if (cbs->hangup_call_cmd_cb) {
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
struct hal_ev_handsfree_hangup *ev = buf;
cbs->hangup_call_cmd_cb((bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->hangup_call_cmd_cb();
#endif
}
}
static void handle_volume(void *buf, uint16_t len, int fd)
{
struct hal_ev_handsfree_volume *ev = buf;
if (cbs->volume_cmd_cb)
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
cbs->volume_cmd_cb(ev->type, ev->volume,
(bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->volume_cmd_cb(ev->type, ev->volume);
#endif
}
static void handle_dial(void *buf, uint16_t len, int fd)
{
struct hal_ev_handsfree_dial *ev = buf;
uint16_t num_len = ev->number_len;
char *number = NULL;
if (len != sizeof(*ev) + num_len ||
(num_len != 0 && ev->number[num_len - 1] != '\0')) {
error("invalid dial event, aborting");
exit(EXIT_FAILURE);
}
if (!cbs->dial_call_cmd_cb)
return;
if (ev->number_len)
number = (char *) ev->number;
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
cbs->dial_call_cmd_cb(number, (bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->dial_call_cmd_cb(number);
#endif
}
static void handle_dtmf(void *buf, uint16_t len, int fd)
{
struct hal_ev_handsfree_dtmf *ev = buf;
if (cbs->dtmf_cmd_cb)
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
cbs->dtmf_cmd_cb(ev->tone, (bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->dtmf_cmd_cb(ev->tone);
#endif
}
static void handle_nrec(void *buf, uint16_t len, int fd)
{
struct hal_ev_handsfree_nrec *ev = buf;
if (cbs->nrec_cmd_cb)
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
cbs->nrec_cmd_cb(ev->nrec, (bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->nrec_cmd_cb(ev->nrec);
#endif
}
static void handle_chld(void *buf, uint16_t len, int fd)
{
struct hal_ev_handsfree_chld *ev = buf;
if (cbs->chld_cmd_cb)
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
cbs->chld_cmd_cb(ev->chld, (bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->chld_cmd_cb(ev->chld);
#endif
}
static void handle_cnum(void *buf, uint16_t len, int fd)
{
if (cbs->cnum_cmd_cb) {
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
struct hal_ev_handsfree_cnum *ev = buf;
cbs->cnum_cmd_cb((bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->cnum_cmd_cb(NULL);
#endif
}
}
static void handle_cind(void *buf, uint16_t len, int fd)
{
if (cbs->cind_cmd_cb) {
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
struct hal_ev_handsfree_cind *ev = buf;
cbs->cind_cmd_cb((bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->cind_cmd_cb();
#endif
}
}
static void handle_cops(void *buf, uint16_t len, int fd)
{
if (cbs->cops_cmd_cb) {
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
struct hal_ev_handsfree_cops *ev = buf;
cbs->cops_cmd_cb((bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->cops_cmd_cb();
#endif
}
}
static void handle_clcc(void *buf, uint16_t len, int fd)
{
if (cbs->clcc_cmd_cb) {
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
struct hal_ev_handsfree_clcc *ev = buf;
cbs->clcc_cmd_cb((bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->clcc_cmd_cb();
#endif
}
}
static void handle_unknown_at(void *buf, uint16_t len, int fd)
{
struct hal_ev_handsfree_unknown_at *ev = buf;
if (len != sizeof(*ev) + ev->len ||
(ev->len != 0 && ev->buf[ev->len - 1] != '\0')) {
error("invalid unknown command event, aborting");
exit(EXIT_FAILURE);
}
if (cbs->unknown_at_cmd_cb)
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
cbs->unknown_at_cmd_cb((char *) ev->buf,
(bt_bdaddr_t *) (ev->bdaddr));
#else
cbs->unknown_at_cmd_cb((char *) ev->buf);
#endif
}
static void handle_hsp_key_press(void *buf, uint16_t len, int fd)
{
if (cbs->key_pressed_cmd_cb)
cbs->key_pressed_cmd_cb();
}
/*
* handlers will be called from notification thread context,
* index in table equals to 'opcode - HAL_MINIMUM_EVENT'
*/
static const struct hal_ipc_handler ev_handlers[] = {
/* HAL_EV_HANDSFREE_CONN_STATE */
{ handle_conn_state, false,
sizeof(struct hal_ev_handsfree_conn_state) },
/* HAL_EV_HANDSFREE_AUDIO_STATE */
{ handle_audio_state, false,
sizeof(struct hal_ev_handsfree_audio_state) },
/* HAL_EV_HANDSFREE_VR */
{ handle_vr_state, false, sizeof(struct hal_ev_handsfree_vr_state) },
/*HAL_EV_HANDSFREE_ANSWER */
{ handle_answer, false, 0 },
/*HAL_EV_HANDSFREE_HANGUP */
{ handle_hangup, false, 0 },
/* HAL_EV_HANDSFREE_VOLUME */
{ handle_volume, false, sizeof(struct hal_ev_handsfree_volume) },
/* HAL_EV_HANDSFREE_DIAL */
{ handle_dial, true, sizeof(struct hal_ev_handsfree_dial) },
/* HAL_EV_HANDSFREE_DTMF */
{ handle_dtmf, false, sizeof(struct hal_ev_handsfree_dtmf) },
/* HAL_EV_HANDSFREE_NREC */
{ handle_nrec, false, sizeof(struct hal_ev_handsfree_nrec) },
/* HAL_EV_HANDSFREE_CHLD */
{ handle_chld, false, sizeof(struct hal_ev_handsfree_chld) },
/* HAL_EV_HANDSFREE_CNUM */
{ handle_cnum, false, 0 },
/* HAL_EV_HANDSFREE_CIND */
{ handle_cind, false, 0 },
/* HAL_EV_HANDSFREE_COPS */
{ handle_cops, false, 0 },
/* HAL_EV_HANDSFREE_CLCC */
{ handle_clcc, false, 0 },
/* HAL_EV_HANDSFREE_UNKNOWN_AT */
{ handle_unknown_at, true, sizeof(struct hal_ev_handsfree_unknown_at) },
/* HAL_EV_HANDSFREE_HSP_KEY_PRESS */
{ handle_hsp_key_press, false, 0 },
};
static uint8_t get_mode(void)
{
char value[PROPERTY_VALUE_MAX];
if (get_config("handsfree", value, NULL) > 0) {
if (!strcasecmp(value, "hfp"))
return HAL_MODE_HANDSFREE_HFP;
if (!strcasecmp(value, "hfp_wbs"))
return HAL_MODE_HANDSFREE_HFP_WBS;
}
return HAL_MODE_HANDSFREE_HSP_ONLY;
}
static bt_status_t init_real(bthf_callbacks_t *callbacks, int max_hf_clients)
{
struct hal_cmd_register_module cmd;
int ret;
DBG("");
if (interface_ready())
return BT_STATUS_DONE;
cbs = callbacks;
hal_ipc_register(HAL_SERVICE_ID_HANDSFREE, ev_handlers,
sizeof(ev_handlers)/sizeof(ev_handlers[0]));
cmd.service_id = HAL_SERVICE_ID_HANDSFREE;
cmd.mode = get_mode();
cmd.max_clients = max_hf_clients;
ret = hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_REGISTER_MODULE,
sizeof(cmd), &cmd, NULL, NULL, NULL);
if (ret != BT_STATUS_SUCCESS) {
cbs = NULL;
hal_ipc_unregister(HAL_SERVICE_ID_HANDSFREE);
}
return ret;
}
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
static bt_status_t init(bthf_callbacks_t *callbacks, int max_hf_clients)
{
return init_real(callbacks, max_hf_clients);
}
#else
static bt_status_t init(bthf_callbacks_t *callbacks)
{
return init_real(callbacks, 1);
}
#endif
static bt_status_t handsfree_connect(bt_bdaddr_t *bd_addr)
{
struct hal_cmd_handsfree_connect cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
if (!bd_addr)
return BT_STATUS_PARM_INVALID;
memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE, HAL_OP_HANDSFREE_CONNECT,
sizeof(cmd), &cmd, NULL, NULL, NULL);
}
static bt_status_t disconnect(bt_bdaddr_t *bd_addr)
{
struct hal_cmd_handsfree_disconnect cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
if (!bd_addr)
return BT_STATUS_PARM_INVALID;
memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
HAL_OP_HANDSFREE_DISCONNECT, sizeof(cmd), &cmd,
NULL, NULL, NULL);
}
static bt_status_t connect_audio(bt_bdaddr_t *bd_addr)
{
struct hal_cmd_handsfree_connect_audio cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
if (!bd_addr)
return BT_STATUS_PARM_INVALID;
memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
HAL_OP_HANDSFREE_CONNECT_AUDIO, sizeof(cmd),
&cmd, NULL, NULL, NULL);
}
static bt_status_t disconnect_audio(bt_bdaddr_t *bd_addr)
{
struct hal_cmd_handsfree_disconnect_audio cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
if (!bd_addr)
return BT_STATUS_PARM_INVALID;
memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
HAL_OP_HANDSFREE_DISCONNECT_AUDIO, sizeof(cmd),
&cmd, NULL, NULL, NULL);
}
static bt_status_t start_voice_recognition_real(bt_bdaddr_t *bd_addr)
{
struct hal_cmd_handsfree_start_vr cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
memset(&cmd, 0, sizeof(cmd));
if (bd_addr)
memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE, HAL_OP_HANDSFREE_START_VR,
sizeof(cmd), &cmd, NULL, NULL, NULL);
}
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
static bt_status_t start_voice_recognition(bt_bdaddr_t *bd_addr)
{
return start_voice_recognition_real(bd_addr);
}
#else
static bt_status_t start_voice_recognition(void)
{
return start_voice_recognition_real(NULL);
}
#endif
static bt_status_t stop_voice_recognition_real(bt_bdaddr_t *bd_addr)
{
struct hal_cmd_handsfree_stop_vr cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
memset(&cmd, 0, sizeof(cmd));
if (bd_addr)
memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE, HAL_OP_HANDSFREE_STOP_VR,
sizeof(cmd), &cmd, NULL, NULL, NULL);
}
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
static bt_status_t stop_voice_recognition(bt_bdaddr_t *bd_addr)
{
return stop_voice_recognition_real(bd_addr);
}
#else
static bt_status_t stop_voice_recognition(void)
{
return stop_voice_recognition_real(NULL);
}
#endif
static bt_status_t volume_control_real(bthf_volume_type_t type, int volume,
bt_bdaddr_t *bd_addr)
{
struct hal_cmd_handsfree_volume_control cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
memset(&cmd, 0, sizeof(cmd));
cmd.type = type;
cmd.volume = volume;
if (bd_addr)
memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
HAL_OP_HANDSFREE_VOLUME_CONTROL, sizeof(cmd),
&cmd, NULL, NULL, NULL);
}
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
static bt_status_t volume_control(bthf_volume_type_t type, int volume,
bt_bdaddr_t *bd_addr)
{
return volume_control_real(type, volume, bd_addr);
}
#else
static bt_status_t volume_control(bthf_volume_type_t type, int volume)
{
return volume_control_real(type, volume, NULL);
}
#endif
static bt_status_t device_status_notification(bthf_network_state_t state,
bthf_service_type_t type,
int signal, int battery)
{
struct hal_cmd_handsfree_device_status_notif cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
cmd.state = state;
cmd.type = type;
cmd.signal = signal;
cmd.battery = battery;
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
HAL_OP_HANDSFREE_DEVICE_STATUS_NOTIF,
sizeof(cmd), &cmd, NULL, NULL, NULL);
}
static bt_status_t cops_response_real(const char *cops, bt_bdaddr_t *bd_addr)
{
char buf[IPC_MTU];
struct hal_cmd_handsfree_cops_response *cmd = (void *) buf;
size_t len;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
if (!cops)
return BT_STATUS_PARM_INVALID;
memset(cmd, 0, sizeof(*cmd));
if (bd_addr)
memcpy(cmd->bdaddr, bd_addr, sizeof(cmd->bdaddr));
/* Size of cmd.buf */
cmd->len = strlen(cops) + 1;
memcpy(cmd->buf, cops, cmd->len);
len = sizeof(*cmd) + cmd->len;
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
HAL_OP_HANDSFREE_COPS_RESPONSE,
len, cmd, NULL, NULL, NULL);
}
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
static bt_status_t cops_response(const char *cops, bt_bdaddr_t *bd_addr)
{
return cops_response_real(cops, bd_addr);
}
#else
static bt_status_t cops_response(const char *cops)
{
return cops_response_real(cops, NULL);
}
#endif
static bt_status_t cind_response_real(int svc, int num_active, int num_held,
bthf_call_state_t state, int signal,
int roam, int batt_chg,
bt_bdaddr_t *bd_addr)
{
struct hal_cmd_handsfree_cind_response cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
memset(&cmd, 0, sizeof(cmd));
if (bd_addr)
memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
cmd.svc = svc;
cmd.num_active = num_active;
cmd.num_held = num_held;
cmd.state = state;
cmd.signal = signal;
cmd.roam = roam;
cmd.batt_chg = batt_chg;
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
HAL_OP_HANDSFREE_CIND_RESPONSE,
sizeof(cmd), &cmd, NULL, NULL, NULL);
}
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
static bt_status_t cind_response(int svc, int num_active, int num_held,
bthf_call_state_t state, int signal,
int roam, int batt_chg,
bt_bdaddr_t *bd_addr)
{
return cind_response_real(svc, num_active, num_held, state, signal,
roam, batt_chg, bd_addr);
}
#else
static bt_status_t cind_response(int svc, int num_active, int num_held,
bthf_call_state_t state, int signal,
int roam, int batt_chg)
{
return cind_response_real(svc, num_active, num_held, state, signal,
roam, batt_chg, NULL);
}
#endif
static bt_status_t formatted_at_response_real(const char *rsp,
bt_bdaddr_t *bd_addr)
{
char buf[IPC_MTU];
struct hal_cmd_handsfree_formatted_at_response *cmd = (void *) buf;
size_t len;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
if (!rsp)
return BT_STATUS_PARM_INVALID;
memset(cmd, 0, sizeof(*cmd));
if (bd_addr)
memcpy(cmd->bdaddr, bd_addr, sizeof(cmd->bdaddr));
cmd->len = strlen(rsp) + 1;
memcpy(cmd->buf, rsp, cmd->len);
len = sizeof(*cmd) + cmd->len;
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
HAL_OP_HANDSFREE_FORMATTED_AT_RESPONSE,
len, cmd, NULL, NULL, NULL);
}
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
static bt_status_t formatted_at_response(const char *rsp, bt_bdaddr_t *bd_addr)
{
return formatted_at_response_real(rsp, bd_addr);
}
#else
static bt_status_t formatted_at_response(const char *rsp)
{
return formatted_at_response_real(rsp, NULL);
}
#endif
static bt_status_t at_response_real(bthf_at_response_t response, int error,
bt_bdaddr_t *bd_addr)
{
struct hal_cmd_handsfree_at_response cmd;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
if (bd_addr)
memcpy(cmd.bdaddr, bd_addr, sizeof(cmd.bdaddr));
memset(&cmd, 0, sizeof(cmd));
cmd.response = response;
cmd.error = error;
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
HAL_OP_HANDSFREE_AT_RESPONSE,
sizeof(cmd), &cmd, NULL, NULL, NULL);
}
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
static bt_status_t at_response(bthf_at_response_t response, int error,
bt_bdaddr_t *bd_addr)
{
return at_response_real(response, error, bd_addr);
}
#else
static bt_status_t at_response(bthf_at_response_t response, int error)
{
return at_response_real(response, error, NULL);
}
#endif
static bt_status_t clcc_response_real(int index, bthf_call_direction_t dir,
bthf_call_state_t state,
bthf_call_mode_t mode,
bthf_call_mpty_type_t mpty,
const char *number,
bthf_call_addrtype_t type,
bt_bdaddr_t *bd_addr)
{
char buf[IPC_MTU];
struct hal_cmd_handsfree_clcc_response *cmd = (void *) buf;
size_t len;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
memset(cmd, 0, sizeof(*cmd));
if (bd_addr)
memcpy(cmd->bdaddr, bd_addr, sizeof(cmd->bdaddr));
cmd->index = index;
cmd->dir = dir;
cmd->state = state;
cmd->mode = mode;
cmd->mpty = mpty;
cmd->type = type;
if (number) {
cmd->number_len = strlen(number) + 1;
memcpy(cmd->number, number, cmd->number_len);
} else {
cmd->number_len = 0;
}
len = sizeof(*cmd) + cmd->number_len;
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
HAL_OP_HANDSFREE_CLCC_RESPONSE,
len, cmd, NULL, NULL, NULL);
}
#if ANDROID_VERSION >= PLATFORM_VER(5, 0, 0)
static bt_status_t clcc_response(int index, bthf_call_direction_t dir,
bthf_call_state_t state,
bthf_call_mode_t mode,
bthf_call_mpty_type_t mpty,
const char *number,
bthf_call_addrtype_t type,
bt_bdaddr_t *bd_addr)
{
return clcc_response_real(index, dir, state, mode, mpty, number, type,
bd_addr);
}
#else
static bt_status_t clcc_response(int index, bthf_call_direction_t dir,
bthf_call_state_t state,
bthf_call_mode_t mode,
bthf_call_mpty_type_t mpty,
const char *number,
bthf_call_addrtype_t type)
{
return clcc_response_real(index, dir, state, mode, mpty, number, type,
NULL);
}
#endif
static bt_status_t phone_state_change(int num_active, int num_held,
bthf_call_state_t state,
const char *number,
bthf_call_addrtype_t type)
{
char buf[IPC_MTU];
struct hal_cmd_handsfree_phone_state_change *cmd = (void *) buf;
size_t len;
DBG("");
if (!interface_ready())
return BT_STATUS_NOT_READY;
cmd->num_active = num_active;
cmd->num_held = num_held;
cmd->state = state;
cmd->type = type;
if (number) {
cmd->number_len = strlen(number) + 1;
memcpy(cmd->number, number, cmd->number_len);
} else {
cmd->number_len = 0;
}
len = sizeof(*cmd) + cmd->number_len;
return hal_ipc_cmd(HAL_SERVICE_ID_HANDSFREE,
HAL_OP_HANDSFREE_PHONE_STATE_CHANGE,
len, cmd, NULL, NULL, NULL);
}
static void cleanup(void)
{
struct hal_cmd_unregister_module cmd;
DBG("");
if (!interface_ready())
return;
cbs = NULL;
cmd.service_id = HAL_SERVICE_ID_HANDSFREE;
hal_ipc_cmd(HAL_SERVICE_ID_CORE, HAL_OP_UNREGISTER_MODULE,
sizeof(cmd), &cmd, NULL, NULL, NULL);
hal_ipc_unregister(HAL_SERVICE_ID_HANDSFREE);
}
static bthf_interface_t iface = {
.size = sizeof(iface),
.init = init,
.connect = handsfree_connect,
.disconnect = disconnect,
.connect_audio = connect_audio,
.disconnect_audio = disconnect_audio,
.start_voice_recognition = start_voice_recognition,
.stop_voice_recognition = stop_voice_recognition,
.volume_control = volume_control,
.device_status_notification = device_status_notification,
.cops_response = cops_response,
.cind_response = cind_response,
.formatted_at_response = formatted_at_response,
.at_response = at_response,
.clcc_response = clcc_response,
.phone_state_change = phone_state_change,
.cleanup = cleanup
};
bthf_interface_t *bt_get_handsfree_interface(void)
{
return &iface;
}
|