1 /*
2  * Copyright (C) 2015-2021 Alibaba Group Holding Limited
3  */
4 
5 #ifndef __BT_VENDOR_DRV_H_
6 #define __BT_VENDOR_DRV_H_
7 
8 #include <stdio.h>
9 #include <stdint.h>
10 #include "osif_customer.h"
11 #include "hci_if.h"
12 #include "hci_tp.h"
13 #include "wifi_conf.h"
14 
15 bool bt_vendor_open_flag = false;
16 void *bt_vendor_open_sem = NULL;
17 void *bt_vendor_tx_sem = NULL;
18 
19 unsigned int sleep(unsigned int seconds);
20 extern void hci_if_del_task(void);
21 extern void hci_if_deinit(void);
22 
bt_vendor_init_callback(T_HCI_IF_EVT evt,bool status,uint8_t * p_buf,uint32_t len)23 static bool bt_vendor_init_callback(T_HCI_IF_EVT evt, bool status, uint8_t *p_buf, uint32_t len)
24 {
25     if (evt == HCI_IF_EVT_OPENED) {
26         if (status == true) {
27             printf("hci_if_open success\r\n");
28         } else {
29             printf("hci_if_open fail\r\n");
30         }
31 
32         bt_vendor_open_flag = status;
33         os_sem_give(bt_vendor_open_sem);
34     } else {
35         printf("Other event enter bt_vendor_init_callback! evt = 0x%x, status = 0x%x\r\n", evt, status);
36     }
37 }
38 
bt_vendor_tx_callback(void)39 static bool bt_vendor_tx_callback(void)
40 {
41     os_sem_give(bt_vendor_tx_sem);
42 }
43 
44 /**
45 * 初始化BT驱动、加载patch、调节Coex,其他厂家IC初始化的操作
46 *
47 * @return  操作状态,0代表成功,其他值表示失败
48 */
bt_vendor_drv_bring_up(void)49 int bt_vendor_drv_bring_up(void)
50 {
51     int ret = 0;
52 
53 	if(bt_vendor_open_flag == true) {
54     	printf("no need to bt_vendor_drv_bring_up again!\r\n");
55         return 1;
56 	}
57 
58     if (os_sem_create(&bt_vendor_open_sem, 0, 1) != true) {
59         printf("Init bt_vendor_open_sem error!\r\n");
60         return 1;
61     }
62 
63     wifi_on(1);
64     sleep(1);
65 
66     hci_if_open(bt_vendor_init_callback);
67 
68     if (os_sem_take(bt_vendor_open_sem, 0xFFFFFFFF) == true) {
69         if (bt_vendor_open_flag == true) {
70             if (os_sem_create(&bt_vendor_tx_sem, 1, 1) != true) {
71                 printf("Init bt_vendor_tx_sem error!\r\n");
72 				ret = 1;
73             }
74         } else {
75             ret = 1;
76         }
77     } else {
78         printf("Take bt_vendor_open_sem error!\r\n");
79 		ret = 1;
80     }
81 
82     os_sem_delete(bt_vendor_open_sem);
83 	hci_if_del_task();
84 	hci_if_deinit();
85     return ret;
86 }
87 /**
88 * 设置RX事件回调,BT Driver收到EVENT/ACL数据后,通过此接口通知BT Stack进行读取操作
89 * @param[in]  ready_to_rx  回调函数
90 * @return     操作状态,0代表成功,其他值表示失败
91 */
bt_vendor_drv_set_rx_ind(void (* ready_to_rx)(void))92 int bt_vendor_drv_set_rx_ind(void (*ready_to_rx)(void))
93 {
94 	if (bt_vendor_open_flag == true) {
95     	hci_tp_set_rx_ind(ready_to_rx);
96     } else {
97         printf("must call after bt_vendor_drv_bring_up\r\n");
98     }
99 }
100 
101 /**
102 * 从BT Controller读取EVENT/ACL数据
103 * @param[out]  data 数据保存的起始内存地址
104 * @param[in]   len  期望读取的数据长度
105 * @return      实际读取的数据长度
106 */
bt_vendor_drv_rx(uint8_t * data,size_t len)107 size_t bt_vendor_drv_rx(uint8_t *data, size_t len)
108 {
109 	return hci_tp_recv(data, len);
110 }
111 /**
112 * BT Stack通过此接口往Controller发送数据
113 * @param[in]  data    数据的起始内存地址
114 * @param[in]  len     期望发送的数据长度
115 * @param[in]  reserve NULL
116 * @return             实际发送的数据长度
117 */
118 
bt_vendor_drv_tx(uint8_t * data,size_t len,void * reserve)119 size_t bt_vendor_drv_tx(uint8_t *data, size_t len, void *reserve)
120 {
121 	os_sem_take(bt_vendor_tx_sem, 0xFFFFFFFF);
122 	hci_tp_send(data, len, bt_vendor_tx_callback);
123 	return len;
124 }
125 #endif