You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2016/08/09 18:04:45 UTC

[1/2] incubator-mynewt-core git commit: util - Functions to alloc mempools and mbuf pools.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop 51294d51a -> b077f8a9c


util - Functions to alloc mempools and mbuf pools.


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/e56d563a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/e56d563a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/e56d563a

Branch: refs/heads/develop
Commit: e56d563aa6c0bd092c59cc3edb9805a0b33ce5e1
Parents: 51294d5
Author: Christopher Collins <cc...@apache.org>
Authored: Mon Aug 8 15:57:20 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Tue Aug 9 10:58:39 2016 -0700

----------------------------------------------------------------------
 libs/util/include/util/mem.h |  38 ++++++++++++
 libs/util/src/mem.c          | 126 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 164 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e56d563a/libs/util/include/util/mem.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/mem.h b/libs/util/include/util/mem.h
new file mode 100644
index 0000000..52ddce4
--- /dev/null
+++ b/libs/util/include/util/mem.h
@@ -0,0 +1,38 @@
+/**
+ * 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.
+ */
+
+#ifndef H_UTIL_MEM_
+#define H_UTIL_MEM_
+
+struct os_mempool;
+struct os_mbuf_pool;
+
+int mem_malloc_mempool(struct os_mempool *mempool, int num_blocks,
+                       int block_size, char *name, void **out_buf);
+
+int mem_malloc_mbuf_pool(struct os_mempool *mempool,
+                         struct os_mbuf_pool *mbuf_pool, int num_blocks,
+                         int block_size, char *name,
+                         void **out_buf);
+int mem_malloc_mbufpkt_pool(struct os_mempool *mempool,
+                            struct os_mbuf_pool *mbuf_pool, int num_blocks,
+                            int block_size, char *name,
+                            void **out_buf);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e56d563a/libs/util/src/mem.c
----------------------------------------------------------------------
diff --git a/libs/util/src/mem.c b/libs/util/src/mem.c
new file mode 100644
index 0000000..eeca71f
--- /dev/null
+++ b/libs/util/src/mem.c
@@ -0,0 +1,126 @@
+/**
+ * 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"
+
+/**
+ * Mallocs a block of memory and initializes a mempool to use it.
+ *
+ * @param mempool               The mempool to initialize.
+ * @param num_blocks            The total number of memory blocks in the
+ *                                  mempool.
+ * @param block_size            The size of each mempool entry.
+ * @param name                  The name to give the mempool.
+ * @param out_buf               On success, this points to the malloced memory.
+ *                                  Pass NULL if you don't need this
+ *                                  information.
+ *
+ * @return                      0 on success;
+ *                              OS_ENOMEM on malloc failure;
+ *                              Other OS code on unexpected error.
+ */
+int
+mem_malloc_mempool(struct os_mempool *mempool, int num_blocks, int block_size,
+                   char *name, void **out_buf)
+{
+    void *buf;
+    int rc;
+
+    block_size = OS_ALIGN(block_size, OS_ALIGNMENT);
+
+    if (num_blocks > 0) {
+        buf = malloc(OS_MEMPOOL_BYTES(num_blocks, block_size));
+        if (buf == NULL) {
+            return OS_ENOMEM;
+        }
+    } else {
+        buf = NULL;
+    }
+
+    rc = os_mempool_init(mempool, num_blocks, block_size, buf, name);
+    if (rc != 0) {
+        free(buf);
+        return rc;
+    }
+
+    if (out_buf != NULL) {
+        *out_buf = buf;
+    }
+
+    return 0;
+}
+
+/**
+ * Mallocs a block of memory and initializes an mbuf pool to use it.
+ *
+ * @param mempool               The mempool to initialize.
+ * @param mbuf_pool             The mbuf pool to initialize.
+ * @param num_blocks            The total number of mbufs in the pool.
+ * @param block_size            The size of each mbuf.
+ * @param name                  The name to give the mempool.
+ * @param out_buf               On success, this points to the malloced memory.
+ *                                  Pass NULL if you don't need this
+ *                                  information.
+ *
+ * @return                      0 on success;
+ *                              OS_ENOMEM on malloc failure;
+ *                              Other OS code on unexpected error.
+ */
+int
+mem_malloc_mbuf_pool(struct os_mempool *mempool,
+                     struct os_mbuf_pool *mbuf_pool, int num_blocks,
+                     int block_size, char *name,
+                     void **out_buf)
+{
+    void *buf;
+    int rc;
+
+    block_size = OS_ALIGN(block_size + sizeof (struct os_mbuf), OS_ALIGNMENT);
+
+    rc = mem_malloc_mempool(mempool, num_blocks, block_size, name, &buf);
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = os_mbuf_pool_init(mbuf_pool, mempool, block_size, num_blocks);
+    if (rc != 0) {
+        free(buf);
+        return rc;
+    }
+
+    if (out_buf != NULL) {
+        *out_buf = buf;
+    }
+
+    return 0;
+}
+
+int
+mem_malloc_mbufpkt_pool(struct os_mempool *mempool,
+                        struct os_mbuf_pool *mbuf_pool, int num_blocks,
+                        int block_size, char *name,
+                        void **out_buf)
+{
+    int rc;
+
+    rc = mem_malloc_mbuf_pool(mempool, mbuf_pool, num_blocks,
+                              block_size + sizeof (struct os_mbuf_pkthdr),
+                              name, out_buf);
+    return rc;
+}


[2/2] incubator-mynewt-core git commit: ble - Transports return BLE_ERR_ return codes.

Posted by cc...@apache.org.
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 <cc...@apache.org>
Authored: Tue Aug 9 10:33:36 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
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;
     }