• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3  * All rights reserved.
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 #pragma once
19 
20 #include "stack/ble/ble_common.h"
21 
22 typedef int (*blc_hci_rx_handler_t)(void);
23 typedef int (*blc_hci_tx_handler_t)(void);
24 
25 #define HCI_FLAG_EVENT_PHYTEST_2_WIRE_UART (1 << 23)
26 #define HCI_FLAG_EVENT_TLK_MODULE          (1 << 24)
27 #define HCI_FLAG_EVENT_BT_STD              (1 << 25)  // HCI event
28 #define HCI_FLAG_EVENT_STACK               (1 << 26)  // not used now
29 #define HCI_FLAG_ACL_BT_STD                (1 << 27)
30 #define HCI_FLAG_BT_HCI_CMD                (1 << 28)  // HCI command
31 #define HCI_FLAG_ISO_DATE_STD              (1 << 29)
32 
33 #define TLK_MODULE_EVENT_STATE_CHANGE   0x0730
34 #define TLK_MODULE_EVENT_DATA_RECEIVED  0x0731
35 #define TLK_MODULE_EVENT_DATA_SEND      0x0732
36 #define TLK_MODULE_EVENT_BUFF_AVAILABLE 0x0733
37 
38 #define HCI_MAX_ACL_DATA_LEN 27
39 
40 #define HCI_MAX_DATA_BUFFERS_SALVE  8
41 #define HCI_MAX_DATA_BUFFERS_MASTER 8
42 
43 #define HCI_ADV_REPORT_EVT_RSVD_FIFO 3
44 
45 extern blc_hci_rx_handler_t blc_hci_rx_handler;
46 extern blc_hci_tx_handler_t blc_hci_tx_handler;
47 
48 extern my_fifo_t hci_tx_iso_fifo;
49 
50 typedef struct {
51     u32 size;
52     u8 num;
53     u8 mask;
54     u8 wptr;
55     u8 rptr;
56     u8 *p;
57 } hci_fifo_t;
58 
59 /**
60  *  @brief  Definition for HCI packet type & HCI packet indicator
61  */
62 typedef enum {
63     HCI_TYPE_CMD = 0x01,
64     HCI_TYPE_ACL_DATA = 0x02,
65     HCI_TYPE_SCO_DATA = 0x03,
66     HCI_TYPE_EVENT = 0x04,
67     HCI_TYPE_ISO_DATA = 0x05,  // core_5.2
68 } hci_type_t;
69 
70 /**
71  *  @brief  Definition for HCI ACL Data packets Packet_Boundary_Flag
72  */
73 typedef enum {
74     HCI_FIRST_NAF_PACKET = 0x00,   // LE Host to Controller
75     HCI_CONTINUING_PACKET = 0x01,  // LE Host to Controller / Controller to Host
76     HCI_FIRST_AF_PACKET = 0x02,    // LE 					  Controller to Host
77 } acl_pb_flag_t;
78 
79 /**
80  *  @brief  Definition for HCI ISO Data packets PB_Flag
81  */
82 typedef enum {
83     HCI_ISO_SDU_FIRST_FRAG = 0x00,     // The ISO_Data_Load field contains the first fragment of a fragmented SDU
84     HCI_ISO_SDU_CONTINUE_FRAG = 0x01,  // The ISO_Data_Load field contains a continuation fragment of an SDU
85     HCI_ISO_SDU_COMPLETE = 0x02,       // The ISO_Data_Load field contains a complete SDU
86     HCI_ISO_SDU_LAST_FRAG = 0x03,      // The ISO_Data_Load field contains the last fragment of an SDU.
87 } iso_pb_flag_t;
88 
89 /**
90  *  @brief  Definition for HCI ISO Data packets Packet_Status_Flag
91  */
92 typedef enum {
93     HCI_ISO_VALID_DATA = 0x00,             // Valid data. The complete ISO_SDU was received correctly
94     HCI_ISO_POSSIBLE_INVALID_DATA = 0x01,  // Possibly invalid data
95     HCI_ISO_LOST_DATA = 0x02,  // Part(s) of the ISO_SDU were not received correctly. This is reported as "lost data"
96 } iso_ps_flag_t;
97 
98 // Controller event handler
99 typedef int (*hci_event_handler_t)(u32 h, u8 *para, int n);
100 
101 // Controller data handler
102 typedef int (*hci_data_handler_t)(u16 conn, u8 *p);
103 
104 // hci event
105 extern u32 hci_eventMask;
106 extern u32 hci_le_eventMask;
107 extern u32 hci_le_eventMask_2;
108 extern hci_event_handler_t blc_hci_event_handler;
109 extern hci_data_handler_t blc_hci_data_handler;
110 extern hci_fifo_t bltHci_rxfifo;
111 extern hci_fifo_t bltHci_txfifo;
112 
113 /**
114  * @brief      for user to initialize HCI TX FIFO.
115  * @param[in]  pRxbuf - TX FIFO buffer address.
116  * @param[in]  fifo_size - RX FIFO size
117  * @param[in]  fifo_number - RX FIFO number, can only be 4, 8, 16 or 32
118  * @return     status, 0x00:  succeed
119  * 					   other: failed
120  */
121 ble_sts_t blc_ll_initHciTxFifo(u8 *pTxbuf, int fifo_size, int fifo_number);
122 
123 /**
124  * @brief      for user to initialize HCI RX FIFO.
125  * @param[in]  pRxbuf - RX FIFO buffer address.
126  * @param[in]  fifo_size - RX FIFO size
127  * @param[in]  fifo_number - RX FIFO number, can only be 4, 8, 16 or 32
128  * @return     status, 0x00:  succeed
129  * 					   other: failed
130  */
131 ble_sts_t blc_ll_initHciRxFifo(u8 *pRxbuf, int fifo_size, int fifo_number);
132 
133 /**
134  * @brief      for user to initialize HCI RX ACL Data FIFO.
135  * @param[in]  pRxbuf - RX FIFO buffer address (Attention: buffer size = fifo_size*fifo_num*conn_max_num).
136  * @param[in]  fifo_size - RX FIFO size
137  * @param[in]  fifo_number - RX FIFO number, can only be 4, 8, 16 or 32
138  * @return     status, 0x00:  succeed
139  * 					   other: failed
140  */
141 ble_sts_t blc_ll_initHciAclDataFifo(u8 *pAclbuf, int fifo_size, int fifo_number);
142 
143 /**
144  * @brief      this function is used to get data by USB in RX mode for HCI Layer
145  * @param[in]  none.
146  * @return     0
147  */
148 int blc_hci_rx_from_usb(void);
149 
150 /**
151  * @brief      this function is used to send data by USB in TX mode for HCI Layer
152  * @param[in]  none.
153  * @return     0
154  */
155 int blc_hci_tx_to_usb(void);
156 
157 /**
158  * @brief      this function is used to process HCI data
159  * @param[in]  *p - the pointer of HCI data
160  * @param[in]  n - the length of HCI data
161  * @return     0
162  */
163 int blc_hci_handler(u8 *p, int n);
164 
165 /**
166  * @brief      this function is used to report HCI events
167  * @param[in]  h - HCI Event type
168  * @param[in]  *para - data pointer of event
169  * @param[in]  n - data length of event
170  * @return     none
171  */
172 int blc_hci_send_event(u32 h, u8 *para, int n);
173 
174 /**
175  * @brief      this function is used to process HCI events
176  * @param[in]  none.
177  * @return     0
178  */
179 int blc_hci_proc(void);
180 
181 /**
182  * @brief      this function is used to set HCI EVENT mask
183  * @param[in]  evtMask  -  HCI EVENT mask
184  * @return     0
185  */
186 ble_sts_t blc_hci_setEventMask_cmd(u32 evtMask);  // eventMask: BT/EDR
187 
188 /**
189  * @brief      this function is used to set HCI LE EVENT mask
190  * @param[in]  evtMask  -  HCI LE EVENT mask(BIT<0-31>)
191  * @return     0
192  */
193 ble_sts_t blc_hci_le_setEventMask_cmd(u32 evtMask);  // eventMask: LE event  0~31
194 
195 /**
196  * @brief      this function is used to set HCI LE EVENT mask
197  * @param[in]  evtMask  -  HCI LE EVENT mask(BIT<32-63>)
198  * @return     0
199  */
200 ble_sts_t blc_hci_le_setEventMask_2_cmd(u32 evtMask_2);  // eventMask: LE event 32~63
201 
202 /**
203  * @brief      This function is used to register the controller event processing callback
204  * @param[in]  handler - hci_event_handler_t
205  * @return     none.
206  */
207 void blc_hci_registerControllerEventHandler(hci_event_handler_t handler);
208 
209 /**
210  * @brief      This function is used to register ACL data transmission to Host for processing callback function.
211  * @param[in]  handler - hci_data_handler_t
212  * @return     none.
213  */
214 void blc_hci_registerControllerDataHandler(hci_data_handler_t handle);
215 
216 /**
217  * @brief      this function is used to register HCI TX or RX handler callback function
218  * @param[in]  *prx - blc_hci_rx_handler
219  * @param[in]  *ptx - blc_hci_tx_handler
220  * @return     none.
221  */
222 void blc_register_hci_handler(void *prx, void *ptx);
223 
224 /**
225  * @brief      this function is used to send ACL data to HOST
226  * @param[in]  handle - connect handle
227  * @param[in]  *p - the pointer of l2cap data
228  * @return     0
229  */
230 int blc_hci_sendACLData2Host(u16 handle, u8 *p);
231 
232 int blc_hci_getFreeTxFIFONum(void);
233 
234 /**
235  * @brief      this function is used to send data
236  * @param[in]  h - HCI Event type
237  * @param[in]  *para - data pointer of event
238  * @param[in]  n - data length of event
239  * @return     0,-1
240  */
241 int blc_hci_send_data(u32 h, u8 *para, int n);
242 
243 /**
244  * @brief      This function is used to send ISO data from Controller to HOST.
245  * @param[in]  h - HCI Event type
246  * @param[in]  *iso_load - data pointer of the ISO load
247  * @param[in]  data_load_len - data length of load
248  * @return     0,-1
249  */
250 int blc_hci_iso_send_data(u32 h, u8 *iso_load, int data_load_len);
251