3333#include <util/math.h>
3434#include "ipc_queue.h"
3535
36- //#define DEBUG 1
36+ // DEBUG: 1 print as hex string, 2 print as binary, others no printing
37+ #define DEBUG 0
3738
3839static TaskHandle_t s_rx_task_handle ;
3940static SemaphoreHandle_t s_rx_data_ready ;
@@ -45,7 +46,86 @@ struct mbox_env_tag
4546};
4647struct mbox_env_tag mbox_env ;
4748
48-
49+
50+ #define H4TL_PACKET_HOST 0x61
51+ #define H4TL_PACKET_CTRL 0x62
52+
53+ static char * hci_type_str (int type )
54+ {
55+ char * r ;
56+ switch (type ) {
57+ case 1 :
58+ r = "CMD" ;
59+ break ;
60+ case 2 :
61+ r = "ACL" ;
62+ break ;
63+ case 3 :
64+ r = "SCO" ;
65+ break ;
66+ case 4 :
67+ r = "EVT" ;
68+ break ;
69+ case 5 :
70+ r = "ISO" ;
71+ break ;
72+ default :
73+ r = "UKN" ;
74+ }
75+ return r ;
76+ }
77+
78+ /**
79+ * @brief Send HCI data to debug console.
80+ * @param[in] type HCI type in H4 format
81+ * @param[in] data HCI traffic data
82+ * @param[in] len length of data in bytes.
83+ * @param[in] host2ctrl 1: host to control 0: contrl to host
84+ * @retval None
85+ */
86+ #if DEBUG > 0
87+ void hci_log_to_console (uint8_t type , uint8_t * data , uint16_t len , int host2ctrl )
88+ {
89+ static uint8_t temp [1024 ], * trace ;
90+ static uint16_t seq ;
91+ trace = & temp [0 ];
92+ // Magic for Pebble HCI, 'PBTS'
93+ * trace ++ = 0x50 ;
94+ * trace ++ = 0x42 ;
95+ * trace ++ = 0x54 ;
96+ * trace ++ = 0x53 ;
97+ * trace ++ = 0x06 ;
98+ * trace ++ = 0x01 ;
99+ * trace ++ = (len + 8 ) & 0xff ;
100+ * trace ++ = (len + 8 ) >> 8 ;
101+ * trace ++ = seq & 0xff ;
102+ * trace ++ = seq >> 8 ;
103+ * trace ++ = 0 ;
104+ * trace ++ = 0 ;
105+ * trace ++ = 0 ;
106+ * trace ++ = 0 ;
107+ if (host2ctrl )
108+ * trace ++ = H4TL_PACKET_HOST ;
109+ else
110+ * trace ++ = H4TL_PACKET_CTRL ;
111+ * trace ++ = type ;
112+ seq ++ ;
113+ memcpy (trace , data , len );
114+ #if DEBUG == 1
115+ portDISABLE_INTERRUPTS ();
116+ HAL_DBG_printf ("%s, %s %d\n" , hci_type_str (type ), host2ctrl ?"TX" :"RX" , len );
117+ HAL_DBG_print_data ((char * )data ,0 ,len );
118+ portENABLE_INTERRUPTS ();
119+ #else
120+ len += 16 ;
121+ for (int i = 0 ;i < len ;i ++ )
122+ dbgserial_putchar (temp [i ]);
123+ #endif
124+ }
125+ #else
126+ #define hci_log_to_console (type ,data ,len ,host2ctrl )
127+ #endif
128+
49129static uint8_t read_buf [64 ];
50130static void prv_rx_task_main (void * unused ) {
51131 int consumed_bytes ;
@@ -133,24 +213,18 @@ hci_uart_frame_cb(uint8_t pkt_type, void *data)
133213 case HCI_H4_EVT :
134214 {
135215 struct ble_hci_ev * ev = data ;
136- struct ble_hci_ev_command_complete * cmd_complete = (void * ) ev -> data ;
137- #if DEBUG
138- HAL_DBG_print_data (data , 0 , ev -> length + sizeof (* ev ));
139- PBL_LOG (LOG_LEVEL_INFO , "Event %x" , ev -> opcode );
140- #endif
216+ struct ble_hci_ev_command_complete * cmd_complete = (void * ) ev -> data ;
141217 if (ev -> opcode == BLE_HCI_EVCODE_COMMAND_COMPLETE )
142218 PBL_LOG (LOG_LEVEL_INFO , "\tCMD complete %x" , cmd_complete -> opcode );
143219
144220 if (ev -> opcode == BLE_HCI_EVCODE_COMMAND_COMPLETE && cmd_complete -> opcode == 0xFC11 )
145221 break ;
222+ hci_log_to_console (4 , data , ev -> length + sizeof (* ev ),0 );
146223 }
147224 return ble_transport_to_hs_evt (data );
148225 case HCI_H4_ACL :
149226 struct os_mbuf * om = (struct os_mbuf * )data ;
150- PBL_LOG (LOG_LEVEL_INFO , "ACL IND %x" , OS_MBUF_PKTLEN (om ));
151- #if DEBUG
152- HAL_DBG_print_data ((char * )data , 0 , OS_MBUF_PKTLEN (om ));
153- #endif
227+ hci_log_to_console (4 , OS_MBUF_DATA (om , uint8_t * ), OS_MBUF_PKTLEN (om ),0 );
154228 return ble_transport_to_hs_acl (data );
155229 default :
156230 assert (0 );
@@ -159,7 +233,7 @@ hci_uart_frame_cb(uint8_t pkt_type, void *data)
159233
160234 return -1 ;
161235}
162- #if DEBUG
236+ #if DEBUG > 0
163237void HAL_DBG_printf (const char * fmt , ...)
164238{
165239 va_list args ;
@@ -208,9 +282,7 @@ int ble_transport_to_ll_cmd_impl(void *buf) {
208282 hci_cmd [0 ]= 1 ;
209283 memcpy (& (hci_cmd [1 ]), buf , 3 + cmd -> length );
210284 PBL_LOG (LOG_LEVEL_INFO , "BLE TX CMD %x, len=%d" , cmd -> opcode , 3 + cmd -> length + 1 );
211- #if DEBUG
212- HAL_DBG_print_data ((char * )hci_cmd , 0 , 3 + cmd -> length + 1 );
213- #endif
285+ hci_log_to_console (1 ,buf ,3 + cmd -> length ,1 );
214286 int written = ipc_queue_write (mbox_env .ipc_port , hci_cmd , 3 + cmd -> length + 1 , 10 );
215287 ble_transport_free (buf );
216288 return (written >=0 )?0 :-1 ;
@@ -222,9 +294,7 @@ int ble_transport_to_ll_acl_impl(struct os_mbuf *om) {
222294 PBL_LOG (LOG_LEVEL_INFO , "BLE TX ACL len=%d" , OS_MBUF_PKTLEN (om ));
223295 hci_acl [0 ]= 2 ;
224296 memcpy (& (hci_acl [1 ]), data , OS_MBUF_PKTLEN (om ));
225- #if DEBUG
226- HAL_DBG_print_data ((char * )hci_acl , 0 , OS_MBUF_PKTLEN (om )+ 1 );
227- #endif
297+ hci_log_to_console (2 ,data ,OS_MBUF_PKTLEN (om ),1 );
228298 int written = ipc_queue_write (mbox_env .ipc_port , hci_acl , OS_MBUF_PKTLEN (om )+ 1 , 10 );
229299 os_mbuf_free (om );
230300 return (written >=0 )?0 :-1 ;
@@ -233,5 +303,6 @@ int ble_transport_to_ll_acl_impl(struct os_mbuf *om) {
233303int ble_transport_to_ll_iso_impl (struct os_mbuf * om ) {
234304 uint8_t * data = OS_MBUF_DATA (om , uint8_t * );
235305 int written = ipc_queue_write (mbox_env .ipc_port , data , OS_MBUF_PKTLEN (om ), 10 );
306+ os_mbuf_free (om );
236307 return (written >=0 )?0 :-1 ;
237308}
0 commit comments