• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-2018 Realtek Corporation
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  ******************************************************************************/
18 
19 /******************************************************************************
20  *
21  *  Filename:      userial_vendor.c
22  *
23  *  Description:   Contains vendor-specific userial functions
24  *
25  ******************************************************************************/
26 #undef NDEBUG
27 #define LOG_TAG "bt_userial_vendor"
28 
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <sys/eventfd.h>
33 #include <poll.h>
34 #include <sys/socket.h>
35 #include <termios.h>
36 #include <utils/Log.h>
37 
38 #ifdef CONFIG_SCO_OVER_HCI
39 #include "sbc.h"
40 #endif
41 #include "rtk_socket.h"
42 #include "userial.h"
43 #include "rtk_btservice.h"
44 #include "rtk_heartbeat.h"
45 #include "userial_vendor.h"
46 /******************************************************************************
47 **  Constants & Macros
48 ******************************************************************************/
49 
50 #ifndef VNDUSERIAL_DBG
51 #define VNDUSERIAL_DBG TRUE
52 #endif
53 
54 #if (VNDUSERIAL_DBG == TRUE)
55 #define VNDUSERIALDBG(param, ...)                                                                                      \
56     {                                                                                                                  \
57         HILOGD(param, ##__VA_ARGS__);                                                                                  \
58     }
59 #else
60 #define VNDUSERIALDBG(param, ...)                                                                                      \
61     {                                                                                                                  \
62     }
63 #endif
64 
65 #define VND_PORT_NAME_MAXLEN 256
66 
67 #ifndef BT_CHIP_HW_FLOW_CTRL_ON
68 #define BT_CHIP_HW_FLOW_CTRL_ON TRUE
69 #endif
70 
71 /******************************************************************************
72 **  Extern functions
73 ******************************************************************************/
74 
75 /******************************************************************************
76 **  Local type definitions
77 ******************************************************************************/
78 #if !defined(EFD_SEMAPHORE)
79 #define EFD_SEMAPHORE (1 << 0)
80 #endif
81 
82 #define RTK_DATA_RECEIVED 1
83 #define RTK_DATA_SEND 0
84 struct rtk_object_t {
85     int fd;               // the file descriptor to monitor for events.
86     void *context;        // a context that's passed back to the *_ready functions..
87     pthread_mutex_t lock; // protects the lifetime of this object and all variables.
88 
89     void (*read_ready)(void *context);  // function to call when the file descriptor becomes readable.
90     void (*write_ready)(void *context); // function to call when the file descriptor becomes writeable.
91 };
92 
93 /* vendor serial control block */
94 typedef struct {
95     int fd; /* fd to Bluetooth device */
96     int uart_fd[2];
97     int signal_fd[2];
98     int epoll_fd;
99     int cpoll_fd;
100     int event_fd;
101     struct termios termios; /* serial terminal of BT port */
102     char port_name[VND_PORT_NAME_MAXLEN];
103     pthread_t thread_socket_id;
104     pthread_t thread_uart_id;
105     pthread_t thread_coex_id;
106     bool thread_running;
107 
108     RTB_QUEUE_HEAD *recv_data;
109     RTB_QUEUE_HEAD *send_data;
110     RTB_QUEUE_HEAD *data_order;
111     volatile bool btdriver_state;
112 } vnd_userial_cb_t;
113 
114 #ifdef CONFIG_SCO_OVER_HCI
115 uint16_t btui_msbc_h2[] = {0x0801, 0x3801, 0xc801, 0xf801};
116 typedef struct {
117     pthread_mutex_t sco_recv_mutex;
118     pthread_cond_t sco_recv_cond;
119     pthread_mutex_t sco_send_mutex;
120     pthread_t thread_socket_sco_id;
121     pthread_t thread_recv_sco_id;
122     pthread_t thread_send_sco_id;
123     uint16_t sco_handle;
124     bool thread_sco_running;
125     bool thread_recv_sco_running;
126     bool thread_send_sco_running;
127     uint16_t voice_settings;
128     RTB_QUEUE_HEAD *recv_sco_data;
129     RTB_QUEUE_HEAD *send_sco_data;
130     unsigned char enc_data[480];
131     unsigned int current_pos;
132     uint16_t sco_packet_len;
133     bool msbc_used;
134     int ctrl_fd, data_fd;
135     sbc_t sbc_dec, sbc_enc;
136     uint32_t pcm_enc_seq;
137     int signal_fd[2];
138 } sco_cb_t;
139 #endif
140 
141 /******************************************************************************
142 **  Static functions
143 ******************************************************************************/
144 static void h5_data_ready_cb(serial_data_type_t type, unsigned int total_length);
145 static uint16_t h5_int_transmit_data_cb(serial_data_type_t type, uint8_t *data, uint16_t length);
146 /******************************************************************************
147 **  Static variables
148 ******************************************************************************/
149 #ifdef CONFIG_SCO_OVER_HCI
150 static sco_cb_t sco_cb;
151 #endif
152 static vnd_userial_cb_t vnd_userial;
153 static const hci_h5_t *h5_int_interface;
154 static int packet_recv_state = RTKBT_PACKET_IDLE;
155 static unsigned int packet_bytes_need = 0;
156 static serial_data_type_t current_type = 0;
157 static struct rtk_object_t rtk_socket_object;
158 static struct rtk_object_t rtk_coex_object;
159 static unsigned char h4_read_buffer[2048] = {0};
160 static int h4_read_length = 0;
161 
162 static int coex_packet_recv_state = RTKBT_PACKET_IDLE;
163 static int coex_packet_bytes_need = 0;
164 static serial_data_type_t coex_current_type = 0;
165 static unsigned char coex_resvered_buffer[2048] = {0};
166 static int coex_resvered_length = 0;
167 
168 #ifdef RTK_HANDLE_EVENT
169 static int received_packet_state = RTKBT_PACKET_IDLE;
170 static unsigned int received_packet_bytes_need = 0;
171 static serial_data_type_t recv_packet_current_type = 0;
172 static unsigned char received_resvered_header[2048] = {0};
173 static int received_resvered_length = 0;
174 static rtkbt_version_t rtkbt_version;
175 static rtkbt_lescn_t rtkbt_adv_con;
176 #endif
177 
178 static rtk_parse_manager_t *rtk_parse_manager = NULL;
179 
180 static hci_h5_callbacks_t h5_int_callbacks = {
181     .h5_int_transmit_data_cb = h5_int_transmit_data_cb,
182     .h5_data_ready_cb = h5_data_ready_cb,
183 };
184 
185 static const uint8_t hci_preamble_sizes[] = {COMMAND_PREAMBLE_SIZE, ACL_PREAMBLE_SIZE, SCO_PREAMBLE_SIZE,
186                                              EVENT_PREAMBLE_SIZE};
187 
188 /*****************************************************************************
189 **   Helper Functions
190 *****************************************************************************/
191 
192 /*******************************************************************************
193 **
194 ** Function        userial_to_tcio_baud
195 **
196 ** Description     helper function converts USERIAL baud rates into TCIO
197 **                  conforming baud rates
198 **
199 ** Returns         TRUE/FALSE
200 **
201 *******************************************************************************/
userial_to_tcio_baud(uint8_t cfg_baud,uint32_t * baud)202 uint8_t userial_to_tcio_baud(uint8_t cfg_baud, uint32_t *baud)
203 {
204     if (cfg_baud == USERIAL_BAUD_115200) {
205         *baud = B115200;
206     } else if (cfg_baud == USERIAL_BAUD_4M) {
207         *baud = B4000000;
208     } else if (cfg_baud == USERIAL_BAUD_3M) {
209         *baud = B3000000;
210     } else if (cfg_baud == USERIAL_BAUD_2M) {
211         *baud = B2000000;
212     } else if (cfg_baud == USERIAL_BAUD_1M) {
213         *baud = B1000000;
214     } else if (cfg_baud == USERIAL_BAUD_1_5M) {
215         *baud = B1500000;
216     } else if (cfg_baud == USERIAL_BAUD_921600) {
217         *baud = B921600;
218     } else if (cfg_baud == USERIAL_BAUD_460800) {
219         *baud = B460800;
220     } else if (cfg_baud == USERIAL_BAUD_230400) {
221         *baud = B230400;
222     } else if (cfg_baud == USERIAL_BAUD_57600) {
223         *baud = B57600;
224     } else if (cfg_baud == USERIAL_BAUD_19200) {
225         *baud = B19200;
226     } else if (cfg_baud == USERIAL_BAUD_9600) {
227         *baud = B9600;
228     } else if (cfg_baud == USERIAL_BAUD_1200) {
229         *baud = B1200;
230     } else if (cfg_baud == USERIAL_BAUD_600) {
231         *baud = B600;
232     } else {
233         HILOGE("userial vendor open: unsupported baud idx %i", cfg_baud);
234         *baud = B115200;
235         return FALSE;
236     }
237 
238     return TRUE;
239 }
240 
241 #if (BT_WAKE_VIA_USERIAL_IOCTL == TRUE)
242 /*******************************************************************************
243 **
244 ** Function        userial_ioctl_init_bt_wake
245 **
246 ** Description     helper function to set the open state of the bt_wake if ioctl
247 **                  is used. it should not hurt in the rfkill case but it might
248 **                  be better to compile it out.
249 **
250 ** Returns         none
251 **
252 *******************************************************************************/
userial_ioctl_init_bt_wake(int fd)253 void userial_ioctl_init_bt_wake(int fd)
254 {
255     uint32_t bt_wake_state;
256 
257     /* assert BT_WAKE through ioctl */
258     ioctl(fd, USERIAL_IOCTL_BT_WAKE_ASSERT, NULL);
259     ioctl(fd, USERIAL_IOCTL_BT_WAKE_GET_ST, &bt_wake_state);
260     VNDUSERIALDBG("userial_ioctl_init_bt_wake read back BT_WAKE state=%i", bt_wake_state);
261 }
262 #endif // (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
263 
264 /*****************************************************************************
265 **   Userial Vendor API Functions
266 *****************************************************************************/
267 
268 /*******************************************************************************
269 **
270 ** Function        userial_vendor_init
271 **
272 ** Description     Initialize userial vendor-specific control block
273 **
274 ** Returns         None
275 **
276 *******************************************************************************/
userial_vendor_init(char * bt_device_node)277 void userial_vendor_init(char *bt_device_node)
278 {
279     (void)memset_s(&rtkbt_adv_con, sizeof(rtkbt_lescn_t), 0, sizeof(rtkbt_lescn_t));
280     (void)memset_s(&vnd_userial, sizeof(vnd_userial_cb_t), 0, sizeof(vnd_userial_cb_t));
281     vnd_userial.fd = -1;
282     char value[100];
283     char rtkbt_transtype_usrial_vendor_init = get_rtkbt_transtype();
284     (void)snprintf_s(vnd_userial.port_name, VND_PORT_NAME_MAXLEN, VND_PORT_NAME_MAXLEN, "%s", bt_device_node);
285     if (rtkbt_transtype_usrial_vendor_init & RTKBT_TRANS_H5) {
286         h5_int_interface = hci_get_h5_int_interface();
287         h5_int_interface->h5_int_init(&h5_int_callbacks);
288     }
289     rtk_parse_manager = NULL;
290     (void)memset_s(value, sizeof(value), 0, sizeof(value));
291     (void)strcpy_s(value, sizeof(value), "true");
292 #define STRNCMP_4 4
293     if (strncmp(value, "true", STRNCMP_4) == 0) {
294         rtk_parse_manager = rtk_parse_manager_get_interface();
295         rtk_parse_manager->rtk_parse_init();
296     }
297     vnd_userial.data_order = RtbQueueInit();
298     vnd_userial.recv_data = RtbQueueInit();
299     vnd_userial.send_data = RtbQueueInit();
300 
301     // reset coex gloable variables
302     coex_packet_recv_state = RTKBT_PACKET_IDLE;
303     coex_packet_bytes_need = 0;
304     coex_current_type = 0;
305     coex_resvered_length = 0;
306 
307 #ifdef RTK_HANDLE_EVENT
308     // reset handle event gloable variables
309     received_packet_state = RTKBT_PACKET_IDLE;
310     received_packet_bytes_need = 0;
311     recv_packet_current_type = 0;
312     received_resvered_length = 0;
313 #endif
314 
315 #ifdef CONFIG_SCO_OVER_HCI
316     sco_cb.recv_sco_data = RtbQueueInit();
317     sco_cb.send_sco_data = RtbQueueInit();
318     pthread_mutex_init(&sco_cb.sco_recv_mutex, NULL);
319     pthread_cond_init(&sco_cb.sco_recv_cond, NULL);
320     pthread_mutex_init(&sco_cb.sco_send_mutex, NULL);
321     (void)memset_s(&sco_cb.sbc_enc, sizeofo(sbc_t), 0, sizeof(sbc_t));
322     sbc_init_msbc(&sco_cb.sbc_enc, 0L);
323     sco_cb.sbc_enc.endian = SBC_LE;
324     (void)memset_s(&sco_cb.sbc_dec, sizeof(sbc_t), 0, sizeof(sbc_t));
325     sbc_init_msbc(&sco_cb.sbc_dec, 0L);
326     sco_cb.sbc_dec.endian = SBC_LE;
327 #endif
328 }
329 
330 /*******************************************************************************
331 **
332 ** Function        userial_vendor_open
333 **
334 ** Description     Open the serial port with the given configuration
335 **
336 ** Returns         device fd
337 **
338 *******************************************************************************/
userial_vendor_open(tUSERIAL_CFG * p_cfg)339 int userial_vendor_open(tUSERIAL_CFG *p_cfg)
340 {
341     uint32_t baud;
342     uint8_t data_bits;
343     uint16_t parity;
344     uint8_t stop_bits;
345 
346     vnd_userial.fd = -1;
347 
348     if (!userial_to_tcio_baud(p_cfg->baud, &baud)) {
349         return -1;
350     }
351 
352     if (p_cfg->fmt & USERIAL_DATABITS_8) {
353         data_bits = CS8;
354     } else if (p_cfg->fmt & USERIAL_DATABITS_7) {
355         data_bits = CS7;
356     } else if (p_cfg->fmt & USERIAL_DATABITS_6) {
357         data_bits = CS6;
358     } else if (p_cfg->fmt & USERIAL_DATABITS_5) {
359         data_bits = CS5;
360     } else {
361         HILOGE("userial vendor open: unsupported data bits");
362         return -1;
363     }
364 
365     if (p_cfg->fmt & USERIAL_PARITY_NONE) {
366         parity = 0;
367     } else if (p_cfg->fmt & USERIAL_PARITY_EVEN) {
368         parity = PARENB;
369     } else if (p_cfg->fmt & USERIAL_PARITY_ODD) {
370         parity = (PARENB | PARODD);
371     } else {
372         HILOGE("userial vendor open: unsupported parity bit mode");
373         return -1;
374     }
375 
376     if (p_cfg->fmt & USERIAL_STOPBITS_1) {
377         stop_bits = 0;
378     } else if (p_cfg->fmt & USERIAL_STOPBITS_2) {
379         stop_bits = CSTOPB;
380     } else {
381         HILOGE("userial vendor open: unsupported stop bits");
382         return -1;
383     }
384 
385     HILOGI("userial vendor open: opening %s", vnd_userial.port_name);
386 
387     if ((vnd_userial.fd = open(vnd_userial.port_name, O_RDWR)) == -1) {
388         HILOGE("userial vendor open: unable to open %s", vnd_userial.port_name);
389         return -1;
390     }
391 
392     tcflush(vnd_userial.fd, TCIOFLUSH);
393 
394     tcgetattr(vnd_userial.fd, &vnd_userial.termios);
395     cfmakeraw(&vnd_userial.termios);
396 
397     if (p_cfg->hw_fctrl == USERIAL_HW_FLOW_CTRL_ON) {
398         HILOGI("userial vendor open: with HW flowctrl ON");
399         vnd_userial.termios.c_cflag |= (CRTSCTS | stop_bits | parity);
400     } else {
401         HILOGI("userial vendor open: with HW flowctrl OFF");
402         vnd_userial.termios.c_cflag &= ~CRTSCTS;
403         vnd_userial.termios.c_cflag |= (stop_bits | parity);
404     }
405 
406     tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
407     tcflush(vnd_userial.fd, TCIOFLUSH);
408 
409     tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
410     tcflush(vnd_userial.fd, TCIOFLUSH);
411     tcflush(vnd_userial.fd, TCIOFLUSH);
412 
413     /* set input/output baudrate */
414     cfsetospeed(&vnd_userial.termios, baud);
415     cfsetispeed(&vnd_userial.termios, baud);
416     tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios);
417 
418 #if (BT_WAKE_VIA_USERIAL_IOCTL == TRUE)
419     userial_ioctl_init_bt_wake(vnd_userial.fd);
420 #endif
421 
422     vnd_userial.btdriver_state = true;
423     HILOGI("device fd = %d open", vnd_userial.fd);
424 
425     return vnd_userial.fd;
426 }
427 
userial_socket_close(void)428 static void userial_socket_close(void)
429 {
430     int result;
431 
432     if ((vnd_userial.uart_fd[0] > 0) && (result = close(vnd_userial.uart_fd[0])) < 0) {
433         HILOGE("%s (fd:%d) FAILED result:%d", __func__, vnd_userial.uart_fd[0], result);
434     }
435 
436     if (epoll_ctl(vnd_userial.epoll_fd, EPOLL_CTL_DEL, vnd_userial.uart_fd[1], NULL) == -1) {
437         HILOGE("%s unable to unregister fd %d from epoll set: %s", __func__, vnd_userial.uart_fd[1], strerror(errno));
438     }
439 
440     if (epoll_ctl(vnd_userial.epoll_fd, EPOLL_CTL_DEL, vnd_userial.signal_fd[1], NULL) == -1) {
441         HILOGE("%s unable to unregister signal fd %d from epoll set: %s", __func__, vnd_userial.signal_fd[1],
442                strerror(errno));
443     }
444 
445     if ((vnd_userial.uart_fd[1] > 0) && (result = close(vnd_userial.uart_fd[1])) < 0) {
446         HILOGE("%s (fd:%d) FAILED result:%d", __func__, vnd_userial.uart_fd[1], result);
447     }
448 
449     if (vnd_userial.thread_socket_id != (pthread_t)-1) {
450         pthread_join(vnd_userial.thread_socket_id, NULL);
451     }
452 
453     if (vnd_userial.epoll_fd > 0) {
454         close(vnd_userial.epoll_fd);
455     }
456 
457     if ((vnd_userial.signal_fd[0] > 0) && (result = close(vnd_userial.signal_fd[0])) < 0) {
458         HILOGE("%s (signal fd[0]:%d) FAILED result:%d", __func__, vnd_userial.signal_fd[0], result);
459     }
460     if ((vnd_userial.signal_fd[1] > 0) && (result = close(vnd_userial.signal_fd[1])) < 0) {
461         HILOGE("%s (signal fd[1]:%d) FAILED result:%d", __func__, vnd_userial.signal_fd[1], result);
462     }
463 
464     vnd_userial.epoll_fd = -1;
465     vnd_userial.uart_fd[0] = -1;
466     vnd_userial.uart_fd[1] = -1;
467     vnd_userial.signal_fd[0] = -1;
468     vnd_userial.signal_fd[1] = -1;
469 }
470 
userial_uart_close(void)471 static void userial_uart_close(void)
472 {
473     int result;
474     if ((vnd_userial.fd > 0) && (result = close(vnd_userial.fd)) < 0) {
475         HILOGE("%s (fd:%d) FAILED result:%d", __func__, vnd_userial.fd, result);
476     }
477     if (vnd_userial.thread_uart_id != (pthread_t)-1) {
478         pthread_join(vnd_userial.thread_uart_id, NULL);
479     }
480 }
481 
userial_coex_close(void)482 static void userial_coex_close(void)
483 {
484     int result;
485 
486     if (epoll_ctl(vnd_userial.cpoll_fd, EPOLL_CTL_DEL, vnd_userial.event_fd, NULL) == -1) {
487         HILOGE("%s unable to unregister fd %d from cpoll set: %s", __func__, vnd_userial.event_fd, strerror(errno));
488     }
489 
490     if (epoll_ctl(vnd_userial.cpoll_fd, EPOLL_CTL_DEL, vnd_userial.signal_fd[1], NULL) == -1) {
491         HILOGE("%s unable to unregister fd %d from cpoll set: %s", __func__, vnd_userial.signal_fd[1], strerror(errno));
492     }
493 
494     if ((result = close(vnd_userial.event_fd)) < 0) {
495         HILOGE("%s (fd:%d) FAILED result:%d", __func__, vnd_userial.event_fd, result);
496     }
497 
498     close(vnd_userial.cpoll_fd);
499     if (vnd_userial.thread_coex_id != (pthread_t)-1) {
500         pthread_join(vnd_userial.thread_coex_id, NULL);
501     }
502     vnd_userial.cpoll_fd = -1;
503     vnd_userial.event_fd = -1;
504 }
505 
userial_send_close_signal(void)506 void userial_send_close_signal(void)
507 {
508     unsigned char close_signal = 1;
509     ssize_t ret;
510     RTK_NO_INTR(ret = write(vnd_userial.signal_fd[0], &close_signal, 1));
511 }
512 
513 /*******************************************************************************
514 **
515 ** Function        userial_vendor_close
516 **
517 ** Description     Conduct vendor-specific close work
518 **
519 ** Returns         None
520 **
521 *******************************************************************************/
userial_vendor_close(void)522 void userial_vendor_close(void)
523 {
524     char rtkbt_transtype_close = get_rtkbt_transtype();
525 
526     if (vnd_userial.fd == -1) {
527         return;
528     }
529 
530     if ((rtkbt_transtype_close & RTKBT_TRANS_UART) && (rtkbt_transtype_close & RTKBT_TRANS_H5)) {
531 #if (BT_WAKE_VIA_USERIAL_IOCTL == TRUE)
532         /* de-assert bt_wake BEFORE closing port */
533         ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_DEASSERT, NULL);
534 #endif
535     }
536 
537     vnd_userial.thread_running = false;
538 #ifdef CONFIG_SCO_OVER_HCI
539     if (sco_cb.thread_sco_running) {
540         sco_cb.thread_sco_running = false;
541         unsigned char close_signal = 1;
542         ssize_t ret;
543         RTK_NO_INTR(ret = write(sco_cb.signal_fd[0], &close_signal, 1));
544         pthread_join(sco_cb.thread_socket_sco_id, NULL);
545     }
546 #endif
547     Heartbeat_cleanup();
548     RTK_btservice_destroyed();
549     userial_send_close_signal();
550     userial_uart_close();
551     userial_coex_close();
552     userial_socket_close();
553 
554     if ((rtkbt_transtype_close & RTKBT_TRANS_UART) && (rtkbt_transtype_close & RTKBT_TRANS_H5)) {
555         h5_int_interface->h5_int_cleanup();
556     }
557 
558     vnd_userial.fd = -1;
559     vnd_userial.btdriver_state = false;
560     if (rtk_parse_manager) {
561         rtk_parse_manager->rtk_parse_cleanup();
562     }
563     rtk_parse_manager = NULL;
564 #ifdef CONFIG_SCO_OVER_HCI
565     sbc_finish(&sco_cb.sbc_enc);
566     sbc_finish(&sco_cb.sbc_dec);
567 #endif
568     HILOGD("%s finish", __func__);
569 }
570 
571 /*******************************************************************************
572 **
573 ** Function        userial_vendor_set_baud
574 **
575 ** Description     Set new baud rate
576 **
577 ** Returns         None
578 **
579 *******************************************************************************/
userial_vendor_set_baud(uint8_t userial_baud)580 void userial_vendor_set_baud(uint8_t userial_baud)
581 {
582     uint32_t tcio_baud;
583     HILOGI("userial_vendor_set_baud");
584     userial_to_tcio_baud(userial_baud, &tcio_baud);
585 
586     if (cfsetospeed(&vnd_userial.termios, tcio_baud) < 0) {
587         HILOGE("cfsetospeed fail");
588     }
589 
590     if (cfsetispeed(&vnd_userial.termios, tcio_baud) < 0) {
591         HILOGE("cfsetispeed fail");
592     }
593 
594     if (tcsetattr(vnd_userial.fd, TCSANOW, &vnd_userial.termios) < 0) {
595         HILOGE("tcsetattr fail ");
596     }
597 
598     tcflush(vnd_userial.fd, TCIOFLUSH);
599 }
600 
601 /*******************************************************************************
602 **
603 ** Function        userial_vendor_ioctl
604 **
605 ** Description     ioctl inteface
606 **
607 ** Returns         None
608 **
609 *******************************************************************************/
userial_vendor_ioctl(userial_vendor_ioctl_op_t op,void * p_data)610 void userial_vendor_ioctl(userial_vendor_ioctl_op_t op, void *p_data)
611 {
612     RTK_UNUSED(p_data);
613     switch (op) {
614 #if (BT_WAKE_VIA_USERIAL_IOCTL == TRUE)
615         case USERIAL_OP_ASSERT_BT_WAKE:
616             VNDUSERIALDBG("## userial_vendor_ioctl: Asserting BT_Wake ##");
617             ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_ASSERT, NULL);
618             break;
619 
620         case USERIAL_OP_DEASSERT_BT_WAKE:
621             VNDUSERIALDBG("## userial_vendor_ioctl: De-asserting BT_Wake ##");
622             ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_DEASSERT, NULL);
623             break;
624 
625         case USERIAL_OP_GET_BT_WAKE_STATE:
626             ioctl(vnd_userial.fd, USERIAL_IOCTL_BT_WAKE_GET_ST, p_data);
627             break;
628 #endif //  (BT_WAKE_VIA_USERIAL_IOCTL==TRUE)
629 
630         default:
631             break;
632     }
633 }
634 
635 /*******************************************************************************
636 **
637 ** Function        userial_set_port
638 **
639 ** Description     Configure UART port name
640 **
641 ** Returns         0 : Success
642 **                 Otherwise : Fail
643 **
644 *******************************************************************************/
userial_set_port(char * p_conf_name,char * p_conf_value,int param)645 int userial_set_port(char *p_conf_name, char *p_conf_value, int param)
646 {
647     RTK_UNUSED(p_conf_name);
648     RTK_UNUSED(param);
649     strcpy_s(vnd_userial.port_name, VND_PORT_NAME_MAXLEN, p_conf_value);
650 
651     return 0;
652 }
653 
654 /*******************************************************************************
655 **
656 ** Function        userial_vendor_set_hw_fctrl
657 **
658 ** Description     Conduct vendor-specific close work
659 **
660 ** Returns         None
661 **
662 *******************************************************************************/
userial_vendor_set_hw_fctrl(uint8_t hw_fctrl)663 void userial_vendor_set_hw_fctrl(uint8_t hw_fctrl)
664 {
665     struct termios termios_old;
666 
667     if (vnd_userial.fd == -1) {
668         HILOGE("vnd_userial.fd is -1");
669         return;
670     }
671 
672     tcgetattr(vnd_userial.fd, &termios_old);
673     if (hw_fctrl) {
674         if (termios_old.c_cflag & CRTSCTS) {
675             BTVNDDBG("userial_vendor_set_hw_fctrl already hw flowcontrol on");
676             return;
677         } else {
678             termios_old.c_cflag |= CRTSCTS;
679             tcsetattr(vnd_userial.fd, TCSANOW, &termios_old);
680             BTVNDDBG("userial_vendor_set_hw_fctrl set hw flowcontrol on");
681         }
682     } else {
683         if (termios_old.c_cflag & CRTSCTS) {
684             termios_old.c_cflag &= ~CRTSCTS;
685             tcsetattr(vnd_userial.fd, TCSANOW, &termios_old);
686             return;
687         } else {
688             HILOGI("userial_vendor_set_hw_fctrl set hw flowcontrol off");
689             return;
690         }
691     }
692 }
693 
h4_int_transmit_data(uint8_t * data,uint16_t total_length)694 static uint16_t h4_int_transmit_data(uint8_t *data, uint16_t total_length)
695 {
696     assert(data != NULL);
697     assert(total_length > 0);
698 
699     uint16_t length = total_length;
700     uint16_t transmitted_length = 0;
701     while (length > 0 && vnd_userial.btdriver_state) {
702         ssize_t ret = write(vnd_userial.fd, data + transmitted_length, length);
703         switch (ret) {
704             case -1:
705                 HILOGE("In %s, error writing to the uart serial port: %s", __func__, strerror(errno));
706                 return transmitted_length;
707             case 0:
708                 // If we wrote nothing, don't loop more because we
709                 // can't go to infinity or beyond, ohterwise H5 can resend data
710                 HILOGE("%s, ret %zd", __func__, ret);
711                 return transmitted_length;
712             default:
713                 transmitted_length += ret;
714                 length -= ret;
715                 break;
716         }
717     }
718     return transmitted_length;
719 }
720 
userial_enqueue_coex_rawdata(unsigned char * buffer,int length,bool is_recved)721 static void userial_enqueue_coex_rawdata(unsigned char *buffer, int length, bool is_recved)
722 {
723     RTK_BUFFER *skb_data = RtbAllocate(length, 0);
724     RTK_BUFFER *skb_type = RtbAllocate(1, 0);
725     (void)memcpy_s(skb_data->Data, length, buffer, length);
726     skb_data->Length = length;
727     if (is_recved) {
728         *skb_type->Data = RTK_DATA_RECEIVED;
729         skb_type->Length = 1;
730         RtbQueueTail(vnd_userial.recv_data, skb_data);
731         RtbQueueTail(vnd_userial.data_order, skb_type);
732     } else {
733         *skb_type->Data = RTK_DATA_SEND;
734         skb_type->Length = 1;
735         RtbQueueTail(vnd_userial.send_data, skb_data);
736         RtbQueueTail(vnd_userial.data_order, skb_type);
737     }
738 
739     if (eventfd_write(vnd_userial.event_fd, 1) == -1) {
740         HILOGE("%s unable to write for coex event fd.", __func__);
741     }
742 }
743 
userial_send_cmd_to_controller(unsigned char * recv_buffer,int total_length)744 static void userial_send_cmd_to_controller(unsigned char *recv_buffer, int total_length)
745 {
746     char rtkbt_transtype_send_cmd = get_rtkbt_transtype();
747     if (rtkbt_transtype_send_cmd & RTKBT_TRANS_H4) {
748         h4_int_transmit_data(recv_buffer, total_length);
749     } else {
750         h5_int_interface->h5_send_cmd(DATA_TYPE_COMMAND, &recv_buffer[1], (total_length - 1));
751     }
752     userial_enqueue_coex_rawdata(recv_buffer, total_length, false);
753 }
754 
userial_send_acl_to_controller(unsigned char * recv_buffer,int total_length)755 static void userial_send_acl_to_controller(unsigned char *recv_buffer, int total_length)
756 {
757     char rtkbt_transtype_send_acl_to = get_rtkbt_transtype();
758     if (rtkbt_transtype_send_acl_to & RTKBT_TRANS_H4) {
759         h4_int_transmit_data(recv_buffer, total_length);
760     } else {
761         h5_int_interface->h5_send_acl_data(DATA_TYPE_ACL, &recv_buffer[1], (total_length - 1));
762     }
763     userial_enqueue_coex_rawdata(recv_buffer, total_length, false);
764 }
765 
userial_send_sco_to_controller(unsigned char * recv_buffer,int total_length)766 static void userial_send_sco_to_controller(unsigned char *recv_buffer, int total_length)
767 {
768     char rtkbt_transtype_send_sco_to = get_rtkbt_transtype();
769     if (rtkbt_transtype_send_sco_to & RTKBT_TRANS_H4) {
770         h4_int_transmit_data(recv_buffer, total_length);
771     } else {
772         h5_int_interface->h5_send_sco_data(DATA_TYPE_SCO, &recv_buffer[1], (total_length - 1));
773     }
774     userial_enqueue_coex_rawdata(recv_buffer, total_length, false);
775 }
776 
userial_coex_recv_data_handler(unsigned char * recv_buffer,int total_length)777 static int userial_coex_recv_data_handler(unsigned char *recv_buffer, int total_length)
778 {
779     serial_data_type_t type = 0;
780     unsigned char *p_data = recv_buffer;
781     int length = total_length;
782     HC_BT_HDR *p_buf;
783     uint8_t boundary_flag;
784     uint16_t len, handle, acl_length, l2cap_length;
785     switch (coex_packet_recv_state) {
786         case RTKBT_PACKET_IDLE:
787             coex_packet_bytes_need = 1;
788             while (length) {
789                 type = p_data[0];
790                 length--;
791                 p_data++;
792                 assert((type > DATA_TYPE_COMMAND) && (type <= DATA_TYPE_EVENT));
793                 if (type < DATA_TYPE_ACL || type > DATA_TYPE_EVENT) {
794                     HILOGE("%s invalid data type: %d", __func__, type);
795                     if (!length) {
796                         return total_length;
797                     }
798                     continue;
799                 }
800                 break;
801             }
802             coex_current_type = type;
803             coex_packet_recv_state = RTKBT_PACKET_TYPE;
804             // fall through
805             __attribute__((fallthrough));
806 
807         case RTKBT_PACKET_TYPE:
808             if (coex_current_type == DATA_TYPE_ACL) {
809 #define COEX_PACKET_BYTES_NEED_4 4
810                 coex_packet_bytes_need = COEX_PACKET_BYTES_NEED_4;
811             } else if (coex_current_type == DATA_TYPE_EVENT) {
812 #define COEX_PACKET_BYTES_NEED_2 2
813                 coex_packet_bytes_need = COEX_PACKET_BYTES_NEED_2;
814             } else {
815 #define COEX_PACKET_BYTES_NEED_3 3
816                 coex_packet_bytes_need = COEX_PACKET_BYTES_NEED_3;
817             }
818             coex_resvered_length = 0;
819             coex_packet_recv_state = RTKBT_PACKET_HEADER;
820             // fall through
821             __attribute__((fallthrough));
822 
823         case RTKBT_PACKET_HEADER:
824             if (length >= coex_packet_bytes_need) {
825                 (void)memcpy_s(&coex_resvered_buffer[coex_resvered_length], coex_packet_bytes_need, p_data,
826                                coex_packet_bytes_need);
827                 coex_resvered_length += coex_packet_bytes_need;
828                 length -= coex_packet_bytes_need;
829                 p_data += coex_packet_bytes_need;
830             } else {
831                 (void)memcpy_s(&coex_resvered_buffer[coex_resvered_length], length, p_data, length);
832                 coex_resvered_length += length;
833                 coex_packet_bytes_need -= length;
834                 length = 0;
835                 return total_length;
836             }
837             coex_packet_recv_state = RTKBT_PACKET_CONTENT;
838 
839             if (coex_current_type == DATA_TYPE_ACL) {
840 #define BUFFER_2 2
841                 coex_packet_bytes_need = *(uint16_t *)&coex_resvered_buffer[BUFFER_2];
842             } else if (coex_current_type == DATA_TYPE_EVENT) {
843                 coex_packet_bytes_need = coex_resvered_buffer[1];
844             } else {
845                 coex_packet_bytes_need = coex_resvered_buffer[BUFFER_2];
846             }
847             // fall through
848             __attribute__((fallthrough));
849 
850         case RTKBT_PACKET_CONTENT:
851             if (length >= coex_packet_bytes_need) {
852                 (void)memcpy_s(&coex_resvered_buffer[coex_resvered_length], coex_packet_bytes_need, p_data,
853                                coex_packet_bytes_need);
854                 length -= coex_packet_bytes_need;
855                 p_data += coex_packet_bytes_need;
856                 coex_resvered_length += coex_packet_bytes_need;
857                 coex_packet_bytes_need = 0;
858             } else {
859                 (void)memcpy_s(&coex_resvered_buffer[coex_resvered_length], length, p_data, length);
860                 coex_resvered_length += length;
861                 coex_packet_bytes_need -= length;
862                 length = 0;
863                 return total_length;
864             }
865             coex_packet_recv_state = RTKBT_PACKET_END;
866             // fall through
867             __attribute__((fallthrough));
868 
869         case RTKBT_PACKET_END: {
870             len = BT_HC_HDR_SIZE + coex_resvered_length;
871             uint8_t packet[len];
872             p_buf = (HC_BT_HDR *)packet;
873             p_buf->offset = 0;
874             p_buf->layer_specific = 0;
875             p_buf->len = coex_resvered_length;
876             (void)memcpy_s((uint8_t *)(p_buf + 1), coex_resvered_length, coex_resvered_buffer, coex_resvered_length);
877             switch (coex_current_type) {
878                 case DATA_TYPE_EVENT:
879                     p_buf->event = MSG_HC_TO_STACK_HCI_EVT;
880                     if (rtk_parse_manager) {
881                         rtk_parse_manager->rtk_parse_internal_event_intercept(coex_resvered_buffer);
882                     }
883                     break;
884 
885                 case DATA_TYPE_ACL:
886                     p_buf->event = MSG_HC_TO_STACK_HCI_ACL;
887                     handle = *(uint16_t *)coex_resvered_buffer;
888                     acl_length = *(uint16_t *)&coex_resvered_buffer[BUFFER_2];
889 #define BUFFER_4 4
890                     l2cap_length = *(uint16_t *)&coex_resvered_buffer[BUFFER_4];
891                     boundary_flag = RTK_GET_BOUNDARY_FLAG(handle);
892                     if (rtk_parse_manager) {
893                         rtk_parse_manager->rtk_parse_l2cap_data(coex_resvered_buffer, 0);
894                     }
895                     break;
896 
897                 case DATA_TYPE_SCO:
898                     p_buf->event = MSG_HC_TO_STACK_HCI_SCO;
899                     break;
900 
901                 default:
902                     p_buf->event = MSG_HC_TO_STACK_HCI_ERR;
903                     break;
904             }
905             rtk_btsnoop_capture(p_buf, true);
906         }
907             break;
908 
909         default:
910 
911             break;
912     }
913 
914     coex_packet_recv_state = RTKBT_PACKET_IDLE;
915     coex_packet_bytes_need = 0;
916     coex_current_type = 0;
917     coex_resvered_length = 0;
918 
919     return (total_length - length);
920 }
921 
userial_coex_send_data_handler(unsigned char * send_buffer,int total_length)922 static void userial_coex_send_data_handler(unsigned char *send_buffer, int total_length)
923 {
924     serial_data_type_t type = 0;
925     type = send_buffer[0];
926     int length = total_length;
927     HC_BT_HDR *p_buf;
928     uint8_t boundary_flag;
929     uint16_t len, handle, acl_length, l2cap_length;
930 
931     len = BT_HC_HDR_SIZE + (length - 1);
932     uint8_t packet[len];
933     p_buf = (HC_BT_HDR *)packet;
934     p_buf->offset = 0;
935     p_buf->layer_specific = 0;
936     p_buf->len = total_length - 1;
937     (void)memcpy_s((uint8_t *)(p_buf + 1), length - 1, &send_buffer[1], length - 1);
938 
939     switch (type) {
940         case DATA_TYPE_COMMAND:
941             p_buf->event = MSG_STACK_TO_HC_HCI_CMD;
942             if (rtk_parse_manager) {
943                 rtk_parse_manager->rtk_parse_command(&send_buffer[1]);
944             }
945             break;
946 
947         case DATA_TYPE_ACL:
948             p_buf->event = MSG_STACK_TO_HC_HCI_ACL;
949             handle = *(uint16_t *)&send_buffer[1];
950 #define SENDBUFFER_3 3
951 #define SENDBUFFER_5 5
952             acl_length = *(uint16_t *)&send_buffer[SENDBUFFER_3];
953             l2cap_length = *(uint16_t *)&send_buffer[SENDBUFFER_5];
954             boundary_flag = RTK_GET_BOUNDARY_FLAG(handle);
955             if (rtk_parse_manager) {
956                 rtk_parse_manager->rtk_parse_l2cap_data(&send_buffer[1], 1);
957             }
958 
959             break;
960 
961         case DATA_TYPE_SCO:
962             p_buf->event = MSG_STACK_TO_HC_HCI_SCO;
963             break;
964         default:
965             p_buf->event = 0;
966             HILOGE("%s invalid data type: %d", __func__, type);
967             break;
968     }
969     rtk_btsnoop_capture(p_buf, false);
970 }
971 
userial_coex_handler(void * context)972 static void userial_coex_handler(void *context)
973 {
974     RTK_UNUSED(context);
975     RTK_BUFFER *skb_data;
976     RTK_BUFFER *skb_type;
977     eventfd_t value;
978     unsigned int read_length = 0;
979     eventfd_read(vnd_userial.event_fd, &value);
980     if (!value && !vnd_userial.thread_running) {
981         return;
982     }
983 
984     while (!RtbQueueIsEmpty(vnd_userial.data_order)) {
985         read_length = 0;
986         skb_type = RtbDequeueHead(vnd_userial.data_order);
987         if (skb_type) {
988             if (*(skb_type->Data) == RTK_DATA_RECEIVED) {
989                 skb_data = RtbDequeueHead(vnd_userial.recv_data);
990                 if (skb_data) {
991                     do {
992                         read_length += userial_coex_recv_data_handler((skb_data->Data + read_length),
993                                                                       (skb_data->Length - read_length));
994                     } while (read_length < skb_data->Length);
995                     RtbFree(skb_data);
996                 }
997             } else {
998                 skb_data = RtbDequeueHead(vnd_userial.send_data);
999                 if (skb_data) {
1000                     userial_coex_send_data_handler(skb_data->Data, skb_data->Length);
1001                     RtbFree(skb_data);
1002                 }
1003             }
1004 
1005             RtbFree(skb_type);
1006         }
1007     }
1008 }
1009 
1010 #ifdef CONFIG_SCO_OVER_HCI
1011 // receive sco encode or non-encode data over hci, we need to decode msbc data to pcm, and send it to sco audio hal
userial_recv_sco_thread(void * arg)1012 static void *userial_recv_sco_thread(void *arg)
1013 {
1014     RTK_UNUSED(arg);
1015     RTK_BUFFER *skb_sco_data;
1016     unsigned char dec_data[480];
1017     unsigned char pcm_data[960];
1018     int index = 0;
1019     uint8_t *p_data = NULL;
1020     int res = 0;
1021     size_t writen = 0;
1022     prctl(PR_SET_NAME, (unsigned long)"userial_recv_sco_thread", 0, 0, 0);
1023     pthread_mutex_lock(&sco_cb.sco_recv_mutex);
1024 #define RTBGETQUEUELEN_60 60
1025     while (RtbGetQueueLen(sco_cb.recv_sco_data) > RTBGETQUEUELEN_60) {
1026         RTK_BUFFER *sco_data = RtbDequeueHead(sco_cb.recv_sco_data);
1027         if (sco_data) {
1028             RtbFree(sco_data);
1029         }
1030     }
1031     pthread_mutex_unlock(&sco_cb.sco_recv_mutex);
1032 
1033     HILOGE("userial_recv_sco_thread start");
1034     while (sco_cb.thread_recv_sco_running) {
1035         pthread_mutex_lock(&sco_cb.sco_recv_mutex);
1036         while (RtbQueueIsEmpty(sco_cb.recv_sco_data) && sco_cb.thread_sco_running) {
1037             pthread_cond_wait(&sco_cb.sco_recv_cond, &sco_cb.sco_recv_mutex);
1038         }
1039         pthread_mutex_unlock(&sco_cb.sco_recv_mutex);
1040         skb_sco_data = RtbDequeueHead(sco_cb.recv_sco_data);
1041         if (!skb_sco_data) {
1042             continue;
1043         }
1044         p_data = skb_sco_data->Data;
1045 
1046         if (!sco_cb.msbc_used) {
1047             res = Skt_Send_noblock(sco_cb.data_fd, p_data, sco_cb.sco_packet_len);
1048             if (res < 0) {
1049                 HILOGE("userial_recv_sco_thread, send noblock error");
1050             }
1051         } else {
1052 #define SBC_DECODE_PARAMETER_2 2
1053 #define SBC_DECODE_PARAMETER_58 58
1054 #define SBC_DECODE_PARAMETER_240 240
1055             res = sbc_decode(&sco_cb.sbc_dec, (p_data + SBC_DECODE_PARAMETER_2), SBC_DECODE_PARAMETER_58, dec_data,
1056                              SBC_DECODE_PARAMETER_240, &writen);
1057             if (res > 0) {
1058 #define MEMCPY_S_240 240
1059                 memcpy_s(&pcm_data[MEMCPY_S_240 * index], MEMCPY_S_240, dec_data, MEMCPY_S_240);
1060 
1061 #define INDEX_4 4
1062                 index = (index + 1) % INDEX_4;
1063                 if (index == 0) {
1064 #define SKT_SEND_NOBLOCK_960 960
1065                     Skt_Send_noblock(sco_cb.data_fd, pcm_data, SKT_SEND_NOBLOCK_960);
1066                 }
1067             } else {
1068                 HILOGE("msbc decode fail!");
1069             }
1070         }
1071         RtbFree(skb_sco_data);
1072     }
1073     HILOGE("userial_recv_sco_thread exit");
1074     RtbEmptyQueue(sco_cb.recv_sco_data);
1075     return NULL;
1076 }
1077 
userial_send_sco_thread(void * arg)1078 static void *userial_send_sco_thread(void *arg)
1079 {
1080     RTK_UNUSED(arg);
1081     unsigned char enc_data[240];
1082     unsigned char pcm_data[960 * 2];
1083     unsigned char send_data[100];
1084     int writen = 0;
1085     int num_read;
1086     prctl(PR_SET_NAME, (unsigned long)"userial_send_sco_thread", 0, 0, 0);
1087     sco_cb.pcm_enc_seq = 0;
1088     int i;
1089 
1090     // when start sco send thread, first send 6 sco data to controller
1091     if (!sco_cb.msbc_used) {
1092 #define MEMSET_S_48 48
1093 #define MEMSET_S_6 6
1094 #define DATA_3 3
1095 #define MEMSET_S_4 4
1096 #define CONTROLLER_52 52
1097         (void)memset_s(pcm_data, (MEMSET_S_48 * MEMSET_S_6), 0, (MEMSET_S_48 * MEMSET_S_6));
1098 #define FOR_6 6
1099         for (i = 0; i < FOR_6; i++) {
1100             send_data[0] = DATA_TYPE_SCO;
1101             send_data[DATA_3] = MEMSET_S_48;
1102             *(uint16_t *)&send_data[1] = sco_cb.sco_handle;
1103             (void)memcpy_s(&send_data[MEMSET_S_4], MEMSET_S_48, &pcm_data[i * MEMSET_S_48], MEMSET_S_48);
1104             userial_send_sco_to_controller(send_data, CONTROLLER_52);
1105         }
1106     }
1107     HILOGE("userial_send_sco_thread start");
1108     while (sco_cb.thread_send_sco_running) {
1109         if (!sco_cb.msbc_used) {
1110 #define SKT_READ_48 48
1111 #define SKT_READ_5 5
1112 #define SKT_READ_960 960
1113             num_read = Skt_Read(sco_cb.data_fd, pcm_data, SKT_READ_48 * SKT_READ_5, &sco_cb.thread_send_sco_running);
1114             if (!num_read) {
1115                 continue;
1116             }
1117 #define FOR_5 5
1118             for (i = 0; i < FOR_5; i++) {
1119                 send_data[0] = DATA_TYPE_SCO;
1120                 send_data[DATA_3] = MEMSET_S_48;
1121                 *(uint16_t *)&send_data[1] = sco_cb.sco_handle;
1122                 (void)memcpy_s(&send_data[MEMSET_S_4], MEMSET_S_48, &pcm_data[i * MEMSET_S_48], MEMSET_S_48);
1123                 userial_send_sco_to_controller(send_data, CONTROLLER_52);
1124             }
1125         } else {
1126             num_read = Skt_Read(sco_cb.data_fd, pcm_data, SKT_READ_960, &sco_cb.thread_send_sco_running);
1127             if (!num_read) {
1128                 continue;
1129             }
1130 #define FOR_4 4
1131             for (i = 0; i < FOR_4; i++) {
1132 #define SBC_ENCODE_240 240
1133 #define SBC_ENCODE_60 60
1134 #define SBC_ENCODE_2 2
1135 #define SBC_ENCODE_58 58
1136 #define DATA_59 59
1137                 if (sbc_encode(&sco_cb.sbc_enc, &pcm_data[SBC_ENCODE_240 * i], SBC_ENCODE_240,
1138                                &enc_data[i * SBC_ENCODE_60 + 2L], SBC_ENCODE_58, (ssize_t *)&writen) <= 0) {
1139                     HILOGE("sbc encode error!");
1140                 } else {
1141 #define NUM_4 4
1142                     *(uint16_t *)(&(enc_data[i * SBC_ENCODE_60])) = btui_msbc_h2[sco_cb.pcm_enc_seq % NUM_4];
1143                     sco_cb.pcm_enc_seq++;
1144                     enc_data[i * SBC_ENCODE_60 + DATA_59] = 0x00; // padding
1145                 }
1146             }
1147             for (i = 0; i < FOR_5; i++) {
1148                 send_data[0] = DATA_TYPE_SCO;
1149                 send_data[DATA_3] = SKT_READ_48;
1150                 *(uint16_t *)&send_data[1] = sco_cb.sco_handle;
1151                 (void)memcpy_s(&send_data[MEMSET_S_4], SKT_READ_48, &enc_data[i * SKT_READ_48], SKT_READ_48);
1152                 userial_send_sco_to_controller(send_data, CONTROLLER_52);
1153             }
1154         }
1155     }
1156     HILOGE("userial_send_sco_thread exit");
1157     return NULL;
1158 }
1159 
userial_sco_send_socket_stop()1160 static void userial_sco_send_socket_stop()
1161 {
1162     HILOGE("%s", __func__);
1163     pthread_mutex_lock(&sco_cb.sco_send_mutex);
1164     if (sco_cb.thread_send_sco_running) {
1165         sco_cb.thread_send_sco_running = false;
1166     } else {
1167         pthread_mutex_unlock(&sco_cb.sco_send_mutex);
1168         return;
1169     }
1170     pthread_mutex_unlock(&sco_cb.sco_send_mutex);
1171 
1172     if (sco_cb.thread_send_sco_id != -1) {
1173         pthread_join(sco_cb.thread_send_sco_id, NULL);
1174         sco_cb.thread_send_sco_id = -1;
1175     }
1176 }
1177 
userial_sco_recv_socket_stop()1178 static void userial_sco_recv_socket_stop()
1179 {
1180     HILOGE("%s", __func__);
1181     pthread_mutex_lock(&sco_cb.sco_recv_mutex);
1182     if (sco_cb.thread_recv_sco_running) {
1183         sco_cb.thread_recv_sco_running = false;
1184         pthread_cond_signal(&sco_cb.sco_recv_cond);
1185     } else {
1186         pthread_mutex_unlock(&sco_cb.sco_recv_mutex);
1187         return;
1188     }
1189     pthread_mutex_unlock(&sco_cb.sco_recv_mutex);
1190 
1191     if (sco_cb.thread_recv_sco_id != -1) {
1192         pthread_join(sco_cb.thread_recv_sco_id, NULL);
1193         sco_cb.thread_recv_sco_id = -1;
1194     }
1195 }
1196 
userial_sco_socket_stop()1197 static void userial_sco_socket_stop()
1198 {
1199     HILOGE("%s", __func__);
1200     userial_sco_send_socket_stop();
1201     userial_sco_recv_socket_stop();
1202     if (sco_cb.ctrl_fd > 0) {
1203         close(sco_cb.ctrl_fd);
1204         sco_cb.ctrl_fd = -1;
1205     }
1206     if (sco_cb.data_fd > 0) {
1207         close(sco_cb.data_fd);
1208         sco_cb.data_fd = -1;
1209     }
1210     RtbEmptyQueue(sco_cb.recv_sco_data);
1211 }
1212 
userial_sco_ctrl_skt_handle()1213 static void userial_sco_ctrl_skt_handle()
1214 {
1215     uint8_t cmd = 0, ack = 0;
1216     ;
1217     int result = Skt_Read(sco_cb.ctrl_fd, &cmd, 1, NULL);
1218     if (result == 0) {
1219         userial_sco_socket_stop();
1220         return;
1221     }
1222 
1223     HILOGE("%s, cmd = %d, msbc_used = %d", __func__, cmd, sco_cb.msbc_used);
1224     switch (cmd) {
1225         case SCO_CTRL_CMD_CHECK_READY:
1226 
1227             break;
1228 
1229         case SCO_CTRL_CMD_OUT_START: {
1230             pthread_attr_t thread_attr;
1231             pthread_attr_init(&thread_attr);
1232             pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
1233             sco_cb.thread_send_sco_running = true;
1234             if (pthread_create(&sco_cb.thread_send_sco_id, &thread_attr, userial_send_sco_thread, NULL) != 0) {
1235                 HILOGE("pthread_create : %s", strerror(errno));
1236             }
1237         }
1238             break;
1239 
1240         case SCO_CTRL_CMD_IN_START: {
1241             pthread_attr_t thread_attr;
1242             pthread_attr_init(&thread_attr);
1243             pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
1244             sco_cb.thread_recv_sco_running = true;
1245             if (pthread_create(&sco_cb.thread_recv_sco_id, &thread_attr, userial_recv_sco_thread, NULL) != 0) {
1246                 HILOGE("pthread_create : %s", strerror(errno));
1247             }
1248         }
1249             break;
1250 
1251         case SCO_CTRL_CMD_OUT_STOP:
1252             userial_sco_send_socket_stop();
1253             break;
1254 
1255         case SCO_CTRL_CMD_IN_STOP:
1256             userial_sco_recv_socket_stop();
1257 
1258             break;
1259 
1260         case SCO_CTRL_CMD_SUSPEND:
1261 
1262             break;
1263 
1264         case SCO_CTRL_GET_AUDIO_CONFIG:
1265             if (sco_cb.msbc_used) {
1266 #define ACK_2 2
1267                 ack = ACK_2;
1268                 Skt_Send(sco_cb.ctrl_fd, &ack, 1);
1269             } else {
1270                 ack = 1;
1271                 Skt_Send(sco_cb.ctrl_fd, &ack, 1);
1272             }
1273             break;
1274 
1275         case SCO_CTRL_CMD_CLOSE:
1276             userial_sco_socket_stop();
1277             break;
1278 
1279         default:
1280 
1281             break;
1282     }
1283 }
1284 
userial_socket_sco_thread(void * arg)1285 static void *userial_socket_sco_thread(void *arg)
1286 {
1287     RTK_UNUSED(arg);
1288     struct sockaddr_un addr, remote;
1289     socklen_t len = sizeof(struct sockaddr_un);
1290     fd_set read_set, active_set;
1291     int result, max_fd;
1292     int s_ctrl = socket(AF_LOCAL, SOCK_STREAM, 0);
1293     if (s_ctrl < 0) {
1294         HILOGE("ctrl socket create fail");
1295         return NULL;
1296     }
1297     int s_data = socket(AF_LOCAL, SOCK_STREAM, 0);
1298     if (s_data < 0) {
1299         HILOGE("data socket create fail");
1300         close(s_ctrl);
1301         return NULL;
1302     }
1303     prctl(PR_SET_NAME, (unsigned long)"userial_socket_sco_thread", 0, 0, 0);
1304 
1305     if ((socketpair(AF_UNIX, SOCK_STREAM, 0, sco_cb.signal_fd)) < 0) {
1306         HILOGE("%s, errno : %s", __func__, strerror(errno));
1307         goto socket_close;
1308     }
1309 
1310     // bind sco ctrl socket
1311     // unlink SCO_CTRL_PATH
1312 
1313 #if NOTDEF
1314     memset_s(&addr, sizeof(addr), 0, sizeof(addr));
1315     strcpy_s(addr.sun_path, sizeof(addr.sun_path), SCO_CTRL_PATH);
1316     addr.sun_family = AF_UNIX;
1317     addr.sun_path[0] = 0;
1318     alen = strlen(addr.sun_path) + offsetof(struct sockaddr_un, sun_path);
1319     if (bind(s_ctrl, (struct sockaddr *)&addr, alen) < 0) {
1320         HILOGE("userial_socket_sco_thread, bind ctrl socket error : %s", strerror(errno));
1321         return NULL;
1322     }
1323 #else
1324     if (socket_local_server_bind(s_ctrl, SCO_CTRL_PATH, ANDROID_SOCKET_NAMESPACE_ABSTRACT) < 0) {
1325         HILOGE("ctrl socket failed to create (%s)", strerror(errno));
1326         goto signal_close;
1327     }
1328 #endif
1329 #define LISTEN_5 5
1330     if (listen(s_ctrl, LISTEN_5) < 0) {
1331         HILOGE("userial_socket_sco_thread, listen ctrl socket error : %s", strerror(errno));
1332         goto signal_close;
1333     }
1334 
1335     int res = chmod(SCO_CTRL_PATH, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
1336     if (res < 0) {
1337         HILOGE("chmod ctrl path fail");
1338     }
1339     // bind sco data socket
1340     // unlink SCO_DATA_PATH
1341 
1342     if (socket_local_server_bind(s_data, SCO_DATA_PATH, ANDROID_SOCKET_NAMESPACE_ABSTRACT) < 0) {
1343         HILOGE("data socket failed to create (%s)", strerror(errno));
1344         goto signal_close;
1345     }
1346 
1347     if (listen(s_data, LISTEN_5) < 0) {
1348         HILOGE("userial_socket_sco_thread, listen data socket error : %s", strerror(errno));
1349         goto signal_close;
1350     }
1351     res = chmod(SCO_DATA_PATH, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
1352     if (res < 0) {
1353         HILOGE("chmod data path fail");
1354     }
1355 
1356     HILOGE("userial_socket_sco_thread");
1357     FD_ZERO(&read_set);
1358     FD_ZERO(&active_set);
1359     FD_SET(s_ctrl, &active_set);
1360     FD_SET(s_data, &active_set);
1361     FD_SET(sco_cb.signal_fd[1], &active_set);
1362     max_fd = MAX(s_ctrl, s_data);
1363     max_fd = MAX(max_fd, sco_cb.signal_fd[1]) + 1;
1364     while (sco_cb.thread_sco_running) {
1365         read_set = active_set;
1366         result = select(max_fd, &read_set, NULL, NULL, NULL);
1367         if (result == 0) {
1368             HILOGE("select timeout");
1369             continue;
1370         }
1371         if (result < 0) {
1372             if (errno != EINTR) {
1373                 HILOGE("select failed %s", strerror(errno));
1374             }
1375             continue;
1376         }
1377         if (FD_ISSET(s_ctrl, &read_set)) {
1378             if (sco_cb.ctrl_fd > 0) {
1379                 HILOGE("Already has connect a control fd: %d", sco_cb.ctrl_fd);
1380                 FD_SET(sco_cb.ctrl_fd, &read_set);
1381                 close(sco_cb.ctrl_fd);
1382             }
1383             RTK_NO_INTR(sco_cb.ctrl_fd = accept(s_ctrl, (struct sockaddr *)&remote, &len));
1384             if (sco_cb.ctrl_fd == -1) {
1385                 HILOGE("sock accept failed (%s)", strerror(errno));
1386                 continue;
1387             }
1388             const int size = (512);
1389             setsockopt(sco_cb.ctrl_fd, SOL_SOCKET, SO_RCVBUF, (char *)&size, (int)sizeof(size));
1390             FD_SET(sco_cb.ctrl_fd, &active_set);
1391             max_fd = (MAX(max_fd, sco_cb.ctrl_fd)) + 1;
1392         }
1393 
1394         if (FD_ISSET(s_data, &read_set)) {
1395             if (sco_cb.data_fd > 0) {
1396                 HILOGE("Already has connect a control fd: %d", sco_cb.data_fd);
1397                 close(sco_cb.data_fd);
1398             }
1399             RTK_NO_INTR(sco_cb.data_fd = accept(s_data, (struct sockaddr *)&remote, &len));
1400             if (sco_cb.data_fd == -1) {
1401                 HILOGE("socket accept failed (%s)", strerror(errno));
1402                 continue;
1403             }
1404             const int size = (30 * 960);
1405             int ret = setsockopt(sco_cb.data_fd, SOL_SOCKET, SO_RCVBUF, (char *)&size, (int)sizeof(size));
1406             ret = setsockopt(sco_cb.data_fd, SOL_SOCKET, SO_SNDBUF, (char *)&size, (int)sizeof(size));
1407         }
1408 
1409         if (sco_cb.ctrl_fd > 0 && FD_ISSET(sco_cb.ctrl_fd, &read_set)) {
1410             userial_sco_ctrl_skt_handle();
1411         }
1412     }
1413 
1414     userial_sco_socket_stop();
1415 signal_close:
1416     close(sco_cb.signal_fd[0]);
1417     close(sco_cb.signal_fd[1]);
1418 socket_close:
1419     close(s_ctrl);
1420     close(s_data);
1421 
1422     (void)memset_s(&addr, sizeof(addr), 0, sizeof(addr));
1423     (void)strcpy_s((addr.sun_path + 1), sizeof(addr.sun_path), SCO_DATA_PATH);
1424     addr.sun_path[0] = 0;
1425     unlink(addr.sun_path);
1426 
1427     (void)memset_s(&addr, sizeof(addr), 0, sizeof(addr));
1428     (void)strcpy_s((addr.sun_path + 1), sizeof(addr.sun_path), SCO_CTRL_PATH);
1429     addr.sun_path[0] = 0;
1430     unlink(addr.sun_path);
1431     HILOGE("userial_socket_sco_thread exit");
1432     return NULL;
1433 }
1434 
1435 #endif
1436 
1437 #ifdef RTK_HANDLE_CMD
1438 
1439 #define RECV_BUFFER_2 2
1440 #define RECV_BUFFER_3 3
1441 #define RECV_BUFFER_4 4
1442 #define RECV_BUFFER_5 5
1443 #define RECV_BUFFER_6 6
1444 #define RECV_BUFFER_7 7
1445 #define RECV_BUFFER_9 9
1446 #define RECV_BUFFER_15 15
1447 #define RECV_BUFFER_20 20
1448 #define RECV_BUFFER_40 40
1449 
userial_handle_cmd(unsigned char * recv_buffer,int total_length)1450 static void userial_handle_cmd(unsigned char *recv_buffer, int total_length)
1451 {
1452     RTK_UNUSED(total_length);
1453     uint16_t opcode = *(uint16_t *)recv_buffer;
1454     uint16_t scan_int, scan_win;
1455     static uint16_t voice_settings;
1456     char prop_value[100];
1457     char rtkbt_transtype_handle_cmd = get_rtkbt_transtype();
1458 
1459     switch (opcode) {
1460         case HCI_BLE_WRITE_SCAN_PARAMS:
1461             scan_int = *(uint16_t *)&recv_buffer[RECV_BUFFER_4];
1462             scan_win = *(uint16_t *)&recv_buffer[RECV_BUFFER_6];
1463             if (scan_win > RECV_BUFFER_20) {
1464                 if ((scan_int / scan_win) > RECV_BUFFER_2) {
1465                     *(uint16_t *)&recv_buffer[RECV_BUFFER_4] = (scan_int * RECV_BUFFER_20) / scan_win;
1466                     *(uint16_t *)&recv_buffer[RECV_BUFFER_6] = RECV_BUFFER_20;
1467                 } else {
1468                     *(uint16_t *)&recv_buffer[RECV_BUFFER_4] = RECV_BUFFER_40;
1469                     *(uint16_t *)&recv_buffer[RECV_BUFFER_6] = RECV_BUFFER_20;
1470                 }
1471             } else if (scan_win == scan_int) {
1472                 *(uint16_t *)&recv_buffer[RECV_BUFFER_4] = (scan_int * RECV_BUFFER_5) & 0xFE;
1473             } else if ((scan_int / scan_win) <= RECV_BUFFER_2) {
1474                 *(uint16_t *)&recv_buffer[RECV_BUFFER_4] = (scan_int * RECV_BUFFER_3) & 0xFE;
1475             }
1476             break;
1477 
1478         case HCI_LE_SET_EXTENDED_SCAN_PARAMETERS:
1479             scan_int = *(uint16_t *)&recv_buffer[RECV_BUFFER_7];
1480             scan_win = *(uint16_t *)&recv_buffer[RECV_BUFFER_9];
1481             if (scan_win > RECV_BUFFER_20) {
1482                 if ((scan_int / scan_win) > RECV_BUFFER_2) {
1483                     *(uint16_t *)&recv_buffer[RECV_BUFFER_7] = (scan_int * RECV_BUFFER_20) / scan_win;
1484                     *(uint16_t *)&recv_buffer[RECV_BUFFER_9] = RECV_BUFFER_20;
1485                 } else {
1486                     *(uint16_t *)&recv_buffer[RECV_BUFFER_7] = RECV_BUFFER_40;
1487                     *(uint16_t *)&recv_buffer[RECV_BUFFER_9] = RECV_BUFFER_20;
1488                 }
1489             } else if (scan_win == scan_int) {
1490                 *(uint16_t *)&recv_buffer[RECV_BUFFER_7] = (scan_int * RECV_BUFFER_5) & 0xFE;
1491             } else if ((scan_int / scan_win) <= RECV_BUFFER_2) {
1492                 *(uint16_t *)&recv_buffer[RECV_BUFFER_9] = (scan_int * RECV_BUFFER_3) & 0xFE;
1493             }
1494 
1495             break;
1496 
1497         case HCI_WRITE_VOICE_SETTINGS:
1498             voice_settings = *(uint16_t *)&recv_buffer[RECV_BUFFER_3];
1499             if (rtkbt_transtype_handle_cmd & RTKBT_TRANS_USB) {
1500                 userial_vendor_usb_ioctl(SET_ISO_CFG, &voice_settings);
1501             }
1502 #ifdef CONFIG_SCO_OVER_HCI
1503             sco_cb.voice_settings = voice_settings;
1504 #endif
1505             break;
1506 #ifdef CONFIG_SCO_OVER_HCI
1507         case HCI_SETUP_ESCO_CONNECTION:
1508             sco_cb.voice_settings = *(uint16_t *)&recv_buffer[RECV_BUFFER_15];
1509             sco_cb.ctrl_fd = -1;
1510             sco_cb.data_fd = -1;
1511             break;
1512 #endif
1513         case HCI_SET_EVENT_MASK:
1514             HILOGE("set event mask, it should bt stack init, set coex bt on");
1515             if (rtk_parse_manager) {
1516                 rtk_parse_manager->rtk_set_bt_on(1);
1517             }
1518             Heartbeat_init();
1519             break;
1520 
1521         case HCI_ACCEPT_CONNECTION_REQUEST:
1522             (void)memset_s(prop_value, sizeof(prop_value), 0, sizeof(prop_value));
1523             (void)strcpy_s(prop_value, sizeof(prop_value), "master");
1524             if (strcmp(prop_value, "none") != 0) {
1525                 int role = recv_buffer[RECV_BUFFER_9];
1526                 if (role == 0x01 && (strcmp(prop_value, "master") == 0)) {
1527                     recv_buffer[RECV_BUFFER_9] = 0x00;
1528                 } else if (role == 0x00 && (strcmp(prop_value, "slave") == 0)) {
1529                     recv_buffer[RECV_BUFFER_9] = 0x01;
1530                 }
1531             }
1532             break;
1533 
1534         case HCI_BLE_WRITE_ADV_PARAMS: {
1535             if (rtkbt_version.hci_version > HCI_PROTO_VERSION_4_2) {
1536                 break;
1537             }
1538             rtkbt_adv_con.adverting_type = recv_buffer[7L];
1539             (void)memset_s(prop_value, sizeof(prop_value), 0, sizeof(prop_value));
1540             (void)strcpy_s(prop_value, sizeof(prop_value), "false");
1541             if (rtkbt_adv_con.adverting_type == 0x00 && (strcmp(prop_value, "true") == 0)) {
1542                 recv_buffer[RECV_BUFFER_7] = 0x03;
1543                 rtkbt_adv_con.adverting_type = 0x03;
1544             }
1545         }
1546             break;
1547         case HCI_BLE_WRITE_ADV_ENABLE: {
1548             if (rtkbt_version.hci_version > HCI_PROTO_VERSION_4_2) {
1549                 break;
1550             }
1551             if (recv_buffer[RECV_BUFFER_2] == 0x01) {
1552                 rtkbt_adv_con.adverting_start = TRUE;
1553             } else if (recv_buffer[RECV_BUFFER_2] == 0x00) {
1554                 rtkbt_adv_con.adverting_type = 0;
1555                 rtkbt_adv_con.adverting_enable = FALSE;
1556                 rtkbt_adv_con.adverting_start = FALSE;
1557             }
1558         }
1559             break;
1560         case HCI_BLE_CREATE_LL_CONN:
1561             if (rtkbt_version.hci_version > HCI_PROTO_VERSION_4_2) {
1562                 break;
1563             }
1564             if (rtkbt_adv_con.adverting_enable &&
1565                 ((rtkbt_adv_con.adverting_type == 0x00) || (rtkbt_adv_con.adverting_type == 0x01) ||
1566                  (rtkbt_adv_con.adverting_type == 0x04))) {
1567                 uint8_t disable_adv_cmd[RECV_BUFFER_5] = {0x01, 0x0A, 0x20, 0x01, 0x00};
1568                 rtkbt_adv_con.adverting_enable = FALSE;
1569                 userial_send_cmd_to_controller(disable_adv_cmd, RECV_BUFFER_5);
1570             }
1571             break;
1572         default:
1573             break;
1574     }
1575 }
1576 #endif
1577 
1578 // This recv data from bt process. The data type only have ACL/SCO/COMMAND
1579 //  direction  BT HOST ----> CONTROLLER
userial_recv_H4_rawdata(void * context)1580 static void userial_recv_H4_rawdata(void *context)
1581 {
1582     RTK_UNUSED(context);
1583     serial_data_type_t type = 0;
1584     ssize_t bytes_read;
1585     uint16_t opcode;
1586     uint16_t transmitted_length = 0;
1587     char rtkbt_transtype_recv_H4_rawdata = get_rtkbt_transtype();
1588 
1589     switch (packet_recv_state) {
1590         case RTKBT_PACKET_IDLE:
1591             packet_bytes_need = 1;
1592             do {
1593                 RTK_NO_INTR(bytes_read = read(vnd_userial.uart_fd[1], &type, 1));
1594                 if (bytes_read == -1) {
1595                     HILOGE("%s, state = %d, read error %s", __func__, packet_recv_state, strerror(errno));
1596                     return;
1597                 }
1598                 if (!bytes_read && packet_bytes_need) {
1599                     HILOGE("%s, state = %d, bytes_read 0", __func__, packet_recv_state);
1600                     return;
1601                 }
1602 
1603                 if (type < DATA_TYPE_COMMAND || type > DATA_TYPE_SCO) {
1604                     HILOGE("%s invalid data type: %d", __func__, type);
1605                     assert((type >= DATA_TYPE_COMMAND) && (type <= DATA_TYPE_SCO));
1606                 } else {
1607                     packet_bytes_need -= bytes_read;
1608                     packet_recv_state = RTKBT_PACKET_TYPE;
1609                     current_type = type;
1610                     h4_read_buffer[0] = type;
1611                 }
1612             } while (packet_bytes_need);
1613             // fall through
1614             __attribute__((fallthrough));
1615 
1616         case RTKBT_PACKET_TYPE:
1617             packet_bytes_need = hci_preamble_sizes[HCI_PACKET_TYPE_TO_INDEX(current_type)];
1618             h4_read_length = 0;
1619             packet_recv_state = RTKBT_PACKET_HEADER;
1620             // fall through
1621             __attribute__((fallthrough));
1622 
1623         case RTKBT_PACKET_HEADER:
1624             do {
1625                 RTK_NO_INTR(bytes_read =
1626                                 read(vnd_userial.uart_fd[1], &h4_read_buffer[h4_read_length + 1], packet_bytes_need));
1627                 if (bytes_read == -1) {
1628                     HILOGE("%s, state = %d, read error %s", __func__, packet_recv_state, strerror(errno));
1629                     return;
1630                 }
1631                 if (!bytes_read && packet_bytes_need) {
1632                     HILOGE("%s, state = %d, bytes_read 0, type : %d", __func__, packet_recv_state, current_type);
1633                     return;
1634                 }
1635                 packet_bytes_need -= bytes_read;
1636                 h4_read_length += bytes_read;
1637             } while (packet_bytes_need);
1638             packet_recv_state = RTKBT_PACKET_CONTENT;
1639 
1640             if (current_type == DATA_TYPE_ACL) {
1641                 packet_bytes_need = *(uint16_t *)&h4_read_buffer[COMMON_DATA_LENGTH_INDEX];
1642             } else if (current_type == DATA_TYPE_EVENT) {
1643                 packet_bytes_need = h4_read_buffer[EVENT_DATA_LENGTH_INDEX];
1644             } else {
1645                 packet_bytes_need = h4_read_buffer[COMMON_DATA_LENGTH_INDEX];
1646             }
1647             // fall through
1648             __attribute__((fallthrough));
1649 
1650         case RTKBT_PACKET_CONTENT:
1651             while (packet_bytes_need) {
1652                 RTK_NO_INTR(bytes_read =
1653                                 read(vnd_userial.uart_fd[1], &h4_read_buffer[h4_read_length + 1], packet_bytes_need));
1654                 if (bytes_read == -1) {
1655                     HILOGE("%s, state = %d, read error %s", __func__, packet_recv_state, strerror(errno));
1656                     return;
1657                 }
1658                 if (!bytes_read) {
1659                     HILOGE("%s, state = %d, bytes_read 0", __func__, packet_recv_state);
1660                     return;
1661                 }
1662 
1663                 packet_bytes_need -= bytes_read;
1664                 h4_read_length += bytes_read;
1665             }
1666             packet_recv_state = RTKBT_PACKET_END;
1667             // fall through
1668             __attribute__((fallthrough));
1669 
1670         case RTKBT_PACKET_END:
1671             switch (current_type) {
1672                 case DATA_TYPE_COMMAND:
1673 #ifdef RTK_HANDLE_CMD
1674                     userial_handle_cmd(&h4_read_buffer[1], h4_read_length);
1675 #endif
1676                     if (rtkbt_transtype_recv_H4_rawdata & RTKBT_TRANS_H4) {
1677                         h4_int_transmit_data(h4_read_buffer, (h4_read_length + 1));
1678                     } else {
1679                         opcode = *(uint16_t *)&h4_read_buffer[1];
1680                         if (opcode == HCI_VSC_H5_INIT) {
1681                             h5_int_interface->h5_send_sync_cmd(opcode, NULL, h4_read_length);
1682                         } else {
1683                             transmitted_length =
1684                                 h5_int_interface->h5_send_cmd(type, &h4_read_buffer[1], h4_read_length);
1685                         }
1686                     }
1687                     userial_enqueue_coex_rawdata(h4_read_buffer, (h4_read_length + 1), false);
1688                     break;
1689 
1690                 case DATA_TYPE_ACL:
1691                     userial_send_acl_to_controller(h4_read_buffer, (h4_read_length + 1));
1692                     break;
1693 
1694                 case DATA_TYPE_SCO:
1695                     userial_send_sco_to_controller(h4_read_buffer, (h4_read_length + 1));
1696                     break;
1697                 default:
1698                     HILOGE("%s invalid data type: %d", __func__, current_type);
1699                     userial_enqueue_coex_rawdata(h4_read_buffer, (h4_read_length + 1), false);
1700                     break;
1701             }
1702             break;
1703 
1704         default:
1705 
1706             break;
1707     }
1708 
1709     packet_recv_state = RTKBT_PACKET_IDLE;
1710     packet_bytes_need = 0;
1711     current_type = 0;
1712     h4_read_length = 0;
1713 }
1714 
h5_int_transmit_data_cb(serial_data_type_t type,uint8_t * data,uint16_t length)1715 static uint16_t h5_int_transmit_data_cb(serial_data_type_t type, uint8_t *data, uint16_t length)
1716 {
1717     assert(data != NULL);
1718     assert(length > 0);
1719 
1720     if (type != DATA_TYPE_H5) {
1721         HILOGE("%s invalid data type: %d", __func__, type);
1722         return 0;
1723     }
1724 
1725     uint16_t transmitted_length = 0;
1726     while (length > 0 && vnd_userial.btdriver_state) {
1727         ssize_t ret = write(vnd_userial.fd, data + transmitted_length, length);
1728         switch (ret) {
1729             case -1:
1730                 HILOGE("In %s, error writing to the uart serial port: %s", __func__, strerror(errno));
1731                 goto done;
1732             case 0:
1733                 // If we wrote nothing, don't loop more because we
1734                 // can't go to infinity or beyond, ohterwise H5 can resend data
1735                 HILOGE("%s, ret %zd", __func__, ret);
1736                 goto done;
1737             default:
1738                 transmitted_length += ret;
1739                 length -= ret;
1740                 break;
1741         }
1742     }
1743 
1744 done:;
1745     return transmitted_length;
1746 }
1747 
1748 #ifdef RTK_HANDLE_EVENT
userial_handle_event(unsigned char * recv_buffer,int total_length)1749 static void userial_handle_event(unsigned char *recv_buffer, int total_length)
1750 {
1751     RTK_UNUSED(total_length);
1752     uint8_t event;
1753     uint8_t *p_data = recv_buffer;
1754     event = p_data[0];
1755     switch (event) {
1756         case HCI_COMMAND_COMPLETE_EVT: {
1757             uint16_t opcode = *((uint16_t *)&p_data[3]);
1758             uint8_t *stream = &p_data[6];
1759             if (opcode == HCI_READ_LOCAL_VERSION_INFO) {
1760                 STREAM_TO_UINT8(rtkbt_version.hci_version, stream);
1761                 STREAM_TO_UINT16(rtkbt_version.hci_revision, stream);
1762                 STREAM_TO_UINT8(rtkbt_version.lmp_version, stream);
1763                 STREAM_TO_UINT16(rtkbt_version.manufacturer, stream);
1764                 STREAM_TO_UINT16(rtkbt_version.lmp_subversion, stream);
1765             } else if (opcode == HCI_BLE_WRITE_ADV_ENABLE) {
1766                 if (rtkbt_version.hci_version > HCI_PROTO_VERSION_4_2) {
1767                     break;
1768                 }
1769 #define P_DATA_5 5
1770                 if (rtkbt_adv_con.adverting_start && (p_data[P_DATA_5] == HCI_SUCCESS)) {
1771                     rtkbt_adv_con.adverting_enable = TRUE;
1772                     rtkbt_adv_con.adverting_start = FALSE;
1773                 }
1774             }
1775         }
1776             break;
1777 #ifdef CONFIG_SCO_OVER_HCI
1778 #define P_DATA_2 2
1779 #define P_DATA_3 3
1780 #define SCO_PACKET_LEN_240 240
1781 #define SCO_PACKET_LEN_60 60
1782         case HCI_ESCO_CONNECTION_COMP_EVT: {
1783             if (p_data[P_DATA_2] != 0) {
1784                 sco_cb.thread_sco_running = false;
1785                 sco_cb.thread_recv_sco_running = false;
1786                 sco_cb.thread_send_sco_running = false;
1787                 sco_cb.data_fd = -1;
1788                 sco_cb.ctrl_fd = -1;
1789             } else {
1790                 sco_cb.sco_handle = *((uint16_t *)&p_data[P_DATA_3]);
1791                 pthread_attr_t thread_attr;
1792                 pthread_attr_init(&thread_attr);
1793                 pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
1794                 sco_cb.thread_sco_running = true;
1795                 sco_cb.thread_recv_sco_running = false;
1796                 sco_cb.thread_send_sco_running = false;
1797                 sco_cb.data_fd = -1;
1798                 sco_cb.ctrl_fd = -1;
1799                 if (pthread_create(&sco_cb.thread_socket_sco_id, &thread_attr, userial_socket_sco_thread, NULL) != 0) {
1800                     HILOGE("pthread_create : %s", strerror(errno));
1801                 }
1802 
1803                 RtbEmptyQueue(sco_cb.recv_sco_data);
1804                 if (!(sco_cb.voice_settings & 0x0003)) {
1805                     sco_cb.sco_packet_len = SCO_PACKET_LEN_240; // every 5 cvsd packets form a sco pcm data
1806                     sco_cb.msbc_used = false;
1807                 } else {
1808                     sco_cb.sco_packet_len = SCO_PACKET_LEN_60;
1809                     sco_cb.msbc_used = true;
1810                 }
1811                 sco_cb.current_pos = 0;
1812             }
1813 
1814             HILOGE("userial_handle_event sco_handle: %d", sco_cb.sco_handle);
1815         }
1816             break;
1817 
1818         case HCI_DISCONNECTION_COMP_EVT: {
1819             if ((*((uint16_t *)&p_data[P_DATA_3])) == sco_cb.sco_handle) {
1820                 sco_cb.sco_handle = 0;
1821                 sco_cb.msbc_used = false;
1822                 RtbEmptyQueue(sco_cb.recv_sco_data);
1823                 if (sco_cb.thread_sco_running) {
1824                     sco_cb.thread_sco_running = false;
1825                     unsigned char close_signal = 1;
1826                     ssize_t ret;
1827                     RTK_NO_INTR(ret = write(sco_cb.signal_fd[0], &close_signal, 1));
1828                 }
1829             }
1830         }
1831             break;
1832 #endif
1833         default:
1834             break;
1835     }
1836 }
1837 
1838 #ifdef CONFIG_SCO_OVER_HCI
userial_enqueue_recv_sco_data(unsigned char * recv_buffer,int total_length)1839 static void userial_enqueue_recv_sco_data(unsigned char *recv_buffer, int total_length)
1840 {
1841     RTK_UNUSED(total_length);
1842     uint16_t sco_handle;
1843     uint8_t sco_length;
1844     uint8_t *p_data = recv_buffer;
1845     RTK_BUFFER *skb_sco_data;
1846     int i;
1847     sco_handle = *((uint16_t *)p_data);
1848     uint16_t current_pos = sco_cb.current_pos;
1849     uint16_t sco_packet_len = sco_cb.sco_packet_len;
1850 
1851     if (sco_handle == sco_cb.sco_handle) {
1852         sco_length = p_data[SCO_PREAMBLE_SIZE - 1];
1853         p_data += SCO_PREAMBLE_SIZE;
1854 
1855         if (current_pos) {
1856             if ((sco_packet_len - current_pos) <= sco_length) {
1857                 (void)memcpy_s(&sco_cb.enc_data[current_pos], (sco_packet_len - current_pos), p_data,
1858                                (sco_packet_len - current_pos));
1859                 skb_sco_data = RtbAllocate(sco_packet_len, 0);
1860                 (void)memcpy_s(skb_sco_data->Data, sco_packet_len, sco_cb.enc_data, sco_packet_len);
1861                 RtbAddTail(skb_sco_data, sco_packet_len);
1862                 pthread_mutex_lock(&sco_cb.sco_recv_mutex);
1863                 RtbQueueTail(sco_cb.recv_sco_data, skb_sco_data);
1864                 pthread_cond_signal(&sco_cb.sco_recv_cond);
1865                 pthread_mutex_unlock(&sco_cb.sco_recv_mutex);
1866 
1867                 sco_cb.current_pos = 0;
1868                 p_data += (sco_packet_len - current_pos);
1869                 sco_length -= (sco_packet_len - current_pos);
1870             } else {
1871                 (void)memcpy_s(&sco_cb.enc_data[current_pos], sco_length, p_data, sco_length);
1872                 sco_cb.current_pos += sco_length;
1873                 return;
1874             }
1875         }
1876 
1877         // if use cvsd codec
1878         if (!sco_cb.msbc_used) {
1879             for (i = 0; i < (sco_length / sco_packet_len); i++) {
1880                 skb_sco_data = RtbAllocate(sco_packet_len, 0);
1881                 (void)memcpy_s(skb_sco_data->Data, sco_packet_len, p_data + i * sco_packet_len, sco_packet_len);
1882                 RtbAddTail(skb_sco_data, sco_packet_len);
1883                 RtbQueueTail(sco_cb.recv_sco_data, skb_sco_data);
1884             }
1885             if ((sco_length / sco_packet_len)) {
1886                 pthread_mutex_lock(&sco_cb.sco_recv_mutex);
1887                 pthread_cond_signal(&sco_cb.sco_recv_cond);
1888                 pthread_mutex_unlock(&sco_cb.sco_recv_mutex);
1889             }
1890 
1891             i = (sco_length % sco_packet_len);
1892             current_pos = sco_length - i;
1893             if (i) {
1894                 (void)memcpy_s(sco_cb.enc_data, i, p_data + current_pos, i);
1895                 sco_cb.current_pos = i;
1896             }
1897             return;
1898         }
1899 
1900 #define SCO_P_DATA_2 2
1901         // use msbc codec
1902         for (i = 0; i < sco_length; i++) {
1903             if ((p_data[i] == 0x01) && ((p_data[i + 1] & 0x0f) == 0x08) && (p_data[i + SCO_P_DATA_2] == 0xAD)) {
1904                 if ((sco_length - i) < sco_packet_len) {
1905                     (void)memcpy_s(sco_cb.enc_data, (sco_length - i), &p_data[i], (sco_length - i));
1906                     sco_cb.current_pos = sco_length - i;
1907                     return;
1908                 } else {
1909                     (void)memcpy_s(sco_cb.enc_data, sco_packet_len, &p_data[i], sco_packet_len); // complete msbc data
1910                     skb_sco_data = RtbAllocate(sco_packet_len, 0);
1911                     (void)memcpy_s(skb_sco_data->Data, sco_packet_len, sco_cb.enc_data, sco_packet_len);
1912                     RtbAddTail(skb_sco_data, sco_packet_len);
1913                     pthread_mutex_lock(&sco_cb.sco_recv_mutex);
1914                     RtbQueueTail(sco_cb.recv_sco_data, skb_sco_data);
1915                     pthread_cond_signal(&sco_cb.sco_recv_cond);
1916                     pthread_mutex_unlock(&sco_cb.sco_recv_mutex);
1917 
1918                     sco_cb.current_pos = 0;
1919                     i += (sco_packet_len - 1);
1920                 }
1921             }
1922         }
1923     }
1924 }
1925 #endif
1926 
userial_handle_recv_data(unsigned char * recv_buffer,unsigned int total_length)1927 static int userial_handle_recv_data(unsigned char *recv_buffer, unsigned int total_length)
1928 {
1929     serial_data_type_t type = 0;
1930     unsigned char *p_data = recv_buffer;
1931     unsigned int length = total_length;
1932     uint8_t event;
1933 
1934     if (!length) {
1935         HILOGE("%s, length is 0, return immediately", __func__);
1936         return total_length;
1937     }
1938     switch (received_packet_state) {
1939         case RTKBT_PACKET_IDLE:
1940             received_packet_bytes_need = 1;
1941             while (length) {
1942                 type = p_data[0];
1943                 length--;
1944                 p_data++;
1945                 if (type < DATA_TYPE_ACL || type > DATA_TYPE_EVENT) {
1946                     HILOGE("%s invalid data type: %d", __func__, type);
1947                     assert((type > DATA_TYPE_COMMAND) && (type <= DATA_TYPE_EVENT));
1948                     if (!length) {
1949                         return total_length;
1950                     }
1951 
1952                     continue;
1953                 }
1954                 break;
1955             }
1956             recv_packet_current_type = type;
1957             received_packet_state = RTKBT_PACKET_TYPE;
1958             // fall through
1959             __attribute__((fallthrough));
1960 
1961         case RTKBT_PACKET_TYPE:
1962             received_packet_bytes_need = hci_preamble_sizes[HCI_PACKET_TYPE_TO_INDEX(recv_packet_current_type)];
1963             received_resvered_length = 0;
1964             received_packet_state = RTKBT_PACKET_HEADER;
1965             // fall through
1966             __attribute__((fallthrough));
1967 
1968         case RTKBT_PACKET_HEADER:
1969             if (length >= received_packet_bytes_need) {
1970                 (void)memcpy_s(&received_resvered_header[received_resvered_length], received_packet_bytes_need, p_data,
1971                                received_packet_bytes_need);
1972                 received_resvered_length += received_packet_bytes_need;
1973                 length -= received_packet_bytes_need;
1974                 p_data += received_packet_bytes_need;
1975             } else {
1976                 (void)memcpy_s(&received_resvered_header[received_resvered_length], length, p_data, length);
1977                 received_resvered_length += length;
1978                 received_packet_bytes_need -= length;
1979                 length = 0;
1980                 return total_length;
1981             }
1982             received_packet_state = RTKBT_PACKET_CONTENT;
1983 
1984 #define RESVERED_2 2
1985 #define RESVERED_4 4
1986             if (recv_packet_current_type == DATA_TYPE_ACL) {
1987                 received_packet_bytes_need = *(uint16_t *)&received_resvered_header[RESVERED_2];
1988             } else if (recv_packet_current_type == DATA_TYPE_EVENT) {
1989                 received_packet_bytes_need = received_resvered_header[1];
1990             } else {
1991                 received_packet_bytes_need = received_resvered_header[RESVERED_2];
1992             }
1993             // fall through
1994             __attribute__((fallthrough));
1995 
1996         case RTKBT_PACKET_CONTENT:
1997             if (recv_packet_current_type == DATA_TYPE_EVENT) {
1998                 event = received_resvered_header[0];
1999 
2000                 if (event == HCI_COMMAND_COMPLETE_EVT) {
2001                     if (received_resvered_length == RESVERED_2) {
2002                         if (length >= 1) {
2003                             *p_data = 1;
2004                         }
2005                     }
2006                 } else if (event == HCI_COMMAND_STATUS_EVT) {
2007                     if (received_resvered_length < RESVERED_4) {
2008                         unsigned int act_len = RESVERED_4 - received_resvered_length;
2009                         if (length >= act_len) {
2010                             *(p_data + act_len - 1) = 1;
2011                         }
2012                     }
2013                 }
2014             }
2015             if (length >= received_packet_bytes_need) {
2016                 (void)memcpy_s(&received_resvered_header[received_resvered_length], received_packet_bytes_need, p_data,
2017                                received_packet_bytes_need);
2018                 length -= received_packet_bytes_need;
2019                 p_data += received_packet_bytes_need;
2020                 received_resvered_length += received_packet_bytes_need;
2021                 received_packet_bytes_need = 0;
2022             } else {
2023                 (void)memcpy_s(&received_resvered_header[received_resvered_length], length, p_data, length);
2024                 received_resvered_length += length;
2025                 received_packet_bytes_need -= length;
2026                 length = 0;
2027                 return total_length;
2028             }
2029             received_packet_state = RTKBT_PACKET_END;
2030             // fall through
2031             __attribute__((fallthrough));
2032 
2033         case RTKBT_PACKET_END:
2034             switch (recv_packet_current_type) {
2035                 case DATA_TYPE_EVENT:
2036                     userial_handle_event(received_resvered_header, received_resvered_length);
2037                     break;
2038 #ifdef CONFIG_SCO_OVER_HCI
2039                 case DATA_TYPE_SCO:
2040                     userial_enqueue_recv_sco_data(received_resvered_header, received_resvered_length);
2041                     break;
2042 #endif
2043                 default:
2044 
2045                     break;
2046             }
2047             break;
2048 
2049         default:
2050 
2051             break;
2052     }
2053 
2054     received_packet_state = RTKBT_PACKET_IDLE;
2055     received_packet_bytes_need = 0;
2056     recv_packet_current_type = 0;
2057     received_resvered_length = 0;
2058 
2059     return (total_length - length);
2060 }
2061 #endif
2062 
h5_data_ready_cb(serial_data_type_t type,unsigned int total_length)2063 static void h5_data_ready_cb(serial_data_type_t type, unsigned int total_length)
2064 {
2065     unsigned char buffer[1028] = {0};
2066     int length = 0;
2067     length = h5_int_interface->h5_int_read_data(&buffer[1], total_length);
2068     if (length == -1) {
2069         HILOGE("%s, error read length", __func__);
2070         assert(length != -1);
2071     }
2072     buffer[0] = type;
2073     length++;
2074     uint16_t transmitted_length = 0;
2075     unsigned int real_length = length;
2076 #ifdef RTK_HANDLE_EVENT
2077     unsigned int read_length = 0;
2078     do {
2079         read_length += userial_handle_recv_data(buffer + read_length, real_length - read_length);
2080     } while (vnd_userial.thread_running && read_length < total_length);
2081 #endif
2082 
2083     while (length > 0) {
2084         ssize_t ret;
2085         RTK_NO_INTR(ret = write(vnd_userial.uart_fd[1], buffer + transmitted_length, length));
2086         switch (ret) {
2087             case -1:
2088                 HILOGE("In %s, error writing to the uart serial port: %s", __func__, strerror(errno));
2089                 __attribute__((fallthrough));
2090             case 0:
2091                 // If we wrote nothing, don't loop more because we
2092                 // can't go to infinity or beyond
2093                 if (real_length) {
2094                     userial_enqueue_coex_rawdata(buffer, real_length, true);
2095                 }
2096                 return;
2097             default:
2098                 transmitted_length += ret;
2099                 length -= ret;
2100                 break;
2101         }
2102     }
2103     if (real_length) {
2104         userial_enqueue_coex_rawdata(buffer, real_length, true);
2105     }
2106     return;
2107 }
2108 
2109 // This recv data from driver which is sent or recv by the controller. The data type have ACL/SCO/EVENT
2110 //  direction CONTROLLER -----> BT HOST
userial_recv_uart_rawdata(unsigned char * buffer,unsigned int total_length)2111 static void userial_recv_uart_rawdata(unsigned char *buffer, unsigned int total_length)
2112 {
2113     unsigned int length = total_length;
2114     uint16_t transmitted_length = 0;
2115 #ifdef RTK_HANDLE_EVENT
2116     unsigned int read_length = 0;
2117     do {
2118         read_length += userial_handle_recv_data(buffer + read_length, total_length - read_length);
2119     } while (read_length < total_length);
2120 #endif
2121     while (length > 0 && vnd_userial.thread_running) {
2122         ssize_t ret;
2123         RTK_NO_INTR(ret = write(vnd_userial.uart_fd[1], buffer + transmitted_length, length));
2124         switch (ret) {
2125             case -1:
2126                 HILOGE("In %s, error writing to the uart serial port: %s", __func__, strerror(errno));
2127                 goto done;
2128             case 0:
2129                 // If we wrote nothing, don't loop more because we
2130                 // can't go to infinity or beyond
2131                 goto done;
2132             default:
2133                 transmitted_length += ret;
2134                 length -= ret;
2135                 break;
2136         }
2137     }
2138 done:;
2139     if (total_length) {
2140         userial_enqueue_coex_rawdata(buffer, total_length, true);
2141     }
2142     return;
2143 }
2144 
userial_recv_rawdata_hook(unsigned char * buffer,unsigned int total_length)2145 void userial_recv_rawdata_hook(unsigned char *buffer, unsigned int total_length)
2146 {
2147     uint16_t transmitted_length = 0;
2148     unsigned int real_length = total_length;
2149 
2150     while (vnd_userial.thread_running && (total_length > 0)) {
2151         ssize_t ret;
2152         RTK_NO_INTR(ret = write(vnd_userial.uart_fd[1], buffer + transmitted_length, total_length));
2153         switch (ret) {
2154             case -1:
2155                 HILOGE("In %s, error writing to the uart serial port: %s", __func__, strerror(errno));
2156                 goto done;
2157             case 0:
2158                 // If we wrote nothing, don't loop more because we
2159                 // can't go to infinity or beyond
2160                 goto done;
2161             default:
2162                 transmitted_length += ret;
2163                 total_length -= ret;
2164                 break;
2165         }
2166     }
2167 done:;
2168     if (real_length && vnd_userial.thread_running) {
2169         userial_enqueue_coex_rawdata(buffer, real_length, true);
2170     }
2171     return;
2172 }
2173 
userial_recv_socket_thread(void * arg)2174 static void *userial_recv_socket_thread(void *arg)
2175 {
2176     RTK_UNUSED(arg);
2177     struct epoll_event events[64];
2178     int j;
2179 #define RET_32 32
2180 #define RECV_RET_500 500
2181     while (vnd_userial.thread_running) {
2182         int ret;
2183         do {
2184             ret = epoll_wait(vnd_userial.epoll_fd, events, RET_32, RECV_RET_500);
2185         } while (vnd_userial.thread_running && ret == -1 && errno == EINTR);
2186 
2187         if (ret == -1) {
2188             HILOGE("%s error in epoll_wait: %s", __func__, strerror(errno));
2189         }
2190         for (j = 0; j < ret; ++j) {
2191             struct rtk_object_t *object = (struct rtk_object_t *)events[j].data.ptr;
2192             if (events[j].data.ptr == NULL) {
2193                 continue;
2194             } else {
2195                 if ((events[j].events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR)) && object->read_ready) {
2196                     object->read_ready(object->context);
2197                 }
2198                 if ((events[j].events & EPOLLOUT) && object->write_ready) {
2199                     object->write_ready(object->context);
2200                 }
2201             }
2202         }
2203     }
2204     vnd_userial.thread_socket_id = (pthread_t)-1;
2205     HILOGD("%s exit", __func__);
2206     return NULL;
2207 }
2208 
userial_recv_uart_thread(void * arg)2209 static void *userial_recv_uart_thread(void *arg)
2210 {
2211     RTK_UNUSED(arg);
2212     struct pollfd pfd[2];
2213     pfd[0].events = POLLIN | POLLHUP | POLLERR | POLLRDHUP;
2214     pfd[0].fd = vnd_userial.signal_fd[1];
2215     pfd[1].events = POLLIN | POLLHUP | POLLERR | POLLRDHUP;
2216     pfd[1].fd = vnd_userial.fd;
2217     int ret;
2218     unsigned char read_buffer[2056] = {0};
2219     ssize_t bytes_read;
2220     char rtkbt_transtype_recv_uart_thread = get_rtkbt_transtype();
2221 #define RET_2 2
2222 #define RET_500 500
2223     while (vnd_userial.thread_running) {
2224         do {
2225             ret = poll(pfd, RET_2, RET_500);
2226         } while (ret == -1 && errno == EINTR && vnd_userial.thread_running);
2227 
2228         // exit signal is always at first index
2229         if (pfd[0].revents && !vnd_userial.thread_running) {
2230             HILOGE("receive exit signal and stop thread ");
2231             return NULL;
2232         }
2233 
2234         if (pfd[1].revents & POLLIN) {
2235             RTK_NO_INTR(bytes_read = read(vnd_userial.fd, read_buffer, sizeof(read_buffer)));
2236             if (!bytes_read) {
2237                 continue;
2238             }
2239             if (bytes_read < 0) {
2240                 HILOGE("%s, read fail, error : %s", __func__, strerror(errno));
2241                 continue;
2242             }
2243 
2244             if (rtkbt_transtype_recv_uart_thread & RTKBT_TRANS_H5) {
2245                 h5_int_interface->h5_recv_msg(read_buffer, bytes_read);
2246             } else {
2247                 userial_recv_uart_rawdata(read_buffer, bytes_read);
2248             }
2249         }
2250 
2251         if (pfd[1].revents & (POLLERR | POLLHUP)) {
2252             HILOGE("%s poll error, fd : %d", __func__, vnd_userial.fd);
2253             vnd_userial.btdriver_state = false;
2254             close(vnd_userial.fd);
2255             return NULL;
2256         }
2257         if (ret < 0) {
2258             HILOGE("%s : error (%d)", __func__, ret);
2259             continue;
2260         }
2261     }
2262     vnd_userial.thread_uart_id = (pthread_t)-1;
2263     HILOGD("%s exit", __func__);
2264     return NULL;
2265 }
2266 
userial_coex_thread(void * arg)2267 static void *userial_coex_thread(void *arg)
2268 {
2269     RTK_UNUSED(arg);
2270     struct epoll_event events[64];
2271     int j;
2272     while (vnd_userial.thread_running) {
2273         int ret;
2274 #define RET_64 64
2275 #define RET_500 500
2276         do {
2277             ret = epoll_wait(vnd_userial.cpoll_fd, events, RET_64, RET_500);
2278         } while (ret == -1 && errno == EINTR && vnd_userial.thread_running);
2279         if (ret == -1) {
2280             HILOGE("%s error in epoll_wait: %s", __func__, strerror(errno));
2281         }
2282         for (j = 0; j < ret; ++j) {
2283             struct rtk_object_t *object = (struct rtk_object_t *)events[j].data.ptr;
2284             if (events[j].data.ptr == NULL) {
2285                 continue;
2286             } else {
2287                 if ((events[j].events & (EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR)) && object->read_ready) {
2288                     object->read_ready(object->context);
2289                 }
2290                 if ((events[j].events & EPOLLOUT) && object->write_ready) {
2291                     object->write_ready(object->context);
2292                 }
2293             }
2294         }
2295     }
2296     vnd_userial.thread_coex_id = (pthread_t)-1;
2297     HILOGD("%s exit", __func__);
2298     return NULL;
2299 }
2300 
userial_socket_open(void)2301 int userial_socket_open(void)
2302 {
2303     int ret = 0;
2304     struct epoll_event event;
2305     if ((ret = socketpair(AF_UNIX, SOCK_STREAM, 0, vnd_userial.uart_fd)) < 0) {
2306         HILOGE("%s, errno : %s", __func__, strerror(errno));
2307         return ret;
2308     }
2309 
2310     if ((ret = socketpair(AF_UNIX, SOCK_STREAM, 0, vnd_userial.signal_fd)) < 0) {
2311         HILOGE("%s, errno : %s", __func__, strerror(errno));
2312         return ret;
2313     }
2314 #define EPOLL_64 64
2315 
2316     vnd_userial.epoll_fd = epoll_create(EPOLL_64);
2317     if (vnd_userial.epoll_fd == -1) {
2318         HILOGE("%s unable to create epoll instance: %s", __func__, strerror(errno));
2319         return -1;
2320     }
2321 
2322     rtk_socket_object.fd = vnd_userial.uart_fd[1];
2323     rtk_socket_object.read_ready = userial_recv_H4_rawdata;
2324     (void)memset_s(&event, sizeof(event), 0, sizeof(event));
2325     event.events |= EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR;
2326     event.data.ptr = (void *)&rtk_socket_object;
2327     if (epoll_ctl(vnd_userial.epoll_fd, EPOLL_CTL_ADD, vnd_userial.uart_fd[1], &event) == -1) {
2328         HILOGE("%s unable to register fd %d to epoll set: %s", __func__, vnd_userial.uart_fd[1], strerror(errno));
2329         close(vnd_userial.epoll_fd);
2330         vnd_userial.epoll_fd = -1;
2331         return -1;
2332     }
2333 
2334     event.data.ptr = NULL;
2335     if (epoll_ctl(vnd_userial.epoll_fd, EPOLL_CTL_ADD, vnd_userial.signal_fd[1], &event) == -1) {
2336         HILOGE("%s unable to register signal fd %d to epoll set: %s", __func__, vnd_userial.signal_fd[1],
2337                strerror(errno));
2338         close(vnd_userial.epoll_fd);
2339         vnd_userial.epoll_fd = -1;
2340         return -1;
2341     }
2342     pthread_attr_t thread_attr;
2343     pthread_attr_init(&thread_attr);
2344     pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);
2345     vnd_userial.thread_running = true;
2346     if (pthread_create(&vnd_userial.thread_socket_id, &thread_attr, userial_recv_socket_thread, NULL) != 0) {
2347         HILOGE("pthread_create : %s", strerror(errno));
2348         close(vnd_userial.epoll_fd);
2349         vnd_userial.epoll_fd = -1;
2350         vnd_userial.thread_socket_id = (pthread_t)-1;
2351         return -1;
2352     }
2353 
2354     if (pthread_create(&vnd_userial.thread_uart_id, &thread_attr, userial_recv_uart_thread, NULL) != 0) {
2355         HILOGE("pthread_create : %s", strerror(errno));
2356         close(vnd_userial.epoll_fd);
2357         vnd_userial.thread_running = false;
2358         pthread_join(vnd_userial.thread_socket_id, NULL);
2359         vnd_userial.thread_socket_id = (pthread_t)-1;
2360         return -1;
2361     }
2362 
2363     vnd_userial.cpoll_fd = epoll_create(EPOLL_64);
2364     assert(vnd_userial.cpoll_fd != -1);
2365 
2366 #define EVENTFD_10 10
2367     vnd_userial.event_fd = eventfd(EVENTFD_10, EFD_NONBLOCK);
2368     assert(vnd_userial.event_fd != -1);
2369     if (vnd_userial.event_fd != -1) {
2370         rtk_coex_object.fd = vnd_userial.event_fd;
2371         rtk_coex_object.read_ready = userial_coex_handler;
2372         (void)memset_s(&event, sizeof(event), 0, sizeof(event));
2373         event.events |= EPOLLIN | EPOLLHUP | EPOLLRDHUP | EPOLLERR;
2374         event.data.ptr = (void *)&rtk_coex_object;
2375         if (epoll_ctl(vnd_userial.cpoll_fd, EPOLL_CTL_ADD, vnd_userial.event_fd, &event) == -1) {
2376             HILOGE("%s unable to register fd %d to cpoll set: %s", __func__, vnd_userial.event_fd, strerror(errno));
2377             assert(false);
2378         }
2379 
2380         event.data.ptr = NULL;
2381         if (epoll_ctl(vnd_userial.cpoll_fd, EPOLL_CTL_ADD, vnd_userial.signal_fd[1], &event) == -1) {
2382             HILOGE("%s unable to register fd %d to cpoll set: %s", __func__, vnd_userial.signal_fd[1], strerror(errno));
2383             assert(false);
2384         }
2385 
2386         if (pthread_create(&vnd_userial.thread_coex_id, &thread_attr, userial_coex_thread, NULL) != 0) {
2387             HILOGE("pthread create  coex : %s", strerror(errno));
2388             vnd_userial.thread_coex_id = (pthread_t)-1;
2389             assert(false);
2390         }
2391     }
2392 
2393     ret = vnd_userial.uart_fd[0];
2394     return ret;
2395 }
2396 
userial_vendor_usb_ioctl(int operation,void * param)2397 int userial_vendor_usb_ioctl(int operation, void *param)
2398 {
2399     int retval;
2400     retval = ioctl(vnd_userial.fd, operation, param);
2401     if (retval == -1) {
2402         HILOGE("%s: error: %d : %s", __func__, errno, strerror(errno));
2403     }
2404     return retval;
2405 }
2406 
userial_vendor_usb_open(void)2407 int userial_vendor_usb_open(void)
2408 {
2409     if ((vnd_userial.fd = open(vnd_userial.port_name, O_RDWR)) == -1) {
2410         HILOGE("%s: unable to open %s: %s", __func__, vnd_userial.port_name, strerror(errno));
2411         return -1;
2412     }
2413 
2414     vnd_userial.btdriver_state = true;
2415     HILOGI("device fd = %d open", vnd_userial.fd);
2416 
2417     return vnd_userial.fd;
2418 }
2419