hc
2023-02-18 a08c8b75ee83d7f62c9aefc23bfb42082aa4076c
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
diff --git a/src/io.c b/src/io.c
index 55b46c7..05fdd06 100644
--- a/src/io.c
+++ b/src/io.c
@@ -23,6 +23,11 @@
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <sys/types.h>
+#include <sys/timerfd.h>
+#include <bluetooth/bluetooth.h>
+#include <bluetooth/hci.h>
+#include <bluetooth/hci_lib.h>
+#include <bluetooth/sco.h>
 
 #include <sbc/sbc.h>
 #if ENABLE_AAC
@@ -1509,8 +1514,62 @@ fail_open_ldac:
 }
 #endif
 
+static void close_lsocket(struct ba_transport *t)
+{
+    if (t->sco.listen_fd > 0) {
+        close(t->sco.listen_fd);
+        t->sco.listen_fd = -1;
+    }
+}
+
+static int bind_sco(struct ba_transport *t, int sock)
+{
+    struct sockaddr_sco addr;
+    struct hci_dev_info di;
+
+    if (hci_devinfo(t->device->hci_dev_id, &di) == -1) {
+        error("Couldn't get HCI device info: %s", strerror(errno));
+        return -1;
+    }
+
+    memset(&addr, 0, sizeof(addr));
+    addr.sco_family = AF_BLUETOOTH;
+    bacpy(&addr.sco_bdaddr, &di.bdaddr);
+
+    if (bind(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+        error("Couldn't bind sco socket");
+        return -1;
+    }
+
+    return 0;
+}
+
+static int timeout_set(int fd, unsigned int msec)
+{
+    struct itimerspec itimer;
+    unsigned int sec = msec / 1000;
+
+    memset(&itimer, 0, sizeof(itimer));
+    itimer.it_interval.tv_sec = 0;
+    itimer.it_interval.tv_nsec = 0;
+    itimer.it_value.tv_sec = sec;
+    itimer.it_value.tv_nsec = (msec - (sec * 1000)) * 1000 * 1000;
+
+    return timerfd_settime(fd, 0, &itimer, NULL);
+}
+
+static void close_timerfd(void *data)
+{
+    int fd = (int)data;
+
+    close(fd);
+}
+
 void *io_thread_sco(void *arg) {
     struct ba_transport *t = (struct ba_transport *)arg;
+    int defer = 1;
+    int sco_timer;
+    int timer_added = 0;
 
     pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
     pthread_cleanup_push(PTHREAD_CLEANUP(transport_pthread_cleanup), t);
@@ -1528,6 +1587,33 @@ void *io_thread_sco(void *arg) {
         goto fail_ffb;
     }
 
+    t->sco.listen_fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_SCO);
+    if (t->sco.listen_fd == -1) {
+        error("Couldn't open sco socket: %s", strerror(errno));
+        goto fail_ffb;
+    }
+    pthread_cleanup_push(PTHREAD_CLEANUP(close_lsocket), t);
+    if (bind_sco(t, t->sco.listen_fd) < 0) {
+        error("Couldn't bind sco socket");
+        goto fail_sock;
+    }
+    if (setsockopt(t->sco.listen_fd, SOL_BLUETOOTH, BT_DEFER_SETUP,
+               &defer, sizeof(defer)) < 0) {
+        error("Couldn't set defer for sco");
+        /* Ignore this error */
+    }
+    if (listen(t->sco.listen_fd, 1) < 0) {
+        error("Couldn't listen %s", strerror(errno));
+        goto fail_sock;
+    }
+
+    sco_timer = timerfd_create(CLOCK_REALTIME, 0);
+    if (sco_timer == -1) {
+        error("Couldn't create sco timer %s", strerror(errno));
+        goto fail_sock;
+    }
+    pthread_cleanup_push(PTHREAD_CLEANUP(close_timerfd), (void *)sco_timer);
+
     int poll_timeout = -1;
     struct asrsync asrs = { .frames = 0 };
     struct pollfd pfds[] = {
@@ -1538,6 +1624,8 @@ void *io_thread_sco(void *arg) {
         /* PCM FIFO */
         { -1, POLLIN, 0 },
         { -1, POLLOUT, 0 },
+        { t->sco.listen_fd, POLLIN, 0 },
+        { sco_timer, POLLIN, 0 }, /* [6] for sco timer */
     };
 
     debug("Starting IO loop: %s",
@@ -1595,28 +1683,27 @@ void *io_thread_sco(void *arg) {
             if (sig == TRANSPORT_PCM_SYNC)
                 pthread_cond_signal(&t->sco.spk_pcm.drained);
 
+            /* Received TRANSPORT_PCM_OPEN from client or rfcomm
+             * thread (callsetup=0/1, call=1).
+             * So at least three open events will be received when
+             * answer a call
+             */
             const enum hfp_ind *inds = t->sco.rfcomm->rfcomm.hfp_inds;
-            bool release = false;
-
-            /* It is required to release SCO if we are not transferring audio,
-             * because it will free Bluetooth bandwidth - microphone signal is
-             * transfered even though we are not reading from it! */
-            if (t->sco.spk_pcm.fd == -1 && t->sco.mic_pcm.fd == -1)
-                release = true;
-            /* For HFP HF we have to check if we are in the call stage or in the
-             * call setup stage. Otherwise, it might be not possible to acquire
-             * SCO connection. */
-            if (t->profile == BLUETOOTH_PROFILE_HFP_HF &&
-                    inds[HFP_IND_CALL] == HFP_IND_CALL_NONE &&
-                    inds[HFP_IND_CALLSETUP] == HFP_IND_CALLSETUP_NONE)
-                release = true;
+            if (sig == TRANSPORT_PCM_OPEN) {
+                info("Received transport pcm open event");
+                /* Start a timer to wait for sco connection from
+                 * AG. If timer is expired, connect to AG
+                 */
+                if (t->profile == BLUETOOTH_PROFILE_HFP_HF &&
+                    inds[HFP_IND_CALL] == HFP_IND_CALL_ACTIVE)
+                    timeout_set(sco_timer, 3000);
+            }
 
-            if (release) {
+            if (sig == TRANSPORT_PCM_CLOSE) {
+                info("Received transport pcm close event");
                 transport_release_bt_sco(t);
                 asrs.frames = 0;
             }
-            else
-                transport_acquire_bt_sco(t);
 
             continue;
         }
@@ -1624,6 +1711,7 @@ void *io_thread_sco(void *arg) {
         if (asrs.frames == 0)
             asrsync_init(&asrs, transport_get_sampling(t));
 
+        /* t->bt_fd */
         if (pfds[1].revents & POLLIN) {
             /* dispatch incoming SCO data */
 
@@ -1666,6 +1754,7 @@ retry_sco_read:
             transport_release_bt_sco(t);
         }
 
+        /* t->bt_fd */
         if (pfds[2].revents & POLLOUT) {
             /* write-out SCO data */
 
@@ -1704,6 +1793,7 @@ retry_sco_write:
 
         }
 
+        /* t->sco.spk_pcm.fd */
         if (pfds[3].revents & POLLIN) {
             /* dispatch incoming PCM data */
 
@@ -1740,6 +1830,7 @@ retry_sco_write:
             t->sco.spk_pcm.fd = -1;
         }
 
+        /* t->sco.mic_pcm.fd */
         if (pfds[4].revents & POLLOUT) {
             /* write-out PCM data */
 
@@ -1767,6 +1858,70 @@ retry_sco_write:
 
         }
 
+        /* sco_timer */
+        if (pfds[6].revents & POLLIN) {
+            bool release = false;
+            const enum hfp_ind *inds = t->sco.rfcomm->rfcomm.hfp_inds;
+
+            info("SCO timer expired");
+
+            if (t->bt_fd != -1) {
+                warn("Timer expired but sco link was created");
+                continue;
+            }
+
+            /* TODO: Should  we need to check client */
+            /* if (t->sco.spk_pcm.fd == -1 && t->sco.mic_pcm.fd == -1)
+             *     release = true;
+             */
+
+            if (t->profile == BLUETOOTH_PROFILE_HFP_HF &&
+                inds[HFP_IND_CALL] == HFP_IND_CALL_NONE &&
+                inds[HFP_IND_CALLSETUP] == HFP_IND_CALLSETUP_NONE)
+                release = true;
+            if (release) {
+                transport_release_bt_sco(t);
+                asrs.frames = 0;
+            } else {
+                transport_acquire_bt_sco(t);
+            }
+            continue;
+        }
+
+        /* t->sco.listen_fd */
+        if (pfds[5].revents & POLLIN) {
+            int sock;
+            struct sockaddr_sco addr;
+            socklen_t len = sizeof(addr);
+            const enum hfp_ind *inds = t->sco.rfcomm->rfcomm.hfp_inds;
+            bool release = false;
+
+            info("SCO connection created by peer");
+            sock = accept(pfds[5].fd, (struct sockaddr *)&addr,
+                      &len);
+            if (sock < 0) {
+                error("Couldn't accept sco connection");
+                continue;
+            }
+            /* For HFP HF we have to check if we are in the call
+             * stage or in the call setup stage. Otherwise, it might
+             * be not possible to acquire SCO connection. */
+            if (t->profile == BLUETOOTH_PROFILE_HFP_HF &&
+                inds[HFP_IND_CALL] == HFP_IND_CALL_NONE &&
+                inds[HFP_IND_CALLSETUP] == HFP_IND_CALLSETUP_NONE)
+                release = true;
+
+            if (release) {
+                transport_release_bt_sco(t);
+                asrs.frames = 0;
+            } else {
+                transport_acquire_bt_sco2(t, sock);
+                /* Kill timer that is used to create sco link */
+                timeout_set(sco_timer, 0);
+            }
+            continue;
+        }
+
         /* keep data transfer at a constant bit rate */
         asrsync_sync(&asrs, 48 / 2);
         /* update busy delay (encoding overhead) */
@@ -1776,6 +1931,9 @@ retry_sco_write:
 
 fail:
     pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
+    pthread_cleanup_pop(1); /* for sco timer */
+fail_sock:
+    pthread_cleanup_pop(1); /* for sco listen sock */
 fail_ffb:
     pthread_cleanup_pop(1);
     pthread_cleanup_pop(1);
diff --git a/src/rfcomm.c b/src/rfcomm.c
index 4dede53..1e32b46 100644
--- a/src/rfcomm.c
+++ b/src/rfcomm.c
@@ -237,8 +237,14 @@ static int rfcomm_handler_ciev_resp_cb(struct rfcomm_conn *c, const struct bt_at
         t->rfcomm.hfp_inds[c->hfp_ind_map[index - 1]] = value;
         switch (c->hfp_ind_map[index - 1]) {
         case HFP_IND_CALL:
+            if (value == 1)
+                transport_send_signal(t->rfcomm.sco,
+                              TRANSPORT_PCM_OPEN);
+            else if (value == 0)
+                transport_send_signal(t->rfcomm.sco,
+                              TRANSPORT_PCM_CLOSE);
+            break;
         case HFP_IND_CALLSETUP:
-            transport_send_signal(t->rfcomm.sco, TRANSPORT_PCM_OPEN);
             break;
         case HFP_IND_BATTCHG:
             device_set_battery_level(t->device, value * 100 / 5);
@@ -369,8 +375,26 @@ static int rfcomm_handler_bcs_set_cb(struct rfcomm_conn *c, const struct bt_at *
 }
 
 static int rfcomm_handler_resp_bcs_ok_cb(struct rfcomm_conn *c, const struct bt_at *at) {
+    struct ba_transport * const t = c->t;
+    struct ba_transport *t_sco;
+    struct bt_voice voice = {
+        .setting = BT_VOICE_CVSD_16BIT,
+    };
+
     if (rfcomm_handler_resp_ok_cb(c, at) != 0)
         return -1;
+
+    /* Change voice setting according to codec */
+    if (t->rfcomm.sco->codec == HFP_CODEC_MSBC)
+        voice.setting = BT_VOICE_TRANSPARENT;
+    t_sco = t->rfcomm.sco;
+    if (setsockopt(t_sco->sco.listen_fd, SOL_BLUETOOTH, BT_VOICE,
+               &voice, sizeof(voice)) == -1) {
+        error("setsockopt BT_VOICE error %d, %s", errno,
+              strerror(errno));
+        return 0;
+    }
+
     /* When codec selection is completed, notify connected clients, that
      * transport has been changed. Note, that this event might be emitted
      * for an active transport - codec switching. */
@@ -613,6 +637,7 @@ void *rfcomm_thread(void *arg) {
                     break;
                 case HFP_SLC_BRSF_SET_OK:
                     if (t->rfcomm.hfp_features & HFP_AG_FEAT_CODEC) {
+                        /* XXX: If mSBC is supported, please change 1 to 1,2 */
                         if (rfcomm_write_at(pfds[1].fd, AT_TYPE_CMD_SET, "+BAC", "1") == -1)
                             goto ioerror;
                         conn.handler = &rfcomm_handler_resp_ok;
diff --git a/src/transport.c b/src/transport.c
index 5afab73..1ead791 100644
--- a/src/transport.c
+++ b/src/transport.c
@@ -1006,6 +1006,35 @@ int transport_acquire_bt_sco(struct ba_transport *t) {
     return t->bt_fd;
 }
 
+int transport_acquire_bt_sco2(struct ba_transport *t, int asock)
+{
+
+    struct hci_dev_info di;
+
+    if (t->bt_fd != -1)
+        return t->bt_fd;
+
+    t->bt_fd = asock;
+
+    if (hci_devinfo(t->device->hci_dev_id, &di) == -1) {
+        error("Couldn't get HCI device info: %s", strerror(errno));
+        return -1;
+    }
+
+    t->mtu_read = di.sco_mtu;
+    t->mtu_write = di.sco_mtu;
+    t->release = transport_release_bt_sco;
+
+    /* XXX: It seems, that the MTU values returned by the HCI interface
+     *      are incorrect (or our interpretation of them is incorrect). */
+    t->mtu_read = 48;
+    t->mtu_write = 48;
+
+    debug("New SCO link: %d (MTU: R:%zu W:%zu)", t->bt_fd, t->mtu_read, t->mtu_write);
+
+    return t->bt_fd;
+}
+
 int transport_release_bt_sco(struct ba_transport *t) {
 
     if (t->bt_fd == -1)
diff --git a/src/transport.h b/src/transport.h
index 83e84c9..c2b8ab9 100644
--- a/src/transport.h
+++ b/src/transport.h
@@ -199,6 +199,8 @@ struct ba_transport {
             struct ba_pcm spk_pcm;
             struct ba_pcm mic_pcm;
 
+            int listen_fd;
+
         } sco;
 
     };
@@ -266,6 +268,7 @@ int transport_release_bt_a2dp(struct ba_transport *t);
 int transport_release_bt_rfcomm(struct ba_transport *t);
 
 int transport_acquire_bt_sco(struct ba_transport *t);
+int transport_acquire_bt_sco2(struct ba_transport *t, int asock);
 int transport_release_bt_sco(struct ba_transport *t);
 
 int transport_release_pcm(struct ba_pcm *pcm);
-- 
2.17.1