ble - Transports return BLE_ERR_ return codes. Prior to this change, the HCI transports return code behavior was inconsistent. Sometimes BLE_ERR codes were returned; other times plain errno values were returned.
Now only BLE_ERR error codes are returned. Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/b077f8a9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/b077f8a9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/b077f8a9 Branch: refs/heads/develop Commit: b077f8a9ccd5dab21b62159a3c2092244d7951b7 Parents: e56d563 Author: Christopher Collins <[email protected]> Authored: Tue Aug 9 10:33:36 2016 -0700 Committer: Christopher Collins <[email protected]> Committed: Tue Aug 9 11:00:28 2016 -0700 ---------------------------------------------------------------------- net/nimble/include/nimble/ble.h | 2 ++ net/nimble/src/ble_util.c | 43 +++++++++++++++++++++++ net/nimble/transport/ram/src/ble_hci_ram.c | 42 ++++++++-------------- net/nimble/transport/uart/src/ble_hci_uart.c | 40 +++++++-------------- 4 files changed, 73 insertions(+), 54 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b077f8a9/net/nimble/include/nimble/ble.h ---------------------------------------------------------------------- diff --git a/net/nimble/include/nimble/ble.h b/net/nimble/include/nimble/ble.h index 4deb9f3..6c4c90e 100644 --- a/net/nimble/include/nimble/ble.h +++ b/net/nimble/include/nimble/ble.h @@ -227,4 +227,6 @@ enum ble_error_codes #define BLE_ADDR_TYPE_RPA_PUB_DEFAULT (2) #define BLE_ADDR_TYPE_RPA_RND_DEFAULT (3) +int ble_err_from_os(int os_err); + #endif /* H_BLE_ */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b077f8a9/net/nimble/src/ble_util.c ---------------------------------------------------------------------- diff --git a/net/nimble/src/ble_util.c b/net/nimble/src/ble_util.c new file mode 100644 index 0000000..bfe7083 --- /dev/null +++ b/net/nimble/src/ble_util.c @@ -0,0 +1,43 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "os/os.h" +#include "nimble/ble.h" + +/** + * Converts an OS error code to its equivalent BLE_ERR code. + * + * @param os_err The OS error code to convert. + * + * @return The equivalent BLE_ERR code. + */ +int +ble_err_from_os(int os_err) +{ + switch (os_err) { + case 0: + return 0; + + case OS_ENOMEM: + return BLE_ERR_MEM_CAPACITY; + + default: + return BLE_ERR_UNSPECIFIED; + } +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b077f8a9/net/nimble/transport/ram/src/ble_hci_ram.c ---------------------------------------------------------------------- diff --git a/net/nimble/transport/ram/src/ble_hci_ram.c b/net/nimble/transport/ram/src/ble_hci_ram.c index 23a375d..2ce5fd6 100644 --- a/net/nimble/transport/ram/src/ble_hci_ram.c +++ b/net/nimble/transport/ram/src/ble_hci_ram.c @@ -2,6 +2,8 @@ #include <errno.h> #include <stddef.h> #include "os/os.h" +#include "util/mem.h" +#include "nimble/ble.h" #include "nimble/ble_hci_trans.h" #include "transport/ram/ble_hci_ram.h" @@ -191,43 +193,29 @@ ble_hci_ram_init(const struct ble_hci_ram_cfg *cfg) ble_hci_ram_free_mem(); - if (cfg->num_evt_hi_bufs > 0) { - ble_hci_ram_evt_hi_buf = malloc(OS_MEMPOOL_BYTES(cfg->num_evt_hi_bufs, - cfg->evt_buf_sz)); - if (ble_hci_ram_evt_hi_buf == NULL) { - rc = ENOMEM; - goto err; - } - } - - rc = os_mempool_init(&ble_hci_ram_evt_hi_pool, cfg->num_evt_hi_bufs, - cfg->evt_buf_sz, ble_hci_ram_evt_hi_buf, - "ble_hci_ram_evt_hi_pool"); + rc = mem_malloc_mempool(&ble_hci_ram_evt_hi_pool, + cfg->num_evt_hi_bufs, + cfg->evt_buf_sz, + "ble_hci_ram_evt_hi_pool", + &ble_hci_ram_evt_hi_buf); if (rc != 0) { - rc = EINVAL; + rc = ble_err_from_os(rc); goto err; } - if (cfg->num_evt_lo_bufs > 0) { - ble_hci_ram_evt_lo_buf = malloc(OS_MEMPOOL_BYTES(cfg->num_evt_lo_bufs, - cfg->evt_buf_sz)); - if (ble_hci_ram_evt_lo_buf == NULL) { - rc = ENOMEM; - goto err; - } - } - - rc = os_mempool_init(&ble_hci_ram_evt_lo_pool, cfg->num_evt_lo_bufs, - cfg->evt_buf_sz, ble_hci_ram_evt_lo_buf, - "ble_hci_ram_evt_lo_pool"); + rc = mem_malloc_mempool(&ble_hci_ram_evt_lo_pool, + cfg->num_evt_lo_bufs, + cfg->evt_buf_sz, + "ble_hci_ram_evt_lo_pool", + &ble_hci_ram_evt_lo_buf); if (rc != 0) { - rc = EINVAL; + 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 = ENOMEM; + rc = BLE_ERR_MEM_CAPACITY; goto err; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b077f8a9/net/nimble/transport/uart/src/ble_hci_uart.c ---------------------------------------------------------------------- diff --git a/net/nimble/transport/uart/src/ble_hci_uart.c b/net/nimble/transport/uart/src/ble_hci_uart.c index 1285f56..0fa982a 100755 --- a/net/nimble/transport/uart/src/ble_hci_uart.c +++ b/net/nimble/transport/uart/src/ble_hci_uart.c @@ -23,7 +23,7 @@ #include <errno.h> #include "bsp/bsp.h" #include "os/os.h" -#include "bsp/bsp.h" +#include "util/mem.h" #include "hal/hal_gpio.h" #include "hal/hal_cputime.h" #include "hal/hal_uart.h" @@ -705,39 +705,25 @@ ble_hci_uart_init(const struct ble_hci_uart_cfg *cfg) ble_hci_uart_cfg = *cfg; - ble_hci_uart_evt_buf = malloc( - OS_MEMPOOL_BYTES(ble_hci_uart_cfg.num_evt_bufs, - ble_hci_uart_cfg.evt_buf_sz)); - if (ble_hci_uart_evt_buf == NULL) { - rc = BLE_ERR_MEM_CAPACITY; - goto err; - } - /* Create memory pool of HCI command / event buffers */ - rc = os_mempool_init(&ble_hci_uart_evt_pool, ble_hci_uart_cfg.num_evt_bufs, - ble_hci_uart_cfg.evt_buf_sz, ble_hci_uart_evt_buf, - "ble_hci_uart_evt_pool"); + rc = mem_malloc_mempool(&ble_hci_uart_evt_pool, + cfg->num_evt_bufs, + cfg->evt_buf_sz, + "ble_hci_uart_evt_pool", + &ble_hci_uart_evt_buf); if (rc != 0) { - rc = BLE_ERR_UNSPECIFIED; - goto err; - } - - ble_hci_uart_pkt_buf = malloc( - OS_MEMPOOL_BYTES(ble_hci_uart_cfg.num_evt_bufs, - sizeof (struct os_event))); - if (ble_hci_uart_pkt_buf == NULL) { - rc = BLE_ERR_MEM_CAPACITY; + rc = ble_err_from_os(rc); goto err; } /* Create memory pool of packet list nodes. */ - rc = os_mempool_init(&ble_hci_uart_pkt_pool, - ble_hci_uart_cfg.num_evt_bufs, - sizeof (struct ble_hci_uart_pkt), - ble_hci_uart_pkt_buf, - "ble_hci_uart_pkt_pool"); + rc = mem_malloc_mempool(&ble_hci_uart_pkt_pool, + cfg->num_evt_bufs, + sizeof (struct ble_hci_uart_pkt), + "ble_hci_uart_pkt_pool", + &ble_hci_uart_pkt_buf); if (rc != 0) { - rc = BLE_ERR_UNSPECIFIED; + rc = ble_err_from_os(rc); goto err; }
