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/09/13 03:41:54 UTC

[01/41] incubator-mynewt-core git commit: syscfg / sysinit

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/sterly_refactor fdd134d75 -> d98ddc1c5


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/reboot/src/log_reboot.c
----------------------------------------------------------------------
diff --git a/sys/reboot/src/log_reboot.c b/sys/reboot/src/log_reboot.c
index 470d300..c7c3bbe 100644
--- a/sys/reboot/src/log_reboot.c
+++ b/sys/reboot/src/log_reboot.c
@@ -17,23 +17,21 @@
  * under the License.
  */
 
-#include <os/os.h>
-#include <fcb/fcb.h>
-#include <console/console.h>
-#include "log/log.h"
-#include <bootutil/image.h>
-#include <bootutil/bootutil_misc.h>
-#include <imgmgr/imgmgr.h>
 #include <string.h>
-#include <config/config.h>
-#include <config/config_file.h>
-#include <reboot/log_reboot.h>
-#include <bsp/bsp.h>
 #include <assert.h>
-
-#ifdef SHELL_PRESENT
-#include <shell/shell.h>
-#endif
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
+#include "os/os.h"
+#include "fcb/fcb.h"
+#include "console/console.h"
+#include "log/log.h"
+#include "bootutil/image.h"
+#include "bootutil/bootutil_misc.h"
+#include "imgmgr/imgmgr.h"
+#include "config/config.h"
+#include "config/config_file.h"
+#include "reboot/log_reboot.h"
+#include "bsp/bsp.h"
 
 static struct log_handler reboot_log_handler;
 static struct fcb fcb;
@@ -202,3 +200,17 @@ reboot_cnt_set(int argc, char **argv, char *val)
 err:
     return OS_ENOENT;
 }
+
+void
+log_reboot_pkg_init(void)
+{
+    int rc;
+
+    (void)rc;
+
+#if MYNEWT_VAL(REBOOT_LOG_0_TYPE)
+    rc = reboot_init_handler(MYNEWT_VAL(REBOOT_LOG_0_TYPE),
+                             MYNEWT_VAL(REBOOT_LOG_0_ENTRY_COUNT));
+    SYSINIT_PANIC_ASSERT(rc == 0);
+#endif
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/stats/include/stats/stats.h
----------------------------------------------------------------------
diff --git a/sys/stats/include/stats/stats.h b/sys/stats/include/stats/stats.h
index 078b99a..7e5811e 100644
--- a/sys/stats/include/stats/stats.h
+++ b/sys/stats/include/stats/stats.h
@@ -19,8 +19,9 @@
 #ifndef __UTIL_STATS_H__ 
 #define __UTIL_STATS_H__ 
 
-#include <os/queue.h>
 #include <stdint.h>
+#include "syscfg/syscfg.h"
+#include "os/queue.h"
 
 struct stats_name_map {
     uint16_t snm_off;
@@ -32,7 +33,7 @@ struct stats_hdr {
     uint8_t s_size;
     uint8_t s_cnt;
     uint16_t s_pad1;
-#ifdef STATS_NAME_ENABLE
+#if MYNEWT_VAL(STATS_NAMES)
     struct stats_name_map *s_map;
     int s_map_cnt;
 #endif
@@ -72,7 +73,7 @@ STATS_SECT_DECL(__name) {                   \
 #define STATS_INCN(__sectvarname, __var, __n)  \
     ((__sectvarname).STATS_SECT_VAR(__var) += (__n))
 
-#ifdef STATS_NAME_ENABLE
+#if MYNEWT_VAL(STATS_NAMES)
 
 #define STATS_NAME_MAP_NAME(__sectname) g_stats_map_ ## __sectname
 
@@ -90,17 +91,16 @@ struct stats_name_map STATS_NAME_MAP_NAME(__sectname)[] = {
     &(STATS_NAME_MAP_NAME(__name)[0]),                                      \
     (sizeof(STATS_NAME_MAP_NAME(__name)) / sizeof(struct stats_name_map))
 
-#else /* STATS_NAME_ENABLE */
+#else /* MYNEWT_VAL(STATS_NAME) */
 
 #define STATS_NAME_START(__name)
 #define STATS_NAME(__name, __entry)
 #define STATS_NAME_END(__name)
 #define STATS_NAME_INIT_PARMS(__name) NULL, 0
 
-#endif /* STATS_NAME_ENABLE */
+#endif /* MYNEWT_VAL(STATS_NAME) */
 
-int stats_module_init(void);
-void stats_module_reset(void);
+void stats_module_init(void);
 int stats_init(struct stats_hdr *shdr, uint8_t size, uint8_t cnt, 
     struct stats_name_map *map, uint8_t map_cnt);
 int stats_register(char *name, struct stats_hdr *shdr);
@@ -118,10 +118,10 @@ int stats_group_walk(stats_group_walk_func_t, void *);
 struct stats_hdr *stats_group_find(char *name);
 
 /* Private */
-#ifdef NEWTMGR_PRESENT 
+#if MYNEWT_VAL(STATS_NEWTMGR)
 int stats_nmgr_register_group(void);
 #endif 
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(STATS_CLI)
 int stats_shell_register(void);
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/stats/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/stats/pkg.yml b/sys/stats/pkg.yml
index 309dbd6..c23bedc 100644
--- a/sys/stats/pkg.yml
+++ b/sys/stats/pkg.yml
@@ -27,12 +27,21 @@ pkg.keywords:
 pkg.deps:
     - libs/os
     - libs/util
-    - libs/testutil
-pkg.deps.SHELL:
+pkg.deps.STATS_CLI:
     - libs/shell
-pkg.deps.NEWTMGR:
+pkg.deps.STATS_NEWTMGR:
     - libs/newtmgr
-pkg.req_apis.SHELL:
-    - console
-pkg.cflags.SHELL: -DSHELL_PRESENT
-pkg.cflags.NEWTMGR: -DNEWTMGR_PRESENT 
+
+pkg.init_function: stats_module_init
+pkg.init_stage: 0
+
+pkg.syscfg_defs:
+    STATS_NAMES:
+        description: 'TBD'
+        value: 1
+    STATS_CLI:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_LIBS_SHELL'
+    STATS_NEWTMGR:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_LIBS_NEWTMGR'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/stats/src/stats.c
----------------------------------------------------------------------
diff --git a/sys/stats/src/stats.c b/sys/stats/src/stats.c
index a1f785c..d9dea44 100644
--- a/sys/stats/src/stats.c
+++ b/sys/stats/src/stats.c
@@ -17,14 +17,15 @@
  * under the License.
  */
 
-#include <os/os.h>
-
+#include <assert.h>
 #include <string.h>
+#include <stdio.h>
 
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
+#include "os/os.h"
 #include "stats/stats.h"
 
-#include <stdio.h>
-
 STATS_SECT_START(stats)
     STATS_SECT_ENTRY(num_registered)
 STATS_SECT_END
@@ -38,8 +39,6 @@ STATS_NAME_END(stats)
 STAILQ_HEAD(, stats_hdr) g_stats_registry =
     STAILQ_HEAD_INITIALIZER(g_stats_registry);
 
-static uint8_t stats_module_inited;
-
 int
 stats_walk(struct stats_hdr *hdr, stats_walk_func_t walk_func, void *arg)
 {
@@ -50,7 +49,7 @@ stats_walk(struct stats_hdr *hdr, stats_walk_func_t walk_func, void *arg)
     int ent_n;
     int len;
     int rc;
-#ifdef STATS_NAME_ENABLE
+#if MYNEWT_VAL(STATS_NAMES)
     int i;
 #endif
 
@@ -63,7 +62,7 @@ stats_walk(struct stats_hdr *hdr, stats_walk_func_t walk_func, void *arg)
          * walk function
          */
         name = NULL;
-#ifdef STATS_NAME_ENABLE
+#if MYNEWT_VAL(STATS_NAMES)
         for (i = 0; i < hdr->s_map_cnt; ++i) {
             if (hdr->s_map[i].snm_off == cur) {
                 name = hdr->s_map[i].snm_name;
@@ -92,57 +91,30 @@ err:
 }
 
 
-int
+void
 stats_module_init(void)
 {
     int rc;
 
-    if (stats_module_inited) {
-        return 0;
-    }
-    stats_module_inited = 1;
+    STAILQ_INIT(&g_stats_registry);
 
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(STATS_CLI)
     rc = stats_shell_register();
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 #endif
 
-#ifdef NEWTMGR_PRESENT
+#if MYNEWT_VAL(STATS_NEWTMGR)
     rc = stats_nmgr_register_group();
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 #endif
 
     rc = stats_init(STATS_HDR(g_stats_stats),
                     STATS_SIZE_INIT_PARMS(g_stats_stats, STATS_SIZE_32),
                     STATS_NAME_INIT_PARMS(stats));
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = stats_register("stat", STATS_HDR(g_stats_stats));
-    if (rc != 0) {
-        goto err;
-    }
-
-    return (0);
-err:
-    return (rc);
-}
-
-/**
- * Uninitializes all statistic sections.  This is likely only useful for unit
- * tests that need to run in sequence.
- */
-void
-stats_module_reset(void)
-{
-    stats_module_inited = 0;
-
-    STAILQ_INIT(&g_stats_registry);
+    SYSINIT_PANIC_ASSERT(rc == 0);
 }
 
 int
@@ -153,7 +125,7 @@ stats_init(struct stats_hdr *shdr, uint8_t size, uint8_t cnt,
 
     shdr->s_size = size;
     shdr->s_cnt = cnt;
-#ifdef STATS_NAME_ENABLE
+#if MYNEWT_VAL(STATS_NAMES)
     shdr->s_map = map;
     shdr->s_map_cnt = map_cnt;
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/stats/src/stats_nmgr.c
----------------------------------------------------------------------
diff --git a/sys/stats/src/stats_nmgr.c b/sys/stats/src/stats_nmgr.c
index c7b8dff..2ffdb8f 100644
--- a/sys/stats/src/stats_nmgr.c
+++ b/sys/stats/src/stats_nmgr.c
@@ -17,14 +17,14 @@
  * under the License.
  */
 
-#include <os/os.h>
-
 #include <string.h>
-
 #include <stdio.h>
 
-#ifdef NEWTMGR_PRESENT
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(STATS_NEWTMGR)
 
+#include "os/os.h"
 #include "newtmgr/newtmgr.h"
 #include "json/json.h"
 #include "stats/stats.h"
@@ -180,4 +180,4 @@ err:
     return (rc);
 }
 
-#endif /* NEWTMGR_PRESENT */
+#endif /* MYNEWT_VAL(STATS_NEWTMGR) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/stats/src/stats_shell.c
----------------------------------------------------------------------
diff --git a/sys/stats/src/stats_shell.c b/sys/stats/src/stats_shell.c
index ebb5969..c44f52d 100644
--- a/sys/stats/src/stats_shell.c
+++ b/sys/stats/src/stats_shell.c
@@ -17,19 +17,18 @@
  * under the License.
  */
 
-#include <os/os.h>
-
-#include <string.h>
-
-#include "stats/stats.h"
+#include "syscfg/syscfg.h"
 
 /* Source code is only included if the shell library is enabled.  Otherwise
  * this file is compiled out for code size.
  */
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(STATS_CLI)
 
-#include <shell/shell.h>
-#include <console/console.h>
+#include <string.h>
+#include "shell/shell.h"
+#include "console/console.h"
+#include "os/os.h"
+#include "stats/stats.h"
 
 static int shell_stats_display(int argc, char **argv);
 static struct shell_cmd shell_stats_cmd = {
@@ -117,4 +116,4 @@ stats_shell_register(void)
 }
 
 
-#endif /* SHELL_PRESENT */
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/sysinit/include/sysinit/sysinit.h
----------------------------------------------------------------------
diff --git a/sys/sysinit/include/sysinit/sysinit.h b/sys/sysinit/include/sysinit/sysinit.h
new file mode 100644
index 0000000..fe86b78
--- /dev/null
+++ b/sys/sysinit/include/sysinit/sysinit.h
@@ -0,0 +1,42 @@
+/**
+ * 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_SYSINIT_
+#define H_SYSINIT_
+
+#include "syscfg/syscfg.h"
+
+typedef void sysinit_panic_fn(const char *file, int line);
+
+#if !MYNEWT_VAL(SYSINIT_PANIC_FN)
+#define SYSINIT_PANIC() assert(0)
+#else
+#define SYSINIT_PANIC() MYNEWT_VAL(SYSINIT_PANIC_FN)(__FILE__, __LINE__)
+#endif
+
+#define SYSINIT_PANIC_ASSERT(rc) do \
+{                                   \
+    if (!(rc)) {                    \
+        SYSINIT_PANIC();            \
+    }                               \
+} while (0)
+
+void sysinit(void);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/sysinit/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/sysinit/pkg.yml b/sys/sysinit/pkg.yml
new file mode 100644
index 0000000..e728ccf
--- /dev/null
+++ b/sys/sysinit/pkg.yml
@@ -0,0 +1,34 @@
+#
+# 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.
+#
+
+pkg.name: sys/sysinit
+pkg.description: XXX
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+    - init
+
+pkg.deps:
+    - libs/os
+    - libs/util
+
+pkg.syscfg_defs:
+    SYSINIT_PANIC_FN:
+        description: 'TBD'
+        value:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/targets/unittest/pkg.yml
----------------------------------------------------------------------
diff --git a/targets/unittest/pkg.yml b/targets/unittest/pkg.yml
index c8b283b..30ad3f4 100644
--- a/targets/unittest/pkg.yml
+++ b/targets/unittest/pkg.yml
@@ -23,3 +23,5 @@ pkg.type: "target"
 pkg.description: "Used for unit tests by the \"newt test\" command."
 pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
+
+pkg.deps: sys/sysinit


[02/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/test/src/conf_test.c
----------------------------------------------------------------------
diff --git a/sys/config/test/src/conf_test.c b/sys/config/test/src/conf_test.c
new file mode 100644
index 0000000..3c432c4
--- /dev/null
+++ b/sys/config/test/src/conf_test.c
@@ -0,0 +1,953 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+
+#include <os/os.h>
+#include <testutil/testutil.h>
+#include <nffs/nffs.h>
+#include <fs/fs.h>
+#include <fs/fsutil.h>
+#include <fcb/fcb.h>
+#include "config/config.h"
+#include "config/config_file.h"
+#include "config/config_fcb.h"
+#include "config_test.h"
+#include "config_priv.h"
+
+static uint8_t val8;
+int c2_var_count = 1;
+static char val_string[64][CONF_MAX_VAL_LEN];
+
+static uint32_t val32;
+
+static int test_get_called;
+static int test_set_called;
+static int test_commit_called;
+static int test_export_block;
+
+static char *ctest_handle_get(int argc, char **argv, char *val,
+  int val_len_max);
+static int ctest_handle_set(int argc, char **argv, char *val);
+static int ctest_handle_commit(void);
+static int ctest_handle_export(void (*cb)(char *name, char *value),
+  enum conf_export_tgt tgt);
+static char *c2_handle_get(int argc, char **argv, char *val,
+  int val_len_max);
+static int c2_handle_set(int argc, char **argv, char *val);
+static int c2_handle_export(void (*cb)(char *name, char *value),
+  enum conf_export_tgt tgt);
+static char *c3_handle_get(int argc, char **argv, char *val,
+  int val_len_max);
+static int c3_handle_set(int argc, char **argv, char *val);
+static int c3_handle_export(void (*cb)(char *name, char *value),
+  enum conf_export_tgt tgt);
+
+struct conf_handler config_test_handler = {
+    .ch_name = "myfoo",
+    .ch_get = ctest_handle_get,
+    .ch_set = ctest_handle_set,
+    .ch_commit = ctest_handle_commit,
+    .ch_export = ctest_handle_export
+};
+
+static char *
+ctest_handle_get(int argc, char **argv, char *val, int val_len_max)
+{
+    test_get_called = 1;
+    if (argc == 1 && !strcmp(argv[0], "mybar")) {
+        return conf_str_from_value(CONF_INT8, &val8, val, val_len_max);
+    }
+    return NULL;
+}
+
+static int
+ctest_handle_set(int argc, char **argv, char *val)
+{
+    uint8_t newval;
+    int rc;
+
+    test_set_called = 1;
+    if (argc == 1 && !strcmp(argv[0], "mybar")) {
+        rc = CONF_VALUE_SET(val, CONF_INT8, newval);
+        TEST_ASSERT(rc == 0);
+        val8 = newval;
+        return 0;
+    }
+    return OS_ENOENT;
+}
+
+static int
+ctest_handle_commit(void)
+{
+    test_commit_called = 1;
+    return 0;
+}
+
+static int
+ctest_handle_export(void (*cb)(char *name, char *value),
+  enum conf_export_tgt tgt)
+{
+    char value[32];
+
+    if (test_export_block) {
+        return 0;
+    }
+    conf_str_from_value(CONF_INT8, &val8, value, sizeof(value));
+    cb("myfoo/mybar", value);
+
+    return 0;
+}
+
+struct conf_handler c2_test_handler = {
+    .ch_name = "2nd",
+    .ch_get = c2_handle_get,
+    .ch_set = c2_handle_set,
+    .ch_commit = NULL,
+    .ch_export = c2_handle_export
+};
+
+char *
+c2_var_find(char *name)
+{
+    int idx = 0;
+    int len;
+    char *eptr;
+
+    len = strlen(name);
+    TEST_ASSERT(!strncmp(name, "string", 6));
+    TEST_ASSERT(len > 6);
+
+    idx = strtoul(&name[6], &eptr, 10);
+    TEST_ASSERT(*eptr == '\0');
+    TEST_ASSERT(idx < c2_var_count);
+    return val_string[idx];
+}
+
+static char *
+c2_handle_get(int argc, char **argv, char *val, int val_len_max)
+{
+    int len;
+    char *valptr;
+
+    if (argc == 1) {
+        valptr = c2_var_find(argv[0]);
+        if (!valptr) {
+            return NULL;
+        }
+        len = strlen(val_string[0]);
+        if (len > val_len_max) {
+            len = val_len_max;
+        }
+        strncpy(val, valptr, len);
+    }
+    return NULL;
+}
+
+static int
+c2_handle_set(int argc, char **argv, char *val)
+{
+    char *valptr;
+
+    if (argc == 1) {
+        valptr = c2_var_find(argv[0]);
+        if (!valptr) {
+            return OS_ENOENT;
+        }
+        if (val) {
+            strncpy(valptr, val, sizeof(val_string[0]));
+        } else {
+            memset(valptr, 0, sizeof(val_string[0]));
+        }
+        return 0;
+    }
+    return OS_ENOENT;
+}
+
+static int
+c2_handle_export(void (*cb)(char *name, char *value),
+  enum conf_export_tgt tgt)
+{
+    int i;
+    char name[32];
+
+    for (i = 0; i < c2_var_count; i++) {
+        snprintf(name, sizeof(name), "2nd/string%d", i);
+        cb(name, val_string[i]);
+    }
+    return 0;
+}
+
+struct conf_handler c3_test_handler = {
+    .ch_name = "3",
+    .ch_get = c3_handle_get,
+    .ch_set = c3_handle_set,
+    .ch_commit = NULL,
+    .ch_export = c3_handle_export
+};
+
+static char *
+c3_handle_get(int argc, char **argv, char *val, int val_len_max)
+{
+    if (argc == 1 && !strcmp(argv[0], "v")) {
+        return conf_str_from_value(CONF_INT32, &val32, val, val_len_max);
+    }
+    return NULL;
+}
+
+static int
+c3_handle_set(int argc, char **argv, char *val)
+{
+    uint32_t newval;
+    int rc;
+
+    if (argc == 1 && !strcmp(argv[0], "v")) {
+        rc = CONF_VALUE_SET(val, CONF_INT32, newval);
+        TEST_ASSERT(rc == 0);
+        val32 = newval;
+        return 0;
+    }
+    return OS_ENOENT;
+}
+
+static int
+c3_handle_export(void (*cb)(char *name, char *value),
+  enum conf_export_tgt tgt)
+{
+    char value[32];
+
+    conf_str_from_value(CONF_INT32, &val32, value, sizeof(value));
+    cb("3/v", value);
+
+    return 0;
+}
+
+static void
+ctest_clear_call_state(void)
+{
+    test_get_called = 0;
+    test_set_called = 0;
+    test_commit_called = 0;
+}
+
+static int
+ctest_get_call_state(void)
+{
+    return test_get_called + test_set_called + test_commit_called;
+}
+
+TEST_CASE(config_empty_lookups)
+{
+    int rc;
+    char name[80];
+    char tmp[64], *str;
+
+    strcpy(name, "foo/bar");
+    rc = conf_set_value(name, "tmp");
+    TEST_ASSERT(rc != 0);
+
+    strcpy(name, "foo/bar");
+    str = conf_get_value(name, tmp, sizeof(tmp));
+    TEST_ASSERT(str == NULL);
+}
+
+TEST_CASE(config_test_insert)
+{
+    int rc;
+
+    rc = conf_register(&config_test_handler);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(config_test_insert2)
+{
+    int rc;
+
+    rc = conf_register(&c2_test_handler);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(config_test_getset_unknown)
+{
+    char name[80];
+    char tmp[64], *str;
+    int rc;
+
+    strcpy(name, "foo/bar");
+    rc = conf_set_value(name, "tmp");
+    TEST_ASSERT(rc != 0);
+    TEST_ASSERT(ctest_get_call_state() == 0);
+
+    strcpy(name, "foo/bar");
+    str = conf_get_value(name, tmp, sizeof(tmp));
+    TEST_ASSERT(str == NULL);
+    TEST_ASSERT(ctest_get_call_state() == 0);
+
+    strcpy(name, "myfoo/bar");
+    rc = conf_set_value(name, "tmp");
+    TEST_ASSERT(rc == OS_ENOENT);
+    TEST_ASSERT(test_set_called == 1);
+    ctest_clear_call_state();
+
+    strcpy(name, "myfoo/bar");
+    str = conf_get_value(name, tmp, sizeof(tmp));
+    TEST_ASSERT(str == NULL);
+    TEST_ASSERT(test_get_called == 1);
+    ctest_clear_call_state();
+}
+
+TEST_CASE(config_test_getset_int)
+{
+    char name[80];
+    char tmp[64], *str;
+    int rc;
+
+    strcpy(name, "myfoo/mybar");
+    rc = conf_set_value(name, "42");
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(test_set_called == 1);
+    TEST_ASSERT(val8 == 42);
+    ctest_clear_call_state();
+
+    strcpy(name, "myfoo/mybar");
+    str = conf_get_value(name, tmp, sizeof(tmp));
+    TEST_ASSERT(str);
+    TEST_ASSERT(test_get_called == 1);
+    TEST_ASSERT(!strcmp("42", tmp));
+    ctest_clear_call_state();
+}
+
+TEST_CASE(config_test_getset_bytes)
+{
+    char orig[32];
+    char bytes[32];
+    char str[48];
+    char *ret;
+    int j, i;
+    int tmp;
+    int rc;
+
+    for (j = 1; j < sizeof(orig); j++) {
+        for (i = 0; i < j; i++) {
+            orig[i] = i + j + 1;
+        }
+        ret = conf_str_from_bytes(orig, j, str, sizeof(str));
+        TEST_ASSERT(ret);
+        tmp = strlen(str);
+        TEST_ASSERT(tmp < sizeof(str));
+
+        memset(bytes, 0, sizeof(bytes));
+        tmp = sizeof(bytes);
+
+        tmp = sizeof(bytes);
+        rc = conf_bytes_from_str(str, bytes, &tmp);
+        TEST_ASSERT(rc == 0);
+        TEST_ASSERT(tmp == j);
+        TEST_ASSERT(!memcmp(orig, bytes, j));
+    }
+}
+
+TEST_CASE(config_test_commit)
+{
+    char name[80];
+    int rc;
+
+    strcpy(name, "bar");
+    rc = conf_commit(name);
+    TEST_ASSERT(rc);
+    TEST_ASSERT(ctest_get_call_state() == 0);
+
+    rc = conf_commit(NULL);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(test_commit_called == 1);
+    ctest_clear_call_state();
+
+    strcpy(name, "myfoo");
+    rc = conf_commit(name);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(test_commit_called == 1);
+    ctest_clear_call_state();
+}
+
+static const struct nffs_area_desc config_nffs[] = {
+    { 0x00000000, 16 * 1024 },
+    { 0x00004000, 16 * 1024 },
+    { 0x00008000, 16 * 1024 },
+    { 0x0000c000, 16 * 1024 },
+    { 0, 0 }
+};
+
+TEST_CASE(config_setup_nffs)
+{
+    int rc;
+
+    rc = nffs_init();
+    TEST_ASSERT_FATAL(rc == 0);
+    rc = nffs_format(config_nffs);
+    TEST_ASSERT_FATAL(rc == 0);
+}
+
+static void config_wipe_srcs(void)
+{
+    SLIST_INIT(&conf_load_srcs);
+    conf_save_dst = NULL;
+}
+
+static void config_wipe_fcb(struct flash_area *fa, int cnt)
+{
+    int i;
+
+    for (i = 0; i < cnt; i++) {
+        flash_area_erase(&fa[i], 0, fa[i].fa_size);
+    }
+}
+
+TEST_CASE(config_test_empty_file)
+{
+    int rc;
+    struct conf_file cf_mfg;
+    struct conf_file cf_running;
+    const char cf_mfg_test[] = "";
+    const char cf_running_test[] = "\n\n";
+
+    config_wipe_srcs();
+
+    cf_mfg.cf_name = "/config/mfg";
+    cf_running.cf_name = "/config/running";
+
+    rc = conf_file_src(&cf_mfg);
+    TEST_ASSERT(rc == 0);
+    rc = conf_file_src(&cf_running);
+
+    /*
+     * No files
+     */
+    conf_load();
+
+    rc = fs_mkdir("/config");
+    TEST_ASSERT(rc == 0);
+
+    rc = fsutil_write_file("/config/mfg", cf_mfg_test, sizeof(cf_mfg_test));
+    TEST_ASSERT(rc == 0);
+
+    rc = fsutil_write_file("/config/running", cf_running_test,
+      sizeof(cf_running_test));
+    TEST_ASSERT(rc == 0);
+
+    conf_load();
+    config_wipe_srcs();
+    ctest_clear_call_state();
+}
+
+TEST_CASE(config_test_small_file)
+{
+    int rc;
+    struct conf_file cf_mfg;
+    struct conf_file cf_running;
+    const char cf_mfg_test[] = "myfoo/mybar=1";
+    const char cf_running_test[] = " myfoo/mybar = 8 ";
+
+    config_wipe_srcs();
+
+    cf_mfg.cf_name = "/config/mfg";
+    cf_running.cf_name = "/config/running";
+
+    rc = conf_file_src(&cf_mfg);
+    TEST_ASSERT(rc == 0);
+    rc = conf_file_src(&cf_running);
+
+    rc = fsutil_write_file("/config/mfg", cf_mfg_test, sizeof(cf_mfg_test));
+    TEST_ASSERT(rc == 0);
+
+    conf_load();
+    TEST_ASSERT(test_set_called);
+    TEST_ASSERT(val8 == 1);
+
+    ctest_clear_call_state();
+
+    rc = fsutil_write_file("/config/running", cf_running_test,
+      sizeof(cf_running_test));
+    TEST_ASSERT(rc == 0);
+
+    conf_load();
+    TEST_ASSERT(test_set_called);
+    TEST_ASSERT(val8 == 8);
+
+    ctest_clear_call_state();
+}
+
+TEST_CASE(config_test_multiple_in_file)
+{
+    int rc;
+    struct conf_file cf_mfg;
+    const char cf_mfg_test1[] =
+      "myfoo/mybar=1\n"
+      "myfoo/mybar=14";
+    const char cf_mfg_test2[] =
+      "myfoo/mybar=1\n"
+      "myfoo/mybar=15\n"
+      "\n";
+
+    config_wipe_srcs();
+
+    cf_mfg.cf_name = "/config/mfg";
+    rc = conf_file_src(&cf_mfg);
+    TEST_ASSERT(rc == 0);
+
+    rc = fsutil_write_file("/config/mfg", cf_mfg_test1, sizeof(cf_mfg_test1));
+    TEST_ASSERT(rc == 0);
+
+    conf_load();
+    TEST_ASSERT(test_set_called);
+    TEST_ASSERT(val8 == 14);
+
+    rc = fsutil_write_file("/config/mfg", cf_mfg_test2, sizeof(cf_mfg_test2));
+    TEST_ASSERT(rc == 0);
+
+    conf_load();
+    TEST_ASSERT(test_set_called);
+    TEST_ASSERT(val8 == 15);
+}
+
+int
+conf_test_file_strstr(const char *fname, char *string)
+{
+    int rc;
+    uint32_t len;
+    uint32_t rlen;
+    char *buf;
+    struct fs_file *file;
+
+    rc = fs_open(fname, FS_ACCESS_READ, &file);
+    if (rc) {
+        return rc;
+    }
+    rc = fs_filelen(file, &len);
+    fs_close(file);
+    if (rc) {
+        return rc;
+    }
+
+    buf = (char *)malloc(len + 1);
+    TEST_ASSERT(buf);
+
+    rc = fsutil_read_file(fname, 0, len, buf, &rlen);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(rlen == len);
+    buf[rlen] = '\0';
+
+    if (strstr(buf, string)) {
+        return 0;
+    } else {
+        return -1;
+    }
+}
+
+TEST_CASE(config_test_save_in_file)
+{
+    int rc;
+    struct conf_file cf;
+
+    config_wipe_srcs();
+
+    rc = fs_mkdir("/config");
+    TEST_ASSERT(rc == 0 || rc == FS_EEXIST);
+
+    cf.cf_name = "/config/blah";
+    rc = conf_file_src(&cf);
+    TEST_ASSERT(rc == 0);
+    rc = conf_file_dst(&cf);
+    TEST_ASSERT(rc == 0);
+
+    val8 = 8;
+    rc = conf_save();
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_test_file_strstr(cf.cf_name, "myfoo/mybar=8\n");
+    TEST_ASSERT(rc == 0);
+
+    val8 = 43;
+    rc = conf_save();
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_test_file_strstr(cf.cf_name, "myfoo/mybar=43\n");
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(config_test_save_one_file)
+{
+    int rc;
+    struct conf_file cf;
+
+    config_wipe_srcs();
+    rc = fs_mkdir("/config");
+    TEST_ASSERT(rc == 0 || rc == FS_EEXIST);
+
+    cf.cf_name = "/config/blah";
+    rc = conf_file_src(&cf);
+    TEST_ASSERT(rc == 0);
+    rc = conf_file_dst(&cf);
+    TEST_ASSERT(rc == 0);
+
+    val8 = 33;
+    rc = conf_save();
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_save_one("myfoo/mybar", "42");
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_load();
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(val8 == 42);
+
+    rc = conf_save_one("myfoo/mybar", "44");
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_load();
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(val8 == 44);
+}
+
+struct flash_area fcb_areas[] = {
+    [0] = {
+        .fa_off = 0x00000000,
+        .fa_size = 16 * 1024
+    },
+    [1] = {
+        .fa_off = 0x00004000,
+        .fa_size = 16 * 1024
+    },
+    [2] = {
+        .fa_off = 0x00008000,
+        .fa_size = 16 * 1024
+    },
+    [3] = {
+        .fa_off = 0x0000c000,
+        .fa_size = 16 * 1024
+    }
+};
+
+TEST_CASE(config_test_empty_fcb)
+{
+    int rc;
+    struct conf_fcb cf;
+
+    config_wipe_srcs();
+    config_wipe_fcb(fcb_areas, sizeof(fcb_areas) / sizeof(fcb_areas[0]));
+
+    cf.cf_fcb.f_sectors = fcb_areas;
+    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
+
+    rc = conf_fcb_src(&cf);
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * No values
+     */
+    conf_load();
+
+    config_wipe_srcs();
+    ctest_clear_call_state();
+}
+
+TEST_CASE(config_test_save_1_fcb)
+{
+    int rc;
+    struct conf_fcb cf;
+
+    config_wipe_srcs();
+
+    cf.cf_fcb.f_sectors = fcb_areas;
+    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
+
+    rc = conf_fcb_src(&cf);
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_fcb_dst(&cf);
+    TEST_ASSERT(rc == 0);
+
+    val8 = 33;
+    rc = conf_save();
+    TEST_ASSERT(rc == 0);
+
+    val8 = 0;
+
+    rc = conf_load();
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(val8 == 33);
+}
+
+static void config_test_fill_area(char test_value[64][CONF_MAX_VAL_LEN],
+  int iteration)
+{
+      int i, j;
+
+      for (j = 0; j < 64; j++) {
+          for (i = 0; i < CONF_MAX_VAL_LEN; i++) {
+              test_value[j][i] = ((j * 2) + i + iteration) % 10 + '0';
+          }
+          test_value[j][sizeof(test_value[j]) - 1] = '\0';
+      }
+}
+
+TEST_CASE(config_test_save_2_fcb)
+{
+    int rc;
+    struct conf_fcb cf;
+    char test_value[64][CONF_MAX_VAL_LEN];
+    int i;
+
+    config_wipe_srcs();
+
+    cf.cf_fcb.f_sectors = fcb_areas;
+    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
+
+    rc = conf_fcb_src(&cf);
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_fcb_dst(&cf);
+    TEST_ASSERT(rc == 0);
+
+    config_test_fill_area(test_value, 0);
+    memcpy(val_string, test_value, sizeof(val_string));
+
+    val8 = 42;
+    rc = conf_save();
+    TEST_ASSERT(rc == 0);
+
+    val8 = 0;
+    memset(val_string[0], 0, sizeof(val_string[0]));
+    rc = conf_load();
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(val8 == 42);
+    TEST_ASSERT(!strcmp(val_string[0], test_value[0]));
+    test_export_block = 1;
+
+    /*
+     * Now add the number of settings to max. Keep adjusting the test_data,
+     * check that rollover happens when it's supposed to.
+     */
+    c2_var_count = 64;
+
+    for (i = 0; i < 32; i++) {
+        config_test_fill_area(test_value, i);
+        memcpy(val_string, test_value, sizeof(val_string));
+
+        rc = conf_save();
+        TEST_ASSERT(rc == 0);
+
+        memset(val_string, 0, sizeof(val_string));
+
+        val8 = 0;
+        rc = conf_load();
+        TEST_ASSERT(rc == 0);
+        TEST_ASSERT(!memcmp(val_string, test_value, sizeof(val_string)));
+        TEST_ASSERT(val8 == 42);
+    }
+    c2_var_count = 0;
+}
+
+TEST_CASE(config_test_insert3)
+{
+    int rc;
+
+    rc = conf_register(&c3_test_handler);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(config_test_save_3_fcb)
+{
+    int rc;
+    struct conf_fcb cf;
+    int i;
+
+    config_wipe_srcs();
+    config_wipe_fcb(fcb_areas, sizeof(fcb_areas) / sizeof(fcb_areas[0]));
+
+    cf.cf_fcb.f_sectors = fcb_areas;
+    cf.cf_fcb.f_sector_cnt = 4;
+
+    rc = conf_fcb_src(&cf);
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_fcb_dst(&cf);
+    TEST_ASSERT(rc == 0);
+
+    for (i = 0; i < 4096; i++) {
+        val32 = i;
+
+        rc = conf_save();
+        TEST_ASSERT(rc == 0);
+
+        val32 = 0;
+
+        rc = conf_load();
+        TEST_ASSERT(rc == 0);
+        TEST_ASSERT(val32 == i);
+    }
+}
+
+TEST_CASE(config_test_compress_reset)
+{
+    int rc;
+    struct conf_fcb cf;
+    struct flash_area *fa;
+    char test_value[64][CONF_MAX_VAL_LEN];
+    int elems[4];
+    int i;
+
+    config_wipe_srcs();
+    config_wipe_fcb(fcb_areas, sizeof(fcb_areas) / sizeof(fcb_areas[0]));
+
+    cf.cf_fcb.f_sectors = fcb_areas;
+    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
+
+    rc = conf_fcb_src(&cf);
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_fcb_dst(&cf);
+    TEST_ASSERT(rc == 0);
+
+    c2_var_count = 1;
+    memset(elems, 0, sizeof(elems));
+
+    for (i = 0; ; i++) {
+        config_test_fill_area(test_value, i);
+        memcpy(val_string, test_value, sizeof(val_string));
+
+        rc = conf_save();
+        TEST_ASSERT(rc == 0);
+
+        if (cf.cf_fcb.f_active.fe_area == &fcb_areas[2]) {
+            /*
+             * Started using space just before scratch.
+             */
+            break;
+        }
+        memset(val_string, 0, sizeof(val_string));
+
+        rc = conf_load();
+        TEST_ASSERT(rc == 0);
+        TEST_ASSERT(!memcmp(val_string, test_value, CONF_MAX_VAL_LEN));
+    }
+
+    fa = cf.cf_fcb.f_active.fe_area;
+    rc = fcb_append_to_scratch(&cf.cf_fcb);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(fcb_free_sector_cnt(&cf.cf_fcb) == 0);
+    TEST_ASSERT(fa != cf.cf_fcb.f_active.fe_area);
+
+    config_wipe_srcs();
+
+    memset(&cf, 0, sizeof(cf));
+
+    cf.cf_fcb.f_sectors = fcb_areas;
+    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
+
+    rc = conf_fcb_src(&cf);
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_fcb_dst(&cf);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(fcb_free_sector_cnt(&cf.cf_fcb) == 1);
+    TEST_ASSERT(fa == cf.cf_fcb.f_active.fe_area);
+
+    c2_var_count = 0;
+}
+
+TEST_CASE(config_test_save_one_fcb)
+{
+    int rc;
+    struct conf_fcb cf;
+
+    config_wipe_srcs();
+    config_wipe_fcb(fcb_areas, sizeof(fcb_areas) / sizeof(fcb_areas[0]));
+
+    cf.cf_fcb.f_sectors = fcb_areas;
+    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
+
+    rc = conf_fcb_src(&cf);
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_fcb_dst(&cf);
+    TEST_ASSERT(rc == 0);
+
+    val8 = 33;
+    rc = conf_save();
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_save_one("myfoo/mybar", "42");
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_load();
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(val8 == 42);
+
+    rc = conf_save_one("myfoo/mybar", "44");
+    TEST_ASSERT(rc == 0);
+
+    rc = conf_load();
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(val8 == 44);
+}
+
+TEST_SUITE(config_test_all)
+{
+    /*
+     * Config tests.
+     */
+    config_empty_lookups();
+    config_test_insert();
+    config_test_getset_unknown();
+    config_test_getset_int();
+    config_test_getset_bytes();
+
+    config_test_commit();
+
+    /*
+     * NFFS as backing storage.
+     */
+    config_setup_nffs();
+    config_test_empty_file();
+    config_test_small_file();
+    config_test_multiple_in_file();
+
+    config_test_save_in_file();
+
+    config_test_save_one_file();
+
+    /*
+     * FCB as backing storage.
+     */
+    config_test_empty_fcb();
+    config_test_save_1_fcb();
+
+    config_test_insert2();
+
+    config_test_save_2_fcb();
+
+    config_test_insert3();
+    config_test_save_3_fcb();
+
+    config_test_compress_reset();
+
+    config_test_save_one_fcb();
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/test/src/conf_test.h
----------------------------------------------------------------------
diff --git a/sys/config/test/src/conf_test.h b/sys/config/test/src/conf_test.h
new file mode 100644
index 0000000..b50ff2f
--- /dev/null
+++ b/sys/config/test/src/conf_test.h
@@ -0,0 +1,25 @@
+/**
+ * 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 _CONF_TEST_H_
+#define _CONF_TEST_H_
+
+void config_test_all(void);
+
+#endif /* _CONF_TEST_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/test/src/conf_test_suite.c
----------------------------------------------------------------------
diff --git a/sys/config/test/src/conf_test_suite.c b/sys/config/test/src/conf_test_suite.c
new file mode 100644
index 0000000..65360c3
--- /dev/null
+++ b/sys/config/test/src/conf_test_suite.c
@@ -0,0 +1,40 @@
+/**
+ * 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 "syscfg/syscfg.h"
+#include "os/os.h"
+#include "testutil/testutil.h"
+#include "config/config.h"
+#include "config_test.h"
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    conf_init();
+    config_test_all();
+
+    return tu_any_failed;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/test/src/config_test.h
----------------------------------------------------------------------
diff --git a/sys/config/test/src/config_test.h b/sys/config/test/src/config_test.h
new file mode 100644
index 0000000..da531af
--- /dev/null
+++ b/sys/config/test/src/config_test.h
@@ -0,0 +1,24 @@
+/**
+ * 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 _CONFIG_TEST_H_
+#define _CONFIG_TEST_H_
+
+int config_test_all();
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/coredump/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/coredump/pkg.yml b/sys/coredump/pkg.yml
index b4f05a0..3da1277 100644
--- a/sys/coredump/pkg.yml
+++ b/sys/coredump/pkg.yml
@@ -28,5 +28,3 @@ pkg.deps:
     - hw/hal
     - libs/bootutil
     - libs/imgmgr
-pkg.features:
-    - COREDUMP

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/fcb/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/fcb/pkg.yml b/sys/fcb/pkg.yml
index 6cbfb28..bba5355 100644
--- a/sys/fcb/pkg.yml
+++ b/sys/fcb/pkg.yml
@@ -26,6 +26,3 @@ pkg.keywords:
 
 pkg.deps:
     - libs/os
-    - libs/testutil
-pkg.features:
-    - FCB

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/fcb/src/test/fcb_test.c
----------------------------------------------------------------------
diff --git a/sys/fcb/src/test/fcb_test.c b/sys/fcb/src/test/fcb_test.c
deleted file mode 100644
index b0de576..0000000
--- a/sys/fcb/src/test/fcb_test.c
+++ /dev/null
@@ -1,671 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <string.h>
-
-#include <os/os.h>
-#include <testutil/testutil.h>
-
-#include "fcb/fcb.h"
-#include "fcb/../../src/fcb_priv.h"
-
-static struct fcb test_fcb;
-
-static struct flash_area test_fcb_area[] = {
-    [0] = {
-        .fa_flash_id = 0,
-        .fa_off = 0,
-        .fa_size = 0x4000, /* 16K */
-    },
-    [1] = {
-        .fa_flash_id = 0,
-        .fa_off = 0x4000,
-        .fa_size = 0x4000
-    },
-    [2] = {
-        .fa_flash_id = 0,
-        .fa_off = 0x8000,
-        .fa_size = 0x4000
-    },
-    [3] = {
-        .fa_flash_id = 0,
-        .fa_off = 0xc000,
-        .fa_size = 0x4000
-    }
-};
-
-static void
-fcb_test_wipe(void)
-{
-    int i;
-    int rc;
-    struct flash_area *fap;
-
-    for (i = 0; i < sizeof(test_fcb_area) / sizeof(test_fcb_area[0]); i++) {
-        fap = &test_fcb_area[i];
-        rc = flash_area_erase(fap, 0, fap->fa_size);
-        TEST_ASSERT(rc == 0);
-    }
-}
-
-TEST_CASE(fcb_test_len)
-{
-    uint8_t buf[3];
-    uint16_t len;
-    uint16_t len2;
-    int rc;
-
-    for (len = 0; len < FCB_MAX_LEN; len++) {
-        rc = fcb_put_len(buf, len);
-        TEST_ASSERT(rc == 1 || rc == 2);
-
-        rc = fcb_get_len(buf, &len2);
-        TEST_ASSERT(rc == 1 || rc == 2);
-
-        TEST_ASSERT(len == len2);
-    }
-}
-
-TEST_CASE(fcb_test_init)
-{
-    int rc;
-    struct fcb *fcb;
-
-    fcb = &test_fcb;
-    memset(fcb, 0, sizeof(*fcb));
-
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == FCB_ERR_ARGS);
-
-    fcb->f_sectors = test_fcb_area;
-
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == FCB_ERR_ARGS);
-
-    fcb->f_sector_cnt = 2;
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == 0);
-}
-
-static int
-fcb_test_empty_walk_cb(struct fcb_entry *loc, void *arg)
-{
-    TEST_ASSERT(0);
-    return 0;
-}
-
-TEST_CASE(fcb_test_empty_walk)
-{
-    int rc;
-    struct fcb *fcb;
-
-    fcb_test_wipe();
-    fcb = &test_fcb;
-    memset(fcb, 0, sizeof(*fcb));
-
-    fcb->f_sector_cnt = 2;
-    fcb->f_sectors = test_fcb_area;
-
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == 0);
-
-    rc = fcb_walk(fcb, 0, fcb_test_empty_walk_cb, NULL);
-    TEST_ASSERT(rc == 0);
-}
-
-static uint8_t
-fcb_test_append_data(int msg_len, int off)
-{
-    return (msg_len ^ off);
-}
-
-static int
-fcb_test_data_walk_cb(struct fcb_entry *loc, void *arg)
-{
-    uint16_t len;
-    uint8_t test_data[128];
-    int rc;
-    int i;
-    int *var_cnt = (int *)arg;
-
-    len = loc->fe_data_len;
-
-    TEST_ASSERT(len == *var_cnt);
-
-    rc = flash_area_read(loc->fe_area, loc->fe_data_off, test_data, len);
-    TEST_ASSERT(rc == 0);
-
-    for (i = 0; i < len; i++) {
-        TEST_ASSERT(test_data[i] == fcb_test_append_data(len, i));
-    }
-    (*var_cnt)++;
-    return 0;
-}
-
-TEST_CASE(fcb_test_append)
-{
-    int rc;
-    struct fcb *fcb;
-    struct fcb_entry loc;
-    uint8_t test_data[128];
-    int i;
-    int j;
-    int var_cnt;
-
-    fcb_test_wipe();
-    fcb = &test_fcb;
-    memset(fcb, 0, sizeof(*fcb));
-    fcb->f_sector_cnt = 2;
-    fcb->f_sectors = test_fcb_area;
-
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == 0);
-
-    for (i = 0; i < sizeof(test_data); i++) {
-        for (j = 0; j < i; j++) {
-            test_data[j] = fcb_test_append_data(i, j);
-        }
-        rc = fcb_append(fcb, i, &loc);
-        TEST_ASSERT_FATAL(rc == 0);
-        rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, i);
-        TEST_ASSERT(rc == 0);
-        rc = fcb_append_finish(fcb, &loc);
-        TEST_ASSERT(rc == 0);
-    }
-
-    var_cnt = 0;
-    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(var_cnt == sizeof(test_data));
-}
-
-TEST_CASE(fcb_test_append_too_big)
-{
-    struct fcb *fcb;
-    int rc;
-    int len;
-    struct fcb_entry elem_loc;
-
-    fcb_test_wipe();
-    fcb = &test_fcb;
-    memset(fcb, 0, sizeof(*fcb));
-    fcb->f_sector_cnt = 2;
-    fcb->f_sectors = test_fcb_area;
-
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == 0);
-
-    /*
-     * Max element which fits inside sector is
-     * sector size - (disk header + crc + 1-2 bytes of length).
-     */
-    len = fcb->f_active.fe_area->fa_size;
-
-    rc = fcb_append(fcb, len, &elem_loc);
-    TEST_ASSERT(rc != 0);
-
-    len--;
-    rc = fcb_append(fcb, len, &elem_loc);
-    TEST_ASSERT(rc != 0);
-
-    len -= sizeof(struct fcb_disk_area);
-    rc = fcb_append(fcb, len, &elem_loc);
-    TEST_ASSERT(rc != 0);
-
-    len = fcb->f_active.fe_area->fa_size -
-      (sizeof(struct fcb_disk_area) + 1 + 2);
-    rc = fcb_append(fcb, len, &elem_loc);
-    TEST_ASSERT(rc == 0);
-
-    rc = fcb_append_finish(fcb, &elem_loc);
-    TEST_ASSERT(rc == 0);
-
-    rc = fcb_elem_info(fcb, &elem_loc);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(elem_loc.fe_data_len == len);
-}
-
-struct append_arg {
-    int *elem_cnts;
-};
-
-static int
-fcb_test_cnt_elems_cb(struct fcb_entry *loc, void *arg)
-{
-    struct append_arg *aa = (struct append_arg *)arg;
-    int idx;
-
-    idx = loc->fe_area - &test_fcb_area[0];
-    aa->elem_cnts[idx]++;
-    return 0;
-}
-
-TEST_CASE(fcb_test_append_fill)
-{
-    struct fcb *fcb;
-    int rc;
-    int i;
-    struct fcb_entry loc;
-    uint8_t test_data[128];
-    int elem_cnts[2] = {0, 0};
-    int aa_together_cnts[2];
-    struct append_arg aa_together = {
-        .elem_cnts = aa_together_cnts
-    };
-    int aa_separate_cnts[2];
-    struct append_arg aa_separate = {
-        .elem_cnts = aa_separate_cnts
-    };
-
-    fcb_test_wipe();
-    fcb = &test_fcb;
-    memset(fcb, 0, sizeof(*fcb));
-    fcb->f_sector_cnt = 2;
-    fcb->f_sectors = test_fcb_area;
-
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == 0);
-
-    for (i = 0; i < sizeof(test_data); i++) {
-        test_data[i] = fcb_test_append_data(sizeof(test_data), i);
-    }
-
-    while (1) {
-        rc = fcb_append(fcb, sizeof(test_data), &loc);
-        if (rc == FCB_ERR_NOSPACE) {
-            break;
-        }
-        if (loc.fe_area == &test_fcb_area[0]) {
-            elem_cnts[0]++;
-        } else if (loc.fe_area == &test_fcb_area[1]) {
-            elem_cnts[1]++;
-        } else {
-            TEST_ASSERT(0);
-        }
-
-        rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
-          sizeof(test_data));
-        TEST_ASSERT(rc == 0);
-
-        rc = fcb_append_finish(fcb, &loc);
-        TEST_ASSERT(rc == 0);
-    }
-    TEST_ASSERT(elem_cnts[0] > 0);
-    TEST_ASSERT(elem_cnts[0] == elem_cnts[1]);
-
-    memset(&aa_together_cnts, 0, sizeof(aa_together_cnts));
-    rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_together);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(aa_together.elem_cnts[0] == elem_cnts[0]);
-    TEST_ASSERT(aa_together.elem_cnts[1] == elem_cnts[1]);
-
-    memset(&aa_separate_cnts, 0, sizeof(aa_separate_cnts));
-    rc = fcb_walk(fcb, &test_fcb_area[0], fcb_test_cnt_elems_cb,
-      &aa_separate);
-    TEST_ASSERT(rc == 0);
-    rc = fcb_walk(fcb, &test_fcb_area[1], fcb_test_cnt_elems_cb,
-      &aa_separate);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(aa_separate.elem_cnts[0] == elem_cnts[0]);
-    TEST_ASSERT(aa_separate.elem_cnts[1] == elem_cnts[1]);
-
-}
-
-TEST_CASE(fcb_test_reset)
-{
-    struct fcb *fcb;
-    int rc;
-    int i;
-    struct fcb_entry loc;
-    uint8_t test_data[128];
-    int var_cnt;
-
-    fcb_test_wipe();
-    fcb = &test_fcb;
-    memset(fcb, 0, sizeof(*fcb));
-    fcb->f_sector_cnt = 2;
-    fcb->f_sectors = test_fcb_area;
-
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == 0);
-
-    var_cnt = 0;
-    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(var_cnt == 0);
-
-    rc = fcb_append(fcb, 32, &loc);
-    TEST_ASSERT(rc == 0);
-
-    /*
-     * No ready ones yet. CRC should not match.
-     */
-    var_cnt = 0;
-    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(var_cnt == 0);
-
-    for (i = 0; i < sizeof(test_data); i++) {
-        test_data[i] = fcb_test_append_data(32, i);
-    }
-    rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 32);
-    TEST_ASSERT(rc == 0);
-
-    rc = fcb_append_finish(fcb, &loc);
-    TEST_ASSERT(rc == 0);
-
-    /*
-     * one entry
-     */
-    var_cnt = 32;
-    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(var_cnt == 33);
-
-    /*
-     * Pretend reset
-     */
-    memset(fcb, 0, sizeof(*fcb));
-    fcb->f_sector_cnt = 2;
-    fcb->f_sectors = test_fcb_area;
-
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == 0);
-
-    var_cnt = 32;
-    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(var_cnt == 33);
-
-    rc = fcb_append(fcb, 33, &loc);
-    TEST_ASSERT(rc == 0);
-
-    for (i = 0; i < sizeof(test_data); i++) {
-        test_data[i] = fcb_test_append_data(33, i);
-    }
-    rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 33);
-    TEST_ASSERT(rc == 0);
-
-    rc = fcb_append_finish(fcb, &loc);
-    TEST_ASSERT(rc == 0);
-
-    var_cnt = 32;
-    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(var_cnt == 34);
-
-    /*
-     * Add partial one, make sure that we survive reset then.
-     */
-    rc = fcb_append(fcb, 34, &loc);
-    TEST_ASSERT(rc == 0);
-
-    memset(fcb, 0, sizeof(*fcb));
-    fcb->f_sector_cnt = 2;
-    fcb->f_sectors = test_fcb_area;
-
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == 0);
-
-    /*
-     * Walk should skip that.
-     */
-    var_cnt = 32;
-    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(var_cnt == 34);
-
-    /* Add a 3rd one, should go behind corrupt entry */
-    rc = fcb_append(fcb, 34, &loc);
-    TEST_ASSERT(rc == 0);
-
-    for (i = 0; i < sizeof(test_data); i++) {
-        test_data[i] = fcb_test_append_data(34, i);
-    }
-    rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 34);
-    TEST_ASSERT(rc == 0);
-
-    rc = fcb_append_finish(fcb, &loc);
-    TEST_ASSERT(rc == 0);
-
-    /*
-     * Walk should skip corrupt entry, but report the next one.
-     */
-    var_cnt = 32;
-    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(var_cnt == 35);
-}
-
-TEST_CASE(fcb_test_rotate)
-{
-    struct fcb *fcb;
-    int rc;
-    int old_id;
-    struct fcb_entry loc;
-    uint8_t test_data[128];
-    int elem_cnts[2] = {0, 0};
-    int cnts[2];
-    struct append_arg aa_arg = {
-        .elem_cnts = cnts
-    };
-
-    fcb_test_wipe();
-    fcb = &test_fcb;
-    memset(fcb, 0, sizeof(*fcb));
-    fcb->f_sector_cnt = 2;
-    fcb->f_sectors = test_fcb_area;
-
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == 0);
-
-    old_id = fcb->f_active_id;
-    rc = fcb_rotate(fcb);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(fcb->f_active_id == old_id + 1);
-
-    /*
-     * Now fill up the
-     */
-    while (1) {
-        rc = fcb_append(fcb, sizeof(test_data), &loc);
-        if (rc == FCB_ERR_NOSPACE) {
-            break;
-        }
-        if (loc.fe_area == &test_fcb_area[0]) {
-            elem_cnts[0]++;
-        } else if (loc.fe_area == &test_fcb_area[1]) {
-            elem_cnts[1]++;
-        } else {
-            TEST_ASSERT(0);
-        }
-
-        rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
-          sizeof(test_data));
-        TEST_ASSERT(rc == 0);
-
-        rc = fcb_append_finish(fcb, &loc);
-        TEST_ASSERT(rc == 0);
-    }
-    TEST_ASSERT(elem_cnts[0] > 0 && elem_cnts[0] == elem_cnts[1]);
-
-    old_id = fcb->f_active_id;
-    rc = fcb_rotate(fcb);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(fcb->f_active_id == old_id); /* no new area created */
-
-    memset(cnts, 0, sizeof(cnts));
-    rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(aa_arg.elem_cnts[0] == elem_cnts[0] ||
-      aa_arg.elem_cnts[1] == elem_cnts[1]);
-    TEST_ASSERT(aa_arg.elem_cnts[0] == 0 || aa_arg.elem_cnts[1] == 0);
-
-    /*
-     * One sector is full. The other one should have one entry in it.
-     */
-    rc = fcb_append(fcb, sizeof(test_data), &loc);
-    TEST_ASSERT(rc == 0);
-
-    rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
-      sizeof(test_data));
-    TEST_ASSERT(rc == 0);
-
-    rc = fcb_append_finish(fcb, &loc);
-    TEST_ASSERT(rc == 0);
-
-    old_id = fcb->f_active_id;
-    rc = fcb_rotate(fcb);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(fcb->f_active_id == old_id);
-
-    memset(cnts, 0, sizeof(cnts));
-    rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(aa_arg.elem_cnts[0] == 1 || aa_arg.elem_cnts[1] == 1);
-    TEST_ASSERT(aa_arg.elem_cnts[0] == 0 || aa_arg.elem_cnts[1] == 0);
-}
-
-TEST_CASE(fcb_test_multiple_scratch)
-{
-    struct fcb *fcb;
-    int rc;
-    struct fcb_entry loc;
-    uint8_t test_data[128];
-    int elem_cnts[4];
-    int idx;
-    int cnts[4];
-    struct append_arg aa_arg = {
-        .elem_cnts = cnts
-    };
-
-    fcb_test_wipe();
-    fcb = &test_fcb;
-    memset(fcb, 0, sizeof(*fcb));
-    fcb->f_sector_cnt = 4;
-    fcb->f_scratch_cnt = 1;
-    fcb->f_sectors = test_fcb_area;
-
-    rc = fcb_init(fcb);
-    TEST_ASSERT(rc == 0);
-
-    /*
-     * Now fill up everything. We should be able to get 3 of the sectors
-     * full.
-     */
-    memset(elem_cnts, 0, sizeof(elem_cnts));
-    while (1) {
-        rc = fcb_append(fcb, sizeof(test_data), &loc);
-        if (rc == FCB_ERR_NOSPACE) {
-            break;
-        }
-        idx = loc.fe_area - &test_fcb_area[0];
-        elem_cnts[idx]++;
-
-        rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
-          sizeof(test_data));
-        TEST_ASSERT(rc == 0);
-
-        rc = fcb_append_finish(fcb, &loc);
-        TEST_ASSERT(rc == 0);
-    }
-
-    TEST_ASSERT(elem_cnts[0] > 0);
-    TEST_ASSERT(elem_cnts[0] == elem_cnts[1] && elem_cnts[0] == elem_cnts[2]);
-    TEST_ASSERT(elem_cnts[3] == 0);
-
-    /*
-     * Ask to use scratch block, then fill it up.
-     */
-    rc = fcb_append_to_scratch(fcb);
-    TEST_ASSERT(rc == 0);
-
-    while (1) {
-        rc = fcb_append(fcb, sizeof(test_data), &loc);
-        if (rc == FCB_ERR_NOSPACE) {
-            break;
-        }
-        idx = loc.fe_area - &test_fcb_area[0];
-        elem_cnts[idx]++;
-
-        rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
-          sizeof(test_data));
-        TEST_ASSERT(rc == 0);
-
-        rc = fcb_append_finish(fcb, &loc);
-        TEST_ASSERT(rc == 0);
-    }
-    TEST_ASSERT(elem_cnts[3] == elem_cnts[0]);
-
-    /*
-     * Rotate
-     */
-    rc = fcb_rotate(fcb);
-    TEST_ASSERT(rc == 0);
-
-    memset(&cnts, 0, sizeof(cnts));
-    rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(cnts[0] == 0);
-    TEST_ASSERT(cnts[1] > 0);
-    TEST_ASSERT(cnts[1] == cnts[2] && cnts[1] == cnts[3]);
-
-    rc = fcb_append_to_scratch(fcb);
-    TEST_ASSERT(rc == 0);
-    rc = fcb_append_to_scratch(fcb);
-    TEST_ASSERT(rc != 0);
-}
-
-TEST_SUITE(fcb_test_all)
-{
-    fcb_test_len();
-
-    fcb_test_init();
-
-    fcb_test_empty_walk();
-
-    fcb_test_append();
-
-    fcb_test_append_too_big();
-
-    fcb_test_append_fill();
-
-    fcb_test_reset();
-
-    fcb_test_rotate();
-
-    fcb_test_multiple_scratch();
-}
-
-#ifdef MYNEWT_SELFTEST
-
-int
-main(int argc, char **argv)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    fcb_test_all();
-
-    return tu_any_failed;
-}
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/fcb/test/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/fcb/test/pkg.yml b/sys/fcb/test/pkg.yml
new file mode 100644
index 0000000..31a8669
--- /dev/null
+++ b/sys/fcb/test/pkg.yml
@@ -0,0 +1,30 @@
+# 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.
+#
+pkg.name: sys/fcb/test
+pkg.type: unittest
+pkg.description: "FCB unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps: 
+    - libs/testutil
+    - sys/fcb
+
+pkg.deps.SELFTEST:
+    - libs/console/stub

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/fcb/test/src/fcb_test.c
----------------------------------------------------------------------
diff --git a/sys/fcb/test/src/fcb_test.c b/sys/fcb/test/src/fcb_test.c
new file mode 100644
index 0000000..b172eb0
--- /dev/null
+++ b/sys/fcb/test/src/fcb_test.c
@@ -0,0 +1,672 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+
+#include "syscfg/syscfg.h"
+#include "os/os.h"
+#include "testutil/testutil.h"
+
+#include "fcb/fcb.h"
+#include "fcb/../../src/fcb_priv.h"
+
+static struct fcb test_fcb;
+
+static struct flash_area test_fcb_area[] = {
+    [0] = {
+        .fa_flash_id = 0,
+        .fa_off = 0,
+        .fa_size = 0x4000, /* 16K */
+    },
+    [1] = {
+        .fa_flash_id = 0,
+        .fa_off = 0x4000,
+        .fa_size = 0x4000
+    },
+    [2] = {
+        .fa_flash_id = 0,
+        .fa_off = 0x8000,
+        .fa_size = 0x4000
+    },
+    [3] = {
+        .fa_flash_id = 0,
+        .fa_off = 0xc000,
+        .fa_size = 0x4000
+    }
+};
+
+static void
+fcb_test_wipe(void)
+{
+    int i;
+    int rc;
+    struct flash_area *fap;
+
+    for (i = 0; i < sizeof(test_fcb_area) / sizeof(test_fcb_area[0]); i++) {
+        fap = &test_fcb_area[i];
+        rc = flash_area_erase(fap, 0, fap->fa_size);
+        TEST_ASSERT(rc == 0);
+    }
+}
+
+TEST_CASE(fcb_test_len)
+{
+    uint8_t buf[3];
+    uint16_t len;
+    uint16_t len2;
+    int rc;
+
+    for (len = 0; len < FCB_MAX_LEN; len++) {
+        rc = fcb_put_len(buf, len);
+        TEST_ASSERT(rc == 1 || rc == 2);
+
+        rc = fcb_get_len(buf, &len2);
+        TEST_ASSERT(rc == 1 || rc == 2);
+
+        TEST_ASSERT(len == len2);
+    }
+}
+
+TEST_CASE(fcb_test_init)
+{
+    int rc;
+    struct fcb *fcb;
+
+    fcb = &test_fcb;
+    memset(fcb, 0, sizeof(*fcb));
+
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == FCB_ERR_ARGS);
+
+    fcb->f_sectors = test_fcb_area;
+
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == FCB_ERR_ARGS);
+
+    fcb->f_sector_cnt = 2;
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == 0);
+}
+
+static int
+fcb_test_empty_walk_cb(struct fcb_entry *loc, void *arg)
+{
+    TEST_ASSERT(0);
+    return 0;
+}
+
+TEST_CASE(fcb_test_empty_walk)
+{
+    int rc;
+    struct fcb *fcb;
+
+    fcb_test_wipe();
+    fcb = &test_fcb;
+    memset(fcb, 0, sizeof(*fcb));
+
+    fcb->f_sector_cnt = 2;
+    fcb->f_sectors = test_fcb_area;
+
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == 0);
+
+    rc = fcb_walk(fcb, 0, fcb_test_empty_walk_cb, NULL);
+    TEST_ASSERT(rc == 0);
+}
+
+static uint8_t
+fcb_test_append_data(int msg_len, int off)
+{
+    return (msg_len ^ off);
+}
+
+static int
+fcb_test_data_walk_cb(struct fcb_entry *loc, void *arg)
+{
+    uint16_t len;
+    uint8_t test_data[128];
+    int rc;
+    int i;
+    int *var_cnt = (int *)arg;
+
+    len = loc->fe_data_len;
+
+    TEST_ASSERT(len == *var_cnt);
+
+    rc = flash_area_read(loc->fe_area, loc->fe_data_off, test_data, len);
+    TEST_ASSERT(rc == 0);
+
+    for (i = 0; i < len; i++) {
+        TEST_ASSERT(test_data[i] == fcb_test_append_data(len, i));
+    }
+    (*var_cnt)++;
+    return 0;
+}
+
+TEST_CASE(fcb_test_append)
+{
+    int rc;
+    struct fcb *fcb;
+    struct fcb_entry loc;
+    uint8_t test_data[128];
+    int i;
+    int j;
+    int var_cnt;
+
+    fcb_test_wipe();
+    fcb = &test_fcb;
+    memset(fcb, 0, sizeof(*fcb));
+    fcb->f_sector_cnt = 2;
+    fcb->f_sectors = test_fcb_area;
+
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == 0);
+
+    for (i = 0; i < sizeof(test_data); i++) {
+        for (j = 0; j < i; j++) {
+            test_data[j] = fcb_test_append_data(i, j);
+        }
+        rc = fcb_append(fcb, i, &loc);
+        TEST_ASSERT_FATAL(rc == 0);
+        rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, i);
+        TEST_ASSERT(rc == 0);
+        rc = fcb_append_finish(fcb, &loc);
+        TEST_ASSERT(rc == 0);
+    }
+
+    var_cnt = 0;
+    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(var_cnt == sizeof(test_data));
+}
+
+TEST_CASE(fcb_test_append_too_big)
+{
+    struct fcb *fcb;
+    int rc;
+    int len;
+    struct fcb_entry elem_loc;
+
+    fcb_test_wipe();
+    fcb = &test_fcb;
+    memset(fcb, 0, sizeof(*fcb));
+    fcb->f_sector_cnt = 2;
+    fcb->f_sectors = test_fcb_area;
+
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * Max element which fits inside sector is
+     * sector size - (disk header + crc + 1-2 bytes of length).
+     */
+    len = fcb->f_active.fe_area->fa_size;
+
+    rc = fcb_append(fcb, len, &elem_loc);
+    TEST_ASSERT(rc != 0);
+
+    len--;
+    rc = fcb_append(fcb, len, &elem_loc);
+    TEST_ASSERT(rc != 0);
+
+    len -= sizeof(struct fcb_disk_area);
+    rc = fcb_append(fcb, len, &elem_loc);
+    TEST_ASSERT(rc != 0);
+
+    len = fcb->f_active.fe_area->fa_size -
+      (sizeof(struct fcb_disk_area) + 1 + 2);
+    rc = fcb_append(fcb, len, &elem_loc);
+    TEST_ASSERT(rc == 0);
+
+    rc = fcb_append_finish(fcb, &elem_loc);
+    TEST_ASSERT(rc == 0);
+
+    rc = fcb_elem_info(fcb, &elem_loc);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(elem_loc.fe_data_len == len);
+}
+
+struct append_arg {
+    int *elem_cnts;
+};
+
+static int
+fcb_test_cnt_elems_cb(struct fcb_entry *loc, void *arg)
+{
+    struct append_arg *aa = (struct append_arg *)arg;
+    int idx;
+
+    idx = loc->fe_area - &test_fcb_area[0];
+    aa->elem_cnts[idx]++;
+    return 0;
+}
+
+TEST_CASE(fcb_test_append_fill)
+{
+    struct fcb *fcb;
+    int rc;
+    int i;
+    struct fcb_entry loc;
+    uint8_t test_data[128];
+    int elem_cnts[2] = {0, 0};
+    int aa_together_cnts[2];
+    struct append_arg aa_together = {
+        .elem_cnts = aa_together_cnts
+    };
+    int aa_separate_cnts[2];
+    struct append_arg aa_separate = {
+        .elem_cnts = aa_separate_cnts
+    };
+
+    fcb_test_wipe();
+    fcb = &test_fcb;
+    memset(fcb, 0, sizeof(*fcb));
+    fcb->f_sector_cnt = 2;
+    fcb->f_sectors = test_fcb_area;
+
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == 0);
+
+    for (i = 0; i < sizeof(test_data); i++) {
+        test_data[i] = fcb_test_append_data(sizeof(test_data), i);
+    }
+
+    while (1) {
+        rc = fcb_append(fcb, sizeof(test_data), &loc);
+        if (rc == FCB_ERR_NOSPACE) {
+            break;
+        }
+        if (loc.fe_area == &test_fcb_area[0]) {
+            elem_cnts[0]++;
+        } else if (loc.fe_area == &test_fcb_area[1]) {
+            elem_cnts[1]++;
+        } else {
+            TEST_ASSERT(0);
+        }
+
+        rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
+          sizeof(test_data));
+        TEST_ASSERT(rc == 0);
+
+        rc = fcb_append_finish(fcb, &loc);
+        TEST_ASSERT(rc == 0);
+    }
+    TEST_ASSERT(elem_cnts[0] > 0);
+    TEST_ASSERT(elem_cnts[0] == elem_cnts[1]);
+
+    memset(&aa_together_cnts, 0, sizeof(aa_together_cnts));
+    rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_together);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(aa_together.elem_cnts[0] == elem_cnts[0]);
+    TEST_ASSERT(aa_together.elem_cnts[1] == elem_cnts[1]);
+
+    memset(&aa_separate_cnts, 0, sizeof(aa_separate_cnts));
+    rc = fcb_walk(fcb, &test_fcb_area[0], fcb_test_cnt_elems_cb,
+      &aa_separate);
+    TEST_ASSERT(rc == 0);
+    rc = fcb_walk(fcb, &test_fcb_area[1], fcb_test_cnt_elems_cb,
+      &aa_separate);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(aa_separate.elem_cnts[0] == elem_cnts[0]);
+    TEST_ASSERT(aa_separate.elem_cnts[1] == elem_cnts[1]);
+
+}
+
+TEST_CASE(fcb_test_reset)
+{
+    struct fcb *fcb;
+    int rc;
+    int i;
+    struct fcb_entry loc;
+    uint8_t test_data[128];
+    int var_cnt;
+
+    fcb_test_wipe();
+    fcb = &test_fcb;
+    memset(fcb, 0, sizeof(*fcb));
+    fcb->f_sector_cnt = 2;
+    fcb->f_sectors = test_fcb_area;
+
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == 0);
+
+    var_cnt = 0;
+    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(var_cnt == 0);
+
+    rc = fcb_append(fcb, 32, &loc);
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * No ready ones yet. CRC should not match.
+     */
+    var_cnt = 0;
+    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(var_cnt == 0);
+
+    for (i = 0; i < sizeof(test_data); i++) {
+        test_data[i] = fcb_test_append_data(32, i);
+    }
+    rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 32);
+    TEST_ASSERT(rc == 0);
+
+    rc = fcb_append_finish(fcb, &loc);
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * one entry
+     */
+    var_cnt = 32;
+    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(var_cnt == 33);
+
+    /*
+     * Pretend reset
+     */
+    memset(fcb, 0, sizeof(*fcb));
+    fcb->f_sector_cnt = 2;
+    fcb->f_sectors = test_fcb_area;
+
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == 0);
+
+    var_cnt = 32;
+    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(var_cnt == 33);
+
+    rc = fcb_append(fcb, 33, &loc);
+    TEST_ASSERT(rc == 0);
+
+    for (i = 0; i < sizeof(test_data); i++) {
+        test_data[i] = fcb_test_append_data(33, i);
+    }
+    rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 33);
+    TEST_ASSERT(rc == 0);
+
+    rc = fcb_append_finish(fcb, &loc);
+    TEST_ASSERT(rc == 0);
+
+    var_cnt = 32;
+    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(var_cnt == 34);
+
+    /*
+     * Add partial one, make sure that we survive reset then.
+     */
+    rc = fcb_append(fcb, 34, &loc);
+    TEST_ASSERT(rc == 0);
+
+    memset(fcb, 0, sizeof(*fcb));
+    fcb->f_sector_cnt = 2;
+    fcb->f_sectors = test_fcb_area;
+
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * Walk should skip that.
+     */
+    var_cnt = 32;
+    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(var_cnt == 34);
+
+    /* Add a 3rd one, should go behind corrupt entry */
+    rc = fcb_append(fcb, 34, &loc);
+    TEST_ASSERT(rc == 0);
+
+    for (i = 0; i < sizeof(test_data); i++) {
+        test_data[i] = fcb_test_append_data(34, i);
+    }
+    rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data, 34);
+    TEST_ASSERT(rc == 0);
+
+    rc = fcb_append_finish(fcb, &loc);
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * Walk should skip corrupt entry, but report the next one.
+     */
+    var_cnt = 32;
+    rc = fcb_walk(fcb, 0, fcb_test_data_walk_cb, &var_cnt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(var_cnt == 35);
+}
+
+TEST_CASE(fcb_test_rotate)
+{
+    struct fcb *fcb;
+    int rc;
+    int old_id;
+    struct fcb_entry loc;
+    uint8_t test_data[128];
+    int elem_cnts[2] = {0, 0};
+    int cnts[2];
+    struct append_arg aa_arg = {
+        .elem_cnts = cnts
+    };
+
+    fcb_test_wipe();
+    fcb = &test_fcb;
+    memset(fcb, 0, sizeof(*fcb));
+    fcb->f_sector_cnt = 2;
+    fcb->f_sectors = test_fcb_area;
+
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == 0);
+
+    old_id = fcb->f_active_id;
+    rc = fcb_rotate(fcb);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(fcb->f_active_id == old_id + 1);
+
+    /*
+     * Now fill up the
+     */
+    while (1) {
+        rc = fcb_append(fcb, sizeof(test_data), &loc);
+        if (rc == FCB_ERR_NOSPACE) {
+            break;
+        }
+        if (loc.fe_area == &test_fcb_area[0]) {
+            elem_cnts[0]++;
+        } else if (loc.fe_area == &test_fcb_area[1]) {
+            elem_cnts[1]++;
+        } else {
+            TEST_ASSERT(0);
+        }
+
+        rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
+          sizeof(test_data));
+        TEST_ASSERT(rc == 0);
+
+        rc = fcb_append_finish(fcb, &loc);
+        TEST_ASSERT(rc == 0);
+    }
+    TEST_ASSERT(elem_cnts[0] > 0 && elem_cnts[0] == elem_cnts[1]);
+
+    old_id = fcb->f_active_id;
+    rc = fcb_rotate(fcb);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(fcb->f_active_id == old_id); /* no new area created */
+
+    memset(cnts, 0, sizeof(cnts));
+    rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(aa_arg.elem_cnts[0] == elem_cnts[0] ||
+      aa_arg.elem_cnts[1] == elem_cnts[1]);
+    TEST_ASSERT(aa_arg.elem_cnts[0] == 0 || aa_arg.elem_cnts[1] == 0);
+
+    /*
+     * One sector is full. The other one should have one entry in it.
+     */
+    rc = fcb_append(fcb, sizeof(test_data), &loc);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
+      sizeof(test_data));
+    TEST_ASSERT(rc == 0);
+
+    rc = fcb_append_finish(fcb, &loc);
+    TEST_ASSERT(rc == 0);
+
+    old_id = fcb->f_active_id;
+    rc = fcb_rotate(fcb);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(fcb->f_active_id == old_id);
+
+    memset(cnts, 0, sizeof(cnts));
+    rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(aa_arg.elem_cnts[0] == 1 || aa_arg.elem_cnts[1] == 1);
+    TEST_ASSERT(aa_arg.elem_cnts[0] == 0 || aa_arg.elem_cnts[1] == 0);
+}
+
+TEST_CASE(fcb_test_multiple_scratch)
+{
+    struct fcb *fcb;
+    int rc;
+    struct fcb_entry loc;
+    uint8_t test_data[128];
+    int elem_cnts[4];
+    int idx;
+    int cnts[4];
+    struct append_arg aa_arg = {
+        .elem_cnts = cnts
+    };
+
+    fcb_test_wipe();
+    fcb = &test_fcb;
+    memset(fcb, 0, sizeof(*fcb));
+    fcb->f_sector_cnt = 4;
+    fcb->f_scratch_cnt = 1;
+    fcb->f_sectors = test_fcb_area;
+
+    rc = fcb_init(fcb);
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * Now fill up everything. We should be able to get 3 of the sectors
+     * full.
+     */
+    memset(elem_cnts, 0, sizeof(elem_cnts));
+    while (1) {
+        rc = fcb_append(fcb, sizeof(test_data), &loc);
+        if (rc == FCB_ERR_NOSPACE) {
+            break;
+        }
+        idx = loc.fe_area - &test_fcb_area[0];
+        elem_cnts[idx]++;
+
+        rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
+          sizeof(test_data));
+        TEST_ASSERT(rc == 0);
+
+        rc = fcb_append_finish(fcb, &loc);
+        TEST_ASSERT(rc == 0);
+    }
+
+    TEST_ASSERT(elem_cnts[0] > 0);
+    TEST_ASSERT(elem_cnts[0] == elem_cnts[1] && elem_cnts[0] == elem_cnts[2]);
+    TEST_ASSERT(elem_cnts[3] == 0);
+
+    /*
+     * Ask to use scratch block, then fill it up.
+     */
+    rc = fcb_append_to_scratch(fcb);
+    TEST_ASSERT(rc == 0);
+
+    while (1) {
+        rc = fcb_append(fcb, sizeof(test_data), &loc);
+        if (rc == FCB_ERR_NOSPACE) {
+            break;
+        }
+        idx = loc.fe_area - &test_fcb_area[0];
+        elem_cnts[idx]++;
+
+        rc = flash_area_write(loc.fe_area, loc.fe_data_off, test_data,
+          sizeof(test_data));
+        TEST_ASSERT(rc == 0);
+
+        rc = fcb_append_finish(fcb, &loc);
+        TEST_ASSERT(rc == 0);
+    }
+    TEST_ASSERT(elem_cnts[3] == elem_cnts[0]);
+
+    /*
+     * Rotate
+     */
+    rc = fcb_rotate(fcb);
+    TEST_ASSERT(rc == 0);
+
+    memset(&cnts, 0, sizeof(cnts));
+    rc = fcb_walk(fcb, NULL, fcb_test_cnt_elems_cb, &aa_arg);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(cnts[0] == 0);
+    TEST_ASSERT(cnts[1] > 0);
+    TEST_ASSERT(cnts[1] == cnts[2] && cnts[1] == cnts[3]);
+
+    rc = fcb_append_to_scratch(fcb);
+    TEST_ASSERT(rc == 0);
+    rc = fcb_append_to_scratch(fcb);
+    TEST_ASSERT(rc != 0);
+}
+
+TEST_SUITE(fcb_test_all)
+{
+    fcb_test_len();
+
+    fcb_test_init();
+
+    fcb_test_empty_walk();
+
+    fcb_test_append();
+
+    fcb_test_append_too_big();
+
+    fcb_test_append_fill();
+
+    fcb_test_reset();
+
+    fcb_test_rotate();
+
+    fcb_test_multiple_scratch();
+}
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    fcb_test_all();
+
+    return tu_any_failed;
+}
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/id/include/id/id.h
----------------------------------------------------------------------
diff --git a/sys/id/include/id/id.h b/sys/id/include/id/id.h
index 228d324..b1da40b 100644
--- a/sys/id/include/id/id.h
+++ b/sys/id/include/id/id.h
@@ -28,6 +28,6 @@
 /*
  * Initialize manufacturing info storage/reporting.
  */
-int id_init(void);
+void id_init(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/id/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/id/pkg.yml b/sys/id/pkg.yml
index c76cf14..2795d49 100644
--- a/sys/id/pkg.yml
+++ b/sys/id/pkg.yml
@@ -31,8 +31,15 @@ pkg.deps:
     - libs/os
     - libs/util
     - sys/config
-pkg.deps.SHELL:
+pkg.deps.ID_CLI:
     - libs/shell
-pkg.req_apis.SHELL:
+pkg.req_apis.ID_CLI:
     - console
-pkg.cflags.SHELL: -DSHELL_PRESENT
+
+pkg.init_function: id_init
+pkg.init_stage: 5
+
+pkg.syscfg_defs:
+    ID_CLI:
+        description: 'TBD'
+        value: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/id/src/id.c
----------------------------------------------------------------------
diff --git a/sys/id/src/id.c b/sys/id/src/id.c
index 3d2acc3..36eac5a 100644
--- a/sys/id/src/id.c
+++ b/sys/id/src/id.c
@@ -19,11 +19,13 @@
 #include <inttypes.h>
 #include <string.h>
 #include <stdio.h>
+#include <assert.h>
 
-#include <hal/hal_bsp.h>
-#include <os/os.h>
-#include <config/config.h>
-#include <util/base64.h>
+#include "sysinit/sysinit.h"
+#include "hal/hal_bsp.h"
+#include "os/os.h"
+#include "config/config.h"
+#include "util/base64.h"
 
 #include "id/id.h"
 
@@ -108,8 +110,11 @@ id_conf_export(void (*export_func)(char *name, char *val),
     return 0;
 }
 
-int
+void
 id_init(void)
 {
-    return conf_register(&id_conf);
+    int rc;
+
+    rc = conf_register(&id_conf);
+    SYSINIT_PANIC_ASSERT(rc == 0);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/log/include/log/log.h
----------------------------------------------------------------------
diff --git a/sys/log/include/log/log.h b/sys/log/include/log/log.h
index f5772aa..1dc2a7e 100644
--- a/sys/log/include/log/log.h
+++ b/sys/log/include/log/log.h
@@ -19,6 +19,7 @@
 #ifndef __LOG_H__
 #define __LOG_H__
 
+#include "syscfg/syscfg.h"
 #include "log/ignore.h"
 #include "util/cbmem.h"
 
@@ -111,40 +112,35 @@ struct log_entry_hdr {
 
 #define LOG_NAME_MAX_LEN    (64)
 
-/* Compile in Log Debug by default */
-#ifndef LOG_LEVEL
-#define LOG_LEVEL LOG_LEVEL_DEBUG
-#endif
-
-#if LOG_LEVEL <= LOG_LEVEL_DEBUG
+#if MYNEWT_VAL(LOG_LEVEL) <= LOG_LEVEL_DEBUG
 #define LOG_DEBUG(__l, __mod, __msg, ...) log_printf(__l, __mod, \
         LOG_LEVEL_DEBUG, __msg, ##__VA_ARGS__)
 #else
 #define LOG_DEBUG(__l, __mod, ...) IGNORE(__VA_ARGS__)
 #endif
 
-#if LOG_LEVEL <= LOG_LEVEL_INFO
+#if MYNEWT_VAL(LOG_LEVEL) <= LOG_LEVEL_INFO
 #define LOG_INFO(__l, __mod, __msg, ...) log_printf(__l, __mod, \
         LOG_LEVEL_INFO, __msg, ##__VA_ARGS__)
 #else
 #define LOG_INFO(__l, __mod, ...) IGNORE(__VA_ARGS__)
 #endif
 
-#if LOG_LEVEL <= LOG_LEVEL_INFO
+#if MYNEWT_VAL(LOG_LEVEL) <= LOG_LEVEL_INFO
 #define LOG_WARN(__l, __mod, __msg, ...) log_printf(__l, __mod, \
         LOG_LEVEL_WARN, __msg, ##__VA_ARGS__)
 #else
 #define LOG_WARN(__l, __mod, ...) IGNORE(__VA_ARGS__)
 #endif
 
-#if LOG_LEVEL <= LOG_LEVEL_ERROR
+#if MYNEWT_VAL(LOG_LEVEL) <= LOG_LEVEL_ERROR
 #define LOG_ERROR(__l, __mod, __msg, ...) log_printf(__l, __mod, \
         LOG_LEVEL_ERROR, __msg, ##__VA_ARGS__)
 #else
 #define LOG_ERROR(__l, __mod, ...) IGNORE(__VA_ARGS__)
 #endif
 
-#if LOG_LEVEL <= LOG_LEVEL_CRITICAL
+#if MYNEWT_VAL(LOG_LEVEL) <= LOG_LEVEL_CRITICAL
 #define LOG_CRITICAL(__l, __mod, __msg, ...) log_printf(__l, __mod, \
         LOG_LEVEL_CRITICAL, __msg, ##__VA_ARGS__)
 #else
@@ -166,7 +162,7 @@ struct log {
 #define LOGS_NMGR_OP_LOGS_LIST    (5)
 
 /* Log system level functions (for all logs.) */
-int log_init(void);
+void log_init(void);
 struct log *log_list_get_next(struct log *);
 
 /* Log functions, manipulate a single log */
@@ -192,7 +188,7 @@ int log_fcb_handler_init(struct log_handler *, struct fcb *,
                          uint8_t entries);
 
 /* Private */
-#ifdef NEWTMGR_PRESENT
+#if MYNEWT_VAL(LOG_NEWTMGR)
 int log_nmgr_register_group(void);
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/log/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/log/pkg.yml b/sys/log/pkg.yml
index 2bf5036..dc84d2f 100644
--- a/sys/log/pkg.yml
+++ b/sys/log/pkg.yml
@@ -27,18 +27,30 @@ pkg.keywords:
 pkg.deps:
     - libs/os
     - libs/util
-    - libs/testutil
-pkg.deps.SHELL:
-    - libs/shell
-pkg.deps.NEWTMGR:
+pkg.deps.LOG_NEWTMGR:
     - libs/newtmgr
-pkg.deps.FCB:
+pkg.deps.LOG_FCB:
     - hw/hal
     - sys/fcb
-pkg.deps.TEST:
-    - sys/fcb
-pkg.req_apis.SHELL:
-    - console
-pkg.cflags.SHELL: -DSHELL_PRESENT
-pkg.cflags.NEWTMGR: -DNEWTMGR_PRESENT
-pkg.cflags.FCB: -DFCB_PRESENT
+pkg.deps.LOG_CLI:
+    - libs/shell
+
+pkg.init_function: log_init
+pkg.init_stage: 1
+
+pkg.syscfg_defs:
+    LOG_LEVEL:
+        description: 'TBD'
+        value: 0
+
+    LOG_FCB:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_SYS_FCB'
+
+    LOG_CLI:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_LIBS_SHELL'
+
+    LOG_NEWTMGR:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_LIBS_NEWTMGR'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/log/src/log.c
----------------------------------------------------------------------
diff --git a/sys/log/src/log.c b/sys/log/src/log.c
index 22c1159..eb841e7 100644
--- a/sys/log/src/log.c
+++ b/sys/log/src/log.c
@@ -17,26 +17,25 @@
  * under the License.
  */
 
-#include <os/os.h>
-#include "os/os_time.h"
-
+#include <assert.h>
 #include <string.h>
-
-#include <util/cbmem.h>
-
-#include "log/log.h"
-
 #include <stdio.h>
 #include <stdarg.h>
 
-#ifdef SHELL_PRESENT
-#include <shell/shell.h>
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
+#include "os/os.h"
+#include "util/cbmem.h"
+#include "log/log.h"
+
+#if MYNEWT_VAL(LOG_CLI)
+#include "shell/shell.h"
 #endif
 
 static STAILQ_HEAD(, log) g_log_list = STAILQ_HEAD_INITIALIZER(g_log_list);
 static uint8_t log_inited;
 
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(LOG_CLI)
 int shell_log_dump_all_cmd(int, char **);
 struct shell_cmd g_shell_log_cmd = {
     .sc_cmd = "log",
@@ -44,30 +43,26 @@ struct shell_cmd g_shell_log_cmd = {
 };
 #endif
 
-int
+void
 log_init(void)
 {
-#ifdef NEWTMGR_PRESENT
     int rc;
-#endif
+
+    (void)rc;
 
     if (log_inited) {
-        return (0);
+        return;
     }
     log_inited = 1;
 
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(LOG_CLI)
     shell_cmd_register(&g_shell_log_cmd);
 #endif
 
-#ifdef NEWTMGR_PRESENT
+#if MYNEWT_VAL(LOG_NEWTMGR)
     rc = log_nmgr_register_group();
-    if (rc != 0) {
-        return (rc);
-    }
-#endif /* NEWTMGR_PRESENT */
-
-    return (0);
+    SYSINIT_PANIC_ASSERT(rc == 0);
+#endif
 }
 
 struct log *

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/log/src/log_fcb.c
----------------------------------------------------------------------
diff --git a/sys/log/src/log_fcb.c b/sys/log/src/log_fcb.c
index 3bfde04..d150a68 100644
--- a/sys/log/src/log_fcb.c
+++ b/sys/log/src/log_fcb.c
@@ -16,13 +16,17 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifdef FCB_PRESENT
+
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(LOG_FCB)
+
 #include <string.h>
 
-#include <os/os.h>
+#include "os/os.h"
 
-#include <hal/flash_map.h>
-#include <fcb/fcb.h>
+#include "hal/flash_map.h"
+#include "fcb/fcb.h"
 
 #include "log/log.h"
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/log/src/log_nmgr.c
----------------------------------------------------------------------
diff --git a/sys/log/src/log_nmgr.c b/sys/log/src/log_nmgr.c
index b1e90a0..9b2d711 100644
--- a/sys/log/src/log_nmgr.c
+++ b/sys/log/src/log_nmgr.c
@@ -18,12 +18,12 @@
  */
 
 #include <os/os.h>
-
 #include <string.h>
-
 #include <stdio.h>
 
-#ifdef NEWTMGR_PRESENT
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(LOG_NEWTMGR)
 
 #include "newtmgr/newtmgr.h"
 #include "json/json.h"
@@ -498,4 +498,4 @@ err:
     return (rc);
 }
 
-#endif /* NEWTMGR_PRESENT */
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/log/src/log_shell.c
----------------------------------------------------------------------
diff --git a/sys/log/src/log_shell.c b/sys/log/src/log_shell.c
index 3f67ed7..7188b5d 100644
--- a/sys/log/src/log_shell.c
+++ b/sys/log/src/log_shell.c
@@ -17,24 +17,22 @@
  * under the License.
  */
 
+#include "syscfg/syscfg.h"
+
 /* This whole file is conditionally compiled based on whether the 
- * SHELL_PRESENT identity is provided.
+ * log package is configured to use the shell (MYNEWT_VAL(LOG_CLI)).
  */
 
-#ifdef SHELL_PRESENT
-
-#include <os/os.h>
+#if MYNEWT_VAL(LOG_CLI)
 
+#include <stdio.h>
 #include <string.h>
 
-#include <util/cbmem.h>
-
+#include "os/os.h"
+#include "util/cbmem.h"
 #include "log/log.h"
-
-#include <stdio.h>
-
-#include <shell/shell.h>
-#include <console/console.h> 
+#include "shell/shell.h"
+#include "console/console.h"
 
 static int 
 shell_log_dump_entry(struct log *log, void *arg, void *dptr, uint16_t len) 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/log/src/test/log_test.c
----------------------------------------------------------------------
diff --git a/sys/log/src/test/log_test.c b/sys/log/src/test/log_test.c
deleted file mode 100644
index 7230686..0000000
--- a/sys/log/src/test/log_test.c
+++ /dev/null
@@ -1,162 +0,0 @@
-/**
- * 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 <string.h>
-
-#include <os/os.h>
-#include <testutil/testutil.h>
-#include <fcb/fcb.h>
-#include "log/log.h"
-
-static struct flash_area fcb_areas[] = {
-    [0] = {
-        .fa_off = 0x00000000,
-        .fa_size = 16 * 1024
-    },
-    [1] = {
-        .fa_off = 0x00004000,
-        .fa_size = 16 * 1024
-    }
-};
-static struct log_handler log_fcb_handler;
-static struct fcb log_fcb;
-static struct log my_log;
-
-static char *str_logs[] = {
-    "testdata",
-    "1testdata2",
-    NULL
-};
-static int str_idx = 0;
-static int str_max_idx = 0;
-
-TEST_CASE(log_setup_fcb)
-{
-    int rc;
-    int i;
-
-    log_fcb.f_sectors = fcb_areas;
-    log_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
-    log_fcb.f_magic = 0x7EADBADF;
-    log_fcb.f_version = 0;
-
-    for (i = 0; i < log_fcb.f_sector_cnt; i++) {
-        rc = flash_area_erase(&fcb_areas[i], 0, fcb_areas[i].fa_size);
-        TEST_ASSERT(rc == 0);
-    }
-    rc = fcb_init(&log_fcb);
-    TEST_ASSERT(rc == 0);
-    rc = log_fcb_handler_init(&log_fcb_handler, &log_fcb, 0);
-    TEST_ASSERT(rc == 0);
-
-    log_register("log", &my_log, &log_fcb_handler);
-}
-
-TEST_CASE(log_append_fcb)
-{
-    char *str;
-
-    while (1) {
-        str = str_logs[str_max_idx];
-        if (!str) {
-            break;
-        }
-        log_printf(&my_log, 0, 0, str, strlen(str));
-        str_max_idx++;
-    }
-}
-
-static int
-log_test_walk1(struct log *log, void *arg, void *dptr, uint16_t len)
-{
-    int rc;
-    struct log_entry_hdr ueh;
-    char data[128];
-    int dlen;
-
-    TEST_ASSERT(str_idx < str_max_idx);
-
-    rc = log_read(log, dptr, &ueh, 0, sizeof(ueh));
-    TEST_ASSERT(rc == sizeof(ueh));
-
-    dlen = len - sizeof(ueh);
-    TEST_ASSERT(dlen < sizeof(data));
-
-    rc = log_read(log, dptr, data, sizeof(ueh), dlen);
-    TEST_ASSERT(rc == dlen);
-
-    data[rc] = '\0';
-
-    TEST_ASSERT(strlen(str_logs[str_idx]) == dlen);
-    TEST_ASSERT(!memcmp(str_logs[str_idx], data, dlen));
-    str_idx++;
-
-    return 0;
-}
-
-TEST_CASE(log_walk_fcb)
-{
-    int rc;
-
-    str_idx = 0;
-
-    rc = log_walk(&my_log, log_test_walk1, NULL);
-    TEST_ASSERT(rc == 0);
-}
-
-static int
-log_test_walk2(struct log *log, void *arg, void *dptr, uint16_t len)
-{
-    TEST_ASSERT(0);
-    return 0;
-}
-
-TEST_CASE(log_flush_fcb)
-{
-    int rc;
-
-    rc = log_flush(&my_log);
-    TEST_ASSERT(rc == 0);
-
-    rc = log_walk(&my_log, log_test_walk2, NULL);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_SUITE(log_test_all)
-{
-    log_setup_fcb();
-    log_append_fcb();
-    log_walk_fcb();
-    log_flush_fcb();
-}
-
-#ifdef MYNEWT_SELFTEST
-
-int
-main(int argc, char **argv)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    log_init();
-    log_test_all();
-
-    return tu_any_failed;
-}
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/log/test/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/log/test/pkg.yml b/sys/log/test/pkg.yml
new file mode 100644
index 0000000..f3d094a
--- /dev/null
+++ b/sys/log/test/pkg.yml
@@ -0,0 +1,33 @@
+# 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.
+#
+pkg.name: sys/log/test
+pkg.type: unittest
+pkg.description: "Log unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps: 
+    - libs/testutil
+    - sys/log
+
+pkg.deps.SELFTEST:
+    - libs/console/stub
+
+pkg.syscfg_vals:
+    LOG_FCB: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/log/test/src/log_test.c
----------------------------------------------------------------------
diff --git a/sys/log/test/src/log_test.c b/sys/log/test/src/log_test.c
new file mode 100644
index 0000000..6f83c57
--- /dev/null
+++ b/sys/log/test/src/log_test.c
@@ -0,0 +1,163 @@
+/**
+ * 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 <string.h>
+
+#include "syscfg/syscfg.h"
+#include "os/os.h"
+#include "testutil/testutil.h"
+#include "fcb/fcb.h"
+#include "log/log.h"
+
+static struct flash_area fcb_areas[] = {
+    [0] = {
+        .fa_off = 0x00000000,
+        .fa_size = 16 * 1024
+    },
+    [1] = {
+        .fa_off = 0x00004000,
+        .fa_size = 16 * 1024
+    }
+};
+static struct log_handler log_fcb_handler;
+static struct fcb log_fcb;
+static struct log my_log;
+
+static char *str_logs[] = {
+    "testdata",
+    "1testdata2",
+    NULL
+};
+static int str_idx = 0;
+static int str_max_idx = 0;
+
+TEST_CASE(log_setup_fcb)
+{
+    int rc;
+    int i;
+
+    log_fcb.f_sectors = fcb_areas;
+    log_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
+    log_fcb.f_magic = 0x7EADBADF;
+    log_fcb.f_version = 0;
+
+    for (i = 0; i < log_fcb.f_sector_cnt; i++) {
+        rc = flash_area_erase(&fcb_areas[i], 0, fcb_areas[i].fa_size);
+        TEST_ASSERT(rc == 0);
+    }
+    rc = fcb_init(&log_fcb);
+    TEST_ASSERT(rc == 0);
+    rc = log_fcb_handler_init(&log_fcb_handler, &log_fcb, 0);
+    TEST_ASSERT(rc == 0);
+
+    log_register("log", &my_log, &log_fcb_handler);
+}
+
+TEST_CASE(log_append_fcb)
+{
+    char *str;
+
+    while (1) {
+        str = str_logs[str_max_idx];
+        if (!str) {
+            break;
+        }
+        log_printf(&my_log, 0, 0, str, strlen(str));
+        str_max_idx++;
+    }
+}
+
+static int
+log_test_walk1(struct log *log, void *arg, void *dptr, uint16_t len)
+{
+    int rc;
+    struct log_entry_hdr ueh;
+    char data[128];
+    int dlen;
+
+    TEST_ASSERT(str_idx < str_max_idx);
+
+    rc = log_read(log, dptr, &ueh, 0, sizeof(ueh));
+    TEST_ASSERT(rc == sizeof(ueh));
+
+    dlen = len - sizeof(ueh);
+    TEST_ASSERT(dlen < sizeof(data));
+
+    rc = log_read(log, dptr, data, sizeof(ueh), dlen);
+    TEST_ASSERT(rc == dlen);
+
+    data[rc] = '\0';
+
+    TEST_ASSERT(strlen(str_logs[str_idx]) == dlen);
+    TEST_ASSERT(!memcmp(str_logs[str_idx], data, dlen));
+    str_idx++;
+
+    return 0;
+}
+
+TEST_CASE(log_walk_fcb)
+{
+    int rc;
+
+    str_idx = 0;
+
+    rc = log_walk(&my_log, log_test_walk1, NULL);
+    TEST_ASSERT(rc == 0);
+}
+
+static int
+log_test_walk2(struct log *log, void *arg, void *dptr, uint16_t len)
+{
+    TEST_ASSERT(0);
+    return 0;
+}
+
+TEST_CASE(log_flush_fcb)
+{
+    int rc;
+
+    rc = log_flush(&my_log);
+    TEST_ASSERT(rc == 0);
+
+    rc = log_walk(&my_log, log_test_walk2, NULL);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_SUITE(log_test_all)
+{
+    log_setup_fcb();
+    log_append_fcb();
+    log_walk_fcb();
+    log_flush_fcb();
+}
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    log_init();
+    log_test_all();
+
+    return tu_any_failed;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/mn_socket/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/mn_socket/pkg.yml b/sys/mn_socket/pkg.yml
index 0c9c064..4242b46 100644
--- a/sys/mn_socket/pkg.yml
+++ b/sys/mn_socket/pkg.yml
@@ -28,4 +28,3 @@ pkg.keywords:
 pkg.deps:
     - libs/os
     - libs/util
-    - libs/testutil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/mn_socket/src/test/mn_sock_test.c
----------------------------------------------------------------------
diff --git a/sys/mn_socket/src/test/mn_sock_test.c b/sys/mn_socket/src/test/mn_sock_test.c
deleted file mode 100644
index a8d9cc2..0000000
--- a/sys/mn_socket/src/test/mn_sock_test.c
+++ /dev/null
@@ -1,82 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <string.h>
-
-#include <os/os.h>
-#include <testutil/testutil.h>
-
-#include "mn_socket/mn_socket.h"
-
-TEST_CASE(inet_pton_test)
-{
-    int rc;
-    uint8_t addr[8];
-    struct test_vec {
-        char *str;
-        uint8_t cmp[4];
-    };
-    struct test_vec ok_vec[] = {
-        { "1.1.1.1", { 1, 1, 1, 1 } },
-        { "1.2.3.4", { 1, 2, 3, 4 } },
-        { "010.001.255.255", { 10, 1, 255, 255 } },
-        { "001.002.005.006", { 1, 2, 5, 6 } }
-    };
-    struct test_vec invalid_vec[] = {
-        { "a.b.c.d" },
-        { "1a.b3.4.2" },
-        { "1.3.4.2a" },
-        { "1111.3.4.2" },
-        { "3.256.1.0" },
-    };
-    int i;
-
-    for (i = 0; i < sizeof(ok_vec) / sizeof(ok_vec[0]); i++) {
-        memset(addr, 0xa5, sizeof(addr));
-        rc = mn_inet_pton(MN_PF_INET, ok_vec[i].str, addr);
-        TEST_ASSERT(rc == 1);
-        TEST_ASSERT(!memcmp(ok_vec[i].cmp, addr, sizeof(uint32_t)));
-        TEST_ASSERT(addr[5] == 0xa5);
-    }
-    for (i = 0; i < sizeof(invalid_vec) / sizeof(invalid_vec[0]); i++) {
-        rc = mn_inet_pton(MN_PF_INET, invalid_vec[i].str, addr);
-        TEST_ASSERT(rc == 0);
-    }
-}
-
-TEST_SUITE(mn_socket_test_all)
-{
-    inet_pton_test();
-}
-
-#ifdef MYNEWT_SELFTEST
-
-int
-main(int argc, char **argv)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    mn_socket_test_all();
-
-    return tu_any_failed;
-}
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/mn_socket/test/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/mn_socket/test/pkg.yml b/sys/mn_socket/test/pkg.yml
new file mode 100644
index 0000000..b46d24c
--- /dev/null
+++ b/sys/mn_socket/test/pkg.yml
@@ -0,0 +1,30 @@
+# 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.
+#
+pkg.name: sys/mn_socket/test
+pkg.type: unittest
+pkg.description: "Mynewt socket unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps: 
+    - libs/testutil
+    - sys/mn_socket
+
+pkg.deps.SELFTEST:
+    - libs/console/stub

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/mn_socket/test/src/mn_sock_test.c
----------------------------------------------------------------------
diff --git a/sys/mn_socket/test/src/mn_sock_test.c b/sys/mn_socket/test/src/mn_sock_test.c
new file mode 100644
index 0000000..4babb33
--- /dev/null
+++ b/sys/mn_socket/test/src/mn_sock_test.c
@@ -0,0 +1,83 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+
+#include "syscfg/syscfg.h"
+#include "os/os.h"
+#include "testutil/testutil.h"
+
+#include "mn_socket/mn_socket.h"
+
+TEST_CASE(inet_pton_test)
+{
+    int rc;
+    uint8_t addr[8];
+    struct test_vec {
+        char *str;
+        uint8_t cmp[4];
+    };
+    struct test_vec ok_vec[] = {
+        { "1.1.1.1", { 1, 1, 1, 1 } },
+        { "1.2.3.4", { 1, 2, 3, 4 } },
+        { "010.001.255.255", { 10, 1, 255, 255 } },
+        { "001.002.005.006", { 1, 2, 5, 6 } }
+    };
+    struct test_vec invalid_vec[] = {
+        { "a.b.c.d" },
+        { "1a.b3.4.2" },
+        { "1.3.4.2a" },
+        { "1111.3.4.2" },
+        { "3.256.1.0" },
+    };
+    int i;
+
+    for (i = 0; i < sizeof(ok_vec) / sizeof(ok_vec[0]); i++) {
+        memset(addr, 0xa5, sizeof(addr));
+        rc = mn_inet_pton(MN_PF_INET, ok_vec[i].str, addr);
+        TEST_ASSERT(rc == 1);
+        TEST_ASSERT(!memcmp(ok_vec[i].cmp, addr, sizeof(uint32_t)));
+        TEST_ASSERT(addr[5] == 0xa5);
+    }
+    for (i = 0; i < sizeof(invalid_vec) / sizeof(invalid_vec[0]); i++) {
+        rc = mn_inet_pton(MN_PF_INET, invalid_vec[i].str, addr);
+        TEST_ASSERT(rc == 0);
+    }
+}
+
+TEST_SUITE(mn_socket_test_all)
+{
+    inet_pton_test();
+}
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    mn_socket_test_all();
+
+    return tu_any_failed;
+}
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/reboot/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/reboot/pkg.yml b/sys/reboot/pkg.yml
index 45fba7d..f55397b 100644
--- a/sys/reboot/pkg.yml
+++ b/sys/reboot/pkg.yml
@@ -28,9 +28,19 @@ pkg.keywords:
 pkg.deps:
     - libs/os
     - libs/util
-    - libs/testutil
     - sys/fcb
     - sys/log
     - libs/imgmgr
 pkg.req_apis:
     - console
+
+pkg.init_function: log_reboot_pkg_init
+pkg.init_stage: 2
+
+pkg.syscfg_defs:
+    REBOOT_LOG_0_TYPE:
+        description: 'TBD'
+        value:  LOG_TYPE_STORAGE
+    REBOOT_LOG_0_ENTRY_COUNT:
+        description: 'TBD'
+        value:  10



[35/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/test/src/arch/sim/nffs_test.c
----------------------------------------------------------------------
diff --git a/fs/nffs/test/src/arch/sim/nffs_test.c b/fs/nffs/test/src/arch/sim/nffs_test.c
new file mode 100644
index 0000000..87afcfa
--- /dev/null
+++ b/fs/nffs/test/src/arch/sim/nffs_test.c
@@ -0,0 +1,3251 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <stdlib.h>
+#include <errno.h>
+#include "syscfg/syscfg.h"
+#include "hal/hal_flash.h"
+#include "testutil/testutil.h"
+#include "fs/fs.h"
+#include "nffs/nffs.h"
+#include "nffs/nffs_test.h"
+#include "nffs_test_priv.h"
+#include "nffs_priv.h"
+
+int flash_native_memset(uint32_t offset, uint8_t c, uint32_t len);
+
+static const struct nffs_area_desc nffs_area_descs[] = {
+        { 0x00000000, 16 * 1024 },
+        { 0x00004000, 16 * 1024 },
+        { 0x00008000, 16 * 1024 },
+        { 0x0000c000, 16 * 1024 },
+        { 0x00010000, 64 * 1024 },
+        { 0x00020000, 128 * 1024 },
+        { 0x00040000, 128 * 1024 },
+        { 0x00060000, 128 * 1024 },
+        { 0x00080000, 128 * 1024 },
+        { 0x000a0000, 128 * 1024 },
+        { 0x000c0000, 128 * 1024 },
+        { 0x000e0000, 128 * 1024 },
+        { 0, 0 },
+};
+
+static void
+nffs_test_util_assert_ent_name(struct fs_dirent *dirent,
+                               const char *expected_name)
+{
+    char name[NFFS_FILENAME_MAX_LEN + 1];
+    uint8_t name_len;
+    int rc;
+
+    rc = fs_dirent_name(dirent, sizeof name, name, &name_len);
+    TEST_ASSERT(rc == 0);
+    if (rc == 0) {
+        TEST_ASSERT(strcmp(name, expected_name) == 0);
+    }
+}
+
+static void
+nffs_test_util_assert_file_len(struct fs_file *file, uint32_t expected)
+{
+    uint32_t len;
+    int rc;
+
+    rc = fs_filelen(file, &len);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(len == expected);
+}
+
+static void
+nffs_test_util_assert_cache_is_sane(const char *filename)
+{
+    struct nffs_cache_inode *cache_inode;
+    struct nffs_cache_block *cache_block;
+    struct fs_file *fs_file;
+    struct nffs_file *file;
+    uint32_t cache_start;
+    uint32_t cache_end;
+    uint32_t block_end;
+    int rc;
+
+    rc = fs_open(filename, FS_ACCESS_READ, &fs_file);
+    TEST_ASSERT(rc == 0);
+
+    file = (struct nffs_file *)fs_file;
+    rc = nffs_cache_inode_ensure(&cache_inode, file->nf_inode_entry);
+    TEST_ASSERT(rc == 0);
+
+    nffs_cache_inode_range(cache_inode, &cache_start, &cache_end);
+
+    if (TAILQ_EMPTY(&cache_inode->nci_block_list)) {
+        TEST_ASSERT(cache_start == 0 && cache_end == 0);
+    } else {
+        block_end = 0;  /* Pacify gcc. */
+        TAILQ_FOREACH(cache_block, &cache_inode->nci_block_list, ncb_link) {
+            if (cache_block == TAILQ_FIRST(&cache_inode->nci_block_list)) {
+                TEST_ASSERT(cache_block->ncb_file_offset == cache_start);
+            } else {
+                /* Ensure no gap between this block and its predecessor. */
+                TEST_ASSERT(cache_block->ncb_file_offset == block_end);
+            }
+
+            block_end = cache_block->ncb_file_offset +
+                        cache_block->ncb_block.nb_data_len;
+            if (cache_block == TAILQ_LAST(&cache_inode->nci_block_list,
+                                          nffs_cache_block_list)) {
+
+                TEST_ASSERT(block_end == cache_end);
+            }
+        }
+    }
+
+    rc = fs_close(fs_file);
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+nffs_test_util_assert_contents(const char *filename, const char *contents,
+                               int contents_len)
+{
+    struct fs_file *file;
+    uint32_t bytes_read;
+    void *buf;
+    int rc;
+
+    rc = fs_open(filename, FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == 0);
+
+    buf = malloc(contents_len + 1);
+    TEST_ASSERT(buf != NULL);
+
+    rc = fs_read(file, contents_len + 1, buf, &bytes_read);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(bytes_read == contents_len);
+    TEST_ASSERT(memcmp(buf, contents, contents_len) == 0);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    free(buf);
+
+    nffs_test_util_assert_cache_is_sane(filename);
+}
+
+static int
+nffs_test_util_block_count(const char *filename)
+{
+    struct nffs_hash_entry *entry;
+    struct nffs_block block;
+    struct nffs_file *file;
+    struct fs_file *fs_file;
+    int count;
+    int rc;
+
+    rc = fs_open(filename, FS_ACCESS_READ, &fs_file);
+    TEST_ASSERT(rc == 0);
+
+    file = (struct nffs_file *)fs_file;
+    count = 0;
+    entry = file->nf_inode_entry->nie_last_block_entry;
+    while (entry != NULL) {
+        count++;
+        rc = nffs_block_from_hash_entry(&block, entry);
+        TEST_ASSERT(rc == 0);
+        TEST_ASSERT(block.nb_prev != entry);
+        entry = block.nb_prev;
+    }
+
+    rc = fs_close(fs_file);
+    TEST_ASSERT(rc == 0);
+
+    return count;
+}
+
+static void
+nffs_test_util_assert_block_count(const char *filename, int expected_count)
+{
+    int actual_count;
+
+    actual_count = nffs_test_util_block_count(filename);
+    TEST_ASSERT(actual_count == expected_count);
+}
+
+static void
+nffs_test_util_assert_cache_range(const char *filename,
+                                 uint32_t expected_cache_start,
+                                 uint32_t expected_cache_end)
+{
+    struct nffs_cache_inode *cache_inode;
+    struct nffs_file *file;
+    struct fs_file *fs_file;
+    uint32_t cache_start;
+    uint32_t cache_end;
+    int rc;
+
+    rc = fs_open(filename, FS_ACCESS_READ, &fs_file);
+    TEST_ASSERT(rc == 0);
+
+    file = (struct nffs_file *)fs_file;
+    rc = nffs_cache_inode_ensure(&cache_inode, file->nf_inode_entry);
+    TEST_ASSERT(rc == 0);
+
+    nffs_cache_inode_range(cache_inode, &cache_start, &cache_end);
+    TEST_ASSERT(cache_start == expected_cache_start);
+    TEST_ASSERT(cache_end == expected_cache_end);
+
+    rc = fs_close(fs_file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_cache_is_sane(filename);
+}
+
+static void
+nffs_test_util_create_file_blocks(const char *filename,
+                                 const struct nffs_test_block_desc *blocks,
+                                 int num_blocks)
+{
+    struct fs_file *file;
+    uint32_t total_len;
+    uint32_t offset;
+    char *buf;
+    int num_writes;
+    int rc;
+    int i;
+
+    rc = fs_open(filename, FS_ACCESS_WRITE | FS_ACCESS_TRUNCATE, &file);
+    TEST_ASSERT(rc == 0);
+
+    total_len = 0;
+    if (num_blocks <= 0) {
+        num_writes = 1;
+    } else {
+        num_writes = num_blocks;
+    }
+    for (i = 0; i < num_writes; i++) {
+        rc = fs_write(file, blocks[i].data, blocks[i].data_len);
+        TEST_ASSERT(rc == 0);
+
+        total_len += blocks[i].data_len;
+    }
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    buf = malloc(total_len);
+    TEST_ASSERT(buf != NULL);
+
+    offset = 0;
+    for (i = 0; i < num_writes; i++) {
+        memcpy(buf + offset, blocks[i].data, blocks[i].data_len);
+        offset += blocks[i].data_len;
+    }
+    TEST_ASSERT(offset == total_len);
+
+    nffs_test_util_assert_contents(filename, buf, total_len);
+    if (num_blocks > 0) {
+        nffs_test_util_assert_block_count(filename, num_blocks);
+    }
+
+    free(buf);
+}
+
+static void
+nffs_test_util_create_file(const char *filename, const char *contents,
+                           int contents_len)
+{
+    struct nffs_test_block_desc block;
+
+    block.data = contents;
+    block.data_len = contents_len;
+
+    nffs_test_util_create_file_blocks(filename, &block, 0);
+}
+
+static void
+nffs_test_util_append_file(const char *filename, const char *contents,
+                           int contents_len)
+{
+    struct fs_file *file;
+    int rc;
+
+    rc = fs_open(filename, FS_ACCESS_WRITE | FS_ACCESS_APPEND, &file);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_write(file, contents, contents_len);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+nffs_test_copy_area(const struct nffs_area_desc *from,
+                    const struct nffs_area_desc *to)
+{
+    void *buf;
+    int rc;
+
+    TEST_ASSERT(from->nad_length == to->nad_length);
+
+    buf = malloc(from->nad_length);
+    TEST_ASSERT(buf != NULL);
+
+    rc = hal_flash_read(from->nad_flash_id, from->nad_offset, buf,
+                        from->nad_length);
+    TEST_ASSERT(rc == 0);
+
+    rc = hal_flash_erase(from->nad_flash_id, to->nad_offset, to->nad_length);
+    TEST_ASSERT(rc == 0);
+
+    rc = hal_flash_write(to->nad_flash_id, to->nad_offset, buf, to->nad_length);
+    TEST_ASSERT(rc == 0);
+
+    free(buf);
+}
+
+static void
+nffs_test_util_create_subtree(const char *parent_path,
+                             const struct nffs_test_file_desc *elem)
+{
+    char *path;
+    int rc;
+    int i;
+
+    if (parent_path == NULL) {
+        path = malloc(1);
+        TEST_ASSERT(path != NULL);
+        path[0] = '\0';
+    } else {
+        path = malloc(strlen(parent_path) + 1 + strlen(elem->filename) + 1);
+        TEST_ASSERT(path != NULL);
+
+        sprintf(path, "%s/%s", parent_path, elem->filename);
+    }
+
+    if (elem->is_dir) {
+        if (parent_path != NULL) {
+            rc = fs_mkdir(path);
+            TEST_ASSERT(rc == 0);
+        }
+
+        if (elem->children != NULL) {
+            for (i = 0; elem->children[i].filename != NULL; i++) {
+                nffs_test_util_create_subtree(path, elem->children + i);
+            }
+        }
+    } else {
+        nffs_test_util_create_file(path, elem->contents, elem->contents_len);
+    }
+
+    free(path);
+}
+
+static void
+nffs_test_util_create_tree(const struct nffs_test_file_desc *root_dir)
+{
+    nffs_test_util_create_subtree(NULL, root_dir);
+}
+
+#define NFFS_TEST_TOUCHED_ARR_SZ     (16 * 1024)
+static struct nffs_hash_entry
+    *nffs_test_touched_entries[NFFS_TEST_TOUCHED_ARR_SZ];
+static int nffs_test_num_touched_entries;
+
+/*
+ * Recursively descend directory structure
+ */
+static void
+nffs_test_assert_file(const struct nffs_test_file_desc *file,
+                     struct nffs_inode_entry *inode_entry,
+                     const char *path)
+{
+    const struct nffs_test_file_desc *child_file;
+    struct nffs_inode inode;
+    struct nffs_inode_entry *child_inode_entry;
+    char *child_path;
+    int child_filename_len;
+    int path_len;
+    int rc;
+
+    /*
+     * track of hash entries that have been examined
+     */
+    TEST_ASSERT(nffs_test_num_touched_entries < NFFS_TEST_TOUCHED_ARR_SZ);
+    nffs_test_touched_entries[nffs_test_num_touched_entries] =
+        &inode_entry->nie_hash_entry;
+    nffs_test_num_touched_entries++;
+
+    path_len = strlen(path);
+
+    rc = nffs_inode_from_entry(&inode, inode_entry);
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * recursively examine each child of directory
+     */
+    if (nffs_hash_id_is_dir(inode_entry->nie_hash_entry.nhe_id)) {
+        for (child_file = file->children;
+             child_file != NULL && child_file->filename != NULL;
+             child_file++) {
+
+            /*
+             * Construct full pathname for file
+             * Not null terminated
+             */
+            child_filename_len = strlen(child_file->filename);
+            child_path = malloc(path_len + 1 + child_filename_len + 1);
+            TEST_ASSERT(child_path != NULL);
+            memcpy(child_path, path, path_len);
+            child_path[path_len] = '/';
+            memcpy(child_path + path_len + 1, child_file->filename,
+                   child_filename_len);
+            child_path[path_len + 1 + child_filename_len] = '\0';
+
+            /*
+             * Verify child inode can be found using full pathname
+             */
+            rc = nffs_path_find_inode_entry(child_path, &child_inode_entry);
+            if (rc != 0) {
+                TEST_ASSERT(rc == 0);
+            }
+
+            nffs_test_assert_file(child_file, child_inode_entry, child_path);
+
+            free(child_path);
+        }
+    } else {
+        nffs_test_util_assert_contents(path, file->contents,
+                                       file->contents_len);
+    }
+}
+
+static void
+nffs_test_assert_branch_touched(struct nffs_inode_entry *inode_entry)
+{
+    struct nffs_inode_entry *child;
+    int i;
+
+    if (inode_entry == nffs_lost_found_dir) {
+        return;
+    }
+
+    for (i = 0; i < nffs_test_num_touched_entries; i++) {
+        if (nffs_test_touched_entries[i] == &inode_entry->nie_hash_entry) {
+            break;
+        }
+    }
+    TEST_ASSERT(i < nffs_test_num_touched_entries);
+    nffs_test_touched_entries[i] = NULL;
+
+    if (nffs_hash_id_is_dir(inode_entry->nie_hash_entry.nhe_id)) {
+        SLIST_FOREACH(child, &inode_entry->nie_child_list, nie_sibling_next) {
+            nffs_test_assert_branch_touched(child);
+        }
+    }
+}
+
+static void
+nffs_test_assert_child_inode_present(struct nffs_inode_entry *child)
+{
+    const struct nffs_inode_entry *inode_entry;
+    const struct nffs_inode_entry *parent;
+    struct nffs_inode inode;
+    int rc;
+
+    /*
+     * Sucessfully read inode data from flash
+     */
+    rc = nffs_inode_from_entry(&inode, child);
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * Validate parent
+     */
+    parent = inode.ni_parent;
+    TEST_ASSERT(parent != NULL);
+    TEST_ASSERT(nffs_hash_id_is_dir(parent->nie_hash_entry.nhe_id));
+
+    /*
+     * Make sure inode is in parents child list
+     */
+    SLIST_FOREACH(inode_entry, &parent->nie_child_list, nie_sibling_next) {
+        if (inode_entry == child) {
+            return;
+        }
+    }
+
+    TEST_ASSERT(0);
+}
+
+static void
+nffs_test_assert_block_present(struct nffs_hash_entry *block_entry)
+{
+    const struct nffs_inode_entry *inode_entry;
+    struct nffs_hash_entry *cur;
+    struct nffs_block block;
+    int rc;
+
+    /*
+     * Successfully read block data from flash
+     */
+    rc = nffs_block_from_hash_entry(&block, block_entry);
+    TEST_ASSERT(rc == 0);
+
+    /*
+     * Validate owning inode
+     */
+    inode_entry = block.nb_inode_entry;
+    TEST_ASSERT(inode_entry != NULL);
+    TEST_ASSERT(nffs_hash_id_is_file(inode_entry->nie_hash_entry.nhe_id));
+
+    /*
+     * Validate that block is in owning inode's block chain
+     */
+    cur = inode_entry->nie_last_block_entry;
+    while (cur != NULL) {
+        if (cur == block_entry) {
+            return;
+        }
+
+        rc = nffs_block_from_hash_entry(&block, cur);
+        TEST_ASSERT(rc == 0);
+        cur = block.nb_prev;
+    }
+
+    TEST_ASSERT(0);
+}
+
+/*
+ * Recursively verify that the children of each directory are sorted
+ * on the directory children linked list by filename length
+ */
+static void
+nffs_test_assert_children_sorted(struct nffs_inode_entry *inode_entry)
+{
+    struct nffs_inode_entry *child_entry;
+    struct nffs_inode_entry *prev_entry;
+    struct nffs_inode child_inode;
+    struct nffs_inode prev_inode;
+    int cmp;
+    int rc;
+
+    prev_entry = NULL;
+    SLIST_FOREACH(child_entry, &inode_entry->nie_child_list,
+                  nie_sibling_next) {
+        rc = nffs_inode_from_entry(&child_inode, child_entry);
+        TEST_ASSERT(rc == 0);
+
+        if (prev_entry != NULL) {
+            rc = nffs_inode_from_entry(&prev_inode, prev_entry);
+            TEST_ASSERT(rc == 0);
+
+            rc = nffs_inode_filename_cmp_flash(&prev_inode, &child_inode,
+                                               &cmp);
+            TEST_ASSERT(rc == 0);
+            TEST_ASSERT(cmp < 0);
+        }
+
+        if (nffs_hash_id_is_dir(child_entry->nie_hash_entry.nhe_id)) {
+            nffs_test_assert_children_sorted(child_entry);
+        }
+
+        prev_entry = child_entry;
+    }
+}
+
+static void
+nffs_test_assert_system_once(const struct nffs_test_file_desc *root_dir)
+{
+    struct nffs_inode_entry *inode_entry;
+    struct nffs_hash_entry *entry;
+    struct nffs_hash_entry *next;
+    int i;
+
+    nffs_test_num_touched_entries = 0;
+    nffs_test_assert_file(root_dir, nffs_root_dir, "");
+    nffs_test_assert_branch_touched(nffs_root_dir);
+
+    /* Ensure no orphaned inodes or blocks. */
+    NFFS_HASH_FOREACH(entry, i, next) {
+        TEST_ASSERT(entry->nhe_flash_loc != NFFS_FLASH_LOC_NONE);
+        if (nffs_hash_id_is_inode(entry->nhe_id)) {
+            inode_entry = (void *)entry;
+            TEST_ASSERT(inode_entry->nie_refcnt == 1);
+            if (entry->nhe_id == NFFS_ID_ROOT_DIR) {
+                TEST_ASSERT(inode_entry == nffs_root_dir);
+            } else {
+                nffs_test_assert_child_inode_present(inode_entry);
+            }
+        } else {
+            nffs_test_assert_block_present(entry);
+        }
+    }
+
+    /* Ensure proper sorting. */
+    nffs_test_assert_children_sorted(nffs_root_dir);
+}
+
+static void
+nffs_test_assert_system(const struct nffs_test_file_desc *root_dir,
+                        const struct nffs_area_desc *area_descs)
+{
+    int rc;
+
+    /* Ensure files are as specified, and that there are no other files or
+     * orphaned inodes / blocks.
+     */
+    nffs_test_assert_system_once(root_dir);
+
+    /* Force a garbage collection cycle. */
+    rc = nffs_gc(NULL);
+    TEST_ASSERT(rc == 0);
+
+    /* Ensure file system is still as expected. */
+    nffs_test_assert_system_once(root_dir);
+
+    /* Clear cached data and restore from flash (i.e, simulate a reboot). */
+    rc = nffs_misc_reset();
+    TEST_ASSERT(rc == 0);
+    rc = nffs_detect(area_descs);
+    TEST_ASSERT(rc == 0);
+
+    /* Ensure file system is still as expected. */
+    nffs_test_assert_system_once(root_dir);
+}
+
+static void
+nffs_test_assert_area_seqs(int seq1, int count1, int seq2, int count2)
+{
+    struct nffs_disk_area disk_area;
+    int cur1;
+    int cur2;
+    int rc;
+    int i;
+
+    cur1 = 0;
+    cur2 = 0;
+
+    for (i = 0; i < nffs_num_areas; i++) {
+        rc = nffs_flash_read(i, 0, &disk_area, sizeof disk_area);
+        TEST_ASSERT(rc == 0);
+        TEST_ASSERT(nffs_area_magic_is_set(&disk_area));
+        TEST_ASSERT(disk_area.nda_gc_seq == nffs_areas[i].na_gc_seq);
+        if (i == nffs_scratch_area_idx) {
+            TEST_ASSERT(disk_area.nda_id == NFFS_AREA_ID_NONE);
+        }
+
+        if (nffs_areas[i].na_gc_seq == seq1) {
+            cur1++;
+        } else if (nffs_areas[i].na_gc_seq == seq2) {
+            cur2++;
+        } else {
+            TEST_ASSERT(0);
+        }
+    }
+
+    TEST_ASSERT(cur1 == count1 && cur2 == count2);
+}
+
+static void
+nffs_test_mkdir(void)
+{
+    struct fs_file *file;
+    int rc;
+
+
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_mkdir("/a/b/c/d");
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    rc = fs_mkdir("asdf");
+    TEST_ASSERT(rc == FS_EINVAL);
+
+    rc = fs_mkdir("/a");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_mkdir("/a/b");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_mkdir("/a/b/c");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_mkdir("/a/b/c/d");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_open("/a/b/c/d/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "a",
+                .is_dir = 1,
+                .children = (struct nffs_test_file_desc[]) { {
+                    .filename = "b",
+                    .is_dir = 1,
+                    .children = (struct nffs_test_file_desc[]) { {
+                        .filename = "c",
+                        .is_dir = 1,
+                        .children = (struct nffs_test_file_desc[]) { {
+                            .filename = "d",
+                            .is_dir = 1,
+                            .children = (struct nffs_test_file_desc[]) { {
+                                .filename = "myfile.txt",
+                                .contents = NULL,
+                                .contents_len = 0,
+                            }, {
+                                .filename = NULL,
+                            } },
+                        }, {
+                            .filename = NULL,
+                        } },
+                    }, {
+                        .filename = NULL,
+                    } },
+                }, {
+                    .filename = NULL,
+                } },
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_unlink)
+{
+    struct fs_file *file0;
+    struct fs_file *file1;
+    struct fs_file *file2;
+    struct nffs_file *nfs_file;
+    uint8_t buf[64];
+    uint32_t bytes_read;
+    int initial_num_blocks;
+    int initial_num_inodes;
+    int rc;
+
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    initial_num_blocks = nffs_block_entry_pool.mp_num_free;
+    initial_num_inodes = nffs_inode_entry_pool.mp_num_free;
+
+    nffs_test_util_create_file("/file0.txt", "0", 1);
+
+    rc = fs_open("/file0.txt", FS_ACCESS_READ | FS_ACCESS_WRITE, &file0);
+    TEST_ASSERT(rc == 0);
+    nfs_file = (struct nffs_file *)file0;
+    TEST_ASSERT(nfs_file->nf_inode_entry->nie_refcnt == 2);
+
+    rc = fs_unlink("/file0.txt");
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(nfs_file->nf_inode_entry->nie_refcnt == 1);
+
+    rc = fs_open("/file0.txt", FS_ACCESS_READ, &file2);
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    rc = fs_write(file0, "00", 2);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_seek(file0, 0);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_read(file0, sizeof buf, buf, &bytes_read);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(bytes_read == 2);
+    TEST_ASSERT(memcmp(buf, "00", 2) == 0);
+
+    rc = fs_close(file0);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_open("/file0.txt", FS_ACCESS_READ, &file0);
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    /* Ensure the file was fully removed from RAM. */
+    TEST_ASSERT(nffs_inode_entry_pool.mp_num_free == initial_num_inodes);
+    TEST_ASSERT(nffs_block_entry_pool.mp_num_free == initial_num_blocks);
+
+    /*** Nested unlink. */
+    rc = fs_mkdir("/mydir");
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_create_file("/mydir/file1.txt", "1", 2);
+
+    rc = fs_open("/mydir/file1.txt", FS_ACCESS_READ | FS_ACCESS_WRITE, &file1);
+    TEST_ASSERT(rc == 0);
+    nfs_file = (struct nffs_file *)file1;
+    TEST_ASSERT(nfs_file->nf_inode_entry->nie_refcnt == 2);
+
+    rc = fs_unlink("/mydir");
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(nfs_file->nf_inode_entry->nie_refcnt == 1);
+
+    rc = fs_open("/mydir/file1.txt", FS_ACCESS_READ, &file2);
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    rc = fs_write(file1, "11", 2);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_seek(file1, 0);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_read(file1, sizeof buf, buf, &bytes_read);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(bytes_read == 2);
+    TEST_ASSERT(memcmp(buf, "11", 2) == 0);
+
+    rc = fs_close(file1);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_open("/mydir/file1.txt", FS_ACCESS_READ, &file1);
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+
+    /* Ensure the files and directories were fully removed from RAM. */
+    TEST_ASSERT(nffs_inode_entry_pool.mp_num_free == initial_num_inodes);
+    TEST_ASSERT(nffs_block_entry_pool.mp_num_free == initial_num_blocks);
+}
+
+TEST_CASE(nffs_test_rename)
+{
+    struct fs_file *file;
+    const char contents[] = "contents";
+    int rc;
+
+
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_rename("/nonexistent.txt", "/newname.txt");
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    /*** Rename file. */
+    nffs_test_util_create_file("/myfile.txt", contents, sizeof contents);
+
+    rc = fs_rename("/myfile.txt", "badname");
+    TEST_ASSERT(rc == FS_EINVAL);
+
+    rc = fs_rename("/myfile.txt", "/myfile2.txt");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_open("/myfile.txt", FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    nffs_test_util_assert_contents("/myfile2.txt", contents, sizeof contents);
+
+    rc = fs_mkdir("/mydir");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_mkdir("/mydir/leafdir");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_rename("/myfile2.txt", "/mydir/myfile2.txt");
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/mydir/myfile2.txt", contents,
+                                  sizeof contents);
+
+    /*** Rename directory. */
+    rc = fs_rename("/mydir", "badname");
+    TEST_ASSERT(rc == FS_EINVAL);
+
+    /* Don't allow a directory to be moved into a descendent directory. */
+    rc = fs_rename("/mydir", "/mydir/leafdir/a");
+    TEST_ASSERT(rc == FS_EINVAL);
+
+    rc = fs_rename("/mydir", "/mydir2");
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/mydir2/myfile2.txt", contents,
+                                  sizeof contents);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "mydir2",
+                .is_dir = 1,
+                .children = (struct nffs_test_file_desc[]) { {
+                    .filename = "leafdir",
+                    .is_dir = 1,
+                }, {
+                    .filename = "myfile2.txt",
+                    .contents = "contents",
+                    .contents_len = 9,
+                }, {
+                    .filename = NULL,
+                } },
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_truncate)
+{
+    struct fs_file *file;
+    int rc;
+
+
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE | FS_ACCESS_TRUNCATE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 0);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_write(file, "abcdefgh", 8);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 8);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/myfile.txt", "abcdefgh", 8);
+
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE | FS_ACCESS_TRUNCATE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 0);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_write(file, "1234", 4);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 4);
+    TEST_ASSERT(fs_getpos(file) == 4);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/myfile.txt", "1234", 4);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "myfile.txt",
+                .contents = "1234",
+                .contents_len = 4,
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_append)
+{
+    struct fs_file *file;
+    uint32_t len;
+    char c;
+    int rc;
+    int i;
+
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE | FS_ACCESS_APPEND, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 0);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_write(file, "abcdefgh", 8);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 8);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/myfile.txt", "abcdefgh", 8);
+
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE | FS_ACCESS_APPEND, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 8);
+
+    /* File position should always be at the end of a file after an append.
+     * Seek to the middle prior to writing to test this.
+     */
+    rc = fs_seek(file, 2);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 2);
+
+    rc = fs_write(file, "ijklmnop", 8);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 16);
+    rc = fs_write(file, "qrstuvwx", 8);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 24);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/myfile.txt",
+                                  "abcdefghijklmnopqrstuvwx", 24);
+
+    rc = fs_mkdir("/mydir");
+    TEST_ASSERT_FATAL(rc == 0);
+    rc = fs_open("/mydir/gaga.txt", FS_ACCESS_WRITE | FS_ACCESS_APPEND, &file);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /*** Repeated appends to a large file. */
+    for (i = 0; i < 1000; i++) {
+        rc = fs_filelen(file, &len);
+        TEST_ASSERT_FATAL(rc == 0);
+        TEST_ASSERT(len == i);
+
+        c = '0' + i % 10;
+        rc = fs_write(file, &c, 1);
+        TEST_ASSERT_FATAL(rc == 0);
+    }
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/mydir/gaga.txt",
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789"
+        "01234567890123456789012345678901234567890123456789",
+        1000);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "myfile.txt",
+                .contents = "abcdefghijklmnopqrstuvwx",
+                .contents_len = 24,
+            }, {
+                .filename = "mydir",
+                .is_dir = 1,
+                .children = (struct nffs_test_file_desc[]) { {
+                    .filename = "gaga.txt",
+                    .contents =
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    "01234567890123456789012345678901234567890123456789"
+    ,
+                    .contents_len = 1000,
+                }, {
+                    .filename = NULL,
+                } },
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_read)
+{
+    struct fs_file *file;
+    uint8_t buf[16];
+    uint32_t bytes_read;
+    int rc;
+
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_create_file("/myfile.txt", "1234567890", 10);
+
+    rc = fs_open("/myfile.txt", FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 10);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_read(file, 4, buf, &bytes_read);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(bytes_read == 4);
+    TEST_ASSERT(memcmp(buf, "1234", 4) == 0);
+    TEST_ASSERT(fs_getpos(file) == 4);
+
+    rc = fs_read(file, sizeof buf - 4, buf + 4, &bytes_read);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(bytes_read == 6);
+    TEST_ASSERT(memcmp(buf, "1234567890", 10) == 0);
+    TEST_ASSERT(fs_getpos(file) == 10);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(nffs_test_open)
+{
+    struct fs_file *file;
+    struct fs_dir *dir;
+    int rc;
+
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    /*** Fail to open an invalid path (not rooted). */
+    rc = fs_open("file", FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == FS_EINVAL);
+
+    /*** Fail to open a directory (root directory). */
+    rc = fs_open("/", FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == FS_EINVAL);
+
+    /*** Fail to open a nonexistent file for reading. */
+    rc = fs_open("/1234", FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    /*** Fail to open a child of a nonexistent directory. */
+    rc = fs_open("/dir/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == FS_ENOENT);
+    rc = fs_opendir("/dir", &dir);
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    rc = fs_mkdir("/dir");
+    TEST_ASSERT(rc == 0);
+
+    /*** Fail to open a directory. */
+    rc = fs_open("/dir", FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == FS_EINVAL);
+
+    /*** Successfully open an existing file for reading. */
+    nffs_test_util_create_file("/dir/file.txt", "1234567890", 10);
+    rc = fs_open("/dir/file.txt", FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == 0);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    /*** Successfully open an nonexistent file for writing. */
+    rc = fs_open("/dir/file2.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    /*** Ensure the file can be reopened. */
+    rc = fs_open("/dir/file.txt", FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == 0);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(nffs_test_overwrite_one)
+{
+    struct fs_file *file;
+    int rc;
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_append_file("/myfile.txt", "abcdefgh", 8);
+
+    /*** Overwrite within one block (middle). */
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_seek(file, 3);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 3);
+
+    rc = fs_write(file, "12", 2);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 5);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/myfile.txt", "abc12fgh", 8);
+    nffs_test_util_assert_block_count("/myfile.txt", 1);
+
+    /*** Overwrite within one block (start). */
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_write(file, "xy", 2);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 2);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/myfile.txt", "xyc12fgh", 8);
+    nffs_test_util_assert_block_count("/myfile.txt", 1);
+
+    /*** Overwrite within one block (end). */
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_seek(file, 6);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 6);
+
+    rc = fs_write(file, "<>", 2);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 8);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/myfile.txt", "xyc12f<>", 8);
+    nffs_test_util_assert_block_count("/myfile.txt", 1);
+
+    /*** Overwrite one block middle, extend. */
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_seek(file, 4);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 8);
+    TEST_ASSERT(fs_getpos(file) == 4);
+
+    rc = fs_write(file, "abcdefgh", 8);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 12);
+    TEST_ASSERT(fs_getpos(file) == 12);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/myfile.txt", "xyc1abcdefgh", 12);
+    nffs_test_util_assert_block_count("/myfile.txt", 1);
+
+    /*** Overwrite one block start, extend. */
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 12);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_write(file, "abcdefghijklmnop", 16);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 16);
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents("/myfile.txt", "abcdefghijklmnop", 16);
+    nffs_test_util_assert_block_count("/myfile.txt", 1);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "myfile.txt",
+                .contents = "abcdefghijklmnop",
+                .contents_len = 16,
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_overwrite_two)
+{
+    struct nffs_test_block_desc *blocks = (struct nffs_test_block_desc[]) { {
+        .data = "abcdefgh",
+        .data_len = 8,
+    }, {
+        .data = "ijklmnop",
+        .data_len = 8,
+    } };
+
+    struct fs_file *file;
+    int rc;
+
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    /*** Overwrite two blocks (middle). */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 2);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_seek(file, 7);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 7);
+
+    rc = fs_write(file, "123", 3);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 10);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt", "abcdefg123klmnop", 16);
+    nffs_test_util_assert_block_count("/myfile.txt", 2);
+
+    /*** Overwrite two blocks (start). */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 2);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_write(file, "ABCDEFGHIJ", 10);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 10);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt", "ABCDEFGHIJklmnop", 16);
+    nffs_test_util_assert_block_count("/myfile.txt", 2);
+
+    /*** Overwrite two blocks (end). */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 2);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_seek(file, 6);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 6);
+
+    rc = fs_write(file, "1234567890", 10);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 16);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt", "abcdef1234567890", 16);
+    nffs_test_util_assert_block_count("/myfile.txt", 2);
+
+    /*** Overwrite two blocks middle, extend. */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 2);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_seek(file, 6);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 6);
+
+    rc = fs_write(file, "1234567890!@#$", 14);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 20);
+    TEST_ASSERT(fs_getpos(file) == 20);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt", "abcdef1234567890!@#$", 20);
+    nffs_test_util_assert_block_count("/myfile.txt", 2);
+
+    /*** Overwrite two blocks start, extend. */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 2);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 16);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_write(file, "1234567890!@#$%^&*()", 20);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 20);
+    TEST_ASSERT(fs_getpos(file) == 20);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt", "1234567890!@#$%^&*()", 20);
+    nffs_test_util_assert_block_count("/myfile.txt", 2);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "myfile.txt",
+                .contents = "1234567890!@#$%^&*()",
+                .contents_len = 20,
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_overwrite_three)
+{
+    struct nffs_test_block_desc *blocks = (struct nffs_test_block_desc[]) { {
+        .data = "abcdefgh",
+        .data_len = 8,
+    }, {
+        .data = "ijklmnop",
+        .data_len = 8,
+    }, {
+        .data = "qrstuvwx",
+        .data_len = 8,
+    } };
+
+    struct fs_file *file;
+    int rc;
+
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    /*** Overwrite three blocks (middle). */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_seek(file, 6);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 6);
+
+    rc = fs_write(file, "1234567890!@", 12);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 18);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt",
+                                   "abcdef1234567890!@stuvwx", 24);
+    nffs_test_util_assert_block_count("/myfile.txt", 3);
+
+    /*** Overwrite three blocks (start). */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_write(file, "1234567890!@#$%^&*()", 20);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 20);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt",
+                                   "1234567890!@#$%^&*()uvwx", 24);
+    nffs_test_util_assert_block_count("/myfile.txt", 3);
+
+    /*** Overwrite three blocks (end). */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_seek(file, 6);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 6);
+
+    rc = fs_write(file, "1234567890!@#$%^&*", 18);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 24);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt",
+                                   "abcdef1234567890!@#$%^&*", 24);
+    nffs_test_util_assert_block_count("/myfile.txt", 3);
+
+    /*** Overwrite three blocks middle, extend. */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_seek(file, 6);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 6);
+
+    rc = fs_write(file, "1234567890!@#$%^&*()", 20);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 26);
+    TEST_ASSERT(fs_getpos(file) == 26);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt",
+                                   "abcdef1234567890!@#$%^&*()", 26);
+    nffs_test_util_assert_block_count("/myfile.txt", 3);
+
+    /*** Overwrite three blocks start, extend. */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_write(file, "1234567890!@#$%^&*()abcdefghij", 30);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 30);
+    TEST_ASSERT(fs_getpos(file) == 30);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt",
+                                   "1234567890!@#$%^&*()abcdefghij", 30);
+    nffs_test_util_assert_block_count("/myfile.txt", 3);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "myfile.txt",
+                .contents = "1234567890!@#$%^&*()abcdefghij",
+                .contents_len = 30,
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_overwrite_many)
+{
+    struct nffs_test_block_desc *blocks = (struct nffs_test_block_desc[]) { {
+        .data = "abcdefgh",
+        .data_len = 8,
+    }, {
+        .data = "ijklmnop",
+        .data_len = 8,
+    }, {
+        .data = "qrstuvwx",
+        .data_len = 8,
+    } };
+
+    struct fs_file *file;
+    int rc;
+
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    /*** Overwrite middle of first block. */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_seek(file, 3);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 3);
+
+    rc = fs_write(file, "12", 2);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 5);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt",
+                                   "abc12fghijklmnopqrstuvwx", 24);
+    nffs_test_util_assert_block_count("/myfile.txt", 3);
+
+    /*** Overwrite end of first block, start of second. */
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
+    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 0);
+
+    rc = fs_seek(file, 6);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 6);
+
+    rc = fs_write(file, "1234", 4);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_file_len(file, 24);
+    TEST_ASSERT(fs_getpos(file) == 10);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_contents( "/myfile.txt",
+                                   "abcdef1234klmnopqrstuvwx", 24);
+    nffs_test_util_assert_block_count("/myfile.txt", 3);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "myfile.txt",
+                .contents = "abcdef1234klmnopqrstuvwx",
+                .contents_len = 24,
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_long_filename)
+{
+    int rc;
+
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_create_file("/12345678901234567890.txt", "contents", 8);
+
+    rc = fs_mkdir("/longdir12345678901234567890");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_rename("/12345678901234567890.txt",
+                    "/longdir12345678901234567890/12345678901234567890.txt");
+    TEST_ASSERT(rc == 0);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "longdir12345678901234567890",
+                .is_dir = 1,
+                .children = (struct nffs_test_file_desc[]) { {
+                    .filename = "/12345678901234567890.txt",
+                    .contents = "contents",
+                    .contents_len = 8,
+                }, {
+                    .filename = NULL,
+                } },
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_large_write)
+{
+    static char data[NFFS_BLOCK_MAX_DATA_SZ_MAX * 5];
+    int rc;
+    int i;
+
+    static const struct nffs_area_desc area_descs_two[] = {
+        { 0x00020000, 128 * 1024 },
+        { 0x00040000, 128 * 1024 },
+        { 0, 0 },
+    };
+
+
+
+    /*** Setup. */
+    rc = nffs_format(area_descs_two);
+    TEST_ASSERT(rc == 0);
+
+    for (i = 0; i < sizeof data; i++) {
+        data[i] = i;
+    }
+
+    nffs_test_util_create_file("/myfile.txt", data, sizeof data);
+
+    /* Ensure large write was split across the appropriate number of data
+     * blocks.
+     */
+    TEST_ASSERT(nffs_test_util_block_count("/myfile.txt") ==
+           sizeof data / NFFS_BLOCK_MAX_DATA_SZ_MAX);
+
+    /* Garbage collect and then ensure the large file is still properly divided
+     * according to max data block size.
+     */
+    nffs_gc(NULL);
+    TEST_ASSERT(nffs_test_util_block_count("/myfile.txt") ==
+           sizeof data / NFFS_BLOCK_MAX_DATA_SZ_MAX);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "myfile.txt",
+                .contents = data,
+                .contents_len = sizeof data,
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, area_descs_two);
+}
+
+TEST_CASE(nffs_test_many_children)
+{
+    int rc;
+
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_create_file("/zasdf", NULL, 0);
+    nffs_test_util_create_file("/FfD", NULL, 0);
+    nffs_test_util_create_file("/4Zvv", NULL, 0);
+    nffs_test_util_create_file("/*(*2fs", NULL, 0);
+    nffs_test_util_create_file("/pzzd", NULL, 0);
+    nffs_test_util_create_file("/zasdf0", NULL, 0);
+    nffs_test_util_create_file("/23132.bin", NULL, 0);
+    nffs_test_util_create_file("/asldkfjaldskfadsfsdf.txt", NULL, 0);
+    nffs_test_util_create_file("/sdgaf", NULL, 0);
+    nffs_test_util_create_file("/939302**", NULL, 0);
+    rc = fs_mkdir("/dir");
+    nffs_test_util_create_file("/dir/itw82", NULL, 0);
+    nffs_test_util_create_file("/dir/124", NULL, 0);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) {
+                { "zasdf" },
+                { "FfD" },
+                { "4Zvv" },
+                { "*(*2fs" },
+                { "pzzd" },
+                { "zasdf0" },
+                { "23132.bin" },
+                { "asldkfjaldskfadsfsdf.txt" },
+                { "sdgaf" },
+                { "939302**" },
+                {
+                    .filename = "dir",
+                    .is_dir = 1,
+                    .children = (struct nffs_test_file_desc[]) {
+                        { "itw82" },
+                        { "124" },
+                        { NULL },
+                    },
+                },
+                { NULL },
+            }
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_gc)
+{
+    int rc;
+
+    static const struct nffs_area_desc area_descs_two[] = {
+        { 0x00020000, 128 * 1024 },
+        { 0x00040000, 128 * 1024 },
+        { 0, 0 },
+    };
+
+    struct nffs_test_block_desc blocks[8] = { {
+        .data = "1",
+        .data_len = 1,
+    }, {
+        .data = "2",
+        .data_len = 1,
+    }, {
+        .data = "3",
+        .data_len = 1,
+    }, {
+        .data = "4",
+        .data_len = 1,
+    }, {
+        .data = "5",
+        .data_len = 1,
+    }, {
+        .data = "6",
+        .data_len = 1,
+    }, {
+        .data = "7",
+        .data_len = 1,
+    }, {
+        .data = "8",
+        .data_len = 1,
+    } };
+
+
+    rc = nffs_format(area_descs_two);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 8);
+
+    nffs_gc(NULL);
+
+    nffs_test_util_assert_block_count("/myfile.txt", 1);
+}
+
+TEST_CASE(nffs_test_wear_level)
+{
+    int rc;
+    int i;
+    int j;
+
+    static const struct nffs_area_desc area_descs_uniform[] = {
+        { 0x00000000, 2 * 1024 },
+        { 0x00020000, 2 * 1024 },
+        { 0x00040000, 2 * 1024 },
+        { 0x00060000, 2 * 1024 },
+        { 0x00080000, 2 * 1024 },
+        { 0, 0 },
+    };
+
+
+    /*** Setup. */
+    rc = nffs_format(area_descs_uniform);
+    TEST_ASSERT(rc == 0);
+
+    /* Ensure areas rotate properly. */
+    for (i = 0; i < 255; i++) {
+        for (j = 0; j < nffs_num_areas; j++) {
+            nffs_test_assert_area_seqs(i, nffs_num_areas - j, i + 1, j);
+            nffs_gc(NULL);
+        }
+    }
+
+    /* Ensure proper rollover of sequence numbers. */
+    for (j = 0; j < nffs_num_areas; j++) {
+        nffs_test_assert_area_seqs(255, nffs_num_areas - j, 0, j);
+        nffs_gc(NULL);
+    }
+    for (j = 0; j < nffs_num_areas; j++) {
+        nffs_test_assert_area_seqs(0, nffs_num_areas - j, 1, j);
+        nffs_gc(NULL);
+    }
+}
+
+TEST_CASE(nffs_test_corrupt_scratch)
+{
+    int non_scratch_id;
+    int scratch_id;
+    int rc;
+
+    static const struct nffs_area_desc area_descs_two[] = {
+        { 0x00020000, 128 * 1024 },
+        { 0x00040000, 128 * 1024 },
+        { 0, 0 },
+    };
+
+
+    /*** Setup. */
+    rc = nffs_format(area_descs_two);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_create_file("/myfile.txt", "contents", 8);
+
+    /* Copy the current contents of the non-scratch area to the scratch area.
+     * This will make the scratch area look like it only partially participated
+     * in a garbage collection cycle.
+     */
+    scratch_id = nffs_scratch_area_idx;
+    non_scratch_id = scratch_id ^ 1;
+    nffs_test_copy_area(area_descs_two + non_scratch_id,
+                       area_descs_two + nffs_scratch_area_idx);
+
+    /* Add some more data to the non-scratch area. */
+    rc = fs_mkdir("/mydir");
+    TEST_ASSERT(rc == 0);
+
+    /* Ensure the file system is successfully detected and valid, despite
+     * corruption.
+     */
+
+    rc = nffs_misc_reset();
+    TEST_ASSERT(rc == 0);
+
+    rc = nffs_detect(area_descs_two);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(nffs_scratch_area_idx == scratch_id);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "mydir",
+                .is_dir = 1,
+            }, {
+                .filename = "myfile.txt",
+                .contents = "contents",
+                .contents_len = 8,
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, area_descs_two);
+}
+
+/*
+ * This test no longer works with the current implementation. The
+ * expectation is that intermediate blocks can be removed and the old
+ * method of finding the last current block after restore will allow the
+ * file to be salvaged. Instead, the file should be removed and all data
+ * declared invalid.
+ */
+TEST_CASE(nffs_test_incomplete_block)
+{
+    struct nffs_block block;
+    struct fs_file *fs_file;
+    struct nffs_file *file;
+    uint32_t flash_offset;
+    uint32_t area_offset;
+    uint8_t area_idx;
+    int rc;
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_mkdir("/mydir");
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_create_file("/mydir/a", "aaaa", 4);
+    nffs_test_util_create_file("/mydir/b", "bbbb", 4);
+    nffs_test_util_create_file("/mydir/c", "cccc", 4);
+
+    /* Add a second block to the 'b' file. */
+    nffs_test_util_append_file("/mydir/b", "1234", 4);
+
+    /* Corrupt the 'b' file; make it look like the second block only got half
+     * written.
+     */
+    rc = fs_open("/mydir/b", FS_ACCESS_READ, &fs_file);
+    TEST_ASSERT(rc == 0);
+    file = (struct nffs_file *)fs_file;
+
+    rc = nffs_block_from_hash_entry(&block,
+                                   file->nf_inode_entry->nie_last_block_entry);
+    TEST_ASSERT(rc == 0);
+
+    nffs_flash_loc_expand(block.nb_hash_entry->nhe_flash_loc, &area_idx,
+                         &area_offset);
+    flash_offset = nffs_areas[area_idx].na_offset + area_offset;
+    /*
+     * Overwrite block data - the CRC check should pick this up
+     */
+    rc = flash_native_memset(
+            flash_offset + sizeof (struct nffs_disk_block) + 2, 0xff, 2);
+    TEST_ASSERT(rc == 0);
+
+    rc = nffs_misc_reset();
+    TEST_ASSERT(rc == 0);
+    rc = nffs_detect(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    /* OLD: The entire second block should be removed; the file should only
+     * contain the first block.
+     * Unless we can salvage the block, the entire file should probably be
+     * removed. This is a contrived example which generates bad data on the
+     * what happens to be the last block, but corruption can actually occur
+     * in any block. Sweep should be updated to search look for blocks that
+     * don't have a correct prev_id and then decide whether to delete the
+     * owning inode. XXX
+     */
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "mydir",
+                .is_dir = 1,
+                .children = (struct nffs_test_file_desc[]) { {
+                    .filename = "a",
+                    .contents = "aaaa",
+                    .contents_len = 4,
+#if 0
+/* keep this out until sweep updated to capture bad blocks XXX */
+                }, {
+                    .filename = "b",
+                    .contents = "bbbb",
+                    .contents_len = 4,
+#endif
+                }, {
+                    .filename = "c",
+                    .contents = "cccc",
+                    .contents_len = 4,
+                }, {
+                    .filename = NULL,
+                } },
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_corrupt_block)
+{
+    struct nffs_block block;
+    struct fs_file *fs_file;
+    struct nffs_file *file;
+    uint32_t flash_offset;
+    uint32_t area_offset;
+    uint8_t area_idx;
+    uint8_t off;    /* offset to corrupt */
+    int rc;
+    struct nffs_disk_block ndb;
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_mkdir("/mydir");
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_create_file("/mydir/a", "aaaa", 4);
+    nffs_test_util_create_file("/mydir/b", "bbbb", 4);
+    nffs_test_util_create_file("/mydir/c", "cccc", 4);
+
+    /* Add a second block to the 'b' file. */
+    nffs_test_util_append_file("/mydir/b", "1234", 4);
+
+    /* Corrupt the 'b' file; overwrite the second block's magic number. */
+    rc = fs_open("/mydir/b", FS_ACCESS_READ, &fs_file);
+    TEST_ASSERT(rc == 0);
+    file = (struct nffs_file *)fs_file;
+
+    rc = nffs_block_from_hash_entry(&block,
+                                   file->nf_inode_entry->nie_last_block_entry);
+    TEST_ASSERT(rc == 0);
+
+    nffs_flash_loc_expand(block.nb_hash_entry->nhe_flash_loc, &area_idx,
+                         &area_offset);
+    flash_offset = nffs_areas[area_idx].na_offset + area_offset;
+
+    /*
+     * Overwriting the reserved16 field should invalidate the CRC
+     */
+    off = (char*)&ndb.reserved16 - (char*)&ndb;
+    rc = flash_native_memset(flash_offset + off, 0x43, 1);
+
+    TEST_ASSERT(rc == 0);
+
+    /* Write a fourth file. This file should get restored even though the
+     * previous object has an invalid magic number.
+     */
+    nffs_test_util_create_file("/mydir/d", "dddd", 4);
+
+    rc = nffs_misc_reset();
+    TEST_ASSERT(rc == 0);
+    rc = nffs_detect(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    /* The entire second block should be removed; the file should only contain
+     * the first block.
+     */
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "mydir",
+                .is_dir = 1,
+                .children = (struct nffs_test_file_desc[]) { {
+                    .filename = "a",
+                    .contents = "aaaa",
+                    .contents_len = 4,
+#if 0
+                /*
+                 * In the newer implementation without the find_file_ends
+                 * corrupted inodes are deleted rather than retained with
+                 * partial contents
+                 */
+                }, {
+                    .filename = "b",
+                    .contents = "bbbb",
+                    .contents_len = 4,
+#endif
+                }, {
+                    .filename = "c",
+                    .contents = "cccc",
+                    .contents_len = 4,
+                }, {
+                    .filename = "d",
+                    .contents = "dddd",
+                    .contents_len = 4,
+                }, {
+                    .filename = NULL,
+                } },
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_large_unlink)
+{
+    static char file_contents[1024 * 4];
+    char filename[256];
+    int rc;
+    int i;
+    int j;
+    int k;
+
+
+    /*** Setup. */
+    nffs_config.nc_num_inodes = 1024;
+    nffs_config.nc_num_blocks = 1024;
+
+    rc = nffs_init();
+    TEST_ASSERT(rc == 0);
+
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    for (i = 0; i < 5; i++) {
+        snprintf(filename, sizeof filename, "/dir0_%d", i);
+        rc = fs_mkdir(filename);
+        TEST_ASSERT(rc == 0);
+
+        for (j = 0; j < 5; j++) {
+            snprintf(filename, sizeof filename, "/dir0_%d/dir1_%d", i, j);
+            rc = fs_mkdir(filename);
+            TEST_ASSERT(rc == 0);
+
+            for (k = 0; k < 5; k++) {
+                snprintf(filename, sizeof filename,
+                         "/dir0_%d/dir1_%d/file2_%d", i, j, k);
+                nffs_test_util_create_file(filename, file_contents,
+                                          sizeof file_contents);
+            }
+        }
+
+        for (j = 0; j < 15; j++) {
+            snprintf(filename, sizeof filename, "/dir0_%d/file1_%d", i, j);
+            nffs_test_util_create_file(filename, file_contents,
+                                      sizeof file_contents);
+        }
+    }
+
+    for (i = 0; i < 5; i++) {
+        snprintf(filename, sizeof filename, "/dir0_%d", i);
+        rc = fs_unlink(filename);
+        TEST_ASSERT(rc == 0);
+    }
+
+    /* The entire file system should be empty. */
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_large_system)
+{
+    int rc;
+
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_create_tree(nffs_test_system_01);
+
+    nffs_test_assert_system(nffs_test_system_01, nffs_area_descs);
+
+    rc = fs_unlink("/lvl1dir-0000");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_unlink("/lvl1dir-0004");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_mkdir("/lvl1dir-0000");
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_assert_system(nffs_test_system_01_rm_1014_mk10, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_lost_found)
+{
+    char buf[32];
+    struct nffs_inode_entry *inode_entry;
+    uint32_t flash_offset;
+    uint32_t area_offset;
+    uint8_t area_idx;
+    int rc;
+    struct nffs_disk_inode ndi;
+    uint8_t off;    /* calculated offset for memset */
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_mkdir("/mydir");
+    TEST_ASSERT(rc == 0);
+    rc = fs_mkdir("/mydir/dir1");
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_create_file("/mydir/file1", "aaaa", 4);
+    nffs_test_util_create_file("/mydir/dir1/file2", "bbbb", 4);
+
+    /* Corrupt the mydir inode. */
+    rc = nffs_path_find_inode_entry("/mydir", &inode_entry);
+    TEST_ASSERT(rc == 0);
+
+    snprintf(buf, sizeof buf, "%lu",
+             (unsigned long)inode_entry->nie_hash_entry.nhe_id);
+
+    nffs_flash_loc_expand(inode_entry->nie_hash_entry.nhe_flash_loc,
+                         &area_idx, &area_offset);
+    flash_offset = nffs_areas[area_idx].na_offset + area_offset;
+    /*
+     * Overwrite the sequence number - should be detected as CRC corruption
+     */
+    off = (char*)&ndi.ndi_seq - (char*)&ndi;
+    rc = flash_native_memset(flash_offset + off, 0xaa, 1);
+    TEST_ASSERT(rc == 0);
+
+    /* Clear cached data and restore from flash (i.e, simulate a reboot). */
+    rc = nffs_misc_reset();
+    TEST_ASSERT(rc == 0);
+    rc = nffs_detect(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    /* All contents should now be in the lost+found dir. */
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "lost+found",
+                .is_dir = 1,
+#if 0
+                .children = (struct nffs_test_file_desc[]) { {
+                    .filename = buf,
+                    .is_dir = 1,
+                    .children = (struct nffs_test_file_desc[]) { {
+                        .filename = "file1",
+                        .contents = "aaaa",
+                        .contents_len = 4,
+                    }, {
+                        .filename = "dir1",
+                        .is_dir = 1,
+                        .children = (struct nffs_test_file_desc[]) { {
+                            .filename = "file2",
+                            .contents = "bbbb",
+                            .contents_len = 4,
+                        }, {
+                            .filename = NULL,
+                        } },
+                    }, {
+                        .filename = NULL,
+                    } },
+                }, {
+                    .filename = NULL,
+                } },
+#endif
+            }, {
+                .filename = NULL,
+            } }
+    } };
+
+    nffs_test_assert_system(expected_system, nffs_area_descs);
+}
+
+TEST_CASE(nffs_test_cache_large_file)
+{
+    static char data[NFFS_BLOCK_MAX_DATA_SZ_MAX * 5];
+    struct fs_file *file;
+    uint8_t b;
+    int rc;
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_create_file("/myfile.txt", data, sizeof data);
+    nffs_cache_clear();
+
+    /* Opening a file should not cause any blocks to get cached. */
+    rc = fs_open("/myfile.txt", FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_cache_range("/myfile.txt", 0, 0);
+
+    /* Cache first block. */
+    rc = fs_seek(file, nffs_block_max_data_sz * 0);
+    TEST_ASSERT(rc == 0);
+    rc = fs_read(file, 1, &b, NULL);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_cache_range("/myfile.txt",
+                                     nffs_block_max_data_sz * 0,
+                                     nffs_block_max_data_sz * 1);
+
+    /* Cache second block. */
+    rc = fs_seek(file, nffs_block_max_data_sz * 1);
+    TEST_ASSERT(rc == 0);
+    rc = fs_read(file, 1, &b, NULL);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_cache_range("/myfile.txt",
+                                     nffs_block_max_data_sz * 0,
+                                     nffs_block_max_data_sz * 2);
+
+
+    /* Cache fourth block; prior cache should get erased. */
+    rc = fs_seek(file, nffs_block_max_data_sz * 3);
+    TEST_ASSERT(rc == 0);
+    rc = fs_read(file, 1, &b, NULL);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_cache_range("/myfile.txt",
+                                     nffs_block_max_data_sz * 3,
+                                     nffs_block_max_data_sz * 4);
+
+    /* Cache second and third blocks. */
+    rc = fs_seek(file, nffs_block_max_data_sz * 1);
+    TEST_ASSERT(rc == 0);
+    rc = fs_read(file, 1, &b, NULL);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_cache_range("/myfile.txt",
+                                     nffs_block_max_data_sz * 1,
+                                     nffs_block_max_data_sz * 4);
+
+    /* Cache fifth block. */
+    rc = fs_seek(file, nffs_block_max_data_sz * 4);
+    TEST_ASSERT(rc == 0);
+    rc = fs_read(file, 1, &b, NULL);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_cache_range("/myfile.txt",
+                                     nffs_block_max_data_sz * 1,
+                                     nffs_block_max_data_sz * 5);
+
+    rc = fs_close(file);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(nffs_test_readdir)
+{
+    struct fs_dirent *dirent;
+    struct fs_dir *dir;
+    int rc;
+
+    /*** Setup. */
+    rc = nffs_format(nffs_area_descs);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    rc = fs_mkdir("/mydir");
+    TEST_ASSERT_FATAL(rc == 0);
+
+    nffs_test_util_create_file("/mydir/b", "bbbb", 4);
+    nffs_test_util_create_file("/mydir/a", "aaaa", 4);
+    rc = fs_mkdir("/mydir/c");
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Nonexistent directory. */
+    rc = fs_opendir("/asdf", &dir);
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    /* Fail to opendir a file. */
+    rc = fs_opendir("/mydir/a", &dir);
+    TEST_ASSERT(rc == FS_EINVAL);
+
+    /* Real directory (with trailing slash). */
+    rc = fs_opendir("/mydir/", &dir);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    rc = fs_readdir(dir, &dirent);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_ent_name(dirent, "a");
+    TEST_ASSERT(fs_dirent_is_dir(dirent) == 0);
+
+    rc = fs_readdir(dir, &dirent);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_ent_name(dirent, "b");
+    TEST_ASSERT(fs_dirent_is_dir(dirent) == 0);
+
+    rc = fs_readdir(dir, &dirent);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_ent_name(dirent, "c");
+    TEST_ASSERT(fs_dirent_is_dir(dirent) == 1);
+
+    rc = fs_readdir(dir, &dirent);
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    rc = fs_closedir(dir);
+    TEST_ASSERT(rc == 0);
+
+    /* Root directory. */
+    rc = fs_opendir("/", &dir);
+    TEST_ASSERT(rc == 0);
+    rc = fs_readdir(dir, &dirent);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_ent_name(dirent, "lost+found");
+    TEST_ASSERT(fs_dirent_is_dir(dirent) == 1);
+
+    rc = fs_readdir(dir, &dirent);
+    TEST_ASSERT(rc == 0);
+    nffs_test_util_assert_ent_name(dirent, "mydir");
+    TEST_ASSERT(fs_dirent_is_dir(dirent) == 1);
+
+    rc = fs_closedir(dir);
+    TEST_ASSERT(rc == 0);
+
+    /* Delete entries while iterating. */
+    rc = fs_opendir("/mydir", &dir);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    rc = fs_readdir(dir, &dirent);
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_ent_name(dirent, "a");
+    TEST_ASSERT(fs_dirent_is_dir(dirent) == 0);
+
+    rc = fs_unlink("/mydir/b");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_readdir(dir, &dirent);
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_unlink("/mydir/c");
+    TEST_ASSERT(rc == 0);
+
+    rc = fs_unlink("/mydir");
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_util_assert_ent_name(dirent, "c");
+    TEST_ASSERT(fs_dirent_is_dir(dirent) == 1);
+
+    rc = fs_readdir(dir, &dirent);
+    TEST_ASSERT(rc == FS_ENOENT);
+
+    rc = fs_closedir(dir);
+    TEST_ASSERT(rc == 0);
+
+    /* Ensure directory is gone. */
+    rc = fs_opendir("/mydir", &dir);
+    TEST_ASSERT(rc == FS_ENOENT);
+}
+
+TEST_CASE(nffs_test_split_file)
+{
+    static char data[24 * 1024];
+    int rc;
+    int i;
+
+    /*** Setup. */
+    static const struct nffs_area_desc area_descs_two[] = {
+            { 0x00000000, 16 * 1024 },
+            { 0x00004000, 16 * 1024 },
+            { 0x00008000, 16 * 1024 },
+            { 0, 0 },
+    };
+
+    rc = nffs_format(area_descs_two);
+    TEST_ASSERT(rc == 0);
+
+    for (i = 0; i < sizeof data; i++) {
+        data[i] = i;
+    }
+
+    for (i = 0; i < 256; i++) {
+        nffs_test_util_create_file("/myfile.txt", data, sizeof data);
+        rc = fs_unlink("/myfile.txt");
+        TEST_ASSERT(rc == 0);
+    }
+
+    nffs_test_util_create_file("/myfile.txt", data, sizeof data);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "myfile.txt",
+                .contents = data,
+                .contents_len = sizeof data,
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, area_descs_two);
+}
+
+TEST_CASE(nffs_test_gc_on_oom)
+{
+    int rc;
+
+    /*** Setup. */
+    /* Ensure all areas are the same size. */
+    static const struct nffs_area_desc area_descs_two[] = {
+            { 0x00000000, 16 * 1024 },
+            { 0x00004000, 16 * 1024 },
+            { 0x00008000, 16 * 1024 },
+            { 0, 0 },
+    };
+
+    rc = nffs_format(area_descs_two);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Leak block entries until only four are left. */
+    /* XXX: This is ridiculous.  Need to fix nffs configuration so that the
+     * caller passes a config object rather than writing to a global variable.
+     */
+    while (nffs_block_entry_pool.mp_num_free != 4) {
+        nffs_block_entry_alloc();
+    }
+
+    /*** Write 4 data blocks. */
+    struct nffs_test_block_desc blocks[4] = { {
+        .data = "1",
+        .data_len = 1,
+    }, {
+        .data = "2",
+        .data_len = 1,
+    }, {
+        .data = "3",
+        .data_len = 1,
+    }, {
+        .data = "4",
+        .data_len = 1,
+    } };
+
+    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 4);
+
+    TEST_ASSERT_FATAL(nffs_block_entry_pool.mp_num_free == 0);
+
+    /* Attempt another one-byte write.  This should trigger a garbage
+     * collection cycle, resulting in the four blocks being collated.  The
+     * fifth write consumes an additional block, resulting in 2 out of 4 blocks
+     * in use.
+     */
+    nffs_test_util_append_file("/myfile.txt", "5", 1);
+
+    TEST_ASSERT_FATAL(nffs_block_entry_pool.mp_num_free == 2);
+
+    struct nffs_test_file_desc *expected_system =
+        (struct nffs_test_file_desc[]) { {
+            .filename = "",
+            .is_dir = 1,
+            .children = (struct nffs_test_file_desc[]) { {
+                .filename = "myfile.txt",
+                .contents = "12345",
+                .contents_len = 5,
+            }, {
+                .filename = NULL,
+            } },
+    } };
+
+    nffs_test_assert_system(expected_system, area_descs_two);
+}
+
+TEST_SUITE(nffs_suite_cache)
+{
+    int rc;
+
+    memset(&nffs_config, 0, sizeof nffs_config);
+    nffs_config.nc_num_cache_inodes = 4;
+    nffs_config.nc_num_cache_blocks = 64;
+
+    rc = nffs_init();
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_cache_large_file();
+}
+
+static void
+nffs_test_gen(void)
+{
+    int rc;
+
+    rc = nffs_init();
+    TEST_ASSERT(rc == 0);
+
+    nffs_test_unlink();
+    nffs_test_mkdir();
+    nffs_test_rename();
+    nffs_test_truncate();
+    nffs_test_append();
+    nffs_test_read();
+    nffs_test_open();
+    nffs_test_overwrite_one();
+    nffs_test_overwrite_two();
+    nffs_test_overwrite_three();
+    nffs_test_overwrite_many();
+    nffs_test_long_filename();
+    nffs_test_large_write();
+    nffs_test_many_children();
+    nffs_test_gc();
+    nffs_test_wear_level();
+    nffs_test_corrupt_scratch();
+    nffs_test_incomplete_block();
+    nffs_test_corrupt_block();
+    nffs_test_large_unlink();
+    nffs_test_large_system();
+    nffs_test_lost_found();
+    nffs_test_readdir();
+    nffs_test_split_file();
+    nffs_test_gc_on_oom();
+}
+
+TEST_SUITE(gen_1_1)
+{
+    nffs_config.nc_num_cache_inodes = 1;
+    nffs_config.nc_num_cache_blocks = 1;
+    nffs_test_gen();
+}
+
+TEST_SUITE(gen_4_32)
+{
+    nffs_config.nc_num_cache_inodes = 4;
+    nffs_config.nc_num_cache_blocks = 32;
+    nffs_test_gen();
+}
+
+TEST_SUITE(gen_32_1024)
+{
+    nffs_config.nc_num_cache_inodes = 32;
+    nffs_config.nc_num_cache_blocks = 1024;
+    nffs_test_gen();
+}
+
+int
+nffs_test_all(void)
+{
+    nffs_config.nc_num_inodes = 1024 * 8;
+    nffs_config.nc_num_blocks = 1024 * 20;
+
+    gen_1_1();
+    gen_4_32();
+    gen_32_1024();
+    nffs_suite_cache();
+
+    return tu_any_failed;
+}
+
+void
+print_inode_entry(struct nffs_inode_entry *inode_entry, int indent)
+{
+    struct nffs_inode inode;
+    char name[NFFS_FILENAME_MAX_LEN + 1];
+    uint32_t area_offset;
+    uint8_t area_idx;
+    int rc;
+
+    if (inode_entry == nffs_root_dir) {
+        printf("%*s/\n", indent, "");
+        return;
+    }
+
+    rc = nffs_inode_from_entry(&inode, inode_entry);
+    /*
+     * Dummy inode
+     */
+    if (rc == FS_ENOENT) {
+        printf("    DUMMY %d\n", rc);
+        return;
+    }
+
+    nffs_flash_loc_expand(inode_entry->nie_hash_entry.nhe_flash_loc,
+                         &area_idx, &area_offset);
+
+    rc = nffs_flash_read(area_idx,
+                         area_offset + sizeof (struct nffs_disk_inode),
+                         name, inode.ni_filename_len);
+
+    name[inode.ni_filename_len] = '\0';
+
+    printf("%*s%s\n", indent, "", name[0] == '\0' ? "/" : name);
+}
+
+void
+process_inode_entry(struct nffs_inode_entry *inode_entry, int indent)
+{
+    struct nffs_inode_entry *child;
+
+    print_inode_entry(inode_entry, indent);
+
+    if (nffs_hash_id_is_dir(inode_entry->nie_hash_entry.nhe_id)) {
+        SLIST_FOREACH(child, &inode_entry->nie_child_list, nie_sibling_next) {
+            process_inode_entry(child, indent + 2);
+        }
+    }
+}
+
+int
+print_nffs_flash_inode(struct nffs_area *area, uint32_t off)
+{
+    struct nffs_disk_inode ndi;
+    char filename[128];
+    int len;
+    int rc;
+
+    rc = hal_flash_read(area->na_flash_id, area->na_offset + off,
+                         &ndi, sizeof(ndi));
+    assert(rc == 0);
+
+    memset(filename, 0, sizeof(filename));
+    len = min(sizeof(filename) - 1, ndi.ndi_filename_len);
+    rc = hal_flash_read(area->na_flash_id, area->na_offset + off + sizeof(ndi),
+                         filename, len);
+
+    printf("  off %x %s id %x flen %d seq %d last %x prnt %x flgs %x %s\n",
+           off,
+           (nffs_hash_id_is_file(ndi.ndi_id) ? "File" :
+            (nffs_hash_id_is_dir(ndi.ndi_id) ? "Dir" : "???")),
+           ndi.ndi_id,
+           ndi.ndi_filename_len,
+           ndi.ndi_seq,
+           ndi.ndi_lastblock_id,
+           ndi.ndi_parent_id,
+           ndi.ndi_flags,
+           filename);
+    return sizeof(ndi) + ndi.ndi_filename_len;
+}
+
+int
+print_nffs_flash_block(struct nffs_area *area, uint32_t off)
+{
+    struct nffs_disk_block ndb;
+    int rc;
+
+    rc = hal_flash_read(area->na_flash_id, area->na_offset + off,
+                        &ndb, sizeof(ndb));
+    assert(rc == 0);
+
+    printf("  off %x Block id %x len %d seq %d prev %x own ino %x\n",
+           off,
+           ndb.ndb_id,
+           ndb.ndb_data_len,
+           ndb.ndb_seq,
+           ndb.ndb_prev_id,
+           ndb.ndb_inode_id);
+    return sizeof(ndb) + ndb.ndb_data_len;
+}
+
+int
+print_nffs_flash_object(struct nffs_area *area, uint32_t off)
+{
+    struct nffs_disk_object ndo;
+
+    hal_flash_read(area->na_flash_id, area->na_offset + off,
+                        &ndo.ndo_un_obj, sizeof(ndo.ndo_un_obj));
+
+    if (nffs_hash_id_is_inode(ndo.ndo_disk_inode.ndi_id)) {
+        return print_nffs_flash_inode(area, off);
+
+    } else if (nffs_hash_id_is_block(ndo.ndo_disk_block.ndb_id)) {
+        return print_nffs_flash_block(area, off);
+
+    } else if (ndo.ndo_disk_block.ndb_id == 0xffffffff) {
+        return area->na_length;
+
+    } else {
+        return 1;
+    }
+}
+
+void
+print_nffs_flash_areas(int verbose)
+{
+    struct nffs_area area;
+    struct nffs_disk_area darea;
+    int off;
+    int i;
+
+    for (i = 0; nffs_current_area_descs[i].nad_length != 0; i++) {
+        if (i > NFFS_MAX_AREAS) {
+            return;
+        }
+        area.na_offset = nffs_current_area_descs[i].nad_offset;
+        area.na_length = nffs_current_area_descs[i].nad_length;
+        area.na_flash_id = nffs_current_area_descs[i].nad_flash_id;
+        hal_flash_read(area.na_flash_id, area.na_offset, &darea, sizeof(darea));
+        area.na_id = darea.nda_id;
+        area.na_cur = nffs_areas[i].na_cur;
+        if (!nffs_area_magic_is_set(&darea)) {
+            printf("Area header corrupt!\n");
+        }
+        printf("area %d: id %d %x-%x cur %x len %d flashid %x gc-seq %d %s%s\n",
+               i, area.na_id, area.na_offset, area.na_offset + area.na_length,
+               area.na_cur, area.na_length, area.na_flash_id, darea.nda_gc_seq,
+               nffs_scratch_area_idx == i ? "(scratch)" : "",
+               !nffs_area_magic_is_set(&darea) ? "corrupt" : "");
+        if (verbose < 2) {
+            off = sizeof (struct nffs_disk_area);
+            while (off < area.na_length) {
+                off += print_nffs_flash_object(&area, off);
+            }
+        }
+    }
+}
+
+static int
+nffs_hash_fn(uint32_t id)
+{
+    return id % NFFS_HASH_SIZE;
+}
+
+void
+print_hashlist(struct nffs_hash_entry *he)
+{
+    struct nffs_hash_list *list;
+    int idx = nffs_hash_fn(he->nhe_id);
+    list = nffs_hash + idx;
+
+    SLIST_FOREACH(he, list, nhe_next) {
+        printf("hash_entry %s 0x%x: id 0x%x flash_loc 0x%x next 0x%x\n",
+                   nffs_hash_id_is_inode(he->nhe_id) ? "inode" : "block",
+                   (unsigned int)he,
+                   he->nhe_id, he->nhe_flash_loc,
+                   (unsigned int)he->nhe_next.sle_next);
+   }
+}
+
+void
+print_hash(void)
+{
+    int i;
+    struct nffs_hash_entry *he;
+    struct nffs_hash_entry *next;
+    struct nffs_inode ni;
+    struct nffs_disk_inode di;
+    struct nffs_block nb;
+    struct nffs_disk_block db;
+    uint32_t area_offset;
+    uint8_t area_idx;
+    int rc;
+
+    NFFS_HASH_FOREACH(he, i, next) {
+        if (nffs_hash_id_is_inode(he->nhe_id)) {
+            printf("hash_entry inode %d 0x%x: id 0x%x flash_loc 0x%x next 0x%x\n",
+                   i, (unsigned int)he,
+                   he->nhe_id, he->nhe_flash_loc,
+                   (unsigned int)he->nhe_next.sle_next);
+            if (he->nhe_id == NFFS_ID_ROOT_DIR) {
+                continue;
+            }
+            nffs_flash_loc_expand(he->nhe_flash_loc,
+                                  &area_idx, &area_offset);
+            rc = nffs_inode_read_disk(area_idx, area_offset, &di);
+            if (rc) {
+                printf("%d: fail inode read id 0x%x rc %d\n",
+                   

<TRUNCATED>


[13/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_att_svr_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_att_svr_test.c b/net/nimble/host/test/src/ble_att_svr_test.c
new file mode 100644
index 0000000..1509b41
--- /dev/null
+++ b/net/nimble/host/test/src/ble_att_svr_test.c
@@ -0,0 +1,2314 @@
+/**
+ * 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 <stddef.h>
+#include <errno.h>
+#include <string.h>
+#include "testutil/testutil.h"
+#include "nimble/hci_common.h"
+#include "host/ble_hs_test.h"
+#include "host/ble_uuid.h"
+#include "ble_hs_test_util.h"
+
+static uint8_t *ble_att_svr_test_attr_r_1;
+static uint16_t ble_att_svr_test_attr_r_1_len;
+static uint8_t *ble_att_svr_test_attr_r_2;
+static uint16_t ble_att_svr_test_attr_r_2_len;
+
+static uint8_t ble_att_svr_test_attr_w_1[1024];
+static uint16_t ble_att_svr_test_attr_w_1_len;
+static uint8_t ble_att_svr_test_attr_w_2[1024];
+static uint16_t ble_att_svr_test_attr_w_2_len;
+
+static uint16_t ble_att_svr_test_n_conn_handle;
+static uint16_t ble_att_svr_test_n_attr_handle;
+static uint8_t ble_att_svr_test_attr_n[1024];
+static uint16_t ble_att_svr_test_attr_n_len;
+
+static int
+ble_att_svr_test_misc_gap_cb(struct ble_gap_event *event, void *arg)
+{
+    switch (event->type) {
+    case BLE_GAP_EVENT_NOTIFY_RX:
+        ble_att_svr_test_n_conn_handle = event->notify_rx.conn_handle;
+        ble_att_svr_test_n_attr_handle = event->notify_rx.attr_handle;
+        TEST_ASSERT_FATAL(OS_MBUF_PKTLEN(event->notify_rx.om) <=
+                          sizeof ble_att_svr_test_attr_n);
+        ble_att_svr_test_attr_n_len = OS_MBUF_PKTLEN(event->notify_rx.om);
+        os_mbuf_copydata(event->notify_rx.om, 0, ble_att_svr_test_attr_n_len,
+                         ble_att_svr_test_attr_n);
+        break;
+
+    default:
+        break;
+    }
+
+    return 0;
+}
+
+/**
+ * @return                      The handle of the new test connection.
+ */
+static uint16_t
+ble_att_svr_test_misc_init(uint16_t mtu)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_test_util_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 ble_att_svr_test_misc_gap_cb, NULL);
+
+    ble_hs_lock();
+
+    rc = ble_hs_misc_conn_chan_find(2, BLE_L2CAP_CID_ATT, &conn, &chan);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    if (mtu != 0) {
+        chan->blc_my_mtu = mtu;
+        chan->blc_peer_mtu = mtu;
+        chan->blc_flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
+    }
+
+    ble_hs_unlock();
+
+    ble_att_svr_test_attr_r_1_len = 0;
+    ble_att_svr_test_attr_r_2_len = 0;
+    ble_att_svr_test_attr_w_1_len = 0;
+
+    return 2;
+}
+
+static int
+ble_att_svr_test_misc_attr_fn_r_1(uint16_t conn_handle, uint16_t attr_handle,
+                                  uint8_t op, uint16_t offset,
+                                  struct os_mbuf **om, void *arg)
+{
+    switch (op) {
+    case BLE_ATT_ACCESS_OP_READ:
+        if (offset > ble_att_svr_test_attr_r_1_len) {
+            return BLE_ATT_ERR_INVALID_OFFSET;
+        }
+
+        os_mbuf_append(*om, ble_att_svr_test_attr_r_1 + offset,
+                       ble_att_svr_test_attr_r_1_len - offset);
+        return 0;
+
+    default:
+        return -1;
+    }
+}
+
+static int
+ble_att_svr_test_misc_attr_fn_r_2(uint16_t conn_handle, uint16_t attr_handle,
+                                  uint8_t op, uint16_t offset,
+                                  struct os_mbuf **om, void *arg)
+{
+
+    switch (op) {
+    case BLE_ATT_ACCESS_OP_READ:
+        if (offset > ble_att_svr_test_attr_r_2_len) {
+            return BLE_ATT_ERR_INVALID_OFFSET;
+        }
+
+        os_mbuf_append(*om, ble_att_svr_test_attr_r_2 + offset,
+                       ble_att_svr_test_attr_r_2_len - offset);
+        return 0;
+
+    default:
+        return -1;
+    }
+}
+
+#define BLE_ATT_SVR_TEST_LAST_SVC  11
+#define BLE_ATT_SVR_TEST_LAST_ATTR 24
+
+static int
+ble_att_svr_test_misc_attr_fn_r_group(uint16_t conn_handle,
+                                      uint16_t attr_handle,
+                                      uint8_t op,
+                                      uint16_t offset,
+                                      struct os_mbuf **om,
+                                      void *arg)
+{
+    uint8_t *src;
+    int rc;
+
+    /* Service 0x1122 from 1 to 5 */
+    /* Service 0x2233 from 6 to 10 */
+    /* Service 010203...0f from 11 to 24 */
+
+    static uint8_t vals[25][16] = {
+        [1] =   { 0x22, 0x11 },
+        [2] =   { 0x01, 0x11 },
+        [3] =   { 0x02, 0x11 },
+        [4] =   { 0x03, 0x11 },
+        [5] =   { 0x04, 0x11 },
+        [6] =   { 0x33, 0x22 },
+        [7] =   { 0x01, 0x22 },
+        [8] =   { 0x02, 0x22 },
+        [9] =   { 0x03, 0x22 },
+        [10] =  { 0x04, 0x22 },
+        [11] =  { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 },
+        [12] =  { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
+        [13] =  { 0xdd, 0xdd },
+        [14] =  { 0x55, 0x55 },
+        [15] =  { 0xdd, 0xdd },
+        [16] =  { 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 },
+        [17] =  { 0xdd, 0xdd },
+        [18] =  { 0x66, 0x66 },
+        [19] =  { 0xdd, 0xdd },
+        [20] =  { 0x77, 0x77 },
+        [21] =  { 0xdd, 0xdd },
+        [22] =  { 0x88, 0x88 },
+        [23] =  { 0xdd, 0xdd },
+        [24] =  { 0x99, 0x99 },
+    };
+
+    static uint8_t zeros[14];
+
+    if (op != BLE_ATT_ACCESS_OP_READ) {
+        return -1;
+    }
+
+    TEST_ASSERT_FATAL(attr_handle >= 1 &&
+                      attr_handle <= BLE_ATT_SVR_TEST_LAST_ATTR);
+
+    src = &vals[attr_handle][0];
+    if (memcmp(src + 2, zeros, 14) == 0) {
+        rc = os_mbuf_append(*om, src, 2);
+    } else {
+        rc = os_mbuf_append(*om, src, 16);
+    }
+    if (rc != 0) {
+        return BLE_ATT_ERR_INSUFFICIENT_RES;
+    }
+
+    return 0;
+}
+
+static void
+ble_att_svr_test_misc_register_uuid128(uint8_t *uuid128, uint8_t flags,
+                                       uint16_t expected_handle,
+                                       ble_att_svr_access_fn *fn)
+{
+    uint16_t handle;
+    int rc;
+
+    rc = ble_att_svr_register(uuid128, flags, &handle, fn, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT_FATAL(handle == expected_handle);
+}
+
+static void
+ble_att_svr_test_misc_register_uuid16(uint16_t uuid16, uint8_t flags,
+                                      uint16_t expected_handle,
+                                      ble_att_svr_access_fn *fn)
+{
+    uint8_t uuid128[16];
+    int rc;
+
+    rc = ble_uuid_16_to_128(uuid16, uuid128);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_att_svr_test_misc_register_uuid128(uuid128, flags, expected_handle,
+                                           fn);
+}
+
+static void
+ble_att_svr_test_misc_register_group_attrs(void)
+{
+    /* Service 0x1122 from 1 to 5 */
+    /* Service 0x2233 from 6 to 10 */
+    /* Service 010203...0f from 11 to 24 */
+
+    int i;
+
+    /* Service 0x1122 from 1 to 5 */
+    ble_att_svr_test_misc_register_uuid16(
+        BLE_ATT_UUID_PRIMARY_SERVICE, HA_FLAG_PERM_RW, 1,
+        ble_att_svr_test_misc_attr_fn_r_group);
+    for (i = 2; i <= 5; i++) {
+        if ((i - 2) % 2 == 0) {
+            ble_att_svr_test_misc_register_uuid16(
+                BLE_ATT_UUID_CHARACTERISTIC, HA_FLAG_PERM_RW, i,
+                ble_att_svr_test_misc_attr_fn_r_group);
+        } else {
+            ble_att_svr_test_misc_register_uuid16(
+                i, HA_FLAG_PERM_RW, i,
+                ble_att_svr_test_misc_attr_fn_r_group);
+        }
+    }
+
+    /* Service 0x2233 from 6 to 10 */
+    ble_att_svr_test_misc_register_uuid16(
+        BLE_ATT_UUID_PRIMARY_SERVICE, HA_FLAG_PERM_RW, 6,
+        ble_att_svr_test_misc_attr_fn_r_group);
+    for (i = 7; i <= 10; i++) {
+        ble_att_svr_test_misc_register_uuid16(
+            BLE_ATT_UUID_INCLUDE, HA_FLAG_PERM_RW, i,
+            ble_att_svr_test_misc_attr_fn_r_group);
+    }
+
+    /* Service 010203...0f from 11 to 24 */
+    ble_att_svr_test_misc_register_uuid16(
+        BLE_ATT_UUID_PRIMARY_SERVICE, HA_FLAG_PERM_RW, 11,
+        ble_att_svr_test_misc_attr_fn_r_group);
+    for (i = 12; i <= 24; i++) {
+        if ((i - 12) % 2 == 0) {
+            ble_att_svr_test_misc_register_uuid16(
+                BLE_ATT_UUID_CHARACTERISTIC, HA_FLAG_PERM_RW, i,
+                ble_att_svr_test_misc_attr_fn_r_group);
+        } else {
+            ble_att_svr_test_misc_register_uuid16(
+                i, HA_FLAG_PERM_RW, i,
+                ble_att_svr_test_misc_attr_fn_r_group);
+        }
+    }
+}
+
+static int
+ble_att_svr_test_misc_attr_fn_w_1(uint16_t conn_handle, uint16_t attr_handle,
+                                  uint8_t op, uint16_t offset,
+                                  struct os_mbuf **om, void *arg)
+{
+    switch (op) {
+    case BLE_ATT_ACCESS_OP_WRITE:
+        os_mbuf_copydata(*om, 0, OS_MBUF_PKTLEN(*om),
+                         ble_att_svr_test_attr_w_1);
+        ble_att_svr_test_attr_w_1_len = OS_MBUF_PKTLEN(*om);
+        return 0;
+
+    default:
+        return -1;
+    }
+}
+
+static int
+ble_att_svr_test_misc_attr_fn_w_2(uint16_t conn_handle, uint16_t attr_handle,
+                                  uint8_t op, uint16_t offset,
+                                  struct os_mbuf **om, void *arg)
+{
+    switch (op) {
+    case BLE_ATT_ACCESS_OP_WRITE:
+        os_mbuf_copydata(*om, 0, OS_MBUF_PKTLEN(*om),
+                         ble_att_svr_test_attr_w_2);
+        ble_att_svr_test_attr_w_2_len = OS_MBUF_PKTLEN(*om);
+        return 0;
+
+    default:
+        return -1;
+    }
+}
+
+static int
+ble_att_svr_test_misc_attr_fn_w_fail(uint16_t conn_handle,
+                                     uint16_t attr_handle,
+                                     uint8_t op, uint16_t offset,
+                                     struct os_mbuf **om, void *arg)
+{
+    return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
+}
+
+static void
+ble_att_svr_test_misc_verify_w_1(void *data, int data_len)
+{
+    TEST_ASSERT(ble_att_svr_test_attr_w_1_len == data_len);
+    TEST_ASSERT(memcmp(ble_att_svr_test_attr_w_1, data, data_len) == 0);
+}
+
+static void
+ble_att_svr_test_misc_verify_w_2(void *data, int data_len)
+{
+    TEST_ASSERT(ble_att_svr_test_attr_w_2_len == data_len);
+    TEST_ASSERT(memcmp(ble_att_svr_test_attr_w_2, data, data_len) == 0);
+}
+
+static void
+ble_att_svr_test_misc_verify_tx_err_rsp(uint8_t req_op, uint16_t handle,
+                                        uint8_t error_code)
+{
+    struct ble_att_error_rsp rsp;
+    struct os_mbuf *om;
+    uint8_t buf[BLE_ATT_ERROR_RSP_SZ];
+    int rc;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue();
+
+    rc = os_mbuf_copydata(om, 0, sizeof buf, buf);
+    TEST_ASSERT(rc == 0);
+
+    ble_att_error_rsp_parse(buf, sizeof buf, &rsp);
+
+    TEST_ASSERT(rsp.baep_req_op == req_op);
+    TEST_ASSERT(rsp.baep_handle == handle);
+    TEST_ASSERT(rsp.baep_error_code == error_code);
+}
+
+static void
+ble_att_svr_test_misc_verify_tx_read_blob_rsp(uint8_t *attr_data, int attr_len)
+{
+    struct os_mbuf *om;
+    uint8_t u8;
+    int rc;
+    int i;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue();
+
+    rc = os_mbuf_copydata(om, 0, 1, &u8);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(u8 == BLE_ATT_OP_READ_BLOB_RSP);
+
+    for (i = 0; i < attr_len; i++) {
+        rc = os_mbuf_copydata(om, i + 1, 1, &u8);
+        TEST_ASSERT(rc == 0);
+        TEST_ASSERT(u8 == attr_data[i]);
+    }
+
+    rc = os_mbuf_copydata(om, i + 1, 1, &u8);
+    TEST_ASSERT(rc != 0);
+}
+
+static void
+ble_att_svr_test_misc_rx_read_mult_req(uint16_t conn_handle,
+                                       uint16_t *handles, int num_handles,
+                                       int success)
+{
+    uint8_t buf[256];
+    int off;
+    int rc;
+    int i;
+
+    ble_att_read_mult_req_write(buf, sizeof buf);
+
+    off = BLE_ATT_READ_MULT_REQ_BASE_SZ;
+    for (i = 0; i < num_handles; i++) {
+        htole16(buf + off, handles[i]);
+        off += 2;
+    }
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    if (success) {
+        TEST_ASSERT(rc == 0);
+    } else {
+        TEST_ASSERT(rc != 0);
+    }
+}
+
+static void
+ble_att_svr_test_misc_verify_tx_read_mult_rsp(
+    uint16_t conn_handle, struct ble_hs_test_util_flat_attr *attrs,
+    int num_attrs)
+{
+    struct ble_l2cap_chan *chan;
+    struct os_mbuf *om;
+    uint16_t attr_len;
+    uint16_t mtu;
+    uint8_t u8;
+    int rc;
+    int off;
+    int i;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue();
+
+    rc = os_mbuf_copydata(om, 0, 1, &u8);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(u8 == BLE_ATT_OP_READ_MULT_RSP);
+
+    ble_hs_lock();
+
+    rc = ble_hs_misc_conn_chan_find(conn_handle, BLE_L2CAP_CID_ATT,
+                                    NULL, &chan);
+    TEST_ASSERT_FATAL(rc == 0);
+    mtu = ble_l2cap_chan_mtu(chan);
+
+    ble_hs_unlock();
+
+    off = 1;
+    for (i = 0; i < num_attrs; i++) {
+        attr_len = min(attrs[i].value_len, mtu - off);
+
+        rc = os_mbuf_cmpf(om, off, attrs[i].value, attr_len);
+        TEST_ASSERT(rc == 0);
+
+        off += attr_len;
+    }
+
+    TEST_ASSERT(OS_MBUF_PKTLEN(om) == off);
+}
+
+static void
+ble_att_svr_test_misc_verify_all_read_mult(
+    uint16_t conn_handle, struct ble_hs_test_util_flat_attr *attrs,
+    int num_attrs)
+{
+    uint16_t handles[256];
+    int i;
+
+    TEST_ASSERT_FATAL(num_attrs <= sizeof handles / sizeof handles[0]);
+
+    for (i = 0; i < num_attrs; i++) {
+        handles[i] = attrs[i].handle;
+    }
+
+    ble_att_svr_test_misc_rx_read_mult_req(conn_handle, handles, num_attrs, 1);
+    ble_att_svr_test_misc_verify_tx_read_mult_rsp(conn_handle,
+                                                  attrs, num_attrs);
+}
+
+
+static void
+ble_att_svr_test_misc_verify_tx_write_rsp(void)
+{
+    struct os_mbuf *om;
+    uint8_t u8;
+    int rc;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue();
+
+    rc = os_mbuf_copydata(om, 0, 1, &u8);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(u8 == BLE_ATT_OP_WRITE_RSP);
+}
+
+static void
+ble_att_svr_test_misc_verify_tx_mtu_rsp(uint16_t conn_handle)
+{
+    struct ble_att_mtu_cmd rsp;
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    struct os_mbuf *om;
+    uint8_t buf[BLE_ATT_MTU_CMD_SZ];
+    int rc;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue();
+
+    rc = os_mbuf_copydata(om, 0, sizeof buf, buf);
+    TEST_ASSERT(rc == 0);
+
+    ble_att_mtu_cmd_parse(buf, sizeof buf, &rsp);
+
+    ble_hs_lock();
+    rc = ble_hs_misc_conn_chan_find(conn_handle, BLE_L2CAP_CID_ATT,
+                                    &conn, &chan);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(rsp.bamc_mtu == chan->blc_my_mtu);
+    ble_hs_unlock();
+}
+
+struct ble_att_svr_test_info_entry {
+    uint16_t handle;        /* 0 on last entry */
+    uint16_t uuid16;        /* 0 if not present. */
+    uint8_t uuid128[16];
+};
+
+static void
+ble_att_svr_test_misc_verify_tx_find_info_rsp(
+    struct ble_att_svr_test_info_entry *entries)
+{
+    struct ble_att_svr_test_info_entry *entry;
+    struct ble_att_find_info_rsp rsp;
+    struct os_mbuf *om;
+    uint16_t handle;
+    uint16_t uuid16;
+    uint8_t buf[BLE_ATT_FIND_INFO_RSP_BASE_SZ];
+    uint8_t uuid128[16];
+    int off;
+    int rc;
+
+    ble_hs_test_util_tx_all();
+
+    off = 0;
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+
+    rc = os_mbuf_copydata(om, off, sizeof buf, buf);
+    TEST_ASSERT(rc == 0);
+    off += sizeof buf;
+
+    ble_att_find_info_rsp_parse(buf, sizeof buf, &rsp);
+
+    for (entry = entries; entry->handle != 0; entry++) {
+        rc = os_mbuf_copydata(om, off, 2, &handle);
+        TEST_ASSERT(rc == 0);
+        off += 2;
+
+        handle = le16toh((void *)&handle);
+        TEST_ASSERT(handle == entry->handle);
+
+        if (entry->uuid16 != 0) {
+            TEST_ASSERT(rsp.bafp_format ==
+                        BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT);
+            rc = os_mbuf_copydata(om, off, 2, &uuid16);
+            TEST_ASSERT(rc == 0);
+            off += 2;
+
+            uuid16 = le16toh((void *)&uuid16);
+            TEST_ASSERT(uuid16 == entry->uuid16);
+        } else {
+            TEST_ASSERT(rsp.bafp_format ==
+                        BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT);
+            rc = os_mbuf_copydata(om, off, 16, uuid128);
+            TEST_ASSERT(rc == 0);
+            off += 16;
+
+            TEST_ASSERT(memcmp(uuid128, entry->uuid128, 16) == 0);
+        }
+    }
+
+    /* Ensure there is no extra data in the response. */
+    TEST_ASSERT(off == OS_MBUF_PKTHDR(om)->omp_len);
+}
+
+struct ble_att_svr_test_type_value_entry {
+    uint16_t first;        /* 0 on last entry */
+    uint16_t last;
+};
+
+static void
+ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
+    struct ble_att_svr_test_type_value_entry *entries)
+{
+    struct ble_att_svr_test_type_value_entry *entry;
+    struct os_mbuf *om;
+    uint16_t u16;
+    uint8_t op;
+    int off;
+    int rc;
+
+    ble_hs_test_util_tx_all();
+
+    off = 0;
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+
+    rc = os_mbuf_copydata(om, off, 1, &op);
+    TEST_ASSERT(rc == 0);
+    off += 1;
+
+    TEST_ASSERT(op == BLE_ATT_OP_FIND_TYPE_VALUE_RSP);
+
+    for (entry = entries; entry->first != 0; entry++) {
+        rc = os_mbuf_copydata(om, off, 2, &u16);
+        TEST_ASSERT(rc == 0);
+        htole16(&u16, u16);
+        TEST_ASSERT(u16 == entry->first);
+        off += 2;
+
+        rc = os_mbuf_copydata(om, off, 2, &u16);
+        TEST_ASSERT(rc == 0);
+        htole16(&u16, u16);
+        TEST_ASSERT(u16 == entry->last);
+        off += 2;
+    }
+
+    /* Ensure there is no extra data in the response. */
+    TEST_ASSERT(off == OS_MBUF_PKTHDR(om)->omp_len);
+}
+
+struct ble_att_svr_test_group_type_entry {
+    uint16_t start_handle;  /* 0 on last entry */
+    uint16_t end_handle;    /* 0 on last entry */
+    uint16_t uuid16;        /* 0 if not present. */
+    uint8_t uuid128[16];
+};
+
+/** Returns the number of entries successfully verified. */
+static void
+ble_att_svr_test_misc_verify_tx_read_group_type_rsp(
+    struct ble_att_svr_test_group_type_entry *entries)
+{
+    struct ble_att_svr_test_group_type_entry *entry;
+    struct ble_att_read_group_type_rsp rsp;
+    struct os_mbuf *om;
+    uint16_t u16;
+    uint8_t uuid128[16];
+    int off;
+    int rc;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+
+    ble_att_read_group_type_rsp_parse(om->om_data, om->om_len, &rsp);
+
+    off = BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ;
+    for (entry = entries; entry->start_handle != 0; entry++) {
+        if (entry->uuid16 != 0) {
+            TEST_ASSERT(rsp.bagp_length ==
+                        BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_16);
+        } else {
+            TEST_ASSERT(rsp.bagp_length ==
+                        BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_128);
+        }
+
+        rc = os_mbuf_copydata(om, off, 2, &u16);
+        TEST_ASSERT(rc == 0);
+        htole16(&u16, u16);
+        TEST_ASSERT(u16 == entry->start_handle);
+        off += 2;
+
+        rc = os_mbuf_copydata(om, off, 2, &u16);
+        TEST_ASSERT(rc == 0);
+        htole16(&u16, u16);
+        if (entry->start_handle == BLE_ATT_SVR_TEST_LAST_SVC) {
+            TEST_ASSERT(u16 == 0xffff);
+        } else {
+            TEST_ASSERT(u16 == entry->end_handle);
+        }
+        off += 2;
+
+        if (entry->uuid16 != 0) {
+            rc = os_mbuf_copydata(om, off, 2, &u16);
+            TEST_ASSERT(rc == 0);
+            htole16(&u16, u16);
+            TEST_ASSERT(u16 == entry->uuid16);
+            off += 2;
+        } else {
+            rc = os_mbuf_copydata(om, off, 16, uuid128);
+            TEST_ASSERT(rc == 0);
+            TEST_ASSERT(memcmp(uuid128, entry->uuid128, 16) == 0);
+            off += 16;
+        }
+    }
+
+    /* Ensure there is no extra data in the response. */
+    TEST_ASSERT(off == OS_MBUF_PKTLEN(om));
+}
+
+struct ble_att_svr_test_type_entry {
+    uint16_t handle;  /* 0 on last entry */
+    void *value;
+    int value_len;
+};
+
+/** Returns the number of entries successfully verified. */
+static void
+ble_att_svr_test_misc_verify_tx_read_type_rsp(
+    struct ble_att_svr_test_type_entry *entries)
+{
+    struct ble_att_svr_test_type_entry *entry;
+    struct ble_att_read_type_rsp rsp;
+    struct os_mbuf *om;
+    uint16_t handle;
+    uint8_t buf[512];
+    int off;
+    int rc;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+
+    ble_att_read_type_rsp_parse(om->om_data, om->om_len, &rsp);
+
+    off = BLE_ATT_READ_TYPE_RSP_BASE_SZ;
+    for (entry = entries; entry->handle != 0; entry++) {
+        TEST_ASSERT_FATAL(rsp.batp_length ==
+                          BLE_ATT_READ_TYPE_ADATA_BASE_SZ + entry->value_len);
+
+        rc = os_mbuf_copydata(om, off, 2, &handle);
+        TEST_ASSERT(rc == 0);
+        handle = le16toh(&handle);
+        TEST_ASSERT(handle == entry->handle);
+        off += 2;
+
+        rc = os_mbuf_copydata(om, off, entry->value_len, buf);
+        TEST_ASSERT(rc == 0);
+        TEST_ASSERT(memcmp(entry->value, buf, entry->value_len) == 0);
+        off += entry->value_len;
+    }
+
+    /* Ensure there is no extra data in the response. */
+    TEST_ASSERT(off == OS_MBUF_PKTLEN(om));
+}
+
+static void
+ble_att_svr_test_misc_verify_tx_prep_write_rsp(uint16_t attr_handle,
+                                               uint16_t offset,
+                                               void *data, int data_len)
+{
+    struct ble_att_prep_write_cmd rsp;
+    struct os_mbuf *om;
+    uint8_t buf[1024];
+    int rc;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue();
+
+    rc = os_mbuf_copydata(om, 0, OS_MBUF_PKTLEN(om), buf);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_att_prep_write_rsp_parse(buf, sizeof buf, &rsp);
+
+    TEST_ASSERT(rsp.bapc_handle == attr_handle);
+    TEST_ASSERT(rsp.bapc_offset == offset);
+    TEST_ASSERT(memcmp(buf + BLE_ATT_PREP_WRITE_CMD_BASE_SZ, data,
+                       data_len) == 0);
+
+    TEST_ASSERT(OS_MBUF_PKTLEN(om) ==
+                BLE_ATT_PREP_WRITE_CMD_BASE_SZ + data_len);
+}
+
+static void
+ble_att_svr_test_misc_verify_tx_exec_write_rsp(void)
+{
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    ble_att_exec_write_rsp_parse(om->om_data, om->om_len);
+}
+
+static void
+ble_att_svr_test_misc_mtu_exchange(uint16_t my_mtu, uint16_t peer_sent,
+                                   uint16_t peer_actual, uint16_t chan_mtu)
+{
+    struct ble_att_mtu_cmd req;
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    uint16_t conn_handle;
+    uint8_t buf[BLE_ATT_MTU_CMD_SZ];
+    int rc;
+
+    conn_handle = ble_att_svr_test_misc_init(my_mtu);
+
+    req.bamc_mtu = peer_sent;
+    ble_att_mtu_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+
+    ble_att_svr_test_misc_verify_tx_mtu_rsp(conn_handle);
+
+    ble_hs_lock();
+    rc = ble_hs_misc_conn_chan_find(conn_handle, BLE_L2CAP_CID_ATT,
+                                    &conn, &chan);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(chan->blc_peer_mtu == peer_actual);
+    TEST_ASSERT(ble_l2cap_chan_mtu(chan) == chan_mtu);
+    ble_hs_unlock();
+
+}
+
+static void
+ble_att_svr_test_misc_prep_write(uint16_t conn_handle, uint16_t attr_handle,
+                                 uint16_t offset, void *data,
+                                 int data_len, uint8_t error_code)
+{
+    struct ble_att_prep_write_cmd prep_req;
+    uint8_t buf[1024];
+    int rc;
+
+    prep_req.bapc_handle = attr_handle;
+    prep_req.bapc_offset = offset;
+    ble_att_prep_write_req_write(buf, sizeof buf, &prep_req);
+    memcpy(buf + BLE_ATT_PREP_WRITE_CMD_BASE_SZ, data, data_len);
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(
+        conn_handle, BLE_L2CAP_CID_ATT, buf,
+        BLE_ATT_PREP_WRITE_CMD_BASE_SZ + data_len);
+
+    if (error_code == 0) {
+        TEST_ASSERT(rc == 0);
+        ble_att_svr_test_misc_verify_tx_prep_write_rsp(attr_handle, offset,
+                                                       data, data_len);
+    } else {
+        TEST_ASSERT(rc != 0);
+        ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_PREP_WRITE_REQ,
+                                                attr_handle, error_code);
+    }
+}
+
+static void
+ble_att_svr_test_misc_exec_write(uint16_t conn_handle, uint8_t flags,
+                                 uint8_t error_code, uint16_t error_handle)
+{
+    struct ble_att_exec_write_req exec_req;
+    uint8_t buf[1024];
+    int rc;
+
+    exec_req.baeq_flags = flags;
+    ble_att_exec_write_req_write(buf, sizeof buf, &exec_req);
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf,
+                                                BLE_ATT_EXEC_WRITE_REQ_SZ);
+
+    if (error_code == 0) {
+        TEST_ASSERT(rc == 0);
+        ble_att_svr_test_misc_verify_tx_exec_write_rsp();
+    } else {
+        TEST_ASSERT(rc != 0);
+        ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_EXEC_WRITE_REQ,
+                                                error_handle, error_code);
+    }
+}
+
+static void
+ble_att_svr_test_misc_rx_notify(uint16_t conn_handle, uint16_t attr_handle,
+                                void *attr_val, int attr_len, int good)
+{
+    struct ble_att_notify_req req;
+    uint8_t buf[1024];
+    int off;
+    int rc;
+
+    req.banq_handle = attr_handle;
+    ble_att_notify_req_write(buf, sizeof buf, &req);
+    off = BLE_ATT_NOTIFY_REQ_BASE_SZ;
+
+    memcpy(buf + off, attr_val, attr_len);
+    off += attr_len;
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    if (good) {
+        TEST_ASSERT(rc == 0);
+    } else {
+        TEST_ASSERT(rc == BLE_HS_EBADDATA);
+    }
+}
+
+static void
+ble_att_svr_test_misc_verify_notify(uint16_t conn_handle, uint16_t attr_handle,
+                                    void *attr_val, int attr_len, int good)
+{
+    ble_att_svr_test_n_conn_handle = 0xffff;
+    ble_att_svr_test_n_attr_handle = 0;
+    ble_att_svr_test_attr_n_len = 0;
+
+    ble_att_svr_test_misc_rx_notify(conn_handle, attr_handle, attr_val,
+                                    attr_len, good);
+
+    if (good) {
+        TEST_ASSERT(ble_att_svr_test_n_conn_handle == conn_handle);
+        TEST_ASSERT(ble_att_svr_test_n_attr_handle == attr_handle);
+        TEST_ASSERT(ble_att_svr_test_attr_n_len == attr_len);
+        TEST_ASSERT(memcmp(ble_att_svr_test_attr_n, attr_val, attr_len) == 0);
+    } else {
+        TEST_ASSERT(ble_att_svr_test_n_conn_handle == 0xffff);
+        TEST_ASSERT(ble_att_svr_test_n_attr_handle == 0);
+        TEST_ASSERT(ble_att_svr_test_attr_n_len == 0);
+    }
+}
+
+static void
+ble_att_svr_test_misc_verify_tx_indicate_rsp(void)
+{
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+
+    ble_att_indicate_rsp_parse(om->om_data, om->om_len);
+}
+
+static void
+ble_att_svr_test_misc_rx_indicate(uint16_t conn_handle, uint16_t attr_handle,
+                                  void *attr_val, int attr_len, int good)
+{
+    struct ble_att_indicate_req req;
+    uint8_t buf[1024];
+    int off;
+    int rc;
+
+    req.baiq_handle = attr_handle;
+    ble_att_indicate_req_write(buf, sizeof buf, &req);
+    off = BLE_ATT_INDICATE_REQ_BASE_SZ;
+
+    memcpy(buf + off, attr_val, attr_len);
+    off += attr_len;
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    if (good) {
+        TEST_ASSERT(rc == 0);
+    } else {
+        TEST_ASSERT(rc == BLE_HS_EBADDATA);
+    }
+}
+
+static void
+ble_att_svr_test_misc_verify_indicate(uint16_t conn_handle,
+                                      uint16_t attr_handle,
+                                      void *attr_val, int attr_len, int good)
+{
+    ble_att_svr_test_n_conn_handle = 0xffff;
+    ble_att_svr_test_n_attr_handle = 0;
+    ble_att_svr_test_attr_n_len = 0;
+
+    ble_att_svr_test_misc_rx_indicate(conn_handle, attr_handle, attr_val,
+                                      attr_len, good);
+
+    if (good) {
+        TEST_ASSERT(ble_att_svr_test_n_conn_handle == conn_handle);
+        TEST_ASSERT(ble_att_svr_test_n_attr_handle == attr_handle);
+        TEST_ASSERT(ble_att_svr_test_attr_n_len == attr_len);
+        TEST_ASSERT(memcmp(ble_att_svr_test_attr_n, attr_val, attr_len) == 0);
+        ble_att_svr_test_misc_verify_tx_indicate_rsp();
+    } else {
+        TEST_ASSERT(ble_att_svr_test_n_conn_handle == 0xffff);
+        TEST_ASSERT(ble_att_svr_test_n_attr_handle == 0);
+        TEST_ASSERT(ble_att_svr_test_attr_n_len == 0);
+        ble_hs_test_util_tx_all();
+        TEST_ASSERT(ble_hs_test_util_prev_tx_queue_sz() == 0);
+    }
+}
+
+TEST_CASE(ble_att_svr_test_mtu)
+{
+    /*** MTU too low; should pretend peer sent default value instead. */
+    ble_att_svr_test_misc_mtu_exchange(BLE_ATT_MTU_DFLT, 5,
+                                       BLE_ATT_MTU_DFLT, BLE_ATT_MTU_DFLT);
+
+    /*** MTUs equal. */
+    ble_att_svr_test_misc_mtu_exchange(50, 50, 50, 50);
+
+    /*** Peer's higher than mine. */
+    ble_att_svr_test_misc_mtu_exchange(50, 100, 100, 50);
+
+    /*** Mine higher than peer's. */
+    ble_att_svr_test_misc_mtu_exchange(100, 50, 50, 50);
+}
+
+TEST_CASE(ble_att_svr_test_read)
+{
+    struct ble_att_read_req req;
+    struct ble_hs_conn *conn;
+    struct os_mbuf *om;
+    uint16_t conn_handle;
+    uint8_t buf[BLE_ATT_READ_REQ_SZ];
+    uint8_t uuid_sec[16] = {1};
+    uint8_t uuid[16] = {0};
+    int rc;
+
+    conn_handle = ble_att_svr_test_misc_init(0);
+
+    /*** Nonexistent attribute. */
+    req.barq_handle = 0;
+    ble_att_read_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_REQ, 0,
+                                            BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** Successful read. */
+    ble_att_svr_test_attr_r_1 = (uint8_t[]){0,1,2,3,4,5,6,7};
+    ble_att_svr_test_attr_r_1_len = 8;
+    rc = ble_att_svr_register(uuid, HA_FLAG_PERM_RW, &req.barq_handle,
+                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    ble_att_read_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_hs_test_util_verify_tx_read_rsp(
+        ble_att_svr_test_attr_r_1, ble_att_svr_test_attr_r_1_len);
+
+    /*** Partial read. */
+    ble_att_svr_test_attr_r_1 =
+        (uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
+                    22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39};
+    ble_att_svr_test_attr_r_1_len = 40;
+
+    ble_att_read_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_hs_test_util_verify_tx_read_rsp(ble_att_svr_test_attr_r_1,
+                                        BLE_ATT_MTU_DFLT - 1);
+
+    /*** Read requires encryption. */
+    /* Insufficient authentication. */
+    rc = ble_att_svr_register(uuid_sec, BLE_ATT_F_READ | BLE_ATT_F_READ_ENC,
+                              &req.barq_handle,
+                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    ble_att_read_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_AUTHEN));
+    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_REQ,
+                                            req.barq_handle,
+                                            BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
+
+    /* Security check bypassed for local reads. */
+    rc = ble_att_svr_read_local(req.barq_handle, &om);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(OS_MBUF_PKTLEN(om) == ble_att_svr_test_attr_r_1_len);
+    TEST_ASSERT(os_mbuf_cmpf(om, 0, ble_att_svr_test_attr_r_1,
+                               ble_att_svr_test_attr_r_1_len) == 0);
+    os_mbuf_free_chain(om);
+
+    /* Ensure no response got sent. */
+    ble_hs_test_util_tx_all();
+    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
+
+    /* Encrypt link; success. */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(conn_handle);
+    conn->bhc_sec_state.encrypted = 1;
+    ble_hs_unlock();
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_hs_test_util_verify_tx_read_rsp(ble_att_svr_test_attr_r_1,
+                                        BLE_ATT_MTU_DFLT - 1);
+
+}
+
+TEST_CASE(ble_att_svr_test_read_blob)
+{
+    struct ble_att_read_blob_req req;
+    uint16_t conn_handle;
+    uint8_t buf[BLE_ATT_READ_BLOB_REQ_SZ];
+    uint8_t uuid[16] = {0};
+    int rc;
+
+    conn_handle = ble_att_svr_test_misc_init(0);
+
+    /*** Nonexistent attribute. */
+    req.babq_handle = 0;
+    req.babq_offset = 0;
+    ble_att_read_blob_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_BLOB_REQ, 0,
+                                            BLE_ATT_ERR_INVALID_HANDLE);
+
+
+    /*** Successful partial read. */
+    ble_att_svr_test_attr_r_1 =
+        (uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
+                    22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39};
+    ble_att_svr_test_attr_r_1_len = 40;
+    rc = ble_att_svr_register(uuid, HA_FLAG_PERM_RW, &req.babq_handle,
+                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    ble_att_read_blob_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_blob_rsp(ble_att_svr_test_attr_r_1,
+                                                  BLE_ATT_MTU_DFLT - 1);
+
+    /*** Read remainder of attribute. */
+    req.babq_offset = BLE_ATT_MTU_DFLT - 1;
+    ble_att_read_blob_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_blob_rsp(
+        ble_att_svr_test_attr_r_1 + BLE_ATT_MTU_DFLT - 1,
+        40 - (BLE_ATT_MTU_DFLT - 1));
+
+    /*** Zero-length read. */
+    req.babq_offset = ble_att_svr_test_attr_r_1_len;
+    ble_att_read_blob_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_blob_rsp(ble_att_svr_test_attr_r_1,
+                                                  0);
+
+}
+
+TEST_CASE(ble_att_svr_test_read_mult)
+{
+    uint16_t conn_handle;
+    int rc;
+
+    conn_handle = ble_att_svr_test_misc_init(0);
+
+    struct ble_hs_test_util_flat_attr attrs[2] = {
+        {
+            .handle = 0,
+            .offset = 0,
+            .value = { 1, 2, 3, 4 },
+            .value_len = 4,
+        },
+        {
+            .handle = 0,
+            .offset = 0,
+            .value = { 2, 3, 4, 5, 6 },
+            .value_len = 5,
+        },
+    };
+
+    ble_att_svr_test_attr_r_1 = attrs[0].value;
+    ble_att_svr_test_attr_r_1_len = attrs[0].value_len;
+    ble_att_svr_test_attr_r_2 = attrs[1].value;
+    ble_att_svr_test_attr_r_2_len = attrs[1].value_len;
+
+    rc = ble_att_svr_register(BLE_UUID16(0x1111), HA_FLAG_PERM_RW,
+                              &attrs[0].handle,
+                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    rc = ble_att_svr_register(BLE_UUID16(0x2222), HA_FLAG_PERM_RW,
+                              &attrs[1].handle,
+                              ble_att_svr_test_misc_attr_fn_r_2, NULL);
+    TEST_ASSERT(rc == 0);
+
+    /*** Single nonexistent attribute. */
+    ble_att_svr_test_misc_rx_read_mult_req(
+        conn_handle, ((uint16_t[]){ 100 }), 1, 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_MULT_REQ,
+                                            100, BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** Single attribute. */
+    ble_att_svr_test_misc_verify_all_read_mult(conn_handle, &attrs[0], 1);
+
+    /*** Two attributes. */
+    ble_att_svr_test_misc_verify_all_read_mult(conn_handle, attrs, 2);
+
+    /*** Reverse order. */
+    ble_att_svr_test_misc_verify_all_read_mult(conn_handle, attrs, 2);
+
+    /*** Second attribute nonexistent; verify only error txed. */
+    ble_att_svr_test_misc_rx_read_mult_req(
+        conn_handle, ((uint16_t[]){ attrs[0].handle, 100 }), 2, 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_MULT_REQ,
+                                            100, BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** Response too long; verify only MTU bytes sent. */
+    attrs[0].value_len = 20;
+    memcpy(attrs[0].value,
+           ((uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}),
+           attrs[0].value_len);
+    ble_att_svr_test_attr_r_1_len = attrs[0].value_len;
+
+    attrs[1].value_len = 20;
+    memcpy(attrs[1].value,
+           ((uint8_t[]){
+                22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39
+           }),
+           attrs[1].value_len);
+    ble_att_svr_test_attr_r_2_len = attrs[1].value_len;
+
+    ble_att_svr_test_misc_verify_all_read_mult(conn_handle, attrs, 2);
+
+}
+
+TEST_CASE(ble_att_svr_test_write)
+{
+    struct ble_att_write_req req;
+    struct ble_hs_conn *conn;
+    uint16_t conn_handle;
+    uint8_t buf[BLE_ATT_WRITE_REQ_BASE_SZ + 8];
+    uint8_t uuid_sec[16] = {2};
+    uint8_t uuid_rw[16] = {0};
+    uint8_t uuid_r[16] = {1};
+    int rc;
+
+    conn_handle = ble_att_svr_test_misc_init(0);
+
+    /*** Nonexistent attribute. */
+    req.bawq_handle = 0;
+    ble_att_write_req_write(buf, sizeof buf, &req);
+    memcpy(buf + BLE_ATT_READ_REQ_SZ, ((uint8_t[]){0,1,2,3,4,5,6,7}), 8);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_WRITE_REQ, 0, BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** Write not permitted if non-local. */
+    /* Non-local write (fail). */
+    rc = ble_att_svr_register(uuid_r, BLE_ATT_F_READ, &req.bawq_handle,
+                              ble_att_svr_test_misc_attr_fn_w_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    ble_att_write_req_write(buf, sizeof buf, &req);
+    memcpy(buf + BLE_ATT_WRITE_REQ_BASE_SZ,
+           ((uint8_t[]){0,1,2,3,4,5,6,7}), 8);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == BLE_HS_ENOTSUP);
+    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_WRITE_REQ,
+                                            req.bawq_handle,
+                                            BLE_ATT_ERR_WRITE_NOT_PERMITTED);
+
+    /* Local write (success). */
+    rc = ble_hs_test_util_write_local_flat(req.bawq_handle, buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+
+    /* Ensure no response got sent. */
+    ble_hs_test_util_tx_all();
+    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
+
+    /*** Successful write. */
+    rc = ble_att_svr_register(uuid_rw, HA_FLAG_PERM_RW, &req.bawq_handle,
+                              ble_att_svr_test_misc_attr_fn_w_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    ble_att_write_req_write(buf, sizeof buf, &req);
+    memcpy(buf + BLE_ATT_WRITE_REQ_BASE_SZ,
+           ((uint8_t[]){0,1,2,3,4,5,6,7}), 8);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_write_rsp();
+
+    /*** Write requires encryption. */
+    /* Insufficient authentication. */
+    rc = ble_att_svr_register(uuid_sec, BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC,
+                              &req.bawq_handle,
+                              ble_att_svr_test_misc_attr_fn_w_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    ble_att_write_req_write(buf, sizeof buf, &req);
+    memcpy(buf + BLE_ATT_WRITE_REQ_BASE_SZ,
+           ((uint8_t[]){0,1,2,3,4,5,6,7}), 8);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_AUTHEN));
+    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_WRITE_REQ,
+                                            req.bawq_handle,
+                                            BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
+
+    /* Security check bypassed for local writes. */
+    rc = ble_hs_test_util_write_local_flat(req.bawq_handle, buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+
+    /* Ensure no response got sent. */
+    ble_hs_test_util_tx_all();
+    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
+
+    /* Encrypt link; success. */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(conn_handle);
+    conn->bhc_sec_state.encrypted = 1;
+    ble_hs_unlock();
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_write_rsp();
+}
+
+TEST_CASE(ble_att_svr_test_find_info)
+{
+    struct ble_att_find_info_req req;
+    uint16_t conn_handle;
+    uint16_t handle1;
+    uint16_t handle2;
+    uint16_t handle3;
+    uint8_t buf[BLE_ATT_FIND_INFO_REQ_SZ];
+    uint8_t uuid1[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
+    uint8_t uuid2[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
+    uint8_t uuid3[16] = {
+        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
+        0x00, 0x10, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00
+    };
+    int rc;
+
+    conn_handle = ble_att_svr_test_misc_init(128);
+
+    /*** Start handle of 0. */
+    req.bafq_start_handle = 0;
+    req.bafq_end_handle = 0;
+
+    ble_att_find_info_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_FIND_INFO_REQ, 0, BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** Start handle > end handle. */
+    req.bafq_start_handle = 101;
+    req.bafq_end_handle = 100;
+
+    ble_att_find_info_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_FIND_INFO_REQ, 101, BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** No attributes. */
+    req.bafq_start_handle = 200;
+    req.bafq_end_handle = 300;
+
+    ble_att_find_info_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_FIND_INFO_REQ, 200, BLE_ATT_ERR_ATTR_NOT_FOUND);
+
+    /*** Range too late. */
+    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle1,
+                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    req.bafq_start_handle = 200;
+    req.bafq_end_handle = 300;
+
+    ble_att_find_info_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_FIND_INFO_REQ, 200, BLE_ATT_ERR_ATTR_NOT_FOUND);
+
+    /*** One 128-bit entry. */
+    req.bafq_start_handle = handle1;
+    req.bafq_end_handle = handle1;
+
+    ble_att_find_info_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_find_info_rsp(
+        ((struct ble_att_svr_test_info_entry[]) { {
+            .handle = handle1,
+            .uuid128 = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},
+        }, {
+            .handle = 0,
+        } }));
+
+    /*** Two 128-bit entries. */
+    rc = ble_att_svr_register(uuid2, HA_FLAG_PERM_RW, &handle2,
+                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    req.bafq_start_handle = handle1;
+    req.bafq_end_handle = handle2;
+
+    ble_att_find_info_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_find_info_rsp(
+        ((struct ble_att_svr_test_info_entry[]) { {
+            .handle = handle1,
+            .uuid128 = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},
+        }, {
+            .handle = handle2,
+            .uuid128 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
+        }, {
+            .handle = 0,
+        } }));
+
+    /*** Two 128-bit entries; 16-bit entry doesn't get sent. */
+    rc = ble_att_svr_register(uuid3, HA_FLAG_PERM_RW, &handle3,
+                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    req.bafq_start_handle = handle1;
+    req.bafq_end_handle = handle3;
+
+    ble_att_find_info_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_find_info_rsp(
+        ((struct ble_att_svr_test_info_entry[]) { {
+            .handle = handle1,
+            .uuid128 = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},
+        }, {
+            .handle = handle2,
+            .uuid128 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
+        }, {
+            .handle = 0,
+        } }));
+
+    /*** Remaining 16-bit entry requested. */
+    req.bafq_start_handle = handle3;
+    req.bafq_end_handle = handle3;
+
+    ble_att_find_info_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_find_info_rsp(
+        ((struct ble_att_svr_test_info_entry[]) { {
+            .handle = handle3,
+            .uuid16 = 0x000f,
+        }, {
+            .handle = 0,
+        } }));
+
+}
+
+TEST_CASE(ble_att_svr_test_find_type_value)
+{
+    struct ble_att_find_type_value_req req;
+    uint8_t buf[BLE_ATT_FIND_TYPE_VALUE_REQ_BASE_SZ + 2];
+    uint16_t conn_handle;
+    uint16_t handle1;
+    uint16_t handle2;
+    uint16_t handle3;
+    uint16_t handle4;
+    uint16_t handle5;
+    uint8_t uuid1[16] = {
+        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
+        0x00, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00
+    };
+    uint8_t uuid2[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
+    uint8_t uuid3[16] = {
+        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
+        0x00, 0x10, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00
+    };
+    int rc;
+
+    conn_handle = ble_att_svr_test_misc_init(128);
+
+    /* One-time write of the attribute value at the end of the request. */
+    ble_att_svr_test_attr_r_1 = (uint8_t[]){0x99, 0x99};
+    ble_att_svr_test_attr_r_1_len = 2;
+    memcpy(buf + BLE_ATT_FIND_TYPE_VALUE_REQ_BASE_SZ,
+           ble_att_svr_test_attr_r_1,
+           ble_att_svr_test_attr_r_1_len);
+
+    /*** Start handle of 0. */
+    req.bavq_start_handle = 0;
+    req.bavq_end_handle = 0;
+    req.bavq_attr_type = 0x0001;
+
+    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 0,
+        BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** Start handle > end handle. */
+    req.bavq_start_handle = 101;
+    req.bavq_end_handle = 100;
+
+    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 101,
+        BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** No attributes. */
+    req.bavq_start_handle = 200;
+    req.bavq_end_handle = 300;
+
+    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 200,
+        BLE_ATT_ERR_ATTR_NOT_FOUND);
+
+    /*** Range too late. */
+    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle1,
+                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    req.bavq_start_handle = 200;
+    req.bavq_end_handle = 300;
+
+    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 200,
+        BLE_ATT_ERR_ATTR_NOT_FOUND);
+
+    /*** One entry, one attribute. */
+    req.bavq_start_handle = handle1;
+    req.bavq_end_handle = handle1;
+
+    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
+        ((struct ble_att_svr_test_type_value_entry[]) { {
+            .first = handle1,
+            .last = handle1,
+        }, {
+            .first = 0,
+        } }));
+
+    /*** One entry, two attributes. */
+    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle2,
+                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    req.bavq_start_handle = handle1;
+    req.bavq_end_handle = handle2;
+
+    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
+        ((struct ble_att_svr_test_type_value_entry[]) { {
+            .first = handle1,
+            .last = handle2,
+        }, {
+            .first = 0,
+        } }));
+
+    /*** Entry 1: two attributes; entry 2: one attribute. */
+    rc = ble_att_svr_register(uuid2, HA_FLAG_PERM_RW, &handle3,
+                              ble_att_svr_test_misc_attr_fn_r_2, NULL);
+    TEST_ASSERT(rc == 0);
+
+    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle4,
+                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
+    TEST_ASSERT(rc == 0);
+
+    req.bavq_start_handle = 0x0001;
+    req.bavq_end_handle = 0xffff;
+
+    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
+        ((struct ble_att_svr_test_type_value_entry[]) { {
+            .first = handle1,
+            .last = handle2,
+        }, {
+            .first = handle4,
+            .last = handle4,
+        }, {
+            .first = 0,
+        } }));
+
+    /*** Ensure attribute with wrong value is not included. */
+    ble_att_svr_test_attr_r_2 = (uint8_t[]){0x00, 0x00};
+    ble_att_svr_test_attr_r_2_len = 2;
+
+    req.bavq_start_handle = 0x0001;
+    req.bavq_end_handle = 0xffff;
+
+    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
+        ((struct ble_att_svr_test_type_value_entry[]) { {
+            .first = handle1,
+            .last = handle2,
+        }, {
+            .first = handle4,
+            .last = handle4,
+        }, {
+            .first = 0,
+        } }));
+
+    /*** Ensure attribute with wrong type is not included. */
+    rc = ble_att_svr_register(uuid3, HA_FLAG_PERM_RW, &handle5,
+                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
+
+    req.bavq_start_handle = 0x0001;
+    req.bavq_end_handle = 0xffff;
+
+    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
+        ((struct ble_att_svr_test_type_value_entry[]) { {
+            .first = handle1,
+            .last = handle2,
+        }, {
+            .first = handle4,
+            .last = handle4,
+        }, {
+            .first = 0,
+        } }));
+
+}
+
+static void
+ble_att_svr_test_misc_read_type(uint16_t mtu)
+{
+    struct ble_att_read_type_req req;
+    uint16_t conn_handle;
+    uint8_t buf[BLE_ATT_READ_TYPE_REQ_SZ_16];
+    int rc;
+
+    conn_handle = ble_att_svr_test_misc_init(mtu);
+
+    /*** Start handle of 0. */
+    req.batq_start_handle = 0;
+    req.batq_end_handle = 0;
+
+    ble_att_read_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_READ_TYPE_REQ, 0,
+        BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** Start handle > end handle. */
+    req.batq_start_handle = 101;
+    req.batq_end_handle = 100;
+
+    ble_att_read_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_READ_TYPE_REQ, 101,
+        BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** No attributes. */
+    req.batq_start_handle = 1;
+    req.batq_end_handle = 0xffff;
+
+    ble_att_read_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_READ_TYPE_REQ, 1,
+        BLE_ATT_ERR_ATTR_NOT_FOUND);
+
+    /*** Range too late. */
+    ble_att_svr_test_misc_register_group_attrs();
+    req.batq_start_handle = 200;
+    req.batq_end_handle = 300;
+
+    ble_att_read_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_READ_TYPE_REQ, 200,
+        BLE_ATT_ERR_ATTR_NOT_FOUND);
+
+    /*** One characteristic from one service. */
+    req.batq_start_handle = 1;
+    req.batq_end_handle = 2;
+
+    ble_att_read_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_CHARACTERISTIC);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_type_rsp(
+        ((struct ble_att_svr_test_type_entry[]) { {
+            .handle = 2,
+            .value = (uint8_t[]){ 0x01, 0x11 },
+            .value_len = 2,
+        }, {
+            .handle = 0,
+        } }));
+
+    /*** Both characteristics from one service. */
+    req.batq_start_handle = 1;
+    req.batq_end_handle = 10;
+
+    ble_att_read_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_CHARACTERISTIC);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_type_rsp(
+        ((struct ble_att_svr_test_type_entry[]) { {
+            .handle = 2,
+            .value = (uint8_t[]){ 0x01, 0x11 },
+            .value_len = 2,
+        }, {
+            .handle = 4,
+            .value = (uint8_t[]){ 0x03, 0x11 },
+            .value_len = 2,
+        }, {
+            .handle = 0,
+        } }));
+
+    /*** Ensure 16-bit and 128-bit values are retrieved separately. */
+    req.batq_start_handle = 11;
+    req.batq_end_handle = 0xffff;
+
+    ble_att_read_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_CHARACTERISTIC);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_type_rsp(
+        ((struct ble_att_svr_test_type_entry[]) { {
+            .handle = 12,
+            .value = (uint8_t[]){ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
+            .value_len = 16,
+        }, {
+            .handle = 0,
+        } }));
+
+    req.batq_start_handle = 13;
+    req.batq_end_handle = 0xffff;
+
+    ble_att_read_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_CHARACTERISTIC);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_type_rsp(
+        ((struct ble_att_svr_test_type_entry[]) { {
+            .handle = 14,
+            .value = (uint8_t[]){ 0x55, 0x55 },
+            .value_len = 2,
+        }, {
+            .handle = 0,
+        } }));
+
+    req.batq_start_handle = 15;
+    req.batq_end_handle = 0xffff;
+
+    ble_att_read_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_CHARACTERISTIC);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_type_rsp(
+        ((struct ble_att_svr_test_type_entry[]) { {
+            .handle = 16,
+            .value = (uint8_t[]){ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 },
+            .value_len = 16,
+        }, {
+            .handle = 0,
+        } }));
+
+    /*** Read until the end of the attribute list. */
+    req.batq_start_handle = 17;
+    req.batq_end_handle = 0xffff;
+
+    ble_att_read_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_CHARACTERISTIC);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_type_rsp(
+        ((struct ble_att_svr_test_type_entry[]) { {
+            .handle = 18,
+            .value = (uint8_t[]){ 0x66, 0x66 },
+            .value_len = 2,
+        }, {
+            .handle = 20,
+            .value = (uint8_t[]){ 0x77, 0x77 },
+            .value_len = 2,
+        }, {
+            .handle = 22,
+            .value = (uint8_t[]){ 0x88, 0x88 },
+            .value_len = 2,
+        }, {
+            .handle = 24,
+            .value = (uint8_t[]){ 0x99, 0x99 },
+            .value_len = 2,
+        }, {
+            .handle = 0,
+        } }));
+
+}
+
+TEST_CASE(ble_att_svr_test_read_type)
+{
+    ble_att_svr_test_misc_read_type(0);
+    ble_att_svr_test_misc_read_type(128);
+}
+
+TEST_CASE(ble_att_svr_test_read_group_type)
+{
+    struct ble_att_read_group_type_req req;
+    uint16_t conn_handle;
+    uint8_t buf[BLE_ATT_READ_GROUP_TYPE_REQ_SZ_16];
+    int rc;
+
+    conn_handle = ble_att_svr_test_misc_init(128);
+
+    /*** Start handle of 0. */
+    req.bagq_start_handle = 0;
+    req.bagq_end_handle = 0;
+
+    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_READ_GROUP_TYPE_REQ, 0,
+        BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** Start handle > end handle. */
+    req.bagq_start_handle = 101;
+    req.bagq_end_handle = 100;
+
+    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_READ_GROUP_TYPE_REQ, 101,
+        BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** Invalid group UUID (0x1234). */
+    req.bagq_start_handle = 110;
+    req.bagq_end_handle = 150;
+
+    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ, 0x1234);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_READ_GROUP_TYPE_REQ, 110,
+        BLE_ATT_ERR_UNSUPPORTED_GROUP);
+
+    /*** No attributes. */
+    req.bagq_start_handle = 1;
+    req.bagq_end_handle = 0xffff;
+
+    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_READ_GROUP_TYPE_REQ, 1,
+        BLE_ATT_ERR_ATTR_NOT_FOUND);
+
+    /*** Range too late. */
+    ble_att_svr_test_misc_register_group_attrs();
+    req.bagq_start_handle = 200;
+    req.bagq_end_handle = 300;
+
+    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc != 0);
+    ble_att_svr_test_misc_verify_tx_err_rsp(
+        BLE_ATT_OP_READ_GROUP_TYPE_REQ, 200,
+        BLE_ATT_ERR_ATTR_NOT_FOUND);
+
+    /*** One 16-bit UUID service. */
+    req.bagq_start_handle = 1;
+    req.bagq_end_handle = 5;
+
+    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_group_type_rsp(
+        ((struct ble_att_svr_test_group_type_entry[]) { {
+            .start_handle = 1,
+            .end_handle = 5,
+            .uuid16 = 0x1122,
+        }, {
+            .start_handle = 0,
+        } }));
+
+    /*** Two 16-bit UUID services. */
+    req.bagq_start_handle = 1;
+    req.bagq_end_handle = 10;
+
+    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_group_type_rsp(
+        ((struct ble_att_svr_test_group_type_entry[]) { {
+            .start_handle = 1,
+            .end_handle = 5,
+            .uuid16 = 0x1122,
+        }, {
+            .start_handle = 6,
+            .end_handle = 10,
+            .uuid16 = 0x2233,
+        }, {
+            .start_handle = 0,
+        } }));
+
+    /*** Two 16-bit UUID services; ensure 128-bit service not returned. */
+    req.bagq_start_handle = 1;
+    req.bagq_end_handle = 100;
+
+    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_group_type_rsp(
+        ((struct ble_att_svr_test_group_type_entry[]) { {
+            .start_handle = 1,
+            .end_handle = 5,
+            .uuid16 = 0x1122,
+        }, {
+            .start_handle = 6,
+            .end_handle = 10,
+            .uuid16 = 0x2233,
+        }, {
+            .start_handle = 0,
+        } }));
+
+    /*** One 128-bit service. */
+    req.bagq_start_handle = 11;
+    req.bagq_end_handle = 100;
+
+    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
+    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
+            BLE_ATT_UUID_PRIMARY_SERVICE);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+    ble_att_svr_test_misc_verify_tx_read_group_type_rsp(
+        ((struct ble_att_svr_test_group_type_entry[]) { {
+            .start_handle = 11,
+            .end_handle = 19,
+            .uuid128 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
+        }, {
+            .start_handle = 0,
+        } }));
+
+}
+
+TEST_CASE(ble_att_svr_test_prep_write)
+{
+    struct ble_hs_conn *conn;
+    uint16_t conn_handle;
+    int i;
+
+    static uint8_t data[1024];
+
+    conn_handle = ble_att_svr_test_misc_init(205);
+
+    /* Initialize some attribute data. */
+    for (i = 0; i < sizeof data; i++) {
+        data[i] = i;
+    }
+
+    /* Register two writable attributes. */
+    ble_att_svr_test_misc_register_uuid16(0x1234, HA_FLAG_PERM_RW, 1,
+                                          ble_att_svr_test_misc_attr_fn_w_1);
+    ble_att_svr_test_misc_register_uuid16(0x8989, HA_FLAG_PERM_RW, 2,
+                                          ble_att_svr_test_misc_attr_fn_w_2);
+
+    /* 3: not writable. */
+    ble_att_svr_test_misc_register_uuid16(0xabab, BLE_ATT_F_READ, 3,
+                                          ble_att_svr_test_misc_attr_fn_r_1);
+    /* 4: Encryption required. */
+    ble_att_svr_test_misc_register_uuid16(
+        0xabac, BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC, 4,
+        ble_att_svr_test_misc_attr_fn_w_1);
+
+    /* 5: Encryption+authentication required. */
+    ble_att_svr_test_misc_register_uuid16(
+        0xabad, BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC | BLE_ATT_F_WRITE_AUTHEN,
+        5, ble_att_svr_test_misc_attr_fn_w_1);
+
+    /* 6: Write callback always fails. */
+    ble_att_svr_test_misc_register_uuid16(
+        0xabae, BLE_ATT_F_WRITE, 6, ble_att_svr_test_misc_attr_fn_w_fail);
+
+    /*** Empty write succeeds. */
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     0, 0);
+
+    /*** Empty cancel succeeds. */
+    ble_att_svr_test_misc_exec_write(conn_handle, 0, 0, 0);
+
+    /*** Failure for prep write to nonexistent attribute. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 53525, 0, data, 10,
+                                     BLE_ATT_ERR_INVALID_HANDLE);
+
+    /*** Failure due to write-not-permitted. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 3, 0, data, 35,
+                                     BLE_ATT_ERR_WRITE_NOT_PERMITTED);
+
+    /*** Failure due to insufficient authentication (encryption required). */
+    ble_att_svr_test_misc_prep_write(conn_handle, 4, 0, data, 1,
+                                     BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
+
+    /*** Encrypt connection; ensure previous prep write now succeeds. */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(2);
+    TEST_ASSERT_FATAL(conn != NULL);
+    conn->bhc_sec_state.encrypted = 1;
+    ble_hs_unlock();
+
+    ble_att_svr_test_misc_prep_write(conn_handle, 4, 0, data, 1, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, 0, 0, 0);
+
+    /*** Failure due to insufficient authentication (not authenticated). */
+    ble_att_svr_test_misc_prep_write(conn_handle, 5, 0, data, 35,
+                                     BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
+
+    /*** Failure for write starting at nonzero offset. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 1, data, 10, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     BLE_ATT_ERR_INVALID_OFFSET, 1);
+    ble_att_svr_test_misc_verify_w_1(NULL, 0);
+
+    /*** Success for clear starting at nonzero offset. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 1, data, 10, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, 0, 0, 0);
+    ble_att_svr_test_misc_verify_w_1(NULL, 0);
+
+    /*** Failure for write with gap. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 10, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 11, data, 10, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     BLE_ATT_ERR_INVALID_OFFSET, 1);
+    ble_att_svr_test_misc_verify_w_1(NULL, 0);
+
+    /*** Success for clear with gap. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 10, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 11, data, 10, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, 0, 0, 0);
+    ble_att_svr_test_misc_verify_w_1(NULL, 0);
+
+    /*** Failure for overlong write. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 200, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 200, data + 200, 200, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 400, data + 400, 200, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN, 1);
+    ble_att_svr_test_misc_verify_w_1(NULL, 0);
+
+    /*** Successful two part write. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 20, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 20, data + 20, 20, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     0, 0);
+    ble_att_svr_test_misc_verify_w_1(data, 40);
+
+    /*** Successful three part write. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 35, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 35, data + 35, 43, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 78, data + 78, 1, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     0, 0);
+    ble_att_svr_test_misc_verify_w_1(data, 79);
+
+    /*** Successful two part write to two attributes. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 7, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 7, data + 7, 10, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 2, 0, data, 20, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 2, 20, data + 20, 10, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     0, 0);
+    ble_att_svr_test_misc_verify_w_1(data, 17);
+    ble_att_svr_test_misc_verify_w_2(data, 30);
+
+    /*** Fail write to second attribute; ensure first write doesn't occur. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 5, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 5, data + 5, 2, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 2, 0, data, 11, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 2, 12, data + 11, 19, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     BLE_ATT_ERR_INVALID_OFFSET, 2);
+    ble_att_svr_test_misc_verify_w_1(data, 17);
+    ble_att_svr_test_misc_verify_w_2(data, 30);
+
+    /*** Successful out of order write to two attributes. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 9, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 2, 0, data, 18, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 1, 9, data + 9, 3, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 2, 18, data + 18, 43, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     0, 0);
+    ble_att_svr_test_misc_verify_w_1(data, 12);
+    ble_att_svr_test_misc_verify_w_2(data, 61);
+
+    /*** Fail due to attribute callback error. */
+    ble_att_svr_test_misc_prep_write(conn_handle, 6, 0, data, 35, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 6, 35, data + 35, 43, 0);
+    ble_att_svr_test_misc_prep_write(conn_handle, 6, 78, data + 78, 1, 0);
+    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
+                                     BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN, 6);
+}
+
+TEST_CASE(ble_att_svr_test_notify)
+{
+    uint16_t conn_handle;
+
+    conn_handle = ble_att_svr_test_misc_init(0);
+
+    /*** Successful notifies; verify callback is executed. */
+    /* 3-length attribute. */
+    ble_att_svr_test_misc_verify_notify(conn_handle, 10,
+                                        (uint8_t[]) { 1, 2, 3 }, 3, 1);
+    /* 1-length attribute. */
+    ble_att_svr_test_misc_verify_notify(conn_handle, 1,
+                                        (uint8_t[]) { 0xff }, 1, 1);
+    /* 0-length attribute. */
+    ble_att_svr_test_misc_verify_notify(conn_handle, 43, NULL, 0, 1);
+
+    /*** Bad notifies; verify callback is not executed. */
+    /* Attribute handle of 0. */
+    ble_att_svr_test_misc_verify_notify(conn_handle, 0,
+                                        (uint8_t[]) { 1, 2, 3 }, 3, 0);
+
+}
+
+TEST_CASE(ble_att_svr_test_indicate)
+{
+    uint16_t conn_handle;
+
+    conn_handle = ble_att_svr_test_misc_init(0);
+
+    /*** Successful indicates; verify callback is executed. */
+    /* 3-length attribute. */
+    ble_att_svr_test_misc_verify_indicate(conn_handle, 10,
+                                          (uint8_t[]) { 1, 2, 3 }, 3, 1);
+    /* 1-length attribute. */
+    ble_att_svr_test_misc_verify_indicate(conn_handle, 1,
+                                          (uint8_t[]) { 0xff }, 1, 1);
+    /* 0-length attribute. */
+    ble_att_svr_test_misc_verify_indicate(conn_handle, 43, NULL, 0, 1);
+
+    /*** Bad indicates; verify callback is not executed. */
+    /* Attribute handle of 0. */
+    ble_att_svr_test_misc_verify_indicate(conn_handle, 0,
+                                          (uint8_t[]) { 1, 2, 3 }, 3, 0);
+
+}
+
+TEST_SUITE(ble_att_svr_suite)
+{
+    /* When checking for mbuf leaks, ensure no stale prep entries. */
+    static struct ble_hs_test_util_mbuf_params mbuf_params = {
+        .prev_tx = 1,
+        .rx_queue = 1,
+        .prep_list = 0,
+    };
+
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, &mbuf_params);
+
+    ble_att_svr_test_mtu();
+    ble_att_svr_test_read();
+    ble_att_svr_test_read_blob();
+    ble_att_svr_test_read_mult();
+    ble_att_svr_test_write();
+    ble_att_svr_test_find_info();
+    ble_att_svr_test_find_type_value();
+    ble_att_svr_test_read_type();
+    ble_att_svr_test_read_group_type();
+    ble_att_svr_test_prep_write();
+    ble_att_svr_test_notify();
+    ble_att_svr_test_indicate();
+}
+
+int
+ble_att_svr_test_all(void)
+{
+    ble_att_svr_suite();
+
+    return tu_any_failed;
+}


[19/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.c b/net/nimble/host/src/test/ble_hs_test_util.c
deleted file mode 100644
index 30926b6..0000000
--- a/net/nimble/host/src/test/ble_hs_test_util.c
+++ /dev/null
@@ -1,1438 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include "stats/stats.h"
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "nimble/hci_common.h"
-#include "nimble/ble_hci_trans.h"
-#include "host/ble_hs_adv.h"
-#include "host/ble_hs_id.h"
-#include "transport/ram/ble_hci_ram.h"
-#include "ble_hs_test_util.h"
-
-/* Our global device address. */
-uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
-
-#define BLE_HS_TEST_UTIL_PUB_ADDR_VAL { 0x0a, 0x54, 0xab, 0x49, 0x7f, 0x06 }
-
-static const uint8_t ble_hs_test_util_pub_addr[BLE_DEV_ADDR_LEN] =
-    BLE_HS_TEST_UTIL_PUB_ADDR_VAL;
-
-/** Use lots of small mbufs to ensure correct mbuf usage. */
-#define BLE_HS_TEST_UTIL_NUM_MBUFS      (100)
-#define BLE_HS_TEST_UTIL_BUF_SIZE       OS_ALIGN(100, 4)
-#define BLE_HS_TEST_UTIL_MEMBLOCK_SIZE  \
-    (BLE_HS_TEST_UTIL_BUF_SIZE + BLE_MBUF_MEMBLOCK_OVERHEAD)
-#define BLE_HS_TEST_UTIL_MEMPOOL_SIZE   \
-    OS_MEMPOOL_SIZE(BLE_HS_TEST_UTIL_NUM_MBUFS, BLE_HS_TEST_UTIL_MEMBLOCK_SIZE)
-
-#define BLE_HS_TEST_UTIL_LE_OPCODE(ocf) \
-    ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE, (ocf))
-
-struct os_eventq ble_hs_test_util_evq;
-
-os_membuf_t ble_hs_test_util_mbuf_mpool_data[BLE_HS_TEST_UTIL_MEMPOOL_SIZE];
-struct os_mbuf_pool ble_hs_test_util_mbuf_pool;
-struct os_mempool ble_hs_test_util_mbuf_mpool;
-
-static STAILQ_HEAD(, os_mbuf_pkthdr) ble_hs_test_util_prev_tx_queue;
-struct os_mbuf *ble_hs_test_util_prev_tx_cur;
-
-#define BLE_HS_TEST_UTIL_PREV_HCI_TX_CNT      64
-static uint8_t
-ble_hs_test_util_prev_hci_tx[BLE_HS_TEST_UTIL_PREV_HCI_TX_CNT][260];
-int ble_hs_test_util_num_prev_hci_txes;
-
-uint8_t ble_hs_test_util_cur_hci_tx[260];
-
-const struct ble_gap_adv_params ble_hs_test_util_adv_params = {
-    .conn_mode = BLE_GAP_CONN_MODE_UND,
-    .disc_mode = BLE_GAP_DISC_MODE_GEN,
-
-    .itvl_min = 0,
-    .itvl_max = 0,
-    .channel_map = 0,
-    .filter_policy = 0,
-    .high_duty_cycle = 0,
-};
-
-void
-ble_hs_test_util_prev_tx_enqueue(struct os_mbuf *om)
-{
-    struct os_mbuf_pkthdr *omp;
-
-    assert(OS_MBUF_IS_PKTHDR(om));
-
-    omp = OS_MBUF_PKTHDR(om);
-    if (STAILQ_EMPTY(&ble_hs_test_util_prev_tx_queue)) {
-        STAILQ_INSERT_HEAD(&ble_hs_test_util_prev_tx_queue, omp, omp_next);
-    } else {
-        STAILQ_INSERT_TAIL(&ble_hs_test_util_prev_tx_queue, omp, omp_next);
-    }
-}
-
-static struct os_mbuf *
-ble_hs_test_util_prev_tx_dequeue_once(struct hci_data_hdr *out_hci_hdr)
-{
-    struct os_mbuf_pkthdr *omp;
-    struct os_mbuf *om;
-    int rc;
-
-    omp = STAILQ_FIRST(&ble_hs_test_util_prev_tx_queue);
-    if (omp == NULL) {
-        return NULL;
-    }
-    STAILQ_REMOVE_HEAD(&ble_hs_test_util_prev_tx_queue, omp_next);
-
-    om = OS_MBUF_PKTHDR_TO_MBUF(omp);
-
-    rc = ble_hs_hci_util_data_hdr_strip(om, out_hci_hdr);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT_FATAL(out_hci_hdr->hdh_len == OS_MBUF_PKTLEN(om));
-
-    return om;
-}
-
-struct os_mbuf *
-ble_hs_test_util_prev_tx_dequeue(void)
-{
-    struct ble_l2cap_hdr l2cap_hdr;
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    uint8_t pb;
-    int rc;
-
-    os_mbuf_free_chain(ble_hs_test_util_prev_tx_cur);
-
-    om = ble_hs_test_util_prev_tx_dequeue_once(&hci_hdr);
-    if (om != NULL) {
-        pb = BLE_HCI_DATA_PB(hci_hdr.hdh_handle_pb_bc);
-        TEST_ASSERT_FATAL(pb == BLE_HCI_PB_FIRST_NON_FLUSH);
-
-        rc = ble_l2cap_parse_hdr(om, 0, &l2cap_hdr);
-        TEST_ASSERT_FATAL(rc == 0);
-
-        os_mbuf_adj(om, BLE_L2CAP_HDR_SZ);
-
-        ble_hs_test_util_prev_tx_cur = om;
-        while (OS_MBUF_PKTLEN(ble_hs_test_util_prev_tx_cur) <
-               l2cap_hdr.blh_len) {
-
-            om = ble_hs_test_util_prev_tx_dequeue_once(&hci_hdr);
-            TEST_ASSERT_FATAL(om != NULL);
-
-            pb = BLE_HCI_DATA_PB(hci_hdr.hdh_handle_pb_bc);
-            TEST_ASSERT_FATAL(pb == BLE_HCI_PB_MIDDLE);
-
-            os_mbuf_concat(ble_hs_test_util_prev_tx_cur, om);
-        }
-    } else {
-        ble_hs_test_util_prev_tx_cur = NULL;
-    }
-
-    return ble_hs_test_util_prev_tx_cur;
-}
-
-struct os_mbuf *
-ble_hs_test_util_prev_tx_dequeue_pullup(void)
-{
-    struct os_mbuf *om;
-
-    om = ble_hs_test_util_prev_tx_dequeue();
-    if (om != NULL) {
-        om = os_mbuf_pullup(om, OS_MBUF_PKTLEN(om));
-        TEST_ASSERT_FATAL(om != NULL);
-        ble_hs_test_util_prev_tx_cur = om;
-    }
-
-    return om;
-}
-
-int
-ble_hs_test_util_prev_tx_queue_sz(void)
-{
-    struct os_mbuf_pkthdr *omp;
-    int cnt;
-
-    cnt = 0;
-    STAILQ_FOREACH(omp, &ble_hs_test_util_prev_tx_queue, omp_next) {
-        cnt++;
-    }
-
-    return cnt;
-}
-
-void
-ble_hs_test_util_prev_tx_queue_clear(void)
-{
-    ble_hs_test_util_tx_all();
-    while (!STAILQ_EMPTY(&ble_hs_test_util_prev_tx_queue)) {
-        ble_hs_test_util_prev_tx_dequeue();
-    }
-}
-
-void *
-ble_hs_test_util_get_first_hci_tx(void)
-{
-    if (ble_hs_test_util_num_prev_hci_txes == 0) {
-        return NULL;
-    }
-
-    memcpy(ble_hs_test_util_cur_hci_tx, ble_hs_test_util_prev_hci_tx[0],
-           sizeof ble_hs_test_util_cur_hci_tx);
-
-    ble_hs_test_util_num_prev_hci_txes--;
-    if (ble_hs_test_util_num_prev_hci_txes > 0) {
-        memmove(
-            ble_hs_test_util_prev_hci_tx, ble_hs_test_util_prev_hci_tx + 1,
-            sizeof ble_hs_test_util_prev_hci_tx[0] *
-            ble_hs_test_util_num_prev_hci_txes);
-    }
-
-    return ble_hs_test_util_cur_hci_tx;
-}
-
-void *
-ble_hs_test_util_get_last_hci_tx(void)
-{
-    if (ble_hs_test_util_num_prev_hci_txes == 0) {
-        return NULL;
-    }
-
-    ble_hs_test_util_num_prev_hci_txes--;
-    memcpy(ble_hs_test_util_cur_hci_tx,
-           ble_hs_test_util_prev_hci_tx + ble_hs_test_util_num_prev_hci_txes,
-           sizeof ble_hs_test_util_cur_hci_tx);
-
-    return ble_hs_test_util_cur_hci_tx;
-}
-
-void
-ble_hs_test_util_enqueue_hci_tx(void *cmd)
-{
-    TEST_ASSERT_FATAL(ble_hs_test_util_num_prev_hci_txes <
-                      BLE_HS_TEST_UTIL_PREV_HCI_TX_CNT);
-    memcpy(ble_hs_test_util_prev_hci_tx + ble_hs_test_util_num_prev_hci_txes,
-           cmd, 260);
-
-    ble_hs_test_util_num_prev_hci_txes++;
-}
-
-void
-ble_hs_test_util_prev_hci_tx_clear(void)
-{
-    ble_hs_test_util_num_prev_hci_txes = 0;
-}
-
-static void
-ble_hs_test_util_rx_hci_evt(uint8_t *evt)
-{
-    uint8_t *evbuf;
-    int totlen;
-    int rc;
-
-    totlen = BLE_HCI_EVENT_HDR_LEN + evt[1];
-    TEST_ASSERT_FATAL(totlen <= UINT8_MAX + BLE_HCI_EVENT_HDR_LEN);
-
-    if (os_started()) {
-        evbuf = ble_hci_trans_buf_alloc(
-            BLE_HCI_TRANS_BUF_EVT_LO);
-        TEST_ASSERT_FATAL(evbuf != NULL);
-
-        memcpy(evbuf, evt, totlen);
-        rc = ble_hci_trans_ll_evt_tx(evbuf);
-    } else {
-        rc = ble_hs_hci_evt_process(evt);
-    }
-
-    TEST_ASSERT_FATAL(rc == 0);
-}
-
-void
-ble_hs_test_util_build_cmd_complete(uint8_t *dst, int len,
-                                    uint8_t param_len, uint8_t num_pkts,
-                                    uint16_t opcode)
-{
-    TEST_ASSERT(len >= BLE_HCI_EVENT_CMD_COMPLETE_HDR_LEN);
-
-    dst[0] = BLE_HCI_EVCODE_COMMAND_COMPLETE;
-    dst[1] = 3 + param_len;
-    dst[2] = num_pkts;
-    htole16(dst + 3, opcode);
-}
-
-void
-ble_hs_test_util_build_cmd_status(uint8_t *dst, int len,
-                                  uint8_t status, uint8_t num_pkts,
-                                  uint16_t opcode)
-{
-    TEST_ASSERT(len >= BLE_HCI_EVENT_CMD_STATUS_LEN);
-
-    dst[0] = BLE_HCI_EVCODE_COMMAND_STATUS;
-    dst[1] = BLE_HCI_EVENT_CMD_STATUS_LEN;
-    dst[2] = status;
-    dst[3] = num_pkts;
-    htole16(dst + 4, opcode);
-}
-
-#define BLE_HS_TEST_UTIL_PHONY_ACK_MAX  64
-struct ble_hs_test_util_phony_ack {
-    uint16_t opcode;
-    uint8_t status;
-    uint8_t evt_params[256];
-    uint8_t evt_params_len;
-};
-
-static struct ble_hs_test_util_phony_ack
-ble_hs_test_util_phony_acks[BLE_HS_TEST_UTIL_PHONY_ACK_MAX];
-static int ble_hs_test_util_num_phony_acks;
-
-static int
-ble_hs_test_util_phony_ack_cb(uint8_t *ack, int ack_buf_len)
-{
-    struct ble_hs_test_util_phony_ack *entry;
-
-    if (ble_hs_test_util_num_phony_acks == 0) {
-        return BLE_HS_ETIMEOUT_HCI;
-    }
-
-    entry = ble_hs_test_util_phony_acks;
-
-    ble_hs_test_util_build_cmd_complete(ack, 256,
-                                        entry->evt_params_len + 1, 1,
-                                        entry->opcode);
-    ack[BLE_HCI_EVENT_CMD_COMPLETE_HDR_LEN] = entry->status;
-    memcpy(ack + BLE_HCI_EVENT_CMD_COMPLETE_HDR_LEN + 1, entry->evt_params,
-           entry->evt_params_len);
-
-    ble_hs_test_util_num_phony_acks--;
-    if (ble_hs_test_util_num_phony_acks > 0) {
-        memmove(ble_hs_test_util_phony_acks, ble_hs_test_util_phony_acks + 1,
-                sizeof *entry * ble_hs_test_util_num_phony_acks);
-    }
-
-    return 0;
-}
-
-void
-ble_hs_test_util_set_ack_params(uint16_t opcode, uint8_t status, void *params,
-                                uint8_t params_len)
-{
-    struct ble_hs_test_util_phony_ack *ack;
-
-    ack = ble_hs_test_util_phony_acks + 0;
-    ack->opcode = opcode;
-    ack->status = status;
-
-    if (params == NULL || params_len == 0) {
-        ack->evt_params_len = 0;
-    } else {
-        memcpy(ack->evt_params, params, params_len);
-        ack->evt_params_len = params_len;
-    }
-    ble_hs_test_util_num_phony_acks = 1;
-
-    ble_hs_hci_set_phony_ack_cb(ble_hs_test_util_phony_ack_cb);
-}
-
-void
-ble_hs_test_util_set_ack(uint16_t opcode, uint8_t status)
-{
-    ble_hs_test_util_set_ack_params(opcode, status, NULL, 0);
-}
-
-static void
-ble_hs_test_util_set_ack_seq(struct ble_hs_test_util_phony_ack *acks)
-{
-    int i;
-
-    for (i = 0; acks[i].opcode != 0; i++) {
-        ble_hs_test_util_phony_acks[i] = acks[i];
-    }
-    ble_hs_test_util_num_phony_acks = i;
-
-    ble_hs_hci_set_phony_ack_cb(ble_hs_test_util_phony_ack_cb);
-}
-
-void
-ble_hs_test_util_create_rpa_conn(uint16_t handle, uint8_t own_addr_type,
-                                 const uint8_t *our_rpa,
-                                 uint8_t peer_addr_type,
-                                 const uint8_t *peer_id_addr,
-                                 const uint8_t *peer_rpa,
-                                 ble_gap_event_fn *cb, void *cb_arg)
-{
-    struct hci_le_conn_complete evt;
-    int rc;
-
-    ble_hs_test_util_connect(own_addr_type, peer_addr_type,
-                             peer_id_addr, 0, NULL, cb, cb_arg, 0);
-
-    memset(&evt, 0, sizeof evt);
-    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    evt.status = BLE_ERR_SUCCESS;
-    evt.connection_handle = handle;
-    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
-    evt.peer_addr_type = peer_addr_type;
-    memcpy(evt.peer_addr, peer_id_addr, 6);
-    evt.conn_itvl = BLE_GAP_INITIAL_CONN_ITVL_MAX;
-    evt.conn_latency = BLE_GAP_INITIAL_CONN_LATENCY;
-    evt.supervision_timeout = BLE_GAP_INITIAL_SUPERVISION_TIMEOUT;
-    memcpy(evt.local_rpa, our_rpa, 6);
-    memcpy(evt.peer_rpa, peer_rpa, 6);
-
-    rc = ble_gap_rx_conn_complete(&evt);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_test_util_prev_hci_tx_clear();
-}
-
-void
-ble_hs_test_util_create_conn(uint16_t handle, uint8_t *peer_id_addr,
-                             ble_gap_event_fn *cb, void *cb_arg)
-{
-    static uint8_t null_addr[6];
-
-    ble_hs_test_util_create_rpa_conn(handle, BLE_ADDR_TYPE_PUBLIC, null_addr,
-                                     BLE_ADDR_TYPE_PUBLIC, peer_id_addr,
-                                     null_addr, cb, cb_arg);
-}
-
-static void
-ble_hs_test_util_conn_params_dflt(struct ble_gap_conn_params *conn_params)
-{
-    conn_params->scan_itvl = 0x0010;
-    conn_params->scan_window = 0x0010;
-    conn_params->itvl_min = BLE_GAP_INITIAL_CONN_ITVL_MIN;
-    conn_params->itvl_max = BLE_GAP_INITIAL_CONN_ITVL_MAX;
-    conn_params->latency = BLE_GAP_INITIAL_CONN_LATENCY;
-    conn_params->supervision_timeout = BLE_GAP_INITIAL_SUPERVISION_TIMEOUT;
-    conn_params->min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
-    conn_params->max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
-}
-
-static void
-ble_hs_test_util_hcc_from_conn_params(
-    struct hci_create_conn *hcc, uint8_t own_addr_type, uint8_t peer_addr_type,
-    const uint8_t *peer_addr, const struct ble_gap_conn_params *conn_params)
-{
-    hcc->scan_itvl = conn_params->scan_itvl;
-    hcc->scan_window = conn_params->scan_window;
-
-    if (peer_addr_type == BLE_GAP_ADDR_TYPE_WL) {
-        hcc->filter_policy = BLE_HCI_CONN_FILT_USE_WL;
-        hcc->peer_addr_type = 0;
-        memset(hcc->peer_addr, 0, 6);
-    } else {
-        hcc->filter_policy = BLE_HCI_CONN_FILT_NO_WL;
-        hcc->peer_addr_type = peer_addr_type;
-        memcpy(hcc->peer_addr, peer_addr, 6);
-    }
-    hcc->own_addr_type = own_addr_type;
-    hcc->conn_itvl_min = conn_params->itvl_min;
-    hcc->conn_itvl_max = conn_params->itvl_max;
-    hcc->conn_latency = conn_params->latency;
-    hcc->supervision_timeout = conn_params->supervision_timeout;
-    hcc->min_ce_len = conn_params->min_ce_len;
-    hcc->max_ce_len = conn_params->max_ce_len;
-}
-
-void
-ble_hs_test_util_verify_tx_create_conn(const struct hci_create_conn *exp)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_CREATE_CONN,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_CREATE_CONN_LEN);
-
-    TEST_ASSERT(le16toh(param + 0) == exp->scan_itvl);
-    TEST_ASSERT(le16toh(param + 2) == exp->scan_window);
-    TEST_ASSERT(param[4] == exp->filter_policy);
-    TEST_ASSERT(param[5] == exp->peer_addr_type);
-    TEST_ASSERT(memcmp(param + 6, exp->peer_addr, 6) == 0);
-    TEST_ASSERT(param[12] == exp->own_addr_type);
-    TEST_ASSERT(le16toh(param + 13) == exp->conn_itvl_min);
-    TEST_ASSERT(le16toh(param + 15) == exp->conn_itvl_max);
-    TEST_ASSERT(le16toh(param + 17) == exp->conn_latency);
-    TEST_ASSERT(le16toh(param + 19) == exp->supervision_timeout);
-    TEST_ASSERT(le16toh(param + 21) == exp->min_ce_len);
-    TEST_ASSERT(le16toh(param + 23) == exp->max_ce_len);
-}
-
-int
-ble_hs_test_util_connect(uint8_t own_addr_type, uint8_t peer_addr_type,
-                         const uint8_t *peer_addr, int32_t duration_ms,
-                         const struct ble_gap_conn_params *params,
-                         ble_gap_event_fn *cb, void *cb_arg,
-                         uint8_t ack_status)
-{
-    struct ble_gap_conn_params dflt_params;
-    struct hci_create_conn hcc;
-    int rc;
-
-    /* This function ensures the most recently sent HCI command is the expected
-     * create connection command.  If the current test case has unverified HCI
-     * commands, assume we are not interested in them and clear the queue.
-     */
-    ble_hs_test_util_prev_hci_tx_clear();
-
-    ble_hs_test_util_set_ack(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_CREATE_CONN),
-        ack_status);
-
-    rc = ble_gap_connect(own_addr_type, peer_addr_type, peer_addr, duration_ms,
-                         params, cb, cb_arg);
-
-    TEST_ASSERT(rc == BLE_HS_HCI_ERR(ack_status));
-
-    if (params == NULL) {
-        ble_hs_test_util_conn_params_dflt(&dflt_params);
-        params = &dflt_params;
-    }
-
-    ble_hs_test_util_hcc_from_conn_params(&hcc, own_addr_type,
-                                          peer_addr_type, peer_addr, params);
-    ble_hs_test_util_verify_tx_create_conn(&hcc);
-
-    return rc;
-}
-
-int
-ble_hs_test_util_conn_cancel(uint8_t ack_status)
-{
-    int rc;
-
-    ble_hs_test_util_set_ack(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_CREATE_CONN_CANCEL),
-        ack_status);
-
-    rc = ble_gap_conn_cancel();
-    return rc;
-}
-
-void
-ble_hs_test_util_conn_cancel_full(void)
-{
-    struct hci_le_conn_complete evt;
-    int rc;
-
-    ble_hs_test_util_conn_cancel(0);
-
-    memset(&evt, 0, sizeof evt);
-    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    evt.status = BLE_ERR_UNK_CONN_ID;
-    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
-
-    rc = ble_gap_rx_conn_complete(&evt);
-    TEST_ASSERT_FATAL(rc == 0);
-}
-
-int
-ble_hs_test_util_conn_terminate(uint16_t conn_handle, uint8_t hci_status)
-{
-    int rc;
-
-    ble_hs_test_util_set_ack(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LINK_CTRL,
-                                    BLE_HCI_OCF_DISCONNECT_CMD),
-        hci_status);
-
-    rc = ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
-    return rc;
-}
-
-void
-ble_hs_test_util_conn_disconnect(uint16_t conn_handle)
-{
-    struct hci_disconn_complete evt;
-    int rc;
-
-    rc = ble_hs_test_util_conn_terminate(conn_handle, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Receive disconnection complete event. */
-    evt.connection_handle = conn_handle;
-    evt.status = 0;
-    evt.reason = BLE_ERR_CONN_TERM_LOCAL;
-    ble_gap_rx_disconn_complete(&evt);
-}
-
-int
-ble_hs_test_util_exp_hci_status(int cmd_idx, int fail_idx, uint8_t fail_status)
-{
-    if (cmd_idx == fail_idx) {
-        return BLE_HS_HCI_ERR(fail_status);
-    } else {
-        return 0;
-    }
-}
-
-int
-ble_hs_test_util_disc(uint8_t own_addr_type, int32_t duration_ms,
-                      const struct ble_gap_disc_params *disc_params,
-                      ble_gap_event_fn *cb, void *cb_arg, int fail_idx,
-                      uint8_t fail_status)
-{
-    int rc;
-
-    ble_hs_test_util_set_ack_seq(((struct ble_hs_test_util_phony_ack[]) {
-        {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_SCAN_PARAMS),
-            ble_hs_test_util_exp_hci_status(0, fail_idx, fail_status),
-        },
-        {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
-            ble_hs_test_util_exp_hci_status(1, fail_idx, fail_status),
-        },
-
-        { 0 }
-    }));
-
-    rc = ble_gap_disc(own_addr_type, duration_ms, disc_params,
-                      cb, cb_arg);
-    return rc;
-}
-
-int
-ble_hs_test_util_disc_cancel(uint8_t ack_status)
-{
-    int rc;
-
-    ble_hs_test_util_set_ack(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
-        ack_status);
-
-    rc = ble_gap_disc_cancel();
-    return rc;
-}
-
-static void
-ble_hs_test_util_verify_tx_rd_pwr(void)
-{
-    uint8_t param_len;
-
-    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                   BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR,
-                                   &param_len);
-    TEST_ASSERT(param_len == 0);
-}
-
-int
-ble_hs_test_util_adv_set_fields(struct ble_hs_adv_fields *adv_fields,
-                                uint8_t hci_status)
-{
-    int auto_pwr;
-    int rc;
-
-    auto_pwr = adv_fields->tx_pwr_lvl_is_present &&
-               adv_fields->tx_pwr_lvl == BLE_HS_ADV_TX_PWR_LVL_AUTO;
-
-    if (auto_pwr) {
-        ble_hs_test_util_set_ack_params(
-            ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                        BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR),
-            hci_status,
-            ((uint8_t[1]){0}), 1);
-    }
-
-    rc = ble_gap_adv_set_fields(adv_fields);
-    if (rc == 0 && auto_pwr) {
-        /* Verify tx of set advertising params command. */
-        ble_hs_test_util_verify_tx_rd_pwr();
-    }
-
-    return rc;
-}
-
-int
-ble_hs_test_util_adv_start(uint8_t own_addr_type,
-                           uint8_t peer_addr_type, const uint8_t *peer_addr, 
-                           const struct ble_gap_adv_params *adv_params,
-                           ble_gap_event_fn *cb, void *cb_arg,
-                           int fail_idx, uint8_t fail_status)
-{
-    struct ble_hs_test_util_phony_ack acks[6];
-    int rc;
-    int i;
-
-    i = 0;
-
-    acks[i] = (struct ble_hs_test_util_phony_ack) {
-        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_PARAMS),
-        fail_idx == i ? fail_status : 0,
-    };
-    i++;
-
-    if (adv_params->conn_mode != BLE_GAP_CONN_MODE_DIR) {
-        acks[i] = (struct ble_hs_test_util_phony_ack) {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_DATA),
-            ble_hs_test_util_exp_hci_status(i, fail_idx, fail_status),
-        };
-        i++;
-
-        acks[i] = (struct ble_hs_test_util_phony_ack) {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA),
-            ble_hs_test_util_exp_hci_status(i, fail_idx, fail_status),
-        };
-        i++;
-    }
-
-    acks[i] = (struct ble_hs_test_util_phony_ack) {
-        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_ENABLE),
-        ble_hs_test_util_exp_hci_status(i, fail_idx, fail_status),
-    };
-    i++;
-
-    memset(acks + i, 0, sizeof acks[i]);
-
-    ble_hs_test_util_set_ack_seq(acks);
-    
-    rc = ble_gap_adv_start(own_addr_type, peer_addr_type, peer_addr, 
-                           BLE_HS_FOREVER, adv_params, cb, cb_arg);
-
-    return rc;
-}
-
-int
-ble_hs_test_util_adv_stop(uint8_t hci_status)
-{
-    int rc;
-
-    ble_hs_test_util_set_ack(
-        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_ENABLE),
-        hci_status);
-
-    rc = ble_gap_adv_stop();
-    return rc;
-}
-
-int
-ble_hs_test_util_wl_set(struct ble_gap_white_entry *white_list,
-                        uint8_t white_list_count,
-                        int fail_idx, uint8_t fail_status)
-{
-    struct ble_hs_test_util_phony_ack acks[64];
-    int cmd_idx;
-    int rc;
-    int i;
-
-    TEST_ASSERT_FATAL(white_list_count < 63);
-
-    cmd_idx = 0;
-    acks[cmd_idx] = (struct ble_hs_test_util_phony_ack) {
-        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_CLEAR_WHITE_LIST),
-        ble_hs_test_util_exp_hci_status(cmd_idx, fail_idx, fail_status),
-    };
-    cmd_idx++;
-
-    for (i = 0; i < white_list_count; i++) {
-        acks[cmd_idx] = (struct ble_hs_test_util_phony_ack) {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_ADD_WHITE_LIST),
-            ble_hs_test_util_exp_hci_status(cmd_idx, fail_idx, fail_status),
-        };
-
-        cmd_idx++;
-    }
-    memset(acks + cmd_idx, 0, sizeof acks[cmd_idx]);
-
-    ble_hs_test_util_set_ack_seq(acks);
-    rc = ble_gap_wl_set(white_list, white_list_count);
-    return rc;
-}
-
-int
-ble_hs_test_util_conn_update(uint16_t conn_handle,
-                             struct ble_gap_upd_params *params,
-                             uint8_t hci_status)
-{
-    int rc;
-
-    ble_hs_test_util_set_ack(
-        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_CONN_UPDATE), hci_status);
-
-    rc = ble_gap_update_params(conn_handle, params);
-    return rc;
-}
-
-int
-ble_hs_test_util_set_our_irk(const uint8_t *irk, int fail_idx,
-                             uint8_t hci_status)
-{
-    int rc;
-
-    ble_hs_test_util_set_ack_seq(((struct ble_hs_test_util_phony_ack[]) {
-        {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
-            ble_hs_test_util_exp_hci_status(0, fail_idx, hci_status),
-        },
-        {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_CLR_RESOLV_LIST),
-            ble_hs_test_util_exp_hci_status(1, fail_idx, hci_status),
-        },
-        {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
-            ble_hs_test_util_exp_hci_status(2, fail_idx, hci_status),
-        },
-        {
-            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_ADD_RESOLV_LIST),
-            ble_hs_test_util_exp_hci_status(3, fail_idx, hci_status),
-        },
-        {
-            0
-        }
-    }));
-
-    rc = ble_hs_pvcy_set_our_irk(irk);
-    return rc;
-}
-
-int
-ble_hs_test_util_security_initiate(uint16_t conn_handle, uint8_t hci_status)
-{
-    int rc;
-
-    ble_hs_test_util_set_ack(
-        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_START_ENCRYPT), hci_status);
-
-    rc = ble_gap_security_initiate(conn_handle);
-    return rc;
-}
-
-int
-ble_hs_test_util_l2cap_rx_first_frag(uint16_t conn_handle, uint16_t cid,
-                                     struct hci_data_hdr *hci_hdr,
-                                     struct os_mbuf *om)
-{
-    int rc;
-
-    om = ble_l2cap_prepend_hdr(om, cid, OS_MBUF_PKTLEN(om));
-    TEST_ASSERT_FATAL(om != NULL);
-
-    rc = ble_hs_test_util_l2cap_rx(conn_handle, hci_hdr, om);
-    return rc;
-}
-
-int
-ble_hs_test_util_l2cap_rx(uint16_t conn_handle,
-                          struct hci_data_hdr *hci_hdr,
-                          struct os_mbuf *om)
-{
-    struct ble_hs_conn *conn;
-    ble_l2cap_rx_fn *rx_cb;
-    struct os_mbuf *rx_buf;
-    int rc;
-
-    ble_hs_lock();
-
-    conn = ble_hs_conn_find(conn_handle);
-    if (conn != NULL) {
-        rc = ble_l2cap_rx(conn, hci_hdr, om, &rx_cb, &rx_buf);
-    } else {
-        os_mbuf_free_chain(om);
-    }
-
-    ble_hs_unlock();
-
-    if (conn == NULL) {
-        rc = BLE_HS_ENOTCONN;
-    } else if (rc == 0) {
-        TEST_ASSERT_FATAL(rx_cb != NULL);
-        TEST_ASSERT_FATAL(rx_buf != NULL);
-        rc = rx_cb(conn_handle, &rx_buf);
-        os_mbuf_free_chain(rx_buf);
-    } else if (rc == BLE_HS_EAGAIN) {
-        /* More fragments on the way. */
-        rc = 0;
-    }
-
-    return rc;
-}
-
-int
-ble_hs_test_util_l2cap_rx_payload_flat(uint16_t conn_handle, uint16_t cid,
-                                       const void *data, int len)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    int rc;
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    rc = os_mbuf_append(om, data, len);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    hci_hdr.hdh_handle_pb_bc =
-        ble_hs_hci_util_handle_pb_bc_join(conn_handle,
-                                          BLE_HCI_PB_FIRST_FLUSH, 0);
-    hci_hdr.hdh_len = OS_MBUF_PKTHDR(om)->omp_len;
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, cid, &hci_hdr, om);
-    return rc;
-}
-
-void
-ble_hs_test_util_rx_att_err_rsp(uint16_t conn_handle, uint8_t req_op,
-                                uint8_t error_code, uint16_t err_handle)
-{
-    struct ble_att_error_rsp rsp;
-    uint8_t buf[BLE_ATT_ERROR_RSP_SZ];
-    int rc;
-
-    rsp.baep_req_op = req_op;
-    rsp.baep_handle = err_handle;
-    rsp.baep_error_code = error_code;
-
-    ble_att_error_rsp_write(buf, sizeof buf, &rsp);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-}
-
-void
-ble_hs_test_util_set_startup_acks(void)
-{
-    /* Receive acknowledgements for the startup sequence.  We sent the
-     * corresponding requests when the host task was started.
-     */
-    ble_hs_test_util_set_ack_seq(((struct ble_hs_test_util_phony_ack[]) {
-        {
-            .opcode = ble_hs_hci_util_opcode_join(BLE_HCI_OGF_CTLR_BASEBAND,
-                                                  BLE_HCI_OCF_CB_RESET),
-        },
-        {
-            .opcode = ble_hs_hci_util_opcode_join(
-                BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_SET_EVENT_MASK),
-        },
-        {
-            .opcode = ble_hs_hci_util_opcode_join(
-                BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_SET_EVENT_MASK2),
-        },
-        {
-            .opcode = ble_hs_hci_util_opcode_join(
-                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_EVENT_MASK),
-        },
-        {
-            .opcode = ble_hs_hci_util_opcode_join(
-                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_BUF_SIZE),
-            /* Use a very low buffer size (16) to test fragmentation. */
-            .evt_params = { 0x10, 0x00, 0x20 },
-            .evt_params_len = 3,
-        },
-        {
-            .opcode = ble_hs_hci_util_opcode_join(
-                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_LOC_SUPP_FEAT),
-            .evt_params = { 0 },
-            .evt_params_len = 8,
-        },
-        {
-            .opcode = ble_hs_hci_util_opcode_join(
-                BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_BD_ADDR),
-            .evt_params = BLE_HS_TEST_UTIL_PUB_ADDR_VAL,
-            .evt_params_len = 6,
-        },
-        {
-            .opcode = ble_hs_hci_util_opcode_join(
-                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
-        },
-        {
-            .opcode = ble_hs_hci_util_opcode_join(
-                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CLR_RESOLV_LIST),
-        },
-        {
-            .opcode = ble_hs_hci_util_opcode_join(
-                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
-        },
-        {
-            .opcode = ble_hs_hci_util_opcode_join(
-                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ADD_RESOLV_LIST),
-        },
-        { 0 }
-    }));
-}
-
-void
-ble_hs_test_util_rx_num_completed_pkts_event(
-    struct ble_hs_test_util_num_completed_pkts_entry *entries)
-{
-    struct ble_hs_test_util_num_completed_pkts_entry *entry;
-    uint8_t buf[1024];
-    int num_entries;
-    int off;
-    int i;
-
-    /* Count number of entries. */
-    num_entries = 0;
-    for (entry = entries; entry->handle_id != 0; entry++) {
-        num_entries++;
-    }
-    TEST_ASSERT_FATAL(num_entries <= UINT8_MAX);
-
-    buf[0] = BLE_HCI_EVCODE_NUM_COMP_PKTS;
-    buf[2] = num_entries;
-
-    off = 3;
-    for (i = 0; i < num_entries; i++) {
-        htole16(buf + off, entries[i].handle_id);
-        off += 2;
-    }
-    for (i = 0; i < num_entries; i++) {
-        htole16(buf + off, entries[i].num_pkts);
-        off += 2;
-    }
-
-    buf[1] = off - 2;
-
-    ble_hs_test_util_rx_hci_evt(buf);
-}
-
-void
-ble_hs_test_util_rx_disconn_complete_event(struct hci_disconn_complete *evt)
-{
-    uint8_t buf[BLE_HCI_EVENT_HDR_LEN + BLE_HCI_EVENT_DISCONN_COMPLETE_LEN];
-
-    buf[0] = BLE_HCI_EVCODE_DISCONN_CMP;
-    buf[1] = BLE_HCI_EVENT_DISCONN_COMPLETE_LEN;
-    buf[2] = evt->status;
-    htole16(buf + 3, evt->connection_handle);
-    buf[5] = evt->reason;
-
-    ble_hs_test_util_rx_hci_evt(buf);
-}
-
-uint8_t *
-ble_hs_test_util_verify_tx_hci(uint8_t ogf, uint16_t ocf,
-                               uint8_t *out_param_len)
-{
-    uint16_t opcode;
-    uint8_t *cmd;
-
-    cmd = ble_hs_test_util_get_first_hci_tx();
-    TEST_ASSERT_FATAL(cmd != NULL);
-
-    opcode = le16toh(cmd);
-    TEST_ASSERT(BLE_HCI_OGF(opcode) == ogf);
-    TEST_ASSERT(BLE_HCI_OCF(opcode) == ocf);
-
-    if (out_param_len != NULL) {
-        *out_param_len = cmd[2];
-    }
-
-    return cmd + 3;
-}
-
-void
-ble_hs_test_util_tx_all(void)
-{
-    ble_hs_process_tx_data_queue();
-}
-
-void
-ble_hs_test_util_verify_tx_prep_write(uint16_t attr_handle, uint16_t offset,
-                                      const void *data, int data_len)
-{
-    struct ble_att_prep_write_cmd req;
-    struct os_mbuf *om;
-
-    ble_hs_test_util_tx_all();
-    om = ble_hs_test_util_prev_tx_dequeue();
-    TEST_ASSERT_FATAL(om != NULL);
-    TEST_ASSERT(OS_MBUF_PKTLEN(om) ==
-                BLE_ATT_PREP_WRITE_CMD_BASE_SZ + data_len);
-
-    om = os_mbuf_pullup(om, BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
-    TEST_ASSERT_FATAL(om != NULL);
-
-    ble_att_prep_write_req_parse(om->om_data, om->om_len, &req);
-    TEST_ASSERT(req.bapc_handle == attr_handle);
-    TEST_ASSERT(req.bapc_offset == offset);
-    TEST_ASSERT(os_mbuf_cmpf(om, BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
-                             data, data_len) == 0);
-}
-
-void
-ble_hs_test_util_verify_tx_exec_write(uint8_t expected_flags)
-{
-    struct ble_att_exec_write_req req;
-    struct os_mbuf *om;
-
-    ble_hs_test_util_tx_all();
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    TEST_ASSERT_FATAL(om != NULL);
-    TEST_ASSERT(om->om_len == BLE_ATT_EXEC_WRITE_REQ_SZ);
-
-    ble_att_exec_write_req_parse(om->om_data, om->om_len, &req);
-    TEST_ASSERT(req.baeq_flags == expected_flags);
-}
-
-void
-ble_hs_test_util_verify_tx_read_rsp_gen(uint8_t att_op,
-                                        uint8_t *attr_data, int attr_len)
-{
-    struct os_mbuf *om;
-    uint8_t u8;
-    int rc;
-    int i;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue();
-
-    rc = os_mbuf_copydata(om, 0, 1, &u8);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(u8 == att_op);
-
-    for (i = 0; i < attr_len; i++) {
-        rc = os_mbuf_copydata(om, i + 1, 1, &u8);
-        TEST_ASSERT(rc == 0);
-        TEST_ASSERT(u8 == attr_data[i]);
-    }
-
-    rc = os_mbuf_copydata(om, i + 1, 1, &u8);
-    TEST_ASSERT(rc != 0);
-}
-
-void
-ble_hs_test_util_verify_tx_read_rsp(uint8_t *attr_data, int attr_len)
-{
-    ble_hs_test_util_verify_tx_read_rsp_gen(BLE_ATT_OP_READ_RSP,
-                                            attr_data, attr_len);
-}
-
-void
-ble_hs_test_util_verify_tx_read_blob_rsp(uint8_t *attr_data, int attr_len)
-{
-    ble_hs_test_util_verify_tx_read_rsp_gen(BLE_ATT_OP_READ_BLOB_RSP,
-                                            attr_data, attr_len);
-}
-
-void
-ble_hs_test_util_set_static_rnd_addr(void)
-{
-    uint8_t addr[6] = { 1, 2, 3, 4, 5, 0xc1 };
-    int rc;
-
-    ble_hs_test_util_set_ack(
-        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_RAND_ADDR), 0);
-
-    rc = ble_hs_id_set_rnd(addr);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_hs_test_util_get_first_hci_tx();
-}
-
-struct os_mbuf *
-ble_hs_test_util_om_from_flat(const void *buf, uint16_t len)
-{
-    struct os_mbuf *om;
-
-    om = ble_hs_mbuf_from_flat(buf, len);
-    TEST_ASSERT_FATAL(om != NULL);
-
-    return om;
-}
-
-int
-ble_hs_test_util_flat_attr_cmp(const struct ble_hs_test_util_flat_attr *a,
-                               const struct ble_hs_test_util_flat_attr *b)
-{
-    if (a->handle != b->handle) {
-        return -1;
-    }
-    if (a->offset != b->offset) {
-        return -1;
-    }
-    if (a->value_len != b->value_len) {
-        return -1;
-    }
-    return memcmp(a->value, b->value, a->value_len);
-}
-
-void
-ble_hs_test_util_attr_to_flat(struct ble_hs_test_util_flat_attr *flat,
-                              const struct ble_gatt_attr *attr)
-{
-    int rc;
-
-    flat->handle = attr->handle;
-    flat->offset = attr->offset;
-    rc = ble_hs_mbuf_to_flat(attr->om, flat->value, sizeof flat->value,
-                           &flat->value_len);
-    TEST_ASSERT_FATAL(rc == 0);
-}
-
-void
-ble_hs_test_util_attr_from_flat(struct ble_gatt_attr *attr,
-                                const struct ble_hs_test_util_flat_attr *flat)
-{
-    attr->handle = flat->handle;
-    attr->offset = flat->offset;
-    attr->om = ble_hs_test_util_om_from_flat(flat->value, flat->value_len);
-}
-
-int
-ble_hs_test_util_read_local_flat(uint16_t attr_handle, uint16_t max_len,
-                                 void *buf, uint16_t *out_len)
-{
-    struct os_mbuf *om;
-    int rc;
-
-    rc = ble_att_svr_read_local(attr_handle, &om);
-    if (rc != 0) {
-        return rc;
-    }
-
-    TEST_ASSERT_FATAL(OS_MBUF_PKTLEN(om) <= max_len);
-
-    rc = os_mbuf_copydata(om, 0, OS_MBUF_PKTLEN(om), buf);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    *out_len = OS_MBUF_PKTLEN(om);
-
-    os_mbuf_free_chain(om);
-    return 0;
-}
-
-int
-ble_hs_test_util_write_local_flat(uint16_t attr_handle,
-                                  const void *buf, uint16_t buf_len)
-{
-    struct os_mbuf *om;
-    int rc;
-
-    om = ble_hs_test_util_om_from_flat(buf, buf_len);
-    rc = ble_att_svr_write_local(attr_handle, om);
-    return rc;
-}
-
-int
-ble_hs_test_util_gatt_write_flat(uint16_t conn_handle, uint16_t attr_handle,
-                                 const void *data, uint16_t data_len,
-                                 ble_gatt_attr_fn *cb, void *cb_arg)
-{
-    struct os_mbuf *om;
-    int rc;
-
-    om = ble_hs_test_util_om_from_flat(data, data_len);
-    rc = ble_gattc_write(conn_handle, attr_handle, om, cb, cb_arg);
-
-    return rc;
-}
-
-int
-ble_hs_test_util_gatt_write_no_rsp_flat(uint16_t conn_handle,
-                                        uint16_t attr_handle,
-                                        const void *data, uint16_t data_len)
-{
-    struct os_mbuf *om;
-    int rc;
-
-    om = ble_hs_test_util_om_from_flat(data, data_len);
-    rc = ble_gattc_write_no_rsp(conn_handle, attr_handle, om);
-
-    return rc;
-}
-
-int
-ble_hs_test_util_gatt_write_long_flat(uint16_t conn_handle,
-                                      uint16_t attr_handle,
-                                      const void *data, uint16_t data_len,
-                                      ble_gatt_attr_fn *cb, void *cb_arg)
-{
-    struct os_mbuf *om;
-    int rc;
-
-    om = ble_hs_test_util_om_from_flat(data, data_len);
-    rc = ble_gattc_write_long(conn_handle, attr_handle, om, cb, cb_arg);
-
-    return rc;
-}
-
-static int
-ble_hs_test_util_mbuf_chain_len(const struct os_mbuf *om)
-{
-    int count;
-
-    count = 0;
-    while (om != NULL) {
-        count++;
-        om = SLIST_NEXT(om, om_next);
-    }
-
-    return count;
-}
-
-int
-ble_hs_test_util_mbuf_count(const struct ble_hs_test_util_mbuf_params *params)
-{
-    const struct ble_att_prep_entry *prep;
-    const struct os_mbuf_pkthdr *omp;
-    const struct ble_l2cap_chan *chan;
-    const struct ble_hs_conn *conn;
-    const struct os_mbuf *om;
-    int count;
-    int i;
-
-    ble_hs_process_tx_data_queue();
-    ble_hs_process_rx_data_queue();
-
-    count = ble_hs_test_util_mbuf_mpool.mp_num_free;
-
-    if (params->prev_tx) {
-        count += ble_hs_test_util_mbuf_chain_len(ble_hs_test_util_prev_tx_cur);
-        STAILQ_FOREACH(omp, &ble_hs_test_util_prev_tx_queue, omp_next) {
-            om = OS_MBUF_PKTHDR_TO_MBUF(omp);
-            count += ble_hs_test_util_mbuf_chain_len(om);
-        }
-    }
-
-    ble_hs_lock();
-    for (i = 0; ; i++) {
-        conn = ble_hs_conn_find_by_idx(i);
-        if (conn == NULL) {
-            break;
-        }
-
-        if (params->rx_queue) {
-            SLIST_FOREACH(chan, &conn->bhc_channels, blc_next) {
-                count += ble_hs_test_util_mbuf_chain_len(chan->blc_rx_buf);
-            }
-        }
-
-        if (params->prep_list) {
-            SLIST_FOREACH(prep, &conn->bhc_att_svr.basc_prep_list, bape_next) {
-                count += ble_hs_test_util_mbuf_chain_len(prep->bape_value);
-            }
-        }
-    }
-    ble_hs_unlock();
-
-    return count;
-}
-
-void
-ble_hs_test_util_assert_mbufs_freed(
-    const struct ble_hs_test_util_mbuf_params *params)
-{
-    static const struct ble_hs_test_util_mbuf_params dflt = {
-        .prev_tx = 1,
-        .rx_queue = 1,
-        .prep_list = 1,
-    };
-
-    int count;
-
-    if (params == NULL) {
-        params = &dflt;
-    }
-
-    count = ble_hs_test_util_mbuf_count(params);
-    TEST_ASSERT(count == ble_hs_test_util_mbuf_mpool.mp_num_blocks);
-}
-
-void
-ble_hs_test_util_post_test(void *arg)
-{
-    ble_hs_test_util_assert_mbufs_freed(arg);
-}
-
-static int
-ble_hs_test_util_pkt_txed(struct os_mbuf *om, void *arg)
-{
-    ble_hs_test_util_prev_tx_enqueue(om);
-    return 0;
-}
-
-static int
-ble_hs_test_util_hci_txed(uint8_t *cmdbuf, void *arg)
-{
-    ble_hs_test_util_enqueue_hci_tx(cmdbuf);
-    ble_hci_trans_buf_free(cmdbuf);
-    return 0;
-}
-
-void
-ble_hs_test_util_init(void)
-{
-    struct ble_hci_ram_cfg hci_cfg;
-    struct ble_hs_cfg cfg;
-    int rc;
-
-    tu_init();
-
-    os_eventq_init(&ble_hs_test_util_evq);
-    STAILQ_INIT(&ble_hs_test_util_prev_tx_queue);
-    ble_hs_test_util_prev_tx_cur = NULL;
-
-    os_msys_reset();
-    stats_module_reset();
-
-    cfg = ble_hs_cfg_dflt;
-    cfg.max_connections = 8;
-    cfg.max_l2cap_chans = 3 * cfg.max_connections;
-    cfg.max_services = 16;
-    cfg.max_client_configs = 32;
-    cfg.max_attrs = 64;
-    cfg.max_gattc_procs = 16;
-
-    rc = ble_hs_init(&ble_hs_test_util_evq, &cfg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    rc = os_mempool_init(&ble_hs_test_util_mbuf_mpool,
-                         BLE_HS_TEST_UTIL_NUM_MBUFS, 
-                         BLE_HS_TEST_UTIL_MEMBLOCK_SIZE,
-                         ble_hs_test_util_mbuf_mpool_data, 
-                         "ble_hs_test_util_mbuf_data");
-    TEST_ASSERT_FATAL(rc == 0);
-
-    rc = os_mbuf_pool_init(&ble_hs_test_util_mbuf_pool,
-                           &ble_hs_test_util_mbuf_mpool,
-                           BLE_HS_TEST_UTIL_MEMBLOCK_SIZE,
-                           BLE_HS_TEST_UTIL_NUM_MBUFS);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    rc = os_msys_register(&ble_hs_test_util_mbuf_pool);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_hs_hci_set_phony_ack_cb(NULL);
-
-    ble_hci_trans_cfg_ll(ble_hs_test_util_hci_txed, NULL,
-                         ble_hs_test_util_pkt_txed, NULL);
-
-    hci_cfg = ble_hci_ram_cfg_dflt;
-    rc = ble_hci_ram_init(&hci_cfg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_hs_test_util_set_startup_acks();
-
-    rc = ble_hs_start();
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_hs_test_util_prev_hci_tx_clear();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_hs_test_util.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util.h b/net/nimble/host/src/test/ble_hs_test_util.h
deleted file mode 100644
index 3320e9b..0000000
--- a/net/nimble/host/src/test/ble_hs_test_util.h
+++ /dev/null
@@ -1,176 +0,0 @@
-/**
- * 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_BLE_HS_TEST_UTIL_
-#define H_BLE_HS_TEST_UTIL_
-
-#include <inttypes.h>
-#include "host/ble_gap.h"
-#include "ble_hs_priv.h"
-#include "ble_hs_test_util_store.h"
-struct ble_hs_conn;
-struct ble_l2cap_chan;
-struct hci_disconn_complete;
-struct hci_create_conn;
-
-extern struct os_eventq ble_hs_test_util_evq;
-extern const struct ble_gap_adv_params ble_hs_test_util_adv_params;
-
-struct ble_hs_test_util_num_completed_pkts_entry {
-    uint16_t handle_id; /* 0 for terminating entry in array. */
-    uint16_t num_pkts;
-};
-
-struct ble_hs_test_util_flat_attr {
-    uint16_t handle;
-    uint16_t offset;
-    uint8_t value[BLE_ATT_ATTR_MAX_LEN];
-    uint16_t value_len;
-};
-
-struct ble_hs_test_util_mbuf_params {
-    unsigned prev_tx:1;
-    unsigned rx_queue:1;
-    unsigned prep_list:1;
-};
-
-void ble_hs_test_util_prev_tx_enqueue(struct os_mbuf *om);
-struct os_mbuf *ble_hs_test_util_prev_tx_dequeue(void);
-struct os_mbuf *ble_hs_test_util_prev_tx_dequeue_pullup(void);
-int ble_hs_test_util_prev_tx_queue_sz(void);
-void ble_hs_test_util_prev_tx_queue_clear(void);
-
-void ble_hs_test_util_set_ack_params(uint16_t opcode, uint8_t status,
-                                     void *params, uint8_t params_len);
-void ble_hs_test_util_set_ack(uint16_t opcode, uint8_t status);
-void *ble_hs_test_util_get_first_hci_tx(void);
-void *ble_hs_test_util_get_last_hci_tx(void);
-void ble_hs_test_util_enqueue_hci_tx(void *cmd);
-void ble_hs_test_util_prev_hci_tx_clear(void);
-void ble_hs_test_util_build_cmd_complete(uint8_t *dst, int len,
-                                         uint8_t param_len, uint8_t num_pkts,
-                                         uint16_t opcode);
-void ble_hs_test_util_build_cmd_status(uint8_t *dst, int len,
-                                       uint8_t status, uint8_t num_pkts,
-                                       uint16_t opcode);
-void ble_hs_test_util_create_rpa_conn(uint16_t handle, uint8_t own_addr_type,
-                                      const uint8_t *our_rpa,
-                                      uint8_t peer_addr_type,
-                                      const uint8_t *peer_id_addr,
-                                      const uint8_t *peer_rpa,
-                                      ble_gap_event_fn *cb, void *cb_arg);
-void ble_hs_test_util_create_conn(uint16_t handle, uint8_t *addr,
-                                  ble_gap_event_fn *cb, void *cb_arg);
-int ble_hs_test_util_connect(uint8_t own_addr_type,
-                                   uint8_t peer_addr_type,
-                                   const uint8_t *peer_addr,
-                                   int32_t duration_ms,
-                                   const struct ble_gap_conn_params *params,
-                                   ble_gap_event_fn *cb,
-                                   void *cb_arg,
-                                   uint8_t ack_status);
-int ble_hs_test_util_conn_cancel(uint8_t ack_status);
-void ble_hs_test_util_conn_cancel_full(void);
-int ble_hs_test_util_conn_terminate(uint16_t conn_handle, uint8_t hci_status);
-void ble_hs_test_util_conn_disconnect(uint16_t conn_handle);
-int ble_hs_test_util_exp_hci_status(int cmd_idx, int fail_idx,
-                                    uint8_t fail_status);
-int ble_hs_test_util_disc(uint8_t own_addr_type, int32_t duration_ms,
-                          const struct ble_gap_disc_params *disc_params,
-                          ble_gap_event_fn *cb, void *cb_arg, int fail_idx,
-                          uint8_t fail_status);
-int ble_hs_test_util_disc_cancel(uint8_t ack_status);
-void ble_hs_test_util_verify_tx_create_conn(const struct hci_create_conn *exp);
-int ble_hs_test_util_adv_set_fields(struct ble_hs_adv_fields *adv_fields,
-                                    uint8_t hci_status);
-int ble_hs_test_util_adv_start(uint8_t own_addr_type,
-                               uint8_t peer_addr_type,
-                               const uint8_t *peer_addr,
-                               const struct ble_gap_adv_params *adv_params,
-                               ble_gap_event_fn *cb, void *cb_arg,
-                               int fail_idx, uint8_t fail_status);
-int ble_hs_test_util_adv_stop(uint8_t hci_status);
-int ble_hs_test_util_wl_set(struct ble_gap_white_entry *white_list,
-                            uint8_t white_list_count,
-                            int fail_idx, uint8_t fail_status);
-int ble_hs_test_util_conn_update(uint16_t conn_handle,
-                                 struct ble_gap_upd_params *params,
-                                 uint8_t hci_status);
-int ble_hs_test_util_set_our_irk(const uint8_t *irk, int fail_idx,
-                                 uint8_t hci_status);
-int ble_hs_test_util_security_initiate(uint16_t conn_handle,
-                                       uint8_t hci_status);
-int ble_hs_test_util_l2cap_rx_first_frag(uint16_t conn_handle, uint16_t cid,
-                                         struct hci_data_hdr *hci_hdr,
-                                         struct os_mbuf *om);
-int ble_hs_test_util_l2cap_rx(uint16_t conn_handle,
-                              struct hci_data_hdr *hci_hdr,
-                              struct os_mbuf *om);
-int ble_hs_test_util_l2cap_rx_payload_flat(uint16_t conn_handle, uint16_t cid,
-                                           const void *data, int len);
-void ble_hs_test_util_rx_hci_buf_size_ack(uint16_t buf_size);
-void ble_hs_test_util_rx_att_err_rsp(uint16_t conn_handle, uint8_t req_op,
-                                     uint8_t error_code, uint16_t err_handle);
-void ble_hs_test_util_set_startup_acks(void);
-void ble_hs_test_util_rx_num_completed_pkts_event(
-    struct ble_hs_test_util_num_completed_pkts_entry *entries);
-void ble_hs_test_util_rx_disconn_complete_event(
-    struct hci_disconn_complete *evt);
-uint8_t *ble_hs_test_util_verify_tx_hci(uint8_t ogf, uint16_t ocf,
-                                        uint8_t *out_param_len);
-void ble_hs_test_util_tx_all(void);
-void ble_hs_test_util_verify_tx_prep_write(uint16_t attr_handle,
-                                           uint16_t offset,
-                                           const void *data, int data_len);
-void ble_hs_test_util_verify_tx_exec_write(uint8_t expected_flags);
-void ble_hs_test_util_verify_tx_read_rsp(uint8_t *attr_data, int attr_len);
-void ble_hs_test_util_verify_tx_read_blob_rsp(uint8_t *attr_data,
-                                              int attr_len);
-void ble_hs_test_util_set_static_rnd_addr(void);
-struct os_mbuf *ble_hs_test_util_om_from_flat(const void *buf, uint16_t len);
-int ble_hs_test_util_flat_attr_cmp(const struct ble_hs_test_util_flat_attr *a,
-                                   const struct ble_hs_test_util_flat_attr *b);
-void ble_hs_test_util_attr_to_flat(struct ble_hs_test_util_flat_attr *flat,
-                                   const struct ble_gatt_attr *attr);
-void ble_hs_test_util_attr_from_flat(
-    struct ble_gatt_attr *attr, const struct ble_hs_test_util_flat_attr *flat);
-int ble_hs_test_util_read_local_flat(uint16_t attr_handle, uint16_t max_len,
-                                     void *buf, uint16_t *out_len);
-int ble_hs_test_util_write_local_flat(uint16_t attr_handle,
-                                      const void *buf, uint16_t buf_len);
-int ble_hs_test_util_gatt_write_flat(uint16_t conn_handle,
-                                     uint16_t attr_handle,
-                                     const void *data, uint16_t data_len,
-                                     ble_gatt_attr_fn *cb, void *cb_arg);
-int ble_hs_test_util_gatt_write_no_rsp_flat(uint16_t conn_handle,
-                                            uint16_t attr_handle,
-                                            const void *data,
-                                            uint16_t data_len);
-int ble_hs_test_util_gatt_write_long_flat(uint16_t conn_handle,
-                                          uint16_t attr_handle,
-                                          const void *data, uint16_t data_len,
-                                          ble_gatt_attr_fn *cb, void *cb_arg);
-int ble_hs_test_util_mbuf_count(
-    const struct ble_hs_test_util_mbuf_params *params);
-void ble_hs_test_util_assert_mbufs_freed(
-    const struct ble_hs_test_util_mbuf_params *params);
-void ble_hs_test_util_post_test(void *arg);
-void ble_hs_test_util_init(void);
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_hs_test_util_store.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util_store.c b/net/nimble/host/src/test/ble_hs_test_util_store.c
deleted file mode 100644
index f888753..0000000
--- a/net/nimble/host/src/test/ble_hs_test_util_store.c
+++ /dev/null
@@ -1,248 +0,0 @@
-/**
- * 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 <string.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "ble_hs_test_util.h"
-#include "ble_hs_test_util_store.h"
-
-static int ble_hs_test_util_store_max_our_secs;
-static int ble_hs_test_util_store_max_peer_secs;
-static int ble_hs_test_util_store_max_cccds;
-
-static struct ble_store_value_sec *ble_hs_test_util_store_our_secs;
-static struct ble_store_value_sec *ble_hs_test_util_store_peer_secs;
-static struct ble_store_value_cccd *ble_hs_test_util_store_cccds;
-int ble_hs_test_util_store_num_our_secs;
-int ble_hs_test_util_store_num_peer_secs;
-int ble_hs_test_util_store_num_cccds;
-
-
-#define BLE_HS_TEST_UTIL_STORE_WRITE_GEN(store, num_vals, max_vals, \
-                                         val, idx) do               \
-{                                                                   \
-    if ((idx) == -1) {                                              \
-        if ((num_vals) >= (max_vals)) {                             \
-            return BLE_HS_ENOMEM;                                   \
-        }                                                           \
-        store[(num_vals)] = (val);                                  \
-        (num_vals)++;                                               \
-    } else {                                                        \
-        store[(idx)] = val;                                         \
-    }                                                               \
-    return 0;                                                       \
-} while (0) 
-
-void
-ble_hs_test_util_store_init(int max_our_secs, int max_peer_secs, int max_cccds)
-{
-    free(ble_hs_test_util_store_our_secs);
-    free(ble_hs_test_util_store_peer_secs);
-    free(ble_hs_test_util_store_cccds);
-
-    ble_hs_test_util_store_our_secs = malloc(
-        ble_hs_test_util_store_max_our_secs *
-        sizeof *ble_hs_test_util_store_our_secs);
-    TEST_ASSERT_FATAL(ble_hs_test_util_store_our_secs != NULL);
-
-    ble_hs_test_util_store_peer_secs = malloc(
-        ble_hs_test_util_store_max_peer_secs *
-        sizeof *ble_hs_test_util_store_peer_secs);
-    TEST_ASSERT_FATAL(ble_hs_test_util_store_peer_secs != NULL);
-
-    ble_hs_test_util_store_cccds = malloc(
-        ble_hs_test_util_store_max_cccds *
-        sizeof *ble_hs_test_util_store_cccds);
-    TEST_ASSERT_FATAL(ble_hs_test_util_store_cccds != NULL);
-
-    ble_hs_test_util_store_max_our_secs = max_our_secs;
-    ble_hs_test_util_store_max_peer_secs = max_peer_secs;
-    ble_hs_test_util_store_max_cccds = max_cccds;
-    ble_hs_test_util_store_num_our_secs = 0;
-    ble_hs_test_util_store_num_peer_secs = 0;
-    ble_hs_test_util_store_num_cccds = 0;
-}
-
-static int
-ble_hs_test_util_store_read_sec(struct ble_store_value_sec *store,
-                                int num_values,
-                                struct ble_store_key_sec *key,
-                                struct ble_store_value_sec *value)
-{
-    struct ble_store_value_sec *cur;
-    int skipped;
-    int i;
-
-    skipped = 0;
-
-    for (i = 0; i < num_values; i++) {
-        cur = store + i;
-
-        if (key->peer_addr_type != BLE_STORE_ADDR_TYPE_NONE) {
-            if (cur->peer_addr_type != key->peer_addr_type) {
-                continue;
-            }
-
-            if (memcmp(cur->peer_addr, key->peer_addr,
-                       sizeof cur->peer_addr) != 0) {
-                continue;
-            }
-        }
-
-        if (key->ediv_rand_present) {
-            if (cur->ediv != key->ediv) {
-                continue;
-            }
-
-            if (cur->rand_num != key->rand_num) {
-                continue;
-            }
-        }
-
-        if (key->idx > skipped) {
-            skipped++;
-            continue;
-        }
-
-        *value = *cur;
-        return 0;
-    }
-
-    return BLE_HS_ENOENT;
-}
-
-static int
-ble_hs_test_util_store_find_cccd(struct ble_store_key_cccd *key)
-{
-    struct ble_store_value_cccd *cur;
-    int skipped;
-    int i;
-
-    skipped = 0;
-    for (i = 0; i < ble_hs_test_util_store_num_cccds; i++) {
-        cur = ble_hs_test_util_store_cccds + i;
-
-        if (key->peer_addr_type != BLE_STORE_ADDR_TYPE_NONE) {
-            if (cur->peer_addr_type != key->peer_addr_type) {
-                continue;
-            }
-
-            if (memcmp(cur->peer_addr, key->peer_addr, 6) != 0) {
-                continue;
-            }
-        }
-
-        if (key->chr_val_handle != 0) {
-            if (cur->chr_val_handle != key->chr_val_handle) {
-                continue;
-            }
-        }
-
-        if (key->idx > skipped) {
-            skipped++;
-            continue;
-        }
-
-        return i;
-    }
-
-    return -1;
-}
-
-static int
-ble_hs_test_util_store_read_cccd(struct ble_store_key_cccd *key,
-                                 struct ble_store_value_cccd *value)
-{
-    int idx;
-
-    idx = ble_hs_test_util_store_find_cccd(key);
-    if (idx == -1) {
-        return BLE_HS_ENOENT;
-    }
-
-    *value = ble_hs_test_util_store_cccds[idx];
-    return 0;
-}
-
-int
-ble_hs_test_util_store_read(int obj_type, union ble_store_key *key,
-                            union ble_store_value *dst)
-{
-    switch (obj_type) {
-    case BLE_STORE_OBJ_TYPE_PEER_SEC:
-        return ble_hs_test_util_store_read_sec(
-            ble_hs_test_util_store_peer_secs,
-            ble_hs_test_util_store_num_peer_secs,
-            &key->sec,
-            &dst->sec);
-
-    case BLE_STORE_OBJ_TYPE_OUR_SEC:
-        return ble_hs_test_util_store_read_sec(
-            ble_hs_test_util_store_our_secs,
-            ble_hs_test_util_store_num_our_secs,
-            &key->sec,
-            &dst->sec);
-
-    case BLE_STORE_OBJ_TYPE_CCCD:
-        return ble_hs_test_util_store_read_cccd(&key->cccd, &dst->cccd);
-
-    default:
-        TEST_ASSERT_FATAL(0);
-        return BLE_HS_EUNKNOWN;
-    }
-}
-
-int
-ble_hs_test_util_store_write(int obj_type, union ble_store_value *value)
-{
-    struct ble_store_key_cccd key_cccd;
-    int idx;
-
-    switch (obj_type) {
-    case BLE_STORE_OBJ_TYPE_PEER_SEC:
-        BLE_HS_TEST_UTIL_STORE_WRITE_GEN(
-            ble_hs_test_util_store_peer_secs,
-            ble_hs_test_util_store_num_peer_secs,
-            ble_hs_test_util_store_max_peer_secs,
-            value->sec, -1);
-
-    case BLE_STORE_OBJ_TYPE_OUR_SEC:
-        BLE_HS_TEST_UTIL_STORE_WRITE_GEN(
-            ble_hs_test_util_store_our_secs,
-            ble_hs_test_util_store_num_our_secs,
-            ble_hs_test_util_store_max_our_secs,
-            value->sec, -1);
-
-    case BLE_STORE_OBJ_TYPE_CCCD:
-        ble_store_key_from_value_cccd(&key_cccd, &value->cccd);
-        idx = ble_hs_test_util_store_find_cccd(&key_cccd);
-        BLE_HS_TEST_UTIL_STORE_WRITE_GEN(
-            ble_hs_test_util_store_cccds,
-            ble_hs_test_util_store_num_cccds,
-            ble_hs_test_util_store_max_cccds,
-            value->cccd, idx);
-
-    default:
-        TEST_ASSERT_FATAL(0);
-        return BLE_HS_EUNKNOWN;
-    }
-
-    return 0;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_hs_test_util_store.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test_util_store.h b/net/nimble/host/src/test/ble_hs_test_util_store.h
deleted file mode 100644
index 77ee291..0000000
--- a/net/nimble/host/src/test/ble_hs_test_util_store.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/**
- * 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_BLE_HS_TEST_UTIL_STORE_
-#define H_BLE_HS_TEST_UTIL_STORE_
-
-union ble_store_value;
-union ble_store_key;
-
-extern int ble_hs_test_util_store_num_our_ltks;
-extern int ble_hs_test_util_store_num_peer_ltks;
-extern int ble_hs_test_util_store_num_cccds;
-
-void ble_hs_test_util_store_init(int max_our_ltks, int max_peer_ltks,
-                                 int max_cccds);
-int ble_hs_test_util_store_read(int obj_type, union ble_store_key *key,
-                                union ble_store_value *dst);
-int ble_hs_test_util_store_write(int obj_type, union ble_store_value *value);
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_l2cap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_l2cap_test.c b/net/nimble/host/src/test/ble_l2cap_test.c
deleted file mode 100644
index 69db2f8..0000000
--- a/net/nimble/host/src/test/ble_l2cap_test.c
+++ /dev/null
@@ -1,690 +0,0 @@
-/**
- * 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 <stddef.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/hci_common.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-
-#define BLE_L2CAP_TEST_CID  99
-
-static int ble_l2cap_test_update_status;
-static void *ble_l2cap_test_update_arg;
-
-/*****************************************************************************
- * $util                                                                     *
- *****************************************************************************/
-
-#define BLE_L2CAP_TEST_UTIL_HCI_HDR(handle, pb, len)    \
-    ((struct hci_data_hdr) {                            \
-        .hdh_handle_pb_bc = ((handle)  << 0) |          \
-                            ((pb)      << 12),          \
-        .hdh_len = (len)                                \
-    })
-
-static void
-ble_l2cap_test_util_init(void)
-{
-    ble_hs_test_util_init();
-    ble_l2cap_test_update_status = -1;
-    ble_l2cap_test_update_arg = (void *)(uintptr_t)-1;
-}
-
-static void
-ble_l2cap_test_util_rx_update_req(uint16_t conn_handle, uint8_t id,
-                                  struct ble_l2cap_sig_update_params *params)
-{
-    struct ble_l2cap_sig_update_req req;
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int rc;
-
-    hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_L2CAP_SIG_HDR_SZ + BLE_L2CAP_SIG_UPDATE_REQ_SZ);
-
-    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_UPDATE_REQ, id,
-                                BLE_L2CAP_SIG_UPDATE_REQ_SZ, &om, &v);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    req.itvl_min = params->itvl_min;
-    req.itvl_max = params->itvl_max;
-    req.slave_latency = params->slave_latency;
-    req.timeout_multiplier = params->timeout_multiplier;
-    ble_l2cap_sig_update_req_write(v, BLE_L2CAP_SIG_UPDATE_REQ_SZ, &req);
-
-    ble_hs_test_util_set_ack(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_CONN_UPDATE), 0);
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SIG,
-                                              &hci_hdr, om);
-    TEST_ASSERT_FATAL(rc == 0);
-}
-
-static int
-ble_l2cap_test_util_rx_update_rsp(uint16_t conn_handle,
-                                  uint8_t id, uint16_t result)
-{
-    struct ble_l2cap_sig_update_rsp rsp;
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int rc;
-
-    hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_L2CAP_SIG_HDR_SZ + BLE_L2CAP_SIG_UPDATE_RSP_SZ);
-
-    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_UPDATE_RSP, id,
-                                BLE_L2CAP_SIG_UPDATE_RSP_SZ, &om, &v);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    rsp.result = result;
-    ble_l2cap_sig_update_rsp_write(v, BLE_L2CAP_SIG_UPDATE_RSP_SZ, &rsp);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SIG,
-                                              &hci_hdr, om);
-    return rc;
-}
-
-
-static struct os_mbuf *
-ble_l2cap_test_util_verify_tx_sig_hdr(uint8_t op, uint8_t id,
-                                      uint16_t payload_len,
-                                      struct ble_l2cap_sig_hdr *out_hdr)
-{
-    struct ble_l2cap_sig_hdr hdr;
-    struct os_mbuf *om;
-
-    om = ble_hs_test_util_prev_tx_dequeue();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    TEST_ASSERT(OS_MBUF_PKTLEN(om) == BLE_L2CAP_SIG_HDR_SZ + payload_len);
-    ble_l2cap_sig_hdr_parse(om->om_data, om->om_len, &hdr);
-    TEST_ASSERT(hdr.op == op);
-    if (id != 0) {
-        TEST_ASSERT(hdr.identifier == id);
-    }
-    TEST_ASSERT(hdr.length == payload_len);
-
-    om->om_data += BLE_L2CAP_SIG_HDR_SZ;
-    om->om_len -= BLE_L2CAP_SIG_HDR_SZ;
-
-    if (out_hdr != NULL) {
-        *out_hdr = hdr;
-    }
-
-    return om;
-}
-
-/**
- * @return                      The L2CAP sig identifier in the request.
- */
-static uint8_t
-ble_l2cap_test_util_verify_tx_update_req(
-    struct ble_l2cap_sig_update_params *params)
-{
-    struct ble_l2cap_sig_update_req req;
-    struct ble_l2cap_sig_hdr hdr;
-    struct os_mbuf *om;
-
-    om = ble_l2cap_test_util_verify_tx_sig_hdr(BLE_L2CAP_SIG_OP_UPDATE_REQ, 0,
-                                               BLE_L2CAP_SIG_UPDATE_REQ_SZ,
-                                               &hdr);
-
-    /* Verify payload. */
-    ble_l2cap_sig_update_req_parse(om->om_data, om->om_len, &req);
-    TEST_ASSERT(req.itvl_min == params->itvl_min);
-    TEST_ASSERT(req.itvl_max == params->itvl_max);
-    TEST_ASSERT(req.slave_latency == params->slave_latency);
-    TEST_ASSERT(req.timeout_multiplier == params->timeout_multiplier);
-
-    return hdr.identifier;
-}
-
-static void
-ble_l2cap_test_util_verify_tx_update_rsp(uint8_t exp_id, uint16_t exp_result)
-{
-    struct ble_l2cap_sig_update_rsp rsp;
-    struct os_mbuf *om;
-
-    om = ble_l2cap_test_util_verify_tx_sig_hdr(BLE_L2CAP_SIG_OP_UPDATE_RSP,
-                                               exp_id,
-                                               BLE_L2CAP_SIG_UPDATE_RSP_SZ,
-                                               NULL);
-
-    ble_l2cap_sig_update_rsp_parse(om->om_data, om->om_len, &rsp);
-    TEST_ASSERT(rsp.result == exp_result);
-}
-
-static void
-ble_l2cap_test_util_verify_tx_update_conn(
-    struct ble_gap_upd_params *params)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_CONN_UPDATE,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_CONN_UPDATE_LEN);
-    TEST_ASSERT(le16toh(param + 0) == 2);
-    TEST_ASSERT(le16toh(param + 2) == params->itvl_min);
-    TEST_ASSERT(le16toh(param + 4) == params->itvl_max);
-    TEST_ASSERT(le16toh(param + 6) == params->latency);
-    TEST_ASSERT(le16toh(param + 8) == params->supervision_timeout);
-    TEST_ASSERT(le16toh(param + 10) == params->min_ce_len);
-    TEST_ASSERT(le16toh(param + 12) == params->max_ce_len);
-}
-
-static int
-ble_l2cap_test_util_dummy_rx(uint16_t conn_handle, struct os_mbuf **om)
-{
-    return 0;
-}
-
-static void
-ble_l2cap_test_util_create_conn(uint16_t conn_handle, uint8_t *addr,
-                                ble_gap_event_fn *cb, void *cb_arg)
-{
-    struct ble_l2cap_chan *chan;
-    struct ble_hs_conn *conn;
-
-    ble_hs_test_util_create_conn(conn_handle, addr, cb, cb_arg);
-
-    ble_hs_lock();
-
-    conn = ble_hs_conn_find(conn_handle);
-    TEST_ASSERT_FATAL(conn != NULL);
-
-    chan = ble_l2cap_chan_alloc();
-    TEST_ASSERT_FATAL(chan != NULL);
-
-    chan->blc_cid = BLE_L2CAP_TEST_CID;
-    chan->blc_my_mtu = 240;
-    chan->blc_default_mtu = 240;
-    chan->blc_rx_fn = ble_l2cap_test_util_dummy_rx;
-
-    ble_hs_conn_chan_insert(conn, chan);
-
-    ble_hs_test_util_prev_hci_tx_clear();
-
-    ble_hs_unlock();
-}
-
-static int
-ble_l2cap_test_util_rx_first_frag(uint16_t conn_handle,
-                                  uint16_t l2cap_frag_len,
-                                  uint16_t cid, uint16_t l2cap_len)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    uint16_t hci_len;
-    void *v;
-    int rc;
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    v = os_mbuf_extend(om, l2cap_frag_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    om = ble_l2cap_prepend_hdr(om, cid, l2cap_len);
-    TEST_ASSERT_FATAL(om != NULL);
-
-    hci_len = sizeof hci_hdr + l2cap_frag_len;
-    hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(conn_handle,
-                                          BLE_HCI_PB_FIRST_FLUSH, hci_len);
-    rc = ble_hs_test_util_l2cap_rx(conn_handle, &hci_hdr, om);
-    return rc;
-}
-
-static int
-ble_l2cap_test_util_rx_next_frag(uint16_t conn_handle, uint16_t hci_len)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int rc;
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    v = os_mbuf_extend(om, hci_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(conn_handle,
-                                          BLE_HCI_PB_MIDDLE, hci_len);
-    rc = ble_hs_test_util_l2cap_rx(conn_handle, &hci_hdr, om);
-    return rc;
-}
-
-static void
-ble_l2cap_test_util_verify_first_frag(uint16_t conn_handle,
-                                      uint16_t l2cap_frag_len,
-                                      uint16_t l2cap_len)
-{
-    struct ble_hs_conn *conn;
-    int rc;
-
-    rc = ble_l2cap_test_util_rx_first_frag(conn_handle, l2cap_frag_len,
-                                           BLE_L2CAP_TEST_CID, l2cap_len);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_lock();
-
-    conn = ble_hs_conn_find(conn_handle);
-    TEST_ASSERT_FATAL(conn != NULL);
-    TEST_ASSERT(conn->bhc_rx_chan != NULL &&
-                conn->bhc_rx_chan->blc_cid == BLE_L2CAP_TEST_CID);
-
-    ble_hs_unlock();
-}
-
-static void
-ble_l2cap_test_util_verify_middle_frag(uint16_t conn_handle,
-                                       uint16_t hci_len)
-{
-    struct ble_hs_conn *conn;
-    int rc;
-
-    rc = ble_l2cap_test_util_rx_next_frag(conn_handle, hci_len);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_lock();
-
-    conn = ble_hs_conn_find(conn_handle);
-    TEST_ASSERT_FATAL(conn != NULL);
-    TEST_ASSERT(conn->bhc_rx_chan != NULL &&
-                conn->bhc_rx_chan->blc_cid == BLE_L2CAP_TEST_CID);
-
-    ble_hs_unlock();
-}
-
-static void
-ble_l2cap_test_util_verify_last_frag(uint16_t conn_handle,
-                                     uint16_t hci_len)
-{
-    struct ble_hs_conn *conn;
-    int rc;
-
-    rc = ble_l2cap_test_util_rx_next_frag(conn_handle, hci_len);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_lock();
-
-    conn = ble_hs_conn_find(conn_handle);
-    TEST_ASSERT_FATAL(conn != NULL);
-    TEST_ASSERT(conn->bhc_rx_chan == NULL);
-
-    ble_hs_unlock();
-}
-
-/*****************************************************************************
- * $rx                                                                       *
- *****************************************************************************/
-
-TEST_CASE(ble_l2cap_test_case_bad_header)
-{
-    int rc;
-
-    ble_l2cap_test_util_init();
-
-    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
-                                    NULL, NULL);
-
-    rc = ble_l2cap_test_util_rx_first_frag(2, 14, 1234, 10);
-    TEST_ASSERT(rc == BLE_HS_ENOENT);
-}
-
-/*****************************************************************************
- * $fragmentation                                                            *
- *****************************************************************************/
-
-TEST_CASE(ble_l2cap_test_case_frag_single)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    int rc;
-
-    ble_l2cap_test_util_init();
-
-    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
-                                    NULL, NULL);
-
-    /*** HCI header specifies middle fragment without start. */
-    hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(2, BLE_HCI_PB_MIDDLE, 10);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    om = ble_l2cap_prepend_hdr(om, 0, 5);
-    TEST_ASSERT_FATAL(om != NULL);
-
-    rc = ble_hs_test_util_l2cap_rx(2, &hci_hdr, om);
-    TEST_ASSERT(rc == BLE_HS_EBADDATA);
-
-    /*** Packet consisting of three fragments. */
-    ble_l2cap_test_util_verify_first_frag(2, 10, 30);
-    ble_l2cap_test_util_verify_middle_frag(2, 10);
-    ble_l2cap_test_util_verify_last_frag(2, 10);
-
-    /*** Packet consisting of five fragments. */
-    ble_l2cap_test_util_verify_first_frag(2, 8, 49);
-    ble_l2cap_test_util_verify_middle_frag(2, 13);
-    ble_l2cap_test_util_verify_middle_frag(2, 2);
-    ble_l2cap_test_util_verify_middle_frag(2, 21);
-    ble_l2cap_test_util_verify_last_frag(2, 5);
-}
-
-TEST_CASE(ble_l2cap_test_case_frag_multiple)
-{
-    ble_l2cap_test_util_init();
-
-    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
-                                    NULL, NULL);
-    ble_l2cap_test_util_create_conn(3, ((uint8_t[]){2,3,4,5,6,7}),
-                                    NULL, NULL);
-    ble_l2cap_test_util_create_conn(4, ((uint8_t[]){3,4,5,6,7,8}),
-                                    NULL, NULL);
-
-    ble_l2cap_test_util_verify_first_frag(2, 3, 10);
-    ble_l2cap_test_util_verify_first_frag(3, 2, 5);
-    ble_l2cap_test_util_verify_middle_frag(2, 6);
-    ble_l2cap_test_util_verify_first_frag(4, 1, 4);
-    ble_l2cap_test_util_verify_middle_frag(3, 2);
-    ble_l2cap_test_util_verify_last_frag(3, 1);
-    ble_l2cap_test_util_verify_middle_frag(4, 2);
-    ble_l2cap_test_util_verify_last_frag(4, 1);
-    ble_l2cap_test_util_verify_last_frag(2, 1);
-}
-
-TEST_CASE(ble_l2cap_test_case_frag_channels)
-{
-    struct ble_hs_conn *conn;
-    int rc;
-
-    ble_l2cap_test_util_init();
-
-    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
-                                    NULL, NULL);
-
-    /* Receive a starting fragment on the first channel. */
-    rc = ble_l2cap_test_util_rx_first_frag(2, 14, BLE_L2CAP_TEST_CID, 30);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_lock();
-    conn = ble_hs_conn_find(2);
-    TEST_ASSERT_FATAL(conn != NULL);
-    TEST_ASSERT(conn->bhc_rx_chan != NULL &&
-                conn->bhc_rx_chan->blc_cid == BLE_L2CAP_TEST_CID);
-    ble_hs_unlock();
-
-    /* Receive a starting fragment on a different channel.  The first fragment
-     * should get discarded.
-     */
-    rc = ble_l2cap_test_util_rx_first_frag(2, 14, BLE_L2CAP_CID_ATT, 30);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_lock();
-    conn = ble_hs_conn_find(2);
-    TEST_ASSERT_FATAL(conn != NULL);
-    TEST_ASSERT(conn->bhc_rx_chan != NULL &&
-                conn->bhc_rx_chan->blc_cid == BLE_L2CAP_CID_ATT);
-    ble_hs_unlock();
-}
-
-/*****************************************************************************
- * $unsolicited response                                                     *
- *****************************************************************************/
-
-TEST_CASE(ble_l2cap_test_case_sig_unsol_rsp)
-{
-    int rc;
-
-    ble_l2cap_test_util_init();
-
-    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
-                                    NULL, NULL);
-
-    /* Receive an unsolicited response. */
-    rc = ble_l2cap_test_util_rx_update_rsp(2, 100, 0);
-    TEST_ASSERT(rc == BLE_HS_ENOENT);
-
-    /* Ensure we did not send anything in return. */
-    ble_hs_test_util_tx_all();
-    TEST_ASSERT_FATAL(ble_hs_test_util_prev_tx_dequeue() == NULL);
-}
-
-/*****************************************************************************
- * $update                                                                   *
- *****************************************************************************/
-
-static int
-ble_l2cap_test_util_conn_cb(struct ble_gap_event *event, void *arg)
-{
-    int *accept;
-
-    switch (event->type) {
-    case BLE_GAP_EVENT_L2CAP_UPDATE_REQ:
-        accept = arg;
-        return !*accept;
-
-    default:
-        return 0;
-    }
-}
-
-static void
-ble_l2cap_test_util_peer_updates(int accept)
-{
-    struct ble_l2cap_sig_update_params l2cap_params;
-    struct ble_gap_upd_params params;
-    ble_hs_conn_flags_t conn_flags;
-    int rc;
-
-    ble_l2cap_test_util_init();
-
-    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
-                                    ble_l2cap_test_util_conn_cb,
-                                    &accept);
-
-    l2cap_params.itvl_min = 0x200;
-    l2cap_params.itvl_max = 0x300;
-    l2cap_params.slave_latency = 0;
-    l2cap_params.timeout_multiplier = 0x100;
-    ble_l2cap_test_util_rx_update_req(2, 1, &l2cap_params);
-
-    /* Ensure an update response command got sent. */
-    ble_hs_process_tx_data_queue();
-    ble_l2cap_test_util_verify_tx_update_rsp(1, !accept);
-
-    if (accept) {
-        params.itvl_min = 0x200;
-        params.itvl_max = 0x300;
-        params.latency = 0;
-        params.supervision_timeout = 0x100;
-        params.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
-        params.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
-        ble_l2cap_test_util_verify_tx_update_conn(&params);
-    } else {
-        /* Ensure no update got scheduled. */
-        rc = ble_hs_atomic_conn_flags(2, &conn_flags);
-        TEST_ASSERT(rc == 0 && !(conn_flags & BLE_HS_CONN_F_UPDATE));
-    }
-}
-
-static void
-ble_l2cap_test_util_update_cb(int status, void *arg)
-{
-    ble_l2cap_test_update_status = status;
-    ble_l2cap_test_update_arg = arg;
-}
-
-static void
-ble_l2cap_test_util_we_update(int peer_accepts)
-{
-    struct ble_l2cap_sig_update_params params;
-    uint8_t id;
-    int rc;
-
-    ble_l2cap_test_util_init();
-
-    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
-                                    ble_l2cap_test_util_conn_cb, NULL);
-
-    /* Only the slave can initiate the L2CAP connection update procedure. */
-    ble_hs_atomic_conn_set_flags(2, BLE_HS_CONN_F_MASTER, 0);
-
-    params.itvl_min = 0x200;
-    params.itvl_min = 0x300;
-    params.slave_latency = 0;
-    params.timeout_multiplier = 0x100;
-    rc = ble_l2cap_sig_update(2, &params, ble_l2cap_test_util_update_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_hs_test_util_tx_all();
-
-    /* Ensure an update request got sent. */
-    id = ble_l2cap_test_util_verify_tx_update_req(&params);
-
-    /* Receive response from peer. */
-    rc = ble_l2cap_test_util_rx_update_rsp(2, id, !peer_accepts);
-    TEST_ASSERT(rc == 0);
-
-    /* Ensure callback got called. */
-    if (peer_accepts) {
-        TEST_ASSERT(ble_l2cap_test_update_status == 0);
-    } else {
-        TEST_ASSERT(ble_l2cap_test_update_status == BLE_HS_EREJECT);
-    }
-    TEST_ASSERT(ble_l2cap_test_update_arg == NULL);
-}
-
-TEST_CASE(ble_l2cap_test_case_sig_update_accept)
-{
-    ble_l2cap_test_util_peer_updates(1);
-}
-
-TEST_CASE(ble_l2cap_test_case_sig_update_reject)
-{
-    ble_l2cap_test_util_peer_updates(0);
-}
-
-TEST_CASE(ble_l2cap_test_case_sig_update_init_accept)
-{
-    ble_l2cap_test_util_we_update(1);
-}
-
-TEST_CASE(ble_l2cap_test_case_sig_update_init_reject)
-{
-    ble_l2cap_test_util_we_update(0);
-}
-
-TEST_CASE(ble_l2cap_test_case_sig_update_init_fail_master)
-{
-    struct ble_l2cap_sig_update_params params;
-    int rc;
-
-    ble_l2cap_test_util_init();
-
-    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
-                                    ble_l2cap_test_util_conn_cb, NULL);
-
-    params.itvl_min = 0x200;
-    params.itvl_min = 0x300;
-    params.slave_latency = 0;
-    params.timeout_multiplier = 0x100;
-    rc = ble_l2cap_sig_update(2, &params, ble_l2cap_test_util_update_cb, NULL);
-    TEST_ASSERT_FATAL(rc == BLE_HS_EINVAL);
-
-    /* Ensure callback never called. */
-    ble_hs_test_util_tx_all();
-    TEST_ASSERT(ble_l2cap_test_update_status == -1);
-}
-
-TEST_CASE(ble_l2cap_test_case_sig_update_init_fail_bad_id)
-{
-    struct ble_l2cap_sig_update_params params;
-    uint8_t id;
-    int rc;
-
-    ble_l2cap_test_util_init();
-
-    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
-                                    ble_l2cap_test_util_conn_cb, NULL);
-
-    /* Only the slave can initiate the L2CAP connection update procedure. */
-    ble_hs_atomic_conn_set_flags(2, BLE_HS_CONN_F_MASTER, 0);
-
-    params.itvl_min = 0x200;
-    params.itvl_min = 0x300;
-    params.slave_latency = 0;
-    params.timeout_multiplier = 0x100;
-    rc = ble_l2cap_sig_update(2, &params, ble_l2cap_test_util_update_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_hs_test_util_tx_all();
-
-    /* Ensure an update request got sent. */
-    id = ble_l2cap_test_util_verify_tx_update_req(&params);
-
-    /* Receive response from peer with incorrect ID. */
-    rc = ble_l2cap_test_util_rx_update_rsp(2, id + 1, 0);
-    TEST_ASSERT(rc == BLE_HS_ENOENT);
-
-    /* Ensure callback did not get called. */
-    TEST_ASSERT(ble_l2cap_test_update_status == -1);
-
-    /* Receive response from peer with correct ID. */
-    rc = ble_l2cap_test_util_rx_update_rsp(2, id, 0);
-    TEST_ASSERT(rc == 0);
-
-    /* Ensure callback got called. */
-    TEST_ASSERT(ble_l2cap_test_update_status == 0);
-    TEST_ASSERT(ble_l2cap_test_update_arg == NULL);
-}
-
-TEST_SUITE(ble_l2cap_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_l2cap_test_case_bad_header();
-    ble_l2cap_test_case_frag_single();
-    ble_l2cap_test_case_frag_multiple();
-    ble_l2cap_test_case_frag_channels();
-    ble_l2cap_test_case_sig_unsol_rsp();
-    ble_l2cap_test_case_sig_update_accept();
-    ble_l2cap_test_case_sig_update_reject();
-    ble_l2cap_test_case_sig_update_init_accept();
-    ble_l2cap_test_case_sig_update_init_reject();
-    ble_l2cap_test_case_sig_update_init_fail_master();
-    ble_l2cap_test_case_sig_update_init_fail_bad_id();
-}
-
-int
-ble_l2cap_test_all(void)
-{
-    ble_l2cap_test_suite();
-
-    return tu_any_failed;
-}


[36/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/test/pkg.yml
----------------------------------------------------------------------
diff --git a/fs/nffs/test/pkg.yml b/fs/nffs/test/pkg.yml
new file mode 100644
index 0000000..f7a66f6
--- /dev/null
+++ b/fs/nffs/test/pkg.yml
@@ -0,0 +1,30 @@
+# 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.
+#
+pkg.name: fs/nffs/test
+pkg.type: unittest
+pkg.description: "NFFS unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps: 
+    - fs/nffs
+    - libs/testutil
+
+pkg.deps.SELFTEST:
+    - libs/console/stub

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/test/src/arch/cortex_m4/nffs_test.c
----------------------------------------------------------------------
diff --git a/fs/nffs/test/src/arch/cortex_m4/nffs_test.c b/fs/nffs/test/src/arch/cortex_m4/nffs_test.c
new file mode 100644
index 0000000..654089b
--- /dev/null
+++ b/fs/nffs/test/src/arch/cortex_m4/nffs_test.c
@@ -0,0 +1,27 @@
+/**
+ * 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 "nffs/nffs_test.h"
+
+int
+nffs_test_all(void)
+{
+    return 0;
+}


[29/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/mbedtls/test/src/mbedtls_test.c
----------------------------------------------------------------------
diff --git a/libs/mbedtls/test/src/mbedtls_test.c b/libs/mbedtls/test/src/mbedtls_test.c
new file mode 100644
index 0000000..85038f8
--- /dev/null
+++ b/libs/mbedtls/test/src/mbedtls_test.c
@@ -0,0 +1,228 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+
+#include "syscfg/syscfg.h"
+#include "testutil/testutil.h"
+
+#include "mbedtls/mbedtls_test.h"
+#include "mbedtls/sha1.h"
+#include "mbedtls/sha256.h"
+#include "mbedtls/sha512.h"
+#include "mbedtls/aes.h"
+#include "mbedtls/arc4.h"
+#include "mbedtls/bignum.h"
+#include "mbedtls/ccm.h"
+#include "mbedtls/dhm.h"
+#include "mbedtls/ecjpake.h"
+#include "mbedtls/ecp.h"
+#include "mbedtls/entropy.h"
+#include "mbedtls/gcm.h"
+#include "mbedtls/hmac_drbg.h"
+#include "mbedtls/md5.h"
+#include "mbedtls/pkcs5.h"
+#include "mbedtls/ripemd160.h"
+#include "mbedtls/rsa.h"
+#include "mbedtls/x509.h"
+#include "mbedtls/xtea.h"
+
+TEST_CASE(sha1_test)
+{
+    int rc;
+
+    rc = mbedtls_sha1_self_test(0);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(sha256_test)
+{
+    int rc;
+
+    rc = mbedtls_sha256_self_test(0);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(sha512_test)
+{
+    int rc;
+
+    rc = mbedtls_sha512_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(aes_test)
+{
+    int rc;
+
+    rc = mbedtls_aes_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(arc4_test)
+{
+    int rc;
+
+    rc = mbedtls_arc4_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(bignum_test)
+{
+    int rc;
+
+    rc = mbedtls_mpi_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ccm_test)
+{
+    int rc;
+
+    rc = mbedtls_ccm_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(dhm_test)
+{
+    int rc;
+
+    rc = mbedtls_dhm_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ecp_test)
+{
+    int rc;
+
+    rc = mbedtls_ecp_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(entropy_test)
+{
+#if 0 /* XXX fix this later, no strong entropy source atm */
+    int rc;
+
+    rc = mbedtls_entropy_self_test(1);
+    TEST_ASSERT(rc == 0);
+#endif
+}
+
+TEST_CASE(gcm_test)
+{
+    int rc;
+
+    rc = mbedtls_gcm_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(hmac_drbg_test)
+{
+    int rc;
+
+    rc = mbedtls_hmac_drbg_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(md5_test)
+{
+    int rc;
+
+    rc = mbedtls_md5_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(pkcs5_test)
+{
+    int rc;
+
+    rc = mbedtls_pkcs5_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ripemd160_test)
+{
+    int rc;
+
+    rc = mbedtls_ripemd160_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(rsa_test)
+{
+    int rc;
+
+    rc = mbedtls_rsa_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(x509_test)
+{
+    int rc;
+
+    rc = mbedtls_x509_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(xtea_test)
+{
+    int rc;
+
+    rc = mbedtls_xtea_self_test(1);
+    TEST_ASSERT(rc == 0);
+}
+
+
+TEST_SUITE(mbedtls_test_all)
+{
+    sha1_test();
+    sha256_test();
+    sha512_test();
+    aes_test();
+    arc4_test();
+    bignum_test();
+    ccm_test();
+    dhm_test();
+    ecp_test();
+    entropy_test();
+    gcm_test();
+    hmac_drbg_test();
+    md5_test();
+    pkcs5_test();
+    ripemd160_test();
+    rsa_test();
+    x509_test();
+    xtea_test();
+}
+
+#if MYNEWT_VAL(SELFTEST)
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    mbedtls_test_all();
+
+    return tu_any_failed;
+}
+
+#endif
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/newtmgr/include/newtmgr/newtmgr.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/include/newtmgr/newtmgr.h b/libs/newtmgr/include/newtmgr/newtmgr.h
index 0e5637b..00d1b00 100644
--- a/libs/newtmgr/include/newtmgr/newtmgr.h
+++ b/libs/newtmgr/include/newtmgr/newtmgr.h
@@ -127,7 +127,7 @@ struct nmgr_transport {
 };
 
 
-int nmgr_task_init(uint8_t, os_stack_t *, uint16_t);
+int nmgr_task_init(void);
 int nmgr_transport_init(struct nmgr_transport *nt,
         nmgr_transport_out_func_t output_func);
 int nmgr_rx_req(struct nmgr_transport *nt, struct os_mbuf *req);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/newtmgr/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/newtmgr/pkg.yml b/libs/newtmgr/pkg.yml
index a0e75a2..2dbdb71 100644
--- a/libs/newtmgr/pkg.yml
+++ b/libs/newtmgr/pkg.yml
@@ -28,12 +28,23 @@ pkg.deps:
     - libs/os
     - libs/json
     - libs/util
-    - libs/testutil
     - libs/shell
     - sys/reboot
 
 pkg.deps.BLE_HOST:
     - libs/newtmgr/transport/ble
 
-pkg.features:
-    - NEWTMGR
+pkg.init_function: nmgr_pkg_init
+pkg.init_stage: 5
+
+pkg.syscfg_defs:
+    NEWTMGR_TASK_PRIO:
+        description: 'TBD'
+        type: 'task_priority'
+        value: 'any'
+    NEWTMGR_STACK_SIZE:
+        description: 'TBD'
+        value: 512
+    NEWTMGR_BLE_HOST:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_NET_NIMBLE_HOST'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/newtmgr/src/newtmgr.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/src/newtmgr.c b/libs/newtmgr/src/newtmgr.c
index c3bc614..9b0d696 100644
--- a/libs/newtmgr/src/newtmgr.c
+++ b/libs/newtmgr/src/newtmgr.c
@@ -17,18 +17,22 @@
  * under the License.
  */
 
-#include <os/os.h>
-#include <os/endian.h>
-
 #include <assert.h>
 #include <string.h>
 
-#include <shell/shell.h>
-#include <console/console.h>
-#include <newtmgr/newtmgr.h>
+#include "syscfg/syscfg.h"
+#include "sysinit/sysinit.h"
+#include "os/os.h"
+#include "os/endian.h"
+
+#include "shell/shell.h"
+#include "console/console.h"
+#include "newtmgr/newtmgr.h"
 
 #include "newtmgr_priv.h"
 
+os_stack_t newtmgr_stack[OS_STACK_ALIGN(MYNEWT_VAL(NEWTMGR_STACK_SIZE))];
+
 struct nmgr_transport g_nmgr_shell_transport;
 
 struct os_mutex g_nmgr_group_list_lock;
@@ -624,7 +628,7 @@ err:
 }
 
 int
-nmgr_task_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
+nmgr_task_init(void)
 {
     int rc;
 
@@ -641,8 +645,9 @@ nmgr_task_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
         goto err;
     }
 
-    rc = os_task_init(&g_nmgr_task, "newtmgr", nmgr_task, NULL, prio,
-            OS_WAIT_FOREVER, stack_ptr, stack_len);
+    rc = os_task_init(&g_nmgr_task, "newtmgr", nmgr_task, NULL,
+                      MYNEWT_VAL(NEWTMGR_TASK_PRIO), OS_WAIT_FOREVER,
+                      newtmgr_stack, MYNEWT_VAL(NEWTMGR_STACK_SIZE));
     if (rc != 0) {
         goto err;
     }
@@ -656,3 +661,12 @@ nmgr_task_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
 err:
     return (rc);
 }
+
+void
+nmgr_pkg_init(void)
+{
+    int rc;
+
+    rc = nmgr_task_init();
+    SYSINIT_PANIC_ASSERT(rc == 0);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/newtmgr/transport/ble/include/nmgrble/newtmgr_ble.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/transport/ble/include/nmgrble/newtmgr_ble.h b/libs/newtmgr/transport/ble/include/nmgrble/newtmgr_ble.h
index a320eaa..315a6e8 100644
--- a/libs/newtmgr/transport/ble/include/nmgrble/newtmgr_ble.h
+++ b/libs/newtmgr/transport/ble/include/nmgrble/newtmgr_ble.h
@@ -23,6 +23,6 @@
 int
 nmgr_ble_proc_mq_evt(struct os_event *ev);
 int
-nmgr_ble_gatt_svr_init(struct os_eventq *evq, struct ble_hs_cfg *cfg);
+nmgr_ble_gatt_svr_init(void);
 
 #endif /* _NETMGR_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/newtmgr/transport/ble/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/newtmgr/transport/ble/pkg.yml b/libs/newtmgr/transport/ble/pkg.yml
index 9ab9d4e..8c444fe 100644
--- a/libs/newtmgr/transport/ble/pkg.yml
+++ b/libs/newtmgr/transport/ble/pkg.yml
@@ -28,3 +28,6 @@ pkg.keywords:
 pkg.deps:
     - libs/os
     - net/nimble
+
+pkg.init_function: newtmgr_ble_pkg_init
+pkg.init_stage: 5

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/newtmgr/transport/ble/src/newtmgr_ble.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/transport/ble/src/newtmgr_ble.c b/libs/newtmgr/transport/ble/src/newtmgr_ble.c
index 716a974..5ddbe95 100644
--- a/libs/newtmgr/transport/ble/src/newtmgr_ble.c
+++ b/libs/newtmgr/transport/ble/src/newtmgr_ble.c
@@ -20,9 +20,10 @@
 #include <assert.h>
 #include <stdio.h>
 #include <string.h>
+#include "sysinit/sysinit.h"
 #include "host/ble_hs.h"
-#include <newtmgr/newtmgr.h>
-#include <os/endian.h>
+#include "newtmgr/newtmgr.h"
+#include "os/endian.h"
 
 /* nmgr ble mqueue */
 struct os_mqueue ble_nmgr_mq;
@@ -33,7 +34,6 @@ struct nmgr_transport ble_nt;
 /* ble nmgr attr handle */
 uint16_t g_ble_nmgr_attr_handle;
 
-struct os_eventq *app_evq;
 /**
  * The vendor specific "newtmgr" service consists of one write no-rsp
  * characteristic for newtmgr requests: a single-byte characteristic that can
@@ -193,7 +193,7 @@ nmgr_ble_out(struct nmgr_transport *nt, struct os_mbuf *m)
 {
     int rc;
 
-    rc = os_mqueue_put(&ble_nmgr_mq, app_evq, m);
+    rc = os_mqueue_put(&ble_nmgr_mq, ble_hs_cfg.parent_evq, m);
     if (rc != 0) {
         goto err;
     }
@@ -210,13 +210,11 @@ err:
  * @return 0 on success; non-zero on failure
  */
 int
-nmgr_ble_gatt_svr_init(struct os_eventq *evq, struct ble_hs_cfg *cfg)
+nmgr_ble_gatt_svr_init(void)
 {
     int rc;
 
-    assert(evq != NULL);
-
-    rc = ble_gatts_count_cfg(gatt_svr_svcs, cfg);
+    rc = ble_gatts_count_cfg(gatt_svr_svcs);
     if (rc != 0) {
         goto err;
     }
@@ -226,8 +224,6 @@ nmgr_ble_gatt_svr_init(struct os_eventq *evq, struct ble_hs_cfg *cfg)
         return rc;
     }
 
-    app_evq = evq;
-
     os_mqueue_init(&ble_nmgr_mq, &ble_nmgr_mq);
 
     rc = nmgr_transport_init(&ble_nt, &nmgr_ble_out);
@@ -235,3 +231,12 @@ nmgr_ble_gatt_svr_init(struct os_eventq *evq, struct ble_hs_cfg *cfg)
 err:
     return rc;
 }
+
+void
+newtmgr_ble_pkg_init(void)
+{
+    int rc;
+
+    rc = nmgr_ble_gatt_svr_init();
+    SYSINIT_PANIC_ASSERT(rc == 0);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/include/os/os_mbuf.h
----------------------------------------------------------------------
diff --git a/libs/os/include/os/os_mbuf.h b/libs/os/include/os/os_mbuf.h
index f3c48ae..e0733fd 100644
--- a/libs/os/include/os/os_mbuf.h
+++ b/libs/os/include/os/os_mbuf.h
@@ -245,6 +245,9 @@ void os_msys_reset(void);
 /* Return a packet header mbuf from the system pool */
 struct os_mbuf *os_msys_get_pkthdr(uint16_t dsize, uint16_t user_hdr_len);
 
+int os_msys_count(void);
+int os_msys_num_free(void);
+
 /* Initialize a mbuf pool */
 int os_mbuf_pool_init(struct os_mbuf_pool *, struct os_mempool *mp, 
         uint16_t, uint16_t);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/os/pkg.yml b/libs/os/pkg.yml
index 85133e1..d302374 100644
--- a/libs/os/pkg.yml
+++ b/libs/os/pkg.yml
@@ -24,18 +24,56 @@ pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
 
 pkg.deps:
-    - libs/testutil
+    - sys/sysinit
     - libs/util
+
 pkg.req_apis:
     - console
 
-pkg.deps.SHELL:
+pkg.deps.OS_CLI:
     - libs/shell 
-pkg.cflags.SHELL: -DSHELL_PRESENT 
 
-pkg.deps.COREDUMP:
+pkg.deps.OS_COREDUMP:
     - sys/coredump
-pkg.cflags.COREDUMP: -DCOREDUMP_PRESENT
 
-# Satisfy capability dependencies for the self-contained test executable.
-pkg.deps.SELFTEST: libs/console/stub
+pkg.init_function: os_pkg_init
+pkg.init_stage: 0
+
+pkg.syscfg_defs:
+    OS_CLI:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_LIBS_SHELL'
+    OS_COREDUMP:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_SYS_COREDUMP'
+
+    MSYS_1_BLOCK_COUNT:
+        description: 'TBD'
+        value: 12
+    MSYS_1_BLOCK_SIZE:
+        description: 'TBD'
+        value: 260
+    MSYS_2_BLOCK_COUNT:
+        description: 'TBD'
+        value: 0
+    MSYS_2_BLOCK_SIZE:
+        description: 'TBD'
+        value: 0
+    MSYS_3_BLOCK_COUNT:
+        description: 'TBD'
+        value: 0
+    MSYS_3_BLOCK_SIZE:
+        description: 'TBD'
+        value: 0
+    MSYS_4_BLOCK_COUNT:
+        description: 'TBD'
+        value: 0
+    MSYS_4_BLOCK_SIZE:
+        description: 'TBD'
+        value: 0
+    MSYS_5_BLOCK_COUNT:
+        description: 'TBD'
+        value: 0
+    MSYS_5_BLOCK_SIZE:
+        description: 'TBD'
+        value: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/arch/cortex_m0/os_fault.c
----------------------------------------------------------------------
diff --git a/libs/os/src/arch/cortex_m0/os_fault.c b/libs/os/src/arch/cortex_m0/os_fault.c
index 563f91a..4167682 100644
--- a/libs/os/src/arch/cortex_m0/os_fault.c
+++ b/libs/os/src/arch/cortex_m0/os_fault.c
@@ -17,15 +17,17 @@
  * under the License.
  */
 
-#include <console/console.h>
-#include <hal/hal_system.h>
-#ifdef COREDUMP_PRESENT
-#include <coredump/coredump.h>
+#include <stdint.h>
+#include <unistd.h>
+
+#include "syscfg/syscfg.h"
+#include "console/console.h"
+#include "hal/hal_system.h"
+#if MYNEWT_VAL(OS_COREDUMP)
+#include "coredump/coredump.h"
 #endif
 #include "os/os.h"
 
-#include <stdint.h>
-#include <unistd.h>
 
 struct exception_frame {
     uint32_t r0;
@@ -73,7 +75,7 @@ struct coredump_regs {
 
 void __assert_func(const char *file, int line, const char *func, const char *e);
 
-#ifdef COREDUMP_PRESENT
+#if MYNEWT_VAL(OS_COREDUMP)
 static void
 trap_to_coredump(struct trap_frame *tf, struct coredump_regs *regs)
 {
@@ -131,7 +133,7 @@ __assert_func(const char *file, int line, const char *func, const char *e)
 void
 os_default_irq(struct trap_frame *tf)
 {
-#ifdef COREDUMP_PRESENT
+#if MYNEWT_VAL(OS_COREDUMP)
     struct coredump_regs regs;
 #endif
 
@@ -147,7 +149,7 @@ os_default_irq(struct trap_frame *tf)
     console_printf("r12:0x%08lx  lr:0x%08lx  pc:0x%08lx psr:0x%08lx\n",
       tf->ef->r12, tf->ef->lr, tf->ef->pc, tf->ef->psr);
     console_printf("ICSR:0x%08lx\n", SCB->ICSR);
-#ifdef COREDUMP_PRESENT
+#if MYNEWT_VAL(OS_COREDUMP)
     trap_to_coredump(tf, &regs);
     coredump_dump(&regs, sizeof(regs));
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/arch/cortex_m4/os_arch_arm.c
----------------------------------------------------------------------
diff --git a/libs/os/src/arch/cortex_m4/os_arch_arm.c b/libs/os/src/arch/cortex_m4/os_arch_arm.c
index 48f6976..cef11f9 100755
--- a/libs/os/src/arch/cortex_m4/os_arch_arm.c
+++ b/libs/os/src/arch/cortex_m4/os_arch_arm.c
@@ -210,9 +210,6 @@ os_arch_os_init(void)
             NVIC_SetVector(i, (uint32_t)os_default_irq_asm);
         }
 
-        /* Call bsp related OS initializations */
-        bsp_init();
-
         /* Set the PendSV interrupt exception priority to the lowest priority */
         NVIC_SetPriority(PendSV_IRQn, PEND_SV_PRIO);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/arch/cortex_m4/os_fault.c
----------------------------------------------------------------------
diff --git a/libs/os/src/arch/cortex_m4/os_fault.c b/libs/os/src/arch/cortex_m4/os_fault.c
index a6c2b7c..d7806db 100644
--- a/libs/os/src/arch/cortex_m4/os_fault.c
+++ b/libs/os/src/arch/cortex_m4/os_fault.c
@@ -17,12 +17,13 @@
  * under the License.
  */
 
-#include <console/console.h>
-#include <hal/hal_system.h>
+#include "syscfg/syscfg.h"
+#include "console/console.h"
+#include "hal/hal_system.h"
 #include "os/os.h"
 
-#ifdef COREDUMP_PRESENT
-#include <coredump/coredump.h>
+#if MYNEWT_VAL(OS_COREDUMP)
+#include "coredump/coredump.h"
 #endif
 
 #include <stdint.h>
@@ -74,7 +75,7 @@ struct coredump_regs {
 
 void __assert_func(const char *file, int line, const char *func, const char *e);
 
-#ifdef COREDUMP_PRESENT
+#if MYNEWT_VAL(OS_COREDUMP)
 static void
 trap_to_coredump(struct trap_frame *tf, struct coredump_regs *regs)
 {
@@ -132,7 +133,7 @@ __assert_func(const char *file, int line, const char *func, const char *e)
 void
 os_default_irq(struct trap_frame *tf)
 {
-#ifdef COREDUMP_PRESENT
+#if MYNEWT_VAL(OS_COREDUMP)
     struct coredump_regs regs;
 #endif
 
@@ -151,7 +152,7 @@ os_default_irq(struct trap_frame *tf)
       SCB->ICSR, SCB->HFSR, SCB->CFSR);
     console_printf("BFAR:0x%08lx MMFAR:0x%08lx\n", SCB->BFAR, SCB->MMFAR);
 
-#ifdef COREDUMP_PRESENT
+#if MYNEWT_VAL(OS_COREDUMP)
     trap_to_coredump(tf, &regs);
     coredump_dump(&regs, sizeof(regs));
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/os.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os.c b/libs/os/src/os.c
index 961d7a0..6f47789 100644
--- a/libs/os/src/os.c
+++ b/libs/os/src/os.c
@@ -17,12 +17,14 @@
  * under the License.
  */
 
+#include "sysinit/sysinit.h"
 #include "os/os.h"
 #include "os/queue.h"
 #include "os/os_dev.h"
 #include "os_priv.h"
 
 #include "hal/hal_os_tick.h"
+#include "hal/hal_bsp.h"
 
 #include <assert.h>
 
@@ -113,11 +115,17 @@ os_init(void)
     err = os_arch_os_init();
     assert(err == OS_OK);
 
+    /* Call bsp related OS initializations */
+    bsp_init();
+
     err = (os_error_t) os_dev_initialize_all(OS_DEV_INIT_PRIMARY);
     assert(err == OS_OK);
 
     err = (os_error_t) os_dev_initialize_all(OS_DEV_INIT_SECONDARY);
     assert(err == OS_OK);
+
+    /* Initialize target-specific packages. */
+    sysinit();
 }
 
 /**
@@ -135,3 +143,9 @@ os_start(void)
     err = os_arch_os_start();
     assert(err == OS_OK);
 }
+
+void
+os_pkg_init(void)
+{
+    os_msys_init();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/os_mbuf.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_mbuf.c b/libs/os/src/os_mbuf.c
index a2302df..c7183f9 100644
--- a/libs/os/src/os_mbuf.c
+++ b/libs/os/src/os_mbuf.c
@@ -261,6 +261,33 @@ err:
     return (NULL);
 }
 
+int
+os_msys_count(void)
+{
+    struct os_mbuf_pool *omp;
+    int total;
+
+    total = 0;
+    STAILQ_FOREACH(omp, &g_msys_pool_list, omp_next) {
+        total += omp->omp_pool->mp_num_blocks;
+    }
+
+    return total;
+}
+
+int
+os_msys_num_free(void)
+{
+    struct os_mbuf_pool *omp;
+    int total;
+
+    total = 0;
+    STAILQ_FOREACH(omp, &g_msys_pool_list, omp_next) {
+        total += omp->omp_pool->mp_num_free;
+    }
+
+    return total;
+}
 
 /**
  * Initialize a pool of mbufs.

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/os_msys_init.c
----------------------------------------------------------------------
diff --git a/libs/os/src/os_msys_init.c b/libs/os/src/os_msys_init.c
new file mode 100644
index 0000000..fe0d569
--- /dev/null
+++ b/libs/os/src/os_msys_init.c
@@ -0,0 +1,147 @@
+/**
+ * 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 <assert.h>
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
+#include "os/os_mempool.h"
+#include "util/mem.h"
+#include "os_priv.h"
+
+#if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0
+#define SYSINIT_MSYS_1_MEMBLOCK_SIZE                \
+    OS_ALIGN(MYNEWT_VAL(MSYS_1_BLOCK_SIZE), 4)
+#define SYSINIT_MSYS_1_MEMPOOL_SIZE                 \
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(MSYS_1_BLOCK_COUNT),  \
+                    SYSINIT_MSYS_1_MEMBLOCK_SIZE)
+static os_membuf_t os_msys_init_1_data[SYSINIT_MSYS_1_MEMPOOL_SIZE];
+static struct os_mbuf_pool os_msys_init_1_mbuf_pool;
+static struct os_mempool os_msys_init_1_mempool;
+#endif
+
+#if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
+#define SYSINIT_MSYS_2_MEMBLOCK_SIZE                \
+    OS_ALIGN(MYNEWT_VAL(MSYS_2_BLOCK_SIZE), 4)
+#define SYSINIT_MSYS_2_MEMPOOL_SIZE                 \
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(MSYS_2_BLOCK_COUNT),  \
+                    SYSINIT_MSYS_2_MEMBLOCK_SIZE)
+static os_membuf_t os_msys_init_2_data[SYSINIT_MSYS_2_MEMPOOL_SIZE];
+static struct os_mbuf_pool os_msys_init_2_mbuf_pool;
+static struct os_mempool os_msys_init_2_mempool;
+#endif
+
+#if MYNEWT_VAL(MSYS_3_BLOCK_COUNT) > 0
+#define SYSINIT_MSYS_3_MEMBLOCK_SIZE                \
+    OS_ALIGN(MYNEWT_VAL(MSYS_3_BLOCK_SIZE), 4)
+#define SYSINIT_MSYS_3_MEMPOOL_SIZE                 \
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(MSYS_3_BLOCK_COUNT),  \
+                    SYSINIT_MSYS_3_MEMBLOCK_SIZE)
+static os_membuf_t os_msys_init_3_data[SYSINIT_MSYS_3_MEMPOOL_SIZE];
+static struct os_mbuf_pool os_msys_init_3_mbuf_pool;
+static struct os_mempool os_msys_init_3_mempool;
+#endif
+
+#if MYNEWT_VAL(MSYS_4_BLOCK_COUNT) > 0
+#define SYSINIT_MSYS_4_MEMBLOCK_SIZE                \
+    OS_ALIGN(MYNEWT_VAL(MSYS_4_BLOCK_SIZE), 4)
+#define SYSINIT_MSYS_4_MEMPOOL_SIZE                 \
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(MSYS_4_BLOCK_COUNT),  \
+                    SYSINIT_MSYS_4_MEMBLOCK_SIZE)
+static os_membuf_t os_msys_init_4_data[SYSINIT_MSYS_4_MEMPOOL_SIZE];
+static struct os_mbuf_pool os_msys_init_4_mbuf_pool;
+static struct os_mempool os_msys_init_4_mempool;
+#endif
+
+#if MYNEWT_VAL(MSYS_5_BLOCK_COUNT) > 0
+#define SYSINIT_MSYS_5_MEMBLOCK_SIZE                \
+    OS_ALIGN(MYNEWT_VAL(MSYS_5_BLOCK_SIZE), 4)
+#define SYSINIT_MSYS_5_MEMPOOL_SIZE                 \
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(MSYS_5_BLOCK_COUNT),  \
+                    SYSINIT_MSYS_5_MEMBLOCK_SIZE)
+
+static os_membuf_t os_msys_init_5_data[SYSINIT_MSYS_5_MEMPOOL_SIZE];
+static struct os_mbuf_pool os_msys_init_5_mbuf_pool;
+static struct os_mempool os_msys_init_5_mempool;
+#endif
+
+static void
+os_msys_init_once(void *data, struct os_mempool *mempool,
+                  struct os_mbuf_pool *mbuf_pool,
+                  int block_count, int block_size, char *name)
+{
+    int rc;
+
+    rc = mem_init_mbuf_pool(data, mempool, mbuf_pool, block_count, block_size,
+                            name);
+    SYSINIT_PANIC_ASSERT(rc == 0);
+
+    rc = os_msys_register(mbuf_pool);
+    SYSINIT_PANIC_ASSERT(rc == 0);
+}
+
+void
+os_msys_init(void)
+{
+    os_msys_reset();
+
+#if MYNEWT_VAL(MSYS_1_BLOCK_COUNT) > 0
+    os_msys_init_once(os_msys_init_1_data,
+                      &os_msys_init_1_mempool,
+                      &os_msys_init_1_mbuf_pool,
+                      MYNEWT_VAL(MSYS_1_BLOCK_COUNT),
+                      SYSINIT_MSYS_1_MEMBLOCK_SIZE,
+                      "msys_1");
+#endif
+
+#if MYNEWT_VAL(MSYS_2_BLOCK_COUNT) > 0
+    os_msys_init_once(os_msys_init_2_data,
+                      &os_msys_init_2_mempool,
+                      &os_msys_init_2_mbuf_pool,
+                      MYNEWT_VAL(MSYS_2_BLOCK_COUNT),
+                      SYSINIT_MSYS_2_MEMBLOCK_SIZE,
+                      "msys_2");
+#endif
+
+#if MYNEWT_VAL(MSYS_3_BLOCK_COUNT) > 0
+    os_msys_init_once(os_msys_init_3_data,
+                      &os_msys_init_3_mempool,
+                      &os_msys_init_3_mbuf_pool,
+                      MYNEWT_VAL(MSYS_3_BLOCK_COUNT),
+                      SYSINIT_MSYS_3_MEMBLOCK_SIZE,
+                      "msys_3");
+#endif
+
+#if MYNEWT_VAL(MSYS_4_BLOCK_COUNT) > 0
+    os_msys_init_once(os_msys_init_4_data,
+                      &os_msys_init_4_mempool,
+                      &os_msys_init_4_mbuf_pool,
+                      MYNEWT_VAL(MSYS_4_BLOCK_COUNT),
+                      SYSINIT_MSYS_4_MEMBLOCK_SIZE,
+                      "msys_4");
+#endif
+
+#if MYNEWT_VAL(MSYS_5_BLOCK_COUNT) > 0
+    os_msys_init_once(os_msys_init_5_data,
+                      &os_msys_init_5_mempool,
+                      &os_msys_init_5_mbuf_pool,
+                      MYNEWT_VAL(MSYS_5_BLOCK_COUNT),
+                      SYSINIT_MSYS_5_MEMBLOCK_SIZE,
+                      "msys_5");
+#endif
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/os_priv.h
----------------------------------------------------------------------
diff --git a/libs/os/src/os_priv.h b/libs/os/src/os_priv.h
index b5c8f65..7745a0a 100644
--- a/libs/os/src/os_priv.h
+++ b/libs/os/src/os_priv.h
@@ -33,4 +33,6 @@ extern struct os_task_stailq g_os_task_list;
 extern struct os_task *g_current_task;
 extern struct os_callout_list g_callout_list;
 
+void os_msys_init(void);
+
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/test/arch/cortex_m4/os_test_arch_arm.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/arch/cortex_m4/os_test_arch_arm.c b/libs/os/src/test/arch/cortex_m4/os_test_arch_arm.c
deleted file mode 100644
index 35134f7..0000000
--- a/libs/os/src/test/arch/cortex_m4/os_test_arch_arm.c
+++ /dev/null
@@ -1,27 +0,0 @@
-/**
- * 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 "testutil/testutil.h"
-#include "os_test_priv.h"
-
-void
-os_test_restart(void)
-{
-    tu_restart();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/test/arch/sim/os_test_arch_sim.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/arch/sim/os_test_arch_sim.c b/libs/os/src/test/arch/sim/os_test_arch_sim.c
deleted file mode 100644
index 3b6cfbf..0000000
--- a/libs/os/src/test/arch/sim/os_test_arch_sim.c
+++ /dev/null
@@ -1,52 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <setjmp.h>
-#include <signal.h>
-#include <string.h>
-#include <sys/time.h>
-#include "testutil/testutil.h"
-#include "os/os.h"
-#include "os_test_priv.h"
-
-void
-os_test_restart(void)
-{
-    struct sigaction sa;
-    struct itimerval it;
-    int rc;
-
-    g_os_started = 0;
-
-    memset(&sa, 0, sizeof sa);
-    sa.sa_handler = SIG_IGN;
-
-    sigaction(SIGALRM, &sa, NULL);
-    sigaction(SIGVTALRM, &sa, NULL);
-
-    memset(&it, 0, sizeof(it));
-    rc = setitimer(ITIMER_VIRTUAL, &it, NULL);
-    if (rc != 0) {
-        perror("Cannot set itimer");
-        abort();
-    }
-
-    tu_restart();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/test/eventq_test.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/eventq_test.c b/libs/os/src/test/eventq_test.c
deleted file mode 100644
index cb1ed94..0000000
--- a/libs/os/src/test/eventq_test.c
+++ /dev/null
@@ -1,416 +0,0 @@
-/**
- * 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 <string.h>
-#include "testutil/testutil.h"
-#include "os/os.h"
-#include "os_test_priv.h"
-#include "os/os_eventq.h"
-
-#define MY_STACK_SIZE        (5120)
-#define POLL_STACK_SIZE        (4096)
-/* Task 1 sending task */
-/* Define task stack and task object */
-#define SEND_TASK_PRIO        (1)
-struct os_task eventq_task_s;
-os_stack_t eventq_task_stack_s[MY_STACK_SIZE];
-
-/* Task 2 receiving task */
-#define RECEIVE_TASK_PRIO     (2)
-struct os_task eventq_task_r;
-os_stack_t eventq_task_stack_r[MY_STACK_SIZE];
-
-struct os_eventq my_eventq;
-
-#define SIZE_MULTI_EVENT        (4)
-struct os_eventq multi_eventq[SIZE_MULTI_EVENT];
-
-/* This is to set the events we will use below */
-struct os_event g_event;
-struct os_event m_event[SIZE_MULTI_EVENT];
-
-/* Setting the event to send and receive multiple data */
-uint8_t my_event_type = 1;
-
-/* Setting up data for the poll */
-/* Define the task stack for the eventq_task_poll_send */
-#define SEND_TASK_POLL_PRIO        (3)
-struct os_task eventq_task_poll_s;
-os_stack_t eventq_task_stack_poll_s[POLL_STACK_SIZE];
-
-/* Define the task stack for the eventq_task_poll_receive */
-#define RECEIVE_TASK_POLL_PRIO     (4)
-struct os_task eventq_task_poll_r;
-os_stack_t eventq_task_stack_poll_r[POLL_STACK_SIZE ];
-
-/* Setting the data for the poll timeout */
-/* Define the task stack for the eventq_task_poll_timeout_send */
-#define SEND_TASK_POLL_TIMEOUT_PRIO        (5)
-struct os_task eventq_task_poll_timeout_s;
-os_stack_t eventq_task_stack_poll_timeout_s[POLL_STACK_SIZE];
-
-/* Define the task stack for the eventq_task_poll_receive */
-#define RECEIVE_TASK_POLL_TIMEOUT_PRIO     (6)
-struct os_task eventq_task_poll_timeout_r;
-os_stack_t eventq_task_stack_poll_timeout_r[POLL_STACK_SIZE];
-
-/* Setting the data for the poll single */
-/* Define the task stack for the eventq_task_poll_single_send */
-#define SEND_TASK_POLL_SINGLE_PRIO        (7)
-struct os_task eventq_task_poll_single_s;
-os_stack_t eventq_task_stack_poll_single_s[POLL_STACK_SIZE];
-
-/* Define the task stack for the eventq_task_poll_single_receive */
-#define RECEIVE_TASK_POLL_SINGLE_PRIO     (8)
-struct os_task eventq_task_poll_single_r;
-os_stack_t eventq_task_stack_poll_single_r[POLL_STACK_SIZE];
-
-/* This is the task function  to send data */
-void
-eventq_task_send(void *arg)
-{
-    int i;
-
-    g_event.ev_queued = 0;
-    g_event.ev_type = my_event_type;
-    g_event.ev_arg = NULL;
-
-    os_eventq_put(&my_eventq, &g_event);
-
-    os_time_delay(OS_TICKS_PER_SEC / 2);
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        m_event[i].ev_type = i + 2;
-        m_event[i].ev_arg = NULL;
-
-        /* Put and send */
-        os_eventq_put(&multi_eventq[i], &m_event[i]);
-        os_time_delay(OS_TICKS_PER_SEC / 2);
-    }
-
-    /* This task sleeps until the receive task completes the test. */
-    os_time_delay(1000000);
-}
-
-/* This is the task function is the receiving function */
-void
-eventq_task_receive(void *arg)
-{
-    struct os_event *event;
-    int i;
-
-    event = os_eventq_get(&my_eventq);
-    TEST_ASSERT(event->ev_type == my_event_type);
-
-    /* Receiving multi event from the send task */
-    for (i = 0; i < SIZE_MULTI_EVENT; i++) {
-        event = os_eventq_get(&multi_eventq[i]);
-        TEST_ASSERT(event->ev_type == i + 2);
-    }
-
-    /* Finishes the test when OS has been started */
-    os_test_restart();
-}
-
-void
-eventq_task_poll_send(void *arg)
-{
-    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
-    int i;
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        eventqs[i] = &multi_eventq[i];
-    }
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        m_event[i].ev_type = i + 10;
-        m_event[i].ev_arg = NULL;
-
-        /* Put and send */
-        os_eventq_put(eventqs[i], &m_event[i]);
-        os_time_delay(OS_TICKS_PER_SEC / 2);
-    }
-
-    /* This task sleeps until the receive task completes the test. */
-    os_time_delay(1000000);
-}
-
-void
-eventq_task_poll_receive(void *arg)
-{
-    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
-    struct os_event *event;
-    int i;
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        eventqs[i] = &multi_eventq[i];
-    }
-
-    /* Recieving using the os_eventq_poll*/
-    for (i = 0; i < SIZE_MULTI_EVENT; i++) {
-        event = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, OS_WAIT_FOREVER);
-        TEST_ASSERT(event->ev_type == i +10);
-    }
-
-    /* Finishes the test when OS has been started */
-    os_test_restart();
-
-}
-
-/* Sending with a time failure */
-void
-eventq_task_poll_timeout_send(void *arg)
-{
-    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
-    int i;
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        eventqs[i] = &multi_eventq[i];
-    }
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-         os_time_delay(1000);
-
-        /* Put and send */
-        os_eventq_put(eventqs[i], &m_event[i]);
-        os_time_delay(OS_TICKS_PER_SEC / 2);
-    }
-
-    /* This task sleeps until the receive task completes the test. */
-    os_time_delay(1000000);
-    
-}
-
-/* Receiving multiple event queues with a time failure */
-void
-eventq_task_poll_timeout_receive(void *arg)
-{
-    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
-    struct os_event *event;
-    int i;
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        eventqs[i] = &multi_eventq[i];
-    }
-
-    /* Recieving using the os_eventq_poll_timeout*/
-    for (i = 0; i < SIZE_MULTI_EVENT; i++) {
-        event = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, 200);
-        TEST_ASSERT(event == NULL);
-    }
-
-    /* Finishes the test when OS has been started */
-    os_test_restart();
-
-}
-
-/* Sending a single event to poll */
-void
-eventq_task_poll_single_send(void *arg)
-{
-    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
-    int i;
-    int position = 2;
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        eventqs[i] = &multi_eventq[i];
-    }
-
-    /* Put and send */
-    os_eventq_put(eventqs[position], &m_event[position]);
-    os_time_delay(OS_TICKS_PER_SEC / 2);
-
-    /* This task sleeps until the receive task completes the test. */
-    os_time_delay(1000000);
-}
-
-/* Recieving the single event */
-void
-eventq_task_poll_single_receive(void *arg)
-{
-    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
-    struct os_event *event;
-    int i;
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        eventqs[i] = &multi_eventq[i];
-    }
-
-    /* Recieving using the os_eventq_poll*/
-    event = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, OS_WAIT_FOREVER);
-    TEST_ASSERT(event->ev_type == 20);
-
-    /* Finishes the test when OS has been started */
-    os_test_restart();
-}
-
-TEST_CASE(event_test_sr)
-{
-    int i;
-
-    /* Initializing the OS */
-    os_init();
-    /* Initialize the task */
-    os_task_init(&eventq_task_s, "eventq_task_s", eventq_task_send, NULL,
-        SEND_TASK_PRIO, OS_WAIT_FOREVER, eventq_task_stack_s, MY_STACK_SIZE);
-
-    /* Receive events and check whether the eevnts are correctly received */
-    os_task_init(&eventq_task_r, "eventq_task_r", eventq_task_receive, NULL,
-        RECEIVE_TASK_PRIO, OS_WAIT_FOREVER, eventq_task_stack_r,
-        MY_STACK_SIZE);
-
-    os_eventq_init(&my_eventq);
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        os_eventq_init(&multi_eventq[i]);
-    }
-
-    /* Does not return until OS_restart is called */
-    os_start();
-
-}
-
-/* To test for the basic function of os_eventq_poll() */
-TEST_CASE(event_test_poll_sr)
-{
-    int i;
-
-    /* Initializing the OS */
-    os_init();
-    /* Initialize the task */
-    os_task_init(&eventq_task_poll_s, "eventq_task_poll_s", eventq_task_poll_send,
-        NULL, SEND_TASK_POLL_PRIO, OS_WAIT_FOREVER, eventq_task_stack_poll_s, 
-        POLL_STACK_SIZE);
-
-    /* Receive events and check whether the eevnts are correctly received */
-    os_task_init(&eventq_task_poll_r, "eventq_task_r", eventq_task_poll_receive,
-        NULL, RECEIVE_TASK_POLL_PRIO, OS_WAIT_FOREVER, eventq_task_stack_poll_r,
-        POLL_STACK_SIZE);
-
-    /* Initializing the eventqs. */
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        os_eventq_init(&multi_eventq[i]);
-    }
-
-    /* Does not return until OS_restart is called */
-    os_start();
-
-}
-
-/* Test case for poll timeout */
-TEST_CASE(event_test_poll_timeout_sr)
-{
-    int i;
-
-    /* Initializing the OS */
-    os_init();
-    /* Initialize the task */
-    os_task_init(&eventq_task_poll_timeout_s, "eventq_task_poll_timeout_s", 
-        eventq_task_poll_timeout_send, NULL, SEND_TASK_POLL_TIMEOUT_PRIO,
-        OS_WAIT_FOREVER, eventq_task_stack_poll_timeout_s, POLL_STACK_SIZE);
-
-    /* Receive events and check whether the eevnts are correctly received */
-    os_task_init(&eventq_task_poll_timeout_r, "eventq_task_timeout_r",
-        eventq_task_poll_timeout_receive, NULL, RECEIVE_TASK_POLL_TIMEOUT_PRIO,
-        OS_WAIT_FOREVER, eventq_task_stack_poll_timeout_r, POLL_STACK_SIZE);
-
-    /* Initializing the eventqs. */
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        os_eventq_init(&multi_eventq[i]);
-
-        m_event[i].ev_type = i + 10;
-        m_event[i].ev_arg = NULL;
-    }
-
-    /* Does not return until OS_restart is called */
-    os_start();
-
-}
-
-/* The case for poll single */
-/* Test case for poll timeout */
-TEST_CASE(event_test_poll_single_sr)
-{
-    int i;
-
-    /* Initializing the OS */
-    os_init();
-    /* Initialize the task */
-    os_task_init(&eventq_task_poll_single_s, "eventq_task_poll_single_s", 
-        eventq_task_poll_single_send, NULL, SEND_TASK_POLL_SINGLE_PRIO,
-        OS_WAIT_FOREVER, eventq_task_stack_poll_single_s, POLL_STACK_SIZE);
-
-    /* Receive events and check whether the eevnts are correctly received */
-    os_task_init(&eventq_task_poll_single_r, "eventq_task_single_r",
-        eventq_task_poll_single_receive, NULL, RECEIVE_TASK_POLL_SINGLE_PRIO,
-        OS_WAIT_FOREVER, eventq_task_stack_poll_single_r, POLL_STACK_SIZE);
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        os_eventq_init(&multi_eventq[i]);
-
-        m_event[i].ev_type = 10 * i;
-        m_event[i].ev_arg = NULL;
-    }
-
-    /* Does not return until OS_restart is called */
-    os_start();
-
-}
-
-/**
- * Tests eventq_poll() with a timeout of 0.  This should not involve the
- * scheduler at all, so it should work without starting the OS.
- */
-TEST_CASE(event_test_poll_0timo)
-{
-    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
-    struct os_event *evp;
-    struct os_event ev;
-    int i;
-
-    for (i = 0; i < SIZE_MULTI_EVENT; i++){
-        os_eventq_init(&multi_eventq[i]);
-        eventqs[i] = &multi_eventq[i];
-    }
-
-    evp = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, 0);
-    TEST_ASSERT(evp == NULL);
-
-    /* Ensure no eventq thinks a task is waiting on it. */
-    for (i = 0; i < SIZE_MULTI_EVENT; i++) {
-        TEST_ASSERT(eventqs[i]->evq_task == NULL);
-    }
-
-    /* Put an event on one of the queues. */
-    memset(&ev, 0, sizeof ev);
-    ev.ev_type = 1;
-    os_eventq_put(eventqs[3], &ev);
-
-    evp = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, 0);
-    TEST_ASSERT(evp == &ev);
-}
-
-TEST_SUITE(os_eventq_test_suite)
-{
-    event_test_sr();
-    event_test_poll_sr();
-    event_test_poll_timeout_sr();
-    event_test_poll_single_sr();
-    event_test_poll_0timo();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/test/mbuf_test.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/mbuf_test.c b/libs/os/src/test/mbuf_test.c
deleted file mode 100644
index dd4121d..0000000
--- a/libs/os/src/test/mbuf_test.c
+++ /dev/null
@@ -1,420 +0,0 @@
-/**
- * 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 "testutil/testutil.h"
-#include "os/os.h"
-#include "os_test_priv.h"
-
-#include <string.h>
-
-/* 
- * NOTE: currently, the buffer size cannot be changed as some tests are
- * hard-coded for this size.
- */
-#define MBUF_TEST_POOL_BUF_SIZE     (256)
-#define MBUF_TEST_POOL_BUF_COUNT    (10)
-
-#define MBUF_TEST_DATA_LEN          (1024)
-
-static os_membuf_t os_mbuf_membuf[OS_MEMPOOL_SIZE(MBUF_TEST_POOL_BUF_SIZE,
-        MBUF_TEST_POOL_BUF_COUNT)];
-
-static struct os_mbuf_pool os_mbuf_pool;
-static struct os_mempool os_mbuf_mempool;
-static uint8_t os_mbuf_test_data[MBUF_TEST_DATA_LEN];
-
-static void
-os_mbuf_test_setup(void)
-{
-    int rc;
-    int i;
-
-    rc = os_mempool_init(&os_mbuf_mempool, MBUF_TEST_POOL_BUF_COUNT,
-            MBUF_TEST_POOL_BUF_SIZE, &os_mbuf_membuf[0], "mbuf_pool");
-    TEST_ASSERT_FATAL(rc == 0, "Error creating memory pool %d", rc);
-
-    rc = os_mbuf_pool_init(&os_mbuf_pool, &os_mbuf_mempool,
-            MBUF_TEST_POOL_BUF_SIZE, MBUF_TEST_POOL_BUF_COUNT);
-    TEST_ASSERT_FATAL(rc == 0, "Error creating mbuf pool %d", rc);
-
-    for (i = 0; i < sizeof os_mbuf_test_data; i++) {
-        os_mbuf_test_data[i] = i;
-    }
-}
-
-static void
-os_mbuf_test_misc_assert_sane(struct os_mbuf *om, void *data,
-                              int buflen, int pktlen, int pkthdr_len)
-{
-    uint8_t *data_min;
-    uint8_t *data_max;
-    int totlen;
-    int i;
-
-    TEST_ASSERT_FATAL(om != NULL);
-
-    if (OS_MBUF_IS_PKTHDR(om)) {
-        TEST_ASSERT(OS_MBUF_PKTLEN(om) == pktlen);
-    }
-
-    totlen = 0;
-    for (i = 0; om != NULL; i++) {
-        if (i == 0) {
-            TEST_ASSERT(om->om_len == buflen);
-            TEST_ASSERT(om->om_pkthdr_len == pkthdr_len);
-        }
-
-        data_min = om->om_databuf + om->om_pkthdr_len;
-        data_max = om->om_databuf + om->om_omp->omp_databuf_len - om->om_len;
-        TEST_ASSERT(om->om_data >= data_min && om->om_data <= data_max);
-
-        if (data != NULL) {
-            TEST_ASSERT(memcmp(om->om_data, data + totlen, om->om_len) == 0);
-        }
-
-        totlen += om->om_len;
-        om = SLIST_NEXT(om, om_next);
-    }
-
-    TEST_ASSERT(totlen == pktlen);
-}
-
-
-TEST_CASE(os_mbuf_test_alloc)
-{
-    struct os_mbuf *m;
-    int rc;
-
-    os_mbuf_test_setup();
-
-    m = os_mbuf_get(&os_mbuf_pool, 0);
-    TEST_ASSERT_FATAL(m != NULL, "Error allocating mbuf");
-
-    rc = os_mbuf_free(m);
-    TEST_ASSERT_FATAL(rc == 0, "Error free'ing mbuf %d", rc);
-}
-
-TEST_CASE(os_mbuf_test_get_pkthdr)
-{
-    struct os_mbuf *m;
- 
-    os_mbuf_test_setup();
-
-#if (MBUF_TEST_POOL_BUF_SIZE <= 256)
-    m = os_mbuf_get_pkthdr(&os_mbuf_pool, MBUF_TEST_POOL_BUF_SIZE - 1);
-    TEST_ASSERT_FATAL(m == NULL, "Error: should not have returned mbuf");
-#endif
-
-    m = os_mbuf_get(&os_mbuf_pool, MBUF_TEST_POOL_BUF_SIZE);
-    TEST_ASSERT_FATAL(m == NULL, "Error: should not have returned mbuf");
-}
-
-
-TEST_CASE(os_mbuf_test_dup)
-{
-    struct os_mbuf *om;
-    struct os_mbuf *om2;
-    struct os_mbuf *dup;
-    int rc;
-
-    os_mbuf_test_setup();
-
-    /* Test first allocating and duplicating a single mbuf */
-    om = os_mbuf_get(&os_mbuf_pool, 0);
-    TEST_ASSERT_FATAL(om != NULL, "Error allocating mbuf");
-
-    rc = os_mbuf_append(om, os_mbuf_test_data, 200);
-    TEST_ASSERT_FATAL(rc == 0);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 200, 200, 0);
-
-    dup = os_mbuf_dup(om);
-    TEST_ASSERT_FATAL(dup != NULL, "NULL mbuf returned from dup");
-    TEST_ASSERT_FATAL(dup != om, "duplicate matches original.");
-    os_mbuf_test_misc_assert_sane(dup, os_mbuf_test_data, 200, 200, 0);
-
-    rc = os_mbuf_free(om);
-    TEST_ASSERT_FATAL(rc == 0, "Error free'ing mbuf om %d", rc);
-
-    rc = os_mbuf_free(dup);
-    TEST_ASSERT_FATAL(rc == 0, "Error free'ing mbuf dup %d", rc);
-
-    om = os_mbuf_get(&os_mbuf_pool, 0);
-    TEST_ASSERT_FATAL(om != NULL, "Error allocating mbuf");
-    rc = os_mbuf_append(om, os_mbuf_test_data, 200);
-    TEST_ASSERT_FATAL(rc == 0);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 200, 200, 0);
-
-    om2 = os_mbuf_get(&os_mbuf_pool, 0);
-    TEST_ASSERT_FATAL(om2 != NULL, "Error allocating mbuf");
-    rc = os_mbuf_append(om2, os_mbuf_test_data + 200, 200);
-    TEST_ASSERT_FATAL(rc == 0);
-    os_mbuf_test_misc_assert_sane(om2, os_mbuf_test_data + 200, 200, 200, 0);
-
-    os_mbuf_concat(om, om2);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 200, 400, 0);
-
-    dup = os_mbuf_dup(om);
-    TEST_ASSERT_FATAL(dup != NULL, "NULL mbuf returned from dup");
-    TEST_ASSERT_FATAL(dup != om, "Duplicate matches original");
-    TEST_ASSERT_FATAL(SLIST_NEXT(dup, om_next) != NULL,
-            "NULL chained element, duplicate should match original");
-
-    os_mbuf_test_misc_assert_sane(dup, os_mbuf_test_data, 200, 400, 0);
-
-    rc = os_mbuf_free_chain(om);
-    TEST_ASSERT_FATAL(rc == 0, "Cannot free mbuf chain %d", rc);
-
-    rc = os_mbuf_free_chain(dup);
-    TEST_ASSERT_FATAL(rc == 0, "Cannot free mbuf chain %d", rc);
-}
-
-TEST_CASE(os_mbuf_test_append)
-{
-    struct os_mbuf *om;
-    int rc;
-    uint8_t databuf[] = {0xa, 0xb, 0xc, 0xd};
-    uint8_t cmpbuf[] = {0xff, 0xff, 0xff, 0xff};
-
-    os_mbuf_test_setup();
-
-    om = os_mbuf_get(&os_mbuf_pool, 0);
-    TEST_ASSERT_FATAL(om != NULL, "Error allocating mbuf");
-    os_mbuf_test_misc_assert_sane(om, NULL, 0, 0, 0);
-
-    rc = os_mbuf_append(om, databuf, sizeof(databuf));
-    TEST_ASSERT_FATAL(rc == 0, "Cannot add %d bytes to mbuf",
-            sizeof(databuf));
-    os_mbuf_test_misc_assert_sane(om, databuf, sizeof databuf, sizeof databuf,
-                                  0);
-
-    memcpy(cmpbuf, OS_MBUF_DATA(om, uint8_t *), om->om_len);
-    TEST_ASSERT_FATAL(memcmp(cmpbuf, databuf, sizeof(databuf)) == 0,
-            "Databuf doesn't match cmpbuf");
-}
-
-TEST_CASE(os_mbuf_test_extend)
-{
-    struct os_mbuf *om;
-    void *v;
-
-    os_mbuf_test_setup();
-
-    /*** Series of successful extensions. */
-    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
-    TEST_ASSERT_FATAL(om != NULL);
-
-    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 222);
-    TEST_ASSERT(SLIST_NEXT(om, om_next) == NULL);
-    os_mbuf_test_misc_assert_sane(om, NULL, 0, 0, 18);
-
-    v = os_mbuf_extend(om, 20);
-    TEST_ASSERT(v != NULL);
-    TEST_ASSERT(v == om->om_data);
-    TEST_ASSERT(om->om_len == 20);
-
-    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 202);
-    TEST_ASSERT(SLIST_NEXT(om, om_next) == NULL);
-    os_mbuf_test_misc_assert_sane(om, NULL, 20, 20, 18);
-
-    v = os_mbuf_extend(om, 100);
-    TEST_ASSERT(v != NULL);
-    TEST_ASSERT(v == om->om_data + 20);
-    TEST_ASSERT(om->om_len == 120);
-
-    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 102);
-    TEST_ASSERT(SLIST_NEXT(om, om_next) == NULL);
-    os_mbuf_test_misc_assert_sane(om, NULL, 120, 120, 18);
-
-    v = os_mbuf_extend(om, 101);
-    TEST_ASSERT(v != NULL);
-    TEST_ASSERT(v == om->om_data + 120);
-    TEST_ASSERT(om->om_len == 221);
-
-    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 1);
-    TEST_ASSERT(SLIST_NEXT(om, om_next) == NULL);
-    os_mbuf_test_misc_assert_sane(om, NULL, 221, 221, 18);
-
-    v = os_mbuf_extend(om, 1);
-    TEST_ASSERT(v != NULL);
-    TEST_ASSERT(v == om->om_data + 221);
-    TEST_ASSERT(om->om_len == 222);
-
-    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 0);
-    TEST_ASSERT(SLIST_NEXT(om, om_next) == NULL);
-    os_mbuf_test_misc_assert_sane(om, NULL, 222, 222, 18);
-
-    /* Overflow into next buffer. */
-    v = os_mbuf_extend(om, 1);
-    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 0);
-    TEST_ASSERT(SLIST_NEXT(om, om_next) != NULL);
-
-    TEST_ASSERT(v == SLIST_NEXT(om, om_next)->om_data);
-    TEST_ASSERT(om->om_len == 222);
-    TEST_ASSERT(SLIST_NEXT(om, om_next)->om_len == 1);
-    os_mbuf_test_misc_assert_sane(om, NULL, 222, 223, 18);
-
-    /*** Attempt to extend by an amount larger than max buf size fails. */
-    v = os_mbuf_extend(om, 257);
-    TEST_ASSERT(v == NULL);
-    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 0);
-    TEST_ASSERT(SLIST_NEXT(om, om_next) != NULL);
-
-    TEST_ASSERT(om->om_len == 222);
-    TEST_ASSERT(SLIST_NEXT(om, om_next)->om_len == 1);
-    os_mbuf_test_misc_assert_sane(om, NULL, 222, 223, 18);
-}
-
-TEST_CASE(os_mbuf_test_pullup)
-{
-    struct os_mbuf *om;
-    struct os_mbuf *om2;
-    int rc;
-
-    os_mbuf_test_setup();
-
-    /*** Free when too much os_mbuf_test_data is requested. */
-    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
-    TEST_ASSERT_FATAL(om != NULL);
-
-    om = os_mbuf_pullup(om, 1);
-    TEST_ASSERT(om == NULL);
-
-    /*** No effect when all os_mbuf_test_data is already at the start. */
-    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
-    TEST_ASSERT_FATAL(om != NULL);
-
-    rc = os_mbuf_append(om, os_mbuf_test_data, 1);
-    TEST_ASSERT_FATAL(rc == 0);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 1, 1, 18);
-
-    om = os_mbuf_pullup(om, 1);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 1, 1, 18);
-
-    /*** Spread os_mbuf_test_data across four mbufs. */
-    om2 = os_mbuf_get(&os_mbuf_pool, 10);
-    TEST_ASSERT_FATAL(om2 != NULL);
-    rc = os_mbuf_append(om2, os_mbuf_test_data + 1, 1);
-    TEST_ASSERT_FATAL(rc == 0);
-    os_mbuf_concat(om, om2);
-
-    om2 = os_mbuf_get(&os_mbuf_pool, 10);
-    TEST_ASSERT_FATAL(om2 != NULL);
-    rc = os_mbuf_append(om2, os_mbuf_test_data + 2, 1);
-    TEST_ASSERT_FATAL(rc == 0);
-    os_mbuf_concat(om, om2);
-
-    om2 = os_mbuf_get(&os_mbuf_pool, 10);
-    TEST_ASSERT_FATAL(om2 != NULL);
-    rc = os_mbuf_append(om2, os_mbuf_test_data + 3, 1);
-    TEST_ASSERT_FATAL(rc == 0);
-    os_mbuf_concat(om, om2);
-
-    TEST_ASSERT_FATAL(OS_MBUF_PKTLEN(om) == 4);
-
-    om = os_mbuf_pullup(om, 4);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 4, 4, 18);
-
-    os_mbuf_free_chain(om);
-
-    /*** Require an allocation. */
-    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
-    TEST_ASSERT_FATAL(om != NULL);
-
-    om->om_data += 100;
-    rc = os_mbuf_append(om, os_mbuf_test_data, 100);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    om2 = os_mbuf_get(&os_mbuf_pool, 10);
-    TEST_ASSERT_FATAL(om2 != NULL);
-    rc = os_mbuf_append(om2, os_mbuf_test_data + 100, 100);
-    TEST_ASSERT_FATAL(rc == 0);
-    os_mbuf_concat(om, om2);
-
-    om = os_mbuf_pullup(om, 200);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 200, 200, 18);
-
-    /*** Partial pullup. */
-    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
-    TEST_ASSERT_FATAL(om != NULL);
-
-    om->om_data += 100;
-    rc = os_mbuf_append(om, os_mbuf_test_data, 100);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    om2 = os_mbuf_get(&os_mbuf_pool, 10);
-    TEST_ASSERT_FATAL(om2 != NULL);
-    rc = os_mbuf_append(om2, os_mbuf_test_data + 100, 100);
-    TEST_ASSERT_FATAL(rc == 0);
-    os_mbuf_concat(om, om2);
-
-    om = os_mbuf_pullup(om, 150);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 150, 200, 18);
-}
-
-TEST_CASE(os_mbuf_test_adj)
-{
-    struct os_mbuf *om;
-    int rc;
-
-    os_mbuf_test_setup();
-
-    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
-    TEST_ASSERT_FATAL(om != NULL);
-
-    rc = os_mbuf_append(om, os_mbuf_test_data, sizeof os_mbuf_test_data);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 222,
-                                  sizeof os_mbuf_test_data, 18);
-
-    /* Remove from the front. */
-    os_mbuf_adj(om, 10);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data + 10, 212,
-                                  sizeof os_mbuf_test_data - 10, 18);
-
-    /* Remove from the back. */
-    os_mbuf_adj(om, -10);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data + 10, 212,
-                                  sizeof os_mbuf_test_data - 20, 18);
-
-    /* Remove entire first buffer. */
-    os_mbuf_adj(om, 212);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data + 222, 0,
-                                  sizeof os_mbuf_test_data - 232, 18);
-
-    /* Remove next buffer. */
-    os_mbuf_adj(om, 256);
-    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data + 478, 0,
-                                  sizeof os_mbuf_test_data - 488, 18);
-
-    /* Remove more data than is present. */
-    os_mbuf_adj(om, 1000);
-    os_mbuf_test_misc_assert_sane(om, NULL, 0, 0, 18);
-}
-
-TEST_SUITE(os_mbuf_test_suite)
-{
-    os_mbuf_test_alloc();
-    os_mbuf_test_dup();
-    os_mbuf_test_append();
-    os_mbuf_test_pullup();
-    os_mbuf_test_extend();
-    os_mbuf_test_adj();
-    os_mbuf_test_get_pkthdr();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/test/mempool_test.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/mempool_test.c b/libs/os/src/test/mempool_test.c
deleted file mode 100644
index cd17c90..0000000
--- a/libs/os/src/test/mempool_test.c
+++ /dev/null
@@ -1,227 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <string.h>
-#include "testutil/testutil.h"
-#include "os/os.h"
-#include "os_test_priv.h"
-
-/* Create a memory pool for testing */
-#define NUM_MEM_BLOCKS  (10)
-#define MEM_BLOCK_SIZE  (80)
-
-/* Limit max blocks for testing */
-#define MEMPOOL_TEST_MAX_BLOCKS     (128)
-
-#if OS_CFG_ALIGNMENT == OS_CFG_ALIGN_4
-int alignment = 4;
-#else
-int alignment = 8;
-#endif
-
-/* Test memory pool structure */
-struct os_mempool g_TstMempool;
-
-/* Test memory pool buffer */
-os_membuf_t TstMembuf[OS_MEMPOOL_SIZE(NUM_MEM_BLOCKS, MEM_BLOCK_SIZE)];
-
-/* Array of block pointers. */
-void *block_array[MEMPOOL_TEST_MAX_BLOCKS];
-
-int verbose = 0;
-
-static int
-mempool_test_get_pool_size(int num_blocks, int block_size)
-{
-    int mem_pool_size;
-
-#if OS_CFG_ALIGNMENT == OS_CFG_ALIGN_4
-    mem_pool_size = (num_blocks * ((block_size + 3)/4) * sizeof(os_membuf_t));
-#else
-    mem_pool_size = (num_blocks * ((block_size + 7)/8) * sizeof(os_membuf_t));
-#endif
-
-    return mem_pool_size;
-}
-
-static void
-mempool_test(int num_blocks, int block_size)
-{
-    int cnt;
-    int true_block_size;
-    int mem_pool_size;
-    uint32_t test_block;
-    uint8_t *tstptr;
-    void **free_ptr;
-    void *block;
-    os_error_t rc;
-
-    /* Check for too many blocks */
-    TEST_ASSERT(num_blocks <= MEMPOOL_TEST_MAX_BLOCKS);
-
-    rc = os_mempool_init(&g_TstMempool, num_blocks, MEM_BLOCK_SIZE, 
-                         &TstMembuf[0], "TestMemPool");
-    TEST_ASSERT_FATAL(rc == 0, "Error creating memory pool %d", rc);
-
-    TEST_ASSERT(g_TstMempool.mp_num_free == num_blocks,
-                "Number of free blocks not equal to total blocks!");
-
-    TEST_ASSERT(SLIST_FIRST(&g_TstMempool) == (void *)&TstMembuf[0],
-                "Free list pointer does not point to first block!");
-
-    mem_pool_size = mempool_test_get_pool_size(num_blocks, block_size);
-    TEST_ASSERT(mem_pool_size == sizeof(TstMembuf),
-                "Total memory pool size not correct! (%d vs %lu)",
-                mem_pool_size, (unsigned long)sizeof(TstMembuf));
-
-    /* Get the real block size */
-#if (OS_CFG_ALIGNMENT == OS_CFG_ALIGN_4)
-    true_block_size = (g_TstMempool.mp_block_size + 3) & ~3;
-#else
-    true_block_size = (g_TstMempool.mp_block_size + 7) & ~7;
-#endif
-
-    /* Traverse free list. Better add up to number of blocks! */
-    cnt = 0;
-    free_ptr = (void **)&TstMembuf;
-    tstptr = (uint8_t *)&TstMembuf;
-    while (1) {
-        /* Increment # of elements by 1 */
-        ++cnt;
-
-        /* If the free list is NULL, leave */
-        if (*free_ptr == NULL) {
-            break;
-        }
-
-        TEST_ASSERT(((uint8_t *)*free_ptr - (uint8_t *)free_ptr) ==
-                        true_block_size,
-                    "Free pointers are more than one block apart!");
-
-        /* Move to next memory block */
-        tstptr += true_block_size;
-
-        TEST_ASSERT(*free_ptr == (void *)tstptr,
-                    "Error: free_ptr=%p testptr=%p\n", *free_ptr, tstptr);
-
-        free_ptr = *free_ptr;
-    }
-
-    /* Last one in list better be NULL */
-    TEST_ASSERT(cnt == g_TstMempool.mp_num_blocks,
-                "Free list contains too many elements (%u)", cnt);
-
-    /* Get a block */
-    block = os_memblock_get(&g_TstMempool);
-    TEST_ASSERT(block != NULL,
-                "Error: get block fails when pool should have elements");
-
-    TEST_ASSERT(g_TstMempool.mp_num_free == (num_blocks-1),
-                "Number of free blocks incorrect (%u vs %u)",
-                g_TstMempool.mp_num_free, (num_blocks-1));
-
-    /* Put back the block */
-    rc = os_memblock_put(&g_TstMempool, block);
-    TEST_ASSERT(rc == 0, "Put block fails with error code=%d\n", rc);
-
-    TEST_ASSERT(g_TstMempool.mp_num_free == num_blocks,
-                "Number of free blocks incorrect (%u vs %u)",
-                g_TstMempool.mp_num_free, num_blocks);
-
-    /* remove all the blocks. Make sure we get count. */
-    memset(block_array, 0, sizeof(block_array));
-    cnt = 0;
-    while (1) {
-        block = os_memblock_get(&g_TstMempool);
-        if (block == NULL) {
-            break;
-        }
-        block_array[cnt] = block;
-        ++cnt;
-        if (cnt == MEMPOOL_TEST_MAX_BLOCKS) {
-            break;
-        }
-    }
-
-    TEST_ASSERT((cnt == g_TstMempool.mp_num_blocks) && 
-                (cnt != MEMPOOL_TEST_MAX_BLOCKS),
-                "Got more blocks than mempool contains (%d vs %d)",
-                cnt, g_TstMempool.mp_num_blocks);
-
-    /* Better be no free blocks left! */
-    TEST_ASSERT(g_TstMempool.mp_num_free == 0,
-                "Got all blocks but number free not zero! (%d)",
-                g_TstMempool.mp_num_free);
-
-    /* Now put them all back */
-    for (cnt = 0; cnt < g_TstMempool.mp_num_blocks; ++cnt) {
-        rc = os_memblock_put(&g_TstMempool, block_array[cnt]);
-        TEST_ASSERT(rc == 0,
-                    "Error putting back block %p (cnt=%d err=%d)", 
-                    block_array[cnt], cnt, rc);
-    }
-
-    /* Better be no free blocks left! */
-    TEST_ASSERT(g_TstMempool.mp_num_free == g_TstMempool.mp_num_blocks,
-                "Put all blocks but number free not equal to total!");
-
-    /* Better get error when we try these things! */
-    rc = os_memblock_put(NULL, block_array[0]);
-    TEST_ASSERT(rc != 0,
-                "Should have got an error trying to put to null pool");
-
-    rc = os_memblock_put(&g_TstMempool, NULL);
-    TEST_ASSERT(rc != 0, "No error trying to put to NULL block");
-
-    TEST_ASSERT(os_memblock_get(NULL) == NULL,
-                "No error trying to get a block from NULL pool");
-
-    /* Attempt to free a block outside the range of the membuf */
-    test_block = g_TstMempool.mp_membuf_addr;
-    test_block -= 4;
-    rc = os_memblock_put(&g_TstMempool, (void *)test_block);
-    TEST_ASSERT(rc == OS_INVALID_PARM, "No error freeing bad block address");
-
-    test_block += (true_block_size * g_TstMempool.mp_num_blocks) + 100;
-    rc = os_memblock_put(&g_TstMempool, (void *)test_block);
-    TEST_ASSERT(rc == OS_INVALID_PARM, "No error freeing bad block address");
-
-    /* Attempt to free on bad boundary */
-    test_block = g_TstMempool.mp_membuf_addr;
-    test_block += (true_block_size / 2);
-    rc = os_memblock_put(&g_TstMempool, (void *)test_block);
-    TEST_ASSERT(rc == OS_INVALID_PARM, "No error freeing bad block address");
-}
-
-/**
- * os mempool test 
- *  
- * Main test loop for memory pool testing. 
- * 
- * @return int 
- */
-TEST_CASE(os_mempool_test_case)
-{
-    mempool_test(NUM_MEM_BLOCKS, MEM_BLOCK_SIZE);
-}
-
-TEST_SUITE(os_mempool_test_suite)
-{
-    os_mempool_test_case();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/test/mutex_test.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/mutex_test.c b/libs/os/src/test/mutex_test.c
deleted file mode 100644
index ef6a08d..0000000
--- a/libs/os/src/test/mutex_test.c
+++ /dev/null
@@ -1,407 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <string.h>
-#include <assert.h>
-#include <sys/time.h>
-#include "testutil/testutil.h"
-#include "os/os.h"
-#include "os/os_test.h"
-#include "os/os_cfg.h"
-#include "os/os_mutex.h"
-#include "os_test_priv.h"
-
-#ifdef ARCH_sim
-#define MUTEX_TEST_STACK_SIZE   1024
-#else
-#define MUTEX_TEST_STACK_SIZE   256
-#endif
-
-struct os_task task14;
-os_stack_t stack14[OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE)];
-
-struct os_task task15;
-os_stack_t stack15[OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE)];
-
-struct os_task task16;
-os_stack_t stack16[OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE)];
-
-struct os_task task17;
-os_stack_t stack17[OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE)];
-
-#define TASK14_PRIO (4)
-#define TASK15_PRIO (5)
-#define TASK16_PRIO (6)
-#define TASK17_PRIO (7)
-
-volatile int g_task14_val;
-volatile int g_task15_val;
-volatile int g_task16_val;
-volatile int g_task17_val;
-struct os_mutex g_mutex1;
-struct os_mutex g_mutex2;
-
-static volatile int g_mutex_test;
-
-/**
- * mutex test basic 
- *  
- * Basic mutex tests
- * 
- * @return int 
- */
-static void
-mutex_test_basic_handler(void *arg)
-{
-    struct os_mutex *mu;
-    struct os_task *t;
-    os_error_t err;
-
-    mu = &g_mutex1;
-    t = os_sched_get_current_task();
-
-    /* Test some error cases */
-    TEST_ASSERT(os_mutex_init(NULL)     == OS_INVALID_PARM);
-    TEST_ASSERT(os_mutex_release(NULL)  == OS_INVALID_PARM);
-    TEST_ASSERT(os_mutex_pend(NULL, 0)  == OS_INVALID_PARM);
-
-    /* Get the mutex */
-    err = os_mutex_pend(mu, 0);
-    TEST_ASSERT(err == 0,
-                "Did not get free mutex immediately (err=%d)", err);
-
-    /* Check mutex internals */
-    TEST_ASSERT(mu->mu_owner == t && mu->mu_level == 1 &&
-                mu->mu_prio == t->t_prio && SLIST_EMPTY(&mu->mu_head),
-                "Mutex internals not correct after getting mutex\n"
-                "Mutex: owner=%p prio=%u level=%u head=%p\n"
-                "Task: task=%p prio=%u",
-                mu->mu_owner, mu->mu_prio, mu->mu_level, 
-                SLIST_FIRST(&mu->mu_head),
-                t, t->t_prio);
-
-    /* Get the mutex again; should be level 2 */
-    err = os_mutex_pend(mu, 0);
-    TEST_ASSERT(err == 0, "Did not get my mutex immediately (err=%d)", err);
-
-    /* Check mutex internals */
-    TEST_ASSERT(mu->mu_owner == t && mu->mu_level == 2 &&
-                mu->mu_prio == t->t_prio && SLIST_EMPTY(&mu->mu_head),
-                "Mutex internals not correct after getting mutex\n"
-                "Mutex: owner=%p prio=%u level=%u head=%p\n"
-                "Task: task=%p prio=%u",
-                mu->mu_owner, mu->mu_prio, mu->mu_level, 
-                SLIST_FIRST(&mu->mu_head), t, t->t_prio);
-
-    /* Release mutex */
-    err = os_mutex_release(mu);
-    TEST_ASSERT(err == 0, "Could not release mutex I own (err=%d)", err);
-
-    /* Check mutex internals */
-    TEST_ASSERT(mu->mu_owner == t && mu->mu_level == 1 &&
-                mu->mu_prio == t->t_prio && SLIST_EMPTY(&mu->mu_head),
-                "Error: mutex internals not correct after getting mutex\n"
-                "Mutex: owner=%p prio=%u level=%u head=%p\n"
-                "Task: task=%p prio=%u",
-                mu->mu_owner, mu->mu_prio, mu->mu_level, 
-                SLIST_FIRST(&mu->mu_head), t, t->t_prio);
-
-    /* Release it again */
-    err = os_mutex_release(mu);
-    TEST_ASSERT(err == 0, "Could not release mutex I own (err=%d)", err);
-
-    /* Check mutex internals */
-    TEST_ASSERT(mu->mu_owner == NULL && mu->mu_level == 0 &&
-                mu->mu_prio == t->t_prio && SLIST_EMPTY(&mu->mu_head),
-                "Mutex internals not correct after getting mutex\n"
-                "Mutex: owner=%p prio=%u level=%u head=%p\n"
-                "Task: task=%p prio=%u",
-                mu->mu_owner, mu->mu_prio, mu->mu_level, 
-                SLIST_FIRST(&mu->mu_head), t, t->t_prio);
-
-    os_test_restart();
-}
-
-static void 
-mutex_test1_task14_handler(void *arg)
-{
-    os_error_t err;
-    struct os_task *t;
-    int iters;
-
-    t = os_sched_get_current_task();
-    TEST_ASSERT(t->t_func == mutex_test1_task14_handler);
-
-    for (iters = 0; iters < 3; iters++) {
-        os_time_delay(100);
-
-        g_task14_val = 1;
-
-        err = os_mutex_pend(&g_mutex1, 100);
-        TEST_ASSERT(err == OS_OK);
-        TEST_ASSERT(g_task16_val == 1);
-
-        os_time_delay(100);
-    }
-
-    os_test_restart();
-}
-
-static void 
-mutex_test2_task14_handler(void *arg)
-{
-    os_error_t err;
-    struct os_task *t;
-    int iters;
-
-    t = os_sched_get_current_task();
-    TEST_ASSERT(t->t_func == mutex_test2_task14_handler);
-
-    for (iters = 0; iters < 3; iters++) {
-        err = os_mutex_pend(&g_mutex1, 0);
-        TEST_ASSERT(err == OS_OK, "err=%d", err);
-
-        g_task14_val = 1;
-        os_time_delay(100);
-
-        /* 
-         * Task17 should have its mutex wait flag set; at least the first time
-         * through!
-         */
-        if (iters == 0) {
-            TEST_ASSERT(task17.t_flags & OS_TASK_FLAG_MUTEX_WAIT);
-        }
-
-        if (g_mutex_test == 4) {
-            os_time_delay(150);
-        }
-
-        os_mutex_release(&g_mutex1);
-        os_time_delay(100);
-    }
-
-    os_test_restart();
-}
-
-static void 
-task15_handler(void *arg) 
-{
-    os_error_t err;
-    struct os_task *t;
-
-    if (g_mutex_test == 1) {
-        while (1) {
-            t = os_sched_get_current_task();
-            TEST_ASSERT(t->t_func == task15_handler);
-
-            os_time_delay(50);
-            while (1) {
-                /* Wait here forever */
-            }
-        }
-    } else {
-        if (g_mutex_test == 2) {
-            /* Sleep for 3 seconds */
-            t = os_sched_get_current_task();
-            os_time_delay(500);
-        } else if (g_mutex_test == 3) {
-            /* Sleep for 3 seconds */
-            t = os_sched_get_current_task();
-            os_time_delay(30);
-        }
-
-        while (1) {
-            t = os_sched_get_current_task();
-            TEST_ASSERT(t->t_func == task15_handler);
-
-            err = os_mutex_pend(&g_mutex1, 10000);
-            if (g_mutex_test == 4) {
-                TEST_ASSERT(err == OS_TIMEOUT);
-            } else {
-                TEST_ASSERT(err == OS_OK);
-            }
-
-            os_time_delay(100);
-        }
-    }
-}
-
-static void 
-task16_handler(void *arg) 
-{
-    os_error_t err;
-    struct os_task *t;
-
-    if (g_mutex_test == 1) {
-        while (1) {
-            t = os_sched_get_current_task();
-            TEST_ASSERT(t->t_func == task16_handler);
-
-            /* Get mutex 1 */
-            err = os_mutex_pend(&g_mutex1, 0xFFFFFFFF);
-            TEST_ASSERT(err == OS_OK);
-
-            while (g_task14_val != 1) {
-                /* Wait till task 1 wakes up and sets val. */
-            }
-
-            g_task16_val = 1;
-
-            err = os_mutex_release(&g_mutex1);
-            TEST_ASSERT(err == OS_OK);
-        }
-    } else {
-        if (g_mutex_test == 2) {
-            /* Sleep for 3 seconds */
-            t = os_sched_get_current_task();
-            os_time_delay(30);
-        } else if (g_mutex_test == 3) {
-            /* Sleep for 3 seconds */
-            t = os_sched_get_current_task();
-            os_time_delay(50);
-        }
-
-        while (1) {
-            t = os_sched_get_current_task();
-            TEST_ASSERT(t->t_func == task16_handler);
-
-            err = os_mutex_pend(&g_mutex1, 10000);
-            if (g_mutex_test == 4) {
-                TEST_ASSERT(err == OS_TIMEOUT);
-            } else {
-                TEST_ASSERT(err == OS_OK);
-            }
-
-            if (err == OS_OK) {
-                err = os_mutex_release(&g_mutex1);
-                TEST_ASSERT(err == OS_OK);
-            }
-
-            os_time_delay(10000);
-        }
-    }
-}
-
-static void 
-task17_handler(void *arg)
-{
-    os_error_t err;
-    struct os_task *t;
-
-    while (1) {
-        t = os_sched_get_current_task();
-        TEST_ASSERT(t->t_func == task17_handler);
-
-        if (g_mutex_test == 5) {
-            err = os_mutex_pend(&g_mutex1, 10);
-        } else {
-            err = os_mutex_pend(&g_mutex1, 10000);
-            TEST_ASSERT((t->t_flags & OS_TASK_FLAG_MUTEX_WAIT) == 0);
-        }
-
-        if (g_mutex_test == 4 || g_mutex_test == 5) {
-            TEST_ASSERT(err == OS_TIMEOUT);
-        } else {
-            TEST_ASSERT(err == OS_OK);
-        }
-
-        if (err == OS_OK) {
-            err = os_mutex_release(&g_mutex1);
-            TEST_ASSERT(err == OS_OK);
-        }
-
-        os_time_delay(10000);
-    }
-}
-
-TEST_CASE(os_mutex_test_basic)
-{
-    os_init();
-
-    os_mutex_init(&g_mutex1);
-
-    os_task_init(&task14, "task14", mutex_test_basic_handler, NULL,
-                 TASK14_PRIO, OS_WAIT_FOREVER, stack14,
-                 OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
-
-    os_start();
-}
-
-TEST_CASE(os_mutex_test_case_1)
-{
-    int rc;
-
-    os_init();
-
-    g_mutex_test = 1;
-    g_task14_val = 0;
-    g_task15_val = 0;
-    g_task16_val = 0;
-
-    rc = os_mutex_init(&g_mutex1);
-    TEST_ASSERT(rc == 0);
-    rc = os_mutex_init(&g_mutex2);
-    TEST_ASSERT(rc == 0);
-
-    os_task_init(&task14, "task14", mutex_test1_task14_handler, NULL,
-                 TASK14_PRIO, OS_WAIT_FOREVER, stack14,
-                 OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
-
-    os_task_init(&task15, "task15", task15_handler, NULL, TASK15_PRIO, 
-            OS_WAIT_FOREVER, stack15, OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
-
-    os_task_init(&task16, "task16", task16_handler, NULL, TASK16_PRIO, 
-            OS_WAIT_FOREVER, stack16, OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
-
-    os_start();
-}
-
-TEST_CASE(os_mutex_test_case_2)
-{
-    os_init();
-
-    g_mutex_test = 2;
-    g_task14_val = 0;
-    g_task15_val = 0;
-    g_task16_val = 0;
-    os_mutex_init(&g_mutex1);
-    os_mutex_init(&g_mutex2);
-
-    os_task_init(&task14, "task14", mutex_test2_task14_handler, NULL,
-                 TASK14_PRIO, OS_WAIT_FOREVER, stack14,
-                 OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
-
-    os_task_init(&task15, "task15", task15_handler, NULL, TASK15_PRIO, 
-            OS_WAIT_FOREVER, stack15, OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
-
-    os_task_init(&task16, "task16", task16_handler, NULL, TASK16_PRIO, 
-            OS_WAIT_FOREVER, stack16, OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
-
-    os_task_init(&task17, "task17", task17_handler, NULL, TASK17_PRIO, 
-            OS_WAIT_FOREVER, stack17, OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
- 
-    os_start();
-}
-
-TEST_SUITE(os_mutex_test_suite)
-{
-    os_mutex_test_basic();
-    os_mutex_test_case_1();
-    os_mutex_test_case_2();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/test/os_test.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/os_test.c b/libs/os/src/test/os_test.c
deleted file mode 100644
index 1b3af1a..0000000
--- a/libs/os/src/test/os_test.c
+++ /dev/null
@@ -1,52 +0,0 @@
-/**
- * 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 <assert.h>
-#include <stddef.h>
-#include "testutil/testutil.h"
-#include "os/os_test.h"
-#include "os_test_priv.h"
-
-int
-os_test_all(void)
-{
-    os_mempool_test_suite();
-    os_mutex_test_suite();
-    os_sem_test_suite();
-    os_mbuf_test_suite();
-    os_eventq_test_suite();
-    
-
-    return tu_case_failed;
-}
-
-#ifdef MYNEWT_SELFTEST
-
-int
-main(int argc, char **argv)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    os_test_all();
-
-    return tu_any_failed;
-}
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/test/os_test_priv.h
----------------------------------------------------------------------
diff --git a/libs/os/src/test/os_test_priv.h b/libs/os/src/test/os_test_priv.h
deleted file mode 100644
index 5193c33..0000000
--- a/libs/os/src/test/os_test_priv.h
+++ /dev/null
@@ -1,32 +0,0 @@
-/**
- * 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_OS_TEST_PRIV_
-#define H_OS_TEST_PRIV_
-
-void os_test_restart(void);
-
-int os_mempool_test_suite(void);
-int os_mbuf_test_suite(void);
-int os_mutex_test_suite(void);
-int os_sem_test_suite(void);
-int os_eventq_test_suite(void);
-
-
-#endif


[18/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_os_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_os_test.c b/net/nimble/host/src/test/ble_os_test.c
deleted file mode 100644
index a9c28ea..0000000
--- a/net/nimble/host/src/test/ble_os_test.c
+++ /dev/null
@@ -1,401 +0,0 @@
-/**
- * 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 <string.h>
-#include "os/os.h"
-#include "testutil/testutil.h"
-#include "nimble/hci_common.h"
-#include "nimble/ble_hci_trans.h"
-#include "host/ble_hs_test.h"
-#include "host/ble_gap.h"
-#include "ble_hs_test_util.h"
-
-#define BLE_OS_TEST_STACK_SIZE      256
-#define BLE_OS_TEST_APP_STACK_SIZE  256
-
-#define BLE_OS_TEST_APP_PRIO         9
-#define BLE_OS_TEST_TASK_PRIO        10
-
-static struct os_task ble_os_test_task;
-static struct os_task ble_os_test_app_task;
-static os_stack_t ble_os_test_stack[OS_STACK_ALIGN(BLE_OS_TEST_STACK_SIZE)];
-
-static os_stack_t
-ble_os_test_app_stack[OS_STACK_ALIGN(BLE_OS_TEST_APP_STACK_SIZE)];
-
-static uint8_t ble_os_test_peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-static void ble_os_test_app_task_handler(void *arg);
-
-static int ble_os_test_gap_event_type;
-
-static void
-ble_os_test_init_app_task(void)
-{
-    int rc;
-
-    rc = os_task_init(&ble_os_test_app_task,
-                      "ble_gap_terminate_test_task",
-                      ble_os_test_app_task_handler, NULL,
-                      BLE_OS_TEST_APP_PRIO, OS_WAIT_FOREVER,
-                      ble_os_test_app_stack,
-                      OS_STACK_ALIGN(BLE_OS_TEST_APP_STACK_SIZE));
-    TEST_ASSERT_FATAL(rc == 0);
-}
-
-static void
-ble_os_test_misc_init(void)
-{
-    ble_hs_test_util_init();
-
-    /* Receive acknowledgements for the startup sequence.  We sent the
-     * corresponding requests when the host task was started.
-     */
-    ble_hs_test_util_set_startup_acks();
-
-    ble_os_test_init_app_task();
-}
-
-static int
-ble_os_test_misc_conn_exists(uint16_t conn_handle)
-{
-    struct ble_hs_conn *conn;
-
-    ble_hs_lock();
-
-    if (conn_handle == BLE_HS_CONN_HANDLE_NONE) {
-        conn = ble_hs_conn_first();
-    } else {
-        conn = ble_hs_conn_find(conn_handle);
-    }
-
-    ble_hs_unlock();
-
-    return conn != NULL;
-}
-
-static int
-ble_gap_direct_connect_test_connect_cb(struct ble_gap_event *event, void *arg)
-{
-    struct ble_gap_conn_desc desc;
-    int *cb_called;
-    int rc;
-
-    cb_called = arg;
-    *cb_called = 1;
-
-    TEST_ASSERT(event->type == BLE_GAP_EVENT_CONNECT);
-    TEST_ASSERT(event->connect.status == 0);
-    TEST_ASSERT(event->connect.conn_handle == 2);
-
-    rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(desc.peer_id_addr_type == BLE_ADDR_TYPE_PUBLIC);
-    TEST_ASSERT(memcmp(desc.peer_id_addr, ble_os_test_peer_addr, 6) == 0);
-
-    return 0;
-}
-
-static void
-ble_gap_direct_connect_test_task_handler(void *arg)
-{
-    struct hci_le_conn_complete evt;
-    uint8_t addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int cb_called;
-    int rc;
-
-    /* Set the connect callback so we can verify that it gets called with the
-     * proper arguments.
-     */
-    cb_called = 0;
-
-    /* Make sure there are no created connections and no connections in
-     * progress.
-     */
-    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
-
-    /* Initiate a direct connection. */
-    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                             addr, 0, NULL,
-                             ble_gap_direct_connect_test_connect_cb,
-                             &cb_called, 0);
-    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
-    TEST_ASSERT(!cb_called);
-
-    /* Receive an HCI connection-complete event. */
-    memset(&evt, 0, sizeof evt);
-    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    evt.status = BLE_ERR_SUCCESS;
-    evt.connection_handle = 2;
-    memcpy(evt.peer_addr, addr, 6);
-    rc = ble_gap_rx_conn_complete(&evt);
-    TEST_ASSERT(rc == 0);
-
-    /* The connection should now be created. */
-    TEST_ASSERT(ble_os_test_misc_conn_exists(2));
-    TEST_ASSERT(cb_called);
-
-    tu_restart();
-}
-
-TEST_CASE(ble_gap_direct_connect_test_case)
-{
-    ble_os_test_misc_init();
-
-    os_task_init(&ble_os_test_task,
-                 "ble_gap_direct_connect_test_task",
-                 ble_gap_direct_connect_test_task_handler, NULL,
-                 BLE_OS_TEST_TASK_PRIO, OS_WAIT_FOREVER, ble_os_test_stack,
-                 OS_STACK_ALIGN(BLE_OS_TEST_STACK_SIZE));
-
-    os_start();
-}
-
-static int
-ble_os_disc_test_cb(struct ble_gap_event *event, void *arg)
-{
-    int *cb_called;
-
-    cb_called = arg;
-    *cb_called = 1;
-
-    TEST_ASSERT(event->type == BLE_GAP_EVENT_DISC_COMPLETE);
-
-    return 0;
-}
-
-static void
-ble_os_disc_test_task_handler(void *arg)
-{
-    struct ble_gap_disc_params disc_params;
-    int cb_called;
-    int rc;
-
-    /* Receive acknowledgements for the startup sequence.  We sent the
-     * corresponding requests when the host task was started.
-     */
-    ble_hs_test_util_set_startup_acks();
-
-    /* Set the connect callback so we can verify that it gets called with the
-     * proper arguments.
-     */
-    cb_called = 0;
-
-    /* Make sure there are no created connections and no connections in
-     * progress.
-     */
-    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    /* Initiate the general discovery procedure with a 300 ms timeout. */
-    memset(&disc_params, 0, sizeof disc_params);
-    rc = ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC, 300, &disc_params,
-                               ble_os_disc_test_cb,
-                               &cb_called, 0, 0);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
-    TEST_ASSERT(ble_gap_master_in_progress());
-    TEST_ASSERT(!cb_called);
-
-    /* Receive acks from the controller. */
-    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
-    TEST_ASSERT(ble_gap_master_in_progress());
-    TEST_ASSERT(!cb_called);
-
-    /* Wait 100 ms; verify scan still in progress. */
-    os_time_delay(100 * OS_TICKS_PER_SEC / 1000);
-    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
-    TEST_ASSERT(ble_gap_master_in_progress());
-    TEST_ASSERT(!cb_called);
-
-    ble_hs_test_util_set_ack(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
-        0);
-
-    /* Wait 250 more ms; verify scan completed. */
-    os_time_delay(250 * OS_TICKS_PER_SEC / 1000);
-    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(cb_called);
-
-    tu_restart();
-}
-
-TEST_CASE(ble_os_disc_test_case)
-{
-    ble_os_test_misc_init();
-
-    os_task_init(&ble_os_test_task,
-                 "ble_os_disc_test_task",
-                 ble_os_disc_test_task_handler, NULL,
-                 BLE_OS_TEST_TASK_PRIO, OS_WAIT_FOREVER, ble_os_test_stack,
-                 OS_STACK_ALIGN(BLE_OS_TEST_STACK_SIZE));
-
-    os_start();
-}
-
-static int
-ble_gap_terminate_cb(struct ble_gap_event *event, void *arg)
-{
-    int *disconn_handle;
-
-    ble_os_test_gap_event_type = event->type;
-
-    if (event->type == BLE_GAP_EVENT_DISCONNECT) {
-        disconn_handle = arg;
-        *disconn_handle = event->disconnect.conn.conn_handle;
-    }
-
-    return 0;
-}
-
-
-static void
-ble_gap_terminate_test_task_handler(void *arg)
-{
-    struct hci_disconn_complete disconn_evt;
-    struct hci_le_conn_complete conn_evt;
-    uint8_t addr1[6] = { 1, 2, 3, 4, 5, 6 };
-    uint8_t addr2[6] = { 2, 3, 4, 5, 6, 7 };
-    int disconn_handle;
-    int rc;
-
-    /* Receive acknowledgements for the startup sequence.  We sent the
-     * corresponding requests when the host task was started.
-     */
-    ble_hs_test_util_set_startup_acks();
-
-    /* Set the connect callback so we can verify that it gets called with the
-     * proper arguments.
-     */
-    disconn_handle = 0;
-
-    /* Make sure there are no created connections and no connections in
-     * progress.
-     */
-    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    /* Create two direct connections. */
-    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                             addr1, 0, NULL, ble_gap_terminate_cb,
-                             &disconn_handle, 0);
-    memset(&conn_evt, 0, sizeof conn_evt);
-    conn_evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    conn_evt.status = BLE_ERR_SUCCESS;
-    conn_evt.connection_handle = 1;
-    memcpy(conn_evt.peer_addr, addr1, 6);
-    rc = ble_gap_rx_conn_complete(&conn_evt);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                             addr2, 0, NULL, ble_gap_terminate_cb,
-                             &disconn_handle, 0);
-    memset(&conn_evt, 0, sizeof conn_evt);
-    conn_evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    conn_evt.status = BLE_ERR_SUCCESS;
-    conn_evt.connection_handle = 2;
-    memcpy(conn_evt.peer_addr, addr2, 6);
-    rc = ble_gap_rx_conn_complete(&conn_evt);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT_FATAL(ble_os_test_misc_conn_exists(1));
-    TEST_ASSERT_FATAL(ble_os_test_misc_conn_exists(2));
-
-    /* Terminate the first one. */
-    rc = ble_hs_test_util_conn_terminate(1, 0);
-    TEST_ASSERT(rc == 0);
-    disconn_evt.connection_handle = 1;
-    disconn_evt.status = 0;
-    disconn_evt.reason = BLE_ERR_REM_USER_CONN_TERM;
-    ble_hs_test_util_rx_disconn_complete_event(&disconn_evt);
-    TEST_ASSERT(ble_os_test_gap_event_type == BLE_GAP_EVENT_DISCONNECT);
-    TEST_ASSERT(disconn_handle == 1);
-    TEST_ASSERT_FATAL(!ble_os_test_misc_conn_exists(1));
-    TEST_ASSERT_FATAL(ble_os_test_misc_conn_exists(2));
-
-    /* Terminate the second one. */
-    rc = ble_hs_test_util_conn_terminate(2, 0);
-    TEST_ASSERT(rc == 0);
-    disconn_evt.connection_handle = 2;
-    disconn_evt.status = 0;
-    disconn_evt.reason = BLE_ERR_REM_USER_CONN_TERM;
-    ble_hs_test_util_rx_disconn_complete_event(&disconn_evt);
-    TEST_ASSERT(ble_os_test_gap_event_type == BLE_GAP_EVENT_DISCONNECT);
-    TEST_ASSERT(disconn_handle == 2);
-    TEST_ASSERT_FATAL(!ble_os_test_misc_conn_exists(1));
-    TEST_ASSERT_FATAL(!ble_os_test_misc_conn_exists(2));
-
-    tu_restart();
-}
-
-static void
-ble_os_test_app_task_handler(void *arg)
-{
-    struct os_callout_func *cf;
-    struct os_event *ev;
-    int rc;
-
-    rc = ble_hs_start();
-    TEST_ASSERT(rc == 0);
-
-    while (1) {
-        ev = os_eventq_get(&ble_hs_test_util_evq);
-        switch (ev->ev_type) {
-        case OS_EVENT_T_TIMER:
-            cf = (struct os_callout_func *)ev;
-            assert(cf->cf_func);
-            cf->cf_func(CF_ARG(cf));
-            break;
-        default:
-            assert(0);
-            break;
-        }
-    }
-}
-
-TEST_CASE(ble_gap_terminate_test_case)
-{
-    ble_os_test_misc_init();
-
-    os_task_init(&ble_os_test_task,
-                 "ble_gap_terminate_test_task",
-                 ble_gap_terminate_test_task_handler, NULL,
-                 BLE_OS_TEST_TASK_PRIO, OS_WAIT_FOREVER, ble_os_test_stack,
-                 OS_STACK_ALIGN(BLE_OS_TEST_STACK_SIZE));
-
-    os_start();
-}
-
-TEST_SUITE(ble_os_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_os_disc_test_case();
-    ble_gap_direct_connect_test_case();
-    ble_gap_terminate_test_case();
-}
-
-int
-ble_os_test_all(void)
-{
-    ble_os_test_suite();
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_sm_lgcy_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_lgcy_test.c b/net/nimble/host/src/test/ble_sm_lgcy_test.c
deleted file mode 100644
index 6e451ca..0000000
--- a/net/nimble/host/src/test/ble_sm_lgcy_test.c
+++ /dev/null
@@ -1,839 +0,0 @@
-/**
- * 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 <stddef.h>
-#include <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/hci_common.h"
-#include "nimble/nimble_opt.h"
-#include "host/ble_sm.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-#include "ble_sm_test_util.h"
-
-#if NIMBLE_OPT(SM)
-
-/**
- * Legacy pairing
- * Master: peer
- * Pair algorithm: just works
- * Initiator IO capabilities: 4
- * Responder IO capabilities: 3
- * Bonding: true
- * Initiator address type: BLE_ADDR_TYPE_PUBLIC
- * Responder address type: BLE_ADDR_TYPE_PUBLIC
- * Initiator key distribution: 7
- * Responder key distribution: 7
- */
-TEST_CASE(ble_sm_lgcy_peer_jw_iio4_rio3_b1_iat0_rat0_ik7_rk7)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
-        },
-        .resp_id_addr = {
-            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-        },
-        .pair_req = {
-            .io_cap = 0x04,
-            .oob_data_flag = 0x00,
-            .authreq = 0x05,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_rsp = {
-            .io_cap = 0x03,
-            .oob_data_flag = 0x00,
-            .authreq = 0x09,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .confirm_req[0] = {
-            .value = {
-                0xcd, 0x5b, 0x79, 0x29, 0x53, 0x31, 0x56, 0x23,
-                0x2c, 0x08, 0xed, 0x81, 0x16, 0x55, 0x8e, 0x01,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0x49, 0x39, 0x22, 0x0f, 0x7b, 0x1b, 0x80, 0xcd,
-                0xbe, 0x89, 0xd1, 0x4c, 0xbd, 0x6f, 0xda, 0x2c,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0x7f, 0x42, 0xc0, 0x2f, 0x1d, 0x07, 0x37, 0xfc,
-                0x04, 0x5b, 0x05, 0x9a, 0xed, 0x67, 0xa5, 0x68,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0x42, 0x1a, 0x58, 0xa2, 0x3b, 0x80, 0xde, 0xef,
-                0x95, 0x0d, 0xf7, 0xca, 0x06, 0x05, 0x01, 0x3c,
-            },
-        },
-        .enc_info_req = {
-            .ltk = {
-                0x2f, 0x9b, 0x16, 0xff, 0xf3, 0x73, 0x30, 0x08,
-                0xa8, 0xe5, 0x01, 0xb1, 0x3b, 0xe1, 0x87, 0x00,
-            },
-        },
-        .master_id_req = {
-            .ediv = 0xf8e0,
-            .rand_val = 0xef7c818b00000000,
-        },
-        .id_info_req = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_req = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-            },
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0xc6, 0x17, 0xc0, 0x02, 0x40, 0x0d, 0x27, 0x51,
-                0x8a, 0x77, 0xb5, 0xae, 0xd8, 0xa9, 0x7a, 0x7a,
-            },
-        },
-        .enc_info_rsp = {
-            .ltk = {
-                0xd7, 0x07, 0x22, 0x79, 0x24, 0xc6, 0xcb, 0x4d,
-                0xa3, 0xdd, 0x01, 0xfb, 0x48, 0x87, 0xd4, 0xcf,
-            },
-        },
-        .master_id_rsp = {
-            .ediv = 0x9a39,
-            .rand_val = 0x8e76d9b00000000,
-        },
-        .id_info_rsp = {
-            .irk = {
-                0xeb, 0x8a, 0x06, 0xc4, 0x93, 0x51, 0x04, 0xb3,
-                0x8b, 0xbf, 0xe8, 0x1f, 0x0e, 0x96, 0x2a, 0x54,
-            },
-        },
-        .id_addr_info_rsp = {
-            .addr_type = 0,
-            .bd_addr = {
-                0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0x14, 0x55, 0x93, 0xe1, 0xd1, 0xe7, 0xc4, 0x5d,
-                0x35, 0x97, 0xd3, 0x05, 0x30, 0xc8, 0x9d, 0x83,
-            },
-        },
-        .stk = {
-            0x1c, 0xd7, 0xb6, 0x35, 0x48, 0xfc, 0x9f, 0xef,
-            0x0e, 0x2f, 0x51, 0x77, 0xed, 0xdd, 0xbc, 0xaf,
-        },
-        .pair_alg = 0,
-        .authenticated = false,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_NONE,
-            },
-        },
-    };
-    ble_sm_test_util_peer_lgcy_good(&params);
-}
-
-/**
- * Legacy pairing
- * Master: peer
- * Pair algorithm: passkey entry
- * Initiator IO capabilities: 4
- * Responder IO capabilities: 0
- * Bonding: true
- * Initiator address type: BLE_ADDR_TYPE_PUBLIC
- * Responder address type: BLE_ADDR_TYPE_PUBLIC
- * Initiator key distribution: 7
- * Responder key distribution: 7
- */
-TEST_CASE(ble_sm_lgcy_peer_pk_iio4_rio0_b1_iat0_rat0_ik7_rk7)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
-        },
-        .resp_id_addr = {
-            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-        },
-        .pair_req = {
-            .io_cap = 0x04,
-            .oob_data_flag = 0x00,
-            .authreq = 0x05,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_rsp = {
-            .io_cap = 0x00,
-            .oob_data_flag = 0x00,
-            .authreq = 0x09,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .confirm_req[0] = {
-            .value = {
-                0xa0, 0x10, 0x4a, 0xaa, 0x8b, 0x53, 0x78, 0xbb,
-                0xd2, 0xae, 0x71, 0x1f, 0x4e, 0x00, 0x70, 0x8b,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0x62, 0xf3, 0xba, 0x0e, 0xe5, 0xbe, 0x2e, 0xd8,
-                0x25, 0xb2, 0xec, 0x4c, 0x28, 0x77, 0x28, 0x60,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0x84, 0xcf, 0xe4, 0x04, 0x7d, 0xf3, 0xfc, 0xa1,
-                0x3f, 0x75, 0xd6, 0x5a, 0x7c, 0xb7, 0xa4, 0x39,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0xef, 0x6a, 0x61, 0x6e, 0x02, 0x60, 0x7f, 0x5d,
-                0x7f, 0x0d, 0xa6, 0x3c, 0x06, 0x1a, 0x5d, 0xd6,
-            },
-        },
-        .enc_info_req = {
-            .ltk = {
-                0xad, 0x01, 0x6d, 0x76, 0xa9, 0xd0, 0x23, 0xc9,
-                0x40, 0x0c, 0xbf, 0x2a, 0x4c, 0x23, 0x31, 0xc5,
-            },
-        },
-        .master_id_req = {
-            .ediv = 0xa74f,
-            .rand_val = 0x81cab3fd00000000,
-        },
-        .id_info_req = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_req = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-            },
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0x60, 0x08, 0x49, 0x00, 0x6d, 0x76, 0x98, 0x73,
-                0x9c, 0x95, 0xc4, 0xd9, 0xe8, 0x3a, 0x69, 0xbb,
-            },
-        },
-        .enc_info_rsp = {
-            .ltk = {
-                0x5b, 0x73, 0x39, 0xd9, 0x51, 0x3d, 0x92, 0xa4,
-                0x34, 0x65, 0xa5, 0x70, 0x49, 0xbe, 0x11, 0x28,
-            },
-        },
-        .master_id_rsp = {
-            .ediv = 0x9705,
-            .rand_val = 0x592f1e8d00000000,
-        },
-        .id_info_rsp = {
-            .irk = {
-                0xeb, 0x8a, 0x06, 0xc4, 0x93, 0x51, 0x04, 0xb3,
-                0x8b, 0xbf, 0xe8, 0x1f, 0x0e, 0x96, 0x2a, 0x54,
-            },
-        },
-        .id_addr_info_rsp = {
-            .addr_type = 0,
-            .bd_addr = {
-                0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0xc9, 0x9b, 0xf2, 0x75, 0xb7, 0x0d, 0xe8, 0x60,
-                0x3d, 0xf0, 0xd6, 0xa8, 0x16, 0xc5, 0x6c, 0x2a,
-            },
-        },
-        .stk = {
-            0xf2, 0x3c, 0x36, 0xc4, 0xa1, 0xfb, 0x5a, 0xa7,
-            0x96, 0x20, 0xe4, 0x29, 0xb7, 0x58, 0x22, 0x7a,
-        },
-        .pair_alg = 1,
-        .authenticated = true,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_DISP,
-                .passkey = 46128,
-            },
-        },
-    };
-    ble_sm_test_util_peer_lgcy_good(&params);
-}
-
-/**
- * Legacy pairing
- * Master: us
- * Pair algorithm: just works
- * Initiator IO capabilities: 3
- * Responder IO capabilities: 3
- * Bonding: true
- * Initiator address type: BLE_ADDR_TYPE_PUBLIC
- * Responder address type: BLE_ADDR_TYPE_RANDOM
- * Initiator key distribution: 7
- * Responder key distribution: 5
- */
-TEST_CASE(ble_sm_lgcy_us_jw_iio3_rio3_b1_iat0_rat1_ik7_rk5)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a,
-        },
-        .resp_addr_type = BLE_ADDR_TYPE_RANDOM,
-        .resp_id_addr = {
-            0x11, 0x22, 0x11, 0x22, 0x11, 0xcc,
-        },
-        .pair_req = {
-            .io_cap = 0x03,
-            .oob_data_flag = 0x00,
-            .authreq = 0x01,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_rsp = {
-            .io_cap = 0x03,
-            .oob_data_flag = 0x00,
-            .authreq = 0x01,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x05,
-        },
-        .confirm_req[0] = {
-            .value = {
-                0x1c, 0xb6, 0x10, 0xea, 0x02, 0x08, 0x90, 0x64,
-                0xc7, 0xf8, 0xe5, 0x9c, 0xb4, 0x3a, 0x18, 0xca,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0xb8, 0x6f, 0xd1, 0xc6, 0x74, 0x35, 0xa3, 0x94,
-                0x68, 0x2f, 0xf1, 0x4c, 0x78, 0x44, 0xe8, 0x0d,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0x40, 0x48, 0x17, 0x4d, 0x42, 0xa0, 0xf8, 0xd5,
-                0xbf, 0x65, 0x67, 0xb8, 0x5e, 0x57, 0x38, 0xac,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0x2c, 0xa1, 0xb1, 0xf5, 0x54, 0x9b, 0x43, 0xe9,
-                0xb0, 0x62, 0x6a, 0xb0, 0x02, 0xb8, 0x6c, 0xca,
-            },
-        },
-        .enc_info_req = {
-            .ltk = {
-                0x01, 0x15, 0xb6, 0x93, 0xc9, 0xff, 0xfe, 0x27,
-                0x02, 0x41, 0xfd, 0x7b, 0x0e, 0x31, 0xd4, 0xa6,
-            },
-        },
-        .master_id_req = {
-            .ediv = 0xe4fb,
-            .rand_val = 0x8eee76b100000000,
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0x00, 0x2d, 0xf6, 0x3e, 0x5e, 0x0f, 0xd1, 0xe8,
-                0x4e, 0x5f, 0x61, 0x1c, 0x2c, 0x0b, 0xa5, 0x51,
-            },
-        },
-        .enc_info_rsp = {
-            .ltk = {
-                0x88, 0xbc, 0x95, 0x8d, 0xaa, 0x26, 0x8d, 0xd5,
-                0x18, 0xc9, 0x06, 0x70, 0xc2, 0x30, 0x56, 0x4c,
-            },
-        },
-        .master_id_rsp = {
-            .ediv = 0x4413,
-            .rand_val = 0xfad1c27300000000,
-        },
-        .id_info_rsp = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_rsp = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0x03, 0xad, 0xa4, 0xe1, 0x34, 0x76, 0x95, 0x54,
-                0xe5, 0x8f, 0xa4, 0x06, 0x72, 0xe6, 0xfc, 0x65,
-            },
-        },
-        .stk = {
-            0x31, 0x54, 0x42, 0x6c, 0x1c, 0x03, 0x36, 0x44,
-            0x0b, 0x72, 0x90, 0xa5, 0x1f, 0x79, 0x5b, 0xe9,
-        },
-        .pair_alg = 0,
-        .authenticated = false,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_NONE,
-            },
-        },
-    };
-    ble_sm_test_util_us_lgcy_good(&params);
-}
-
-/**
- * Legacy pairing
- * Master: us
- * Pair algorithm: passkey entry
- * Initiator IO capabilities: 4
- * Responder IO capabilities: 2
- * Bonding: true
- * Initiator address type: BLE_ADDR_TYPE_PUBLIC
- * Responder address type: BLE_ADDR_TYPE_RANDOM
- * Initiator key distribution: 7
- * Responder key distribution: 5
- */
-TEST_CASE(ble_sm_lgcy_us_pk_iio4_rio2_b1_iat0_rat1_ik7_rk5)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a,
-        },
-        .resp_addr_type = BLE_ADDR_TYPE_RANDOM,
-        .resp_id_addr = {
-            0x11, 0x22, 0x11, 0x22, 0x11, 0xcc,
-        },
-        .pair_req = {
-            .io_cap = 0x04,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_rsp = {
-            .io_cap = 0x02,
-            .oob_data_flag = 0x00,
-            .authreq = 0x05,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x05,
-        },
-        .confirm_req[0] = {
-            .value = {
-                0xb5, 0xd4, 0xc5, 0xe8, 0xef, 0xef, 0xd8, 0xd7,
-                0x2b, 0x14, 0x34, 0x35, 0x29, 0x18, 0xda, 0x12,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0x1a, 0x03, 0x20, 0xda, 0x60, 0x21, 0x9b, 0x4b,
-                0x5d, 0x45, 0x90, 0x64, 0xe1, 0x24, 0x2c, 0xb6,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0x45, 0xa3, 0x1a, 0x0b, 0xf6, 0x0f, 0x7c, 0xcf,
-                0x1a, 0xfb, 0xfc, 0x1a, 0xad, 0x62, 0x0e, 0x76,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0x82, 0xbb, 0x9f, 0x67, 0xc4, 0x88, 0xcb, 0x58,
-                0xee, 0xf9, 0x34, 0x35, 0x23, 0xa3, 0xd0, 0x22,
-            },
-        },
-        .enc_info_req = {
-            .ltk = {
-                0xfa, 0x43, 0x8f, 0x1f, 0xe6, 0x2a, 0x94, 0x5b,
-                0x54, 0x89, 0x2b, 0x0f, 0xd7, 0x23, 0x77, 0x9e,
-            },
-        },
-        .master_id_req = {
-            .ediv = 0x88b3,
-            .rand_val = 0x7c970e18dec74560,
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0x2e, 0x70, 0x3c, 0xbf, 0x20, 0xbe, 0x7d, 0x2d,
-                0xb3, 0x50, 0x46, 0x33, 0x4c, 0x20, 0x0e, 0xc8,
-            },
-        },
-        .enc_info_rsp = {
-            .ltk = {
-                0xc1, 0x64, 0x33, 0x10, 0x0f, 0x70, 0x2f, 0x9c,
-                0xe7, 0x31, 0xc5, 0x32, 0xdd, 0x98, 0x16, 0x75,
-            },
-        },
-        .master_id_rsp = {
-            .ediv = 0x1c19,
-            .rand_val = 0xef308872dc2a4cc2,
-        },
-        .id_info_rsp = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_rsp = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0xd7, 0x75, 0xfa, 0xed, 0xd7, 0xdd, 0x7b, 0xb3,
-                0xa4, 0x20, 0xea, 0x2f, 0x75, 0x60, 0xb1, 0x84,
-            },
-        },
-        .stk = {
-            0x9e, 0xe8, 0x35, 0x22, 0xb6, 0xbb, 0x54, 0x0d,
-            0x48, 0x1b, 0x25, 0xa0, 0xd8, 0xe2, 0xa5, 0x08,
-        },
-        .pair_alg = 1,
-        .authenticated = true,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_DISP,
-                .passkey = 46128,
-            },
-        },
-    };
-    ble_sm_test_util_us_lgcy_good(&params);
-}
-
-/**
- * Legacy pairing
- * Master: us
- * Pair algorithm: out of band
- * Initiator IO capabilities: 3
- * Responder IO capabilities: 3
- * Bonding: true
- * Initiator address type: BLE_ADDR_TYPE_PUBLIC
- * Responder address type: BLE_ADDR_TYPE_PUBLIC
- * Initiator key distribution: 7
- * Responder key distribution: 7
- */
-TEST_CASE(ble_sm_lgcy_us_ob_iio3_rio3_b1_iat0_rat0_ik7_rk7)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
-        },
-        .resp_id_addr = {
-            0x66, 0x33, 0x22, 0x66, 0x55, 0x11,
-        },
-        .pair_req = {
-            .io_cap = 0x03,
-            .oob_data_flag = 0x01,
-            .authreq = 0x05,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_rsp = {
-            .io_cap = 0x03,
-            .oob_data_flag = 0x01,
-            .authreq = 0x05,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .confirm_req[0] = {
-            .value = {
-                0x2c, 0x3f, 0x3e, 0xf5, 0x39, 0x50, 0x78, 0x4a,
-                0x3e, 0x14, 0x1a, 0x51, 0xfb, 0x8d, 0x6c, 0x10,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0xa9, 0x5c, 0x18, 0xb1, 0xdb, 0x51, 0x53, 0xa5,
-                0xd3, 0xe7, 0x72, 0x17, 0xfb, 0xa8, 0xfb, 0xeb,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0x40, 0x2f, 0x42, 0xba, 0x10, 0x7b, 0x22, 0x65,
-                0x84, 0xef, 0x63, 0xdf, 0x84, 0x7b, 0x04, 0xef,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0x94, 0xdc, 0x3c, 0xef, 0x65, 0xf7, 0x99, 0x2e,
-                0x50, 0x29, 0x97, 0x2a, 0x57, 0xfd, 0xe6, 0x6a,
-            },
-        },
-        .enc_info_req = {
-            .ltk = {
-                0x8c, 0x8e, 0x57, 0xba, 0x17, 0xbb, 0x04, 0xb5,
-                0x16, 0xad, 0x31, 0x37, 0xf8, 0x3e, 0x4f, 0x21,
-            },
-        },
-        .master_id_req = {
-            .ediv = 0xaaa4,
-            .rand_val = 0xc0c830e300000000,
-        },
-        .id_info_req = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_req = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x66, 0x33, 0x22, 0x66, 0x55, 0x11,
-            },
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0x5a, 0xe4, 0x2b, 0x40, 0x3a, 0x34, 0x1d, 0x94,
-                0x56, 0x7d, 0xf4, 0x41, 0x23, 0x81, 0xc4, 0x11,
-            },
-        },
-        .enc_info_rsp = {
-            .ltk = {
-                0xa6, 0x8e, 0xa0, 0xa4, 0x02, 0x64, 0x4c, 0x09,
-                0x31, 0x25, 0x8a, 0x4f, 0x49, 0x35, 0xb0, 0x1f,
-            },
-        },
-        .master_id_rsp = {
-            .ediv = 0x57a3,
-            .rand_val = 0x8276af9000000000,
-        },
-        .id_info_rsp = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_rsp = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0x8e, 0xef, 0x53, 0x5c, 0x1b, 0x21, 0x67, 0x8d,
-                0x07, 0x5e, 0xaa, 0xe8, 0x41, 0xa9, 0x36, 0xcf,
-            },
-        },
-        .stk = {
-            0x4c, 0xd4, 0xa7, 0xee, 0x83, 0xcd, 0xd1, 0x9e,
-            0x84, 0xeb, 0xb8, 0xd2, 0xaf, 0x4a, 0x71, 0x2e,
-        },
-        .pair_alg = 2,
-        .authenticated = 1,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_OOB,
-                .oob = {
-                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
-                    0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
-                },
-            },
-        },
-    };
-    ble_sm_test_util_us_lgcy_good(&params);
-}
-
-/**
- * Legacy pairing
- * Master: peer
- * Pair algorithm: passkey entry
- * Initiator IO capabilities: 4
- * Responder IO capabilities: 4
- * Bonding: true
- * Initiator address type: BLE_ADDR_TYPE_PUBLIC
- * Responder address type: BLE_ADDR_TYPE_PUBLIC
- * Initiator key distribution: 7
- * Responder key distribution: 7
- */
-TEST_CASE(ble_sm_lgcy_peer_pk_iio4_rio4_b1_iat0_rat0_ik7_rk7)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
-        },
-        .resp_id_addr = {
-            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-        },
-        .pair_req = {
-            .io_cap = 0x04,
-            .oob_data_flag = 0x00,
-            .authreq = 0x05,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_rsp = {
-            .io_cap = 0x04,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .confirm_req[0] = {
-            .value = {
-                0x93, 0x64, 0xb1, 0xb0, 0x07, 0x41, 0x22, 0xe7,
-                0x3e, 0x5a, 0x87, 0xf5, 0x1f, 0x25, 0x79, 0x11,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0x2d, 0x40, 0x15, 0xc4, 0x21, 0xeb, 0xd5, 0x73,
-                0xc8, 0x5d, 0xb8, 0xb9, 0x45, 0x31, 0xd5, 0x58,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0x8c, 0x2c, 0x3b, 0xf3, 0x90, 0xaa, 0x2e, 0xcf,
-                0xc7, 0x5b, 0xf6, 0xae, 0xb6, 0x4c, 0xc3, 0x61,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0x7a, 0x94, 0x97, 0x0a, 0xbe, 0xaf, 0xc0, 0x6b,
-                0xd4, 0xf4, 0x04, 0xd1, 0x21, 0x46, 0x34, 0xf3,
-            },
-        },
-        .enc_info_req = {
-            .ltk = {
-                0x3a, 0x10, 0xd1, 0xab, 0x13, 0xee, 0x16, 0xee,
-                0xcf, 0xae, 0xf1, 0x63, 0xf0, 0x6f, 0xb0, 0x89,
-            },
-        },
-        .master_id_req = {
-            .ediv = 0xb634,
-            .rand_val = 0xa99ac2007b4278a8,
-        },
-        .id_info_req = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_req = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-            },
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0x51, 0x4b, 0x7b, 0x31, 0xf7, 0xa6, 0x8a, 0x60,
-                0x4f, 0x10, 0x04, 0x5f, 0xb8, 0xee, 0xf6, 0xb3,
-            },
-        },
-        .enc_info_rsp = {
-            .ltk = {
-                0xa1, 0x1d, 0xdd, 0xee, 0x85, 0xcb, 0xe0, 0x48,
-                0x1e, 0xdd, 0xa4, 0x9d, 0xed, 0x3f, 0x15, 0x17,
-            },
-        },
-        .master_id_rsp = {
-            .ediv = 0x7e06,
-            .rand_val = 0xe6077f688c5ca67,
-        },
-        .id_info_rsp = {
-            .irk = {
-                0xeb, 0x8a, 0x06, 0xc4, 0x93, 0x51, 0x04, 0xb3,
-                0x8b, 0xbf, 0xe8, 0x1f, 0x0e, 0x96, 0x2a, 0x54,
-            },
-        },
-        .id_addr_info_rsp = {
-            .addr_type = 0,
-            .bd_addr = {
-                0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0x16, 0x7a, 0x2e, 0x9d, 0x43, 0x4d, 0x7b, 0x0b,
-                0x88, 0xe2, 0x11, 0xb0, 0x4d, 0xa1, 0xed, 0x08,
-            },
-        },
-        .stk = {
-            0x6c, 0x3e, 0x78, 0x47, 0xe8, 0x57, 0x9f, 0xe9,
-            0x3a, 0x8f, 0x0a, 0xbb, 0xd4, 0x60, 0xf6, 0x0d,
-        },
-        .pair_alg = 1,
-        .authenticated = true,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_INPUT,
-                .passkey = 449182,
-            },
-        },
-    };
-    ble_sm_test_util_peer_lgcy_good(&params);
-}
-
-TEST_SUITE(ble_sm_lgcy_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    /*** No privacy. */
-
-    /* Peer as initiator. */
-    ble_sm_lgcy_peer_jw_iio4_rio3_b1_iat0_rat0_ik7_rk7();
-    ble_sm_lgcy_peer_pk_iio4_rio0_b1_iat0_rat0_ik7_rk7();
-    ble_sm_lgcy_peer_pk_iio4_rio4_b1_iat0_rat0_ik7_rk7();
-
-    /* Us as initiator. */
-    ble_sm_lgcy_us_jw_iio3_rio3_b1_iat0_rat1_ik7_rk5();
-    ble_sm_lgcy_us_pk_iio4_rio2_b1_iat0_rat1_ik7_rk5();
-    ble_sm_lgcy_us_ob_iio3_rio3_b1_iat0_rat0_ik7_rk7();
-}
-
-#endif



[08/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_hs_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_test_util.c b/net/nimble/host/test/src/ble_hs_test_util.c
new file mode 100644
index 0000000..eed1262
--- /dev/null
+++ b/net/nimble/host/test/src/ble_hs_test_util.c
@@ -0,0 +1,1404 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include "stats/stats.h"
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
+#include "host/ble_hs_adv.h"
+#include "host/ble_hs_id.h"
+#include "transport/ram/ble_hci_ram.h"
+#include "ble_hs_test_util.h"
+
+/* Our global device address. */
+uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
+
+#define BLE_HS_TEST_UTIL_PUB_ADDR_VAL { 0x0a, 0x54, 0xab, 0x49, 0x7f, 0x06 }
+
+static const uint8_t ble_hs_test_util_pub_addr[BLE_DEV_ADDR_LEN] =
+    BLE_HS_TEST_UTIL_PUB_ADDR_VAL;
+
+#define BLE_HS_TEST_UTIL_LE_OPCODE(ocf) \
+    ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE, (ocf))
+
+struct os_eventq ble_hs_test_util_evq;
+
+static STAILQ_HEAD(, os_mbuf_pkthdr) ble_hs_test_util_prev_tx_queue;
+struct os_mbuf *ble_hs_test_util_prev_tx_cur;
+
+#define BLE_HS_TEST_UTIL_PREV_HCI_TX_CNT      64
+static uint8_t
+ble_hs_test_util_prev_hci_tx[BLE_HS_TEST_UTIL_PREV_HCI_TX_CNT][260];
+int ble_hs_test_util_num_prev_hci_txes;
+
+uint8_t ble_hs_test_util_cur_hci_tx[260];
+
+const struct ble_gap_adv_params ble_hs_test_util_adv_params = {
+    .conn_mode = BLE_GAP_CONN_MODE_UND,
+    .disc_mode = BLE_GAP_DISC_MODE_GEN,
+
+    .itvl_min = 0,
+    .itvl_max = 0,
+    .channel_map = 0,
+    .filter_policy = 0,
+    .high_duty_cycle = 0,
+};
+
+void
+ble_hs_test_util_prev_tx_enqueue(struct os_mbuf *om)
+{
+    struct os_mbuf_pkthdr *omp;
+
+    assert(OS_MBUF_IS_PKTHDR(om));
+
+    omp = OS_MBUF_PKTHDR(om);
+    if (STAILQ_EMPTY(&ble_hs_test_util_prev_tx_queue)) {
+        STAILQ_INSERT_HEAD(&ble_hs_test_util_prev_tx_queue, omp, omp_next);
+    } else {
+        STAILQ_INSERT_TAIL(&ble_hs_test_util_prev_tx_queue, omp, omp_next);
+    }
+}
+
+static struct os_mbuf *
+ble_hs_test_util_prev_tx_dequeue_once(struct hci_data_hdr *out_hci_hdr)
+{
+    struct os_mbuf_pkthdr *omp;
+    struct os_mbuf *om;
+    int rc;
+
+    omp = STAILQ_FIRST(&ble_hs_test_util_prev_tx_queue);
+    if (omp == NULL) {
+        return NULL;
+    }
+    STAILQ_REMOVE_HEAD(&ble_hs_test_util_prev_tx_queue, omp_next);
+
+    om = OS_MBUF_PKTHDR_TO_MBUF(omp);
+
+    rc = ble_hs_hci_util_data_hdr_strip(om, out_hci_hdr);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT_FATAL(out_hci_hdr->hdh_len == OS_MBUF_PKTLEN(om));
+
+    return om;
+}
+
+struct os_mbuf *
+ble_hs_test_util_prev_tx_dequeue(void)
+{
+    struct ble_l2cap_hdr l2cap_hdr;
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    uint8_t pb;
+    int rc;
+
+    os_mbuf_free_chain(ble_hs_test_util_prev_tx_cur);
+
+    om = ble_hs_test_util_prev_tx_dequeue_once(&hci_hdr);
+    if (om != NULL) {
+        pb = BLE_HCI_DATA_PB(hci_hdr.hdh_handle_pb_bc);
+        TEST_ASSERT_FATAL(pb == BLE_HCI_PB_FIRST_NON_FLUSH);
+
+        rc = ble_l2cap_parse_hdr(om, 0, &l2cap_hdr);
+        TEST_ASSERT_FATAL(rc == 0);
+
+        os_mbuf_adj(om, BLE_L2CAP_HDR_SZ);
+
+        ble_hs_test_util_prev_tx_cur = om;
+        while (OS_MBUF_PKTLEN(ble_hs_test_util_prev_tx_cur) <
+               l2cap_hdr.blh_len) {
+
+            om = ble_hs_test_util_prev_tx_dequeue_once(&hci_hdr);
+            TEST_ASSERT_FATAL(om != NULL);
+
+            pb = BLE_HCI_DATA_PB(hci_hdr.hdh_handle_pb_bc);
+            TEST_ASSERT_FATAL(pb == BLE_HCI_PB_MIDDLE);
+
+            os_mbuf_concat(ble_hs_test_util_prev_tx_cur, om);
+        }
+    } else {
+        ble_hs_test_util_prev_tx_cur = NULL;
+    }
+
+    return ble_hs_test_util_prev_tx_cur;
+}
+
+struct os_mbuf *
+ble_hs_test_util_prev_tx_dequeue_pullup(void)
+{
+    struct os_mbuf *om;
+
+    om = ble_hs_test_util_prev_tx_dequeue();
+    if (om != NULL) {
+        om = os_mbuf_pullup(om, OS_MBUF_PKTLEN(om));
+        TEST_ASSERT_FATAL(om != NULL);
+        ble_hs_test_util_prev_tx_cur = om;
+    }
+
+    return om;
+}
+
+int
+ble_hs_test_util_prev_tx_queue_sz(void)
+{
+    struct os_mbuf_pkthdr *omp;
+    int cnt;
+
+    cnt = 0;
+    STAILQ_FOREACH(omp, &ble_hs_test_util_prev_tx_queue, omp_next) {
+        cnt++;
+    }
+
+    return cnt;
+}
+
+void
+ble_hs_test_util_prev_tx_queue_clear(void)
+{
+    ble_hs_test_util_tx_all();
+    while (!STAILQ_EMPTY(&ble_hs_test_util_prev_tx_queue)) {
+        ble_hs_test_util_prev_tx_dequeue();
+    }
+}
+
+void *
+ble_hs_test_util_get_first_hci_tx(void)
+{
+    if (ble_hs_test_util_num_prev_hci_txes == 0) {
+        return NULL;
+    }
+
+    memcpy(ble_hs_test_util_cur_hci_tx, ble_hs_test_util_prev_hci_tx[0],
+           sizeof ble_hs_test_util_cur_hci_tx);
+
+    ble_hs_test_util_num_prev_hci_txes--;
+    if (ble_hs_test_util_num_prev_hci_txes > 0) {
+        memmove(
+            ble_hs_test_util_prev_hci_tx, ble_hs_test_util_prev_hci_tx + 1,
+            sizeof ble_hs_test_util_prev_hci_tx[0] *
+            ble_hs_test_util_num_prev_hci_txes);
+    }
+
+    return ble_hs_test_util_cur_hci_tx;
+}
+
+void *
+ble_hs_test_util_get_last_hci_tx(void)
+{
+    if (ble_hs_test_util_num_prev_hci_txes == 0) {
+        return NULL;
+    }
+
+    ble_hs_test_util_num_prev_hci_txes--;
+    memcpy(ble_hs_test_util_cur_hci_tx,
+           ble_hs_test_util_prev_hci_tx + ble_hs_test_util_num_prev_hci_txes,
+           sizeof ble_hs_test_util_cur_hci_tx);
+
+    return ble_hs_test_util_cur_hci_tx;
+}
+
+void
+ble_hs_test_util_enqueue_hci_tx(void *cmd)
+{
+    TEST_ASSERT_FATAL(ble_hs_test_util_num_prev_hci_txes <
+                      BLE_HS_TEST_UTIL_PREV_HCI_TX_CNT);
+    memcpy(ble_hs_test_util_prev_hci_tx + ble_hs_test_util_num_prev_hci_txes,
+           cmd, 260);
+
+    ble_hs_test_util_num_prev_hci_txes++;
+}
+
+void
+ble_hs_test_util_prev_hci_tx_clear(void)
+{
+    ble_hs_test_util_num_prev_hci_txes = 0;
+}
+
+static void
+ble_hs_test_util_rx_hci_evt(uint8_t *evt)
+{
+    uint8_t *evbuf;
+    int totlen;
+    int rc;
+
+    totlen = BLE_HCI_EVENT_HDR_LEN + evt[1];
+    TEST_ASSERT_FATAL(totlen <= UINT8_MAX + BLE_HCI_EVENT_HDR_LEN);
+
+    if (os_started()) {
+        evbuf = ble_hci_trans_buf_alloc(
+            BLE_HCI_TRANS_BUF_EVT_LO);
+        TEST_ASSERT_FATAL(evbuf != NULL);
+
+        memcpy(evbuf, evt, totlen);
+        rc = ble_hci_trans_ll_evt_tx(evbuf);
+    } else {
+        rc = ble_hs_hci_evt_process(evt);
+    }
+
+    TEST_ASSERT_FATAL(rc == 0);
+}
+
+void
+ble_hs_test_util_build_cmd_complete(uint8_t *dst, int len,
+                                    uint8_t param_len, uint8_t num_pkts,
+                                    uint16_t opcode)
+{
+    TEST_ASSERT(len >= BLE_HCI_EVENT_CMD_COMPLETE_HDR_LEN);
+
+    dst[0] = BLE_HCI_EVCODE_COMMAND_COMPLETE;
+    dst[1] = 3 + param_len;
+    dst[2] = num_pkts;
+    htole16(dst + 3, opcode);
+}
+
+void
+ble_hs_test_util_build_cmd_status(uint8_t *dst, int len,
+                                  uint8_t status, uint8_t num_pkts,
+                                  uint16_t opcode)
+{
+    TEST_ASSERT(len >= BLE_HCI_EVENT_CMD_STATUS_LEN);
+
+    dst[0] = BLE_HCI_EVCODE_COMMAND_STATUS;
+    dst[1] = BLE_HCI_EVENT_CMD_STATUS_LEN;
+    dst[2] = status;
+    dst[3] = num_pkts;
+    htole16(dst + 4, opcode);
+}
+
+#define BLE_HS_TEST_UTIL_PHONY_ACK_MAX  64
+struct ble_hs_test_util_phony_ack {
+    uint16_t opcode;
+    uint8_t status;
+    uint8_t evt_params[256];
+    uint8_t evt_params_len;
+};
+
+static struct ble_hs_test_util_phony_ack
+ble_hs_test_util_phony_acks[BLE_HS_TEST_UTIL_PHONY_ACK_MAX];
+static int ble_hs_test_util_num_phony_acks;
+
+static int
+ble_hs_test_util_phony_ack_cb(uint8_t *ack, int ack_buf_len)
+{
+    struct ble_hs_test_util_phony_ack *entry;
+
+    if (ble_hs_test_util_num_phony_acks == 0) {
+        return BLE_HS_ETIMEOUT_HCI;
+    }
+
+    entry = ble_hs_test_util_phony_acks;
+
+    ble_hs_test_util_build_cmd_complete(ack, 256,
+                                        entry->evt_params_len + 1, 1,
+                                        entry->opcode);
+    ack[BLE_HCI_EVENT_CMD_COMPLETE_HDR_LEN] = entry->status;
+    memcpy(ack + BLE_HCI_EVENT_CMD_COMPLETE_HDR_LEN + 1, entry->evt_params,
+           entry->evt_params_len);
+
+    ble_hs_test_util_num_phony_acks--;
+    if (ble_hs_test_util_num_phony_acks > 0) {
+        memmove(ble_hs_test_util_phony_acks, ble_hs_test_util_phony_acks + 1,
+                sizeof *entry * ble_hs_test_util_num_phony_acks);
+    }
+
+    return 0;
+}
+
+void
+ble_hs_test_util_set_ack_params(uint16_t opcode, uint8_t status, void *params,
+                                uint8_t params_len)
+{
+    struct ble_hs_test_util_phony_ack *ack;
+
+    ack = ble_hs_test_util_phony_acks + 0;
+    ack->opcode = opcode;
+    ack->status = status;
+
+    if (params == NULL || params_len == 0) {
+        ack->evt_params_len = 0;
+    } else {
+        memcpy(ack->evt_params, params, params_len);
+        ack->evt_params_len = params_len;
+    }
+    ble_hs_test_util_num_phony_acks = 1;
+
+    ble_hs_hci_set_phony_ack_cb(ble_hs_test_util_phony_ack_cb);
+}
+
+void
+ble_hs_test_util_set_ack(uint16_t opcode, uint8_t status)
+{
+    ble_hs_test_util_set_ack_params(opcode, status, NULL, 0);
+}
+
+static void
+ble_hs_test_util_set_ack_seq(struct ble_hs_test_util_phony_ack *acks)
+{
+    int i;
+
+    for (i = 0; acks[i].opcode != 0; i++) {
+        ble_hs_test_util_phony_acks[i] = acks[i];
+    }
+    ble_hs_test_util_num_phony_acks = i;
+
+    ble_hs_hci_set_phony_ack_cb(ble_hs_test_util_phony_ack_cb);
+}
+
+void
+ble_hs_test_util_create_rpa_conn(uint16_t handle, uint8_t own_addr_type,
+                                 const uint8_t *our_rpa,
+                                 uint8_t peer_addr_type,
+                                 const uint8_t *peer_id_addr,
+                                 const uint8_t *peer_rpa,
+                                 ble_gap_event_fn *cb, void *cb_arg)
+{
+    struct hci_le_conn_complete evt;
+    int rc;
+
+    ble_hs_test_util_connect(own_addr_type, peer_addr_type,
+                             peer_id_addr, 0, NULL, cb, cb_arg, 0);
+
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_SUCCESS;
+    evt.connection_handle = handle;
+    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
+    evt.peer_addr_type = peer_addr_type;
+    memcpy(evt.peer_addr, peer_id_addr, 6);
+    evt.conn_itvl = BLE_GAP_INITIAL_CONN_ITVL_MAX;
+    evt.conn_latency = BLE_GAP_INITIAL_CONN_LATENCY;
+    evt.supervision_timeout = BLE_GAP_INITIAL_SUPERVISION_TIMEOUT;
+    memcpy(evt.local_rpa, our_rpa, 6);
+    memcpy(evt.peer_rpa, peer_rpa, 6);
+
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_test_util_prev_hci_tx_clear();
+}
+
+void
+ble_hs_test_util_create_conn(uint16_t handle, uint8_t *peer_id_addr,
+                             ble_gap_event_fn *cb, void *cb_arg)
+{
+    static uint8_t null_addr[6];
+
+    ble_hs_test_util_create_rpa_conn(handle, BLE_ADDR_TYPE_PUBLIC, null_addr,
+                                     BLE_ADDR_TYPE_PUBLIC, peer_id_addr,
+                                     null_addr, cb, cb_arg);
+}
+
+static void
+ble_hs_test_util_conn_params_dflt(struct ble_gap_conn_params *conn_params)
+{
+    conn_params->scan_itvl = 0x0010;
+    conn_params->scan_window = 0x0010;
+    conn_params->itvl_min = BLE_GAP_INITIAL_CONN_ITVL_MIN;
+    conn_params->itvl_max = BLE_GAP_INITIAL_CONN_ITVL_MAX;
+    conn_params->latency = BLE_GAP_INITIAL_CONN_LATENCY;
+    conn_params->supervision_timeout = BLE_GAP_INITIAL_SUPERVISION_TIMEOUT;
+    conn_params->min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
+    conn_params->max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
+}
+
+static void
+ble_hs_test_util_hcc_from_conn_params(
+    struct hci_create_conn *hcc, uint8_t own_addr_type, uint8_t peer_addr_type,
+    const uint8_t *peer_addr, const struct ble_gap_conn_params *conn_params)
+{
+    hcc->scan_itvl = conn_params->scan_itvl;
+    hcc->scan_window = conn_params->scan_window;
+
+    if (peer_addr_type == BLE_GAP_ADDR_TYPE_WL) {
+        hcc->filter_policy = BLE_HCI_CONN_FILT_USE_WL;
+        hcc->peer_addr_type = 0;
+        memset(hcc->peer_addr, 0, 6);
+    } else {
+        hcc->filter_policy = BLE_HCI_CONN_FILT_NO_WL;
+        hcc->peer_addr_type = peer_addr_type;
+        memcpy(hcc->peer_addr, peer_addr, 6);
+    }
+    hcc->own_addr_type = own_addr_type;
+    hcc->conn_itvl_min = conn_params->itvl_min;
+    hcc->conn_itvl_max = conn_params->itvl_max;
+    hcc->conn_latency = conn_params->latency;
+    hcc->supervision_timeout = conn_params->supervision_timeout;
+    hcc->min_ce_len = conn_params->min_ce_len;
+    hcc->max_ce_len = conn_params->max_ce_len;
+}
+
+void
+ble_hs_test_util_verify_tx_create_conn(const struct hci_create_conn *exp)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_CREATE_CONN,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_CREATE_CONN_LEN);
+
+    TEST_ASSERT(le16toh(param + 0) == exp->scan_itvl);
+    TEST_ASSERT(le16toh(param + 2) == exp->scan_window);
+    TEST_ASSERT(param[4] == exp->filter_policy);
+    TEST_ASSERT(param[5] == exp->peer_addr_type);
+    TEST_ASSERT(memcmp(param + 6, exp->peer_addr, 6) == 0);
+    TEST_ASSERT(param[12] == exp->own_addr_type);
+    TEST_ASSERT(le16toh(param + 13) == exp->conn_itvl_min);
+    TEST_ASSERT(le16toh(param + 15) == exp->conn_itvl_max);
+    TEST_ASSERT(le16toh(param + 17) == exp->conn_latency);
+    TEST_ASSERT(le16toh(param + 19) == exp->supervision_timeout);
+    TEST_ASSERT(le16toh(param + 21) == exp->min_ce_len);
+    TEST_ASSERT(le16toh(param + 23) == exp->max_ce_len);
+}
+
+int
+ble_hs_test_util_connect(uint8_t own_addr_type, uint8_t peer_addr_type,
+                         const uint8_t *peer_addr, int32_t duration_ms,
+                         const struct ble_gap_conn_params *params,
+                         ble_gap_event_fn *cb, void *cb_arg,
+                         uint8_t ack_status)
+{
+    struct ble_gap_conn_params dflt_params;
+    struct hci_create_conn hcc;
+    int rc;
+
+    /* This function ensures the most recently sent HCI command is the expected
+     * create connection command.  If the current test case has unverified HCI
+     * commands, assume we are not interested in them and clear the queue.
+     */
+    ble_hs_test_util_prev_hci_tx_clear();
+
+    ble_hs_test_util_set_ack(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_CREATE_CONN),
+        ack_status);
+
+    rc = ble_gap_connect(own_addr_type, peer_addr_type, peer_addr, duration_ms,
+                         params, cb, cb_arg);
+
+    TEST_ASSERT(rc == BLE_HS_HCI_ERR(ack_status));
+
+    if (params == NULL) {
+        ble_hs_test_util_conn_params_dflt(&dflt_params);
+        params = &dflt_params;
+    }
+
+    ble_hs_test_util_hcc_from_conn_params(&hcc, own_addr_type,
+                                          peer_addr_type, peer_addr, params);
+    ble_hs_test_util_verify_tx_create_conn(&hcc);
+
+    return rc;
+}
+
+int
+ble_hs_test_util_conn_cancel(uint8_t ack_status)
+{
+    int rc;
+
+    ble_hs_test_util_set_ack(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_CREATE_CONN_CANCEL),
+        ack_status);
+
+    rc = ble_gap_conn_cancel();
+    return rc;
+}
+
+void
+ble_hs_test_util_conn_cancel_full(void)
+{
+    struct hci_le_conn_complete evt;
+    int rc;
+
+    ble_hs_test_util_conn_cancel(0);
+
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_UNK_CONN_ID;
+    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
+
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT_FATAL(rc == 0);
+}
+
+int
+ble_hs_test_util_conn_terminate(uint16_t conn_handle, uint8_t hci_status)
+{
+    int rc;
+
+    ble_hs_test_util_set_ack(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LINK_CTRL,
+                                    BLE_HCI_OCF_DISCONNECT_CMD),
+        hci_status);
+
+    rc = ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
+    return rc;
+}
+
+void
+ble_hs_test_util_conn_disconnect(uint16_t conn_handle)
+{
+    struct hci_disconn_complete evt;
+    int rc;
+
+    rc = ble_hs_test_util_conn_terminate(conn_handle, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Receive disconnection complete event. */
+    evt.connection_handle = conn_handle;
+    evt.status = 0;
+    evt.reason = BLE_ERR_CONN_TERM_LOCAL;
+    ble_gap_rx_disconn_complete(&evt);
+}
+
+int
+ble_hs_test_util_exp_hci_status(int cmd_idx, int fail_idx, uint8_t fail_status)
+{
+    if (cmd_idx == fail_idx) {
+        return BLE_HS_HCI_ERR(fail_status);
+    } else {
+        return 0;
+    }
+}
+
+int
+ble_hs_test_util_disc(uint8_t own_addr_type, int32_t duration_ms,
+                      const struct ble_gap_disc_params *disc_params,
+                      ble_gap_event_fn *cb, void *cb_arg, int fail_idx,
+                      uint8_t fail_status)
+{
+    int rc;
+
+    ble_hs_test_util_set_ack_seq(((struct ble_hs_test_util_phony_ack[]) {
+        {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_SCAN_PARAMS),
+            ble_hs_test_util_exp_hci_status(0, fail_idx, fail_status),
+        },
+        {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
+            ble_hs_test_util_exp_hci_status(1, fail_idx, fail_status),
+        },
+
+        { 0 }
+    }));
+
+    rc = ble_gap_disc(own_addr_type, duration_ms, disc_params,
+                      cb, cb_arg);
+    return rc;
+}
+
+int
+ble_hs_test_util_disc_cancel(uint8_t ack_status)
+{
+    int rc;
+
+    ble_hs_test_util_set_ack(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
+        ack_status);
+
+    rc = ble_gap_disc_cancel();
+    return rc;
+}
+
+static void
+ble_hs_test_util_verify_tx_rd_pwr(void)
+{
+    uint8_t param_len;
+
+    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                   BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR,
+                                   &param_len);
+    TEST_ASSERT(param_len == 0);
+}
+
+int
+ble_hs_test_util_adv_set_fields(struct ble_hs_adv_fields *adv_fields,
+                                uint8_t hci_status)
+{
+    int auto_pwr;
+    int rc;
+
+    auto_pwr = adv_fields->tx_pwr_lvl_is_present &&
+               adv_fields->tx_pwr_lvl == BLE_HS_ADV_TX_PWR_LVL_AUTO;
+
+    if (auto_pwr) {
+        ble_hs_test_util_set_ack_params(
+            ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                        BLE_HCI_OCF_LE_RD_ADV_CHAN_TXPWR),
+            hci_status,
+            ((uint8_t[1]){0}), 1);
+    }
+
+    rc = ble_gap_adv_set_fields(adv_fields);
+    if (rc == 0 && auto_pwr) {
+        /* Verify tx of set advertising params command. */
+        ble_hs_test_util_verify_tx_rd_pwr();
+    }
+
+    return rc;
+}
+
+int
+ble_hs_test_util_adv_start(uint8_t own_addr_type,
+                           uint8_t peer_addr_type, const uint8_t *peer_addr, 
+                           const struct ble_gap_adv_params *adv_params,
+                           ble_gap_event_fn *cb, void *cb_arg,
+                           int fail_idx, uint8_t fail_status)
+{
+    struct ble_hs_test_util_phony_ack acks[6];
+    int rc;
+    int i;
+
+    i = 0;
+
+    acks[i] = (struct ble_hs_test_util_phony_ack) {
+        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_PARAMS),
+        fail_idx == i ? fail_status : 0,
+    };
+    i++;
+
+    if (adv_params->conn_mode != BLE_GAP_CONN_MODE_DIR) {
+        acks[i] = (struct ble_hs_test_util_phony_ack) {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_DATA),
+            ble_hs_test_util_exp_hci_status(i, fail_idx, fail_status),
+        };
+        i++;
+
+        acks[i] = (struct ble_hs_test_util_phony_ack) {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA),
+            ble_hs_test_util_exp_hci_status(i, fail_idx, fail_status),
+        };
+        i++;
+    }
+
+    acks[i] = (struct ble_hs_test_util_phony_ack) {
+        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_ENABLE),
+        ble_hs_test_util_exp_hci_status(i, fail_idx, fail_status),
+    };
+    i++;
+
+    memset(acks + i, 0, sizeof acks[i]);
+
+    ble_hs_test_util_set_ack_seq(acks);
+    
+    rc = ble_gap_adv_start(own_addr_type, peer_addr_type, peer_addr, 
+                           BLE_HS_FOREVER, adv_params, cb, cb_arg);
+
+    return rc;
+}
+
+int
+ble_hs_test_util_adv_stop(uint8_t hci_status)
+{
+    int rc;
+
+    ble_hs_test_util_set_ack(
+        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADV_ENABLE),
+        hci_status);
+
+    rc = ble_gap_adv_stop();
+    return rc;
+}
+
+int
+ble_hs_test_util_wl_set(struct ble_gap_white_entry *white_list,
+                        uint8_t white_list_count,
+                        int fail_idx, uint8_t fail_status)
+{
+    struct ble_hs_test_util_phony_ack acks[64];
+    int cmd_idx;
+    int rc;
+    int i;
+
+    TEST_ASSERT_FATAL(white_list_count < 63);
+
+    cmd_idx = 0;
+    acks[cmd_idx] = (struct ble_hs_test_util_phony_ack) {
+        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_CLEAR_WHITE_LIST),
+        ble_hs_test_util_exp_hci_status(cmd_idx, fail_idx, fail_status),
+    };
+    cmd_idx++;
+
+    for (i = 0; i < white_list_count; i++) {
+        acks[cmd_idx] = (struct ble_hs_test_util_phony_ack) {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_ADD_WHITE_LIST),
+            ble_hs_test_util_exp_hci_status(cmd_idx, fail_idx, fail_status),
+        };
+
+        cmd_idx++;
+    }
+    memset(acks + cmd_idx, 0, sizeof acks[cmd_idx]);
+
+    ble_hs_test_util_set_ack_seq(acks);
+    rc = ble_gap_wl_set(white_list, white_list_count);
+    return rc;
+}
+
+int
+ble_hs_test_util_conn_update(uint16_t conn_handle,
+                             struct ble_gap_upd_params *params,
+                             uint8_t hci_status)
+{
+    int rc;
+
+    ble_hs_test_util_set_ack(
+        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_CONN_UPDATE), hci_status);
+
+    rc = ble_gap_update_params(conn_handle, params);
+    return rc;
+}
+
+int
+ble_hs_test_util_set_our_irk(const uint8_t *irk, int fail_idx,
+                             uint8_t hci_status)
+{
+    int rc;
+
+    ble_hs_test_util_set_ack_seq(((struct ble_hs_test_util_phony_ack[]) {
+        {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
+            ble_hs_test_util_exp_hci_status(0, fail_idx, hci_status),
+        },
+        {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_CLR_RESOLV_LIST),
+            ble_hs_test_util_exp_hci_status(1, fail_idx, hci_status),
+        },
+        {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
+            ble_hs_test_util_exp_hci_status(2, fail_idx, hci_status),
+        },
+        {
+            BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_ADD_RESOLV_LIST),
+            ble_hs_test_util_exp_hci_status(3, fail_idx, hci_status),
+        },
+        {
+            0
+        }
+    }));
+
+    rc = ble_hs_pvcy_set_our_irk(irk);
+    return rc;
+}
+
+int
+ble_hs_test_util_security_initiate(uint16_t conn_handle, uint8_t hci_status)
+{
+    int rc;
+
+    ble_hs_test_util_set_ack(
+        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_START_ENCRYPT), hci_status);
+
+    rc = ble_gap_security_initiate(conn_handle);
+    return rc;
+}
+
+int
+ble_hs_test_util_l2cap_rx_first_frag(uint16_t conn_handle, uint16_t cid,
+                                     struct hci_data_hdr *hci_hdr,
+                                     struct os_mbuf *om)
+{
+    int rc;
+
+    om = ble_l2cap_prepend_hdr(om, cid, OS_MBUF_PKTLEN(om));
+    TEST_ASSERT_FATAL(om != NULL);
+
+    rc = ble_hs_test_util_l2cap_rx(conn_handle, hci_hdr, om);
+    return rc;
+}
+
+int
+ble_hs_test_util_l2cap_rx(uint16_t conn_handle,
+                          struct hci_data_hdr *hci_hdr,
+                          struct os_mbuf *om)
+{
+    struct ble_hs_conn *conn;
+    ble_l2cap_rx_fn *rx_cb;
+    struct os_mbuf *rx_buf;
+    int rc;
+
+    ble_hs_lock();
+
+    conn = ble_hs_conn_find(conn_handle);
+    if (conn != NULL) {
+        rc = ble_l2cap_rx(conn, hci_hdr, om, &rx_cb, &rx_buf);
+    } else {
+        os_mbuf_free_chain(om);
+    }
+
+    ble_hs_unlock();
+
+    if (conn == NULL) {
+        rc = BLE_HS_ENOTCONN;
+    } else if (rc == 0) {
+        TEST_ASSERT_FATAL(rx_cb != NULL);
+        TEST_ASSERT_FATAL(rx_buf != NULL);
+        rc = rx_cb(conn_handle, &rx_buf);
+        os_mbuf_free_chain(rx_buf);
+    } else if (rc == BLE_HS_EAGAIN) {
+        /* More fragments on the way. */
+        rc = 0;
+    }
+
+    return rc;
+}
+
+int
+ble_hs_test_util_l2cap_rx_payload_flat(uint16_t conn_handle, uint16_t cid,
+                                       const void *data, int len)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    int rc;
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    rc = os_mbuf_append(om, data, len);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    hci_hdr.hdh_handle_pb_bc =
+        ble_hs_hci_util_handle_pb_bc_join(conn_handle,
+                                          BLE_HCI_PB_FIRST_FLUSH, 0);
+    hci_hdr.hdh_len = OS_MBUF_PKTHDR(om)->omp_len;
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, cid, &hci_hdr, om);
+    return rc;
+}
+
+void
+ble_hs_test_util_rx_att_err_rsp(uint16_t conn_handle, uint8_t req_op,
+                                uint8_t error_code, uint16_t err_handle)
+{
+    struct ble_att_error_rsp rsp;
+    uint8_t buf[BLE_ATT_ERROR_RSP_SZ];
+    int rc;
+
+    rsp.baep_req_op = req_op;
+    rsp.baep_handle = err_handle;
+    rsp.baep_error_code = error_code;
+
+    ble_att_error_rsp_write(buf, sizeof buf, &rsp);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+}
+
+void
+ble_hs_test_util_set_startup_acks(void)
+{
+    /* Receive acknowledgements for the startup sequence.  We sent the
+     * corresponding requests when the host task was started.
+     */
+    ble_hs_test_util_set_ack_seq(((struct ble_hs_test_util_phony_ack[]) {
+        {
+            .opcode = ble_hs_hci_util_opcode_join(BLE_HCI_OGF_CTLR_BASEBAND,
+                                                  BLE_HCI_OCF_CB_RESET),
+        },
+        {
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_SET_EVENT_MASK),
+        },
+        {
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_CTLR_BASEBAND, BLE_HCI_OCF_CB_SET_EVENT_MASK2),
+        },
+        {
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_EVENT_MASK),
+        },
+        {
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_BUF_SIZE),
+            /* Use a very low buffer size (16) to test fragmentation. */
+            .evt_params = { 0x10, 0x00, 0x20 },
+            .evt_params_len = 3,
+        },
+        {
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_RD_LOC_SUPP_FEAT),
+            .evt_params = { 0 },
+            .evt_params_len = 8,
+        },
+        {
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_INFO_PARAMS, BLE_HCI_OCF_IP_RD_BD_ADDR),
+            .evt_params = BLE_HS_TEST_UTIL_PUB_ADDR_VAL,
+            .evt_params_len = 6,
+        },
+        {
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
+        },
+        {
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_CLR_RESOLV_LIST),
+        },
+        {
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_SET_ADDR_RES_EN),
+        },
+        {
+            .opcode = ble_hs_hci_util_opcode_join(
+                BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_ADD_RESOLV_LIST),
+        },
+        { 0 }
+    }));
+}
+
+void
+ble_hs_test_util_rx_num_completed_pkts_event(
+    struct ble_hs_test_util_num_completed_pkts_entry *entries)
+{
+    struct ble_hs_test_util_num_completed_pkts_entry *entry;
+    uint8_t buf[1024];
+    int num_entries;
+    int off;
+    int i;
+
+    /* Count number of entries. */
+    num_entries = 0;
+    for (entry = entries; entry->handle_id != 0; entry++) {
+        num_entries++;
+    }
+    TEST_ASSERT_FATAL(num_entries <= UINT8_MAX);
+
+    buf[0] = BLE_HCI_EVCODE_NUM_COMP_PKTS;
+    buf[2] = num_entries;
+
+    off = 3;
+    for (i = 0; i < num_entries; i++) {
+        htole16(buf + off, entries[i].handle_id);
+        off += 2;
+    }
+    for (i = 0; i < num_entries; i++) {
+        htole16(buf + off, entries[i].num_pkts);
+        off += 2;
+    }
+
+    buf[1] = off - 2;
+
+    ble_hs_test_util_rx_hci_evt(buf);
+}
+
+void
+ble_hs_test_util_rx_disconn_complete_event(struct hci_disconn_complete *evt)
+{
+    uint8_t buf[BLE_HCI_EVENT_HDR_LEN + BLE_HCI_EVENT_DISCONN_COMPLETE_LEN];
+
+    buf[0] = BLE_HCI_EVCODE_DISCONN_CMP;
+    buf[1] = BLE_HCI_EVENT_DISCONN_COMPLETE_LEN;
+    buf[2] = evt->status;
+    htole16(buf + 3, evt->connection_handle);
+    buf[5] = evt->reason;
+
+    ble_hs_test_util_rx_hci_evt(buf);
+}
+
+uint8_t *
+ble_hs_test_util_verify_tx_hci(uint8_t ogf, uint16_t ocf,
+                               uint8_t *out_param_len)
+{
+    uint16_t opcode;
+    uint8_t *cmd;
+
+    cmd = ble_hs_test_util_get_first_hci_tx();
+    TEST_ASSERT_FATAL(cmd != NULL);
+
+    opcode = le16toh(cmd);
+    TEST_ASSERT(BLE_HCI_OGF(opcode) == ogf);
+    TEST_ASSERT(BLE_HCI_OCF(opcode) == ocf);
+
+    if (out_param_len != NULL) {
+        *out_param_len = cmd[2];
+    }
+
+    return cmd + 3;
+}
+
+void
+ble_hs_test_util_tx_all(void)
+{
+    ble_hs_process_tx_data_queue();
+}
+
+void
+ble_hs_test_util_verify_tx_prep_write(uint16_t attr_handle, uint16_t offset,
+                                      const void *data, int data_len)
+{
+    struct ble_att_prep_write_cmd req;
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+    om = ble_hs_test_util_prev_tx_dequeue();
+    TEST_ASSERT_FATAL(om != NULL);
+    TEST_ASSERT(OS_MBUF_PKTLEN(om) ==
+                BLE_ATT_PREP_WRITE_CMD_BASE_SZ + data_len);
+
+    om = os_mbuf_pullup(om, BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
+    TEST_ASSERT_FATAL(om != NULL);
+
+    ble_att_prep_write_req_parse(om->om_data, om->om_len, &req);
+    TEST_ASSERT(req.bapc_handle == attr_handle);
+    TEST_ASSERT(req.bapc_offset == offset);
+    TEST_ASSERT(os_mbuf_cmpf(om, BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
+                             data, data_len) == 0);
+}
+
+void
+ble_hs_test_util_verify_tx_exec_write(uint8_t expected_flags)
+{
+    struct ble_att_exec_write_req req;
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+    TEST_ASSERT(om->om_len == BLE_ATT_EXEC_WRITE_REQ_SZ);
+
+    ble_att_exec_write_req_parse(om->om_data, om->om_len, &req);
+    TEST_ASSERT(req.baeq_flags == expected_flags);
+}
+
+void
+ble_hs_test_util_verify_tx_read_rsp_gen(uint8_t att_op,
+                                        uint8_t *attr_data, int attr_len)
+{
+    struct os_mbuf *om;
+    uint8_t u8;
+    int rc;
+    int i;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue();
+
+    rc = os_mbuf_copydata(om, 0, 1, &u8);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(u8 == att_op);
+
+    for (i = 0; i < attr_len; i++) {
+        rc = os_mbuf_copydata(om, i + 1, 1, &u8);
+        TEST_ASSERT(rc == 0);
+        TEST_ASSERT(u8 == attr_data[i]);
+    }
+
+    rc = os_mbuf_copydata(om, i + 1, 1, &u8);
+    TEST_ASSERT(rc != 0);
+}
+
+void
+ble_hs_test_util_verify_tx_read_rsp(uint8_t *attr_data, int attr_len)
+{
+    ble_hs_test_util_verify_tx_read_rsp_gen(BLE_ATT_OP_READ_RSP,
+                                            attr_data, attr_len);
+}
+
+void
+ble_hs_test_util_verify_tx_read_blob_rsp(uint8_t *attr_data, int attr_len)
+{
+    ble_hs_test_util_verify_tx_read_rsp_gen(BLE_ATT_OP_READ_BLOB_RSP,
+                                            attr_data, attr_len);
+}
+
+void
+ble_hs_test_util_set_static_rnd_addr(void)
+{
+    uint8_t addr[6] = { 1, 2, 3, 4, 5, 0xc1 };
+    int rc;
+
+    ble_hs_test_util_set_ack(
+        BLE_HS_TEST_UTIL_LE_OPCODE(BLE_HCI_OCF_LE_SET_RAND_ADDR), 0);
+
+    rc = ble_hs_id_set_rnd(addr);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_hs_test_util_get_first_hci_tx();
+}
+
+struct os_mbuf *
+ble_hs_test_util_om_from_flat(const void *buf, uint16_t len)
+{
+    struct os_mbuf *om;
+
+    om = ble_hs_mbuf_from_flat(buf, len);
+    TEST_ASSERT_FATAL(om != NULL);
+
+    return om;
+}
+
+int
+ble_hs_test_util_flat_attr_cmp(const struct ble_hs_test_util_flat_attr *a,
+                               const struct ble_hs_test_util_flat_attr *b)
+{
+    if (a->handle != b->handle) {
+        return -1;
+    }
+    if (a->offset != b->offset) {
+        return -1;
+    }
+    if (a->value_len != b->value_len) {
+        return -1;
+    }
+    return memcmp(a->value, b->value, a->value_len);
+}
+
+void
+ble_hs_test_util_attr_to_flat(struct ble_hs_test_util_flat_attr *flat,
+                              const struct ble_gatt_attr *attr)
+{
+    int rc;
+
+    flat->handle = attr->handle;
+    flat->offset = attr->offset;
+    rc = ble_hs_mbuf_to_flat(attr->om, flat->value, sizeof flat->value,
+                           &flat->value_len);
+    TEST_ASSERT_FATAL(rc == 0);
+}
+
+void
+ble_hs_test_util_attr_from_flat(struct ble_gatt_attr *attr,
+                                const struct ble_hs_test_util_flat_attr *flat)
+{
+    attr->handle = flat->handle;
+    attr->offset = flat->offset;
+    attr->om = ble_hs_test_util_om_from_flat(flat->value, flat->value_len);
+}
+
+int
+ble_hs_test_util_read_local_flat(uint16_t attr_handle, uint16_t max_len,
+                                 void *buf, uint16_t *out_len)
+{
+    struct os_mbuf *om;
+    int rc;
+
+    rc = ble_att_svr_read_local(attr_handle, &om);
+    if (rc != 0) {
+        return rc;
+    }
+
+    TEST_ASSERT_FATAL(OS_MBUF_PKTLEN(om) <= max_len);
+
+    rc = os_mbuf_copydata(om, 0, OS_MBUF_PKTLEN(om), buf);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    *out_len = OS_MBUF_PKTLEN(om);
+
+    os_mbuf_free_chain(om);
+    return 0;
+}
+
+int
+ble_hs_test_util_write_local_flat(uint16_t attr_handle,
+                                  const void *buf, uint16_t buf_len)
+{
+    struct os_mbuf *om;
+    int rc;
+
+    om = ble_hs_test_util_om_from_flat(buf, buf_len);
+    rc = ble_att_svr_write_local(attr_handle, om);
+    return rc;
+}
+
+int
+ble_hs_test_util_gatt_write_flat(uint16_t conn_handle, uint16_t attr_handle,
+                                 const void *data, uint16_t data_len,
+                                 ble_gatt_attr_fn *cb, void *cb_arg)
+{
+    struct os_mbuf *om;
+    int rc;
+
+    om = ble_hs_test_util_om_from_flat(data, data_len);
+    rc = ble_gattc_write(conn_handle, attr_handle, om, cb, cb_arg);
+
+    return rc;
+}
+
+int
+ble_hs_test_util_gatt_write_no_rsp_flat(uint16_t conn_handle,
+                                        uint16_t attr_handle,
+                                        const void *data, uint16_t data_len)
+{
+    struct os_mbuf *om;
+    int rc;
+
+    om = ble_hs_test_util_om_from_flat(data, data_len);
+    rc = ble_gattc_write_no_rsp(conn_handle, attr_handle, om);
+
+    return rc;
+}
+
+int
+ble_hs_test_util_gatt_write_long_flat(uint16_t conn_handle,
+                                      uint16_t attr_handle,
+                                      const void *data, uint16_t data_len,
+                                      ble_gatt_attr_fn *cb, void *cb_arg)
+{
+    struct os_mbuf *om;
+    int rc;
+
+    om = ble_hs_test_util_om_from_flat(data, data_len);
+    rc = ble_gattc_write_long(conn_handle, attr_handle, om, cb, cb_arg);
+
+    return rc;
+}
+
+static int
+ble_hs_test_util_mbuf_chain_len(const struct os_mbuf *om)
+{
+    int count;
+
+    count = 0;
+    while (om != NULL) {
+        count++;
+        om = SLIST_NEXT(om, om_next);
+    }
+
+    return count;
+}
+
+int
+ble_hs_test_util_mbuf_count(const struct ble_hs_test_util_mbuf_params *params)
+{
+    const struct ble_att_prep_entry *prep;
+    const struct os_mbuf_pkthdr *omp;
+    const struct ble_l2cap_chan *chan;
+    const struct ble_hs_conn *conn;
+    const struct os_mbuf *om;
+    int count;
+    int i;
+
+    ble_hs_process_tx_data_queue();
+    ble_hs_process_rx_data_queue();
+
+    count = os_msys_num_free();
+
+    if (params->prev_tx) {
+        count += ble_hs_test_util_mbuf_chain_len(ble_hs_test_util_prev_tx_cur);
+        STAILQ_FOREACH(omp, &ble_hs_test_util_prev_tx_queue, omp_next) {
+            om = OS_MBUF_PKTHDR_TO_MBUF(omp);
+            count += ble_hs_test_util_mbuf_chain_len(om);
+        }
+    }
+
+    ble_hs_lock();
+    for (i = 0; ; i++) {
+        conn = ble_hs_conn_find_by_idx(i);
+        if (conn == NULL) {
+            break;
+        }
+
+        if (params->rx_queue) {
+            SLIST_FOREACH(chan, &conn->bhc_channels, blc_next) {
+                count += ble_hs_test_util_mbuf_chain_len(chan->blc_rx_buf);
+            }
+        }
+
+        if (params->prep_list) {
+            SLIST_FOREACH(prep, &conn->bhc_att_svr.basc_prep_list, bape_next) {
+                count += ble_hs_test_util_mbuf_chain_len(prep->bape_value);
+            }
+        }
+    }
+    ble_hs_unlock();
+
+    return count;
+}
+
+void
+ble_hs_test_util_assert_mbufs_freed(
+    const struct ble_hs_test_util_mbuf_params *params)
+{
+    static const struct ble_hs_test_util_mbuf_params dflt = {
+        .prev_tx = 1,
+        .rx_queue = 1,
+        .prep_list = 1,
+    };
+
+    int count;
+
+    if (params == NULL) {
+        params = &dflt;
+    }
+
+    count = ble_hs_test_util_mbuf_count(params);
+    TEST_ASSERT(count == os_msys_count());
+}
+
+void
+ble_hs_test_util_post_test(void *arg)
+{
+    ble_hs_test_util_assert_mbufs_freed(arg);
+}
+
+static int
+ble_hs_test_util_pkt_txed(struct os_mbuf *om, void *arg)
+{
+    ble_hs_test_util_prev_tx_enqueue(om);
+    return 0;
+}
+
+static int
+ble_hs_test_util_hci_txed(uint8_t *cmdbuf, void *arg)
+{
+    ble_hs_test_util_enqueue_hci_tx(cmdbuf);
+    ble_hci_trans_buf_free(cmdbuf);
+    return 0;
+}
+
+void
+ble_hs_test_util_init_no_start(void)
+{
+    ble_hs_cfg.parent_evq = &ble_hs_test_util_evq;
+
+    tu_init();
+
+    os_eventq_init(&ble_hs_test_util_evq);
+    STAILQ_INIT(&ble_hs_test_util_prev_tx_queue);
+    ble_hs_test_util_prev_tx_cur = NULL;
+
+    ble_hs_hci_set_phony_ack_cb(NULL);
+
+    ble_hci_trans_cfg_ll(ble_hs_test_util_hci_txed, NULL,
+                         ble_hs_test_util_pkt_txed, NULL);
+
+    ble_hs_test_util_set_startup_acks();
+
+    ble_hs_max_services = 16;
+    ble_hs_max_client_configs = 32;
+    ble_hs_max_attrs = 64;
+
+    ble_hs_test_util_prev_hci_tx_clear();
+}
+
+void
+ble_hs_test_util_init(void)
+{
+    int rc;
+
+    ble_hs_test_util_init_no_start();
+
+    rc = ble_hs_start();
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_hs_test_util_prev_hci_tx_clear();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_hs_test_util.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_test_util.h b/net/nimble/host/test/src/ble_hs_test_util.h
new file mode 100644
index 0000000..5a821cd
--- /dev/null
+++ b/net/nimble/host/test/src/ble_hs_test_util.h
@@ -0,0 +1,177 @@
+/**
+ * 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_BLE_HS_TEST_UTIL_
+#define H_BLE_HS_TEST_UTIL_
+
+#include <inttypes.h>
+#include "host/ble_gap.h"
+#include "ble_hs_priv.h"
+#include "ble_hs_test_util_store.h"
+struct ble_hs_conn;
+struct ble_l2cap_chan;
+struct hci_disconn_complete;
+struct hci_create_conn;
+
+extern struct os_eventq ble_hs_test_util_evq;
+extern const struct ble_gap_adv_params ble_hs_test_util_adv_params;
+
+struct ble_hs_test_util_num_completed_pkts_entry {
+    uint16_t handle_id; /* 0 for terminating entry in array. */
+    uint16_t num_pkts;
+};
+
+struct ble_hs_test_util_flat_attr {
+    uint16_t handle;
+    uint16_t offset;
+    uint8_t value[BLE_ATT_ATTR_MAX_LEN];
+    uint16_t value_len;
+};
+
+struct ble_hs_test_util_mbuf_params {
+    unsigned prev_tx:1;
+    unsigned rx_queue:1;
+    unsigned prep_list:1;
+};
+
+void ble_hs_test_util_prev_tx_enqueue(struct os_mbuf *om);
+struct os_mbuf *ble_hs_test_util_prev_tx_dequeue(void);
+struct os_mbuf *ble_hs_test_util_prev_tx_dequeue_pullup(void);
+int ble_hs_test_util_prev_tx_queue_sz(void);
+void ble_hs_test_util_prev_tx_queue_clear(void);
+
+void ble_hs_test_util_set_ack_params(uint16_t opcode, uint8_t status,
+                                     void *params, uint8_t params_len);
+void ble_hs_test_util_set_ack(uint16_t opcode, uint8_t status);
+void *ble_hs_test_util_get_first_hci_tx(void);
+void *ble_hs_test_util_get_last_hci_tx(void);
+void ble_hs_test_util_enqueue_hci_tx(void *cmd);
+void ble_hs_test_util_prev_hci_tx_clear(void);
+void ble_hs_test_util_build_cmd_complete(uint8_t *dst, int len,
+                                         uint8_t param_len, uint8_t num_pkts,
+                                         uint16_t opcode);
+void ble_hs_test_util_build_cmd_status(uint8_t *dst, int len,
+                                       uint8_t status, uint8_t num_pkts,
+                                       uint16_t opcode);
+void ble_hs_test_util_create_rpa_conn(uint16_t handle, uint8_t own_addr_type,
+                                      const uint8_t *our_rpa,
+                                      uint8_t peer_addr_type,
+                                      const uint8_t *peer_id_addr,
+                                      const uint8_t *peer_rpa,
+                                      ble_gap_event_fn *cb, void *cb_arg);
+void ble_hs_test_util_create_conn(uint16_t handle, uint8_t *addr,
+                                  ble_gap_event_fn *cb, void *cb_arg);
+int ble_hs_test_util_connect(uint8_t own_addr_type,
+                                   uint8_t peer_addr_type,
+                                   const uint8_t *peer_addr,
+                                   int32_t duration_ms,
+                                   const struct ble_gap_conn_params *params,
+                                   ble_gap_event_fn *cb,
+                                   void *cb_arg,
+                                   uint8_t ack_status);
+int ble_hs_test_util_conn_cancel(uint8_t ack_status);
+void ble_hs_test_util_conn_cancel_full(void);
+int ble_hs_test_util_conn_terminate(uint16_t conn_handle, uint8_t hci_status);
+void ble_hs_test_util_conn_disconnect(uint16_t conn_handle);
+int ble_hs_test_util_exp_hci_status(int cmd_idx, int fail_idx,
+                                    uint8_t fail_status);
+int ble_hs_test_util_disc(uint8_t own_addr_type, int32_t duration_ms,
+                          const struct ble_gap_disc_params *disc_params,
+                          ble_gap_event_fn *cb, void *cb_arg, int fail_idx,
+                          uint8_t fail_status);
+int ble_hs_test_util_disc_cancel(uint8_t ack_status);
+void ble_hs_test_util_verify_tx_create_conn(const struct hci_create_conn *exp);
+int ble_hs_test_util_adv_set_fields(struct ble_hs_adv_fields *adv_fields,
+                                    uint8_t hci_status);
+int ble_hs_test_util_adv_start(uint8_t own_addr_type,
+                               uint8_t peer_addr_type,
+                               const uint8_t *peer_addr,
+                               const struct ble_gap_adv_params *adv_params,
+                               ble_gap_event_fn *cb, void *cb_arg,
+                               int fail_idx, uint8_t fail_status);
+int ble_hs_test_util_adv_stop(uint8_t hci_status);
+int ble_hs_test_util_wl_set(struct ble_gap_white_entry *white_list,
+                            uint8_t white_list_count,
+                            int fail_idx, uint8_t fail_status);
+int ble_hs_test_util_conn_update(uint16_t conn_handle,
+                                 struct ble_gap_upd_params *params,
+                                 uint8_t hci_status);
+int ble_hs_test_util_set_our_irk(const uint8_t *irk, int fail_idx,
+                                 uint8_t hci_status);
+int ble_hs_test_util_security_initiate(uint16_t conn_handle,
+                                       uint8_t hci_status);
+int ble_hs_test_util_l2cap_rx_first_frag(uint16_t conn_handle, uint16_t cid,
+                                         struct hci_data_hdr *hci_hdr,
+                                         struct os_mbuf *om);
+int ble_hs_test_util_l2cap_rx(uint16_t conn_handle,
+                              struct hci_data_hdr *hci_hdr,
+                              struct os_mbuf *om);
+int ble_hs_test_util_l2cap_rx_payload_flat(uint16_t conn_handle, uint16_t cid,
+                                           const void *data, int len);
+void ble_hs_test_util_rx_hci_buf_size_ack(uint16_t buf_size);
+void ble_hs_test_util_rx_att_err_rsp(uint16_t conn_handle, uint8_t req_op,
+                                     uint8_t error_code, uint16_t err_handle);
+void ble_hs_test_util_set_startup_acks(void);
+void ble_hs_test_util_rx_num_completed_pkts_event(
+    struct ble_hs_test_util_num_completed_pkts_entry *entries);
+void ble_hs_test_util_rx_disconn_complete_event(
+    struct hci_disconn_complete *evt);
+uint8_t *ble_hs_test_util_verify_tx_hci(uint8_t ogf, uint16_t ocf,
+                                        uint8_t *out_param_len);
+void ble_hs_test_util_tx_all(void);
+void ble_hs_test_util_verify_tx_prep_write(uint16_t attr_handle,
+                                           uint16_t offset,
+                                           const void *data, int data_len);
+void ble_hs_test_util_verify_tx_exec_write(uint8_t expected_flags);
+void ble_hs_test_util_verify_tx_read_rsp(uint8_t *attr_data, int attr_len);
+void ble_hs_test_util_verify_tx_read_blob_rsp(uint8_t *attr_data,
+                                              int attr_len);
+void ble_hs_test_util_set_static_rnd_addr(void);
+struct os_mbuf *ble_hs_test_util_om_from_flat(const void *buf, uint16_t len);
+int ble_hs_test_util_flat_attr_cmp(const struct ble_hs_test_util_flat_attr *a,
+                                   const struct ble_hs_test_util_flat_attr *b);
+void ble_hs_test_util_attr_to_flat(struct ble_hs_test_util_flat_attr *flat,
+                                   const struct ble_gatt_attr *attr);
+void ble_hs_test_util_attr_from_flat(
+    struct ble_gatt_attr *attr, const struct ble_hs_test_util_flat_attr *flat);
+int ble_hs_test_util_read_local_flat(uint16_t attr_handle, uint16_t max_len,
+                                     void *buf, uint16_t *out_len);
+int ble_hs_test_util_write_local_flat(uint16_t attr_handle,
+                                      const void *buf, uint16_t buf_len);
+int ble_hs_test_util_gatt_write_flat(uint16_t conn_handle,
+                                     uint16_t attr_handle,
+                                     const void *data, uint16_t data_len,
+                                     ble_gatt_attr_fn *cb, void *cb_arg);
+int ble_hs_test_util_gatt_write_no_rsp_flat(uint16_t conn_handle,
+                                            uint16_t attr_handle,
+                                            const void *data,
+                                            uint16_t data_len);
+int ble_hs_test_util_gatt_write_long_flat(uint16_t conn_handle,
+                                          uint16_t attr_handle,
+                                          const void *data, uint16_t data_len,
+                                          ble_gatt_attr_fn *cb, void *cb_arg);
+int ble_hs_test_util_mbuf_count(
+    const struct ble_hs_test_util_mbuf_params *params);
+void ble_hs_test_util_assert_mbufs_freed(
+    const struct ble_hs_test_util_mbuf_params *params);
+void ble_hs_test_util_post_test(void *arg);
+void ble_hs_test_util_init_no_start(void);
+void ble_hs_test_util_init(void);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_hs_test_util_store.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_test_util_store.c b/net/nimble/host/test/src/ble_hs_test_util_store.c
new file mode 100644
index 0000000..f888753
--- /dev/null
+++ b/net/nimble/host/test/src/ble_hs_test_util_store.c
@@ -0,0 +1,248 @@
+/**
+ * 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 <string.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "ble_hs_test_util.h"
+#include "ble_hs_test_util_store.h"
+
+static int ble_hs_test_util_store_max_our_secs;
+static int ble_hs_test_util_store_max_peer_secs;
+static int ble_hs_test_util_store_max_cccds;
+
+static struct ble_store_value_sec *ble_hs_test_util_store_our_secs;
+static struct ble_store_value_sec *ble_hs_test_util_store_peer_secs;
+static struct ble_store_value_cccd *ble_hs_test_util_store_cccds;
+int ble_hs_test_util_store_num_our_secs;
+int ble_hs_test_util_store_num_peer_secs;
+int ble_hs_test_util_store_num_cccds;
+
+
+#define BLE_HS_TEST_UTIL_STORE_WRITE_GEN(store, num_vals, max_vals, \
+                                         val, idx) do               \
+{                                                                   \
+    if ((idx) == -1) {                                              \
+        if ((num_vals) >= (max_vals)) {                             \
+            return BLE_HS_ENOMEM;                                   \
+        }                                                           \
+        store[(num_vals)] = (val);                                  \
+        (num_vals)++;                                               \
+    } else {                                                        \
+        store[(idx)] = val;                                         \
+    }                                                               \
+    return 0;                                                       \
+} while (0) 
+
+void
+ble_hs_test_util_store_init(int max_our_secs, int max_peer_secs, int max_cccds)
+{
+    free(ble_hs_test_util_store_our_secs);
+    free(ble_hs_test_util_store_peer_secs);
+    free(ble_hs_test_util_store_cccds);
+
+    ble_hs_test_util_store_our_secs = malloc(
+        ble_hs_test_util_store_max_our_secs *
+        sizeof *ble_hs_test_util_store_our_secs);
+    TEST_ASSERT_FATAL(ble_hs_test_util_store_our_secs != NULL);
+
+    ble_hs_test_util_store_peer_secs = malloc(
+        ble_hs_test_util_store_max_peer_secs *
+        sizeof *ble_hs_test_util_store_peer_secs);
+    TEST_ASSERT_FATAL(ble_hs_test_util_store_peer_secs != NULL);
+
+    ble_hs_test_util_store_cccds = malloc(
+        ble_hs_test_util_store_max_cccds *
+        sizeof *ble_hs_test_util_store_cccds);
+    TEST_ASSERT_FATAL(ble_hs_test_util_store_cccds != NULL);
+
+    ble_hs_test_util_store_max_our_secs = max_our_secs;
+    ble_hs_test_util_store_max_peer_secs = max_peer_secs;
+    ble_hs_test_util_store_max_cccds = max_cccds;
+    ble_hs_test_util_store_num_our_secs = 0;
+    ble_hs_test_util_store_num_peer_secs = 0;
+    ble_hs_test_util_store_num_cccds = 0;
+}
+
+static int
+ble_hs_test_util_store_read_sec(struct ble_store_value_sec *store,
+                                int num_values,
+                                struct ble_store_key_sec *key,
+                                struct ble_store_value_sec *value)
+{
+    struct ble_store_value_sec *cur;
+    int skipped;
+    int i;
+
+    skipped = 0;
+
+    for (i = 0; i < num_values; i++) {
+        cur = store + i;
+
+        if (key->peer_addr_type != BLE_STORE_ADDR_TYPE_NONE) {
+            if (cur->peer_addr_type != key->peer_addr_type) {
+                continue;
+            }
+
+            if (memcmp(cur->peer_addr, key->peer_addr,
+                       sizeof cur->peer_addr) != 0) {
+                continue;
+            }
+        }
+
+        if (key->ediv_rand_present) {
+            if (cur->ediv != key->ediv) {
+                continue;
+            }
+
+            if (cur->rand_num != key->rand_num) {
+                continue;
+            }
+        }
+
+        if (key->idx > skipped) {
+            skipped++;
+            continue;
+        }
+
+        *value = *cur;
+        return 0;
+    }
+
+    return BLE_HS_ENOENT;
+}
+
+static int
+ble_hs_test_util_store_find_cccd(struct ble_store_key_cccd *key)
+{
+    struct ble_store_value_cccd *cur;
+    int skipped;
+    int i;
+
+    skipped = 0;
+    for (i = 0; i < ble_hs_test_util_store_num_cccds; i++) {
+        cur = ble_hs_test_util_store_cccds + i;
+
+        if (key->peer_addr_type != BLE_STORE_ADDR_TYPE_NONE) {
+            if (cur->peer_addr_type != key->peer_addr_type) {
+                continue;
+            }
+
+            if (memcmp(cur->peer_addr, key->peer_addr, 6) != 0) {
+                continue;
+            }
+        }
+
+        if (key->chr_val_handle != 0) {
+            if (cur->chr_val_handle != key->chr_val_handle) {
+                continue;
+            }
+        }
+
+        if (key->idx > skipped) {
+            skipped++;
+            continue;
+        }
+
+        return i;
+    }
+
+    return -1;
+}
+
+static int
+ble_hs_test_util_store_read_cccd(struct ble_store_key_cccd *key,
+                                 struct ble_store_value_cccd *value)
+{
+    int idx;
+
+    idx = ble_hs_test_util_store_find_cccd(key);
+    if (idx == -1) {
+        return BLE_HS_ENOENT;
+    }
+
+    *value = ble_hs_test_util_store_cccds[idx];
+    return 0;
+}
+
+int
+ble_hs_test_util_store_read(int obj_type, union ble_store_key *key,
+                            union ble_store_value *dst)
+{
+    switch (obj_type) {
+    case BLE_STORE_OBJ_TYPE_PEER_SEC:
+        return ble_hs_test_util_store_read_sec(
+            ble_hs_test_util_store_peer_secs,
+            ble_hs_test_util_store_num_peer_secs,
+            &key->sec,
+            &dst->sec);
+
+    case BLE_STORE_OBJ_TYPE_OUR_SEC:
+        return ble_hs_test_util_store_read_sec(
+            ble_hs_test_util_store_our_secs,
+            ble_hs_test_util_store_num_our_secs,
+            &key->sec,
+            &dst->sec);
+
+    case BLE_STORE_OBJ_TYPE_CCCD:
+        return ble_hs_test_util_store_read_cccd(&key->cccd, &dst->cccd);
+
+    default:
+        TEST_ASSERT_FATAL(0);
+        return BLE_HS_EUNKNOWN;
+    }
+}
+
+int
+ble_hs_test_util_store_write(int obj_type, union ble_store_value *value)
+{
+    struct ble_store_key_cccd key_cccd;
+    int idx;
+
+    switch (obj_type) {
+    case BLE_STORE_OBJ_TYPE_PEER_SEC:
+        BLE_HS_TEST_UTIL_STORE_WRITE_GEN(
+            ble_hs_test_util_store_peer_secs,
+            ble_hs_test_util_store_num_peer_secs,
+            ble_hs_test_util_store_max_peer_secs,
+            value->sec, -1);
+
+    case BLE_STORE_OBJ_TYPE_OUR_SEC:
+        BLE_HS_TEST_UTIL_STORE_WRITE_GEN(
+            ble_hs_test_util_store_our_secs,
+            ble_hs_test_util_store_num_our_secs,
+            ble_hs_test_util_store_max_our_secs,
+            value->sec, -1);
+
+    case BLE_STORE_OBJ_TYPE_CCCD:
+        ble_store_key_from_value_cccd(&key_cccd, &value->cccd);
+        idx = ble_hs_test_util_store_find_cccd(&key_cccd);
+        BLE_HS_TEST_UTIL_STORE_WRITE_GEN(
+            ble_hs_test_util_store_cccds,
+            ble_hs_test_util_store_num_cccds,
+            ble_hs_test_util_store_max_cccds,
+            value->cccd, idx);
+
+    default:
+        TEST_ASSERT_FATAL(0);
+        return BLE_HS_EUNKNOWN;
+    }
+
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_hs_test_util_store.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_test_util_store.h b/net/nimble/host/test/src/ble_hs_test_util_store.h
new file mode 100644
index 0000000..77ee291
--- /dev/null
+++ b/net/nimble/host/test/src/ble_hs_test_util_store.h
@@ -0,0 +1,36 @@
+/**
+ * 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_BLE_HS_TEST_UTIL_STORE_
+#define H_BLE_HS_TEST_UTIL_STORE_
+
+union ble_store_value;
+union ble_store_key;
+
+extern int ble_hs_test_util_store_num_our_ltks;
+extern int ble_hs_test_util_store_num_peer_ltks;
+extern int ble_hs_test_util_store_num_cccds;
+
+void ble_hs_test_util_store_init(int max_our_ltks, int max_peer_ltks,
+                                 int max_cccds);
+int ble_hs_test_util_store_read(int obj_type, union ble_store_key *key,
+                                union ble_store_value *dst);
+int ble_hs_test_util_store_write(int obj_type, union ble_store_value *value);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_l2cap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_l2cap_test.c b/net/nimble/host/test/src/ble_l2cap_test.c
new file mode 100644
index 0000000..69db2f8
--- /dev/null
+++ b/net/nimble/host/test/src/ble_l2cap_test.c
@@ -0,0 +1,690 @@
+/**
+ * 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 <stddef.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/hci_common.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+
+#define BLE_L2CAP_TEST_CID  99
+
+static int ble_l2cap_test_update_status;
+static void *ble_l2cap_test_update_arg;
+
+/*****************************************************************************
+ * $util                                                                     *
+ *****************************************************************************/
+
+#define BLE_L2CAP_TEST_UTIL_HCI_HDR(handle, pb, len)    \
+    ((struct hci_data_hdr) {                            \
+        .hdh_handle_pb_bc = ((handle)  << 0) |          \
+                            ((pb)      << 12),          \
+        .hdh_len = (len)                                \
+    })
+
+static void
+ble_l2cap_test_util_init(void)
+{
+    ble_hs_test_util_init();
+    ble_l2cap_test_update_status = -1;
+    ble_l2cap_test_update_arg = (void *)(uintptr_t)-1;
+}
+
+static void
+ble_l2cap_test_util_rx_update_req(uint16_t conn_handle, uint8_t id,
+                                  struct ble_l2cap_sig_update_params *params)
+{
+    struct ble_l2cap_sig_update_req req;
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int rc;
+
+    hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_L2CAP_SIG_HDR_SZ + BLE_L2CAP_SIG_UPDATE_REQ_SZ);
+
+    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_UPDATE_REQ, id,
+                                BLE_L2CAP_SIG_UPDATE_REQ_SZ, &om, &v);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    req.itvl_min = params->itvl_min;
+    req.itvl_max = params->itvl_max;
+    req.slave_latency = params->slave_latency;
+    req.timeout_multiplier = params->timeout_multiplier;
+    ble_l2cap_sig_update_req_write(v, BLE_L2CAP_SIG_UPDATE_REQ_SZ, &req);
+
+    ble_hs_test_util_set_ack(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_CONN_UPDATE), 0);
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SIG,
+                                              &hci_hdr, om);
+    TEST_ASSERT_FATAL(rc == 0);
+}
+
+static int
+ble_l2cap_test_util_rx_update_rsp(uint16_t conn_handle,
+                                  uint8_t id, uint16_t result)
+{
+    struct ble_l2cap_sig_update_rsp rsp;
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int rc;
+
+    hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_L2CAP_SIG_HDR_SZ + BLE_L2CAP_SIG_UPDATE_RSP_SZ);
+
+    rc = ble_l2cap_sig_init_cmd(BLE_L2CAP_SIG_OP_UPDATE_RSP, id,
+                                BLE_L2CAP_SIG_UPDATE_RSP_SZ, &om, &v);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    rsp.result = result;
+    ble_l2cap_sig_update_rsp_write(v, BLE_L2CAP_SIG_UPDATE_RSP_SZ, &rsp);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SIG,
+                                              &hci_hdr, om);
+    return rc;
+}
+
+
+static struct os_mbuf *
+ble_l2cap_test_util_verify_tx_sig_hdr(uint8_t op, uint8_t id,
+                                      uint16_t payload_len,
+                                      struct ble_l2cap_sig_hdr *out_hdr)
+{
+    struct ble_l2cap_sig_hdr hdr;
+    struct os_mbuf *om;
+
+    om = ble_hs_test_util_prev_tx_dequeue();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    TEST_ASSERT(OS_MBUF_PKTLEN(om) == BLE_L2CAP_SIG_HDR_SZ + payload_len);
+    ble_l2cap_sig_hdr_parse(om->om_data, om->om_len, &hdr);
+    TEST_ASSERT(hdr.op == op);
+    if (id != 0) {
+        TEST_ASSERT(hdr.identifier == id);
+    }
+    TEST_ASSERT(hdr.length == payload_len);
+
+    om->om_data += BLE_L2CAP_SIG_HDR_SZ;
+    om->om_len -= BLE_L2CAP_SIG_HDR_SZ;
+
+    if (out_hdr != NULL) {
+        *out_hdr = hdr;
+    }
+
+    return om;
+}
+
+/**
+ * @return                      The L2CAP sig identifier in the request.
+ */
+static uint8_t
+ble_l2cap_test_util_verify_tx_update_req(
+    struct ble_l2cap_sig_update_params *params)
+{
+    struct ble_l2cap_sig_update_req req;
+    struct ble_l2cap_sig_hdr hdr;
+    struct os_mbuf *om;
+
+    om = ble_l2cap_test_util_verify_tx_sig_hdr(BLE_L2CAP_SIG_OP_UPDATE_REQ, 0,
+                                               BLE_L2CAP_SIG_UPDATE_REQ_SZ,
+                                               &hdr);
+
+    /* Verify payload. */
+    ble_l2cap_sig_update_req_parse(om->om_data, om->om_len, &req);
+    TEST_ASSERT(req.itvl_min == params->itvl_min);
+    TEST_ASSERT(req.itvl_max == params->itvl_max);
+    TEST_ASSERT(req.slave_latency == params->slave_latency);
+    TEST_ASSERT(req.timeout_multiplier == params->timeout_multiplier);
+
+    return hdr.identifier;
+}
+
+static void
+ble_l2cap_test_util_verify_tx_update_rsp(uint8_t exp_id, uint16_t exp_result)
+{
+    struct ble_l2cap_sig_update_rsp rsp;
+    struct os_mbuf *om;
+
+    om = ble_l2cap_test_util_verify_tx_sig_hdr(BLE_L2CAP_SIG_OP_UPDATE_RSP,
+                                               exp_id,
+                                               BLE_L2CAP_SIG_UPDATE_RSP_SZ,
+                                               NULL);
+
+    ble_l2cap_sig_update_rsp_parse(om->om_data, om->om_len, &rsp);
+    TEST_ASSERT(rsp.result == exp_result);
+}
+
+static void
+ble_l2cap_test_util_verify_tx_update_conn(
+    struct ble_gap_upd_params *params)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_CONN_UPDATE,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_CONN_UPDATE_LEN);
+    TEST_ASSERT(le16toh(param + 0) == 2);
+    TEST_ASSERT(le16toh(param + 2) == params->itvl_min);
+    TEST_ASSERT(le16toh(param + 4) == params->itvl_max);
+    TEST_ASSERT(le16toh(param + 6) == params->latency);
+    TEST_ASSERT(le16toh(param + 8) == params->supervision_timeout);
+    TEST_ASSERT(le16toh(param + 10) == params->min_ce_len);
+    TEST_ASSERT(le16toh(param + 12) == params->max_ce_len);
+}
+
+static int
+ble_l2cap_test_util_dummy_rx(uint16_t conn_handle, struct os_mbuf **om)
+{
+    return 0;
+}
+
+static void
+ble_l2cap_test_util_create_conn(uint16_t conn_handle, uint8_t *addr,
+                                ble_gap_event_fn *cb, void *cb_arg)
+{
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+
+    ble_hs_test_util_create_conn(conn_handle, addr, cb, cb_arg);
+
+    ble_hs_lock();
+
+    conn = ble_hs_conn_find(conn_handle);
+    TEST_ASSERT_FATAL(conn != NULL);
+
+    chan = ble_l2cap_chan_alloc();
+    TEST_ASSERT_FATAL(chan != NULL);
+
+    chan->blc_cid = BLE_L2CAP_TEST_CID;
+    chan->blc_my_mtu = 240;
+    chan->blc_default_mtu = 240;
+    chan->blc_rx_fn = ble_l2cap_test_util_dummy_rx;
+
+    ble_hs_conn_chan_insert(conn, chan);
+
+    ble_hs_test_util_prev_hci_tx_clear();
+
+    ble_hs_unlock();
+}
+
+static int
+ble_l2cap_test_util_rx_first_frag(uint16_t conn_handle,
+                                  uint16_t l2cap_frag_len,
+                                  uint16_t cid, uint16_t l2cap_len)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    uint16_t hci_len;
+    void *v;
+    int rc;
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    v = os_mbuf_extend(om, l2cap_frag_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    om = ble_l2cap_prepend_hdr(om, cid, l2cap_len);
+    TEST_ASSERT_FATAL(om != NULL);
+
+    hci_len = sizeof hci_hdr + l2cap_frag_len;
+    hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(conn_handle,
+                                          BLE_HCI_PB_FIRST_FLUSH, hci_len);
+    rc = ble_hs_test_util_l2cap_rx(conn_handle, &hci_hdr, om);
+    return rc;
+}
+
+static int
+ble_l2cap_test_util_rx_next_frag(uint16_t conn_handle, uint16_t hci_len)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int rc;
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    v = os_mbuf_extend(om, hci_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(conn_handle,
+                                          BLE_HCI_PB_MIDDLE, hci_len);
+    rc = ble_hs_test_util_l2cap_rx(conn_handle, &hci_hdr, om);
+    return rc;
+}
+
+static void
+ble_l2cap_test_util_verify_first_frag(uint16_t conn_handle,
+                                      uint16_t l2cap_frag_len,
+                                      uint16_t l2cap_len)
+{
+    struct ble_hs_conn *conn;
+    int rc;
+
+    rc = ble_l2cap_test_util_rx_first_frag(conn_handle, l2cap_frag_len,
+                                           BLE_L2CAP_TEST_CID, l2cap_len);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_lock();
+
+    conn = ble_hs_conn_find(conn_handle);
+    TEST_ASSERT_FATAL(conn != NULL);
+    TEST_ASSERT(conn->bhc_rx_chan != NULL &&
+                conn->bhc_rx_chan->blc_cid == BLE_L2CAP_TEST_CID);
+
+    ble_hs_unlock();
+}
+
+static void
+ble_l2cap_test_util_verify_middle_frag(uint16_t conn_handle,
+                                       uint16_t hci_len)
+{
+    struct ble_hs_conn *conn;
+    int rc;
+
+    rc = ble_l2cap_test_util_rx_next_frag(conn_handle, hci_len);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_lock();
+
+    conn = ble_hs_conn_find(conn_handle);
+    TEST_ASSERT_FATAL(conn != NULL);
+    TEST_ASSERT(conn->bhc_rx_chan != NULL &&
+                conn->bhc_rx_chan->blc_cid == BLE_L2CAP_TEST_CID);
+
+    ble_hs_unlock();
+}
+
+static void
+ble_l2cap_test_util_verify_last_frag(uint16_t conn_handle,
+                                     uint16_t hci_len)
+{
+    struct ble_hs_conn *conn;
+    int rc;
+
+    rc = ble_l2cap_test_util_rx_next_frag(conn_handle, hci_len);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_lock();
+
+    conn = ble_hs_conn_find(conn_handle);
+    TEST_ASSERT_FATAL(conn != NULL);
+    TEST_ASSERT(conn->bhc_rx_chan == NULL);
+
+    ble_hs_unlock();
+}
+
+/*****************************************************************************
+ * $rx                                                                       *
+ *****************************************************************************/
+
+TEST_CASE(ble_l2cap_test_case_bad_header)
+{
+    int rc;
+
+    ble_l2cap_test_util_init();
+
+    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
+                                    NULL, NULL);
+
+    rc = ble_l2cap_test_util_rx_first_frag(2, 14, 1234, 10);
+    TEST_ASSERT(rc == BLE_HS_ENOENT);
+}
+
+/*****************************************************************************
+ * $fragmentation                                                            *
+ *****************************************************************************/
+
+TEST_CASE(ble_l2cap_test_case_frag_single)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    int rc;
+
+    ble_l2cap_test_util_init();
+
+    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
+                                    NULL, NULL);
+
+    /*** HCI header specifies middle fragment without start. */
+    hci_hdr = BLE_L2CAP_TEST_UTIL_HCI_HDR(2, BLE_HCI_PB_MIDDLE, 10);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    om = ble_l2cap_prepend_hdr(om, 0, 5);
+    TEST_ASSERT_FATAL(om != NULL);
+
+    rc = ble_hs_test_util_l2cap_rx(2, &hci_hdr, om);
+    TEST_ASSERT(rc == BLE_HS_EBADDATA);
+
+    /*** Packet consisting of three fragments. */
+    ble_l2cap_test_util_verify_first_frag(2, 10, 30);
+    ble_l2cap_test_util_verify_middle_frag(2, 10);
+    ble_l2cap_test_util_verify_last_frag(2, 10);
+
+    /*** Packet consisting of five fragments. */
+    ble_l2cap_test_util_verify_first_frag(2, 8, 49);
+    ble_l2cap_test_util_verify_middle_frag(2, 13);
+    ble_l2cap_test_util_verify_middle_frag(2, 2);
+    ble_l2cap_test_util_verify_middle_frag(2, 21);
+    ble_l2cap_test_util_verify_last_frag(2, 5);
+}
+
+TEST_CASE(ble_l2cap_test_case_frag_multiple)
+{
+    ble_l2cap_test_util_init();
+
+    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
+                                    NULL, NULL);
+    ble_l2cap_test_util_create_conn(3, ((uint8_t[]){2,3,4,5,6,7}),
+                                    NULL, NULL);
+    ble_l2cap_test_util_create_conn(4, ((uint8_t[]){3,4,5,6,7,8}),
+                                    NULL, NULL);
+
+    ble_l2cap_test_util_verify_first_frag(2, 3, 10);
+    ble_l2cap_test_util_verify_first_frag(3, 2, 5);
+    ble_l2cap_test_util_verify_middle_frag(2, 6);
+    ble_l2cap_test_util_verify_first_frag(4, 1, 4);
+    ble_l2cap_test_util_verify_middle_frag(3, 2);
+    ble_l2cap_test_util_verify_last_frag(3, 1);
+    ble_l2cap_test_util_verify_middle_frag(4, 2);
+    ble_l2cap_test_util_verify_last_frag(4, 1);
+    ble_l2cap_test_util_verify_last_frag(2, 1);
+}
+
+TEST_CASE(ble_l2cap_test_case_frag_channels)
+{
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_l2cap_test_util_init();
+
+    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
+                                    NULL, NULL);
+
+    /* Receive a starting fragment on the first channel. */
+    rc = ble_l2cap_test_util_rx_first_frag(2, 14, BLE_L2CAP_TEST_CID, 30);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_lock();
+    conn = ble_hs_conn_find(2);
+    TEST_ASSERT_FATAL(conn != NULL);
+    TEST_ASSERT(conn->bhc_rx_chan != NULL &&
+                conn->bhc_rx_chan->blc_cid == BLE_L2CAP_TEST_CID);
+    ble_hs_unlock();
+
+    /* Receive a starting fragment on a different channel.  The first fragment
+     * should get discarded.
+     */
+    rc = ble_l2cap_test_util_rx_first_frag(2, 14, BLE_L2CAP_CID_ATT, 30);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_lock();
+    conn = ble_hs_conn_find(2);
+    TEST_ASSERT_FATAL(conn != NULL);
+    TEST_ASSERT(conn->bhc_rx_chan != NULL &&
+                conn->bhc_rx_chan->blc_cid == BLE_L2CAP_CID_ATT);
+    ble_hs_unlock();
+}
+
+/*****************************************************************************
+ * $unsolicited response                                                     *
+ *****************************************************************************/
+
+TEST_CASE(ble_l2cap_test_case_sig_unsol_rsp)
+{
+    int rc;
+
+    ble_l2cap_test_util_init();
+
+    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
+                                    NULL, NULL);
+
+    /* Receive an unsolicited response. */
+    rc = ble_l2cap_test_util_rx_update_rsp(2, 100, 0);
+    TEST_ASSERT(rc == BLE_HS_ENOENT);
+
+    /* Ensure we did not send anything in return. */
+    ble_hs_test_util_tx_all();
+    TEST_ASSERT_FATAL(ble_hs_test_util_prev_tx_dequeue() == NULL);
+}
+
+/*****************************************************************************
+ * $update                                                                   *
+ *****************************************************************************/
+
+static int
+ble_l2cap_test_util_conn_cb(struct ble_gap_event *event, void *arg)
+{
+    int *accept;
+
+    switch (event->type) {
+    case BLE_GAP_EVENT_L2CAP_UPDATE_REQ:
+        accept = arg;
+        return !*accept;
+
+    default:
+        return 0;
+    }
+}
+
+static void
+ble_l2cap_test_util_peer_updates(int accept)
+{
+    struct ble_l2cap_sig_update_params l2cap_params;
+    struct ble_gap_upd_params params;
+    ble_hs_conn_flags_t conn_flags;
+    int rc;
+
+    ble_l2cap_test_util_init();
+
+    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
+                                    ble_l2cap_test_util_conn_cb,
+                                    &accept);
+
+    l2cap_params.itvl_min = 0x200;
+    l2cap_params.itvl_max = 0x300;
+    l2cap_params.slave_latency = 0;
+    l2cap_params.timeout_multiplier = 0x100;
+    ble_l2cap_test_util_rx_update_req(2, 1, &l2cap_params);
+
+    /* Ensure an update response command got sent. */
+    ble_hs_process_tx_data_queue();
+    ble_l2cap_test_util_verify_tx_update_rsp(1, !accept);
+
+    if (accept) {
+        params.itvl_min = 0x200;
+        params.itvl_max = 0x300;
+        params.latency = 0;
+        params.supervision_timeout = 0x100;
+        params.min_ce_len = BLE_GAP_INITIAL_CONN_MIN_CE_LEN;
+        params.max_ce_len = BLE_GAP_INITIAL_CONN_MAX_CE_LEN;
+        ble_l2cap_test_util_verify_tx_update_conn(&params);
+    } else {
+        /* Ensure no update got scheduled. */
+        rc = ble_hs_atomic_conn_flags(2, &conn_flags);
+        TEST_ASSERT(rc == 0 && !(conn_flags & BLE_HS_CONN_F_UPDATE));
+    }
+}
+
+static void
+ble_l2cap_test_util_update_cb(int status, void *arg)
+{
+    ble_l2cap_test_update_status = status;
+    ble_l2cap_test_update_arg = arg;
+}
+
+static void
+ble_l2cap_test_util_we_update(int peer_accepts)
+{
+    struct ble_l2cap_sig_update_params params;
+    uint8_t id;
+    int rc;
+
+    ble_l2cap_test_util_init();
+
+    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
+                                    ble_l2cap_test_util_conn_cb, NULL);
+
+    /* Only the slave can initiate the L2CAP connection update procedure. */
+    ble_hs_atomic_conn_set_flags(2, BLE_HS_CONN_F_MASTER, 0);
+
+    params.itvl_min = 0x200;
+    params.itvl_min = 0x300;
+    params.slave_latency = 0;
+    params.timeout_multiplier = 0x100;
+    rc = ble_l2cap_sig_update(2, &params, ble_l2cap_test_util_update_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_hs_test_util_tx_all();
+
+    /* Ensure an update request got sent. */
+    id = ble_l2cap_test_util_verify_tx_update_req(&params);
+
+    /* Receive response from peer. */
+    rc = ble_l2cap_test_util_rx_update_rsp(2, id, !peer_accepts);
+    TEST_ASSERT(rc == 0);
+
+    /* Ensure callback got called. */
+    if (peer_accepts) {
+        TEST_ASSERT(ble_l2cap_test_update_status == 0);
+    } else {
+        TEST_ASSERT(ble_l2cap_test_update_status == BLE_HS_EREJECT);
+    }
+    TEST_ASSERT(ble_l2cap_test_update_arg == NULL);
+}
+
+TEST_CASE(ble_l2cap_test_case_sig_update_accept)
+{
+    ble_l2cap_test_util_peer_updates(1);
+}
+
+TEST_CASE(ble_l2cap_test_case_sig_update_reject)
+{
+    ble_l2cap_test_util_peer_updates(0);
+}
+
+TEST_CASE(ble_l2cap_test_case_sig_update_init_accept)
+{
+    ble_l2cap_test_util_we_update(1);
+}
+
+TEST_CASE(ble_l2cap_test_case_sig_update_init_reject)
+{
+    ble_l2cap_test_util_we_update(0);
+}
+
+TEST_CASE(ble_l2cap_test_case_sig_update_init_fail_master)
+{
+    struct ble_l2cap_sig_update_params params;
+    int rc;
+
+    ble_l2cap_test_util_init();
+
+    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
+                                    ble_l2cap_test_util_conn_cb, NULL);
+
+    params.itvl_min = 0x200;
+    params.itvl_min = 0x300;
+    params.slave_latency = 0;
+    params.timeout_multiplier = 0x100;
+    rc = ble_l2cap_sig_update(2, &params, ble_l2cap_test_util_update_cb, NULL);
+    TEST_ASSERT_FATAL(rc == BLE_HS_EINVAL);
+
+    /* Ensure callback never called. */
+    ble_hs_test_util_tx_all();
+    TEST_ASSERT(ble_l2cap_test_update_status == -1);
+}
+
+TEST_CASE(ble_l2cap_test_case_sig_update_init_fail_bad_id)
+{
+    struct ble_l2cap_sig_update_params params;
+    uint8_t id;
+    int rc;
+
+    ble_l2cap_test_util_init();
+
+    ble_l2cap_test_util_create_conn(2, ((uint8_t[]){1,2,3,4,5,6}),
+                                    ble_l2cap_test_util_conn_cb, NULL);
+
+    /* Only the slave can initiate the L2CAP connection update procedure. */
+    ble_hs_atomic_conn_set_flags(2, BLE_HS_CONN_F_MASTER, 0);
+
+    params.itvl_min = 0x200;
+    params.itvl_min = 0x300;
+    params.slave_latency = 0;
+    params.timeout_multiplier = 0x100;
+    rc = ble_l2cap_sig_update(2, &params, ble_l2cap_test_util_update_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_hs_test_util_tx_all();
+
+    /* Ensure an update request got sent. */
+    id = ble_l2cap_test_util_verify_tx_update_req(&params);
+
+    /* Receive response from peer with incorrect ID. */
+    rc = ble_l2cap_test_util_rx_update_rsp(2, id + 1, 0);
+    TEST_ASSERT(rc == BLE_HS_ENOENT);
+
+    /* Ensure callback did not get called. */
+    TEST_ASSERT(ble_l2cap_test_update_status == -1);
+
+    /* Receive response from peer with correct ID. */
+    rc = ble_l2cap_test_util_rx_update_rsp(2, id, 0);
+    TEST_ASSERT(rc == 0);
+
+    /* Ensure callback got called. */
+    TEST_ASSERT(ble_l2cap_test_update_status == 0);
+    TEST_ASSERT(ble_l2cap_test_update_arg == NULL);
+}
+
+TEST_SUITE(ble_l2cap_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_l2cap_test_case_bad_header();
+    ble_l2cap_test_case_frag_single();
+    ble_l2cap_test_case_frag_multiple();
+    ble_l2cap_test_case_frag_channels();
+    ble_l2cap_test_case_sig_unsol_rsp();
+    ble_l2cap_test_case_sig_update_accept();
+    ble_l2cap_test_case_sig_update_reject();
+    ble_l2cap_test_case_sig_update_init_accept();
+    ble_l2cap_test_case_sig_update_init_reject();
+    ble_l2cap_test_case_sig_update_init_fail_master();
+    ble_l2cap_test_case_sig_update_init_fail_bad_id();
+}
+
+int
+ble_l2cap_test_all(void)
+{
+    ble_l2cap_test_suite();
+
+    return tu_any_failed;
+}


[41/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
syscfg / sysinit


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

Branch: refs/heads/sterly_refactor
Commit: d98ddc1c5e877d18e5ac68b6d342cf39ba068b20
Parents: fdd134d
Author: Christopher Collins <cc...@apache.org>
Authored: Mon Sep 12 19:49:17 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Mon Sep 12 20:39:09 2016 -0700

----------------------------------------------------------------------
 apps/blecent/pkg.yml                            |    4 +-
 apps/blecent/src/main.c                         |   94 +-
 apps/blehci/src/main.c                          |   52 +-
 apps/bleprph/pkg.yml                            |   20 +-
 apps/bleprph/src/bleprph.h                      |    2 +-
 apps/bleprph/src/gatt_svr.c                     |    4 +-
 apps/bleprph/src/main.c                         |  125 +-
 apps/bletest/pkg.yml                            |    4 +
 apps/bletest/src/bletest_hci.c                  |    6 +-
 apps/bletest/src/bletest_priv.h                 |    2 +-
 apps/bletest/src/main.c                         |  129 +-
 apps/bletiny/pkg.yml                            |   12 +-
 apps/bletiny/src/bletiny.h                      |    4 +-
 apps/bletiny/src/cmd.c                          |    4 +-
 apps/bletiny/src/gatt_svr.c                     |    4 +-
 apps/bletiny/src/main.c                         |  123 +-
 apps/bleuart/pkg.yml                            |    6 +-
 apps/bleuart/src/main.c                         |  101 +-
 apps/blinky/src/main.c                          |  114 +-
 apps/boot/pkg.yml                               |   36 +-
 apps/boot/src/boot.c                            |   16 +-
 apps/ffs2native/src/main.c                      |   14 +-
 apps/luatest/src/main.c                         |   55 +-
 apps/sblinky/pkg.yml                            |   12 -
 apps/sblinky/src/main.c                         |   72 +-
 apps/slinky/pkg.yml                             |   10 +-
 apps/slinky/src/main.c                          |  117 +-
 apps/test/pkg.yml                               |    2 -
 drivers/adc/adc_nrf51/pkg.yml                   |    7 +-
 drivers/adc/adc_nrf52/pkg.yml                   |    7 +-
 drivers/adc/pkg.yml                             |    4 -
 drivers/uart/pkg.yml                            |    1 -
 fs/fs/pkg.yml                                   |   12 +-
 fs/fs/src/fs_cli.c                              |    6 +-
 fs/fs/src/fs_mount.c                            |    8 +-
 fs/fs/src/fs_priv.h                             |    6 +-
 fs/nffs/include/nffs/nffs.h                     |    3 +
 fs/nffs/pkg.yml                                 |   10 +-
 fs/nffs/src/nffs.c                              |   54 +-
 fs/nffs/src/nffs_misc.c                         |   70 +
 fs/nffs/src/nffs_priv.h                         |    3 +
 fs/nffs/src/nffs_restore.c                      |    2 +-
 fs/nffs/src/test/arch/cortex_m4/nffs_test.c     |   27 -
 fs/nffs/src/test/arch/sim/nffs_test.c           | 3250 ---------
 fs/nffs/src/test/arch/sim/nffs_test_priv.h      |   42 -
 fs/nffs/src/test/arch/sim/nffs_test_system_01.c | 6537 ------------------
 fs/nffs/test/pkg.yml                            |   30 +
 fs/nffs/test/src/arch/cortex_m4/nffs_test.c     |   27 +
 fs/nffs/test/src/arch/sim/nffs_test.c           | 3251 +++++++++
 fs/nffs/test/src/arch/sim/nffs_test_priv.h      |   42 +
 fs/nffs/test/src/arch/sim/nffs_test_system_01.c | 6537 ++++++++++++++++++
 hw/bsp/arduino_primo_nrf52/pkg.yml              |   59 +-
 hw/bsp/bmd300eval/pkg.yml                       |   57 +-
 hw/bsp/native/pkg.yml                           |    3 +-
 hw/bsp/native/src/hal_bsp.c                     |   75 -
 hw/bsp/nrf51-arduino_101/pkg.yml                |   55 +-
 hw/bsp/nrf51-blenano/pkg.yml                    |   55 +-
 hw/bsp/nrf51dk-16kbram/pkg.yml                  |   56 +-
 hw/bsp/nrf51dk/pkg.yml                          |   52 +-
 hw/bsp/nrf52dk/pkg.yml                          |   60 +-
 hw/bsp/nrf52dk/src/os_bsp.c                     |   78 +-
 hw/bsp/nrf52pdk/pkg.yml                         |   58 +-
 hw/bsp/olimex_stm32-e407_devboard/pkg.yml       |   45 +-
 hw/hal/include/hal/flash_map.h                  |    5 +-
 hw/hal/include/hal/hal_bsp.h                    |    1 +
 hw/hal/pkg.yml                                  |    3 -
 hw/hal/src/flash_map.c                          |   76 +-
 hw/hal/src/test/flash_map_test.c                |  169 -
 hw/hal/test/pkg.yml                             |   30 +
 hw/hal/test/src/flash_map_test.c                |  170 +
 hw/mcu/native/include/mcu/mcu_hal.h             |    2 -
 hw/mcu/native/src/hal_adc.c                     |    4 +-
 hw/mcu/native/src/hal_dac.c                     |    2 +
 hw/mcu/native/src/hal_pwm.c                     |    3 +-
 hw/mcu/nordic/nrf51xxx/pkg.yml                  |    3 -
 hw/mcu/nordic/nrf52xxx/include/mcu/cortex_m4.h  |    3 +-
 hw/mcu/nordic/nrf52xxx/pkg.yml                  |    3 -
 hw/mcu/nordic/nrf52xxx/src/hal_os_tick.c        |   17 +-
 hw/mcu/nordic/pkg.yml                           |    5 +-
 libs/baselibc/pkg.yml                           |    4 -
 libs/bleuart/include/bleuart/bleuart.h          |    6 +-
 libs/bleuart/pkg.yml                            |    8 +
 libs/bleuart/src/bleuart.c                      |   29 +-
 libs/boot_serial/pkg.yml                        |    2 +-
 libs/boot_serial/src/test/boot_test.c           |  230 -
 libs/boot_serial/test/pkg.yml                   |   30 +
 libs/boot_serial/test/src/boot_test.c           |  231 +
 libs/bootutil/pkg.yml                           |   25 +-
 libs/bootutil/src/bootutil_misc.c               |   20 +-
 libs/bootutil/src/image_ec.c                    |   17 +-
 libs/bootutil/src/image_rsa.c                   |   13 +-
 libs/bootutil/src/image_validate.c              |   27 +-
 libs/bootutil/src/loader.c                      |    6 +
 libs/bootutil/src/test/boot_test.c              | 1169 ----
 libs/bootutil/test/pkg.yml                      |   33 +
 libs/bootutil/test/src/boot_serial_test.c       | 1171 ++++
 libs/bootutil/test/src/boot_test.c              | 1170 ++++
 libs/console/full/pkg.yml                       |    4 +-
 libs/console/full/src/cons_fmt.c                |    7 +-
 libs/console/full/src/cons_tty.c                |   17 +-
 libs/crash_test/pkg.yml                         |   18 +-
 libs/crash_test/src/crash_cli.c                 |    7 +-
 libs/crash_test/src/crash_nmgr.c                |   12 +-
 libs/crash_test/src/crash_test.c                |   18 +-
 libs/crash_test/src/crash_test_priv.h           |    4 +-
 libs/elua/elua_base/include/elua_base/elua.h    |    2 +-
 libs/elua/elua_base/pkg.yml                     |   18 +-
 libs/elua/elua_base/src/lmynewt.c               |   23 +-
 libs/imgmgr/include/imgmgr/imgmgr.h             |    2 +-
 libs/imgmgr/pkg.yml                             |   18 +-
 libs/imgmgr/src/imgmgr.c                        |   24 +-
 libs/imgmgr/src/imgmgr_coredump.c               |   13 +-
 libs/imgmgr/src/imgmgr_fs.c                     |   21 +-
 libs/imgmgr/src/imgmgr_priv.h                   |    3 +-
 libs/inet_def_service/pkg.yml                   |    3 -
 libs/json/pkg.yml                               |    2 -
 libs/json/src/test/test_json.c                  |   41 -
 libs/json/src/test/test_json.h                  |   27 -
 libs/json/src/test/test_json_simple.c           |  360 -
 libs/json/test/pkg.yml                          |   30 +
 libs/json/test/src/test_json.c                  |   43 +
 libs/json/test/src/test_json.h                  |   27 +
 libs/json/test/src/test_json_simple.c           |  360 +
 libs/mbedtls/pkg.yml                            |    4 +-
 libs/mbedtls/src/test/mbedtls_test.c            |  228 -
 libs/mbedtls/test/pkg.yml                       |   30 +
 libs/mbedtls/test/src/mbedtls_test.c            |  228 +
 libs/newtmgr/include/newtmgr/newtmgr.h          |    2 +-
 libs/newtmgr/pkg.yml                            |   17 +-
 libs/newtmgr/src/newtmgr.c                      |   32 +-
 .../transport/ble/include/nmgrble/newtmgr_ble.h |    2 +-
 libs/newtmgr/transport/ble/pkg.yml              |    3 +
 libs/newtmgr/transport/ble/src/newtmgr_ble.c    |   25 +-
 libs/os/include/os/os_mbuf.h                    |    3 +
 libs/os/pkg.yml                                 |   52 +-
 libs/os/src/arch/cortex_m0/os_fault.c           |   20 +-
 libs/os/src/arch/cortex_m4/os_arch_arm.c        |    3 -
 libs/os/src/arch/cortex_m4/os_fault.c           |   15 +-
 libs/os/src/os.c                                |   14 +
 libs/os/src/os_mbuf.c                           |   27 +
 libs/os/src/os_msys_init.c                      |  147 +
 libs/os/src/os_priv.h                           |    2 +
 .../src/test/arch/cortex_m4/os_test_arch_arm.c  |   27 -
 libs/os/src/test/arch/sim/os_test_arch_sim.c    |   52 -
 libs/os/src/test/eventq_test.c                  |  416 --
 libs/os/src/test/mbuf_test.c                    |  420 --
 libs/os/src/test/mempool_test.c                 |  227 -
 libs/os/src/test/mutex_test.c                   |  407 --
 libs/os/src/test/os_test.c                      |   52 -
 libs/os/src/test/os_test_priv.h                 |   32 -
 libs/os/src/test/sem_test.c                     |  401 --
 libs/os/test/pkg.yml                            |   30 +
 .../test/src/arch/cortex_m4/os_test_arch_arm.c  |   27 +
 libs/os/test/src/arch/sim/os_test_arch_sim.c    |   52 +
 libs/os/test/src/eventq_test.c                  |  416 ++
 libs/os/test/src/mbuf_test.c                    |  420 ++
 libs/os/test/src/mempool_test.c                 |  227 +
 libs/os/test/src/mutex_test.c                   |  407 ++
 libs/os/test/src/os_test.c                      |   53 +
 libs/os/test/src/os_test_priv.h                 |   32 +
 libs/os/test/src/sem_test.c                     |  401 ++
 libs/shell/include/shell/shell.h                |    3 +-
 libs/shell/pkg.yml                              |   17 +-
 libs/shell/src/shell.c                          |   80 +-
 libs/testutil/include/testutil/testutil.h       |    4 +-
 libs/testutil/pkg.yml                           |    2 -
 libs/util/include/util/mem.h                    |    3 +
 libs/util/pkg.yml                               |    1 -
 libs/util/src/mem.c                             |   20 +
 libs/util/src/test/cbmem_test.c                 |  176 -
 libs/util/src/test/util_test.c                  |   45 -
 libs/util/src/test/util_test_priv.h             |   25 -
 libs/util/test/pkg.yml                          |   30 +
 libs/util/test/src/cbmem_test.c                 |  176 +
 libs/util/test/src/util_test.c                  |   46 +
 libs/util/test/src/util_test_priv.h             |   25 +
 libs/wifi_mgmt/pkg.yml                          |   13 +-
 libs/wifi_mgmt/src/wifi.c                       |    2 +-
 libs/wifi_mgmt/src/wifi_cli.c                   |    6 +-
 libs/wifi_mgmt/src/wifi_priv.h                  |    4 +-
 .../controller/include/controller/ble_ll.h      |    5 +-
 .../controller/include/controller/ble_ll_conn.h |    6 +-
 net/nimble/controller/pkg.yml                   |  170 +-
 net/nimble/controller/src/ble_ll.c              |   68 +-
 net/nimble/controller/src/ble_ll_adv.c          |   21 +-
 net/nimble/controller/src/ble_ll_conn.c         |   71 +-
 net/nimble/controller/src/ble_ll_conn_hci.c     |   13 +-
 net/nimble/controller/src/ble_ll_conn_priv.h    |    2 +-
 net/nimble/controller/src/ble_ll_ctrl.c         |   27 +-
 net/nimble/controller/src/ble_ll_hci.c          |   21 +-
 net/nimble/controller/src/ble_ll_hci_ev.c       |    3 +-
 net/nimble/controller/src/ble_ll_rand.c         |   11 +-
 net/nimble/controller/src/ble_ll_resolv.c       |   11 +-
 net/nimble/controller/src/ble_ll_rng.c          |    2 +-
 net/nimble/controller/src/ble_ll_scan.c         |   25 +-
 net/nimble/controller/src/ble_ll_supp_cmd.c     |    4 +-
 net/nimble/controller/src/ble_ll_whitelist.c    |    5 +-
 net/nimble/drivers/native/src/ble_hw.c          |    3 +-
 net/nimble/drivers/native/src/ble_phy.c         |    5 +-
 net/nimble/drivers/nrf51/src/ble_hw.c           |    9 +-
 net/nimble/drivers/nrf51/src/ble_phy.c          |   39 +-
 net/nimble/drivers/nrf52/src/ble_hw.c           |   11 +-
 net/nimble/drivers/nrf52/src/ble_phy.c          |   31 +-
 net/nimble/host/include/host/ble_gatt.h         |    3 +-
 net/nimble/host/include/host/ble_hs.h           |  121 +-
 net/nimble/host/include/host/ble_sm.h           |    4 +-
 net/nimble/host/pkg.yml                         |  206 +-
 .../include/services/mandatory/ble_svc_gap.h    |    2 +-
 .../include/services/mandatory/ble_svc_gatt.h   |    2 +-
 net/nimble/host/services/mandatory/pkg.yml      |    3 +
 .../host/services/mandatory/src/ble_svc_gap.c   |    4 +-
 .../host/services/mandatory/src/ble_svc_gatt.c  |    4 +-
 .../services/mandatory/src/ble_svc_mandatory.c  |   36 +
 net/nimble/host/src/ble_att_clt.c               |   48 +-
 net/nimble/host/src/ble_att_priv.h              |    2 +
 net/nimble/host/src/ble_att_svr.c               |   73 +-
 net/nimble/host/src/ble_eddystone.c             |    4 +-
 net/nimble/host/src/ble_gap.c                   |   40 +-
 net/nimble/host/src/ble_gattc.c                 |  101 +-
 net/nimble/host/src/ble_gatts.c                 |  136 +-
 net/nimble/host/src/ble_hs.c                    |  175 +-
 net/nimble/host/src/ble_hs_adv.c                |    4 +-
 net/nimble/host/src/ble_hs_cfg.c                |   69 +-
 net/nimble/host/src/ble_hs_conn.c               |   57 +-
 net/nimble/host/src/ble_hs_dbg.c                |    2 +-
 net/nimble/host/src/ble_hs_hci.c                |    6 +-
 net/nimble/host/src/ble_hs_hci_priv.h           |    2 +-
 net/nimble/host/src/ble_hs_misc.c               |   20 -
 net/nimble/host/src/ble_hs_priv.h               |   19 +-
 net/nimble/host/src/ble_hs_pvcy.c               |    2 +-
 net/nimble/host/src/ble_l2cap.c                 |   39 +-
 net/nimble/host/src/ble_l2cap_sig.c             |   38 +-
 net/nimble/host/src/ble_sm.c                    |   69 +-
 net/nimble/host/src/ble_sm_alg.c                |  134 +-
 net/nimble/host/src/ble_sm_cmd.c                |    2 +-
 net/nimble/host/src/ble_sm_lgcy.c               |    2 +-
 net/nimble/host/src/ble_sm_priv.h               |    9 +-
 net/nimble/host/src/ble_sm_sc.c                 |    4 +-
 net/nimble/host/src/test/ble_att_clt_test.c     |  536 --
 net/nimble/host/src/test/ble_att_svr_test.c     | 2314 -------
 net/nimble/host/src/test/ble_gap_test.c         | 2578 -------
 net/nimble/host/src/test/ble_gatt_conn_test.c   |  533 --
 net/nimble/host/src/test/ble_gatt_disc_c_test.c |  547 --
 net/nimble/host/src/test/ble_gatt_disc_d_test.c |  363 -
 net/nimble/host/src/test/ble_gatt_disc_s_test.c |  406 --
 net/nimble/host/src/test/ble_gatt_find_s_test.c |  342 -
 net/nimble/host/src/test/ble_gatt_read_test.c   |  823 ---
 net/nimble/host/src/test/ble_gatt_write_test.c  |  639 --
 .../host/src/test/ble_gatts_notify_test.c       |  983 ---
 net/nimble/host/src/test/ble_gatts_read_test.c  |  261 -
 net/nimble/host/src/test/ble_gatts_reg_test.c   |  718 --
 net/nimble/host/src/test/ble_hs_adv_test.c      | 1486 ----
 net/nimble/host/src/test/ble_hs_conn_test.c     |  219 -
 net/nimble/host/src/test/ble_hs_hci_test.c      |   99 -
 net/nimble/host/src/test/ble_hs_test.c          |   60 -
 net/nimble/host/src/test/ble_hs_test_util.c     | 1438 ----
 net/nimble/host/src/test/ble_hs_test_util.h     |  176 -
 .../host/src/test/ble_hs_test_util_store.c      |  248 -
 .../host/src/test/ble_hs_test_util_store.h      |   36 -
 net/nimble/host/src/test/ble_l2cap_test.c       |  690 --
 net/nimble/host/src/test/ble_os_test.c          |  401 --
 net/nimble/host/src/test/ble_sm_lgcy_test.c     |  839 ---
 net/nimble/host/src/test/ble_sm_sc_test.c       | 4910 -------------
 net/nimble/host/src/test/ble_sm_test.c          |  678 --
 net/nimble/host/src/test/ble_sm_test_util.c     | 2404 -------
 net/nimble/host/src/test/ble_sm_test_util.h     |  119 -
 net/nimble/host/src/test/ble_uuid_test.c        |   93 -
 net/nimble/host/test/pkg.yml                    |   41 +
 net/nimble/host/test/src/ble_att_clt_test.c     |  536 ++
 net/nimble/host/test/src/ble_att_svr_test.c     | 2314 +++++++
 net/nimble/host/test/src/ble_gap_test.c         | 2580 +++++++
 net/nimble/host/test/src/ble_gatt_conn_test.c   |  533 ++
 net/nimble/host/test/src/ble_gatt_disc_c_test.c |  547 ++
 net/nimble/host/test/src/ble_gatt_disc_d_test.c |  363 +
 net/nimble/host/test/src/ble_gatt_disc_s_test.c |  406 ++
 net/nimble/host/test/src/ble_gatt_find_s_test.c |  342 +
 net/nimble/host/test/src/ble_gatt_read_test.c   |  823 +++
 net/nimble/host/test/src/ble_gatt_write_test.c  |  639 ++
 .../host/test/src/ble_gatts_notify_test.c       |  983 +++
 net/nimble/host/test/src/ble_gatts_read_test.c  |  261 +
 net/nimble/host/test/src/ble_gatts_reg_test.c   |  718 ++
 net/nimble/host/test/src/ble_hs_adv_test.c      | 1486 ++++
 net/nimble/host/test/src/ble_hs_conn_test.c     |  219 +
 net/nimble/host/test/src/ble_hs_hci_test.c      |   99 +
 net/nimble/host/test/src/ble_hs_test.c          |   61 +
 net/nimble/host/test/src/ble_hs_test_util.c     | 1404 ++++
 net/nimble/host/test/src/ble_hs_test_util.h     |  177 +
 .../host/test/src/ble_hs_test_util_store.c      |  248 +
 .../host/test/src/ble_hs_test_util_store.h      |   36 +
 net/nimble/host/test/src/ble_l2cap_test.c       |  690 ++
 net/nimble/host/test/src/ble_os_test.c          |  401 ++
 net/nimble/host/test/src/ble_sm_lgcy_test.c     |  839 +++
 net/nimble/host/test/src/ble_sm_sc_test.c       | 4910 +++++++++++++
 net/nimble/host/test/src/ble_sm_test.c          |  678 ++
 net/nimble/host/test/src/ble_sm_test_util.c     | 2410 +++++++
 net/nimble/host/test/src/ble_sm_test_util.h     |  119 +
 net/nimble/host/test/src/ble_uuid_test.c        |   93 +
 net/nimble/include/nimble/nimble_opt.h          |  377 -
 net/nimble/include/nimble/nimble_opt_auto.h     |  104 +-
 net/nimble/pkg.yml                              |   22 +
 .../ram/include/transport/ram/ble_hci_ram.h     |   24 +-
 net/nimble/transport/ram/pkg.yml                |   16 +
 net/nimble/transport/ram/src/ble_hci_ram.c      |   28 +-
 .../uart/include/transport/uart/ble_hci_uart.h  |    2 +-
 net/nimble/transport/uart/pkg.yml               |   32 +
 net/nimble/transport/uart/src/ble_hci_uart.c    |   63 +-
 sys/config/include/config/config.h              |    4 +-
 sys/config/pkg.yml                              |   51 +-
 sys/config/src/config.c                         |   31 +-
 sys/config/src/config_cli.c                     |    3 +-
 sys/config/src/config_fcb.c                     |    5 +-
 sys/config/src/config_file.c                    |    4 +-
 sys/config/src/config_init.c                    |  105 +
 sys/config/src/config_json_line.c               |    8 +-
 sys/config/src/config_nmgr.c                    |    8 +-
 sys/config/src/config_store.c                   |    8 +-
 sys/config/src/test/conf_test.c                 |  953 ---
 sys/config/src/test/conf_test.h                 |   25 -
 sys/config/src/test/conf_test_suite.c           |   39 -
 sys/config/src/test/config_test.h               |   24 -
 sys/config/test/pkg.yml                         |   32 +
 sys/config/test/src/conf_test.c                 |  953 +++
 sys/config/test/src/conf_test.h                 |   25 +
 sys/config/test/src/conf_test_suite.c           |   40 +
 sys/config/test/src/config_test.h               |   24 +
 sys/coredump/pkg.yml                            |    2 -
 sys/fcb/pkg.yml                                 |    3 -
 sys/fcb/src/test/fcb_test.c                     |  671 --
 sys/fcb/test/pkg.yml                            |   30 +
 sys/fcb/test/src/fcb_test.c                     |  672 ++
 sys/id/include/id/id.h                          |    2 +-
 sys/id/pkg.yml                                  |   13 +-
 sys/id/src/id.c                                 |   17 +-
 sys/log/include/log/log.h                       |   20 +-
 sys/log/pkg.yml                                 |   36 +-
 sys/log/src/log.c                               |   41 +-
 sys/log/src/log_fcb.c                           |   12 +-
 sys/log/src/log_nmgr.c                          |    8 +-
 sys/log/src/log_shell.c                         |   20 +-
 sys/log/src/test/log_test.c                     |  162 -
 sys/log/test/pkg.yml                            |   33 +
 sys/log/test/src/log_test.c                     |  163 +
 sys/mn_socket/pkg.yml                           |    1 -
 sys/mn_socket/src/test/mn_sock_test.c           |   82 -
 sys/mn_socket/test/pkg.yml                      |   30 +
 sys/mn_socket/test/src/mn_sock_test.c           |   83 +
 sys/reboot/pkg.yml                              |   12 +-
 sys/reboot/src/log_reboot.c                     |   42 +-
 sys/stats/include/stats/stats.h                 |   18 +-
 sys/stats/pkg.yml                               |   23 +-
 sys/stats/src/stats.c                           |   60 +-
 sys/stats/src/stats_nmgr.c                      |   10 +-
 sys/stats/src/stats_shell.c                     |   17 +-
 sys/sysinit/include/sysinit/sysinit.h           |   42 +
 sys/sysinit/pkg.yml                             |   34 +
 targets/unittest/pkg.yml                        |    2 +
 356 files changed, 46034 insertions(+), 44392 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/blecent/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/blecent/pkg.yml b/apps/blecent/pkg.yml
index 3798fee..3fd6796 100644
--- a/apps/blecent/pkg.yml
+++ b/apps/blecent/pkg.yml
@@ -33,6 +33,6 @@ pkg.deps:
     - libs/console/full
     - libs/baselibc
 
-pkg.cflags:
+pkg.syscfg_vals:
     # DEBUG logging is a bit noisy; use INFO.
-    - "-DLOG_LEVEL=1"
+    LOG_LEVEL: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/blecent/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blecent/src/main.c b/apps/blecent/src/main.c
index 66a4e3f..1f9a424 100755
--- a/apps/blecent/src/main.c
+++ b/apps/blecent/src/main.c
@@ -19,19 +19,15 @@
 
 #include <assert.h>
 #include <string.h>
+#include "syscfg/syscfg.h"
 #include "bsp/bsp.h"
 #include "os/os.h"
-#include "hal/hal_cputime.h"
-#include "console/console.h"
 
 /* BLE */
 #include "nimble/ble.h"
 #include "controller/ble_ll.h"
 #include "host/ble_hs.h"
 
-/* RAM HCI transport. */
-#include "transport/ram/ble_hci_ram.h"
-
 /* RAM persistence layer. */
 #include "store/ram/ble_store_ram.h"
 
@@ -42,25 +38,10 @@
 /* Application-specified header. */
 #include "blecent.h"
 
-#define BSWAP16(x)  ((uint16_t)(((x) << 8) | (((x) & 0xff00) >> 8)))
-
-/** Mbuf settings. */
-#define MBUF_NUM_MBUFS      (12)
-#define MBUF_BUF_SIZE       OS_ALIGN(BLE_MBUF_PAYLOAD_SIZE, 4)
-#define MBUF_MEMBLOCK_SIZE  (MBUF_BUF_SIZE + BLE_MBUF_MEMBLOCK_OVERHEAD)
-#define MBUF_MEMPOOL_SIZE   OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
-
-static os_membuf_t blecent_mbuf_mpool_data[MBUF_MEMPOOL_SIZE];
-struct os_mbuf_pool blecent_mbuf_pool;
-struct os_mempool blecent_mbuf_mpool;
-
 /** Log data. */
 static struct log_handler blecent_log_console_handler;
 struct log blecent_log;
 
-/** Priority of the nimble host and controller tasks. */
-#define BLE_LL_TASK_PRI             (OS_TASK_PRI_HIGHEST)
-
 /** blecent task settings. */
 #define BLECENT_TASK_PRIO           1
 #define BLECENT_STACK_SIZE          (OS_STACK_ALIGN(336))
@@ -508,47 +489,12 @@ blecent_task_handler(void *unused)
 int
 main(void)
 {
-    struct ble_hs_cfg cfg;
-    uint32_t seed;
     int rc;
-    int i;
 
     /* Initialize OS */
     os_init();
 
-    /* Set cputime to count at 1 usec increments */
-    rc = cputime_init(1000000);
-    assert(rc == 0);
-
-    /* Seed random number generator with least significant bytes of device
-     * address.
-     */
-    seed = 0;
-    for (i = 0; i < 4; ++i) {
-        seed |= g_dev_addr[i];
-        seed <<= 8;
-    }
-    srand(seed);
-
-    /* Initialize msys mbufs. */
-    rc = os_mempool_init(&blecent_mbuf_mpool, MBUF_NUM_MBUFS,
-                         MBUF_MEMBLOCK_SIZE, blecent_mbuf_mpool_data,
-                         "blecent_mbuf_data");
-    assert(rc == 0);
-
-    rc = os_mbuf_pool_init(&blecent_mbuf_pool, &blecent_mbuf_mpool,
-                           MBUF_MEMBLOCK_SIZE, MBUF_NUM_MBUFS);
-    assert(rc == 0);
-
-    rc = os_msys_register(&blecent_mbuf_pool);
-    assert(rc == 0);
-
-    /* Initialize the console (for log output). */
-    rc = console_init(NULL);
-    assert(rc == 0);
-
-    /* Initialize the logging system. */
-    log_init();
+    /* Initialize the blecent log. */
     log_console_handler_init(&blecent_log_console_handler);
     log_register("blecent", &blecent_log, &blecent_log_console_handler);
 
@@ -562,39 +508,15 @@ main(void)
                  NULL, BLECENT_TASK_PRIO, OS_WAIT_FOREVER,
                  blecent_stack, BLECENT_STACK_SIZE);
 
-    /* Initialize the BLE LL */
-    rc = ble_ll_init(BLE_LL_TASK_PRI, MBUF_NUM_MBUFS, BLE_MBUF_PAYLOAD_SIZE);
-    assert(rc == 0);
-
-    /* Initialize the RAM HCI transport. */
-    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
-    assert(rc == 0);
-
     /* Configure the host. */
-    cfg = ble_hs_cfg_dflt;
-    cfg.max_hci_bufs = 3;
-    cfg.max_gattc_procs = 5;
-    cfg.sm_bonding = 1;
-    cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
-    cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
-    cfg.reset_cb = blecent_on_reset;
-    cfg.sync_cb = blecent_on_sync;
-    cfg.store_read_cb = ble_store_ram_read;
-    cfg.store_write_cb = ble_store_ram_write;
-
-    /* Initialize GATT services. */
-    rc = ble_svc_gap_init(&cfg);
-    assert(rc == 0);
-
-    rc = ble_svc_gatt_init(&cfg);
-    assert(rc == 0);
-
-    /* Initialize the BLE host. */
-    rc = ble_hs_init(&blecent_evq, &cfg);
-    assert(rc == 0);
+    ble_hs_cfg.parent_evq = &blecent_evq;
+    ble_hs_cfg.reset_cb = blecent_on_reset;
+    ble_hs_cfg.sync_cb = blecent_on_sync;
+    ble_hs_cfg.store_read_cb = ble_store_ram_read;
+    ble_hs_cfg.store_write_cb = ble_store_ram_write;
 
     /* Initialize data structures to track connected peers. */
-    rc = peer_init(cfg.max_connections, 64, 64, 64);
+    rc = peer_init(MYNEWT_VAL(BLE_MAX_CONNECTIONS), 64, 64, 64);
     assert(rc == 0);
 
     /* Set the default device name. */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/blehci/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blehci/src/main.c b/apps/blehci/src/main.c
index 20fc4bc..5c3f7f4 100755
--- a/apps/blehci/src/main.c
+++ b/apps/blehci/src/main.c
@@ -18,67 +18,19 @@
  */
 #include <assert.h>
 #include "os/os.h"
-#include "hal/hal_cputime.h"
-#include "hal/hal_uart.h"
-
-/* BLE */
-#include "nimble/ble.h"
-#include "controller/ble_ll.h"
-#include "transport/uart/ble_hci_uart.h"
-
-/* Nimble task priorities */
-#define BLE_LL_TASK_PRI         (OS_TASK_PRI_HIGHEST)
-
-/* Create a mbuf pool of BLE mbufs */
-#define MBUF_NUM_MBUFS      (7)
-#define MBUF_BUF_SIZE       OS_ALIGN(BLE_MBUF_PAYLOAD_SIZE, 4)
-#define MBUF_MEMBLOCK_SIZE  (MBUF_BUF_SIZE + BLE_MBUF_MEMBLOCK_OVERHEAD)
-#define MBUF_MEMPOOL_SIZE   OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
 
 /* Our global device address (public) */
-uint8_t g_dev_addr[BLE_DEV_ADDR_LEN] = { 0 };
+uint8_t g_dev_addr[6] = { 0 };
 
 /* Our random address (in case we need it) */
-uint8_t g_random_addr[BLE_DEV_ADDR_LEN] = { 0 };
-
-#define HCI_MAX_BUFS        (5)
-
-os_membuf_t default_mbuf_mpool_data[MBUF_MEMPOOL_SIZE];
-
-struct os_mbuf_pool default_mbuf_pool;
-struct os_mempool default_mbuf_mpool;
+uint8_t g_random_addr[6] = { 0 };
 
 int
 main(void)
 {
-    int rc;
-
     /* Initialize OS */
     os_init();
 
-    /* Set cputime to count at 1 usec increments */
-    rc = cputime_init(1000000);
-    assert(rc == 0);
-
-    rc = os_mempool_init(&default_mbuf_mpool, MBUF_NUM_MBUFS,
-                         MBUF_MEMBLOCK_SIZE, default_mbuf_mpool_data,
-                         "default_mbuf_data");
-    assert(rc == 0);
-
-    rc = os_mbuf_pool_init(&default_mbuf_pool, &default_mbuf_mpool,
-                           MBUF_MEMBLOCK_SIZE, MBUF_NUM_MBUFS);
-    assert(rc == 0);
-
-    rc = os_msys_register(&default_mbuf_pool);
-    assert(rc == 0);
-
-    /* Initialize the BLE LL */
-    rc = ble_ll_init(BLE_LL_TASK_PRI, MBUF_NUM_MBUFS, BLE_MBUF_PAYLOAD_SIZE);
-    assert(rc == 0);
-
-    rc = ble_hci_uart_init(&ble_hci_uart_cfg_dflt);
-    assert(rc == 0);
-
     /* Start the OS */
     os_start();
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bleprph/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bleprph/pkg.yml b/apps/bleprph/pkg.yml
index 7c45ee2..42c89d0 100644
--- a/apps/bleprph/pkg.yml
+++ b/apps/bleprph/pkg.yml
@@ -34,14 +34,22 @@ pkg.deps:
     - libs/baselibc
     - libs/newtmgr
     - libs/newtmgr/transport/ble
+    - sys/sysinit
 
-pkg.cflags:
+pkg.syscfg_vals:
     # Use INFO log level to reduce code size.  DEBUG is too large for nRF51.
-    - "-DLOG_LEVEL=1"
+    LOG_LEVEL: 1
 
-    # Disable unused roles; bleprph is a peripheral-only app.
-    - "-DNIMBLE_OPT_ROLE_OBSERVER=0"
-    - "-DNIMBLE_OPT_ROLE_CENTRAL=0"
+    BOOTUTIL_NFFS: 0
+    CONFIG_NFFS: 0
+
+    # Disable central and observer roles.
+    BLE_ROLE_BROADCASTER: 1
+    BLE_ROLE_CENTRAL: 0
+    BLE_ROLE_OBSERVER: 0
+    BLE_ROLE_PERIPHERAL: 1
 
     # Disable unused eddystone feature.
-    - "-DNIMBLE_OPT_EDDYSTONE=0"
+    BLE_EDDYSTONE: 0
+
+    BLE_SM: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bleprph/src/bleprph.h
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/bleprph.h b/apps/bleprph/src/bleprph.h
index 2e3c539..8db1dc7 100644
--- a/apps/bleprph/src/bleprph.h
+++ b/apps/bleprph/src/bleprph.h
@@ -42,7 +42,7 @@ extern struct log bleprph_log;
 #define GATT_SVR_CHR_ALERT_NOT_CTRL_PT        0x2A44
 
 void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg);
-int gatt_svr_init(struct ble_hs_cfg *cfg);
+int gatt_svr_init(void);
 
 /** Misc. */
 void print_bytes(const uint8_t *bytes, int len);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bleprph/src/gatt_svr.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/gatt_svr.c b/apps/bleprph/src/gatt_svr.c
index 061db76..c71a43a 100644
--- a/apps/bleprph/src/gatt_svr.c
+++ b/apps/bleprph/src/gatt_svr.c
@@ -316,11 +316,11 @@ gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
 }
 
 int
-gatt_svr_init(struct ble_hs_cfg *cfg)
+gatt_svr_init(void)
 {
     int rc;
 
-    rc = ble_gatts_count_cfg(gatt_svr_svcs, cfg);
+    rc = ble_gatts_count_cfg(gatt_svr_svcs);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index 9d75a66..9af8006 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -32,67 +32,25 @@
 /* BLE */
 #include "nimble/ble.h"
 #include "host/ble_hs.h"
-#include "host/ble_hs_adv.h"
-#include "host/ble_uuid.h"
-#include "host/ble_att.h"
-#include "host/ble_gap.h"
-#include "host/ble_gatt.h"
-#include "host/ble_l2cap.h"
-#include "host/ble_sm.h"
-#include "controller/ble_ll.h"
-
-/* RAM HCI transport. */
-#include "transport/ram/ble_hci_ram.h"
-
-/* RAM persistence layer. */
-#include "store/ram/ble_store_ram.h"
-
-/* Mandatory services. */
 #include "services/mandatory/ble_svc_gap.h"
-#include "services/mandatory/ble_svc_gatt.h"
-
-/* Newtmgr include */
-#include "newtmgr/newtmgr.h"
-#include "nmgrble/newtmgr_ble.h"
 
 /* Application-specified header. */
 #include "bleprph.h"
 
-/** Mbuf settings. */
-#define MBUF_NUM_MBUFS      (12)
-#define MBUF_BUF_SIZE       OS_ALIGN(BLE_MBUF_PAYLOAD_SIZE, 4)
-#define MBUF_MEMBLOCK_SIZE  (MBUF_BUF_SIZE + BLE_MBUF_MEMBLOCK_OVERHEAD)
-#define MBUF_MEMPOOL_SIZE   OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
-
-static os_membuf_t bleprph_mbuf_mpool_data[MBUF_MEMPOOL_SIZE];
-struct os_mbuf_pool bleprph_mbuf_pool;
-struct os_mempool bleprph_mbuf_mpool;
+#include "nmgrble/newtmgr_ble.h"
 
 /** Log data. */
 static struct log_handler bleprph_log_console_handler;
 struct log bleprph_log;
 
-/** Priority of the nimble host and controller tasks. */
-#define BLE_LL_TASK_PRI             (OS_TASK_PRI_HIGHEST)
-
 /** bleprph task settings. */
 #define BLEPRPH_TASK_PRIO           1
 #define BLEPRPH_STACK_SIZE          (OS_STACK_ALIGN(336))
 
-#define NEWTMGR_TASK_PRIO (4)
-#define NEWTMGR_TASK_STACK_SIZE (OS_STACK_ALIGN(512))
-os_stack_t newtmgr_stack[NEWTMGR_TASK_STACK_SIZE];
-
 struct os_eventq bleprph_evq;
 struct os_task bleprph_task;
 bssnz_t os_stack_t bleprph_stack[BLEPRPH_STACK_SIZE];
 
-/** Our global device address (public) */
-uint8_t g_dev_addr[BLE_DEV_ADDR_LEN] = {0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a};
-
-/** Our random address (in case we need it) */
-uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
-
 static int bleprph_gap_event(struct ble_gap_event *event, void *arg);
 
 /**
@@ -332,47 +290,15 @@ bleprph_task_handler(void *unused)
 int
 main(void)
 {
-    struct ble_hs_cfg cfg;
-    uint32_t seed;
     int rc;
-    int i;
+
+    /* Set initial BLE device address. */
+    memcpy(g_dev_addr, (uint8_t[6]){0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a}, 6);
 
     /* Initialize OS */
     os_init();
 
-    /* Set cputime to count at 1 usec increments */
-    rc = cputime_init(1000000);
-    assert(rc == 0);
-
-    /* Seed random number generator with least significant bytes of device
-     * address.
-     */
-    seed = 0;
-    for (i = 0; i < 4; ++i) {
-        seed |= g_dev_addr[i];
-        seed <<= 8;
-    }
-    srand(seed);
-
-    /* Initialize msys mbufs. */
-    rc = os_mempool_init(&bleprph_mbuf_mpool, MBUF_NUM_MBUFS,
-                         MBUF_MEMBLOCK_SIZE, bleprph_mbuf_mpool_data,
-                         "bleprph_mbuf_data");
-    assert(rc == 0);
-
-    rc = os_mbuf_pool_init(&bleprph_mbuf_pool, &bleprph_mbuf_mpool,
-                           MBUF_MEMBLOCK_SIZE, MBUF_NUM_MBUFS);
-    assert(rc == 0);
-
-    rc = os_msys_register(&bleprph_mbuf_pool);
-    assert(rc == 0);
-
-    /* Initialize the console (for log output). */
-    rc = console_init(NULL);
-    assert(rc == 0);
-
-    /* Initialize the logging system. */
-    log_init();
+    /* Initialize the bleprph log. */
     log_console_handler_init(&bleprph_log_console_handler);
     log_register("bleprph", &bleprph_log, &bleprph_log_console_handler);
 
@@ -386,46 +312,15 @@ main(void)
                  NULL, BLEPRPH_TASK_PRIO, OS_WAIT_FOREVER,
                  bleprph_stack, BLEPRPH_STACK_SIZE);
 
-    /* Initialize the BLE LL */
-    rc = ble_ll_init(BLE_LL_TASK_PRI, MBUF_NUM_MBUFS, BLE_MBUF_PAYLOAD_SIZE);
-    assert(rc == 0);
-
-    /* Initialize the RAM HCI transport. */
-    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
-    assert(rc == 0);
-
     /* Initialize the NimBLE host configuration. */
-    cfg = ble_hs_cfg_dflt;
-    cfg.max_gattc_procs = 2;
-    cfg.sm_bonding = 1;
-    cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
-    cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
-    cfg.reset_cb = bleprph_on_reset;
-    cfg.sync_cb = bleprph_on_sync;
-    cfg.store_read_cb = ble_store_ram_read;
-    cfg.store_write_cb = ble_store_ram_write;
-    cfg.gatts_register_cb = gatt_svr_register_cb;
-
-    /* Initialize GATT services. */
-    rc = ble_svc_gap_init(&cfg);
-    assert(rc == 0);
-
-    rc = ble_svc_gatt_init(&cfg);
-    assert(rc == 0);
-
-    rc = nmgr_ble_gatt_svr_init(&bleprph_evq, &cfg);
-    assert(rc == 0);
-
-    rc = gatt_svr_init(&cfg);
-    assert(rc == 0);
+    ble_hs_cfg.parent_evq = &bleprph_evq;
+    ble_hs_cfg.reset_cb = bleprph_on_reset;
+    ble_hs_cfg.sync_cb = bleprph_on_sync;
+    ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb;
 
-    /* Initialize NimBLE host. */
-    rc = ble_hs_init(&bleprph_evq, &cfg);
+    rc = gatt_svr_init();
     assert(rc == 0);
 
-    nmgr_task_init(NEWTMGR_TASK_PRIO, newtmgr_stack, NEWTMGR_TASK_STACK_SIZE);
-    imgmgr_module_init();
-
     /* Set the default device name. */
     rc = ble_svc_gap_device_name_set("nimble-bleprph");
     assert(rc == 0);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bletest/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bletest/pkg.yml b/apps/bletest/pkg.yml
index 3e94ede..c435194 100644
--- a/apps/bletest/pkg.yml
+++ b/apps/bletest/pkg.yml
@@ -38,3 +38,7 @@ pkg.deps:
     - sys/log
     - sys/stats
 pkg.cflags: -DBLETEST
+
+pkg.syscfg_vals:
+    MSYS_1_BLOCK_COUNT: 16
+    MSYS_1_BLOCK_SIZE: 260

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bletest/src/bletest_hci.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/bletest_hci.c b/apps/bletest/src/bletest_hci.c
index 64d0270..7c8bb08 100755
--- a/apps/bletest/src/bletest_hci.c
+++ b/apps/bletest/src/bletest_hci.c
@@ -59,7 +59,7 @@ bletest_send_conn_update(uint16_t handle)
     assert(rc == 0);
 }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 void
 bletest_ltk_req_reply(uint16_t handle)
 {
@@ -153,7 +153,7 @@ bletest_hci_rd_bd_addr(void)
     return rc;
 }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 int
 bletest_hci_le_encrypt(uint8_t *key, uint8_t *pt)
 {
@@ -372,7 +372,7 @@ bletest_hci_le_set_adv_data(uint8_t *data, uint8_t len)
     return ble_hs_hci_cmd_tx_empty_ack(buf);
 }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 int
 bletest_hci_le_start_encrypt(struct hci_start_encrypt *cmd)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bletest/src/bletest_priv.h
----------------------------------------------------------------------
diff --git a/apps/bletest/src/bletest_priv.h b/apps/bletest/src/bletest_priv.h
index 28f3074..05d03d1 100644
--- a/apps/bletest/src/bletest_priv.h
+++ b/apps/bletest/src/bletest_priv.h
@@ -22,7 +22,7 @@
 
 void bletest_send_conn_update(uint16_t handle);
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 void bletest_ltk_req_reply(uint16_t handle);
 int bletest_send_ltk_req_neg_reply(uint16_t handle);
 int bletest_send_ltk_req_reply(uint16_t handle);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index c2635dd..70b68dd 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -54,23 +54,7 @@
 #include "../src/ble_hs_priv.h"
 #include "bletest_priv.h"
 
-/* Task priorities */
-#define BLE_LL_TASK_PRI     (OS_TASK_PRI_HIGHEST)
-#define HOST_TASK_PRIO      (OS_TASK_PRI_HIGHEST + 1)
-#define BLETEST_TASK_PRIO   (HOST_TASK_PRIO + 1)
-#define SHELL_TASK_PRIO     (BLETEST_TASK_PRIO + 1)
-#define NEWTMGR_TASK_PRIO   (SHELL_TASK_PRIO + 1)
-
-/* Shell task stack */
-#define SHELL_TASK_STACK_SIZE (OS_STACK_ALIGN(256))
-os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
-
-/* Newt manager task stack */
-#define NEWTMGR_TASK_STACK_SIZE (OS_STACK_ALIGN(448))
-os_stack_t newtmgr_stack[NEWTMGR_TASK_STACK_SIZE];
-
-/* Shell maximum input line length */
-#define SHELL_MAX_INPUT_LEN     (256)
+#define BLETEST_TASK_PRIO   5
 
 /* For LED toggling */
 int g_led_pin;
@@ -85,16 +69,6 @@ uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
 uint8_t g_host_adv_data[BLE_HCI_MAX_ADV_DATA_LEN];
 uint8_t g_host_adv_len;
 
-/* Create a mbuf pool of BLE mbufs */
-#define MBUF_NUM_MBUFS      (16)
-#define MBUF_BUF_SIZE       OS_ALIGN(BLE_MBUF_PAYLOAD_SIZE, 4)
-#define MBUF_MEMBLOCK_SIZE  (MBUF_BUF_SIZE + BLE_MBUF_MEMBLOCK_OVERHEAD)
-#define MBUF_MEMPOOL_SIZE   OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
-
-struct os_mbuf_pool g_mbuf_pool;
-struct os_mempool g_mbuf_mempool;
-os_membuf_t g_mbuf_buffer[MBUF_MEMPOOL_SIZE];
-
 /* Some application configurations */
 #define BLETEST_ROLE_ADVERTISER         (0)
 #define BLETEST_ROLE_SCANNER            (1)
@@ -190,7 +164,7 @@ const uint8_t g_ble_ll_encrypt_test_encrypted_data[16] =
     0x05, 0x8e, 0x3b, 0x8e, 0x27, 0xc2, 0xc6, 0x66
 };
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 uint8_t g_bletest_adv_irk[16] = {
     0xec, 0x02, 0x34, 0xa3, 0x57, 0xc8, 0xad, 0x05,
     0x34, 0x10, 0x10, 0xa6, 0x0a, 0x39, 0x7d, 0x9b
@@ -202,7 +176,7 @@ uint8_t g_bletest_init_irk[16] = {
 };
 #endif
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /* LTK 0x4C68384139F574D836BCF34E9DFB01BF */
 const uint8_t g_bletest_LTK[16] =
 {
@@ -386,7 +360,7 @@ bletest_init_advertising(void)
     rc = bletest_hci_le_set_adv_params(&adv);
     assert(rc == 0);
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     if ((adv.own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) ||
         (BLETEST_CFG_ADV_ADDR_RES_EN == 1)) {
         rc = bletest_hci_le_add_resolv_list(g_bletest_adv_irk,
@@ -432,7 +406,7 @@ bletest_init_scanner(void)
     rc = ble_hs_hci_cmd_tx_empty_ack(buf);
     if (rc == 0) {
         add_whitelist = BLETEST_CFG_SCAN_FILT_POLICY;
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
         if (own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
             rc = bletest_hci_le_add_resolv_list(g_bletest_init_irk,
                                                 g_bletest_adv_irk,
@@ -512,7 +486,7 @@ bletest_init_initiator(void)
         assert(rc == 0);
     }
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
         if ((hcc->peer_addr_type > BLE_HCI_CONN_PEER_ADDR_RANDOM) ||
             (hcc->own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM)) {
             rc = bletest_hci_le_add_resolv_list(g_bletest_init_irk,
@@ -587,7 +561,7 @@ bletest_execute_initiator(void)
                 new_chan_map[4] = 0;
                 bletest_hci_le_set_host_chan_class(new_chan_map);
             } else if (g_bletest_state == 4) {
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
                 struct hci_start_encrypt hsle;
                 for (i = 0; i < g_bletest_current_conns; ++i) {
                     if (ble_ll_conn_find_active_conn(i + 1)) {
@@ -601,7 +575,7 @@ bletest_execute_initiator(void)
                 }
 #endif
             } else if (g_bletest_state == 8) {
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
                 struct hci_start_encrypt hsle;
                 for (i = 0; i < g_bletest_current_conns; ++i) {
                     if (ble_ll_conn_find_active_conn(i + 1)) {
@@ -725,7 +699,7 @@ bletest_execute_advertiser(void)
 #if (BLETEST_CONCURRENT_CONN_TEST == 1)
     /* See if it is time to hand a data packet to the connection */
     if ((int32_t)(os_time_get() - g_next_os_time) >= 0) {
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
         /* Do we need to send a LTK reply? */
         if (g_bletest_ltk_reply_handle) {
             //bletest_send_ltk_req_neg_reply(g_bletest_ltk_reply_handle);
@@ -965,7 +939,7 @@ bletest_task_handler(void *arg)
     rc = bletest_hci_le_rd_max_datalen();
     assert(rc == 0);
 
-#if (BLE_LL_CFG_FEAT_DATA_LEN_EXT == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_DATA_LEN_EXT) == 1)
     /* Read suggested data length */
     rc = bletest_hci_le_rd_sugg_datalen();
     assert(rc == 0);
@@ -986,7 +960,7 @@ bletest_task_handler(void *arg)
 #endif
 
     /* Encrypt a block */
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     rc = bletest_hci_le_encrypt((uint8_t *)g_ble_ll_encrypt_test_key,
                                 (uint8_t *)g_ble_ll_encrypt_test_plain_text);
     assert(rc == 0);
@@ -1037,32 +1011,11 @@ bletest_task_handler(void *arg)
 int
 main(void)
 {
-    int i;
     int rc;
-    uint32_t seed;
-#if 0
-    int cnt;
-    struct nffs_area_desc descs[NFFS_AREA_MAX];
-#endif
 
     /* Initialize OS */
     os_init();
 
-    /* Set cputime to count at 1 usec increments */
-    rc = cputime_init(1000000);
-    assert(rc == 0);
-
-    rc = os_mempool_init(&g_mbuf_mempool, MBUF_NUM_MBUFS,
-            MBUF_MEMBLOCK_SIZE, &g_mbuf_buffer[0], "mbuf_pool");
-    assert(rc == 0);
-
-    rc = os_mbuf_pool_init(&g_mbuf_pool, &g_mbuf_mempool, MBUF_MEMBLOCK_SIZE,
-                           MBUF_NUM_MBUFS);
-    assert(rc == 0);
-
-    rc = os_msys_register(&g_mbuf_pool);
-    assert(rc == 0);
-
     /* Dummy device address */
 #if BLETEST_CFG_ROLE == BLETEST_ROLE_ADVERTISER
     g_dev_addr[0] = 0x00;
@@ -1094,72 +1047,16 @@ main(void)
     g_bletest_cur_peer_addr[5] = 0x08;
 #endif
 
-    /*
-     * Seed random number generator with least significant bytes of device
-     * address.
-     */
-    seed = 0;
-    for (i = 0; i < 4; ++i) {
-        seed |= g_dev_addr[i];
-        seed <<= 8;
-    }
-    srand(seed);
+    /* Set the NimBLE host's parent event queue. */
+    ble_hs_cfg.parent_evq = &g_bletest_evq;
 
     /* Set the led pin as an output */
     g_led_pin = LED_BLINK_PIN;
     hal_gpio_init_out(g_led_pin, 1);
 
-#if 0
-    rc = hal_flash_init();
-    assert(rc == 0);
-
-    nffs_config.nc_num_inodes = 32;
-    nffs_config.nc_num_blocks = 64;
-    nffs_config.nc_num_files = 2;
-    nffs_config.nc_num_dirs = 2;
-    rc = nffs_init();
-    assert(rc == 0);
-
-    cnt = NFFS_AREA_MAX;
-    rc = flash_area_to_nffs_desc(FLASH_AREA_NFFS, &cnt, descs);
-    assert(rc == 0);
-    if (nffs_detect(descs) == FS_ECORRUPT) {
-        rc = nffs_format(descs);
-        assert(rc == 0);
-    }
-#endif
-
-    rc = shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
-                         SHELL_MAX_INPUT_LEN);
-    assert(rc == 0);
-
-    rc = nmgr_task_init(NEWTMGR_TASK_PRIO, newtmgr_stack,
-                        NEWTMGR_TASK_STACK_SIZE);
-    assert(rc == 0);
-
-#if 0
-    imgmgr_module_init();
-#endif
-
-    /* Init statistics module */
-    rc = stats_module_init();
-    assert(rc == 0);
-
     /* Initialize eventq for bletest task */
     os_eventq_init(&g_bletest_evq);
 
-    /* Initialize the BLE LL */
-    rc = ble_ll_init(BLE_LL_TASK_PRI, MBUF_NUM_MBUFS, BLE_MBUF_PAYLOAD_SIZE);
-    assert(rc == 0);
-
-    /* Initialize host */
-    rc = ble_hs_init(&g_bletest_evq, NULL);
-    assert(rc == 0);
-
-    /* Initialize the RAM HCI transport. */
-    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
-    assert(rc == 0);
-
     rc = os_task_init(&bletest_task, "bletest", bletest_task_handler, NULL,
                       BLETEST_TASK_PRIO, OS_WAIT_FOREVER, bletest_stack,
                       BLETEST_STACK_SIZE);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bletiny/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bletiny/pkg.yml b/apps/bletiny/pkg.yml
index 625adc6..2eb4998 100644
--- a/apps/bletiny/pkg.yml
+++ b/apps/bletiny/pkg.yml
@@ -33,14 +33,14 @@ pkg.deps:
     - libs/console/full
     - libs/shell
 
-pkg.cflags:
-    ### Disable some features to make bletiny small enough to fit on the nRF51.
-    ### These features can be re-enabled in the target definition.
+### Disable some features to make bletiny small enough to fit on the nRF51.
+### These features can be re-enabled in the target definition.
+pkg.syscfg_vals:
     # Set log level to info (disable debug logging).
-    - "-DLOG_LEVEL=1"
+    LOG_LEVEL: 1
 
     # Disable security manager (pairing and bonding).
-    - "-DNIMBLE_OPT_SM=0"
+    BLE_SM: 0
 
     # Disable eddystone beacons.
-    - "-DNIMBLE_OPT_EDDYSTONE=0"
+    BLE_EDDYSTONE: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bletiny/src/bletiny.h
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/bletiny.h b/apps/bletiny/src/bletiny.h
index e24f0f0..af75bfa 100644
--- a/apps/bletiny/src/bletiny.h
+++ b/apps/bletiny/src/bletiny.h
@@ -78,7 +78,7 @@ struct bletiny_conn {
     struct bletiny_svc_list svcs;
 };
 
-extern struct bletiny_conn bletiny_conns[NIMBLE_OPT(MAX_CONNECTIONS)];
+extern struct bletiny_conn bletiny_conns[MYNEWT_VAL(BLE_MAX_CONNECTIONS)];
 extern int bletiny_num_conns;
 
 extern uint16_t nm_attr_val_handle;
@@ -190,7 +190,7 @@ int bletiny_rssi(uint16_t conn_handle, int8_t *out_rssi);
 #define GATT_SVR_CHR_ALERT_NOT_CTRL_PT        0x2A44
 
 void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg);
-int gatt_svr_init(struct ble_hs_cfg *cfg);
+int gatt_svr_init(void);
 
 /** Misc. */
 void print_bytes(const uint8_t *bytes, int len);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bletiny/src/cmd.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/cmd.c b/apps/bletiny/src/cmd.c
index e50d6b0..7c6889a 100644
--- a/apps/bletiny/src/cmd.c
+++ b/apps/bletiny/src/cmd.c
@@ -1821,7 +1821,7 @@ cmd_wl(int argc, char **argv)
 static int
 cmd_write(int argc, char **argv)
 {
-    struct ble_gatt_attr attrs[NIMBLE_OPT(GATT_WRITE_MAX_ATTRS)] = { { 0 } };
+    struct ble_gatt_attr attrs[MYNEWT_VAL(BLE_GATT_WRITE_MAX_ATTRS)] = { { 0 } };
     uint16_t attr_handle;
     uint16_t conn_handle;
     int total_attr_len;
@@ -2162,7 +2162,7 @@ cmd_keystore(int argc, char **argv)
 static int
 cmd_passkey(int argc, char **argv)
 {
-#if !NIMBLE_OPT(SM)
+#if !NIMBLE_BLE_SM
     return BLE_HS_ENOTSUP;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bletiny/src/gatt_svr.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/gatt_svr.c b/apps/bletiny/src/gatt_svr.c
index ca920e2..1a128b9 100644
--- a/apps/bletiny/src/gatt_svr.c
+++ b/apps/bletiny/src/gatt_svr.c
@@ -326,11 +326,11 @@ gatt_svr_register(void)
 }
 
 int
-gatt_svr_init(struct ble_hs_cfg *cfg)
+gatt_svr_init(void)
 {
     int rc;
 
-    rc = ble_gatts_count_cfg(gatt_svr_svcs, cfg);
+    rc = ble_gatts_count_cfg(gatt_svr_svcs);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletiny/src/main.c b/apps/bletiny/src/main.c
index dbb340b..76ec1c6 100755
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <errno.h>
+#include "syscfg/syscfg.h"
 #include "bsp/bsp.h"
 #include "log/log.h"
 #include "stats/stats.h"
@@ -63,16 +64,8 @@
 #include "../src/ble_hs_atomic_priv.h"
 #include "../src/ble_hs_hci_priv.h"
 
-/* Nimble task priorities */
-#define BLE_LL_TASK_PRI         (OS_TASK_PRI_HIGHEST)
-
-#define SHELL_TASK_PRIO         (3)
-#define SHELL_MAX_INPUT_LEN     (256)
-#define SHELL_TASK_STACK_SIZE   (OS_STACK_ALIGN(512))
-static bssnz_t os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
-
 /* Our global device address (public) */
-uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
+uint8_t g_dev_addr[BLE_DEV_ADDR_LEN] = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a};
 
 /* Our random address (in case we need it) */
 uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
@@ -80,25 +73,11 @@ uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
 /* A buffer for host advertising data */
 uint8_t g_host_adv_len;
 
-/** Our public address.  Note: this is in reverse byte order. */
-static uint8_t bletiny_addr[6] = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a};
-
-/* Create a mbuf pool of BLE mbufs */
-#define MBUF_NUM_MBUFS      (16)
-#define MBUF_BUF_SIZE       OS_ALIGN(BLE_MBUF_PAYLOAD_SIZE, 4)
-#define MBUF_MEMBLOCK_SIZE  (MBUF_BUF_SIZE + BLE_MBUF_MEMBLOCK_OVERHEAD)
-#define MBUF_MEMPOOL_SIZE   OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
-
-os_membuf_t default_mbuf_mpool_data[MBUF_MEMPOOL_SIZE];
-
-struct os_mbuf_pool default_mbuf_pool;
-struct os_mempool default_mbuf_mpool;
-
 /* BLETINY variables */
 #define BLETINY_STACK_SIZE             (OS_STACK_ALIGN(512))
 #define BLETINY_TASK_PRIO              1
 
-#if NIMBLE_OPT(ROLE_CENTRAL)
+#if MYNEWT_VAL(BLE_ROLE_CENTRAL)
 #define BLETINY_MAX_SVCS               32
 #define BLETINY_MAX_CHRS               64
 #define BLETINY_MAX_DSCS               64
@@ -115,7 +94,7 @@ bssnz_t os_stack_t bletiny_stack[BLETINY_STACK_SIZE];
 static struct log_handler bletiny_log_console_handler;
 struct log bletiny_log;
 
-bssnz_t struct bletiny_conn bletiny_conns[NIMBLE_OPT(MAX_CONNECTIONS)];
+bssnz_t struct bletiny_conn bletiny_conns[MYNEWT_VAL(BLE_MAX_CONNECTIONS)];
 int bletiny_num_conns;
 
 static void *bletiny_svc_mem;
@@ -631,7 +610,7 @@ bletiny_conn_add(struct ble_gap_conn_desc *desc)
 {
     struct bletiny_conn *conn;
 
-    assert(bletiny_num_conns < NIMBLE_OPT(MAX_CONNECTIONS));
+    assert(bletiny_num_conns < MYNEWT_VAL(BLE_MAX_CONNECTIONS));
 
     conn = bletiny_conns + bletiny_num_conns;
     bletiny_num_conns++;
@@ -659,7 +638,7 @@ bletiny_conn_delete_idx(int idx)
     /* This '#if' is not strictly necessary.  It is here to prevent a spurious
      * warning from being reported.
      */
-#if NIMBLE_OPT(MAX_CONNECTIONS) > 1
+#if MYNEWT_VAL(BLE_MAX_CONNECTIONS) > 1
     int i;
     for (i = idx + 1; i < bletiny_num_conns; i++) {
         bletiny_conns[i - 1] = bletiny_conns[i];
@@ -1056,8 +1035,9 @@ bletiny_tx_timer_cb(void *arg)
     }
 
     len = bletiny_tx_data.tx_len;
+
     om = NULL;
-    if (default_mbuf_mpool.mp_num_free >= 4) {
+    if (os_msys_num_free() >= 4) {
         om = os_msys_get_pkthdr(len + 4, sizeof(struct ble_mbuf_hdr));
     }
 
@@ -1428,7 +1408,7 @@ bletiny_l2cap_update(uint16_t conn_handle,
 int
 bletiny_sec_pair(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT(SM)
+#if !NIMBLE_BLE_SM
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1441,7 +1421,7 @@ bletiny_sec_pair(uint16_t conn_handle)
 int
 bletiny_sec_start(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT(SM)
+#if !NIMBLE_BLE_SM
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1458,7 +1438,7 @@ bletiny_sec_restart(uint16_t conn_handle,
                     uint64_t rand_val,
                     int auth)
 {
-#if !NIMBLE_OPT(SM)
+#if !NIMBLE_BLE_SM
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1527,7 +1507,7 @@ bletiny_tx_start(uint16_t handle, uint16_t len, uint16_t rate, uint16_t num)
     }
 
     /* XXX: for now, must have contiguous mbuf space */
-    if ((len + 4) > MBUF_BUF_SIZE) {
+    if ((len + 4) > MYNEWT_VAL_MSYS_1_BLOCK_SIZE) {
         return -2;
     }
 
@@ -1602,33 +1582,11 @@ bletiny_task_handler(void *arg)
 int
 main(void)
 {
-    struct ble_hci_ram_cfg hci_cfg;
-    struct ble_hs_cfg cfg;
-    uint32_t seed;
     int rc;
-    int i;
 
     /* Initialize OS */
     os_init();
 
-    /* Set cputime to count at 1 usec increments */
-    rc = cputime_init(1000000);
-    assert(rc == 0);
-
-    /* Dummy device address */
-    memcpy(g_dev_addr, bletiny_addr, 6);
-
-    /*
-     * Seed random number generator with least significant bytes of device
-     * address.
-     */
-    seed = 0;
-    for (i = 0; i < 4; ++i) {
-        seed |= g_dev_addr[i];
-        seed <<= 8;
-    }
-    srand(seed);
-
     /* Allocate some application specific memory pools. */
     bletiny_svc_mem = malloc(
         OS_MEMPOOL_BYTES(BLETINY_MAX_SVCS, sizeof (struct bletiny_svc)));
@@ -1657,26 +1615,7 @@ main(void)
                          "bletiny_dsc_pool");
     assert(rc == 0);
 
-    /* Initialize msys mbufs. */
-    rc = os_mempool_init(&default_mbuf_mpool, MBUF_NUM_MBUFS,
-                         MBUF_MEMBLOCK_SIZE, default_mbuf_mpool_data,
-                         "default_mbuf_data");
-    assert(rc == 0);
-
-    rc = os_mbuf_pool_init(&default_mbuf_pool, &default_mbuf_mpool,
-                           MBUF_MEMBLOCK_SIZE, MBUF_NUM_MBUFS);
-    assert(rc == 0);
-
-    rc = os_msys_register(&default_mbuf_pool);
-    assert(rc == 0);
-
-    /* Create the shell task. */
-    rc = shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
-                         SHELL_MAX_INPUT_LEN);
-    assert(rc == 0);
-
-    /* Initialize the logging system. */
-    log_init();
+    /* Initialize the bletiny system. */
     log_console_handler_init(&bletiny_log_console_handler);
     log_register("bletiny", &bletiny_log, &bletiny_log_console_handler);
 
@@ -1690,38 +1629,14 @@ main(void)
                  NULL, BLETINY_TASK_PRIO, OS_WAIT_FOREVER,
                  bletiny_stack, BLETINY_STACK_SIZE);
 
-    rc = stats_module_init();
-    assert(rc == 0);
-
-    /* Initialize the BLE LL */
-    rc = ble_ll_init(BLE_LL_TASK_PRI, MBUF_NUM_MBUFS, BLE_MBUF_PAYLOAD_SIZE);
-    assert(rc == 0);
-
-    /* Initialize the RAM HCI transport. */
-    hci_cfg = ble_hci_ram_cfg_dflt;
-    rc = ble_hci_ram_init(&hci_cfg);
-    assert(rc == 0);
-
     /* Initialize the NimBLE host configuration. */
-    cfg = ble_hs_cfg_dflt;
-    cfg.max_gattc_procs = 2;
-    cfg.reset_cb = bletiny_on_reset;
-    cfg.store_read_cb = ble_store_ram_read;
-    cfg.store_write_cb = ble_store_ram_write;
-    cfg.gatts_register_cb = gatt_svr_register_cb;
-
-    /* Initialize GATT services. */
-    rc = ble_svc_gap_init(&cfg);
-    assert(rc == 0);
-
-    rc = ble_svc_gatt_init(&cfg);
-    assert(rc == 0);
-
-    rc = gatt_svr_init(&cfg);
-    assert(rc == 0);
+    ble_hs_cfg.parent_evq = &bletiny_evq;
+    ble_hs_cfg.reset_cb = bletiny_on_reset;
+    ble_hs_cfg.store_read_cb = ble_store_ram_read;
+    ble_hs_cfg.store_write_cb = ble_store_ram_write;
+    ble_hs_cfg.gatts_register_cb = gatt_svr_register_cb;
 
-    /* Initialize NimBLE host. */
-    rc = ble_hs_init(&bletiny_evq, &cfg);
+    rc = gatt_svr_init();
     assert(rc == 0);
 
     rc = cmd_init();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bleuart/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bleuart/pkg.yml b/apps/bleuart/pkg.yml
index c93011b..3f51860 100644
--- a/apps/bleuart/pkg.yml
+++ b/apps/bleuart/pkg.yml
@@ -36,7 +36,7 @@ pkg.deps:
     - libs/newtmgr/transport/ble
     - libs/bleuart
 
-pkg.cflags:
+pkg.syscfg_vals:
     # Disable unused roles; bleuart is a peripheral-only app.
-    - "-DNIMBLE_OPT_ROLE_OBSERVER=0"
-    - "-DNIMBLE_OPT_ROLE_CENTRAL=0"
+    BLE_ROLE_OBSERVER: 0
+    BLE_ROLE_CENTRAL: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/bleuart/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleuart/src/main.c b/apps/bleuart/src/main.c
index a530195..94c4fcd 100755
--- a/apps/bleuart/src/main.c
+++ b/apps/bleuart/src/main.c
@@ -55,28 +55,10 @@
 #include "nmgrble/newtmgr_ble.h"
 #include "bleuart/bleuart.h"
 
-/** Mbuf settings. */
-#define MBUF_NUM_MBUFS      (12)
-#define MBUF_BUF_SIZE       OS_ALIGN(BLE_MBUF_PAYLOAD_SIZE, 4)
-#define MBUF_MEMBLOCK_SIZE  (MBUF_BUF_SIZE + BLE_MBUF_MEMBLOCK_OVERHEAD)
-#define MBUF_MEMPOOL_SIZE   OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
-
-#define MAX_CONSOLE_INPUT 120
-static os_membuf_t bleuart_mbuf_mpool_data[MBUF_MEMPOOL_SIZE];
-struct os_mbuf_pool bleuart_mbuf_pool;
-struct os_mempool bleuart_mbuf_mpool;
-
-/** Priority of the nimble host and controller tasks. */
-#define BLE_LL_TASK_PRI             (OS_TASK_PRI_HIGHEST)
-
 /** bleuart task settings. */
 #define bleuart_TASK_PRIO           1
 #define bleuart_STACK_SIZE          (OS_STACK_ALIGN(336))
 
-#define NEWTMGR_TASK_PRIO (4)
-#define NEWTMGR_TASK_STACK_SIZE (OS_STACK_ALIGN(512))
-os_stack_t newtmgr_stack[NEWTMGR_TASK_STACK_SIZE];
-
 struct os_eventq bleuart_evq;
 struct os_task bleuart_task;
 bssnz_t os_stack_t bleuart_stack[bleuart_STACK_SIZE];
@@ -253,100 +235,27 @@ bleuart_task_handler(void *unused)
 int
 main(void)
 {
-    struct ble_hs_cfg cfg;
-    uint32_t seed;
     int rc;
-    int i;
 
     /* Initialize OS */
     os_init();
 
-    /* Set cputime to count at 1 usec increments */
-    rc = cputime_init(1000000);
-    assert(rc == 0);
-
-    /* Seed random number generator with least significant bytes of device
-     * address.
-     */
-    seed = 0;
-    for (i = 0; i < 4; ++i) {
-        seed |= g_dev_addr[i];
-        seed <<= 8;
-    }
-    srand(seed);
-
-    /* Initialize msys mbufs. */
-    rc = os_mempool_init(&bleuart_mbuf_mpool, MBUF_NUM_MBUFS,
-                         MBUF_MEMBLOCK_SIZE, bleuart_mbuf_mpool_data,
-                         "bleuart_mbuf_data");
-    assert(rc == 0);
-
-    rc = os_mbuf_pool_init(&bleuart_mbuf_pool, &bleuart_mbuf_mpool,
-                           MBUF_MEMBLOCK_SIZE, MBUF_NUM_MBUFS);
-    assert(rc == 0);
-
-    rc = os_msys_register(&bleuart_mbuf_pool);
-    assert(rc == 0);
-
     os_task_init(&bleuart_task, "bleuart", bleuart_task_handler,
                  NULL, bleuart_TASK_PRIO, OS_WAIT_FOREVER,
                  bleuart_stack, bleuart_STACK_SIZE);
 
-    /* Initialize the BLE LL */
-    rc = ble_ll_init(BLE_LL_TASK_PRI, MBUF_NUM_MBUFS, BLE_MBUF_PAYLOAD_SIZE);
-    assert(rc == 0);
-
-    /* Initialize the RAM HCI transport. */
-    rc = ble_hci_ram_init(&ble_hci_ram_cfg_dflt);
-    assert(rc == 0);
-
     /* Initialize the BLE host. */
-    cfg = ble_hs_cfg_dflt;
-    cfg.max_connections = 1;
-    cfg.max_gattc_procs = 2;
-    cfg.max_l2cap_chans = 3;
-    cfg.max_l2cap_sig_procs = 1;
-    cfg.sm_bonding = 1;
-    cfg.sm_our_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
-    cfg.sm_their_key_dist = BLE_SM_PAIR_KEY_DIST_ENC;
-    cfg.sync_cb = bleuart_on_sync;
-    cfg.store_read_cb = ble_store_ram_read;
-    cfg.store_write_cb = ble_store_ram_write;
-
-    /* Populate config with the required GATT server settings. */
-    cfg.max_attrs = 0;
-    cfg.max_services = 0;
-    cfg.max_client_configs = 0;
-
-    rc = ble_svc_gap_init(&cfg);
-    assert(rc == 0);
+    ble_hs_cfg.parent_evq = &bleuart_evq;
+    ble_hs_cfg.sync_cb = bleuart_on_sync;
+    ble_hs_cfg.store_read_cb = ble_store_ram_read;
+    ble_hs_cfg.store_write_cb = ble_store_ram_write;
 
-    rc = ble_svc_gatt_init(&cfg);
-    assert(rc == 0);
-
-    rc = bleuart_gatt_svr_init(&cfg);
+    rc = bleuart_gatt_svr_init();
     assert(rc == 0);
 
     /* Initialize eventq */
     os_eventq_init(&bleuart_evq);
 
-    /* Nmgr ble GATT server initialization */
-    rc = nmgr_ble_gatt_svr_init(&bleuart_evq, &cfg);
-    assert(rc == 0);
-
-
-    rc = ble_hs_init(&bleuart_evq, &cfg);
-    assert(rc == 0);
-
-    bleuart_init(MAX_CONSOLE_INPUT);
-
-    nmgr_task_init(NEWTMGR_TASK_PRIO, newtmgr_stack, NEWTMGR_TASK_STACK_SIZE);
-    imgmgr_module_init();
-
-    /* Register GATT attributes (services, characteristics, and
-     * descriptors).
-     */
-
     /* Set the default device name. */
     rc = ble_svc_gap_device_name_set("Mynewt_BLEuart");
     assert(rc == 0);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/blinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blinky/src/main.c b/apps/blinky/src/main.c
index 8d96fd0..e7736c9 100755
--- a/apps/blinky/src/main.c
+++ b/apps/blinky/src/main.c
@@ -29,15 +29,6 @@
 #ifdef ARCH_sim
 #include <mcu/mcu_sim.h>
 #endif
-#if 0
-#include "nrf.h"
-#include "nrf_drv_saadc.h"
-#include "app_error.h"
-#endif
-
-/* Init all tasks */
-volatile int tasks_initialized;
-int init_tasks(void);
 
 /* Task 1 */
 #define TASK1_PRIO (1)
@@ -52,11 +43,6 @@ static volatile int g_task1_loops;
 struct os_task task2;
 os_stack_t stack2[TASK2_STACK_SIZE];
 
-#define SHELL_TASK_PRIO (3)
-#define SHELL_MAX_INPUT_LEN     (256)
-#define SHELL_TASK_STACK_SIZE (OS_STACK_ALIGN(1024))
-os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
-
 struct log_handler log_console_handler;
 struct log my_log;
 
@@ -68,51 +54,6 @@ struct os_sem g_test_sem;
 /* For LED toggling */
 int g_led_pin;
 
-#define DEFAULT_MBUF_MPOOL_BUF_LEN (256)
-#define DEFAULT_MBUF_MPOOL_NBUFS (5)
-
-uint8_t default_mbuf_mpool_data[DEFAULT_MBUF_MPOOL_BUF_LEN *
-    DEFAULT_MBUF_MPOOL_NBUFS];
-
-struct os_mbuf_pool default_mbuf_pool;
-struct os_mempool default_mbuf_mpool;
-
-#if 0
-#define SAADC_SAMPLES_IN_BUFFER (4)
-static nrf_saadc_value_t       m_buffer_pool[2][SAADC_SAMPLES_IN_BUFFER];
-
-int event_finished;
-int total_events;
-
-static void
-saadc_cb(const nrf_drv_saadc_evt_t *event)
-{
-    if (event->type == NRF_DRV_SAADC_EVT_DONE) {
-        ++event_finished;
-    }
-    ++total_events;
-}
-
-void
-saadc_test(void)
-{
-    ret_code_t rc;
-    nrf_saadc_channel_config_t cc =
-        NRF_DRV_SAADC_DEFAULT_CHANNEL_CONFIG_SE(NRF_SAADC_INPUT_AIN0);
-
-    rc = nrf_drv_saadc_init(NULL, saadc_cb);
-    APP_ERROR_CHECK(rc);
-    rc = nrf_drv_saadc_channel_init(0, &cc);
-    APP_ERROR_CHECK(rc);
-
-    rc = nrf_drv_saadc_buffer_convert(m_buffer_pool[0], SAADC_SAMPLES_IN_BUFFER);
-    APP_ERROR_CHECK(rc);
-
-    rc = nrf_drv_saadc_buffer_convert(m_buffer_pool[1], SAADC_SAMPLES_IN_BUFFER);
-    APP_ERROR_CHECK(rc);
-}
-#endif
-
 void
 task1_handler(void *arg)
 {
@@ -136,12 +77,7 @@ task1_handler(void *arg)
 
         /* Release semaphore to task 2 */
         os_sem_release(&g_test_sem);
-
-#if 0
-        nrf_drv_saadc_sample();
-#endif
     }
-
 }
 
 void
@@ -163,30 +99,6 @@ task2_handler(void *arg)
 }
 
 /**
- * init_tasks
- *
- * Called by main.c after os_init(). This function performs initializations
- * that are required before tasks are running.
- *
- * @return int 0 success; error otherwise.
- */
-int
-init_tasks(void)
-{
-    /* Initialize global test semaphore */
-    os_sem_init(&g_test_sem, 0);
-
-    os_task_init(&task1, "task1", task1_handler, NULL,
-            TASK1_PRIO, OS_WAIT_FOREVER, stack1, TASK1_STACK_SIZE);
-
-    os_task_init(&task2, "task2", task2_handler, NULL,
-            TASK2_PRIO, OS_WAIT_FOREVER, stack2, TASK2_STACK_SIZE);
-
-    tasks_initialized = 1;
-    return 0;
-}
-
-/**
  * main
  *
  * The main function for the project. This function initializes the os, calls
@@ -204,36 +116,24 @@ main(int argc, char **argv)
     mcu_sim_parse_args(argc, argv);
 #endif
 
-    conf_init();
-
     os_init();
 
-    rc = os_mempool_init(&default_mbuf_mpool, DEFAULT_MBUF_MPOOL_NBUFS,
-            DEFAULT_MBUF_MPOOL_BUF_LEN, default_mbuf_mpool_data,
-            "default_mbuf_data");
+    /* Initialize global test semaphore */
+    rc = os_sem_init(&g_test_sem, 0);
     assert(rc == 0);
 
-    rc = os_mbuf_pool_init(&default_mbuf_pool, &default_mbuf_mpool,
-            DEFAULT_MBUF_MPOOL_BUF_LEN, DEFAULT_MBUF_MPOOL_NBUFS);
+    rc = os_task_init(&task1, "task1", task1_handler, NULL,
+            TASK1_PRIO, OS_WAIT_FOREVER, stack1, TASK1_STACK_SIZE);
     assert(rc == 0);
 
-    rc = os_msys_register(&default_mbuf_pool);
+    rc = os_task_init(&task2, "task2", task2_handler, NULL,
+            TASK2_PRIO, OS_WAIT_FOREVER, stack2, TASK2_STACK_SIZE);
     assert(rc == 0);
 
-    shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
-                    SHELL_MAX_INPUT_LEN);
-
-    stats_module_init();
-
-#if 0
-    saadc_test();
-#endif
-    rc = init_tasks();
     os_start();
 
     /* os start should never return. If it does, this should be an error */
     assert(0);
 
-    return rc;
+    return 0;
 }
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/boot/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/boot/pkg.yml b/apps/boot/pkg.yml
index 10ec482..31e2018 100644
--- a/apps/boot/pkg.yml
+++ b/apps/boot/pkg.yml
@@ -25,29 +25,37 @@ pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
     - loader
 
-pkg.features: bootloader
-
 pkg.deps:
     - sys/config
-    - fs/nffs
     - libs/bootutil
     - libs/mbedtls
     - libs/os
     - libs/util
     - libs/console/stub
 
-pkg.cflags: -DLOG_LEVEL=255
-pkg.cflags.NFFS:
-    - -DNFFS_PRESENT
-pkg.cflags.FCB:
-    - -DFCB_PRESENT
+pkg.deps.BOOT_NFFS:
+    - fs/nffs
+
+pkg.deps.BOOT_FCB:
+    - sys/fcb
 
-#
-# Define BOOT_SERIAL in target features to include serial downloader.
-# And uncomment 'libs/console/stub' from pkg.deps.
-#
 pkg.deps.BOOT_SERIAL.OVERWRITE:
     - libs/console/full
     - libs/boot_serial
-pkg.cflags.BOOT_SERIAL:
-    - -DBOOT_SERIAL
+
+pkg.syscfg_vals:
+    LOG_LEVEL: 255
+
+pkg.syscfg_defs:
+    BOOT_LOADER:
+        description: 'TBD'
+        value: 1
+    BOOT_NFFS:
+        description: 'TBD'
+        value: 1
+    BOOT_FCB:
+        description: 'TBD'
+        value: 0
+    BOOT_SERIAL:
+        description: 'TBD'
+        value: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/boot/src/boot.c
----------------------------------------------------------------------
diff --git a/apps/boot/src/boot.c b/apps/boot/src/boot.c
index 97a9b32..1fb70a4 100755
--- a/apps/boot/src/boot.c
+++ b/apps/boot/src/boot.c
@@ -27,16 +27,16 @@
 #include <hal/hal_flash.h>
 #include <config/config.h>
 #include <config/config_file.h>
-#ifdef NFFS_PRESENT
+#if MYNEWT_VAL(BOOT_NFFS)
 #include <fs/fs.h>
 #include <nffs/nffs.h>
-#elif FCB_PRESENT
+#elif MYNEWT_VAL(BOOT_FCB)
 #include <fcb/fcb.h>
 #include <config/config_fcb.h>
 #else
 #error "Need NFFS or FCB for config storage"
 #endif
-#ifdef BOOT_SERIAL
+#ifdef MYNEWT_VAL(BOOT_SERIAL)
 #include <hal/hal_gpio.h>
 #include <boot_serial/boot_serial.h>
 #endif
@@ -58,7 +58,7 @@ static struct os_task boot_ser_task;
 static os_stack_t boot_ser_stack[BOOT_SER_STACK_SZ];
 #endif
 
-#ifdef NFFS_PRESENT
+#if MYNEWT_VAL(BOOT_NFFS)
 #define MY_CONFIG_FILE "/cfg/run"
 
 static struct conf_file my_conf = {
@@ -78,7 +78,7 @@ setup_for_nffs(void)
      * be readable.
      */
     cnt = NFFS_AREA_MAX;
-    rc = flash_area_to_nffs_desc(FLASH_AREA_NFFS, &cnt, nffs_descs);
+    rc = nffs_misc_desc_from_flash_area(FLASH_AREA_NFFS, &cnt, nffs_descs);
     assert(rc == 0);
 
     /*
@@ -167,14 +167,14 @@ main(void)
 
     conf_init();
 
-#ifdef NFFS_PRESENT
+#if MYNEWT_VAL(BOOT_NFFS)
     setup_for_nffs();
-#elif FCB_PRESENT
+#elif MYNEWT_VAL(BOOT_FCB)
     setup_for_fcb();
 #endif
     bootutil_cfg_register();
 
-#ifdef BOOT_SERIAL
+#if MYNEWT_VAL(BOOT_SERIAL)
     /*
      * Configure a GPIO as input, and compare it against expected value.
      * If it matches, await for download commands from serial.

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/ffs2native/src/main.c
----------------------------------------------------------------------
diff --git a/apps/ffs2native/src/main.c b/apps/ffs2native/src/main.c
index 98493b0..90bb3fb 100644
--- a/apps/ffs2native/src/main.c
+++ b/apps/ffs2native/src/main.c
@@ -666,18 +666,7 @@ main(int argc, char **argv)
     }
 
     os_init();
-    if (standalone == 0) {
-        rc = flash_area_to_nffs_desc(FLASH_AREA_NFFS, &cnt, area_descs);
-        assert(rc == 0);
-    }
-
-    rc = hal_flash_init();
-    assert(rc == 0);
 
-    rc = nffs_init();
-    assert(rc == 0);
-
-    log_init();
     log_console_handler_init(&nffs_log_console_handler);
     log_register("nffs-log", &nffs_log, &nffs_log_console_handler);
 
@@ -698,6 +687,9 @@ main(int argc, char **argv)
         return 0;
     }
 
+    rc = nffs_misc_desc_from_flash_area(FLASH_AREA_NFFS, &cnt, area_descs);
+    assert(rc == 0);
+
     if (copy_in_dir) {
         /*
          * Build filesystem from contents of directory

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/luatest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/luatest/src/main.c b/apps/luatest/src/main.c
index 9cd3d5a..f71cbe3 100755
--- a/apps/luatest/src/main.c
+++ b/apps/luatest/src/main.c
@@ -19,28 +19,14 @@
 
 #include <assert.h>
 #include <string.h>
-#include <os/os.h>
-#include <bsp/bsp.h>
-#include <hal/hal_flash.h>
-#include <console/console.h>
-#include <shell/shell.h>
-#include <elua_base/elua.h>
-#include <fs/fs.h>
-#include <nffs/nffs.h>
-#include <hal/flash_map.h>
+#include "os/os.h"
+#include "bsp/bsp.h"
+#include "elua_base/elua.h"
+#include "fs/fs.h"
 #ifdef ARCH_sim
-#include <mcu/mcu_sim.h>
+#include "mcu/mcu_sim.h"
 #endif
 
-/* Init all tasks */
-int init_tasks(void);
-
-/* Shell */
-#define SHELL_TASK_PRIO      (8)
-#define SHELL_MAX_INPUT_LEN     (256)
-#define SHELL_TASK_STACK_SIZE (OS_STACK_ALIGN(2048))
-static os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
-
 static void
 create_script_file(void)
 {
@@ -62,45 +48,21 @@ create_script_file(void)
 /**
  * main
  *
- * The main function for the project. This function initializes the os, calls
- * init_tasks to initialize tasks (and possibly other objects), then starts the
- * OS. We should not return from os start.
+ * The main function for the project. This function initializes and starts the
+ * OS.  We should not return from os start.
  *
  * @return int NOTE: this function should never return!
  */
 int
 main(int argc, char **argv)
 {
-    int rc;
-    struct nffs_area_desc descs[NFFS_AREA_MAX];
-    int cnt;
-
 #ifdef ARCH_sim
     mcu_sim_parse_args(argc, argv);
 #endif
 
-    rc = hal_flash_init();
-    assert(rc == 0);
-
     /* Initialize OS */
     os_init();
 
-    /* Init tasks */
-    shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
-                         SHELL_MAX_INPUT_LEN);
-
-    nffs_init();
-
-    cnt = NFFS_AREA_MAX;
-    rc = flash_area_to_nffs_desc(FLASH_AREA_NFFS, &cnt, descs);
-    assert(rc == 0);
-
-    if (nffs_detect(descs) == FS_ECORRUPT) {
-        rc = nffs_format(descs);
-        assert(rc == 0);
-    }
-
-    lua_init();
     create_script_file();
 
     /* Start the OS */
@@ -109,6 +71,5 @@ main(int argc, char **argv)
     /* os start should never return. If it does, this should be an error */
     assert(0);
 
-    return rc;
+    return 0;
 }
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/sblinky/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/sblinky/pkg.yml b/apps/sblinky/pkg.yml
index c45b5b4..45dfbac 100644
--- a/apps/sblinky/pkg.yml
+++ b/apps/sblinky/pkg.yml
@@ -27,18 +27,6 @@ pkg.keywords:
 pkg.req_apis:
     - ADC_HW_IMPL
 
-pkg.feature_blacklist:
-    ".*": SHELL
-
-pkg.feature_whitelist:
-    ".*newtmgr.*": SHELL
-
-pkg.deps.ADC_NRF51:
-    - drivers/adc/adc_nrf51
-
-pkg.deps.ADC_NRF52:
-    - drivers/adc/adc_nrf52
-
 pkg.deps:
     - libs/newtmgr
     - hw/mcu/nordic

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/sblinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/sblinky/src/main.c b/apps/sblinky/src/main.c
index aff72eb..f9799be 100755
--- a/apps/sblinky/src/main.c
+++ b/apps/sblinky/src/main.c
@@ -82,11 +82,6 @@ static volatile int g_task1_loops;
 struct os_task task2;
 os_stack_t stack2[TASK2_STACK_SIZE];
 
-#define SHELL_TASK_PRIO (3)
-#define SHELL_MAX_INPUT_LEN     (256)
-#define SHELL_TASK_STACK_SIZE (OS_STACK_ALIGN(1024))
-os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
-
 struct log_handler log_console_handler;
 struct log my_log;
 
@@ -98,16 +93,7 @@ struct os_sem g_test_sem;
 /* For LED toggling */
 int g_led_pin;
 
-#define DEFAULT_MBUF_MPOOL_BUF_LEN (256)
-#define DEFAULT_MBUF_MPOOL_NBUFS (5)
-
-uint8_t default_mbuf_mpool_data[DEFAULT_MBUF_MPOOL_BUF_LEN *
-    DEFAULT_MBUF_MPOOL_NBUFS];
-
-struct os_mbuf_pool default_mbuf_pool;
-struct os_mempool default_mbuf_mpool;
-
-#ifdef BSP_CFG_SPI_MASTER
+#if MYNEWT_VAL(SPI_MASTER)
 uint8_t g_spi_tx_buf[32];
 uint8_t g_spi_rx_buf[32];
 
@@ -145,7 +131,7 @@ sblinky_spi_tx_vals(int spi_num, uint8_t *buf, int len)
 #endif
 #endif
 
-#ifdef BSP_CFG_SPI_SLAVE
+#if MYNEWT_VAL(SPI_SLAVE)
 uint8_t g_spi_tx_buf[32];
 uint8_t g_spi_rx_buf[32];
 
@@ -179,7 +165,7 @@ sblinky_spi_cfg(int spi_num)
     int spi_id;
     struct hal_spi_settings my_spi;
 
-#ifdef BSP_CFG_SPI_MASTER
+#if MYNEWT_VAL(SPI_MASTER)
     my_spi.spi_type = HAL_SPI_TYPE_MASTER;
     my_spi.data_order = HAL_SPI_MSB_FIRST;
     my_spi.data_mode = HAL_SPI_MODE0;
@@ -190,7 +176,7 @@ sblinky_spi_cfg(int spi_num)
     spi_id = 0;
 #endif
 
-#ifdef BSP_CFG_SPI_SLAVE
+#if MYNEWT_VAL(SPI_SLAVE)
     my_spi.spi_type = HAL_SPI_TYPE_SLAVE;
     my_spi.data_order = HAL_SPI_MSB_FIRST;
     my_spi.data_mode = HAL_SPI_MODE0;
@@ -279,7 +265,7 @@ task1_handler(void *arg)
     int rc;
     struct os_task *t;
     struct adc_dev *adc;
-#ifdef BSP_CFG_SPI_MASTER
+#if MYNEWT_VAL(SPI_MASTER)
     int i;
     uint8_t last_val;
 #endif
@@ -305,7 +291,7 @@ task1_handler(void *arg)
     adc_chan_config(adc, 0, &cc);
 #endif
 
-#ifdef BSP_CFG_SPI_MASTER
+#if MYNEWT_VAL(SPI_MASTER)
     /* Use SS pin for testing */
     hal_gpio_init_out(SPI0_CONFIG_CSN_PIN, 1);
     sblinky_spi_cfg(0);
@@ -349,7 +335,7 @@ task1_handler(void *arg)
     hal_spi_enable(0);
 #endif
 
-#ifdef BSP_CFG_SPI_SLAVE
+#if MYNEWT_VAL(SPI_SLAVE)
     sblinky_spi_cfg(SPI_SLAVE_ID);
     hal_spi_enable(SPI_SLAVE_ID);
 
@@ -387,7 +373,7 @@ task1_handler(void *arg)
 
         ++g_task1_loops;
 
-#ifdef BSP_CFG_SPI_MASTER
+#if MYNEWT_VAL(SPI_MASTER)
         /*
          * Send a spi buffer using non-blocking callbacks.
          * Every other transfer should use a NULL rxbuf
@@ -410,6 +396,8 @@ task1_handler(void *arg)
 #if 0
         nrf_drv_saadc_sample();
 #endif
+        /* Release semaphore to task 2 */
+        os_sem_release(&g_test_sem);
     }
 
     os_dev_close((struct os_dev *) adc);
@@ -418,7 +406,7 @@ task1_handler(void *arg)
 void
 task2_handler(void *arg)
 {
-#ifdef BSP_CFG_SPI_SLAVE
+#if MYNEWT_VAL(SPI_SLAVE)
     int rc;
 #endif
     struct os_task *t;
@@ -434,7 +422,7 @@ task2_handler(void *arg)
         /* Wait for semaphore from ISR */
         os_sem_pend(&g_test_sem, OS_TIMEOUT_NEVER);
 
-#ifdef BSP_CFG_SPI_SLAVE
+#if MYNEWT_VAL(SPI_SLAVE)
         /* transmit back what we just received */
         memcpy(g_spi_tx_buf, g_spi_rx_buf, 32);
         rc = hal_spi_txrx(SPI_SLAVE_ID, g_spi_tx_buf, g_spi_rx_buf, 32);
@@ -485,41 +473,11 @@ main(int argc, char **argv)
     mcu_sim_parse_args(argc, argv);
 #endif
 
-    conf_init();
-
     os_init();
 
-    rc = os_mempool_init(&default_mbuf_mpool, DEFAULT_MBUF_MPOOL_NBUFS,
-            DEFAULT_MBUF_MPOOL_BUF_LEN, default_mbuf_mpool_data,
-            "default_mbuf_data");
-    assert(rc == 0);
-
-    rc = os_mbuf_pool_init(&default_mbuf_pool, &default_mbuf_mpool,
-            DEFAULT_MBUF_MPOOL_BUF_LEN, DEFAULT_MBUF_MPOOL_NBUFS);
-    assert(rc == 0);
-
-    rc = os_msys_register(&default_mbuf_pool);
-    assert(rc == 0);
-
-    shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
-                    SHELL_MAX_INPUT_LEN);
-
-    (void) console_init(shell_console_rx_cb);
-
-    stats_module_init();
-
-#ifdef NRF52
-    rc = os_dev_create((struct os_dev *) &my_dev, "adc0",
-            OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
-            nrf52_adc_dev_init, &adc_config);
-    assert(rc == 0);
-#endif
-#ifdef NRF51
-    rc = os_dev_create((struct os_dev *) &my_dev, "adc0",
-            OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
-            nrf51_adc_dev_init, &adc_config);
-    assert(rc == 0);
-#endif
+    /* Initialize the sblinky log. */
+    log_console_handler_init(&log_console_handler);
+    log_register("sblinky", &my_log, &log_console_handler);
 
 #if 0
     saadc_test();

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/slinky/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/slinky/pkg.yml b/apps/slinky/pkg.yml
index 3219caa..381305f 100644
--- a/apps/slinky/pkg.yml
+++ b/apps/slinky/pkg.yml
@@ -38,11 +38,5 @@ pkg.deps:
     - sys/log
     - sys/stats
 
-pkg.cflags:
-    - "-DSTATS_NAME_ENABLE=1"
-
-pkg.cflags.NFFS:
-    - "-DNFFS_PRESENT"
-
-pkg.cflags.FCB:
-    - "-DFCB_PRESENT"
+pkg.syscfg_vals:
+    STATS_NAMES: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/slinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/slinky/src/main.c b/apps/slinky/src/main.c
index 7f724e6..257a43e 100755
--- a/apps/slinky/src/main.c
+++ b/apps/slinky/src/main.c
@@ -16,6 +16,8 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
+#include "syscfg/syscfg.h"
 #include <os/os.h>
 #include <bsp/bsp.h>
 #include <hal/hal_gpio.h>
@@ -27,11 +29,11 @@
 #include <config/config.h>
 #include <hal/flash_map.h>
 #include <hal/hal_system.h>
-#ifdef NFFS_PRESENT
+#if MYNEWT_PKG_FS_FS
 #include <fs/fs.h>
 #include <nffs/nffs.h>
 #include <config/config_file.h>
-#elif FCB_PRESENT
+#elif MYNEWT_PKG_SYS_FCB
 #include <fcb/fcb.h>
 #include <config/config_fcb.h>
 #else
@@ -71,15 +73,6 @@ static volatile int g_task1_loops;
 struct os_task task2;
 os_stack_t stack2[TASK2_STACK_SIZE];
 
-#define SHELL_TASK_PRIO (3)
-#define SHELL_MAX_INPUT_LEN     (256)
-#define SHELL_TASK_STACK_SIZE (OS_STACK_ALIGN(384))
-os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
-
-#define NEWTMGR_TASK_PRIO (4)
-#define NEWTMGR_TASK_STACK_SIZE (OS_STACK_ALIGN(896))
-os_stack_t newtmgr_stack[NEWTMGR_TASK_STACK_SIZE];
-
 struct log_handler log_cbmem_handler;
 struct log my_log;
 
@@ -101,34 +94,6 @@ STATS_NAME_START(gpio_stats)
 STATS_NAME(gpio_stats, toggles)
 STATS_NAME_END(gpio_stats)
 
-#ifdef NFFS_PRESENT
-/* configuration file */
-#define MY_CONFIG_DIR  "/cfg"
-#define MY_CONFIG_FILE "/cfg/run"
-#define MY_CONFIG_MAX_LINES  32
-
-static struct conf_file my_conf = {
-    .cf_name = MY_CONFIG_FILE,
-    .cf_maxlines = MY_CONFIG_MAX_LINES
-};
-#elif FCB_PRESENT
-struct flash_area conf_fcb_area[NFFS_AREA_MAX + 1];
-
-static struct conf_fcb my_conf = {
-    .cf_fcb.f_magic = 0xc09f6e5e,
-    .cf_fcb.f_sectors = conf_fcb_area
-};
-#endif
-
-#define DEFAULT_MBUF_MPOOL_BUF_LEN (256)
-#define DEFAULT_MBUF_MPOOL_NBUFS (10)
-
-uint8_t default_mbuf_mpool_data[DEFAULT_MBUF_MPOOL_BUF_LEN *
-    DEFAULT_MBUF_MPOOL_NBUFS];
-
-struct os_mbuf_pool default_mbuf_pool;
-struct os_mempool default_mbuf_mpool;
-
 static char *test_conf_get(int argc, char **argv, char *val, int max_len);
 static int test_conf_set(int argc, char **argv, char *val);
 static int test_conf_commit(void);
@@ -276,41 +241,7 @@ init_tasks(void)
     return 0;
 }
 
-#ifdef NFFS_PRESENT
-static void
-setup_for_nffs(void)
-{
-    /* NFFS_AREA_MAX is defined in the BSP-specified bsp.h header file. */
-    struct nffs_area_desc descs[NFFS_AREA_MAX + 1];
-    int cnt;
-    int rc;
-
-    /* Initialize nffs's internal state. */
-    rc = nffs_init();
-    assert(rc == 0);
-
-    /* Convert the set of flash blocks we intend to use for nffs into an array
-     * of nffs area descriptors.
-     */
-    cnt = NFFS_AREA_MAX;
-    rc = flash_area_to_nffs_desc(FLASH_AREA_NFFS, &cnt, descs);
-    assert(rc == 0);
-
-    /* Attempt to restore an existing nffs file system from flash. */
-    if (nffs_detect(descs) == FS_ECORRUPT) {
-        /* No valid nffs instance detected; format a new one. */
-        rc = nffs_format(descs);
-        assert(rc == 0);
-    }
-
-    fs_mkdir(MY_CONFIG_DIR);
-    rc = conf_file_src(&my_conf);
-    assert(rc == 0);
-    rc = conf_file_dst(&my_conf);
-    assert(rc == 0);
-}
-
-#elif FCB_PRESENT
+#if !MYNEWT_VAL(CONFIG_NFFS)
 
 static void
 setup_for_fcb(void)
@@ -358,49 +289,19 @@ main(int argc, char **argv)
     mcu_sim_parse_args(argc, argv);
 #endif
 
-    conf_init();
+    os_init();
+
     rc = conf_register(&test_conf_handler);
     assert(rc == 0);
 
-    log_init();
     cbmem_init(&cbmem, cbmem_buf, MAX_CBMEM_BUF);
     log_cbmem_handler_init(&log_cbmem_handler, &cbmem);
     log_register("log", &my_log, &log_cbmem_handler);
 
-    os_init();
-
-    rc = os_mempool_init(&default_mbuf_mpool, DEFAULT_MBUF_MPOOL_NBUFS,
-            DEFAULT_MBUF_MPOOL_BUF_LEN, default_mbuf_mpool_data,
-            "default_mbuf_data");
-    assert(rc == 0);
-
-    rc = os_mbuf_pool_init(&default_mbuf_pool, &default_mbuf_mpool,
-            DEFAULT_MBUF_MPOOL_BUF_LEN, DEFAULT_MBUF_MPOOL_NBUFS);
-    assert(rc == 0);
-
-    rc = os_msys_register(&default_mbuf_pool);
-    assert(rc == 0);
-
-    rc = hal_flash_init();
-    assert(rc == 0);
-
-#ifdef NFFS_PRESENT
-    setup_for_nffs();
-#elif FCB_PRESENT
+#if !MYNEWT_VAL(CONFIG_NFFS)
     setup_for_fcb();
 #endif
 
-    id_init();
-
-    shell_task_init(SHELL_TASK_PRIO, shell_stack, SHELL_TASK_STACK_SIZE,
-                    SHELL_MAX_INPUT_LEN);
-
-    nmgr_task_init(NEWTMGR_TASK_PRIO, newtmgr_stack, NEWTMGR_TASK_STACK_SIZE);
-    imgmgr_module_init();
-    bootutil_cfg_register();
-
-    stats_module_init();
-
     stats_init(STATS_HDR(g_stats_gpio_toggle),
                STATS_SIZE_INIT_PARMS(g_stats_gpio_toggle, STATS_SIZE_32),
                STATS_NAME_INIT_PARMS(gpio_stats));
@@ -409,8 +310,6 @@ main(int argc, char **argv)
 
     flash_test_init();
 
-    reboot_init_handler(LOG_TYPE_STORAGE, 10);
-
     conf_load();
 
     log_reboot(HARD_REBOOT);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/apps/test/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/test/pkg.yml b/apps/test/pkg.yml
index 7e05e28..5dc353a 100644
--- a/apps/test/pkg.yml
+++ b/apps/test/pkg.yml
@@ -24,8 +24,6 @@ pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
 
-pkg.features:
-    - TEST
 pkg.deps:
     - fs/nffs
     - libs/bootutil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/drivers/adc/adc_nrf51/pkg.yml
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_nrf51/pkg.yml b/drivers/adc/adc_nrf51/pkg.yml
index 93ee635..2f604ed 100644
--- a/drivers/adc/adc_nrf51/pkg.yml
+++ b/drivers/adc/adc_nrf51/pkg.yml
@@ -22,12 +22,7 @@ pkg.description: ADC driver for the NRF51
 pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
-pkg.features:
-    - ADC_NRF51
 pkg.apis:
     - ADC_HW_IMPL
-pkg.deps.TEST:
-   - hw/hal
-   - hw/mcu/nordic
+pkg.deps:
    - drivers/adc
-   - libs/testutil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/drivers/adc/adc_nrf52/pkg.yml
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_nrf52/pkg.yml b/drivers/adc/adc_nrf52/pkg.yml
index bf978ab..fa2eb70 100644
--- a/drivers/adc/adc_nrf52/pkg.yml
+++ b/drivers/adc/adc_nrf52/pkg.yml
@@ -22,12 +22,7 @@ pkg.description: ADC driver for the NRF52
 pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
-pkg.features: 
-    - ADC_NRF52
 pkg.apis: 
     - ADC_HW_IMPL
-pkg.deps.TEST:
-   - hw/hal
-   - hw/mcu/nordic
+pkg.deps:
    - drivers/adc
-   - libs/testutil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/drivers/adc/pkg.yml
----------------------------------------------------------------------
diff --git a/drivers/adc/pkg.yml b/drivers/adc/pkg.yml
index 8b4a968..94469bc 100644
--- a/drivers/adc/pkg.yml
+++ b/drivers/adc/pkg.yml
@@ -24,7 +24,3 @@ pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
 pkg.req_apis: 
     - ADC_HW_IMPL
-
-pkg.deps.TEST:
-   - hw/hal
-   - libs/testutil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/drivers/uart/pkg.yml
----------------------------------------------------------------------
diff --git a/drivers/uart/pkg.yml b/drivers/uart/pkg.yml
index ce6eb6a..5b95e32 100644
--- a/drivers/uart/pkg.yml
+++ b/drivers/uart/pkg.yml
@@ -25,4 +25,3 @@ pkg.keywords:
 pkg.deps:
 pkg.req_apis: 
     - UART_HW_IMPL
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/fs/pkg.yml
----------------------------------------------------------------------
diff --git a/fs/fs/pkg.yml b/fs/fs/pkg.yml
index 64909cb..f965d43 100644
--- a/fs/fs/pkg.yml
+++ b/fs/fs/pkg.yml
@@ -26,10 +26,10 @@ pkg.keywords:
     - filesystem
     - ffs
 
-pkg.deps.SHELL:
+pkg.deps.FS_CLI:
     - libs/shell
-pkg.req_apis.SHELL:
-    - console
-pkg.cflags.SHELL: -DSHELL_PRESENT
-pkg.features:
-    - FS
+
+pkg.syscfg_defs:
+    FS_CLI:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_LIBS_SHELL'



[23/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_gap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gap_test.c b/net/nimble/host/src/test/ble_gap_test.c
deleted file mode 100644
index 1943d01..0000000
--- a/net/nimble/host/src/test/ble_gap_test.c
+++ /dev/null
@@ -1,2578 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "nimble/hci_common.h"
-#include "host/ble_hs_adv.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-
-static int ble_gap_test_conn_event_type;
-static int ble_gap_test_conn_status;
-static struct ble_gap_conn_desc ble_gap_test_conn_desc;
-static void *ble_gap_test_conn_arg;
-static struct ble_gap_upd_params ble_gap_test_conn_peer_params;
-static struct ble_gap_upd_params ble_gap_test_conn_self_params;
-
-static int ble_gap_test_disc_event_type;
-static struct ble_gap_disc_desc ble_gap_test_disc_desc;
-static void *ble_gap_test_disc_arg;
-
-/*****************************************************************************
- * $misc                                                                     *
- *****************************************************************************/
-
-static int
-ble_gap_test_util_update_in_progress(uint16_t conn_handle)
-{
-    ble_hs_conn_flags_t conn_flags;
-    int rc;
-
-    rc = ble_hs_atomic_conn_flags(conn_handle, &conn_flags);
-    return rc == 0 && conn_flags & BLE_HS_CONN_F_UPDATE;
-}
-
-static void
-ble_gap_test_util_reset_cb_info(void)
-{
-    ble_gap_test_conn_event_type = -1;
-    ble_gap_test_conn_status = -1;
-    memset(&ble_gap_test_conn_desc, 0xff, sizeof ble_gap_test_conn_desc);
-    ble_gap_test_conn_arg = (void *)-1;
-
-    ble_gap_test_disc_event_type = -1;
-    memset(&ble_gap_test_disc_desc, 0xff, sizeof ble_gap_test_disc_desc);
-    ble_gap_test_disc_arg = (void *)-1;
-}
-
-static void
-ble_gap_test_util_init(void)
-{
-    ble_hs_test_util_init();
-    ble_hs_test_util_set_static_rnd_addr();
-    ble_gap_test_util_reset_cb_info();
-}
-
-static int
-ble_gap_test_util_disc_cb(struct ble_gap_event *event, void *arg)
-{
-    ble_gap_test_disc_event_type = event->type;
-    ble_gap_test_disc_arg = arg;
-
-    if (event->type == BLE_GAP_EVENT_DISC) {
-        ble_gap_test_disc_desc = event->disc;
-    }
-
-    return 0;
-}
-
-static int
-ble_gap_test_util_connect_cb(struct ble_gap_event *event, void *arg)
-{
-    int *fail_reason;
-
-    ble_gap_test_conn_event_type = event->type;
-    ble_gap_test_conn_arg = arg;
-
-    switch (event->type) {
-    case BLE_GAP_EVENT_CONNECT:
-        ble_gap_test_conn_status = event->connect.status;
-        ble_gap_conn_find(event->connect.conn_handle, &ble_gap_test_conn_desc);
-        break;
-
-    case BLE_GAP_EVENT_DISCONNECT:
-        ble_gap_test_conn_status = event->disconnect.reason;
-        ble_gap_test_conn_desc = event->disconnect.conn;
-        break;
-
-    case BLE_GAP_EVENT_CONN_UPDATE:
-        ble_gap_test_conn_status = event->conn_update.status;
-        ble_gap_conn_find(event->conn_update.conn_handle,
-                          &ble_gap_test_conn_desc);
-        break;
-
-    case BLE_GAP_EVENT_CONN_CANCEL:
-        break;
-
-    case BLE_GAP_EVENT_TERM_FAILURE:
-        ble_gap_test_conn_status = event->term_failure.status;
-        ble_gap_conn_find(event->term_failure.conn_handle,
-                          &ble_gap_test_conn_desc);
-        break;
-
-    case BLE_GAP_EVENT_ADV_COMPLETE:
-        ble_gap_test_conn_arg = arg;
-        break;
-
-    case BLE_GAP_EVENT_CONN_UPDATE_REQ:
-        ble_gap_test_conn_peer_params = *event->conn_update_req.peer_params;
-        *event->conn_update_req.self_params = ble_gap_test_conn_self_params;
-        ble_gap_conn_find(event->conn_update_req.conn_handle,
-                          &ble_gap_test_conn_desc);
-
-        fail_reason = arg;
-        if (fail_reason == NULL) {
-            return 0;
-        } else {
-            return *fail_reason;
-        }
-        break;
-
-    default:
-        TEST_ASSERT_FATAL(0);
-        break;
-    }
-
-    return 0;
-}
-
-static void
-ble_gap_test_util_verify_tx_clear_wl(void)
-{
-    uint8_t param_len;
-
-    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                   BLE_HCI_OCF_LE_CLEAR_WHITE_LIST,
-                                   &param_len);
-    TEST_ASSERT(param_len == 0);
-}
-
-static void
-ble_gap_test_util_verify_tx_add_wl(struct ble_gap_white_entry *entry)
-{
-    uint8_t param_len;
-    uint8_t *param;
-    int i;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_ADD_WHITE_LIST,
-                                           &param_len);
-    TEST_ASSERT(param_len == 7);
-    TEST_ASSERT(param[0] == entry->addr_type);
-    for (i = 0; i < 6; i++) {
-        TEST_ASSERT(param[1 + i] == entry->addr[i]);
-    }
-}
-
-static void
-ble_gap_test_util_verify_tx_set_scan_params(uint8_t own_addr_type,
-                                            uint8_t scan_type,
-                                            uint16_t itvl,
-                                            uint16_t scan_window,
-                                            uint8_t filter_policy)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_SET_SCAN_PARAMS,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_SET_SCAN_PARAM_LEN);
-    TEST_ASSERT(param[0] == scan_type);
-    TEST_ASSERT(le16toh(param + 1) == itvl);
-    TEST_ASSERT(le16toh(param + 3) == scan_window);
-    TEST_ASSERT(param[5] == own_addr_type);
-    TEST_ASSERT(param[6] == filter_policy);
-}
-
-static void
-ble_gap_test_util_verify_tx_scan_enable(uint8_t enable,
-                                        uint8_t filter_duplicates)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_SET_SCAN_ENABLE,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_SET_SCAN_ENABLE_LEN);
-    TEST_ASSERT(param[0] == enable);
-    TEST_ASSERT(param[1] == filter_duplicates);
-}
-
-static void
-ble_hs_test_util_verify_tx_create_conn_cancel(void)
-{
-    uint8_t param_len;
-
-    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                   BLE_HCI_OCF_LE_CREATE_CONN_CANCEL,
-                                   &param_len);
-    TEST_ASSERT(param_len == 0);
-}
-
-static void
-ble_gap_test_util_verify_tx_disconnect(void)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LINK_CTRL,
-                                           BLE_HCI_OCF_DISCONNECT_CMD,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_DISCONNECT_CMD_LEN);
-    TEST_ASSERT(le16toh(param + 0) == 2);
-    TEST_ASSERT(param[2] == BLE_ERR_REM_USER_CONN_TERM);
-}
-
-static void
-ble_gap_test_util_verify_tx_adv_params(void)
-{
-    uint8_t param_len;
-
-    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                   BLE_HCI_OCF_LE_SET_ADV_PARAMS,
-                                   &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_SET_ADV_PARAM_LEN);
-
-    /* Note: Content of message verified in ble_hs_adv_test.c. */
-}
-
-static void
-ble_gap_test_util_verify_tx_adv_data(void)
-{
-    uint8_t param_len;
-
-    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                   BLE_HCI_OCF_LE_SET_ADV_DATA,
-                                   &param_len);
-    /* Note: Content of message verified in ble_hs_adv_test.c. */
-}
-
-static void
-ble_gap_test_util_verify_tx_rsp_data(void)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA,
-                                           &param_len);
-    (void)param; /* XXX: Verify other fields. */
-}
-
-static void
-ble_gap_test_util_verify_tx_adv_enable(int enabled)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_SET_ADV_ENABLE,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_SET_ADV_ENABLE_LEN);
-    TEST_ASSERT(param[0] == !!enabled);
-}
-
-static void
-ble_gap_test_util_verify_tx_update_conn(struct ble_gap_upd_params *params)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_CONN_UPDATE,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_CONN_UPDATE_LEN);
-    TEST_ASSERT(le16toh(param + 0) == 2);
-    TEST_ASSERT(le16toh(param + 2) == params->itvl_min);
-    TEST_ASSERT(le16toh(param + 4) == params->itvl_max);
-    TEST_ASSERT(le16toh(param + 6) == params->latency);
-    TEST_ASSERT(le16toh(param + 8) == params->supervision_timeout);
-    TEST_ASSERT(le16toh(param + 10) == params->min_ce_len);
-    TEST_ASSERT(le16toh(param + 12) == params->max_ce_len);
-}
-
-static void
-ble_gap_test_util_verify_tx_params_reply_pos(void)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_REM_CONN_PARAM_RR,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_CONN_PARAM_REPLY_LEN);
-    TEST_ASSERT(le16toh(param + 0) == 2);
-    TEST_ASSERT(le16toh(param + 2) == ble_gap_test_conn_self_params.itvl_min);
-    TEST_ASSERT(le16toh(param + 4) == ble_gap_test_conn_self_params.itvl_max);
-    TEST_ASSERT(le16toh(param + 6) == ble_gap_test_conn_self_params.latency);
-    TEST_ASSERT(le16toh(param + 8) ==
-                ble_gap_test_conn_self_params.supervision_timeout);
-    TEST_ASSERT(le16toh(param + 10) ==
-                ble_gap_test_conn_self_params.min_ce_len);
-    TEST_ASSERT(le16toh(param + 12) ==
-                ble_gap_test_conn_self_params.max_ce_len);
-}
-
-static void
-ble_gap_test_util_verify_tx_params_reply_neg(uint8_t reason)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_REM_CONN_PARAM_NRR,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_CONN_PARAM_NEG_REPLY_LEN);
-    TEST_ASSERT(le16toh(param + 0) == 2);
-    TEST_ASSERT(param[2] == reason);
-}
-
-static void
-ble_gap_test_util_rx_update_complete(
-    uint8_t status,
-    struct ble_gap_upd_params *params)
-{
-    struct hci_le_conn_upd_complete evt;
-
-    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_UPD_COMPLETE;
-    evt.status = status;
-    evt.connection_handle = 2;
-    evt.conn_itvl = params->itvl_max;
-    evt.conn_latency = params->latency;
-    evt.supervision_timeout = params->supervision_timeout;
-
-    ble_gap_rx_update_complete(&evt);
-}
-
-static int
-ble_gap_test_util_rx_param_req(struct ble_gap_upd_params *params, int pos,
-                               int *cmd_idx, int cmd_fail_idx,
-                               uint8_t fail_status)
-{
-    struct hci_le_conn_param_req evt;
-    uint16_t opcode;
-    uint8_t hci_status;
-
-    evt.subevent_code = BLE_HCI_LE_SUBEV_REM_CONN_PARM_REQ;
-    evt.connection_handle = 2;
-    evt.itvl_min = params->itvl_min;
-    evt.itvl_max = params->itvl_max;
-    evt.latency = params->latency;
-    evt.timeout = params->supervision_timeout;
-
-    if (pos) {
-        opcode = ble_hs_hci_util_opcode_join(
-            BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_REM_CONN_PARAM_RR);
-    } else {
-        opcode = ble_hs_hci_util_opcode_join(
-            BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_REM_CONN_PARAM_NRR);
-    }
-    if (*cmd_idx == cmd_fail_idx) {
-        hci_status = fail_status;
-    } else {
-        hci_status = 0;
-    }
-    (*cmd_idx)++;
-
-    ble_hs_test_util_set_ack(opcode, hci_status);
-    ble_gap_rx_param_req(&evt);
-
-    return hci_status;
-}
-
-/*****************************************************************************
- * $white list                                                               *
- *****************************************************************************/
-
-static void
-ble_gap_test_util_wl_set(struct ble_gap_white_entry *white_list,
-                         int white_list_count, int cmd_fail_idx,
-                         uint8_t fail_status)
-{
-    int cmd_idx;
-    int rc;
-    int i;
-
-    ble_gap_test_util_init();
-    cmd_idx = 0;
-
-    rc = ble_hs_test_util_wl_set(white_list, white_list_count, cmd_fail_idx,
-                                 fail_status);
-    TEST_ASSERT(rc == BLE_HS_HCI_ERR(fail_status));
-
-    /* Verify tx of clear white list command. */
-    ble_gap_test_util_verify_tx_clear_wl();
-    if (cmd_idx >= cmd_fail_idx) {
-        return;
-    }
-    cmd_idx++;
-
-    /* Verify tx of add white list commands. */
-    for (i = 0; i < white_list_count; i++) {
-        ble_gap_test_util_verify_tx_add_wl(white_list + i);
-        if (cmd_idx >= cmd_fail_idx) {
-            return;
-        }
-        cmd_idx++;
-    }
-}
-
-TEST_CASE(ble_gap_test_case_wl_bad_args)
-{
-    int rc;
-
-    ble_gap_test_util_init();
-
-    /*** 0 white list entries. */
-    rc = ble_hs_test_util_wl_set(NULL, 0, 0, 0);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-
-    /*** Invalid address type. */
-    rc = ble_hs_test_util_wl_set(
-        ((struct ble_gap_white_entry[]) { {
-            5, { 1, 2, 3, 4, 5, 6 }
-        }, }),
-        1, 0, 0);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-
-    /*** White-list-using connection in progress. */
-    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
-                                  BLE_GAP_ADDR_TYPE_WL, NULL, 0, NULL,
-                                  ble_gap_test_util_connect_cb, NULL, 0);
-    TEST_ASSERT(rc == 0);
-
-    rc = ble_hs_test_util_wl_set(
-        ((struct ble_gap_white_entry[]) { {
-            BLE_ADDR_TYPE_PUBLIC, { 1, 2, 3, 4, 5, 6 }
-        }, }),
-        1, 0, 0);
-    TEST_ASSERT(rc == BLE_HS_EBUSY);
-}
-
-TEST_CASE(ble_gap_test_case_wl_ctlr_fail)
-{
-    int i;
-
-    struct ble_gap_white_entry white_list[] = {
-        { BLE_ADDR_TYPE_PUBLIC, { 1, 2, 3, 4, 5, 6 } },
-        { BLE_ADDR_TYPE_PUBLIC, { 2, 3, 4, 5, 6, 7 } },
-        { BLE_ADDR_TYPE_PUBLIC, { 3, 4, 5, 6, 7, 8 } },
-        { BLE_ADDR_TYPE_PUBLIC, { 4, 5, 6, 7, 8, 9 } },
-    };
-    int white_list_count = sizeof white_list / sizeof white_list[0];
-
-    for (i = 0; i < 5; i++) {
-        ble_gap_test_util_wl_set(white_list, white_list_count, i,
-                                 BLE_ERR_UNSPECIFIED);
-    }
-}
-
-TEST_CASE(ble_gap_test_case_wl_good)
-{
-    struct ble_gap_white_entry white_list[] = {
-        { BLE_ADDR_TYPE_PUBLIC, { 1, 2, 3, 4, 5, 6 } },
-        { BLE_ADDR_TYPE_PUBLIC, { 2, 3, 4, 5, 6, 7 } },
-        { BLE_ADDR_TYPE_PUBLIC, { 3, 4, 5, 6, 7, 8 } },
-        { BLE_ADDR_TYPE_PUBLIC, { 4, 5, 6, 7, 8, 9 } },
-    };
-    int white_list_count = sizeof white_list / sizeof white_list[0];
-
-    ble_gap_test_util_wl_set(white_list, white_list_count, 0, 0);
-}
-
-TEST_SUITE(ble_gap_test_suite_wl)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gap_test_case_wl_good();
-    ble_gap_test_case_wl_bad_args();
-    ble_gap_test_case_wl_ctlr_fail();
-}
-
-/*****************************************************************************
- * $discovery                                                                *
- *****************************************************************************/
-
-static int
-ble_gap_test_util_disc(uint8_t own_addr_type,
-                       const struct ble_gap_disc_params *disc_params,
-                       struct ble_gap_disc_desc *desc, int cmd_fail_idx,
-                       uint8_t fail_status)
-{
-    int rc;
-
-    ble_gap_test_util_init();
-
-    TEST_ASSERT(!ble_gap_disc_active());
-
-    /* Begin the discovery procedure. */
-    rc = ble_hs_test_util_disc(own_addr_type, BLE_HS_FOREVER, disc_params,
-                               ble_gap_test_util_disc_cb, NULL, cmd_fail_idx,
-                               fail_status);
-    TEST_ASSERT(rc == BLE_HS_HCI_ERR(fail_status));
-    if (rc == 0) {
-        TEST_ASSERT(ble_gap_master_in_progress());
-        ble_gap_rx_adv_report(desc);
-    } else {
-        TEST_ASSERT(ble_gap_test_disc_event_type == -1);
-    }
-
-    if (cmd_fail_idx > 0) {
-        /* Verify tx of set scan parameters command. */
-        ble_gap_test_util_verify_tx_set_scan_params(
-            own_addr_type,
-            disc_params->passive ?
-                BLE_HCI_SCAN_TYPE_PASSIVE :
-                BLE_HCI_SCAN_TYPE_ACTIVE,
-            disc_params->itvl,
-            disc_params->window,
-            disc_params->filter_policy);
-    }
-
-    if (cmd_fail_idx > 1) {
-        /* Verify tx of scan enable command. */
-        ble_gap_test_util_verify_tx_scan_enable(
-            1, disc_params->filter_duplicates);
-    }
-
-    if (rc == 0) {
-        TEST_ASSERT(ble_gap_disc_active());
-    }
-
-    return rc;
-}
-
-TEST_CASE(ble_gap_test_case_disc_bad_args)
-{
-    struct ble_gap_disc_params params;
-    int rc;
-
-    params.itvl = 0;
-    params.window = 0;
-    params.filter_policy = BLE_HCI_SCAN_FILT_NO_WL;
-    params.limited = 0;
-    params.passive = 0;
-    params.filter_duplicates = 0;
-
-    ble_gap_test_util_init();
-
-    /*** Invalid filter policy. */
-    params.filter_policy = 6;
-    rc = ble_gap_disc(BLE_ADDR_TYPE_PUBLIC, 0, &params,
-                      ble_gap_test_util_disc_cb, NULL);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-}
-
-TEST_CASE(ble_gap_test_case_disc_good)
-{
-    uint8_t adv_data[32];
-    uint8_t flags;
-    uint8_t own_addr_type;
-    int passive;
-    int limited;
-    int rc;
-
-    struct ble_gap_disc_desc desc = {
-        .event_type = BLE_HCI_ADV_TYPE_ADV_IND,
-        .addr_type = BLE_ADDR_TYPE_PUBLIC,
-        .length_data = 0,
-        .rssi = 0,
-        .addr = { 1, 2, 3, 4, 5, 6 },
-        .data = adv_data,
-    };
-    struct ble_gap_disc_params disc_params = {
-        .itvl = BLE_GAP_SCAN_SLOW_INTERVAL1,
-        .window = BLE_GAP_SCAN_SLOW_WINDOW1,
-        .filter_policy = BLE_HCI_CONN_FILT_NO_WL,
-        .limited = 0,
-        .passive = 0,
-        .filter_duplicates = 0,
-    };
-
-    flags = BLE_HS_ADV_F_DISC_LTD;
-    rc = ble_hs_adv_set_flat(BLE_HS_ADV_TYPE_FLAGS, 1, &flags,
-                             desc.data, &desc.length_data,
-                             sizeof adv_data);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    for (own_addr_type = 0;
-         own_addr_type <= BLE_ADDR_TYPE_RPA_RND_DEFAULT;
-         own_addr_type++)
-    for (passive = 0; passive <= 1; passive++)
-    for (limited = 0; limited <= 1; limited++) {
-        disc_params.passive = passive;
-        disc_params.limited = limited;
-        ble_gap_test_util_disc(own_addr_type, &disc_params, &desc, -1, 0);
-
-        TEST_ASSERT(ble_gap_master_in_progress());
-        TEST_ASSERT(ble_gap_test_disc_event_type == BLE_GAP_EVENT_DISC);
-        TEST_ASSERT(ble_gap_test_disc_desc.event_type ==
-                    BLE_HCI_ADV_TYPE_ADV_IND);
-        TEST_ASSERT(ble_gap_test_disc_desc.addr_type ==
-                    BLE_ADDR_TYPE_PUBLIC);
-        TEST_ASSERT(ble_gap_test_disc_desc.length_data == 3);
-        TEST_ASSERT(ble_gap_test_disc_desc.rssi == 0);
-        TEST_ASSERT(memcmp(ble_gap_test_disc_desc.addr, desc.addr, 6) == 0);
-        TEST_ASSERT(ble_gap_test_disc_arg == NULL);
-
-    }
-}
-
-TEST_CASE(ble_gap_test_case_disc_ltd_mismatch)
-{
-    int rc;
-    struct ble_gap_disc_desc desc = {
-        .event_type = BLE_HCI_ADV_TYPE_ADV_IND,
-        .addr_type = BLE_ADDR_TYPE_PUBLIC,
-        .length_data = 0,
-        .rssi = 0,
-        .addr = { 1, 2, 3, 4, 5, 6 },
-        .data = (uint8_t[BLE_HCI_MAX_ADV_DATA_LEN]){
-            2, 
-            BLE_HS_ADV_TYPE_FLAGS,
-            BLE_HS_ADV_F_DISC_GEN,
-        },
-    };
-    struct ble_gap_disc_params disc_params = {
-        .itvl = BLE_GAP_SCAN_SLOW_INTERVAL1,
-        .window = BLE_GAP_SCAN_SLOW_WINDOW1,
-        .filter_policy = BLE_HCI_CONN_FILT_NO_WL,
-        .limited = 1,
-        .passive = 0,
-        .filter_duplicates = 0,
-    };
-
-    rc = ble_gap_test_util_disc(BLE_ADDR_TYPE_PUBLIC, &disc_params, &desc,
-                                -1, 0);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(ble_gap_master_in_progress());
-
-    /* Verify that the report was ignored because of a mismatched LTD flag. */
-    TEST_ASSERT(ble_gap_test_disc_event_type == -1);
-
-    /* Stop the scan and swap the flags. */
-    rc = ble_hs_test_util_disc_cancel(0);
-    TEST_ASSERT(rc == 0);
-
-    desc.data[2] = BLE_HS_ADV_F_DISC_LTD;
-    disc_params.limited = 0;
-    rc = ble_gap_test_util_disc(BLE_ADDR_TYPE_PUBLIC, &disc_params, &desc,
-                                -1, 0);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(ble_gap_master_in_progress());
-
-    /* This time we should have reported the advertisement; general discovery
-     * hears everything.
-     */
-    TEST_ASSERT(ble_gap_test_disc_event_type == BLE_GAP_EVENT_DISC);
-
-}
-
-TEST_CASE(ble_gap_test_case_disc_hci_fail)
-{
-    int fail_idx;
-    int limited;
-    int rc;
-
-    struct ble_gap_disc_desc desc = {
-        .event_type = BLE_HCI_ADV_TYPE_ADV_IND,
-        .addr_type = BLE_ADDR_TYPE_PUBLIC,
-        .length_data = 0,
-        .rssi = 0,
-        .addr = { 1, 2, 3, 4, 5, 6 },
-        .data = NULL,
-    };
-    struct ble_gap_disc_params disc_params = {
-        .itvl = BLE_GAP_SCAN_SLOW_INTERVAL1,
-        .window = BLE_GAP_SCAN_SLOW_WINDOW1,
-        .filter_policy = BLE_HCI_CONN_FILT_NO_WL,
-        .limited = 0,
-        .passive = 0,
-        .filter_duplicates = 0,
-    };
-
-    for (limited = 0; limited <= 1; limited++) {
-        disc_params.limited = limited;
-
-        for (fail_idx = 0; fail_idx < 2; fail_idx++) {
-            rc = ble_gap_test_util_disc(BLE_ADDR_TYPE_PUBLIC, &disc_params,
-                                        &desc, fail_idx, BLE_ERR_UNSUPPORTED);
-            TEST_ASSERT(rc == BLE_HS_HCI_ERR(BLE_ERR_UNSUPPORTED));
-            TEST_ASSERT(!ble_gap_master_in_progress());
-        }
-    }
-}
-
-static void
-ble_gap_test_util_disc_dflts_once(int limited)
-{
-    struct ble_gap_disc_params params;
-    uint16_t exp_window;
-    uint16_t exp_itvl;
-    int rc;
-
-    ble_gap_test_util_init();
-
-    memset(&params, 0, sizeof params);
-    params.limited = limited;
-
-    rc = ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC, 0, &params,
-                               ble_gap_test_util_disc_cb, NULL, -1, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    if (limited) {
-        exp_itvl = BLE_GAP_LIM_DISC_SCAN_INT;
-        exp_window = BLE_GAP_LIM_DISC_SCAN_WINDOW;
-    } else {
-        exp_itvl = BLE_GAP_SCAN_FAST_INTERVAL_MIN;
-        exp_window = BLE_GAP_SCAN_FAST_WINDOW;
-    }
-    ble_gap_test_util_verify_tx_set_scan_params(
-        BLE_ADDR_TYPE_PUBLIC,
-        BLE_HCI_SCAN_TYPE_ACTIVE,
-        exp_itvl,
-        exp_window,
-        BLE_HCI_SCAN_FILT_NO_WL);
-
-    ble_gap_test_util_verify_tx_scan_enable(1, 0);
-}
-
-TEST_CASE(ble_gap_test_case_disc_dflts)
-{
-    ble_gap_test_util_disc_dflts_once(0);
-    ble_gap_test_util_disc_dflts_once(1);
-}
-
-TEST_CASE(ble_gap_test_case_disc_already)
-{
-    static const struct ble_gap_disc_params disc_params = { 0 };
-    int rc;
-
-    ble_gap_test_util_init();
-
-    /* Start a discovery procedure. */
-    rc = ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER,
-                               &disc_params, ble_gap_test_util_disc_cb,
-                               NULL, -1, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Ensure host indicates BLE_HS_EALREADY if we try to discover. */
-    rc = ble_gap_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER, &disc_params,
-                               ble_gap_test_util_disc_cb, NULL);
-    TEST_ASSERT(rc == BLE_HS_EALREADY);
-}
-
-TEST_CASE(ble_gap_test_case_disc_busy)
-{
-    static const struct ble_gap_disc_params disc_params = { 0 };
-    static const uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int rc;
-
-    ble_gap_test_util_init();
-
-    /* Start a connect procedure. */
-    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                                  peer_addr, 0, NULL,
-                                  ble_gap_test_util_connect_cb, NULL, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Ensure host indicates BLE_HS_EBUSY if we try to discover. */
-    rc = ble_gap_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER, &disc_params,
-                               ble_gap_test_util_disc_cb, NULL);
-    TEST_ASSERT(rc == BLE_HS_EBUSY);
-}
-
-TEST_SUITE(ble_gap_test_suite_disc)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gap_test_case_disc_bad_args();
-    ble_gap_test_case_disc_good();
-    ble_gap_test_case_disc_ltd_mismatch();
-    ble_gap_test_case_disc_hci_fail();
-    ble_gap_test_case_disc_dflts();
-    ble_gap_test_case_disc_already();
-    ble_gap_test_case_disc_busy();
-}
-
-/*****************************************************************************
- * $direct connect                                                           *
- *****************************************************************************/
-
-TEST_CASE(ble_gap_test_case_conn_gen_good)
-{
-    struct hci_le_conn_complete evt;
-    struct ble_gap_conn_params params;
-    int rc;
-
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    ble_gap_test_util_init();
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_gap_conn_active());
-
-    params.scan_itvl = 0x12;
-    params.scan_window = 0x11;
-    params.itvl_min = 25;
-    params.itvl_max = 26;
-    params.latency = 1;
-    params.supervision_timeout = 20;
-    params.min_ce_len = 3;
-    params.max_ce_len = 4;
-
-    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
-                                  BLE_ADDR_TYPE_PUBLIC, peer_addr, 0, &params,
-                                  ble_gap_test_util_connect_cb, NULL, 0);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(ble_gap_master_in_progress());
-    TEST_ASSERT(ble_gap_conn_active());
-
-    TEST_ASSERT(ble_gap_master_in_progress());
-    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == BLE_HS_ENOTCONN);
-
-    /* Receive connection complete event. */
-    memset(&evt, 0, sizeof evt);
-    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    evt.status = BLE_ERR_SUCCESS;
-    evt.connection_handle = 2;
-    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
-    memcpy(evt.peer_addr, peer_addr, 6);
-    rc = ble_gap_rx_conn_complete(&evt);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONNECT);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-
-    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == 0);
-}
-
-TEST_CASE(ble_gap_test_case_conn_gen_bad_args)
-{
-    int rc;
-
-    ble_gap_test_util_init();
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    /*** Invalid address type. */
-    rc = ble_gap_connect(BLE_ADDR_TYPE_PUBLIC, 5,
-                         ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), 0, NULL,
-                         ble_gap_test_util_connect_cb, NULL);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    /*** Connection already in progress. */
-    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
-                                  BLE_ADDR_TYPE_PUBLIC,
-                                  ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), 0,
-                                  NULL, ble_gap_test_util_connect_cb,
-                                  NULL, 0);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(ble_gap_master_in_progress());
-
-    rc = ble_gap_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                         ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), 0, NULL,
-                         ble_gap_test_util_connect_cb, NULL);
-    TEST_ASSERT(rc == BLE_HS_EALREADY);
-}
-
-TEST_CASE(ble_gap_test_case_conn_gen_dflt_params)
-{
-    static const uint8_t peer_addr[6] = { 2, 3, 8, 6, 6, 1 };
-    int rc;
-
-    ble_gap_test_util_init();
-
-    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
-                                  BLE_ADDR_TYPE_PUBLIC, peer_addr, 0, NULL,
-                                  ble_gap_test_util_connect_cb, NULL, 0);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ble_gap_test_case_conn_gen_already)
-{
-    static const struct ble_gap_conn_params conn_params = { 0 };
-    static const uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int rc;
-
-    ble_gap_test_util_init();
-
-    /* Start a connect procedure. */
-    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                                  peer_addr, 0, NULL,
-                                  ble_gap_test_util_connect_cb, NULL, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Ensure host indicates BLE_HS_EALREADY if we try to connect. */
-    rc = ble_gap_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                         peer_addr, BLE_HS_FOREVER, &conn_params,
-                         ble_gap_test_util_connect_cb, NULL);
-    TEST_ASSERT(rc == BLE_HS_EALREADY);
-}
-
-TEST_CASE(ble_gap_test_case_conn_gen_busy)
-{
-    static const struct ble_gap_disc_params disc_params = { 0 };
-    static const struct ble_gap_conn_params conn_params = { 0 };
-    static const uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int rc;
-
-    ble_gap_test_util_init();
-
-    /* Start a discovery procedure. */
-    rc = ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER,
-                               &disc_params, ble_gap_test_util_disc_cb,
-                               NULL, -1, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Ensure host indicates BLE_HS_EBUSY if we try to connect. */
-    rc = ble_gap_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                         peer_addr, BLE_HS_FOREVER, &conn_params,
-                         ble_gap_test_util_connect_cb, NULL);
-    TEST_ASSERT(rc == BLE_HS_EBUSY);
-}
-
-TEST_SUITE(ble_gap_test_suite_conn_gen)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gap_test_case_conn_gen_good();
-    ble_gap_test_case_conn_gen_bad_args();
-    ble_gap_test_case_conn_gen_dflt_params();
-    ble_gap_test_case_conn_gen_already();
-    ble_gap_test_case_conn_gen_busy();
-}
-
-/*****************************************************************************
- * $cancel                                                                   *
- *****************************************************************************/
-
-static void
-ble_gap_test_util_conn_cancel(uint8_t hci_status)
-{
-    struct hci_le_conn_complete evt;
-    int rc;
-
-    /* Initiate cancel procedure. */
-    rc = ble_hs_test_util_conn_cancel(hci_status);
-    TEST_ASSERT(rc == BLE_HS_HCI_ERR(hci_status));
-
-    /* Verify tx of cancel create connection command. */
-    ble_hs_test_util_verify_tx_create_conn_cancel();
-    if (rc != 0) {
-        return;
-    }
-    TEST_ASSERT(ble_gap_master_in_progress());
-
-    /* Receive connection complete event. */
-    memset(&evt, 0, sizeof evt);
-    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    evt.status = BLE_ERR_UNK_CONN_ID;
-    rc = ble_gap_rx_conn_complete(&evt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_CANCEL);
-}
-
-static void
-ble_gap_test_util_conn_and_cancel(uint8_t *peer_addr, uint8_t hci_status)
-{
-    int rc;
-
-    ble_gap_test_util_init();
-
-    /* Begin creating a connection. */
-    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
-                                  BLE_ADDR_TYPE_PUBLIC, peer_addr, 0, NULL,
-                                  ble_gap_test_util_connect_cb, NULL, 0);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(ble_gap_master_in_progress());
-
-    /* Initiate cancel procedure. */
-    ble_gap_test_util_conn_cancel(hci_status);
-    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == BLE_HS_ENOTCONN);
-}
-
-TEST_CASE(ble_gap_test_case_conn_cancel_bad_args)
-{
-    int rc;
-
-    ble_gap_test_util_init();
-
-    /* Initiate cancel procedure with no connection in progress. */
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    rc = ble_hs_test_util_conn_cancel(0);
-    TEST_ASSERT(rc == BLE_HS_EALREADY);
-}
-
-TEST_CASE(ble_gap_test_case_conn_cancel_good)
-{
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    ble_gap_test_util_conn_and_cancel(peer_addr, 0);
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_CANCEL);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == BLE_HS_CONN_HANDLE_NONE);
-}
-
-TEST_CASE(ble_gap_test_case_conn_cancel_ctlr_fail)
-{
-    struct hci_le_conn_complete evt;
-    int rc;
-
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    ble_gap_test_util_conn_and_cancel(peer_addr, BLE_ERR_REPEATED_ATTEMPTS);
-
-    /* Make sure the host didn't invoke the application callback.  The cancel
-     * failure was indicated via the return code from the gap call.
-     */
-    TEST_ASSERT(ble_gap_test_conn_event_type == -1);
-
-    /* Allow connection complete to succeed. */
-    memset(&evt, 0, sizeof evt);
-    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    evt.status = BLE_ERR_SUCCESS;
-    evt.connection_handle = 2;
-    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
-    memcpy(evt.peer_addr, peer_addr, 6);
-    rc = ble_gap_rx_conn_complete(&evt);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONNECT);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-
-    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == 0);
-}
-
-TEST_SUITE(ble_gap_test_suite_conn_cancel)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gap_test_case_conn_cancel_good();
-    ble_gap_test_case_conn_cancel_bad_args();
-    ble_gap_test_case_conn_cancel_ctlr_fail();
-}
-
-/*****************************************************************************
- * $terminate                                                                *
- *****************************************************************************/
-
-static void
-ble_gap_test_util_terminate(uint8_t *peer_addr, uint8_t hci_status)
-{
-    struct hci_disconn_complete evt;
-    int rc;
-
-    ble_gap_test_util_init();
-
-    /* Create a connection. */
-    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
-                                 NULL);
-
-    /* Reset the callback event code; we don't care about the successful
-     * connection in this test.
-     */
-    ble_gap_test_conn_event_type = -1;
-
-    /* Terminate the connection. */
-    rc = ble_hs_test_util_conn_terminate(2, hci_status);
-    TEST_ASSERT(rc == BLE_HS_HCI_ERR(hci_status));
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    /* Verify tx of disconnect command. */
-    ble_gap_test_util_verify_tx_disconnect();
-
-    if (hci_status == 0) {
-        /* Receive disconnection complete event. */
-        evt.connection_handle = 2;
-        evt.status = 0;
-        evt.reason = BLE_ERR_CONN_TERM_LOCAL;
-        ble_gap_rx_disconn_complete(&evt);
-    }
-}
-
-TEST_CASE(ble_gap_test_case_conn_terminate_bad_args)
-{
-    int rc;
-
-    ble_gap_test_util_init();
-
-    /*** Nonexistent connection. */
-    rc = ble_hs_test_util_conn_terminate(2, 0);
-    TEST_ASSERT(rc == BLE_HS_ENOTCONN);
-}
-
-TEST_CASE(ble_gap_test_case_conn_terminate_good)
-{
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    ble_gap_test_util_terminate(peer_addr, 0);
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_DISCONNECT);
-    TEST_ASSERT(ble_gap_test_conn_status ==
-                BLE_HS_HCI_ERR(BLE_ERR_CONN_TERM_LOCAL));
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(ble_gap_test_conn_desc.peer_id_addr_type ==
-                BLE_ADDR_TYPE_PUBLIC);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-    TEST_ASSERT(ble_gap_test_conn_arg == NULL);
-
-    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == BLE_HS_ENOTCONN);
-    TEST_ASSERT(!ble_gap_master_in_progress());
-}
-
-TEST_CASE(ble_gap_test_case_conn_terminate_ctlr_fail)
-{
-    struct hci_disconn_complete evt;
-    int rc;
-
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    ble_gap_test_util_init();
-
-    /* Create a connection. */
-    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
-                                 NULL);
-
-    /* Terminate the connection. */
-    rc = ble_hs_test_util_conn_terminate(2, 0);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    /* Verify tx of disconnect command. */
-    ble_gap_test_util_verify_tx_disconnect();
-
-    /* Receive failed disconnection complete event. */
-    evt.connection_handle = 2;
-    evt.status = BLE_ERR_UNSUPPORTED;
-    evt.reason = 0;
-    ble_gap_rx_disconn_complete(&evt);
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_TERM_FAILURE);
-    TEST_ASSERT(ble_gap_test_conn_status ==
-                BLE_HS_HCI_ERR(BLE_ERR_UNSUPPORTED));
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(ble_gap_test_conn_desc.peer_id_addr_type ==
-                BLE_ADDR_TYPE_PUBLIC);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-    TEST_ASSERT(ble_gap_test_conn_arg == NULL);
-
-    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == 0);
-    TEST_ASSERT(!ble_gap_master_in_progress());
-}
-
-TEST_CASE(ble_gap_test_case_conn_terminate_hci_fail)
-{
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    ble_gap_test_util_terminate(peer_addr, BLE_ERR_REPEATED_ATTEMPTS);
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == -1);
-    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == 0);
-    TEST_ASSERT(!ble_gap_master_in_progress());
-}
-
-TEST_SUITE(ble_gap_test_suite_conn_terminate)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gap_test_case_conn_terminate_bad_args();
-    ble_gap_test_case_conn_terminate_good();
-    ble_gap_test_case_conn_terminate_ctlr_fail();
-    ble_gap_test_case_conn_terminate_hci_fail();
-}
-
-/*****************************************************************************
- * $conn find                                                                *
- *****************************************************************************/
-
-TEST_CASE(ble_gap_test_case_conn_find)
-{
-
-    struct ble_gap_conn_desc desc;
-    struct ble_hs_conn *conn;
-    uint8_t pub_addr[6];
-    int rc;
-
-    /*** We are master; public addresses. */
-    ble_gap_test_util_init();
-
-    ble_hs_test_util_create_rpa_conn(8,
-                                     BLE_ADDR_TYPE_PUBLIC,
-                                     ((uint8_t[6]){0,0,0,0,0,0}),
-                                     BLE_ADDR_TYPE_PUBLIC,
-                                     ((uint8_t[6]){2,3,4,5,6,7}),
-                                     ((uint8_t[6]){0,0,0,0,0,0}),
-                                     ble_gap_test_util_connect_cb,
-                                     NULL);
-
-
-    rc = ble_hs_id_copy_addr(BLE_ADDR_TYPE_PUBLIC, pub_addr, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    rc = ble_gap_conn_find(8, &desc);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(desc.conn_handle == 8);
-    TEST_ASSERT(desc.our_id_addr_type == BLE_ADDR_TYPE_PUBLIC);
-    TEST_ASSERT(desc.our_ota_addr_type == BLE_ADDR_TYPE_PUBLIC);
-    TEST_ASSERT(desc.peer_ota_addr_type == BLE_ADDR_TYPE_PUBLIC);
-    TEST_ASSERT(desc.role == BLE_GAP_ROLE_MASTER);
-    TEST_ASSERT(memcmp(desc.our_ota_addr, pub_addr, 6) == 0);
-    TEST_ASSERT(memcmp(desc.our_id_addr, pub_addr, 6) == 0);
-    TEST_ASSERT(memcmp(desc.peer_ota_addr,
-                       ((uint8_t[6]){2,3,4,5,6,7}), 6) == 0);
-    TEST_ASSERT(memcmp(desc.peer_id_addr,
-                       ((uint8_t[6]){2,3,4,5,6,7}), 6) == 0);
-    TEST_ASSERT(desc.conn_itvl == BLE_GAP_INITIAL_CONN_ITVL_MAX);
-    TEST_ASSERT(desc.conn_latency == BLE_GAP_INITIAL_CONN_LATENCY);
-    TEST_ASSERT(desc.supervision_timeout ==
-                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
-    TEST_ASSERT(desc.master_clock_accuracy == 0);
-    TEST_ASSERT(!desc.sec_state.encrypted);
-    TEST_ASSERT(!desc.sec_state.authenticated);
-    TEST_ASSERT(!desc.sec_state.bonded);
-
-    /*** Swap roles. */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(8);
-    conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
-    ble_hs_unlock();
-
-    rc = ble_gap_conn_find(8, &desc);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(desc.role == BLE_GAP_ROLE_SLAVE);
-
-    /*** We are master; RPAs. */
-    ble_gap_test_util_init();
-
-    ble_hs_test_util_create_rpa_conn(54,
-                                     BLE_ADDR_TYPE_RPA_PUB_DEFAULT,
-                                     ((uint8_t[6]){0x40,1,2,3,4,5}),
-                                     BLE_ADDR_TYPE_RPA_RND_DEFAULT,
-                                     ((uint8_t[6]){3,4,5,6,7,8}),
-                                     ((uint8_t[6]){0x50,1,2,3,4,5}),
-                                     ble_gap_test_util_connect_cb,
-                                     NULL);
-
-    rc = ble_gap_conn_find(54, &desc);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(desc.conn_handle == 54);
-    TEST_ASSERT(desc.our_id_addr_type == BLE_ADDR_TYPE_PUBLIC);
-    TEST_ASSERT(desc.our_ota_addr_type == BLE_ADDR_TYPE_RPA_PUB_DEFAULT);
-    TEST_ASSERT(desc.peer_ota_addr_type == BLE_ADDR_TYPE_RPA_RND_DEFAULT);
-    TEST_ASSERT(desc.role == BLE_GAP_ROLE_MASTER);
-    TEST_ASSERT(memcmp(desc.our_ota_addr,
-                       ((uint8_t[6]){0x40,1,2,3,4,5}), 6) == 0);
-    TEST_ASSERT(memcmp(desc.our_id_addr, pub_addr, 6) == 0);
-    TEST_ASSERT(memcmp(desc.peer_ota_addr,
-                       ((uint8_t[6]){0x50,1,2,3,4,5}), 6) == 0);
-    TEST_ASSERT(memcmp(desc.peer_id_addr,
-                       ((uint8_t[6]){3,4,5,6,7,8}), 6) == 0);
-    TEST_ASSERT(desc.conn_itvl == BLE_GAP_INITIAL_CONN_ITVL_MAX);
-    TEST_ASSERT(desc.conn_latency == BLE_GAP_INITIAL_CONN_LATENCY);
-    TEST_ASSERT(desc.supervision_timeout ==
-                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
-    TEST_ASSERT(desc.master_clock_accuracy == 0);
-    TEST_ASSERT(!desc.sec_state.encrypted);
-    TEST_ASSERT(!desc.sec_state.authenticated);
-    TEST_ASSERT(!desc.sec_state.bonded);
-
-    /*** Swap roles. */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(54);
-    conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
-    ble_hs_unlock();
-
-    rc = ble_gap_conn_find(54, &desc);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(desc.role == BLE_GAP_ROLE_SLAVE);
-}
-
-TEST_SUITE(ble_gap_test_suite_conn_find)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gap_test_case_conn_find();
-}
-
-/*****************************************************************************
- * $advertise                                                                *
- *****************************************************************************/
-
-static void
-ble_gap_test_util_adv(uint8_t own_addr_type, uint8_t peer_addr_type,
-                      const uint8_t *peer_addr, uint8_t conn_mode,
-                      uint8_t disc_mode, int connect_status,
-                      int cmd_fail_idx, uint8_t fail_status)
-{
-    struct hci_le_conn_complete evt;
-    struct ble_gap_adv_params adv_params;
-    struct ble_hs_adv_fields adv_fields;
-    uint8_t hci_status;
-    int cmd_idx;
-    int rc;
-
-    ble_gap_test_util_init();
-
-    adv_params = ble_hs_test_util_adv_params;
-    adv_params.conn_mode = conn_mode;
-    adv_params.disc_mode = disc_mode;
-
-    TEST_ASSERT(!ble_gap_adv_active());
-
-    cmd_idx = 0;
-
-    if (conn_mode != BLE_GAP_CONN_MODE_DIR) {
-        memset(&adv_fields, 0, sizeof adv_fields);
-        adv_fields.tx_pwr_lvl_is_present = 1;
-        adv_fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
-
-        hci_status = ble_hs_test_util_exp_hci_status(cmd_idx, cmd_fail_idx,
-                                                     fail_status);
-        rc = ble_hs_test_util_adv_set_fields(&adv_fields, hci_status);
-
-        if (adv_fields.tx_pwr_lvl_is_present &&
-            adv_fields.tx_pwr_lvl == BLE_HS_ADV_TX_PWR_LVL_AUTO) {
-
-            TEST_ASSERT_FATAL(rc == BLE_HS_HCI_ERR(hci_status));
-            cmd_idx++;
-        }
-    }
-
-    if (fail_status == 0 || cmd_fail_idx >= cmd_idx) {
-        rc = ble_hs_test_util_adv_start(own_addr_type, peer_addr_type,
-                                        peer_addr, &adv_params,
-                                        ble_gap_test_util_connect_cb, NULL,
-                                        cmd_fail_idx - cmd_idx, fail_status);
-
-        TEST_ASSERT(rc == BLE_HS_HCI_ERR(fail_status));
-    }
-
-    if (fail_status == 0 || cmd_fail_idx >= cmd_idx) {
-        /* Verify tx of set advertising params command. */
-        ble_gap_test_util_verify_tx_adv_params();
-    }
-    cmd_idx++;
-
-    if (conn_mode != BLE_GAP_CONN_MODE_DIR) {
-        if (fail_status == 0 || cmd_fail_idx >= cmd_idx) {
-            /* Verify tx of set advertise data command. */
-            ble_gap_test_util_verify_tx_adv_data();
-        }
-        cmd_idx++;
-
-        if (fail_status == 0 || cmd_fail_idx >= cmd_idx) {
-            /* Verify tx of set scan response data command. */
-            ble_gap_test_util_verify_tx_rsp_data();
-        }
-        cmd_idx++;
-    }
-
-    if (fail_status == 0 || cmd_fail_idx >= cmd_idx) {
-        /* Verify tx of set advertise enable command. */
-        ble_gap_test_util_verify_tx_adv_enable(1);
-    }
-    cmd_idx++;
-
-    if (connect_status != -1 &&
-        (fail_status == 0 || cmd_fail_idx >= cmd_idx)) {
-
-        TEST_ASSERT(ble_gap_adv_active());
-
-        /* Receive a connection complete event. */
-        if (conn_mode != BLE_GAP_CONN_MODE_NON) {
-            memset(&evt, 0, sizeof evt);
-            evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-            evt.status = connect_status;
-            evt.connection_handle = 2;
-            evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_SLAVE;
-            memcpy(evt.peer_addr, peer_addr, 6);
-            rc = ble_gap_rx_conn_complete(&evt);
-            TEST_ASSERT(rc == 0);
-
-            if (connect_status == 0 ||
-                connect_status == BLE_ERR_DIR_ADV_TMO) {
-
-                TEST_ASSERT(!ble_gap_adv_active());
-            } else {
-                TEST_ASSERT(ble_gap_adv_active());
-            }
-        }
-    }
-}
-
-TEST_CASE(ble_gap_test_case_adv_bad_args)
-{
-    struct ble_gap_adv_params adv_params;
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int rc;
-
-    TEST_ASSERT(!ble_gap_adv_active());
-
-    /*** Invalid discoverable mode. */
-    adv_params = ble_hs_test_util_adv_params;
-    adv_params.disc_mode = 43;
-    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                                    peer_addr, &adv_params,
-                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-    TEST_ASSERT(!ble_gap_adv_active());
-
-    /*** Invalid connectable mode. */
-    adv_params = ble_hs_test_util_adv_params;
-    adv_params.conn_mode = 27;
-    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                                    peer_addr, &adv_params,
-                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-    TEST_ASSERT(!ble_gap_adv_active());
-
-    /*** Invalid peer address type with directed advertisable mode. */
-    adv_params = ble_hs_test_util_adv_params;
-    adv_params.conn_mode = BLE_GAP_CONN_MODE_DIR;
-    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, 12,
-                                    peer_addr, &adv_params,
-                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-    TEST_ASSERT(!ble_gap_adv_active());
-
-    /*** Advertising already in progress. */
-    adv_params = ble_hs_test_util_adv_params;
-    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                                    peer_addr, &adv_params,
-                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(ble_gap_adv_active());
-
-    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                                    peer_addr, &adv_params,
-                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
-    TEST_ASSERT(rc == BLE_HS_EALREADY);
-    TEST_ASSERT(ble_gap_adv_active());
-}
-
-static void
-ble_gap_test_util_adv_verify_dflt_params(uint8_t own_addr_type,
-                                         uint8_t peer_addr_type,
-                                         const uint8_t *peer_addr,
-                                         uint8_t conn_mode,
-                                         uint8_t disc_mode)
-{
-    struct ble_gap_adv_params adv_params;
-    struct hci_adv_params hci_cmd;
-    uint8_t *hci_buf;
-    uint8_t hci_param_len;
-    int rc;
-
-    ble_gap_test_util_init();
-
-    TEST_ASSERT(!ble_gap_adv_active());
-
-    adv_params = ble_hs_test_util_adv_params;
-    adv_params.conn_mode = conn_mode;
-    adv_params.disc_mode = disc_mode;
-
-    /* Let stack calculate all default parameters. */
-    adv_params.itvl_min = 0;
-    adv_params.itvl_max = 0;
-    adv_params.channel_map = 0;
-    adv_params.filter_policy = 0;
-    adv_params.high_duty_cycle = 0;
-
-    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                                    peer_addr, &adv_params,
-                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Ensure default parameters properly filled in. */
-    hci_buf = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                             BLE_HCI_OCF_LE_SET_ADV_PARAMS,
-                                             &hci_param_len);
-    TEST_ASSERT_FATAL(hci_buf != NULL);
-    TEST_ASSERT_FATAL(hci_param_len == BLE_HCI_SET_ADV_PARAM_LEN);
-
-    hci_cmd.adv_itvl_min = le16toh(hci_buf + 0);
-    hci_cmd.adv_itvl_max = le16toh(hci_buf + 2);
-    hci_cmd.adv_type = hci_buf[4];
-    hci_cmd.own_addr_type = hci_buf[5];
-    hci_cmd.peer_addr_type = hci_buf[6];
-    memcpy(hci_cmd.peer_addr, hci_buf + 7, 6);
-    hci_cmd.adv_channel_map = hci_buf[13];
-    hci_cmd.adv_filter_policy = hci_buf[14];
-
-    if (conn_mode == BLE_GAP_CONN_MODE_NON) {
-        TEST_ASSERT(hci_cmd.adv_itvl_min == BLE_GAP_ADV_FAST_INTERVAL2_MIN);
-        TEST_ASSERT(hci_cmd.adv_itvl_max == BLE_GAP_ADV_FAST_INTERVAL2_MAX);
-    } else {
-        TEST_ASSERT(hci_cmd.adv_itvl_min == BLE_GAP_ADV_FAST_INTERVAL1_MIN);
-        TEST_ASSERT(hci_cmd.adv_itvl_max == BLE_GAP_ADV_FAST_INTERVAL1_MAX);
-    }
-
-    if (conn_mode == BLE_GAP_CONN_MODE_NON) {
-        if (disc_mode == BLE_GAP_DISC_MODE_NON) {
-            TEST_ASSERT(hci_cmd.adv_type == BLE_HCI_ADV_TYPE_ADV_NONCONN_IND);
-        } else {
-            TEST_ASSERT(hci_cmd.adv_type == BLE_HCI_ADV_TYPE_ADV_SCAN_IND);
-        }
-    } else if (conn_mode == BLE_GAP_CONN_MODE_UND) {
-        TEST_ASSERT(hci_cmd.adv_type == BLE_HCI_ADV_TYPE_ADV_IND);
-    } else {
-        TEST_ASSERT(hci_cmd.adv_type == BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_LD);
-    }
-}
-
-TEST_CASE(ble_gap_test_case_adv_dflt_params)
-{
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    int d;
-    int c;
-
-    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
-        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
-            ble_gap_test_util_adv_verify_dflt_params(
-                BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC, peer_addr, c, d);
-        }
-    }
-}
-
-TEST_CASE(ble_gap_test_case_adv_good)
-{
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int d;
-    int c;
-
-    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
-        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
-            ble_gap_test_util_adv(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                                  peer_addr, c, d, BLE_ERR_SUCCESS, -1, 0);
-
-            if (c != BLE_GAP_CONN_MODE_NON) {
-                TEST_ASSERT(!ble_gap_adv_active());
-                TEST_ASSERT(ble_gap_test_conn_event_type ==
-                                BLE_GAP_EVENT_CONNECT);
-                TEST_ASSERT(ble_gap_test_conn_status == 0);
-                TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-                TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                                   peer_addr, 6) == 0);
-                TEST_ASSERT(ble_gap_test_conn_arg == NULL);
-            }
-        }
-    }
-}
-
-TEST_CASE(ble_gap_test_case_adv_ctlr_fail)
-{
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int d;
-    int c;
-
-    for (c = BLE_GAP_CONN_MODE_NON + 1; c < BLE_GAP_CONN_MODE_MAX; c++) {
-        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
-            ble_gap_test_util_adv(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                                  peer_addr, c, d, BLE_ERR_DIR_ADV_TMO, -1, 0);
-
-            TEST_ASSERT(!ble_gap_adv_active());
-            TEST_ASSERT(ble_gap_test_conn_event_type ==
-                        BLE_GAP_EVENT_ADV_COMPLETE);
-            TEST_ASSERT(ble_gap_test_conn_desc.conn_handle ==
-                        BLE_HS_CONN_HANDLE_NONE);
-            TEST_ASSERT(ble_gap_test_conn_arg == NULL);
-        }
-    }
-}
-
-TEST_CASE(ble_gap_test_case_adv_hci_fail)
-{
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int num_hci_cmds;
-    int fail_idx;
-    int d;
-    int c;
-
-    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
-        if (c == BLE_GAP_CONN_MODE_DIR) {
-            num_hci_cmds = 2;
-        } else {
-            num_hci_cmds = 5;
-        }
-
-        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
-            for (fail_idx = 0; fail_idx < num_hci_cmds; fail_idx++) {
-                ble_gap_test_util_adv(BLE_ADDR_TYPE_PUBLIC,
-                                      BLE_ADDR_TYPE_PUBLIC, peer_addr,
-                                      c, d, 0, fail_idx, BLE_ERR_UNSUPPORTED);
-
-                TEST_ASSERT(!ble_gap_adv_active());
-                TEST_ASSERT(ble_gap_test_conn_event_type == -1);
-            }
-        }
-    }
-}
-
-TEST_SUITE(ble_gap_test_suite_adv)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gap_test_case_adv_bad_args();
-    ble_gap_test_case_adv_dflt_params();
-    ble_gap_test_case_adv_good();
-    ble_gap_test_case_adv_ctlr_fail();
-    ble_gap_test_case_adv_hci_fail();
-}
-
-/*****************************************************************************
- * $stop advertise                                                           *
- *****************************************************************************/
-
-static void
-ble_gap_test_util_stop_adv(uint8_t peer_addr_type, const uint8_t *peer_addr,
-                           uint8_t conn_mode, uint8_t disc_mode,
-                           int cmd_fail_idx, uint8_t fail_status)
-{
-    uint8_t hci_status;
-    int rc;
-
-    ble_gap_test_util_init();
-
-    /* Start advertising; don't rx a successful connection event. */
-    ble_gap_test_util_adv(BLE_ADDR_TYPE_PUBLIC, peer_addr_type, peer_addr,
-                          conn_mode, disc_mode, -1, -1, 0);
-
-    TEST_ASSERT(ble_gap_adv_active());
-
-    /* Stop advertising. */
-    hci_status = cmd_fail_idx == 0 ? fail_status : 0;
-
-    rc = ble_hs_test_util_adv_stop(hci_status);
-    TEST_ASSERT(rc == BLE_HS_HCI_ERR(hci_status));
-
-    /* Verify tx of advertising enable command. */
-    ble_gap_test_util_verify_tx_adv_enable(0);
-}
-
-TEST_CASE(ble_gap_test_case_stop_adv_good)
-{
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int d;
-    int c;
-
-    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
-        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
-            ble_gap_test_util_stop_adv(BLE_ADDR_TYPE_PUBLIC, peer_addr, c, d,
-                                       -1, 0);
-            TEST_ASSERT(!ble_gap_adv_active());
-            TEST_ASSERT(ble_gap_test_conn_event_type == -1);
-            TEST_ASSERT(ble_gap_test_conn_status == -1);
-            TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == (uint16_t)-1);
-            TEST_ASSERT(ble_gap_test_conn_arg == (void *)-1);
-        }
-    }
-}
-
-TEST_CASE(ble_gap_test_case_stop_adv_hci_fail)
-{
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int d;
-    int c;
-
-    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
-        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
-            ble_gap_test_util_stop_adv(BLE_ADDR_TYPE_PUBLIC, peer_addr, c, d,
-                                       0, BLE_ERR_UNSUPPORTED);
-            TEST_ASSERT(ble_gap_adv_active());
-            TEST_ASSERT(ble_gap_test_conn_event_type == -1);
-            TEST_ASSERT(ble_gap_test_conn_status == -1);
-            TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == (uint16_t)-1);
-            TEST_ASSERT(ble_gap_test_conn_arg == (void *)-1);
-        }
-    }
-}
-
-TEST_SUITE(ble_gap_test_suite_stop_adv)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gap_test_case_stop_adv_good();
-    ble_gap_test_case_stop_adv_hci_fail();
-}
-
-/*****************************************************************************
- * $update connection                                                        *
- *****************************************************************************/
-
-static void
-ble_gap_test_util_update(struct ble_gap_upd_params *params,
-                         int cmd_fail_idx, uint8_t hci_status,
-                         uint8_t event_status)
-{
-    int status;
-    int rc;
-
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    ble_gap_test_util_init();
-
-    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
-                                 NULL);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    rc = ble_hs_test_util_conn_update(2, params, hci_status);
-    TEST_ASSERT(rc == BLE_HS_HCI_ERR(hci_status));
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    /* Verify tx of connection update command. */
-    ble_gap_test_util_verify_tx_update_conn(params);
-
-    if (rc == 0) {
-        TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
-    } else {
-        TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-        return;
-    }
-
-    /* Receive connection update complete event. */
-    ble_gap_test_util_rx_update_complete(event_status, params);
-
-    if (event_status != 0) {
-        status = BLE_HS_HCI_ERR(event_status);
-        goto fail;
-    }
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
-    TEST_ASSERT(ble_gap_test_conn_status == 0);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl == params->itvl_max);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency == params->latency);
-    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
-        params->supervision_timeout);
-
-    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-
-    return;
-
-fail:
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
-    TEST_ASSERT(ble_gap_test_conn_status == status);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
-                BLE_GAP_INITIAL_CONN_ITVL_MAX);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
-                BLE_GAP_INITIAL_CONN_LATENCY);
-    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
-                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
-    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-}
-
-static void
-ble_gap_test_util_update_peer(uint8_t status,
-                              struct ble_gap_upd_params *params)
-{
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    ble_gap_test_util_init();
-
-    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
-                                 NULL);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    /* Receive connection update complete event. */
-    ble_gap_test_util_rx_update_complete(status, params);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
-    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(status));
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-
-    if (status == 0) {
-        TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl == params->itvl_max);
-        TEST_ASSERT(ble_gap_test_conn_desc.conn_latency == params->latency);
-        TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
-                    params->supervision_timeout);
-    }
-
-    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-}
-
-static void
-ble_gap_test_util_update_req_pos(struct ble_gap_upd_params *peer_params,
-                                 struct ble_gap_upd_params *self_params,
-                                 int cmd_fail_idx, uint8_t hci_status)
-{
-    int cmd_idx;
-    int rc;
-
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    ble_gap_test_util_init();
-    cmd_idx = 0;
-
-    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
-                                 NULL);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    ble_gap_test_conn_self_params = *self_params;
-    rc = ble_gap_test_util_rx_param_req(peer_params, 1, &cmd_idx, cmd_fail_idx,
-                                        hci_status);
-    if (rc != 0) {
-        goto hci_fail;
-    }
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
-
-    /* Verify tx of connection parameters reply command. */
-    ble_gap_test_util_verify_tx_params_reply_pos();
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
-
-    /* Receive connection update complete event. */
-    ble_gap_test_util_rx_update_complete(0, self_params);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
-    TEST_ASSERT(ble_gap_test_conn_status == 0);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl == self_params->itvl_max);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency == self_params->latency);
-    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
-                self_params->supervision_timeout);
-
-    return;
-
-hci_fail:
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
-    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(hci_status));
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
-                BLE_GAP_INITIAL_CONN_ITVL_MAX);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
-                BLE_GAP_INITIAL_CONN_LATENCY);
-    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
-                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
-}
-
-static void
-ble_gap_test_util_update_req_neg(struct ble_gap_upd_params *peer_params,
-                                 int cmd_fail_idx, uint8_t hci_status)
-{
-    int cmd_idx;
-    int reason;
-    int rc;
-
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    ble_gap_test_util_init();
-    cmd_idx = 0;
-
-    reason = BLE_ERR_UNSPECIFIED;
-    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
-                                 &reason);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-
-    rc = ble_gap_test_util_rx_param_req(peer_params, 0, &cmd_idx, cmd_fail_idx,
-                                        hci_status);
-    if (rc != 0) {
-        goto hci_fail;
-    }
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-
-    /* Verify tx of connection parameters negative reply command. */
-    ble_gap_test_util_verify_tx_params_reply_neg(reason);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-
-    return;
-
-hci_fail:
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
-    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(hci_status));
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
-                BLE_GAP_INITIAL_CONN_ITVL_MAX);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
-                BLE_GAP_INITIAL_CONN_LATENCY);
-    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
-                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
-}
-
-static void
-ble_gap_test_util_update_req_concurrent(
-    struct ble_gap_upd_params *init_params,
-    struct ble_gap_upd_params *peer_params,
-    struct ble_gap_upd_params *self_params,
-    int cmd_fail_idx,
-    uint8_t fail_status)
-{
-    uint8_t hci_status;
-    int cmd_idx;
-    int rc;
-
-    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
-
-    ble_gap_test_util_init();
-
-    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
-                                 NULL);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-
-    hci_status = cmd_fail_idx == 0 ? fail_status : 0;
-    rc = ble_hs_test_util_conn_update(2, init_params, hci_status);
-    TEST_ASSERT(rc == BLE_HS_HCI_ERR(hci_status));
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    /* Verify tx of connection update command. */
-    ble_gap_test_util_verify_tx_update_conn(init_params);
-
-    if (rc == 0) {
-        TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
-    } else {
-        TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-        return;
-    }
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
-
-    /* Receive connection parameter update request from peer. */
-    ble_gap_test_conn_self_params = *self_params;
-    rc = ble_gap_test_util_rx_param_req(peer_params, 1, &cmd_idx, cmd_fail_idx,
-                                        hci_status);
-    if (rc != 0) {
-        goto hci_fail;
-    }
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
-
-    /* Verify tx of connection parameters reply command. */
-    ble_gap_test_util_verify_tx_params_reply_pos();
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
-
-    /* Receive connection update complete event. */
-    ble_gap_test_util_rx_update_complete(0, self_params);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
-    TEST_ASSERT(ble_gap_test_conn_status == 0);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl == self_params->itvl_max);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency == self_params->latency);
-    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
-                self_params->supervision_timeout);
-
-    return;
-
-hci_fail:
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
-    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(fail_status));
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
-    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
-                       peer_addr, 6) == 0);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
-                BLE_GAP_INITIAL_CONN_ITVL_MAX);
-    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
-                BLE_GAP_INITIAL_CONN_LATENCY);
-    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
-                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
-}
-
-TEST_CASE(ble_gap_test_case_update_conn_good)
-{
-    ble_gap_test_util_update(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 10,
-            .itvl_max = 100,
-            .supervision_timeout = 0,
-            .min_ce_len = 123,
-            .max_ce_len = 456,
-        }}),
-        -1, 0, 0);
-
-    ble_gap_test_util_update(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 100,
-            .itvl_max = 100,
-            .supervision_timeout = 100,
-            .min_ce_len = 554,
-            .max_ce_len = 554,
-        }}),
-        -1, 0, 0);
-}
-
-TEST_CASE(ble_gap_test_case_update_conn_bad)
-{
-    ble_gap_test_util_update(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 10,
-            .itvl_max = 100,
-            .supervision_timeout = 0,
-            .min_ce_len = 123,
-            .max_ce_len = 456,
-        }}),
-        -1, 0, BLE_ERR_LMP_COLLISION);
-}
-
-TEST_CASE(ble_gap_test_case_update_conn_hci_fail)
-{
-    ble_gap_test_util_update(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 10,
-            .itvl_max = 100,
-            .supervision_timeout = 0,
-            .min_ce_len = 123,
-            .max_ce_len = 456,
-        }}),
-        0, BLE_ERR_UNSUPPORTED, 0);
-}
-
-TEST_CASE(ble_gap_test_case_update_peer_good)
-{
-    ble_gap_test_util_update_peer(0,
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 10,
-            .itvl_max = 100,
-            .supervision_timeout = 0,
-            .min_ce_len = 123,
-            .max_ce_len = 456,
-        }}));
-
-    ble_gap_test_util_update_peer(0,
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 100,
-            .itvl_max = 100,
-            .supervision_timeout = 100,
-            .min_ce_len = 554,
-            .max_ce_len = 554,
-        }}));
-}
-
-TEST_CASE(ble_gap_test_case_update_req_good)
-{
-    ble_gap_test_util_update_req_pos(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 50,
-            .itvl_max = 500,
-            .supervision_timeout = 20,
-            .min_ce_len = 555,
-            .max_ce_len = 888,
-        }}),
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 10,
-            .itvl_max = 100,
-            .supervision_timeout = 0,
-            .min_ce_len = 123,
-            .max_ce_len = 456,
-        }}),
-        -1, 0);
-
-    ble_gap_test_util_update_req_pos(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 50,
-            .itvl_max = 500,
-            .supervision_timeout = 20,
-            .min_ce_len = 555,
-            .max_ce_len = 888,
-        }}),
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 100,
-            .itvl_max = 100,
-            .supervision_timeout = 100,
-            .min_ce_len = 554,
-            .max_ce_len = 554,
-        }}),
-        -1, 0);
-
-}
-
-TEST_CASE(ble_gap_test_case_update_req_hci_fail)
-{
-    ble_gap_test_util_update_req_pos(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 50,
-            .itvl_max = 500,
-            .supervision_timeout = 20,
-            .min_ce_len = 555,
-            .max_ce_len = 888,
-        }}),
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 10,
-            .itvl_max = 100,
-            .supervision_timeout = 0,
-            .min_ce_len = 123,
-            .max_ce_len = 456,
-        }}),
-        0, BLE_ERR_UNSUPPORTED);
-}
-
-TEST_CASE(ble_gap_test_case_update_req_reject)
-{
-    ble_gap_test_util_update_req_neg(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 50,
-            .itvl_max = 500,
-            .supervision_timeout = 20,
-            .min_ce_len = 555,
-            .max_ce_len = 888,
-        }}),
-        -1, 0);
-
-    ble_gap_test_util_update_req_neg(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 50,
-            .itvl_max = 500,
-            .supervision_timeout = 20,
-            .min_ce_len = 555,
-            .max_ce_len = 888,
-        }}),
-        -1, 0);
-}
-
-TEST_CASE(ble_gap_test_case_update_concurrent_good)
-{
-    ble_gap_test_util_update_req_concurrent(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 10,
-            .itvl_max = 100,
-            .supervision_timeout = 0,
-            .min_ce_len = 123,
-            .max_ce_len = 456,
-        }}),
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 50,
-            .itvl_max = 500,
-            .supervision_timeout = 20,
-            .min_ce_len = 555,
-            .max_ce_len = 888,
-        }}),
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 10,
-            .itvl_max = 100,
-            .supervision_timeout = 0,
-            .min_ce_len = 123,
-            .max_ce_len = 456,
-        }}),
-        -1, 0);
-
-    ble_gap_test_util_update_req_concurrent(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 10,
-            .itvl_max = 100,
-            .supervision_timeout = 0,
-            .min_ce_len = 123,
-            .max_ce_len = 456,
-        }}),
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 50,
-            .itvl_max = 500,
-            .supervision_timeout = 20,
-            .min_ce_len = 555,
-            .max_ce_len = 888,
-        }}),
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 20,
-            .itvl_max = 200,
-            .supervision_timeout = 2,
-            .min_ce_len = 111,
-            .max_ce_len = 222,
-        }}),
-        -1, 0);
-}
-
-TEST_CASE(ble_gap_test_case_update_concurrent_hci_fail)
-{
-    ble_gap_test_util_update_req_concurrent(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 10,
-            .itvl_max = 100,
-            .supervision_timeout = 0,
-            .min_ce_len = 123,
-            .max_ce_len = 456,
-        }}),
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 50,
-            .itvl_max = 500,
-            .supervision_timeout = 20,
-            .min_ce_len = 555,
-            .max_ce_len = 888,
-        }}),
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 20,
-            .itvl_max = 200,
-            .supervision_timeout = 2,
-            .min_ce_len = 111,
-            .max_ce_len = 222,
-        }}),
-        0, BLE_ERR_UNSUPPORTED);
-
-    ble_gap_test_util_update_req_concurrent(
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 10,
-            .itvl_max = 100,
-            .supervision_timeout = 0,
-            .min_ce_len = 123,
-            .max_ce_len = 456,
-        }}),
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 50,
-            .itvl_max = 500,
-            .supervision_timeout = 20,
-            .min_ce_len = 555,
-            .max_ce_len = 888,
-        }}),
-        ((struct ble_gap_upd_params[]) { {
-            .itvl_min = 20,
-            .itvl_max = 200,
-            .supervision_timeout = 2,
-            .min_ce_len = 111,
-            .max_ce_len = 222,
-        }}),
-        1, BLE_ERR_UNSUPPORTED);
-}
-
-TEST_SUITE(ble_gap_test_suite_update_conn)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gap_test_case_update_conn_good();
-    ble_gap_test_case_update_conn_bad();
-    ble_gap_test_case_update_conn_hci_fail();
-    ble_gap_test_case_update_peer_good();
-    ble_gap_test_case_update_req_good();
-    ble_gap_test_case_update_req_hci_fail();
-    ble_gap_test_case_update_req_reject();
-    ble_gap_test_case_update_concurrent_good();
-    ble_gap_test_case_update_concurrent_hci_fail();
-}
-
-/*****************************************************************************
- * $timeout                                                                  *
- *****************************************************************************/
-
-static void
-ble_gap_test_util_conn_forever(void)
-{
-    int32_t ticks_from_now;
-
-    /* Initiate a connect procedure with no timeout. */
-    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
-                             BLE_ADDR_TYPE_PUBLIC,
-                             ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), BLE_HS_FOREVER,
-                             NULL, ble_gap_test_util_connect_cb,
-                             NULL, 0);
-
-    /* Ensure no pending GAP event. */
-    ticks_from_now = ble_gap_heartbeat();
-    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
-
-    /* Advance 100 seconds; ensure no timeout reported. */
-    os_time_advance(100 * OS_TICKS_PER_SEC);
-    ble_gap_heartbeat();
-    TEST_ASSERT(ble_gap_test_conn_event_type == -1);
-    TEST_ASSERT(ble_gap_conn_active());
-}
-
-static void
-ble_gap_test_util_conn_timeout(int32_t duration_ms)
-{
-    struct hci_le_conn_complete evt;
-    uint32_t duration_ticks;
-    int32_t ticks_from_now;
-    int rc;
-
-    TEST_ASSERT_FATAL(duration_ms != BLE_HS_FOREVER);
-
-    /* Initiate a connect procedure with the specified timeout. */
-    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
-                             BLE_ADDR_TYPE_PUBLIC,
-                             ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), duration_ms,
-                             NULL, ble_gap_test_util_connect_cb,
-                             NULL, 0);
-
-    /* Ensure next GAP event is at the expected time. */
-    rc = os_time_ms_to_ticks(duration_ms, &duration_ticks);
-    TEST_ASSERT_FATAL(rc == 0);
-    ticks_from_now = ble_gap_heartbeat();
-    TEST_ASSERT(ticks_from_now == duration_ticks);
-
-    /* Advance duration ms; ensure timeout event does not get reported before
-     * connection complete event rxed.
-     */
-    os_time_advance(duration_ms);
-
-    ble_hs_test_util_set_ack(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_CREATE_CONN_CANCEL),
-        0);
-
-    TEST_ASSERT(ble_gap_test_conn_event_type == -1);
-
-    ticks_from_now = ble_gap_heartbeat();
-    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
-
-    /* Ensure cancel create connection command was sent. */
-    ble_hs_test_util_verify_tx_create_conn_cancel();
-
-    /* Ensure timer has been stopped. */
-    ticks_from_now = ble_gap_heartbeat();
-    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
-
-    /* Receive the connection complete event indicating a successful cancel. */
-    memset(&evt, 0, sizeof evt);
-    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    evt.status = BLE_ERR_UNK_CONN_ID;
-    rc = ble_gap_rx_conn_complete(&evt);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Ensure the GAP event was triggered. */
-    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONNECT);
-    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_ETIMEOUT);
-
-    /* Clear GAP event for remainder of test. */
-    ble_gap_test_util_reset_cb_info();
-}
-
-static void
-ble_gap_test_util_disc_forever(void)
-{
-    struct ble_gap_disc_params params;
-    int32_t ticks_from_now;
-
-    memset(&params, 0, sizeof params);
-
-    /* Initiate a discovery procedure with no timeout. */
-    ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC,
-                          BLE_HS_FOREVER, &params, ble_gap_test_util_disc_cb,
-                          NULL, -1, 0);
-
-    /* Ensure no pending GAP event. */
-    ticks_from_now = ble_gap_heartbeat();
-    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
-
-    /* Advance 100 seconds; ensure no timeout reported. */
-    os_time_advance(100 * OS_TICKS_PER_SEC);
-    TEST_ASSERT(ble_gap_test_disc_event_type == -1);
-    TEST_ASSERT(ble_gap_disc_active());
-}
-
-static void
-ble_gap_test_util_disc_timeout(int32_t duration_ms)
-{
-    struct ble_gap_disc_params params;
-    uint32_t duration_ticks;
-    int32_t ticks_from_now;
-    int rc;
-
-    TEST_ASSERT_FATAL(duration_ms != BLE_HS_FOREVER);
-
-    memset(&params, 0, sizeof params);
-
-    /* Initiate a discovery procedure with the specified timeout. */
-    ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC,
-                          duration_ms, &params, ble_gap_test_util_disc_cb,
-                          NULL, -1, 0);
-
-    /* Ensure next GAP event is at the expected time. */
-    rc = os_time_ms_to_ticks(duration_ms, &duration_ticks);
-    TEST_ASSERT_FATAL(rc == 0);
-    ticks_from_now = ble_gap_heartbeat();
-    TEST_ASSERT(ticks_from_now == duration_ticks);
-
-    /* Advance duration ms; ensure timeout event was reported. */
-    os_time_advance(duration_ms);
-
-    ble_hs_test_util_set_ack(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
-        0);
-    ticks_from_now = ble_gap_heartbeat();
-    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
-
-    TEST_ASSERT(ble_gap_test_disc_event_type == BLE_GAP_EVENT_DISC_COMPLETE);
-
-    /* Clear GAP event for remainder of test. */
-    ble_gap_test_util_reset_cb_info();
-}
-
-TEST_CASE(ble_gap_test_case_conn_timeout_conn_forever)
-{
-    ble_gap_test_util_init();
-
-    /* 3 ms. */
-    ble_gap_test_util_conn_timeout(3);
-
-    /* No timeout. */
-    ble_gap_test_util_conn_forever();
-
-}
-
-TEST_CASE(ble_gap_test_case_conn_timeout_conn_timeout)
-{
-    ble_gap_test_util_init();
-
-    /* 30 ms. */
-    ble_gap_test_util_conn_timeout(30);
-
-    /* 5 ms. */
-    ble_gap_test_util_conn_timeout(5);
-
-}
-
-TEST_CASE(ble_gap_test_case_conn_forever_conn_timeout)
-{
-    ble_gap_test_util_init();
-
-    /* No timeout. */
-    ble_gap_test_util_conn_forever();
-
-    /* Cancel connect procedure manually. */
-    ble_gap_test_util_conn_cancel(0);
-
-    /* Clear GAP event for remainder of test. */
-    ble_gap_test_util_reset_cb_info();
-
-    /* 3 ms. */
-    ble_gap_test_util_conn_timeout(3);
-}
-
-TEST_CASE(ble_gap_test_case_disc_timeout_disc_forever)
-{
-    ble_gap_test_util_init();
-
-    /* 3 ms. */
-    ble_gap_test_util_disc_timeout(3);
-
-    /* No timeout. */
-    ble_gap_test_util_disc_forever();
-
-}
-
-TEST_CASE(ble_gap_test_case_disc_timeout_disc_timeout)
-{
-    ble_gap_test_util_init();
-
-    /* 30 ms. */
-    ble_gap_test_util_disc_timeout(30);
-
-    /* 5 ms. */
-    ble_gap_test_util_disc_timeout(5);
-
-}
-
-TEST_CASE(ble_gap_test_case_disc_forever_disc_timeout)
-{
-    ble_gap_test_util_init();
-
-    /* No timeout. */
-    ble_gap_test_util_disc_forever();
-
-    /* Cancel discovery procedure manually. */
-    ble_hs_test_util_disc_cancel(0);
-
-    /* 3 ms. */
-    ble_gap_test_util_disc_timeout(3);
-}
-
-TEST_CASE(ble_gap_test_case_conn_timeout_disc_timeout)
-{
-    ble_gap_test_util_init();
-
-    /* 15 seconds. */
-    ble_gap_test_util_conn_timeout(15000);
-
-    /* 1285 ms. */
-    ble_gap_test_util_disc_timeout(1285);
-}
-
-TEST_SUITE(ble_gap_test_suite_timeout)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gap_test_case_conn_timeout_conn_forever();
-    ble_gap_test_case_conn_timeout_conn_timeout();
-    ble_gap_test_case_conn_forever_conn_timeout();
-
-    ble_gap_test_case_disc_timeout_disc_forever();
-    ble_gap_test_case_disc_timeout_disc_timeout();
-    ble_gap_test_case_disc_forever_disc_timeout();
-
-    ble_gap_test_case_conn_timeout_disc_timeout();
-}
-
-/*****************************************************************************
- * $all                                                                      *
- *****************************************************************************/
-
-int
-ble_gap_test_all(void)
-{
-    ble_gap_test_suite_wl();
-    ble_gap_test_suite_disc();
-    ble_gap_test_suite_conn_gen();
-    ble_gap_test_suite_conn_cancel();
-    ble_gap_test_suite_conn_terminate();
-    ble_gap_test_suite_conn_find();
-    ble_gap_test_suite_adv();
-    ble_gap_test_suite_stop_adv();
-    ble_gap_test_suite_update_conn();
-    ble_gap_test_suite_timeout();
-
-    return tu_any_failed;
-}


[09/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_gatts_reg_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gatts_reg_test.c b/net/nimble/host/test/src/ble_gatts_reg_test.c
new file mode 100644
index 0000000..ad5f18f
--- /dev/null
+++ b/net/nimble/host/test/src/ble_gatts_reg_test.c
@@ -0,0 +1,718 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "host/ble_uuid.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+
+#define BLE_GATTS_REG_TEST_MAX_ENTRIES  256
+
+struct ble_gatts_reg_test_entry {
+    uint8_t op;
+    uint8_t uuid128[16];
+    uint16_t handle;
+    uint16_t val_handle; /* If a characteristic. */
+
+    const struct ble_gatt_svc_def *svc;
+    const struct ble_gatt_chr_def *chr;
+    const struct ble_gatt_dsc_def *dsc;
+};
+
+static struct ble_gatts_reg_test_entry
+ble_gatts_reg_test_entries[BLE_GATTS_REG_TEST_MAX_ENTRIES];
+
+static int ble_gatts_reg_test_num_entries;
+
+static void
+ble_gatts_reg_test_init(void)
+{
+    ble_hs_test_util_init();
+    ble_gatts_reg_test_num_entries = 0;
+}
+
+static void
+ble_gatts_reg_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
+{
+    struct ble_gatts_reg_test_entry *entry;
+
+    TEST_ASSERT_FATAL(ble_gatts_reg_test_num_entries <
+                      BLE_GATTS_REG_TEST_MAX_ENTRIES);
+
+    entry = ble_gatts_reg_test_entries + ble_gatts_reg_test_num_entries++;
+    memset(entry, 0, sizeof *entry);
+
+    entry->op = ctxt->op;
+    switch (ctxt->op) {
+    case BLE_GATT_REGISTER_OP_SVC:
+        memcpy(entry->uuid128, ctxt->svc.svc_def->uuid128, 16);
+        entry->handle = ctxt->svc.handle;
+        entry->svc = ctxt->svc.svc_def;
+        break;
+
+    case BLE_GATT_REGISTER_OP_CHR:
+        memcpy(entry->uuid128, ctxt->chr.chr_def->uuid128, 16);
+        entry->handle = ctxt->chr.def_handle;
+        entry->val_handle = ctxt->chr.val_handle;
+        entry->svc = ctxt->chr.svc_def;
+        entry->chr = ctxt->chr.chr_def;
+        break;
+
+    case BLE_GATT_REGISTER_OP_DSC:
+        memcpy(entry->uuid128, ctxt->dsc.dsc_def->uuid128, 16);
+        entry->handle = ctxt->dsc.handle;
+        entry->svc = ctxt->dsc.svc_def;
+        entry->chr = ctxt->dsc.chr_def;
+        entry->dsc = ctxt->dsc.dsc_def;
+        break;
+
+    default:
+        TEST_ASSERT(0);
+        break;
+    }
+}
+
+static void
+ble_gatts_reg_test_misc_lookup_good(struct ble_gatts_reg_test_entry *entry)
+{
+    uint16_t chr_def_handle;
+    uint16_t chr_val_handle;
+    uint16_t svc_handle;
+    uint16_t dsc_handle;
+    int rc;
+
+    switch (entry->op) {
+    case BLE_GATT_REGISTER_OP_SVC:
+        rc = ble_gatts_find_svc(entry->uuid128, &svc_handle);
+        TEST_ASSERT_FATAL(rc == 0);
+        TEST_ASSERT(svc_handle == entry->handle);
+        break;
+
+    case BLE_GATT_REGISTER_OP_CHR:
+        rc = ble_gatts_find_chr(entry->svc->uuid128, entry->chr->uuid128,
+                                &chr_def_handle, &chr_val_handle);
+        TEST_ASSERT_FATAL(rc == 0);
+        TEST_ASSERT(chr_def_handle == entry->handle);
+        TEST_ASSERT(chr_val_handle == entry->val_handle);
+        break;
+
+    case BLE_GATT_REGISTER_OP_DSC:
+        rc = ble_gatts_find_dsc(entry->svc->uuid128, entry->chr->uuid128,
+                                entry->dsc->uuid128, &dsc_handle);
+        break;
+
+    default:
+        TEST_ASSERT(0);
+        break;
+    }
+}
+
+static void
+ble_gatts_reg_test_misc_lookup_bad(struct ble_gatts_reg_test_entry *entry)
+{
+    struct ble_gatts_reg_test_entry *cur;
+    uint8_t wrong_uuid[16];
+    int rc;
+    int i;
+
+    switch (entry->op) {
+    case BLE_GATT_REGISTER_OP_SVC:
+        /* Wrong service UUID. */
+        memcpy(wrong_uuid, entry->svc->uuid128, 16);
+        wrong_uuid[15]++;
+        rc = ble_gatts_find_svc(wrong_uuid, NULL);
+        TEST_ASSERT(rc == BLE_HS_ENOENT);
+        break;
+
+    case BLE_GATT_REGISTER_OP_CHR:
+        /* Correct service UUID, wrong characteristic UUID. */
+        memcpy(wrong_uuid, entry->chr->uuid128, 16);
+        wrong_uuid[15]++;
+        rc = ble_gatts_find_chr(entry->svc->uuid128, wrong_uuid, NULL, NULL);
+        TEST_ASSERT(rc == BLE_HS_ENOENT);
+
+        /* Incorrect service UUID, correct characteristic UUID. */
+        memcpy(wrong_uuid, entry->svc->uuid128, 16);
+        wrong_uuid[15]++;
+        rc = ble_gatts_find_chr(wrong_uuid, entry->chr->uuid128, NULL, NULL);
+        TEST_ASSERT(rc == BLE_HS_ENOENT);
+
+        /* Existing (but wrong) service, correct characteristic UUID. */
+        for (i = 0; i < ble_gatts_reg_test_num_entries; i++) {
+            cur = ble_gatts_reg_test_entries + i;
+            switch (cur->op) {
+            case BLE_GATT_REGISTER_OP_SVC:
+                if (cur->svc != entry->svc) {
+                    rc = ble_gatts_find_chr(cur->svc->uuid128,
+                                            entry->chr->uuid128,
+                                            NULL, NULL);
+                    TEST_ASSERT(rc == BLE_HS_ENOENT);
+                }
+                break;
+
+            case BLE_GATT_REGISTER_OP_CHR:
+                /* Characteristic that isn't in this service. */
+                if (cur->svc != entry->svc) {
+                    rc = ble_gatts_find_chr(entry->svc->uuid128,
+                                            cur->chr->uuid128,
+                                            NULL, NULL);
+                    TEST_ASSERT(rc == BLE_HS_ENOENT);
+                }
+                break;
+
+            case BLE_GATT_REGISTER_OP_DSC:
+                /* Use descriptor UUID instead of characteristic UUID. */
+                rc = ble_gatts_find_chr(entry->svc->uuid128,
+                                        cur->dsc->uuid128,
+                                        NULL, NULL);
+                TEST_ASSERT(rc == BLE_HS_ENOENT);
+                break;
+
+            default:
+                TEST_ASSERT(0);
+                break;
+            }
+        }
+        break;
+
+    case BLE_GATT_REGISTER_OP_DSC:
+        /* Correct svc/chr UUID, wrong dsc UUID. */
+        memcpy(wrong_uuid, entry->dsc->uuid128, 16);
+        wrong_uuid[15]++;
+        rc = ble_gatts_find_dsc(entry->svc->uuid128, entry->chr->uuid128,
+                                wrong_uuid, NULL);
+        TEST_ASSERT(rc == BLE_HS_ENOENT);
+
+        /* Incorrect svc UUID, correct chr/dsc UUID. */
+        memcpy(wrong_uuid, entry->svc->uuid128, 16);
+        wrong_uuid[15]++;
+        rc = ble_gatts_find_dsc(wrong_uuid, entry->chr->uuid128,
+                                entry->dsc->uuid128, NULL);
+        TEST_ASSERT(rc == BLE_HS_ENOENT);
+
+        for (i = 0; i < ble_gatts_reg_test_num_entries; i++) {
+            cur = ble_gatts_reg_test_entries + i;
+            switch (cur->op) {
+            case BLE_GATT_REGISTER_OP_SVC:
+                /* Existing (but wrong) svc, correct chr/dsc UUID. */
+                if (cur->svc != entry->svc) {
+                    rc = ble_gatts_find_dsc(cur->svc->uuid128,
+                                            entry->chr->uuid128,
+                                            entry->dsc->uuid128,
+                                            NULL);
+                    TEST_ASSERT(rc == BLE_HS_ENOENT);
+                }
+                break;
+
+            case BLE_GATT_REGISTER_OP_CHR:
+                /* Existing (but wrong) svc/chr, correct dsc UUID. */
+                if (cur->chr != entry->chr) {
+                    rc = ble_gatts_find_dsc(cur->svc->uuid128,
+                                            cur->chr->uuid128,
+                                            entry->dsc->uuid128,
+                                            NULL);
+                    TEST_ASSERT(rc == BLE_HS_ENOENT);
+                }
+                break;
+
+            case BLE_GATT_REGISTER_OP_DSC:
+                /* Descriptor that isn't in this characteristic. */
+                if (cur->chr != entry->chr) {
+                    rc = ble_gatts_find_dsc(cur->svc->uuid128,
+                                            cur->chr->uuid128,
+                                            entry->dsc->uuid128,
+                                            NULL);
+                    TEST_ASSERT(rc == BLE_HS_ENOENT);
+                }
+                break;
+
+            default:
+                TEST_ASSERT(0);
+                break;
+            }
+        }
+        break;
+
+    default:
+        TEST_ASSERT(0);
+        break;
+    }
+}
+
+static void
+ble_gatts_reg_test_misc_verify_entry(uint8_t op, const uint8_t *uuid128)
+{
+    struct ble_gatts_reg_test_entry *entry;
+    int i;
+
+    for (i = 0; i < ble_gatts_reg_test_num_entries; i++) {
+        entry = ble_gatts_reg_test_entries + i;
+        if (entry->op == op && memcmp(entry->uuid128, uuid128, 16) == 0) {
+            break;
+        }
+    }
+    TEST_ASSERT_FATAL(entry != NULL);
+
+    /* Verify that characteristic value handle was properly assigned at
+     * registration.
+     */
+    if (op == BLE_GATT_REGISTER_OP_CHR) {
+        TEST_ASSERT(*entry->chr->val_handle == entry->val_handle);
+    }
+
+    /* Verify that the entry can be looked up. */
+    ble_gatts_reg_test_misc_lookup_good(entry);
+
+    /* Verify that "barely incorrect" UUID information doesn't retrieve any
+     * handles.
+     */
+    ble_gatts_reg_test_misc_lookup_bad(entry);
+}
+
+static int
+ble_gatts_reg_test_misc_dummy_access(uint16_t conn_handle,
+                                     uint16_t attr_handle,
+                                     struct ble_gatt_access_ctxt *ctxt,
+                                     void *arg)
+{
+    return 0;
+}
+
+TEST_CASE(ble_gatts_reg_test_svc_return)
+{
+    int rc;
+
+    /*** Missing UUID. */
+    ble_gatts_reg_test_init();
+    struct ble_gatt_svc_def svcs_no_uuid[] = { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+    }, {
+        0
+    } };
+
+    rc = ble_gatts_register_svcs(svcs_no_uuid, NULL, NULL);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+
+    /*** Circular dependency. */
+    ble_gatts_reg_test_init();
+    struct ble_gatt_svc_def svcs_circ[] = { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .includes = (const struct ble_gatt_svc_def*[]) { svcs_circ + 1, NULL },
+    }, {
+        .type = BLE_GATT_SVC_TYPE_SECONDARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .includes = (const struct ble_gatt_svc_def*[]) { svcs_circ + 0, NULL },
+    }, {
+        0
+    } };
+
+    rc = ble_gatts_register_svcs(svcs_circ, NULL, NULL);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+
+    /*** Success. */
+    ble_gatts_reg_test_init();
+    struct ble_gatt_svc_def svcs_good[] = { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .includes = (const struct ble_gatt_svc_def*[]) { svcs_good + 1, NULL },
+    }, {
+        .type = BLE_GATT_SVC_TYPE_SECONDARY,
+        .uuid128 = BLE_UUID16(0x1234),
+    }, {
+        0
+    } };
+
+    rc = ble_gatts_register_svcs(svcs_good, NULL, NULL);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ble_gatts_reg_test_chr_return)
+{
+    int rc;
+
+    /*** Missing callback. */
+    ble_gatts_reg_test_init();
+    struct ble_gatt_svc_def svcs_no_chr_cb[] = { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(0x1111),
+            .flags = BLE_GATT_CHR_F_READ,
+        }, {
+            0
+        } },
+    }, {
+        0
+    } };
+
+    rc = ble_gatts_register_svcs(svcs_no_chr_cb, NULL, NULL);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+
+    /*** Success. */
+    ble_gatts_reg_test_init();
+    struct ble_gatt_svc_def svcs_good[] = { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(0x1111),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_READ,
+        }, {
+            0
+        } },
+    }, {
+        0
+    } };
+
+    rc = ble_gatts_register_svcs(svcs_good, NULL, NULL);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ble_gatts_reg_test_dsc_return)
+{
+    int rc;
+
+    /*** Missing callback. */
+    ble_gatts_reg_test_init();
+    struct ble_gatt_svc_def svcs_no_dsc_cb[] = { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(0x1111),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_READ,
+            .descriptors = (struct ble_gatt_dsc_def[]) { {
+                .uuid128 = BLE_UUID16(0x8888),
+                .att_flags = 5,
+            }, {
+                0
+            } },
+        }, {
+            0
+        } },
+    }, {
+        0
+    } };
+
+    rc = ble_gatts_register_svcs(svcs_no_dsc_cb, NULL, NULL);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+
+    /*** Success. */
+    ble_gatts_reg_test_init();
+    struct ble_gatt_svc_def svcs_good[] = { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(0x1111),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_READ,
+            .descriptors = (struct ble_gatt_dsc_def[]) { {
+                .uuid128 = BLE_UUID16(0x8888),
+                .access_cb = ble_gatts_reg_test_misc_dummy_access,
+                .att_flags = 5,
+            }, {
+                0
+            } },
+        }, {
+            0
+        } },
+    }, {
+        0
+    } };
+
+    rc = ble_gatts_register_svcs(svcs_good, NULL, NULL);
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+ble_gatts_reg_test_misc_svcs(struct ble_gatt_svc_def *svcs)
+{
+    const struct ble_gatt_svc_def *svc;
+    const struct ble_gatt_chr_def *chr;
+    const struct ble_gatt_dsc_def *dsc;
+    int rc;
+
+    ble_gatts_reg_test_init();
+
+    /* Register all the attributes. */
+    rc = ble_gatts_register_svcs(svcs, ble_gatts_reg_test_misc_reg_cb,
+                                 NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Verify that the appropriate callbacks were executed. */
+    for (svc = svcs; svc->type != BLE_GATT_SVC_TYPE_END; svc++) {
+        ble_gatts_reg_test_misc_verify_entry(BLE_GATT_REGISTER_OP_SVC,
+                                             svc->uuid128);
+
+        if (svc->characteristics != NULL) {
+            for (chr = svc->characteristics; chr->uuid128 != NULL; chr++) {
+                ble_gatts_reg_test_misc_verify_entry(BLE_GATT_REGISTER_OP_CHR,
+                                                     chr->uuid128);
+
+                if (chr->descriptors != NULL) {
+                    for (dsc = chr->descriptors; dsc->uuid128 != NULL; dsc++) {
+                        ble_gatts_reg_test_misc_verify_entry(
+                            BLE_GATT_REGISTER_OP_DSC, dsc->uuid128);
+                    }
+                }
+            }
+        }
+    }
+}
+
+TEST_CASE(ble_gatts_reg_test_svc_cb)
+{
+    /*** 1 primary. */
+    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+    }, {
+        0
+    } });
+
+    /*** 3 primary. */
+    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+    }, {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x2234),
+    }, {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x3234),
+    }, {
+        0
+    } });
+
+
+    /*** 1 primary, 1 secondary. */
+    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+    }, {
+        .type = BLE_GATT_SVC_TYPE_SECONDARY,
+        .uuid128 = BLE_UUID16(0x2222),
+    }, {
+        0
+    } });
+
+    /*** 1 primary, 1 secondary, 1 include. */
+    struct ble_gatt_svc_def svcs[] = {
+        [0] = {
+            .type = BLE_GATT_SVC_TYPE_PRIMARY,
+            .uuid128 = BLE_UUID16(0x1234),
+            .includes = (const struct ble_gatt_svc_def*[]) { svcs + 1, NULL, },
+        },
+        [1] = {
+            .type = BLE_GATT_SVC_TYPE_SECONDARY,
+            .uuid128 = BLE_UUID16(0x2222),
+        }, {
+            0
+        }
+    };
+    ble_gatts_reg_test_misc_svcs(svcs);
+}
+
+TEST_CASE(ble_gatts_reg_test_chr_cb)
+{
+    uint16_t val_handles[16];
+
+    /*** 1 characteristic. */
+    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(0x1111),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_READ,
+            .val_handle = val_handles + 0,
+        }, {
+            0
+        } },
+    }, {
+        0
+    } });
+
+    /*** 3 characteristics. */
+    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(0x1111),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_READ,
+            .val_handle = val_handles + 0,
+        }, {
+            .uuid128 = BLE_UUID16(0x2222),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_WRITE,
+            .val_handle = val_handles + 1,
+        }, {
+            0
+        } },
+    }, {
+        .type = BLE_GATT_SVC_TYPE_SECONDARY,
+        .uuid128 = BLE_UUID16(0x5678),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(0x3333),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_READ,
+            .val_handle = val_handles + 2,
+        }, {
+            0
+        } },
+    }, {
+        0
+    } });
+}
+
+TEST_CASE(ble_gatts_reg_test_dsc_cb)
+{
+    uint16_t val_handles[16];
+
+    /*** 1 descriptor. */
+    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(0x1111),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_READ,
+            .val_handle = val_handles + 0,
+            .descriptors = (struct ble_gatt_dsc_def[]) { {
+                .uuid128 = BLE_UUID16(0x111a),
+                .att_flags = 5,
+                .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            }, {
+                0
+            } },
+        }, {
+            0
+        } },
+    }, {
+        0
+    } });
+
+    /*** 5+ descriptors. */
+    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(0x1234),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(0x1111),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_READ,
+            .val_handle = val_handles + 0,
+            .descriptors = (struct ble_gatt_dsc_def[]) { {
+                .uuid128 = BLE_UUID16(0x111a),
+                .att_flags = 5,
+                .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            }, {
+                0
+            } },
+        }, {
+            .uuid128 = BLE_UUID16(0x2222),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_WRITE,
+            .val_handle = val_handles + 1,
+        }, {
+            0
+        } },
+    }, {
+        .type = BLE_GATT_SVC_TYPE_SECONDARY,
+        .uuid128 = BLE_UUID16(0x5678),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(0x3333),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_READ,
+            .val_handle = val_handles + 2,
+            .descriptors = (struct ble_gatt_dsc_def[]) { {
+                .uuid128 = BLE_UUID16(0x333a),
+                .att_flags = 5,
+                .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            }, {
+                .uuid128 = BLE_UUID16(0x333b),
+                .att_flags = 5,
+                .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            }, {
+                .uuid128 = BLE_UUID16(0x333c),
+                .att_flags = 5,
+                .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            }, {
+                .uuid128 = BLE_UUID16(0x333e),
+                .att_flags = 5,
+                .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            }, {
+                0
+            } },
+        }, {
+            .uuid128 = BLE_UUID16(0x4444),
+            .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            .flags = BLE_GATT_CHR_F_READ,
+            .val_handle = val_handles + 3,
+            .descriptors = (struct ble_gatt_dsc_def[]) { {
+                .uuid128 = BLE_UUID16(0x444a),
+                .att_flags = 5,
+                .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            }, {
+                .uuid128 = BLE_UUID16(0x444b),
+                .att_flags = 5,
+                .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            }, {
+                .uuid128 = BLE_UUID16(0x444c),
+                .att_flags = 5,
+                .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            }, {
+                .uuid128 = BLE_UUID16(0x444e),
+                .att_flags = 5,
+                .access_cb = ble_gatts_reg_test_misc_dummy_access,
+            }, {
+                0
+            } },
+        }, {
+            0
+        } },
+    }, {
+        0
+    } });
+}
+
+TEST_SUITE(ble_gatts_reg_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gatts_reg_test_svc_return();
+    ble_gatts_reg_test_chr_return();
+    ble_gatts_reg_test_dsc_return();
+
+    ble_gatts_reg_test_svc_cb();
+    ble_gatts_reg_test_chr_cb();
+    ble_gatts_reg_test_dsc_cb();
+}
+
+int
+ble_gatts_reg_test_all(void)
+{
+    ble_gatts_reg_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_hs_adv_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_adv_test.c b/net/nimble/host/test/src/ble_hs_adv_test.c
new file mode 100644
index 0000000..e99fd5b
--- /dev/null
+++ b/net/nimble/host/test/src/ble_hs_adv_test.c
@@ -0,0 +1,1486 @@
+/**
+ * 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 <stddef.h>
+#include <errno.h>
+#include <string.h>
+#include "testutil/testutil.h"
+#include "nimble/hci_common.h"
+#include "host/ble_hs_adv.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+
+#define BLE_ADV_TEST_DATA_OFF   4
+
+static void
+ble_hs_adv_test_misc_verify_tx_adv_data_hdr(uint8_t *cmd, int data_len)
+{
+    uint16_t opcode;
+
+    opcode = le16toh(cmd + 0);
+    TEST_ASSERT(BLE_HCI_OGF(opcode) == BLE_HCI_OGF_LE);
+    TEST_ASSERT(BLE_HCI_OCF(opcode) == BLE_HCI_OCF_LE_SET_ADV_DATA);
+
+    TEST_ASSERT(cmd[2] == BLE_HCI_SET_ADV_DATA_LEN);
+    TEST_ASSERT(cmd[3] == data_len);
+}
+
+static void
+ble_hs_adv_test_misc_verify_tx_rsp_data_hdr(uint8_t *cmd, int data_len)
+{
+    uint16_t opcode;
+
+    opcode = le16toh(cmd + 0);
+    TEST_ASSERT(BLE_HCI_OGF(opcode) == BLE_HCI_OGF_LE);
+    TEST_ASSERT(BLE_HCI_OCF(opcode) == BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA);
+
+    TEST_ASSERT(cmd[2] == BLE_HCI_SET_SCAN_RSP_DATA_LEN);
+    TEST_ASSERT(cmd[3] == data_len);
+}
+
+static void
+ble_hs_adv_test_misc_verify_tx_field(uint8_t *cmd, uint8_t type,
+                                     uint8_t val_len, void *val)
+{
+    TEST_ASSERT(cmd[0] == val_len + 1);
+    TEST_ASSERT(cmd[1] == type);
+    TEST_ASSERT(memcmp(cmd + 2, val, val_len) == 0);
+}
+
+struct ble_hs_adv_test_field {
+    uint8_t type;       /* 0 indicates end of array. */
+    uint8_t *val;
+    uint8_t val_len;
+};
+
+static int
+ble_hs_adv_test_misc_calc_data_len(struct ble_hs_adv_test_field *fields)
+{
+    struct ble_hs_adv_test_field *field;
+    int len;
+
+    len = 0;
+    if (fields != NULL) {
+        for (field = fields; field->type != 0; field++) {
+            len += 2 + field->val_len;
+        }
+    }
+
+    return len;
+}
+
+static void
+ble_hs_adv_test_misc_verify_tx_fields(uint8_t *cmd,
+                                      struct ble_hs_adv_test_field *fields)
+{
+    struct ble_hs_adv_test_field *field;
+
+    for (field = fields; field->type != 0; field++) {
+        ble_hs_adv_test_misc_verify_tx_field(cmd, field->type, field->val_len,
+                                             field->val);
+        cmd += 2 + field->val_len;
+    }
+}
+
+static void
+ble_hs_adv_test_misc_verify_tx_adv_data(struct ble_hs_adv_test_field *fields)
+{
+    int data_len;
+    uint8_t *cmd;
+
+    cmd = ble_hs_test_util_get_last_hci_tx();
+    TEST_ASSERT_FATAL(cmd != NULL);
+
+    data_len = ble_hs_adv_test_misc_calc_data_len(fields);
+    ble_hs_adv_test_misc_verify_tx_adv_data_hdr(cmd, data_len);
+    if (fields != NULL) {
+        ble_hs_adv_test_misc_verify_tx_fields(cmd + BLE_ADV_TEST_DATA_OFF,
+                                              fields);
+    }
+}
+
+static void
+ble_hs_adv_test_misc_verify_tx_rsp_data(struct ble_hs_adv_test_field *fields)
+{
+    int data_len;
+    uint8_t *cmd;
+
+    cmd = ble_hs_test_util_get_last_hci_tx();
+    TEST_ASSERT_FATAL(cmd != NULL);
+
+    data_len = ble_hs_adv_test_misc_calc_data_len(fields);
+    ble_hs_adv_test_misc_verify_tx_rsp_data_hdr(cmd, data_len);
+    if (fields != NULL) {
+        ble_hs_adv_test_misc_verify_tx_fields(cmd + BLE_ADV_TEST_DATA_OFF,
+                                              fields);
+    }
+}
+
+static void
+ble_hs_adv_test_misc_tx_and_verify_data(
+    uint8_t disc_mode,
+    struct ble_hs_adv_fields *adv_fields,
+    struct ble_hs_adv_test_field *test_adv_fields,
+    struct ble_hs_adv_fields *rsp_fields,
+    struct ble_hs_adv_test_field *test_rsp_fields)
+{
+    struct ble_gap_adv_params adv_params;
+    int rc;
+
+    ble_hs_test_util_init();
+
+    adv_params = ble_hs_test_util_adv_params;
+    adv_params.disc_mode = disc_mode;
+
+    rc = ble_hs_test_util_adv_set_fields(adv_fields, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    rc = ble_gap_adv_rsp_set_fields(rsp_fields);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, 0, NULL, &adv_params,
+                                    NULL, NULL, 0, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Discard the adv-enable command. */
+    ble_hs_test_util_get_last_hci_tx();
+
+    ble_hs_adv_test_misc_verify_tx_rsp_data(test_rsp_fields);
+    ble_hs_adv_test_misc_verify_tx_adv_data(test_adv_fields);
+
+    /* Ensure the same data gets sent on repeated advertise procedures. */
+    rc = ble_hs_test_util_adv_stop(0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, 0, NULL, &adv_params,
+                                    NULL, NULL, 0, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Discard the adv-enable command. */
+    ble_hs_test_util_get_last_hci_tx();
+
+    ble_hs_adv_test_misc_verify_tx_rsp_data(test_rsp_fields);
+    ble_hs_adv_test_misc_verify_tx_adv_data(test_adv_fields);
+}
+
+TEST_CASE(ble_hs_adv_test_case_flags)
+{
+    struct ble_hs_adv_fields adv_fields;
+    struct ble_hs_adv_fields rsp_fields;
+
+    memset(&adv_fields, 0, sizeof adv_fields);
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+
+    /* Default flags. */
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON,
+        &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /* Flags |= limited discoverable. */
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_LTD, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]) {
+                    BLE_HS_ADV_F_DISC_LTD | BLE_HS_ADV_F_BREDR_UNSUP
+                 },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /* Flags = general discoverable. */
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_GEN, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]) {
+                    BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP
+                 },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+}
+
+TEST_CASE(ble_hs_adv_test_case_user)
+{
+    struct ble_hs_adv_fields adv_fields;
+    struct ble_hs_adv_fields rsp_fields;
+
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+
+    /*** Complete 16-bit service class UUIDs. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
+    adv_fields.uuids16 = (uint16_t[]) { 0x0001, 0x1234, 0x54ab };
+    adv_fields.num_uuids16 = 3;
+    adv_fields.uuids16_is_complete = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_COMP_UUIDS16,
+                .val = (uint8_t[]) { 0x01, 0x00, 0x34, 0x12, 0xab, 0x54 },
+                .val_len = 6,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** Incomplete 16-bit service class UUIDs. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.uuids16 = (uint16_t[]) { 0x0001, 0x1234, 0x54ab };
+    adv_fields.num_uuids16 = 3;
+    adv_fields.uuids16_is_complete = 0;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS16,
+                .val = (uint8_t[]) { 0x01, 0x00, 0x34, 0x12, 0xab, 0x54 },
+                .val_len = 6,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** Complete 32-bit service class UUIDs. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.uuids32 = (uint32_t[]) { 0x12345678, 0xabacadae };
+    adv_fields.num_uuids32 = 2;
+    adv_fields.uuids32_is_complete = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_COMP_UUIDS32,
+                .val = (uint8_t[]) { 0x78,0x56,0x34,0x12,0xae,0xad,0xac,0xab },
+                .val_len = 8,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** Incomplete 32-bit service class UUIDs. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.uuids32 = (uint32_t[]) { 0x12345678, 0xabacadae };
+    adv_fields.num_uuids32 = 2;
+    adv_fields.uuids32_is_complete = 0;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS32,
+                .val = (uint8_t[]) { 0x78,0x56,0x34,0x12,0xae,0xad,0xac,0xab },
+                .val_len = 8,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** Complete 128-bit service class UUIDs. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.uuids128 = (uint8_t[]) {
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    };
+    adv_fields.num_uuids128 = 1;
+    adv_fields.uuids128_is_complete = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_COMP_UUIDS128,
+                .val = (uint8_t[]) {
+                    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+                    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+                },
+                .val_len = 16,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** Incomplete 128-bit service class UUIDs. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.uuids128 = (uint8_t[]) {
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    };
+    adv_fields.num_uuids128 = 1;
+    adv_fields.uuids128_is_complete = 0;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS128,
+                .val = (uint8_t[]) {
+                    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+                    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+                },
+                .val_len = 16,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** Complete name. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.name = (uint8_t *)"myname";
+    adv_fields.name_len = 6;
+    adv_fields.name_is_complete = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_COMP_NAME,
+                .val = (uint8_t*)"myname",
+                .val_len = 6,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** Incomplete name. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.name = (uint8_t *)"myname";
+    adv_fields.name_len = 6;
+    adv_fields.name_is_complete = 0;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_INCOMP_NAME,
+                .val = (uint8_t*)"myname",
+                .val_len = 6,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** Class of device. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.device_class = (uint8_t[]){ 1,2,3 };
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_DEVICE_CLASS,
+                .val = (uint8_t[]) { 1,2,3 },
+                .val_len = BLE_HS_ADV_DEVICE_CLASS_LEN,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** Slave interval range. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.slave_itvl_range = (uint8_t[]){ 1,2,3,4 };
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_SLAVE_ITVL_RANGE,
+                .val = (uint8_t[]) { 1,2,3,4 },
+                .val_len = BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** 0x16 - Service data - 16-bit UUID. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.svc_data_uuid16 = (uint8_t[]){ 1,2,3,4 };
+    adv_fields.svc_data_uuid16_len = 4;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID16,
+                .val = (uint8_t[]) { 1,2,3,4 },
+                .val_len = 4,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** 0x17 - Public target address. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.public_tgt_addr = (uint8_t[]){ 1,2,3,4,5,6, 6,5,4,3,2,1 };
+    adv_fields.num_public_tgt_addrs = 2;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_PUBLIC_TGT_ADDR,
+                .val = (uint8_t[]){ 1,2,3,4,5,6, 6,5,4,3,2,1 },
+                .val_len = 2 * BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** 0x19 - Appearance. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.appearance = 0x1234;
+    adv_fields.appearance_is_present = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_APPEARANCE,
+                .val = (uint8_t[]){ 0x34, 0x12 },
+                .val_len = BLE_HS_ADV_APPEARANCE_LEN,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** 0x1a - Advertising interval. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.adv_itvl = 0x1234;
+    adv_fields.adv_itvl_is_present = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_ADV_ITVL,
+                .val = (uint8_t[]){ 0x34, 0x12 },
+                .val_len = BLE_HS_ADV_ADV_ITVL_LEN,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** 0x1b - LE bluetooth device address. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.le_addr = (uint8_t[]){ 1,2,3,4,5,6,7 };
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_LE_ADDR,
+                .val = (uint8_t[]) { 1,2,3,4,5,6,7 },
+                .val_len = BLE_HS_ADV_LE_ADDR_LEN,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** 0x1c - LE role. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.le_role = BLE_HS_ADV_LE_ROLE_BOTH_PERIPH_PREF;
+    adv_fields.le_role_is_present = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_LE_ROLE,
+                .val = (uint8_t[]) { BLE_HS_ADV_LE_ROLE_BOTH_PERIPH_PREF },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** 0x20 - Service data - 32-bit UUID. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.svc_data_uuid32 = (uint8_t[]){ 1,2,3,4,5 };
+    adv_fields.svc_data_uuid32_len = 5;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID32,
+                .val = (uint8_t[]) { 1,2,3,4,5 },
+                .val_len = 5,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** 0x21 - Service data - 128-bit UUID. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.svc_data_uuid128 =
+        (uint8_t[]){ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 };
+    adv_fields.svc_data_uuid128_len = 18;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID128,
+                .val = (uint8_t[]){ 1,2,3,4,5,6,7,8,9,10,
+                                    11,12,13,14,15,16,17,18 },
+                .val_len = 18,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** 0x24 - URI. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.uri = (uint8_t[]){ 1,2,3,4 };
+    adv_fields.uri_len = 4;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_URI,
+                .val = (uint8_t[]) { 1,2,3,4 },
+                .val_len = 4,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** 0xff - Manufacturer specific data. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    adv_fields.mfg_data = (uint8_t[]){ 1,2,3,4 };
+    adv_fields.mfg_data_len = 4;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_MFG_DATA,
+                .val = (uint8_t[]) { 1,2,3,4 },
+                .val_len = 4,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+}
+
+TEST_CASE(ble_hs_adv_test_case_user_rsp)
+{
+    struct ble_hs_adv_fields rsp_fields;
+    struct ble_hs_adv_fields adv_fields;
+
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.flags_is_present = 1;
+    adv_fields.tx_pwr_lvl_is_present = 1;
+
+    /*** Complete 16-bit service class UUIDs. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.uuids16 = (uint16_t[]) { 0x0001, 0x1234, 0x54ab };
+    rsp_fields.num_uuids16 = 3;
+    rsp_fields.uuids16_is_complete = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_COMP_UUIDS16,
+                .val = (uint8_t[]) { 0x01, 0x00, 0x34, 0x12, 0xab, 0x54 },
+                .val_len = 6,
+            },
+            { 0 },
+        });
+
+    /*** Incomplete 16-bit service class UUIDs. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.uuids16 = (uint16_t[]) { 0x0001, 0x1234, 0x54ab };
+    rsp_fields.num_uuids16 = 3;
+    rsp_fields.uuids16_is_complete = 0;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS16,
+                .val = (uint8_t[]) { 0x01, 0x00, 0x34, 0x12, 0xab, 0x54 },
+                .val_len = 6,
+            },
+            { 0 },
+        });
+
+    /*** Complete 32-bit service class UUIDs. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.uuids32 = (uint32_t[]) { 0x12345678, 0xabacadae };
+    rsp_fields.num_uuids32 = 2;
+    rsp_fields.uuids32_is_complete = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_COMP_UUIDS32,
+                .val = (uint8_t[]) { 0x78,0x56,0x34,0x12,0xae,0xad,0xac,0xab },
+                .val_len = 8,
+            },
+            { 0 },
+        });
+
+    /*** Incomplete 32-bit service class UUIDs. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.uuids32 = (uint32_t[]) { 0x12345678, 0xabacadae };
+    rsp_fields.num_uuids32 = 2;
+    rsp_fields.uuids32_is_complete = 0;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS32,
+                .val = (uint8_t[]) { 0x78,0x56,0x34,0x12,0xae,0xad,0xac,0xab },
+                .val_len = 8,
+            },
+            { 0 },
+        });
+
+    /*** Complete 128-bit service class UUIDs. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.uuids128 = (uint8_t[]) {
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    };
+    rsp_fields.num_uuids128 = 1;
+    rsp_fields.uuids128_is_complete = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_COMP_UUIDS128,
+                .val = (uint8_t[]) {
+                    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+                    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+                },
+                .val_len = 16,
+            },
+            { 0 },
+        });
+
+    /*** Incomplete 128-bit service class UUIDs. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.uuids128 = (uint8_t[]) {
+        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+    };
+    rsp_fields.num_uuids128 = 1;
+    rsp_fields.uuids128_is_complete = 0;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS128,
+                .val = (uint8_t[]) {
+                    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
+                    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
+                },
+                .val_len = 16,
+            },
+            { 0 },
+        });
+
+    /*** Complete name. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.name = (uint8_t *)"myname";
+    rsp_fields.name_len = 6;
+    rsp_fields.name_is_complete = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_COMP_NAME,
+                .val = (uint8_t*)"myname",
+                .val_len = 6,
+            },
+            { 0 },
+        });
+
+    /*** Incomplete name. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.name = (uint8_t *)"myname";
+    rsp_fields.name_len = 6;
+    rsp_fields.name_is_complete = 0;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_INCOMP_NAME,
+                .val = (uint8_t*)"myname",
+                .val_len = 6,
+            },
+            { 0 },
+        });
+
+    /*** Class of device. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.device_class = (uint8_t[]){ 1,2,3 };
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_DEVICE_CLASS,
+                .val = (uint8_t[]) { 1,2,3 },
+                .val_len = BLE_HS_ADV_DEVICE_CLASS_LEN,
+            },
+            { 0 },
+        });
+
+    /*** Slave interval range. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.slave_itvl_range = (uint8_t[]){ 1,2,3,4 };
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_SLAVE_ITVL_RANGE,
+                .val = (uint8_t[]) { 1,2,3,4 },
+                .val_len = BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN,
+            },
+            { 0 },
+        });
+
+    /*** 0x16 - Service data - 16-bit UUID. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.svc_data_uuid16 = (uint8_t[]){ 1,2,3,4 };
+    rsp_fields.svc_data_uuid16_len = 4;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID16,
+                .val = (uint8_t[]) { 1,2,3,4 },
+                .val_len = 4,
+            },
+            { 0 },
+        });
+
+    /*** 0x17 - Public target address. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.public_tgt_addr = (uint8_t[]){ 1,2,3,4,5,6, 6,5,4,3,2,1 };
+    rsp_fields.num_public_tgt_addrs = 2;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_PUBLIC_TGT_ADDR,
+                .val = (uint8_t[]){ 1,2,3,4,5,6, 6,5,4,3,2,1 },
+                .val_len = 2 * BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN,
+            },
+            { 0 },
+        });
+
+    /*** 0x19 - Appearance. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.appearance = 0x1234;
+    rsp_fields.appearance_is_present = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_APPEARANCE,
+                .val = (uint8_t[]){ 0x34, 0x12 },
+                .val_len = BLE_HS_ADV_APPEARANCE_LEN,
+            },
+            { 0 },
+        });
+
+    /*** 0x1a - Advertising interval. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.adv_itvl = 0x1234;
+    rsp_fields.adv_itvl_is_present = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_ADV_ITVL,
+                .val = (uint8_t[]){ 0x34, 0x12 },
+                .val_len = BLE_HS_ADV_ADV_ITVL_LEN,
+            },
+            { 0 },
+        });
+
+    /*** 0x1b - LE bluetooth device address. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.le_addr = (uint8_t[]){ 1,2,3,4,5,6,7 };
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_LE_ADDR,
+                .val = (uint8_t[]) { 1,2,3,4,5,6,7 },
+                .val_len = BLE_HS_ADV_LE_ADDR_LEN,
+            },
+            { 0 },
+        });
+
+    /*** 0x1c - LE role. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.le_role = BLE_HS_ADV_LE_ROLE_BOTH_PERIPH_PREF;
+    rsp_fields.le_role_is_present = 1;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_LE_ROLE,
+                .val = (uint8_t[]) { BLE_HS_ADV_LE_ROLE_BOTH_PERIPH_PREF },
+                .val_len = 1,
+            },
+            { 0 },
+        });
+
+    /*** 0x20 - Service data - 32-bit UUID. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.svc_data_uuid32 = (uint8_t[]){ 1,2,3,4,5 };
+    rsp_fields.svc_data_uuid32_len = 5;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID32,
+                .val = (uint8_t[]) { 1,2,3,4,5 },
+                .val_len = 5,
+            },
+            { 0 },
+        });
+
+    /*** 0x21 - Service data - 128-bit UUID. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.svc_data_uuid128 =
+        (uint8_t[]){ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 };
+    rsp_fields.svc_data_uuid128_len = 18;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID128,
+                .val = (uint8_t[]){ 1,2,3,4,5,6,7,8,9,10,
+                                    11,12,13,14,15,16,17,18 },
+                .val_len = 18,
+            },
+            { 0 },
+        });
+
+    /*** 0x24 - URI. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.uri = (uint8_t[]){ 1,2,3,4 };
+    rsp_fields.uri_len = 4;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_URI,
+                .val = (uint8_t[]) { 1,2,3,4 },
+                .val_len = 4,
+            },
+            { 0 },
+        });
+
+    /*** 0xff - Manufacturer specific data. */
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+    rsp_fields.mfg_data = (uint8_t[]){ 1,2,3,4 };
+    rsp_fields.mfg_data_len = 4;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
+                .val = (uint8_t[]){ 0 },
+                .val_len = 1,
+            },
+            {
+                .type = BLE_HS_ADV_TYPE_FLAGS,
+                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
+                .val_len = 1,
+            },
+            { 0 },
+        },
+        &rsp_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_MFG_DATA,
+                .val = (uint8_t[]) { 1,2,3,4 },
+                .val_len = 4,
+            },
+            { 0 },
+        });
+}
+
+TEST_CASE(ble_hs_adv_test_case_user_full_payload)
+{
+    /* Intentionally allocate an extra byte. */
+    static const uint8_t mfg_data[30] = {
+        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+        0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
+        0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
+        0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
+    };
+
+    struct ble_hs_adv_fields adv_fields;
+    struct ble_hs_adv_fields rsp_fields;
+    int rc;
+
+    ble_hs_test_util_init();
+
+    memset(&rsp_fields, 0, sizeof rsp_fields);
+
+    /***
+     * An advertisement should allow 31 bytes of user data.  Each field has a
+     * two-byte header, leaving 29 bytes of payload.
+     */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.mfg_data = (void *)mfg_data;
+    adv_fields.mfg_data_len = 29;
+
+    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
+        (struct ble_hs_adv_test_field[]) {
+            {
+                .type = BLE_HS_ADV_TYPE_MFG_DATA,
+                .val = (void *)mfg_data,
+                .val_len = 29,
+            },
+            { 0 },
+        }, &rsp_fields, NULL);
+
+    /*** Fail with 30 bytes. */
+    rc = ble_hs_test_util_adv_stop(0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    adv_fields.mfg_data_len = 30;
+    rc = ble_gap_adv_set_fields(&adv_fields);
+    TEST_ASSERT(rc == BLE_HS_EMSGSIZE);
+}
+
+TEST_SUITE(ble_hs_adv_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_hs_adv_test_case_flags();
+    ble_hs_adv_test_case_user();
+    ble_hs_adv_test_case_user_rsp();
+    ble_hs_adv_test_case_user_full_payload();
+}
+
+int
+ble_hs_adv_test_all(void)
+{
+    ble_hs_adv_test_suite();
+
+    return tu_any_failed;
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_hs_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_conn_test.c b/net/nimble/host/test/src/ble_hs_conn_test.c
new file mode 100644
index 0000000..c957446
--- /dev/null
+++ b/net/nimble/host/test/src/ble_hs_conn_test.c
@@ -0,0 +1,219 @@
+/**
+ * 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 <stddef.h>
+#include <errno.h>
+#include <string.h>
+#include "testutil/testutil.h"
+#include "nimble/hci_common.h"
+#include "host/ble_hs_adv.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+
+static int
+ble_hs_conn_test_util_any()
+{
+    struct ble_hs_conn *conn;
+
+    ble_hs_lock();
+    conn = ble_hs_conn_first();
+    ble_hs_unlock();
+
+    return conn != NULL;
+}
+
+TEST_CASE(ble_hs_conn_test_direct_connect_success)
+{
+    struct hci_le_conn_complete evt;
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    uint8_t addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int rc;
+
+    ble_hs_test_util_init();
+
+    /* Ensure no current or pending connections. */
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_hs_conn_test_util_any());
+
+    /* Initiate connection. */
+    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
+                                        BLE_ADDR_TYPE_PUBLIC,
+                                        addr, 0, NULL, NULL, NULL, 0);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(ble_gap_master_in_progress());
+
+    /* Receive successful connection complete event. */
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_SUCCESS;
+    evt.connection_handle = 2;
+    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
+    memcpy(evt.peer_addr, addr, 6);
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    ble_hs_lock();
+
+    conn = ble_hs_conn_first();
+    TEST_ASSERT_FATAL(conn != NULL);
+    TEST_ASSERT(conn->bhc_handle == 2);
+    TEST_ASSERT(memcmp(conn->bhc_peer_addr, addr, 6) == 0);
+
+    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+    TEST_ASSERT_FATAL(chan != NULL);
+    TEST_ASSERT(chan->blc_my_mtu == BLE_ATT_MTU_PREFERRED_DFLT);
+    TEST_ASSERT(chan->blc_peer_mtu == 0);
+    TEST_ASSERT(chan->blc_default_mtu == BLE_ATT_MTU_DFLT);
+
+    ble_hs_unlock();
+}
+
+TEST_CASE(ble_hs_conn_test_direct_connectable_success)
+{
+    struct hci_le_conn_complete evt;
+    struct ble_gap_adv_params adv_params;
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    uint8_t addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int rc;
+
+    ble_hs_test_util_init();
+
+    /* Ensure no current or pending connections. */
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_gap_adv_active());
+    TEST_ASSERT(!ble_hs_conn_test_util_any());
+
+    /* Initiate advertising. */
+    adv_params = ble_hs_test_util_adv_params;
+    adv_params.conn_mode = BLE_GAP_CONN_MODE_DIR;
+    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                    addr, &adv_params, NULL, NULL, 0, 0);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(ble_gap_adv_active());
+
+    /* Receive successful connection complete event. */
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_SUCCESS;
+    evt.connection_handle = 2;
+    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_SLAVE;
+    memcpy(evt.peer_addr, addr, 6);
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_gap_adv_active());
+
+    ble_hs_lock();
+
+    conn = ble_hs_conn_first();
+    TEST_ASSERT_FATAL(conn != NULL);
+    TEST_ASSERT(conn->bhc_handle == 2);
+    TEST_ASSERT(memcmp(conn->bhc_peer_addr, addr, 6) == 0);
+
+    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+    TEST_ASSERT_FATAL(chan != NULL);
+    TEST_ASSERT(chan->blc_my_mtu == BLE_ATT_MTU_PREFERRED_DFLT);
+    TEST_ASSERT(chan->blc_peer_mtu == 0);
+    TEST_ASSERT(chan->blc_default_mtu == BLE_ATT_MTU_DFLT);
+
+    ble_hs_unlock();
+}
+
+TEST_CASE(ble_hs_conn_test_undirect_connectable_success)
+{
+    struct ble_hs_adv_fields adv_fields;
+    struct hci_le_conn_complete evt;
+    struct ble_gap_adv_params adv_params;
+    struct ble_l2cap_chan *chan;
+    struct ble_hs_conn *conn;
+    uint8_t addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int rc;
+
+    ble_hs_test_util_init();
+
+    /* Ensure no current or pending connections. */
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_gap_adv_active());
+    TEST_ASSERT(!ble_hs_conn_test_util_any());
+
+    /* Initiate advertising. */
+    memset(&adv_fields, 0, sizeof adv_fields);
+    adv_fields.tx_pwr_lvl_is_present = 1;
+    rc = ble_gap_adv_set_fields(&adv_fields);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    adv_params = ble_hs_test_util_adv_params;
+    adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
+    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                    addr, &adv_params, NULL, NULL, 0, 0);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(ble_gap_adv_active());
+
+    /* Receive successful connection complete event. */
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_SUCCESS;
+    evt.connection_handle = 2;
+    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_SLAVE;
+    memcpy(evt.peer_addr, addr, 6);
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_gap_adv_active());
+
+    ble_hs_lock();
+
+    conn = ble_hs_conn_first();
+    TEST_ASSERT_FATAL(conn != NULL);
+    TEST_ASSERT(conn->bhc_handle == 2);
+    TEST_ASSERT(memcmp(conn->bhc_peer_addr, addr, 6) == 0);
+
+    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
+    TEST_ASSERT_FATAL(chan != NULL);
+    TEST_ASSERT(chan->blc_my_mtu == BLE_ATT_MTU_PREFERRED_DFLT);
+    TEST_ASSERT(chan->blc_peer_mtu == 0);
+    TEST_ASSERT(chan->blc_default_mtu == BLE_ATT_MTU_DFLT);
+
+    ble_hs_unlock();
+}
+
+TEST_SUITE(conn_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_hs_conn_test_direct_connect_success();
+    ble_hs_conn_test_direct_connectable_success();
+    ble_hs_conn_test_undirect_connectable_success();
+}
+
+int
+ble_hs_conn_test_all(void)
+{
+    conn_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_hs_hci_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_hci_test.c b/net/nimble/host/test/src/ble_hs_hci_test.c
new file mode 100644
index 0000000..21184b8
--- /dev/null
+++ b/net/nimble/host/test/src/ble_hs_hci_test.c
@@ -0,0 +1,99 @@
+/**
+ * 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 <stddef.h>
+#include <errno.h>
+#include <string.h>
+#include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
+#include "host/ble_hs_test.h"
+#include "testutil/testutil.h"
+#include "ble_hs_test_util.h"
+
+TEST_CASE(ble_hs_hci_test_event_bad)
+{
+    uint8_t *buf;
+    int rc;
+
+    /*** Invalid event code. */
+    buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
+    TEST_ASSERT_FATAL(buf != NULL);
+
+    buf[0] = 0xff;
+    buf[1] = 0;
+    rc = ble_hs_hci_evt_process(buf);
+    TEST_ASSERT(rc == BLE_HS_ENOTSUP);
+}
+
+TEST_CASE(ble_hs_hci_test_rssi)
+{
+    uint8_t params[BLE_HCI_READ_RSSI_ACK_PARAM_LEN];
+    uint16_t opcode;
+    int8_t rssi;
+    int rc;
+
+    opcode = ble_hs_hci_util_opcode_join(BLE_HCI_OGF_STATUS_PARAMS,
+                                  BLE_HCI_OCF_RD_RSSI);
+
+    /*** Success. */
+    /* Connection handle. */
+    htole16(params + 0, 1);
+
+    /* RSSI. */
+    params[2] = -8;
+
+    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params);
+
+    rc = ble_hs_hci_util_read_rssi(1, &rssi);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(rssi == -8);
+
+    /*** Failure: incorrect connection handle. */
+    htole16(params + 0, 99);
+
+    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params);
+
+    rc = ble_hs_hci_util_read_rssi(1, &rssi);
+    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
+
+    /*** Failure: params too short. */
+    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params - 1);
+    rc = ble_hs_hci_util_read_rssi(1, &rssi);
+    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
+
+    /*** Failure: params too long. */
+    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params + 1);
+    rc = ble_hs_hci_util_read_rssi(1, &rssi);
+    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
+}
+
+TEST_SUITE(ble_hs_hci_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_hs_hci_test_event_bad();
+    ble_hs_hci_test_rssi();
+}
+
+int
+ble_hs_hci_test_all(void)
+{
+    ble_hs_hci_suite();
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_hs_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_hs_test.c b/net/nimble/host/test/src/ble_hs_test.c
new file mode 100644
index 0000000..e1f4dad
--- /dev/null
+++ b/net/nimble/host/test/src/ble_hs_test.c
@@ -0,0 +1,61 @@
+/**
+ * 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 "syscfg/syscfg.h"
+#include "os/os.h"
+#include "nimble/hci_common.h"
+#include "host/ble_hs_test.h"
+#include "testutil/testutil.h"
+#include "ble_hs_test_util.h"
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_parse_args(argc, argv);
+
+    tu_init();
+
+    ble_att_clt_test_all();
+    ble_att_svr_test_all();
+    ble_gap_test_all();
+    ble_gatt_conn_test_all();
+    ble_gatt_disc_c_test_all();
+    ble_gatt_disc_d_test_all();
+    ble_gatt_disc_s_test_all();
+    ble_gatt_find_s_test_all();
+    ble_gatt_read_test_all();
+    ble_gatt_write_test_all();
+    ble_gatts_notify_test_all();
+    ble_gatts_read_test_suite();
+    ble_gatts_reg_test_all();
+    ble_hs_hci_test_all();
+    ble_hs_adv_test_all();
+    ble_hs_conn_test_all();
+    ble_l2cap_test_all();
+    ble_os_test_all();
+    ble_sm_test_all();
+    ble_uuid_test_all();
+
+    return tu_any_failed;
+}
+
+#endif


[31/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bootutil/src/test/boot_test.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/test/boot_test.c b/libs/bootutil/src/test/boot_test.c
deleted file mode 100644
index e78d2e4..0000000
--- a/libs/bootutil/src/test/boot_test.c
+++ /dev/null
@@ -1,1169 +0,0 @@
-/**
- * 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 <assert.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <inttypes.h>
-#include "testutil/testutil.h"
-#include "hal/hal_flash.h"
-#include "hal/flash_map.h"
-#include "fs/fs.h"
-#include "nffs/nffs.h"
-#include <config/config_file.h>
-#include "bootutil/image.h"
-#include "bootutil/loader.h"
-#include "bootutil/bootutil_misc.h"
-#include "../src/bootutil_priv.h"
-
-#include "mbedtls/sha256.h"
-
-#define BOOT_TEST_HEADER_SIZE       0x200
-
-/** Internal flash layout. */
-static struct flash_area boot_test_area_descs[] = {
-    [0] = { .fa_off = 0x00020000, .fa_size = 128 * 1024 },
-    [1] = { .fa_off = 0x00040000, .fa_size = 128 * 1024 },
-    [2] = { .fa_off = 0x00060000, .fa_size = 128 * 1024 },
-    [3] = { .fa_off = 0x00080000, .fa_size = 128 * 1024 },
-    [4] = { .fa_off = 0x000a0000, .fa_size = 128 * 1024 },
-    [5] = { .fa_off = 0x000c0000, .fa_size = 128 * 1024 },
-    [6] = { .fa_off = 0x000e0000, .fa_size = 128 * 1024 },
-};
-
-static const struct flash_area boot_test_format_descs[] = {
-    [0] = { .fa_off = 0x00004000, .fa_size = 16 * 1024 },
-    [1] = { .fa_off = 0x00008000, .fa_size = 16 * 1024 },
-    [2] = { .fa_off = 0x0000c000, .fa_size = 16 * 1024 },
-    [3] = { .fa_off = 0, .fa_size = 0 },
-};
-
-/** Areas representing the beginning of image slots. */
-static uint8_t boot_test_slot_areas[] = {
-    0, 3,
-};
-
-/** Flash offsets of the two image slots. */
-static struct {
-    uint8_t flash_id;
-    uint32_t address;
-} boot_test_img_addrs[] = {
-    { 0, 0x20000 },
-    { 0, 0x80000 },
-};
-
-#define BOOT_TEST_AREA_IDX_SCRATCH 6
-
-#define MY_CONF_PATH "/cfg/run"
-
-static struct conf_file my_conf = {
-    .cf_name = MY_CONF_PATH
-};
-
-static uint8_t
-boot_test_util_byte_at(int img_msb, uint32_t image_offset)
-{
-    uint32_t u32;
-    uint8_t *u8p;
-
-    TEST_ASSERT(image_offset < 0x01000000);
-    u32 = image_offset + (img_msb << 24);
-    u8p = (void *)&u32;
-    return u8p[image_offset % 4];
-}
-
-static void
-boot_test_util_init_flash(void)
-{
-    const struct flash_area *area_desc;
-    int rc;
-    struct nffs_area_desc nffs_descs[32];
-    int cnt;
-
-    rc = hal_flash_init();
-    TEST_ASSERT(rc == 0);
-
-    for (area_desc = boot_test_area_descs;
-         area_desc->fa_size != 0;
-         area_desc++) {
-
-        rc = flash_area_erase(area_desc, 0, area_desc->fa_size);
-        TEST_ASSERT(rc == 0);
-    }
-    cnt = 32;
-
-    rc = flash_area_to_nffs_desc(FLASH_AREA_NFFS, &cnt, nffs_descs);
-    TEST_ASSERT(rc == 0);
-
-    rc = nffs_init();
-    TEST_ASSERT(rc == 0);
-    rc = nffs_format(nffs_descs);
-    TEST_ASSERT(rc == 0);
-
-    fs_mkdir("/cfg");
-}
-
-static void
-boot_test_util_copy_area(int from_area_idx, int to_area_idx)
-{
-    const struct flash_area *from_area_desc;
-    const struct flash_area *to_area_desc;
-    void *buf;
-    int rc;
-
-    from_area_desc = boot_test_area_descs + from_area_idx;
-    to_area_desc = boot_test_area_descs + to_area_idx;
-
-    TEST_ASSERT(from_area_desc->fa_size == to_area_desc->fa_size);
-
-    buf = malloc(from_area_desc->fa_size);
-    TEST_ASSERT(buf != NULL);
-
-    rc = flash_area_read(from_area_desc, 0, buf,
-                         from_area_desc->fa_size);
-    TEST_ASSERT(rc == 0);
-
-    rc = flash_area_erase(to_area_desc,
-                          0,
-                          to_area_desc->fa_size);
-    TEST_ASSERT(rc == 0);
-
-    rc = flash_area_write(to_area_desc, 0, buf,
-                          to_area_desc->fa_size);
-    TEST_ASSERT(rc == 0);
-
-    free(buf);
-}
-
-static void
-boot_test_util_swap_areas(int area_idx1, int area_idx2)
-{
-    const struct flash_area *area_desc1;
-    const struct flash_area *area_desc2;
-    void *buf1;
-    void *buf2;
-    int rc;
-
-    area_desc1 = boot_test_area_descs + area_idx1;
-    area_desc2 = boot_test_area_descs + area_idx2;
-
-    TEST_ASSERT(area_desc1->fa_size == area_desc2->fa_size);
-
-    buf1 = malloc(area_desc1->fa_size);
-    TEST_ASSERT(buf1 != NULL);
-
-    buf2 = malloc(area_desc2->fa_size);
-    TEST_ASSERT(buf2 != NULL);
-
-    rc = flash_area_read(area_desc1, 0, buf1, area_desc1->fa_size);
-    TEST_ASSERT(rc == 0);
-
-    rc = flash_area_read(area_desc2, 0, buf2, area_desc2->fa_size);
-    TEST_ASSERT(rc == 0);
-
-    rc = flash_area_erase(area_desc1, 0, area_desc1->fa_size);
-    TEST_ASSERT(rc == 0);
-
-    rc = flash_area_erase(area_desc2, 0, area_desc2->fa_size);
-    TEST_ASSERT(rc == 0);
-
-    rc = flash_area_write(area_desc1, 0, buf2, area_desc1->fa_size);
-    TEST_ASSERT(rc == 0);
-
-    rc = flash_area_write(area_desc2, 0, buf1, area_desc2->fa_size);
-    TEST_ASSERT(rc == 0);
-
-    free(buf1);
-    free(buf2);
-}
-
-static void
-boot_test_util_write_image(const struct image_header *hdr, int slot)
-{
-    uint32_t image_off;
-    uint32_t off;
-    uint8_t flash_id;
-    uint8_t buf[256];
-    int chunk_sz;
-    int rc;
-    int i;
-
-    TEST_ASSERT(slot == 0 || slot == 1);
-
-    flash_id = boot_test_img_addrs[slot].flash_id;
-    off = boot_test_img_addrs[slot].address;
-
-    rc = hal_flash_write(flash_id, off, hdr, sizeof *hdr);
-    TEST_ASSERT(rc == 0);
-
-    off += hdr->ih_hdr_size;
-
-    image_off = 0;
-    while (image_off < hdr->ih_img_size) {
-        if (hdr->ih_img_size - image_off > sizeof buf) {
-            chunk_sz = sizeof buf;
-        } else {
-            chunk_sz = hdr->ih_img_size - image_off;
-        }
-
-        for (i = 0; i < chunk_sz; i++) {
-            buf[i] = boot_test_util_byte_at(slot, image_off + i);
-        }
-
-        rc = hal_flash_write(flash_id, off + image_off, buf, chunk_sz);
-        TEST_ASSERT(rc == 0);
-
-        image_off += chunk_sz;
-    }
-}
-
-static void
-boot_test_util_write_hash(const struct image_header *hdr, int slot)
-{
-    uint8_t tmpdata[1024];
-    uint8_t hash[32];
-    int rc;
-    uint32_t off;
-    uint32_t blk_sz;
-    uint32_t sz;
-    mbedtls_sha256_context ctx;
-    uint8_t flash_id;
-    uint32_t addr;
-    struct image_tlv tlv;
-
-    mbedtls_sha256_init(&ctx);
-    mbedtls_sha256_starts(&ctx, 0);
-
-    flash_id = boot_test_img_addrs[slot].flash_id;
-    addr = boot_test_img_addrs[slot].address;
-
-    sz = hdr->ih_hdr_size + hdr->ih_img_size;
-    for (off = 0; off < sz; off += blk_sz) {
-        blk_sz = sz - off;
-        if (blk_sz > sizeof(tmpdata)) {
-            blk_sz = sizeof(tmpdata);
-        }
-        rc = hal_flash_read(flash_id, addr + off, tmpdata, blk_sz);
-        TEST_ASSERT(rc == 0);
-        mbedtls_sha256_update(&ctx, tmpdata, blk_sz);
-    }
-    mbedtls_sha256_finish(&ctx, hash);
-
-    tlv.it_type = IMAGE_TLV_SHA256;
-    tlv._pad = 0;
-    tlv.it_len = sizeof(hash);
-
-    rc = hal_flash_write(flash_id, addr + off, &tlv, sizeof(tlv));
-    TEST_ASSERT(rc == 0);
-    off += sizeof(tlv);
-    rc = hal_flash_write(flash_id, addr + off, hash, sizeof(hash));
-    TEST_ASSERT(rc == 0);
-}
-
-static void
-boot_test_util_verify_area(const struct flash_area *area_desc,
-                           const struct image_header *hdr,
-                           uint32_t image_addr, int img_msb)
-{
-    struct image_header temp_hdr;
-    uint32_t area_end;
-    uint32_t img_size;
-    uint32_t img_off;
-    uint32_t img_end;
-    uint32_t addr;
-    uint8_t buf[256];
-    int rem_area;
-    int past_image;
-    int chunk_sz;
-    int rem_img;
-    int rc;
-    int i;
-
-    addr = area_desc->fa_off;
-
-    if (hdr != NULL) {
-        img_size = hdr->ih_img_size;
-
-        if (addr == image_addr) {
-            rc = hal_flash_read(area_desc->fa_flash_id, image_addr,
-                                &temp_hdr, sizeof temp_hdr);
-            TEST_ASSERT(rc == 0);
-            TEST_ASSERT(memcmp(&temp_hdr, hdr, sizeof *hdr) == 0);
-
-            addr += hdr->ih_hdr_size;
-        }
-    } else {
-        img_size = 0;
-    }
-
-    area_end = area_desc->fa_off + area_desc->fa_size;
-    img_end = image_addr + img_size;
-    past_image = addr >= img_end;
-
-    while (addr < area_end) {
-        rem_area = area_end - addr;
-        rem_img = img_end - addr;
-
-        if (hdr != NULL) {
-            img_off = addr - image_addr - hdr->ih_hdr_size;
-        } else {
-            img_off = 0;
-        }
-
-        if (rem_area > sizeof buf) {
-            chunk_sz = sizeof buf;
-        } else {
-            chunk_sz = rem_area;
-        }
-
-        rc = hal_flash_read(area_desc->fa_flash_id, addr, buf, chunk_sz);
-        TEST_ASSERT(rc == 0);
-
-        for (i = 0; i < chunk_sz; i++) {
-            if (rem_img > 0) {
-                TEST_ASSERT(buf[i] == boot_test_util_byte_at(img_msb,
-                                                        img_off + i));
-            } else if (past_image) {
-                TEST_ASSERT(buf[i] == 0xff);
-            }
-        }
-
-        addr += chunk_sz;
-    }
-}
-
-static void
-boot_test_util_verify_status_clear(void)
-{
-    struct fs_file *file;
-    int rc;
-    int empty = 1;
-    char *needle = "boot/status=";
-    int nlen = strlen(needle);
-    uint32_t len, hlen;
-    char *haystack, *ptr;
-
-    rc = fs_open(MY_CONF_PATH, FS_ACCESS_READ, &file);
-    if (rc != 0) {
-        return;
-    }
-    rc = fs_filelen(file, &len);
-    TEST_ASSERT(rc == 0);
-
-    haystack = malloc(len + 1);
-    TEST_ASSERT(haystack);
-
-    rc = fs_read(file, len, haystack, &hlen);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(hlen == len);
-    haystack[len] = '\0';
-
-    fs_close(file);
-
-    ptr = haystack;
-    while ((ptr = strstr(ptr, needle))) {
-        if (ptr[nlen] == '\n') {
-            empty = 1;
-        } else {
-            empty = 0;
-        }
-        ptr += nlen;
-    }
-    TEST_ASSERT(empty == 1);
-    free(haystack);
-
-    rc = fs_open(BOOT_PATH_STATUS, FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == FS_ENOENT);
-}
-
-static void
-boot_test_util_verify_flash(const struct image_header *hdr0, int orig_slot_0,
-                            const struct image_header *hdr1, int orig_slot_1)
-{
-    const struct flash_area *area_desc;
-    int area_idx;
-
-    area_idx = 0;
-
-    while (1) {
-        area_desc = boot_test_area_descs + area_idx;
-        if (area_desc->fa_off == boot_test_img_addrs[1].address &&
-            area_desc->fa_flash_id == boot_test_img_addrs[1].flash_id) {
-            break;
-        }
-
-        boot_test_util_verify_area(area_desc, hdr0,
-                                   boot_test_img_addrs[0].address, orig_slot_0);
-        area_idx++;
-    }
-
-    while (1) {
-        if (area_idx == BOOT_TEST_AREA_IDX_SCRATCH) {
-            break;
-        }
-
-        area_desc = boot_test_area_descs + area_idx;
-        boot_test_util_verify_area(area_desc, hdr1,
-                                   boot_test_img_addrs[1].address, orig_slot_1);
-        area_idx++;
-    }
-}
-
-TEST_CASE(boot_test_setup)
-{
-    int rc;
-
-    rc = conf_file_src(&my_conf);
-    assert(rc == 0);
-    rc = conf_file_dst(&my_conf);
-    assert(rc == 0);
-
-    bootutil_cfg_register();
-}
-
-TEST_CASE(boot_test_nv_ns_10)
-{
-    struct boot_rsp rsp;
-    int rc;
-
-    struct image_header hdr = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 12 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 0, 2, 3, 4 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr, 0);
-    boot_test_util_write_hash(&hdr, 0);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_nv_ns_01)
-{
-    struct boot_rsp rsp;
-    int rc;
-
-
-    struct image_header hdr = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 10 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 1, 2, 3, 432 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr, 1);
-    boot_test_util_write_hash(&hdr, 1);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr, 1, NULL, 0xff);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_nv_ns_11)
-{
-    struct boot_rsp rsp;
-    int rc;
-
-    struct image_header hdr0 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 5 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 0, 5, 21, 432 },
-    };
-
-    struct image_header hdr1 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 32 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 1, 2, 3, 432 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr0, 0);
-    boot_test_util_write_hash(&hdr0, 0);
-    boot_test_util_write_image(&hdr1, 1);
-    boot_test_util_write_hash(&hdr1, 1);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr0, 0, &hdr1, 1);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_vm_ns_10)
-{
-    struct boot_rsp rsp;
-    int rc;
-
-
-    struct image_header hdr = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 12 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 0, 2, 3, 4 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr, 0);
-    boot_test_util_write_hash(&hdr, 0);
-
-    rc = boot_vect_write_main(&hdr.ih_ver);
-    TEST_ASSERT(rc == 0);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_vm_ns_01)
-{
-    struct boot_rsp rsp;
-    int rc;
-
-    struct image_header hdr = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 10 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 1, 2, 3, 432 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr, 1);
-    boot_test_util_write_hash(&hdr, 1);
-
-    rc = boot_vect_write_main(&hdr.ih_ver);
-    TEST_ASSERT(rc == 0);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr, 1, NULL, 0xff);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_vm_ns_11_a)
-{
-    struct boot_rsp rsp;
-    int rc;
-
-    struct image_header hdr0 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 5 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 0, 5, 21, 432 },
-    };
-
-    struct image_header hdr1 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 32 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 1, 2, 3, 432 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr0, 0);
-    boot_test_util_write_hash(&hdr0, 0);
-    boot_test_util_write_image(&hdr1, 1);
-    boot_test_util_write_hash(&hdr1, 1);
-
-    rc = boot_vect_write_main(&hdr0.ih_ver);
-    TEST_ASSERT(rc == 0);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr0, 0, &hdr1, 1);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_vm_ns_11_b)
-{
-    struct boot_rsp rsp;
-    int rc;
-
-    struct image_header hdr0 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 5 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 0, 5, 21, 432 },
-    };
-
-    struct image_header hdr1 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 32 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 1, 2, 3, 432 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr0, 0);
-    boot_test_util_write_hash(&hdr0, 0);
-    boot_test_util_write_image(&hdr1, 1);
-    boot_test_util_write_hash(&hdr1, 1);
-
-    rc = boot_vect_write_main(&hdr1.ih_ver);
-    TEST_ASSERT(rc == 0);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_vm_ns_11_2areas)
-{
-    struct boot_rsp rsp;
-    int rc;
-
-    struct image_header hdr0 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 5 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 0, 5, 21, 432 },
-    };
-
-    struct image_header hdr1 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 196 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 1, 2, 3, 432 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr0, 0);
-    boot_test_util_write_hash(&hdr0, 0);
-    boot_test_util_write_image(&hdr1, 1);
-    boot_test_util_write_hash(&hdr1, 1);
-
-    rc = boot_vect_write_main(&hdr1.ih_ver);
-    TEST_ASSERT(rc == 0);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_nv_bs_10)
-{
-    struct boot_status status;
-    struct boot_rsp rsp;
-    int rc;
-
-    struct image_header hdr = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 12 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 0, 2, 3, 4 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr, 0);
-    boot_test_util_write_hash(&hdr, 0);
-    boot_test_util_swap_areas(boot_test_slot_areas[1],
-      BOOT_TEST_AREA_IDX_SCRATCH);
-
-    status.length = hdr.ih_hdr_size + hdr.ih_img_size + hdr.ih_tlv_size;
-    status.state = 1;
-
-    rc = boot_write_status(&status);
-    TEST_ASSERT(rc == 0);
-    conf_load();
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_nv_bs_11)
-{
-    struct boot_status status;
-    struct boot_rsp rsp;
-    int len;
-    int rc;
-
-    struct image_header hdr0 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 12 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 0, 2, 3, 4 },
-    };
-
-    struct image_header hdr1 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 17 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 1, 1, 5, 5 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr0, 0);
-    boot_test_util_write_hash(&hdr0, 0);
-    boot_test_util_write_image(&hdr1, 1);
-    boot_test_util_write_hash(&hdr1, 1);
-    boot_test_util_copy_area(boot_test_slot_areas[1],
-      BOOT_TEST_AREA_IDX_SCRATCH);
-
-    status.length = hdr0.ih_hdr_size + hdr0.ih_img_size + hdr0.ih_tlv_size;
-    len = hdr1.ih_hdr_size + hdr1.ih_img_size + hdr1.ih_tlv_size;
-    if (len > status.length) {
-        status.length = len;
-    }
-    status.state = 1;
-
-    rc = boot_write_status(&status);
-    TEST_ASSERT(rc == 0);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_nv_bs_11_2areas)
-{
-    struct boot_status status;
-    struct boot_rsp rsp;
-    int rc;
-    int len;
-
-    struct image_header hdr0 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 150 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 0, 5, 21, 432 },
-    };
-
-    struct image_header hdr1 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 190 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 1, 2, 3, 432 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr0, 0);
-    boot_test_util_write_hash(&hdr0, 0);
-    boot_test_util_write_image(&hdr1, 1);
-    boot_test_util_write_hash(&hdr1, 1);
-    boot_test_util_swap_areas(boot_test_slot_areas[0],
-      boot_test_slot_areas[1]);
-
-    status.length = hdr0.ih_hdr_size + hdr0.ih_img_size + hdr0.ih_tlv_size;
-    len = hdr1.ih_hdr_size + hdr1.ih_img_size + hdr1.ih_tlv_size;
-    if (len > status.length) {
-        status.length = len;
-    }
-    status.state = 1 << 8;
-
-    rc = boot_write_status(&status);
-    TEST_ASSERT(rc == 0);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_vb_ns_11)
-{
-    struct boot_rsp rsp;
-    int rc;
-    int i;
-
-    struct image_header hdr0 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 5 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 0, 5, 21, 432 },
-    };
-
-    struct image_header hdr1 = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 32 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 1, 2, 3, 432 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr0, 0);
-    boot_test_util_write_image(&hdr1, 1);
-    boot_test_util_write_hash(&hdr0, 0);
-    boot_test_util_write_hash(&hdr1, 1);
-
-    rc = boot_vect_write_main(&hdr0.ih_ver);
-    TEST_ASSERT(rc == 0);
-
-    rc = boot_vect_write_test(&hdr1.ih_ver);
-    TEST_ASSERT(rc == 0);
-
-    /* First boot should use the test image. */
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
-    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
-    boot_test_util_verify_status_clear();
-
-    /* Ensure all subsequent boots use the main image. */
-    for (i = 0; i < 10; i++) {
-        rc = boot_go(&req, &rsp);
-        TEST_ASSERT(rc == 0);
-
-        TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
-        TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
-        TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
-
-        boot_test_util_verify_flash(&hdr0, 0, &hdr1, 1);
-        boot_test_util_verify_status_clear();
-    }
-}
-
-TEST_CASE(boot_test_no_hash)
-{
-    struct boot_rsp rsp;
-    int rc;
-
-    struct image_header hdr = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 0,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 12 * 1024,
-        .ih_flags = 0,
-        .ih_ver = { 0, 2, 3, 4 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr, 0);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc != 0);
-
-    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_no_flag_has_hash)
-{
-    struct boot_rsp rsp;
-    int rc;
-
-    struct image_header hdr = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 12 * 1024,
-        .ih_flags = 0,
-        .ih_ver = { 0, 2, 3, 4 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr, 0);
-    boot_test_util_write_hash(&hdr, 0);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc != 0);
-
-    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_CASE(boot_test_invalid_hash)
-{
-    struct boot_rsp rsp;
-    int rc;
-
-    struct image_header hdr = {
-        .ih_magic = IMAGE_MAGIC,
-        .ih_tlv_size = 4 + 32,
-        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
-        .ih_img_size = 12 * 1024,
-        .ih_flags = IMAGE_F_SHA256,
-        .ih_ver = { 0, 2, 3, 4 },
-    };
-
-    struct boot_req req = {
-        .br_area_descs = boot_test_area_descs,
-        .br_slot_areas = boot_test_slot_areas,
-        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
-        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
-    };
-
-    struct image_tlv tlv = {
-        .it_type = IMAGE_TLV_SHA256,
-        .it_len = 32
-    };
-    boot_test_util_init_flash();
-    boot_test_util_write_image(&hdr, 0);
-    rc = hal_flash_write(boot_test_img_addrs[0].flash_id,
-      boot_test_img_addrs[0].address + hdr.ih_hdr_size + hdr.ih_img_size,
-      &tlv, sizeof(tlv));
-    TEST_ASSERT(rc == 0);
-
-    rc = boot_go(&req, &rsp);
-    TEST_ASSERT(rc != 0);
-
-    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
-    boot_test_util_verify_status_clear();
-}
-
-TEST_SUITE(boot_test_main)
-{
-    boot_test_setup();
-    boot_test_nv_ns_10();
-    boot_test_nv_ns_01();
-    boot_test_nv_ns_11();
-    boot_test_vm_ns_10();
-    boot_test_vm_ns_01();
-    boot_test_vm_ns_11_a();
-    boot_test_vm_ns_11_b();
-    boot_test_vm_ns_11_2areas();
-    boot_test_nv_bs_10();
-    boot_test_nv_bs_11();
-    boot_test_nv_bs_11_2areas();
-    boot_test_vb_ns_11();
-    boot_test_no_hash();
-    boot_test_no_flag_has_hash();
-    boot_test_invalid_hash();
-}
-
-int
-boot_test_all(void)
-{
-    boot_test_main();
-    return tu_any_failed;
-}
-
-#ifdef MYNEWT_SELFTEST
-
-int
-main(void)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    boot_test_all();
-
-    return tu_any_failed;
-}
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bootutil/test/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/bootutil/test/pkg.yml b/libs/bootutil/test/pkg.yml
new file mode 100644
index 0000000..f5bb9d5
--- /dev/null
+++ b/libs/bootutil/test/pkg.yml
@@ -0,0 +1,33 @@
+# 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.
+#
+pkg.name: libs/bootutil/test
+pkg.type: unittest
+pkg.description: "Bootutil unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps: 
+    - libs/bootutil
+    - libs/testutil
+
+pkg.deps.SELFTEST:
+    - libs/console/stub
+
+pkg.syscfg_vals:
+    BOOTUTIL_NFFS: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bootutil/test/src/boot_serial_test.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/test/src/boot_serial_test.c b/libs/bootutil/test/src/boot_serial_test.c
new file mode 100644
index 0000000..420a802
--- /dev/null
+++ b/libs/bootutil/test/src/boot_serial_test.c
@@ -0,0 +1,1171 @@
+/**
+ * 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 <assert.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+#include "syscfg/syscfg.h"
+#include "os/endian.h"
+#include "testutil/testutil.h"
+#include "hal/hal_flash.h"
+#include "hal/flash_map.h"
+#include "fs/fs.h"
+#include "nffs/nffs.h"
+#include "config/config_file.h"
+#include "bootutil/image.h"
+#include "bootutil/loader.h"
+#include "bootutil/bootutil_misc.h"
+#include "../src/bootutil_priv.h"
+
+#include "mbedtls/sha256.h"
+
+#define BOOT_TEST_HEADER_SIZE       0x200
+
+/** Internal flash layout. */
+static struct flash_area boot_test_area_descs[] = {
+    [0] = { .fa_off = 0x00020000, .fa_size = 128 * 1024 },
+    [1] = { .fa_off = 0x00040000, .fa_size = 128 * 1024 },
+    [2] = { .fa_off = 0x00060000, .fa_size = 128 * 1024 },
+    [3] = { .fa_off = 0x00080000, .fa_size = 128 * 1024 },
+    [4] = { .fa_off = 0x000a0000, .fa_size = 128 * 1024 },
+    [5] = { .fa_off = 0x000c0000, .fa_size = 128 * 1024 },
+    [6] = { .fa_off = 0x000e0000, .fa_size = 128 * 1024 },
+};
+
+static const struct flash_area boot_test_format_descs[] = {
+    [0] = { .fa_off = 0x00004000, .fa_size = 16 * 1024 },
+    [1] = { .fa_off = 0x00008000, .fa_size = 16 * 1024 },
+    [2] = { .fa_off = 0x0000c000, .fa_size = 16 * 1024 },
+    [3] = { .fa_off = 0, .fa_size = 0 },
+};
+
+/** Areas representing the beginning of image slots. */
+static uint8_t boot_test_slot_areas[] = {
+    0, 3,
+};
+
+/** Flash offsets of the two image slots. */
+static struct {
+    uint8_t flash_id;
+    uint32_t address;
+} boot_test_img_addrs[] = {
+    { 0, 0x20000 },
+    { 0, 0x80000 },
+};
+
+#define BOOT_TEST_AREA_IDX_SCRATCH 6
+
+#define MY_CONF_PATH "/cfg/run"
+
+static struct conf_file my_conf = {
+    .cf_name = MY_CONF_PATH
+};
+
+static uint8_t
+boot_test_util_byte_at(int img_msb, uint32_t image_offset)
+{
+    uint32_t u32;
+    uint8_t *u8p;
+
+    TEST_ASSERT(image_offset < 0x01000000);
+    u32 = image_offset + (img_msb << 24);
+    u8p = (void *)&u32;
+    return u8p[image_offset % 4];
+}
+
+static void
+boot_test_util_init_flash(void)
+{
+    const struct flash_area *area_desc;
+    int rc;
+    struct nffs_area_desc nffs_descs[32];
+    int cnt;
+
+    rc = hal_flash_init();
+    TEST_ASSERT(rc == 0);
+
+    for (area_desc = boot_test_area_descs;
+         area_desc->fa_size != 0;
+         area_desc++) {
+
+        rc = flash_area_erase(area_desc, 0, area_desc->fa_size);
+        TEST_ASSERT(rc == 0);
+    }
+    cnt = 32;
+
+    rc = nffs_misc_desc_from_flash_area(FLASH_AREA_NFFS, &cnt, nffs_descs);
+    TEST_ASSERT(rc == 0);
+
+    rc = nffs_init();
+    TEST_ASSERT(rc == 0);
+    rc = nffs_format(nffs_descs);
+    TEST_ASSERT(rc == 0);
+
+    fs_mkdir("/cfg");
+}
+
+static void
+boot_test_util_copy_area(int from_area_idx, int to_area_idx)
+{
+    const struct flash_area *from_area_desc;
+    const struct flash_area *to_area_desc;
+    void *buf;
+    int rc;
+
+    from_area_desc = boot_test_area_descs + from_area_idx;
+    to_area_desc = boot_test_area_descs + to_area_idx;
+
+    TEST_ASSERT(from_area_desc->fa_size == to_area_desc->fa_size);
+
+    buf = malloc(from_area_desc->fa_size);
+    TEST_ASSERT(buf != NULL);
+
+    rc = flash_area_read(from_area_desc, 0, buf,
+                         from_area_desc->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_erase(to_area_desc,
+                          0,
+                          to_area_desc->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_write(to_area_desc, 0, buf,
+                          to_area_desc->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    free(buf);
+}
+
+static void
+boot_test_util_swap_areas(int area_idx1, int area_idx2)
+{
+    const struct flash_area *area_desc1;
+    const struct flash_area *area_desc2;
+    void *buf1;
+    void *buf2;
+    int rc;
+
+    area_desc1 = boot_test_area_descs + area_idx1;
+    area_desc2 = boot_test_area_descs + area_idx2;
+
+    TEST_ASSERT(area_desc1->fa_size == area_desc2->fa_size);
+
+    buf1 = malloc(area_desc1->fa_size);
+    TEST_ASSERT(buf1 != NULL);
+
+    buf2 = malloc(area_desc2->fa_size);
+    TEST_ASSERT(buf2 != NULL);
+
+    rc = flash_area_read(area_desc1, 0, buf1, area_desc1->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_read(area_desc2, 0, buf2, area_desc2->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_erase(area_desc1, 0, area_desc1->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_erase(area_desc2, 0, area_desc2->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_write(area_desc1, 0, buf2, area_desc1->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_write(area_desc2, 0, buf1, area_desc2->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    free(buf1);
+    free(buf2);
+}
+
+static void
+boot_test_util_write_image(const struct image_header *hdr, int slot)
+{
+    uint32_t image_off;
+    uint32_t off;
+    uint8_t flash_id;
+    uint8_t buf[256];
+    int chunk_sz;
+    int rc;
+    int i;
+
+    TEST_ASSERT(slot == 0 || slot == 1);
+
+    flash_id = boot_test_img_addrs[slot].flash_id;
+    off = boot_test_img_addrs[slot].address;
+
+    rc = hal_flash_write(flash_id, off, hdr, sizeof *hdr);
+    TEST_ASSERT(rc == 0);
+
+    off += hdr->ih_hdr_size;
+
+    image_off = 0;
+    while (image_off < hdr->ih_img_size) {
+        if (hdr->ih_img_size - image_off > sizeof buf) {
+            chunk_sz = sizeof buf;
+        } else {
+            chunk_sz = hdr->ih_img_size - image_off;
+        }
+
+        for (i = 0; i < chunk_sz; i++) {
+            buf[i] = boot_test_util_byte_at(slot, image_off + i);
+        }
+
+        rc = hal_flash_write(flash_id, off + image_off, buf, chunk_sz);
+        TEST_ASSERT(rc == 0);
+
+        image_off += chunk_sz;
+    }
+}
+
+static void
+boot_test_util_write_hash(const struct image_header *hdr, int slot)
+{
+    uint8_t tmpdata[1024];
+    uint8_t hash[32];
+    int rc;
+    uint32_t off;
+    uint32_t blk_sz;
+    uint32_t sz;
+    mbedtls_sha256_context ctx;
+    uint8_t flash_id;
+    uint32_t addr;
+    struct image_tlv tlv;
+
+    mbedtls_sha256_init(&ctx);
+    mbedtls_sha256_starts(&ctx, 0);
+
+    flash_id = boot_test_img_addrs[slot].flash_id;
+    addr = boot_test_img_addrs[slot].address;
+
+    sz = hdr->ih_hdr_size + hdr->ih_img_size;
+    for (off = 0; off < sz; off += blk_sz) {
+        blk_sz = sz - off;
+        if (blk_sz > sizeof(tmpdata)) {
+            blk_sz = sizeof(tmpdata);
+        }
+        rc = hal_flash_read(flash_id, addr + off, tmpdata, blk_sz);
+        TEST_ASSERT(rc == 0);
+        mbedtls_sha256_update(&ctx, tmpdata, blk_sz);
+    }
+    mbedtls_sha256_finish(&ctx, hash);
+
+    tlv.it_type = IMAGE_TLV_SHA256;
+    tlv._pad = 0;
+    tlv.it_len = sizeof(hash);
+
+    rc = hal_flash_write(flash_id, addr + off, &tlv, sizeof(tlv));
+    TEST_ASSERT(rc == 0);
+    off += sizeof(tlv);
+    rc = hal_flash_write(flash_id, addr + off, hash, sizeof(hash));
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+boot_test_util_verify_area(const struct flash_area *area_desc,
+                           const struct image_header *hdr,
+                           uint32_t image_addr, int img_msb)
+{
+    struct image_header temp_hdr;
+    uint32_t area_end;
+    uint32_t img_size;
+    uint32_t img_off;
+    uint32_t img_end;
+    uint32_t addr;
+    uint8_t buf[256];
+    int rem_area;
+    int past_image;
+    int chunk_sz;
+    int rem_img;
+    int rc;
+    int i;
+
+    addr = area_desc->fa_off;
+
+    if (hdr != NULL) {
+        img_size = hdr->ih_img_size;
+
+        if (addr == image_addr) {
+            rc = hal_flash_read(area_desc->fa_flash_id, image_addr,
+                                &temp_hdr, sizeof temp_hdr);
+            TEST_ASSERT(rc == 0);
+            TEST_ASSERT(memcmp(&temp_hdr, hdr, sizeof *hdr) == 0);
+
+            addr += hdr->ih_hdr_size;
+        }
+    } else {
+        img_size = 0;
+    }
+
+    area_end = area_desc->fa_off + area_desc->fa_size;
+    img_end = image_addr + img_size;
+    past_image = addr >= img_end;
+
+    while (addr < area_end) {
+        rem_area = area_end - addr;
+        rem_img = img_end - addr;
+
+        if (hdr != NULL) {
+            img_off = addr - image_addr - hdr->ih_hdr_size;
+        } else {
+            img_off = 0;
+        }
+
+        if (rem_area > sizeof buf) {
+            chunk_sz = sizeof buf;
+        } else {
+            chunk_sz = rem_area;
+        }
+
+        rc = hal_flash_read(area_desc->fa_flash_id, addr, buf, chunk_sz);
+        TEST_ASSERT(rc == 0);
+
+        for (i = 0; i < chunk_sz; i++) {
+            if (rem_img > 0) {
+                TEST_ASSERT(buf[i] == boot_test_util_byte_at(img_msb,
+                                                        img_off + i));
+            } else if (past_image) {
+                TEST_ASSERT(buf[i] == 0xff);
+            }
+        }
+
+        addr += chunk_sz;
+    }
+}
+
+static void
+boot_test_util_verify_status_clear(void)
+{
+    struct fs_file *file;
+    int rc;
+    int empty = 1;
+    char *needle = "boot/status=";
+    int nlen = strlen(needle);
+    uint32_t len, hlen;
+    char *haystack, *ptr;
+
+    rc = fs_open(MY_CONF_PATH, FS_ACCESS_READ, &file);
+    if (rc != 0) {
+        return;
+    }
+    rc = fs_filelen(file, &len);
+    TEST_ASSERT(rc == 0);
+
+    haystack = malloc(len + 1);
+    TEST_ASSERT(haystack);
+
+    rc = fs_read(file, len, haystack, &hlen);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(hlen == len);
+    haystack[len] = '\0';
+
+    fs_close(file);
+
+    ptr = haystack;
+    while ((ptr = strstr(ptr, needle))) {
+        if (ptr[nlen] == '\n') {
+            empty = 1;
+        } else {
+            empty = 0;
+        }
+        ptr += nlen;
+    }
+    TEST_ASSERT(empty == 1);
+    free(haystack);
+
+    rc = fs_open(BOOT_PATH_STATUS, FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == FS_ENOENT);
+}
+
+static void
+boot_test_util_verify_flash(const struct image_header *hdr0, int orig_slot_0,
+                            const struct image_header *hdr1, int orig_slot_1)
+{
+    const struct flash_area *area_desc;
+    int area_idx;
+
+    area_idx = 0;
+
+    while (1) {
+        area_desc = boot_test_area_descs + area_idx;
+        if (area_desc->fa_off == boot_test_img_addrs[1].address &&
+            area_desc->fa_flash_id == boot_test_img_addrs[1].flash_id) {
+            break;
+        }
+
+        boot_test_util_verify_area(area_desc, hdr0,
+                                   boot_test_img_addrs[0].address, orig_slot_0);
+        area_idx++;
+    }
+
+    while (1) {
+        if (area_idx == BOOT_TEST_AREA_IDX_SCRATCH) {
+            break;
+        }
+
+        area_desc = boot_test_area_descs + area_idx;
+        boot_test_util_verify_area(area_desc, hdr1,
+                                   boot_test_img_addrs[1].address, orig_slot_1);
+        area_idx++;
+    }
+}
+
+TEST_CASE(boot_test_setup)
+{
+    int rc;
+
+    rc = conf_file_src(&my_conf);
+    assert(rc == 0);
+    rc = conf_file_dst(&my_conf);
+    assert(rc == 0);
+
+    bootutil_cfg_register();
+}
+
+TEST_CASE(boot_test_nv_ns_10)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+    boot_test_util_write_hash(&hdr, 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_nv_ns_01)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 10 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 1);
+    boot_test_util_write_hash(&hdr, 1);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr, 1, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_nv_ns_11)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 5 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 32 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr0, 0, &hdr1, 1);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vm_ns_10)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+    boot_test_util_write_hash(&hdr, 0);
+
+    rc = boot_vect_write_main(&hdr.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vm_ns_01)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 10 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 1);
+    boot_test_util_write_hash(&hdr, 1);
+
+    rc = boot_vect_write_main(&hdr.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr, 1, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vm_ns_11_a)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 5 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 32 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+
+    rc = boot_vect_write_main(&hdr0.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr0, 0, &hdr1, 1);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vm_ns_11_b)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 5 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 32 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+
+    rc = boot_vect_write_main(&hdr1.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vm_ns_11_2areas)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 5 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 196 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+
+    rc = boot_vect_write_main(&hdr1.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_nv_bs_10)
+{
+    struct boot_status status;
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+    boot_test_util_write_hash(&hdr, 0);
+    boot_test_util_swap_areas(boot_test_slot_areas[1],
+      BOOT_TEST_AREA_IDX_SCRATCH);
+
+    status.length = hdr.ih_hdr_size + hdr.ih_img_size + hdr.ih_tlv_size;
+    status.state = 1;
+
+    rc = boot_write_status(&status);
+    TEST_ASSERT(rc == 0);
+    conf_load();
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_nv_bs_11)
+{
+    struct boot_status status;
+    struct boot_rsp rsp;
+    int len;
+    int rc;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 17 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 1, 5, 5 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+    boot_test_util_copy_area(boot_test_slot_areas[1],
+      BOOT_TEST_AREA_IDX_SCRATCH);
+
+    status.length = hdr0.ih_hdr_size + hdr0.ih_img_size + hdr0.ih_tlv_size;
+    len = hdr1.ih_hdr_size + hdr1.ih_img_size + hdr1.ih_tlv_size;
+    if (len > status.length) {
+        status.length = len;
+    }
+    status.state = 1;
+
+    rc = boot_write_status(&status);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_nv_bs_11_2areas)
+{
+    struct boot_status status;
+    struct boot_rsp rsp;
+    int rc;
+    int len;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 150 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 190 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+    boot_test_util_swap_areas(boot_test_slot_areas[0],
+      boot_test_slot_areas[1]);
+
+    status.length = hdr0.ih_hdr_size + hdr0.ih_img_size + hdr0.ih_tlv_size;
+    len = hdr1.ih_hdr_size + hdr1.ih_img_size + hdr1.ih_tlv_size;
+    if (len > status.length) {
+        status.length = len;
+    }
+    status.state = 1 << 8;
+
+    rc = boot_write_status(&status);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vb_ns_11)
+{
+    struct boot_rsp rsp;
+    int rc;
+    int i;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 5 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 32 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_hash(&hdr1, 1);
+
+    rc = boot_vect_write_main(&hdr0.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_vect_write_test(&hdr1.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    /* First boot should use the test image. */
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
+    boot_test_util_verify_status_clear();
+
+    /* Ensure all subsequent boots use the main image. */
+    for (i = 0; i < 10; i++) {
+        rc = boot_go(&req, &rsp);
+        TEST_ASSERT(rc == 0);
+
+        TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
+        TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+        TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+        boot_test_util_verify_flash(&hdr0, 0, &hdr1, 1);
+        boot_test_util_verify_status_clear();
+    }
+}
+
+TEST_CASE(boot_test_no_hash)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 0,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = 0,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc != 0);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_no_flag_has_hash)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = 0,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+    boot_test_util_write_hash(&hdr, 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc != 0);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_invalid_hash)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    struct image_tlv tlv = {
+        .it_type = IMAGE_TLV_SHA256,
+        .it_len = 32
+    };
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+    rc = hal_flash_write(boot_test_img_addrs[0].flash_id,
+      boot_test_img_addrs[0].address + hdr.ih_hdr_size + hdr.ih_img_size,
+      &tlv, sizeof(tlv));
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc != 0);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_SUITE(boot_test_main)
+{
+    boot_test_setup();
+    boot_test_nv_ns_10();
+    boot_test_nv_ns_01();
+    boot_test_nv_ns_11();
+    boot_test_vm_ns_10();
+    boot_test_vm_ns_01();
+    boot_test_vm_ns_11_a();
+    boot_test_vm_ns_11_b();
+    boot_test_vm_ns_11_2areas();
+    boot_test_nv_bs_10();
+    boot_test_nv_bs_11();
+    boot_test_nv_bs_11_2areas();
+    boot_test_vb_ns_11();
+    boot_test_no_hash();
+    boot_test_no_flag_has_hash();
+    boot_test_invalid_hash();
+}
+
+int
+boot_test_all(void)
+{
+    boot_test_main();
+    return tu_any_failed;
+}
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(void)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    boot_test_all();
+
+    return tu_any_failed;
+}
+
+#endif


[07/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_os_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_os_test.c b/net/nimble/host/test/src/ble_os_test.c
new file mode 100644
index 0000000..2b2da51
--- /dev/null
+++ b/net/nimble/host/test/src/ble_os_test.c
@@ -0,0 +1,401 @@
+/**
+ * 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 <string.h>
+#include "os/os.h"
+#include "testutil/testutil.h"
+#include "nimble/hci_common.h"
+#include "nimble/ble_hci_trans.h"
+#include "host/ble_hs_test.h"
+#include "host/ble_gap.h"
+#include "ble_hs_test_util.h"
+
+#define BLE_OS_TEST_STACK_SIZE      256
+#define BLE_OS_TEST_APP_STACK_SIZE  256
+
+#define BLE_OS_TEST_APP_PRIO         9
+#define BLE_OS_TEST_TASK_PRIO        10
+
+static struct os_task ble_os_test_task;
+static struct os_task ble_os_test_app_task;
+static os_stack_t ble_os_test_stack[OS_STACK_ALIGN(BLE_OS_TEST_STACK_SIZE)];
+
+static os_stack_t
+ble_os_test_app_stack[OS_STACK_ALIGN(BLE_OS_TEST_APP_STACK_SIZE)];
+
+static uint8_t ble_os_test_peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+static void ble_os_test_app_task_handler(void *arg);
+
+static int ble_os_test_gap_event_type;
+
+static void
+ble_os_test_init_app_task(void)
+{
+    int rc;
+
+    rc = os_task_init(&ble_os_test_app_task,
+                      "ble_gap_terminate_test_task",
+                      ble_os_test_app_task_handler, NULL,
+                      BLE_OS_TEST_APP_PRIO, OS_WAIT_FOREVER,
+                      ble_os_test_app_stack,
+                      OS_STACK_ALIGN(BLE_OS_TEST_APP_STACK_SIZE));
+    TEST_ASSERT_FATAL(rc == 0);
+}
+
+static void
+ble_os_test_misc_init(void)
+{
+    ble_hs_test_util_init_no_start();
+
+    /* Receive acknowledgements for the startup sequence.  We sent the
+     * corresponding requests when the host task was started.
+     */
+    ble_hs_test_util_set_startup_acks();
+
+    ble_os_test_init_app_task();
+}
+
+static int
+ble_os_test_misc_conn_exists(uint16_t conn_handle)
+{
+    struct ble_hs_conn *conn;
+
+    ble_hs_lock();
+
+    if (conn_handle == BLE_HS_CONN_HANDLE_NONE) {
+        conn = ble_hs_conn_first();
+    } else {
+        conn = ble_hs_conn_find(conn_handle);
+    }
+
+    ble_hs_unlock();
+
+    return conn != NULL;
+}
+
+static int
+ble_gap_direct_connect_test_connect_cb(struct ble_gap_event *event, void *arg)
+{
+    struct ble_gap_conn_desc desc;
+    int *cb_called;
+    int rc;
+
+    cb_called = arg;
+    *cb_called = 1;
+
+    TEST_ASSERT(event->type == BLE_GAP_EVENT_CONNECT);
+    TEST_ASSERT(event->connect.status == 0);
+    TEST_ASSERT(event->connect.conn_handle == 2);
+
+    rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(desc.peer_id_addr_type == BLE_ADDR_TYPE_PUBLIC);
+    TEST_ASSERT(memcmp(desc.peer_id_addr, ble_os_test_peer_addr, 6) == 0);
+
+    return 0;
+}
+
+static void
+ble_gap_direct_connect_test_task_handler(void *arg)
+{
+    struct hci_le_conn_complete evt;
+    uint8_t addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int cb_called;
+    int rc;
+
+    /* Set the connect callback so we can verify that it gets called with the
+     * proper arguments.
+     */
+    cb_called = 0;
+
+    /* Make sure there are no created connections and no connections in
+     * progress.
+     */
+    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
+
+    /* Initiate a direct connection. */
+    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                             addr, 0, NULL,
+                             ble_gap_direct_connect_test_connect_cb,
+                             &cb_called, 0);
+    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
+    TEST_ASSERT(!cb_called);
+
+    /* Receive an HCI connection-complete event. */
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_SUCCESS;
+    evt.connection_handle = 2;
+    memcpy(evt.peer_addr, addr, 6);
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT(rc == 0);
+
+    /* The connection should now be created. */
+    TEST_ASSERT(ble_os_test_misc_conn_exists(2));
+    TEST_ASSERT(cb_called);
+
+    tu_restart();
+}
+
+TEST_CASE(ble_gap_direct_connect_test_case)
+{
+    ble_os_test_misc_init();
+
+    os_task_init(&ble_os_test_task,
+                 "ble_gap_direct_connect_test_task",
+                 ble_gap_direct_connect_test_task_handler, NULL,
+                 BLE_OS_TEST_TASK_PRIO, OS_WAIT_FOREVER, ble_os_test_stack,
+                 OS_STACK_ALIGN(BLE_OS_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+static int
+ble_os_disc_test_cb(struct ble_gap_event *event, void *arg)
+{
+    int *cb_called;
+
+    cb_called = arg;
+    *cb_called = 1;
+
+    TEST_ASSERT(event->type == BLE_GAP_EVENT_DISC_COMPLETE);
+
+    return 0;
+}
+
+static void
+ble_os_disc_test_task_handler(void *arg)
+{
+    struct ble_gap_disc_params disc_params;
+    int cb_called;
+    int rc;
+
+    /* Receive acknowledgements for the startup sequence.  We sent the
+     * corresponding requests when the host task was started.
+     */
+    ble_hs_test_util_set_startup_acks();
+
+    /* Set the connect callback so we can verify that it gets called with the
+     * proper arguments.
+     */
+    cb_called = 0;
+
+    /* Make sure there are no created connections and no connections in
+     * progress.
+     */
+    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    /* Initiate the general discovery procedure with a 300 ms timeout. */
+    memset(&disc_params, 0, sizeof disc_params);
+    rc = ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC, 300, &disc_params,
+                               ble_os_disc_test_cb,
+                               &cb_called, 0, 0);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
+    TEST_ASSERT(ble_gap_master_in_progress());
+    TEST_ASSERT(!cb_called);
+
+    /* Receive acks from the controller. */
+    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
+    TEST_ASSERT(ble_gap_master_in_progress());
+    TEST_ASSERT(!cb_called);
+
+    /* Wait 100 ms; verify scan still in progress. */
+    os_time_delay(100 * OS_TICKS_PER_SEC / 1000);
+    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
+    TEST_ASSERT(ble_gap_master_in_progress());
+    TEST_ASSERT(!cb_called);
+
+    ble_hs_test_util_set_ack(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
+        0);
+
+    /* Wait 250 more ms; verify scan completed. */
+    os_time_delay(250 * OS_TICKS_PER_SEC / 1000);
+    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(cb_called);
+
+    tu_restart();
+}
+
+TEST_CASE(ble_os_disc_test_case)
+{
+    ble_os_test_misc_init();
+
+    os_task_init(&ble_os_test_task,
+                 "ble_os_disc_test_task",
+                 ble_os_disc_test_task_handler, NULL,
+                 BLE_OS_TEST_TASK_PRIO, OS_WAIT_FOREVER, ble_os_test_stack,
+                 OS_STACK_ALIGN(BLE_OS_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+static int
+ble_gap_terminate_cb(struct ble_gap_event *event, void *arg)
+{
+    int *disconn_handle;
+
+    ble_os_test_gap_event_type = event->type;
+
+    if (event->type == BLE_GAP_EVENT_DISCONNECT) {
+        disconn_handle = arg;
+        *disconn_handle = event->disconnect.conn.conn_handle;
+    }
+
+    return 0;
+}
+
+
+static void
+ble_gap_terminate_test_task_handler(void *arg)
+{
+    struct hci_disconn_complete disconn_evt;
+    struct hci_le_conn_complete conn_evt;
+    uint8_t addr1[6] = { 1, 2, 3, 4, 5, 6 };
+    uint8_t addr2[6] = { 2, 3, 4, 5, 6, 7 };
+    int disconn_handle;
+    int rc;
+
+    /* Receive acknowledgements for the startup sequence.  We sent the
+     * corresponding requests when the host task was started.
+     */
+    ble_hs_test_util_set_startup_acks();
+
+    /* Set the connect callback so we can verify that it gets called with the
+     * proper arguments.
+     */
+    disconn_handle = 0;
+
+    /* Make sure there are no created connections and no connections in
+     * progress.
+     */
+    TEST_ASSERT(!ble_os_test_misc_conn_exists(BLE_HS_CONN_HANDLE_NONE));
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    /* Create two direct connections. */
+    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                             addr1, 0, NULL, ble_gap_terminate_cb,
+                             &disconn_handle, 0);
+    memset(&conn_evt, 0, sizeof conn_evt);
+    conn_evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    conn_evt.status = BLE_ERR_SUCCESS;
+    conn_evt.connection_handle = 1;
+    memcpy(conn_evt.peer_addr, addr1, 6);
+    rc = ble_gap_rx_conn_complete(&conn_evt);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                             addr2, 0, NULL, ble_gap_terminate_cb,
+                             &disconn_handle, 0);
+    memset(&conn_evt, 0, sizeof conn_evt);
+    conn_evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    conn_evt.status = BLE_ERR_SUCCESS;
+    conn_evt.connection_handle = 2;
+    memcpy(conn_evt.peer_addr, addr2, 6);
+    rc = ble_gap_rx_conn_complete(&conn_evt);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT_FATAL(ble_os_test_misc_conn_exists(1));
+    TEST_ASSERT_FATAL(ble_os_test_misc_conn_exists(2));
+
+    /* Terminate the first one. */
+    rc = ble_hs_test_util_conn_terminate(1, 0);
+    TEST_ASSERT(rc == 0);
+    disconn_evt.connection_handle = 1;
+    disconn_evt.status = 0;
+    disconn_evt.reason = BLE_ERR_REM_USER_CONN_TERM;
+    ble_hs_test_util_rx_disconn_complete_event(&disconn_evt);
+    TEST_ASSERT(ble_os_test_gap_event_type == BLE_GAP_EVENT_DISCONNECT);
+    TEST_ASSERT(disconn_handle == 1);
+    TEST_ASSERT_FATAL(!ble_os_test_misc_conn_exists(1));
+    TEST_ASSERT_FATAL(ble_os_test_misc_conn_exists(2));
+
+    /* Terminate the second one. */
+    rc = ble_hs_test_util_conn_terminate(2, 0);
+    TEST_ASSERT(rc == 0);
+    disconn_evt.connection_handle = 2;
+    disconn_evt.status = 0;
+    disconn_evt.reason = BLE_ERR_REM_USER_CONN_TERM;
+    ble_hs_test_util_rx_disconn_complete_event(&disconn_evt);
+    TEST_ASSERT(ble_os_test_gap_event_type == BLE_GAP_EVENT_DISCONNECT);
+    TEST_ASSERT(disconn_handle == 2);
+    TEST_ASSERT_FATAL(!ble_os_test_misc_conn_exists(1));
+    TEST_ASSERT_FATAL(!ble_os_test_misc_conn_exists(2));
+
+    tu_restart();
+}
+
+static void
+ble_os_test_app_task_handler(void *arg)
+{
+    struct os_callout_func *cf;
+    struct os_event *ev;
+    int rc;
+
+    rc = ble_hs_start();
+    TEST_ASSERT(rc == 0);
+
+    while (1) {
+        ev = os_eventq_get(&ble_hs_test_util_evq);
+        switch (ev->ev_type) {
+        case OS_EVENT_T_TIMER:
+            cf = (struct os_callout_func *)ev;
+            assert(cf->cf_func);
+            cf->cf_func(CF_ARG(cf));
+            break;
+        default:
+            assert(0);
+            break;
+        }
+    }
+}
+
+TEST_CASE(ble_gap_terminate_test_case)
+{
+    ble_os_test_misc_init();
+
+    os_task_init(&ble_os_test_task,
+                 "ble_gap_terminate_test_task",
+                 ble_gap_terminate_test_task_handler, NULL,
+                 BLE_OS_TEST_TASK_PRIO, OS_WAIT_FOREVER, ble_os_test_stack,
+                 OS_STACK_ALIGN(BLE_OS_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+TEST_SUITE(ble_os_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_os_disc_test_case();
+    ble_gap_direct_connect_test_case();
+    ble_gap_terminate_test_case();
+}
+
+int
+ble_os_test_all(void)
+{
+    ble_os_test_suite();
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_sm_lgcy_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_sm_lgcy_test.c b/net/nimble/host/test/src/ble_sm_lgcy_test.c
new file mode 100644
index 0000000..4791d7a
--- /dev/null
+++ b/net/nimble/host/test/src/ble_sm_lgcy_test.c
@@ -0,0 +1,839 @@
+/**
+ * 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 <stddef.h>
+#include <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/hci_common.h"
+#include "nimble/nimble_opt.h"
+#include "host/ble_sm.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+#include "ble_sm_test_util.h"
+
+#if NIMBLE_BLE_SM
+
+/**
+ * Legacy pairing
+ * Master: peer
+ * Pair algorithm: just works
+ * Initiator IO capabilities: 4
+ * Responder IO capabilities: 3
+ * Bonding: true
+ * Initiator address type: BLE_ADDR_TYPE_PUBLIC
+ * Responder address type: BLE_ADDR_TYPE_PUBLIC
+ * Initiator key distribution: 7
+ * Responder key distribution: 7
+ */
+TEST_CASE(ble_sm_lgcy_peer_jw_iio4_rio3_b1_iat0_rat0_ik7_rk7)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
+        },
+        .resp_id_addr = {
+            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+        },
+        .pair_req = {
+            .io_cap = 0x04,
+            .oob_data_flag = 0x00,
+            .authreq = 0x05,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_rsp = {
+            .io_cap = 0x03,
+            .oob_data_flag = 0x00,
+            .authreq = 0x09,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .confirm_req[0] = {
+            .value = {
+                0xcd, 0x5b, 0x79, 0x29, 0x53, 0x31, 0x56, 0x23,
+                0x2c, 0x08, 0xed, 0x81, 0x16, 0x55, 0x8e, 0x01,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0x49, 0x39, 0x22, 0x0f, 0x7b, 0x1b, 0x80, 0xcd,
+                0xbe, 0x89, 0xd1, 0x4c, 0xbd, 0x6f, 0xda, 0x2c,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0x7f, 0x42, 0xc0, 0x2f, 0x1d, 0x07, 0x37, 0xfc,
+                0x04, 0x5b, 0x05, 0x9a, 0xed, 0x67, 0xa5, 0x68,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0x42, 0x1a, 0x58, 0xa2, 0x3b, 0x80, 0xde, 0xef,
+                0x95, 0x0d, 0xf7, 0xca, 0x06, 0x05, 0x01, 0x3c,
+            },
+        },
+        .enc_info_req = {
+            .ltk = {
+                0x2f, 0x9b, 0x16, 0xff, 0xf3, 0x73, 0x30, 0x08,
+                0xa8, 0xe5, 0x01, 0xb1, 0x3b, 0xe1, 0x87, 0x00,
+            },
+        },
+        .master_id_req = {
+            .ediv = 0xf8e0,
+            .rand_val = 0xef7c818b00000000,
+        },
+        .id_info_req = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_req = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+            },
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0xc6, 0x17, 0xc0, 0x02, 0x40, 0x0d, 0x27, 0x51,
+                0x8a, 0x77, 0xb5, 0xae, 0xd8, 0xa9, 0x7a, 0x7a,
+            },
+        },
+        .enc_info_rsp = {
+            .ltk = {
+                0xd7, 0x07, 0x22, 0x79, 0x24, 0xc6, 0xcb, 0x4d,
+                0xa3, 0xdd, 0x01, 0xfb, 0x48, 0x87, 0xd4, 0xcf,
+            },
+        },
+        .master_id_rsp = {
+            .ediv = 0x9a39,
+            .rand_val = 0x8e76d9b00000000,
+        },
+        .id_info_rsp = {
+            .irk = {
+                0xeb, 0x8a, 0x06, 0xc4, 0x93, 0x51, 0x04, 0xb3,
+                0x8b, 0xbf, 0xe8, 0x1f, 0x0e, 0x96, 0x2a, 0x54,
+            },
+        },
+        .id_addr_info_rsp = {
+            .addr_type = 0,
+            .bd_addr = {
+                0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0x14, 0x55, 0x93, 0xe1, 0xd1, 0xe7, 0xc4, 0x5d,
+                0x35, 0x97, 0xd3, 0x05, 0x30, 0xc8, 0x9d, 0x83,
+            },
+        },
+        .stk = {
+            0x1c, 0xd7, 0xb6, 0x35, 0x48, 0xfc, 0x9f, 0xef,
+            0x0e, 0x2f, 0x51, 0x77, 0xed, 0xdd, 0xbc, 0xaf,
+        },
+        .pair_alg = 0,
+        .authenticated = false,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_NONE,
+            },
+        },
+    };
+    ble_sm_test_util_peer_lgcy_good(&params);
+}
+
+/**
+ * Legacy pairing
+ * Master: peer
+ * Pair algorithm: passkey entry
+ * Initiator IO capabilities: 4
+ * Responder IO capabilities: 0
+ * Bonding: true
+ * Initiator address type: BLE_ADDR_TYPE_PUBLIC
+ * Responder address type: BLE_ADDR_TYPE_PUBLIC
+ * Initiator key distribution: 7
+ * Responder key distribution: 7
+ */
+TEST_CASE(ble_sm_lgcy_peer_pk_iio4_rio0_b1_iat0_rat0_ik7_rk7)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
+        },
+        .resp_id_addr = {
+            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+        },
+        .pair_req = {
+            .io_cap = 0x04,
+            .oob_data_flag = 0x00,
+            .authreq = 0x05,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_rsp = {
+            .io_cap = 0x00,
+            .oob_data_flag = 0x00,
+            .authreq = 0x09,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .confirm_req[0] = {
+            .value = {
+                0xa0, 0x10, 0x4a, 0xaa, 0x8b, 0x53, 0x78, 0xbb,
+                0xd2, 0xae, 0x71, 0x1f, 0x4e, 0x00, 0x70, 0x8b,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0x62, 0xf3, 0xba, 0x0e, 0xe5, 0xbe, 0x2e, 0xd8,
+                0x25, 0xb2, 0xec, 0x4c, 0x28, 0x77, 0x28, 0x60,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0x84, 0xcf, 0xe4, 0x04, 0x7d, 0xf3, 0xfc, 0xa1,
+                0x3f, 0x75, 0xd6, 0x5a, 0x7c, 0xb7, 0xa4, 0x39,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0xef, 0x6a, 0x61, 0x6e, 0x02, 0x60, 0x7f, 0x5d,
+                0x7f, 0x0d, 0xa6, 0x3c, 0x06, 0x1a, 0x5d, 0xd6,
+            },
+        },
+        .enc_info_req = {
+            .ltk = {
+                0xad, 0x01, 0x6d, 0x76, 0xa9, 0xd0, 0x23, 0xc9,
+                0x40, 0x0c, 0xbf, 0x2a, 0x4c, 0x23, 0x31, 0xc5,
+            },
+        },
+        .master_id_req = {
+            .ediv = 0xa74f,
+            .rand_val = 0x81cab3fd00000000,
+        },
+        .id_info_req = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_req = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+            },
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0x60, 0x08, 0x49, 0x00, 0x6d, 0x76, 0x98, 0x73,
+                0x9c, 0x95, 0xc4, 0xd9, 0xe8, 0x3a, 0x69, 0xbb,
+            },
+        },
+        .enc_info_rsp = {
+            .ltk = {
+                0x5b, 0x73, 0x39, 0xd9, 0x51, 0x3d, 0x92, 0xa4,
+                0x34, 0x65, 0xa5, 0x70, 0x49, 0xbe, 0x11, 0x28,
+            },
+        },
+        .master_id_rsp = {
+            .ediv = 0x9705,
+            .rand_val = 0x592f1e8d00000000,
+        },
+        .id_info_rsp = {
+            .irk = {
+                0xeb, 0x8a, 0x06, 0xc4, 0x93, 0x51, 0x04, 0xb3,
+                0x8b, 0xbf, 0xe8, 0x1f, 0x0e, 0x96, 0x2a, 0x54,
+            },
+        },
+        .id_addr_info_rsp = {
+            .addr_type = 0,
+            .bd_addr = {
+                0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0xc9, 0x9b, 0xf2, 0x75, 0xb7, 0x0d, 0xe8, 0x60,
+                0x3d, 0xf0, 0xd6, 0xa8, 0x16, 0xc5, 0x6c, 0x2a,
+            },
+        },
+        .stk = {
+            0xf2, 0x3c, 0x36, 0xc4, 0xa1, 0xfb, 0x5a, 0xa7,
+            0x96, 0x20, 0xe4, 0x29, 0xb7, 0x58, 0x22, 0x7a,
+        },
+        .pair_alg = 1,
+        .authenticated = true,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_DISP,
+                .passkey = 46128,
+            },
+        },
+    };
+    ble_sm_test_util_peer_lgcy_good(&params);
+}
+
+/**
+ * Legacy pairing
+ * Master: us
+ * Pair algorithm: just works
+ * Initiator IO capabilities: 3
+ * Responder IO capabilities: 3
+ * Bonding: true
+ * Initiator address type: BLE_ADDR_TYPE_PUBLIC
+ * Responder address type: BLE_ADDR_TYPE_RANDOM
+ * Initiator key distribution: 7
+ * Responder key distribution: 5
+ */
+TEST_CASE(ble_sm_lgcy_us_jw_iio3_rio3_b1_iat0_rat1_ik7_rk5)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a,
+        },
+        .resp_addr_type = BLE_ADDR_TYPE_RANDOM,
+        .resp_id_addr = {
+            0x11, 0x22, 0x11, 0x22, 0x11, 0xcc,
+        },
+        .pair_req = {
+            .io_cap = 0x03,
+            .oob_data_flag = 0x00,
+            .authreq = 0x01,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_rsp = {
+            .io_cap = 0x03,
+            .oob_data_flag = 0x00,
+            .authreq = 0x01,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x05,
+        },
+        .confirm_req[0] = {
+            .value = {
+                0x1c, 0xb6, 0x10, 0xea, 0x02, 0x08, 0x90, 0x64,
+                0xc7, 0xf8, 0xe5, 0x9c, 0xb4, 0x3a, 0x18, 0xca,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0xb8, 0x6f, 0xd1, 0xc6, 0x74, 0x35, 0xa3, 0x94,
+                0x68, 0x2f, 0xf1, 0x4c, 0x78, 0x44, 0xe8, 0x0d,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0x40, 0x48, 0x17, 0x4d, 0x42, 0xa0, 0xf8, 0xd5,
+                0xbf, 0x65, 0x67, 0xb8, 0x5e, 0x57, 0x38, 0xac,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0x2c, 0xa1, 0xb1, 0xf5, 0x54, 0x9b, 0x43, 0xe9,
+                0xb0, 0x62, 0x6a, 0xb0, 0x02, 0xb8, 0x6c, 0xca,
+            },
+        },
+        .enc_info_req = {
+            .ltk = {
+                0x01, 0x15, 0xb6, 0x93, 0xc9, 0xff, 0xfe, 0x27,
+                0x02, 0x41, 0xfd, 0x7b, 0x0e, 0x31, 0xd4, 0xa6,
+            },
+        },
+        .master_id_req = {
+            .ediv = 0xe4fb,
+            .rand_val = 0x8eee76b100000000,
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0x00, 0x2d, 0xf6, 0x3e, 0x5e, 0x0f, 0xd1, 0xe8,
+                0x4e, 0x5f, 0x61, 0x1c, 0x2c, 0x0b, 0xa5, 0x51,
+            },
+        },
+        .enc_info_rsp = {
+            .ltk = {
+                0x88, 0xbc, 0x95, 0x8d, 0xaa, 0x26, 0x8d, 0xd5,
+                0x18, 0xc9, 0x06, 0x70, 0xc2, 0x30, 0x56, 0x4c,
+            },
+        },
+        .master_id_rsp = {
+            .ediv = 0x4413,
+            .rand_val = 0xfad1c27300000000,
+        },
+        .id_info_rsp = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_rsp = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0x03, 0xad, 0xa4, 0xe1, 0x34, 0x76, 0x95, 0x54,
+                0xe5, 0x8f, 0xa4, 0x06, 0x72, 0xe6, 0xfc, 0x65,
+            },
+        },
+        .stk = {
+            0x31, 0x54, 0x42, 0x6c, 0x1c, 0x03, 0x36, 0x44,
+            0x0b, 0x72, 0x90, 0xa5, 0x1f, 0x79, 0x5b, 0xe9,
+        },
+        .pair_alg = 0,
+        .authenticated = false,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_NONE,
+            },
+        },
+    };
+    ble_sm_test_util_us_lgcy_good(&params);
+}
+
+/**
+ * Legacy pairing
+ * Master: us
+ * Pair algorithm: passkey entry
+ * Initiator IO capabilities: 4
+ * Responder IO capabilities: 2
+ * Bonding: true
+ * Initiator address type: BLE_ADDR_TYPE_PUBLIC
+ * Responder address type: BLE_ADDR_TYPE_RANDOM
+ * Initiator key distribution: 7
+ * Responder key distribution: 5
+ */
+TEST_CASE(ble_sm_lgcy_us_pk_iio4_rio2_b1_iat0_rat1_ik7_rk5)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a,
+        },
+        .resp_addr_type = BLE_ADDR_TYPE_RANDOM,
+        .resp_id_addr = {
+            0x11, 0x22, 0x11, 0x22, 0x11, 0xcc,
+        },
+        .pair_req = {
+            .io_cap = 0x04,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_rsp = {
+            .io_cap = 0x02,
+            .oob_data_flag = 0x00,
+            .authreq = 0x05,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x05,
+        },
+        .confirm_req[0] = {
+            .value = {
+                0xb5, 0xd4, 0xc5, 0xe8, 0xef, 0xef, 0xd8, 0xd7,
+                0x2b, 0x14, 0x34, 0x35, 0x29, 0x18, 0xda, 0x12,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0x1a, 0x03, 0x20, 0xda, 0x60, 0x21, 0x9b, 0x4b,
+                0x5d, 0x45, 0x90, 0x64, 0xe1, 0x24, 0x2c, 0xb6,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0x45, 0xa3, 0x1a, 0x0b, 0xf6, 0x0f, 0x7c, 0xcf,
+                0x1a, 0xfb, 0xfc, 0x1a, 0xad, 0x62, 0x0e, 0x76,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0x82, 0xbb, 0x9f, 0x67, 0xc4, 0x88, 0xcb, 0x58,
+                0xee, 0xf9, 0x34, 0x35, 0x23, 0xa3, 0xd0, 0x22,
+            },
+        },
+        .enc_info_req = {
+            .ltk = {
+                0xfa, 0x43, 0x8f, 0x1f, 0xe6, 0x2a, 0x94, 0x5b,
+                0x54, 0x89, 0x2b, 0x0f, 0xd7, 0x23, 0x77, 0x9e,
+            },
+        },
+        .master_id_req = {
+            .ediv = 0x88b3,
+            .rand_val = 0x7c970e18dec74560,
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0x2e, 0x70, 0x3c, 0xbf, 0x20, 0xbe, 0x7d, 0x2d,
+                0xb3, 0x50, 0x46, 0x33, 0x4c, 0x20, 0x0e, 0xc8,
+            },
+        },
+        .enc_info_rsp = {
+            .ltk = {
+                0xc1, 0x64, 0x33, 0x10, 0x0f, 0x70, 0x2f, 0x9c,
+                0xe7, 0x31, 0xc5, 0x32, 0xdd, 0x98, 0x16, 0x75,
+            },
+        },
+        .master_id_rsp = {
+            .ediv = 0x1c19,
+            .rand_val = 0xef308872dc2a4cc2,
+        },
+        .id_info_rsp = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_rsp = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0xd7, 0x75, 0xfa, 0xed, 0xd7, 0xdd, 0x7b, 0xb3,
+                0xa4, 0x20, 0xea, 0x2f, 0x75, 0x60, 0xb1, 0x84,
+            },
+        },
+        .stk = {
+            0x9e, 0xe8, 0x35, 0x22, 0xb6, 0xbb, 0x54, 0x0d,
+            0x48, 0x1b, 0x25, 0xa0, 0xd8, 0xe2, 0xa5, 0x08,
+        },
+        .pair_alg = 1,
+        .authenticated = true,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_DISP,
+                .passkey = 46128,
+            },
+        },
+    };
+    ble_sm_test_util_us_lgcy_good(&params);
+}
+
+/**
+ * Legacy pairing
+ * Master: us
+ * Pair algorithm: out of band
+ * Initiator IO capabilities: 3
+ * Responder IO capabilities: 3
+ * Bonding: true
+ * Initiator address type: BLE_ADDR_TYPE_PUBLIC
+ * Responder address type: BLE_ADDR_TYPE_PUBLIC
+ * Initiator key distribution: 7
+ * Responder key distribution: 7
+ */
+TEST_CASE(ble_sm_lgcy_us_ob_iio3_rio3_b1_iat0_rat0_ik7_rk7)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
+        },
+        .resp_id_addr = {
+            0x66, 0x33, 0x22, 0x66, 0x55, 0x11,
+        },
+        .pair_req = {
+            .io_cap = 0x03,
+            .oob_data_flag = 0x01,
+            .authreq = 0x05,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_rsp = {
+            .io_cap = 0x03,
+            .oob_data_flag = 0x01,
+            .authreq = 0x05,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .confirm_req[0] = {
+            .value = {
+                0x2c, 0x3f, 0x3e, 0xf5, 0x39, 0x50, 0x78, 0x4a,
+                0x3e, 0x14, 0x1a, 0x51, 0xfb, 0x8d, 0x6c, 0x10,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0xa9, 0x5c, 0x18, 0xb1, 0xdb, 0x51, 0x53, 0xa5,
+                0xd3, 0xe7, 0x72, 0x17, 0xfb, 0xa8, 0xfb, 0xeb,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0x40, 0x2f, 0x42, 0xba, 0x10, 0x7b, 0x22, 0x65,
+                0x84, 0xef, 0x63, 0xdf, 0x84, 0x7b, 0x04, 0xef,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0x94, 0xdc, 0x3c, 0xef, 0x65, 0xf7, 0x99, 0x2e,
+                0x50, 0x29, 0x97, 0x2a, 0x57, 0xfd, 0xe6, 0x6a,
+            },
+        },
+        .enc_info_req = {
+            .ltk = {
+                0x8c, 0x8e, 0x57, 0xba, 0x17, 0xbb, 0x04, 0xb5,
+                0x16, 0xad, 0x31, 0x37, 0xf8, 0x3e, 0x4f, 0x21,
+            },
+        },
+        .master_id_req = {
+            .ediv = 0xaaa4,
+            .rand_val = 0xc0c830e300000000,
+        },
+        .id_info_req = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_req = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x66, 0x33, 0x22, 0x66, 0x55, 0x11,
+            },
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0x5a, 0xe4, 0x2b, 0x40, 0x3a, 0x34, 0x1d, 0x94,
+                0x56, 0x7d, 0xf4, 0x41, 0x23, 0x81, 0xc4, 0x11,
+            },
+        },
+        .enc_info_rsp = {
+            .ltk = {
+                0xa6, 0x8e, 0xa0, 0xa4, 0x02, 0x64, 0x4c, 0x09,
+                0x31, 0x25, 0x8a, 0x4f, 0x49, 0x35, 0xb0, 0x1f,
+            },
+        },
+        .master_id_rsp = {
+            .ediv = 0x57a3,
+            .rand_val = 0x8276af9000000000,
+        },
+        .id_info_rsp = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_rsp = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0x8e, 0xef, 0x53, 0x5c, 0x1b, 0x21, 0x67, 0x8d,
+                0x07, 0x5e, 0xaa, 0xe8, 0x41, 0xa9, 0x36, 0xcf,
+            },
+        },
+        .stk = {
+            0x4c, 0xd4, 0xa7, 0xee, 0x83, 0xcd, 0xd1, 0x9e,
+            0x84, 0xeb, 0xb8, 0xd2, 0xaf, 0x4a, 0x71, 0x2e,
+        },
+        .pair_alg = 2,
+        .authenticated = 1,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_OOB,
+                .oob = {
+                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+                    0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
+                },
+            },
+        },
+    };
+    ble_sm_test_util_us_lgcy_good(&params);
+}
+
+/**
+ * Legacy pairing
+ * Master: peer
+ * Pair algorithm: passkey entry
+ * Initiator IO capabilities: 4
+ * Responder IO capabilities: 4
+ * Bonding: true
+ * Initiator address type: BLE_ADDR_TYPE_PUBLIC
+ * Responder address type: BLE_ADDR_TYPE_PUBLIC
+ * Initiator key distribution: 7
+ * Responder key distribution: 7
+ */
+TEST_CASE(ble_sm_lgcy_peer_pk_iio4_rio4_b1_iat0_rat0_ik7_rk7)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
+        },
+        .resp_id_addr = {
+            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+        },
+        .pair_req = {
+            .io_cap = 0x04,
+            .oob_data_flag = 0x00,
+            .authreq = 0x05,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_rsp = {
+            .io_cap = 0x04,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .confirm_req[0] = {
+            .value = {
+                0x93, 0x64, 0xb1, 0xb0, 0x07, 0x41, 0x22, 0xe7,
+                0x3e, 0x5a, 0x87, 0xf5, 0x1f, 0x25, 0x79, 0x11,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0x2d, 0x40, 0x15, 0xc4, 0x21, 0xeb, 0xd5, 0x73,
+                0xc8, 0x5d, 0xb8, 0xb9, 0x45, 0x31, 0xd5, 0x58,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0x8c, 0x2c, 0x3b, 0xf3, 0x90, 0xaa, 0x2e, 0xcf,
+                0xc7, 0x5b, 0xf6, 0xae, 0xb6, 0x4c, 0xc3, 0x61,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0x7a, 0x94, 0x97, 0x0a, 0xbe, 0xaf, 0xc0, 0x6b,
+                0xd4, 0xf4, 0x04, 0xd1, 0x21, 0x46, 0x34, 0xf3,
+            },
+        },
+        .enc_info_req = {
+            .ltk = {
+                0x3a, 0x10, 0xd1, 0xab, 0x13, 0xee, 0x16, 0xee,
+                0xcf, 0xae, 0xf1, 0x63, 0xf0, 0x6f, 0xb0, 0x89,
+            },
+        },
+        .master_id_req = {
+            .ediv = 0xb634,
+            .rand_val = 0xa99ac2007b4278a8,
+        },
+        .id_info_req = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_req = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+            },
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0x51, 0x4b, 0x7b, 0x31, 0xf7, 0xa6, 0x8a, 0x60,
+                0x4f, 0x10, 0x04, 0x5f, 0xb8, 0xee, 0xf6, 0xb3,
+            },
+        },
+        .enc_info_rsp = {
+            .ltk = {
+                0xa1, 0x1d, 0xdd, 0xee, 0x85, 0xcb, 0xe0, 0x48,
+                0x1e, 0xdd, 0xa4, 0x9d, 0xed, 0x3f, 0x15, 0x17,
+            },
+        },
+        .master_id_rsp = {
+            .ediv = 0x7e06,
+            .rand_val = 0xe6077f688c5ca67,
+        },
+        .id_info_rsp = {
+            .irk = {
+                0xeb, 0x8a, 0x06, 0xc4, 0x93, 0x51, 0x04, 0xb3,
+                0x8b, 0xbf, 0xe8, 0x1f, 0x0e, 0x96, 0x2a, 0x54,
+            },
+        },
+        .id_addr_info_rsp = {
+            .addr_type = 0,
+            .bd_addr = {
+                0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0x16, 0x7a, 0x2e, 0x9d, 0x43, 0x4d, 0x7b, 0x0b,
+                0x88, 0xe2, 0x11, 0xb0, 0x4d, 0xa1, 0xed, 0x08,
+            },
+        },
+        .stk = {
+            0x6c, 0x3e, 0x78, 0x47, 0xe8, 0x57, 0x9f, 0xe9,
+            0x3a, 0x8f, 0x0a, 0xbb, 0xd4, 0x60, 0xf6, 0x0d,
+        },
+        .pair_alg = 1,
+        .authenticated = true,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_INPUT,
+                .passkey = 449182,
+            },
+        },
+    };
+    ble_sm_test_util_peer_lgcy_good(&params);
+}
+
+TEST_SUITE(ble_sm_lgcy_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    /*** No privacy. */
+
+    /* Peer as initiator. */
+    ble_sm_lgcy_peer_jw_iio4_rio3_b1_iat0_rat0_ik7_rk7();
+    ble_sm_lgcy_peer_pk_iio4_rio0_b1_iat0_rat0_ik7_rk7();
+    ble_sm_lgcy_peer_pk_iio4_rio4_b1_iat0_rat0_ik7_rk7();
+
+    /* Us as initiator. */
+    ble_sm_lgcy_us_jw_iio3_rio3_b1_iat0_rat1_ik7_rk5();
+    ble_sm_lgcy_us_pk_iio4_rio2_b1_iat0_rat1_ik7_rk5();
+    ble_sm_lgcy_us_ob_iio3_rio3_b1_iat0_rat0_ik7_rk7();
+}
+
+#endif



[24/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_att_svr_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_att_svr_test.c b/net/nimble/host/src/test/ble_att_svr_test.c
deleted file mode 100644
index 1509b41..0000000
--- a/net/nimble/host/src/test/ble_att_svr_test.c
+++ /dev/null
@@ -1,2314 +0,0 @@
-/**
- * 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 <stddef.h>
-#include <errno.h>
-#include <string.h>
-#include "testutil/testutil.h"
-#include "nimble/hci_common.h"
-#include "host/ble_hs_test.h"
-#include "host/ble_uuid.h"
-#include "ble_hs_test_util.h"
-
-static uint8_t *ble_att_svr_test_attr_r_1;
-static uint16_t ble_att_svr_test_attr_r_1_len;
-static uint8_t *ble_att_svr_test_attr_r_2;
-static uint16_t ble_att_svr_test_attr_r_2_len;
-
-static uint8_t ble_att_svr_test_attr_w_1[1024];
-static uint16_t ble_att_svr_test_attr_w_1_len;
-static uint8_t ble_att_svr_test_attr_w_2[1024];
-static uint16_t ble_att_svr_test_attr_w_2_len;
-
-static uint16_t ble_att_svr_test_n_conn_handle;
-static uint16_t ble_att_svr_test_n_attr_handle;
-static uint8_t ble_att_svr_test_attr_n[1024];
-static uint16_t ble_att_svr_test_attr_n_len;
-
-static int
-ble_att_svr_test_misc_gap_cb(struct ble_gap_event *event, void *arg)
-{
-    switch (event->type) {
-    case BLE_GAP_EVENT_NOTIFY_RX:
-        ble_att_svr_test_n_conn_handle = event->notify_rx.conn_handle;
-        ble_att_svr_test_n_attr_handle = event->notify_rx.attr_handle;
-        TEST_ASSERT_FATAL(OS_MBUF_PKTLEN(event->notify_rx.om) <=
-                          sizeof ble_att_svr_test_attr_n);
-        ble_att_svr_test_attr_n_len = OS_MBUF_PKTLEN(event->notify_rx.om);
-        os_mbuf_copydata(event->notify_rx.om, 0, ble_att_svr_test_attr_n_len,
-                         ble_att_svr_test_attr_n);
-        break;
-
-    default:
-        break;
-    }
-
-    return 0;
-}
-
-/**
- * @return                      The handle of the new test connection.
- */
-static uint16_t
-ble_att_svr_test_misc_init(uint16_t mtu)
-{
-    struct ble_l2cap_chan *chan;
-    struct ble_hs_conn *conn;
-    int rc;
-
-    ble_hs_test_util_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 ble_att_svr_test_misc_gap_cb, NULL);
-
-    ble_hs_lock();
-
-    rc = ble_hs_misc_conn_chan_find(2, BLE_L2CAP_CID_ATT, &conn, &chan);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    if (mtu != 0) {
-        chan->blc_my_mtu = mtu;
-        chan->blc_peer_mtu = mtu;
-        chan->blc_flags |= BLE_L2CAP_CHAN_F_TXED_MTU;
-    }
-
-    ble_hs_unlock();
-
-    ble_att_svr_test_attr_r_1_len = 0;
-    ble_att_svr_test_attr_r_2_len = 0;
-    ble_att_svr_test_attr_w_1_len = 0;
-
-    return 2;
-}
-
-static int
-ble_att_svr_test_misc_attr_fn_r_1(uint16_t conn_handle, uint16_t attr_handle,
-                                  uint8_t op, uint16_t offset,
-                                  struct os_mbuf **om, void *arg)
-{
-    switch (op) {
-    case BLE_ATT_ACCESS_OP_READ:
-        if (offset > ble_att_svr_test_attr_r_1_len) {
-            return BLE_ATT_ERR_INVALID_OFFSET;
-        }
-
-        os_mbuf_append(*om, ble_att_svr_test_attr_r_1 + offset,
-                       ble_att_svr_test_attr_r_1_len - offset);
-        return 0;
-
-    default:
-        return -1;
-    }
-}
-
-static int
-ble_att_svr_test_misc_attr_fn_r_2(uint16_t conn_handle, uint16_t attr_handle,
-                                  uint8_t op, uint16_t offset,
-                                  struct os_mbuf **om, void *arg)
-{
-
-    switch (op) {
-    case BLE_ATT_ACCESS_OP_READ:
-        if (offset > ble_att_svr_test_attr_r_2_len) {
-            return BLE_ATT_ERR_INVALID_OFFSET;
-        }
-
-        os_mbuf_append(*om, ble_att_svr_test_attr_r_2 + offset,
-                       ble_att_svr_test_attr_r_2_len - offset);
-        return 0;
-
-    default:
-        return -1;
-    }
-}
-
-#define BLE_ATT_SVR_TEST_LAST_SVC  11
-#define BLE_ATT_SVR_TEST_LAST_ATTR 24
-
-static int
-ble_att_svr_test_misc_attr_fn_r_group(uint16_t conn_handle,
-                                      uint16_t attr_handle,
-                                      uint8_t op,
-                                      uint16_t offset,
-                                      struct os_mbuf **om,
-                                      void *arg)
-{
-    uint8_t *src;
-    int rc;
-
-    /* Service 0x1122 from 1 to 5 */
-    /* Service 0x2233 from 6 to 10 */
-    /* Service 010203...0f from 11 to 24 */
-
-    static uint8_t vals[25][16] = {
-        [1] =   { 0x22, 0x11 },
-        [2] =   { 0x01, 0x11 },
-        [3] =   { 0x02, 0x11 },
-        [4] =   { 0x03, 0x11 },
-        [5] =   { 0x04, 0x11 },
-        [6] =   { 0x33, 0x22 },
-        [7] =   { 0x01, 0x22 },
-        [8] =   { 0x02, 0x22 },
-        [9] =   { 0x03, 0x22 },
-        [10] =  { 0x04, 0x22 },
-        [11] =  { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 },
-        [12] =  { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
-        [13] =  { 0xdd, 0xdd },
-        [14] =  { 0x55, 0x55 },
-        [15] =  { 0xdd, 0xdd },
-        [16] =  { 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 },
-        [17] =  { 0xdd, 0xdd },
-        [18] =  { 0x66, 0x66 },
-        [19] =  { 0xdd, 0xdd },
-        [20] =  { 0x77, 0x77 },
-        [21] =  { 0xdd, 0xdd },
-        [22] =  { 0x88, 0x88 },
-        [23] =  { 0xdd, 0xdd },
-        [24] =  { 0x99, 0x99 },
-    };
-
-    static uint8_t zeros[14];
-
-    if (op != BLE_ATT_ACCESS_OP_READ) {
-        return -1;
-    }
-
-    TEST_ASSERT_FATAL(attr_handle >= 1 &&
-                      attr_handle <= BLE_ATT_SVR_TEST_LAST_ATTR);
-
-    src = &vals[attr_handle][0];
-    if (memcmp(src + 2, zeros, 14) == 0) {
-        rc = os_mbuf_append(*om, src, 2);
-    } else {
-        rc = os_mbuf_append(*om, src, 16);
-    }
-    if (rc != 0) {
-        return BLE_ATT_ERR_INSUFFICIENT_RES;
-    }
-
-    return 0;
-}
-
-static void
-ble_att_svr_test_misc_register_uuid128(uint8_t *uuid128, uint8_t flags,
-                                       uint16_t expected_handle,
-                                       ble_att_svr_access_fn *fn)
-{
-    uint16_t handle;
-    int rc;
-
-    rc = ble_att_svr_register(uuid128, flags, &handle, fn, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT_FATAL(handle == expected_handle);
-}
-
-static void
-ble_att_svr_test_misc_register_uuid16(uint16_t uuid16, uint8_t flags,
-                                      uint16_t expected_handle,
-                                      ble_att_svr_access_fn *fn)
-{
-    uint8_t uuid128[16];
-    int rc;
-
-    rc = ble_uuid_16_to_128(uuid16, uuid128);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_att_svr_test_misc_register_uuid128(uuid128, flags, expected_handle,
-                                           fn);
-}
-
-static void
-ble_att_svr_test_misc_register_group_attrs(void)
-{
-    /* Service 0x1122 from 1 to 5 */
-    /* Service 0x2233 from 6 to 10 */
-    /* Service 010203...0f from 11 to 24 */
-
-    int i;
-
-    /* Service 0x1122 from 1 to 5 */
-    ble_att_svr_test_misc_register_uuid16(
-        BLE_ATT_UUID_PRIMARY_SERVICE, HA_FLAG_PERM_RW, 1,
-        ble_att_svr_test_misc_attr_fn_r_group);
-    for (i = 2; i <= 5; i++) {
-        if ((i - 2) % 2 == 0) {
-            ble_att_svr_test_misc_register_uuid16(
-                BLE_ATT_UUID_CHARACTERISTIC, HA_FLAG_PERM_RW, i,
-                ble_att_svr_test_misc_attr_fn_r_group);
-        } else {
-            ble_att_svr_test_misc_register_uuid16(
-                i, HA_FLAG_PERM_RW, i,
-                ble_att_svr_test_misc_attr_fn_r_group);
-        }
-    }
-
-    /* Service 0x2233 from 6 to 10 */
-    ble_att_svr_test_misc_register_uuid16(
-        BLE_ATT_UUID_PRIMARY_SERVICE, HA_FLAG_PERM_RW, 6,
-        ble_att_svr_test_misc_attr_fn_r_group);
-    for (i = 7; i <= 10; i++) {
-        ble_att_svr_test_misc_register_uuid16(
-            BLE_ATT_UUID_INCLUDE, HA_FLAG_PERM_RW, i,
-            ble_att_svr_test_misc_attr_fn_r_group);
-    }
-
-    /* Service 010203...0f from 11 to 24 */
-    ble_att_svr_test_misc_register_uuid16(
-        BLE_ATT_UUID_PRIMARY_SERVICE, HA_FLAG_PERM_RW, 11,
-        ble_att_svr_test_misc_attr_fn_r_group);
-    for (i = 12; i <= 24; i++) {
-        if ((i - 12) % 2 == 0) {
-            ble_att_svr_test_misc_register_uuid16(
-                BLE_ATT_UUID_CHARACTERISTIC, HA_FLAG_PERM_RW, i,
-                ble_att_svr_test_misc_attr_fn_r_group);
-        } else {
-            ble_att_svr_test_misc_register_uuid16(
-                i, HA_FLAG_PERM_RW, i,
-                ble_att_svr_test_misc_attr_fn_r_group);
-        }
-    }
-}
-
-static int
-ble_att_svr_test_misc_attr_fn_w_1(uint16_t conn_handle, uint16_t attr_handle,
-                                  uint8_t op, uint16_t offset,
-                                  struct os_mbuf **om, void *arg)
-{
-    switch (op) {
-    case BLE_ATT_ACCESS_OP_WRITE:
-        os_mbuf_copydata(*om, 0, OS_MBUF_PKTLEN(*om),
-                         ble_att_svr_test_attr_w_1);
-        ble_att_svr_test_attr_w_1_len = OS_MBUF_PKTLEN(*om);
-        return 0;
-
-    default:
-        return -1;
-    }
-}
-
-static int
-ble_att_svr_test_misc_attr_fn_w_2(uint16_t conn_handle, uint16_t attr_handle,
-                                  uint8_t op, uint16_t offset,
-                                  struct os_mbuf **om, void *arg)
-{
-    switch (op) {
-    case BLE_ATT_ACCESS_OP_WRITE:
-        os_mbuf_copydata(*om, 0, OS_MBUF_PKTLEN(*om),
-                         ble_att_svr_test_attr_w_2);
-        ble_att_svr_test_attr_w_2_len = OS_MBUF_PKTLEN(*om);
-        return 0;
-
-    default:
-        return -1;
-    }
-}
-
-static int
-ble_att_svr_test_misc_attr_fn_w_fail(uint16_t conn_handle,
-                                     uint16_t attr_handle,
-                                     uint8_t op, uint16_t offset,
-                                     struct os_mbuf **om, void *arg)
-{
-    return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
-}
-
-static void
-ble_att_svr_test_misc_verify_w_1(void *data, int data_len)
-{
-    TEST_ASSERT(ble_att_svr_test_attr_w_1_len == data_len);
-    TEST_ASSERT(memcmp(ble_att_svr_test_attr_w_1, data, data_len) == 0);
-}
-
-static void
-ble_att_svr_test_misc_verify_w_2(void *data, int data_len)
-{
-    TEST_ASSERT(ble_att_svr_test_attr_w_2_len == data_len);
-    TEST_ASSERT(memcmp(ble_att_svr_test_attr_w_2, data, data_len) == 0);
-}
-
-static void
-ble_att_svr_test_misc_verify_tx_err_rsp(uint8_t req_op, uint16_t handle,
-                                        uint8_t error_code)
-{
-    struct ble_att_error_rsp rsp;
-    struct os_mbuf *om;
-    uint8_t buf[BLE_ATT_ERROR_RSP_SZ];
-    int rc;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue();
-
-    rc = os_mbuf_copydata(om, 0, sizeof buf, buf);
-    TEST_ASSERT(rc == 0);
-
-    ble_att_error_rsp_parse(buf, sizeof buf, &rsp);
-
-    TEST_ASSERT(rsp.baep_req_op == req_op);
-    TEST_ASSERT(rsp.baep_handle == handle);
-    TEST_ASSERT(rsp.baep_error_code == error_code);
-}
-
-static void
-ble_att_svr_test_misc_verify_tx_read_blob_rsp(uint8_t *attr_data, int attr_len)
-{
-    struct os_mbuf *om;
-    uint8_t u8;
-    int rc;
-    int i;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue();
-
-    rc = os_mbuf_copydata(om, 0, 1, &u8);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(u8 == BLE_ATT_OP_READ_BLOB_RSP);
-
-    for (i = 0; i < attr_len; i++) {
-        rc = os_mbuf_copydata(om, i + 1, 1, &u8);
-        TEST_ASSERT(rc == 0);
-        TEST_ASSERT(u8 == attr_data[i]);
-    }
-
-    rc = os_mbuf_copydata(om, i + 1, 1, &u8);
-    TEST_ASSERT(rc != 0);
-}
-
-static void
-ble_att_svr_test_misc_rx_read_mult_req(uint16_t conn_handle,
-                                       uint16_t *handles, int num_handles,
-                                       int success)
-{
-    uint8_t buf[256];
-    int off;
-    int rc;
-    int i;
-
-    ble_att_read_mult_req_write(buf, sizeof buf);
-
-    off = BLE_ATT_READ_MULT_REQ_BASE_SZ;
-    for (i = 0; i < num_handles; i++) {
-        htole16(buf + off, handles[i]);
-        off += 2;
-    }
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    if (success) {
-        TEST_ASSERT(rc == 0);
-    } else {
-        TEST_ASSERT(rc != 0);
-    }
-}
-
-static void
-ble_att_svr_test_misc_verify_tx_read_mult_rsp(
-    uint16_t conn_handle, struct ble_hs_test_util_flat_attr *attrs,
-    int num_attrs)
-{
-    struct ble_l2cap_chan *chan;
-    struct os_mbuf *om;
-    uint16_t attr_len;
-    uint16_t mtu;
-    uint8_t u8;
-    int rc;
-    int off;
-    int i;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue();
-
-    rc = os_mbuf_copydata(om, 0, 1, &u8);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(u8 == BLE_ATT_OP_READ_MULT_RSP);
-
-    ble_hs_lock();
-
-    rc = ble_hs_misc_conn_chan_find(conn_handle, BLE_L2CAP_CID_ATT,
-                                    NULL, &chan);
-    TEST_ASSERT_FATAL(rc == 0);
-    mtu = ble_l2cap_chan_mtu(chan);
-
-    ble_hs_unlock();
-
-    off = 1;
-    for (i = 0; i < num_attrs; i++) {
-        attr_len = min(attrs[i].value_len, mtu - off);
-
-        rc = os_mbuf_cmpf(om, off, attrs[i].value, attr_len);
-        TEST_ASSERT(rc == 0);
-
-        off += attr_len;
-    }
-
-    TEST_ASSERT(OS_MBUF_PKTLEN(om) == off);
-}
-
-static void
-ble_att_svr_test_misc_verify_all_read_mult(
-    uint16_t conn_handle, struct ble_hs_test_util_flat_attr *attrs,
-    int num_attrs)
-{
-    uint16_t handles[256];
-    int i;
-
-    TEST_ASSERT_FATAL(num_attrs <= sizeof handles / sizeof handles[0]);
-
-    for (i = 0; i < num_attrs; i++) {
-        handles[i] = attrs[i].handle;
-    }
-
-    ble_att_svr_test_misc_rx_read_mult_req(conn_handle, handles, num_attrs, 1);
-    ble_att_svr_test_misc_verify_tx_read_mult_rsp(conn_handle,
-                                                  attrs, num_attrs);
-}
-
-
-static void
-ble_att_svr_test_misc_verify_tx_write_rsp(void)
-{
-    struct os_mbuf *om;
-    uint8_t u8;
-    int rc;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue();
-
-    rc = os_mbuf_copydata(om, 0, 1, &u8);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(u8 == BLE_ATT_OP_WRITE_RSP);
-}
-
-static void
-ble_att_svr_test_misc_verify_tx_mtu_rsp(uint16_t conn_handle)
-{
-    struct ble_att_mtu_cmd rsp;
-    struct ble_l2cap_chan *chan;
-    struct ble_hs_conn *conn;
-    struct os_mbuf *om;
-    uint8_t buf[BLE_ATT_MTU_CMD_SZ];
-    int rc;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue();
-
-    rc = os_mbuf_copydata(om, 0, sizeof buf, buf);
-    TEST_ASSERT(rc == 0);
-
-    ble_att_mtu_cmd_parse(buf, sizeof buf, &rsp);
-
-    ble_hs_lock();
-    rc = ble_hs_misc_conn_chan_find(conn_handle, BLE_L2CAP_CID_ATT,
-                                    &conn, &chan);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(rsp.bamc_mtu == chan->blc_my_mtu);
-    ble_hs_unlock();
-}
-
-struct ble_att_svr_test_info_entry {
-    uint16_t handle;        /* 0 on last entry */
-    uint16_t uuid16;        /* 0 if not present. */
-    uint8_t uuid128[16];
-};
-
-static void
-ble_att_svr_test_misc_verify_tx_find_info_rsp(
-    struct ble_att_svr_test_info_entry *entries)
-{
-    struct ble_att_svr_test_info_entry *entry;
-    struct ble_att_find_info_rsp rsp;
-    struct os_mbuf *om;
-    uint16_t handle;
-    uint16_t uuid16;
-    uint8_t buf[BLE_ATT_FIND_INFO_RSP_BASE_SZ];
-    uint8_t uuid128[16];
-    int off;
-    int rc;
-
-    ble_hs_test_util_tx_all();
-
-    off = 0;
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-
-    rc = os_mbuf_copydata(om, off, sizeof buf, buf);
-    TEST_ASSERT(rc == 0);
-    off += sizeof buf;
-
-    ble_att_find_info_rsp_parse(buf, sizeof buf, &rsp);
-
-    for (entry = entries; entry->handle != 0; entry++) {
-        rc = os_mbuf_copydata(om, off, 2, &handle);
-        TEST_ASSERT(rc == 0);
-        off += 2;
-
-        handle = le16toh((void *)&handle);
-        TEST_ASSERT(handle == entry->handle);
-
-        if (entry->uuid16 != 0) {
-            TEST_ASSERT(rsp.bafp_format ==
-                        BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT);
-            rc = os_mbuf_copydata(om, off, 2, &uuid16);
-            TEST_ASSERT(rc == 0);
-            off += 2;
-
-            uuid16 = le16toh((void *)&uuid16);
-            TEST_ASSERT(uuid16 == entry->uuid16);
-        } else {
-            TEST_ASSERT(rsp.bafp_format ==
-                        BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT);
-            rc = os_mbuf_copydata(om, off, 16, uuid128);
-            TEST_ASSERT(rc == 0);
-            off += 16;
-
-            TEST_ASSERT(memcmp(uuid128, entry->uuid128, 16) == 0);
-        }
-    }
-
-    /* Ensure there is no extra data in the response. */
-    TEST_ASSERT(off == OS_MBUF_PKTHDR(om)->omp_len);
-}
-
-struct ble_att_svr_test_type_value_entry {
-    uint16_t first;        /* 0 on last entry */
-    uint16_t last;
-};
-
-static void
-ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
-    struct ble_att_svr_test_type_value_entry *entries)
-{
-    struct ble_att_svr_test_type_value_entry *entry;
-    struct os_mbuf *om;
-    uint16_t u16;
-    uint8_t op;
-    int off;
-    int rc;
-
-    ble_hs_test_util_tx_all();
-
-    off = 0;
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-
-    rc = os_mbuf_copydata(om, off, 1, &op);
-    TEST_ASSERT(rc == 0);
-    off += 1;
-
-    TEST_ASSERT(op == BLE_ATT_OP_FIND_TYPE_VALUE_RSP);
-
-    for (entry = entries; entry->first != 0; entry++) {
-        rc = os_mbuf_copydata(om, off, 2, &u16);
-        TEST_ASSERT(rc == 0);
-        htole16(&u16, u16);
-        TEST_ASSERT(u16 == entry->first);
-        off += 2;
-
-        rc = os_mbuf_copydata(om, off, 2, &u16);
-        TEST_ASSERT(rc == 0);
-        htole16(&u16, u16);
-        TEST_ASSERT(u16 == entry->last);
-        off += 2;
-    }
-
-    /* Ensure there is no extra data in the response. */
-    TEST_ASSERT(off == OS_MBUF_PKTHDR(om)->omp_len);
-}
-
-struct ble_att_svr_test_group_type_entry {
-    uint16_t start_handle;  /* 0 on last entry */
-    uint16_t end_handle;    /* 0 on last entry */
-    uint16_t uuid16;        /* 0 if not present. */
-    uint8_t uuid128[16];
-};
-
-/** Returns the number of entries successfully verified. */
-static void
-ble_att_svr_test_misc_verify_tx_read_group_type_rsp(
-    struct ble_att_svr_test_group_type_entry *entries)
-{
-    struct ble_att_svr_test_group_type_entry *entry;
-    struct ble_att_read_group_type_rsp rsp;
-    struct os_mbuf *om;
-    uint16_t u16;
-    uint8_t uuid128[16];
-    int off;
-    int rc;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-
-    ble_att_read_group_type_rsp_parse(om->om_data, om->om_len, &rsp);
-
-    off = BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ;
-    for (entry = entries; entry->start_handle != 0; entry++) {
-        if (entry->uuid16 != 0) {
-            TEST_ASSERT(rsp.bagp_length ==
-                        BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_16);
-        } else {
-            TEST_ASSERT(rsp.bagp_length ==
-                        BLE_ATT_READ_GROUP_TYPE_ADATA_SZ_128);
-        }
-
-        rc = os_mbuf_copydata(om, off, 2, &u16);
-        TEST_ASSERT(rc == 0);
-        htole16(&u16, u16);
-        TEST_ASSERT(u16 == entry->start_handle);
-        off += 2;
-
-        rc = os_mbuf_copydata(om, off, 2, &u16);
-        TEST_ASSERT(rc == 0);
-        htole16(&u16, u16);
-        if (entry->start_handle == BLE_ATT_SVR_TEST_LAST_SVC) {
-            TEST_ASSERT(u16 == 0xffff);
-        } else {
-            TEST_ASSERT(u16 == entry->end_handle);
-        }
-        off += 2;
-
-        if (entry->uuid16 != 0) {
-            rc = os_mbuf_copydata(om, off, 2, &u16);
-            TEST_ASSERT(rc == 0);
-            htole16(&u16, u16);
-            TEST_ASSERT(u16 == entry->uuid16);
-            off += 2;
-        } else {
-            rc = os_mbuf_copydata(om, off, 16, uuid128);
-            TEST_ASSERT(rc == 0);
-            TEST_ASSERT(memcmp(uuid128, entry->uuid128, 16) == 0);
-            off += 16;
-        }
-    }
-
-    /* Ensure there is no extra data in the response. */
-    TEST_ASSERT(off == OS_MBUF_PKTLEN(om));
-}
-
-struct ble_att_svr_test_type_entry {
-    uint16_t handle;  /* 0 on last entry */
-    void *value;
-    int value_len;
-};
-
-/** Returns the number of entries successfully verified. */
-static void
-ble_att_svr_test_misc_verify_tx_read_type_rsp(
-    struct ble_att_svr_test_type_entry *entries)
-{
-    struct ble_att_svr_test_type_entry *entry;
-    struct ble_att_read_type_rsp rsp;
-    struct os_mbuf *om;
-    uint16_t handle;
-    uint8_t buf[512];
-    int off;
-    int rc;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-
-    ble_att_read_type_rsp_parse(om->om_data, om->om_len, &rsp);
-
-    off = BLE_ATT_READ_TYPE_RSP_BASE_SZ;
-    for (entry = entries; entry->handle != 0; entry++) {
-        TEST_ASSERT_FATAL(rsp.batp_length ==
-                          BLE_ATT_READ_TYPE_ADATA_BASE_SZ + entry->value_len);
-
-        rc = os_mbuf_copydata(om, off, 2, &handle);
-        TEST_ASSERT(rc == 0);
-        handle = le16toh(&handle);
-        TEST_ASSERT(handle == entry->handle);
-        off += 2;
-
-        rc = os_mbuf_copydata(om, off, entry->value_len, buf);
-        TEST_ASSERT(rc == 0);
-        TEST_ASSERT(memcmp(entry->value, buf, entry->value_len) == 0);
-        off += entry->value_len;
-    }
-
-    /* Ensure there is no extra data in the response. */
-    TEST_ASSERT(off == OS_MBUF_PKTLEN(om));
-}
-
-static void
-ble_att_svr_test_misc_verify_tx_prep_write_rsp(uint16_t attr_handle,
-                                               uint16_t offset,
-                                               void *data, int data_len)
-{
-    struct ble_att_prep_write_cmd rsp;
-    struct os_mbuf *om;
-    uint8_t buf[1024];
-    int rc;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue();
-
-    rc = os_mbuf_copydata(om, 0, OS_MBUF_PKTLEN(om), buf);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_att_prep_write_rsp_parse(buf, sizeof buf, &rsp);
-
-    TEST_ASSERT(rsp.bapc_handle == attr_handle);
-    TEST_ASSERT(rsp.bapc_offset == offset);
-    TEST_ASSERT(memcmp(buf + BLE_ATT_PREP_WRITE_CMD_BASE_SZ, data,
-                       data_len) == 0);
-
-    TEST_ASSERT(OS_MBUF_PKTLEN(om) ==
-                BLE_ATT_PREP_WRITE_CMD_BASE_SZ + data_len);
-}
-
-static void
-ble_att_svr_test_misc_verify_tx_exec_write_rsp(void)
-{
-    struct os_mbuf *om;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    ble_att_exec_write_rsp_parse(om->om_data, om->om_len);
-}
-
-static void
-ble_att_svr_test_misc_mtu_exchange(uint16_t my_mtu, uint16_t peer_sent,
-                                   uint16_t peer_actual, uint16_t chan_mtu)
-{
-    struct ble_att_mtu_cmd req;
-    struct ble_l2cap_chan *chan;
-    struct ble_hs_conn *conn;
-    uint16_t conn_handle;
-    uint8_t buf[BLE_ATT_MTU_CMD_SZ];
-    int rc;
-
-    conn_handle = ble_att_svr_test_misc_init(my_mtu);
-
-    req.bamc_mtu = peer_sent;
-    ble_att_mtu_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-
-    ble_att_svr_test_misc_verify_tx_mtu_rsp(conn_handle);
-
-    ble_hs_lock();
-    rc = ble_hs_misc_conn_chan_find(conn_handle, BLE_L2CAP_CID_ATT,
-                                    &conn, &chan);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(chan->blc_peer_mtu == peer_actual);
-    TEST_ASSERT(ble_l2cap_chan_mtu(chan) == chan_mtu);
-    ble_hs_unlock();
-
-}
-
-static void
-ble_att_svr_test_misc_prep_write(uint16_t conn_handle, uint16_t attr_handle,
-                                 uint16_t offset, void *data,
-                                 int data_len, uint8_t error_code)
-{
-    struct ble_att_prep_write_cmd prep_req;
-    uint8_t buf[1024];
-    int rc;
-
-    prep_req.bapc_handle = attr_handle;
-    prep_req.bapc_offset = offset;
-    ble_att_prep_write_req_write(buf, sizeof buf, &prep_req);
-    memcpy(buf + BLE_ATT_PREP_WRITE_CMD_BASE_SZ, data, data_len);
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(
-        conn_handle, BLE_L2CAP_CID_ATT, buf,
-        BLE_ATT_PREP_WRITE_CMD_BASE_SZ + data_len);
-
-    if (error_code == 0) {
-        TEST_ASSERT(rc == 0);
-        ble_att_svr_test_misc_verify_tx_prep_write_rsp(attr_handle, offset,
-                                                       data, data_len);
-    } else {
-        TEST_ASSERT(rc != 0);
-        ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_PREP_WRITE_REQ,
-                                                attr_handle, error_code);
-    }
-}
-
-static void
-ble_att_svr_test_misc_exec_write(uint16_t conn_handle, uint8_t flags,
-                                 uint8_t error_code, uint16_t error_handle)
-{
-    struct ble_att_exec_write_req exec_req;
-    uint8_t buf[1024];
-    int rc;
-
-    exec_req.baeq_flags = flags;
-    ble_att_exec_write_req_write(buf, sizeof buf, &exec_req);
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf,
-                                                BLE_ATT_EXEC_WRITE_REQ_SZ);
-
-    if (error_code == 0) {
-        TEST_ASSERT(rc == 0);
-        ble_att_svr_test_misc_verify_tx_exec_write_rsp();
-    } else {
-        TEST_ASSERT(rc != 0);
-        ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_EXEC_WRITE_REQ,
-                                                error_handle, error_code);
-    }
-}
-
-static void
-ble_att_svr_test_misc_rx_notify(uint16_t conn_handle, uint16_t attr_handle,
-                                void *attr_val, int attr_len, int good)
-{
-    struct ble_att_notify_req req;
-    uint8_t buf[1024];
-    int off;
-    int rc;
-
-    req.banq_handle = attr_handle;
-    ble_att_notify_req_write(buf, sizeof buf, &req);
-    off = BLE_ATT_NOTIFY_REQ_BASE_SZ;
-
-    memcpy(buf + off, attr_val, attr_len);
-    off += attr_len;
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    if (good) {
-        TEST_ASSERT(rc == 0);
-    } else {
-        TEST_ASSERT(rc == BLE_HS_EBADDATA);
-    }
-}
-
-static void
-ble_att_svr_test_misc_verify_notify(uint16_t conn_handle, uint16_t attr_handle,
-                                    void *attr_val, int attr_len, int good)
-{
-    ble_att_svr_test_n_conn_handle = 0xffff;
-    ble_att_svr_test_n_attr_handle = 0;
-    ble_att_svr_test_attr_n_len = 0;
-
-    ble_att_svr_test_misc_rx_notify(conn_handle, attr_handle, attr_val,
-                                    attr_len, good);
-
-    if (good) {
-        TEST_ASSERT(ble_att_svr_test_n_conn_handle == conn_handle);
-        TEST_ASSERT(ble_att_svr_test_n_attr_handle == attr_handle);
-        TEST_ASSERT(ble_att_svr_test_attr_n_len == attr_len);
-        TEST_ASSERT(memcmp(ble_att_svr_test_attr_n, attr_val, attr_len) == 0);
-    } else {
-        TEST_ASSERT(ble_att_svr_test_n_conn_handle == 0xffff);
-        TEST_ASSERT(ble_att_svr_test_n_attr_handle == 0);
-        TEST_ASSERT(ble_att_svr_test_attr_n_len == 0);
-    }
-}
-
-static void
-ble_att_svr_test_misc_verify_tx_indicate_rsp(void)
-{
-    struct os_mbuf *om;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-
-    ble_att_indicate_rsp_parse(om->om_data, om->om_len);
-}
-
-static void
-ble_att_svr_test_misc_rx_indicate(uint16_t conn_handle, uint16_t attr_handle,
-                                  void *attr_val, int attr_len, int good)
-{
-    struct ble_att_indicate_req req;
-    uint8_t buf[1024];
-    int off;
-    int rc;
-
-    req.baiq_handle = attr_handle;
-    ble_att_indicate_req_write(buf, sizeof buf, &req);
-    off = BLE_ATT_INDICATE_REQ_BASE_SZ;
-
-    memcpy(buf + off, attr_val, attr_len);
-    off += attr_len;
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    if (good) {
-        TEST_ASSERT(rc == 0);
-    } else {
-        TEST_ASSERT(rc == BLE_HS_EBADDATA);
-    }
-}
-
-static void
-ble_att_svr_test_misc_verify_indicate(uint16_t conn_handle,
-                                      uint16_t attr_handle,
-                                      void *attr_val, int attr_len, int good)
-{
-    ble_att_svr_test_n_conn_handle = 0xffff;
-    ble_att_svr_test_n_attr_handle = 0;
-    ble_att_svr_test_attr_n_len = 0;
-
-    ble_att_svr_test_misc_rx_indicate(conn_handle, attr_handle, attr_val,
-                                      attr_len, good);
-
-    if (good) {
-        TEST_ASSERT(ble_att_svr_test_n_conn_handle == conn_handle);
-        TEST_ASSERT(ble_att_svr_test_n_attr_handle == attr_handle);
-        TEST_ASSERT(ble_att_svr_test_attr_n_len == attr_len);
-        TEST_ASSERT(memcmp(ble_att_svr_test_attr_n, attr_val, attr_len) == 0);
-        ble_att_svr_test_misc_verify_tx_indicate_rsp();
-    } else {
-        TEST_ASSERT(ble_att_svr_test_n_conn_handle == 0xffff);
-        TEST_ASSERT(ble_att_svr_test_n_attr_handle == 0);
-        TEST_ASSERT(ble_att_svr_test_attr_n_len == 0);
-        ble_hs_test_util_tx_all();
-        TEST_ASSERT(ble_hs_test_util_prev_tx_queue_sz() == 0);
-    }
-}
-
-TEST_CASE(ble_att_svr_test_mtu)
-{
-    /*** MTU too low; should pretend peer sent default value instead. */
-    ble_att_svr_test_misc_mtu_exchange(BLE_ATT_MTU_DFLT, 5,
-                                       BLE_ATT_MTU_DFLT, BLE_ATT_MTU_DFLT);
-
-    /*** MTUs equal. */
-    ble_att_svr_test_misc_mtu_exchange(50, 50, 50, 50);
-
-    /*** Peer's higher than mine. */
-    ble_att_svr_test_misc_mtu_exchange(50, 100, 100, 50);
-
-    /*** Mine higher than peer's. */
-    ble_att_svr_test_misc_mtu_exchange(100, 50, 50, 50);
-}
-
-TEST_CASE(ble_att_svr_test_read)
-{
-    struct ble_att_read_req req;
-    struct ble_hs_conn *conn;
-    struct os_mbuf *om;
-    uint16_t conn_handle;
-    uint8_t buf[BLE_ATT_READ_REQ_SZ];
-    uint8_t uuid_sec[16] = {1};
-    uint8_t uuid[16] = {0};
-    int rc;
-
-    conn_handle = ble_att_svr_test_misc_init(0);
-
-    /*** Nonexistent attribute. */
-    req.barq_handle = 0;
-    ble_att_read_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_REQ, 0,
-                                            BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** Successful read. */
-    ble_att_svr_test_attr_r_1 = (uint8_t[]){0,1,2,3,4,5,6,7};
-    ble_att_svr_test_attr_r_1_len = 8;
-    rc = ble_att_svr_register(uuid, HA_FLAG_PERM_RW, &req.barq_handle,
-                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    ble_att_read_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_hs_test_util_verify_tx_read_rsp(
-        ble_att_svr_test_attr_r_1, ble_att_svr_test_attr_r_1_len);
-
-    /*** Partial read. */
-    ble_att_svr_test_attr_r_1 =
-        (uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
-                    22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39};
-    ble_att_svr_test_attr_r_1_len = 40;
-
-    ble_att_read_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_hs_test_util_verify_tx_read_rsp(ble_att_svr_test_attr_r_1,
-                                        BLE_ATT_MTU_DFLT - 1);
-
-    /*** Read requires encryption. */
-    /* Insufficient authentication. */
-    rc = ble_att_svr_register(uuid_sec, BLE_ATT_F_READ | BLE_ATT_F_READ_ENC,
-                              &req.barq_handle,
-                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    ble_att_read_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_AUTHEN));
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_REQ,
-                                            req.barq_handle,
-                                            BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
-
-    /* Security check bypassed for local reads. */
-    rc = ble_att_svr_read_local(req.barq_handle, &om);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(OS_MBUF_PKTLEN(om) == ble_att_svr_test_attr_r_1_len);
-    TEST_ASSERT(os_mbuf_cmpf(om, 0, ble_att_svr_test_attr_r_1,
-                               ble_att_svr_test_attr_r_1_len) == 0);
-    os_mbuf_free_chain(om);
-
-    /* Ensure no response got sent. */
-    ble_hs_test_util_tx_all();
-    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
-
-    /* Encrypt link; success. */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(conn_handle);
-    conn->bhc_sec_state.encrypted = 1;
-    ble_hs_unlock();
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_hs_test_util_verify_tx_read_rsp(ble_att_svr_test_attr_r_1,
-                                        BLE_ATT_MTU_DFLT - 1);
-
-}
-
-TEST_CASE(ble_att_svr_test_read_blob)
-{
-    struct ble_att_read_blob_req req;
-    uint16_t conn_handle;
-    uint8_t buf[BLE_ATT_READ_BLOB_REQ_SZ];
-    uint8_t uuid[16] = {0};
-    int rc;
-
-    conn_handle = ble_att_svr_test_misc_init(0);
-
-    /*** Nonexistent attribute. */
-    req.babq_handle = 0;
-    req.babq_offset = 0;
-    ble_att_read_blob_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_BLOB_REQ, 0,
-                                            BLE_ATT_ERR_INVALID_HANDLE);
-
-
-    /*** Successful partial read. */
-    ble_att_svr_test_attr_r_1 =
-        (uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,
-                    22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39};
-    ble_att_svr_test_attr_r_1_len = 40;
-    rc = ble_att_svr_register(uuid, HA_FLAG_PERM_RW, &req.babq_handle,
-                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    ble_att_read_blob_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_blob_rsp(ble_att_svr_test_attr_r_1,
-                                                  BLE_ATT_MTU_DFLT - 1);
-
-    /*** Read remainder of attribute. */
-    req.babq_offset = BLE_ATT_MTU_DFLT - 1;
-    ble_att_read_blob_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_blob_rsp(
-        ble_att_svr_test_attr_r_1 + BLE_ATT_MTU_DFLT - 1,
-        40 - (BLE_ATT_MTU_DFLT - 1));
-
-    /*** Zero-length read. */
-    req.babq_offset = ble_att_svr_test_attr_r_1_len;
-    ble_att_read_blob_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_blob_rsp(ble_att_svr_test_attr_r_1,
-                                                  0);
-
-}
-
-TEST_CASE(ble_att_svr_test_read_mult)
-{
-    uint16_t conn_handle;
-    int rc;
-
-    conn_handle = ble_att_svr_test_misc_init(0);
-
-    struct ble_hs_test_util_flat_attr attrs[2] = {
-        {
-            .handle = 0,
-            .offset = 0,
-            .value = { 1, 2, 3, 4 },
-            .value_len = 4,
-        },
-        {
-            .handle = 0,
-            .offset = 0,
-            .value = { 2, 3, 4, 5, 6 },
-            .value_len = 5,
-        },
-    };
-
-    ble_att_svr_test_attr_r_1 = attrs[0].value;
-    ble_att_svr_test_attr_r_1_len = attrs[0].value_len;
-    ble_att_svr_test_attr_r_2 = attrs[1].value;
-    ble_att_svr_test_attr_r_2_len = attrs[1].value_len;
-
-    rc = ble_att_svr_register(BLE_UUID16(0x1111), HA_FLAG_PERM_RW,
-                              &attrs[0].handle,
-                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    rc = ble_att_svr_register(BLE_UUID16(0x2222), HA_FLAG_PERM_RW,
-                              &attrs[1].handle,
-                              ble_att_svr_test_misc_attr_fn_r_2, NULL);
-    TEST_ASSERT(rc == 0);
-
-    /*** Single nonexistent attribute. */
-    ble_att_svr_test_misc_rx_read_mult_req(
-        conn_handle, ((uint16_t[]){ 100 }), 1, 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_MULT_REQ,
-                                            100, BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** Single attribute. */
-    ble_att_svr_test_misc_verify_all_read_mult(conn_handle, &attrs[0], 1);
-
-    /*** Two attributes. */
-    ble_att_svr_test_misc_verify_all_read_mult(conn_handle, attrs, 2);
-
-    /*** Reverse order. */
-    ble_att_svr_test_misc_verify_all_read_mult(conn_handle, attrs, 2);
-
-    /*** Second attribute nonexistent; verify only error txed. */
-    ble_att_svr_test_misc_rx_read_mult_req(
-        conn_handle, ((uint16_t[]){ attrs[0].handle, 100 }), 2, 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_READ_MULT_REQ,
-                                            100, BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** Response too long; verify only MTU bytes sent. */
-    attrs[0].value_len = 20;
-    memcpy(attrs[0].value,
-           ((uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}),
-           attrs[0].value_len);
-    ble_att_svr_test_attr_r_1_len = attrs[0].value_len;
-
-    attrs[1].value_len = 20;
-    memcpy(attrs[1].value,
-           ((uint8_t[]){
-                22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39
-           }),
-           attrs[1].value_len);
-    ble_att_svr_test_attr_r_2_len = attrs[1].value_len;
-
-    ble_att_svr_test_misc_verify_all_read_mult(conn_handle, attrs, 2);
-
-}
-
-TEST_CASE(ble_att_svr_test_write)
-{
-    struct ble_att_write_req req;
-    struct ble_hs_conn *conn;
-    uint16_t conn_handle;
-    uint8_t buf[BLE_ATT_WRITE_REQ_BASE_SZ + 8];
-    uint8_t uuid_sec[16] = {2};
-    uint8_t uuid_rw[16] = {0};
-    uint8_t uuid_r[16] = {1};
-    int rc;
-
-    conn_handle = ble_att_svr_test_misc_init(0);
-
-    /*** Nonexistent attribute. */
-    req.bawq_handle = 0;
-    ble_att_write_req_write(buf, sizeof buf, &req);
-    memcpy(buf + BLE_ATT_READ_REQ_SZ, ((uint8_t[]){0,1,2,3,4,5,6,7}), 8);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_WRITE_REQ, 0, BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** Write not permitted if non-local. */
-    /* Non-local write (fail). */
-    rc = ble_att_svr_register(uuid_r, BLE_ATT_F_READ, &req.bawq_handle,
-                              ble_att_svr_test_misc_attr_fn_w_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    ble_att_write_req_write(buf, sizeof buf, &req);
-    memcpy(buf + BLE_ATT_WRITE_REQ_BASE_SZ,
-           ((uint8_t[]){0,1,2,3,4,5,6,7}), 8);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == BLE_HS_ENOTSUP);
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_WRITE_REQ,
-                                            req.bawq_handle,
-                                            BLE_ATT_ERR_WRITE_NOT_PERMITTED);
-
-    /* Local write (success). */
-    rc = ble_hs_test_util_write_local_flat(req.bawq_handle, buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-
-    /* Ensure no response got sent. */
-    ble_hs_test_util_tx_all();
-    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
-
-    /*** Successful write. */
-    rc = ble_att_svr_register(uuid_rw, HA_FLAG_PERM_RW, &req.bawq_handle,
-                              ble_att_svr_test_misc_attr_fn_w_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    ble_att_write_req_write(buf, sizeof buf, &req);
-    memcpy(buf + BLE_ATT_WRITE_REQ_BASE_SZ,
-           ((uint8_t[]){0,1,2,3,4,5,6,7}), 8);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_write_rsp();
-
-    /*** Write requires encryption. */
-    /* Insufficient authentication. */
-    rc = ble_att_svr_register(uuid_sec, BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC,
-                              &req.bawq_handle,
-                              ble_att_svr_test_misc_attr_fn_w_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    ble_att_write_req_write(buf, sizeof buf, &req);
-    memcpy(buf + BLE_ATT_WRITE_REQ_BASE_SZ,
-           ((uint8_t[]){0,1,2,3,4,5,6,7}), 8);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == BLE_HS_ATT_ERR(BLE_ATT_ERR_INSUFFICIENT_AUTHEN));
-    ble_att_svr_test_misc_verify_tx_err_rsp(BLE_ATT_OP_WRITE_REQ,
-                                            req.bawq_handle,
-                                            BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
-
-    /* Security check bypassed for local writes. */
-    rc = ble_hs_test_util_write_local_flat(req.bawq_handle, buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-
-    /* Ensure no response got sent. */
-    ble_hs_test_util_tx_all();
-    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
-
-    /* Encrypt link; success. */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(conn_handle);
-    conn->bhc_sec_state.encrypted = 1;
-    ble_hs_unlock();
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_write_rsp();
-}
-
-TEST_CASE(ble_att_svr_test_find_info)
-{
-    struct ble_att_find_info_req req;
-    uint16_t conn_handle;
-    uint16_t handle1;
-    uint16_t handle2;
-    uint16_t handle3;
-    uint8_t buf[BLE_ATT_FIND_INFO_REQ_SZ];
-    uint8_t uuid1[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
-    uint8_t uuid2[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
-    uint8_t uuid3[16] = {
-        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
-        0x00, 0x10, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00
-    };
-    int rc;
-
-    conn_handle = ble_att_svr_test_misc_init(128);
-
-    /*** Start handle of 0. */
-    req.bafq_start_handle = 0;
-    req.bafq_end_handle = 0;
-
-    ble_att_find_info_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_FIND_INFO_REQ, 0, BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** Start handle > end handle. */
-    req.bafq_start_handle = 101;
-    req.bafq_end_handle = 100;
-
-    ble_att_find_info_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_FIND_INFO_REQ, 101, BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** No attributes. */
-    req.bafq_start_handle = 200;
-    req.bafq_end_handle = 300;
-
-    ble_att_find_info_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_FIND_INFO_REQ, 200, BLE_ATT_ERR_ATTR_NOT_FOUND);
-
-    /*** Range too late. */
-    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle1,
-                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    req.bafq_start_handle = 200;
-    req.bafq_end_handle = 300;
-
-    ble_att_find_info_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_FIND_INFO_REQ, 200, BLE_ATT_ERR_ATTR_NOT_FOUND);
-
-    /*** One 128-bit entry. */
-    req.bafq_start_handle = handle1;
-    req.bafq_end_handle = handle1;
-
-    ble_att_find_info_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_find_info_rsp(
-        ((struct ble_att_svr_test_info_entry[]) { {
-            .handle = handle1,
-            .uuid128 = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},
-        }, {
-            .handle = 0,
-        } }));
-
-    /*** Two 128-bit entries. */
-    rc = ble_att_svr_register(uuid2, HA_FLAG_PERM_RW, &handle2,
-                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    req.bafq_start_handle = handle1;
-    req.bafq_end_handle = handle2;
-
-    ble_att_find_info_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_find_info_rsp(
-        ((struct ble_att_svr_test_info_entry[]) { {
-            .handle = handle1,
-            .uuid128 = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},
-        }, {
-            .handle = handle2,
-            .uuid128 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
-        }, {
-            .handle = 0,
-        } }));
-
-    /*** Two 128-bit entries; 16-bit entry doesn't get sent. */
-    rc = ble_att_svr_register(uuid3, HA_FLAG_PERM_RW, &handle3,
-                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    req.bafq_start_handle = handle1;
-    req.bafq_end_handle = handle3;
-
-    ble_att_find_info_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_find_info_rsp(
-        ((struct ble_att_svr_test_info_entry[]) { {
-            .handle = handle1,
-            .uuid128 = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},
-        }, {
-            .handle = handle2,
-            .uuid128 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
-        }, {
-            .handle = 0,
-        } }));
-
-    /*** Remaining 16-bit entry requested. */
-    req.bafq_start_handle = handle3;
-    req.bafq_end_handle = handle3;
-
-    ble_att_find_info_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_find_info_rsp(
-        ((struct ble_att_svr_test_info_entry[]) { {
-            .handle = handle3,
-            .uuid16 = 0x000f,
-        }, {
-            .handle = 0,
-        } }));
-
-}
-
-TEST_CASE(ble_att_svr_test_find_type_value)
-{
-    struct ble_att_find_type_value_req req;
-    uint8_t buf[BLE_ATT_FIND_TYPE_VALUE_REQ_BASE_SZ + 2];
-    uint16_t conn_handle;
-    uint16_t handle1;
-    uint16_t handle2;
-    uint16_t handle3;
-    uint16_t handle4;
-    uint16_t handle5;
-    uint8_t uuid1[16] = {
-        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
-        0x00, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00
-    };
-    uint8_t uuid2[16] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
-    uint8_t uuid3[16] = {
-        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
-        0x00, 0x10, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00
-    };
-    int rc;
-
-    conn_handle = ble_att_svr_test_misc_init(128);
-
-    /* One-time write of the attribute value at the end of the request. */
-    ble_att_svr_test_attr_r_1 = (uint8_t[]){0x99, 0x99};
-    ble_att_svr_test_attr_r_1_len = 2;
-    memcpy(buf + BLE_ATT_FIND_TYPE_VALUE_REQ_BASE_SZ,
-           ble_att_svr_test_attr_r_1,
-           ble_att_svr_test_attr_r_1_len);
-
-    /*** Start handle of 0. */
-    req.bavq_start_handle = 0;
-    req.bavq_end_handle = 0;
-    req.bavq_attr_type = 0x0001;
-
-    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 0,
-        BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** Start handle > end handle. */
-    req.bavq_start_handle = 101;
-    req.bavq_end_handle = 100;
-
-    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 101,
-        BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** No attributes. */
-    req.bavq_start_handle = 200;
-    req.bavq_end_handle = 300;
-
-    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 200,
-        BLE_ATT_ERR_ATTR_NOT_FOUND);
-
-    /*** Range too late. */
-    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle1,
-                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    req.bavq_start_handle = 200;
-    req.bavq_end_handle = 300;
-
-    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_FIND_TYPE_VALUE_REQ, 200,
-        BLE_ATT_ERR_ATTR_NOT_FOUND);
-
-    /*** One entry, one attribute. */
-    req.bavq_start_handle = handle1;
-    req.bavq_end_handle = handle1;
-
-    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
-        ((struct ble_att_svr_test_type_value_entry[]) { {
-            .first = handle1,
-            .last = handle1,
-        }, {
-            .first = 0,
-        } }));
-
-    /*** One entry, two attributes. */
-    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle2,
-                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    req.bavq_start_handle = handle1;
-    req.bavq_end_handle = handle2;
-
-    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
-        ((struct ble_att_svr_test_type_value_entry[]) { {
-            .first = handle1,
-            .last = handle2,
-        }, {
-            .first = 0,
-        } }));
-
-    /*** Entry 1: two attributes; entry 2: one attribute. */
-    rc = ble_att_svr_register(uuid2, HA_FLAG_PERM_RW, &handle3,
-                              ble_att_svr_test_misc_attr_fn_r_2, NULL);
-    TEST_ASSERT(rc == 0);
-
-    rc = ble_att_svr_register(uuid1, HA_FLAG_PERM_RW, &handle4,
-                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
-    TEST_ASSERT(rc == 0);
-
-    req.bavq_start_handle = 0x0001;
-    req.bavq_end_handle = 0xffff;
-
-    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
-        ((struct ble_att_svr_test_type_value_entry[]) { {
-            .first = handle1,
-            .last = handle2,
-        }, {
-            .first = handle4,
-            .last = handle4,
-        }, {
-            .first = 0,
-        } }));
-
-    /*** Ensure attribute with wrong value is not included. */
-    ble_att_svr_test_attr_r_2 = (uint8_t[]){0x00, 0x00};
-    ble_att_svr_test_attr_r_2_len = 2;
-
-    req.bavq_start_handle = 0x0001;
-    req.bavq_end_handle = 0xffff;
-
-    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
-        ((struct ble_att_svr_test_type_value_entry[]) { {
-            .first = handle1,
-            .last = handle2,
-        }, {
-            .first = handle4,
-            .last = handle4,
-        }, {
-            .first = 0,
-        } }));
-
-    /*** Ensure attribute with wrong type is not included. */
-    rc = ble_att_svr_register(uuid3, HA_FLAG_PERM_RW, &handle5,
-                              ble_att_svr_test_misc_attr_fn_r_1, NULL);
-
-    req.bavq_start_handle = 0x0001;
-    req.bavq_end_handle = 0xffff;
-
-    ble_att_find_type_value_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_find_type_value_rsp(
-        ((struct ble_att_svr_test_type_value_entry[]) { {
-            .first = handle1,
-            .last = handle2,
-        }, {
-            .first = handle4,
-            .last = handle4,
-        }, {
-            .first = 0,
-        } }));
-
-}
-
-static void
-ble_att_svr_test_misc_read_type(uint16_t mtu)
-{
-    struct ble_att_read_type_req req;
-    uint16_t conn_handle;
-    uint8_t buf[BLE_ATT_READ_TYPE_REQ_SZ_16];
-    int rc;
-
-    conn_handle = ble_att_svr_test_misc_init(mtu);
-
-    /*** Start handle of 0. */
-    req.batq_start_handle = 0;
-    req.batq_end_handle = 0;
-
-    ble_att_read_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_READ_TYPE_REQ, 0,
-        BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** Start handle > end handle. */
-    req.batq_start_handle = 101;
-    req.batq_end_handle = 100;
-
-    ble_att_read_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_READ_TYPE_REQ, 101,
-        BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** No attributes. */
-    req.batq_start_handle = 1;
-    req.batq_end_handle = 0xffff;
-
-    ble_att_read_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_READ_TYPE_REQ, 1,
-        BLE_ATT_ERR_ATTR_NOT_FOUND);
-
-    /*** Range too late. */
-    ble_att_svr_test_misc_register_group_attrs();
-    req.batq_start_handle = 200;
-    req.batq_end_handle = 300;
-
-    ble_att_read_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_READ_TYPE_REQ, 200,
-        BLE_ATT_ERR_ATTR_NOT_FOUND);
-
-    /*** One characteristic from one service. */
-    req.batq_start_handle = 1;
-    req.batq_end_handle = 2;
-
-    ble_att_read_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_CHARACTERISTIC);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_type_rsp(
-        ((struct ble_att_svr_test_type_entry[]) { {
-            .handle = 2,
-            .value = (uint8_t[]){ 0x01, 0x11 },
-            .value_len = 2,
-        }, {
-            .handle = 0,
-        } }));
-
-    /*** Both characteristics from one service. */
-    req.batq_start_handle = 1;
-    req.batq_end_handle = 10;
-
-    ble_att_read_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_CHARACTERISTIC);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_type_rsp(
-        ((struct ble_att_svr_test_type_entry[]) { {
-            .handle = 2,
-            .value = (uint8_t[]){ 0x01, 0x11 },
-            .value_len = 2,
-        }, {
-            .handle = 4,
-            .value = (uint8_t[]){ 0x03, 0x11 },
-            .value_len = 2,
-        }, {
-            .handle = 0,
-        } }));
-
-    /*** Ensure 16-bit and 128-bit values are retrieved separately. */
-    req.batq_start_handle = 11;
-    req.batq_end_handle = 0xffff;
-
-    ble_att_read_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_CHARACTERISTIC);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_type_rsp(
-        ((struct ble_att_svr_test_type_entry[]) { {
-            .handle = 12,
-            .value = (uint8_t[]){ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
-            .value_len = 16,
-        }, {
-            .handle = 0,
-        } }));
-
-    req.batq_start_handle = 13;
-    req.batq_end_handle = 0xffff;
-
-    ble_att_read_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_CHARACTERISTIC);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_type_rsp(
-        ((struct ble_att_svr_test_type_entry[]) { {
-            .handle = 14,
-            .value = (uint8_t[]){ 0x55, 0x55 },
-            .value_len = 2,
-        }, {
-            .handle = 0,
-        } }));
-
-    req.batq_start_handle = 15;
-    req.batq_end_handle = 0xffff;
-
-    ble_att_read_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_CHARACTERISTIC);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_type_rsp(
-        ((struct ble_att_svr_test_type_entry[]) { {
-            .handle = 16,
-            .value = (uint8_t[]){ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2 },
-            .value_len = 16,
-        }, {
-            .handle = 0,
-        } }));
-
-    /*** Read until the end of the attribute list. */
-    req.batq_start_handle = 17;
-    req.batq_end_handle = 0xffff;
-
-    ble_att_read_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_CHARACTERISTIC);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_type_rsp(
-        ((struct ble_att_svr_test_type_entry[]) { {
-            .handle = 18,
-            .value = (uint8_t[]){ 0x66, 0x66 },
-            .value_len = 2,
-        }, {
-            .handle = 20,
-            .value = (uint8_t[]){ 0x77, 0x77 },
-            .value_len = 2,
-        }, {
-            .handle = 22,
-            .value = (uint8_t[]){ 0x88, 0x88 },
-            .value_len = 2,
-        }, {
-            .handle = 24,
-            .value = (uint8_t[]){ 0x99, 0x99 },
-            .value_len = 2,
-        }, {
-            .handle = 0,
-        } }));
-
-}
-
-TEST_CASE(ble_att_svr_test_read_type)
-{
-    ble_att_svr_test_misc_read_type(0);
-    ble_att_svr_test_misc_read_type(128);
-}
-
-TEST_CASE(ble_att_svr_test_read_group_type)
-{
-    struct ble_att_read_group_type_req req;
-    uint16_t conn_handle;
-    uint8_t buf[BLE_ATT_READ_GROUP_TYPE_REQ_SZ_16];
-    int rc;
-
-    conn_handle = ble_att_svr_test_misc_init(128);
-
-    /*** Start handle of 0. */
-    req.bagq_start_handle = 0;
-    req.bagq_end_handle = 0;
-
-    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_READ_GROUP_TYPE_REQ, 0,
-        BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** Start handle > end handle. */
-    req.bagq_start_handle = 101;
-    req.bagq_end_handle = 100;
-
-    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_READ_GROUP_TYPE_REQ, 101,
-        BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** Invalid group UUID (0x1234). */
-    req.bagq_start_handle = 110;
-    req.bagq_end_handle = 150;
-
-    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ, 0x1234);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_READ_GROUP_TYPE_REQ, 110,
-        BLE_ATT_ERR_UNSUPPORTED_GROUP);
-
-    /*** No attributes. */
-    req.bagq_start_handle = 1;
-    req.bagq_end_handle = 0xffff;
-
-    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_READ_GROUP_TYPE_REQ, 1,
-        BLE_ATT_ERR_ATTR_NOT_FOUND);
-
-    /*** Range too late. */
-    ble_att_svr_test_misc_register_group_attrs();
-    req.bagq_start_handle = 200;
-    req.bagq_end_handle = 300;
-
-    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc != 0);
-    ble_att_svr_test_misc_verify_tx_err_rsp(
-        BLE_ATT_OP_READ_GROUP_TYPE_REQ, 200,
-        BLE_ATT_ERR_ATTR_NOT_FOUND);
-
-    /*** One 16-bit UUID service. */
-    req.bagq_start_handle = 1;
-    req.bagq_end_handle = 5;
-
-    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_group_type_rsp(
-        ((struct ble_att_svr_test_group_type_entry[]) { {
-            .start_handle = 1,
-            .end_handle = 5,
-            .uuid16 = 0x1122,
-        }, {
-            .start_handle = 0,
-        } }));
-
-    /*** Two 16-bit UUID services. */
-    req.bagq_start_handle = 1;
-    req.bagq_end_handle = 10;
-
-    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_group_type_rsp(
-        ((struct ble_att_svr_test_group_type_entry[]) { {
-            .start_handle = 1,
-            .end_handle = 5,
-            .uuid16 = 0x1122,
-        }, {
-            .start_handle = 6,
-            .end_handle = 10,
-            .uuid16 = 0x2233,
-        }, {
-            .start_handle = 0,
-        } }));
-
-    /*** Two 16-bit UUID services; ensure 128-bit service not returned. */
-    req.bagq_start_handle = 1;
-    req.bagq_end_handle = 100;
-
-    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_group_type_rsp(
-        ((struct ble_att_svr_test_group_type_entry[]) { {
-            .start_handle = 1,
-            .end_handle = 5,
-            .uuid16 = 0x1122,
-        }, {
-            .start_handle = 6,
-            .end_handle = 10,
-            .uuid16 = 0x2233,
-        }, {
-            .start_handle = 0,
-        } }));
-
-    /*** One 128-bit service. */
-    req.bagq_start_handle = 11;
-    req.bagq_end_handle = 100;
-
-    ble_att_read_group_type_req_write(buf, sizeof buf, &req);
-    htole16(buf + BLE_ATT_READ_GROUP_TYPE_REQ_BASE_SZ,
-            BLE_ATT_UUID_PRIMARY_SERVICE);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-    ble_att_svr_test_misc_verify_tx_read_group_type_rsp(
-        ((struct ble_att_svr_test_group_type_entry[]) { {
-            .start_handle = 11,
-            .end_handle = 19,
-            .uuid128 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16},
-        }, {
-            .start_handle = 0,
-        } }));
-
-}
-
-TEST_CASE(ble_att_svr_test_prep_write)
-{
-    struct ble_hs_conn *conn;
-    uint16_t conn_handle;
-    int i;
-
-    static uint8_t data[1024];
-
-    conn_handle = ble_att_svr_test_misc_init(205);
-
-    /* Initialize some attribute data. */
-    for (i = 0; i < sizeof data; i++) {
-        data[i] = i;
-    }
-
-    /* Register two writable attributes. */
-    ble_att_svr_test_misc_register_uuid16(0x1234, HA_FLAG_PERM_RW, 1,
-                                          ble_att_svr_test_misc_attr_fn_w_1);
-    ble_att_svr_test_misc_register_uuid16(0x8989, HA_FLAG_PERM_RW, 2,
-                                          ble_att_svr_test_misc_attr_fn_w_2);
-
-    /* 3: not writable. */
-    ble_att_svr_test_misc_register_uuid16(0xabab, BLE_ATT_F_READ, 3,
-                                          ble_att_svr_test_misc_attr_fn_r_1);
-    /* 4: Encryption required. */
-    ble_att_svr_test_misc_register_uuid16(
-        0xabac, BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC, 4,
-        ble_att_svr_test_misc_attr_fn_w_1);
-
-    /* 5: Encryption+authentication required. */
-    ble_att_svr_test_misc_register_uuid16(
-        0xabad, BLE_ATT_F_WRITE | BLE_ATT_F_WRITE_ENC | BLE_ATT_F_WRITE_AUTHEN,
-        5, ble_att_svr_test_misc_attr_fn_w_1);
-
-    /* 6: Write callback always fails. */
-    ble_att_svr_test_misc_register_uuid16(
-        0xabae, BLE_ATT_F_WRITE, 6, ble_att_svr_test_misc_attr_fn_w_fail);
-
-    /*** Empty write succeeds. */
-    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
-                                     0, 0);
-
-    /*** Empty cancel succeeds. */
-    ble_att_svr_test_misc_exec_write(conn_handle, 0, 0, 0);
-
-    /*** Failure for prep write to nonexistent attribute. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 53525, 0, data, 10,
-                                     BLE_ATT_ERR_INVALID_HANDLE);
-
-    /*** Failure due to write-not-permitted. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 3, 0, data, 35,
-                                     BLE_ATT_ERR_WRITE_NOT_PERMITTED);
-
-    /*** Failure due to insufficient authentication (encryption required). */
-    ble_att_svr_test_misc_prep_write(conn_handle, 4, 0, data, 1,
-                                     BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
-
-    /*** Encrypt connection; ensure previous prep write now succeeds. */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(2);
-    TEST_ASSERT_FATAL(conn != NULL);
-    conn->bhc_sec_state.encrypted = 1;
-    ble_hs_unlock();
-
-    ble_att_svr_test_misc_prep_write(conn_handle, 4, 0, data, 1, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, 0, 0, 0);
-
-    /*** Failure due to insufficient authentication (not authenticated). */
-    ble_att_svr_test_misc_prep_write(conn_handle, 5, 0, data, 35,
-                                     BLE_ATT_ERR_INSUFFICIENT_AUTHEN);
-
-    /*** Failure for write starting at nonzero offset. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 1, data, 10, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
-                                     BLE_ATT_ERR_INVALID_OFFSET, 1);
-    ble_att_svr_test_misc_verify_w_1(NULL, 0);
-
-    /*** Success for clear starting at nonzero offset. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 1, data, 10, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, 0, 0, 0);
-    ble_att_svr_test_misc_verify_w_1(NULL, 0);
-
-    /*** Failure for write with gap. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 10, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 11, data, 10, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
-                                     BLE_ATT_ERR_INVALID_OFFSET, 1);
-    ble_att_svr_test_misc_verify_w_1(NULL, 0);
-
-    /*** Success for clear with gap. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 10, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 11, data, 10, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, 0, 0, 0);
-    ble_att_svr_test_misc_verify_w_1(NULL, 0);
-
-    /*** Failure for overlong write. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 200, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 200, data + 200, 200, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 400, data + 400, 200, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
-                                     BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN, 1);
-    ble_att_svr_test_misc_verify_w_1(NULL, 0);
-
-    /*** Successful two part write. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 20, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 20, data + 20, 20, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
-                                     0, 0);
-    ble_att_svr_test_misc_verify_w_1(data, 40);
-
-    /*** Successful three part write. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 35, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 35, data + 35, 43, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 78, data + 78, 1, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
-                                     0, 0);
-    ble_att_svr_test_misc_verify_w_1(data, 79);
-
-    /*** Successful two part write to two attributes. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 7, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 7, data + 7, 10, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 2, 0, data, 20, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 2, 20, data + 20, 10, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
-                                     0, 0);
-    ble_att_svr_test_misc_verify_w_1(data, 17);
-    ble_att_svr_test_misc_verify_w_2(data, 30);
-
-    /*** Fail write to second attribute; ensure first write doesn't occur. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 5, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 5, data + 5, 2, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 2, 0, data, 11, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 2, 12, data + 11, 19, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
-                                     BLE_ATT_ERR_INVALID_OFFSET, 2);
-    ble_att_svr_test_misc_verify_w_1(data, 17);
-    ble_att_svr_test_misc_verify_w_2(data, 30);
-
-    /*** Successful out of order write to two attributes. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 0, data, 9, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 2, 0, data, 18, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 1, 9, data + 9, 3, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 2, 18, data + 18, 43, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
-                                     0, 0);
-    ble_att_svr_test_misc_verify_w_1(data, 12);
-    ble_att_svr_test_misc_verify_w_2(data, 61);
-
-    /*** Fail due to attribute callback error. */
-    ble_att_svr_test_misc_prep_write(conn_handle, 6, 0, data, 35, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 6, 35, data + 35, 43, 0);
-    ble_att_svr_test_misc_prep_write(conn_handle, 6, 78, data + 78, 1, 0);
-    ble_att_svr_test_misc_exec_write(conn_handle, BLE_ATT_EXEC_WRITE_F_CONFIRM,
-                                     BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN, 6);
-}
-
-TEST_CASE(ble_att_svr_test_notify)
-{
-    uint16_t conn_handle;
-
-    conn_handle = ble_att_svr_test_misc_init(0);
-
-    /*** Successful notifies; verify callback is executed. */
-    /* 3-length attribute. */
-    ble_att_svr_test_misc_verify_notify(conn_handle, 10,
-                                        (uint8_t[]) { 1, 2, 3 }, 3, 1);
-    /* 1-length attribute. */
-    ble_att_svr_test_misc_verify_notify(conn_handle, 1,
-                                        (uint8_t[]) { 0xff }, 1, 1);
-    /* 0-length attribute. */
-    ble_att_svr_test_misc_verify_notify(conn_handle, 43, NULL, 0, 1);
-
-    /*** Bad notifies; verify callback is not executed. */
-    /* Attribute handle of 0. */
-    ble_att_svr_test_misc_verify_notify(conn_handle, 0,
-                                        (uint8_t[]) { 1, 2, 3 }, 3, 0);
-
-}
-
-TEST_CASE(ble_att_svr_test_indicate)
-{
-    uint16_t conn_handle;
-
-    conn_handle = ble_att_svr_test_misc_init(0);
-
-    /*** Successful indicates; verify callback is executed. */
-    /* 3-length attribute. */
-    ble_att_svr_test_misc_verify_indicate(conn_handle, 10,
-                                          (uint8_t[]) { 1, 2, 3 }, 3, 1);
-    /* 1-length attribute. */
-    ble_att_svr_test_misc_verify_indicate(conn_handle, 1,
-                                          (uint8_t[]) { 0xff }, 1, 1);
-    /* 0-length attribute. */
-    ble_att_svr_test_misc_verify_indicate(conn_handle, 43, NULL, 0, 1);
-
-    /*** Bad indicates; verify callback is not executed. */
-    /* Attribute handle of 0. */
-    ble_att_svr_test_misc_verify_indicate(conn_handle, 0,
-                                          (uint8_t[]) { 1, 2, 3 }, 3, 0);
-
-}
-
-TEST_SUITE(ble_att_svr_suite)
-{
-    /* When checking for mbuf leaks, ensure no stale prep entries. */
-    static struct ble_hs_test_util_mbuf_params mbuf_params = {
-        .prev_tx = 1,
-        .rx_queue = 1,
-        .prep_list = 0,
-    };
-
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, &mbuf_params);
-
-    ble_att_svr_test_mtu();
-    ble_att_svr_test_read();
-    ble_att_svr_test_read_blob();
-    ble_att_svr_test_read_mult();
-    ble_att_svr_test_write();
-    ble_att_svr_test_find_info();
-    ble_att_svr_test_find_type_value();
-    ble_att_svr_test_read_type();
-    ble_att_svr_test_read_group_type();
-    ble_att_svr_test_prep_write();
-    ble_att_svr_test_notify();
-    ble_att_svr_test_indicate();
-}
-
-int
-ble_att_svr_test_all(void)
-{
-    ble_att_svr_suite();
-
-    return tu_any_failed;
-}


[25/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_l2cap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap.c b/net/nimble/host/src/ble_l2cap.c
index 43ccd6f..0dae650 100644
--- a/net/nimble/host/src/ble_l2cap.c
+++ b/net/nimble/host/src/ble_l2cap.c
@@ -19,6 +19,7 @@
 
 #include <string.h>
 #include <errno.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "nimble/ble.h"
 #include "nimble/hci_common.h"
@@ -29,7 +30,10 @@ _Static_assert(sizeof (struct ble_l2cap_hdr) == BLE_L2CAP_HDR_SZ,
 
 struct os_mempool ble_l2cap_chan_pool;
 
-static void *ble_l2cap_chan_mem;
+static os_membuf_t ble_l2cap_chan_mem[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_L2CAP_MAX_CHANS),
+                     sizeof (struct ble_l2cap_chan))
+];
 
 STATS_SECT_DECL(ble_l2cap_stats) ble_l2cap_stats;
 STATS_NAME_START(ble_l2cap_stats)
@@ -292,57 +296,34 @@ ble_l2cap_tx(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan,
     return 0;
 }
 
-static void
-ble_l2cap_free_mem(void)
-{
-    free(ble_l2cap_chan_mem);
-    ble_l2cap_chan_mem = NULL;
-}
-
 int
 ble_l2cap_init(void)
 {
     int rc;
 
-    ble_l2cap_free_mem();
-
-    ble_l2cap_chan_mem = malloc(
-        OS_MEMPOOL_BYTES(ble_hs_cfg.max_l2cap_chans,
-                         sizeof (struct ble_l2cap_chan)));
-    if (ble_l2cap_chan_mem == NULL) {
-        rc = BLE_HS_ENOMEM;
-        goto err;
-    }
-
-    rc = os_mempool_init(&ble_l2cap_chan_pool, ble_hs_cfg.max_l2cap_chans,
+    rc = os_mempool_init(&ble_l2cap_chan_pool, MYNEWT_VAL(BLE_L2CAP_MAX_CHANS),
                          sizeof (struct ble_l2cap_chan),
                          ble_l2cap_chan_mem, "ble_l2cap_chan_pool");
     if (rc != 0) {
-        rc = BLE_HS_EOS;
-        goto err;
+        return BLE_HS_EOS;
     }
 
     rc = ble_l2cap_sig_init();
     if (rc != 0) {
-        goto err;
+        return rc;
     }
 
     rc = ble_sm_init();
     if (rc != 0) {
-        goto err;
+        return rc;
     }
 
     rc = stats_init_and_reg(
         STATS_HDR(ble_l2cap_stats), STATS_SIZE_INIT_PARMS(ble_l2cap_stats,
         STATS_SIZE_32), STATS_NAME_INIT_PARMS(ble_l2cap_stats), "ble_l2cap");
     if (rc != 0) {
-        rc = BLE_HS_EOS;
-        goto err;
+        return BLE_HS_EOS;
     }
 
     return 0;
-
-err:
-    ble_l2cap_free_mem();
-    return rc;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_l2cap_sig.c b/net/nimble/host/src/ble_l2cap_sig.c
index 4838067..70a3b1d 100644
--- a/net/nimble/host/src/ble_l2cap_sig.c
+++ b/net/nimble/host/src/ble_l2cap_sig.c
@@ -102,7 +102,11 @@ static ble_l2cap_sig_rx_fn * const ble_l2cap_sig_dispatch[] = {
 
 static uint8_t ble_l2cap_sig_cur_id;
 
-static void *ble_l2cap_sig_proc_mem;
+static os_membuf_t ble_l2cap_sig_proc_mem[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_L2CAP_SIG_MAX_PROCS),
+                    sizeof (struct ble_l2cap_sig_proc))
+];
+
 static struct os_mempool ble_l2cap_sig_proc_pool;
 
 /*****************************************************************************
@@ -112,7 +116,7 @@ static struct os_mempool ble_l2cap_sig_proc_pool;
 static void
 ble_l2cap_sig_dbg_assert_proc_not_inserted(struct ble_l2cap_sig_proc *proc)
 {
-#if BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     struct ble_l2cap_sig_proc *cur;
 
     STAILQ_FOREACH(cur, &ble_l2cap_sig_procs, next) {
@@ -607,32 +611,16 @@ ble_l2cap_sig_init(void)
 {
     int rc;
 
-    free(ble_l2cap_sig_proc_mem);
-
     STAILQ_INIT(&ble_l2cap_sig_procs);
 
-    if (ble_hs_cfg.max_l2cap_sig_procs > 0) {
-        ble_l2cap_sig_proc_mem = malloc(
-            OS_MEMPOOL_BYTES(ble_hs_cfg.max_l2cap_sig_procs,
-                             sizeof (struct ble_l2cap_sig_proc)));
-        if (ble_l2cap_sig_proc_mem == NULL) {
-            rc = BLE_HS_ENOMEM;
-            goto err;
-        }
-
-        rc = os_mempool_init(&ble_l2cap_sig_proc_pool,
-                             ble_hs_cfg.max_l2cap_sig_procs,
-                             sizeof (struct ble_l2cap_sig_proc),
-                             ble_l2cap_sig_proc_mem,
-                             "ble_l2cap_sig_proc_pool");
-        if (rc != 0) {
-            goto err;
-        }
+    rc = os_mempool_init(&ble_l2cap_sig_proc_pool,
+                         MYNEWT_VAL(BLE_L2CAP_SIG_MAX_PROCS),
+                         sizeof (struct ble_l2cap_sig_proc),
+                         ble_l2cap_sig_proc_mem,
+                         "ble_l2cap_sig_proc_pool");
+    if (rc != 0) {
+        return rc;
     }
 
     return 0;
-
-err:
-    free(ble_l2cap_sig_proc_mem);
-    return rc;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm.c b/net/nimble/host/src/ble_sm.c
index f2a7b5c..edc140a 100644
--- a/net/nimble/host/src/ble_sm.c
+++ b/net/nimble/host/src/ble_sm.c
@@ -49,7 +49,7 @@
 #include "host/ble_sm.h"
 #include "ble_hs_priv.h"
 
-#if NIMBLE_OPT(SM)
+#if MYNEWT_VAL(BLE_SM)
 
 /** Procedure timeout; 30 seconds. */
 #define BLE_SM_TIMEOUT_OS_TICKS             (30 * OS_TICKS_PER_SEC)
@@ -85,7 +85,7 @@ static ble_sm_rx_fn * const ble_sm_dispatch[] = {
    [BLE_SM_OP_SIGN_INFO] = ble_sm_sign_info_rx,
    [BLE_SM_OP_SEC_REQ] = ble_sm_sec_req_rx,
    [BLE_SM_OP_PAIR_KEYPRESS_NOTIFY] = ble_sm_rx_noop,
-#if NIMBLE_OPT_SM_SC
+#if MYNEWT_VAL(BLE_SM_SC)
    [BLE_SM_OP_PAIR_PUBLIC_KEY] = ble_sm_sc_public_key_rx,
    [BLE_SM_OP_PAIR_DHKEY_CHECK] = ble_sm_sc_dhkey_check_rx,
 #else
@@ -118,7 +118,7 @@ ble_sm_state_dispatch[BLE_SM_PROC_STATE_CNT] = {
     [BLE_SM_PROC_STATE_ENC_RESTORE]   = ble_sm_enc_restore_exec,
     [BLE_SM_PROC_STATE_KEY_EXCH]      = ble_sm_key_exch_exec,
     [BLE_SM_PROC_STATE_SEC_REQ]       = ble_sm_sec_req_exec,
-#if NIMBLE_OPT_SM_SC
+#if MYNEWT_VAL(BLE_SM_SC)
     [BLE_SM_PROC_STATE_PUBLIC_KEY]    = ble_sm_sc_public_key_exec,
     [BLE_SM_PROC_STATE_DHKEY_CHECK]   = ble_sm_sc_dhkey_check_exec,
 #else
@@ -127,7 +127,11 @@ ble_sm_state_dispatch[BLE_SM_PROC_STATE_CNT] = {
 #endif
 };
 
-static void *ble_sm_proc_mem;
+static os_membuf_t ble_sm_proc_mem[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_SM_MAX_PROCS),
+                    sizeof (struct ble_sm_proc))
+];
+
 static struct os_mempool ble_sm_proc_pool;
 
 /* Maintains the list of active security manager procedures. */
@@ -140,7 +144,7 @@ static void ble_sm_pair_cfg(struct ble_sm_proc *proc);
  * $debug                                                                    *
  *****************************************************************************/
 
-#ifdef BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
 
 static uint8_t ble_sm_dbg_next_pair_rand[16];
 static uint8_t ble_sm_dbg_next_pair_rand_set;
@@ -212,7 +216,7 @@ ble_sm_dbg_num_procs(void)
 
     cnt = 0;
     STAILQ_FOREACH(proc, &ble_sm_procs, next) {
-        BLE_HS_DBG_ASSERT(cnt < ble_hs_cfg.max_l2cap_sm_procs);
+        BLE_HS_DBG_ASSERT(cnt < MYNEWT_VAL(BLE_SM_MAX_PROCS));
         cnt++;
     }
 
@@ -224,7 +228,7 @@ ble_sm_dbg_num_procs(void)
 static void
 ble_sm_dbg_assert_no_cycles(void)
 {
-#if BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     ble_sm_dbg_num_procs();
 #endif
 }
@@ -232,7 +236,7 @@ ble_sm_dbg_assert_no_cycles(void)
 static void
 ble_sm_dbg_assert_not_inserted(struct ble_sm_proc *proc)
 {
-#if BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     struct ble_sm_proc *cur;
 
     STAILQ_FOREACH(cur, &ble_sm_procs, next) {
@@ -250,7 +254,7 @@ ble_sm_gen_pair_rand(uint8_t *pair_rand)
 {
     int rc;
 
-#ifdef BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     if (ble_sm_dbg_next_pair_rand_set) {
         ble_sm_dbg_next_pair_rand_set = 0;
         memcpy(pair_rand, ble_sm_dbg_next_pair_rand,
@@ -272,7 +276,7 @@ ble_sm_gen_ediv(uint16_t *ediv)
 {
     int rc;
 
-#ifdef BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     if (ble_sm_dbg_next_ediv_set) {
         ble_sm_dbg_next_ediv_set = 0;
         *ediv = ble_sm_dbg_next_ediv;
@@ -293,7 +297,7 @@ ble_sm_gen_master_id_rand(uint64_t *master_id_rand)
 {
     int rc;
 
-#ifdef BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     if (ble_sm_dbg_next_master_id_rand_set) {
         ble_sm_dbg_next_master_id_rand_set = 0;
         *master_id_rand = ble_sm_dbg_next_master_id_rand;
@@ -314,7 +318,7 @@ ble_sm_gen_ltk(struct ble_sm_proc *proc, uint8_t *ltk)
 {
     int rc;
 
-#ifdef BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     if (ble_sm_dbg_next_ltk_set) {
         ble_sm_dbg_next_ltk_set = 0;
         memcpy(ltk, ble_sm_dbg_next_ltk,
@@ -336,7 +340,7 @@ ble_sm_gen_csrk(struct ble_sm_proc *proc, uint8_t *csrk)
 {
     int rc;
 
-#ifdef BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     if (ble_sm_dbg_next_csrk_set) {
         ble_sm_dbg_next_csrk_set = 0;
         memcpy(csrk, ble_sm_dbg_next_csrk,
@@ -358,7 +362,7 @@ ble_sm_gen_pub_priv(void *pub, uint32_t *priv)
 {
     int rc;
 
-#ifdef BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     if (ble_sm_dbg_sc_keys_set) {
         ble_sm_dbg_sc_keys_set = 0;
         memcpy(pub, ble_sm_dbg_sc_pub_key, sizeof ble_sm_dbg_sc_pub_key);
@@ -644,7 +648,7 @@ ble_sm_proc_find(uint16_t conn_handle, uint8_t state, int is_initiator,
 static void
 ble_sm_insert(struct ble_sm_proc *proc)
 {
-#ifdef BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     struct ble_sm_proc *cur;
 
     STAILQ_FOREACH(cur, &ble_sm_procs, next) {
@@ -2371,46 +2375,25 @@ ble_sm_connection_broken(uint16_t conn_handle)
     ble_sm_process_result(conn_handle, &res);
 }
 
-static void
-ble_sm_free_mem(void)
-{
-    free(ble_sm_proc_mem);
-}
-
 int
 ble_sm_init(void)
 {
     int rc;
 
-    ble_sm_free_mem();
-
     STAILQ_INIT(&ble_sm_procs);
 
-    if (ble_hs_cfg.max_l2cap_sm_procs > 0) {
-        ble_sm_proc_mem = malloc(
-            OS_MEMPOOL_BYTES(ble_hs_cfg.max_l2cap_sm_procs,
-                             sizeof (struct ble_sm_proc)));
-        if (ble_sm_proc_mem == NULL) {
-            rc = BLE_HS_ENOMEM;
-            goto err;
-        }
-        rc = os_mempool_init(&ble_sm_proc_pool,
-                             ble_hs_cfg.max_l2cap_sm_procs,
-                             sizeof (struct ble_sm_proc),
-                             ble_sm_proc_mem,
-                             "ble_sm_proc_pool");
-        if (rc != 0) {
-            goto err;
-        }
+    rc = os_mempool_init(&ble_sm_proc_pool,
+                         MYNEWT_VAL(BLE_SM_MAX_PROCS),
+                         sizeof (struct ble_sm_proc),
+                         ble_sm_proc_mem,
+                         "ble_sm_proc_pool");
+    if (rc != 0) {
+        return rc;
     }
 
     ble_sm_sc_init();
 
     return 0;
-
-err:
-    ble_sm_free_mem();
-    return rc;
 }
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_sm_alg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_alg.c b/net/nimble/host/src/ble_sm_alg.c
index a2b0fcf..6b30bd8 100644
--- a/net/nimble/host/src/ble_sm_alg.c
+++ b/net/nimble/host/src/ble_sm_alg.c
@@ -20,62 +20,27 @@
 
 #include <inttypes.h>
 #include <string.h>
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(BLE_SM)
+
+#include "nimble/ble.h"
+#include "nimble/nimble_opt.h"
+#include "ble_hs_priv.h"
 #include "mbedtls/aes.h"
+
+#if MYNEWT_VAL(BLE_SM_SC)
+
 #include "tinycrypt/aes.h"
 #include "tinycrypt/constants.h"
 #include "tinycrypt/utils.h"
 #include "tinycrypt/cmac_mode.h"
 #include "tinycrypt/ecc_dh.h"
-#include "nimble/ble.h"
-#include "nimble/nimble_opt.h"
-#include "ble_hs_priv.h"
 
-#if NIMBLE_OPT(SM)
+#endif
 
 static mbedtls_aes_context ble_sm_alg_ctxt;
 
-/* based on Core Specification 4.2 Vol 3. Part H 2.3.5.6.1 */
-static const uint32_t ble_sm_alg_dbg_priv_key[8] = {
-    0xcd3c1abd, 0x5899b8a6, 0xeb40b799, 0x4aff607b, 0xd2103f50, 0x74c9b3e3,
-    0xa3c55f38, 0x3f49f6d4
-};
-
-static const uint8_t ble_sm_alg_dbg_pub_key[64] = {
-    0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
-    0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
-    0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
-    0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20,
-    0x8b, 0xd2, 0x89, 0x15, 0xd0, 0x8e, 0x1c, 0x74,
-    0x24, 0x30, 0xed, 0x8f, 0xc2, 0x45, 0x63, 0x76,
-    0x5c, 0x15, 0x52, 0x5a, 0xbf, 0x9a, 0x32, 0x63,
-    0x6d, 0xeb, 0x2a, 0x65, 0x49, 0x9c, 0x80, 0xdc
-};
-
-static const uint8_t ble_sm_alg_dbg_f4[16] = {
-    0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
-    0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2,
-};
-
-static const uint8_t ble_sm_alg_dbg_f5[32] = {
-    0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
-    0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29,
-    0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05, 0x98,
-    0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79, 0x86, 0x69,
-};
-
-static const uint8_t ble_sm_alg_dbg_f6[16] = {
-    0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
-    0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3,
-};
-
-static void
-ble_sm_alg_log_buf(const char *name, const uint8_t *buf, int len)
-{
-    BLE_HS_LOG(DEBUG, "    %s=", name);
-    ble_hs_log_flat_buf(buf, len);
-    BLE_HS_LOG(DEBUG, "\n");
-}
-
 static void
 ble_sm_alg_xor_128(uint8_t *p, uint8_t *q, uint8_t *r)
 {
@@ -113,36 +78,6 @@ ble_sm_alg_encrypt(uint8_t *key, uint8_t *plaintext, uint8_t *enc_data)
     return 0;
 }
 
-/**
- * Cypher based Message Authentication Code (CMAC) with AES 128 bit
- *
- * @param key                   128-bit key.
- * @param in                    Message to be authenticated.
- * @param len                   Length of the message in octets.
- * @param out                   Output; message authentication code.
- */
-static int
-ble_sm_alg_aes_cmac(const uint8_t *key, const uint8_t *in, size_t len,
-                    uint8_t *out)
-{
-    struct tc_aes_key_sched_struct sched;
-    struct tc_cmac_struct state;
-
-    if (tc_cmac_setup(&state, key, &sched) == TC_FAIL) {
-        return BLE_HS_EUNKNOWN;
-    }
-
-    if (tc_cmac_update(&state, in, len) == TC_FAIL) {
-        return BLE_HS_EUNKNOWN;
-    }
-
-    if (tc_cmac_final(out, &state) == TC_FAIL) {
-        return BLE_HS_EUNKNOWN;
-    }
-
-    return 0;
-}
-
 int
 ble_sm_alg_s1(uint8_t *k, uint8_t *r1, uint8_t *r2, uint8_t *out)
 {
@@ -248,6 +183,46 @@ done:
     return rc;
 }
 
+#if MYNEWT_VAL(BLE_SM_SC)
+
+static void
+ble_sm_alg_log_buf(const char *name, const uint8_t *buf, int len)
+{
+    BLE_HS_LOG(DEBUG, "    %s=", name);
+    ble_hs_log_flat_buf(buf, len);
+    BLE_HS_LOG(DEBUG, "\n");
+}
+
+/**
+ * Cypher based Message Authentication Code (CMAC) with AES 128 bit
+ *
+ * @param key                   128-bit key.
+ * @param in                    Message to be authenticated.
+ * @param len                   Length of the message in octets.
+ * @param out                   Output; message authentication code.
+ */
+static int
+ble_sm_alg_aes_cmac(const uint8_t *key, const uint8_t *in, size_t len,
+                    uint8_t *out)
+{
+    struct tc_aes_key_sched_struct sched;
+    struct tc_cmac_struct state;
+
+    if (tc_cmac_setup(&state, key, &sched) == TC_FAIL) {
+        return BLE_HS_EUNKNOWN;
+    }
+
+    if (tc_cmac_update(&state, in, len) == TC_FAIL) {
+        return BLE_HS_EUNKNOWN;
+    }
+
+    if (tc_cmac_final(out, &state) == TC_FAIL) {
+        return BLE_HS_EUNKNOWN;
+    }
+
+    return 0;
+}
+
 int
 ble_sm_alg_f4(uint8_t *u, uint8_t *v, uint8_t *x, uint8_t z,
               uint8_t *out_enc_data)
@@ -465,6 +440,12 @@ ble_sm_alg_gen_dhkey(uint8_t *peer_pub_key_x, uint8_t *peer_pub_key_y,
     return 0;
 }
 
+/* based on Core Specification 4.2 Vol 3. Part H 2.3.5.6.1 */
+static const uint32_t ble_sm_alg_dbg_priv_key[8] = {
+    0xcd3c1abd, 0x5899b8a6, 0xeb40b799, 0x4aff607b, 0xd2103f50, 0x74c9b3e3,
+    0xa3c55f38, 0x3f49f6d4
+};
+
 /**
  * pub: 64 bytes
  * priv: 32 bytes
@@ -497,3 +478,4 @@ ble_sm_alg_gen_key_pair(void *pub, uint32_t *priv)
 }
 
 #endif
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_sm_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_cmd.c b/net/nimble/host/src/ble_sm_cmd.c
index 9854bde..f510b47 100644
--- a/net/nimble/host/src/ble_sm_cmd.c
+++ b/net/nimble/host/src/ble_sm_cmd.c
@@ -25,7 +25,7 @@
 #include "host/ble_sm.h"
 #include "ble_hs_priv.h"
 
-#if NIMBLE_OPT(SM)
+#if NIMBLE_BLE_SM
 
 static int
 ble_sm_tx(uint16_t conn_handle, struct os_mbuf *txom)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_sm_lgcy.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_lgcy.c b/net/nimble/host/src/ble_sm_lgcy.c
index d844a6f..c884fb4 100644
--- a/net/nimble/host/src/ble_sm_lgcy.c
+++ b/net/nimble/host/src/ble_sm_lgcy.c
@@ -25,7 +25,7 @@
 #include "host/ble_sm.h"
 #include "ble_hs_priv.h"
 
-#if NIMBLE_OPT(SM)
+#if NIMBLE_BLE_SM
 
 /**
  * Create some shortened names for the passkey actions so that the table is

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_sm_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_priv.h b/net/nimble/host/src/ble_sm_priv.h
index 80798e1..4c7ab48 100644
--- a/net/nimble/host/src/ble_sm_priv.h
+++ b/net/nimble/host/src/ble_sm_priv.h
@@ -21,6 +21,7 @@
 #define H_BLE_SM_PRIV_
 
 #include <inttypes.h>
+#include "syscfg/syscfg.h"
 #include "os/queue.h"
 #include "nimble/nimble_opt.h"
 
@@ -196,7 +197,7 @@ struct ble_sm_dhkey_check {
     uint8_t value[16];
 };
 
-#if NIMBLE_OPT(SM)
+#if NIMBLE_BLE_SM
 
 #define BLE_SM_PROC_STATE_NONE              ((uint8_t)-1)
     
@@ -263,7 +264,7 @@ struct ble_sm_proc {
     struct ble_sm_keys our_keys;
     struct ble_sm_keys peer_keys;
 
-#if NIMBLE_OPT(SM_SC)
+#if MYNEWT_VAL(BLE_SM_SC)
     /* Secure connections. */
     uint8_t passkey_bits_exchanged;
     uint8_t ri;
@@ -284,7 +285,7 @@ struct ble_sm_result {
     unsigned restore:1;
 };
 
-#ifdef BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
 void ble_sm_dbg_set_next_pair_rand(uint8_t *next_pair_rand);
 void ble_sm_dbg_set_next_ediv(uint16_t next_ediv);
 void ble_sm_dbg_set_next_master_id_rand(uint64_t next_master_id_rand);
@@ -406,7 +407,7 @@ void ble_sm_lgcy_random_exec(struct ble_sm_proc *proc,
 void ble_sm_lgcy_random_rx(struct ble_sm_proc *proc,
                            struct ble_sm_result *res);
 
-#if NIMBLE_OPT(SM_SC)
+#if MYNEWT_VAL(BLE_SM_SC)
 int ble_sm_sc_io_action(struct ble_sm_proc *proc);
 void ble_sm_sc_confirm_exec(struct ble_sm_proc *proc,
                             struct ble_sm_result *res);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_sm_sc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_sm_sc.c b/net/nimble/host/src/ble_sm_sc.c
index 539d0f9..6a7d083 100644
--- a/net/nimble/host/src/ble_sm_sc.c
+++ b/net/nimble/host/src/ble_sm_sc.c
@@ -24,7 +24,7 @@
 #include "ble_hs_priv.h"
 #include "ble_sm_priv.h"
 
-#if NIMBLE_OPT(SM_SC)
+#if MYNEWT_VAL(BLE_SM_SC)
 
 #define BLE_SM_SC_PASSKEY_BYTES     4
 #define BLE_SM_SC_PASSKEY_BITS      20
@@ -721,4 +721,4 @@ ble_sm_sc_init(void)
     ble_sm_sc_keys_generated = 0;
 }
 
-#endif  /* NIMBLE_OPT_SM_SC */
+#endif  /* MYNEWT_VAL(BLE_SM_SC) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_att_clt_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_att_clt_test.c b/net/nimble/host/src/test/ble_att_clt_test.c
deleted file mode 100644
index ab89e9d..0000000
--- a/net/nimble/host/src/test/ble_att_clt_test.c
+++ /dev/null
@@ -1,536 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-
-/**
- * @return                      The handle of the new test connection.
- */
-static uint16_t
-ble_att_clt_test_misc_init(void)
-{
-    ble_hs_test_util_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}), NULL,
-                                 NULL);
-    return 2;
-}
-
-static void
-ble_att_clt_test_misc_verify_tx_write(uint16_t handle_id, void *value,
-                                      int value_len, int is_req)
-{
-    struct ble_att_write_req req;
-    struct os_mbuf *om;
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    if (is_req) {
-        ble_att_write_req_parse(om->om_data, om->om_len, &req);
-    } else {
-        ble_att_write_cmd_parse(om->om_data, om->om_len, &req);
-    }
-
-    TEST_ASSERT(req.bawq_handle == handle_id);
-    TEST_ASSERT(om->om_len == BLE_ATT_WRITE_REQ_BASE_SZ + value_len);
-    TEST_ASSERT(memcmp(om->om_data + BLE_ATT_WRITE_REQ_BASE_SZ, value,
-                       value_len) == 0);
-}
-
-static void
-ble_att_clt_test_tx_write_req_or_cmd(uint16_t conn_handle,
-                                     struct ble_att_write_req *req,
-                                     void *value, int value_len, int is_req)
-{
-    struct os_mbuf *om;
-    int rc;
-
-    om = ble_hs_test_util_om_from_flat(value, value_len);
-    if (is_req) {
-        rc = ble_att_clt_tx_write_req(conn_handle, req, om);
-    } else {
-        rc = ble_att_clt_tx_write_cmd(conn_handle, req, om);
-    }
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ble_att_clt_test_tx_find_info)
-{
-    struct ble_att_find_info_req req;
-    uint16_t conn_handle;
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    /*** Success. */
-    req.bafq_start_handle = 1;
-    req.bafq_end_handle = 0xffff;
-    rc = ble_att_clt_tx_find_info(conn_handle, &req);
-    TEST_ASSERT(rc == 0);
-
-    /*** Error: start handle of 0. */
-    req.bafq_start_handle = 0;
-    req.bafq_end_handle = 0xffff;
-    rc = ble_att_clt_tx_find_info(conn_handle, &req);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-
-    /*** Error: start handle greater than end handle. */
-    req.bafq_start_handle = 500;
-    req.bafq_end_handle = 499;
-    rc = ble_att_clt_tx_find_info(conn_handle, &req);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-
-    /*** Success; start and end handles equal. */
-    req.bafq_start_handle = 500;
-    req.bafq_end_handle = 500;
-    rc = ble_att_clt_tx_find_info(conn_handle, &req);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ble_att_clt_test_rx_find_info)
-{
-    struct ble_att_find_info_rsp rsp;
-    uint16_t conn_handle;
-    uint8_t buf[1024];
-    uint8_t uuid128_1[16] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
-    int off;
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    /*** One 128-bit UUID. */
-    /* Receive response with attribute mapping. */
-    off = 0;
-    rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT;
-    ble_att_find_info_rsp_write(buf + off, sizeof buf - off, &rsp);
-    off += BLE_ATT_FIND_INFO_RSP_BASE_SZ;
-
-    htole16(buf + off, 1);
-    off += 2;
-    memcpy(buf + off, uuid128_1, 16);
-    off += 16;
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    TEST_ASSERT(rc == 0);
-
-    /*** One 16-bit UUID. */
-    /* Receive response with attribute mapping. */
-    off = 0;
-    rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT;
-    ble_att_find_info_rsp_write(buf + off, sizeof buf - off, &rsp);
-    off += BLE_ATT_FIND_INFO_RSP_BASE_SZ;
-
-    htole16(buf + off, 2);
-    off += 2;
-    htole16(buf + off, 0x000f);
-    off += 2;
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    TEST_ASSERT(rc == 0);
-
-    /*** Two 16-bit UUIDs. */
-    /* Receive response with attribute mappings. */
-    off = 0;
-    rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT;
-    ble_att_find_info_rsp_write(buf + off, sizeof buf - off, &rsp);
-    off += BLE_ATT_FIND_INFO_RSP_BASE_SZ;
-
-    htole16(buf + off, 3);
-    off += 2;
-    htole16(buf + off, 0x0010);
-    off += 2;
-
-    htole16(buf + off, 4);
-    off += 2;
-    htole16(buf + off, 0x0011);
-    off += 2;
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    TEST_ASSERT(rc == 0);
-}
-
-static void
-ble_att_clt_test_case_tx_write_req_or_cmd(int is_req)
-{
-    struct ble_att_write_req req;
-    uint16_t conn_handle;
-    uint8_t value300[500] = { 0 };
-    uint8_t value5[5] = { 6, 7, 54, 34, 8 };
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    /*** 5-byte write. */
-    req.bawq_handle = 0x1234;
-    ble_att_clt_test_tx_write_req_or_cmd(conn_handle, &req, value5,
-                                         sizeof value5, is_req);
-    ble_hs_test_util_tx_all();
-    ble_att_clt_test_misc_verify_tx_write(0x1234, value5, sizeof value5,
-                                          is_req);
-
-    /*** Overlong write; verify command truncated to ATT MTU. */
-    req.bawq_handle = 0xab83;
-    ble_att_clt_test_tx_write_req_or_cmd(conn_handle, &req, value300,
-                                         sizeof value300, is_req);
-    ble_hs_test_util_tx_all();
-    ble_att_clt_test_misc_verify_tx_write(0xab83, value300,
-                                          BLE_ATT_MTU_DFLT - 3, is_req);
-}
-
-static void
-ble_att_clt_test_misc_prep_good(uint16_t handle, uint16_t offset,
-                                uint8_t *attr_data, uint16_t attr_data_len)
-{
-    struct ble_att_prep_write_cmd req;
-    struct os_mbuf *om;
-    uint16_t conn_handle;
-    int rc;
-    int i;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    req.bapc_handle = handle;
-    req.bapc_offset = offset;
-    om = ble_hs_test_util_om_from_flat(attr_data, attr_data_len);
-    rc = ble_att_clt_tx_prep_write(conn_handle, &req, om);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_test_util_tx_all();
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    TEST_ASSERT_FATAL(om != NULL);
-    TEST_ASSERT(om->om_len == BLE_ATT_PREP_WRITE_CMD_BASE_SZ + attr_data_len);
-
-    ble_att_prep_write_req_parse(om->om_data, om->om_len, &req);
-    TEST_ASSERT(req.bapc_handle == handle);
-    TEST_ASSERT(req.bapc_offset == offset);
-    for (i = 0; i < attr_data_len; i++) {
-        TEST_ASSERT(om->om_data[BLE_ATT_PREP_WRITE_CMD_BASE_SZ + i] ==
-                    attr_data[i]);
-    }
-}
-
-static void
-ble_att_clt_test_misc_exec_good(uint8_t flags)
-{
-    struct ble_att_exec_write_req req;
-    struct os_mbuf *om;
-    uint16_t conn_handle;
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    req.baeq_flags = flags;
-    rc = ble_att_clt_tx_exec_write(conn_handle, &req);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_test_util_tx_all();
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    TEST_ASSERT_FATAL(om != NULL);
-    TEST_ASSERT(om->om_len == BLE_ATT_EXEC_WRITE_REQ_SZ);
-
-    ble_att_exec_write_req_parse(om->om_data, om->om_len, &req);
-    TEST_ASSERT(req.baeq_flags == flags);
-}
-
-static void
-ble_att_clt_test_misc_prep_bad(uint16_t handle, uint16_t offset,
-                               uint8_t *attr_data, uint16_t attr_data_len,
-                               int status)
-{
-    struct ble_att_prep_write_cmd req;
-    struct os_mbuf *om;
-    uint16_t conn_handle;
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    om = ble_hs_test_util_om_from_flat(attr_data, attr_data_len);
-
-    req.bapc_handle = handle;
-    req.bapc_offset = offset;
-    rc = ble_att_clt_tx_prep_write(conn_handle, &req, om);
-    TEST_ASSERT(rc == status);
-}
-
-TEST_CASE(ble_att_clt_test_tx_write)
-{
-    ble_att_clt_test_case_tx_write_req_or_cmd(0);
-    ble_att_clt_test_case_tx_write_req_or_cmd(1);
-}
-
-TEST_CASE(ble_att_clt_test_tx_read)
-{
-    struct ble_att_read_req req;
-    uint16_t conn_handle;
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    /*** Success. */
-    req.barq_handle = 1;
-    rc = ble_att_clt_tx_read(conn_handle, &req);
-    TEST_ASSERT(rc == 0);
-
-    /*** Error: handle of 0. */
-    req.barq_handle = 0;
-    rc = ble_att_clt_tx_read(conn_handle, &req);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-}
-
-TEST_CASE(ble_att_clt_test_rx_read)
-{
-    uint16_t conn_handle;
-    uint8_t buf[1024];
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    /*** Basic success. */
-    buf[0] = BLE_ATT_OP_READ_RSP;
-    buf[1] = 0;
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, 2);
-    TEST_ASSERT(rc == 0);
-
-    /*** Larger response. */
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, 20);
-    TEST_ASSERT(rc == 0);
-
-    /*** Zero-length response. */
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, 1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ble_att_clt_test_tx_read_blob)
-{
-    struct ble_att_read_blob_req req;
-    uint16_t conn_handle;
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    /*** Success. */
-    req.babq_handle = 1;
-    req.babq_offset = 0;
-    rc = ble_att_clt_tx_read_blob(conn_handle, &req);
-    TEST_ASSERT(rc == 0);
-
-    /*** Error: handle of 0. */
-    req.babq_handle = 0;
-    req.babq_offset = 0;
-    rc = ble_att_clt_tx_read_blob(conn_handle, &req);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-}
-
-TEST_CASE(ble_att_clt_test_rx_read_blob)
-{
-    uint16_t conn_handle;
-    uint8_t buf[1024];
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    /*** Basic success. */
-    buf[0] = BLE_ATT_OP_READ_BLOB_RSP;
-    buf[1] = 0;
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, 2);
-    TEST_ASSERT(rc == 0);
-
-    /*** Larger response. */
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, 20);
-    TEST_ASSERT(rc == 0);
-
-    /*** Zero-length response. */
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, 1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ble_att_clt_test_tx_read_mult)
-{
-    struct os_mbuf *om;
-    uint16_t conn_handle;
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    /*** Success. */
-    rc = ble_att_clt_tx_read_mult(conn_handle, ((uint16_t[]){ 1, 2 }), 2);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_test_util_tx_all();
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    TEST_ASSERT_FATAL(om != NULL);
-    TEST_ASSERT(om->om_len == BLE_ATT_READ_MULT_REQ_BASE_SZ + 4);
-
-    ble_att_read_mult_req_parse(om->om_data, om->om_len);
-    TEST_ASSERT(le16toh(om->om_data + BLE_ATT_READ_MULT_REQ_BASE_SZ) == 1);
-    TEST_ASSERT(le16toh(om->om_data + BLE_ATT_READ_MULT_REQ_BASE_SZ + 2) == 2);
-
-    /*** Error: no handles. */
-    rc = ble_att_clt_tx_read_mult(conn_handle, NULL, 0);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-}
-
-TEST_CASE(ble_att_clt_test_rx_read_mult)
-{
-    uint16_t conn_handle;
-    uint8_t buf[1024];
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    /*** Basic success. */
-    ble_att_read_mult_rsp_write(buf, sizeof buf);
-    htole16(buf + BLE_ATT_READ_MULT_RSP_BASE_SZ + 0, 12);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(
-        conn_handle, BLE_L2CAP_CID_ATT, buf,
-        BLE_ATT_READ_MULT_RSP_BASE_SZ + 2);
-    TEST_ASSERT(rc == 0);
-
-    /*** Larger response. */
-    htole16(buf + BLE_ATT_READ_MULT_RSP_BASE_SZ + 0, 12);
-    htole16(buf + BLE_ATT_READ_MULT_RSP_BASE_SZ + 2, 43);
-    htole16(buf + BLE_ATT_READ_MULT_RSP_BASE_SZ + 4, 91);
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(
-        conn_handle, BLE_L2CAP_CID_ATT, buf,
-        BLE_ATT_READ_MULT_RSP_BASE_SZ + 6);
-    TEST_ASSERT(rc == 0);
-
-    /*** Zero-length response. */
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(
-        conn_handle, BLE_L2CAP_CID_ATT, buf,
-        BLE_ATT_READ_MULT_RSP_BASE_SZ + 0);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ble_att_clt_test_tx_prep_write)
-{
-    uint8_t attr_data[512];
-    int i;
-
-    for (i = 0; i < sizeof attr_data; i++) {
-        attr_data[i] = i;
-    }
-
-    /*** Success. */
-    ble_att_clt_test_misc_prep_good(123, 0, attr_data, 16);
-    ble_att_clt_test_misc_prep_good(5432, 100, attr_data, 2);
-    ble_att_clt_test_misc_prep_good(0x1234, 400, attr_data, 0);
-    ble_att_clt_test_misc_prep_good(5432, 0, attr_data,
-                                    BLE_ATT_MTU_DFLT -
-                                        BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
-    ble_att_clt_test_misc_prep_good(0x1234, 507, attr_data, 5);
-
-    /*** Error: handle of 0. */
-    ble_att_clt_test_misc_prep_bad(0, 0, attr_data, 16, BLE_HS_EINVAL);
-
-    /*** Error: offset + length greater than maximum attribute size. */
-    ble_att_clt_test_misc_prep_bad(1, 507, attr_data, 6, BLE_HS_EINVAL);
-
-    /*** Error: packet larger than MTU. */
-    ble_att_clt_test_misc_prep_bad(1, 0, attr_data,
-                                   BLE_ATT_MTU_DFLT -
-                                       BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1,
-                                   BLE_HS_EINVAL);
-}
-
-TEST_CASE(ble_att_clt_test_rx_prep_write)
-{
-    struct ble_att_prep_write_cmd rsp;
-    uint16_t conn_handle;
-    uint8_t buf[1024];
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    /*** Basic success. */
-    rsp.bapc_handle = 0x1234;
-    rsp.bapc_offset = 0;
-    ble_att_prep_write_rsp_write(buf, sizeof buf, &rsp);
-    memset(buf + BLE_ATT_PREP_WRITE_CMD_BASE_SZ, 1, 5);
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(
-        conn_handle, BLE_L2CAP_CID_ATT, buf,
-        BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 5);
-    TEST_ASSERT(rc == 0);
-
-    /*** 0-length write. */
-    rsp.bapc_handle = 0x1234;
-    rsp.bapc_offset = 0;
-    ble_att_prep_write_rsp_write(buf, sizeof buf, &rsp);
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(
-        conn_handle, BLE_L2CAP_CID_ATT, buf, BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ble_att_clt_test_tx_exec_write)
-{
-    struct ble_att_exec_write_req req;
-    uint16_t conn_handle;
-    int rc;
-
-    conn_handle = ble_att_clt_test_misc_init();
-
-    /*** Success. */
-    ble_att_clt_test_misc_exec_good(0);
-    ble_att_clt_test_misc_exec_good(BLE_ATT_EXEC_WRITE_F_CONFIRM);
-
-    /*** Error: invalid flags value. */
-    req.baeq_flags = 0x02;
-    rc = ble_att_clt_tx_exec_write(conn_handle, &req);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-}
-
-TEST_SUITE(ble_att_clt_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_att_clt_test_tx_find_info();
-    ble_att_clt_test_rx_find_info();
-    ble_att_clt_test_tx_read();
-    ble_att_clt_test_rx_read();
-    ble_att_clt_test_tx_read_blob();
-    ble_att_clt_test_rx_read_blob();
-    ble_att_clt_test_tx_read_mult();
-    ble_att_clt_test_rx_read_mult();
-    ble_att_clt_test_tx_write();
-    ble_att_clt_test_tx_prep_write();
-    ble_att_clt_test_rx_prep_write();
-    ble_att_clt_test_tx_exec_write();
-}
-
-int
-ble_att_clt_test_all(void)
-{
-    ble_att_clt_suite();
-
-    return tu_any_failed;
-}


[10/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_gatt_read_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gatt_read_test.c b/net/nimble/host/test/src/ble_gatt_read_test.c
new file mode 100644
index 0000000..822de5c
--- /dev/null
+++ b/net/nimble/host/test/src/ble_gatt_read_test.c
@@ -0,0 +1,823 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include <limits.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "host/ble_hs_test.h"
+#include "host/ble_uuid.h"
+#include "ble_hs_test_util.h"
+
+struct ble_gatt_read_test_attr {
+    uint16_t conn_handle;
+    uint16_t handle;
+    uint8_t value_len;
+    uint8_t value[BLE_ATT_ATTR_MAX_LEN];
+};
+
+#define BLE_GATT_READ_TEST_MAX_ATTRS    256
+
+struct ble_gatt_read_test_attr
+    ble_gatt_read_test_attrs[BLE_GATT_READ_TEST_MAX_ATTRS];
+int ble_gatt_read_test_num_attrs;
+int ble_gatt_read_test_complete;
+
+uint16_t ble_gatt_read_test_bad_conn_handle;
+int ble_gatt_read_test_bad_status;
+
+static void
+ble_gatt_read_test_misc_init(void)
+{
+    ble_hs_test_util_init();
+    ble_gatt_read_test_num_attrs = 0;
+    ble_gatt_read_test_complete = 0;
+    ble_gatt_read_test_bad_conn_handle = 0;
+    ble_gatt_read_test_bad_status = 0;
+
+    memset(&ble_gatt_read_test_attrs[0], 0,
+           sizeof ble_gatt_read_test_attrs[0]);
+}
+
+static int
+ble_gatt_read_test_cb(uint16_t conn_handle, const struct ble_gatt_error *error,
+                      struct ble_gatt_attr *attr, void *arg)
+{
+    struct ble_gatt_read_test_attr *dst;
+    int *stop_after;
+
+    stop_after = arg;
+
+    TEST_ASSERT_FATAL(error != NULL);
+
+    if (error->status != 0) {
+        ble_gatt_read_test_bad_conn_handle = conn_handle;
+        ble_gatt_read_test_bad_status = error->status;
+        ble_gatt_read_test_complete = 1;
+        return 0;
+    }
+
+    if (attr == NULL) {
+        ble_gatt_read_test_complete = 1;
+        return 0;
+    }
+
+    TEST_ASSERT_FATAL(ble_gatt_read_test_num_attrs <
+                      BLE_GATT_READ_TEST_MAX_ATTRS);
+    dst = ble_gatt_read_test_attrs + ble_gatt_read_test_num_attrs++;
+
+    TEST_ASSERT_FATAL(OS_MBUF_PKTLEN(attr->om) <= sizeof dst->value);
+
+    dst->conn_handle = conn_handle;
+    dst->handle = attr->handle;
+    dst->value_len = OS_MBUF_PKTLEN(attr->om);
+    os_mbuf_copydata(attr->om, 0, OS_MBUF_PKTLEN(attr->om), dst->value);
+
+    if (stop_after != NULL && *stop_after > 0) {
+        (*stop_after)--;
+        if (*stop_after == 0) {
+            ble_gatt_read_test_complete = 1;
+            return 1;
+        }
+    } else {
+        ble_gatt_read_test_complete = 1;
+    }
+
+    return 0;
+}
+
+static int
+ble_gatt_read_test_long_cb(uint16_t conn_handle,
+                           const struct ble_gatt_error *error,
+                           struct ble_gatt_attr *attr, void *arg)
+{
+    struct ble_gatt_read_test_attr *dst;
+    int *reads_left;
+
+    reads_left = arg;
+
+    TEST_ASSERT_FATAL(error != NULL);
+
+    if (error->status != 0) {
+        ble_gatt_read_test_bad_conn_handle = conn_handle;
+        ble_gatt_read_test_bad_status = error->status;
+        ble_gatt_read_test_complete = 1;
+        return 0;
+    }
+
+    if (attr == NULL) {
+        ble_gatt_read_test_complete = 1;
+        return 0;
+    }
+
+    dst = ble_gatt_read_test_attrs + 0;
+
+    TEST_ASSERT_FATAL(OS_MBUF_PKTLEN(attr->om) <=
+        dst->value_len + sizeof dst->value);
+    TEST_ASSERT(attr->offset == dst->value_len);
+
+    if (attr->offset == 0) {
+        dst->conn_handle = conn_handle;
+        dst->handle = attr->handle;
+    } else {
+        TEST_ASSERT(conn_handle == dst->conn_handle);
+        TEST_ASSERT(attr->handle == dst->handle);
+    }
+    os_mbuf_copydata(attr->om, 0, OS_MBUF_PKTLEN(attr->om),
+                     dst->value + dst->value_len);
+    dst->value_len += OS_MBUF_PKTLEN(attr->om);
+
+    if (reads_left != NULL && *reads_left > 0) {
+        (*reads_left)--;
+        if (*reads_left == 0) {
+            ble_gatt_read_test_complete = 1;
+            return 1;
+        }
+    }
+
+    return 0;
+}
+
+static void
+ble_gatt_read_test_misc_rx_rsp_good_raw(uint16_t conn_handle,
+                                        uint8_t att_op,
+                                        const void *data, int data_len)
+{
+    uint8_t buf[1024];
+    int rc;
+
+    TEST_ASSERT_FATAL(data_len <= sizeof buf);
+
+    /* Send the pending ATT Read Request. */
+    ble_hs_test_util_tx_all();
+
+    buf[0] = att_op;
+    memcpy(buf + 1, data, data_len);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, 1 + data_len);
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+ble_gatt_read_test_misc_rx_rsp_good(uint16_t conn_handle,
+                                    struct ble_hs_test_util_flat_attr *attr)
+{
+    ble_gatt_read_test_misc_rx_rsp_good_raw(conn_handle, BLE_ATT_OP_READ_RSP,
+                                            attr->value,
+                                            attr->value_len);
+}
+
+static void
+ble_gatt_read_test_misc_rx_rsp_bad(uint16_t conn_handle,
+                                   uint8_t att_error, uint16_t err_handle)
+{
+    /* Send the pending ATT Read Request. */
+    ble_hs_test_util_tx_all();
+
+    ble_hs_test_util_rx_att_err_rsp(conn_handle, BLE_ATT_OP_READ_REQ,
+                                    att_error, err_handle);
+}
+
+static int
+ble_gatt_read_test_misc_uuid_rx_rsp_good(
+    uint16_t conn_handle, struct ble_hs_test_util_flat_attr *attrs)
+{
+    struct ble_att_read_type_rsp rsp;
+    uint8_t buf[1024];
+    int prev_len;
+    int off;
+    int rc;
+    int i;
+
+    if (ble_gatt_read_test_complete || attrs[0].handle == 0) {
+        return 0;
+    }
+
+    /* Send the pending ATT Read By Type Request. */
+    ble_hs_test_util_tx_all();
+
+    rsp.batp_length = 2 + attrs[0].value_len;
+    ble_att_read_type_rsp_write(buf, sizeof buf, &rsp);
+
+    prev_len = 0;
+    off = BLE_ATT_READ_TYPE_RSP_BASE_SZ;
+    for (i = 0; attrs[i].handle != 0; i++) {
+        if (prev_len != 0 && prev_len != attrs[i].value_len) {
+            break;
+        }
+        prev_len = attrs[i].value_len;
+
+        htole16(buf + off, attrs[i].handle);
+        off += 2;
+
+        memcpy(buf + off, attrs[i].value, attrs[i].value_len);
+        off += attrs[i].value_len;
+    }
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    TEST_ASSERT(rc == 0);
+
+    return i;
+}
+
+static void
+ble_gatt_read_test_misc_verify_good(struct ble_hs_test_util_flat_attr *attr)
+{
+    int rc;
+
+    ble_gatt_read_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    rc = ble_gattc_read(2, attr->handle, ble_gatt_read_test_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_gatt_read_test_misc_rx_rsp_good(2, attr);
+
+    TEST_ASSERT(ble_gatt_read_test_num_attrs == 1);
+    TEST_ASSERT(ble_gatt_read_test_attrs[0].conn_handle == 2);
+    TEST_ASSERT(ble_gatt_read_test_attrs[0].handle == attr->handle);
+    TEST_ASSERT(ble_gatt_read_test_attrs[0].value_len == attr->value_len);
+    TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[0].value, attr->value,
+                       attr->value_len) == 0);
+}
+
+static void
+ble_gatt_read_test_misc_verify_bad(uint8_t att_status,
+                                   struct ble_hs_test_util_flat_attr *attr)
+{
+    int rc;
+
+    ble_gatt_read_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    rc = ble_gattc_read(2, attr->handle, ble_gatt_read_test_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_gatt_read_test_misc_rx_rsp_bad(2, att_status, attr->handle);
+
+    TEST_ASSERT(ble_gatt_read_test_num_attrs == 0);
+    TEST_ASSERT(ble_gatt_read_test_bad_conn_handle == 2);
+    TEST_ASSERT(ble_gatt_read_test_bad_status ==
+                BLE_HS_ERR_ATT_BASE + att_status);
+    TEST_ASSERT(!ble_gattc_any_jobs());
+}
+
+static void
+ble_gatt_read_test_misc_uuid_verify_good(
+    uint16_t start_handle, uint16_t end_handle, void *uuid128,
+    int stop_after, struct ble_hs_test_util_flat_attr *attrs)
+{
+    int num_read;
+    int idx;
+    int rc;
+    int i;
+
+    ble_gatt_read_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    rc = ble_gattc_read_by_uuid(2, start_handle, end_handle, uuid128,
+                                ble_gatt_read_test_cb, &stop_after);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    idx = 0;
+    while (1) {
+        num_read = ble_gatt_read_test_misc_uuid_rx_rsp_good(2, attrs + idx);
+        if (num_read == 0) {
+            ble_hs_test_util_tx_all();
+            ble_hs_test_util_rx_att_err_rsp(2, BLE_ATT_OP_READ_TYPE_REQ,
+                                            BLE_ATT_ERR_ATTR_NOT_FOUND,
+                                            start_handle);
+            break;
+        }
+
+        idx += num_read;
+    }
+
+    TEST_ASSERT(ble_gatt_read_test_complete);
+    TEST_ASSERT(idx == ble_gatt_read_test_num_attrs);
+
+    for (i = 0; i < idx; i++) {
+        TEST_ASSERT(ble_gatt_read_test_attrs[i].conn_handle == 2);
+        TEST_ASSERT(ble_gatt_read_test_attrs[i].handle == attrs[i].handle);
+        TEST_ASSERT(ble_gatt_read_test_attrs[i].value_len ==
+                    attrs[i].value_len);
+        TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[i].value, attrs[i].value,
+                           attrs[i].value_len) == 0);
+    }
+    TEST_ASSERT(!ble_gattc_any_jobs());
+}
+
+static void
+ble_gatt_read_test_misc_long_verify_good(
+    int max_reads, struct ble_hs_test_util_flat_attr *attr)
+{
+    int reads_left;
+    int chunk_sz;
+    int rem_len;
+    int att_op;
+    int off;
+    int rc;
+
+    ble_gatt_read_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    if (max_reads == 0) {
+        max_reads = INT_MAX;
+    }
+    reads_left = max_reads;
+    rc = ble_gattc_read_long(2, attr->handle, ble_gatt_read_test_long_cb,
+                             &reads_left);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    off = 0;
+    rem_len = attr->value_len;
+    do {
+        if (rem_len > BLE_ATT_MTU_DFLT - 1) {
+            chunk_sz = BLE_ATT_MTU_DFLT - 1;
+        } else {
+            chunk_sz = rem_len;
+        }
+        if (off == 0) {
+            att_op = BLE_ATT_OP_READ_RSP;
+        } else {
+            att_op = BLE_ATT_OP_READ_BLOB_RSP;
+        }
+        ble_gatt_read_test_misc_rx_rsp_good_raw(2, att_op,
+                                                attr->value + off, chunk_sz);
+        rem_len -= chunk_sz;
+        off += chunk_sz;
+    } while (rem_len > 0 && reads_left > 0);
+
+    TEST_ASSERT(ble_gatt_read_test_complete);
+    TEST_ASSERT(!ble_gattc_any_jobs());
+    TEST_ASSERT(ble_gatt_read_test_attrs[0].conn_handle == 2);
+    TEST_ASSERT(ble_gatt_read_test_attrs[0].handle == attr->handle);
+    if (reads_left > 0) {
+        TEST_ASSERT(ble_gatt_read_test_attrs[0].value_len == attr->value_len);
+    }
+    TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[0].value, attr->value,
+                       ble_gatt_read_test_attrs[0].value_len) == 0);
+}
+
+static void
+ble_gatt_read_test_misc_long_verify_bad(
+    uint8_t att_status, struct ble_hs_test_util_flat_attr *attr)
+{
+    int rc;
+
+    ble_gatt_read_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    rc = ble_gattc_read_long(2, attr->handle,
+                             ble_gatt_read_test_long_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_gatt_read_test_misc_rx_rsp_bad(2, att_status, attr->handle);
+
+    TEST_ASSERT(ble_gatt_read_test_num_attrs == 0);
+    TEST_ASSERT(ble_gatt_read_test_bad_conn_handle == 2);
+    TEST_ASSERT(ble_gatt_read_test_bad_status ==
+                BLE_HS_ERR_ATT_BASE + att_status);
+    TEST_ASSERT(!ble_gattc_any_jobs());
+}
+
+static int
+ble_gatt_read_test_misc_extract_handles(
+    struct ble_hs_test_util_flat_attr *attrs, uint16_t *handles)
+{
+    int i;
+
+    for (i = 0; attrs[i].handle != 0; i++) {
+        handles[i] = attrs[i].handle;
+    }
+    return i;
+}
+
+static void
+ble_gatt_read_test_misc_mult_verify_good(
+    struct ble_hs_test_util_flat_attr *attrs)
+{
+    uint8_t expected_value[512];
+    uint16_t handles[256];
+    int num_attrs;
+    int chunk_sz;
+    int off;
+    int rc;
+    int i;
+
+    ble_gatt_read_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    num_attrs = ble_gatt_read_test_misc_extract_handles(attrs, handles);
+
+    off = 0;
+    for (i = 0; i < num_attrs; i++) {
+        if (attrs[i].value_len > BLE_ATT_MTU_DFLT - 1 - off) {
+            chunk_sz = BLE_ATT_MTU_DFLT - 1 - off;
+        } else {
+            chunk_sz = attrs[i].value_len;
+        }
+
+        if (chunk_sz > 0) {
+            memcpy(expected_value + off, attrs[i].value, chunk_sz);
+            off += chunk_sz;
+        }
+    }
+
+    rc = ble_gattc_read_mult(2, handles, num_attrs,
+                             ble_gatt_read_test_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_gatt_read_test_misc_rx_rsp_good_raw(2, BLE_ATT_OP_READ_MULT_RSP,
+                                            expected_value, off);
+
+    TEST_ASSERT(ble_gatt_read_test_complete);
+    TEST_ASSERT(!ble_gattc_any_jobs());
+    TEST_ASSERT(ble_gatt_read_test_attrs[0].conn_handle == 2);
+    TEST_ASSERT(ble_gatt_read_test_attrs[0].value_len == off);
+    TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[0].value, expected_value,
+                       off) == 0);
+}
+
+static void
+ble_gatt_read_test_misc_mult_verify_bad(
+    uint8_t att_status, uint16_t err_handle,
+    struct ble_hs_test_util_flat_attr *attrs)
+{
+    uint16_t handles[256];
+    int num_attrs;
+    int rc;
+
+    ble_gatt_read_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    num_attrs = ble_gatt_read_test_misc_extract_handles(attrs, handles);
+
+    rc = ble_gattc_read_mult(2, handles, num_attrs,
+                             ble_gatt_read_test_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_gatt_read_test_misc_rx_rsp_bad(2, att_status, err_handle);
+
+    TEST_ASSERT(ble_gatt_read_test_num_attrs == 0);
+    TEST_ASSERT(ble_gatt_read_test_bad_conn_handle == 2);
+    TEST_ASSERT(ble_gatt_read_test_bad_status ==
+                BLE_HS_ERR_ATT_BASE + att_status);
+    TEST_ASSERT(!ble_gattc_any_jobs());
+}
+
+TEST_CASE(ble_gatt_read_test_by_handle)
+{
+    /* Read a seven-byte attribute. */
+    ble_gatt_read_test_misc_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
+        .handle = 43,
+        .value = { 1,2,3,4,5,6,7 },
+        .value_len = 7
+    } });
+
+    /* Read a one-byte attribute. */
+    ble_gatt_read_test_misc_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
+        .handle = 0x5432,
+        .value = { 0xff },
+        .value_len = 1
+    } });
+
+    /* Read a 200-byte attribute. */
+    ble_gatt_read_test_misc_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
+        .handle = 815,
+        .value = { 0 },
+        .value_len = 200,
+    } });
+
+    /* Fail due to attribute not found. */
+    ble_gatt_read_test_misc_verify_bad(BLE_ATT_ERR_ATTR_NOT_FOUND,
+        (struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 719,
+            .value = { 1,2,3,4,5,6,7 },
+            .value_len = 7
+        } });
+
+    /* Fail due to invalid PDU. */
+    ble_gatt_read_test_misc_verify_bad(BLE_ATT_ERR_INVALID_PDU,
+        (struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 65,
+            .value = { 0xfa, 0x4c },
+            .value_len = 2
+        } });
+}
+
+TEST_CASE(ble_gatt_read_test_by_uuid)
+{
+    /* Read a single seven-byte attribute. */
+    ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 0,
+        (struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 43,
+            .value = { 1,2,3,4,5,6,7 },
+            .value_len = 7
+        }, {
+            0,
+        } });
+
+    /* Read two seven-byte attributes; one response. */
+    ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 0,
+        (struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 43,
+            .value = { 1,2,3,4,5,6,7 },
+            .value_len = 7
+        }, {
+            .handle = 44,
+            .value = { 2,3,4,5,6,7,8 },
+            .value_len = 7
+        }, {
+            0,
+        } });
+
+    /* Read two attributes; two responses. */
+    ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 0,
+        (struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 43,
+            .value = { 1,2,3,4,5,6,7 },
+            .value_len = 7
+        }, {
+            .handle = 44,
+            .value = { 2,3,4 },
+            .value_len = 3
+        }, {
+            0,
+        } });
+
+    /* Stop after three reads. */
+    ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 3,
+        (struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 43,
+            .value = { 1,2,3,4,5,6,7 },
+            .value_len = 7
+        }, {
+            .handle = 44,
+            .value = { 2,3,4 },
+            .value_len = 3
+        }, {
+            .handle = 45,
+            .value = { 2,3,4 },
+            .value_len = 3
+        }, {
+            .handle = 46,
+            .value = { 3,4,5,6 },
+            .value_len = 4
+        }, {
+            .handle = 47,
+            .value = { 2,3,4 },
+            .value_len = 3
+        }, {
+            0,
+        } });
+}
+
+TEST_CASE(ble_gatt_read_test_long)
+{
+    uint8_t data512[512];
+    int i;
+
+    for (i = 0; i < sizeof data512; i++) {
+        data512[i] = i;
+    }
+
+    /* Read a seven-byte attribute. */
+    ble_gatt_read_test_misc_long_verify_good(0,
+        (struct ble_hs_test_util_flat_attr[]) { {
+        .handle = 43,
+        .value = { 1,2,3,4,5,6,7 },
+        .value_len = 7
+    } });
+
+    /* Read a zero-byte attribute. */
+    ble_gatt_read_test_misc_long_verify_good(0,
+        (struct ble_hs_test_util_flat_attr[]) { {
+        .handle = 43,
+        .value = { 0 },
+        .value_len = 0
+    } });
+
+    /* Read a 60-byte attribute; three requests. */
+    ble_gatt_read_test_misc_long_verify_good(0,
+        (struct ble_hs_test_util_flat_attr[]) { {
+        .handle = 34,
+        .value = {
+            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+            17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
+            33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
+            49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60
+        },
+        .value_len = 60
+    } });
+
+    /* Stop after two reads. */
+    ble_gatt_read_test_misc_long_verify_good(2,
+        (struct ble_hs_test_util_flat_attr[]) { {
+        .handle = 34,
+        .value = {
+            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+            17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
+            33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
+            49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60
+        },
+        .value_len = 60
+    } });
+
+    /* Fail due to attribute not found. */
+    ble_gatt_read_test_misc_long_verify_bad(BLE_ATT_ERR_ATTR_NOT_FOUND,
+        (struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 719,
+            .value = { 1, 2, 3, 4, 5, 6, 7 }, 
+            .value_len = 7
+        } });
+}
+
+TEST_CASE(ble_gatt_read_test_mult)
+{
+    uint8_t data512[512];
+    int i;
+
+    for (i = 0; i < sizeof data512; i++) {
+        data512[i] = i;
+    }
+
+    /* Read one attribute. */
+    ble_gatt_read_test_misc_mult_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
+        .handle = 43,
+        .value = { 0, 1, 2, 3, 4, 5, 6, 7 },
+        .value_len = 7
+    }, {
+        0
+    } });
+
+    /* Read two attributes. */
+    ble_gatt_read_test_misc_mult_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
+        .handle = 43,
+        .value = { 0, 1, 2, 3, 4, 5, 6, 7 },
+        .value_len = 7,
+    }, {
+        .handle = 44,
+        .value = { 8, 9, 10, 11 },
+        .value_len = 4,
+    }, {
+        0
+    } });
+
+    /* Read two attributes (swap order). */
+    ble_gatt_read_test_misc_mult_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
+        .handle = 44,
+        .value = { 8, 9, 10, 11 },
+        .value_len = 4,
+    }, {
+        .handle = 43,
+        .value = { 0, 1, 2, 3, 4, 5, 6, 7 },
+        .value_len = 7,
+    }, {
+        0
+    } });
+
+    /* Read five attributes. */
+    ble_gatt_read_test_misc_mult_verify_good(
+        (struct ble_hs_test_util_flat_attr[]) { {
+        .handle = 43,
+        .value = { 0, 1, 2, 3, 4, 5, 6, 7 },
+        .value_len = 7,
+    }, {
+        .handle = 44,
+        .value = { 8, 9, 10, 11 },
+        .value_len = 4,
+    }, {
+        .handle = 145,
+        .value = { 12, 13 },
+        .value_len = 2,
+    }, {
+        .handle = 191,
+        .value = { 14, 15, 16 },
+        .value_len = 3,
+    }, {
+        .handle = 352,
+        .value = { 17, 18, 19, 20 },
+        .value_len = 4,
+    }, {
+        0
+    } });
+
+    /* Fail due to attribute not found. */
+    ble_gatt_read_test_misc_mult_verify_bad(BLE_ATT_ERR_ATTR_NOT_FOUND, 719,
+        (struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 719,
+            .value = { 1,2,3,4,5,6,7 },
+            .value_len = 7
+        }, {
+            0
+        } });
+}
+
+TEST_CASE(ble_gatt_read_test_concurrent)
+{
+    int rc;
+    int i;
+
+    ble_gatt_read_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    /***
+     * Perform three concurrent reads.  Assert that each response is correctly
+     * matched up with its corresponding GATT procedure.
+     */
+
+    struct ble_hs_test_util_flat_attr attrs[3] = {
+        {
+            .handle = 1,
+            .offset = 0,
+            .value_len = 3,
+            .value = { 1, 2, 3 },
+        },
+        {
+            .handle = 2,
+            .offset = 0,
+            .value_len = 4,
+            .value = { 2, 3, 4, 5 },
+        },
+        {
+            .handle = 3,
+            .offset = 0,
+            .value_len = 5,
+            .value = { 3, 4, 5, 6, 7 },
+        },
+    };
+
+    rc = ble_gattc_read(2, attrs[0].handle, ble_gatt_read_test_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+    rc = ble_gattc_read(2, attrs[1].handle, ble_gatt_read_test_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+    rc = ble_gattc_read(2, attrs[2].handle, ble_gatt_read_test_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_gatt_read_test_misc_rx_rsp_good(2, attrs + 0);
+    ble_gatt_read_test_misc_rx_rsp_good(2, attrs + 1);
+    ble_gatt_read_test_misc_rx_rsp_good(2, attrs + 2);
+
+    TEST_ASSERT(ble_gatt_read_test_num_attrs == 3);
+
+    for (i = 0; i < 3; i++) {
+        TEST_ASSERT(ble_gatt_read_test_attrs[i].handle == attrs[i].handle);
+        TEST_ASSERT(ble_gatt_read_test_attrs[i].value_len ==
+                    attrs[i].value_len);
+        TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[i].value, attrs[i].value,
+                           attrs[i].value_len) == 0);
+    }
+}
+
+TEST_SUITE(ble_gatt_read_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gatt_read_test_by_handle();
+    ble_gatt_read_test_by_uuid();
+    ble_gatt_read_test_long();
+    ble_gatt_read_test_mult();
+    ble_gatt_read_test_concurrent();
+}
+
+int
+ble_gatt_read_test_all(void)
+{
+    ble_gatt_read_test_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_gatt_write_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gatt_write_test.c b/net/nimble/host/test/src/ble_gatt_write_test.c
new file mode 100644
index 0000000..f548198
--- /dev/null
+++ b/net/nimble/host/test/src/ble_gatt_write_test.c
@@ -0,0 +1,639 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "host/ble_hs_test.h"
+#include "host/ble_gatt.h"
+#include "host/ble_uuid.h"
+#include "ble_hs_test_util.h"
+
+#define BLE_GATT_WRITE_TEST_MAX_ATTRS   128
+
+static int ble_gatt_write_test_cb_called;
+
+static uint8_t ble_gatt_write_test_attr_value[BLE_ATT_ATTR_MAX_LEN];
+static struct ble_gatt_error ble_gatt_write_test_error;
+
+static struct ble_hs_test_util_flat_attr
+ble_gatt_write_test_attrs[BLE_GATT_WRITE_TEST_MAX_ATTRS];
+static int ble_gatt_write_test_num_attrs;
+
+static void
+ble_gatt_write_test_init(void)
+{
+    int i;
+
+    ble_hs_test_util_init();
+    ble_gatt_write_test_cb_called = 0;
+    ble_gatt_write_test_num_attrs = 0;
+
+    for (i = 0; i < sizeof ble_gatt_write_test_attr_value; i++) {
+        ble_gatt_write_test_attr_value[i] = i;
+    }
+}
+
+static int
+ble_gatt_write_test_cb_good(uint16_t conn_handle,
+                            const struct ble_gatt_error *error,
+                            struct ble_gatt_attr *attr, void *arg)
+{
+    int *attr_len;
+
+    attr_len = arg;
+
+    TEST_ASSERT(error != NULL);
+    TEST_ASSERT(conn_handle == 2);
+
+    ble_gatt_write_test_error = *error;
+
+    if (attr_len != NULL) {
+        TEST_ASSERT(error->status == 0);
+        TEST_ASSERT(attr->handle == 100);
+    }
+
+    ble_gatt_write_test_cb_called = 1;
+
+    return 0;
+}
+
+static void
+ble_gatt_write_test_rx_rsp(uint16_t conn_handle)
+{
+    uint8_t op;
+    int rc;
+
+    op = BLE_ATT_OP_WRITE_RSP;
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                &op, 1);
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+ble_gatt_write_test_rx_prep_rsp(uint16_t conn_handle, uint16_t attr_handle,
+                                uint16_t offset,
+                                const void *attr_data, uint16_t attr_data_len)
+{
+    struct ble_att_prep_write_cmd rsp;
+    uint8_t buf[512];
+    int rc;
+
+    rsp.bapc_handle = attr_handle;
+    rsp.bapc_offset = offset;
+    ble_att_prep_write_rsp_write(buf, sizeof buf, &rsp);
+
+    memcpy(buf + BLE_ATT_PREP_WRITE_CMD_BASE_SZ, attr_data, attr_data_len);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(
+        conn_handle, BLE_L2CAP_CID_ATT, buf,
+        BLE_ATT_PREP_WRITE_CMD_BASE_SZ + attr_data_len);
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+ble_gatt_write_test_rx_exec_rsp(uint16_t conn_handle)
+{
+    uint8_t op;
+    int rc;
+
+    op = BLE_ATT_OP_EXEC_WRITE_RSP;
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                &op, 1);
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+ble_gatt_write_test_misc_long_good(int attr_len)
+{
+    uint16_t mtu;
+    int off;
+    int len;
+    int rc;
+
+    ble_gatt_write_test_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    mtu = ble_att_mtu(2);
+
+    rc = ble_hs_test_util_gatt_write_long_flat(
+        2, 100, ble_gatt_write_test_attr_value, attr_len,
+        ble_gatt_write_test_cb_good, &attr_len);
+    TEST_ASSERT(rc == 0);
+
+    off = 0;
+    while (off < attr_len) {
+        len = mtu - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
+        if (off + len > attr_len) {
+            len = attr_len - off;
+        }
+
+        /* Send the pending ATT Prep Write Command. */
+        ble_hs_test_util_verify_tx_prep_write(
+            100, off, ble_gatt_write_test_attr_value + off, len);
+
+        /* Receive Prep Write response. */
+        ble_gatt_write_test_rx_prep_rsp(
+            2, 100, off, ble_gatt_write_test_attr_value + off, len);
+
+        /* Verify callback hasn't gotten called. */
+        TEST_ASSERT(!ble_gatt_write_test_cb_called);
+
+        off += len;
+    }
+
+    /* Verify execute write request sent. */
+    ble_hs_test_util_verify_tx_exec_write(BLE_ATT_EXEC_WRITE_F_CONFIRM);
+
+    /* Receive Exec Write response. */
+    ble_hs_test_util_tx_all();
+    ble_gatt_write_test_rx_exec_rsp(2);
+
+    /* Verify callback got called. */
+    TEST_ASSERT(ble_gatt_write_test_cb_called);
+}
+
+typedef void ble_gatt_write_test_long_fail_fn(uint16_t conn_handle,
+                                              int off, int len);
+
+static void
+ble_gatt_write_test_misc_long_bad(int attr_len,
+                                  ble_gatt_write_test_long_fail_fn *cb)
+{
+    uint16_t mtu;
+    int fail_now;
+    int off;
+    int len;
+    int rc;
+
+    ble_gatt_write_test_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+    mtu = ble_att_mtu(2);
+
+    rc = ble_hs_test_util_gatt_write_long_flat(
+        2, 100, ble_gatt_write_test_attr_value, attr_len,
+        ble_gatt_write_test_cb_good, NULL);
+    TEST_ASSERT(rc == 0);
+
+    fail_now = 0;
+    off = 0;
+    while (off < attr_len) {
+        len = mtu - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
+        if (off + len > attr_len) {
+            len = attr_len - off;
+        }
+
+        /* Send the pending ATT Prep Write Command. */
+        ble_hs_test_util_verify_tx_prep_write(
+            100, off, ble_gatt_write_test_attr_value + off, len);
+
+        /* Receive Prep Write response. */
+        len = BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
+        if (off + len >= attr_len) {
+            len = attr_len - off;
+            fail_now = 1;
+        }
+        if (!fail_now) {
+            ble_gatt_write_test_rx_prep_rsp(
+                2, 100, off, ble_gatt_write_test_attr_value + off, len);
+        } else {
+            cb(2, off, len);
+            break;
+        }
+
+        /* Verify callback hasn't gotten called. */
+        TEST_ASSERT(!ble_gatt_write_test_cb_called);
+
+        off += len;
+    }
+
+    /* Verify callback was called. */
+    TEST_ASSERT(ble_gatt_write_test_cb_called);
+    TEST_ASSERT(ble_gatt_write_test_error.status == BLE_HS_EBADDATA);
+    TEST_ASSERT(ble_gatt_write_test_error.att_handle == 0);
+}
+
+static void
+ble_gatt_write_test_misc_long_fail_handle(uint16_t conn_handle,
+                                          int off, int len)
+{
+    ble_gatt_write_test_rx_prep_rsp(
+        conn_handle, 99, off, ble_gatt_write_test_attr_value + off,
+        len);
+}
+
+static void
+ble_gatt_write_test_misc_long_fail_offset(uint16_t conn_handle,
+                                          int off, int len)
+{
+    ble_gatt_write_test_rx_prep_rsp(
+        conn_handle, 100, off + 1, ble_gatt_write_test_attr_value + off,
+        len);
+}
+
+static void
+ble_gatt_write_test_misc_long_fail_value(uint16_t conn_handle,
+                                         int off, int len)
+{
+    ble_gatt_write_test_rx_prep_rsp(
+        conn_handle, 100, off, ble_gatt_write_test_attr_value + off + 1,
+        len);
+}
+
+static void
+ble_gatt_write_test_misc_long_fail_length(uint16_t conn_handle,
+                                          int off, int len)
+{
+    ble_gatt_write_test_rx_prep_rsp(
+        conn_handle, 100, off, ble_gatt_write_test_attr_value + off,
+        len - 1);
+}
+
+static int
+ble_gatt_write_test_reliable_cb_good(uint16_t conn_handle,
+                                     const struct ble_gatt_error *error,
+                                     struct ble_gatt_attr *attrs,
+                                     uint8_t num_attrs, void *arg)
+{
+    int i;
+
+    TEST_ASSERT_FATAL(num_attrs <= BLE_GATT_WRITE_TEST_MAX_ATTRS);
+
+    TEST_ASSERT(conn_handle == 2);
+
+    ble_gatt_write_test_num_attrs = num_attrs;
+    for (i = 0; i < num_attrs; i++) {
+        ble_hs_test_util_attr_to_flat(ble_gatt_write_test_attrs + i,
+                                      attrs + i);
+    }
+
+    ble_gatt_write_test_cb_called = 1;
+
+    return 0;
+}
+
+static void
+ble_gatt_write_test_misc_reliable_good(
+    struct ble_hs_test_util_flat_attr *flat_attrs)
+{
+    const struct ble_hs_test_util_flat_attr *attr;
+    struct ble_gatt_attr attrs[16];
+    uint16_t mtu;
+    int num_attrs;
+    int attr_idx;
+    int len;
+    int off;
+    int rc;
+    int i;
+
+    ble_gatt_write_test_init();
+
+    for (num_attrs = 0; flat_attrs[num_attrs].handle != 0; num_attrs++) {
+        TEST_ASSERT_FATAL(num_attrs < sizeof attrs / sizeof attrs[0]);
+        ble_hs_test_util_attr_from_flat(attrs + num_attrs,
+                                        flat_attrs + num_attrs);
+    }
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+    mtu = ble_att_mtu(2);
+
+    rc = ble_gattc_write_reliable(2, attrs, num_attrs,
+                                  ble_gatt_write_test_reliable_cb_good, NULL);
+    TEST_ASSERT(rc == 0);
+
+    attr_idx = 0;
+    off = 0;
+    while (attr_idx < num_attrs) {
+        attr = flat_attrs + attr_idx;
+
+        len = mtu - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
+        if (off + len > attr->value_len) {
+            len = attr->value_len - off;
+        }
+
+        /* Send the pending ATT Prep Write Command. */
+        ble_hs_test_util_verify_tx_prep_write(attr->handle, off,
+                                              attr->value + off, len);
+
+        /* Receive Prep Write response. */
+        ble_gatt_write_test_rx_prep_rsp(2, attr->handle, off,
+                                        attr->value + off, len);
+
+        /* Verify callback hasn't gotten called. */
+        TEST_ASSERT(!ble_gatt_write_test_cb_called);
+
+        off += len;
+        if (off >= attr->value_len) {
+            attr_idx++;
+            off = 0;
+        }
+    }
+
+    /* Verify execute write request sent. */
+    ble_hs_test_util_verify_tx_exec_write(BLE_ATT_EXEC_WRITE_F_CONFIRM);
+
+    /* Receive Exec Write response. */
+    ble_hs_test_util_tx_all();
+    ble_gatt_write_test_rx_exec_rsp(2);
+
+    /* Verify callback got called. */
+    TEST_ASSERT(ble_gatt_write_test_cb_called);
+    TEST_ASSERT(ble_gatt_write_test_num_attrs == num_attrs);
+    for (i = 0; i < num_attrs; i++) {
+        rc = ble_hs_test_util_flat_attr_cmp(
+            ble_gatt_write_test_attrs + i, flat_attrs + i);
+        TEST_ASSERT(rc == 0);
+    }
+}
+
+TEST_CASE(ble_gatt_write_test_no_rsp)
+{
+    int attr_len;
+    int rc;
+
+    ble_gatt_write_test_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    attr_len = 4;
+    rc = ble_hs_test_util_gatt_write_no_rsp_flat(
+        2, 100, ble_gatt_write_test_attr_value, attr_len);
+    TEST_ASSERT(rc == 0);
+
+    /* Send the pending ATT Write Command. */
+    ble_hs_test_util_tx_all();
+
+    /* No response expected; verify callback not called. */
+    TEST_ASSERT(!ble_gatt_write_test_cb_called);
+}
+
+TEST_CASE(ble_gatt_write_test_rsp)
+{
+    int attr_len;
+
+    ble_gatt_write_test_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    attr_len = 4;
+    ble_hs_test_util_gatt_write_flat(2, 100, ble_gatt_write_test_attr_value,
+                                     attr_len, ble_gatt_write_test_cb_good,
+                                     &attr_len);
+
+    /* Send the pending ATT Write Command. */
+    ble_hs_test_util_tx_all();
+
+    /* Response not received yet; verify callback not called. */
+    TEST_ASSERT(!ble_gatt_write_test_cb_called);
+
+    /* Receive write response. */
+    ble_gatt_write_test_rx_rsp(2);
+
+    /* Verify callback got called. */
+    TEST_ASSERT(ble_gatt_write_test_cb_called);
+}
+
+TEST_CASE(ble_gatt_write_test_long_good)
+{
+    /*** 1 prep write req/rsp. */
+    ble_gatt_write_test_misc_long_good(
+        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
+
+    /*** 2 prep write reqs/rsps. */
+    ble_gatt_write_test_misc_long_good(
+        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1);
+
+    /*** Maximum reqs/rsps. */
+    ble_gatt_write_test_misc_long_good(BLE_ATT_ATTR_MAX_LEN);
+}
+
+TEST_CASE(ble_gatt_write_test_long_bad_handle)
+{
+    /*** 1 prep write req/rsp. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
+        ble_gatt_write_test_misc_long_fail_handle);
+
+    /*** 2 prep write reqs/rsps. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1,
+        ble_gatt_write_test_misc_long_fail_handle);
+
+    /*** Maximum reqs/rsps. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_ATTR_MAX_LEN,
+        ble_gatt_write_test_misc_long_fail_handle);
+}
+
+TEST_CASE(ble_gatt_write_test_long_bad_offset)
+{
+    /*** 1 prep write req/rsp. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
+        ble_gatt_write_test_misc_long_fail_offset);
+
+    /*** 2 prep write reqs/rsps. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1,
+        ble_gatt_write_test_misc_long_fail_offset);
+
+    /*** Maximum reqs/rsps. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_ATTR_MAX_LEN,
+        ble_gatt_write_test_misc_long_fail_offset);
+}
+
+TEST_CASE(ble_gatt_write_test_long_bad_value)
+{
+    /*** 1 prep write req/rsp. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
+        ble_gatt_write_test_misc_long_fail_value);
+
+    /*** 2 prep write reqs/rsps. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1,
+        ble_gatt_write_test_misc_long_fail_value);
+
+    /*** Maximum reqs/rsps. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_ATTR_MAX_LEN,
+        ble_gatt_write_test_misc_long_fail_value);
+}
+
+TEST_CASE(ble_gatt_write_test_long_bad_length)
+{
+    /*** 1 prep write req/rsp. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
+        ble_gatt_write_test_misc_long_fail_length);
+
+    /*** 2 prep write reqs/rsps. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1,
+        ble_gatt_write_test_misc_long_fail_length);
+
+    /*** Maximum reqs/rsps. */
+    ble_gatt_write_test_misc_long_bad(
+        BLE_ATT_ATTR_MAX_LEN,
+        ble_gatt_write_test_misc_long_fail_length);
+}
+
+TEST_CASE(ble_gatt_write_test_reliable_good)
+{
+    /*** 1 attribute. */
+    ble_gatt_write_test_misc_reliable_good(
+        ((struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 100,
+            .value_len = 2,
+            .value = { 1, 2 },
+        }, {
+            0
+        } }));
+
+    /*** 2 attributes. */
+    ble_gatt_write_test_misc_reliable_good(
+        ((struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 100,
+            .value_len = 2,
+            .value = { 1,2 },
+        }, {
+            .handle = 113,
+            .value_len = 6,
+            .value = { 5,6,7,8,9,10 },
+        }, {
+            0
+        } }));
+
+    /*** 3 attributes. */
+    ble_gatt_write_test_misc_reliable_good(
+        ((struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 100,
+            .value_len = 2,
+            .value = { 1,2 },
+        }, {
+            .handle = 113,
+            .value_len = 6,
+            .value = { 5,6,7,8,9,10 },
+        }, {
+            .handle = 144,
+            .value_len = 1,
+            .value = { 0xff },
+        }, {
+            0
+        } }));
+
+    /*** Long attributes. */
+    ble_gatt_write_test_misc_reliable_good(
+        ((struct ble_hs_test_util_flat_attr[]) { {
+            .handle = 100,
+            .value_len = 20,
+            .value = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 },
+        }, {
+            .handle = 144,
+            .value_len = 20,
+            .value = { 11,12,13,14,15,16,17,18,19,110,
+                       111,112,113,114,115,116,117,118,119,120 },
+        }, {
+            0
+        } }));
+}
+
+TEST_CASE(ble_gatt_write_test_long_queue_full)
+{
+    int off;
+    int len;
+    int rc;
+    int i;
+
+    ble_gatt_write_test_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    rc = ble_hs_test_util_gatt_write_long_flat(
+        2, 100, ble_gatt_write_test_attr_value, 128,
+        ble_gatt_write_test_cb_good, NULL);
+    TEST_ASSERT(rc == 0);
+
+    off = 0;
+    for (i = 0; i < 2; i++) {
+        /* Verify prep write request was sent. */
+        ble_hs_test_util_tx_all();
+        TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() != NULL);
+
+        /* Receive Prep Write response. */
+        len = BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
+        ble_gatt_write_test_rx_prep_rsp(
+            2, 100, off, ble_gatt_write_test_attr_value + off, len);
+
+        /* Verify callback hasn't gotten called. */
+        TEST_ASSERT(!ble_gatt_write_test_cb_called);
+
+        off += len;
+    }
+
+    /* Verify prep write request was sent. */
+    ble_hs_test_util_tx_all();
+    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() != NULL);
+
+    /* Receive queue full error. */
+    ble_hs_test_util_rx_att_err_rsp(2, BLE_ATT_OP_PREP_WRITE_REQ,
+                                    BLE_ATT_ERR_PREPARE_QUEUE_FULL, 100);
+
+    /* Verify callback was called. */
+    TEST_ASSERT(ble_gatt_write_test_cb_called);
+    TEST_ASSERT(ble_gatt_write_test_error.status ==
+                BLE_HS_ATT_ERR(BLE_ATT_ERR_PREPARE_QUEUE_FULL));
+    TEST_ASSERT(ble_gatt_write_test_error.att_handle == 100);
+
+    /* Verify clear queue command got sent. */
+    ble_hs_test_util_verify_tx_exec_write(0);
+}
+
+TEST_SUITE(ble_gatt_write_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gatt_write_test_no_rsp();
+    ble_gatt_write_test_rsp();
+    ble_gatt_write_test_long_good();
+    ble_gatt_write_test_long_bad_handle();
+    ble_gatt_write_test_long_bad_offset();
+    ble_gatt_write_test_long_bad_value();
+    ble_gatt_write_test_long_bad_length();
+    ble_gatt_write_test_long_queue_full();
+    ble_gatt_write_test_reliable_good();
+}
+
+int
+ble_gatt_write_test_all(void)
+{
+    ble_gatt_write_test_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_gatts_notify_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gatts_notify_test.c b/net/nimble/host/test/src/ble_gatts_notify_test.c
new file mode 100644
index 0000000..4516e66
--- /dev/null
+++ b/net/nimble/host/test/src/ble_gatts_notify_test.c
@@ -0,0 +1,983 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "host/ble_uuid.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+#include "ble_hs_test_util_store.h"
+
+#define BLE_GATTS_NOTIFY_TEST_CHR_1_UUID    0x1111
+#define BLE_GATTS_NOTIFY_TEST_CHR_2_UUID    0x2222
+
+#define BLE_GATTS_NOTIFY_TEST_MAX_EVENTS    16
+
+static uint8_t ble_gatts_notify_test_peer_addr[6] = {2,3,4,5,6,7};
+
+static int
+ble_gatts_notify_test_misc_access(uint16_t conn_handle,
+                                  uint16_t attr_handle, 
+                                  struct ble_gatt_access_ctxt *ctxt,
+                                  void *arg);
+static void
+ble_gatts_notify_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt,
+                                  void *arg);
+
+static const struct ble_gatt_svc_def ble_gatts_notify_test_svcs[] = { {
+    .type = BLE_GATT_SVC_TYPE_PRIMARY,
+    .uuid128 = BLE_UUID16(0x1234),
+    .characteristics = (struct ble_gatt_chr_def[]) { {
+        .uuid128 = BLE_UUID16(BLE_GATTS_NOTIFY_TEST_CHR_1_UUID),
+        .access_cb = ble_gatts_notify_test_misc_access,
+        .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY |
+                 BLE_GATT_CHR_F_INDICATE,
+    }, {
+        .uuid128 = BLE_UUID16(BLE_GATTS_NOTIFY_TEST_CHR_2_UUID),
+        .access_cb = ble_gatts_notify_test_misc_access,
+        .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY |
+                 BLE_GATT_CHR_F_INDICATE,
+    }, {
+        0
+    } },
+}, {
+    0
+} };
+
+
+static uint16_t ble_gatts_notify_test_chr_1_def_handle;
+static uint8_t ble_gatts_notify_test_chr_1_val[1024];
+static int ble_gatts_notify_test_chr_1_len;
+static uint16_t ble_gatts_notify_test_chr_2_def_handle;
+static uint8_t ble_gatts_notify_test_chr_2_val[1024];
+static int ble_gatts_notify_test_chr_2_len;
+
+static struct ble_gap_event
+ble_gatts_notify_test_events[BLE_GATTS_NOTIFY_TEST_MAX_EVENTS];
+
+static int ble_gatts_notify_test_num_events;
+
+typedef int ble_store_write_fn(int obj_type, union ble_store_value *val);
+
+typedef int ble_store_delete_fn(int obj_type, union ble_store_key *key);
+
+static int
+ble_gatts_notify_test_util_gap_event(struct ble_gap_event *event, void *arg)
+{
+    switch (event->type) {
+    case BLE_GAP_EVENT_NOTIFY_TX:
+    case BLE_GAP_EVENT_SUBSCRIBE:
+        TEST_ASSERT_FATAL(ble_gatts_notify_test_num_events <
+                          BLE_GATTS_NOTIFY_TEST_MAX_EVENTS);
+
+        ble_gatts_notify_test_events[ble_gatts_notify_test_num_events++] =
+            *event;
+
+    default:
+        break;
+    }
+
+    return 0;
+}
+
+static uint16_t
+ble_gatts_notify_test_misc_read_notify(uint16_t conn_handle,
+                                       uint16_t chr_def_handle)
+{
+    struct ble_att_read_req req;
+    struct os_mbuf *om;
+    uint8_t buf[BLE_ATT_READ_REQ_SZ];
+    uint16_t flags;
+    int rc;
+
+    req.barq_handle = chr_def_handle + 2;
+    ble_att_read_req_write(buf, sizeof buf, &req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+    TEST_ASSERT_FATAL(om->om_len == 3);
+    TEST_ASSERT_FATAL(om->om_data[0] == BLE_ATT_OP_READ_RSP);
+
+    flags = le16toh(om->om_data + 1);
+    return flags;
+}
+
+static void
+ble_gatts_notify_test_misc_enable_notify(uint16_t conn_handle,
+                                         uint16_t chr_def_handle,
+                                         uint16_t flags)
+{
+    struct ble_att_write_req req;
+    uint8_t buf[BLE_ATT_WRITE_REQ_BASE_SZ + 2];
+    int rc;
+
+    req.bawq_handle = chr_def_handle + 2;
+    ble_att_write_req_write(buf, sizeof buf, &req);
+
+    htole16(buf + BLE_ATT_WRITE_REQ_BASE_SZ, flags);
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+ble_gatts_notify_test_util_next_event(struct ble_gap_event *event)
+{
+    TEST_ASSERT_FATAL(ble_gatts_notify_test_num_events > 0);
+
+    *event = *ble_gatts_notify_test_events;
+
+    ble_gatts_notify_test_num_events--;
+    if (ble_gatts_notify_test_num_events > 0) {
+        memmove(ble_gatts_notify_test_events + 0,
+                ble_gatts_notify_test_events + 1,
+                ble_gatts_notify_test_num_events * sizeof *event);
+    }
+}
+
+static void
+ble_gatts_notify_test_util_verify_sub_event(uint16_t conn_handle,
+                                            uint8_t attr_handle,
+                                            uint8_t reason,
+                                            uint8_t prevn, uint8_t curn,
+                                            uint8_t previ, uint8_t curi)
+{
+    struct ble_gap_event event;
+
+    ble_gatts_notify_test_util_next_event(&event);
+
+    TEST_ASSERT(event.type == BLE_GAP_EVENT_SUBSCRIBE);
+    TEST_ASSERT(event.subscribe.conn_handle == conn_handle);
+    TEST_ASSERT(event.subscribe.attr_handle == attr_handle);
+    TEST_ASSERT(event.subscribe.reason == reason);
+    TEST_ASSERT(event.subscribe.prev_notify == prevn);
+    TEST_ASSERT(event.subscribe.cur_notify == curn);
+    TEST_ASSERT(event.subscribe.prev_indicate == previ);
+    TEST_ASSERT(event.subscribe.cur_indicate == curi);
+}
+
+static void
+ble_gatts_notify_test_util_verify_tx_event(uint16_t conn_handle,
+                                           uint8_t attr_handle,
+                                           int status,
+                                           int indication)
+{
+    struct ble_gap_event event;
+
+    ble_gatts_notify_test_util_next_event(&event);
+
+    TEST_ASSERT(event.type == BLE_GAP_EVENT_NOTIFY_TX);
+    TEST_ASSERT(event.notify_tx.status == status);
+    TEST_ASSERT(event.notify_tx.conn_handle == conn_handle);
+    TEST_ASSERT(event.notify_tx.attr_handle == attr_handle);
+    TEST_ASSERT(event.notify_tx.indication == indication);
+}
+
+static void
+ble_gatts_notify_test_util_verify_ack_event(uint16_t conn_handle,
+                                            uint8_t attr_handle)
+{
+    ble_gatts_notify_test_util_verify_tx_event(conn_handle, attr_handle,
+                                               BLE_HS_EDONE, 1);
+}
+
+static void
+ble_gatts_notify_test_misc_init(uint16_t *out_conn_handle, int bonding,
+                                uint16_t chr1_flags, uint16_t chr2_flags)
+{
+    struct ble_hs_conn *conn;
+    uint16_t flags;
+    int exp_num_cccds;
+    int rc;
+
+    ble_hs_test_util_init();
+
+    ble_gatts_notify_test_num_events = 0;
+
+    ble_hs_test_util_store_init(10, 10, 10);
+    ble_hs_cfg.store_read_cb = ble_hs_test_util_store_read;
+    ble_hs_cfg.store_write_cb = ble_hs_test_util_store_write;
+
+    rc = ble_gatts_register_svcs(ble_gatts_notify_test_svcs,
+                                 ble_gatts_notify_test_misc_reg_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT_FATAL(ble_gatts_notify_test_chr_1_def_handle != 0);
+    TEST_ASSERT_FATAL(ble_gatts_notify_test_chr_2_def_handle != 0);
+
+    ble_gatts_start();
+
+    ble_hs_test_util_create_conn(2, ble_gatts_notify_test_peer_addr,
+                                 ble_gatts_notify_test_util_gap_event, NULL);
+    *out_conn_handle = 2;
+
+    if (bonding) {
+        ble_hs_lock();
+        conn = ble_hs_conn_find(2);
+        TEST_ASSERT_FATAL(conn != NULL);
+        conn->bhc_sec_state.encrypted = 1;
+        conn->bhc_sec_state.authenticated = 1;
+        conn->bhc_sec_state.bonded = 1;
+        ble_hs_unlock();
+    }
+
+    /* Ensure notifications disabled on new connection. */
+    flags = ble_gatts_notify_test_misc_read_notify(
+        2, ble_gatts_notify_test_chr_1_def_handle);
+    TEST_ASSERT(flags == 0);
+    flags = ble_gatts_notify_test_misc_read_notify(
+        2, ble_gatts_notify_test_chr_2_def_handle);
+    TEST_ASSERT(flags == 0);
+
+    /* Set initial notification / indication state and verify that subscription
+     * callback gets executed.
+     */
+    if (chr1_flags != 0) {
+        ble_gatts_notify_test_misc_enable_notify(
+            2, ble_gatts_notify_test_chr_1_def_handle, chr1_flags);
+
+        ble_gatts_notify_test_util_verify_sub_event(
+            *out_conn_handle,
+            ble_gatts_notify_test_chr_1_def_handle + 1,
+            BLE_GAP_SUBSCRIBE_REASON_WRITE,
+            0, chr1_flags == BLE_GATTS_CLT_CFG_F_NOTIFY,
+            0, chr1_flags == BLE_GATTS_CLT_CFG_F_INDICATE);
+    }
+    if (chr2_flags != 0) {
+        ble_gatts_notify_test_misc_enable_notify(
+            2, ble_gatts_notify_test_chr_2_def_handle, chr2_flags);
+
+        ble_gatts_notify_test_util_verify_sub_event(
+            *out_conn_handle,
+            ble_gatts_notify_test_chr_2_def_handle + 1,
+            BLE_GAP_SUBSCRIBE_REASON_WRITE,
+            0, chr2_flags == BLE_GATTS_CLT_CFG_F_NOTIFY,
+            0, chr2_flags == BLE_GATTS_CLT_CFG_F_INDICATE);
+    }
+
+    /* Ensure no extraneous subscription callbacks were executed. */
+    TEST_ASSERT(ble_gatts_notify_test_num_events == 0);
+
+    /* Toss both write responses. */
+    ble_hs_test_util_prev_tx_queue_clear();
+
+    /* Ensure notification / indication state reads back correctly. */
+    flags = ble_gatts_notify_test_misc_read_notify(
+        2, ble_gatts_notify_test_chr_1_def_handle);
+    TEST_ASSERT(flags == chr1_flags);
+    flags = ble_gatts_notify_test_misc_read_notify(
+        2, ble_gatts_notify_test_chr_2_def_handle);
+    TEST_ASSERT(flags == chr2_flags);
+
+    /* Ensure both CCCDs still persisted. */
+    if (bonding) {
+        exp_num_cccds = (chr1_flags != 0) + (chr2_flags != 0);
+    } else {
+        exp_num_cccds = 0;
+    }
+    TEST_ASSERT(ble_hs_test_util_store_num_cccds == exp_num_cccds);
+}
+
+static void
+ble_gatts_notify_test_disconnect(uint16_t conn_handle,
+                                 uint8_t chr1_flags,
+                                 uint8_t chr1_indicate_in_progress,
+                                 uint8_t chr2_flags,
+                                 uint8_t chr2_indicate_in_progress)
+{
+    ble_hs_test_util_conn_disconnect(conn_handle);
+
+    if (chr1_indicate_in_progress) {
+        ble_gatts_notify_test_util_verify_tx_event(
+            conn_handle,
+            ble_gatts_notify_test_chr_1_def_handle + 1,
+            BLE_HS_ENOTCONN,
+            1);
+    }
+
+    /* Verify subscription callback executed for each subscribed
+     * characteristic.
+     */
+    if (chr1_flags != 0) {
+        ble_gatts_notify_test_util_verify_sub_event(
+            conn_handle,
+            ble_gatts_notify_test_chr_1_def_handle + 1,
+            BLE_GAP_SUBSCRIBE_REASON_TERM,
+            chr1_flags == BLE_GATTS_CLT_CFG_F_NOTIFY, 0,
+            chr1_flags == BLE_GATTS_CLT_CFG_F_INDICATE, 0);
+    }
+
+    if (chr2_indicate_in_progress) {
+        ble_gatts_notify_test_util_verify_tx_event(
+            conn_handle,
+            ble_gatts_notify_test_chr_2_def_handle + 1,
+            BLE_HS_ENOTCONN,
+            1);
+    }
+
+    if (chr2_flags != 0) {
+        ble_gatts_notify_test_util_verify_sub_event(
+            conn_handle,
+            ble_gatts_notify_test_chr_2_def_handle + 1,
+            BLE_GAP_SUBSCRIBE_REASON_TERM,
+            chr2_flags == BLE_GATTS_CLT_CFG_F_NOTIFY, 0,
+            chr2_flags == BLE_GATTS_CLT_CFG_F_INDICATE, 0);
+    }
+}
+
+static void
+ble_gatts_notify_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt,
+                                  void *arg)
+{
+    uint16_t uuid16;
+
+    if (ctxt->op == BLE_GATT_REGISTER_OP_CHR) {
+        uuid16 = ble_uuid_128_to_16(ctxt->chr.chr_def->uuid128);
+        switch (uuid16) {
+        case BLE_GATTS_NOTIFY_TEST_CHR_1_UUID:
+            ble_gatts_notify_test_chr_1_def_handle = ctxt->chr.def_handle;
+            break;
+
+        case BLE_GATTS_NOTIFY_TEST_CHR_2_UUID:
+            ble_gatts_notify_test_chr_2_def_handle = ctxt->chr.def_handle;
+            break;
+
+        default:
+            TEST_ASSERT_FATAL(0);
+            break;
+        }
+    }
+}
+
+static int
+ble_gatts_notify_test_misc_access(uint16_t conn_handle,
+                                  uint16_t attr_handle,
+                                  struct ble_gatt_access_ctxt *ctxt,
+                                  void *arg)
+{
+    int rc;
+
+    TEST_ASSERT_FATAL(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
+    TEST_ASSERT(conn_handle == 0xffff);
+
+    if (attr_handle == ble_gatts_notify_test_chr_1_def_handle + 1) {
+        TEST_ASSERT(ctxt->chr ==
+                    &ble_gatts_notify_test_svcs[0].characteristics[0]);
+        rc = os_mbuf_copyinto(ctxt->om, 0, ble_gatts_notify_test_chr_1_val,
+                              ble_gatts_notify_test_chr_1_len);
+        TEST_ASSERT_FATAL(rc == 0);
+    } else if (attr_handle == ble_gatts_notify_test_chr_2_def_handle + 1) {
+        TEST_ASSERT(ctxt->chr ==
+                    &ble_gatts_notify_test_svcs[0].characteristics[1]);
+        rc = os_mbuf_copyinto(ctxt->om, 0, ble_gatts_notify_test_chr_2_val,
+                              ble_gatts_notify_test_chr_2_len);
+        TEST_ASSERT_FATAL(rc == 0);
+    } else {
+        TEST_ASSERT_FATAL(0);
+    }
+
+    return 0;
+}
+
+static void
+ble_gatts_notify_test_misc_rx_indicate_rsp(uint16_t conn_handle,
+                                           uint16_t attr_handle)
+{
+    uint8_t buf[BLE_ATT_INDICATE_RSP_SZ];
+    int rc;
+
+    ble_att_indicate_rsp_write(buf, sizeof buf);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+
+    ble_gatts_notify_test_util_verify_ack_event(conn_handle, attr_handle);
+}
+
+
+static void
+ble_gatts_notify_test_misc_verify_tx_n(uint16_t conn_handle,
+                                       uint16_t attr_handle,
+                                       uint8_t *attr_data, int attr_len)
+{
+    struct ble_att_notify_req req;
+    struct os_mbuf *om;
+    int i;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    ble_att_notify_req_parse(om->om_data, om->om_len, &req);
+    TEST_ASSERT(req.banq_handle == attr_handle);
+
+    for (i = 0; i < attr_len; i++) {
+        TEST_ASSERT(om->om_data[BLE_ATT_NOTIFY_REQ_BASE_SZ + i] ==
+                    attr_data[i]);
+    }
+
+    ble_gatts_notify_test_util_verify_tx_event(conn_handle, attr_handle, 0, 0);
+}
+
+static void
+ble_gatts_notify_test_misc_verify_tx_i(uint16_t conn_handle,
+                                       uint16_t attr_handle,
+                                       uint8_t *attr_data, int attr_len)
+{
+    struct ble_att_indicate_req req;
+    struct os_mbuf *om;
+    int i;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    ble_att_indicate_req_parse(om->om_data, om->om_len, &req);
+    TEST_ASSERT(req.baiq_handle == attr_handle);
+
+    for (i = 0; i < attr_len; i++) {
+        TEST_ASSERT(om->om_data[BLE_ATT_INDICATE_REQ_BASE_SZ + i] ==
+                    attr_data[i]);
+    }
+
+    ble_gatts_notify_test_util_verify_tx_event(conn_handle, attr_handle, 0, 1);
+}
+
+static void
+ble_gatts_notify_test_misc_verify_tx_gen(uint16_t conn_handle, int attr_idx,
+                                         uint8_t chr_flags)
+{
+    uint16_t attr_handle;
+    uint16_t attr_len;
+    void *attr_val;
+
+    switch (attr_idx) {
+    case 1:
+        attr_handle = ble_gatts_notify_test_chr_1_def_handle + 1;
+        attr_len = ble_gatts_notify_test_chr_1_len;
+        attr_val = ble_gatts_notify_test_chr_1_val;
+        break;
+
+    case 2:
+        attr_handle = ble_gatts_notify_test_chr_2_def_handle + 1;
+        attr_len = ble_gatts_notify_test_chr_2_len;
+        attr_val = ble_gatts_notify_test_chr_2_val;
+        break;
+
+    default:
+        TEST_ASSERT_FATAL(0);
+        break;
+    }
+
+    switch (chr_flags) {
+    case 0:
+        break;
+
+    case BLE_GATTS_CLT_CFG_F_NOTIFY:
+        ble_gatts_notify_test_misc_verify_tx_n(conn_handle, attr_handle,
+                                               attr_val, attr_len);
+        break;
+
+    case BLE_GATTS_CLT_CFG_F_INDICATE:
+        ble_gatts_notify_test_misc_verify_tx_i(conn_handle, attr_handle,
+                                               attr_val, attr_len);
+        break;
+
+    default:
+        TEST_ASSERT_FATAL(0);
+        break;
+    }
+}
+
+static void
+ble_gatts_notify_test_restore_bonding(uint16_t conn_handle,
+                                      uint8_t chr1_flags, uint8_t chr1_tx,
+                                      uint8_t chr2_flags, uint8_t chr2_tx)
+{
+    struct ble_hs_conn *conn;
+
+    ble_hs_lock();
+    conn = ble_hs_conn_find(conn_handle);
+    TEST_ASSERT_FATAL(conn != NULL);
+    conn->bhc_sec_state.encrypted = 1;
+    conn->bhc_sec_state.authenticated = 1;
+    conn->bhc_sec_state.bonded = 1;
+    ble_hs_unlock();
+
+    ble_gatts_bonding_restored(conn_handle);
+
+    /* Verify subscription callback executed for each subscribed
+     * characteristic.
+     */
+    if (chr1_flags != 0) {
+        ble_gatts_notify_test_util_verify_sub_event(
+            conn_handle,
+            ble_gatts_notify_test_chr_1_def_handle + 1,
+            BLE_GAP_SUBSCRIBE_REASON_RESTORE,
+            0, chr1_flags == BLE_GATTS_CLT_CFG_F_NOTIFY,
+            0, chr1_flags == BLE_GATTS_CLT_CFG_F_INDICATE);
+
+    }
+    if (chr1_tx) {
+        ble_gatts_notify_test_misc_verify_tx_gen(conn_handle, 1, chr1_flags);
+    }
+
+
+    if (chr2_flags != 0) {
+        ble_gatts_notify_test_util_verify_sub_event(
+            conn_handle,
+            ble_gatts_notify_test_chr_2_def_handle + 1,
+            BLE_GAP_SUBSCRIBE_REASON_RESTORE,
+            0, chr2_flags == BLE_GATTS_CLT_CFG_F_NOTIFY,
+            0, chr2_flags == BLE_GATTS_CLT_CFG_F_INDICATE);
+    }
+    if (chr2_tx) {
+        ble_gatts_notify_test_misc_verify_tx_gen(conn_handle, 2, chr2_flags);
+    }
+}
+
+TEST_CASE(ble_gatts_notify_test_n)
+{
+    uint16_t conn_handle;
+    uint16_t flags;
+
+    ble_gatts_notify_test_misc_init(&conn_handle, 0,
+                                    BLE_GATTS_CLT_CFG_F_NOTIFY,
+                                    BLE_GATTS_CLT_CFG_F_NOTIFY);
+
+    /* Ensure notifications read back as enabled. */
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
+    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_NOTIFY);
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
+    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_NOTIFY);
+
+    /* Update characteristic 1's value. */
+    ble_gatts_notify_test_chr_1_len = 1;
+    ble_gatts_notify_test_chr_1_val[0] = 0xab;
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
+
+    /* Verify notification sent properly. */
+    ble_gatts_notify_test_misc_verify_tx_n(
+        conn_handle,
+        ble_gatts_notify_test_chr_1_def_handle + 1,
+        ble_gatts_notify_test_chr_1_val,
+        ble_gatts_notify_test_chr_1_len);
+
+    /* Update characteristic 2's value. */
+    ble_gatts_notify_test_chr_2_len = 16;
+    memcpy(ble_gatts_notify_test_chr_2_val,
+           ((uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}), 16);
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
+
+    /* Verify notification sent properly. */
+    ble_gatts_notify_test_misc_verify_tx_n(
+        conn_handle,
+        ble_gatts_notify_test_chr_2_def_handle + 1,
+        ble_gatts_notify_test_chr_2_val,
+        ble_gatts_notify_test_chr_2_len);
+
+    /***
+     * Disconnect, modify characteristic values, and reconnect.  Ensure
+     * notifications are not sent and are no longer enabled.
+     */
+
+    ble_gatts_notify_test_disconnect(conn_handle,
+                                     BLE_GATTS_CLT_CFG_F_NOTIFY, 0,
+                                     BLE_GATTS_CLT_CFG_F_NOTIFY, 0);
+
+    /* Update characteristic 1's value. */
+    ble_gatts_notify_test_chr_1_len = 1;
+    ble_gatts_notify_test_chr_1_val[0] = 0xdd;
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
+
+    /* Update characteristic 2's value. */
+    ble_gatts_notify_test_chr_2_len = 16;
+    memcpy(ble_gatts_notify_test_chr_2_val,
+           ((uint8_t[]){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}), 16);
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
+
+    ble_hs_test_util_create_conn(conn_handle, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 ble_gatts_notify_test_util_gap_event, NULL);
+
+    /* Ensure no notifications sent. */
+    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
+
+    /* Ensure notifications disabled. */
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
+    TEST_ASSERT(flags == 0);
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
+    TEST_ASSERT(flags == 0);
+}
+
+TEST_CASE(ble_gatts_notify_test_i)
+{
+    uint16_t conn_handle;
+    uint16_t flags;
+
+    ble_gatts_notify_test_misc_init(&conn_handle, 0,
+                                    BLE_GATTS_CLT_CFG_F_INDICATE,
+                                    BLE_GATTS_CLT_CFG_F_INDICATE);
+
+    /* Update characteristic 1's value. */
+    ble_gatts_notify_test_chr_1_len = 1;
+    ble_gatts_notify_test_chr_1_val[0] = 0xab;
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
+
+    /* Verify indication sent properly. */
+    ble_gatts_notify_test_misc_verify_tx_i(
+        conn_handle,
+        ble_gatts_notify_test_chr_1_def_handle + 1,
+        ble_gatts_notify_test_chr_1_val,
+        ble_gatts_notify_test_chr_1_len);
+
+    /* Update characteristic 2's value. */
+    ble_gatts_notify_test_chr_2_len = 16;
+    memcpy(ble_gatts_notify_test_chr_2_val,
+           ((uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}), 16);
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
+
+    /* Verify the second indication doesn't get sent until the first is
+     * confirmed.
+     */
+    ble_hs_test_util_tx_all();
+    TEST_ASSERT(ble_hs_test_util_prev_tx_queue_sz() == 0);
+
+    /* Receive the confirmation for the first indication. */
+    ble_gatts_notify_test_misc_rx_indicate_rsp(
+        conn_handle,
+        ble_gatts_notify_test_chr_1_def_handle + 1);
+
+    /* Verify indication sent properly. */
+    ble_hs_test_util_tx_all();
+    ble_gatts_notify_test_misc_verify_tx_i(
+        conn_handle,
+        ble_gatts_notify_test_chr_2_def_handle + 1,
+        ble_gatts_notify_test_chr_2_val,
+        ble_gatts_notify_test_chr_2_len);
+
+    /* Receive the confirmation for the second indication. */
+    ble_gatts_notify_test_misc_rx_indicate_rsp(
+        conn_handle,
+        ble_gatts_notify_test_chr_2_def_handle + 1);
+
+    /* Verify no pending GATT jobs. */
+    TEST_ASSERT(!ble_gattc_any_jobs());
+
+    /***
+     * Disconnect, modify characteristic values, and reconnect.  Ensure
+     * indications are not sent and are no longer enabled.
+     */
+
+    ble_gatts_notify_test_disconnect(conn_handle,
+                                     BLE_GATTS_CLT_CFG_F_INDICATE, 0,
+                                     BLE_GATTS_CLT_CFG_F_INDICATE, 0);
+
+    /* Update characteristic 1's value. */
+    ble_gatts_notify_test_chr_1_len = 1;
+    ble_gatts_notify_test_chr_1_val[0] = 0xdd;
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
+
+    /* Update characteristic 2's value. */
+    ble_gatts_notify_test_chr_2_len = 16;
+    memcpy(ble_gatts_notify_test_chr_2_val,
+           ((uint8_t[]){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}), 16);
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
+
+    ble_hs_test_util_create_conn(conn_handle, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 ble_gatts_notify_test_util_gap_event, NULL);
+
+    /* Ensure no indications sent. */
+    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
+
+    /* Ensure indications disabled. */
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
+    TEST_ASSERT(flags == 0);
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
+    TEST_ASSERT(flags == 0);
+}
+
+TEST_CASE(ble_gatts_notify_test_bonded_n)
+{
+    uint16_t conn_handle;
+    uint16_t flags;
+
+    ble_gatts_notify_test_misc_init(&conn_handle, 1,
+                                    BLE_GATTS_CLT_CFG_F_NOTIFY,
+                                    BLE_GATTS_CLT_CFG_F_NOTIFY);
+
+    /* Disconnect. */
+    ble_gatts_notify_test_disconnect(conn_handle,
+                                     BLE_GATTS_CLT_CFG_F_NOTIFY, 0,
+                                     BLE_GATTS_CLT_CFG_F_NOTIFY, 0);
+
+    /* Ensure both CCCDs still persisted. */
+    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 2);
+
+    /* Update characteristic 1's value. */
+    ble_gatts_notify_test_chr_1_len = 1;
+    ble_gatts_notify_test_chr_1_val[0] = 0xdd;
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
+
+    /* Update characteristic 2's value. */
+    ble_gatts_notify_test_chr_2_len = 16;
+    memcpy(ble_gatts_notify_test_chr_2_val,
+           ((uint8_t[]){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}), 16);
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
+
+    /* Reconnect; ensure notifications don't get sent while unbonded and that
+     * notifications appear disabled.
+     */
+
+    ble_hs_test_util_create_conn(conn_handle, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 ble_gatts_notify_test_util_gap_event, NULL);
+
+    ble_gatts_notify_test_num_events = 0;
+    /* Ensure no notifications sent. */
+    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
+
+    /* Ensure notifications disabled. */
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
+    TEST_ASSERT(flags == 0);
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
+    TEST_ASSERT(flags == 0);
+
+    /* Simulate a successful encryption procedure (bonding restoration). */
+    ble_gatts_notify_test_restore_bonding(conn_handle,
+                                          BLE_GATTS_CLT_CFG_F_NOTIFY, 1,
+                                          BLE_GATTS_CLT_CFG_F_NOTIFY, 1);
+
+    /* Ensure notifications enabled. */
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
+    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_NOTIFY);
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
+    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_NOTIFY);
+
+    /* Ensure both CCCDs still persisted. */
+    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 2);
+}
+
+TEST_CASE(ble_gatts_notify_test_bonded_i)
+{
+    uint16_t conn_handle;
+    uint16_t flags;
+
+    ble_gatts_notify_test_misc_init(&conn_handle, 1,
+                                    BLE_GATTS_CLT_CFG_F_INDICATE,
+                                    BLE_GATTS_CLT_CFG_F_INDICATE);
+
+    /* Disconnect. */
+    ble_gatts_notify_test_disconnect(conn_handle,
+                                     BLE_GATTS_CLT_CFG_F_INDICATE, 0,
+                                     BLE_GATTS_CLT_CFG_F_INDICATE, 0);
+
+    /* Ensure both CCCDs still persisted. */
+    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 2);
+
+    /* Update characteristic 1's value. */
+    ble_gatts_notify_test_chr_1_len = 1;
+    ble_gatts_notify_test_chr_1_val[0] = 0xab;
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
+
+    /* Update characteristic 2's value. */
+    ble_gatts_notify_test_chr_2_len = 16;
+    memcpy(ble_gatts_notify_test_chr_2_val,
+           ((uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}), 16);
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
+
+    /* Reconnect; ensure notifications don't get sent while unbonded and that
+     * notifications appear disabled.
+     */
+
+    ble_hs_test_util_create_conn(conn_handle, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 ble_gatts_notify_test_util_gap_event, NULL);
+
+    /* Ensure no indications sent. */
+    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
+
+    /* Ensure notifications disabled. */
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
+    TEST_ASSERT(flags == 0);
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
+    TEST_ASSERT(flags == 0);
+
+    /* Simulate a successful encryption procedure (bonding restoration). */
+    ble_gatts_notify_test_restore_bonding(conn_handle,
+                                          BLE_GATTS_CLT_CFG_F_INDICATE, 1,
+                                          BLE_GATTS_CLT_CFG_F_INDICATE, 0);
+
+    /* Verify the second indication doesn't get sent until the first is
+     * confirmed.
+     */
+    ble_hs_test_util_tx_all();
+    TEST_ASSERT(ble_hs_test_util_prev_tx_queue_sz() == 0);
+
+    /* Receive the confirmation for the first indication. */
+    ble_gatts_notify_test_misc_rx_indicate_rsp(
+        conn_handle,
+        ble_gatts_notify_test_chr_1_def_handle + 1);
+
+    /* Verify indication sent properly. */
+    ble_hs_test_util_tx_all();
+    ble_gatts_notify_test_misc_verify_tx_i(
+        conn_handle,
+        ble_gatts_notify_test_chr_2_def_handle + 1,
+        ble_gatts_notify_test_chr_2_val,
+        ble_gatts_notify_test_chr_2_len);
+
+    /* Receive the confirmation for the second indication. */
+    ble_gatts_notify_test_misc_rx_indicate_rsp(
+        conn_handle,
+        ble_gatts_notify_test_chr_2_def_handle + 1);
+
+    /* Verify no pending GATT jobs. */
+    TEST_ASSERT(!ble_gattc_any_jobs());
+
+    /* Ensure notifications enabled. */
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
+    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_INDICATE);
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
+    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_INDICATE);
+
+    /* Ensure both CCCDs still persisted. */
+    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 2);
+}
+
+TEST_CASE(ble_gatts_notify_test_bonded_i_no_ack)
+{
+    struct ble_store_value_cccd value_cccd;
+    struct ble_store_key_cccd key_cccd;
+    uint16_t conn_handle;
+    uint16_t flags;
+    int rc;
+
+    ble_gatts_notify_test_misc_init(&conn_handle, 1,
+                                    BLE_GATTS_CLT_CFG_F_INDICATE, 0);
+
+    /* Update characteristic 1's value. */
+    ble_gatts_notify_test_chr_1_len = 1;
+    ble_gatts_notify_test_chr_1_val[0] = 0xab;
+    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
+
+    /* Verify indication sent properly. */
+    ble_hs_test_util_tx_all();
+    ble_gatts_notify_test_misc_verify_tx_i(
+        conn_handle,
+        ble_gatts_notify_test_chr_1_def_handle + 1,
+        ble_gatts_notify_test_chr_1_val,
+        ble_gatts_notify_test_chr_1_len);
+
+    /* Verify 'updated' state is still persisted. */
+    key_cccd.peer_addr_type = BLE_STORE_ADDR_TYPE_NONE;
+    key_cccd.chr_val_handle = ble_gatts_notify_test_chr_1_def_handle + 1;
+    key_cccd.idx = 0;
+
+    rc = ble_store_read_cccd(&key_cccd, &value_cccd);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(value_cccd.value_changed);
+
+    /* Disconnect. */
+    ble_gatts_notify_test_disconnect(conn_handle,
+                                     BLE_GATTS_CLT_CFG_F_INDICATE, 1, 0, 0);
+
+    /* Ensure CCCD still persisted. */
+    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 1);
+
+    /* Reconnect. */
+    ble_hs_test_util_create_conn(conn_handle, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 ble_gatts_notify_test_util_gap_event, NULL);
+
+    /* Simulate a successful encryption procedure (bonding restoration). */
+    ble_gatts_notify_test_restore_bonding(conn_handle,
+                                          BLE_GATTS_CLT_CFG_F_INDICATE, 1,
+                                          0, 0);
+
+    /* Receive the confirmation for the indication. */
+    ble_gatts_notify_test_misc_rx_indicate_rsp(
+        conn_handle,
+        ble_gatts_notify_test_chr_1_def_handle + 1);
+
+    /* Verify no pending GATT jobs. */
+    TEST_ASSERT(!ble_gattc_any_jobs());
+
+    /* Ensure indication enabled. */
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
+    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_INDICATE);
+    flags = ble_gatts_notify_test_misc_read_notify(
+        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
+    TEST_ASSERT(flags == 0);
+
+    /* Ensure CCCD still persisted. */
+    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 1);
+
+    /* Verify 'updated' state is no longer persisted. */
+    rc = ble_store_read_cccd(&key_cccd, &value_cccd);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(!value_cccd.value_changed);
+}
+
+TEST_SUITE(ble_gatts_notify_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gatts_notify_test_n();
+    ble_gatts_notify_test_i();
+
+    ble_gatts_notify_test_bonded_n();
+    ble_gatts_notify_test_bonded_i();
+
+    ble_gatts_notify_test_bonded_i_no_ack();
+
+    /* XXX: Test corner cases:
+     *     o Bonding after CCCD configuration.
+     *     o Disconnect prior to rx of indicate ack.
+     */
+}
+
+int
+ble_gatts_notify_test_all(void)
+{
+    ble_gatts_notify_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_gatts_read_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gatts_read_test.c b/net/nimble/host/test/src/ble_gatts_read_test.c
new file mode 100644
index 0000000..cef9f92
--- /dev/null
+++ b/net/nimble/host/test/src/ble_gatts_read_test.c
@@ -0,0 +1,261 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "host/ble_uuid.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+
+#define BLE_GATTS_READ_TEST_CHR_1_UUID    0x1111
+#define BLE_GATTS_READ_TEST_CHR_2_UUID    0x2222
+
+static uint8_t ble_gatts_read_test_peer_addr[6] = {2,3,4,5,6,7};
+
+static int
+ble_gatts_read_test_util_access_1(uint16_t conn_handle,
+                                  uint16_t attr_handle,
+                                  struct ble_gatt_access_ctxt *ctxt,
+                                  void *arg);
+
+static int
+ble_gatts_read_test_util_access_2(uint16_t conn_handle,
+                                  uint16_t attr_handle,
+                                  struct ble_gatt_access_ctxt *ctxt,
+                                  void *arg);
+static void
+ble_gatts_read_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt,
+                                void *arg);
+
+static const struct ble_gatt_svc_def ble_gatts_read_test_svcs[] = { {
+    .type = BLE_GATT_SVC_TYPE_PRIMARY,
+    .uuid128 = BLE_UUID16(0x1234),
+    .characteristics = (struct ble_gatt_chr_def[]) { {
+        .uuid128 = BLE_UUID16(BLE_GATTS_READ_TEST_CHR_1_UUID),
+        .access_cb = ble_gatts_read_test_util_access_1,
+        .flags = BLE_GATT_CHR_F_READ
+    }, {
+        .uuid128 = BLE_UUID16(BLE_GATTS_READ_TEST_CHR_2_UUID),
+        .access_cb = ble_gatts_read_test_util_access_2,
+        .flags = BLE_GATT_CHR_F_READ
+    }, {
+        0
+    } },
+}, {
+    0
+} };
+
+
+static uint16_t ble_gatts_read_test_chr_1_def_handle;
+static uint16_t ble_gatts_read_test_chr_1_val_handle;
+static uint8_t ble_gatts_read_test_chr_1_val[1024];
+static int ble_gatts_read_test_chr_1_len;
+static uint16_t ble_gatts_read_test_chr_2_def_handle;
+static uint16_t ble_gatts_read_test_chr_2_val_handle;
+
+static void
+ble_gatts_read_test_misc_init(uint16_t *out_conn_handle)
+{
+    int rc;
+
+    ble_hs_test_util_init();
+
+    rc = ble_gatts_register_svcs(ble_gatts_read_test_svcs,
+                                 ble_gatts_read_test_misc_reg_cb, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT_FATAL(ble_gatts_read_test_chr_1_def_handle != 0);
+    TEST_ASSERT_FATAL(ble_gatts_read_test_chr_1_val_handle ==
+                      ble_gatts_read_test_chr_1_def_handle + 1);
+    TEST_ASSERT_FATAL(ble_gatts_read_test_chr_2_def_handle != 0);
+    TEST_ASSERT_FATAL(ble_gatts_read_test_chr_2_val_handle ==
+                      ble_gatts_read_test_chr_2_def_handle + 1);
+
+    ble_gatts_start();
+
+    ble_hs_test_util_create_conn(2, ble_gatts_read_test_peer_addr, NULL, NULL);
+
+    if (out_conn_handle != NULL) {
+        *out_conn_handle = 2;
+    }
+}
+
+static void
+ble_gatts_read_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt,
+                                void *arg)
+{
+    uint16_t uuid16;
+
+    if (ctxt->op == BLE_GATT_REGISTER_OP_CHR) {
+        uuid16 = ble_uuid_128_to_16(ctxt->chr.chr_def->uuid128);
+        switch (uuid16) {
+        case BLE_GATTS_READ_TEST_CHR_1_UUID:
+            ble_gatts_read_test_chr_1_def_handle = ctxt->chr.def_handle;
+            ble_gatts_read_test_chr_1_val_handle = ctxt->chr.val_handle;
+            break;
+
+        case BLE_GATTS_READ_TEST_CHR_2_UUID:
+            ble_gatts_read_test_chr_2_def_handle = ctxt->chr.def_handle;
+            ble_gatts_read_test_chr_2_val_handle = ctxt->chr.val_handle;
+            break;
+
+        default:
+            TEST_ASSERT_FATAL(0);
+            break;
+        }
+    }
+}
+
+static int
+ble_gatts_read_test_util_access_1(uint16_t conn_handle,
+                                  uint16_t attr_handle,
+                                  struct ble_gatt_access_ctxt *ctxt,
+                                  void *arg)
+{
+    int rc;
+
+    TEST_ASSERT_FATAL(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
+    TEST_ASSERT_FATAL(attr_handle == ble_gatts_read_test_chr_1_val_handle);
+
+    TEST_ASSERT(ctxt->chr ==
+                &ble_gatts_read_test_svcs[0].characteristics[0]);
+
+    rc = os_mbuf_append(ctxt->om, ble_gatts_read_test_chr_1_val,
+                        ble_gatts_read_test_chr_1_len);
+    TEST_ASSERT(rc == 0);
+
+    return 0;
+}
+
+static int
+ble_gatts_read_test_util_access_2(uint16_t conn_handle,
+                                  uint16_t attr_handle,
+                                  struct ble_gatt_access_ctxt *ctxt,
+                                  void *arg)
+{
+    uint8_t *buf;
+
+    TEST_ASSERT_FATAL(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
+    TEST_ASSERT_FATAL(attr_handle == ble_gatts_read_test_chr_2_def_handle + 1);
+
+    TEST_ASSERT(ctxt->chr ==
+                &ble_gatts_read_test_svcs[0].characteristics[1]);
+
+    buf = os_mbuf_extend(ctxt->om, 6);
+    TEST_ASSERT_FATAL(buf != NULL);
+
+    buf[0] = 0;
+    buf[1] = 10;
+    buf[2] = 20;
+    buf[3] = 30;
+    buf[4] = 40;
+    buf[5] = 50;
+
+    return 0;
+}
+
+static void
+ble_gatts_read_test_once(uint16_t conn_handle, uint16_t attr_id,
+                         void *expected_value, uint16_t expected_len)
+{
+    struct ble_att_read_req read_req;
+    uint8_t buf[BLE_ATT_READ_REQ_SZ];
+    int rc;
+
+    read_req.barq_handle = attr_id;
+    ble_att_read_req_write(buf, sizeof buf, &read_req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_test_util_verify_tx_read_rsp(expected_value, expected_len);
+}
+
+TEST_CASE(ble_gatts_read_test_case_basic)
+{
+    uint16_t conn_handle;
+
+    ble_gatts_read_test_misc_init(&conn_handle);
+
+    /*** Application points attribute at static data. */
+    ble_gatts_read_test_chr_1_val[0] = 1;
+    ble_gatts_read_test_chr_1_val[1] = 2;
+    ble_gatts_read_test_chr_1_val[2] = 3;
+    ble_gatts_read_test_chr_1_len = 3;
+    ble_gatts_read_test_once(conn_handle,
+                             ble_gatts_read_test_chr_1_val_handle,
+                             ble_gatts_read_test_chr_1_val,
+                             ble_gatts_read_test_chr_1_len);
+
+    /*** Application uses stack-provided buffer for dynamic attribute. */
+    ble_gatts_read_test_once(conn_handle,
+                             ble_gatts_read_test_chr_2_def_handle + 1,
+                             ((uint8_t[6]){0,10,20,30,40,50}), 6);
+
+}
+
+TEST_CASE(ble_gatts_read_test_case_long)
+{
+    struct ble_att_read_blob_req read_blob_req;
+    struct ble_att_read_req read_req;
+    uint8_t buf[max(BLE_ATT_READ_REQ_SZ, BLE_ATT_READ_BLOB_REQ_SZ)];
+    uint16_t conn_handle;
+    int rc;
+    int i;
+
+    ble_gatts_read_test_misc_init(&conn_handle);
+
+    /*** Prepare characteristic value. */
+    ble_gatts_read_test_chr_1_len = 40;
+    for (i = 0; i < ble_gatts_read_test_chr_1_len; i++) {
+        ble_gatts_read_test_chr_1_val[i] = i;
+    }
+
+    /* Receive first read request. */
+    read_req.barq_handle = ble_gatts_read_test_chr_1_val_handle;
+    ble_att_read_req_write(buf, sizeof buf, &read_req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_test_util_verify_tx_read_rsp(ble_gatts_read_test_chr_1_val, 22);
+
+    /* Receive follow-up read blob request. */
+    read_blob_req.babq_handle = ble_gatts_read_test_chr_1_val_handle;
+    read_blob_req.babq_offset = 22;
+    ble_att_read_blob_req_write(buf, sizeof buf, &read_blob_req);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, sizeof buf);
+    TEST_ASSERT(rc == 0);
+
+    /* Ensure response starts at appropriate offset (22). */
+    ble_hs_test_util_verify_tx_read_blob_rsp(
+        ble_gatts_read_test_chr_1_val + 22, 18);
+}
+
+TEST_SUITE(ble_gatts_read_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gatts_read_test_case_basic();
+    ble_gatts_read_test_case_long();
+}



[14/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_sm_test_util.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_test_util.h b/net/nimble/host/src/test/ble_sm_test_util.h
deleted file mode 100644
index 3323be6..0000000
--- a/net/nimble/host/src/test/ble_sm_test_util.h
+++ /dev/null
@@ -1,119 +0,0 @@
-/**
- * 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_BLE_SM_TEST_UTIL_
-#define H_BLE_SM_TEST_UTIL_
-
-struct ble_sm_test_passkey_info {
-    struct ble_sm_io passkey;
-    uint32_t exp_numcmp;
-    unsigned io_before_rx:1;
-};
-
-struct ble_sm_test_params {
-    uint8_t init_addr_type;
-    uint8_t init_id_addr[6];
-    uint8_t init_rpa[6];
-    uint8_t resp_addr_type;
-    uint8_t resp_id_addr[6];
-    uint8_t resp_rpa[6];
-    struct ble_sm_test_passkey_info passkey_info;
-
-    struct ble_sm_sec_req sec_req;
-    struct ble_sm_pair_cmd pair_req;
-    struct ble_sm_pair_cmd pair_rsp;
-    struct ble_sm_pair_confirm confirm_req[20];
-    struct ble_sm_pair_confirm confirm_rsp[20];
-    struct ble_sm_pair_random random_req[20];
-    struct ble_sm_pair_random random_rsp[20];
-    struct ble_sm_id_info id_info_req;
-    struct ble_sm_id_info id_info_rsp;
-    struct ble_sm_id_addr_info id_addr_info_req;
-    struct ble_sm_id_addr_info id_addr_info_rsp;
-    struct ble_sm_sign_info sign_info_req;
-    struct ble_sm_sign_info sign_info_rsp;
-    struct ble_sm_pair_fail pair_fail;
-
-    int pair_alg;
-    unsigned authenticated:1;
-
-    /*** Secure connections fields. */
-    uint8_t ltk[16];
-    uint8_t our_priv_key[32];
-    struct ble_sm_public_key public_key_req;
-    struct ble_sm_public_key public_key_rsp;
-    struct ble_sm_dhkey_check dhkey_check_req;
-    struct ble_sm_dhkey_check dhkey_check_rsp;
-
-    /*** Legacy fields. */
-    uint8_t stk[16];
-    struct ble_sm_enc_info enc_info_req;
-    struct ble_sm_enc_info enc_info_rsp;
-    struct ble_sm_master_id master_id_req;
-    struct ble_sm_master_id master_id_rsp;
-};
-
-extern int ble_sm_test_gap_event;
-extern int ble_sm_test_gap_status;
-extern struct ble_gap_sec_state ble_sm_test_sec_state;
-
-extern int ble_sm_test_store_obj_type;
-extern union ble_store_key ble_sm_test_store_key;
-extern union ble_store_value ble_sm_test_store_value;
-
-void ble_sm_test_util_init(void);
-int ble_sm_test_util_conn_cb(struct ble_gap_event *ctxt, void *arg);
-void ble_sm_test_util_io_inject(struct ble_sm_test_passkey_info *passkey_info,
-                                uint8_t cur_sm_state);
-void ble_sm_test_util_io_inject_bad(uint16_t conn_handle,
-                                    uint8_t correct_io_act);
-void ble_sm_test_util_io_check_pre(
-    struct ble_sm_test_passkey_info *passkey_info,
-    uint8_t cur_sm_state);
-void ble_sm_test_util_io_check_post(
-    struct ble_sm_test_passkey_info *passkey_info,
-    uint8_t cur_sm_state);
-void ble_sm_test_util_rx_sec_req(uint16_t conn_handle,
-                                 struct ble_sm_sec_req *cmd,
-                                 int exp_status);
-void ble_sm_test_util_verify_tx_pair_fail(struct ble_sm_pair_fail *exp_cmd);
-void ble_sm_test_util_us_lgcy_good(struct ble_sm_test_params *params);
-void ble_sm_test_util_peer_fail_inval(int we_are_master,
-                                      uint8_t *init_addr,
-                                      uint8_t *resp_addr,
-                                      struct ble_sm_pair_cmd *pair_req,
-                                      struct ble_sm_pair_fail *pair_fail);
-void ble_sm_test_util_peer_lgcy_fail_confirm(
-    uint8_t *init_addr,
-    uint8_t *resp_addr,
-    struct ble_sm_pair_cmd *pair_req,
-    struct ble_sm_pair_cmd *pair_rsp,
-    struct ble_sm_pair_confirm *confirm_req,
-    struct ble_sm_pair_confirm *confirm_rsp,
-    struct ble_sm_pair_random *random_req,
-    struct ble_sm_pair_random *random_rsp,
-    struct ble_sm_pair_fail *fail_rsp);
-
-void ble_sm_test_util_peer_lgcy_good(struct ble_sm_test_params *params);
-void ble_sm_test_util_peer_bonding_bad(uint16_t ediv, uint64_t rand_num);
-void ble_sm_test_util_peer_sc_good(struct ble_sm_test_params *params);
-void ble_sm_test_util_us_sc_good(struct ble_sm_test_params *params);
-void ble_sm_test_util_us_fail_inval(struct ble_sm_test_params *params);
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_uuid_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_uuid_test.c b/net/nimble/host/src/test/ble_uuid_test.c
deleted file mode 100644
index 1011303..0000000
--- a/net/nimble/host/src/test/ble_uuid_test.c
+++ /dev/null
@@ -1,93 +0,0 @@
-/**
- * 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 <stddef.h>
-#include <string.h>
-#include "testutil/testutil.h"
-#include "host/ble_hs_test.h"
-#include "host/ble_uuid.h"
-#include "ble_hs_test_util.h"
-
-TEST_CASE(ble_uuid_test_128_to_16)
-{
-    uint16_t uuid16;
-
-    /*** RFCOMM */
-    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
-        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
-        0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00
-    }));
-    TEST_ASSERT(uuid16 == 0x0003);
-
-    /*** BNEP */
-    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
-        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
-        0x00, 0x10, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00
-    }));
-    TEST_ASSERT(uuid16 == 0x000f);
-
-    /*** L2CAP */
-    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
-        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
-        0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00
-    }));
-    TEST_ASSERT(uuid16 == 0x0100);
-
-    /*** ObEXObjectPush */
-    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
-        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
-        0x00, 0x10, 0x00, 0x00, 0x05, 0x11, 0x00, 0x00
-    }));
-    TEST_ASSERT(uuid16 == 0x1105);
-
-    /*** Invalid base. */
-    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
-        0xfb, 0x34, 0x9c, 0x5f, 0x80, 0x00, 0x00, 0x80,
-        0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00
-    }));
-    TEST_ASSERT(uuid16 == 0);
-
-    /*** Invalid prefix. */
-    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
-        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
-        0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x01
-    }));
-    TEST_ASSERT(uuid16 == 0);
-
-    /*** 16-bit UUID of 0. */
-    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
-        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
-        0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
-    }));
-}
-
-TEST_SUITE(ble_uuid_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_uuid_test_128_to_16();
-}
-
-int
-ble_uuid_test_all(void)
-{
-    ble_uuid_test_suite();
-
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/pkg.yml b/net/nimble/host/test/pkg.yml
new file mode 100644
index 0000000..ce5ed4e
--- /dev/null
+++ b/net/nimble/host/test/pkg.yml
@@ -0,0 +1,41 @@
+# 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.
+#
+pkg.name: net/nimble/host/test
+pkg.type: unittest
+pkg.description: "NimBLE host unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+    - libs/testutil
+    - net/nimble/host
+
+pkg.deps.SELFTEST:
+    - libs/console/stub
+    - net/nimble/transport/ram
+
+pkg.syscfg_vals.SELFTEST:
+    BLE_HS_DEBUG: 1
+    BLE_HS_PHONY_HCI_ACKS: 1
+    BLE_HS_REQUIRE_OS: 0
+    BLE_MAX_CONNECTIONS: 8
+    BLE_GATT_MAX_PROCS: 16
+    BLE_SM: 1
+    BLE_SM_SC: 1
+    MSYS_1_BLOCK_COUNT: 100

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_att_clt_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_att_clt_test.c b/net/nimble/host/test/src/ble_att_clt_test.c
new file mode 100644
index 0000000..ab89e9d
--- /dev/null
+++ b/net/nimble/host/test/src/ble_att_clt_test.c
@@ -0,0 +1,536 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+
+/**
+ * @return                      The handle of the new test connection.
+ */
+static uint16_t
+ble_att_clt_test_misc_init(void)
+{
+    ble_hs_test_util_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}), NULL,
+                                 NULL);
+    return 2;
+}
+
+static void
+ble_att_clt_test_misc_verify_tx_write(uint16_t handle_id, void *value,
+                                      int value_len, int is_req)
+{
+    struct ble_att_write_req req;
+    struct os_mbuf *om;
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    if (is_req) {
+        ble_att_write_req_parse(om->om_data, om->om_len, &req);
+    } else {
+        ble_att_write_cmd_parse(om->om_data, om->om_len, &req);
+    }
+
+    TEST_ASSERT(req.bawq_handle == handle_id);
+    TEST_ASSERT(om->om_len == BLE_ATT_WRITE_REQ_BASE_SZ + value_len);
+    TEST_ASSERT(memcmp(om->om_data + BLE_ATT_WRITE_REQ_BASE_SZ, value,
+                       value_len) == 0);
+}
+
+static void
+ble_att_clt_test_tx_write_req_or_cmd(uint16_t conn_handle,
+                                     struct ble_att_write_req *req,
+                                     void *value, int value_len, int is_req)
+{
+    struct os_mbuf *om;
+    int rc;
+
+    om = ble_hs_test_util_om_from_flat(value, value_len);
+    if (is_req) {
+        rc = ble_att_clt_tx_write_req(conn_handle, req, om);
+    } else {
+        rc = ble_att_clt_tx_write_cmd(conn_handle, req, om);
+    }
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ble_att_clt_test_tx_find_info)
+{
+    struct ble_att_find_info_req req;
+    uint16_t conn_handle;
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    /*** Success. */
+    req.bafq_start_handle = 1;
+    req.bafq_end_handle = 0xffff;
+    rc = ble_att_clt_tx_find_info(conn_handle, &req);
+    TEST_ASSERT(rc == 0);
+
+    /*** Error: start handle of 0. */
+    req.bafq_start_handle = 0;
+    req.bafq_end_handle = 0xffff;
+    rc = ble_att_clt_tx_find_info(conn_handle, &req);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+
+    /*** Error: start handle greater than end handle. */
+    req.bafq_start_handle = 500;
+    req.bafq_end_handle = 499;
+    rc = ble_att_clt_tx_find_info(conn_handle, &req);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+
+    /*** Success; start and end handles equal. */
+    req.bafq_start_handle = 500;
+    req.bafq_end_handle = 500;
+    rc = ble_att_clt_tx_find_info(conn_handle, &req);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ble_att_clt_test_rx_find_info)
+{
+    struct ble_att_find_info_rsp rsp;
+    uint16_t conn_handle;
+    uint8_t buf[1024];
+    uint8_t uuid128_1[16] = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
+    int off;
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    /*** One 128-bit UUID. */
+    /* Receive response with attribute mapping. */
+    off = 0;
+    rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT;
+    ble_att_find_info_rsp_write(buf + off, sizeof buf - off, &rsp);
+    off += BLE_ATT_FIND_INFO_RSP_BASE_SZ;
+
+    htole16(buf + off, 1);
+    off += 2;
+    memcpy(buf + off, uuid128_1, 16);
+    off += 16;
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    TEST_ASSERT(rc == 0);
+
+    /*** One 16-bit UUID. */
+    /* Receive response with attribute mapping. */
+    off = 0;
+    rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT;
+    ble_att_find_info_rsp_write(buf + off, sizeof buf - off, &rsp);
+    off += BLE_ATT_FIND_INFO_RSP_BASE_SZ;
+
+    htole16(buf + off, 2);
+    off += 2;
+    htole16(buf + off, 0x000f);
+    off += 2;
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    TEST_ASSERT(rc == 0);
+
+    /*** Two 16-bit UUIDs. */
+    /* Receive response with attribute mappings. */
+    off = 0;
+    rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT;
+    ble_att_find_info_rsp_write(buf + off, sizeof buf - off, &rsp);
+    off += BLE_ATT_FIND_INFO_RSP_BASE_SZ;
+
+    htole16(buf + off, 3);
+    off += 2;
+    htole16(buf + off, 0x0010);
+    off += 2;
+
+    htole16(buf + off, 4);
+    off += 2;
+    htole16(buf + off, 0x0011);
+    off += 2;
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+ble_att_clt_test_case_tx_write_req_or_cmd(int is_req)
+{
+    struct ble_att_write_req req;
+    uint16_t conn_handle;
+    uint8_t value300[500] = { 0 };
+    uint8_t value5[5] = { 6, 7, 54, 34, 8 };
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    /*** 5-byte write. */
+    req.bawq_handle = 0x1234;
+    ble_att_clt_test_tx_write_req_or_cmd(conn_handle, &req, value5,
+                                         sizeof value5, is_req);
+    ble_hs_test_util_tx_all();
+    ble_att_clt_test_misc_verify_tx_write(0x1234, value5, sizeof value5,
+                                          is_req);
+
+    /*** Overlong write; verify command truncated to ATT MTU. */
+    req.bawq_handle = 0xab83;
+    ble_att_clt_test_tx_write_req_or_cmd(conn_handle, &req, value300,
+                                         sizeof value300, is_req);
+    ble_hs_test_util_tx_all();
+    ble_att_clt_test_misc_verify_tx_write(0xab83, value300,
+                                          BLE_ATT_MTU_DFLT - 3, is_req);
+}
+
+static void
+ble_att_clt_test_misc_prep_good(uint16_t handle, uint16_t offset,
+                                uint8_t *attr_data, uint16_t attr_data_len)
+{
+    struct ble_att_prep_write_cmd req;
+    struct os_mbuf *om;
+    uint16_t conn_handle;
+    int rc;
+    int i;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    req.bapc_handle = handle;
+    req.bapc_offset = offset;
+    om = ble_hs_test_util_om_from_flat(attr_data, attr_data_len);
+    rc = ble_att_clt_tx_prep_write(conn_handle, &req, om);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_test_util_tx_all();
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+    TEST_ASSERT(om->om_len == BLE_ATT_PREP_WRITE_CMD_BASE_SZ + attr_data_len);
+
+    ble_att_prep_write_req_parse(om->om_data, om->om_len, &req);
+    TEST_ASSERT(req.bapc_handle == handle);
+    TEST_ASSERT(req.bapc_offset == offset);
+    for (i = 0; i < attr_data_len; i++) {
+        TEST_ASSERT(om->om_data[BLE_ATT_PREP_WRITE_CMD_BASE_SZ + i] ==
+                    attr_data[i]);
+    }
+}
+
+static void
+ble_att_clt_test_misc_exec_good(uint8_t flags)
+{
+    struct ble_att_exec_write_req req;
+    struct os_mbuf *om;
+    uint16_t conn_handle;
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    req.baeq_flags = flags;
+    rc = ble_att_clt_tx_exec_write(conn_handle, &req);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_test_util_tx_all();
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+    TEST_ASSERT(om->om_len == BLE_ATT_EXEC_WRITE_REQ_SZ);
+
+    ble_att_exec_write_req_parse(om->om_data, om->om_len, &req);
+    TEST_ASSERT(req.baeq_flags == flags);
+}
+
+static void
+ble_att_clt_test_misc_prep_bad(uint16_t handle, uint16_t offset,
+                               uint8_t *attr_data, uint16_t attr_data_len,
+                               int status)
+{
+    struct ble_att_prep_write_cmd req;
+    struct os_mbuf *om;
+    uint16_t conn_handle;
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    om = ble_hs_test_util_om_from_flat(attr_data, attr_data_len);
+
+    req.bapc_handle = handle;
+    req.bapc_offset = offset;
+    rc = ble_att_clt_tx_prep_write(conn_handle, &req, om);
+    TEST_ASSERT(rc == status);
+}
+
+TEST_CASE(ble_att_clt_test_tx_write)
+{
+    ble_att_clt_test_case_tx_write_req_or_cmd(0);
+    ble_att_clt_test_case_tx_write_req_or_cmd(1);
+}
+
+TEST_CASE(ble_att_clt_test_tx_read)
+{
+    struct ble_att_read_req req;
+    uint16_t conn_handle;
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    /*** Success. */
+    req.barq_handle = 1;
+    rc = ble_att_clt_tx_read(conn_handle, &req);
+    TEST_ASSERT(rc == 0);
+
+    /*** Error: handle of 0. */
+    req.barq_handle = 0;
+    rc = ble_att_clt_tx_read(conn_handle, &req);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+}
+
+TEST_CASE(ble_att_clt_test_rx_read)
+{
+    uint16_t conn_handle;
+    uint8_t buf[1024];
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    /*** Basic success. */
+    buf[0] = BLE_ATT_OP_READ_RSP;
+    buf[1] = 0;
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, 2);
+    TEST_ASSERT(rc == 0);
+
+    /*** Larger response. */
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, 20);
+    TEST_ASSERT(rc == 0);
+
+    /*** Zero-length response. */
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, 1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ble_att_clt_test_tx_read_blob)
+{
+    struct ble_att_read_blob_req req;
+    uint16_t conn_handle;
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    /*** Success. */
+    req.babq_handle = 1;
+    req.babq_offset = 0;
+    rc = ble_att_clt_tx_read_blob(conn_handle, &req);
+    TEST_ASSERT(rc == 0);
+
+    /*** Error: handle of 0. */
+    req.babq_handle = 0;
+    req.babq_offset = 0;
+    rc = ble_att_clt_tx_read_blob(conn_handle, &req);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+}
+
+TEST_CASE(ble_att_clt_test_rx_read_blob)
+{
+    uint16_t conn_handle;
+    uint8_t buf[1024];
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    /*** Basic success. */
+    buf[0] = BLE_ATT_OP_READ_BLOB_RSP;
+    buf[1] = 0;
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, 2);
+    TEST_ASSERT(rc == 0);
+
+    /*** Larger response. */
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, 20);
+    TEST_ASSERT(rc == 0);
+
+    /*** Zero-length response. */
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, 1);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ble_att_clt_test_tx_read_mult)
+{
+    struct os_mbuf *om;
+    uint16_t conn_handle;
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    /*** Success. */
+    rc = ble_att_clt_tx_read_mult(conn_handle, ((uint16_t[]){ 1, 2 }), 2);
+    TEST_ASSERT(rc == 0);
+
+    ble_hs_test_util_tx_all();
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+    TEST_ASSERT(om->om_len == BLE_ATT_READ_MULT_REQ_BASE_SZ + 4);
+
+    ble_att_read_mult_req_parse(om->om_data, om->om_len);
+    TEST_ASSERT(le16toh(om->om_data + BLE_ATT_READ_MULT_REQ_BASE_SZ) == 1);
+    TEST_ASSERT(le16toh(om->om_data + BLE_ATT_READ_MULT_REQ_BASE_SZ + 2) == 2);
+
+    /*** Error: no handles. */
+    rc = ble_att_clt_tx_read_mult(conn_handle, NULL, 0);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+}
+
+TEST_CASE(ble_att_clt_test_rx_read_mult)
+{
+    uint16_t conn_handle;
+    uint8_t buf[1024];
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    /*** Basic success. */
+    ble_att_read_mult_rsp_write(buf, sizeof buf);
+    htole16(buf + BLE_ATT_READ_MULT_RSP_BASE_SZ + 0, 12);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(
+        conn_handle, BLE_L2CAP_CID_ATT, buf,
+        BLE_ATT_READ_MULT_RSP_BASE_SZ + 2);
+    TEST_ASSERT(rc == 0);
+
+    /*** Larger response. */
+    htole16(buf + BLE_ATT_READ_MULT_RSP_BASE_SZ + 0, 12);
+    htole16(buf + BLE_ATT_READ_MULT_RSP_BASE_SZ + 2, 43);
+    htole16(buf + BLE_ATT_READ_MULT_RSP_BASE_SZ + 4, 91);
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(
+        conn_handle, BLE_L2CAP_CID_ATT, buf,
+        BLE_ATT_READ_MULT_RSP_BASE_SZ + 6);
+    TEST_ASSERT(rc == 0);
+
+    /*** Zero-length response. */
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(
+        conn_handle, BLE_L2CAP_CID_ATT, buf,
+        BLE_ATT_READ_MULT_RSP_BASE_SZ + 0);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ble_att_clt_test_tx_prep_write)
+{
+    uint8_t attr_data[512];
+    int i;
+
+    for (i = 0; i < sizeof attr_data; i++) {
+        attr_data[i] = i;
+    }
+
+    /*** Success. */
+    ble_att_clt_test_misc_prep_good(123, 0, attr_data, 16);
+    ble_att_clt_test_misc_prep_good(5432, 100, attr_data, 2);
+    ble_att_clt_test_misc_prep_good(0x1234, 400, attr_data, 0);
+    ble_att_clt_test_misc_prep_good(5432, 0, attr_data,
+                                    BLE_ATT_MTU_DFLT -
+                                        BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
+    ble_att_clt_test_misc_prep_good(0x1234, 507, attr_data, 5);
+
+    /*** Error: handle of 0. */
+    ble_att_clt_test_misc_prep_bad(0, 0, attr_data, 16, BLE_HS_EINVAL);
+
+    /*** Error: offset + length greater than maximum attribute size. */
+    ble_att_clt_test_misc_prep_bad(1, 507, attr_data, 6, BLE_HS_EINVAL);
+
+    /*** Error: packet larger than MTU. */
+    ble_att_clt_test_misc_prep_bad(1, 0, attr_data,
+                                   BLE_ATT_MTU_DFLT -
+                                       BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1,
+                                   BLE_HS_EINVAL);
+}
+
+TEST_CASE(ble_att_clt_test_rx_prep_write)
+{
+    struct ble_att_prep_write_cmd rsp;
+    uint16_t conn_handle;
+    uint8_t buf[1024];
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    /*** Basic success. */
+    rsp.bapc_handle = 0x1234;
+    rsp.bapc_offset = 0;
+    ble_att_prep_write_rsp_write(buf, sizeof buf, &rsp);
+    memset(buf + BLE_ATT_PREP_WRITE_CMD_BASE_SZ, 1, 5);
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(
+        conn_handle, BLE_L2CAP_CID_ATT, buf,
+        BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 5);
+    TEST_ASSERT(rc == 0);
+
+    /*** 0-length write. */
+    rsp.bapc_handle = 0x1234;
+    rsp.bapc_offset = 0;
+    ble_att_prep_write_rsp_write(buf, sizeof buf, &rsp);
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(
+        conn_handle, BLE_L2CAP_CID_ATT, buf, BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ble_att_clt_test_tx_exec_write)
+{
+    struct ble_att_exec_write_req req;
+    uint16_t conn_handle;
+    int rc;
+
+    conn_handle = ble_att_clt_test_misc_init();
+
+    /*** Success. */
+    ble_att_clt_test_misc_exec_good(0);
+    ble_att_clt_test_misc_exec_good(BLE_ATT_EXEC_WRITE_F_CONFIRM);
+
+    /*** Error: invalid flags value. */
+    req.baeq_flags = 0x02;
+    rc = ble_att_clt_tx_exec_write(conn_handle, &req);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+}
+
+TEST_SUITE(ble_att_clt_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_att_clt_test_tx_find_info();
+    ble_att_clt_test_rx_find_info();
+    ble_att_clt_test_tx_read();
+    ble_att_clt_test_rx_read();
+    ble_att_clt_test_tx_read_blob();
+    ble_att_clt_test_rx_read_blob();
+    ble_att_clt_test_tx_read_mult();
+    ble_att_clt_test_rx_read_mult();
+    ble_att_clt_test_tx_write();
+    ble_att_clt_test_tx_prep_write();
+    ble_att_clt_test_rx_prep_write();
+    ble_att_clt_test_tx_exec_write();
+}
+
+int
+ble_att_clt_test_all(void)
+{
+    ble_att_clt_suite();
+
+    return tu_any_failed;
+}


[12/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_gap_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gap_test.c b/net/nimble/host/test/src/ble_gap_test.c
new file mode 100644
index 0000000..16a7309
--- /dev/null
+++ b/net/nimble/host/test/src/ble_gap_test.c
@@ -0,0 +1,2580 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "nimble/hci_common.h"
+#include "host/ble_hs_adv.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+
+static int ble_gap_test_conn_event_type;
+static int ble_gap_test_conn_status;
+static struct ble_gap_conn_desc ble_gap_test_conn_desc;
+static void *ble_gap_test_conn_arg;
+static struct ble_gap_upd_params ble_gap_test_conn_peer_params;
+static struct ble_gap_upd_params ble_gap_test_conn_self_params;
+
+static int ble_gap_test_disc_event_type;
+static struct ble_gap_disc_desc ble_gap_test_disc_desc;
+static void *ble_gap_test_disc_arg;
+
+/*****************************************************************************
+ * $misc                                                                     *
+ *****************************************************************************/
+
+static int
+ble_gap_test_util_update_in_progress(uint16_t conn_handle)
+{
+    ble_hs_conn_flags_t conn_flags;
+    int rc;
+
+    rc = ble_hs_atomic_conn_flags(conn_handle, &conn_flags);
+    return rc == 0 && conn_flags & BLE_HS_CONN_F_UPDATE;
+}
+
+static void
+ble_gap_test_util_reset_cb_info(void)
+{
+    ble_gap_test_conn_event_type = -1;
+    ble_gap_test_conn_status = -1;
+    memset(&ble_gap_test_conn_desc, 0xff, sizeof ble_gap_test_conn_desc);
+    ble_gap_test_conn_arg = (void *)-1;
+
+    ble_gap_test_disc_event_type = -1;
+    memset(&ble_gap_test_disc_desc, 0xff, sizeof ble_gap_test_disc_desc);
+    ble_gap_test_disc_arg = (void *)-1;
+}
+
+static void
+ble_gap_test_util_init(void)
+{
+    ble_hs_test_util_init();
+    ble_hs_test_util_set_static_rnd_addr();
+    ble_gap_test_util_reset_cb_info();
+}
+
+static int
+ble_gap_test_util_disc_cb(struct ble_gap_event *event, void *arg)
+{
+    ble_gap_test_disc_event_type = event->type;
+    ble_gap_test_disc_arg = arg;
+
+    if (event->type == BLE_GAP_EVENT_DISC) {
+        ble_gap_test_disc_desc = event->disc;
+    }
+
+    return 0;
+}
+
+static int
+ble_gap_test_util_connect_cb(struct ble_gap_event *event, void *arg)
+{
+    int *fail_reason;
+
+    ble_gap_test_conn_event_type = event->type;
+    ble_gap_test_conn_arg = arg;
+
+    switch (event->type) {
+    case BLE_GAP_EVENT_CONNECT:
+        ble_gap_test_conn_status = event->connect.status;
+        ble_gap_conn_find(event->connect.conn_handle, &ble_gap_test_conn_desc);
+        break;
+
+    case BLE_GAP_EVENT_DISCONNECT:
+        ble_gap_test_conn_status = event->disconnect.reason;
+        ble_gap_test_conn_desc = event->disconnect.conn;
+        break;
+
+    case BLE_GAP_EVENT_CONN_UPDATE:
+        ble_gap_test_conn_status = event->conn_update.status;
+        ble_gap_conn_find(event->conn_update.conn_handle,
+                          &ble_gap_test_conn_desc);
+        break;
+
+    case BLE_GAP_EVENT_CONN_CANCEL:
+        break;
+
+    case BLE_GAP_EVENT_TERM_FAILURE:
+        ble_gap_test_conn_status = event->term_failure.status;
+        ble_gap_conn_find(event->term_failure.conn_handle,
+                          &ble_gap_test_conn_desc);
+        break;
+
+    case BLE_GAP_EVENT_ADV_COMPLETE:
+        ble_gap_test_conn_arg = arg;
+        break;
+
+    case BLE_GAP_EVENT_CONN_UPDATE_REQ:
+        ble_gap_test_conn_peer_params = *event->conn_update_req.peer_params;
+        *event->conn_update_req.self_params = ble_gap_test_conn_self_params;
+        ble_gap_conn_find(event->conn_update_req.conn_handle,
+                          &ble_gap_test_conn_desc);
+
+        fail_reason = arg;
+        if (fail_reason == NULL) {
+            return 0;
+        } else {
+            return *fail_reason;
+        }
+        break;
+
+    default:
+        TEST_ASSERT_FATAL(0);
+        break;
+    }
+
+    return 0;
+}
+
+static void
+ble_gap_test_util_verify_tx_clear_wl(void)
+{
+    uint8_t param_len;
+
+    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                   BLE_HCI_OCF_LE_CLEAR_WHITE_LIST,
+                                   &param_len);
+    TEST_ASSERT(param_len == 0);
+}
+
+static void
+ble_gap_test_util_verify_tx_add_wl(struct ble_gap_white_entry *entry)
+{
+    uint8_t param_len;
+    uint8_t *param;
+    int i;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_ADD_WHITE_LIST,
+                                           &param_len);
+    TEST_ASSERT(param_len == 7);
+    TEST_ASSERT(param[0] == entry->addr_type);
+    for (i = 0; i < 6; i++) {
+        TEST_ASSERT(param[1 + i] == entry->addr[i]);
+    }
+}
+
+static void
+ble_gap_test_util_verify_tx_set_scan_params(uint8_t own_addr_type,
+                                            uint8_t scan_type,
+                                            uint16_t itvl,
+                                            uint16_t scan_window,
+                                            uint8_t filter_policy)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_SET_SCAN_PARAMS,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_SET_SCAN_PARAM_LEN);
+    TEST_ASSERT(param[0] == scan_type);
+    TEST_ASSERT(le16toh(param + 1) == itvl);
+    TEST_ASSERT(le16toh(param + 3) == scan_window);
+    TEST_ASSERT(param[5] == own_addr_type);
+    TEST_ASSERT(param[6] == filter_policy);
+}
+
+static void
+ble_gap_test_util_verify_tx_scan_enable(uint8_t enable,
+                                        uint8_t filter_duplicates)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_SET_SCAN_ENABLE,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_SET_SCAN_ENABLE_LEN);
+    TEST_ASSERT(param[0] == enable);
+    TEST_ASSERT(param[1] == filter_duplicates);
+}
+
+static void
+ble_hs_test_util_verify_tx_create_conn_cancel(void)
+{
+    uint8_t param_len;
+
+    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                   BLE_HCI_OCF_LE_CREATE_CONN_CANCEL,
+                                   &param_len);
+    TEST_ASSERT(param_len == 0);
+}
+
+static void
+ble_gap_test_util_verify_tx_disconnect(void)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LINK_CTRL,
+                                           BLE_HCI_OCF_DISCONNECT_CMD,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_DISCONNECT_CMD_LEN);
+    TEST_ASSERT(le16toh(param + 0) == 2);
+    TEST_ASSERT(param[2] == BLE_ERR_REM_USER_CONN_TERM);
+}
+
+static void
+ble_gap_test_util_verify_tx_adv_params(void)
+{
+    uint8_t param_len;
+
+    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                   BLE_HCI_OCF_LE_SET_ADV_PARAMS,
+                                   &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_SET_ADV_PARAM_LEN);
+
+    /* Note: Content of message verified in ble_hs_adv_test.c. */
+}
+
+static void
+ble_gap_test_util_verify_tx_adv_data(void)
+{
+    uint8_t param_len;
+
+    ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                   BLE_HCI_OCF_LE_SET_ADV_DATA,
+                                   &param_len);
+    /* Note: Content of message verified in ble_hs_adv_test.c. */
+}
+
+static void
+ble_gap_test_util_verify_tx_rsp_data(void)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA,
+                                           &param_len);
+    (void)param; /* XXX: Verify other fields. */
+}
+
+static void
+ble_gap_test_util_verify_tx_adv_enable(int enabled)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_SET_ADV_ENABLE,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_SET_ADV_ENABLE_LEN);
+    TEST_ASSERT(param[0] == !!enabled);
+}
+
+static void
+ble_gap_test_util_verify_tx_update_conn(struct ble_gap_upd_params *params)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_CONN_UPDATE,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_CONN_UPDATE_LEN);
+    TEST_ASSERT(le16toh(param + 0) == 2);
+    TEST_ASSERT(le16toh(param + 2) == params->itvl_min);
+    TEST_ASSERT(le16toh(param + 4) == params->itvl_max);
+    TEST_ASSERT(le16toh(param + 6) == params->latency);
+    TEST_ASSERT(le16toh(param + 8) == params->supervision_timeout);
+    TEST_ASSERT(le16toh(param + 10) == params->min_ce_len);
+    TEST_ASSERT(le16toh(param + 12) == params->max_ce_len);
+}
+
+static void
+ble_gap_test_util_verify_tx_params_reply_pos(void)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_REM_CONN_PARAM_RR,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_CONN_PARAM_REPLY_LEN);
+    TEST_ASSERT(le16toh(param + 0) == 2);
+    TEST_ASSERT(le16toh(param + 2) == ble_gap_test_conn_self_params.itvl_min);
+    TEST_ASSERT(le16toh(param + 4) == ble_gap_test_conn_self_params.itvl_max);
+    TEST_ASSERT(le16toh(param + 6) == ble_gap_test_conn_self_params.latency);
+    TEST_ASSERT(le16toh(param + 8) ==
+                ble_gap_test_conn_self_params.supervision_timeout);
+    TEST_ASSERT(le16toh(param + 10) ==
+                ble_gap_test_conn_self_params.min_ce_len);
+    TEST_ASSERT(le16toh(param + 12) ==
+                ble_gap_test_conn_self_params.max_ce_len);
+}
+
+static void
+ble_gap_test_util_verify_tx_params_reply_neg(uint8_t reason)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_REM_CONN_PARAM_NRR,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_CONN_PARAM_NEG_REPLY_LEN);
+    TEST_ASSERT(le16toh(param + 0) == 2);
+    TEST_ASSERT(param[2] == reason);
+}
+
+static void
+ble_gap_test_util_rx_update_complete(
+    uint8_t status,
+    struct ble_gap_upd_params *params)
+{
+    struct hci_le_conn_upd_complete evt;
+
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_UPD_COMPLETE;
+    evt.status = status;
+    evt.connection_handle = 2;
+    evt.conn_itvl = params->itvl_max;
+    evt.conn_latency = params->latency;
+    evt.supervision_timeout = params->supervision_timeout;
+
+    ble_gap_rx_update_complete(&evt);
+}
+
+static int
+ble_gap_test_util_rx_param_req(struct ble_gap_upd_params *params, int pos,
+                               int *cmd_idx, int cmd_fail_idx,
+                               uint8_t fail_status)
+{
+    struct hci_le_conn_param_req evt;
+    uint16_t opcode;
+    uint8_t hci_status;
+
+    evt.subevent_code = BLE_HCI_LE_SUBEV_REM_CONN_PARM_REQ;
+    evt.connection_handle = 2;
+    evt.itvl_min = params->itvl_min;
+    evt.itvl_max = params->itvl_max;
+    evt.latency = params->latency;
+    evt.timeout = params->supervision_timeout;
+
+    if (pos) {
+        opcode = ble_hs_hci_util_opcode_join(
+            BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_REM_CONN_PARAM_RR);
+    } else {
+        opcode = ble_hs_hci_util_opcode_join(
+            BLE_HCI_OGF_LE, BLE_HCI_OCF_LE_REM_CONN_PARAM_NRR);
+    }
+    if (*cmd_idx == cmd_fail_idx) {
+        hci_status = fail_status;
+    } else {
+        hci_status = 0;
+    }
+    (*cmd_idx)++;
+
+    ble_hs_test_util_set_ack(opcode, hci_status);
+    ble_gap_rx_param_req(&evt);
+
+    return hci_status;
+}
+
+/*****************************************************************************
+ * $white list                                                               *
+ *****************************************************************************/
+
+static void
+ble_gap_test_util_wl_set(struct ble_gap_white_entry *white_list,
+                         int white_list_count, int cmd_fail_idx,
+                         uint8_t fail_status)
+{
+    int cmd_idx;
+    int rc;
+    int i;
+
+    ble_gap_test_util_init();
+    cmd_idx = 0;
+
+    rc = ble_hs_test_util_wl_set(white_list, white_list_count, cmd_fail_idx,
+                                 fail_status);
+    TEST_ASSERT(rc == BLE_HS_HCI_ERR(fail_status));
+
+    /* Verify tx of clear white list command. */
+    ble_gap_test_util_verify_tx_clear_wl();
+    if (cmd_idx >= cmd_fail_idx) {
+        return;
+    }
+    cmd_idx++;
+
+    /* Verify tx of add white list commands. */
+    for (i = 0; i < white_list_count; i++) {
+        ble_gap_test_util_verify_tx_add_wl(white_list + i);
+        if (cmd_idx >= cmd_fail_idx) {
+            return;
+        }
+        cmd_idx++;
+    }
+}
+
+TEST_CASE(ble_gap_test_case_wl_bad_args)
+{
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /*** 0 white list entries. */
+    rc = ble_hs_test_util_wl_set(NULL, 0, 0, 0);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+
+    /*** Invalid address type. */
+    rc = ble_hs_test_util_wl_set(
+        ((struct ble_gap_white_entry[]) { {
+            5, { 1, 2, 3, 4, 5, 6 }
+        }, }),
+        1, 0, 0);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+
+    /*** White-list-using connection in progress. */
+    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
+                                  BLE_GAP_ADDR_TYPE_WL, NULL, 0, NULL,
+                                  ble_gap_test_util_connect_cb, NULL, 0);
+    TEST_ASSERT(rc == 0);
+
+    rc = ble_hs_test_util_wl_set(
+        ((struct ble_gap_white_entry[]) { {
+            BLE_ADDR_TYPE_PUBLIC, { 1, 2, 3, 4, 5, 6 }
+        }, }),
+        1, 0, 0);
+    TEST_ASSERT(rc == BLE_HS_EBUSY);
+}
+
+TEST_CASE(ble_gap_test_case_wl_ctlr_fail)
+{
+    int i;
+
+    struct ble_gap_white_entry white_list[] = {
+        { BLE_ADDR_TYPE_PUBLIC, { 1, 2, 3, 4, 5, 6 } },
+        { BLE_ADDR_TYPE_PUBLIC, { 2, 3, 4, 5, 6, 7 } },
+        { BLE_ADDR_TYPE_PUBLIC, { 3, 4, 5, 6, 7, 8 } },
+        { BLE_ADDR_TYPE_PUBLIC, { 4, 5, 6, 7, 8, 9 } },
+    };
+    int white_list_count = sizeof white_list / sizeof white_list[0];
+
+    for (i = 0; i < 5; i++) {
+        ble_gap_test_util_wl_set(white_list, white_list_count, i,
+                                 BLE_ERR_UNSPECIFIED);
+    }
+}
+
+TEST_CASE(ble_gap_test_case_wl_good)
+{
+    struct ble_gap_white_entry white_list[] = {
+        { BLE_ADDR_TYPE_PUBLIC, { 1, 2, 3, 4, 5, 6 } },
+        { BLE_ADDR_TYPE_PUBLIC, { 2, 3, 4, 5, 6, 7 } },
+        { BLE_ADDR_TYPE_PUBLIC, { 3, 4, 5, 6, 7, 8 } },
+        { BLE_ADDR_TYPE_PUBLIC, { 4, 5, 6, 7, 8, 9 } },
+    };
+    int white_list_count = sizeof white_list / sizeof white_list[0];
+
+    ble_gap_test_util_wl_set(white_list, white_list_count, 0, 0);
+}
+
+TEST_SUITE(ble_gap_test_suite_wl)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_wl_good();
+    ble_gap_test_case_wl_bad_args();
+    ble_gap_test_case_wl_ctlr_fail();
+}
+
+/*****************************************************************************
+ * $discovery                                                                *
+ *****************************************************************************/
+
+static int
+ble_gap_test_util_disc(uint8_t own_addr_type,
+                       const struct ble_gap_disc_params *disc_params,
+                       struct ble_gap_disc_desc *desc, int cmd_fail_idx,
+                       uint8_t fail_status)
+{
+    int rc;
+
+    ble_gap_test_util_init();
+
+    TEST_ASSERT(!ble_gap_disc_active());
+
+    /* Begin the discovery procedure. */
+    rc = ble_hs_test_util_disc(own_addr_type, BLE_HS_FOREVER, disc_params,
+                               ble_gap_test_util_disc_cb, NULL, cmd_fail_idx,
+                               fail_status);
+    TEST_ASSERT(rc == BLE_HS_HCI_ERR(fail_status));
+    if (rc == 0) {
+        TEST_ASSERT(ble_gap_master_in_progress());
+        ble_gap_rx_adv_report(desc);
+    } else {
+        TEST_ASSERT(ble_gap_test_disc_event_type == -1);
+    }
+
+    if (cmd_fail_idx > 0) {
+        /* Verify tx of set scan parameters command. */
+        ble_gap_test_util_verify_tx_set_scan_params(
+            own_addr_type,
+            disc_params->passive ?
+                BLE_HCI_SCAN_TYPE_PASSIVE :
+                BLE_HCI_SCAN_TYPE_ACTIVE,
+            disc_params->itvl,
+            disc_params->window,
+            disc_params->filter_policy);
+    }
+
+    if (cmd_fail_idx > 1) {
+        /* Verify tx of scan enable command. */
+        ble_gap_test_util_verify_tx_scan_enable(
+            1, disc_params->filter_duplicates);
+    }
+
+    if (rc == 0) {
+        TEST_ASSERT(ble_gap_disc_active());
+    }
+
+    return rc;
+}
+
+TEST_CASE(ble_gap_test_case_disc_bad_args)
+{
+    struct ble_gap_disc_params params;
+    int rc;
+
+    params.itvl = 0;
+    params.window = 0;
+    params.filter_policy = BLE_HCI_SCAN_FILT_NO_WL;
+    params.limited = 0;
+    params.passive = 0;
+    params.filter_duplicates = 0;
+
+    ble_gap_test_util_init();
+
+    /*** Invalid filter policy. */
+    params.filter_policy = 6;
+    rc = ble_gap_disc(BLE_ADDR_TYPE_PUBLIC, 0, &params,
+                      ble_gap_test_util_disc_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+}
+
+TEST_CASE(ble_gap_test_case_disc_good)
+{
+    uint8_t adv_data[32];
+    uint8_t flags;
+    uint8_t own_addr_type;
+    int passive;
+    int limited;
+    int rc;
+
+    struct ble_gap_disc_desc desc = {
+        .event_type = BLE_HCI_ADV_TYPE_ADV_IND,
+        .addr_type = BLE_ADDR_TYPE_PUBLIC,
+        .length_data = 0,
+        .rssi = 0,
+        .addr = { 1, 2, 3, 4, 5, 6 },
+        .data = adv_data,
+    };
+    struct ble_gap_disc_params disc_params = {
+        .itvl = BLE_GAP_SCAN_SLOW_INTERVAL1,
+        .window = BLE_GAP_SCAN_SLOW_WINDOW1,
+        .filter_policy = BLE_HCI_CONN_FILT_NO_WL,
+        .limited = 0,
+        .passive = 0,
+        .filter_duplicates = 0,
+    };
+
+    flags = BLE_HS_ADV_F_DISC_LTD;
+    rc = ble_hs_adv_set_flat(BLE_HS_ADV_TYPE_FLAGS, 1, &flags,
+                             desc.data, &desc.length_data,
+                             sizeof adv_data);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    for (own_addr_type = 0;
+         own_addr_type <= BLE_ADDR_TYPE_RPA_RND_DEFAULT;
+         own_addr_type++)
+    for (passive = 0; passive <= 1; passive++)
+    for (limited = 0; limited <= 1; limited++) {
+        disc_params.passive = passive;
+        disc_params.limited = limited;
+        ble_gap_test_util_disc(own_addr_type, &disc_params, &desc, -1, 0);
+
+        TEST_ASSERT(ble_gap_master_in_progress());
+        TEST_ASSERT(ble_gap_test_disc_event_type == BLE_GAP_EVENT_DISC);
+        TEST_ASSERT(ble_gap_test_disc_desc.event_type ==
+                    BLE_HCI_ADV_TYPE_ADV_IND);
+        TEST_ASSERT(ble_gap_test_disc_desc.addr_type ==
+                    BLE_ADDR_TYPE_PUBLIC);
+        TEST_ASSERT(ble_gap_test_disc_desc.length_data == 3);
+        TEST_ASSERT(ble_gap_test_disc_desc.rssi == 0);
+        TEST_ASSERT(memcmp(ble_gap_test_disc_desc.addr, desc.addr, 6) == 0);
+        TEST_ASSERT(ble_gap_test_disc_arg == NULL);
+
+    }
+}
+
+TEST_CASE(ble_gap_test_case_disc_ltd_mismatch)
+{
+    int rc;
+    struct ble_gap_disc_desc desc = {
+        .event_type = BLE_HCI_ADV_TYPE_ADV_IND,
+        .addr_type = BLE_ADDR_TYPE_PUBLIC,
+        .length_data = 0,
+        .rssi = 0,
+        .addr = { 1, 2, 3, 4, 5, 6 },
+        .data = (uint8_t[BLE_HCI_MAX_ADV_DATA_LEN]){
+            2, 
+            BLE_HS_ADV_TYPE_FLAGS,
+            BLE_HS_ADV_F_DISC_GEN,
+        },
+    };
+    struct ble_gap_disc_params disc_params = {
+        .itvl = BLE_GAP_SCAN_SLOW_INTERVAL1,
+        .window = BLE_GAP_SCAN_SLOW_WINDOW1,
+        .filter_policy = BLE_HCI_CONN_FILT_NO_WL,
+        .limited = 1,
+        .passive = 0,
+        .filter_duplicates = 0,
+    };
+
+    rc = ble_gap_test_util_disc(BLE_ADDR_TYPE_PUBLIC, &disc_params, &desc,
+                                -1, 0);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(ble_gap_master_in_progress());
+
+    /* Verify that the report was ignored because of a mismatched LTD flag. */
+    TEST_ASSERT(ble_gap_test_disc_event_type == -1);
+
+    /* Stop the scan and swap the flags. */
+    rc = ble_hs_test_util_disc_cancel(0);
+    TEST_ASSERT(rc == 0);
+
+    desc.data[2] = BLE_HS_ADV_F_DISC_LTD;
+    disc_params.limited = 0;
+    rc = ble_gap_test_util_disc(BLE_ADDR_TYPE_PUBLIC, &disc_params, &desc,
+                                -1, 0);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(ble_gap_master_in_progress());
+
+    /* This time we should have reported the advertisement; general discovery
+     * hears everything.
+     */
+    TEST_ASSERT(ble_gap_test_disc_event_type == BLE_GAP_EVENT_DISC);
+
+}
+
+TEST_CASE(ble_gap_test_case_disc_hci_fail)
+{
+    int fail_idx;
+    int limited;
+    int rc;
+
+    struct ble_gap_disc_desc desc = {
+        .event_type = BLE_HCI_ADV_TYPE_ADV_IND,
+        .addr_type = BLE_ADDR_TYPE_PUBLIC,
+        .length_data = 0,
+        .rssi = 0,
+        .addr = { 1, 2, 3, 4, 5, 6 },
+        .data = NULL,
+    };
+    struct ble_gap_disc_params disc_params = {
+        .itvl = BLE_GAP_SCAN_SLOW_INTERVAL1,
+        .window = BLE_GAP_SCAN_SLOW_WINDOW1,
+        .filter_policy = BLE_HCI_CONN_FILT_NO_WL,
+        .limited = 0,
+        .passive = 0,
+        .filter_duplicates = 0,
+    };
+
+    for (limited = 0; limited <= 1; limited++) {
+        disc_params.limited = limited;
+
+        for (fail_idx = 0; fail_idx < 2; fail_idx++) {
+            rc = ble_gap_test_util_disc(BLE_ADDR_TYPE_PUBLIC, &disc_params,
+                                        &desc, fail_idx, BLE_ERR_UNSUPPORTED);
+            TEST_ASSERT(rc == BLE_HS_HCI_ERR(BLE_ERR_UNSUPPORTED));
+            TEST_ASSERT(!ble_gap_master_in_progress());
+        }
+    }
+}
+
+static void
+ble_gap_test_util_disc_dflts_once(int limited)
+{
+    struct ble_gap_disc_params params;
+    uint16_t exp_window;
+    uint16_t exp_itvl;
+    int rc;
+
+    ble_gap_test_util_init();
+
+    memset(&params, 0, sizeof params);
+    params.limited = limited;
+
+    rc = ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC, 0, &params,
+                               ble_gap_test_util_disc_cb, NULL, -1, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    if (limited) {
+        exp_itvl = BLE_GAP_LIM_DISC_SCAN_INT;
+        exp_window = BLE_GAP_LIM_DISC_SCAN_WINDOW;
+    } else {
+        exp_itvl = BLE_GAP_SCAN_FAST_INTERVAL_MIN;
+        exp_window = BLE_GAP_SCAN_FAST_WINDOW;
+    }
+    ble_gap_test_util_verify_tx_set_scan_params(
+        BLE_ADDR_TYPE_PUBLIC,
+        BLE_HCI_SCAN_TYPE_ACTIVE,
+        exp_itvl,
+        exp_window,
+        BLE_HCI_SCAN_FILT_NO_WL);
+
+    ble_gap_test_util_verify_tx_scan_enable(1, 0);
+}
+
+TEST_CASE(ble_gap_test_case_disc_dflts)
+{
+    ble_gap_test_util_disc_dflts_once(0);
+    ble_gap_test_util_disc_dflts_once(1);
+}
+
+TEST_CASE(ble_gap_test_case_disc_already)
+{
+    static const struct ble_gap_disc_params disc_params = { 0 };
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Start a discovery procedure. */
+    rc = ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER,
+                               &disc_params, ble_gap_test_util_disc_cb,
+                               NULL, -1, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure host indicates BLE_HS_EALREADY if we try to discover. */
+    rc = ble_gap_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER, &disc_params,
+                               ble_gap_test_util_disc_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EALREADY);
+}
+
+TEST_CASE(ble_gap_test_case_disc_busy)
+{
+    static const struct ble_gap_disc_params disc_params = { 0 };
+    static const uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Start a connect procedure. */
+    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                  peer_addr, 0, NULL,
+                                  ble_gap_test_util_connect_cb, NULL, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure host indicates BLE_HS_EBUSY if we try to discover. */
+    rc = ble_gap_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER, &disc_params,
+                               ble_gap_test_util_disc_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EBUSY);
+}
+
+TEST_SUITE(ble_gap_test_suite_disc)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_disc_bad_args();
+    ble_gap_test_case_disc_good();
+    ble_gap_test_case_disc_ltd_mismatch();
+    ble_gap_test_case_disc_hci_fail();
+    ble_gap_test_case_disc_dflts();
+    ble_gap_test_case_disc_already();
+    ble_gap_test_case_disc_busy();
+}
+
+/*****************************************************************************
+ * $direct connect                                                           *
+ *****************************************************************************/
+
+TEST_CASE(ble_gap_test_case_conn_gen_good)
+{
+    struct hci_le_conn_complete evt;
+    struct ble_gap_conn_params params;
+    int rc;
+
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    ble_gap_test_util_init();
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_gap_conn_active());
+
+    params.scan_itvl = 0x12;
+    params.scan_window = 0x11;
+    params.itvl_min = 25;
+    params.itvl_max = 26;
+    params.latency = 1;
+    params.supervision_timeout = 20;
+    params.min_ce_len = 3;
+    params.max_ce_len = 4;
+
+    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
+                                  BLE_ADDR_TYPE_PUBLIC, peer_addr, 0, &params,
+                                  ble_gap_test_util_connect_cb, NULL, 0);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(ble_gap_master_in_progress());
+    TEST_ASSERT(ble_gap_conn_active());
+
+    TEST_ASSERT(ble_gap_master_in_progress());
+    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == BLE_HS_ENOTCONN);
+
+    /* Receive connection complete event. */
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_SUCCESS;
+    evt.connection_handle = 2;
+    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
+    memcpy(evt.peer_addr, peer_addr, 6);
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONNECT);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+
+    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == 0);
+}
+
+TEST_CASE(ble_gap_test_case_conn_gen_bad_args)
+{
+    int rc;
+
+    ble_gap_test_util_init();
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    /*** Invalid address type. */
+    rc = ble_gap_connect(BLE_ADDR_TYPE_PUBLIC, 5,
+                         ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), 0, NULL,
+                         ble_gap_test_util_connect_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    /*** Connection already in progress. */
+    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
+                                  BLE_ADDR_TYPE_PUBLIC,
+                                  ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), 0,
+                                  NULL, ble_gap_test_util_connect_cb,
+                                  NULL, 0);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(ble_gap_master_in_progress());
+
+    rc = ble_gap_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                         ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), 0, NULL,
+                         ble_gap_test_util_connect_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EALREADY);
+}
+
+TEST_CASE(ble_gap_test_case_conn_gen_dflt_params)
+{
+    static const uint8_t peer_addr[6] = { 2, 3, 8, 6, 6, 1 };
+    int rc;
+
+    ble_gap_test_util_init();
+
+    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
+                                  BLE_ADDR_TYPE_PUBLIC, peer_addr, 0, NULL,
+                                  ble_gap_test_util_connect_cb, NULL, 0);
+    TEST_ASSERT(rc == 0);
+}
+
+TEST_CASE(ble_gap_test_case_conn_gen_already)
+{
+    static const struct ble_gap_conn_params conn_params = { 0 };
+    static const uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Start a connect procedure. */
+    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                  peer_addr, 0, NULL,
+                                  ble_gap_test_util_connect_cb, NULL, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure host indicates BLE_HS_EALREADY if we try to connect. */
+    rc = ble_gap_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                         peer_addr, BLE_HS_FOREVER, &conn_params,
+                         ble_gap_test_util_connect_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EALREADY);
+}
+
+TEST_CASE(ble_gap_test_case_conn_gen_busy)
+{
+    static const struct ble_gap_disc_params disc_params = { 0 };
+    static const struct ble_gap_conn_params conn_params = { 0 };
+    static const uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Start a discovery procedure. */
+    rc = ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC, BLE_HS_FOREVER,
+                               &disc_params, ble_gap_test_util_disc_cb,
+                               NULL, -1, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure host indicates BLE_HS_EBUSY if we try to connect. */
+    rc = ble_gap_connect(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                         peer_addr, BLE_HS_FOREVER, &conn_params,
+                         ble_gap_test_util_connect_cb, NULL);
+    TEST_ASSERT(rc == BLE_HS_EBUSY);
+}
+
+TEST_SUITE(ble_gap_test_suite_conn_gen)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_conn_gen_good();
+    ble_gap_test_case_conn_gen_bad_args();
+    ble_gap_test_case_conn_gen_dflt_params();
+    ble_gap_test_case_conn_gen_already();
+    ble_gap_test_case_conn_gen_busy();
+}
+
+/*****************************************************************************
+ * $cancel                                                                   *
+ *****************************************************************************/
+
+static void
+ble_gap_test_util_conn_cancel(uint8_t hci_status)
+{
+    struct hci_le_conn_complete evt;
+    int rc;
+
+    /* Initiate cancel procedure. */
+    rc = ble_hs_test_util_conn_cancel(hci_status);
+    TEST_ASSERT(rc == BLE_HS_HCI_ERR(hci_status));
+
+    /* Verify tx of cancel create connection command. */
+    ble_hs_test_util_verify_tx_create_conn_cancel();
+    if (rc != 0) {
+        return;
+    }
+    TEST_ASSERT(ble_gap_master_in_progress());
+
+    /* Receive connection complete event. */
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_UNK_CONN_ID;
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_CANCEL);
+}
+
+static void
+ble_gap_test_util_conn_and_cancel(uint8_t *peer_addr, uint8_t hci_status)
+{
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Begin creating a connection. */
+    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
+                                  BLE_ADDR_TYPE_PUBLIC, peer_addr, 0, NULL,
+                                  ble_gap_test_util_connect_cb, NULL, 0);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(ble_gap_master_in_progress());
+
+    /* Initiate cancel procedure. */
+    ble_gap_test_util_conn_cancel(hci_status);
+    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == BLE_HS_ENOTCONN);
+}
+
+TEST_CASE(ble_gap_test_case_conn_cancel_bad_args)
+{
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Initiate cancel procedure with no connection in progress. */
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    rc = ble_hs_test_util_conn_cancel(0);
+    TEST_ASSERT(rc == BLE_HS_EALREADY);
+}
+
+TEST_CASE(ble_gap_test_case_conn_cancel_good)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    ble_gap_test_util_conn_and_cancel(peer_addr, 0);
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_CANCEL);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == BLE_HS_CONN_HANDLE_NONE);
+}
+
+TEST_CASE(ble_gap_test_case_conn_cancel_ctlr_fail)
+{
+    struct hci_le_conn_complete evt;
+    int rc;
+
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    ble_gap_test_util_conn_and_cancel(peer_addr, BLE_ERR_REPEATED_ATTEMPTS);
+
+    /* Make sure the host didn't invoke the application callback.  The cancel
+     * failure was indicated via the return code from the gap call.
+     */
+    TEST_ASSERT(ble_gap_test_conn_event_type == -1);
+
+    /* Allow connection complete to succeed. */
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_SUCCESS;
+    evt.connection_handle = 2;
+    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
+    memcpy(evt.peer_addr, peer_addr, 6);
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONNECT);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+
+    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == 0);
+}
+
+TEST_SUITE(ble_gap_test_suite_conn_cancel)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_conn_cancel_good();
+    ble_gap_test_case_conn_cancel_bad_args();
+    ble_gap_test_case_conn_cancel_ctlr_fail();
+}
+
+/*****************************************************************************
+ * $terminate                                                                *
+ *****************************************************************************/
+
+static void
+ble_gap_test_util_terminate(uint8_t *peer_addr, uint8_t hci_status)
+{
+    struct hci_disconn_complete evt;
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Create a connection. */
+    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
+                                 NULL);
+
+    /* Reset the callback event code; we don't care about the successful
+     * connection in this test.
+     */
+    ble_gap_test_conn_event_type = -1;
+
+    /* Terminate the connection. */
+    rc = ble_hs_test_util_conn_terminate(2, hci_status);
+    TEST_ASSERT(rc == BLE_HS_HCI_ERR(hci_status));
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    /* Verify tx of disconnect command. */
+    ble_gap_test_util_verify_tx_disconnect();
+
+    if (hci_status == 0) {
+        /* Receive disconnection complete event. */
+        evt.connection_handle = 2;
+        evt.status = 0;
+        evt.reason = BLE_ERR_CONN_TERM_LOCAL;
+        ble_gap_rx_disconn_complete(&evt);
+    }
+}
+
+TEST_CASE(ble_gap_test_case_conn_terminate_bad_args)
+{
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /*** Nonexistent connection. */
+    rc = ble_hs_test_util_conn_terminate(2, 0);
+    TEST_ASSERT(rc == BLE_HS_ENOTCONN);
+}
+
+TEST_CASE(ble_gap_test_case_conn_terminate_good)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    ble_gap_test_util_terminate(peer_addr, 0);
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_DISCONNECT);
+    TEST_ASSERT(ble_gap_test_conn_status ==
+                BLE_HS_HCI_ERR(BLE_ERR_CONN_TERM_LOCAL));
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(ble_gap_test_conn_desc.peer_id_addr_type ==
+                BLE_ADDR_TYPE_PUBLIC);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+    TEST_ASSERT(ble_gap_test_conn_arg == NULL);
+
+    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == BLE_HS_ENOTCONN);
+    TEST_ASSERT(!ble_gap_master_in_progress());
+}
+
+TEST_CASE(ble_gap_test_case_conn_terminate_ctlr_fail)
+{
+    struct hci_disconn_complete evt;
+    int rc;
+
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    ble_gap_test_util_init();
+
+    /* Create a connection. */
+    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
+                                 NULL);
+
+    /* Terminate the connection. */
+    rc = ble_hs_test_util_conn_terminate(2, 0);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    /* Verify tx of disconnect command. */
+    ble_gap_test_util_verify_tx_disconnect();
+
+    /* Receive failed disconnection complete event. */
+    evt.connection_handle = 2;
+    evt.status = BLE_ERR_UNSUPPORTED;
+    evt.reason = 0;
+    ble_gap_rx_disconn_complete(&evt);
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_TERM_FAILURE);
+    TEST_ASSERT(ble_gap_test_conn_status ==
+                BLE_HS_HCI_ERR(BLE_ERR_UNSUPPORTED));
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(ble_gap_test_conn_desc.peer_id_addr_type ==
+                BLE_ADDR_TYPE_PUBLIC);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+    TEST_ASSERT(ble_gap_test_conn_arg == NULL);
+
+    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == 0);
+    TEST_ASSERT(!ble_gap_master_in_progress());
+}
+
+TEST_CASE(ble_gap_test_case_conn_terminate_hci_fail)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    ble_gap_test_util_terminate(peer_addr, BLE_ERR_REPEATED_ATTEMPTS);
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == -1);
+    TEST_ASSERT(ble_hs_atomic_conn_flags(2, NULL) == 0);
+    TEST_ASSERT(!ble_gap_master_in_progress());
+}
+
+TEST_SUITE(ble_gap_test_suite_conn_terminate)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_conn_terminate_bad_args();
+    ble_gap_test_case_conn_terminate_good();
+    ble_gap_test_case_conn_terminate_ctlr_fail();
+    ble_gap_test_case_conn_terminate_hci_fail();
+}
+
+/*****************************************************************************
+ * $conn find                                                                *
+ *****************************************************************************/
+
+TEST_CASE(ble_gap_test_case_conn_find)
+{
+
+    struct ble_gap_conn_desc desc;
+    struct ble_hs_conn *conn;
+    uint8_t pub_addr[6];
+    int rc;
+
+    /*** We are master; public addresses. */
+    ble_gap_test_util_init();
+
+    ble_hs_test_util_create_rpa_conn(8,
+                                     BLE_ADDR_TYPE_PUBLIC,
+                                     ((uint8_t[6]){0,0,0,0,0,0}),
+                                     BLE_ADDR_TYPE_PUBLIC,
+                                     ((uint8_t[6]){2,3,4,5,6,7}),
+                                     ((uint8_t[6]){0,0,0,0,0,0}),
+                                     ble_gap_test_util_connect_cb,
+                                     NULL);
+
+
+    rc = ble_hs_id_copy_addr(BLE_ADDR_TYPE_PUBLIC, pub_addr, NULL);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    rc = ble_gap_conn_find(8, &desc);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(desc.conn_handle == 8);
+    TEST_ASSERT(desc.our_id_addr_type == BLE_ADDR_TYPE_PUBLIC);
+    TEST_ASSERT(desc.our_ota_addr_type == BLE_ADDR_TYPE_PUBLIC);
+    TEST_ASSERT(desc.peer_ota_addr_type == BLE_ADDR_TYPE_PUBLIC);
+    TEST_ASSERT(desc.role == BLE_GAP_ROLE_MASTER);
+    TEST_ASSERT(memcmp(desc.our_ota_addr, pub_addr, 6) == 0);
+    TEST_ASSERT(memcmp(desc.our_id_addr, pub_addr, 6) == 0);
+    TEST_ASSERT(memcmp(desc.peer_ota_addr,
+                       ((uint8_t[6]){2,3,4,5,6,7}), 6) == 0);
+    TEST_ASSERT(memcmp(desc.peer_id_addr,
+                       ((uint8_t[6]){2,3,4,5,6,7}), 6) == 0);
+    TEST_ASSERT(desc.conn_itvl == BLE_GAP_INITIAL_CONN_ITVL_MAX);
+    TEST_ASSERT(desc.conn_latency == BLE_GAP_INITIAL_CONN_LATENCY);
+    TEST_ASSERT(desc.supervision_timeout ==
+                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
+    TEST_ASSERT(desc.master_clock_accuracy == 0);
+    TEST_ASSERT(!desc.sec_state.encrypted);
+    TEST_ASSERT(!desc.sec_state.authenticated);
+    TEST_ASSERT(!desc.sec_state.bonded);
+
+    /*** Swap roles. */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(8);
+    conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
+    ble_hs_unlock();
+
+    rc = ble_gap_conn_find(8, &desc);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(desc.role == BLE_GAP_ROLE_SLAVE);
+
+    /*** We are master; RPAs. */
+    ble_gap_test_util_init();
+
+    ble_hs_test_util_create_rpa_conn(54,
+                                     BLE_ADDR_TYPE_RPA_PUB_DEFAULT,
+                                     ((uint8_t[6]){0x40,1,2,3,4,5}),
+                                     BLE_ADDR_TYPE_RPA_RND_DEFAULT,
+                                     ((uint8_t[6]){3,4,5,6,7,8}),
+                                     ((uint8_t[6]){0x50,1,2,3,4,5}),
+                                     ble_gap_test_util_connect_cb,
+                                     NULL);
+
+    rc = ble_gap_conn_find(54, &desc);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(desc.conn_handle == 54);
+    TEST_ASSERT(desc.our_id_addr_type == BLE_ADDR_TYPE_PUBLIC);
+    TEST_ASSERT(desc.our_ota_addr_type == BLE_ADDR_TYPE_RPA_PUB_DEFAULT);
+    TEST_ASSERT(desc.peer_ota_addr_type == BLE_ADDR_TYPE_RPA_RND_DEFAULT);
+    TEST_ASSERT(desc.role == BLE_GAP_ROLE_MASTER);
+    TEST_ASSERT(memcmp(desc.our_ota_addr,
+                       ((uint8_t[6]){0x40,1,2,3,4,5}), 6) == 0);
+    TEST_ASSERT(memcmp(desc.our_id_addr, pub_addr, 6) == 0);
+    TEST_ASSERT(memcmp(desc.peer_ota_addr,
+                       ((uint8_t[6]){0x50,1,2,3,4,5}), 6) == 0);
+    TEST_ASSERT(memcmp(desc.peer_id_addr,
+                       ((uint8_t[6]){3,4,5,6,7,8}), 6) == 0);
+    TEST_ASSERT(desc.conn_itvl == BLE_GAP_INITIAL_CONN_ITVL_MAX);
+    TEST_ASSERT(desc.conn_latency == BLE_GAP_INITIAL_CONN_LATENCY);
+    TEST_ASSERT(desc.supervision_timeout ==
+                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
+    TEST_ASSERT(desc.master_clock_accuracy == 0);
+    TEST_ASSERT(!desc.sec_state.encrypted);
+    TEST_ASSERT(!desc.sec_state.authenticated);
+    TEST_ASSERT(!desc.sec_state.bonded);
+
+    /*** Swap roles. */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(54);
+    conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
+    ble_hs_unlock();
+
+    rc = ble_gap_conn_find(54, &desc);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(desc.role == BLE_GAP_ROLE_SLAVE);
+}
+
+TEST_SUITE(ble_gap_test_suite_conn_find)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_conn_find();
+}
+
+/*****************************************************************************
+ * $advertise                                                                *
+ *****************************************************************************/
+
+static void
+ble_gap_test_util_adv(uint8_t own_addr_type, uint8_t peer_addr_type,
+                      const uint8_t *peer_addr, uint8_t conn_mode,
+                      uint8_t disc_mode, int connect_status,
+                      int cmd_fail_idx, uint8_t fail_status)
+{
+    struct hci_le_conn_complete evt;
+    struct ble_gap_adv_params adv_params;
+    struct ble_hs_adv_fields adv_fields;
+    uint8_t hci_status;
+    int cmd_idx;
+    int rc;
+
+    ble_gap_test_util_init();
+
+    adv_params = ble_hs_test_util_adv_params;
+    adv_params.conn_mode = conn_mode;
+    adv_params.disc_mode = disc_mode;
+
+    TEST_ASSERT(!ble_gap_adv_active());
+
+    cmd_idx = 0;
+
+    if (conn_mode != BLE_GAP_CONN_MODE_DIR) {
+        memset(&adv_fields, 0, sizeof adv_fields);
+        adv_fields.tx_pwr_lvl_is_present = 1;
+        adv_fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
+
+        hci_status = ble_hs_test_util_exp_hci_status(cmd_idx, cmd_fail_idx,
+                                                     fail_status);
+        rc = ble_hs_test_util_adv_set_fields(&adv_fields, hci_status);
+
+        if (adv_fields.tx_pwr_lvl_is_present &&
+            adv_fields.tx_pwr_lvl == BLE_HS_ADV_TX_PWR_LVL_AUTO) {
+
+            TEST_ASSERT_FATAL(rc == BLE_HS_HCI_ERR(hci_status));
+            cmd_idx++;
+        }
+    }
+
+    if (fail_status == 0 || cmd_fail_idx >= cmd_idx) {
+        rc = ble_hs_test_util_adv_start(own_addr_type, peer_addr_type,
+                                        peer_addr, &adv_params,
+                                        ble_gap_test_util_connect_cb, NULL,
+                                        cmd_fail_idx - cmd_idx, fail_status);
+
+        TEST_ASSERT(rc == BLE_HS_HCI_ERR(fail_status));
+    }
+
+    if (fail_status == 0 || cmd_fail_idx >= cmd_idx) {
+        /* Verify tx of set advertising params command. */
+        ble_gap_test_util_verify_tx_adv_params();
+    }
+    cmd_idx++;
+
+    if (conn_mode != BLE_GAP_CONN_MODE_DIR) {
+        if (fail_status == 0 || cmd_fail_idx >= cmd_idx) {
+            /* Verify tx of set advertise data command. */
+            ble_gap_test_util_verify_tx_adv_data();
+        }
+        cmd_idx++;
+
+        if (fail_status == 0 || cmd_fail_idx >= cmd_idx) {
+            /* Verify tx of set scan response data command. */
+            ble_gap_test_util_verify_tx_rsp_data();
+        }
+        cmd_idx++;
+    }
+
+    if (fail_status == 0 || cmd_fail_idx >= cmd_idx) {
+        /* Verify tx of set advertise enable command. */
+        ble_gap_test_util_verify_tx_adv_enable(1);
+    }
+    cmd_idx++;
+
+    if (connect_status != -1 &&
+        (fail_status == 0 || cmd_fail_idx >= cmd_idx)) {
+
+        TEST_ASSERT(ble_gap_adv_active());
+
+        /* Receive a connection complete event. */
+        if (conn_mode != BLE_GAP_CONN_MODE_NON) {
+            memset(&evt, 0, sizeof evt);
+            evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+            evt.status = connect_status;
+            evt.connection_handle = 2;
+            evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_SLAVE;
+            memcpy(evt.peer_addr, peer_addr, 6);
+            rc = ble_gap_rx_conn_complete(&evt);
+            TEST_ASSERT(rc == 0);
+
+            if (connect_status == 0 ||
+                connect_status == BLE_ERR_DIR_ADV_TMO) {
+
+                TEST_ASSERT(!ble_gap_adv_active());
+            } else {
+                TEST_ASSERT(ble_gap_adv_active());
+            }
+        }
+    }
+}
+
+TEST_CASE(ble_gap_test_case_adv_bad_args)
+{
+    struct ble_gap_adv_params adv_params;
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int rc;
+
+    ble_gap_test_util_init();
+
+    TEST_ASSERT(!ble_gap_adv_active());
+
+    /*** Invalid discoverable mode. */
+    adv_params = ble_hs_test_util_adv_params;
+    adv_params.disc_mode = 43;
+    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                    peer_addr, &adv_params,
+                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+    TEST_ASSERT(!ble_gap_adv_active());
+
+    /*** Invalid connectable mode. */
+    adv_params = ble_hs_test_util_adv_params;
+    adv_params.conn_mode = 27;
+    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                    peer_addr, &adv_params,
+                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+    TEST_ASSERT(!ble_gap_adv_active());
+
+    /*** Invalid peer address type with directed advertisable mode. */
+    adv_params = ble_hs_test_util_adv_params;
+    adv_params.conn_mode = BLE_GAP_CONN_MODE_DIR;
+    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, 12,
+                                    peer_addr, &adv_params,
+                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
+    TEST_ASSERT(rc == BLE_HS_EINVAL);
+    TEST_ASSERT(!ble_gap_adv_active());
+
+    /*** Advertising already in progress. */
+    adv_params = ble_hs_test_util_adv_params;
+    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                    peer_addr, &adv_params,
+                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(ble_gap_adv_active());
+
+    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                    peer_addr, &adv_params,
+                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
+    TEST_ASSERT(rc == BLE_HS_EALREADY);
+    TEST_ASSERT(ble_gap_adv_active());
+}
+
+static void
+ble_gap_test_util_adv_verify_dflt_params(uint8_t own_addr_type,
+                                         uint8_t peer_addr_type,
+                                         const uint8_t *peer_addr,
+                                         uint8_t conn_mode,
+                                         uint8_t disc_mode)
+{
+    struct ble_gap_adv_params adv_params;
+    struct hci_adv_params hci_cmd;
+    uint8_t *hci_buf;
+    uint8_t hci_param_len;
+    int rc;
+
+    ble_gap_test_util_init();
+
+    TEST_ASSERT(!ble_gap_adv_active());
+
+    adv_params = ble_hs_test_util_adv_params;
+    adv_params.conn_mode = conn_mode;
+    adv_params.disc_mode = disc_mode;
+
+    /* Let stack calculate all default parameters. */
+    adv_params.itvl_min = 0;
+    adv_params.itvl_max = 0;
+    adv_params.channel_map = 0;
+    adv_params.filter_policy = 0;
+    adv_params.high_duty_cycle = 0;
+
+    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                    peer_addr, &adv_params,
+                                    ble_gap_test_util_connect_cb, NULL, 0, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure default parameters properly filled in. */
+    hci_buf = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                             BLE_HCI_OCF_LE_SET_ADV_PARAMS,
+                                             &hci_param_len);
+    TEST_ASSERT_FATAL(hci_buf != NULL);
+    TEST_ASSERT_FATAL(hci_param_len == BLE_HCI_SET_ADV_PARAM_LEN);
+
+    hci_cmd.adv_itvl_min = le16toh(hci_buf + 0);
+    hci_cmd.adv_itvl_max = le16toh(hci_buf + 2);
+    hci_cmd.adv_type = hci_buf[4];
+    hci_cmd.own_addr_type = hci_buf[5];
+    hci_cmd.peer_addr_type = hci_buf[6];
+    memcpy(hci_cmd.peer_addr, hci_buf + 7, 6);
+    hci_cmd.adv_channel_map = hci_buf[13];
+    hci_cmd.adv_filter_policy = hci_buf[14];
+
+    if (conn_mode == BLE_GAP_CONN_MODE_NON) {
+        TEST_ASSERT(hci_cmd.adv_itvl_min == BLE_GAP_ADV_FAST_INTERVAL2_MIN);
+        TEST_ASSERT(hci_cmd.adv_itvl_max == BLE_GAP_ADV_FAST_INTERVAL2_MAX);
+    } else {
+        TEST_ASSERT(hci_cmd.adv_itvl_min == BLE_GAP_ADV_FAST_INTERVAL1_MIN);
+        TEST_ASSERT(hci_cmd.adv_itvl_max == BLE_GAP_ADV_FAST_INTERVAL1_MAX);
+    }
+
+    if (conn_mode == BLE_GAP_CONN_MODE_NON) {
+        if (disc_mode == BLE_GAP_DISC_MODE_NON) {
+            TEST_ASSERT(hci_cmd.adv_type == BLE_HCI_ADV_TYPE_ADV_NONCONN_IND);
+        } else {
+            TEST_ASSERT(hci_cmd.adv_type == BLE_HCI_ADV_TYPE_ADV_SCAN_IND);
+        }
+    } else if (conn_mode == BLE_GAP_CONN_MODE_UND) {
+        TEST_ASSERT(hci_cmd.adv_type == BLE_HCI_ADV_TYPE_ADV_IND);
+    } else {
+        TEST_ASSERT(hci_cmd.adv_type == BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_LD);
+    }
+}
+
+TEST_CASE(ble_gap_test_case_adv_dflt_params)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    int d;
+    int c;
+
+    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
+        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
+            ble_gap_test_util_adv_verify_dflt_params(
+                BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC, peer_addr, c, d);
+        }
+    }
+}
+
+TEST_CASE(ble_gap_test_case_adv_good)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int d;
+    int c;
+
+    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
+        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
+            ble_gap_test_util_adv(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                  peer_addr, c, d, BLE_ERR_SUCCESS, -1, 0);
+
+            if (c != BLE_GAP_CONN_MODE_NON) {
+                TEST_ASSERT(!ble_gap_adv_active());
+                TEST_ASSERT(ble_gap_test_conn_event_type ==
+                                BLE_GAP_EVENT_CONNECT);
+                TEST_ASSERT(ble_gap_test_conn_status == 0);
+                TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+                TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                                   peer_addr, 6) == 0);
+                TEST_ASSERT(ble_gap_test_conn_arg == NULL);
+            }
+        }
+    }
+}
+
+TEST_CASE(ble_gap_test_case_adv_ctlr_fail)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int d;
+    int c;
+
+    for (c = BLE_GAP_CONN_MODE_NON + 1; c < BLE_GAP_CONN_MODE_MAX; c++) {
+        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
+            ble_gap_test_util_adv(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
+                                  peer_addr, c, d, BLE_ERR_DIR_ADV_TMO, -1, 0);
+
+            TEST_ASSERT(!ble_gap_adv_active());
+            TEST_ASSERT(ble_gap_test_conn_event_type ==
+                        BLE_GAP_EVENT_ADV_COMPLETE);
+            TEST_ASSERT(ble_gap_test_conn_desc.conn_handle ==
+                        BLE_HS_CONN_HANDLE_NONE);
+            TEST_ASSERT(ble_gap_test_conn_arg == NULL);
+        }
+    }
+}
+
+TEST_CASE(ble_gap_test_case_adv_hci_fail)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int num_hci_cmds;
+    int fail_idx;
+    int d;
+    int c;
+
+    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
+        if (c == BLE_GAP_CONN_MODE_DIR) {
+            num_hci_cmds = 2;
+        } else {
+            num_hci_cmds = 5;
+        }
+
+        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
+            for (fail_idx = 0; fail_idx < num_hci_cmds; fail_idx++) {
+                ble_gap_test_util_adv(BLE_ADDR_TYPE_PUBLIC,
+                                      BLE_ADDR_TYPE_PUBLIC, peer_addr,
+                                      c, d, 0, fail_idx, BLE_ERR_UNSUPPORTED);
+
+                TEST_ASSERT(!ble_gap_adv_active());
+                TEST_ASSERT(ble_gap_test_conn_event_type == -1);
+            }
+        }
+    }
+}
+
+TEST_SUITE(ble_gap_test_suite_adv)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_adv_bad_args();
+    ble_gap_test_case_adv_dflt_params();
+    ble_gap_test_case_adv_good();
+    ble_gap_test_case_adv_ctlr_fail();
+    ble_gap_test_case_adv_hci_fail();
+}
+
+/*****************************************************************************
+ * $stop advertise                                                           *
+ *****************************************************************************/
+
+static void
+ble_gap_test_util_stop_adv(uint8_t peer_addr_type, const uint8_t *peer_addr,
+                           uint8_t conn_mode, uint8_t disc_mode,
+                           int cmd_fail_idx, uint8_t fail_status)
+{
+    uint8_t hci_status;
+    int rc;
+
+    ble_gap_test_util_init();
+
+    /* Start advertising; don't rx a successful connection event. */
+    ble_gap_test_util_adv(BLE_ADDR_TYPE_PUBLIC, peer_addr_type, peer_addr,
+                          conn_mode, disc_mode, -1, -1, 0);
+
+    TEST_ASSERT(ble_gap_adv_active());
+
+    /* Stop advertising. */
+    hci_status = cmd_fail_idx == 0 ? fail_status : 0;
+
+    rc = ble_hs_test_util_adv_stop(hci_status);
+    TEST_ASSERT(rc == BLE_HS_HCI_ERR(hci_status));
+
+    /* Verify tx of advertising enable command. */
+    ble_gap_test_util_verify_tx_adv_enable(0);
+}
+
+TEST_CASE(ble_gap_test_case_stop_adv_good)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int d;
+    int c;
+
+    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
+        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
+            ble_gap_test_util_stop_adv(BLE_ADDR_TYPE_PUBLIC, peer_addr, c, d,
+                                       -1, 0);
+            TEST_ASSERT(!ble_gap_adv_active());
+            TEST_ASSERT(ble_gap_test_conn_event_type == -1);
+            TEST_ASSERT(ble_gap_test_conn_status == -1);
+            TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == (uint16_t)-1);
+            TEST_ASSERT(ble_gap_test_conn_arg == (void *)-1);
+        }
+    }
+}
+
+TEST_CASE(ble_gap_test_case_stop_adv_hci_fail)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+    int d;
+    int c;
+
+    for (c = BLE_GAP_CONN_MODE_NON; c < BLE_GAP_CONN_MODE_MAX; c++) {
+        for (d = BLE_GAP_DISC_MODE_NON; d < BLE_GAP_DISC_MODE_MAX; d++) {
+            ble_gap_test_util_stop_adv(BLE_ADDR_TYPE_PUBLIC, peer_addr, c, d,
+                                       0, BLE_ERR_UNSUPPORTED);
+            TEST_ASSERT(ble_gap_adv_active());
+            TEST_ASSERT(ble_gap_test_conn_event_type == -1);
+            TEST_ASSERT(ble_gap_test_conn_status == -1);
+            TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == (uint16_t)-1);
+            TEST_ASSERT(ble_gap_test_conn_arg == (void *)-1);
+        }
+    }
+}
+
+TEST_SUITE(ble_gap_test_suite_stop_adv)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_stop_adv_good();
+    ble_gap_test_case_stop_adv_hci_fail();
+}
+
+/*****************************************************************************
+ * $update connection                                                        *
+ *****************************************************************************/
+
+static void
+ble_gap_test_util_update(struct ble_gap_upd_params *params,
+                         int cmd_fail_idx, uint8_t hci_status,
+                         uint8_t event_status)
+{
+    int status;
+    int rc;
+
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    ble_gap_test_util_init();
+
+    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
+                                 NULL);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    rc = ble_hs_test_util_conn_update(2, params, hci_status);
+    TEST_ASSERT(rc == BLE_HS_HCI_ERR(hci_status));
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    /* Verify tx of connection update command. */
+    ble_gap_test_util_verify_tx_update_conn(params);
+
+    if (rc == 0) {
+        TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
+    } else {
+        TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
+        return;
+    }
+
+    /* Receive connection update complete event. */
+    ble_gap_test_util_rx_update_complete(event_status, params);
+
+    if (event_status != 0) {
+        status = BLE_HS_HCI_ERR(event_status);
+        goto fail;
+    }
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
+    TEST_ASSERT(ble_gap_test_conn_status == 0);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl == params->itvl_max);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency == params->latency);
+    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
+        params->supervision_timeout);
+
+    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
+
+    return;
+
+fail:
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
+    TEST_ASSERT(ble_gap_test_conn_status == status);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
+                BLE_GAP_INITIAL_CONN_ITVL_MAX);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
+                BLE_GAP_INITIAL_CONN_LATENCY);
+    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
+                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
+    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
+}
+
+static void
+ble_gap_test_util_update_peer(uint8_t status,
+                              struct ble_gap_upd_params *params)
+{
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    ble_gap_test_util_init();
+
+    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
+                                 NULL);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    /* Receive connection update complete event. */
+    ble_gap_test_util_rx_update_complete(status, params);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
+    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(status));
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+
+    if (status == 0) {
+        TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl == params->itvl_max);
+        TEST_ASSERT(ble_gap_test_conn_desc.conn_latency == params->latency);
+        TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
+                    params->supervision_timeout);
+    }
+
+    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
+}
+
+static void
+ble_gap_test_util_update_req_pos(struct ble_gap_upd_params *peer_params,
+                                 struct ble_gap_upd_params *self_params,
+                                 int cmd_fail_idx, uint8_t hci_status)
+{
+    int cmd_idx;
+    int rc;
+
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    ble_gap_test_util_init();
+    cmd_idx = 0;
+
+    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
+                                 NULL);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    ble_gap_test_conn_self_params = *self_params;
+    rc = ble_gap_test_util_rx_param_req(peer_params, 1, &cmd_idx, cmd_fail_idx,
+                                        hci_status);
+    if (rc != 0) {
+        goto hci_fail;
+    }
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
+
+    /* Verify tx of connection parameters reply command. */
+    ble_gap_test_util_verify_tx_params_reply_pos();
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
+
+    /* Receive connection update complete event. */
+    ble_gap_test_util_rx_update_complete(0, self_params);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
+    TEST_ASSERT(ble_gap_test_conn_status == 0);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl == self_params->itvl_max);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency == self_params->latency);
+    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
+                self_params->supervision_timeout);
+
+    return;
+
+hci_fail:
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
+    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(hci_status));
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
+                BLE_GAP_INITIAL_CONN_ITVL_MAX);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
+                BLE_GAP_INITIAL_CONN_LATENCY);
+    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
+                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
+}
+
+static void
+ble_gap_test_util_update_req_neg(struct ble_gap_upd_params *peer_params,
+                                 int cmd_fail_idx, uint8_t hci_status)
+{
+    int cmd_idx;
+    int reason;
+    int rc;
+
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    ble_gap_test_util_init();
+    cmd_idx = 0;
+
+    reason = BLE_ERR_UNSPECIFIED;
+    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
+                                 &reason);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
+
+    rc = ble_gap_test_util_rx_param_req(peer_params, 0, &cmd_idx, cmd_fail_idx,
+                                        hci_status);
+    if (rc != 0) {
+        goto hci_fail;
+    }
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
+
+    /* Verify tx of connection parameters negative reply command. */
+    ble_gap_test_util_verify_tx_params_reply_neg(reason);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
+
+    return;
+
+hci_fail:
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
+    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(hci_status));
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
+                BLE_GAP_INITIAL_CONN_ITVL_MAX);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
+                BLE_GAP_INITIAL_CONN_LATENCY);
+    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
+                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
+}
+
+static void
+ble_gap_test_util_update_req_concurrent(
+    struct ble_gap_upd_params *init_params,
+    struct ble_gap_upd_params *peer_params,
+    struct ble_gap_upd_params *self_params,
+    int cmd_fail_idx,
+    uint8_t fail_status)
+{
+    uint8_t hci_status;
+    int cmd_idx;
+    int rc;
+
+    uint8_t peer_addr[6] = { 1, 2, 3, 4, 5, 6 };
+
+    ble_gap_test_util_init();
+
+    ble_hs_test_util_create_conn(2, peer_addr, ble_gap_test_util_connect_cb,
+                                 NULL);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
+
+    hci_status = cmd_fail_idx == 0 ? fail_status : 0;
+    rc = ble_hs_test_util_conn_update(2, init_params, hci_status);
+    TEST_ASSERT(rc == BLE_HS_HCI_ERR(hci_status));
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+
+    /* Verify tx of connection update command. */
+    ble_gap_test_util_verify_tx_update_conn(init_params);
+
+    if (rc == 0) {
+        TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
+    } else {
+        TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
+        return;
+    }
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
+
+    /* Receive connection parameter update request from peer. */
+    ble_gap_test_conn_self_params = *self_params;
+    rc = ble_gap_test_util_rx_param_req(peer_params, 1, &cmd_idx, cmd_fail_idx,
+                                        hci_status);
+    if (rc != 0) {
+        goto hci_fail;
+    }
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
+
+    /* Verify tx of connection parameters reply command. */
+    ble_gap_test_util_verify_tx_params_reply_pos();
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
+
+    /* Receive connection update complete event. */
+    ble_gap_test_util_rx_update_complete(0, self_params);
+
+    TEST_ASSERT(!ble_gap_master_in_progress());
+    TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
+    TEST_ASSERT(ble_gap_test_conn_status == 0);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl == self_params->itvl_max);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency == self_params->latency);
+    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
+                self_params->supervision_timeout);
+
+    return;
+
+hci_fail:
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
+    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(fail_status));
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_handle == 2);
+    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
+                       peer_addr, 6) == 0);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_itvl ==
+                BLE_GAP_INITIAL_CONN_ITVL_MAX);
+    TEST_ASSERT(ble_gap_test_conn_desc.conn_latency ==
+                BLE_GAP_INITIAL_CONN_LATENCY);
+    TEST_ASSERT(ble_gap_test_conn_desc.supervision_timeout ==
+                BLE_GAP_INITIAL_SUPERVISION_TIMEOUT);
+}
+
+TEST_CASE(ble_gap_test_case_update_conn_good)
+{
+    ble_gap_test_util_update(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 10,
+            .itvl_max = 100,
+            .supervision_timeout = 0,
+            .min_ce_len = 123,
+            .max_ce_len = 456,
+        }}),
+        -1, 0, 0);
+
+    ble_gap_test_util_update(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 100,
+            .itvl_max = 100,
+            .supervision_timeout = 100,
+            .min_ce_len = 554,
+            .max_ce_len = 554,
+        }}),
+        -1, 0, 0);
+}
+
+TEST_CASE(ble_gap_test_case_update_conn_bad)
+{
+    ble_gap_test_util_update(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 10,
+            .itvl_max = 100,
+            .supervision_timeout = 0,
+            .min_ce_len = 123,
+            .max_ce_len = 456,
+        }}),
+        -1, 0, BLE_ERR_LMP_COLLISION);
+}
+
+TEST_CASE(ble_gap_test_case_update_conn_hci_fail)
+{
+    ble_gap_test_util_update(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 10,
+            .itvl_max = 100,
+            .supervision_timeout = 0,
+            .min_ce_len = 123,
+            .max_ce_len = 456,
+        }}),
+        0, BLE_ERR_UNSUPPORTED, 0);
+}
+
+TEST_CASE(ble_gap_test_case_update_peer_good)
+{
+    ble_gap_test_util_update_peer(0,
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 10,
+            .itvl_max = 100,
+            .supervision_timeout = 0,
+            .min_ce_len = 123,
+            .max_ce_len = 456,
+        }}));
+
+    ble_gap_test_util_update_peer(0,
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 100,
+            .itvl_max = 100,
+            .supervision_timeout = 100,
+            .min_ce_len = 554,
+            .max_ce_len = 554,
+        }}));
+}
+
+TEST_CASE(ble_gap_test_case_update_req_good)
+{
+    ble_gap_test_util_update_req_pos(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 50,
+            .itvl_max = 500,
+            .supervision_timeout = 20,
+            .min_ce_len = 555,
+            .max_ce_len = 888,
+        }}),
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 10,
+            .itvl_max = 100,
+            .supervision_timeout = 0,
+            .min_ce_len = 123,
+            .max_ce_len = 456,
+        }}),
+        -1, 0);
+
+    ble_gap_test_util_update_req_pos(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 50,
+            .itvl_max = 500,
+            .supervision_timeout = 20,
+            .min_ce_len = 555,
+            .max_ce_len = 888,
+        }}),
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 100,
+            .itvl_max = 100,
+            .supervision_timeout = 100,
+            .min_ce_len = 554,
+            .max_ce_len = 554,
+        }}),
+        -1, 0);
+
+}
+
+TEST_CASE(ble_gap_test_case_update_req_hci_fail)
+{
+    ble_gap_test_util_update_req_pos(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 50,
+            .itvl_max = 500,
+            .supervision_timeout = 20,
+            .min_ce_len = 555,
+            .max_ce_len = 888,
+        }}),
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 10,
+            .itvl_max = 100,
+            .supervision_timeout = 0,
+            .min_ce_len = 123,
+            .max_ce_len = 456,
+        }}),
+        0, BLE_ERR_UNSUPPORTED);
+}
+
+TEST_CASE(ble_gap_test_case_update_req_reject)
+{
+    ble_gap_test_util_update_req_neg(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 50,
+            .itvl_max = 500,
+            .supervision_timeout = 20,
+            .min_ce_len = 555,
+            .max_ce_len = 888,
+        }}),
+        -1, 0);
+
+    ble_gap_test_util_update_req_neg(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 50,
+            .itvl_max = 500,
+            .supervision_timeout = 20,
+            .min_ce_len = 555,
+            .max_ce_len = 888,
+        }}),
+        -1, 0);
+}
+
+TEST_CASE(ble_gap_test_case_update_concurrent_good)
+{
+    ble_gap_test_util_update_req_concurrent(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 10,
+            .itvl_max = 100,
+            .supervision_timeout = 0,
+            .min_ce_len = 123,
+            .max_ce_len = 456,
+        }}),
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 50,
+            .itvl_max = 500,
+            .supervision_timeout = 20,
+            .min_ce_len = 555,
+            .max_ce_len = 888,
+        }}),
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 10,
+            .itvl_max = 100,
+            .supervision_timeout = 0,
+            .min_ce_len = 123,
+            .max_ce_len = 456,
+        }}),
+        -1, 0);
+
+    ble_gap_test_util_update_req_concurrent(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 10,
+            .itvl_max = 100,
+            .supervision_timeout = 0,
+            .min_ce_len = 123,
+            .max_ce_len = 456,
+        }}),
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 50,
+            .itvl_max = 500,
+            .supervision_timeout = 20,
+            .min_ce_len = 555,
+            .max_ce_len = 888,
+        }}),
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 20,
+            .itvl_max = 200,
+            .supervision_timeout = 2,
+            .min_ce_len = 111,
+            .max_ce_len = 222,
+        }}),
+        -1, 0);
+}
+
+TEST_CASE(ble_gap_test_case_update_concurrent_hci_fail)
+{
+    ble_gap_test_util_update_req_concurrent(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 10,
+            .itvl_max = 100,
+            .supervision_timeout = 0,
+            .min_ce_len = 123,
+            .max_ce_len = 456,
+        }}),
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 50,
+            .itvl_max = 500,
+            .supervision_timeout = 20,
+            .min_ce_len = 555,
+            .max_ce_len = 888,
+        }}),
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 20,
+            .itvl_max = 200,
+            .supervision_timeout = 2,
+            .min_ce_len = 111,
+            .max_ce_len = 222,
+        }}),
+        0, BLE_ERR_UNSUPPORTED);
+
+    ble_gap_test_util_update_req_concurrent(
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 10,
+            .itvl_max = 100,
+            .supervision_timeout = 0,
+            .min_ce_len = 123,
+            .max_ce_len = 456,
+        }}),
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 50,
+            .itvl_max = 500,
+            .supervision_timeout = 20,
+            .min_ce_len = 555,
+            .max_ce_len = 888,
+        }}),
+        ((struct ble_gap_upd_params[]) { {
+            .itvl_min = 20,
+            .itvl_max = 200,
+            .supervision_timeout = 2,
+            .min_ce_len = 111,
+            .max_ce_len = 222,
+        }}),
+        1, BLE_ERR_UNSUPPORTED);
+}
+
+TEST_SUITE(ble_gap_test_suite_update_conn)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_update_conn_good();
+    ble_gap_test_case_update_conn_bad();
+    ble_gap_test_case_update_conn_hci_fail();
+    ble_gap_test_case_update_peer_good();
+    ble_gap_test_case_update_req_good();
+    ble_gap_test_case_update_req_hci_fail();
+    ble_gap_test_case_update_req_reject();
+    ble_gap_test_case_update_concurrent_good();
+    ble_gap_test_case_update_concurrent_hci_fail();
+}
+
+/*****************************************************************************
+ * $timeout                                                                  *
+ *****************************************************************************/
+
+static void
+ble_gap_test_util_conn_forever(void)
+{
+    int32_t ticks_from_now;
+
+    /* Initiate a connect procedure with no timeout. */
+    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
+                             BLE_ADDR_TYPE_PUBLIC,
+                             ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), BLE_HS_FOREVER,
+                             NULL, ble_gap_test_util_connect_cb,
+                             NULL, 0);
+
+    /* Ensure no pending GAP event. */
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
+
+    /* Advance 100 seconds; ensure no timeout reported. */
+    os_time_advance(100 * OS_TICKS_PER_SEC);
+    ble_gap_heartbeat();
+    TEST_ASSERT(ble_gap_test_conn_event_type == -1);
+    TEST_ASSERT(ble_gap_conn_active());
+}
+
+static void
+ble_gap_test_util_conn_timeout(int32_t duration_ms)
+{
+    struct hci_le_conn_complete evt;
+    uint32_t duration_ticks;
+    int32_t ticks_from_now;
+    int rc;
+
+    TEST_ASSERT_FATAL(duration_ms != BLE_HS_FOREVER);
+
+    /* Initiate a connect procedure with the specified timeout. */
+    ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
+                             BLE_ADDR_TYPE_PUBLIC,
+                             ((uint8_t[]){ 1, 2, 3, 4, 5, 6 }), duration_ms,
+                             NULL, ble_gap_test_util_connect_cb,
+                             NULL, 0);
+
+    /* Ensure next GAP event is at the expected time. */
+    rc = os_time_ms_to_ticks(duration_ms, &duration_ticks);
+    TEST_ASSERT_FATAL(rc == 0);
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == duration_ticks);
+
+    /* Advance duration ms; ensure timeout event does not get reported before
+     * connection complete event rxed.
+     */
+    os_time_advance(duration_ms);
+
+    ble_hs_test_util_set_ack(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_CREATE_CONN_CANCEL),
+        0);
+
+    TEST_ASSERT(ble_gap_test_conn_event_type == -1);
+
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
+
+    /* Ensure cancel create connection command was sent. */
+    ble_hs_test_util_verify_tx_create_conn_cancel();
+
+    /* Ensure timer has been stopped. */
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
+
+    /* Receive the connection complete event indicating a successful cancel. */
+    memset(&evt, 0, sizeof evt);
+    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
+    evt.status = BLE_ERR_UNK_CONN_ID;
+    rc = ble_gap_rx_conn_complete(&evt);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure the GAP event was triggered. */
+    TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONNECT);
+    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_ETIMEOUT);
+
+    /* Clear GAP event for remainder of test. */
+    ble_gap_test_util_reset_cb_info();
+}
+
+static void
+ble_gap_test_util_disc_forever(void)
+{
+    struct ble_gap_disc_params params;
+    int32_t ticks_from_now;
+
+    memset(&params, 0, sizeof params);
+
+    /* Initiate a discovery procedure with no timeout. */
+    ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC,
+                          BLE_HS_FOREVER, &params, ble_gap_test_util_disc_cb,
+                          NULL, -1, 0);
+
+    /* Ensure no pending GAP event. */
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
+
+    /* Advance 100 seconds; ensure no timeout reported. */
+    os_time_advance(100 * OS_TICKS_PER_SEC);
+    TEST_ASSERT(ble_gap_test_disc_event_type == -1);
+    TEST_ASSERT(ble_gap_disc_active());
+}
+
+static void
+ble_gap_test_util_disc_timeout(int32_t duration_ms)
+{
+    struct ble_gap_disc_params params;
+    uint32_t duration_ticks;
+    int32_t ticks_from_now;
+    int rc;
+
+    TEST_ASSERT_FATAL(duration_ms != BLE_HS_FOREVER);
+
+    memset(&params, 0, sizeof params);
+
+    /* Initiate a discovery procedure with the specified timeout. */
+    ble_hs_test_util_disc(BLE_ADDR_TYPE_PUBLIC,
+                          duration_ms, &params, ble_gap_test_util_disc_cb,
+                          NULL, -1, 0);
+
+    /* Ensure next GAP event is at the expected time. */
+    rc = os_time_ms_to_ticks(duration_ms, &duration_ticks);
+    TEST_ASSERT_FATAL(rc == 0);
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == duration_ticks);
+
+    /* Advance duration ms; ensure timeout event was reported. */
+    os_time_advance(duration_ms);
+
+    ble_hs_test_util_set_ack(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_SET_SCAN_ENABLE),
+        0);
+    ticks_from_now = ble_gap_heartbeat();
+    TEST_ASSERT(ticks_from_now == BLE_HS_FOREVER);
+
+    TEST_ASSERT(ble_gap_test_disc_event_type == BLE_GAP_EVENT_DISC_COMPLETE);
+
+    /* Clear GAP event for remainder of test. */
+    ble_gap_test_util_reset_cb_info();
+}
+
+TEST_CASE(ble_gap_test_case_conn_timeout_conn_forever)
+{
+    ble_gap_test_util_init();
+
+    /* 3 ms. */
+    ble_gap_test_util_conn_timeout(3);
+
+    /* No timeout. */
+    ble_gap_test_util_conn_forever();
+
+}
+
+TEST_CASE(ble_gap_test_case_conn_timeout_conn_timeout)
+{
+    ble_gap_test_util_init();
+
+    /* 30 ms. */
+    ble_gap_test_util_conn_timeout(30);
+
+    /* 5 ms. */
+    ble_gap_test_util_conn_timeout(5);
+
+}
+
+TEST_CASE(ble_gap_test_case_conn_forever_conn_timeout)
+{
+    ble_gap_test_util_init();
+
+    /* No timeout. */
+    ble_gap_test_util_conn_forever();
+
+    /* Cancel connect procedure manually. */
+    ble_gap_test_util_conn_cancel(0);
+
+    /* Clear GAP event for remainder of test. */
+    ble_gap_test_util_reset_cb_info();
+
+    /* 3 ms. */
+    ble_gap_test_util_conn_timeout(3);
+}
+
+TEST_CASE(ble_gap_test_case_disc_timeout_disc_forever)
+{
+    ble_gap_test_util_init();
+
+    /* 3 ms. */
+    ble_gap_test_util_disc_timeout(3);
+
+    /* No timeout. */
+    ble_gap_test_util_disc_forever();
+
+}
+
+TEST_CASE(ble_gap_test_case_disc_timeout_disc_timeout)
+{
+    ble_gap_test_util_init();
+
+    /* 30 ms. */
+    ble_gap_test_util_disc_timeout(30);
+
+    /* 5 ms. */
+    ble_gap_test_util_disc_timeout(5);
+
+}
+
+TEST_CASE(ble_gap_test_case_disc_forever_disc_timeout)
+{
+    ble_gap_test_util_init();
+
+    /* No timeout. */
+    ble_gap_test_util_disc_forever();
+
+    /* Cancel discovery procedure manually. */
+    ble_hs_test_util_disc_cancel(0);
+
+    /* 3 ms. */
+    ble_gap_test_util_disc_timeout(3);
+}
+
+TEST_CASE(ble_gap_test_case_conn_timeout_disc_timeout)
+{
+    ble_gap_test_util_init();
+
+    /* 15 seconds. */
+    ble_gap_test_util_conn_timeout(15000);
+
+    /* 1285 ms. */
+    ble_gap_test_util_disc_timeout(1285);
+}
+
+TEST_SUITE(ble_gap_test_suite_timeout)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gap_test_case_conn_timeout_conn_forever();
+    ble_gap_test_case_conn_timeout_conn_timeout();
+    ble_gap_test_case_conn_forever_conn_timeout();
+
+    ble_gap_test_case_disc_timeout_disc_forever();
+    ble_gap_test_case_disc_timeout_disc_timeout();
+    ble_gap_test_case_disc_forever_disc_timeout();
+
+    ble_gap_test_case_conn_timeout_disc_timeout();
+}
+
+/*****************************************************************************
+ * $all                                                                      *
+ *****************************************************************************/
+
+int
+ble_gap_test_all(void)
+{
+    ble_gap_test_suite_wl();
+    ble_gap_test_suite_disc();
+    ble_gap_test_suite_conn_gen();
+    ble_gap_test_suite_conn_cancel();
+    ble_gap_test_suite_conn_terminate();
+    ble_gap_test_suite_conn_find();
+    ble_gap_test_suite_adv();
+    ble_gap_test_suite_stop_adv();
+    ble_gap_test_suite_update_conn();
+    ble_gap_test_suite_timeout();
+
+    return tu_any_failed;
+}


[22/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_gatt_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_conn_test.c b/net/nimble/host/src/test/ble_gatt_conn_test.c
deleted file mode 100644
index be4a46d..0000000
--- a/net/nimble/host/src/test/ble_gatt_conn_test.c
+++ /dev/null
@@ -1,533 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-
-#define BLE_GATT_BREAK_TEST_READ_ATTR_HANDLE        0x9383
-#define BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE       0x1234
-
-static uint8_t ble_gatt_conn_test_write_value[] = { 1, 3, 64, 21, 6 };
-
-struct ble_gatt_conn_test_cb_arg {
-    uint16_t exp_conn_handle;
-    int called;
-};
-
-static int
-ble_gatt_conn_test_attr_cb(uint16_t conn_handle, uint16_t attr_handle,
-                           uint8_t op, uint16_t offset, struct os_mbuf **om,
-                           void *arg)
-{
-    uint8_t *buf;
-
-    switch (op) {
-    case BLE_ATT_ACCESS_OP_READ:
-        buf = os_mbuf_extend(*om, 1);
-        TEST_ASSERT_FATAL(buf != NULL);
-        *buf = 1;
-        return 0;
-
-    default:
-        return -1;
-    }
-}
-
-static int
-ble_gatt_conn_test_mtu_cb(uint16_t conn_handle,
-                          const struct ble_gatt_error *error,
-                          uint16_t mtu, void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(mtu == 0);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_disc_all_svcs_cb(uint16_t conn_handle,
-                                    const struct ble_gatt_error *error,
-                                    const struct ble_gatt_svc *service,
-                                    void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(service == NULL);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_disc_svc_uuid_cb(uint16_t conn_handle,
-                                    const struct ble_gatt_error *error,
-                                    const struct ble_gatt_svc *service,
-                                    void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(service == NULL);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_find_inc_svcs_cb(uint16_t conn_handle,
-                                    const struct ble_gatt_error *error,
-                                    const struct ble_gatt_svc *service,
-                                    void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(service == NULL);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_disc_all_chrs_cb(uint16_t conn_handle,
-                                    const struct ble_gatt_error *error,
-                                    const struct ble_gatt_chr *chr, void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(chr == NULL);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_disc_chr_uuid_cb(uint16_t conn_handle,
-                                    const struct ble_gatt_error *error,
-                                    const struct ble_gatt_chr *chr, void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(chr == NULL);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_disc_all_dscs_cb(uint16_t conn_handle,
-                                    const struct ble_gatt_error *error,
-                                    uint16_t chr_def_handle,
-                                    const struct ble_gatt_dsc *dsc,
-                                    void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(dsc == NULL);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_read_cb(uint16_t conn_handle,
-                           const struct ble_gatt_error *error,
-                           struct ble_gatt_attr *attr, void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(attr == NULL);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_read_uuid_cb(uint16_t conn_handle,
-                                const struct ble_gatt_error *error,
-                                struct ble_gatt_attr *attr, void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(attr == NULL);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_read_long_cb(uint16_t conn_handle,
-                                const struct ble_gatt_error *error,
-                                struct ble_gatt_attr *attr, void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(attr == NULL);
-
-    cb_arg->called++;
-
-    return 0;
-}
-static int
-ble_gatt_conn_test_read_mult_cb(uint16_t conn_handle,
-                                const struct ble_gatt_error *error,
-                                struct ble_gatt_attr *attr, void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(attr->om == NULL);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_write_cb(uint16_t conn_handle,
-                            const struct ble_gatt_error *error,
-                            struct ble_gatt_attr *attr,
-                            void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(attr != NULL);
-    TEST_ASSERT(attr->handle == BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_write_long_cb(uint16_t conn_handle,
-                                 const struct ble_gatt_error *error,
-                                 struct ble_gatt_attr *attr, void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(attr != NULL);
-    TEST_ASSERT(attr->handle == BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-static int
-ble_gatt_conn_test_write_rel_cb(uint16_t conn_handle,
-                                const struct ble_gatt_error *error,
-                                struct ble_gatt_attr *attrs,
-                                uint8_t num_attrs,
-                                void *arg)
-{
-    struct ble_gatt_conn_test_cb_arg *cb_arg;
-
-    cb_arg = arg;
-
-    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
-    TEST_ASSERT(!cb_arg->called);
-    TEST_ASSERT_FATAL(error != NULL);
-    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(attrs != NULL);
-
-    cb_arg->called++;
-
-    return 0;
-}
-
-TEST_CASE(ble_gatt_conn_test_disconnect)
-{
-    struct ble_gatt_conn_test_cb_arg mtu_arg            = { 0 };
-    struct ble_gatt_conn_test_cb_arg disc_all_svcs_arg  = { 0 };
-    struct ble_gatt_conn_test_cb_arg disc_svc_uuid_arg  = { 0 };
-    struct ble_gatt_conn_test_cb_arg find_inc_svcs_arg  = { 0 };
-    struct ble_gatt_conn_test_cb_arg disc_all_chrs_arg  = { 0 };
-    struct ble_gatt_conn_test_cb_arg disc_chr_uuid_arg  = { 0 };
-    struct ble_gatt_conn_test_cb_arg disc_all_dscs_arg  = { 0 };
-    struct ble_gatt_conn_test_cb_arg read_arg           = { 0 };
-    struct ble_gatt_conn_test_cb_arg read_uuid_arg      = { 0 };
-    struct ble_gatt_conn_test_cb_arg read_long_arg      = { 0 };
-    struct ble_gatt_conn_test_cb_arg read_mult_arg      = { 0 };
-    struct ble_gatt_conn_test_cb_arg write_arg          = { 0 };
-    struct ble_gatt_conn_test_cb_arg write_long_arg     = { 0 };
-    struct ble_gatt_conn_test_cb_arg write_rel_arg      = { 0 };
-    struct ble_gatt_attr attr;
-    uint16_t attr_handle;
-    int rc;
-
-    ble_hs_test_util_init();
-
-    /*** Register an attribute to allow indicatations to be sent. */
-    rc = ble_att_svr_register(BLE_UUID16(0x1212), BLE_ATT_F_READ,
-                              &attr_handle,
-                              ble_gatt_conn_test_attr_cb, NULL);
-    TEST_ASSERT(rc == 0);
-
-    /* Create three connections. */
-    ble_hs_test_util_create_conn(1, ((uint8_t[]){1,2,3,4,5,6,7,8}),
-                                 NULL, NULL);
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-    ble_hs_test_util_create_conn(3, ((uint8_t[]){3,4,5,6,7,8,9,10}),
-                                 NULL, NULL);
-
-    /*** Schedule some GATT procedures. */
-    /* Connection 1. */
-    mtu_arg.exp_conn_handle = 1;
-    ble_gattc_exchange_mtu(1, ble_gatt_conn_test_mtu_cb, &mtu_arg);
-
-    disc_all_svcs_arg.exp_conn_handle = 1;
-    rc = ble_gattc_disc_all_svcs(1, ble_gatt_conn_test_disc_all_svcs_cb,
-                                 &disc_all_svcs_arg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    disc_svc_uuid_arg.exp_conn_handle = 1;
-    rc = ble_gattc_disc_svc_by_uuid(1, BLE_UUID16(0x1111),
-                                    ble_gatt_conn_test_disc_svc_uuid_cb,
-                                    &disc_svc_uuid_arg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    find_inc_svcs_arg.exp_conn_handle = 1;
-    rc = ble_gattc_find_inc_svcs(1, 1, 0xffff,
-                                 ble_gatt_conn_test_find_inc_svcs_cb,
-                                 &find_inc_svcs_arg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    disc_all_chrs_arg.exp_conn_handle = 1;
-    rc = ble_gattc_disc_all_chrs(1, 1, 0xffff,
-                                 ble_gatt_conn_test_disc_all_chrs_cb,
-                                 &disc_all_chrs_arg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Connection 2. */
-    disc_all_dscs_arg.exp_conn_handle = 2;
-    rc = ble_gattc_disc_all_dscs(2, 3, 0xffff,
-                                 ble_gatt_conn_test_disc_all_dscs_cb,
-                                 &disc_all_dscs_arg);
-
-    disc_chr_uuid_arg.exp_conn_handle = 2;
-    rc = ble_gattc_disc_chrs_by_uuid(2, 2, 0xffff, BLE_UUID16(0x2222),
-                                     ble_gatt_conn_test_disc_chr_uuid_cb,
-                                     &disc_chr_uuid_arg);
-
-    read_arg.exp_conn_handle = 2;
-    rc = ble_gattc_read(2, BLE_GATT_BREAK_TEST_READ_ATTR_HANDLE,
-                        ble_gatt_conn_test_read_cb, &read_arg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    read_uuid_arg.exp_conn_handle = 2;
-    rc = ble_gattc_read_by_uuid(2, 1, 0xffff, BLE_UUID16(0x3333),
-                                ble_gatt_conn_test_read_uuid_cb,
-                                &read_uuid_arg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    read_long_arg.exp_conn_handle = 2;
-    rc = ble_gattc_read_long(2, BLE_GATT_BREAK_TEST_READ_ATTR_HANDLE,
-                             ble_gatt_conn_test_read_long_cb, &read_long_arg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Connection 3. */
-    read_mult_arg.exp_conn_handle = 3;
-    rc = ble_gattc_read_mult(3, ((uint16_t[3]){5,6,7}), 3,
-                             ble_gatt_conn_test_read_mult_cb, &read_mult_arg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    write_arg.exp_conn_handle = 3;
-    rc = ble_hs_test_util_gatt_write_flat(
-        3, BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE,
-        ble_gatt_conn_test_write_value, sizeof ble_gatt_conn_test_write_value,
-        ble_gatt_conn_test_write_cb, &write_arg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    write_long_arg.exp_conn_handle = 3;
-    rc = ble_hs_test_util_gatt_write_long_flat(
-        3, BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE,
-        ble_gatt_conn_test_write_value, sizeof ble_gatt_conn_test_write_value,
-        ble_gatt_conn_test_write_long_cb, &write_long_arg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    attr.handle = 8;
-    attr.offset = 0;
-    attr.om = os_msys_get_pkthdr(0, 0);
-    write_rel_arg.exp_conn_handle = 3;
-    rc = ble_gattc_write_reliable(
-        3, &attr, 1, ble_gatt_conn_test_write_rel_cb, &write_rel_arg);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /*** Start the procedures. */
-    ble_hs_test_util_tx_all();
-
-    /*** Break the connections; verify proper callbacks got called. */
-    /* Connection 1. */
-    ble_gattc_connection_broken(1);
-    TEST_ASSERT(mtu_arg.called == 1);
-    TEST_ASSERT(disc_all_svcs_arg.called == 1);
-    TEST_ASSERT(disc_svc_uuid_arg.called == 1);
-    TEST_ASSERT(find_inc_svcs_arg.called == 1);
-    TEST_ASSERT(disc_all_chrs_arg.called == 1);
-    TEST_ASSERT(disc_chr_uuid_arg.called == 0);
-    TEST_ASSERT(disc_all_dscs_arg.called == 0);
-    TEST_ASSERT(read_arg.called == 0);
-    TEST_ASSERT(read_uuid_arg.called == 0);
-    TEST_ASSERT(read_long_arg.called == 0);
-    TEST_ASSERT(read_mult_arg.called == 0);
-    TEST_ASSERT(write_arg.called == 0);
-    TEST_ASSERT(write_long_arg.called == 0);
-    TEST_ASSERT(write_rel_arg.called == 0);
-
-    /* Connection 2. */
-    ble_gattc_connection_broken(2);
-    TEST_ASSERT(mtu_arg.called == 1);
-    TEST_ASSERT(disc_all_svcs_arg.called == 1);
-    TEST_ASSERT(disc_svc_uuid_arg.called == 1);
-    TEST_ASSERT(find_inc_svcs_arg.called == 1);
-    TEST_ASSERT(disc_all_chrs_arg.called == 1);
-    TEST_ASSERT(disc_chr_uuid_arg.called == 1);
-    TEST_ASSERT(disc_all_dscs_arg.called == 1);
-    TEST_ASSERT(read_arg.called == 1);
-    TEST_ASSERT(read_uuid_arg.called == 1);
-    TEST_ASSERT(read_long_arg.called == 1);
-    TEST_ASSERT(read_mult_arg.called == 0);
-    TEST_ASSERT(write_arg.called == 0);
-    TEST_ASSERT(write_long_arg.called == 0);
-    TEST_ASSERT(write_rel_arg.called == 0);
-
-    /* Connection 3. */
-    ble_gattc_connection_broken(3);
-    TEST_ASSERT(mtu_arg.called == 1);
-    TEST_ASSERT(disc_all_svcs_arg.called == 1);
-    TEST_ASSERT(disc_svc_uuid_arg.called == 1);
-    TEST_ASSERT(find_inc_svcs_arg.called == 1);
-    TEST_ASSERT(disc_all_chrs_arg.called == 1);
-    TEST_ASSERT(disc_chr_uuid_arg.called == 1);
-    TEST_ASSERT(disc_all_dscs_arg.called == 1);
-    TEST_ASSERT(read_arg.called == 1);
-    TEST_ASSERT(read_uuid_arg.called == 1);
-    TEST_ASSERT(read_long_arg.called == 1);
-    TEST_ASSERT(read_mult_arg.called == 1);
-    TEST_ASSERT(write_arg.called == 1);
-    TEST_ASSERT(write_long_arg.called == 1);
-    TEST_ASSERT(write_rel_arg.called == 1);
-}
-
-TEST_SUITE(ble_gatt_break_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gatt_conn_test_disconnect();
-}
-
-int
-ble_gatt_conn_test_all(void)
-{
-    ble_gatt_break_suite();
-
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_gatt_disc_c_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_disc_c_test.c b/net/nimble/host/src/test/ble_gatt_disc_c_test.c
deleted file mode 100644
index a4eb67b..0000000
--- a/net/nimble/host/src/test/ble_gatt_disc_c_test.c
+++ /dev/null
@@ -1,547 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include <limits.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "host/ble_hs_test.h"
-#include "host/ble_gatt.h"
-#include "host/ble_uuid.h"
-#include "ble_hs_test_util.h"
-
-struct ble_gatt_disc_c_test_char {
-    uint16_t def_handle;
-    uint16_t val_handle;
-    uint16_t uuid16; /* 0 if not present. */
-    uint8_t properties;
-    uint8_t uuid128[16];
-};
-
-#define BLE_GATT_DISC_C_TEST_MAX_CHARS  256
-static struct ble_gatt_chr
-    ble_gatt_disc_c_test_chars[BLE_GATT_DISC_C_TEST_MAX_CHARS];
-static int ble_gatt_disc_c_test_num_chars;
-static int ble_gatt_disc_c_test_rx_complete;
-
-static void
-ble_gatt_disc_c_test_init(void)
-{
-    ble_hs_test_util_init();
-
-    ble_gatt_disc_c_test_num_chars = 0;
-    ble_gatt_disc_c_test_rx_complete = 0;
-}
-
-static int
-ble_gatt_disc_c_test_misc_rx_rsp_once(
-    uint16_t conn_handle, struct ble_gatt_disc_c_test_char *chars)
-{
-    struct ble_att_read_type_rsp rsp;
-    uint8_t buf[1024];
-    int off;
-    int rc;
-    int i;
-
-    /* Send the pending ATT Read By Type Request. */
-    ble_hs_test_util_tx_all();
-
-    if (chars[0].uuid16 != 0) {
-       rsp.batp_length = BLE_ATT_READ_TYPE_ADATA_BASE_SZ +
-                         BLE_GATT_CHR_DECL_SZ_16;
-    } else {
-       rsp.batp_length = BLE_ATT_READ_TYPE_ADATA_BASE_SZ +
-                         BLE_GATT_CHR_DECL_SZ_128;
-    }
-
-    ble_att_read_type_rsp_write(buf, BLE_ATT_READ_TYPE_RSP_BASE_SZ, &rsp);
-
-    off = BLE_ATT_READ_TYPE_RSP_BASE_SZ;
-    for (i = 0; ; i++) {
-        if (chars[i].def_handle == 0) {
-            /* No more services. */
-            break;
-        }
-
-        /* If the value length is changing, we need a separate response. */
-        if (((chars[i].uuid16 == 0) ^ (chars[0].uuid16 == 0)) != 0) {
-            break;
-        }
-
-        htole16(buf + off, chars[i].def_handle);
-        off += 2;
-
-        buf[off] = chars[i].properties;
-        off++;
-
-        htole16(buf + off, chars[i].val_handle);
-        off += 2;
-
-        if (chars[i].uuid16 != 0) {
-            htole16(buf + off, chars[i].uuid16);
-            off += 2;
-        } else {
-            memcpy(buf + off, chars[i].uuid128, 16);
-            off += 16;
-        }
-    }
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    TEST_ASSERT(rc == 0);
-
-    return i;
-}
-
-static void
-ble_gatt_disc_c_test_misc_rx_rsp(uint16_t conn_handle,
-                                 uint16_t end_handle,
-                                 struct ble_gatt_disc_c_test_char *chars)
-{
-    int count;
-    int idx;
-
-    idx = 0;
-    while (chars[idx].def_handle != 0) {
-        count = ble_gatt_disc_c_test_misc_rx_rsp_once(conn_handle,
-                                                      chars + idx);
-        if (count == 0) {
-            break;
-        }
-        idx += count;
-    }
-
-    if (chars[idx - 1].def_handle != end_handle) {
-        /* Send the pending ATT Request. */
-        ble_hs_test_util_tx_all();
-        ble_hs_test_util_rx_att_err_rsp(conn_handle, BLE_ATT_OP_READ_TYPE_REQ,
-                                        BLE_ATT_ERR_ATTR_NOT_FOUND,
-                                        chars[idx - 1].def_handle);
-    }
-}
-
-static void
-ble_gatt_disc_c_test_misc_verify_chars(struct ble_gatt_disc_c_test_char *chars,
-                                       int stop_after)
-{
-    uint16_t uuid16;
-    int i;
-
-    if (stop_after == 0) {
-        stop_after = INT_MAX;
-    }
-
-    for (i = 0; i < stop_after && chars[i].def_handle != 0; i++) {
-        TEST_ASSERT(chars[i].def_handle ==
-                    ble_gatt_disc_c_test_chars[i].def_handle);
-        TEST_ASSERT(chars[i].val_handle ==
-                    ble_gatt_disc_c_test_chars[i].val_handle);
-        if (chars[i].uuid16 != 0) {
-            uuid16 = ble_uuid_128_to_16(ble_gatt_disc_c_test_chars[i].uuid128);
-            TEST_ASSERT(chars[i].uuid16 == uuid16);
-        } else {
-            TEST_ASSERT(memcmp(chars[i].uuid128,
-                               ble_gatt_disc_c_test_chars[i].uuid128,
-                               16) == 0);
-        }
-    }
-
-    TEST_ASSERT(i == ble_gatt_disc_c_test_num_chars);
-    TEST_ASSERT(ble_gatt_disc_c_test_rx_complete);
-}
-
-static int
-ble_gatt_disc_c_test_misc_cb(uint16_t conn_handle,
-                             const struct ble_gatt_error *error,
-                             const struct ble_gatt_chr *chr, void *arg)
-{
-    struct ble_gatt_chr *dst;
-    int *stop_after;
-
-    TEST_ASSERT(error != NULL);
-    TEST_ASSERT(!ble_gatt_disc_c_test_rx_complete);
-
-    stop_after = arg;
-
-    switch (error->status) {
-    case 0:
-        TEST_ASSERT_FATAL(ble_gatt_disc_c_test_num_chars <
-                          BLE_GATT_DISC_C_TEST_MAX_CHARS);
-
-        dst = ble_gatt_disc_c_test_chars + ble_gatt_disc_c_test_num_chars++;
-        *dst = *chr;
-        break;
-
-    case BLE_HS_EDONE:
-        ble_gatt_disc_c_test_rx_complete = 1;
-        break;
-
-    default:
-        TEST_ASSERT(0);
-        break;
-    }
-
-    if (*stop_after > 0) {
-        (*stop_after)--;
-        if (*stop_after == 0) {
-            ble_gatt_disc_c_test_rx_complete = 1;
-            return 1;
-        }
-    }
-
-    return 0;
-}
-
-static void
-ble_gatt_disc_c_test_misc_all(uint16_t start_handle, uint16_t end_handle,
-                              int stop_after,
-                              struct ble_gatt_disc_c_test_char *chars)
-{
-    int num_left;
-    int rc;
-
-    ble_gatt_disc_c_test_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    num_left = stop_after;
-    rc = ble_gattc_disc_all_chrs(2, start_handle, end_handle,
-                                 ble_gatt_disc_c_test_misc_cb, &num_left);
-    TEST_ASSERT(rc == 0);
-
-    ble_gatt_disc_c_test_misc_rx_rsp(2, end_handle, chars);
-    ble_gatt_disc_c_test_misc_verify_chars(chars, stop_after);
-}
-
-static void
-ble_gatt_disc_c_test_misc_uuid(uint16_t start_handle, uint16_t end_handle,
-                               int stop_after, uint8_t *uuid128,
-                               struct ble_gatt_disc_c_test_char *rsp_chars,
-                               struct ble_gatt_disc_c_test_char *ret_chars)
-{
-    int rc;
-
-    ble_gatt_disc_c_test_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    rc = ble_gattc_disc_chrs_by_uuid(2, start_handle, end_handle,
-                                     uuid128,
-                                     ble_gatt_disc_c_test_misc_cb,
-                                     &stop_after);
-    TEST_ASSERT(rc == 0);
-
-    ble_gatt_disc_c_test_misc_rx_rsp(2, end_handle, rsp_chars);
-    ble_gatt_disc_c_test_misc_verify_chars(ret_chars, 0);
-}
-
-TEST_CASE(ble_gatt_disc_c_test_disc_all)
-{
-    /*** One 16-bit characteristic. */
-    ble_gatt_disc_c_test_misc_all(50, 100, 0,
-                                  (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, { 0 }
-    });
-
-    /*** Two 16-bit characteristics. */
-    ble_gatt_disc_c_test_misc_all(50, 100, 0,
-                                  (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 57,
-            .val_handle = 58,
-            .uuid16 = 0x64ba,
-        }, { 0 }
-    });
-
-    /*** Five 16-bit characteristics. */
-    ble_gatt_disc_c_test_misc_all(50, 100, 0,
-                                  (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 57,
-            .val_handle = 58,
-            .uuid16 = 0x64ba,
-        }, {
-            .def_handle = 59,
-            .val_handle = 60,
-            .uuid16 = 0x5372,
-        }, {
-            .def_handle = 61,
-            .val_handle = 62,
-            .uuid16 = 0xab93,
-        }, {
-            .def_handle = 63,
-            .val_handle = 64,
-            .uuid16 = 0x0023,
-        }, { 0 }
-    });
-
-    /*** Interleaved 16-bit and 128-bit characteristics. */
-    ble_gatt_disc_c_test_misc_all(50, 100, 0,
-                                  (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 83,
-            .val_handle = 84,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 87,
-            .val_handle = 88,
-            .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
-        }, {
-            .def_handle = 91,
-            .val_handle = 92,
-            .uuid16 = 0x0003,
-        }, {
-            .def_handle = 93,
-            .val_handle = 94,
-            .uuid128 = { 1,0,4,0,6,9,17,7,8,43,7,4,12,43,19,35 },
-        }, {
-            .def_handle = 98,
-            .val_handle = 99,
-            .uuid16 = 0xabfa,
-        }, { 0 }
-    });
-
-    /*** Ends with final handle ID. */
-    ble_gatt_disc_c_test_misc_all(50, 100, 0,
-                                  (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 99,
-            .val_handle = 100,
-            .uuid16 = 0x64ba,
-        }, { 0 }
-    });
-
-    /*** Stop after two characteristics. */
-    ble_gatt_disc_c_test_misc_all(50, 100, 2,
-                                  (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 57,
-            .val_handle = 58,
-            .uuid16 = 0x64ba,
-        }, {
-            .def_handle = 59,
-            .val_handle = 60,
-            .uuid16 = 0x5372,
-        }, {
-            .def_handle = 61,
-            .val_handle = 62,
-            .uuid16 = 0xab93,
-        }, {
-            .def_handle = 63,
-            .val_handle = 64,
-            .uuid16 = 0x0023,
-        }, { 0 }
-    });
-}
-
-TEST_CASE(ble_gatt_disc_c_test_disc_uuid)
-{
-    /*** One 16-bit characteristic. */
-    ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x2010),
-        (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, { 0 } },
-        (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, { 0 } }
-    );
-
-    /*** No matching characteristics. */
-    ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x2010),
-        (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x1234,
-        }, { 0 } },
-        (struct ble_gatt_disc_c_test_char[]) {
-        { 0 } }
-    );
-
-    /*** 2/5 16-bit characteristics. */
-    ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x2010),
-        (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 57,
-            .val_handle = 58,
-            .uuid16 = 0x64ba,
-        }, {
-            .def_handle = 59,
-            .val_handle = 60,
-            .uuid16 = 0x5372,
-        }, {
-            .def_handle = 61,
-            .val_handle = 62,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 63,
-            .val_handle = 64,
-            .uuid16 = 0x0023,
-        }, { 0 } },
-        (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 61,
-            .val_handle = 62,
-            .uuid16 = 0x2010,
-        }, { 0 } }
-    );
-
-    /*** Interleaved 16-bit and 128-bit characteristics. */
-    ble_gatt_disc_c_test_misc_uuid(
-        50, 100, 0,
-        ((uint8_t[]){ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }),
-        (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 83,
-            .val_handle = 84,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 87,
-            .val_handle = 88,
-            .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
-        }, {
-            .def_handle = 91,
-            .val_handle = 92,
-            .uuid16 = 0x0003,
-        }, {
-            .def_handle = 93,
-            .val_handle = 94,
-            .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
-        }, {
-            .def_handle = 98,
-            .val_handle = 99,
-            .uuid16 = 0xabfa,
-        }, { 0 } },
-        (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 87,
-            .val_handle = 88,
-            .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
-        }, {
-            .def_handle = 93,
-            .val_handle = 94,
-            .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
-        }, { 0 } }
-    );
-
-    /*** Ends with final handle ID. */
-    ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x64ba),
-        (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 99,
-            .val_handle = 100,
-            .uuid16 = 0x64ba,
-        }, { 0 } },
-        (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 99,
-            .val_handle = 100,
-            .uuid16 = 0x64ba,
-        }, { 0 } }
-    );
-
-    /*** Stop after first characteristic. */
-    ble_gatt_disc_c_test_misc_uuid(50, 100, 1, BLE_UUID16(0x2010),
-        (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 57,
-            .val_handle = 58,
-            .uuid16 = 0x64ba,
-        }, {
-            .def_handle = 59,
-            .val_handle = 60,
-            .uuid16 = 0x5372,
-        }, {
-            .def_handle = 61,
-            .val_handle = 62,
-            .uuid16 = 0x2010,
-        }, {
-            .def_handle = 63,
-            .val_handle = 64,
-            .uuid16 = 0x0023,
-        }, { 0 } },
-        (struct ble_gatt_disc_c_test_char[]) {
-        {
-            .def_handle = 55,
-            .val_handle = 56,
-            .uuid16 = 0x2010,
-        }, { 0 } }
-    );
-}
-
-TEST_SUITE(ble_gatt_disc_c_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gatt_disc_c_test_disc_all();
-    ble_gatt_disc_c_test_disc_uuid();
-}
-
-int
-ble_gatt_disc_c_test_all(void)
-{
-    ble_gatt_disc_c_test_suite();
-
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_gatt_disc_d_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_disc_d_test.c b/net/nimble/host/src/test/ble_gatt_disc_d_test.c
deleted file mode 100644
index 7e021e2..0000000
--- a/net/nimble/host/src/test/ble_gatt_disc_d_test.c
+++ /dev/null
@@ -1,363 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include <limits.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "host/ble_hs_test.h"
-#include "host/ble_gatt.h"
-#include "host/ble_uuid.h"
-#include "ble_hs_test_util.h"
-
-struct ble_gatt_disc_d_test_dsc {
-    uint16_t chr_val_handle; /* 0 if last entry. */
-    uint16_t dsc_handle;
-    uint8_t dsc_uuid128[16];
-};
-
-#define BLE_GATT_DISC_D_TEST_MAX_DSCS  256
-static struct ble_gatt_disc_d_test_dsc
-    ble_gatt_disc_d_test_dscs[BLE_GATT_DISC_D_TEST_MAX_DSCS];
-static int ble_gatt_disc_d_test_num_dscs;
-static int ble_gatt_disc_d_test_rx_complete;
-
-static void
-ble_gatt_disc_d_test_init(void)
-{
-    ble_hs_test_util_init();
-
-    ble_gatt_disc_d_test_num_dscs = 0;
-    ble_gatt_disc_d_test_rx_complete = 0;
-}
-
-static int
-ble_gatt_disc_d_test_misc_rx_rsp_once(
-    uint16_t conn_handle, struct ble_gatt_disc_d_test_dsc *dscs)
-{
-    struct ble_att_find_info_rsp rsp;
-    uint8_t buf[1024];
-    uint16_t uuid16_cur;
-    uint16_t uuid16_0;
-    int off;
-    int rc;
-    int i;
-
-    /* Send the pending ATT Read By Type Request. */
-    ble_hs_test_util_tx_all();
-
-    uuid16_0 = ble_uuid_128_to_16(dscs[0].dsc_uuid128);
-    if (uuid16_0 != 0) {
-        rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT;
-    } else {
-        rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT;
-    }
-
-    ble_att_find_info_rsp_write(buf, BLE_ATT_FIND_INFO_RSP_BASE_SZ, &rsp);
-
-    off = BLE_ATT_FIND_INFO_RSP_BASE_SZ;
-    for (i = 0; ; i++) {
-        if (dscs[i].chr_val_handle == 0) {
-            /* No more descriptors. */
-            break;
-        }
-
-        /* If the value length is changing, we need a separate response. */
-        uuid16_cur = ble_uuid_128_to_16(dscs[i].dsc_uuid128);
-        if (((uuid16_0 == 0) ^ (uuid16_cur == 0)) != 0) {
-            break;
-        }
-
-        htole16(buf + off, dscs[i].dsc_handle);
-        off += 2;
-
-        if (uuid16_cur != 0) {
-            htole16(buf + off, uuid16_cur);
-            off += 2;
-        } else {
-            memcpy(buf + off, dscs[i].dsc_uuid128, 16);
-            off += 16;
-        }
-    }
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    TEST_ASSERT(rc == 0);
-
-    return i;
-}
-
-static void
-ble_gatt_disc_d_test_misc_rx_rsp(uint16_t conn_handle,
-                                 uint16_t end_handle,
-                                 struct ble_gatt_disc_d_test_dsc *dscs)
-{
-    int count;
-    int idx;
-
-    idx = 0;
-    while (dscs[idx].chr_val_handle != 0) {
-        count = ble_gatt_disc_d_test_misc_rx_rsp_once(conn_handle, dscs + idx);
-        if (count == 0) {
-            break;
-        }
-        idx += count;
-    }
-
-    if (dscs[idx - 1].dsc_handle != end_handle) {
-        /* Send the pending ATT Request. */
-        ble_hs_test_util_tx_all();
-        ble_hs_test_util_rx_att_err_rsp(conn_handle, BLE_ATT_OP_FIND_INFO_REQ,
-                                        BLE_ATT_ERR_ATTR_NOT_FOUND,
-                                        end_handle);
-    }
-}
-
-static void
-ble_gatt_disc_d_test_misc_verify_dscs(struct ble_gatt_disc_d_test_dsc *dscs,
-                                      int stop_after)
-{
-    int i;
-
-    if (stop_after == 0) {
-        stop_after = INT_MAX;
-    }
-
-    for (i = 0; i < stop_after && dscs[i].chr_val_handle != 0; i++) {
-        TEST_ASSERT(dscs[i].chr_val_handle ==
-                    ble_gatt_disc_d_test_dscs[i].chr_val_handle);
-        TEST_ASSERT(dscs[i].dsc_handle ==
-                    ble_gatt_disc_d_test_dscs[i].dsc_handle);
-        TEST_ASSERT(memcmp(dscs[i].dsc_uuid128,
-                           ble_gatt_disc_d_test_dscs[i].dsc_uuid128,
-                           16) == 0);
-    }
-
-    TEST_ASSERT(i == ble_gatt_disc_d_test_num_dscs);
-    TEST_ASSERT(ble_gatt_disc_d_test_rx_complete);
-}
-
-static int
-ble_gatt_disc_d_test_misc_cb(uint16_t conn_handle,
-                             const struct ble_gatt_error *error,
-                             uint16_t chr_val_handle,
-                             const struct ble_gatt_dsc *dsc,
-                             void *arg)
-{
-    struct ble_gatt_disc_d_test_dsc *dst;
-    int *stop_after;
-
-    TEST_ASSERT(error != NULL);
-    TEST_ASSERT(!ble_gatt_disc_d_test_rx_complete);
-
-    stop_after = arg;
-
-    switch (error->status) {
-    case 0:
-        TEST_ASSERT_FATAL(ble_gatt_disc_d_test_num_dscs <
-                          BLE_GATT_DISC_D_TEST_MAX_DSCS);
-
-        dst = ble_gatt_disc_d_test_dscs + ble_gatt_disc_d_test_num_dscs++;
-        dst->chr_val_handle = chr_val_handle;
-        dst->dsc_handle = dsc->handle;
-        memcpy(dst->dsc_uuid128, dsc->uuid128, 16);
-        break;
-
-    case BLE_HS_EDONE:
-        ble_gatt_disc_d_test_rx_complete = 1;
-        break;
-
-    default:
-        TEST_ASSERT(0);
-        break;
-    }
-
-    if (*stop_after > 0) {
-        (*stop_after)--;
-        if (*stop_after == 0) {
-            ble_gatt_disc_d_test_rx_complete = 1;
-            return 1;
-        }
-    }
-
-    return 0;
-}
-
-static void
-ble_gatt_disc_d_test_misc_all(uint16_t chr_val_handle, uint16_t end_handle,
-                              int stop_after,
-                              struct ble_gatt_disc_d_test_dsc *dscs)
-{
-    int num_left;
-    int rc;
-
-    ble_gatt_disc_d_test_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    num_left = stop_after;
-    rc = ble_gattc_disc_all_dscs(2, chr_val_handle, end_handle,
-                                 ble_gatt_disc_d_test_misc_cb, &num_left);
-    TEST_ASSERT(rc == 0);
-
-    ble_gatt_disc_d_test_misc_rx_rsp(2, end_handle, dscs);
-    ble_gatt_disc_d_test_misc_verify_dscs(dscs, stop_after);
-}
-
-TEST_CASE(ble_gatt_disc_d_test_1)
-{
-    /*** One 16-bit descriptor. */
-    ble_gatt_disc_d_test_misc_all(5, 10, 0,
-        ((struct ble_gatt_disc_d_test_dsc[]) { {
-            .chr_val_handle = 5,
-            .dsc_handle = 6,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x1234),
-        }, {
-            0
-        } })
-    );
-
-    /*** Two 16-bit descriptors. */
-    ble_gatt_disc_d_test_misc_all(50, 100, 0,
-        ((struct ble_gatt_disc_d_test_dsc[]) { {
-            .chr_val_handle = 50,
-            .dsc_handle = 51,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x1111),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 52,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x2222),
-        }, {
-            0
-        } })
-    );
-
-    /*** Five 16-bit descriptors. */
-    ble_gatt_disc_d_test_misc_all(50, 100, 0,
-        ((struct ble_gatt_disc_d_test_dsc[]) { {
-            .chr_val_handle = 50,
-            .dsc_handle = 51,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x1111),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 52,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x2222),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 53,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x3333),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 54,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x4444),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 55,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x5555),
-        }, {
-            0
-        } })
-    );
-
-    /*** Interleaved 16-bit and 128-bit descriptors. */
-    ble_gatt_disc_d_test_misc_all(50, 100, 0,
-        ((struct ble_gatt_disc_d_test_dsc[]) { {
-            .chr_val_handle = 50,
-            .dsc_handle = 51,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x1111),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 52,
-            .dsc_uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 53,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x3333),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 54,
-            .dsc_uuid128 = { 1,0,4,0,6,9,17,7,8,43,7,4,12,43,19,35 },
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 55,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x5555),
-        }, {
-            0
-        } })
-    );
-
-    /*** Ends with final handle ID. */
-    ble_gatt_disc_d_test_misc_all(50, 52, 0,
-        ((struct ble_gatt_disc_d_test_dsc[]) { {
-            .chr_val_handle = 50,
-            .dsc_handle = 51,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x1111),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 52,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x2222),
-        }, {
-            0
-        } })
-    );
-
-    /*** Stop after two descriptors. */
-    ble_gatt_disc_d_test_misc_all(50, 100, 2,
-        ((struct ble_gatt_disc_d_test_dsc[]) { {
-            .chr_val_handle = 50,
-            .dsc_handle = 51,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x1111),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 52,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x2222),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 53,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x3333),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 54,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x4444),
-        }, {
-            .chr_val_handle = 50,
-            .dsc_handle = 55,
-            .dsc_uuid128 = BLE_UUID16_ARR(0x5555),
-        }, {
-            0
-        } })
-    );
-}
-
-TEST_SUITE(ble_gatt_disc_d_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gatt_disc_d_test_1();
-}
-
-int
-ble_gatt_disc_d_test_all(void)
-{
-    ble_gatt_disc_d_test_suite();
-
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_gatt_disc_s_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_disc_s_test.c b/net/nimble/host/src/test/ble_gatt_disc_s_test.c
deleted file mode 100644
index 2e278d6..0000000
--- a/net/nimble/host/src/test/ble_gatt_disc_s_test.c
+++ /dev/null
@@ -1,406 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "host/ble_hs_test.h"
-#include "host/ble_uuid.h"
-#include "ble_hs_test_util.h"
-
-struct ble_gatt_disc_s_test_svc {
-    uint16_t start_handle;
-    uint16_t end_handle;
-    uint16_t uuid16;
-    uint8_t uuid128[16];
-};
-
-#define BLE_GATT_DISC_S_TEST_MAX_SERVICES  256
-static struct ble_gatt_svc
-    ble_gatt_disc_s_test_svcs[BLE_GATT_DISC_S_TEST_MAX_SERVICES];
-static int ble_gatt_disc_s_test_num_svcs;
-static int ble_gatt_disc_s_test_rx_complete;
-
-static void
-ble_gatt_disc_s_test_init(void)
-{
-    ble_hs_test_util_init();
-    ble_gatt_disc_s_test_num_svcs = 0;
-    ble_gatt_disc_s_test_rx_complete = 0;
-}
-
-static int
-ble_gatt_disc_s_test_misc_svc_length(struct ble_gatt_disc_s_test_svc *service)
-{
-    if (service->uuid16 != 0) {
-        return 6;
-    } else {
-        return 20;
-    }
-}
-
-static int
-ble_gatt_disc_s_test_misc_rx_all_rsp_once(
-    uint16_t conn_handle, struct ble_gatt_disc_s_test_svc *services)
-{
-    struct ble_att_read_group_type_rsp rsp;
-    uint8_t buf[1024];
-    int off;
-    int rc;
-    int i;
-
-    /* Send the pending ATT Read By Group Type Request. */
-    ble_hs_test_util_tx_all();
-
-    rsp.bagp_length = ble_gatt_disc_s_test_misc_svc_length(services);
-    ble_att_read_group_type_rsp_write(buf, BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ,
-                                      &rsp);
-
-    off = BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ;
-    for (i = 0; ; i++) {
-        if (services[i].start_handle == 0) {
-            /* No more services. */
-            break;
-        }
-
-        rc = ble_gatt_disc_s_test_misc_svc_length(services + i);
-        if (rc != rsp.bagp_length) {
-            /* UUID length is changing; Need a separate response. */
-            break;
-        }
-
-        htole16(buf + off, services[i].start_handle);
-        off += 2;
-
-        htole16(buf + off, services[i].end_handle);
-        off += 2;
-
-        if (services[i].uuid16 != 0) {
-            htole16(buf + off, services[i].uuid16);
-            off += 2;
-        } else {
-            memcpy(buf + off, services[i].uuid128, 16);
-            off += 16;
-        }
-    }
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    TEST_ASSERT(rc == 0);
-
-    return i;
-}
-
-static void
-ble_gatt_disc_s_test_misc_rx_all_rsp(
-    uint16_t conn_handle, struct ble_gatt_disc_s_test_svc *services)
-{
-    int count;
-    int idx;
-
-    idx = 0;
-    while (services[idx].start_handle != 0) {
-        count = ble_gatt_disc_s_test_misc_rx_all_rsp_once(conn_handle,
-                                                          services + idx);
-        idx += count;
-    }
-
-    if (services[idx - 1].end_handle != 0xffff) {
-        /* Send the pending ATT Request. */
-        ble_hs_test_util_tx_all();
-        ble_hs_test_util_rx_att_err_rsp(conn_handle,
-                                        BLE_ATT_OP_READ_GROUP_TYPE_REQ,
-                                        BLE_ATT_ERR_ATTR_NOT_FOUND,
-                                        services[idx - 1].start_handle);
-    }
-}
-
-static int
-ble_gatt_disc_s_test_misc_rx_uuid_rsp_once(
-    uint16_t conn_handle, struct ble_gatt_disc_s_test_svc *services)
-{
-    uint8_t buf[1024];
-    int off;
-    int rc;
-    int i;
-
-    /* Send the pending ATT Find By Type Value Request. */
-    ble_hs_test_util_tx_all();
-
-    buf[0] = BLE_ATT_OP_FIND_TYPE_VALUE_RSP;
-    off = BLE_ATT_FIND_TYPE_VALUE_RSP_BASE_SZ;
-    for (i = 0; ; i++) {
-        if (services[i].start_handle == 0) {
-            /* No more services. */
-            break;
-        }
-
-        htole16(buf + off, services[i].start_handle);
-        off += 2;
-
-        htole16(buf + off, services[i].end_handle);
-        off += 2;
-    }
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    TEST_ASSERT(rc == 0);
-
-    return i;
-}
-
-static void
-ble_gatt_disc_s_test_misc_rx_uuid_rsp(
-    uint16_t conn_handle, struct ble_gatt_disc_s_test_svc *services)
-{
-    int count;
-    int idx;
-
-    idx = 0;
-    while (services[idx].start_handle != 0) {
-        count = ble_gatt_disc_s_test_misc_rx_uuid_rsp_once(conn_handle,
-                                                           services + idx);
-        idx += count;
-    }
-
-    if (services[idx - 1].end_handle != 0xffff) {
-        /* Send the pending ATT Request. */
-        ble_hs_test_util_tx_all();
-        ble_hs_test_util_rx_att_err_rsp(conn_handle,
-                                        BLE_ATT_OP_FIND_TYPE_VALUE_REQ,
-                                        BLE_ATT_ERR_ATTR_NOT_FOUND,
-                                        services[idx - 1].start_handle);
-    }
-}
-
-static void
-ble_gatt_disc_s_test_misc_verify_services(
-    struct ble_gatt_disc_s_test_svc *services)
-{
-    uint16_t uuid16;
-    uint8_t *uuid128;
-    int i;
-
-    for (i = 0; services[i].start_handle != 0; i++) {
-        TEST_ASSERT(services[i].start_handle ==
-                    ble_gatt_disc_s_test_svcs[i].start_handle);
-        TEST_ASSERT(services[i].end_handle ==
-                    ble_gatt_disc_s_test_svcs[i].end_handle);
-
-        uuid128 = ble_gatt_disc_s_test_svcs[i].uuid128;
-        uuid16 = ble_uuid_128_to_16(uuid128);
-        if (uuid16 != 0) {
-            TEST_ASSERT(services[i].uuid16 == uuid16);
-        } else {
-            TEST_ASSERT(memcmp(services[i].uuid128, uuid128, 16) == 0);
-        }
-    }
-
-    TEST_ASSERT(i == ble_gatt_disc_s_test_num_svcs);
-    TEST_ASSERT(ble_gatt_disc_s_test_rx_complete);
-}
-
-static int
-ble_gatt_disc_s_test_misc_disc_cb(uint16_t conn_handle,
-                                  const struct ble_gatt_error *error,
-                                  const struct ble_gatt_svc *service,
-                                  void *arg)
-{
-    TEST_ASSERT(error != NULL);
-    TEST_ASSERT(!ble_gatt_disc_s_test_rx_complete);
-
-    switch (error->status) {
-    case 0:
-        TEST_ASSERT(service != NULL);
-        TEST_ASSERT_FATAL(ble_gatt_disc_s_test_num_svcs <
-                          BLE_GATT_DISC_S_TEST_MAX_SERVICES);
-        ble_gatt_disc_s_test_svcs[ble_gatt_disc_s_test_num_svcs++] = *service;
-        break;
-
-    case BLE_HS_EDONE:
-        TEST_ASSERT(service == NULL);
-        ble_gatt_disc_s_test_rx_complete = 1;
-        break;
-
-    default:
-        TEST_ASSERT(0);
-    }
-
-    return 0;
-}
-
-static void
-ble_gatt_disc_s_test_misc_good_all(struct ble_gatt_disc_s_test_svc *services)
-{
-    int rc;
-
-    ble_gatt_disc_s_test_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    rc = ble_gattc_disc_all_svcs(2, ble_gatt_disc_s_test_misc_disc_cb, NULL);
-    TEST_ASSERT(rc == 0);
-
-    ble_gatt_disc_s_test_misc_rx_all_rsp(2, services);
-    ble_gatt_disc_s_test_misc_verify_services(services);
-}
-
-static void
-ble_gatt_disc_s_test_misc_good_uuid(
-    struct ble_gatt_disc_s_test_svc *services)
-{
-    int rc;
-
-    ble_gatt_disc_s_test_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    if (services[0].uuid16 != 0) {
-        rc = ble_uuid_16_to_128(services[0].uuid16, services[0].uuid128);
-        TEST_ASSERT_FATAL(rc == 0);
-    }
-    rc = ble_gattc_disc_svc_by_uuid(2, services[0].uuid128,
-                                    ble_gatt_disc_s_test_misc_disc_cb, NULL);
-    TEST_ASSERT(rc == 0);
-
-    ble_gatt_disc_s_test_misc_rx_uuid_rsp(2, services);
-    ble_gatt_disc_s_test_misc_verify_services(services);
-}
-
-TEST_CASE(ble_gatt_disc_s_test_disc_all)
-{
-    /*** One 128-bit service. */
-    ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 0 }
-    });
-
-    /*** Two 128-bit services. */
-    ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 10, 50, 0,    {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, },
-        { 0 }
-    });
-
-    /*** Five 128-bit services. */
-    ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 10, 50, 0,    {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, },
-        { 80, 120, 0,   {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, },
-        { 123, 678, 0,  {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }, },
-        { 751, 999, 0,  {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }, },
-        { 0 }
-    });
-
-    /*** One 128-bit service, one 16-bit-service. */
-    ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 6, 7, 0x1234 },
-        { 0 }
-    });
-
-    /*** End with handle 0xffff. */
-    ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 7, 0xffff, 0, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, },
-        { 0 }
-    });
-}
-
-TEST_CASE(ble_gatt_disc_s_test_disc_service_uuid)
-{
-    /*** 128-bit service; one entry. */
-    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 0 }
-    });
-
-    /*** 128-bit service; two entries. */
-    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 8, 43, 0,     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 0 }
-    });
-
-    /*** 128-bit service; five entries. */
-    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 8, 43, 0,     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 67, 100, 0,   {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 102, 103, 0,  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 262, 900, 0,  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 0 }
-    });
-
-    /*** 128-bit service; end with handle 0xffff. */
-    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 7, 0xffff, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
-        { 0 }
-    });
-
-    /*** 16-bit service; one entry. */
-    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0x1234 },
-        { 0 }
-    });
-
-    /*** 16-bit service; two entries. */
-    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0x1234 },
-        { 85, 243, 0x1234 },
-        { 0 }
-    });
-
-    /*** 16-bit service; five entries. */
-    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0x1234 },
-        { 85, 243, 0x1234 },
-        { 382, 383, 0x1234 },
-        { 562, 898, 0x1234 },
-        { 902, 984, 0x1234 },
-        { 0 }
-    });
-
-    /*** 16-bit service; end with handle 0xffff. */
-    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
-        { 1, 5, 0x1234 },
-        { 9, 0xffff, 0x1234 },
-        { 0 }
-    });
-}
-
-TEST_SUITE(ble_gatt_disc_s_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gatt_disc_s_test_disc_all();
-    ble_gatt_disc_s_test_disc_service_uuid();
-}
-
-int
-ble_gatt_disc_s_test_all(void)
-{
-    ble_gatt_disc_s_test_suite();
-
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_gatt_find_s_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_find_s_test.c b/net/nimble/host/src/test/ble_gatt_find_s_test.c
deleted file mode 100644
index c3ab93d..0000000
--- a/net/nimble/host/src/test/ble_gatt_find_s_test.c
+++ /dev/null
@@ -1,342 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "host/ble_hs_test.h"
-#include "host/ble_uuid.h"
-#include "ble_hs_test_util.h"
-
-static struct ble_gatt_svc ble_gatt_find_s_test_svcs[256];
-static int ble_gatt_find_s_test_num_svcs;
-static int ble_gatt_find_s_test_proc_complete;
-
-struct ble_gatt_find_s_test_entry {
-    uint16_t inc_handle; /* 0 indicates no more entries. */
-    uint16_t start_handle;
-    uint16_t end_handle;
-    uint8_t uuid128[16];
-};
-
-static void
-ble_gatt_find_s_test_misc_init(void)
-{
-    ble_hs_test_util_init();
-    ble_gatt_find_s_test_num_svcs = 0;
-    ble_gatt_find_s_test_proc_complete = 0;
-}
-
-static int
-ble_gatt_find_s_test_misc_cb(uint16_t conn_handle,
-                             const struct ble_gatt_error *error,
-                             const struct ble_gatt_svc *service,
-                             void *arg)
-{
-    TEST_ASSERT(!ble_gatt_find_s_test_proc_complete);
-    TEST_ASSERT(error != NULL);
-
-    switch (error->status) {
-    case 0:
-        ble_gatt_find_s_test_svcs[ble_gatt_find_s_test_num_svcs++] = *service;
-        break;
-
-    case BLE_HS_EDONE:
-        ble_gatt_find_s_test_proc_complete = 1;
-        break;
-
-    default:
-        TEST_ASSERT(0);
-        break;
-    }
-
-    return 0;
-}
-static int
-ble_gatt_find_s_test_misc_rx_read_type(
-    uint16_t conn_handle, struct ble_gatt_find_s_test_entry *entries)
-{
-    struct ble_att_read_type_rsp rsp;
-    uint16_t uuid16;
-    uint8_t buf[1024];
-    int off;
-    int rc;
-    int i;
-
-    memset(&rsp, 0, sizeof rsp);
-
-    off = BLE_ATT_READ_TYPE_RSP_BASE_SZ;
-    for (i = 0; entries[i].inc_handle != 0; i++) {
-        if (rsp.batp_length == BLE_GATTS_INC_SVC_LEN_NO_UUID + 2) {
-            break;
-        }
-
-        uuid16 = ble_uuid_128_to_16(entries[i].uuid128);
-        if (uuid16 == 0) {
-            if (rsp.batp_length != 0) {
-                break;
-            }
-            rsp.batp_length = BLE_GATTS_INC_SVC_LEN_NO_UUID + 2;
-        } else {
-            rsp.batp_length = BLE_GATTS_INC_SVC_LEN_UUID + 2;
-        }
-
-        TEST_ASSERT_FATAL(off + rsp.batp_length <= sizeof buf);
-
-        htole16(buf + off, entries[i].inc_handle);
-        off += 2;
-
-        htole16(buf + off, entries[i].start_handle);
-        off += 2;
-
-        htole16(buf + off, entries[i].end_handle);
-        off += 2;
-
-        if (uuid16 != 0) {
-            htole16(buf + off, uuid16);
-            off += 2;
-        }
-    }
-
-    if (i == 0) {
-        ble_hs_test_util_rx_att_err_rsp(conn_handle, BLE_ATT_OP_READ_TYPE_REQ,
-                                        BLE_ATT_ERR_ATTR_NOT_FOUND, 0);
-        return 0;
-    }
-
-    ble_att_read_type_rsp_write(buf + 0, BLE_ATT_READ_TYPE_RSP_BASE_SZ, &rsp);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    TEST_ASSERT(rc == 0);
-
-    return i;
-}
-
-static void
-ble_gatt_find_s_test_misc_rx_read(uint16_t conn_handle, uint8_t *uuid128)
-{
-    uint8_t buf[17];
-    int rc;
-
-    buf[0] = BLE_ATT_OP_READ_RSP;
-    memcpy(buf + 1, uuid128, 16);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, 17);
-    TEST_ASSERT(rc == 0);
-}
-
-static void
-ble_gatt_find_s_test_misc_verify_tx_read_type(uint16_t start_handle,
-                                              uint16_t end_handle)
-{
-    struct ble_att_read_type_req req;
-    struct os_mbuf *om;
-    uint16_t uuid16;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    ble_att_read_type_req_parse(om->om_data, om->om_len, &req);
-
-    TEST_ASSERT(req.batq_start_handle == start_handle);
-    TEST_ASSERT(req.batq_end_handle == end_handle);
-    TEST_ASSERT(om->om_len == BLE_ATT_READ_TYPE_REQ_BASE_SZ + 2);
-    uuid16 = le16toh(om->om_data + BLE_ATT_READ_TYPE_REQ_BASE_SZ);
-    TEST_ASSERT(uuid16 == BLE_ATT_UUID_INCLUDE);
-}
-
-static void
-ble_gatt_find_s_test_misc_verify_tx_read(uint16_t handle)
-{
-    struct ble_att_read_req req;
-    struct os_mbuf *om;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    ble_att_read_req_parse(om->om_data, om->om_len, &req);
-
-    TEST_ASSERT(req.barq_handle == handle);
-    TEST_ASSERT(om->om_len == BLE_ATT_READ_REQ_SZ);
-}
-
-static void
-ble_gatt_find_s_test_misc_find_inc(uint16_t conn_handle,
-                                   uint16_t start_handle, uint16_t end_handle,
-                                   struct ble_gatt_find_s_test_entry *entries)
-{
-    struct ble_gatt_svc service;
-    int cur_start;
-    int num_found;
-    int idx;
-    int rc;
-    int i;
-
-    rc = ble_gattc_find_inc_svcs(conn_handle, start_handle, end_handle,
-                                 ble_gatt_find_s_test_misc_cb, &service);
-    TEST_ASSERT(rc == 0);
-
-    cur_start = start_handle;
-    idx = 0;
-    while (1) {
-        ble_gatt_find_s_test_misc_verify_tx_read_type(cur_start, end_handle);
-        num_found = ble_gatt_find_s_test_misc_rx_read_type(conn_handle,
-                                                           entries + idx);
-        if (num_found == 0) {
-            break;
-        }
-
-        if (ble_uuid_128_to_16(entries[idx].uuid128) == 0) {
-            TEST_ASSERT(num_found == 1);
-            ble_gatt_find_s_test_misc_verify_tx_read(
-                entries[idx].start_handle);
-            ble_gatt_find_s_test_misc_rx_read(conn_handle,
-                                              entries[idx].uuid128);
-        }
-
-        idx += num_found;
-        cur_start = entries[idx - 1].inc_handle + 1;
-    }
-    TEST_ASSERT(idx == ble_gatt_find_s_test_num_svcs);
-    TEST_ASSERT(ble_gatt_find_s_test_proc_complete);
-
-    for (i = 0; i < ble_gatt_find_s_test_num_svcs; i++) {
-        TEST_ASSERT(ble_gatt_find_s_test_svcs[i].start_handle ==
-                    entries[i].start_handle);
-        TEST_ASSERT(ble_gatt_find_s_test_svcs[i].end_handle ==
-                    entries[i].end_handle);
-        TEST_ASSERT(memcmp(ble_gatt_find_s_test_svcs[i].uuid128,
-                           entries[i].uuid128, 16) == 0);
-    }
-}
-
-TEST_CASE(ble_gatt_find_s_test_1)
-{
-    /* Two 16-bit UUID services; one response. */
-    ble_gatt_find_s_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-    ble_gatt_find_s_test_misc_find_inc(2, 5, 10,
-        ((struct ble_gatt_find_s_test_entry[]) { {
-            .inc_handle = 6,
-            .start_handle = 35,
-            .end_handle = 49,
-            .uuid128 = BLE_UUID16_ARR(0x5155),
-        }, {
-            .inc_handle = 9,
-            .start_handle = 543,
-            .end_handle = 870,
-            .uuid128 = BLE_UUID16_ARR(0x1122),
-        }, {
-            0,
-        } })
-    );
-
-    /* One 128-bit UUID service; two responses. */
-    ble_gatt_find_s_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-    ble_gatt_find_s_test_misc_find_inc(2, 34, 100,
-        ((struct ble_gatt_find_s_test_entry[]) { {
-            .inc_handle = 36,
-            .start_handle = 403,
-            .end_handle = 859,
-            .uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 },
-        }, {
-            0,
-        } })
-    );
-
-    /* Two 128-bit UUID service; four responses. */
-    ble_gatt_find_s_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-    ble_gatt_find_s_test_misc_find_inc(2, 34, 100,
-        ((struct ble_gatt_find_s_test_entry[]) { {
-            .inc_handle = 36,
-            .start_handle = 403,
-            .end_handle = 859,
-            .uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 },
-        }, {
-            .inc_handle = 39,
-            .start_handle = 900,
-            .end_handle = 932,
-            .uuid128 = { 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 },
-        }, {
-            0,
-        } })
-    );
-
-    /* Two 16-bit UUID; three 128-bit UUID; seven responses. */
-    ble_gatt_find_s_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-    ble_gatt_find_s_test_misc_find_inc(2, 1, 100,
-        ((struct ble_gatt_find_s_test_entry[]) { {
-            .inc_handle = 36,
-            .start_handle = 403,
-            .end_handle = 859,
-            .uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 },
-        }, {
-            .inc_handle = 37,
-            .start_handle = 35,
-            .end_handle = 49,
-            .uuid128 = BLE_UUID16_ARR(0x5155),
-        }, {
-            .inc_handle = 38,
-            .start_handle = 543,
-            .end_handle = 870,
-            .uuid128 = BLE_UUID16_ARR(0x1122),
-        }, {
-            .inc_handle = 39,
-            .start_handle = 900,
-            .end_handle = 932,
-            .uuid128 = { 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 },
-        }, {
-            .inc_handle = 40,
-            .start_handle = 940,
-            .end_handle = 950,
-            .uuid128 = { 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 },
-        }, {
-            0,
-        } })
-    );
-}
-
-TEST_SUITE(ble_gatt_find_s_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gatt_find_s_test_1();
-}
-
-int
-ble_gatt_find_s_test_all(void)
-{
-    ble_gatt_find_s_test_suite();
-
-    return tu_any_failed;
-}


[37/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/test/arch/sim/nffs_test_system_01.c
----------------------------------------------------------------------
diff --git a/fs/nffs/src/test/arch/sim/nffs_test_system_01.c b/fs/nffs/src/test/arch/sim/nffs_test_system_01.c
deleted file mode 100644
index cd30544..0000000
--- a/fs/nffs/src/test/arch/sim/nffs_test_system_01.c
+++ /dev/null
@@ -1,6537 +0,0 @@
-/**
- * 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.
- */
-
-
-/** Generated by makefs.rb */
-
-#include <stddef.h>
-#include "nffs_test_priv.h"
-
-const struct nffs_test_file_desc *nffs_test_system_01 =
-    (struct nffs_test_file_desc[]) {
-{
-.filename = "",
-.is_dir = 1,
-.children = (struct nffs_test_file_desc[]) {
- {
- .filename = "lvl1dir-0000",
- .is_dir = 1,
- .children = (struct nffs_test_file_desc[]) {
-  {
-  .filename = "lvl2dir-0000",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0001",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0002",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0003",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0004",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
- { .filename = NULL, } }
- },
- {
- .filename = "lvl1dir-0001",
- .is_dir = 1,
- .children = (struct nffs_test_file_desc[]) {
-  {
-  .filename = "lvl2dir-0000",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0001",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0002",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0003",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0004",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
- { .filename = NULL, } }
- },
- {
- .filename = "lvl1dir-0002",
- .is_dir = 1,
- .children = (struct nffs_test_file_desc[]) {
-  {
-  .filename = "lvl2dir-0000",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0001",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0002",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0003",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0004",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
- { .filename = NULL, } }
- },
- {
- .filename = "lvl1dir-0003",
- .is_dir = 1,
- .children = (struct nffs_test_file_desc[]) {
-  {
-  .filename = "lvl2dir-0000",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0001",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0002",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0003",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0004",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
- { .filename = NULL, } }
- },
- {
- .filename = "lvl1dir-0004",
- .is_dir = 1,
- .children = (struct nffs_test_file_desc[]) {
-  {
-  .filename = "lvl2dir-0000",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0001",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0002",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0003",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0004",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
- { .filename = NULL, } }
- },
-{ .filename = NULL, } }
-},
-};
-
-const struct nffs_test_file_desc *nffs_test_system_01_rm_1014_mk10 =
-    (struct nffs_test_file_desc[]) {
-{
-.filename = "",
-.is_dir = 1,
-.children = (struct nffs_test_file_desc[]) {
- {
- .filename = "lvl1dir-0000",
- .is_dir = 1,
- },
- {
- .filename = "lvl1dir-0001",
- .is_dir = 1,
- .children = (struct nffs_test_file_desc[]) {
-  {
-  .filename = "lvl2dir-0000",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0001",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0001",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0002",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0003",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-   {
-   .filename = "lvl3dir-0004",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0003",
-    .contents = "3",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0004",
-    .contents = "4",
-    .contents_len = 1,
-    },
-   { .filename = NULL, } }
-   },
-  { .filename = NULL, } }
-  },
-  {
-  .filename = "lvl2dir-0002",
-  .is_dir = 1,
-  .children = (struct nffs_test_file_desc[]) {
-   {
-   .filename = "lvl3dir-0000",
-   .is_dir = 1,
-   .children = (struct nffs_test_file_desc[]) {
-    {
-    .filename = "lvl4file-0000",
-    .contents = "0",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0001",
-    .contents = "1",
-    .contents_len = 1,
-    },
-    {
-    .filename = "lvl4file-0002",
-    .contents = "2",
-    .conte

<TRUNCATED>


[05/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_sm_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_sm_test.c b/net/nimble/host/test/src/ble_sm_test.c
new file mode 100644
index 0000000..980bb72
--- /dev/null
+++ b/net/nimble/host/test/src/ble_sm_test.c
@@ -0,0 +1,678 @@
+/**
+ * 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 <stddef.h>
+#include <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/hci_common.h"
+#include "nimble/nimble_opt.h"
+#include "host/ble_sm.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+#include "ble_sm_test_util.h"
+
+#if NIMBLE_BLE_SM
+
+/*****************************************************************************
+ * $misc                                                                     *
+ *****************************************************************************/
+
+TEST_CASE(ble_sm_test_case_f4)
+{
+	uint8_t u[32] = { 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
+			  0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
+			  0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
+			  0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
+	uint8_t v[32] = { 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
+			  0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
+			  0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
+			  0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
+	uint8_t x[16] = { 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
+			  0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
+	uint8_t z = 0x00;
+	uint8_t exp[16] = { 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
+			    0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
+	uint8_t res[16];
+	int err;
+
+	err = ble_sm_alg_f4(u, v, x, z, res);
+	TEST_ASSERT_FATAL(err == 0);
+    TEST_ASSERT(memcmp(res, exp, 16) == 0);
+}
+
+TEST_CASE(ble_sm_test_case_f5)
+{
+	uint8_t w[32] = { 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
+			  0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
+			  0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
+			  0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
+	uint8_t n1[16] = { 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
+			   0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
+	uint8_t n2[16] = { 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
+			   0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
+    uint8_t a1t = 0x00;
+	uint8_t a1[6] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56 };
+    uint8_t a2t = 0x00;
+    uint8_t a2[6] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7 };
+	uint8_t exp_ltk[16] = { 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05,
+				0x98, 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79,
+				0x86, 0x69 };
+	uint8_t exp_mackey[16] = { 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f,
+				   0xfd, 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1,
+				   0x65, 0x29 };
+	uint8_t mackey[16], ltk[16];
+	int err;
+
+	err = ble_sm_alg_f5(w, n1, n2, a1t, a1, a2t, a2, mackey, ltk);
+	TEST_ASSERT_FATAL(err == 0);
+    TEST_ASSERT(memcmp(mackey, exp_mackey, 16) == 0);
+    TEST_ASSERT(memcmp(ltk, exp_ltk, 16) == 0);
+}
+
+TEST_CASE(ble_sm_test_case_f6)
+{
+	uint8_t w[16] = { 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
+			  0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
+	uint8_t n1[16] = { 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
+			   0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
+	uint8_t n2[16] = { 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
+			   0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
+	uint8_t r[16] = { 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
+			  0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
+	uint8_t io_cap[3] = { 0x02, 0x01, 0x01 };
+    uint8_t a1t = 0x00;
+	uint8_t a1[6] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56 };
+    uint8_t a2t = 0x00;
+    uint8_t a2[6] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7 };
+	uint8_t exp[16] = { 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
+			    0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
+	uint8_t res[16];
+	int err;
+
+	err = ble_sm_alg_f6(w, n1, n2, r, io_cap, a1t, a1, a2t, a2, res);
+	TEST_ASSERT_FATAL(err == 0);
+    TEST_ASSERT(memcmp(res, exp, 16) == 0);
+}
+
+TEST_CASE(ble_sm_test_case_g2)
+{
+	uint8_t u[32] = { 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
+			  0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
+			  0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
+			  0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
+	uint8_t v[32] = { 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
+			  0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
+			  0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
+			  0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
+	uint8_t x[16] = { 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
+			  0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
+	uint8_t y[16] = { 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
+			  0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
+	uint32_t exp_val = 0x2f9ed5ba % 1000000;
+	uint32_t val;
+	int err;
+
+	err = ble_sm_alg_g2(u, v, x, y, &val);
+	TEST_ASSERT_FATAL(err == 0);
+	TEST_ASSERT(val == exp_val);
+}
+
+TEST_CASE(ble_sm_test_case_conn_broken)
+{
+    struct hci_disconn_complete disconn_evt;
+    int rc;
+
+    ble_sm_test_util_init();
+
+    ble_sm_dbg_set_next_pair_rand(((uint8_t[16]){0}));
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[6]){1,2,3,5,6,7}),
+                                 ble_sm_test_util_conn_cb, NULL);
+
+    /* Initiate the pairing procedure. */
+    rc = ble_hs_test_util_security_initiate(2, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
+
+    /* Terminate the connection. */
+    disconn_evt.connection_handle = 2;
+    disconn_evt.status = 0;
+    disconn_evt.reason = BLE_ERR_REM_USER_CONN_TERM;
+    ble_gap_rx_disconn_complete(&disconn_evt);
+
+    /* Verify security callback got called. */
+    TEST_ASSERT(ble_sm_test_gap_status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(!ble_sm_test_sec_state.encrypted);
+    TEST_ASSERT(!ble_sm_test_sec_state.authenticated);
+}
+
+/*****************************************************************************
+ * $peer                                                                     *
+ *****************************************************************************/
+
+TEST_CASE(ble_sm_test_case_peer_fail_inval)
+{
+    /* Invalid role detected before other arguments. */
+    ble_sm_test_util_peer_fail_inval(
+        1,
+        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
+        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
+        ((struct ble_sm_pair_cmd[1]) { {
+            .io_cap = 0x14,
+            .oob_data_flag = 0,
+            .authreq = 0x12,
+            .max_enc_key_size = 20,
+            .init_key_dist = 0x0b,
+            .resp_key_dist = 0x11,
+        } }),
+        ((struct ble_sm_pair_fail[1]) { {
+            .reason = BLE_SM_ERR_CMD_NOT_SUPP,
+        } })
+    );
+
+    /* Invalid IO capabiltiies. */
+    ble_sm_test_util_peer_fail_inval(
+        0,
+        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
+        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
+        ((struct ble_sm_pair_cmd[1]) { {
+            .io_cap = 0x14,
+            .oob_data_flag = 0,
+            .authreq = 0x05,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        } }),
+        ((struct ble_sm_pair_fail[1]) { {
+            .reason = BLE_SM_ERR_INVAL,
+        } })
+    );
+
+    /* Invalid OOB flag. */
+    ble_sm_test_util_peer_fail_inval(
+        0,
+        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
+        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
+        ((struct ble_sm_pair_cmd[1]) { {
+            .io_cap = 0x04,
+            .oob_data_flag = 2,
+            .authreq = 0x05,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        } }),
+        ((struct ble_sm_pair_fail[1]) { {
+            .reason = BLE_SM_ERR_INVAL,
+        } })
+    );
+
+    /* Invalid authreq - reserved bonding flag. */
+    ble_sm_test_util_peer_fail_inval(
+        0,
+        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
+        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
+        ((struct ble_sm_pair_cmd[1]) { {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x2,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        } }),
+        ((struct ble_sm_pair_fail[1]) { {
+            .reason = BLE_SM_ERR_INVAL,
+        } })
+    );
+
+    /* Invalid authreq - reserved other flag. */
+    ble_sm_test_util_peer_fail_inval(
+        0,
+        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
+        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
+        ((struct ble_sm_pair_cmd[1]) { {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x20,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        } }),
+        ((struct ble_sm_pair_fail[1]) { {
+            .reason = BLE_SM_ERR_INVAL,
+        } })
+    );
+
+    /* Invalid key size - too small. */
+    ble_sm_test_util_peer_fail_inval(
+        0,
+        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
+        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
+        ((struct ble_sm_pair_cmd[1]) { {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x5,
+            .max_enc_key_size = 6,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        } }),
+        ((struct ble_sm_pair_fail[1]) { {
+            .reason = BLE_SM_ERR_INVAL,
+        } })
+    );
+
+    /* Invalid key size - too large. */
+    ble_sm_test_util_peer_fail_inval(
+        0,
+        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
+        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
+        ((struct ble_sm_pair_cmd[1]) { {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x5,
+            .max_enc_key_size = 17,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        } }),
+        ((struct ble_sm_pair_fail[1]) { {
+            .reason = BLE_SM_ERR_INVAL,
+        } })
+    );
+
+    /* Invalid init key dist. */
+    ble_sm_test_util_peer_fail_inval(
+        0,
+        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
+        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
+        ((struct ble_sm_pair_cmd[1]) { {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x5,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0x10,
+            .resp_key_dist = 0x07,
+        } }),
+        ((struct ble_sm_pair_fail[1]) { {
+            .reason = BLE_SM_ERR_INVAL,
+        } })
+    );
+
+    /* Invalid resp key dist. */
+    ble_sm_test_util_peer_fail_inval(
+        0,
+        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
+        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
+        ((struct ble_sm_pair_cmd[1]) { {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x5,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x10,
+        } }),
+        ((struct ble_sm_pair_fail[1]) { {
+            .reason = BLE_SM_ERR_INVAL,
+        } })
+    );
+}
+
+TEST_CASE(ble_sm_test_case_peer_lgcy_fail_confirm)
+{
+    ble_sm_test_util_peer_lgcy_fail_confirm(
+        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
+        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
+        ((struct ble_sm_pair_cmd[1]) { {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x05,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        } }),
+        ((struct ble_sm_pair_cmd[1]) { {
+            .io_cap = 3,
+            .oob_data_flag = 0,
+            .authreq = 0,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0,
+            .resp_key_dist = 0,
+        } }),
+        ((struct ble_sm_pair_confirm[1]) { {
+            .value = {
+                0x0a, 0xac, 0xa2, 0xae, 0xa6, 0x98, 0xdc, 0x6d,
+                0x65, 0x84, 0x11, 0x69, 0x47, 0x36, 0x8d, 0xa0,
+            },
+        } }),
+        ((struct ble_sm_pair_confirm[1]) { {
+            .value = {
+                0x45, 0xd2, 0x2c, 0x38, 0xd8, 0x91, 0x4f, 0x19,
+                0xa2, 0xd4, 0xfc, 0x7d, 0xad, 0x37, 0x79, 0xe0
+            },
+        } }),
+        ((struct ble_sm_pair_random[1]) { {
+            .value = {
+                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+            },
+        } }),
+        ((struct ble_sm_pair_random[1]) { {
+            .value = {
+                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            },
+        } }),
+        ((struct ble_sm_pair_fail[1]) { {
+            .reason = BLE_SM_ERR_CONFIRM_MISMATCH,
+        } })
+    );
+}
+
+TEST_CASE(ble_sm_test_case_peer_bonding_bad)
+{
+    ble_sm_test_util_peer_bonding_bad(0x5684, 32);
+    ble_sm_test_util_peer_bonding_bad(54325, 65437);
+}
+
+TEST_CASE(ble_sm_test_case_peer_sec_req_inval)
+{
+    struct ble_sm_pair_fail fail;
+    struct ble_sm_sec_req sec_req;
+    int rc;
+
+    ble_sm_test_util_init();
+
+    ble_sm_dbg_set_next_pair_rand(((uint8_t[16]){0}));
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[6]){1,2,3,5,6,7}),
+                                 ble_sm_test_util_conn_cb,
+                                 NULL);
+
+    /*** We are the slave; reject the security request. */
+    ble_hs_atomic_conn_set_flags(2, BLE_HS_CONN_F_MASTER, 0);
+
+    sec_req.authreq = 0;
+    ble_sm_test_util_rx_sec_req(
+        2, &sec_req, BLE_HS_SM_US_ERR(BLE_SM_ERR_CMD_NOT_SUPP));
+
+    ble_hs_test_util_tx_all();
+
+    fail.reason = BLE_SM_ERR_CMD_NOT_SUPP;
+    ble_sm_test_util_verify_tx_pair_fail(&fail);
+
+    /*** Pairing already in progress; ignore security request. */
+    ble_hs_atomic_conn_set_flags(2, BLE_HS_CONN_F_MASTER, 1);
+    rc = ble_sm_pair_initiate(2);
+    TEST_ASSERT_FATAL(rc == 0);
+    ble_hs_test_util_tx_all();
+    ble_hs_test_util_prev_tx_queue_clear();
+
+    ble_sm_test_util_rx_sec_req(2, &sec_req, BLE_HS_EALREADY);
+    ble_hs_test_util_tx_all();
+    TEST_ASSERT(ble_hs_test_util_prev_tx_queue_sz() == 0);
+}
+
+/*****************************************************************************
+ * $us                                                                       *
+ *****************************************************************************/
+
+TEST_CASE(ble_sm_test_case_us_fail_inval)
+{
+    struct ble_sm_test_params params;
+
+    /* Invalid IO capabiltiies. */
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
+        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
+        .pair_req = (struct ble_sm_pair_cmd) {
+            .io_cap = 3,
+            .oob_data_flag = 0,
+            .authreq = 0,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0,
+            .resp_key_dist = 0,
+        },
+        .pair_rsp = (struct ble_sm_pair_cmd) {
+            .io_cap = 0x14,
+            .oob_data_flag = 0,
+            .authreq = 0x05,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_fail = (struct ble_sm_pair_fail) {
+            .reason = BLE_SM_ERR_INVAL,
+        },
+    };
+    ble_sm_test_util_us_fail_inval(&params);
+
+    /* Invalid OOB flag. */
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
+        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
+        .pair_req = (struct ble_sm_pair_cmd) {
+            .io_cap = 3,
+            .oob_data_flag = 0,
+            .authreq = 0,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0,
+            .resp_key_dist = 0,
+        },
+        .pair_rsp = (struct ble_sm_pair_cmd) {
+            .io_cap = 0x14,
+            .oob_data_flag = 2,
+            .authreq = 0x05,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_fail = (struct ble_sm_pair_fail) {
+            .reason = BLE_SM_ERR_INVAL,
+        },
+    };
+    ble_sm_test_util_us_fail_inval(&params);
+
+    /* Invalid authreq - reserved bonding flag. */
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
+        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
+        .pair_req = (struct ble_sm_pair_cmd) {
+            .io_cap = 3,
+            .oob_data_flag = 0,
+            .authreq = 0,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0,
+            .resp_key_dist = 0,
+        },
+        .pair_rsp = (struct ble_sm_pair_cmd) {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x02,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_fail = (struct ble_sm_pair_fail) {
+            .reason = BLE_SM_ERR_INVAL,
+        },
+    };
+    ble_sm_test_util_us_fail_inval(&params);
+
+    /* Invalid authreq - reserved other flag. */
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
+        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
+        .pair_req = (struct ble_sm_pair_cmd) {
+            .io_cap = 3,
+            .oob_data_flag = 0,
+            .authreq = 0,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0,
+            .resp_key_dist = 0,
+        },
+        .pair_rsp = (struct ble_sm_pair_cmd) {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x20,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_fail = (struct ble_sm_pair_fail) {
+            .reason = BLE_SM_ERR_INVAL,
+        },
+    };
+    ble_sm_test_util_us_fail_inval(&params);
+
+    /* Invalid key size - too small. */
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
+        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
+        .pair_req = (struct ble_sm_pair_cmd) {
+            .io_cap = 3,
+            .oob_data_flag = 0,
+            .authreq = 0,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0,
+            .resp_key_dist = 0,
+        },
+        .pair_rsp = (struct ble_sm_pair_cmd) {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x05,
+            .max_enc_key_size = 6,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_fail = (struct ble_sm_pair_fail) {
+            .reason = BLE_SM_ERR_INVAL,
+        },
+    };
+    ble_sm_test_util_us_fail_inval(&params);
+
+    /* Invalid key size - too large. */
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
+        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
+        .pair_req = (struct ble_sm_pair_cmd) {
+            .io_cap = 3,
+            .oob_data_flag = 0,
+            .authreq = 0,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0,
+            .resp_key_dist = 0,
+        },
+        .pair_rsp = (struct ble_sm_pair_cmd) {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x05,
+            .max_enc_key_size = 17,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_fail = (struct ble_sm_pair_fail) {
+            .reason = BLE_SM_ERR_INVAL,
+        },
+    };
+    ble_sm_test_util_us_fail_inval(&params);
+
+    /* Invalid init key dist. */
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
+        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
+        .pair_req = (struct ble_sm_pair_cmd) {
+            .io_cap = 3,
+            .oob_data_flag = 0,
+            .authreq = 0,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0,
+            .resp_key_dist = 0,
+        },
+        .pair_rsp = (struct ble_sm_pair_cmd) {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x05,
+            .max_enc_key_size = 17,
+            .init_key_dist = 0x10,
+            .resp_key_dist = 0x07,
+        },
+        .pair_fail = (struct ble_sm_pair_fail) {
+            .reason = BLE_SM_ERR_INVAL,
+        },
+    };
+    ble_sm_test_util_us_fail_inval(&params);
+
+    /* Invalid resp key dist. */
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
+        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
+        .pair_req = (struct ble_sm_pair_cmd) {
+            .io_cap = 3,
+            .oob_data_flag = 0,
+            .authreq = 0,
+            .max_enc_key_size = 16,
+            .init_key_dist = 0,
+            .resp_key_dist = 0,
+        },
+        .pair_rsp = (struct ble_sm_pair_cmd) {
+            .io_cap = 0x04,
+            .oob_data_flag = 0,
+            .authreq = 0x05,
+            .max_enc_key_size = 17,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x10,
+        },
+        .pair_fail = (struct ble_sm_pair_fail) {
+            .reason = BLE_SM_ERR_INVAL,
+        },
+    };
+    ble_sm_test_util_us_fail_inval(&params);
+}
+
+TEST_SUITE(ble_sm_gen_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_sm_test_case_f4();
+    ble_sm_test_case_f5();
+    ble_sm_test_case_f6();
+    ble_sm_test_case_g2();
+
+    ble_sm_test_case_peer_fail_inval();
+    ble_sm_test_case_peer_lgcy_fail_confirm();
+    ble_sm_test_case_us_fail_inval();
+    ble_sm_test_case_peer_bonding_bad();
+    ble_sm_test_case_conn_broken();
+    ble_sm_test_case_peer_sec_req_inval();
+}
+#endif
+
+int
+ble_sm_test_all(void)
+{
+#if !NIMBLE_BLE_SM
+    return 0;
+#else
+    ble_sm_gen_test_suite();
+    ble_sm_lgcy_test_suite();
+    ble_sm_sc_test_suite();
+
+    return tu_any_failed;
+#endif
+}


[11/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_gatt_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gatt_conn_test.c b/net/nimble/host/test/src/ble_gatt_conn_test.c
new file mode 100644
index 0000000..be4a46d
--- /dev/null
+++ b/net/nimble/host/test/src/ble_gatt_conn_test.c
@@ -0,0 +1,533 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+
+#define BLE_GATT_BREAK_TEST_READ_ATTR_HANDLE        0x9383
+#define BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE       0x1234
+
+static uint8_t ble_gatt_conn_test_write_value[] = { 1, 3, 64, 21, 6 };
+
+struct ble_gatt_conn_test_cb_arg {
+    uint16_t exp_conn_handle;
+    int called;
+};
+
+static int
+ble_gatt_conn_test_attr_cb(uint16_t conn_handle, uint16_t attr_handle,
+                           uint8_t op, uint16_t offset, struct os_mbuf **om,
+                           void *arg)
+{
+    uint8_t *buf;
+
+    switch (op) {
+    case BLE_ATT_ACCESS_OP_READ:
+        buf = os_mbuf_extend(*om, 1);
+        TEST_ASSERT_FATAL(buf != NULL);
+        *buf = 1;
+        return 0;
+
+    default:
+        return -1;
+    }
+}
+
+static int
+ble_gatt_conn_test_mtu_cb(uint16_t conn_handle,
+                          const struct ble_gatt_error *error,
+                          uint16_t mtu, void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(mtu == 0);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_disc_all_svcs_cb(uint16_t conn_handle,
+                                    const struct ble_gatt_error *error,
+                                    const struct ble_gatt_svc *service,
+                                    void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(service == NULL);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_disc_svc_uuid_cb(uint16_t conn_handle,
+                                    const struct ble_gatt_error *error,
+                                    const struct ble_gatt_svc *service,
+                                    void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(service == NULL);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_find_inc_svcs_cb(uint16_t conn_handle,
+                                    const struct ble_gatt_error *error,
+                                    const struct ble_gatt_svc *service,
+                                    void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(service == NULL);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_disc_all_chrs_cb(uint16_t conn_handle,
+                                    const struct ble_gatt_error *error,
+                                    const struct ble_gatt_chr *chr, void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(chr == NULL);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_disc_chr_uuid_cb(uint16_t conn_handle,
+                                    const struct ble_gatt_error *error,
+                                    const struct ble_gatt_chr *chr, void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(chr == NULL);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_disc_all_dscs_cb(uint16_t conn_handle,
+                                    const struct ble_gatt_error *error,
+                                    uint16_t chr_def_handle,
+                                    const struct ble_gatt_dsc *dsc,
+                                    void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(dsc == NULL);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_read_cb(uint16_t conn_handle,
+                           const struct ble_gatt_error *error,
+                           struct ble_gatt_attr *attr, void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(attr == NULL);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_read_uuid_cb(uint16_t conn_handle,
+                                const struct ble_gatt_error *error,
+                                struct ble_gatt_attr *attr, void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(attr == NULL);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_read_long_cb(uint16_t conn_handle,
+                                const struct ble_gatt_error *error,
+                                struct ble_gatt_attr *attr, void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(attr == NULL);
+
+    cb_arg->called++;
+
+    return 0;
+}
+static int
+ble_gatt_conn_test_read_mult_cb(uint16_t conn_handle,
+                                const struct ble_gatt_error *error,
+                                struct ble_gatt_attr *attr, void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(attr->om == NULL);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_write_cb(uint16_t conn_handle,
+                            const struct ble_gatt_error *error,
+                            struct ble_gatt_attr *attr,
+                            void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(attr != NULL);
+    TEST_ASSERT(attr->handle == BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_write_long_cb(uint16_t conn_handle,
+                                 const struct ble_gatt_error *error,
+                                 struct ble_gatt_attr *attr, void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(attr != NULL);
+    TEST_ASSERT(attr->handle == BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+static int
+ble_gatt_conn_test_write_rel_cb(uint16_t conn_handle,
+                                const struct ble_gatt_error *error,
+                                struct ble_gatt_attr *attrs,
+                                uint8_t num_attrs,
+                                void *arg)
+{
+    struct ble_gatt_conn_test_cb_arg *cb_arg;
+
+    cb_arg = arg;
+
+    TEST_ASSERT(cb_arg->exp_conn_handle == conn_handle);
+    TEST_ASSERT(!cb_arg->called);
+    TEST_ASSERT_FATAL(error != NULL);
+    TEST_ASSERT(error->status == BLE_HS_ENOTCONN);
+    TEST_ASSERT(attrs != NULL);
+
+    cb_arg->called++;
+
+    return 0;
+}
+
+TEST_CASE(ble_gatt_conn_test_disconnect)
+{
+    struct ble_gatt_conn_test_cb_arg mtu_arg            = { 0 };
+    struct ble_gatt_conn_test_cb_arg disc_all_svcs_arg  = { 0 };
+    struct ble_gatt_conn_test_cb_arg disc_svc_uuid_arg  = { 0 };
+    struct ble_gatt_conn_test_cb_arg find_inc_svcs_arg  = { 0 };
+    struct ble_gatt_conn_test_cb_arg disc_all_chrs_arg  = { 0 };
+    struct ble_gatt_conn_test_cb_arg disc_chr_uuid_arg  = { 0 };
+    struct ble_gatt_conn_test_cb_arg disc_all_dscs_arg  = { 0 };
+    struct ble_gatt_conn_test_cb_arg read_arg           = { 0 };
+    struct ble_gatt_conn_test_cb_arg read_uuid_arg      = { 0 };
+    struct ble_gatt_conn_test_cb_arg read_long_arg      = { 0 };
+    struct ble_gatt_conn_test_cb_arg read_mult_arg      = { 0 };
+    struct ble_gatt_conn_test_cb_arg write_arg          = { 0 };
+    struct ble_gatt_conn_test_cb_arg write_long_arg     = { 0 };
+    struct ble_gatt_conn_test_cb_arg write_rel_arg      = { 0 };
+    struct ble_gatt_attr attr;
+    uint16_t attr_handle;
+    int rc;
+
+    ble_hs_test_util_init();
+
+    /*** Register an attribute to allow indicatations to be sent. */
+    rc = ble_att_svr_register(BLE_UUID16(0x1212), BLE_ATT_F_READ,
+                              &attr_handle,
+                              ble_gatt_conn_test_attr_cb, NULL);
+    TEST_ASSERT(rc == 0);
+
+    /* Create three connections. */
+    ble_hs_test_util_create_conn(1, ((uint8_t[]){1,2,3,4,5,6,7,8}),
+                                 NULL, NULL);
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+    ble_hs_test_util_create_conn(3, ((uint8_t[]){3,4,5,6,7,8,9,10}),
+                                 NULL, NULL);
+
+    /*** Schedule some GATT procedures. */
+    /* Connection 1. */
+    mtu_arg.exp_conn_handle = 1;
+    ble_gattc_exchange_mtu(1, ble_gatt_conn_test_mtu_cb, &mtu_arg);
+
+    disc_all_svcs_arg.exp_conn_handle = 1;
+    rc = ble_gattc_disc_all_svcs(1, ble_gatt_conn_test_disc_all_svcs_cb,
+                                 &disc_all_svcs_arg);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    disc_svc_uuid_arg.exp_conn_handle = 1;
+    rc = ble_gattc_disc_svc_by_uuid(1, BLE_UUID16(0x1111),
+                                    ble_gatt_conn_test_disc_svc_uuid_cb,
+                                    &disc_svc_uuid_arg);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    find_inc_svcs_arg.exp_conn_handle = 1;
+    rc = ble_gattc_find_inc_svcs(1, 1, 0xffff,
+                                 ble_gatt_conn_test_find_inc_svcs_cb,
+                                 &find_inc_svcs_arg);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    disc_all_chrs_arg.exp_conn_handle = 1;
+    rc = ble_gattc_disc_all_chrs(1, 1, 0xffff,
+                                 ble_gatt_conn_test_disc_all_chrs_cb,
+                                 &disc_all_chrs_arg);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Connection 2. */
+    disc_all_dscs_arg.exp_conn_handle = 2;
+    rc = ble_gattc_disc_all_dscs(2, 3, 0xffff,
+                                 ble_gatt_conn_test_disc_all_dscs_cb,
+                                 &disc_all_dscs_arg);
+
+    disc_chr_uuid_arg.exp_conn_handle = 2;
+    rc = ble_gattc_disc_chrs_by_uuid(2, 2, 0xffff, BLE_UUID16(0x2222),
+                                     ble_gatt_conn_test_disc_chr_uuid_cb,
+                                     &disc_chr_uuid_arg);
+
+    read_arg.exp_conn_handle = 2;
+    rc = ble_gattc_read(2, BLE_GATT_BREAK_TEST_READ_ATTR_HANDLE,
+                        ble_gatt_conn_test_read_cb, &read_arg);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    read_uuid_arg.exp_conn_handle = 2;
+    rc = ble_gattc_read_by_uuid(2, 1, 0xffff, BLE_UUID16(0x3333),
+                                ble_gatt_conn_test_read_uuid_cb,
+                                &read_uuid_arg);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    read_long_arg.exp_conn_handle = 2;
+    rc = ble_gattc_read_long(2, BLE_GATT_BREAK_TEST_READ_ATTR_HANDLE,
+                             ble_gatt_conn_test_read_long_cb, &read_long_arg);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Connection 3. */
+    read_mult_arg.exp_conn_handle = 3;
+    rc = ble_gattc_read_mult(3, ((uint16_t[3]){5,6,7}), 3,
+                             ble_gatt_conn_test_read_mult_cb, &read_mult_arg);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    write_arg.exp_conn_handle = 3;
+    rc = ble_hs_test_util_gatt_write_flat(
+        3, BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE,
+        ble_gatt_conn_test_write_value, sizeof ble_gatt_conn_test_write_value,
+        ble_gatt_conn_test_write_cb, &write_arg);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    write_long_arg.exp_conn_handle = 3;
+    rc = ble_hs_test_util_gatt_write_long_flat(
+        3, BLE_GATT_BREAK_TEST_WRITE_ATTR_HANDLE,
+        ble_gatt_conn_test_write_value, sizeof ble_gatt_conn_test_write_value,
+        ble_gatt_conn_test_write_long_cb, &write_long_arg);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    attr.handle = 8;
+    attr.offset = 0;
+    attr.om = os_msys_get_pkthdr(0, 0);
+    write_rel_arg.exp_conn_handle = 3;
+    rc = ble_gattc_write_reliable(
+        3, &attr, 1, ble_gatt_conn_test_write_rel_cb, &write_rel_arg);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /*** Start the procedures. */
+    ble_hs_test_util_tx_all();
+
+    /*** Break the connections; verify proper callbacks got called. */
+    /* Connection 1. */
+    ble_gattc_connection_broken(1);
+    TEST_ASSERT(mtu_arg.called == 1);
+    TEST_ASSERT(disc_all_svcs_arg.called == 1);
+    TEST_ASSERT(disc_svc_uuid_arg.called == 1);
+    TEST_ASSERT(find_inc_svcs_arg.called == 1);
+    TEST_ASSERT(disc_all_chrs_arg.called == 1);
+    TEST_ASSERT(disc_chr_uuid_arg.called == 0);
+    TEST_ASSERT(disc_all_dscs_arg.called == 0);
+    TEST_ASSERT(read_arg.called == 0);
+    TEST_ASSERT(read_uuid_arg.called == 0);
+    TEST_ASSERT(read_long_arg.called == 0);
+    TEST_ASSERT(read_mult_arg.called == 0);
+    TEST_ASSERT(write_arg.called == 0);
+    TEST_ASSERT(write_long_arg.called == 0);
+    TEST_ASSERT(write_rel_arg.called == 0);
+
+    /* Connection 2. */
+    ble_gattc_connection_broken(2);
+    TEST_ASSERT(mtu_arg.called == 1);
+    TEST_ASSERT(disc_all_svcs_arg.called == 1);
+    TEST_ASSERT(disc_svc_uuid_arg.called == 1);
+    TEST_ASSERT(find_inc_svcs_arg.called == 1);
+    TEST_ASSERT(disc_all_chrs_arg.called == 1);
+    TEST_ASSERT(disc_chr_uuid_arg.called == 1);
+    TEST_ASSERT(disc_all_dscs_arg.called == 1);
+    TEST_ASSERT(read_arg.called == 1);
+    TEST_ASSERT(read_uuid_arg.called == 1);
+    TEST_ASSERT(read_long_arg.called == 1);
+    TEST_ASSERT(read_mult_arg.called == 0);
+    TEST_ASSERT(write_arg.called == 0);
+    TEST_ASSERT(write_long_arg.called == 0);
+    TEST_ASSERT(write_rel_arg.called == 0);
+
+    /* Connection 3. */
+    ble_gattc_connection_broken(3);
+    TEST_ASSERT(mtu_arg.called == 1);
+    TEST_ASSERT(disc_all_svcs_arg.called == 1);
+    TEST_ASSERT(disc_svc_uuid_arg.called == 1);
+    TEST_ASSERT(find_inc_svcs_arg.called == 1);
+    TEST_ASSERT(disc_all_chrs_arg.called == 1);
+    TEST_ASSERT(disc_chr_uuid_arg.called == 1);
+    TEST_ASSERT(disc_all_dscs_arg.called == 1);
+    TEST_ASSERT(read_arg.called == 1);
+    TEST_ASSERT(read_uuid_arg.called == 1);
+    TEST_ASSERT(read_long_arg.called == 1);
+    TEST_ASSERT(read_mult_arg.called == 1);
+    TEST_ASSERT(write_arg.called == 1);
+    TEST_ASSERT(write_long_arg.called == 1);
+    TEST_ASSERT(write_rel_arg.called == 1);
+}
+
+TEST_SUITE(ble_gatt_break_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gatt_conn_test_disconnect();
+}
+
+int
+ble_gatt_conn_test_all(void)
+{
+    ble_gatt_break_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_gatt_disc_c_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gatt_disc_c_test.c b/net/nimble/host/test/src/ble_gatt_disc_c_test.c
new file mode 100644
index 0000000..a4eb67b
--- /dev/null
+++ b/net/nimble/host/test/src/ble_gatt_disc_c_test.c
@@ -0,0 +1,547 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include <limits.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "host/ble_hs_test.h"
+#include "host/ble_gatt.h"
+#include "host/ble_uuid.h"
+#include "ble_hs_test_util.h"
+
+struct ble_gatt_disc_c_test_char {
+    uint16_t def_handle;
+    uint16_t val_handle;
+    uint16_t uuid16; /* 0 if not present. */
+    uint8_t properties;
+    uint8_t uuid128[16];
+};
+
+#define BLE_GATT_DISC_C_TEST_MAX_CHARS  256
+static struct ble_gatt_chr
+    ble_gatt_disc_c_test_chars[BLE_GATT_DISC_C_TEST_MAX_CHARS];
+static int ble_gatt_disc_c_test_num_chars;
+static int ble_gatt_disc_c_test_rx_complete;
+
+static void
+ble_gatt_disc_c_test_init(void)
+{
+    ble_hs_test_util_init();
+
+    ble_gatt_disc_c_test_num_chars = 0;
+    ble_gatt_disc_c_test_rx_complete = 0;
+}
+
+static int
+ble_gatt_disc_c_test_misc_rx_rsp_once(
+    uint16_t conn_handle, struct ble_gatt_disc_c_test_char *chars)
+{
+    struct ble_att_read_type_rsp rsp;
+    uint8_t buf[1024];
+    int off;
+    int rc;
+    int i;
+
+    /* Send the pending ATT Read By Type Request. */
+    ble_hs_test_util_tx_all();
+
+    if (chars[0].uuid16 != 0) {
+       rsp.batp_length = BLE_ATT_READ_TYPE_ADATA_BASE_SZ +
+                         BLE_GATT_CHR_DECL_SZ_16;
+    } else {
+       rsp.batp_length = BLE_ATT_READ_TYPE_ADATA_BASE_SZ +
+                         BLE_GATT_CHR_DECL_SZ_128;
+    }
+
+    ble_att_read_type_rsp_write(buf, BLE_ATT_READ_TYPE_RSP_BASE_SZ, &rsp);
+
+    off = BLE_ATT_READ_TYPE_RSP_BASE_SZ;
+    for (i = 0; ; i++) {
+        if (chars[i].def_handle == 0) {
+            /* No more services. */
+            break;
+        }
+
+        /* If the value length is changing, we need a separate response. */
+        if (((chars[i].uuid16 == 0) ^ (chars[0].uuid16 == 0)) != 0) {
+            break;
+        }
+
+        htole16(buf + off, chars[i].def_handle);
+        off += 2;
+
+        buf[off] = chars[i].properties;
+        off++;
+
+        htole16(buf + off, chars[i].val_handle);
+        off += 2;
+
+        if (chars[i].uuid16 != 0) {
+            htole16(buf + off, chars[i].uuid16);
+            off += 2;
+        } else {
+            memcpy(buf + off, chars[i].uuid128, 16);
+            off += 16;
+        }
+    }
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    TEST_ASSERT(rc == 0);
+
+    return i;
+}
+
+static void
+ble_gatt_disc_c_test_misc_rx_rsp(uint16_t conn_handle,
+                                 uint16_t end_handle,
+                                 struct ble_gatt_disc_c_test_char *chars)
+{
+    int count;
+    int idx;
+
+    idx = 0;
+    while (chars[idx].def_handle != 0) {
+        count = ble_gatt_disc_c_test_misc_rx_rsp_once(conn_handle,
+                                                      chars + idx);
+        if (count == 0) {
+            break;
+        }
+        idx += count;
+    }
+
+    if (chars[idx - 1].def_handle != end_handle) {
+        /* Send the pending ATT Request. */
+        ble_hs_test_util_tx_all();
+        ble_hs_test_util_rx_att_err_rsp(conn_handle, BLE_ATT_OP_READ_TYPE_REQ,
+                                        BLE_ATT_ERR_ATTR_NOT_FOUND,
+                                        chars[idx - 1].def_handle);
+    }
+}
+
+static void
+ble_gatt_disc_c_test_misc_verify_chars(struct ble_gatt_disc_c_test_char *chars,
+                                       int stop_after)
+{
+    uint16_t uuid16;
+    int i;
+
+    if (stop_after == 0) {
+        stop_after = INT_MAX;
+    }
+
+    for (i = 0; i < stop_after && chars[i].def_handle != 0; i++) {
+        TEST_ASSERT(chars[i].def_handle ==
+                    ble_gatt_disc_c_test_chars[i].def_handle);
+        TEST_ASSERT(chars[i].val_handle ==
+                    ble_gatt_disc_c_test_chars[i].val_handle);
+        if (chars[i].uuid16 != 0) {
+            uuid16 = ble_uuid_128_to_16(ble_gatt_disc_c_test_chars[i].uuid128);
+            TEST_ASSERT(chars[i].uuid16 == uuid16);
+        } else {
+            TEST_ASSERT(memcmp(chars[i].uuid128,
+                               ble_gatt_disc_c_test_chars[i].uuid128,
+                               16) == 0);
+        }
+    }
+
+    TEST_ASSERT(i == ble_gatt_disc_c_test_num_chars);
+    TEST_ASSERT(ble_gatt_disc_c_test_rx_complete);
+}
+
+static int
+ble_gatt_disc_c_test_misc_cb(uint16_t conn_handle,
+                             const struct ble_gatt_error *error,
+                             const struct ble_gatt_chr *chr, void *arg)
+{
+    struct ble_gatt_chr *dst;
+    int *stop_after;
+
+    TEST_ASSERT(error != NULL);
+    TEST_ASSERT(!ble_gatt_disc_c_test_rx_complete);
+
+    stop_after = arg;
+
+    switch (error->status) {
+    case 0:
+        TEST_ASSERT_FATAL(ble_gatt_disc_c_test_num_chars <
+                          BLE_GATT_DISC_C_TEST_MAX_CHARS);
+
+        dst = ble_gatt_disc_c_test_chars + ble_gatt_disc_c_test_num_chars++;
+        *dst = *chr;
+        break;
+
+    case BLE_HS_EDONE:
+        ble_gatt_disc_c_test_rx_complete = 1;
+        break;
+
+    default:
+        TEST_ASSERT(0);
+        break;
+    }
+
+    if (*stop_after > 0) {
+        (*stop_after)--;
+        if (*stop_after == 0) {
+            ble_gatt_disc_c_test_rx_complete = 1;
+            return 1;
+        }
+    }
+
+    return 0;
+}
+
+static void
+ble_gatt_disc_c_test_misc_all(uint16_t start_handle, uint16_t end_handle,
+                              int stop_after,
+                              struct ble_gatt_disc_c_test_char *chars)
+{
+    int num_left;
+    int rc;
+
+    ble_gatt_disc_c_test_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    num_left = stop_after;
+    rc = ble_gattc_disc_all_chrs(2, start_handle, end_handle,
+                                 ble_gatt_disc_c_test_misc_cb, &num_left);
+    TEST_ASSERT(rc == 0);
+
+    ble_gatt_disc_c_test_misc_rx_rsp(2, end_handle, chars);
+    ble_gatt_disc_c_test_misc_verify_chars(chars, stop_after);
+}
+
+static void
+ble_gatt_disc_c_test_misc_uuid(uint16_t start_handle, uint16_t end_handle,
+                               int stop_after, uint8_t *uuid128,
+                               struct ble_gatt_disc_c_test_char *rsp_chars,
+                               struct ble_gatt_disc_c_test_char *ret_chars)
+{
+    int rc;
+
+    ble_gatt_disc_c_test_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    rc = ble_gattc_disc_chrs_by_uuid(2, start_handle, end_handle,
+                                     uuid128,
+                                     ble_gatt_disc_c_test_misc_cb,
+                                     &stop_after);
+    TEST_ASSERT(rc == 0);
+
+    ble_gatt_disc_c_test_misc_rx_rsp(2, end_handle, rsp_chars);
+    ble_gatt_disc_c_test_misc_verify_chars(ret_chars, 0);
+}
+
+TEST_CASE(ble_gatt_disc_c_test_disc_all)
+{
+    /*** One 16-bit characteristic. */
+    ble_gatt_disc_c_test_misc_all(50, 100, 0,
+                                  (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, { 0 }
+    });
+
+    /*** Two 16-bit characteristics. */
+    ble_gatt_disc_c_test_misc_all(50, 100, 0,
+                                  (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 57,
+            .val_handle = 58,
+            .uuid16 = 0x64ba,
+        }, { 0 }
+    });
+
+    /*** Five 16-bit characteristics. */
+    ble_gatt_disc_c_test_misc_all(50, 100, 0,
+                                  (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 57,
+            .val_handle = 58,
+            .uuid16 = 0x64ba,
+        }, {
+            .def_handle = 59,
+            .val_handle = 60,
+            .uuid16 = 0x5372,
+        }, {
+            .def_handle = 61,
+            .val_handle = 62,
+            .uuid16 = 0xab93,
+        }, {
+            .def_handle = 63,
+            .val_handle = 64,
+            .uuid16 = 0x0023,
+        }, { 0 }
+    });
+
+    /*** Interleaved 16-bit and 128-bit characteristics. */
+    ble_gatt_disc_c_test_misc_all(50, 100, 0,
+                                  (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 83,
+            .val_handle = 84,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 87,
+            .val_handle = 88,
+            .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
+        }, {
+            .def_handle = 91,
+            .val_handle = 92,
+            .uuid16 = 0x0003,
+        }, {
+            .def_handle = 93,
+            .val_handle = 94,
+            .uuid128 = { 1,0,4,0,6,9,17,7,8,43,7,4,12,43,19,35 },
+        }, {
+            .def_handle = 98,
+            .val_handle = 99,
+            .uuid16 = 0xabfa,
+        }, { 0 }
+    });
+
+    /*** Ends with final handle ID. */
+    ble_gatt_disc_c_test_misc_all(50, 100, 0,
+                                  (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 99,
+            .val_handle = 100,
+            .uuid16 = 0x64ba,
+        }, { 0 }
+    });
+
+    /*** Stop after two characteristics. */
+    ble_gatt_disc_c_test_misc_all(50, 100, 2,
+                                  (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 57,
+            .val_handle = 58,
+            .uuid16 = 0x64ba,
+        }, {
+            .def_handle = 59,
+            .val_handle = 60,
+            .uuid16 = 0x5372,
+        }, {
+            .def_handle = 61,
+            .val_handle = 62,
+            .uuid16 = 0xab93,
+        }, {
+            .def_handle = 63,
+            .val_handle = 64,
+            .uuid16 = 0x0023,
+        }, { 0 }
+    });
+}
+
+TEST_CASE(ble_gatt_disc_c_test_disc_uuid)
+{
+    /*** One 16-bit characteristic. */
+    ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x2010),
+        (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, { 0 } },
+        (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, { 0 } }
+    );
+
+    /*** No matching characteristics. */
+    ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x2010),
+        (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x1234,
+        }, { 0 } },
+        (struct ble_gatt_disc_c_test_char[]) {
+        { 0 } }
+    );
+
+    /*** 2/5 16-bit characteristics. */
+    ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x2010),
+        (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 57,
+            .val_handle = 58,
+            .uuid16 = 0x64ba,
+        }, {
+            .def_handle = 59,
+            .val_handle = 60,
+            .uuid16 = 0x5372,
+        }, {
+            .def_handle = 61,
+            .val_handle = 62,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 63,
+            .val_handle = 64,
+            .uuid16 = 0x0023,
+        }, { 0 } },
+        (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 61,
+            .val_handle = 62,
+            .uuid16 = 0x2010,
+        }, { 0 } }
+    );
+
+    /*** Interleaved 16-bit and 128-bit characteristics. */
+    ble_gatt_disc_c_test_misc_uuid(
+        50, 100, 0,
+        ((uint8_t[]){ 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 }),
+        (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 83,
+            .val_handle = 84,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 87,
+            .val_handle = 88,
+            .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
+        }, {
+            .def_handle = 91,
+            .val_handle = 92,
+            .uuid16 = 0x0003,
+        }, {
+            .def_handle = 93,
+            .val_handle = 94,
+            .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
+        }, {
+            .def_handle = 98,
+            .val_handle = 99,
+            .uuid16 = 0xabfa,
+        }, { 0 } },
+        (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 87,
+            .val_handle = 88,
+            .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
+        }, {
+            .def_handle = 93,
+            .val_handle = 94,
+            .uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
+        }, { 0 } }
+    );
+
+    /*** Ends with final handle ID. */
+    ble_gatt_disc_c_test_misc_uuid(50, 100, 0, BLE_UUID16(0x64ba),
+        (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 99,
+            .val_handle = 100,
+            .uuid16 = 0x64ba,
+        }, { 0 } },
+        (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 99,
+            .val_handle = 100,
+            .uuid16 = 0x64ba,
+        }, { 0 } }
+    );
+
+    /*** Stop after first characteristic. */
+    ble_gatt_disc_c_test_misc_uuid(50, 100, 1, BLE_UUID16(0x2010),
+        (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 57,
+            .val_handle = 58,
+            .uuid16 = 0x64ba,
+        }, {
+            .def_handle = 59,
+            .val_handle = 60,
+            .uuid16 = 0x5372,
+        }, {
+            .def_handle = 61,
+            .val_handle = 62,
+            .uuid16 = 0x2010,
+        }, {
+            .def_handle = 63,
+            .val_handle = 64,
+            .uuid16 = 0x0023,
+        }, { 0 } },
+        (struct ble_gatt_disc_c_test_char[]) {
+        {
+            .def_handle = 55,
+            .val_handle = 56,
+            .uuid16 = 0x2010,
+        }, { 0 } }
+    );
+}
+
+TEST_SUITE(ble_gatt_disc_c_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gatt_disc_c_test_disc_all();
+    ble_gatt_disc_c_test_disc_uuid();
+}
+
+int
+ble_gatt_disc_c_test_all(void)
+{
+    ble_gatt_disc_c_test_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_gatt_disc_d_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gatt_disc_d_test.c b/net/nimble/host/test/src/ble_gatt_disc_d_test.c
new file mode 100644
index 0000000..7e021e2
--- /dev/null
+++ b/net/nimble/host/test/src/ble_gatt_disc_d_test.c
@@ -0,0 +1,363 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include <limits.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "host/ble_hs_test.h"
+#include "host/ble_gatt.h"
+#include "host/ble_uuid.h"
+#include "ble_hs_test_util.h"
+
+struct ble_gatt_disc_d_test_dsc {
+    uint16_t chr_val_handle; /* 0 if last entry. */
+    uint16_t dsc_handle;
+    uint8_t dsc_uuid128[16];
+};
+
+#define BLE_GATT_DISC_D_TEST_MAX_DSCS  256
+static struct ble_gatt_disc_d_test_dsc
+    ble_gatt_disc_d_test_dscs[BLE_GATT_DISC_D_TEST_MAX_DSCS];
+static int ble_gatt_disc_d_test_num_dscs;
+static int ble_gatt_disc_d_test_rx_complete;
+
+static void
+ble_gatt_disc_d_test_init(void)
+{
+    ble_hs_test_util_init();
+
+    ble_gatt_disc_d_test_num_dscs = 0;
+    ble_gatt_disc_d_test_rx_complete = 0;
+}
+
+static int
+ble_gatt_disc_d_test_misc_rx_rsp_once(
+    uint16_t conn_handle, struct ble_gatt_disc_d_test_dsc *dscs)
+{
+    struct ble_att_find_info_rsp rsp;
+    uint8_t buf[1024];
+    uint16_t uuid16_cur;
+    uint16_t uuid16_0;
+    int off;
+    int rc;
+    int i;
+
+    /* Send the pending ATT Read By Type Request. */
+    ble_hs_test_util_tx_all();
+
+    uuid16_0 = ble_uuid_128_to_16(dscs[0].dsc_uuid128);
+    if (uuid16_0 != 0) {
+        rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_16BIT;
+    } else {
+        rsp.bafp_format = BLE_ATT_FIND_INFO_RSP_FORMAT_128BIT;
+    }
+
+    ble_att_find_info_rsp_write(buf, BLE_ATT_FIND_INFO_RSP_BASE_SZ, &rsp);
+
+    off = BLE_ATT_FIND_INFO_RSP_BASE_SZ;
+    for (i = 0; ; i++) {
+        if (dscs[i].chr_val_handle == 0) {
+            /* No more descriptors. */
+            break;
+        }
+
+        /* If the value length is changing, we need a separate response. */
+        uuid16_cur = ble_uuid_128_to_16(dscs[i].dsc_uuid128);
+        if (((uuid16_0 == 0) ^ (uuid16_cur == 0)) != 0) {
+            break;
+        }
+
+        htole16(buf + off, dscs[i].dsc_handle);
+        off += 2;
+
+        if (uuid16_cur != 0) {
+            htole16(buf + off, uuid16_cur);
+            off += 2;
+        } else {
+            memcpy(buf + off, dscs[i].dsc_uuid128, 16);
+            off += 16;
+        }
+    }
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    TEST_ASSERT(rc == 0);
+
+    return i;
+}
+
+static void
+ble_gatt_disc_d_test_misc_rx_rsp(uint16_t conn_handle,
+                                 uint16_t end_handle,
+                                 struct ble_gatt_disc_d_test_dsc *dscs)
+{
+    int count;
+    int idx;
+
+    idx = 0;
+    while (dscs[idx].chr_val_handle != 0) {
+        count = ble_gatt_disc_d_test_misc_rx_rsp_once(conn_handle, dscs + idx);
+        if (count == 0) {
+            break;
+        }
+        idx += count;
+    }
+
+    if (dscs[idx - 1].dsc_handle != end_handle) {
+        /* Send the pending ATT Request. */
+        ble_hs_test_util_tx_all();
+        ble_hs_test_util_rx_att_err_rsp(conn_handle, BLE_ATT_OP_FIND_INFO_REQ,
+                                        BLE_ATT_ERR_ATTR_NOT_FOUND,
+                                        end_handle);
+    }
+}
+
+static void
+ble_gatt_disc_d_test_misc_verify_dscs(struct ble_gatt_disc_d_test_dsc *dscs,
+                                      int stop_after)
+{
+    int i;
+
+    if (stop_after == 0) {
+        stop_after = INT_MAX;
+    }
+
+    for (i = 0; i < stop_after && dscs[i].chr_val_handle != 0; i++) {
+        TEST_ASSERT(dscs[i].chr_val_handle ==
+                    ble_gatt_disc_d_test_dscs[i].chr_val_handle);
+        TEST_ASSERT(dscs[i].dsc_handle ==
+                    ble_gatt_disc_d_test_dscs[i].dsc_handle);
+        TEST_ASSERT(memcmp(dscs[i].dsc_uuid128,
+                           ble_gatt_disc_d_test_dscs[i].dsc_uuid128,
+                           16) == 0);
+    }
+
+    TEST_ASSERT(i == ble_gatt_disc_d_test_num_dscs);
+    TEST_ASSERT(ble_gatt_disc_d_test_rx_complete);
+}
+
+static int
+ble_gatt_disc_d_test_misc_cb(uint16_t conn_handle,
+                             const struct ble_gatt_error *error,
+                             uint16_t chr_val_handle,
+                             const struct ble_gatt_dsc *dsc,
+                             void *arg)
+{
+    struct ble_gatt_disc_d_test_dsc *dst;
+    int *stop_after;
+
+    TEST_ASSERT(error != NULL);
+    TEST_ASSERT(!ble_gatt_disc_d_test_rx_complete);
+
+    stop_after = arg;
+
+    switch (error->status) {
+    case 0:
+        TEST_ASSERT_FATAL(ble_gatt_disc_d_test_num_dscs <
+                          BLE_GATT_DISC_D_TEST_MAX_DSCS);
+
+        dst = ble_gatt_disc_d_test_dscs + ble_gatt_disc_d_test_num_dscs++;
+        dst->chr_val_handle = chr_val_handle;
+        dst->dsc_handle = dsc->handle;
+        memcpy(dst->dsc_uuid128, dsc->uuid128, 16);
+        break;
+
+    case BLE_HS_EDONE:
+        ble_gatt_disc_d_test_rx_complete = 1;
+        break;
+
+    default:
+        TEST_ASSERT(0);
+        break;
+    }
+
+    if (*stop_after > 0) {
+        (*stop_after)--;
+        if (*stop_after == 0) {
+            ble_gatt_disc_d_test_rx_complete = 1;
+            return 1;
+        }
+    }
+
+    return 0;
+}
+
+static void
+ble_gatt_disc_d_test_misc_all(uint16_t chr_val_handle, uint16_t end_handle,
+                              int stop_after,
+                              struct ble_gatt_disc_d_test_dsc *dscs)
+{
+    int num_left;
+    int rc;
+
+    ble_gatt_disc_d_test_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    num_left = stop_after;
+    rc = ble_gattc_disc_all_dscs(2, chr_val_handle, end_handle,
+                                 ble_gatt_disc_d_test_misc_cb, &num_left);
+    TEST_ASSERT(rc == 0);
+
+    ble_gatt_disc_d_test_misc_rx_rsp(2, end_handle, dscs);
+    ble_gatt_disc_d_test_misc_verify_dscs(dscs, stop_after);
+}
+
+TEST_CASE(ble_gatt_disc_d_test_1)
+{
+    /*** One 16-bit descriptor. */
+    ble_gatt_disc_d_test_misc_all(5, 10, 0,
+        ((struct ble_gatt_disc_d_test_dsc[]) { {
+            .chr_val_handle = 5,
+            .dsc_handle = 6,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x1234),
+        }, {
+            0
+        } })
+    );
+
+    /*** Two 16-bit descriptors. */
+    ble_gatt_disc_d_test_misc_all(50, 100, 0,
+        ((struct ble_gatt_disc_d_test_dsc[]) { {
+            .chr_val_handle = 50,
+            .dsc_handle = 51,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x1111),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 52,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x2222),
+        }, {
+            0
+        } })
+    );
+
+    /*** Five 16-bit descriptors. */
+    ble_gatt_disc_d_test_misc_all(50, 100, 0,
+        ((struct ble_gatt_disc_d_test_dsc[]) { {
+            .chr_val_handle = 50,
+            .dsc_handle = 51,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x1111),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 52,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x2222),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 53,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x3333),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 54,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x4444),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 55,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x5555),
+        }, {
+            0
+        } })
+    );
+
+    /*** Interleaved 16-bit and 128-bit descriptors. */
+    ble_gatt_disc_d_test_misc_all(50, 100, 0,
+        ((struct ble_gatt_disc_d_test_dsc[]) { {
+            .chr_val_handle = 50,
+            .dsc_handle = 51,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x1111),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 52,
+            .dsc_uuid128 = { 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 },
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 53,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x3333),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 54,
+            .dsc_uuid128 = { 1,0,4,0,6,9,17,7,8,43,7,4,12,43,19,35 },
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 55,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x5555),
+        }, {
+            0
+        } })
+    );
+
+    /*** Ends with final handle ID. */
+    ble_gatt_disc_d_test_misc_all(50, 52, 0,
+        ((struct ble_gatt_disc_d_test_dsc[]) { {
+            .chr_val_handle = 50,
+            .dsc_handle = 51,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x1111),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 52,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x2222),
+        }, {
+            0
+        } })
+    );
+
+    /*** Stop after two descriptors. */
+    ble_gatt_disc_d_test_misc_all(50, 100, 2,
+        ((struct ble_gatt_disc_d_test_dsc[]) { {
+            .chr_val_handle = 50,
+            .dsc_handle = 51,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x1111),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 52,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x2222),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 53,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x3333),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 54,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x4444),
+        }, {
+            .chr_val_handle = 50,
+            .dsc_handle = 55,
+            .dsc_uuid128 = BLE_UUID16_ARR(0x5555),
+        }, {
+            0
+        } })
+    );
+}
+
+TEST_SUITE(ble_gatt_disc_d_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gatt_disc_d_test_1();
+}
+
+int
+ble_gatt_disc_d_test_all(void)
+{
+    ble_gatt_disc_d_test_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_gatt_disc_s_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gatt_disc_s_test.c b/net/nimble/host/test/src/ble_gatt_disc_s_test.c
new file mode 100644
index 0000000..2e278d6
--- /dev/null
+++ b/net/nimble/host/test/src/ble_gatt_disc_s_test.c
@@ -0,0 +1,406 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "host/ble_hs_test.h"
+#include "host/ble_uuid.h"
+#include "ble_hs_test_util.h"
+
+struct ble_gatt_disc_s_test_svc {
+    uint16_t start_handle;
+    uint16_t end_handle;
+    uint16_t uuid16;
+    uint8_t uuid128[16];
+};
+
+#define BLE_GATT_DISC_S_TEST_MAX_SERVICES  256
+static struct ble_gatt_svc
+    ble_gatt_disc_s_test_svcs[BLE_GATT_DISC_S_TEST_MAX_SERVICES];
+static int ble_gatt_disc_s_test_num_svcs;
+static int ble_gatt_disc_s_test_rx_complete;
+
+static void
+ble_gatt_disc_s_test_init(void)
+{
+    ble_hs_test_util_init();
+    ble_gatt_disc_s_test_num_svcs = 0;
+    ble_gatt_disc_s_test_rx_complete = 0;
+}
+
+static int
+ble_gatt_disc_s_test_misc_svc_length(struct ble_gatt_disc_s_test_svc *service)
+{
+    if (service->uuid16 != 0) {
+        return 6;
+    } else {
+        return 20;
+    }
+}
+
+static int
+ble_gatt_disc_s_test_misc_rx_all_rsp_once(
+    uint16_t conn_handle, struct ble_gatt_disc_s_test_svc *services)
+{
+    struct ble_att_read_group_type_rsp rsp;
+    uint8_t buf[1024];
+    int off;
+    int rc;
+    int i;
+
+    /* Send the pending ATT Read By Group Type Request. */
+    ble_hs_test_util_tx_all();
+
+    rsp.bagp_length = ble_gatt_disc_s_test_misc_svc_length(services);
+    ble_att_read_group_type_rsp_write(buf, BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ,
+                                      &rsp);
+
+    off = BLE_ATT_READ_GROUP_TYPE_RSP_BASE_SZ;
+    for (i = 0; ; i++) {
+        if (services[i].start_handle == 0) {
+            /* No more services. */
+            break;
+        }
+
+        rc = ble_gatt_disc_s_test_misc_svc_length(services + i);
+        if (rc != rsp.bagp_length) {
+            /* UUID length is changing; Need a separate response. */
+            break;
+        }
+
+        htole16(buf + off, services[i].start_handle);
+        off += 2;
+
+        htole16(buf + off, services[i].end_handle);
+        off += 2;
+
+        if (services[i].uuid16 != 0) {
+            htole16(buf + off, services[i].uuid16);
+            off += 2;
+        } else {
+            memcpy(buf + off, services[i].uuid128, 16);
+            off += 16;
+        }
+    }
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    TEST_ASSERT(rc == 0);
+
+    return i;
+}
+
+static void
+ble_gatt_disc_s_test_misc_rx_all_rsp(
+    uint16_t conn_handle, struct ble_gatt_disc_s_test_svc *services)
+{
+    int count;
+    int idx;
+
+    idx = 0;
+    while (services[idx].start_handle != 0) {
+        count = ble_gatt_disc_s_test_misc_rx_all_rsp_once(conn_handle,
+                                                          services + idx);
+        idx += count;
+    }
+
+    if (services[idx - 1].end_handle != 0xffff) {
+        /* Send the pending ATT Request. */
+        ble_hs_test_util_tx_all();
+        ble_hs_test_util_rx_att_err_rsp(conn_handle,
+                                        BLE_ATT_OP_READ_GROUP_TYPE_REQ,
+                                        BLE_ATT_ERR_ATTR_NOT_FOUND,
+                                        services[idx - 1].start_handle);
+    }
+}
+
+static int
+ble_gatt_disc_s_test_misc_rx_uuid_rsp_once(
+    uint16_t conn_handle, struct ble_gatt_disc_s_test_svc *services)
+{
+    uint8_t buf[1024];
+    int off;
+    int rc;
+    int i;
+
+    /* Send the pending ATT Find By Type Value Request. */
+    ble_hs_test_util_tx_all();
+
+    buf[0] = BLE_ATT_OP_FIND_TYPE_VALUE_RSP;
+    off = BLE_ATT_FIND_TYPE_VALUE_RSP_BASE_SZ;
+    for (i = 0; ; i++) {
+        if (services[i].start_handle == 0) {
+            /* No more services. */
+            break;
+        }
+
+        htole16(buf + off, services[i].start_handle);
+        off += 2;
+
+        htole16(buf + off, services[i].end_handle);
+        off += 2;
+    }
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    TEST_ASSERT(rc == 0);
+
+    return i;
+}
+
+static void
+ble_gatt_disc_s_test_misc_rx_uuid_rsp(
+    uint16_t conn_handle, struct ble_gatt_disc_s_test_svc *services)
+{
+    int count;
+    int idx;
+
+    idx = 0;
+    while (services[idx].start_handle != 0) {
+        count = ble_gatt_disc_s_test_misc_rx_uuid_rsp_once(conn_handle,
+                                                           services + idx);
+        idx += count;
+    }
+
+    if (services[idx - 1].end_handle != 0xffff) {
+        /* Send the pending ATT Request. */
+        ble_hs_test_util_tx_all();
+        ble_hs_test_util_rx_att_err_rsp(conn_handle,
+                                        BLE_ATT_OP_FIND_TYPE_VALUE_REQ,
+                                        BLE_ATT_ERR_ATTR_NOT_FOUND,
+                                        services[idx - 1].start_handle);
+    }
+}
+
+static void
+ble_gatt_disc_s_test_misc_verify_services(
+    struct ble_gatt_disc_s_test_svc *services)
+{
+    uint16_t uuid16;
+    uint8_t *uuid128;
+    int i;
+
+    for (i = 0; services[i].start_handle != 0; i++) {
+        TEST_ASSERT(services[i].start_handle ==
+                    ble_gatt_disc_s_test_svcs[i].start_handle);
+        TEST_ASSERT(services[i].end_handle ==
+                    ble_gatt_disc_s_test_svcs[i].end_handle);
+
+        uuid128 = ble_gatt_disc_s_test_svcs[i].uuid128;
+        uuid16 = ble_uuid_128_to_16(uuid128);
+        if (uuid16 != 0) {
+            TEST_ASSERT(services[i].uuid16 == uuid16);
+        } else {
+            TEST_ASSERT(memcmp(services[i].uuid128, uuid128, 16) == 0);
+        }
+    }
+
+    TEST_ASSERT(i == ble_gatt_disc_s_test_num_svcs);
+    TEST_ASSERT(ble_gatt_disc_s_test_rx_complete);
+}
+
+static int
+ble_gatt_disc_s_test_misc_disc_cb(uint16_t conn_handle,
+                                  const struct ble_gatt_error *error,
+                                  const struct ble_gatt_svc *service,
+                                  void *arg)
+{
+    TEST_ASSERT(error != NULL);
+    TEST_ASSERT(!ble_gatt_disc_s_test_rx_complete);
+
+    switch (error->status) {
+    case 0:
+        TEST_ASSERT(service != NULL);
+        TEST_ASSERT_FATAL(ble_gatt_disc_s_test_num_svcs <
+                          BLE_GATT_DISC_S_TEST_MAX_SERVICES);
+        ble_gatt_disc_s_test_svcs[ble_gatt_disc_s_test_num_svcs++] = *service;
+        break;
+
+    case BLE_HS_EDONE:
+        TEST_ASSERT(service == NULL);
+        ble_gatt_disc_s_test_rx_complete = 1;
+        break;
+
+    default:
+        TEST_ASSERT(0);
+    }
+
+    return 0;
+}
+
+static void
+ble_gatt_disc_s_test_misc_good_all(struct ble_gatt_disc_s_test_svc *services)
+{
+    int rc;
+
+    ble_gatt_disc_s_test_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    rc = ble_gattc_disc_all_svcs(2, ble_gatt_disc_s_test_misc_disc_cb, NULL);
+    TEST_ASSERT(rc == 0);
+
+    ble_gatt_disc_s_test_misc_rx_all_rsp(2, services);
+    ble_gatt_disc_s_test_misc_verify_services(services);
+}
+
+static void
+ble_gatt_disc_s_test_misc_good_uuid(
+    struct ble_gatt_disc_s_test_svc *services)
+{
+    int rc;
+
+    ble_gatt_disc_s_test_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+
+    if (services[0].uuid16 != 0) {
+        rc = ble_uuid_16_to_128(services[0].uuid16, services[0].uuid128);
+        TEST_ASSERT_FATAL(rc == 0);
+    }
+    rc = ble_gattc_disc_svc_by_uuid(2, services[0].uuid128,
+                                    ble_gatt_disc_s_test_misc_disc_cb, NULL);
+    TEST_ASSERT(rc == 0);
+
+    ble_gatt_disc_s_test_misc_rx_uuid_rsp(2, services);
+    ble_gatt_disc_s_test_misc_verify_services(services);
+}
+
+TEST_CASE(ble_gatt_disc_s_test_disc_all)
+{
+    /*** One 128-bit service. */
+    ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 0 }
+    });
+
+    /*** Two 128-bit services. */
+    ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 10, 50, 0,    {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, },
+        { 0 }
+    });
+
+    /*** Five 128-bit services. */
+    ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 10, 50, 0,    {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, },
+        { 80, 120, 0,   {3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }, },
+        { 123, 678, 0,  {4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 }, },
+        { 751, 999, 0,  {5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 }, },
+        { 0 }
+    });
+
+    /*** One 128-bit service, one 16-bit-service. */
+    ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 6, 7, 0x1234 },
+        { 0 }
+    });
+
+    /*** End with handle 0xffff. */
+    ble_gatt_disc_s_test_misc_good_all((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 7, 0xffff, 0, {2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, },
+        { 0 }
+    });
+}
+
+TEST_CASE(ble_gatt_disc_s_test_disc_service_uuid)
+{
+    /*** 128-bit service; one entry. */
+    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 0 }
+    });
+
+    /*** 128-bit service; two entries. */
+    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 8, 43, 0,     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 0 }
+    });
+
+    /*** 128-bit service; five entries. */
+    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 8, 43, 0,     {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 67, 100, 0,   {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 102, 103, 0,  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 262, 900, 0,  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 0 }
+    });
+
+    /*** 128-bit service; end with handle 0xffff. */
+    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0,      {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 7, 0xffff, 0, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, },
+        { 0 }
+    });
+
+    /*** 16-bit service; one entry. */
+    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0x1234 },
+        { 0 }
+    });
+
+    /*** 16-bit service; two entries. */
+    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0x1234 },
+        { 85, 243, 0x1234 },
+        { 0 }
+    });
+
+    /*** 16-bit service; five entries. */
+    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0x1234 },
+        { 85, 243, 0x1234 },
+        { 382, 383, 0x1234 },
+        { 562, 898, 0x1234 },
+        { 902, 984, 0x1234 },
+        { 0 }
+    });
+
+    /*** 16-bit service; end with handle 0xffff. */
+    ble_gatt_disc_s_test_misc_good_uuid((struct ble_gatt_disc_s_test_svc[]) {
+        { 1, 5, 0x1234 },
+        { 9, 0xffff, 0x1234 },
+        { 0 }
+    });
+}
+
+TEST_SUITE(ble_gatt_disc_s_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gatt_disc_s_test_disc_all();
+    ble_gatt_disc_s_test_disc_service_uuid();
+}
+
+int
+ble_gatt_disc_s_test_all(void)
+{
+    ble_gatt_disc_s_test_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_gatt_find_s_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_gatt_find_s_test.c b/net/nimble/host/test/src/ble_gatt_find_s_test.c
new file mode 100644
index 0000000..c3ab93d
--- /dev/null
+++ b/net/nimble/host/test/src/ble_gatt_find_s_test.c
@@ -0,0 +1,342 @@
+/**
+ * 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 <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/ble.h"
+#include "host/ble_hs_test.h"
+#include "host/ble_uuid.h"
+#include "ble_hs_test_util.h"
+
+static struct ble_gatt_svc ble_gatt_find_s_test_svcs[256];
+static int ble_gatt_find_s_test_num_svcs;
+static int ble_gatt_find_s_test_proc_complete;
+
+struct ble_gatt_find_s_test_entry {
+    uint16_t inc_handle; /* 0 indicates no more entries. */
+    uint16_t start_handle;
+    uint16_t end_handle;
+    uint8_t uuid128[16];
+};
+
+static void
+ble_gatt_find_s_test_misc_init(void)
+{
+    ble_hs_test_util_init();
+    ble_gatt_find_s_test_num_svcs = 0;
+    ble_gatt_find_s_test_proc_complete = 0;
+}
+
+static int
+ble_gatt_find_s_test_misc_cb(uint16_t conn_handle,
+                             const struct ble_gatt_error *error,
+                             const struct ble_gatt_svc *service,
+                             void *arg)
+{
+    TEST_ASSERT(!ble_gatt_find_s_test_proc_complete);
+    TEST_ASSERT(error != NULL);
+
+    switch (error->status) {
+    case 0:
+        ble_gatt_find_s_test_svcs[ble_gatt_find_s_test_num_svcs++] = *service;
+        break;
+
+    case BLE_HS_EDONE:
+        ble_gatt_find_s_test_proc_complete = 1;
+        break;
+
+    default:
+        TEST_ASSERT(0);
+        break;
+    }
+
+    return 0;
+}
+static int
+ble_gatt_find_s_test_misc_rx_read_type(
+    uint16_t conn_handle, struct ble_gatt_find_s_test_entry *entries)
+{
+    struct ble_att_read_type_rsp rsp;
+    uint16_t uuid16;
+    uint8_t buf[1024];
+    int off;
+    int rc;
+    int i;
+
+    memset(&rsp, 0, sizeof rsp);
+
+    off = BLE_ATT_READ_TYPE_RSP_BASE_SZ;
+    for (i = 0; entries[i].inc_handle != 0; i++) {
+        if (rsp.batp_length == BLE_GATTS_INC_SVC_LEN_NO_UUID + 2) {
+            break;
+        }
+
+        uuid16 = ble_uuid_128_to_16(entries[i].uuid128);
+        if (uuid16 == 0) {
+            if (rsp.batp_length != 0) {
+                break;
+            }
+            rsp.batp_length = BLE_GATTS_INC_SVC_LEN_NO_UUID + 2;
+        } else {
+            rsp.batp_length = BLE_GATTS_INC_SVC_LEN_UUID + 2;
+        }
+
+        TEST_ASSERT_FATAL(off + rsp.batp_length <= sizeof buf);
+
+        htole16(buf + off, entries[i].inc_handle);
+        off += 2;
+
+        htole16(buf + off, entries[i].start_handle);
+        off += 2;
+
+        htole16(buf + off, entries[i].end_handle);
+        off += 2;
+
+        if (uuid16 != 0) {
+            htole16(buf + off, uuid16);
+            off += 2;
+        }
+    }
+
+    if (i == 0) {
+        ble_hs_test_util_rx_att_err_rsp(conn_handle, BLE_ATT_OP_READ_TYPE_REQ,
+                                        BLE_ATT_ERR_ATTR_NOT_FOUND, 0);
+        return 0;
+    }
+
+    ble_att_read_type_rsp_write(buf + 0, BLE_ATT_READ_TYPE_RSP_BASE_SZ, &rsp);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, off);
+    TEST_ASSERT(rc == 0);
+
+    return i;
+}
+
+static void
+ble_gatt_find_s_test_misc_rx_read(uint16_t conn_handle, uint8_t *uuid128)
+{
+    uint8_t buf[17];
+    int rc;
+
+    buf[0] = BLE_ATT_OP_READ_RSP;
+    memcpy(buf + 1, uuid128, 16);
+
+    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
+                                                buf, 17);
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+ble_gatt_find_s_test_misc_verify_tx_read_type(uint16_t start_handle,
+                                              uint16_t end_handle)
+{
+    struct ble_att_read_type_req req;
+    struct os_mbuf *om;
+    uint16_t uuid16;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    ble_att_read_type_req_parse(om->om_data, om->om_len, &req);
+
+    TEST_ASSERT(req.batq_start_handle == start_handle);
+    TEST_ASSERT(req.batq_end_handle == end_handle);
+    TEST_ASSERT(om->om_len == BLE_ATT_READ_TYPE_REQ_BASE_SZ + 2);
+    uuid16 = le16toh(om->om_data + BLE_ATT_READ_TYPE_REQ_BASE_SZ);
+    TEST_ASSERT(uuid16 == BLE_ATT_UUID_INCLUDE);
+}
+
+static void
+ble_gatt_find_s_test_misc_verify_tx_read(uint16_t handle)
+{
+    struct ble_att_read_req req;
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    ble_att_read_req_parse(om->om_data, om->om_len, &req);
+
+    TEST_ASSERT(req.barq_handle == handle);
+    TEST_ASSERT(om->om_len == BLE_ATT_READ_REQ_SZ);
+}
+
+static void
+ble_gatt_find_s_test_misc_find_inc(uint16_t conn_handle,
+                                   uint16_t start_handle, uint16_t end_handle,
+                                   struct ble_gatt_find_s_test_entry *entries)
+{
+    struct ble_gatt_svc service;
+    int cur_start;
+    int num_found;
+    int idx;
+    int rc;
+    int i;
+
+    rc = ble_gattc_find_inc_svcs(conn_handle, start_handle, end_handle,
+                                 ble_gatt_find_s_test_misc_cb, &service);
+    TEST_ASSERT(rc == 0);
+
+    cur_start = start_handle;
+    idx = 0;
+    while (1) {
+        ble_gatt_find_s_test_misc_verify_tx_read_type(cur_start, end_handle);
+        num_found = ble_gatt_find_s_test_misc_rx_read_type(conn_handle,
+                                                           entries + idx);
+        if (num_found == 0) {
+            break;
+        }
+
+        if (ble_uuid_128_to_16(entries[idx].uuid128) == 0) {
+            TEST_ASSERT(num_found == 1);
+            ble_gatt_find_s_test_misc_verify_tx_read(
+                entries[idx].start_handle);
+            ble_gatt_find_s_test_misc_rx_read(conn_handle,
+                                              entries[idx].uuid128);
+        }
+
+        idx += num_found;
+        cur_start = entries[idx - 1].inc_handle + 1;
+    }
+    TEST_ASSERT(idx == ble_gatt_find_s_test_num_svcs);
+    TEST_ASSERT(ble_gatt_find_s_test_proc_complete);
+
+    for (i = 0; i < ble_gatt_find_s_test_num_svcs; i++) {
+        TEST_ASSERT(ble_gatt_find_s_test_svcs[i].start_handle ==
+                    entries[i].start_handle);
+        TEST_ASSERT(ble_gatt_find_s_test_svcs[i].end_handle ==
+                    entries[i].end_handle);
+        TEST_ASSERT(memcmp(ble_gatt_find_s_test_svcs[i].uuid128,
+                           entries[i].uuid128, 16) == 0);
+    }
+}
+
+TEST_CASE(ble_gatt_find_s_test_1)
+{
+    /* Two 16-bit UUID services; one response. */
+    ble_gatt_find_s_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+    ble_gatt_find_s_test_misc_find_inc(2, 5, 10,
+        ((struct ble_gatt_find_s_test_entry[]) { {
+            .inc_handle = 6,
+            .start_handle = 35,
+            .end_handle = 49,
+            .uuid128 = BLE_UUID16_ARR(0x5155),
+        }, {
+            .inc_handle = 9,
+            .start_handle = 543,
+            .end_handle = 870,
+            .uuid128 = BLE_UUID16_ARR(0x1122),
+        }, {
+            0,
+        } })
+    );
+
+    /* One 128-bit UUID service; two responses. */
+    ble_gatt_find_s_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+    ble_gatt_find_s_test_misc_find_inc(2, 34, 100,
+        ((struct ble_gatt_find_s_test_entry[]) { {
+            .inc_handle = 36,
+            .start_handle = 403,
+            .end_handle = 859,
+            .uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 },
+        }, {
+            0,
+        } })
+    );
+
+    /* Two 128-bit UUID service; four responses. */
+    ble_gatt_find_s_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+    ble_gatt_find_s_test_misc_find_inc(2, 34, 100,
+        ((struct ble_gatt_find_s_test_entry[]) { {
+            .inc_handle = 36,
+            .start_handle = 403,
+            .end_handle = 859,
+            .uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 },
+        }, {
+            .inc_handle = 39,
+            .start_handle = 900,
+            .end_handle = 932,
+            .uuid128 = { 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 },
+        }, {
+            0,
+        } })
+    );
+
+    /* Two 16-bit UUID; three 128-bit UUID; seven responses. */
+    ble_gatt_find_s_test_misc_init();
+    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
+                                 NULL, NULL);
+    ble_gatt_find_s_test_misc_find_inc(2, 1, 100,
+        ((struct ble_gatt_find_s_test_entry[]) { {
+            .inc_handle = 36,
+            .start_handle = 403,
+            .end_handle = 859,
+            .uuid128 = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 },
+        }, {
+            .inc_handle = 37,
+            .start_handle = 35,
+            .end_handle = 49,
+            .uuid128 = BLE_UUID16_ARR(0x5155),
+        }, {
+            .inc_handle = 38,
+            .start_handle = 543,
+            .end_handle = 870,
+            .uuid128 = BLE_UUID16_ARR(0x1122),
+        }, {
+            .inc_handle = 39,
+            .start_handle = 900,
+            .end_handle = 932,
+            .uuid128 = { 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 },
+        }, {
+            .inc_handle = 40,
+            .start_handle = 940,
+            .end_handle = 950,
+            .uuid128 = { 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 },
+        }, {
+            0,
+        } })
+    );
+}
+
+TEST_SUITE(ble_gatt_find_s_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_gatt_find_s_test_1();
+}
+
+int
+ble_gatt_find_s_test_all(void)
+{
+    ble_gatt_find_s_test_suite();
+
+    return tu_any_failed;
+}


[27/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/util/src/test/cbmem_test.c
----------------------------------------------------------------------
diff --git a/libs/util/src/test/cbmem_test.c b/libs/util/src/test/cbmem_test.c
deleted file mode 100644
index b486334..0000000
--- a/libs/util/src/test/cbmem_test.c
+++ /dev/null
@@ -1,176 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <string.h>
-
-#include "testutil/testutil.h"
-#include "util/cbmem.h" 
-
-#define CBMEM1_BUF_SIZE (64 * 1024)
-
-struct cbmem cbmem1;
-uint8_t cbmem1_buf[CBMEM1_BUF_SIZE];
-uint8_t cbmem1_entry[1024];
-
-/*
- * Things to test.
- *
- * - Wrap of the circular buffer.  
- * - Reading through all entries.
- */
-
-static void
-setup_cbmem1(void)
-{
-    int i;
-    int rc;
-
-    rc = cbmem_init(&cbmem1, cbmem1_buf, CBMEM1_BUF_SIZE);
-    TEST_ASSERT_FATAL(rc == 0, "cbmem_init() failed, non-zero RC = %d", rc);
-
-    memset(cbmem1_entry, 0xff, sizeof(cbmem1_entry));
-
-    /* Insert 65 1024 entries, and overflow buffer.  
-     * This should overflow two entries, because the buffer is sized for 64 
-     * entries, and then the headers themselves will eat into one of the entries, 
-     * so there should be a total of 63 entries.
-     * Ensure no data corruption.
-     */
-    for (i = 0; i < 65; i++) {
-        cbmem1_entry[0] = i;
-        rc = cbmem_append(&cbmem1, cbmem1_entry, sizeof(cbmem1_entry));
-        TEST_ASSERT_FATAL(rc == 0, "Could not append entry %d, rc = %d", i, rc);
-    }
-}
-
-static int 
-cbmem_test_case_1_walk(struct cbmem *cbmem, struct cbmem_entry_hdr *hdr, 
-        void *arg)
-{
-    uint8_t expected;
-    uint8_t actual;
-    int rc;
-
-    expected = *(uint8_t *) arg;
-
-    rc = cbmem_read(cbmem, hdr, &actual, 0, sizeof(actual));
-    TEST_ASSERT_FATAL(rc == 1, "Couldn't read 1 byte from cbmem");
-    TEST_ASSERT_FATAL(actual == expected, 
-            "Actual doesn't equal expected (%d = %d)", actual, expected);
-
-    *(uint8_t *) arg = ++expected;
-
-    return (0);
-}
-
-TEST_CASE(cbmem_test_case_1) 
-{
-    int i;
-    int rc;
-
-    /* i starts at 2, for the 2 overwritten entries. */
-    i = 2;
-    rc = cbmem_walk(&cbmem1, cbmem_test_case_1_walk, &i);
-    TEST_ASSERT_FATAL(rc == 0, "Could not walk cbmem tree!  rc = %d", rc);
-    TEST_ASSERT_FATAL(i == 65, 
-            "Did not go through every element of walk, %d processed", i - 2);
-
-}
-
-TEST_CASE(cbmem_test_case_2)
-{
-    struct cbmem_entry_hdr *hdr;
-    struct cbmem_iter iter;
-    uint8_t i;
-    uint8_t val;
-    int rc;
-
-    i = 2;
-    cbmem_iter_start(&cbmem1, &iter);
-    while (1) {
-        hdr = cbmem_iter_next(&cbmem1, &iter);
-        if (hdr == NULL) {
-            break;
-        }
-
-        rc = cbmem_read(&cbmem1, hdr, &val, 0, sizeof(val));
-        TEST_ASSERT_FATAL(rc == 1, "Couldn't read 1 byte from cbmem");
-        TEST_ASSERT_FATAL(val == i, "Entry index does not match %d vs %d", 
-                val, i);
-
-        i++;
-    }
-
-    /* i starts at 2, for the 2 overwritten entries */
-    TEST_ASSERT_FATAL(i == 65, 
-            "Did not iterate through all 63 elements of CBMEM1, processed %d", 
-            i - 2);
-}
-
-TEST_CASE(cbmem_test_case_3)
-{
-    struct cbmem_entry_hdr *hdr;
-    struct cbmem_iter iter;
-    uint16_t off;
-    uint16_t len;
-    uint8_t buf[128];
-    int i;
-    int rc;
-
-    i = 0;
-    cbmem_iter_start(&cbmem1, &iter);
-    while (1) {
-        hdr = cbmem_iter_next(&cbmem1, &iter);
-        if (hdr == NULL) {
-            break;
-        }
-        
-        /* first ensure we can read the entire entry */
-        off = 0;
-        len = 0;
-        while (1) {
-            rc = cbmem_read(&cbmem1, hdr, buf, off, sizeof(buf));
-            TEST_ASSERT_FATAL(rc >= 0,
-                    "Error reading from buffer rc=%d, off=%d,len=%d", rc, off, 
-                    sizeof(buf));
-            if (rc == 0) {
-                break;
-            }
-            off += rc;
-            len += rc;
-        }
-        TEST_ASSERT_FATAL(len == 1024, 
-                "Couldn't read full entry, expected %d got %d", 1024, len);
-        i++;
-
-        /* go apesh*t, and read data out of bounds, see what we get. */
-        rc = cbmem_read(&cbmem1, hdr, buf, 2048, sizeof(buf));
-        TEST_ASSERT_FATAL(rc < 0, 
-                "Reading invalid should return error, instead %d returned.",
-                rc);
-    }
-}
-
-TEST_SUITE(cbmem_test_suite)
-{
-    setup_cbmem1();
-    cbmem_test_case_1();
-    cbmem_test_case_2();
-    cbmem_test_case_3();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/util/src/test/util_test.c
----------------------------------------------------------------------
diff --git a/libs/util/src/test/util_test.c b/libs/util/src/test/util_test.c
deleted file mode 100644
index c236ab5..0000000
--- a/libs/util/src/test/util_test.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/**
- * 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 <assert.h>
-#include <stddef.h>
-#include "testutil/testutil.h"
-#include "util_test_priv.h"
-
-int
-util_test_all(void)
-{
-    cbmem_test_suite();
-    return tu_case_failed;
-}
-
-#ifdef MYNEWT_SELFTEST
-
-int
-main(int argc, char **argv)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    util_test_all();
-
-    return tu_any_failed;
-}
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/util/src/test/util_test_priv.h
----------------------------------------------------------------------
diff --git a/libs/util/src/test/util_test_priv.h b/libs/util/src/test/util_test_priv.h
deleted file mode 100644
index cc5533d..0000000
--- a/libs/util/src/test/util_test_priv.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/**
- * 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 __UTIL_TEST_PRIV_
-#define __UTIL_TEST_PRIV_
-
-int cbmem_test_suite(void);
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/util/test/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/util/test/pkg.yml b/libs/util/test/pkg.yml
new file mode 100644
index 0000000..3a8cf98
--- /dev/null
+++ b/libs/util/test/pkg.yml
@@ -0,0 +1,30 @@
+# 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.
+#
+pkg.name: libs/util/test
+pkg.type: unittest
+pkg.description: "Util unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps: 
+    - libs/testutil
+    - libs/util
+
+pkg.deps.SELFTEST:
+    - libs/console/stub

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/util/test/src/cbmem_test.c
----------------------------------------------------------------------
diff --git a/libs/util/test/src/cbmem_test.c b/libs/util/test/src/cbmem_test.c
new file mode 100644
index 0000000..b486334
--- /dev/null
+++ b/libs/util/test/src/cbmem_test.c
@@ -0,0 +1,176 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+
+#include "testutil/testutil.h"
+#include "util/cbmem.h" 
+
+#define CBMEM1_BUF_SIZE (64 * 1024)
+
+struct cbmem cbmem1;
+uint8_t cbmem1_buf[CBMEM1_BUF_SIZE];
+uint8_t cbmem1_entry[1024];
+
+/*
+ * Things to test.
+ *
+ * - Wrap of the circular buffer.  
+ * - Reading through all entries.
+ */
+
+static void
+setup_cbmem1(void)
+{
+    int i;
+    int rc;
+
+    rc = cbmem_init(&cbmem1, cbmem1_buf, CBMEM1_BUF_SIZE);
+    TEST_ASSERT_FATAL(rc == 0, "cbmem_init() failed, non-zero RC = %d", rc);
+
+    memset(cbmem1_entry, 0xff, sizeof(cbmem1_entry));
+
+    /* Insert 65 1024 entries, and overflow buffer.  
+     * This should overflow two entries, because the buffer is sized for 64 
+     * entries, and then the headers themselves will eat into one of the entries, 
+     * so there should be a total of 63 entries.
+     * Ensure no data corruption.
+     */
+    for (i = 0; i < 65; i++) {
+        cbmem1_entry[0] = i;
+        rc = cbmem_append(&cbmem1, cbmem1_entry, sizeof(cbmem1_entry));
+        TEST_ASSERT_FATAL(rc == 0, "Could not append entry %d, rc = %d", i, rc);
+    }
+}
+
+static int 
+cbmem_test_case_1_walk(struct cbmem *cbmem, struct cbmem_entry_hdr *hdr, 
+        void *arg)
+{
+    uint8_t expected;
+    uint8_t actual;
+    int rc;
+
+    expected = *(uint8_t *) arg;
+
+    rc = cbmem_read(cbmem, hdr, &actual, 0, sizeof(actual));
+    TEST_ASSERT_FATAL(rc == 1, "Couldn't read 1 byte from cbmem");
+    TEST_ASSERT_FATAL(actual == expected, 
+            "Actual doesn't equal expected (%d = %d)", actual, expected);
+
+    *(uint8_t *) arg = ++expected;
+
+    return (0);
+}
+
+TEST_CASE(cbmem_test_case_1) 
+{
+    int i;
+    int rc;
+
+    /* i starts at 2, for the 2 overwritten entries. */
+    i = 2;
+    rc = cbmem_walk(&cbmem1, cbmem_test_case_1_walk, &i);
+    TEST_ASSERT_FATAL(rc == 0, "Could not walk cbmem tree!  rc = %d", rc);
+    TEST_ASSERT_FATAL(i == 65, 
+            "Did not go through every element of walk, %d processed", i - 2);
+
+}
+
+TEST_CASE(cbmem_test_case_2)
+{
+    struct cbmem_entry_hdr *hdr;
+    struct cbmem_iter iter;
+    uint8_t i;
+    uint8_t val;
+    int rc;
+
+    i = 2;
+    cbmem_iter_start(&cbmem1, &iter);
+    while (1) {
+        hdr = cbmem_iter_next(&cbmem1, &iter);
+        if (hdr == NULL) {
+            break;
+        }
+
+        rc = cbmem_read(&cbmem1, hdr, &val, 0, sizeof(val));
+        TEST_ASSERT_FATAL(rc == 1, "Couldn't read 1 byte from cbmem");
+        TEST_ASSERT_FATAL(val == i, "Entry index does not match %d vs %d", 
+                val, i);
+
+        i++;
+    }
+
+    /* i starts at 2, for the 2 overwritten entries */
+    TEST_ASSERT_FATAL(i == 65, 
+            "Did not iterate through all 63 elements of CBMEM1, processed %d", 
+            i - 2);
+}
+
+TEST_CASE(cbmem_test_case_3)
+{
+    struct cbmem_entry_hdr *hdr;
+    struct cbmem_iter iter;
+    uint16_t off;
+    uint16_t len;
+    uint8_t buf[128];
+    int i;
+    int rc;
+
+    i = 0;
+    cbmem_iter_start(&cbmem1, &iter);
+    while (1) {
+        hdr = cbmem_iter_next(&cbmem1, &iter);
+        if (hdr == NULL) {
+            break;
+        }
+        
+        /* first ensure we can read the entire entry */
+        off = 0;
+        len = 0;
+        while (1) {
+            rc = cbmem_read(&cbmem1, hdr, buf, off, sizeof(buf));
+            TEST_ASSERT_FATAL(rc >= 0,
+                    "Error reading from buffer rc=%d, off=%d,len=%d", rc, off, 
+                    sizeof(buf));
+            if (rc == 0) {
+                break;
+            }
+            off += rc;
+            len += rc;
+        }
+        TEST_ASSERT_FATAL(len == 1024, 
+                "Couldn't read full entry, expected %d got %d", 1024, len);
+        i++;
+
+        /* go apesh*t, and read data out of bounds, see what we get. */
+        rc = cbmem_read(&cbmem1, hdr, buf, 2048, sizeof(buf));
+        TEST_ASSERT_FATAL(rc < 0, 
+                "Reading invalid should return error, instead %d returned.",
+                rc);
+    }
+}
+
+TEST_SUITE(cbmem_test_suite)
+{
+    setup_cbmem1();
+    cbmem_test_case_1();
+    cbmem_test_case_2();
+    cbmem_test_case_3();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/util/test/src/util_test.c
----------------------------------------------------------------------
diff --git a/libs/util/test/src/util_test.c b/libs/util/test/src/util_test.c
new file mode 100644
index 0000000..d528568
--- /dev/null
+++ b/libs/util/test/src/util_test.c
@@ -0,0 +1,46 @@
+/**
+ * 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 <assert.h>
+#include <stddef.h>
+#include "syscfg/syscfg.h"
+#include "testutil/testutil.h"
+#include "util_test_priv.h"
+
+int
+util_test_all(void)
+{
+    cbmem_test_suite();
+    return tu_case_failed;
+}
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    util_test_all();
+
+    return tu_any_failed;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/util/test/src/util_test_priv.h
----------------------------------------------------------------------
diff --git a/libs/util/test/src/util_test_priv.h b/libs/util/test/src/util_test_priv.h
new file mode 100644
index 0000000..cc5533d
--- /dev/null
+++ b/libs/util/test/src/util_test_priv.h
@@ -0,0 +1,25 @@
+/**
+ * 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 __UTIL_TEST_PRIV_
+#define __UTIL_TEST_PRIV_
+
+int cbmem_test_suite(void);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/wifi_mgmt/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/wifi_mgmt/pkg.yml b/libs/wifi_mgmt/pkg.yml
index ebe582c..711dc75 100644
--- a/libs/wifi_mgmt/pkg.yml
+++ b/libs/wifi_mgmt/pkg.yml
@@ -25,11 +25,10 @@ pkg.keywords:
 pkg.deps:
     - "@apache-mynewt-core/libs/os"
     - "@apache-mynewt-core/libs/util"
-pkg.reqs:
-    - console
-pkg.cflags.SHELL:
-    - -DSHELL_PRESENT
-
-pkg.deps.TEST:
-   - libs/testutil
+pkg.deps.WIFI_MGMT_CLI:
+    - libs/shell
 
+pkg.syscfg_defs:
+    WIFI_MGMT_CLI:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_LIBS_SHELL'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/wifi_mgmt/src/wifi.c
----------------------------------------------------------------------
diff --git a/libs/wifi_mgmt/src/wifi.c b/libs/wifi_mgmt/src/wifi.c
index b5b6a72..b96a2ed 100644
--- a/libs/wifi_mgmt/src/wifi.c
+++ b/libs/wifi_mgmt/src/wifi.c
@@ -338,7 +338,7 @@ wifi_task(void *arg)
 int
 wifi_task_init(uint8_t prio, os_stack_t *stack, uint16_t stack_size)
 {
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(WIFI_MGMT_CLI)
     shell_cmd_register(&wifi_cli_cmd);
 #endif
     os_eventq_init(&wifi_evq);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/wifi_mgmt/src/wifi_cli.c
----------------------------------------------------------------------
diff --git a/libs/wifi_mgmt/src/wifi_cli.c b/libs/wifi_mgmt/src/wifi_cli.c
index 4cd9552..350418f 100644
--- a/libs/wifi_mgmt/src/wifi_cli.c
+++ b/libs/wifi_mgmt/src/wifi_cli.c
@@ -16,7 +16,11 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifdef SHELL_PRESENT
+
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(WIFI_MGMT_CLI)
+
 #include <stddef.h>
 #include <string.h>
 #include <assert.h>

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/wifi_mgmt/src/wifi_priv.h
----------------------------------------------------------------------
diff --git a/libs/wifi_mgmt/src/wifi_priv.h b/libs/wifi_mgmt/src/wifi_priv.h
index dfba81d..d7fbb1f 100644
--- a/libs/wifi_mgmt/src/wifi_priv.h
+++ b/libs/wifi_mgmt/src/wifi_priv.h
@@ -20,7 +20,9 @@
 #ifndef __WIFI_PRIV_H__
 #define __WIFI_PRIV_H__
 
-#ifdef SHELL_PRESENT
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(WIFI_MGMT_CLI)
 extern struct shell_cmd wifi_cli_cmd;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/include/controller/ble_ll.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll.h b/net/nimble/controller/include/controller/ble_ll.h
index 2be8425..6f48ac6 100644
--- a/net/nimble/controller/include/controller/ble_ll.h
+++ b/net/nimble/controller/include/controller/ble_ll.h
@@ -287,8 +287,7 @@ struct ble_dev_addr
 
 /*--- External API ---*/
 /* Initialize the Link Layer */
-int
-ble_ll_init(uint8_t ll_task_prio, uint8_t num_acl_pkts, uint16_t acl_pkt_size);
+void ble_ll_init(void);
 
 /* Reset the Link Layer */
 int ble_ll_reset(void);
@@ -390,7 +389,7 @@ void ble_ll_log(uint8_t id, uint8_t arg8, uint16_t arg16, uint32_t arg32);
 #define ble_ll_log(m,n,o,p)
 #endif
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /* LTK 0x4C68384139F574D836BCF34E9DFB01BF */
 extern const uint8_t g_bletest_LTK[];
 extern uint16_t g_bletest_EDIV;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/include/controller/ble_ll_conn.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/include/controller/ble_ll_conn.h b/net/nimble/controller/include/controller/ble_ll_conn.h
index 38a3a22..0b15d06 100644
--- a/net/nimble/controller/include/controller/ble_ll_conn.h
+++ b/net/nimble/controller/include/controller/ble_ll_conn.h
@@ -53,7 +53,7 @@
 /* Definition for RSSI when the RSSI is unknown */
 #define BLE_LL_CONN_UNKNOWN_RSSI        (127)
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /*
  * Encryption states for a connection
  *
@@ -232,7 +232,7 @@ struct ble_ll_conn_sm
     /* For scheduling connections */
     struct ble_ll_sched_item conn_sch;
 
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
     struct os_callout_func auth_pyld_timer;
 #endif
 
@@ -244,7 +244,7 @@ struct ble_ll_conn_sm
      * allocate these from a pool? Not sure what to do. For now, I just use
      * a large chunk of memory per connection.
      */
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     struct ble_ll_conn_enc_data enc_data;
 #endif
     /*

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/controller/pkg.yml b/net/nimble/controller/pkg.yml
index 8e52aba..19846bb 100644
--- a/net/nimble/controller/pkg.yml
+++ b/net/nimble/controller/pkg.yml
@@ -34,5 +34,171 @@ pkg.deps:
     - sys/stats
     - net/nimble
 
-pkg.features:
-    - BLE_DEVICE
+pkg.init_function: ble_ll_init
+pkg.init_stage: 2
+
+pkg.syscfg_defs:
+    BLE_DEVICE:
+        description: 'TBD'
+        value: 1
+
+    BLE_LL_PRIO:
+        description: 'TBD'
+        type: 'task_priority'
+        value: 0
+
+    # Sleep clock accuracy (sca). This is the amount of drift in the system
+    # during when the device is sleeping (in parts per million).
+    #
+    # NOTE: 'the' master sca is an enumerated value based on the sca. Rather
+    # than have a piece of code calculate this value, the developer must set
+    # this value based on the value of the SCA using the following table:
+    #
+    #  SCA between 251 and 500 ppm (inclusive); master sca = 0
+    #  SCA between 151 and 250 ppm (inclusive); master sca = 1
+    #  SCA between 101 and 150 ppm (inclusive); master sca = 2
+    #  SCA between 76 and 100 ppm (inclusive); master sca = 3
+    #  SCA between 51 and 75 ppm (inclusive); master sca = 4
+    #  SCA between 31 and 50 ppm (inclusive); master sca = 5
+    #  SCA between 21 and 30 ppm (inclusive); master sca = 6
+    #  SCA between 0 and 20 ppm (inclusive); master sca = 7
+    #
+    #  For example:
+    #      if your clock drift is 101 ppm, your master should be set to 2.
+    #      if your clock drift is 20, your master sca should be set to 7.
+    #
+    #  The values provided below are merely meant to be an example and should
+    #  be replaced by values appropriate for your platform.
+    BLE_LL_OUR_SCA:
+        description: 'TBD'
+        value: '60'    # in ppm
+
+    BLE_LL_MASTER_SCA:
+        description: 'TBD'
+        value: '4'
+
+    BLE_LL_TX_PWR_DBM:
+        description: 'Transmit power level.'
+        value: '0'
+
+    BLE_NUM_COMP_PKT_RATE:
+        description: >
+            Determines the maximum rate at which the controller will send the
+            number of completed packets event to the host. Rate is in os time
+            ticks.
+        value: '((2000 * OS_TICKS_PER_SEC) / 1000)'
+
+    BLE_LL_MFRG_ID:
+        description: >
+            Manufacturer ID. Should be set to unique ID per manufacturer.
+        value: '0xFFFF'
+
+    # Configuration items for the number of duplicate advertisers and the
+    # number of advertisers from which we have heard a scan response.
+    BLE_LL_NUM_SCAN_DUP_ADVS:
+        description: 'TBD'
+        value: '8'
+    BLE_LL_NUM_SCAN_RSP_ADVS:
+        description: 'TBD'
+        value: '8'
+
+    BLE_LL_WHITELIST_SIZE:
+        description: 'Size of the LL whitelist.'
+        value: '8'
+
+    BLE_LL_RESOLV_LIST_SIZE:
+        description: 'Size of the resolving list.'
+        value: '4'
+
+    # Data length management definitions for connections. These define the
+    # maximum size of the PDU's that will be sent and/or received in a
+    # connection.
+    BLE_LL_MAX_PKT_SIZE:
+        description: 'TBD'
+        value: '251'
+    BLE_LL_SUPP_MAX_RX_BYTES:
+        description: 'TBD'
+        value: 'MYNEWT_VAL_BLE_LL_MAX_PKT_SIZE'
+    BLE_LL_SUPP_MAX_TX_BYTES:
+        description: 'TBD'
+        value: 'MYNEWT_VAL_BLE_LL_MAX_PKT_SIZE'
+    BLE_LL_CONN_INIT_MAX_TX_BYTES:
+        description: 'TBD'
+        value: '27'
+
+    # The number of slots that will be allocated to each connection
+    BLE_LL_CONN_INIT_SLOTS:
+        description: 'TBD'
+        value: '2'
+
+    # The number of random bytes to store
+    BLE_LL_RNG_BUFSIZE:
+        description: 'TBD'
+        value: '32'
+
+    # Configuration for LL supported features.
+    #
+    # There are a total 8 features that the LL can support. These can be found
+    # in v4.2, Vol 6 Part B Section 4.6.
+    #
+    # These feature definitions are used to inform a host or other controller
+    # about the LL features supported by the controller.
+    #
+    # NOTE: 'the' controller always supports extended reject indicate and thus
+    # is not listed here.
+
+
+    BLE_LL_CFG_FEAT_LE_ENCRYPTION:
+        description: >
+            This option enables/disables encryption support in the controller.
+            This option saves both both code and RAM.
+        value: '1'
+
+    BLE_LL_CFG_FEAT_CONN_PARAM_REQ:
+        description: >
+            This option enables/disables the connection parameter request
+            procedure.  This is implemented in the controller but is disabled
+            by default.
+        value: '0'
+
+    BLE_LL_CFG_FEAT_SLAVE_INIT_FEAT_XCHG:
+        description: >
+            This option allows a slave to initiate the feature exchange
+            procedure.  This feature is implemented but currently has no impact
+            on code or ram size
+        value: '1'
+
+    BLE_LL_CFG_FEAT_LE_PING:
+        description: >
+            This option allows a controller to send/receive LE pings.
+            Currently, this feature is not implemented by the controller so
+            turning it on or off has no effect.
+        value: '1'
+
+    BLE_LL_CFG_FEAT_DATA_LEN_EXT:
+        description: >
+            This option enables/disables the data length update procedure in
+            the controller. If enabled, the controller is allowed to change the
+            size of tx/rx pdu's used in a connection. This option has only
+            minor impact on code size and non on RAM.
+        value: '1'
+
+    BLE_LL_CFG_FEAT_LL_PRIVACY:
+        description: >
+            This option is used to enable/disable LL privacy. Currently, this
+            feature is not supported by the nimble controller.
+        value: '1'
+
+    BLE_LL_CFG_FEAT_EXT_SCAN_FILT:
+        description: >
+            This option is used to enable/disable the extended scanner filter
+            policy feature. Currently, this feature is not supported by the
+            nimble controller.
+        value: '0'
+
+    BLE_LL_ACL_PKT_COUNT:
+        description: 'TBD'
+        value: 12
+    BLE_LL_ACL_PKT_SIZE:
+        description: 'TBD'
+        value: 260

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll.c b/net/nimble/controller/src/ble_ll.c
index 1a4f558..978497a 100644
--- a/net/nimble/controller/src/ble_ll.c
+++ b/net/nimble/controller/src/ble_ll.c
@@ -20,6 +20,8 @@
 #include <stdint.h>
 #include <assert.h>
 #include <string.h>
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "stats/stats.h"
 #include "bsp/bsp.h"
@@ -186,6 +188,12 @@ os_stack_t g_ble_ll_stack[BLE_LL_STACK_SIZE];
 struct os_mempool g_ble_ll_hci_ev_pool;
 static void *ble_ll_hci_os_event_buf;
 
+/** Our global device address (public) */
+uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
+
+/** Our random address */
+uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
+
 /* XXX: temporary logging until we transition to real logging */
 #ifdef BLE_LL_LOG
 struct ble_ll_log
@@ -872,7 +880,7 @@ ble_ll_task(void *arg)
     ble_phy_init();
 
     /* Set output power to 1mW (0 dBm) */
-    ble_phy_txpwr_set(NIMBLE_OPT_LL_TX_PWR_DBM);
+    ble_phy_txpwr_set(MYNEWT_VAL(BLE_LL_TX_PWR_DBM));
 
     /* Tell the host that we are ready to receive packets */
     ble_ll_hci_send_noop();
@@ -1089,7 +1097,7 @@ ble_ll_reset(void)
     ble_ll_whitelist_clear();
 
     /* Reset resolving list */
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     ble_ll_resolv_list_reset();
 #endif
 
@@ -1099,13 +1107,30 @@ ble_ll_reset(void)
     return rc;
 }
 
+static void
+ble_ll_seed_prng(void)
+{
+    uint32_t seed;
+    int i;
+
+    /* Seed random number generator with least significant bytes of device
+     * address.
+     */
+    seed = 0;
+    for (i = 0; i < 4; ++i) {
+        seed |= g_dev_addr[i];
+        seed <<= 8;
+    }
+    srand(seed);
+}
+
 /**
  * Initialize the Link Layer. Should be called only once
  *
  * @return int
  */
-int
-ble_ll_init(uint8_t ll_task_prio, uint8_t num_acl_pkts, uint16_t acl_pkt_size)
+void
+ble_ll_init(void)
 {
     int rc;
     uint8_t features;
@@ -1115,8 +1140,8 @@ ble_ll_init(uint8_t ll_task_prio, uint8_t num_acl_pkts, uint16_t acl_pkt_size)
     lldata = &g_ble_ll_data;
 
     /* Set acl pkt size and number */
-    lldata->ll_num_acl_pkts = num_acl_pkts;
-    lldata->ll_acl_pkt_size = acl_pkt_size;
+    lldata->ll_num_acl_pkts = MYNEWT_VAL(BLE_LL_ACL_PKT_COUNT);
+    lldata->ll_acl_pkt_size = MYNEWT_VAL(BLE_LL_ACL_PKT_SIZE);
 
     /* Initialize eventq */
     os_eventq_init(&lldata->ll_evq);
@@ -1135,13 +1160,13 @@ ble_ll_init(uint8_t ll_task_prio, uint8_t num_acl_pkts, uint16_t acl_pkt_size)
 
     ble_ll_hci_os_event_buf = malloc(
         OS_MEMPOOL_BYTES(16, sizeof (struct os_event)));
-    assert(ble_ll_hci_os_event_buf != NULL);
+    SYSINIT_PANIC_ASSERT(ble_ll_hci_os_event_buf != NULL);
 
     /* Create memory pool of OS events */
     rc = os_mempool_init(&g_ble_ll_hci_ev_pool, 16,
                          sizeof (struct os_event), ble_ll_hci_os_event_buf,
                          "g_ble_ll_hci_ev_pool");
-    assert(rc == 0);
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     /* Initialize LL HCI */
     ble_ll_hci_init();
@@ -1161,45 +1186,50 @@ ble_ll_init(uint8_t ll_task_prio, uint8_t num_acl_pkts, uint16_t acl_pkt_size)
     /* Set the supported features. NOTE: we always support extended reject. */
     features = BLE_LL_FEAT_EXTENDED_REJ;
 
-#if (BLE_LL_CFG_FEAT_DATA_LEN_EXT == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_DATA_LEN_EXT) == 1)
     features |= BLE_LL_FEAT_DATA_LEN_EXT;
 #endif
-#if (BLE_LL_CFG_FEAT_CONN_PARAM_REQ == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_CONN_PARAM_REQ) == 1)
     features |= BLE_LL_FEAT_CONN_PARM_REQ;
 #endif
-#if (BLE_LL_CFG_FEAT_SLAVE_INIT_FEAT_XCHG == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_SLAVE_INIT_FEAT_XCHG) == 1)
     features |= BLE_LL_FEAT_SLAVE_INIT;
 #endif
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     features |= BLE_LL_FEAT_LE_ENCRYPTION;
 #endif
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     features |= (BLE_LL_FEAT_LL_PRIVACY | BLE_LL_FEAT_EXT_SCAN_FILT);
     ble_ll_resolv_init();
 #endif
 
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
     features |= BLE_LL_FEAT_LE_PING;
 #endif
 
     /* Initialize random number generation */
     ble_ll_rand_init();
 
+    /* XXX: This really doesn't belong here, as the address probably has not
+     * been set yet.
+     */
+    ble_ll_seed_prng();
+
     lldata->ll_supp_features = features;
 
     /* Initialize the LL task */
-    os_task_init(&g_ble_ll_task, "ble_ll", ble_ll_task, NULL, ll_task_prio,
-                 OS_WAIT_FOREVER, g_ble_ll_stack, BLE_LL_STACK_SIZE);
+    os_task_init(&g_ble_ll_task, "ble_ll", ble_ll_task, NULL,
+                 MYNEWT_VAL(BLE_LL_PRIO), OS_WAIT_FOREVER, g_ble_ll_stack,
+                 BLE_LL_STACK_SIZE);
 
     rc = stats_init_and_reg(STATS_HDR(ble_ll_stats),
                             STATS_SIZE_INIT_PARMS(ble_ll_stats, STATS_SIZE_32),
                             STATS_NAME_INIT_PARMS(ble_ll_stats),
                             "ble_ll");
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
-    ble_hci_trans_cfg_ll(ble_ll_hci_cmd_rx, NULL,
-                                    ble_ll_hci_acl_rx, NULL);
-    return rc;
+    ble_hci_trans_cfg_ll(ble_ll_hci_cmd_rx, NULL, ble_ll_hci_acl_rx, NULL);
 }
 
 #ifdef BLE_LL_LOG

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_adv.c b/net/nimble/controller/src/ble_ll_adv.c
index 1f43a5d..01de299 100644
--- a/net/nimble/controller/src/ble_ll_adv.c
+++ b/net/nimble/controller/src/ble_ll_adv.c
@@ -19,6 +19,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <assert.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "bsp/bsp.h"
 #include "ble/xcvr.h"
@@ -120,7 +121,7 @@ struct ble_ll_adv_sm g_ble_ll_adv_sm;
 #define BLE_LL_ADV_DIRECT_SCHED_MAX_USECS   (502)
 
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 void
 ble_ll_adv_chk_rpa_timeout(struct ble_ll_adv_sm *advsm)
 {
@@ -373,12 +374,12 @@ ble_ll_adv_tx_start_cb(struct ble_ll_sched_item *sch)
         goto adv_tx_done;
     }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     /* XXX: automatically do this in the phy based on channel? */
     ble_phy_encrypt_disable();
 #endif
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     advsm->adv_rpa_index = -1;
     if (ble_ll_resolv_enabled()) {
         ble_phy_resolv_list_enable();
@@ -578,7 +579,7 @@ ble_ll_adv_set_adv_params(uint8_t *cmd)
         return BLE_ERR_INV_HCI_CMD_PARMS;
     }
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     if (own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
         /* Copy peer address */
         memcpy(advsm->peer_addr, cmd + 7, BLE_DEV_ADDR_LEN);
@@ -696,7 +697,7 @@ ble_ll_adv_sm_start(struct ble_ll_adv_sm *advsm)
     }
 
     /* This will generate an RPA for both initiator addr and adva */
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     ble_ll_adv_chk_rpa_timeout(advsm);
 #endif
 
@@ -759,7 +760,7 @@ ble_ll_adv_scheduled(uint32_t sch_start)
 int
 ble_ll_adv_read_txpwr(uint8_t *rspbuf, uint8_t *rsplen)
 {
-    rspbuf[0] = NIMBLE_OPT_LL_TX_PWR_DBM;
+    rspbuf[0] = MYNEWT_VAL(BLE_LL_TX_PWR_DBM);
     *rsplen = 1;
     return BLE_ERR_SUCCESS;
 }
@@ -909,7 +910,7 @@ ble_ll_adv_rx_req(uint8_t pdu_type, struct os_mbuf *rxpdu)
     peer_addr_type = txadd;
     resolved = 0;
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     if (ble_ll_is_rpa(peer, txadd) && ble_ll_resolv_enabled()) {
         advsm->adv_rpa_index = ble_hw_resolv_list_match();
         if (advsm->adv_rpa_index >= 0) {
@@ -1000,7 +1001,7 @@ ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
             (advsm->adv_type == BLE_HCI_ADV_TYPE_ADV_DIRECT_IND_LD)) {
             ident_addr = inita;
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
             if (resolved) {
                 ident_addr = g_ble_ll_resolv_list[advsm->adv_rpa_index].rl_identity_addr;
                 addr_type = g_ble_ll_resolv_list[advsm->adv_rpa_index].rl_addr_type;
@@ -1014,7 +1015,7 @@ ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
     }
 
     if (valid) {
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
         if (resolved) {
             /* Retain the resolvable private address that we received. */
             memcpy(advsm->adv_rpa, inita, BLE_DEV_ADDR_LEN);
@@ -1296,7 +1297,7 @@ ble_ll_adv_event_done(void *arg)
     }
 
     /* We need to regenerate our RPA's if we have passed timeout */
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     ble_ll_adv_chk_rpa_timeout(advsm);
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_conn.c b/net/nimble/controller/src/ble_ll_conn.c
index cb0f89b..8ec6bca 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -20,6 +20,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <assert.h>
+#include "syscfg/syscfg.h"
 #include "bsp/bsp.h"
 #include "os/os.h"
 #include "nimble/ble.h"
@@ -124,7 +125,7 @@ struct ble_ll_empty_pdu
 };
 
 /* We cannot have more than 254 connections given our current implementation */
-#if (NIMBLE_OPT_MAX_CONNECTIONS >= 255)
+#if (MYNEWT_VAL(BLE_MAX_CONNECTIONS) >= 255)
     #error "Maximum # of connections is 254"
 #endif
 
@@ -144,7 +145,7 @@ struct ble_ll_conn_sm *g_ble_ll_conn_create_sm;
 struct ble_ll_conn_sm *g_ble_ll_conn_cur_sm;
 
 /* Connection state machine array */
-struct ble_ll_conn_sm g_ble_ll_conn_sm[NIMBLE_OPT_MAX_CONNECTIONS];
+struct ble_ll_conn_sm g_ble_ll_conn_sm[MYNEWT_VAL(BLE_MAX_CONNECTIONS)];
 
 /* List of active connections */
 struct ble_ll_conn_active_list g_ble_ll_conn_active_list;
@@ -211,7 +212,7 @@ STATS_NAME_START(ble_ll_conn_stats)
     STATS_NAME(ble_ll_conn_stats, mic_failures)
 STATS_NAME_END(ble_ll_conn_stats)
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /**
  * Called to determine if the received PDU is an empty PDU or not.
  */
@@ -313,7 +314,7 @@ ble_ll_conn_find_active_conn(uint16_t handle)
     struct ble_ll_conn_sm *connsm;
 
     connsm = NULL;
-    if ((handle != 0) && (handle <= NIMBLE_OPT_MAX_CONNECTIONS)) {
+    if ((handle != 0) && (handle <= MYNEWT_VAL(BLE_MAX_CONNECTIONS))) {
         connsm = &g_ble_ll_conn_sm[handle - 1];
         if (connsm->conn_state == BLE_LL_CONN_STATE_IDLE) {
             connsm = NULL;
@@ -364,7 +365,7 @@ ble_ll_conn_calc_window_widening(struct ble_ll_conn_sm *connsm)
     if (time_since_last_anchor > 0) {
         delta_msec = cputime_ticks_to_usecs(time_since_last_anchor) / 1000;
         total_sca_ppm = g_ble_sca_ppm_tbl[connsm->master_sca] +
-            NIMBLE_OPT_LL_OUR_SCA;
+            MYNEWT_VAL(BLE_LL_OUR_SCA);
         window_widening = (total_sca_ppm * delta_msec) / 1000;
     }
 
@@ -598,7 +599,7 @@ ble_ll_conn_wait_txend(void *arg)
     ble_ll_event_send(&connsm->conn_ev_end);
 }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 static void
 ble_ll_conn_start_rx_encrypt(void *arg)
 {
@@ -699,7 +700,7 @@ ble_ll_conn_chk_csm_flags(struct ble_ll_conn_sm *connsm)
 {
     uint8_t update_status;
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     if (connsm->csmflags.cfbit.send_ltk_req) {
         /*
          * Send Long term key request event to host. If masked, we need to
@@ -789,7 +790,7 @@ ble_ll_conn_tx_data_pdu(struct ble_ll_conn_sm *connsm)
         m = OS_MBUF_PKTHDR_TO_MBUF(pkthdr);
         nextpkthdr = STAILQ_NEXT(pkthdr, omp_next);
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
         /*
          * If we are encrypting, we are only allowed to send certain
          * kinds of LL control PDU's. If none is enqueued, send empty pdu!
@@ -839,7 +840,7 @@ ble_ll_conn_tx_data_pdu(struct ble_ll_conn_sm *connsm)
             if (cur_offset == 0) {
                 hdr_byte = ble_hdr->txinfo.hdr_byte & BLE_LL_DATA_HDR_LLID_MASK;
             }
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
             if (connsm->enc_data.enc_state > CONN_ENC_S_ENCRYPTED) {
                 /* We will allow a next packet if it itself is allowed */
                 pkthdr = OS_MBUF_PKTHDR(connsm->cur_tx_pdu);
@@ -854,7 +855,7 @@ ble_ll_conn_tx_data_pdu(struct ble_ll_conn_sm *connsm)
             /* Empty PDU here. NOTE: header byte gets set later */
             pktlen = 0;
             cur_txlen = 0;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
             if (connsm->enc_data.enc_state > CONN_ENC_S_ENCRYPTED) {
                 /* We will allow a next packet if it itself is allowed */
                 if (nextpkthdr && !ble_ll_ctrl_enc_allowed_pdu(nextpkthdr)) {
@@ -977,7 +978,7 @@ conn_tx_pdu:
         txend_func = NULL;
     }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     int is_ctrl;
     uint8_t llid;
     uint8_t opcode;
@@ -1118,7 +1119,7 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
     ble_phy_setchan(connsm->data_chan_index, connsm->access_addr,
                     connsm->crcinit);
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     ble_phy_resolv_list_disable();
 #endif
 
@@ -1126,7 +1127,7 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
         /* Set start time of transmission */
         rc = ble_phy_tx_set_start_time(sch->start_time + XCVR_PROC_DELAY_USECS);
         if (!rc) {
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
             if (CONN_F_ENCRYPTED(connsm)) {
                 ble_phy_encrypt_enable(connsm->enc_data.tx_pkt_cntr,
                                        connsm->enc_data.iv,
@@ -1148,7 +1149,7 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
             rc = BLE_LL_SCHED_STATE_DONE;
         }
     } else {
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
             if (CONN_F_ENCRYPTED(connsm)) {
                 ble_phy_encrypt_enable(connsm->enc_data.rx_pkt_cntr,
                                        connsm->enc_data.iv,
@@ -1263,7 +1264,7 @@ ble_ll_conn_can_send_next_pdu(struct ble_ll_conn_sm *connsm, uint32_t begtime)
     return rc;
 }
 
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
 /**
  * Callback for the Authenticated payload timer. This function is called
  * when the authenticated payload timer expires. When the authenticated
@@ -1337,7 +1338,7 @@ ble_ll_conn_master_init(struct ble_ll_conn_sm *connsm,
     /* Set default ce parameters */
     connsm->tx_win_size = BLE_LL_CONN_TX_WIN_MIN;
     connsm->tx_win_off = 0;
-    connsm->master_sca = NIMBLE_OPT_LL_MASTER_SCA;
+    connsm->master_sca = MYNEWT_VAL(BLE_LL_MASTER_SCA);
 
     /* Hop increment is a random value between 5 and 16. */
     connsm->hop_inc = (rand() % 12) + 5;
@@ -1465,12 +1466,12 @@ ble_ll_conn_sm_new(struct ble_ll_conn_sm *connsm)
     connsm->eff_max_rx_octets = BLE_LL_CONN_SUPP_BYTES_MIN;
 
     /* Reset encryption data */
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     memset(&connsm->enc_data, 0, sizeof(struct ble_ll_conn_enc_data));
     connsm->enc_data.enc_state = CONN_ENC_S_UNENCRYPTED;
 #endif
 
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
     connsm->auth_pyld_tmo = BLE_LL_CONN_DEF_AUTH_PYLD_TMO;
     CONN_F_LE_PING_SUPP(connsm) = 1;
     os_callout_func_init(&connsm->auth_pyld_timer,
@@ -1558,7 +1559,7 @@ ble_ll_conn_end(struct ble_ll_conn_sm *connsm, uint8_t ble_err)
     /* Stop any control procedures that might be running */
     os_callout_stop(&connsm->ctrl_proc_rsp_timer.cf_c);
 
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
     os_callout_stop(&connsm->auth_pyld_timer.cf_c);
 #endif
 
@@ -1738,7 +1739,7 @@ ble_ll_conn_next_event(struct ble_ll_conn_sm *connsm)
      * Calculate ce end time. For a slave, we need to add window widening and
      * the transmit window if we still have one.
      */
-    itvl = NIMBLE_OPT_LL_CONN_INIT_SLOTS * BLE_LL_SCHED_USECS_PER_SLOT;
+    itvl = MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS) * BLE_LL_SCHED_USECS_PER_SLOT;
     if (connsm->conn_role == BLE_LL_CONN_ROLE_SLAVE) {
         cur_ww = ble_ll_conn_calc_window_widening(connsm);
         max_ww = (connsm->conn_itvl * (BLE_LL_CONN_ITVL_USECS/2)) - BLE_LL_IFS;
@@ -1803,7 +1804,7 @@ ble_ll_conn_created(struct ble_ll_conn_sm *connsm, uint32_t endtime)
         usecs = 1250 + (connsm->tx_win_off * BLE_LL_CONN_TX_WIN_USECS);
         connsm->anchor_point = endtime + cputime_usecs_to_ticks(usecs);
         usecs = connsm->slave_cur_tx_win_usecs +
-            (NIMBLE_OPT_LL_CONN_INIT_SLOTS * BLE_LL_SCHED_USECS_PER_SLOT);
+            (MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS) * BLE_LL_SCHED_USECS_PER_SLOT);
         connsm->ce_end_time = connsm->anchor_point +
             cputime_usecs_to_ticks(usecs);
         connsm->slave_cur_window_widening = 0;
@@ -1891,7 +1892,7 @@ ble_ll_conn_event_end(void *arg)
         connsm->slave_cur_tx_win_usecs = 0;
     }
 
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
     /*
      * If we are encrypted and have passed the authenticated payload timeout
      * we need to send an event to tell the host. Unfortunately, I think we
@@ -2290,7 +2291,7 @@ ble_ll_init_rx_isr_end(struct os_mbuf *rxpdu, uint8_t crcok)
         resolved = 0;
         chk_wl = ble_ll_scan_whitelist_enabled();
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
         if (ble_ll_is_rpa(adv_addr, addr_type) && ble_ll_resolv_enabled()) {
             index = ble_hw_resolv_list_match();
             if (index >= 0) {
@@ -2335,7 +2336,7 @@ ble_ll_init_rx_isr_end(struct os_mbuf *rxpdu, uint8_t crcok)
         /* Attempt to schedule new connection. Possible that this might fail */
         endtime = ble_hdr->beg_cputime + BLE_TX_DUR_USECS_M(pyld_len);
         if (!ble_ll_sched_master_new(connsm, endtime,
-                                     NIMBLE_OPT_LL_CONN_INIT_SLOTS)) {
+                                     MYNEWT_VAL(BLE_LL_CONN_INIT_SLOTS))) {
             /* Setup to transmit the connect request */
             rc = ble_ll_conn_request_send(addr_type, adv_addr,
                                           connsm->tx_win_off, index);
@@ -2497,7 +2498,7 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr)
                 goto conn_rx_data_pdu_end;
             }
 
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
             /*
              * Reset authenticated payload timeout if valid MIC. NOTE: we dont
              * check the MIC failure bit as that would have terminated the
@@ -2536,7 +2537,7 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr)
                     goto conn_rx_data_pdu_end;
                 }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
                 /*
                  * XXX: should we check to see if we are in a state where we
                  * might expect to get an encrypted PDU?
@@ -2682,14 +2683,14 @@ ble_ll_conn_rx_isr_end(struct os_mbuf *rxpdu)
         conn_nesn = connsm->next_exp_seqnum;
         if ((hdr_sn && conn_nesn) || (!hdr_sn && !conn_nesn)) {
             connsm->next_exp_seqnum ^= 1;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
             if (CONN_F_ENCRYPTED(connsm) && !ble_ll_conn_is_empty_pdu(rxpdu)) {
                 ++connsm->enc_data.rx_pkt_cntr;
             }
 #endif
         }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
         ble_ll_log(BLE_LL_LOG_ID_CONN_RX,
                    hdr_byte,
                    (uint16_t)connsm->tx_seqnum << 8 | conn_nesn,
@@ -2727,7 +2728,7 @@ ble_ll_conn_rx_isr_end(struct os_mbuf *rxpdu)
                  */
                 txpdu = connsm->cur_tx_pdu;
                 if (txpdu) {
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
                     if (connsm->enc_data.tx_encrypted) {
                         ++connsm->enc_data.tx_pkt_cntr;
                     }
@@ -2789,7 +2790,7 @@ chk_rx_terminate_ind:
         } else {
             /* A slave always replies */
             reply = 1;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
             if (is_ctrl && (opcode == BLE_LL_CTRL_PAUSE_ENC_RSP)) {
                 connsm->enc_data.enc_state = CONN_ENC_S_UNENCRYPTED;
             }
@@ -2851,7 +2852,7 @@ ble_ll_conn_enqueue_pkt(struct ble_ll_conn_sm *connsm, struct os_mbuf *om,
     }
 
     lifo = 0;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     if (connsm->enc_data.enc_state > CONN_ENC_S_ENCRYPTED) {
         uint8_t llid;
 
@@ -3125,17 +3126,17 @@ ble_ll_conn_module_reset(void)
     conn_params = &g_ble_ll_conn_params;
     max_phy_pyld = ble_phy_max_data_pdu_pyld();
 
-    maxbytes = min(NIMBLE_OPT_LL_SUPP_MAX_RX_BYTES, max_phy_pyld);
+    maxbytes = min(MYNEWT_VAL(BLE_LL_SUPP_MAX_RX_BYTES), max_phy_pyld);
     conn_params->supp_max_rx_octets = maxbytes;
     conn_params->supp_max_rx_time =
         BLE_TX_DUR_USECS_M(maxbytes + BLE_LL_DATA_MIC_LEN);
 
-    maxbytes = min(NIMBLE_OPT_LL_SUPP_MAX_TX_BYTES, max_phy_pyld);
+    maxbytes = min(MYNEWT_VAL(BLE_LL_SUPP_MAX_TX_BYTES), max_phy_pyld);
     conn_params->supp_max_tx_octets = maxbytes;
     conn_params->supp_max_tx_time =
         BLE_TX_DUR_USECS_M(maxbytes + BLE_LL_DATA_MIC_LEN);
 
-    maxbytes = min(NIMBLE_OPT_LL_CONN_INIT_MAX_TX_BYTES, max_phy_pyld);
+    maxbytes = min(MYNEWT_VAL(BLE_LL_CONN_INIT_MAX_TX_BYTES), max_phy_pyld);
     conn_params->conn_init_max_tx_octets = maxbytes;
     conn_params->conn_init_max_tx_time =
         BLE_TX_DUR_USECS_M(maxbytes + BLE_LL_DATA_MIC_LEN);
@@ -3171,7 +3172,7 @@ ble_ll_conn_module_init(void)
      * the specification allows a handle of zero; we just avoid using it.
      */
     connsm = &g_ble_ll_conn_sm[0];
-    for (i = 0; i < NIMBLE_OPT_MAX_CONNECTIONS; ++i) {
+    for (i = 0; i < MYNEWT_VAL(BLE_MAX_CONNECTIONS); ++i) {
 
         memset(connsm, 0, sizeof(struct ble_ll_conn_sm));
         connsm->conn_handle = i + 1;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_conn_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_conn_hci.c b/net/nimble/controller/src/ble_ll_conn_hci.c
index f653ca6..3f3b77c 100644
--- a/net/nimble/controller/src/ble_ll_conn_hci.c
+++ b/net/nimble/controller/src/ble_ll_conn_hci.c
@@ -20,6 +20,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <assert.h>
+#include "syscfg/syscfg.h"
 #include "bsp/bsp.h"
 #include "os/os.h"
 #include "nimble/ble.h"
@@ -228,7 +229,7 @@ ble_ll_conn_num_comp_pkts_event_send(void)
 
     /* Check rate limit */
     if ((uint32_t)(g_ble_ll_next_num_comp_pkt_evt - os_time_get()) <
-         NIMBLE_OPT_NUM_COMP_PKT_RATE) {
+         MYNEWT_VAL(BLE_NUM_COMP_PKT_RATE)) {
         return;
     }
 
@@ -295,11 +296,11 @@ ble_ll_conn_num_comp_pkts_event_send(void)
 
     if (event_sent) {
         g_ble_ll_next_num_comp_pkt_evt = os_time_get() +
-            NIMBLE_OPT_NUM_COMP_PKT_RATE;
+            MYNEWT_VAL(BLE_NUM_COMP_PKT_RATE);
     }
 }
 
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
 /**
  * Send a authenticated payload timeout event
  *
@@ -928,7 +929,7 @@ ble_ll_conn_hci_set_chan_class(uint8_t *cmdbuf)
     return rc;
 }
 
-#if (BLE_LL_CFG_FEAT_DATA_LEN_EXT == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_DATA_LEN_EXT) == 1)
 int
 ble_ll_conn_hci_set_data_len(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t *rsplen)
 {
@@ -969,7 +970,7 @@ ble_ll_conn_hci_set_data_len(uint8_t *cmdbuf, uint8_t *rspbuf, uint8_t *rsplen)
 }
 #endif
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /**
  * LE start encrypt command
  *
@@ -1062,7 +1063,7 @@ ltk_key_cmd_complete:
 }
 #endif
 
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
 /**
  * Read authenticated payload timeout (OGF=3, OCF==0x007B)
  *

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_conn_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_conn_priv.h b/net/nimble/controller/src/ble_ll_conn_priv.h
index b855b22..17a5edd 100644
--- a/net/nimble/controller/src/ble_ll_conn_priv.h
+++ b/net/nimble/controller/src/ble_ll_conn_priv.h
@@ -148,7 +148,7 @@ int ble_ll_conn_hci_wr_auth_pyld_tmo(uint8_t *cmdbuf, uint8_t *rsp,
                                      uint8_t *rsplen);
 int ble_ll_conn_hci_rd_auth_pyld_tmo(uint8_t *cmdbuf, uint8_t *rsp,
                                      uint8_t *rsplen);
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
 void ble_ll_conn_auth_pyld_timer_start(struct ble_ll_conn_sm *connsm);
 #else
 #define ble_ll_conn_auth_pyld_timer_start(x)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_ctrl.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_ctrl.c b/net/nimble/controller/src/ble_ll_ctrl.c
index 3ba69af..1d98ed3 100644
--- a/net/nimble/controller/src/ble_ll_ctrl.c
+++ b/net/nimble/controller/src/ble_ll_ctrl.c
@@ -19,6 +19,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <string.h>
+#include "syscfg/syscfg.h"
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
 #include "nimble/hci_common.h"
@@ -306,7 +307,7 @@ ble_ll_ctrl_proc_unk_rsp(struct ble_ll_conn_sm *connsm, uint8_t *dptr)
         break;
     case BLE_LL_CTRL_PING_REQ:
         CONN_F_LE_PING_SUPP(connsm) = 0;
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
         os_callout_stop(&connsm->auth_pyld_timer.cf_c);
 #endif
         ctrl_proc = BLE_LL_CTRL_PROC_LE_PING;
@@ -346,7 +347,7 @@ ble_ll_ctrl_datalen_upd_make(struct ble_ll_conn_sm *connsm, uint8_t *dptr)
     htole16(dptr + 7, connsm->max_tx_time);
 }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 void
 ble_ll_calc_session_key(struct ble_ll_conn_sm *connsm)
 {
@@ -697,7 +698,7 @@ ble_ll_ctrl_rx_start_enc_rsp(struct ble_ll_conn_sm *connsm)
         /* We are encrypted */
         connsm->enc_data.enc_state = CONN_ENC_S_ENCRYPTED;
         ble_ll_ctrl_proc_stop(connsm, BLE_LL_CTRL_PROC_ENCRYPT);
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
         ble_ll_conn_auth_pyld_timer_start(connsm);
 #endif
         rc = BLE_ERR_MAX;
@@ -777,7 +778,7 @@ ble_ll_ctrl_version_ind_make(struct ble_ll_conn_sm *connsm, uint8_t *pyld)
 
     /* Fill out response */
     pyld[0] = BLE_HCI_VER_BCS_4_2;
-    htole16(pyld + 1, NIMBLE_OPT_LL_MFRG_ID);
+    htole16(pyld + 1, MYNEWT_VAL(BLE_LL_MFRG_ID));
     htole16(pyld + 3, BLE_LL_SUB_VERS_NR);
 }
 
@@ -946,7 +947,7 @@ ble_ll_ctrl_rx_reject_ind(struct ble_ll_conn_sm *connsm, uint8_t *dptr,
             ble_ll_hci_ev_conn_update(connsm, ble_error);
         }
         break;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     case BLE_LL_CTRL_PROC_ENCRYPT:
         ble_ll_ctrl_proc_stop(connsm, BLE_LL_CTRL_PROC_ENCRYPT);
         ble_ll_hci_ev_encrypt_chg(connsm, ble_error);
@@ -1302,7 +1303,7 @@ ble_ll_ctrl_proc_init(struct ble_ll_conn_sm *connsm, int ctrl_proc)
             opcode = BLE_LL_CTRL_LENGTH_REQ;
             ble_ll_ctrl_datalen_upd_make(connsm, dptr);
             break;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
         /* XXX: deal with already encrypted connection.*/
         case BLE_LL_CTRL_PROC_ENCRYPT:
             /* If we are already encrypted we do pause procedure */
@@ -1517,7 +1518,7 @@ ble_ll_ctrl_rx_pdu(struct ble_ll_conn_sm *connsm, struct os_mbuf *om)
     uint8_t *dptr;
     uint8_t *rspbuf;
     uint8_t *rspdata;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     int restart_encryption;
 #endif
 
@@ -1562,7 +1563,7 @@ ble_ll_ctrl_rx_pdu(struct ble_ll_conn_sm *connsm, struct os_mbuf *om)
         goto rx_malformed_ctrl;
     }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     restart_encryption = 0;
 #endif
 
@@ -1673,7 +1674,7 @@ ble_ll_ctrl_rx_pdu(struct ble_ll_conn_sm *connsm, struct os_mbuf *om)
     case BLE_LL_CTRL_SLAVE_FEATURE_REQ:
         rsp_opcode = ble_ll_ctrl_rx_feature_req(connsm, dptr, rspbuf, opcode);
         break;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     case BLE_LL_CTRL_ENC_REQ:
         rsp_opcode = ble_ll_ctrl_rx_enc_req(connsm, dptr, rspdata);
         break;
@@ -1733,7 +1734,7 @@ ll_ctrl_send_rsp:
         }
         len = g_ble_ll_ctrl_pkt_lengths[rsp_opcode] + 1;
         ble_ll_conn_enqueue_pkt(connsm, om, BLE_LL_LLID_CTRL, len);
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
         if (restart_encryption) {
             /* XXX: what happens if this fails? Meaning we cant allocate
                mbuf? */
@@ -1822,18 +1823,18 @@ ble_ll_ctrl_tx_done(struct os_mbuf *txpdu, struct ble_ll_conn_sm *connsm)
             connsm->reject_reason = txpdu->om_data[2];
             connsm->csmflags.cfbit.host_expects_upd_event = 1;
         }
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
         if (connsm->enc_data.enc_state > CONN_ENC_S_ENCRYPTED) {
             connsm->enc_data.enc_state = CONN_ENC_S_UNENCRYPTED;
         }
 #endif
         break;
     case BLE_LL_CTRL_REJECT_IND:
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
         connsm->enc_data.enc_state = CONN_ENC_S_UNENCRYPTED;
 #endif
         break;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     case BLE_LL_CTRL_PAUSE_ENC_REQ:
         /* note: fall-through intentional */
     case BLE_LL_CTRL_ENC_REQ:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_hci.c b/net/nimble/controller/src/ble_ll_hci.c
index 352dda2..401aacc 100644
--- a/net/nimble/controller/src/ble_ll_hci.c
+++ b/net/nimble/controller/src/ble_ll_hci.c
@@ -19,6 +19,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <string.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
@@ -103,7 +104,7 @@ ble_ll_hci_send_noop(void)
     return rc;
 }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /**
  * LE encrypt command
  *
@@ -171,7 +172,7 @@ ble_ll_hci_rd_local_version(uint8_t *rspbuf, uint8_t *rsplen)
 
     hci_rev = 0;
     lmp_subver = 0;
-    mfrg = NIMBLE_OPT_LL_MFRG_ID;
+    mfrg = MYNEWT_VAL(BLE_LL_MFRG_ID);
 
     /* Place the data packet length and number of packets in the buffer */
     rspbuf[0] = BLE_HCI_VER_BCS_4_2;
@@ -281,7 +282,7 @@ ble_ll_hci_le_read_bufsize(uint8_t *rspbuf, uint8_t *rsplen)
     return BLE_ERR_SUCCESS;
 }
 
-#if (BLE_LL_CFG_FEAT_DATA_LEN_EXT == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_DATA_LEN_EXT) == 1)
 /**
  * HCI write suggested default data length command.
  *
@@ -615,7 +616,7 @@ ble_ll_hci_le_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen)
     case BLE_HCI_OCF_LE_RD_REM_FEAT:
         rc = ble_ll_conn_hci_read_rem_features(cmdbuf);
         break;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     case BLE_HCI_OCF_LE_ENCRYPT:
         rc = ble_ll_hci_le_encrypt(cmdbuf, rspbuf, rsplen);
         break;
@@ -623,7 +624,7 @@ ble_ll_hci_le_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen)
     case BLE_HCI_OCF_LE_RAND:
         rc = ble_ll_hci_le_rand(rspbuf, rsplen);
         break;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     case BLE_HCI_OCF_LE_START_ENCRYPT:
         rc = ble_ll_conn_hci_le_start_encrypt(cmdbuf);
         break;
@@ -642,7 +643,7 @@ ble_ll_hci_le_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen)
     case BLE_HCI_OCF_LE_REM_CONN_PARAM_RR:
         rc = ble_ll_conn_hci_param_reply(cmdbuf, 1);
         break;
-#if (BLE_LL_CFG_FEAT_DATA_LEN_EXT == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_DATA_LEN_EXT) == 1)
     case BLE_HCI_OCF_LE_SET_DATA_LEN:
         rc = ble_ll_conn_hci_set_data_len(cmdbuf, rspbuf, rsplen);
         break;
@@ -653,7 +654,7 @@ ble_ll_hci_le_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen)
         rc = ble_ll_hci_le_wr_sugg_data_len(cmdbuf);
         break;
 #endif
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     case BLE_HCI_OCF_LE_ADD_RESOLV_LIST :
         rc = ble_ll_resolv_list_add(cmdbuf);
         break;
@@ -759,7 +760,7 @@ ble_ll_hci_ctlr_bb_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen)
 {
     int rc;
     uint8_t len;
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
     uint8_t *rspbuf;
 #endif
 
@@ -771,7 +772,7 @@ ble_ll_hci_ctlr_bb_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen)
 
     /* Move past HCI command header */
     cmdbuf += BLE_HCI_CMD_HDR_LEN;
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
     rspbuf = cmdbuf + BLE_HCI_EVENT_CMD_COMPLETE_MIN_LEN;
 #endif
 
@@ -793,7 +794,7 @@ ble_ll_hci_ctlr_bb_cmd_proc(uint8_t *cmdbuf, uint16_t ocf, uint8_t *rsplen)
             rc = BLE_ERR_SUCCESS;
         }
         break;
-#if (BLE_LL_CFG_FEAT_LE_PING == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
     case BLE_HCI_OCF_CB_RD_AUTH_PYLD_TMO:
         rc = ble_ll_conn_hci_wr_auth_pyld_tmo(cmdbuf, rspbuf, rsplen);
         break;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_hci_ev.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_hci_ev.c b/net/nimble/controller/src/ble_ll_hci_ev.c
index 2548bdc..9b8e315 100644
--- a/net/nimble/controller/src/ble_ll_hci_ev.c
+++ b/net/nimble/controller/src/ble_ll_hci_ev.c
@@ -19,6 +19,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <string.h>
+#include "syscfg/syscfg.h"
 #include "nimble/ble.h"
 #include "nimble/hci_common.h"
 #include "nimble/ble_hci_trans.h"
@@ -111,7 +112,7 @@ ble_ll_hci_ev_conn_update(struct ble_ll_conn_sm *connsm, uint8_t status)
     }
 }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 void
 ble_ll_hci_ev_encrypt_chg(struct ble_ll_conn_sm *connsm, uint8_t status)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_rand.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_rand.c b/net/nimble/controller/src/ble_ll_rand.c
index 8d26f8d..1306138 100644
--- a/net/nimble/controller/src/ble_ll_rand.c
+++ b/net/nimble/controller/src/ble_ll_rand.c
@@ -20,6 +20,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <string.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
@@ -35,10 +36,10 @@ struct ble_ll_rnum_data
 };
 
 struct ble_ll_rnum_data g_ble_ll_rnum_data;
-uint8_t g_ble_ll_rnum_buf[NIMBLE_OPT_LL_RNG_BUFSIZE];
+uint8_t g_ble_ll_rnum_buf[MYNEWT_VAL(BLE_LL_RNG_BUFSIZE)];
 
 #define IS_RNUM_BUF_END(x)  \
-    (x == &g_ble_ll_rnum_buf[NIMBLE_OPT_LL_RNG_BUFSIZE - 1])
+    (x == &g_ble_ll_rnum_buf[MYNEWT_VAL(BLE_LL_RNG_BUFSIZE) - 1])
 
 void
 ble_ll_rand_sample(uint8_t rnum)
@@ -46,7 +47,7 @@ ble_ll_rand_sample(uint8_t rnum)
     os_sr_t sr;
 
     OS_ENTER_CRITICAL(sr);
-    if (g_ble_ll_rnum_data.rnd_size < NIMBLE_OPT_LL_RNG_BUFSIZE) {
+    if (g_ble_ll_rnum_data.rnd_size < MYNEWT_VAL(BLE_LL_RNG_BUFSIZE)) {
         ++g_ble_ll_rnum_data.rnd_size;
         g_ble_ll_rnum_data.rnd_in[0] = rnum;
         if (IS_RNUM_BUF_END(g_ble_ll_rnum_data.rnd_in)) {
@@ -94,7 +95,7 @@ ble_ll_rand_data_get(uint8_t *buf, uint8_t len)
         /* Wait till bytes are in buffer. */
         if (len) {
             while ((g_ble_ll_rnum_data.rnd_size < len) &&
-                   (g_ble_ll_rnum_data.rnd_size < NIMBLE_OPT_LL_RNG_BUFSIZE)) {
+                   (g_ble_ll_rnum_data.rnd_size < MYNEWT_VAL(BLE_LL_RNG_BUFSIZE))) {
                 /* Spin here */
             }
         }
@@ -138,7 +139,7 @@ int
 ble_ll_rand_start(void)
 {
     /* Start the generation of numbers if we are not full */
-    if (g_ble_ll_rnum_data.rnd_size < NIMBLE_OPT_LL_RNG_BUFSIZE) {
+    if (g_ble_ll_rnum_data.rnd_size < MYNEWT_VAL(BLE_LL_RNG_BUFSIZE)) {
         ble_hw_rng_start();
     }
     return 0;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_resolv.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_resolv.c b/net/nimble/controller/src/ble_ll_resolv.c
index 4d91685..608da8c 100644
--- a/net/nimble/controller/src/ble_ll_resolv.c
+++ b/net/nimble/controller/src/ble_ll_resolv.c
@@ -19,6 +19,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <string.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
@@ -30,7 +31,7 @@
 #include "controller/ble_hw.h"
 #include "ble_ll_conn_priv.h"
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 
 /* Flag denoting whether or not address translation is enabled. */
 uint8_t g_ble_ll_addr_res_enabled;
@@ -38,7 +39,7 @@ uint8_t g_ble_ll_resolv_list_size;
 uint8_t g_ble_ll_resolv_list_cnt;
 uint32_t g_ble_ll_resolv_rpa_tmo;
 
-struct ble_ll_resolv_entry g_ble_ll_resolv_list[NIMBLE_OPT_LL_RESOLV_LIST_SIZE];
+struct ble_ll_resolv_entry g_ble_ll_resolv_list[MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE)];
 
 /**
  * Called to determine if a change is allowed to the resolving list at this
@@ -457,12 +458,12 @@ ble_ll_resolv_init(void)
     g_ble_ll_resolv_rpa_tmo = 15 * 60 * OS_TICKS_PER_SEC;
 
     hw_size = ble_hw_resolv_list_size();
-    if (hw_size > NIMBLE_OPT_LL_RESOLV_LIST_SIZE) {
-        hw_size = NIMBLE_OPT_LL_RESOLV_LIST_SIZE;
+    if (hw_size > MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE)) {
+        hw_size = MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE);
     }
     g_ble_ll_resolv_list_size = hw_size;
 
 }
 
-#endif  /* if BLE_LL_CFG_FEAT_LL_PRIVACY == 1 */
+#endif  /* if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1 */
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_rng.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_rng.c b/net/nimble/controller/src/ble_ll_rng.c
index d93c420..5f5728b 100644
--- a/net/nimble/controller/src/ble_ll_rng.c
+++ b/net/nimble/controller/src/ble_ll_rng.c
@@ -34,7 +34,7 @@ struct ble_ll_rnum_data
     uint8_t _pad;
 };
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 int
 ble_ll_rng_init(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_scan.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_scan.c b/net/nimble/controller/src/ble_ll_scan.c
index 64c3990..fb73ddd 100644
--- a/net/nimble/controller/src/ble_ll_scan.c
+++ b/net/nimble/controller/src/ble_ll_scan.c
@@ -20,6 +20,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <assert.h>
+#include "syscfg/syscfg.h"
 #include "bsp/bsp.h"
 #include "os/os.h"
 #include "nimble/ble.h"
@@ -50,10 +51,10 @@
  */
 
 /* Dont allow more than 255 of these entries */
-#if NIMBLE_OPT_LL_NUM_SCAN_DUP_ADVS > 255
+#if MYNEWT_VAL(BLE_LL_NUM_SCAN_DUP_ADVS) > 255
     #error "Cannot have more than 255 duplicate entries!"
 #endif
-#if NIMBLE_OPT_LL_NUM_SCAN_RSP_ADVS > 255
+#if MYNEWT_VAL(BLE_LL_NUM_SCAN_RSP_ADVS) > 255
     #error "Cannot have more than 255 scan response entries!"
 #endif
 
@@ -79,12 +80,12 @@ struct ble_ll_scan_advertisers
 /* Contains list of advertisers that we have heard scan responses from */
 static uint8_t g_ble_ll_scan_num_rsp_advs;
 struct ble_ll_scan_advertisers
-g_ble_ll_scan_rsp_advs[NIMBLE_OPT_LL_NUM_SCAN_RSP_ADVS];
+g_ble_ll_scan_rsp_advs[MYNEWT_VAL(BLE_LL_NUM_SCAN_RSP_ADVS)];
 
 /* Used to filter duplicate advertising events to host */
 static uint8_t g_ble_ll_scan_num_dup_advs;
 struct ble_ll_scan_advertisers
-g_ble_ll_scan_dup_advs[NIMBLE_OPT_LL_NUM_SCAN_DUP_ADVS];
+g_ble_ll_scan_dup_advs[MYNEWT_VAL(BLE_LL_NUM_SCAN_DUP_ADVS)];
 
 /* See Vol 6 Part B Section 4.4.3.2. Active scanning backoff */
 static void
@@ -136,7 +137,7 @@ ble_ll_scan_req_pdu_make(struct ble_ll_scan_sm *scansm, uint8_t *adv_addr,
     uint8_t     pdu_type;
     uint8_t     *scana;
     struct os_mbuf *m;
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     uint8_t rpa[BLE_DEV_ADDR_LEN];
     struct ble_ll_resolv_entry *rl;
 #endif
@@ -159,7 +160,7 @@ ble_ll_scan_req_pdu_make(struct ble_ll_scan_sm *scansm, uint8_t *adv_addr,
         scana = g_random_addr;
     }
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     if (scansm->own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
         rl = NULL;
         if (ble_ll_is_rpa(adv_addr, adv_addr_type)) {
@@ -280,7 +281,7 @@ ble_ll_scan_add_dup_adv(uint8_t *addr, uint8_t txadd, uint8_t subev)
     if (!adv) {
         /* XXX: for now, if we dont have room, just leave */
         num_advs = g_ble_ll_scan_num_dup_advs;
-        if (num_advs == NIMBLE_OPT_LL_NUM_SCAN_DUP_ADVS) {
+        if (num_advs == MYNEWT_VAL(BLE_LL_NUM_SCAN_DUP_ADVS)) {
             return;
         }
 
@@ -347,7 +348,7 @@ ble_ll_scan_add_scan_rsp_adv(uint8_t *addr, uint8_t txadd)
 
     /* XXX: for now, if we dont have room, just leave */
     num_advs = g_ble_ll_scan_num_rsp_advs;
-    if (num_advs == NIMBLE_OPT_LL_NUM_SCAN_RSP_ADVS) {
+    if (num_advs == MYNEWT_VAL(BLE_LL_NUM_SCAN_RSP_ADVS)) {
         return;
     }
 
@@ -569,11 +570,11 @@ ble_ll_scan_start(struct ble_ll_scan_sm *scansm, uint8_t chan)
      */
     ble_phy_set_txend_cb(NULL, NULL);
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     ble_phy_encrypt_disable();
 #endif
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     if (ble_ll_resolv_enabled()) {
         ble_phy_resolv_list_enable();
     } else {
@@ -972,7 +973,7 @@ ble_ll_scan_rx_isr_end(struct os_mbuf *rxpdu, uint8_t crcok)
     resolved = 0;
 
     index = -1;
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     if (ble_ll_is_rpa(peer, peer_addr_type) && ble_ll_resolv_enabled()) {
         index = ble_hw_resolv_list_match();
         if (index >= 0) {
@@ -1133,7 +1134,7 @@ ble_ll_scan_rx_pkt_in(uint8_t ptype, uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
     ident_addr_type = txadd;
 
     scansm = &g_ble_ll_scan_sm;
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     index = scansm->scan_rpa_index;
     if (index >= 0) {
         ident_addr = g_ble_ll_resolv_list[index].rl_identity_addr;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_supp_cmd.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_supp_cmd.c b/net/nimble/controller/src/ble_ll_supp_cmd.c
index 738ee9c..3344f8e 100644
--- a/net/nimble/controller/src/ble_ll_supp_cmd.c
+++ b/net/nimble/controller/src/ble_ll_supp_cmd.c
@@ -106,7 +106,7 @@
 #define BLE_SUPP_CMD_LE_SET_HOST_CHAN_CLASS (1 << 3)
 #define BLE_SUPP_CMD_LE_RD_CHAN_MAP         (1 << 4)
 #define BLE_SUPP_CMD_LE_RD_REM_USED_FEAT    (1 << 5)
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 #define BLE_SUPP_CMD_LE_ENCRYPT             (1 << 6)
 #else
 #define BLE_SUPP_CMD_LE_ENCRYPT             (0 << 6)
@@ -126,7 +126,7 @@
 )
 
 /* Octet 28 */
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 #define BLE_SUPP_CMD_LE_START_ENCRYPT       (1 << 0)
 #else
 #define BLE_SUPP_CMD_LE_START_ENCRYPT       (0 << 0)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/controller/src/ble_ll_whitelist.c
----------------------------------------------------------------------
diff --git a/net/nimble/controller/src/ble_ll_whitelist.c b/net/nimble/controller/src/ble_ll_whitelist.c
index f3af569..b92b157 100644
--- a/net/nimble/controller/src/ble_ll_whitelist.c
+++ b/net/nimble/controller/src/ble_ll_whitelist.c
@@ -19,6 +19,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <string.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
@@ -30,8 +31,8 @@
 #include "controller/ble_hw.h"
 #include "hal/hal_cputime.h"
 
-#if (NIMBLE_OPT_LL_WHITELIST_SIZE < BLE_HW_WHITE_LIST_SIZE)
-#define BLE_LL_WHITELIST_SIZE       NIMBLE_OPT_LL_WHITELIST_SIZE
+#if (MYNEWT_VAL(BLE_LL_WHITELIST_SIZE) < BLE_HW_WHITE_LIST_SIZE)
+#define BLE_LL_WHITELIST_SIZE       MYNEWT_VAL(BLE_LL_WHITELIST_SIZE)
 #else
 #define BLE_LL_WHITELIST_SIZE       BLE_HW_WHITE_LIST_SIZE
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/drivers/native/src/ble_hw.c
----------------------------------------------------------------------
diff --git a/net/nimble/drivers/native/src/ble_hw.c b/net/nimble/drivers/native/src/ble_hw.c
index bb30c53..214ad51 100644
--- a/net/nimble/drivers/native/src/ble_hw.c
+++ b/net/nimble/drivers/native/src/ble_hw.c
@@ -20,6 +20,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <string.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "nimble/ble.h"
 #include "nimble/nimble_opt.h"
@@ -164,7 +165,7 @@ ble_hw_rng_read(void)
     return 0;
 }
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 /**
  * Clear the resolving list
  *

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/drivers/native/src/ble_phy.c
----------------------------------------------------------------------
diff --git a/net/nimble/drivers/native/src/ble_phy.c b/net/nimble/drivers/native/src/ble_phy.c
index e2c9f25..d19e54b 100644
--- a/net/nimble/drivers/native/src/ble_phy.c
+++ b/net/nimble/drivers/native/src/ble_phy.c
@@ -19,6 +19,7 @@
 
 #include <stdint.h>
 #include <assert.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "nimble/ble.h"             /* XXX: needed for ble mbuf header.*/
 #include "controller/ble_phy.h"
@@ -249,7 +250,7 @@ ble_phy_rx(void)
     return 0;
 }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /**
  * Called to enable encryption at the PHY. Note that this state will persist
  * in the PHY; in other words, if you call this function you have to call
@@ -510,7 +511,7 @@ ble_phy_max_data_pdu_pyld(void)
     return BLE_LL_DATA_PDU_MAX_PYLD;
 }
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 void
 ble_phy_resolv_list_enable(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/drivers/nrf51/src/ble_hw.c
----------------------------------------------------------------------
diff --git a/net/nimble/drivers/nrf51/src/ble_hw.c b/net/nimble/drivers/nrf51/src/ble_hw.c
index afeda82..259bd05 100644
--- a/net/nimble/drivers/nrf51/src/ble_hw.c
+++ b/net/nimble/drivers/nrf51/src/ble_hw.c
@@ -20,6 +20,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <string.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "ble/xcvr.h"
 #include "nimble/ble.h"
@@ -38,11 +39,11 @@ static uint8_t g_ble_hw_whitelist_mask;
 ble_rng_isr_cb_t g_ble_rng_isr_cb;
 
 /* If LL privacy is enabled, allocate memory for AAR */
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 
 /* The NRF51 supports up to 16 IRK entries */
-#if (NIMBLE_OPT_LL_RESOLV_LIST_SIZE < 16)
-#define NRF_IRK_LIST_ENTRIES    (NIMBLE_OPT_LL_RESOLV_LIST_SIZE)
+#if (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE) < 16)
+#define NRF_IRK_LIST_ENTRIES    (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE))
 #else
 #define NRF_IRK_LIST_ENTRIES    (16)
 #endif
@@ -339,7 +340,7 @@ ble_hw_rng_read(void)
     return rnum;
 }
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY))
 /**
  * Clear the resolving list
  *

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/drivers/nrf51/src/ble_phy.c
----------------------------------------------------------------------
diff --git a/net/nimble/drivers/nrf51/src/ble_phy.c b/net/nimble/drivers/nrf51/src/ble_phy.c
index 5b588bf..db6618f 100644
--- a/net/nimble/drivers/nrf51/src/ble_phy.c
+++ b/net/nimble/drivers/nrf51/src/ble_phy.c
@@ -20,6 +20,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <assert.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "bsp/cmsis_nvic.h"
 #include "nimble/ble.h"
@@ -94,7 +95,7 @@ struct ble_phy_obj g_ble_phy_data;
 /* Global transmit/receive buffer */
 static uint32_t g_ble_phy_txrx_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /* Make sure word-aligned for faster copies */
 static uint32_t g_ble_phy_enc_buf[(NRF_ENC_BUF_SIZE + 3) / 4];
 #endif
@@ -164,10 +165,10 @@ STATS_NAME_END(ble_phy_stats)
  *  bit in the NVIC just to be sure when we disable the PHY.
  */
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /* XXX: test this. only needs 43 bytes. Should just not use the macro for this*/
 /* Per nordic, the number of bytes needed for scratch is 16 + MAX_PKT_SIZE */
-#define NRF_ENC_SCRATCH_WORDS   (((NIMBLE_OPT_LL_MAX_PKT_SIZE + 16) + 3) / 4)
+#define NRF_ENC_SCRATCH_WORDS   (((MYNEWT_VAL(BLE_LL_MAX_PKT_SIZE) + 16) + 3) / 4)
 
 uint32_t g_nrf_encrypt_scratchpad[NRF_ENC_SCRATCH_WORDS];
 
@@ -241,7 +242,7 @@ nrf_wait_disabled(void)
 static void
 ble_phy_rx_xcvr_setup(void)
 {
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     if (g_ble_phy_data.phy_encrypted) {
         NRF_RADIO->PACKETPTR = (uint32_t)&g_ble_phy_enc_buf[0];
         NRF_CCM->INPTR = (uint32_t)&g_ble_phy_enc_buf[0];
@@ -260,7 +261,7 @@ ble_phy_rx_xcvr_setup(void)
     NRF_RADIO->PACKETPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
 #endif
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     if (g_ble_phy_data.phy_privacy) {
         NRF_RADIO->PCNF0 = (6 << RADIO_PCNF0_LFLEN_Pos) |
                            (2 << RADIO_PCNF0_S1LEN_Pos) |
@@ -330,7 +331,7 @@ ble_phy_tx_end_isr(void)
     NRF_RADIO->EVENTS_END = 0;
     wfr_time = NRF_RADIO->SHORTS;
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     /*
      * XXX: not sure what to do. We had a HW error during transmission.
      * For now I just count a stat but continue on like all is good.
@@ -382,7 +383,7 @@ static void
 ble_phy_rx_end_isr(void)
 {
     int rc;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     uint8_t *dptr;
 #endif
     uint8_t crcok;
@@ -400,7 +401,7 @@ ble_phy_rx_end_isr(void)
     ble_hdr = BLE_MBUF_HDR_PTR(g_ble_phy_data.rxpdu);
     assert(NRF_RADIO->EVENTS_RSSIEND != 0);
     ble_hdr->rxinfo.rssi = -1 * NRF_RADIO->RSSISAMPLE;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     dptr = g_ble_phy_data.rxpdu->om_data;
 #endif
     /* Count PHY crc errors and valid packets */
@@ -410,7 +411,7 @@ ble_phy_rx_end_isr(void)
     } else {
         STATS_INC(ble_phy_stats, rx_valid);
         ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_CRC_OK;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
         if (g_ble_phy_data.phy_encrypted) {
             /* Only set MIC failure flag if frame is not zero length */
             if ((dptr[1] != 0) && (NRF_CCM->MICSTATUS == 0)) {
@@ -445,7 +446,7 @@ ble_phy_rx_end_isr(void)
     rxpdu = g_ble_phy_data.rxpdu;
     g_ble_phy_data.rxpdu = NULL;
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1) || (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1) || (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     if (g_ble_phy_data.phy_encrypted || g_ble_phy_data.phy_privacy) {
         /*
          * XXX: This is a horrible ugly hack to deal with the RAM S1 byte.
@@ -509,7 +510,7 @@ ble_phy_rx_start_isr(void)
         g_ble_phy_data.phy_rx_started = 1;
         NRF_RADIO->INTENSET = RADIO_INTENSET_END_Msk;
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
         /* Must start aar if we need to  */
         if (g_ble_phy_data.phy_privacy) {
             NRF_RADIO->EVENTS_BCMATCH = 0;
@@ -619,14 +620,14 @@ ble_phy_init(void)
     /* Captures tx/rx start in timer0 capture 1 */
     NRF_PPI->CHENSET = PPI_CHEN_CH26_Msk;
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     NRF_CCM->INTENCLR = 0xffffffff;
     NRF_CCM->SHORTS = CCM_SHORTS_ENDKSGEN_CRYPT_Msk;
     NRF_CCM->EVENTS_ERROR = 0;
     memset(g_nrf_encrypt_scratchpad, 0, sizeof(g_nrf_encrypt_scratchpad));
 #endif
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     g_ble_phy_data.phy_aar_scratch = 0;
     NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
     NRF_AAR->INTENCLR = 0xffffffff;
@@ -697,7 +698,7 @@ ble_phy_rx(void)
     return 0;
 }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /**
  * Called to enable encryption at the PHY. Note that this state will persist
  * in the PHY; in other words, if you call this function you have to call
@@ -844,7 +845,7 @@ ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
     ble_hdr = BLE_MBUF_HDR_PTR(txpdu);
     payload_len = ble_hdr->txinfo.pyld_len;
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     if (g_ble_phy_data.phy_encrypted) {
         /* RAM representation has S0, LENGTH and S1 fields. (3 bytes) */
         dptr = (uint8_t *)&g_ble_phy_enc_buf[0];
@@ -864,7 +865,7 @@ ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
         NRF_PPI->CHENSET = PPI_CHEN_CH24_Msk;
     } else {
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
         /* Reconfigure PCNF0 */
         NRF_RADIO->PCNF0 = (NRF_LFLEN_BITS << RADIO_PCNF0_LFLEN_Pos) |
                            (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
@@ -879,7 +880,7 @@ ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
     }
 #else
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     /* Reconfigure PCNF0 */
     NRF_RADIO->PCNF0 = (NRF_LFLEN_BITS << RADIO_PCNF0_LFLEN_Pos) |
                        (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
@@ -1139,14 +1140,14 @@ ble_phy_xcvr_state_get(void)
 uint8_t
 ble_phy_max_data_pdu_pyld(void)
 {
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     return NRF_MAX_ENCRYPTED_PYLD_LEN;
 #else
     return BLE_LL_DATA_PDU_MAX_PYLD;
 #endif
 }
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 void
 ble_phy_resolv_list_enable(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/drivers/nrf52/src/ble_hw.c
----------------------------------------------------------------------
diff --git a/net/nimble/drivers/nrf52/src/ble_hw.c b/net/nimble/drivers/nrf52/src/ble_hw.c
index 66efb08..569596c 100644
--- a/net/nimble/drivers/nrf52/src/ble_hw.c
+++ b/net/nimble/drivers/nrf52/src/ble_hw.c
@@ -20,6 +20,7 @@
 #include <stdint.h>
 #include <assert.h>
 #include <string.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "ble/xcvr.h"
 #include "nimble/ble.h"
@@ -28,6 +29,8 @@
 #include "controller/ble_hw.h"
 #include "bsp/cmsis_nvic.h"
 
+#include "nrf52_bitfields.h"
+
 /* Total number of resolving list elements */
 #define BLE_HW_RESOLV_LIST_SIZE     (16)
 
@@ -38,11 +41,11 @@ static uint8_t g_ble_hw_whitelist_mask;
 ble_rng_isr_cb_t g_ble_rng_isr_cb;
 
 /* If LL privacy is enabled, allocate memory for AAR */
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 
 /* The NRF51 supports up to 16 IRK entries */
-#if (NIMBLE_OPT_LL_RESOLV_LIST_SIZE < 16)
-#define NRF_IRK_LIST_ENTRIES    (NIMBLE_OPT_LL_RESOLV_LIST_SIZE)
+#if (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE) < 16)
+#define NRF_IRK_LIST_ENTRIES    (MYNEWT_VAL(BLE_LL_RESOLV_LIST_SIZE))
 #else
 #define NRF_IRK_LIST_ENTRIES    (16)
 #endif
@@ -339,7 +342,7 @@ ble_hw_rng_read(void)
     return rnum;
 }
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY))
 /**
  * Clear the resolving list
  *



[38/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/test/arch/sim/nffs_test_priv.h
----------------------------------------------------------------------
diff --git a/fs/nffs/src/test/arch/sim/nffs_test_priv.h b/fs/nffs/src/test/arch/sim/nffs_test_priv.h
deleted file mode 100644
index a3508c1..0000000
--- a/fs/nffs/src/test/arch/sim/nffs_test_priv.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/**
- * 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_NFFS_TEST_PRIV_
-#define H_NFFS_TEST_PRIV_
-
-struct nffs_test_block_desc {
-    const char *data;
-    int data_len;
-};
-
-struct nffs_test_file_desc {
-    const char *filename;
-    int is_dir;
-    const char *contents;
-    int contents_len;
-    struct nffs_test_file_desc *children;
-};
-
-int nffs_test(void);
-
-extern const struct nffs_test_file_desc *nffs_test_system_01;
-extern const struct nffs_test_file_desc *nffs_test_system_01_rm_1014_mk10;
-
-#endif
-


[32/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/arduino_primo_nrf52/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/arduino_primo_nrf52/pkg.yml b/hw/bsp/arduino_primo_nrf52/pkg.yml
index c49bc24..63fe461 100644
--- a/hw/bsp/arduino_primo_nrf52/pkg.yml
+++ b/hw/bsp/arduino_primo_nrf52/pkg.yml
@@ -33,12 +33,63 @@ pkg.linkerscript: "primo.ld"
 pkg.linkerscript.bootloader.OVERWRITE: "boot-primo.ld"
 pkg.downloadscript: primo_download.sh
 pkg.debugscript: primo_debug.sh
-pkg.cflags: -DNRF52 -DSPI_MASTER_0_ENABLE
+pkg.cflags:
+    # Nordic SDK files require these defines.
+    - '-DNRF52'
+    - '-DSPI_MASTER_0_ENABLE'
+
 pkg.deps:
-    - hw/mcu/nordic
     - hw/mcu/nordic/nrf52xxx
     - libs/baselibc
-    - drivers/uart/uart_hal
-    - drivers/uart/uart_bitbang
+
 pkg.deps.BLE_DEVICE:
     - net/nimble/drivers/nrf52
+
+pkg.deps.ADC_0:
+    - drivers/adc/adc_nrf52
+
+pkg.deps.UART_0:
+    - drivers/uart/uart_hal
+
+pkg.deps.UART_1:
+    - drivers/uart/uart_bitbang
+
+pkg.syscfg_defs:
+    BSP_NRF52:
+        description: 'TBD'
+        value: 1
+
+    CLOCK_FREQ:
+        description: 'TBD'
+        value:  1000000
+
+    XTAL_32768:
+        description: 'TBD'
+        value: 1
+
+    ADC_0:
+        description: 'TBD'
+        value:  1
+    ADC_0_RESOLUTION:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_RESOLUTION'
+    ADC_0_OVERSAMPLE:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_OVERSAMPLE'
+    ADC_0_INTERRUPT_PRIORITY:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_IRQ_PRIORITY'
+
+    UART_0:
+        description: 'TBD'
+        value:  1
+    UART_1:
+        description: 'TBD'
+        value:  0
+
+    SPI_MASTER:
+        description: 'TBD'
+        value:  1
+    SPI_SLAVE:
+        description: 'TBD'
+        value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/bmd300eval/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/bmd300eval/pkg.yml b/hw/bsp/bmd300eval/pkg.yml
index 5cc570d..0035ce2 100644
--- a/hw/bsp/bmd300eval/pkg.yml
+++ b/hw/bsp/bmd300eval/pkg.yml
@@ -32,10 +32,63 @@ pkg.linkerscript: "bmd300eval.ld"
 pkg.linkerscript.bootloader.OVERWRITE: "boot-bmd300eval.ld"
 pkg.downloadscript: bmd300eval_download.sh
 pkg.debugscript: bmd300eval_debug.sh
-pkg.cflags: -DNRF52 -DBSP_HAS_32768_XTAL -DSPI_MASTER_0_ENABLE
+pkg.cflags:
+    # Nordic SDK files require these defines.
+    - '-DNRF52'
+    - '-DSPI_MASTER_0_ENABLE'
+
 pkg.deps:
-    - hw/mcu/nordic
     - hw/mcu/nordic/nrf52xxx
     - libs/baselibc
+
 pkg.deps.BLE_DEVICE:
     - net/nimble/drivers/nrf52
+
+pkg.deps.ADC_0:
+    - drivers/adc/adc_nrf52
+
+pkg.deps.UART_0:
+    - drivers/uart/uart_hal
+
+pkg.deps.UART_1:
+    - drivers/uart/uart_bitbang
+
+pkg.syscfg_defs:
+    BSP_NRF52:
+        description: 'TBD'
+        value: 1
+
+    CLOCK_FREQ:
+        description: 'TBD'
+        value:  1000000
+
+    XTAL_32768:
+        description: 'TBD'
+        value: 1
+
+    ADC_0:
+        description: 'TBD'
+        value:  1
+    ADC_0_RESOLUTION:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_RESOLUTION'
+    ADC_0_OVERSAMPLE:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_OVERSAMPLE'
+    ADC_0_INTERRUPT_PRIORITY:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_IRQ_PRIORITY'
+
+    UART_0:
+        description: 'TBD'
+        value:  1
+    UART_1:
+        description: 'TBD'
+        value:  0
+
+    SPI_MASTER:
+        description: 'TBD'
+        value:  1
+    SPI_SLAVE:
+        description: 'TBD'
+        value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/native/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/native/pkg.yml b/hw/bsp/native/pkg.yml
index 0de14d6..ede1525 100644
--- a/hw/bsp/native/pkg.yml
+++ b/hw/bsp/native/pkg.yml
@@ -25,11 +25,12 @@ pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
     - native
     - bsp
+pkg.debugscript: "native_debug.sh"
 
 pkg.arch: sim
 pkg.compiler: compiler/sim
 pkg.deps:
     - hw/mcu/native
+
 pkg.deps.BLE_DEVICE:
     - net/nimble/drivers/native
-pkg.debugscript: "native_debug.sh"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/native/src/hal_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/native/src/hal_bsp.c b/hw/bsp/native/src/hal_bsp.c
index dadee14..d65ac11 100644
--- a/hw/bsp/native/src/hal_bsp.c
+++ b/hw/bsp/native/src/hal_bsp.c
@@ -23,7 +23,6 @@
 #include <inttypes.h>
 #include "hal/hal_flash_int.h"
 #include "mcu/native_bsp.h"
-#include "hal/hal_adc_int.h"
 #include "bsp/bsp_sysid.h"
 #include "mcu/mcu_hal.h"
 #include <bsp/bsp.h>
@@ -31,7 +30,6 @@
 #include <mcu/hal_pwm.h>
 #include <mcu/hal_dac.h>
 
-
 const struct hal_flash *
 bsp_flash_dev(uint8_t id)
 {
@@ -43,76 +41,3 @@ bsp_flash_dev(uint8_t id)
     }
     return &native_flash_dev;
 }
-
-/* This is the factory that creates adc devices for the application.
- * Based on the system ID defined, this maps to a creation function for
- * the specific underlying driver */
-extern struct hal_adc *
-bsp_get_hal_adc(enum system_device_id sysid) 
-{    
-    switch(sysid) {
-        case NATIVE_A0:
-            return native_adc_random_create(MCU_ADC_CHANNEL_0);
-        case NATIVE_A1:
-            return native_adc_random_create(MCU_ADC_CHANNEL_1);
-        case NATIVE_A2:
-            return native_adc_mmm_create(MCU_ADC_CHANNEL_2);
-        case NATIVE_A3:
-            return native_adc_mmm_create(MCU_ADC_CHANNEL_3);
-        case NATIVE_A4:
-            return native_adc_mmm_create(MCU_ADC_CHANNEL_4);
-        case NATIVE_A5:        
-            /* for this BSP we hard code the name of the ADC sample file */
-            return native_adc_file_create(MCU_ADC_CHANNEL_5, "foo.txt");
-        default:
-            break;
-    }
-    return NULL;
-}
-
-struct hal_pwm *
-bsp_get_hal_pwm_driver(enum system_device_id sysid) 
-{
-    switch(sysid) 
-    {
-        case NATIVE_BSP_PWM_0:
-            return native_pwm_create (NATIVE_MCU_PWM0);
-        case NATIVE_BSP_PWM_1:
-            return native_pwm_create (NATIVE_MCU_PWM1);
-        case NATIVE_BSP_PWM_2:
-            return native_pwm_create (NATIVE_MCU_PWM2);
-        case NATIVE_BSP_PWM_3:
-            return native_pwm_create (NATIVE_MCU_PWM3);
-        case NATIVE_BSP_PWM_4:
-            return native_pwm_create (NATIVE_MCU_PWM4);
-        case NATIVE_BSP_PWM_5:
-            return native_pwm_create (NATIVE_MCU_PWM5);
-        case NATIVE_BSP_PWM_6:
-            return native_pwm_create (NATIVE_MCU_PWM6);
-        case NATIVE_BSP_PWM_7:
-            return native_pwm_create (NATIVE_MCU_PWM7);
-        default:
-            break;
-    }
-    return NULL;
-}
-
-
-struct hal_dac *
-bsp_get_hal_dac(enum system_device_id sysid) 
-{
-    switch(sysid) 
-    {
-        case NATIVE_BSP_DAC_0:
-            return native_dac_create(NATIVE_MCU_DAC0);
-        case NATIVE_BSP_DAC_1:
-            return native_dac_create(NATIVE_MCU_DAC1);
-        case NATIVE_BSP_DAC_2:
-            return native_dac_create(NATIVE_MCU_DAC2);
-        case NATIVE_BSP_DAC_3:
-            return native_dac_create(NATIVE_MCU_DAC3);
-        default:
-            break;
-    }
-    return NULL;   
-}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/nrf51-arduino_101/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51-arduino_101/pkg.yml b/hw/bsp/nrf51-arduino_101/pkg.yml
index ee29287..05ff47a 100644
--- a/hw/bsp/nrf51-arduino_101/pkg.yml
+++ b/hw/bsp/nrf51-arduino_101/pkg.yml
@@ -32,9 +32,62 @@ pkg.linkerscript: "nrf51-arduino_101.ld"
 pkg.linkerscript.bootloader.OVERWRITE: "boot-nrf51-arduino_101.ld"
 pkg.downloadscript: nrf51dk-16kbram_download.sh
 pkg.debugscript: nrf51dk-16kbram_debug.sh
-pkg.cflags: -DNRF51
+pkg.cflags:
+    # Nordic SDK files require these defines.
+    - '-DNRF52'
+
 pkg.deps:
     - hw/mcu/nordic/nrf51xxx
     - libs/baselibc
+
 pkg.deps.BLE_DEVICE:
     - net/nimble/drivers/nrf51
+
+pkg.deps.ADC_0:
+    - drivers/adc/adc_nrf51
+
+pkg.deps.UART_0:
+    - drivers/uart/uart_hal
+
+pkg.deps.UART_1:
+    - drivers/uart/uart_bitbang
+
+pkg.syscfg_defs:
+    BSP_NRF51:
+        description: 'TBD'
+        value: 1
+
+    CLOCK_FREQ:
+        description: 'TBD'
+        value:  1000000
+
+    XTAL_32768:
+        description: 'TBD'
+        value: 1
+
+    ADC_0:
+        description: 'TBD'
+        value:  1
+    ADC_0_RESOLUTION:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_RESOLUTION'
+    ADC_0_OVERSAMPLE:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_OVERSAMPLE'
+    ADC_0_INTERRUPT_PRIORITY:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_IRQ_PRIORITY'
+
+    UART_0:
+        description: 'TBD'
+        value:  1
+    UART_1:
+        description: 'TBD'
+        value:  0
+
+    SPI_MASTER:
+        description: 'TBD'
+        value:  1
+    SPI_SLAVE:
+        description: 'TBD'
+        value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/nrf51-blenano/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51-blenano/pkg.yml b/hw/bsp/nrf51-blenano/pkg.yml
index ea41d92..20cd656 100644
--- a/hw/bsp/nrf51-blenano/pkg.yml
+++ b/hw/bsp/nrf51-blenano/pkg.yml
@@ -33,9 +33,62 @@ pkg.linkerscript: "nrf51dk.ld"
 pkg.linkerscript.bootloader.OVERWRITE: "boot-nrf51dk.ld"
 pkg.downloadscript: nrf51dk_download.sh
 pkg.debugscript: nrf51dk_debug.sh
-pkg.cflags: -DNRF51
+pkg.cflags:
+    # Nordic SDK files require these defines.
+    - '-DNRF52'
+
 pkg.deps:
     - hw/mcu/nordic/nrf51xxx
     - libs/baselibc
+
 pkg.deps.BLE_DEVICE:
     - net/nimble/drivers/nrf51
+
+pkg.deps.ADC_0:
+    - drivers/adc/adc_nrf51
+
+pkg.deps.UART_0:
+    - drivers/uart/uart_hal
+
+pkg.deps.UART_1:
+    - drivers/uart/uart_bitbang
+
+pkg.syscfg_defs:
+    BSP_NRF51:
+        description: 'TBD'
+        value: 1
+
+    CLOCK_FREQ:
+        description: 'TBD'
+        value:  1000000
+
+    XTAL_32768:
+        description: 'TBD'
+        value: 1
+
+    ADC_0:
+        description: 'TBD'
+        value:  1
+    ADC_0_RESOLUTION:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_RESOLUTION'
+    ADC_0_OVERSAMPLE:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_OVERSAMPLE'
+    ADC_0_INTERRUPT_PRIORITY:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_IRQ_PRIORITY'
+
+    UART_0:
+        description: 'TBD'
+        value:  1
+    UART_1:
+        description: 'TBD'
+        value:  0
+
+    SPI_MASTER:
+        description: 'TBD'
+        value:  1
+    SPI_SLAVE:
+        description: 'TBD'
+        value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/nrf51dk-16kbram/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk-16kbram/pkg.yml b/hw/bsp/nrf51dk-16kbram/pkg.yml
index 5273846..c9c11d2 100644
--- a/hw/bsp/nrf51dk-16kbram/pkg.yml
+++ b/hw/bsp/nrf51dk-16kbram/pkg.yml
@@ -32,10 +32,62 @@ pkg.linkerscript: "nrf51dk-16kbram.ld"
 pkg.linkerscript.bootloader.OVERWRITE: "boot-nrf51dk-16kbram.ld"
 pkg.downloadscript: nrf51dk-16kbram_download.sh
 pkg.debugscript: nrf51dk-16kbram_debug.sh
-pkg.cflags: -DNRF51
+pkg.cflags:
+    # Nordic SDK files require these defines.
+    - '-DNRF52'
+
 pkg.deps:
     - hw/mcu/nordic/nrf51xxx
     - libs/baselibc
-    - drivers/uart/uart_hal
+
 pkg.deps.BLE_DEVICE:
     - net/nimble/drivers/nrf51
+
+pkg.deps.ADC_0:
+    - drivers/adc/adc_nrf51
+
+pkg.deps.UART_0:
+    - drivers/uart/uart_hal
+
+pkg.deps.UART_1:
+    - drivers/uart/uart_bitbang
+
+pkg.syscfg_defs:
+    BSP_NRF51:
+        description: 'TBD'
+        value: 1
+
+    CLOCK_FREQ:
+        description: 'TBD'
+        value:  1000000
+
+    XTAL_32768:
+        description: 'TBD'
+        value: 1
+
+    ADC_0:
+        description: 'TBD'
+        value:  1
+    ADC_0_RESOLUTION:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_RESOLUTION'
+    ADC_0_OVERSAMPLE:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_OVERSAMPLE'
+    ADC_0_INTERRUPT_PRIORITY:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_IRQ_PRIORITY'
+
+    UART_0:
+        description: 'TBD'
+        value:  1
+    UART_1:
+        description: 'TBD'
+        value:  0
+
+    SPI_MASTER:
+        description: 'TBD'
+        value:  1
+    SPI_SLAVE:
+        description: 'TBD'
+        value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/nrf51dk/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/pkg.yml b/hw/bsp/nrf51dk/pkg.yml
index 6b3705d..c6d66bc 100644
--- a/hw/bsp/nrf51dk/pkg.yml
+++ b/hw/bsp/nrf51dk/pkg.yml
@@ -32,12 +32,56 @@ pkg.linkerscript: "nrf51dk.ld"
 pkg.linkerscript.bootloader.OVERWRITE: "boot-nrf51dk.ld"
 pkg.downloadscript: nrf51dk_download.sh
 pkg.debugscript: nrf51dk_debug.sh
-pkg.cflags: -DNRF51 -DBSP_HAS_32768_XTAL
+pkg.cflags:
+    # Nordic SDK files require this define.
+    - '-DNRF51'
+
 pkg.deps:
     - hw/mcu/nordic/nrf51xxx
     - libs/baselibc
-    - drivers/uart/uart_hal
+
 pkg.deps.BLE_DEVICE:
     - net/nimble/drivers/nrf51
-pkg.features:
-    - ADC_NRF51
+
+pkg.deps.ADC_0:
+    - drivers/adc/adc_nrf51
+
+pkg.deps.UART_0:
+    - drivers/uart/uart_hal
+
+pkg.syscfg_defs:
+    BSP_NRF51:
+        description: 'TBD'
+        value: 1
+
+    CLOCK_FREQ:
+        description: 'TBD'
+        value: 1000000
+
+    32768_XTAL:
+        description: 'TBD'
+        value: 1
+
+    ADC_0:
+        description: 'TBD'
+        value:  1
+    ADC_0_RESOLUTION:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_RESOLUTION'
+    ADC_0_OVERSAMPLE:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_OVERSAMPLE'
+    ADC_0_INTERRUPT_PRIORITY:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_IRQ_PRIORITY'
+
+    UART_0:
+        description: 'TBD'
+        value:  1
+
+    SPI_MASTER:
+        description: 'TBD'
+        value:  1
+    SPI_SLAVE:
+        description: 'TBD'
+        value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/nrf52dk/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf52dk/pkg.yml b/hw/bsp/nrf52dk/pkg.yml
index 41b4222..5e617e5 100644
--- a/hw/bsp/nrf52dk/pkg.yml
+++ b/hw/bsp/nrf52dk/pkg.yml
@@ -32,13 +32,63 @@ pkg.linkerscript: "nrf52dk.ld"
 pkg.linkerscript.bootloader.OVERWRITE: "boot-nrf52dk.ld"
 pkg.downloadscript: nrf52dk_download.sh
 pkg.debugscript: nrf52dk_debug.sh
-pkg.cflags: -DNRF52 -DBSP_HAS_32768_XTAL -DSPI_MASTER_0_ENABLE
+pkg.cflags:
+    # Nordic SDK files require these defines.
+    - '-DNRF52'
+    - '-DSPI_MASTER_0_ENABLE'
+
 pkg.deps:
-    - hw/mcu/nordic
     - hw/mcu/nordic/nrf52xxx
     - libs/baselibc
-    - drivers/uart/uart_hal
+
 pkg.deps.BLE_DEVICE:
     - net/nimble/drivers/nrf52
-pkg.features:
-    - ADC_NRF52
+
+pkg.deps.ADC_0:
+    - drivers/adc/adc_nrf52
+
+pkg.deps.UART_0:
+    - drivers/uart/uart_hal
+
+pkg.deps.UART_1:
+    - drivers/uart/uart_bitbang
+
+pkg.syscfg_defs:
+    BSP_NRF52:
+        description: 'TBD'
+        value: 1
+
+    CLOCK_FREQ:
+        description: 'TBD'
+        value:  1000000
+
+    XTAL_32768:
+        description: 'TBD'
+        value: 1
+
+    ADC_0:
+        description: 'TBD'
+        value:  1
+    ADC_0_RESOLUTION:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_RESOLUTION'
+    ADC_0_OVERSAMPLE:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_OVERSAMPLE'
+    ADC_0_INTERRUPT_PRIORITY:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_IRQ_PRIORITY'
+
+    UART_0:
+        description: 'TBD'
+        value:  1
+    UART_1:
+        description: 'TBD'
+        value:  0
+
+    SPI_MASTER:
+        description: 'TBD'
+        value:  1
+    SPI_SLAVE:
+        description: 'TBD'
+        value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/nrf52dk/src/os_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf52dk/src/os_bsp.c b/hw/bsp/nrf52dk/src/os_bsp.c
index 0a521af..1b3cfdd 100644
--- a/hw/bsp/nrf52dk/src/os_bsp.c
+++ b/hw/bsp/nrf52dk/src/os_bsp.c
@@ -16,25 +16,32 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+
 #include <assert.h>
-#include <hal/flash_map.h>
-#include <hal/hal_bsp.h>
-#include <hal/hal_cputime.h>
-#include <mcu/nrf52_hal.h>
 
-#include <os/os_dev.h>
+#include "syscfg/syscfg.h"
+#include "hal/flash_map.h"
+#include "hal/hal_bsp.h"
+#include "hal/hal_cputime.h"
+#include "hal/hal_flash.h"
+#include "hal/hal_spi.h"
+#include "mcu/nrf52_hal.h"
+#include "uart/uart.h"
+#include "uart_hal/uart_hal.h"
+#include "os/os_dev.h"
 
-#include <uart/uart.h>
-#include <uart_hal/uart_hal.h>
-#include <hal/hal_spi.h>
-#ifdef BSP_CFG_SPI_MASTER
+#if MYNEWT_VAL(SPI_MASTER)
 #include "nrf_drv_spi.h"
 #endif
-#ifdef BSP_CFG_SPI_SLAVE
+#if MYNEWT_VAL(SPI_SLAVE)
 #include "nrf_drv_spis.h"
 #endif
 #include "nrf_drv_config.h"
-#include <app_util_platform.h>
+#include "app_util_platform.h"
+#include "nrf.h"
+#include "app_error.h"
+#include "adc_nrf52/adc_nrf52.h"
+#include "nrf_drv_saadc.h"
 
 static struct flash_area bsp_flash_areas[] = {
     [FLASH_AREA_BOOTLOADER] = {
@@ -64,9 +71,21 @@ static struct flash_area bsp_flash_areas[] = {
         .fa_size = (12 * 1024)
     }
 };
-static struct uart_dev hal_uart0;
 
-void _close(int fd);
+#if MYNEWT_VAL(UART_0)
+static struct uart_dev os_bsp_uart0;
+#endif
+
+#if MYNEWT_VAL(ADC_0)
+static struct adc_dev os_bsp_adc0;
+static nrf_drv_saadc_config_t os_bsp_adc0_config = {
+    .resolution         = MYNEWT_VAL(ADC_0_RESOLUTION),
+    .oversample         = MYNEWT_VAL(ADC_0_OVERSAMPLE),
+    .interrupt_priority = MYNEWT_VAL(ADC_0_INTERRUPT_PRIORITY),
+};
+#endif
+
+//void _close(int fd);
 
 /*
  * Returns the flash map slot where the currently active image is located.
@@ -86,36 +105,53 @@ void
 bsp_init(void)
 {
     int rc;
-#ifdef BSP_CFG_SPI_MASTER
+
+#if MYNEWT_VAL(SPI_MASTER)
     nrf_drv_spi_config_t spi_cfg = NRF_DRV_SPI_DEFAULT_CONFIG(0);
 #endif
-#ifdef BSP_CFG_SPI_SLAVE
+#if MYNEWT_VAL(SPI_SLAVE)
     nrf_drv_spis_config_t spi_cfg = NRF_DRV_SPIS_DEFAULT_CONFIG(0);
 #endif
 
     /*
      * XXX this reference is here to keep this function in.
      */
-    _sbrk(0);
-    _close(0);
+    (void)_sbrk;
+    //(void)_close;
+
+    /* Set cputime to count at 1 usec increments */
+    rc = cputime_init(MYNEWT_VAL(CLOCK_FREQ));
+    assert(rc == 0);
 
     flash_area_init(bsp_flash_areas,
       sizeof(bsp_flash_areas) / sizeof(bsp_flash_areas[0]));
 
-    rc = os_dev_create((struct os_dev *) &hal_uart0, "uart0",
-      OS_DEV_INIT_PRIMARY, 0, uart_hal_init, (void *)bsp_uart_config());
+    rc = hal_flash_init();
     assert(rc == 0);
 
-#ifdef BSP_CFG_SPI_MASTER
+#if MYNEWT_VAL(SPI_MASTER)
     /*  We initialize one SPI interface as a master. */
     rc = hal_spi_init(0, &spi_cfg, HAL_SPI_TYPE_MASTER);
     assert(rc == 0);
 #endif
 
-#ifdef BSP_CFG_SPI_SLAVE
+#if MYNEWT_VAL(SPI_SLAVE)
     /*  We initialize one SPI interface as a master. */
     spi_cfg.csn_pin = SPIS0_CONFIG_CSN_PIN;
     rc = hal_spi_init(0, &spi_cfg, HAL_SPI_TYPE_SLAVE);
     assert(rc == 0);
 #endif
+
+#if MYNEWT_VAL(UART_0)
+    rc = os_dev_create((struct os_dev *) &os_bsp_uart0, "uart0",
+      OS_DEV_INIT_PRIMARY, 0, uart_hal_init, (void *)bsp_uart_config());
+    assert(rc == 0);
+#endif
+
+#if MYNEWT_VAL(ADC_0)
+    rc = os_dev_create((struct os_dev *) &os_bsp_adc0, "adc0",
+            OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
+            nrf52_adc_dev_init, &os_bsp_adc0_config);
+    assert(rc == 0);
+#endif
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/nrf52pdk/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf52pdk/pkg.yml b/hw/bsp/nrf52pdk/pkg.yml
index 6bc7ef4..d57f7e2 100644
--- a/hw/bsp/nrf52pdk/pkg.yml
+++ b/hw/bsp/nrf52pdk/pkg.yml
@@ -32,11 +32,63 @@ pkg.linkerscript: "nrf52pdk.ld"
 pkg.linkerscript.bootloader.OVERWRITE: "boot-nrf52pdk.ld"
 pkg.downloadscript: nrf52pdk_download.sh
 pkg.debugscript: nrf52pdk_debug.sh
-pkg.cflags: -DNRF52 -DSPI_MASTER_0_ENABLE
+pkg.cflags:
+    # Nordic SDK files require these defines.
+    - '-DNRF52'
+    - '-DSPI_MASTER_0_ENABLE'
+
 pkg.deps:
-    - hw/mcu/nordic
     - hw/mcu/nordic/nrf52xxx
     - libs/baselibc
-    - drivers/uart/uart_hal
+
 pkg.deps.BLE_DEVICE:
     - net/nimble/drivers/nrf52
+
+pkg.deps.ADC_0:
+    - drivers/adc/adc_nrf52
+
+pkg.deps.UART_0:
+    - drivers/uart/uart_hal
+
+pkg.deps.UART_1:
+    - drivers/uart/uart_bitbang
+
+pkg.syscfg_defs:
+    BSP_NRF52:
+        description: 'TBD'
+        value: 1
+
+    CLOCK_FREQ:
+        description: 'TBD'
+        value:  1000000
+
+    XTAL_32768:
+        description: 'TBD'
+        value: 0
+
+    ADC_0:
+        description: 'TBD'
+        value:  0
+    ADC_0_RESOLUTION:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_RESOLUTION'
+    ADC_0_OVERSAMPLE:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_OVERSAMPLE'
+    ADC_0_INTERRUPT_PRIORITY:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_IRQ_PRIORITY'
+
+    UART_0:
+        description: 'TBD'
+        value:  1
+    UART_1:
+        description: 'TBD'
+        value:  0
+
+    SPI_MASTER:
+        description: 'TBD'
+        value:  0
+    SPI_SLAVE:
+        description: 'TBD'
+        value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/bsp/olimex_stm32-e407_devboard/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/olimex_stm32-e407_devboard/pkg.yml b/hw/bsp/olimex_stm32-e407_devboard/pkg.yml
index 761d9ba..3b23131 100644
--- a/hw/bsp/olimex_stm32-e407_devboard/pkg.yml
+++ b/hw/bsp/olimex_stm32-e407_devboard/pkg.yml
@@ -33,8 +33,51 @@ pkg.linkerscript: "olimex_stm32-e407_devboard.ld"
 pkg.linkerscript.bootloader.OVERWRITE: "boot-olimex_stm32-e407_devboard.ld"
 pkg.downloadscript: "olimex_stm32-e407_devboard_download.sh"
 pkg.debugscript: "olimex_stm32-e407_devboard_debug.sh"
-pkg.cflags: -DSTM32F407xx
+pkg.cflags:
+    # STM SDK files require these defines.
+    - '-DSTM32F407xx'
+
 pkg.deps:
     - hw/mcu/stm/stm32f4xx
     - libs/baselibc
+
+pkg.deps.ADC_0:
+    - drivers/adc/adc_nrf52
+
+pkg.deps.UART_0:
     - drivers/uart/uart_hal
+
+pkg.deps.UART_1:
+    - drivers/uart/uart_bitbang
+
+pkg.syscfg_defs:
+    CLOCK_FREQ:
+        description: 'TBD'
+        value:  1000000
+
+    ADC_0:
+        description: 'TBD'
+        value:  1
+    ADC_0_RESOLUTION:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_RESOLUTION'
+    ADC_0_OVERSAMPLE:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_OVERSAMPLE'
+    ADC_0_INTERRUPT_PRIORITY:
+        description: 'TBD'
+        value: 'SAADC_CONFIG_IRQ_PRIORITY'
+
+    UART_0:
+        description: 'TBD'
+        value:  1
+    UART_1:
+        description: 'TBD'
+        value:  0
+
+    SPI_MASTER:
+        description: 'TBD'
+        value:  1
+    SPI_SLAVE:
+        description: 'TBD'
+        value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/hal/include/hal/flash_map.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/flash_map.h b/hw/hal/include/hal/flash_map.h
index 1bf07ee..a073078 100644
--- a/hw/hal/include/hal/flash_map.h
+++ b/hw/hal/include/hal/flash_map.h
@@ -61,6 +61,9 @@ struct flash_area {
 #define FLASH_AREA_CORE                 2
 #define FLASH_AREA_REBOOT_LOG           5
 
+extern const struct flash_area *flash_map;
+extern int flash_map_entries;
+
 /*
  * Initializes flash map. Memory will be referenced by flash_map code
  * from this on.
@@ -97,7 +100,7 @@ int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret);
  * Given flash map index, return sector info in NFFS area desc format.
  */
 struct nffs_area_desc;
-int flash_area_to_nffs_desc(int idx, int *cnt, struct nffs_area_desc *nad);
+int nffs_misc_desc_from_flash_area(int idx, int *cnt, struct nffs_area_desc *nad);
 
 #ifdef __cplusplus
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/hal/include/hal/hal_bsp.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_bsp.h b/hw/hal/include/hal/hal_bsp.h
index 8ca8a69..c5d0ba1 100644
--- a/hw/hal/include/hal/hal_bsp.h
+++ b/hw/hal/include/hal/hal_bsp.h
@@ -29,6 +29,7 @@ extern "C" {
 /*
  * Initializes BSP; registers flash_map with the system.
  */
+void bsp_init_devs(void);
 void bsp_init(void);
 
 /*

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/hal/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/hal/pkg.yml b/hw/hal/pkg.yml
index f350040..2049849 100644
--- a/hw/hal/pkg.yml
+++ b/hw/hal/pkg.yml
@@ -24,6 +24,3 @@ pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
 
 pkg.deps: libs/os
-pkg.deps.NFFS:
-    - fs/nffs
-pkg.cflags.NFFS: -DNFFS_PRESENT

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/hal/src/flash_map.c
----------------------------------------------------------------------
diff --git a/hw/hal/src/flash_map.c b/hw/hal/src/flash_map.c
index 4b06c29..d66b0ad 100644
--- a/hw/hal/src/flash_map.c
+++ b/hw/hal/src/flash_map.c
@@ -19,16 +19,14 @@
 #include <inttypes.h>
 #include <string.h>
 
+#include "syscfg/syscfg.h"
 #include "hal/hal_bsp.h"
 #include "hal/hal_flash.h"
 #include "hal/hal_flash_int.h"
-#ifdef NFFS_PRESENT
-#include <nffs/nffs.h>
-#endif
 #include "hal/flash_map.h"
 
-static const struct flash_area *flash_map;
-static int flash_map_entries;
+const struct flash_area *flash_map;
+int flash_map_entries;
 
 void
 flash_area_init(const struct flash_area *map, int map_entries)
@@ -86,74 +84,6 @@ flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
     return 0;
 }
 
-#ifdef NFFS_PRESENT
-/*
- * Turn flash region into a set of areas for NFFS use.
- *
- * Limit the number of regions we return to be less than *cnt.
- * If sector count within region exceeds that, collect multiple sectors
- * to a region.
- */
-int
-flash_area_to_nffs_desc(int idx, int *cnt, struct nffs_area_desc *nad)
-{
-    int i, j;
-    const struct hal_flash *hf;
-    const struct flash_area *fa;
-    int max_cnt, move_on;
-    int first_idx, last_idx;
-    uint32_t start, size;
-    uint32_t min_size;
-
-    if (!flash_map || idx >= flash_map_entries) {
-        return -1;
-    }
-    first_idx = last_idx = -1;
-    max_cnt = *cnt;
-    *cnt = 0;
-
-    fa = &flash_map[idx];
-
-    hf = bsp_flash_dev(fa->fa_flash_id);
-    for (i = 0; i < hf->hf_sector_cnt; i++) {
-        hf->hf_itf->hff_sector_info(i, &start, &size);
-        if (start >= fa->fa_off && start < fa->fa_off + fa->fa_size) {
-            if (first_idx == -1) {
-                first_idx = i;
-            }
-            last_idx = i;
-            *cnt = *cnt + 1;
-        }
-    }
-    if (*cnt > max_cnt) {
-        min_size = fa->fa_size / max_cnt;
-    } else {
-        min_size = 0;
-    }
-    *cnt = 0;
-
-    move_on = 1;
-    for (i = first_idx, j = 0; i < last_idx + 1; i++) {
-        hf->hf_itf->hff_sector_info(i, &start, &size);
-        if (move_on) {
-            nad[j].nad_flash_id = fa->fa_flash_id;
-            nad[j].nad_offset = start;
-            nad[j].nad_length = size;
-            *cnt = *cnt + 1;
-            move_on = 0;
-        } else {
-            nad[j].nad_length += size;
-        }
-        if (nad[j].nad_length >= min_size) {
-            j++;
-            move_on = 1;
-        }
-    }
-    nad[*cnt].nad_length = 0;
-    return 0;
-}
-#endif /* NFFS_PRESENT */
-
 int
 flash_area_read(const struct flash_area *fa, uint32_t off, void *dst,
   uint32_t len)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/hal/src/test/flash_map_test.c
----------------------------------------------------------------------
diff --git a/hw/hal/src/test/flash_map_test.c b/hw/hal/src/test/flash_map_test.c
deleted file mode 100644
index 06e065d..0000000
--- a/hw/hal/src/test/flash_map_test.c
+++ /dev/null
@@ -1,169 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <string.h>
-
-#include <os/os.h>
-#include <testutil/testutil.h>
-#include "hal/flash_map.h"
-#include "hal/hal_bsp.h"
-#include "hal/hal_flash.h"
-#include "hal/hal_flash_int.h"
-
-/*
- * Test flash_area_to_sectors()
- */
-TEST_CASE(flash_map_test_case_1)
-{
-    const struct flash_area *fa;
-    int areas_checked = 0;
-    int i, j, rc;
-    const struct hal_flash *hf;
-    struct flash_area my_secs[32];
-    int my_sec_cnt;
-    uint32_t end;
-
-    os_init();
-
-    for (i = 0; i < 8; i++) {
-        rc = flash_area_open(i, &fa);
-        if (rc) {
-            continue;
-        }
-
-        hf = bsp_flash_dev(fa->fa_flash_id);
-        TEST_ASSERT_FATAL(hf != NULL, "bsp_flash_dev");
-
-        rc = flash_area_to_sectors(i, &my_sec_cnt, my_secs);
-        TEST_ASSERT_FATAL(rc == 0, "flash_area_to_sectors failed");
-
-        end = fa->fa_off;
-        for (j = 0; j < my_sec_cnt; j++) {
-            TEST_ASSERT_FATAL(end == my_secs[j].fa_off, "Non contiguous area");
-            TEST_ASSERT_FATAL(my_secs[j].fa_flash_id == fa->fa_flash_id,
-              "Sectors not in same flash?");
-            end = my_secs[j].fa_off + my_secs[j].fa_size;
-        }
-        if (my_sec_cnt) {
-            areas_checked++;
-            TEST_ASSERT_FATAL(my_secs[my_sec_cnt - 1].fa_off +
-              my_secs[my_sec_cnt - 1].fa_size == fa->fa_off + fa->fa_size,
-              "Last sector not in the end");
-        }
-    }
-    TEST_ASSERT_FATAL(areas_checked != 0, "No flash map areas to check!");
-}
-
-/*
- * Test flash_erase
- */
-TEST_CASE(flash_map_test_case_2)
-{
-    const struct flash_area *fa;
-    struct flash_area secs[32];
-    int sec_cnt;
-    int i;
-    int rc;
-    uint32_t off;
-    uint8_t wd[256];
-    uint8_t rd[256];
-
-    os_init();
-
-    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fa);
-    TEST_ASSERT_FATAL(rc == 0, "flash_area_open() fail");
-
-    rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &sec_cnt, secs);
-    TEST_ASSERT_FATAL(rc == 0, "flash_area_to_sectors failed");
-
-    /*
-     * First erase the area so it's ready for use.
-     */
-    for (i = 0; i < sec_cnt; i++) {
-        rc = hal_flash_erase_sector(secs[i].fa_flash_id, secs[i].fa_off);
-        TEST_ASSERT_FATAL(rc == 0, "hal_flash_erase_sector() failed");
-    }
-    TEST_ASSERT_FATAL(rc == 0, "read data != write data");
-
-    memset(wd, 0xa5, sizeof(wd));
-
-    /* write stuff to beginning of every sector */
-    off = 0;
-    for (i = 0; i < sec_cnt; i++) {
-        rc = flash_area_write(fa, off, wd, sizeof(wd));
-        TEST_ASSERT_FATAL(rc == 0, "flash_area_write() fail");
-
-        /* read it back via hal_flash_Read() */
-        rc = hal_flash_read(fa->fa_flash_id, fa->fa_off + off, rd, sizeof(rd));
-        TEST_ASSERT_FATAL(rc == 0, "hal_flash_read() fail");
-
-        rc = memcmp(wd, rd, sizeof(wd));
-        TEST_ASSERT_FATAL(rc == 0, "read data != write data");
-
-        /* write stuff to end of area */
-        rc = hal_flash_write(fa->fa_flash_id,
-          fa->fa_off + off + secs[i].fa_size - sizeof(wd), wd, sizeof(wd));
-        TEST_ASSERT_FATAL(rc == 0, "hal_flash_write() fail");
-
-        /* and read it back */
-        memset(rd, 0, sizeof(rd));
-        rc = flash_area_read(fa, off + secs[i].fa_size - sizeof(rd),
-          rd, sizeof(rd));
-        TEST_ASSERT_FATAL(rc == 0, "hal_flash_read() fail");
-
-        rc = memcmp(wd, rd, sizeof(rd));
-        TEST_ASSERT_FATAL(rc == 0, "read data != write data");
-
-        off += secs[i].fa_size;
-    }
-    /* erase it */
-    rc = flash_area_erase(fa, 0, fa->fa_size);
-    TEST_ASSERT_FATAL(rc == 0, "read data != write data");
-
-    /* should read back ff all throughout*/
-    memset(wd, 0xff, sizeof(wd));
-    for (off = 0; off < fa->fa_size; off += sizeof(rd)) {
-         rc = flash_area_read(fa, off, rd, sizeof(rd));
-         TEST_ASSERT_FATAL(rc == 0, "hal_flash_read() fail");
-
-         rc = memcmp(wd, rd, sizeof(rd));
-         TEST_ASSERT_FATAL(rc == 0, "area not erased");
-    }
-}
-
-TEST_SUITE(flash_map_test_suite)
-{
-    flash_map_test_case_1();
-    flash_map_test_case_2();
-}
-
-#ifdef MYNEWT_SELFTEST
-
-int
-main(int argc, char **argv)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    flash_map_test_suite();
-
-    return tu_any_failed;
-}
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/hal/test/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/hal/test/pkg.yml b/hw/hal/test/pkg.yml
new file mode 100644
index 0000000..943c0b3
--- /dev/null
+++ b/hw/hal/test/pkg.yml
@@ -0,0 +1,30 @@
+# 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.
+#
+pkg.name: hw/hal/test
+pkg.type: unittest
+pkg.description: "HAL unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+    - hw/hal
+    - libs/testutil
+
+pkg.deps.SELFTEST:
+    - libs/console/stub

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/hal/test/src/flash_map_test.c
----------------------------------------------------------------------
diff --git a/hw/hal/test/src/flash_map_test.c b/hw/hal/test/src/flash_map_test.c
new file mode 100644
index 0000000..71d1e45
--- /dev/null
+++ b/hw/hal/test/src/flash_map_test.c
@@ -0,0 +1,170 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+
+#include "syscfg/syscfg.h"
+#include "os/os.h"
+#include "testutil/testutil.h"
+#include "hal/flash_map.h"
+#include "hal/hal_bsp.h"
+#include "hal/hal_flash.h"
+#include "hal/hal_flash_int.h"
+
+/*
+ * Test flash_area_to_sectors()
+ */
+TEST_CASE(flash_map_test_case_1)
+{
+    const struct flash_area *fa;
+    int areas_checked = 0;
+    int i, j, rc;
+    const struct hal_flash *hf;
+    struct flash_area my_secs[32];
+    int my_sec_cnt;
+    uint32_t end;
+
+    os_init();
+
+    for (i = 0; i < 8; i++) {
+        rc = flash_area_open(i, &fa);
+        if (rc) {
+            continue;
+        }
+
+        hf = bsp_flash_dev(fa->fa_flash_id);
+        TEST_ASSERT_FATAL(hf != NULL, "bsp_flash_dev");
+
+        rc = flash_area_to_sectors(i, &my_sec_cnt, my_secs);
+        TEST_ASSERT_FATAL(rc == 0, "flash_area_to_sectors failed");
+
+        end = fa->fa_off;
+        for (j = 0; j < my_sec_cnt; j++) {
+            TEST_ASSERT_FATAL(end == my_secs[j].fa_off, "Non contiguous area");
+            TEST_ASSERT_FATAL(my_secs[j].fa_flash_id == fa->fa_flash_id,
+              "Sectors not in same flash?");
+            end = my_secs[j].fa_off + my_secs[j].fa_size;
+        }
+        if (my_sec_cnt) {
+            areas_checked++;
+            TEST_ASSERT_FATAL(my_secs[my_sec_cnt - 1].fa_off +
+              my_secs[my_sec_cnt - 1].fa_size == fa->fa_off + fa->fa_size,
+              "Last sector not in the end");
+        }
+    }
+    TEST_ASSERT_FATAL(areas_checked != 0, "No flash map areas to check!");
+}
+
+/*
+ * Test flash_erase
+ */
+TEST_CASE(flash_map_test_case_2)
+{
+    const struct flash_area *fa;
+    struct flash_area secs[32];
+    int sec_cnt;
+    int i;
+    int rc;
+    uint32_t off;
+    uint8_t wd[256];
+    uint8_t rd[256];
+
+    os_init();
+
+    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fa);
+    TEST_ASSERT_FATAL(rc == 0, "flash_area_open() fail");
+
+    rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &sec_cnt, secs);
+    TEST_ASSERT_FATAL(rc == 0, "flash_area_to_sectors failed");
+
+    /*
+     * First erase the area so it's ready for use.
+     */
+    for (i = 0; i < sec_cnt; i++) {
+        rc = hal_flash_erase_sector(secs[i].fa_flash_id, secs[i].fa_off);
+        TEST_ASSERT_FATAL(rc == 0, "hal_flash_erase_sector() failed");
+    }
+    TEST_ASSERT_FATAL(rc == 0, "read data != write data");
+
+    memset(wd, 0xa5, sizeof(wd));
+
+    /* write stuff to beginning of every sector */
+    off = 0;
+    for (i = 0; i < sec_cnt; i++) {
+        rc = flash_area_write(fa, off, wd, sizeof(wd));
+        TEST_ASSERT_FATAL(rc == 0, "flash_area_write() fail");
+
+        /* read it back via hal_flash_Read() */
+        rc = hal_flash_read(fa->fa_flash_id, fa->fa_off + off, rd, sizeof(rd));
+        TEST_ASSERT_FATAL(rc == 0, "hal_flash_read() fail");
+
+        rc = memcmp(wd, rd, sizeof(wd));
+        TEST_ASSERT_FATAL(rc == 0, "read data != write data");
+
+        /* write stuff to end of area */
+        rc = hal_flash_write(fa->fa_flash_id,
+          fa->fa_off + off + secs[i].fa_size - sizeof(wd), wd, sizeof(wd));
+        TEST_ASSERT_FATAL(rc == 0, "hal_flash_write() fail");
+
+        /* and read it back */
+        memset(rd, 0, sizeof(rd));
+        rc = flash_area_read(fa, off + secs[i].fa_size - sizeof(rd),
+          rd, sizeof(rd));
+        TEST_ASSERT_FATAL(rc == 0, "hal_flash_read() fail");
+
+        rc = memcmp(wd, rd, sizeof(rd));
+        TEST_ASSERT_FATAL(rc == 0, "read data != write data");
+
+        off += secs[i].fa_size;
+    }
+    /* erase it */
+    rc = flash_area_erase(fa, 0, fa->fa_size);
+    TEST_ASSERT_FATAL(rc == 0, "read data != write data");
+
+    /* should read back ff all throughout*/
+    memset(wd, 0xff, sizeof(wd));
+    for (off = 0; off < fa->fa_size; off += sizeof(rd)) {
+         rc = flash_area_read(fa, off, rd, sizeof(rd));
+         TEST_ASSERT_FATAL(rc == 0, "hal_flash_read() fail");
+
+         rc = memcmp(wd, rd, sizeof(rd));
+         TEST_ASSERT_FATAL(rc == 0, "area not erased");
+    }
+}
+
+TEST_SUITE(flash_map_test_suite)
+{
+    flash_map_test_case_1();
+    flash_map_test_case_2();
+}
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    flash_map_test_suite();
+
+    return tu_any_failed;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/mcu/native/include/mcu/mcu_hal.h
----------------------------------------------------------------------
diff --git a/hw/mcu/native/include/mcu/mcu_hal.h b/hw/mcu/native/include/mcu/mcu_hal.h
index e6163b9..aced4dc 100644
--- a/hw/mcu/native/include/mcu/mcu_hal.h
+++ b/hw/mcu/native/include/mcu/mcu_hal.h
@@ -26,8 +26,6 @@
 extern "C" {
 #endif
 
-#include <hal/hal_adc.h>
-    
 /* this is a native build and these pins are not real.  They are used only
  * for simulated HAL devices */
     

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/mcu/native/src/hal_adc.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_adc.c b/hw/mcu/native/src/hal_adc.c
index 9460740..f54acc7 100644
--- a/hw/mcu/native/src/hal_adc.c
+++ b/hw/mcu/native/src/hal_adc.c
@@ -16,6 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
+#if 0
 #include <fcntl.h>
 #include <unistd.h>
 #include <inttypes.h>
@@ -23,7 +24,6 @@
 #include <hal/hal_adc.h>
 #include <hal/hal_adc_int.h>
 #include <mcu/mcu_hal.h>
-#if 1
 
 
 /* forwards for the const structure below */
@@ -236,4 +236,4 @@ native_adc_file_read(struct hal_adc *padc)
     return -1;    
 }
 
-#endif
\ No newline at end of file
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/mcu/native/src/hal_dac.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_dac.c b/hw/mcu/native/src/hal_dac.c
index 9f60879..d5055ce 100644
--- a/hw/mcu/native/src/hal_dac.c
+++ b/hw/mcu/native/src/hal_dac.c
@@ -1,3 +1,4 @@
+#if 0
 /**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -111,3 +112,4 @@ native_dac_write(struct hal_dac *pdac, int val)
     }
     return -1;    
 }
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/mcu/native/src/hal_pwm.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_pwm.c b/hw/mcu/native/src/hal_pwm.c
index 5d0688b..032bcad 100644
--- a/hw/mcu/native/src/hal_pwm.c
+++ b/hw/mcu/native/src/hal_pwm.c
@@ -1,3 +1,4 @@
+#if 0
 /**
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
@@ -130,4 +131,4 @@ native_pwm_enable_duty(struct hal_pwm *ppwm, uint16_t fraction)
     }
     return -2;
 }
-
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/mcu/nordic/nrf51xxx/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf51xxx/pkg.yml b/hw/mcu/nordic/nrf51xxx/pkg.yml
index 7ecb890..4485725 100644
--- a/hw/mcu/nordic/nrf51xxx/pkg.yml
+++ b/hw/mcu/nordic/nrf51xxx/pkg.yml
@@ -24,9 +24,6 @@ pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
     - nrf51
 
-pkg.features:
-    - NRF51
-
 pkg.deps: 
     - hw/hal 
     - libs/cmsis-core 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/mcu/nordic/nrf52xxx/include/mcu/cortex_m4.h
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf52xxx/include/mcu/cortex_m4.h b/hw/mcu/nordic/nrf52xxx/include/mcu/cortex_m4.h
index 138a931..09e9c93 100644
--- a/hw/mcu/nordic/nrf52xxx/include/mcu/cortex_m4.h
+++ b/hw/mcu/nordic/nrf52xxx/include/mcu/cortex_m4.h
@@ -20,9 +20,10 @@
 #ifndef __MCU_CORTEX_M4_H__
 #define __MCU_CORTEX_M4_H__
 
+#include "syscfg/syscfg.h"
 #include "nrf52.h"
 
-#if defined(BSP_HAS_32768_XTAL)
+#if MYNEWT_VAL(XTAL_32768)
 #define OS_TICKS_PER_SEC    (128)
 #else
 #define OS_TICKS_PER_SEC    (1000)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/mcu/nordic/nrf52xxx/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf52xxx/pkg.yml b/hw/mcu/nordic/nrf52xxx/pkg.yml
index 707765e..27fdb70 100644
--- a/hw/mcu/nordic/nrf52xxx/pkg.yml
+++ b/hw/mcu/nordic/nrf52xxx/pkg.yml
@@ -24,9 +24,6 @@ pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
     - nrf52
 
-pkg.features:
-    - NRF52
-
 pkg.deps: 
     - hw/hal 
     - hw/mcu/nordic

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/mcu/nordic/nrf52xxx/src/hal_os_tick.c
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf52xxx/src/hal_os_tick.c b/hw/mcu/nordic/nrf52xxx/src/hal_os_tick.c
index 2b5d58f..a96e13a 100644
--- a/hw/mcu/nordic/nrf52xxx/src/hal_os_tick.c
+++ b/hw/mcu/nordic/nrf52xxx/src/hal_os_tick.c
@@ -18,11 +18,12 @@
  */
 #include <assert.h>
 #include <os/os.h>
-#include <hal/hal_os_tick.h>
-#include <nrf52_bitfields.h>
-#include <bsp/cmsis_nvic.h>
+#include "syscfg/syscfg.h"
+#include "hal/hal_os_tick.h"
+#include "nrf52_bitfields.h"
+#include "bsp/cmsis_nvic.h"
 
-#if defined(BSP_HAS_32768_XTAL)
+#if MYNEWT_VAL(XTAL_32768)
 #define RTC_FREQ            32768       /* in Hz */
 #define OS_TICK_TIMER       NRF_RTC1
 #define OS_TICK_IRQ         RTC1_IRQn
@@ -85,7 +86,7 @@ nrf52_os_tick_counter(void)
      */
     OS_ASSERT_CRITICAL();
 
-#if defined(BSP_HAS_32768_XTAL)
+#if MYNEWT_VAL(XTAL_32768)
     return OS_TICK_TIMER->COUNTER;
 #else
     /*
@@ -103,7 +104,7 @@ nrf52_os_tick_set_ocmp(uint32_t ocmp)
 
     OS_ASSERT_CRITICAL();
     while (1) {
-#if defined(BSP_HAS_32768_XTAL)
+#if MYNEWT_VAL(XTAL_32768)
         int delta;
 
         ocmp &= 0xffffff;
@@ -143,7 +144,7 @@ nrf52_timer_handler(void)
     OS_ENTER_CRITICAL(sr);
 
     /* Calculate elapsed ticks and advance OS time. */
-#if defined(BSP_HAS_32768_XTAL)
+#if MYNEWT_VAL(XTAL_32768)
     int delta;
 
     counter = nrf52_os_tick_counter();
@@ -205,7 +206,7 @@ os_tick_idle(os_time_t ticks)
     }
 }
 
-#if defined(BSP_HAS_32768_XTAL)
+#if MYNEWT_VAL(XTAL_32768)
 void
 os_tick_init(uint32_t os_ticks_per_sec, int prio)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/hw/mcu/nordic/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/pkg.yml b/hw/mcu/nordic/pkg.yml
index 4c84dca..c695143 100644
--- a/hw/mcu/nordic/pkg.yml
+++ b/hw/mcu/nordic/pkg.yml
@@ -26,12 +26,12 @@ pkg.keywords:
 
 pkg.type: sdk
 
-pkg.ign_files.NRF52: 
+pkg.ign_files.BSP_NRF52: 
     - "nrf_drv_adc.c"
     - "pstorage*"
     - "sdk_mapped_flags.c"
 
-pkg.ign_files.NRF51:
+pkg.ign_files.BSP_NRF51:
     - "nrf_saadc.c"
     - "nrf_drv_saadc.c"
     - "nrf_drv_comp.c"
@@ -57,4 +57,3 @@ pkg.cflags: -std=gnu99 -DNRF52_PAN_28
 pkg.deps: 
     - hw/hal 
     - libs/cmsis-core 
-    - compiler/arm-none-eabi-m4

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/baselibc/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/baselibc/pkg.yml b/libs/baselibc/pkg.yml
index b0a947c..d7e3110 100644
--- a/libs/baselibc/pkg.yml
+++ b/libs/baselibc/pkg.yml
@@ -26,7 +26,3 @@ pkg.keywords:
 
 pkg.req_apis:
     - console
-pkg.features:
-    - LIBC
-    - BASELIBC
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bleuart/include/bleuart/bleuart.h
----------------------------------------------------------------------
diff --git a/libs/bleuart/include/bleuart/bleuart.h b/libs/bleuart/include/bleuart/bleuart.h
index 14ae930..6639747 100644
--- a/libs/bleuart/include/bleuart/bleuart.h
+++ b/libs/bleuart/include/bleuart/bleuart.h
@@ -20,12 +20,12 @@
 #ifndef _BLEUART_H_
 #define _BLEUART_H_
 
-int
-bleuart_init(int max_input);
+void
+bleuart_init(void);
 int
 bleuart_svc_register(void);
 int
-bleuart_gatt_svr_init(struct ble_hs_cfg *cfg);
+bleuart_gatt_svr_init(void);
 void
 bleuart_set_conn_handle(uint16_t conn_handle);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bleuart/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/bleuart/pkg.yml b/libs/bleuart/pkg.yml
index 5173293..272f53d 100644
--- a/libs/bleuart/pkg.yml
+++ b/libs/bleuart/pkg.yml
@@ -32,3 +32,11 @@ pkg.deps:
 
 pkg.req_apis:
     - console
+
+pkg.init_function: bleuart_init
+pkg.init_stage: 5
+
+pkg.syscfg_defs:
+    BLEUART_MAX_INPUT:
+        description: 'TBD'
+        value: 120

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bleuart/src/bleuart.c
----------------------------------------------------------------------
diff --git a/libs/bleuart/src/bleuart.c b/libs/bleuart/src/bleuart.c
index 81e433c..b7b2591 100644
--- a/libs/bleuart/src/bleuart.c
+++ b/libs/bleuart/src/bleuart.c
@@ -20,10 +20,12 @@
 #include <assert.h>
 #include <stdio.h>
 #include <string.h>
+
+#include "sysinit/sysinit.h"
 #include "host/ble_hs.h"
-#include <bleuart/bleuart.h>
-#include <os/endian.h>
-#include <console/console.h>
+#include "bleuart/bleuart.h"
+#include "os/endian.h"
+#include "console/console.h"
 
 /* ble uart attr read handle */
 uint16_t g_bleuart_attr_read_handle;
@@ -34,9 +36,6 @@ uint16_t g_bleuart_attr_write_handle;
 /* Pointer to a console buffer */
 char *console_buf;
 
-/* Console max input */
-uint16_t console_max_input;
-
 uint16_t g_console_conn_handle;
 /**
  * The vendor specific "bleuart" service consists of one write no-rsp characteristic
@@ -122,11 +121,11 @@ gatt_svr_chr_access_uart_write(uint16_t conn_handle, uint16_t attr_handle,
  * @return 0 on success; non-zero on failure
  */
 int
-bleuart_gatt_svr_init(struct ble_hs_cfg *cfg)
+bleuart_gatt_svr_init(void)
 {
     int rc;
 
-    rc = ble_gatts_count_cfg(gatt_svr_svcs, cfg);
+    rc = ble_gatts_count_cfg(gatt_svr_svcs);
     if (rc != 0) {
         goto err;
     }
@@ -154,7 +153,7 @@ bleuart_uart_read(void)
     off = 0;
     while (1) {
         rc = console_read(console_buf + off,
-                          console_max_input - off, &full_line);
+                          MYNEWT_VAL(BLEUART_MAX_INPUT) - off, &full_line);
         if (rc <= 0 && !full_line) {
             continue;
         }
@@ -189,16 +188,14 @@ bleuart_set_conn_handle(uint16_t conn_handle) {
  *
  * @param Maximum input
  */
-int
-bleuart_init(int max_input)
+void
+bleuart_init(void)
 {
     int rc;
 
     rc = console_init(bleuart_uart_read);
-    assert(rc == 0);
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
-    console_buf = malloc(max_input);
-    console_max_input = max_input;
-    assert(console_buf);
-    return 0;
+    console_buf = malloc(MYNEWT_VAL(BLEUART_MAX_INPUT));
+    SYSINIT_PANIC_ASSERT(console_buf != NULL);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/boot_serial/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/boot_serial/pkg.yml b/libs/boot_serial/pkg.yml
index 4a98343..b556e13 100644
--- a/libs/boot_serial/pkg.yml
+++ b/libs/boot_serial/pkg.yml
@@ -28,8 +28,8 @@ pkg.keywords:
 pkg.deps:
     - hw/hal
     - libs/os
-    - libs/testutil
     - libs/bootutil
     - libs/util
+
 pkg.req_apis:
     - console

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/boot_serial/src/test/boot_test.c
----------------------------------------------------------------------
diff --git a/libs/boot_serial/src/test/boot_test.c b/libs/boot_serial/src/test/boot_test.c
deleted file mode 100644
index e11a649..0000000
--- a/libs/boot_serial/src/test/boot_test.c
+++ /dev/null
@@ -1,230 +0,0 @@
-/*
- * 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 <assert.h>
-#include <stddef.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <inttypes.h>
-#include <util/base64.h>
-#include <util/crc16.h>
-#include <testutil/testutil.h>
-#include <hal/hal_flash.h>
-#include <hal/flash_map.h>
-
-#include "../boot_serial_priv.h"
-
-void
-tx_msg(void *src, int len)
-{
-    char *msg;
-    char *enc;
-    int off;
-    uint16_t crc;
-
-    crc = htons(crc16_ccitt(CRC16_INITIAL_CRC, src, len));
-
-    /*
-     * Lazy, malloc a buffer, fill it and send it.
-     */
-    msg = malloc(len + 2 * sizeof(uint16_t));
-    assert(msg);
-
-    *(uint16_t *)msg = ntohs(len + sizeof(uint16_t));
-    off = sizeof(uint16_t);
-    memcpy(&msg[off], src, len);
-    off += len;
-    memcpy(&msg[off], &crc, sizeof(crc));
-    off += sizeof(uint16_t);
-
-    enc = malloc(BASE64_ENCODE_SIZE(off) + 1);
-    assert(enc);
-
-    off = base64_encode(msg, off, enc, 1);
-    assert(off > 0);
-
-    boot_serial_input(enc, off + 1);
-
-    free(enc);
-    free(msg);
-}
-
-TEST_CASE(boot_serial_setup)
-{
-
-}
-
-TEST_CASE(boot_serial_empty_msg)
-{
-    char buf[4];
-    struct nmgr_hdr hdr;
-
-    boot_serial_input(buf, 0);
-
-    tx_msg(buf, 0);
-
-    strcpy(buf, "--");
-    tx_msg(buf, 2);
-
-    memset(&hdr, 0, sizeof(hdr));
-    tx_msg(&hdr, sizeof(hdr));
-
-    hdr.nh_op = NMGR_OP_WRITE;
-
-    tx_msg(&hdr, sizeof(hdr));
-}
-
-TEST_CASE(boot_serial_empty_img_msg)
-{
-    char buf[sizeof(struct nmgr_hdr) + 32];
-    struct nmgr_hdr *hdr;
-
-    hdr = (struct nmgr_hdr *)buf;
-    memset(hdr, 0, sizeof(*hdr));
-    hdr->nh_op = NMGR_OP_WRITE;
-    hdr->nh_group = htons(NMGR_GROUP_ID_IMAGE);
-    hdr->nh_id = IMGMGR_NMGR_OP_UPLOAD;
-    hdr->nh_len = htons(2);
-    strcpy((char *)(hdr + 1), "{}");
-
-    tx_msg(buf, sizeof(*hdr) + 2);
-}
-
-TEST_CASE(boot_serial_img_msg)
-{
-    char img[16];
-    char enc_img[BASE64_ENCODE_SIZE(sizeof(img))];
-    char buf[sizeof(struct nmgr_hdr) + sizeof(enc_img) + 32];
-    int len;
-    int rc;
-    struct nmgr_hdr *hdr;
-    const struct flash_area *fap;
-
-    memset(img, 0xa5, sizeof(img));
-    len = base64_encode(img, sizeof(img), enc_img, 1);
-    assert(len > 0);
-
-    hdr = (struct nmgr_hdr *)buf;
-    memset(hdr, 0, sizeof(*hdr));
-    hdr->nh_op = NMGR_OP_WRITE;
-    hdr->nh_group = htons(NMGR_GROUP_ID_IMAGE);
-    hdr->nh_id = IMGMGR_NMGR_OP_UPLOAD;
-
-    len = sprintf((char *)(hdr + 1), "{\"off\":0,\"len\":16,\"data\":\"%s\"}",
-      enc_img);
-    hdr->nh_len = htons(len);
-
-    len = sizeof(*hdr) + len;
-
-    tx_msg(buf, len);
-
-    /*
-     * Validate contents inside image 0 slot
-     */
-    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
-    assert(rc == 0);
-
-    rc = flash_area_read(fap, 0, enc_img, sizeof(img));
-    assert(rc == 0);
-    assert(!memcmp(enc_img, img, sizeof(img)));
-}
-
-TEST_CASE(boot_serial_upload_bigger_image)
-{
-    char img[256];
-    char enc_img[64];
-    char buf[sizeof(struct nmgr_hdr) + 128];
-    int len;
-    int off;
-    int rc;
-    struct nmgr_hdr *hdr;
-    const struct flash_area *fap;
-    int i;
-
-    for (i = 0; i < sizeof(img); i++) {
-        img[i] = i;
-    }
-
-    for (off = 0; off < sizeof(img); off += 32) {
-        len = base64_encode(&img[off], 32, enc_img, 1);
-        assert(len > 0);
-
-        hdr = (struct nmgr_hdr *)buf;
-        memset(hdr, 0, sizeof(*hdr));
-        hdr->nh_op = NMGR_OP_WRITE;
-        hdr->nh_group = htons(NMGR_GROUP_ID_IMAGE);
-        hdr->nh_id = IMGMGR_NMGR_OP_UPLOAD;
-
-        if (off) {
-            len = sprintf((char *)(hdr + 1), "{\"off\":%d,\"data\":\"%s\"}",
-              off, enc_img);
-        } else {
-            len = sprintf((char *)(hdr + 1), "{\"off\": 0 ,\"len\":%ld, "
-              "\"data\":\"%s\"}", (long)sizeof(img), enc_img);
-        }
-        hdr->nh_len = htons(len);
-
-        len = sizeof(*hdr) + len;
-
-        tx_msg(buf, len);
-    }
-
-    /*
-     * Validate contents inside image 0 slot
-     */
-    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
-    assert(rc == 0);
-
-    for (off = 0; off < sizeof(img); off += sizeof(enc_img)) {
-        rc = flash_area_read(fap, off, enc_img, sizeof(enc_img));
-        assert(rc == 0);
-        assert(!memcmp(enc_img, &img[off], sizeof(enc_img)));
-    }
-}
-
-TEST_SUITE(boot_serial_suite)
-{
-    boot_serial_setup();
-    boot_serial_empty_msg();
-    boot_serial_empty_img_msg();
-    boot_serial_img_msg();
-    boot_serial_upload_bigger_image();
-}
-
-int
-boot_serial_test(void)
-{
-    boot_serial_suite();
-    return tu_any_failed;
-}
-
-#ifdef MYNEWT_SELFTEST
-int
-main(void)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    boot_serial_test();
-
-    return tu_any_failed;
-}
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/boot_serial/test/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/boot_serial/test/pkg.yml b/libs/boot_serial/test/pkg.yml
new file mode 100644
index 0000000..e123ce1
--- /dev/null
+++ b/libs/boot_serial/test/pkg.yml
@@ -0,0 +1,30 @@
+# 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.
+#
+pkg.name: libs/boot_serial/test
+pkg.type: unittest
+pkg.description: "Boot serial unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+    - libs/boot_serial
+    - libs/testutil
+
+pkg.deps.SELFTEST:
+    - libs/console/stub

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/boot_serial/test/src/boot_test.c
----------------------------------------------------------------------
diff --git a/libs/boot_serial/test/src/boot_test.c b/libs/boot_serial/test/src/boot_test.c
new file mode 100644
index 0000000..853a391
--- /dev/null
+++ b/libs/boot_serial/test/src/boot_test.c
@@ -0,0 +1,231 @@
+/*
+ * 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 <assert.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+#include "syscfg/syscfg.h"
+#include "util/base64.h"
+#include "util/crc16.h"
+#include "testutil/testutil.h"
+#include "hal/hal_flash.h"
+#include "hal/flash_map.h"
+
+#include "boot_serial_priv.h"
+
+void
+tx_msg(void *src, int len)
+{
+    char *msg;
+    char *enc;
+    int off;
+    uint16_t crc;
+
+    crc = htons(crc16_ccitt(CRC16_INITIAL_CRC, src, len));
+
+    /*
+     * Lazy, malloc a buffer, fill it and send it.
+     */
+    msg = malloc(len + 2 * sizeof(uint16_t));
+    assert(msg);
+
+    *(uint16_t *)msg = ntohs(len + sizeof(uint16_t));
+    off = sizeof(uint16_t);
+    memcpy(&msg[off], src, len);
+    off += len;
+    memcpy(&msg[off], &crc, sizeof(crc));
+    off += sizeof(uint16_t);
+
+    enc = malloc(BASE64_ENCODE_SIZE(off) + 1);
+    assert(enc);
+
+    off = base64_encode(msg, off, enc, 1);
+    assert(off > 0);
+
+    boot_serial_input(enc, off + 1);
+
+    free(enc);
+    free(msg);
+}
+
+TEST_CASE(boot_serial_setup)
+{
+
+}
+
+TEST_CASE(boot_serial_empty_msg)
+{
+    char buf[4];
+    struct nmgr_hdr hdr;
+
+    boot_serial_input(buf, 0);
+
+    tx_msg(buf, 0);
+
+    strcpy(buf, "--");
+    tx_msg(buf, 2);
+
+    memset(&hdr, 0, sizeof(hdr));
+    tx_msg(&hdr, sizeof(hdr));
+
+    hdr.nh_op = NMGR_OP_WRITE;
+
+    tx_msg(&hdr, sizeof(hdr));
+}
+
+TEST_CASE(boot_serial_empty_img_msg)
+{
+    char buf[sizeof(struct nmgr_hdr) + 32];
+    struct nmgr_hdr *hdr;
+
+    hdr = (struct nmgr_hdr *)buf;
+    memset(hdr, 0, sizeof(*hdr));
+    hdr->nh_op = NMGR_OP_WRITE;
+    hdr->nh_group = htons(NMGR_GROUP_ID_IMAGE);
+    hdr->nh_id = IMGMGR_NMGR_OP_UPLOAD;
+    hdr->nh_len = htons(2);
+    strcpy((char *)(hdr + 1), "{}");
+
+    tx_msg(buf, sizeof(*hdr) + 2);
+}
+
+TEST_CASE(boot_serial_img_msg)
+{
+    char img[16];
+    char enc_img[BASE64_ENCODE_SIZE(sizeof(img))];
+    char buf[sizeof(struct nmgr_hdr) + sizeof(enc_img) + 32];
+    int len;
+    int rc;
+    struct nmgr_hdr *hdr;
+    const struct flash_area *fap;
+
+    memset(img, 0xa5, sizeof(img));
+    len = base64_encode(img, sizeof(img), enc_img, 1);
+    assert(len > 0);
+
+    hdr = (struct nmgr_hdr *)buf;
+    memset(hdr, 0, sizeof(*hdr));
+    hdr->nh_op = NMGR_OP_WRITE;
+    hdr->nh_group = htons(NMGR_GROUP_ID_IMAGE);
+    hdr->nh_id = IMGMGR_NMGR_OP_UPLOAD;
+
+    len = sprintf((char *)(hdr + 1), "{\"off\":0,\"len\":16,\"data\":\"%s\"}",
+      enc_img);
+    hdr->nh_len = htons(len);
+
+    len = sizeof(*hdr) + len;
+
+    tx_msg(buf, len);
+
+    /*
+     * Validate contents inside image 0 slot
+     */
+    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
+    assert(rc == 0);
+
+    rc = flash_area_read(fap, 0, enc_img, sizeof(img));
+    assert(rc == 0);
+    assert(!memcmp(enc_img, img, sizeof(img)));
+}
+
+TEST_CASE(boot_serial_upload_bigger_image)
+{
+    char img[256];
+    char enc_img[64];
+    char buf[sizeof(struct nmgr_hdr) + 128];
+    int len;
+    int off;
+    int rc;
+    struct nmgr_hdr *hdr;
+    const struct flash_area *fap;
+    int i;
+
+    for (i = 0; i < sizeof(img); i++) {
+        img[i] = i;
+    }
+
+    for (off = 0; off < sizeof(img); off += 32) {
+        len = base64_encode(&img[off], 32, enc_img, 1);
+        assert(len > 0);
+
+        hdr = (struct nmgr_hdr *)buf;
+        memset(hdr, 0, sizeof(*hdr));
+        hdr->nh_op = NMGR_OP_WRITE;
+        hdr->nh_group = htons(NMGR_GROUP_ID_IMAGE);
+        hdr->nh_id = IMGMGR_NMGR_OP_UPLOAD;
+
+        if (off) {
+            len = sprintf((char *)(hdr + 1), "{\"off\":%d,\"data\":\"%s\"}",
+              off, enc_img);
+        } else {
+            len = sprintf((char *)(hdr + 1), "{\"off\": 0 ,\"len\":%ld, "
+              "\"data\":\"%s\"}", (long)sizeof(img), enc_img);
+        }
+        hdr->nh_len = htons(len);
+
+        len = sizeof(*hdr) + len;
+
+        tx_msg(buf, len);
+    }
+
+    /*
+     * Validate contents inside image 0 slot
+     */
+    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
+    assert(rc == 0);
+
+    for (off = 0; off < sizeof(img); off += sizeof(enc_img)) {
+        rc = flash_area_read(fap, off, enc_img, sizeof(enc_img));
+        assert(rc == 0);
+        assert(!memcmp(enc_img, &img[off], sizeof(enc_img)));
+    }
+}
+
+TEST_SUITE(boot_serial_suite)
+{
+    boot_serial_setup();
+    boot_serial_empty_msg();
+    boot_serial_empty_img_msg();
+    boot_serial_img_msg();
+    boot_serial_upload_bigger_image();
+}
+
+int
+boot_serial_test(void)
+{
+    boot_serial_suite();
+    return tu_any_failed;
+}
+
+#if MYNEWT_VAL(SELFTEST)
+int
+main(void)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    boot_serial_test();
+
+    return tu_any_failed;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bootutil/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/bootutil/pkg.yml b/libs/bootutil/pkg.yml
index 53c741c..f9d2f70 100644
--- a/libs/bootutil/pkg.yml
+++ b/libs/bootutil/pkg.yml
@@ -26,15 +26,24 @@ pkg.keywords:
     - bootloader
 
 pkg.deps: 
-    - libs/os 
-    - libs/testutil
-    - libs/mbedtls
     - hw/hal
+    - libs/mbedtls
+    - libs/os 
     - sys/config
 
-pkg.deps.FS:
-    - fs/fs
-pkg.cflags.NFFS: -DUSE_STATUS_FILE
+pkg.deps.BOOTUTIL_NFFS:
+    - fs/nffs
+
+pkg.init_function: bootutil_pkg_init
+pkg.init_stage: 5
 
-pkg.cflags.IMAGE_KEYS_RSA: -DIMAGE_SIGNATURES_RSA
-pkg.cflags.IMAGE_KEYS_EC: -DIMAGE_SIGNATURES_EC
+pkg.syscfg_defs:
+    BOOTUTIL_NFFS:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_FS_NFFS'
+    BOOTUTIL_SIGN_RSA:
+        description: 'TBD'
+        value: '0'
+    BOOTUTIL_SIGN_EC:
+        description: 'TBD'
+        value: '0'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bootutil/src/bootutil_misc.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_misc.c b/libs/bootutil/src/bootutil_misc.c
index 94536ef..17052fe 100644
--- a/libs/bootutil/src/bootutil_misc.c
+++ b/libs/bootutil/src/bootutil_misc.c
@@ -19,22 +19,24 @@
 
 #include <string.h>
 #include <inttypes.h>
-#include <hal/hal_flash.h>
-#include <config/config.h>
-#include <os/os.h>
+
+#include "syscfg/syscfg.h"
+#include "hal/hal_flash.h"
+#include "config/config.h"
+#include "os/os.h"
 #include "bootutil/image.h"
 #include "bootutil_priv.h"
 
-#ifdef USE_STATUS_FILE
-#include <fs/fs.h>
-#include <fs/fsutil.h>
+#if MYNEWT_VAL(BOOTUTIL_NFFS)
+#include "fs/fs.h"
+#include "fs/fsutil.h"
 #endif
 
 static int boot_conf_set(int argc, char **argv, char *val);
 
 static struct image_version boot_main;
 static struct image_version boot_test;
-#ifndef USE_STATUS_FILE
+#if !MYNEWT_VAL(BOOTUTIL_NFFS)
 static struct boot_status boot_saved;
 #endif
 
@@ -69,7 +71,7 @@ boot_conf_set(int argc, char **argv, char *val)
                 memset(&boot_test, 0, len);
                 rc = 0;
             }
-#ifndef USE_STATUS_FILE
+#if !MYNEWT_VAL(BOOTUTIL_NFFS)
         } else if (!strcmp(argv[0], "status")) {
             if (!val) {
                 boot_saved.state = 0;
@@ -237,7 +239,7 @@ bootutil_cfg_register(void)
     conf_register(&boot_conf_handler);
 }
 
-#ifndef USE_STATUS_FILE
+#if !MYNEWT_VAL(BOOTUTIL_NFFS)
 int
 boot_read_status(struct boot_status *bs)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bootutil/src/image_ec.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/image_ec.c b/libs/bootutil/src/image_ec.c
index 6b21ab2..f3adfcf 100644
--- a/libs/bootutil/src/image_ec.c
+++ b/libs/bootutil/src/image_ec.c
@@ -16,13 +16,16 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifdef IMAGE_SIGNATURES_EC
-#include <bootutil/sign_key.h>
 
-#include <mbedtls/sha256.h>
-#include <mbedtls/ecdsa.h>
-#include <mbedtls/oid.h>
-#include <mbedtls/asn1.h>
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(BOOTUTIL_SIGN_EC)
+#include "bootutil/sign_key.h"
+
+#include "mbedtls/sha256.h"
+#include "mbedtls/ecdsa.h"
+#include "mbedtls/oid.h"
+#include "mbedtls/asn1.h"
 
 #include "bootutil_priv.h"
 
@@ -115,4 +118,4 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,
 
     return rc;
 }
-#endif /* IMAGE_SIGNATURES_EC */
+#endif /* MYNEWT_VAL(BOOTUTIL_SIGN_EC) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bootutil/src/image_rsa.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/image_rsa.c b/libs/bootutil/src/image_rsa.c
index 8f02329..e556092 100644
--- a/libs/bootutil/src/image_rsa.c
+++ b/libs/bootutil/src/image_rsa.c
@@ -16,11 +16,14 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifdef IMAGE_SIGNATURES_RSA
-#include <bootutil/sign_key.h>
 
-#include <mbedtls/rsa.h>
-#include <mbedtls/asn1.h>
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
+#include "bootutil/sign_key.h"
+
+#include "mbedtls/rsa.h"
+#include "mbedtls/asn1.h"
 
 #include "bootutil_priv.h"
 
@@ -138,4 +141,4 @@ bootutil_verify_sig(uint8_t *hash, uint32_t hlen, uint8_t *sig, int slen,
 
     return rc;
 }
-#endif /* IMAGE_SIGNATURES_RSA */
+#endif /* MYNEWT_VAL(BOOTUTIL_SIGN_RSA) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bootutil/src/image_validate.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/image_validate.c b/libs/bootutil/src/image_validate.c
index b491790..17820aa 100644
--- a/libs/bootutil/src/image_validate.c
+++ b/libs/bootutil/src/image_validate.c
@@ -21,15 +21,16 @@
 #include <stddef.h>
 #include <inttypes.h>
 #include <string.h>
-#include <hal/hal_flash.h>
 
-#include <bootutil/image.h>
-#include <bootutil/sign_key.h>
+#include "syscfg/syscfg.h"
+#include "hal/hal_flash.h"
+#include "bootutil/image.h"
+#include "bootutil/sign_key.h"
 
-#include <mbedtls/sha256.h>
-#include <mbedtls/rsa.h>
-#include <mbedtls/ecdsa.h>
-#include <mbedtls/asn1.h>
+#include "mbedtls/sha256.h"
+#include "mbedtls/rsa.h"
+#include "mbedtls/ecdsa.h"
+#include "mbedtls/asn1.h"
 
 #include "bootutil_priv.h"
 
@@ -83,7 +84,7 @@ bootutil_img_validate(struct image_header *hdr, uint8_t flash_id, uint32_t addr,
     uint32_t off;
     uint32_t size;
     uint32_t sha_off = 0;
-#if defined(IMAGE_SIGNATURES_RSA) || defined(IMAGE_SIGNATURES_EC)
+#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA) || MYNEWT_VAL(BOOTUTIL_SIGN_EC)
     uint32_t sig_off = 0;
     uint32_t sig_len = 0;
 #endif
@@ -92,12 +93,12 @@ bootutil_img_validate(struct image_header *hdr, uint8_t flash_id, uint32_t addr,
     uint8_t hash[32];
     int rc;
 
-#ifdef IMAGE_SIGNATURES_RSA
+#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
     if ((hdr->ih_flags & IMAGE_F_PKCS15_RSA2048_SHA256) == 0) {
         return -1;
     }
 #endif
-#ifdef IMAGE_SIGNATURES_EC
+#if MYNEWT_VAL(BOOTUTIL_SIGN_EC)
     if ((hdr->ih_flags & IMAGE_F_ECDSA224_SHA256) == 0) {
         return -1;
     }
@@ -128,7 +129,7 @@ bootutil_img_validate(struct image_header *hdr, uint8_t flash_id, uint32_t addr,
             }
             sha_off = addr + off + sizeof(tlv);
         }
-#ifdef IMAGE_SIGNATURES_RSA
+#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA)
         if (tlv.it_type == IMAGE_TLV_RSA2048) {
             if (tlv.it_len != 256) { /* 2048 bits */
                 return -1;
@@ -137,7 +138,7 @@ bootutil_img_validate(struct image_header *hdr, uint8_t flash_id, uint32_t addr,
             sig_len = tlv.it_len;
         }
 #endif
-#ifdef IMAGE_SIGNATURES_EC
+#if MYNEWT_VAL(BOOTUTIL_SIGN_EC)
         if (tlv.it_type == IMAGE_TLV_ECDSA224) {
             if (tlv.it_len < 64) { /* oids + 2 * 28 bytes */
                 return -1;
@@ -162,7 +163,7 @@ bootutil_img_validate(struct image_header *hdr, uint8_t flash_id, uint32_t addr,
             return -1;
         }
     }
-#if defined(IMAGE_SIGNATURES_RSA) || defined(IMAGE_SIGNATURES_EC)
+#if MYNEWT_VAL(BOOTUTIL_SIGN_RSA) || MYNEWT_VAL(BOOTUTIL_SIGN_EC)
     if (!sig_off) {
         /*
          * Header said there should be PKCS1.v5 signature, no TLV

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bootutil/src/loader.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/loader.c b/libs/bootutil/src/loader.c
index bd7fbd8..6d08b9a 100644
--- a/libs/bootutil/src/loader.c
+++ b/libs/bootutil/src/loader.c
@@ -499,3 +499,9 @@ boot_go(const struct boot_req *req, struct boot_rsp *rsp)
 
     return 0;
 }
+
+void
+bootutil_pkg_init(void)
+{
+    bootutil_cfg_register();
+}


[17/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_sm_sc_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_sc_test.c b/net/nimble/host/src/test/ble_sm_sc_test.c
deleted file mode 100644
index 518720c..0000000
--- a/net/nimble/host/src/test/ble_sm_sc_test.c
+++ /dev/null
@@ -1,4910 +0,0 @@
-/**
- * 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 <stddef.h>
-#include <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/hci_common.h"
-#include "nimble/nimble_opt.h"
-#include "host/ble_sm.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-#include "ble_sm_test_util.h"
-
-#if NIMBLE_OPT(SM)
-
-/**
- * Secure connections pairing
- * Master: peer
- * Pair algorithm: just works
- * Initiator IO capabilities: 3
- * Responder IO capabilities: 3
- * Bonding: true
- * Initiator address type: 0
- * Responder address type: 0
- * Initiator key distribution: 5
- * Responder key distribution: 7
- */
-TEST_CASE(ble_sm_sc_peer_jw_iio3_rio3_b1_iat0_rat0_ik5_rk7)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
-        },
-        .resp_id_addr = {
-            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-        },
-        .pair_req = {
-            .io_cap = 0x03,
-            .oob_data_flag = 0x00,
-            .authreq = 0x09,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x0d,
-            .resp_key_dist = 0x0f,
-        },
-        .pair_rsp = {
-            .io_cap = 0x03,
-            .oob_data_flag = 0x00,
-            .authreq = 0x09,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x05,
-            .resp_key_dist = 0x07,
-        },
-        .our_priv_key = {
-            0x54, 0x8d, 0x20, 0xb8, 0x97, 0x0b, 0xbc, 0x43,
-            0x9a, 0xad, 0x10, 0x6f, 0x60, 0x74, 0xd4, 0x6a,
-            0x55, 0xc1, 0x7a, 0x17, 0x8b, 0x60, 0xe0, 0xb4,
-            0x5a, 0xe6, 0x58, 0xf1, 0xea, 0x12, 0xd9, 0xfb,
-        },
-        .public_key_req = {
-            .x = {
-                0xbc, 0xf2, 0xd8, 0xa5, 0xdb, 0xa3, 0x95, 0x6c,
-                0x99, 0xf9, 0x11, 0x0d, 0x4d, 0x2e, 0xf0, 0xbd,
-                0xee, 0x9b, 0x69, 0xb6, 0xcd, 0x88, 0x74, 0xbe,
-                0x40, 0xe8, 0xe5, 0xcc, 0xdc, 0x88, 0x44, 0x53,
-            },
-            .y = {
-                0xbf, 0xa9, 0x82, 0x0e, 0x18, 0x7a, 0x14, 0xf8,
-                0x77, 0xfd, 0x8e, 0x92, 0x2a, 0xf8, 0x5d, 0x39,
-                0xd1, 0x6d, 0x92, 0x1f, 0x38, 0x74, 0x99, 0xdc,
-                0x6c, 0x2c, 0x94, 0x23, 0xf9, 0x72, 0x56, 0xab,
-            },
-        },
-        .public_key_rsp = {
-            .x = {
-                0x72, 0x8c, 0xd1, 0x88, 0xd7, 0xbe, 0x49, 0xb2,
-                0xc5, 0x5c, 0x95, 0xb3, 0x64, 0xe0, 0x12, 0x32,
-                0xb6, 0xc9, 0x47, 0x63, 0x37, 0x38, 0x5b, 0x9c,
-                0x1e, 0x1b, 0x1a, 0x06, 0x09, 0xe2, 0x31, 0x85,
-            },
-            .y = {
-                0x19, 0x3a, 0x29, 0x69, 0x62, 0xd6, 0x30, 0xe7,
-                0xe8, 0x48, 0x63, 0xdc, 0x00, 0x73, 0x0a, 0x70,
-                0x7d, 0x2e, 0x29, 0xcc, 0x91, 0x77, 0x71, 0xb1,
-                0x75, 0xb8, 0xf7, 0xdc, 0xb0, 0xe2, 0x91, 0x10,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0x82, 0xed, 0xd0, 0x62, 0x91, 0x3d, 0x96, 0x7f,
-                0x13, 0xc5, 0x0d, 0x02, 0x2b, 0x5e, 0x43, 0x16,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0xa4, 0x34, 0x5f, 0xb3, 0xaf, 0x73, 0x43, 0x64,
-                0xcd, 0x19, 0x1b, 0x5b, 0x87, 0x58, 0x31, 0x66,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0xc0, 0x91, 0xfb, 0xb3, 0x77, 0xa2, 0x02, 0x0b,
-                0xc6, 0xcd, 0x6c, 0x04, 0x51, 0x45, 0x45, 0x39,
-            },
-        },
-        .dhkey_check_req = {
-            .value = {
-                0x82, 0x65, 0x1d, 0x02, 0xed, 0x89, 0x13, 0x44,
-                0x04, 0x1a, 0x14, 0x7c, 0x32, 0x9a, 0x1e, 0x7d,
-            },
-        },
-        .dhkey_check_rsp = {
-            .value = {
-                0x06, 0x3c, 0x28, 0x4a, 0xe5, 0x48, 0x4b, 0x51,
-                0x65, 0x4e, 0x14, 0x5e, 0x2f, 0xdd, 0xfa, 0x22,
-            },
-        },
-        .id_info_req = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_req = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-            },
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0x40, 0x53, 0xeb, 0x7a, 0x4d, 0x8e, 0xa2, 0xb5,
-                0xca, 0xa1, 0xb6, 0xae, 0x7e, 0x6a, 0x4d, 0xd9,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0xbc, 0x13, 0x4b, 0x45, 0xda, 0x76, 0x5b, 0xcd,
-                0xc2, 0x43, 0x81, 0xb8, 0xc3, 0x68, 0x12, 0xbb,
-            },
-        },
-        .ltk = {
-            0x63, 0x59, 0x8a, 0x14, 0x09, 0x4b, 0x94, 0x6e,
-            0xff, 0xae, 0x5e, 0x53, 0x86, 0x02, 0xa3, 0x6c,
-        },
-        .pair_alg = BLE_SM_PAIR_ALG_JW,
-        .authenticated = 0,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_NONE,
-            },
-        },
-    };
-    ble_sm_test_util_peer_sc_good(&params);
-}
-
-/**
- * Secure connections pairing
- * Master: peer
- * Pair algorithm: passkey entry
- * Initiator IO capabilities: 0
- * Responder IO capabilities: 2
- * Bonding: true
- * Initiator address type: 0
- * Responder address type: 0
- * Initiator key distribution: 5
- * Responder key distribution: 7
- */
-TEST_CASE(ble_sm_sc_peer_pk_iio0_rio2_b1_iat0_rat0_ik5_rk7)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
-        },
-        .resp_id_addr = {
-            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-        },
-        .pair_req = {
-            .io_cap = 0x00,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x0d,
-            .resp_key_dist = 0x0f,
-        },
-        .pair_rsp = {
-            .io_cap = 0x02,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x05,
-            .resp_key_dist = 0x07,
-        },
-        .our_priv_key = {
-            0x54, 0x8d, 0x20, 0xb8, 0x97, 0x0b, 0xbc, 0x43,
-            0x9a, 0xad, 0x10, 0x6f, 0x60, 0x74, 0xd4, 0x6a,
-            0x55, 0xc1, 0x7a, 0x17, 0x8b, 0x60, 0xe0, 0xb4,
-            0x5a, 0xe6, 0x58, 0xf1, 0xea, 0x12, 0xd9, 0xfb,
-        },
-        .public_key_req = {
-            .x = {
-                0x22, 0x26, 0xcc, 0x64, 0x4d, 0xc1, 0x01, 0xd1,
-                0xb9, 0x8d, 0xe2, 0xd4, 0xbc, 0x55, 0x37, 0x4c,
-                0x12, 0x81, 0x14, 0x83, 0x81, 0xe8, 0x36, 0x1b,
-                0x78, 0xff, 0x49, 0xfc, 0xe9, 0x2e, 0x56, 0xc0,
-            },
-            .y = {
-                0xd9, 0x31, 0xa5, 0x8d, 0x02, 0xf1, 0x94, 0xb6,
-                0x83, 0x97, 0xd1, 0xfb, 0x01, 0x97, 0x4d, 0x06,
-                0xec, 0x18, 0x8d, 0x4a, 0xd2, 0x14, 0x12, 0x95,
-                0x2d, 0x4d, 0x18, 0xde, 0x4d, 0xaa, 0x91, 0x25,
-            },
-        },
-        .public_key_rsp = {
-            .x = {
-                0x72, 0x8c, 0xd1, 0x88, 0xd7, 0xbe, 0x49, 0xb2,
-                0xc5, 0x5c, 0x95, 0xb3, 0x64, 0xe0, 0x12, 0x32,
-                0xb6, 0xc9, 0x47, 0x63, 0x37, 0x38, 0x5b, 0x9c,
-                0x1e, 0x1b, 0x1a, 0x06, 0x09, 0xe2, 0x31, 0x85,
-            },
-            .y = {
-                0x19, 0x3a, 0x29, 0x69, 0x62, 0xd6, 0x30, 0xe7,
-                0xe8, 0x48, 0x63, 0xdc, 0x00, 0x73, 0x0a, 0x70,
-                0x7d, 0x2e, 0x29, 0xcc, 0x91, 0x77, 0x71, 0xb1,
-                0x75, 0xb8, 0xf7, 0xdc, 0xb0, 0xe2, 0x91, 0x10,
-            },
-        },
-        .confirm_req[0] = {
-            .value = {
-                0x2c, 0x16, 0x15, 0x0d, 0xe8, 0x18, 0x50, 0xd8,
-                0xae, 0x04, 0x6c, 0xa8, 0x50, 0xb8, 0xe5, 0x85,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0x62, 0x53, 0xfb, 0x69, 0x94, 0x33, 0x11, 0xd3,
-                0x8e, 0x03, 0xd5, 0x05, 0xd7, 0x68, 0x33, 0x16,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0xd5, 0x0e, 0x27, 0xcf, 0xa4, 0xc1, 0x52, 0x1b,
-                0xf1, 0x9d, 0x5f, 0xbe, 0xe2, 0xc0, 0x48, 0x38,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0x94, 0x31, 0x95, 0x44, 0x6c, 0xc5, 0x73, 0xc8,
-                0x8d, 0x72, 0x06, 0xe7, 0xfd, 0x16, 0x70, 0x5d,
-            },
-        },
-        .confirm_req[1] = {
-            .value = {
-                0x80, 0xae, 0x74, 0xaa, 0x9a, 0xfc, 0x09, 0x97,
-                0x10, 0x01, 0x4e, 0xbb, 0x16, 0x36, 0x6b, 0xc7,
-            },
-        },
-        .confirm_rsp[1] = {
-            .value = {
-                0x5a, 0xb1, 0xe5, 0x81, 0x5a, 0x1b, 0xef, 0xf4,
-                0xa8, 0x3d, 0xaa, 0x3f, 0x02, 0x1f, 0x78, 0x55,
-            },
-        },
-        .random_req[1] = {
-            .value = {
-                0x04, 0x4a, 0xf4, 0xd5, 0x4b, 0x4f, 0x77, 0x37,
-                0x2a, 0x3c, 0xfe, 0x83, 0x34, 0x6b, 0x38, 0x1a,
-            },
-        },
-        .random_rsp[1] = {
-            .value = {
-                0x24, 0xb3, 0x47, 0xc8, 0xb0, 0xa2, 0xa3, 0xd8,
-                0x78, 0x3d, 0x09, 0x8d, 0xea, 0x49, 0xf6, 0x22,
-            },
-        },
-        .confirm_req[2] = {
-            .value = {
-                0x56, 0x5f, 0x07, 0x30, 0x3a, 0xc1, 0x44, 0xf9,
-                0x00, 0x03, 0xb3, 0x93, 0x58, 0xb4, 0x2c, 0x85,
-            },
-        },
-        .confirm_rsp[2] = {
-            .value = {
-                0x50, 0x8a, 0xb3, 0x0b, 0xe4, 0x2e, 0xd3, 0x49,
-                0x59, 0x40, 0xb2, 0x71, 0xc9, 0x49, 0x29, 0x19,
-            },
-        },
-        .random_req[2] = {
-            .value = {
-                0x32, 0x37, 0x8e, 0x63, 0x6d, 0xbd, 0xd6, 0x18,
-                0xee, 0xa7, 0x0e, 0xe5, 0x7e, 0x5f, 0xe1, 0x80,
-            },
-        },
-        .random_rsp[2] = {
-            .value = {
-                0xa2, 0x1a, 0x92, 0xcd, 0xc0, 0x8f, 0x92, 0xb0,
-                0xe6, 0xbe, 0x43, 0x55, 0xc8, 0x47, 0x56, 0x4b,
-            },
-        },
-        .confirm_req[3] = {
-            .value = {
-                0x1b, 0xa0, 0x82, 0xda, 0xfc, 0xaf, 0x3f, 0x9c,
-                0xdf, 0xff, 0xa2, 0x18, 0xba, 0xbd, 0x9b, 0x48,
-            },
-        },
-        .confirm_rsp[3] = {
-            .value = {
-                0x6a, 0x90, 0xb7, 0x1c, 0x93, 0x4e, 0x4a, 0x8b,
-                0xda, 0xe8, 0x13, 0x6e, 0x01, 0x91, 0x74, 0xb1,
-            },
-        },
-        .random_req[3] = {
-            .value = {
-                0x41, 0xbf, 0x60, 0x64, 0x1d, 0xfc, 0xe2, 0xee,
-                0x00, 0xa3, 0x2a, 0xb1, 0xf8, 0x34, 0x6b, 0xeb,
-            },
-        },
-        .random_rsp[3] = {
-            .value = {
-                0xeb, 0x9c, 0xaf, 0x20, 0x14, 0x0f, 0xf2, 0x3e,
-                0xee, 0x45, 0xca, 0xe8, 0xdc, 0x17, 0xab, 0x22,
-            },
-        },
-        .confirm_req[4] = {
-            .value = {
-                0x75, 0x8f, 0x97, 0xbb, 0x87, 0xa8, 0x70, 0xda,
-                0x94, 0x5a, 0xd6, 0x09, 0x78, 0xe3, 0xdd, 0x43,
-            },
-        },
-        .confirm_rsp[4] = {
-            .value = {
-                0x8c, 0x2d, 0xa7, 0x44, 0xd9, 0x15, 0xa8, 0x9e,
-                0xdf, 0x3a, 0x59, 0xa5, 0xee, 0x92, 0x24, 0x3c,
-            },
-        },
-        .random_req[4] = {
-            .value = {
-                0xb9, 0xe0, 0xf3, 0xf6, 0x6f, 0xbd, 0xa0, 0x7a,
-                0x82, 0x20, 0x61, 0xbe, 0xf3, 0xe6, 0x4e, 0xac,
-            },
-        },
-        .random_rsp[4] = {
-            .value = {
-                0xdd, 0x9b, 0xd3, 0x10, 0xed, 0x12, 0xe8, 0xb5,
-                0xa2, 0x59, 0xe1, 0xdc, 0x5c, 0xd8, 0x6e, 0x96,
-            },
-        },
-        .confirm_req[5] = {
-            .value = {
-                0x9d, 0xc7, 0x97, 0x67, 0x8d, 0xd0, 0xd6, 0x1a,
-                0x4d, 0x52, 0xc0, 0x8d, 0x87, 0xa9, 0x75, 0xf5,
-            },
-        },
-        .confirm_rsp[5] = {
-            .value = {
-                0xd4, 0x5d, 0x61, 0x76, 0x38, 0xe3, 0x81, 0x85,
-                0x18, 0x5f, 0xac, 0xde, 0x49, 0x57, 0xf6, 0x9b,
-            },
-        },
-        .random_req[5] = {
-            .value = {
-                0xfe, 0x83, 0xe9, 0xc6, 0xe9, 0xa4, 0x83, 0x0d,
-                0xaf, 0x27, 0x6f, 0x79, 0x7a, 0x2b, 0x2d, 0x1f,
-            },
-        },
-        .random_rsp[5] = {
-            .value = {
-                0xf2, 0x0c, 0x9e, 0x75, 0x5b, 0xb1, 0x8c, 0xf1,
-                0x46, 0x4f, 0x68, 0xe8, 0x0a, 0x65, 0xd5, 0x81,
-            },
-        },
-        .confirm_req[6] = {
-            .value = {
-                0x15, 0x2b, 0x2e, 0x14, 0xf7, 0x31, 0xa2, 0xff,
-                0x93, 0xa7, 0x28, 0x65, 0xb1, 0x68, 0x96, 0xc6,
-            },
-        },
-        .confirm_rsp[6] = {
-            .value = {
-                0x6f, 0x01, 0x22, 0x14, 0x78, 0xfb, 0x93, 0xf4,
-                0xfa, 0xf1, 0x6d, 0x33, 0x49, 0x0e, 0x7d, 0x56,
-            },
-        },
-        .random_req[6] = {
-            .value = {
-                0x05, 0xe5, 0xed, 0x99, 0x63, 0x05, 0x29, 0xb1,
-                0xbd, 0xf7, 0x2b, 0xa6, 0x94, 0xfe, 0x45, 0xb2,
-            },
-        },
-        .random_rsp[6] = {
-            .value = {
-                0x51, 0xf1, 0x2a, 0xa6, 0x7b, 0xe0, 0xb3, 0x20,
-                0x7d, 0x7e, 0xd3, 0x47, 0xfb, 0x83, 0xe1, 0xc6,
-            },
-        },
-        .confirm_req[7] = {
-            .value = {
-                0x9e, 0x7a, 0x3d, 0x12, 0x3b, 0x30, 0x81, 0x23,
-                0x1c, 0x94, 0x42, 0x73, 0x41, 0x68, 0xc6, 0x17,
-            },
-        },
-        .confirm_rsp[7] = {
-            .value = {
-                0x55, 0x31, 0x41, 0xe8, 0x1f, 0x11, 0xa6, 0x06,
-                0x7a, 0x7c, 0x84, 0x10, 0xad, 0xd3, 0x73, 0xcf,
-            },
-        },
-        .random_req[7] = {
-            .value = {
-                0xcb, 0x92, 0x18, 0xf6, 0x59, 0x6a, 0x1b, 0x18,
-                0x63, 0x72, 0x54, 0xc2, 0x1a, 0x3d, 0x09, 0x67,
-            },
-        },
-        .random_rsp[7] = {
-            .value = {
-                0xae, 0xf2, 0x96, 0xfd, 0xff, 0xd7, 0x18, 0xac,
-                0x5d, 0xb2, 0x9d, 0x89, 0x56, 0x2a, 0x19, 0xae,
-            },
-        },
-        .confirm_req[8] = {
-            .value = {
-                0x06, 0x8d, 0x5d, 0x19, 0xb3, 0x27, 0xc9, 0x6a,
-                0xe8, 0x58, 0xe7, 0x17, 0x10, 0x6a, 0xf9, 0xf7,
-            },
-        },
-        .confirm_rsp[8] = {
-            .value = {
-                0xf0, 0xbc, 0x2a, 0x03, 0x1f, 0x9b, 0x7b, 0x58,
-                0x43, 0x0f, 0xf5, 0x17, 0xc4, 0xbd, 0xec, 0x23,
-            },
-        },
-        .random_req[8] = {
-            .value = {
-                0xbe, 0x78, 0xcd, 0x84, 0x91, 0x4a, 0x1b, 0xdd,
-                0x6a, 0x0d, 0x88, 0x72, 0x9e, 0xc2, 0x4f, 0x5a,
-            },
-        },
-        .random_rsp[8] = {
-            .value = {
-                0xff, 0xac, 0xfe, 0x71, 0x2f, 0x6a, 0x13, 0xdc,
-                0xd3, 0x02, 0x81, 0x88, 0xbf, 0xc9, 0x9c, 0xd6,
-            },
-        },
-        .confirm_req[9] = {
-            .value = {
-                0xb0, 0x8d, 0x47, 0x23, 0x7e, 0xdb, 0xf5, 0x64,
-                0x5e, 0x83, 0x52, 0x9f, 0x06, 0x65, 0x84, 0x10,
-            },
-        },
-        .confirm_rsp[9] = {
-            .value = {
-                0x4d, 0x3f, 0xd4, 0x5a, 0x45, 0x57, 0xe9, 0xd7,
-                0x1e, 0x65, 0x7a, 0xa0, 0xd8, 0x5a, 0xa8, 0x29,
-            },
-        },
-        .random_req[9] = {
-            .value = {
-                0xb0, 0xcd, 0xfa, 0x39, 0x0d, 0x2e, 0x07, 0xfe,
-                0x36, 0x47, 0x8d, 0x8e, 0x1a, 0x47, 0x67, 0xf2,
-            },
-        },
-        .random_rsp[9] = {
-            .value = {
-                0xb4, 0xf5, 0x12, 0x64, 0xf4, 0xf6, 0xd7, 0x6e,
-                0xeb, 0x1e, 0x9a, 0x3f, 0x18, 0xba, 0xfb, 0x99,
-            },
-        },
-        .confirm_req[10] = {
-            .value = {
-                0xc9, 0x76, 0xb3, 0x3f, 0x80, 0xd9, 0x0c, 0xfb,
-                0xe3, 0x90, 0x1b, 0x7a, 0xbc, 0xe1, 0x7c, 0xde,
-            },
-        },
-        .confirm_rsp[10] = {
-            .value = {
-                0x21, 0x6a, 0x45, 0x6e, 0x6a, 0xac, 0xba, 0x9e,
-                0x66, 0x39, 0x5b, 0xb6, 0x74, 0xfe, 0x2b, 0x28,
-            },
-        },
-        .random_req[10] = {
-            .value = {
-                0xc0, 0xd4, 0xdf, 0x7b, 0x0f, 0x2f, 0xaa, 0x68,
-                0x4e, 0x3d, 0xa4, 0x59, 0x6f, 0x24, 0xe6, 0x7e,
-            },
-        },
-        .random_rsp[10] = {
-            .value = {
-                0xdf, 0x89, 0x49, 0xe7, 0x9f, 0x60, 0xdd, 0xf6,
-                0x44, 0x97, 0xe3, 0x15, 0x52, 0x65, 0x67, 0x3e,
-            },
-        },
-        .confirm_req[11] = {
-            .value = {
-                0xb0, 0x3f, 0x34, 0xce, 0x7d, 0x2e, 0xf1, 0xab,
-                0x23, 0xd5, 0x89, 0xf5, 0xaa, 0xa8, 0x59, 0x9f,
-            },
-        },
-        .confirm_rsp[11] = {
-            .value = {
-                0xb1, 0x33, 0x6a, 0x64, 0xd8, 0xeb, 0x8b, 0xa0,
-                0xf4, 0x1a, 0x15, 0x28, 0xb9, 0xe4, 0xa1, 0x31,
-            },
-        },
-        .random_req[11] = {
-            .value = {
-                0xd2, 0x88, 0x24, 0xfe, 0x95, 0x11, 0xc5, 0x0a,
-                0x21, 0xfb, 0x96, 0xea, 0x61, 0xb9, 0x8b, 0x26,
-            },
-        },
-        .random_rsp[11] = {
-            .value = {
-                0x8f, 0x22, 0x66, 0x8e, 0x7e, 0x62, 0x34, 0x37,
-                0xfc, 0x4a, 0x48, 0x1f, 0xf7, 0x38, 0x3b, 0x4e,
-            },
-        },
-        .confirm_req[12] = {
-            .value = {
-                0xc4, 0x50, 0xc8, 0x53, 0x58, 0xfb, 0xea, 0x9a,
-                0xdc, 0x35, 0xc7, 0xf3, 0x5b, 0x7c, 0xfb, 0xe4,
-            },
-        },
-        .confirm_rsp[12] = {
-            .value = {
-                0x27, 0xd9, 0x32, 0xd6, 0x43, 0xbf, 0x57, 0x3f,
-                0x35, 0x73, 0x3c, 0x3e, 0xbe, 0x53, 0x19, 0xff,
-            },
-        },
-        .random_req[12] = {
-            .value = {
-                0x99, 0xa1, 0x7a, 0x5f, 0xe0, 0x48, 0x1c, 0x6c,
-                0x84, 0xac, 0xab, 0xed, 0x69, 0x55, 0x1e, 0x66,
-            },
-        },
-        .random_rsp[12] = {
-            .value = {
-                0x37, 0x50, 0x90, 0x35, 0xef, 0x84, 0x06, 0x18,
-                0xfd, 0x3b, 0xc1, 0x8a, 0x46, 0x91, 0xb8, 0x21,
-            },
-        },
-        .confirm_req[13] = {
-            .value = {
-                0x2f, 0xcb, 0x3e, 0xc3, 0xce, 0x82, 0x0b, 0x5c,
-                0xdc, 0x9c, 0xbd, 0x44, 0xf9, 0x04, 0x22, 0x8c,
-            },
-        },
-        .confirm_rsp[13] = {
-            .value = {
-                0xab, 0xf2, 0x2e, 0x40, 0xd0, 0x74, 0x4f, 0xd4,
-                0x26, 0x9c, 0x89, 0x9e, 0x38, 0x77, 0xac, 0x9d,
-            },
-        },
-        .random_req[13] = {
-            .value = {
-                0xbc, 0xda, 0x58, 0xa2, 0x98, 0x88, 0xfe, 0x9f,
-                0x95, 0x0e, 0x3a, 0x91, 0xba, 0xe9, 0xbf, 0x02,
-            },
-        },
-        .random_rsp[13] = {
-            .value = {
-                0x04, 0xb9, 0x4c, 0x26, 0xce, 0x87, 0x8f, 0x17,
-                0xdc, 0xbc, 0x36, 0x94, 0x47, 0x67, 0x9f, 0xde,
-            },
-        },
-        .confirm_req[14] = {
-            .value = {
-                0xbd, 0xb6, 0x54, 0xc8, 0x1f, 0x51, 0x23, 0x98,
-                0x48, 0x3d, 0x47, 0x9d, 0xa3, 0xb8, 0xe7, 0x55,
-            },
-        },
-        .confirm_rsp[14] = {
-            .value = {
-                0x06, 0xc2, 0x7b, 0x80, 0x76, 0x9c, 0x37, 0x78,
-                0x46, 0xc5, 0x45, 0x43, 0x5d, 0x8d, 0x5b, 0x3e,
-            },
-        },
-        .random_req[14] = {
-            .value = {
-                0xef, 0x9e, 0x8a, 0x3a, 0xb7, 0xde, 0xa8, 0x07,
-                0x58, 0x73, 0xe0, 0x07, 0xfc, 0x62, 0xdb, 0x62,
-            },
-        },
-        .random_rsp[14] = {
-            .value = {
-                0xfa, 0xd5, 0xb2, 0x4e, 0x20, 0x01, 0x93, 0xc0,
-                0xb3, 0x76, 0xa5, 0x7a, 0x92, 0x8f, 0xb9, 0x6d,
-            },
-        },
-        .confirm_req[15] = {
-            .value = {
-                0x76, 0x2e, 0xc6, 0x64, 0x6c, 0x13, 0x01, 0x7e,
-                0x34, 0x78, 0x12, 0xb8, 0x1a, 0xb7, 0xf7, 0x39,
-            },
-        },
-        .confirm_rsp[15] = {
-            .value = {
-                0xbd, 0xae, 0x10, 0x32, 0xdb, 0x63, 0x30, 0x6f,
-                0x68, 0x19, 0x49, 0x5e, 0x34, 0x4f, 0x13, 0xc6,
-            },
-        },
-        .random_req[15] = {
-            .value = {
-                0x95, 0x2e, 0xe4, 0xe3, 0xb2, 0xdc, 0x79, 0xad,
-                0x5f, 0x0c, 0x19, 0x9c, 0x47, 0x9c, 0x79, 0x17,
-            },
-        },
-        .random_rsp[15] = {
-            .value = {
-                0x9e, 0x3d, 0x7f, 0xcd, 0x18, 0x40, 0xd7, 0xac,
-                0xa1, 0x45, 0x5f, 0xcb, 0x29, 0x57, 0x2b, 0x63,
-            },
-        },
-        .confirm_req[16] = {
-            .value = {
-                0x10, 0x18, 0x9d, 0xf2, 0xed, 0x76, 0x5c, 0x5f,
-                0x32, 0xa6, 0x29, 0x61, 0x12, 0xb2, 0xb8, 0xa2,
-            },
-        },
-        .confirm_rsp[16] = {
-            .value = {
-                0x3c, 0xd4, 0xbd, 0xe9, 0xd3, 0x29, 0xac, 0xf7,
-                0xfc, 0x04, 0xd3, 0xe4, 0x46, 0x14, 0x28, 0x2c,
-            },
-        },
-        .random_req[16] = {
-            .value = {
-                0x6d, 0xe8, 0x77, 0xc3, 0xab, 0x49, 0x6b, 0x79,
-                0x4f, 0x0f, 0x4c, 0x65, 0xc5, 0x77, 0x68, 0xd9,
-            },
-        },
-        .random_rsp[16] = {
-            .value = {
-                0xd0, 0x59, 0xf3, 0x53, 0xb1, 0x14, 0x81, 0x88,
-                0x26, 0x88, 0xef, 0x4b, 0xa4, 0x7d, 0x0a, 0x84,
-            },
-        },
-        .confirm_req[17] = {
-            .value = {
-                0xa3, 0x96, 0x9f, 0x96, 0x53, 0x0e, 0x38, 0x78,
-                0x9e, 0xbd, 0xf7, 0x65, 0x23, 0x73, 0x99, 0xa7,
-            },
-        },
-        .confirm_rsp[17] = {
-            .value = {
-                0x6b, 0x25, 0x8d, 0x51, 0xd8, 0xc4, 0xd9, 0xbf,
-                0xa6, 0x4f, 0xa3, 0x25, 0x28, 0xb5, 0x7c, 0x05,
-            },
-        },
-        .random_req[17] = {
-            .value = {
-                0xa5, 0xac, 0xd9, 0xb6, 0x9e, 0x98, 0x75, 0xae,
-                0x9b, 0x16, 0xe1, 0x60, 0xc6, 0xa5, 0x07, 0xf2,
-            },
-        },
-        .random_rsp[17] = {
-            .value = {
-                0x65, 0x53, 0x56, 0xe6, 0x2c, 0x22, 0x68, 0xc9,
-                0xb8, 0xbe, 0xb1, 0x40, 0x08, 0xe2, 0xb6, 0xb9,
-            },
-        },
-        .confirm_req[18] = {
-            .value = {
-                0x67, 0xcd, 0x0e, 0x4f, 0xfc, 0x38, 0x7f, 0x8a,
-                0x3b, 0xea, 0xff, 0x86, 0xf3, 0x8a, 0x92, 0xcb,
-            },
-        },
-        .confirm_rsp[18] = {
-            .value = {
-                0x22, 0x95, 0x1f, 0x20, 0xc9, 0x5c, 0x73, 0x39,
-                0xa4, 0xd9, 0xc1, 0x37, 0x9d, 0x94, 0xb2, 0xfd,
-            },
-        },
-        .random_req[18] = {
-            .value = {
-                0xe1, 0x80, 0x82, 0xdd, 0x21, 0x6c, 0xe4, 0x93,
-                0xa3, 0x41, 0x0f, 0xfc, 0x96, 0x42, 0x8b, 0xde,
-            },
-        },
-        .random_rsp[18] = {
-            .value = {
-                0x11, 0x1c, 0xd7, 0x7a, 0xe7, 0x1a, 0x88, 0xdd,
-                0x2a, 0xdf, 0xe5, 0x30, 0xca, 0x0b, 0x9f, 0xb6,
-            },
-        },
-        .confirm_req[19] = {
-            .value = {
-                0x45, 0x9b, 0x36, 0x3d, 0xf8, 0xc0, 0x43, 0x6d,
-                0x94, 0xcf, 0xbd, 0x5f, 0xfe, 0xec, 0xd7, 0x4b,
-            },
-        },
-        .confirm_rsp[19] = {
-            .value = {
-                0xf0, 0xaa, 0xfd, 0xae, 0xb7, 0x73, 0x3c, 0x9d,
-                0x93, 0xd4, 0x00, 0xea, 0x81, 0x31, 0xde, 0x41,
-            },
-        },
-        .random_req[19] = {
-            .value = {
-                0x1a, 0xaa, 0xff, 0x2a, 0xdc, 0xcc, 0x89, 0xbc,
-                0xcf, 0x48, 0x5c, 0x1e, 0x4d, 0x69, 0x85, 0x39,
-            },
-        },
-        .random_rsp[19] = {
-            .value = {
-                0xe7, 0xd0, 0xcb, 0x9a, 0xb5, 0x76, 0xec, 0xfc,
-                0x48, 0xa3, 0x41, 0x48, 0x4c, 0xa7, 0xec, 0xb7,
-            },
-        },
-        .dhkey_check_req = {
-            .value = {
-                0xe3, 0x4e, 0x42, 0xb5, 0xe3, 0x63, 0x4b, 0x7c,
-                0xf0, 0x9f, 0xef, 0x6e, 0x97, 0xe2, 0x86, 0xc0,
-            },
-        },
-        .dhkey_check_rsp = {
-            .value = {
-                0xea, 0x8a, 0xab, 0x7f, 0x15, 0x21, 0x5a, 0x36,
-                0x9b, 0x56, 0xee, 0x51, 0x61, 0x97, 0xe2, 0x0a,
-            },
-        },
-        .id_info_req = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_req = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-            },
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0x85, 0x54, 0x52, 0xe3, 0xb4, 0xe8, 0x26, 0xa4,
-                0x38, 0xb0, 0x4c, 0xa0, 0x41, 0xf5, 0x30, 0x6e,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0x6f, 0x93, 0xb8, 0x9c, 0x26, 0x88, 0xb4, 0x20,
-                0x87, 0x95, 0xf2, 0xf4, 0x3a, 0xbe, 0x92, 0xb7,
-            },
-        },
-        .ltk = {
-            0x30, 0xf6, 0xd3, 0x2e, 0x1c, 0x81, 0x2c, 0x96,
-            0x56, 0x30, 0x55, 0xec, 0x9b, 0x72, 0xf4, 0x83,
-        },
-        .pair_alg = BLE_SM_PAIR_ALG_PASSKEY,
-        .authenticated = 1,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_INPUT,
-                .passkey = 879894,
-            },
-        },
-    };
-    ble_sm_test_util_peer_sc_good(&params);
-}
-
-/**
- * Secure connections pairing
- * Master: peer
- * Pair algorithm: passkey entry
- * Initiator IO capabilities: 2
- * Responder IO capabilities: 0
- * Bonding: true
- * Initiator address type: 0
- * Responder address type: 0
- * Initiator key distribution: 5
- * Responder key distribution: 7
- */
-TEST_CASE(ble_sm_sc_peer_pk_iio2_rio0_b1_iat0_rat0_ik5_rk7)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
-        },
-        .resp_id_addr = {
-            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-        },
-        .pair_req = {
-            .io_cap = 0x02,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x0d,
-            .resp_key_dist = 0x0f,
-        },
-        .pair_rsp = {
-            .io_cap = 0x00,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x05,
-            .resp_key_dist = 0x07,
-        },
-        .our_priv_key = {
-            0xd6, 0x2f, 0x4f, 0x6b, 0xeb, 0xfc, 0xbd, 0xee,
-            0x9b, 0x94, 0xd7, 0x15, 0x98, 0xc6, 0x0c, 0x83,
-            0x9b, 0xc7, 0xa2, 0x45, 0xfd, 0x00, 0xe8, 0xa4,
-            0x52, 0xe9, 0x70, 0x2f, 0xd7, 0x62, 0xf1, 0xa4,
-        },
-        .public_key_req = {
-            .x = {
-                0xd6, 0xa7, 0xaf, 0xc1, 0x18, 0x8b, 0x92, 0x2f,
-                0xbc, 0xbc, 0x4d, 0xb8, 0x5c, 0xfb, 0x39, 0x7c,
-                0x1e, 0x90, 0x7e, 0xfa, 0xa2, 0x0d, 0xee, 0x9e,
-                0xb4, 0x9e, 0xbe, 0x50, 0xf0, 0xbc, 0x2c, 0x10,
-            },
-            .y = {
-                0xa4, 0x25, 0xad, 0x75, 0xbe, 0xab, 0x1e, 0xcf,
-                0x4e, 0xc8, 0x19, 0xab, 0x6c, 0x68, 0x38, 0xa4,
-                0xe7, 0x43, 0x7b, 0x19, 0xef, 0x28, 0xd5, 0x93,
-                0x52, 0xe9, 0xb9, 0x31, 0x68, 0x60, 0x19, 0x71,
-            },
-        },
-        .public_key_rsp = {
-            .x = {
-                0xbc, 0x6a, 0xcf, 0xc6, 0x8a, 0x3a, 0xdc, 0x89,
-                0xdd, 0xa9, 0xaf, 0x29, 0xc7, 0xaf, 0xe2, 0x8b,
-                0x25, 0xee, 0xce, 0xa6, 0x10, 0x1d, 0x33, 0x2f,
-                0xd5, 0xfc, 0x30, 0xb8, 0xb1, 0x7b, 0xb1, 0x6e,
-            },
-            .y = {
-                0x1a, 0xc6, 0x42, 0x36, 0x98, 0x40, 0x4f, 0x90,
-                0x82, 0xa0, 0x10, 0x3a, 0xa5, 0x0f, 0xcf, 0x57,
-                0xd2, 0x2e, 0x80, 0x9d, 0x61, 0xc7, 0x21, 0xac,
-                0x47, 0x5b, 0x93, 0x75, 0x02, 0x30, 0x40, 0x14,
-            },
-        },
-        .confirm_req[0] = {
-            .value = {
-                0xd1, 0x64, 0x49, 0xa0, 0xc4, 0x28, 0x81, 0x57,
-                0x0c, 0x25, 0x62, 0xfb, 0x2c, 0xa2, 0xb0, 0xc7,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0xea, 0xae, 0x4e, 0x03, 0x00, 0xf9, 0xd1, 0x65,
-                0xc7, 0x6a, 0x0d, 0x74, 0x4f, 0x02, 0x0b, 0x94,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0x05, 0xb2, 0x09, 0x9b, 0x36, 0x23, 0x4f, 0x74,
-                0x4e, 0xc9, 0x7a, 0x2c, 0x65, 0x3a, 0xd1, 0xf6,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0x50, 0xd8, 0x88, 0xd4, 0x7e, 0xc1, 0x36, 0x92,
-                0x0f, 0xa7, 0x17, 0x3c, 0xb4, 0xeb, 0xee, 0xa6,
-            },
-        },
-        .confirm_req[1] = {
-            .value = {
-                0xab, 0xa2, 0xd0, 0xec, 0xdd, 0xf3, 0xd2, 0xa9,
-                0x2d, 0xde, 0x4b, 0x02, 0x66, 0x45, 0x2f, 0xc0,
-            },
-        },
-        .confirm_rsp[1] = {
-            .value = {
-                0xa9, 0xc1, 0x9d, 0x75, 0xd0, 0xb6, 0xec, 0x06,
-                0x31, 0x87, 0xb6, 0x9d, 0x31, 0xdc, 0x92, 0x7c,
-            },
-        },
-        .random_req[1] = {
-            .value = {
-                0xb9, 0x5b, 0xe0, 0x0f, 0x83, 0xe7, 0x2d, 0x77,
-                0x2f, 0x55, 0x0a, 0x2c, 0xd9, 0xc1, 0x46, 0xcd,
-            },
-        },
-        .random_rsp[1] = {
-            .value = {
-                0xa2, 0x9a, 0x5b, 0x99, 0xb1, 0xc0, 0xc5, 0xd6,
-                0xf1, 0x87, 0x0b, 0x49, 0x9c, 0xfd, 0xfe, 0xd5,
-            },
-        },
-        .confirm_req[2] = {
-            .value = {
-                0x3a, 0x9d, 0x58, 0xe5, 0xb0, 0x31, 0xd9, 0xde,
-                0xac, 0xd2, 0x44, 0xb7, 0xe1, 0xe5, 0x89, 0x50,
-            },
-        },
-        .confirm_rsp[2] = {
-            .value = {
-                0xae, 0x4e, 0x4f, 0x84, 0x5f, 0x4c, 0xd1, 0x9b,
-                0x81, 0x22, 0x9c, 0x68, 0x52, 0xe0, 0x9a, 0xfc,
-            },
-        },
-        .random_req[2] = {
-            .value = {
-                0xa5, 0xbb, 0x5f, 0x9a, 0xa2, 0x97, 0xdb, 0xcd,
-                0x3d, 0xfe, 0xd9, 0x58, 0x21, 0x52, 0x99, 0xb7,
-            },
-        },
-        .random_rsp[2] = {
-            .value = {
-                0xea, 0x44, 0xdd, 0x0c, 0xbf, 0xb5, 0x6b, 0xc7,
-                0xe1, 0x19, 0xe8, 0x0b, 0xc2, 0x15, 0x04, 0x37,
-            },
-        },
-        .confirm_req[3] = {
-            .value = {
-                0xa8, 0xa3, 0xdb, 0x08, 0xca, 0x31, 0xd5, 0xef,
-                0x17, 0x37, 0x77, 0xd0, 0x64, 0x2e, 0x2f, 0x2f,
-            },
-        },
-        .confirm_rsp[3] = {
-            .value = {
-                0xe4, 0xf6, 0xa5, 0x94, 0x1a, 0x09, 0x4b, 0x75,
-                0x79, 0xb8, 0x0c, 0xe6, 0xe2, 0x28, 0x5a, 0x2c,
-            },
-        },
-        .random_req[3] = {
-            .value = {
-                0x1a, 0x3f, 0x80, 0x6f, 0xd3, 0xe8, 0xc5, 0xfb,
-                0x9b, 0xda, 0xa1, 0x07, 0x68, 0x1a, 0x54, 0xbc,
-            },
-        },
-        .random_rsp[3] = {
-            .value = {
-                0x1b, 0x48, 0x22, 0x87, 0x04, 0x24, 0x87, 0xba,
-                0x14, 0xb9, 0x85, 0xb2, 0xa6, 0xf5, 0xea, 0x89,
-            },
-        },
-        .confirm_req[4] = {
-            .value = {
-                0x31, 0xcb, 0xc4, 0x0c, 0x36, 0xb5, 0xe2, 0x32,
-                0xd8, 0x0e, 0xd3, 0x86, 0x96, 0xe3, 0x8c, 0x84,
-            },
-        },
-        .confirm_rsp[4] = {
-            .value = {
-                0x90, 0x11, 0x30, 0x35, 0x5f, 0xe5, 0x45, 0xff,
-                0xab, 0xd3, 0xe0, 0xbe, 0x1c, 0x20, 0x23, 0xb8,
-            },
-        },
-        .random_req[4] = {
-            .value = {
-                0xa0, 0xc7, 0x79, 0x28, 0x87, 0x19, 0xa3, 0x78,
-                0x33, 0xe5, 0x1a, 0x81, 0xba, 0x9b, 0xe3, 0x5c,
-            },
-        },
-        .random_rsp[4] = {
-            .value = {
-                0x43, 0xcf, 0x20, 0x1b, 0x39, 0x3f, 0xdf, 0x73,
-                0x58, 0xd2, 0x0d, 0xc7, 0x41, 0xd7, 0x58, 0xea,
-            },
-        },
-        .confirm_req[5] = {
-            .value = {
-                0x59, 0xda, 0x78, 0xeb, 0xd5, 0xcd, 0x8e, 0x23,
-                0xe5, 0x5e, 0xa7, 0xa5, 0xba, 0x13, 0x00, 0xff,
-            },
-        },
-        .confirm_rsp[5] = {
-            .value = {
-                0x31, 0x7a, 0xf0, 0x56, 0x82, 0x69, 0xdb, 0xcd,
-                0x27, 0x5a, 0x11, 0xd3, 0x65, 0x82, 0x0d, 0xda,
-            },
-        },
-        .random_req[5] = {
-            .value = {
-                0x2e, 0xe8, 0x76, 0x40, 0x9c, 0x49, 0x07, 0x42,
-                0x1e, 0x45, 0x7b, 0x1e, 0x73, 0xa3, 0x71, 0x05,
-            },
-        },
-        .random_rsp[5] = {
-            .value = {
-                0x64, 0x99, 0x42, 0x5d, 0x05, 0xd6, 0x12, 0x41,
-                0x2a, 0x44, 0x55, 0x26, 0xe7, 0x08, 0x5e, 0xfb,
-            },
-        },
-        .confirm_req[6] = {
-            .value = {
-                0x1c, 0x55, 0xe1, 0x75, 0x4f, 0x6e, 0xdd, 0x7e,
-                0xc8, 0xff, 0x76, 0x25, 0xdb, 0x2a, 0x6d, 0xe3,
-            },
-        },
-        .confirm_rsp[6] = {
-            .value = {
-                0xf6, 0x36, 0x78, 0x88, 0x62, 0xa8, 0x78, 0xe6,
-                0xf9, 0xa1, 0x17, 0x63, 0x86, 0xd3, 0xae, 0x60,
-            },
-        },
-        .random_req[6] = {
-            .value = {
-                0x96, 0x9a, 0x1c, 0xbe, 0x82, 0x82, 0xc2, 0xa7,
-                0x18, 0xc3, 0x7b, 0x40, 0x5d, 0x6c, 0x4e, 0xe3,
-            },
-        },
-        .random_rsp[6] = {
-            .value = {
-                0x2b, 0x7d, 0x36, 0xc3, 0xf7, 0x59, 0x63, 0x40,
-                0x6f, 0xc0, 0x2a, 0x2b, 0x1b, 0xd7, 0x41, 0x38,
-            },
-        },
-        .confirm_req[7] = {
-            .value = {
-                0x88, 0x99, 0x53, 0xae, 0x2a, 0xaf, 0x97, 0x5a,
-                0xcc, 0x9f, 0xfd, 0xe2, 0x1d, 0xd3, 0x27, 0x66,
-            },
-        },
-        .confirm_rsp[7] = {
-            .value = {
-                0xdb, 0xae, 0xfb, 0xf7, 0x33, 0xd4, 0xd1, 0xcb,
-                0xfe, 0x75, 0x8e, 0x81, 0x16, 0xd1, 0x49, 0xeb,
-            },
-        },
-        .random_req[7] = {
-            .value = {
-                0x13, 0x5c, 0x00, 0x34, 0xe5, 0x96, 0xd0, 0x97,
-                0xb1, 0x84, 0x3d, 0x00, 0xb4, 0x2a, 0x4a, 0x12,
-            },
-        },
-        .random_rsp[7] = {
-            .value = {
-                0xed, 0x94, 0x1f, 0x41, 0x12, 0xe5, 0x35, 0x5b,
-                0xa6, 0x6a, 0x72, 0x1e, 0xa2, 0x7c, 0xe1, 0x6c,
-            },
-        },
-        .confirm_req[8] = {
-            .value = {
-                0xa3, 0xc7, 0x17, 0xad, 0xb6, 0xe6, 0xaa, 0x16,
-                0x8d, 0x4b, 0x70, 0x5f, 0x49, 0x73, 0xa7, 0x19,
-            },
-        },
-        .confirm_rsp[8] = {
-            .value = {
-                0x10, 0xb0, 0x31, 0xa7, 0x16, 0x61, 0xf7, 0xd6,
-                0xe6, 0x16, 0x9e, 0xb1, 0x9e, 0xb5, 0x5e, 0x94,
-            },
-        },
-        .random_req[8] = {
-            .value = {
-                0x6f, 0xe7, 0x62, 0x73, 0xfb, 0xbf, 0xf1, 0x4a,
-                0x14, 0xa1, 0x09, 0x45, 0xd4, 0xde, 0x26, 0xad,
-            },
-        },
-        .random_rsp[8] = {
-            .value = {
-                0x3f, 0x48, 0xa7, 0xdf, 0x4a, 0xd5, 0x55, 0x26,
-                0xd3, 0x32, 0xbf, 0x98, 0x4a, 0x20, 0xad, 0xb0,
-            },
-        },
-        .confirm_req[9] = {
-            .value = {
-                0x88, 0x1c, 0xef, 0xfe, 0x4e, 0x68, 0x41, 0x7c,
-                0xe8, 0xe8, 0x81, 0x1a, 0xb9, 0x9e, 0xaf, 0xc6,
-            },
-        },
-        .confirm_rsp[9] = {
-            .value = {
-                0xa3, 0x53, 0x2a, 0xe1, 0xbd, 0x9d, 0xbe, 0x89,
-                0xf8, 0xc7, 0x70, 0x6e, 0xa9, 0x12, 0x07, 0x0d,
-            },
-        },
-        .random_req[9] = {
-            .value = {
-                0x52, 0x06, 0x56, 0x09, 0xf4, 0xb2, 0xb9, 0x63,
-                0x3f, 0x2e, 0x59, 0x6c, 0x6b, 0x43, 0xb6, 0xc0,
-            },
-        },
-        .random_rsp[9] = {
-            .value = {
-                0x36, 0xb0, 0x33, 0x84, 0x52, 0xd1, 0x60, 0xac,
-                0x37, 0x81, 0x6b, 0x18, 0x5f, 0xfc, 0x61, 0xb1,
-            },
-        },
-        .confirm_req[10] = {
-            .value = {
-                0xc8, 0x55, 0xb7, 0x9e, 0x3e, 0xf0, 0x26, 0xa4,
-                0x55, 0xb3, 0x1d, 0x4d, 0xa1, 0x5d, 0xa9, 0xaf,
-            },
-        },
-        .confirm_rsp[10] = {
-            .value = {
-                0xb7, 0xb9, 0x6b, 0x8e, 0xef, 0xd3, 0xbc, 0x58,
-                0x10, 0xbe, 0x5a, 0x9a, 0x4d, 0xbc, 0xec, 0xe3,
-            },
-        },
-        .random_req[10] = {
-            .value = {
-                0x55, 0xa1, 0xf4, 0xd7, 0xfa, 0xe1, 0x84, 0x03,
-                0xed, 0xb6, 0x95, 0x63, 0x4b, 0x93, 0x93, 0xc2,
-            },
-        },
-        .random_rsp[10] = {
-            .value = {
-                0x72, 0xa9, 0xe5, 0xf7, 0x48, 0x1f, 0x64, 0x71,
-                0xd9, 0x81, 0xf0, 0xc5, 0x4d, 0x38, 0xac, 0x9a,
-            },
-        },
-        .confirm_req[11] = {
-            .value = {
-                0x12, 0x37, 0x56, 0xa6, 0x66, 0xa1, 0x23, 0xee,
-                0xe3, 0x1e, 0x20, 0x66, 0x66, 0x85, 0x7c, 0xa8,
-            },
-        },
-        .confirm_rsp[11] = {
-            .value = {
-                0x74, 0xaa, 0xbb, 0x5a, 0xdf, 0xd9, 0xc4, 0xaf,
-                0xe4, 0xa7, 0xe6, 0x4b, 0x45, 0x97, 0xf8, 0x7d,
-            },
-        },
-        .random_req[11] = {
-            .value = {
-                0x29, 0xce, 0xcc, 0xb7, 0xb2, 0x1e, 0x0e, 0xa8,
-                0x48, 0x90, 0x43, 0x6d, 0x34, 0xa4, 0xa3, 0x12,
-            },
-        },
-        .random_rsp[11] = {
-            .value = {
-                0x2e, 0xaf, 0x4c, 0x63, 0x84, 0x2c, 0x62, 0x67,
-                0x68, 0x8f, 0x0b, 0xfd, 0xff, 0xef, 0x15, 0x26,
-            },
-        },
-        .confirm_req[12] = {
-            .value = {
-                0xec, 0xcf, 0x6a, 0x60, 0x77, 0x04, 0x2c, 0x62,
-                0x42, 0xf0, 0x21, 0xfd, 0x53, 0xd6, 0x8a, 0xe8,
-            },
-        },
-        .confirm_rsp[12] = {
-            .value = {
-                0x2c, 0x13, 0x65, 0x69, 0xd7, 0x66, 0x04, 0x13,
-                0x3c, 0xa8, 0xfb, 0xe5, 0x76, 0xbb, 0x4f, 0x48,
-            },
-        },
-        .random_req[12] = {
-            .value = {
-                0x0d, 0x93, 0x30, 0xe2, 0x76, 0xf1, 0xbc, 0x24,
-                0x61, 0x0d, 0xcd, 0xef, 0x33, 0x98, 0xe2, 0x3b,
-            },
-        },
-        .random_rsp[12] = {
-            .value = {
-                0xb6, 0x32, 0x69, 0x81, 0xc0, 0x81, 0x46, 0xae,
-                0x8d, 0x5a, 0x17, 0xb5, 0xc0, 0x0f, 0x9f, 0x4e,
-            },
-        },
-        .confirm_req[13] = {
-            .value = {
-                0x89, 0x96, 0x22, 0x0c, 0x76, 0xdf, 0x27, 0x13,
-                0x96, 0x5a, 0x0c, 0x88, 0x65, 0x18, 0x74, 0x52,
-            },
-        },
-        .confirm_rsp[13] = {
-            .value = {
-                0x1c, 0x77, 0x25, 0x22, 0xc0, 0x28, 0x88, 0x45,
-                0x29, 0x62, 0x7a, 0x8e, 0xc0, 0x2a, 0x5c, 0xd8,
-            },
-        },
-        .random_req[13] = {
-            .value = {
-                0xcc, 0x84, 0xb6, 0x98, 0x3e, 0xf9, 0x09, 0xd2,
-                0x71, 0x47, 0x56, 0xb1, 0x09, 0xf5, 0xd2, 0x0b,
-            },
-        },
-        .random_rsp[13] = {
-            .value = {
-                0xf0, 0xcf, 0x1c, 0xa6, 0x24, 0xcd, 0xfa, 0x42,
-                0xa4, 0x93, 0x8b, 0xa0, 0xe3, 0x42, 0x72, 0x51,
-            },
-        },
-        .confirm_req[14] = {
-            .value = {
-                0xab, 0xb0, 0xa3, 0x80, 0x0d, 0xcb, 0x8e, 0xf6,
-                0x6c, 0x07, 0x50, 0xe9, 0x8a, 0x85, 0x02, 0xae,
-            },
-        },
-        .confirm_rsp[14] = {
-            .value = {
-                0xf6, 0x52, 0xd8, 0x34, 0x15, 0x62, 0x9f, 0x6e,
-                0x2b, 0x52, 0xdc, 0x1c, 0x70, 0x17, 0x0a, 0x31,
-            },
-        },
-        .random_req[14] = {
-            .value = {
-                0x8d, 0xc9, 0x0a, 0x45, 0xe9, 0x81, 0x0d, 0x5e,
-                0xbb, 0xd8, 0x94, 0x29, 0x68, 0x42, 0x44, 0xe2,
-            },
-        },
-        .random_rsp[14] = {
-            .value = {
-                0x96, 0x2a, 0x35, 0x39, 0x09, 0xf7, 0x66, 0x5a,
-                0xb6, 0x33, 0x77, 0x6d, 0xba, 0xd3, 0x8a, 0xfb,
-            },
-        },
-        .confirm_req[15] = {
-            .value = {
-                0x53, 0x08, 0x9b, 0x37, 0xc3, 0x79, 0xe6, 0x8c,
-                0x42, 0x30, 0x94, 0x73, 0x6f, 0x39, 0x64, 0x20,
-            },
-        },
-        .confirm_rsp[15] = {
-            .value = {
-                0x4d, 0xb7, 0xe9, 0x50, 0x8e, 0x0f, 0xe0, 0xd5,
-                0x3e, 0xf6, 0x32, 0xdd, 0xb8, 0x18, 0x77, 0xd3,
-            },
-        },
-        .random_req[15] = {
-            .value = {
-                0x8d, 0x49, 0x14, 0xdd, 0x95, 0x57, 0x55, 0x14,
-                0x48, 0x97, 0xd3, 0x73, 0x29, 0xa0, 0xb9, 0x2b,
-            },
-        },
-        .random_rsp[15] = {
-            .value = {
-                0xcf, 0x38, 0x8b, 0xab, 0xe4, 0x2b, 0x3f, 0x13,
-                0xc3, 0xfb, 0x07, 0xee, 0x0e, 0x33, 0x2f, 0x04,
-            },
-        },
-        .confirm_req[16] = {
-            .value = {
-                0xc6, 0x58, 0x13, 0x19, 0x56, 0x06, 0x52, 0x4b,
-                0x3d, 0x5e, 0x9d, 0xa8, 0x48, 0xf2, 0x40, 0xf3,
-            },
-        },
-        .confirm_rsp[16] = {
-            .value = {
-                0xbb, 0x93, 0xd2, 0xed, 0x89, 0x66, 0xa5, 0x1c,
-                0xc9, 0x2a, 0x42, 0x2c, 0xff, 0x4a, 0x80, 0x84,
-            },
-        },
-        .random_req[16] = {
-            .value = {
-                0x3d, 0x9c, 0x11, 0x2a, 0xd3, 0xce, 0x4b, 0x20,
-                0xf2, 0xfb, 0xdd, 0x18, 0x4d, 0x7c, 0x58, 0xb6,
-            },
-        },
-        .random_rsp[16] = {
-            .value = {
-                0xda, 0x80, 0x63, 0x9d, 0xa2, 0x73, 0x61, 0xdd,
-                0x9a, 0x45, 0x91, 0x4d, 0x78, 0x39, 0x54, 0x75,
-            },
-        },
-        .confirm_req[17] = {
-            .value = {
-                0x2e, 0xe4, 0x44, 0xe8, 0xdb, 0xc2, 0xbd, 0x62,
-                0xd1, 0xc4, 0x23, 0x4e, 0x5f, 0x65, 0xb6, 0x3b,
-            },
-        },
-        .confirm_rsp[17] = {
-            .value = {
-                0x19, 0x91, 0xa3, 0xc7, 0x3b, 0x68, 0x12, 0x24,
-                0xcd, 0xd6, 0x02, 0xf5, 0xcd, 0x19, 0x6c, 0x88,
-            },
-        },
-        .random_req[17] = {
-            .value = {
-                0xf0, 0x28, 0x18, 0xe8, 0xa7, 0x3e, 0xd8, 0x21,
-                0x42, 0x58, 0xb3, 0x72, 0xa0, 0x34, 0x89, 0x04,
-            },
-        },
-        .random_rsp[17] = {
-            .value = {
-                0xe9, 0xff, 0x0b, 0x9a, 0xfd, 0x29, 0x95, 0x37,
-                0x89, 0x2c, 0x84, 0xfa, 0x02, 0xa0, 0xb6, 0xeb,
-            },
-        },
-        .confirm_req[18] = {
-            .value = {
-                0x4f, 0x90, 0x70, 0xbe, 0xc4, 0x81, 0x9f, 0xc1,
-                0x74, 0xa3, 0x01, 0x2e, 0x78, 0x7a, 0xe2, 0x61,
-            },
-        },
-        .confirm_rsp[18] = {
-            .value = {
-                0xbb, 0xd5, 0x91, 0xec, 0x81, 0xe0, 0x9b, 0x5e,
-                0xe9, 0xd2, 0x93, 0x57, 0xa8, 0x27, 0xdd, 0x9b,
-            },
-        },
-        .random_req[18] = {
-            .value = {
-                0x78, 0xa4, 0x35, 0x1a, 0xbc, 0xa7, 0x19, 0x8c,
-                0x96, 0x8f, 0x63, 0x9d, 0x11, 0xee, 0x27, 0x44,
-            },
-        },
-        .random_rsp[18] = {
-            .value = {
-                0x39, 0x5b, 0x71, 0xfd, 0x7e, 0x39, 0x6b, 0xbe,
-                0xaf, 0xe1, 0x55, 0x90, 0xa6, 0x58, 0xec, 0xc5,
-            },
-        },
-        .confirm_req[19] = {
-            .value = {
-                0x91, 0x43, 0xe5, 0xc8, 0x26, 0x0c, 0x8c, 0x6c,
-                0xf3, 0xd1, 0x30, 0xb3, 0x22, 0x94, 0x4c, 0x67,
-            },
-        },
-        .confirm_rsp[19] = {
-            .value = {
-                0x51, 0xc4, 0x3e, 0x09, 0xca, 0x03, 0xbe, 0x2c,
-                0xe8, 0x1a, 0x5d, 0x07, 0x12, 0x14, 0x2d, 0x43,
-            },
-        },
-        .random_req[19] = {
-            .value = {
-                0x2f, 0xa1, 0x20, 0xde, 0xf5, 0xb4, 0xa6, 0x92,
-                0x31, 0xe9, 0x86, 0x63, 0xef, 0xc1, 0x85, 0x3b,
-            },
-        },
-        .random_rsp[19] = {
-            .value = {
-                0x41, 0xd0, 0xd0, 0x96, 0x93, 0xd1, 0xcb, 0xed,
-                0xab, 0x27, 0xd5, 0x88, 0x5e, 0xe6, 0x5e, 0x5c,
-            },
-        },
-        .dhkey_check_req = {
-            .value = {
-                0xec, 0xc5, 0x5f, 0xf3, 0xae, 0xfe, 0x79, 0x65,
-                0x17, 0x5a, 0x60, 0xf7, 0x36, 0x4f, 0x90, 0x45,
-            },
-        },
-        .dhkey_check_rsp = {
-            .value = {
-                0xa7, 0x45, 0x7a, 0x54, 0x1b, 0x64, 0x08, 0x60,
-                0x51, 0x7d, 0x74, 0x27, 0x48, 0xa2, 0xf1, 0x0f,
-            },
-        },
-        .id_info_req = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_req = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-            },
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0x36, 0x9a, 0xd9, 0x25, 0x5c, 0xdb, 0x78, 0xdc,
-                0x1d, 0x2c, 0x83, 0xf7, 0xde, 0x99, 0xa0, 0x66,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0x61, 0x6e, 0x9a, 0x26, 0xc5, 0xd0, 0x85, 0xdc,
-                0xea, 0x9d, 0xca, 0x3b, 0x17, 0xd7, 0x43, 0x80,
-            },
-        },
-        .ltk = {
-            0xd6, 0x02, 0xba, 0x3d, 0xa2, 0xce, 0x93, 0x1a,
-            0xfd, 0xd6, 0xb5, 0x54, 0x90, 0xc4, 0x2a, 0x8f,
-        },
-        .pair_alg = BLE_SM_PAIR_ALG_PASSKEY,
-        .authenticated = 1,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_DISP,
-                .passkey = 222333,
-            },
-        },
-    };
-    ble_sm_test_util_peer_sc_good(&params);
-}
-
-/**
- * Secure connections pairing
- * Master: peer
- * Pair algorithm: numeric comparison
- * Initiator IO capabilities: 1
- * Responder IO capabilities: 1
- * Bonding: true
- * Initiator address type: 0
- * Responder address type: 0
- * Initiator key distribution: 5
- * Responder key distribution: 7
- */
-TEST_CASE(ble_sm_sc_peer_nc_iio1_rio1_b1_iat0_rat0_ik5_rk7)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
-        },
-        .resp_id_addr = {
-            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-        },
-        .pair_req = {
-            .io_cap = 0x01,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x0d,
-            .resp_key_dist = 0x0f,
-        },
-        .pair_rsp = {
-            .io_cap = 0x01,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x05,
-            .resp_key_dist = 0x07,
-        },
-        .our_priv_key = {
-            0xd6, 0x2f, 0x4f, 0x6b, 0xeb, 0xfc, 0xbd, 0xee,
-            0x9b, 0x94, 0xd7, 0x15, 0x98, 0xc6, 0x0c, 0x83,
-            0x9b, 0xc7, 0xa2, 0x45, 0xfd, 0x00, 0xe8, 0xa4,
-            0x52, 0xe9, 0x70, 0x2f, 0xd7, 0x62, 0xf1, 0xa4,
-        },
-        .public_key_req = {
-            .x = {
-                0x41, 0x0d, 0x95, 0x8a, 0x68, 0xb8, 0xcf, 0x07,
-                0x58, 0x25, 0x5f, 0x97, 0xd2, 0x99, 0x71, 0x44,
-                0x06, 0xfc, 0x9c, 0x4d, 0xd1, 0x74, 0x80, 0xed,
-                0x49, 0xd1, 0x36, 0x6b, 0x55, 0x8b, 0x54, 0x3b,
-            },
-            .y = {
-                0x0f, 0x1a, 0x61, 0x45, 0xe5, 0x4b, 0x11, 0x13,
-                0xb3, 0x15, 0x87, 0x09, 0xec, 0x16, 0xf8, 0x41,
-                0x2e, 0xe2, 0x15, 0x93, 0x14, 0x56, 0x9f, 0xcd,
-                0x60, 0x7d, 0x92, 0xec, 0xd3, 0xb5, 0x85, 0xc5,
-            },
-        },
-        .public_key_rsp = {
-            .x = {
-                0xbc, 0x6a, 0xcf, 0xc6, 0x8a, 0x3a, 0xdc, 0x89,
-                0xdd, 0xa9, 0xaf, 0x29, 0xc7, 0xaf, 0xe2, 0x8b,
-                0x25, 0xee, 0xce, 0xa6, 0x10, 0x1d, 0x33, 0x2f,
-                0xd5, 0xfc, 0x30, 0xb8, 0xb1, 0x7b, 0xb1, 0x6e,
-            },
-            .y = {
-                0x1a, 0xc6, 0x42, 0x36, 0x98, 0x40, 0x4f, 0x90,
-                0x82, 0xa0, 0x10, 0x3a, 0xa5, 0x0f, 0xcf, 0x57,
-                0xd2, 0x2e, 0x80, 0x9d, 0x61, 0xc7, 0x21, 0xac,
-                0x47, 0x5b, 0x93, 0x75, 0x02, 0x30, 0x40, 0x14,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0x73, 0xc8, 0x56, 0x5e, 0x33, 0x37, 0x26, 0xb6,
-                0x00, 0x65, 0x9c, 0xa1, 0xee, 0xbf, 0x61, 0xf6,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0x7c, 0x23, 0x03, 0x70, 0x54, 0xa2, 0x70, 0xe4,
-                0x2d, 0xe9, 0x88, 0x6f, 0x40, 0xd6, 0x2f, 0xb2,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0x2d, 0x9f, 0xe8, 0x1d, 0xf2, 0x4e, 0x2e, 0x58,
-                0x16, 0x8c, 0x83, 0x89, 0x92, 0x70, 0xa2, 0xba,
-            },
-        },
-        .dhkey_check_req = {
-            .value = {
-                0xc0, 0x8a, 0x1c, 0xff, 0x7f, 0xd6, 0xbc, 0xee,
-                0x19, 0xa5, 0xc6, 0x3a, 0xbd, 0x48, 0x4b, 0xc3,
-            },
-        },
-        .dhkey_check_rsp = {
-            .value = {
-                0x38, 0x36, 0x83, 0xd5, 0x1a, 0xfb, 0xe6, 0x3c,
-                0x80, 0x0c, 0x81, 0x81, 0x78, 0x12, 0x41, 0x38,
-            },
-        },
-        .id_info_req = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_req = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
-            },
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0x52, 0xf4, 0xcc, 0x2f, 0xc6, 0xc1, 0xdb, 0x07,
-                0xa5, 0x38, 0xc1, 0x09, 0x82, 0x2e, 0xa3, 0x53,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0xc1, 0xa3, 0x62, 0x6a, 0x9e, 0xaa, 0x37, 0xd9,
-                0x65, 0x9f, 0x7f, 0x5d, 0x62, 0x0c, 0x1c, 0x6c,
-            },
-        },
-        .ltk = {
-            0xd8, 0x7f, 0x0a, 0x94, 0x41, 0xa5, 0xfd, 0x84,
-            0x15, 0x01, 0xb7, 0x2a, 0x7a, 0xe4, 0xfd, 0xfb,
-        },
-        .pair_alg = BLE_SM_PAIR_ALG_NUMCMP,
-        .authenticated = 1,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_NUMCMP,
-                .numcmp_accept = 1,
-            },
-            .exp_numcmp = 516214,
-        },
-    };
-    ble_sm_test_util_peer_sc_good(&params);
-}
-
-/**
- * Secure connections pairing
- * Master: us
- * Pair algorithm: just works
- * Initiator IO capabilities: 3
- * Responder IO capabilities: 4
- * Bonding: true
- * Initiator address type: 0
- * Responder address type: 0
- * Initiator key distribution: 7
- * Responder key distribution: 5
- */
-TEST_CASE(ble_sm_sc_us_jw_iio3_rio4_b1_iat0_rat0_ik7_rk5)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
-        },
-        .resp_id_addr = {
-            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
-        },
-        .pair_req = {
-            .io_cap = 0x03,
-            .oob_data_flag = 0x00,
-            .authreq = 0x09,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_rsp = {
-            .io_cap = 0x04,
-            .oob_data_flag = 0x00,
-            .authreq = 0x09,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x05,
-        },
-        .our_priv_key = {
-            0xaf, 0xce, 0x12, 0x45, 0xa8, 0xe0, 0xa9, 0x45,
-            0x8a, 0x56, 0xc5, 0xbf, 0x3b, 0xf9, 0x04, 0x69,
-            0xf2, 0xf9, 0xe4, 0xd4, 0x7e, 0xb7, 0xc9, 0x65,
-            0xb1, 0x68, 0x3e, 0xab, 0xcd, 0x8e, 0x6f, 0x1f,
-        },
-        .public_key_req = {
-            .x = {
-                0x45, 0xca, 0xda, 0xe3, 0x65, 0x7c, 0xf5, 0x37,
-                0x36, 0x66, 0x8b, 0x3b, 0x54, 0xb9, 0x2b, 0xb2,
-                0x09, 0xd5, 0x6e, 0xe0, 0x04, 0x1d, 0xd6, 0x49,
-                0xff, 0x55, 0x41, 0x35, 0xa0, 0x2f, 0x12, 0xee,
-            },
-            .y = {
-                0x65, 0x41, 0xd3, 0x7b, 0x59, 0xf2, 0xaf, 0x94,
-                0x78, 0xd8, 0x63, 0xc4, 0x9b, 0x9a, 0x9a, 0x92,
-                0x33, 0x0f, 0x14, 0x67, 0x98, 0x51, 0x9d, 0xff,
-                0xef, 0x59, 0xb7, 0x17, 0xc2, 0x16, 0x72, 0x18,
-            },
-        },
-        .public_key_rsp = {
-            .x = {
-                0x9e, 0x44, 0x09, 0x57, 0xb7, 0x01, 0x78, 0x5b,
-                0x4e, 0x50, 0x0d, 0x99, 0x0d, 0x52, 0x88, 0x24,
-                0x19, 0xf5, 0x40, 0x53, 0x06, 0x1e, 0x68, 0xd0,
-                0xfd, 0xd2, 0x84, 0x8b, 0xae, 0x9d, 0xf7, 0xd9,
-            },
-            .y = {
-                0xc2, 0xe7, 0xe0, 0x01, 0xb3, 0x2a, 0x1b, 0x01,
-                0x19, 0xd1, 0x14, 0xb5, 0xc8, 0x98, 0x02, 0x2a,
-                0xbe, 0x6b, 0x33, 0x1a, 0x99, 0x18, 0x77, 0x23,
-                0xd4, 0x8b, 0x8c, 0x09, 0xf5, 0x77, 0x20, 0xa0,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0xbd, 0x85, 0xbe, 0x80, 0xd9, 0x77, 0x16, 0xa3,
-                0x65, 0x1a, 0xdf, 0xff, 0x5a, 0x6f, 0x8b, 0x37,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0xb5, 0x59, 0x7c, 0x8e, 0x7b, 0x01, 0xac, 0x09,
-                0x8f, 0xe8, 0x97, 0x98, 0x8d, 0x3f, 0xb7, 0x63,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0x86, 0x1f, 0x76, 0x11, 0x2e, 0x83, 0xed, 0x99,
-                0x9b, 0xc0, 0x9a, 0xab, 0x7f, 0x94, 0x20, 0xcb,
-            },
-        },
-        .dhkey_check_req = {
-            .value = {
-                0xe0, 0x9f, 0x87, 0x87, 0x9f, 0x82, 0xc5, 0x06,
-                0x5f, 0x11, 0xfa, 0xa0, 0xe3, 0xbf, 0x72, 0xf2,
-            },
-        },
-        .dhkey_check_rsp = {
-            .value = {
-                0x26, 0xc2, 0xf1, 0xb9, 0xf1, 0xc2, 0xbd, 0xcb,
-                0xdb, 0x94, 0x96, 0x8e, 0x08, 0xcc, 0x53, 0xd4,
-            },
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0x74, 0x14, 0xcd, 0x5a, 0x49, 0x2e, 0xb6, 0x0d,
-                0xc6, 0x82, 0xb0, 0x0f, 0x9c, 0xe6, 0xe5, 0x41,
-            },
-        },
-        .id_info_rsp = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_rsp = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0xfb, 0x93, 0xa2, 0xb7, 0x4d, 0x0e, 0xcc, 0x92,
-                0xe4, 0xbf, 0x5b, 0x3c, 0x6d, 0x87, 0x5b, 0x2d,
-            },
-        },
-        .ltk = {
-            0x2e, 0x6c, 0x8b, 0xdb, 0x9e, 0x19, 0x3e, 0x3d,
-            0x4d, 0x6d, 0x29, 0xbc, 0x89, 0xca, 0x57, 0xed,
-        },
-        .pair_alg = BLE_SM_PAIR_ALG_JW,
-        .authenticated = 0,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_NONE,
-            },
-        },
-    };
-    ble_sm_test_util_us_sc_good(&params);
-}
-
-/**
- * Secure connections pairing
- * Master: us
- * Pair algorithm: passkey entry
- * Initiator IO capabilities: 2
- * Responder IO capabilities: 4
- * Bonding: true
- * Initiator address type: 0
- * Responder address type: 0
- * Initiator key distribution: 7
- * Responder key distribution: 5
- */
-TEST_CASE(ble_sm_sc_us_pk_iio2_rio4_b1_iat0_rat0_ik7_rk5)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
-        },
-        .resp_id_addr = {
-            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
-        },
-        .pair_req = {
-            .io_cap = 0x02,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_rsp = {
-            .io_cap = 0x04,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x05,
-        },
-        .our_priv_key = {
-            0xb1, 0x6b, 0x4f, 0x81, 0xbc, 0xe3, 0x60, 0x9e,
-            0x00, 0x20, 0xf1, 0x73, 0x3e, 0xfb, 0xcc, 0x6e,
-            0x8c, 0xb6, 0xd2, 0x51, 0xd9, 0x36, 0x8a, 0x6d,
-            0xca, 0x8c, 0xd7, 0xbe, 0x96, 0x03, 0xdf, 0xd6,
-        },
-        .public_key_req = {
-            .x = {
-                0xe5, 0x0f, 0x02, 0x0a, 0x37, 0x90, 0x94, 0x5a,
-                0x06, 0x21, 0xf7, 0xbc, 0xd5, 0xbe, 0xb9, 0x24,
-                0x8a, 0x35, 0xfd, 0xf8, 0x5e, 0xe2, 0x70, 0xd5,
-                0x5a, 0xe8, 0xe7, 0xdd, 0x13, 0x90, 0xeb, 0xd4,
-            },
-            .y = {
-                0x41, 0xc8, 0x51, 0x1a, 0x25, 0x44, 0x01, 0x53,
-                0x42, 0x74, 0x07, 0x9c, 0x18, 0xe6, 0x3b, 0x8a,
-                0xce, 0x7a, 0x37, 0x1f, 0x18, 0x5c, 0x02, 0x7c,
-                0x67, 0x16, 0xf5, 0x30, 0x2b, 0x31, 0xa9, 0xc7,
-            },
-        },
-        .public_key_rsp = {
-            .x = {
-                0x1b, 0xd2, 0x03, 0x79, 0xb4, 0x9b, 0x0a, 0xd7,
-                0x1b, 0x28, 0x73, 0x2a, 0xd7, 0xe6, 0xa0, 0xd4,
-                0x2d, 0x95, 0x8d, 0x29, 0xaf, 0x6a, 0xab, 0xee,
-                0xa0, 0x0d, 0x13, 0x4d, 0xe7, 0x16, 0x76, 0x91,
-            },
-            .y = {
-                0x2a, 0x26, 0x2c, 0x50, 0x55, 0xd1, 0x2b, 0x83,
-                0xf6, 0x5f, 0xdb, 0x99, 0x5f, 0x85, 0xf6, 0x78,
-                0x1c, 0x14, 0xed, 0xd3, 0x70, 0x5e, 0xe5, 0x2c,
-                0x05, 0x1e, 0x5c, 0xec, 0xf8, 0x65, 0x43, 0x49,
-            },
-        },
-        .confirm_req[0] = {
-            .value = {
-                0x55, 0x2c, 0xaa, 0x41, 0x59, 0x42, 0x4d, 0xfe,
-                0x47, 0x74, 0xcd, 0x2b, 0x11, 0xab, 0x21, 0xe6,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0x6a, 0x3c, 0x45, 0xf5, 0xb2, 0xe2, 0x04, 0x30,
-                0xde, 0xd6, 0x3c, 0x6d, 0x85, 0x00, 0x00, 0x2c,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0x78, 0x06, 0x04, 0x60, 0x76, 0xe9, 0xc4, 0x5a,
-                0xfb, 0x34, 0x44, 0xae, 0x45, 0xa0, 0x84, 0xde,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0x91, 0xc8, 0xfd, 0x1b, 0xb2, 0x85, 0x08, 0x76,
-                0xd3, 0xf1, 0xc4, 0xa0, 0xfa, 0x92, 0x8c, 0x94,
-            },
-        },
-        .confirm_req[1] = {
-            .value = {
-                0xb1, 0x2f, 0x68, 0x35, 0xa1, 0xa5, 0x84, 0xb1,
-                0x4f, 0x1a, 0xb1, 0xb5, 0xf0, 0xb2, 0xbe, 0x61,
-            },
-        },
-        .confirm_rsp[1] = {
-            .value = {
-                0x07, 0xd8, 0x43, 0x74, 0xe8, 0x42, 0xf3, 0xf1,
-                0x87, 0x3d, 0x9e, 0x92, 0xea, 0x33, 0xe8, 0x54,
-            },
-        },
-        .random_req[1] = {
-            .value = {
-                0x4c, 0xb7, 0xcc, 0x6d, 0x90, 0x9f, 0x1e, 0x2d,
-                0x9d, 0x1e, 0x52, 0xa7, 0xe0, 0x0c, 0x7b, 0xf7,
-            },
-        },
-        .random_rsp[1] = {
-            .value = {
-                0x5c, 0x32, 0x82, 0xc8, 0x76, 0x17, 0x3b, 0x18,
-                0x66, 0xda, 0xbf, 0xc3, 0x13, 0x49, 0x05, 0xfb,
-            },
-        },
-        .confirm_req[2] = {
-            .value = {
-                0x27, 0x61, 0x4d, 0x04, 0x64, 0xa9, 0x58, 0xf1,
-                0xe0, 0xf9, 0xe5, 0x78, 0x0b, 0x54, 0x89, 0x0a,
-            },
-        },
-        .confirm_rsp[2] = {
-            .value = {
-                0xe4, 0x8f, 0xdb, 0xc8, 0x35, 0xed, 0x4e, 0x7d,
-                0xbc, 0x92, 0x7f, 0x58, 0x02, 0xaa, 0xbf, 0x6b,
-            },
-        },
-        .random_req[2] = {
-            .value = {
-                0xfe, 0x85, 0x08, 0xe0, 0x35, 0x90, 0x13, 0xa9,
-                0xd3, 0xcf, 0xb6, 0x6d, 0x36, 0xaf, 0xbd, 0x59,
-            },
-        },
-        .random_rsp[2] = {
-            .value = {
-                0x47, 0x40, 0x8e, 0x97, 0xe3, 0xfe, 0x8f, 0x52,
-                0x29, 0x5e, 0x6b, 0x44, 0xdf, 0x0d, 0x60, 0xf4,
-            },
-        },
-        .confirm_req[3] = {
-            .value = {
-                0xac, 0xab, 0x13, 0x7c, 0x1a, 0x6e, 0x7a, 0xdb,
-                0xf6, 0xe8, 0x72, 0x9f, 0xc5, 0xc3, 0x99, 0x1b,
-            },
-        },
-        .confirm_rsp[3] = {
-            .value = {
-                0x79, 0xf2, 0xd1, 0x89, 0x5e, 0xa5, 0xa2, 0x90,
-                0xee, 0x25, 0x36, 0x81, 0x5a, 0x87, 0x20, 0x82,
-            },
-        },
-        .random_req[3] = {
-            .value = {
-                0xd4, 0x46, 0xa0, 0xc4, 0x3d, 0xae, 0x22, 0x06,
-                0xaf, 0x5d, 0x93, 0x96, 0xb7, 0x06, 0xc3, 0x61,
-            },
-        },
-        .random_rsp[3] = {
-            .value = {
-                0x5f, 0x81, 0x97, 0x8b, 0x52, 0x87, 0x1c, 0x67,
-                0xe0, 0x04, 0xcc, 0x50, 0xd9, 0x2b, 0x16, 0xb5,
-            },
-        },
-        .confirm_req[4] = {
-            .value = {
-                0x6c, 0x51, 0xc3, 0x61, 0x77, 0x7f, 0xf1, 0x05,
-                0x9e, 0x0f, 0xba, 0xfd, 0x32, 0x02, 0x09, 0x45,
-            },
-        },
-        .confirm_rsp[4] = {
-            .value = {
-                0x54, 0xe5, 0x24, 0x81, 0x62, 0x68, 0xe2, 0x45,
-                0x86, 0x2c, 0x11, 0x28, 0x15, 0xa8, 0x8e, 0x5b,
-            },
-        },
-        .random_req[4] = {
-            .value = {
-                0xbb, 0x29, 0x3a, 0xba, 0xe6, 0x4f, 0x06, 0xcf,
-                0xa3, 0x13, 0x27, 0xf2, 0xcb, 0xe4, 0xd2, 0xe6,
-            },
-        },
-        .random_rsp[4] = {
-            .value = {
-                0x50, 0xba, 0xd0, 0x0e, 0x26, 0xab, 0x04, 0xf8,
-                0xa2, 0x03, 0x1e, 0x63, 0x9a, 0xf7, 0x15, 0xdc,
-            },
-        },
-        .confirm_req[5] = {
-            .value = {
-                0x12, 0x3e, 0xfe, 0x5a, 0xb1, 0x09, 0x6f, 0x17,
-                0xb7, 0x77, 0x7e, 0x65, 0x88, 0xd4, 0x95, 0x56,
-            },
-        },
-        .confirm_rsp[5] = {
-            .value = {
-                0xc6, 0x9b, 0xac, 0xde, 0x7e, 0x03, 0x7a, 0xd3,
-                0xf1, 0xff, 0x3c, 0x4f, 0x4a, 0x85, 0xba, 0x73,
-            },
-        },
-        .random_req[5] = {
-            .value = {
-                0x17, 0xd5, 0x5e, 0x69, 0x30, 0x2c, 0x1f, 0x01,
-                0x87, 0x9c, 0xd6, 0xd2, 0xe4, 0x48, 0x8c, 0x84,
-            },
-        },
-        .random_rsp[5] = {
-            .value = {
-                0x9d, 0x54, 0x83, 0x4a, 0xcd, 0x93, 0x7c, 0x1e,
-                0x5b, 0xaf, 0xd2, 0x66, 0x8c, 0x2d, 0xaa, 0xc3,
-            },
-        },
-        .confirm_req[6] = {
-            .value = {
-                0xdc, 0x24, 0x69, 0xa8, 0xd3, 0xa9, 0x17, 0x11,
-                0x08, 0x37, 0x1a, 0x1e, 0x92, 0x03, 0xee, 0x36,
-            },
-        },
-        .confirm_rsp[6] = {
-            .value = {
-                0x98, 0xf8, 0x72, 0x71, 0x99, 0xa0, 0xbd, 0xcd,
-                0xb1, 0x97, 0x4c, 0x8a, 0xb8, 0xa8, 0x1a, 0x52,
-            },
-        },
-        .random_req[6] = {
-            .value = {
-                0xbf, 0xb1, 0x8e, 0xa5, 0x14, 0xe3, 0xeb, 0x9e,
-                0x29, 0x27, 0xe0, 0x19, 0xb1, 0xb2, 0x5c, 0xfe,
-            },
-        },
-        .random_rsp[6] = {
-            .value = {
-                0xae, 0x8a, 0x92, 0x78, 0x53, 0x7b, 0xdb, 0x8c,
-                0xec, 0x3a, 0x99, 0x2b, 0x94, 0xf1, 0x17, 0xfe,
-            },
-        },
-        .confirm_req[7] = {
-            .value = {
-                0xcf, 0xaf, 0x70, 0x73, 0x53, 0x65, 0x89, 0x57,
-                0x36, 0x98, 0xd2, 0x28, 0x86, 0x79, 0xfe, 0x85,
-            },
-        },
-        .confirm_rsp[7] = {
-            .value = {
-                0x0d, 0x2d, 0x77, 0x8a, 0x21, 0x11, 0xd9, 0x61,
-                0x9f, 0x80, 0x32, 0x8a, 0x32, 0x09, 0x42, 0x42,
-            },
-        },
-        .random_req[7] = {
-            .value = {
-                0x8b, 0xd2, 0x53, 0xcd, 0x96, 0xd1, 0x14, 0xb5,
-                0xea, 0x17, 0xb1, 0xa3, 0xa8, 0xfc, 0x3c, 0x2b,
-            },
-        },
-        .random_rsp[7] = {
-            .value = {
-                0xc2, 0x4f, 0x84, 0x60, 0x54, 0x79, 0x16, 0xed,
-                0x1a, 0x6e, 0x78, 0xa0, 0x99, 0x58, 0xf2, 0x94,
-            },
-        },
-        .confirm_req[8] = {
-            .value = {
-                0x9a, 0x4c, 0xbc, 0x9c, 0x55, 0x15, 0xa2, 0x4f,
-                0xa2, 0x5d, 0x3b, 0xa7, 0x43, 0xb3, 0x9c, 0x63,
-            },
-        },
-        .confirm_rsp[8] = {
-            .value = {
-                0xa3, 0xb1, 0x88, 0xa5, 0x70, 0xca, 0xa3, 0xa9,
-                0x67, 0x2a, 0xac, 0x99, 0x5e, 0x61, 0x68, 0xa0,
-            },
-        },
-        .random_req[8] = {
-            .value = {
-                0xcf, 0xcf, 0x5b, 0x94, 0xe0, 0xb2, 0x9d, 0x5a,
-                0x86, 0x71, 0x45, 0xce, 0xd9, 0xce, 0x13, 0xba,
-            },
-        },
-        .random_rsp[8] = {
-            .value = {
-                0x10, 0x96, 0x8a, 0x50, 0xa4, 0xd0, 0xaa, 0x5f,
-                0xd6, 0x32, 0xdb, 0x09, 0x7e, 0x22, 0x96, 0x42,
-            },
-        },
-        .confirm_req[9] = {
-            .value = {
-                0xf0, 0x90, 0x61, 0x25, 0x04, 0x29, 0x4f, 0xb6,
-                0x8b, 0xd5, 0x73, 0x49, 0xbd, 0xf7, 0x9b, 0xe7,
-            },
-        },
-        .confirm_rsp[9] = {
-            .value = {
-                0x5b, 0xe6, 0xb4, 0x3f, 0x1b, 0x77, 0x12, 0x75,
-                0x84, 0x94, 0xc6, 0x07, 0xfa, 0xa1, 0x41, 0x94,
-            },
-        },
-        .random_req[9] = {
-            .value = {
-                0x3d, 0x1a, 0xa3, 0x95, 0xec, 0x72, 0x84, 0xf4,
-                0xc5, 0xcd, 0xaa, 0x48, 0xe9, 0x0c, 0x0f, 0xe3,
-            },
-        },
-        .random_rsp[9] = {
-            .value = {
-                0x8a, 0x5a, 0x53, 0xfc, 0x07, 0x52, 0x01, 0xb9,
-                0xe9, 0x2d, 0xe7, 0x9d, 0x8c, 0x7c, 0xc7, 0xb3,
-            },
-        },
-        .confirm_req[10] = {
-            .value = {
-                0xe7, 0x8e, 0xc5, 0x08, 0x7f, 0x7e, 0xb8, 0xdc,
-                0x05, 0x88, 0x3a, 0x92, 0x5a, 0xf5, 0x9b, 0xa9,
-            },
-        },
-        .confirm_rsp[10] = {
-            .value = {
-                0xf7, 0xa2, 0xb6, 0xec, 0xcd, 0xef, 0xcb, 0xb7,
-                0x6f, 0xc3, 0xac, 0x17, 0xe2, 0xfd, 0xfa, 0x42,
-            },
-        },
-        .random_req[10] = {
-            .value = {
-                0x0d, 0xd1, 0xa2, 0x1d, 0xff, 0x74, 0xc5, 0x99,
-                0xe0, 0x67, 0x07, 0x99, 0x95, 0x75, 0x39, 0x76,
-            },
-        },
-        .random_rsp[10] = {
-            .value = {
-                0x2f, 0x13, 0xd1, 0x59, 0xfe, 0x20, 0x60, 0xf0,
-                0x02, 0x0c, 0xea, 0x79, 0xd7, 0x40, 0x86, 0x85,
-            },
-        },
-        .confirm_req[11] = {
-            .value = {
-                0x8b, 0x57, 0x87, 0xdd, 0xb1, 0xcc, 0x2d, 0x65,
-                0xc1, 0xba, 0xac, 0x88, 0x48, 0x23, 0xda, 0xe7,
-            },
-        },
-        .confirm_rsp[11] = {
-            .value = {
-                0xb3, 0xc4, 0x2e, 0xea, 0x33, 0xaf, 0x12, 0x9c,
-                0xb5, 0xab, 0xa1, 0x95, 0x30, 0xca, 0x46, 0x48,
-            },
-        },
-        .random_req[11] = {
-            .value = {
-                0x35, 0x57, 0xcd, 0xd5, 0xd2, 0xf8, 0xd7, 0xf2,
-                0x7b, 0xe3, 0xd7, 0xba, 0x31, 0xa5, 0xca, 0xfd,
-            },
-        },
-        .random_rsp[11] = {
-            .value = {
-                0xe2, 0x3b, 0x20, 0xbe, 0xec, 0xa5, 0x34, 0x3b,
-                0x76, 0x23, 0x53, 0x28, 0x36, 0xc4, 0x60, 0x13,
-            },
-        },
-        .confirm_req[12] = {
-            .value = {
-                0xc9, 0xfe, 0x03, 0x49, 0xe4, 0xff, 0x7e, 0xf7,
-                0x00, 0xd1, 0x2b, 0x13, 0xb1, 0x15, 0x6e, 0x92,
-            },
-        },
-        .confirm_rsp[12] = {
-            .value = {
-                0xbc, 0xa2, 0xf2, 0x03, 0x5c, 0xfd, 0x20, 0x7b,
-                0xd0, 0x1f, 0xd6, 0x50, 0xec, 0xc6, 0x7b, 0x31,
-            },
-        },
-        .random_req[12] = {
-            .value = {
-                0x04, 0x50, 0xea, 0xb8, 0xca, 0x36, 0x1a, 0x61,
-                0x92, 0xed, 0xa0, 0x67, 0x78, 0x15, 0x10, 0xb5,
-            },
-        },
-        .random_rsp[12] = {
-            .value = {
-                0x0c, 0x8e, 0x9d, 0x7b, 0x9d, 0x7e, 0xda, 0x23,
-                0xbb, 0x61, 0xd9, 0xff, 0x46, 0x77, 0x33, 0x1b,
-            },
-        },
-        .confirm_req[13] = {
-            .value = {
-                0x9a, 0xff, 0xd6, 0xe5, 0x1a, 0xc3, 0xd3, 0x37,
-                0x34, 0xeb, 0x3e, 0x3a, 0x8e, 0x0b, 0x86, 0xb4,
-            },
-        },
-        .confirm_rsp[13] = {
-            .value = {
-                0xf6, 0x32, 0x19, 0xb4, 0x08, 0x6b, 0x8a, 0x0f,
-                0xc9, 0x9c, 0x1b, 0x68, 0xb8, 0xa0, 0xd0, 0xc9,
-            },
-        },
-        .random_req[13] = {
-            .value = {
-                0x86, 0xeb, 0x5c, 0xf9, 0x33, 0x54, 0x7d, 0xe4,
-                0xa4, 0xe2, 0xe1, 0xf6, 0x6b, 0xea, 0x34, 0xed,
-            },
-        },
-        .random_rsp[13] = {
-            .value = {
-                0xad, 0x53, 0xa0, 0x6e, 0xde, 0x1d, 0xda, 0x99,
-                0x31, 0x45, 0xe5, 0x3a, 0x73, 0xa1, 0x5e, 0xe1,
-            },
-        },
-        .confirm_req[14] = {
-            .value = {
-                0x93, 0xd4, 0xe0, 0xaa, 0x0c, 0x91, 0xba, 0xde,
-                0xc9, 0x5c, 0x68, 0xb0, 0xce, 0xb6, 0x84, 0xcd,
-            },
-        },
-        .confirm_rsp[14] = {
-            .value = {
-                0x85, 0xc7, 0x05, 0x02, 0x21, 0x9d, 0x4c, 0x4c,
-                0x16, 0xf7, 0x8f, 0x7b, 0xaa, 0xb4, 0x8f, 0x37,
-            },
-        },
-        .random_req[14] = {
-            .value = {
-                0x84, 0xfd, 0xf1, 0x39, 0x1a, 0x9a, 0xa5, 0xb8,
-                0x49, 0xc0, 0x66, 0xdc, 0x33, 0x71, 0x32, 0x87,
-            },
-        },
-        .random_rsp[14] = {
-            .value = {
-                0x5d, 0xaf, 0x38, 0xcd, 0xb5, 0x83, 0xaa, 0xa0,
-                0xab, 0x30, 0x82, 0xed, 0x6f, 0xd2, 0x75, 0xe7,
-            },
-        },
-        .confirm_req[15] = {
-            .value = {
-                0x88, 0x12, 0xe8, 0x89, 0xd4, 0x52, 0x6d, 0xac,
-                0x61, 0x2a, 0x85, 0x85, 0x1e, 0x9c, 0x82, 0x21,
-            },
-        },
-        .confirm_rsp[15] = {
-            .value = {
-                0xc1, 0xe9, 0xcd, 0x21, 0x29, 0x6a, 0x78, 0xe4,
-                0x7b, 0x7d, 0x73, 0x25, 0x9e, 0x9b, 0x95, 0x8b,
-            },
-        },
-        .random_req[15] = {
-            .value = {
-                0x95, 0x87, 0x9d, 0x5a, 0x10, 0x14, 0xa0, 0xdf,
-                0x5e, 0x02, 0x22, 0x39, 0x23, 0xc9, 0xbc, 0xba,
-            },
-        },
-        .random_rsp[15] = {
-            .value = {
-                0x1b, 0x91, 0xe2, 0xdf, 0xca, 0xfe, 0x2b, 0x61,
-                0x33, 0x8c, 0x83, 0xbf, 0xcf, 0xc3, 0x72, 0xcc,
-            },
-        },
-        .confirm_req[16] = {
-            .value = {
-                0xce, 0xc9, 0x68, 0xf7, 0xea, 0x41, 0x18, 0x5c,
-                0x16, 0x6a, 0x98, 0x13, 0x0c, 0x10, 0xc2, 0xa3,
-            },
-        },
-        .confirm_rsp[16] = {
-            .value = {
-                0x97, 0x73, 0xc9, 0x72, 0x68, 0x99, 0x63, 0xed,
-                0x81, 0x3b, 0x5c, 0xee, 0x37, 0xfc, 0xca, 0xae,
-            },
-        },
-        .random_req[16] = {
-            .value = {
-                0x5b, 0x85, 0xb0, 0x1b, 0xc3, 0xde, 0x18, 0xba,
-                0xc1, 0xc7, 0x89, 0x99, 0xfe, 0xcd, 0xdb, 0x6a,
-            },
-        },
-        .random_rsp[16] = {
-            .value = {
-                0x5e, 0x1a, 0xcb, 0xbc, 0xda, 0x41, 0x06, 0x5a,
-                0x14, 0x34, 0x3a, 0xb1, 0xa1, 0x6f, 0xb2, 0xd8,
-            },
-        },
-        .confirm_req[17] = {
-            .value = {
-                0x1d, 0x59, 0x8a, 0xb0, 0x19, 0xe5, 0xff, 0x45,
-                0xb6, 0xc3, 0x33, 0x64, 0xd1, 0x6e, 0xee, 0xdd,
-            },
-        },
-        .confirm_rsp[17] = {
-            .value = {
-                0x4c, 0x9b, 0xe8, 0x68, 0x52, 0x34, 0xef, 0xe1,
-                0x84, 0xbd, 0x37, 0x85, 0x53, 0x0d, 0xd5, 0xc1,
-            },
-        },
-        .random_req[17] = {
-            .value = {
-                0xa6, 0xf7, 0x97, 0x18, 0x9a, 0x3e, 0x9d, 0xcf,
-                0x91, 0xa3, 0xa3, 0x8e, 0xda, 0x8f, 0x8f, 0x90,
-            },
-        },
-        .random_rsp[17] = {
-            .value = {
-                0x94, 0x10, 0x19, 0x17, 0x8d, 0x0a, 0x72, 0xfd,
-                0x24, 0x9d, 0xfd, 0x37, 0x4e, 0xdf, 0x4c, 0x30,
-            },
-        },
-        .confirm_req[18] = {
-            .value = {
-                0xfc, 0x64, 0x8a, 0x8b, 0x37, 0x17, 0x90, 0x6d,
-                0x25, 0x0e, 0xc6, 0x18, 0xc9, 0xc9, 0xc2, 0x2a,
-            },
-        },
-        .confirm_rsp[18] = {
-            .value = {
-                0x50, 0x98, 0x86, 0xf5, 0xc0, 0xda, 0x45, 0x2d,
-                0xea, 0xc8, 0x9d, 0x28, 0x04, 0xd8, 0x73, 0x6f,
-            },
-        },
-        .random_req[18] = {
-            .value = {
-                0x13, 0x10, 0x38, 0xe8, 0x17, 0x6d, 0x72, 0xd5,
-                0x94, 0xaf, 0xed, 0x4f, 0x23, 0xa0, 0x41, 0xfc,
-            },
-        },
-        .random_rsp[18] = {
-            .value = {
-                0xdf, 0xed, 0xf7, 0x08, 0xce, 0x64, 0xbc, 0x11,
-                0x41, 0x7a, 0xd9, 0xf7, 0x4a, 0xd9, 0x4a, 0x15,
-            },
-        },
-        .confirm_req[19] = {
-            .value = {
-                0xae, 0x24, 0x8f, 0xdf, 0xb0, 0x57, 0xc4, 0x9c,
-                0xe6, 0xae, 0x9b, 0xc2, 0x4d, 0x3d, 0x1c, 0xcb,
-            },
-        },
-        .confirm_rsp[19] = {
-            .value = {
-                0xcc, 0x5c, 0xa3, 0xbe, 0xd7, 0x83, 0xee, 0x60,
-                0x80, 0xff, 0x5f, 0x1a, 0x07, 0xbf, 0x4c, 0x33,
-            },
-        },
-        .random_req[19] = {
-            .value = {
-                0x93, 0xc3, 0x62, 0x06, 0xcb, 0xe5, 0xb0, 0x01,
-                0x02, 0x18, 0xa2, 0x50, 0x4c, 0x73, 0xa2, 0x27,
-            },
-        },
-        .random_rsp[19] = {
-            .value = {
-                0x11, 0x2a, 0xd3, 0x06, 0x28, 0x9c, 0xdf, 0x73,
-                0xa5, 0xa4, 0xe5, 0x1e, 0x07, 0xcf, 0xee, 0x71,
-            },
-        },
-        .dhkey_check_req = {
-            .value = {
-                0x73, 0xa0, 0x40, 0x58, 0x78, 0x20, 0x5f, 0x2c,
-                0xf4, 0x19, 0x23, 0xa8, 0x74, 0xbd, 0xc2, 0x3e,
-            },
-        },
-        .dhkey_check_rsp = {
-            .value = {
-                0x5a, 0x30, 0xbc, 0xce, 0xec, 0xdf, 0xf0, 0x32,
-                0x3c, 0x18, 0xa3, 0xd3, 0x3f, 0x20, 0x87, 0x10,
-            },
-        },
-        .sign_info_req = {
-            .sig_key = {
-                0x2e, 0x81, 0x09, 0xde, 0x32, 0xc5, 0x28, 0x34,
-                0xe1, 0x45, 0x4a, 0x35, 0x49, 0xef, 0xa2, 0xed,
-            },
-        },
-        .id_info_rsp = {
-            .irk = {
-                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
-                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
-            },
-        },
-        .id_addr_info_rsp = {
-            .addr_type = 0,
-            .bd_addr = {
-                0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
-            },
-        },
-        .sign_info_rsp = {
-            .sig_key = {
-                0x90, 0x3d, 0x26, 0x65, 0xc1, 0xd1, 0x5a, 0x9d,
-                0xda, 0xab, 0x0d, 0x00, 0x05, 0x0e, 0x6c, 0x5d,
-            },
-        },
-        .ltk = {
-            0xf1, 0x41, 0x1a, 0x5b, 0x60, 0xc1, 0x43, 0xc6,
-            0x80, 0x34, 0x5e, 0x7f, 0xd8, 0x0c, 0x75, 0xdc,
-        },
-        .pair_alg = BLE_SM_PAIR_ALG_PASSKEY,
-        .authenticated = 1,
-        .passkey_info = {
-            .passkey = {
-                .action = BLE_SM_IOACT_INPUT,
-                .passkey = 516645,
-            },
-        },
-    };
-    ble_sm_test_util_us_sc_good(&params);
-}
-
-/**
- * Secure connections pairing
- * Master: us
- * Pair algorithm: passkey entry
- * Initiator IO capabilities: 0
- * Responder IO capabilities: 4
- * Bonding: true
- * Initiator address type: 0
- * Responder address type: 0
- * Initiator key distribution: 7
- * Responder key distribution: 5
- */
-TEST_CASE(ble_sm_sc_us_pk_iio0_rio4_b1_iat0_rat0_ik7_rk5)
-{
-    struct ble_sm_test_params params;
-
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {
-            0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
-        },
-        .resp_id_addr = {
-            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
-        },
-        .pair_req = {
-            .io_cap = 0x00,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_rsp = {
-            .io_cap = 0x04,
-            .oob_data_flag = 0x00,
-            .authreq = 0x0d,
-            .max_enc_key_size = 0x10,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x05,
-        },
-        .our_priv_key = {
-            0xb1, 0x6b, 0x4f, 0x81, 0xbc, 0xe3, 0x60, 0x9e,
-            0x00, 0x20, 0xf1, 0x73, 0x3e, 0xfb, 0xcc, 0x6e,
-            0x8c, 0xb6, 0xd2, 0x51, 0xd9, 0x36, 0x8a, 0x6d,
-            0xca, 0x8c, 0xd7, 0xbe, 0x96, 0x03, 0xdf, 0xd6,
-        },
-        .public_key_req = {
-            .x = {
-                0xe5, 0x0f, 0x02, 0x0a, 0x37, 0x90, 0x94, 0x5a,
-                0x06, 0x21, 0xf7, 0xbc, 0xd5, 0xbe, 0xb9, 0x24,
-                0x8a, 0x35, 0xfd, 0xf8, 0x5e, 0xe2, 0x70, 0xd5,
-                0x5a, 0xe8, 0xe7, 0xdd, 0x13, 0x90, 0xeb, 0xd4,
-            },
-            .y = {
-                0x41, 0xc8, 0x51, 0x1a, 0x25, 0x44, 0x01, 0x53,
-                0x42, 0x74, 0x07, 0x9c, 0x18, 0xe6, 0x3b, 0x8a,
-                0xce, 0x7a, 0x37, 0x1f, 0x18, 0x5c, 0x02, 0x7c,
-                0x67, 0x16, 0xf5, 0x30, 0x2b, 0x31, 0xa9, 0xc7,
-            },
-        },
-        .public_key_rsp = {
-            .x = {
-                0x03, 0x0d, 0x13, 0x55, 0xd9, 0xee, 0x3f, 0xac,
-                0x8e, 0x8a, 0xa6, 0x2a, 0xcb, 0x60, 0x35, 0xb9,
-                0xb2, 0x4d, 0x63, 0x91, 0x5e, 0xa1, 0xdd, 0xdf,
-                0x60, 0xdc, 0x6e, 0x09, 0xb9, 0x9e, 0xf1, 0x4d,
-            },
-            .y = {
-                0xa8, 0x09, 0x31, 0x1e, 0x39, 0x96, 0x74, 0x41,
-                0xea, 0x19, 0x4f, 0x24, 0x36, 0x57, 0x7c, 0x9f,
-                0x21, 0xa3, 0xad, 0xa1, 0x3d, 0xe2, 0x1c, 0x6a,
-                0xd6, 0xc9, 0xdb, 0xff, 0xce, 0x0a, 0x94, 0x12,
-            },
-        },
-        .confirm_req[0] = {
-            .value = {
-                0x3b, 0x3d, 0xb2, 0x2f, 0x72, 0x0f, 0x93, 0x19,
-                0x95, 0xdb, 0x88, 0xdf, 0x5d, 0x58, 0x95, 0x37,
-            },
-        },
-        .confirm_rsp[0] = {
-            .value = {
-                0x98, 0xab, 0x20, 0x8d, 0x51, 0x3b, 0x6c, 0x29,
-                0x2d, 0x73, 0x15, 0xf6, 0x6d, 0x6d, 0xb9, 0xb3,
-            },
-        },
-        .random_req[0] = {
-            .value = {
-                0xc1, 0xdf, 0x20, 0x3d, 0x7b, 0xcb, 0x5f, 0xe2,
-                0x9a, 0x23, 0x9c, 0xba, 0x2f, 0x42, 0x3c, 0xb8,
-            },
-        },
-        .random_rsp[0] = {
-            .value = {
-                0x7d, 0x82, 0xb4, 0xae, 0x41, 0xdb, 0x67, 0x8f,
-                0x54, 0x01, 0x21, 0x64, 0x31, 0xd4, 0xfc, 0xb5,
-            },
-        },
-        .confirm_req[1] = {
-            .value = {
-                0xc3, 0xa5, 0xd0, 0xdd, 0xd5, 0xec, 0x1d, 0xc3,
-                0x14, 0x95, 0x79, 0xb2, 0x61, 0x4d, 0x4f, 0x36,
-            },
-        },
-        .confirm_rsp[1] = {
-            .value = {
-                0xe2, 0x20, 0xf4, 0x4d, 0xa1, 0x9c, 0x83, 0x51,
-                0x18, 0xf9, 0x35, 0x2a, 0x51, 0x50, 0xdf, 0xe7,
-            },
-        },
-        .random_req[1] = {
-            .value = {
-                0x71, 0xcb, 0x01, 0xb4, 0x83, 0xdc, 0xd8, 0x54,
-                0x0f, 0xe5, 0xd5, 0x6b, 0x6a, 0x0d, 0x98, 0xb6,
-            },
-        },
-        .random_rsp[1] = {
-            .value = {
-                0x30, 0xbf, 0xd3, 0xfd, 0xf4, 0xc2, 0xa1, 0xd0,
-                0xba, 0x4b, 0x27, 0x7c, 0x29, 0x98, 0x54, 0xa2,
-            },
-        },
-        .confirm_req[2] = {
-            .value = {
-                0xf0, 0x92, 0xc4, 0xda, 0x8a, 0x17, 0x7c, 0xc6,
-                0x14, 0x05, 0x7d, 0xbb, 0xfc, 0x7c, 0xcd, 0x0a,
-            },
-        },
-        .confirm_rsp[2] = {
-            .value = {
-                0xf3, 0x89, 0xca, 0xe0, 0xfb, 0xbe, 0x8c, 0xc3,
-                0x4c, 0x6c, 0x6e, 0x11, 0x36, 0x4e, 0xaa, 0x25,
-            },
-        },
-        .random_req[2] = {
-            .value = {
-                0x78, 0x5a, 0xf0, 0x1e, 0x2a, 0x0d, 0x16, 0xb3,
-                0x03, 0x4b, 0x4b, 0x68, 0x17, 0xe0, 0xf0, 0x82,
-            },
-        },
-        .random_rsp[2] = {
-            .value = {
-                0xbf, 0x96, 0xdd, 0xf5, 0x30, 0x2a, 0xe9, 0x8c,
-                0xb9, 0x13, 0xc5, 0xb7, 0x15, 0x1f, 0xa3, 0x9b,
-            },
-        },
-        .confirm_req[3] = {
-            .value = {
-                0x36, 0xe4, 0x4c, 0x00, 0xe7, 0x0d, 0xee, 0xe4,
-                0x95, 0xb8, 0x6a, 0xf9, 0xf7, 0x24, 0xef, 0xea,
-            },
-        },
-        .confirm_rsp[3] = {
-            .value = {
-                0x2e, 0x90, 0x87, 0x85, 0xb8, 0x29, 0x93, 0x9e,
-                0x38, 0xa6, 0xdb, 0x17, 0xb2, 0xa8, 0x32, 0x65,
-            },
-        },
-        .random_req[3] = {
-            .value = {
-                0x0a, 0xee, 0x93, 0xf6, 0x56, 0x35, 0x8e, 0xed,
-                0x3f, 0x45, 0xa5, 0x01, 0x59, 0xeb, 0xea, 0xa8,
-            },
-        },
-        .random_rsp[3] = {
-            .value = {
-                0x38, 0xd0, 0xf8, 0x11, 0x5e, 0x47, 0x72, 0x66,
-                0xce, 0x56, 0x9c, 0x81, 0x5f, 0x52, 0xd4, 0x9a,
-            },
-        },
-        .confirm_req[4] = {
-            .value = {
-                0x2c, 0x98, 0x9b, 0x71, 0xe4, 0xde, 0x6d, 0x20,
-                0x84, 0x30, 0xab, 0x7a, 0xfc, 0x43, 0x82, 0xc6,
-            },
-        },
-        .confirm_rsp[4] = {
-            .value = {
-                0x76, 0xfe, 0x1f, 0x78, 0xaa, 0x42, 0xd5, 0xc6,
-                0x9f, 0xe4, 0xa7, 0xc7, 0xb8, 0xd2, 0x1e, 0x59,
-            },
-        },
-        .random_req[4] = {
-            .value = {
-                0x61, 0x5e, 0x47, 0xb1, 0x77, 0x6f, 0x04, 0xee,
-                0x94, 0xc4, 0x6c, 0xa9, 0xf5, 0xf8, 0x11, 0x6e,
-            },
-        },
-        .random_rsp[4] = {
-            .value = {
-                0xa5, 0xad, 0x98, 0x65, 0x28, 0xfc, 0x6b, 0x02,
-                0x6d, 0x9a, 0x29, 0x61, 0x1c, 0x02, 0x0a, 0x6b,
-            },
-        },
-        .confirm_req[5] = {
-            .value = {
-                0x4d, 0x55, 0x3e, 0x1f, 0x87, 0x12, 0xc7, 0x6c,
-                0xd7, 0x9a, 0xa6, 0xf1, 0x6e, 0x48, 0xd3, 0x7d,
-            },
-        },
-        .confirm_rsp[5] = {
-            .value = {
-                0xff, 0x77, 0x6e, 0xba, 0x1f, 0xb7, 0xbd, 0x0b,
-                0x3f, 0xce, 0xd5, 0x39, 0x81, 0x17, 0x51, 0xfc,
-            },
-        },
-        .random_req[5] = {
-            .value = {
-                0xf9, 0x2c, 0x77, 0x41, 0xcd, 0x9a, 0x10, 0x99,
-                0xc6, 0x70, 0x5a, 0xc8, 0x24, 0x26, 0xb2, 0xc8,
-            },
-        },
-        .random_rsp[5] = {
-            .value = {
-                0xa0, 0x44, 0x0a, 0x8b, 0xda, 0x6a, 0x74, 0x90,
-                0x5f, 0x89, 0x44, 0xa5, 0x9a, 0x58, 0xd5, 0x08,
-            },
-        },
-        .confirm_req[6] = {
-            .value = {
-                0x7e, 0x6e, 0x89, 0xc8, 0xbe, 0xde, 0x1c, 0xc3,
-                0x45, 0xb6, 0x4c, 0x83, 0x71, 0xe2, 0xd6, 0xda,
-            },
-        },
-        .confirm_rsp[6] = {
-            .value = {
-                0x4a, 0x29, 0x7b, 0x88, 0x97, 0xc1, 0x60, 0x85,
-                0x32, 0x7d, 0xf1, 0xaa, 0x04, 0x13, 0x89, 0x11,
-            },
-        },
-        .random_req[6] = {
-            .value = {
-                0x2a, 0xaf, 0x7d, 0x21, 0x4e, 0x14, 0xf5, 0x7e,
-                0xcc, 0x39, 0xf7, 0x56, 0x45, 0x87, 0x23, 0x64,
-            },
-        },
-        .random_rsp[6] = {
-            .value = {
-                0x74, 0xd2, 0xff, 0xf0, 0x19, 0xf7, 0x87, 0xe7,
-                0x0d, 0x65, 0x27, 0x61, 0xea, 0x9e, 0x05, 0x3d,
-            },
-        },
-        .confirm_req[7] = {
-            .value = {
-                0x4f, 0x77, 0x22, 0x08, 0x58, 0xed, 0x8c, 0x60,
-                0xbf, 0xbc, 0x78, 0x0c, 0x80, 0xc9, 0xb7, 0x60,
-            },
-        },
-        .confirm_rsp[7] = {
-            .value = {
-                0xd2, 0x47, 0xfd, 0xea, 0xa3, 0x32, 0x53, 0xc1,
-                0x06, 0xcd, 0x64, 0xeb, 0x88, 0x64, 0x0e, 0xe5,
-            },
-        },
-        .random_req[7] = {
-            .value = {
-                0xc8, 0xd0, 0x45, 0xa8, 0x29, 0xdb, 0x5a, 0x42,
-                0xfe, 0x68, 0xa8, 0x7a, 0x0a, 0x13, 0x22, 0xa4,
-            },
-        },
-        .random_rsp[7] = {
-            .value = {
-                0x78, 0x14, 0x46, 0xe2, 0x47, 0x0e, 0xd4, 0xb4,
-                0xde, 0x35, 0x4a, 0x82, 0x4b, 0x32, 0x9b, 0x46,
-            },
-        },
-        .confirm_req[8] = {
-            .value = {
-                0x24, 0x96, 0xe5, 0x50, 0xfa, 

<TRUNCATED>


[26/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/drivers/nrf52/src/ble_phy.c
----------------------------------------------------------------------
diff --git a/net/nimble/drivers/nrf52/src/ble_phy.c b/net/nimble/drivers/nrf52/src/ble_phy.c
index 7389b20..46bb39a 100644
--- a/net/nimble/drivers/nrf52/src/ble_phy.c
+++ b/net/nimble/drivers/nrf52/src/ble_phy.c
@@ -20,6 +20,7 @@
 #include <stdint.h>
 #include <string.h>
 #include <assert.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "ble/xcvr.h"
 #include "bsp/cmsis_nvic.h"
@@ -89,7 +90,7 @@ struct ble_phy_obj g_ble_phy_data;
 /* Global transmit/receive buffer */
 static uint32_t g_ble_phy_txrx_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /* Make sure word-aligned for faster copies */
 static uint32_t g_ble_phy_enc_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
 #endif
@@ -159,7 +160,7 @@ STATS_NAME_END(ble_phy_stats)
  *  bit in the NVIC just to be sure when we disable the PHY.
  */
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 
 /*
  * Per nordic, the number of bytes needed for scratch is 16 + MAX_PKT_SIZE.
@@ -168,7 +169,7 @@ STATS_NAME_END(ble_phy_stats)
  * space for 267 bytes of scratch. I used 268 bytes since not sure if this
  * needs to be aligned and burning a byte is no big deal.
  */
-//#define NRF_ENC_SCRATCH_WORDS (((NIMBLE_OPT_LL_MAX_PKT_SIZE + 16) + 3) / 4)
+//#define NRF_ENC_SCRATCH_WORDS (((MYNEWT_VAL(BLE_LL_MAX_PKT_SIZE) + 16) + 3) / 4)
 #define NRF_ENC_SCRATCH_WORDS   (67)
 
 uint32_t g_nrf_encrypt_scratchpad[NRF_ENC_SCRATCH_WORDS];
@@ -243,7 +244,7 @@ nrf_wait_disabled(void)
 static void
 ble_phy_rx_xcvr_setup(void)
 {
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     if (g_ble_phy_data.phy_encrypted) {
         NRF_RADIO->PACKETPTR = (uint32_t)&g_ble_phy_enc_buf[0];
         NRF_CCM->INPTR = (uint32_t)&g_ble_phy_enc_buf[0];
@@ -262,7 +263,7 @@ ble_phy_rx_xcvr_setup(void)
     NRF_RADIO->PACKETPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
 #endif
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     if (g_ble_phy_data.phy_privacy) {
         NRF_AAR->ENABLE = AAR_ENABLE_ENABLE_Enabled;
         NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
@@ -325,7 +326,7 @@ ble_phy_tx_end_isr(void)
     NRF_RADIO->EVENTS_END = 0;
     wfr_time = NRF_RADIO->SHORTS;
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     /*
      * XXX: not sure what to do. We had a HW error during transmission.
      * For now I just count a stat but continue on like all is good.
@@ -403,7 +404,7 @@ ble_phy_rx_end_isr(void)
     } else {
         STATS_INC(ble_phy_stats, rx_valid);
         ble_hdr->rxinfo.flags |= BLE_MBUF_HDR_F_CRC_OK;
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
         if (g_ble_phy_data.phy_encrypted) {
             /* Only set MIC failure flag if frame is not zero length */
             if ((dptr[1] != 0) && (NRF_CCM->MICSTATUS == 0)) {
@@ -499,7 +500,7 @@ ble_phy_rx_start_isr(void)
         g_ble_phy_data.phy_rx_started = 1;
         NRF_RADIO->INTENSET = RADIO_INTENSET_END_Msk;
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
         /* Must start aar if we need to  */
         if (g_ble_phy_data.phy_privacy) {
             NRF_RADIO->EVENTS_BCMATCH = 0;
@@ -611,14 +612,14 @@ ble_phy_init(void)
     /* Captures tx/rx start in timer0 capture 1 */
     NRF_PPI->CHENSET = PPI_CHEN_CH26_Msk;
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     NRF_CCM->INTENCLR = 0xffffffff;
     NRF_CCM->SHORTS = CCM_SHORTS_ENDKSGEN_CRYPT_Msk;
     NRF_CCM->EVENTS_ERROR = 0;
     memset(g_nrf_encrypt_scratchpad, 0, sizeof(g_nrf_encrypt_scratchpad));
 #endif
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     g_ble_phy_data.phy_aar_scratch = 0;
     NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
     NRF_AAR->INTENCLR = 0xffffffff;
@@ -689,7 +690,7 @@ ble_phy_rx(void)
     return 0;
 }
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 /**
  * Called to enable encryption at the PHY. Note that this state will persist
  * in the PHY; in other words, if you call this function you have to call
@@ -826,7 +827,7 @@ ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
     ble_hdr = BLE_MBUF_HDR_PTR(txpdu);
     payload_len = ble_hdr->txinfo.pyld_len;
 
-#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
     if (g_ble_phy_data.phy_encrypted) {
         dptr = (uint8_t *)&g_ble_phy_enc_buf[0];
         NRF_CCM->SHORTS = 1;
@@ -839,14 +840,14 @@ ble_phy_tx(struct os_mbuf *txpdu, uint8_t end_trans)
         NRF_PPI->CHENCLR = PPI_CHEN_CH25_Msk | PPI_CHEN_CH23_Msk;
         NRF_PPI->CHENSET = PPI_CHEN_CH24_Msk;
     } else {
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
         NRF_PPI->CHENCLR = PPI_CHEN_CH23_Msk;
         NRF_AAR->IRKPTR = (uint32_t)&g_nrf_irk_list[0];
 #endif
         dptr = (uint8_t *)&g_ble_phy_txrx_buf[0];
     }
 #else
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
     NRF_PPI->CHENCLR = PPI_CHEN_CH23_Msk;
 #endif
     dptr = (uint8_t *)&g_ble_phy_txrx_buf[0];
@@ -1106,7 +1107,7 @@ ble_phy_max_data_pdu_pyld(void)
     return BLE_LL_DATA_PDU_MAX_PYLD;
 }
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 void
 ble_phy_resolv_list_enable(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/include/host/ble_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gatt.h b/net/nimble/host/include/host/ble_gatt.h
index fbc32a2..c5bc769 100644
--- a/net/nimble/host/include/host/ble_gatt.h
+++ b/net/nimble/host/include/host/ble_gatt.h
@@ -436,8 +436,7 @@ int ble_gatts_register_svcs(const struct ble_gatt_svc_def *svcs,
 int ble_gatts_add_svcs(const struct ble_gatt_svc_def *svcs);
 int ble_gatts_count_resources(const struct ble_gatt_svc_def *svcs,
                               struct ble_gatt_resources *res);
-int ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs,
-                        struct ble_hs_cfg *cfg);
+int ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs);
 
 void ble_gatts_chr_updated(uint16_t chr_def_handle);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/include/host/ble_hs.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_hs.h b/net/nimble/host/include/host/ble_hs.h
index 954d278..7b520a5 100644
--- a/net/nimble/host/include/host/ble_hs.h
+++ b/net/nimble/host/include/host/ble_hs.h
@@ -94,58 +94,10 @@ typedef void ble_hs_reset_fn(int reason);
 typedef void ble_hs_sync_fn(void);
 
 struct ble_hs_cfg {
-    /**
-     * An HCI buffer is a "flat" 260-byte buffer.  HCI buffers are used by the
-     * controller to send unsolicited events to the host.
-     *
-     * HCI buffers can get tied up when the controller sends lots of
-     * asynchronous / unsolicited events (i.e., non-acks).  When the controller
-     * needs to send one of these events, it allocates an HCI buffer, fills it
-     * with the event payload, and puts it on a host queue.  If the controller
-     * sends a quick burst of these events, the buffer pool may be exhausted,
-     * preventing the host from sending an HCI command to the controller.
-     *
-     * Every time the controller sends a non-ack HCI event to the host, it also
-     * allocates an OS event (it is unfortunate that these are both called
-     * "events").  The OS event is put on the host-parent-task's event queue;
-     * it is what wakes up the host-parent-task and indicates that an HCI event
-     * needs to be processsed.  The pool of OS events is allocated with the
-     * same number of elements as the HCI buffer pool.
-     */
-    /* XXX: This should either be renamed to indicate it is only used for OS
-     * events, or it should go away entirely (copy the number from the
-     * transport's config).
-     */
-    uint8_t max_hci_bufs;
-
-    /*** Connection settings. */
-    /**
-     * The maximum number of concurrent connections.  This is set
-     * automatically according to the build-time option
-     * NIMBLE_OPT_MAX_CONNECTIONS.
-     */
-    uint8_t max_connections;
+    struct os_eventq *parent_evq;
 
     /*** GATT server settings. */
     /**
-     * These are acquired at service registration time and never freed.  You
-     * need one of these for every service that you register.
-     */
-    uint16_t max_services;
-
-    /**
-     * The total number of in-RAM client characteristic configuration
-     * descriptors (CCCDs).  One of these is consumed each time a peer
-     * subscribes to notifications or indications for a characteristic that
-     * your device serves.  In addition, at service registration time, the host
-     * uses one of these for each characteristic that supports notifications or
-     * indications.  So, the formula which guarantees no resource exhaustion
-     * is:
-     *     (num-subscribable-characteristics) * (max-connections + 1)
-     */
-    uint16_t max_client_configs;
-
-    /**
      * An optional callback that gets executed upon registration of each GATT
      * resource (service, characteristic, or descriptor).
      */
@@ -157,62 +109,10 @@ struct ble_hs_cfg {
      */
     void *gatts_register_arg;
 
-    /*** GATT client settings. */
-    /**
-     * The maximum number of concurrent GATT client procedures.  When you
-     * initiate a GATT procedure (e.g., read a characteristic, discover
-     * services, etc.), one of these is consumed.  The resource is freed when
-     * the procedure completes.
-     */
-    uint8_t max_gattc_procs;
-
-    /*** ATT server settings. */
-    /**
-     * The total number of local ATT attributes.  Attributes are consumed at
-     * service registration time and are never freed.  Attributes are used by
-     * GATT server entities: services, characteristics, and descriptors
-     * according to the following formula:
-     *     (num-services + (num-characteristics * 2) + num-descriptors)
-     *
-     * Every characteristic that supports indications or notifications
-     * automatically gets a descriptor.  All other descriptors are specified by
-     * the application at service registration time.
-     */
-    uint16_t max_attrs;
-
-    /**
-     * A GATT server uses these when a peer performs a "write long
-     * characteristic values" or "write long characteristic descriptors"
-     * procedure.  One of these resources is consumed each time a peer sends a
-     * partial write.  These procedures are not used often.
-     */
-    uint8_t max_prep_entries;
-
-    /*** L2CAP settings. */
-    /**
-     * Each connection requires three L2CAP channels (signal, ATT, and security
-     * manager).  In addition, the nimble host may allow channels to be created
-     * "on the fly" (connection-oriented channels).  This functionality is not
-     * available at the moment, so a safe formula to use is:
-     *     (max-connections * 3)
-     */
-    uint8_t max_l2cap_chans;
-
-    /**
-     * The maximum number of concurrent L2CAP signalling procedures.  Only one
-     * L2CAP signalling procedure is supported: slave-initiated connection
-     * update.  You will never need more of these than the max number of
-     * connections.
-     */
-    uint8_t max_l2cap_sig_procs;
-
-    /**
-     * The maximum number of concurrent security manager procedures.  Security
-     * manager procedures include pairing and restoration of a bonded link.
+    /***
+     * Security manager settings.  The only reason these are configurable at
+     * runtime is to simplify security testing.
      */
-    uint8_t max_l2cap_sm_procs;
-
-    /*** Security manager settings. */
     uint8_t sm_io_cap;
     unsigned sm_oob_data_flag:1;
     unsigned sm_bonding:1;
@@ -239,23 +139,18 @@ struct ble_hs_cfg {
     /**
      * These function callbacks handle persistence of sercurity material
      * (bonding).
+     * XXX: These need to go away.  Instead, the nimble host package should
+     * require the host-store API (not yet implemented)..
      */
     ble_store_read_fn *store_read_cb;
     ble_store_write_fn *store_write_cb;
     ble_store_delete_fn *store_delete_cb;
-
-    /*** Privacy settings. */
-    /**
-     * The frequency at which new resovlable private addresses are generated.
-     * Units are seconds.
-     */
-    uint16_t rpa_timeout;
 };
 
-extern const struct ble_hs_cfg ble_hs_cfg_dflt;
+extern struct ble_hs_cfg ble_hs_cfg;
 
 int ble_hs_synced(void);
 int ble_hs_start(void);
-int ble_hs_init(struct os_eventq *app_evq, struct ble_hs_cfg *cfg);
+void ble_hs_init(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/include/host/ble_sm.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_sm.h b/net/nimble/host/include/host/ble_sm.h
index 51052ea..748ff2d 100644
--- a/net/nimble/host/include/host/ble_sm.h
+++ b/net/nimble/host/include/host/ble_sm.h
@@ -21,7 +21,7 @@
 #define H_BLE_SM_
 
 #include <inttypes.h>
-#include "nimble/nimble_opt.h"
+#include "syscfg/syscfg.h"
 
 #define BLE_SM_ERR_PASSKEY                      0x01
 #define BLE_SM_ERR_OOB                          0x02
@@ -91,7 +91,7 @@ struct ble_sm_io {
     };
 };
 
-#if NIMBLE_OPT(SM)
+#if NIMBLE_BLE_SM
 int ble_sm_inject_io(uint16_t conn_handle, struct ble_sm_io *pkey);
 #else
 #define ble_sm_inject_io(conn_handle, pkey) \

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/host/pkg.yml b/net/nimble/host/pkg.yml
index a45ba1e..c08545b 100644
--- a/net/nimble/host/pkg.yml
+++ b/net/nimble/host/pkg.yml
@@ -30,29 +30,203 @@ pkg.deps:
     - sys/stats
     - libs/os
     - libs/util
-    - libs/mbedtls
     - net/nimble
 
-    # Tinycrypt is only required when secure connections (NIMBPLE_OPT_SM_SC)
-    # is enabled.  It always gets built as a dependency, but not is not
-    # included by the linker unless SC is enabled.  XXX: We should not build
-    # this library if it is not required.
+pkg.deps.BLE_SM:
+    - libs/mbedtls
+
+pkg.deps.BLE_SM_SC:
     - libs/tinycrypt
 
 pkg.req_apis:
     - ble_transport
     - console
 
-pkg.features:
-    - BLE_HOST
+pkg.init_function: "ble_hs_init"
+pkg.init_stage: 2
+
+pkg.syscfg_defs:
+    # Debug settings.
+    BLE_HS_DEBUG:
+        description: 'TBD'
+        value: 0
+    BLE_HS_PHONY_HCI_ACKS:
+        description: 'TBD'
+        value: 0
+    BLE_HS_REQUIRE_OS:
+        description: 'TBD'
+        value: 1
+
+    # Misc settings.
+    BLE_HS_HEARTBEAT_FREQ:
+        description: 'Milliseconds.'
+        value: 1000
+
+    # L2CAP settings.
+    BLE_L2CAP_MAX_CHANS:
+        description: 'TBD'
+        value: '3*MYNEWT_VAL_BLE_MAX_CONNECTIONS'
+    BLE_L2CAP_SIG_MAX_PROCS:
+        description: 'TBD'
+        value: 1
+
+    # Security manager settings.
+    BLE_SM:
+        description: 'Security manager legacy pairing.'
+        value: 1
+    BLE_SM_SC:
+        description: 'Security manager secure connections (4.2).'
+        value: 0
+
+    BLE_SM_MAX_PROCS:
+        description: 'TBD'
+        value: 1
+    BLE_SM_IO_CAP:
+        description: 'TBD'
+        value: 'BLE_HS_IO_NO_INPUT_OUTPUT'
+    BLE_SM_OOB_DATA_FLAG:
+        description: 'TBD'
+        value: 0
+    BLE_SM_BONDING:
+        description: 'TBD'
+        value: 0
+    BLE_SM_MITM:
+        description: 'TBD'
+        value: 0
+    BLE_SM_KEYPRESS:
+        description: 'TBD'
+        value: 0
+    BLE_SM_OUR_KEY_DIST:
+        description: 'TBD'
+        value: 0
+    BLE_SM_THEIR_KEY_DIST:
+        description: 'TBD'
+        value: 0
+
+    # Supported GATT procedures.  By default:
+    #     o Notify and indicate are enabled;
+    #     o All other procedures are enabled for centrals.
+    BLE_GATT_DISC_ALL_SVCS:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_DISC_SVC_UUID:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_FIND_INC_SVCS:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_DISC_ALL_CHRS:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_DISC_CHR_UUID:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_DISC_ALL_DSCS:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_READ:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_READ_UUID:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_READ_LONG:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_READ_MULT:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_WRITE_NO_RSP:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_SIGNED_WRITE:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_WRITE:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_WRITE_LONG:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_WRITE_RELIABLE:
+        description: 'TBD'
+        value: MYNEWT_VAL_BLE_ROLE_CENTRAL
+    BLE_GATT_NOTIFY:
+        description: 'TBD'
+        value: 1
+    BLE_GATT_INDICATE:
+        description: 'TBD'
+        value: 1
+
+    # GATT options.
+    BLE_GATT_WRITE_MAX_ATTRS:
+        description: >
+            The maximum number of attributes that can be written with a single
+            GATT Reliable Write procedure.
+        value: 4
+    BLE_GATT_MAX_PROCS:
+        description: 'TBD'
+        value: 4
+
+    # Supported server ATT commands.
+    BLE_ATT_SVR_FIND_INFO:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_FIND_TYPE:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_READ_TYPE:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_READ:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_READ_BLOB:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_READ_MULT:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_READ_GROUP_TYPE:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_WRITE:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_WRITE_NO_RSP:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_SIGNED_WRITE:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_PREP_WRITE:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_EXEC_WRITE:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_NOTIFY:
+        description: 'TBD'
+        value: 1
+    BLE_ATT_SVR_INDICATE:
+        description: 'TBD'
+        value: 1
+
+    # ATT options.
+    BLE_ATT_MAX_PREP_ENTRIES:
+        description: >
+            A GATT server uses these when a peer performs a "write long
+            characteristic values" or "write long characteristic descriptors"
+            procedure.  One of these resources is consumed each time a peer
+            sends a partial write.
+        value: 64
 
-# Satisfy capability dependencies for the self-contained test executable.
-pkg.deps.SELFTEST:
-    - libs/console/stub
-    - net/nimble/transport/ram
+    # Privacy options.
+    BLE_RPA_TIMEOUT:
+        description: 'TBD'
+        value: 300
 
-pkg.cflags.SELFTEST:
-    - "-DPHONY_HCI_ACKS=1"
-    - "-DNIMBLE_OPT_SM=1"
-    - "-DNIMBLE_OPT_SM_SC=1"
-pkg.cflags.TEST: -DBLE_HS_DEBUG
+    # Miscellaneous features.
+    BLE_EDDYSTONE:
+        description: 'TBD'
+        value: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gap.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gap.h b/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gap.h
index 95d4226..dcc2712 100644
--- a/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gap.h
+++ b/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gap.h
@@ -34,6 +34,6 @@ struct ble_hs_cfg;
 const char *ble_svc_gap_device_name(void);
 int ble_svc_gap_device_name_set(const char *name);
 
-int ble_svc_gap_init(struct ble_hs_cfg *cfg);
+int ble_svc_gap_init(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gatt.h b/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gatt.h
index d2438c2..721946f 100644
--- a/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gatt.h
+++ b/net/nimble/host/services/mandatory/include/services/mandatory/ble_svc_gatt.h
@@ -5,6 +5,6 @@ struct ble_hs_cfg;
 
 #define BLE_SVC_GATT_CHR_SERVICE_CHANGED_UUID16     0x2a05
 
-int ble_svc_gatt_init(struct ble_hs_cfg *cfg);
+int ble_svc_gatt_init(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/services/mandatory/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/mandatory/pkg.yml b/net/nimble/host/services/mandatory/pkg.yml
index b63c634..255d49b 100644
--- a/net/nimble/host/services/mandatory/pkg.yml
+++ b/net/nimble/host/services/mandatory/pkg.yml
@@ -30,3 +30,6 @@ pkg.keywords:
 
 pkg.deps:
     - net/nimble/host
+
+pkg.init_function: ble_svc_mandatory_pkg_init
+pkg.init_stage: 3

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/services/mandatory/src/ble_svc_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/mandatory/src/ble_svc_gap.c b/net/nimble/host/services/mandatory/src/ble_svc_gap.c
index d49396d..11f9d23 100644
--- a/net/nimble/host/services/mandatory/src/ble_svc_gap.c
+++ b/net/nimble/host/services/mandatory/src/ble_svc_gap.c
@@ -149,11 +149,11 @@ ble_svc_gap_device_name_set(const char *name)
 }
 
 int
-ble_svc_gap_init(struct ble_hs_cfg *cfg)
+ble_svc_gap_init(void)
 {
     int rc;
 
-    rc = ble_gatts_count_cfg(ble_svc_gap_defs, cfg);
+    rc = ble_gatts_count_cfg(ble_svc_gap_defs);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/services/mandatory/src/ble_svc_gatt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/mandatory/src/ble_svc_gatt.c b/net/nimble/host/services/mandatory/src/ble_svc_gatt.c
index 0e8a139..616aa42 100644
--- a/net/nimble/host/services/mandatory/src/ble_svc_gatt.c
+++ b/net/nimble/host/services/mandatory/src/ble_svc_gatt.c
@@ -72,11 +72,11 @@ ble_svc_gatt_access(uint16_t conn_handle, uint16_t attr_handle,
 }
 
 int
-ble_svc_gatt_init(struct ble_hs_cfg *cfg)
+ble_svc_gatt_init(void)
 {
     int rc;
 
-    rc = ble_gatts_count_cfg(ble_svc_gatt_defs, cfg);
+    rc = ble_gatts_count_cfg(ble_svc_gatt_defs);
     if (rc != 0) {
         return rc;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/services/mandatory/src/ble_svc_mandatory.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/mandatory/src/ble_svc_mandatory.c b/net/nimble/host/services/mandatory/src/ble_svc_mandatory.c
new file mode 100644
index 0000000..90fdb4b
--- /dev/null
+++ b/net/nimble/host/services/mandatory/src/ble_svc_mandatory.c
@@ -0,0 +1,36 @@
+/**
+ * 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 <assert.h>
+
+#include "sysinit/sysinit.h"
+#include "services/mandatory/ble_svc_gap.h"
+#include "services/mandatory/ble_svc_gatt.h"
+
+void
+ble_svc_mandatory_pkg_init(void)
+{
+    int rc;
+
+    rc = ble_svc_gap_init();
+    SYSINIT_PANIC_ASSERT(rc == 0);
+
+    rc = ble_svc_gatt_init();
+    SYSINIT_PANIC_ASSERT(rc == 0);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_att_clt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_clt.c b/net/nimble/host/src/ble_att_clt.c
index 87e2f44..e9a882c 100644
--- a/net/nimble/host/src/ble_att_clt.c
+++ b/net/nimble/host/src/ble_att_clt.c
@@ -187,7 +187,7 @@ int
 ble_att_clt_tx_find_info(uint16_t conn_handle,
                          const struct ble_att_find_info_req *req)
 {
-#if !NIMBLE_OPT(ATT_CLT_FIND_INFO)
+#if !NIMBLE_BLE_ATT_CLT_FIND_INFO
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -273,7 +273,7 @@ ble_att_clt_parse_find_info_entry(struct os_mbuf **rxom, uint8_t rsp_format,
 int
 ble_att_clt_rx_find_info(uint16_t conn_handle, struct os_mbuf **om)
 {
-#if !NIMBLE_OPT(ATT_CLT_FIND_INFO)
+#if !NIMBLE_BLE_ATT_CLT_FIND_INFO
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -320,7 +320,7 @@ ble_att_clt_tx_find_type_value(uint16_t conn_handle,
                                const struct ble_att_find_type_value_req *req,
                                const void *attribute_value, int value_len)
 {
-#if !NIMBLE_OPT(ATT_CLT_FIND_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_FIND_TYPE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -384,7 +384,7 @@ ble_att_clt_parse_find_type_value_hinfo(
 int
 ble_att_clt_rx_find_type_value(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_CLT_FIND_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_FIND_TYPE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -426,7 +426,7 @@ ble_att_clt_tx_read_type(uint16_t conn_handle,
                          const struct ble_att_read_type_req *req,
                          const void *uuid128)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_READ_TYPE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -491,7 +491,7 @@ ble_att_clt_parse_read_type_adata(struct os_mbuf **om, int data_len,
 int
 ble_att_clt_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_READ_TYPE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -536,7 +536,7 @@ done:
 int
 ble_att_clt_tx_read(uint16_t conn_handle, const struct ble_att_read_req *req)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ)
+#if !NIMBLE_BLE_ATT_CLT_READ
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -566,7 +566,7 @@ ble_att_clt_tx_read(uint16_t conn_handle, const struct ble_att_read_req *req)
 int
 ble_att_clt_rx_read(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ)
+#if !NIMBLE_BLE_ATT_CLT_READ
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -590,7 +590,7 @@ int
 ble_att_clt_tx_read_blob(uint16_t conn_handle,
                          const struct ble_att_read_blob_req *req)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_BLOB)
+#if !NIMBLE_BLE_ATT_CLT_READ_BLOB
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -621,7 +621,7 @@ ble_att_clt_tx_read_blob(uint16_t conn_handle,
 int
 ble_att_clt_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_BLOB)
+#if !NIMBLE_BLE_ATT_CLT_READ_BLOB
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -681,7 +681,7 @@ int
 ble_att_clt_tx_read_mult(uint16_t conn_handle, const uint16_t *att_handles,
                          int num_att_handles)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_MULT)
+#if !NIMBLE_BLE_ATT_CLT_READ_MULT
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -710,7 +710,7 @@ ble_att_clt_tx_read_mult(uint16_t conn_handle, const uint16_t *att_handles,
 int
 ble_att_clt_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_MULT)
+#if !NIMBLE_BLE_ATT_CLT_READ_MULT
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -735,7 +735,7 @@ ble_att_clt_tx_read_group_type(uint16_t conn_handle,
                                const struct ble_att_read_group_type_req *req,
                                const void *uuid128)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_GROUP_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_READ_GROUP_TYPE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -805,7 +805,7 @@ ble_att_clt_parse_read_group_type_adata(
 int
 ble_att_clt_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_GROUP_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_READ_GROUP_TYPE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -878,7 +878,7 @@ ble_att_clt_tx_write_req(uint16_t conn_handle,
                          const struct ble_att_write_req *req,
                          struct os_mbuf *txom)
 {
-#if !NIMBLE_OPT(ATT_CLT_WRITE)
+#if !NIMBLE_BLE_ATT_CLT_WRITE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -895,7 +895,7 @@ ble_att_clt_tx_write_cmd(uint16_t conn_handle,
                          const struct ble_att_write_req *req,
                          struct os_mbuf *txom)
 {
-#if !NIMBLE_OPT(ATT_CLT_WRITE_NO_RSP)
+#if !NIMBLE_BLE_ATT_CLT_WRITE_NO_RSP
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -910,7 +910,7 @@ ble_att_clt_tx_write_cmd(uint16_t conn_handle,
 int
 ble_att_clt_rx_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_CLT_WRITE)
+#if !NIMBLE_BLE_ATT_CLT_WRITE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -930,7 +930,7 @@ ble_att_clt_tx_prep_write(uint16_t conn_handle,
                           const struct ble_att_prep_write_cmd *req,
                           struct os_mbuf *txom)
 {
-#if !NIMBLE_OPT(ATT_CLT_PREP_WRITE)
+#if !NIMBLE_BLE_ATT_CLT_PREP_WRITE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -981,7 +981,7 @@ err:
 int
 ble_att_clt_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_CLT_PREP_WRITE)
+#if !NIMBLE_BLE_ATT_CLT_PREP_WRITE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1017,7 +1017,7 @@ int
 ble_att_clt_tx_exec_write(uint16_t conn_handle,
                           const struct ble_att_exec_write_req *req)
 {
-#if !NIMBLE_OPT(ATT_CLT_EXEC_WRITE)
+#if !NIMBLE_BLE_ATT_CLT_EXEC_WRITE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1048,7 +1048,7 @@ ble_att_clt_tx_exec_write(uint16_t conn_handle,
 int
 ble_att_clt_rx_exec_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_CLT_EXEC_WRITE)
+#if !NIMBLE_BLE_ATT_CLT_EXEC_WRITE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1074,7 +1074,7 @@ ble_att_clt_tx_notify(uint16_t conn_handle,
                       const struct ble_att_notify_req *req,
                       struct os_mbuf *txom)
 {
-#if !NIMBLE_OPT(ATT_CLT_NOTIFY)
+#if !NIMBLE_BLE_ATT_CLT_NOTIFY
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1116,7 +1116,7 @@ ble_att_clt_tx_indicate(uint16_t conn_handle,
                         const struct ble_att_indicate_req *req,
                         struct os_mbuf *txom)
 {
-#if !NIMBLE_OPT(ATT_CLT_INDICATE)
+#if !NIMBLE_BLE_ATT_CLT_INDICATE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1155,7 +1155,7 @@ err:
 int
 ble_att_clt_rx_indicate(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_CLT_INDICATE)
+#if !NIMBLE_BLE_ATT_CLT_INDICATE
     return BLE_HS_ENOTSUP;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_att_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_priv.h b/net/nimble/host/src/ble_att_priv.h
index edf9dce..04e4365 100644
--- a/net/nimble/host/src/ble_att_priv.h
+++ b/net/nimble/host/src/ble_att_priv.h
@@ -174,6 +174,8 @@ int ble_att_init(void);
 
 /*** @svr */
 
+int ble_att_svr_start(void);
+
 struct ble_att_svr_entry *
 ble_att_svr_find_by_uuid(struct ble_att_svr_entry *start_at,
                          const uint8_t *uuid,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_att_svr.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_svr.c b/net/nimble/host/src/ble_att_svr.c
index 93c57ff..0caf7ca 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -33,7 +33,11 @@ static uint16_t ble_att_svr_id;
 static void *ble_att_svr_entry_mem;
 static struct os_mempool ble_att_svr_entry_pool;
 
-static void *ble_att_svr_prep_entry_mem;
+static os_membuf_t ble_att_svr_prep_entry_mem[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_ATT_MAX_PREP_ENTRIES),
+                    sizeof (struct ble_att_prep_entry))
+];
+
 static struct os_mempool ble_att_svr_prep_entry_pool;
 
 static struct ble_att_svr_entry *
@@ -876,7 +880,7 @@ done:
 int
 ble_att_svr_rx_find_info(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_FIND_INFO)
+#if !MYNEWT_VAL(BLE_ATT_SVR_FIND_INFO)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1193,7 +1197,7 @@ done:
 int
 ble_att_svr_rx_find_type_value(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_FIND_TYPE)
+#if !MYNEWT_VAL(BLE_ATT_SVR_FIND_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1372,7 +1376,7 @@ done:
 int
 ble_att_svr_rx_read_type(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_READ_TYPE)
+#if !MYNEWT_VAL(BLE_ATT_SVR_READ_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1456,7 +1460,7 @@ done:
 int
 ble_att_svr_rx_read(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_READ)
+#if !MYNEWT_VAL(BLE_ATT_SVR_READ)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1512,7 +1516,7 @@ done:
 int
 ble_att_svr_rx_read_blob(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_READ_BLOB)
+#if !MYNEWT_VAL(BLE_ATT_SVR_READ_BLOB)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1653,7 +1657,7 @@ done:
 int
 ble_att_svr_rx_read_mult(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_READ_MULT)
+#if !MYNEWT_VAL(BLE_ATT_SVR_READ_MULT)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1960,7 +1964,7 @@ done:
 int
 ble_att_svr_rx_read_group_type(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_READ_GROUP_TYPE)
+#if !MYNEWT_VAL(BLE_ATT_SVR_READ_GROUP_TYPE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2066,7 +2070,7 @@ done:
 int
 ble_att_svr_rx_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_WRITE)
+#if !MYNEWT_VAL(BLE_ATT_SVR_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2120,7 +2124,7 @@ done:
 int
 ble_att_svr_rx_write_no_rsp(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_WRITE_NO_RSP)
+#if !MYNEWT_VAL(BLE_ATT_SVR_WRITE_NO_RSP)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2415,7 +2419,7 @@ ble_att_svr_insert_prep_entry(uint16_t conn_handle,
 int
 ble_att_svr_rx_prep_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_PREP_WRITE)
+#if !MYNEWT_VAL(BLE_ATT_SVR_PREP_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2529,7 +2533,7 @@ done:
 int
 ble_att_svr_rx_exec_write(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_EXEC_WRITE)
+#if !MYNEWT_VAL(BLE_ATT_SVR_EXEC_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2603,7 +2607,7 @@ done:
 int
 ble_att_svr_rx_notify(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_NOTIFY)
+#if !MYNEWT_VAL(BLE_ATT_SVR_NOTIFY)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2670,7 +2674,7 @@ done:
 int
 ble_att_svr_rx_indicate(uint16_t conn_handle, struct os_mbuf **rxom)
 {
-#if !NIMBLE_OPT(ATT_SVR_INDICATE)
+#if !MYNEWT_VAL(BLE_ATT_SVR_INDICATE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2721,29 +2725,29 @@ done:
 }
 
 static void
-ble_att_svr_free_mem(void)
+ble_att_svr_free_start_mem(void)
 {
     free(ble_att_svr_entry_mem);
     ble_att_svr_entry_mem = NULL;
 }
 
 int
-ble_att_svr_init(void)
+ble_att_svr_start(void)
 {
     int rc;
 
-    ble_att_svr_free_mem();
+    ble_att_svr_free_start_mem();
 
-    if (ble_hs_cfg.max_attrs > 0) {
+    if (ble_hs_max_attrs > 0) {
         ble_att_svr_entry_mem = malloc(
-            OS_MEMPOOL_BYTES(ble_hs_cfg.max_attrs,
+            OS_MEMPOOL_BYTES(ble_hs_max_attrs,
                              sizeof (struct ble_att_svr_entry)));
         if (ble_att_svr_entry_mem == NULL) {
             rc = BLE_HS_ENOMEM;
             goto err;
         }
 
-        rc = os_mempool_init(&ble_att_svr_entry_pool, ble_hs_cfg.max_attrs,
+        rc = os_mempool_init(&ble_att_svr_entry_pool, ble_hs_max_attrs,
                              sizeof (struct ble_att_svr_entry),
                              ble_att_svr_entry_mem, "ble_att_svr_entry_pool");
         if (rc != 0) {
@@ -2752,23 +2756,26 @@ ble_att_svr_init(void)
         }
     }
 
-    if (ble_hs_cfg.max_prep_entries > 0) {
-        ble_att_svr_prep_entry_mem = malloc(
-            OS_MEMPOOL_BYTES(ble_hs_cfg.max_prep_entries,
-                             sizeof (struct ble_att_prep_entry)));
-        if (ble_att_svr_prep_entry_mem == NULL) {
-            rc = BLE_HS_ENOMEM;
-            goto err;
-        }
+    return 0;
+
+err:
+    ble_att_svr_free_start_mem();
+    return rc;
+}
 
+int
+ble_att_svr_init(void)
+{
+    int rc;
+
+    if (MYNEWT_VAL(BLE_ATT_MAX_PREP_ENTRIES) > 0) {
         rc = os_mempool_init(&ble_att_svr_prep_entry_pool,
-                             ble_hs_cfg.max_prep_entries,
+                             MYNEWT_VAL(BLE_ATT_MAX_PREP_ENTRIES),
                              sizeof (struct ble_att_prep_entry),
                              ble_att_svr_prep_entry_mem,
                              "ble_att_svr_prep_entry_pool");
         if (rc != 0) {
-            rc = BLE_HS_EOS;
-            goto err;
+            return BLE_HS_EOS;
         }
     }
 
@@ -2777,8 +2784,4 @@ ble_att_svr_init(void)
     ble_att_svr_id = 0;
 
     return 0;
-
-err:
-    ble_att_svr_free_mem();
-    return rc;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_eddystone.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_eddystone.c b/net/nimble/host/src/ble_eddystone.c
index 2ca496f..ca43828 100644
--- a/net/nimble/host/src/ble_eddystone.c
+++ b/net/nimble/host/src/ble_eddystone.c
@@ -122,7 +122,7 @@ ble_eddystone_set_adv_data_gen(struct ble_hs_adv_fields *adv_fields,
 int
 ble_eddystone_set_adv_data_uid(struct ble_hs_adv_fields *adv_fields, void *uid)
 {
-#if !NIMBLE_OPT(EDDYSTONE)
+#if !MYNEWT_VAL(BLE_EDDYSTONE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -167,7 +167,7 @@ ble_eddystone_set_adv_data_url(struct ble_hs_adv_fields *adv_fields,
                                uint8_t url_scheme, char *url_body,
                                uint8_t url_body_len, uint8_t url_suffix)
 {
-#if !NIMBLE_OPT(EDDYSTONE)
+#if !MYNEWT_VAL(BLE_EDDYSTONE)
     return BLE_HS_ENOTSUP;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap.c b/net/nimble/host/src/ble_gap.c
index 49d678e..81be873 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -743,7 +743,7 @@ ble_gap_conn_broken(uint16_t conn_handle, int reason)
 void
 ble_gap_rx_disconn_complete(struct hci_disconn_complete *evt)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return;
 #endif
 
@@ -766,7 +766,7 @@ ble_gap_rx_disconn_complete(struct hci_disconn_complete *evt)
 void
 ble_gap_rx_update_complete(struct hci_le_conn_upd_complete *evt)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return;
 #endif
 
@@ -905,7 +905,7 @@ ble_gap_accept_slave_conn(uint8_t addr_type, uint8_t *addr)
 void
 ble_gap_rx_adv_report(struct ble_gap_disc_desc *desc)
 {
-#if !NIMBLE_OPT(ROLE_OBSERVER)
+#if !MYNEWT_VAL(BLE_ROLE_OBSERVER)
     return;
 #endif
 
@@ -943,7 +943,7 @@ ble_gap_rx_adv_report(struct ble_gap_disc_desc *desc)
 int
 ble_gap_rx_conn_complete(struct hci_le_conn_complete *evt)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1183,7 +1183,7 @@ ble_gap_heartbeat(void)
 static int
 ble_gap_wl_busy(void)
 {
-#if !NIMBLE_OPT(WHITELIST)
+#if !MYNEWT_VAL(BLE_WHITELIST)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1241,7 +1241,7 @@ int
 ble_gap_wl_set(const struct ble_gap_white_entry *white_list,
                uint8_t white_list_count)
 {
-#if !NIMBLE_OPT(WHITELIST)
+#if !MYNEWT_VAL(BLE_WHITELIST)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1330,7 +1330,7 @@ ble_gap_adv_enable_tx(int enable)
 int
 ble_gap_adv_stop(void)
 {
-#if !NIMBLE_OPT(ADVERTISE)
+#if !NIMBLE_BLE_ADVERTISE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1674,7 +1674,7 @@ ble_gap_adv_start(uint8_t own_addr_type, uint8_t peer_addr_type,
                   const struct ble_gap_adv_params *adv_params,
                   ble_gap_event_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(ADVERTISE)
+#if !NIMBLE_BLE_ADVERTISE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1768,7 +1768,7 @@ done:
 int
 ble_gap_adv_set_fields(const struct ble_hs_adv_fields *adv_fields)
 {
-#if !NIMBLE_OPT(ADVERTISE)
+#if !NIMBLE_BLE_ADVERTISE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1825,7 +1825,7 @@ done:
 int
 ble_gap_adv_rsp_set_fields(const struct ble_hs_adv_fields *rsp_fields)
 {
-#if !NIMBLE_OPT(ADVERTISE)
+#if !NIMBLE_BLE_ADVERTISE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2038,7 +2038,7 @@ ble_gap_disc(uint8_t own_addr_type, int32_t duration_ms,
              const struct ble_gap_disc_params *disc_params,
              ble_gap_event_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(ROLE_OBSERVER)
+#if !MYNEWT_VAL(BLE_ROLE_OBSERVER)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2223,7 +2223,7 @@ ble_gap_connect(uint8_t own_addr_type,
                 const struct ble_gap_conn_params *conn_params,
                 ble_gap_event_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(ROLE_CENTRAL)
+#if !MYNEWT_VAL(BLE_ROLE_CENTRAL)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2493,7 +2493,7 @@ ble_gap_tx_param_neg_reply(uint16_t conn_handle, uint8_t reject_reason)
 void
 ble_gap_rx_param_req(struct hci_le_conn_param_req *evt)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return;
 #endif
 
@@ -2592,7 +2592,7 @@ int
 ble_gap_update_params(uint16_t conn_handle,
                       const struct ble_gap_upd_params *params)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2654,7 +2654,7 @@ done:
 int
 ble_gap_security_initiate(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT(SM)
+#if !NIMBLE_BLE_SM
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2737,7 +2737,7 @@ ble_gap_encryption_initiate(uint16_t conn_handle,
                             uint64_t rand_val,
                             int auth)
 {
-#if !NIMBLE_OPT(SM)
+#if !NIMBLE_BLE_SM
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2761,7 +2761,7 @@ void
 ble_gap_passkey_event(uint16_t conn_handle,
                       struct ble_gap_passkey_params *passkey_params)
 {
-#if !NIMBLE_OPT(SM)
+#if !NIMBLE_BLE_SM
     return;
 #endif
 
@@ -2780,7 +2780,7 @@ ble_gap_passkey_event(uint16_t conn_handle,
 void
 ble_gap_enc_event(uint16_t conn_handle, int status, int security_restored)
 {
-#if !NIMBLE_OPT(SM)
+#if !NIMBLE_BLE_SM
     return;
 #endif
 
@@ -2831,7 +2831,7 @@ void
 ble_gap_notify_rx_event(uint16_t conn_handle, uint16_t attr_handle,
                         struct os_mbuf *om, int is_indication)
 {
-#if !NIMBLE_OPT(GATT_NOTIFY) && !NIMBLE_OPT(GATT_INDICATE)
+#if !MYNEWT_VAL(BLE_GATT_NOTIFY) && !MYNEWT_VAL(BLE_GATT_INDICATE)
     return;
 #endif
 
@@ -2852,7 +2852,7 @@ void
 ble_gap_notify_tx_event(int status, uint16_t conn_handle, uint16_t attr_handle,
                         int is_indication)
 {
-#if !NIMBLE_OPT(GATT_NOTIFY) && !NIMBLE_OPT(GATT_INDICATE)
+#if !MYNEWT_VAL(BLE_GATT_NOTIFY) && !MYNEWT_VAL(BLE_GATT_INDICATE)
     return;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_gattc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gattc.c b/net/nimble/host/src/ble_gattc.c
index 43d2134..e37deaf 100644
--- a/net/nimble/host/src/ble_gattc.c
+++ b/net/nimble/host/src/ble_gattc.c
@@ -182,7 +182,7 @@ struct ble_gattc_proc {
         } write_long;
 
         struct {
-            struct ble_gatt_attr attrs[NIMBLE_OPT(GATT_WRITE_MAX_ATTRS)];
+            struct ble_gatt_attr attrs[MYNEWT_VAL(BLE_GATT_WRITE_MAX_ATTRS)];
             uint8_t num_attrs;
             uint8_t cur_attr;
             uint16_t length;
@@ -317,7 +317,11 @@ static const struct ble_gattc_rx_exec_entry {
 };
 
 /* Maintains the list of active GATT client procedures. */
-static void *ble_gattc_proc_mem;
+static os_membuf_t ble_gattc_proc_mem[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_GATT_MAX_PROCS),
+                    sizeof (struct ble_gattc_proc))
+];
+
 static struct os_mempool ble_gattc_proc_pool;
 static struct ble_gattc_proc_list ble_gattc_procs;
 
@@ -368,7 +372,7 @@ STATS_NAME_END(ble_gattc_stats)
 static void
 ble_gattc_dbg_assert_proc_not_inserted(struct ble_gattc_proc *proc)
 {
-#if BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     struct ble_gattc_proc *cur;
 
     ble_hs_lock();
@@ -1250,7 +1254,7 @@ int
 ble_gattc_disc_all_svcs(uint16_t conn_handle, ble_gatt_disc_svc_fn *cb,
                         void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_DISC_ALL_SVCS)
+#if !MYNEWT_VAL(BLE_GATT_DISC_ALL_SVCS)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1454,7 +1458,7 @@ int
 ble_gattc_disc_svc_by_uuid(uint16_t conn_handle, const void *svc_uuid128,
                            ble_gatt_disc_svc_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_DISC_SVC_UUID)
+#if !MYNEWT_VAL(BLE_GATT_DISC_SVC_UUID)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -1775,7 +1779,7 @@ ble_gattc_find_inc_svcs(uint16_t conn_handle, uint16_t start_handle,
                         uint16_t end_handle,
                         ble_gatt_disc_svc_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_FIND_INC_SVCS)
+#if !MYNEWT_VAL(BLE_GATT_FIND_INC_SVCS)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2007,7 +2011,7 @@ ble_gattc_disc_all_chrs(uint16_t conn_handle, uint16_t start_handle,
                         uint16_t end_handle, ble_gatt_chr_fn *cb,
                         void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_DISC_ALL_CHRS)
+#if !MYNEWT_VAL(BLE_GATT_DISC_ALL_CHRS)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2252,7 +2256,7 @@ ble_gattc_disc_chrs_by_uuid(uint16_t conn_handle, uint16_t start_handle,
                             uint16_t end_handle, const void *uuid128,
                             ble_gatt_chr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_DISC_CHR_UUID)
+#if !MYNEWT_VAL(BLE_GATT_DISC_CHR_UUID)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2458,7 +2462,7 @@ ble_gattc_disc_all_dscs(uint16_t conn_handle, uint16_t chr_val_handle,
                         uint16_t chr_end_handle,
                         ble_gatt_dsc_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_DISC_ALL_DSCS)
+#if !MYNEWT_VAL(BLE_GATT_DISC_ALL_DSCS)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2587,7 +2591,7 @@ int
 ble_gattc_read(uint16_t conn_handle, uint16_t attr_handle,
                ble_gatt_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_READ)
+#if !MYNEWT_VAL(BLE_GATT_READ)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2755,7 +2759,7 @@ ble_gattc_read_by_uuid(uint16_t conn_handle, uint16_t start_handle,
                        uint16_t end_handle, const void *uuid128,
                        ble_gatt_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_READ_UUID)
+#if !MYNEWT_VAL(BLE_GATT_READ_UUID)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -2944,7 +2948,7 @@ int
 ble_gattc_read_long(uint16_t conn_handle, uint16_t handle,
                     ble_gatt_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_READ_LONG)
+#if !MYNEWT_VAL(BLE_GATT_READ_LONG)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3063,7 +3067,7 @@ ble_gattc_read_mult(uint16_t conn_handle, const uint16_t *handles,
                     uint8_t num_handles, ble_gatt_attr_fn *cb,
                     void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_READ_MULT)
+#if !MYNEWT_VAL(BLE_GATT_READ_MULT)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3119,7 +3123,7 @@ int
 ble_gattc_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle,
                        struct os_mbuf *txom)
 {
-#if !NIMBLE_OPT(GATT_WRITE_NO_RSP)
+#if !MYNEWT_VAL(BLE_GATT_WRITE_NO_RSP)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3242,7 +3246,7 @@ int
 ble_gattc_write(uint16_t conn_handle, uint16_t attr_handle,
                 struct os_mbuf *txom, ble_gatt_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_WRITE)
+#if !MYNEWT_VAL(BLE_GATT_WRITE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3570,7 +3574,7 @@ int
 ble_gattc_write_long(uint16_t conn_handle, uint16_t attr_handle,
                      struct os_mbuf *txom, ble_gatt_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_WRITE_LONG)
+#if !MYNEWT_VAL(BLE_GATT_WRITE_LONG)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3852,7 +3856,7 @@ ble_gattc_write_reliable(uint16_t conn_handle,
                          int num_attrs,
                          ble_gatt_reliable_attr_fn *cb, void *cb_arg)
 {
-#if !NIMBLE_OPT(GATT_WRITE_RELIABLE)
+#if !MYNEWT_VAL(BLE_GATT_WRITE_RELIABLE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3864,7 +3868,7 @@ ble_gattc_write_reliable(uint16_t conn_handle,
 
     STATS_INC(ble_gattc_stats, write_reliable);
 
-    if (num_attrs > NIMBLE_OPT(GATT_WRITE_MAX_ATTRS)) {
+    if (num_attrs > MYNEWT_VAL(BLE_GATT_WRITE_MAX_ATTRS)) {
         rc = BLE_HS_EINVAL;
         goto done;
     }
@@ -3932,7 +3936,7 @@ int
 ble_gattc_notify_custom(uint16_t conn_handle, uint16_t chr_val_handle,
                         struct os_mbuf *txom)
 {
-#if !NIMBLE_OPT(GATT_NOTIFY)
+#if !MYNEWT_VAL(BLE_GATT_NOTIFY)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -3995,7 +3999,7 @@ err:
 int
 ble_gattc_notify(uint16_t conn_handle, uint16_t chr_val_handle)
 {
-#if !NIMBLE_OPT(GATT_NOTIFY)
+#if !MYNEWT_VAL(BLE_GATT_NOTIFY)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -4093,7 +4097,7 @@ ble_gatts_indicate_fail_notconn(uint16_t conn_handle)
 int
 ble_gattc_indicate(uint16_t conn_handle, uint16_t chr_val_handle)
 {
-#if !NIMBLE_OPT(GATT_INDICATE)
+#if !MYNEWT_VAL(BLE_GATT_INDICATE)
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -4212,7 +4216,7 @@ void
 ble_gattc_rx_find_info_idata(uint16_t conn_handle,
                              struct ble_att_find_info_idata *idata)
 {
-#if !NIMBLE_OPT(ATT_CLT_FIND_INFO)
+#if !NIMBLE_BLE_ATT_CLT_FIND_INFO
     return;
 #endif
 
@@ -4233,7 +4237,7 @@ ble_gattc_rx_find_info_idata(uint16_t conn_handle,
 void
 ble_gattc_rx_find_info_complete(uint16_t conn_handle, int status)
 {
-#if !NIMBLE_OPT(ATT_CLT_FIND_INFO)
+#if !NIMBLE_BLE_ATT_CLT_FIND_INFO
     return;
 #endif
 
@@ -4255,7 +4259,7 @@ void
 ble_gattc_rx_find_type_value_hinfo(uint16_t conn_handle,
                                    struct ble_att_find_type_value_hinfo *hinfo)
 {
-#if !NIMBLE_OPT(ATT_CLT_FIND_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_FIND_TYPE
     return;
 #endif
 
@@ -4276,7 +4280,7 @@ ble_gattc_rx_find_type_value_hinfo(uint16_t conn_handle,
 void
 ble_gattc_rx_find_type_value_complete(uint16_t conn_handle, int status)
 {
-#if !NIMBLE_OPT(ATT_CLT_FIND_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_FIND_TYPE
     return;
 #endif
 
@@ -4298,7 +4302,7 @@ void
 ble_gattc_rx_read_type_adata(uint16_t conn_handle,
                              struct ble_att_read_type_adata *adata)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_READ_TYPE
     return;
 #endif
 
@@ -4322,7 +4326,7 @@ ble_gattc_rx_read_type_adata(uint16_t conn_handle,
 void
 ble_gattc_rx_read_type_complete(uint16_t conn_handle, int status)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_READ_TYPE
     return;
 #endif
 
@@ -4347,7 +4351,7 @@ void
 ble_gattc_rx_read_group_type_adata(uint16_t conn_handle,
                                    struct ble_att_read_group_type_adata *adata)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_GROUP_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_READ_GROUP_TYPE
     return;
 #endif
 
@@ -4368,7 +4372,7 @@ ble_gattc_rx_read_group_type_adata(uint16_t conn_handle,
 void
 ble_gattc_rx_read_group_type_complete(uint16_t conn_handle, int status)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_GROUP_TYPE)
+#if !NIMBLE_BLE_ATT_CLT_READ_GROUP_TYPE
     return;
 #endif
 
@@ -4389,7 +4393,7 @@ ble_gattc_rx_read_group_type_complete(uint16_t conn_handle, int status)
 void
 ble_gattc_rx_read_rsp(uint16_t conn_handle, int status, struct os_mbuf **om)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ)
+#if !NIMBLE_BLE_ATT_CLT_READ
     return;
 #endif
 
@@ -4414,7 +4418,7 @@ void
 ble_gattc_rx_read_blob_rsp(uint16_t conn_handle, int status,
                            struct os_mbuf **om)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_BLOB)
+#if !NIMBLE_BLE_ATT_CLT_READ_BLOB
     return;
 #endif
 
@@ -4436,7 +4440,7 @@ void
 ble_gattc_rx_read_mult_rsp(uint16_t conn_handle, int status,
                            struct os_mbuf **om)
 {
-#if !NIMBLE_OPT(ATT_CLT_READ_MULT)
+#if !NIMBLE_BLE_ATT_CLT_READ_MULT
     return;
 #endif
 
@@ -4456,7 +4460,7 @@ ble_gattc_rx_read_mult_rsp(uint16_t conn_handle, int status,
 void
 ble_gattc_rx_write_rsp(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT(ATT_CLT_WRITE)
+#if !NIMBLE_BLE_ATT_CLT_WRITE
     return;
 #endif
 
@@ -4478,7 +4482,7 @@ ble_gattc_rx_prep_write_rsp(uint16_t conn_handle, int status,
                             struct ble_att_prep_write_cmd *rsp,
                             struct os_mbuf **om)
 {
-#if !NIMBLE_OPT(ATT_CLT_PREP_WRITE)
+#if !NIMBLE_BLE_ATT_CLT_PREP_WRITE
     return;
 #endif
 
@@ -4502,7 +4506,7 @@ ble_gattc_rx_prep_write_rsp(uint16_t conn_handle, int status,
 void
 ble_gattc_rx_exec_write_rsp(uint16_t conn_handle, int status)
 {
-#if !NIMBLE_OPT(ATT_CLT_EXEC_WRITE)
+#if !NIMBLE_BLE_ATT_CLT_EXEC_WRITE
     return;
 #endif
 
@@ -4525,7 +4529,7 @@ ble_gattc_rx_exec_write_rsp(uint16_t conn_handle, int status)
 void
 ble_gattc_rx_indicate_rsp(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT(ATT_CLT_INDICATE)
+#if !NIMBLE_BLE_ATT_CLT_INDICATE
     return;
 #endif
 
@@ -4570,26 +4574,16 @@ ble_gattc_init(void)
 {
     int rc;
 
-    free(ble_gattc_proc_mem);
-
     STAILQ_INIT(&ble_gattc_procs);
 
-    if (ble_hs_cfg.max_gattc_procs > 0) {
-        ble_gattc_proc_mem = malloc(
-            OS_MEMPOOL_BYTES(ble_hs_cfg.max_gattc_procs,
-                             sizeof (struct ble_gattc_proc)));
-        if (ble_gattc_proc_mem == NULL) {
-            rc = BLE_HS_ENOMEM;
-            goto err;
-        }
-
+    if (MYNEWT_VAL(BLE_GATT_MAX_PROCS) > 0) {
         rc = os_mempool_init(&ble_gattc_proc_pool,
-                             ble_hs_cfg.max_gattc_procs,
+                             MYNEWT_VAL(BLE_GATT_MAX_PROCS),
                              sizeof (struct ble_gattc_proc),
                              ble_gattc_proc_mem,
                              "ble_gattc_proc_pool");
         if (rc != 0) {
-            goto err;
+            return rc;
         }
     }
 
@@ -4597,15 +4591,8 @@ ble_gattc_init(void)
         STATS_HDR(ble_gattc_stats), STATS_SIZE_INIT_PARMS(ble_gattc_stats,
         STATS_SIZE_32), STATS_NAME_INIT_PARMS(ble_gattc_stats), "ble_gattc");
     if (rc != 0) {
-        rc = BLE_HS_EOS;
-        goto err;
+        return BLE_HS_EOS;
     }
 
     return 0;
-
-err:
-    free(ble_gattc_proc_mem);
-    ble_gattc_proc_mem = NULL;
-
-    return rc;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_gatts.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index 4bdc6c7..0286790 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -779,7 +779,7 @@ ble_gatts_register_chr(const struct ble_gatt_svc_def *svc,
     }
 
     if (ble_gatts_chr_clt_cfg_allowed(chr) != 0) {
-        if (ble_gatts_num_cfgable_chrs > ble_hs_cfg.max_client_configs) {
+        if (ble_gatts_num_cfgable_chrs > ble_hs_max_client_configs) {
             return BLE_HS_ENOMEM;
         }
         ble_gatts_num_cfgable_chrs++;
@@ -1020,7 +1020,7 @@ ble_gatts_register_svcs(const struct ble_gatt_svc_def *svcs,
 
     for (i = 0; svcs[i].type != BLE_GATT_SVC_TYPE_END; i++) {
         idx = ble_gatts_num_svc_entries + i;
-        if (idx >= ble_hs_cfg.max_services) {
+        if (idx >= ble_hs_max_services) {
             return BLE_HS_ENOMEM;
         }
 
@@ -1110,6 +1110,24 @@ ble_gatts_connection_broken(uint16_t conn_handle)
     }
 }
 
+static void
+ble_gatts_free_svc_defs(void)
+{
+    free(ble_gatts_svc_defs);
+    ble_gatts_svc_defs = NULL;
+    ble_gatts_num_svc_defs = 0;
+}
+
+static void
+ble_gatts_free_mem(void)
+{
+    free(ble_gatts_clt_cfg_mem);
+    ble_gatts_clt_cfg_mem = NULL;
+
+    free(ble_gatts_svc_entries);
+    ble_gatts_svc_entries = NULL;
+}
+
 int
 ble_gatts_start(void)
 {
@@ -1120,18 +1138,53 @@ ble_gatts_start(void)
     int num_elems;
     int idx;
     int rc;
+    int i;
+
+    ble_gatts_free_mem();
+
+    if (ble_hs_max_client_configs > 0) {
+        ble_gatts_clt_cfg_mem = malloc(
+            OS_MEMPOOL_BYTES(ble_hs_max_client_configs,
+                             sizeof (struct ble_gatts_clt_cfg)));
+        if (ble_gatts_clt_cfg_mem == NULL) {
+            rc = BLE_HS_ENOMEM;
+            goto err;
+        }
+    }
+
+    if (ble_hs_max_services > 0) {
+        ble_gatts_svc_entries =
+            malloc(ble_hs_max_services * sizeof *ble_gatts_svc_entries);
+        if (ble_gatts_svc_entries == NULL) {
+            rc = BLE_HS_ENOMEM;
+            goto err;
+        }
+    }
+
+
+    ble_gatts_num_svc_entries = 0;
+    for (i = 0; i < ble_gatts_num_svc_defs; i++) {
+        rc = ble_gatts_register_svcs(ble_gatts_svc_defs[i],
+                                     ble_hs_cfg.gatts_register_cb,
+                                     ble_hs_cfg.gatts_register_arg);
+        if (rc != 0) {
+            goto err;
+        }
+    }
+    ble_gatts_free_svc_defs();
 
     if (ble_gatts_num_cfgable_chrs == 0) {
         return 0;
     }
 
     /* Initialize client-configuration memory pool. */
-    num_elems = ble_hs_cfg.max_client_configs / ble_gatts_num_cfgable_chrs;
+    num_elems = ble_hs_max_client_configs / ble_gatts_num_cfgable_chrs;
     rc = os_mempool_init(&ble_gatts_clt_cfg_pool, num_elems,
                          ble_gatts_clt_cfg_size(), ble_gatts_clt_cfg_mem,
                          "ble_gatts_clt_cfg_pool");
     if (rc != 0) {
-        return BLE_HS_EOS;
+        rc = BLE_HS_EOS;
+        goto err;
     }
 
     /* Allocate the cached array of handles for the configuration
@@ -1139,7 +1192,8 @@ ble_gatts_start(void)
      */
     ble_gatts_clt_cfgs = os_memblock_get(&ble_gatts_clt_cfg_pool);
     if (ble_gatts_clt_cfgs == NULL) {
-        return BLE_HS_ENOMEM;
+        rc = BLE_HS_ENOMEM;
+        goto err;
     }
 
     /* Fill the cache. */
@@ -1161,6 +1215,11 @@ ble_gatts_start(void)
     }
 
     return 0;
+
+err:
+    ble_gatts_free_mem();
+    ble_gatts_free_svc_defs();
+    return rc;
 }
 
 int
@@ -1997,8 +2056,7 @@ ble_gatts_count_resources(const struct ble_gatt_svc_def *svcs,
  *                                  invalid resource definition.
  */
 int
-ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs,
-                    struct ble_hs_cfg *cfg)
+ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs)
 {
     struct ble_gatt_resources res = { 0 };
     int rc;
@@ -2008,85 +2066,31 @@ ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs,
         return rc;
     }
 
-    cfg->max_services += res.svcs;
-    cfg->max_attrs += res.attrs;
+    ble_hs_max_services += res.svcs;
+    ble_hs_max_attrs += res.attrs;
 
     /* Reserve an extra CCCD for the cache. */
-    cfg->max_client_configs += res.cccds * (cfg->max_connections + 1);
+    ble_hs_max_client_configs +=
+        res.cccds * (MYNEWT_VAL(BLE_MAX_CONNECTIONS) + 1);
 
     return 0;
 }
 
-static void
-ble_gatts_free_svc_defs(void)
-{
-    free(ble_gatts_svc_defs);
-    ble_gatts_svc_defs = NULL;
-    ble_gatts_num_svc_defs = 0;
-}
-
-static void
-ble_gatts_free_mem(void)
-{
-    free(ble_gatts_clt_cfg_mem);
-    ble_gatts_clt_cfg_mem = NULL;
-
-    free(ble_gatts_svc_entries);
-    ble_gatts_svc_entries = NULL;
-}
-
 int
 ble_gatts_init(void)
 {
     int rc;
-    int i;
 
-    ble_gatts_free_mem();
     ble_gatts_num_cfgable_chrs = 0;
     ble_gatts_clt_cfgs = NULL;
 
-    if (ble_hs_cfg.max_client_configs > 0) {
-        ble_gatts_clt_cfg_mem = malloc(
-            OS_MEMPOOL_BYTES(ble_hs_cfg.max_client_configs,
-                             sizeof (struct ble_gatts_clt_cfg)));
-        if (ble_gatts_clt_cfg_mem == NULL) {
-            rc = BLE_HS_ENOMEM;
-            goto err;
-        }
-    }
-
-    if (ble_hs_cfg.max_services > 0) {
-        ble_gatts_svc_entries =
-            malloc(ble_hs_cfg.max_services * sizeof *ble_gatts_svc_entries);
-        if (ble_gatts_svc_entries == NULL) {
-            rc = BLE_HS_ENOMEM;
-            goto err;
-        }
-    }
-
-    ble_gatts_num_svc_entries = 0;
-    for (i = 0; i < ble_gatts_num_svc_defs; i++) {
-        rc = ble_gatts_register_svcs(ble_gatts_svc_defs[i],
-                                     ble_hs_cfg.gatts_register_cb,
-                                     ble_hs_cfg.gatts_register_arg);
-        if (rc != 0) {
-            goto err;
-        }
-    }
-    ble_gatts_free_svc_defs();
-
     rc = stats_init_and_reg(
         STATS_HDR(ble_gatts_stats), STATS_SIZE_INIT_PARMS(ble_gatts_stats,
         STATS_SIZE_32), STATS_NAME_INIT_PARMS(ble_gatts_stats), "ble_gatts");
     if (rc != 0) {
-        rc = BLE_HS_EOS;
-        goto err;
+        return BLE_HS_EOS;
     }
 
     return 0;
 
-err:
-    ble_gatts_free_mem();
-    ble_gatts_free_svc_defs();
-    return rc;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_hs.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs.c b/net/nimble/host/src/ble_hs.c
index 02db879..84a69a6 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -19,6 +19,8 @@
 
 #include <assert.h>
 #include <errno.h>
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
 #include "bsp/bsp.h"
 #include "stats/stats.h"
 #include "util/tpq.h"
@@ -26,6 +28,10 @@
 #include "nimble/ble_hci_trans.h"
 #include "ble_hs_priv.h"
 
+#define BLE_HS_HCI_EVT_COUNT                    \
+    (MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT) +     \
+     MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT))
+
 /**
  * The maximum number of events the host will process in a row before returning
  * control to the parent task.
@@ -35,7 +41,9 @@
 static struct log_handler ble_hs_log_console_handler;
 
 struct os_mempool ble_hs_hci_ev_pool;
-static void *ble_hs_hci_os_event_buf;
+static os_membuf_t ble_hs_hci_os_event_buf[
+    OS_MEMPOOL_SIZE(BLE_HS_HCI_EVT_COUNT, sizeof (struct os_event))
+];
 
 /** OS event - triggers tx of pending notifications and indications. */
 static struct os_event ble_hs_event_tx_notifications = {
@@ -52,15 +60,13 @@ static struct os_event ble_hs_event_reset = {
 uint8_t ble_hs_sync_state;
 static int ble_hs_reset_reason;
 
-#if MYNEWT_SELFTEST
-/** Use a higher frequency timer to allow tests to run faster. */
-#define BLE_HS_HEARTBEAT_OS_TICKS       (OS_TICKS_PER_SEC / 10)
-#else
-#define BLE_HS_HEARTBEAT_OS_TICKS       OS_TICKS_PER_SEC
-#endif
+#define BLE_HS_HEARTBEAT_OS_TICKS       \
+    (MYNEWT_VAL(BLE_HS_HEARTBEAT_FREQ) * OS_TICKS_PER_SEC / 1000)
 
 #define BLE_HS_SYNC_RETRY_RATE          (OS_TICKS_PER_SEC / 10)    
 
+static struct os_task *ble_hs_parent_task;
+
 /**
  * Handles unresponsive timeouts and periodic retries in case of resource
  * shortage.
@@ -71,16 +77,20 @@ static struct os_callout_func ble_hs_event_co;
 /* Queue for host-specific OS events. */
 static struct os_eventq ble_hs_evq;
 
-/* Task structures for the host's parent task. */
-static struct os_eventq *ble_hs_parent_evq;
-static struct os_task *ble_hs_parent_task;
-
 static struct os_mqueue ble_hs_rx_q;
 static struct os_mqueue ble_hs_tx_q;
 
 static struct os_mutex ble_hs_mutex;
 
-#if BLE_HS_DEBUG
+/** These values keep track of required ATT and GATT resources counts.  They
+ * increase as services are added, and are read when the ATT server and GATT
+ * server are started.
+ */
+uint16_t ble_hs_max_attrs;
+uint16_t ble_hs_max_services;
+uint16_t ble_hs_max_client_configs;
+
+#if MYNEWT_VAL(BLE_HS_DEBUG)
 static uint8_t ble_hs_dbg_mutex_locked;
 #endif
 
@@ -102,7 +112,7 @@ ble_hs_locked_by_cur_task(void)
 {
     struct os_task *owner;
 
-#if BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     if (!os_started()) {
         return ble_hs_dbg_mutex_locked;
     }
@@ -118,7 +128,8 @@ ble_hs_locked_by_cur_task(void)
 int
 ble_hs_is_parent_task(void)
 {
-    return !os_started() || os_sched_get_current_task() == ble_hs_parent_task;
+    return !os_started() ||
+           os_sched_get_current_task() == ble_hs_parent_task;
 }
 
 void
@@ -128,7 +139,7 @@ ble_hs_lock(void)
 
     BLE_HS_DBG_ASSERT(!ble_hs_locked_by_cur_task());
 
-#if BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     if (!os_started()) {
         ble_hs_dbg_mutex_locked = 1;
         return;
@@ -144,7 +155,7 @@ ble_hs_unlock(void)
 {
     int rc;
 
-#if BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     if (!os_started()) {
         BLE_HS_DBG_ASSERT(ble_hs_dbg_mutex_locked);
         ble_hs_dbg_mutex_locked = 0;
@@ -345,7 +356,7 @@ ble_hs_event_handle(void *unused)
          * task's eventq to indicate that more host events are enqueued.
          */
         if (i >= BLE_HS_MAX_EVS_IN_A_ROW) {
-            os_eventq_put(ble_hs_parent_evq, &ble_hs_event_co.cf_c.c_ev);
+            os_eventq_put(ble_hs_cfg.parent_evq, &ble_hs_event_co.cf_c.c_ev);
             break;
         }
         i++;
@@ -395,7 +406,7 @@ void
 ble_hs_event_enqueue(struct os_event *ev)
 {
     os_eventq_put(&ble_hs_evq, ev);
-    os_eventq_put(ble_hs_parent_evq, &ble_hs_event_co.cf_c.c_ev);
+    os_eventq_put(ble_hs_cfg.parent_evq, &ble_hs_event_co.cf_c.c_ev);
 }
 
 void
@@ -421,7 +432,7 @@ ble_hs_enqueue_hci_event(uint8_t *hci_evt)
 void
 ble_hs_notifications_sched(void)
 {
-#if MYNEWT_SELFTEST
+#if !MYNEWT_VAL(BLE_HS_REQUIRE_OS)
     if (!os_started()) {
         ble_gatts_tx_notifications();
         return;
@@ -464,12 +475,31 @@ ble_hs_start(void)
 {
     int rc;
 
+    if (ble_hs_cfg.parent_evq == NULL) {
+        return BLE_HS_EINVAL;
+    }
+
     ble_hs_parent_task = os_sched_get_current_task();
 
-    ble_gatts_start();
+    os_callout_func_init(&ble_hs_heartbeat_timer, ble_hs_cfg.parent_evq,
+                         ble_hs_heartbeat, NULL);
+
+    rc = ble_att_svr_start();
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = ble_gatts_start();
+    if (rc != 0) {
+        return rc;
+    }
 
     rc = ble_hs_sync();
-    return rc;
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
 }
 
 /**
@@ -488,7 +518,7 @@ ble_hs_rx_data(struct os_mbuf *om, void *arg)
 
     rc = os_mqueue_put(&ble_hs_rx_q, &ble_hs_evq, om);
     if (rc == 0) {
-        os_eventq_put(ble_hs_parent_evq, &ble_hs_event_co.cf_c.c_ev);
+        os_eventq_put(ble_hs_cfg.parent_evq, &ble_hs_event_co.cf_c.c_ev);
     } else {
         os_mbuf_free_chain(om);
         rc = BLE_HS_EOS;
@@ -515,113 +545,58 @@ ble_hs_tx_data(struct os_mbuf *om)
         os_mbuf_free_chain(om);
         return BLE_HS_EOS;
     }
-    os_eventq_put(ble_hs_parent_evq, &ble_hs_event_co.cf_c.c_ev);
+    os_eventq_put(ble_hs_cfg.parent_evq, &ble_hs_event_co.cf_c.c_ev);
 
     return 0;
 }
 
-static void
-ble_hs_free_mem(void)
-{
-    free(ble_hs_hci_os_event_buf);
-    ble_hs_hci_os_event_buf = NULL;
-}
-
 /**
  * Initializes the NimBLE host.  This function must be called before the OS is
  * started.  The NimBLE stack requires an application task to function.  One
  * application task in particular is designated as the "host parent task".  In
  * addition to application-specific work, the host parent task does work for
  * NimBLE by processing events generated by the host.
- *
- * @param app_evq               The event queue associated with the host parent
- *                                  task.
- * @param cfg                   The set of configuration settings to initialize
- *                                  the host with.  Specify null for defaults.
- *
- * @return                      0 on success;
- *                              BLE_HS_ENOMEM if initialization failed due to
- *                                  resource exhaustion.
- *                              Other nonzero on error.
  */
-int
-ble_hs_init(struct os_eventq *app_evq, struct ble_hs_cfg *cfg)
+void
+ble_hs_init(void)
 {
     int rc;
 
-    ble_hs_free_mem();
-
-    if (app_evq == NULL) {
-        rc = BLE_HS_EINVAL;
-        goto err;
-    }
-    ble_hs_parent_evq = app_evq;
-
-    ble_hs_cfg_init(cfg);
-
     log_init();
     log_console_handler_init(&ble_hs_log_console_handler);
     log_register("ble_hs", &ble_hs_log, &ble_hs_log_console_handler);
 
-    ble_hs_hci_os_event_buf = malloc(
-        OS_MEMPOOL_BYTES(ble_hs_cfg.max_hci_bufs, sizeof (struct os_event)));
-    if (ble_hs_hci_os_event_buf == NULL) {
-        rc = BLE_HS_ENOMEM;
-        goto err;
-    }
-
     /* Create memory pool of OS events */
-    rc = os_mempool_init(&ble_hs_hci_ev_pool, ble_hs_cfg.max_hci_bufs,
+    rc = os_mempool_init(&ble_hs_hci_ev_pool, BLE_HS_HCI_EVT_COUNT,
                          sizeof (struct os_event), ble_hs_hci_os_event_buf,
                          "ble_hs_hci_ev_pool");
-    assert(rc == 0);
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     /* Initialize eventq */
     os_eventq_init(&ble_hs_evq);
 
-    /* Initialize stats. */
-    rc = stats_module_init();
-    if (rc != 0) {
-        rc = BLE_HS_EOS;
-        goto err;
-    }
-
     ble_hs_hci_init();
 
     rc = ble_hs_conn_init();
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = ble_l2cap_init();
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = ble_att_init();
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = ble_att_svr_init();
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = ble_gap_init();
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = ble_gattc_init();
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = ble_gatts_init();
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     os_mqueue_init(&ble_hs_rx_q, NULL);
     os_mqueue_init(&ble_hs_tx_q, NULL);
@@ -629,31 +604,21 @@ ble_hs_init(struct os_eventq *app_evq, struct ble_hs_cfg *cfg)
     rc = stats_init_and_reg(
         STATS_HDR(ble_hs_stats), STATS_SIZE_INIT_PARMS(ble_hs_stats,
         STATS_SIZE_32), STATS_NAME_INIT_PARMS(ble_hs_stats), "ble_hs");
-    if (rc != 0) {
-        rc = BLE_HS_EOS;
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
-    os_callout_func_init(&ble_hs_heartbeat_timer, ble_hs_parent_evq,
-                         ble_hs_heartbeat, NULL);
     os_callout_func_init(&ble_hs_event_co, &ble_hs_evq,
                          ble_hs_event_handle, NULL);
 
     rc = os_mutex_init(&ble_hs_mutex);
-    if (rc != 0) {
-        rc = BLE_HS_EOS;
-        goto err;
-    }
-#if BLE_HS_DEBUG
+    SYSINIT_PANIC_ASSERT(rc == 0);
+
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     ble_hs_dbg_mutex_locked = 0;
 #endif
 
     /* Configure the HCI transport to communicate with a host. */
     ble_hci_trans_cfg_hs(ble_hs_hci_rx_evt, NULL, ble_hs_rx_data, NULL);
 
-    return 0;
-
-err:
-    ble_hs_free_mem();
-    return rc;
+    /* Configure storage mechanism. */
+    /* XXX */
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_hs_adv.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_adv.c b/net/nimble/host/src/ble_hs_adv.c
index da0d5c1..7bd7612 100644
--- a/net/nimble/host/src/ble_hs_adv.c
+++ b/net/nimble/host/src/ble_hs_adv.c
@@ -48,7 +48,7 @@ int
 ble_hs_adv_set_flat(uint8_t type, int data_len, const void *data,
                     uint8_t *dst, uint8_t *dst_len, uint8_t max_len)
 {
-#if !NIMBLE_OPT(ADVERTISE)
+#if !NIMBLE_BLE_ADVERTISE
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -118,7 +118,7 @@ int
 ble_hs_adv_set_fields(const struct ble_hs_adv_fields *adv_fields,
                       uint8_t *dst, uint8_t *dst_len, uint8_t max_len)
 {
-#if !NIMBLE_OPT(ADVERTISE)
+#if !NIMBLE_BLE_ADVERTISE
     return BLE_HS_ENOTSUP;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_hs_cfg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_cfg.c b/net/nimble/host/src/ble_hs_cfg.c
index fb5fb45..31da94f 100644
--- a/net/nimble/host/src/ble_hs_cfg.c
+++ b/net/nimble/host/src/ble_hs_cfg.c
@@ -17,64 +17,19 @@
  * under the License.
  */
 
-#include "ble_hs_priv.h"
+#include "syscfg/syscfg.h"
+#include "host/ble_hs.h"
 
-#if NIMBLE_OPT(CONNECT)
-#define BLE_HS_CFG_MAX_CONNECTIONS NIMBLE_OPT(MAX_CONNECTIONS)
-#else
-#define BLE_HS_CFG_MAX_CONNECTIONS 0
-#endif
-
-const struct ble_hs_cfg ble_hs_cfg_dflt = {
-    /** HCI settings. */
-    .max_hci_bufs = 3,
-
-    /** Connection settings. */
-    .max_connections = BLE_HS_CFG_MAX_CONNECTIONS,
-
-    /** GATT server settings. */
-    /* These get set to zero with the expectation that they will be increased
-     * as needed when each supported GATT service is initialized.
-     */
-    .max_services = 0,
-    .max_client_configs = 0,
-
-    /** GATT client settings. */
-    .max_gattc_procs = 4,
-
-    /** ATT server settings. */
-    /* This is set to 0; see note above re: GATT server settings. */
-    .max_attrs = 0,
-    .max_prep_entries = 6,
-
-    /** L2CAP settings. */
-    /* Three channels per connection (sig, att, and sm). */
-    .max_l2cap_chans = 3 * BLE_HS_CFG_MAX_CONNECTIONS,
-    .max_l2cap_sig_procs = 1,
-    .max_l2cap_sm_procs = 1,
+struct ble_hs_cfg ble_hs_cfg = {
+    .parent_evq = NULL,
 
     /** Security manager settings. */
-    .sm_io_cap = BLE_HS_IO_NO_INPUT_OUTPUT,
-    .sm_oob_data_flag = 0,
-    .sm_bonding = 0,
-    .sm_mitm = 0,
-    .sm_sc = 0,
-    .sm_keypress = 0,
-    .sm_our_key_dist = 0,
-    .sm_their_key_dist = 0,
-
-    /** Privacy settings. */
-    .rpa_timeout = 300,
+    .sm_io_cap = MYNEWT_VAL(BLE_SM_IO_CAP),
+    .sm_oob_data_flag = MYNEWT_VAL(BLE_SM_OOB_DATA_FLAG),
+    .sm_bonding = MYNEWT_VAL(BLE_SM_BONDING),
+    .sm_mitm = MYNEWT_VAL(BLE_SM_MITM),
+    .sm_sc = MYNEWT_VAL(BLE_SM_SC),
+    .sm_keypress = MYNEWT_VAL(BLE_SM_KEYPRESS),
+    .sm_our_key_dist = MYNEWT_VAL(BLE_SM_OUR_KEY_DIST),
+    .sm_their_key_dist = MYNEWT_VAL(BLE_SM_THEIR_KEY_DIST),
 };
-
-struct ble_hs_cfg ble_hs_cfg;
-
-void
-ble_hs_cfg_init(struct ble_hs_cfg *cfg)
-{
-    if (cfg == NULL) {
-        ble_hs_cfg = ble_hs_cfg_dflt;
-    } else {
-        ble_hs_cfg = *cfg;
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_hs_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_conn.c b/net/nimble/host/src/ble_hs_conn.c
index 76196ef..c0d08d0 100644
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@ -19,6 +19,7 @@
 
 #include <string.h>
 #include <errno.h>
+#include "syscfg/syscfg.h"
 #include "os/os.h"
 #include "host/ble_hs_id.h"
 #include "ble_hs_priv.h"
@@ -29,14 +30,17 @@
 static SLIST_HEAD(, ble_hs_conn) ble_hs_conns;
 static struct os_mempool ble_hs_conn_pool;
 
-static os_membuf_t *ble_hs_conn_elem_mem;
+static os_membuf_t ble_hs_conn_elem_mem[
+    OS_MEMPOOL_SIZE(MYNEWT_VAL(BLE_MAX_CONNECTIONS),
+                    sizeof (struct ble_hs_conn))
+];
 
 static const uint8_t ble_hs_conn_null_addr[6];
 
 int
 ble_hs_conn_can_alloc(void)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return 0;
 #endif
 
@@ -48,7 +52,7 @@ ble_hs_conn_can_alloc(void)
 struct ble_l2cap_chan *
 ble_hs_conn_chan_find(struct ble_hs_conn *conn, uint16_t cid)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return NULL;
 #endif
 
@@ -69,7 +73,7 @@ ble_hs_conn_chan_find(struct ble_hs_conn *conn, uint16_t cid)
 int
 ble_hs_conn_chan_insert(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return BLE_HS_ENOTSUP;
 #endif
 
@@ -100,7 +104,7 @@ ble_hs_conn_chan_insert(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 struct ble_hs_conn *
 ble_hs_conn_alloc(void)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return NULL;
 #endif
 
@@ -137,7 +141,7 @@ ble_hs_conn_alloc(void)
     /* XXX: We should create the SM channel even if not configured.  We need it
      * to reject SM messages.
      */
-#if NIMBLE_OPT(SM)
+#if NIMBLE_BLE_SM
     chan = ble_sm_create_chan();
     if (chan == NULL) {
         goto err;
@@ -176,7 +180,7 @@ ble_hs_conn_delete_chan(struct ble_hs_conn *conn, struct ble_l2cap_chan *chan)
 void
 ble_hs_conn_free(struct ble_hs_conn *conn)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return;
 #endif
 
@@ -202,7 +206,7 @@ ble_hs_conn_free(struct ble_hs_conn *conn)
 void
 ble_hs_conn_insert(struct ble_hs_conn *conn)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return;
 #endif
 
@@ -215,7 +219,7 @@ ble_hs_conn_insert(struct ble_hs_conn *conn)
 void
 ble_hs_conn_remove(struct ble_hs_conn *conn)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return;
 #endif
 
@@ -227,7 +231,7 @@ ble_hs_conn_remove(struct ble_hs_conn *conn)
 struct ble_hs_conn *
 ble_hs_conn_find(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return NULL;
 #endif
 
@@ -247,7 +251,7 @@ ble_hs_conn_find(uint16_t conn_handle)
 struct ble_hs_conn *
 ble_hs_conn_find_by_addr(uint8_t addr_type, uint8_t *addr)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return NULL;
 #endif
 
@@ -269,7 +273,7 @@ ble_hs_conn_find_by_addr(uint8_t addr_type, uint8_t *addr)
 struct ble_hs_conn *
 ble_hs_conn_find_by_idx(int idx)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return NULL;
 #endif
 
@@ -293,7 +297,7 @@ ble_hs_conn_find_by_idx(int idx)
 int
 ble_hs_conn_exists(uint16_t conn_handle)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return 0;
 #endif
     return ble_hs_conn_find(conn_handle) != NULL;
@@ -305,7 +309,7 @@ ble_hs_conn_exists(uint16_t conn_handle)
 struct ble_hs_conn *
 ble_hs_conn_first(void)
 {
-#if !NIMBLE_OPT(CONNECT)
+#if !NIMBLE_BLE_CONNECT
     return NULL;
 #endif
 
@@ -363,40 +367,19 @@ ble_hs_conn_addrs(const struct ble_hs_conn *conn,
     }
 }
 
-static void
-ble_hs_conn_free_mem(void)
-{
-    free(ble_hs_conn_elem_mem);
-    ble_hs_conn_elem_mem = NULL;
-}
-
 int 
 ble_hs_conn_init(void)
 {
     int rc;
 
-    ble_hs_conn_free_mem();
-
-    ble_hs_conn_elem_mem = malloc(
-        OS_MEMPOOL_BYTES(ble_hs_cfg.max_connections,
-                         sizeof (struct ble_hs_conn)));
-    if (ble_hs_conn_elem_mem == NULL) {
-        rc = BLE_HS_ENOMEM;
-        goto err;
-    }
-    rc = os_mempool_init(&ble_hs_conn_pool, ble_hs_cfg.max_connections,
+    rc = os_mempool_init(&ble_hs_conn_pool, MYNEWT_VAL(BLE_MAX_CONNECTIONS),
                          sizeof (struct ble_hs_conn),
                          ble_hs_conn_elem_mem, "ble_hs_conn_pool");
     if (rc != 0) {
-        rc = BLE_HS_EOS;
-        goto err;
+        return BLE_HS_EOS;
     }
 
     SLIST_INIT(&ble_hs_conns);
 
     return 0;
-
-err:
-    ble_hs_conn_free_mem();
-    return rc;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_hs_dbg.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_dbg.c b/net/nimble/host/src/ble_hs_dbg.c
index bb6ebdf..915090d 100644
--- a/net/nimble/host/src/ble_hs_dbg.c
+++ b/net/nimble/host/src/ble_hs_dbg.c
@@ -455,7 +455,7 @@ ble_hs_dbg_cmd_status_disp(uint8_t *evdata, uint8_t len)
 void
 ble_hs_dbg_event_disp(uint8_t *evbuf)
 {
-#if LOG_LEVEL > LOG_LEVEL_DEBUG
+#if MYNEWT_VAL(LOG_LEVEL) > LOG_LEVEL_DEBUG
     return;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_hs_hci.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci.c b/net/nimble/host/src/ble_hs_hci.c
index 433c2e0..2b2d275 100644
--- a/net/nimble/host/src/ble_hs_hci.c
+++ b/net/nimble/host/src/ble_hs_hci.c
@@ -34,11 +34,11 @@ static uint8_t *ble_hs_hci_ack;
 static uint16_t ble_hs_hci_buf_sz;
 static uint8_t ble_hs_hci_max_pkts;
 
-#if PHONY_HCI_ACKS
+#if MYNEWT_VAL(BLE_HS_PHONY_HCI_ACKS)
 static ble_hs_hci_phony_ack_fn *ble_hs_hci_phony_ack_cb;
 #endif
 
-#if PHONY_HCI_ACKS
+#if MYNEWT_VAL(BLE_HS_PHONY_HCI_ACKS)
 void
 ble_hs_hci_set_phony_ack_cb(ble_hs_hci_phony_ack_fn *cb)
 {
@@ -218,7 +218,7 @@ ble_hs_hci_wait_for_ack(void)
 {
     int rc;
 
-#if PHONY_HCI_ACKS
+#if MYNEWT_VAL(BLE_HS_PHONY_HCI_ACKS)
     if (ble_hs_hci_phony_ack_cb == NULL) {
         rc = BLE_HS_ETIMEOUT_HCI;
     } else {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_hs_hci_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_hci_priv.h b/net/nimble/host/src/ble_hs_hci_priv.h
index 7d2fb12..e9df8ea 100644
--- a/net/nimble/host/src/ble_hs_hci_priv.h
+++ b/net/nimble/host/src/ble_hs_hci_priv.h
@@ -38,7 +38,7 @@ int ble_hs_hci_cmd_tx_empty_ack(void *cmd);
 void ble_hs_hci_rx_ack(uint8_t *ack_ev);
 void ble_hs_hci_init(void);
 
-#if PHONY_HCI_ACKS
+#if MYNEWT_VAL(BLE_HS_PHONY_HCI_ACKS)
 typedef int ble_hs_hci_phony_ack_fn(uint8_t *ack, int ack_buf_len);
 void ble_hs_hci_set_phony_ack_cb(ble_hs_hci_phony_ack_fn *cb);
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_hs_misc.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_misc.c b/net/nimble/host/src/ble_hs_misc.c
index 216196b..74a661f 100644
--- a/net/nimble/host/src/ble_hs_misc.c
+++ b/net/nimble/host/src/ble_hs_misc.c
@@ -26,26 +26,6 @@
 const uint8_t ble_hs_misc_null_addr[6];
 
 int
-ble_hs_misc_malloc_mempool(void **mem, struct os_mempool *pool,
-                           int num_entries, int entry_size, char *name)
-{
-    int rc;
-
-    *mem = malloc(OS_MEMPOOL_BYTES(num_entries, entry_size));
-    if (*mem == NULL) {
-        return BLE_HS_ENOMEM;
-    }
-
-    rc = os_mempool_init(pool, num_entries, entry_size, *mem, name);
-    if (rc != 0) {
-        free(*mem);
-        return BLE_HS_EOS;
-    }
-
-    return 0;
-}
-
-int
 ble_hs_misc_conn_chan_find(uint16_t conn_handle, uint16_t cid,
                            struct ble_hs_conn **out_conn,
                            struct ble_l2cap_chan **out_chan)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_hs_priv.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_priv.h b/net/nimble/host/src/ble_hs_priv.h
index d61f0ba..1d77ae6 100644
--- a/net/nimble/host/src/ble_hs_priv.h
+++ b/net/nimble/host/src/ble_hs_priv.h
@@ -58,6 +58,12 @@ struct os_event;
 #define BLE_HS_SYNC_STATE_BRINGUP       1
 #define BLE_HS_SYNC_STATE_GOOD          2
 
+#if NIMBLE_BLE_CONNECT
+#define BLE_HS_MAX_CONNECTIONS MYNEWT_VAL(BLE_MAX_CONNECTIONS)
+#else
+#define BLE_HS_MAX_CONNECTIONS 0
+#endif
+
 STATS_SECT_START(ble_hs_stats)
     STATS_SECT_ENTRY(conn_create)
     STATS_SECT_ENTRY(conn_delete)
@@ -71,12 +77,15 @@ STATS_SECT_START(ble_hs_stats)
 STATS_SECT_END
 extern STATS_SECT_DECL(ble_hs_stats) ble_hs_stats;
 
-extern struct ble_hs_cfg ble_hs_cfg;
 extern struct os_mbuf_pool ble_hs_mbuf_pool;
 extern uint8_t ble_hs_sync_state;
 
 extern const uint8_t ble_hs_misc_null_addr[6];
 
+extern uint16_t ble_hs_max_attrs;
+extern uint16_t ble_hs_max_services;
+extern uint16_t ble_hs_max_client_configs;
+
 void ble_hs_process_tx_data_queue(void);
 void ble_hs_process_rx_data_queue(void);
 int ble_hs_tx_data(struct os_mbuf *om);
@@ -86,8 +95,6 @@ void ble_hs_event_enqueue(struct os_event *ev);
 int ble_hs_hci_rx_evt(uint8_t *hci_ev, void *arg);
 int ble_hs_hci_evt_acl_process(struct os_mbuf *om);
 
-int ble_hs_misc_malloc_mempool(void **mem, struct os_mempool *pool,
-                               int num_entries, int entry_size, char *name);
 int ble_hs_misc_conn_chan_find(uint16_t conn_handle, uint16_t cid,
                                struct ble_hs_conn **out_conn,
                                struct ble_l2cap_chan **out_chan);
@@ -96,8 +103,6 @@ int ble_hs_misc_conn_chan_find_reqd(uint16_t conn_handle, uint16_t cid,
                                     struct ble_l2cap_chan **out_chan);
 uint8_t ble_hs_misc_addr_type_to_id(uint8_t addr_type);
 
-void ble_hs_cfg_init(struct ble_hs_cfg *cfg);
-
 int ble_hs_locked_by_cur_task(void);
 int ble_hs_is_parent_task(void);
 void ble_hs_lock(void);
@@ -107,7 +112,7 @@ void ble_hs_hw_error(uint8_t hw_code);
 void ble_hs_heartbeat_sched(int32_t ticks);
 void ble_hs_notifications_sched(void);
 
-#if LOG_LEVEL <= LOG_LEVEL_DEBUG
+#if MYNEWT_VAL(LOG_LEVEL) <= LOG_LEVEL_DEBUG
 
 #define BLE_HS_LOG_CMD(is_tx, cmd_type, cmd_name, conn_handle,                \
                        log_cb, cmd) do                                        \
@@ -132,7 +137,7 @@ void ble_hs_notifications_sched(void);
 
 #endif
 
-#if BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
     #define BLE_HS_DBG_ASSERT(x) assert(x)
     #define BLE_HS_DBG_ASSERT_EVAL(x) assert(x)
 #else

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/ble_hs_pvcy.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_hs_pvcy.c b/net/nimble/host/src/ble_hs_pvcy.c
index ad3fa0c..e471f36 100644
--- a/net/nimble/host/src/ble_hs_pvcy.c
+++ b/net/nimble/host/src/ble_hs_pvcy.c
@@ -135,7 +135,7 @@ ble_hs_pvcy_ensure_started(void)
     }
 
     /* Set up the periodic change of our RPA. */
-    rc = ble_hs_pvcy_set_addr_timeout(ble_hs_cfg.rpa_timeout);
+    rc = ble_hs_pvcy_set_addr_timeout(MYNEWT_VAL(BLE_RPA_TIMEOUT));
     if (rc != 0) {
         return rc;
     }



[20/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_gatts_reg_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatts_reg_test.c b/net/nimble/host/src/test/ble_gatts_reg_test.c
deleted file mode 100644
index ad5f18f..0000000
--- a/net/nimble/host/src/test/ble_gatts_reg_test.c
+++ /dev/null
@@ -1,718 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "host/ble_uuid.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-
-#define BLE_GATTS_REG_TEST_MAX_ENTRIES  256
-
-struct ble_gatts_reg_test_entry {
-    uint8_t op;
-    uint8_t uuid128[16];
-    uint16_t handle;
-    uint16_t val_handle; /* If a characteristic. */
-
-    const struct ble_gatt_svc_def *svc;
-    const struct ble_gatt_chr_def *chr;
-    const struct ble_gatt_dsc_def *dsc;
-};
-
-static struct ble_gatts_reg_test_entry
-ble_gatts_reg_test_entries[BLE_GATTS_REG_TEST_MAX_ENTRIES];
-
-static int ble_gatts_reg_test_num_entries;
-
-static void
-ble_gatts_reg_test_init(void)
-{
-    ble_hs_test_util_init();
-    ble_gatts_reg_test_num_entries = 0;
-}
-
-static void
-ble_gatts_reg_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
-{
-    struct ble_gatts_reg_test_entry *entry;
-
-    TEST_ASSERT_FATAL(ble_gatts_reg_test_num_entries <
-                      BLE_GATTS_REG_TEST_MAX_ENTRIES);
-
-    entry = ble_gatts_reg_test_entries + ble_gatts_reg_test_num_entries++;
-    memset(entry, 0, sizeof *entry);
-
-    entry->op = ctxt->op;
-    switch (ctxt->op) {
-    case BLE_GATT_REGISTER_OP_SVC:
-        memcpy(entry->uuid128, ctxt->svc.svc_def->uuid128, 16);
-        entry->handle = ctxt->svc.handle;
-        entry->svc = ctxt->svc.svc_def;
-        break;
-
-    case BLE_GATT_REGISTER_OP_CHR:
-        memcpy(entry->uuid128, ctxt->chr.chr_def->uuid128, 16);
-        entry->handle = ctxt->chr.def_handle;
-        entry->val_handle = ctxt->chr.val_handle;
-        entry->svc = ctxt->chr.svc_def;
-        entry->chr = ctxt->chr.chr_def;
-        break;
-
-    case BLE_GATT_REGISTER_OP_DSC:
-        memcpy(entry->uuid128, ctxt->dsc.dsc_def->uuid128, 16);
-        entry->handle = ctxt->dsc.handle;
-        entry->svc = ctxt->dsc.svc_def;
-        entry->chr = ctxt->dsc.chr_def;
-        entry->dsc = ctxt->dsc.dsc_def;
-        break;
-
-    default:
-        TEST_ASSERT(0);
-        break;
-    }
-}
-
-static void
-ble_gatts_reg_test_misc_lookup_good(struct ble_gatts_reg_test_entry *entry)
-{
-    uint16_t chr_def_handle;
-    uint16_t chr_val_handle;
-    uint16_t svc_handle;
-    uint16_t dsc_handle;
-    int rc;
-
-    switch (entry->op) {
-    case BLE_GATT_REGISTER_OP_SVC:
-        rc = ble_gatts_find_svc(entry->uuid128, &svc_handle);
-        TEST_ASSERT_FATAL(rc == 0);
-        TEST_ASSERT(svc_handle == entry->handle);
-        break;
-
-    case BLE_GATT_REGISTER_OP_CHR:
-        rc = ble_gatts_find_chr(entry->svc->uuid128, entry->chr->uuid128,
-                                &chr_def_handle, &chr_val_handle);
-        TEST_ASSERT_FATAL(rc == 0);
-        TEST_ASSERT(chr_def_handle == entry->handle);
-        TEST_ASSERT(chr_val_handle == entry->val_handle);
-        break;
-
-    case BLE_GATT_REGISTER_OP_DSC:
-        rc = ble_gatts_find_dsc(entry->svc->uuid128, entry->chr->uuid128,
-                                entry->dsc->uuid128, &dsc_handle);
-        break;
-
-    default:
-        TEST_ASSERT(0);
-        break;
-    }
-}
-
-static void
-ble_gatts_reg_test_misc_lookup_bad(struct ble_gatts_reg_test_entry *entry)
-{
-    struct ble_gatts_reg_test_entry *cur;
-    uint8_t wrong_uuid[16];
-    int rc;
-    int i;
-
-    switch (entry->op) {
-    case BLE_GATT_REGISTER_OP_SVC:
-        /* Wrong service UUID. */
-        memcpy(wrong_uuid, entry->svc->uuid128, 16);
-        wrong_uuid[15]++;
-        rc = ble_gatts_find_svc(wrong_uuid, NULL);
-        TEST_ASSERT(rc == BLE_HS_ENOENT);
-        break;
-
-    case BLE_GATT_REGISTER_OP_CHR:
-        /* Correct service UUID, wrong characteristic UUID. */
-        memcpy(wrong_uuid, entry->chr->uuid128, 16);
-        wrong_uuid[15]++;
-        rc = ble_gatts_find_chr(entry->svc->uuid128, wrong_uuid, NULL, NULL);
-        TEST_ASSERT(rc == BLE_HS_ENOENT);
-
-        /* Incorrect service UUID, correct characteristic UUID. */
-        memcpy(wrong_uuid, entry->svc->uuid128, 16);
-        wrong_uuid[15]++;
-        rc = ble_gatts_find_chr(wrong_uuid, entry->chr->uuid128, NULL, NULL);
-        TEST_ASSERT(rc == BLE_HS_ENOENT);
-
-        /* Existing (but wrong) service, correct characteristic UUID. */
-        for (i = 0; i < ble_gatts_reg_test_num_entries; i++) {
-            cur = ble_gatts_reg_test_entries + i;
-            switch (cur->op) {
-            case BLE_GATT_REGISTER_OP_SVC:
-                if (cur->svc != entry->svc) {
-                    rc = ble_gatts_find_chr(cur->svc->uuid128,
-                                            entry->chr->uuid128,
-                                            NULL, NULL);
-                    TEST_ASSERT(rc == BLE_HS_ENOENT);
-                }
-                break;
-
-            case BLE_GATT_REGISTER_OP_CHR:
-                /* Characteristic that isn't in this service. */
-                if (cur->svc != entry->svc) {
-                    rc = ble_gatts_find_chr(entry->svc->uuid128,
-                                            cur->chr->uuid128,
-                                            NULL, NULL);
-                    TEST_ASSERT(rc == BLE_HS_ENOENT);
-                }
-                break;
-
-            case BLE_GATT_REGISTER_OP_DSC:
-                /* Use descriptor UUID instead of characteristic UUID. */
-                rc = ble_gatts_find_chr(entry->svc->uuid128,
-                                        cur->dsc->uuid128,
-                                        NULL, NULL);
-                TEST_ASSERT(rc == BLE_HS_ENOENT);
-                break;
-
-            default:
-                TEST_ASSERT(0);
-                break;
-            }
-        }
-        break;
-
-    case BLE_GATT_REGISTER_OP_DSC:
-        /* Correct svc/chr UUID, wrong dsc UUID. */
-        memcpy(wrong_uuid, entry->dsc->uuid128, 16);
-        wrong_uuid[15]++;
-        rc = ble_gatts_find_dsc(entry->svc->uuid128, entry->chr->uuid128,
-                                wrong_uuid, NULL);
-        TEST_ASSERT(rc == BLE_HS_ENOENT);
-
-        /* Incorrect svc UUID, correct chr/dsc UUID. */
-        memcpy(wrong_uuid, entry->svc->uuid128, 16);
-        wrong_uuid[15]++;
-        rc = ble_gatts_find_dsc(wrong_uuid, entry->chr->uuid128,
-                                entry->dsc->uuid128, NULL);
-        TEST_ASSERT(rc == BLE_HS_ENOENT);
-
-        for (i = 0; i < ble_gatts_reg_test_num_entries; i++) {
-            cur = ble_gatts_reg_test_entries + i;
-            switch (cur->op) {
-            case BLE_GATT_REGISTER_OP_SVC:
-                /* Existing (but wrong) svc, correct chr/dsc UUID. */
-                if (cur->svc != entry->svc) {
-                    rc = ble_gatts_find_dsc(cur->svc->uuid128,
-                                            entry->chr->uuid128,
-                                            entry->dsc->uuid128,
-                                            NULL);
-                    TEST_ASSERT(rc == BLE_HS_ENOENT);
-                }
-                break;
-
-            case BLE_GATT_REGISTER_OP_CHR:
-                /* Existing (but wrong) svc/chr, correct dsc UUID. */
-                if (cur->chr != entry->chr) {
-                    rc = ble_gatts_find_dsc(cur->svc->uuid128,
-                                            cur->chr->uuid128,
-                                            entry->dsc->uuid128,
-                                            NULL);
-                    TEST_ASSERT(rc == BLE_HS_ENOENT);
-                }
-                break;
-
-            case BLE_GATT_REGISTER_OP_DSC:
-                /* Descriptor that isn't in this characteristic. */
-                if (cur->chr != entry->chr) {
-                    rc = ble_gatts_find_dsc(cur->svc->uuid128,
-                                            cur->chr->uuid128,
-                                            entry->dsc->uuid128,
-                                            NULL);
-                    TEST_ASSERT(rc == BLE_HS_ENOENT);
-                }
-                break;
-
-            default:
-                TEST_ASSERT(0);
-                break;
-            }
-        }
-        break;
-
-    default:
-        TEST_ASSERT(0);
-        break;
-    }
-}
-
-static void
-ble_gatts_reg_test_misc_verify_entry(uint8_t op, const uint8_t *uuid128)
-{
-    struct ble_gatts_reg_test_entry *entry;
-    int i;
-
-    for (i = 0; i < ble_gatts_reg_test_num_entries; i++) {
-        entry = ble_gatts_reg_test_entries + i;
-        if (entry->op == op && memcmp(entry->uuid128, uuid128, 16) == 0) {
-            break;
-        }
-    }
-    TEST_ASSERT_FATAL(entry != NULL);
-
-    /* Verify that characteristic value handle was properly assigned at
-     * registration.
-     */
-    if (op == BLE_GATT_REGISTER_OP_CHR) {
-        TEST_ASSERT(*entry->chr->val_handle == entry->val_handle);
-    }
-
-    /* Verify that the entry can be looked up. */
-    ble_gatts_reg_test_misc_lookup_good(entry);
-
-    /* Verify that "barely incorrect" UUID information doesn't retrieve any
-     * handles.
-     */
-    ble_gatts_reg_test_misc_lookup_bad(entry);
-}
-
-static int
-ble_gatts_reg_test_misc_dummy_access(uint16_t conn_handle,
-                                     uint16_t attr_handle,
-                                     struct ble_gatt_access_ctxt *ctxt,
-                                     void *arg)
-{
-    return 0;
-}
-
-TEST_CASE(ble_gatts_reg_test_svc_return)
-{
-    int rc;
-
-    /*** Missing UUID. */
-    ble_gatts_reg_test_init();
-    struct ble_gatt_svc_def svcs_no_uuid[] = { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-    }, {
-        0
-    } };
-
-    rc = ble_gatts_register_svcs(svcs_no_uuid, NULL, NULL);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-
-    /*** Circular dependency. */
-    ble_gatts_reg_test_init();
-    struct ble_gatt_svc_def svcs_circ[] = { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-        .includes = (const struct ble_gatt_svc_def*[]) { svcs_circ + 1, NULL },
-    }, {
-        .type = BLE_GATT_SVC_TYPE_SECONDARY,
-        .uuid128 = BLE_UUID16(0x1234),
-        .includes = (const struct ble_gatt_svc_def*[]) { svcs_circ + 0, NULL },
-    }, {
-        0
-    } };
-
-    rc = ble_gatts_register_svcs(svcs_circ, NULL, NULL);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-
-    /*** Success. */
-    ble_gatts_reg_test_init();
-    struct ble_gatt_svc_def svcs_good[] = { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-        .includes = (const struct ble_gatt_svc_def*[]) { svcs_good + 1, NULL },
-    }, {
-        .type = BLE_GATT_SVC_TYPE_SECONDARY,
-        .uuid128 = BLE_UUID16(0x1234),
-    }, {
-        0
-    } };
-
-    rc = ble_gatts_register_svcs(svcs_good, NULL, NULL);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ble_gatts_reg_test_chr_return)
-{
-    int rc;
-
-    /*** Missing callback. */
-    ble_gatts_reg_test_init();
-    struct ble_gatt_svc_def svcs_no_chr_cb[] = { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(0x1111),
-            .flags = BLE_GATT_CHR_F_READ,
-        }, {
-            0
-        } },
-    }, {
-        0
-    } };
-
-    rc = ble_gatts_register_svcs(svcs_no_chr_cb, NULL, NULL);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-
-    /*** Success. */
-    ble_gatts_reg_test_init();
-    struct ble_gatt_svc_def svcs_good[] = { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(0x1111),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_READ,
-        }, {
-            0
-        } },
-    }, {
-        0
-    } };
-
-    rc = ble_gatts_register_svcs(svcs_good, NULL, NULL);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ble_gatts_reg_test_dsc_return)
-{
-    int rc;
-
-    /*** Missing callback. */
-    ble_gatts_reg_test_init();
-    struct ble_gatt_svc_def svcs_no_dsc_cb[] = { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(0x1111),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_READ,
-            .descriptors = (struct ble_gatt_dsc_def[]) { {
-                .uuid128 = BLE_UUID16(0x8888),
-                .att_flags = 5,
-            }, {
-                0
-            } },
-        }, {
-            0
-        } },
-    }, {
-        0
-    } };
-
-    rc = ble_gatts_register_svcs(svcs_no_dsc_cb, NULL, NULL);
-    TEST_ASSERT(rc == BLE_HS_EINVAL);
-
-    /*** Success. */
-    ble_gatts_reg_test_init();
-    struct ble_gatt_svc_def svcs_good[] = { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(0x1111),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_READ,
-            .descriptors = (struct ble_gatt_dsc_def[]) { {
-                .uuid128 = BLE_UUID16(0x8888),
-                .access_cb = ble_gatts_reg_test_misc_dummy_access,
-                .att_flags = 5,
-            }, {
-                0
-            } },
-        }, {
-            0
-        } },
-    }, {
-        0
-    } };
-
-    rc = ble_gatts_register_svcs(svcs_good, NULL, NULL);
-    TEST_ASSERT(rc == 0);
-}
-
-static void
-ble_gatts_reg_test_misc_svcs(struct ble_gatt_svc_def *svcs)
-{
-    const struct ble_gatt_svc_def *svc;
-    const struct ble_gatt_chr_def *chr;
-    const struct ble_gatt_dsc_def *dsc;
-    int rc;
-
-    ble_gatts_reg_test_init();
-
-    /* Register all the attributes. */
-    rc = ble_gatts_register_svcs(svcs, ble_gatts_reg_test_misc_reg_cb,
-                                 NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Verify that the appropriate callbacks were executed. */
-    for (svc = svcs; svc->type != BLE_GATT_SVC_TYPE_END; svc++) {
-        ble_gatts_reg_test_misc_verify_entry(BLE_GATT_REGISTER_OP_SVC,
-                                             svc->uuid128);
-
-        if (svc->characteristics != NULL) {
-            for (chr = svc->characteristics; chr->uuid128 != NULL; chr++) {
-                ble_gatts_reg_test_misc_verify_entry(BLE_GATT_REGISTER_OP_CHR,
-                                                     chr->uuid128);
-
-                if (chr->descriptors != NULL) {
-                    for (dsc = chr->descriptors; dsc->uuid128 != NULL; dsc++) {
-                        ble_gatts_reg_test_misc_verify_entry(
-                            BLE_GATT_REGISTER_OP_DSC, dsc->uuid128);
-                    }
-                }
-            }
-        }
-    }
-}
-
-TEST_CASE(ble_gatts_reg_test_svc_cb)
-{
-    /*** 1 primary. */
-    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-    }, {
-        0
-    } });
-
-    /*** 3 primary. */
-    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-    }, {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x2234),
-    }, {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x3234),
-    }, {
-        0
-    } });
-
-
-    /*** 1 primary, 1 secondary. */
-    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-    }, {
-        .type = BLE_GATT_SVC_TYPE_SECONDARY,
-        .uuid128 = BLE_UUID16(0x2222),
-    }, {
-        0
-    } });
-
-    /*** 1 primary, 1 secondary, 1 include. */
-    struct ble_gatt_svc_def svcs[] = {
-        [0] = {
-            .type = BLE_GATT_SVC_TYPE_PRIMARY,
-            .uuid128 = BLE_UUID16(0x1234),
-            .includes = (const struct ble_gatt_svc_def*[]) { svcs + 1, NULL, },
-        },
-        [1] = {
-            .type = BLE_GATT_SVC_TYPE_SECONDARY,
-            .uuid128 = BLE_UUID16(0x2222),
-        }, {
-            0
-        }
-    };
-    ble_gatts_reg_test_misc_svcs(svcs);
-}
-
-TEST_CASE(ble_gatts_reg_test_chr_cb)
-{
-    uint16_t val_handles[16];
-
-    /*** 1 characteristic. */
-    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(0x1111),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_READ,
-            .val_handle = val_handles + 0,
-        }, {
-            0
-        } },
-    }, {
-        0
-    } });
-
-    /*** 3 characteristics. */
-    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(0x1111),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_READ,
-            .val_handle = val_handles + 0,
-        }, {
-            .uuid128 = BLE_UUID16(0x2222),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_WRITE,
-            .val_handle = val_handles + 1,
-        }, {
-            0
-        } },
-    }, {
-        .type = BLE_GATT_SVC_TYPE_SECONDARY,
-        .uuid128 = BLE_UUID16(0x5678),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(0x3333),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_READ,
-            .val_handle = val_handles + 2,
-        }, {
-            0
-        } },
-    }, {
-        0
-    } });
-}
-
-TEST_CASE(ble_gatts_reg_test_dsc_cb)
-{
-    uint16_t val_handles[16];
-
-    /*** 1 descriptor. */
-    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(0x1111),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_READ,
-            .val_handle = val_handles + 0,
-            .descriptors = (struct ble_gatt_dsc_def[]) { {
-                .uuid128 = BLE_UUID16(0x111a),
-                .att_flags = 5,
-                .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            }, {
-                0
-            } },
-        }, {
-            0
-        } },
-    }, {
-        0
-    } });
-
-    /*** 5+ descriptors. */
-    ble_gatts_reg_test_misc_svcs((struct ble_gatt_svc_def[]) { {
-        .type = BLE_GATT_SVC_TYPE_PRIMARY,
-        .uuid128 = BLE_UUID16(0x1234),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(0x1111),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_READ,
-            .val_handle = val_handles + 0,
-            .descriptors = (struct ble_gatt_dsc_def[]) { {
-                .uuid128 = BLE_UUID16(0x111a),
-                .att_flags = 5,
-                .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            }, {
-                0
-            } },
-        }, {
-            .uuid128 = BLE_UUID16(0x2222),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_WRITE,
-            .val_handle = val_handles + 1,
-        }, {
-            0
-        } },
-    }, {
-        .type = BLE_GATT_SVC_TYPE_SECONDARY,
-        .uuid128 = BLE_UUID16(0x5678),
-        .characteristics = (struct ble_gatt_chr_def[]) { {
-            .uuid128 = BLE_UUID16(0x3333),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_READ,
-            .val_handle = val_handles + 2,
-            .descriptors = (struct ble_gatt_dsc_def[]) { {
-                .uuid128 = BLE_UUID16(0x333a),
-                .att_flags = 5,
-                .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            }, {
-                .uuid128 = BLE_UUID16(0x333b),
-                .att_flags = 5,
-                .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            }, {
-                .uuid128 = BLE_UUID16(0x333c),
-                .att_flags = 5,
-                .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            }, {
-                .uuid128 = BLE_UUID16(0x333e),
-                .att_flags = 5,
-                .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            }, {
-                0
-            } },
-        }, {
-            .uuid128 = BLE_UUID16(0x4444),
-            .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            .flags = BLE_GATT_CHR_F_READ,
-            .val_handle = val_handles + 3,
-            .descriptors = (struct ble_gatt_dsc_def[]) { {
-                .uuid128 = BLE_UUID16(0x444a),
-                .att_flags = 5,
-                .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            }, {
-                .uuid128 = BLE_UUID16(0x444b),
-                .att_flags = 5,
-                .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            }, {
-                .uuid128 = BLE_UUID16(0x444c),
-                .att_flags = 5,
-                .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            }, {
-                .uuid128 = BLE_UUID16(0x444e),
-                .att_flags = 5,
-                .access_cb = ble_gatts_reg_test_misc_dummy_access,
-            }, {
-                0
-            } },
-        }, {
-            0
-        } },
-    }, {
-        0
-    } });
-}
-
-TEST_SUITE(ble_gatts_reg_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gatts_reg_test_svc_return();
-    ble_gatts_reg_test_chr_return();
-    ble_gatts_reg_test_dsc_return();
-
-    ble_gatts_reg_test_svc_cb();
-    ble_gatts_reg_test_chr_cb();
-    ble_gatts_reg_test_dsc_cb();
-}
-
-int
-ble_gatts_reg_test_all(void)
-{
-    ble_gatts_reg_suite();
-
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_hs_adv_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_adv_test.c b/net/nimble/host/src/test/ble_hs_adv_test.c
deleted file mode 100644
index e99fd5b..0000000
--- a/net/nimble/host/src/test/ble_hs_adv_test.c
+++ /dev/null
@@ -1,1486 +0,0 @@
-/**
- * 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 <stddef.h>
-#include <errno.h>
-#include <string.h>
-#include "testutil/testutil.h"
-#include "nimble/hci_common.h"
-#include "host/ble_hs_adv.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-
-#define BLE_ADV_TEST_DATA_OFF   4
-
-static void
-ble_hs_adv_test_misc_verify_tx_adv_data_hdr(uint8_t *cmd, int data_len)
-{
-    uint16_t opcode;
-
-    opcode = le16toh(cmd + 0);
-    TEST_ASSERT(BLE_HCI_OGF(opcode) == BLE_HCI_OGF_LE);
-    TEST_ASSERT(BLE_HCI_OCF(opcode) == BLE_HCI_OCF_LE_SET_ADV_DATA);
-
-    TEST_ASSERT(cmd[2] == BLE_HCI_SET_ADV_DATA_LEN);
-    TEST_ASSERT(cmd[3] == data_len);
-}
-
-static void
-ble_hs_adv_test_misc_verify_tx_rsp_data_hdr(uint8_t *cmd, int data_len)
-{
-    uint16_t opcode;
-
-    opcode = le16toh(cmd + 0);
-    TEST_ASSERT(BLE_HCI_OGF(opcode) == BLE_HCI_OGF_LE);
-    TEST_ASSERT(BLE_HCI_OCF(opcode) == BLE_HCI_OCF_LE_SET_SCAN_RSP_DATA);
-
-    TEST_ASSERT(cmd[2] == BLE_HCI_SET_SCAN_RSP_DATA_LEN);
-    TEST_ASSERT(cmd[3] == data_len);
-}
-
-static void
-ble_hs_adv_test_misc_verify_tx_field(uint8_t *cmd, uint8_t type,
-                                     uint8_t val_len, void *val)
-{
-    TEST_ASSERT(cmd[0] == val_len + 1);
-    TEST_ASSERT(cmd[1] == type);
-    TEST_ASSERT(memcmp(cmd + 2, val, val_len) == 0);
-}
-
-struct ble_hs_adv_test_field {
-    uint8_t type;       /* 0 indicates end of array. */
-    uint8_t *val;
-    uint8_t val_len;
-};
-
-static int
-ble_hs_adv_test_misc_calc_data_len(struct ble_hs_adv_test_field *fields)
-{
-    struct ble_hs_adv_test_field *field;
-    int len;
-
-    len = 0;
-    if (fields != NULL) {
-        for (field = fields; field->type != 0; field++) {
-            len += 2 + field->val_len;
-        }
-    }
-
-    return len;
-}
-
-static void
-ble_hs_adv_test_misc_verify_tx_fields(uint8_t *cmd,
-                                      struct ble_hs_adv_test_field *fields)
-{
-    struct ble_hs_adv_test_field *field;
-
-    for (field = fields; field->type != 0; field++) {
-        ble_hs_adv_test_misc_verify_tx_field(cmd, field->type, field->val_len,
-                                             field->val);
-        cmd += 2 + field->val_len;
-    }
-}
-
-static void
-ble_hs_adv_test_misc_verify_tx_adv_data(struct ble_hs_adv_test_field *fields)
-{
-    int data_len;
-    uint8_t *cmd;
-
-    cmd = ble_hs_test_util_get_last_hci_tx();
-    TEST_ASSERT_FATAL(cmd != NULL);
-
-    data_len = ble_hs_adv_test_misc_calc_data_len(fields);
-    ble_hs_adv_test_misc_verify_tx_adv_data_hdr(cmd, data_len);
-    if (fields != NULL) {
-        ble_hs_adv_test_misc_verify_tx_fields(cmd + BLE_ADV_TEST_DATA_OFF,
-                                              fields);
-    }
-}
-
-static void
-ble_hs_adv_test_misc_verify_tx_rsp_data(struct ble_hs_adv_test_field *fields)
-{
-    int data_len;
-    uint8_t *cmd;
-
-    cmd = ble_hs_test_util_get_last_hci_tx();
-    TEST_ASSERT_FATAL(cmd != NULL);
-
-    data_len = ble_hs_adv_test_misc_calc_data_len(fields);
-    ble_hs_adv_test_misc_verify_tx_rsp_data_hdr(cmd, data_len);
-    if (fields != NULL) {
-        ble_hs_adv_test_misc_verify_tx_fields(cmd + BLE_ADV_TEST_DATA_OFF,
-                                              fields);
-    }
-}
-
-static void
-ble_hs_adv_test_misc_tx_and_verify_data(
-    uint8_t disc_mode,
-    struct ble_hs_adv_fields *adv_fields,
-    struct ble_hs_adv_test_field *test_adv_fields,
-    struct ble_hs_adv_fields *rsp_fields,
-    struct ble_hs_adv_test_field *test_rsp_fields)
-{
-    struct ble_gap_adv_params adv_params;
-    int rc;
-
-    ble_hs_test_util_init();
-
-    adv_params = ble_hs_test_util_adv_params;
-    adv_params.disc_mode = disc_mode;
-
-    rc = ble_hs_test_util_adv_set_fields(adv_fields, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    rc = ble_gap_adv_rsp_set_fields(rsp_fields);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, 0, NULL, &adv_params,
-                                    NULL, NULL, 0, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Discard the adv-enable command. */
-    ble_hs_test_util_get_last_hci_tx();
-
-    ble_hs_adv_test_misc_verify_tx_rsp_data(test_rsp_fields);
-    ble_hs_adv_test_misc_verify_tx_adv_data(test_adv_fields);
-
-    /* Ensure the same data gets sent on repeated advertise procedures. */
-    rc = ble_hs_test_util_adv_stop(0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, 0, NULL, &adv_params,
-                                    NULL, NULL, 0, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Discard the adv-enable command. */
-    ble_hs_test_util_get_last_hci_tx();
-
-    ble_hs_adv_test_misc_verify_tx_rsp_data(test_rsp_fields);
-    ble_hs_adv_test_misc_verify_tx_adv_data(test_adv_fields);
-}
-
-TEST_CASE(ble_hs_adv_test_case_flags)
-{
-    struct ble_hs_adv_fields adv_fields;
-    struct ble_hs_adv_fields rsp_fields;
-
-    memset(&adv_fields, 0, sizeof adv_fields);
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-
-    /* Default flags. */
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON,
-        &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /* Flags |= limited discoverable. */
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_LTD, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]) {
-                    BLE_HS_ADV_F_DISC_LTD | BLE_HS_ADV_F_BREDR_UNSUP
-                 },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /* Flags = general discoverable. */
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_GEN, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]) {
-                    BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP
-                 },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-}
-
-TEST_CASE(ble_hs_adv_test_case_user)
-{
-    struct ble_hs_adv_fields adv_fields;
-    struct ble_hs_adv_fields rsp_fields;
-
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-
-    /*** Complete 16-bit service class UUIDs. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
-    adv_fields.uuids16 = (uint16_t[]) { 0x0001, 0x1234, 0x54ab };
-    adv_fields.num_uuids16 = 3;
-    adv_fields.uuids16_is_complete = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_COMP_UUIDS16,
-                .val = (uint8_t[]) { 0x01, 0x00, 0x34, 0x12, 0xab, 0x54 },
-                .val_len = 6,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** Incomplete 16-bit service class UUIDs. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.uuids16 = (uint16_t[]) { 0x0001, 0x1234, 0x54ab };
-    adv_fields.num_uuids16 = 3;
-    adv_fields.uuids16_is_complete = 0;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS16,
-                .val = (uint8_t[]) { 0x01, 0x00, 0x34, 0x12, 0xab, 0x54 },
-                .val_len = 6,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** Complete 32-bit service class UUIDs. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.uuids32 = (uint32_t[]) { 0x12345678, 0xabacadae };
-    adv_fields.num_uuids32 = 2;
-    adv_fields.uuids32_is_complete = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_COMP_UUIDS32,
-                .val = (uint8_t[]) { 0x78,0x56,0x34,0x12,0xae,0xad,0xac,0xab },
-                .val_len = 8,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** Incomplete 32-bit service class UUIDs. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.uuids32 = (uint32_t[]) { 0x12345678, 0xabacadae };
-    adv_fields.num_uuids32 = 2;
-    adv_fields.uuids32_is_complete = 0;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS32,
-                .val = (uint8_t[]) { 0x78,0x56,0x34,0x12,0xae,0xad,0xac,0xab },
-                .val_len = 8,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** Complete 128-bit service class UUIDs. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.uuids128 = (uint8_t[]) {
-        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
-        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
-    };
-    adv_fields.num_uuids128 = 1;
-    adv_fields.uuids128_is_complete = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_COMP_UUIDS128,
-                .val = (uint8_t[]) {
-                    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
-                    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
-                },
-                .val_len = 16,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** Incomplete 128-bit service class UUIDs. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.uuids128 = (uint8_t[]) {
-        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
-        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
-    };
-    adv_fields.num_uuids128 = 1;
-    adv_fields.uuids128_is_complete = 0;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS128,
-                .val = (uint8_t[]) {
-                    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
-                    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
-                },
-                .val_len = 16,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** Complete name. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.name = (uint8_t *)"myname";
-    adv_fields.name_len = 6;
-    adv_fields.name_is_complete = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_COMP_NAME,
-                .val = (uint8_t*)"myname",
-                .val_len = 6,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** Incomplete name. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.name = (uint8_t *)"myname";
-    adv_fields.name_len = 6;
-    adv_fields.name_is_complete = 0;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_INCOMP_NAME,
-                .val = (uint8_t*)"myname",
-                .val_len = 6,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** Class of device. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.device_class = (uint8_t[]){ 1,2,3 };
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_DEVICE_CLASS,
-                .val = (uint8_t[]) { 1,2,3 },
-                .val_len = BLE_HS_ADV_DEVICE_CLASS_LEN,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** Slave interval range. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.slave_itvl_range = (uint8_t[]){ 1,2,3,4 };
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_SLAVE_ITVL_RANGE,
-                .val = (uint8_t[]) { 1,2,3,4 },
-                .val_len = BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** 0x16 - Service data - 16-bit UUID. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.svc_data_uuid16 = (uint8_t[]){ 1,2,3,4 };
-    adv_fields.svc_data_uuid16_len = 4;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID16,
-                .val = (uint8_t[]) { 1,2,3,4 },
-                .val_len = 4,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** 0x17 - Public target address. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.public_tgt_addr = (uint8_t[]){ 1,2,3,4,5,6, 6,5,4,3,2,1 };
-    adv_fields.num_public_tgt_addrs = 2;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_PUBLIC_TGT_ADDR,
-                .val = (uint8_t[]){ 1,2,3,4,5,6, 6,5,4,3,2,1 },
-                .val_len = 2 * BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** 0x19 - Appearance. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.appearance = 0x1234;
-    adv_fields.appearance_is_present = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_APPEARANCE,
-                .val = (uint8_t[]){ 0x34, 0x12 },
-                .val_len = BLE_HS_ADV_APPEARANCE_LEN,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** 0x1a - Advertising interval. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.adv_itvl = 0x1234;
-    adv_fields.adv_itvl_is_present = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_ADV_ITVL,
-                .val = (uint8_t[]){ 0x34, 0x12 },
-                .val_len = BLE_HS_ADV_ADV_ITVL_LEN,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** 0x1b - LE bluetooth device address. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.le_addr = (uint8_t[]){ 1,2,3,4,5,6,7 };
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_LE_ADDR,
-                .val = (uint8_t[]) { 1,2,3,4,5,6,7 },
-                .val_len = BLE_HS_ADV_LE_ADDR_LEN,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** 0x1c - LE role. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.le_role = BLE_HS_ADV_LE_ROLE_BOTH_PERIPH_PREF;
-    adv_fields.le_role_is_present = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_LE_ROLE,
-                .val = (uint8_t[]) { BLE_HS_ADV_LE_ROLE_BOTH_PERIPH_PREF },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** 0x20 - Service data - 32-bit UUID. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.svc_data_uuid32 = (uint8_t[]){ 1,2,3,4,5 };
-    adv_fields.svc_data_uuid32_len = 5;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID32,
-                .val = (uint8_t[]) { 1,2,3,4,5 },
-                .val_len = 5,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** 0x21 - Service data - 128-bit UUID. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.svc_data_uuid128 =
-        (uint8_t[]){ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 };
-    adv_fields.svc_data_uuid128_len = 18;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID128,
-                .val = (uint8_t[]){ 1,2,3,4,5,6,7,8,9,10,
-                                    11,12,13,14,15,16,17,18 },
-                .val_len = 18,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** 0x24 - URI. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.uri = (uint8_t[]){ 1,2,3,4 };
-    adv_fields.uri_len = 4;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_URI,
-                .val = (uint8_t[]) { 1,2,3,4 },
-                .val_len = 4,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** 0xff - Manufacturer specific data. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    adv_fields.mfg_data = (uint8_t[]){ 1,2,3,4 };
-    adv_fields.mfg_data_len = 4;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_MFG_DATA,
-                .val = (uint8_t[]) { 1,2,3,4 },
-                .val_len = 4,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-}
-
-TEST_CASE(ble_hs_adv_test_case_user_rsp)
-{
-    struct ble_hs_adv_fields rsp_fields;
-    struct ble_hs_adv_fields adv_fields;
-
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.flags_is_present = 1;
-    adv_fields.tx_pwr_lvl_is_present = 1;
-
-    /*** Complete 16-bit service class UUIDs. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.uuids16 = (uint16_t[]) { 0x0001, 0x1234, 0x54ab };
-    rsp_fields.num_uuids16 = 3;
-    rsp_fields.uuids16_is_complete = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_COMP_UUIDS16,
-                .val = (uint8_t[]) { 0x01, 0x00, 0x34, 0x12, 0xab, 0x54 },
-                .val_len = 6,
-            },
-            { 0 },
-        });
-
-    /*** Incomplete 16-bit service class UUIDs. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.uuids16 = (uint16_t[]) { 0x0001, 0x1234, 0x54ab };
-    rsp_fields.num_uuids16 = 3;
-    rsp_fields.uuids16_is_complete = 0;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS16,
-                .val = (uint8_t[]) { 0x01, 0x00, 0x34, 0x12, 0xab, 0x54 },
-                .val_len = 6,
-            },
-            { 0 },
-        });
-
-    /*** Complete 32-bit service class UUIDs. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.uuids32 = (uint32_t[]) { 0x12345678, 0xabacadae };
-    rsp_fields.num_uuids32 = 2;
-    rsp_fields.uuids32_is_complete = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_COMP_UUIDS32,
-                .val = (uint8_t[]) { 0x78,0x56,0x34,0x12,0xae,0xad,0xac,0xab },
-                .val_len = 8,
-            },
-            { 0 },
-        });
-
-    /*** Incomplete 32-bit service class UUIDs. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.uuids32 = (uint32_t[]) { 0x12345678, 0xabacadae };
-    rsp_fields.num_uuids32 = 2;
-    rsp_fields.uuids32_is_complete = 0;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS32,
-                .val = (uint8_t[]) { 0x78,0x56,0x34,0x12,0xae,0xad,0xac,0xab },
-                .val_len = 8,
-            },
-            { 0 },
-        });
-
-    /*** Complete 128-bit service class UUIDs. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.uuids128 = (uint8_t[]) {
-        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
-        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
-    };
-    rsp_fields.num_uuids128 = 1;
-    rsp_fields.uuids128_is_complete = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_COMP_UUIDS128,
-                .val = (uint8_t[]) {
-                    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
-                    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
-                },
-                .val_len = 16,
-            },
-            { 0 },
-        });
-
-    /*** Incomplete 128-bit service class UUIDs. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.uuids128 = (uint8_t[]) {
-        0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
-        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
-    };
-    rsp_fields.num_uuids128 = 1;
-    rsp_fields.uuids128_is_complete = 0;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_INCOMP_UUIDS128,
-                .val = (uint8_t[]) {
-                    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
-                    0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
-                },
-                .val_len = 16,
-            },
-            { 0 },
-        });
-
-    /*** Complete name. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.name = (uint8_t *)"myname";
-    rsp_fields.name_len = 6;
-    rsp_fields.name_is_complete = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_COMP_NAME,
-                .val = (uint8_t*)"myname",
-                .val_len = 6,
-            },
-            { 0 },
-        });
-
-    /*** Incomplete name. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.name = (uint8_t *)"myname";
-    rsp_fields.name_len = 6;
-    rsp_fields.name_is_complete = 0;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_INCOMP_NAME,
-                .val = (uint8_t*)"myname",
-                .val_len = 6,
-            },
-            { 0 },
-        });
-
-    /*** Class of device. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.device_class = (uint8_t[]){ 1,2,3 };
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_DEVICE_CLASS,
-                .val = (uint8_t[]) { 1,2,3 },
-                .val_len = BLE_HS_ADV_DEVICE_CLASS_LEN,
-            },
-            { 0 },
-        });
-
-    /*** Slave interval range. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.slave_itvl_range = (uint8_t[]){ 1,2,3,4 };
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_SLAVE_ITVL_RANGE,
-                .val = (uint8_t[]) { 1,2,3,4 },
-                .val_len = BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN,
-            },
-            { 0 },
-        });
-
-    /*** 0x16 - Service data - 16-bit UUID. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.svc_data_uuid16 = (uint8_t[]){ 1,2,3,4 };
-    rsp_fields.svc_data_uuid16_len = 4;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID16,
-                .val = (uint8_t[]) { 1,2,3,4 },
-                .val_len = 4,
-            },
-            { 0 },
-        });
-
-    /*** 0x17 - Public target address. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.public_tgt_addr = (uint8_t[]){ 1,2,3,4,5,6, 6,5,4,3,2,1 };
-    rsp_fields.num_public_tgt_addrs = 2;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_PUBLIC_TGT_ADDR,
-                .val = (uint8_t[]){ 1,2,3,4,5,6, 6,5,4,3,2,1 },
-                .val_len = 2 * BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN,
-            },
-            { 0 },
-        });
-
-    /*** 0x19 - Appearance. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.appearance = 0x1234;
-    rsp_fields.appearance_is_present = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_APPEARANCE,
-                .val = (uint8_t[]){ 0x34, 0x12 },
-                .val_len = BLE_HS_ADV_APPEARANCE_LEN,
-            },
-            { 0 },
-        });
-
-    /*** 0x1a - Advertising interval. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.adv_itvl = 0x1234;
-    rsp_fields.adv_itvl_is_present = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_ADV_ITVL,
-                .val = (uint8_t[]){ 0x34, 0x12 },
-                .val_len = BLE_HS_ADV_ADV_ITVL_LEN,
-            },
-            { 0 },
-        });
-
-    /*** 0x1b - LE bluetooth device address. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.le_addr = (uint8_t[]){ 1,2,3,4,5,6,7 };
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_LE_ADDR,
-                .val = (uint8_t[]) { 1,2,3,4,5,6,7 },
-                .val_len = BLE_HS_ADV_LE_ADDR_LEN,
-            },
-            { 0 },
-        });
-
-    /*** 0x1c - LE role. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.le_role = BLE_HS_ADV_LE_ROLE_BOTH_PERIPH_PREF;
-    rsp_fields.le_role_is_present = 1;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_LE_ROLE,
-                .val = (uint8_t[]) { BLE_HS_ADV_LE_ROLE_BOTH_PERIPH_PREF },
-                .val_len = 1,
-            },
-            { 0 },
-        });
-
-    /*** 0x20 - Service data - 32-bit UUID. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.svc_data_uuid32 = (uint8_t[]){ 1,2,3,4,5 };
-    rsp_fields.svc_data_uuid32_len = 5;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID32,
-                .val = (uint8_t[]) { 1,2,3,4,5 },
-                .val_len = 5,
-            },
-            { 0 },
-        });
-
-    /*** 0x21 - Service data - 128-bit UUID. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.svc_data_uuid128 =
-        (uint8_t[]){ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18 };
-    rsp_fields.svc_data_uuid128_len = 18;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_SVC_DATA_UUID128,
-                .val = (uint8_t[]){ 1,2,3,4,5,6,7,8,9,10,
-                                    11,12,13,14,15,16,17,18 },
-                .val_len = 18,
-            },
-            { 0 },
-        });
-
-    /*** 0x24 - URI. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.uri = (uint8_t[]){ 1,2,3,4 };
-    rsp_fields.uri_len = 4;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_URI,
-                .val = (uint8_t[]) { 1,2,3,4 },
-                .val_len = 4,
-            },
-            { 0 },
-        });
-
-    /*** 0xff - Manufacturer specific data. */
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-    rsp_fields.mfg_data = (uint8_t[]){ 1,2,3,4 };
-    rsp_fields.mfg_data_len = 4;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_TX_PWR_LVL,
-                .val = (uint8_t[]){ 0 },
-                .val_len = 1,
-            },
-            {
-                .type = BLE_HS_ADV_TYPE_FLAGS,
-                .val = (uint8_t[]){ BLE_HS_ADV_F_BREDR_UNSUP },
-                .val_len = 1,
-            },
-            { 0 },
-        },
-        &rsp_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_MFG_DATA,
-                .val = (uint8_t[]) { 1,2,3,4 },
-                .val_len = 4,
-            },
-            { 0 },
-        });
-}
-
-TEST_CASE(ble_hs_adv_test_case_user_full_payload)
-{
-    /* Intentionally allocate an extra byte. */
-    static const uint8_t mfg_data[30] = {
-        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
-        0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
-        0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
-        0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e,
-    };
-
-    struct ble_hs_adv_fields adv_fields;
-    struct ble_hs_adv_fields rsp_fields;
-    int rc;
-
-    ble_hs_test_util_init();
-
-    memset(&rsp_fields, 0, sizeof rsp_fields);
-
-    /***
-     * An advertisement should allow 31 bytes of user data.  Each field has a
-     * two-byte header, leaving 29 bytes of payload.
-     */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.mfg_data = (void *)mfg_data;
-    adv_fields.mfg_data_len = 29;
-
-    ble_hs_adv_test_misc_tx_and_verify_data(BLE_GAP_DISC_MODE_NON, &adv_fields,
-        (struct ble_hs_adv_test_field[]) {
-            {
-                .type = BLE_HS_ADV_TYPE_MFG_DATA,
-                .val = (void *)mfg_data,
-                .val_len = 29,
-            },
-            { 0 },
-        }, &rsp_fields, NULL);
-
-    /*** Fail with 30 bytes. */
-    rc = ble_hs_test_util_adv_stop(0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    adv_fields.mfg_data_len = 30;
-    rc = ble_gap_adv_set_fields(&adv_fields);
-    TEST_ASSERT(rc == BLE_HS_EMSGSIZE);
-}
-
-TEST_SUITE(ble_hs_adv_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_hs_adv_test_case_flags();
-    ble_hs_adv_test_case_user();
-    ble_hs_adv_test_case_user_rsp();
-    ble_hs_adv_test_case_user_full_payload();
-}
-
-int
-ble_hs_adv_test_all(void)
-{
-    ble_hs_adv_test_suite();
-
-    return tu_any_failed;
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_hs_conn_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_conn_test.c b/net/nimble/host/src/test/ble_hs_conn_test.c
deleted file mode 100644
index c957446..0000000
--- a/net/nimble/host/src/test/ble_hs_conn_test.c
+++ /dev/null
@@ -1,219 +0,0 @@
-/**
- * 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 <stddef.h>
-#include <errno.h>
-#include <string.h>
-#include "testutil/testutil.h"
-#include "nimble/hci_common.h"
-#include "host/ble_hs_adv.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-
-static int
-ble_hs_conn_test_util_any()
-{
-    struct ble_hs_conn *conn;
-
-    ble_hs_lock();
-    conn = ble_hs_conn_first();
-    ble_hs_unlock();
-
-    return conn != NULL;
-}
-
-TEST_CASE(ble_hs_conn_test_direct_connect_success)
-{
-    struct hci_le_conn_complete evt;
-    struct ble_l2cap_chan *chan;
-    struct ble_hs_conn *conn;
-    uint8_t addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int rc;
-
-    ble_hs_test_util_init();
-
-    /* Ensure no current or pending connections. */
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_hs_conn_test_util_any());
-
-    /* Initiate connection. */
-    rc = ble_hs_test_util_connect(BLE_ADDR_TYPE_PUBLIC,
-                                        BLE_ADDR_TYPE_PUBLIC,
-                                        addr, 0, NULL, NULL, NULL, 0);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(ble_gap_master_in_progress());
-
-    /* Receive successful connection complete event. */
-    memset(&evt, 0, sizeof evt);
-    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    evt.status = BLE_ERR_SUCCESS;
-    evt.connection_handle = 2;
-    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_MASTER;
-    memcpy(evt.peer_addr, addr, 6);
-    rc = ble_gap_rx_conn_complete(&evt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(!ble_gap_master_in_progress());
-
-    ble_hs_lock();
-
-    conn = ble_hs_conn_first();
-    TEST_ASSERT_FATAL(conn != NULL);
-    TEST_ASSERT(conn->bhc_handle == 2);
-    TEST_ASSERT(memcmp(conn->bhc_peer_addr, addr, 6) == 0);
-
-    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
-    TEST_ASSERT_FATAL(chan != NULL);
-    TEST_ASSERT(chan->blc_my_mtu == BLE_ATT_MTU_PREFERRED_DFLT);
-    TEST_ASSERT(chan->blc_peer_mtu == 0);
-    TEST_ASSERT(chan->blc_default_mtu == BLE_ATT_MTU_DFLT);
-
-    ble_hs_unlock();
-}
-
-TEST_CASE(ble_hs_conn_test_direct_connectable_success)
-{
-    struct hci_le_conn_complete evt;
-    struct ble_gap_adv_params adv_params;
-    struct ble_l2cap_chan *chan;
-    struct ble_hs_conn *conn;
-    uint8_t addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int rc;
-
-    ble_hs_test_util_init();
-
-    /* Ensure no current or pending connections. */
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_gap_adv_active());
-    TEST_ASSERT(!ble_hs_conn_test_util_any());
-
-    /* Initiate advertising. */
-    adv_params = ble_hs_test_util_adv_params;
-    adv_params.conn_mode = BLE_GAP_CONN_MODE_DIR;
-    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                                    addr, &adv_params, NULL, NULL, 0, 0);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(ble_gap_adv_active());
-
-    /* Receive successful connection complete event. */
-    memset(&evt, 0, sizeof evt);
-    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    evt.status = BLE_ERR_SUCCESS;
-    evt.connection_handle = 2;
-    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_SLAVE;
-    memcpy(evt.peer_addr, addr, 6);
-    rc = ble_gap_rx_conn_complete(&evt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_gap_adv_active());
-
-    ble_hs_lock();
-
-    conn = ble_hs_conn_first();
-    TEST_ASSERT_FATAL(conn != NULL);
-    TEST_ASSERT(conn->bhc_handle == 2);
-    TEST_ASSERT(memcmp(conn->bhc_peer_addr, addr, 6) == 0);
-
-    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
-    TEST_ASSERT_FATAL(chan != NULL);
-    TEST_ASSERT(chan->blc_my_mtu == BLE_ATT_MTU_PREFERRED_DFLT);
-    TEST_ASSERT(chan->blc_peer_mtu == 0);
-    TEST_ASSERT(chan->blc_default_mtu == BLE_ATT_MTU_DFLT);
-
-    ble_hs_unlock();
-}
-
-TEST_CASE(ble_hs_conn_test_undirect_connectable_success)
-{
-    struct ble_hs_adv_fields adv_fields;
-    struct hci_le_conn_complete evt;
-    struct ble_gap_adv_params adv_params;
-    struct ble_l2cap_chan *chan;
-    struct ble_hs_conn *conn;
-    uint8_t addr[6] = { 1, 2, 3, 4, 5, 6 };
-    int rc;
-
-    ble_hs_test_util_init();
-
-    /* Ensure no current or pending connections. */
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_gap_adv_active());
-    TEST_ASSERT(!ble_hs_conn_test_util_any());
-
-    /* Initiate advertising. */
-    memset(&adv_fields, 0, sizeof adv_fields);
-    adv_fields.tx_pwr_lvl_is_present = 1;
-    rc = ble_gap_adv_set_fields(&adv_fields);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    adv_params = ble_hs_test_util_adv_params;
-    adv_params.conn_mode = BLE_GAP_CONN_MODE_UND;
-    rc = ble_hs_test_util_adv_start(BLE_ADDR_TYPE_PUBLIC, BLE_ADDR_TYPE_PUBLIC,
-                                    addr, &adv_params, NULL, NULL, 0, 0);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(ble_gap_adv_active());
-
-    /* Receive successful connection complete event. */
-    memset(&evt, 0, sizeof evt);
-    evt.subevent_code = BLE_HCI_LE_SUBEV_CONN_COMPLETE;
-    evt.status = BLE_ERR_SUCCESS;
-    evt.connection_handle = 2;
-    evt.role = BLE_HCI_LE_CONN_COMPLETE_ROLE_SLAVE;
-    memcpy(evt.peer_addr, addr, 6);
-    rc = ble_gap_rx_conn_complete(&evt);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(!ble_gap_master_in_progress());
-    TEST_ASSERT(!ble_gap_adv_active());
-
-    ble_hs_lock();
-
-    conn = ble_hs_conn_first();
-    TEST_ASSERT_FATAL(conn != NULL);
-    TEST_ASSERT(conn->bhc_handle == 2);
-    TEST_ASSERT(memcmp(conn->bhc_peer_addr, addr, 6) == 0);
-
-    chan = ble_hs_conn_chan_find(conn, BLE_L2CAP_CID_ATT);
-    TEST_ASSERT_FATAL(chan != NULL);
-    TEST_ASSERT(chan->blc_my_mtu == BLE_ATT_MTU_PREFERRED_DFLT);
-    TEST_ASSERT(chan->blc_peer_mtu == 0);
-    TEST_ASSERT(chan->blc_default_mtu == BLE_ATT_MTU_DFLT);
-
-    ble_hs_unlock();
-}
-
-TEST_SUITE(conn_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_hs_conn_test_direct_connect_success();
-    ble_hs_conn_test_direct_connectable_success();
-    ble_hs_conn_test_undirect_connectable_success();
-}
-
-int
-ble_hs_conn_test_all(void)
-{
-    conn_suite();
-
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_hs_hci_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_hci_test.c b/net/nimble/host/src/test/ble_hs_hci_test.c
deleted file mode 100644
index 21184b8..0000000
--- a/net/nimble/host/src/test/ble_hs_hci_test.c
+++ /dev/null
@@ -1,99 +0,0 @@
-/**
- * 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 <stddef.h>
-#include <errno.h>
-#include <string.h>
-#include "nimble/hci_common.h"
-#include "nimble/ble_hci_trans.h"
-#include "host/ble_hs_test.h"
-#include "testutil/testutil.h"
-#include "ble_hs_test_util.h"
-
-TEST_CASE(ble_hs_hci_test_event_bad)
-{
-    uint8_t *buf;
-    int rc;
-
-    /*** Invalid event code. */
-    buf = ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_EVT_HI);
-    TEST_ASSERT_FATAL(buf != NULL);
-
-    buf[0] = 0xff;
-    buf[1] = 0;
-    rc = ble_hs_hci_evt_process(buf);
-    TEST_ASSERT(rc == BLE_HS_ENOTSUP);
-}
-
-TEST_CASE(ble_hs_hci_test_rssi)
-{
-    uint8_t params[BLE_HCI_READ_RSSI_ACK_PARAM_LEN];
-    uint16_t opcode;
-    int8_t rssi;
-    int rc;
-
-    opcode = ble_hs_hci_util_opcode_join(BLE_HCI_OGF_STATUS_PARAMS,
-                                  BLE_HCI_OCF_RD_RSSI);
-
-    /*** Success. */
-    /* Connection handle. */
-    htole16(params + 0, 1);
-
-    /* RSSI. */
-    params[2] = -8;
-
-    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params);
-
-    rc = ble_hs_hci_util_read_rssi(1, &rssi);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(rssi == -8);
-
-    /*** Failure: incorrect connection handle. */
-    htole16(params + 0, 99);
-
-    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params);
-
-    rc = ble_hs_hci_util_read_rssi(1, &rssi);
-    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
-
-    /*** Failure: params too short. */
-    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params - 1);
-    rc = ble_hs_hci_util_read_rssi(1, &rssi);
-    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
-
-    /*** Failure: params too long. */
-    ble_hs_test_util_set_ack_params(opcode, 0, params, sizeof params + 1);
-    rc = ble_hs_hci_util_read_rssi(1, &rssi);
-    TEST_ASSERT(rc == BLE_HS_ECONTROLLER);
-}
-
-TEST_SUITE(ble_hs_hci_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_hs_hci_test_event_bad();
-    ble_hs_hci_test_rssi();
-}
-
-int
-ble_hs_hci_test_all(void)
-{
-    ble_hs_hci_suite();
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_hs_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_hs_test.c b/net/nimble/host/src/test/ble_hs_test.c
deleted file mode 100644
index 3bc468e..0000000
--- a/net/nimble/host/src/test/ble_hs_test.c
+++ /dev/null
@@ -1,60 +0,0 @@
-/**
- * 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/hci_common.h"
-#include "host/ble_hs_test.h"
-#include "testutil/testutil.h"
-#include "ble_hs_test_util.h"
-
-#ifdef MYNEWT_SELFTEST
-
-int
-main(int argc, char **argv)
-{
-    tu_config.tc_print_results = 1;
-    tu_parse_args(argc, argv);
-
-    tu_init();
-
-    ble_att_clt_test_all();
-    ble_att_svr_test_all();
-    ble_gap_test_all();
-    ble_gatt_conn_test_all();
-    ble_gatt_disc_c_test_all();
-    ble_gatt_disc_d_test_all();
-    ble_gatt_disc_s_test_all();
-    ble_gatt_find_s_test_all();
-    ble_gatt_read_test_all();
-    ble_gatt_write_test_all();
-    ble_gatts_notify_test_all();
-    ble_gatts_read_test_suite();
-    ble_gatts_reg_test_all();
-    ble_hs_hci_test_all();
-    ble_hs_adv_test_all();
-    ble_hs_conn_test_all();
-    ble_l2cap_test_all();
-    ble_os_test_all();
-    ble_sm_test_all();
-    ble_uuid_test_all();
-
-    return tu_any_failed;
-}
-
-#endif


[06/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_sm_sc_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_sm_sc_test.c b/net/nimble/host/test/src/ble_sm_sc_test.c
new file mode 100644
index 0000000..24ecab4
--- /dev/null
+++ b/net/nimble/host/test/src/ble_sm_sc_test.c
@@ -0,0 +1,4910 @@
+/**
+ * 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 <stddef.h>
+#include <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/hci_common.h"
+#include "nimble/nimble_opt.h"
+#include "host/ble_sm.h"
+#include "host/ble_hs_test.h"
+#include "ble_hs_test_util.h"
+#include "ble_sm_test_util.h"
+
+#if NIMBLE_BLE_SM
+
+/**
+ * Secure connections pairing
+ * Master: peer
+ * Pair algorithm: just works
+ * Initiator IO capabilities: 3
+ * Responder IO capabilities: 3
+ * Bonding: true
+ * Initiator address type: 0
+ * Responder address type: 0
+ * Initiator key distribution: 5
+ * Responder key distribution: 7
+ */
+TEST_CASE(ble_sm_sc_peer_jw_iio3_rio3_b1_iat0_rat0_ik5_rk7)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
+        },
+        .resp_id_addr = {
+            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+        },
+        .pair_req = {
+            .io_cap = 0x03,
+            .oob_data_flag = 0x00,
+            .authreq = 0x09,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x0d,
+            .resp_key_dist = 0x0f,
+        },
+        .pair_rsp = {
+            .io_cap = 0x03,
+            .oob_data_flag = 0x00,
+            .authreq = 0x09,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x05,
+            .resp_key_dist = 0x07,
+        },
+        .our_priv_key = {
+            0x54, 0x8d, 0x20, 0xb8, 0x97, 0x0b, 0xbc, 0x43,
+            0x9a, 0xad, 0x10, 0x6f, 0x60, 0x74, 0xd4, 0x6a,
+            0x55, 0xc1, 0x7a, 0x17, 0x8b, 0x60, 0xe0, 0xb4,
+            0x5a, 0xe6, 0x58, 0xf1, 0xea, 0x12, 0xd9, 0xfb,
+        },
+        .public_key_req = {
+            .x = {
+                0xbc, 0xf2, 0xd8, 0xa5, 0xdb, 0xa3, 0x95, 0x6c,
+                0x99, 0xf9, 0x11, 0x0d, 0x4d, 0x2e, 0xf0, 0xbd,
+                0xee, 0x9b, 0x69, 0xb6, 0xcd, 0x88, 0x74, 0xbe,
+                0x40, 0xe8, 0xe5, 0xcc, 0xdc, 0x88, 0x44, 0x53,
+            },
+            .y = {
+                0xbf, 0xa9, 0x82, 0x0e, 0x18, 0x7a, 0x14, 0xf8,
+                0x77, 0xfd, 0x8e, 0x92, 0x2a, 0xf8, 0x5d, 0x39,
+                0xd1, 0x6d, 0x92, 0x1f, 0x38, 0x74, 0x99, 0xdc,
+                0x6c, 0x2c, 0x94, 0x23, 0xf9, 0x72, 0x56, 0xab,
+            },
+        },
+        .public_key_rsp = {
+            .x = {
+                0x72, 0x8c, 0xd1, 0x88, 0xd7, 0xbe, 0x49, 0xb2,
+                0xc5, 0x5c, 0x95, 0xb3, 0x64, 0xe0, 0x12, 0x32,
+                0xb6, 0xc9, 0x47, 0x63, 0x37, 0x38, 0x5b, 0x9c,
+                0x1e, 0x1b, 0x1a, 0x06, 0x09, 0xe2, 0x31, 0x85,
+            },
+            .y = {
+                0x19, 0x3a, 0x29, 0x69, 0x62, 0xd6, 0x30, 0xe7,
+                0xe8, 0x48, 0x63, 0xdc, 0x00, 0x73, 0x0a, 0x70,
+                0x7d, 0x2e, 0x29, 0xcc, 0x91, 0x77, 0x71, 0xb1,
+                0x75, 0xb8, 0xf7, 0xdc, 0xb0, 0xe2, 0x91, 0x10,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0x82, 0xed, 0xd0, 0x62, 0x91, 0x3d, 0x96, 0x7f,
+                0x13, 0xc5, 0x0d, 0x02, 0x2b, 0x5e, 0x43, 0x16,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0xa4, 0x34, 0x5f, 0xb3, 0xaf, 0x73, 0x43, 0x64,
+                0xcd, 0x19, 0x1b, 0x5b, 0x87, 0x58, 0x31, 0x66,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0xc0, 0x91, 0xfb, 0xb3, 0x77, 0xa2, 0x02, 0x0b,
+                0xc6, 0xcd, 0x6c, 0x04, 0x51, 0x45, 0x45, 0x39,
+            },
+        },
+        .dhkey_check_req = {
+            .value = {
+                0x82, 0x65, 0x1d, 0x02, 0xed, 0x89, 0x13, 0x44,
+                0x04, 0x1a, 0x14, 0x7c, 0x32, 0x9a, 0x1e, 0x7d,
+            },
+        },
+        .dhkey_check_rsp = {
+            .value = {
+                0x06, 0x3c, 0x28, 0x4a, 0xe5, 0x48, 0x4b, 0x51,
+                0x65, 0x4e, 0x14, 0x5e, 0x2f, 0xdd, 0xfa, 0x22,
+            },
+        },
+        .id_info_req = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_req = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+            },
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0x40, 0x53, 0xeb, 0x7a, 0x4d, 0x8e, 0xa2, 0xb5,
+                0xca, 0xa1, 0xb6, 0xae, 0x7e, 0x6a, 0x4d, 0xd9,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0xbc, 0x13, 0x4b, 0x45, 0xda, 0x76, 0x5b, 0xcd,
+                0xc2, 0x43, 0x81, 0xb8, 0xc3, 0x68, 0x12, 0xbb,
+            },
+        },
+        .ltk = {
+            0x63, 0x59, 0x8a, 0x14, 0x09, 0x4b, 0x94, 0x6e,
+            0xff, 0xae, 0x5e, 0x53, 0x86, 0x02, 0xa3, 0x6c,
+        },
+        .pair_alg = BLE_SM_PAIR_ALG_JW,
+        .authenticated = 0,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_NONE,
+            },
+        },
+    };
+    ble_sm_test_util_peer_sc_good(&params);
+}
+
+/**
+ * Secure connections pairing
+ * Master: peer
+ * Pair algorithm: passkey entry
+ * Initiator IO capabilities: 0
+ * Responder IO capabilities: 2
+ * Bonding: true
+ * Initiator address type: 0
+ * Responder address type: 0
+ * Initiator key distribution: 5
+ * Responder key distribution: 7
+ */
+TEST_CASE(ble_sm_sc_peer_pk_iio0_rio2_b1_iat0_rat0_ik5_rk7)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
+        },
+        .resp_id_addr = {
+            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+        },
+        .pair_req = {
+            .io_cap = 0x00,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x0d,
+            .resp_key_dist = 0x0f,
+        },
+        .pair_rsp = {
+            .io_cap = 0x02,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x05,
+            .resp_key_dist = 0x07,
+        },
+        .our_priv_key = {
+            0x54, 0x8d, 0x20, 0xb8, 0x97, 0x0b, 0xbc, 0x43,
+            0x9a, 0xad, 0x10, 0x6f, 0x60, 0x74, 0xd4, 0x6a,
+            0x55, 0xc1, 0x7a, 0x17, 0x8b, 0x60, 0xe0, 0xb4,
+            0x5a, 0xe6, 0x58, 0xf1, 0xea, 0x12, 0xd9, 0xfb,
+        },
+        .public_key_req = {
+            .x = {
+                0x22, 0x26, 0xcc, 0x64, 0x4d, 0xc1, 0x01, 0xd1,
+                0xb9, 0x8d, 0xe2, 0xd4, 0xbc, 0x55, 0x37, 0x4c,
+                0x12, 0x81, 0x14, 0x83, 0x81, 0xe8, 0x36, 0x1b,
+                0x78, 0xff, 0x49, 0xfc, 0xe9, 0x2e, 0x56, 0xc0,
+            },
+            .y = {
+                0xd9, 0x31, 0xa5, 0x8d, 0x02, 0xf1, 0x94, 0xb6,
+                0x83, 0x97, 0xd1, 0xfb, 0x01, 0x97, 0x4d, 0x06,
+                0xec, 0x18, 0x8d, 0x4a, 0xd2, 0x14, 0x12, 0x95,
+                0x2d, 0x4d, 0x18, 0xde, 0x4d, 0xaa, 0x91, 0x25,
+            },
+        },
+        .public_key_rsp = {
+            .x = {
+                0x72, 0x8c, 0xd1, 0x88, 0xd7, 0xbe, 0x49, 0xb2,
+                0xc5, 0x5c, 0x95, 0xb3, 0x64, 0xe0, 0x12, 0x32,
+                0xb6, 0xc9, 0x47, 0x63, 0x37, 0x38, 0x5b, 0x9c,
+                0x1e, 0x1b, 0x1a, 0x06, 0x09, 0xe2, 0x31, 0x85,
+            },
+            .y = {
+                0x19, 0x3a, 0x29, 0x69, 0x62, 0xd6, 0x30, 0xe7,
+                0xe8, 0x48, 0x63, 0xdc, 0x00, 0x73, 0x0a, 0x70,
+                0x7d, 0x2e, 0x29, 0xcc, 0x91, 0x77, 0x71, 0xb1,
+                0x75, 0xb8, 0xf7, 0xdc, 0xb0, 0xe2, 0x91, 0x10,
+            },
+        },
+        .confirm_req[0] = {
+            .value = {
+                0x2c, 0x16, 0x15, 0x0d, 0xe8, 0x18, 0x50, 0xd8,
+                0xae, 0x04, 0x6c, 0xa8, 0x50, 0xb8, 0xe5, 0x85,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0x62, 0x53, 0xfb, 0x69, 0x94, 0x33, 0x11, 0xd3,
+                0x8e, 0x03, 0xd5, 0x05, 0xd7, 0x68, 0x33, 0x16,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0xd5, 0x0e, 0x27, 0xcf, 0xa4, 0xc1, 0x52, 0x1b,
+                0xf1, 0x9d, 0x5f, 0xbe, 0xe2, 0xc0, 0x48, 0x38,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0x94, 0x31, 0x95, 0x44, 0x6c, 0xc5, 0x73, 0xc8,
+                0x8d, 0x72, 0x06, 0xe7, 0xfd, 0x16, 0x70, 0x5d,
+            },
+        },
+        .confirm_req[1] = {
+            .value = {
+                0x80, 0xae, 0x74, 0xaa, 0x9a, 0xfc, 0x09, 0x97,
+                0x10, 0x01, 0x4e, 0xbb, 0x16, 0x36, 0x6b, 0xc7,
+            },
+        },
+        .confirm_rsp[1] = {
+            .value = {
+                0x5a, 0xb1, 0xe5, 0x81, 0x5a, 0x1b, 0xef, 0xf4,
+                0xa8, 0x3d, 0xaa, 0x3f, 0x02, 0x1f, 0x78, 0x55,
+            },
+        },
+        .random_req[1] = {
+            .value = {
+                0x04, 0x4a, 0xf4, 0xd5, 0x4b, 0x4f, 0x77, 0x37,
+                0x2a, 0x3c, 0xfe, 0x83, 0x34, 0x6b, 0x38, 0x1a,
+            },
+        },
+        .random_rsp[1] = {
+            .value = {
+                0x24, 0xb3, 0x47, 0xc8, 0xb0, 0xa2, 0xa3, 0xd8,
+                0x78, 0x3d, 0x09, 0x8d, 0xea, 0x49, 0xf6, 0x22,
+            },
+        },
+        .confirm_req[2] = {
+            .value = {
+                0x56, 0x5f, 0x07, 0x30, 0x3a, 0xc1, 0x44, 0xf9,
+                0x00, 0x03, 0xb3, 0x93, 0x58, 0xb4, 0x2c, 0x85,
+            },
+        },
+        .confirm_rsp[2] = {
+            .value = {
+                0x50, 0x8a, 0xb3, 0x0b, 0xe4, 0x2e, 0xd3, 0x49,
+                0x59, 0x40, 0xb2, 0x71, 0xc9, 0x49, 0x29, 0x19,
+            },
+        },
+        .random_req[2] = {
+            .value = {
+                0x32, 0x37, 0x8e, 0x63, 0x6d, 0xbd, 0xd6, 0x18,
+                0xee, 0xa7, 0x0e, 0xe5, 0x7e, 0x5f, 0xe1, 0x80,
+            },
+        },
+        .random_rsp[2] = {
+            .value = {
+                0xa2, 0x1a, 0x92, 0xcd, 0xc0, 0x8f, 0x92, 0xb0,
+                0xe6, 0xbe, 0x43, 0x55, 0xc8, 0x47, 0x56, 0x4b,
+            },
+        },
+        .confirm_req[3] = {
+            .value = {
+                0x1b, 0xa0, 0x82, 0xda, 0xfc, 0xaf, 0x3f, 0x9c,
+                0xdf, 0xff, 0xa2, 0x18, 0xba, 0xbd, 0x9b, 0x48,
+            },
+        },
+        .confirm_rsp[3] = {
+            .value = {
+                0x6a, 0x90, 0xb7, 0x1c, 0x93, 0x4e, 0x4a, 0x8b,
+                0xda, 0xe8, 0x13, 0x6e, 0x01, 0x91, 0x74, 0xb1,
+            },
+        },
+        .random_req[3] = {
+            .value = {
+                0x41, 0xbf, 0x60, 0x64, 0x1d, 0xfc, 0xe2, 0xee,
+                0x00, 0xa3, 0x2a, 0xb1, 0xf8, 0x34, 0x6b, 0xeb,
+            },
+        },
+        .random_rsp[3] = {
+            .value = {
+                0xeb, 0x9c, 0xaf, 0x20, 0x14, 0x0f, 0xf2, 0x3e,
+                0xee, 0x45, 0xca, 0xe8, 0xdc, 0x17, 0xab, 0x22,
+            },
+        },
+        .confirm_req[4] = {
+            .value = {
+                0x75, 0x8f, 0x97, 0xbb, 0x87, 0xa8, 0x70, 0xda,
+                0x94, 0x5a, 0xd6, 0x09, 0x78, 0xe3, 0xdd, 0x43,
+            },
+        },
+        .confirm_rsp[4] = {
+            .value = {
+                0x8c, 0x2d, 0xa7, 0x44, 0xd9, 0x15, 0xa8, 0x9e,
+                0xdf, 0x3a, 0x59, 0xa5, 0xee, 0x92, 0x24, 0x3c,
+            },
+        },
+        .random_req[4] = {
+            .value = {
+                0xb9, 0xe0, 0xf3, 0xf6, 0x6f, 0xbd, 0xa0, 0x7a,
+                0x82, 0x20, 0x61, 0xbe, 0xf3, 0xe6, 0x4e, 0xac,
+            },
+        },
+        .random_rsp[4] = {
+            .value = {
+                0xdd, 0x9b, 0xd3, 0x10, 0xed, 0x12, 0xe8, 0xb5,
+                0xa2, 0x59, 0xe1, 0xdc, 0x5c, 0xd8, 0x6e, 0x96,
+            },
+        },
+        .confirm_req[5] = {
+            .value = {
+                0x9d, 0xc7, 0x97, 0x67, 0x8d, 0xd0, 0xd6, 0x1a,
+                0x4d, 0x52, 0xc0, 0x8d, 0x87, 0xa9, 0x75, 0xf5,
+            },
+        },
+        .confirm_rsp[5] = {
+            .value = {
+                0xd4, 0x5d, 0x61, 0x76, 0x38, 0xe3, 0x81, 0x85,
+                0x18, 0x5f, 0xac, 0xde, 0x49, 0x57, 0xf6, 0x9b,
+            },
+        },
+        .random_req[5] = {
+            .value = {
+                0xfe, 0x83, 0xe9, 0xc6, 0xe9, 0xa4, 0x83, 0x0d,
+                0xaf, 0x27, 0x6f, 0x79, 0x7a, 0x2b, 0x2d, 0x1f,
+            },
+        },
+        .random_rsp[5] = {
+            .value = {
+                0xf2, 0x0c, 0x9e, 0x75, 0x5b, 0xb1, 0x8c, 0xf1,
+                0x46, 0x4f, 0x68, 0xe8, 0x0a, 0x65, 0xd5, 0x81,
+            },
+        },
+        .confirm_req[6] = {
+            .value = {
+                0x15, 0x2b, 0x2e, 0x14, 0xf7, 0x31, 0xa2, 0xff,
+                0x93, 0xa7, 0x28, 0x65, 0xb1, 0x68, 0x96, 0xc6,
+            },
+        },
+        .confirm_rsp[6] = {
+            .value = {
+                0x6f, 0x01, 0x22, 0x14, 0x78, 0xfb, 0x93, 0xf4,
+                0xfa, 0xf1, 0x6d, 0x33, 0x49, 0x0e, 0x7d, 0x56,
+            },
+        },
+        .random_req[6] = {
+            .value = {
+                0x05, 0xe5, 0xed, 0x99, 0x63, 0x05, 0x29, 0xb1,
+                0xbd, 0xf7, 0x2b, 0xa6, 0x94, 0xfe, 0x45, 0xb2,
+            },
+        },
+        .random_rsp[6] = {
+            .value = {
+                0x51, 0xf1, 0x2a, 0xa6, 0x7b, 0xe0, 0xb3, 0x20,
+                0x7d, 0x7e, 0xd3, 0x47, 0xfb, 0x83, 0xe1, 0xc6,
+            },
+        },
+        .confirm_req[7] = {
+            .value = {
+                0x9e, 0x7a, 0x3d, 0x12, 0x3b, 0x30, 0x81, 0x23,
+                0x1c, 0x94, 0x42, 0x73, 0x41, 0x68, 0xc6, 0x17,
+            },
+        },
+        .confirm_rsp[7] = {
+            .value = {
+                0x55, 0x31, 0x41, 0xe8, 0x1f, 0x11, 0xa6, 0x06,
+                0x7a, 0x7c, 0x84, 0x10, 0xad, 0xd3, 0x73, 0xcf,
+            },
+        },
+        .random_req[7] = {
+            .value = {
+                0xcb, 0x92, 0x18, 0xf6, 0x59, 0x6a, 0x1b, 0x18,
+                0x63, 0x72, 0x54, 0xc2, 0x1a, 0x3d, 0x09, 0x67,
+            },
+        },
+        .random_rsp[7] = {
+            .value = {
+                0xae, 0xf2, 0x96, 0xfd, 0xff, 0xd7, 0x18, 0xac,
+                0x5d, 0xb2, 0x9d, 0x89, 0x56, 0x2a, 0x19, 0xae,
+            },
+        },
+        .confirm_req[8] = {
+            .value = {
+                0x06, 0x8d, 0x5d, 0x19, 0xb3, 0x27, 0xc9, 0x6a,
+                0xe8, 0x58, 0xe7, 0x17, 0x10, 0x6a, 0xf9, 0xf7,
+            },
+        },
+        .confirm_rsp[8] = {
+            .value = {
+                0xf0, 0xbc, 0x2a, 0x03, 0x1f, 0x9b, 0x7b, 0x58,
+                0x43, 0x0f, 0xf5, 0x17, 0xc4, 0xbd, 0xec, 0x23,
+            },
+        },
+        .random_req[8] = {
+            .value = {
+                0xbe, 0x78, 0xcd, 0x84, 0x91, 0x4a, 0x1b, 0xdd,
+                0x6a, 0x0d, 0x88, 0x72, 0x9e, 0xc2, 0x4f, 0x5a,
+            },
+        },
+        .random_rsp[8] = {
+            .value = {
+                0xff, 0xac, 0xfe, 0x71, 0x2f, 0x6a, 0x13, 0xdc,
+                0xd3, 0x02, 0x81, 0x88, 0xbf, 0xc9, 0x9c, 0xd6,
+            },
+        },
+        .confirm_req[9] = {
+            .value = {
+                0xb0, 0x8d, 0x47, 0x23, 0x7e, 0xdb, 0xf5, 0x64,
+                0x5e, 0x83, 0x52, 0x9f, 0x06, 0x65, 0x84, 0x10,
+            },
+        },
+        .confirm_rsp[9] = {
+            .value = {
+                0x4d, 0x3f, 0xd4, 0x5a, 0x45, 0x57, 0xe9, 0xd7,
+                0x1e, 0x65, 0x7a, 0xa0, 0xd8, 0x5a, 0xa8, 0x29,
+            },
+        },
+        .random_req[9] = {
+            .value = {
+                0xb0, 0xcd, 0xfa, 0x39, 0x0d, 0x2e, 0x07, 0xfe,
+                0x36, 0x47, 0x8d, 0x8e, 0x1a, 0x47, 0x67, 0xf2,
+            },
+        },
+        .random_rsp[9] = {
+            .value = {
+                0xb4, 0xf5, 0x12, 0x64, 0xf4, 0xf6, 0xd7, 0x6e,
+                0xeb, 0x1e, 0x9a, 0x3f, 0x18, 0xba, 0xfb, 0x99,
+            },
+        },
+        .confirm_req[10] = {
+            .value = {
+                0xc9, 0x76, 0xb3, 0x3f, 0x80, 0xd9, 0x0c, 0xfb,
+                0xe3, 0x90, 0x1b, 0x7a, 0xbc, 0xe1, 0x7c, 0xde,
+            },
+        },
+        .confirm_rsp[10] = {
+            .value = {
+                0x21, 0x6a, 0x45, 0x6e, 0x6a, 0xac, 0xba, 0x9e,
+                0x66, 0x39, 0x5b, 0xb6, 0x74, 0xfe, 0x2b, 0x28,
+            },
+        },
+        .random_req[10] = {
+            .value = {
+                0xc0, 0xd4, 0xdf, 0x7b, 0x0f, 0x2f, 0xaa, 0x68,
+                0x4e, 0x3d, 0xa4, 0x59, 0x6f, 0x24, 0xe6, 0x7e,
+            },
+        },
+        .random_rsp[10] = {
+            .value = {
+                0xdf, 0x89, 0x49, 0xe7, 0x9f, 0x60, 0xdd, 0xf6,
+                0x44, 0x97, 0xe3, 0x15, 0x52, 0x65, 0x67, 0x3e,
+            },
+        },
+        .confirm_req[11] = {
+            .value = {
+                0xb0, 0x3f, 0x34, 0xce, 0x7d, 0x2e, 0xf1, 0xab,
+                0x23, 0xd5, 0x89, 0xf5, 0xaa, 0xa8, 0x59, 0x9f,
+            },
+        },
+        .confirm_rsp[11] = {
+            .value = {
+                0xb1, 0x33, 0x6a, 0x64, 0xd8, 0xeb, 0x8b, 0xa0,
+                0xf4, 0x1a, 0x15, 0x28, 0xb9, 0xe4, 0xa1, 0x31,
+            },
+        },
+        .random_req[11] = {
+            .value = {
+                0xd2, 0x88, 0x24, 0xfe, 0x95, 0x11, 0xc5, 0x0a,
+                0x21, 0xfb, 0x96, 0xea, 0x61, 0xb9, 0x8b, 0x26,
+            },
+        },
+        .random_rsp[11] = {
+            .value = {
+                0x8f, 0x22, 0x66, 0x8e, 0x7e, 0x62, 0x34, 0x37,
+                0xfc, 0x4a, 0x48, 0x1f, 0xf7, 0x38, 0x3b, 0x4e,
+            },
+        },
+        .confirm_req[12] = {
+            .value = {
+                0xc4, 0x50, 0xc8, 0x53, 0x58, 0xfb, 0xea, 0x9a,
+                0xdc, 0x35, 0xc7, 0xf3, 0x5b, 0x7c, 0xfb, 0xe4,
+            },
+        },
+        .confirm_rsp[12] = {
+            .value = {
+                0x27, 0xd9, 0x32, 0xd6, 0x43, 0xbf, 0x57, 0x3f,
+                0x35, 0x73, 0x3c, 0x3e, 0xbe, 0x53, 0x19, 0xff,
+            },
+        },
+        .random_req[12] = {
+            .value = {
+                0x99, 0xa1, 0x7a, 0x5f, 0xe0, 0x48, 0x1c, 0x6c,
+                0x84, 0xac, 0xab, 0xed, 0x69, 0x55, 0x1e, 0x66,
+            },
+        },
+        .random_rsp[12] = {
+            .value = {
+                0x37, 0x50, 0x90, 0x35, 0xef, 0x84, 0x06, 0x18,
+                0xfd, 0x3b, 0xc1, 0x8a, 0x46, 0x91, 0xb8, 0x21,
+            },
+        },
+        .confirm_req[13] = {
+            .value = {
+                0x2f, 0xcb, 0x3e, 0xc3, 0xce, 0x82, 0x0b, 0x5c,
+                0xdc, 0x9c, 0xbd, 0x44, 0xf9, 0x04, 0x22, 0x8c,
+            },
+        },
+        .confirm_rsp[13] = {
+            .value = {
+                0xab, 0xf2, 0x2e, 0x40, 0xd0, 0x74, 0x4f, 0xd4,
+                0x26, 0x9c, 0x89, 0x9e, 0x38, 0x77, 0xac, 0x9d,
+            },
+        },
+        .random_req[13] = {
+            .value = {
+                0xbc, 0xda, 0x58, 0xa2, 0x98, 0x88, 0xfe, 0x9f,
+                0x95, 0x0e, 0x3a, 0x91, 0xba, 0xe9, 0xbf, 0x02,
+            },
+        },
+        .random_rsp[13] = {
+            .value = {
+                0x04, 0xb9, 0x4c, 0x26, 0xce, 0x87, 0x8f, 0x17,
+                0xdc, 0xbc, 0x36, 0x94, 0x47, 0x67, 0x9f, 0xde,
+            },
+        },
+        .confirm_req[14] = {
+            .value = {
+                0xbd, 0xb6, 0x54, 0xc8, 0x1f, 0x51, 0x23, 0x98,
+                0x48, 0x3d, 0x47, 0x9d, 0xa3, 0xb8, 0xe7, 0x55,
+            },
+        },
+        .confirm_rsp[14] = {
+            .value = {
+                0x06, 0xc2, 0x7b, 0x80, 0x76, 0x9c, 0x37, 0x78,
+                0x46, 0xc5, 0x45, 0x43, 0x5d, 0x8d, 0x5b, 0x3e,
+            },
+        },
+        .random_req[14] = {
+            .value = {
+                0xef, 0x9e, 0x8a, 0x3a, 0xb7, 0xde, 0xa8, 0x07,
+                0x58, 0x73, 0xe0, 0x07, 0xfc, 0x62, 0xdb, 0x62,
+            },
+        },
+        .random_rsp[14] = {
+            .value = {
+                0xfa, 0xd5, 0xb2, 0x4e, 0x20, 0x01, 0x93, 0xc0,
+                0xb3, 0x76, 0xa5, 0x7a, 0x92, 0x8f, 0xb9, 0x6d,
+            },
+        },
+        .confirm_req[15] = {
+            .value = {
+                0x76, 0x2e, 0xc6, 0x64, 0x6c, 0x13, 0x01, 0x7e,
+                0x34, 0x78, 0x12, 0xb8, 0x1a, 0xb7, 0xf7, 0x39,
+            },
+        },
+        .confirm_rsp[15] = {
+            .value = {
+                0xbd, 0xae, 0x10, 0x32, 0xdb, 0x63, 0x30, 0x6f,
+                0x68, 0x19, 0x49, 0x5e, 0x34, 0x4f, 0x13, 0xc6,
+            },
+        },
+        .random_req[15] = {
+            .value = {
+                0x95, 0x2e, 0xe4, 0xe3, 0xb2, 0xdc, 0x79, 0xad,
+                0x5f, 0x0c, 0x19, 0x9c, 0x47, 0x9c, 0x79, 0x17,
+            },
+        },
+        .random_rsp[15] = {
+            .value = {
+                0x9e, 0x3d, 0x7f, 0xcd, 0x18, 0x40, 0xd7, 0xac,
+                0xa1, 0x45, 0x5f, 0xcb, 0x29, 0x57, 0x2b, 0x63,
+            },
+        },
+        .confirm_req[16] = {
+            .value = {
+                0x10, 0x18, 0x9d, 0xf2, 0xed, 0x76, 0x5c, 0x5f,
+                0x32, 0xa6, 0x29, 0x61, 0x12, 0xb2, 0xb8, 0xa2,
+            },
+        },
+        .confirm_rsp[16] = {
+            .value = {
+                0x3c, 0xd4, 0xbd, 0xe9, 0xd3, 0x29, 0xac, 0xf7,
+                0xfc, 0x04, 0xd3, 0xe4, 0x46, 0x14, 0x28, 0x2c,
+            },
+        },
+        .random_req[16] = {
+            .value = {
+                0x6d, 0xe8, 0x77, 0xc3, 0xab, 0x49, 0x6b, 0x79,
+                0x4f, 0x0f, 0x4c, 0x65, 0xc5, 0x77, 0x68, 0xd9,
+            },
+        },
+        .random_rsp[16] = {
+            .value = {
+                0xd0, 0x59, 0xf3, 0x53, 0xb1, 0x14, 0x81, 0x88,
+                0x26, 0x88, 0xef, 0x4b, 0xa4, 0x7d, 0x0a, 0x84,
+            },
+        },
+        .confirm_req[17] = {
+            .value = {
+                0xa3, 0x96, 0x9f, 0x96, 0x53, 0x0e, 0x38, 0x78,
+                0x9e, 0xbd, 0xf7, 0x65, 0x23, 0x73, 0x99, 0xa7,
+            },
+        },
+        .confirm_rsp[17] = {
+            .value = {
+                0x6b, 0x25, 0x8d, 0x51, 0xd8, 0xc4, 0xd9, 0xbf,
+                0xa6, 0x4f, 0xa3, 0x25, 0x28, 0xb5, 0x7c, 0x05,
+            },
+        },
+        .random_req[17] = {
+            .value = {
+                0xa5, 0xac, 0xd9, 0xb6, 0x9e, 0x98, 0x75, 0xae,
+                0x9b, 0x16, 0xe1, 0x60, 0xc6, 0xa5, 0x07, 0xf2,
+            },
+        },
+        .random_rsp[17] = {
+            .value = {
+                0x65, 0x53, 0x56, 0xe6, 0x2c, 0x22, 0x68, 0xc9,
+                0xb8, 0xbe, 0xb1, 0x40, 0x08, 0xe2, 0xb6, 0xb9,
+            },
+        },
+        .confirm_req[18] = {
+            .value = {
+                0x67, 0xcd, 0x0e, 0x4f, 0xfc, 0x38, 0x7f, 0x8a,
+                0x3b, 0xea, 0xff, 0x86, 0xf3, 0x8a, 0x92, 0xcb,
+            },
+        },
+        .confirm_rsp[18] = {
+            .value = {
+                0x22, 0x95, 0x1f, 0x20, 0xc9, 0x5c, 0x73, 0x39,
+                0xa4, 0xd9, 0xc1, 0x37, 0x9d, 0x94, 0xb2, 0xfd,
+            },
+        },
+        .random_req[18] = {
+            .value = {
+                0xe1, 0x80, 0x82, 0xdd, 0x21, 0x6c, 0xe4, 0x93,
+                0xa3, 0x41, 0x0f, 0xfc, 0x96, 0x42, 0x8b, 0xde,
+            },
+        },
+        .random_rsp[18] = {
+            .value = {
+                0x11, 0x1c, 0xd7, 0x7a, 0xe7, 0x1a, 0x88, 0xdd,
+                0x2a, 0xdf, 0xe5, 0x30, 0xca, 0x0b, 0x9f, 0xb6,
+            },
+        },
+        .confirm_req[19] = {
+            .value = {
+                0x45, 0x9b, 0x36, 0x3d, 0xf8, 0xc0, 0x43, 0x6d,
+                0x94, 0xcf, 0xbd, 0x5f, 0xfe, 0xec, 0xd7, 0x4b,
+            },
+        },
+        .confirm_rsp[19] = {
+            .value = {
+                0xf0, 0xaa, 0xfd, 0xae, 0xb7, 0x73, 0x3c, 0x9d,
+                0x93, 0xd4, 0x00, 0xea, 0x81, 0x31, 0xde, 0x41,
+            },
+        },
+        .random_req[19] = {
+            .value = {
+                0x1a, 0xaa, 0xff, 0x2a, 0xdc, 0xcc, 0x89, 0xbc,
+                0xcf, 0x48, 0x5c, 0x1e, 0x4d, 0x69, 0x85, 0x39,
+            },
+        },
+        .random_rsp[19] = {
+            .value = {
+                0xe7, 0xd0, 0xcb, 0x9a, 0xb5, 0x76, 0xec, 0xfc,
+                0x48, 0xa3, 0x41, 0x48, 0x4c, 0xa7, 0xec, 0xb7,
+            },
+        },
+        .dhkey_check_req = {
+            .value = {
+                0xe3, 0x4e, 0x42, 0xb5, 0xe3, 0x63, 0x4b, 0x7c,
+                0xf0, 0x9f, 0xef, 0x6e, 0x97, 0xe2, 0x86, 0xc0,
+            },
+        },
+        .dhkey_check_rsp = {
+            .value = {
+                0xea, 0x8a, 0xab, 0x7f, 0x15, 0x21, 0x5a, 0x36,
+                0x9b, 0x56, 0xee, 0x51, 0x61, 0x97, 0xe2, 0x0a,
+            },
+        },
+        .id_info_req = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_req = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+            },
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0x85, 0x54, 0x52, 0xe3, 0xb4, 0xe8, 0x26, 0xa4,
+                0x38, 0xb0, 0x4c, 0xa0, 0x41, 0xf5, 0x30, 0x6e,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0x6f, 0x93, 0xb8, 0x9c, 0x26, 0x88, 0xb4, 0x20,
+                0x87, 0x95, 0xf2, 0xf4, 0x3a, 0xbe, 0x92, 0xb7,
+            },
+        },
+        .ltk = {
+            0x30, 0xf6, 0xd3, 0x2e, 0x1c, 0x81, 0x2c, 0x96,
+            0x56, 0x30, 0x55, 0xec, 0x9b, 0x72, 0xf4, 0x83,
+        },
+        .pair_alg = BLE_SM_PAIR_ALG_PASSKEY,
+        .authenticated = 1,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_INPUT,
+                .passkey = 879894,
+            },
+        },
+    };
+    ble_sm_test_util_peer_sc_good(&params);
+}
+
+/**
+ * Secure connections pairing
+ * Master: peer
+ * Pair algorithm: passkey entry
+ * Initiator IO capabilities: 2
+ * Responder IO capabilities: 0
+ * Bonding: true
+ * Initiator address type: 0
+ * Responder address type: 0
+ * Initiator key distribution: 5
+ * Responder key distribution: 7
+ */
+TEST_CASE(ble_sm_sc_peer_pk_iio2_rio0_b1_iat0_rat0_ik5_rk7)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
+        },
+        .resp_id_addr = {
+            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+        },
+        .pair_req = {
+            .io_cap = 0x02,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x0d,
+            .resp_key_dist = 0x0f,
+        },
+        .pair_rsp = {
+            .io_cap = 0x00,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x05,
+            .resp_key_dist = 0x07,
+        },
+        .our_priv_key = {
+            0xd6, 0x2f, 0x4f, 0x6b, 0xeb, 0xfc, 0xbd, 0xee,
+            0x9b, 0x94, 0xd7, 0x15, 0x98, 0xc6, 0x0c, 0x83,
+            0x9b, 0xc7, 0xa2, 0x45, 0xfd, 0x00, 0xe8, 0xa4,
+            0x52, 0xe9, 0x70, 0x2f, 0xd7, 0x62, 0xf1, 0xa4,
+        },
+        .public_key_req = {
+            .x = {
+                0xd6, 0xa7, 0xaf, 0xc1, 0x18, 0x8b, 0x92, 0x2f,
+                0xbc, 0xbc, 0x4d, 0xb8, 0x5c, 0xfb, 0x39, 0x7c,
+                0x1e, 0x90, 0x7e, 0xfa, 0xa2, 0x0d, 0xee, 0x9e,
+                0xb4, 0x9e, 0xbe, 0x50, 0xf0, 0xbc, 0x2c, 0x10,
+            },
+            .y = {
+                0xa4, 0x25, 0xad, 0x75, 0xbe, 0xab, 0x1e, 0xcf,
+                0x4e, 0xc8, 0x19, 0xab, 0x6c, 0x68, 0x38, 0xa4,
+                0xe7, 0x43, 0x7b, 0x19, 0xef, 0x28, 0xd5, 0x93,
+                0x52, 0xe9, 0xb9, 0x31, 0x68, 0x60, 0x19, 0x71,
+            },
+        },
+        .public_key_rsp = {
+            .x = {
+                0xbc, 0x6a, 0xcf, 0xc6, 0x8a, 0x3a, 0xdc, 0x89,
+                0xdd, 0xa9, 0xaf, 0x29, 0xc7, 0xaf, 0xe2, 0x8b,
+                0x25, 0xee, 0xce, 0xa6, 0x10, 0x1d, 0x33, 0x2f,
+                0xd5, 0xfc, 0x30, 0xb8, 0xb1, 0x7b, 0xb1, 0x6e,
+            },
+            .y = {
+                0x1a, 0xc6, 0x42, 0x36, 0x98, 0x40, 0x4f, 0x90,
+                0x82, 0xa0, 0x10, 0x3a, 0xa5, 0x0f, 0xcf, 0x57,
+                0xd2, 0x2e, 0x80, 0x9d, 0x61, 0xc7, 0x21, 0xac,
+                0x47, 0x5b, 0x93, 0x75, 0x02, 0x30, 0x40, 0x14,
+            },
+        },
+        .confirm_req[0] = {
+            .value = {
+                0xd1, 0x64, 0x49, 0xa0, 0xc4, 0x28, 0x81, 0x57,
+                0x0c, 0x25, 0x62, 0xfb, 0x2c, 0xa2, 0xb0, 0xc7,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0xea, 0xae, 0x4e, 0x03, 0x00, 0xf9, 0xd1, 0x65,
+                0xc7, 0x6a, 0x0d, 0x74, 0x4f, 0x02, 0x0b, 0x94,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0x05, 0xb2, 0x09, 0x9b, 0x36, 0x23, 0x4f, 0x74,
+                0x4e, 0xc9, 0x7a, 0x2c, 0x65, 0x3a, 0xd1, 0xf6,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0x50, 0xd8, 0x88, 0xd4, 0x7e, 0xc1, 0x36, 0x92,
+                0x0f, 0xa7, 0x17, 0x3c, 0xb4, 0xeb, 0xee, 0xa6,
+            },
+        },
+        .confirm_req[1] = {
+            .value = {
+                0xab, 0xa2, 0xd0, 0xec, 0xdd, 0xf3, 0xd2, 0xa9,
+                0x2d, 0xde, 0x4b, 0x02, 0x66, 0x45, 0x2f, 0xc0,
+            },
+        },
+        .confirm_rsp[1] = {
+            .value = {
+                0xa9, 0xc1, 0x9d, 0x75, 0xd0, 0xb6, 0xec, 0x06,
+                0x31, 0x87, 0xb6, 0x9d, 0x31, 0xdc, 0x92, 0x7c,
+            },
+        },
+        .random_req[1] = {
+            .value = {
+                0xb9, 0x5b, 0xe0, 0x0f, 0x83, 0xe7, 0x2d, 0x77,
+                0x2f, 0x55, 0x0a, 0x2c, 0xd9, 0xc1, 0x46, 0xcd,
+            },
+        },
+        .random_rsp[1] = {
+            .value = {
+                0xa2, 0x9a, 0x5b, 0x99, 0xb1, 0xc0, 0xc5, 0xd6,
+                0xf1, 0x87, 0x0b, 0x49, 0x9c, 0xfd, 0xfe, 0xd5,
+            },
+        },
+        .confirm_req[2] = {
+            .value = {
+                0x3a, 0x9d, 0x58, 0xe5, 0xb0, 0x31, 0xd9, 0xde,
+                0xac, 0xd2, 0x44, 0xb7, 0xe1, 0xe5, 0x89, 0x50,
+            },
+        },
+        .confirm_rsp[2] = {
+            .value = {
+                0xae, 0x4e, 0x4f, 0x84, 0x5f, 0x4c, 0xd1, 0x9b,
+                0x81, 0x22, 0x9c, 0x68, 0x52, 0xe0, 0x9a, 0xfc,
+            },
+        },
+        .random_req[2] = {
+            .value = {
+                0xa5, 0xbb, 0x5f, 0x9a, 0xa2, 0x97, 0xdb, 0xcd,
+                0x3d, 0xfe, 0xd9, 0x58, 0x21, 0x52, 0x99, 0xb7,
+            },
+        },
+        .random_rsp[2] = {
+            .value = {
+                0xea, 0x44, 0xdd, 0x0c, 0xbf, 0xb5, 0x6b, 0xc7,
+                0xe1, 0x19, 0xe8, 0x0b, 0xc2, 0x15, 0x04, 0x37,
+            },
+        },
+        .confirm_req[3] = {
+            .value = {
+                0xa8, 0xa3, 0xdb, 0x08, 0xca, 0x31, 0xd5, 0xef,
+                0x17, 0x37, 0x77, 0xd0, 0x64, 0x2e, 0x2f, 0x2f,
+            },
+        },
+        .confirm_rsp[3] = {
+            .value = {
+                0xe4, 0xf6, 0xa5, 0x94, 0x1a, 0x09, 0x4b, 0x75,
+                0x79, 0xb8, 0x0c, 0xe6, 0xe2, 0x28, 0x5a, 0x2c,
+            },
+        },
+        .random_req[3] = {
+            .value = {
+                0x1a, 0x3f, 0x80, 0x6f, 0xd3, 0xe8, 0xc5, 0xfb,
+                0x9b, 0xda, 0xa1, 0x07, 0x68, 0x1a, 0x54, 0xbc,
+            },
+        },
+        .random_rsp[3] = {
+            .value = {
+                0x1b, 0x48, 0x22, 0x87, 0x04, 0x24, 0x87, 0xba,
+                0x14, 0xb9, 0x85, 0xb2, 0xa6, 0xf5, 0xea, 0x89,
+            },
+        },
+        .confirm_req[4] = {
+            .value = {
+                0x31, 0xcb, 0xc4, 0x0c, 0x36, 0xb5, 0xe2, 0x32,
+                0xd8, 0x0e, 0xd3, 0x86, 0x96, 0xe3, 0x8c, 0x84,
+            },
+        },
+        .confirm_rsp[4] = {
+            .value = {
+                0x90, 0x11, 0x30, 0x35, 0x5f, 0xe5, 0x45, 0xff,
+                0xab, 0xd3, 0xe0, 0xbe, 0x1c, 0x20, 0x23, 0xb8,
+            },
+        },
+        .random_req[4] = {
+            .value = {
+                0xa0, 0xc7, 0x79, 0x28, 0x87, 0x19, 0xa3, 0x78,
+                0x33, 0xe5, 0x1a, 0x81, 0xba, 0x9b, 0xe3, 0x5c,
+            },
+        },
+        .random_rsp[4] = {
+            .value = {
+                0x43, 0xcf, 0x20, 0x1b, 0x39, 0x3f, 0xdf, 0x73,
+                0x58, 0xd2, 0x0d, 0xc7, 0x41, 0xd7, 0x58, 0xea,
+            },
+        },
+        .confirm_req[5] = {
+            .value = {
+                0x59, 0xda, 0x78, 0xeb, 0xd5, 0xcd, 0x8e, 0x23,
+                0xe5, 0x5e, 0xa7, 0xa5, 0xba, 0x13, 0x00, 0xff,
+            },
+        },
+        .confirm_rsp[5] = {
+            .value = {
+                0x31, 0x7a, 0xf0, 0x56, 0x82, 0x69, 0xdb, 0xcd,
+                0x27, 0x5a, 0x11, 0xd3, 0x65, 0x82, 0x0d, 0xda,
+            },
+        },
+        .random_req[5] = {
+            .value = {
+                0x2e, 0xe8, 0x76, 0x40, 0x9c, 0x49, 0x07, 0x42,
+                0x1e, 0x45, 0x7b, 0x1e, 0x73, 0xa3, 0x71, 0x05,
+            },
+        },
+        .random_rsp[5] = {
+            .value = {
+                0x64, 0x99, 0x42, 0x5d, 0x05, 0xd6, 0x12, 0x41,
+                0x2a, 0x44, 0x55, 0x26, 0xe7, 0x08, 0x5e, 0xfb,
+            },
+        },
+        .confirm_req[6] = {
+            .value = {
+                0x1c, 0x55, 0xe1, 0x75, 0x4f, 0x6e, 0xdd, 0x7e,
+                0xc8, 0xff, 0x76, 0x25, 0xdb, 0x2a, 0x6d, 0xe3,
+            },
+        },
+        .confirm_rsp[6] = {
+            .value = {
+                0xf6, 0x36, 0x78, 0x88, 0x62, 0xa8, 0x78, 0xe6,
+                0xf9, 0xa1, 0x17, 0x63, 0x86, 0xd3, 0xae, 0x60,
+            },
+        },
+        .random_req[6] = {
+            .value = {
+                0x96, 0x9a, 0x1c, 0xbe, 0x82, 0x82, 0xc2, 0xa7,
+                0x18, 0xc3, 0x7b, 0x40, 0x5d, 0x6c, 0x4e, 0xe3,
+            },
+        },
+        .random_rsp[6] = {
+            .value = {
+                0x2b, 0x7d, 0x36, 0xc3, 0xf7, 0x59, 0x63, 0x40,
+                0x6f, 0xc0, 0x2a, 0x2b, 0x1b, 0xd7, 0x41, 0x38,
+            },
+        },
+        .confirm_req[7] = {
+            .value = {
+                0x88, 0x99, 0x53, 0xae, 0x2a, 0xaf, 0x97, 0x5a,
+                0xcc, 0x9f, 0xfd, 0xe2, 0x1d, 0xd3, 0x27, 0x66,
+            },
+        },
+        .confirm_rsp[7] = {
+            .value = {
+                0xdb, 0xae, 0xfb, 0xf7, 0x33, 0xd4, 0xd1, 0xcb,
+                0xfe, 0x75, 0x8e, 0x81, 0x16, 0xd1, 0x49, 0xeb,
+            },
+        },
+        .random_req[7] = {
+            .value = {
+                0x13, 0x5c, 0x00, 0x34, 0xe5, 0x96, 0xd0, 0x97,
+                0xb1, 0x84, 0x3d, 0x00, 0xb4, 0x2a, 0x4a, 0x12,
+            },
+        },
+        .random_rsp[7] = {
+            .value = {
+                0xed, 0x94, 0x1f, 0x41, 0x12, 0xe5, 0x35, 0x5b,
+                0xa6, 0x6a, 0x72, 0x1e, 0xa2, 0x7c, 0xe1, 0x6c,
+            },
+        },
+        .confirm_req[8] = {
+            .value = {
+                0xa3, 0xc7, 0x17, 0xad, 0xb6, 0xe6, 0xaa, 0x16,
+                0x8d, 0x4b, 0x70, 0x5f, 0x49, 0x73, 0xa7, 0x19,
+            },
+        },
+        .confirm_rsp[8] = {
+            .value = {
+                0x10, 0xb0, 0x31, 0xa7, 0x16, 0x61, 0xf7, 0xd6,
+                0xe6, 0x16, 0x9e, 0xb1, 0x9e, 0xb5, 0x5e, 0x94,
+            },
+        },
+        .random_req[8] = {
+            .value = {
+                0x6f, 0xe7, 0x62, 0x73, 0xfb, 0xbf, 0xf1, 0x4a,
+                0x14, 0xa1, 0x09, 0x45, 0xd4, 0xde, 0x26, 0xad,
+            },
+        },
+        .random_rsp[8] = {
+            .value = {
+                0x3f, 0x48, 0xa7, 0xdf, 0x4a, 0xd5, 0x55, 0x26,
+                0xd3, 0x32, 0xbf, 0x98, 0x4a, 0x20, 0xad, 0xb0,
+            },
+        },
+        .confirm_req[9] = {
+            .value = {
+                0x88, 0x1c, 0xef, 0xfe, 0x4e, 0x68, 0x41, 0x7c,
+                0xe8, 0xe8, 0x81, 0x1a, 0xb9, 0x9e, 0xaf, 0xc6,
+            },
+        },
+        .confirm_rsp[9] = {
+            .value = {
+                0xa3, 0x53, 0x2a, 0xe1, 0xbd, 0x9d, 0xbe, 0x89,
+                0xf8, 0xc7, 0x70, 0x6e, 0xa9, 0x12, 0x07, 0x0d,
+            },
+        },
+        .random_req[9] = {
+            .value = {
+                0x52, 0x06, 0x56, 0x09, 0xf4, 0xb2, 0xb9, 0x63,
+                0x3f, 0x2e, 0x59, 0x6c, 0x6b, 0x43, 0xb6, 0xc0,
+            },
+        },
+        .random_rsp[9] = {
+            .value = {
+                0x36, 0xb0, 0x33, 0x84, 0x52, 0xd1, 0x60, 0xac,
+                0x37, 0x81, 0x6b, 0x18, 0x5f, 0xfc, 0x61, 0xb1,
+            },
+        },
+        .confirm_req[10] = {
+            .value = {
+                0xc8, 0x55, 0xb7, 0x9e, 0x3e, 0xf0, 0x26, 0xa4,
+                0x55, 0xb3, 0x1d, 0x4d, 0xa1, 0x5d, 0xa9, 0xaf,
+            },
+        },
+        .confirm_rsp[10] = {
+            .value = {
+                0xb7, 0xb9, 0x6b, 0x8e, 0xef, 0xd3, 0xbc, 0x58,
+                0x10, 0xbe, 0x5a, 0x9a, 0x4d, 0xbc, 0xec, 0xe3,
+            },
+        },
+        .random_req[10] = {
+            .value = {
+                0x55, 0xa1, 0xf4, 0xd7, 0xfa, 0xe1, 0x84, 0x03,
+                0xed, 0xb6, 0x95, 0x63, 0x4b, 0x93, 0x93, 0xc2,
+            },
+        },
+        .random_rsp[10] = {
+            .value = {
+                0x72, 0xa9, 0xe5, 0xf7, 0x48, 0x1f, 0x64, 0x71,
+                0xd9, 0x81, 0xf0, 0xc5, 0x4d, 0x38, 0xac, 0x9a,
+            },
+        },
+        .confirm_req[11] = {
+            .value = {
+                0x12, 0x37, 0x56, 0xa6, 0x66, 0xa1, 0x23, 0xee,
+                0xe3, 0x1e, 0x20, 0x66, 0x66, 0x85, 0x7c, 0xa8,
+            },
+        },
+        .confirm_rsp[11] = {
+            .value = {
+                0x74, 0xaa, 0xbb, 0x5a, 0xdf, 0xd9, 0xc4, 0xaf,
+                0xe4, 0xa7, 0xe6, 0x4b, 0x45, 0x97, 0xf8, 0x7d,
+            },
+        },
+        .random_req[11] = {
+            .value = {
+                0x29, 0xce, 0xcc, 0xb7, 0xb2, 0x1e, 0x0e, 0xa8,
+                0x48, 0x90, 0x43, 0x6d, 0x34, 0xa4, 0xa3, 0x12,
+            },
+        },
+        .random_rsp[11] = {
+            .value = {
+                0x2e, 0xaf, 0x4c, 0x63, 0x84, 0x2c, 0x62, 0x67,
+                0x68, 0x8f, 0x0b, 0xfd, 0xff, 0xef, 0x15, 0x26,
+            },
+        },
+        .confirm_req[12] = {
+            .value = {
+                0xec, 0xcf, 0x6a, 0x60, 0x77, 0x04, 0x2c, 0x62,
+                0x42, 0xf0, 0x21, 0xfd, 0x53, 0xd6, 0x8a, 0xe8,
+            },
+        },
+        .confirm_rsp[12] = {
+            .value = {
+                0x2c, 0x13, 0x65, 0x69, 0xd7, 0x66, 0x04, 0x13,
+                0x3c, 0xa8, 0xfb, 0xe5, 0x76, 0xbb, 0x4f, 0x48,
+            },
+        },
+        .random_req[12] = {
+            .value = {
+                0x0d, 0x93, 0x30, 0xe2, 0x76, 0xf1, 0xbc, 0x24,
+                0x61, 0x0d, 0xcd, 0xef, 0x33, 0x98, 0xe2, 0x3b,
+            },
+        },
+        .random_rsp[12] = {
+            .value = {
+                0xb6, 0x32, 0x69, 0x81, 0xc0, 0x81, 0x46, 0xae,
+                0x8d, 0x5a, 0x17, 0xb5, 0xc0, 0x0f, 0x9f, 0x4e,
+            },
+        },
+        .confirm_req[13] = {
+            .value = {
+                0x89, 0x96, 0x22, 0x0c, 0x76, 0xdf, 0x27, 0x13,
+                0x96, 0x5a, 0x0c, 0x88, 0x65, 0x18, 0x74, 0x52,
+            },
+        },
+        .confirm_rsp[13] = {
+            .value = {
+                0x1c, 0x77, 0x25, 0x22, 0xc0, 0x28, 0x88, 0x45,
+                0x29, 0x62, 0x7a, 0x8e, 0xc0, 0x2a, 0x5c, 0xd8,
+            },
+        },
+        .random_req[13] = {
+            .value = {
+                0xcc, 0x84, 0xb6, 0x98, 0x3e, 0xf9, 0x09, 0xd2,
+                0x71, 0x47, 0x56, 0xb1, 0x09, 0xf5, 0xd2, 0x0b,
+            },
+        },
+        .random_rsp[13] = {
+            .value = {
+                0xf0, 0xcf, 0x1c, 0xa6, 0x24, 0xcd, 0xfa, 0x42,
+                0xa4, 0x93, 0x8b, 0xa0, 0xe3, 0x42, 0x72, 0x51,
+            },
+        },
+        .confirm_req[14] = {
+            .value = {
+                0xab, 0xb0, 0xa3, 0x80, 0x0d, 0xcb, 0x8e, 0xf6,
+                0x6c, 0x07, 0x50, 0xe9, 0x8a, 0x85, 0x02, 0xae,
+            },
+        },
+        .confirm_rsp[14] = {
+            .value = {
+                0xf6, 0x52, 0xd8, 0x34, 0x15, 0x62, 0x9f, 0x6e,
+                0x2b, 0x52, 0xdc, 0x1c, 0x70, 0x17, 0x0a, 0x31,
+            },
+        },
+        .random_req[14] = {
+            .value = {
+                0x8d, 0xc9, 0x0a, 0x45, 0xe9, 0x81, 0x0d, 0x5e,
+                0xbb, 0xd8, 0x94, 0x29, 0x68, 0x42, 0x44, 0xe2,
+            },
+        },
+        .random_rsp[14] = {
+            .value = {
+                0x96, 0x2a, 0x35, 0x39, 0x09, 0xf7, 0x66, 0x5a,
+                0xb6, 0x33, 0x77, 0x6d, 0xba, 0xd3, 0x8a, 0xfb,
+            },
+        },
+        .confirm_req[15] = {
+            .value = {
+                0x53, 0x08, 0x9b, 0x37, 0xc3, 0x79, 0xe6, 0x8c,
+                0x42, 0x30, 0x94, 0x73, 0x6f, 0x39, 0x64, 0x20,
+            },
+        },
+        .confirm_rsp[15] = {
+            .value = {
+                0x4d, 0xb7, 0xe9, 0x50, 0x8e, 0x0f, 0xe0, 0xd5,
+                0x3e, 0xf6, 0x32, 0xdd, 0xb8, 0x18, 0x77, 0xd3,
+            },
+        },
+        .random_req[15] = {
+            .value = {
+                0x8d, 0x49, 0x14, 0xdd, 0x95, 0x57, 0x55, 0x14,
+                0x48, 0x97, 0xd3, 0x73, 0x29, 0xa0, 0xb9, 0x2b,
+            },
+        },
+        .random_rsp[15] = {
+            .value = {
+                0xcf, 0x38, 0x8b, 0xab, 0xe4, 0x2b, 0x3f, 0x13,
+                0xc3, 0xfb, 0x07, 0xee, 0x0e, 0x33, 0x2f, 0x04,
+            },
+        },
+        .confirm_req[16] = {
+            .value = {
+                0xc6, 0x58, 0x13, 0x19, 0x56, 0x06, 0x52, 0x4b,
+                0x3d, 0x5e, 0x9d, 0xa8, 0x48, 0xf2, 0x40, 0xf3,
+            },
+        },
+        .confirm_rsp[16] = {
+            .value = {
+                0xbb, 0x93, 0xd2, 0xed, 0x89, 0x66, 0xa5, 0x1c,
+                0xc9, 0x2a, 0x42, 0x2c, 0xff, 0x4a, 0x80, 0x84,
+            },
+        },
+        .random_req[16] = {
+            .value = {
+                0x3d, 0x9c, 0x11, 0x2a, 0xd3, 0xce, 0x4b, 0x20,
+                0xf2, 0xfb, 0xdd, 0x18, 0x4d, 0x7c, 0x58, 0xb6,
+            },
+        },
+        .random_rsp[16] = {
+            .value = {
+                0xda, 0x80, 0x63, 0x9d, 0xa2, 0x73, 0x61, 0xdd,
+                0x9a, 0x45, 0x91, 0x4d, 0x78, 0x39, 0x54, 0x75,
+            },
+        },
+        .confirm_req[17] = {
+            .value = {
+                0x2e, 0xe4, 0x44, 0xe8, 0xdb, 0xc2, 0xbd, 0x62,
+                0xd1, 0xc4, 0x23, 0x4e, 0x5f, 0x65, 0xb6, 0x3b,
+            },
+        },
+        .confirm_rsp[17] = {
+            .value = {
+                0x19, 0x91, 0xa3, 0xc7, 0x3b, 0x68, 0x12, 0x24,
+                0xcd, 0xd6, 0x02, 0xf5, 0xcd, 0x19, 0x6c, 0x88,
+            },
+        },
+        .random_req[17] = {
+            .value = {
+                0xf0, 0x28, 0x18, 0xe8, 0xa7, 0x3e, 0xd8, 0x21,
+                0x42, 0x58, 0xb3, 0x72, 0xa0, 0x34, 0x89, 0x04,
+            },
+        },
+        .random_rsp[17] = {
+            .value = {
+                0xe9, 0xff, 0x0b, 0x9a, 0xfd, 0x29, 0x95, 0x37,
+                0x89, 0x2c, 0x84, 0xfa, 0x02, 0xa0, 0xb6, 0xeb,
+            },
+        },
+        .confirm_req[18] = {
+            .value = {
+                0x4f, 0x90, 0x70, 0xbe, 0xc4, 0x81, 0x9f, 0xc1,
+                0x74, 0xa3, 0x01, 0x2e, 0x78, 0x7a, 0xe2, 0x61,
+            },
+        },
+        .confirm_rsp[18] = {
+            .value = {
+                0xbb, 0xd5, 0x91, 0xec, 0x81, 0xe0, 0x9b, 0x5e,
+                0xe9, 0xd2, 0x93, 0x57, 0xa8, 0x27, 0xdd, 0x9b,
+            },
+        },
+        .random_req[18] = {
+            .value = {
+                0x78, 0xa4, 0x35, 0x1a, 0xbc, 0xa7, 0x19, 0x8c,
+                0x96, 0x8f, 0x63, 0x9d, 0x11, 0xee, 0x27, 0x44,
+            },
+        },
+        .random_rsp[18] = {
+            .value = {
+                0x39, 0x5b, 0x71, 0xfd, 0x7e, 0x39, 0x6b, 0xbe,
+                0xaf, 0xe1, 0x55, 0x90, 0xa6, 0x58, 0xec, 0xc5,
+            },
+        },
+        .confirm_req[19] = {
+            .value = {
+                0x91, 0x43, 0xe5, 0xc8, 0x26, 0x0c, 0x8c, 0x6c,
+                0xf3, 0xd1, 0x30, 0xb3, 0x22, 0x94, 0x4c, 0x67,
+            },
+        },
+        .confirm_rsp[19] = {
+            .value = {
+                0x51, 0xc4, 0x3e, 0x09, 0xca, 0x03, 0xbe, 0x2c,
+                0xe8, 0x1a, 0x5d, 0x07, 0x12, 0x14, 0x2d, 0x43,
+            },
+        },
+        .random_req[19] = {
+            .value = {
+                0x2f, 0xa1, 0x20, 0xde, 0xf5, 0xb4, 0xa6, 0x92,
+                0x31, 0xe9, 0x86, 0x63, 0xef, 0xc1, 0x85, 0x3b,
+            },
+        },
+        .random_rsp[19] = {
+            .value = {
+                0x41, 0xd0, 0xd0, 0x96, 0x93, 0xd1, 0xcb, 0xed,
+                0xab, 0x27, 0xd5, 0x88, 0x5e, 0xe6, 0x5e, 0x5c,
+            },
+        },
+        .dhkey_check_req = {
+            .value = {
+                0xec, 0xc5, 0x5f, 0xf3, 0xae, 0xfe, 0x79, 0x65,
+                0x17, 0x5a, 0x60, 0xf7, 0x36, 0x4f, 0x90, 0x45,
+            },
+        },
+        .dhkey_check_rsp = {
+            .value = {
+                0xa7, 0x45, 0x7a, 0x54, 0x1b, 0x64, 0x08, 0x60,
+                0x51, 0x7d, 0x74, 0x27, 0x48, 0xa2, 0xf1, 0x0f,
+            },
+        },
+        .id_info_req = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_req = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+            },
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0x36, 0x9a, 0xd9, 0x25, 0x5c, 0xdb, 0x78, 0xdc,
+                0x1d, 0x2c, 0x83, 0xf7, 0xde, 0x99, 0xa0, 0x66,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0x61, 0x6e, 0x9a, 0x26, 0xc5, 0xd0, 0x85, 0xdc,
+                0xea, 0x9d, 0xca, 0x3b, 0x17, 0xd7, 0x43, 0x80,
+            },
+        },
+        .ltk = {
+            0xd6, 0x02, 0xba, 0x3d, 0xa2, 0xce, 0x93, 0x1a,
+            0xfd, 0xd6, 0xb5, 0x54, 0x90, 0xc4, 0x2a, 0x8f,
+        },
+        .pair_alg = BLE_SM_PAIR_ALG_PASSKEY,
+        .authenticated = 1,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_DISP,
+                .passkey = 222333,
+            },
+        },
+    };
+    ble_sm_test_util_peer_sc_good(&params);
+}
+
+/**
+ * Secure connections pairing
+ * Master: peer
+ * Pair algorithm: numeric comparison
+ * Initiator IO capabilities: 1
+ * Responder IO capabilities: 1
+ * Bonding: true
+ * Initiator address type: 0
+ * Responder address type: 0
+ * Initiator key distribution: 5
+ * Responder key distribution: 7
+ */
+TEST_CASE(ble_sm_sc_peer_nc_iio1_rio1_b1_iat0_rat0_ik5_rk7)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
+        },
+        .resp_id_addr = {
+            0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+        },
+        .pair_req = {
+            .io_cap = 0x01,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x0d,
+            .resp_key_dist = 0x0f,
+        },
+        .pair_rsp = {
+            .io_cap = 0x01,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x05,
+            .resp_key_dist = 0x07,
+        },
+        .our_priv_key = {
+            0xd6, 0x2f, 0x4f, 0x6b, 0xeb, 0xfc, 0xbd, 0xee,
+            0x9b, 0x94, 0xd7, 0x15, 0x98, 0xc6, 0x0c, 0x83,
+            0x9b, 0xc7, 0xa2, 0x45, 0xfd, 0x00, 0xe8, 0xa4,
+            0x52, 0xe9, 0x70, 0x2f, 0xd7, 0x62, 0xf1, 0xa4,
+        },
+        .public_key_req = {
+            .x = {
+                0x41, 0x0d, 0x95, 0x8a, 0x68, 0xb8, 0xcf, 0x07,
+                0x58, 0x25, 0x5f, 0x97, 0xd2, 0x99, 0x71, 0x44,
+                0x06, 0xfc, 0x9c, 0x4d, 0xd1, 0x74, 0x80, 0xed,
+                0x49, 0xd1, 0x36, 0x6b, 0x55, 0x8b, 0x54, 0x3b,
+            },
+            .y = {
+                0x0f, 0x1a, 0x61, 0x45, 0xe5, 0x4b, 0x11, 0x13,
+                0xb3, 0x15, 0x87, 0x09, 0xec, 0x16, 0xf8, 0x41,
+                0x2e, 0xe2, 0x15, 0x93, 0x14, 0x56, 0x9f, 0xcd,
+                0x60, 0x7d, 0x92, 0xec, 0xd3, 0xb5, 0x85, 0xc5,
+            },
+        },
+        .public_key_rsp = {
+            .x = {
+                0xbc, 0x6a, 0xcf, 0xc6, 0x8a, 0x3a, 0xdc, 0x89,
+                0xdd, 0xa9, 0xaf, 0x29, 0xc7, 0xaf, 0xe2, 0x8b,
+                0x25, 0xee, 0xce, 0xa6, 0x10, 0x1d, 0x33, 0x2f,
+                0xd5, 0xfc, 0x30, 0xb8, 0xb1, 0x7b, 0xb1, 0x6e,
+            },
+            .y = {
+                0x1a, 0xc6, 0x42, 0x36, 0x98, 0x40, 0x4f, 0x90,
+                0x82, 0xa0, 0x10, 0x3a, 0xa5, 0x0f, 0xcf, 0x57,
+                0xd2, 0x2e, 0x80, 0x9d, 0x61, 0xc7, 0x21, 0xac,
+                0x47, 0x5b, 0x93, 0x75, 0x02, 0x30, 0x40, 0x14,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0x73, 0xc8, 0x56, 0x5e, 0x33, 0x37, 0x26, 0xb6,
+                0x00, 0x65, 0x9c, 0xa1, 0xee, 0xbf, 0x61, 0xf6,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0x7c, 0x23, 0x03, 0x70, 0x54, 0xa2, 0x70, 0xe4,
+                0x2d, 0xe9, 0x88, 0x6f, 0x40, 0xd6, 0x2f, 0xb2,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0x2d, 0x9f, 0xe8, 0x1d, 0xf2, 0x4e, 0x2e, 0x58,
+                0x16, 0x8c, 0x83, 0x89, 0x92, 0x70, 0xa2, 0xba,
+            },
+        },
+        .dhkey_check_req = {
+            .value = {
+                0xc0, 0x8a, 0x1c, 0xff, 0x7f, 0xd6, 0xbc, 0xee,
+                0x19, 0xa5, 0xc6, 0x3a, 0xbd, 0x48, 0x4b, 0xc3,
+            },
+        },
+        .dhkey_check_rsp = {
+            .value = {
+                0x38, 0x36, 0x83, 0xd5, 0x1a, 0xfb, 0xe6, 0x3c,
+                0x80, 0x0c, 0x81, 0x81, 0x78, 0x12, 0x41, 0x38,
+            },
+        },
+        .id_info_req = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_req = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x33, 0x22, 0x11, 0x00, 0x45, 0x0a,
+            },
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0x52, 0xf4, 0xcc, 0x2f, 0xc6, 0xc1, 0xdb, 0x07,
+                0xa5, 0x38, 0xc1, 0x09, 0x82, 0x2e, 0xa3, 0x53,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0xc1, 0xa3, 0x62, 0x6a, 0x9e, 0xaa, 0x37, 0xd9,
+                0x65, 0x9f, 0x7f, 0x5d, 0x62, 0x0c, 0x1c, 0x6c,
+            },
+        },
+        .ltk = {
+            0xd8, 0x7f, 0x0a, 0x94, 0x41, 0xa5, 0xfd, 0x84,
+            0x15, 0x01, 0xb7, 0x2a, 0x7a, 0xe4, 0xfd, 0xfb,
+        },
+        .pair_alg = BLE_SM_PAIR_ALG_NUMCMP,
+        .authenticated = 1,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_NUMCMP,
+                .numcmp_accept = 1,
+            },
+            .exp_numcmp = 516214,
+        },
+    };
+    ble_sm_test_util_peer_sc_good(&params);
+}
+
+/**
+ * Secure connections pairing
+ * Master: us
+ * Pair algorithm: just works
+ * Initiator IO capabilities: 3
+ * Responder IO capabilities: 4
+ * Bonding: true
+ * Initiator address type: 0
+ * Responder address type: 0
+ * Initiator key distribution: 7
+ * Responder key distribution: 5
+ */
+TEST_CASE(ble_sm_sc_us_jw_iio3_rio4_b1_iat0_rat0_ik7_rk5)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
+        },
+        .resp_id_addr = {
+            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
+        },
+        .pair_req = {
+            .io_cap = 0x03,
+            .oob_data_flag = 0x00,
+            .authreq = 0x09,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_rsp = {
+            .io_cap = 0x04,
+            .oob_data_flag = 0x00,
+            .authreq = 0x09,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x05,
+        },
+        .our_priv_key = {
+            0xaf, 0xce, 0x12, 0x45, 0xa8, 0xe0, 0xa9, 0x45,
+            0x8a, 0x56, 0xc5, 0xbf, 0x3b, 0xf9, 0x04, 0x69,
+            0xf2, 0xf9, 0xe4, 0xd4, 0x7e, 0xb7, 0xc9, 0x65,
+            0xb1, 0x68, 0x3e, 0xab, 0xcd, 0x8e, 0x6f, 0x1f,
+        },
+        .public_key_req = {
+            .x = {
+                0x45, 0xca, 0xda, 0xe3, 0x65, 0x7c, 0xf5, 0x37,
+                0x36, 0x66, 0x8b, 0x3b, 0x54, 0xb9, 0x2b, 0xb2,
+                0x09, 0xd5, 0x6e, 0xe0, 0x04, 0x1d, 0xd6, 0x49,
+                0xff, 0x55, 0x41, 0x35, 0xa0, 0x2f, 0x12, 0xee,
+            },
+            .y = {
+                0x65, 0x41, 0xd3, 0x7b, 0x59, 0xf2, 0xaf, 0x94,
+                0x78, 0xd8, 0x63, 0xc4, 0x9b, 0x9a, 0x9a, 0x92,
+                0x33, 0x0f, 0x14, 0x67, 0x98, 0x51, 0x9d, 0xff,
+                0xef, 0x59, 0xb7, 0x17, 0xc2, 0x16, 0x72, 0x18,
+            },
+        },
+        .public_key_rsp = {
+            .x = {
+                0x9e, 0x44, 0x09, 0x57, 0xb7, 0x01, 0x78, 0x5b,
+                0x4e, 0x50, 0x0d, 0x99, 0x0d, 0x52, 0x88, 0x24,
+                0x19, 0xf5, 0x40, 0x53, 0x06, 0x1e, 0x68, 0xd0,
+                0xfd, 0xd2, 0x84, 0x8b, 0xae, 0x9d, 0xf7, 0xd9,
+            },
+            .y = {
+                0xc2, 0xe7, 0xe0, 0x01, 0xb3, 0x2a, 0x1b, 0x01,
+                0x19, 0xd1, 0x14, 0xb5, 0xc8, 0x98, 0x02, 0x2a,
+                0xbe, 0x6b, 0x33, 0x1a, 0x99, 0x18, 0x77, 0x23,
+                0xd4, 0x8b, 0x8c, 0x09, 0xf5, 0x77, 0x20, 0xa0,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0xbd, 0x85, 0xbe, 0x80, 0xd9, 0x77, 0x16, 0xa3,
+                0x65, 0x1a, 0xdf, 0xff, 0x5a, 0x6f, 0x8b, 0x37,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0xb5, 0x59, 0x7c, 0x8e, 0x7b, 0x01, 0xac, 0x09,
+                0x8f, 0xe8, 0x97, 0x98, 0x8d, 0x3f, 0xb7, 0x63,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0x86, 0x1f, 0x76, 0x11, 0x2e, 0x83, 0xed, 0x99,
+                0x9b, 0xc0, 0x9a, 0xab, 0x7f, 0x94, 0x20, 0xcb,
+            },
+        },
+        .dhkey_check_req = {
+            .value = {
+                0xe0, 0x9f, 0x87, 0x87, 0x9f, 0x82, 0xc5, 0x06,
+                0x5f, 0x11, 0xfa, 0xa0, 0xe3, 0xbf, 0x72, 0xf2,
+            },
+        },
+        .dhkey_check_rsp = {
+            .value = {
+                0x26, 0xc2, 0xf1, 0xb9, 0xf1, 0xc2, 0xbd, 0xcb,
+                0xdb, 0x94, 0x96, 0x8e, 0x08, 0xcc, 0x53, 0xd4,
+            },
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0x74, 0x14, 0xcd, 0x5a, 0x49, 0x2e, 0xb6, 0x0d,
+                0xc6, 0x82, 0xb0, 0x0f, 0x9c, 0xe6, 0xe5, 0x41,
+            },
+        },
+        .id_info_rsp = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_rsp = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0xfb, 0x93, 0xa2, 0xb7, 0x4d, 0x0e, 0xcc, 0x92,
+                0xe4, 0xbf, 0x5b, 0x3c, 0x6d, 0x87, 0x5b, 0x2d,
+            },
+        },
+        .ltk = {
+            0x2e, 0x6c, 0x8b, 0xdb, 0x9e, 0x19, 0x3e, 0x3d,
+            0x4d, 0x6d, 0x29, 0xbc, 0x89, 0xca, 0x57, 0xed,
+        },
+        .pair_alg = BLE_SM_PAIR_ALG_JW,
+        .authenticated = 0,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_NONE,
+            },
+        },
+    };
+    ble_sm_test_util_us_sc_good(&params);
+}
+
+/**
+ * Secure connections pairing
+ * Master: us
+ * Pair algorithm: passkey entry
+ * Initiator IO capabilities: 2
+ * Responder IO capabilities: 4
+ * Bonding: true
+ * Initiator address type: 0
+ * Responder address type: 0
+ * Initiator key distribution: 7
+ * Responder key distribution: 5
+ */
+TEST_CASE(ble_sm_sc_us_pk_iio2_rio4_b1_iat0_rat0_ik7_rk5)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
+        },
+        .resp_id_addr = {
+            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
+        },
+        .pair_req = {
+            .io_cap = 0x02,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_rsp = {
+            .io_cap = 0x04,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x05,
+        },
+        .our_priv_key = {
+            0xb1, 0x6b, 0x4f, 0x81, 0xbc, 0xe3, 0x60, 0x9e,
+            0x00, 0x20, 0xf1, 0x73, 0x3e, 0xfb, 0xcc, 0x6e,
+            0x8c, 0xb6, 0xd2, 0x51, 0xd9, 0x36, 0x8a, 0x6d,
+            0xca, 0x8c, 0xd7, 0xbe, 0x96, 0x03, 0xdf, 0xd6,
+        },
+        .public_key_req = {
+            .x = {
+                0xe5, 0x0f, 0x02, 0x0a, 0x37, 0x90, 0x94, 0x5a,
+                0x06, 0x21, 0xf7, 0xbc, 0xd5, 0xbe, 0xb9, 0x24,
+                0x8a, 0x35, 0xfd, 0xf8, 0x5e, 0xe2, 0x70, 0xd5,
+                0x5a, 0xe8, 0xe7, 0xdd, 0x13, 0x90, 0xeb, 0xd4,
+            },
+            .y = {
+                0x41, 0xc8, 0x51, 0x1a, 0x25, 0x44, 0x01, 0x53,
+                0x42, 0x74, 0x07, 0x9c, 0x18, 0xe6, 0x3b, 0x8a,
+                0xce, 0x7a, 0x37, 0x1f, 0x18, 0x5c, 0x02, 0x7c,
+                0x67, 0x16, 0xf5, 0x30, 0x2b, 0x31, 0xa9, 0xc7,
+            },
+        },
+        .public_key_rsp = {
+            .x = {
+                0x1b, 0xd2, 0x03, 0x79, 0xb4, 0x9b, 0x0a, 0xd7,
+                0x1b, 0x28, 0x73, 0x2a, 0xd7, 0xe6, 0xa0, 0xd4,
+                0x2d, 0x95, 0x8d, 0x29, 0xaf, 0x6a, 0xab, 0xee,
+                0xa0, 0x0d, 0x13, 0x4d, 0xe7, 0x16, 0x76, 0x91,
+            },
+            .y = {
+                0x2a, 0x26, 0x2c, 0x50, 0x55, 0xd1, 0x2b, 0x83,
+                0xf6, 0x5f, 0xdb, 0x99, 0x5f, 0x85, 0xf6, 0x78,
+                0x1c, 0x14, 0xed, 0xd3, 0x70, 0x5e, 0xe5, 0x2c,
+                0x05, 0x1e, 0x5c, 0xec, 0xf8, 0x65, 0x43, 0x49,
+            },
+        },
+        .confirm_req[0] = {
+            .value = {
+                0x55, 0x2c, 0xaa, 0x41, 0x59, 0x42, 0x4d, 0xfe,
+                0x47, 0x74, 0xcd, 0x2b, 0x11, 0xab, 0x21, 0xe6,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0x6a, 0x3c, 0x45, 0xf5, 0xb2, 0xe2, 0x04, 0x30,
+                0xde, 0xd6, 0x3c, 0x6d, 0x85, 0x00, 0x00, 0x2c,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0x78, 0x06, 0x04, 0x60, 0x76, 0xe9, 0xc4, 0x5a,
+                0xfb, 0x34, 0x44, 0xae, 0x45, 0xa0, 0x84, 0xde,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0x91, 0xc8, 0xfd, 0x1b, 0xb2, 0x85, 0x08, 0x76,
+                0xd3, 0xf1, 0xc4, 0xa0, 0xfa, 0x92, 0x8c, 0x94,
+            },
+        },
+        .confirm_req[1] = {
+            .value = {
+                0xb1, 0x2f, 0x68, 0x35, 0xa1, 0xa5, 0x84, 0xb1,
+                0x4f, 0x1a, 0xb1, 0xb5, 0xf0, 0xb2, 0xbe, 0x61,
+            },
+        },
+        .confirm_rsp[1] = {
+            .value = {
+                0x07, 0xd8, 0x43, 0x74, 0xe8, 0x42, 0xf3, 0xf1,
+                0x87, 0x3d, 0x9e, 0x92, 0xea, 0x33, 0xe8, 0x54,
+            },
+        },
+        .random_req[1] = {
+            .value = {
+                0x4c, 0xb7, 0xcc, 0x6d, 0x90, 0x9f, 0x1e, 0x2d,
+                0x9d, 0x1e, 0x52, 0xa7, 0xe0, 0x0c, 0x7b, 0xf7,
+            },
+        },
+        .random_rsp[1] = {
+            .value = {
+                0x5c, 0x32, 0x82, 0xc8, 0x76, 0x17, 0x3b, 0x18,
+                0x66, 0xda, 0xbf, 0xc3, 0x13, 0x49, 0x05, 0xfb,
+            },
+        },
+        .confirm_req[2] = {
+            .value = {
+                0x27, 0x61, 0x4d, 0x04, 0x64, 0xa9, 0x58, 0xf1,
+                0xe0, 0xf9, 0xe5, 0x78, 0x0b, 0x54, 0x89, 0x0a,
+            },
+        },
+        .confirm_rsp[2] = {
+            .value = {
+                0xe4, 0x8f, 0xdb, 0xc8, 0x35, 0xed, 0x4e, 0x7d,
+                0xbc, 0x92, 0x7f, 0x58, 0x02, 0xaa, 0xbf, 0x6b,
+            },
+        },
+        .random_req[2] = {
+            .value = {
+                0xfe, 0x85, 0x08, 0xe0, 0x35, 0x90, 0x13, 0xa9,
+                0xd3, 0xcf, 0xb6, 0x6d, 0x36, 0xaf, 0xbd, 0x59,
+            },
+        },
+        .random_rsp[2] = {
+            .value = {
+                0x47, 0x40, 0x8e, 0x97, 0xe3, 0xfe, 0x8f, 0x52,
+                0x29, 0x5e, 0x6b, 0x44, 0xdf, 0x0d, 0x60, 0xf4,
+            },
+        },
+        .confirm_req[3] = {
+            .value = {
+                0xac, 0xab, 0x13, 0x7c, 0x1a, 0x6e, 0x7a, 0xdb,
+                0xf6, 0xe8, 0x72, 0x9f, 0xc5, 0xc3, 0x99, 0x1b,
+            },
+        },
+        .confirm_rsp[3] = {
+            .value = {
+                0x79, 0xf2, 0xd1, 0x89, 0x5e, 0xa5, 0xa2, 0x90,
+                0xee, 0x25, 0x36, 0x81, 0x5a, 0x87, 0x20, 0x82,
+            },
+        },
+        .random_req[3] = {
+            .value = {
+                0xd4, 0x46, 0xa0, 0xc4, 0x3d, 0xae, 0x22, 0x06,
+                0xaf, 0x5d, 0x93, 0x96, 0xb7, 0x06, 0xc3, 0x61,
+            },
+        },
+        .random_rsp[3] = {
+            .value = {
+                0x5f, 0x81, 0x97, 0x8b, 0x52, 0x87, 0x1c, 0x67,
+                0xe0, 0x04, 0xcc, 0x50, 0xd9, 0x2b, 0x16, 0xb5,
+            },
+        },
+        .confirm_req[4] = {
+            .value = {
+                0x6c, 0x51, 0xc3, 0x61, 0x77, 0x7f, 0xf1, 0x05,
+                0x9e, 0x0f, 0xba, 0xfd, 0x32, 0x02, 0x09, 0x45,
+            },
+        },
+        .confirm_rsp[4] = {
+            .value = {
+                0x54, 0xe5, 0x24, 0x81, 0x62, 0x68, 0xe2, 0x45,
+                0x86, 0x2c, 0x11, 0x28, 0x15, 0xa8, 0x8e, 0x5b,
+            },
+        },
+        .random_req[4] = {
+            .value = {
+                0xbb, 0x29, 0x3a, 0xba, 0xe6, 0x4f, 0x06, 0xcf,
+                0xa3, 0x13, 0x27, 0xf2, 0xcb, 0xe4, 0xd2, 0xe6,
+            },
+        },
+        .random_rsp[4] = {
+            .value = {
+                0x50, 0xba, 0xd0, 0x0e, 0x26, 0xab, 0x04, 0xf8,
+                0xa2, 0x03, 0x1e, 0x63, 0x9a, 0xf7, 0x15, 0xdc,
+            },
+        },
+        .confirm_req[5] = {
+            .value = {
+                0x12, 0x3e, 0xfe, 0x5a, 0xb1, 0x09, 0x6f, 0x17,
+                0xb7, 0x77, 0x7e, 0x65, 0x88, 0xd4, 0x95, 0x56,
+            },
+        },
+        .confirm_rsp[5] = {
+            .value = {
+                0xc6, 0x9b, 0xac, 0xde, 0x7e, 0x03, 0x7a, 0xd3,
+                0xf1, 0xff, 0x3c, 0x4f, 0x4a, 0x85, 0xba, 0x73,
+            },
+        },
+        .random_req[5] = {
+            .value = {
+                0x17, 0xd5, 0x5e, 0x69, 0x30, 0x2c, 0x1f, 0x01,
+                0x87, 0x9c, 0xd6, 0xd2, 0xe4, 0x48, 0x8c, 0x84,
+            },
+        },
+        .random_rsp[5] = {
+            .value = {
+                0x9d, 0x54, 0x83, 0x4a, 0xcd, 0x93, 0x7c, 0x1e,
+                0x5b, 0xaf, 0xd2, 0x66, 0x8c, 0x2d, 0xaa, 0xc3,
+            },
+        },
+        .confirm_req[6] = {
+            .value = {
+                0xdc, 0x24, 0x69, 0xa8, 0xd3, 0xa9, 0x17, 0x11,
+                0x08, 0x37, 0x1a, 0x1e, 0x92, 0x03, 0xee, 0x36,
+            },
+        },
+        .confirm_rsp[6] = {
+            .value = {
+                0x98, 0xf8, 0x72, 0x71, 0x99, 0xa0, 0xbd, 0xcd,
+                0xb1, 0x97, 0x4c, 0x8a, 0xb8, 0xa8, 0x1a, 0x52,
+            },
+        },
+        .random_req[6] = {
+            .value = {
+                0xbf, 0xb1, 0x8e, 0xa5, 0x14, 0xe3, 0xeb, 0x9e,
+                0x29, 0x27, 0xe0, 0x19, 0xb1, 0xb2, 0x5c, 0xfe,
+            },
+        },
+        .random_rsp[6] = {
+            .value = {
+                0xae, 0x8a, 0x92, 0x78, 0x53, 0x7b, 0xdb, 0x8c,
+                0xec, 0x3a, 0x99, 0x2b, 0x94, 0xf1, 0x17, 0xfe,
+            },
+        },
+        .confirm_req[7] = {
+            .value = {
+                0xcf, 0xaf, 0x70, 0x73, 0x53, 0x65, 0x89, 0x57,
+                0x36, 0x98, 0xd2, 0x28, 0x86, 0x79, 0xfe, 0x85,
+            },
+        },
+        .confirm_rsp[7] = {
+            .value = {
+                0x0d, 0x2d, 0x77, 0x8a, 0x21, 0x11, 0xd9, 0x61,
+                0x9f, 0x80, 0x32, 0x8a, 0x32, 0x09, 0x42, 0x42,
+            },
+        },
+        .random_req[7] = {
+            .value = {
+                0x8b, 0xd2, 0x53, 0xcd, 0x96, 0xd1, 0x14, 0xb5,
+                0xea, 0x17, 0xb1, 0xa3, 0xa8, 0xfc, 0x3c, 0x2b,
+            },
+        },
+        .random_rsp[7] = {
+            .value = {
+                0xc2, 0x4f, 0x84, 0x60, 0x54, 0x79, 0x16, 0xed,
+                0x1a, 0x6e, 0x78, 0xa0, 0x99, 0x58, 0xf2, 0x94,
+            },
+        },
+        .confirm_req[8] = {
+            .value = {
+                0x9a, 0x4c, 0xbc, 0x9c, 0x55, 0x15, 0xa2, 0x4f,
+                0xa2, 0x5d, 0x3b, 0xa7, 0x43, 0xb3, 0x9c, 0x63,
+            },
+        },
+        .confirm_rsp[8] = {
+            .value = {
+                0xa3, 0xb1, 0x88, 0xa5, 0x70, 0xca, 0xa3, 0xa9,
+                0x67, 0x2a, 0xac, 0x99, 0x5e, 0x61, 0x68, 0xa0,
+            },
+        },
+        .random_req[8] = {
+            .value = {
+                0xcf, 0xcf, 0x5b, 0x94, 0xe0, 0xb2, 0x9d, 0x5a,
+                0x86, 0x71, 0x45, 0xce, 0xd9, 0xce, 0x13, 0xba,
+            },
+        },
+        .random_rsp[8] = {
+            .value = {
+                0x10, 0x96, 0x8a, 0x50, 0xa4, 0xd0, 0xaa, 0x5f,
+                0xd6, 0x32, 0xdb, 0x09, 0x7e, 0x22, 0x96, 0x42,
+            },
+        },
+        .confirm_req[9] = {
+            .value = {
+                0xf0, 0x90, 0x61, 0x25, 0x04, 0x29, 0x4f, 0xb6,
+                0x8b, 0xd5, 0x73, 0x49, 0xbd, 0xf7, 0x9b, 0xe7,
+            },
+        },
+        .confirm_rsp[9] = {
+            .value = {
+                0x5b, 0xe6, 0xb4, 0x3f, 0x1b, 0x77, 0x12, 0x75,
+                0x84, 0x94, 0xc6, 0x07, 0xfa, 0xa1, 0x41, 0x94,
+            },
+        },
+        .random_req[9] = {
+            .value = {
+                0x3d, 0x1a, 0xa3, 0x95, 0xec, 0x72, 0x84, 0xf4,
+                0xc5, 0xcd, 0xaa, 0x48, 0xe9, 0x0c, 0x0f, 0xe3,
+            },
+        },
+        .random_rsp[9] = {
+            .value = {
+                0x8a, 0x5a, 0x53, 0xfc, 0x07, 0x52, 0x01, 0xb9,
+                0xe9, 0x2d, 0xe7, 0x9d, 0x8c, 0x7c, 0xc7, 0xb3,
+            },
+        },
+        .confirm_req[10] = {
+            .value = {
+                0xe7, 0x8e, 0xc5, 0x08, 0x7f, 0x7e, 0xb8, 0xdc,
+                0x05, 0x88, 0x3a, 0x92, 0x5a, 0xf5, 0x9b, 0xa9,
+            },
+        },
+        .confirm_rsp[10] = {
+            .value = {
+                0xf7, 0xa2, 0xb6, 0xec, 0xcd, 0xef, 0xcb, 0xb7,
+                0x6f, 0xc3, 0xac, 0x17, 0xe2, 0xfd, 0xfa, 0x42,
+            },
+        },
+        .random_req[10] = {
+            .value = {
+                0x0d, 0xd1, 0xa2, 0x1d, 0xff, 0x74, 0xc5, 0x99,
+                0xe0, 0x67, 0x07, 0x99, 0x95, 0x75, 0x39, 0x76,
+            },
+        },
+        .random_rsp[10] = {
+            .value = {
+                0x2f, 0x13, 0xd1, 0x59, 0xfe, 0x20, 0x60, 0xf0,
+                0x02, 0x0c, 0xea, 0x79, 0xd7, 0x40, 0x86, 0x85,
+            },
+        },
+        .confirm_req[11] = {
+            .value = {
+                0x8b, 0x57, 0x87, 0xdd, 0xb1, 0xcc, 0x2d, 0x65,
+                0xc1, 0xba, 0xac, 0x88, 0x48, 0x23, 0xda, 0xe7,
+            },
+        },
+        .confirm_rsp[11] = {
+            .value = {
+                0xb3, 0xc4, 0x2e, 0xea, 0x33, 0xaf, 0x12, 0x9c,
+                0xb5, 0xab, 0xa1, 0x95, 0x30, 0xca, 0x46, 0x48,
+            },
+        },
+        .random_req[11] = {
+            .value = {
+                0x35, 0x57, 0xcd, 0xd5, 0xd2, 0xf8, 0xd7, 0xf2,
+                0x7b, 0xe3, 0xd7, 0xba, 0x31, 0xa5, 0xca, 0xfd,
+            },
+        },
+        .random_rsp[11] = {
+            .value = {
+                0xe2, 0x3b, 0x20, 0xbe, 0xec, 0xa5, 0x34, 0x3b,
+                0x76, 0x23, 0x53, 0x28, 0x36, 0xc4, 0x60, 0x13,
+            },
+        },
+        .confirm_req[12] = {
+            .value = {
+                0xc9, 0xfe, 0x03, 0x49, 0xe4, 0xff, 0x7e, 0xf7,
+                0x00, 0xd1, 0x2b, 0x13, 0xb1, 0x15, 0x6e, 0x92,
+            },
+        },
+        .confirm_rsp[12] = {
+            .value = {
+                0xbc, 0xa2, 0xf2, 0x03, 0x5c, 0xfd, 0x20, 0x7b,
+                0xd0, 0x1f, 0xd6, 0x50, 0xec, 0xc6, 0x7b, 0x31,
+            },
+        },
+        .random_req[12] = {
+            .value = {
+                0x04, 0x50, 0xea, 0xb8, 0xca, 0x36, 0x1a, 0x61,
+                0x92, 0xed, 0xa0, 0x67, 0x78, 0x15, 0x10, 0xb5,
+            },
+        },
+        .random_rsp[12] = {
+            .value = {
+                0x0c, 0x8e, 0x9d, 0x7b, 0x9d, 0x7e, 0xda, 0x23,
+                0xbb, 0x61, 0xd9, 0xff, 0x46, 0x77, 0x33, 0x1b,
+            },
+        },
+        .confirm_req[13] = {
+            .value = {
+                0x9a, 0xff, 0xd6, 0xe5, 0x1a, 0xc3, 0xd3, 0x37,
+                0x34, 0xeb, 0x3e, 0x3a, 0x8e, 0x0b, 0x86, 0xb4,
+            },
+        },
+        .confirm_rsp[13] = {
+            .value = {
+                0xf6, 0x32, 0x19, 0xb4, 0x08, 0x6b, 0x8a, 0x0f,
+                0xc9, 0x9c, 0x1b, 0x68, 0xb8, 0xa0, 0xd0, 0xc9,
+            },
+        },
+        .random_req[13] = {
+            .value = {
+                0x86, 0xeb, 0x5c, 0xf9, 0x33, 0x54, 0x7d, 0xe4,
+                0xa4, 0xe2, 0xe1, 0xf6, 0x6b, 0xea, 0x34, 0xed,
+            },
+        },
+        .random_rsp[13] = {
+            .value = {
+                0xad, 0x53, 0xa0, 0x6e, 0xde, 0x1d, 0xda, 0x99,
+                0x31, 0x45, 0xe5, 0x3a, 0x73, 0xa1, 0x5e, 0xe1,
+            },
+        },
+        .confirm_req[14] = {
+            .value = {
+                0x93, 0xd4, 0xe0, 0xaa, 0x0c, 0x91, 0xba, 0xde,
+                0xc9, 0x5c, 0x68, 0xb0, 0xce, 0xb6, 0x84, 0xcd,
+            },
+        },
+        .confirm_rsp[14] = {
+            .value = {
+                0x85, 0xc7, 0x05, 0x02, 0x21, 0x9d, 0x4c, 0x4c,
+                0x16, 0xf7, 0x8f, 0x7b, 0xaa, 0xb4, 0x8f, 0x37,
+            },
+        },
+        .random_req[14] = {
+            .value = {
+                0x84, 0xfd, 0xf1, 0x39, 0x1a, 0x9a, 0xa5, 0xb8,
+                0x49, 0xc0, 0x66, 0xdc, 0x33, 0x71, 0x32, 0x87,
+            },
+        },
+        .random_rsp[14] = {
+            .value = {
+                0x5d, 0xaf, 0x38, 0xcd, 0xb5, 0x83, 0xaa, 0xa0,
+                0xab, 0x30, 0x82, 0xed, 0x6f, 0xd2, 0x75, 0xe7,
+            },
+        },
+        .confirm_req[15] = {
+            .value = {
+                0x88, 0x12, 0xe8, 0x89, 0xd4, 0x52, 0x6d, 0xac,
+                0x61, 0x2a, 0x85, 0x85, 0x1e, 0x9c, 0x82, 0x21,
+            },
+        },
+        .confirm_rsp[15] = {
+            .value = {
+                0xc1, 0xe9, 0xcd, 0x21, 0x29, 0x6a, 0x78, 0xe4,
+                0x7b, 0x7d, 0x73, 0x25, 0x9e, 0x9b, 0x95, 0x8b,
+            },
+        },
+        .random_req[15] = {
+            .value = {
+                0x95, 0x87, 0x9d, 0x5a, 0x10, 0x14, 0xa0, 0xdf,
+                0x5e, 0x02, 0x22, 0x39, 0x23, 0xc9, 0xbc, 0xba,
+            },
+        },
+        .random_rsp[15] = {
+            .value = {
+                0x1b, 0x91, 0xe2, 0xdf, 0xca, 0xfe, 0x2b, 0x61,
+                0x33, 0x8c, 0x83, 0xbf, 0xcf, 0xc3, 0x72, 0xcc,
+            },
+        },
+        .confirm_req[16] = {
+            .value = {
+                0xce, 0xc9, 0x68, 0xf7, 0xea, 0x41, 0x18, 0x5c,
+                0x16, 0x6a, 0x98, 0x13, 0x0c, 0x10, 0xc2, 0xa3,
+            },
+        },
+        .confirm_rsp[16] = {
+            .value = {
+                0x97, 0x73, 0xc9, 0x72, 0x68, 0x99, 0x63, 0xed,
+                0x81, 0x3b, 0x5c, 0xee, 0x37, 0xfc, 0xca, 0xae,
+            },
+        },
+        .random_req[16] = {
+            .value = {
+                0x5b, 0x85, 0xb0, 0x1b, 0xc3, 0xde, 0x18, 0xba,
+                0xc1, 0xc7, 0x89, 0x99, 0xfe, 0xcd, 0xdb, 0x6a,
+            },
+        },
+        .random_rsp[16] = {
+            .value = {
+                0x5e, 0x1a, 0xcb, 0xbc, 0xda, 0x41, 0x06, 0x5a,
+                0x14, 0x34, 0x3a, 0xb1, 0xa1, 0x6f, 0xb2, 0xd8,
+            },
+        },
+        .confirm_req[17] = {
+            .value = {
+                0x1d, 0x59, 0x8a, 0xb0, 0x19, 0xe5, 0xff, 0x45,
+                0xb6, 0xc3, 0x33, 0x64, 0xd1, 0x6e, 0xee, 0xdd,
+            },
+        },
+        .confirm_rsp[17] = {
+            .value = {
+                0x4c, 0x9b, 0xe8, 0x68, 0x52, 0x34, 0xef, 0xe1,
+                0x84, 0xbd, 0x37, 0x85, 0x53, 0x0d, 0xd5, 0xc1,
+            },
+        },
+        .random_req[17] = {
+            .value = {
+                0xa6, 0xf7, 0x97, 0x18, 0x9a, 0x3e, 0x9d, 0xcf,
+                0x91, 0xa3, 0xa3, 0x8e, 0xda, 0x8f, 0x8f, 0x90,
+            },
+        },
+        .random_rsp[17] = {
+            .value = {
+                0x94, 0x10, 0x19, 0x17, 0x8d, 0x0a, 0x72, 0xfd,
+                0x24, 0x9d, 0xfd, 0x37, 0x4e, 0xdf, 0x4c, 0x30,
+            },
+        },
+        .confirm_req[18] = {
+            .value = {
+                0xfc, 0x64, 0x8a, 0x8b, 0x37, 0x17, 0x90, 0x6d,
+                0x25, 0x0e, 0xc6, 0x18, 0xc9, 0xc9, 0xc2, 0x2a,
+            },
+        },
+        .confirm_rsp[18] = {
+            .value = {
+                0x50, 0x98, 0x86, 0xf5, 0xc0, 0xda, 0x45, 0x2d,
+                0xea, 0xc8, 0x9d, 0x28, 0x04, 0xd8, 0x73, 0x6f,
+            },
+        },
+        .random_req[18] = {
+            .value = {
+                0x13, 0x10, 0x38, 0xe8, 0x17, 0x6d, 0x72, 0xd5,
+                0x94, 0xaf, 0xed, 0x4f, 0x23, 0xa0, 0x41, 0xfc,
+            },
+        },
+        .random_rsp[18] = {
+            .value = {
+                0xdf, 0xed, 0xf7, 0x08, 0xce, 0x64, 0xbc, 0x11,
+                0x41, 0x7a, 0xd9, 0xf7, 0x4a, 0xd9, 0x4a, 0x15,
+            },
+        },
+        .confirm_req[19] = {
+            .value = {
+                0xae, 0x24, 0x8f, 0xdf, 0xb0, 0x57, 0xc4, 0x9c,
+                0xe6, 0xae, 0x9b, 0xc2, 0x4d, 0x3d, 0x1c, 0xcb,
+            },
+        },
+        .confirm_rsp[19] = {
+            .value = {
+                0xcc, 0x5c, 0xa3, 0xbe, 0xd7, 0x83, 0xee, 0x60,
+                0x80, 0xff, 0x5f, 0x1a, 0x07, 0xbf, 0x4c, 0x33,
+            },
+        },
+        .random_req[19] = {
+            .value = {
+                0x93, 0xc3, 0x62, 0x06, 0xcb, 0xe5, 0xb0, 0x01,
+                0x02, 0x18, 0xa2, 0x50, 0x4c, 0x73, 0xa2, 0x27,
+            },
+        },
+        .random_rsp[19] = {
+            .value = {
+                0x11, 0x2a, 0xd3, 0x06, 0x28, 0x9c, 0xdf, 0x73,
+                0xa5, 0xa4, 0xe5, 0x1e, 0x07, 0xcf, 0xee, 0x71,
+            },
+        },
+        .dhkey_check_req = {
+            .value = {
+                0x73, 0xa0, 0x40, 0x58, 0x78, 0x20, 0x5f, 0x2c,
+                0xf4, 0x19, 0x23, 0xa8, 0x74, 0xbd, 0xc2, 0x3e,
+            },
+        },
+        .dhkey_check_rsp = {
+            .value = {
+                0x5a, 0x30, 0xbc, 0xce, 0xec, 0xdf, 0xf0, 0x32,
+                0x3c, 0x18, 0xa3, 0xd3, 0x3f, 0x20, 0x87, 0x10,
+            },
+        },
+        .sign_info_req = {
+            .sig_key = {
+                0x2e, 0x81, 0x09, 0xde, 0x32, 0xc5, 0x28, 0x34,
+                0xe1, 0x45, 0x4a, 0x35, 0x49, 0xef, 0xa2, 0xed,
+            },
+        },
+        .id_info_rsp = {
+            .irk = {
+                0xef, 0x8d, 0xe2, 0x16, 0x4f, 0xec, 0x43, 0x0d,
+                0xbf, 0x5b, 0xdd, 0x34, 0xc0, 0x53, 0x1e, 0xb8,
+            },
+        },
+        .id_addr_info_rsp = {
+            .addr_type = 0,
+            .bd_addr = {
+                0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
+            },
+        },
+        .sign_info_rsp = {
+            .sig_key = {
+                0x90, 0x3d, 0x26, 0x65, 0xc1, 0xd1, 0x5a, 0x9d,
+                0xda, 0xab, 0x0d, 0x00, 0x05, 0x0e, 0x6c, 0x5d,
+            },
+        },
+        .ltk = {
+            0xf1, 0x41, 0x1a, 0x5b, 0x60, 0xc1, 0x43, 0xc6,
+            0x80, 0x34, 0x5e, 0x7f, 0xd8, 0x0c, 0x75, 0xdc,
+        },
+        .pair_alg = BLE_SM_PAIR_ALG_PASSKEY,
+        .authenticated = 1,
+        .passkey_info = {
+            .passkey = {
+                .action = BLE_SM_IOACT_INPUT,
+                .passkey = 516645,
+            },
+        },
+    };
+    ble_sm_test_util_us_sc_good(&params);
+}
+
+/**
+ * Secure connections pairing
+ * Master: us
+ * Pair algorithm: passkey entry
+ * Initiator IO capabilities: 0
+ * Responder IO capabilities: 4
+ * Bonding: true
+ * Initiator address type: 0
+ * Responder address type: 0
+ * Initiator key distribution: 7
+ * Responder key distribution: 5
+ */
+TEST_CASE(ble_sm_sc_us_pk_iio0_rio4_b1_iat0_rat0_ik7_rk5)
+{
+    struct ble_sm_test_params params;
+
+    params = (struct ble_sm_test_params) {
+        .init_id_addr = {
+            0x01, 0x01, 0x01, 0x07, 0x08, 0x01,
+        },
+        .resp_id_addr = {
+            0xca, 0x61, 0xa0, 0x67, 0x94, 0xe0,
+        },
+        .pair_req = {
+            .io_cap = 0x00,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x07,
+        },
+        .pair_rsp = {
+            .io_cap = 0x04,
+            .oob_data_flag = 0x00,
+            .authreq = 0x0d,
+            .max_enc_key_size = 0x10,
+            .init_key_dist = 0x07,
+            .resp_key_dist = 0x05,
+        },
+        .our_priv_key = {
+            0xb1, 0x6b, 0x4f, 0x81, 0xbc, 0xe3, 0x60, 0x9e,
+            0x00, 0x20, 0xf1, 0x73, 0x3e, 0xfb, 0xcc, 0x6e,
+            0x8c, 0xb6, 0xd2, 0x51, 0xd9, 0x36, 0x8a, 0x6d,
+            0xca, 0x8c, 0xd7, 0xbe, 0x96, 0x03, 0xdf, 0xd6,
+        },
+        .public_key_req = {
+            .x = {
+                0xe5, 0x0f, 0x02, 0x0a, 0x37, 0x90, 0x94, 0x5a,
+                0x06, 0x21, 0xf7, 0xbc, 0xd5, 0xbe, 0xb9, 0x24,
+                0x8a, 0x35, 0xfd, 0xf8, 0x5e, 0xe2, 0x70, 0xd5,
+                0x5a, 0xe8, 0xe7, 0xdd, 0x13, 0x90, 0xeb, 0xd4,
+            },
+            .y = {
+                0x41, 0xc8, 0x51, 0x1a, 0x25, 0x44, 0x01, 0x53,
+                0x42, 0x74, 0x07, 0x9c, 0x18, 0xe6, 0x3b, 0x8a,
+                0xce, 0x7a, 0x37, 0x1f, 0x18, 0x5c, 0x02, 0x7c,
+                0x67, 0x16, 0xf5, 0x30, 0x2b, 0x31, 0xa9, 0xc7,
+            },
+        },
+        .public_key_rsp = {
+            .x = {
+                0x03, 0x0d, 0x13, 0x55, 0xd9, 0xee, 0x3f, 0xac,
+                0x8e, 0x8a, 0xa6, 0x2a, 0xcb, 0x60, 0x35, 0xb9,
+                0xb2, 0x4d, 0x63, 0x91, 0x5e, 0xa1, 0xdd, 0xdf,
+                0x60, 0xdc, 0x6e, 0x09, 0xb9, 0x9e, 0xf1, 0x4d,
+            },
+            .y = {
+                0xa8, 0x09, 0x31, 0x1e, 0x39, 0x96, 0x74, 0x41,
+                0xea, 0x19, 0x4f, 0x24, 0x36, 0x57, 0x7c, 0x9f,
+                0x21, 0xa3, 0xad, 0xa1, 0x3d, 0xe2, 0x1c, 0x6a,
+                0xd6, 0xc9, 0xdb, 0xff, 0xce, 0x0a, 0x94, 0x12,
+            },
+        },
+        .confirm_req[0] = {
+            .value = {
+                0x3b, 0x3d, 0xb2, 0x2f, 0x72, 0x0f, 0x93, 0x19,
+                0x95, 0xdb, 0x88, 0xdf, 0x5d, 0x58, 0x95, 0x37,
+            },
+        },
+        .confirm_rsp[0] = {
+            .value = {
+                0x98, 0xab, 0x20, 0x8d, 0x51, 0x3b, 0x6c, 0x29,
+                0x2d, 0x73, 0x15, 0xf6, 0x6d, 0x6d, 0xb9, 0xb3,
+            },
+        },
+        .random_req[0] = {
+            .value = {
+                0xc1, 0xdf, 0x20, 0x3d, 0x7b, 0xcb, 0x5f, 0xe2,
+                0x9a, 0x23, 0x9c, 0xba, 0x2f, 0x42, 0x3c, 0xb8,
+            },
+        },
+        .random_rsp[0] = {
+            .value = {
+                0x7d, 0x82, 0xb4, 0xae, 0x41, 0xdb, 0x67, 0x8f,
+                0x54, 0x01, 0x21, 0x64, 0x31, 0xd4, 0xfc, 0xb5,
+            },
+        },
+        .confirm_req[1] = {
+            .value = {
+                0xc3, 0xa5, 0xd0, 0xdd, 0xd5, 0xec, 0x1d, 0xc3,
+                0x14, 0x95, 0x79, 0xb2, 0x61, 0x4d, 0x4f, 0x36,
+            },
+        },
+        .confirm_rsp[1] = {
+            .value = {
+                0xe2, 0x20, 0xf4, 0x4d, 0xa1, 0x9c, 0x83, 0x51,
+                0x18, 0xf9, 0x35, 0x2a, 0x51, 0x50, 0xdf, 0xe7,
+            },
+        },
+        .random_req[1] = {
+            .value = {
+                0x71, 0xcb, 0x01, 0xb4, 0x83, 0xdc, 0xd8, 0x54,
+                0x0f, 0xe5, 0xd5, 0x6b, 0x6a, 0x0d, 0x98, 0xb6,
+            },
+        },
+        .random_rsp[1] = {
+            .value = {
+                0x30, 0xbf, 0xd3, 0xfd, 0xf4, 0xc2, 0xa1, 0xd0,
+                0xba, 0x4b, 0x27, 0x7c, 0x29, 0x98, 0x54, 0xa2,
+            },
+        },
+        .confirm_req[2] = {
+            .value = {
+                0xf0, 0x92, 0xc4, 0xda, 0x8a, 0x17, 0x7c, 0xc6,
+                0x14, 0x05, 0x7d, 0xbb, 0xfc, 0x7c, 0xcd, 0x0a,
+            },
+        },
+        .confirm_rsp[2] = {
+            .value = {
+                0xf3, 0x89, 0xca, 0xe0, 0xfb, 0xbe, 0x8c, 0xc3,
+                0x4c, 0x6c, 0x6e, 0x11, 0x36, 0x4e, 0xaa, 0x25,
+            },
+        },
+        .random_req[2] = {
+            .value = {
+                0x78, 0x5a, 0xf0, 0x1e, 0x2a, 0x0d, 0x16, 0xb3,
+                0x03, 0x4b, 0x4b, 0x68, 0x17, 0xe0, 0xf0, 0x82,
+            },
+        },
+        .random_rsp[2] = {
+            .value = {
+                0xbf, 0x96, 0xdd, 0xf5, 0x30, 0x2a, 0xe9, 0x8c,
+                0xb9, 0x13, 0xc5, 0xb7, 0x15, 0x1f, 0xa3, 0x9b,
+            },
+        },
+        .confirm_req[3] = {
+            .value = {
+                0x36, 0xe4, 0x4c, 0x00, 0xe7, 0x0d, 0xee, 0xe4,
+                0x95, 0xb8, 0x6a, 0xf9, 0xf7, 0x24, 0xef, 0xea,
+            },
+        },
+        .confirm_rsp[3] = {
+            .value = {
+                0x2e, 0x90, 0x87, 0x85, 0xb8, 0x29, 0x93, 0x9e,
+                0x38, 0xa6, 0xdb, 0x17, 0xb2, 0xa8, 0x32, 0x65,
+            },
+        },
+        .random_req[3] = {
+            .value = {
+                0x0a, 0xee, 0x93, 0xf6, 0x56, 0x35, 0x8e, 0xed,
+                0x3f, 0x45, 0xa5, 0x01, 0x59, 0xeb, 0xea, 0xa8,
+            },
+        },
+        .random_rsp[3] = {
+            .value = {
+                0x38, 0xd0, 0xf8, 0x11, 0x5e, 0x47, 0x72, 0x66,
+                0xce, 0x56, 0x9c, 0x81, 0x5f, 0x52, 0xd4, 0x9a,
+            },
+        },
+        .confirm_req[4] = {
+            .value = {
+                0x2c, 0x98, 0x9b, 0x71, 0xe4, 0xde, 0x6d, 0x20,
+                0x84, 0x30, 0xab, 0x7a, 0xfc, 0x43, 0x82, 0xc6,
+            },
+        },
+        .confirm_rsp[4] = {
+            .value = {
+                0x76, 0xfe, 0x1f, 0x78, 0xaa, 0x42, 0xd5, 0xc6,
+                0x9f, 0xe4, 0xa7, 0xc7, 0xb8, 0xd2, 0x1e, 0x59,
+            },
+        },
+        .random_req[4] = {
+            .value = {
+                0x61, 0x5e, 0x47, 0xb1, 0x77, 0x6f, 0x04, 0xee,
+                0x94, 0xc4, 0x6c, 0xa9, 0xf5, 0xf8, 0x11, 0x6e,
+            },
+        },
+        .random_rsp[4] = {
+            .value = {
+                0xa5, 0xad, 0x98, 0x65, 0x28, 0xfc, 0x6b, 0x02,
+                0x6d, 0x9a, 0x29, 0x61, 0x1c, 0x02, 0x0a, 0x6b,
+            },
+        },
+        .confirm_req[5] = {
+            .value = {
+                0x4d, 0x55, 0x3e, 0x1f, 0x87, 0x12, 0xc7, 0x6c,
+                0xd7, 0x9a, 0xa6, 0xf1, 0x6e, 0x48, 0xd3, 0x7d,
+            },
+        },
+        .confirm_rsp[5] = {
+            .value = {
+                0xff, 0x77, 0x6e, 0xba, 0x1f, 0xb7, 0xbd, 0x0b,
+                0x3f, 0xce, 0xd5, 0x39, 0x81, 0x17, 0x51, 0xfc,
+            },
+        },
+        .random_req[5] = {
+            .value = {
+                0xf9, 0x2c, 0x77, 0x41, 0xcd, 0x9a, 0x10, 0x99,
+                0xc6, 0x70, 0x5a, 0xc8, 0x24, 0x26, 0xb2, 0xc8,
+            },
+        },
+        .random_rsp[5] = {
+            .value = {
+                0xa0, 0x44, 0x0a, 0x8b, 0xda, 0x6a, 0x74, 0x90,
+                0x5f, 0x89, 0x44, 0xa5, 0x9a, 0x58, 0xd5, 0x08,
+            },
+        },
+        .confirm_req[6] = {
+            .value = {
+                0x7e, 0x6e, 0x89, 0xc8, 0xbe, 0xde, 0x1c, 0xc3,
+                0x45, 0xb6, 0x4c, 0x83, 0x71, 0xe2, 0xd6, 0xda,
+            },
+        },
+        .confirm_rsp[6] = {
+            .value = {
+                0x4a, 0x29, 0x7b, 0x88, 0x97, 0xc1, 0x60, 0x85,
+                0x32, 0x7d, 0xf1, 0xaa, 0x04, 0x13, 0x89, 0x11,
+            },
+        },
+        .random_req[6] = {
+            .value = {
+                0x2a, 0xaf, 0x7d, 0x21, 0x4e, 0x14, 0xf5, 0x7e,
+                0xcc, 0x39, 0xf7, 0x56, 0x45, 0x87, 0x23, 0x64,
+            },
+        },
+        .random_rsp[6] = {
+            .value = {
+                0x74, 0xd2, 0xff, 0xf0, 0x19, 0xf7, 0x87, 0xe7,
+                0x0d, 0x65, 0x27, 0x61, 0xea, 0x9e, 0x05, 0x3d,
+            },
+        },
+        .confirm_req[7] = {
+            .value = {
+                0x4f, 0x77, 0x22, 0x08, 0x58, 0xed, 0x8c, 0x60,
+                0xbf, 0xbc, 0x78, 0x0c, 0x80, 0xc9, 0xb7, 0x60,
+            },
+        },
+        .confirm_rsp[7] = {
+            .value = {
+                0xd2, 0x47, 0xfd, 0xea, 0xa3, 0x32, 0x53, 0xc1,
+                0x06, 0xcd, 0x64, 0xeb, 0x88, 0x64, 0x0e, 0xe5,
+            },
+        },
+        .random_req[7] = {
+            .value = {
+                0xc8, 0xd0, 0x45, 0xa8, 0x29, 0xdb, 0x5a, 0x42,
+                0xfe, 0x68, 0xa8, 0x7a, 0x0a, 0x13, 0x22, 0xa4,
+            },
+        },
+        .random_rsp[7] = {
+            .value = {
+                0x78, 0x14, 0x46, 0xe2, 0x47, 0x0e, 0xd4, 0xb4,
+                0xde, 0x35, 0x4a, 0x82, 0x4b, 0x32, 0x9b, 0x46,
+            },
+        },
+        .confirm_req[8] = {
+            .value = {
+                0x24, 0x96, 0xe5, 0x50, 0xfa, 0xff,

<TRUNCATED>


[16/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_sm_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_test.c b/net/nimble/host/src/test/ble_sm_test.c
deleted file mode 100644
index f139ddb..0000000
--- a/net/nimble/host/src/test/ble_sm_test.c
+++ /dev/null
@@ -1,678 +0,0 @@
-/**
- * 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 <stddef.h>
-#include <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/hci_common.h"
-#include "nimble/nimble_opt.h"
-#include "host/ble_sm.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-#include "ble_sm_test_util.h"
-
-#if NIMBLE_OPT(SM)
-
-/*****************************************************************************
- * $misc                                                                     *
- *****************************************************************************/
-
-TEST_CASE(ble_sm_test_case_f4)
-{
-	uint8_t u[32] = { 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
-			  0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
-			  0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
-			  0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
-	uint8_t v[32] = { 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
-			  0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
-			  0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
-			  0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
-	uint8_t x[16] = { 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
-			  0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
-	uint8_t z = 0x00;
-	uint8_t exp[16] = { 0x2d, 0x87, 0x74, 0xa9, 0xbe, 0xa1, 0xed, 0xf1,
-			    0x1c, 0xbd, 0xa9, 0x07, 0xf1, 0x16, 0xc9, 0xf2 };
-	uint8_t res[16];
-	int err;
-
-	err = ble_sm_alg_f4(u, v, x, z, res);
-	TEST_ASSERT_FATAL(err == 0);
-    TEST_ASSERT(memcmp(res, exp, 16) == 0);
-}
-
-TEST_CASE(ble_sm_test_case_f5)
-{
-	uint8_t w[32] = { 0x98, 0xa6, 0xbf, 0x73, 0xf3, 0x34, 0x8d, 0x86,
-			  0xf1, 0x66, 0xf8, 0xb4, 0x13, 0x6b, 0x79, 0x99,
-			  0x9b, 0x7d, 0x39, 0x0a, 0xa6, 0x10, 0x10, 0x34,
-			  0x05, 0xad, 0xc8, 0x57, 0xa3, 0x34, 0x02, 0xec };
-	uint8_t n1[16] = { 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
-			   0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
-	uint8_t n2[16] = { 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
-			   0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
-    uint8_t a1t = 0x00;
-	uint8_t a1[6] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56 };
-    uint8_t a2t = 0x00;
-    uint8_t a2[6] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7 };
-	uint8_t exp_ltk[16] = { 0x38, 0x0a, 0x75, 0x94, 0xb5, 0x22, 0x05,
-				0x98, 0x23, 0xcd, 0xd7, 0x69, 0x11, 0x79,
-				0x86, 0x69 };
-	uint8_t exp_mackey[16] = { 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f,
-				   0xfd, 0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1,
-				   0x65, 0x29 };
-	uint8_t mackey[16], ltk[16];
-	int err;
-
-	err = ble_sm_alg_f5(w, n1, n2, a1t, a1, a2t, a2, mackey, ltk);
-	TEST_ASSERT_FATAL(err == 0);
-    TEST_ASSERT(memcmp(mackey, exp_mackey, 16) == 0);
-    TEST_ASSERT(memcmp(ltk, exp_ltk, 16) == 0);
-}
-
-TEST_CASE(ble_sm_test_case_f6)
-{
-	uint8_t w[16] = { 0x20, 0x6e, 0x63, 0xce, 0x20, 0x6a, 0x3f, 0xfd,
-			  0x02, 0x4a, 0x08, 0xa1, 0x76, 0xf1, 0x65, 0x29 };
-	uint8_t n1[16] = { 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
-			   0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
-	uint8_t n2[16] = { 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
-			   0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
-	uint8_t r[16] = { 0xc8, 0x0f, 0x2d, 0x0c, 0xd2, 0x42, 0xda, 0x08,
-			  0x54, 0xbb, 0x53, 0xb4, 0x3b, 0x34, 0xa3, 0x12 };
-	uint8_t io_cap[3] = { 0x02, 0x01, 0x01 };
-    uint8_t a1t = 0x00;
-	uint8_t a1[6] = { 0xce, 0xbf, 0x37, 0x37, 0x12, 0x56 };
-    uint8_t a2t = 0x00;
-    uint8_t a2[6] = { 0xc1, 0xcf, 0x2d, 0x70, 0x13, 0xa7 };
-	uint8_t exp[16] = { 0x61, 0x8f, 0x95, 0xda, 0x09, 0x0b, 0x6c, 0xd2,
-			    0xc5, 0xe8, 0xd0, 0x9c, 0x98, 0x73, 0xc4, 0xe3 };
-	uint8_t res[16];
-	int err;
-
-	err = ble_sm_alg_f6(w, n1, n2, r, io_cap, a1t, a1, a2t, a2, res);
-	TEST_ASSERT_FATAL(err == 0);
-    TEST_ASSERT(memcmp(res, exp, 16) == 0);
-}
-
-TEST_CASE(ble_sm_test_case_g2)
-{
-	uint8_t u[32] = { 0xe6, 0x9d, 0x35, 0x0e, 0x48, 0x01, 0x03, 0xcc,
-			  0xdb, 0xfd, 0xf4, 0xac, 0x11, 0x91, 0xf4, 0xef,
-			  0xb9, 0xa5, 0xf9, 0xe9, 0xa7, 0x83, 0x2c, 0x5e,
-			  0x2c, 0xbe, 0x97, 0xf2, 0xd2, 0x03, 0xb0, 0x20 };
-	uint8_t v[32] = { 0xfd, 0xc5, 0x7f, 0xf4, 0x49, 0xdd, 0x4f, 0x6b,
-			  0xfb, 0x7c, 0x9d, 0xf1, 0xc2, 0x9a, 0xcb, 0x59,
-			  0x2a, 0xe7, 0xd4, 0xee, 0xfb, 0xfc, 0x0a, 0x90,
-			  0x9a, 0xbb, 0xf6, 0x32, 0x3d, 0x8b, 0x18, 0x55 };
-	uint8_t x[16] = { 0xab, 0xae, 0x2b, 0x71, 0xec, 0xb2, 0xff, 0xff,
-			  0x3e, 0x73, 0x77, 0xd1, 0x54, 0x84, 0xcb, 0xd5 };
-	uint8_t y[16] = { 0xcf, 0xc4, 0x3d, 0xff, 0xf7, 0x83, 0x65, 0x21,
-			  0x6e, 0x5f, 0xa7, 0x25, 0xcc, 0xe7, 0xe8, 0xa6 };
-	uint32_t exp_val = 0x2f9ed5ba % 1000000;
-	uint32_t val;
-	int err;
-
-	err = ble_sm_alg_g2(u, v, x, y, &val);
-	TEST_ASSERT_FATAL(err == 0);
-	TEST_ASSERT(val == exp_val);
-}
-
-TEST_CASE(ble_sm_test_case_conn_broken)
-{
-    struct hci_disconn_complete disconn_evt;
-    int rc;
-
-    ble_sm_test_util_init();
-
-    ble_sm_dbg_set_next_pair_rand(((uint8_t[16]){0}));
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[6]){1,2,3,5,6,7}),
-                                 ble_sm_test_util_conn_cb, NULL);
-
-    /* Initiate the pairing procedure. */
-    rc = ble_hs_test_util_security_initiate(2, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
-
-    /* Terminate the connection. */
-    disconn_evt.connection_handle = 2;
-    disconn_evt.status = 0;
-    disconn_evt.reason = BLE_ERR_REM_USER_CONN_TERM;
-    ble_gap_rx_disconn_complete(&disconn_evt);
-
-    /* Verify security callback got called. */
-    TEST_ASSERT(ble_sm_test_gap_status == BLE_HS_ENOTCONN);
-    TEST_ASSERT(!ble_sm_test_sec_state.encrypted);
-    TEST_ASSERT(!ble_sm_test_sec_state.authenticated);
-}
-
-/*****************************************************************************
- * $peer                                                                     *
- *****************************************************************************/
-
-TEST_CASE(ble_sm_test_case_peer_fail_inval)
-{
-    /* Invalid role detected before other arguments. */
-    ble_sm_test_util_peer_fail_inval(
-        1,
-        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
-        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
-        ((struct ble_sm_pair_cmd[1]) { {
-            .io_cap = 0x14,
-            .oob_data_flag = 0,
-            .authreq = 0x12,
-            .max_enc_key_size = 20,
-            .init_key_dist = 0x0b,
-            .resp_key_dist = 0x11,
-        } }),
-        ((struct ble_sm_pair_fail[1]) { {
-            .reason = BLE_SM_ERR_CMD_NOT_SUPP,
-        } })
-    );
-
-    /* Invalid IO capabiltiies. */
-    ble_sm_test_util_peer_fail_inval(
-        0,
-        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
-        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
-        ((struct ble_sm_pair_cmd[1]) { {
-            .io_cap = 0x14,
-            .oob_data_flag = 0,
-            .authreq = 0x05,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        } }),
-        ((struct ble_sm_pair_fail[1]) { {
-            .reason = BLE_SM_ERR_INVAL,
-        } })
-    );
-
-    /* Invalid OOB flag. */
-    ble_sm_test_util_peer_fail_inval(
-        0,
-        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
-        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
-        ((struct ble_sm_pair_cmd[1]) { {
-            .io_cap = 0x04,
-            .oob_data_flag = 2,
-            .authreq = 0x05,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        } }),
-        ((struct ble_sm_pair_fail[1]) { {
-            .reason = BLE_SM_ERR_INVAL,
-        } })
-    );
-
-    /* Invalid authreq - reserved bonding flag. */
-    ble_sm_test_util_peer_fail_inval(
-        0,
-        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
-        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
-        ((struct ble_sm_pair_cmd[1]) { {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x2,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        } }),
-        ((struct ble_sm_pair_fail[1]) { {
-            .reason = BLE_SM_ERR_INVAL,
-        } })
-    );
-
-    /* Invalid authreq - reserved other flag. */
-    ble_sm_test_util_peer_fail_inval(
-        0,
-        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
-        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
-        ((struct ble_sm_pair_cmd[1]) { {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x20,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        } }),
-        ((struct ble_sm_pair_fail[1]) { {
-            .reason = BLE_SM_ERR_INVAL,
-        } })
-    );
-
-    /* Invalid key size - too small. */
-    ble_sm_test_util_peer_fail_inval(
-        0,
-        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
-        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
-        ((struct ble_sm_pair_cmd[1]) { {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x5,
-            .max_enc_key_size = 6,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        } }),
-        ((struct ble_sm_pair_fail[1]) { {
-            .reason = BLE_SM_ERR_INVAL,
-        } })
-    );
-
-    /* Invalid key size - too large. */
-    ble_sm_test_util_peer_fail_inval(
-        0,
-        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
-        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
-        ((struct ble_sm_pair_cmd[1]) { {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x5,
-            .max_enc_key_size = 17,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        } }),
-        ((struct ble_sm_pair_fail[1]) { {
-            .reason = BLE_SM_ERR_INVAL,
-        } })
-    );
-
-    /* Invalid init key dist. */
-    ble_sm_test_util_peer_fail_inval(
-        0,
-        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
-        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
-        ((struct ble_sm_pair_cmd[1]) { {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x5,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0x10,
-            .resp_key_dist = 0x07,
-        } }),
-        ((struct ble_sm_pair_fail[1]) { {
-            .reason = BLE_SM_ERR_INVAL,
-        } })
-    );
-
-    /* Invalid resp key dist. */
-    ble_sm_test_util_peer_fail_inval(
-        0,
-        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
-        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
-        ((struct ble_sm_pair_cmd[1]) { {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x5,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x10,
-        } }),
-        ((struct ble_sm_pair_fail[1]) { {
-            .reason = BLE_SM_ERR_INVAL,
-        } })
-    );
-}
-
-TEST_CASE(ble_sm_test_case_peer_lgcy_fail_confirm)
-{
-    ble_sm_test_util_peer_lgcy_fail_confirm(
-        ((uint8_t[]){0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c}),
-        ((uint8_t[]){0x03, 0x02, 0x01, 0x50, 0x13, 0x00}),
-        ((struct ble_sm_pair_cmd[1]) { {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x05,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        } }),
-        ((struct ble_sm_pair_cmd[1]) { {
-            .io_cap = 3,
-            .oob_data_flag = 0,
-            .authreq = 0,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0,
-            .resp_key_dist = 0,
-        } }),
-        ((struct ble_sm_pair_confirm[1]) { {
-            .value = {
-                0x0a, 0xac, 0xa2, 0xae, 0xa6, 0x98, 0xdc, 0x6d,
-                0x65, 0x84, 0x11, 0x69, 0x47, 0x36, 0x8d, 0xa0,
-            },
-        } }),
-        ((struct ble_sm_pair_confirm[1]) { {
-            .value = {
-                0x45, 0xd2, 0x2c, 0x38, 0xd8, 0x91, 0x4f, 0x19,
-                0xa2, 0xd4, 0xfc, 0x7d, 0xad, 0x37, 0x79, 0xe0
-            },
-        } }),
-        ((struct ble_sm_pair_random[1]) { {
-            .value = {
-                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-                0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
-            },
-        } }),
-        ((struct ble_sm_pair_random[1]) { {
-            .value = {
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-            },
-        } }),
-        ((struct ble_sm_pair_fail[1]) { {
-            .reason = BLE_SM_ERR_CONFIRM_MISMATCH,
-        } })
-    );
-}
-
-TEST_CASE(ble_sm_test_case_peer_bonding_bad)
-{
-    ble_sm_test_util_peer_bonding_bad(0x5684, 32);
-    ble_sm_test_util_peer_bonding_bad(54325, 65437);
-}
-
-TEST_CASE(ble_sm_test_case_peer_sec_req_inval)
-{
-    struct ble_sm_pair_fail fail;
-    struct ble_sm_sec_req sec_req;
-    int rc;
-
-    ble_sm_test_util_init();
-
-    ble_sm_dbg_set_next_pair_rand(((uint8_t[16]){0}));
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[6]){1,2,3,5,6,7}),
-                                 ble_sm_test_util_conn_cb,
-                                 NULL);
-
-    /*** We are the slave; reject the security request. */
-    ble_hs_atomic_conn_set_flags(2, BLE_HS_CONN_F_MASTER, 0);
-
-    sec_req.authreq = 0;
-    ble_sm_test_util_rx_sec_req(
-        2, &sec_req, BLE_HS_SM_US_ERR(BLE_SM_ERR_CMD_NOT_SUPP));
-
-    ble_hs_test_util_tx_all();
-
-    fail.reason = BLE_SM_ERR_CMD_NOT_SUPP;
-    ble_sm_test_util_verify_tx_pair_fail(&fail);
-
-    /*** Pairing already in progress; ignore security request. */
-    ble_hs_atomic_conn_set_flags(2, BLE_HS_CONN_F_MASTER, 1);
-    rc = ble_sm_pair_initiate(2);
-    TEST_ASSERT_FATAL(rc == 0);
-    ble_hs_test_util_tx_all();
-    ble_hs_test_util_prev_tx_queue_clear();
-
-    ble_sm_test_util_rx_sec_req(2, &sec_req, BLE_HS_EALREADY);
-    ble_hs_test_util_tx_all();
-    TEST_ASSERT(ble_hs_test_util_prev_tx_queue_sz() == 0);
-}
-
-/*****************************************************************************
- * $us                                                                       *
- *****************************************************************************/
-
-TEST_CASE(ble_sm_test_case_us_fail_inval)
-{
-    struct ble_sm_test_params params;
-
-    /* Invalid IO capabiltiies. */
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
-        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
-        .pair_req = (struct ble_sm_pair_cmd) {
-            .io_cap = 3,
-            .oob_data_flag = 0,
-            .authreq = 0,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0,
-            .resp_key_dist = 0,
-        },
-        .pair_rsp = (struct ble_sm_pair_cmd) {
-            .io_cap = 0x14,
-            .oob_data_flag = 0,
-            .authreq = 0x05,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_fail = (struct ble_sm_pair_fail) {
-            .reason = BLE_SM_ERR_INVAL,
-        },
-    };
-    ble_sm_test_util_us_fail_inval(&params);
-
-    /* Invalid OOB flag. */
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
-        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
-        .pair_req = (struct ble_sm_pair_cmd) {
-            .io_cap = 3,
-            .oob_data_flag = 0,
-            .authreq = 0,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0,
-            .resp_key_dist = 0,
-        },
-        .pair_rsp = (struct ble_sm_pair_cmd) {
-            .io_cap = 0x14,
-            .oob_data_flag = 2,
-            .authreq = 0x05,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_fail = (struct ble_sm_pair_fail) {
-            .reason = BLE_SM_ERR_INVAL,
-        },
-    };
-    ble_sm_test_util_us_fail_inval(&params);
-
-    /* Invalid authreq - reserved bonding flag. */
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
-        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
-        .pair_req = (struct ble_sm_pair_cmd) {
-            .io_cap = 3,
-            .oob_data_flag = 0,
-            .authreq = 0,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0,
-            .resp_key_dist = 0,
-        },
-        .pair_rsp = (struct ble_sm_pair_cmd) {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x02,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_fail = (struct ble_sm_pair_fail) {
-            .reason = BLE_SM_ERR_INVAL,
-        },
-    };
-    ble_sm_test_util_us_fail_inval(&params);
-
-    /* Invalid authreq - reserved other flag. */
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
-        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
-        .pair_req = (struct ble_sm_pair_cmd) {
-            .io_cap = 3,
-            .oob_data_flag = 0,
-            .authreq = 0,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0,
-            .resp_key_dist = 0,
-        },
-        .pair_rsp = (struct ble_sm_pair_cmd) {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x20,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_fail = (struct ble_sm_pair_fail) {
-            .reason = BLE_SM_ERR_INVAL,
-        },
-    };
-    ble_sm_test_util_us_fail_inval(&params);
-
-    /* Invalid key size - too small. */
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
-        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
-        .pair_req = (struct ble_sm_pair_cmd) {
-            .io_cap = 3,
-            .oob_data_flag = 0,
-            .authreq = 0,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0,
-            .resp_key_dist = 0,
-        },
-        .pair_rsp = (struct ble_sm_pair_cmd) {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x05,
-            .max_enc_key_size = 6,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_fail = (struct ble_sm_pair_fail) {
-            .reason = BLE_SM_ERR_INVAL,
-        },
-    };
-    ble_sm_test_util_us_fail_inval(&params);
-
-    /* Invalid key size - too large. */
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
-        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
-        .pair_req = (struct ble_sm_pair_cmd) {
-            .io_cap = 3,
-            .oob_data_flag = 0,
-            .authreq = 0,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0,
-            .resp_key_dist = 0,
-        },
-        .pair_rsp = (struct ble_sm_pair_cmd) {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x05,
-            .max_enc_key_size = 17,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x07,
-        },
-        .pair_fail = (struct ble_sm_pair_fail) {
-            .reason = BLE_SM_ERR_INVAL,
-        },
-    };
-    ble_sm_test_util_us_fail_inval(&params);
-
-    /* Invalid init key dist. */
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
-        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
-        .pair_req = (struct ble_sm_pair_cmd) {
-            .io_cap = 3,
-            .oob_data_flag = 0,
-            .authreq = 0,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0,
-            .resp_key_dist = 0,
-        },
-        .pair_rsp = (struct ble_sm_pair_cmd) {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x05,
-            .max_enc_key_size = 17,
-            .init_key_dist = 0x10,
-            .resp_key_dist = 0x07,
-        },
-        .pair_fail = (struct ble_sm_pair_fail) {
-            .reason = BLE_SM_ERR_INVAL,
-        },
-    };
-    ble_sm_test_util_us_fail_inval(&params);
-
-    /* Invalid resp key dist. */
-    params = (struct ble_sm_test_params) {
-        .init_id_addr = {0xe1, 0xfc, 0xda, 0xf4, 0xb7, 0x6c},
-        .resp_id_addr = {0x03, 0x02, 0x01, 0x50, 0x13, 0x00},
-        .pair_req = (struct ble_sm_pair_cmd) {
-            .io_cap = 3,
-            .oob_data_flag = 0,
-            .authreq = 0,
-            .max_enc_key_size = 16,
-            .init_key_dist = 0,
-            .resp_key_dist = 0,
-        },
-        .pair_rsp = (struct ble_sm_pair_cmd) {
-            .io_cap = 0x04,
-            .oob_data_flag = 0,
-            .authreq = 0x05,
-            .max_enc_key_size = 17,
-            .init_key_dist = 0x07,
-            .resp_key_dist = 0x10,
-        },
-        .pair_fail = (struct ble_sm_pair_fail) {
-            .reason = BLE_SM_ERR_INVAL,
-        },
-    };
-    ble_sm_test_util_us_fail_inval(&params);
-}
-
-TEST_SUITE(ble_sm_gen_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_sm_test_case_f4();
-    ble_sm_test_case_f5();
-    ble_sm_test_case_f6();
-    ble_sm_test_case_g2();
-
-    ble_sm_test_case_peer_fail_inval();
-    ble_sm_test_case_peer_lgcy_fail_confirm();
-    ble_sm_test_case_us_fail_inval();
-    ble_sm_test_case_peer_bonding_bad();
-    ble_sm_test_case_conn_broken();
-    ble_sm_test_case_peer_sec_req_inval();
-}
-#endif
-
-int
-ble_sm_test_all(void)
-{
-#if !NIMBLE_OPT(SM)
-    return 0;
-#else
-    ble_sm_gen_test_suite();
-    ble_sm_lgcy_test_suite();
-    ble_sm_sc_test_suite();
-
-    return tu_any_failed;
-#endif
-}


[15/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_sm_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_sm_test_util.c b/net/nimble/host/src/test/ble_sm_test_util.c
deleted file mode 100644
index 5cb1d40..0000000
--- a/net/nimble/host/src/test/ble_sm_test_util.c
+++ /dev/null
@@ -1,2404 +0,0 @@
-/**
- * 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 <stddef.h>
-#include <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/hci_common.h"
-#include "nimble/nimble_opt.h"
-#include "host/ble_sm.h"
-#include "host/ble_hs_test.h"
-#include "host/ble_hs_id.h"
-#include "ble_hs_test_util.h"
-#include "ble_sm_test_util.h"
-
-int ble_sm_test_gap_event_type;
-int ble_sm_test_gap_status;
-struct ble_gap_sec_state ble_sm_test_sec_state;
-
-int ble_sm_test_store_obj_type;
-union ble_store_key ble_sm_test_store_key;
-union ble_store_value ble_sm_test_store_value;
-
-static ble_store_read_fn ble_sm_test_util_store_read;
-static ble_store_write_fn ble_sm_test_util_store_write;
-
-struct ble_sm_test_util_entity {
-    uint8_t addr_type;
-    uint8_t id_addr_type;
-    uint8_t *id_addr;
-    uint8_t *rpa;
-
-    struct ble_sm_pair_cmd *pair_cmd;
-    struct ble_sm_pair_confirm *confirms;
-    struct ble_sm_pair_random *randoms;
-    struct ble_sm_id_info *id_info;
-    struct ble_sm_id_addr_info *id_addr_info;
-    struct ble_sm_sign_info *sign_info;
-    uint8_t *ltk;
-
-    uint8_t key_dist;
-
-    /*** Secure connections fields. */
-    struct ble_sm_public_key *public_key;
-    struct ble_sm_dhkey_check *dhkey_check;
-
-    /*** Legacy fields. */
-    struct ble_sm_enc_info *enc_info;
-    struct ble_sm_master_id *master_id;
-    uint64_t rand_num;
-    uint16_t ediv;
-};
-
-#define BLE_SM_TEST_UTIL_HCI_HDR(handle, pb, len) \
-    ((struct hci_data_hdr) {                            \
-        .hdh_handle_pb_bc = ((handle)  << 0) |          \
-                            ((pb)      << 12),          \
-        .hdh_len = (len)                                \
-    })
-
-static int
-ble_sm_test_util_store_read(int obj_type, union ble_store_key *key,
-                                  union ble_store_value *val)
-{
-    ble_sm_test_store_obj_type = obj_type;
-    ble_sm_test_store_key = *key;
-
-    return ble_hs_test_util_store_read(obj_type, key, val);
-}
-
-static int
-ble_sm_test_util_store_write(int obj_type, union ble_store_value *val)
-{
-    ble_sm_test_store_obj_type = obj_type;
-    ble_sm_test_store_value = *val;
-
-    return ble_hs_test_util_store_write(obj_type, val);
-}
-
-void
-ble_sm_test_util_init(void)
-{
-    ble_hs_test_util_init();
-    ble_hs_test_util_store_init(10, 10, 10);
-    ble_hs_cfg.store_read_cb = ble_sm_test_util_store_read;
-    ble_hs_cfg.store_write_cb = ble_sm_test_util_store_write;
-
-    ble_sm_test_store_obj_type = -1;
-    ble_sm_test_gap_event_type = -1;
-    ble_sm_test_gap_status = -1;
-
-    memset(&ble_sm_test_sec_state, 0xff, sizeof ble_sm_test_sec_state);
-}
-
-static void
-ble_sm_test_util_params_to_entity(struct ble_sm_test_params *params,
-                                  int initiator,
-                                  struct ble_sm_test_util_entity *out_entity)
-{
-    int sc;
-
-    memset(out_entity, 0, sizeof *out_entity);
-
-    sc = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_SC &&
-         params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_SC;
-
-    if (initiator) {
-        out_entity->key_dist = params->pair_rsp.init_key_dist;
-
-        out_entity->addr_type = params->init_addr_type;
-        out_entity->id_addr = params->init_id_addr;
-        out_entity->rpa = params->init_rpa;
-
-        out_entity->pair_cmd = &params->pair_req;
-        out_entity->confirms = params->confirm_req;
-        out_entity->randoms = params->random_req;
-        out_entity->id_info = &params->id_info_rsp;
-        out_entity->id_addr_info = &params->id_addr_info_rsp;
-        out_entity->sign_info = &params->sign_info_rsp;
-
-        if (sc) {
-            out_entity->ltk = params->ltk;
-            out_entity->public_key = &params->public_key_req;
-            out_entity->dhkey_check = &params->dhkey_check_req;
-        } else {
-            out_entity->enc_info = &params->enc_info_rsp;
-            out_entity->master_id = &params->master_id_rsp;
-            if (out_entity->key_dist & BLE_SM_PAIR_KEY_DIST_ENC) {
-                out_entity->rand_num = params->master_id_rsp.rand_val;
-                out_entity->ediv = params->master_id_rsp.ediv;
-                out_entity->ltk = params->enc_info_rsp.ltk;
-            }
-        }
-    } else {
-        out_entity->key_dist = params->pair_rsp.resp_key_dist;
-
-        out_entity->addr_type = params->resp_addr_type;
-        out_entity->id_addr = params->resp_id_addr;
-        out_entity->rpa = params->resp_rpa;
-
-        out_entity->pair_cmd = &params->pair_rsp;
-        out_entity->confirms = params->confirm_rsp;
-        out_entity->randoms = params->random_rsp;
-        out_entity->id_info = &params->id_info_req;
-        out_entity->id_addr_info = &params->id_addr_info_req;
-        out_entity->sign_info = &params->sign_info_req;
-
-        if (sc) {
-            out_entity->ltk = params->ltk;
-            out_entity->public_key = &params->public_key_rsp;
-            out_entity->dhkey_check = &params->dhkey_check_rsp;
-        } else {
-            out_entity->enc_info = &params->enc_info_req;
-            out_entity->master_id = &params->master_id_req;
-            if (out_entity->key_dist & BLE_SM_PAIR_KEY_DIST_ENC) {
-                out_entity->rand_num = params->master_id_req.rand_val;
-                out_entity->ediv = params->master_id_req.ediv;
-                out_entity->ltk = params->enc_info_req.ltk;
-            }
-        }
-    }
-
-    out_entity->id_addr_type =
-        ble_hs_misc_addr_type_to_id(out_entity->addr_type);
-}
-
-static void
-ble_sm_test_util_params_to_entities(struct ble_sm_test_params *params,
-                                    int we_are_initiator,
-                                    struct ble_sm_test_util_entity *out_us,
-                                    struct ble_sm_test_util_entity *out_peer)
-{
-    ble_sm_test_util_params_to_entity(params, we_are_initiator, out_us);
-    ble_sm_test_util_params_to_entity(params, !we_are_initiator, out_peer);
-}
-
-static void
-ble_sm_test_util_init_good(struct ble_sm_test_params *params,
-                           int we_are_initiator,
-                           struct ble_hs_conn **out_conn,
-                           struct ble_sm_test_util_entity *out_us,
-                           struct ble_sm_test_util_entity *out_peer)
-{
-    struct ble_hs_conn *conn;
-
-    ble_sm_test_util_init();
-
-    ble_sm_test_util_params_to_entities(params, we_are_initiator,
-                                        out_us, out_peer);
-
-    ble_hs_cfg.sm_io_cap = out_us->pair_cmd->io_cap;
-    ble_hs_cfg.sm_oob_data_flag = out_us->pair_cmd->oob_data_flag;
-    ble_hs_cfg.sm_bonding = !!(out_us->pair_cmd->authreq &
-                               BLE_SM_PAIR_AUTHREQ_BOND);
-    ble_hs_cfg.sm_mitm = !!(out_us->pair_cmd->authreq &
-                            BLE_SM_PAIR_AUTHREQ_MITM);
-    ble_hs_cfg.sm_sc = !!(out_us->pair_cmd->authreq &
-                          BLE_SM_PAIR_AUTHREQ_SC);
-    ble_hs_cfg.sm_keypress = !!(out_us->pair_cmd->authreq &
-                                BLE_SM_PAIR_AUTHREQ_KEYPRESS);
-
-    if (we_are_initiator) {
-        ble_hs_cfg.sm_our_key_dist = out_us->pair_cmd->init_key_dist;
-        ble_hs_cfg.sm_their_key_dist = out_us->pair_cmd->resp_key_dist;
-    } else {
-        ble_hs_cfg.sm_our_key_dist = out_us->pair_cmd->resp_key_dist;
-        ble_hs_cfg.sm_their_key_dist = out_us->pair_cmd->init_key_dist;
-    }
-
-    ble_hs_id_set_pub(out_us->id_addr);
-    ble_sm_dbg_set_next_pair_rand(out_us->randoms[0].value);
-    ble_sm_dbg_set_next_ediv(out_us->ediv);
-    ble_sm_dbg_set_next_master_id_rand(out_us->rand_num);
-    ble_sm_dbg_set_next_ltk(out_us->ltk);
-    ble_hs_test_util_set_our_irk(out_us->id_info->irk, 0, 0);
-    ble_sm_dbg_set_next_csrk(out_us->sign_info->sig_key);
-
-    if (out_us->public_key != NULL) {
-        ble_sm_dbg_set_sc_keys(out_us->public_key->x, params->our_priv_key);
-    }
-
-    ble_hs_test_util_create_rpa_conn(2, out_us->addr_type, out_us->rpa,
-                                     out_peer->addr_type,
-                                     out_peer->id_addr, out_peer->rpa,
-                                     ble_sm_test_util_conn_cb,
-                                     NULL);
-
-    /* This test code and modifies the connection object after unlocking
-     * the host mutex.  It is not OK for real code to do this, but this test
-     * can assume the connection list is unchanging.
-     */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(2);
-    TEST_ASSERT_FATAL(conn != NULL);
-    ble_hs_unlock();
-
-    if (!we_are_initiator) {
-        /* Peer is the initiator so we must be the slave. */
-        conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
-    }
-
-    if (out_conn != NULL) {
-        *out_conn = conn;
-    }
-}
-
-struct ble_gap_passkey_params ble_sm_test_ioact;
-
-int
-ble_sm_test_util_conn_cb(struct ble_gap_event *event, void *arg)
-{
-    struct ble_gap_conn_desc desc;
-    int rc;
-
-    switch (event->type) {
-    case BLE_GAP_EVENT_ENC_CHANGE:
-        ble_sm_test_gap_status = event->enc_change.status;
-
-        rc = ble_gap_conn_find(event->enc_change.conn_handle, &desc);
-        TEST_ASSERT_FATAL(rc == 0);
-        ble_sm_test_sec_state = desc.sec_state;
-        rc = 0;
-        break;
-
-    case BLE_GAP_EVENT_PASSKEY_ACTION:
-        ble_sm_test_ioact = event->passkey.params;
-        break;
-
-    default:
-        return 0;
-    }
-
-    ble_sm_test_gap_event_type = event->type;
-
-    return rc;
-}
-
-static void
-ble_sm_test_util_rx_pair_cmd(uint16_t conn_handle, uint8_t op,
-                                   struct ble_sm_pair_cmd *cmd,
-                                   int rx_status)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int payload_len;
-    int rc;
-
-    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    payload_len = BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ;
-
-    v = os_mbuf_extend(om, payload_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    ble_sm_pair_cmd_write(v, payload_len, op == BLE_SM_OP_PAIR_REQ,
-                                cmd);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
-                                              &hci_hdr, om);
-    TEST_ASSERT(rc == rx_status);
-}
-
-static void
-ble_sm_test_util_rx_pair_req(uint16_t conn_handle,
-                                   struct ble_sm_pair_cmd *req,
-                                   int rx_status)
-{
-    ble_sm_test_util_rx_pair_cmd(conn_handle, BLE_SM_OP_PAIR_REQ,
-                                       req, rx_status);
-}
-
-static void
-ble_sm_test_util_rx_pair_rsp(uint16_t conn_handle, struct ble_sm_pair_cmd *rsp,
-                             int rx_status)
-{
-    ble_sm_test_util_rx_pair_cmd(conn_handle, BLE_SM_OP_PAIR_RSP,
-                                       rsp, rx_status);
-}
-
-static void
-ble_sm_test_util_rx_confirm(uint16_t conn_handle,
-                            struct ble_sm_pair_confirm *cmd)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int payload_len;
-    int rc;
-
-    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PAIR_CONFIRM_SZ);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    payload_len = BLE_SM_HDR_SZ + BLE_SM_PAIR_CONFIRM_SZ;
-
-    v = os_mbuf_extend(om, payload_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    ble_sm_pair_confirm_write(v, payload_len, cmd);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
-                                              &hci_hdr, om);
-    TEST_ASSERT_FATAL(rc == 0);
-}
-
-static void
-ble_sm_test_util_rx_random(uint16_t conn_handle,
-                           struct ble_sm_pair_random *cmd,
-                           int exp_status)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int payload_len;
-    int rc;
-
-    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PAIR_RANDOM_SZ);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    payload_len = BLE_SM_HDR_SZ + BLE_SM_PAIR_RANDOM_SZ;
-
-    v = os_mbuf_extend(om, payload_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    ble_sm_pair_random_write(v, payload_len, cmd);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
-                                              &hci_hdr, om);
-    TEST_ASSERT_FATAL(rc == exp_status);
-}
-
-void
-ble_sm_test_util_rx_sec_req(uint16_t conn_handle, struct ble_sm_sec_req *cmd,
-                            int exp_status)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int payload_len;
-    int rc;
-
-    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_SEC_REQ_SZ);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    payload_len = BLE_SM_HDR_SZ + BLE_SM_SEC_REQ_SZ;
-
-    v = os_mbuf_extend(om, payload_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    ble_sm_sec_req_write(v, payload_len, cmd);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
-                                              &hci_hdr, om);
-    TEST_ASSERT_FATAL(rc == exp_status);
-}
-
-static void
-ble_sm_test_util_rx_public_key(uint16_t conn_handle,
-                               struct ble_sm_public_key *cmd)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int payload_len;
-    int rc;
-
-    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PUBLIC_KEY_SZ);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    payload_len = BLE_SM_HDR_SZ + BLE_SM_PUBLIC_KEY_SZ;
-
-    v = os_mbuf_extend(om, payload_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    ble_sm_public_key_write(v, payload_len, cmd);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
-                                              &hci_hdr, om);
-    TEST_ASSERT_FATAL(rc == 0);
-}
-
-static void
-ble_sm_test_util_rx_dhkey_check(uint16_t conn_handle,
-                                struct ble_sm_dhkey_check *cmd,
-                                int exp_status)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int payload_len;
-    int rc;
-
-    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_DHKEY_CHECK_SZ);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    payload_len = BLE_SM_HDR_SZ + BLE_SM_DHKEY_CHECK_SZ;
-
-    v = os_mbuf_extend(om, payload_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    ble_sm_dhkey_check_write(v, payload_len, cmd);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
-                                              &hci_hdr, om);
-    TEST_ASSERT_FATAL(rc == exp_status);
-}
-
-static void
-ble_sm_test_util_rx_enc_info(uint16_t conn_handle,
-                             struct ble_sm_enc_info *cmd,
-                             int exp_status)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int payload_len;
-    int rc;
-
-    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_ENC_INFO_SZ);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    payload_len = BLE_SM_HDR_SZ + BLE_SM_ENC_INFO_SZ;
-
-    v = os_mbuf_extend(om, payload_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    ble_sm_enc_info_write(v, payload_len, cmd);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
-                                              &hci_hdr, om);
-    TEST_ASSERT_FATAL(rc == exp_status);
-}
-
-static void
-ble_sm_test_util_rx_master_id(uint16_t conn_handle,
-                             struct ble_sm_master_id *cmd,
-                             int exp_status)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int payload_len;
-    int rc;
-
-    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_MASTER_ID_SZ);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    payload_len = BLE_SM_HDR_SZ + BLE_SM_MASTER_ID_SZ;
-
-    v = os_mbuf_extend(om, payload_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    ble_sm_master_id_write(v, payload_len, cmd);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
-                                              &hci_hdr, om);
-    TEST_ASSERT_FATAL(rc == exp_status);
-}
-
-static void
-ble_sm_test_util_rx_id_info(uint16_t conn_handle,
-                            struct ble_sm_id_info *cmd,
-                            int exp_status)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int payload_len;
-    int rc;
-
-    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_ID_INFO_SZ);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    payload_len = BLE_SM_HDR_SZ + BLE_SM_ID_INFO_SZ;
-
-    v = os_mbuf_extend(om, payload_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    ble_sm_id_info_write(v, payload_len, cmd);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
-                                              &hci_hdr, om);
-    TEST_ASSERT_FATAL(rc == exp_status);
-}
-
-static void
-ble_sm_test_util_rx_id_addr_info(uint16_t conn_handle,
-                                 struct ble_sm_id_addr_info *cmd,
-                                 int exp_status)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int payload_len;
-    int rc;
-
-    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_ID_ADDR_INFO_SZ);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    payload_len = BLE_SM_HDR_SZ + BLE_SM_ID_ADDR_INFO_SZ;
-
-    v = os_mbuf_extend(om, payload_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    ble_sm_id_addr_info_write(v, payload_len, cmd);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
-                                              &hci_hdr, om);
-    TEST_ASSERT_FATAL(rc == exp_status);
-}
-
-static void
-ble_sm_test_util_rx_sign_info(uint16_t conn_handle,
-                              struct ble_sm_sign_info *cmd,
-                              int exp_status)
-{
-    struct hci_data_hdr hci_hdr;
-    struct os_mbuf *om;
-    void *v;
-    int payload_len;
-    int rc;
-
-    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
-        2, BLE_HCI_PB_FIRST_FLUSH,
-        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_SIGN_INFO_SZ);
-
-    om = ble_hs_mbuf_l2cap_pkt();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    payload_len = BLE_SM_HDR_SZ + BLE_SM_SIGN_INFO_SZ;
-
-    v = os_mbuf_extend(om, payload_len);
-    TEST_ASSERT_FATAL(v != NULL);
-
-    ble_sm_sign_info_write(v, payload_len, cmd);
-
-    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
-                                              &hci_hdr, om);
-    TEST_ASSERT_FATAL(rc == exp_status);
-}
-
-static struct os_mbuf *
-ble_sm_test_util_verify_tx_hdr(uint8_t sm_op, uint16_t payload_len)
-{
-    struct os_mbuf *om;
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    TEST_ASSERT(OS_MBUF_PKTLEN(om) == BLE_SM_HDR_SZ + payload_len);
-    TEST_ASSERT_FATAL(om->om_data[0] == sm_op);
-
-    om->om_data += BLE_SM_HDR_SZ;
-    om->om_len -= BLE_SM_HDR_SZ;
-
-    return om;
-}
-
-static void
-ble_sm_test_util_verify_tx_pair_cmd(
-    uint8_t op,
-    struct ble_sm_pair_cmd *exp_cmd)
-{
-    struct ble_sm_pair_cmd cmd;
-    struct os_mbuf *om;
-
-    om = ble_sm_test_util_verify_tx_hdr(op, BLE_SM_PAIR_CMD_SZ);
-    ble_sm_pair_cmd_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(cmd.io_cap == exp_cmd->io_cap);
-    TEST_ASSERT(cmd.oob_data_flag == exp_cmd->oob_data_flag);
-    TEST_ASSERT(cmd.authreq == exp_cmd->authreq);
-    TEST_ASSERT(cmd.max_enc_key_size == exp_cmd->max_enc_key_size);
-    TEST_ASSERT(cmd.init_key_dist == exp_cmd->init_key_dist);
-    TEST_ASSERT(cmd.resp_key_dist == exp_cmd->resp_key_dist);
-}
-
-static void
-ble_sm_test_util_verify_tx_pair_req(
-    struct ble_sm_pair_cmd *exp_req)
-{
-    ble_sm_test_util_verify_tx_pair_cmd(BLE_SM_OP_PAIR_REQ,
-                                              exp_req);
-}
-
-static void
-ble_sm_test_util_verify_tx_pair_rsp(
-    struct ble_sm_pair_cmd *exp_rsp)
-{
-    ble_sm_test_util_verify_tx_pair_cmd(BLE_SM_OP_PAIR_RSP,
-                                              exp_rsp);
-}
-
-static void
-ble_sm_test_util_verify_tx_pair_confirm(
-    struct ble_sm_pair_confirm *exp_cmd)
-{
-    struct ble_sm_pair_confirm cmd;
-    struct os_mbuf *om;
-
-    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_CONFIRM,
-                                        BLE_SM_PAIR_CONFIRM_SZ);
-    ble_sm_pair_confirm_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(memcmp(cmd.value, exp_cmd->value, 16) == 0);
-}
-
-static void
-ble_sm_test_util_verify_tx_pair_random(
-    struct ble_sm_pair_random *exp_cmd)
-{
-    struct ble_sm_pair_random cmd;
-    struct os_mbuf *om;
-
-    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_RANDOM,
-                                        BLE_SM_PAIR_RANDOM_SZ);
-    ble_sm_pair_random_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(memcmp(cmd.value, exp_cmd->value, 16) == 0);
-}
-
-static void
-ble_sm_test_util_verify_tx_public_key(
-    struct ble_sm_public_key *exp_cmd)
-{
-    struct ble_sm_public_key cmd;
-    struct os_mbuf *om;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_PUBLIC_KEY,
-                                              BLE_SM_PUBLIC_KEY_SZ);
-    ble_sm_public_key_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(memcmp(cmd.x, exp_cmd->x, sizeof cmd.x) == 0);
-    TEST_ASSERT(memcmp(cmd.y, exp_cmd->y, sizeof cmd.y) == 0);
-}
-
-static void
-ble_sm_test_util_verify_tx_dhkey_check(
-    struct ble_sm_dhkey_check *exp_cmd)
-{
-    struct ble_sm_dhkey_check cmd;
-    struct os_mbuf *om;
-
-    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_DHKEY_CHECK,
-                                              BLE_SM_DHKEY_CHECK_SZ);
-    ble_sm_dhkey_check_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(memcmp(cmd.value, exp_cmd->value, 16) == 0);
-}
-
-static void
-ble_sm_test_util_verify_tx_enc_info(struct ble_sm_enc_info *exp_cmd)
-{
-    struct ble_sm_enc_info cmd;
-    struct os_mbuf *om;
-
-    ble_hs_test_util_tx_all();
-    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_ENC_INFO,
-                                        BLE_SM_ENC_INFO_SZ);
-    ble_sm_enc_info_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(memcmp(cmd.ltk, exp_cmd->ltk, 16) == 0);
-
-    /* Ensure LTK is sent in little endian. */
-    TEST_ASSERT(memcmp(om->om_data, cmd.ltk, 16) == 0);
-}
-
-static void
-ble_sm_test_util_verify_tx_master_id(struct ble_sm_master_id *exp_cmd)
-{
-    struct ble_sm_master_id cmd;
-    struct os_mbuf *om;
-
-    ble_hs_test_util_tx_all();
-    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_MASTER_ID,
-                                        BLE_SM_MASTER_ID_SZ);
-    ble_sm_master_id_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(cmd.ediv == exp_cmd->ediv);
-    TEST_ASSERT(cmd.rand_val == exp_cmd->rand_val);
-}
-
-static void
-ble_sm_test_util_verify_tx_id_info(struct ble_sm_id_info *exp_cmd)
-{
-    struct ble_sm_id_info cmd;
-    struct os_mbuf *om;
-
-    ble_hs_test_util_tx_all();
-    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_IDENTITY_INFO,
-                                        BLE_SM_ID_INFO_SZ);
-    ble_sm_id_info_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(memcmp(cmd.irk, exp_cmd->irk, 16) == 0);
-
-    /* Ensure IRK is sent in little endian. */
-    TEST_ASSERT(memcmp(om->om_data, cmd.irk, 16) == 0);
-}
-
-static void
-ble_sm_test_util_verify_tx_id_addr_info(struct ble_sm_id_addr_info *exp_cmd)
-{
-    struct ble_sm_id_addr_info cmd;
-    struct os_mbuf *om;
-    const uint8_t *our_id_addr;
-    int rc;
-
-    ble_hs_lock();
-    rc = ble_hs_id_addr(exp_cmd->addr_type, &our_id_addr, NULL);
-    ble_hs_unlock();
-
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_hs_test_util_tx_all();
-    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_IDENTITY_ADDR_INFO,
-                                        BLE_SM_ID_ADDR_INFO_SZ);
-    ble_sm_id_addr_info_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(cmd.addr_type == exp_cmd->addr_type);
-    TEST_ASSERT(memcmp(cmd.bd_addr, exp_cmd->bd_addr, 6) == 0);
-    TEST_ASSERT(memcmp(cmd.bd_addr, our_id_addr, 6) == 0);
-}
-
-static void
-ble_sm_test_util_verify_tx_sign_info(struct ble_sm_sign_info *exp_cmd)
-{
-    struct ble_sm_sign_info cmd;
-    struct os_mbuf *om;
-
-    ble_hs_test_util_tx_all();
-    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_SIGN_INFO,
-                                        BLE_SM_ID_INFO_SZ);
-    ble_sm_sign_info_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(memcmp(cmd.sig_key, exp_cmd->sig_key, 16) == 0);
-
-    /* Ensure CSRK is sent in little endian. */
-    TEST_ASSERT(memcmp(om->om_data, cmd.sig_key, 16) == 0);
-}
-
-static void
-ble_sm_test_util_verify_tx_sec_req(struct ble_sm_sec_req *exp_cmd)
-{
-    struct ble_sm_sec_req cmd;
-    struct os_mbuf *om;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_SEC_REQ, BLE_SM_SEC_REQ_SZ);
-    ble_sm_sec_req_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(cmd.authreq == exp_cmd->authreq);
-}
-
-void
-ble_sm_test_util_verify_tx_pair_fail(
-    struct ble_sm_pair_fail *exp_cmd)
-{
-    struct ble_sm_pair_fail cmd;
-    struct os_mbuf *om;
-
-    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_FAIL,
-                                        BLE_SM_PAIR_FAIL_SZ);
-    ble_sm_pair_fail_parse(om->om_data, om->om_len, &cmd);
-
-    TEST_ASSERT(cmd.reason == exp_cmd->reason);
-}
-
-static void
-ble_sm_test_util_rx_lt_key_req(uint16_t conn_handle, uint64_t r, uint16_t ediv)
-{
-    struct hci_le_lt_key_req evt;
-    int rc;
-
-    evt.subevent_code = BLE_HCI_LE_SUBEV_LT_KEY_REQ;
-    evt.connection_handle = conn_handle;
-    evt.random_number = r;
-    evt.encrypted_diversifier = ediv;
-
-    rc = ble_sm_ltk_req_rx(&evt);
-    TEST_ASSERT_FATAL(rc == 0);
-}
-
-static void
-ble_sm_test_util_verify_tx_lt_key_req_reply(uint16_t conn_handle, uint8_t *stk)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_LT_KEY_REQ_REPLY,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_LT_KEY_REQ_REPLY_LEN);
-    TEST_ASSERT(le16toh(param + 0) == conn_handle);
-    TEST_ASSERT(memcmp(param + 2, stk, 16) == 0);
-}
-
-static void
-ble_sm_test_util_verify_tx_lt_key_req_neg_reply(uint16_t conn_handle)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_LT_KEY_REQ_NEG_REPLY_LEN);
-    TEST_ASSERT(le16toh(param + 0) == conn_handle);
-}
-
-static void
-ble_sm_test_util_set_lt_key_req_neg_reply_ack(uint8_t status,
-                                              uint16_t conn_handle)
-{
-    static uint8_t params[BLE_HCI_LT_KEY_REQ_NEG_REPLY_ACK_PARAM_LEN];
-
-    htole16(params, conn_handle);
-    ble_hs_test_util_set_ack_params(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY),
-        status, params, sizeof params);
-}
-
-static void
-ble_sm_test_util_set_lt_key_req_reply_ack(uint8_t status, uint16_t conn_handle)
-{
-    static uint8_t params[BLE_HCI_LT_KEY_REQ_REPLY_ACK_PARAM_LEN];
-
-    htole16(params, conn_handle);
-    ble_hs_test_util_set_ack_params(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_LT_KEY_REQ_REPLY),
-        status, params, sizeof params);
-}
-
-static void
-ble_sm_test_util_rx_enc_change(uint16_t conn_handle, uint8_t status,
-                                     uint8_t encryption_enabled)
-{
-    struct hci_encrypt_change evt;
-
-    evt.status = status;
-    evt.encryption_enabled = encryption_enabled;
-    evt.connection_handle = conn_handle;
-
-    ble_sm_enc_change_rx(&evt);
-}
-
-static void
-ble_sm_test_util_verify_tx_start_enc(uint16_t conn_handle,
-                                     uint64_t random_number,
-                                     uint16_t ediv,
-                                     uint8_t *ltk)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_START_ENCRYPT,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_LE_START_ENCRYPT_LEN);
-    TEST_ASSERT(le16toh(param + 0) == conn_handle);
-    TEST_ASSERT(le64toh(param + 2) == random_number);
-    TEST_ASSERT(le16toh(param + 10) == ediv);
-    TEST_ASSERT(memcmp(param + 12, ltk, 16) == 0);
-}
-
-static void
-ble_sm_test_util_verify_tx_add_resolve_list(uint8_t peer_id_addr_type,
-                                            uint8_t *peer_id_addr,
-                                            uint8_t *peer_irk,
-                                            uint8_t *our_irk)
-{
-    uint8_t param_len;
-    uint8_t *param;
-
-    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
-                                           BLE_HCI_OCF_LE_ADD_RESOLV_LIST,
-                                           &param_len);
-    TEST_ASSERT(param_len == BLE_HCI_ADD_TO_RESOLV_LIST_LEN);
-    TEST_ASSERT(param[0] == peer_id_addr_type);
-    TEST_ASSERT(memcmp(param + 1, peer_id_addr, 6) == 0);
-
-    /* Ensure IRKs are sent in little endian. */
-    TEST_ASSERT(memcmp(param + 7, peer_irk, 16) == 0);
-    TEST_ASSERT(memcmp(param + 23, our_irk, 16) == 0);
-}
-
-void
-ble_sm_test_util_io_inject(struct ble_sm_test_passkey_info *passkey_info,
-                           uint8_t cur_sm_state)
-{
-    uint8_t io_sm_state;
-    int rc;
-
-    io_sm_state = ble_sm_ioact_state(passkey_info->passkey.action);
-    if (io_sm_state != cur_sm_state) {
-        return;
-    }
-
-    if (passkey_info->passkey.action == BLE_SM_IOACT_NUMCMP) {
-        TEST_ASSERT(ble_sm_test_ioact.numcmp == passkey_info->exp_numcmp);
-    }
-
-    rc = ble_sm_inject_io(2, &passkey_info->passkey);
-    TEST_ASSERT(rc == 0);
-}
-
-void
-ble_sm_test_util_io_inject_bad(uint16_t conn_handle, uint8_t correct_io_act)
-{
-    struct ble_sm_proc *proc;
-    struct ble_sm_io io;
-    uint8_t io_sm_state;
-    int already_injected;
-    int rc;
-    int i;
-
-    /* Lock mutex to prevent thread-safety assert from failing. */
-    ble_hs_lock();
-    proc = ble_sm_proc_find(conn_handle, BLE_SM_PROC_STATE_NONE, -1, NULL);
-    ble_hs_unlock();
-
-    TEST_ASSERT_FATAL(proc != NULL);
-
-    io_sm_state = ble_sm_ioact_state(correct_io_act);
-
-    for (i = 1; i < BLE_SM_IOACT_MAX_PLUS_ONE; i++) {
-        if (io_sm_state != proc->state  ||
-            i != correct_io_act         ||
-            proc->flags & BLE_SM_PROC_F_IO_INJECTED) {
-
-            already_injected = proc->flags & BLE_SM_PROC_F_IO_INJECTED;
-
-            io.action = i;
-            rc = ble_sm_inject_io(conn_handle, &io);
-
-            if (already_injected) {
-                TEST_ASSERT(rc == BLE_HS_EALREADY);
-            } else {
-                TEST_ASSERT(rc == BLE_HS_EINVAL);
-            }
-        }
-    }
-}
-
-void
-ble_sm_test_util_io_check_pre(struct ble_sm_test_passkey_info *passkey_info,
-                              uint8_t cur_sm_state)
-{
-    uint8_t io_sm_state;
-    int rc;
-
-    io_sm_state = ble_sm_ioact_state(passkey_info->passkey.action);
-    if (io_sm_state != cur_sm_state) {
-        return;
-    }
-
-    if (!passkey_info->io_before_rx) {
-        return;
-    }
-
-    if (passkey_info->passkey.action == BLE_SM_IOACT_NUMCMP) {
-        TEST_ASSERT(ble_sm_test_ioact.numcmp == passkey_info->exp_numcmp);
-    }
-
-    rc = ble_sm_inject_io(2, &passkey_info->passkey);
-    TEST_ASSERT(rc == 0);
-}
-
-void
-ble_sm_test_util_io_check_post(struct ble_sm_test_passkey_info *passkey_info,
-                               uint8_t cur_sm_state)
-{
-    uint8_t io_sm_state;
-    int rc;
-
-    io_sm_state = ble_sm_ioact_state(passkey_info->passkey.action);
-    if (io_sm_state != cur_sm_state) {
-        return;
-    }
-
-    if (passkey_info->io_before_rx) {
-        return;
-    }
-
-    if (passkey_info->passkey.action == BLE_SM_IOACT_NUMCMP) {
-        TEST_ASSERT(ble_sm_test_ioact.numcmp == passkey_info->exp_numcmp);
-    }
-
-    /* Ensure response not sent until user performs IO. */
-    ble_hs_test_util_tx_all();
-    TEST_ASSERT(ble_hs_test_util_prev_tx_queue_sz() == 0);
-
-    rc = ble_sm_inject_io(2, &passkey_info->passkey);
-    TEST_ASSERT_FATAL(rc == 0);
-}
-
-static void
-ble_sm_test_util_verify_persist(struct ble_sm_test_params *params,
-                                int we_are_initiator)
-{
-    struct ble_sm_test_util_entity peer_entity;
-    struct ble_sm_test_util_entity our_entity;
-    struct ble_store_value_sec value_sec;
-    struct ble_store_key_sec key_sec;
-    int csrk_expected;
-    int ltk_expected;
-    int peer_irk_expected;
-    int our_irk_expected;
-    int bonding;
-    int sc;
-    int rc;
-
-    ble_sm_test_util_params_to_entities(params, we_are_initiator,
-                                        &our_entity, &peer_entity);
-
-    sc = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_SC &&
-         params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_SC;
-
-    bonding = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_BOND &&
-              params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_BOND;
-
-    memset(&key_sec, 0, sizeof key_sec);
-    key_sec.peer_addr_type = BLE_STORE_ADDR_TYPE_NONE;
-
-    rc = ble_store_read_peer_sec(&key_sec, &value_sec);
-    if (!bonding) {
-        TEST_ASSERT(rc == BLE_HS_ENOENT);
-        peer_irk_expected = 0;
-    } else {
-        TEST_ASSERT_FATAL(rc == 0);
-
-        ltk_expected =
-            sc || !!(peer_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ENC);
-        peer_irk_expected =
-            !!(peer_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ID);
-        csrk_expected =
-            !!(peer_entity.key_dist & BLE_SM_PAIR_KEY_DIST_SIGN);
-
-        TEST_ASSERT(value_sec.peer_addr_type == peer_entity.id_addr_type);
-        TEST_ASSERT(memcmp(value_sec.peer_addr, peer_entity.id_addr, 6) == 0);
-        TEST_ASSERT(value_sec.ediv == peer_entity.ediv);
-        TEST_ASSERT(value_sec.rand_num == peer_entity.rand_num);
-        TEST_ASSERT(value_sec.authenticated == params->authenticated);
-
-        TEST_ASSERT(value_sec.ltk_present == ltk_expected);
-        TEST_ASSERT(memcmp(value_sec.ltk, peer_entity.ltk, 16) == 0);
-
-        TEST_ASSERT(value_sec.irk_present == peer_irk_expected);
-        if (peer_irk_expected) {
-            TEST_ASSERT(memcmp(value_sec.irk,
-                               peer_entity.id_info->irk, 16) == 0);
-        }
-
-        TEST_ASSERT(value_sec.csrk_present == csrk_expected);
-        if (csrk_expected) {
-            TEST_ASSERT(memcmp(value_sec.csrk,
-                               peer_entity.sign_info->sig_key, 16) == 0);
-        }
-    }
-
-    rc = ble_store_read_our_sec(&key_sec, &value_sec);
-    if (!bonding) {
-        TEST_ASSERT(rc == BLE_HS_ENOENT);
-    } else {
-        TEST_ASSERT_FATAL(rc == 0);
-
-        ltk_expected =
-            sc || !!(our_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ENC);
-        our_irk_expected =
-            !!(our_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ID);
-        csrk_expected =
-            !!(our_entity.key_dist & BLE_SM_PAIR_KEY_DIST_SIGN);
-
-        TEST_ASSERT(value_sec.peer_addr_type == peer_entity.id_addr_type);
-        TEST_ASSERT(memcmp(value_sec.peer_addr, peer_entity.id_addr, 6) == 0);
-        TEST_ASSERT(value_sec.ediv == our_entity.ediv);
-        TEST_ASSERT(value_sec.rand_num == our_entity.rand_num);
-        TEST_ASSERT(value_sec.authenticated == params->authenticated);
-
-        TEST_ASSERT(value_sec.ltk_present == ltk_expected);
-        TEST_ASSERT(memcmp(value_sec.ltk, our_entity.ltk, 16) == 0);
-
-        TEST_ASSERT(value_sec.irk_present == our_irk_expected);
-        if (our_irk_expected) {
-            TEST_ASSERT(memcmp(value_sec.irk,
-                               our_entity.id_info->irk, 16) == 0);
-        }
-
-        TEST_ASSERT(value_sec.csrk_present == csrk_expected);
-        if (csrk_expected) {
-            TEST_ASSERT(memcmp(value_sec.csrk,
-                               our_entity.sign_info->sig_key, 16) == 0);
-        }
-    }
-
-    /* Verify no other keys were persisted. */
-    key_sec.idx++;
-    rc = ble_store_read_our_sec(&key_sec, &value_sec);
-    TEST_ASSERT_FATAL(rc == BLE_HS_ENOENT);
-    rc = ble_store_read_peer_sec(&key_sec, &value_sec);
-    TEST_ASSERT_FATAL(rc == BLE_HS_ENOENT);
-
-    /* Verify we sent the peer's IRK to the controller. */
-    if (peer_irk_expected) {
-        ble_sm_test_util_verify_tx_add_resolve_list(peer_entity.id_addr_type,
-                                                    peer_entity.id_addr,
-                                                    peer_entity.id_info->irk,
-                                                    our_entity.id_info->irk);
-    }
-}
-
-static void
-ble_sm_test_util_peer_bonding_good(int send_enc_req,
-                                   uint8_t our_addr_type,
-                                   uint8_t *our_rpa,
-                                   uint8_t peer_addr_type,
-                                   uint8_t *peer_id_addr,
-                                   uint8_t *peer_rpa,
-                                   uint8_t *ltk, int authenticated,
-                                   uint16_t ediv, uint64_t rand_num)
-{
-    struct ble_hs_conn *conn;
-    int rc;
-
-    ble_hs_test_util_create_rpa_conn(2, our_addr_type, our_rpa, peer_addr_type,
-                                     peer_id_addr, peer_rpa,
-                                     ble_sm_test_util_conn_cb, NULL);
-
-    /* This test inspects and modifies the connection object after unlocking
-     * the host mutex.  It is not OK for real code to do this, but this test
-     * can assume the connection list is unchanging.
-     */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(2);
-    TEST_ASSERT_FATAL(conn != NULL);
-    conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
-    ble_hs_unlock();
-
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    if (send_enc_req) {
-        rc = ble_sm_slave_initiate(2);
-        TEST_ASSERT(rc == 0);
-    }
-
-    /* Receive a long term key request from the controller. */
-    ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
-    ble_sm_test_util_rx_lt_key_req(2, rand_num, ediv);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-
-    /* Ensure the LTK request event got sent to the application. */
-    TEST_ASSERT(ble_sm_test_store_obj_type ==
-                BLE_STORE_OBJ_TYPE_OUR_SEC);
-    TEST_ASSERT(ble_sm_test_store_key.sec.peer_addr_type ==
-                ble_hs_misc_addr_type_to_id(peer_addr_type));
-    TEST_ASSERT(ble_sm_test_store_key.sec.ediv_rand_present);
-    TEST_ASSERT(ble_sm_test_store_key.sec.ediv == ediv);
-    TEST_ASSERT(ble_sm_test_store_key.sec.rand_num == rand_num);
-
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
-
-    /* Ensure we sent the expected long term key request reply command. */
-    ble_sm_test_util_verify_tx_lt_key_req_reply(2, ltk);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
-
-    /* Receive an encryption changed event. */
-    ble_sm_test_util_rx_enc_change(2, 0, 1);
-
-    /* Pairing should now be complete. */
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Verify that security callback was executed. */
-    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
-    TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                authenticated);
-
-    /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                authenticated);
-
-    ble_hs_test_util_conn_disconnect(2);
-}
-
-void
-ble_sm_test_util_peer_bonding_bad(uint16_t ediv, uint64_t rand_num)
-{
-    struct ble_hs_conn *conn;
-
-    ble_sm_test_util_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[6]){1,2,3,4,5,6}),
-                                 ble_sm_test_util_conn_cb,
-                                 NULL);
-
-    /* This test inspects and modifies the connection object after unlocking
-     * the host mutex.  It is not OK for real code to do this, but this test
-     * can assume the connection list is unchanging.
-     */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(2);
-    TEST_ASSERT_FATAL(conn != NULL);
-    conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
-    ble_hs_unlock();
-
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Receive a long term key request from the controller. */
-    ble_sm_test_util_set_lt_key_req_neg_reply_ack(0, 2);
-    ble_sm_test_util_rx_lt_key_req(2, rand_num, ediv);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-
-    /* Ensure the LTK request event got sent to the application. */
-    TEST_ASSERT(ble_sm_test_store_obj_type ==
-                BLE_STORE_OBJ_TYPE_OUR_SEC);
-    TEST_ASSERT(ble_sm_test_store_key.sec.ediv_rand_present);
-    TEST_ASSERT(ble_sm_test_store_key.sec.ediv == ediv);
-    TEST_ASSERT(ble_sm_test_store_key.sec.rand_num == rand_num);
-
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-
-    /* Ensure we sent the expected long term key request neg reply command. */
-    ble_sm_test_util_verify_tx_lt_key_req_neg_reply(2);
-
-    /* Ensure the security procedure was aborted. */
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(!conn->bhc_sec_state.authenticated);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-}
-
-/**
- * @param send_enc_req          Whether this procedure is initiated by a slave
- *                                  security request;
- *                                  1: Peer sends a security request at start.
- *                                  0: No security request; we initiate.
- */
-static void
-ble_sm_test_util_us_bonding_good(int send_enc_req, uint8_t our_addr_type,
-                                 uint8_t *our_rpa,
-                                 uint8_t peer_addr_type,
-                                 uint8_t *peer_id_addr, uint8_t *peer_rpa,
-                                 uint8_t *ltk, int authenticated,
-                                 uint16_t ediv, uint64_t rand_num)
-{
-    struct ble_sm_sec_req sec_req;
-    struct ble_hs_conn *conn;
-
-    ble_hs_test_util_create_rpa_conn(2, our_addr_type, our_rpa,
-                                     peer_addr_type, peer_id_addr,
-                                     peer_rpa, ble_sm_test_util_conn_cb, NULL);
-
-    /* This test inspects and modifies the connection object after unlocking
-     * the host mutex.  It is not OK for real code to do this, but this test
-     * can assume the connection list is unchanging.
-     */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(2);
-    TEST_ASSERT_FATAL(conn != NULL);
-    ble_hs_unlock();
-
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    ble_hs_test_util_set_ack(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_START_ENCRYPT),
-        0);
-
-    if (send_enc_req) {
-        sec_req.authreq = 0;
-        sec_req.authreq |= BLE_SM_PAIR_AUTHREQ_BOND;
-        if (authenticated) {
-            sec_req.authreq |= BLE_SM_PAIR_AUTHREQ_MITM;
-        }
-        ble_sm_test_util_rx_sec_req(2, &sec_req, 0);
-    } else {
-        ble_gap_security_initiate(2);
-    }
-
-    /* Ensure we sent the expected start encryption command. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_start_enc(2, rand_num, ediv, ltk);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
-
-    /* Receive an encryption changed event. */
-    ble_sm_test_util_rx_enc_change(2, 0, 1);
-
-    /* Pairing should now be complete. */
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Verify that security callback was executed. */
-    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
-    TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                authenticated);
-
-    /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                authenticated);
-
-    ble_hs_test_util_conn_disconnect(2);
-}
-
-void
-ble_sm_test_util_peer_fail_inval(
-    int we_are_master,
-    uint8_t *init_id_addr,
-    uint8_t *resp_addr,
-    struct ble_sm_pair_cmd *pair_req,
-    struct ble_sm_pair_fail *pair_fail)
-{
-    struct ble_hs_conn *conn;
-
-    ble_sm_test_util_init();
-    ble_hs_id_set_pub(resp_addr);
-
-    ble_hs_test_util_create_conn(2, init_id_addr, ble_sm_test_util_conn_cb,
-                                 NULL);
-
-    /* This test inspects and modifies the connection object after unlocking
-     * the host mutex.  It is not OK for real code to do this, but this test
-     * can assume the connection list is unchanging.
-     */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(2);
-    TEST_ASSERT_FATAL(conn != NULL);
-    ble_hs_unlock();
-
-    if (!we_are_master) {
-        conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
-    }
-
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Receive a pair request from the peer. */
-    ble_sm_test_util_rx_pair_req(2, pair_req,
-                                 BLE_HS_SM_US_ERR(pair_fail->reason));
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Ensure we sent the expected pair fail. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_fail(pair_fail);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Verify that security callback was not executed. */
-    TEST_ASSERT(ble_sm_test_gap_event_type == -1);
-    TEST_ASSERT(ble_sm_test_gap_status == -1);
-
-    /* Verify that connection has correct security state. */
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(!conn->bhc_sec_state.authenticated);
-}
-
-void
-ble_sm_test_util_peer_lgcy_fail_confirm(
-    uint8_t *init_id_addr,
-    uint8_t *resp_addr,
-    struct ble_sm_pair_cmd *pair_req,
-    struct ble_sm_pair_cmd *pair_rsp,
-    struct ble_sm_pair_confirm *confirm_req,
-    struct ble_sm_pair_confirm *confirm_rsp,
-    struct ble_sm_pair_random *random_req,
-    struct ble_sm_pair_random *random_rsp,
-    struct ble_sm_pair_fail *fail_rsp)
-{
-    struct ble_hs_conn *conn;
-
-    ble_sm_test_util_init();
-    ble_hs_id_set_pub(resp_addr);
-    ble_sm_dbg_set_next_pair_rand(random_rsp->value);
-
-    ble_hs_test_util_create_conn(2, init_id_addr, ble_sm_test_util_conn_cb,
-                                 NULL);
-
-    /* This test inspects and modifies the connection object after unlocking
-     * the host mutex.  It is not OK for real code to do this, but this test
-     * can assume the connection list is unchanging.
-     */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(2);
-    TEST_ASSERT_FATAL(conn != NULL);
-    ble_hs_unlock();
-
-    /* Peer is the initiator so we must be the slave. */
-    conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
-
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Receive a pair request from the peer. */
-    ble_sm_test_util_rx_pair_req(2, pair_req, 0);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
-
-    /* Ensure we sent the expected pair response. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_rsp(pair_rsp);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
-
-    /* Receive a pair confirm from the peer. */
-    ble_sm_test_util_rx_confirm(2, confirm_req);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
-
-    /* Ensure we sent the expected pair confirm. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_confirm(confirm_rsp);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
-
-    /* Receive a pair random from the peer. */
-    ble_sm_test_util_rx_random(
-        2, random_req, BLE_HS_SM_US_ERR(BLE_SM_ERR_CONFIRM_MISMATCH));
-
-    /* Ensure we sent the expected pair fail. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_fail(fail_rsp);
-
-    /* The proc should now be freed. */
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Verify that security callback was executed. */
-    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
-    TEST_ASSERT(ble_sm_test_gap_status ==
-                BLE_HS_SM_US_ERR(BLE_SM_ERR_CONFIRM_MISMATCH));
-    TEST_ASSERT(!ble_sm_test_sec_state.encrypted);
-    TEST_ASSERT(!ble_sm_test_sec_state.authenticated);
-
-    /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
-                conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                conn->bhc_sec_state.authenticated);
-}
-
-static void
-ble_sm_test_util_bonding_all(struct ble_sm_test_params *params,
-                             int we_are_original_initiator)
-{
-    struct ble_sm_test_util_entity peer_entity;
-    struct ble_sm_test_util_entity our_entity;
-    int sc;
-
-    if (!(params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_BOND) ||
-        !(params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_BOND)) {
-
-        /* Bonding not performed. */
-        return;
-    }
-
-    sc = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_SC &&
-         params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_SC;
-
-    ble_sm_test_util_params_to_entities(params, we_are_original_initiator,
-                                        &our_entity, &peer_entity);
-
-    if (sc || peer_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ENC) {
-        /* We are master; we initiate procedure. */
-        ble_sm_test_util_us_bonding_good(0, our_entity.addr_type,
-                                         our_entity.rpa,
-                                         peer_entity.addr_type,
-                                         peer_entity.id_addr,
-                                         peer_entity.rpa,
-                                         peer_entity.ltk,
-                                         params->authenticated,
-                                         peer_entity.ediv,
-                                         peer_entity.rand_num);
-
-        /* We are master; peer initiates procedure via security request. */
-        ble_sm_test_util_us_bonding_good(1, our_entity.addr_type,
-                                         our_entity.rpa,
-                                         peer_entity.addr_type,
-                                         peer_entity.id_addr,
-                                         peer_entity.rpa,
-                                         peer_entity.ltk,
-                                         params->authenticated,
-                                         peer_entity.ediv,
-                                         peer_entity.rand_num);
-    }
-
-    if (sc || our_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ENC) {
-        /* Peer is master; peer initiates procedure. */
-        ble_sm_test_util_peer_bonding_good(0, our_entity.addr_type,
-                                           our_entity.rpa,
-                                           peer_entity.addr_type,
-                                           peer_entity.id_addr,
-                                           peer_entity.rpa,
-                                           our_entity.ltk,
-                                           params->authenticated,
-                                           our_entity.ediv,
-                                           our_entity.rand_num);
-
-        /* Peer is master; we initiate procedure via security request. */
-        ble_sm_test_util_peer_bonding_good(1, our_entity.addr_type,
-                                           our_entity.rpa,
-                                           peer_entity.addr_type,
-                                           peer_entity.id_addr,
-                                           peer_entity.rpa,
-                                           our_entity.ltk,
-                                           params->authenticated,
-                                           our_entity.ediv,
-                                           our_entity.rand_num);
-    }
-}
-
-static void
-ble_sm_test_util_rx_keys(struct ble_sm_test_params *params,
-                         int we_are_initiator)
-{
-    struct ble_sm_id_addr_info *peer_id_addr_info;
-    struct ble_sm_sign_info *peer_sign_info;
-    struct ble_sm_master_id *peer_master_id;
-    struct ble_sm_enc_info *peer_enc_info;
-    struct ble_sm_id_info *peer_id_info;
-    uint8_t peer_key_dist;
-    int sc;
-
-    if (we_are_initiator) {
-        peer_key_dist = params->pair_rsp.resp_key_dist;
-        peer_id_addr_info = &params->id_addr_info_req;
-        peer_sign_info = &params->sign_info_req;
-        peer_master_id = &params->master_id_req;
-        peer_enc_info = &params->enc_info_req;
-        peer_id_info = &params->id_info_req;
-    } else {
-        peer_key_dist = params->pair_rsp.init_key_dist;
-        peer_id_addr_info = &params->id_addr_info_rsp;
-        peer_sign_info = &params->sign_info_rsp;
-        peer_master_id = &params->master_id_rsp;
-        peer_enc_info = &params->enc_info_rsp;
-        peer_id_info = &params->id_info_rsp;
-    }
-
-    sc = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_SC &&
-         params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_SC;
-
-    /* Receive key material from peer. */
-    if (!sc && (peer_key_dist & BLE_SM_PAIR_KEY_DIST_ENC)) {
-        ble_sm_test_util_rx_enc_info(2, peer_enc_info, 0);
-        ble_sm_test_util_rx_master_id(2, peer_master_id, 0);
-    }
-    if (peer_key_dist & BLE_SM_PAIR_KEY_DIST_ID) {
-        ble_hs_test_util_set_ack(
-            ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                        BLE_HCI_OCF_LE_ADD_RESOLV_LIST), 0);
-        ble_sm_test_util_rx_id_info(2, peer_id_info, 0);
-        ble_sm_test_util_rx_id_addr_info(2, peer_id_addr_info, 0);
-    }
-    if (peer_key_dist & BLE_SM_PAIR_KEY_DIST_SIGN) {
-        ble_sm_test_util_rx_sign_info(2, peer_sign_info, 0);
-    }
-}
-
-static void
-ble_sm_test_util_verify_tx_keys(struct ble_sm_test_params *params,
-                                int we_are_initiator)
-{
-    struct ble_sm_id_addr_info *our_id_addr_info;
-    struct ble_sm_sign_info *our_sign_info;
-    struct ble_sm_master_id *our_master_id;
-    struct ble_sm_enc_info *our_enc_info;
-    struct ble_sm_id_info *our_id_info;
-    uint8_t our_key_dist;
-    int sc;
-
-    if (we_are_initiator) {
-        our_key_dist = params->pair_rsp.init_key_dist;
-        our_id_addr_info = &params->id_addr_info_rsp;
-        our_sign_info = &params->sign_info_rsp;
-        our_master_id = &params->master_id_rsp;
-        our_enc_info = &params->enc_info_rsp;
-        our_id_info = &params->id_info_rsp;
-    } else {
-        our_key_dist = params->pair_rsp.resp_key_dist;
-        our_id_addr_info = &params->id_addr_info_req;
-        our_sign_info = &params->sign_info_req;
-        our_master_id = &params->master_id_req;
-        our_enc_info = &params->enc_info_req;
-        our_id_info = &params->id_info_req;
-    }
-
-    sc = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_SC &&
-         params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_SC;
-
-    if (!sc && our_key_dist & BLE_SM_PAIR_KEY_DIST_ENC) {
-        ble_sm_test_util_verify_tx_enc_info(our_enc_info);
-        ble_sm_test_util_verify_tx_master_id(our_master_id);
-    }
-    if (our_key_dist & BLE_SM_PAIR_KEY_DIST_ID) {
-        ble_sm_test_util_verify_tx_id_info(our_id_info);
-        ble_sm_test_util_verify_tx_id_addr_info(our_id_addr_info);
-    }
-    if (our_key_dist & BLE_SM_PAIR_KEY_DIST_SIGN) {
-        ble_sm_test_util_verify_tx_sign_info(our_sign_info);
-    }
-}
-
-static void
-ble_sm_test_util_us_lgcy_good_once(struct ble_sm_test_params *params)
-{
-    struct ble_sm_test_util_entity peer_entity;
-    struct ble_sm_test_util_entity our_entity;
-    struct ble_hs_conn *conn;
-    int rc;
-
-    ble_sm_test_util_init_good(params, 1, &conn, &our_entity, &peer_entity);
-
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    ble_hs_test_util_set_ack(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_START_ENCRYPT), 0);
-    if (params->sec_req.authreq != 0) {
-        ble_sm_test_util_rx_sec_req(2, &params->sec_req, 0);
-    } else {
-        /* Initiate the pairing procedure. */
-        rc = ble_gap_security_initiate(2);
-        TEST_ASSERT_FATAL(rc == 0);
-    }
-
-    /* Ensure we sent the expected pair request. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_req(our_entity.pair_cmd);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive a pair response from the peer. */
-    ble_sm_test_util_rx_pair_rsp(2, peer_entity.pair_cmd, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    ble_sm_test_util_io_inject(&params->passkey_info,
-                               BLE_SM_PROC_STATE_CONFIRM);
-
-    /* Ensure we sent the expected pair confirm. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_confirm(our_entity.confirms);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive a pair confirm from the peer. */
-    ble_sm_test_util_rx_confirm(2, peer_entity.confirms);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Ensure we sent the expected pair random. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_random(our_entity.randoms);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive a pair random from the peer. */
-    ble_sm_test_util_rx_random(2, peer_entity.randoms, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Ensure we sent the expected start encryption command. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_start_enc(2, 0, 0, params->stk);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive an encryption changed event. */
-    ble_sm_test_util_rx_enc_change(2, 0, 1);
-
-    /* Receive key material from peer. */
-    ble_sm_test_util_rx_keys(params, 1);
-
-    /* Verify key material gets sent to peer. */
-    ble_sm_test_util_verify_tx_keys(params, 1);
-
-    /* Pairing should now be complete. */
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Verify that security callback was executed. */
-    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
-    TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated == params->authenticated);
-
-    /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
-                conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                conn->bhc_sec_state.authenticated);
-
-    /* Verify the appropriate security material was persisted. */
-    ble_sm_test_util_verify_persist(params, 1);
-
-    ble_hs_test_util_conn_disconnect(2);
-}
-
-void
-ble_sm_test_util_us_lgcy_good(struct ble_sm_test_params *params)
-{
-    /*** We are master. */
-
-    /* We initiate pairing. */
-    params->sec_req.authreq = 0;
-    ble_sm_test_util_us_lgcy_good_once(params);
-
-    /* Peer initiates with security request. */
-    params->sec_req.authreq = params->pair_rsp.authreq;
-    ble_sm_test_util_us_lgcy_good_once(params);
-
-    /* Verify link can be restored via the encryption procedure. */
-    ble_sm_test_util_bonding_all(params, 1);
-}
-
-static void
-ble_sm_test_util_peer_lgcy_good_once(struct ble_sm_test_params *params)
-{
-    struct ble_sm_test_util_entity peer_entity;
-    struct ble_sm_test_util_entity our_entity;
-    struct ble_hs_conn *conn;
-    int rc;
-
-    ble_sm_test_util_init_good(params, 0, &conn, &our_entity, &peer_entity);
-
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    if (params->sec_req.authreq != 0) {
-        rc = ble_sm_slave_initiate(2);
-        TEST_ASSERT(rc == 0);
-
-        /* Ensure we sent the expected security request. */
-        ble_sm_test_util_verify_tx_sec_req(&params->sec_req);
-    }
-
-    /* Receive a pair request from the peer. */
-    ble_sm_test_util_rx_pair_req(2, peer_entity.pair_cmd, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Ensure we sent the expected pair response. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_rsp(our_entity.pair_cmd);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    ble_sm_test_util_io_check_pre(&params->passkey_info,
-                                  BLE_SM_PROC_STATE_CONFIRM);
-
-    /* Receive a pair confirm from the peer. */
-    ble_sm_test_util_rx_confirm(2, peer_entity.confirms);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    ble_sm_test_util_io_check_post(&params->passkey_info,
-                                   BLE_SM_PROC_STATE_CONFIRM);
-
-    /* Ensure we sent the expected pair confirm. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_confirm(our_entity.confirms);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive a pair random from the peer. */
-    ble_sm_test_util_rx_random(2, peer_entity.randoms, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Ensure we sent the expected pair random. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_random(our_entity.randoms);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive a long term key request from the controller. */
-    ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
-    ble_sm_test_util_rx_lt_key_req(2, 0, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Ensure we sent the expected long term key request reply command. */
-    ble_sm_test_util_verify_tx_lt_key_req_reply(2, params->stk);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive an encryption changed event. */
-    ble_sm_test_util_rx_enc_change(2, 0, 1);
-
-    /* Verify key material gets sent to peer. */
-    ble_sm_test_util_verify_tx_keys(params, 0);
-
-    /* Receive key material from peer. */
-    ble_sm_test_util_rx_keys(params, 0);
-
-    /* Pairing should now be complete. */
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Verify that security callback was executed. */
-    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
-    TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                params->authenticated);
-
-    /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
-                conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                conn->bhc_sec_state.authenticated);
-
-    /* Verify the appropriate security material was persisted. */
-    ble_sm_test_util_verify_persist(params, 0);
-
-    ble_hs_test_util_conn_disconnect(2);
-}
-
-void
-ble_sm_test_util_peer_lgcy_good(struct ble_sm_test_params *params)
-{
-    /*** Peer is master. */
-
-    /* Peer performs IO first; peer initiates pairing. */
-    params->passkey_info.io_before_rx = 0;
-    params->sec_req.authreq = 0;
-    ble_sm_test_util_peer_lgcy_good_once(params);
-
-    /* Peer performs IO first; we initiate with security request. */
-    params->passkey_info.io_before_rx = 0;
-    params->sec_req.authreq = params->pair_rsp.authreq;
-    ble_sm_test_util_peer_lgcy_good_once(params);
-
-    /* We perform IO first; peer initiates pairing. */
-    params->passkey_info.io_before_rx = 1;
-    params->sec_req.authreq = 0;
-    ble_sm_test_util_peer_lgcy_good_once(params);
-
-    /* We perform IO first; we initiate with security request. */
-    params->passkey_info.io_before_rx = 1;
-    params->sec_req.authreq = params->pair_rsp.authreq;
-    ble_sm_test_util_peer_lgcy_good_once(params);
-
-    /* Verify link can be restored via the encryption procedure. */
-    ble_sm_test_util_bonding_all(params, 0);
-}
-
-static void
-ble_sm_test_util_us_sc_good_once(struct ble_sm_test_params *params)
-{
-    struct ble_sm_test_util_entity peer_entity;
-    struct ble_sm_test_util_entity our_entity;
-    struct ble_hs_conn *conn;
-    int num_iters;
-    int rc;
-    int i;
-
-    ble_sm_test_util_init_good(params, 1, &conn, &our_entity, &peer_entity);
-
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    ble_hs_test_util_set_ack(
-        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
-                                    BLE_HCI_OCF_LE_START_ENCRYPT), 0);
-    if (params->sec_req.authreq != 0) {
-        ble_sm_test_util_rx_sec_req(2, &params->sec_req, 0);
-    } else {
-        /* Initiate the pairing procedure. */
-        rc = ble_gap_security_initiate(2);
-        TEST_ASSERT_FATAL(rc == 0);
-    }
-
-    /* Ensure we sent the expected pair request. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_req(our_entity.pair_cmd);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive a pair response from the peer. */
-    ble_sm_test_util_rx_pair_rsp(2, peer_entity.pair_cmd, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Ensure we sent the expected public key. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_public_key(our_entity.public_key);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive a public key from the peer. */
-    ble_sm_test_util_rx_public_key(2, peer_entity.public_key);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    switch (params->pair_alg) {
-    case BLE_SM_PAIR_ALG_PASSKEY:
-        num_iters = 20;
-        break;
-
-    default:
-        num_iters = 1;
-        break;
-    }
-
-    ble_sm_test_util_io_inject(&params->passkey_info,
-                               BLE_SM_PROC_STATE_CONFIRM);
-
-    for (i = 0; i < num_iters; i++) {
-        if (params->pair_alg != BLE_SM_PAIR_ALG_JW      &&
-            params->pair_alg != BLE_SM_PAIR_ALG_NUMCMP) {
-
-            if (i < num_iters - 1) {
-                ble_sm_dbg_set_next_pair_rand(
-                    our_entity.randoms[i + 1].value);
-            }
-
-            /* Ensure we sent the expected pair confirm. */
-            ble_hs_test_util_tx_all();
-            ble_sm_test_util_verify_tx_pair_confirm(our_entity.confirms + i);
-            TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-            TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-            ble_sm_test_util_io_inject_bad(
-                2, params->passkey_info.passkey.action);
-        }
-
-        /* Receive a pair confirm from the peer. */
-        ble_sm_test_util_rx_confirm(2, peer_entity.confirms + i);
-        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-        /* Ensure we sent the expected pair random. */
-        ble_hs_test_util_tx_all();
-        ble_sm_test_util_verify_tx_pair_random(our_entity.randoms + i);
-        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-        /* Receive a pair random from the peer. */
-        ble_sm_test_util_rx_random(2, peer_entity.randoms + i, 0);
-        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-    }
-
-    ble_sm_test_util_io_inject(&params->passkey_info,
-                               BLE_SM_PROC_STATE_DHKEY_CHECK);
-
-    /* Ensure we sent the expected dhkey check. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_dhkey_check(our_entity.dhkey_check);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive a dhkey check from the peer. */
-    ble_sm_test_util_rx_dhkey_check(2, peer_entity.dhkey_check, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Ensure we sent the expected start encryption command. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_start_enc(2, 0, 0, params->ltk);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive an encryption changed event. */
-    ble_sm_test_util_rx_enc_change(2, 0, 1);
-
-    /* Receive key material from peer. */
-    ble_sm_test_util_rx_keys(params, 1);
-
-    /* Verify key material gets sent to peer. */
-    ble_sm_test_util_verify_tx_keys(params, 1);
-
-    /* Pairing should now be complete. */
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Verify that security callback was executed. */
-    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
-    TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                params->authenticated);
-
-    /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
-                conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                conn->bhc_sec_state.authenticated);
-
-    /* Verify the appropriate security material was persisted. */
-    ble_sm_test_util_verify_persist(params, 1);
-
-    ble_hs_test_util_conn_disconnect(2);
-}
-
-void
-ble_sm_test_util_us_sc_good(struct ble_sm_test_params *params)
-{
-    /*** We are master. */
-
-    /* We initiate pairing. */
-    params->passkey_info.io_before_rx = 0;
-    params->sec_req.authreq = 0;
-    ble_sm_test_util_us_sc_good_once(params);
-
-    /* Peer initiates with security request. */
-    params->passkey_info.io_before_rx = 0;
-    params->sec_req.authreq = params->pair_rsp.authreq;
-    ble_sm_test_util_us_sc_good_once(params);
-
-    /* Verify link can be restored via the encryption procedure. */
-    ble_sm_test_util_bonding_all(params, 1);
-}
-
-static void
-ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_params *params)
-{
-    struct ble_sm_test_util_entity peer_entity;
-    struct ble_sm_test_util_entity our_entity;
-    struct ble_hs_conn *conn;
-    int num_iters;
-    int rc;
-    int i;
-
-    ble_sm_test_util_init_good(params, 0, &conn, &our_entity, &peer_entity);
-
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    if (params->sec_req.authreq != 0) {
-        rc = ble_sm_slave_initiate(2);
-        TEST_ASSERT(rc == 0);
-
-        /* Ensure we sent the expected security request. */
-        ble_sm_test_util_verify_tx_sec_req(&params->sec_req);
-    }
-
-    /* Receive a pair request from the peer. */
-    ble_sm_test_util_rx_pair_req(2, peer_entity.pair_cmd, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Ensure we sent the expected pair response. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_rsp(our_entity.pair_cmd);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive a public key from the peer. */
-    ble_sm_test_util_rx_public_key(2, peer_entity.public_key);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Ensure we sent the expected public key. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_public_key(our_entity.public_key);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    switch (params->pair_alg) {
-    case BLE_SM_PAIR_ALG_PASSKEY:
-        num_iters = 20;
-        break;
-
-    default:
-        num_iters = 1;
-        break;
-    }
-
-    ble_sm_test_util_io_check_pre(&params->passkey_info,
-                                  BLE_SM_PROC_STATE_CONFIRM);
-
-    for (i = 0; i < num_iters; i++) {
-        if (params->pair_alg != BLE_SM_PAIR_ALG_JW      &&
-            params->pair_alg != BLE_SM_PAIR_ALG_NUMCMP) {
-
-            /* Receive a pair confirm from the peer. */
-            ble_sm_test_util_rx_confirm(2, peer_entity.confirms + i);
-            TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-            TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-            ble_sm_test_util_io_inject_bad(
-                2, params->passkey_info.passkey.action);
-
-            if (i < num_iters - 1) {
-                ble_sm_dbg_set_next_pair_rand(
-                    our_entity.randoms[i + 1].value);
-            }
-        }
-
-        if (i == 0) {
-            ble_sm_test_util_io_check_post(&params->passkey_info,
-                                           BLE_SM_PROC_STATE_CONFIRM);
-        }
-
-        /* Ensure we sent the expected pair confirm. */
-        ble_hs_test_util_tx_all();
-        ble_sm_test_util_verify_tx_pair_confirm(our_entity.confirms + i);
-        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-        /* Receive a pair random from the peer. */
-        ble_sm_test_util_rx_random(2, peer_entity.randoms + i, 0);
-        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-        /* Ensure we sent the expected pair random. */
-        ble_hs_test_util_tx_all();
-        ble_sm_test_util_verify_tx_pair_random(our_entity.randoms + i);
-        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    }
-
-    ble_sm_test_util_io_check_pre(&params->passkey_info,
-                                  BLE_SM_PROC_STATE_DHKEY_CHECK);
-
-    /* Receive a dhkey check from the peer. */
-    ble_sm_test_util_rx_dhkey_check(2, peer_entity.dhkey_check, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    ble_sm_test_util_io_check_post(&params->passkey_info,
-                                   BLE_SM_PROC_STATE_DHKEY_CHECK);
-
-    /* Ensure we sent the expected dhkey check. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_dhkey_check(our_entity.dhkey_check);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive a long term key request from the controller. */
-    ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
-    ble_sm_test_util_rx_lt_key_req(2, 0, 0);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Ensure we sent the expected long term key request reply command. */
-    ble_sm_test_util_verify_tx_lt_key_req_reply(2, params->ltk);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive an encryption changed event. */
-    ble_sm_test_util_rx_enc_change(2, 0, 1);
-
-    /* Verify key material gets sent to peer. */
-    ble_sm_test_util_verify_tx_keys(params, 0);
-
-    /* Receive key material from peer. */
-    ble_sm_test_util_rx_keys(params, 0);
-
-    /* Pairing should now be complete. */
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Verify that security callback was executed. */
-    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
-    TEST_ASSERT(ble_sm_test_gap_status == 0);
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                params->authenticated);
-
-    /* Verify that connection has correct security state. */
-    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
-                conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
-                conn->bhc_sec_state.authenticated);
-
-    /* Verify the appropriate security material was persisted. */
-    ble_sm_test_util_verify_persist(params, 0);
-
-    ble_hs_test_util_conn_disconnect(2);
-}
-
-void
-ble_sm_test_util_peer_sc_good(struct ble_sm_test_params *params)
-{
-    /*** Peer is master. */
-
-    /* Peer performs IO first; peer initiates pairing. */
-    params->passkey_info.io_before_rx = 0;
-    params->sec_req.authreq = 0;
-    ble_sm_test_util_peer_sc_good_once(params);
-
-    /* Peer performs IO first; we initiate with security request. */
-    params->passkey_info.io_before_rx = 0;
-    params->sec_req.authreq = params->pair_req.authreq;
-    ble_sm_test_util_peer_sc_good_once(params);
-
-    /* We perform IO first; peer initiates pairing. */
-    params->passkey_info.io_before_rx = 1;
-    params->sec_req.authreq = 0;
-    ble_sm_test_util_peer_sc_good_once(params);
-
-    /* We perform IO first; we initiate with security request. */
-    params->passkey_info.io_before_rx = 1;
-    params->sec_req.authreq = params->pair_req.authreq;
-    ble_sm_test_util_peer_sc_good_once(params);
-
-    /* Verify link can be restored via the encryption procedure. */
-    ble_sm_test_util_bonding_all(params, 0);
-}
-
-void
-ble_sm_test_util_us_fail_inval(struct ble_sm_test_params *params)
-{
-    struct ble_hs_conn *conn;
-    int rc;
-
-    ble_sm_test_util_init();
-    ble_hs_id_set_pub(params->resp_id_addr);
-
-    ble_sm_dbg_set_next_pair_rand(((uint8_t[16]){0}));
-
-    ble_hs_test_util_create_conn(2, params->init_id_addr,
-                                 ble_sm_test_util_conn_cb,
-                                 NULL);
-
-    /* This test inspects and modifies the connection object after unlocking
-     * the host mutex.  It is not OK for real code to do this, but this test
-     * can assume the connection list is unchanging.
-     */
-    ble_hs_lock();
-    conn = ble_hs_conn_find(2);
-    TEST_ASSERT_FATAL(conn != NULL);
-    ble_hs_unlock();
-
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Initiate the pairing procedure. */
-    rc = ble_hs_test_util_security_initiate(2, 0);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Ensure we sent the expected pair request. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_req(&params->pair_req);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
-    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
-
-    /* Receive a pair response from the peer. */
-    ble_sm_test_util_rx_pair_rsp(
-        2, &params->pair_rsp, BLE_HS_SM_US_ERR(BLE_SM_ERR_INVAL));
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Ensure we sent the expected pair fail. */
-    ble_hs_test_util_tx_all();
-    ble_sm_test_util_verify_tx_pair_fail(&params->pair_fail);
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
-
-    /* Verify that security callback was not executed. */
-    TEST_ASSERT(ble_sm_test_gap_event_type == -1);
-    TEST_ASSERT(ble_sm_test_gap_status == -1);
-
-    /* Verify that connection has correct security state. */
-    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
-    TEST_ASSERT(!conn->bhc_sec_state.authenticated);
-}


[28/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/src/test/sem_test.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/sem_test.c b/libs/os/src/test/sem_test.c
deleted file mode 100644
index ec79185..0000000
--- a/libs/os/src/test/sem_test.c
+++ /dev/null
@@ -1,401 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <string.h>
-#include "testutil/testutil.h"
-#include "os/os.h"
-#include "os/os_cfg.h"
-#include "os/os_sem.h"
-#include "os_test_priv.h"
-
-#ifdef ARCH_sim
-#define SEM_TEST_STACK_SIZE     1024
-#else 
-#define SEM_TEST_STACK_SIZE     512
-#endif
-
-struct os_task task1;
-os_stack_t stack1[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
-
-struct os_task task2;
-os_stack_t stack2[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
-
-struct os_task task3;
-os_stack_t stack3[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
-
-struct os_task task4;
-os_stack_t stack4[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
-
-#define TASK1_PRIO (1) 
-#define TASK2_PRIO (2) 
-#define TASK3_PRIO (3) 
-#define TASK4_PRIO (4) 
-
-struct os_sem g_sem1;
-
-/* 
- * TEST NUMBERS:
- *  10: In this test we have the highest priority task getting the semaphore
- *  then sleeping. Two lower priority tasks then wake up and attempt to get
- *  the semaphore. They are blocked until the higher priority task releases
- *  the semaphore, at which point the lower priority tasks should wake up in
- *  order, get the semaphore, then release it and go back to sleep.
- * 
- */
-
-/**
- * sem test disp sem
- *  
- * Display semaphore contents 
- * 
- * @param sem 
- */
-static const char *
-sem_test_sem_to_s(const struct os_sem *sem)
-{
-    static char buf[128];
-
-    snprintf(buf, sizeof buf, "\tSemaphore: tokens=%u head=%p",
-             sem->sem_tokens, SLIST_FIRST(&sem->sem_head));
-
-    return buf;
-}
-
-static void 
-sem_test_sleep_task_handler(void *arg)
-{
-    struct os_task *t;
-
-    t = os_sched_get_current_task();
-    TEST_ASSERT(t->t_func == sem_test_sleep_task_handler);
-
-    os_time_delay(2000);
-    os_test_restart();
-}
-
-static void
-sem_test_pend_release_loop(int delay, int timeout, int itvl)
-{
-    os_error_t err;
-
-    os_time_delay(delay);
-
-    while (1) {
-        err = os_sem_pend(&g_sem1, timeout);
-        TEST_ASSERT((err == OS_OK) || (err == OS_TIMEOUT));
-
-        err = os_sem_release(&g_sem1);
-        TEST_ASSERT(err == OS_OK);
-
-        os_time_delay(itvl);
-    }
-}
-
-/**
- * sem test basic 
- *  
- * Basic semaphore tests
- * 
- * @return int 
- */
-static void 
-sem_test_basic_handler(void *arg)
-{
-    struct os_task *t;
-    struct os_sem *sem;
-    os_error_t err;
-
-    sem = &g_sem1;
-    t = os_sched_get_current_task();
-
-    /* Test some error cases */
-    TEST_ASSERT(os_sem_init(NULL, 1)    == OS_INVALID_PARM);
-    TEST_ASSERT(os_sem_release(NULL)    == OS_INVALID_PARM);
-    TEST_ASSERT(os_sem_pend(NULL, 1)    == OS_INVALID_PARM);
-
-    /* Get the semaphore */
-    err = os_sem_pend(sem, 0);
-    TEST_ASSERT(err == 0,
-                "Did not get free semaphore immediately (err=%d)", err);
-
-    /* Check semaphore internals */
-    TEST_ASSERT(sem->sem_tokens == 0 && SLIST_EMPTY(&sem->sem_head),
-                "Semaphore internals wrong after getting semaphore\n"
-                "%s\n"
-                "Task: task=%p prio=%u", sem_test_sem_to_s(sem), t, t->t_prio);
-
-    /* Get the semaphore again; should fail */
-    err = os_sem_pend(sem, 0);
-    TEST_ASSERT(err == OS_TIMEOUT,
-                "Did not time out waiting for semaphore (err=%d)", err);
-
-    /* Check semaphore internals */
-    TEST_ASSERT(sem->sem_tokens == 0 && SLIST_EMPTY(&sem->sem_head),
-                "Semaphore internals wrong after getting semaphore\n"
-                "%s\n"
-                "Task: task=%p prio=%u\n", sem_test_sem_to_s(sem), t,
-                t->t_prio);
-
-    /* Release semaphore */
-    err = os_sem_release(sem);
-    TEST_ASSERT(err == 0,
-                "Could not release semaphore I own (err=%d)", err);
-
-    /* Check semaphore internals */
-    TEST_ASSERT(sem->sem_tokens == 1 && SLIST_EMPTY(&sem->sem_head),
-                "Semaphore internals wrong after releasing semaphore\n"
-                "%s\n"
-                "Task: task=%p prio=%u\n", sem_test_sem_to_s(sem), t,
-                t->t_prio);
-
-    /* Release it again */
-    err = os_sem_release(sem);
-    TEST_ASSERT(err == 0,
-                "Could not release semaphore again (err=%d)\n", err);
-
-    /* Check semaphore internals */
-    TEST_ASSERT(sem->sem_tokens == 2 && SLIST_EMPTY(&sem->sem_head),
-                "Semaphore internals wrong after releasing semaphore\n"
-                "%s\n"
-                "Task: task=%p prio=%u\n", sem_test_sem_to_s(sem), t,
-                t->t_prio);
-
-    os_test_restart();
-}
-
-static void 
-sem_test_1_task1_handler(void *arg)
-{
-    os_error_t err;
-    struct os_task *t;
-    int i;;
-
-    for (i = 0; i < 3; i++) {
-        t = os_sched_get_current_task();
-        TEST_ASSERT(t->t_func == sem_test_1_task1_handler);
-
-
-        err = os_sem_pend(&g_sem1, 0);
-        TEST_ASSERT(err == OS_OK);
-
-        /* Sleep to let other tasks run */
-        os_time_delay(100);
-
-        /* Release the semaphore */
-        err = os_sem_release(&g_sem1);
-        TEST_ASSERT(err == OS_OK);
-
-        /* Sleep to let other tasks run */
-        os_time_delay(100);
-    }
-
-    os_test_restart();
-}
-
-TEST_CASE(os_sem_test_basic)
-{
-    os_error_t err;
-
-    os_init();
-
-    err = os_sem_init(&g_sem1, 1);
-    TEST_ASSERT(err == OS_OK);
-
-    os_task_init(&task1, "task1", sem_test_basic_handler, NULL, TASK1_PRIO, 
-            OS_WAIT_FOREVER, stack1, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_start();
-}
-
-static void 
-sem_test_1_task2_handler(void *arg) 
-{
-    sem_test_pend_release_loop(0, 100, 100);
-}
-
-static void 
-sem_test_1_task3_handler(void *arg) 
-{
-    sem_test_pend_release_loop(0, OS_TIMEOUT_NEVER, 2000);
-}
-
-TEST_CASE(os_sem_test_case_1)
-{
-    os_error_t err;
-
-    os_init();
-
-    err = os_sem_init(&g_sem1, 1);
-    TEST_ASSERT(err == OS_OK);
-
-    os_task_init(&task1, "task1", sem_test_1_task1_handler, NULL,
-                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
-                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_task_init(&task2, "task2", sem_test_1_task2_handler, NULL,
-                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
-                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_task_init(&task3, "task3", sem_test_1_task3_handler, NULL, TASK3_PRIO, 
-                 OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_start();
-}
-
-static void 
-sem_test_2_task2_handler(void *arg) 
-{
-    sem_test_pend_release_loop(0, 2000, 2000);
-}
-
-static void 
-sem_test_2_task3_handler(void *arg) 
-{
-    sem_test_pend_release_loop(0, OS_TIMEOUT_NEVER, 2000);
-}
-
-static void 
-sem_test_2_task4_handler(void *arg) 
-{
-    sem_test_pend_release_loop(0, 2000, 2000);
-}
-
-TEST_CASE(os_sem_test_case_2)
-{
-    os_error_t err;
-
-    os_init();
-
-    err = os_sem_init(&g_sem1, 1);
-    TEST_ASSERT(err == OS_OK);
-
-    os_task_init(&task1, "task1", sem_test_sleep_task_handler, NULL,
-                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
-                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_task_init(&task2, "task2", sem_test_2_task2_handler, NULL,
-                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
-                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_task_init(&task3, "task3", sem_test_2_task3_handler, NULL, TASK3_PRIO,
-            OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_task_init(&task4, "task4", sem_test_2_task4_handler, NULL, TASK4_PRIO,
-            OS_WAIT_FOREVER, stack4, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_start();
-}
-
-static void 
-sem_test_3_task2_handler(void *arg) 
-{
-    sem_test_pend_release_loop(100, 2000, 2000);
-}
-
-static void 
-sem_test_3_task3_handler(void *arg) 
-{
-    sem_test_pend_release_loop(150, 2000, 2000);
-}
-
-static void 
-sem_test_3_task4_handler(void *arg) 
-{
-    sem_test_pend_release_loop(0, 2000, 2000);
-}
-
-TEST_CASE(os_sem_test_case_3)
-{
-    os_error_t err;
-
-    os_init();
-
-    err = os_sem_init(&g_sem1, 1);
-    TEST_ASSERT(err == OS_OK);
-
-    os_task_init(&task1, "task1", sem_test_sleep_task_handler, NULL,
-                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
-                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_task_init(&task2, "task2", sem_test_3_task2_handler, NULL,
-                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
-                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_task_init(&task3, "task3", sem_test_3_task3_handler, NULL, TASK3_PRIO,
-            OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_task_init(&task4, "task4", sem_test_3_task4_handler, NULL, TASK4_PRIO,
-            OS_WAIT_FOREVER, stack4, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_start();
-}
-
-static void 
-sem_test_4_task2_handler(void *arg) 
-{
-    sem_test_pend_release_loop(60, 2000, 2000);
-}
-
-static void 
-sem_test_4_task3_handler(void *arg) 
-{
-    sem_test_pend_release_loop(60, 2000, 2000);
-}
-
-static void 
-sem_test_4_task4_handler(void *arg) 
-{
-    sem_test_pend_release_loop(0, 2000, 2000);
-}
-
-
-TEST_CASE(os_sem_test_case_4)
-{
-    os_error_t err;
-
-    os_init();
-
-    err = os_sem_init(&g_sem1, 1);
-    TEST_ASSERT(err == OS_OK);
-
-    os_task_init(&task1, "task1", sem_test_sleep_task_handler, NULL,
-                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
-                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_task_init(&task2, "task2", sem_test_4_task2_handler, NULL,
-                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
-                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_task_init(&task3, "task3", sem_test_4_task3_handler, NULL, TASK3_PRIO,
-                 OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_task_init(&task4, "task4", sem_test_4_task4_handler, NULL, TASK4_PRIO,
-                 OS_WAIT_FOREVER, stack4, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
-
-    os_start();
-}
-
-TEST_SUITE(os_sem_test_suite)
-{
-    os_sem_test_basic();
-    os_sem_test_case_1();
-    os_sem_test_case_2();
-    os_sem_test_case_3();
-    os_sem_test_case_4();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/test/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/os/test/pkg.yml b/libs/os/test/pkg.yml
new file mode 100644
index 0000000..e517618
--- /dev/null
+++ b/libs/os/test/pkg.yml
@@ -0,0 +1,30 @@
+# 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.
+#
+pkg.name: libs/os/test
+pkg.type: unittest
+pkg.description: "OS unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps: 
+    - libs/os
+    - libs/testutil
+
+pkg.deps.SELFTEST:
+    - libs/console/stub

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/test/src/arch/cortex_m4/os_test_arch_arm.c
----------------------------------------------------------------------
diff --git a/libs/os/test/src/arch/cortex_m4/os_test_arch_arm.c b/libs/os/test/src/arch/cortex_m4/os_test_arch_arm.c
new file mode 100644
index 0000000..35134f7
--- /dev/null
+++ b/libs/os/test/src/arch/cortex_m4/os_test_arch_arm.c
@@ -0,0 +1,27 @@
+/**
+ * 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 "testutil/testutil.h"
+#include "os_test_priv.h"
+
+void
+os_test_restart(void)
+{
+    tu_restart();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/test/src/arch/sim/os_test_arch_sim.c
----------------------------------------------------------------------
diff --git a/libs/os/test/src/arch/sim/os_test_arch_sim.c b/libs/os/test/src/arch/sim/os_test_arch_sim.c
new file mode 100644
index 0000000..3b6cfbf
--- /dev/null
+++ b/libs/os/test/src/arch/sim/os_test_arch_sim.c
@@ -0,0 +1,52 @@
+/**
+ * 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 <stdio.h>
+#include <setjmp.h>
+#include <signal.h>
+#include <string.h>
+#include <sys/time.h>
+#include "testutil/testutil.h"
+#include "os/os.h"
+#include "os_test_priv.h"
+
+void
+os_test_restart(void)
+{
+    struct sigaction sa;
+    struct itimerval it;
+    int rc;
+
+    g_os_started = 0;
+
+    memset(&sa, 0, sizeof sa);
+    sa.sa_handler = SIG_IGN;
+
+    sigaction(SIGALRM, &sa, NULL);
+    sigaction(SIGVTALRM, &sa, NULL);
+
+    memset(&it, 0, sizeof(it));
+    rc = setitimer(ITIMER_VIRTUAL, &it, NULL);
+    if (rc != 0) {
+        perror("Cannot set itimer");
+        abort();
+    }
+
+    tu_restart();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/test/src/eventq_test.c
----------------------------------------------------------------------
diff --git a/libs/os/test/src/eventq_test.c b/libs/os/test/src/eventq_test.c
new file mode 100644
index 0000000..cb1ed94
--- /dev/null
+++ b/libs/os/test/src/eventq_test.c
@@ -0,0 +1,416 @@
+/**
+ * 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 <string.h>
+#include "testutil/testutil.h"
+#include "os/os.h"
+#include "os_test_priv.h"
+#include "os/os_eventq.h"
+
+#define MY_STACK_SIZE        (5120)
+#define POLL_STACK_SIZE        (4096)
+/* Task 1 sending task */
+/* Define task stack and task object */
+#define SEND_TASK_PRIO        (1)
+struct os_task eventq_task_s;
+os_stack_t eventq_task_stack_s[MY_STACK_SIZE];
+
+/* Task 2 receiving task */
+#define RECEIVE_TASK_PRIO     (2)
+struct os_task eventq_task_r;
+os_stack_t eventq_task_stack_r[MY_STACK_SIZE];
+
+struct os_eventq my_eventq;
+
+#define SIZE_MULTI_EVENT        (4)
+struct os_eventq multi_eventq[SIZE_MULTI_EVENT];
+
+/* This is to set the events we will use below */
+struct os_event g_event;
+struct os_event m_event[SIZE_MULTI_EVENT];
+
+/* Setting the event to send and receive multiple data */
+uint8_t my_event_type = 1;
+
+/* Setting up data for the poll */
+/* Define the task stack for the eventq_task_poll_send */
+#define SEND_TASK_POLL_PRIO        (3)
+struct os_task eventq_task_poll_s;
+os_stack_t eventq_task_stack_poll_s[POLL_STACK_SIZE];
+
+/* Define the task stack for the eventq_task_poll_receive */
+#define RECEIVE_TASK_POLL_PRIO     (4)
+struct os_task eventq_task_poll_r;
+os_stack_t eventq_task_stack_poll_r[POLL_STACK_SIZE ];
+
+/* Setting the data for the poll timeout */
+/* Define the task stack for the eventq_task_poll_timeout_send */
+#define SEND_TASK_POLL_TIMEOUT_PRIO        (5)
+struct os_task eventq_task_poll_timeout_s;
+os_stack_t eventq_task_stack_poll_timeout_s[POLL_STACK_SIZE];
+
+/* Define the task stack for the eventq_task_poll_receive */
+#define RECEIVE_TASK_POLL_TIMEOUT_PRIO     (6)
+struct os_task eventq_task_poll_timeout_r;
+os_stack_t eventq_task_stack_poll_timeout_r[POLL_STACK_SIZE];
+
+/* Setting the data for the poll single */
+/* Define the task stack for the eventq_task_poll_single_send */
+#define SEND_TASK_POLL_SINGLE_PRIO        (7)
+struct os_task eventq_task_poll_single_s;
+os_stack_t eventq_task_stack_poll_single_s[POLL_STACK_SIZE];
+
+/* Define the task stack for the eventq_task_poll_single_receive */
+#define RECEIVE_TASK_POLL_SINGLE_PRIO     (8)
+struct os_task eventq_task_poll_single_r;
+os_stack_t eventq_task_stack_poll_single_r[POLL_STACK_SIZE];
+
+/* This is the task function  to send data */
+void
+eventq_task_send(void *arg)
+{
+    int i;
+
+    g_event.ev_queued = 0;
+    g_event.ev_type = my_event_type;
+    g_event.ev_arg = NULL;
+
+    os_eventq_put(&my_eventq, &g_event);
+
+    os_time_delay(OS_TICKS_PER_SEC / 2);
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        m_event[i].ev_type = i + 2;
+        m_event[i].ev_arg = NULL;
+
+        /* Put and send */
+        os_eventq_put(&multi_eventq[i], &m_event[i]);
+        os_time_delay(OS_TICKS_PER_SEC / 2);
+    }
+
+    /* This task sleeps until the receive task completes the test. */
+    os_time_delay(1000000);
+}
+
+/* This is the task function is the receiving function */
+void
+eventq_task_receive(void *arg)
+{
+    struct os_event *event;
+    int i;
+
+    event = os_eventq_get(&my_eventq);
+    TEST_ASSERT(event->ev_type == my_event_type);
+
+    /* Receiving multi event from the send task */
+    for (i = 0; i < SIZE_MULTI_EVENT; i++) {
+        event = os_eventq_get(&multi_eventq[i]);
+        TEST_ASSERT(event->ev_type == i + 2);
+    }
+
+    /* Finishes the test when OS has been started */
+    os_test_restart();
+}
+
+void
+eventq_task_poll_send(void *arg)
+{
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
+    int i;
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        eventqs[i] = &multi_eventq[i];
+    }
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        m_event[i].ev_type = i + 10;
+        m_event[i].ev_arg = NULL;
+
+        /* Put and send */
+        os_eventq_put(eventqs[i], &m_event[i]);
+        os_time_delay(OS_TICKS_PER_SEC / 2);
+    }
+
+    /* This task sleeps until the receive task completes the test. */
+    os_time_delay(1000000);
+}
+
+void
+eventq_task_poll_receive(void *arg)
+{
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
+    struct os_event *event;
+    int i;
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        eventqs[i] = &multi_eventq[i];
+    }
+
+    /* Recieving using the os_eventq_poll*/
+    for (i = 0; i < SIZE_MULTI_EVENT; i++) {
+        event = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, OS_WAIT_FOREVER);
+        TEST_ASSERT(event->ev_type == i +10);
+    }
+
+    /* Finishes the test when OS has been started */
+    os_test_restart();
+
+}
+
+/* Sending with a time failure */
+void
+eventq_task_poll_timeout_send(void *arg)
+{
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
+    int i;
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        eventqs[i] = &multi_eventq[i];
+    }
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+         os_time_delay(1000);
+
+        /* Put and send */
+        os_eventq_put(eventqs[i], &m_event[i]);
+        os_time_delay(OS_TICKS_PER_SEC / 2);
+    }
+
+    /* This task sleeps until the receive task completes the test. */
+    os_time_delay(1000000);
+    
+}
+
+/* Receiving multiple event queues with a time failure */
+void
+eventq_task_poll_timeout_receive(void *arg)
+{
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
+    struct os_event *event;
+    int i;
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        eventqs[i] = &multi_eventq[i];
+    }
+
+    /* Recieving using the os_eventq_poll_timeout*/
+    for (i = 0; i < SIZE_MULTI_EVENT; i++) {
+        event = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, 200);
+        TEST_ASSERT(event == NULL);
+    }
+
+    /* Finishes the test when OS has been started */
+    os_test_restart();
+
+}
+
+/* Sending a single event to poll */
+void
+eventq_task_poll_single_send(void *arg)
+{
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
+    int i;
+    int position = 2;
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        eventqs[i] = &multi_eventq[i];
+    }
+
+    /* Put and send */
+    os_eventq_put(eventqs[position], &m_event[position]);
+    os_time_delay(OS_TICKS_PER_SEC / 2);
+
+    /* This task sleeps until the receive task completes the test. */
+    os_time_delay(1000000);
+}
+
+/* Recieving the single event */
+void
+eventq_task_poll_single_receive(void *arg)
+{
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
+    struct os_event *event;
+    int i;
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        eventqs[i] = &multi_eventq[i];
+    }
+
+    /* Recieving using the os_eventq_poll*/
+    event = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, OS_WAIT_FOREVER);
+    TEST_ASSERT(event->ev_type == 20);
+
+    /* Finishes the test when OS has been started */
+    os_test_restart();
+}
+
+TEST_CASE(event_test_sr)
+{
+    int i;
+
+    /* Initializing the OS */
+    os_init();
+    /* Initialize the task */
+    os_task_init(&eventq_task_s, "eventq_task_s", eventq_task_send, NULL,
+        SEND_TASK_PRIO, OS_WAIT_FOREVER, eventq_task_stack_s, MY_STACK_SIZE);
+
+    /* Receive events and check whether the eevnts are correctly received */
+    os_task_init(&eventq_task_r, "eventq_task_r", eventq_task_receive, NULL,
+        RECEIVE_TASK_PRIO, OS_WAIT_FOREVER, eventq_task_stack_r,
+        MY_STACK_SIZE);
+
+    os_eventq_init(&my_eventq);
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        os_eventq_init(&multi_eventq[i]);
+    }
+
+    /* Does not return until OS_restart is called */
+    os_start();
+
+}
+
+/* To test for the basic function of os_eventq_poll() */
+TEST_CASE(event_test_poll_sr)
+{
+    int i;
+
+    /* Initializing the OS */
+    os_init();
+    /* Initialize the task */
+    os_task_init(&eventq_task_poll_s, "eventq_task_poll_s", eventq_task_poll_send,
+        NULL, SEND_TASK_POLL_PRIO, OS_WAIT_FOREVER, eventq_task_stack_poll_s, 
+        POLL_STACK_SIZE);
+
+    /* Receive events and check whether the eevnts are correctly received */
+    os_task_init(&eventq_task_poll_r, "eventq_task_r", eventq_task_poll_receive,
+        NULL, RECEIVE_TASK_POLL_PRIO, OS_WAIT_FOREVER, eventq_task_stack_poll_r,
+        POLL_STACK_SIZE);
+
+    /* Initializing the eventqs. */
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        os_eventq_init(&multi_eventq[i]);
+    }
+
+    /* Does not return until OS_restart is called */
+    os_start();
+
+}
+
+/* Test case for poll timeout */
+TEST_CASE(event_test_poll_timeout_sr)
+{
+    int i;
+
+    /* Initializing the OS */
+    os_init();
+    /* Initialize the task */
+    os_task_init(&eventq_task_poll_timeout_s, "eventq_task_poll_timeout_s", 
+        eventq_task_poll_timeout_send, NULL, SEND_TASK_POLL_TIMEOUT_PRIO,
+        OS_WAIT_FOREVER, eventq_task_stack_poll_timeout_s, POLL_STACK_SIZE);
+
+    /* Receive events and check whether the eevnts are correctly received */
+    os_task_init(&eventq_task_poll_timeout_r, "eventq_task_timeout_r",
+        eventq_task_poll_timeout_receive, NULL, RECEIVE_TASK_POLL_TIMEOUT_PRIO,
+        OS_WAIT_FOREVER, eventq_task_stack_poll_timeout_r, POLL_STACK_SIZE);
+
+    /* Initializing the eventqs. */
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        os_eventq_init(&multi_eventq[i]);
+
+        m_event[i].ev_type = i + 10;
+        m_event[i].ev_arg = NULL;
+    }
+
+    /* Does not return until OS_restart is called */
+    os_start();
+
+}
+
+/* The case for poll single */
+/* Test case for poll timeout */
+TEST_CASE(event_test_poll_single_sr)
+{
+    int i;
+
+    /* Initializing the OS */
+    os_init();
+    /* Initialize the task */
+    os_task_init(&eventq_task_poll_single_s, "eventq_task_poll_single_s", 
+        eventq_task_poll_single_send, NULL, SEND_TASK_POLL_SINGLE_PRIO,
+        OS_WAIT_FOREVER, eventq_task_stack_poll_single_s, POLL_STACK_SIZE);
+
+    /* Receive events and check whether the eevnts are correctly received */
+    os_task_init(&eventq_task_poll_single_r, "eventq_task_single_r",
+        eventq_task_poll_single_receive, NULL, RECEIVE_TASK_POLL_SINGLE_PRIO,
+        OS_WAIT_FOREVER, eventq_task_stack_poll_single_r, POLL_STACK_SIZE);
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        os_eventq_init(&multi_eventq[i]);
+
+        m_event[i].ev_type = 10 * i;
+        m_event[i].ev_arg = NULL;
+    }
+
+    /* Does not return until OS_restart is called */
+    os_start();
+
+}
+
+/**
+ * Tests eventq_poll() with a timeout of 0.  This should not involve the
+ * scheduler at all, so it should work without starting the OS.
+ */
+TEST_CASE(event_test_poll_0timo)
+{
+    struct os_eventq *eventqs[SIZE_MULTI_EVENT];
+    struct os_event *evp;
+    struct os_event ev;
+    int i;
+
+    for (i = 0; i < SIZE_MULTI_EVENT; i++){
+        os_eventq_init(&multi_eventq[i]);
+        eventqs[i] = &multi_eventq[i];
+    }
+
+    evp = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, 0);
+    TEST_ASSERT(evp == NULL);
+
+    /* Ensure no eventq thinks a task is waiting on it. */
+    for (i = 0; i < SIZE_MULTI_EVENT; i++) {
+        TEST_ASSERT(eventqs[i]->evq_task == NULL);
+    }
+
+    /* Put an event on one of the queues. */
+    memset(&ev, 0, sizeof ev);
+    ev.ev_type = 1;
+    os_eventq_put(eventqs[3], &ev);
+
+    evp = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, 0);
+    TEST_ASSERT(evp == &ev);
+}
+
+TEST_SUITE(os_eventq_test_suite)
+{
+    event_test_sr();
+    event_test_poll_sr();
+    event_test_poll_timeout_sr();
+    event_test_poll_single_sr();
+    event_test_poll_0timo();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/test/src/mbuf_test.c
----------------------------------------------------------------------
diff --git a/libs/os/test/src/mbuf_test.c b/libs/os/test/src/mbuf_test.c
new file mode 100644
index 0000000..dd4121d
--- /dev/null
+++ b/libs/os/test/src/mbuf_test.c
@@ -0,0 +1,420 @@
+/**
+ * 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 "testutil/testutil.h"
+#include "os/os.h"
+#include "os_test_priv.h"
+
+#include <string.h>
+
+/* 
+ * NOTE: currently, the buffer size cannot be changed as some tests are
+ * hard-coded for this size.
+ */
+#define MBUF_TEST_POOL_BUF_SIZE     (256)
+#define MBUF_TEST_POOL_BUF_COUNT    (10)
+
+#define MBUF_TEST_DATA_LEN          (1024)
+
+static os_membuf_t os_mbuf_membuf[OS_MEMPOOL_SIZE(MBUF_TEST_POOL_BUF_SIZE,
+        MBUF_TEST_POOL_BUF_COUNT)];
+
+static struct os_mbuf_pool os_mbuf_pool;
+static struct os_mempool os_mbuf_mempool;
+static uint8_t os_mbuf_test_data[MBUF_TEST_DATA_LEN];
+
+static void
+os_mbuf_test_setup(void)
+{
+    int rc;
+    int i;
+
+    rc = os_mempool_init(&os_mbuf_mempool, MBUF_TEST_POOL_BUF_COUNT,
+            MBUF_TEST_POOL_BUF_SIZE, &os_mbuf_membuf[0], "mbuf_pool");
+    TEST_ASSERT_FATAL(rc == 0, "Error creating memory pool %d", rc);
+
+    rc = os_mbuf_pool_init(&os_mbuf_pool, &os_mbuf_mempool,
+            MBUF_TEST_POOL_BUF_SIZE, MBUF_TEST_POOL_BUF_COUNT);
+    TEST_ASSERT_FATAL(rc == 0, "Error creating mbuf pool %d", rc);
+
+    for (i = 0; i < sizeof os_mbuf_test_data; i++) {
+        os_mbuf_test_data[i] = i;
+    }
+}
+
+static void
+os_mbuf_test_misc_assert_sane(struct os_mbuf *om, void *data,
+                              int buflen, int pktlen, int pkthdr_len)
+{
+    uint8_t *data_min;
+    uint8_t *data_max;
+    int totlen;
+    int i;
+
+    TEST_ASSERT_FATAL(om != NULL);
+
+    if (OS_MBUF_IS_PKTHDR(om)) {
+        TEST_ASSERT(OS_MBUF_PKTLEN(om) == pktlen);
+    }
+
+    totlen = 0;
+    for (i = 0; om != NULL; i++) {
+        if (i == 0) {
+            TEST_ASSERT(om->om_len == buflen);
+            TEST_ASSERT(om->om_pkthdr_len == pkthdr_len);
+        }
+
+        data_min = om->om_databuf + om->om_pkthdr_len;
+        data_max = om->om_databuf + om->om_omp->omp_databuf_len - om->om_len;
+        TEST_ASSERT(om->om_data >= data_min && om->om_data <= data_max);
+
+        if (data != NULL) {
+            TEST_ASSERT(memcmp(om->om_data, data + totlen, om->om_len) == 0);
+        }
+
+        totlen += om->om_len;
+        om = SLIST_NEXT(om, om_next);
+    }
+
+    TEST_ASSERT(totlen == pktlen);
+}
+
+
+TEST_CASE(os_mbuf_test_alloc)
+{
+    struct os_mbuf *m;
+    int rc;
+
+    os_mbuf_test_setup();
+
+    m = os_mbuf_get(&os_mbuf_pool, 0);
+    TEST_ASSERT_FATAL(m != NULL, "Error allocating mbuf");
+
+    rc = os_mbuf_free(m);
+    TEST_ASSERT_FATAL(rc == 0, "Error free'ing mbuf %d", rc);
+}
+
+TEST_CASE(os_mbuf_test_get_pkthdr)
+{
+    struct os_mbuf *m;
+ 
+    os_mbuf_test_setup();
+
+#if (MBUF_TEST_POOL_BUF_SIZE <= 256)
+    m = os_mbuf_get_pkthdr(&os_mbuf_pool, MBUF_TEST_POOL_BUF_SIZE - 1);
+    TEST_ASSERT_FATAL(m == NULL, "Error: should not have returned mbuf");
+#endif
+
+    m = os_mbuf_get(&os_mbuf_pool, MBUF_TEST_POOL_BUF_SIZE);
+    TEST_ASSERT_FATAL(m == NULL, "Error: should not have returned mbuf");
+}
+
+
+TEST_CASE(os_mbuf_test_dup)
+{
+    struct os_mbuf *om;
+    struct os_mbuf *om2;
+    struct os_mbuf *dup;
+    int rc;
+
+    os_mbuf_test_setup();
+
+    /* Test first allocating and duplicating a single mbuf */
+    om = os_mbuf_get(&os_mbuf_pool, 0);
+    TEST_ASSERT_FATAL(om != NULL, "Error allocating mbuf");
+
+    rc = os_mbuf_append(om, os_mbuf_test_data, 200);
+    TEST_ASSERT_FATAL(rc == 0);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 200, 200, 0);
+
+    dup = os_mbuf_dup(om);
+    TEST_ASSERT_FATAL(dup != NULL, "NULL mbuf returned from dup");
+    TEST_ASSERT_FATAL(dup != om, "duplicate matches original.");
+    os_mbuf_test_misc_assert_sane(dup, os_mbuf_test_data, 200, 200, 0);
+
+    rc = os_mbuf_free(om);
+    TEST_ASSERT_FATAL(rc == 0, "Error free'ing mbuf om %d", rc);
+
+    rc = os_mbuf_free(dup);
+    TEST_ASSERT_FATAL(rc == 0, "Error free'ing mbuf dup %d", rc);
+
+    om = os_mbuf_get(&os_mbuf_pool, 0);
+    TEST_ASSERT_FATAL(om != NULL, "Error allocating mbuf");
+    rc = os_mbuf_append(om, os_mbuf_test_data, 200);
+    TEST_ASSERT_FATAL(rc == 0);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 200, 200, 0);
+
+    om2 = os_mbuf_get(&os_mbuf_pool, 0);
+    TEST_ASSERT_FATAL(om2 != NULL, "Error allocating mbuf");
+    rc = os_mbuf_append(om2, os_mbuf_test_data + 200, 200);
+    TEST_ASSERT_FATAL(rc == 0);
+    os_mbuf_test_misc_assert_sane(om2, os_mbuf_test_data + 200, 200, 200, 0);
+
+    os_mbuf_concat(om, om2);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 200, 400, 0);
+
+    dup = os_mbuf_dup(om);
+    TEST_ASSERT_FATAL(dup != NULL, "NULL mbuf returned from dup");
+    TEST_ASSERT_FATAL(dup != om, "Duplicate matches original");
+    TEST_ASSERT_FATAL(SLIST_NEXT(dup, om_next) != NULL,
+            "NULL chained element, duplicate should match original");
+
+    os_mbuf_test_misc_assert_sane(dup, os_mbuf_test_data, 200, 400, 0);
+
+    rc = os_mbuf_free_chain(om);
+    TEST_ASSERT_FATAL(rc == 0, "Cannot free mbuf chain %d", rc);
+
+    rc = os_mbuf_free_chain(dup);
+    TEST_ASSERT_FATAL(rc == 0, "Cannot free mbuf chain %d", rc);
+}
+
+TEST_CASE(os_mbuf_test_append)
+{
+    struct os_mbuf *om;
+    int rc;
+    uint8_t databuf[] = {0xa, 0xb, 0xc, 0xd};
+    uint8_t cmpbuf[] = {0xff, 0xff, 0xff, 0xff};
+
+    os_mbuf_test_setup();
+
+    om = os_mbuf_get(&os_mbuf_pool, 0);
+    TEST_ASSERT_FATAL(om != NULL, "Error allocating mbuf");
+    os_mbuf_test_misc_assert_sane(om, NULL, 0, 0, 0);
+
+    rc = os_mbuf_append(om, databuf, sizeof(databuf));
+    TEST_ASSERT_FATAL(rc == 0, "Cannot add %d bytes to mbuf",
+            sizeof(databuf));
+    os_mbuf_test_misc_assert_sane(om, databuf, sizeof databuf, sizeof databuf,
+                                  0);
+
+    memcpy(cmpbuf, OS_MBUF_DATA(om, uint8_t *), om->om_len);
+    TEST_ASSERT_FATAL(memcmp(cmpbuf, databuf, sizeof(databuf)) == 0,
+            "Databuf doesn't match cmpbuf");
+}
+
+TEST_CASE(os_mbuf_test_extend)
+{
+    struct os_mbuf *om;
+    void *v;
+
+    os_mbuf_test_setup();
+
+    /*** Series of successful extensions. */
+    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
+    TEST_ASSERT_FATAL(om != NULL);
+
+    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 222);
+    TEST_ASSERT(SLIST_NEXT(om, om_next) == NULL);
+    os_mbuf_test_misc_assert_sane(om, NULL, 0, 0, 18);
+
+    v = os_mbuf_extend(om, 20);
+    TEST_ASSERT(v != NULL);
+    TEST_ASSERT(v == om->om_data);
+    TEST_ASSERT(om->om_len == 20);
+
+    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 202);
+    TEST_ASSERT(SLIST_NEXT(om, om_next) == NULL);
+    os_mbuf_test_misc_assert_sane(om, NULL, 20, 20, 18);
+
+    v = os_mbuf_extend(om, 100);
+    TEST_ASSERT(v != NULL);
+    TEST_ASSERT(v == om->om_data + 20);
+    TEST_ASSERT(om->om_len == 120);
+
+    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 102);
+    TEST_ASSERT(SLIST_NEXT(om, om_next) == NULL);
+    os_mbuf_test_misc_assert_sane(om, NULL, 120, 120, 18);
+
+    v = os_mbuf_extend(om, 101);
+    TEST_ASSERT(v != NULL);
+    TEST_ASSERT(v == om->om_data + 120);
+    TEST_ASSERT(om->om_len == 221);
+
+    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 1);
+    TEST_ASSERT(SLIST_NEXT(om, om_next) == NULL);
+    os_mbuf_test_misc_assert_sane(om, NULL, 221, 221, 18);
+
+    v = os_mbuf_extend(om, 1);
+    TEST_ASSERT(v != NULL);
+    TEST_ASSERT(v == om->om_data + 221);
+    TEST_ASSERT(om->om_len == 222);
+
+    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 0);
+    TEST_ASSERT(SLIST_NEXT(om, om_next) == NULL);
+    os_mbuf_test_misc_assert_sane(om, NULL, 222, 222, 18);
+
+    /* Overflow into next buffer. */
+    v = os_mbuf_extend(om, 1);
+    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 0);
+    TEST_ASSERT(SLIST_NEXT(om, om_next) != NULL);
+
+    TEST_ASSERT(v == SLIST_NEXT(om, om_next)->om_data);
+    TEST_ASSERT(om->om_len == 222);
+    TEST_ASSERT(SLIST_NEXT(om, om_next)->om_len == 1);
+    os_mbuf_test_misc_assert_sane(om, NULL, 222, 223, 18);
+
+    /*** Attempt to extend by an amount larger than max buf size fails. */
+    v = os_mbuf_extend(om, 257);
+    TEST_ASSERT(v == NULL);
+    TEST_ASSERT(OS_MBUF_TRAILINGSPACE(om) == 0);
+    TEST_ASSERT(SLIST_NEXT(om, om_next) != NULL);
+
+    TEST_ASSERT(om->om_len == 222);
+    TEST_ASSERT(SLIST_NEXT(om, om_next)->om_len == 1);
+    os_mbuf_test_misc_assert_sane(om, NULL, 222, 223, 18);
+}
+
+TEST_CASE(os_mbuf_test_pullup)
+{
+    struct os_mbuf *om;
+    struct os_mbuf *om2;
+    int rc;
+
+    os_mbuf_test_setup();
+
+    /*** Free when too much os_mbuf_test_data is requested. */
+    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
+    TEST_ASSERT_FATAL(om != NULL);
+
+    om = os_mbuf_pullup(om, 1);
+    TEST_ASSERT(om == NULL);
+
+    /*** No effect when all os_mbuf_test_data is already at the start. */
+    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
+    TEST_ASSERT_FATAL(om != NULL);
+
+    rc = os_mbuf_append(om, os_mbuf_test_data, 1);
+    TEST_ASSERT_FATAL(rc == 0);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 1, 1, 18);
+
+    om = os_mbuf_pullup(om, 1);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 1, 1, 18);
+
+    /*** Spread os_mbuf_test_data across four mbufs. */
+    om2 = os_mbuf_get(&os_mbuf_pool, 10);
+    TEST_ASSERT_FATAL(om2 != NULL);
+    rc = os_mbuf_append(om2, os_mbuf_test_data + 1, 1);
+    TEST_ASSERT_FATAL(rc == 0);
+    os_mbuf_concat(om, om2);
+
+    om2 = os_mbuf_get(&os_mbuf_pool, 10);
+    TEST_ASSERT_FATAL(om2 != NULL);
+    rc = os_mbuf_append(om2, os_mbuf_test_data + 2, 1);
+    TEST_ASSERT_FATAL(rc == 0);
+    os_mbuf_concat(om, om2);
+
+    om2 = os_mbuf_get(&os_mbuf_pool, 10);
+    TEST_ASSERT_FATAL(om2 != NULL);
+    rc = os_mbuf_append(om2, os_mbuf_test_data + 3, 1);
+    TEST_ASSERT_FATAL(rc == 0);
+    os_mbuf_concat(om, om2);
+
+    TEST_ASSERT_FATAL(OS_MBUF_PKTLEN(om) == 4);
+
+    om = os_mbuf_pullup(om, 4);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 4, 4, 18);
+
+    os_mbuf_free_chain(om);
+
+    /*** Require an allocation. */
+    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
+    TEST_ASSERT_FATAL(om != NULL);
+
+    om->om_data += 100;
+    rc = os_mbuf_append(om, os_mbuf_test_data, 100);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    om2 = os_mbuf_get(&os_mbuf_pool, 10);
+    TEST_ASSERT_FATAL(om2 != NULL);
+    rc = os_mbuf_append(om2, os_mbuf_test_data + 100, 100);
+    TEST_ASSERT_FATAL(rc == 0);
+    os_mbuf_concat(om, om2);
+
+    om = os_mbuf_pullup(om, 200);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 200, 200, 18);
+
+    /*** Partial pullup. */
+    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
+    TEST_ASSERT_FATAL(om != NULL);
+
+    om->om_data += 100;
+    rc = os_mbuf_append(om, os_mbuf_test_data, 100);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    om2 = os_mbuf_get(&os_mbuf_pool, 10);
+    TEST_ASSERT_FATAL(om2 != NULL);
+    rc = os_mbuf_append(om2, os_mbuf_test_data + 100, 100);
+    TEST_ASSERT_FATAL(rc == 0);
+    os_mbuf_concat(om, om2);
+
+    om = os_mbuf_pullup(om, 150);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 150, 200, 18);
+}
+
+TEST_CASE(os_mbuf_test_adj)
+{
+    struct os_mbuf *om;
+    int rc;
+
+    os_mbuf_test_setup();
+
+    om = os_mbuf_get_pkthdr(&os_mbuf_pool, 10);
+    TEST_ASSERT_FATAL(om != NULL);
+
+    rc = os_mbuf_append(om, os_mbuf_test_data, sizeof os_mbuf_test_data);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data, 222,
+                                  sizeof os_mbuf_test_data, 18);
+
+    /* Remove from the front. */
+    os_mbuf_adj(om, 10);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data + 10, 212,
+                                  sizeof os_mbuf_test_data - 10, 18);
+
+    /* Remove from the back. */
+    os_mbuf_adj(om, -10);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data + 10, 212,
+                                  sizeof os_mbuf_test_data - 20, 18);
+
+    /* Remove entire first buffer. */
+    os_mbuf_adj(om, 212);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data + 222, 0,
+                                  sizeof os_mbuf_test_data - 232, 18);
+
+    /* Remove next buffer. */
+    os_mbuf_adj(om, 256);
+    os_mbuf_test_misc_assert_sane(om, os_mbuf_test_data + 478, 0,
+                                  sizeof os_mbuf_test_data - 488, 18);
+
+    /* Remove more data than is present. */
+    os_mbuf_adj(om, 1000);
+    os_mbuf_test_misc_assert_sane(om, NULL, 0, 0, 18);
+}
+
+TEST_SUITE(os_mbuf_test_suite)
+{
+    os_mbuf_test_alloc();
+    os_mbuf_test_dup();
+    os_mbuf_test_append();
+    os_mbuf_test_pullup();
+    os_mbuf_test_extend();
+    os_mbuf_test_adj();
+    os_mbuf_test_get_pkthdr();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/test/src/mempool_test.c
----------------------------------------------------------------------
diff --git a/libs/os/test/src/mempool_test.c b/libs/os/test/src/mempool_test.c
new file mode 100644
index 0000000..cd17c90
--- /dev/null
+++ b/libs/os/test/src/mempool_test.c
@@ -0,0 +1,227 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+#include "testutil/testutil.h"
+#include "os/os.h"
+#include "os_test_priv.h"
+
+/* Create a memory pool for testing */
+#define NUM_MEM_BLOCKS  (10)
+#define MEM_BLOCK_SIZE  (80)
+
+/* Limit max blocks for testing */
+#define MEMPOOL_TEST_MAX_BLOCKS     (128)
+
+#if OS_CFG_ALIGNMENT == OS_CFG_ALIGN_4
+int alignment = 4;
+#else
+int alignment = 8;
+#endif
+
+/* Test memory pool structure */
+struct os_mempool g_TstMempool;
+
+/* Test memory pool buffer */
+os_membuf_t TstMembuf[OS_MEMPOOL_SIZE(NUM_MEM_BLOCKS, MEM_BLOCK_SIZE)];
+
+/* Array of block pointers. */
+void *block_array[MEMPOOL_TEST_MAX_BLOCKS];
+
+int verbose = 0;
+
+static int
+mempool_test_get_pool_size(int num_blocks, int block_size)
+{
+    int mem_pool_size;
+
+#if OS_CFG_ALIGNMENT == OS_CFG_ALIGN_4
+    mem_pool_size = (num_blocks * ((block_size + 3)/4) * sizeof(os_membuf_t));
+#else
+    mem_pool_size = (num_blocks * ((block_size + 7)/8) * sizeof(os_membuf_t));
+#endif
+
+    return mem_pool_size;
+}
+
+static void
+mempool_test(int num_blocks, int block_size)
+{
+    int cnt;
+    int true_block_size;
+    int mem_pool_size;
+    uint32_t test_block;
+    uint8_t *tstptr;
+    void **free_ptr;
+    void *block;
+    os_error_t rc;
+
+    /* Check for too many blocks */
+    TEST_ASSERT(num_blocks <= MEMPOOL_TEST_MAX_BLOCKS);
+
+    rc = os_mempool_init(&g_TstMempool, num_blocks, MEM_BLOCK_SIZE, 
+                         &TstMembuf[0], "TestMemPool");
+    TEST_ASSERT_FATAL(rc == 0, "Error creating memory pool %d", rc);
+
+    TEST_ASSERT(g_TstMempool.mp_num_free == num_blocks,
+                "Number of free blocks not equal to total blocks!");
+
+    TEST_ASSERT(SLIST_FIRST(&g_TstMempool) == (void *)&TstMembuf[0],
+                "Free list pointer does not point to first block!");
+
+    mem_pool_size = mempool_test_get_pool_size(num_blocks, block_size);
+    TEST_ASSERT(mem_pool_size == sizeof(TstMembuf),
+                "Total memory pool size not correct! (%d vs %lu)",
+                mem_pool_size, (unsigned long)sizeof(TstMembuf));
+
+    /* Get the real block size */
+#if (OS_CFG_ALIGNMENT == OS_CFG_ALIGN_4)
+    true_block_size = (g_TstMempool.mp_block_size + 3) & ~3;
+#else
+    true_block_size = (g_TstMempool.mp_block_size + 7) & ~7;
+#endif
+
+    /* Traverse free list. Better add up to number of blocks! */
+    cnt = 0;
+    free_ptr = (void **)&TstMembuf;
+    tstptr = (uint8_t *)&TstMembuf;
+    while (1) {
+        /* Increment # of elements by 1 */
+        ++cnt;
+
+        /* If the free list is NULL, leave */
+        if (*free_ptr == NULL) {
+            break;
+        }
+
+        TEST_ASSERT(((uint8_t *)*free_ptr - (uint8_t *)free_ptr) ==
+                        true_block_size,
+                    "Free pointers are more than one block apart!");
+
+        /* Move to next memory block */
+        tstptr += true_block_size;
+
+        TEST_ASSERT(*free_ptr == (void *)tstptr,
+                    "Error: free_ptr=%p testptr=%p\n", *free_ptr, tstptr);
+
+        free_ptr = *free_ptr;
+    }
+
+    /* Last one in list better be NULL */
+    TEST_ASSERT(cnt == g_TstMempool.mp_num_blocks,
+                "Free list contains too many elements (%u)", cnt);
+
+    /* Get a block */
+    block = os_memblock_get(&g_TstMempool);
+    TEST_ASSERT(block != NULL,
+                "Error: get block fails when pool should have elements");
+
+    TEST_ASSERT(g_TstMempool.mp_num_free == (num_blocks-1),
+                "Number of free blocks incorrect (%u vs %u)",
+                g_TstMempool.mp_num_free, (num_blocks-1));
+
+    /* Put back the block */
+    rc = os_memblock_put(&g_TstMempool, block);
+    TEST_ASSERT(rc == 0, "Put block fails with error code=%d\n", rc);
+
+    TEST_ASSERT(g_TstMempool.mp_num_free == num_blocks,
+                "Number of free blocks incorrect (%u vs %u)",
+                g_TstMempool.mp_num_free, num_blocks);
+
+    /* remove all the blocks. Make sure we get count. */
+    memset(block_array, 0, sizeof(block_array));
+    cnt = 0;
+    while (1) {
+        block = os_memblock_get(&g_TstMempool);
+        if (block == NULL) {
+            break;
+        }
+        block_array[cnt] = block;
+        ++cnt;
+        if (cnt == MEMPOOL_TEST_MAX_BLOCKS) {
+            break;
+        }
+    }
+
+    TEST_ASSERT((cnt == g_TstMempool.mp_num_blocks) && 
+                (cnt != MEMPOOL_TEST_MAX_BLOCKS),
+                "Got more blocks than mempool contains (%d vs %d)",
+                cnt, g_TstMempool.mp_num_blocks);
+
+    /* Better be no free blocks left! */
+    TEST_ASSERT(g_TstMempool.mp_num_free == 0,
+                "Got all blocks but number free not zero! (%d)",
+                g_TstMempool.mp_num_free);
+
+    /* Now put them all back */
+    for (cnt = 0; cnt < g_TstMempool.mp_num_blocks; ++cnt) {
+        rc = os_memblock_put(&g_TstMempool, block_array[cnt]);
+        TEST_ASSERT(rc == 0,
+                    "Error putting back block %p (cnt=%d err=%d)", 
+                    block_array[cnt], cnt, rc);
+    }
+
+    /* Better be no free blocks left! */
+    TEST_ASSERT(g_TstMempool.mp_num_free == g_TstMempool.mp_num_blocks,
+                "Put all blocks but number free not equal to total!");
+
+    /* Better get error when we try these things! */
+    rc = os_memblock_put(NULL, block_array[0]);
+    TEST_ASSERT(rc != 0,
+                "Should have got an error trying to put to null pool");
+
+    rc = os_memblock_put(&g_TstMempool, NULL);
+    TEST_ASSERT(rc != 0, "No error trying to put to NULL block");
+
+    TEST_ASSERT(os_memblock_get(NULL) == NULL,
+                "No error trying to get a block from NULL pool");
+
+    /* Attempt to free a block outside the range of the membuf */
+    test_block = g_TstMempool.mp_membuf_addr;
+    test_block -= 4;
+    rc = os_memblock_put(&g_TstMempool, (void *)test_block);
+    TEST_ASSERT(rc == OS_INVALID_PARM, "No error freeing bad block address");
+
+    test_block += (true_block_size * g_TstMempool.mp_num_blocks) + 100;
+    rc = os_memblock_put(&g_TstMempool, (void *)test_block);
+    TEST_ASSERT(rc == OS_INVALID_PARM, "No error freeing bad block address");
+
+    /* Attempt to free on bad boundary */
+    test_block = g_TstMempool.mp_membuf_addr;
+    test_block += (true_block_size / 2);
+    rc = os_memblock_put(&g_TstMempool, (void *)test_block);
+    TEST_ASSERT(rc == OS_INVALID_PARM, "No error freeing bad block address");
+}
+
+/**
+ * os mempool test 
+ *  
+ * Main test loop for memory pool testing. 
+ * 
+ * @return int 
+ */
+TEST_CASE(os_mempool_test_case)
+{
+    mempool_test(NUM_MEM_BLOCKS, MEM_BLOCK_SIZE);
+}
+
+TEST_SUITE(os_mempool_test_suite)
+{
+    os_mempool_test_case();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/test/src/mutex_test.c
----------------------------------------------------------------------
diff --git a/libs/os/test/src/mutex_test.c b/libs/os/test/src/mutex_test.c
new file mode 100644
index 0000000..ef6a08d
--- /dev/null
+++ b/libs/os/test/src/mutex_test.c
@@ -0,0 +1,407 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <sys/time.h>
+#include "testutil/testutil.h"
+#include "os/os.h"
+#include "os/os_test.h"
+#include "os/os_cfg.h"
+#include "os/os_mutex.h"
+#include "os_test_priv.h"
+
+#ifdef ARCH_sim
+#define MUTEX_TEST_STACK_SIZE   1024
+#else
+#define MUTEX_TEST_STACK_SIZE   256
+#endif
+
+struct os_task task14;
+os_stack_t stack14[OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE)];
+
+struct os_task task15;
+os_stack_t stack15[OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE)];
+
+struct os_task task16;
+os_stack_t stack16[OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE)];
+
+struct os_task task17;
+os_stack_t stack17[OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE)];
+
+#define TASK14_PRIO (4)
+#define TASK15_PRIO (5)
+#define TASK16_PRIO (6)
+#define TASK17_PRIO (7)
+
+volatile int g_task14_val;
+volatile int g_task15_val;
+volatile int g_task16_val;
+volatile int g_task17_val;
+struct os_mutex g_mutex1;
+struct os_mutex g_mutex2;
+
+static volatile int g_mutex_test;
+
+/**
+ * mutex test basic 
+ *  
+ * Basic mutex tests
+ * 
+ * @return int 
+ */
+static void
+mutex_test_basic_handler(void *arg)
+{
+    struct os_mutex *mu;
+    struct os_task *t;
+    os_error_t err;
+
+    mu = &g_mutex1;
+    t = os_sched_get_current_task();
+
+    /* Test some error cases */
+    TEST_ASSERT(os_mutex_init(NULL)     == OS_INVALID_PARM);
+    TEST_ASSERT(os_mutex_release(NULL)  == OS_INVALID_PARM);
+    TEST_ASSERT(os_mutex_pend(NULL, 0)  == OS_INVALID_PARM);
+
+    /* Get the mutex */
+    err = os_mutex_pend(mu, 0);
+    TEST_ASSERT(err == 0,
+                "Did not get free mutex immediately (err=%d)", err);
+
+    /* Check mutex internals */
+    TEST_ASSERT(mu->mu_owner == t && mu->mu_level == 1 &&
+                mu->mu_prio == t->t_prio && SLIST_EMPTY(&mu->mu_head),
+                "Mutex internals not correct after getting mutex\n"
+                "Mutex: owner=%p prio=%u level=%u head=%p\n"
+                "Task: task=%p prio=%u",
+                mu->mu_owner, mu->mu_prio, mu->mu_level, 
+                SLIST_FIRST(&mu->mu_head),
+                t, t->t_prio);
+
+    /* Get the mutex again; should be level 2 */
+    err = os_mutex_pend(mu, 0);
+    TEST_ASSERT(err == 0, "Did not get my mutex immediately (err=%d)", err);
+
+    /* Check mutex internals */
+    TEST_ASSERT(mu->mu_owner == t && mu->mu_level == 2 &&
+                mu->mu_prio == t->t_prio && SLIST_EMPTY(&mu->mu_head),
+                "Mutex internals not correct after getting mutex\n"
+                "Mutex: owner=%p prio=%u level=%u head=%p\n"
+                "Task: task=%p prio=%u",
+                mu->mu_owner, mu->mu_prio, mu->mu_level, 
+                SLIST_FIRST(&mu->mu_head), t, t->t_prio);
+
+    /* Release mutex */
+    err = os_mutex_release(mu);
+    TEST_ASSERT(err == 0, "Could not release mutex I own (err=%d)", err);
+
+    /* Check mutex internals */
+    TEST_ASSERT(mu->mu_owner == t && mu->mu_level == 1 &&
+                mu->mu_prio == t->t_prio && SLIST_EMPTY(&mu->mu_head),
+                "Error: mutex internals not correct after getting mutex\n"
+                "Mutex: owner=%p prio=%u level=%u head=%p\n"
+                "Task: task=%p prio=%u",
+                mu->mu_owner, mu->mu_prio, mu->mu_level, 
+                SLIST_FIRST(&mu->mu_head), t, t->t_prio);
+
+    /* Release it again */
+    err = os_mutex_release(mu);
+    TEST_ASSERT(err == 0, "Could not release mutex I own (err=%d)", err);
+
+    /* Check mutex internals */
+    TEST_ASSERT(mu->mu_owner == NULL && mu->mu_level == 0 &&
+                mu->mu_prio == t->t_prio && SLIST_EMPTY(&mu->mu_head),
+                "Mutex internals not correct after getting mutex\n"
+                "Mutex: owner=%p prio=%u level=%u head=%p\n"
+                "Task: task=%p prio=%u",
+                mu->mu_owner, mu->mu_prio, mu->mu_level, 
+                SLIST_FIRST(&mu->mu_head), t, t->t_prio);
+
+    os_test_restart();
+}
+
+static void 
+mutex_test1_task14_handler(void *arg)
+{
+    os_error_t err;
+    struct os_task *t;
+    int iters;
+
+    t = os_sched_get_current_task();
+    TEST_ASSERT(t->t_func == mutex_test1_task14_handler);
+
+    for (iters = 0; iters < 3; iters++) {
+        os_time_delay(100);
+
+        g_task14_val = 1;
+
+        err = os_mutex_pend(&g_mutex1, 100);
+        TEST_ASSERT(err == OS_OK);
+        TEST_ASSERT(g_task16_val == 1);
+
+        os_time_delay(100);
+    }
+
+    os_test_restart();
+}
+
+static void 
+mutex_test2_task14_handler(void *arg)
+{
+    os_error_t err;
+    struct os_task *t;
+    int iters;
+
+    t = os_sched_get_current_task();
+    TEST_ASSERT(t->t_func == mutex_test2_task14_handler);
+
+    for (iters = 0; iters < 3; iters++) {
+        err = os_mutex_pend(&g_mutex1, 0);
+        TEST_ASSERT(err == OS_OK, "err=%d", err);
+
+        g_task14_val = 1;
+        os_time_delay(100);
+
+        /* 
+         * Task17 should have its mutex wait flag set; at least the first time
+         * through!
+         */
+        if (iters == 0) {
+            TEST_ASSERT(task17.t_flags & OS_TASK_FLAG_MUTEX_WAIT);
+        }
+
+        if (g_mutex_test == 4) {
+            os_time_delay(150);
+        }
+
+        os_mutex_release(&g_mutex1);
+        os_time_delay(100);
+    }
+
+    os_test_restart();
+}
+
+static void 
+task15_handler(void *arg) 
+{
+    os_error_t err;
+    struct os_task *t;
+
+    if (g_mutex_test == 1) {
+        while (1) {
+            t = os_sched_get_current_task();
+            TEST_ASSERT(t->t_func == task15_handler);
+
+            os_time_delay(50);
+            while (1) {
+                /* Wait here forever */
+            }
+        }
+    } else {
+        if (g_mutex_test == 2) {
+            /* Sleep for 3 seconds */
+            t = os_sched_get_current_task();
+            os_time_delay(500);
+        } else if (g_mutex_test == 3) {
+            /* Sleep for 3 seconds */
+            t = os_sched_get_current_task();
+            os_time_delay(30);
+        }
+
+        while (1) {
+            t = os_sched_get_current_task();
+            TEST_ASSERT(t->t_func == task15_handler);
+
+            err = os_mutex_pend(&g_mutex1, 10000);
+            if (g_mutex_test == 4) {
+                TEST_ASSERT(err == OS_TIMEOUT);
+            } else {
+                TEST_ASSERT(err == OS_OK);
+            }
+
+            os_time_delay(100);
+        }
+    }
+}
+
+static void 
+task16_handler(void *arg) 
+{
+    os_error_t err;
+    struct os_task *t;
+
+    if (g_mutex_test == 1) {
+        while (1) {
+            t = os_sched_get_current_task();
+            TEST_ASSERT(t->t_func == task16_handler);
+
+            /* Get mutex 1 */
+            err = os_mutex_pend(&g_mutex1, 0xFFFFFFFF);
+            TEST_ASSERT(err == OS_OK);
+
+            while (g_task14_val != 1) {
+                /* Wait till task 1 wakes up and sets val. */
+            }
+
+            g_task16_val = 1;
+
+            err = os_mutex_release(&g_mutex1);
+            TEST_ASSERT(err == OS_OK);
+        }
+    } else {
+        if (g_mutex_test == 2) {
+            /* Sleep for 3 seconds */
+            t = os_sched_get_current_task();
+            os_time_delay(30);
+        } else if (g_mutex_test == 3) {
+            /* Sleep for 3 seconds */
+            t = os_sched_get_current_task();
+            os_time_delay(50);
+        }
+
+        while (1) {
+            t = os_sched_get_current_task();
+            TEST_ASSERT(t->t_func == task16_handler);
+
+            err = os_mutex_pend(&g_mutex1, 10000);
+            if (g_mutex_test == 4) {
+                TEST_ASSERT(err == OS_TIMEOUT);
+            } else {
+                TEST_ASSERT(err == OS_OK);
+            }
+
+            if (err == OS_OK) {
+                err = os_mutex_release(&g_mutex1);
+                TEST_ASSERT(err == OS_OK);
+            }
+
+            os_time_delay(10000);
+        }
+    }
+}
+
+static void 
+task17_handler(void *arg)
+{
+    os_error_t err;
+    struct os_task *t;
+
+    while (1) {
+        t = os_sched_get_current_task();
+        TEST_ASSERT(t->t_func == task17_handler);
+
+        if (g_mutex_test == 5) {
+            err = os_mutex_pend(&g_mutex1, 10);
+        } else {
+            err = os_mutex_pend(&g_mutex1, 10000);
+            TEST_ASSERT((t->t_flags & OS_TASK_FLAG_MUTEX_WAIT) == 0);
+        }
+
+        if (g_mutex_test == 4 || g_mutex_test == 5) {
+            TEST_ASSERT(err == OS_TIMEOUT);
+        } else {
+            TEST_ASSERT(err == OS_OK);
+        }
+
+        if (err == OS_OK) {
+            err = os_mutex_release(&g_mutex1);
+            TEST_ASSERT(err == OS_OK);
+        }
+
+        os_time_delay(10000);
+    }
+}
+
+TEST_CASE(os_mutex_test_basic)
+{
+    os_init();
+
+    os_mutex_init(&g_mutex1);
+
+    os_task_init(&task14, "task14", mutex_test_basic_handler, NULL,
+                 TASK14_PRIO, OS_WAIT_FOREVER, stack14,
+                 OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+TEST_CASE(os_mutex_test_case_1)
+{
+    int rc;
+
+    os_init();
+
+    g_mutex_test = 1;
+    g_task14_val = 0;
+    g_task15_val = 0;
+    g_task16_val = 0;
+
+    rc = os_mutex_init(&g_mutex1);
+    TEST_ASSERT(rc == 0);
+    rc = os_mutex_init(&g_mutex2);
+    TEST_ASSERT(rc == 0);
+
+    os_task_init(&task14, "task14", mutex_test1_task14_handler, NULL,
+                 TASK14_PRIO, OS_WAIT_FOREVER, stack14,
+                 OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
+
+    os_task_init(&task15, "task15", task15_handler, NULL, TASK15_PRIO, 
+            OS_WAIT_FOREVER, stack15, OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
+
+    os_task_init(&task16, "task16", task16_handler, NULL, TASK16_PRIO, 
+            OS_WAIT_FOREVER, stack16, OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+TEST_CASE(os_mutex_test_case_2)
+{
+    os_init();
+
+    g_mutex_test = 2;
+    g_task14_val = 0;
+    g_task15_val = 0;
+    g_task16_val = 0;
+    os_mutex_init(&g_mutex1);
+    os_mutex_init(&g_mutex2);
+
+    os_task_init(&task14, "task14", mutex_test2_task14_handler, NULL,
+                 TASK14_PRIO, OS_WAIT_FOREVER, stack14,
+                 OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
+
+    os_task_init(&task15, "task15", task15_handler, NULL, TASK15_PRIO, 
+            OS_WAIT_FOREVER, stack15, OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
+
+    os_task_init(&task16, "task16", task16_handler, NULL, TASK16_PRIO, 
+            OS_WAIT_FOREVER, stack16, OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
+
+    os_task_init(&task17, "task17", task17_handler, NULL, TASK17_PRIO, 
+            OS_WAIT_FOREVER, stack17, OS_STACK_ALIGN(MUTEX_TEST_STACK_SIZE));
+ 
+    os_start();
+}
+
+TEST_SUITE(os_mutex_test_suite)
+{
+    os_mutex_test_basic();
+    os_mutex_test_case_1();
+    os_mutex_test_case_2();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/test/src/os_test.c
----------------------------------------------------------------------
diff --git a/libs/os/test/src/os_test.c b/libs/os/test/src/os_test.c
new file mode 100644
index 0000000..809ab9f
--- /dev/null
+++ b/libs/os/test/src/os_test.c
@@ -0,0 +1,53 @@
+/**
+ * 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 <assert.h>
+#include <stddef.h>
+#include "syscfg/syscfg.h"
+#include "testutil/testutil.h"
+#include "os/os_test.h"
+#include "os_test_priv.h"
+
+int
+os_test_all(void)
+{
+    os_mempool_test_suite();
+    os_mutex_test_suite();
+    os_sem_test_suite();
+    os_mbuf_test_suite();
+    os_eventq_test_suite();
+    
+
+    return tu_case_failed;
+}
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    os_test_all();
+
+    return tu_any_failed;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/test/src/os_test_priv.h
----------------------------------------------------------------------
diff --git a/libs/os/test/src/os_test_priv.h b/libs/os/test/src/os_test_priv.h
new file mode 100644
index 0000000..5193c33
--- /dev/null
+++ b/libs/os/test/src/os_test_priv.h
@@ -0,0 +1,32 @@
+/**
+ * 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_OS_TEST_PRIV_
+#define H_OS_TEST_PRIV_
+
+void os_test_restart(void);
+
+int os_mempool_test_suite(void);
+int os_mbuf_test_suite(void);
+int os_mutex_test_suite(void);
+int os_sem_test_suite(void);
+int os_eventq_test_suite(void);
+
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/os/test/src/sem_test.c
----------------------------------------------------------------------
diff --git a/libs/os/test/src/sem_test.c b/libs/os/test/src/sem_test.c
new file mode 100644
index 0000000..ec79185
--- /dev/null
+++ b/libs/os/test/src/sem_test.c
@@ -0,0 +1,401 @@
+/**
+ * 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 <stdio.h>
+#include <string.h>
+#include "testutil/testutil.h"
+#include "os/os.h"
+#include "os/os_cfg.h"
+#include "os/os_sem.h"
+#include "os_test_priv.h"
+
+#ifdef ARCH_sim
+#define SEM_TEST_STACK_SIZE     1024
+#else 
+#define SEM_TEST_STACK_SIZE     512
+#endif
+
+struct os_task task1;
+os_stack_t stack1[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
+
+struct os_task task2;
+os_stack_t stack2[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
+
+struct os_task task3;
+os_stack_t stack3[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
+
+struct os_task task4;
+os_stack_t stack4[OS_STACK_ALIGN(SEM_TEST_STACK_SIZE)];
+
+#define TASK1_PRIO (1) 
+#define TASK2_PRIO (2) 
+#define TASK3_PRIO (3) 
+#define TASK4_PRIO (4) 
+
+struct os_sem g_sem1;
+
+/* 
+ * TEST NUMBERS:
+ *  10: In this test we have the highest priority task getting the semaphore
+ *  then sleeping. Two lower priority tasks then wake up and attempt to get
+ *  the semaphore. They are blocked until the higher priority task releases
+ *  the semaphore, at which point the lower priority tasks should wake up in
+ *  order, get the semaphore, then release it and go back to sleep.
+ * 
+ */
+
+/**
+ * sem test disp sem
+ *  
+ * Display semaphore contents 
+ * 
+ * @param sem 
+ */
+static const char *
+sem_test_sem_to_s(const struct os_sem *sem)
+{
+    static char buf[128];
+
+    snprintf(buf, sizeof buf, "\tSemaphore: tokens=%u head=%p",
+             sem->sem_tokens, SLIST_FIRST(&sem->sem_head));
+
+    return buf;
+}
+
+static void 
+sem_test_sleep_task_handler(void *arg)
+{
+    struct os_task *t;
+
+    t = os_sched_get_current_task();
+    TEST_ASSERT(t->t_func == sem_test_sleep_task_handler);
+
+    os_time_delay(2000);
+    os_test_restart();
+}
+
+static void
+sem_test_pend_release_loop(int delay, int timeout, int itvl)
+{
+    os_error_t err;
+
+    os_time_delay(delay);
+
+    while (1) {
+        err = os_sem_pend(&g_sem1, timeout);
+        TEST_ASSERT((err == OS_OK) || (err == OS_TIMEOUT));
+
+        err = os_sem_release(&g_sem1);
+        TEST_ASSERT(err == OS_OK);
+
+        os_time_delay(itvl);
+    }
+}
+
+/**
+ * sem test basic 
+ *  
+ * Basic semaphore tests
+ * 
+ * @return int 
+ */
+static void 
+sem_test_basic_handler(void *arg)
+{
+    struct os_task *t;
+    struct os_sem *sem;
+    os_error_t err;
+
+    sem = &g_sem1;
+    t = os_sched_get_current_task();
+
+    /* Test some error cases */
+    TEST_ASSERT(os_sem_init(NULL, 1)    == OS_INVALID_PARM);
+    TEST_ASSERT(os_sem_release(NULL)    == OS_INVALID_PARM);
+    TEST_ASSERT(os_sem_pend(NULL, 1)    == OS_INVALID_PARM);
+
+    /* Get the semaphore */
+    err = os_sem_pend(sem, 0);
+    TEST_ASSERT(err == 0,
+                "Did not get free semaphore immediately (err=%d)", err);
+
+    /* Check semaphore internals */
+    TEST_ASSERT(sem->sem_tokens == 0 && SLIST_EMPTY(&sem->sem_head),
+                "Semaphore internals wrong after getting semaphore\n"
+                "%s\n"
+                "Task: task=%p prio=%u", sem_test_sem_to_s(sem), t, t->t_prio);
+
+    /* Get the semaphore again; should fail */
+    err = os_sem_pend(sem, 0);
+    TEST_ASSERT(err == OS_TIMEOUT,
+                "Did not time out waiting for semaphore (err=%d)", err);
+
+    /* Check semaphore internals */
+    TEST_ASSERT(sem->sem_tokens == 0 && SLIST_EMPTY(&sem->sem_head),
+                "Semaphore internals wrong after getting semaphore\n"
+                "%s\n"
+                "Task: task=%p prio=%u\n", sem_test_sem_to_s(sem), t,
+                t->t_prio);
+
+    /* Release semaphore */
+    err = os_sem_release(sem);
+    TEST_ASSERT(err == 0,
+                "Could not release semaphore I own (err=%d)", err);
+
+    /* Check semaphore internals */
+    TEST_ASSERT(sem->sem_tokens == 1 && SLIST_EMPTY(&sem->sem_head),
+                "Semaphore internals wrong after releasing semaphore\n"
+                "%s\n"
+                "Task: task=%p prio=%u\n", sem_test_sem_to_s(sem), t,
+                t->t_prio);
+
+    /* Release it again */
+    err = os_sem_release(sem);
+    TEST_ASSERT(err == 0,
+                "Could not release semaphore again (err=%d)\n", err);
+
+    /* Check semaphore internals */
+    TEST_ASSERT(sem->sem_tokens == 2 && SLIST_EMPTY(&sem->sem_head),
+                "Semaphore internals wrong after releasing semaphore\n"
+                "%s\n"
+                "Task: task=%p prio=%u\n", sem_test_sem_to_s(sem), t,
+                t->t_prio);
+
+    os_test_restart();
+}
+
+static void 
+sem_test_1_task1_handler(void *arg)
+{
+    os_error_t err;
+    struct os_task *t;
+    int i;;
+
+    for (i = 0; i < 3; i++) {
+        t = os_sched_get_current_task();
+        TEST_ASSERT(t->t_func == sem_test_1_task1_handler);
+
+
+        err = os_sem_pend(&g_sem1, 0);
+        TEST_ASSERT(err == OS_OK);
+
+        /* Sleep to let other tasks run */
+        os_time_delay(100);
+
+        /* Release the semaphore */
+        err = os_sem_release(&g_sem1);
+        TEST_ASSERT(err == OS_OK);
+
+        /* Sleep to let other tasks run */
+        os_time_delay(100);
+    }
+
+    os_test_restart();
+}
+
+TEST_CASE(os_sem_test_basic)
+{
+    os_error_t err;
+
+    os_init();
+
+    err = os_sem_init(&g_sem1, 1);
+    TEST_ASSERT(err == OS_OK);
+
+    os_task_init(&task1, "task1", sem_test_basic_handler, NULL, TASK1_PRIO, 
+            OS_WAIT_FOREVER, stack1, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+static void 
+sem_test_1_task2_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, 100, 100);
+}
+
+static void 
+sem_test_1_task3_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, OS_TIMEOUT_NEVER, 2000);
+}
+
+TEST_CASE(os_sem_test_case_1)
+{
+    os_error_t err;
+
+    os_init();
+
+    err = os_sem_init(&g_sem1, 1);
+    TEST_ASSERT(err == OS_OK);
+
+    os_task_init(&task1, "task1", sem_test_1_task1_handler, NULL,
+                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task2, "task2", sem_test_1_task2_handler, NULL,
+                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task3, "task3", sem_test_1_task3_handler, NULL, TASK3_PRIO, 
+                 OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+static void 
+sem_test_2_task2_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, 2000, 2000);
+}
+
+static void 
+sem_test_2_task3_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, OS_TIMEOUT_NEVER, 2000);
+}
+
+static void 
+sem_test_2_task4_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, 2000, 2000);
+}
+
+TEST_CASE(os_sem_test_case_2)
+{
+    os_error_t err;
+
+    os_init();
+
+    err = os_sem_init(&g_sem1, 1);
+    TEST_ASSERT(err == OS_OK);
+
+    os_task_init(&task1, "task1", sem_test_sleep_task_handler, NULL,
+                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task2, "task2", sem_test_2_task2_handler, NULL,
+                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task3, "task3", sem_test_2_task3_handler, NULL, TASK3_PRIO,
+            OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task4, "task4", sem_test_2_task4_handler, NULL, TASK4_PRIO,
+            OS_WAIT_FOREVER, stack4, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+static void 
+sem_test_3_task2_handler(void *arg) 
+{
+    sem_test_pend_release_loop(100, 2000, 2000);
+}
+
+static void 
+sem_test_3_task3_handler(void *arg) 
+{
+    sem_test_pend_release_loop(150, 2000, 2000);
+}
+
+static void 
+sem_test_3_task4_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, 2000, 2000);
+}
+
+TEST_CASE(os_sem_test_case_3)
+{
+    os_error_t err;
+
+    os_init();
+
+    err = os_sem_init(&g_sem1, 1);
+    TEST_ASSERT(err == OS_OK);
+
+    os_task_init(&task1, "task1", sem_test_sleep_task_handler, NULL,
+                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task2, "task2", sem_test_3_task2_handler, NULL,
+                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task3, "task3", sem_test_3_task3_handler, NULL, TASK3_PRIO,
+            OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task4, "task4", sem_test_3_task4_handler, NULL, TASK4_PRIO,
+            OS_WAIT_FOREVER, stack4, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+static void 
+sem_test_4_task2_handler(void *arg) 
+{
+    sem_test_pend_release_loop(60, 2000, 2000);
+}
+
+static void 
+sem_test_4_task3_handler(void *arg) 
+{
+    sem_test_pend_release_loop(60, 2000, 2000);
+}
+
+static void 
+sem_test_4_task4_handler(void *arg) 
+{
+    sem_test_pend_release_loop(0, 2000, 2000);
+}
+
+
+TEST_CASE(os_sem_test_case_4)
+{
+    os_error_t err;
+
+    os_init();
+
+    err = os_sem_init(&g_sem1, 1);
+    TEST_ASSERT(err == OS_OK);
+
+    os_task_init(&task1, "task1", sem_test_sleep_task_handler, NULL,
+                 TASK1_PRIO, OS_WAIT_FOREVER, stack1,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task2, "task2", sem_test_4_task2_handler, NULL,
+                 TASK2_PRIO, OS_WAIT_FOREVER, stack2,
+                 OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task3, "task3", sem_test_4_task3_handler, NULL, TASK3_PRIO,
+                 OS_WAIT_FOREVER, stack3, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_task_init(&task4, "task4", sem_test_4_task4_handler, NULL, TASK4_PRIO,
+                 OS_WAIT_FOREVER, stack4, OS_STACK_ALIGN(SEM_TEST_STACK_SIZE));
+
+    os_start();
+}
+
+TEST_SUITE(os_sem_test_suite)
+{
+    os_sem_test_basic();
+    os_sem_test_case_1();
+    os_sem_test_case_2();
+    os_sem_test_case_3();
+    os_sem_test_case_4();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/shell/include/shell/shell.h
----------------------------------------------------------------------
diff --git a/libs/shell/include/shell/shell.h b/libs/shell/include/shell/shell.h
index 53c890d..6b89740 100644
--- a/libs/shell/include/shell/shell.h
+++ b/libs/shell/include/shell/shell.h
@@ -40,7 +40,6 @@ int shell_nlip_input_register(shell_nlip_input_func_t nf, void *arg);
 int shell_nlip_output(struct os_mbuf *m);
 
 void shell_console_rx_cb(void);
-int shell_task_init(uint8_t prio, os_stack_t *stack, uint16_t stack_size,
-                    int max_input_length);
+void shell_init(void);
 
 #endif /* __SHELL_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/shell/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/shell/pkg.yml b/libs/shell/pkg.yml
index dcfc484..36372ee 100644
--- a/libs/shell/pkg.yml
+++ b/libs/shell/pkg.yml
@@ -28,5 +28,18 @@ pkg.deps:
     - libs/util
 pkg.req_apis:
     - console
-pkg.features:
-    - SHELL 
+
+pkg.init_function: shell_init
+pkg.init_stage: 5
+
+pkg.syscfg_defs:
+    SHELL_TASK_PRIO:
+        description: 'TBD'
+        type: 'task_priority'
+        value: 'any'
+    SHELL_STACK_SIZE:
+        description: 'TBD'
+        value:     512
+    SHELL_MAX_INPUT_LEN:
+        description: 'TBD'
+        value:  256

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/libs/shell/src/shell.c b/libs/shell/src/shell.c
index 115a62c..7fe15e5 100644
--- a/libs/shell/src/shell.c
+++ b/libs/shell/src/shell.c
@@ -17,22 +17,22 @@
  * under the License.
  */
 
-#include <os/os.h>
-
-#include <console/console.h>
-
-#include "shell/shell.h"
-#include "shell_priv.h"
-
-#include <os/endian.h>
-#include <util/base64.h>
-#include <util/crc16.h>
 
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
 #include <errno.h>
 
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
+#include "console/console.h"
+#include "os/os.h"
+#include "os/endian.h"
+#include "util/base64.h"
+#include "util/crc16.h"
+#include "shell/shell.h"
+#include "shell_priv.h"
+
 static shell_nlip_input_func_t g_shell_nlip_in_func;
 static void *g_shell_nlip_in_arg;
 
@@ -42,6 +42,8 @@ static struct os_mqueue g_shell_nlip_mq;
 #define SHELL_HELP_PER_LINE     6
 #define SHELL_MAX_ARGS          20
 
+static os_stack_t shell_stack[OS_STACK_ALIGN(MYNEWT_VAL(SHELL_STACK_SIZE))];
+
 static int shell_echo_cmd(int argc, char **argv);
 static int shell_help_cmd(int argc, char **argv);
 
@@ -73,7 +75,6 @@ static struct os_event console_rdy_ev;
 static struct os_mutex g_shell_cmd_list_lock;
 
 static char *shell_line;
-static int shell_line_capacity;
 static int shell_line_len;
 static char *argv[SHELL_MAX_ARGS];
 
@@ -417,7 +418,7 @@ shell_read_console(void)
 
     while (1) {
         rc = console_read(shell_line + shell_line_len,
-          shell_line_capacity - shell_line_len, &full_line);
+          MYNEWT_VAL(SHELL_MAX_INPUT_LEN) - shell_line_len, &full_line);
         if (rc <= 0 && !full_line) {
             break;
         }
@@ -523,52 +524,36 @@ shell_help_cmd(int argc, char **argv)
     return (0);
 }
 
-int
-shell_task_init(uint8_t prio, os_stack_t *stack, uint16_t stack_size,
-                int max_input_length)
+void
+shell_init(void)
 {
     int rc;
 
     free(shell_line);
+    shell_line = NULL;
 
-    if (max_input_length > 0) {
-        shell_line = malloc(max_input_length);
-        if (shell_line == NULL) {
-            rc = ENOMEM;
-            goto err;
-        }
-    }
-    shell_line_capacity = max_input_length;
+#if MYNEWT_VAL(SHELL_MAX_INPUT_LEN) > 0
+    shell_line = malloc(MYNEWT_VAL(SHELL_MAX_INPUT_LEN));
+    SYSINIT_PANIC_ASSERT(shell_line != NULL);
+#endif
 
     rc = os_mutex_init(&g_shell_cmd_list_lock);
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = shell_cmd_register(&g_shell_echo_cmd);
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = shell_cmd_register(&g_shell_help_cmd);
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = shell_cmd_register(&g_shell_os_tasks_display_cmd);
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = shell_cmd_register(&g_shell_os_mpool_display_cmd);
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = shell_cmd_register(&g_shell_os_date_cmd);
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     os_eventq_init(&shell_evq);
     os_mqueue_init(&g_shell_nlip_mq, NULL);
@@ -576,14 +561,7 @@ shell_task_init(uint8_t prio, os_stack_t *stack, uint16_t stack_size,
     console_init(shell_console_rx_cb);
 
     rc = os_task_init(&shell_task, "shell", shell_task_func,
-            NULL, prio, OS_WAIT_FOREVER, stack, stack_size);
-    if (rc != 0) {
-        goto err;
-    }
-
-    return (0);
-err:
-    free(shell_line);
-    shell_line = NULL;
-    return (rc);
+            NULL, MYNEWT_VAL(SHELL_TASK_PRIO), OS_WAIT_FOREVER, shell_stack,
+            MYNEWT_VAL(SHELL_STACK_SIZE));
+    SYSINIT_PANIC_ASSERT(rc == 0);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/testutil/include/testutil/testutil.h
----------------------------------------------------------------------
diff --git a/libs/testutil/include/testutil/testutil.h b/libs/testutil/include/testutil/testutil.h
index 9c6192a..de61394 100644
--- a/libs/testutil/include/testutil/testutil.h
+++ b/libs/testutil/include/testutil/testutil.h
@@ -23,6 +23,8 @@
 #include <inttypes.h>
 #include <setjmp.h>
 
+#include "syscfg/syscfg.h"
+
 /*****************************************************************************
  * Public declarations                                                       *
  *****************************************************************************/
@@ -163,7 +165,7 @@ extern jmp_buf tu_case_jb;
 #define TEST_PASS(...)                                                        \
     tu_case_pass_manual(__FILE__, __LINE__, __VA_ARGS__);
 
-#ifdef MYNEWT_UNIT_TEST
+#if MYNEWT_VAL(TEST)
 #define ASSERT_IF_TEST(expr) assert(expr)
 #else
 #define ASSERT_IF_TEST(expr)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/testutil/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/testutil/pkg.yml b/libs/testutil/pkg.yml
index 605065e..8314776 100644
--- a/libs/testutil/pkg.yml
+++ b/libs/testutil/pkg.yml
@@ -28,5 +28,3 @@ pkg.keywords:
 pkg.deps:
     - hw/hal
     - libs/os
-
-pkg.cflags.TEST: "-DMYNEWT_UNIT_TEST"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/util/include/util/mem.h
----------------------------------------------------------------------
diff --git a/libs/util/include/util/mem.h b/libs/util/include/util/mem.h
index 52ddce4..46601ba 100644
--- a/libs/util/include/util/mem.h
+++ b/libs/util/include/util/mem.h
@@ -34,5 +34,8 @@ 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 mem_init_mbuf_pool(void *mem, struct os_mempool *mempool,
+                       struct os_mbuf_pool *mbuf_pool, int num_blocks,
+                       int block_size, char *name);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/util/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/util/pkg.yml b/libs/util/pkg.yml
index 3b37b11..b922830 100644
--- a/libs/util/pkg.yml
+++ b/libs/util/pkg.yml
@@ -28,4 +28,3 @@ pkg.keywords:
 pkg.deps:
     - hw/hal
     - libs/os
-    - libs/testutil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/util/src/mem.c
----------------------------------------------------------------------
diff --git a/libs/util/src/mem.c b/libs/util/src/mem.c
index faf8234..7cbe3d5 100644
--- a/libs/util/src/mem.c
+++ b/libs/util/src/mem.c
@@ -144,3 +144,23 @@ mem_malloc_mbufpkt_pool(struct os_mempool *mempool,
                               name, out_buf);
     return rc;
 }
+
+int
+mem_init_mbuf_pool(void *mem, struct os_mempool *mempool,
+                   struct os_mbuf_pool *mbuf_pool, int num_blocks,
+                   int block_size, char *name)
+{
+    int rc;
+
+    rc = os_mempool_init(mempool, num_blocks, block_size, mem, name);
+    if (rc != 0) {
+        return rc;
+    }
+
+    rc = os_mbuf_pool_init(mbuf_pool, mempool, block_size, num_blocks);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
+}


[30/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/bootutil/test/src/boot_test.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/test/src/boot_test.c b/libs/bootutil/test/src/boot_test.c
new file mode 100644
index 0000000..b86ca82
--- /dev/null
+++ b/libs/bootutil/test/src/boot_test.c
@@ -0,0 +1,1170 @@
+/**
+ * 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 <assert.h>
+#include <stddef.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <inttypes.h>
+#include "syscfg/syscfg.h"
+#include "testutil/testutil.h"
+#include "hal/hal_flash.h"
+#include "hal/flash_map.h"
+#include "fs/fs.h"
+#include "nffs/nffs.h"
+#include "config/config_file.h"
+#include "bootutil/image.h"
+#include "bootutil/loader.h"
+#include "bootutil/bootutil_misc.h"
+#include "../src/bootutil_priv.h"
+
+#include "mbedtls/sha256.h"
+
+#define BOOT_TEST_HEADER_SIZE       0x200
+
+/** Internal flash layout. */
+static struct flash_area boot_test_area_descs[] = {
+    [0] = { .fa_off = 0x00020000, .fa_size = 128 * 1024 },
+    [1] = { .fa_off = 0x00040000, .fa_size = 128 * 1024 },
+    [2] = { .fa_off = 0x00060000, .fa_size = 128 * 1024 },
+    [3] = { .fa_off = 0x00080000, .fa_size = 128 * 1024 },
+    [4] = { .fa_off = 0x000a0000, .fa_size = 128 * 1024 },
+    [5] = { .fa_off = 0x000c0000, .fa_size = 128 * 1024 },
+    [6] = { .fa_off = 0x000e0000, .fa_size = 128 * 1024 },
+};
+
+static const struct flash_area boot_test_format_descs[] = {
+    [0] = { .fa_off = 0x00004000, .fa_size = 16 * 1024 },
+    [1] = { .fa_off = 0x00008000, .fa_size = 16 * 1024 },
+    [2] = { .fa_off = 0x0000c000, .fa_size = 16 * 1024 },
+    [3] = { .fa_off = 0, .fa_size = 0 },
+};
+
+/** Areas representing the beginning of image slots. */
+static uint8_t boot_test_slot_areas[] = {
+    0, 3,
+};
+
+/** Flash offsets of the two image slots. */
+static struct {
+    uint8_t flash_id;
+    uint32_t address;
+} boot_test_img_addrs[] = {
+    { 0, 0x20000 },
+    { 0, 0x80000 },
+};
+
+#define BOOT_TEST_AREA_IDX_SCRATCH 6
+
+#define MY_CONF_PATH "/cfg/run"
+
+static struct conf_file my_conf = {
+    .cf_name = MY_CONF_PATH
+};
+
+static uint8_t
+boot_test_util_byte_at(int img_msb, uint32_t image_offset)
+{
+    uint32_t u32;
+    uint8_t *u8p;
+
+    TEST_ASSERT(image_offset < 0x01000000);
+    u32 = image_offset + (img_msb << 24);
+    u8p = (void *)&u32;
+    return u8p[image_offset % 4];
+}
+
+static void
+boot_test_util_init_flash(void)
+{
+    const struct flash_area *area_desc;
+    int rc;
+    struct nffs_area_desc nffs_descs[32];
+    int cnt;
+
+    rc = hal_flash_init();
+    TEST_ASSERT(rc == 0);
+
+    for (area_desc = boot_test_area_descs;
+         area_desc->fa_size != 0;
+         area_desc++) {
+
+        rc = flash_area_erase(area_desc, 0, area_desc->fa_size);
+        TEST_ASSERT(rc == 0);
+    }
+    cnt = 32;
+
+    rc = nffs_misc_desc_from_flash_area(FLASH_AREA_NFFS, &cnt, nffs_descs);
+    TEST_ASSERT(rc == 0);
+
+    rc = nffs_init();
+    TEST_ASSERT(rc == 0);
+    rc = nffs_format(nffs_descs);
+    TEST_ASSERT(rc == 0);
+
+    fs_mkdir("/cfg");
+}
+
+static void
+boot_test_util_copy_area(int from_area_idx, int to_area_idx)
+{
+    const struct flash_area *from_area_desc;
+    const struct flash_area *to_area_desc;
+    void *buf;
+    int rc;
+
+    from_area_desc = boot_test_area_descs + from_area_idx;
+    to_area_desc = boot_test_area_descs + to_area_idx;
+
+    TEST_ASSERT(from_area_desc->fa_size == to_area_desc->fa_size);
+
+    buf = malloc(from_area_desc->fa_size);
+    TEST_ASSERT(buf != NULL);
+
+    rc = flash_area_read(from_area_desc, 0, buf,
+                         from_area_desc->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_erase(to_area_desc,
+                          0,
+                          to_area_desc->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_write(to_area_desc, 0, buf,
+                          to_area_desc->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    free(buf);
+}
+
+static void
+boot_test_util_swap_areas(int area_idx1, int area_idx2)
+{
+    const struct flash_area *area_desc1;
+    const struct flash_area *area_desc2;
+    void *buf1;
+    void *buf2;
+    int rc;
+
+    area_desc1 = boot_test_area_descs + area_idx1;
+    area_desc2 = boot_test_area_descs + area_idx2;
+
+    TEST_ASSERT(area_desc1->fa_size == area_desc2->fa_size);
+
+    buf1 = malloc(area_desc1->fa_size);
+    TEST_ASSERT(buf1 != NULL);
+
+    buf2 = malloc(area_desc2->fa_size);
+    TEST_ASSERT(buf2 != NULL);
+
+    rc = flash_area_read(area_desc1, 0, buf1, area_desc1->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_read(area_desc2, 0, buf2, area_desc2->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_erase(area_desc1, 0, area_desc1->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_erase(area_desc2, 0, area_desc2->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_write(area_desc1, 0, buf2, area_desc1->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    rc = flash_area_write(area_desc2, 0, buf1, area_desc2->fa_size);
+    TEST_ASSERT(rc == 0);
+
+    free(buf1);
+    free(buf2);
+}
+
+static void
+boot_test_util_write_image(const struct image_header *hdr, int slot)
+{
+    uint32_t image_off;
+    uint32_t off;
+    uint8_t flash_id;
+    uint8_t buf[256];
+    int chunk_sz;
+    int rc;
+    int i;
+
+    TEST_ASSERT(slot == 0 || slot == 1);
+
+    flash_id = boot_test_img_addrs[slot].flash_id;
+    off = boot_test_img_addrs[slot].address;
+
+    rc = hal_flash_write(flash_id, off, hdr, sizeof *hdr);
+    TEST_ASSERT(rc == 0);
+
+    off += hdr->ih_hdr_size;
+
+    image_off = 0;
+    while (image_off < hdr->ih_img_size) {
+        if (hdr->ih_img_size - image_off > sizeof buf) {
+            chunk_sz = sizeof buf;
+        } else {
+            chunk_sz = hdr->ih_img_size - image_off;
+        }
+
+        for (i = 0; i < chunk_sz; i++) {
+            buf[i] = boot_test_util_byte_at(slot, image_off + i);
+        }
+
+        rc = hal_flash_write(flash_id, off + image_off, buf, chunk_sz);
+        TEST_ASSERT(rc == 0);
+
+        image_off += chunk_sz;
+    }
+}
+
+static void
+boot_test_util_write_hash(const struct image_header *hdr, int slot)
+{
+    uint8_t tmpdata[1024];
+    uint8_t hash[32];
+    int rc;
+    uint32_t off;
+    uint32_t blk_sz;
+    uint32_t sz;
+    mbedtls_sha256_context ctx;
+    uint8_t flash_id;
+    uint32_t addr;
+    struct image_tlv tlv;
+
+    mbedtls_sha256_init(&ctx);
+    mbedtls_sha256_starts(&ctx, 0);
+
+    flash_id = boot_test_img_addrs[slot].flash_id;
+    addr = boot_test_img_addrs[slot].address;
+
+    sz = hdr->ih_hdr_size + hdr->ih_img_size;
+    for (off = 0; off < sz; off += blk_sz) {
+        blk_sz = sz - off;
+        if (blk_sz > sizeof(tmpdata)) {
+            blk_sz = sizeof(tmpdata);
+        }
+        rc = hal_flash_read(flash_id, addr + off, tmpdata, blk_sz);
+        TEST_ASSERT(rc == 0);
+        mbedtls_sha256_update(&ctx, tmpdata, blk_sz);
+    }
+    mbedtls_sha256_finish(&ctx, hash);
+
+    tlv.it_type = IMAGE_TLV_SHA256;
+    tlv._pad = 0;
+    tlv.it_len = sizeof(hash);
+
+    rc = hal_flash_write(flash_id, addr + off, &tlv, sizeof(tlv));
+    TEST_ASSERT(rc == 0);
+    off += sizeof(tlv);
+    rc = hal_flash_write(flash_id, addr + off, hash, sizeof(hash));
+    TEST_ASSERT(rc == 0);
+}
+
+static void
+boot_test_util_verify_area(const struct flash_area *area_desc,
+                           const struct image_header *hdr,
+                           uint32_t image_addr, int img_msb)
+{
+    struct image_header temp_hdr;
+    uint32_t area_end;
+    uint32_t img_size;
+    uint32_t img_off;
+    uint32_t img_end;
+    uint32_t addr;
+    uint8_t buf[256];
+    int rem_area;
+    int past_image;
+    int chunk_sz;
+    int rem_img;
+    int rc;
+    int i;
+
+    addr = area_desc->fa_off;
+
+    if (hdr != NULL) {
+        img_size = hdr->ih_img_size;
+
+        if (addr == image_addr) {
+            rc = hal_flash_read(area_desc->fa_flash_id, image_addr,
+                                &temp_hdr, sizeof temp_hdr);
+            TEST_ASSERT(rc == 0);
+            TEST_ASSERT(memcmp(&temp_hdr, hdr, sizeof *hdr) == 0);
+
+            addr += hdr->ih_hdr_size;
+        }
+    } else {
+        img_size = 0;
+    }
+
+    area_end = area_desc->fa_off + area_desc->fa_size;
+    img_end = image_addr + img_size;
+    past_image = addr >= img_end;
+
+    while (addr < area_end) {
+        rem_area = area_end - addr;
+        rem_img = img_end - addr;
+
+        if (hdr != NULL) {
+            img_off = addr - image_addr - hdr->ih_hdr_size;
+        } else {
+            img_off = 0;
+        }
+
+        if (rem_area > sizeof buf) {
+            chunk_sz = sizeof buf;
+        } else {
+            chunk_sz = rem_area;
+        }
+
+        rc = hal_flash_read(area_desc->fa_flash_id, addr, buf, chunk_sz);
+        TEST_ASSERT(rc == 0);
+
+        for (i = 0; i < chunk_sz; i++) {
+            if (rem_img > 0) {
+                TEST_ASSERT(buf[i] == boot_test_util_byte_at(img_msb,
+                                                        img_off + i));
+            } else if (past_image) {
+                TEST_ASSERT(buf[i] == 0xff);
+            }
+        }
+
+        addr += chunk_sz;
+    }
+}
+
+static void
+boot_test_util_verify_status_clear(void)
+{
+    struct fs_file *file;
+    int rc;
+    int empty = 1;
+    char *needle = "boot/status=";
+    int nlen = strlen(needle);
+    uint32_t len, hlen;
+    char *haystack, *ptr;
+
+    rc = fs_open(MY_CONF_PATH, FS_ACCESS_READ, &file);
+    if (rc != 0) {
+        return;
+    }
+    rc = fs_filelen(file, &len);
+    TEST_ASSERT(rc == 0);
+
+    haystack = malloc(len + 1);
+    TEST_ASSERT(haystack);
+
+    rc = fs_read(file, len, haystack, &hlen);
+    TEST_ASSERT(rc == 0);
+    TEST_ASSERT(hlen == len);
+    haystack[len] = '\0';
+
+    fs_close(file);
+
+    ptr = haystack;
+    while ((ptr = strstr(ptr, needle))) {
+        if (ptr[nlen] == '\n') {
+            empty = 1;
+        } else {
+            empty = 0;
+        }
+        ptr += nlen;
+    }
+    TEST_ASSERT(empty == 1);
+    free(haystack);
+
+    rc = fs_open(BOOT_PATH_STATUS, FS_ACCESS_READ, &file);
+    TEST_ASSERT(rc == FS_ENOENT);
+}
+
+static void
+boot_test_util_verify_flash(const struct image_header *hdr0, int orig_slot_0,
+                            const struct image_header *hdr1, int orig_slot_1)
+{
+    const struct flash_area *area_desc;
+    int area_idx;
+
+    area_idx = 0;
+
+    while (1) {
+        area_desc = boot_test_area_descs + area_idx;
+        if (area_desc->fa_off == boot_test_img_addrs[1].address &&
+            area_desc->fa_flash_id == boot_test_img_addrs[1].flash_id) {
+            break;
+        }
+
+        boot_test_util_verify_area(area_desc, hdr0,
+                                   boot_test_img_addrs[0].address, orig_slot_0);
+        area_idx++;
+    }
+
+    while (1) {
+        if (area_idx == BOOT_TEST_AREA_IDX_SCRATCH) {
+            break;
+        }
+
+        area_desc = boot_test_area_descs + area_idx;
+        boot_test_util_verify_area(area_desc, hdr1,
+                                   boot_test_img_addrs[1].address, orig_slot_1);
+        area_idx++;
+    }
+}
+
+TEST_CASE(boot_test_setup)
+{
+    int rc;
+
+    rc = conf_file_src(&my_conf);
+    assert(rc == 0);
+    rc = conf_file_dst(&my_conf);
+    assert(rc == 0);
+
+    bootutil_cfg_register();
+}
+
+TEST_CASE(boot_test_nv_ns_10)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+    boot_test_util_write_hash(&hdr, 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_nv_ns_01)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 10 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 1);
+    boot_test_util_write_hash(&hdr, 1);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr, 1, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_nv_ns_11)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 5 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 32 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr0, 0, &hdr1, 1);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vm_ns_10)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+    boot_test_util_write_hash(&hdr, 0);
+
+    rc = boot_vect_write_main(&hdr.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vm_ns_01)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 10 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 1);
+    boot_test_util_write_hash(&hdr, 1);
+
+    rc = boot_vect_write_main(&hdr.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr, 1, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vm_ns_11_a)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 5 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 32 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+
+    rc = boot_vect_write_main(&hdr0.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr0, 0, &hdr1, 1);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vm_ns_11_b)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 5 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 32 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+
+    rc = boot_vect_write_main(&hdr1.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vm_ns_11_2areas)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 5 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 196 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+
+    rc = boot_vect_write_main(&hdr1.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_nv_bs_10)
+{
+    struct boot_status status;
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+    boot_test_util_write_hash(&hdr, 0);
+    boot_test_util_swap_areas(boot_test_slot_areas[1],
+      BOOT_TEST_AREA_IDX_SCRATCH);
+
+    status.length = hdr.ih_hdr_size + hdr.ih_img_size + hdr.ih_tlv_size;
+    status.state = 1;
+
+    rc = boot_write_status(&status);
+    TEST_ASSERT(rc == 0);
+    conf_load();
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr, sizeof hdr) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_nv_bs_11)
+{
+    struct boot_status status;
+    struct boot_rsp rsp;
+    int len;
+    int rc;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 17 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 1, 5, 5 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+    boot_test_util_copy_area(boot_test_slot_areas[1],
+      BOOT_TEST_AREA_IDX_SCRATCH);
+
+    status.length = hdr0.ih_hdr_size + hdr0.ih_img_size + hdr0.ih_tlv_size;
+    len = hdr1.ih_hdr_size + hdr1.ih_img_size + hdr1.ih_tlv_size;
+    if (len > status.length) {
+        status.length = len;
+    }
+    status.state = 1;
+
+    rc = boot_write_status(&status);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_nv_bs_11_2areas)
+{
+    struct boot_status status;
+    struct boot_rsp rsp;
+    int rc;
+    int len;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 150 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 190 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr1, 1);
+    boot_test_util_swap_areas(boot_test_slot_areas[0],
+      boot_test_slot_areas[1]);
+
+    status.length = hdr0.ih_hdr_size + hdr0.ih_img_size + hdr0.ih_tlv_size;
+    len = hdr1.ih_hdr_size + hdr1.ih_img_size + hdr1.ih_tlv_size;
+    if (len > status.length) {
+        status.length = len;
+    }
+    status.state = 1 << 8;
+
+    rc = boot_write_status(&status);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_vb_ns_11)
+{
+    struct boot_rsp rsp;
+    int rc;
+    int i;
+
+    struct image_header hdr0 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 5 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 5, 21, 432 },
+    };
+
+    struct image_header hdr1 = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 32 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 1, 2, 3, 432 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr0, 0);
+    boot_test_util_write_image(&hdr1, 1);
+    boot_test_util_write_hash(&hdr0, 0);
+    boot_test_util_write_hash(&hdr1, 1);
+
+    rc = boot_vect_write_main(&hdr0.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_vect_write_test(&hdr1.ih_ver);
+    TEST_ASSERT(rc == 0);
+
+    /* First boot should use the test image. */
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc == 0);
+
+    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr1, sizeof hdr1) == 0);
+    TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+    TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+    boot_test_util_verify_flash(&hdr1, 1, &hdr0, 0);
+    boot_test_util_verify_status_clear();
+
+    /* Ensure all subsequent boots use the main image. */
+    for (i = 0; i < 10; i++) {
+        rc = boot_go(&req, &rsp);
+        TEST_ASSERT(rc == 0);
+
+        TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
+        TEST_ASSERT(rsp.br_flash_id == boot_test_img_addrs[0].flash_id);
+        TEST_ASSERT(rsp.br_image_addr == boot_test_img_addrs[0].address);
+
+        boot_test_util_verify_flash(&hdr0, 0, &hdr1, 1);
+        boot_test_util_verify_status_clear();
+    }
+}
+
+TEST_CASE(boot_test_no_hash)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 0,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = 0,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc != 0);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_no_flag_has_hash)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = 0,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+    boot_test_util_write_hash(&hdr, 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc != 0);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_CASE(boot_test_invalid_hash)
+{
+    struct boot_rsp rsp;
+    int rc;
+
+    struct image_header hdr = {
+        .ih_magic = IMAGE_MAGIC,
+        .ih_tlv_size = 4 + 32,
+        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
+        .ih_img_size = 12 * 1024,
+        .ih_flags = IMAGE_F_SHA256,
+        .ih_ver = { 0, 2, 3, 4 },
+    };
+
+    struct boot_req req = {
+        .br_area_descs = boot_test_area_descs,
+        .br_slot_areas = boot_test_slot_areas,
+        .br_num_image_areas = BOOT_TEST_AREA_IDX_SCRATCH + 1,
+        .br_scratch_area_idx = BOOT_TEST_AREA_IDX_SCRATCH,
+    };
+
+    struct image_tlv tlv = {
+        .it_type = IMAGE_TLV_SHA256,
+        .it_len = 32
+    };
+    boot_test_util_init_flash();
+    boot_test_util_write_image(&hdr, 0);
+    rc = hal_flash_write(boot_test_img_addrs[0].flash_id,
+      boot_test_img_addrs[0].address + hdr.ih_hdr_size + hdr.ih_img_size,
+      &tlv, sizeof(tlv));
+    TEST_ASSERT(rc == 0);
+
+    rc = boot_go(&req, &rsp);
+    TEST_ASSERT(rc != 0);
+
+    boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
+    boot_test_util_verify_status_clear();
+}
+
+TEST_SUITE(boot_test_main)
+{
+    boot_test_setup();
+    boot_test_nv_ns_10();
+    boot_test_nv_ns_01();
+    boot_test_nv_ns_11();
+    boot_test_vm_ns_10();
+    boot_test_vm_ns_01();
+    boot_test_vm_ns_11_a();
+    boot_test_vm_ns_11_b();
+    boot_test_vm_ns_11_2areas();
+    boot_test_nv_bs_10();
+    boot_test_nv_bs_11();
+    boot_test_nv_bs_11_2areas();
+    boot_test_vb_ns_11();
+    boot_test_no_hash();
+    boot_test_no_flag_has_hash();
+    boot_test_invalid_hash();
+}
+
+int
+boot_test_all(void)
+{
+    boot_test_main();
+    return tu_any_failed;
+}
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(void)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    boot_test_all();
+
+    return tu_any_failed;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/console/full/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/console/full/pkg.yml b/libs/console/full/pkg.yml
index 57d6b6f..69e3ddb 100644
--- a/libs/console/full/pkg.yml
+++ b/libs/console/full/pkg.yml
@@ -28,4 +28,6 @@ pkg.deps:
     - libs/os
     - drivers/uart
 pkg.apis: console
-pkg.cflags.BASELIBC: -DBASELIBC_PRESENT
+
+pkg.init_function: console_pkg_init
+pkg.init_stage: 5

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/console/full/src/cons_fmt.c
----------------------------------------------------------------------
diff --git a/libs/console/full/src/cons_fmt.c b/libs/console/full/src/cons_fmt.c
index 10f90e2..2b64c01 100644
--- a/libs/console/full/src/cons_fmt.c
+++ b/libs/console/full/src/cons_fmt.c
@@ -18,12 +18,13 @@
  */
 #include <stdarg.h>
 #include <stdio.h>
-#include <console/console.h>
-#include <os/os_time.h>
+#include "syscfg/syscfg.h"
+#include "console/console.h"
+#include "os/os_time.h"
 
 #define CONS_OUTPUT_MAX_LINE	128
 
-#ifdef BASELIBC_PRESENT
+#if MYNEWT_PKG(LIBS_BASELIBC)
 size_t console_file_write(FILE *p, const char *str, size_t cnt);
 
 static const struct File_methods console_file_ops = {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/console/full/src/cons_tty.c
----------------------------------------------------------------------
diff --git a/libs/console/full/src/cons_tty.c b/libs/console/full/src/cons_tty.c
index e7ccfe8..49dfefc 100644
--- a/libs/console/full/src/cons_tty.c
+++ b/libs/console/full/src/cons_tty.c
@@ -18,9 +18,11 @@
  */
 
 #include <inttypes.h>
-#include <os/os.h>
-#include <uart/uart.h>
-#include <bsp/bsp.h>
+#include <assert.h>
+#include "sysinit/sysinit.h"
+#include "os/os.h"
+#include "uart/uart.h"
+#include "bsp/bsp.h"
 #include "console/console.h"
 
 /** Indicates whether the previous line of output was completed. */
@@ -402,3 +404,12 @@ console_init(console_rx_cb rx_cb)
 
     return 0;
 }
+
+void
+console_pkg_init(void)
+{
+    int rc;
+
+    rc = console_init(NULL);
+    SYSINIT_PANIC_ASSERT(rc == 0);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/crash_test/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/crash_test/pkg.yml b/libs/crash_test/pkg.yml
index 872c029..f823781 100644
--- a/libs/crash_test/pkg.yml
+++ b/libs/crash_test/pkg.yml
@@ -22,15 +22,19 @@ pkg.description: Generate different kinds of faults
 pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
 
-pkg.deps.SHELL:
+pkg.deps.CRASH_TEST_CLI:
     - libs/shell
-pkg.req_apis.SHELL:
+pkg.req_apis.CRASH_TEST_CLI:
     - console
-pkg.cflags.SHELL:
-    - -DSHELL_PRESENT
 
-pkg.deps.NEWTMGR:
+pkg.deps.CRASH_TEST_NEWTMGR:
     - libs/newtmgr
     - libs/json
-pkg.cflags.NEWTMGR:
-    - -DNEWTMGR_PRESENT
+
+pkg.syscfg_defs:
+    CRASH_TEST_CLI:
+        description: 'TBD'
+        value: 1
+    CRASH_TEST_NEWTMGR:
+        description: 'TBD'
+        value: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/crash_test/src/crash_cli.c
----------------------------------------------------------------------
diff --git a/libs/crash_test/src/crash_cli.c b/libs/crash_test/src/crash_cli.c
index db22e13..4d49bef 100644
--- a/libs/crash_test/src/crash_cli.c
+++ b/libs/crash_test/src/crash_cli.c
@@ -16,7 +16,10 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifdef SHELL_PRESENT
+
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(CRASH_TEST_CLI)
 #include <inttypes.h>
 #include <os/os.h>
 #include <console/console.h>
@@ -43,4 +46,4 @@ crash_cli_cmd(int argc, char **argv)
     return 0;
 }
 
-#endif /* SHELL_PRESENT */
+#endif /* MYNEWT_VAL(CRASH_TEST_CLI) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/crash_test/src/crash_nmgr.c
----------------------------------------------------------------------
diff --git a/libs/crash_test/src/crash_nmgr.c b/libs/crash_test/src/crash_nmgr.c
index e53fdf8..d9bf4b0 100644
--- a/libs/crash_test/src/crash_nmgr.c
+++ b/libs/crash_test/src/crash_nmgr.c
@@ -17,13 +17,15 @@
  * under the License.
  */
 
-#ifdef NEWTMGR_PRESENT
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(CRASH_TEST_NEWTMGR)
 
 #include <string.h>
 
-#include <newtmgr/newtmgr.h>
-#include <json/json.h>
-#include <console/console.h>
+#include "newtmgr/newtmgr.h"
+#include "json/json.h"
+#include "console/console.h"
 
 #include "crash_test/crash_test.h"
 #include "crash_test_priv.h"
@@ -70,4 +72,4 @@ crash_test_nmgr_write(struct nmgr_jbuf *njb)
     return 0;
 }
 
-#endif
+#endif /* MYNEWT_VAL(CRASH_TEST_NEWTMGR) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/crash_test/src/crash_test.c
----------------------------------------------------------------------
diff --git a/libs/crash_test/src/crash_test.c b/libs/crash_test/src/crash_test.c
index 91f1dbd..a6e2ea2 100644
--- a/libs/crash_test/src/crash_test.c
+++ b/libs/crash_test/src/crash_test.c
@@ -17,20 +17,22 @@
  * under the License.
  */
 #include <inttypes.h>
-#include <os/os.h>
-#include <console/console.h>
 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
 
+#include "syscfg/syscfg.h"
+#include "os/os.h"
+#include "console/console.h"
+
 #include "crash_test/crash_test.h"
 #include "crash_test_priv.h"
 
-#ifdef SHELL_PRESENT
-#include <shell/shell.h>
+#if MYNEWT_VAL(CRASH_TEST_CLI)
+#include "shell/shell.h"
 #endif
-#ifdef NEWTMGR_PRESENT
-#include <newtmgr/newtmgr.h>
+#if MYNEWT_VAL(CRASH_TEST_NEWTMGR)
+#include "newtmgr/newtmgr.h"
 #endif
 
 int
@@ -60,10 +62,10 @@ crash_device(char *how)
 int
 crash_test_init(void)
 {
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(CRASH_TEST_CLI)
     shell_cmd_register(&crash_cmd_struct);
 #endif
-#ifdef NEWTMGR_PRESENT
+#if MYNEWT_VAL(CRASH_TEST_NEWTMGR)
     nmgr_group_register(&crash_test_nmgr_group);
 #endif
     return 0;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/crash_test/src/crash_test_priv.h
----------------------------------------------------------------------
diff --git a/libs/crash_test/src/crash_test_priv.h b/libs/crash_test/src/crash_test_priv.h
index e4af708..09dc5a3 100644
--- a/libs/crash_test/src/crash_test_priv.h
+++ b/libs/crash_test/src/crash_test_priv.h
@@ -19,10 +19,10 @@
 #ifndef __CRASH_TEST_PRIV_H__
 #define __CRASH_TEST_PRIV_H__
 
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(CRASH_TEST_CLI)
 extern struct shell_cmd crash_cmd_struct;
 #endif
-#ifdef NEWTMGR_PRESENT
+#if MYNEWT_VAL(CRASH_TEST_NEWTMGR)
 extern struct nmgr_group crash_test_nmgr_group;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/elua/elua_base/include/elua_base/elua.h
----------------------------------------------------------------------
diff --git a/libs/elua/elua_base/include/elua_base/elua.h b/libs/elua/elua_base/include/elua_base/elua.h
index dae75a1..e1396e4 100644
--- a/libs/elua/elua_base/include/elua_base/elua.h
+++ b/libs/elua/elua_base/include/elua_base/elua.h
@@ -22,6 +22,6 @@
 
 int lua_main( int argc, char **argv );
 
-int lua_init(void);
+void lua_init(void);
 
 #endif /* __ELUA_BASE_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/elua/elua_base/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/elua/elua_base/pkg.yml b/libs/elua/elua_base/pkg.yml
index 5180159..9714507 100644
--- a/libs/elua/elua_base/pkg.yml
+++ b/libs/elua/elua_base/pkg.yml
@@ -28,11 +28,23 @@ pkg.keywords:
     - scripting
     - interpreter
 
-pkg.cflags: -DLUA_OPTIMIZE_MEMORY=2 -DLUA_CROSS_COMPILER -DLUA_USE_MKSTEMP -DLUA_NUMBER_INTEGRAL -DMYNEWT
+pkg.cflags:
+    - "-DLUA_OPTIMIZE_MEMORY=2"
+    - "-DLUA_CROSS_COMPILER"
+    - "-DLUA_USE_MKSTEMP"
+    - "-DLUA_NUMBER_INTEGRAL"
+    - "-DMYNEWT"
 pkg.req_apis:
     - console
 pkg.deps:
     - fs/fs
-pkg.deps.SHELL:
+pkg.deps.ELUA_CLI:
     - libs/shell
-pkg.cflags.SHELL: -DSHELL_PRESENT
+
+pkg.init_function: lua_init
+pkg.init_stage: 5
+
+pkg.syscfg_defs:
+    ELUA_CLI:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_LIBS_SHELL'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/elua/elua_base/src/lmynewt.c
----------------------------------------------------------------------
diff --git a/libs/elua/elua_base/src/lmynewt.c b/libs/elua/elua_base/src/lmynewt.c
index e7eda1e..bf40a2e 100644
--- a/libs/elua/elua_base/src/lmynewt.c
+++ b/libs/elua/elua_base/src/lmynewt.c
@@ -16,12 +16,16 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#include <shell/shell.h>
-#include <elua_base/elua.h>
+
+#include <assert.h>
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
+#include "shell/shell.h"
+#include "elua_base/elua.h"
 
 #ifdef MYNEWT
 
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(ELUA_CLI)
 static int lua_cmd(int argc, char **argv);
 
 static struct shell_cmd lua_shell_cmd = {
@@ -37,13 +41,16 @@ lua_cmd(int argc, char **argv)
 }
 #endif
 
-int
+void
 lua_init(void)
 {
-#ifdef SHELL_PRESENT
-    return shell_cmd_register(&lua_shell_cmd);
-#else
-    return 0;
+    int rc;
+
+    (void)rc;
+
+#if MYNEWT_VAL(ELUA_CLI)
+    rc = shell_cmd_register(&lua_shell_cmd);
+    SYSINIT_PANIC_ASSERT(rc == 0);
 #endif
 }
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/imgmgr/include/imgmgr/imgmgr.h
----------------------------------------------------------------------
diff --git a/libs/imgmgr/include/imgmgr/imgmgr.h b/libs/imgmgr/include/imgmgr/imgmgr.h
index 17c9e7f..b6299a0 100644
--- a/libs/imgmgr/include/imgmgr/imgmgr.h
+++ b/libs/imgmgr/include/imgmgr/imgmgr.h
@@ -35,7 +35,7 @@
 
 #define IMGMGR_HASH_LEN                 32
 
-int imgmgr_module_init(void);
+void imgmgr_module_init(void);
 
 struct image_version;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/imgmgr/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/imgmgr/pkg.yml b/libs/imgmgr/pkg.yml
index ed02f32..ff7812e 100644
--- a/libs/imgmgr/pkg.yml
+++ b/libs/imgmgr/pkg.yml
@@ -27,10 +27,20 @@ pkg.deps:
     - libs/newtmgr
     - libs/bootutil
     - libs/util
-pkg.deps.FS:
+
+pkg.deps.IMGMGR_FS:
     - fs/fs
-pkg.cflags.FS: -DFS_PRESENT
 
-pkg.deps.COREDUMP:
+pkg.deps.IMGMGR_COREDUMP:
     - sys/coredump
-pkg.cflags.COREDUMP: -DCOREDUMP_PRESENT
+
+pkg.init_function: imgmgr_module_init
+pkg.init_stage: 5
+
+pkg.syscfg_defs:
+    IMGMGR_FS:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_FS_FS'
+    IMGMGR_COREDUMP:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_SYS_COREDUMP'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/imgmgr/src/imgmgr.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr.c b/libs/imgmgr/src/imgmgr.c
index 5e8539a..0cb7bc4 100644
--- a/libs/imgmgr/src/imgmgr.c
+++ b/libs/imgmgr/src/imgmgr.c
@@ -21,13 +21,14 @@
 #include <limits.h>
 #include <assert.h>
 #include <string.h>
-#include <hal/hal_bsp.h>
-#include <hal/flash_map.h>
-#include <newtmgr/newtmgr.h>
-#include <json/json.h>
-#include <util/base64.h>
 
-#include <bootutil/image.h>
+#include "sysinit/sysinit.h"
+#include "hal/hal_bsp.h"
+#include "hal/flash_map.h"
+#include "newtmgr/newtmgr.h"
+#include "json/json.h"
+#include "util/base64.h"
+#include "bootutil/image.h"
 
 #include "imgmgr/imgmgr.h"
 #include "imgmgr_priv.h"
@@ -51,7 +52,7 @@ static const struct nmgr_handler imgr_nmgr_handlers[] = {
         .nh_write = imgr_boot_write
     },
     [IMGMGR_NMGR_OP_FILE] = {
-#ifdef FS_PRESENT
+#if MYNEWT_VAL(IMGMGR_FS)
         .nh_read = imgr_file_download,
         .nh_write = imgr_file_upload
 #else
@@ -68,7 +69,7 @@ static const struct nmgr_handler imgr_nmgr_handlers[] = {
         .nh_write = imgr_boot2_write
     },
     [IMGMGR_NMGR_OP_CORELIST] = {
-#ifdef COREDUMP_PRESENT
+#if MYNEWT_VAL(IMGMGR_COREDUMP)
         .nh_read = imgr_core_list,
         .nh_write = imgr_noop,
 #else
@@ -77,7 +78,7 @@ static const struct nmgr_handler imgr_nmgr_handlers[] = {
 #endif
     },
     [IMGMGR_NMGR_OP_CORELOAD] = {
-#ifdef COREDUMP_PRESENT
+#if MYNEWT_VAL(IMGMGR_COREDUMP)
         .nh_read = imgr_core_load,
         .nh_write = imgr_core_erase,
 #else
@@ -487,12 +488,11 @@ err:
     return 0;
 }
 
-int
+void
 imgmgr_module_init(void)
 {
     int rc;
 
     rc = nmgr_group_register(&imgr_nmgr_group);
-    assert(rc == 0);
-    return rc;
+    SYSINIT_PANIC_ASSERT(rc == 0);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/imgmgr/src/imgmgr_coredump.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_coredump.c b/libs/imgmgr/src/imgmgr_coredump.c
index 6f3543b..17c430d 100644
--- a/libs/imgmgr/src/imgmgr_coredump.c
+++ b/libs/imgmgr/src/imgmgr_coredump.c
@@ -16,14 +16,17 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifdef COREDUMP_PRESENT
+
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(IMGMGR_COREDUMP)
 
 #include <limits.h>
 
-#include <hal/flash_map.h>
-#include <newtmgr/newtmgr.h>
-#include <coredump/coredump.h>
-#include <util/base64.h>
+#include "hal/flash_map.h"
+#include "newtmgr/newtmgr.h"
+#include "coredump/coredump.h"
+#include "util/base64.h"
 
 #include "imgmgr/imgmgr.h"
 #include "imgmgr_priv.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/imgmgr/src/imgmgr_fs.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_fs.c b/libs/imgmgr/src/imgmgr_fs.c
index 0da77de..af4579d 100644
--- a/libs/imgmgr/src/imgmgr_fs.c
+++ b/libs/imgmgr/src/imgmgr_fs.c
@@ -16,21 +16,24 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifdef FS_PRESENT
-#include <os/os.h>
-#include <os/endian.h>
+
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(IMGMGR_FS)
 
 #include <limits.h>
 #include <assert.h>
 #include <string.h>
 #include <stdio.h>
 
-#include <newtmgr/newtmgr.h>
-#include <bootutil/image.h>
-#include <fs/fs.h>
-#include <json/json.h>
-#include <util/base64.h>
-#include <bsp/bsp.h>
+#include "os/os.h"
+#include "os/endian.h"
+#include "newtmgr/newtmgr.h"
+#include "bootutil/image.h"
+#include "fs/fs.h"
+#include "json/json.h"
+#include "util/base64.h"
+#include "bsp/bsp.h"
 
 #include "imgmgr/imgmgr.h"
 #include "imgmgr_priv.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/imgmgr/src/imgmgr_priv.h
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_priv.h b/libs/imgmgr/src/imgmgr_priv.h
index e8dcde3..0efd688 100644
--- a/libs/imgmgr/src/imgmgr_priv.h
+++ b/libs/imgmgr/src/imgmgr_priv.h
@@ -21,6 +21,7 @@
 #define __IMGMGR_PRIV_H_
 
 #include <stdint.h>
+#include "syscfg/syscfg.h"
 
 #define IMGMGR_MAX_IMGS		2
 
@@ -87,7 +88,7 @@ struct imgr_state {
         uint32_t off;
         uint32_t size;
         const struct flash_area *fa;
-#ifdef FS_PRESENT
+#if MYNEWT_VAL(IMGMGR_FS)
         struct fs_file *file;
 #endif
     } upload;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/inet_def_service/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/inet_def_service/pkg.yml b/libs/inet_def_service/pkg.yml
index 103b0d5..7fb9ff2 100644
--- a/libs/inet_def_service/pkg.yml
+++ b/libs/inet_def_service/pkg.yml
@@ -30,9 +30,6 @@ pkg.deps:
     - sys/mn_socket
     - libs/os
     - libs/util
-    - libs/testutil
 
 pkg.reqs:
     - console
-
-pkg.features:

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/json/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/json/pkg.yml b/libs/json/pkg.yml
index 3dc0be6..4944622 100644
--- a/libs/json/pkg.yml
+++ b/libs/json/pkg.yml
@@ -22,7 +22,5 @@ pkg.description: JSON encoding / decoding library.
 pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
-pkg.deps.TEST:
-   - libs/testutil
 
 pkg.cflags.float_user: -DFLOAT_SUPPORT

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/json/src/test/test_json.c
----------------------------------------------------------------------
diff --git a/libs/json/src/test/test_json.c b/libs/json/src/test/test_json.c
deleted file mode 100644
index 80e9abe..0000000
--- a/libs/json/src/test/test_json.c
+++ /dev/null
@@ -1,41 +0,0 @@
-/**
- * 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 <testutil/testutil.h>
-#include <test_json.h>
-
-
-TEST_SUITE(test_json_suite) {
-    test_json_simple_encode();
-    test_json_simple_decode();
-}
-
-#ifdef MYNEWT_SELFTEST
-
-int
-main(int argc, char **argv)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    test_json_suite();
-
-    return tu_any_failed;
-}
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/json/src/test/test_json.h
----------------------------------------------------------------------
diff --git a/libs/json/src/test/test_json.h b/libs/json/src/test/test_json.h
deleted file mode 100644
index ea5efcf..0000000
--- a/libs/json/src/test/test_json.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/**
- * 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 TEST_JSON_H
-#define TEST_JSON_H
-
-TEST_CASE_DECL(test_json_simple_encode);
-TEST_CASE_DECL(test_json_simple_decode);
-
-#endif /* TEST_JSON_H */
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/json/src/test/test_json_simple.c
----------------------------------------------------------------------
diff --git a/libs/json/src/test/test_json_simple.c b/libs/json/src/test/test_json_simple.c
deleted file mode 100644
index 55d50f4..0000000
--- a/libs/json/src/test/test_json_simple.c
+++ /dev/null
@@ -1,360 +0,0 @@
-/**
- * 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 <assert.h>
-#include <string.h>
-#include "testutil/testutil.h"
-#include "test_json.h"
-#include "json/json.h"
-
-static char *output = "{\"KeyBool\": true,\"KeyInt\": -1234,\"KeyUint\": 1353214,\"KeyString\": \"foobar\",\"KeyStringN\": \"foobarlong\",\"KeyIntArr\": [153,2532,-322]}";
-
-static char *output1 ="{\"KeyBoolArr\": [true, false], \"KeyUintArr\": [0, 65535, 4294967295, 8589934590, 3451257]}";
-static char *outputboolspace = "{\"KeyBoolArr\": [    true    ,    false,true         ]}";
-static char *outputboolempty = "{\"KeyBoolArr\": , \"KeyBoolArr\": [  ]}";
-
-static char bigbuf[512];
-static int buf_index;
-
-static int test_write(void *buf, char* data, int len) {
-    int i;
-    for(i = 0; i < len; i++) {
-        bigbuf[buf_index++] = data[i];
-    }
-    return len;
-}
-
-TEST_CASE(test_json_simple_encode){
-    struct json_encoder encoder;
-    struct json_value value;
-    int rc;
-
-    /* reset the state of the internal test */
-    buf_index = 0;
-    memset(&encoder, 0, sizeof(encoder));
-
-    encoder.je_write = test_write;
-    encoder.je_arg= NULL;
-
-    rc = json_encode_object_start(&encoder);
-    TEST_ASSERT(rc == 0);
-
-    JSON_VALUE_BOOL(&value, 1);
-    rc = json_encode_object_entry(&encoder, "KeyBool", &value);
-    TEST_ASSERT(rc == 0);
-
-    JSON_VALUE_INT(&value, -1234);
-    rc = json_encode_object_entry(&encoder, "KeyInt", &value);
-    TEST_ASSERT(rc == 0);
-
-    JSON_VALUE_UINT(&value, 1353214);
-    rc = json_encode_object_entry(&encoder, "KeyUint", &value);
-    TEST_ASSERT(rc == 0);
-
-    JSON_VALUE_STRING(&value, "foobar");
-    rc = json_encode_object_entry(&encoder, "KeyString", &value);
-    TEST_ASSERT(rc == 0);
-
-    /* we'll decode later differently */
-    JSON_VALUE_STRINGN(&value, "foobarlongstring", 10);
-    rc = json_encode_object_entry(&encoder, "KeyStringN", &value);
-    TEST_ASSERT(rc == 0);
-
-    rc = json_encode_array_name(&encoder, "KeyIntArr");
-    TEST_ASSERT(rc == 0);
-
-    rc = json_encode_array_start(&encoder);
-    TEST_ASSERT(rc == 0);
-
-    JSON_VALUE_INT(&value, 153);
-    rc = json_encode_array_value(&encoder, &value);
-    TEST_ASSERT(rc == 0);
-
-    JSON_VALUE_INT(&value, 2532);
-    rc = json_encode_array_value(&encoder, &value);
-    TEST_ASSERT(rc == 0);
-
-    JSON_VALUE_INT(&value, -322);
-    rc = json_encode_array_value(&encoder, &value);
-    TEST_ASSERT(rc == 0);
-
-    rc = json_encode_array_finish(&encoder);
-    TEST_ASSERT(rc == 0);
-
-    rc = json_encode_object_finish(&encoder);
-    TEST_ASSERT(rc == 0);
-
-    /* does it match what we expect it to */
-    rc = strcmp(bigbuf, output);
-    TEST_ASSERT(rc == 0);
-}
-
-
-/* a test structure to hold the json flat buffer and pass bytes
- * to the decoder */
-struct test_jbuf {
-    /* json_buffer must be first element in the structure */
-    struct json_buffer json_buf;
-    char * start_buf;
-    char * end_buf;
-    int current_position;
-};
-
-
-static char
-test_jbuf_read_next(struct json_buffer *jb) {
-    char c;
-    struct test_jbuf  *ptjb = (struct test_jbuf*) jb;
-
-    if((ptjb->start_buf + ptjb->current_position) <= ptjb->end_buf) {
-        c = *(ptjb->start_buf + ptjb->current_position);
-        ptjb->current_position++;
-        return c;
-    }
-    return '\0';
-}
-
-/* this goes backward in the buffer one character */
-static char
-test_jbuf_read_prev(struct json_buffer *jb) {
-    char c;
-    struct test_jbuf  *ptjb = (struct test_jbuf*) jb;
-    if(ptjb->current_position) {
-       ptjb->current_position--;
-       c = *(ptjb->start_buf + ptjb->current_position);
-       return c;
-    }
-
-    /* can't rewind */
-    return '\0';
-
-}
-
-static int
-test_jbuf_readn(struct json_buffer *jb, char *buf, int size) {
-    struct test_jbuf  *ptjb = (struct test_jbuf*) jb;
-
-    int remlen;
-
-    remlen = ptjb->end_buf - (ptjb->start_buf + ptjb->current_position);
-    if (size > remlen) {
-        size = remlen;
-    }
-
-    memcpy(buf, ptjb->start_buf + ptjb->current_position, size);
-    ptjb->current_position += size;
-    return size;
-}
-
-static void
-test_buf_init(struct test_jbuf *ptjb, char *string) {
-    /* initialize the decode */
-    ptjb->json_buf.jb_read_next = test_jbuf_read_next;
-    ptjb->json_buf.jb_read_prev = test_jbuf_read_prev;
-    ptjb->json_buf.jb_readn = test_jbuf_readn;
-    ptjb->start_buf = string;
-    ptjb->end_buf = string + strlen(string);
-    /* end buf points to the NULL */
-    ptjb->current_position = 0;
-}
-
-/* now test the decode on a string */
-TEST_CASE(test_json_simple_decode){
-    struct test_jbuf tjb;
-    struct test_jbuf tjb1;
-    struct test_jbuf tjbboolspacearr;
-    struct test_jbuf tjbboolemptyarr;
-    long long unsigned int uint_val;
-    long long int int_val;
-    bool bool_val;
-    char string1[16];
-    char string2[16];
-    long long int intarr[8];
-    int rc;
-    int rc1;
-    int rcbsa;
-    int array_count;
-    int array_countemp;
-    bool boolarr[2];
-    unsigned long long uintarr[5];
-    int array_count1;
-    int array_count1u;
-    bool boolspacearr[3];
-    bool boolemptyarr[2];
-    
-    struct json_attr_t test_attr[7] = {
-        [0] = {
-            .attribute = "KeyBool",
-            .type = t_boolean,
-            .addr.boolean = &bool_val,
-            .nodefault = true
-        },
-        [1] = {
-            .attribute = "KeyInt",
-            .type = t_integer,
-            .addr.integer = &int_val,
-            .nodefault = true
-            },
-        [2] = {
-            .attribute = "KeyUint",
-            .type = t_uinteger,
-            .addr.uinteger = &uint_val,
-            .nodefault = true
-            },
-        [3] = {
-            .attribute = "KeyString",
-            .type = t_string,
-            .addr.string = string1,
-            .nodefault = true,
-            .len = sizeof(string1)
-            },
-        [4] = {
-            .attribute = "KeyStringN",
-            .type = t_string,
-            .addr.string = string2,
-            .nodefault = true,
-            .len = sizeof(string2)
-        },
-        [5] = {
-            .attribute = "KeyIntArr",
-            .type = t_array,
-            .addr.array = {
-                .element_type = t_integer,
-                .arr.integers.store = intarr,
-                .maxlen = sizeof intarr / sizeof intarr[0],
-                .count = &array_count,
-            },
-            .nodefault = true,
-            .len = sizeof(intarr)
-        },
-        [6] = {
-            .attribute = NULL
-        }
-    };
-    
-    test_buf_init(&tjb, output);
-
-    rc = json_read_object(&tjb.json_buf, test_attr);
-    TEST_ASSERT(rc==0);
-    TEST_ASSERT(bool_val == 1);
-    TEST_ASSERT(int_val ==  -1234);
-    TEST_ASSERT(uint_val == 1353214);
-
-    rc = memcmp(string1, "foobar", strlen("foobar"));
-    TEST_ASSERT(rc==0);
-
-    rc = memcmp(string2, "foobarlongstring", 10);
-    TEST_ASSERT(rc==0);
-
-    TEST_ASSERT(array_count == 3);
-    TEST_ASSERT(intarr[0] == 153);
-    TEST_ASSERT(intarr[1] == 2532);
-    TEST_ASSERT(intarr[2] == -322);
-
-   /*testing for the boolean*/
-   struct json_attr_t test_attr1[2] = {
-       [0] = {
-           .attribute = "KeyBoolArr",
-           .type = t_array,
-           .addr.array = {
-               .element_type = t_boolean,
-               .arr.booleans.store = boolarr,
-               .maxlen = sizeof boolarr / sizeof boolarr[0],
-               .count =&array_count1,
-           },
-           .nodefault = true,
-           .len = sizeof( boolarr),
-       },
-
-       [1] = {
-           .attribute = "KeyUintArr",
-           .type = t_array,
-           .addr.array = {
-               .element_type = t_uinteger,
-               .arr.uintegers.store = uintarr,
-               .maxlen = sizeof uintarr / sizeof uintarr[0],
-               .count =&array_count1u,
-           },
-           .nodefault = true,
-           .len = sizeof( uintarr),
-       }
-   };
-   
-   test_buf_init(&tjb1, output1);
-
-   rc1 = json_read_object(&tjb1.json_buf, test_attr1);
-   TEST_ASSERT(rc1==0);
-
-   TEST_ASSERT(boolarr[0] == true);
-   TEST_ASSERT(boolarr[1] == false);
-
-   TEST_ASSERT(uintarr[0] == 0);
-   TEST_ASSERT(uintarr[1] == 65535);
-   TEST_ASSERT(uintarr[2] == 4294967295);
-   TEST_ASSERT(uintarr[3] == 8589934590);
-   TEST_ASSERT(uintarr[4] ==  3451257);
-
-    /*testing arrays with empty spaces within the elements*/
-    struct json_attr_t test_boolspacearr[2] = {
-       [0] = {    
-           .attribute = "KeyBoolArr",
-           .type = t_array,
-           .addr.array = {
-               .element_type = t_boolean,
-               .arr.booleans.store = boolspacearr,
-               .maxlen = sizeof boolspacearr / sizeof boolspacearr[0],
-               .count =&array_count1,
-           },
-           .nodefault = true,
-           .len = sizeof( boolspacearr),
-       }
-           
-    };
-    
-    test_buf_init(&tjbboolspacearr, outputboolspace);
-
-    rcbsa = json_read_object(&tjbboolspacearr.json_buf, test_boolspacearr);
-    TEST_ASSERT(rcbsa == 0);
-
-    TEST_ASSERT(boolspacearr[0] == true);
-    TEST_ASSERT(boolspacearr[1] == false);
-    TEST_ASSERT(boolspacearr[2] == true);
-
-    /*testing array with empty value*/
-    struct json_attr_t test_boolemptyarr[2] = {
-        [0] = {
-            .attribute = "KeyBoolArr",
-           .type = t_array,
-           .addr.array = {
-               .element_type = t_boolean,
-               .arr.booleans.store = boolemptyarr,
-               .maxlen = sizeof boolemptyarr / sizeof boolemptyarr[0],
-               .count =&array_countemp,
-           },
-           .nodefault = true,
-           .len = sizeof( boolemptyarr),
-        }
-    };
-   
-   test_buf_init(&tjbboolemptyarr, outputboolempty);
-
-    rcbsa = json_read_object(&tjbboolemptyarr.json_buf, test_boolemptyarr);
-    TEST_ASSERT(rcbsa == 6); 
-    
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/json/test/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/json/test/pkg.yml b/libs/json/test/pkg.yml
new file mode 100644
index 0000000..65f2de0
--- /dev/null
+++ b/libs/json/test/pkg.yml
@@ -0,0 +1,30 @@
+# 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.
+#
+pkg.name: libs/json/test
+pkg.type: unittest
+pkg.description: "JSON unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps: 
+    - libs/json
+    - libs/testutil
+
+pkg.deps.SELFTEST:
+    - libs/console/stub

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/json/test/src/test_json.c
----------------------------------------------------------------------
diff --git a/libs/json/test/src/test_json.c b/libs/json/test/src/test_json.c
new file mode 100644
index 0000000..dab3f06
--- /dev/null
+++ b/libs/json/test/src/test_json.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 "syscfg/syscfg.h"
+#include "testutil/testutil.h"
+#include "test_json.h"
+
+
+TEST_SUITE(test_json_suite) {
+    test_json_simple_encode();
+    test_json_simple_decode();
+}
+
+#if MYNEWT_VAL(SELFTEST)
+
+int
+main(int argc, char **argv)
+{
+    tu_config.tc_print_results = 1;
+    tu_init();
+
+    test_json_suite();
+
+    return tu_any_failed;
+}
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/json/test/src/test_json.h
----------------------------------------------------------------------
diff --git a/libs/json/test/src/test_json.h b/libs/json/test/src/test_json.h
new file mode 100644
index 0000000..ea5efcf
--- /dev/null
+++ b/libs/json/test/src/test_json.h
@@ -0,0 +1,27 @@
+/**
+ * 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 TEST_JSON_H
+#define TEST_JSON_H
+
+TEST_CASE_DECL(test_json_simple_encode);
+TEST_CASE_DECL(test_json_simple_decode);
+
+#endif /* TEST_JSON_H */
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/json/test/src/test_json_simple.c
----------------------------------------------------------------------
diff --git a/libs/json/test/src/test_json_simple.c b/libs/json/test/src/test_json_simple.c
new file mode 100644
index 0000000..55d50f4
--- /dev/null
+++ b/libs/json/test/src/test_json_simple.c
@@ -0,0 +1,360 @@
+/**
+ * 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 <assert.h>
+#include <string.h>
+#include "testutil/testutil.h"
+#include "test_json.h"
+#include "json/json.h"
+
+static char *output = "{\"KeyBool\": true,\"KeyInt\": -1234,\"KeyUint\": 1353214,\"KeyString\": \"foobar\",\"KeyStringN\": \"foobarlong\",\"KeyIntArr\": [153,2532,-322]}";
+
+static char *output1 ="{\"KeyBoolArr\": [true, false], \"KeyUintArr\": [0, 65535, 4294967295, 8589934590, 3451257]}";
+static char *outputboolspace = "{\"KeyBoolArr\": [    true    ,    false,true         ]}";
+static char *outputboolempty = "{\"KeyBoolArr\": , \"KeyBoolArr\": [  ]}";
+
+static char bigbuf[512];
+static int buf_index;
+
+static int test_write(void *buf, char* data, int len) {
+    int i;
+    for(i = 0; i < len; i++) {
+        bigbuf[buf_index++] = data[i];
+    }
+    return len;
+}
+
+TEST_CASE(test_json_simple_encode){
+    struct json_encoder encoder;
+    struct json_value value;
+    int rc;
+
+    /* reset the state of the internal test */
+    buf_index = 0;
+    memset(&encoder, 0, sizeof(encoder));
+
+    encoder.je_write = test_write;
+    encoder.je_arg= NULL;
+
+    rc = json_encode_object_start(&encoder);
+    TEST_ASSERT(rc == 0);
+
+    JSON_VALUE_BOOL(&value, 1);
+    rc = json_encode_object_entry(&encoder, "KeyBool", &value);
+    TEST_ASSERT(rc == 0);
+
+    JSON_VALUE_INT(&value, -1234);
+    rc = json_encode_object_entry(&encoder, "KeyInt", &value);
+    TEST_ASSERT(rc == 0);
+
+    JSON_VALUE_UINT(&value, 1353214);
+    rc = json_encode_object_entry(&encoder, "KeyUint", &value);
+    TEST_ASSERT(rc == 0);
+
+    JSON_VALUE_STRING(&value, "foobar");
+    rc = json_encode_object_entry(&encoder, "KeyString", &value);
+    TEST_ASSERT(rc == 0);
+
+    /* we'll decode later differently */
+    JSON_VALUE_STRINGN(&value, "foobarlongstring", 10);
+    rc = json_encode_object_entry(&encoder, "KeyStringN", &value);
+    TEST_ASSERT(rc == 0);
+
+    rc = json_encode_array_name(&encoder, "KeyIntArr");
+    TEST_ASSERT(rc == 0);
+
+    rc = json_encode_array_start(&encoder);
+    TEST_ASSERT(rc == 0);
+
+    JSON_VALUE_INT(&value, 153);
+    rc = json_encode_array_value(&encoder, &value);
+    TEST_ASSERT(rc == 0);
+
+    JSON_VALUE_INT(&value, 2532);
+    rc = json_encode_array_value(&encoder, &value);
+    TEST_ASSERT(rc == 0);
+
+    JSON_VALUE_INT(&value, -322);
+    rc = json_encode_array_value(&encoder, &value);
+    TEST_ASSERT(rc == 0);
+
+    rc = json_encode_array_finish(&encoder);
+    TEST_ASSERT(rc == 0);
+
+    rc = json_encode_object_finish(&encoder);
+    TEST_ASSERT(rc == 0);
+
+    /* does it match what we expect it to */
+    rc = strcmp(bigbuf, output);
+    TEST_ASSERT(rc == 0);
+}
+
+
+/* a test structure to hold the json flat buffer and pass bytes
+ * to the decoder */
+struct test_jbuf {
+    /* json_buffer must be first element in the structure */
+    struct json_buffer json_buf;
+    char * start_buf;
+    char * end_buf;
+    int current_position;
+};
+
+
+static char
+test_jbuf_read_next(struct json_buffer *jb) {
+    char c;
+    struct test_jbuf  *ptjb = (struct test_jbuf*) jb;
+
+    if((ptjb->start_buf + ptjb->current_position) <= ptjb->end_buf) {
+        c = *(ptjb->start_buf + ptjb->current_position);
+        ptjb->current_position++;
+        return c;
+    }
+    return '\0';
+}
+
+/* this goes backward in the buffer one character */
+static char
+test_jbuf_read_prev(struct json_buffer *jb) {
+    char c;
+    struct test_jbuf  *ptjb = (struct test_jbuf*) jb;
+    if(ptjb->current_position) {
+       ptjb->current_position--;
+       c = *(ptjb->start_buf + ptjb->current_position);
+       return c;
+    }
+
+    /* can't rewind */
+    return '\0';
+
+}
+
+static int
+test_jbuf_readn(struct json_buffer *jb, char *buf, int size) {
+    struct test_jbuf  *ptjb = (struct test_jbuf*) jb;
+
+    int remlen;
+
+    remlen = ptjb->end_buf - (ptjb->start_buf + ptjb->current_position);
+    if (size > remlen) {
+        size = remlen;
+    }
+
+    memcpy(buf, ptjb->start_buf + ptjb->current_position, size);
+    ptjb->current_position += size;
+    return size;
+}
+
+static void
+test_buf_init(struct test_jbuf *ptjb, char *string) {
+    /* initialize the decode */
+    ptjb->json_buf.jb_read_next = test_jbuf_read_next;
+    ptjb->json_buf.jb_read_prev = test_jbuf_read_prev;
+    ptjb->json_buf.jb_readn = test_jbuf_readn;
+    ptjb->start_buf = string;
+    ptjb->end_buf = string + strlen(string);
+    /* end buf points to the NULL */
+    ptjb->current_position = 0;
+}
+
+/* now test the decode on a string */
+TEST_CASE(test_json_simple_decode){
+    struct test_jbuf tjb;
+    struct test_jbuf tjb1;
+    struct test_jbuf tjbboolspacearr;
+    struct test_jbuf tjbboolemptyarr;
+    long long unsigned int uint_val;
+    long long int int_val;
+    bool bool_val;
+    char string1[16];
+    char string2[16];
+    long long int intarr[8];
+    int rc;
+    int rc1;
+    int rcbsa;
+    int array_count;
+    int array_countemp;
+    bool boolarr[2];
+    unsigned long long uintarr[5];
+    int array_count1;
+    int array_count1u;
+    bool boolspacearr[3];
+    bool boolemptyarr[2];
+    
+    struct json_attr_t test_attr[7] = {
+        [0] = {
+            .attribute = "KeyBool",
+            .type = t_boolean,
+            .addr.boolean = &bool_val,
+            .nodefault = true
+        },
+        [1] = {
+            .attribute = "KeyInt",
+            .type = t_integer,
+            .addr.integer = &int_val,
+            .nodefault = true
+            },
+        [2] = {
+            .attribute = "KeyUint",
+            .type = t_uinteger,
+            .addr.uinteger = &uint_val,
+            .nodefault = true
+            },
+        [3] = {
+            .attribute = "KeyString",
+            .type = t_string,
+            .addr.string = string1,
+            .nodefault = true,
+            .len = sizeof(string1)
+            },
+        [4] = {
+            .attribute = "KeyStringN",
+            .type = t_string,
+            .addr.string = string2,
+            .nodefault = true,
+            .len = sizeof(string2)
+        },
+        [5] = {
+            .attribute = "KeyIntArr",
+            .type = t_array,
+            .addr.array = {
+                .element_type = t_integer,
+                .arr.integers.store = intarr,
+                .maxlen = sizeof intarr / sizeof intarr[0],
+                .count = &array_count,
+            },
+            .nodefault = true,
+            .len = sizeof(intarr)
+        },
+        [6] = {
+            .attribute = NULL
+        }
+    };
+    
+    test_buf_init(&tjb, output);
+
+    rc = json_read_object(&tjb.json_buf, test_attr);
+    TEST_ASSERT(rc==0);
+    TEST_ASSERT(bool_val == 1);
+    TEST_ASSERT(int_val ==  -1234);
+    TEST_ASSERT(uint_val == 1353214);
+
+    rc = memcmp(string1, "foobar", strlen("foobar"));
+    TEST_ASSERT(rc==0);
+
+    rc = memcmp(string2, "foobarlongstring", 10);
+    TEST_ASSERT(rc==0);
+
+    TEST_ASSERT(array_count == 3);
+    TEST_ASSERT(intarr[0] == 153);
+    TEST_ASSERT(intarr[1] == 2532);
+    TEST_ASSERT(intarr[2] == -322);
+
+   /*testing for the boolean*/
+   struct json_attr_t test_attr1[2] = {
+       [0] = {
+           .attribute = "KeyBoolArr",
+           .type = t_array,
+           .addr.array = {
+               .element_type = t_boolean,
+               .arr.booleans.store = boolarr,
+               .maxlen = sizeof boolarr / sizeof boolarr[0],
+               .count =&array_count1,
+           },
+           .nodefault = true,
+           .len = sizeof( boolarr),
+       },
+
+       [1] = {
+           .attribute = "KeyUintArr",
+           .type = t_array,
+           .addr.array = {
+               .element_type = t_uinteger,
+               .arr.uintegers.store = uintarr,
+               .maxlen = sizeof uintarr / sizeof uintarr[0],
+               .count =&array_count1u,
+           },
+           .nodefault = true,
+           .len = sizeof( uintarr),
+       }
+   };
+   
+   test_buf_init(&tjb1, output1);
+
+   rc1 = json_read_object(&tjb1.json_buf, test_attr1);
+   TEST_ASSERT(rc1==0);
+
+   TEST_ASSERT(boolarr[0] == true);
+   TEST_ASSERT(boolarr[1] == false);
+
+   TEST_ASSERT(uintarr[0] == 0);
+   TEST_ASSERT(uintarr[1] == 65535);
+   TEST_ASSERT(uintarr[2] == 4294967295);
+   TEST_ASSERT(uintarr[3] == 8589934590);
+   TEST_ASSERT(uintarr[4] ==  3451257);
+
+    /*testing arrays with empty spaces within the elements*/
+    struct json_attr_t test_boolspacearr[2] = {
+       [0] = {    
+           .attribute = "KeyBoolArr",
+           .type = t_array,
+           .addr.array = {
+               .element_type = t_boolean,
+               .arr.booleans.store = boolspacearr,
+               .maxlen = sizeof boolspacearr / sizeof boolspacearr[0],
+               .count =&array_count1,
+           },
+           .nodefault = true,
+           .len = sizeof( boolspacearr),
+       }
+           
+    };
+    
+    test_buf_init(&tjbboolspacearr, outputboolspace);
+
+    rcbsa = json_read_object(&tjbboolspacearr.json_buf, test_boolspacearr);
+    TEST_ASSERT(rcbsa == 0);
+
+    TEST_ASSERT(boolspacearr[0] == true);
+    TEST_ASSERT(boolspacearr[1] == false);
+    TEST_ASSERT(boolspacearr[2] == true);
+
+    /*testing array with empty value*/
+    struct json_attr_t test_boolemptyarr[2] = {
+        [0] = {
+            .attribute = "KeyBoolArr",
+           .type = t_array,
+           .addr.array = {
+               .element_type = t_boolean,
+               .arr.booleans.store = boolemptyarr,
+               .maxlen = sizeof boolemptyarr / sizeof boolemptyarr[0],
+               .count =&array_countemp,
+           },
+           .nodefault = true,
+           .len = sizeof( boolemptyarr),
+        }
+    };
+   
+   test_buf_init(&tjbboolemptyarr, outputboolempty);
+
+    rcbsa = json_read_object(&tjbboolemptyarr.json_buf, test_boolemptyarr);
+    TEST_ASSERT(rcbsa == 6); 
+    
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/mbedtls/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/mbedtls/pkg.yml b/libs/mbedtls/pkg.yml
index 6d364b6..76a1a7c 100644
--- a/libs/mbedtls/pkg.yml
+++ b/libs/mbedtls/pkg.yml
@@ -25,7 +25,5 @@ pkg.keywords:
     - ssl
     - tls
 
-pkg.cflags: -DMBEDTLS_USER_CONFIG_FILE=\"mbedtls/config_mynewt.h\"
-pkg.deps:
-    - libs/testutil
+pkg.cflags: '-DMBEDTLS_USER_CONFIG_FILE=\"mbedtls/config_mynewt.h\"'
 pkg.cflags.TEST: -DTEST

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/mbedtls/src/test/mbedtls_test.c
----------------------------------------------------------------------
diff --git a/libs/mbedtls/src/test/mbedtls_test.c b/libs/mbedtls/src/test/mbedtls_test.c
deleted file mode 100644
index dee9040..0000000
--- a/libs/mbedtls/src/test/mbedtls_test.c
+++ /dev/null
@@ -1,228 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <string.h>
-
-#include <testutil/testutil.h>
-
-#include "mbedtls/mbedtls_test.h"
-
-#include "mbedtls/sha1.h"
-#include "mbedtls/sha256.h"
-#include "mbedtls/sha512.h"
-#include "mbedtls/aes.h"
-#include "mbedtls/arc4.h"
-#include "mbedtls/bignum.h"
-#include "mbedtls/ccm.h"
-#include "mbedtls/dhm.h"
-#include "mbedtls/ecjpake.h"
-#include "mbedtls/ecp.h"
-#include "mbedtls/entropy.h"
-#include "mbedtls/gcm.h"
-#include "mbedtls/hmac_drbg.h"
-#include "mbedtls/md5.h"
-#include "mbedtls/pkcs5.h"
-#include "mbedtls/ripemd160.h"
-#include "mbedtls/rsa.h"
-#include "mbedtls/x509.h"
-#include "mbedtls/xtea.h"
-
-TEST_CASE(sha1_test)
-{
-    int rc;
-
-    rc = mbedtls_sha1_self_test(0);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(sha256_test)
-{
-    int rc;
-
-    rc = mbedtls_sha256_self_test(0);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(sha512_test)
-{
-    int rc;
-
-    rc = mbedtls_sha512_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(aes_test)
-{
-    int rc;
-
-    rc = mbedtls_aes_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(arc4_test)
-{
-    int rc;
-
-    rc = mbedtls_arc4_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(bignum_test)
-{
-    int rc;
-
-    rc = mbedtls_mpi_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ccm_test)
-{
-    int rc;
-
-    rc = mbedtls_ccm_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(dhm_test)
-{
-    int rc;
-
-    rc = mbedtls_dhm_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ecp_test)
-{
-    int rc;
-
-    rc = mbedtls_ecp_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(entropy_test)
-{
-#if 0 /* XXX fix this later, no strong entropy source atm */
-    int rc;
-
-    rc = mbedtls_entropy_self_test(1);
-    TEST_ASSERT(rc == 0);
-#endif
-}
-
-TEST_CASE(gcm_test)
-{
-    int rc;
-
-    rc = mbedtls_gcm_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(hmac_drbg_test)
-{
-    int rc;
-
-    rc = mbedtls_hmac_drbg_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(md5_test)
-{
-    int rc;
-
-    rc = mbedtls_md5_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(pkcs5_test)
-{
-    int rc;
-
-    rc = mbedtls_pkcs5_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(ripemd160_test)
-{
-    int rc;
-
-    rc = mbedtls_ripemd160_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(rsa_test)
-{
-    int rc;
-
-    rc = mbedtls_rsa_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(x509_test)
-{
-    int rc;
-
-    rc = mbedtls_x509_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(xtea_test)
-{
-    int rc;
-
-    rc = mbedtls_xtea_self_test(1);
-    TEST_ASSERT(rc == 0);
-}
-
-
-TEST_SUITE(mbedtls_test_all)
-{
-    sha1_test();
-    sha256_test();
-    sha512_test();
-    aes_test();
-    arc4_test();
-    bignum_test();
-    ccm_test();
-    dhm_test();
-    ecp_test();
-    entropy_test();
-    gcm_test();
-    hmac_drbg_test();
-    md5_test();
-    pkcs5_test();
-    ripemd160_test();
-    rsa_test();
-    x509_test();
-    xtea_test();
-}
-
-#ifdef MYNEWT_SELFTEST
-int
-main(int argc, char **argv)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    mbedtls_test_all();
-
-    return tu_any_failed;
-}
-
-#endif
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/libs/mbedtls/test/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/mbedtls/test/pkg.yml b/libs/mbedtls/test/pkg.yml
new file mode 100644
index 0000000..fd318eb
--- /dev/null
+++ b/libs/mbedtls/test/pkg.yml
@@ -0,0 +1,30 @@
+# 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.
+#
+pkg.name: libs/mbedtls/test
+pkg.type: unittest
+pkg.description: "mbedtls unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps: 
+    - libs/mbedtls
+    - libs/testutil
+
+pkg.deps.SELFTEST:
+    - libs/console/stub


[21/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_gatt_read_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_read_test.c b/net/nimble/host/src/test/ble_gatt_read_test.c
deleted file mode 100644
index 822de5c..0000000
--- a/net/nimble/host/src/test/ble_gatt_read_test.c
+++ /dev/null
@@ -1,823 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include <limits.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "host/ble_hs_test.h"
-#include "host/ble_uuid.h"
-#include "ble_hs_test_util.h"
-
-struct ble_gatt_read_test_attr {
-    uint16_t conn_handle;
-    uint16_t handle;
-    uint8_t value_len;
-    uint8_t value[BLE_ATT_ATTR_MAX_LEN];
-};
-
-#define BLE_GATT_READ_TEST_MAX_ATTRS    256
-
-struct ble_gatt_read_test_attr
-    ble_gatt_read_test_attrs[BLE_GATT_READ_TEST_MAX_ATTRS];
-int ble_gatt_read_test_num_attrs;
-int ble_gatt_read_test_complete;
-
-uint16_t ble_gatt_read_test_bad_conn_handle;
-int ble_gatt_read_test_bad_status;
-
-static void
-ble_gatt_read_test_misc_init(void)
-{
-    ble_hs_test_util_init();
-    ble_gatt_read_test_num_attrs = 0;
-    ble_gatt_read_test_complete = 0;
-    ble_gatt_read_test_bad_conn_handle = 0;
-    ble_gatt_read_test_bad_status = 0;
-
-    memset(&ble_gatt_read_test_attrs[0], 0,
-           sizeof ble_gatt_read_test_attrs[0]);
-}
-
-static int
-ble_gatt_read_test_cb(uint16_t conn_handle, const struct ble_gatt_error *error,
-                      struct ble_gatt_attr *attr, void *arg)
-{
-    struct ble_gatt_read_test_attr *dst;
-    int *stop_after;
-
-    stop_after = arg;
-
-    TEST_ASSERT_FATAL(error != NULL);
-
-    if (error->status != 0) {
-        ble_gatt_read_test_bad_conn_handle = conn_handle;
-        ble_gatt_read_test_bad_status = error->status;
-        ble_gatt_read_test_complete = 1;
-        return 0;
-    }
-
-    if (attr == NULL) {
-        ble_gatt_read_test_complete = 1;
-        return 0;
-    }
-
-    TEST_ASSERT_FATAL(ble_gatt_read_test_num_attrs <
-                      BLE_GATT_READ_TEST_MAX_ATTRS);
-    dst = ble_gatt_read_test_attrs + ble_gatt_read_test_num_attrs++;
-
-    TEST_ASSERT_FATAL(OS_MBUF_PKTLEN(attr->om) <= sizeof dst->value);
-
-    dst->conn_handle = conn_handle;
-    dst->handle = attr->handle;
-    dst->value_len = OS_MBUF_PKTLEN(attr->om);
-    os_mbuf_copydata(attr->om, 0, OS_MBUF_PKTLEN(attr->om), dst->value);
-
-    if (stop_after != NULL && *stop_after > 0) {
-        (*stop_after)--;
-        if (*stop_after == 0) {
-            ble_gatt_read_test_complete = 1;
-            return 1;
-        }
-    } else {
-        ble_gatt_read_test_complete = 1;
-    }
-
-    return 0;
-}
-
-static int
-ble_gatt_read_test_long_cb(uint16_t conn_handle,
-                           const struct ble_gatt_error *error,
-                           struct ble_gatt_attr *attr, void *arg)
-{
-    struct ble_gatt_read_test_attr *dst;
-    int *reads_left;
-
-    reads_left = arg;
-
-    TEST_ASSERT_FATAL(error != NULL);
-
-    if (error->status != 0) {
-        ble_gatt_read_test_bad_conn_handle = conn_handle;
-        ble_gatt_read_test_bad_status = error->status;
-        ble_gatt_read_test_complete = 1;
-        return 0;
-    }
-
-    if (attr == NULL) {
-        ble_gatt_read_test_complete = 1;
-        return 0;
-    }
-
-    dst = ble_gatt_read_test_attrs + 0;
-
-    TEST_ASSERT_FATAL(OS_MBUF_PKTLEN(attr->om) <=
-        dst->value_len + sizeof dst->value);
-    TEST_ASSERT(attr->offset == dst->value_len);
-
-    if (attr->offset == 0) {
-        dst->conn_handle = conn_handle;
-        dst->handle = attr->handle;
-    } else {
-        TEST_ASSERT(conn_handle == dst->conn_handle);
-        TEST_ASSERT(attr->handle == dst->handle);
-    }
-    os_mbuf_copydata(attr->om, 0, OS_MBUF_PKTLEN(attr->om),
-                     dst->value + dst->value_len);
-    dst->value_len += OS_MBUF_PKTLEN(attr->om);
-
-    if (reads_left != NULL && *reads_left > 0) {
-        (*reads_left)--;
-        if (*reads_left == 0) {
-            ble_gatt_read_test_complete = 1;
-            return 1;
-        }
-    }
-
-    return 0;
-}
-
-static void
-ble_gatt_read_test_misc_rx_rsp_good_raw(uint16_t conn_handle,
-                                        uint8_t att_op,
-                                        const void *data, int data_len)
-{
-    uint8_t buf[1024];
-    int rc;
-
-    TEST_ASSERT_FATAL(data_len <= sizeof buf);
-
-    /* Send the pending ATT Read Request. */
-    ble_hs_test_util_tx_all();
-
-    buf[0] = att_op;
-    memcpy(buf + 1, data, data_len);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, 1 + data_len);
-    TEST_ASSERT(rc == 0);
-}
-
-static void
-ble_gatt_read_test_misc_rx_rsp_good(uint16_t conn_handle,
-                                    struct ble_hs_test_util_flat_attr *attr)
-{
-    ble_gatt_read_test_misc_rx_rsp_good_raw(conn_handle, BLE_ATT_OP_READ_RSP,
-                                            attr->value,
-                                            attr->value_len);
-}
-
-static void
-ble_gatt_read_test_misc_rx_rsp_bad(uint16_t conn_handle,
-                                   uint8_t att_error, uint16_t err_handle)
-{
-    /* Send the pending ATT Read Request. */
-    ble_hs_test_util_tx_all();
-
-    ble_hs_test_util_rx_att_err_rsp(conn_handle, BLE_ATT_OP_READ_REQ,
-                                    att_error, err_handle);
-}
-
-static int
-ble_gatt_read_test_misc_uuid_rx_rsp_good(
-    uint16_t conn_handle, struct ble_hs_test_util_flat_attr *attrs)
-{
-    struct ble_att_read_type_rsp rsp;
-    uint8_t buf[1024];
-    int prev_len;
-    int off;
-    int rc;
-    int i;
-
-    if (ble_gatt_read_test_complete || attrs[0].handle == 0) {
-        return 0;
-    }
-
-    /* Send the pending ATT Read By Type Request. */
-    ble_hs_test_util_tx_all();
-
-    rsp.batp_length = 2 + attrs[0].value_len;
-    ble_att_read_type_rsp_write(buf, sizeof buf, &rsp);
-
-    prev_len = 0;
-    off = BLE_ATT_READ_TYPE_RSP_BASE_SZ;
-    for (i = 0; attrs[i].handle != 0; i++) {
-        if (prev_len != 0 && prev_len != attrs[i].value_len) {
-            break;
-        }
-        prev_len = attrs[i].value_len;
-
-        htole16(buf + off, attrs[i].handle);
-        off += 2;
-
-        memcpy(buf + off, attrs[i].value, attrs[i].value_len);
-        off += attrs[i].value_len;
-    }
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, off);
-    TEST_ASSERT(rc == 0);
-
-    return i;
-}
-
-static void
-ble_gatt_read_test_misc_verify_good(struct ble_hs_test_util_flat_attr *attr)
-{
-    int rc;
-
-    ble_gatt_read_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    rc = ble_gattc_read(2, attr->handle, ble_gatt_read_test_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_gatt_read_test_misc_rx_rsp_good(2, attr);
-
-    TEST_ASSERT(ble_gatt_read_test_num_attrs == 1);
-    TEST_ASSERT(ble_gatt_read_test_attrs[0].conn_handle == 2);
-    TEST_ASSERT(ble_gatt_read_test_attrs[0].handle == attr->handle);
-    TEST_ASSERT(ble_gatt_read_test_attrs[0].value_len == attr->value_len);
-    TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[0].value, attr->value,
-                       attr->value_len) == 0);
-}
-
-static void
-ble_gatt_read_test_misc_verify_bad(uint8_t att_status,
-                                   struct ble_hs_test_util_flat_attr *attr)
-{
-    int rc;
-
-    ble_gatt_read_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    rc = ble_gattc_read(2, attr->handle, ble_gatt_read_test_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_gatt_read_test_misc_rx_rsp_bad(2, att_status, attr->handle);
-
-    TEST_ASSERT(ble_gatt_read_test_num_attrs == 0);
-    TEST_ASSERT(ble_gatt_read_test_bad_conn_handle == 2);
-    TEST_ASSERT(ble_gatt_read_test_bad_status ==
-                BLE_HS_ERR_ATT_BASE + att_status);
-    TEST_ASSERT(!ble_gattc_any_jobs());
-}
-
-static void
-ble_gatt_read_test_misc_uuid_verify_good(
-    uint16_t start_handle, uint16_t end_handle, void *uuid128,
-    int stop_after, struct ble_hs_test_util_flat_attr *attrs)
-{
-    int num_read;
-    int idx;
-    int rc;
-    int i;
-
-    ble_gatt_read_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    rc = ble_gattc_read_by_uuid(2, start_handle, end_handle, uuid128,
-                                ble_gatt_read_test_cb, &stop_after);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    idx = 0;
-    while (1) {
-        num_read = ble_gatt_read_test_misc_uuid_rx_rsp_good(2, attrs + idx);
-        if (num_read == 0) {
-            ble_hs_test_util_tx_all();
-            ble_hs_test_util_rx_att_err_rsp(2, BLE_ATT_OP_READ_TYPE_REQ,
-                                            BLE_ATT_ERR_ATTR_NOT_FOUND,
-                                            start_handle);
-            break;
-        }
-
-        idx += num_read;
-    }
-
-    TEST_ASSERT(ble_gatt_read_test_complete);
-    TEST_ASSERT(idx == ble_gatt_read_test_num_attrs);
-
-    for (i = 0; i < idx; i++) {
-        TEST_ASSERT(ble_gatt_read_test_attrs[i].conn_handle == 2);
-        TEST_ASSERT(ble_gatt_read_test_attrs[i].handle == attrs[i].handle);
-        TEST_ASSERT(ble_gatt_read_test_attrs[i].value_len ==
-                    attrs[i].value_len);
-        TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[i].value, attrs[i].value,
-                           attrs[i].value_len) == 0);
-    }
-    TEST_ASSERT(!ble_gattc_any_jobs());
-}
-
-static void
-ble_gatt_read_test_misc_long_verify_good(
-    int max_reads, struct ble_hs_test_util_flat_attr *attr)
-{
-    int reads_left;
-    int chunk_sz;
-    int rem_len;
-    int att_op;
-    int off;
-    int rc;
-
-    ble_gatt_read_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    if (max_reads == 0) {
-        max_reads = INT_MAX;
-    }
-    reads_left = max_reads;
-    rc = ble_gattc_read_long(2, attr->handle, ble_gatt_read_test_long_cb,
-                             &reads_left);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    off = 0;
-    rem_len = attr->value_len;
-    do {
-        if (rem_len > BLE_ATT_MTU_DFLT - 1) {
-            chunk_sz = BLE_ATT_MTU_DFLT - 1;
-        } else {
-            chunk_sz = rem_len;
-        }
-        if (off == 0) {
-            att_op = BLE_ATT_OP_READ_RSP;
-        } else {
-            att_op = BLE_ATT_OP_READ_BLOB_RSP;
-        }
-        ble_gatt_read_test_misc_rx_rsp_good_raw(2, att_op,
-                                                attr->value + off, chunk_sz);
-        rem_len -= chunk_sz;
-        off += chunk_sz;
-    } while (rem_len > 0 && reads_left > 0);
-
-    TEST_ASSERT(ble_gatt_read_test_complete);
-    TEST_ASSERT(!ble_gattc_any_jobs());
-    TEST_ASSERT(ble_gatt_read_test_attrs[0].conn_handle == 2);
-    TEST_ASSERT(ble_gatt_read_test_attrs[0].handle == attr->handle);
-    if (reads_left > 0) {
-        TEST_ASSERT(ble_gatt_read_test_attrs[0].value_len == attr->value_len);
-    }
-    TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[0].value, attr->value,
-                       ble_gatt_read_test_attrs[0].value_len) == 0);
-}
-
-static void
-ble_gatt_read_test_misc_long_verify_bad(
-    uint8_t att_status, struct ble_hs_test_util_flat_attr *attr)
-{
-    int rc;
-
-    ble_gatt_read_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    rc = ble_gattc_read_long(2, attr->handle,
-                             ble_gatt_read_test_long_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_gatt_read_test_misc_rx_rsp_bad(2, att_status, attr->handle);
-
-    TEST_ASSERT(ble_gatt_read_test_num_attrs == 0);
-    TEST_ASSERT(ble_gatt_read_test_bad_conn_handle == 2);
-    TEST_ASSERT(ble_gatt_read_test_bad_status ==
-                BLE_HS_ERR_ATT_BASE + att_status);
-    TEST_ASSERT(!ble_gattc_any_jobs());
-}
-
-static int
-ble_gatt_read_test_misc_extract_handles(
-    struct ble_hs_test_util_flat_attr *attrs, uint16_t *handles)
-{
-    int i;
-
-    for (i = 0; attrs[i].handle != 0; i++) {
-        handles[i] = attrs[i].handle;
-    }
-    return i;
-}
-
-static void
-ble_gatt_read_test_misc_mult_verify_good(
-    struct ble_hs_test_util_flat_attr *attrs)
-{
-    uint8_t expected_value[512];
-    uint16_t handles[256];
-    int num_attrs;
-    int chunk_sz;
-    int off;
-    int rc;
-    int i;
-
-    ble_gatt_read_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    num_attrs = ble_gatt_read_test_misc_extract_handles(attrs, handles);
-
-    off = 0;
-    for (i = 0; i < num_attrs; i++) {
-        if (attrs[i].value_len > BLE_ATT_MTU_DFLT - 1 - off) {
-            chunk_sz = BLE_ATT_MTU_DFLT - 1 - off;
-        } else {
-            chunk_sz = attrs[i].value_len;
-        }
-
-        if (chunk_sz > 0) {
-            memcpy(expected_value + off, attrs[i].value, chunk_sz);
-            off += chunk_sz;
-        }
-    }
-
-    rc = ble_gattc_read_mult(2, handles, num_attrs,
-                             ble_gatt_read_test_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_gatt_read_test_misc_rx_rsp_good_raw(2, BLE_ATT_OP_READ_MULT_RSP,
-                                            expected_value, off);
-
-    TEST_ASSERT(ble_gatt_read_test_complete);
-    TEST_ASSERT(!ble_gattc_any_jobs());
-    TEST_ASSERT(ble_gatt_read_test_attrs[0].conn_handle == 2);
-    TEST_ASSERT(ble_gatt_read_test_attrs[0].value_len == off);
-    TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[0].value, expected_value,
-                       off) == 0);
-}
-
-static void
-ble_gatt_read_test_misc_mult_verify_bad(
-    uint8_t att_status, uint16_t err_handle,
-    struct ble_hs_test_util_flat_attr *attrs)
-{
-    uint16_t handles[256];
-    int num_attrs;
-    int rc;
-
-    ble_gatt_read_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    num_attrs = ble_gatt_read_test_misc_extract_handles(attrs, handles);
-
-    rc = ble_gattc_read_mult(2, handles, num_attrs,
-                             ble_gatt_read_test_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_gatt_read_test_misc_rx_rsp_bad(2, att_status, err_handle);
-
-    TEST_ASSERT(ble_gatt_read_test_num_attrs == 0);
-    TEST_ASSERT(ble_gatt_read_test_bad_conn_handle == 2);
-    TEST_ASSERT(ble_gatt_read_test_bad_status ==
-                BLE_HS_ERR_ATT_BASE + att_status);
-    TEST_ASSERT(!ble_gattc_any_jobs());
-}
-
-TEST_CASE(ble_gatt_read_test_by_handle)
-{
-    /* Read a seven-byte attribute. */
-    ble_gatt_read_test_misc_verify_good(
-        (struct ble_hs_test_util_flat_attr[]) { {
-        .handle = 43,
-        .value = { 1,2,3,4,5,6,7 },
-        .value_len = 7
-    } });
-
-    /* Read a one-byte attribute. */
-    ble_gatt_read_test_misc_verify_good(
-        (struct ble_hs_test_util_flat_attr[]) { {
-        .handle = 0x5432,
-        .value = { 0xff },
-        .value_len = 1
-    } });
-
-    /* Read a 200-byte attribute. */
-    ble_gatt_read_test_misc_verify_good(
-        (struct ble_hs_test_util_flat_attr[]) { {
-        .handle = 815,
-        .value = { 0 },
-        .value_len = 200,
-    } });
-
-    /* Fail due to attribute not found. */
-    ble_gatt_read_test_misc_verify_bad(BLE_ATT_ERR_ATTR_NOT_FOUND,
-        (struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 719,
-            .value = { 1,2,3,4,5,6,7 },
-            .value_len = 7
-        } });
-
-    /* Fail due to invalid PDU. */
-    ble_gatt_read_test_misc_verify_bad(BLE_ATT_ERR_INVALID_PDU,
-        (struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 65,
-            .value = { 0xfa, 0x4c },
-            .value_len = 2
-        } });
-}
-
-TEST_CASE(ble_gatt_read_test_by_uuid)
-{
-    /* Read a single seven-byte attribute. */
-    ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 0,
-        (struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 43,
-            .value = { 1,2,3,4,5,6,7 },
-            .value_len = 7
-        }, {
-            0,
-        } });
-
-    /* Read two seven-byte attributes; one response. */
-    ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 0,
-        (struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 43,
-            .value = { 1,2,3,4,5,6,7 },
-            .value_len = 7
-        }, {
-            .handle = 44,
-            .value = { 2,3,4,5,6,7,8 },
-            .value_len = 7
-        }, {
-            0,
-        } });
-
-    /* Read two attributes; two responses. */
-    ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 0,
-        (struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 43,
-            .value = { 1,2,3,4,5,6,7 },
-            .value_len = 7
-        }, {
-            .handle = 44,
-            .value = { 2,3,4 },
-            .value_len = 3
-        }, {
-            0,
-        } });
-
-    /* Stop after three reads. */
-    ble_gatt_read_test_misc_uuid_verify_good(1, 100, BLE_UUID16(0x1234), 3,
-        (struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 43,
-            .value = { 1,2,3,4,5,6,7 },
-            .value_len = 7
-        }, {
-            .handle = 44,
-            .value = { 2,3,4 },
-            .value_len = 3
-        }, {
-            .handle = 45,
-            .value = { 2,3,4 },
-            .value_len = 3
-        }, {
-            .handle = 46,
-            .value = { 3,4,5,6 },
-            .value_len = 4
-        }, {
-            .handle = 47,
-            .value = { 2,3,4 },
-            .value_len = 3
-        }, {
-            0,
-        } });
-}
-
-TEST_CASE(ble_gatt_read_test_long)
-{
-    uint8_t data512[512];
-    int i;
-
-    for (i = 0; i < sizeof data512; i++) {
-        data512[i] = i;
-    }
-
-    /* Read a seven-byte attribute. */
-    ble_gatt_read_test_misc_long_verify_good(0,
-        (struct ble_hs_test_util_flat_attr[]) { {
-        .handle = 43,
-        .value = { 1,2,3,4,5,6,7 },
-        .value_len = 7
-    } });
-
-    /* Read a zero-byte attribute. */
-    ble_gatt_read_test_misc_long_verify_good(0,
-        (struct ble_hs_test_util_flat_attr[]) { {
-        .handle = 43,
-        .value = { 0 },
-        .value_len = 0
-    } });
-
-    /* Read a 60-byte attribute; three requests. */
-    ble_gatt_read_test_misc_long_verify_good(0,
-        (struct ble_hs_test_util_flat_attr[]) { {
-        .handle = 34,
-        .value = {
-            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
-            17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
-            33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
-            49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60
-        },
-        .value_len = 60
-    } });
-
-    /* Stop after two reads. */
-    ble_gatt_read_test_misc_long_verify_good(2,
-        (struct ble_hs_test_util_flat_attr[]) { {
-        .handle = 34,
-        .value = {
-            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
-            17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
-            33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
-            49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60
-        },
-        .value_len = 60
-    } });
-
-    /* Fail due to attribute not found. */
-    ble_gatt_read_test_misc_long_verify_bad(BLE_ATT_ERR_ATTR_NOT_FOUND,
-        (struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 719,
-            .value = { 1, 2, 3, 4, 5, 6, 7 }, 
-            .value_len = 7
-        } });
-}
-
-TEST_CASE(ble_gatt_read_test_mult)
-{
-    uint8_t data512[512];
-    int i;
-
-    for (i = 0; i < sizeof data512; i++) {
-        data512[i] = i;
-    }
-
-    /* Read one attribute. */
-    ble_gatt_read_test_misc_mult_verify_good(
-        (struct ble_hs_test_util_flat_attr[]) { {
-        .handle = 43,
-        .value = { 0, 1, 2, 3, 4, 5, 6, 7 },
-        .value_len = 7
-    }, {
-        0
-    } });
-
-    /* Read two attributes. */
-    ble_gatt_read_test_misc_mult_verify_good(
-        (struct ble_hs_test_util_flat_attr[]) { {
-        .handle = 43,
-        .value = { 0, 1, 2, 3, 4, 5, 6, 7 },
-        .value_len = 7,
-    }, {
-        .handle = 44,
-        .value = { 8, 9, 10, 11 },
-        .value_len = 4,
-    }, {
-        0
-    } });
-
-    /* Read two attributes (swap order). */
-    ble_gatt_read_test_misc_mult_verify_good(
-        (struct ble_hs_test_util_flat_attr[]) { {
-        .handle = 44,
-        .value = { 8, 9, 10, 11 },
-        .value_len = 4,
-    }, {
-        .handle = 43,
-        .value = { 0, 1, 2, 3, 4, 5, 6, 7 },
-        .value_len = 7,
-    }, {
-        0
-    } });
-
-    /* Read five attributes. */
-    ble_gatt_read_test_misc_mult_verify_good(
-        (struct ble_hs_test_util_flat_attr[]) { {
-        .handle = 43,
-        .value = { 0, 1, 2, 3, 4, 5, 6, 7 },
-        .value_len = 7,
-    }, {
-        .handle = 44,
-        .value = { 8, 9, 10, 11 },
-        .value_len = 4,
-    }, {
-        .handle = 145,
-        .value = { 12, 13 },
-        .value_len = 2,
-    }, {
-        .handle = 191,
-        .value = { 14, 15, 16 },
-        .value_len = 3,
-    }, {
-        .handle = 352,
-        .value = { 17, 18, 19, 20 },
-        .value_len = 4,
-    }, {
-        0
-    } });
-
-    /* Fail due to attribute not found. */
-    ble_gatt_read_test_misc_mult_verify_bad(BLE_ATT_ERR_ATTR_NOT_FOUND, 719,
-        (struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 719,
-            .value = { 1,2,3,4,5,6,7 },
-            .value_len = 7
-        }, {
-            0
-        } });
-}
-
-TEST_CASE(ble_gatt_read_test_concurrent)
-{
-    int rc;
-    int i;
-
-    ble_gatt_read_test_misc_init();
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    /***
-     * Perform three concurrent reads.  Assert that each response is correctly
-     * matched up with its corresponding GATT procedure.
-     */
-
-    struct ble_hs_test_util_flat_attr attrs[3] = {
-        {
-            .handle = 1,
-            .offset = 0,
-            .value_len = 3,
-            .value = { 1, 2, 3 },
-        },
-        {
-            .handle = 2,
-            .offset = 0,
-            .value_len = 4,
-            .value = { 2, 3, 4, 5 },
-        },
-        {
-            .handle = 3,
-            .offset = 0,
-            .value_len = 5,
-            .value = { 3, 4, 5, 6, 7 },
-        },
-    };
-
-    rc = ble_gattc_read(2, attrs[0].handle, ble_gatt_read_test_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-    rc = ble_gattc_read(2, attrs[1].handle, ble_gatt_read_test_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-    rc = ble_gattc_read(2, attrs[2].handle, ble_gatt_read_test_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    ble_gatt_read_test_misc_rx_rsp_good(2, attrs + 0);
-    ble_gatt_read_test_misc_rx_rsp_good(2, attrs + 1);
-    ble_gatt_read_test_misc_rx_rsp_good(2, attrs + 2);
-
-    TEST_ASSERT(ble_gatt_read_test_num_attrs == 3);
-
-    for (i = 0; i < 3; i++) {
-        TEST_ASSERT(ble_gatt_read_test_attrs[i].handle == attrs[i].handle);
-        TEST_ASSERT(ble_gatt_read_test_attrs[i].value_len ==
-                    attrs[i].value_len);
-        TEST_ASSERT(memcmp(ble_gatt_read_test_attrs[i].value, attrs[i].value,
-                           attrs[i].value_len) == 0);
-    }
-}
-
-TEST_SUITE(ble_gatt_read_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gatt_read_test_by_handle();
-    ble_gatt_read_test_by_uuid();
-    ble_gatt_read_test_long();
-    ble_gatt_read_test_mult();
-    ble_gatt_read_test_concurrent();
-}
-
-int
-ble_gatt_read_test_all(void)
-{
-    ble_gatt_read_test_suite();
-
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_gatt_write_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatt_write_test.c b/net/nimble/host/src/test/ble_gatt_write_test.c
deleted file mode 100644
index f548198..0000000
--- a/net/nimble/host/src/test/ble_gatt_write_test.c
+++ /dev/null
@@ -1,639 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "host/ble_hs_test.h"
-#include "host/ble_gatt.h"
-#include "host/ble_uuid.h"
-#include "ble_hs_test_util.h"
-
-#define BLE_GATT_WRITE_TEST_MAX_ATTRS   128
-
-static int ble_gatt_write_test_cb_called;
-
-static uint8_t ble_gatt_write_test_attr_value[BLE_ATT_ATTR_MAX_LEN];
-static struct ble_gatt_error ble_gatt_write_test_error;
-
-static struct ble_hs_test_util_flat_attr
-ble_gatt_write_test_attrs[BLE_GATT_WRITE_TEST_MAX_ATTRS];
-static int ble_gatt_write_test_num_attrs;
-
-static void
-ble_gatt_write_test_init(void)
-{
-    int i;
-
-    ble_hs_test_util_init();
-    ble_gatt_write_test_cb_called = 0;
-    ble_gatt_write_test_num_attrs = 0;
-
-    for (i = 0; i < sizeof ble_gatt_write_test_attr_value; i++) {
-        ble_gatt_write_test_attr_value[i] = i;
-    }
-}
-
-static int
-ble_gatt_write_test_cb_good(uint16_t conn_handle,
-                            const struct ble_gatt_error *error,
-                            struct ble_gatt_attr *attr, void *arg)
-{
-    int *attr_len;
-
-    attr_len = arg;
-
-    TEST_ASSERT(error != NULL);
-    TEST_ASSERT(conn_handle == 2);
-
-    ble_gatt_write_test_error = *error;
-
-    if (attr_len != NULL) {
-        TEST_ASSERT(error->status == 0);
-        TEST_ASSERT(attr->handle == 100);
-    }
-
-    ble_gatt_write_test_cb_called = 1;
-
-    return 0;
-}
-
-static void
-ble_gatt_write_test_rx_rsp(uint16_t conn_handle)
-{
-    uint8_t op;
-    int rc;
-
-    op = BLE_ATT_OP_WRITE_RSP;
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                &op, 1);
-    TEST_ASSERT(rc == 0);
-}
-
-static void
-ble_gatt_write_test_rx_prep_rsp(uint16_t conn_handle, uint16_t attr_handle,
-                                uint16_t offset,
-                                const void *attr_data, uint16_t attr_data_len)
-{
-    struct ble_att_prep_write_cmd rsp;
-    uint8_t buf[512];
-    int rc;
-
-    rsp.bapc_handle = attr_handle;
-    rsp.bapc_offset = offset;
-    ble_att_prep_write_rsp_write(buf, sizeof buf, &rsp);
-
-    memcpy(buf + BLE_ATT_PREP_WRITE_CMD_BASE_SZ, attr_data, attr_data_len);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(
-        conn_handle, BLE_L2CAP_CID_ATT, buf,
-        BLE_ATT_PREP_WRITE_CMD_BASE_SZ + attr_data_len);
-    TEST_ASSERT(rc == 0);
-}
-
-static void
-ble_gatt_write_test_rx_exec_rsp(uint16_t conn_handle)
-{
-    uint8_t op;
-    int rc;
-
-    op = BLE_ATT_OP_EXEC_WRITE_RSP;
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                &op, 1);
-    TEST_ASSERT(rc == 0);
-}
-
-static void
-ble_gatt_write_test_misc_long_good(int attr_len)
-{
-    uint16_t mtu;
-    int off;
-    int len;
-    int rc;
-
-    ble_gatt_write_test_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    mtu = ble_att_mtu(2);
-
-    rc = ble_hs_test_util_gatt_write_long_flat(
-        2, 100, ble_gatt_write_test_attr_value, attr_len,
-        ble_gatt_write_test_cb_good, &attr_len);
-    TEST_ASSERT(rc == 0);
-
-    off = 0;
-    while (off < attr_len) {
-        len = mtu - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
-        if (off + len > attr_len) {
-            len = attr_len - off;
-        }
-
-        /* Send the pending ATT Prep Write Command. */
-        ble_hs_test_util_verify_tx_prep_write(
-            100, off, ble_gatt_write_test_attr_value + off, len);
-
-        /* Receive Prep Write response. */
-        ble_gatt_write_test_rx_prep_rsp(
-            2, 100, off, ble_gatt_write_test_attr_value + off, len);
-
-        /* Verify callback hasn't gotten called. */
-        TEST_ASSERT(!ble_gatt_write_test_cb_called);
-
-        off += len;
-    }
-
-    /* Verify execute write request sent. */
-    ble_hs_test_util_verify_tx_exec_write(BLE_ATT_EXEC_WRITE_F_CONFIRM);
-
-    /* Receive Exec Write response. */
-    ble_hs_test_util_tx_all();
-    ble_gatt_write_test_rx_exec_rsp(2);
-
-    /* Verify callback got called. */
-    TEST_ASSERT(ble_gatt_write_test_cb_called);
-}
-
-typedef void ble_gatt_write_test_long_fail_fn(uint16_t conn_handle,
-                                              int off, int len);
-
-static void
-ble_gatt_write_test_misc_long_bad(int attr_len,
-                                  ble_gatt_write_test_long_fail_fn *cb)
-{
-    uint16_t mtu;
-    int fail_now;
-    int off;
-    int len;
-    int rc;
-
-    ble_gatt_write_test_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-    mtu = ble_att_mtu(2);
-
-    rc = ble_hs_test_util_gatt_write_long_flat(
-        2, 100, ble_gatt_write_test_attr_value, attr_len,
-        ble_gatt_write_test_cb_good, NULL);
-    TEST_ASSERT(rc == 0);
-
-    fail_now = 0;
-    off = 0;
-    while (off < attr_len) {
-        len = mtu - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
-        if (off + len > attr_len) {
-            len = attr_len - off;
-        }
-
-        /* Send the pending ATT Prep Write Command. */
-        ble_hs_test_util_verify_tx_prep_write(
-            100, off, ble_gatt_write_test_attr_value + off, len);
-
-        /* Receive Prep Write response. */
-        len = BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
-        if (off + len >= attr_len) {
-            len = attr_len - off;
-            fail_now = 1;
-        }
-        if (!fail_now) {
-            ble_gatt_write_test_rx_prep_rsp(
-                2, 100, off, ble_gatt_write_test_attr_value + off, len);
-        } else {
-            cb(2, off, len);
-            break;
-        }
-
-        /* Verify callback hasn't gotten called. */
-        TEST_ASSERT(!ble_gatt_write_test_cb_called);
-
-        off += len;
-    }
-
-    /* Verify callback was called. */
-    TEST_ASSERT(ble_gatt_write_test_cb_called);
-    TEST_ASSERT(ble_gatt_write_test_error.status == BLE_HS_EBADDATA);
-    TEST_ASSERT(ble_gatt_write_test_error.att_handle == 0);
-}
-
-static void
-ble_gatt_write_test_misc_long_fail_handle(uint16_t conn_handle,
-                                          int off, int len)
-{
-    ble_gatt_write_test_rx_prep_rsp(
-        conn_handle, 99, off, ble_gatt_write_test_attr_value + off,
-        len);
-}
-
-static void
-ble_gatt_write_test_misc_long_fail_offset(uint16_t conn_handle,
-                                          int off, int len)
-{
-    ble_gatt_write_test_rx_prep_rsp(
-        conn_handle, 100, off + 1, ble_gatt_write_test_attr_value + off,
-        len);
-}
-
-static void
-ble_gatt_write_test_misc_long_fail_value(uint16_t conn_handle,
-                                         int off, int len)
-{
-    ble_gatt_write_test_rx_prep_rsp(
-        conn_handle, 100, off, ble_gatt_write_test_attr_value + off + 1,
-        len);
-}
-
-static void
-ble_gatt_write_test_misc_long_fail_length(uint16_t conn_handle,
-                                          int off, int len)
-{
-    ble_gatt_write_test_rx_prep_rsp(
-        conn_handle, 100, off, ble_gatt_write_test_attr_value + off,
-        len - 1);
-}
-
-static int
-ble_gatt_write_test_reliable_cb_good(uint16_t conn_handle,
-                                     const struct ble_gatt_error *error,
-                                     struct ble_gatt_attr *attrs,
-                                     uint8_t num_attrs, void *arg)
-{
-    int i;
-
-    TEST_ASSERT_FATAL(num_attrs <= BLE_GATT_WRITE_TEST_MAX_ATTRS);
-
-    TEST_ASSERT(conn_handle == 2);
-
-    ble_gatt_write_test_num_attrs = num_attrs;
-    for (i = 0; i < num_attrs; i++) {
-        ble_hs_test_util_attr_to_flat(ble_gatt_write_test_attrs + i,
-                                      attrs + i);
-    }
-
-    ble_gatt_write_test_cb_called = 1;
-
-    return 0;
-}
-
-static void
-ble_gatt_write_test_misc_reliable_good(
-    struct ble_hs_test_util_flat_attr *flat_attrs)
-{
-    const struct ble_hs_test_util_flat_attr *attr;
-    struct ble_gatt_attr attrs[16];
-    uint16_t mtu;
-    int num_attrs;
-    int attr_idx;
-    int len;
-    int off;
-    int rc;
-    int i;
-
-    ble_gatt_write_test_init();
-
-    for (num_attrs = 0; flat_attrs[num_attrs].handle != 0; num_attrs++) {
-        TEST_ASSERT_FATAL(num_attrs < sizeof attrs / sizeof attrs[0]);
-        ble_hs_test_util_attr_from_flat(attrs + num_attrs,
-                                        flat_attrs + num_attrs);
-    }
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-    mtu = ble_att_mtu(2);
-
-    rc = ble_gattc_write_reliable(2, attrs, num_attrs,
-                                  ble_gatt_write_test_reliable_cb_good, NULL);
-    TEST_ASSERT(rc == 0);
-
-    attr_idx = 0;
-    off = 0;
-    while (attr_idx < num_attrs) {
-        attr = flat_attrs + attr_idx;
-
-        len = mtu - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
-        if (off + len > attr->value_len) {
-            len = attr->value_len - off;
-        }
-
-        /* Send the pending ATT Prep Write Command. */
-        ble_hs_test_util_verify_tx_prep_write(attr->handle, off,
-                                              attr->value + off, len);
-
-        /* Receive Prep Write response. */
-        ble_gatt_write_test_rx_prep_rsp(2, attr->handle, off,
-                                        attr->value + off, len);
-
-        /* Verify callback hasn't gotten called. */
-        TEST_ASSERT(!ble_gatt_write_test_cb_called);
-
-        off += len;
-        if (off >= attr->value_len) {
-            attr_idx++;
-            off = 0;
-        }
-    }
-
-    /* Verify execute write request sent. */
-    ble_hs_test_util_verify_tx_exec_write(BLE_ATT_EXEC_WRITE_F_CONFIRM);
-
-    /* Receive Exec Write response. */
-    ble_hs_test_util_tx_all();
-    ble_gatt_write_test_rx_exec_rsp(2);
-
-    /* Verify callback got called. */
-    TEST_ASSERT(ble_gatt_write_test_cb_called);
-    TEST_ASSERT(ble_gatt_write_test_num_attrs == num_attrs);
-    for (i = 0; i < num_attrs; i++) {
-        rc = ble_hs_test_util_flat_attr_cmp(
-            ble_gatt_write_test_attrs + i, flat_attrs + i);
-        TEST_ASSERT(rc == 0);
-    }
-}
-
-TEST_CASE(ble_gatt_write_test_no_rsp)
-{
-    int attr_len;
-    int rc;
-
-    ble_gatt_write_test_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    attr_len = 4;
-    rc = ble_hs_test_util_gatt_write_no_rsp_flat(
-        2, 100, ble_gatt_write_test_attr_value, attr_len);
-    TEST_ASSERT(rc == 0);
-
-    /* Send the pending ATT Write Command. */
-    ble_hs_test_util_tx_all();
-
-    /* No response expected; verify callback not called. */
-    TEST_ASSERT(!ble_gatt_write_test_cb_called);
-}
-
-TEST_CASE(ble_gatt_write_test_rsp)
-{
-    int attr_len;
-
-    ble_gatt_write_test_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    attr_len = 4;
-    ble_hs_test_util_gatt_write_flat(2, 100, ble_gatt_write_test_attr_value,
-                                     attr_len, ble_gatt_write_test_cb_good,
-                                     &attr_len);
-
-    /* Send the pending ATT Write Command. */
-    ble_hs_test_util_tx_all();
-
-    /* Response not received yet; verify callback not called. */
-    TEST_ASSERT(!ble_gatt_write_test_cb_called);
-
-    /* Receive write response. */
-    ble_gatt_write_test_rx_rsp(2);
-
-    /* Verify callback got called. */
-    TEST_ASSERT(ble_gatt_write_test_cb_called);
-}
-
-TEST_CASE(ble_gatt_write_test_long_good)
-{
-    /*** 1 prep write req/rsp. */
-    ble_gatt_write_test_misc_long_good(
-        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ);
-
-    /*** 2 prep write reqs/rsps. */
-    ble_gatt_write_test_misc_long_good(
-        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1);
-
-    /*** Maximum reqs/rsps. */
-    ble_gatt_write_test_misc_long_good(BLE_ATT_ATTR_MAX_LEN);
-}
-
-TEST_CASE(ble_gatt_write_test_long_bad_handle)
-{
-    /*** 1 prep write req/rsp. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
-        ble_gatt_write_test_misc_long_fail_handle);
-
-    /*** 2 prep write reqs/rsps. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1,
-        ble_gatt_write_test_misc_long_fail_handle);
-
-    /*** Maximum reqs/rsps. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_ATTR_MAX_LEN,
-        ble_gatt_write_test_misc_long_fail_handle);
-}
-
-TEST_CASE(ble_gatt_write_test_long_bad_offset)
-{
-    /*** 1 prep write req/rsp. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
-        ble_gatt_write_test_misc_long_fail_offset);
-
-    /*** 2 prep write reqs/rsps. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1,
-        ble_gatt_write_test_misc_long_fail_offset);
-
-    /*** Maximum reqs/rsps. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_ATTR_MAX_LEN,
-        ble_gatt_write_test_misc_long_fail_offset);
-}
-
-TEST_CASE(ble_gatt_write_test_long_bad_value)
-{
-    /*** 1 prep write req/rsp. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
-        ble_gatt_write_test_misc_long_fail_value);
-
-    /*** 2 prep write reqs/rsps. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1,
-        ble_gatt_write_test_misc_long_fail_value);
-
-    /*** Maximum reqs/rsps. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_ATTR_MAX_LEN,
-        ble_gatt_write_test_misc_long_fail_value);
-}
-
-TEST_CASE(ble_gatt_write_test_long_bad_length)
-{
-    /*** 1 prep write req/rsp. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ,
-        ble_gatt_write_test_misc_long_fail_length);
-
-    /*** 2 prep write reqs/rsps. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ + 1,
-        ble_gatt_write_test_misc_long_fail_length);
-
-    /*** Maximum reqs/rsps. */
-    ble_gatt_write_test_misc_long_bad(
-        BLE_ATT_ATTR_MAX_LEN,
-        ble_gatt_write_test_misc_long_fail_length);
-}
-
-TEST_CASE(ble_gatt_write_test_reliable_good)
-{
-    /*** 1 attribute. */
-    ble_gatt_write_test_misc_reliable_good(
-        ((struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 100,
-            .value_len = 2,
-            .value = { 1, 2 },
-        }, {
-            0
-        } }));
-
-    /*** 2 attributes. */
-    ble_gatt_write_test_misc_reliable_good(
-        ((struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 100,
-            .value_len = 2,
-            .value = { 1,2 },
-        }, {
-            .handle = 113,
-            .value_len = 6,
-            .value = { 5,6,7,8,9,10 },
-        }, {
-            0
-        } }));
-
-    /*** 3 attributes. */
-    ble_gatt_write_test_misc_reliable_good(
-        ((struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 100,
-            .value_len = 2,
-            .value = { 1,2 },
-        }, {
-            .handle = 113,
-            .value_len = 6,
-            .value = { 5,6,7,8,9,10 },
-        }, {
-            .handle = 144,
-            .value_len = 1,
-            .value = { 0xff },
-        }, {
-            0
-        } }));
-
-    /*** Long attributes. */
-    ble_gatt_write_test_misc_reliable_good(
-        ((struct ble_hs_test_util_flat_attr[]) { {
-            .handle = 100,
-            .value_len = 20,
-            .value = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 },
-        }, {
-            .handle = 144,
-            .value_len = 20,
-            .value = { 11,12,13,14,15,16,17,18,19,110,
-                       111,112,113,114,115,116,117,118,119,120 },
-        }, {
-            0
-        } }));
-}
-
-TEST_CASE(ble_gatt_write_test_long_queue_full)
-{
-    int off;
-    int len;
-    int rc;
-    int i;
-
-    ble_gatt_write_test_init();
-
-    ble_hs_test_util_create_conn(2, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 NULL, NULL);
-
-    rc = ble_hs_test_util_gatt_write_long_flat(
-        2, 100, ble_gatt_write_test_attr_value, 128,
-        ble_gatt_write_test_cb_good, NULL);
-    TEST_ASSERT(rc == 0);
-
-    off = 0;
-    for (i = 0; i < 2; i++) {
-        /* Verify prep write request was sent. */
-        ble_hs_test_util_tx_all();
-        TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() != NULL);
-
-        /* Receive Prep Write response. */
-        len = BLE_ATT_MTU_DFLT - BLE_ATT_PREP_WRITE_CMD_BASE_SZ;
-        ble_gatt_write_test_rx_prep_rsp(
-            2, 100, off, ble_gatt_write_test_attr_value + off, len);
-
-        /* Verify callback hasn't gotten called. */
-        TEST_ASSERT(!ble_gatt_write_test_cb_called);
-
-        off += len;
-    }
-
-    /* Verify prep write request was sent. */
-    ble_hs_test_util_tx_all();
-    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() != NULL);
-
-    /* Receive queue full error. */
-    ble_hs_test_util_rx_att_err_rsp(2, BLE_ATT_OP_PREP_WRITE_REQ,
-                                    BLE_ATT_ERR_PREPARE_QUEUE_FULL, 100);
-
-    /* Verify callback was called. */
-    TEST_ASSERT(ble_gatt_write_test_cb_called);
-    TEST_ASSERT(ble_gatt_write_test_error.status ==
-                BLE_HS_ATT_ERR(BLE_ATT_ERR_PREPARE_QUEUE_FULL));
-    TEST_ASSERT(ble_gatt_write_test_error.att_handle == 100);
-
-    /* Verify clear queue command got sent. */
-    ble_hs_test_util_verify_tx_exec_write(0);
-}
-
-TEST_SUITE(ble_gatt_write_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gatt_write_test_no_rsp();
-    ble_gatt_write_test_rsp();
-    ble_gatt_write_test_long_good();
-    ble_gatt_write_test_long_bad_handle();
-    ble_gatt_write_test_long_bad_offset();
-    ble_gatt_write_test_long_bad_value();
-    ble_gatt_write_test_long_bad_length();
-    ble_gatt_write_test_long_queue_full();
-    ble_gatt_write_test_reliable_good();
-}
-
-int
-ble_gatt_write_test_all(void)
-{
-    ble_gatt_write_test_suite();
-
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_gatts_notify_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatts_notify_test.c b/net/nimble/host/src/test/ble_gatts_notify_test.c
deleted file mode 100644
index 4516e66..0000000
--- a/net/nimble/host/src/test/ble_gatts_notify_test.c
+++ /dev/null
@@ -1,983 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "nimble/ble.h"
-#include "host/ble_uuid.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-#include "ble_hs_test_util_store.h"
-
-#define BLE_GATTS_NOTIFY_TEST_CHR_1_UUID    0x1111
-#define BLE_GATTS_NOTIFY_TEST_CHR_2_UUID    0x2222
-
-#define BLE_GATTS_NOTIFY_TEST_MAX_EVENTS    16
-
-static uint8_t ble_gatts_notify_test_peer_addr[6] = {2,3,4,5,6,7};
-
-static int
-ble_gatts_notify_test_misc_access(uint16_t conn_handle,
-                                  uint16_t attr_handle, 
-                                  struct ble_gatt_access_ctxt *ctxt,
-                                  void *arg);
-static void
-ble_gatts_notify_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt,
-                                  void *arg);
-
-static const struct ble_gatt_svc_def ble_gatts_notify_test_svcs[] = { {
-    .type = BLE_GATT_SVC_TYPE_PRIMARY,
-    .uuid128 = BLE_UUID16(0x1234),
-    .characteristics = (struct ble_gatt_chr_def[]) { {
-        .uuid128 = BLE_UUID16(BLE_GATTS_NOTIFY_TEST_CHR_1_UUID),
-        .access_cb = ble_gatts_notify_test_misc_access,
-        .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY |
-                 BLE_GATT_CHR_F_INDICATE,
-    }, {
-        .uuid128 = BLE_UUID16(BLE_GATTS_NOTIFY_TEST_CHR_2_UUID),
-        .access_cb = ble_gatts_notify_test_misc_access,
-        .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY |
-                 BLE_GATT_CHR_F_INDICATE,
-    }, {
-        0
-    } },
-}, {
-    0
-} };
-
-
-static uint16_t ble_gatts_notify_test_chr_1_def_handle;
-static uint8_t ble_gatts_notify_test_chr_1_val[1024];
-static int ble_gatts_notify_test_chr_1_len;
-static uint16_t ble_gatts_notify_test_chr_2_def_handle;
-static uint8_t ble_gatts_notify_test_chr_2_val[1024];
-static int ble_gatts_notify_test_chr_2_len;
-
-static struct ble_gap_event
-ble_gatts_notify_test_events[BLE_GATTS_NOTIFY_TEST_MAX_EVENTS];
-
-static int ble_gatts_notify_test_num_events;
-
-typedef int ble_store_write_fn(int obj_type, union ble_store_value *val);
-
-typedef int ble_store_delete_fn(int obj_type, union ble_store_key *key);
-
-static int
-ble_gatts_notify_test_util_gap_event(struct ble_gap_event *event, void *arg)
-{
-    switch (event->type) {
-    case BLE_GAP_EVENT_NOTIFY_TX:
-    case BLE_GAP_EVENT_SUBSCRIBE:
-        TEST_ASSERT_FATAL(ble_gatts_notify_test_num_events <
-                          BLE_GATTS_NOTIFY_TEST_MAX_EVENTS);
-
-        ble_gatts_notify_test_events[ble_gatts_notify_test_num_events++] =
-            *event;
-
-    default:
-        break;
-    }
-
-    return 0;
-}
-
-static uint16_t
-ble_gatts_notify_test_misc_read_notify(uint16_t conn_handle,
-                                       uint16_t chr_def_handle)
-{
-    struct ble_att_read_req req;
-    struct os_mbuf *om;
-    uint8_t buf[BLE_ATT_READ_REQ_SZ];
-    uint16_t flags;
-    int rc;
-
-    req.barq_handle = chr_def_handle + 2;
-    ble_att_read_req_write(buf, sizeof buf, &req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    TEST_ASSERT_FATAL(om != NULL);
-    TEST_ASSERT_FATAL(om->om_len == 3);
-    TEST_ASSERT_FATAL(om->om_data[0] == BLE_ATT_OP_READ_RSP);
-
-    flags = le16toh(om->om_data + 1);
-    return flags;
-}
-
-static void
-ble_gatts_notify_test_misc_enable_notify(uint16_t conn_handle,
-                                         uint16_t chr_def_handle,
-                                         uint16_t flags)
-{
-    struct ble_att_write_req req;
-    uint8_t buf[BLE_ATT_WRITE_REQ_BASE_SZ + 2];
-    int rc;
-
-    req.bawq_handle = chr_def_handle + 2;
-    ble_att_write_req_write(buf, sizeof buf, &req);
-
-    htole16(buf + BLE_ATT_WRITE_REQ_BASE_SZ, flags);
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-}
-
-static void
-ble_gatts_notify_test_util_next_event(struct ble_gap_event *event)
-{
-    TEST_ASSERT_FATAL(ble_gatts_notify_test_num_events > 0);
-
-    *event = *ble_gatts_notify_test_events;
-
-    ble_gatts_notify_test_num_events--;
-    if (ble_gatts_notify_test_num_events > 0) {
-        memmove(ble_gatts_notify_test_events + 0,
-                ble_gatts_notify_test_events + 1,
-                ble_gatts_notify_test_num_events * sizeof *event);
-    }
-}
-
-static void
-ble_gatts_notify_test_util_verify_sub_event(uint16_t conn_handle,
-                                            uint8_t attr_handle,
-                                            uint8_t reason,
-                                            uint8_t prevn, uint8_t curn,
-                                            uint8_t previ, uint8_t curi)
-{
-    struct ble_gap_event event;
-
-    ble_gatts_notify_test_util_next_event(&event);
-
-    TEST_ASSERT(event.type == BLE_GAP_EVENT_SUBSCRIBE);
-    TEST_ASSERT(event.subscribe.conn_handle == conn_handle);
-    TEST_ASSERT(event.subscribe.attr_handle == attr_handle);
-    TEST_ASSERT(event.subscribe.reason == reason);
-    TEST_ASSERT(event.subscribe.prev_notify == prevn);
-    TEST_ASSERT(event.subscribe.cur_notify == curn);
-    TEST_ASSERT(event.subscribe.prev_indicate == previ);
-    TEST_ASSERT(event.subscribe.cur_indicate == curi);
-}
-
-static void
-ble_gatts_notify_test_util_verify_tx_event(uint16_t conn_handle,
-                                           uint8_t attr_handle,
-                                           int status,
-                                           int indication)
-{
-    struct ble_gap_event event;
-
-    ble_gatts_notify_test_util_next_event(&event);
-
-    TEST_ASSERT(event.type == BLE_GAP_EVENT_NOTIFY_TX);
-    TEST_ASSERT(event.notify_tx.status == status);
-    TEST_ASSERT(event.notify_tx.conn_handle == conn_handle);
-    TEST_ASSERT(event.notify_tx.attr_handle == attr_handle);
-    TEST_ASSERT(event.notify_tx.indication == indication);
-}
-
-static void
-ble_gatts_notify_test_util_verify_ack_event(uint16_t conn_handle,
-                                            uint8_t attr_handle)
-{
-    ble_gatts_notify_test_util_verify_tx_event(conn_handle, attr_handle,
-                                               BLE_HS_EDONE, 1);
-}
-
-static void
-ble_gatts_notify_test_misc_init(uint16_t *out_conn_handle, int bonding,
-                                uint16_t chr1_flags, uint16_t chr2_flags)
-{
-    struct ble_hs_conn *conn;
-    uint16_t flags;
-    int exp_num_cccds;
-    int rc;
-
-    ble_hs_test_util_init();
-
-    ble_gatts_notify_test_num_events = 0;
-
-    ble_hs_test_util_store_init(10, 10, 10);
-    ble_hs_cfg.store_read_cb = ble_hs_test_util_store_read;
-    ble_hs_cfg.store_write_cb = ble_hs_test_util_store_write;
-
-    rc = ble_gatts_register_svcs(ble_gatts_notify_test_svcs,
-                                 ble_gatts_notify_test_misc_reg_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT_FATAL(ble_gatts_notify_test_chr_1_def_handle != 0);
-    TEST_ASSERT_FATAL(ble_gatts_notify_test_chr_2_def_handle != 0);
-
-    ble_gatts_start();
-
-    ble_hs_test_util_create_conn(2, ble_gatts_notify_test_peer_addr,
-                                 ble_gatts_notify_test_util_gap_event, NULL);
-    *out_conn_handle = 2;
-
-    if (bonding) {
-        ble_hs_lock();
-        conn = ble_hs_conn_find(2);
-        TEST_ASSERT_FATAL(conn != NULL);
-        conn->bhc_sec_state.encrypted = 1;
-        conn->bhc_sec_state.authenticated = 1;
-        conn->bhc_sec_state.bonded = 1;
-        ble_hs_unlock();
-    }
-
-    /* Ensure notifications disabled on new connection. */
-    flags = ble_gatts_notify_test_misc_read_notify(
-        2, ble_gatts_notify_test_chr_1_def_handle);
-    TEST_ASSERT(flags == 0);
-    flags = ble_gatts_notify_test_misc_read_notify(
-        2, ble_gatts_notify_test_chr_2_def_handle);
-    TEST_ASSERT(flags == 0);
-
-    /* Set initial notification / indication state and verify that subscription
-     * callback gets executed.
-     */
-    if (chr1_flags != 0) {
-        ble_gatts_notify_test_misc_enable_notify(
-            2, ble_gatts_notify_test_chr_1_def_handle, chr1_flags);
-
-        ble_gatts_notify_test_util_verify_sub_event(
-            *out_conn_handle,
-            ble_gatts_notify_test_chr_1_def_handle + 1,
-            BLE_GAP_SUBSCRIBE_REASON_WRITE,
-            0, chr1_flags == BLE_GATTS_CLT_CFG_F_NOTIFY,
-            0, chr1_flags == BLE_GATTS_CLT_CFG_F_INDICATE);
-    }
-    if (chr2_flags != 0) {
-        ble_gatts_notify_test_misc_enable_notify(
-            2, ble_gatts_notify_test_chr_2_def_handle, chr2_flags);
-
-        ble_gatts_notify_test_util_verify_sub_event(
-            *out_conn_handle,
-            ble_gatts_notify_test_chr_2_def_handle + 1,
-            BLE_GAP_SUBSCRIBE_REASON_WRITE,
-            0, chr2_flags == BLE_GATTS_CLT_CFG_F_NOTIFY,
-            0, chr2_flags == BLE_GATTS_CLT_CFG_F_INDICATE);
-    }
-
-    /* Ensure no extraneous subscription callbacks were executed. */
-    TEST_ASSERT(ble_gatts_notify_test_num_events == 0);
-
-    /* Toss both write responses. */
-    ble_hs_test_util_prev_tx_queue_clear();
-
-    /* Ensure notification / indication state reads back correctly. */
-    flags = ble_gatts_notify_test_misc_read_notify(
-        2, ble_gatts_notify_test_chr_1_def_handle);
-    TEST_ASSERT(flags == chr1_flags);
-    flags = ble_gatts_notify_test_misc_read_notify(
-        2, ble_gatts_notify_test_chr_2_def_handle);
-    TEST_ASSERT(flags == chr2_flags);
-
-    /* Ensure both CCCDs still persisted. */
-    if (bonding) {
-        exp_num_cccds = (chr1_flags != 0) + (chr2_flags != 0);
-    } else {
-        exp_num_cccds = 0;
-    }
-    TEST_ASSERT(ble_hs_test_util_store_num_cccds == exp_num_cccds);
-}
-
-static void
-ble_gatts_notify_test_disconnect(uint16_t conn_handle,
-                                 uint8_t chr1_flags,
-                                 uint8_t chr1_indicate_in_progress,
-                                 uint8_t chr2_flags,
-                                 uint8_t chr2_indicate_in_progress)
-{
-    ble_hs_test_util_conn_disconnect(conn_handle);
-
-    if (chr1_indicate_in_progress) {
-        ble_gatts_notify_test_util_verify_tx_event(
-            conn_handle,
-            ble_gatts_notify_test_chr_1_def_handle + 1,
-            BLE_HS_ENOTCONN,
-            1);
-    }
-
-    /* Verify subscription callback executed for each subscribed
-     * characteristic.
-     */
-    if (chr1_flags != 0) {
-        ble_gatts_notify_test_util_verify_sub_event(
-            conn_handle,
-            ble_gatts_notify_test_chr_1_def_handle + 1,
-            BLE_GAP_SUBSCRIBE_REASON_TERM,
-            chr1_flags == BLE_GATTS_CLT_CFG_F_NOTIFY, 0,
-            chr1_flags == BLE_GATTS_CLT_CFG_F_INDICATE, 0);
-    }
-
-    if (chr2_indicate_in_progress) {
-        ble_gatts_notify_test_util_verify_tx_event(
-            conn_handle,
-            ble_gatts_notify_test_chr_2_def_handle + 1,
-            BLE_HS_ENOTCONN,
-            1);
-    }
-
-    if (chr2_flags != 0) {
-        ble_gatts_notify_test_util_verify_sub_event(
-            conn_handle,
-            ble_gatts_notify_test_chr_2_def_handle + 1,
-            BLE_GAP_SUBSCRIBE_REASON_TERM,
-            chr2_flags == BLE_GATTS_CLT_CFG_F_NOTIFY, 0,
-            chr2_flags == BLE_GATTS_CLT_CFG_F_INDICATE, 0);
-    }
-}
-
-static void
-ble_gatts_notify_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt,
-                                  void *arg)
-{
-    uint16_t uuid16;
-
-    if (ctxt->op == BLE_GATT_REGISTER_OP_CHR) {
-        uuid16 = ble_uuid_128_to_16(ctxt->chr.chr_def->uuid128);
-        switch (uuid16) {
-        case BLE_GATTS_NOTIFY_TEST_CHR_1_UUID:
-            ble_gatts_notify_test_chr_1_def_handle = ctxt->chr.def_handle;
-            break;
-
-        case BLE_GATTS_NOTIFY_TEST_CHR_2_UUID:
-            ble_gatts_notify_test_chr_2_def_handle = ctxt->chr.def_handle;
-            break;
-
-        default:
-            TEST_ASSERT_FATAL(0);
-            break;
-        }
-    }
-}
-
-static int
-ble_gatts_notify_test_misc_access(uint16_t conn_handle,
-                                  uint16_t attr_handle,
-                                  struct ble_gatt_access_ctxt *ctxt,
-                                  void *arg)
-{
-    int rc;
-
-    TEST_ASSERT_FATAL(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
-    TEST_ASSERT(conn_handle == 0xffff);
-
-    if (attr_handle == ble_gatts_notify_test_chr_1_def_handle + 1) {
-        TEST_ASSERT(ctxt->chr ==
-                    &ble_gatts_notify_test_svcs[0].characteristics[0]);
-        rc = os_mbuf_copyinto(ctxt->om, 0, ble_gatts_notify_test_chr_1_val,
-                              ble_gatts_notify_test_chr_1_len);
-        TEST_ASSERT_FATAL(rc == 0);
-    } else if (attr_handle == ble_gatts_notify_test_chr_2_def_handle + 1) {
-        TEST_ASSERT(ctxt->chr ==
-                    &ble_gatts_notify_test_svcs[0].characteristics[1]);
-        rc = os_mbuf_copyinto(ctxt->om, 0, ble_gatts_notify_test_chr_2_val,
-                              ble_gatts_notify_test_chr_2_len);
-        TEST_ASSERT_FATAL(rc == 0);
-    } else {
-        TEST_ASSERT_FATAL(0);
-    }
-
-    return 0;
-}
-
-static void
-ble_gatts_notify_test_misc_rx_indicate_rsp(uint16_t conn_handle,
-                                           uint16_t attr_handle)
-{
-    uint8_t buf[BLE_ATT_INDICATE_RSP_SZ];
-    int rc;
-
-    ble_att_indicate_rsp_write(buf, sizeof buf);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-
-    ble_gatts_notify_test_util_verify_ack_event(conn_handle, attr_handle);
-}
-
-
-static void
-ble_gatts_notify_test_misc_verify_tx_n(uint16_t conn_handle,
-                                       uint16_t attr_handle,
-                                       uint8_t *attr_data, int attr_len)
-{
-    struct ble_att_notify_req req;
-    struct os_mbuf *om;
-    int i;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    ble_att_notify_req_parse(om->om_data, om->om_len, &req);
-    TEST_ASSERT(req.banq_handle == attr_handle);
-
-    for (i = 0; i < attr_len; i++) {
-        TEST_ASSERT(om->om_data[BLE_ATT_NOTIFY_REQ_BASE_SZ + i] ==
-                    attr_data[i]);
-    }
-
-    ble_gatts_notify_test_util_verify_tx_event(conn_handle, attr_handle, 0, 0);
-}
-
-static void
-ble_gatts_notify_test_misc_verify_tx_i(uint16_t conn_handle,
-                                       uint16_t attr_handle,
-                                       uint8_t *attr_data, int attr_len)
-{
-    struct ble_att_indicate_req req;
-    struct os_mbuf *om;
-    int i;
-
-    ble_hs_test_util_tx_all();
-
-    om = ble_hs_test_util_prev_tx_dequeue_pullup();
-    TEST_ASSERT_FATAL(om != NULL);
-
-    ble_att_indicate_req_parse(om->om_data, om->om_len, &req);
-    TEST_ASSERT(req.baiq_handle == attr_handle);
-
-    for (i = 0; i < attr_len; i++) {
-        TEST_ASSERT(om->om_data[BLE_ATT_INDICATE_REQ_BASE_SZ + i] ==
-                    attr_data[i]);
-    }
-
-    ble_gatts_notify_test_util_verify_tx_event(conn_handle, attr_handle, 0, 1);
-}
-
-static void
-ble_gatts_notify_test_misc_verify_tx_gen(uint16_t conn_handle, int attr_idx,
-                                         uint8_t chr_flags)
-{
-    uint16_t attr_handle;
-    uint16_t attr_len;
-    void *attr_val;
-
-    switch (attr_idx) {
-    case 1:
-        attr_handle = ble_gatts_notify_test_chr_1_def_handle + 1;
-        attr_len = ble_gatts_notify_test_chr_1_len;
-        attr_val = ble_gatts_notify_test_chr_1_val;
-        break;
-
-    case 2:
-        attr_handle = ble_gatts_notify_test_chr_2_def_handle + 1;
-        attr_len = ble_gatts_notify_test_chr_2_len;
-        attr_val = ble_gatts_notify_test_chr_2_val;
-        break;
-
-    default:
-        TEST_ASSERT_FATAL(0);
-        break;
-    }
-
-    switch (chr_flags) {
-    case 0:
-        break;
-
-    case BLE_GATTS_CLT_CFG_F_NOTIFY:
-        ble_gatts_notify_test_misc_verify_tx_n(conn_handle, attr_handle,
-                                               attr_val, attr_len);
-        break;
-
-    case BLE_GATTS_CLT_CFG_F_INDICATE:
-        ble_gatts_notify_test_misc_verify_tx_i(conn_handle, attr_handle,
-                                               attr_val, attr_len);
-        break;
-
-    default:
-        TEST_ASSERT_FATAL(0);
-        break;
-    }
-}
-
-static void
-ble_gatts_notify_test_restore_bonding(uint16_t conn_handle,
-                                      uint8_t chr1_flags, uint8_t chr1_tx,
-                                      uint8_t chr2_flags, uint8_t chr2_tx)
-{
-    struct ble_hs_conn *conn;
-
-    ble_hs_lock();
-    conn = ble_hs_conn_find(conn_handle);
-    TEST_ASSERT_FATAL(conn != NULL);
-    conn->bhc_sec_state.encrypted = 1;
-    conn->bhc_sec_state.authenticated = 1;
-    conn->bhc_sec_state.bonded = 1;
-    ble_hs_unlock();
-
-    ble_gatts_bonding_restored(conn_handle);
-
-    /* Verify subscription callback executed for each subscribed
-     * characteristic.
-     */
-    if (chr1_flags != 0) {
-        ble_gatts_notify_test_util_verify_sub_event(
-            conn_handle,
-            ble_gatts_notify_test_chr_1_def_handle + 1,
-            BLE_GAP_SUBSCRIBE_REASON_RESTORE,
-            0, chr1_flags == BLE_GATTS_CLT_CFG_F_NOTIFY,
-            0, chr1_flags == BLE_GATTS_CLT_CFG_F_INDICATE);
-
-    }
-    if (chr1_tx) {
-        ble_gatts_notify_test_misc_verify_tx_gen(conn_handle, 1, chr1_flags);
-    }
-
-
-    if (chr2_flags != 0) {
-        ble_gatts_notify_test_util_verify_sub_event(
-            conn_handle,
-            ble_gatts_notify_test_chr_2_def_handle + 1,
-            BLE_GAP_SUBSCRIBE_REASON_RESTORE,
-            0, chr2_flags == BLE_GATTS_CLT_CFG_F_NOTIFY,
-            0, chr2_flags == BLE_GATTS_CLT_CFG_F_INDICATE);
-    }
-    if (chr2_tx) {
-        ble_gatts_notify_test_misc_verify_tx_gen(conn_handle, 2, chr2_flags);
-    }
-}
-
-TEST_CASE(ble_gatts_notify_test_n)
-{
-    uint16_t conn_handle;
-    uint16_t flags;
-
-    ble_gatts_notify_test_misc_init(&conn_handle, 0,
-                                    BLE_GATTS_CLT_CFG_F_NOTIFY,
-                                    BLE_GATTS_CLT_CFG_F_NOTIFY);
-
-    /* Ensure notifications read back as enabled. */
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
-    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_NOTIFY);
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
-    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_NOTIFY);
-
-    /* Update characteristic 1's value. */
-    ble_gatts_notify_test_chr_1_len = 1;
-    ble_gatts_notify_test_chr_1_val[0] = 0xab;
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
-
-    /* Verify notification sent properly. */
-    ble_gatts_notify_test_misc_verify_tx_n(
-        conn_handle,
-        ble_gatts_notify_test_chr_1_def_handle + 1,
-        ble_gatts_notify_test_chr_1_val,
-        ble_gatts_notify_test_chr_1_len);
-
-    /* Update characteristic 2's value. */
-    ble_gatts_notify_test_chr_2_len = 16;
-    memcpy(ble_gatts_notify_test_chr_2_val,
-           ((uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}), 16);
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
-
-    /* Verify notification sent properly. */
-    ble_gatts_notify_test_misc_verify_tx_n(
-        conn_handle,
-        ble_gatts_notify_test_chr_2_def_handle + 1,
-        ble_gatts_notify_test_chr_2_val,
-        ble_gatts_notify_test_chr_2_len);
-
-    /***
-     * Disconnect, modify characteristic values, and reconnect.  Ensure
-     * notifications are not sent and are no longer enabled.
-     */
-
-    ble_gatts_notify_test_disconnect(conn_handle,
-                                     BLE_GATTS_CLT_CFG_F_NOTIFY, 0,
-                                     BLE_GATTS_CLT_CFG_F_NOTIFY, 0);
-
-    /* Update characteristic 1's value. */
-    ble_gatts_notify_test_chr_1_len = 1;
-    ble_gatts_notify_test_chr_1_val[0] = 0xdd;
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
-
-    /* Update characteristic 2's value. */
-    ble_gatts_notify_test_chr_2_len = 16;
-    memcpy(ble_gatts_notify_test_chr_2_val,
-           ((uint8_t[]){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}), 16);
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
-
-    ble_hs_test_util_create_conn(conn_handle, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 ble_gatts_notify_test_util_gap_event, NULL);
-
-    /* Ensure no notifications sent. */
-    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
-
-    /* Ensure notifications disabled. */
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
-    TEST_ASSERT(flags == 0);
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
-    TEST_ASSERT(flags == 0);
-}
-
-TEST_CASE(ble_gatts_notify_test_i)
-{
-    uint16_t conn_handle;
-    uint16_t flags;
-
-    ble_gatts_notify_test_misc_init(&conn_handle, 0,
-                                    BLE_GATTS_CLT_CFG_F_INDICATE,
-                                    BLE_GATTS_CLT_CFG_F_INDICATE);
-
-    /* Update characteristic 1's value. */
-    ble_gatts_notify_test_chr_1_len = 1;
-    ble_gatts_notify_test_chr_1_val[0] = 0xab;
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
-
-    /* Verify indication sent properly. */
-    ble_gatts_notify_test_misc_verify_tx_i(
-        conn_handle,
-        ble_gatts_notify_test_chr_1_def_handle + 1,
-        ble_gatts_notify_test_chr_1_val,
-        ble_gatts_notify_test_chr_1_len);
-
-    /* Update characteristic 2's value. */
-    ble_gatts_notify_test_chr_2_len = 16;
-    memcpy(ble_gatts_notify_test_chr_2_val,
-           ((uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}), 16);
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
-
-    /* Verify the second indication doesn't get sent until the first is
-     * confirmed.
-     */
-    ble_hs_test_util_tx_all();
-    TEST_ASSERT(ble_hs_test_util_prev_tx_queue_sz() == 0);
-
-    /* Receive the confirmation for the first indication. */
-    ble_gatts_notify_test_misc_rx_indicate_rsp(
-        conn_handle,
-        ble_gatts_notify_test_chr_1_def_handle + 1);
-
-    /* Verify indication sent properly. */
-    ble_hs_test_util_tx_all();
-    ble_gatts_notify_test_misc_verify_tx_i(
-        conn_handle,
-        ble_gatts_notify_test_chr_2_def_handle + 1,
-        ble_gatts_notify_test_chr_2_val,
-        ble_gatts_notify_test_chr_2_len);
-
-    /* Receive the confirmation for the second indication. */
-    ble_gatts_notify_test_misc_rx_indicate_rsp(
-        conn_handle,
-        ble_gatts_notify_test_chr_2_def_handle + 1);
-
-    /* Verify no pending GATT jobs. */
-    TEST_ASSERT(!ble_gattc_any_jobs());
-
-    /***
-     * Disconnect, modify characteristic values, and reconnect.  Ensure
-     * indications are not sent and are no longer enabled.
-     */
-
-    ble_gatts_notify_test_disconnect(conn_handle,
-                                     BLE_GATTS_CLT_CFG_F_INDICATE, 0,
-                                     BLE_GATTS_CLT_CFG_F_INDICATE, 0);
-
-    /* Update characteristic 1's value. */
-    ble_gatts_notify_test_chr_1_len = 1;
-    ble_gatts_notify_test_chr_1_val[0] = 0xdd;
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
-
-    /* Update characteristic 2's value. */
-    ble_gatts_notify_test_chr_2_len = 16;
-    memcpy(ble_gatts_notify_test_chr_2_val,
-           ((uint8_t[]){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}), 16);
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
-
-    ble_hs_test_util_create_conn(conn_handle, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 ble_gatts_notify_test_util_gap_event, NULL);
-
-    /* Ensure no indications sent. */
-    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
-
-    /* Ensure indications disabled. */
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
-    TEST_ASSERT(flags == 0);
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
-    TEST_ASSERT(flags == 0);
-}
-
-TEST_CASE(ble_gatts_notify_test_bonded_n)
-{
-    uint16_t conn_handle;
-    uint16_t flags;
-
-    ble_gatts_notify_test_misc_init(&conn_handle, 1,
-                                    BLE_GATTS_CLT_CFG_F_NOTIFY,
-                                    BLE_GATTS_CLT_CFG_F_NOTIFY);
-
-    /* Disconnect. */
-    ble_gatts_notify_test_disconnect(conn_handle,
-                                     BLE_GATTS_CLT_CFG_F_NOTIFY, 0,
-                                     BLE_GATTS_CLT_CFG_F_NOTIFY, 0);
-
-    /* Ensure both CCCDs still persisted. */
-    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 2);
-
-    /* Update characteristic 1's value. */
-    ble_gatts_notify_test_chr_1_len = 1;
-    ble_gatts_notify_test_chr_1_val[0] = 0xdd;
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
-
-    /* Update characteristic 2's value. */
-    ble_gatts_notify_test_chr_2_len = 16;
-    memcpy(ble_gatts_notify_test_chr_2_val,
-           ((uint8_t[]){1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16}), 16);
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
-
-    /* Reconnect; ensure notifications don't get sent while unbonded and that
-     * notifications appear disabled.
-     */
-
-    ble_hs_test_util_create_conn(conn_handle, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 ble_gatts_notify_test_util_gap_event, NULL);
-
-    ble_gatts_notify_test_num_events = 0;
-    /* Ensure no notifications sent. */
-    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
-
-    /* Ensure notifications disabled. */
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
-    TEST_ASSERT(flags == 0);
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
-    TEST_ASSERT(flags == 0);
-
-    /* Simulate a successful encryption procedure (bonding restoration). */
-    ble_gatts_notify_test_restore_bonding(conn_handle,
-                                          BLE_GATTS_CLT_CFG_F_NOTIFY, 1,
-                                          BLE_GATTS_CLT_CFG_F_NOTIFY, 1);
-
-    /* Ensure notifications enabled. */
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
-    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_NOTIFY);
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
-    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_NOTIFY);
-
-    /* Ensure both CCCDs still persisted. */
-    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 2);
-}
-
-TEST_CASE(ble_gatts_notify_test_bonded_i)
-{
-    uint16_t conn_handle;
-    uint16_t flags;
-
-    ble_gatts_notify_test_misc_init(&conn_handle, 1,
-                                    BLE_GATTS_CLT_CFG_F_INDICATE,
-                                    BLE_GATTS_CLT_CFG_F_INDICATE);
-
-    /* Disconnect. */
-    ble_gatts_notify_test_disconnect(conn_handle,
-                                     BLE_GATTS_CLT_CFG_F_INDICATE, 0,
-                                     BLE_GATTS_CLT_CFG_F_INDICATE, 0);
-
-    /* Ensure both CCCDs still persisted. */
-    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 2);
-
-    /* Update characteristic 1's value. */
-    ble_gatts_notify_test_chr_1_len = 1;
-    ble_gatts_notify_test_chr_1_val[0] = 0xab;
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
-
-    /* Update characteristic 2's value. */
-    ble_gatts_notify_test_chr_2_len = 16;
-    memcpy(ble_gatts_notify_test_chr_2_val,
-           ((uint8_t[]){0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}), 16);
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_2_def_handle + 1);
-
-    /* Reconnect; ensure notifications don't get sent while unbonded and that
-     * notifications appear disabled.
-     */
-
-    ble_hs_test_util_create_conn(conn_handle, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 ble_gatts_notify_test_util_gap_event, NULL);
-
-    /* Ensure no indications sent. */
-    TEST_ASSERT(ble_hs_test_util_prev_tx_dequeue() == NULL);
-
-    /* Ensure notifications disabled. */
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
-    TEST_ASSERT(flags == 0);
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
-    TEST_ASSERT(flags == 0);
-
-    /* Simulate a successful encryption procedure (bonding restoration). */
-    ble_gatts_notify_test_restore_bonding(conn_handle,
-                                          BLE_GATTS_CLT_CFG_F_INDICATE, 1,
-                                          BLE_GATTS_CLT_CFG_F_INDICATE, 0);
-
-    /* Verify the second indication doesn't get sent until the first is
-     * confirmed.
-     */
-    ble_hs_test_util_tx_all();
-    TEST_ASSERT(ble_hs_test_util_prev_tx_queue_sz() == 0);
-
-    /* Receive the confirmation for the first indication. */
-    ble_gatts_notify_test_misc_rx_indicate_rsp(
-        conn_handle,
-        ble_gatts_notify_test_chr_1_def_handle + 1);
-
-    /* Verify indication sent properly. */
-    ble_hs_test_util_tx_all();
-    ble_gatts_notify_test_misc_verify_tx_i(
-        conn_handle,
-        ble_gatts_notify_test_chr_2_def_handle + 1,
-        ble_gatts_notify_test_chr_2_val,
-        ble_gatts_notify_test_chr_2_len);
-
-    /* Receive the confirmation for the second indication. */
-    ble_gatts_notify_test_misc_rx_indicate_rsp(
-        conn_handle,
-        ble_gatts_notify_test_chr_2_def_handle + 1);
-
-    /* Verify no pending GATT jobs. */
-    TEST_ASSERT(!ble_gattc_any_jobs());
-
-    /* Ensure notifications enabled. */
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
-    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_INDICATE);
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
-    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_INDICATE);
-
-    /* Ensure both CCCDs still persisted. */
-    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 2);
-}
-
-TEST_CASE(ble_gatts_notify_test_bonded_i_no_ack)
-{
-    struct ble_store_value_cccd value_cccd;
-    struct ble_store_key_cccd key_cccd;
-    uint16_t conn_handle;
-    uint16_t flags;
-    int rc;
-
-    ble_gatts_notify_test_misc_init(&conn_handle, 1,
-                                    BLE_GATTS_CLT_CFG_F_INDICATE, 0);
-
-    /* Update characteristic 1's value. */
-    ble_gatts_notify_test_chr_1_len = 1;
-    ble_gatts_notify_test_chr_1_val[0] = 0xab;
-    ble_gatts_chr_updated(ble_gatts_notify_test_chr_1_def_handle + 1);
-
-    /* Verify indication sent properly. */
-    ble_hs_test_util_tx_all();
-    ble_gatts_notify_test_misc_verify_tx_i(
-        conn_handle,
-        ble_gatts_notify_test_chr_1_def_handle + 1,
-        ble_gatts_notify_test_chr_1_val,
-        ble_gatts_notify_test_chr_1_len);
-
-    /* Verify 'updated' state is still persisted. */
-    key_cccd.peer_addr_type = BLE_STORE_ADDR_TYPE_NONE;
-    key_cccd.chr_val_handle = ble_gatts_notify_test_chr_1_def_handle + 1;
-    key_cccd.idx = 0;
-
-    rc = ble_store_read_cccd(&key_cccd, &value_cccd);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(value_cccd.value_changed);
-
-    /* Disconnect. */
-    ble_gatts_notify_test_disconnect(conn_handle,
-                                     BLE_GATTS_CLT_CFG_F_INDICATE, 1, 0, 0);
-
-    /* Ensure CCCD still persisted. */
-    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 1);
-
-    /* Reconnect. */
-    ble_hs_test_util_create_conn(conn_handle, ((uint8_t[]){2,3,4,5,6,7,8,9}),
-                                 ble_gatts_notify_test_util_gap_event, NULL);
-
-    /* Simulate a successful encryption procedure (bonding restoration). */
-    ble_gatts_notify_test_restore_bonding(conn_handle,
-                                          BLE_GATTS_CLT_CFG_F_INDICATE, 1,
-                                          0, 0);
-
-    /* Receive the confirmation for the indication. */
-    ble_gatts_notify_test_misc_rx_indicate_rsp(
-        conn_handle,
-        ble_gatts_notify_test_chr_1_def_handle + 1);
-
-    /* Verify no pending GATT jobs. */
-    TEST_ASSERT(!ble_gattc_any_jobs());
-
-    /* Ensure indication enabled. */
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_1_def_handle);
-    TEST_ASSERT(flags == BLE_GATTS_CLT_CFG_F_INDICATE);
-    flags = ble_gatts_notify_test_misc_read_notify(
-        conn_handle, ble_gatts_notify_test_chr_2_def_handle);
-    TEST_ASSERT(flags == 0);
-
-    /* Ensure CCCD still persisted. */
-    TEST_ASSERT(ble_hs_test_util_store_num_cccds == 1);
-
-    /* Verify 'updated' state is no longer persisted. */
-    rc = ble_store_read_cccd(&key_cccd, &value_cccd);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT(!value_cccd.value_changed);
-}
-
-TEST_SUITE(ble_gatts_notify_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gatts_notify_test_n();
-    ble_gatts_notify_test_i();
-
-    ble_gatts_notify_test_bonded_n();
-    ble_gatts_notify_test_bonded_i();
-
-    ble_gatts_notify_test_bonded_i_no_ack();
-
-    /* XXX: Test corner cases:
-     *     o Bonding after CCCD configuration.
-     *     o Disconnect prior to rx of indicate ack.
-     */
-}
-
-int
-ble_gatts_notify_test_all(void)
-{
-    ble_gatts_notify_suite();
-
-    return tu_any_failed;
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/src/test/ble_gatts_read_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/test/ble_gatts_read_test.c b/net/nimble/host/src/test/ble_gatts_read_test.c
deleted file mode 100644
index cef9f92..0000000
--- a/net/nimble/host/src/test/ble_gatts_read_test.c
+++ /dev/null
@@ -1,261 +0,0 @@
-/**
- * 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 <string.h>
-#include <errno.h>
-#include "testutil/testutil.h"
-#include "host/ble_uuid.h"
-#include "host/ble_hs_test.h"
-#include "ble_hs_test_util.h"
-
-#define BLE_GATTS_READ_TEST_CHR_1_UUID    0x1111
-#define BLE_GATTS_READ_TEST_CHR_2_UUID    0x2222
-
-static uint8_t ble_gatts_read_test_peer_addr[6] = {2,3,4,5,6,7};
-
-static int
-ble_gatts_read_test_util_access_1(uint16_t conn_handle,
-                                  uint16_t attr_handle,
-                                  struct ble_gatt_access_ctxt *ctxt,
-                                  void *arg);
-
-static int
-ble_gatts_read_test_util_access_2(uint16_t conn_handle,
-                                  uint16_t attr_handle,
-                                  struct ble_gatt_access_ctxt *ctxt,
-                                  void *arg);
-static void
-ble_gatts_read_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt,
-                                void *arg);
-
-static const struct ble_gatt_svc_def ble_gatts_read_test_svcs[] = { {
-    .type = BLE_GATT_SVC_TYPE_PRIMARY,
-    .uuid128 = BLE_UUID16(0x1234),
-    .characteristics = (struct ble_gatt_chr_def[]) { {
-        .uuid128 = BLE_UUID16(BLE_GATTS_READ_TEST_CHR_1_UUID),
-        .access_cb = ble_gatts_read_test_util_access_1,
-        .flags = BLE_GATT_CHR_F_READ
-    }, {
-        .uuid128 = BLE_UUID16(BLE_GATTS_READ_TEST_CHR_2_UUID),
-        .access_cb = ble_gatts_read_test_util_access_2,
-        .flags = BLE_GATT_CHR_F_READ
-    }, {
-        0
-    } },
-}, {
-    0
-} };
-
-
-static uint16_t ble_gatts_read_test_chr_1_def_handle;
-static uint16_t ble_gatts_read_test_chr_1_val_handle;
-static uint8_t ble_gatts_read_test_chr_1_val[1024];
-static int ble_gatts_read_test_chr_1_len;
-static uint16_t ble_gatts_read_test_chr_2_def_handle;
-static uint16_t ble_gatts_read_test_chr_2_val_handle;
-
-static void
-ble_gatts_read_test_misc_init(uint16_t *out_conn_handle)
-{
-    int rc;
-
-    ble_hs_test_util_init();
-
-    rc = ble_gatts_register_svcs(ble_gatts_read_test_svcs,
-                                 ble_gatts_read_test_misc_reg_cb, NULL);
-    TEST_ASSERT_FATAL(rc == 0);
-    TEST_ASSERT_FATAL(ble_gatts_read_test_chr_1_def_handle != 0);
-    TEST_ASSERT_FATAL(ble_gatts_read_test_chr_1_val_handle ==
-                      ble_gatts_read_test_chr_1_def_handle + 1);
-    TEST_ASSERT_FATAL(ble_gatts_read_test_chr_2_def_handle != 0);
-    TEST_ASSERT_FATAL(ble_gatts_read_test_chr_2_val_handle ==
-                      ble_gatts_read_test_chr_2_def_handle + 1);
-
-    ble_gatts_start();
-
-    ble_hs_test_util_create_conn(2, ble_gatts_read_test_peer_addr, NULL, NULL);
-
-    if (out_conn_handle != NULL) {
-        *out_conn_handle = 2;
-    }
-}
-
-static void
-ble_gatts_read_test_misc_reg_cb(struct ble_gatt_register_ctxt *ctxt,
-                                void *arg)
-{
-    uint16_t uuid16;
-
-    if (ctxt->op == BLE_GATT_REGISTER_OP_CHR) {
-        uuid16 = ble_uuid_128_to_16(ctxt->chr.chr_def->uuid128);
-        switch (uuid16) {
-        case BLE_GATTS_READ_TEST_CHR_1_UUID:
-            ble_gatts_read_test_chr_1_def_handle = ctxt->chr.def_handle;
-            ble_gatts_read_test_chr_1_val_handle = ctxt->chr.val_handle;
-            break;
-
-        case BLE_GATTS_READ_TEST_CHR_2_UUID:
-            ble_gatts_read_test_chr_2_def_handle = ctxt->chr.def_handle;
-            ble_gatts_read_test_chr_2_val_handle = ctxt->chr.val_handle;
-            break;
-
-        default:
-            TEST_ASSERT_FATAL(0);
-            break;
-        }
-    }
-}
-
-static int
-ble_gatts_read_test_util_access_1(uint16_t conn_handle,
-                                  uint16_t attr_handle,
-                                  struct ble_gatt_access_ctxt *ctxt,
-                                  void *arg)
-{
-    int rc;
-
-    TEST_ASSERT_FATAL(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
-    TEST_ASSERT_FATAL(attr_handle == ble_gatts_read_test_chr_1_val_handle);
-
-    TEST_ASSERT(ctxt->chr ==
-                &ble_gatts_read_test_svcs[0].characteristics[0]);
-
-    rc = os_mbuf_append(ctxt->om, ble_gatts_read_test_chr_1_val,
-                        ble_gatts_read_test_chr_1_len);
-    TEST_ASSERT(rc == 0);
-
-    return 0;
-}
-
-static int
-ble_gatts_read_test_util_access_2(uint16_t conn_handle,
-                                  uint16_t attr_handle,
-                                  struct ble_gatt_access_ctxt *ctxt,
-                                  void *arg)
-{
-    uint8_t *buf;
-
-    TEST_ASSERT_FATAL(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
-    TEST_ASSERT_FATAL(attr_handle == ble_gatts_read_test_chr_2_def_handle + 1);
-
-    TEST_ASSERT(ctxt->chr ==
-                &ble_gatts_read_test_svcs[0].characteristics[1]);
-
-    buf = os_mbuf_extend(ctxt->om, 6);
-    TEST_ASSERT_FATAL(buf != NULL);
-
-    buf[0] = 0;
-    buf[1] = 10;
-    buf[2] = 20;
-    buf[3] = 30;
-    buf[4] = 40;
-    buf[5] = 50;
-
-    return 0;
-}
-
-static void
-ble_gatts_read_test_once(uint16_t conn_handle, uint16_t attr_id,
-                         void *expected_value, uint16_t expected_len)
-{
-    struct ble_att_read_req read_req;
-    uint8_t buf[BLE_ATT_READ_REQ_SZ];
-    int rc;
-
-    read_req.barq_handle = attr_id;
-    ble_att_read_req_write(buf, sizeof buf, &read_req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_test_util_verify_tx_read_rsp(expected_value, expected_len);
-}
-
-TEST_CASE(ble_gatts_read_test_case_basic)
-{
-    uint16_t conn_handle;
-
-    ble_gatts_read_test_misc_init(&conn_handle);
-
-    /*** Application points attribute at static data. */
-    ble_gatts_read_test_chr_1_val[0] = 1;
-    ble_gatts_read_test_chr_1_val[1] = 2;
-    ble_gatts_read_test_chr_1_val[2] = 3;
-    ble_gatts_read_test_chr_1_len = 3;
-    ble_gatts_read_test_once(conn_handle,
-                             ble_gatts_read_test_chr_1_val_handle,
-                             ble_gatts_read_test_chr_1_val,
-                             ble_gatts_read_test_chr_1_len);
-
-    /*** Application uses stack-provided buffer for dynamic attribute. */
-    ble_gatts_read_test_once(conn_handle,
-                             ble_gatts_read_test_chr_2_def_handle + 1,
-                             ((uint8_t[6]){0,10,20,30,40,50}), 6);
-
-}
-
-TEST_CASE(ble_gatts_read_test_case_long)
-{
-    struct ble_att_read_blob_req read_blob_req;
-    struct ble_att_read_req read_req;
-    uint8_t buf[max(BLE_ATT_READ_REQ_SZ, BLE_ATT_READ_BLOB_REQ_SZ)];
-    uint16_t conn_handle;
-    int rc;
-    int i;
-
-    ble_gatts_read_test_misc_init(&conn_handle);
-
-    /*** Prepare characteristic value. */
-    ble_gatts_read_test_chr_1_len = 40;
-    for (i = 0; i < ble_gatts_read_test_chr_1_len; i++) {
-        ble_gatts_read_test_chr_1_val[i] = i;
-    }
-
-    /* Receive first read request. */
-    read_req.barq_handle = ble_gatts_read_test_chr_1_val_handle;
-    ble_att_read_req_write(buf, sizeof buf, &read_req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-
-    ble_hs_test_util_verify_tx_read_rsp(ble_gatts_read_test_chr_1_val, 22);
-
-    /* Receive follow-up read blob request. */
-    read_blob_req.babq_handle = ble_gatts_read_test_chr_1_val_handle;
-    read_blob_req.babq_offset = 22;
-    ble_att_read_blob_req_write(buf, sizeof buf, &read_blob_req);
-
-    rc = ble_hs_test_util_l2cap_rx_payload_flat(conn_handle, BLE_L2CAP_CID_ATT,
-                                                buf, sizeof buf);
-    TEST_ASSERT(rc == 0);
-
-    /* Ensure response starts at appropriate offset (22). */
-    ble_hs_test_util_verify_tx_read_blob_rsp(
-        ble_gatts_read_test_chr_1_val + 22, 18);
-}
-
-TEST_SUITE(ble_gatts_read_test_suite)
-{
-    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
-
-    ble_gatts_read_test_case_basic();
-    ble_gatts_read_test_case_long();
-}



[34/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/test/src/arch/sim/nffs_test_priv.h
----------------------------------------------------------------------
diff --git a/fs/nffs/test/src/arch/sim/nffs_test_priv.h b/fs/nffs/test/src/arch/sim/nffs_test_priv.h
new file mode 100644
index 0000000..a3508c1
--- /dev/null
+++ b/fs/nffs/test/src/arch/sim/nffs_test_priv.h
@@ -0,0 +1,42 @@
+/**
+ * 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_NFFS_TEST_PRIV_
+#define H_NFFS_TEST_PRIV_
+
+struct nffs_test_block_desc {
+    const char *data;
+    int data_len;
+};
+
+struct nffs_test_file_desc {
+    const char *filename;
+    int is_dir;
+    const char *contents;
+    int contents_len;
+    struct nffs_test_file_desc *children;
+};
+
+int nffs_test(void);
+
+extern const struct nffs_test_file_desc *nffs_test_system_01;
+extern const struct nffs_test_file_desc *nffs_test_system_01_rm_1014_mk10;
+
+#endif
+


[39/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/test/arch/sim/nffs_test.c
----------------------------------------------------------------------
diff --git a/fs/nffs/src/test/arch/sim/nffs_test.c b/fs/nffs/src/test/arch/sim/nffs_test.c
deleted file mode 100644
index a61b4bc..0000000
--- a/fs/nffs/src/test/arch/sim/nffs_test.c
+++ /dev/null
@@ -1,3250 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <string.h>
-#include <assert.h>
-#include <stdlib.h>
-#include <errno.h>
-#include "hal/hal_flash.h"
-#include "testutil/testutil.h"
-#include "fs/fs.h"
-#include "nffs/nffs.h"
-#include "nffs/nffs_test.h"
-#include "nffs_test_priv.h"
-#include "nffs_priv.h"
-
-int flash_native_memset(uint32_t offset, uint8_t c, uint32_t len);
-
-static const struct nffs_area_desc nffs_area_descs[] = {
-        { 0x00000000, 16 * 1024 },
-        { 0x00004000, 16 * 1024 },
-        { 0x00008000, 16 * 1024 },
-        { 0x0000c000, 16 * 1024 },
-        { 0x00010000, 64 * 1024 },
-        { 0x00020000, 128 * 1024 },
-        { 0x00040000, 128 * 1024 },
-        { 0x00060000, 128 * 1024 },
-        { 0x00080000, 128 * 1024 },
-        { 0x000a0000, 128 * 1024 },
-        { 0x000c0000, 128 * 1024 },
-        { 0x000e0000, 128 * 1024 },
-        { 0, 0 },
-};
-
-static void
-nffs_test_util_assert_ent_name(struct fs_dirent *dirent,
-                               const char *expected_name)
-{
-    char name[NFFS_FILENAME_MAX_LEN + 1];
-    uint8_t name_len;
-    int rc;
-
-    rc = fs_dirent_name(dirent, sizeof name, name, &name_len);
-    TEST_ASSERT(rc == 0);
-    if (rc == 0) {
-        TEST_ASSERT(strcmp(name, expected_name) == 0);
-    }
-}
-
-static void
-nffs_test_util_assert_file_len(struct fs_file *file, uint32_t expected)
-{
-    uint32_t len;
-    int rc;
-
-    rc = fs_filelen(file, &len);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(len == expected);
-}
-
-static void
-nffs_test_util_assert_cache_is_sane(const char *filename)
-{
-    struct nffs_cache_inode *cache_inode;
-    struct nffs_cache_block *cache_block;
-    struct fs_file *fs_file;
-    struct nffs_file *file;
-    uint32_t cache_start;
-    uint32_t cache_end;
-    uint32_t block_end;
-    int rc;
-
-    rc = fs_open(filename, FS_ACCESS_READ, &fs_file);
-    TEST_ASSERT(rc == 0);
-
-    file = (struct nffs_file *)fs_file;
-    rc = nffs_cache_inode_ensure(&cache_inode, file->nf_inode_entry);
-    TEST_ASSERT(rc == 0);
-
-    nffs_cache_inode_range(cache_inode, &cache_start, &cache_end);
-
-    if (TAILQ_EMPTY(&cache_inode->nci_block_list)) {
-        TEST_ASSERT(cache_start == 0 && cache_end == 0);
-    } else {
-        block_end = 0;  /* Pacify gcc. */
-        TAILQ_FOREACH(cache_block, &cache_inode->nci_block_list, ncb_link) {
-            if (cache_block == TAILQ_FIRST(&cache_inode->nci_block_list)) {
-                TEST_ASSERT(cache_block->ncb_file_offset == cache_start);
-            } else {
-                /* Ensure no gap between this block and its predecessor. */
-                TEST_ASSERT(cache_block->ncb_file_offset == block_end);
-            }
-
-            block_end = cache_block->ncb_file_offset +
-                        cache_block->ncb_block.nb_data_len;
-            if (cache_block == TAILQ_LAST(&cache_inode->nci_block_list,
-                                          nffs_cache_block_list)) {
-
-                TEST_ASSERT(block_end == cache_end);
-            }
-        }
-    }
-
-    rc = fs_close(fs_file);
-    TEST_ASSERT(rc == 0);
-}
-
-static void
-nffs_test_util_assert_contents(const char *filename, const char *contents,
-                               int contents_len)
-{
-    struct fs_file *file;
-    uint32_t bytes_read;
-    void *buf;
-    int rc;
-
-    rc = fs_open(filename, FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == 0);
-
-    buf = malloc(contents_len + 1);
-    TEST_ASSERT(buf != NULL);
-
-    rc = fs_read(file, contents_len + 1, buf, &bytes_read);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(bytes_read == contents_len);
-    TEST_ASSERT(memcmp(buf, contents, contents_len) == 0);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    free(buf);
-
-    nffs_test_util_assert_cache_is_sane(filename);
-}
-
-static int
-nffs_test_util_block_count(const char *filename)
-{
-    struct nffs_hash_entry *entry;
-    struct nffs_block block;
-    struct nffs_file *file;
-    struct fs_file *fs_file;
-    int count;
-    int rc;
-
-    rc = fs_open(filename, FS_ACCESS_READ, &fs_file);
-    TEST_ASSERT(rc == 0);
-
-    file = (struct nffs_file *)fs_file;
-    count = 0;
-    entry = file->nf_inode_entry->nie_last_block_entry;
-    while (entry != NULL) {
-        count++;
-        rc = nffs_block_from_hash_entry(&block, entry);
-        TEST_ASSERT(rc == 0);
-        TEST_ASSERT(block.nb_prev != entry);
-        entry = block.nb_prev;
-    }
-
-    rc = fs_close(fs_file);
-    TEST_ASSERT(rc == 0);
-
-    return count;
-}
-
-static void
-nffs_test_util_assert_block_count(const char *filename, int expected_count)
-{
-    int actual_count;
-
-    actual_count = nffs_test_util_block_count(filename);
-    TEST_ASSERT(actual_count == expected_count);
-}
-
-static void
-nffs_test_util_assert_cache_range(const char *filename,
-                                 uint32_t expected_cache_start,
-                                 uint32_t expected_cache_end)
-{
-    struct nffs_cache_inode *cache_inode;
-    struct nffs_file *file;
-    struct fs_file *fs_file;
-    uint32_t cache_start;
-    uint32_t cache_end;
-    int rc;
-
-    rc = fs_open(filename, FS_ACCESS_READ, &fs_file);
-    TEST_ASSERT(rc == 0);
-
-    file = (struct nffs_file *)fs_file;
-    rc = nffs_cache_inode_ensure(&cache_inode, file->nf_inode_entry);
-    TEST_ASSERT(rc == 0);
-
-    nffs_cache_inode_range(cache_inode, &cache_start, &cache_end);
-    TEST_ASSERT(cache_start == expected_cache_start);
-    TEST_ASSERT(cache_end == expected_cache_end);
-
-    rc = fs_close(fs_file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_cache_is_sane(filename);
-}
-
-static void
-nffs_test_util_create_file_blocks(const char *filename,
-                                 const struct nffs_test_block_desc *blocks,
-                                 int num_blocks)
-{
-    struct fs_file *file;
-    uint32_t total_len;
-    uint32_t offset;
-    char *buf;
-    int num_writes;
-    int rc;
-    int i;
-
-    rc = fs_open(filename, FS_ACCESS_WRITE | FS_ACCESS_TRUNCATE, &file);
-    TEST_ASSERT(rc == 0);
-
-    total_len = 0;
-    if (num_blocks <= 0) {
-        num_writes = 1;
-    } else {
-        num_writes = num_blocks;
-    }
-    for (i = 0; i < num_writes; i++) {
-        rc = fs_write(file, blocks[i].data, blocks[i].data_len);
-        TEST_ASSERT(rc == 0);
-
-        total_len += blocks[i].data_len;
-    }
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    buf = malloc(total_len);
-    TEST_ASSERT(buf != NULL);
-
-    offset = 0;
-    for (i = 0; i < num_writes; i++) {
-        memcpy(buf + offset, blocks[i].data, blocks[i].data_len);
-        offset += blocks[i].data_len;
-    }
-    TEST_ASSERT(offset == total_len);
-
-    nffs_test_util_assert_contents(filename, buf, total_len);
-    if (num_blocks > 0) {
-        nffs_test_util_assert_block_count(filename, num_blocks);
-    }
-
-    free(buf);
-}
-
-static void
-nffs_test_util_create_file(const char *filename, const char *contents,
-                           int contents_len)
-{
-    struct nffs_test_block_desc block;
-
-    block.data = contents;
-    block.data_len = contents_len;
-
-    nffs_test_util_create_file_blocks(filename, &block, 0);
-}
-
-static void
-nffs_test_util_append_file(const char *filename, const char *contents,
-                           int contents_len)
-{
-    struct fs_file *file;
-    int rc;
-
-    rc = fs_open(filename, FS_ACCESS_WRITE | FS_ACCESS_APPEND, &file);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_write(file, contents, contents_len);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-}
-
-static void
-nffs_test_copy_area(const struct nffs_area_desc *from,
-                    const struct nffs_area_desc *to)
-{
-    void *buf;
-    int rc;
-
-    TEST_ASSERT(from->nad_length == to->nad_length);
-
-    buf = malloc(from->nad_length);
-    TEST_ASSERT(buf != NULL);
-
-    rc = hal_flash_read(from->nad_flash_id, from->nad_offset, buf,
-                        from->nad_length);
-    TEST_ASSERT(rc == 0);
-
-    rc = hal_flash_erase(from->nad_flash_id, to->nad_offset, to->nad_length);
-    TEST_ASSERT(rc == 0);
-
-    rc = hal_flash_write(to->nad_flash_id, to->nad_offset, buf, to->nad_length);
-    TEST_ASSERT(rc == 0);
-
-    free(buf);
-}
-
-static void
-nffs_test_util_create_subtree(const char *parent_path,
-                             const struct nffs_test_file_desc *elem)
-{
-    char *path;
-    int rc;
-    int i;
-
-    if (parent_path == NULL) {
-        path = malloc(1);
-        TEST_ASSERT(path != NULL);
-        path[0] = '\0';
-    } else {
-        path = malloc(strlen(parent_path) + 1 + strlen(elem->filename) + 1);
-        TEST_ASSERT(path != NULL);
-
-        sprintf(path, "%s/%s", parent_path, elem->filename);
-    }
-
-    if (elem->is_dir) {
-        if (parent_path != NULL) {
-            rc = fs_mkdir(path);
-            TEST_ASSERT(rc == 0);
-        }
-
-        if (elem->children != NULL) {
-            for (i = 0; elem->children[i].filename != NULL; i++) {
-                nffs_test_util_create_subtree(path, elem->children + i);
-            }
-        }
-    } else {
-        nffs_test_util_create_file(path, elem->contents, elem->contents_len);
-    }
-
-    free(path);
-}
-
-static void
-nffs_test_util_create_tree(const struct nffs_test_file_desc *root_dir)
-{
-    nffs_test_util_create_subtree(NULL, root_dir);
-}
-
-#define NFFS_TEST_TOUCHED_ARR_SZ     (16 * 1024)
-static struct nffs_hash_entry
-    *nffs_test_touched_entries[NFFS_TEST_TOUCHED_ARR_SZ];
-static int nffs_test_num_touched_entries;
-
-/*
- * Recursively descend directory structure
- */
-static void
-nffs_test_assert_file(const struct nffs_test_file_desc *file,
-                     struct nffs_inode_entry *inode_entry,
-                     const char *path)
-{
-    const struct nffs_test_file_desc *child_file;
-    struct nffs_inode inode;
-    struct nffs_inode_entry *child_inode_entry;
-    char *child_path;
-    int child_filename_len;
-    int path_len;
-    int rc;
-
-    /*
-     * track of hash entries that have been examined
-     */
-    TEST_ASSERT(nffs_test_num_touched_entries < NFFS_TEST_TOUCHED_ARR_SZ);
-    nffs_test_touched_entries[nffs_test_num_touched_entries] =
-        &inode_entry->nie_hash_entry;
-    nffs_test_num_touched_entries++;
-
-    path_len = strlen(path);
-
-    rc = nffs_inode_from_entry(&inode, inode_entry);
-    TEST_ASSERT(rc == 0);
-
-    /*
-     * recursively examine each child of directory
-     */
-    if (nffs_hash_id_is_dir(inode_entry->nie_hash_entry.nhe_id)) {
-        for (child_file = file->children;
-             child_file != NULL && child_file->filename != NULL;
-             child_file++) {
-
-            /*
-             * Construct full pathname for file
-             * Not null terminated
-             */
-            child_filename_len = strlen(child_file->filename);
-            child_path = malloc(path_len + 1 + child_filename_len + 1);
-            TEST_ASSERT(child_path != NULL);
-            memcpy(child_path, path, path_len);
-            child_path[path_len] = '/';
-            memcpy(child_path + path_len + 1, child_file->filename,
-                   child_filename_len);
-            child_path[path_len + 1 + child_filename_len] = '\0';
-
-            /*
-             * Verify child inode can be found using full pathname
-             */
-            rc = nffs_path_find_inode_entry(child_path, &child_inode_entry);
-            if (rc != 0) {
-                TEST_ASSERT(rc == 0);
-            }
-
-            nffs_test_assert_file(child_file, child_inode_entry, child_path);
-
-            free(child_path);
-        }
-    } else {
-        nffs_test_util_assert_contents(path, file->contents,
-                                       file->contents_len);
-    }
-}
-
-static void
-nffs_test_assert_branch_touched(struct nffs_inode_entry *inode_entry)
-{
-    struct nffs_inode_entry *child;
-    int i;
-
-    if (inode_entry == nffs_lost_found_dir) {
-        return;
-    }
-
-    for (i = 0; i < nffs_test_num_touched_entries; i++) {
-        if (nffs_test_touched_entries[i] == &inode_entry->nie_hash_entry) {
-            break;
-        }
-    }
-    TEST_ASSERT(i < nffs_test_num_touched_entries);
-    nffs_test_touched_entries[i] = NULL;
-
-    if (nffs_hash_id_is_dir(inode_entry->nie_hash_entry.nhe_id)) {
-        SLIST_FOREACH(child, &inode_entry->nie_child_list, nie_sibling_next) {
-            nffs_test_assert_branch_touched(child);
-        }
-    }
-}
-
-static void
-nffs_test_assert_child_inode_present(struct nffs_inode_entry *child)
-{
-    const struct nffs_inode_entry *inode_entry;
-    const struct nffs_inode_entry *parent;
-    struct nffs_inode inode;
-    int rc;
-
-    /*
-     * Sucessfully read inode data from flash
-     */
-    rc = nffs_inode_from_entry(&inode, child);
-    TEST_ASSERT(rc == 0);
-
-    /*
-     * Validate parent
-     */
-    parent = inode.ni_parent;
-    TEST_ASSERT(parent != NULL);
-    TEST_ASSERT(nffs_hash_id_is_dir(parent->nie_hash_entry.nhe_id));
-
-    /*
-     * Make sure inode is in parents child list
-     */
-    SLIST_FOREACH(inode_entry, &parent->nie_child_list, nie_sibling_next) {
-        if (inode_entry == child) {
-            return;
-        }
-    }
-
-    TEST_ASSERT(0);
-}
-
-static void
-nffs_test_assert_block_present(struct nffs_hash_entry *block_entry)
-{
-    const struct nffs_inode_entry *inode_entry;
-    struct nffs_hash_entry *cur;
-    struct nffs_block block;
-    int rc;
-
-    /*
-     * Successfully read block data from flash
-     */
-    rc = nffs_block_from_hash_entry(&block, block_entry);
-    TEST_ASSERT(rc == 0);
-
-    /*
-     * Validate owning inode
-     */
-    inode_entry = block.nb_inode_entry;
-    TEST_ASSERT(inode_entry != NULL);
-    TEST_ASSERT(nffs_hash_id_is_file(inode_entry->nie_hash_entry.nhe_id));
-
-    /*
-     * Validate that block is in owning inode's block chain
-     */
-    cur = inode_entry->nie_last_block_entry;
-    while (cur != NULL) {
-        if (cur == block_entry) {
-            return;
-        }
-
-        rc = nffs_block_from_hash_entry(&block, cur);
-        TEST_ASSERT(rc == 0);
-        cur = block.nb_prev;
-    }
-
-    TEST_ASSERT(0);
-}
-
-/*
- * Recursively verify that the children of each directory are sorted
- * on the directory children linked list by filename length
- */
-static void
-nffs_test_assert_children_sorted(struct nffs_inode_entry *inode_entry)
-{
-    struct nffs_inode_entry *child_entry;
-    struct nffs_inode_entry *prev_entry;
-    struct nffs_inode child_inode;
-    struct nffs_inode prev_inode;
-    int cmp;
-    int rc;
-
-    prev_entry = NULL;
-    SLIST_FOREACH(child_entry, &inode_entry->nie_child_list,
-                  nie_sibling_next) {
-        rc = nffs_inode_from_entry(&child_inode, child_entry);
-        TEST_ASSERT(rc == 0);
-
-        if (prev_entry != NULL) {
-            rc = nffs_inode_from_entry(&prev_inode, prev_entry);
-            TEST_ASSERT(rc == 0);
-
-            rc = nffs_inode_filename_cmp_flash(&prev_inode, &child_inode,
-                                               &cmp);
-            TEST_ASSERT(rc == 0);
-            TEST_ASSERT(cmp < 0);
-        }
-
-        if (nffs_hash_id_is_dir(child_entry->nie_hash_entry.nhe_id)) {
-            nffs_test_assert_children_sorted(child_entry);
-        }
-
-        prev_entry = child_entry;
-    }
-}
-
-static void
-nffs_test_assert_system_once(const struct nffs_test_file_desc *root_dir)
-{
-    struct nffs_inode_entry *inode_entry;
-    struct nffs_hash_entry *entry;
-    struct nffs_hash_entry *next;
-    int i;
-
-    nffs_test_num_touched_entries = 0;
-    nffs_test_assert_file(root_dir, nffs_root_dir, "");
-    nffs_test_assert_branch_touched(nffs_root_dir);
-
-    /* Ensure no orphaned inodes or blocks. */
-    NFFS_HASH_FOREACH(entry, i, next) {
-        TEST_ASSERT(entry->nhe_flash_loc != NFFS_FLASH_LOC_NONE);
-        if (nffs_hash_id_is_inode(entry->nhe_id)) {
-            inode_entry = (void *)entry;
-            TEST_ASSERT(inode_entry->nie_refcnt == 1);
-            if (entry->nhe_id == NFFS_ID_ROOT_DIR) {
-                TEST_ASSERT(inode_entry == nffs_root_dir);
-            } else {
-                nffs_test_assert_child_inode_present(inode_entry);
-            }
-        } else {
-            nffs_test_assert_block_present(entry);
-        }
-    }
-
-    /* Ensure proper sorting. */
-    nffs_test_assert_children_sorted(nffs_root_dir);
-}
-
-static void
-nffs_test_assert_system(const struct nffs_test_file_desc *root_dir,
-                        const struct nffs_area_desc *area_descs)
-{
-    int rc;
-
-    /* Ensure files are as specified, and that there are no other files or
-     * orphaned inodes / blocks.
-     */
-    nffs_test_assert_system_once(root_dir);
-
-    /* Force a garbage collection cycle. */
-    rc = nffs_gc(NULL);
-    TEST_ASSERT(rc == 0);
-
-    /* Ensure file system is still as expected. */
-    nffs_test_assert_system_once(root_dir);
-
-    /* Clear cached data and restore from flash (i.e, simulate a reboot). */
-    rc = nffs_misc_reset();
-    TEST_ASSERT(rc == 0);
-    rc = nffs_detect(area_descs);
-    TEST_ASSERT(rc == 0);
-
-    /* Ensure file system is still as expected. */
-    nffs_test_assert_system_once(root_dir);
-}
-
-static void
-nffs_test_assert_area_seqs(int seq1, int count1, int seq2, int count2)
-{
-    struct nffs_disk_area disk_area;
-    int cur1;
-    int cur2;
-    int rc;
-    int i;
-
-    cur1 = 0;
-    cur2 = 0;
-
-    for (i = 0; i < nffs_num_areas; i++) {
-        rc = nffs_flash_read(i, 0, &disk_area, sizeof disk_area);
-        TEST_ASSERT(rc == 0);
-        TEST_ASSERT(nffs_area_magic_is_set(&disk_area));
-        TEST_ASSERT(disk_area.nda_gc_seq == nffs_areas[i].na_gc_seq);
-        if (i == nffs_scratch_area_idx) {
-            TEST_ASSERT(disk_area.nda_id == NFFS_AREA_ID_NONE);
-        }
-
-        if (nffs_areas[i].na_gc_seq == seq1) {
-            cur1++;
-        } else if (nffs_areas[i].na_gc_seq == seq2) {
-            cur2++;
-        } else {
-            TEST_ASSERT(0);
-        }
-    }
-
-    TEST_ASSERT(cur1 == count1 && cur2 == count2);
-}
-
-static void
-nffs_test_mkdir(void)
-{
-    struct fs_file *file;
-    int rc;
-
-
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_mkdir("/a/b/c/d");
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    rc = fs_mkdir("asdf");
-    TEST_ASSERT(rc == FS_EINVAL);
-
-    rc = fs_mkdir("/a");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_mkdir("/a/b");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_mkdir("/a/b/c");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_mkdir("/a/b/c/d");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_open("/a/b/c/d/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "a",
-                .is_dir = 1,
-                .children = (struct nffs_test_file_desc[]) { {
-                    .filename = "b",
-                    .is_dir = 1,
-                    .children = (struct nffs_test_file_desc[]) { {
-                        .filename = "c",
-                        .is_dir = 1,
-                        .children = (struct nffs_test_file_desc[]) { {
-                            .filename = "d",
-                            .is_dir = 1,
-                            .children = (struct nffs_test_file_desc[]) { {
-                                .filename = "myfile.txt",
-                                .contents = NULL,
-                                .contents_len = 0,
-                            }, {
-                                .filename = NULL,
-                            } },
-                        }, {
-                            .filename = NULL,
-                        } },
-                    }, {
-                        .filename = NULL,
-                    } },
-                }, {
-                    .filename = NULL,
-                } },
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_unlink)
-{
-    struct fs_file *file0;
-    struct fs_file *file1;
-    struct fs_file *file2;
-    struct nffs_file *nfs_file;
-    uint8_t buf[64];
-    uint32_t bytes_read;
-    int initial_num_blocks;
-    int initial_num_inodes;
-    int rc;
-
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    initial_num_blocks = nffs_block_entry_pool.mp_num_free;
-    initial_num_inodes = nffs_inode_entry_pool.mp_num_free;
-
-    nffs_test_util_create_file("/file0.txt", "0", 1);
-
-    rc = fs_open("/file0.txt", FS_ACCESS_READ | FS_ACCESS_WRITE, &file0);
-    TEST_ASSERT(rc == 0);
-    nfs_file = (struct nffs_file *)file0;
-    TEST_ASSERT(nfs_file->nf_inode_entry->nie_refcnt == 2);
-
-    rc = fs_unlink("/file0.txt");
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(nfs_file->nf_inode_entry->nie_refcnt == 1);
-
-    rc = fs_open("/file0.txt", FS_ACCESS_READ, &file2);
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    rc = fs_write(file0, "00", 2);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_seek(file0, 0);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_read(file0, sizeof buf, buf, &bytes_read);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(bytes_read == 2);
-    TEST_ASSERT(memcmp(buf, "00", 2) == 0);
-
-    rc = fs_close(file0);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_open("/file0.txt", FS_ACCESS_READ, &file0);
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    /* Ensure the file was fully removed from RAM. */
-    TEST_ASSERT(nffs_inode_entry_pool.mp_num_free == initial_num_inodes);
-    TEST_ASSERT(nffs_block_entry_pool.mp_num_free == initial_num_blocks);
-
-    /*** Nested unlink. */
-    rc = fs_mkdir("/mydir");
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_create_file("/mydir/file1.txt", "1", 2);
-
-    rc = fs_open("/mydir/file1.txt", FS_ACCESS_READ | FS_ACCESS_WRITE, &file1);
-    TEST_ASSERT(rc == 0);
-    nfs_file = (struct nffs_file *)file1;
-    TEST_ASSERT(nfs_file->nf_inode_entry->nie_refcnt == 2);
-
-    rc = fs_unlink("/mydir");
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(nfs_file->nf_inode_entry->nie_refcnt == 1);
-
-    rc = fs_open("/mydir/file1.txt", FS_ACCESS_READ, &file2);
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    rc = fs_write(file1, "11", 2);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_seek(file1, 0);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_read(file1, sizeof buf, buf, &bytes_read);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(bytes_read == 2);
-    TEST_ASSERT(memcmp(buf, "11", 2) == 0);
-
-    rc = fs_close(file1);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_open("/mydir/file1.txt", FS_ACCESS_READ, &file1);
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-
-    /* Ensure the files and directories were fully removed from RAM. */
-    TEST_ASSERT(nffs_inode_entry_pool.mp_num_free == initial_num_inodes);
-    TEST_ASSERT(nffs_block_entry_pool.mp_num_free == initial_num_blocks);
-}
-
-TEST_CASE(nffs_test_rename)
-{
-    struct fs_file *file;
-    const char contents[] = "contents";
-    int rc;
-
-
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_rename("/nonexistent.txt", "/newname.txt");
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    /*** Rename file. */
-    nffs_test_util_create_file("/myfile.txt", contents, sizeof contents);
-
-    rc = fs_rename("/myfile.txt", "badname");
-    TEST_ASSERT(rc == FS_EINVAL);
-
-    rc = fs_rename("/myfile.txt", "/myfile2.txt");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_open("/myfile.txt", FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    nffs_test_util_assert_contents("/myfile2.txt", contents, sizeof contents);
-
-    rc = fs_mkdir("/mydir");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_mkdir("/mydir/leafdir");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_rename("/myfile2.txt", "/mydir/myfile2.txt");
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/mydir/myfile2.txt", contents,
-                                  sizeof contents);
-
-    /*** Rename directory. */
-    rc = fs_rename("/mydir", "badname");
-    TEST_ASSERT(rc == FS_EINVAL);
-
-    /* Don't allow a directory to be moved into a descendent directory. */
-    rc = fs_rename("/mydir", "/mydir/leafdir/a");
-    TEST_ASSERT(rc == FS_EINVAL);
-
-    rc = fs_rename("/mydir", "/mydir2");
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/mydir2/myfile2.txt", contents,
-                                  sizeof contents);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "mydir2",
-                .is_dir = 1,
-                .children = (struct nffs_test_file_desc[]) { {
-                    .filename = "leafdir",
-                    .is_dir = 1,
-                }, {
-                    .filename = "myfile2.txt",
-                    .contents = "contents",
-                    .contents_len = 9,
-                }, {
-                    .filename = NULL,
-                } },
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_truncate)
-{
-    struct fs_file *file;
-    int rc;
-
-
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE | FS_ACCESS_TRUNCATE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 0);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_write(file, "abcdefgh", 8);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 8);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/myfile.txt", "abcdefgh", 8);
-
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE | FS_ACCESS_TRUNCATE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 0);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_write(file, "1234", 4);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 4);
-    TEST_ASSERT(fs_getpos(file) == 4);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/myfile.txt", "1234", 4);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "myfile.txt",
-                .contents = "1234",
-                .contents_len = 4,
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_append)
-{
-    struct fs_file *file;
-    uint32_t len;
-    char c;
-    int rc;
-    int i;
-
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE | FS_ACCESS_APPEND, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 0);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_write(file, "abcdefgh", 8);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 8);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/myfile.txt", "abcdefgh", 8);
-
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE | FS_ACCESS_APPEND, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 8);
-
-    /* File position should always be at the end of a file after an append.
-     * Seek to the middle prior to writing to test this.
-     */
-    rc = fs_seek(file, 2);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 2);
-
-    rc = fs_write(file, "ijklmnop", 8);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 16);
-    rc = fs_write(file, "qrstuvwx", 8);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 24);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/myfile.txt",
-                                  "abcdefghijklmnopqrstuvwx", 24);
-
-    rc = fs_mkdir("/mydir");
-    TEST_ASSERT_FATAL(rc == 0);
-    rc = fs_open("/mydir/gaga.txt", FS_ACCESS_WRITE | FS_ACCESS_APPEND, &file);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /*** Repeated appends to a large file. */
-    for (i = 0; i < 1000; i++) {
-        rc = fs_filelen(file, &len);
-        TEST_ASSERT_FATAL(rc == 0);
-        TEST_ASSERT(len == i);
-
-        c = '0' + i % 10;
-        rc = fs_write(file, &c, 1);
-        TEST_ASSERT_FATAL(rc == 0);
-    }
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/mydir/gaga.txt",
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789"
-        "01234567890123456789012345678901234567890123456789",
-        1000);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "myfile.txt",
-                .contents = "abcdefghijklmnopqrstuvwx",
-                .contents_len = 24,
-            }, {
-                .filename = "mydir",
-                .is_dir = 1,
-                .children = (struct nffs_test_file_desc[]) { {
-                    .filename = "gaga.txt",
-                    .contents =
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    "01234567890123456789012345678901234567890123456789"
-    ,
-                    .contents_len = 1000,
-                }, {
-                    .filename = NULL,
-                } },
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_read)
-{
-    struct fs_file *file;
-    uint8_t buf[16];
-    uint32_t bytes_read;
-    int rc;
-
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_create_file("/myfile.txt", "1234567890", 10);
-
-    rc = fs_open("/myfile.txt", FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 10);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_read(file, 4, buf, &bytes_read);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(bytes_read == 4);
-    TEST_ASSERT(memcmp(buf, "1234", 4) == 0);
-    TEST_ASSERT(fs_getpos(file) == 4);
-
-    rc = fs_read(file, sizeof buf - 4, buf + 4, &bytes_read);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(bytes_read == 6);
-    TEST_ASSERT(memcmp(buf, "1234567890", 10) == 0);
-    TEST_ASSERT(fs_getpos(file) == 10);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(nffs_test_open)
-{
-    struct fs_file *file;
-    struct fs_dir *dir;
-    int rc;
-
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    /*** Fail to open an invalid path (not rooted). */
-    rc = fs_open("file", FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == FS_EINVAL);
-
-    /*** Fail to open a directory (root directory). */
-    rc = fs_open("/", FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == FS_EINVAL);
-
-    /*** Fail to open a nonexistent file for reading. */
-    rc = fs_open("/1234", FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    /*** Fail to open a child of a nonexistent directory. */
-    rc = fs_open("/dir/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == FS_ENOENT);
-    rc = fs_opendir("/dir", &dir);
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    rc = fs_mkdir("/dir");
-    TEST_ASSERT(rc == 0);
-
-    /*** Fail to open a directory. */
-    rc = fs_open("/dir", FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == FS_EINVAL);
-
-    /*** Successfully open an existing file for reading. */
-    nffs_test_util_create_file("/dir/file.txt", "1234567890", 10);
-    rc = fs_open("/dir/file.txt", FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == 0);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    /*** Successfully open an nonexistent file for writing. */
-    rc = fs_open("/dir/file2.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    /*** Ensure the file can be reopened. */
-    rc = fs_open("/dir/file.txt", FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == 0);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(nffs_test_overwrite_one)
-{
-    struct fs_file *file;
-    int rc;
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_append_file("/myfile.txt", "abcdefgh", 8);
-
-    /*** Overwrite within one block (middle). */
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_seek(file, 3);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 3);
-
-    rc = fs_write(file, "12", 2);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 5);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/myfile.txt", "abc12fgh", 8);
-    nffs_test_util_assert_block_count("/myfile.txt", 1);
-
-    /*** Overwrite within one block (start). */
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_write(file, "xy", 2);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 2);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/myfile.txt", "xyc12fgh", 8);
-    nffs_test_util_assert_block_count("/myfile.txt", 1);
-
-    /*** Overwrite within one block (end). */
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_seek(file, 6);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 6);
-
-    rc = fs_write(file, "<>", 2);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 8);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/myfile.txt", "xyc12f<>", 8);
-    nffs_test_util_assert_block_count("/myfile.txt", 1);
-
-    /*** Overwrite one block middle, extend. */
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_seek(file, 4);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 8);
-    TEST_ASSERT(fs_getpos(file) == 4);
-
-    rc = fs_write(file, "abcdefgh", 8);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 12);
-    TEST_ASSERT(fs_getpos(file) == 12);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/myfile.txt", "xyc1abcdefgh", 12);
-    nffs_test_util_assert_block_count("/myfile.txt", 1);
-
-    /*** Overwrite one block start, extend. */
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 12);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_write(file, "abcdefghijklmnop", 16);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 16);
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents("/myfile.txt", "abcdefghijklmnop", 16);
-    nffs_test_util_assert_block_count("/myfile.txt", 1);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "myfile.txt",
-                .contents = "abcdefghijklmnop",
-                .contents_len = 16,
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_overwrite_two)
-{
-    struct nffs_test_block_desc *blocks = (struct nffs_test_block_desc[]) { {
-        .data = "abcdefgh",
-        .data_len = 8,
-    }, {
-        .data = "ijklmnop",
-        .data_len = 8,
-    } };
-
-    struct fs_file *file;
-    int rc;
-
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    /*** Overwrite two blocks (middle). */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 2);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_seek(file, 7);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 7);
-
-    rc = fs_write(file, "123", 3);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 10);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt", "abcdefg123klmnop", 16);
-    nffs_test_util_assert_block_count("/myfile.txt", 2);
-
-    /*** Overwrite two blocks (start). */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 2);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_write(file, "ABCDEFGHIJ", 10);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 10);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt", "ABCDEFGHIJklmnop", 16);
-    nffs_test_util_assert_block_count("/myfile.txt", 2);
-
-    /*** Overwrite two blocks (end). */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 2);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_seek(file, 6);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 6);
-
-    rc = fs_write(file, "1234567890", 10);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 16);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt", "abcdef1234567890", 16);
-    nffs_test_util_assert_block_count("/myfile.txt", 2);
-
-    /*** Overwrite two blocks middle, extend. */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 2);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_seek(file, 6);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 6);
-
-    rc = fs_write(file, "1234567890!@#$", 14);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 20);
-    TEST_ASSERT(fs_getpos(file) == 20);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt", "abcdef1234567890!@#$", 20);
-    nffs_test_util_assert_block_count("/myfile.txt", 2);
-
-    /*** Overwrite two blocks start, extend. */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 2);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 16);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_write(file, "1234567890!@#$%^&*()", 20);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 20);
-    TEST_ASSERT(fs_getpos(file) == 20);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt", "1234567890!@#$%^&*()", 20);
-    nffs_test_util_assert_block_count("/myfile.txt", 2);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "myfile.txt",
-                .contents = "1234567890!@#$%^&*()",
-                .contents_len = 20,
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_overwrite_three)
-{
-    struct nffs_test_block_desc *blocks = (struct nffs_test_block_desc[]) { {
-        .data = "abcdefgh",
-        .data_len = 8,
-    }, {
-        .data = "ijklmnop",
-        .data_len = 8,
-    }, {
-        .data = "qrstuvwx",
-        .data_len = 8,
-    } };
-
-    struct fs_file *file;
-    int rc;
-
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    /*** Overwrite three blocks (middle). */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_seek(file, 6);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 6);
-
-    rc = fs_write(file, "1234567890!@", 12);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 18);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt",
-                                   "abcdef1234567890!@stuvwx", 24);
-    nffs_test_util_assert_block_count("/myfile.txt", 3);
-
-    /*** Overwrite three blocks (start). */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_write(file, "1234567890!@#$%^&*()", 20);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 20);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt",
-                                   "1234567890!@#$%^&*()uvwx", 24);
-    nffs_test_util_assert_block_count("/myfile.txt", 3);
-
-    /*** Overwrite three blocks (end). */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_seek(file, 6);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 6);
-
-    rc = fs_write(file, "1234567890!@#$%^&*", 18);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 24);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt",
-                                   "abcdef1234567890!@#$%^&*", 24);
-    nffs_test_util_assert_block_count("/myfile.txt", 3);
-
-    /*** Overwrite three blocks middle, extend. */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_seek(file, 6);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 6);
-
-    rc = fs_write(file, "1234567890!@#$%^&*()", 20);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 26);
-    TEST_ASSERT(fs_getpos(file) == 26);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt",
-                                   "abcdef1234567890!@#$%^&*()", 26);
-    nffs_test_util_assert_block_count("/myfile.txt", 3);
-
-    /*** Overwrite three blocks start, extend. */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_write(file, "1234567890!@#$%^&*()abcdefghij", 30);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 30);
-    TEST_ASSERT(fs_getpos(file) == 30);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt",
-                                   "1234567890!@#$%^&*()abcdefghij", 30);
-    nffs_test_util_assert_block_count("/myfile.txt", 3);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "myfile.txt",
-                .contents = "1234567890!@#$%^&*()abcdefghij",
-                .contents_len = 30,
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_overwrite_many)
-{
-    struct nffs_test_block_desc *blocks = (struct nffs_test_block_desc[]) { {
-        .data = "abcdefgh",
-        .data_len = 8,
-    }, {
-        .data = "ijklmnop",
-        .data_len = 8,
-    }, {
-        .data = "qrstuvwx",
-        .data_len = 8,
-    } };
-
-    struct fs_file *file;
-    int rc;
-
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    /*** Overwrite middle of first block. */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_seek(file, 3);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 3);
-
-    rc = fs_write(file, "12", 2);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 5);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt",
-                                   "abc12fghijklmnopqrstuvwx", 24);
-    nffs_test_util_assert_block_count("/myfile.txt", 3);
-
-    /*** Overwrite end of first block, start of second. */
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 3);
-    rc = fs_open("/myfile.txt", FS_ACCESS_WRITE, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 0);
-
-    rc = fs_seek(file, 6);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 6);
-
-    rc = fs_write(file, "1234", 4);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_file_len(file, 24);
-    TEST_ASSERT(fs_getpos(file) == 10);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_contents( "/myfile.txt",
-                                   "abcdef1234klmnopqrstuvwx", 24);
-    nffs_test_util_assert_block_count("/myfile.txt", 3);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "myfile.txt",
-                .contents = "abcdef1234klmnopqrstuvwx",
-                .contents_len = 24,
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_long_filename)
-{
-    int rc;
-
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_create_file("/12345678901234567890.txt", "contents", 8);
-
-    rc = fs_mkdir("/longdir12345678901234567890");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_rename("/12345678901234567890.txt",
-                    "/longdir12345678901234567890/12345678901234567890.txt");
-    TEST_ASSERT(rc == 0);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "longdir12345678901234567890",
-                .is_dir = 1,
-                .children = (struct nffs_test_file_desc[]) { {
-                    .filename = "/12345678901234567890.txt",
-                    .contents = "contents",
-                    .contents_len = 8,
-                }, {
-                    .filename = NULL,
-                } },
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_large_write)
-{
-    static char data[NFFS_BLOCK_MAX_DATA_SZ_MAX * 5];
-    int rc;
-    int i;
-
-    static const struct nffs_area_desc area_descs_two[] = {
-        { 0x00020000, 128 * 1024 },
-        { 0x00040000, 128 * 1024 },
-        { 0, 0 },
-    };
-
-
-
-    /*** Setup. */
-    rc = nffs_format(area_descs_two);
-    TEST_ASSERT(rc == 0);
-
-    for (i = 0; i < sizeof data; i++) {
-        data[i] = i;
-    }
-
-    nffs_test_util_create_file("/myfile.txt", data, sizeof data);
-
-    /* Ensure large write was split across the appropriate number of data
-     * blocks.
-     */
-    TEST_ASSERT(nffs_test_util_block_count("/myfile.txt") ==
-           sizeof data / NFFS_BLOCK_MAX_DATA_SZ_MAX);
-
-    /* Garbage collect and then ensure the large file is still properly divided
-     * according to max data block size.
-     */
-    nffs_gc(NULL);
-    TEST_ASSERT(nffs_test_util_block_count("/myfile.txt") ==
-           sizeof data / NFFS_BLOCK_MAX_DATA_SZ_MAX);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "myfile.txt",
-                .contents = data,
-                .contents_len = sizeof data,
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, area_descs_two);
-}
-
-TEST_CASE(nffs_test_many_children)
-{
-    int rc;
-
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_create_file("/zasdf", NULL, 0);
-    nffs_test_util_create_file("/FfD", NULL, 0);
-    nffs_test_util_create_file("/4Zvv", NULL, 0);
-    nffs_test_util_create_file("/*(*2fs", NULL, 0);
-    nffs_test_util_create_file("/pzzd", NULL, 0);
-    nffs_test_util_create_file("/zasdf0", NULL, 0);
-    nffs_test_util_create_file("/23132.bin", NULL, 0);
-    nffs_test_util_create_file("/asldkfjaldskfadsfsdf.txt", NULL, 0);
-    nffs_test_util_create_file("/sdgaf", NULL, 0);
-    nffs_test_util_create_file("/939302**", NULL, 0);
-    rc = fs_mkdir("/dir");
-    nffs_test_util_create_file("/dir/itw82", NULL, 0);
-    nffs_test_util_create_file("/dir/124", NULL, 0);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) {
-                { "zasdf" },
-                { "FfD" },
-                { "4Zvv" },
-                { "*(*2fs" },
-                { "pzzd" },
-                { "zasdf0" },
-                { "23132.bin" },
-                { "asldkfjaldskfadsfsdf.txt" },
-                { "sdgaf" },
-                { "939302**" },
-                {
-                    .filename = "dir",
-                    .is_dir = 1,
-                    .children = (struct nffs_test_file_desc[]) {
-                        { "itw82" },
-                        { "124" },
-                        { NULL },
-                    },
-                },
-                { NULL },
-            }
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_gc)
-{
-    int rc;
-
-    static const struct nffs_area_desc area_descs_two[] = {
-        { 0x00020000, 128 * 1024 },
-        { 0x00040000, 128 * 1024 },
-        { 0, 0 },
-    };
-
-    struct nffs_test_block_desc blocks[8] = { {
-        .data = "1",
-        .data_len = 1,
-    }, {
-        .data = "2",
-        .data_len = 1,
-    }, {
-        .data = "3",
-        .data_len = 1,
-    }, {
-        .data = "4",
-        .data_len = 1,
-    }, {
-        .data = "5",
-        .data_len = 1,
-    }, {
-        .data = "6",
-        .data_len = 1,
-    }, {
-        .data = "7",
-        .data_len = 1,
-    }, {
-        .data = "8",
-        .data_len = 1,
-    } };
-
-
-    rc = nffs_format(area_descs_two);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 8);
-
-    nffs_gc(NULL);
-
-    nffs_test_util_assert_block_count("/myfile.txt", 1);
-}
-
-TEST_CASE(nffs_test_wear_level)
-{
-    int rc;
-    int i;
-    int j;
-
-    static const struct nffs_area_desc area_descs_uniform[] = {
-        { 0x00000000, 2 * 1024 },
-        { 0x00020000, 2 * 1024 },
-        { 0x00040000, 2 * 1024 },
-        { 0x00060000, 2 * 1024 },
-        { 0x00080000, 2 * 1024 },
-        { 0, 0 },
-    };
-
-
-    /*** Setup. */
-    rc = nffs_format(area_descs_uniform);
-    TEST_ASSERT(rc == 0);
-
-    /* Ensure areas rotate properly. */
-    for (i = 0; i < 255; i++) {
-        for (j = 0; j < nffs_num_areas; j++) {
-            nffs_test_assert_area_seqs(i, nffs_num_areas - j, i + 1, j);
-            nffs_gc(NULL);
-        }
-    }
-
-    /* Ensure proper rollover of sequence numbers. */
-    for (j = 0; j < nffs_num_areas; j++) {
-        nffs_test_assert_area_seqs(255, nffs_num_areas - j, 0, j);
-        nffs_gc(NULL);
-    }
-    for (j = 0; j < nffs_num_areas; j++) {
-        nffs_test_assert_area_seqs(0, nffs_num_areas - j, 1, j);
-        nffs_gc(NULL);
-    }
-}
-
-TEST_CASE(nffs_test_corrupt_scratch)
-{
-    int non_scratch_id;
-    int scratch_id;
-    int rc;
-
-    static const struct nffs_area_desc area_descs_two[] = {
-        { 0x00020000, 128 * 1024 },
-        { 0x00040000, 128 * 1024 },
-        { 0, 0 },
-    };
-
-
-    /*** Setup. */
-    rc = nffs_format(area_descs_two);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_create_file("/myfile.txt", "contents", 8);
-
-    /* Copy the current contents of the non-scratch area to the scratch area.
-     * This will make the scratch area look like it only partially participated
-     * in a garbage collection cycle.
-     */
-    scratch_id = nffs_scratch_area_idx;
-    non_scratch_id = scratch_id ^ 1;
-    nffs_test_copy_area(area_descs_two + non_scratch_id,
-                       area_descs_two + nffs_scratch_area_idx);
-
-    /* Add some more data to the non-scratch area. */
-    rc = fs_mkdir("/mydir");
-    TEST_ASSERT(rc == 0);
-
-    /* Ensure the file system is successfully detected and valid, despite
-     * corruption.
-     */
-
-    rc = nffs_misc_reset();
-    TEST_ASSERT(rc == 0);
-
-    rc = nffs_detect(area_descs_two);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(nffs_scratch_area_idx == scratch_id);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "mydir",
-                .is_dir = 1,
-            }, {
-                .filename = "myfile.txt",
-                .contents = "contents",
-                .contents_len = 8,
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, area_descs_two);
-}
-
-/*
- * This test no longer works with the current implementation. The
- * expectation is that intermediate blocks can be removed and the old
- * method of finding the last current block after restore will allow the
- * file to be salvaged. Instead, the file should be removed and all data
- * declared invalid.
- */
-TEST_CASE(nffs_test_incomplete_block)
-{
-    struct nffs_block block;
-    struct fs_file *fs_file;
-    struct nffs_file *file;
-    uint32_t flash_offset;
-    uint32_t area_offset;
-    uint8_t area_idx;
-    int rc;
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_mkdir("/mydir");
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_create_file("/mydir/a", "aaaa", 4);
-    nffs_test_util_create_file("/mydir/b", "bbbb", 4);
-    nffs_test_util_create_file("/mydir/c", "cccc", 4);
-
-    /* Add a second block to the 'b' file. */
-    nffs_test_util_append_file("/mydir/b", "1234", 4);
-
-    /* Corrupt the 'b' file; make it look like the second block only got half
-     * written.
-     */
-    rc = fs_open("/mydir/b", FS_ACCESS_READ, &fs_file);
-    TEST_ASSERT(rc == 0);
-    file = (struct nffs_file *)fs_file;
-
-    rc = nffs_block_from_hash_entry(&block,
-                                   file->nf_inode_entry->nie_last_block_entry);
-    TEST_ASSERT(rc == 0);
-
-    nffs_flash_loc_expand(block.nb_hash_entry->nhe_flash_loc, &area_idx,
-                         &area_offset);
-    flash_offset = nffs_areas[area_idx].na_offset + area_offset;
-    /*
-     * Overwrite block data - the CRC check should pick this up
-     */
-    rc = flash_native_memset(
-            flash_offset + sizeof (struct nffs_disk_block) + 2, 0xff, 2);
-    TEST_ASSERT(rc == 0);
-
-    rc = nffs_misc_reset();
-    TEST_ASSERT(rc == 0);
-    rc = nffs_detect(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    /* OLD: The entire second block should be removed; the file should only
-     * contain the first block.
-     * Unless we can salvage the block, the entire file should probably be
-     * removed. This is a contrived example which generates bad data on the
-     * what happens to be the last block, but corruption can actually occur
-     * in any block. Sweep should be updated to search look for blocks that
-     * don't have a correct prev_id and then decide whether to delete the
-     * owning inode. XXX
-     */
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "mydir",
-                .is_dir = 1,
-                .children = (struct nffs_test_file_desc[]) { {
-                    .filename = "a",
-                    .contents = "aaaa",
-                    .contents_len = 4,
-#if 0
-/* keep this out until sweep updated to capture bad blocks XXX */
-                }, {
-                    .filename = "b",
-                    .contents = "bbbb",
-                    .contents_len = 4,
-#endif
-                }, {
-                    .filename = "c",
-                    .contents = "cccc",
-                    .contents_len = 4,
-                }, {
-                    .filename = NULL,
-                } },
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_corrupt_block)
-{
-    struct nffs_block block;
-    struct fs_file *fs_file;
-    struct nffs_file *file;
-    uint32_t flash_offset;
-    uint32_t area_offset;
-    uint8_t area_idx;
-    uint8_t off;    /* offset to corrupt */
-    int rc;
-    struct nffs_disk_block ndb;
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_mkdir("/mydir");
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_create_file("/mydir/a", "aaaa", 4);
-    nffs_test_util_create_file("/mydir/b", "bbbb", 4);
-    nffs_test_util_create_file("/mydir/c", "cccc", 4);
-
-    /* Add a second block to the 'b' file. */
-    nffs_test_util_append_file("/mydir/b", "1234", 4);
-
-    /* Corrupt the 'b' file; overwrite the second block's magic number. */
-    rc = fs_open("/mydir/b", FS_ACCESS_READ, &fs_file);
-    TEST_ASSERT(rc == 0);
-    file = (struct nffs_file *)fs_file;
-
-    rc = nffs_block_from_hash_entry(&block,
-                                   file->nf_inode_entry->nie_last_block_entry);
-    TEST_ASSERT(rc == 0);
-
-    nffs_flash_loc_expand(block.nb_hash_entry->nhe_flash_loc, &area_idx,
-                         &area_offset);
-    flash_offset = nffs_areas[area_idx].na_offset + area_offset;
-
-    /*
-     * Overwriting the reserved16 field should invalidate the CRC
-     */
-    off = (char*)&ndb.reserved16 - (char*)&ndb;
-    rc = flash_native_memset(flash_offset + off, 0x43, 1);
-
-    TEST_ASSERT(rc == 0);
-
-    /* Write a fourth file. This file should get restored even though the
-     * previous object has an invalid magic number.
-     */
-    nffs_test_util_create_file("/mydir/d", "dddd", 4);
-
-    rc = nffs_misc_reset();
-    TEST_ASSERT(rc == 0);
-    rc = nffs_detect(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    /* The entire second block should be removed; the file should only contain
-     * the first block.
-     */
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "mydir",
-                .is_dir = 1,
-                .children = (struct nffs_test_file_desc[]) { {
-                    .filename = "a",
-                    .contents = "aaaa",
-                    .contents_len = 4,
-#if 0
-                /*
-                 * In the newer implementation without the find_file_ends
-                 * corrupted inodes are deleted rather than retained with
-                 * partial contents
-                 */
-                }, {
-                    .filename = "b",
-                    .contents = "bbbb",
-                    .contents_len = 4,
-#endif
-                }, {
-                    .filename = "c",
-                    .contents = "cccc",
-                    .contents_len = 4,
-                }, {
-                    .filename = "d",
-                    .contents = "dddd",
-                    .contents_len = 4,
-                }, {
-                    .filename = NULL,
-                } },
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_large_unlink)
-{
-    static char file_contents[1024 * 4];
-    char filename[256];
-    int rc;
-    int i;
-    int j;
-    int k;
-
-
-    /*** Setup. */
-    nffs_config.nc_num_inodes = 1024;
-    nffs_config.nc_num_blocks = 1024;
-
-    rc = nffs_init();
-    TEST_ASSERT(rc == 0);
-
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    for (i = 0; i < 5; i++) {
-        snprintf(filename, sizeof filename, "/dir0_%d", i);
-        rc = fs_mkdir(filename);
-        TEST_ASSERT(rc == 0);
-
-        for (j = 0; j < 5; j++) {
-            snprintf(filename, sizeof filename, "/dir0_%d/dir1_%d", i, j);
-            rc = fs_mkdir(filename);
-            TEST_ASSERT(rc == 0);
-
-            for (k = 0; k < 5; k++) {
-                snprintf(filename, sizeof filename,
-                         "/dir0_%d/dir1_%d/file2_%d", i, j, k);
-                nffs_test_util_create_file(filename, file_contents,
-                                          sizeof file_contents);
-            }
-        }
-
-        for (j = 0; j < 15; j++) {
-            snprintf(filename, sizeof filename, "/dir0_%d/file1_%d", i, j);
-            nffs_test_util_create_file(filename, file_contents,
-                                      sizeof file_contents);
-        }
-    }
-
-    for (i = 0; i < 5; i++) {
-        snprintf(filename, sizeof filename, "/dir0_%d", i);
-        rc = fs_unlink(filename);
-        TEST_ASSERT(rc == 0);
-    }
-
-    /* The entire file system should be empty. */
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_large_system)
-{
-    int rc;
-
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_create_tree(nffs_test_system_01);
-
-    nffs_test_assert_system(nffs_test_system_01, nffs_area_descs);
-
-    rc = fs_unlink("/lvl1dir-0000");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_unlink("/lvl1dir-0004");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_mkdir("/lvl1dir-0000");
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_assert_system(nffs_test_system_01_rm_1014_mk10, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_lost_found)
-{
-    char buf[32];
-    struct nffs_inode_entry *inode_entry;
-    uint32_t flash_offset;
-    uint32_t area_offset;
-    uint8_t area_idx;
-    int rc;
-    struct nffs_disk_inode ndi;
-    uint8_t off;    /* calculated offset for memset */
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_mkdir("/mydir");
-    TEST_ASSERT(rc == 0);
-    rc = fs_mkdir("/mydir/dir1");
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_create_file("/mydir/file1", "aaaa", 4);
-    nffs_test_util_create_file("/mydir/dir1/file2", "bbbb", 4);
-
-    /* Corrupt the mydir inode. */
-    rc = nffs_path_find_inode_entry("/mydir", &inode_entry);
-    TEST_ASSERT(rc == 0);
-
-    snprintf(buf, sizeof buf, "%lu",
-             (unsigned long)inode_entry->nie_hash_entry.nhe_id);
-
-    nffs_flash_loc_expand(inode_entry->nie_hash_entry.nhe_flash_loc,
-                         &area_idx, &area_offset);
-    flash_offset = nffs_areas[area_idx].na_offset + area_offset;
-    /*
-     * Overwrite the sequence number - should be detected as CRC corruption
-     */
-    off = (char*)&ndi.ndi_seq - (char*)&ndi;
-    rc = flash_native_memset(flash_offset + off, 0xaa, 1);
-    TEST_ASSERT(rc == 0);
-
-    /* Clear cached data and restore from flash (i.e, simulate a reboot). */
-    rc = nffs_misc_reset();
-    TEST_ASSERT(rc == 0);
-    rc = nffs_detect(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    /* All contents should now be in the lost+found dir. */
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "lost+found",
-                .is_dir = 1,
-#if 0
-                .children = (struct nffs_test_file_desc[]) { {
-                    .filename = buf,
-                    .is_dir = 1,
-                    .children = (struct nffs_test_file_desc[]) { {
-                        .filename = "file1",
-                        .contents = "aaaa",
-                        .contents_len = 4,
-                    }, {
-                        .filename = "dir1",
-                        .is_dir = 1,
-                        .children = (struct nffs_test_file_desc[]) { {
-                            .filename = "file2",
-                            .contents = "bbbb",
-                            .contents_len = 4,
-                        }, {
-                            .filename = NULL,
-                        } },
-                    }, {
-                        .filename = NULL,
-                    } },
-                }, {
-                    .filename = NULL,
-                } },
-#endif
-            }, {
-                .filename = NULL,
-            } }
-    } };
-
-    nffs_test_assert_system(expected_system, nffs_area_descs);
-}
-
-TEST_CASE(nffs_test_cache_large_file)
-{
-    static char data[NFFS_BLOCK_MAX_DATA_SZ_MAX * 5];
-    struct fs_file *file;
-    uint8_t b;
-    int rc;
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_create_file("/myfile.txt", data, sizeof data);
-    nffs_cache_clear();
-
-    /* Opening a file should not cause any blocks to get cached. */
-    rc = fs_open("/myfile.txt", FS_ACCESS_READ, &file);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_cache_range("/myfile.txt", 0, 0);
-
-    /* Cache first block. */
-    rc = fs_seek(file, nffs_block_max_data_sz * 0);
-    TEST_ASSERT(rc == 0);
-    rc = fs_read(file, 1, &b, NULL);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_cache_range("/myfile.txt",
-                                     nffs_block_max_data_sz * 0,
-                                     nffs_block_max_data_sz * 1);
-
-    /* Cache second block. */
-    rc = fs_seek(file, nffs_block_max_data_sz * 1);
-    TEST_ASSERT(rc == 0);
-    rc = fs_read(file, 1, &b, NULL);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_cache_range("/myfile.txt",
-                                     nffs_block_max_data_sz * 0,
-                                     nffs_block_max_data_sz * 2);
-
-
-    /* Cache fourth block; prior cache should get erased. */
-    rc = fs_seek(file, nffs_block_max_data_sz * 3);
-    TEST_ASSERT(rc == 0);
-    rc = fs_read(file, 1, &b, NULL);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_cache_range("/myfile.txt",
-                                     nffs_block_max_data_sz * 3,
-                                     nffs_block_max_data_sz * 4);
-
-    /* Cache second and third blocks. */
-    rc = fs_seek(file, nffs_block_max_data_sz * 1);
-    TEST_ASSERT(rc == 0);
-    rc = fs_read(file, 1, &b, NULL);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_cache_range("/myfile.txt",
-                                     nffs_block_max_data_sz * 1,
-                                     nffs_block_max_data_sz * 4);
-
-    /* Cache fifth block. */
-    rc = fs_seek(file, nffs_block_max_data_sz * 4);
-    TEST_ASSERT(rc == 0);
-    rc = fs_read(file, 1, &b, NULL);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_cache_range("/myfile.txt",
-                                     nffs_block_max_data_sz * 1,
-                                     nffs_block_max_data_sz * 5);
-
-    rc = fs_close(file);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(nffs_test_readdir)
-{
-    struct fs_dirent *dirent;
-    struct fs_dir *dir;
-    int rc;
-
-    /*** Setup. */
-    rc = nffs_format(nffs_area_descs);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    rc = fs_mkdir("/mydir");
-    TEST_ASSERT_FATAL(rc == 0);
-
-    nffs_test_util_create_file("/mydir/b", "bbbb", 4);
-    nffs_test_util_create_file("/mydir/a", "aaaa", 4);
-    rc = fs_mkdir("/mydir/c");
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Nonexistent directory. */
-    rc = fs_opendir("/asdf", &dir);
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    /* Fail to opendir a file. */
-    rc = fs_opendir("/mydir/a", &dir);
-    TEST_ASSERT(rc == FS_EINVAL);
-
-    /* Real directory (with trailing slash). */
-    rc = fs_opendir("/mydir/", &dir);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    rc = fs_readdir(dir, &dirent);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_ent_name(dirent, "a");
-    TEST_ASSERT(fs_dirent_is_dir(dirent) == 0);
-
-    rc = fs_readdir(dir, &dirent);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_ent_name(dirent, "b");
-    TEST_ASSERT(fs_dirent_is_dir(dirent) == 0);
-
-    rc = fs_readdir(dir, &dirent);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_ent_name(dirent, "c");
-    TEST_ASSERT(fs_dirent_is_dir(dirent) == 1);
-
-    rc = fs_readdir(dir, &dirent);
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    rc = fs_closedir(dir);
-    TEST_ASSERT(rc == 0);
-
-    /* Root directory. */
-    rc = fs_opendir("/", &dir);
-    TEST_ASSERT(rc == 0);
-    rc = fs_readdir(dir, &dirent);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_ent_name(dirent, "lost+found");
-    TEST_ASSERT(fs_dirent_is_dir(dirent) == 1);
-
-    rc = fs_readdir(dir, &dirent);
-    TEST_ASSERT(rc == 0);
-    nffs_test_util_assert_ent_name(dirent, "mydir");
-    TEST_ASSERT(fs_dirent_is_dir(dirent) == 1);
-
-    rc = fs_closedir(dir);
-    TEST_ASSERT(rc == 0);
-
-    /* Delete entries while iterating. */
-    rc = fs_opendir("/mydir", &dir);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    rc = fs_readdir(dir, &dirent);
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_ent_name(dirent, "a");
-    TEST_ASSERT(fs_dirent_is_dir(dirent) == 0);
-
-    rc = fs_unlink("/mydir/b");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_readdir(dir, &dirent);
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_unlink("/mydir/c");
-    TEST_ASSERT(rc == 0);
-
-    rc = fs_unlink("/mydir");
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_util_assert_ent_name(dirent, "c");
-    TEST_ASSERT(fs_dirent_is_dir(dirent) == 1);
-
-    rc = fs_readdir(dir, &dirent);
-    TEST_ASSERT(rc == FS_ENOENT);
-
-    rc = fs_closedir(dir);
-    TEST_ASSERT(rc == 0);
-
-    /* Ensure directory is gone. */
-    rc = fs_opendir("/mydir", &dir);
-    TEST_ASSERT(rc == FS_ENOENT);
-}
-
-TEST_CASE(nffs_test_split_file)
-{
-    static char data[24 * 1024];
-    int rc;
-    int i;
-
-    /*** Setup. */
-    static const struct nffs_area_desc area_descs_two[] = {
-            { 0x00000000, 16 * 1024 },
-            { 0x00004000, 16 * 1024 },
-            { 0x00008000, 16 * 1024 },
-            { 0, 0 },
-    };
-
-    rc = nffs_format(area_descs_two);
-    TEST_ASSERT(rc == 0);
-
-    for (i = 0; i < sizeof data; i++) {
-        data[i] = i;
-    }
-
-    for (i = 0; i < 256; i++) {
-        nffs_test_util_create_file("/myfile.txt", data, sizeof data);
-        rc = fs_unlink("/myfile.txt");
-        TEST_ASSERT(rc == 0);
-    }
-
-    nffs_test_util_create_file("/myfile.txt", data, sizeof data);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "myfile.txt",
-                .contents = data,
-                .contents_len = sizeof data,
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, area_descs_two);
-}
-
-TEST_CASE(nffs_test_gc_on_oom)
-{
-    int rc;
-
-    /*** Setup. */
-    /* Ensure all areas are the same size. */
-    static const struct nffs_area_desc area_descs_two[] = {
-            { 0x00000000, 16 * 1024 },
-            { 0x00004000, 16 * 1024 },
-            { 0x00008000, 16 * 1024 },
-            { 0, 0 },
-    };
-
-    rc = nffs_format(area_descs_two);
-    TEST_ASSERT_FATAL(rc == 0);
-
-    /* Leak block entries until only four are left. */
-    /* XXX: This is ridiculous.  Need to fix nffs configuration so that the
-     * caller passes a config object rather than writing to a global variable.
-     */
-    while (nffs_block_entry_pool.mp_num_free != 4) {
-        nffs_block_entry_alloc();
-    }
-
-    /*** Write 4 data blocks. */
-    struct nffs_test_block_desc blocks[4] = { {
-        .data = "1",
-        .data_len = 1,
-    }, {
-        .data = "2",
-        .data_len = 1,
-    }, {
-        .data = "3",
-        .data_len = 1,
-    }, {
-        .data = "4",
-        .data_len = 1,
-    } };
-
-    nffs_test_util_create_file_blocks("/myfile.txt", blocks, 4);
-
-    TEST_ASSERT_FATAL(nffs_block_entry_pool.mp_num_free == 0);
-
-    /* Attempt another one-byte write.  This should trigger a garbage
-     * collection cycle, resulting in the four blocks being collated.  The
-     * fifth write consumes an additional block, resulting in 2 out of 4 blocks
-     * in use.
-     */
-    nffs_test_util_append_file("/myfile.txt", "5", 1);
-
-    TEST_ASSERT_FATAL(nffs_block_entry_pool.mp_num_free == 2);
-
-    struct nffs_test_file_desc *expected_system =
-        (struct nffs_test_file_desc[]) { {
-            .filename = "",
-            .is_dir = 1,
-            .children = (struct nffs_test_file_desc[]) { {
-                .filename = "myfile.txt",
-                .contents = "12345",
-                .contents_len = 5,
-            }, {
-                .filename = NULL,
-            } },
-    } };
-
-    nffs_test_assert_system(expected_system, area_descs_two);
-}
-
-TEST_SUITE(nffs_suite_cache)
-{
-    int rc;
-
-    memset(&nffs_config, 0, sizeof nffs_config);
-    nffs_config.nc_num_cache_inodes = 4;
-    nffs_config.nc_num_cache_blocks = 64;
-
-    rc = nffs_init();
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_cache_large_file();
-}
-
-static void
-nffs_test_gen(void)
-{
-    int rc;
-
-    rc = nffs_init();
-    TEST_ASSERT(rc == 0);
-
-    nffs_test_unlink();
-    nffs_test_mkdir();
-    nffs_test_rename();
-    nffs_test_truncate();
-    nffs_test_append();
-    nffs_test_read();
-    nffs_test_open();
-    nffs_test_overwrite_one();
-    nffs_test_overwrite_two();
-    nffs_test_overwrite_three();
-    nffs_test_overwrite_many();
-    nffs_test_long_filename();
-    nffs_test_large_write();
-    nffs_test_many_children();
-    nffs_test_gc();
-    nffs_test_wear_level();
-    nffs_test_corrupt_scratch();
-    nffs_test_incomplete_block();
-    nffs_test_corrupt_block();
-    nffs_test_large_unlink();
-    nffs_test_large_system();
-    nffs_test_lost_found();
-    nffs_test_readdir();
-    nffs_test_split_file();
-    nffs_test_gc_on_oom();
-}
-
-TEST_SUITE(gen_1_1)
-{
-    nffs_config.nc_num_cache_inodes = 1;
-    nffs_config.nc_num_cache_blocks = 1;
-    nffs_test_gen();
-}
-
-TEST_SUITE(gen_4_32)
-{
-    nffs_config.nc_num_cache_inodes = 4;
-    nffs_config.nc_num_cache_blocks = 32;
-    nffs_test_gen();
-}
-
-TEST_SUITE(gen_32_1024)
-{
-    nffs_config.nc_num_cache_inodes = 32;
-    nffs_config.nc_num_cache_blocks = 1024;
-    nffs_test_gen();
-}
-
-int
-nffs_test_all(void)
-{
-    nffs_config.nc_num_inodes = 1024 * 8;
-    nffs_config.nc_num_blocks = 1024 * 20;
-
-    gen_1_1();
-    gen_4_32();
-    gen_32_1024();
-    nffs_suite_cache();
-
-    return tu_any_failed;
-}
-
-void
-print_inode_entry(struct nffs_inode_entry *inode_entry, int indent)
-{
-    struct nffs_inode inode;
-    char name[NFFS_FILENAME_MAX_LEN + 1];
-    uint32_t area_offset;
-    uint8_t area_idx;
-    int rc;
-
-    if (inode_entry == nffs_root_dir) {
-        printf("%*s/\n", indent, "");
-        return;
-    }
-
-    rc = nffs_inode_from_entry(&inode, inode_entry);
-    /*
-     * Dummy inode
-     */
-    if (rc == FS_ENOENT) {
-        printf("    DUMMY %d\n", rc);
-        return;
-    }
-
-    nffs_flash_loc_expand(inode_entry->nie_hash_entry.nhe_flash_loc,
-                         &area_idx, &area_offset);
-
-    rc = nffs_flash_read(area_idx,
-                         area_offset + sizeof (struct nffs_disk_inode),
-                         name, inode.ni_filename_len);
-
-    name[inode.ni_filename_len] = '\0';
-
-    printf("%*s%s\n", indent, "", name[0] == '\0' ? "/" : name);
-}
-
-void
-process_inode_entry(struct nffs_inode_entry *inode_entry, int indent)
-{
-    struct nffs_inode_entry *child;
-
-    print_inode_entry(inode_entry, indent);
-
-    if (nffs_hash_id_is_dir(inode_entry->nie_hash_entry.nhe_id)) {
-        SLIST_FOREACH(child, &inode_entry->nie_child_list, nie_sibling_next) {
-            process_inode_entry(child, indent + 2);
-        }
-    }
-}
-
-int
-print_nffs_flash_inode(struct nffs_area *area, uint32_t off)
-{
-    struct nffs_disk_inode ndi;
-    char filename[128];
-    int len;
-    int rc;
-
-    rc = hal_flash_read(area->na_flash_id, area->na_offset + off,
-                         &ndi, sizeof(ndi));
-    assert(rc == 0);
-
-    memset(filename, 0, sizeof(filename));
-    len = min(sizeof(filename) - 1, ndi.ndi_filename_len);
-    rc = hal_flash_read(area->na_flash_id, area->na_offset + off + sizeof(ndi),
-                         filename, len);
-
-    printf("  off %x %s id %x flen %d seq %d last %x prnt %x flgs %x %s\n",
-           off,
-           (nffs_hash_id_is_file(ndi.ndi_id) ? "File" :
-            (nffs_hash_id_is_dir(ndi.ndi_id) ? "Dir" : "???")),
-           ndi.ndi_id,
-           ndi.ndi_filename_len,
-           ndi.ndi_seq,
-           ndi.ndi_lastblock_id,
-           ndi.ndi_parent_id,
-           ndi.ndi_flags,
-           filename);
-    return sizeof(ndi) + ndi.ndi_filename_len;
-}
-
-int
-print_nffs_flash_block(struct nffs_area *area, uint32_t off)
-{
-    struct nffs_disk_block ndb;
-    int rc;
-
-    rc = hal_flash_read(area->na_flash_id, area->na_offset + off,
-                        &ndb, sizeof(ndb));
-    assert(rc == 0);
-
-    printf("  off %x Block id %x len %d seq %d prev %x own ino %x\n",
-           off,
-           ndb.ndb_id,
-           ndb.ndb_data_len,
-           ndb.ndb_seq,
-           ndb.ndb_prev_id,
-           ndb.ndb_inode_id);
-    return sizeof(ndb) + ndb.ndb_data_len;
-}
-
-int
-print_nffs_flash_object(struct nffs_area *area, uint32_t off)
-{
-    struct nffs_disk_object ndo;
-
-    hal_flash_read(area->na_flash_id, area->na_offset + off,
-                        &ndo.ndo_un_obj, sizeof(ndo.ndo_un_obj));
-
-    if (nffs_hash_id_is_inode(ndo.ndo_disk_inode.ndi_id)) {
-        return print_nffs_flash_inode(area, off);
-
-    } else if (nffs_hash_id_is_block(ndo.ndo_disk_block.ndb_id)) {
-        return print_nffs_flash_block(area, off);
-
-    } else if (ndo.ndo_disk_block.ndb_id == 0xffffffff) {
-        return area->na_length;
-
-    } else {
-        return 1;
-    }
-}
-
-void
-print_nffs_flash_areas(int verbose)
-{
-    struct nffs_area area;
-    struct nffs_disk_area darea;
-    int off;
-    int i;
-
-    for (i = 0; nffs_current_area_descs[i].nad_length != 0; i++) {
-        if (i > NFFS_MAX_AREAS) {
-            return;
-        }
-        area.na_offset = nffs_current_area_descs[i].nad_offset;
-        area.na_length = nffs_current_area_descs[i].nad_length;
-        area.na_flash_id = nffs_current_area_descs[i].nad_flash_id;
-        hal_flash_read(area.na_flash_id, area.na_offset, &darea, sizeof(darea));
-        area.na_id = darea.nda_id;
-        area.na_cur = nffs_areas[i].na_cur;
-        if (!nffs_area_magic_is_set(&darea)) {
-            printf("Area header corrupt!\n");
-        }
-        printf("area %d: id %d %x-%x cur %x len %d flashid %x gc-seq %d %s%s\n",
-               i, area.na_id, area.na_offset, area.na_offset + area.na_length,
-               area.na_cur, area.na_length, area.na_flash_id, darea.nda_gc_seq,
-               nffs_scratch_area_idx == i ? "(scratch)" : "",
-               !nffs_area_magic_is_set(&darea) ? "corrupt" : "");
-        if (verbose < 2) {
-            off = sizeof (struct nffs_disk_area);
-            while (off < area.na_length) {
-                off += print_nffs_flash_object(&area, off);
-            }
-        }
-    }
-}
-
-static int
-nffs_hash_fn(uint32_t id)
-{
-    return id % NFFS_HASH_SIZE;
-}
-
-void
-print_hashlist(struct nffs_hash_entry *he)
-{
-    struct nffs_hash_list *list;
-    int idx = nffs_hash_fn(he->nhe_id);
-    list = nffs_hash + idx;
-
-    SLIST_FOREACH(he, list, nhe_next) {
-        printf("hash_entry %s 0x%x: id 0x%x flash_loc 0x%x next 0x%x\n",
-                   nffs_hash_id_is_inode(he->nhe_id) ? "inode" : "block",
-                   (unsigned int)he,
-                   he->nhe_id, he->nhe_flash_loc,
-                   (unsigned int)he->nhe_next.sle_next);
-   }
-}
-
-void
-print_hash(void)
-{
-    int i;
-    struct nffs_hash_entry *he;
-    struct nffs_hash_entry *next;
-    struct nffs_inode ni;
-    struct nffs_disk_inode di;
-    struct nffs_block nb;
-    struct nffs_disk_block db;
-    uint32_t area_offset;
-    uint8_t area_idx;
-    int rc;
-
-    NFFS_HASH_FOREACH(he, i, next) {
-        if (nffs_hash_id_is_inode(he->nhe_id)) {
-            printf("hash_entry inode %d 0x%x: id 0x%x flash_loc 0x%x next 0x%x\n",
-                   i, (unsigned int)he,
-                   he->nhe_id, he->nhe_flash_loc,
-                   (unsigned int)he->nhe_next.sle_next);
-            if (he->nhe_id == NFFS_ID_ROOT_DIR) {
-                continue;
-            }
-            nffs_flash_loc_expand(he->nhe_flash_loc,
-                                  &area_idx, &area_offset);
-            rc = nffs_inode_read_disk(area_idx, area_offset, &di);
-            if (rc) {
-                printf("%d: fail inode read id 0x%x rc %d\n",
-                       i, he->nhe_id, rc);


<TRUNCATED>


[03/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_sm_test_util.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_sm_test_util.h b/net/nimble/host/test/src/ble_sm_test_util.h
new file mode 100644
index 0000000..3323be6
--- /dev/null
+++ b/net/nimble/host/test/src/ble_sm_test_util.h
@@ -0,0 +1,119 @@
+/**
+ * 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_BLE_SM_TEST_UTIL_
+#define H_BLE_SM_TEST_UTIL_
+
+struct ble_sm_test_passkey_info {
+    struct ble_sm_io passkey;
+    uint32_t exp_numcmp;
+    unsigned io_before_rx:1;
+};
+
+struct ble_sm_test_params {
+    uint8_t init_addr_type;
+    uint8_t init_id_addr[6];
+    uint8_t init_rpa[6];
+    uint8_t resp_addr_type;
+    uint8_t resp_id_addr[6];
+    uint8_t resp_rpa[6];
+    struct ble_sm_test_passkey_info passkey_info;
+
+    struct ble_sm_sec_req sec_req;
+    struct ble_sm_pair_cmd pair_req;
+    struct ble_sm_pair_cmd pair_rsp;
+    struct ble_sm_pair_confirm confirm_req[20];
+    struct ble_sm_pair_confirm confirm_rsp[20];
+    struct ble_sm_pair_random random_req[20];
+    struct ble_sm_pair_random random_rsp[20];
+    struct ble_sm_id_info id_info_req;
+    struct ble_sm_id_info id_info_rsp;
+    struct ble_sm_id_addr_info id_addr_info_req;
+    struct ble_sm_id_addr_info id_addr_info_rsp;
+    struct ble_sm_sign_info sign_info_req;
+    struct ble_sm_sign_info sign_info_rsp;
+    struct ble_sm_pair_fail pair_fail;
+
+    int pair_alg;
+    unsigned authenticated:1;
+
+    /*** Secure connections fields. */
+    uint8_t ltk[16];
+    uint8_t our_priv_key[32];
+    struct ble_sm_public_key public_key_req;
+    struct ble_sm_public_key public_key_rsp;
+    struct ble_sm_dhkey_check dhkey_check_req;
+    struct ble_sm_dhkey_check dhkey_check_rsp;
+
+    /*** Legacy fields. */
+    uint8_t stk[16];
+    struct ble_sm_enc_info enc_info_req;
+    struct ble_sm_enc_info enc_info_rsp;
+    struct ble_sm_master_id master_id_req;
+    struct ble_sm_master_id master_id_rsp;
+};
+
+extern int ble_sm_test_gap_event;
+extern int ble_sm_test_gap_status;
+extern struct ble_gap_sec_state ble_sm_test_sec_state;
+
+extern int ble_sm_test_store_obj_type;
+extern union ble_store_key ble_sm_test_store_key;
+extern union ble_store_value ble_sm_test_store_value;
+
+void ble_sm_test_util_init(void);
+int ble_sm_test_util_conn_cb(struct ble_gap_event *ctxt, void *arg);
+void ble_sm_test_util_io_inject(struct ble_sm_test_passkey_info *passkey_info,
+                                uint8_t cur_sm_state);
+void ble_sm_test_util_io_inject_bad(uint16_t conn_handle,
+                                    uint8_t correct_io_act);
+void ble_sm_test_util_io_check_pre(
+    struct ble_sm_test_passkey_info *passkey_info,
+    uint8_t cur_sm_state);
+void ble_sm_test_util_io_check_post(
+    struct ble_sm_test_passkey_info *passkey_info,
+    uint8_t cur_sm_state);
+void ble_sm_test_util_rx_sec_req(uint16_t conn_handle,
+                                 struct ble_sm_sec_req *cmd,
+                                 int exp_status);
+void ble_sm_test_util_verify_tx_pair_fail(struct ble_sm_pair_fail *exp_cmd);
+void ble_sm_test_util_us_lgcy_good(struct ble_sm_test_params *params);
+void ble_sm_test_util_peer_fail_inval(int we_are_master,
+                                      uint8_t *init_addr,
+                                      uint8_t *resp_addr,
+                                      struct ble_sm_pair_cmd *pair_req,
+                                      struct ble_sm_pair_fail *pair_fail);
+void ble_sm_test_util_peer_lgcy_fail_confirm(
+    uint8_t *init_addr,
+    uint8_t *resp_addr,
+    struct ble_sm_pair_cmd *pair_req,
+    struct ble_sm_pair_cmd *pair_rsp,
+    struct ble_sm_pair_confirm *confirm_req,
+    struct ble_sm_pair_confirm *confirm_rsp,
+    struct ble_sm_pair_random *random_req,
+    struct ble_sm_pair_random *random_rsp,
+    struct ble_sm_pair_fail *fail_rsp);
+
+void ble_sm_test_util_peer_lgcy_good(struct ble_sm_test_params *params);
+void ble_sm_test_util_peer_bonding_bad(uint16_t ediv, uint64_t rand_num);
+void ble_sm_test_util_peer_sc_good(struct ble_sm_test_params *params);
+void ble_sm_test_util_us_sc_good(struct ble_sm_test_params *params);
+void ble_sm_test_util_us_fail_inval(struct ble_sm_test_params *params);
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_uuid_test.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_uuid_test.c b/net/nimble/host/test/src/ble_uuid_test.c
new file mode 100644
index 0000000..1011303
--- /dev/null
+++ b/net/nimble/host/test/src/ble_uuid_test.c
@@ -0,0 +1,93 @@
+/**
+ * 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 <stddef.h>
+#include <string.h>
+#include "testutil/testutil.h"
+#include "host/ble_hs_test.h"
+#include "host/ble_uuid.h"
+#include "ble_hs_test_util.h"
+
+TEST_CASE(ble_uuid_test_128_to_16)
+{
+    uint16_t uuid16;
+
+    /*** RFCOMM */
+    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
+        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
+        0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00
+    }));
+    TEST_ASSERT(uuid16 == 0x0003);
+
+    /*** BNEP */
+    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
+        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
+        0x00, 0x10, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x00
+    }));
+    TEST_ASSERT(uuid16 == 0x000f);
+
+    /*** L2CAP */
+    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
+        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
+        0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00
+    }));
+    TEST_ASSERT(uuid16 == 0x0100);
+
+    /*** ObEXObjectPush */
+    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
+        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
+        0x00, 0x10, 0x00, 0x00, 0x05, 0x11, 0x00, 0x00
+    }));
+    TEST_ASSERT(uuid16 == 0x1105);
+
+    /*** Invalid base. */
+    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
+        0xfb, 0x34, 0x9c, 0x5f, 0x80, 0x00, 0x00, 0x80,
+        0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00
+    }));
+    TEST_ASSERT(uuid16 == 0);
+
+    /*** Invalid prefix. */
+    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
+        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
+        0x00, 0x10, 0x00, 0x00, 0x03, 0x00, 0x00, 0x01
+    }));
+    TEST_ASSERT(uuid16 == 0);
+
+    /*** 16-bit UUID of 0. */
+    uuid16 = ble_uuid_128_to_16(((uint8_t[]) {
+        0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00, 0x00, 0x80,
+        0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
+    }));
+}
+
+TEST_SUITE(ble_uuid_test_suite)
+{
+    tu_suite_set_post_test_cb(ble_hs_test_util_post_test, NULL);
+
+    ble_uuid_test_128_to_16();
+}
+
+int
+ble_uuid_test_all(void)
+{
+    ble_uuid_test_suite();
+
+    return tu_any_failed;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/include/nimble/nimble_opt.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/nimble_opt.h b/net/nimble/include/nimble/nimble_opt.h
index 319c08d..82d2d55 100644
--- a/net/nimble/include/nimble/nimble_opt.h
+++ b/net/nimble/include/nimble/nimble_opt.h
@@ -20,383 +20,6 @@
 #ifndef H_NIMBLE_OPT_
 #define H_NIMBLE_OPT_
 
-/** HOST / CONTROLLER: Maximum number of concurrent connections. */
-
-#ifndef NIMBLE_OPT_MAX_CONNECTIONS
-#define NIMBLE_OPT_MAX_CONNECTIONS              1
-#endif
-
-
-/**
- * HOST / CONTROLLER: Supported GAP roles.  By default, all four roles are
- * enabled.
- */
-
-#ifndef NIMBLE_OPT_ROLE_CENTRAL
-#define NIMBLE_OPT_ROLE_CENTRAL                 1
-#endif
-
-#ifndef NIMBLE_OPT_ROLE_PERIPHERAL
-#define NIMBLE_OPT_ROLE_PERIPHERAL              1
-#endif
-
-#ifndef NIMBLE_OPT_ROLE_BROADCASTER
-#define NIMBLE_OPT_ROLE_BROADCASTER             1
-#endif
-
-#ifndef NIMBLE_OPT_ROLE_OBSERVER
-#define NIMBLE_OPT_ROLE_OBSERVER                1
-#endif
-
-#ifndef NIMBLE_OPT_WHITELIST
-#define NIMBLE_OPT_WHITELIST                    1
-#endif
-
-/** HOST: Security manager legacy pairing.  Enabled by default. */
-
-#ifndef NIMBLE_OPT_SM
-#define NIMBLE_OPT_SM                           1
-#endif
-
-/** HOST: Security manage secure connections (4.2).  Disabled by default. */
-
-#ifndef NIMBLE_OPT_SM_SC
-#define NIMBLE_OPT_SM_SC                        0
-#endif
-
-/**
- * HOST: Supported GATT procedures.  By default:
- *     o Notify and indicate are enabled;
- *     o All other procedures are enabled for centrals.
- */
-
-#ifndef NIMBLE_OPT_GATT_DISC_ALL_SVCS
-#define NIMBLE_OPT_GATT_DISC_ALL_SVCS           NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_DISC_SVC_UUID
-#define NIMBLE_OPT_GATT_DISC_SVC_UUID           NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_FIND_INC_SVCS
-#define NIMBLE_OPT_GATT_FIND_INC_SVCS           NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_DISC_ALL_CHRS
-#define NIMBLE_OPT_GATT_DISC_ALL_CHRS           NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_DISC_CHR_UUID
-#define NIMBLE_OPT_GATT_DISC_CHR_UUID           NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_DISC_ALL_DSCS
-#define NIMBLE_OPT_GATT_DISC_ALL_DSCS           NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_READ
-#define NIMBLE_OPT_GATT_READ                    NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_READ_UUID
-#define NIMBLE_OPT_GATT_READ_UUID               NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_READ_LONG
-#define NIMBLE_OPT_GATT_READ_LONG               NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_READ_MULT
-#define NIMBLE_OPT_GATT_READ_MULT               NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_WRITE_NO_RSP
-#define NIMBLE_OPT_GATT_WRITE_NO_RSP            NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_SIGNED_WRITE
-#define NIMBLE_OPT_GATT_SIGNED_WRITE            NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_WRITE
-#define NIMBLE_OPT_GATT_WRITE                   NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_WRITE_LONG
-#define NIMBLE_OPT_GATT_WRITE_LONG              NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_WRITE_RELIABLE
-#define NIMBLE_OPT_GATT_WRITE_RELIABLE          NIMBLE_OPT_ROLE_CENTRAL
-#endif
-
-#ifndef NIMBLE_OPT_GATT_NOTIFY
-#define NIMBLE_OPT_GATT_NOTIFY                  1
-#endif
-
-#ifndef NIMBLE_OPT_GATT_INDICATE
-#define NIMBLE_OPT_GATT_INDICATE                1
-#endif
-
-/** HOST: GATT options. */
-
-/* The maximum number of attributes that can be written with a single GATT
- * Reliable Write procedure.
- */
-#ifndef NIMBLE_OPT_GATT_WRITE_MAX_ATTRS
-#define NIMBLE_OPT_GATT_WRITE_MAX_ATTRS         4
-#endif
-
-/** HOST: Supported server ATT commands. */
-
-#ifndef NIMBLE_OPT_ATT_SVR_FIND_INFO
-#define NIMBLE_OPT_ATT_SVR_FIND_INFO            1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_FIND_TYPE
-#define NIMBLE_OPT_ATT_SVR_FIND_TYPE            1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_READ_TYPE
-#define NIMBLE_OPT_ATT_SVR_READ_TYPE            1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_READ
-#define NIMBLE_OPT_ATT_SVR_READ                 1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_READ_BLOB
-#define NIMBLE_OPT_ATT_SVR_READ_BLOB            1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_READ_MULT
-#define NIMBLE_OPT_ATT_SVR_READ_MULT            1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_READ_GROUP_TYPE
-#define NIMBLE_OPT_ATT_SVR_READ_GROUP_TYPE      1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_WRITE
-#define NIMBLE_OPT_ATT_SVR_WRITE                1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_WRITE_NO_RSP
-#define NIMBLE_OPT_ATT_SVR_WRITE_NO_RSP         1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_SIGNED_WRITE
-#define NIMBLE_OPT_ATT_SVR_SIGNED_WRITE         1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_PREP_WRITE
-#define NIMBLE_OPT_ATT_SVR_PREP_WRITE           1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_EXEC_WRITE
-#define NIMBLE_OPT_ATT_SVR_EXEC_WRITE           1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_NOTIFY
-#define NIMBLE_OPT_ATT_SVR_NOTIFY               1
-#endif
-
-#ifndef NIMBLE_OPT_ATT_SVR_INDICATE
-#define NIMBLE_OPT_ATT_SVR_INDICATE             1
-#endif
-
-
-/** HOST: Miscellaneous features. */
-
-#ifndef NIMBLE_OPT_EDDYSTONE
-#define NIMBLE_OPT_EDDYSTONE                    1
-#endif
-
-
-/*** CONTROLLER ***/
-
-/*
- * Sleep clock accuracy (sca). This is the amount of drift in the system during
- * when the device is sleeping (in parts per million).
- *
- * NOTE: the master sca is an enumerated value based on the sca. Rather than
- * have a piece of code calculate this value, the developer must set this
- * value based on the value of the SCA using the following table:
- *
- *  SCA between 251 and 500 ppm (inclusive); master sca = 0
- *  SCA between 151 and 250 ppm (inclusive); master sca = 1
- *  SCA between 101 and 150 ppm (inclusive); master sca = 2
- *  SCA between 76 and 100 ppm (inclusive); master sca = 3
- *  SCA between 51 and 75 ppm (inclusive); master sca = 4
- *  SCA between 31 and 50 ppm (inclusive); master sca = 5
- *  SCA between 21 and 30 ppm (inclusive); master sca = 6
- *  SCA between 0 and 20 ppm (inclusive); master sca = 7
- *
- *  For example:
- *      if your clock drift is 101 ppm, your master should be set to 2.
- *      if your clock drift is 20, your master sca should be set to 7.
- *
- *  The values provided below are merely meant to be an example and should
- *  be replaced by values appropriate for your platform.
- */
-#ifndef NIMBLE_OPT_LL_OUR_SCA
-#define NIMBLE_OPT_LL_OUR_SCA                   (60)    /* in ppm */
-#endif
-
-#ifndef NIMBLE_OPT_LL_MASTER_SCA
-#define NIMBLE_OPT_LL_MASTER_SCA                (4)
-#endif
-
-/* transmit power level */
-#ifndef NIMBLE_OPT_LL_TX_PWR_DBM
-#define NIMBLE_OPT_LL_TX_PWR_DBM                (0)
-#endif
-
-/*
- * Determines the maximum rate at which the controller will send the
- * number of completed packets event to the host. Rate is in os time ticks
- */
-#ifndef NIMBLE_OPT_NUM_COMP_PKT_RATE
-#define NIMBLE_OPT_NUM_COMP_PKT_RATE    ((2000 * OS_TICKS_PER_SEC) / 1000)
-#endif
-
-/* Manufacturer ID. Should be set to unique ID per manufacturer */
-#ifndef NIMBLE_OPT_LL_MFRG_ID
-#define NIMBLE_OPT_LL_MFRG_ID                   (0xFFFF)
-#endif
-
-/*
- * Configuration items for the number of duplicate advertisers and the
- * number of advertisers from which we have heard a scan response.
- */
-#ifndef NIMBLE_OPT_LL_NUM_SCAN_DUP_ADVS
-#define NIMBLE_OPT_LL_NUM_SCAN_DUP_ADVS         (8)
-#endif
-
-#ifndef NIMBLE_OPT_LL_NUM_SCAN_RSP_ADVS
-#define NIMBLE_OPT_LL_NUM_SCAN_RSP_ADVS         (8)
-#endif
-
-/* Size of the LL whitelist */
-#ifndef NIMBLE_OPT_LL_WHITELIST_SIZE
-#define NIMBLE_OPT_LL_WHITELIST_SIZE            (8)
-#endif
-
-/* Size of the resolving ist */
-#ifndef NIMBLE_OPT_LL_RESOLV_LIST_SIZE
-#define NIMBLE_OPT_LL_RESOLV_LIST_SIZE          (4)
-#endif
-
-/*
- * Data length management definitions for connections. These define the maximum
- * size of the PDU's that will be sent and/or received in a connection.
- */
-#ifndef NIMBLE_OPT_LL_MAX_PKT_SIZE
-#define NIMBLE_OPT_LL_MAX_PKT_SIZE              (251)
-#endif
-
-#ifndef NIMBLE_OPT_LL_SUPP_MAX_RX_BYTES
-#define NIMBLE_OPT_LL_SUPP_MAX_RX_BYTES         (NIMBLE_OPT_LL_MAX_PKT_SIZE)
-#endif
-
-#ifndef NIMBLE_OPT_LL_SUPP_MAX_TX_BYTES
-#define NIMBLE_OPT_LL_SUPP_MAX_TX_BYTES         (NIMBLE_OPT_LL_MAX_PKT_SIZE)
-#endif
-
-#ifndef NIMBLE_OPT_LL_CONN_INIT_MAX_TX_BYTES
-#define NIMBLE_OPT_LL_CONN_INIT_MAX_TX_BYTES    (27)
-#endif
-
-/* The number of slots that will be allocated to each connection */
-#ifndef NIMBLE_OPT_LL_CONN_INIT_SLOTS
-#define NIMBLE_OPT_LL_CONN_INIT_SLOTS           (2)
-#endif
-
-/* The number of random bytes to store */
-#ifndef NIMBLE_OPT_LL_RNG_BUFSIZE
-#define NIMBLE_OPT_LL_RNG_BUFSIZE               (32)
-#endif
-
-/*
- * Configuration for LL supported features.
- *
- * There are a total 8 features that the LL can support. These can be found in
- * v4.2, Vol 6 Part B Section 4.6.
- *
- * These feature definitions are used to inform a host or other controller
- * about the LL features supported by the controller.
- *
- * NOTE: the controller always supports extended reject indicate and thus is
- * not listed here.
- */
-
- /*
-  * This option enables/disables encryption support in the controller. This
-  * option saves both both code and RAM.
-  */
-#ifndef BLE_LL_CFG_FEAT_LE_ENCRYPTION
-#define BLE_LL_CFG_FEAT_LE_ENCRYPTION           (1)
-#endif
-
-/*
- * This option enables/disables the connection parameter request procedure.
- * This is implemented in the controller but is disabled by default.
- */
-#ifndef BLE_LL_CFG_FEAT_CONN_PARAM_REQ
-#define BLE_LL_CFG_FEAT_CONN_PARAM_REQ          (0)
-#endif
-
-/*
- * This option allows a slave to initiate the feature exchange procedure.
- * This feature is implemented but currently has no impact on code or ram size
- */
-#ifndef BLE_LL_CFG_FEAT_SLAVE_INIT_FEAT_XCHG
-#define BLE_LL_CFG_FEAT_SLAVE_INIT_FEAT_XCHG    (1)
-#endif
-
-/*
- * This option allows a controller to send/receive LE pings. Currently,
- * this feature is not implemented by the controller so turning it on or off
- * has no effect.
- */
-#ifndef BLE_LL_CFG_FEAT_LE_PING
-#define  BLE_LL_CFG_FEAT_LE_PING                (1)
-#endif
-
-/*
- * This option enables/disables the data length update procedure in the
- * controller. If enabled, the controller is allowed to change the size of
- * tx/rx pdu's used in a connection. This option has only minor impact on
- * code size and non on RAM.
- */
-#ifndef BLE_LL_CFG_FEAT_DATA_LEN_EXT
-#define  BLE_LL_CFG_FEAT_DATA_LEN_EXT           (1)
-#endif
-
-/*
- * This option is used to enable/disable LL privacy. Currently, this feature
- * is not supported by the nimble controller.
- */
-#ifndef BLE_LL_CFG_FEAT_LL_PRIVACY
-#define BLE_LL_CFG_FEAT_LL_PRIVACY              (1)
-#endif
-
-/*
- * This option is used to enable/disable the extended scanner filter policy
- * feature. Currently, this feature is not supported by the nimble controller.
- */
-#ifndef BLE_LL_CFG_FEAT_EXT_SCAN_FILT
-#define  BLE_LL_CFG_FEAT_EXT_SCAN_FILT          (0)
-#endif
-
-/**
- * This macro exists to help catch bugs at compile time.  If code uses this
- * macro to check an option value, the compiler will complain when this header
- * is not included.  If the code checks the option symbol directly without
- * including this header, it will appear as though the option is set to 0.
- */
-#define NIMBLE_OPT(x)                           NIMBLE_OPT_ ## x
-
 /* Include automatically-generated settings. */
 #include "nimble/nimble_opt_auto.h"
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/include/nimble/nimble_opt_auto.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/nimble_opt_auto.h b/net/nimble/include/nimble/nimble_opt_auto.h
index 2547c6c..536416f 100644
--- a/net/nimble/include/nimble/nimble_opt_auto.h
+++ b/net/nimble/include/nimble/nimble_opt_auto.h
@@ -20,6 +20,7 @@
 #ifndef H_NIMBLE_OPT_AUTO_
 #define H_NIMBLE_OPT_AUTO_
 
+#include "syscfg/syscfg.h"
 #include "nimble/nimble_opt.h"
 
 /***
@@ -29,78 +30,87 @@
  * in nimble_opt.h.
  */
 
-#undef NIMBLE_OPT_ADVERTISE
-#define NIMBLE_OPT_ADVERTISE                    \
-    (NIMBLE_OPT_ROLE_BROADCASTER || NIMBLE_OPT_ROLE_PERIPHERAL)
+#undef NIMBLE_BLE_ADVERTISE
+#define NIMBLE_BLE_ADVERTISE                    \
+    (MYNEWT_VAL(BLE_ROLE_BROADCASTER) || MYNEWT_VAL(BLE_ROLE_PERIPHERAL))
 
-#undef NIMBLE_OPT_SCAN
-#define NIMBLE_OPT_SCAN                         \
-    (NIMBLE_OPT_ROLE_CENTRAL || NIMBLE_OPT_ROLE_OBSERVER)
+#undef NIMBLE_BLE_SCAN
+#define NIMBLE_BLE_SCAN                         \
+    (MYNEWT_VAL(BLE_ROLE_CENTRAL) || MYNEWT_VAL(BLE_ROLE_OBSERVER))
 
-#undef NIMBLE_OPT_CONNECT
-#define NIMBLE_OPT_CONNECT                      \
-    (NIMBLE_OPT_ROLE_CENTRAL || NIMBLE_OPT_ROLE_PERIPHERAL)
+#undef NIMBLE_BLE_CONNECT
+#define NIMBLE_BLE_CONNECT                      \
+    (MYNEWT_VAL(BLE_ROLE_CENTRAL) || MYNEWT_VAL(BLE_ROLE_PERIPHERAL))
 
 
 /** Supported client ATT commands. */
 
-#undef NIMBLE_OPT_ATT_CLT_FIND_INFO
-#define NIMBLE_OPT_ATT_CLT_FIND_INFO            (NIMBLE_OPT_GATT_DISC_ALL_DSCS)
+#undef NIMBLE_BLE_ATT_CLT_FIND_INFO
+#define NIMBLE_BLE_ATT_CLT_FIND_INFO            \
+    (MYNEWT_VAL(BLE_GATT_DISC_ALL_DSCS))
 
-#undef NIMBLE_OPT_ATT_CLT_FIND_TYPE
-#define NIMBLE_OPT_ATT_CLT_FIND_TYPE            (NIMBLE_OPT_GATT_DISC_SVC_UUID)
+#undef NIMBLE_BLE_ATT_CLT_FIND_TYPE
+#define NIMBLE_BLE_ATT_CLT_FIND_TYPE            \
+    (MYNEWT_VAL(BLE_GATT_DISC_SVC_UUID))
 
-#undef NIMBLE_OPT_ATT_CLT_READ_TYPE
-#define NIMBLE_OPT_ATT_CLT_READ_TYPE            \
-    (NIMBLE_OPT_GATT_FIND_INC_SVCS ||           \
-     NIMBLE_OPT_GATT_DISC_ALL_CHRS ||           \
-     NIMBLE_OPT_GATT_DISC_CHRS_UUID ||          \
-     NIMBLE_OPT_GATT_READ_UUID)
+#undef NIMBLE_BLE_ATT_CLT_READ_TYPE
+#define NIMBLE_BLE_ATT_CLT_READ_TYPE            \
+    (MYNEWT_VAL(BLE_GATT_FIND_INC_SVCS) ||      \
+     MYNEWT_VAL(BLE_GATT_DISC_ALL_CHRS) ||      \
+     MYNEWT_VAL(BLE_GATT_DISC_CHRS_UUID) ||     \
+     MYNEWT_VAL(BLE_GATT_READ_UUID))
     
-#undef NIMBLE_OPT_ATT_CLT_READ
-#define NIMBLE_OPT_ATT_CLT_READ                 \
-    (NIMBLE_OPT_GATT_READ ||                    \
-     NIMBLE_OPT_GATT_READ_LONG ||               \
-     NIMBLE_OPT_GATT_FIND_INC_SVCS)
+#undef NIMBLE_BLE_ATT_CLT_READ
+#define NIMBLE_BLE_ATT_CLT_READ                 \
+    (MYNEWT_VAL(BLE_GATT_READ) ||               \
+     MYNEWT_VAL(BLE_GATT_READ_LONG) ||          \
+     MYNEWT_VAL(BLE_GATT_FIND_INC_SVCS))
 
-#undef NIMBLE_OPT_ATT_CLT_READ_BLOB
-#define NIMBLE_OPT_ATT_CLT_READ_BLOB            (NIMBLE_OPT_GATT_READ_LONG)
+#undef NIMBLE_BLE_ATT_CLT_READ_BLOB
+#define NIMBLE_BLE_ATT_CLT_READ_BLOB            \
+    (MYNEWT_VAL(BLE_GATT_READ_LONG))
 
-#undef NIMBLE_OPT_ATT_CLT_READ_MULT
-#define NIMBLE_OPT_ATT_CLT_READ_MULT            (NIMBLE_OPT_GATT_READ_MULT)
+#undef NIMBLE_BLE_ATT_CLT_READ_MULT
+#define NIMBLE_BLE_ATT_CLT_READ_MULT            \
+    (MYNEWT_VAL(BLE_GATT_READ_MULT))
 
-#undef NIMBLE_OPT_ATT_CLT_READ_GROUP_TYPE
-#define NIMBLE_OPT_ATT_CLT_READ_GROUP_TYPE      \
-    (NIMBLE_OPT_GATT_DISC_ALL_SVCS)
+#undef NIMBLE_BLE_ATT_CLT_READ_GROUP_TYPE
+#define NIMBLE_BLE_ATT_CLT_READ_GROUP_TYPE      \
+    (MYNEWT_VAL(BLE_GATT_DISC_ALL_SVCS))
 
-#undef NIMBLE_OPT_ATT_CLT_WRITE
-#define NIMBLE_OPT_ATT_CLT_WRITE                (NIMBLE_OPT_GATT_WRITE)
+#undef NIMBLE_BLE_ATT_CLT_WRITE
+#define NIMBLE_BLE_ATT_CLT_WRITE                \
+    (MYNEWT_VAL(BLE_GATT_WRITE))
 
-#undef NIMBLE_OPT_ATT_CLT_WRITE_NO_RSP
-#define NIMBLE_OPT_ATT_CLT_WRITE_NO_RSP         (NIMBLE_OPT_GATT_WRITE_NO_RSP)
+#undef NIMBLE_BLE_ATT_CLT_WRITE_NO_RSP
+#define NIMBLE_BLE_ATT_CLT_WRITE_NO_RSP         \
+    (MYNEWT_VAL(BLE_GATT_WRITE_NO_RSP))
 
-#undef NIMBLE_OPT_ATT_CLT_PREP_WRITE
-#define NIMBLE_OPT_ATT_CLT_PREP_WRITE           (NIMBLE_OPT_GATT_WRITE_LONG)
+#undef NIMBLE_BLE_ATT_CLT_PREP_WRITE
+#define NIMBLE_BLE_ATT_CLT_PREP_WRITE           \
+    (MYNEWT_VAL(BLE_GATT_WRITE_LONG))
 
-#undef NIMBLE_OPT_ATT_CLT_EXEC_WRITE
-#define NIMBLE_OPT_ATT_CLT_EXEC_WRITE           (NIMBLE_OPT_GATT_WRITE_LONG)
+#undef NIMBLE_BLE_ATT_CLT_EXEC_WRITE
+#define NIMBLE_BLE_ATT_CLT_EXEC_WRITE           \
+    (MYNEWT_VAL(BLE_GATT_WRITE_LONG))
 
-#undef NIMBLE_OPT_ATT_CLT_NOTIFY  
-#define NIMBLE_OPT_ATT_CLT_NOTIFY               (NIMBLE_OPT_GATT_NOTIFY)
+#undef NIMBLE_BLE_ATT_CLT_NOTIFY  
+#define NIMBLE_BLE_ATT_CLT_NOTIFY               \
+    (MYNEWT_VAL(BLE_GATT_NOTIFY))
 
-#undef NIMBLE_OPT_ATT_CLT_INDICATE
-#define NIMBLE_OPT_ATT_CLT_INDICATE             (NIMBLE_OPT_GATT_INDICATE)
+#undef NIMBLE_BLE_ATT_CLT_INDICATE
+#define NIMBLE_BLE_ATT_CLT_INDICATE             \
+    (MYNEWT_VAL(BLE_GATT_INDICATE))
 
 /** Security manager settings. */
 
+
 /* Secure connections implies security manager support
  * Note: For now, security manager is synonymous with legacy pairing.  In the
  * future, a new setting for legacy pairing may be introduced as a sibling of
  * the SC setting.
  */
-#if NIMBLE_OPT_SM_SC
-#undef NIMBLE_OPT_SM
-#define NIMBLE_OPT_SM                           1
-#endif
+#undef NIMBLE_BLE_SM
+#define NIMBLE_BLE_SM   (MYNEWT_VAL(BLE_SM) || MYNEWT_VAL(BLE_SM_SC))
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/pkg.yml b/net/nimble/pkg.yml
index ec9a473..10d2004 100644
--- a/net/nimble/pkg.yml
+++ b/net/nimble/pkg.yml
@@ -27,3 +27,25 @@ pkg.keywords:
 
 pkg.deps:
     - libs/os
+
+pkg.syscfg_defs:
+    # Supported GAP roles.  By default, all four roles are enabled.
+    BLE_ROLE_CENTRAL:
+        description: 'TBD'
+        value: 1
+    BLE_ROLE_PERIPHERAL:
+        description: 'TBD'
+        value: 1
+    BLE_ROLE_BROADCASTER:
+        description: 'TBD'
+        value: 1
+    BLE_ROLE_OBSERVER:
+        description: 'TBD'
+        value: 1
+
+    BLE_MAX_CONNECTIONS:
+        description: 'The maximum number of concurrent connections.'
+        value: 1
+    BLE_WHITELIST:
+        description: 'TBD'
+        value: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
----------------------------------------------------------------------
diff --git a/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h b/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
index 9d2d672..4b0aed1 100644
--- a/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
+++ b/net/nimble/transport/ram/include/transport/ram/ble_hci_ram.h
@@ -3,28 +3,6 @@
 
 #include "nimble/ble_hci_trans.h"
 
-struct ble_hci_ram_cfg {
-    /** Number of high-priority event buffers. */
-    uint16_t num_evt_hi_bufs;
-
-    /** Number of low-priority event buffers. */
-    uint16_t num_evt_lo_bufs;
-
-    /** Size of each event buffer, in bytes. */
-    uint16_t evt_buf_sz;
-
-    /* Note: For information about high-priority vs. low-priority event
-     * buffers, see net/nimble/include/nimble/ble_hci_trans.h.
-     */
-
-    /* Note: host-to-controller command buffers are not configurable.  The RAM
-     * transport only allows one outstanding command, so it uses a single
-     * statically-allocated buffer.
-     */
-};
-
-extern const struct ble_hci_ram_cfg ble_hci_ram_cfg_dflt;
-
-int ble_hci_ram_init(const struct ble_hci_ram_cfg *cfg);
+int ble_hci_ram_init(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/transport/ram/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/transport/ram/pkg.yml b/net/nimble/transport/ram/pkg.yml
index a3524a1..3d551fe 100644
--- a/net/nimble/transport/ram/pkg.yml
+++ b/net/nimble/transport/ram/pkg.yml
@@ -31,3 +31,19 @@ pkg.deps:
 
 pkg.apis:
     - ble_transport
+
+pkg.init_function: ble_hci_ram_pkg_init
+pkg.init_stage: 1
+
+pkg.syscfg_defs:
+    BLE_HCI_EVT_HI_BUF_COUNT:
+        description: 'Number of high-priority event buffers.'
+        value:  1
+
+    BLE_HCI_EVT_LO_BUF_COUNT:
+        description: 'Number of low-priority event buffers.'
+        value:  2
+
+    BLE_HCI_EVT_BUF_SIZE:
+        description: 'Size of each event buffer, in bytes.'
+        value:  260

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/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 2ce5fd6..cce6672 100644
--- a/net/nimble/transport/ram/src/ble_hci_ram.c
+++ b/net/nimble/transport/ram/src/ble_hci_ram.c
@@ -1,19 +1,14 @@
 #include <assert.h>
 #include <errno.h>
 #include <stddef.h>
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.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"
 
-/** Default configuration. */
-const struct ble_hci_ram_cfg ble_hci_ram_cfg_dflt = {
-    .num_evt_hi_bufs = 1,
-    .num_evt_lo_bufs = 2,
-    .evt_buf_sz = BLE_HCI_TRANS_CMD_SZ,
-};
-
 static ble_hci_trans_rx_cmd_fn *ble_hci_ram_rx_cmd_hs_cb;
 static void *ble_hci_ram_rx_cmd_hs_arg;
 
@@ -187,15 +182,15 @@ ble_hci_trans_reset(void)
  *                              A BLE_ERR_[...] error code on failure.
  */
 int
-ble_hci_ram_init(const struct ble_hci_ram_cfg *cfg)
+ble_hci_ram_init(void)
 {
     int rc;
 
     ble_hci_ram_free_mem();
 
     rc = mem_malloc_mempool(&ble_hci_ram_evt_hi_pool,
-                            cfg->num_evt_hi_bufs,
-                            cfg->evt_buf_sz,
+                            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) {
@@ -204,8 +199,8 @@ ble_hci_ram_init(const struct ble_hci_ram_cfg *cfg)
     }
 
     rc = mem_malloc_mempool(&ble_hci_ram_evt_lo_pool,
-                            cfg->num_evt_lo_bufs,
-                            cfg->evt_buf_sz,
+                            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) {
@@ -225,3 +220,12 @@ err:
     ble_hci_ram_free_mem();
     return rc;
 }
+
+void
+ble_hci_ram_pkg_init(void)
+{
+    int rc;
+
+    rc = ble_hci_ram_init();
+    SYSINIT_PANIC_ASSERT(rc == 0);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
----------------------------------------------------------------------
diff --git a/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h b/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
index 1fbaa74..415b320 100644
--- a/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
+++ b/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
@@ -14,6 +14,6 @@ struct ble_hci_uart_cfg {
 
 extern const struct ble_hci_uart_cfg ble_hci_uart_cfg_dflt;
 
-int ble_hci_uart_init(const struct ble_hci_uart_cfg *cfg);
+int ble_hci_uart_init(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/transport/uart/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/transport/uart/pkg.yml b/net/nimble/transport/uart/pkg.yml
index cce429c..56f0cda 100644
--- a/net/nimble/transport/uart/pkg.yml
+++ b/net/nimble/transport/uart/pkg.yml
@@ -32,3 +32,35 @@ pkg.deps:
 
 pkg.apis:
     - ble_transport
+
+pkg.syscfg_defs:
+    BLE_HCI_EVT_HI_BUF_COUNT:
+        description: 'TBD'
+        value:     8
+    BLE_HCI_EVT_LO_BUF_COUNT:
+        description: 'TBD'
+        value:     0
+
+    BLE_HCI_UART_BUF_SIZE:
+        description: 'TBD'
+        value: 260
+
+    BLE_HCI_UART_PORT:
+        description: 'TBD'
+        value:            0
+    BLE_HCI_UART_BAUD:
+        description: 'TBD'
+        value:            1000000
+    BLE_HCI_UART_DATA_BITS:
+        description: 'TBD'
+        value:       8
+    BLE_HCI_UART_STOP_BITS:
+        description: 'TBD'
+        value:       1
+    BLE_HCI_UART_PARITY:
+        description: 'TBD'
+        value:          HAL_UART_PARITY_NONE
+    BLE_HCI_UART_FLOW_CTRL:
+        description: 'TBD'
+        value:       HAL_UART_FLOW_CTL_RTS_CTS
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/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 0fa982a..e7e07a1 100755
--- a/net/nimble/transport/uart/src/ble_hci_uart.c
+++ b/net/nimble/transport/uart/src/ble_hci_uart.c
@@ -36,6 +36,9 @@
 
 #include "transport/uart/ble_hci_uart.h"
 
+#define BLE_HCI_UART_EVT_COUNT  \
+    (MYNEWT_VAL(BLE_HCI_EVT_HI_BUF_COUNT) + MYNEWT_VAL(BLE_HCI_EVT_LO_BUF_COUNT))
+
 /***
  * NOTE:
  * The UART HCI transport doesn't use event buffer priorities.  All incoming
@@ -48,19 +51,6 @@
 #define BLE_HCI_UART_H4_SCO         0x03
 #define BLE_HCI_UART_H4_EVT         0x04
 
-/** Default configuration. */
-const struct ble_hci_uart_cfg ble_hci_uart_cfg_dflt = {
-    .uart_port = 0,
-    .baud = 1000000,
-    .flow_ctrl = HAL_UART_FLOW_CTL_RTS_CTS,
-    .data_bits = 8,
-    .stop_bits = 1,
-    .parity = HAL_UART_PARITY_NONE,
-
-    .num_evt_bufs = 8,
-    .evt_buf_sz = BLE_HCI_TRANS_CMD_SZ,
-};
-
 static ble_hci_trans_rx_cmd_fn *ble_hci_uart_rx_cmd_cb;
 static void *ble_hci_uart_rx_cmd_arg;
 
@@ -117,8 +107,6 @@ static struct {
     STAILQ_HEAD(, ble_hci_uart_pkt) tx_pkts; /* Packet queue to send to UART */
 } ble_hci_uart_state;
 
-static struct ble_hci_uart_cfg ble_hci_uart_cfg;
-
 static int
 ble_hci_uart_acl_tx(struct os_mbuf *om)
 {
@@ -138,7 +126,7 @@ ble_hci_uart_acl_tx(struct os_mbuf *om)
     STAILQ_INSERT_TAIL(&ble_hci_uart_state.tx_pkts, pkt, next);
     OS_EXIT_CRITICAL(sr);
 
-    hal_uart_start_tx(ble_hci_uart_cfg.uart_port);
+    hal_uart_start_tx(MYNEWT_VAL(BLE_HCI_UART_PORT));
 
     return 0;
 }
@@ -162,7 +150,7 @@ ble_hci_uart_cmdevt_tx(uint8_t *hci_ev, uint8_t h4_type)
     STAILQ_INSERT_TAIL(&ble_hci_uart_state.tx_pkts, pkt, next);
     OS_EXIT_CRITICAL(sr);
 
-    hal_uart_start_tx(ble_hci_uart_cfg.uart_port);
+    hal_uart_start_tx(MYNEWT_VAL(BLE_HCI_UART_PORT));
 
     return 0;
 }
@@ -463,19 +451,19 @@ ble_hci_uart_config(void)
 {
     int rc;
 
-    rc = hal_uart_init_cbs(ble_hci_uart_cfg.uart_port,
+    rc = hal_uart_init_cbs(MYNEWT_VAL(BLE_HCI_UART_PORT),
                            ble_hci_uart_tx_char, NULL,
                            ble_hci_uart_rx_char, NULL);
     if (rc != 0) {
         return BLE_ERR_UNSPECIFIED;
     }
 
-    rc = hal_uart_config(ble_hci_uart_cfg.uart_port,
-                         ble_hci_uart_cfg.baud,
-                         ble_hci_uart_cfg.data_bits,
-                         ble_hci_uart_cfg.stop_bits,
-                         ble_hci_uart_cfg.parity,
-                         ble_hci_uart_cfg.flow_ctrl);
+    rc = hal_uart_config(MYNEWT_VAL(BLE_HCI_UART_PORT),
+                         MYNEWT_VAL(BLE_HCI_UART_BAUD),
+                         MYNEWT_VAL(BLE_HCI_UART_DATA_BITS),
+                         MYNEWT_VAL(BLE_HCI_UART_STOP_BITS),
+                         MYNEWT_VAL(BLE_HCI_UART_PARITY),
+                         MYNEWT_VAL(BLE_HCI_UART_FLOW_CTRL));
     if (rc != 0) {
         return BLE_ERR_HW_FAIL;
     }
@@ -569,9 +557,9 @@ ble_hci_trans_hs_acl_tx(struct os_mbuf *om)
  */
 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)
+                     void *cmd_arg,
+                     ble_hci_trans_rx_acl_fn *acl_cb,
+                     void *acl_arg)
 {
     ble_hci_uart_set_rx_cbs(cmd_cb, cmd_arg, acl_cb, acl_arg);
 }
@@ -591,9 +579,9 @@ ble_hci_trans_cfg_hs(ble_hci_trans_rx_cmd_fn *cmd_cb,
  */
 void
 ble_hci_trans_cfg_ll(ble_hci_trans_rx_cmd_fn *cmd_cb,
-                            void *cmd_arg,
-                            ble_hci_trans_rx_acl_fn *acl_cb,
-                            void *acl_arg)
+                     void *cmd_arg,
+                     ble_hci_trans_rx_acl_fn *acl_cb,
+                     void *acl_arg)
 {
     ble_hci_uart_set_rx_cbs(cmd_cb, cmd_arg, acl_cb, acl_arg);
 }
@@ -656,7 +644,7 @@ ble_hci_trans_reset(void)
     int rc;
 
     /* Close the UART to prevent race conditions as the buffers are freed. */
-    rc = hal_uart_close(ble_hci_uart_cfg.uart_port);
+    rc = hal_uart_close(MYNEWT_VAL(BLE_HCI_UART_PORT));
     if (rc != 0) {
         return BLE_ERR_HW_FAIL;
     }
@@ -690,25 +678,20 @@ ble_hci_trans_reset(void)
 /**
  * Initializes the UART HCI transport module.
  *
- * @param cfg                   The settings to initialize the HCI UART
- *                                  transport with.
- *
  * @return                      0 on success;
  *                              A BLE_ERR_[...] error code on failure.
  */
 int
-ble_hci_uart_init(const struct ble_hci_uart_cfg *cfg)
+ble_hci_uart_init(void)
 {
     int rc;
 
     ble_hci_uart_free_mem();
 
-    ble_hci_uart_cfg = *cfg;
-
     /* Create memory pool of HCI command / event buffers */
     rc = mem_malloc_mempool(&ble_hci_uart_evt_pool,
-                            cfg->num_evt_bufs,
-                            cfg->evt_buf_sz,
+                            BLE_HCI_UART_EVT_COUNT,
+                            MYNEWT_VAL(BLE_HCI_UART_BUF_SIZE),
                             "ble_hci_uart_evt_pool",
                             &ble_hci_uart_evt_buf);
     if (rc != 0) {
@@ -718,7 +701,7 @@ ble_hci_uart_init(const struct ble_hci_uart_cfg *cfg)
 
     /* Create memory pool of packet list nodes. */
     rc = mem_malloc_mempool(&ble_hci_uart_pkt_pool,
-                            cfg->num_evt_bufs,
+                            BLE_HCI_UART_EVT_COUNT,
                             sizeof (struct ble_hci_uart_pkt),
                             "ble_hci_uart_pkt_pool",
                             &ble_hci_uart_pkt_buf);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/include/config/config.h
----------------------------------------------------------------------
diff --git a/sys/config/include/config/config.h b/sys/config/include/config/config.h
index 3402b46..bc8e7d2 100644
--- a/sys/config/include/config/config.h
+++ b/sys/config/include/config/config.h
@@ -57,13 +57,15 @@ struct conf_handler {
       enum conf_export_tgt tgt);
 };
 
-int conf_init(void);
+void conf_init(void);
 int conf_register(struct conf_handler *);
 int conf_load(void);
 
 int conf_save(void);
 int conf_save_one(const char *name, char *var);
 
+void conf_store_init(void);
+
 /*
   XXXX for later
   int conf_save_lib(char *name);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/config/pkg.yml b/sys/config/pkg.yml
index a31f33e..e005530 100644
--- a/sys/config/pkg.yml
+++ b/sys/config/pkg.yml
@@ -25,23 +25,44 @@ pkg.keywords:
 
 pkg.deps:
     - libs/util
-    - libs/testutil
-pkg.deps.SHELL:
+pkg.deps.CONFIG_CLI:
     - libs/shell
-pkg.req_apis.SHELL:
-    - console
-pkg.cflags.SHELL: -DSHELL_PRESENT
-pkg.deps.NEWTMGR:
+pkg.deps.CONFIG_NEWTMGR:
     - libs/newtmgr
     - libs/json
-pkg.cflags.NEWTMGR: -DNEWTMGR_PRESENT
-pkg.deps.FCB:
+pkg.deps.CONFIG_FCB:
     - sys/fcb
-pkg.cflags.FCB: -DFCB_PRESENT
-pkg.deps.FS:
-    - fs/fs
-pkg.cflags.FS: -DFS_PRESENT
-
-pkg.deps.TEST:
+pkg.deps.CONFIG_NFFS:
     - fs/nffs
-    - sys/fcb
+
+pkg.init_function: config_pkg_init
+pkg.init_stage: 5
+
+pkg.syscfg_defs:
+    CONFIG_FCB:
+        description: 'TBD'
+        value: 1
+    CONFIG_FCB_MAGIC:
+        description: 'TBD'
+        value: 0xc09f6e5e
+
+    CONFIG_NFFS:
+        description: 'TBD'
+        value: 'MYNEWT_PKG_FS_NFFS'
+    CONFIG_NFFS_DIR:
+        description: 'TBD'
+        value: '"/cfg"'
+    CONFIG_NFFS_FILE:
+        description: 'TBD'
+        value: '"/cfg/run"'
+    CONFIG_NFFS_MAX_LINES:
+        description: 'TBD'
+        value: 32
+
+    CONFIG_NEWTMGR:
+        description: 'TBD'
+        value: 0
+
+    CONFIG_CLI:
+        description: 'TBD'
+        value: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/config.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config.c b/sys/config/src/config.c
index c21cec7..e37efb8 100644
--- a/sys/config/src/config.c
+++ b/sys/config/src/config.c
@@ -20,37 +20,42 @@
 #include <string.h>
 #include <stdio.h>
 
-#include <os/os.h>
-#include <util/base64.h>
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
+#include "os/os.h"
+#include "util/base64.h"
 
 #include "config/config.h"
 #include "config_priv.h"
 
-struct conf_handler_head conf_handlers = SLIST_HEAD_INITIALIZER(&conf_handlers);
+struct conf_handler_head conf_handlers;
 
 static uint8_t conf_cmd_inited;
 
-int
+void
 conf_init(void)
 {
     int rc;
 
-    rc = 0;
+    SLIST_INIT(&conf_handlers);
+    conf_store_init();
+
     if (conf_cmd_inited) {
-        goto done;
+        return;
     }
-#ifdef SHELL_PRESENT
+
+    (void)rc;
+
+#if MYNEWT_VAL(CONFIG_CLI)
     rc = conf_cli_register();
+    SYSINIT_PANIC_ASSERT(rc == 0);
 #endif
-#ifdef NEWTMGR_PRESENT
+#if MYNEWT_VAL(CONFIG_NEWTMGR)
     rc = conf_nmgr_register();
+    SYSINIT_PANIC_ASSERT(rc == 0);
 #endif
-    if (!rc) {
-        conf_cmd_inited = 1;
-    }
 
-done:
-    return rc;
+    conf_cmd_inited = 1;
 }
 
 int

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/config_cli.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_cli.c b/sys/config/src/config_cli.c
index 2923845..7cd2a3d 100644
--- a/sys/config/src/config_cli.c
+++ b/sys/config/src/config_cli.c
@@ -19,10 +19,11 @@
 
 #include <stddef.h>
 
+#include "syscfg/syscfg.h"
 #include "config/config.h"
 #include "config_priv.h"
 
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(CONFIG_CLI)
 #include <string.h>
 
 #include <shell/shell.h>

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/config_fcb.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_fcb.c b/sys/config/src/config_fcb.c
index cc6debf..94a772f 100644
--- a/sys/config/src/config_fcb.c
+++ b/sys/config/src/config_fcb.c
@@ -17,7 +17,10 @@
  * under the License.
  */
 
-#ifdef FCB_PRESENT
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(CONFIG_FCB)
+
 #include <os/os.h>
 #include <fcb/fcb.h>
 #include <string.h>

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/config_file.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_file.c b/sys/config/src/config_file.c
index c6d744d..fd518d5 100644
--- a/sys/config/src/config_file.c
+++ b/sys/config/src/config_file.c
@@ -17,7 +17,9 @@
  * under the License.
  */
 
-#ifdef FS_PRESENT
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(CONFIG_NFFS)
 
 #include <string.h>
 #include <assert.h>

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/config_init.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_init.c b/sys/config/src/config_init.c
new file mode 100644
index 0000000..0b3a503
--- /dev/null
+++ b/sys/config/src/config_init.c
@@ -0,0 +1,105 @@
+/**
+ * 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 <assert.h>
+
+#include "sysinit/sysinit.h"
+#include "syscfg/syscfg.h"
+#include "sysinit/sysinit.h"
+#include "bsp/bsp.h"
+
+#include "config/config.h"
+#include "config/config_file.h"
+
+#if MYNEWT_VAL(CONFIG_NFFS)
+#include "fs/fs.h"
+
+static struct conf_file config_init_conf_file = {
+    .cf_name = MYNEWT_VAL(CONFIG_NFFS_FILE),
+    .cf_maxlines = MYNEWT_VAL(CONFIG_NFFS_MAX_LINES)
+};
+
+static void
+config_init_fs(void)
+{
+    int rc;
+
+    fs_mkdir(MYNEWT_VAL(CONFIG_NFFS_DIR));
+    rc = conf_file_src(&config_init_conf_file);
+    SYSINIT_PANIC_ASSERT(rc == 0);
+    rc = conf_file_dst(&config_init_conf_file);
+    SYSINIT_PANIC_ASSERT(rc == 0);
+}
+
+#elif MYNEWT_VAL(CONFIG_FCB)
+#include "fcb/fcb.h"
+#include "config/config_fcb.h"
+
+static struct flash_area conf_fcb_area[NFFS_AREA_MAX + 1];
+
+static struct conf_fcb config_init_conf_fcb = {
+    .cf_fcb.f_magic = MYNEWT_VAL(CONFIG_FCB_MAGIC),
+    .cf_fcb.f_sectors = conf_fcb_area,
+};
+
+static void
+config_init_fcb(void)
+{
+    int cnt;
+    int rc;
+
+    rc = flash_area_to_sectors(FLASH_AREA_NFFS, &cnt, NULL);
+    SYSINIT_PANIC_ASSERT(rc == 0);
+    SYSINIT_PANIC_ASSERT(
+        cnt <= sizeof(conf_fcb_area) / sizeof(conf_fcb_area[0]));
+    flash_area_to_sectors(FLASH_AREA_NFFS, &cnt, conf_fcb_area);
+
+    config_init_conf_fcb.cf_fcb.f_sector_cnt = cnt;
+
+    rc = conf_fcb_src(&config_init_conf_fcb);
+    if (rc) {
+        for (cnt = 0;
+             cnt < config_init_conf_fcb.cf_fcb.f_sector_cnt;
+             cnt++) {
+
+            flash_area_erase(&conf_fcb_area[cnt], 0,
+                             conf_fcb_area[cnt].fa_size);
+        }
+        rc = conf_fcb_src(&config_init_conf_fcb);
+    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
+    rc = conf_fcb_dst(&config_init_conf_fcb);
+    SYSINIT_PANIC_ASSERT(rc == 0);
+}
+
+#else
+#error "Need NFFS or FCB for config storage"
+#endif
+
+void
+config_pkg_init(void)
+{
+    conf_init();
+
+#if MYNEWT_VAL(CONFIG_NFFS)
+    config_init_fs();
+#elif MYNEWT_VAL(CONFIG_FCB)
+    config_init_fcb();
+#endif
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/config_json_line.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_json_line.c b/sys/config/src/config_json_line.c
index d8dc745..219ca85 100644
--- a/sys/config/src/config_json_line.c
+++ b/sys/config/src/config_json_line.c
@@ -16,12 +16,14 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#ifdef NEWTMGR_PRESENT
+
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(CONFIG_NEWTMGR)
 
 #include "config/config.h"
 #include "config_priv.h"
-
-#include <json/json.h>
+#include "json/json.h"
 
 int
 conf_json_line(struct json_buffer *jb, char *name, int nlen, char *value,

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/config_nmgr.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_nmgr.c b/sys/config/src/config_nmgr.c
index ae4aa11..2470b00 100644
--- a/sys/config/src/config_nmgr.c
+++ b/sys/config/src/config_nmgr.c
@@ -17,12 +17,14 @@
  * under the License.
  */
 
-#ifdef NEWTMGR_PRESENT
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(CONFIG_NEWTMGR)
 
 #include <string.h>
 
-#include <newtmgr/newtmgr.h>
-#include <json/json.h>
+#include "newtmgr/newtmgr.h"
+#include "json/json.h"
 
 #include "config/config.h"
 #include "config_priv.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/config_store.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_store.c b/sys/config/src/config_store.c
index 87f9955..8ae3472 100644
--- a/sys/config/src/config_store.c
+++ b/sys/config/src/config_store.c
@@ -31,7 +31,7 @@ struct conf_dup_check_arg {
     int is_dup;
 };
 
-struct conf_store_head conf_load_srcs = SLIST_HEAD_INITIALIZER(&conf_load_srcs);
+struct conf_store_head conf_load_srcs;
 struct conf_store *conf_save_dst;
 
 void
@@ -170,3 +170,9 @@ conf_save(void)
     }
     return rc;
 }
+
+void
+conf_store_init(void)
+{
+    SLIST_INIT(&conf_load_srcs);
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/test/conf_test.c
----------------------------------------------------------------------
diff --git a/sys/config/src/test/conf_test.c b/sys/config/src/test/conf_test.c
deleted file mode 100644
index cde1cca..0000000
--- a/sys/config/src/test/conf_test.c
+++ /dev/null
@@ -1,953 +0,0 @@
-/**
- * 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 <stdio.h>
-#include <string.h>
-
-#include <os/os.h>
-#include <testutil/testutil.h>
-#include <nffs/nffs.h>
-#include <fs/fs.h>
-#include <fs/fsutil.h>
-#include <fcb/fcb.h>
-#include "config/config.h"
-#include "config/config_file.h"
-#include "config/config_fcb.h"
-#include "test/config_test.h"
-#include "config/../../src/config_priv.h"
-
-static uint8_t val8;
-int c2_var_count = 1;
-static char val_string[64][CONF_MAX_VAL_LEN];
-
-static uint32_t val32;
-
-static int test_get_called;
-static int test_set_called;
-static int test_commit_called;
-static int test_export_block;
-
-static char *ctest_handle_get(int argc, char **argv, char *val,
-  int val_len_max);
-static int ctest_handle_set(int argc, char **argv, char *val);
-static int ctest_handle_commit(void);
-static int ctest_handle_export(void (*cb)(char *name, char *value),
-  enum conf_export_tgt tgt);
-static char *c2_handle_get(int argc, char **argv, char *val,
-  int val_len_max);
-static int c2_handle_set(int argc, char **argv, char *val);
-static int c2_handle_export(void (*cb)(char *name, char *value),
-  enum conf_export_tgt tgt);
-static char *c3_handle_get(int argc, char **argv, char *val,
-  int val_len_max);
-static int c3_handle_set(int argc, char **argv, char *val);
-static int c3_handle_export(void (*cb)(char *name, char *value),
-  enum conf_export_tgt tgt);
-
-struct conf_handler config_test_handler = {
-    .ch_name = "myfoo",
-    .ch_get = ctest_handle_get,
-    .ch_set = ctest_handle_set,
-    .ch_commit = ctest_handle_commit,
-    .ch_export = ctest_handle_export
-};
-
-static char *
-ctest_handle_get(int argc, char **argv, char *val, int val_len_max)
-{
-    test_get_called = 1;
-    if (argc == 1 && !strcmp(argv[0], "mybar")) {
-        return conf_str_from_value(CONF_INT8, &val8, val, val_len_max);
-    }
-    return NULL;
-}
-
-static int
-ctest_handle_set(int argc, char **argv, char *val)
-{
-    uint8_t newval;
-    int rc;
-
-    test_set_called = 1;
-    if (argc == 1 && !strcmp(argv[0], "mybar")) {
-        rc = CONF_VALUE_SET(val, CONF_INT8, newval);
-        TEST_ASSERT(rc == 0);
-        val8 = newval;
-        return 0;
-    }
-    return OS_ENOENT;
-}
-
-static int
-ctest_handle_commit(void)
-{
-    test_commit_called = 1;
-    return 0;
-}
-
-static int
-ctest_handle_export(void (*cb)(char *name, char *value),
-  enum conf_export_tgt tgt)
-{
-    char value[32];
-
-    if (test_export_block) {
-        return 0;
-    }
-    conf_str_from_value(CONF_INT8, &val8, value, sizeof(value));
-    cb("myfoo/mybar", value);
-
-    return 0;
-}
-
-struct conf_handler c2_test_handler = {
-    .ch_name = "2nd",
-    .ch_get = c2_handle_get,
-    .ch_set = c2_handle_set,
-    .ch_commit = NULL,
-    .ch_export = c2_handle_export
-};
-
-char *
-c2_var_find(char *name)
-{
-    int idx = 0;
-    int len;
-    char *eptr;
-
-    len = strlen(name);
-    TEST_ASSERT(!strncmp(name, "string", 6));
-    TEST_ASSERT(len > 6);
-
-    idx = strtoul(&name[6], &eptr, 10);
-    TEST_ASSERT(*eptr == '\0');
-    TEST_ASSERT(idx < c2_var_count);
-    return val_string[idx];
-}
-
-static char *
-c2_handle_get(int argc, char **argv, char *val, int val_len_max)
-{
-    int len;
-    char *valptr;
-
-    if (argc == 1) {
-        valptr = c2_var_find(argv[0]);
-        if (!valptr) {
-            return NULL;
-        }
-        len = strlen(val_string[0]);
-        if (len > val_len_max) {
-            len = val_len_max;
-        }
-        strncpy(val, valptr, len);
-    }
-    return NULL;
-}
-
-static int
-c2_handle_set(int argc, char **argv, char *val)
-{
-    char *valptr;
-
-    if (argc == 1) {
-        valptr = c2_var_find(argv[0]);
-        if (!valptr) {
-            return OS_ENOENT;
-        }
-        if (val) {
-            strncpy(valptr, val, sizeof(val_string[0]));
-        } else {
-            memset(valptr, 0, sizeof(val_string[0]));
-        }
-        return 0;
-    }
-    return OS_ENOENT;
-}
-
-static int
-c2_handle_export(void (*cb)(char *name, char *value),
-  enum conf_export_tgt tgt)
-{
-    int i;
-    char name[32];
-
-    for (i = 0; i < c2_var_count; i++) {
-        snprintf(name, sizeof(name), "2nd/string%d", i);
-        cb(name, val_string[i]);
-    }
-    return 0;
-}
-
-struct conf_handler c3_test_handler = {
-    .ch_name = "3",
-    .ch_get = c3_handle_get,
-    .ch_set = c3_handle_set,
-    .ch_commit = NULL,
-    .ch_export = c3_handle_export
-};
-
-static char *
-c3_handle_get(int argc, char **argv, char *val, int val_len_max)
-{
-    if (argc == 1 && !strcmp(argv[0], "v")) {
-        return conf_str_from_value(CONF_INT32, &val32, val, val_len_max);
-    }
-    return NULL;
-}
-
-static int
-c3_handle_set(int argc, char **argv, char *val)
-{
-    uint32_t newval;
-    int rc;
-
-    if (argc == 1 && !strcmp(argv[0], "v")) {
-        rc = CONF_VALUE_SET(val, CONF_INT32, newval);
-        TEST_ASSERT(rc == 0);
-        val32 = newval;
-        return 0;
-    }
-    return OS_ENOENT;
-}
-
-static int
-c3_handle_export(void (*cb)(char *name, char *value),
-  enum conf_export_tgt tgt)
-{
-    char value[32];
-
-    conf_str_from_value(CONF_INT32, &val32, value, sizeof(value));
-    cb("3/v", value);
-
-    return 0;
-}
-
-static void
-ctest_clear_call_state(void)
-{
-    test_get_called = 0;
-    test_set_called = 0;
-    test_commit_called = 0;
-}
-
-static int
-ctest_get_call_state(void)
-{
-    return test_get_called + test_set_called + test_commit_called;
-}
-
-TEST_CASE(config_empty_lookups)
-{
-    int rc;
-    char name[80];
-    char tmp[64], *str;
-
-    strcpy(name, "foo/bar");
-    rc = conf_set_value(name, "tmp");
-    TEST_ASSERT(rc != 0);
-
-    strcpy(name, "foo/bar");
-    str = conf_get_value(name, tmp, sizeof(tmp));
-    TEST_ASSERT(str == NULL);
-}
-
-TEST_CASE(config_test_insert)
-{
-    int rc;
-
-    rc = conf_register(&config_test_handler);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(config_test_insert2)
-{
-    int rc;
-
-    rc = conf_register(&c2_test_handler);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(config_test_getset_unknown)
-{
-    char name[80];
-    char tmp[64], *str;
-    int rc;
-
-    strcpy(name, "foo/bar");
-    rc = conf_set_value(name, "tmp");
-    TEST_ASSERT(rc != 0);
-    TEST_ASSERT(ctest_get_call_state() == 0);
-
-    strcpy(name, "foo/bar");
-    str = conf_get_value(name, tmp, sizeof(tmp));
-    TEST_ASSERT(str == NULL);
-    TEST_ASSERT(ctest_get_call_state() == 0);
-
-    strcpy(name, "myfoo/bar");
-    rc = conf_set_value(name, "tmp");
-    TEST_ASSERT(rc == OS_ENOENT);
-    TEST_ASSERT(test_set_called == 1);
-    ctest_clear_call_state();
-
-    strcpy(name, "myfoo/bar");
-    str = conf_get_value(name, tmp, sizeof(tmp));
-    TEST_ASSERT(str == NULL);
-    TEST_ASSERT(test_get_called == 1);
-    ctest_clear_call_state();
-}
-
-TEST_CASE(config_test_getset_int)
-{
-    char name[80];
-    char tmp[64], *str;
-    int rc;
-
-    strcpy(name, "myfoo/mybar");
-    rc = conf_set_value(name, "42");
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(test_set_called == 1);
-    TEST_ASSERT(val8 == 42);
-    ctest_clear_call_state();
-
-    strcpy(name, "myfoo/mybar");
-    str = conf_get_value(name, tmp, sizeof(tmp));
-    TEST_ASSERT(str);
-    TEST_ASSERT(test_get_called == 1);
-    TEST_ASSERT(!strcmp("42", tmp));
-    ctest_clear_call_state();
-}
-
-TEST_CASE(config_test_getset_bytes)
-{
-    char orig[32];
-    char bytes[32];
-    char str[48];
-    char *ret;
-    int j, i;
-    int tmp;
-    int rc;
-
-    for (j = 1; j < sizeof(orig); j++) {
-        for (i = 0; i < j; i++) {
-            orig[i] = i + j + 1;
-        }
-        ret = conf_str_from_bytes(orig, j, str, sizeof(str));
-        TEST_ASSERT(ret);
-        tmp = strlen(str);
-        TEST_ASSERT(tmp < sizeof(str));
-
-        memset(bytes, 0, sizeof(bytes));
-        tmp = sizeof(bytes);
-
-        tmp = sizeof(bytes);
-        rc = conf_bytes_from_str(str, bytes, &tmp);
-        TEST_ASSERT(rc == 0);
-        TEST_ASSERT(tmp == j);
-        TEST_ASSERT(!memcmp(orig, bytes, j));
-    }
-}
-
-TEST_CASE(config_test_commit)
-{
-    char name[80];
-    int rc;
-
-    strcpy(name, "bar");
-    rc = conf_commit(name);
-    TEST_ASSERT(rc);
-    TEST_ASSERT(ctest_get_call_state() == 0);
-
-    rc = conf_commit(NULL);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(test_commit_called == 1);
-    ctest_clear_call_state();
-
-    strcpy(name, "myfoo");
-    rc = conf_commit(name);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(test_commit_called == 1);
-    ctest_clear_call_state();
-}
-
-static const struct nffs_area_desc config_nffs[] = {
-    { 0x00000000, 16 * 1024 },
-    { 0x00004000, 16 * 1024 },
-    { 0x00008000, 16 * 1024 },
-    { 0x0000c000, 16 * 1024 },
-    { 0, 0 }
-};
-
-TEST_CASE(config_setup_nffs)
-{
-    int rc;
-
-    rc = nffs_init();
-    TEST_ASSERT_FATAL(rc == 0);
-    rc = nffs_format(config_nffs);
-    TEST_ASSERT_FATAL(rc == 0);
-}
-
-static void config_wipe_srcs(void)
-{
-    SLIST_INIT(&conf_load_srcs);
-    conf_save_dst = NULL;
-}
-
-static void config_wipe_fcb(struct flash_area *fa, int cnt)
-{
-    int i;
-
-    for (i = 0; i < cnt; i++) {
-        flash_area_erase(&fa[i], 0, fa[i].fa_size);
-    }
-}
-
-TEST_CASE(config_test_empty_file)
-{
-    int rc;
-    struct conf_file cf_mfg;
-    struct conf_file cf_running;
-    const char cf_mfg_test[] = "";
-    const char cf_running_test[] = "\n\n";
-
-    config_wipe_srcs();
-
-    cf_mfg.cf_name = "/config/mfg";
-    cf_running.cf_name = "/config/running";
-
-    rc = conf_file_src(&cf_mfg);
-    TEST_ASSERT(rc == 0);
-    rc = conf_file_src(&cf_running);
-
-    /*
-     * No files
-     */
-    conf_load();
-
-    rc = fs_mkdir("/config");
-    TEST_ASSERT(rc == 0);
-
-    rc = fsutil_write_file("/config/mfg", cf_mfg_test, sizeof(cf_mfg_test));
-    TEST_ASSERT(rc == 0);
-
-    rc = fsutil_write_file("/config/running", cf_running_test,
-      sizeof(cf_running_test));
-    TEST_ASSERT(rc == 0);
-
-    conf_load();
-    config_wipe_srcs();
-    ctest_clear_call_state();
-}
-
-TEST_CASE(config_test_small_file)
-{
-    int rc;
-    struct conf_file cf_mfg;
-    struct conf_file cf_running;
-    const char cf_mfg_test[] = "myfoo/mybar=1";
-    const char cf_running_test[] = " myfoo/mybar = 8 ";
-
-    config_wipe_srcs();
-
-    cf_mfg.cf_name = "/config/mfg";
-    cf_running.cf_name = "/config/running";
-
-    rc = conf_file_src(&cf_mfg);
-    TEST_ASSERT(rc == 0);
-    rc = conf_file_src(&cf_running);
-
-    rc = fsutil_write_file("/config/mfg", cf_mfg_test, sizeof(cf_mfg_test));
-    TEST_ASSERT(rc == 0);
-
-    conf_load();
-    TEST_ASSERT(test_set_called);
-    TEST_ASSERT(val8 == 1);
-
-    ctest_clear_call_state();
-
-    rc = fsutil_write_file("/config/running", cf_running_test,
-      sizeof(cf_running_test));
-    TEST_ASSERT(rc == 0);
-
-    conf_load();
-    TEST_ASSERT(test_set_called);
-    TEST_ASSERT(val8 == 8);
-
-    ctest_clear_call_state();
-}
-
-TEST_CASE(config_test_multiple_in_file)
-{
-    int rc;
-    struct conf_file cf_mfg;
-    const char cf_mfg_test1[] =
-      "myfoo/mybar=1\n"
-      "myfoo/mybar=14";
-    const char cf_mfg_test2[] =
-      "myfoo/mybar=1\n"
-      "myfoo/mybar=15\n"
-      "\n";
-
-    config_wipe_srcs();
-
-    cf_mfg.cf_name = "/config/mfg";
-    rc = conf_file_src(&cf_mfg);
-    TEST_ASSERT(rc == 0);
-
-    rc = fsutil_write_file("/config/mfg", cf_mfg_test1, sizeof(cf_mfg_test1));
-    TEST_ASSERT(rc == 0);
-
-    conf_load();
-    TEST_ASSERT(test_set_called);
-    TEST_ASSERT(val8 == 14);
-
-    rc = fsutil_write_file("/config/mfg", cf_mfg_test2, sizeof(cf_mfg_test2));
-    TEST_ASSERT(rc == 0);
-
-    conf_load();
-    TEST_ASSERT(test_set_called);
-    TEST_ASSERT(val8 == 15);
-}
-
-int
-conf_test_file_strstr(const char *fname, char *string)
-{
-    int rc;
-    uint32_t len;
-    uint32_t rlen;
-    char *buf;
-    struct fs_file *file;
-
-    rc = fs_open(fname, FS_ACCESS_READ, &file);
-    if (rc) {
-        return rc;
-    }
-    rc = fs_filelen(file, &len);
-    fs_close(file);
-    if (rc) {
-        return rc;
-    }
-
-    buf = (char *)malloc(len + 1);
-    TEST_ASSERT(buf);
-
-    rc = fsutil_read_file(fname, 0, len, buf, &rlen);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(rlen == len);
-    buf[rlen] = '\0';
-
-    if (strstr(buf, string)) {
-        return 0;
-    } else {
-        return -1;
-    }
-}
-
-TEST_CASE(config_test_save_in_file)
-{
-    int rc;
-    struct conf_file cf;
-
-    config_wipe_srcs();
-
-    rc = fs_mkdir("/config");
-    TEST_ASSERT(rc == 0 || rc == FS_EEXIST);
-
-    cf.cf_name = "/config/blah";
-    rc = conf_file_src(&cf);
-    TEST_ASSERT(rc == 0);
-    rc = conf_file_dst(&cf);
-    TEST_ASSERT(rc == 0);
-
-    val8 = 8;
-    rc = conf_save();
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_test_file_strstr(cf.cf_name, "myfoo/mybar=8\n");
-    TEST_ASSERT(rc == 0);
-
-    val8 = 43;
-    rc = conf_save();
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_test_file_strstr(cf.cf_name, "myfoo/mybar=43\n");
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(config_test_save_one_file)
-{
-    int rc;
-    struct conf_file cf;
-
-    config_wipe_srcs();
-    rc = fs_mkdir("/config");
-    TEST_ASSERT(rc == 0 || rc == FS_EEXIST);
-
-    cf.cf_name = "/config/blah";
-    rc = conf_file_src(&cf);
-    TEST_ASSERT(rc == 0);
-    rc = conf_file_dst(&cf);
-    TEST_ASSERT(rc == 0);
-
-    val8 = 33;
-    rc = conf_save();
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_save_one("myfoo/mybar", "42");
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_load();
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(val8 == 42);
-
-    rc = conf_save_one("myfoo/mybar", "44");
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_load();
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(val8 == 44);
-}
-
-struct flash_area fcb_areas[] = {
-    [0] = {
-        .fa_off = 0x00000000,
-        .fa_size = 16 * 1024
-    },
-    [1] = {
-        .fa_off = 0x00004000,
-        .fa_size = 16 * 1024
-    },
-    [2] = {
-        .fa_off = 0x00008000,
-        .fa_size = 16 * 1024
-    },
-    [3] = {
-        .fa_off = 0x0000c000,
-        .fa_size = 16 * 1024
-    }
-};
-
-TEST_CASE(config_test_empty_fcb)
-{
-    int rc;
-    struct conf_fcb cf;
-
-    config_wipe_srcs();
-    config_wipe_fcb(fcb_areas, sizeof(fcb_areas) / sizeof(fcb_areas[0]));
-
-    cf.cf_fcb.f_sectors = fcb_areas;
-    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
-
-    rc = conf_fcb_src(&cf);
-    TEST_ASSERT(rc == 0);
-
-    /*
-     * No values
-     */
-    conf_load();
-
-    config_wipe_srcs();
-    ctest_clear_call_state();
-}
-
-TEST_CASE(config_test_save_1_fcb)
-{
-    int rc;
-    struct conf_fcb cf;
-
-    config_wipe_srcs();
-
-    cf.cf_fcb.f_sectors = fcb_areas;
-    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
-
-    rc = conf_fcb_src(&cf);
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_fcb_dst(&cf);
-    TEST_ASSERT(rc == 0);
-
-    val8 = 33;
-    rc = conf_save();
-    TEST_ASSERT(rc == 0);
-
-    val8 = 0;
-
-    rc = conf_load();
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(val8 == 33);
-}
-
-static void config_test_fill_area(char test_value[64][CONF_MAX_VAL_LEN],
-  int iteration)
-{
-      int i, j;
-
-      for (j = 0; j < 64; j++) {
-          for (i = 0; i < CONF_MAX_VAL_LEN; i++) {
-              test_value[j][i] = ((j * 2) + i + iteration) % 10 + '0';
-          }
-          test_value[j][sizeof(test_value[j]) - 1] = '\0';
-      }
-}
-
-TEST_CASE(config_test_save_2_fcb)
-{
-    int rc;
-    struct conf_fcb cf;
-    char test_value[64][CONF_MAX_VAL_LEN];
-    int i;
-
-    config_wipe_srcs();
-
-    cf.cf_fcb.f_sectors = fcb_areas;
-    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
-
-    rc = conf_fcb_src(&cf);
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_fcb_dst(&cf);
-    TEST_ASSERT(rc == 0);
-
-    config_test_fill_area(test_value, 0);
-    memcpy(val_string, test_value, sizeof(val_string));
-
-    val8 = 42;
-    rc = conf_save();
-    TEST_ASSERT(rc == 0);
-
-    val8 = 0;
-    memset(val_string[0], 0, sizeof(val_string[0]));
-    rc = conf_load();
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(val8 == 42);
-    TEST_ASSERT(!strcmp(val_string[0], test_value[0]));
-    test_export_block = 1;
-
-    /*
-     * Now add the number of settings to max. Keep adjusting the test_data,
-     * check that rollover happens when it's supposed to.
-     */
-    c2_var_count = 64;
-
-    for (i = 0; i < 32; i++) {
-        config_test_fill_area(test_value, i);
-        memcpy(val_string, test_value, sizeof(val_string));
-
-        rc = conf_save();
-        TEST_ASSERT(rc == 0);
-
-        memset(val_string, 0, sizeof(val_string));
-
-        val8 = 0;
-        rc = conf_load();
-        TEST_ASSERT(rc == 0);
-        TEST_ASSERT(!memcmp(val_string, test_value, sizeof(val_string)));
-        TEST_ASSERT(val8 == 42);
-    }
-    c2_var_count = 0;
-}
-
-TEST_CASE(config_test_insert3)
-{
-    int rc;
-
-    rc = conf_register(&c3_test_handler);
-    TEST_ASSERT(rc == 0);
-}
-
-TEST_CASE(config_test_save_3_fcb)
-{
-    int rc;
-    struct conf_fcb cf;
-    int i;
-
-    config_wipe_srcs();
-    config_wipe_fcb(fcb_areas, sizeof(fcb_areas) / sizeof(fcb_areas[0]));
-
-    cf.cf_fcb.f_sectors = fcb_areas;
-    cf.cf_fcb.f_sector_cnt = 4;
-
-    rc = conf_fcb_src(&cf);
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_fcb_dst(&cf);
-    TEST_ASSERT(rc == 0);
-
-    for (i = 0; i < 4096; i++) {
-        val32 = i;
-
-        rc = conf_save();
-        TEST_ASSERT(rc == 0);
-
-        val32 = 0;
-
-        rc = conf_load();
-        TEST_ASSERT(rc == 0);
-        TEST_ASSERT(val32 == i);
-    }
-}
-
-TEST_CASE(config_test_compress_reset)
-{
-    int rc;
-    struct conf_fcb cf;
-    struct flash_area *fa;
-    char test_value[64][CONF_MAX_VAL_LEN];
-    int elems[4];
-    int i;
-
-    config_wipe_srcs();
-    config_wipe_fcb(fcb_areas, sizeof(fcb_areas) / sizeof(fcb_areas[0]));
-
-    cf.cf_fcb.f_sectors = fcb_areas;
-    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
-
-    rc = conf_fcb_src(&cf);
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_fcb_dst(&cf);
-    TEST_ASSERT(rc == 0);
-
-    c2_var_count = 1;
-    memset(elems, 0, sizeof(elems));
-
-    for (i = 0; ; i++) {
-        config_test_fill_area(test_value, i);
-        memcpy(val_string, test_value, sizeof(val_string));
-
-        rc = conf_save();
-        TEST_ASSERT(rc == 0);
-
-        if (cf.cf_fcb.f_active.fe_area == &fcb_areas[2]) {
-            /*
-             * Started using space just before scratch.
-             */
-            break;
-        }
-        memset(val_string, 0, sizeof(val_string));
-
-        rc = conf_load();
-        TEST_ASSERT(rc == 0);
-        TEST_ASSERT(!memcmp(val_string, test_value, CONF_MAX_VAL_LEN));
-    }
-
-    fa = cf.cf_fcb.f_active.fe_area;
-    rc = fcb_append_to_scratch(&cf.cf_fcb);
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(fcb_free_sector_cnt(&cf.cf_fcb) == 0);
-    TEST_ASSERT(fa != cf.cf_fcb.f_active.fe_area);
-
-    config_wipe_srcs();
-
-    memset(&cf, 0, sizeof(cf));
-
-    cf.cf_fcb.f_sectors = fcb_areas;
-    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
-
-    rc = conf_fcb_src(&cf);
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_fcb_dst(&cf);
-    TEST_ASSERT(rc == 0);
-
-    TEST_ASSERT(fcb_free_sector_cnt(&cf.cf_fcb) == 1);
-    TEST_ASSERT(fa == cf.cf_fcb.f_active.fe_area);
-
-    c2_var_count = 0;
-}
-
-TEST_CASE(config_test_save_one_fcb)
-{
-    int rc;
-    struct conf_fcb cf;
-
-    config_wipe_srcs();
-    config_wipe_fcb(fcb_areas, sizeof(fcb_areas) / sizeof(fcb_areas[0]));
-
-    cf.cf_fcb.f_sectors = fcb_areas;
-    cf.cf_fcb.f_sector_cnt = sizeof(fcb_areas) / sizeof(fcb_areas[0]);
-
-    rc = conf_fcb_src(&cf);
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_fcb_dst(&cf);
-    TEST_ASSERT(rc == 0);
-
-    val8 = 33;
-    rc = conf_save();
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_save_one("myfoo/mybar", "42");
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_load();
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(val8 == 42);
-
-    rc = conf_save_one("myfoo/mybar", "44");
-    TEST_ASSERT(rc == 0);
-
-    rc = conf_load();
-    TEST_ASSERT(rc == 0);
-    TEST_ASSERT(val8 == 44);
-}
-
-TEST_SUITE(config_test_all)
-{
-    /*
-     * Config tests.
-     */
-    config_empty_lookups();
-    config_test_insert();
-    config_test_getset_unknown();
-    config_test_getset_int();
-    config_test_getset_bytes();
-
-    config_test_commit();
-
-    /*
-     * NFFS as backing storage.
-     */
-    config_setup_nffs();
-    config_test_empty_file();
-    config_test_small_file();
-    config_test_multiple_in_file();
-
-    config_test_save_in_file();
-
-    config_test_save_one_file();
-
-    /*
-     * FCB as backing storage.
-     */
-    config_test_empty_fcb();
-    config_test_save_1_fcb();
-
-    config_test_insert2();
-
-    config_test_save_2_fcb();
-
-    config_test_insert3();
-    config_test_save_3_fcb();
-
-    config_test_compress_reset();
-
-    config_test_save_one_fcb();
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/test/conf_test.h
----------------------------------------------------------------------
diff --git a/sys/config/src/test/conf_test.h b/sys/config/src/test/conf_test.h
deleted file mode 100644
index b50ff2f..0000000
--- a/sys/config/src/test/conf_test.h
+++ /dev/null
@@ -1,25 +0,0 @@
-/**
- * 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 _CONF_TEST_H_
-#define _CONF_TEST_H_
-
-void config_test_all(void);
-
-#endif /* _CONF_TEST_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/test/conf_test_suite.c
----------------------------------------------------------------------
diff --git a/sys/config/src/test/conf_test_suite.c b/sys/config/src/test/conf_test_suite.c
deleted file mode 100644
index ef059cb..0000000
--- a/sys/config/src/test/conf_test_suite.c
+++ /dev/null
@@ -1,39 +0,0 @@
-/**
- * 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 <testutil/testutil.h>
-#include "config/config.h"
-#include "test/config_test.h"
-
-#ifdef MYNEWT_SELFTEST
-
-int
-main(int argc, char **argv)
-{
-    tu_config.tc_print_results = 1;
-    tu_init();
-
-    conf_init();
-    config_test_all();
-
-    return tu_any_failed;
-}
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/src/test/config_test.h
----------------------------------------------------------------------
diff --git a/sys/config/src/test/config_test.h b/sys/config/src/test/config_test.h
deleted file mode 100644
index da531af..0000000
--- a/sys/config/src/test/config_test.h
+++ /dev/null
@@ -1,24 +0,0 @@
-/**
- * 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 _CONFIG_TEST_H_
-#define _CONFIG_TEST_H_
-
-int config_test_all();
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/sys/config/test/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/config/test/pkg.yml b/sys/config/test/pkg.yml
new file mode 100644
index 0000000..7b533a6
--- /dev/null
+++ b/sys/config/test/pkg.yml
@@ -0,0 +1,32 @@
+# 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.
+#
+pkg.name: sys/config/test
+pkg.type: unittest
+pkg.description: "Config unit tests."
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps: 
+    - libs/testutil
+    - sys/config
+
+pkg.deps.SELFTEST:
+    - fs/nffs
+    - sys/fcb
+    - libs/console/stub


[33/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/test/src/arch/sim/nffs_test_system_01.c
----------------------------------------------------------------------
diff --git a/fs/nffs/test/src/arch/sim/nffs_test_system_01.c b/fs/nffs/test/src/arch/sim/nffs_test_system_01.c
new file mode 100644
index 0000000..cd30544
--- /dev/null
+++ b/fs/nffs/test/src/arch/sim/nffs_test_system_01.c
@@ -0,0 +1,6537 @@
+/**
+ * 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.
+ */
+
+
+/** Generated by makefs.rb */
+
+#include <stddef.h>
+#include "nffs_test_priv.h"
+
+const struct nffs_test_file_desc *nffs_test_system_01 =
+    (struct nffs_test_file_desc[]) {
+{
+.filename = "",
+.is_dir = 1,
+.children = (struct nffs_test_file_desc[]) {
+ {
+ .filename = "lvl1dir-0000",
+ .is_dir = 1,
+ .children = (struct nffs_test_file_desc[]) {
+  {
+  .filename = "lvl2dir-0000",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0001",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0002",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0003",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0004",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+ { .filename = NULL, } }
+ },
+ {
+ .filename = "lvl1dir-0001",
+ .is_dir = 1,
+ .children = (struct nffs_test_file_desc[]) {
+  {
+  .filename = "lvl2dir-0000",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0001",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0002",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0003",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0004",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+ { .filename = NULL, } }
+ },
+ {
+ .filename = "lvl1dir-0002",
+ .is_dir = 1,
+ .children = (struct nffs_test_file_desc[]) {
+  {
+  .filename = "lvl2dir-0000",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0001",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0002",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0003",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0004",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+ { .filename = NULL, } }
+ },
+ {
+ .filename = "lvl1dir-0003",
+ .is_dir = 1,
+ .children = (struct nffs_test_file_desc[]) {
+  {
+  .filename = "lvl2dir-0000",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0001",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0002",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0003",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0004",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+ { .filename = NULL, } }
+ },
+ {
+ .filename = "lvl1dir-0004",
+ .is_dir = 1,
+ .children = (struct nffs_test_file_desc[]) {
+  {
+  .filename = "lvl2dir-0000",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0001",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0002",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0003",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0004",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+ { .filename = NULL, } }
+ },
+{ .filename = NULL, } }
+},
+};
+
+const struct nffs_test_file_desc *nffs_test_system_01_rm_1014_mk10 =
+    (struct nffs_test_file_desc[]) {
+{
+.filename = "",
+.is_dir = 1,
+.children = (struct nffs_test_file_desc[]) {
+ {
+ .filename = "lvl1dir-0000",
+ .is_dir = 1,
+ },
+ {
+ .filename = "lvl1dir-0001",
+ .is_dir = 1,
+ .children = (struct nffs_test_file_desc[]) {
+  {
+  .filename = "lvl2dir-0000",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0001",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0001",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0002",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0003",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+   {
+   .filename = "lvl3dir-0004",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0003",
+    .contents = "3",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0004",
+    .contents = "4",
+    .contents_len = 1,
+    },
+   { .filename = NULL, } }
+   },
+  { .filename = NULL, } }
+  },
+  {
+  .filename = "lvl2dir-0002",
+  .is_dir = 1,
+  .children = (struct nffs_test_file_desc[]) {
+   {
+   .filename = "lvl3dir-0000",
+   .is_dir = 1,
+   .children = (struct nffs_test_file_desc[]) {
+    {
+    .filename = "lvl4file-0000",
+    .contents = "0",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0001",
+    .contents = "1",
+    .contents_len = 1,
+    },
+    {
+    .filename = "lvl4file-0002",
+    .contents = "2",
+    .contents_

<TRUNCATED>


[40/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/fs/src/fs_cli.c
----------------------------------------------------------------------
diff --git a/fs/fs/src/fs_cli.c b/fs/fs/src/fs_cli.c
index 4498d87..d481c6e 100644
--- a/fs/fs/src/fs_cli.c
+++ b/fs/fs/src/fs_cli.c
@@ -17,7 +17,9 @@
  * under the License.
  */
 
-#ifdef SHELL_PRESENT
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(FS_CLI)
 
 #include <inttypes.h>
 #include <string.h>
@@ -227,4 +229,4 @@ fs_cli_init(void)
     shell_cmd_register(&fs_mv_struct);
     shell_cmd_register(&fs_cat_struct);
 }
-#endif /* SHELL_PRESENT */
+#endif /* MYNEWT_VAL(FS_CLI) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/fs/src/fs_mount.c
----------------------------------------------------------------------
diff --git a/fs/fs/src/fs_mount.c b/fs/fs/src/fs_mount.c
index 9653505..374fa1b 100644
--- a/fs/fs/src/fs_mount.c
+++ b/fs/fs/src/fs_mount.c
@@ -16,8 +16,10 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-#include <fs/fs.h>
-#include <fs/fs_if.h>
+
+#include "syscfg/syscfg.h"
+#include "fs/fs.h"
+#include "fs/fs_if.h"
 #include "fs_priv.h"
 
 const struct fs_ops *fs_root_ops = NULL;
@@ -30,7 +32,7 @@ fs_register(const struct fs_ops *fops)
     }
     fs_root_ops = fops;
 
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(FS_CLI)
     fs_cli_init();
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/fs/src/fs_priv.h
----------------------------------------------------------------------
diff --git a/fs/fs/src/fs_priv.h b/fs/fs/src/fs_priv.h
index af08e2f..f8f092d 100644
--- a/fs/fs/src/fs_priv.h
+++ b/fs/fs/src/fs_priv.h
@@ -19,11 +19,13 @@
 #ifndef __FS_PRIV_H__
 #define __FS_PRIV_H__
 
+#include "syscfg/syscfg.h"
+
 struct fs_ops;
 extern const struct fs_ops *fs_root_ops;
 
-#ifdef SHELL_PRESENT
+#if MYNEWT_VAL(FS_CLI)
 void fs_cli_init(void);
-#endif /* SHELL_PRESENT */
+#endif
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/include/nffs/nffs.h
----------------------------------------------------------------------
diff --git a/fs/nffs/include/nffs/nffs.h b/fs/nffs/include/nffs/nffs.h
index 73a0ec1..4a608cb 100644
--- a/fs/nffs/include/nffs/nffs.h
+++ b/fs/nffs/include/nffs/nffs.h
@@ -22,6 +22,7 @@
 
 #include <stddef.h>
 #include <inttypes.h>
+#include "fs/fs.h"
 
 #define NFFS_FILENAME_MAX_LEN   256  /* Does not require null terminator. */
 #define NFFS_MAX_AREAS          256
@@ -58,4 +59,6 @@ int nffs_init(void);
 int nffs_detect(const struct nffs_area_desc *area_descs);
 int nffs_format(const struct nffs_area_desc *area_descs);
 
+int nffs_misc_desc_from_flash_area(int idx, int *cnt, struct nffs_area_desc *nad);
+
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/pkg.yml
----------------------------------------------------------------------
diff --git a/fs/nffs/pkg.yml b/fs/nffs/pkg.yml
index 61e07e0..1a3f11d 100644
--- a/fs/nffs/pkg.yml
+++ b/fs/nffs/pkg.yml
@@ -26,10 +26,18 @@ pkg.keywords:
     - filesystem
     - ffs
 
-pkg.features: NFFS
 pkg.deps:
     - fs/fs
     - hw/hal
     - libs/os
     - libs/testutil
     - sys/log
+    - sys/stats
+
+pkg.init_function: nffs_pkg_init
+pkg.init_stage: 2
+
+pkg.syscfg_defs:
+    NFFS_DETECT_FAIL:
+        description: 'TBD'
+        value: 'NFFS_DETECT_FAIL_FORMAT'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/nffs.c
----------------------------------------------------------------------
diff --git a/fs/nffs/src/nffs.c b/fs/nffs/src/nffs.c
index 28fdeea..5ee63aa 100644
--- a/fs/nffs/src/nffs.c
+++ b/fs/nffs/src/nffs.c
@@ -21,7 +21,11 @@
 #include <string.h>
 #include <stdlib.h>
 #include <assert.h>
+
+#include "sysinit/sysinit.h"
+#include "bsp/bsp.h"
 #include "hal/hal_flash.h"
+#include "hal/flash_map.h"
 #include "os/os_mempool.h"
 #include "os/os_mutex.h"
 #include "os/os_malloc.h"
@@ -686,7 +690,6 @@ nffs_init(void)
         return FS_ENOMEM;
     }
 
-    log_init();
     log_console_handler_init(&nffs_log_console_handler);
     log_register("nffs", &nffs_log, &nffs_log_console_handler);
 
@@ -698,3 +701,52 @@ nffs_init(void)
     fs_register(&nffs_ops);
     return 0;
 }
+
+void
+nffs_pkg_init(void)
+{
+    struct nffs_area_desc descs[NFFS_AREA_MAX + 1];
+    int cnt;
+    int rc;
+
+    /* Initialize nffs's internal state. */
+    rc = nffs_init();
+    SYSINIT_PANIC_ASSERT(rc == 0);
+
+    /* Convert the set of flash blocks we intend to use for nffs into an array
+     * of nffs area descriptors.
+     */
+    cnt = NFFS_AREA_MAX;
+    rc = nffs_misc_desc_from_flash_area(FLASH_AREA_NFFS, &cnt, descs);
+    SYSINIT_PANIC_ASSERT(rc == 0);
+
+    /* Attempt to restore an existing nffs file system from flash. */
+    rc = nffs_detect(descs);
+    switch (rc) {
+    case 0:
+        break;
+
+    case FS_ECORRUPT:
+        /* No valid nffs instance detected; act based on configued detection
+         * failure policy.
+         */
+        switch (MYNEWT_VAL(NFFS_DETECT_FAIL)) {
+        case NFFS_DETECT_FAIL_IGNORE:
+            break;
+
+        case NFFS_DETECT_FAIL_FORMAT:
+            rc = nffs_format(descs);
+            SYSINIT_PANIC_ASSERT(rc == 0);
+            break;
+
+        default:
+            SYSINIT_PANIC();
+            break;
+        }
+        break;
+
+    default:
+        SYSINIT_PANIC();
+        break;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/nffs_misc.c
----------------------------------------------------------------------
diff --git a/fs/nffs/src/nffs_misc.c b/fs/nffs/src/nffs_misc.c
index cbf0e45..76591ca 100644
--- a/fs/nffs/src/nffs_misc.c
+++ b/fs/nffs/src/nffs_misc.c
@@ -18,6 +18,9 @@
  */
 
 #include <assert.h>
+#include "hal/flash_map.h"
+#include "hal/hal_bsp.h"
+#include "hal/hal_flash_int.h"
 #include "os/os_malloc.h"
 #include "nffs/nffs.h"
 #include "nffs_priv.h"
@@ -437,3 +440,70 @@ nffs_misc_ready(void)
 {
     return nffs_root_dir != NULL;
 }
+
+
+/*
+ * Turn flash region into a set of areas for NFFS use.
+ *
+ * Limit the number of regions we return to be less than *cnt.
+ * If sector count within region exceeds that, collect multiple sectors
+ * to a region.
+ */
+int
+nffs_misc_desc_from_flash_area(int idx, int *cnt, struct nffs_area_desc *nad)
+{
+    int i, j;
+    const struct hal_flash *hf;
+    const struct flash_area *fa;
+    int max_cnt, move_on;
+    int first_idx, last_idx;
+    uint32_t start, size;
+    uint32_t min_size;
+
+    if (!flash_map || idx >= flash_map_entries) {
+        return -1;
+    }
+    first_idx = last_idx = -1;
+    max_cnt = *cnt;
+    *cnt = 0;
+
+    fa = &flash_map[idx];
+
+    hf = bsp_flash_dev(fa->fa_flash_id);
+    for (i = 0; i < hf->hf_sector_cnt; i++) {
+        hf->hf_itf->hff_sector_info(i, &start, &size);
+        if (start >= fa->fa_off && start < fa->fa_off + fa->fa_size) {
+            if (first_idx == -1) {
+                first_idx = i;
+            }
+            last_idx = i;
+            *cnt = *cnt + 1;
+        }
+    }
+    if (*cnt > max_cnt) {
+        min_size = fa->fa_size / max_cnt;
+    } else {
+        min_size = 0;
+    }
+    *cnt = 0;
+
+    move_on = 1;
+    for (i = first_idx, j = 0; i < last_idx + 1; i++) {
+        hf->hf_itf->hff_sector_info(i, &start, &size);
+        if (move_on) {
+            nad[j].nad_flash_id = fa->fa_flash_id;
+            nad[j].nad_offset = start;
+            nad[j].nad_length = size;
+            *cnt = *cnt + 1;
+            move_on = 0;
+        } else {
+            nad[j].nad_length += size;
+        }
+        if (nad[j].nad_length >= min_size) {
+            j++;
+            move_on = 1;
+        }
+    }
+    nad[*cnt].nad_length = 0;
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/nffs_priv.h
----------------------------------------------------------------------
diff --git a/fs/nffs/src/nffs_priv.h b/fs/nffs/src/nffs_priv.h
index 47c4e9a..c8da07e 100644
--- a/fs/nffs/src/nffs_priv.h
+++ b/fs/nffs/src/nffs_priv.h
@@ -58,6 +58,9 @@
 
 #define NFFS_BLOCK_MAX_DATA_SZ_MAX   2048
 
+#define NFFS_DETECT_FAIL_IGNORE     1
+#define NFFS_DETECT_FAIL_FORMAT     2
+
 /** On-disk representation of an area header. */
 struct nffs_disk_area {
     uint32_t nda_magic[4];  /* NFFS_AREA_MAGIC{0,1,2,3} */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/nffs_restore.c
----------------------------------------------------------------------
diff --git a/fs/nffs/src/nffs_restore.c b/fs/nffs/src/nffs_restore.c
index fd425fc..b0ed578 100644
--- a/fs/nffs/src/nffs_restore.c
+++ b/fs/nffs/src/nffs_restore.c
@@ -1198,7 +1198,7 @@ nffs_restore_corrupt_scratch(void)
 static void
 nffs_log_contents(void)
 {
-#if LOG_LEVEL > LOG_LEVEL_DEBUG
+#if MYNEWT_VAL(LOG_LEVEL) > LOG_LEVEL_DEBUG
     return;
 #endif
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/fs/nffs/src/test/arch/cortex_m4/nffs_test.c
----------------------------------------------------------------------
diff --git a/fs/nffs/src/test/arch/cortex_m4/nffs_test.c b/fs/nffs/src/test/arch/cortex_m4/nffs_test.c
deleted file mode 100644
index 654089b..0000000
--- a/fs/nffs/src/test/arch/cortex_m4/nffs_test.c
+++ /dev/null
@@ -1,27 +0,0 @@
-/**
- * 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 "nffs/nffs_test.h"
-
-int
-nffs_test_all(void)
-{
-    return 0;
-}


[04/41] incubator-mynewt-core git commit: syscfg / sysinit

Posted by cc...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d98ddc1c/net/nimble/host/test/src/ble_sm_test_util.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/test/src/ble_sm_test_util.c b/net/nimble/host/test/src/ble_sm_test_util.c
new file mode 100644
index 0000000..51dde82
--- /dev/null
+++ b/net/nimble/host/test/src/ble_sm_test_util.c
@@ -0,0 +1,2410 @@
+/**
+ * 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 <stddef.h>
+#include <string.h>
+#include <errno.h>
+#include "testutil/testutil.h"
+#include "nimble/hci_common.h"
+#include "nimble/nimble_opt.h"
+#include "host/ble_sm.h"
+#include "host/ble_hs_test.h"
+#include "host/ble_hs_id.h"
+#include "ble_hs_test_util.h"
+#include "ble_sm_test_util.h"
+
+int ble_sm_test_gap_event_type;
+int ble_sm_test_gap_status;
+struct ble_gap_sec_state ble_sm_test_sec_state;
+
+int ble_sm_test_store_obj_type;
+union ble_store_key ble_sm_test_store_key;
+union ble_store_value ble_sm_test_store_value;
+
+static ble_store_read_fn ble_sm_test_util_store_read;
+static ble_store_write_fn ble_sm_test_util_store_write;
+
+struct ble_sm_test_util_entity {
+    uint8_t addr_type;
+    uint8_t id_addr_type;
+    uint8_t *id_addr;
+    uint8_t *rpa;
+
+    struct ble_sm_pair_cmd *pair_cmd;
+    struct ble_sm_pair_confirm *confirms;
+    struct ble_sm_pair_random *randoms;
+    struct ble_sm_id_info *id_info;
+    struct ble_sm_id_addr_info *id_addr_info;
+    struct ble_sm_sign_info *sign_info;
+    uint8_t *ltk;
+
+    uint8_t key_dist;
+
+    /*** Secure connections fields. */
+    struct ble_sm_public_key *public_key;
+    struct ble_sm_dhkey_check *dhkey_check;
+
+    /*** Legacy fields. */
+    struct ble_sm_enc_info *enc_info;
+    struct ble_sm_master_id *master_id;
+    uint64_t rand_num;
+    uint16_t ediv;
+};
+
+#define BLE_SM_TEST_UTIL_HCI_HDR(handle, pb, len) \
+    ((struct hci_data_hdr) {                            \
+        .hdh_handle_pb_bc = ((handle)  << 0) |          \
+                            ((pb)      << 12),          \
+        .hdh_len = (len)                                \
+    })
+
+static int
+ble_sm_test_util_store_read(int obj_type, union ble_store_key *key,
+                                  union ble_store_value *val)
+{
+    ble_sm_test_store_obj_type = obj_type;
+    ble_sm_test_store_key = *key;
+
+    return ble_hs_test_util_store_read(obj_type, key, val);
+}
+
+static int
+ble_sm_test_util_store_write(int obj_type, union ble_store_value *val)
+{
+    ble_sm_test_store_obj_type = obj_type;
+    ble_sm_test_store_value = *val;
+
+    return ble_hs_test_util_store_write(obj_type, val);
+}
+
+void
+ble_sm_test_util_init(void)
+{
+    ble_hs_test_util_init();
+    ble_hs_test_util_store_init(10, 10, 10);
+    ble_hs_cfg.store_read_cb = ble_sm_test_util_store_read;
+    ble_hs_cfg.store_write_cb = ble_sm_test_util_store_write;
+
+    ble_sm_test_store_obj_type = -1;
+    ble_sm_test_gap_event_type = -1;
+    ble_sm_test_gap_status = -1;
+
+    memset(&ble_sm_test_sec_state, 0xff, sizeof ble_sm_test_sec_state);
+}
+
+static void
+ble_sm_test_util_params_to_entity(struct ble_sm_test_params *params,
+                                  int initiator,
+                                  struct ble_sm_test_util_entity *out_entity)
+{
+    int sc;
+
+    memset(out_entity, 0, sizeof *out_entity);
+
+    sc = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_SC &&
+         params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_SC;
+
+    if (initiator) {
+        out_entity->key_dist = params->pair_rsp.init_key_dist;
+
+        out_entity->addr_type = params->init_addr_type;
+        out_entity->id_addr = params->init_id_addr;
+        out_entity->rpa = params->init_rpa;
+
+        out_entity->pair_cmd = &params->pair_req;
+        out_entity->confirms = params->confirm_req;
+        out_entity->randoms = params->random_req;
+        out_entity->id_info = &params->id_info_rsp;
+        out_entity->id_addr_info = &params->id_addr_info_rsp;
+        out_entity->sign_info = &params->sign_info_rsp;
+
+        if (sc) {
+            out_entity->ltk = params->ltk;
+            out_entity->public_key = &params->public_key_req;
+            out_entity->dhkey_check = &params->dhkey_check_req;
+        } else {
+            out_entity->enc_info = &params->enc_info_rsp;
+            out_entity->master_id = &params->master_id_rsp;
+            if (out_entity->key_dist & BLE_SM_PAIR_KEY_DIST_ENC) {
+                out_entity->rand_num = params->master_id_rsp.rand_val;
+                out_entity->ediv = params->master_id_rsp.ediv;
+                out_entity->ltk = params->enc_info_rsp.ltk;
+            }
+        }
+    } else {
+        out_entity->key_dist = params->pair_rsp.resp_key_dist;
+
+        out_entity->addr_type = params->resp_addr_type;
+        out_entity->id_addr = params->resp_id_addr;
+        out_entity->rpa = params->resp_rpa;
+
+        out_entity->pair_cmd = &params->pair_rsp;
+        out_entity->confirms = params->confirm_rsp;
+        out_entity->randoms = params->random_rsp;
+        out_entity->id_info = &params->id_info_req;
+        out_entity->id_addr_info = &params->id_addr_info_req;
+        out_entity->sign_info = &params->sign_info_req;
+
+        if (sc) {
+            out_entity->ltk = params->ltk;
+            out_entity->public_key = &params->public_key_rsp;
+            out_entity->dhkey_check = &params->dhkey_check_rsp;
+        } else {
+            out_entity->enc_info = &params->enc_info_req;
+            out_entity->master_id = &params->master_id_req;
+            if (out_entity->key_dist & BLE_SM_PAIR_KEY_DIST_ENC) {
+                out_entity->rand_num = params->master_id_req.rand_val;
+                out_entity->ediv = params->master_id_req.ediv;
+                out_entity->ltk = params->enc_info_req.ltk;
+            }
+        }
+    }
+
+    out_entity->id_addr_type =
+        ble_hs_misc_addr_type_to_id(out_entity->addr_type);
+}
+
+static void
+ble_sm_test_util_params_to_entities(struct ble_sm_test_params *params,
+                                    int we_are_initiator,
+                                    struct ble_sm_test_util_entity *out_us,
+                                    struct ble_sm_test_util_entity *out_peer)
+{
+    ble_sm_test_util_params_to_entity(params, we_are_initiator, out_us);
+    ble_sm_test_util_params_to_entity(params, !we_are_initiator, out_peer);
+}
+
+static void
+ble_sm_test_util_init_good(struct ble_sm_test_params *params,
+                           int we_are_initiator,
+                           struct ble_hs_conn **out_conn,
+                           struct ble_sm_test_util_entity *out_us,
+                           struct ble_sm_test_util_entity *out_peer)
+{
+    struct ble_hs_conn *conn;
+
+    ble_sm_test_util_init();
+
+    ble_sm_test_util_params_to_entities(params, we_are_initiator,
+                                        out_us, out_peer);
+
+    ble_hs_cfg.sm_io_cap = out_us->pair_cmd->io_cap;
+    ble_hs_cfg.sm_oob_data_flag = out_us->pair_cmd->oob_data_flag;
+    ble_hs_cfg.sm_bonding = !!(out_us->pair_cmd->authreq &
+                               BLE_SM_PAIR_AUTHREQ_BOND);
+    ble_hs_cfg.sm_mitm = !!(out_us->pair_cmd->authreq &
+                            BLE_SM_PAIR_AUTHREQ_MITM);
+    ble_hs_cfg.sm_sc = !!(out_us->pair_cmd->authreq &
+                          BLE_SM_PAIR_AUTHREQ_SC);
+    ble_hs_cfg.sm_keypress = !!(out_us->pair_cmd->authreq &
+                                BLE_SM_PAIR_AUTHREQ_KEYPRESS);
+
+    if (we_are_initiator) {
+        ble_hs_cfg.sm_our_key_dist = out_us->pair_cmd->init_key_dist;
+        ble_hs_cfg.sm_their_key_dist = out_us->pair_cmd->resp_key_dist;
+    } else {
+        ble_hs_cfg.sm_our_key_dist = out_us->pair_cmd->resp_key_dist;
+        ble_hs_cfg.sm_their_key_dist = out_us->pair_cmd->init_key_dist;
+    }
+
+    ble_hs_id_set_pub(out_us->id_addr);
+    ble_sm_dbg_set_next_pair_rand(out_us->randoms[0].value);
+    ble_sm_dbg_set_next_ediv(out_us->ediv);
+    ble_sm_dbg_set_next_master_id_rand(out_us->rand_num);
+    ble_sm_dbg_set_next_ltk(out_us->ltk);
+    ble_hs_test_util_set_our_irk(out_us->id_info->irk, 0, 0);
+    ble_sm_dbg_set_next_csrk(out_us->sign_info->sig_key);
+
+    if (out_us->public_key != NULL) {
+        ble_sm_dbg_set_sc_keys(out_us->public_key->x, params->our_priv_key);
+    }
+
+    ble_hs_test_util_create_rpa_conn(2, out_us->addr_type, out_us->rpa,
+                                     out_peer->addr_type,
+                                     out_peer->id_addr, out_peer->rpa,
+                                     ble_sm_test_util_conn_cb,
+                                     NULL);
+
+    /* This test code and modifies the connection object after unlocking
+     * the host mutex.  It is not OK for real code to do this, but this test
+     * can assume the connection list is unchanging.
+     */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(2);
+    TEST_ASSERT_FATAL(conn != NULL);
+    ble_hs_unlock();
+
+    if (!we_are_initiator) {
+        /* Peer is the initiator so we must be the slave. */
+        conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
+    }
+
+    if (out_conn != NULL) {
+        *out_conn = conn;
+    }
+}
+
+struct ble_gap_passkey_params ble_sm_test_ioact;
+
+int
+ble_sm_test_util_conn_cb(struct ble_gap_event *event, void *arg)
+{
+    struct ble_gap_conn_desc desc;
+    int rc;
+
+    switch (event->type) {
+    case BLE_GAP_EVENT_ENC_CHANGE:
+        ble_sm_test_gap_status = event->enc_change.status;
+
+        rc = ble_gap_conn_find(event->enc_change.conn_handle, &desc);
+        TEST_ASSERT_FATAL(rc == 0);
+        ble_sm_test_sec_state = desc.sec_state;
+        rc = 0;
+        break;
+
+    case BLE_GAP_EVENT_PASSKEY_ACTION:
+        ble_sm_test_ioact = event->passkey.params;
+        break;
+
+    default:
+        return 0;
+    }
+
+    ble_sm_test_gap_event_type = event->type;
+
+    return rc;
+}
+
+static void
+ble_sm_test_util_rx_pair_cmd(uint16_t conn_handle, uint8_t op,
+                                   struct ble_sm_pair_cmd *cmd,
+                                   int rx_status)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int payload_len;
+    int rc;
+
+    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    payload_len = BLE_SM_HDR_SZ + BLE_SM_PAIR_CMD_SZ;
+
+    v = os_mbuf_extend(om, payload_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    ble_sm_pair_cmd_write(v, payload_len, op == BLE_SM_OP_PAIR_REQ,
+                                cmd);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
+                                              &hci_hdr, om);
+    TEST_ASSERT(rc == rx_status);
+}
+
+static void
+ble_sm_test_util_rx_pair_req(uint16_t conn_handle,
+                                   struct ble_sm_pair_cmd *req,
+                                   int rx_status)
+{
+    ble_sm_test_util_rx_pair_cmd(conn_handle, BLE_SM_OP_PAIR_REQ,
+                                       req, rx_status);
+}
+
+static void
+ble_sm_test_util_rx_pair_rsp(uint16_t conn_handle, struct ble_sm_pair_cmd *rsp,
+                             int rx_status)
+{
+    ble_sm_test_util_rx_pair_cmd(conn_handle, BLE_SM_OP_PAIR_RSP,
+                                       rsp, rx_status);
+}
+
+static void
+ble_sm_test_util_rx_confirm(uint16_t conn_handle,
+                            struct ble_sm_pair_confirm *cmd)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int payload_len;
+    int rc;
+
+    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PAIR_CONFIRM_SZ);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    payload_len = BLE_SM_HDR_SZ + BLE_SM_PAIR_CONFIRM_SZ;
+
+    v = os_mbuf_extend(om, payload_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    ble_sm_pair_confirm_write(v, payload_len, cmd);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
+                                              &hci_hdr, om);
+    TEST_ASSERT_FATAL(rc == 0);
+}
+
+static void
+ble_sm_test_util_rx_random(uint16_t conn_handle,
+                           struct ble_sm_pair_random *cmd,
+                           int exp_status)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int payload_len;
+    int rc;
+
+    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PAIR_RANDOM_SZ);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    payload_len = BLE_SM_HDR_SZ + BLE_SM_PAIR_RANDOM_SZ;
+
+    v = os_mbuf_extend(om, payload_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    ble_sm_pair_random_write(v, payload_len, cmd);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
+                                              &hci_hdr, om);
+    TEST_ASSERT_FATAL(rc == exp_status);
+}
+
+void
+ble_sm_test_util_rx_sec_req(uint16_t conn_handle, struct ble_sm_sec_req *cmd,
+                            int exp_status)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int payload_len;
+    int rc;
+
+    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_SEC_REQ_SZ);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    payload_len = BLE_SM_HDR_SZ + BLE_SM_SEC_REQ_SZ;
+
+    v = os_mbuf_extend(om, payload_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    ble_sm_sec_req_write(v, payload_len, cmd);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
+                                              &hci_hdr, om);
+    TEST_ASSERT_FATAL(rc == exp_status);
+}
+
+static void
+ble_sm_test_util_rx_public_key(uint16_t conn_handle,
+                               struct ble_sm_public_key *cmd)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int payload_len;
+    int rc;
+
+    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_PUBLIC_KEY_SZ);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    payload_len = BLE_SM_HDR_SZ + BLE_SM_PUBLIC_KEY_SZ;
+
+    v = os_mbuf_extend(om, payload_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    ble_sm_public_key_write(v, payload_len, cmd);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
+                                              &hci_hdr, om);
+    TEST_ASSERT_FATAL(rc == 0);
+}
+
+static void
+ble_sm_test_util_rx_dhkey_check(uint16_t conn_handle,
+                                struct ble_sm_dhkey_check *cmd,
+                                int exp_status)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int payload_len;
+    int rc;
+
+    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_DHKEY_CHECK_SZ);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    payload_len = BLE_SM_HDR_SZ + BLE_SM_DHKEY_CHECK_SZ;
+
+    v = os_mbuf_extend(om, payload_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    ble_sm_dhkey_check_write(v, payload_len, cmd);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
+                                              &hci_hdr, om);
+    TEST_ASSERT_FATAL(rc == exp_status);
+}
+
+static void
+ble_sm_test_util_rx_enc_info(uint16_t conn_handle,
+                             struct ble_sm_enc_info *cmd,
+                             int exp_status)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int payload_len;
+    int rc;
+
+    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_ENC_INFO_SZ);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    payload_len = BLE_SM_HDR_SZ + BLE_SM_ENC_INFO_SZ;
+
+    v = os_mbuf_extend(om, payload_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    ble_sm_enc_info_write(v, payload_len, cmd);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
+                                              &hci_hdr, om);
+    TEST_ASSERT_FATAL(rc == exp_status);
+}
+
+static void
+ble_sm_test_util_rx_master_id(uint16_t conn_handle,
+                             struct ble_sm_master_id *cmd,
+                             int exp_status)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int payload_len;
+    int rc;
+
+    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_MASTER_ID_SZ);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    payload_len = BLE_SM_HDR_SZ + BLE_SM_MASTER_ID_SZ;
+
+    v = os_mbuf_extend(om, payload_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    ble_sm_master_id_write(v, payload_len, cmd);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
+                                              &hci_hdr, om);
+    TEST_ASSERT_FATAL(rc == exp_status);
+}
+
+static void
+ble_sm_test_util_rx_id_info(uint16_t conn_handle,
+                            struct ble_sm_id_info *cmd,
+                            int exp_status)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int payload_len;
+    int rc;
+
+    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_ID_INFO_SZ);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    payload_len = BLE_SM_HDR_SZ + BLE_SM_ID_INFO_SZ;
+
+    v = os_mbuf_extend(om, payload_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    ble_sm_id_info_write(v, payload_len, cmd);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
+                                              &hci_hdr, om);
+    TEST_ASSERT_FATAL(rc == exp_status);
+}
+
+static void
+ble_sm_test_util_rx_id_addr_info(uint16_t conn_handle,
+                                 struct ble_sm_id_addr_info *cmd,
+                                 int exp_status)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int payload_len;
+    int rc;
+
+    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_ID_ADDR_INFO_SZ);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    payload_len = BLE_SM_HDR_SZ + BLE_SM_ID_ADDR_INFO_SZ;
+
+    v = os_mbuf_extend(om, payload_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    ble_sm_id_addr_info_write(v, payload_len, cmd);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
+                                              &hci_hdr, om);
+    TEST_ASSERT_FATAL(rc == exp_status);
+}
+
+static void
+ble_sm_test_util_rx_sign_info(uint16_t conn_handle,
+                              struct ble_sm_sign_info *cmd,
+                              int exp_status)
+{
+    struct hci_data_hdr hci_hdr;
+    struct os_mbuf *om;
+    void *v;
+    int payload_len;
+    int rc;
+
+    hci_hdr = BLE_SM_TEST_UTIL_HCI_HDR(
+        2, BLE_HCI_PB_FIRST_FLUSH,
+        BLE_L2CAP_HDR_SZ + BLE_SM_HDR_SZ + BLE_SM_SIGN_INFO_SZ);
+
+    om = ble_hs_mbuf_l2cap_pkt();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    payload_len = BLE_SM_HDR_SZ + BLE_SM_SIGN_INFO_SZ;
+
+    v = os_mbuf_extend(om, payload_len);
+    TEST_ASSERT_FATAL(v != NULL);
+
+    ble_sm_sign_info_write(v, payload_len, cmd);
+
+    rc = ble_hs_test_util_l2cap_rx_first_frag(conn_handle, BLE_L2CAP_CID_SM,
+                                              &hci_hdr, om);
+    TEST_ASSERT_FATAL(rc == exp_status);
+}
+
+static struct os_mbuf *
+ble_sm_test_util_verify_tx_hdr(uint8_t sm_op, uint16_t payload_len)
+{
+    struct os_mbuf *om;
+
+    om = ble_hs_test_util_prev_tx_dequeue_pullup();
+    TEST_ASSERT_FATAL(om != NULL);
+
+    TEST_ASSERT(OS_MBUF_PKTLEN(om) == BLE_SM_HDR_SZ + payload_len);
+    TEST_ASSERT_FATAL(om->om_data[0] == sm_op);
+
+    om->om_data += BLE_SM_HDR_SZ;
+    om->om_len -= BLE_SM_HDR_SZ;
+
+    return om;
+}
+
+static void
+ble_sm_test_util_verify_tx_pair_cmd(
+    uint8_t op,
+    struct ble_sm_pair_cmd *exp_cmd)
+{
+    struct ble_sm_pair_cmd cmd;
+    struct os_mbuf *om;
+
+    om = ble_sm_test_util_verify_tx_hdr(op, BLE_SM_PAIR_CMD_SZ);
+    ble_sm_pair_cmd_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(cmd.io_cap == exp_cmd->io_cap);
+    TEST_ASSERT(cmd.oob_data_flag == exp_cmd->oob_data_flag);
+    TEST_ASSERT(cmd.authreq == exp_cmd->authreq);
+    TEST_ASSERT(cmd.max_enc_key_size == exp_cmd->max_enc_key_size);
+    TEST_ASSERT(cmd.init_key_dist == exp_cmd->init_key_dist);
+    TEST_ASSERT(cmd.resp_key_dist == exp_cmd->resp_key_dist);
+}
+
+static void
+ble_sm_test_util_verify_tx_pair_req(
+    struct ble_sm_pair_cmd *exp_req)
+{
+    ble_sm_test_util_verify_tx_pair_cmd(BLE_SM_OP_PAIR_REQ,
+                                              exp_req);
+}
+
+static void
+ble_sm_test_util_verify_tx_pair_rsp(
+    struct ble_sm_pair_cmd *exp_rsp)
+{
+    ble_sm_test_util_verify_tx_pair_cmd(BLE_SM_OP_PAIR_RSP,
+                                              exp_rsp);
+}
+
+static void
+ble_sm_test_util_verify_tx_pair_confirm(
+    struct ble_sm_pair_confirm *exp_cmd)
+{
+    struct ble_sm_pair_confirm cmd;
+    struct os_mbuf *om;
+
+    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_CONFIRM,
+                                        BLE_SM_PAIR_CONFIRM_SZ);
+    ble_sm_pair_confirm_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(memcmp(cmd.value, exp_cmd->value, 16) == 0);
+}
+
+static void
+ble_sm_test_util_verify_tx_pair_random(
+    struct ble_sm_pair_random *exp_cmd)
+{
+    struct ble_sm_pair_random cmd;
+    struct os_mbuf *om;
+
+    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_RANDOM,
+                                        BLE_SM_PAIR_RANDOM_SZ);
+    ble_sm_pair_random_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(memcmp(cmd.value, exp_cmd->value, 16) == 0);
+}
+
+static void
+ble_sm_test_util_verify_tx_public_key(
+    struct ble_sm_public_key *exp_cmd)
+{
+    struct ble_sm_public_key cmd;
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_PUBLIC_KEY,
+                                              BLE_SM_PUBLIC_KEY_SZ);
+    ble_sm_public_key_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(memcmp(cmd.x, exp_cmd->x, sizeof cmd.x) == 0);
+    TEST_ASSERT(memcmp(cmd.y, exp_cmd->y, sizeof cmd.y) == 0);
+}
+
+static void
+ble_sm_test_util_verify_tx_dhkey_check(
+    struct ble_sm_dhkey_check *exp_cmd)
+{
+    struct ble_sm_dhkey_check cmd;
+    struct os_mbuf *om;
+
+    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_DHKEY_CHECK,
+                                              BLE_SM_DHKEY_CHECK_SZ);
+    ble_sm_dhkey_check_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(memcmp(cmd.value, exp_cmd->value, 16) == 0);
+}
+
+static void
+ble_sm_test_util_verify_tx_enc_info(struct ble_sm_enc_info *exp_cmd)
+{
+    struct ble_sm_enc_info cmd;
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_ENC_INFO,
+                                        BLE_SM_ENC_INFO_SZ);
+    ble_sm_enc_info_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(memcmp(cmd.ltk, exp_cmd->ltk, 16) == 0);
+
+    /* Ensure LTK is sent in little endian. */
+    TEST_ASSERT(memcmp(om->om_data, cmd.ltk, 16) == 0);
+}
+
+static void
+ble_sm_test_util_verify_tx_master_id(struct ble_sm_master_id *exp_cmd)
+{
+    struct ble_sm_master_id cmd;
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_MASTER_ID,
+                                        BLE_SM_MASTER_ID_SZ);
+    ble_sm_master_id_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(cmd.ediv == exp_cmd->ediv);
+    TEST_ASSERT(cmd.rand_val == exp_cmd->rand_val);
+}
+
+static void
+ble_sm_test_util_verify_tx_id_info(struct ble_sm_id_info *exp_cmd)
+{
+    struct ble_sm_id_info cmd;
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_IDENTITY_INFO,
+                                        BLE_SM_ID_INFO_SZ);
+    ble_sm_id_info_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(memcmp(cmd.irk, exp_cmd->irk, 16) == 0);
+
+    /* Ensure IRK is sent in little endian. */
+    TEST_ASSERT(memcmp(om->om_data, cmd.irk, 16) == 0);
+}
+
+static void
+ble_sm_test_util_verify_tx_id_addr_info(struct ble_sm_id_addr_info *exp_cmd)
+{
+    struct ble_sm_id_addr_info cmd;
+    struct os_mbuf *om;
+    const uint8_t *our_id_addr;
+    int rc;
+
+    ble_hs_lock();
+    rc = ble_hs_id_addr(exp_cmd->addr_type, &our_id_addr, NULL);
+    ble_hs_unlock();
+
+    TEST_ASSERT_FATAL(rc == 0);
+
+    ble_hs_test_util_tx_all();
+    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_IDENTITY_ADDR_INFO,
+                                        BLE_SM_ID_ADDR_INFO_SZ);
+    ble_sm_id_addr_info_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(cmd.addr_type == exp_cmd->addr_type);
+    TEST_ASSERT(memcmp(cmd.bd_addr, exp_cmd->bd_addr, 6) == 0);
+    TEST_ASSERT(memcmp(cmd.bd_addr, our_id_addr, 6) == 0);
+}
+
+static void
+ble_sm_test_util_verify_tx_sign_info(struct ble_sm_sign_info *exp_cmd)
+{
+    struct ble_sm_sign_info cmd;
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_SIGN_INFO,
+                                        BLE_SM_ID_INFO_SZ);
+    ble_sm_sign_info_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(memcmp(cmd.sig_key, exp_cmd->sig_key, 16) == 0);
+
+    /* Ensure CSRK is sent in little endian. */
+    TEST_ASSERT(memcmp(om->om_data, cmd.sig_key, 16) == 0);
+}
+
+static void
+ble_sm_test_util_verify_tx_sec_req(struct ble_sm_sec_req *exp_cmd)
+{
+    struct ble_sm_sec_req cmd;
+    struct os_mbuf *om;
+
+    ble_hs_test_util_tx_all();
+
+    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_SEC_REQ, BLE_SM_SEC_REQ_SZ);
+    ble_sm_sec_req_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(cmd.authreq == exp_cmd->authreq);
+}
+
+void
+ble_sm_test_util_verify_tx_pair_fail(
+    struct ble_sm_pair_fail *exp_cmd)
+{
+    struct ble_sm_pair_fail cmd;
+    struct os_mbuf *om;
+
+    om = ble_sm_test_util_verify_tx_hdr(BLE_SM_OP_PAIR_FAIL,
+                                        BLE_SM_PAIR_FAIL_SZ);
+    ble_sm_pair_fail_parse(om->om_data, om->om_len, &cmd);
+
+    TEST_ASSERT(cmd.reason == exp_cmd->reason);
+}
+
+static void
+ble_sm_test_util_rx_lt_key_req(uint16_t conn_handle, uint64_t r, uint16_t ediv)
+{
+    struct hci_le_lt_key_req evt;
+    int rc;
+
+    evt.subevent_code = BLE_HCI_LE_SUBEV_LT_KEY_REQ;
+    evt.connection_handle = conn_handle;
+    evt.random_number = r;
+    evt.encrypted_diversifier = ediv;
+
+    rc = ble_sm_ltk_req_rx(&evt);
+    TEST_ASSERT_FATAL(rc == 0);
+}
+
+static void
+ble_sm_test_util_verify_tx_lt_key_req_reply(uint16_t conn_handle, uint8_t *stk)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_LT_KEY_REQ_REPLY,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_LT_KEY_REQ_REPLY_LEN);
+    TEST_ASSERT(le16toh(param + 0) == conn_handle);
+    TEST_ASSERT(memcmp(param + 2, stk, 16) == 0);
+}
+
+static void
+ble_sm_test_util_verify_tx_lt_key_req_neg_reply(uint16_t conn_handle)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_LT_KEY_REQ_NEG_REPLY_LEN);
+    TEST_ASSERT(le16toh(param + 0) == conn_handle);
+}
+
+static void
+ble_sm_test_util_set_lt_key_req_neg_reply_ack(uint8_t status,
+                                              uint16_t conn_handle)
+{
+    static uint8_t params[BLE_HCI_LT_KEY_REQ_NEG_REPLY_ACK_PARAM_LEN];
+
+    htole16(params, conn_handle);
+    ble_hs_test_util_set_ack_params(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_LT_KEY_REQ_NEG_REPLY),
+        status, params, sizeof params);
+}
+
+static void
+ble_sm_test_util_set_lt_key_req_reply_ack(uint8_t status, uint16_t conn_handle)
+{
+    static uint8_t params[BLE_HCI_LT_KEY_REQ_REPLY_ACK_PARAM_LEN];
+
+    htole16(params, conn_handle);
+    ble_hs_test_util_set_ack_params(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_LT_KEY_REQ_REPLY),
+        status, params, sizeof params);
+}
+
+static void
+ble_sm_test_util_rx_enc_change(uint16_t conn_handle, uint8_t status,
+                                     uint8_t encryption_enabled)
+{
+    struct hci_encrypt_change evt;
+
+    evt.status = status;
+    evt.encryption_enabled = encryption_enabled;
+    evt.connection_handle = conn_handle;
+
+    ble_sm_enc_change_rx(&evt);
+}
+
+static void
+ble_sm_test_util_verify_tx_start_enc(uint16_t conn_handle,
+                                     uint64_t random_number,
+                                     uint16_t ediv,
+                                     uint8_t *ltk)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_START_ENCRYPT,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_LE_START_ENCRYPT_LEN);
+    TEST_ASSERT(le16toh(param + 0) == conn_handle);
+    TEST_ASSERT(le64toh(param + 2) == random_number);
+    TEST_ASSERT(le16toh(param + 10) == ediv);
+    TEST_ASSERT(memcmp(param + 12, ltk, 16) == 0);
+}
+
+static void
+ble_sm_test_util_verify_tx_add_resolve_list(uint8_t peer_id_addr_type,
+                                            uint8_t *peer_id_addr,
+                                            uint8_t *peer_irk,
+                                            uint8_t *our_irk)
+{
+    uint8_t param_len;
+    uint8_t *param;
+
+    param = ble_hs_test_util_verify_tx_hci(BLE_HCI_OGF_LE,
+                                           BLE_HCI_OCF_LE_ADD_RESOLV_LIST,
+                                           &param_len);
+    TEST_ASSERT(param_len == BLE_HCI_ADD_TO_RESOLV_LIST_LEN);
+    TEST_ASSERT(param[0] == peer_id_addr_type);
+    TEST_ASSERT(memcmp(param + 1, peer_id_addr, 6) == 0);
+
+    /* Ensure IRKs are sent in little endian. */
+    TEST_ASSERT(memcmp(param + 7, peer_irk, 16) == 0);
+    TEST_ASSERT(memcmp(param + 23, our_irk, 16) == 0);
+}
+
+void
+ble_sm_test_util_io_inject(struct ble_sm_test_passkey_info *passkey_info,
+                           uint8_t cur_sm_state)
+{
+    uint8_t io_sm_state;
+    int rc;
+
+    io_sm_state = ble_sm_ioact_state(passkey_info->passkey.action);
+    if (io_sm_state != cur_sm_state) {
+        return;
+    }
+
+    if (passkey_info->passkey.action == BLE_SM_IOACT_NUMCMP) {
+        TEST_ASSERT(ble_sm_test_ioact.numcmp == passkey_info->exp_numcmp);
+    }
+
+    rc = ble_sm_inject_io(2, &passkey_info->passkey);
+    TEST_ASSERT(rc == 0);
+}
+
+void
+ble_sm_test_util_io_inject_bad(uint16_t conn_handle, uint8_t correct_io_act)
+{
+    struct ble_sm_proc *proc;
+    struct ble_sm_io io;
+    uint8_t io_sm_state;
+    int already_injected;
+    int rc;
+    int i;
+
+    /* Lock mutex to prevent thread-safety assert from failing. */
+    ble_hs_lock();
+    proc = ble_sm_proc_find(conn_handle, BLE_SM_PROC_STATE_NONE, -1, NULL);
+    ble_hs_unlock();
+
+    TEST_ASSERT_FATAL(proc != NULL);
+
+    io_sm_state = ble_sm_ioact_state(correct_io_act);
+
+    for (i = 1; i < BLE_SM_IOACT_MAX_PLUS_ONE; i++) {
+        if (io_sm_state != proc->state  ||
+            i != correct_io_act         ||
+            proc->flags & BLE_SM_PROC_F_IO_INJECTED) {
+
+            already_injected = proc->flags & BLE_SM_PROC_F_IO_INJECTED;
+
+            io.action = i;
+            rc = ble_sm_inject_io(conn_handle, &io);
+
+            if (already_injected) {
+                TEST_ASSERT(rc == BLE_HS_EALREADY);
+            } else {
+                TEST_ASSERT(rc == BLE_HS_EINVAL);
+            }
+        }
+    }
+}
+
+void
+ble_sm_test_util_io_check_pre(struct ble_sm_test_passkey_info *passkey_info,
+                              uint8_t cur_sm_state)
+{
+    uint8_t io_sm_state;
+    int rc;
+
+    io_sm_state = ble_sm_ioact_state(passkey_info->passkey.action);
+    if (io_sm_state != cur_sm_state) {
+        return;
+    }
+
+    if (!passkey_info->io_before_rx) {
+        return;
+    }
+
+    if (passkey_info->passkey.action == BLE_SM_IOACT_NUMCMP) {
+        TEST_ASSERT(ble_sm_test_ioact.numcmp == passkey_info->exp_numcmp);
+    }
+
+    rc = ble_sm_inject_io(2, &passkey_info->passkey);
+    TEST_ASSERT(rc == 0);
+}
+
+void
+ble_sm_test_util_io_check_post(struct ble_sm_test_passkey_info *passkey_info,
+                               uint8_t cur_sm_state)
+{
+    uint8_t io_sm_state;
+    int rc;
+
+    io_sm_state = ble_sm_ioact_state(passkey_info->passkey.action);
+    if (io_sm_state != cur_sm_state) {
+        return;
+    }
+
+    if (passkey_info->io_before_rx) {
+        return;
+    }
+
+    if (passkey_info->passkey.action == BLE_SM_IOACT_NUMCMP) {
+        TEST_ASSERT(ble_sm_test_ioact.numcmp == passkey_info->exp_numcmp);
+    }
+
+    /* Ensure response not sent until user performs IO. */
+    ble_hs_test_util_tx_all();
+    TEST_ASSERT(ble_hs_test_util_prev_tx_queue_sz() == 0);
+
+    rc = ble_sm_inject_io(2, &passkey_info->passkey);
+    TEST_ASSERT_FATAL(rc == 0);
+}
+
+static void
+ble_sm_test_util_verify_persist(struct ble_sm_test_params *params,
+                                int we_are_initiator)
+{
+    struct ble_sm_test_util_entity peer_entity;
+    struct ble_sm_test_util_entity our_entity;
+    struct ble_store_value_sec value_sec;
+    struct ble_store_key_sec key_sec;
+    int csrk_expected;
+    int ltk_expected;
+    int peer_irk_expected;
+    int our_irk_expected;
+    int bonding;
+    int sc;
+    int rc;
+
+    ble_sm_test_util_params_to_entities(params, we_are_initiator,
+                                        &our_entity, &peer_entity);
+
+    sc = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_SC &&
+         params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_SC;
+
+    bonding = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_BOND &&
+              params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_BOND;
+
+    memset(&key_sec, 0, sizeof key_sec);
+    key_sec.peer_addr_type = BLE_STORE_ADDR_TYPE_NONE;
+
+    rc = ble_store_read_peer_sec(&key_sec, &value_sec);
+    if (!bonding) {
+        TEST_ASSERT(rc == BLE_HS_ENOENT);
+        peer_irk_expected = 0;
+    } else {
+        TEST_ASSERT_FATAL(rc == 0);
+
+        ltk_expected =
+            sc || !!(peer_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ENC);
+        peer_irk_expected =
+            !!(peer_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ID);
+        csrk_expected =
+            !!(peer_entity.key_dist & BLE_SM_PAIR_KEY_DIST_SIGN);
+
+        TEST_ASSERT(value_sec.peer_addr_type == peer_entity.id_addr_type);
+        TEST_ASSERT(memcmp(value_sec.peer_addr, peer_entity.id_addr, 6) == 0);
+        TEST_ASSERT(value_sec.ediv == peer_entity.ediv);
+        TEST_ASSERT(value_sec.rand_num == peer_entity.rand_num);
+        TEST_ASSERT(value_sec.authenticated == params->authenticated);
+
+        TEST_ASSERT(value_sec.ltk_present == ltk_expected);
+        TEST_ASSERT(memcmp(value_sec.ltk, peer_entity.ltk, 16) == 0);
+
+        TEST_ASSERT(value_sec.irk_present == peer_irk_expected);
+        if (peer_irk_expected) {
+            TEST_ASSERT(memcmp(value_sec.irk,
+                               peer_entity.id_info->irk, 16) == 0);
+        }
+
+        TEST_ASSERT(value_sec.csrk_present == csrk_expected);
+        if (csrk_expected) {
+            TEST_ASSERT(memcmp(value_sec.csrk,
+                               peer_entity.sign_info->sig_key, 16) == 0);
+        }
+    }
+
+    rc = ble_store_read_our_sec(&key_sec, &value_sec);
+    if (!bonding) {
+        TEST_ASSERT(rc == BLE_HS_ENOENT);
+    } else {
+        TEST_ASSERT_FATAL(rc == 0);
+
+        ltk_expected =
+            sc || !!(our_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ENC);
+        our_irk_expected =
+            !!(our_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ID);
+        csrk_expected =
+            !!(our_entity.key_dist & BLE_SM_PAIR_KEY_DIST_SIGN);
+
+        TEST_ASSERT(value_sec.peer_addr_type == peer_entity.id_addr_type);
+        TEST_ASSERT(memcmp(value_sec.peer_addr, peer_entity.id_addr, 6) == 0);
+        TEST_ASSERT(value_sec.ediv == our_entity.ediv);
+        TEST_ASSERT(value_sec.rand_num == our_entity.rand_num);
+        TEST_ASSERT(value_sec.authenticated == params->authenticated);
+
+        TEST_ASSERT(value_sec.ltk_present == ltk_expected);
+        TEST_ASSERT(memcmp(value_sec.ltk, our_entity.ltk, 16) == 0);
+
+        TEST_ASSERT(value_sec.irk_present == our_irk_expected);
+        if (our_irk_expected) {
+            TEST_ASSERT(memcmp(value_sec.irk,
+                               our_entity.id_info->irk, 16) == 0);
+        }
+
+        TEST_ASSERT(value_sec.csrk_present == csrk_expected);
+        if (csrk_expected) {
+            TEST_ASSERT(memcmp(value_sec.csrk,
+                               our_entity.sign_info->sig_key, 16) == 0);
+        }
+    }
+
+    /* Verify no other keys were persisted. */
+    key_sec.idx++;
+    rc = ble_store_read_our_sec(&key_sec, &value_sec);
+    TEST_ASSERT_FATAL(rc == BLE_HS_ENOENT);
+    rc = ble_store_read_peer_sec(&key_sec, &value_sec);
+    TEST_ASSERT_FATAL(rc == BLE_HS_ENOENT);
+
+    /* Verify we sent the peer's IRK to the controller. */
+    if (peer_irk_expected) {
+        ble_sm_test_util_verify_tx_add_resolve_list(peer_entity.id_addr_type,
+                                                    peer_entity.id_addr,
+                                                    peer_entity.id_info->irk,
+                                                    our_entity.id_info->irk);
+    }
+}
+
+static void
+ble_sm_test_util_peer_bonding_good(int send_enc_req,
+                                   uint8_t our_addr_type,
+                                   uint8_t *our_rpa,
+                                   uint8_t peer_addr_type,
+                                   uint8_t *peer_id_addr,
+                                   uint8_t *peer_rpa,
+                                   uint8_t *ltk, int authenticated,
+                                   uint16_t ediv, uint64_t rand_num)
+{
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_hs_test_util_create_rpa_conn(2, our_addr_type, our_rpa, peer_addr_type,
+                                     peer_id_addr, peer_rpa,
+                                     ble_sm_test_util_conn_cb, NULL);
+
+    /* This test inspects and modifies the connection object after unlocking
+     * the host mutex.  It is not OK for real code to do this, but this test
+     * can assume the connection list is unchanging.
+     */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(2);
+    TEST_ASSERT_FATAL(conn != NULL);
+    conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
+    ble_hs_unlock();
+
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    if (send_enc_req) {
+        rc = ble_sm_slave_initiate(2);
+        TEST_ASSERT(rc == 0);
+    }
+
+    /* Receive a long term key request from the controller. */
+    ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
+    ble_sm_test_util_rx_lt_key_req(2, rand_num, ediv);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+
+    /* Ensure the LTK request event got sent to the application. */
+    TEST_ASSERT(ble_sm_test_store_obj_type ==
+                BLE_STORE_OBJ_TYPE_OUR_SEC);
+    TEST_ASSERT(ble_sm_test_store_key.sec.peer_addr_type ==
+                ble_hs_misc_addr_type_to_id(peer_addr_type));
+    TEST_ASSERT(ble_sm_test_store_key.sec.ediv_rand_present);
+    TEST_ASSERT(ble_sm_test_store_key.sec.ediv == ediv);
+    TEST_ASSERT(ble_sm_test_store_key.sec.rand_num == rand_num);
+
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
+
+    /* Ensure we sent the expected long term key request reply command. */
+    ble_sm_test_util_verify_tx_lt_key_req_reply(2, ltk);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
+
+    /* Receive an encryption changed event. */
+    ble_sm_test_util_rx_enc_change(2, 0, 1);
+
+    /* Pairing should now be complete. */
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Verify that security callback was executed. */
+    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
+    TEST_ASSERT(ble_sm_test_gap_status == 0);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                authenticated);
+
+    /* Verify that connection has correct security state. */
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                authenticated);
+
+    ble_hs_test_util_conn_disconnect(2);
+}
+
+void
+ble_sm_test_util_peer_bonding_bad(uint16_t ediv, uint64_t rand_num)
+{
+    struct ble_hs_conn *conn;
+
+    ble_sm_test_util_init();
+
+    ble_hs_test_util_create_conn(2, ((uint8_t[6]){1,2,3,4,5,6}),
+                                 ble_sm_test_util_conn_cb,
+                                 NULL);
+
+    /* This test inspects and modifies the connection object after unlocking
+     * the host mutex.  It is not OK for real code to do this, but this test
+     * can assume the connection list is unchanging.
+     */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(2);
+    TEST_ASSERT_FATAL(conn != NULL);
+    conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
+    ble_hs_unlock();
+
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Receive a long term key request from the controller. */
+    ble_sm_test_util_set_lt_key_req_neg_reply_ack(0, 2);
+    ble_sm_test_util_rx_lt_key_req(2, rand_num, ediv);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+
+    /* Ensure the LTK request event got sent to the application. */
+    TEST_ASSERT(ble_sm_test_store_obj_type ==
+                BLE_STORE_OBJ_TYPE_OUR_SEC);
+    TEST_ASSERT(ble_sm_test_store_key.sec.ediv_rand_present);
+    TEST_ASSERT(ble_sm_test_store_key.sec.ediv == ediv);
+    TEST_ASSERT(ble_sm_test_store_key.sec.rand_num == rand_num);
+
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+
+    /* Ensure we sent the expected long term key request neg reply command. */
+    ble_sm_test_util_verify_tx_lt_key_req_neg_reply(2);
+
+    /* Ensure the security procedure was aborted. */
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(!conn->bhc_sec_state.authenticated);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+}
+
+/**
+ * @param send_enc_req          Whether this procedure is initiated by a slave
+ *                                  security request;
+ *                                  1: Peer sends a security request at start.
+ *                                  0: No security request; we initiate.
+ */
+static void
+ble_sm_test_util_us_bonding_good(int send_enc_req, uint8_t our_addr_type,
+                                 uint8_t *our_rpa,
+                                 uint8_t peer_addr_type,
+                                 uint8_t *peer_id_addr, uint8_t *peer_rpa,
+                                 uint8_t *ltk, int authenticated,
+                                 uint16_t ediv, uint64_t rand_num)
+{
+    struct ble_sm_sec_req sec_req;
+    struct ble_hs_conn *conn;
+
+    ble_hs_test_util_create_rpa_conn(2, our_addr_type, our_rpa,
+                                     peer_addr_type, peer_id_addr,
+                                     peer_rpa, ble_sm_test_util_conn_cb, NULL);
+
+    /* This test inspects and modifies the connection object after unlocking
+     * the host mutex.  It is not OK for real code to do this, but this test
+     * can assume the connection list is unchanging.
+     */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(2);
+    TEST_ASSERT_FATAL(conn != NULL);
+    ble_hs_unlock();
+
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    ble_hs_test_util_set_ack(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_START_ENCRYPT),
+        0);
+
+    if (send_enc_req) {
+        sec_req.authreq = 0;
+        sec_req.authreq |= BLE_SM_PAIR_AUTHREQ_BOND;
+        if (authenticated) {
+            sec_req.authreq |= BLE_SM_PAIR_AUTHREQ_MITM;
+        }
+        ble_sm_test_util_rx_sec_req(2, &sec_req, 0);
+    } else {
+        ble_gap_security_initiate(2);
+    }
+
+    /* Ensure we sent the expected start encryption command. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_start_enc(2, rand_num, ediv, ltk);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
+
+    /* Receive an encryption changed event. */
+    ble_sm_test_util_rx_enc_change(2, 0, 1);
+
+    /* Pairing should now be complete. */
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Verify that security callback was executed. */
+    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
+    TEST_ASSERT(ble_sm_test_gap_status == 0);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                authenticated);
+
+    /* Verify that connection has correct security state. */
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                authenticated);
+
+    ble_hs_test_util_conn_disconnect(2);
+}
+
+void
+ble_sm_test_util_peer_fail_inval(
+    int we_are_master,
+    uint8_t *init_id_addr,
+    uint8_t *resp_addr,
+    struct ble_sm_pair_cmd *pair_req,
+    struct ble_sm_pair_fail *pair_fail)
+{
+    struct ble_hs_conn *conn;
+
+    ble_sm_test_util_init();
+    ble_hs_id_set_pub(resp_addr);
+
+    ble_hs_test_util_create_conn(2, init_id_addr, ble_sm_test_util_conn_cb,
+                                 NULL);
+
+    /* This test inspects and modifies the connection object after unlocking
+     * the host mutex.  It is not OK for real code to do this, but this test
+     * can assume the connection list is unchanging.
+     */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(2);
+    TEST_ASSERT_FATAL(conn != NULL);
+    ble_hs_unlock();
+
+    if (!we_are_master) {
+        conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
+    }
+
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Receive a pair request from the peer. */
+    ble_sm_test_util_rx_pair_req(2, pair_req,
+                                 BLE_HS_SM_US_ERR(pair_fail->reason));
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Ensure we sent the expected pair fail. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_fail(pair_fail);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Verify that security callback was not executed. */
+    TEST_ASSERT(ble_sm_test_gap_event_type == -1);
+    TEST_ASSERT(ble_sm_test_gap_status == -1);
+
+    /* Verify that connection has correct security state. */
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(!conn->bhc_sec_state.authenticated);
+}
+
+void
+ble_sm_test_util_peer_lgcy_fail_confirm(
+    uint8_t *init_id_addr,
+    uint8_t *resp_addr,
+    struct ble_sm_pair_cmd *pair_req,
+    struct ble_sm_pair_cmd *pair_rsp,
+    struct ble_sm_pair_confirm *confirm_req,
+    struct ble_sm_pair_confirm *confirm_rsp,
+    struct ble_sm_pair_random *random_req,
+    struct ble_sm_pair_random *random_rsp,
+    struct ble_sm_pair_fail *fail_rsp)
+{
+    struct ble_hs_conn *conn;
+
+    ble_sm_test_util_init();
+    ble_hs_id_set_pub(resp_addr);
+    ble_sm_dbg_set_next_pair_rand(random_rsp->value);
+
+    if (pair_rsp->authreq & BLE_SM_PAIR_AUTHREQ_SC) {
+        ble_hs_cfg.sm_sc = 1;
+    } else {
+        ble_hs_cfg.sm_sc = 0;
+    }
+
+    ble_hs_test_util_create_conn(2, init_id_addr, ble_sm_test_util_conn_cb,
+                                 NULL);
+
+    /* This test inspects and modifies the connection object after unlocking
+     * the host mutex.  It is not OK for real code to do this, but this test
+     * can assume the connection list is unchanging.
+     */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(2);
+    TEST_ASSERT_FATAL(conn != NULL);
+    ble_hs_unlock();
+
+    /* Peer is the initiator so we must be the slave. */
+    conn->bhc_flags &= ~BLE_HS_CONN_F_MASTER;
+
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Receive a pair request from the peer. */
+    ble_sm_test_util_rx_pair_req(2, pair_req, 0);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
+
+    /* Ensure we sent the expected pair response. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_rsp(pair_rsp);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
+
+    /* Receive a pair confirm from the peer. */
+    ble_sm_test_util_rx_confirm(2, confirm_req);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
+
+    /* Ensure we sent the expected pair confirm. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_confirm(confirm_rsp);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, BLE_SM_IOACT_NONE);
+
+    /* Receive a pair random from the peer. */
+    ble_sm_test_util_rx_random(
+        2, random_req, BLE_HS_SM_US_ERR(BLE_SM_ERR_CONFIRM_MISMATCH));
+
+    /* Ensure we sent the expected pair fail. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_fail(fail_rsp);
+
+    /* The proc should now be freed. */
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Verify that security callback was executed. */
+    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
+    TEST_ASSERT(ble_sm_test_gap_status ==
+                BLE_HS_SM_US_ERR(BLE_SM_ERR_CONFIRM_MISMATCH));
+    TEST_ASSERT(!ble_sm_test_sec_state.encrypted);
+    TEST_ASSERT(!ble_sm_test_sec_state.authenticated);
+
+    /* Verify that connection has correct security state. */
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
+                conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                conn->bhc_sec_state.authenticated);
+}
+
+static void
+ble_sm_test_util_bonding_all(struct ble_sm_test_params *params,
+                             int we_are_original_initiator)
+{
+    struct ble_sm_test_util_entity peer_entity;
+    struct ble_sm_test_util_entity our_entity;
+    int sc;
+
+    if (!(params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_BOND) ||
+        !(params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_BOND)) {
+
+        /* Bonding not performed. */
+        return;
+    }
+
+    sc = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_SC &&
+         params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_SC;
+
+    ble_sm_test_util_params_to_entities(params, we_are_original_initiator,
+                                        &our_entity, &peer_entity);
+
+    if (sc || peer_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ENC) {
+        /* We are master; we initiate procedure. */
+        ble_sm_test_util_us_bonding_good(0, our_entity.addr_type,
+                                         our_entity.rpa,
+                                         peer_entity.addr_type,
+                                         peer_entity.id_addr,
+                                         peer_entity.rpa,
+                                         peer_entity.ltk,
+                                         params->authenticated,
+                                         peer_entity.ediv,
+                                         peer_entity.rand_num);
+
+        /* We are master; peer initiates procedure via security request. */
+        ble_sm_test_util_us_bonding_good(1, our_entity.addr_type,
+                                         our_entity.rpa,
+                                         peer_entity.addr_type,
+                                         peer_entity.id_addr,
+                                         peer_entity.rpa,
+                                         peer_entity.ltk,
+                                         params->authenticated,
+                                         peer_entity.ediv,
+                                         peer_entity.rand_num);
+    }
+
+    if (sc || our_entity.key_dist & BLE_SM_PAIR_KEY_DIST_ENC) {
+        /* Peer is master; peer initiates procedure. */
+        ble_sm_test_util_peer_bonding_good(0, our_entity.addr_type,
+                                           our_entity.rpa,
+                                           peer_entity.addr_type,
+                                           peer_entity.id_addr,
+                                           peer_entity.rpa,
+                                           our_entity.ltk,
+                                           params->authenticated,
+                                           our_entity.ediv,
+                                           our_entity.rand_num);
+
+        /* Peer is master; we initiate procedure via security request. */
+        ble_sm_test_util_peer_bonding_good(1, our_entity.addr_type,
+                                           our_entity.rpa,
+                                           peer_entity.addr_type,
+                                           peer_entity.id_addr,
+                                           peer_entity.rpa,
+                                           our_entity.ltk,
+                                           params->authenticated,
+                                           our_entity.ediv,
+                                           our_entity.rand_num);
+    }
+}
+
+static void
+ble_sm_test_util_rx_keys(struct ble_sm_test_params *params,
+                         int we_are_initiator)
+{
+    struct ble_sm_id_addr_info *peer_id_addr_info;
+    struct ble_sm_sign_info *peer_sign_info;
+    struct ble_sm_master_id *peer_master_id;
+    struct ble_sm_enc_info *peer_enc_info;
+    struct ble_sm_id_info *peer_id_info;
+    uint8_t peer_key_dist;
+    int sc;
+
+    if (we_are_initiator) {
+        peer_key_dist = params->pair_rsp.resp_key_dist;
+        peer_id_addr_info = &params->id_addr_info_req;
+        peer_sign_info = &params->sign_info_req;
+        peer_master_id = &params->master_id_req;
+        peer_enc_info = &params->enc_info_req;
+        peer_id_info = &params->id_info_req;
+    } else {
+        peer_key_dist = params->pair_rsp.init_key_dist;
+        peer_id_addr_info = &params->id_addr_info_rsp;
+        peer_sign_info = &params->sign_info_rsp;
+        peer_master_id = &params->master_id_rsp;
+        peer_enc_info = &params->enc_info_rsp;
+        peer_id_info = &params->id_info_rsp;
+    }
+
+    sc = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_SC &&
+         params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_SC;
+
+    /* Receive key material from peer. */
+    if (!sc && (peer_key_dist & BLE_SM_PAIR_KEY_DIST_ENC)) {
+        ble_sm_test_util_rx_enc_info(2, peer_enc_info, 0);
+        ble_sm_test_util_rx_master_id(2, peer_master_id, 0);
+    }
+    if (peer_key_dist & BLE_SM_PAIR_KEY_DIST_ID) {
+        ble_hs_test_util_set_ack(
+            ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                        BLE_HCI_OCF_LE_ADD_RESOLV_LIST), 0);
+        ble_sm_test_util_rx_id_info(2, peer_id_info, 0);
+        ble_sm_test_util_rx_id_addr_info(2, peer_id_addr_info, 0);
+    }
+    if (peer_key_dist & BLE_SM_PAIR_KEY_DIST_SIGN) {
+        ble_sm_test_util_rx_sign_info(2, peer_sign_info, 0);
+    }
+}
+
+static void
+ble_sm_test_util_verify_tx_keys(struct ble_sm_test_params *params,
+                                int we_are_initiator)
+{
+    struct ble_sm_id_addr_info *our_id_addr_info;
+    struct ble_sm_sign_info *our_sign_info;
+    struct ble_sm_master_id *our_master_id;
+    struct ble_sm_enc_info *our_enc_info;
+    struct ble_sm_id_info *our_id_info;
+    uint8_t our_key_dist;
+    int sc;
+
+    if (we_are_initiator) {
+        our_key_dist = params->pair_rsp.init_key_dist;
+        our_id_addr_info = &params->id_addr_info_rsp;
+        our_sign_info = &params->sign_info_rsp;
+        our_master_id = &params->master_id_rsp;
+        our_enc_info = &params->enc_info_rsp;
+        our_id_info = &params->id_info_rsp;
+    } else {
+        our_key_dist = params->pair_rsp.resp_key_dist;
+        our_id_addr_info = &params->id_addr_info_req;
+        our_sign_info = &params->sign_info_req;
+        our_master_id = &params->master_id_req;
+        our_enc_info = &params->enc_info_req;
+        our_id_info = &params->id_info_req;
+    }
+
+    sc = params->pair_req.authreq & BLE_SM_PAIR_AUTHREQ_SC &&
+         params->pair_rsp.authreq & BLE_SM_PAIR_AUTHREQ_SC;
+
+    if (!sc && our_key_dist & BLE_SM_PAIR_KEY_DIST_ENC) {
+        ble_sm_test_util_verify_tx_enc_info(our_enc_info);
+        ble_sm_test_util_verify_tx_master_id(our_master_id);
+    }
+    if (our_key_dist & BLE_SM_PAIR_KEY_DIST_ID) {
+        ble_sm_test_util_verify_tx_id_info(our_id_info);
+        ble_sm_test_util_verify_tx_id_addr_info(our_id_addr_info);
+    }
+    if (our_key_dist & BLE_SM_PAIR_KEY_DIST_SIGN) {
+        ble_sm_test_util_verify_tx_sign_info(our_sign_info);
+    }
+}
+
+static void
+ble_sm_test_util_us_lgcy_good_once(struct ble_sm_test_params *params)
+{
+    struct ble_sm_test_util_entity peer_entity;
+    struct ble_sm_test_util_entity our_entity;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_sm_test_util_init_good(params, 1, &conn, &our_entity, &peer_entity);
+
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    ble_hs_test_util_set_ack(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_START_ENCRYPT), 0);
+    if (params->sec_req.authreq != 0) {
+        ble_sm_test_util_rx_sec_req(2, &params->sec_req, 0);
+    } else {
+        /* Initiate the pairing procedure. */
+        rc = ble_gap_security_initiate(2);
+        TEST_ASSERT_FATAL(rc == 0);
+    }
+
+    /* Ensure we sent the expected pair request. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_req(our_entity.pair_cmd);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive a pair response from the peer. */
+    ble_sm_test_util_rx_pair_rsp(2, peer_entity.pair_cmd, 0);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    ble_sm_test_util_io_inject(&params->passkey_info,
+                               BLE_SM_PROC_STATE_CONFIRM);
+
+    /* Ensure we sent the expected pair confirm. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_confirm(our_entity.confirms);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive a pair confirm from the peer. */
+    ble_sm_test_util_rx_confirm(2, peer_entity.confirms);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Ensure we sent the expected pair random. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_random(our_entity.randoms);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive a pair random from the peer. */
+    ble_sm_test_util_rx_random(2, peer_entity.randoms, 0);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Ensure we sent the expected start encryption command. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_start_enc(2, 0, 0, params->stk);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive an encryption changed event. */
+    ble_sm_test_util_rx_enc_change(2, 0, 1);
+
+    /* Receive key material from peer. */
+    ble_sm_test_util_rx_keys(params, 1);
+
+    /* Verify key material gets sent to peer. */
+    ble_sm_test_util_verify_tx_keys(params, 1);
+
+    /* Pairing should now be complete. */
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Verify that security callback was executed. */
+    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
+    TEST_ASSERT(ble_sm_test_gap_status == 0);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated == params->authenticated);
+
+    /* Verify that connection has correct security state. */
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
+                conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                conn->bhc_sec_state.authenticated);
+
+    /* Verify the appropriate security material was persisted. */
+    ble_sm_test_util_verify_persist(params, 1);
+
+    ble_hs_test_util_conn_disconnect(2);
+}
+
+void
+ble_sm_test_util_us_lgcy_good(struct ble_sm_test_params *params)
+{
+    /*** We are master. */
+
+    /* We initiate pairing. */
+    params->sec_req.authreq = 0;
+    ble_sm_test_util_us_lgcy_good_once(params);
+
+    /* Peer initiates with security request. */
+    params->sec_req.authreq = params->pair_rsp.authreq;
+    ble_sm_test_util_us_lgcy_good_once(params);
+
+    /* Verify link can be restored via the encryption procedure. */
+    ble_sm_test_util_bonding_all(params, 1);
+}
+
+static void
+ble_sm_test_util_peer_lgcy_good_once(struct ble_sm_test_params *params)
+{
+    struct ble_sm_test_util_entity peer_entity;
+    struct ble_sm_test_util_entity our_entity;
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_sm_test_util_init_good(params, 0, &conn, &our_entity, &peer_entity);
+
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    if (params->sec_req.authreq != 0) {
+        rc = ble_sm_slave_initiate(2);
+        TEST_ASSERT(rc == 0);
+
+        /* Ensure we sent the expected security request. */
+        ble_sm_test_util_verify_tx_sec_req(&params->sec_req);
+    }
+
+    /* Receive a pair request from the peer. */
+    ble_sm_test_util_rx_pair_req(2, peer_entity.pair_cmd, 0);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Ensure we sent the expected pair response. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_rsp(our_entity.pair_cmd);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    ble_sm_test_util_io_check_pre(&params->passkey_info,
+                                  BLE_SM_PROC_STATE_CONFIRM);
+
+    /* Receive a pair confirm from the peer. */
+    ble_sm_test_util_rx_confirm(2, peer_entity.confirms);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    ble_sm_test_util_io_check_post(&params->passkey_info,
+                                   BLE_SM_PROC_STATE_CONFIRM);
+
+    /* Ensure we sent the expected pair confirm. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_confirm(our_entity.confirms);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive a pair random from the peer. */
+    ble_sm_test_util_rx_random(2, peer_entity.randoms, 0);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Ensure we sent the expected pair random. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_random(our_entity.randoms);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive a long term key request from the controller. */
+    ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
+    ble_sm_test_util_rx_lt_key_req(2, 0, 0);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Ensure we sent the expected long term key request reply command. */
+    ble_sm_test_util_verify_tx_lt_key_req_reply(2, params->stk);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive an encryption changed event. */
+    ble_sm_test_util_rx_enc_change(2, 0, 1);
+
+    /* Verify key material gets sent to peer. */
+    ble_sm_test_util_verify_tx_keys(params, 0);
+
+    /* Receive key material from peer. */
+    ble_sm_test_util_rx_keys(params, 0);
+
+    /* Pairing should now be complete. */
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Verify that security callback was executed. */
+    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
+    TEST_ASSERT(ble_sm_test_gap_status == 0);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                params->authenticated);
+
+    /* Verify that connection has correct security state. */
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
+                conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                conn->bhc_sec_state.authenticated);
+
+    /* Verify the appropriate security material was persisted. */
+    ble_sm_test_util_verify_persist(params, 0);
+
+    ble_hs_test_util_conn_disconnect(2);
+}
+
+void
+ble_sm_test_util_peer_lgcy_good(struct ble_sm_test_params *params)
+{
+    /*** Peer is master. */
+
+    /* Peer performs IO first; peer initiates pairing. */
+    params->passkey_info.io_before_rx = 0;
+    params->sec_req.authreq = 0;
+    ble_sm_test_util_peer_lgcy_good_once(params);
+
+    /* Peer performs IO first; we initiate with security request. */
+    params->passkey_info.io_before_rx = 0;
+    params->sec_req.authreq = params->pair_rsp.authreq;
+    ble_sm_test_util_peer_lgcy_good_once(params);
+
+    /* We perform IO first; peer initiates pairing. */
+    params->passkey_info.io_before_rx = 1;
+    params->sec_req.authreq = 0;
+    ble_sm_test_util_peer_lgcy_good_once(params);
+
+    /* We perform IO first; we initiate with security request. */
+    params->passkey_info.io_before_rx = 1;
+    params->sec_req.authreq = params->pair_rsp.authreq;
+    ble_sm_test_util_peer_lgcy_good_once(params);
+
+    /* Verify link can be restored via the encryption procedure. */
+    ble_sm_test_util_bonding_all(params, 0);
+}
+
+static void
+ble_sm_test_util_us_sc_good_once(struct ble_sm_test_params *params)
+{
+    struct ble_sm_test_util_entity peer_entity;
+    struct ble_sm_test_util_entity our_entity;
+    struct ble_hs_conn *conn;
+    int num_iters;
+    int rc;
+    int i;
+
+    ble_sm_test_util_init_good(params, 1, &conn, &our_entity, &peer_entity);
+
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    ble_hs_test_util_set_ack(
+        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LE,
+                                    BLE_HCI_OCF_LE_START_ENCRYPT), 0);
+    if (params->sec_req.authreq != 0) {
+        ble_sm_test_util_rx_sec_req(2, &params->sec_req, 0);
+    } else {
+        /* Initiate the pairing procedure. */
+        rc = ble_gap_security_initiate(2);
+        TEST_ASSERT_FATAL(rc == 0);
+    }
+
+    /* Ensure we sent the expected pair request. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_req(our_entity.pair_cmd);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive a pair response from the peer. */
+    ble_sm_test_util_rx_pair_rsp(2, peer_entity.pair_cmd, 0);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Ensure we sent the expected public key. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_public_key(our_entity.public_key);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive a public key from the peer. */
+    ble_sm_test_util_rx_public_key(2, peer_entity.public_key);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    switch (params->pair_alg) {
+    case BLE_SM_PAIR_ALG_PASSKEY:
+        num_iters = 20;
+        break;
+
+    default:
+        num_iters = 1;
+        break;
+    }
+
+    ble_sm_test_util_io_inject(&params->passkey_info,
+                               BLE_SM_PROC_STATE_CONFIRM);
+
+    for (i = 0; i < num_iters; i++) {
+        if (params->pair_alg != BLE_SM_PAIR_ALG_JW      &&
+            params->pair_alg != BLE_SM_PAIR_ALG_NUMCMP) {
+
+            if (i < num_iters - 1) {
+                ble_sm_dbg_set_next_pair_rand(
+                    our_entity.randoms[i + 1].value);
+            }
+
+            /* Ensure we sent the expected pair confirm. */
+            ble_hs_test_util_tx_all();
+            ble_sm_test_util_verify_tx_pair_confirm(our_entity.confirms + i);
+            TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+            TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+            ble_sm_test_util_io_inject_bad(
+                2, params->passkey_info.passkey.action);
+        }
+
+        /* Receive a pair confirm from the peer. */
+        ble_sm_test_util_rx_confirm(2, peer_entity.confirms + i);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+        /* Ensure we sent the expected pair random. */
+        ble_hs_test_util_tx_all();
+        ble_sm_test_util_verify_tx_pair_random(our_entity.randoms + i);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+        /* Receive a pair random from the peer. */
+        ble_sm_test_util_rx_random(2, peer_entity.randoms + i, 0);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+    }
+
+    ble_sm_test_util_io_inject(&params->passkey_info,
+                               BLE_SM_PROC_STATE_DHKEY_CHECK);
+
+    /* Ensure we sent the expected dhkey check. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_dhkey_check(our_entity.dhkey_check);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive a dhkey check from the peer. */
+    ble_sm_test_util_rx_dhkey_check(2, peer_entity.dhkey_check, 0);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Ensure we sent the expected start encryption command. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_start_enc(2, 0, 0, params->ltk);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive an encryption changed event. */
+    ble_sm_test_util_rx_enc_change(2, 0, 1);
+
+    /* Receive key material from peer. */
+    ble_sm_test_util_rx_keys(params, 1);
+
+    /* Verify key material gets sent to peer. */
+    ble_sm_test_util_verify_tx_keys(params, 1);
+
+    /* Pairing should now be complete. */
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Verify that security callback was executed. */
+    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
+    TEST_ASSERT(ble_sm_test_gap_status == 0);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                params->authenticated);
+
+    /* Verify that connection has correct security state. */
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
+                conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                conn->bhc_sec_state.authenticated);
+
+    /* Verify the appropriate security material was persisted. */
+    ble_sm_test_util_verify_persist(params, 1);
+
+    ble_hs_test_util_conn_disconnect(2);
+}
+
+void
+ble_sm_test_util_us_sc_good(struct ble_sm_test_params *params)
+{
+    /*** We are master. */
+
+    /* We initiate pairing. */
+    params->passkey_info.io_before_rx = 0;
+    params->sec_req.authreq = 0;
+    ble_sm_test_util_us_sc_good_once(params);
+
+    /* Peer initiates with security request. */
+    params->passkey_info.io_before_rx = 0;
+    params->sec_req.authreq = params->pair_rsp.authreq;
+    ble_sm_test_util_us_sc_good_once(params);
+
+    /* Verify link can be restored via the encryption procedure. */
+    ble_sm_test_util_bonding_all(params, 1);
+}
+
+static void
+ble_sm_test_util_peer_sc_good_once(struct ble_sm_test_params *params)
+{
+    struct ble_sm_test_util_entity peer_entity;
+    struct ble_sm_test_util_entity our_entity;
+    struct ble_hs_conn *conn;
+    int num_iters;
+    int rc;
+    int i;
+
+    ble_sm_test_util_init_good(params, 0, &conn, &our_entity, &peer_entity);
+
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    if (params->sec_req.authreq != 0) {
+        rc = ble_sm_slave_initiate(2);
+        TEST_ASSERT(rc == 0);
+
+        /* Ensure we sent the expected security request. */
+        ble_sm_test_util_verify_tx_sec_req(&params->sec_req);
+    }
+
+    /* Receive a pair request from the peer. */
+    ble_sm_test_util_rx_pair_req(2, peer_entity.pair_cmd, 0);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Ensure we sent the expected pair response. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_rsp(our_entity.pair_cmd);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive a public key from the peer. */
+    ble_sm_test_util_rx_public_key(2, peer_entity.public_key);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Ensure we sent the expected public key. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_public_key(our_entity.public_key);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    switch (params->pair_alg) {
+    case BLE_SM_PAIR_ALG_PASSKEY:
+        num_iters = 20;
+        break;
+
+    default:
+        num_iters = 1;
+        break;
+    }
+
+    ble_sm_test_util_io_check_pre(&params->passkey_info,
+                                  BLE_SM_PROC_STATE_CONFIRM);
+
+    for (i = 0; i < num_iters; i++) {
+        if (params->pair_alg != BLE_SM_PAIR_ALG_JW      &&
+            params->pair_alg != BLE_SM_PAIR_ALG_NUMCMP) {
+
+            /* Receive a pair confirm from the peer. */
+            ble_sm_test_util_rx_confirm(2, peer_entity.confirms + i);
+            TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+            TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+            ble_sm_test_util_io_inject_bad(
+                2, params->passkey_info.passkey.action);
+
+            if (i < num_iters - 1) {
+                ble_sm_dbg_set_next_pair_rand(
+                    our_entity.randoms[i + 1].value);
+            }
+        }
+
+        if (i == 0) {
+            ble_sm_test_util_io_check_post(&params->passkey_info,
+                                           BLE_SM_PROC_STATE_CONFIRM);
+        }
+
+        /* Ensure we sent the expected pair confirm. */
+        ble_hs_test_util_tx_all();
+        ble_sm_test_util_verify_tx_pair_confirm(our_entity.confirms + i);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+        /* Receive a pair random from the peer. */
+        ble_sm_test_util_rx_random(2, peer_entity.randoms + i, 0);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+        /* Ensure we sent the expected pair random. */
+        ble_hs_test_util_tx_all();
+        ble_sm_test_util_verify_tx_pair_random(our_entity.randoms + i);
+        TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+        TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+        ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    }
+
+    ble_sm_test_util_io_check_pre(&params->passkey_info,
+                                  BLE_SM_PROC_STATE_DHKEY_CHECK);
+
+    /* Receive a dhkey check from the peer. */
+    ble_sm_test_util_rx_dhkey_check(2, peer_entity.dhkey_check, 0);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    ble_sm_test_util_io_check_post(&params->passkey_info,
+                                   BLE_SM_PROC_STATE_DHKEY_CHECK);
+
+    /* Ensure we sent the expected dhkey check. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_dhkey_check(our_entity.dhkey_check);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive a long term key request from the controller. */
+    ble_sm_test_util_set_lt_key_req_reply_ack(0, 2);
+    ble_sm_test_util_rx_lt_key_req(2, 0, 0);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Ensure we sent the expected long term key request reply command. */
+    ble_sm_test_util_verify_tx_lt_key_req_reply(2, params->ltk);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive an encryption changed event. */
+    ble_sm_test_util_rx_enc_change(2, 0, 1);
+
+    /* Verify key material gets sent to peer. */
+    ble_sm_test_util_verify_tx_keys(params, 0);
+
+    /* Receive key material from peer. */
+    ble_sm_test_util_rx_keys(params, 0);
+
+    /* Pairing should now be complete. */
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Verify that security callback was executed. */
+    TEST_ASSERT(ble_sm_test_gap_event_type == BLE_GAP_EVENT_ENC_CHANGE);
+    TEST_ASSERT(ble_sm_test_gap_status == 0);
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                params->authenticated);
+
+    /* Verify that connection has correct security state. */
+    TEST_ASSERT(ble_sm_test_sec_state.encrypted ==
+                conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_test_sec_state.authenticated ==
+                conn->bhc_sec_state.authenticated);
+
+    /* Verify the appropriate security material was persisted. */
+    ble_sm_test_util_verify_persist(params, 0);
+
+    ble_hs_test_util_conn_disconnect(2);
+}
+
+void
+ble_sm_test_util_peer_sc_good(struct ble_sm_test_params *params)
+{
+    /*** Peer is master. */
+
+    /* Peer performs IO first; peer initiates pairing. */
+    params->passkey_info.io_before_rx = 0;
+    params->sec_req.authreq = 0;
+    ble_sm_test_util_peer_sc_good_once(params);
+
+    /* Peer performs IO first; we initiate with security request. */
+    params->passkey_info.io_before_rx = 0;
+    params->sec_req.authreq = params->pair_req.authreq;
+    ble_sm_test_util_peer_sc_good_once(params);
+
+    /* We perform IO first; peer initiates pairing. */
+    params->passkey_info.io_before_rx = 1;
+    params->sec_req.authreq = 0;
+    ble_sm_test_util_peer_sc_good_once(params);
+
+    /* We perform IO first; we initiate with security request. */
+    params->passkey_info.io_before_rx = 1;
+    params->sec_req.authreq = params->pair_req.authreq;
+    ble_sm_test_util_peer_sc_good_once(params);
+
+    /* Verify link can be restored via the encryption procedure. */
+    ble_sm_test_util_bonding_all(params, 0);
+}
+
+void
+ble_sm_test_util_us_fail_inval(struct ble_sm_test_params *params)
+{
+    struct ble_hs_conn *conn;
+    int rc;
+
+    ble_sm_test_util_init();
+    ble_hs_id_set_pub(params->resp_id_addr);
+
+    ble_sm_dbg_set_next_pair_rand(((uint8_t[16]){0}));
+
+    ble_hs_test_util_create_conn(2, params->init_id_addr,
+                                 ble_sm_test_util_conn_cb,
+                                 NULL);
+
+    /* This test inspects and modifies the connection object after unlocking
+     * the host mutex.  It is not OK for real code to do this, but this test
+     * can assume the connection list is unchanging.
+     */
+    ble_hs_lock();
+    conn = ble_hs_conn_find(2);
+    TEST_ASSERT_FATAL(conn != NULL);
+    ble_hs_unlock();
+
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Initiate the pairing procedure. */
+    rc = ble_hs_test_util_security_initiate(2, 0);
+    TEST_ASSERT_FATAL(rc == 0);
+
+    /* Ensure we sent the expected pair request. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_req(&params->pair_req);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 1);
+    ble_sm_test_util_io_inject_bad(2, params->passkey_info.passkey.action);
+
+    /* Receive a pair response from the peer. */
+    ble_sm_test_util_rx_pair_rsp(
+        2, &params->pair_rsp, BLE_HS_SM_US_ERR(BLE_SM_ERR_INVAL));
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Ensure we sent the expected pair fail. */
+    ble_hs_test_util_tx_all();
+    ble_sm_test_util_verify_tx_pair_fail(&params->pair_fail);
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(ble_sm_dbg_num_procs() == 0);
+
+    /* Verify that security callback was not executed. */
+    TEST_ASSERT(ble_sm_test_gap_event_type == -1);
+    TEST_ASSERT(ble_sm_test_gap_status == -1);
+
+    /* Verify that connection has correct security state. */
+    TEST_ASSERT(!conn->bhc_sec_state.encrypted);
+    TEST_ASSERT(!conn->bhc_sec_state.authenticated);
+}