diff --git a/nimble/transport/emspi/src/ble_hci_emspi.c b/nimble/transport/emspi/src/ble_hci_emspi.c index 1487984b3..2c1f951dd 100644 --- a/nimble/transport/emspi/src/ble_hci_emspi.c +++ b/nimble/transport/emspi/src/ble_hci_emspi.c @@ -61,17 +61,6 @@ #define BLE_HCI_EMSPI_OP_TX 0x42 #define BLE_HCI_EMSPI_OP_RX 0x81 -/** - * A packet to be sent over the UART. This can be a command, an event, or ACL - * data. - */ -struct ble_hci_emspi_pkt { - STAILQ_ENTRY(ble_hci_emspi_pkt) next; - void *data; - uint8_t type; -}; -STAILQ_HEAD(, ble_hci_emspi_pkt) ble_hci_emspi_tx_q; - static os_event_fn ble_hci_emspi_event_txrx; static struct os_event ble_hci_emspi_ev_txrx = { @@ -89,19 +78,56 @@ static ble_hci_trans_rx_acl_fn *ble_hci_emspi_rx_acl_cb; static void *ble_hci_emspi_rx_acl_arg; static struct os_mempool ble_hci_emspi_evt_hi_pool; -static void *ble_hci_emspi_evt_hi_buf; +static os_membuf_t ble_hci_emspi_evt_hi_buf[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)) +]; + static struct os_mempool ble_hci_emspi_evt_lo_pool; -static void *ble_hci_emspi_evt_lo_buf; +static os_membuf_t ble_hci_emspi_evt_lo_buf[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)) +]; static struct os_mempool ble_hci_emspi_cmd_pool; -static void *ble_hci_emspi_cmd_buf; - -static struct os_mempool ble_hci_emspi_pkt_pool; -static void *ble_hci_emspi_pkt_buf; +static os_membuf_t ble_hci_emspi_cmd_buf[ + OS_MEMPOOL_SIZE(1, BLE_HCI_TRANS_CMD_SZ) +]; static struct os_mbuf_pool ble_hci_emspi_acl_mbuf_pool; static struct os_mempool_ext ble_hci_emspi_acl_pool; -static void *ble_hci_emspi_acl_buf; + +/* + * The MBUF payload size must accommodate the HCI data header size plus the + * maximum ACL data packet length. The ACL block size is the size of the + * mbufs we will allocate. + */ +#define ACL_BLOCK_SIZE OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) \ + + BLE_MBUF_MEMBLOCK_OVERHEAD \ + + BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT) + +static os_membuf_t ble_hci_emspi_acl_buf[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_ACL_BUF_COUNT), + ACL_BLOCK_SIZE) +]; + +/** + * A packet to be sent over the EMSPI. This can be a command, an event, or ACL + * data. + */ +struct ble_hci_emspi_pkt { + STAILQ_ENTRY(ble_hci_emspi_pkt) next; + void *data; + uint8_t type; +}; +STAILQ_HEAD(, ble_hci_emspi_pkt) ble_hci_emspi_tx_q; + +static struct os_mempool ble_hci_emspi_pkt_pool; +static os_membuf_t ble_hci_emspi_pkt_buf[ + OS_MEMPOOL_SIZE(BLE_HCI_EMSPI_PKT_EVT_COUNT + 1 + + MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT), + sizeof (struct ble_hci_emspi_pkt)) +]; static void ble_hci_emspi_rdy_isr(void *arg) @@ -866,53 +892,49 @@ ble_hci_emspi_init_hw(void) void ble_hci_emspi_init(void) { - int acl_block_size; int rc; /* Ensure this function only gets called by sysinit. */ SYSINIT_ASSERT_ACTIVE(); - /* - * The MBUF payload size must accommodate the HCI data header size plus the - * maximum ACL data packet length. The ACL block size is the size of the - * mbufs we will allocate. - */ - acl_block_size = MYNEWT_VAL(BLE_ACL_BUF_SIZE) + - BLE_MBUF_MEMBLOCK_OVERHEAD + - BLE_HCI_DATA_HDR_SZ; - acl_block_size = OS_ALIGN(acl_block_size, OS_ALIGNMENT); - rc = mem_malloc_mempool_ext(&ble_hci_emspi_acl_pool, - MYNEWT_VAL(BLE_ACL_BUF_COUNT), - acl_block_size, - "ble_hci_emspi_acl_pool", - &ble_hci_emspi_acl_buf); + rc = os_mempool_ext_init(&ble_hci_emspi_acl_pool, + MYNEWT_VAL(BLE_ACL_BUF_COUNT), + ACL_BLOCK_SIZE, + ble_hci_emspi_acl_buf, + "ble_hci_emspi_acl_pool"); SYSINIT_PANIC_ASSERT(rc == 0); rc = os_mbuf_pool_init(&ble_hci_emspi_acl_mbuf_pool, &ble_hci_emspi_acl_pool.mpe_mp, - acl_block_size, + ACL_BLOCK_SIZE, MYNEWT_VAL(BLE_ACL_BUF_COUNT)); SYSINIT_PANIC_ASSERT(rc == 0); - rc = mem_malloc_mempool(&ble_hci_emspi_cmd_pool, - 1, - BLE_HCI_TRANS_CMD_SZ, - "ble_hci_emspi_cmd_pool", - &ble_hci_emspi_cmd_buf); + /* + * Create memory pool of HCI command buffers. NOTE: we currently dont + * allow this to be configured. The controller will only allow one + * outstanding command. We decided to keep this a pool in case we allow + * allow the controller to handle more than one outstanding command. + */ + rc = os_mempool_init(&ble_hci_emspi_cmd_pool, + 1, + BLE_HCI_TRANS_CMD_SZ, + ble_hci_emspi_cmd_buf, + "ble_hci_emspi_cmd_pool"); SYSINIT_PANIC_ASSERT(rc == 0); - rc = mem_malloc_mempool(&ble_hci_emspi_evt_hi_pool, - MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), - MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), - "ble_hci_emspi_evt_hi_pool", - &ble_hci_emspi_evt_hi_buf); + rc = os_mempool_init(&ble_hci_emspi_evt_hi_pool, + MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), + ble_hci_emspi_evt_hi_buf, + "ble_hci_emspi_evt_hi_pool"); SYSINIT_PANIC_ASSERT(rc == 0); - rc = mem_malloc_mempool(&ble_hci_emspi_evt_lo_pool, - MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), - MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), - "ble_hci_emspi_evt_lo_pool", - &ble_hci_emspi_evt_lo_buf); + rc = os_mempool_init(&ble_hci_emspi_evt_lo_pool, + MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), + ble_hci_emspi_evt_lo_buf, + "ble_hci_emspi_evt_lo_pool"); SYSINIT_PANIC_ASSERT(rc == 0); /* @@ -921,12 +943,12 @@ ble_hci_emspi_init(void) * and lo), the number of command buffers (currently 1) and the total * number of buffers that the controller could possibly hand to the host. */ - rc = mem_malloc_mempool(&ble_hci_emspi_pkt_pool, - BLE_HCI_EMSPI_PKT_EVT_COUNT + 1 + - MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT), - sizeof (struct ble_hci_emspi_pkt), - "ble_hci_emspi_pkt_pool", - &ble_hci_emspi_pkt_buf); + rc = os_mempool_init(&ble_hci_emspi_pkt_pool, + BLE_HCI_EMSPI_PKT_EVT_COUNT + 1 + + MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT), + sizeof (struct ble_hci_emspi_pkt), + ble_hci_emspi_pkt_buf, + "ble_hci_emspi_pkt_pool"); SYSINIT_PANIC_ASSERT(rc == 0); STAILQ_INIT(&ble_hci_emspi_tx_q); diff --git a/nimble/transport/ram/include/transport/ram/ble_hci_ram.h b/nimble/transport/ram/include/transport/ram/ble_hci_ram.h index 5e8a25bb1..0fc66f22a 100644 --- a/nimble/transport/ram/include/transport/ram/ble_hci_ram.h +++ b/nimble/transport/ram/include/transport/ram/ble_hci_ram.h @@ -26,7 +26,7 @@ extern "C" { #endif -int ble_hci_ram_init(void); +void ble_hci_ram_init(void); #ifdef __cplusplus } diff --git a/nimble/transport/ram/pkg.yml b/nimble/transport/ram/pkg.yml index a9d46a7b9..bf301bf13 100644 --- a/nimble/transport/ram/pkg.yml +++ b/nimble/transport/ram/pkg.yml @@ -33,4 +33,4 @@ pkg.apis: - ble_transport pkg.init: - ble_hci_ram_pkg_init: 100 + ble_hci_ram_init: 100 diff --git a/nimble/transport/ram/src/ble_hci_ram.c b/nimble/transport/ram/src/ble_hci_ram.c index 34406d44c..3f10e9df7 100644 --- a/nimble/transport/ram/src/ble_hci_ram.c +++ b/nimble/transport/ram/src/ble_hci_ram.c @@ -40,13 +40,22 @@ static void *ble_hci_ram_rx_acl_hs_arg; static ble_hci_trans_rx_acl_fn *ble_hci_ram_rx_acl_ll_cb; static void *ble_hci_ram_rx_acl_ll_arg; -static struct os_mempool ble_hci_ram_evt_hi_pool; -static void *ble_hci_ram_evt_hi_buf; -static struct os_mempool ble_hci_ram_evt_lo_pool; -static void *ble_hci_ram_evt_lo_buf; +static struct os_mempool ble_hci_ram_cmd_pool; +static os_membuf_t ble_hci_ram_cmd_buf[ + OS_MEMPOOL_SIZE(1, BLE_HCI_TRANS_CMD_SZ) +]; -static uint8_t *ble_hci_ram_hs_cmd_buf; -static uint8_t ble_hci_ram_hs_cmd_buf_alloced; +static struct os_mempool ble_hci_ram_evt_hi_pool; +static os_membuf_t ble_hci_ram_evt_hi_buf[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)) +]; + +static struct os_mempool ble_hci_ram_evt_lo_pool; +static os_membuf_t ble_hci_ram_evt_lo_buf[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)) +]; void ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn *cmd_cb, @@ -122,6 +131,10 @@ ble_hci_trans_buf_alloc(int type) uint8_t *buf; switch (type) { + case BLE_HCI_TRANS_BUF_CMD: + buf = os_memblock_get(&ble_hci_ram_cmd_pool); + break; + case BLE_HCI_TRANS_BUF_EVT_HI: buf = os_memblock_get(&ble_hci_ram_evt_hi_pool); if (buf == NULL) { @@ -136,12 +149,6 @@ ble_hci_trans_buf_alloc(int type) buf = os_memblock_get(&ble_hci_ram_evt_lo_pool); break; - case BLE_HCI_TRANS_BUF_CMD: - assert(!ble_hci_ram_hs_cmd_buf_alloced); - ble_hci_ram_hs_cmd_buf_alloced = 1; - buf = ble_hci_ram_hs_cmd_buf; - break; - default: assert(0); buf = NULL; @@ -155,16 +162,23 @@ ble_hci_trans_buf_free(uint8_t *buf) { int rc; - if (buf == ble_hci_ram_hs_cmd_buf) { - assert(ble_hci_ram_hs_cmd_buf_alloced); - ble_hci_ram_hs_cmd_buf_alloced = 0; - } else if (os_memblock_from(&ble_hci_ram_evt_hi_pool, buf)) { + /* XXX: this may look a bit odd, but the controller uses the command + * buffer to send back the command complete/status as an immediate + * response to the command. This was done to insure that the controller + * could always send back one of these events when a command was received. + * Thus, we check to see which pool the buffer came from so we can free + * it to the appropriate pool + */ + if (os_memblock_from(&ble_hci_ram_evt_hi_pool, buf)) { rc = os_memblock_put(&ble_hci_ram_evt_hi_pool, buf); assert(rc == 0); - } else { - assert(os_memblock_from(&ble_hci_ram_evt_lo_pool, buf)); + } else if (os_memblock_from(&ble_hci_ram_evt_lo_pool, buf)) { rc = os_memblock_put(&ble_hci_ram_evt_lo_pool, buf); assert(rc == 0); + } else { + assert(os_memblock_from(&ble_hci_ram_cmd_pool, buf)); + rc = os_memblock_put(&ble_hci_ram_cmd_pool, buf); + assert(rc == 0); } } @@ -178,20 +192,6 @@ ble_hci_trans_set_acl_free_cb(os_mempool_put_fn *cb, void *arg) return BLE_ERR_UNSUPPORTED; } -static void -ble_hci_ram_free_mem(void) -{ - free(ble_hci_ram_evt_hi_buf); - ble_hci_ram_evt_hi_buf = NULL; - - free(ble_hci_ram_evt_lo_buf); - ble_hci_ram_evt_lo_buf = NULL; - - free(ble_hci_ram_hs_cmd_buf); - ble_hci_ram_hs_cmd_buf = NULL; - ble_hci_ram_hs_cmd_buf_alloced = 0; -} - int ble_hci_trans_reset(void) { @@ -201,63 +201,38 @@ ble_hci_trans_reset(void) return 0; } -/** - * Initializes the RAM HCI transport module. - * - * @param cfg The settings to initialize the HCI RAM - * transport with. - * - * @return 0 on success; - * A BLE_ERR_[...] error code on failure. - */ -int -ble_hci_ram_init(void) -{ - int rc; - - ble_hci_ram_free_mem(); - - rc = mem_malloc_mempool(&ble_hci_ram_evt_hi_pool, - MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), - MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), - "ble_hci_ram_evt_hi_pool", - &ble_hci_ram_evt_hi_buf); - if (rc != 0) { - rc = ble_err_from_os(rc); - goto err; - } - - rc = mem_malloc_mempool(&ble_hci_ram_evt_lo_pool, - MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), - MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), - "ble_hci_ram_evt_lo_pool", - &ble_hci_ram_evt_lo_buf); - if (rc != 0) { - rc = ble_err_from_os(rc); - goto err; - } - - ble_hci_ram_hs_cmd_buf = malloc(BLE_HCI_TRANS_CMD_SZ); - if (ble_hci_ram_hs_cmd_buf == NULL) { - rc = BLE_ERR_MEM_CAPACITY; - goto err; - } - - return 0; - -err: - ble_hci_ram_free_mem(); - return rc; -} - void -ble_hci_ram_pkg_init(void) +ble_hci_ram_init(void) { int rc; /* Ensure this function only gets called by sysinit. */ SYSINIT_ASSERT_ACTIVE(); - rc = ble_hci_ram_init(); + /* + * Create memory pool of HCI command buffers. NOTE: we currently dont + * allow this to be configured. The controller will only allow one + * outstanding command. We decided to keep this a pool in case we allow + * allow the controller to handle more than one outstanding command. + */ + rc = os_mempool_init(&ble_hci_ram_cmd_pool, + 1, + BLE_HCI_TRANS_CMD_SZ, + ble_hci_ram_cmd_buf, + "ble_hci_ram_cmd_pool"); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = os_mempool_init(&ble_hci_ram_evt_hi_pool, + MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), + ble_hci_ram_evt_hi_buf, + "ble_hci_ram_evt_hi_pool"); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = os_mempool_init(&ble_hci_ram_evt_lo_pool, + MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), + ble_hci_ram_evt_lo_buf, + "ble_hci_ram_evt_lo_pool"); SYSINIT_PANIC_ASSERT(rc == 0); } diff --git a/nimble/transport/socket/src/ble_hci_socket.c b/nimble/transport/socket/src/ble_hci_socket.c index 8c12b348e..7a1f308db 100644 --- a/nimble/transport/socket/src/ble_hci_socket.c +++ b/nimble/transport/socket/src/ble_hci_socket.c @@ -145,16 +145,38 @@ STATS_NAME_END(hci_sock_stats) struct os_task ble_sock_task; static struct os_mempool ble_hci_sock_evt_hi_pool; -static void *ble_hci_sock_evt_hi_buf; +static os_membuf_t ble_hci_sock_evt_hi_buf[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)) +]; + static struct os_mempool ble_hci_sock_evt_lo_pool; -static void *ble_hci_sock_evt_lo_buf; +static os_membuf_t ble_hci_sock_evt_lo_buf[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)) +]; static struct os_mempool ble_hci_sock_cmd_pool; -static void *ble_hci_sock_cmd_buf; +static os_membuf_t ble_hci_sock_cmd_buf[ + OS_MEMPOOL_SIZE(1, BLE_HCI_TRANS_CMD_SZ) +]; -static struct os_mbuf_pool ble_hci_sock_acl_mbuf_pool; static struct os_mempool ble_hci_sock_acl_pool; -static void *ble_hci_sock_acl_buf; +static struct os_mbuf_pool ble_hci_sock_acl_mbuf_pool; + +#define ACL_BLOCK_SIZE OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) \ + + BLE_MBUF_MEMBLOCK_OVERHEAD \ + + BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT) +/* + * The MBUF payload size must accommodate the HCI data header size plus the + * maximum ACL data packet length. The ACL block size is the size of the + * mbufs we will allocate. + */ + +static os_membuf_t ble_hci_sock_acl_buf[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_ACL_BUF_COUNT), + ACL_BLOCK_SIZE) +]; static ble_hci_trans_rx_cmd_fn *ble_hci_sock_rx_cmd_cb; static void *ble_hci_sock_rx_cmd_arg; @@ -752,8 +774,6 @@ ble_hci_sock_init_task(void) void ble_hci_sock_init(void) { - int acl_data_length; - int acl_block_size; int rc; /* Ensure this function only gets called by sysinit. */ @@ -765,25 +785,16 @@ ble_hci_sock_init(void) ble_hci_sock_init_task(); ble_hci_sock_state.ev.ev_cb = ble_hci_sock_rx_ev; - /* - * The MBUF payload size must accommodate the HCI data header size plus the - * maximum ACL data packet length. The ACL block size is the size of the - * mbufs we will allocate. - */ - acl_data_length = MYNEWT_VAL(BLE_ACL_BUF_SIZE); - acl_block_size = acl_data_length + BLE_MBUF_MEMBLOCK_OVERHEAD + - BLE_HCI_DATA_HDR_SZ; - acl_block_size = OS_ALIGN(acl_block_size, OS_ALIGNMENT); - rc = mem_malloc_mempool(&ble_hci_sock_acl_pool, - MYNEWT_VAL(BLE_ACL_BUF_COUNT), - acl_block_size, - "ble_hci_sock_acl_pool", - &ble_hci_sock_acl_buf); + rc = os_mempool_init(&ble_hci_sock_acl_pool, + MYNEWT_VAL(BLE_ACL_BUF_COUNT), + ACL_BLOCK_SIZE, + ble_hci_sock_acl_buf, + "ble_hci_sock_acl_pool"); SYSINIT_PANIC_ASSERT(rc == 0); rc = os_mbuf_pool_init(&ble_hci_sock_acl_mbuf_pool, &ble_hci_sock_acl_pool, - acl_block_size, + ACL_BLOCK_SIZE, MYNEWT_VAL(BLE_ACL_BUF_COUNT)); SYSINIT_PANIC_ASSERT(rc == 0); @@ -793,32 +804,32 @@ ble_hci_sock_init(void) * outstanding command. We decided to keep this a pool in case we allow * allow the controller to handle more than one outstanding command. */ - rc = mem_malloc_mempool(&ble_hci_sock_cmd_pool, - 1, - BLE_HCI_TRANS_CMD_SZ, - "ble_hci_sock_cmd_pool", - &ble_hci_sock_cmd_buf); + rc = os_mempool_init(&ble_hci_sock_cmd_pool, + 1, + BLE_HCI_TRANS_CMD_SZ, + &ble_hci_sock_cmd_buf, + "ble_hci_sock_cmd_pool"); SYSINIT_PANIC_ASSERT(rc == 0); - rc = mem_malloc_mempool(&ble_hci_sock_evt_hi_pool, - MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), - MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), - "ble_hci_sock_evt_hi_pool", - &ble_hci_sock_evt_hi_buf); + rc = os_mempool_init(&ble_hci_sock_evt_hi_pool, + MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), + &ble_hci_sock_evt_hi_buf, + "ble_hci_sock_evt_hi_pool"); SYSINIT_PANIC_ASSERT(rc == 0); - rc = mem_malloc_mempool(&ble_hci_sock_evt_lo_pool, - MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), - MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), - "ble_hci_sock_evt_lo_pool", - &ble_hci_sock_evt_lo_buf); + rc = os_mempool_init(&ble_hci_sock_evt_lo_pool, + MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), + ble_hci_sock_evt_lo_buf, + "ble_hci_sock_evt_lo_pool"); SYSINIT_PANIC_ASSERT(rc == 0); rc = ble_hci_sock_config(); SYSINIT_PANIC_ASSERT_MSG(rc == 0, "Failure configuring socket HCI"); rc = stats_init_and_reg(STATS_HDR(hci_sock_stats), - STATS_SIZE_INIT_PARMS(hci_sock_stats, STATS_SIZE_32), - STATS_NAME_INIT_PARMS(hci_sock_stats), "hci_socket"); + STATS_SIZE_INIT_PARMS(hci_sock_stats, STATS_SIZE_32), + STATS_NAME_INIT_PARMS(hci_sock_stats), "hci_socket"); SYSINIT_PANIC_ASSERT(rc == 0); } diff --git a/nimble/transport/uart/src/ble_hci_uart.c b/nimble/transport/uart/src/ble_hci_uart.c index 883875438..79e10f7fd 100644 --- a/nimble/transport/uart/src/ble_hci_uart.c +++ b/nimble/transport/uart/src/ble_hci_uart.c @@ -82,19 +82,55 @@ static ble_hci_trans_rx_acl_fn *ble_hci_uart_rx_acl_cb; static void *ble_hci_uart_rx_acl_arg; static struct os_mempool ble_hci_uart_evt_hi_pool; -static void *ble_hci_uart_evt_hi_buf; +static os_membuf_t ble_hci_uart_evt_hi_buf[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)) +]; + static struct os_mempool ble_hci_uart_evt_lo_pool; -static void *ble_hci_uart_evt_lo_buf; +static os_membuf_t ble_hci_uart_evt_lo_buf[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE)) +]; static struct os_mempool ble_hci_uart_cmd_pool; -static void *ble_hci_uart_cmd_buf; - -static struct os_mempool ble_hci_uart_pkt_pool; -static void *ble_hci_uart_pkt_buf; +static os_membuf_t ble_hci_uart_cmd_buf[ + OS_MEMPOOL_SIZE(1, BLE_HCI_TRANS_CMD_SZ) +]; static struct os_mbuf_pool ble_hci_uart_acl_mbuf_pool; static struct os_mempool_ext ble_hci_uart_acl_pool; -static void *ble_hci_uart_acl_buf; + +/* + * The MBUF payload size must accommodate the HCI data header size plus the + * maximum ACL data packet length. The ACL block size is the size of the + * mbufs we will allocate. + */ +#define ACL_BLOCK_SIZE OS_ALIGN(MYNEWT_VAL(BLE_ACL_BUF_SIZE) \ + + BLE_MBUF_MEMBLOCK_OVERHEAD \ + + BLE_HCI_DATA_HDR_SZ, OS_ALIGNMENT) + +static os_membuf_t ble_hci_uart_acl_buf[ + OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_ACL_BUF_COUNT), + ACL_BLOCK_SIZE) +]; + +/** + * A packet to be sent over the UART. This can be a command, an event, or ACL + * data. + */ +struct ble_hci_uart_pkt { + STAILQ_ENTRY(ble_hci_uart_pkt) next; + void *data; + uint8_t type; +}; + +static struct os_mempool ble_hci_uart_pkt_pool; +static os_membuf_t ble_hci_uart_pkt_buf[ + OS_MEMPOOL_SIZE(BLE_HCI_UART_EVT_COUNT + 1 + + MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT), + sizeof (struct ble_hci_uart_pkt)) +]; /** * An incoming or outgoing command or event. @@ -115,16 +151,6 @@ struct ble_hci_uart_acl { uint16_t rxd_bytes; /* current count of bytes received for packet */ }; -/** - * A packet to be sent over the UART. This can be a command, an event, or ACL - * data. - */ -struct ble_hci_uart_pkt { - STAILQ_ENTRY(ble_hci_uart_pkt) next; - void *data; - uint8_t type; -}; - /** * Structure for transmitting ACL packets over UART * @@ -153,8 +179,6 @@ static struct { STAILQ_HEAD(, ble_hci_uart_pkt) tx_pkts; /* Packet queue to send to UART */ } ble_hci_uart_state; -static uint16_t ble_hci_uart_max_acl_datalen; - /** * Allocates a buffer (mbuf) for ACL operation. * @@ -617,7 +641,7 @@ ble_hci_uart_rx_acl(uint8_t data) * Data portion cannot exceed data length of acl buffer. If it does * this is considered to be a loss of sync. */ - if (pktlen > ble_hci_uart_max_acl_datalen) { + if (pktlen > MYNEWT_VAL(BLE_ACL_BUF_SIZE)) { os_mbuf_free_chain(ble_hci_uart_state.rx_acl.buf); #if MYNEWT_VAL(BLE_DEVICE) ble_hci_uart_sync_lost(); @@ -1023,33 +1047,21 @@ ble_hci_trans_reset(void) void ble_hci_uart_init(void) { - int acl_data_length; - int acl_block_size; int rc; /* Ensure this function only gets called by sysinit. */ SYSINIT_ASSERT_ACTIVE(); - /* - * The MBUF payload size must accommodate the HCI data header size plus the - * maximum ACL data packet length. The ACL block size is the size of the - * mbufs we will allocate. - */ - acl_data_length = MYNEWT_VAL(BLE_ACL_BUF_SIZE); - ble_hci_uart_max_acl_datalen = acl_data_length; - acl_block_size = acl_data_length + BLE_MBUF_MEMBLOCK_OVERHEAD + - BLE_HCI_DATA_HDR_SZ; - acl_block_size = OS_ALIGN(acl_block_size, OS_ALIGNMENT); - rc = mem_malloc_mempool_ext(&ble_hci_uart_acl_pool, - MYNEWT_VAL(BLE_ACL_BUF_COUNT), - acl_block_size, - "ble_hci_uart_acl_pool", - &ble_hci_uart_acl_buf); + rc = os_mempool_ext_init(&ble_hci_uart_acl_pool, + MYNEWT_VAL(BLE_ACL_BUF_COUNT), + ACL_BLOCK_SIZE, + ble_hci_uart_acl_buf, + "ble_hci_uart_acl_pool"); SYSINIT_PANIC_ASSERT(rc == 0); rc = os_mbuf_pool_init(&ble_hci_uart_acl_mbuf_pool, &ble_hci_uart_acl_pool.mpe_mp, - acl_block_size, + ACL_BLOCK_SIZE, MYNEWT_VAL(BLE_ACL_BUF_COUNT)); SYSINIT_PANIC_ASSERT(rc == 0); @@ -1059,25 +1071,25 @@ ble_hci_uart_init(void) * outstanding command. We decided to keep this a pool in case we allow * allow the controller to handle more than one outstanding command. */ - rc = mem_malloc_mempool(&ble_hci_uart_cmd_pool, - 1, - BLE_HCI_TRANS_CMD_SZ, - "ble_hci_uart_cmd_pool", - &ble_hci_uart_cmd_buf); + rc = os_mempool_init(&ble_hci_uart_cmd_pool, + 1, + BLE_HCI_TRANS_CMD_SZ, + ble_hci_uart_cmd_buf, + "ble_hci_uart_cmd_pool"); SYSINIT_PANIC_ASSERT(rc == 0); - rc = mem_malloc_mempool(&ble_hci_uart_evt_hi_pool, - MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), - MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), - "ble_hci_uart_evt_hi_pool", - &ble_hci_uart_evt_hi_buf); + rc = os_mempool_init(&ble_hci_uart_evt_hi_pool, + MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), + ble_hci_uart_evt_hi_buf, + "ble_hci_uart_evt_hi_pool"); SYSINIT_PANIC_ASSERT(rc == 0); - rc = mem_malloc_mempool(&ble_hci_uart_evt_lo_pool, - MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), - MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), - "ble_hci_uart_evt_lo_pool", - &ble_hci_uart_evt_lo_buf); + rc = os_mempool_init(&ble_hci_uart_evt_lo_pool, + MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT), + MYNEWT_VAL(BLE_HCI_EVT_BUF_SIZE), + ble_hci_uart_evt_lo_buf, + "ble_hci_uart_evt_lo_pool"); SYSINIT_PANIC_ASSERT(rc == 0); /* @@ -1086,12 +1098,12 @@ ble_hci_uart_init(void) * and lo), the number of command buffers (currently 1) and the total * number of buffers that the controller could possibly hand to the host. */ - rc = mem_malloc_mempool(&ble_hci_uart_pkt_pool, - BLE_HCI_UART_EVT_COUNT + 1 + - MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT), - sizeof (struct ble_hci_uart_pkt), - "ble_hci_uart_pkt_pool", - &ble_hci_uart_pkt_buf); + rc = os_mempool_init(&ble_hci_uart_pkt_pool, + BLE_HCI_UART_EVT_COUNT + 1 + + MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT), + sizeof (struct ble_hci_uart_pkt), + ble_hci_uart_pkt_buf, + "ble_hci_uart_pkt_pool"); SYSINIT_PANIC_ASSERT(rc == 0); rc = ble_hci_uart_config();