xref: /nrf52832-nimble/packages/NimBLE-latest/porting/npl/dummy/src/hci_dummy.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include <assert.h>
21 #include <stdio.h>
22 #include <stdint.h>
23 #include "syscfg/syscfg.h"
24 #include "sysinit/sysinit.h"
25 #include "os/os_mempool.h"
26 #include "nimble/ble.h"
27 #include "nimble/ble_hci_trans.h"
28 #include "nimble/hci_common.h"
29 
30 /* HCI packet types */
31 #define HCI_PKT_CMD     0x01
32 #define HCI_PKT_ACL     0x02
33 #define HCI_PKT_EVT     0x04
34 #define HCI_PKT_GTL     0x05
35 
36 /* Buffers for HCI commands data */
37 static uint8_t trans_buf_cmd[BLE_HCI_TRANS_CMD_SZ];
38 static uint8_t trans_buf_cmd_allocd;
39 
40 /* Buffers for HCI events data */
41 static uint8_t trans_buf_evt_hi_pool_buf[ OS_MEMPOOL_BYTES(
42                                             MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
43                                             MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)) ];
44 static struct os_mempool trans_buf_evt_hi_pool;
45 static uint8_t trans_buf_evt_lo_pool_buf[ OS_MEMPOOL_BYTES(
46                                             MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
47                                             MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)) ];
48 static struct os_mempool trans_buf_evt_lo_pool;
49 
50 /* Buffers for HCI ACL data */
51 #define ACL_POOL_BLOCK_SIZE OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) + \
52                                             BLE_MBUF_MEMBLOCK_OVERHEAD + \
53                                             BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT)
54 static uint8_t trans_buf_acl_pool_buf[ OS_MEMPOOL_BYTES(
55                                             MYNEWT_VAL(BLE_ACL_BUF_COUNT),
56                                             ACL_POOL_BLOCK_SIZE) ];
57 static struct os_mempool trans_buf_acl_pool;
58 static struct os_mbuf_pool trans_buf_acl_mbuf_pool;
59 
60 /* Host interface */
61 static ble_hci_trans_rx_cmd_fn *trans_rx_cmd_cb;
62 static void *trans_rx_cmd_arg;
63 static ble_hci_trans_rx_acl_fn *trans_rx_acl_cb;
64 static void *trans_rx_acl_arg;
65 
66 /* Called by NimBLE host to reset HCI transport state (i.e. on host reset) */
67 int
ble_hci_trans_reset(void)68 ble_hci_trans_reset(void)
69 {
70     return 0;
71 }
72 
73 /* Called by NimBLE host to setup callbacks from HCI transport */
74 void
ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn * cmd_cb,void * cmd_arg,ble_hci_trans_rx_acl_fn * acl_cb,void * acl_arg)75 ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn *cmd_cb, void *cmd_arg,
76                      ble_hci_trans_rx_acl_fn *acl_cb, void *acl_arg)
77 {
78     trans_rx_cmd_cb = cmd_cb;
79     trans_rx_cmd_arg = cmd_arg;
80     trans_rx_acl_cb = acl_cb;
81     trans_rx_acl_arg = acl_arg;
82 }
83 
84 /*
85  * Called by NimBLE host to allocate buffer for HCI Command packet.
86  * Called by HCI transport to allocate buffer for HCI Event packet.
87  */
88 uint8_t *
ble_hci_trans_buf_alloc(int type)89 ble_hci_trans_buf_alloc(int type)
90 {
91     uint8_t *buf;
92 
93     switch (type) {
94     case BLE_HCI_TRANS_BUF_CMD:
95         assert(!trans_buf_cmd_allocd);
96         trans_buf_cmd_allocd = 1;
97         buf = trans_buf_cmd;
98         break;
99     case BLE_HCI_TRANS_BUF_EVT_HI:
100         buf = os_memblock_get(&trans_buf_evt_hi_pool);
101         if (buf) {
102             break;
103         }
104         /* no break */
105     case BLE_HCI_TRANS_BUF_EVT_LO:
106         buf = os_memblock_get(&trans_buf_evt_lo_pool);
107         break;
108     default:
109         assert(0);
110         buf = NULL;
111     }
112 
113     return buf;
114 }
115 
116 /*
117  * Called by NimBLE host to free buffer allocated for HCI Event packet.
118  * Called by HCI transport to free buffer allocated for HCI Command packet.
119  */
120 void
ble_hci_trans_buf_free(uint8_t * buf)121 ble_hci_trans_buf_free(uint8_t *buf)
122 {
123     int rc;
124 
125     if (buf == trans_buf_cmd) {
126         assert(trans_buf_cmd_allocd);
127         trans_buf_cmd_allocd = 0;
128     } else if (os_memblock_from(&trans_buf_evt_hi_pool, buf)) {
129         rc = os_memblock_put(&trans_buf_evt_hi_pool, buf);
130         assert(rc == 0);
131     } else {
132         assert(os_memblock_from(&trans_buf_evt_lo_pool, buf));
133         rc = os_memblock_put(&trans_buf_evt_lo_pool, buf);
134         assert(rc == 0);
135     }
136 }
137 
138 /* Called by NimBLE host to send HCI Command packet over HCI transport */
139 int
ble_hci_trans_hs_cmd_tx(uint8_t * cmd)140 ble_hci_trans_hs_cmd_tx(uint8_t *cmd)
141 {
142     uint8_t *buf = cmd;
143 
144     /*
145      * TODO Send HCI Command packet somewhere.
146      * Buffer pointed by 'cmd' contains complete HCI Command packet as defined
147      * by Core spec.
148      */
149 
150     ble_hci_trans_buf_free(buf);
151 
152     return 0;
153 }
154 
155 /* Called by NimBLE host to send HCI ACL Data packet over HCI transport */
156 int
ble_hci_trans_hs_acl_tx(struct os_mbuf * om)157 ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
158 {
159     uint8_t *buf = om->om_data;
160 
161     /*
162      * TODO Send HCI ACL Data packet somewhere.
163      * mbuf pointed by 'om' contains complete HCI ACL Data packet as defined
164      * by Core spec.
165      */
166     (void)buf;
167 
168     os_mbuf_free_chain(om);
169 
170     return 0;
171 }
172 
173 /* Called by application to send HCI ACL Data packet to host */
174 int
hci_transport_send_acl_to_host(uint8_t * buf,uint16_t size)175 hci_transport_send_acl_to_host(uint8_t *buf, uint16_t size)
176 {
177     struct os_mbuf *trans_mbuf;
178     int rc;
179 
180     trans_mbuf = os_mbuf_get_pkthdr(&trans_buf_acl_mbuf_pool,
181                                     sizeof(struct ble_mbuf_hdr));
182     os_mbuf_append(trans_mbuf, buf, size);
183     rc = trans_rx_acl_cb(trans_mbuf, trans_rx_acl_arg);
184 
185     return rc;
186 }
187 
188 /* Called by application to send HCI Event packet to host */
189 int
hci_transport_send_evt_to_host(uint8_t * buf,uint8_t size)190 hci_transport_send_evt_to_host(uint8_t *buf, uint8_t size)
191 {
192     uint8_t *trans_buf;
193     int rc;
194 
195     /* Allocate LE Advertising Report Event from lo pool only */
196     if ((buf[0] == BLE_HCI_EVCODE_LE_META) &&
197         (buf[2] == BLE_HCI_LE_SUBEV_ADV_RPT)) {
198         trans_buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_LO);
199         if (!trans_buf) {
200             /* Skip advertising report if we're out of memory */
201             return 0;
202         }
203     } else {
204         trans_buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
205     }
206 
207     memcpy(trans_buf, buf, size);
208 
209     rc = trans_rx_cmd_cb(trans_buf, trans_rx_cmd_arg);
210     if (rc != 0) {
211         ble_hci_trans_buf_free(trans_buf);
212     }
213 
214     return rc;
215 }
216 
217 /* Called by application to initialize transport structures */
218 int
hci_transport_init(void)219 hci_transport_init(void)
220 {
221     int rc;
222 
223     trans_buf_cmd_allocd = 0;
224 
225     rc = os_mempool_init(&trans_buf_acl_pool, MYNEWT_VAL(BLE_ACL_BUF_COUNT),
226                                 ACL_POOL_BLOCK_SIZE, trans_buf_acl_pool_buf,
227                                 "dummy_hci_acl_pool");
228     SYSINIT_PANIC_ASSERT(rc == 0);
229 
230     rc = os_mbuf_pool_init(&trans_buf_acl_mbuf_pool, &trans_buf_acl_pool,
231                                 ACL_POOL_BLOCK_SIZE,
232                                 MYNEWT_VAL(BLE_ACL_BUF_COUNT));
233     SYSINIT_PANIC_ASSERT(rc == 0);
234 
235     rc = os_mempool_init(&trans_buf_evt_hi_pool,
236                                 MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT),
237                                 MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE),
238                                 trans_buf_evt_hi_pool_buf,
239                                 "dummy_hci_hci_evt_hi_pool");
240     SYSINIT_PANIC_ASSERT(rc == 0);
241 
242     rc = os_mempool_init(&trans_buf_evt_lo_pool,
243                                 MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT),
244                                 MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE),
245                                 trans_buf_evt_lo_pool_buf,
246                                 "dummy_hci_hci_evt_lo_pool");
247     SYSINIT_PANIC_ASSERT(rc == 0);
248 
249     return 0;
250 }
251