1 /*
2  * Copyright (C) 2016 YunOS Project. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <bt_errno.h>
17 #include <stddef.h>
18 
19 #include <ble_os.h>
20 
21 #define BT_DBG_ENABLED 0
22 
23 #include <common/log.h>
24 #include <bluetooth/bluetooth.h>
25 #include <bluetooth/hci.h>
26 #include <bluetooth/hci_driver.h>
27 
28 #include "bt_vendor_drv.h"
29 #include <hci_api.h>
30 
31 #include <aos/kernel.h>
32 
33 #define H4_NONE      0x00
34 #define H4_CMD       0x01
35 #define H4_ACL       0x02
36 #define H4_SCO       0x03
37 #define H4_EVT       0x04
38 
39 
40 static int h4_open(void);
41 static int h4_send(struct net_buf *buf);
42 
43 static cpu_stack_t hci_rx_task_stack[CONFIG_BT_RX_STACK_SIZE / 4];
44 
45 static struct bt_hci_driver drv = {
46     .name       = "H4",
47     .bus        = BT_HCI_DRIVER_BUS_VIRTUAL,
48     .open       = h4_open,
49     .send       = h4_send,
50 };
51 
52 static struct rx_t {
53     struct net_buf *buf;
54     uint16_t cur_len;
55 
56     uint16_t remaining;
57     uint16_t discard;
58 
59     uint8_t     have_hdr;
60     uint8_t     discardable;
61 
62     uint8_t  hdr_len;
63 
64     uint8_t  type;
65     union {
66         struct bt_hci_evt_hdr evt;
67         struct bt_hci_acl_hdr acl;
68         uint8_t hdr[4];
69     };
70 
71     uint8_t ongoing;
72 };
73 
74 
75 static struct {
76     const struct bt_hci_driver *drv;
77     char *dev_name;
78 //    aos_dev_t *dev;
79     ktask_t task;
80     ksem_t sem;
81     struct rx_t rx;
82 } hci_h4 = {
83     &drv,
84     NULL,
85     NULL,
86 };
87 
88 #ifndef CHIP_HAAS1000
89 
tg_bt_hal_vendor_bringup()90 int tg_bt_hal_vendor_bringup() {
91     return bt_vendor_drv_bring_up();
92 }
93 
tg_bt_hal_hcit_set_rx_ind_callback(rx_ind_cb_t cb)94 void tg_bt_hal_hcit_set_rx_ind_callback(rx_ind_cb_t cb) {
95     bt_vendor_drv_set_rx_ind(cb);
96 }
97 
tg_bt_hal_hcit_rx(uint8_t * data,size_t len)98 size_t tg_bt_hal_hcit_rx(uint8_t *data, size_t len) {
99     return bt_vendor_drv_rx(data, len);
100 }
101 
tg_bt_hal_hcit_tx(uint8_t * data,size_t len,void * reserve)102 size_t tg_bt_hal_hcit_tx(uint8_t *data, size_t len, void *reserve) {
103     return bt_vendor_drv_tx(data, len, reserve);
104 }
105 
106 #endif
107 
h4_send(struct net_buf * buf)108 static int h4_send(struct net_buf *buf)
109 {
110     int ret = -1;
111     uint8_t type  = bt_buf_get_type(buf);
112     uint8_t hcit_type[1];
113 
114     BT_DBG("%s, type = %d", __func__, type);
115     if (type == BT_BUF_ACL_OUT) {
116         hcit_type[0] = H4_ACL;
117         //g_hci_debug_counter.acl_out_count++;
118     } else if (type == BT_BUF_CMD) {
119         hcit_type[0] = H4_CMD;
120         //g_hci_debug_counter.cmd_out_count++;
121     } else {
122         BT_ERR("Unknown buffer type");
123         return -1;
124     }
125 
126 #if 1   //send in one packet
127     uint8_t *pBuf;
128     const int data_len = buf->len+1;
129 
130     pBuf = aos_malloc(data_len);
131     if (pBuf == NULL) {
132         BT_ERR("malloc failed");
133         return -1;
134     }
135 
136     pBuf[0] = hcit_type[0];
137     memcpy(pBuf+1, buf->data, buf->len);
138 
139     BT_INFO("Send to driver: %s", bt_hex_real(pBuf, data_len));
140     ret = tg_bt_hal_hcit_tx(pBuf, data_len, NULL);
141     aos_free(pBuf);
142 #else
143     tg_bt_hal_hcit_tx(hcit_type, 1, NULL);
144     BT_DBG("buf %p type %u len %u:%s", buf, type, buf->len, bt_hex(buf->data, buf->len));
145 
146     const int data_len = buf->len;
147 
148     BT_DBG("payload send");
149     ret = tg_bt_hal_hcit_tx(buf->data, buf->len, NULL);
150 #endif
151     if (ret > 0 && ret == data_len) {
152         ret = 0;
153         net_buf_unref(buf);
154     }
155 
156     if (ret == -BT_HCI_ERR_MEM_CAPACITY_EXCEEDED) {
157         ret = -ENOMEM;
158     }
159     return ret;
160 }
161 
is_adv_report_event(uint8_t * data,uint16_t len)162 static inline int is_adv_report_event(uint8_t *data, uint16_t len)
163 {
164     return (data[0] == H4_EVT && data[1] == BT_HCI_EVT_LE_META_EVENT
165             && data[3] == BT_HCI_EVT_LE_ADVERTISING_REPORT);
166 }
167 
hci_event_recv(uint8_t * data,uint16_t data_len)168 int hci_event_recv(uint8_t *data, uint16_t data_len)
169 {
170     struct net_buf *buf;
171     uint8_t *pdata = data;
172     int32_t len = data_len;
173     struct bt_hci_evt_hdr hdr;
174     uint8_t sub_event = 0;
175     uint8_t discardable = 0;
176 
177     if (pdata == NULL || len == 0) {
178         return -1;
179     }
180 
181     if (*pdata++ != H4_EVT) {
182         goto err;
183     }
184 
185     if (len < 3) {
186         goto err;
187     }
188 
189     hdr.evt = *pdata++;
190     hdr.len = *pdata++;
191 
192     if (len < hdr.len + 3) {
193         goto err;
194     }
195 
196     if (hdr.evt == BT_HCI_EVT_LE_META_EVENT) {
197         sub_event = *pdata++;
198 
199         if (sub_event == BT_HCI_EVT_LE_ADVERTISING_REPORT) {
200             discardable = 1;
201         }
202     }
203 
204     if (hdr.evt == BT_HCI_EVT_CMD_COMPLETE ||
205         hdr.evt  == BT_HCI_EVT_CMD_STATUS) {
206         buf = bt_buf_get_cmd_complete(0);
207 
208         if (buf == NULL) {
209             //g_hci_debug_counter.event_in_is_null_count++;
210             goto err;
211         }
212     } else {
213         buf = bt_buf_get_rx(BT_BUF_EVT, 0);
214     }
215 
216     if (!buf && discardable) {
217         //g_hci_debug_counter.event_discard_count++;
218         goto err;
219     }
220 
221     if (!buf) {
222         //g_hci_debug_counter.event_in_is_null_count++;
223         goto err;
224     }
225 
226     bt_buf_set_type(buf, BT_BUF_EVT);
227 
228     net_buf_add_mem(buf, ((uint8_t *)(data)) + 1, hdr.len + sizeof(hdr));
229 
230     BT_DBG("event %s", bt_hex(buf->data, buf->len));
231     //g_hci_debug_counter.event_in_count++;
232 
233     if (bt_hci_evt_is_prio(hdr.evt)) {
234         bt_recv_prio(buf);
235     } else {
236         bt_recv(buf);
237     }
238 
239     return 0;
240 
241 err:
242     return -1;
243 }
244 
hci_acl_recv(uint8_t * data,uint16_t data_len)245 int hci_acl_recv(uint8_t *data, uint16_t data_len)
246 {
247     struct net_buf *buf;
248     uint8_t *pdata = data;
249     int32_t len = data_len;
250     uint16_t handle;
251     uint16_t acl_len;
252 
253     if (pdata == NULL || len == 0) {
254         return -1;
255     }
256 
257     if (*pdata++ != H4_ACL) {
258         goto err;
259     }
260 
261     if (len < 5) {
262         goto err;
263     }
264 
265     handle = ((*pdata + 1) << 16) | (*(pdata));
266     pdata += 2;
267     acl_len = ((*pdata + 1) << 16) | (*(pdata));
268     pdata += 2;
269 
270     (void)handle;
271 
272     if (len < acl_len + 5) {
273         goto err;
274     }
275 
276     buf = bt_buf_get_rx(BT_BUF_ACL_IN, 0);
277 
278     if (!buf) {
279         //g_hci_debug_counter.hci_in_is_null_count++;
280         goto err;
281     }
282 
283     bt_buf_set_type(buf, BT_BUF_ACL_IN);
284 
285     net_buf_add_mem(buf, data + 1, acl_len + 4);
286     //g_hci_debug_counter.acl_in_count++;
287     BT_DBG("acl %s", bt_hex(buf->data, buf->len));
288 
289     bt_recv(buf);
290     return 0;
291 
292 err:
293     return -1;
294 }
295 
_hci_recv_event2(void)296 static void _hci_recv_event2(void)
297 {
298     krhino_sem_give(&hci_h4.sem);
299 }
300 
copy_hdr(struct net_buf * buf)301 static void copy_hdr(struct net_buf *buf)
302 {
303     net_buf_add_mem(buf, hci_h4.rx.hdr, hci_h4.rx.hdr_len);
304 }
305 
reset_rx(void)306 static void reset_rx(void)
307 {
308     hci_h4.rx.type = H4_NONE;
309     hci_h4.rx.remaining = 0;
310     hci_h4.rx.have_hdr = false;
311     hci_h4.rx.hdr_len = 0;
312     hci_h4.rx.discardable = false;
313 }
314 
read_byte(uint8_t * data,uint32_t len)315 static uint32_t read_byte(uint8_t *data, uint32_t len)
316 {
317     int32_t read_len;
318 
319     read_len = tg_bt_hal_hcit_rx(data, len);
320 
321     if (read_len == 0) {
322         hci_h4.rx.ongoing = 0;
323     }
324 
325     return read_len;
326 }
327 
h4_get_type(void)328 static inline void h4_get_type(void)
329 {
330     /* Get packet type */
331     if (read_byte(&hci_h4.rx.type, 1) != 1) {
332         hci_h4.rx.type = H4_NONE;
333         return;
334     }
335 
336     switch (hci_h4.rx.type) {
337         case H4_EVT:
338             hci_h4.rx.remaining = sizeof(hci_h4.rx.evt);
339             hci_h4.rx.hdr_len = hci_h4.rx.remaining;
340             break;
341 
342         case H4_ACL:
343             hci_h4.rx.remaining = sizeof(hci_h4.rx.acl);
344             hci_h4.rx.hdr_len = hci_h4.rx.remaining;
345             break;
346 
347         default:
348             BT_ERR("Unknown H:4 type 0x%02x", hci_h4.rx.type);
349             hci_h4.rx.type = H4_NONE;
350     }
351 }
352 
get_acl_hdr(void)353 static inline void get_acl_hdr(void)
354 {
355     struct bt_hci_acl_hdr *hdr = &hci_h4.rx.acl;
356     int to_read = sizeof(*hdr) - hci_h4.rx.remaining;
357 
358     hci_h4.rx.remaining -= read_byte((uint8_t *)hdr + to_read, hci_h4.rx.remaining);
359     if (!hci_h4.rx.remaining) {
360         hci_h4.rx.remaining = hdr->len;
361         BT_DBG("Got ACL header. Payload %u bytes", hci_h4.rx.remaining);
362         hci_h4.rx.have_hdr = true;
363     }
364 }
365 
get_evt_hdr(void)366 static inline void get_evt_hdr(void)
367 {
368     struct bt_hci_evt_hdr *hdr = &hci_h4.rx.evt;
369     int32_t to_read = hci_h4.rx.hdr_len - hci_h4.rx.remaining;
370 
371     hci_h4.rx.remaining -= read_byte((uint8_t *)hdr + to_read, hci_h4.rx.remaining);
372     if (!hci_h4.rx.remaining) {
373         hci_h4.rx.remaining = hdr->len - (hci_h4.rx.hdr_len - sizeof(*hdr));
374 
375         BT_DBG("Got event header. Payload %u bytes", hdr->len);
376         hci_h4.rx.have_hdr = true;
377     }
378 }
379 
h4_discard(int32_t len)380 static int32_t h4_discard(int32_t len)
381 {
382     uint8_t buf[33];
383 
384     return read_byte(buf, (len<sizeof(buf)?len:sizeof(buf)));
385 }
386 
read_payload(void)387 static inline void read_payload(void)
388 {
389     struct net_buf *buf;
390     bool prio;
391     int read;
392 
393     if (!hci_h4.rx.buf) {
394         if (hci_h4.rx.type == H4_ACL) {
395             hci_h4.rx.buf = buf = bt_buf_get_rx(BT_BUF_ACL_IN, 0);
396         } else {
397             if (hci_h4.rx.evt.evt == BT_HCI_EVT_CMD_COMPLETE || hci_h4.rx.evt.evt  == BT_HCI_EVT_CMD_STATUS) {
398                 hci_h4.rx.buf = bt_buf_get_cmd_complete(0);
399             } else {
400                 hci_h4.rx.buf = buf = bt_buf_get_rx(BT_BUF_EVT, 0);
401             }
402         }
403 
404         if (!hci_h4.rx.buf) {
405             if (hci_h4.rx.discardable) {
406                 BT_DBG("Discarding event 0x%02x", hci_h4.rx.evt.evt);
407                 hci_h4.rx.discard = hci_h4.rx.remaining;
408                 reset_rx();
409                 return;
410             }
411 
412             BT_ERR("Failed to allocate, deferring to rx_thread");
413             return;
414         }
415 
416         BT_DBG("Allocated h4_dev.rx.buf %p", hci_h4.rx.buf);
417 
418         copy_hdr(hci_h4.rx.buf);
419         hci_h4.rx.cur_len = hci_h4.rx.hdr_len + 1;
420     }
421 
422     read = read_byte(net_buf_tail(hci_h4.rx.buf), hci_h4.rx.remaining);
423     net_buf_add(hci_h4.rx.buf, read);
424     hci_h4.rx.cur_len += read;
425     hci_h4.rx.remaining -= read;
426 
427     BT_DBG("got %d bytes, remaining %u", read, hci_h4.rx.remaining);
428 
429     if (hci_h4.rx.remaining) {
430         return;
431     }
432 
433     prio = ((hci_h4.rx.type == H4_EVT) && bt_hci_evt_is_prio(hci_h4.rx.evt.evt));
434     buf = hci_h4.rx.buf;
435     hci_h4.rx.buf = NULL;
436 
437     if (hci_h4.rx.type == H4_EVT) {
438         bt_buf_set_type(buf, BT_BUF_EVT);
439     } else {
440         bt_buf_set_type(buf, BT_BUF_ACL_IN);
441     }
442 
443     reset_rx();
444     if (prio) {
445         BT_DBG("Calling bt_recv_prio(%p)", buf);
446         bt_recv_prio(buf);
447     } else {
448         BT_DBG("Putting buf %p to rx fifo", buf);
449         bt_recv(buf);
450     }
451 }
452 
read_header(void)453 static inline void read_header(void)
454 {
455     switch (hci_h4.rx.type) {
456         case H4_NONE:
457             h4_get_type();
458             return;
459 
460         case H4_EVT:
461             get_evt_hdr();
462             break;
463         case H4_ACL:
464             get_acl_hdr();
465             break;
466 
467         default:
468             reset_rx();
469             return;
470     }
471 }
472 
process_rx(void)473 static void process_rx(void)
474 {
475     BT_DBG("remaining %u discard %u have_hdr %u h4_dev.rx.buf %p",
476            hci_h4.rx.remaining, hci_h4.rx.discard, hci_h4.rx.have_hdr, hci_h4.rx.buf);
477 
478     if (hci_h4.rx.discard) {
479         hci_h4.rx.discard -= h4_discard(hci_h4.rx.discard);
480         return;
481     }
482 
483     if (hci_h4.rx.have_hdr) {
484         read_payload();
485     } else {
486         read_header();
487     }
488 }
489 
recv_data(void)490 static void recv_data(void)
491 {
492     do {
493         hci_h4.rx.ongoing = 1;
494         process_rx();
495     } while (hci_h4.rx.ongoing);
496 }
497 
hci_rx_task(void * arg)498 static void hci_rx_task(void *arg)
499 {
500     while (1) {
501         krhino_sem_take(&hci_h4.sem, RHINO_WAIT_FOREVER);
502         recv_data();
503     }
504 }
505 
h4_open(void)506 static int h4_open(void)
507 {
508     krhino_sem_create(&hci_h4.sem, "h4", 0);
509 
510 
511     int ret;
512 
513     ret = tg_bt_hal_vendor_bringup();
514     if (ret != 0) {
515         BT_ERR("vendor chip bringup failed");
516         return false;
517     }
518     tg_bt_hal_hcit_set_rx_ind_callback(_hci_recv_event2);
519 
520     krhino_task_create(&hci_h4.task, "hci_rx_task", NULL,
521                        CONFIG_BT_RX_PRIO, 1, hci_rx_task_stack,
522                        CONFIG_BT_RX_STACK_SIZE / 4, (task_entry_t)hci_rx_task, 1);
523 
524     return 0;
525 }
526 
hci_driver_init(char * name)527 int hci_driver_init(char *name)
528 {
529     int ret;
530 
531     ret = bt_hci_driver_register(&drv);
532 
533     if (ret) {
534         return ret;
535     }
536 
537     hci_h4.dev_name = name;
538 
539     return 0;
540 }
541 
hci_h4_driver_init()542 int hci_h4_driver_init()
543 {
544     hci_driver_init("hci");
545     return 0;
546 }
547