You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by vi...@apache.org on 2016/10/10 19:26:17 UTC

[01/50] [abbrv] incubator-mynewt-core git commit: newtmgr; move defs to default commands to nmgr os package header file.

Repository: incubator-mynewt-core
Updated Branches:
  refs/heads/develop ec1af7e76 -> fda3dd6f3
  refs/heads/sterly_refactor 0c0f3e129 -> 5d11ead1a


newtmgr; move defs to default commands to nmgr os package header file.


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

Branch: refs/heads/sterly_refactor
Commit: 895d567e2862ead08375c41e3f4c731c512baab8
Parents: 7dc40e7
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Sep 19 14:40:35 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Sep 19 14:40:35 2016 -0700

----------------------------------------------------------------------
 libs/newtmgr/include/newtmgr/newtmgr.h    | 11 -----------
 libs/newtmgr/os/include/nmgr_os/nmgr_os.h | 10 ++++++++++
 libs/newtmgr/os/src/newtmgr_os.c          |  2 ++
 3 files changed, 12 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/895d567e/libs/newtmgr/include/newtmgr/newtmgr.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/include/newtmgr/newtmgr.h b/libs/newtmgr/include/newtmgr/newtmgr.h
index 85e6c44..ab84806 100644
--- a/libs/newtmgr/include/newtmgr/newtmgr.h
+++ b/libs/newtmgr/include/newtmgr/newtmgr.h
@@ -61,17 +61,6 @@
 #define NMGR_ERR_ENOENT   (5)
 #define NMGR_ERR_EPERUSER (256)
 
-
-/*
- * Id's for default group commands
- */
-#define NMGR_ID_ECHO            0
-#define NMGR_ID_CONS_ECHO_CTRL  1
-#define NMGR_ID_TASKSTATS       2
-#define NMGR_ID_MPSTATS         3
-#define NMGR_ID_DATETIME_STR    4
-#define NMGR_ID_RESET           5
-
 struct nmgr_hdr {
     uint8_t  nh_op;             /* NMGR_OP_XXX */
     uint8_t  nh_flags;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/895d567e/libs/newtmgr/os/include/nmgr_os/nmgr_os.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/os/include/nmgr_os/nmgr_os.h b/libs/newtmgr/os/include/nmgr_os/nmgr_os.h
index 2c89237..49b6725 100644
--- a/libs/newtmgr/os/include/nmgr_os/nmgr_os.h
+++ b/libs/newtmgr/os/include/nmgr_os/nmgr_os.h
@@ -20,6 +20,16 @@
 #ifndef _NMGR_OS_H_
 #define _NMGR_OS_H_
 
+/*
+ * Id's for OS group commands
+ */
+#define NMGR_ID_ECHO            0
+#define NMGR_ID_CONS_ECHO_CTRL  1
+#define NMGR_ID_TASKSTATS       2
+#define NMGR_ID_MPSTATS         3
+#define NMGR_ID_DATETIME_STR    4
+#define NMGR_ID_RESET           5
+
 int nmgr_os_groups_register(void);
 
 #endif /* _NMGR_OS_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/895d567e/libs/newtmgr/os/src/newtmgr_os.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/os/src/newtmgr_os.c b/libs/newtmgr/os/src/newtmgr_os.c
index 88b6553..cab692b 100644
--- a/libs/newtmgr/os/src/newtmgr_os.c
+++ b/libs/newtmgr/os/src/newtmgr_os.c
@@ -32,6 +32,8 @@
 #include <util/datetime.h>
 #include <reboot/log_reboot.h>
 
+#include "nmgr_os/nmgr_os.h"
+
 static struct os_callout_func nmgr_reset_callout;
 
 static int nmgr_def_echo(struct nmgr_jbuf *);


[19/50] [abbrv] incubator-mynewt-core git commit: Merge branch 'develop' into sterly_refactor

Posted by vi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/test/src/ble_gap_test.c
----------------------------------------------------------------------
diff --cc net/nimble/host/test/src/ble_gap_test.c
index 16a7309,0000000..3fe398f
mode 100644,000000..100644
--- a/net/nimble/host/test/src/ble_gap_test.c
+++ b/net/nimble/host/test/src/ble_gap_test.c
@@@ -1,2580 -1,0 +1,2920 @@@
 +/**
 + * 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 struct ble_gap_event ble_gap_test_event;
 +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;
++    memset(&ble_gap_test_event, 0xff, sizeof ble_gap_test_event);
 +    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_event = *event;
 +    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;
 +
++    case BLE_GAP_EVENT_MTU:
++        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)
++    const 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_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);
++    TEST_ASSERT(ble_gap_test_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_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);
++    TEST_ASSERT(ble_gap_test_event.type == 0xff);
 +
 +    /* 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_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;
++    ble_gap_test_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_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_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_gap_test_event.type == 0xff);
 +    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_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_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_ASSERT(ble_gap_test_event.type == 0xff);
 +            }
 +        }
 +    }
 +}
 +
 +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_event.type == 0xff);
 +            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_event.type == 0xff);
 +            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)
++ble_gap_test_util_update_no_l2cap(struct ble_gap_upd_params *params,
++                                  int master,
++                                  uint8_t hci_status, int event_status)
 +{
-     int status;
++    struct ble_hs_conn *conn;
 +    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);
 +
++    if (!master) {
++        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();
++    }
++
++    /* Erase callback info reported during connection establishment; we only
++     * care about updates.
++     */
++    ble_gap_test_util_reset_cb_info();
++
 +    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));
++        TEST_ASSERT(ble_gap_dbg_update_active(2));
++
++        /* Receive connection update complete event. */
++        ble_gap_test_util_rx_update_complete(event_status, params);
++
++        TEST_ASSERT(!ble_gap_master_in_progress());
++        TEST_ASSERT(!ble_gap_dbg_update_active(2));
++
++        TEST_ASSERT(ble_gap_test_event.type == BLE_GAP_EVENT_CONN_UPDATE);
++        TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_HCI_ERR(event_status));
++        if (event_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);
++        }
 +    } else {
-         TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
-         return;
++        TEST_ASSERT(!ble_gap_master_in_progress());
++        TEST_ASSERT(!ble_gap_dbg_update_active(2));
++
++        TEST_ASSERT(ble_gap_test_event.type == 0xff);
 +    }
++}
 +
-     /* Receive connection update complete event. */
-     ble_gap_test_util_rx_update_complete(event_status, params);
++static void
++ble_gap_test_util_update_l2cap(struct ble_gap_upd_params *params,
++                               uint8_t hci_status, int event_status,
++                               uint16_t l2cap_result)
++{
++    struct ble_l2cap_sig_update_params l2cap_params;
++    struct ble_hs_conn *conn;
++    uint8_t id;
++    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);
++
++    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();
++
++    /* Erase callback info reported during connection establishment; we only
++     * care about updates.
++     */
++    ble_gap_test_util_reset_cb_info();
++
++    rc = ble_hs_test_util_conn_update(2, params, hci_status);
++    TEST_ASSERT(rc == 0);
++
++    /* Verify tx of connection update command. */
++    ble_gap_test_util_verify_tx_update_conn(params);
++
++    switch (hci_status) {
++    case 0:
++        /* Receive connection update complete event. */
++        ble_gap_test_util_rx_update_complete(event_status, params);
++        break;
++    case BLE_ERR_UNKNOWN_HCI_CMD:
++        break;
++    default:
++        TEST_ASSERT_FATAL(0);
++        break;
++    }
++
++    TEST_ASSERT(ble_gap_dbg_update_active(2));
++
++    l2cap_params.itvl_min = params->itvl_min;
++    l2cap_params.itvl_max = params->itvl_max;
++    l2cap_params.slave_latency = params->latency;
++    l2cap_params.timeout_multiplier = params->supervision_timeout;
++    id = ble_hs_test_util_verify_tx_l2cap_update_req(&l2cap_params);
 +
-     if (event_status != 0) {
-         status = BLE_HS_HCI_ERR(event_status);
-         goto fail;
++    /* Receive l2cap connection parameter update response. */
++    ble_hs_test_util_rx_l2cap_update_rsp(2, id, l2cap_result);
++    if (l2cap_result == BLE_L2CAP_SIG_UPDATE_RSP_RESULT_ACCEPT) {
++        TEST_ASSERT(ble_gap_dbg_update_active(2));
++
++        /* Receive connection update complete event. */
++        ble_gap_test_util_rx_update_complete(0, params);
++    }
++
++    TEST_ASSERT(!ble_gap_dbg_update_active(2));
++
++    TEST_ASSERT(ble_gap_test_event.type == BLE_GAP_EVENT_CONN_UPDATE);
++    if (l2cap_result != BLE_L2CAP_SIG_UPDATE_RSP_RESULT_ACCEPT) {
++        TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_EREJECT);
++    } else {
++        TEST_ASSERT(ble_gap_test_conn_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_conn_desc.conn_handle == 2);
++    TEST_ASSERT(memcmp(ble_gap_test_conn_desc.peer_id_addr,
++                       peer_addr, 6) == 0);
++}
++
++static void
++ble_gap_test_util_update_no_l2cap_tmo(struct ble_gap_upd_params *params,
++                                      int master)
++{
++    struct ble_hs_conn *conn;
++    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);
++
++    if (!master) {
++        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();
 +    }
 +
++    /* Erase callback info reported during connection establishment; we only
++     * care about updates.
++     */
++    ble_gap_test_util_reset_cb_info();
++
 +    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);
++    rc = ble_hs_test_util_conn_update(2, params, 0);
++    TEST_ASSERT(rc == 0);
++    TEST_ASSERT(!ble_gap_master_in_progress());
++
++    /* Verify tx of connection update command. */
++    ble_gap_test_util_verify_tx_update_conn(params);
++
++    /* Ensure no update event reported. */
++    TEST_ASSERT(ble_gap_test_event.type == 0xff);
++
++    /* Advance 29 seconds; ensure no timeout reported. */
++    os_time_advance(29 * OS_TICKS_PER_SEC);
++    ble_gap_heartbeat();
++    TEST_ASSERT(ble_gap_test_event.type == 0xff);
++
++    /* Advance 30th second; ensure timeout reported. */
++    os_time_advance(1 * OS_TICKS_PER_SEC);
++
++    /* Timeout will result in a terminate HCI command being sent; schedule ack
++     * from controller.
++     */
++    ble_hs_test_util_set_ack_disconnect(0);
++
++    ble_gap_heartbeat();
++
++    /* Verify terminate was sent. */
++    ble_gap_test_util_verify_tx_disconnect();
++
++    TEST_ASSERT(ble_gap_test_event.type == BLE_GAP_EVENT_CONN_UPDATE);
++    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_ETIMEOUT);
 +    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));
++static void
++ble_gap_test_util_update_l2cap_tmo(struct ble_gap_upd_params *params,
++                                   uint8_t hci_status, uint8_t event_status,
++                                   int rx_l2cap)
++{
++    struct ble_l2cap_sig_update_params l2cap_params;
++    struct ble_hs_conn *conn;
++    uint8_t id;
++    int rc;
 +
-     return;
++    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);
++
++    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();
++
++    /* Erase callback info reported during connection establishment; we only
++     * care about updates.
++     */
++    ble_gap_test_util_reset_cb_info();
++
++    rc = ble_hs_test_util_conn_update(2, params, hci_status);
++    TEST_ASSERT(rc == 0);
++
++    /* Verify tx of connection update command. */
++    ble_gap_test_util_verify_tx_update_conn(params);
++
++    switch (hci_status) {
++    case 0:
++        /* Receive connection update complete event. */
++        ble_gap_test_util_rx_update_complete(event_status, params);
++        break;
++    case BLE_ERR_UNKNOWN_HCI_CMD:
++        break;
++    default:
++        TEST_ASSERT_FATAL(0);
++        break;
++    }
++
++    TEST_ASSERT(ble_gap_dbg_update_active(2));
++
++    if (rx_l2cap) {
++        l2cap_params.itvl_min = params->itvl_min;
++        l2cap_params.itvl_max = params->itvl_max;
++        l2cap_params.slave_latency = params->latency;
++        l2cap_params.timeout_multiplier = params->supervision_timeout;
++        id = ble_hs_test_util_verify_tx_l2cap_update_req(&l2cap_params);
++
++        /* Receive l2cap connection parameter update response. */
++        ble_hs_test_util_rx_l2cap_update_rsp(
++            2, id, BLE_L2CAP_SIG_UPDATE_RSP_RESULT_ACCEPT);
++    }
++
++    TEST_ASSERT(ble_gap_dbg_update_active(2));
++
++    /* Ensure no update event reported. */
++    TEST_ASSERT(ble_gap_test_event.type == 0xff);
++
++    /* Advance 29 seconds; ensure no timeout reported. */
++    os_time_advance(29 * OS_TICKS_PER_SEC);
++    ble_gap_heartbeat();
++    ble_l2cap_sig_heartbeat();
++    TEST_ASSERT(ble_gap_test_event.type == 0xff);
++
++    /* Advance 30th second; ensure timeout reported. */
++    os_time_advance(1 * OS_TICKS_PER_SEC);
 +
- fail:
-     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
-     TEST_ASSERT(ble_gap_test_conn_status == status);
++    /* Timeout will result in a terminate HCI command being sent; schedule ack
++     * from controller.
++     */
++    ble_hs_test_util_set_ack_disconnect(0);
++
++    ble_gap_heartbeat();
++    ble_l2cap_sig_heartbeat();
++
++    /* Verify terminate was sent. */
++    ble_gap_test_util_verify_tx_disconnect();
++
++    TEST_ASSERT(ble_gap_test_event.type == BLE_GAP_EVENT_CONN_UPDATE);
++    TEST_ASSERT(ble_gap_test_conn_status == BLE_HS_ETIMEOUT);
 +    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_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));
++    TEST_ASSERT(!ble_gap_dbg_update_active(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));
++
++    /* We don't maintain an update entry when the peer initiates. */
++    TEST_ASSERT(!ble_gap_dbg_update_active(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));
++    TEST_ASSERT(!ble_gap_dbg_update_active(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_dbg_update_active(2));
 +
-     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
++    TEST_ASSERT(ble_gap_test_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_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));
++    TEST_ASSERT(!ble_gap_dbg_update_active(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));
++    TEST_ASSERT(!ble_gap_dbg_update_active(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));
++    TEST_ASSERT(!ble_gap_dbg_update_active(2));
 +
 +    return;
 +
 +hci_fail:
-     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
++    TEST_ASSERT(ble_gap_test_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));
++    TEST_ASSERT(!ble_gap_dbg_update_active(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));
++        TEST_ASSERT(ble_gap_dbg_update_active(2));
 +    } else {
-         TEST_ASSERT(!ble_gap_test_util_update_in_progress(2));
++        TEST_ASSERT(!ble_gap_dbg_update_active(2));
 +        return;
 +    }
 +
 +    TEST_ASSERT(!ble_gap_master_in_progress());
-     TEST_ASSERT(ble_gap_test_util_update_in_progress(2));
++    TEST_ASSERT(ble_gap_dbg_update_active(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));
++    TEST_ASSERT(ble_gap_dbg_update_active(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));
++    TEST_ASSERT(ble_gap_dbg_update_active(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_dbg_update_active(2));
 +
-     TEST_ASSERT(ble_gap_test_conn_event_type == BLE_GAP_EVENT_CONN_UPDATE);
++    TEST_ASSERT(ble_gap_test_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_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(
++    ble_gap_test_util_update_no_l2cap(
 +        ((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);
++        1, 0, 0);
 +
-     ble_gap_test_util_update(
++    ble_gap_test_util_update_no_l2cap(
 +        ((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);
++        1, 0, 0);
 +}
 +
 +TEST_CASE(ble_gap_test_case_update_conn_bad)
 +{
-     ble_gap_test_util_update(
++    ble_gap_test_util_update_no_l2cap(
 +        ((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);
++        1, 0, BLE_ERR_LMP_COLLISION);
 +}
 +
 +TEST_CASE(ble_gap_test_case_update_conn_hci_fail)
 +{
-     ble_gap_test_util_update(
++    ble_gap_test_util_update_no_l2cap(
 +        ((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);
++        1, BLE_ERR_UNSUPPORTED, 0);
++}
++
++TEST_CASE(ble_gap_test_case_update_conn_l2cap)
++{
++    struct ble_gap_upd_params params = {
++        .itvl_min = 10,
++        .itvl_max = 100,
++        .supervision_timeout = 0,
++        .min_ce_len = 123,
++        .max_ce_len = 456,
++    };
++
++    /* Accepted L2CAP failover; success. */
++    ble_gap_test_util_update_l2cap(&params, BLE_ERR_UNKNOWN_HCI_CMD, 0,
++                                   BLE_L2CAP_SIG_UPDATE_RSP_RESULT_ACCEPT);
++    ble_gap_test_util_update_l2cap(&params, 0, BLE_ERR_UNSUPP_REM_FEATURE,
++                                   BLE_L2CAP_SIG_UPDATE_RSP_RESULT_ACCEPT);
++
++    /* Accepted L2CAP failover; failure. */
++    ble_gap_test_util_update_l2cap(&params, BLE_ERR_UNKNOWN_HCI_CMD, 0, 
++                                   BLE_L2CAP_SIG_UPDATE_RSP_RESULT_ACCEPT);
++    ble_gap_test_util_update_l2cap(&params, 0, BLE_ERR_UNSUPP_REM_FEATURE,
++                                   BLE_L2CAP_SIG_UPDATE_RSP_RESULT_ACCEPT);
++
++    /* Rejected L2CAP failovers. */
++    ble_gap_test_util_update_l2cap(&params, BLE_ERR_UNKNOWN_HCI_CMD, 0, 
++                                   BLE_L2CAP_SIG_UPDATE_RSP_RESULT_REJECT);
++    ble_gap_test_util_update_l2cap(&params, 0, BLE_ERR_UNSUPP_REM_FEATURE,
++                                   BLE_L2CAP_SIG_UPDATE_RSP_RESULT_REJECT);
++
++    /* Don't attempt L2CAP failover on other event status. */
++    ble_gap_test_util_update_l2cap(&params, BLE_ERR_UNKNOWN_HCI_CMD, 0, 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,
 +            .su

<TRUNCATED>


[26/50] [abbrv] incubator-mynewt-core git commit: remove hal watchdog stop, as many platforms don't provide the ability to stop a watchdog, so the OS must have intrinisic mechanisms for tickling.

Posted by vi...@apache.org.
remove hal watchdog stop, as many platforms don't provide the ability to stop a watchdog, so the OS must have intrinisic mechanisms for tickling.


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

Branch: refs/heads/sterly_refactor
Commit: 8371baab850ff45dbdc873aff4011147848ec106
Parents: 8dffea7
Author: Sterling Hughes <st...@apache.org>
Authored: Wed Sep 21 19:59:14 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Wed Sep 21 19:59:14 2016 -0700

----------------------------------------------------------------------
 hw/hal/include/hal/hal_watchdog.h | 7 -------
 1 file changed, 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8371baab/hw/hal/include/hal/hal_watchdog.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_watchdog.h b/hw/hal/include/hal/hal_watchdog.h
index 50f6aff..e0161ce 100644
--- a/hw/hal/include/hal/hal_watchdog.h
+++ b/hw/hal/include/hal/hal_watchdog.h
@@ -41,13 +41,6 @@ int hal_watchdog_init(int expire_secs);
 int hal_watchdog_enable(void);
 
 /*
- * Stops the watchdog.
- *
- * @return			0 on success; non-zero on failure.
- */
-int hal_watchdog_stop(void);
-
-/*
  * Tickles the watchdog. Needs to be done before 'expire_secs' fires.
  */
 void hal_watchdog_tickle(void);


[30/50] [abbrv] incubator-mynewt-core git commit: Re-enable shell for apps that need it.

Posted by vi...@apache.org.
Re-enable shell for apps that need it.


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

Branch: refs/heads/sterly_refactor
Commit: a87a31d71ffead84fdbb00378984ece267ff3496
Parents: d262536
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Sep 21 20:26:18 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Sep 21 20:26:18 2016 -0700

----------------------------------------------------------------------
 apps/bletiny/pkg.yml |  3 +++
 apps/blinky/pkg.yml  |  4 ++++
 apps/slinky/pkg.yml  |  3 +++
 apps/splitty/pkg.yml | 11 ++++-------
 4 files changed, 14 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a87a31d7/apps/bletiny/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bletiny/pkg.yml b/apps/bletiny/pkg.yml
index 95f77db..465edc4 100644
--- a/apps/bletiny/pkg.yml
+++ b/apps/bletiny/pkg.yml
@@ -37,6 +37,9 @@ pkg.deps:
 ### 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:
+    # Enable the shell task.
+    SHELL_TASK: 1
+
     # Set log level to info (disable debug logging).
     LOG_LEVEL: 1
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a87a31d7/apps/blinky/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/blinky/pkg.yml b/apps/blinky/pkg.yml
index 64bbace..a63becb 100644
--- a/apps/blinky/pkg.yml
+++ b/apps/blinky/pkg.yml
@@ -32,3 +32,7 @@ pkg.deps:
     - sys/config
     - sys/log
     - sys/stats
+
+pkg.syscfg_vals:
+    # Enable the shell task.
+    SHELL_TASK: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a87a31d7/apps/slinky/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/slinky/pkg.yml b/apps/slinky/pkg.yml
index e46a86c..4e11759 100644
--- a/apps/slinky/pkg.yml
+++ b/apps/slinky/pkg.yml
@@ -41,4 +41,7 @@ pkg.deps:
     - sys/stats
 
 pkg.syscfg_vals:
+    # Enable the shell task.
+    SHELL_TASK: 1
+
     STATS_NAMES: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a87a31d7/apps/splitty/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/splitty/pkg.yml b/apps/splitty/pkg.yml
index 576c0c8..d436423 100644
--- a/apps/splitty/pkg.yml
+++ b/apps/splitty/pkg.yml
@@ -39,11 +39,8 @@ pkg.deps:
     - sys/log
     - sys/stats
 
-pkg.cflags:
-    - "-DSTATS_NAME_ENABLE=1"
+pkg.syscfg_vals:
+    # Enable the shell task.
+    SHELL_TASK: 1
 
-pkg.cflags.NFFS:
-    - "-DNFFS_PRESENT"
-
-pkg.cflags.FCB:
-    - "-DFCB_PRESENT"
+    STATS_NAMES: 1


[13/50] [abbrv] incubator-mynewt-core git commit: format logging so the library logs to mynewt logging system

Posted by vi...@apache.org.
format logging so the library logs to mynewt logging system


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

Branch: refs/heads/sterly_refactor
Commit: 7f682f6483d1efd4d638ebb8972bdb4b0eebf299
Parents: b402017
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Fri Sep 16 15:32:27 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Wed Sep 21 10:48:28 2016 -0700

----------------------------------------------------------------------
 libs/iotivity/src/port/mynewt/config.h     |  2 +
 libs/iotivity/src/port/mynewt/ip_adaptor.c | 66 ++++++++++++++-----------
 libs/iotivity/src/port/oc_log.h            | 11 +++--
 3 files changed, 48 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f682f64/libs/iotivity/src/port/mynewt/config.h
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/config.h b/libs/iotivity/src/port/mynewt/config.h
index c2602c4..10b4f95 100644
--- a/libs/iotivity/src/port/mynewt/config.h
+++ b/libs/iotivity/src/port/mynewt/config.h
@@ -5,6 +5,8 @@
 /* Time resolution */
 #include <stdint.h>
 #include <os/os.h>
+#include <log/log.h>
+extern struct log oc_log;
 
 typedef os_time_t oc_clock_time_t;
 #define OC_CLOCK_CONF_TICKS_PER_SECOND (OS_TICKS_PER_SEC)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f682f64/libs/iotivity/src/port/mynewt/ip_adaptor.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/ip_adaptor.c b/libs/iotivity/src/port/mynewt/ip_adaptor.c
index 0b6fd1a..dfb1f9c 100644
--- a/libs/iotivity/src/port/mynewt/ip_adaptor.c
+++ b/libs/iotivity/src/port/mynewt/ip_adaptor.c
@@ -58,7 +58,23 @@ struct os_sem oc_write_sem;
 #define MAX_CBMEM_BUF   (600)
 static uint32_t *cbmem_buf;
 static struct cbmem cbmem;
-static struct log oc_log;
+struct log oc_log;
+
+static int oc_log_init(void) {
+
+    log_init();
+
+    cbmem_buf = malloc(sizeof(uint32_t) * MAX_CBMEM_BUF);
+    if (cbmem_buf == NULL) {
+        return -1;
+    }
+
+    cbmem_init(&cbmem, cbmem_buf, MAX_CBMEM_BUF);
+    log_register("iot", &oc_log, &log_cbmem_handler, &cbmem);
+
+    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "OC Init");
+    return 0;
+}
 
 /* not sure if these semaphores are necessary yet.  If we are running
  * all of this from one task, we may not need these */
@@ -93,7 +109,7 @@ oc_send_buffer(oc_message_t *message)
     int rc;
 
     while (1) {
-        LOG_INFO(&oc_log, LOG_MODULE_DEFAULT,
+        LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY,
                  "attempt send buffer %u\n", message->length);
 
         to.msin6_len = sizeof(to);
@@ -112,7 +128,7 @@ oc_send_buffer(oc_message_t *message)
         rc = mn_sendto(send_sock, &m, (struct mn_sockaddr *) &to);
         /* TODO what to do if this fails, we can't keep the buffer */
         if (rc != 0) {
-            LOG_ERROR(&oc_log, LOG_MODULE_DEFAULT,
+            LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
                       "Failed sending buffer %u\n", message->length);
         } else {
             break;
@@ -130,7 +146,7 @@ oc_attempt_rx(struct mn_socket * rxsock) {
     oc_message_t *message = NULL;
     struct mn_sockaddr_in6 from;
 
-    LOG_DEBUG(&oc_log, LOG_MODULE_DEFAULT, "attempt rx from %u\n", rxsock);
+    LOG_DEBUG(&oc_log, LOG_MODULE_IOTIVITY, "attempt rx from %u\n", rxsock);
 
     rc= mn_recvfrom(rxsock, &m, (struct mn_sockaddr *) &from);
 
@@ -144,7 +160,7 @@ oc_attempt_rx(struct mn_socket * rxsock) {
 
     pkt = OS_MBUF_PKTHDR(m);
 
-    LOG_DEBUG(&oc_log, LOG_MODULE_DEFAULT,
+    LOG_DEBUG(&oc_log, LOG_MODULE_IOTIVITY,
               "rx from %u %p-%u\n", rxsock, pkt, pkt->omp_len);
 
     message = oc_allocate_message();
@@ -173,7 +189,7 @@ oc_attempt_rx(struct mn_socket * rxsock) {
     message->endpoint.ipv6_addr.scope = from.msin6_scope_id;
     message->endpoint.ipv6_addr.port = ntohs(from.msin6_port);
 
-    LOG_INFO(&oc_log, LOG_MODULE_DEFAULT, "rx from %u len %u\n",
+    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "rx from %u len %u\n",
              rxsock, message->length);
 
     return message;
@@ -245,21 +261,21 @@ oc_init_net_task(void) {
     /* start this thing running to check right away */
     rc = os_sem_init(&oc_read_sem, 1);
     if (0 != rc) {
-        LOG_ERROR(&oc_log, LOG_MODULE_DEFAULT,
+        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
                   "Could not initialize oc read sem\n");
         return rc;
     }
 
     rc = os_sem_init(&oc_write_sem, 1);
     if (0 != rc) {
-        LOG_ERROR(&oc_log, LOG_MODULE_DEFAULT,
+        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
                   "Could not initialize oc write sem\n");
         return rc;
     }
 
     oc_stack = (os_stack_t*) malloc(sizeof(os_stack_t)*OC_NET_TASK_STACK_SIZE);
     if (NULL == oc_stack) {
-        LOG_ERROR(&oc_log, LOG_MODULE_DEFAULT,
+        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
                   "Could not malloc oc stack\n");
         return -1;
     }
@@ -269,7 +285,7 @@ oc_init_net_task(void) {
             oc_stack, OC_NET_TASK_STACK_SIZE);
 
     if (rc != 0) {
-        LOG_ERROR(&oc_log, LOG_MODULE_DEFAULT, "Could not start oc task\n");
+        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY, "Could not start oc task\n");
         free(oc_stack);
     }
 
@@ -279,7 +295,7 @@ oc_init_net_task(void) {
 void
 oc_connectivity_shutdown(void)
 {
-    LOG_INFO(&oc_log, LOG_MODULE_DEFAULT, "OC shutdown");
+    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "OC shutdown");
 
     if (ucast) {
         mn_close(ucast);
@@ -299,28 +315,22 @@ oc_connectivity_init(void)
 
     memset(&itf, 0, sizeof(itf));
 
-    log_init();
-
-    cbmem_buf = malloc(sizeof(uint32_t) * MAX_CBMEM_BUF);
-    if (cbmem_buf == NULL) {
-        return -1;
-    }
-
-    cbmem_init(&cbmem, cbmem_buf, MAX_CBMEM_BUF);
-    log_register("iot", &oc_log, &log_cbmem_handler, &cbmem);
-
-    LOG_INFO(&oc_log, LOG_MODULE_DEFAULT, "OC Init");
+    rc = oc_log_init();
+    if ( rc != 0) {
+        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
+                  "Could not create oc logging\n");
+        return rc;    }
 
     rc = mn_socket(&ucast, MN_PF_INET6, MN_SOCK_DGRAM, 0);
     if ( rc != 0 || !ucast ) {
-        LOG_ERROR(&oc_log, LOG_MODULE_DEFAULT,
+        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
                   "Could not create oc unicast socket\n");
         return rc;
     }
     rc = mn_socket(&mcast, MN_PF_INET6, MN_SOCK_DGRAM, 0);
     if ( rc != 0 || !mcast ) {
         mn_close(ucast);
-        LOG_ERROR(&oc_log, LOG_MODULE_DEFAULT,
+        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
                   "Could not create oc multicast socket\n");
         return rc;
     }
@@ -335,7 +345,7 @@ oc_connectivity_init(void)
 
     rc = mn_bind(ucast, (struct mn_sockaddr *)&sin);
     if (rc != 0) {
-        LOG_ERROR(&oc_log, LOG_MODULE_DEFAULT,
+        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
                   "Could not bind oc unicast socket\n");
         goto oc_connectivity_init_err;
     }
@@ -359,19 +369,19 @@ oc_connectivity_init(void)
 
         rc = mn_setsockopt(mcast, MN_SO_LEVEL, MN_MCAST_JOIN_GROUP, &join);
         if (rc != 0) {
-            LOG_ERROR(&oc_log, LOG_MODULE_DEFAULT,
+            LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
                  "Could not join multicast group on %s\n", itf.mif_name);
             continue;
         }
 
-        LOG_INFO(&oc_log, LOG_MODULE_DEFAULT,
+        LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY,
                   "Joined Coap multicast grop on %s\n", itf.mif_name);
     }
 
     sin.msin6_port = htons(COAP_PORT_UNSECURED);
     rc = mn_bind(mcast, (struct mn_sockaddr *)&sin);
     if (rc != 0) {
-        LOG_ERROR(&oc_log, LOG_MODULE_DEFAULT,
+        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
                   "Could not bind oc multicast socket\n");
         goto oc_connectivity_init_err;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7f682f64/libs/iotivity/src/port/oc_log.h
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/oc_log.h b/libs/iotivity/src/port/oc_log.h
index a9676b9..c03233e 100644
--- a/libs/iotivity/src/port/oc_log.h
+++ b/libs/iotivity/src/port/oc_log.h
@@ -18,11 +18,14 @@
 #define OC_LOG_H
 
 #include <stdio.h>
+#include "mynewt/config.h"
 
 #define PRINT(...) printf(__VA_ARGS__)
 
 #define PRINTipaddr(endpoint)                                                  \
-  PRINT(                                                                       \
+{\
+  char tmp_buf[16*3+6]; /* 16 octets plus port */                              \
+  sprintf(tmp_buf,                                                             \
     "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%"    \
     "02x]:%d",                                                                 \
     ((endpoint).ipv6_addr.address)[0], ((endpoint).ipv6_addr.address)[1],      \
@@ -33,10 +36,12 @@
     ((endpoint).ipv6_addr.address)[10], ((endpoint).ipv6_addr.address)[11],    \
     ((endpoint).ipv6_addr.address)[12], ((endpoint).ipv6_addr.address)[13],    \
     ((endpoint).ipv6_addr.address)[14], ((endpoint).ipv6_addr.address)[15],    \
-    (endpoint).ipv6_addr.port)
+    (endpoint).ipv6_addr.port);                                                \
+    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "%s", tmp_buf);                     \
+}
 
 #if DEBUG
-#define LOG(...) PRINT(__VA_ARGS__)
+#define LOG(...) LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY,__VA_ARGS__)
 #define LOGipaddr(endpoint) PRINTipaddr(endpoint)
 #else
 #define LOG(...)


[37/50] [abbrv] incubator-mynewt-core git commit: console - Don't attempt write if uninitialized.

Posted by vi...@apache.org.
console - Don't attempt write if uninitialized.


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

Branch: refs/heads/sterly_refactor
Commit: a7833689ea50889dee3471f02056db8557f9ad12
Parents: 94737de
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Sep 22 16:20:53 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Thu Sep 22 16:29:16 2016 -0700

----------------------------------------------------------------------
 libs/console/full/src/cons_fmt.c | 8 ++++++++
 1 file changed, 8 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a7833689/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 2b64c01..9995322 100644
--- a/libs/console/full/src/cons_fmt.c
+++ b/libs/console/full/src/cons_fmt.c
@@ -41,6 +41,10 @@ console_printf(const char *fmt, ...)
 {
     va_list args;
 
+    if (!console_is_init()) {
+        return;
+    }
+
     /* Prefix each line with a timestamp. */
     if (!console_is_midline) {
         fprintf((FILE *)&console_file, "%lu:", (unsigned long)os_time_get());
@@ -60,6 +64,10 @@ console_printf(const char *fmt, ...)
     char buf[CONS_OUTPUT_MAX_LINE];
     int len;
 
+    if (!console_is_init()) {
+        return;
+    }
+
     /* Prefix each line with a timestamp. */
     if (!console_is_midline) {
         len = snprintf(buf, sizeof(buf), "%lu:", (unsigned long)os_time_get());


[28/50] [abbrv] incubator-mynewt-core git commit: Disable all BSP peripherals by default.

Posted by vi...@apache.org.
Disable all BSP peripherals by default.

If an app needs a peripheral, it should enable it in its syscfg.
Pulling in everything be default was adding too much code size.


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

Branch: refs/heads/sterly_refactor
Commit: 6c48ecd7f002a483232de90bd4481a85b9fa1aed
Parents: 8371baa
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Sep 21 20:06:19 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Sep 21 20:13:48 2016 -0700

----------------------------------------------------------------------
 hw/bsp/arduino_primo_nrf52/pkg.yml        | 4 ++--
 hw/bsp/bmd300eval/pkg.yml                 | 4 ++--
 hw/bsp/nrf51-arduino_101/pkg.yml          | 4 ++--
 hw/bsp/nrf51-blenano/pkg.yml              | 2 +-
 hw/bsp/nrf51dk-16kbram/pkg.yml            | 4 ++--
 hw/bsp/nrf52dk/pkg.yml                    | 4 ++--
 hw/bsp/nrf52dk/src/os_bsp.c               | 5 ++++-
 hw/bsp/olimex_stm32-e407_devboard/pkg.yml | 4 ++--
 8 files changed, 17 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6c48ecd7/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 724468b..843ad6c 100644
--- a/hw/bsp/arduino_primo_nrf52/pkg.yml
+++ b/hw/bsp/arduino_primo_nrf52/pkg.yml
@@ -70,7 +70,7 @@ pkg.syscfg_defs:
 
     ADC_0:
         description: 'TBD'
-        value:  1
+        value:  0
     ADC_0_RESOLUTION:
         description: 'TBD'
         value: 'SAADC_CONFIG_RESOLUTION'
@@ -115,7 +115,7 @@ pkg.syscfg_defs:
 
     SPI_MASTER:
         description: 'TBD'
-        value:  1
+        value:  0
     SPI_SLAVE:
         description: 'TBD'
         value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6c48ecd7/hw/bsp/bmd300eval/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/bmd300eval/pkg.yml b/hw/bsp/bmd300eval/pkg.yml
index cd1af60..8849cdb 100644
--- a/hw/bsp/bmd300eval/pkg.yml
+++ b/hw/bsp/bmd300eval/pkg.yml
@@ -69,7 +69,7 @@ pkg.syscfg_defs:
 
     ADC_0:
         description: 'TBD'
-        value:  1
+        value:  0
     ADC_0_RESOLUTION:
         description: 'TBD'
         value: 'SAADC_CONFIG_RESOLUTION'
@@ -89,7 +89,7 @@ pkg.syscfg_defs:
 
     SPI_MASTER:
         description: 'TBD'
-        value:  1
+        value:  0
     SPI_SLAVE:
         description: 'TBD'
         value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6c48ecd7/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 7719a9f..e451a2e 100644
--- a/hw/bsp/nrf51-arduino_101/pkg.yml
+++ b/hw/bsp/nrf51-arduino_101/pkg.yml
@@ -67,7 +67,7 @@ pkg.syscfg_defs:
 
     ADC_0:
         description: 'TBD'
-        value:  1
+        value:  0
     ADC_0_RESOLUTION:
         description: 'TBD'
         value: 'SAADC_CONFIG_RESOLUTION'
@@ -87,7 +87,7 @@ pkg.syscfg_defs:
 
     SPI_MASTER:
         description: 'TBD'
-        value:  1
+        value:  0
     SPI_SLAVE:
         description: 'TBD'
         value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6c48ecd7/hw/bsp/nrf51-blenano/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51-blenano/pkg.yml b/hw/bsp/nrf51-blenano/pkg.yml
index 494d265..55f3e4d 100644
--- a/hw/bsp/nrf51-blenano/pkg.yml
+++ b/hw/bsp/nrf51-blenano/pkg.yml
@@ -69,7 +69,7 @@ pkg.syscfg_defs:
 
     ADC_0:
         description: 'TBD'
-        value:  1
+        value:  0
     ADC_0_RESOLUTION:
         description: 'TBD'
         value: 'SAADC_CONFIG_RESOLUTION'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6c48ecd7/hw/bsp/nrf51dk-16kbram/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk-16kbram/pkg.yml b/hw/bsp/nrf51dk-16kbram/pkg.yml
index 6b65dfb..991c55c 100644
--- a/hw/bsp/nrf51dk-16kbram/pkg.yml
+++ b/hw/bsp/nrf51dk-16kbram/pkg.yml
@@ -68,7 +68,7 @@ pkg.syscfg_defs:
 
     ADC_0:
         description: 'TBD'
-        value:  1
+        value:  0
     ADC_0_RESOLUTION:
         description: 'TBD'
         value: 'SAADC_CONFIG_RESOLUTION'
@@ -88,7 +88,7 @@ pkg.syscfg_defs:
 
     SPI_MASTER:
         description: 'TBD'
-        value:  1
+        value:  0
     SPI_SLAVE:
         description: 'TBD'
         value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6c48ecd7/hw/bsp/nrf52dk/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf52dk/pkg.yml b/hw/bsp/nrf52dk/pkg.yml
index e80dea9..e3d81c0 100644
--- a/hw/bsp/nrf52dk/pkg.yml
+++ b/hw/bsp/nrf52dk/pkg.yml
@@ -69,7 +69,7 @@ pkg.syscfg_defs:
 
     ADC_0:
         description: 'TBD'
-        value:  1
+        value:  0
     ADC_0_RESOLUTION:
         description: 'TBD'
         value: 'SAADC_CONFIG_RESOLUTION'
@@ -102,7 +102,7 @@ pkg.syscfg_defs:
 
     SPI_MASTER:
         description: 'TBD'
-        value:  1
+        value:  0
     SPI_SLAVE:
         description: 'TBD'
         value:  0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6c48ecd7/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 90798fe..80f8bac 100644
--- a/hw/bsp/nrf52dk/src/os_bsp.c
+++ b/hw/bsp/nrf52dk/src/os_bsp.c
@@ -40,9 +40,12 @@
 #include "app_util_platform.h"
 #include "nrf.h"
 #include "app_error.h"
-#include "adc_nrf52/adc_nrf52.h"
 #include "nrf_drv_saadc.h"
 
+#if MYNEWT_VAL(ADC_0)
+#include "adc_nrf52/adc_nrf52.h"
+#endif
+
 static struct flash_area bsp_flash_areas[] = {
     [FLASH_AREA_BOOTLOADER] = {
         .fa_flash_id = 0,       /* internal flash */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6c48ecd7/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 1842a85..a06aaf0 100644
--- a/hw/bsp/olimex_stm32-e407_devboard/pkg.yml
+++ b/hw/bsp/olimex_stm32-e407_devboard/pkg.yml
@@ -57,7 +57,7 @@ pkg.syscfg_defs:
 
     ADC_0:
         description: 'TBD'
-        value:  1
+        value:  0
     ADC_0_RESOLUTION:
         description: 'TBD'
         value: 'SAADC_CONFIG_RESOLUTION'
@@ -77,7 +77,7 @@ pkg.syscfg_defs:
 
     SPI_MASTER:
         description: 'TBD'
-        value:  1
+        value:  0
     SPI_SLAVE:
         description: 'TBD'
         value:  0


[27/50] [abbrv] incubator-mynewt-core git commit: Config - Allow neither fcb nor nffs

Posted by vi...@apache.org.
Config - Allow neither fcb nor nffs


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

Branch: refs/heads/sterly_refactor
Commit: d2625362a88dfdc3eae781c566d63cc0cfaa1a88
Parents: 6547b7d
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Sep 21 20:11:07 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Sep 21 20:13:48 2016 -0700

----------------------------------------------------------------------
 sys/config/pkg.yml           | 2 +-
 sys/config/src/config_init.c | 2 --
 2 files changed, 1 insertion(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d2625362/sys/config/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/config/pkg.yml b/sys/config/pkg.yml
index e005530..2d7c83d 100644
--- a/sys/config/pkg.yml
+++ b/sys/config/pkg.yml
@@ -41,7 +41,7 @@ pkg.init_stage: 5
 pkg.syscfg_defs:
     CONFIG_FCB:
         description: 'TBD'
-        value: 1
+        value: 0
     CONFIG_FCB_MAGIC:
         description: 'TBD'
         value: 0xc09f6e5e

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d2625362/sys/config/src/config_init.c
----------------------------------------------------------------------
diff --git a/sys/config/src/config_init.c b/sys/config/src/config_init.c
index 0b3a503..13a5b95 100644
--- a/sys/config/src/config_init.c
+++ b/sys/config/src/config_init.c
@@ -88,8 +88,6 @@ config_init_fcb(void)
     SYSINIT_PANIC_ASSERT(rc == 0);
 }
 
-#else
-#error "Need NFFS or FCB for config storage"
 #endif
 
 void


[50/50] [abbrv] incubator-mynewt-core git commit: No ticket-Adding bsp/sysid and config to blepeprh

Posted by vi...@apache.org.
No ticket-Adding bsp/sysid and config to blepeprh

- Add bsp/sysid dependency and enable config in bleperph app.


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

Branch: refs/heads/develop
Commit: fda3dd6f39f3675e354586d0f7c9f794ca3b7c73
Parents: ec1af7e
Author: Vipul Rahane <vi...@apache.org>
Authored: Mon Oct 10 12:24:12 2016 -0700
Committer: Vipul Rahane <vi...@apache.org>
Committed: Mon Oct 10 12:24:12 2016 -0700

----------------------------------------------------------------------
 apps/bleprph/pkg.yml    | 1 +
 apps/bleprph/syscfg.yml | 3 +++
 2 files changed, 4 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fda3dd6f/apps/bleprph/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bleprph/pkg.yml b/apps/bleprph/pkg.yml
index 72b8b31..4773ef9 100644
--- a/apps/bleprph/pkg.yml
+++ b/apps/bleprph/pkg.yml
@@ -36,3 +36,4 @@ pkg.deps:
     - mgmt/newtmgr
     - mgmt/newtmgr/transport/ble
     - sys/sysinit
+    - sys/id

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/fda3dd6f/apps/bleprph/syscfg.yml
----------------------------------------------------------------------
diff --git a/apps/bleprph/syscfg.yml b/apps/bleprph/syscfg.yml
index efccca0..738dfb2 100644
--- a/apps/bleprph/syscfg.yml
+++ b/apps/bleprph/syscfg.yml
@@ -21,3 +21,6 @@ syscfg.vals:
     # Enable newtmgr commands.
     STATS_NEWTMGR: 1
     LOG_NEWTMGR: 1
+
+    # Enable Config.
+    CONFIG_NEWTMGR: 1


[14/50] [abbrv] incubator-mynewt-core git commit: working client and server after refactoring the adaptor to allow for serial and gatt interfaces

Posted by vi...@apache.org.
working client and server after refactoring the adaptor to allow
for serial and gatt interfaces


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

Branch: refs/heads/sterly_refactor
Commit: 12ef82c64f072d79bcffd417ad803b503dac6431
Parents: 29a11be
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Wed Sep 21 10:47:56 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Wed Sep 21 10:48:28 2016 -0700

----------------------------------------------------------------------
 apps/ocf_sample/pkg.yml                        |   8 +-
 apps/ocf_sample/src/main.c                     | 141 ++++++++++--
 libs/iotivity/pkg.yml                          |   2 +-
 libs/iotivity/src/api/oc_ri.c                  |   2 +-
 libs/iotivity/src/port/mynewt/adaptor.c        | 223 +++++++++++++++++++
 libs/iotivity/src/port/mynewt/adaptor.h        |  61 +++++
 libs/iotivity/src/port/mynewt/ble_adaptor.c    |  18 ++
 libs/iotivity/src/port/mynewt/config.h         |   2 +
 libs/iotivity/src/port/mynewt/ip_adaptor.c     | 234 ++++++++------------
 libs/iotivity/src/port/mynewt/serial_adaptor.c | 153 +++++++++++++
 libs/iotivity/src/port/oc_connectivity.h       |   3 +-
 libs/os/include/os/os_eventq.h                 |   2 +-
 12 files changed, 685 insertions(+), 164 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/apps/ocf_sample/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/pkg.yml b/apps/ocf_sample/pkg.yml
index 32c96f2..52858b5 100644
--- a/apps/ocf_sample/pkg.yml
+++ b/apps/ocf_sample/pkg.yml
@@ -33,4 +33,10 @@ pkg.deps:
     - libs/iotivity
 
 # this tells the library that you intend to suppor the server functionality
-pkg.cflags: -DOC_SERVER
\ No newline at end of file
+pkg.cflags:
+
+#-DOC_SERVER -- build the server examples 
+#-DOC_CLIENT -- build the client examples
+#-DOC_TRANSPORT_GATT -- to send COAP over GATT
+#-DOC_TRANSPORT_SERIAL -- to send COAP over serial
+#-DOC_TRANSPORT_IP -- to send COAP over IP ��
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/apps/ocf_sample/src/main.c
----------------------------------------------------------------------
diff --git a/apps/ocf_sample/src/main.c b/apps/ocf_sample/src/main.c
index 4d90495..78846d2 100644
--- a/apps/ocf_sample/src/main.c
+++ b/apps/ocf_sample/src/main.c
@@ -28,7 +28,7 @@
 #include "mn_socket/arch/sim/native_sock.h"
 
 /* Shell */
-#define SHELL_TASK_PRIO      (8)
+#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];
@@ -38,8 +38,6 @@ static os_stack_t shell_stack[SHELL_TASK_STACK_SIZE];
 static os_stack_t ocf_stack[OCF_TASK_STACK_SIZE];
 struct os_task ocf_task;
 
-static bool light_state = false;
-
 #define DEFAULT_MBUF_MPOOL_BUF_LEN (256)
 #define DEFAULT_MBUF_MPOOL_NBUFS (10)
 
@@ -49,6 +47,13 @@ static uint8_t default_mbuf_mpool_data[DEFAULT_MBUF_MPOOL_BUF_LEN *
 static struct os_mbuf_pool default_mbuf_pool;
 static struct os_mempool default_mbuf_mpool;
 
+#ifdef OC_CLIENT
+static void issue_requests(void);
+#endif
+
+#ifdef OC_SERVER
+static bool light_state = false;
+
 static void
 get_light(oc_request_t *request, oc_interface_mask_t interface)
 {
@@ -93,13 +98,6 @@ put_light(oc_request_t *request, oc_interface_mask_t interface)
 }
 
 static void
-app_init(void)
-{
-  oc_init_platform("Mynewt", NULL, NULL);
-  oc_add_device("/oic/d", "oic.d.light", "MynewtLED", "1.0", "1.0", NULL, NULL);
-}
-
-static void
 register_resources(void)
 {
   oc_resource_t *res = oc_new_resource("/light/1", 1, 0);
@@ -107,22 +105,131 @@ register_resources(void)
   oc_resource_bind_resource_interface(res, OC_IF_RW);
   oc_resource_set_default_interface(res, OC_IF_RW);
 
-#ifdef OC_SECURITY
-  oc_resource_make_secure(res);
-#endif
-
   oc_resource_set_discoverable(res);
   oc_resource_set_periodic_observable(res, 1);
   oc_resource_set_request_handler(res, OC_GET, get_light);
   oc_resource_set_request_handler(res, OC_PUT, put_light);
   oc_add_resource(res);
 }
+#endif
 
-struct os_sem ocf_main_loop_sem;
+#ifdef OC_CLIENT
+#define MAX_URI_LENGTH (30)
+static char light_1[MAX_URI_LENGTH];
+static oc_server_handle_t light_server;
+static bool light_state = false;
+
+static void
+set_device_custom_property(void *data)
+{
+  oc_set_custom_device_property(purpose, "operate mynewt-light");
+}
+
+static oc_event_callback_retval_t
+stop_observe(void *data)
+{
+  PRINT("Stopping OBSERVE\n");
+  oc_stop_observe(light_1, &light_server);
+  return DONE;
+}
+
+static void
+put_light(oc_client_response_t *data)
+{
+  PRINT("PUT_light:\n");
+  if (data->code == OC_STATUS_CHANGED)
+    PRINT("PUT response OK\n");
+  else
+    PRINT("PUT response code %d\n", data->code);
+}
+
+static void
+observe_light(oc_client_response_t *data)
+{
+  PRINT("OBSERVE_light:\n");
+  oc_rep_t *rep = data->payload;
+  while (rep != NULL) {
+    PRINT("key %s, value ", oc_string(rep->name));
+    switch (rep->type) {
+    case BOOL:
+      PRINT("%d\n", rep->value_boolean);
+      light_state = rep->value_boolean;
+      break;
+    default:
+      break;
+    }
+    rep = rep->next;
+  }
+
+  if (oc_init_put(light_1, &light_server, NULL, &put_light, LOW_QOS)) {
+    oc_rep_start_root_object();
+    oc_rep_set_boolean(root, state, !light_state);
+    oc_rep_end_root_object();
+    if (oc_do_put())
+      PRINT("Sent PUT request\n");
+    else
+      PRINT("Could not send PUT\n");
+  } else
+    PRINT("Could not init PUT\n");
+}
+
+static oc_discovery_flags_t
+discovery(const char *di, const char *uri, oc_string_array_t types,
+          oc_interface_mask_t interfaces, oc_server_handle_t *server)
+{
+  int i;
+  int uri_len = strlen(uri);
+  uri_len = (uri_len >= MAX_URI_LENGTH) ? MAX_URI_LENGTH - 1 : uri_len;
+
+  for (i = 0; i < oc_string_array_get_allocated_size(types); i++) {
+    char *t = oc_string_array_get_item(types, i);
+    if (strlen(t) == 11 && strncmp(t, "oic.r.light", 11) == 0) {
+      memcpy(&light_server, server, sizeof(oc_server_handle_t));
+
+      strncpy(light_1, uri, uri_len);
+      light_1[uri_len] = '\0';
+
+      oc_do_observe(light_1, &light_server, NULL, &observe_light, LOW_QOS);
+      oc_set_delayed_callback(NULL, &stop_observe, 30);
+      return OC_STOP_DISCOVERY;
+    }
+  }
+  return OC_CONTINUE_DISCOVERY;
+}
+
+static void
+issue_requests(void)
+{
+  oc_do_ip_discovery("oic.r.light", &discovery);
+}
+
+#endif
+
+static void
+app_init(void)
+{
+  oc_init_platform("Mynewt", NULL, NULL);
+#ifdef OC_CLIENT
+  oc_add_device("/oic/d", "oic.d.phone", "MynewtClient", "1.0", "1.0",
+                set_device_custom_property, NULL);
+#endif
+
+#ifdef OC_SERVER
+    oc_add_device("/oic/d", "oic.d.light", "MynewtServer", "1.0", "1.0", NULL, NULL);
+#endif
+}
 
 
  oc_handler_t ocf_handler = {.init = app_init,
-                          .register_resources = register_resources };
+#ifdef OC_SERVER
+                          .register_resources = register_resources,
+#endif
+#ifdef OC_CLIENT
+                          .requests_entry = issue_requests,
+#endif
+ };
+
+struct os_sem ocf_main_loop_sem;
 
  void
 oc_signal_main_loop(void) {
@@ -184,8 +291,10 @@ main(int argc, char **argv)
                          SHELL_MAX_INPUT_LEN);
     assert(rc == 0);
 
+#ifdef OC_TRANSPORT_IP
     rc = native_sock_init();
     assert(rc == 0);
+#endif
 
     ocf_task_init();
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/libs/iotivity/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/iotivity/pkg.yml b/libs/iotivity/pkg.yml
index 87b4b45..f837bf4 100644
--- a/libs/iotivity/pkg.yml
+++ b/libs/iotivity/pkg.yml
@@ -29,7 +29,7 @@ pkg.deps:
     - "@apache-mynewt-core/sys/mn_socket"
     - "@apache-mynewt-core/sys/log"
 
-# remove debug option to save logging 
+# remove debug option to save logging
 pkg.cflags: -std=c99
         -DDEBUG=1
         -DSECURE=0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/libs/iotivity/src/api/oc_ri.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/api/oc_ri.c b/libs/iotivity/src/api/oc_ri.c
index 76d7ed2..7a6df95 100644
--- a/libs/iotivity/src/api/oc_ri.c
+++ b/libs/iotivity/src/api/oc_ri.c
@@ -895,7 +895,7 @@ oc_ri_invoke_client_cb(void *response, oc_endpoint_t *endpoint)
           break;
         }
       }
-      coap_get_header_observe(pkt, &client_response.observe_option);
+      coap_get_header_observe(pkt, (uint32_t*)&client_response.observe_option);
 
       bool separate = false;
       /*

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/libs/iotivity/src/port/mynewt/adaptor.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/adaptor.c b/libs/iotivity/src/port/mynewt/adaptor.c
new file mode 100644
index 0000000..2a80f4c
--- /dev/null
+++ b/libs/iotivity/src/port/mynewt/adaptor.c
@@ -0,0 +1,223 @@
+/**
+ * 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 <os/os.h>
+#include <os/endian.h>
+#include <string.h>
+#include <log/log.h>
+#include "../oc_network_events_mutex.h"
+#include "../oc_connectivity.h"
+#include "oc_buffer.h"
+#include "../oc_log.h"
+#include "adaptor.h"
+
+struct os_eventq oc_event_q;
+
+/* not sure if these semaphores are necessary yet.  If we are running
+ * all of this from one task, we may not need these */
+static struct os_mutex oc_net_mutex;
+
+void
+oc_network_event_handler_mutex_init(void)
+{
+    os_error_t rc;
+    rc = os_mutex_init(&oc_net_mutex);
+    assert(rc == 0);
+}
+
+void
+oc_network_event_handler_mutex_lock(void)
+{
+    os_mutex_pend(&oc_net_mutex, OS_TIMEOUT_NEVER);
+}
+
+void
+oc_network_event_handler_mutex_unlock(void)
+{
+    os_mutex_release(&oc_net_mutex);
+}
+
+/* need a task to process OCF messages */
+#define OC_NET_TASK_STACK_SIZE          OS_STACK_ALIGN(300)
+#define OC_NET_TASK_PRIORITY            (4)
+struct os_task oc_task;
+os_stack_t *oc_stack;
+
+void
+oc_send_buffer(oc_message_t *message) {
+
+    switch (message->endpoint.flags)
+    {
+#ifdef OC_TRANSPORT_IP
+        case IP:
+            oc_send_buffer_ip(message);
+            break;
+#endif
+#ifdef OC_TRANSPORT_GATT
+        case GATT:
+            oc_send_buffer_gatt(message);
+            break;
+#endif
+#ifdef OC_TRANSPORT_SERIAL
+        case SERIAL:
+            oc_send_buffer_serial(message);
+            break;
+#endif
+        default:
+            ERROR("Unknown transport option %u\n", message->endpoint.flags);
+            oc_message_unref(message);
+    }
+}
+
+void oc_send_multicast_message(oc_message_t *message)
+{
+
+    /* send on all the transports.  Don't forget to reference the message
+     * so it doesn't get deleted  */
+
+#ifdef OC_TRANSPORT_IP
+    oc_send_buffer_ip_mcast(message);
+#endif
+
+#ifdef OC_TRANSPORT_GATT
+    /* no multicast for GATT, just send unicast */
+    oc_message_add_ref(message);
+    oc_send_buffer_gatt(message);
+#endif
+
+#ifdef OC_TRANSPORT_SERIAL
+    /* no multi-cast for serial.  just send unicast */
+    oc_message_add_ref(message);
+    oc_send_buffer_serial(message);
+#endif
+}
+
+/* send all the entries to the OCF stack through the same task */
+void
+oc_task_handler(void *arg) {
+    while (1) {
+        oc_message_t *pmsg;
+        (void) pmsg;    /* to avoid unused */
+        struct os_event *evt = os_eventq_get(&oc_event_q);
+
+        switch(evt->ev_type) {
+
+#ifdef OC_TRANSPORT_IP
+            case OC_ADATOR_EVENT_IP:
+                while ((pmsg = oc_attempt_rx_ip()) != NULL) {
+                    oc_network_event(pmsg);
+                }
+                break;
+#endif
+
+#ifdef OC_TRANSPORT_SERIAL
+            case OC_ADATOR_EVENT_SERIAL:
+                while ((pmsg = oc_attempt_rx_serial()) != NULL) {
+                    oc_network_event(pmsg);
+                }
+                break;
+#endif
+
+#ifdef OC_TRANSPORT_GATT
+            case OC_ADATOR_EVENT_GATT:
+                while ((pmsg = oc_attempt_rx_gatt()) != NULL) {
+                    oc_network_event(pmsg);
+                }
+                break;
+#endif
+            default:
+                ERROR("oc_task_handler: Unidentified event %d\n", evt->ev_type);
+
+        }
+    }
+}
+
+static int
+oc_init_task(void) {
+    int rc;
+
+    os_eventq_init(&oc_event_q);
+
+    oc_stack = (os_stack_t*) malloc(sizeof(os_stack_t)*OC_NET_TASK_STACK_SIZE);
+    if (NULL == oc_stack) {
+        ERROR("Could not malloc oc stack\n");
+        return -1;
+    }
+
+    rc = os_task_init(&oc_task, "oc", oc_task_handler, NULL,
+            OC_NET_TASK_PRIORITY, OS_WAIT_FOREVER,
+            oc_stack, OC_NET_TASK_STACK_SIZE);
+
+    if (rc != 0) {
+        ERROR("Could not start oc task\n");
+        free(oc_stack);
+    }
+
+    return rc;
+}
+
+void
+oc_connectivity_shutdown(void)
+{
+#ifdef OC_TRANSPORT_IP
+    oc_connectivity_shutdown_ip();
+#endif
+#ifdef OC_TRANSPORT_SERIAL
+    oc_connectivity_shutdown_serial();
+#endif
+#ifdef OC_TRANSPORT_GATT
+    oc_connectivity_shutdown_gatt();
+#endif
+}
+
+int
+oc_connectivity_init(void)
+{
+    int rc;
+
+#ifdef OC_TRANSPORT_IP
+    rc = oc_connectivity_init_ip();
+    if (rc != 0) {
+        goto oc_connectivity_init_err;
+    }
+#endif
+#ifdef OC_TRANSPORT_SERIAL
+    rc = oc_connectivity_init_serial();
+    if (rc != 0) {
+        goto oc_connectivity_init_err;
+    }
+#endif
+#ifdef OC_TRANSPORT_GATT
+    rc = oc_connectivity_init_gatt();
+    if (rc != 0) {
+        goto oc_connectivity_init_err;
+    }
+#endif
+
+    rc = oc_init_task();
+    if (rc != 0) {
+        goto oc_connectivity_init_err;
+    }
+
+    return 0;
+
+oc_connectivity_init_err:
+    oc_connectivity_shutdown();
+    return rc;
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/libs/iotivity/src/port/mynewt/adaptor.h
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/adaptor.h b/libs/iotivity/src/port/mynewt/adaptor.h
new file mode 100644
index 0000000..431c7e9
--- /dev/null
+++ b/libs/iotivity/src/port/mynewt/adaptor.h
@@ -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.
+ */
+
+#ifndef ADAPTOR_H
+#define ADAPTOR_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define OC_ADATOR_EVENT_IP      (OS_EVENT_T_PERUSER + 0)
+#define OC_ADATOR_EVENT_SERIAL  (OS_EVENT_T_PERUSER + 1)
+#define OC_ADATOR_EVENT_GATT    (OS_EVENT_T_PERUSER + 2)
+
+extern struct os_eventq oc_event_q;
+
+
+#ifdef OC_TRANSPORT_IP
+int oc_connectivity_init_ip(void);
+void oc_connectivity_shutdown_ip(void);
+void oc_send_buffer_ip(oc_message_t *message);
+void oc_send_buffer_ip_mcast(oc_message_t *message);
+oc_message_t *oc_attempt_rx_ip(void);
+#endif
+
+#ifdef OC_TRANSPORT_GATT
+int oc_connectivity_init_gatt(void);
+void oc_connectivity_shutdown_gatt(void);
+void oc_send_buffer_gatt(oc_message_t *message);
+oc_message_t *oc_attempt_rx_gatt(void);
+#endif
+
+#ifdef OC_TRANSPORT_SERIAL
+int oc_connectivity_init_serial(void);
+void oc_connectivity_shutdown_serial(void);
+void oc_send_buffer_serial(oc_message_t *message);
+oc_message_t *oc_attempt_rx_serial(void);
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ADAPTOR_H */
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/libs/iotivity/src/port/mynewt/ble_adaptor.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/ble_adaptor.c b/libs/iotivity/src/port/mynewt/ble_adaptor.c
new file mode 100644
index 0000000..f736fe1
--- /dev/null
+++ b/libs/iotivity/src/port/mynewt/ble_adaptor.c
@@ -0,0 +1,18 @@
+/**
+ * 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.
+ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/libs/iotivity/src/port/mynewt/config.h
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/config.h b/libs/iotivity/src/port/mynewt/config.h
index 10b4f95..83e19de 100644
--- a/libs/iotivity/src/port/mynewt/config.h
+++ b/libs/iotivity/src/port/mynewt/config.h
@@ -54,4 +54,6 @@ typedef os_time_t oc_clock_time_t;
 /* Max inactivity timeout before tearing down DTLS connection */
 //#define DTLS_INACTIVITY_TIMEOUT (10)
 
+#define MYNEWT_OCF_SERIAL_PORT  (1)
+
 #endif /* CONFIG_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/libs/iotivity/src/port/mynewt/ip_adaptor.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/ip_adaptor.c b/libs/iotivity/src/port/mynewt/ip_adaptor.c
index 9b8bbff..8ae67e4 100644
--- a/libs/iotivity/src/port/mynewt/ip_adaptor.c
+++ b/libs/iotivity/src/port/mynewt/ip_adaptor.c
@@ -24,15 +24,22 @@
 #include <log/log.h>
 #include <mn_socket/mn_socket.h>
 
-#include "../oc_network_events_mutex.h"
 #include "../oc_connectivity.h"
 #include "oc_buffer.h"
 #include "../oc_log.h"
+#include "adaptor.h"
+
+#ifdef OC_TRANSPORT_IP
+
+struct os_event oc_sock_read_event = {
+    .ev_type = OC_ADATOR_EVENT_IP,
+};
 
 #ifdef OC_SECURITY
 #error This implementation does not yet support security
 #endif
 
+
 #define COAP_PORT_UNSECURED (5683)
 /* TODO use inet_pton when its available */
 const struct mn_in6_addr coap_all_nodes_v6 = {
@@ -40,89 +47,86 @@ const struct mn_in6_addr coap_all_nodes_v6 = {
                0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFD}
 };
 
-/* need a task to process OCF messages */
-#define OC_NET_TASK_STACK_SIZE          OS_STACK_ALIGN(300)
-#define OC_NET_TASK_PRIORITY            (4)
-struct os_task oc_task;
-os_stack_t *oc_stack;
 
 /* sockets to use for coap unicast and multicast */
-struct mn_socket *mcast;
 struct mn_socket *ucast;
 
-/* to wake our task when stuff is ready */
-struct os_sem oc_read_sem;
-struct os_sem oc_write_sem;
-
-/* not sure if these semaphores are necessary yet.  If we are running
- * all of this from one task, we may not need these */
-static struct os_mutex oc_net_mutex;
-
-void
-oc_network_event_handler_mutex_init(void)
-{
-    os_error_t rc;
-    rc = os_mutex_init(&oc_net_mutex);
-    assert(rc == 0);
-}
-
-void
-oc_network_event_handler_mutex_lock(void)
-{
-    os_mutex_pend(&oc_net_mutex, OS_TIMEOUT_NEVER);
-}
-
-void
-oc_network_event_handler_mutex_unlock(void)
-{
-    os_mutex_release(&oc_net_mutex);
-}
+#ifdef OC_SERVER
+struct mn_socket *mcast;
+#endif
 
-void
-oc_send_buffer(oc_message_t *message)
+static void
+oc_send_buffer_ip_int(oc_message_t *message, int mcast)
 {
     struct mn_sockaddr_in6 to;
-    struct mn_socket * send_sock;
     struct os_mbuf m;
     int rc;
 
-    while (1) {
-        LOG("attempt send buffer %lu\n", message->length);
-
-        to.msin6_len = sizeof(to);
-        to.msin6_family = MN_AF_INET6;
-        to.msin6_scope_id = message->endpoint.ipv6_addr.scope;
-        to.msin6_port = htons(message->endpoint.ipv6_addr.port);
-        memcpy(&to.msin6_addr, message->endpoint.ipv6_addr.address,
-               sizeof(to.msin6_addr));
-        send_sock = ucast;
-
-        /* put on an mbuf header to make the socket happy */
-        memset(&m,0, sizeof(m));
-        m.om_data = message->data;
-        m.om_len = message->length;
-
-        rc = mn_sendto(send_sock, &m, (struct mn_sockaddr *) &to);
-        /* TODO what to do if this fails, we can't keep the buffer */
+    LOG("oc_transport_ip attempt send buffer %lu\n", message->length);
+
+    to.msin6_len = sizeof(to);
+    to.msin6_family = MN_AF_INET6;
+
+    to.msin6_port = htons(message->endpoint.ipv6_addr.port);
+    memcpy(&to.msin6_addr, message->endpoint.ipv6_addr.address,
+           sizeof(to.msin6_addr));
+
+    /* put on an mbuf header to make the socket happy */
+    memset(&m,0, sizeof(m));
+    m.om_data = message->data;
+    m.om_len = message->length;
+    to.msin6_scope_id = message->endpoint.ipv6_addr.scope;
+
+    if (mcast) {
+        struct mn_itf itf;
+        memset(&itf, 0, sizeof(itf));
+
+        while (1) {
+            rc = mn_itf_getnext(&itf);
+            if (rc) {
+                break;
+            }
+
+            if (0 == (itf.mif_flags & MN_ITF_F_UP)) {
+                continue;
+            }
+
+            to.msin6_scope_id = itf.mif_idx;
+
+            rc = mn_sendto(ucast, &m, (struct mn_sockaddr *) &to);
+            if (rc != 0) {
+                ERROR("Failed sending buffer %lu on itf %d\n",
+                      message->length, to.msin6_scope_id);
+            }
+        }
+    } else {
+        rc = mn_sendto(ucast, &m, (struct mn_sockaddr *) &to);
         if (rc != 0) {
-            ERROR("Failed sending buffer %lu\n", message->length);
-        } else {
-            break;
+            ERROR("Failed sending buffer %lu on itf %d\n",
+                  message->length, to.msin6_scope_id);
         }
-        /* if we failed to write, wait around until we can */
-        os_sem_pend(&oc_write_sem, OS_TIMEOUT_NEVER);
     }
+    oc_message_unref(message);
+}
+
+void
+oc_send_buffer_ip(oc_message_t *message) {
+    oc_send_buffer_ip_int(message, 0);
+}
+void
+oc_send_buffer_ip_mcast(oc_message_t *message) {
+    oc_send_buffer_ip_int(message, 1);
 }
 
 oc_message_t *
-oc_attempt_rx(struct mn_socket * rxsock) {
+oc_attempt_rx_ip_sock(struct mn_socket * rxsock) {
     int rc;
     struct os_mbuf *m = NULL;
     struct os_mbuf_pkthdr *pkt;
     oc_message_t *message = NULL;
     struct mn_sockaddr_in6 from;
 
-    LOG("attempt rx from %p\n", rxsock);
+    LOG("oc_transport_ip attempt rx from %p\n", rxsock);
 
     rc= mn_recvfrom(rxsock, &m, (struct mn_sockaddr *) &from);
 
@@ -181,108 +185,55 @@ rx_attempt_err:
 }
 
 oc_message_t *
-oc_attempt_rx_mcast(void) {
-    return oc_attempt_rx(mcast);
-}
-
-oc_message_t *
-oc_attempt_rx_ucast(void) {
-    return oc_attempt_rx(ucast);
+oc_attempt_rx_ip(void) {
+    oc_message_t *pmsg;
+    pmsg = oc_attempt_rx_ip_sock(ucast);
+#ifdef OC_SERVER
+    if (pmsg == NULL ) {
+        pmsg = oc_attempt_rx_ip_sock(mcast);
+    }
+#endif
+    return pmsg;
 }
 
 static void oc_socks_readable(void *cb_arg, int err);
-static void oc_socks_writable(void *cb_arg, int err);
 
 union mn_socket_cb oc_sock_cbs = {
     .socket.readable = oc_socks_readable,
-    .socket.writable = oc_socks_writable
+    .socket.writable = NULL
 };
 
 void
 oc_socks_readable(void *cb_arg, int err)
 {
-    os_sem_release(&oc_read_sem);
-}
-
-void
-oc_socks_writable(void *cb_arg, int err)
-{
-    os_sem_release(&oc_write_sem);
-}
-
-void
-oc_task_handler(void *arg) {
-    while (1) {
-        oc_message_t *pmsg;
-        os_sem_pend(&oc_read_sem, OS_TIMEOUT_NEVER);
-        pmsg = oc_attempt_rx_ucast();
-        if (pmsg) {
-            oc_network_event(pmsg);
-        }
-
-        pmsg = oc_attempt_rx_mcast();
-        if (pmsg) {
-            oc_network_event(pmsg);
-        }
-    }
-}
-
-static int
-oc_init_net_task(void) {
-    int rc;
-
-    /* start this thing running to check right away */
-    rc = os_sem_init(&oc_read_sem, 1);
-    if (0 != rc) {
-        ERROR("Could not initialize oc read sem\n");
-        return rc;
-    }
-
-    rc = os_sem_init(&oc_write_sem, 1);
-    if (0 != rc) {
-        ERROR("Could not initialize oc write sem\n");
-        return rc;
-    }
-
-    oc_stack = (os_stack_t*) malloc(sizeof(os_stack_t)*OC_NET_TASK_STACK_SIZE);
-    if (NULL == oc_stack) {
-        ERROR("Could not malloc oc stack\n");
-        return -1;
-    }
-
-    rc = os_task_init(&oc_task, "oc", oc_task_handler, NULL,
-            OC_NET_TASK_PRIORITY, OS_WAIT_FOREVER,
-            oc_stack, OC_NET_TASK_STACK_SIZE);
-
-    if (rc != 0) {
-        ERROR("Could not start oc task\n");
-        free(oc_stack);
-    }
-
-    return rc;
+    os_eventq_put(&oc_event_q, &oc_sock_read_event);
 }
 
 void
-oc_connectivity_shutdown(void)
+oc_connectivity_shutdown_ip(void)
 {
-    LOG("OC shutdown");
+    LOG("OC shutdown IP\n");
 
     if (ucast) {
         mn_close(ucast);
     }
 
+#ifdef OC_SERVER
     if (mcast) {
         mn_close(mcast);
     }
+#endif
+
 }
 
 int
-oc_connectivity_init(void)
+oc_connectivity_init_ip(void)
 {
     int rc;
     struct mn_sockaddr_in6 sin;
     struct mn_itf itf;
 
+    LOG("OC transport init IP\n");
     memset(&itf, 0, sizeof(itf));
 
     rc = oc_log_init();
@@ -295,14 +246,17 @@ oc_connectivity_init(void)
         ERROR("Could not create oc unicast socket\n");
         return rc;
     }
+    mn_socket_set_cbs(ucast, ucast, &oc_sock_cbs);
+
+#ifdef OC_SERVER
     rc = mn_socket(&mcast, MN_PF_INET6, MN_SOCK_DGRAM, 0);
     if ( rc != 0 || !mcast ) {
         mn_close(ucast);
         ERROR("Could not create oc multicast socket\n");
         return rc;
     }
-    mn_socket_set_cbs(ucast, ucast, &oc_sock_cbs);
     mn_socket_set_cbs(mcast, mcast, &oc_sock_cbs);
+#endif
 
     sin.msin6_len = sizeof(sin);
     sin.msin6_family = MN_AF_INET6;
@@ -316,6 +270,7 @@ oc_connectivity_init(void)
         goto oc_connectivity_init_err;
     }
 
+#ifdef OC_SERVER
     /* Set socket option to join multicast group on all valid interfaces */
     while (1) {
         struct mn_mreq join;
@@ -348,11 +303,7 @@ oc_connectivity_init(void)
         ERROR("Could not bind oc multicast socket\n");
         goto oc_connectivity_init_err;
     }
-
-    rc = oc_init_net_task();
-    if (rc != 0) {
-        goto oc_connectivity_init_err;
-    }
+#endif
 
     return 0;
 
@@ -361,7 +312,4 @@ oc_connectivity_init_err:
     return rc;
 }
 
-void oc_send_multicast_message(oc_message_t *message)
-{
-    oc_send_buffer(message);
-}
\ No newline at end of file
+#endif
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/libs/iotivity/src/port/mynewt/serial_adaptor.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/serial_adaptor.c b/libs/iotivity/src/port/mynewt/serial_adaptor.c
new file mode 100644
index 0000000..ddca3bb
--- /dev/null
+++ b/libs/iotivity/src/port/mynewt/serial_adaptor.c
@@ -0,0 +1,153 @@
+/**
+ * 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 <os/os.h>
+#include <shell/shell.h>
+#include "oc_buffer.h"
+#include "../oc_log.h"
+#include "adaptor.h"
+
+#ifdef OC_TRANSPORT_SERIAL
+
+struct os_mqueue oc_serial_mqueue;
+
+static int
+oc_serial_in(struct os_mbuf *m, void *arg)
+{
+    return os_mqueue_put(&oc_serial_mqueue, &oc_event_q, m);
+}
+
+void
+oc_connectivity_shutdown_serial(void) {
+    shell_nlip_input_register(NULL, NULL);
+}
+
+int
+oc_connectivity_init_serial(void) {
+    int rc;
+
+    rc = shell_nlip_input_register(oc_serial_in, NULL);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = os_mqueue_init(&oc_serial_mqueue, NULL);
+    if (rc != 0) {
+        goto err;
+    }
+    /* override the eventq type */
+    oc_serial_mqueue.mq_ev.ev_type = OC_ADATOR_EVENT_SERIAL;
+
+err:
+    oc_connectivity_shutdown_serial();
+    return rc;
+}
+
+
+void oc_send_buffer_serial(oc_message_t *message) {
+    int rc;
+    struct os_mbuf *m;
+
+    /* get a packet header */
+    m = os_msys_get_pkthdr(0, 0);
+    if (m == NULL) {
+        goto err;
+    }
+
+    /* add this data to the mbuf */
+    rc = os_mbuf_append(m, message->data, message->length);
+    if (rc != 0) {
+        goto err;
+    }
+
+    /* send over the shell output */
+    rc = shell_nlip_output(m);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return;
+
+    err:
+    ERROR("Unable to send message via oc_serial %d\n", rc);
+    oc_message_unref(message);
+    return;
+
+}
+
+oc_message_t *
+oc_attempt_rx_serial(void) {
+    int rc;
+    struct os_mbuf *m = NULL;
+    struct os_mbuf_pkthdr *pkt;
+    oc_message_t *message = NULL;
+
+    LOG("oc_transport_serial attempt rx\n");
+
+    /* get an mbuf from the queue */
+    m = os_mqueue_get(&oc_serial_mqueue);
+
+    if (!OS_MBUF_IS_PKTHDR(m)) {
+        goto rx_attempt_err;
+    }
+
+    pkt = OS_MBUF_PKTHDR(m);
+
+    LOG("oc_transport_serial rx %p-%u\n", pkt, pkt->omp_len);
+
+    message = oc_allocate_message();
+    if (NULL == message) {
+        ERROR("Could not allocate OC message buffer\n");
+        goto rx_attempt_err;
+    }
+
+    if (pkt->omp_len > MAX_PAYLOAD_SIZE) {
+        ERROR("Message to large for OC message buffer\n");
+        goto rx_attempt_err;
+    }
+    /* copy to message from mbuf chain */
+    rc = os_mbuf_copydata(m, 0, pkt->omp_len, message->data);
+    if (rc != 0) {
+        ERROR("Failed to copy message from mbuf to OC message buffer \n");
+        goto rx_attempt_err;
+    }
+
+    os_mbuf_free_chain(m);
+
+    message->endpoint.flags = SERIAL;
+    message->length = pkt->omp_len;
+
+    LOG("Successfully rx length %lu\n", message->length);
+    return message;
+
+    /* add the addr info to the message */
+rx_attempt_err:
+    if (m) {
+        os_mbuf_free_chain(m);
+    }
+
+    if (message) {
+        oc_message_unref(message);
+    }
+
+    return NULL;
+}
+
+#endif 
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/libs/iotivity/src/port/oc_connectivity.h
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/oc_connectivity.h b/libs/iotivity/src/port/oc_connectivity.h
index b004cea..4e4cdec 100644
--- a/libs/iotivity/src/port/oc_connectivity.h
+++ b/libs/iotivity/src/port/oc_connectivity.h
@@ -44,7 +44,8 @@ typedef struct
     GATT = 1 << 1,
     IPSP = 1 << 2,
     MULTICAST = 1 << 3,
-    SECURED = 1 << 4
+    SECURED = 1 << 4,
+    SERIAL = 1 <<5,
   } flags;
 
   union

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/12ef82c6/libs/os/include/os/os_eventq.h
----------------------------------------------------------------------
diff --git a/libs/os/include/os/os_eventq.h b/libs/os/include/os/os_eventq.h
index 9e2b1be..b298f92 100644
--- a/libs/os/include/os/os_eventq.h
+++ b/libs/os/include/os/os_eventq.h
@@ -33,7 +33,7 @@ struct os_event {
 #define OS_EVENT_QUEUED(__ev) ((__ev)->ev_queued)
 
 #define OS_EVENT_T_TIMER (1)
-#define OS_EVENT_T_MQUEUE_DATA (2) 
+#define OS_EVENT_T_MQUEUE_DATA (2)
 #define OS_EVENT_T_PERUSER (16)
 
 struct os_eventq {


[44/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-391 STM32F4 ADC driver and ADC HAL support

Posted by vi...@apache.org.
MYNEWT-391 STM32F4 ADC driver and ADC HAL support

- More cleanup
- Removing asserts from config functions
- Checking failure of gpio init function instead of asserting


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

Branch: refs/heads/sterly_refactor
Commit: 5d11ead1aaf3e32c14316d8dfc230ce448e26f20
Parents: 01fb015
Author: Vipul Rahane <vi...@runtime.io>
Authored: Thu Sep 22 20:19:18 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Thu Sep 22 20:21:03 2016 -0700

----------------------------------------------------------------------
 drivers/adc/adc_stm32f4/pkg.yml           |  3 +-
 drivers/adc/adc_stm32f4/src/adc_stm32f4.c | 80 +++++++++++++++-----------
 drivers/adc/include/adc/adc.h             |  1 -
 3 files changed, 47 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5d11ead1/drivers/adc/adc_stm32f4/pkg.yml
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/pkg.yml b/drivers/adc/adc_stm32f4/pkg.yml
index bb24fd7..25a2c71 100644
--- a/drivers/adc/adc_stm32f4/pkg.yml
+++ b/drivers/adc/adc_stm32f4/pkg.yml
@@ -26,8 +26,9 @@ pkg.features:
     - ADC_STM32F4
 pkg.apis:
     - ADC_HW_IMPL
+pkg.deps:
+   - drivers/adc
 pkg.deps.TEST:
    - hw/hal
    - hw/mcu/stm/stm32f4xx
-   - drivers/adc
    - libs/testutil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5d11ead1/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/src/adc_stm32f4.c b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
index 480464a..a2edfa8 100644
--- a/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
+++ b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
@@ -19,7 +19,6 @@
 
 
 #include <hal/hal_bsp.h>
-#include <adc/adc.h>
 #include <assert.h>
 #include <os/os.h>
 #include <bsp/cmsis_nvic.h>
@@ -31,6 +30,11 @@
 #include "adc_stm32f4/adc_stm32f4.h"
 #include "stm32f4xx_hal_dma.h"
 #include "mcu/stm32f4xx_mynewt_hal.h"
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(ADC_1)||MYNEWT_VAL(ADC_2)||MYNEWT_VAL(ADC_3)
+#include <adc/adc.h>
+#endif
 
 #define STM32F4_IS_DMA_ADC_CHANNEL(CHANNEL) ((CHANNEL) <= DMA_CHANNEL_2)
 
@@ -88,13 +92,14 @@ stm32f4_adc_clk_disable(ADC_HandleTypeDef *hadc) {
     }
 }
 
-static GPIO_InitTypeDef
-stm32f4_resolve_adc_gpio(ADC_HandleTypeDef *adc, uint8_t cnum)
+static int
+stm32f4_resolve_adc_gpio(ADC_HandleTypeDef *adc, uint8_t cnum, GPIO_InitTypeDef *gpio)
 {
     uintptr_t adc_addr = (uintptr_t)adc->Instance;
     uint32_t pin;
-    GPIO_InitTypeDef gpio_td;
+    int rc;
 
+    rc = OS_OK;
     switch (adc_addr) {
         case (uintptr_t)ADC1:
         case (uintptr_t)ADC2:
@@ -180,16 +185,17 @@ stm32f4_resolve_adc_gpio(ADC_HandleTypeDef *adc, uint8_t cnum)
                     goto done;
             }
         default:
-            assert(0);
+            rc = OS_EINVAL;
+            return rc;
     }
 done:
-    gpio_td = (GPIO_InitTypeDef){
+    *gpio = (GPIO_InitTypeDef) {
         .Pin = pin,
         .Mode = GPIO_MODE_ANALOG,
         .Pull = GPIO_NOPULL,
         .Alternate = pin
     };
-    return gpio_td;
+    return rc;
 }
 
 static IRQn_Type
@@ -311,7 +317,7 @@ HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
     ++stm32f4_adc_stats.adc_dma_xfer_complete;
 
     adc = adc_dma[stm32f4_resolve_dma_handle_idx(hdma)];
-    cfg  = adc->ad_dev_cfg;
+    cfg  = (struct stm32f4_adc_dev_cfg *)adc->ad_dev.od_init_arg;
 
     buf = cfg->primarybuf;
     /**
@@ -364,26 +370,18 @@ stm32f4_adc_dma_init(ADC_HandleTypeDef* hadc)
 }
 
 static void
-stm32f4_adc_init(void *arg)
+stm32f4_adc_init(struct adc_dev *dev)
 {
-    struct adc_dev *dev;
     struct stm32f4_adc_dev_cfg *adc_config;
     ADC_HandleTypeDef *hadc;
-    GPIO_InitTypeDef gpio_td;
-    uint8_t cnum;
 
-    assert(arg);
+    assert(dev);
 
-    dev = (struct adc_dev *)arg;
-    adc_config = (struct stm32f4_adc_dev_cfg *)dev->ad_dev_cfg;
+    adc_config = (struct stm32f4_adc_dev_cfg *)dev->ad_dev.od_init_arg;
     hadc = adc_config->sac_adc_handle;
-    cnum = dev->ad_chans->c_cnum;
 
     stm32f4_adc_dma_init(hadc);
 
-    gpio_td = stm32f4_resolve_adc_gpio(hadc, cnum);
-    hal_gpio_init_stm(gpio_td.Pin, &gpio_td);
-
     if (HAL_ADC_Init(hadc) != HAL_OK) {
         assert(0);
     }
@@ -399,17 +397,11 @@ stm32f4_adc_uninit(struct adc_dev *dev)
     uint8_t cnum;
 
     assert(dev);
-    cfg  = dev->ad_dev_cfg;
+    cfg  = (struct stm32f4_adc_dev_cfg *)dev->ad_dev.od_init_arg;
     hadc = cfg->sac_adc_handle;
     hdma = hadc->DMA_Handle;
     cnum = dev->ad_chans->c_cnum;
 
-    gpio_td = stm32f4_resolve_adc_gpio(hadc, cnum);
-
-    if(hal_gpio_deinit_stm(gpio_td.Pin, &gpio_td)) {
-        assert(0);
-    }
-
     __HAL_RCC_DMA2_CLK_DISABLE();
     if (HAL_DMA_DeInit(hdma) != HAL_OK) {
         assert(0);
@@ -417,6 +409,17 @@ stm32f4_adc_uninit(struct adc_dev *dev)
     stm32f4_adc_clk_disable(hadc);
 
     NVIC_DisableIRQ(stm32f4_resolve_adc_dma_irq(hdma));
+
+    if (stm32f4_resolve_adc_gpio(hadc, cnum, &gpio_td)) {
+        goto err;
+    }
+
+    if(hal_gpio_deinit_stm(gpio_td.Pin, &gpio_td)) {
+        goto err;
+    }
+
+err:
+    return;
 }
 
 /**
@@ -460,7 +463,7 @@ stm32f4_adc_open(struct os_dev *odev, uint32_t wait, void *arg)
 
     stm32f4_adc_init(dev);
 
-    cfg  = dev->ad_dev_cfg;
+    cfg  = (struct stm32f4_adc_dev_cfg *)dev->ad_dev.od_init_arg;
     hadc = cfg->sac_adc_handle;
     hdma = hadc->DMA_Handle;
 
@@ -513,14 +516,15 @@ stm32f4_adc_configure_channel(struct adc_dev *dev, uint8_t cnum,
     ADC_HandleTypeDef *hadc;
     struct stm32f4_adc_dev_cfg *cfg;
     struct adc_chan_config *chan_cfg;
+    GPIO_InitTypeDef gpio_td;
 
     rc = OS_EINVAL;
 
-    if (dev != NULL && IS_ADC_CHANNEL(cnum)) {
+    if (dev == NULL && !IS_ADC_CHANNEL(cnum)) {
         goto err;
     }
 
-    cfg  = dev->ad_dev_cfg;
+    cfg  = (struct stm32f4_adc_dev_cfg *)dev->ad_dev.od_init_arg;
     hadc = cfg->sac_adc_handle;
     chan_cfg = cfg->sac_chans;
 
@@ -533,6 +537,13 @@ stm32f4_adc_configure_channel(struct adc_dev *dev, uint8_t cnum,
     dev->ad_chans[cnum].c_res = chan_cfg->c_res;
     dev->ad_chans[cnum].c_refmv = chan_cfg->c_refmv;
     dev->ad_chans[cnum].c_configured = 1;
+    dev->ad_chans[cnum].c_cnum = cnum;
+
+    if (stm32f4_resolve_adc_gpio(hadc, cnum, &gpio_td)) {
+        goto err;
+    }
+
+    hal_gpio_init_stm(gpio_td.Pin, &gpio_td);
 
 #if 0
     if (HAL_ADC_Start_IT(hadc) != HAL_OK) {
@@ -562,9 +573,9 @@ stm32f4_adc_set_buffer(struct adc_dev *dev, void *buf1, void *buf2,
 
     assert(dev != NULL && buf1 != NULL);
     rc = OS_OK;
-    buflen /= sizeof(uint16_t);
+    buflen /= sizeof(uint32_t);
 
-    cfg  = dev->ad_dev_cfg;
+    cfg  = (struct stm32f4_adc_dev_cfg *)dev->ad_dev.od_init_arg;
 
     cfg->primarybuf = buf1;
     cfg->secondarybuf = buf2;
@@ -580,7 +591,7 @@ stm32f4_adc_release_buffer(struct adc_dev *dev, void *buf, int buf_len)
     struct stm32f4_adc_dev_cfg *cfg;
 
     assert(dev);
-    cfg  = dev->ad_dev_cfg;
+    cfg  = (struct stm32f4_adc_dev_cfg *)dev->ad_dev.od_init_arg;
     hadc = cfg->sac_adc_handle;
 
     HAL_ADC_Stop_DMA(hadc);
@@ -602,7 +613,7 @@ stm32f4_adc_sample(struct adc_dev *dev)
     struct stm32f4_adc_dev_cfg *cfg;
 
     assert(dev);
-    cfg  = dev->ad_dev_cfg;
+    cfg  = (struct stm32f4_adc_dev_cfg *)dev->ad_dev.od_init_arg;
     hadc = cfg->sac_adc_handle;
 
     rc = OS_EINVAL;
@@ -632,7 +643,7 @@ stm32f4_adc_read_channel(struct adc_dev *dev, uint8_t cnum, int *result)
     struct stm32f4_adc_dev_cfg *cfg;
 
     assert(dev != NULL && result != NULL);
-    cfg  = dev->ad_dev_cfg;
+    cfg  = (struct stm32f4_adc_dev_cfg *)dev->ad_dev.od_init_arg;
     hadc = cfg->sac_adc_handle;
 
     *result = HAL_ADC_GetValue(hadc);
@@ -708,7 +719,6 @@ stm32f4_adc_dev_init(struct os_dev *odev, void *arg)
     OS_DEV_SETHANDLERS(odev, stm32f4_adc_open, stm32f4_adc_close);
 
     af = &dev->ad_funcs;
-    dev->ad_dev_cfg = arg;
 
     af->af_configure_channel = stm32f4_adc_configure_channel;
     af->af_sample = stm32f4_adc_sample;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5d11ead1/drivers/adc/include/adc/adc.h
----------------------------------------------------------------------
diff --git a/drivers/adc/include/adc/adc.h b/drivers/adc/include/adc/adc.h
index 60df1ad..2ffb6cd 100644
--- a/drivers/adc/include/adc/adc.h
+++ b/drivers/adc/include/adc/adc.h
@@ -154,7 +154,6 @@ struct adc_dev {
     struct os_mutex ad_lock;
     struct adc_driver_funcs ad_funcs;
     struct adc_chan_config *ad_chans;
-    void *ad_dev_cfg;
     int ad_chan_count;
     adc_event_handler_func_t ad_event_handler_func;
     void *ad_event_handler_arg;


[33/50] [abbrv] incubator-mynewt-core git commit: sys/reboot - Don't require fcb.

Posted by vi...@apache.org.
sys/reboot - Don't require fcb.


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

Branch: refs/heads/sterly_refactor
Commit: 4b9def2d60dfccf5a1a08555298494c310c3ef9c
Parents: b7375d0
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Sep 22 00:07:21 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Thu Sep 22 00:07:21 2016 -0700

----------------------------------------------------------------------
 sys/reboot/pkg.yml          | 11 ++++----
 sys/reboot/src/log_reboot.c | 54 +++++++++++++++++++++++++++++-----------
 2 files changed, 46 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4b9def2d/sys/reboot/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/reboot/pkg.yml b/sys/reboot/pkg.yml
index afa950b..a34d45c 100644
--- a/sys/reboot/pkg.yml
+++ b/sys/reboot/pkg.yml
@@ -28,9 +28,10 @@ pkg.keywords:
 pkg.deps:
     - libs/os
     - libs/util
-    - sys/fcb
     - sys/log
     - libs/imgmgr
+pkg.deps.REBOOT_LOG_FCB:
+    - sys/fcb
 pkg.req_apis:
     - console
 
@@ -38,9 +39,9 @@ pkg.init_function: log_reboot_pkg_init
 pkg.init_stage: 2
 
 pkg.syscfg_defs:
-    REBOOT_LOG_0_TYPE:
+    REBOOT_LOG_FCB:
         description: 'TBD'
-        value: LOG_STORE_CONSOLE
-    REBOOT_LOG_0_ENTRY_COUNT:
+        value: 0
+    REBOOT_LOG_ENTRY_COUNT:
         description: 'TBD'
-        value:  10
+        value: 10

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4b9def2d/sys/reboot/src/log_reboot.c
----------------------------------------------------------------------
diff --git a/sys/reboot/src/log_reboot.c b/sys/reboot/src/log_reboot.c
index 6a6bb63..73cbda0 100644
--- a/sys/reboot/src/log_reboot.c
+++ b/sys/reboot/src/log_reboot.c
@@ -22,7 +22,6 @@
 #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"
@@ -32,6 +31,11 @@
 #include "config/config_file.h"
 #include "reboot/log_reboot.h"
 #include "bsp/bsp.h"
+#include "hal/flash_map.h"
+
+#if MYNEWT_VAL(REBOOT_LOG_FCB)
+#include "fcb/fcb.h"
+#endif
 
 static struct log_handler *reboot_log_handler;
 static struct log reboot_log;
@@ -41,9 +45,6 @@ static char reboot_cnt_str[12];
 static char soft_reboot_str[12];
 static char *reboot_cnt_get(int argc, char **argv, char *buf, int max_len);
 static int reboot_cnt_set(int argc, char **argv, char *val);
-static struct flash_area sector;
-
-static struct fcb_log reboot_log_fcb;
 
 struct conf_handler reboot_conf_handler = {
     .ch_name = "reboot",
@@ -53,6 +54,11 @@ struct conf_handler reboot_conf_handler = {
     .ch_export = NULL
 };
 
+#if MYNEWT_VAL(REBOOT_LOG_FCB)
+static struct fcb_log reboot_log_fcb;
+static struct flash_area sector;
+#endif
+
 /**
  * Reboot log initilization
  * @param type of log(console or storage); number of entries to restore
@@ -61,17 +67,25 @@ struct conf_handler reboot_conf_handler = {
 int
 reboot_init_handler(int log_store_type, uint8_t entries)
 {
-    int rc;
+#if MYNEWT_VAL(REBOOT_LOG_FCB)
     const struct flash_area *ptr;
     struct fcb *fcbp = &reboot_log_fcb.fl_fcb;
+#endif
+    void *arg;
+    int rc;
 
     rc = conf_register(&reboot_conf_handler);
+    if (rc != 0) {
+        return rc;
+    }
 
     switch (log_store_type) {
+#if MYNEWT_VAL(REBOOT_LOG_FCB)
         case LOG_STORE_FCB:
             if (flash_area_open(FLASH_AREA_REBOOT_LOG, &ptr)) {
-                goto err;
+                return rc;
             }
+            fcbp = &reboot_log_fcb.fl_fcb;
             sector = *ptr;
             fcbp->f_sectors = &sector;
             fcbp->f_sector_cnt = 1;
@@ -82,15 +96,18 @@ reboot_init_handler(int log_store_type, uint8_t entries)
 
             rc = fcb_init(fcbp);
             if (rc) {
-                goto err;
+                return rc;
             }
             reboot_log_handler = (struct log_handler *)&log_fcb_handler;
             if (rc) {
-                goto err;
+                return rc;
             }
+            arg = &reboot_log_fcb;
             break;
+#endif
        case LOG_STORE_CONSOLE:
             reboot_log_handler = (struct log_handler *)&log_console_handler;
+            arg = NULL;
             break;
        default:
             assert(0);
@@ -98,9 +115,12 @@ reboot_init_handler(int log_store_type, uint8_t entries)
 
     rc = log_register("reboot_log", &reboot_log,
                       (struct log_handler *)reboot_log_handler,
-                      &reboot_log_fcb);
-err:
-    return (rc);
+                      arg);
+    if (rc != 0) {
+        return rc;
+    }
+
+    return 0;
 }
 
 /**
@@ -207,13 +227,19 @@ err:
 void
 log_reboot_pkg_init(void)
 {
+    int type;
     int rc;
 
     (void)rc;
+    (void)type;
 
-#if MYNEWT_VAL(REBOOT_LOG_0_TYPE)
-    rc = reboot_init_handler(MYNEWT_VAL(REBOOT_LOG_0_TYPE),
-                             MYNEWT_VAL(REBOOT_LOG_0_ENTRY_COUNT));
+#if MYNEWT_VAL(REBOOT_LOG_ENTRY_COUNT)
+#if MYNEWT_VAL(REBOOT_LOG_FCB)
+    type = LOG_STORE_FCB;
+#else
+    type = LOG_STORE_CONSOLE;
+#endif
+    rc = reboot_init_handler(type, MYNEWT_VAL(REBOOT_LOG_ENTRY_COUNT));
     SYSINIT_PANIC_ASSERT(rc == 0);
 #endif
 }


[29/50] [abbrv] incubator-mynewt-core git commit: Shell - Only create task if so configured.

Posted by vi...@apache.org.
Shell - Only create task if so configured.

Some packages depend on the shell package without actually requiring the
shell task to exist.  This might not make the most sense, but for now,
add a syscfg option (SHELL_TASK) to allow control over whether the shell
task gets created.


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

Branch: refs/heads/sterly_refactor
Commit: 6547b7dac0bebdd8892f88bf452e309093cac60c
Parents: 6c48ecd
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Sep 21 20:09:34 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Sep 21 20:13:48 2016 -0700

----------------------------------------------------------------------
 libs/shell/pkg.yml     | 7 +++++--
 libs/shell/src/shell.c | 4 ++++
 2 files changed, 9 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6547b7da/libs/shell/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/shell/pkg.yml b/libs/shell/pkg.yml
index 36372ee..5e2cd69 100644
--- a/libs/shell/pkg.yml
+++ b/libs/shell/pkg.yml
@@ -33,13 +33,16 @@ pkg.init_function: shell_init
 pkg.init_stage: 5
 
 pkg.syscfg_defs:
+    SHELL_TASK:
+        description: 'TBD'
+        value: 0
     SHELL_TASK_PRIO:
         description: 'TBD'
         type: 'task_priority'
         value: 'any'
     SHELL_STACK_SIZE:
         description: 'TBD'
-        value:     512
+        value: 512
     SHELL_MAX_INPUT_LEN:
         description: 'TBD'
-        value:  256
+        value: 256

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/6547b7da/libs/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/libs/shell/src/shell.c b/libs/shell/src/shell.c
index 1cb88de..be9acde 100644
--- a/libs/shell/src/shell.c
+++ b/libs/shell/src/shell.c
@@ -535,6 +535,10 @@ shell_help_cmd(int argc, char **argv)
 void
 shell_init(void)
 {
+#if !MYNEWT_VAL(SHELL_TASK)
+    return;
+#endif
+
     int rc;
 
     free(shell_line);


[09/50] [abbrv] incubator-mynewt-core git commit: newtmgr; don't export os_eventq for newtmgr task.

Posted by vi...@apache.org.
newtmgr; don't export os_eventq for newtmgr task.


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

Branch: refs/heads/sterly_refactor
Commit: f4a3543447811d76e154fa220aad2ce18f9424f6
Parents: ad9ce1c
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Sep 21 09:07:59 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Wed Sep 21 09:07:59 2016 -0700

----------------------------------------------------------------------
 libs/newtmgr/include/newtmgr/newtmgr_priv.h     | 24 --------------------
 libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h  |  3 ++-
 libs/newtmgr/nmgr_os/src/newtmgr_os.c           |  9 +++-----
 libs/newtmgr/src/newtmgr.c                      |  8 +++----
 libs/newtmgr_oic/include/newtmgr/newtmgr_priv.h | 24 --------------------
 libs/newtmgr_oic/src/newtmgr.c                  | 22 ++++++++----------
 6 files changed, 17 insertions(+), 73 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f4a35434/libs/newtmgr/include/newtmgr/newtmgr_priv.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/include/newtmgr/newtmgr_priv.h b/libs/newtmgr/include/newtmgr/newtmgr_priv.h
deleted file mode 100644
index 7217711..0000000
--- a/libs/newtmgr/include/newtmgr/newtmgr_priv.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 __NETMGR_PRIV_H_
-#define __NETMGR_PRIV_H_
-
-extern struct os_eventq g_nmgr_evq;
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f4a35434/libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h b/libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h
index 49b6725..43de227 100644
--- a/libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h
+++ b/libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h
@@ -30,6 +30,7 @@
 #define NMGR_ID_DATETIME_STR    4
 #define NMGR_ID_RESET           5
 
-int nmgr_os_groups_register(void);
+struct os_eventq;
+int nmgr_os_groups_register(struct os_eventq *nmgr_evq);
 
 #endif /* _NMGR_OS_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f4a35434/libs/newtmgr/nmgr_os/src/newtmgr_os.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/nmgr_os/src/newtmgr_os.c b/libs/newtmgr/nmgr_os/src/newtmgr_os.c
index cab692b..6c1a12f 100644
--- a/libs/newtmgr/nmgr_os/src/newtmgr_os.c
+++ b/libs/newtmgr/nmgr_os/src/newtmgr_os.c
@@ -26,7 +26,6 @@
 #include <hal/hal_system.h>
 
 #include <newtmgr/newtmgr.h>
-#include <newtmgr/newtmgr_priv.h>
 
 #include <console/console.h>
 #include <util/datetime.h>
@@ -309,10 +308,6 @@ nmgr_reset_tmo(void *arg)
 static int
 nmgr_reset(struct nmgr_jbuf *njb)
 {
-    if (nmgr_reset_callout.cf_func == NULL) {
-        os_callout_func_init(&nmgr_reset_callout, &g_nmgr_evq,
-          nmgr_reset_tmo, NULL);
-    }
     log_reboot(SOFT_REBOOT);
     os_callout_reset(&nmgr_reset_callout.cf_c, OS_TICKS_PER_SEC / 4);
 
@@ -322,8 +317,10 @@ nmgr_reset(struct nmgr_jbuf *njb)
 }
 
 int
-nmgr_os_groups_register(void)
+nmgr_os_groups_register(struct os_eventq *nmgr_evq)
 {
+    os_callout_func_init(&nmgr_reset_callout, nmgr_evq, nmgr_reset_tmo, NULL);
+
     return nmgr_group_register(&nmgr_def_group);
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f4a35434/libs/newtmgr/src/newtmgr.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/src/newtmgr.c b/libs/newtmgr/src/newtmgr.c
index f9d3846..8ff2ea3 100644
--- a/libs/newtmgr/src/newtmgr.c
+++ b/libs/newtmgr/src/newtmgr.c
@@ -27,14 +27,12 @@
 #include <newtmgr/newtmgr.h>
 #include <nmgr_os/nmgr_os.h>
 
-#include "newtmgr/newtmgr_priv.h"
-
 struct nmgr_transport g_nmgr_shell_transport;
 
 struct os_mutex g_nmgr_group_list_lock;
 
-struct os_eventq g_nmgr_evq;
-struct os_task g_nmgr_task;
+static struct os_eventq g_nmgr_evq;
+static struct os_task g_nmgr_task;
 
 STAILQ_HEAD(, nmgr_group) g_nmgr_group_list =
     STAILQ_HEAD_INITIALIZER(g_nmgr_group_list);
@@ -552,7 +550,7 @@ nmgr_task_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
         goto err;
     }
 
-    rc = nmgr_os_groups_register();
+    rc = nmgr_os_groups_register(&g_nmgr_evq);
     if (rc != 0) {
         goto err;
     }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f4a35434/libs/newtmgr_oic/include/newtmgr/newtmgr_priv.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr_oic/include/newtmgr/newtmgr_priv.h b/libs/newtmgr_oic/include/newtmgr/newtmgr_priv.h
deleted file mode 100644
index 7217711..0000000
--- a/libs/newtmgr_oic/include/newtmgr/newtmgr_priv.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 __NETMGR_PRIV_H_
-#define __NETMGR_PRIV_H_
-
-extern struct os_eventq g_nmgr_evq;
-
-#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/f4a35434/libs/newtmgr_oic/src/newtmgr.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr_oic/src/newtmgr.c b/libs/newtmgr_oic/src/newtmgr.c
index 434bf46..4d7f25b 100644
--- a/libs/newtmgr_oic/src/newtmgr.c
+++ b/libs/newtmgr_oic/src/newtmgr.c
@@ -28,18 +28,17 @@
 
 #include <iotivity/oc_api.h>
 
-#include "newtmgr/newtmgr_priv.h"
-
 #define NMGR_OC_EVENT	(OS_EVENT_T_PERUSER)
 #define NMGR_OC_TIMER	(OS_EVENT_T_PERUSER + 1)
 
 struct nmgr_state {
     struct os_mutex ns_group_lock;
     STAILQ_HEAD(, nmgr_group) ns_groups;
+    struct os_eventq ns_evq;
     struct os_event ns_oc_event;
     struct os_callout ns_oc_timer;
     struct os_task ns_task;
-    struct nmgr_jbuf ns_jbuf;
+    struct nmgr_jbuf ns_jbuf;		/* JSON buffer for NMGR task */
     char ns_rsp[NMGR_MAX_MTU];
 };
 
@@ -47,13 +46,8 @@ static struct nmgr_state nmgr_state = {
   .ns_groups = STAILQ_HEAD_INITIALIZER(nmgr_state.ns_groups),
   .ns_oc_event.ev_type = NMGR_OC_EVENT,
   .ns_oc_timer.c_ev.ev_type = NMGR_OC_TIMER,
-  .ns_oc_timer.c_evq = &g_nmgr_evq
+  .ns_oc_timer.c_evq = &nmgr_state.ns_evq
 };
-struct os_eventq g_nmgr_evq;
-
-/* JSON buffer for NMGR task
- */
-
 
 static void nmgr_oic_get(oc_request_t *request, oc_interface_mask_t interface);
 static void nmgr_oic_put(oc_request_t *request, oc_interface_mask_t interface);
@@ -445,7 +439,9 @@ static const oc_handler_t nmgr_oc_handler = {
 void
 oc_signal_main_loop(void)
 {
-    os_eventq_put(&g_nmgr_evq, &nmgr_state.ns_oc_event);
+    struct nmgr_state *ns = &nmgr_state;
+
+    os_eventq_put(&ns->ns_evq, &ns->ns_oc_event);
 }
 
 void
@@ -460,7 +456,7 @@ nmgr_oic_task(void *arg)
 
     oc_main_init((oc_handler_t *)&nmgr_oc_handler);
     while (1) {
-        ev = os_eventq_get(&g_nmgr_evq);
+        ev = os_eventq_get(&ns->ns_evq);
         switch (ev->ev_type) {
         case NMGR_OC_EVENT:
         case NMGR_OC_TIMER:
@@ -485,7 +481,7 @@ nmgr_oic_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
     struct nmgr_state *ns = &nmgr_state;
     int rc;
 
-    os_eventq_init(&g_nmgr_evq);
+    os_eventq_init(&ns->ns_evq);
 
     rc = os_task_init(&ns->ns_task, "newtmgr_oic", nmgr_oic_task, NULL, prio,
             OS_WAIT_FOREVER, stack_ptr, stack_len);
@@ -493,7 +489,7 @@ nmgr_oic_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
         goto err;
     }
 
-    rc = nmgr_os_groups_register();
+    rc = nmgr_os_groups_register(&ns->ns_evq);
     if (rc != 0) {
         goto err;
     }


[06/50] [abbrv] incubator-mynewt-core git commit: iotivity; increase the define for # of resources.

Posted by vi...@apache.org.
iotivity; increase the define for # of resources.


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

Branch: refs/heads/sterly_refactor
Commit: 3ba8d18d748c12eabf8f5910dc72cb821b8ee4b0
Parents: afc5fca
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Sep 20 17:46:58 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Tue Sep 20 17:46:58 2016 -0700

----------------------------------------------------------------------
 libs/iotivity/src/port/mynewt/config.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/3ba8d18d/libs/iotivity/src/port/mynewt/config.h
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/config.h b/libs/iotivity/src/port/mynewt/config.h
index 1889a70..c2602c4 100644
--- a/libs/iotivity/src/port/mynewt/config.h
+++ b/libs/iotivity/src/port/mynewt/config.h
@@ -21,7 +21,7 @@ typedef os_time_t oc_clock_time_t;
 
 /* Server-side parameters */
 /* Maximum number of server resources */
-#define MAX_APP_RESOURCES (2)
+#define MAX_APP_RESOURCES (8)
 
 /* Common paramters */
 /* Maximum number of concurrent requests */


[38/50] [abbrv] incubator-mynewt-core git commit: boot[util] - Remove config/fs/nffs vestiges.

Posted by vi...@apache.org.
boot[util] - Remove config/fs/nffs vestiges.


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

Branch: refs/heads/sterly_refactor
Commit: 742b7c48e4955642086cad57c128d6891e3f5fbb
Parents: a783368
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Sep 22 16:21:51 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Thu Sep 22 16:29:16 2016 -0700

----------------------------------------------------------------------
 apps/boot/pkg.yml                 | 13 -------------
 apps/boot/src/boot.c              |  2 --
 libs/bootutil/pkg.yml             |  7 -------
 libs/bootutil/src/bootutil_misc.c |  1 -
 4 files changed, 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/742b7c48/apps/boot/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/boot/pkg.yml b/apps/boot/pkg.yml
index 22566b6..b8f8062 100644
--- a/apps/boot/pkg.yml
+++ b/apps/boot/pkg.yml
@@ -26,18 +26,11 @@ pkg.keywords:
     - loader
 
 pkg.deps:
-    - sys/config
     - libs/bootutil
     - libs/os
     - libs/util
     - libs/console/stub
 
-pkg.deps.BOOT_NFFS:
-    - fs/nffs
-
-pkg.deps.BOOT_FCB:
-    - sys/fcb
-
 pkg.deps.BOOT_SERIAL.OVERWRITE:
     - libs/console/full
     - libs/boot_serial
@@ -46,12 +39,6 @@ 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/742b7c48/apps/boot/src/boot.c
----------------------------------------------------------------------
diff --git a/apps/boot/src/boot.c b/apps/boot/src/boot.c
index fa0ba84..1a98a5b 100755
--- a/apps/boot/src/boot.c
+++ b/apps/boot/src/boot.c
@@ -27,8 +27,6 @@
 #include <hal/hal_bsp.h>
 #include <hal/hal_system.h>
 #include <hal/hal_flash.h>
-#include <config/config.h>
-#include <config/config_file.h>
 #if MYNEWT_VAL(BOOT_SERIAL)
 #include <hal/hal_gpio.h>
 #include <boot_serial/boot_serial.h>

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/742b7c48/libs/bootutil/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/bootutil/pkg.yml b/libs/bootutil/pkg.yml
index 1293ea8..77599f8 100644
--- a/libs/bootutil/pkg.yml
+++ b/libs/bootutil/pkg.yml
@@ -29,15 +29,8 @@ pkg.deps:
     - hw/hal
     - libs/mbedtls
     - libs/os 
-    - sys/config
-
-pkg.deps.BOOTUTIL_NFFS:
-    - fs/nffs
 
 pkg.syscfg_defs:
-    BOOTUTIL_NFFS:
-        description: 'TBD'
-        value: 'MYNEWT_PKG_FS_NFFS'
     BOOTUTIL_SIGN_RSA:
         description: 'TBD'
         value: '0'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/742b7c48/libs/bootutil/src/bootutil_misc.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_misc.c b/libs/bootutil/src/bootutil_misc.c
index b6a1c31..f64d2a4 100644
--- a/libs/bootutil/src/bootutil_misc.c
+++ b/libs/bootutil/src/bootutil_misc.c
@@ -25,7 +25,6 @@
 #include "hal/hal_bsp.h"
 #include "hal/hal_flash.h"
 #include "hal/flash_map.h"
-#include "config/config.h"
 #include "os/os.h"
 #include "bootutil/image.h"
 #include "bootutil/loader.h"


[11/50] [abbrv] incubator-mynewt-core git commit: packages; depend on newtmgr API, instead of specific newtmgr pkg.

Posted by vi...@apache.org.
packages; depend on newtmgr API, instead of specific newtmgr pkg.


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

Branch: refs/heads/sterly_refactor
Commit: b402017f553b73f9ce5d9608b2ad9ff85840643d
Parents: 4cb7c5b
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Sep 21 10:32:27 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Wed Sep 21 10:32:27 2016 -0700

----------------------------------------------------------------------
 libs/crash_test/pkg.yml      | 3 ++-
 libs/imgmgr/pkg.yml          | 3 ++-
 libs/newtmgr/nmgr_os/pkg.yml | 5 ++---
 sys/config/pkg.yml           | 6 +++++-
 sys/log/pkg.yml              | 4 ++--
 sys/stats/pkg.yml            | 4 ++--
 6 files changed, 15 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b402017f/libs/crash_test/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/crash_test/pkg.yml b/libs/crash_test/pkg.yml
index 872c029..541dba7 100644
--- a/libs/crash_test/pkg.yml
+++ b/libs/crash_test/pkg.yml
@@ -30,7 +30,8 @@ pkg.cflags.SHELL:
     - -DSHELL_PRESENT
 
 pkg.deps.NEWTMGR:
-    - libs/newtmgr
     - libs/json
+pkg.req_apis.NEWTMGR:
+    - newtmgr
 pkg.cflags.NEWTMGR:
     - -DNEWTMGR_PRESENT

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b402017f/libs/imgmgr/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/imgmgr/pkg.yml b/libs/imgmgr/pkg.yml
index 5d674d0..dc214c1 100644
--- a/libs/imgmgr/pkg.yml
+++ b/libs/imgmgr/pkg.yml
@@ -24,12 +24,13 @@ pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
 
 pkg.deps:
-    - libs/newtmgr
     - libs/bootutil
     - libs/util
 pkg.deps.FS:
     - fs/fs
 pkg.cflags.FS: -DFS_PRESENT
+pkg.req_apis:
+    - newtmgr
 
 pkg.deps.COREDUMP:
     - sys/coredump

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b402017f/libs/newtmgr/nmgr_os/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/newtmgr/nmgr_os/pkg.yml b/libs/newtmgr/nmgr_os/pkg.yml
index d6610b2..66a3f13 100644
--- a/libs/newtmgr/nmgr_os/pkg.yml
+++ b/libs/newtmgr/nmgr_os/pkg.yml
@@ -29,8 +29,7 @@ pkg.deps:
     - libs/json
     - libs/util
     - libs/testutil
-    - libs/newtmgr
     - sys/reboot
 
-pkg.reqs:
-    - console
+pkg.req_apis:
+    - newtmgr

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b402017f/sys/config/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/config/pkg.yml b/sys/config/pkg.yml
index a31f33e..1aa4879 100644
--- a/sys/config/pkg.yml
+++ b/sys/config/pkg.yml
@@ -26,15 +26,19 @@ pkg.keywords:
 pkg.deps:
     - libs/util
     - libs/testutil
+
 pkg.deps.SHELL:
     - libs/shell
 pkg.req_apis.SHELL:
     - console
 pkg.cflags.SHELL: -DSHELL_PRESENT
+
 pkg.deps.NEWTMGR:
-    - libs/newtmgr
     - libs/json
+pkg.req_apis.NEWTMGR:
+    - newtmgr
 pkg.cflags.NEWTMGR: -DNEWTMGR_PRESENT
+
 pkg.deps.FCB:
     - sys/fcb
 pkg.cflags.FCB: -DFCB_PRESENT

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b402017f/sys/log/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/log/pkg.yml b/sys/log/pkg.yml
index 2bf5036..ed41fb3 100644
--- a/sys/log/pkg.yml
+++ b/sys/log/pkg.yml
@@ -30,13 +30,13 @@ pkg.deps:
     - libs/testutil
 pkg.deps.SHELL:
     - libs/shell
-pkg.deps.NEWTMGR:
-    - libs/newtmgr
 pkg.deps.FCB:
     - hw/hal
     - sys/fcb
 pkg.deps.TEST:
     - sys/fcb
+pkg.req_apis.NEWTMGR:
+    - newtmgr
 pkg.req_apis.SHELL:
     - console
 pkg.cflags.SHELL: -DSHELL_PRESENT

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b402017f/sys/stats/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/stats/pkg.yml b/sys/stats/pkg.yml
index 309dbd6..ab9a0f4 100644
--- a/sys/stats/pkg.yml
+++ b/sys/stats/pkg.yml
@@ -30,8 +30,8 @@ pkg.deps:
     - libs/testutil
 pkg.deps.SHELL:
     - libs/shell
-pkg.deps.NEWTMGR:
-    - libs/newtmgr
+pkg.req_apis.NEWTMGR:
+    - newtmgr
 pkg.req_apis.SHELL:
     - console
 pkg.cflags.SHELL: -DSHELL_PRESENT


[42/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-391 STM32F4 ADC driver and ADC HAL support

Posted by vi...@apache.org.
MYNEWT-391 STM32F4 ADC driver and ADC HAL support

- Remove log handling from sblinky(experimental app) to resolve build errors


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

Branch: refs/heads/sterly_refactor
Commit: dfca22057430edad38fb98006d99cdd28c92bbf4
Parents: 28814b8
Author: Vipul Rahane <vi...@runtime.io>
Authored: Wed Sep 21 22:23:31 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Thu Sep 22 20:21:03 2016 -0700

----------------------------------------------------------------------
 apps/sblinky/src/main.c | 7 -------
 1 file changed, 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/dfca2205/apps/sblinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/sblinky/src/main.c b/apps/sblinky/src/main.c
index 4277b70..2358c0d 100755
--- a/apps/sblinky/src/main.c
+++ b/apps/sblinky/src/main.c
@@ -22,7 +22,6 @@
 #include "hal/hal_spi.h"
 #include "console/console.h"
 #include "shell/shell.h"
-#include "log/log.h"
 #include "stats/stats.h"
 #include "config/config.h"
 #include <os/os_dev.h>
@@ -91,9 +90,6 @@ static volatile int g_task1_loops;
 struct os_task task2;
 os_stack_t stack2[TASK2_STACK_SIZE];
 
-struct log_handler log_console_handler;
-struct log my_log;
-
 static volatile int g_task2_loops;
 
 /* Global test semaphore */
@@ -589,9 +585,6 @@ main(int argc, char **argv)
 
     os_init();
 
-    /* Initialize the sblinky log. */
-    log_console_handler_init(&log_console_handler);
-    log_register("sblinky", &my_log, &log_console_handler);
 #if 0
     saadc_test();
 #endif


[32/50] [abbrv] incubator-mynewt-core git commit: Restore newtmgr required API for some packages.

Posted by vi...@apache.org.
Restore newtmgr required API for some packages.


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

Branch: refs/heads/sterly_refactor
Commit: b7375d0a8fce6079de365a0fa6dbff0d1de0be47
Parents: 72c74e5
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Sep 21 20:45:19 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Sep 21 22:27:46 2016 -0700

----------------------------------------------------------------------
 libs/crash_test/pkg.yml | 2 ++
 libs/imgmgr/pkg.yml     | 3 +++
 sys/config/pkg.yml      | 4 +++-
 sys/log/pkg.yml         | 5 +++--
 sys/stats/pkg.yml       | 4 ++--
 5 files changed, 13 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b7375d0a/libs/crash_test/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/crash_test/pkg.yml b/libs/crash_test/pkg.yml
index f823781..c5514d9 100644
--- a/libs/crash_test/pkg.yml
+++ b/libs/crash_test/pkg.yml
@@ -26,6 +26,8 @@ pkg.deps.CRASH_TEST_CLI:
     - libs/shell
 pkg.req_apis.CRASH_TEST_CLI:
     - console
+pkg.req_apis.CRASH_TEST_NEWTMGR:
+    - newtmgr
 
 pkg.deps.CRASH_TEST_NEWTMGR:
     - libs/newtmgr

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b7375d0a/libs/imgmgr/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/imgmgr/pkg.yml b/libs/imgmgr/pkg.yml
index 5194768..65d12a5 100644
--- a/libs/imgmgr/pkg.yml
+++ b/libs/imgmgr/pkg.yml
@@ -27,6 +27,9 @@ pkg.deps:
     - libs/bootutil
     - libs/util
 
+pkg.req_apis:
+    - newtmgr
+
 pkg.deps.IMGMGR_FS:
     - fs/fs
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b7375d0a/sys/config/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/config/pkg.yml b/sys/config/pkg.yml
index 2d7c83d..05b1f94 100644
--- a/sys/config/pkg.yml
+++ b/sys/config/pkg.yml
@@ -28,13 +28,15 @@ pkg.deps:
 pkg.deps.CONFIG_CLI:
     - libs/shell
 pkg.deps.CONFIG_NEWTMGR:
-    - libs/newtmgr
     - libs/json
 pkg.deps.CONFIG_FCB:
     - sys/fcb
 pkg.deps.CONFIG_NFFS:
     - fs/nffs
 
+pkg.req_apis.CONFIG_NEWTMGR:
+    - newtmgr
+
 pkg.init_function: config_pkg_init
 pkg.init_stage: 5
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b7375d0a/sys/log/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/log/pkg.yml b/sys/log/pkg.yml
index dc84d2f..bd57e84 100644
--- a/sys/log/pkg.yml
+++ b/sys/log/pkg.yml
@@ -27,14 +27,15 @@ pkg.keywords:
 pkg.deps:
     - libs/os
     - libs/util
-pkg.deps.LOG_NEWTMGR:
-    - libs/newtmgr
 pkg.deps.LOG_FCB:
     - hw/hal
     - sys/fcb
 pkg.deps.LOG_CLI:
     - libs/shell
 
+pkg.req_apis.LOG_NEWTMGR:
+    - newtmgr
+
 pkg.init_function: log_init
 pkg.init_stage: 1
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b7375d0a/sys/stats/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/stats/pkg.yml b/sys/stats/pkg.yml
index 9927ef3..bde92c8 100644
--- a/sys/stats/pkg.yml
+++ b/sys/stats/pkg.yml
@@ -29,8 +29,8 @@ pkg.deps:
     - libs/util
 pkg.deps.STATS_CLI:
     - libs/shell
-pkg.deps.STATS_NEWTMGR:
-    - libs/newtmgr
+pkg.req_apis.STATS_NEWTMGR:
+    - newtmgr
 
 pkg.init_function: stats_module_init
 pkg.init_stage: 0


[21/50] [abbrv] incubator-mynewt-core git commit: Merge branch 'develop' into sterly_refactor

Posted by vi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/os/test/src/sem_test.c
----------------------------------------------------------------------
diff --cc libs/os/test/src/sem_test.c
index ec79185,0000000..6b40069
mode 100644,000000..100644
--- a/libs/os/test/src/sem_test.c
+++ b/libs/os/test/src/sem_test.c
@@@ -1,401 -1,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_time_delay(2 * OS_TICKS_PER_SEC);
 +    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);
++        os_time_delay(OS_TICKS_PER_SEC / 10);
 +
 +        /* 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_time_delay(OS_TICKS_PER_SEC / 10);
 +    }
 +
 +    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);
++    sem_test_pend_release_loop(0, OS_TICKS_PER_SEC / 10, OS_TICKS_PER_SEC / 10);
 +}
 +
 +static void 
 +sem_test_1_task3_handler(void *arg) 
 +{
-     sem_test_pend_release_loop(0, OS_TIMEOUT_NEVER, 2000);
++    sem_test_pend_release_loop(0, OS_TIMEOUT_NEVER, OS_TICKS_PER_SEC * 2);
 +}
 +
 +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/c5901fcc/libs/shell/include/shell/shell.h
----------------------------------------------------------------------
diff --cc libs/shell/include/shell/shell.h
index 6b89740,1d98148..48351a0
--- a/libs/shell/include/shell/shell.h
+++ b/libs/shell/include/shell/shell.h
@@@ -40,6 -40,10 +40,9 @@@ int shell_nlip_input_register(shell_nli
  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);
  
+ int shell_cmd_list_lock(void);
+ int shell_cmd_list_unlock(void);
+ 
  #endif /* __SHELL_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/shell/src/shell.c
----------------------------------------------------------------------
diff --cc libs/shell/src/shell.c
index 7fe15e5,8c904ec..18f040a
--- a/libs/shell/src/shell.c
+++ b/libs/shell/src/shell.c
@@@ -17,22 -17,23 +17,20 @@@
   * under the License.
   */
  
 -#include <os/os.h>
 -
 -#include <console/console.h>
 -#include <console/prompt.h>
 -
 -#include "shell/shell.h"
 -#include "shell_priv.h"
 -#include "shell/shell_prompt.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,10 -43,9 +40,11 @@@ 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);
+ int shell_prompt_cmd(int argc, char **argv);
  
  static struct shell_cmd g_shell_echo_cmd = {
      .sc_cmd = "echo",
@@@ -530,30 -539,50 +537,35 @@@ 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 = OS_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_prompt_cmd);
+     if (rc != 0) {
+         goto err;
+     }
+     
      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);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/util/test/src/util_test.c
----------------------------------------------------------------------
diff --cc libs/util/test/src/util_test.c
index d528568,0000000..4e21a9a
mode 100644,000000..100644
--- a/libs/util/test/src/util_test.c
+++ b/libs/util/test/src/util_test.c
@@@ -1,46 -1,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 "util_test_priv.h"
 +
 +int
 +util_test_all(void)
 +{
 +    cbmem_test_suite();
 +    return tu_case_failed;
 +}
 +
++int
++hex_fmt_test_all(void)
++{
++    hex_fmt_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();
- 
++    hex_fmt_test_all();
 +    return tu_any_failed;
 +}
 +
 +#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/util/test/src/util_test_priv.h
----------------------------------------------------------------------
diff --cc libs/util/test/src/util_test_priv.h
index cc5533d,0000000..c82df29
mode 100644,000000..100644
--- a/libs/util/test/src/util_test_priv.h
+++ b/libs/util/test/src/util_test_priv.h
@@@ -1,25 -1,0 +1,26 @@@
 +/**
 + * 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);
++int hex_fmt_test_suite(void);
 +
 +#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/wifi_mgmt/src/wifi_cli.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/controller/include/controller/ble_ll.h
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/controller/src/ble_ll.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/controller/src/ble_ll_adv.c
----------------------------------------------------------------------
diff --cc net/nimble/controller/src/ble_ll_adv.c
index 01de299,3e2ce1f..64f5e23
--- a/net/nimble/controller/src/ble_ll_adv.c
+++ b/net/nimble/controller/src/ble_ll_adv.c
@@@ -121,7 -122,24 +123,24 @@@ 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)
+ /**
+  * Called to change advertisers ADVA and INITA (for directed advertisements)
+  * as an advertiser needs to adhere to the resolvable private address generation
+  * timer.
+  *
+  * NOTE: the resolvable private address code uses its own timer to regenerate
+  * local resolvable private addresses. The advertising code uses its own
+  * timer to reset the INITA (for directed advertisements). This code also sets
+  * the appropriate txadd and rxadd bits that will go into the advertisement.
+  *
+  * Another thing to note: it is possible that an IRK is all zeroes in the
+  * resolving list. That is why we need to check if the generated address is
+  * in fact a RPA as a resolving list entry with all zeroes will use the
+  * identity address (which may be a private address or public).
+  *
+  * @param advsm
+  */
  void
  ble_ll_adv_chk_rpa_timeout(struct ble_ll_adv_sm *advsm)
  {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/controller/src/ble_ll_conn.c
----------------------------------------------------------------------
diff --cc net/nimble/controller/src/ble_ll_conn.c
index 8ec6bca,43944a8..ba8fbc4
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@@ -212,7 -215,25 +216,25 @@@ STATS_NAME_START(ble_ll_conn_stats
      STATS_NAME(ble_ll_conn_stats, mic_failures)
  STATS_NAME_END(ble_ll_conn_stats)
  
+ /**
+  * Get the event buffer allocated to send the connection complete event
+  * when we are initiating.
+  *
+  * @return uint8_t*
+  */
+ static uint8_t *
+ ble_ll_init_get_conn_comp_ev(void)
+ {
+     uint8_t *evbuf;
+ 
+     evbuf = g_ble_ll_conn_comp_ev;
+     assert(evbuf != NULL);
+     g_ble_ll_conn_comp_ev = NULL;
+ 
+     return evbuf;
+ }
+ 
 -#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.
   */
@@@ -593,13 -620,11 +621,11 @@@ ble_ll_conn_wait_txend(void *arg
  {
      struct ble_ll_conn_sm *connsm;
  
-     ble_ll_conn_current_sm_over();
- 
      connsm = (struct ble_ll_conn_sm *)arg;
-     ble_ll_event_send(&connsm->conn_ev_end);
+     ble_ll_conn_current_sm_over(connsm);
  }
  
 -#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)
  {
@@@ -2681,10 -2737,10 +2738,10 @@@ ble_ll_conn_rx_isr_end(uint8_t *rxbuf, 
           */
          hdr_sn = hdr_byte & BLE_LL_DATA_HDR_SN_MASK;
          conn_nesn = connsm->next_exp_seqnum;
-         if ((hdr_sn && conn_nesn) || (!hdr_sn && !conn_nesn)) {
+         if (rxpdu && ((hdr_sn && conn_nesn) || (!hdr_sn && !conn_nesn))) {
              connsm->next_exp_seqnum ^= 1;
 -#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
 -            if (CONN_F_ENCRYPTED(connsm) && !ble_ll_conn_is_empty_pdu(rxbuf)) {
 +#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

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/controller/src/ble_ll_conn_hci.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/controller/src/ble_ll_conn_priv.h
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/controller/src/ble_ll_hci.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/controller/src/ble_ll_hci_ev.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/controller/src/ble_ll_resolv.c
----------------------------------------------------------------------
diff --cc net/nimble/controller/src/ble_ll_resolv.c
index 608da8c,903c95f..a507d19
--- a/net/nimble/controller/src/ble_ll_resolv.c
+++ b/net/nimble/controller/src/ble_ll_resolv.c
@@@ -31,15 -30,18 +31,18 @@@
  #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;
- 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_data
+ {
+     uint8_t addr_res_enabled;
+     uint8_t rl_size;
+     uint8_t rl_cnt;
+     uint32_t rpa_tmo;
+     struct os_callout_func rpa_timer;
+ };
+ struct ble_ll_resolv_data g_ble_ll_resolv_data;
  
 -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
@@@ -455,15 -553,19 +554,19 @@@ ble_ll_resolv_init(void
      uint8_t hw_size;
  
      /* Default is 15 minutes */
-     g_ble_ll_resolv_rpa_tmo = 15 * 60 * OS_TICKS_PER_SEC;
+     g_ble_ll_resolv_data.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;
+     g_ble_ll_resolv_data.rl_size = hw_size;
  
+     os_callout_func_init(&g_ble_ll_resolv_data.rpa_timer,
+                          &g_ble_ll_data.ll_evq,
+                          ble_ll_resolv_rpa_timer_cb,
+                          NULL);
  }
  
 -#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/c5901fcc/net/nimble/controller/src/ble_ll_scan.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/drivers/native/src/ble_phy.c
----------------------------------------------------------------------
diff --cc net/nimble/drivers/native/src/ble_phy.c
index d19e54b,ff195fc..0f1ef5a
--- a/net/nimble/drivers/native/src/ble_phy.c
+++ b/net/nimble/drivers/native/src/ble_phy.c
@@@ -18,10 -18,12 +18,13 @@@
   */
  
  #include <stdint.h>
+ #include <string.h>
  #include <assert.h>
 +#include "syscfg/syscfg.h"
  #include "os/os.h"
- #include "nimble/ble.h"             /* XXX: needed for ble mbuf header.*/
+ #include "ble/xcvr.h"
+ #include "nimble/ble.h"
+ #include "nimble/nimble_opt.h"
  #include "controller/ble_phy.h"
  #include "controller/ble_ll.h"
  

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/drivers/nrf51/src/ble_phy.c
----------------------------------------------------------------------
diff --cc net/nimble/drivers/nrf51/src/ble_phy.c
index db6618f,a24a434..b8d2f6b
--- a/net/nimble/drivers/nrf51/src/ble_phy.c
+++ b/net/nimble/drivers/nrf51/src/ble_phy.c
@@@ -27,13 -26,8 +27,8 @@@
  #include "nimble/nimble_opt.h"
  #include "controller/ble_phy.h"
  #include "controller/ble_ll.h"
 -#include "mcu/nrf51_bitfields.h"
 +#include "nrf51_bitfields.h"
  
- /*
-  * XXX: need to make the copy from mbuf into the PHY data structures 32-bit
-  * copies or we are screwed.
-  */
- 
  /* XXX: 4) Make sure RF is higher priority interrupt than schedule */
  
  /*
@@@ -93,9 -88,10 +89,10 @@@ struct ble_phy_obj g_ble_phy_data
  
  /* XXX: if 27 byte packets desired we can make this smaller */
  /* Global transmit/receive buffer */
- static uint32_t g_ble_phy_txrx_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
+ static uint32_t g_ble_phy_tx_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
+ static uint32_t g_ble_phy_rx_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
@@@ -242,11 -284,16 +285,16 @@@ nrf_wait_disabled(void
  static void
  ble_phy_rx_xcvr_setup(void)
  {
- #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+     uint8_t *dptr;
+ 
+     dptr = (uint8_t *)&g_ble_phy_rx_buf[0];
+ 
 -#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
++#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
      if (g_ble_phy_data.phy_encrypted) {
+         dptr += 3;
          NRF_RADIO->PACKETPTR = (uint32_t)&g_ble_phy_enc_buf[0];
          NRF_CCM->INPTR = (uint32_t)&g_ble_phy_enc_buf[0];
-         NRF_CCM->OUTPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
+         NRF_CCM->OUTPTR = (uint32_t)dptr;
          NRF_CCM->SCRATCHPTR = (uint32_t)&g_nrf_encrypt_scratchpad[0];
          NRF_CCM->MODE = CCM_MODE_MODE_Decryption;
          NRF_CCM->CNFPTR = (uint32_t)&g_nrf_ccm_data;
@@@ -255,14 -302,16 +303,16 @@@
          NRF_CCM->EVENTS_ENDCRYPT = 0;
          NRF_PPI->CHENSET = PPI_CHEN_CH24_Msk | PPI_CHEN_CH25_Msk;
      } else {
-         NRF_RADIO->PACKETPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
+         NRF_RADIO->PACKETPTR = (uint32_t)dptr;
      }
  #else
-     NRF_RADIO->PACKETPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
+     NRF_RADIO->PACKETPTR = (uint32_t)dptr;
  #endif
  
- #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 -#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
++#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
      if (g_ble_phy_data.phy_privacy) {
+         dptr += 3;
+         NRF_RADIO->PACKETPTR = (uint32_t)dptr;
          NRF_RADIO->PCNF0 = (6 << RADIO_PCNF0_LFLEN_Pos) |
                             (2 << RADIO_PCNF0_S1LEN_Pos) |
                             (NRF_S0_LEN << RADIO_PCNF0_S0LEN_Pos);
@@@ -411,7 -464,7 +465,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 (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
 -#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
++#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
          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)) {
@@@ -442,11 -495,7 +496,7 @@@
  #endif
      }
  
-     /* Call Link Layer receive payload function */
-     rxpdu = g_ble_phy_data.rxpdu;
-     g_ble_phy_data.rxpdu = NULL;
- 
- #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1) || (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
 -#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1) || (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
++#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) || MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
      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.

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/drivers/nrf52/src/ble_phy.c
----------------------------------------------------------------------
diff --cc net/nimble/drivers/nrf52/src/ble_phy.c
index 46bb39a,2e34669..6498a06
--- a/net/nimble/drivers/nrf52/src/ble_phy.c
+++ b/net/nimble/drivers/nrf52/src/ble_phy.c
@@@ -28,13 -27,8 +28,8 @@@
  #include "nimble/nimble_opt.h"
  #include "controller/ble_phy.h"
  #include "controller/ble_ll.h"
 -#include "mcu/nrf52_bitfields.h"
 +#include "nrf52_bitfields.h"
  
- /*
-  * XXX: need to make the copy from mbuf into the PHY data structures 32-bit
-  * copies or we are screwed.
-  */
- 
  /* XXX: 4) Make sure RF is higher priority interrupt than schedule */
  
  /*
@@@ -88,9 -82,10 +83,10 @@@ struct ble_phy_obj g_ble_phy_data
  
  /* XXX: if 27 byte packets desired we can make this smaller */
  /* Global transmit/receive buffer */
- static uint32_t g_ble_phy_txrx_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
+ static uint32_t g_ble_phy_tx_buf[(BLE_PHY_MAX_PDU_LEN + 3) / 4];
+ static uint32_t g_ble_phy_rx_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
@@@ -244,7 -285,12 +286,12 @@@ nrf_wait_disabled(void
  static void
  ble_phy_rx_xcvr_setup(void)
  {
- #if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+     uint8_t *dptr;
+ 
+     dptr = (uint8_t *)&g_ble_phy_rx_buf[0];
+     dptr += 3;
+ 
 -#if (BLE_LL_CFG_FEAT_LE_ENCRYPTION == 1)
++#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
      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];
@@@ -257,13 -303,13 +304,13 @@@
          NRF_CCM->EVENTS_ENDCRYPT = 0;
          NRF_PPI->CHENSET = PPI_CHEN_CH24_Msk | PPI_CHEN_CH25_Msk;
      } else {
-         NRF_RADIO->PACKETPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
+         NRF_RADIO->PACKETPTR = (uint32_t)dptr;
      }
  #else
-     NRF_RADIO->PACKETPTR = (uint32_t)g_ble_phy_data.rxpdu->om_data;
+     NRF_RADIO->PACKETPTR = (uint32_t)dptr;
  #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];
@@@ -827,12 -870,14 +871,14 @@@ ble_phy_tx(struct os_mbuf *txpdu, uint8
      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];
+         ++dptr;
+         pktptr = (uint8_t *)&g_ble_phy_tx_buf[0];
          NRF_CCM->SHORTS = 1;
-         NRF_CCM->INPTR = (uint32_t)&g_ble_phy_enc_buf[0];
-         NRF_CCM->OUTPTR = (uint32_t)&g_ble_phy_txrx_buf[0];
+         NRF_CCM->INPTR = (uint32_t)dptr;
+         NRF_CCM->OUTPTR = (uint32_t)pktptr;
          NRF_CCM->SCRATCHPTR = (uint32_t)&g_nrf_encrypt_scratchpad[0];
          NRF_CCM->EVENTS_ERROR = 0;
          NRF_CCM->MODE = CCM_MODE_LENGTH_Msk;
@@@ -844,13 -889,17 +890,17 @@@
          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];
+         dptr = (uint8_t *)&g_ble_phy_tx_buf[0];
+         ++dptr;
+         pktptr = dptr;
      }
  #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];
+     dptr = (uint8_t *)&g_ble_phy_tx_buf[0];
+     ++dptr;
+     pktptr = dptr;
  #endif
  
      /* RAM representation has S0, LENGTH and S1 fields. (3 bytes) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/services/gap/include/services/gap/ble_svc_gap.h
----------------------------------------------------------------------
diff --cc net/nimble/host/services/gap/include/services/gap/ble_svc_gap.h
index 0000000,95d4226..dcc2712
mode 000000,100644..100644
--- a/net/nimble/host/services/gap/include/services/gap/ble_svc_gap.h
+++ b/net/nimble/host/services/gap/include/services/gap/ble_svc_gap.h
@@@ -1,0 -1,39 +1,39 @@@
+ /**
+  * 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_SVC_GAP_
+ #define H_BLE_SVC_GAP_
+ 
+ struct ble_hs_cfg;
+ 
+ #define BLE_SVC_GAP_UUID16                                  0x1800
+ #define BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME                  0x2a00
+ #define BLE_SVC_GAP_CHR_UUID16_APPEARANCE                   0x2a01
+ #define BLE_SVC_GAP_CHR_UUID16_PERIPH_PRIV_FLAG             0x2a02
+ #define BLE_SVC_GAP_CHR_UUID16_RECONNECT_ADDR               0x2a03
+ #define BLE_SVC_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS      0x2a04
+ 
+ #define BLE_SVC_GAP_APPEARANCE_GEN_COMPUTER                 128
+ 
+ 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/c5901fcc/net/nimble/host/services/gap/src/ble_svc_gap.c
----------------------------------------------------------------------
diff --cc net/nimble/host/services/gap/src/ble_svc_gap.c
index 0000000,1a2e8a4..eb67b6c
mode 000000,100644..100644
--- a/net/nimble/host/services/gap/src/ble_svc_gap.c
+++ b/net/nimble/host/services/gap/src/ble_svc_gap.c
@@@ -1,0 -1,167 +1,167 @@@
+ /**
+  * 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 "host/ble_hs.h"
+ #include "services/gap/ble_svc_gap.h"
+ 
+ /* XXX: This should be configurable. */
+ #define BLE_SVC_GAP_NAME_MAX_LEN    31
+ 
+ static char ble_svc_gap_name[BLE_SVC_GAP_NAME_MAX_LEN + 1] = "nimble";
+ static uint16_t ble_svc_gap_appearance;
+ static uint8_t ble_svc_gap_privacy_flag;
+ static uint8_t ble_svc_gap_reconnect_addr[6];
+ static uint8_t ble_svc_gap_pref_conn_params[8];
+ 
+ static int
+ ble_svc_gap_access(uint16_t conn_handle, uint16_t attr_handle,
+                    struct ble_gatt_access_ctxt *ctxt, void *arg);
+ 
+ static const struct ble_gatt_svc_def ble_svc_gap_defs[] = {
+     {
+         /*** Service: GAP. */
+         .type = BLE_GATT_SVC_TYPE_PRIMARY,
+         .uuid128 = BLE_UUID16(BLE_SVC_GAP_UUID16),
+         .characteristics = (struct ble_gatt_chr_def[]) { {
+             /*** Characteristic: Device Name. */
+             .uuid128 = BLE_UUID16(BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME),
+             .access_cb = ble_svc_gap_access,
+             .flags = BLE_GATT_CHR_F_READ,
+         }, {
+             /*** Characteristic: Appearance. */
+             .uuid128 = BLE_UUID16(BLE_SVC_GAP_CHR_UUID16_APPEARANCE),
+             .access_cb = ble_svc_gap_access,
+             .flags = BLE_GATT_CHR_F_READ,
+         }, {
+             /*** Characteristic: Peripheral Privacy Flag. */
+             .uuid128 = BLE_UUID16(BLE_SVC_GAP_CHR_UUID16_PERIPH_PRIV_FLAG),
+             .access_cb = ble_svc_gap_access,
+             .flags = BLE_GATT_CHR_F_READ,
+         }, {
+             /*** Characteristic: Reconnection Address. */
+             .uuid128 = BLE_UUID16(BLE_SVC_GAP_CHR_UUID16_RECONNECT_ADDR),
+             .access_cb = ble_svc_gap_access,
+             .flags = BLE_GATT_CHR_F_WRITE,
+         }, {
+             /*** Characteristic: Peripheral Preferred Connection Parameters. */
+             .uuid128 =
+                 BLE_UUID16(BLE_SVC_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS),
+             .access_cb = ble_svc_gap_access,
+             .flags = BLE_GATT_CHR_F_READ,
+         }, {
+             0, /* No more characteristics in this service. */
+         } },
+     },
+ 
+     {
+         0, /* No more services. */
+     },
+ };
+ 
+ static int
+ ble_svc_gap_access(uint16_t conn_handle, uint16_t attr_handle,
+                    struct ble_gatt_access_ctxt *ctxt, void *arg)
+ {
+     uint16_t uuid16;
+     int rc;
+ 
+     uuid16 = ble_uuid_128_to_16(ctxt->chr->uuid128);
+     assert(uuid16 != 0);
+ 
+     switch (uuid16) {
+     case BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME:
+         assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
+         rc = os_mbuf_append(ctxt->om, ble_svc_gap_name,
+                             strlen(ble_svc_gap_name));
+         return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+ 
+     case BLE_SVC_GAP_CHR_UUID16_APPEARANCE:
+         assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
+         rc = os_mbuf_append(ctxt->om, &ble_svc_gap_appearance,
+                             sizeof ble_svc_gap_appearance);
+         return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+ 
+     case BLE_SVC_GAP_CHR_UUID16_PERIPH_PRIV_FLAG:
+         assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
+         rc = os_mbuf_append(ctxt->om, &ble_svc_gap_privacy_flag,
+                             sizeof ble_svc_gap_privacy_flag);
+         return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+ 
+     case BLE_SVC_GAP_CHR_UUID16_RECONNECT_ADDR:
+         assert(ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR);
+         if (OS_MBUF_PKTLEN(ctxt->om) != sizeof ble_svc_gap_reconnect_addr) {
+             return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
+         }
+         ble_hs_mbuf_to_flat(ctxt->om, ble_svc_gap_reconnect_addr,
+                             sizeof ble_svc_gap_reconnect_addr, NULL);
+         return 0;
+ 
+     case BLE_SVC_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS:
+         assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
+         rc = os_mbuf_append(ctxt->om, &ble_svc_gap_pref_conn_params,
+                             sizeof ble_svc_gap_pref_conn_params);
+         return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+ 
+     default:
+         assert(0);
+         return BLE_ATT_ERR_UNLIKELY;
+     }
+ }
+ 
+ const char *
+ ble_svc_gap_device_name(void)
+ {
+     return ble_svc_gap_name;
+ }
+ 
+ int
+ ble_svc_gap_device_name_set(const char *name)
+ {
+     int len;
+ 
+     len = strlen(name);
+     if (len > BLE_SVC_GAP_NAME_MAX_LEN) {
+         return BLE_HS_EINVAL;
+     }
+ 
+     memcpy(ble_svc_gap_name, name, len);
+     ble_svc_gap_name[len] = '\0';
+ 
+     return 0;
+ }
+ 
+ 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;
+     }
+ 
+     rc = ble_gatts_add_svcs(ble_svc_gap_defs);
+     if (rc != 0) {
+         return rc;
+     }
+ 
+     return 0;
+ }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/services/gatt/pkg.yml
----------------------------------------------------------------------
diff --cc net/nimble/host/services/gatt/pkg.yml
index 0000000,b8fdabe..54343bd
mode 000000,100644..100644
--- a/net/nimble/host/services/gatt/pkg.yml
+++ b/net/nimble/host/services/gatt/pkg.yml
@@@ -1,0 -1,31 +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: net/nimble/host/services/gatt
+ pkg.description: Implements the GATT service.
+ pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+ pkg.homepage: "http://mynewt.apache.org/"
+ pkg.keywords:
+     - ble
+     - bluetooth
+     - nimble
+     - gatt
+ 
+ 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/c5901fcc/net/nimble/host/services/gatt/src/ble_svc_gatt.c
----------------------------------------------------------------------
diff --cc net/nimble/host/services/gatt/src/ble_svc_gatt.c
index 0000000,74d3ac9..c6426f0
mode 000000,100644..100644
--- a/net/nimble/host/services/gatt/src/ble_svc_gatt.c
+++ b/net/nimble/host/services/gatt/src/ble_svc_gatt.c
@@@ -1,0 -1,90 +1,90 @@@
+ /**
+  * 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 "host/ble_hs.h"
+ #include "services/gatt/ble_svc_gatt.h"
+ 
+ static int
+ ble_svc_gatt_access(uint16_t conn_handle, uint16_t attr_handle,
+                     struct ble_gatt_access_ctxt *ctxt, void *arg);
+ 
+ static const struct ble_gatt_svc_def ble_svc_gatt_defs[] = {
+     {
+         /*** Service: GATT */
+         .type = BLE_GATT_SVC_TYPE_PRIMARY,
+         .uuid128 = BLE_UUID16(BLE_GATT_SVC_UUID16),
+         .characteristics = (struct ble_gatt_chr_def[]) { {
+             .uuid128 = BLE_UUID16(BLE_SVC_GATT_CHR_SERVICE_CHANGED_UUID16),
+             .access_cb = ble_svc_gatt_access,
+             .flags = BLE_GATT_CHR_F_INDICATE,
+         }, {
+             0, /* No more characteristics in this service. */
+         } },
+     },
+ 
+     {
+         0, /* No more services. */
+     },
+ };
+ 
+ static int
+ ble_svc_gatt_access(uint16_t conn_handle, uint16_t attr_handle,
+                     struct ble_gatt_access_ctxt *ctxt, void *arg)
+ {
+     uint8_t *u8;
+ 
+     /* The only operation allowed for this characteristic is indicate.  This
+      * access callback gets called by the stack when it needs to read the
+      * characteristic value to populate the outgoing indication command.
+      * Therefore, this callback should only get called during an attempt to
+      * read the characteristic.
+      */
+     assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
+     assert(ctxt->chr == &ble_svc_gatt_defs[0].characteristics[0]);
+ 
+     /* XXX: For now, always respond with 0 (unchanged). */
+     u8 = os_mbuf_extend(ctxt->om, 1);
+     if (u8 == NULL) {
+         return BLE_ATT_ERR_INSUFFICIENT_RES;
+     }
+ 
+     *u8 = 0;
+ 
+     return 0;
+ }
+ 
+ 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;
+     }
+ 
+     rc = ble_gatts_add_svcs(ble_svc_gatt_defs);
+     if (rc != 0) {
+         return rc;
+     }
+ 
+     return 0;
+ }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_att_clt.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_att_priv.h
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_att_svr.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_gap.c
----------------------------------------------------------------------
diff --cc net/nimble/host/src/ble_gap.c
index 81be873,2699e72..5014a86
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@@ -770,8 -897,12 +895,13 @@@ ble_gap_rx_update_complete(struct hci_l
      return;
  #endif
  
++    struct ble_gap_update_entry *entry;
+     struct ble_l2cap_sig_update_params l2cap_params;
      struct ble_gap_event event;
      struct ble_hs_conn *conn;
+     int cb_status;
+     int call_cb;
+     int rc;
  
      STATS_INC(ble_gap_stats, rx_update_complete);
  

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_gattc.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_gatts.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_hs.c
----------------------------------------------------------------------
diff --cc net/nimble/host/src/ble_hs.c
index 84a69a6,1899a74..85b524b
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@@ -60,13 -50,15 +60,15 @@@ static struct os_event ble_hs_event_res
  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;
  
+ #define BLE_HS_SYNC_RETRY_RATE          (OS_TICKS_PER_SEC / 10)    
+ 
  /**
   * Handles unresponsive timeouts and periodic retries in case of resource
   * shortage.
@@@ -406,25 -394,25 +409,41 @@@ voi
  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
 +ble_hs_enqueue_hci_event(uint8_t *hci_evt)
 +{
 +    struct os_event *ev;
 +
 +    ev = os_memblock_get(&ble_hs_hci_ev_pool);
 +    if (ev == NULL) {
 +        ble_hci_trans_buf_free(ev->ev_arg);
 +    } else {
 +        ev->ev_queued = 0;
 +        ev->ev_type = BLE_HOST_HCI_EVENT_CTLR_EVENT;
 +        ev->ev_arg = hci_evt;
 +        ble_hs_event_enqueue(ev);
 +    }
  }
  
+ void
+ ble_hs_enqueue_hci_event(uint8_t *hci_evt)
+ {
+     struct os_event *ev;
+ 
+     ev = os_memblock_get(&ble_hs_hci_ev_pool);
+     if (ev == NULL) {
+         ble_hci_trans_buf_free(hci_evt);
+     } else {
+         ev->ev_queued = 0;
+         ev->ev_type = BLE_HOST_HCI_EVENT_CTLR_EVENT;
+         ev->ev_arg = hci_evt;
+         ble_hs_event_enqueue(ev);
+     }
+ }
+ 
  /**
   * Schedules for all pending notifications and indications to be sent in the
   * host parent task.
@@@ -562,15 -548,31 +581,14 @@@ 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);
+     log_register("ble_hs", &ble_hs_log, &log_console_handler, NULL);
  
 -    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);
@@@ -619,6 -649,9 +637,5 @@@
      /* 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);
  
-     /* Configure storage mechanism. */
-     /* XXX */
+     return 0;
 -
 -err:
 -    ble_hs_free_mem();
 -    return rc;
  }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_hs_adv.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_hs_conn.c
----------------------------------------------------------------------
diff --cc net/nimble/host/src/ble_hs_conn.c
index c0d08d0,34d9590..4d1f60c
--- a/net/nimble/host/src/ble_hs_conn.c
+++ b/net/nimble/host/src/ble_hs_conn.c
@@@ -249,9 -245,20 +249,20 @@@ ble_hs_conn_find(uint16_t conn_handle
  }
  
  struct ble_hs_conn *
+ ble_hs_conn_find_assert(uint16_t conn_handle)
+ {
+     struct ble_hs_conn *conn;
+ 
+     conn = ble_hs_conn_find(conn_handle);
+     BLE_HS_DBG_ASSERT(conn != NULL);
+ 
+     return conn;
+ }
+ 
+ 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
  

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_hs_misc.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_hs_priv.h
----------------------------------------------------------------------
diff --cc net/nimble/host/src/ble_hs_priv.h
index 1d77ae6,6bebec9..2a3ce20
--- a/net/nimble/host/src/ble_hs_priv.h
+++ b/net/nimble/host/src/ble_hs_priv.h
@@@ -98,11 -89,15 +98,11 @@@ int ble_hs_hci_evt_acl_process(struct o
  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);
- int ble_hs_misc_conn_chan_find_reqd(uint16_t conn_handle, uint16_t cid,
-                                     struct ble_hs_conn **out_conn,
-                                     struct ble_l2cap_chan **out_chan);
+ void ble_hs_misc_conn_chan_find_reqd(uint16_t conn_handle, uint16_t cid,
+                                      struct ble_hs_conn **out_conn,
+                                      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);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_l2cap_sig.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_sm.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_sm_cmd.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_sm_lgcy.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_sm_priv.h
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/src/ble_sm_sc.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/test/src/ble_att_clt_test.c
----------------------------------------------------------------------
diff --cc net/nimble/host/test/src/ble_att_clt_test.c
index ab89e9d,0000000..14d66f6
mode 100644,000000..100644
--- a/net/nimble/host/test/src/ble_att_clt_test.c
+++ b/net/nimble/host/test/src/ble_att_clt_test.c
@@@ -1,536 -1,0 +1,567 @@@
 +/**
 + * 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);
 +}
 +
++static void
++ble_att_clt_test_misc_tx_mtu(uint16_t conn_handle, uint16_t mtu, int status)
++{
++    struct ble_att_mtu_cmd req;
++    int rc;
++
++    req.bamc_mtu = mtu;
++    rc = ble_att_clt_tx_mtu(conn_handle, &req);
++    TEST_ASSERT(rc == status);
++
++    if (rc == 0) {
++        ble_hs_test_util_verify_tx_mtu_cmd(1, mtu);
++    }
++}
++
++
 +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_CASE(ble_att_clt_test_tx_mtu)
++{
++    uint16_t conn_handle;
++
++    conn_handle = ble_att_clt_test_misc_init();
++
++    /*** Success. */
++    ble_att_clt_test_misc_tx_mtu(conn_handle, 50, 0);
++
++    /*** Error: repeated sends. */
++    ble_att_clt_test_misc_tx_mtu(conn_handle, 50, BLE_HS_EALREADY);
++    ble_att_clt_test_misc_tx_mtu(conn_handle, 60, BLE_HS_EALREADY);
++}
++
 +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();
++    ble_att_clt_test_tx_mtu();
 +}
 +
 +int
 +ble_att_clt_test_all(void)
 +{
 +    ble_att_clt_suite();
 +
 +    return tu_any_failed;
 +}


[47/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-392 STM32F4 hal_uart_init() func missing

Posted by vi...@apache.org.
MYNEWT-392 STM32F4 hal_uart_init() func missing

- Adding hal_uart_init() function which passes the config looking up the
  uart port. Since uart_config takes care of setting the interrupt
  vector and enabling it, not changing that.


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

Branch: refs/heads/sterly_refactor
Commit: c10e6631060d44bc9ac2ae62f7b5a56bf1246033
Parents: d4c5807
Author: Vipul Rahane <vi...@runtime.io>
Authored: Thu Sep 22 13:11:03 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Thu Sep 22 20:21:03 2016 -0700

----------------------------------------------------------------------
 hw/mcu/stm/stm32f4xx/src/hal_uart.c | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c10e6631/hw/mcu/stm/stm32f4xx/src/hal_uart.c
----------------------------------------------------------------------
diff --git a/hw/mcu/stm/stm32f4xx/src/hal_uart.c b/hw/mcu/stm/stm32f4xx/src/hal_uart.c
index 685e3d2..3f78359 100644
--- a/hw/mcu/stm/stm32f4xx/src/hal_uart.c
+++ b/hw/mcu/stm/stm32f4xx/src/hal_uart.c
@@ -261,6 +261,24 @@ hal_uart_set_nvic(IRQn_Type irqn, struct hal_uart *uart)
 }
 
 int
+hal_uart_init(int port, void *arg)
+{
+    struct hal_uart *u;
+
+    if (port != 0) {
+        return -1;
+    }
+
+    u = &uarts[port];
+    if (u->u_open) {
+        return -1;
+    }
+    u->u_cfg = arg;
+
+    return 0;
+}
+
+int
 hal_uart_config(int port, int32_t baudrate, uint8_t databits, uint8_t stopbits,
   enum hal_uart_parity parity, enum hal_uart_flow_ctl flow_ctl)
 {


[31/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-390: NRF hal_gpio_read does not return 1 when pin high

Posted by vi...@apache.org.
MYNEWT-390: NRF hal_gpio_read does not return 1 when pin high

This API is supposed to return 1 but was returning 1 << pin. This
has now been fixed so if the pin is high, 1 is returned.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/72c74e5b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/72c74e5b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/72c74e5b

Branch: refs/heads/sterly_refactor
Commit: 72c74e5b138f272c466d35bdd399330ec9de4c1f
Parents: a87a31d
Author: William San Filippo <wi...@runtime.io>
Authored: Wed Sep 21 21:53:07 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Wed Sep 21 21:53:07 2016 -0700

----------------------------------------------------------------------
 hw/mcu/nordic/nrf51xxx/src/hal_gpio.c | 2 +-
 hw/mcu/nordic/nrf52xxx/src/hal_gpio.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/72c74e5b/hw/mcu/nordic/nrf51xxx/src/hal_gpio.c
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf51xxx/src/hal_gpio.c b/hw/mcu/nordic/nrf51xxx/src/hal_gpio.c
index e37851d..58e6fac 100644
--- a/hw/mcu/nordic/nrf51xxx/src/hal_gpio.c
+++ b/hw/mcu/nordic/nrf51xxx/src/hal_gpio.c
@@ -414,7 +414,7 @@ void hal_gpio_write(int pin, int val)
  */
 int hal_gpio_read(int pin)
 {
-    return (NRF_GPIO->IN & GPIO_MASK(pin));
+    return ((NRF_GPIO->IN & GPIO_MASK(pin)) != 0);
 }
 
 /**

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/72c74e5b/hw/mcu/nordic/nrf52xxx/src/hal_gpio.c
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf52xxx/src/hal_gpio.c b/hw/mcu/nordic/nrf52xxx/src/hal_gpio.c
index a9536f3..ec5e31e 100644
--- a/hw/mcu/nordic/nrf52xxx/src/hal_gpio.c
+++ b/hw/mcu/nordic/nrf52xxx/src/hal_gpio.c
@@ -160,7 +160,7 @@ void hal_gpio_write(int pin, int val)
  */
 int hal_gpio_read(int pin)
 {
-    return (NRF_P0->IN & HAL_GPIO_MASK(pin));
+    return ((NRF_P0->IN & HAL_GPIO_MASK(pin)) != 0);
 }
 
 /**


[04/50] [abbrv] incubator-mynewt-core git commit: iotivity; fix sending partial data when error happens while constructing discovery response.

Posted by vi...@apache.org.
iotivity; fix sending partial data when error happens while constructing
discovery response.


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

Branch: refs/heads/sterly_refactor
Commit: b43a21f389948d62ba81219d944b6a308ac04a23
Parents: c1aab98
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Sep 20 14:09:08 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Tue Sep 20 14:09:08 2016 -0700

----------------------------------------------------------------------
 libs/iotivity/src/api/oc_discovery.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/b43a21f3/libs/iotivity/src/api/oc_discovery.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/api/oc_discovery.c b/libs/iotivity/src/api/oc_discovery.c
index 34d34b8..e8ca760 100644
--- a/libs/iotivity/src/api/oc_discovery.c
+++ b/libs/iotivity/src/api/oc_discovery.c
@@ -156,7 +156,7 @@ oc_core_discovery_handler(oc_request_t *request, oc_interface_mask_t interface)
 
   int response_length = oc_rep_finalize();
 
-  if (matches && response_length) {
+  if (matches && response_length > 0) {
     request->response->response_buffer->response_length = response_length;
     request->response->response_buffer->code = oc_status_code(OC_STATUS_OK);
   } else {


[05/50] [abbrv] incubator-mynewt-core git commit: newtmgr; clean up nmgr API use a bit.

Posted by vi...@apache.org.
newtmgr; clean up nmgr API use a bit.


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

Branch: refs/heads/sterly_refactor
Commit: afc5fcafb45e677a0008bb4aa2426b57ee8a5e7f
Parents: b43a21f
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Sep 20 16:42:12 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Tue Sep 20 16:42:12 2016 -0700

----------------------------------------------------------------------
 libs/newtmgr/include/newtmgr/newtmgr.h |  2 --
 libs/newtmgr/src/newtmgr.c             |  4 ++--
 sys/log/src/log_nmgr.c                 | 10 +++++-----
 sys/stats/src/stats_nmgr.c             | 20 ++++++++++----------
 4 files changed, 17 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/afc5fcaf/libs/newtmgr/include/newtmgr/newtmgr.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/include/newtmgr/newtmgr.h b/libs/newtmgr/include/newtmgr/newtmgr.h
index ab84806..176a006 100644
--- a/libs/newtmgr/include/newtmgr/newtmgr.h
+++ b/libs/newtmgr/include/newtmgr/newtmgr.h
@@ -80,9 +80,7 @@ struct nmgr_jbuf {
     uint16_t njb_off;
     uint16_t njb_end;
 };
-int nmgr_jbuf_init(struct nmgr_jbuf *njb);
 int nmgr_jbuf_setoerr(struct nmgr_jbuf *njb, int errcode);
-extern struct nmgr_jbuf nmgr_task_jbuf;
 
 typedef int (*nmgr_handler_func_t)(struct nmgr_jbuf *);
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/afc5fcaf/libs/newtmgr/src/newtmgr.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/src/newtmgr.c b/libs/newtmgr/src/newtmgr.c
index 2bd88b4..f9d3846 100644
--- a/libs/newtmgr/src/newtmgr.c
+++ b/libs/newtmgr/src/newtmgr.c
@@ -41,7 +41,7 @@ STAILQ_HEAD(, nmgr_group) g_nmgr_group_list =
 
 /* JSON buffer for NMGR task
  */
-struct nmgr_jbuf nmgr_task_jbuf;
+static struct nmgr_jbuf nmgr_task_jbuf;
 
 int
 nmgr_group_list_lock(void)
@@ -259,7 +259,7 @@ err:
     return (rc);
 }
 
-int
+static int
 nmgr_jbuf_init(struct nmgr_jbuf *njb)
 {
     memset(njb, 0, sizeof(*njb));

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/afc5fcaf/sys/log/src/log_nmgr.c
----------------------------------------------------------------------
diff --git a/sys/log/src/log_nmgr.c b/sys/log/src/log_nmgr.c
index b1e90a0..b3e7bb9 100644
--- a/sys/log/src/log_nmgr.c
+++ b/sys/log/src/log_nmgr.c
@@ -263,7 +263,7 @@ log_nmgr_read(struct nmgr_jbuf *njb)
         return rc;
     }
 
-    encoder = (struct json_encoder *) &nmgr_task_jbuf.njb_enc;
+    encoder = (struct json_encoder *) &njb->njb_enc;
 
     json_encode_object_start(encoder);
     json_encode_array_name(encoder, "logs");
@@ -325,7 +325,7 @@ log_nmgr_module_list(struct nmgr_jbuf *njb)
     int module;
     char *str;
 
-    encoder = (struct json_encoder *) &nmgr_task_jbuf.njb_enc;
+    encoder = (struct json_encoder *) &njb->njb_enc;
 
     json_encode_object_start(encoder);
     JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
@@ -366,7 +366,7 @@ log_nmgr_logs_list(struct nmgr_jbuf *njb)
     struct json_encoder *encoder;
     struct log *log;
 
-    encoder = (struct json_encoder *) &nmgr_task_jbuf.njb_enc;
+    encoder = (struct json_encoder *) &njb->njb_enc;
 
     json_encode_object_start(encoder);
     JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
@@ -408,7 +408,7 @@ log_nmgr_level_list(struct nmgr_jbuf *njb)
     int level;
     char *str;
 
-    encoder = (struct json_encoder *) &nmgr_task_jbuf.njb_enc;
+    encoder = (struct json_encoder *) &njb->njb_enc;
 
     json_encode_object_start(encoder);
     JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
@@ -465,7 +465,7 @@ log_nmgr_clear(struct nmgr_jbuf *njb)
         }
     }
 
-    encoder = (struct json_encoder *) &nmgr_task_jbuf.njb_enc;
+    encoder = (struct json_encoder *) &njb->njb_enc;
 
     json_encode_object_start(encoder);
     json_encode_object_finish(encoder);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/afc5fcaf/sys/stats/src/stats_nmgr.c
----------------------------------------------------------------------
diff --git a/sys/stats/src/stats_nmgr.c b/sys/stats/src/stats_nmgr.c
index c7b8dff..c8d62c5 100644
--- a/sys/stats/src/stats_nmgr.c
+++ b/sys/stats/src/stats_nmgr.c
@@ -124,14 +124,14 @@ stats_nmgr_read(struct nmgr_jbuf *njb)
 
     json_encode_object_start(&njb->njb_enc);
     JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
-    json_encode_object_entry(&nmgr_task_jbuf.njb_enc, "rc", &jv);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
     JSON_VALUE_STRINGN(&jv, stats_name, strlen(stats_name));
-    json_encode_object_entry(&nmgr_task_jbuf.njb_enc, "name", &jv);
+    json_encode_object_entry(&njb->njb_enc, "name", &jv);
     JSON_VALUE_STRINGN(&jv, "sys", sizeof("sys")-1);
-    json_encode_object_entry(&nmgr_task_jbuf.njb_enc, "group", &jv);
-    json_encode_object_key(&nmgr_task_jbuf.njb_enc, "fields");
-    json_encode_object_start(&nmgr_task_jbuf.njb_enc);
-    stats_walk(hdr, stats_nmgr_walk_func, &nmgr_task_jbuf.njb_enc);
+    json_encode_object_entry(&njb->njb_enc, "group", &jv);
+    json_encode_object_key(&njb->njb_enc, "fields");
+    json_encode_object_start(&njb->njb_enc);
+    stats_walk(hdr, stats_nmgr_walk_func, &njb->njb_enc);
     json_encode_object_finish(&njb->njb_enc);
     json_encode_object_finish(&njb->njb_enc);
 
@@ -149,10 +149,10 @@ stats_nmgr_list(struct nmgr_jbuf *njb)
 
     json_encode_object_start(&njb->njb_enc);
     JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
-    json_encode_object_entry(&nmgr_task_jbuf.njb_enc, "rc", &jv);
-    json_encode_array_name(&nmgr_task_jbuf.njb_enc, "stat_list");
-    json_encode_array_start(&nmgr_task_jbuf.njb_enc);
-    stats_group_walk(stats_nmgr_encode_name, &nmgr_task_jbuf.njb_enc);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
+    json_encode_array_name(&njb->njb_enc, "stat_list");
+    json_encode_array_start(&njb->njb_enc);
+    stats_group_walk(stats_nmgr_encode_name, &njb->njb_enc);
     json_encode_array_finish(&njb->njb_enc);
     json_encode_object_finish(&njb->njb_enc);
 


[35/50] [abbrv] incubator-mynewt-core git commit: add watchdog implementation for nordic, simplify hal_watchdog by making hal_watchdog_enable() return void.

Posted by vi...@apache.org.
add watchdog implementation for nordic, simplify hal_watchdog by making hal_watchdog_enable() return void.


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

Branch: refs/heads/sterly_refactor
Commit: 9714934ad05037585f6a14c8a7b38023f2e85b7a
Parents: 7066db4
Author: Sterling Hughes <st...@apache.org>
Authored: Thu Sep 22 14:14:51 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Thu Sep 22 14:14:51 2016 -0700

----------------------------------------------------------------------
 hw/hal/include/hal/hal_watchdog.h | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9714934a/hw/hal/include/hal/hal_watchdog.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_watchdog.h b/hw/hal/include/hal/hal_watchdog.h
index e0161ce..973f40d 100644
--- a/hw/hal/include/hal/hal_watchdog.h
+++ b/hw/hal/include/hal/hal_watchdog.h
@@ -35,10 +35,8 @@ int hal_watchdog_init(int expire_secs);
 
 /*
  * Starts the watchdog.
- *
- * @return			0 on success; non-zero on failure.
  */
-int hal_watchdog_enable(void);
+void hal_watchdog_enable(void);
 
 /*
  * Tickles the watchdog. Needs to be done before 'expire_secs' fires.


[24/50] [abbrv] incubator-mynewt-core git commit: Conclude merge from sterly_refactor.

Posted by vi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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 ba8fbc4..4a553d0 100644
--- a/net/nimble/controller/src/ble_ll_conn.c
+++ b/net/nimble/controller/src/ble_ll_conn.c
@@ -234,7 +234,7 @@ ble_ll_init_get_conn_comp_ev(void)
     return evbuf;
 }
 
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
 /**
  * Called to determine if the received PDU is an empty PDU or not.
  */
@@ -625,7 +625,7 @@ ble_ll_conn_wait_txend(void *arg)
     ble_ll_conn_current_sm_over(connsm);
 }
 
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
 static void
 ble_ll_conn_start_rx_encrypt(void *arg)
 {
@@ -724,7 +724,7 @@ ble_ll_conn_chk_csm_flags(struct ble_ll_conn_sm *connsm)
 {
     uint8_t update_status;
 
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
     if (connsm->csmflags.cfbit.send_ltk_req) {
         /*
          * Send Long term key request event to host. If masked, we need to
@@ -814,7 +814,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 (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
         /*
          * If we are encrypting, we are only allowed to send certain
          * kinds of LL control PDU's. If none is enqueued, send empty pdu!
@@ -864,7 +864,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 (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
             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);
@@ -879,7 +879,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 (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
             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)) {
@@ -1002,7 +1002,7 @@ conn_tx_pdu:
         txend_func = NULL;
     }
 
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
     int is_ctrl;
     uint8_t llid;
     uint8_t opcode;
@@ -1143,7 +1143,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 (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
     ble_phy_resolv_list_disable();
 #endif
 
@@ -1151,7 +1151,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 (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
             if (CONN_F_ENCRYPTED(connsm)) {
                 ble_phy_encrypt_enable(connsm->enc_data.tx_pkt_cntr,
                                        connsm->enc_data.iv,
@@ -1173,7 +1173,7 @@ ble_ll_conn_event_start_cb(struct ble_ll_sched_item *sch)
             rc = BLE_LL_SCHED_STATE_DONE;
         }
     } else {
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
             if (CONN_F_ENCRYPTED(connsm)) {
                 ble_phy_encrypt_enable(connsm->enc_data.rx_pkt_cntr,
                                        connsm->enc_data.iv,
@@ -1288,7 +1288,7 @@ ble_ll_conn_can_send_next_pdu(struct ble_ll_conn_sm *connsm, uint32_t begtime)
     return rc;
 }
 
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
 /**
  * Callback for the Authenticated payload timer. This function is called
  * when the authenticated payload timer expires. When the authenticated
@@ -1490,12 +1490,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 (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
     memset(&connsm->enc_data, 0, sizeof(struct ble_ll_conn_enc_data));
     connsm->enc_data.enc_state = CONN_ENC_S_UNENCRYPTED;
 #endif
 
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
     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,
@@ -1584,7 +1584,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 (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
     os_callout_stop(&connsm->auth_pyld_timer.cf_c);
 #endif
 
@@ -1926,7 +1926,7 @@ ble_ll_conn_event_end(void *arg)
         connsm->slave_cur_tx_win_usecs = 0;
     }
 
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
     /*
      * 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
@@ -1990,7 +1990,7 @@ ble_ll_conn_req_pdu_update(struct os_mbuf *m, uint8_t *adva, uint8_t addr_type,
     struct ble_mbuf_hdr *ble_hdr;
     struct ble_ll_conn_sm *connsm;
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
     int is_rpa;
     struct ble_ll_resolv_entry *rl;
 #endif
@@ -2017,7 +2017,7 @@ ble_ll_conn_req_pdu_update(struct os_mbuf *m, uint8_t *adva, uint8_t addr_type,
     }
 
     /* XXX: do this ahead of time? Calculate the local rpa I mean */
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
     if (connsm->own_addr_type > BLE_HCI_ADV_OWN_ADDR_RANDOM) {
         rl = NULL;
         is_rpa = ble_ll_is_rpa(adva, addr_type);
@@ -2166,7 +2166,7 @@ ble_ll_conn_event_halt(void)
 void
 ble_ll_init_rx_pkt_in(uint8_t *rxbuf, struct ble_mbuf_hdr *ble_hdr)
 {
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
     int8_t rpa_index;
 #endif
     uint8_t addr_type;
@@ -2189,7 +2189,7 @@ ble_ll_init_rx_pkt_in(uint8_t *rxbuf, struct ble_mbuf_hdr *ble_hdr)
                 addr_type = BLE_HCI_CONN_PEER_ADDR_PUBLIC;
             }
 
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
             /*
              * Did we resolve this address? If so, set correct peer address
              * and peer address type.
@@ -2326,7 +2326,7 @@ ble_ll_init_rx_isr_end(uint8_t *rxbuf, uint8_t crcok,
         resolved = 0;
         chk_wl = ble_ll_scan_whitelist_enabled();
 
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
         if (ble_ll_is_rpa(adv_addr, addr_type) && ble_ll_resolv_enabled()) {
             index = ble_hw_resolv_list_match();
             if (index >= 0) {
@@ -2549,7 +2549,7 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr)
                 goto conn_rx_data_pdu_end;
             }
 
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_PING)
             /*
              * Reset authenticated payload timeout if valid MIC. NOTE: we dont
              * check the MIC failure bit as that would have terminated the
@@ -2588,7 +2588,7 @@ ble_ll_conn_rx_data_pdu(struct os_mbuf *rxpdu, struct ble_mbuf_hdr *hdr)
                     goto conn_rx_data_pdu_end;
                 }
 
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
                 /*
                  * XXX: should we check to see if we are in a state where we
                  * might expect to get an encrypted PDU?
@@ -2740,14 +2740,14 @@ ble_ll_conn_rx_isr_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr)
         conn_nesn = connsm->next_exp_seqnum;
         if (rxpdu && ((hdr_sn && conn_nesn) || (!hdr_sn && !conn_nesn))) {
             connsm->next_exp_seqnum ^= 1;
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
-            if (CONN_F_ENCRYPTED(connsm) && !ble_ll_conn_is_empty_pdu(rxpdu)) {
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
+            if (CONN_F_ENCRYPTED(connsm) && !ble_ll_conn_is_empty_pdu(rxbuf)) {
                 ++connsm->enc_data.rx_pkt_cntr;
             }
 #endif
         }
 
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
         ble_ll_log(BLE_LL_LOG_ID_CONN_RX,
                    hdr_byte,
                    (uint16_t)connsm->tx_seqnum << 8 | conn_nesn,
@@ -2785,7 +2785,7 @@ ble_ll_conn_rx_isr_end(uint8_t *rxbuf, struct ble_mbuf_hdr *rxhdr)
                  */
                 txpdu = connsm->cur_tx_pdu;
                 if (txpdu) {
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
                     if (connsm->enc_data.tx_encrypted) {
                         ++connsm->enc_data.tx_pkt_cntr;
                     }
@@ -2847,7 +2847,7 @@ chk_rx_terminate_ind:
         } else {
             /* A slave always replies */
             reply = 1;
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
             if (is_ctrl && (opcode == BLE_LL_CTRL_PAUSE_ENC_RSP)) {
                 connsm->enc_data.enc_state = CONN_ENC_S_UNENCRYPTED;
             }
@@ -2912,7 +2912,7 @@ ble_ll_conn_enqueue_pkt(struct ble_ll_conn_sm *connsm, struct os_mbuf *om,
     }
 
     lifo = 0;
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LE_ENCRYPTION)
     if (connsm->enc_data.enc_state > CONN_ENC_S_ENCRYPTED) {
         uint8_t llid;
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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 18defc4..d8a58c3 100644
--- a/net/nimble/controller/src/ble_ll_conn_hci.c
+++ b/net/nimble/controller/src/ble_ll_conn_hci.c
@@ -260,7 +260,7 @@ ble_ll_conn_num_comp_pkts_event_send(struct ble_ll_conn_sm *connsm)
      * (i.e. enqueued in a connection state machine).
      */
     if ((uint32_t)(g_ble_ll_last_num_comp_pkt_evt - os_time_get()) <
-         (NIMBLE_OPT_NUM_COMP_PKT_RATE * OS_TICKS_PER_SEC)) {
+         (MYNEWT_VAL(BLE_NUM_COMP_PKT_RATE) * OS_TICKS_PER_SEC)) {
         /*
          * If this connection has completed packets, send an event right away.
          * We do this to increase throughput but we dont want to search the
@@ -341,7 +341,7 @@ ble_ll_conn_num_comp_pkts_event_send(struct ble_ll_conn_sm *connsm)
 
     if (event_sent) {
         g_ble_ll_last_num_comp_pkt_evt = os_time_get() +
-            (NIMBLE_OPT_NUM_COMP_PKT_RATE * OS_TICKS_PER_SEC);
+            (MYNEWT_VAL(BLE_NUM_COMP_PKT_RATE) * OS_TICKS_PER_SEC);
     }
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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 0ac7e80..2d58282 100644
--- a/net/nimble/controller/src/ble_ll_scan.c
+++ b/net/nimble/controller/src/ble_ll_scan.c
@@ -387,7 +387,7 @@ ble_ll_hci_send_adv_report(uint8_t pdu_type, uint8_t txadd, uint8_t *rxbuf,
                            struct ble_ll_scan_sm *scansm)
 {
     int rc;
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
     int index;
 #endif
     uint8_t evtype;
@@ -443,7 +443,7 @@ ble_ll_hci_send_adv_report(uint8_t pdu_type, uint8_t txadd, uint8_t *rxbuf,
             }
 
             rxbuf += BLE_LL_PDU_HDR_LEN;
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
             if (BLE_MBUF_HDR_RESOLVED(hdr)) {
                 index = scansm->scan_rpa_index;
                 adv_addr = g_ble_ll_resolv_list[index].rl_identity_addr;
@@ -1105,7 +1105,7 @@ ble_ll_scan_wfr_timer_exp(void)
 void
 ble_ll_scan_rx_pkt_in(uint8_t ptype, uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
 {
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
     int index;
 #endif
     uint8_t *adv_addr;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/net/nimble/host/services/gap/include/services/gap/ble_svc_gap.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/gap/include/services/gap/ble_svc_gap.h b/net/nimble/host/services/gap/include/services/gap/ble_svc_gap.h
index dcc2712..070599d 100644
--- a/net/nimble/host/services/gap/include/services/gap/ble_svc_gap.h
+++ b/net/nimble/host/services/gap/include/services/gap/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(void);
+void ble_svc_gap_init(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/net/nimble/host/services/gap/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/gap/pkg.yml b/net/nimble/host/services/gap/pkg.yml
index b84e816..376a555 100644
--- a/net/nimble/host/services/gap/pkg.yml
+++ b/net/nimble/host/services/gap/pkg.yml
@@ -29,3 +29,6 @@ pkg.keywords:
 
 pkg.deps:
     - net/nimble/host
+
+pkg.init_function: ble_svc_gap_init
+pkg.init_stage: 3

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/net/nimble/host/services/gap/src/ble_svc_gap.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/gap/src/ble_svc_gap.c b/net/nimble/host/services/gap/src/ble_svc_gap.c
index eb67b6c..98fd749 100644
--- a/net/nimble/host/services/gap/src/ble_svc_gap.c
+++ b/net/nimble/host/services/gap/src/ble_svc_gap.c
@@ -19,6 +19,8 @@
 
 #include <assert.h>
 #include <string.h>
+
+#include "sysinit/sysinit.h"
 #include "host/ble_hs.h"
 #include "services/gap/ble_svc_gap.h"
 
@@ -148,20 +150,14 @@ ble_svc_gap_device_name_set(const char *name)
     return 0;
 }
 
-int
+void
 ble_svc_gap_init(void)
 {
     int rc;
 
     rc = ble_gatts_count_cfg(ble_svc_gap_defs);
-    if (rc != 0) {
-        return rc;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = ble_gatts_add_svcs(ble_svc_gap_defs);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
+    SYSINIT_PANIC_ASSERT(rc == 0);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/net/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h b/net/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h
index 320a3ff..adc4433 100644
--- a/net/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h
+++ b/net/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h
@@ -24,6 +24,6 @@ struct ble_hs_cfg;
 
 #define BLE_SVC_GATT_CHR_SERVICE_CHANGED_UUID16     0x2a05
 
-int ble_svc_gatt_init(struct ble_hs_cfg *cfg);
+void ble_svc_gatt_init(void);
 
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/net/nimble/host/services/gatt/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/gatt/pkg.yml b/net/nimble/host/services/gatt/pkg.yml
index 54343bd..ec0eb1e 100644
--- a/net/nimble/host/services/gatt/pkg.yml
+++ b/net/nimble/host/services/gatt/pkg.yml
@@ -30,5 +30,5 @@ pkg.keywords:
 pkg.deps:
     - net/nimble/host
 
-pkg.init_function: ble_svc_mandatory_pkg_init
+pkg.init_function: ble_svc_gatt_init
 pkg.init_stage: 3

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/net/nimble/host/services/gatt/src/ble_svc_gatt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/services/gatt/src/ble_svc_gatt.c b/net/nimble/host/services/gatt/src/ble_svc_gatt.c
index c6426f0..e447e2a 100644
--- a/net/nimble/host/services/gatt/src/ble_svc_gatt.c
+++ b/net/nimble/host/services/gatt/src/ble_svc_gatt.c
@@ -19,6 +19,7 @@
 
 #include <assert.h>
 
+#include "sysinit/sysinit.h"
 #include "host/ble_hs.h"
 #include "services/gatt/ble_svc_gatt.h"
 
@@ -71,20 +72,14 @@ ble_svc_gatt_access(uint16_t conn_handle, uint16_t attr_handle,
     return 0;
 }
 
-int
+void
 ble_svc_gatt_init(void)
 {
     int rc;
 
     rc = ble_gatts_count_cfg(ble_svc_gatt_defs);
-    if (rc != 0) {
-        return rc;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = ble_gatts_add_svcs(ble_svc_gatt_defs);
-    if (rc != 0) {
-        return rc;
-    }
-
-    return 0;
+    SYSINIT_PANIC_ASSERT(rc == 0);
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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
deleted file mode 100644
index 90fdb4b..0000000
--- a/net/nimble/host/services/mandatory/src/ble_svc_mandatory.c
+++ /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.
- */
-
-#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/8dffea7d/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 5014a86..57a24da 100644
--- a/net/nimble/host/src/ble_gap.c
+++ b/net/nimble/host/src/ble_gap.c
@@ -216,7 +216,7 @@ STATS_NAME_END(ble_gap_stats)
  * $debug                                                                    *
  *****************************************************************************/
 
-#if BLE_HS_DEBUG
+#if MYNEWT_VAL(BLE_HS_DEBUG)
 int
 ble_gap_dbg_update_active(uint16_t conn_handle)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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 85b524b..3b33341 100644
--- a/net/nimble/host/src/ble_hs.c
+++ b/net/nimble/host/src/ble_hs.c
@@ -38,8 +38,6 @@
  */
 #define BLE_HS_MAX_EVS_IN_A_ROW 2
 
-static struct log_handler ble_hs_log_console_handler;
-
 struct os_mempool ble_hs_hci_ev_pool;
 static os_membuf_t ble_hs_hci_os_event_buf[
     OS_MEMPOOL_SIZE(BLE_HS_HCI_EVT_COUNT, sizeof (struct os_event))
@@ -419,22 +417,6 @@ ble_hs_enqueue_hci_event(uint8_t *hci_evt)
 
     ev = os_memblock_get(&ble_hs_hci_ev_pool);
     if (ev == NULL) {
-        ble_hci_trans_buf_free(ev->ev_arg);
-    } else {
-        ev->ev_queued = 0;
-        ev->ev_type = BLE_HOST_HCI_EVENT_CTLR_EVENT;
-        ev->ev_arg = hci_evt;
-        ble_hs_event_enqueue(ev);
-    }
-}
-
-void
-ble_hs_enqueue_hci_event(uint8_t *hci_evt)
-{
-    struct os_event *ev;
-
-    ev = os_memblock_get(&ble_hs_hci_ev_pool);
-    if (ev == NULL) {
         ble_hci_trans_buf_free(hci_evt);
     } else {
         ev->ev_queued = 0;
@@ -636,6 +618,4 @@ ble_hs_init(void)
 
     /* 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;
 }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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/8dffea7d/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
index 3fe398f..34a5cd9 100644
--- a/net/nimble/host/test/src/ble_gap_test.c
+++ b/net/nimble/host/test/src/ble_gap_test.c
@@ -2743,8 +2743,8 @@ TEST_CASE(ble_gap_test_case_conn_timeout_conn_forever)
 {
     ble_gap_test_util_init();
 
-    /* 3 ms. */
-    ble_gap_test_util_conn_timeout(3);
+    /* 30 ms. */
+    ble_gap_test_util_conn_timeout(30);
 
     /* No timeout. */
     ble_gap_test_util_conn_forever();
@@ -2758,8 +2758,8 @@ TEST_CASE(ble_gap_test_case_conn_timeout_conn_timeout)
     /* 30 ms. */
     ble_gap_test_util_conn_timeout(30);
 
-    /* 5 ms. */
-    ble_gap_test_util_conn_timeout(5);
+    /* 20 ms. */
+    ble_gap_test_util_conn_timeout(20);
 
 }
 
@@ -2776,16 +2776,16 @@ TEST_CASE(ble_gap_test_case_conn_forever_conn_timeout)
     /* Clear GAP event for remainder of test. */
     ble_gap_test_util_reset_cb_info();
 
-    /* 3 ms. */
-    ble_gap_test_util_conn_timeout(3);
+    /* 30 ms. */
+    ble_gap_test_util_conn_timeout(30);
 }
 
 TEST_CASE(ble_gap_test_case_disc_timeout_disc_forever)
 {
     ble_gap_test_util_init();
 
-    /* 3 ms. */
-    ble_gap_test_util_disc_timeout(3);
+    /* 30 ms. */
+    ble_gap_test_util_disc_timeout(30);
 
     /* No timeout. */
     ble_gap_test_util_disc_forever();
@@ -2799,8 +2799,8 @@ TEST_CASE(ble_gap_test_case_disc_timeout_disc_timeout)
     /* 30 ms. */
     ble_gap_test_util_disc_timeout(30);
 
-    /* 5 ms. */
-    ble_gap_test_util_disc_timeout(5);
+    /* 20 ms. */
+    ble_gap_test_util_disc_timeout(20);
 
 }
 
@@ -2814,8 +2814,8 @@ TEST_CASE(ble_gap_test_case_disc_forever_disc_timeout)
     /* Cancel discovery procedure manually. */
     ble_hs_test_util_disc_cancel(0);
 
-    /* 3 ms. */
-    ble_gap_test_util_disc_timeout(3);
+    /* 30 ms. */
+    ble_gap_test_util_disc_timeout(30);
 }
 
 TEST_CASE(ble_gap_test_case_conn_timeout_disc_timeout)
@@ -2825,8 +2825,8 @@ TEST_CASE(ble_gap_test_case_conn_timeout_disc_timeout)
     /* 15 seconds. */
     ble_gap_test_util_conn_timeout(15000);
 
-    /* 1285 ms. */
-    ble_gap_test_util_disc_timeout(1285);
+    /* 1280 ms. */
+    ble_gap_test_util_disc_timeout(1280);
 }
 
 TEST_SUITE(ble_gap_test_suite_timeout)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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
index 81e059a..554b687 100644
--- a/net/nimble/host/test/src/ble_hs_test_util.c
+++ b/net/nimble/host/test/src/ble_hs_test_util.c
@@ -539,23 +539,6 @@ ble_hs_test_util_conn_cancel_full(void)
     TEST_ASSERT_FATAL(rc == 0);
 }
 
-int
-ble_hs_test_util_conn_terminate(uint16_t conn_handle, uint8_t hci_status)
-{
-    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);
-}
-
 void
 ble_hs_test_util_set_ack_disconnect(uint8_t hci_status)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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 458df5a..effe2c3 100644
--- a/net/nimble/transport/ram/src/ble_hci_ram.c
+++ b/net/nimble/transport/ram/src/ble_hci_ram.c
@@ -20,6 +20,8 @@
 #include <assert.h>
 #include <errno.h>
 #include <stddef.h>
+#include "syscfg/syscfg.h"
+#include "sysinit/sysinit.h"
 #include "os/os.h"
 #include "util/mem.h"
 #include "nimble/ble.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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 d660e4c..8ec1431 100755
--- a/net/nimble/transport/uart/src/ble_hci_uart.c
+++ b/net/nimble/transport/uart/src/ble_hci_uart.c
@@ -21,6 +21,7 @@
 #include <string.h>
 #include <stdio.h>
 #include <errno.h>
+#include "syscfg/syscfg.h"
 #include "bsp/bsp.h"
 #include "os/os.h"
 #include "util/mem.h"
@@ -46,7 +47,7 @@
  */
 
 /* XXX: for now, define this here */
-#ifdef FEATURE_BLE_DEVICE
+#if MYNEWT_VAL(BLE_DEVICE)
 extern void ble_ll_data_buffer_overflow(void);
 extern void ble_ll_hw_error(uint8_t err);
 
@@ -298,7 +299,7 @@ ble_hci_uart_tx_char(void *arg)
     return rc;
 }
 
-#ifdef FEATURE_BLE_DEVICE
+#if MYNEWT_VAL(BLE_DEVICE)
 /**
  * HCI uart sync lost.
  *
@@ -331,7 +332,7 @@ ble_hci_uart_rx_pkt_type(uint8_t data)
 
     switch (ble_hci_uart_state.rx_type) {
     /* Host should never receive a command! */
-#ifdef FEATURE_BLE_DEVICE
+#if MYNEWT_VAL(BLE_DEVICE)
     case BLE_HCI_UART_H4_CMD:
         ble_hci_uart_state.rx_cmd.len = 0;
         ble_hci_uart_state.rx_cmd.cur = 0;
@@ -344,7 +345,7 @@ ble_hci_uart_rx_pkt_type(uint8_t data)
 #endif
 
         /* Controller should never receive an event */
-#ifdef FEATURE_BLE_HOST
+#if MYNEWT_VAL(BLE_HOST)
     case BLE_HCI_UART_H4_EVT:
         /*
          * XXX: we should not assert if host cannot allocate an event. Need
@@ -372,7 +373,7 @@ ble_hci_uart_rx_pkt_type(uint8_t data)
         break;
 
     default:
-#ifdef FEATURE_BLE_DEVICE
+#if MYNEWT_VAL(BLE_DEVICE)
         /*
          * If we receive an unknown HCI packet type this is considered a loss
          * of sync.
@@ -391,7 +392,7 @@ ble_hci_uart_rx_pkt_type(uint8_t data)
     return 0;
 }
 
-#ifdef FEATURE_BLE_DEVICE
+#if MYNEWT_VAL(BLE_DEVICE)
 /**
  * HCI uart sync loss.
  *
@@ -501,7 +502,7 @@ ble_hci_uart_rx_skip_cmd(uint8_t data)
 }
 #endif
 
-#ifdef FEATURE_BLE_HOST
+#if MYNEWT_VAL(BLE_HOST)
 static void
 ble_hci_uart_rx_evt(uint8_t data)
 {
@@ -594,7 +595,7 @@ ble_hci_uart_rx_skip_acl(uint8_t data)
 
     if (rxd_bytes == ble_hci_uart_state.rx_acl.len) {
 /* XXX: I dont like this but for now this denotes controller only */
-#ifdef FEATURE_BLE_DEVICE
+#if MYNEWT_VAL(BLE_DEVICE)
         ble_ll_data_buffer_overflow();
 #endif
         ble_hci_uart_state.rx_type = BLE_HCI_UART_H4_NONE;
@@ -607,7 +608,7 @@ ble_hci_uart_rx_char(void *arg, uint8_t data)
     switch (ble_hci_uart_state.rx_type) {
     case BLE_HCI_UART_H4_NONE:
         return ble_hci_uart_rx_pkt_type(data);
-#ifdef FEATURE_BLE_DEVICE
+#if MYNEWT_VAL(BLE_DEVICE)
     case BLE_HCI_UART_H4_CMD:
         ble_hci_uart_rx_cmd(data);
         return 0;
@@ -618,7 +619,7 @@ ble_hci_uart_rx_char(void *arg, uint8_t data)
         ble_hci_uart_rx_sync_loss(data);
         return 0;
 #endif
-#ifdef FEATURE_BLE_HOST
+#if MYNEWT_VAL(BLE_HOST)
     case BLE_HCI_UART_H4_EVT:
         ble_hci_uart_rx_evt(data);
         return 0;
@@ -960,13 +961,6 @@ ble_hci_uart_init(void)
 
     ble_hci_uart_free_mem();
 
-    /* Create memory pool of HCI command / event buffers */
-    rc = mem_malloc_mempool(&ble_hci_uart_evt_pool,
-                            BLE_HCI_UART_EVT_COUNT,
-                            MYNEWT_VAL(BLE_HCI_UART_BUF_SIZE),
-                            "ble_hci_uart_evt_pool",
-                            &ble_hci_uart_evt_buf);
-
     /*
      * XXX: For now, we will keep the ACL buffer size such that it can
      * accommodate BLE_MBUF_PAYLOAD_SIZE. It should be possible to make this

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/sys/config/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/config/pkg.yml b/sys/config/pkg.yml
index 2d7c83d..e005530 100644
--- a/sys/config/pkg.yml
+++ b/sys/config/pkg.yml
@@ -41,7 +41,7 @@ pkg.init_stage: 5
 pkg.syscfg_defs:
     CONFIG_FCB:
         description: 'TBD'
-        value: 0
+        value: 1
     CONFIG_FCB_MAGIC:
         description: 'TBD'
         value: 0xc09f6e5e

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/sys/config/test/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/config/test/pkg.yml b/sys/config/test/pkg.yml
index 7b533a6..0a366db 100644
--- a/sys/config/test/pkg.yml
+++ b/sys/config/test/pkg.yml
@@ -30,3 +30,7 @@ pkg.deps.SELFTEST:
     - fs/nffs
     - sys/fcb
     - libs/console/stub
+
+pkg.syscfg_vals.SELFTEST:
+    CONFIG_NFFS: 1
+    CONFIG_FCB: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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
index a1e7177..39acd2b 100644
--- a/sys/mn_socket/test/src/mn_sock_test.c
+++ b/sys/mn_socket/test/src/mn_sock_test.c
@@ -847,7 +847,7 @@ mn_socket_test_handler(void *arg)
     sock_udp_ll();
     sock_udp_mcast_v4();
     sock_udp_mcast_v6();
-    os_test_restart();
+    tu_restart();
 }
 
 TEST_CASE(socket_tests)

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/sys/reboot/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/reboot/pkg.yml b/sys/reboot/pkg.yml
index f55397b..afa950b 100644
--- a/sys/reboot/pkg.yml
+++ b/sys/reboot/pkg.yml
@@ -40,7 +40,7 @@ pkg.init_stage: 2
 pkg.syscfg_defs:
     REBOOT_LOG_0_TYPE:
         description: 'TBD'
-        value:  LOG_TYPE_STORAGE
+        value: LOG_STORE_CONSOLE
     REBOOT_LOG_0_ENTRY_COUNT:
         description: 'TBD'
         value:  10

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/sys/stats/pkg.yml
----------------------------------------------------------------------
diff --git a/sys/stats/pkg.yml b/sys/stats/pkg.yml
index c23bedc..9927ef3 100644
--- a/sys/stats/pkg.yml
+++ b/sys/stats/pkg.yml
@@ -38,7 +38,7 @@ pkg.init_stage: 0
 pkg.syscfg_defs:
     STATS_NAMES:
         description: 'TBD'
-        value: 1
+        value: 0
     STATS_CLI:
         description: 'TBD'
         value: 'MYNEWT_PKG_LIBS_SHELL'


[12/50] [abbrv] incubator-mynewt-core git commit: fix logging so that we use printf during sim, and use logs when debug is defined

Posted by vi...@apache.org.
fix logging so that we use printf during sim, and use logs when debug is defined


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

Branch: refs/heads/sterly_refactor
Commit: 29a11be746551e3c2b8c8f17407274669d739b5b
Parents: 7f682f6
Author: Paul Dietrich <pa...@yahoo.com>
Authored: Fri Sep 16 15:54:27 2016 -0700
Committer: Paul Dietrich <pa...@yahoo.com>
Committed: Wed Sep 21 10:48:28 2016 -0700

----------------------------------------------------------------------
 libs/iotivity/pkg.yml                      |  1 +
 libs/iotivity/src/port/mynewt/ip_adaptor.c | 79 +++++++------------------
 libs/iotivity/src/port/mynewt/log.c        | 43 ++++++++++++++
 libs/iotivity/src/port/oc_log.h            | 27 ++++++++-
 4 files changed, 90 insertions(+), 60 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/29a11be7/libs/iotivity/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/iotivity/pkg.yml b/libs/iotivity/pkg.yml
index e5c95fb..87b4b45 100644
--- a/libs/iotivity/pkg.yml
+++ b/libs/iotivity/pkg.yml
@@ -29,6 +29,7 @@ pkg.deps:
     - "@apache-mynewt-core/sys/mn_socket"
     - "@apache-mynewt-core/sys/log"
 
+# remove debug option to save logging 
 pkg.cflags: -std=c99
         -DDEBUG=1
         -DSECURE=0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/29a11be7/libs/iotivity/src/port/mynewt/ip_adaptor.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/ip_adaptor.c b/libs/iotivity/src/port/mynewt/ip_adaptor.c
index dfb1f9c..9b8bbff 100644
--- a/libs/iotivity/src/port/mynewt/ip_adaptor.c
+++ b/libs/iotivity/src/port/mynewt/ip_adaptor.c
@@ -27,6 +27,7 @@
 #include "../oc_network_events_mutex.h"
 #include "../oc_connectivity.h"
 #include "oc_buffer.h"
+#include "../oc_log.h"
 
 #ifdef OC_SECURITY
 #error This implementation does not yet support security
@@ -53,29 +54,6 @@ struct mn_socket *ucast;
 struct os_sem oc_read_sem;
 struct os_sem oc_write_sem;
 
-/* logging data for this module. TODO, the application should
- * define the logging strategy for this module */
-#define MAX_CBMEM_BUF   (600)
-static uint32_t *cbmem_buf;
-static struct cbmem cbmem;
-struct log oc_log;
-
-static int oc_log_init(void) {
-
-    log_init();
-
-    cbmem_buf = malloc(sizeof(uint32_t) * MAX_CBMEM_BUF);
-    if (cbmem_buf == NULL) {
-        return -1;
-    }
-
-    cbmem_init(&cbmem, cbmem_buf, MAX_CBMEM_BUF);
-    log_register("iot", &oc_log, &log_cbmem_handler, &cbmem);
-
-    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "OC Init");
-    return 0;
-}
-
 /* not sure if these semaphores are necessary yet.  If we are running
  * all of this from one task, we may not need these */
 static struct os_mutex oc_net_mutex;
@@ -109,8 +87,7 @@ oc_send_buffer(oc_message_t *message)
     int rc;
 
     while (1) {
-        LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY,
-                 "attempt send buffer %u\n", message->length);
+        LOG("attempt send buffer %lu\n", message->length);
 
         to.msin6_len = sizeof(to);
         to.msin6_family = MN_AF_INET6;
@@ -128,8 +105,7 @@ oc_send_buffer(oc_message_t *message)
         rc = mn_sendto(send_sock, &m, (struct mn_sockaddr *) &to);
         /* TODO what to do if this fails, we can't keep the buffer */
         if (rc != 0) {
-            LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                      "Failed sending buffer %u\n", message->length);
+            ERROR("Failed sending buffer %lu\n", message->length);
         } else {
             break;
         }
@@ -146,7 +122,7 @@ oc_attempt_rx(struct mn_socket * rxsock) {
     oc_message_t *message = NULL;
     struct mn_sockaddr_in6 from;
 
-    LOG_DEBUG(&oc_log, LOG_MODULE_IOTIVITY, "attempt rx from %u\n", rxsock);
+    LOG("attempt rx from %p\n", rxsock);
 
     rc= mn_recvfrom(rxsock, &m, (struct mn_sockaddr *) &from);
 
@@ -160,23 +136,22 @@ oc_attempt_rx(struct mn_socket * rxsock) {
 
     pkt = OS_MBUF_PKTHDR(m);
 
-    LOG_DEBUG(&oc_log, LOG_MODULE_IOTIVITY,
-              "rx from %u %p-%u\n", rxsock, pkt, pkt->omp_len);
+    LOG("rx from %p %p-%u\n", rxsock, pkt, pkt->omp_len);
 
     message = oc_allocate_message();
     if (NULL == message) {
-        /* TODO log an error that we dropped a frame */
+        ERROR("Could not allocate OC message buffer\n");
         goto rx_attempt_err;
     }
 
     if (pkt->omp_len > MAX_PAYLOAD_SIZE) {
-        /* TODO what do we do with this */
+        ERROR("Message to large for OC message buffer\n");
         goto rx_attempt_err;
     }
     /* copy to message from mbuf chain */
     rc = os_mbuf_copydata(m, 0, pkt->omp_len, message->data);
     if (rc != 0) {
-        /* TODO what do we do with this */
+        ERROR("Failed to copy message from mbuf to OC message buffer \n");
         goto rx_attempt_err;
     }
 
@@ -189,9 +164,7 @@ oc_attempt_rx(struct mn_socket * rxsock) {
     message->endpoint.ipv6_addr.scope = from.msin6_scope_id;
     message->endpoint.ipv6_addr.port = ntohs(from.msin6_port);
 
-    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "rx from %u len %u\n",
-             rxsock, message->length);
-
+    LOG("Successfully rx from %p len %lu\n", rxsock, message->length);
     return message;
 
     /* add the addr info to the message */
@@ -261,22 +234,19 @@ oc_init_net_task(void) {
     /* start this thing running to check right away */
     rc = os_sem_init(&oc_read_sem, 1);
     if (0 != rc) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not initialize oc read sem\n");
+        ERROR("Could not initialize oc read sem\n");
         return rc;
     }
 
     rc = os_sem_init(&oc_write_sem, 1);
     if (0 != rc) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not initialize oc write sem\n");
+        ERROR("Could not initialize oc write sem\n");
         return rc;
     }
 
     oc_stack = (os_stack_t*) malloc(sizeof(os_stack_t)*OC_NET_TASK_STACK_SIZE);
     if (NULL == oc_stack) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not malloc oc stack\n");
+        ERROR("Could not malloc oc stack\n");
         return -1;
     }
 
@@ -285,7 +255,7 @@ oc_init_net_task(void) {
             oc_stack, OC_NET_TASK_STACK_SIZE);
 
     if (rc != 0) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY, "Could not start oc task\n");
+        ERROR("Could not start oc task\n");
         free(oc_stack);
     }
 
@@ -295,7 +265,7 @@ oc_init_net_task(void) {
 void
 oc_connectivity_shutdown(void)
 {
-    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "OC shutdown");
+    LOG("OC shutdown");
 
     if (ucast) {
         mn_close(ucast);
@@ -317,21 +287,18 @@ oc_connectivity_init(void)
 
     rc = oc_log_init();
     if ( rc != 0) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not create oc logging\n");
+        ERROR("Could not create oc logging\n");
         return rc;    }
 
     rc = mn_socket(&ucast, MN_PF_INET6, MN_SOCK_DGRAM, 0);
     if ( rc != 0 || !ucast ) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not create oc unicast socket\n");
+        ERROR("Could not create oc unicast socket\n");
         return rc;
     }
     rc = mn_socket(&mcast, MN_PF_INET6, MN_SOCK_DGRAM, 0);
     if ( rc != 0 || !mcast ) {
         mn_close(ucast);
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not create oc multicast socket\n");
+        ERROR("Could not create oc multicast socket\n");
         return rc;
     }
     mn_socket_set_cbs(ucast, ucast, &oc_sock_cbs);
@@ -345,8 +312,7 @@ oc_connectivity_init(void)
 
     rc = mn_bind(ucast, (struct mn_sockaddr *)&sin);
     if (rc != 0) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not bind oc unicast socket\n");
+        ERROR("Could not bind oc unicast socket\n");
         goto oc_connectivity_init_err;
     }
 
@@ -369,20 +335,17 @@ oc_connectivity_init(void)
 
         rc = mn_setsockopt(mcast, MN_SO_LEVEL, MN_MCAST_JOIN_GROUP, &join);
         if (rc != 0) {
-            LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                 "Could not join multicast group on %s\n", itf.mif_name);
+            ERROR("Could not join multicast group on %s\n", itf.mif_name);
             continue;
         }
 
-        LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Joined Coap multicast grop on %s\n", itf.mif_name);
+        LOG("Joined Coap multicast grop on %s\n", itf.mif_name);
     }
 
     sin.msin6_port = htons(COAP_PORT_UNSECURED);
     rc = mn_bind(mcast, (struct mn_sockaddr *)&sin);
     if (rc != 0) {
-        LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,
-                  "Could not bind oc multicast socket\n");
+        ERROR("Could not bind oc multicast socket\n");
         goto oc_connectivity_init_err;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/29a11be7/libs/iotivity/src/port/mynewt/log.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/mynewt/log.c b/libs/iotivity/src/port/mynewt/log.c
new file mode 100644
index 0000000..8c76fc9
--- /dev/null
+++ b/libs/iotivity/src/port/mynewt/log.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 <log/log.h>
+
+/* logging data for this module. TODO, the application should
+ * define the logging strategy for this module */
+#define MAX_CBMEM_BUF   (600)
+static uint32_t *cbmem_buf;
+static struct cbmem cbmem;
+struct log oc_log;
+
+int
+oc_log_init(void) {
+
+    log_init();
+
+    cbmem_buf = malloc(sizeof(uint32_t) * MAX_CBMEM_BUF);
+    if (cbmem_buf == NULL) {
+        return -1;
+    }
+
+    cbmem_init(&cbmem, cbmem_buf, MAX_CBMEM_BUF);
+    log_register("iot", &oc_log, &log_cbmem_handler, &cbmem);
+
+    LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "OC Init");
+    return 0;
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/29a11be7/libs/iotivity/src/port/oc_log.h
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/port/oc_log.h b/libs/iotivity/src/port/oc_log.h
index c03233e..6744499 100644
--- a/libs/iotivity/src/port/oc_log.h
+++ b/libs/iotivity/src/port/oc_log.h
@@ -23,6 +23,20 @@
 #define PRINT(...) printf(__VA_ARGS__)
 
 #define PRINTipaddr(endpoint)                                                  \
+  PRINT(                                                                      \
+    "[%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%"    \
+    "02x]:%d",                                                                 \
+    ((endpoint).ipv6_addr.address)[0], ((endpoint).ipv6_addr.address)[1],      \
+    ((endpoint).ipv6_addr.address)[2], ((endpoint).ipv6_addr.address)[3],      \
+    ((endpoint).ipv6_addr.address)[4], ((endpoint).ipv6_addr.address)[5],      \
+    ((endpoint).ipv6_addr.address)[6], ((endpoint).ipv6_addr.address)[7],      \
+    ((endpoint).ipv6_addr.address)[8], ((endpoint).ipv6_addr.address)[9],      \
+    ((endpoint).ipv6_addr.address)[10], ((endpoint).ipv6_addr.address)[11],    \
+    ((endpoint).ipv6_addr.address)[12], ((endpoint).ipv6_addr.address)[13],    \
+    ((endpoint).ipv6_addr.address)[14], ((endpoint).ipv6_addr.address)[15],    \
+    (endpoint).ipv6_addr.port)
+
+#define LogMynewtipaddr(endpoint)                                                  \
 {\
   char tmp_buf[16*3+6]; /* 16 octets plus port */                              \
   sprintf(tmp_buf,                                                             \
@@ -40,12 +54,21 @@
     LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY, "%s", tmp_buf);                     \
 }
 
-#if DEBUG
-#define LOG(...) LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY,__VA_ARGS__)
+#if defined(ARCH_sim)
+#define LOG(...) PRINT(__VA_ARGS__)
+#define ERROR(...) LOG(__VA_ARGS__)
 #define LOGipaddr(endpoint) PRINTipaddr(endpoint)
+#define oc_log_init()   0
+#elif defined(DEBUG)
+int oc_log_init(void);
+#define LOG(...) LOG_INFO(&oc_log, LOG_MODULE_IOTIVITY,__VA_ARGS__)
+#define ERROR(...) LOG_ERROR(&oc_log, LOG_MODULE_IOTIVITY,__VA_ARGS__)
+#define LOGipaddr(endpoint) LogMynewtipaddr(endpoint)
 #else
 #define LOG(...)
+#define ERROR(...)
 #define LOGipaddr(endpoint)
+#define oc_log_init()   0
 #endif
 
 #endif /* OC_LOG_H */


[02/50] [abbrv] incubator-mynewt-core git commit: newtmgr/os is not a good name, newtmgr/nmgr_os is slightly better.

Posted by vi...@apache.org.
newtmgr/os is not a good name, newtmgr/nmgr_os is slightly better.


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

Branch: refs/heads/sterly_refactor
Commit: 5e925850fb477d3a6f2f11475b9b5f41cbdca7ea
Parents: 895d567
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Mon Sep 19 14:46:50 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Mon Sep 19 14:46:50 2016 -0700

----------------------------------------------------------------------
 libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h |  35 +++
 libs/newtmgr/nmgr_os/pkg.yml                   |  36 +++
 libs/newtmgr/nmgr_os/src/newtmgr_os.c          | 329 ++++++++++++++++++++
 libs/newtmgr/os/include/nmgr_os/nmgr_os.h      |  35 ---
 libs/newtmgr/os/pkg.yml                        |  36 ---
 libs/newtmgr/os/src/newtmgr_os.c               | 329 --------------------
 libs/newtmgr/pkg.yml                           |   2 +-
 7 files changed, 401 insertions(+), 401 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5e925850/libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h b/libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h
new file mode 100644
index 0000000..49b6725
--- /dev/null
+++ b/libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h
@@ -0,0 +1,35 @@
+/**
+ * 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 _NMGR_OS_H_
+#define _NMGR_OS_H_
+
+/*
+ * Id's for OS group commands
+ */
+#define NMGR_ID_ECHO            0
+#define NMGR_ID_CONS_ECHO_CTRL  1
+#define NMGR_ID_TASKSTATS       2
+#define NMGR_ID_MPSTATS         3
+#define NMGR_ID_DATETIME_STR    4
+#define NMGR_ID_RESET           5
+
+int nmgr_os_groups_register(void);
+
+#endif /* _NMGR_OS_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5e925850/libs/newtmgr/nmgr_os/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/newtmgr/nmgr_os/pkg.yml b/libs/newtmgr/nmgr_os/pkg.yml
new file mode 100644
index 0000000..d6610b2
--- /dev/null
+++ b/libs/newtmgr/nmgr_os/pkg.yml
@@ -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.
+#
+
+pkg.name: libs/newtmgr/nmgr_os
+pkg.description: Default newtmgr command.
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+    - hw/hal
+    - libs/os
+    - libs/json
+    - libs/util
+    - libs/testutil
+    - libs/newtmgr
+    - sys/reboot
+
+pkg.reqs:
+    - console

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5e925850/libs/newtmgr/nmgr_os/src/newtmgr_os.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/nmgr_os/src/newtmgr_os.c b/libs/newtmgr/nmgr_os/src/newtmgr_os.c
new file mode 100644
index 0000000..cab692b
--- /dev/null
+++ b/libs/newtmgr/nmgr_os/src/newtmgr_os.c
@@ -0,0 +1,329 @@
+/**
+ * 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 <os/endian.h>
+
+#include <assert.h>
+#include <string.h>
+
+#include <hal/hal_system.h>
+
+#include <newtmgr/newtmgr.h>
+#include <newtmgr/newtmgr_priv.h>
+
+#include <console/console.h>
+#include <util/datetime.h>
+#include <reboot/log_reboot.h>
+
+#include "nmgr_os/nmgr_os.h"
+
+static struct os_callout_func nmgr_reset_callout;
+
+static int nmgr_def_echo(struct nmgr_jbuf *);
+static int nmgr_def_console_echo(struct nmgr_jbuf *);
+static int nmgr_def_taskstat_read(struct nmgr_jbuf *njb);
+static int nmgr_def_mpstat_read(struct nmgr_jbuf *njb);
+static int nmgr_datetime_get(struct nmgr_jbuf *njb);
+static int nmgr_datetime_set(struct nmgr_jbuf *njb);
+static int nmgr_reset(struct nmgr_jbuf *njb);
+
+static const struct nmgr_handler nmgr_def_group_handlers[] = {
+    [NMGR_ID_ECHO] = {
+        nmgr_def_echo, nmgr_def_echo
+    },
+    [NMGR_ID_CONS_ECHO_CTRL] = {
+        nmgr_def_console_echo, nmgr_def_console_echo
+    },
+    [NMGR_ID_TASKSTATS] = {
+        nmgr_def_taskstat_read, NULL
+    },
+    [NMGR_ID_MPSTATS] = {
+        nmgr_def_mpstat_read, NULL
+    },
+    [NMGR_ID_DATETIME_STR] = {
+        nmgr_datetime_get, nmgr_datetime_set
+    },
+    [NMGR_ID_RESET] = {
+        NULL, nmgr_reset
+    },
+};
+
+#define NMGR_DEF_GROUP_SZ                                               \
+    (sizeof(nmgr_def_group_handlers) / sizeof(nmgr_def_group_handlers[0]))
+
+static struct nmgr_group nmgr_def_group = {
+    .ng_handlers = (struct nmgr_handler *)nmgr_def_group_handlers,
+    .ng_handlers_count = NMGR_DEF_GROUP_SZ,
+    .ng_group_id = NMGR_GROUP_ID_DEFAULT
+};
+
+static int
+nmgr_def_echo(struct nmgr_jbuf *njb)
+{
+    uint8_t echo_buf[128];
+    struct json_attr_t attrs[] = {
+        { "d", t_string, .addr.string = (char *) &echo_buf[0],
+            .len = sizeof(echo_buf) },
+        { NULL },
+    };
+    struct json_value jv;
+    int rc;
+
+    rc = json_read_object((struct json_buffer *) njb, attrs);
+    if (rc != 0) {
+        goto err;
+    }
+
+    json_encode_object_start(&njb->njb_enc);
+    JSON_VALUE_STRINGN(&jv, (char *) echo_buf, strlen((char *) echo_buf));
+    json_encode_object_entry(&njb->njb_enc, "r", &jv);
+    json_encode_object_finish(&njb->njb_enc);
+
+    return (0);
+err:
+    return (rc);
+}
+
+static int
+nmgr_def_console_echo(struct nmgr_jbuf *njb)
+{
+    long long int echo_on = 1;
+    int rc;
+    struct json_attr_t attrs[3] = {
+        [0] = {
+            .attribute = "echo",
+            .type = t_integer,
+            .addr.integer = &echo_on,
+            .nodefault = 1
+        },
+        [1] = {
+            .attribute = NULL
+        }
+    };
+
+    rc = json_read_object(&njb->njb_buf, attrs);
+    if (rc) {
+        return OS_EINVAL;
+    }
+
+    if (echo_on) {
+        console_echo(1);
+    } else {
+        console_echo(0);
+    }
+    return (0);
+}
+
+static int
+nmgr_def_taskstat_read(struct nmgr_jbuf *njb)
+{
+    struct os_task *prev_task;
+    struct os_task_info oti;
+    struct json_value jv;
+
+    json_encode_object_start(&njb->njb_enc);
+    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
+
+    json_encode_object_key(&njb->njb_enc, "tasks");
+    json_encode_object_start(&njb->njb_enc);
+
+    prev_task = NULL;
+    while (1) {
+        prev_task = os_task_info_get_next(prev_task, &oti);
+        if (prev_task == NULL) {
+            break;
+        }
+
+        json_encode_object_key(&njb->njb_enc, oti.oti_name);
+
+        json_encode_object_start(&njb->njb_enc);
+        JSON_VALUE_UINT(&jv, oti.oti_prio);
+        json_encode_object_entry(&njb->njb_enc, "prio", &jv);
+        JSON_VALUE_UINT(&jv, oti.oti_taskid);
+        json_encode_object_entry(&njb->njb_enc, "tid", &jv);
+        JSON_VALUE_UINT(&jv, oti.oti_state);
+        json_encode_object_entry(&njb->njb_enc, "state", &jv);
+        JSON_VALUE_UINT(&jv, oti.oti_stkusage);
+        json_encode_object_entry(&njb->njb_enc, "stkuse", &jv);
+        JSON_VALUE_UINT(&jv, oti.oti_stksize);
+        json_encode_object_entry(&njb->njb_enc, "stksiz", &jv);
+        JSON_VALUE_UINT(&jv, oti.oti_cswcnt);
+        json_encode_object_entry(&njb->njb_enc, "cswcnt", &jv);
+        JSON_VALUE_UINT(&jv, oti.oti_runtime);
+        json_encode_object_entry(&njb->njb_enc, "runtime", &jv);
+        JSON_VALUE_UINT(&jv, oti.oti_last_checkin);
+        json_encode_object_entry(&njb->njb_enc, "last_checkin", &jv);
+        JSON_VALUE_UINT(&jv, oti.oti_next_checkin);
+        json_encode_object_entry(&njb->njb_enc, "next_checkin", &jv);
+        json_encode_object_finish(&njb->njb_enc);
+    }
+    json_encode_object_finish(&njb->njb_enc);
+    json_encode_object_finish(&njb->njb_enc);
+
+    return (0);
+}
+
+static int
+nmgr_def_mpstat_read(struct nmgr_jbuf *njb)
+{
+    struct os_mempool *prev_mp;
+    struct os_mempool_info omi;
+    struct json_value jv;
+
+    json_encode_object_start(&njb->njb_enc);
+    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
+
+    json_encode_object_key(&njb->njb_enc, "mpools");
+    json_encode_object_start(&njb->njb_enc);
+
+    prev_mp = NULL;
+    while (1) {
+        prev_mp = os_mempool_info_get_next(prev_mp, &omi);
+        if (prev_mp == NULL) {
+            break;
+        }
+
+        json_encode_object_key(&njb->njb_enc, omi.omi_name);
+
+        json_encode_object_start(&njb->njb_enc);
+        JSON_VALUE_UINT(&jv, omi.omi_block_size);
+        json_encode_object_entry(&njb->njb_enc, "blksiz", &jv);
+        JSON_VALUE_UINT(&jv, omi.omi_num_blocks);
+        json_encode_object_entry(&njb->njb_enc, "nblks", &jv);
+        JSON_VALUE_UINT(&jv, omi.omi_num_free);
+        json_encode_object_entry(&njb->njb_enc, "nfree", &jv);
+        json_encode_object_finish(&njb->njb_enc);
+    }
+
+    json_encode_object_finish(&njb->njb_enc);
+    json_encode_object_finish(&njb->njb_enc);
+
+    return (0);
+}
+
+static int
+nmgr_datetime_get(struct nmgr_jbuf *njb)
+{
+    struct os_timeval tv;
+    struct os_timezone tz;
+    char buf[DATETIME_BUFSIZE];
+    struct json_value jv;
+    int rc;
+
+    json_encode_object_start(&njb->njb_enc);
+    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
+
+    /* Display the current datetime */
+    rc = os_gettimeofday(&tv, &tz);
+    assert(rc == 0);
+    rc = format_datetime(&tv, &tz, buf, DATETIME_BUFSIZE);
+    if (rc) {
+        rc = OS_EINVAL;
+        goto err;
+    }
+
+    JSON_VALUE_STRING(&jv, buf)
+    json_encode_object_entry(&njb->njb_enc, "datetime", &jv);
+    json_encode_object_finish(&njb->njb_enc);
+
+    return OS_OK;
+err:
+    return (rc);
+}
+
+static int
+nmgr_datetime_set(struct nmgr_jbuf *njb)
+{
+    struct os_timeval tv;
+    struct os_timezone tz;
+    struct json_value jv;
+    char buf[DATETIME_BUFSIZE];
+    int rc = OS_OK;
+    const struct json_attr_t datetime_write_attr[2] = {
+        [0] = {
+            .attribute = "datetime",
+            .type = t_string,
+            .addr.string = buf,
+            .len = sizeof(buf),
+        },
+        [1] = {
+            .attribute = "rc",
+            .type = t_uinteger,
+
+        }
+    };
+
+    rc = json_read_object(&njb->njb_buf, datetime_write_attr);
+    if (rc) {
+        rc = OS_EINVAL;
+        goto out;
+    }
+
+    /* Set the current datetime */
+    rc = parse_datetime(buf, &tv, &tz);
+    if (!rc) {
+        rc = os_settimeofday(&tv, &tz);
+        if (rc) {
+          rc = OS_EINVAL;
+          goto out;
+        }
+    } else {
+        rc = OS_EINVAL;
+        goto out;
+    }
+
+out:
+    json_encode_object_start(&njb->njb_enc);
+    JSON_VALUE_INT(&jv, rc);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
+    json_encode_object_finish(&njb->njb_enc);
+    return OS_OK;
+}
+
+static void
+nmgr_reset_tmo(void *arg)
+{
+    system_reset();
+}
+
+static int
+nmgr_reset(struct nmgr_jbuf *njb)
+{
+    if (nmgr_reset_callout.cf_func == NULL) {
+        os_callout_func_init(&nmgr_reset_callout, &g_nmgr_evq,
+          nmgr_reset_tmo, NULL);
+    }
+    log_reboot(SOFT_REBOOT);
+    os_callout_reset(&nmgr_reset_callout.cf_c, OS_TICKS_PER_SEC / 4);
+
+    nmgr_jbuf_setoerr(njb, 0);
+
+    return OS_OK;
+}
+
+int
+nmgr_os_groups_register(void)
+{
+    return nmgr_group_register(&nmgr_def_group);
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5e925850/libs/newtmgr/os/include/nmgr_os/nmgr_os.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr/os/include/nmgr_os/nmgr_os.h b/libs/newtmgr/os/include/nmgr_os/nmgr_os.h
deleted file mode 100644
index 49b6725..0000000
--- a/libs/newtmgr/os/include/nmgr_os/nmgr_os.h
+++ /dev/null
@@ -1,35 +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 _NMGR_OS_H_
-#define _NMGR_OS_H_
-
-/*
- * Id's for OS group commands
- */
-#define NMGR_ID_ECHO            0
-#define NMGR_ID_CONS_ECHO_CTRL  1
-#define NMGR_ID_TASKSTATS       2
-#define NMGR_ID_MPSTATS         3
-#define NMGR_ID_DATETIME_STR    4
-#define NMGR_ID_RESET           5
-
-int nmgr_os_groups_register(void);
-
-#endif /* _NMGR_OS_H_ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5e925850/libs/newtmgr/os/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/newtmgr/os/pkg.yml b/libs/newtmgr/os/pkg.yml
deleted file mode 100644
index 4260849..0000000
--- a/libs/newtmgr/os/pkg.yml
+++ /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.
-#
-
-pkg.name: libs/newtmgr/os
-pkg.description: Default newtmgr command.
-pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
-pkg.homepage: "http://mynewt.apache.org/"
-pkg.keywords:
-
-pkg.deps:
-    - hw/hal
-    - libs/os
-    - libs/json
-    - libs/util
-    - libs/testutil
-    - libs/newtmgr
-    - sys/reboot
-
-pkg.reqs:
-    - console

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5e925850/libs/newtmgr/os/src/newtmgr_os.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr/os/src/newtmgr_os.c b/libs/newtmgr/os/src/newtmgr_os.c
deleted file mode 100644
index cab692b..0000000
--- a/libs/newtmgr/os/src/newtmgr_os.c
+++ /dev/null
@@ -1,329 +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 <os/endian.h>
-
-#include <assert.h>
-#include <string.h>
-
-#include <hal/hal_system.h>
-
-#include <newtmgr/newtmgr.h>
-#include <newtmgr/newtmgr_priv.h>
-
-#include <console/console.h>
-#include <util/datetime.h>
-#include <reboot/log_reboot.h>
-
-#include "nmgr_os/nmgr_os.h"
-
-static struct os_callout_func nmgr_reset_callout;
-
-static int nmgr_def_echo(struct nmgr_jbuf *);
-static int nmgr_def_console_echo(struct nmgr_jbuf *);
-static int nmgr_def_taskstat_read(struct nmgr_jbuf *njb);
-static int nmgr_def_mpstat_read(struct nmgr_jbuf *njb);
-static int nmgr_datetime_get(struct nmgr_jbuf *njb);
-static int nmgr_datetime_set(struct nmgr_jbuf *njb);
-static int nmgr_reset(struct nmgr_jbuf *njb);
-
-static const struct nmgr_handler nmgr_def_group_handlers[] = {
-    [NMGR_ID_ECHO] = {
-        nmgr_def_echo, nmgr_def_echo
-    },
-    [NMGR_ID_CONS_ECHO_CTRL] = {
-        nmgr_def_console_echo, nmgr_def_console_echo
-    },
-    [NMGR_ID_TASKSTATS] = {
-        nmgr_def_taskstat_read, NULL
-    },
-    [NMGR_ID_MPSTATS] = {
-        nmgr_def_mpstat_read, NULL
-    },
-    [NMGR_ID_DATETIME_STR] = {
-        nmgr_datetime_get, nmgr_datetime_set
-    },
-    [NMGR_ID_RESET] = {
-        NULL, nmgr_reset
-    },
-};
-
-#define NMGR_DEF_GROUP_SZ                                               \
-    (sizeof(nmgr_def_group_handlers) / sizeof(nmgr_def_group_handlers[0]))
-
-static struct nmgr_group nmgr_def_group = {
-    .ng_handlers = (struct nmgr_handler *)nmgr_def_group_handlers,
-    .ng_handlers_count = NMGR_DEF_GROUP_SZ,
-    .ng_group_id = NMGR_GROUP_ID_DEFAULT
-};
-
-static int
-nmgr_def_echo(struct nmgr_jbuf *njb)
-{
-    uint8_t echo_buf[128];
-    struct json_attr_t attrs[] = {
-        { "d", t_string, .addr.string = (char *) &echo_buf[0],
-            .len = sizeof(echo_buf) },
-        { NULL },
-    };
-    struct json_value jv;
-    int rc;
-
-    rc = json_read_object((struct json_buffer *) njb, attrs);
-    if (rc != 0) {
-        goto err;
-    }
-
-    json_encode_object_start(&njb->njb_enc);
-    JSON_VALUE_STRINGN(&jv, (char *) echo_buf, strlen((char *) echo_buf));
-    json_encode_object_entry(&njb->njb_enc, "r", &jv);
-    json_encode_object_finish(&njb->njb_enc);
-
-    return (0);
-err:
-    return (rc);
-}
-
-static int
-nmgr_def_console_echo(struct nmgr_jbuf *njb)
-{
-    long long int echo_on = 1;
-    int rc;
-    struct json_attr_t attrs[3] = {
-        [0] = {
-            .attribute = "echo",
-            .type = t_integer,
-            .addr.integer = &echo_on,
-            .nodefault = 1
-        },
-        [1] = {
-            .attribute = NULL
-        }
-    };
-
-    rc = json_read_object(&njb->njb_buf, attrs);
-    if (rc) {
-        return OS_EINVAL;
-    }
-
-    if (echo_on) {
-        console_echo(1);
-    } else {
-        console_echo(0);
-    }
-    return (0);
-}
-
-static int
-nmgr_def_taskstat_read(struct nmgr_jbuf *njb)
-{
-    struct os_task *prev_task;
-    struct os_task_info oti;
-    struct json_value jv;
-
-    json_encode_object_start(&njb->njb_enc);
-    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
-    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
-
-    json_encode_object_key(&njb->njb_enc, "tasks");
-    json_encode_object_start(&njb->njb_enc);
-
-    prev_task = NULL;
-    while (1) {
-        prev_task = os_task_info_get_next(prev_task, &oti);
-        if (prev_task == NULL) {
-            break;
-        }
-
-        json_encode_object_key(&njb->njb_enc, oti.oti_name);
-
-        json_encode_object_start(&njb->njb_enc);
-        JSON_VALUE_UINT(&jv, oti.oti_prio);
-        json_encode_object_entry(&njb->njb_enc, "prio", &jv);
-        JSON_VALUE_UINT(&jv, oti.oti_taskid);
-        json_encode_object_entry(&njb->njb_enc, "tid", &jv);
-        JSON_VALUE_UINT(&jv, oti.oti_state);
-        json_encode_object_entry(&njb->njb_enc, "state", &jv);
-        JSON_VALUE_UINT(&jv, oti.oti_stkusage);
-        json_encode_object_entry(&njb->njb_enc, "stkuse", &jv);
-        JSON_VALUE_UINT(&jv, oti.oti_stksize);
-        json_encode_object_entry(&njb->njb_enc, "stksiz", &jv);
-        JSON_VALUE_UINT(&jv, oti.oti_cswcnt);
-        json_encode_object_entry(&njb->njb_enc, "cswcnt", &jv);
-        JSON_VALUE_UINT(&jv, oti.oti_runtime);
-        json_encode_object_entry(&njb->njb_enc, "runtime", &jv);
-        JSON_VALUE_UINT(&jv, oti.oti_last_checkin);
-        json_encode_object_entry(&njb->njb_enc, "last_checkin", &jv);
-        JSON_VALUE_UINT(&jv, oti.oti_next_checkin);
-        json_encode_object_entry(&njb->njb_enc, "next_checkin", &jv);
-        json_encode_object_finish(&njb->njb_enc);
-    }
-    json_encode_object_finish(&njb->njb_enc);
-    json_encode_object_finish(&njb->njb_enc);
-
-    return (0);
-}
-
-static int
-nmgr_def_mpstat_read(struct nmgr_jbuf *njb)
-{
-    struct os_mempool *prev_mp;
-    struct os_mempool_info omi;
-    struct json_value jv;
-
-    json_encode_object_start(&njb->njb_enc);
-    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
-    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
-
-    json_encode_object_key(&njb->njb_enc, "mpools");
-    json_encode_object_start(&njb->njb_enc);
-
-    prev_mp = NULL;
-    while (1) {
-        prev_mp = os_mempool_info_get_next(prev_mp, &omi);
-        if (prev_mp == NULL) {
-            break;
-        }
-
-        json_encode_object_key(&njb->njb_enc, omi.omi_name);
-
-        json_encode_object_start(&njb->njb_enc);
-        JSON_VALUE_UINT(&jv, omi.omi_block_size);
-        json_encode_object_entry(&njb->njb_enc, "blksiz", &jv);
-        JSON_VALUE_UINT(&jv, omi.omi_num_blocks);
-        json_encode_object_entry(&njb->njb_enc, "nblks", &jv);
-        JSON_VALUE_UINT(&jv, omi.omi_num_free);
-        json_encode_object_entry(&njb->njb_enc, "nfree", &jv);
-        json_encode_object_finish(&njb->njb_enc);
-    }
-
-    json_encode_object_finish(&njb->njb_enc);
-    json_encode_object_finish(&njb->njb_enc);
-
-    return (0);
-}
-
-static int
-nmgr_datetime_get(struct nmgr_jbuf *njb)
-{
-    struct os_timeval tv;
-    struct os_timezone tz;
-    char buf[DATETIME_BUFSIZE];
-    struct json_value jv;
-    int rc;
-
-    json_encode_object_start(&njb->njb_enc);
-    JSON_VALUE_INT(&jv, NMGR_ERR_EOK);
-    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
-
-    /* Display the current datetime */
-    rc = os_gettimeofday(&tv, &tz);
-    assert(rc == 0);
-    rc = format_datetime(&tv, &tz, buf, DATETIME_BUFSIZE);
-    if (rc) {
-        rc = OS_EINVAL;
-        goto err;
-    }
-
-    JSON_VALUE_STRING(&jv, buf)
-    json_encode_object_entry(&njb->njb_enc, "datetime", &jv);
-    json_encode_object_finish(&njb->njb_enc);
-
-    return OS_OK;
-err:
-    return (rc);
-}
-
-static int
-nmgr_datetime_set(struct nmgr_jbuf *njb)
-{
-    struct os_timeval tv;
-    struct os_timezone tz;
-    struct json_value jv;
-    char buf[DATETIME_BUFSIZE];
-    int rc = OS_OK;
-    const struct json_attr_t datetime_write_attr[2] = {
-        [0] = {
-            .attribute = "datetime",
-            .type = t_string,
-            .addr.string = buf,
-            .len = sizeof(buf),
-        },
-        [1] = {
-            .attribute = "rc",
-            .type = t_uinteger,
-
-        }
-    };
-
-    rc = json_read_object(&njb->njb_buf, datetime_write_attr);
-    if (rc) {
-        rc = OS_EINVAL;
-        goto out;
-    }
-
-    /* Set the current datetime */
-    rc = parse_datetime(buf, &tv, &tz);
-    if (!rc) {
-        rc = os_settimeofday(&tv, &tz);
-        if (rc) {
-          rc = OS_EINVAL;
-          goto out;
-        }
-    } else {
-        rc = OS_EINVAL;
-        goto out;
-    }
-
-out:
-    json_encode_object_start(&njb->njb_enc);
-    JSON_VALUE_INT(&jv, rc);
-    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
-    json_encode_object_finish(&njb->njb_enc);
-    return OS_OK;
-}
-
-static void
-nmgr_reset_tmo(void *arg)
-{
-    system_reset();
-}
-
-static int
-nmgr_reset(struct nmgr_jbuf *njb)
-{
-    if (nmgr_reset_callout.cf_func == NULL) {
-        os_callout_func_init(&nmgr_reset_callout, &g_nmgr_evq,
-          nmgr_reset_tmo, NULL);
-    }
-    log_reboot(SOFT_REBOOT);
-    os_callout_reset(&nmgr_reset_callout.cf_c, OS_TICKS_PER_SEC / 4);
-
-    nmgr_jbuf_setoerr(njb, 0);
-
-    return OS_OK;
-}
-
-int
-nmgr_os_groups_register(void)
-{
-    return nmgr_group_register(&nmgr_def_group);
-}
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5e925850/libs/newtmgr/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/newtmgr/pkg.yml b/libs/newtmgr/pkg.yml
index 40301b7..b5643e3 100644
--- a/libs/newtmgr/pkg.yml
+++ b/libs/newtmgr/pkg.yml
@@ -27,7 +27,7 @@ pkg.deps:
     - hw/hal
     - libs/os
     - libs/json
-    - libs/newtmgr/os
+    - libs/newtmgr/nmgr_os
     - libs/util
     - libs/testutil
     - libs/shell


[36/50] [abbrv] incubator-mynewt-core git commit: add watchdog implementation for nordic, simplify hal_watchdog by making hal_watchdog_enable() return void.

Posted by vi...@apache.org.
add watchdog implementation for nordic, simplify hal_watchdog by making hal_watchdog_enable() return void.


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

Branch: refs/heads/sterly_refactor
Commit: 94737dee6a63970586e51f8011a0c0f14e22806a
Parents: 9714934
Author: Sterling Hughes <st...@apache.org>
Authored: Thu Sep 22 14:15:22 2016 -0700
Committer: Sterling Hughes <st...@apache.org>
Committed: Thu Sep 22 14:15:22 2016 -0700

----------------------------------------------------------------------
 hw/mcu/nordic/nrf51xxx/src/hal_watchdog.c | 66 ++++++++++++++++++++++++++
 hw/mcu/nordic/nrf52xxx/src/hal_watchdog.c | 66 ++++++++++++++++++++++++++
 2 files changed, 132 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/94737dee/hw/mcu/nordic/nrf51xxx/src/hal_watchdog.c
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf51xxx/src/hal_watchdog.c b/hw/mcu/nordic/nrf51xxx/src/hal_watchdog.c
new file mode 100644
index 0000000..486aeec
--- /dev/null
+++ b/hw/mcu/nordic/nrf51xxx/src/hal_watchdog.c
@@ -0,0 +1,66 @@
+/**
+ * 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 "hal/hal_watchdog.h"
+
+#include <assert.h>
+
+#include "app_util_platform.h"
+#include "nrf.h"
+#include "nrf_wdt.h"
+#include "nrf_drv_wdt.h"
+
+static void
+nrf51_hal_wdt_default_handler(void)
+{
+    assert(0);
+}
+
+int
+hal_watchdog_init(int expire_secs)
+{
+    nrf_drv_wdt_config_t cfg;
+    int rc;
+
+    cfg.behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP;
+    cfg.interrupt_priority = WDT_CONFIG_IRQ_PRIORITY;
+    cfg.reload_value = (uint32_t) expire_secs;
+
+    rc = nrf_drv_wdt_init(&cfg, nrf51_hal_wdt_default_handler);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+void
+hal_watchdog_enable(void)
+{
+    nrf_drv_wdt_enable();
+}
+
+void
+hal_watchdog_tickle(void)
+{
+    nrf_drv_wdt_feed();
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/94737dee/hw/mcu/nordic/nrf52xxx/src/hal_watchdog.c
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf52xxx/src/hal_watchdog.c b/hw/mcu/nordic/nrf52xxx/src/hal_watchdog.c
new file mode 100644
index 0000000..6ba1aba
--- /dev/null
+++ b/hw/mcu/nordic/nrf52xxx/src/hal_watchdog.c
@@ -0,0 +1,66 @@
+/**
+ * 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 "hal/hal_watchdog.h"
+
+#include <assert.h>
+
+#include "app_util_platform.h"
+#include "nrf.h"
+#include "nrf_wdt.h"
+#include "nrf_drv_wdt.h"
+
+static void
+nrf52_hal_wdt_default_handler(void)
+{
+    assert(0);
+}
+
+int
+hal_watchdog_init(int expire_secs)
+{
+    nrf_drv_wdt_config_t cfg;
+    int rc;
+
+    cfg.behaviour = NRF_WDT_BEHAVIOUR_RUN_SLEEP;
+    cfg.interrupt_priority = WDT_CONFIG_IRQ_PRIORITY;
+    cfg.reload_value = (uint32_t) expire_secs;
+
+    rc = nrf_drv_wdt_init(&cfg, nrf52_hal_wdt_default_handler);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+void
+hal_watchdog_enable(void)
+{
+    nrf_drv_wdt_enable();
+}
+
+void
+hal_watchdog_tickle(void)
+{
+    nrf_drv_wdt_feed();
+}
+


[10/50] [abbrv] incubator-mynewt-core git commit: newtmgr_oic; fix compiler warning.

Posted by vi...@apache.org.
newtmgr_oic; fix compiler warning.


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

Branch: refs/heads/sterly_refactor
Commit: 4cb7c5bb03bb1c252e1f2c1efe8fc71a88e2f42c
Parents: f4a3543
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Sep 21 09:56:02 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Wed Sep 21 09:56:02 2016 -0700

----------------------------------------------------------------------
 libs/newtmgr_oic/src/newtmgr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/4cb7c5bb/libs/newtmgr_oic/src/newtmgr.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr_oic/src/newtmgr.c b/libs/newtmgr_oic/src/newtmgr.c
index 4d7f25b..29a07de 100644
--- a/libs/newtmgr_oic/src/newtmgr.c
+++ b/libs/newtmgr_oic/src/newtmgr.c
@@ -301,8 +301,8 @@ nmgr_jbuf_setoerr(struct nmgr_jbuf *njb, int errcode)
 static struct nmgr_handler *
 nmgr_oic_find_handler(const char *q, int qlen)
 {
-    int grp;
-    int id;
+    int grp = -1;
+    int id = -1;
     char *str;
     char *eptr;
     int slen;


[43/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-391 STM32F4 ADC driver and ADC HAL support

Posted by vi...@apache.org.
MYNEWT-391 STM32F4 ADC driver and ADC HAL support

- Addressing PR review
- moving gpio_init to adc_init and adding comment for intentional
  fallthrough for the resolution of the gpios.


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

Branch: refs/heads/sterly_refactor
Commit: 01fb0159b17ad182b23f464b196498bcae266e2f
Parents: c10e663
Author: Vipul Rahane <vi...@runtime.io>
Authored: Thu Sep 22 14:48:50 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Thu Sep 22 20:21:03 2016 -0700

----------------------------------------------------------------------
 drivers/adc/adc_stm32f4/src/adc_stm32f4.c | 38 +++++++++++++++++---------
 1 file changed, 25 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/01fb0159/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/src/adc_stm32f4.c b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
index efef0b4..480464a 100644
--- a/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
+++ b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
@@ -124,6 +124,10 @@ stm32f4_resolve_adc_gpio(ADC_HandleTypeDef *adc, uint8_t cnum)
                     pin = ADC12_CH15_PIN;
                     goto done;
             }
+        /*
+         * Falling through intentionally as ADC_3 contains seperate pins for
+         * Channels that ADC_1 and ADC_2 contain as well.
+         */
         case (uintptr_t)ADC3:
             switch(cnum) {
                 case ADC_CHANNEL_0:
@@ -362,15 +366,24 @@ stm32f4_adc_dma_init(ADC_HandleTypeDef* hadc)
 static void
 stm32f4_adc_init(void *arg)
 {
-    struct stm32f4_adc_dev_cfg *adc_config = (struct stm32f4_adc_dev_cfg *)arg;
+    struct adc_dev *dev;
+    struct stm32f4_adc_dev_cfg *adc_config;
     ADC_HandleTypeDef *hadc;
+    GPIO_InitTypeDef gpio_td;
+    uint8_t cnum;
 
-    assert(adc_config);
+    assert(arg);
 
+    dev = (struct adc_dev *)arg;
+    adc_config = (struct stm32f4_adc_dev_cfg *)dev->ad_dev_cfg;
     hadc = adc_config->sac_adc_handle;
+    cnum = dev->ad_chans->c_cnum;
 
     stm32f4_adc_dma_init(hadc);
 
+    gpio_td = stm32f4_resolve_adc_gpio(hadc, cnum);
+    hal_gpio_init_stm(gpio_td.Pin, &gpio_td);
+
     if (HAL_ADC_Init(hadc) != HAL_OK) {
         assert(0);
     }
@@ -445,8 +458,7 @@ stm32f4_adc_open(struct os_dev *odev, uint32_t wait, void *arg)
         goto err;
     }
 
-
-    stm32f4_adc_init(dev->ad_dev_cfg);
+    stm32f4_adc_init(dev);
 
     cfg  = dev->ad_dev_cfg;
     hadc = cfg->sac_adc_handle;
@@ -500,21 +512,21 @@ stm32f4_adc_configure_channel(struct adc_dev *dev, uint8_t cnum,
     int rc;
     ADC_HandleTypeDef *hadc;
     struct stm32f4_adc_dev_cfg *cfg;
-    GPIO_InitTypeDef gpio_td;
     struct adc_chan_config *chan_cfg;
 
-    assert(dev != NULL && IS_ADC_CHANNEL(cnum));
+    rc = OS_EINVAL;
+
+    if (dev != NULL && IS_ADC_CHANNEL(cnum)) {
+        goto err;
+    }
+
     cfg  = dev->ad_dev_cfg;
     hadc = cfg->sac_adc_handle;
     chan_cfg = cfg->sac_chans;
 
-    gpio_td = stm32f4_resolve_adc_gpio(hadc, cnum);
-    hal_gpio_init_stm(gpio_td.Pin, &gpio_td);
-
-
     cfgdata = (ADC_ChannelConfTypeDef *)cfgdata;
 
-    if ((rc = HAL_ADC_ConfigChannel(hadc, cfgdata)) != HAL_OK) {
+    if ((HAL_ADC_ConfigChannel(hadc, cfgdata)) != HAL_OK) {
         goto err;
     }
 
@@ -524,11 +536,11 @@ stm32f4_adc_configure_channel(struct adc_dev *dev, uint8_t cnum,
 
 #if 0
     if (HAL_ADC_Start_IT(hadc) != HAL_OK) {
-        assert(0);
+        goto err;
     }
 #endif
 
-    return (0);
+    return (OS_OK);
 err:
     return (rc);
 }


[16/50] [abbrv] incubator-mynewt-core git commit: Merge branch 'develop' into sterly_refactor

Posted by vi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/test/src/ble_sm_test_util.c
----------------------------------------------------------------------
diff --cc net/nimble/host/test/src/ble_sm_test_util.c
index 51dde82,0000000..aab2bc8
mode 100644,000000..100644
--- a/net/nimble/host/test/src/ble_sm_test_util.c
+++ b/net/nimble/host/test/src/ble_sm_test_util.c
@@@ -1,2410 -1,0 +1,2414 @@@
 +/**
 + * 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;
++static struct ble_gap_passkey_params ble_sm_test_ioact;
 +
 +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_ioact, 0, sizeof ble_sm_test_ioact);
 +    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;
++    return 0;
 +}
 +
 +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) {
++        TEST_ASSERT(ble_sm_test_ioact.action == BLE_SM_IOACT_NONE);
 +        return;
 +    }
 +
++    TEST_ASSERT(ble_sm_test_ioact.action == passkey_info->passkey.action);
++
 +    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);
++
++    ble_sm_test_ioact.action = BLE_SM_IOACT_NONE;
 +}
 +
 +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_ASSE

<TRUNCATED>
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/transport/ram/src/ble_hci_ram.c
----------------------------------------------------------------------



[39/50] [abbrv] incubator-mynewt-core git commit: Console - init early in case of startup crash.

Posted by vi...@apache.org.
Console - init early in case of startup crash.


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

Branch: refs/heads/sterly_refactor
Commit: 2454b8c1866c9caf04ca5e4cfb070c75a5f619dd
Parents: 742b7c4
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Sep 22 16:28:40 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Thu Sep 22 16:29:16 2016 -0700

----------------------------------------------------------------------
 libs/console/full/pkg.yml | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/2454b8c1/libs/console/full/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/console/full/pkg.yml b/libs/console/full/pkg.yml
index 69e3ddb..cf0e2d2 100644
--- a/libs/console/full/pkg.yml
+++ b/libs/console/full/pkg.yml
@@ -30,4 +30,4 @@ pkg.deps:
 pkg.apis: console
 
 pkg.init_function: console_pkg_init
-pkg.init_stage: 5
+pkg.init_stage: 0


[08/50] [abbrv] incubator-mynewt-core git commit: newtmgr; export api 'newtmgr'.

Posted by vi...@apache.org.
newtmgr; export api 'newtmgr'.


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

Branch: refs/heads/sterly_refactor
Commit: ad9ce1c78e4403458ff72e95faf121c0e5559506
Parents: 63dfa91
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Wed Sep 21 08:16:48 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Wed Sep 21 08:16:48 2016 -0700

----------------------------------------------------------------------
 libs/newtmgr/pkg.yml | 3 +++
 1 file changed, 3 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/ad9ce1c7/libs/newtmgr/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/newtmgr/pkg.yml b/libs/newtmgr/pkg.yml
index b5643e3..93fbae7 100644
--- a/libs/newtmgr/pkg.yml
+++ b/libs/newtmgr/pkg.yml
@@ -36,5 +36,8 @@ pkg.deps:
 pkg.deps.BLE_HOST:
     - libs/newtmgr/transport/ble
 
+pkg.apis:
+    - newtmgr
+
 pkg.features:
     - NEWTMGR


[34/50] [abbrv] incubator-mynewt-core git commit: bletest - Remove nffs dependency.

Posted by vi...@apache.org.
bletest - Remove nffs dependency.


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

Branch: refs/heads/sterly_refactor
Commit: 7066db44024ff1a36c483182b10793069b95b25a
Parents: 4b9def2
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Sep 22 00:29:02 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Thu Sep 22 00:29:02 2016 -0700

----------------------------------------------------------------------
 apps/bletest/pkg.yml    | 1 -
 apps/bletest/src/main.c | 2 --
 2 files changed, 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7066db44/apps/bletest/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bletest/pkg.yml b/apps/bletest/pkg.yml
index c435194..9068192 100644
--- a/apps/bletest/pkg.yml
+++ b/apps/bletest/pkg.yml
@@ -25,7 +25,6 @@ pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
 
 pkg.deps: 
-    - fs/nffs
     - net/nimble/controller
     - net/nimble/host
     - net/nimble/transport/ram

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/7066db44/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bletest/src/main.c b/apps/bletest/src/main.c
index 88e19c9..0282b4e 100755
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@ -29,8 +29,6 @@
 #include "shell/shell.h"
 #include "stats/stats.h"
 #include "hal/flash_map.h"
-#include "fs/fs.h"
-#include "nffs/nffs.h"
 #include "newtmgr/newtmgr.h"
 #include "imgmgr/imgmgr.h"
 


[07/50] [abbrv] incubator-mynewt-core git commit: newtmgr_oic; run OIC server, and newtmgr protocol over it.

Posted by vi...@apache.org.
newtmgr_oic; run OIC server, and newtmgr protocol over it.


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

Branch: refs/heads/sterly_refactor
Commit: 63dfa915288fc48f9ae265b31979900fafb49d6a
Parents: 3ba8d18
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Sep 20 17:54:12 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Tue Sep 20 17:54:12 2016 -0700

----------------------------------------------------------------------
 libs/newtmgr_oic/include/newtmgr/newtmgr.h      | 112 +++++
 libs/newtmgr_oic/include/newtmgr/newtmgr_priv.h |  24 +
 libs/newtmgr_oic/pkg.yml                        |  44 ++
 libs/newtmgr_oic/src/newtmgr.c                  | 504 +++++++++++++++++++
 4 files changed, 684 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/63dfa915/libs/newtmgr_oic/include/newtmgr/newtmgr.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr_oic/include/newtmgr/newtmgr.h b/libs/newtmgr_oic/include/newtmgr/newtmgr.h
new file mode 100644
index 0000000..dec4a5a
--- /dev/null
+++ b/libs/newtmgr_oic/include/newtmgr/newtmgr.h
@@ -0,0 +1,112 @@
+/**
+ * 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 _NEWTMGR_H_
+#define _NEWTMGR_H_
+
+#include <json/json.h>
+#include <inttypes.h>
+#include <os/os.h>
+
+/* MTU for newtmgr responses */
+#define NMGR_MAX_MTU 1024
+
+#ifndef STR
+/* Stringification of constants */
+#define STR(x) #x
+#endif
+
+/* First 64 groups are reserved for system level newtmgr commands.
+ * Per-user commands are then defined after group 64.
+ */
+#define NMGR_GROUP_ID_DEFAULT   (0)
+#define NMGR_GROUP_ID_IMAGE     (1)
+#define NMGR_GROUP_ID_STATS     (2)
+#define NMGR_GROUP_ID_CONFIG    (3)
+#define NMGR_GROUP_ID_LOGS      (4)
+#define NMGR_GROUP_ID_CRASH     (5)
+#define NMGR_GROUP_ID_SPLIT     (6)
+#define NMGR_GROUP_ID_PERUSER   (64)
+
+#define NMGR_OP_READ            (0)
+#define NMGR_OP_READ_RSP        (1)
+#define NMGR_OP_WRITE           (2)
+#define NMGR_OP_WRITE_RSP       (3)
+
+
+/**
+ * Newtmgr JSON error codes
+ */
+#define NMGR_ERR_EOK      (0)
+#define NMGR_ERR_EUNKNOWN (1)
+#define NMGR_ERR_ENOMEM   (2)
+#define NMGR_ERR_EINVAL   (3)
+#define NMGR_ERR_ETIMEOUT (4)
+#define NMGR_ERR_ENOENT   (5)
+#define NMGR_ERR_EPERUSER (256)
+
+struct nmgr_hdr {
+    uint8_t  nh_op;             /* NMGR_OP_XXX */
+    uint8_t  nh_flags;
+    uint16_t nh_len;            /* length of the payload */
+    uint16_t nh_group;          /* NMGR_GROUP_XXX */
+    uint8_t  nh_seq;            /* sequence number */
+    uint8_t  nh_id;             /* message ID within group */
+};
+
+struct nmgr_jbuf {
+    /* json_buffer must be first element in the structure */
+    struct json_buffer njb_buf;
+    struct json_encoder njb_enc;
+    char *njb_in;
+    uint16_t njb_in_off;
+    uint16_t njb_in_end;
+    char *njb_out;
+    uint16_t njb_out_off;
+    uint16_t njb_out_end;
+};
+int nmgr_jbuf_setoerr(struct nmgr_jbuf *njb, int errcode);
+
+typedef int (*nmgr_handler_func_t)(struct nmgr_jbuf *);
+
+#define NMGR_HANDLER_FUNC(__name)                                           \
+    int __name(struct nmgr_hdr *nmr, struct os_mbuf *req, uint16_t srcoff,  \
+            struct os_mbuf *rsp)
+
+struct nmgr_handler {
+    nmgr_handler_func_t nh_read;
+    nmgr_handler_func_t nh_write;
+};
+
+struct nmgr_group {
+    struct nmgr_handler *ng_handlers;
+    uint16_t ng_handlers_count;
+    uint16_t ng_group_id;
+    STAILQ_ENTRY(nmgr_group) ng_next;
+};
+
+#define NMGR_GROUP_SET_HANDLERS(__group, __handlers)       \
+    (__group)->ng_handlers = (__handlers);                 \
+    (__group)->ng_handlers_count = (sizeof((__handlers)) / \
+            sizeof(struct nmgr_handler));
+
+int nmgr_oic_init(uint8_t, os_stack_t *, uint16_t);
+int nmgr_group_register(struct nmgr_group *group);
+
+#endif /* _NETMGR_H */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/63dfa915/libs/newtmgr_oic/include/newtmgr/newtmgr_priv.h
----------------------------------------------------------------------
diff --git a/libs/newtmgr_oic/include/newtmgr/newtmgr_priv.h b/libs/newtmgr_oic/include/newtmgr/newtmgr_priv.h
new file mode 100644
index 0000000..7217711
--- /dev/null
+++ b/libs/newtmgr_oic/include/newtmgr/newtmgr_priv.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 __NETMGR_PRIV_H_
+#define __NETMGR_PRIV_H_
+
+extern struct os_eventq g_nmgr_evq;
+
+#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/63dfa915/libs/newtmgr_oic/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/newtmgr_oic/pkg.yml b/libs/newtmgr_oic/pkg.yml
new file mode 100644
index 0000000..75d20f5
--- /dev/null
+++ b/libs/newtmgr_oic/pkg.yml
@@ -0,0 +1,44 @@
+#
+# 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/newtmgr_oic
+pkg.description: Server-side newtmgr functionality for OIC
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+    - libs/os
+    - libs/iotivity
+    - libs/json
+    - libs/newtmgr/nmgr_os
+    - libs/util
+    - libs/testutil
+    - libs/shell
+    - sys/reboot
+
+pkg.cflags:
+    - -DOC_SERVER
+
+pkg.apis:
+    - newtmgr
+
+pkg.features:
+    - NEWTMGR
+    - OC_SERVER

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/63dfa915/libs/newtmgr_oic/src/newtmgr.c
----------------------------------------------------------------------
diff --git a/libs/newtmgr_oic/src/newtmgr.c b/libs/newtmgr_oic/src/newtmgr.c
new file mode 100644
index 0000000..434bf46
--- /dev/null
+++ b/libs/newtmgr_oic/src/newtmgr.c
@@ -0,0 +1,504 @@
+/**
+ * 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 <os/endian.h>
+
+#include <assert.h>
+#include <string.h>
+
+#include <newtmgr/newtmgr.h>
+#include <nmgr_os/nmgr_os.h>
+
+#include <iotivity/oc_api.h>
+
+#include "newtmgr/newtmgr_priv.h"
+
+#define NMGR_OC_EVENT	(OS_EVENT_T_PERUSER)
+#define NMGR_OC_TIMER	(OS_EVENT_T_PERUSER + 1)
+
+struct nmgr_state {
+    struct os_mutex ns_group_lock;
+    STAILQ_HEAD(, nmgr_group) ns_groups;
+    struct os_event ns_oc_event;
+    struct os_callout ns_oc_timer;
+    struct os_task ns_task;
+    struct nmgr_jbuf ns_jbuf;
+    char ns_rsp[NMGR_MAX_MTU];
+};
+
+static struct nmgr_state nmgr_state = {
+  .ns_groups = STAILQ_HEAD_INITIALIZER(nmgr_state.ns_groups),
+  .ns_oc_event.ev_type = NMGR_OC_EVENT,
+  .ns_oc_timer.c_ev.ev_type = NMGR_OC_TIMER,
+  .ns_oc_timer.c_evq = &g_nmgr_evq
+};
+struct os_eventq g_nmgr_evq;
+
+/* JSON buffer for NMGR task
+ */
+
+
+static void nmgr_oic_get(oc_request_t *request, oc_interface_mask_t interface);
+static void nmgr_oic_put(oc_request_t *request, oc_interface_mask_t interface);
+
+int
+nmgr_group_list_lock(void)
+{
+    int rc;
+
+    if (!os_started()) {
+        return (0);
+    }
+
+    rc = os_mutex_pend(&nmgr_state.ns_group_lock, OS_WAIT_FOREVER);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+int
+nmgr_group_list_unlock(void)
+{
+    int rc;
+
+    if (!os_started()) {
+        return (0);
+    }
+
+    rc = os_mutex_release(&nmgr_state.ns_group_lock);
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+
+int
+nmgr_group_register(struct nmgr_group *group)
+{
+    int rc;
+
+    rc = nmgr_group_list_lock();
+    if (rc != 0) {
+        goto err;
+    }
+
+    STAILQ_INSERT_TAIL(&nmgr_state.ns_groups, group, ng_next);
+
+    rc = nmgr_group_list_unlock();
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+static struct nmgr_group *
+nmgr_find_group(uint16_t group_id)
+{
+    struct nmgr_group *group;
+    int rc;
+
+    group = NULL;
+
+    rc = nmgr_group_list_lock();
+    if (rc != 0) {
+        goto err;
+    }
+
+    STAILQ_FOREACH(group, &nmgr_state.ns_groups, ng_next) {
+        if (group->ng_group_id == group_id) {
+            break;
+        }
+    }
+
+    rc = nmgr_group_list_unlock();
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (group);
+err:
+    return (NULL);
+}
+
+static struct nmgr_handler *
+nmgr_find_handler(uint16_t group_id, uint16_t handler_id)
+{
+    struct nmgr_group *group;
+    struct nmgr_handler *handler;
+
+    group = nmgr_find_group(group_id);
+    if (!group) {
+        goto err;
+    }
+
+    if (handler_id >= group->ng_handlers_count) {
+        goto err;
+    }
+
+    handler = &group->ng_handlers[handler_id];
+
+    return (handler);
+err:
+    return (NULL);
+}
+
+int
+nmgr_rsp_extend(struct nmgr_hdr *hdr, struct os_mbuf *rsp, void *data,
+        uint16_t len)
+{
+    int rc;
+
+    rc = os_mbuf_append(rsp, data, len);
+    if (rc != 0) {
+        goto err;
+    }
+    hdr->nh_len += len;
+
+    return (0);
+err:
+    return (rc);
+}
+
+static char
+nmgr_jbuf_read_next(struct json_buffer *jb)
+{
+    struct nmgr_jbuf *njb;
+    char c;
+
+    njb = (struct nmgr_jbuf *) jb;
+
+    if (njb->njb_in_off + 1 > njb->njb_in_end) {
+        return '\0';
+    }
+
+    c = njb->njb_in[njb->njb_in_off];
+    ++njb->njb_in_off;
+
+    return (c);
+}
+
+static char
+nmgr_jbuf_read_prev(struct json_buffer *jb)
+{
+    struct nmgr_jbuf *njb;
+    char c;
+
+    njb = (struct nmgr_jbuf *) jb;
+
+    if (njb->njb_in_off == 0) {
+        return '\0';
+    }
+
+    --njb->njb_in_off;
+    c = njb->njb_in[njb->njb_in_off];
+
+    return (c);
+}
+
+static int
+nmgr_jbuf_readn(struct json_buffer *jb, char *buf, int size)
+{
+    struct nmgr_jbuf *njb;
+    int read;
+    int left;
+
+    njb = (struct nmgr_jbuf *) jb;
+
+    left = njb->njb_in_end - njb->njb_in_off;
+    read = size > left ? left : size;
+
+    memcpy(buf, njb->njb_in + njb->njb_in_off, read);
+
+    return (read);
+}
+
+static int
+nmgr_jbuf_write(void *arg, char *data, int len)
+{
+    struct nmgr_jbuf *njb;
+    int rc;
+
+    njb = (struct nmgr_jbuf *) arg;
+
+    if (njb->njb_out_off + len >= njb->njb_out_end) {
+        assert(0);
+        goto err;
+    }
+    memcpy(njb->njb_out + njb->njb_out_off, data, len);
+    njb->njb_out_off += len;
+    njb->njb_out[njb->njb_out_off] = '\0';
+
+    return (0);
+err:
+    return (rc);
+}
+
+static void
+nmgr_jbuf_init(struct nmgr_jbuf *njb)
+{
+    memset(njb, 0, sizeof(*njb));
+
+    njb->njb_buf.jb_read_next = nmgr_jbuf_read_next;
+    njb->njb_buf.jb_read_prev = nmgr_jbuf_read_prev;
+    njb->njb_buf.jb_readn = nmgr_jbuf_readn;
+    njb->njb_enc.je_write = nmgr_jbuf_write;
+    njb->njb_enc.je_arg = njb;
+}
+
+static void
+nmgr_jbuf_setibuf(struct nmgr_jbuf *njb, char *ptr, uint16_t len)
+{
+    njb->njb_in_off = 0;
+    njb->njb_in_end = len;
+    njb->njb_in = ptr;
+    njb->njb_enc.je_wr_commas = 0;
+}
+
+static void
+nmgr_jbuf_setobuf(struct nmgr_jbuf *njb, char *ptr, uint16_t maxlen)
+{
+    njb->njb_out = ptr;
+    njb->njb_out_off = 0;
+    njb->njb_out_end = maxlen;
+    njb->njb_out[0] = '\0';
+}
+
+int
+nmgr_jbuf_setoerr(struct nmgr_jbuf *njb, int errcode)
+{
+    struct json_value jv;
+
+    json_encode_object_start(&njb->njb_enc);
+    JSON_VALUE_INT(&jv, errcode);
+    json_encode_object_entry(&njb->njb_enc, "rc", &jv);
+    json_encode_object_finish(&njb->njb_enc);
+
+    return (0);
+}
+
+static struct nmgr_handler *
+nmgr_oic_find_handler(const char *q, int qlen)
+{
+    int grp;
+    int id;
+    char *str;
+    char *eptr;
+    int slen;
+
+    slen = oc_ri_get_query_value(q, qlen, "gr", &str);
+    if (slen > 0) {
+        grp = strtoul(str, &eptr, 0);
+        if (*eptr != '\0' && *eptr != '&') {
+            return NULL;
+        }
+    }
+    slen = oc_ri_get_query_value(q, qlen, "id", &str);
+    if (slen > 0) {
+        id = strtoul(str, &eptr, 0);
+        if (*eptr != '\0' && *eptr != '&') {
+            return NULL;
+        }
+    }
+    return nmgr_find_handler(grp, id);
+}
+
+static void
+nmgr_oic_op(oc_request_t *req, oc_interface_mask_t mask, int isset)
+{
+    struct nmgr_state *ns = &nmgr_state;
+    struct nmgr_handler *handler;
+    oc_rep_t *data;
+    int rc;
+
+    if (!req->query_len) {
+        goto bad_req;
+    }
+
+    handler = nmgr_oic_find_handler(req->query, req->query_len);
+    if (!handler) {
+        goto bad_req;
+    }
+
+    /*
+     * Setup state for JSON encoding.
+     */
+    nmgr_jbuf_setobuf(&ns->ns_jbuf, ns->ns_rsp, sizeof(ns->ns_rsp));
+
+    data = req->request_payload;
+    if (data) {
+        if (data->type != STRING) {
+            goto bad_req;
+        }
+        nmgr_jbuf_setibuf(&ns->ns_jbuf, oc_string(data->value_string),
+          oc_string_len(data->value_string));
+    } else {
+        nmgr_jbuf_setibuf(&ns->ns_jbuf, NULL, 0);
+    }
+
+    if (!isset) {
+        if (handler->nh_read) {
+            rc = handler->nh_read(&ns->ns_jbuf);
+        } else {
+            goto bad_req;
+        }
+    } else {
+        if (handler->nh_write) {
+            rc = handler->nh_write(&ns->ns_jbuf);
+        } else {
+            goto bad_req;
+        }
+    }
+    if (rc) {
+        goto bad_req;
+    }
+
+    oc_rep_start_root_object();
+    switch (mask) {
+    case OC_IF_BASELINE:
+        oc_process_baseline_interface(req->resource);
+    case OC_IF_RW:
+        oc_rep_set_text_string(root, "key", ns->ns_rsp);
+        break;
+    default:
+        break;
+    }
+    oc_rep_end_root_object();
+    oc_send_response(req, OC_STATUS_OK);
+
+    return;
+bad_req:
+    oc_send_response(req, OC_STATUS_BAD_REQUEST);
+}
+
+static void
+nmgr_oic_get(oc_request_t *req, oc_interface_mask_t mask)
+{
+    nmgr_oic_op(req, mask, 0);
+}
+
+static void
+nmgr_oic_put(oc_request_t *req, oc_interface_mask_t mask)
+{
+    nmgr_oic_op(req, mask, 1);
+}
+
+static void
+nmgr_app_init(void)
+{
+    oc_init_platform("MyNewt", NULL, NULL);
+    oc_add_device("/oic/d", "oic.d.light", "MynewtLed", "1.0", "1.0", NULL,
+      NULL);
+}
+
+static void
+nmgr_register_resources(void)
+{
+    uint8_t mode;
+    oc_resource_t *res = NULL;
+    char name[12];
+
+    snprintf(name, sizeof(name), "/nmgr");
+    res = oc_new_resource(name, 1, 0);
+    oc_resource_bind_resource_type(res, "x.mynewt.nmgr");
+    mode = OC_IF_RW;
+    oc_resource_bind_resource_interface(res, mode);
+    oc_resource_set_default_interface(res, mode);
+    oc_resource_set_discoverable(res);
+    oc_resource_set_request_handler(res, OC_GET, nmgr_oic_get);
+    oc_resource_set_request_handler(res, OC_PUT, nmgr_oic_put);
+    oc_add_resource(res);
+}
+
+static const oc_handler_t nmgr_oc_handler = {
+    .init = nmgr_app_init,
+    .register_resources = nmgr_register_resources
+};
+
+void
+oc_signal_main_loop(void)
+{
+    os_eventq_put(&g_nmgr_evq, &nmgr_state.ns_oc_event);
+}
+
+void
+nmgr_oic_task(void *arg)
+{
+    struct nmgr_state *ns = &nmgr_state;
+    struct os_event *ev;
+    struct os_callout_func *ocf;
+    os_time_t next_event;
+
+    nmgr_jbuf_init(&ns->ns_jbuf);
+
+    oc_main_init((oc_handler_t *)&nmgr_oc_handler);
+    while (1) {
+        ev = os_eventq_get(&g_nmgr_evq);
+        switch (ev->ev_type) {
+        case NMGR_OC_EVENT:
+        case NMGR_OC_TIMER:
+            next_event = oc_main_poll();
+            if (next_event) {
+                os_callout_reset(&ns->ns_oc_timer, next_event - os_time_get());
+            } else {
+                os_callout_stop(&ns->ns_oc_timer);
+            }
+            break;
+        case OS_EVENT_T_TIMER:
+            ocf = (struct os_callout_func *)ev;
+            ocf->cf_func(CF_ARG(ocf));
+            break;
+        }
+    }
+}
+
+int
+nmgr_oic_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
+{
+    struct nmgr_state *ns = &nmgr_state;
+    int rc;
+
+    os_eventq_init(&g_nmgr_evq);
+
+    rc = os_task_init(&ns->ns_task, "newtmgr_oic", nmgr_oic_task, NULL, prio,
+            OS_WAIT_FOREVER, stack_ptr, stack_len);
+    if (rc != 0) {
+        goto err;
+    }
+
+    rc = nmgr_os_groups_register();
+    if (rc != 0) {
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}


[15/50] [abbrv] incubator-mynewt-core git commit: Merge branch 'develop' into sterly_refactor

Posted by vi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
----------------------------------------------------------------------
diff --cc net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
index 415b320,7b6a817..9d5b7c9
--- a/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
+++ b/net/nimble/transport/uart/include/transport/uart/ble_hci_uart.h
@@@ -1,19 -1,22 +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 H_BLE_HCI_UART_
  #define H_BLE_HCI_UART_
  
--struct ble_hci_uart_cfg {
--    uint32_t baud;
-     uint16_t num_evt_bufs;
 -    uint16_t num_evt_hi_bufs;
 -    uint16_t num_evt_lo_bufs;
--    uint16_t evt_buf_sz;
 -    uint16_t num_acl_bufs;
 -    uint16_t num_msys_bufs;
--    uint8_t uart_port;
--    uint8_t flow_ctrl;
--    uint8_t data_bits;
--    uint8_t stop_bits;
--    uint8_t parity;
--};
--
--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/c5901fcc/net/nimble/transport/uart/pkg.yml
----------------------------------------------------------------------
diff --cc net/nimble/transport/uart/pkg.yml
index 56f0cda,cce429c..252feba
--- a/net/nimble/transport/uart/pkg.yml
+++ b/net/nimble/transport/uart/pkg.yml
@@@ -32,35 -32,3 +32,41 @@@ pkg.deps
  
  pkg.apis:
      - ble_transport
 +
 +pkg.syscfg_defs:
++    BLE_HCI_EVT_BUF_SIZE:
++        description: 'TBD'
++        # The largest event the nimble controller will send is 70 bytes.
++        value: 70
 +    BLE_HCI_EVT_HI_BUF_COUNT:
 +        description: 'TBD'
-         value:     8
++        value: 8
 +    BLE_HCI_EVT_LO_BUF_COUNT:
 +        description: 'TBD'
-         value:     0
++        value: 8
++    BLE_HCI_ACL_BUF_COUNT:
++        description: 'TBD'
++        value: 4
 +
-     BLE_HCI_UART_BUF_SIZE:
++    BLE_HCI_ACL_OUT_COUNT:
 +        description: 'TBD'
-         value: 260
++        value: 4
 +
 +    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/c5901fcc/net/nimble/transport/uart/src/ble_hci_uart.c
----------------------------------------------------------------------
diff --cc net/nimble/transport/uart/src/ble_hci_uart.c
index e7e07a1,663c817..d660e4c
--- a/net/nimble/transport/uart/src/ble_hci_uart.c
+++ b/net/nimble/transport/uart/src/ble_hci_uart.c
@@@ -36,21 -36,50 +36,38 @@@
  
  #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
 + * and outgoing events and commands use buffers from the same pool.
 + */
 +
+ /* XXX: for now, define this here */
+ #ifdef FEATURE_BLE_DEVICE
+ extern void ble_ll_data_buffer_overflow(void);
+ extern void ble_ll_hw_error(uint8_t err);
+ 
+ static const uint8_t ble_hci_uart_reset_cmd[4] = { 0x01, 0x03, 0x0C, 0x00 };
+ #endif
+ 
+ /***
+  * NOTES:
 - * The UART HCI transport doesn't use event buffer priorities.  All incoming
 - * and outgoing events use buffers from the same pool.
 - *
+  * The "skip" definitions are here so that when buffers cannot be allocated,
+  * the command or acl packets are simply skipped so that the HCI interface
+  * does not lose synchronization and resets dont (necessarily) occur.
+  */
  #define BLE_HCI_UART_H4_NONE        0x00
  #define BLE_HCI_UART_H4_CMD         0x01
  #define BLE_HCI_UART_H4_ACL         0x02
  #define BLE_HCI_UART_H4_SCO         0x03
  #define BLE_HCI_UART_H4_EVT         0x04
+ #define BLE_HCI_UART_H4_SYNC_LOSS   0x80
+ #define BLE_HCI_UART_H4_SKIP_CMD    0x81
+ #define BLE_HCI_UART_H4_SKIP_ACL    0x82
  
 -/** 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_hi_bufs = 8,
 -    .num_evt_lo_bufs = 8,
 -
 -    /* The largest event the nimble controller will send is 70 bytes. */
 -    .evt_buf_sz = 70,
 -
 -    .num_acl_bufs = 4
 -};
 -
  static ble_hci_trans_rx_cmd_fn *ble_hci_uart_rx_cmd_cb;
  static void *ble_hci_uart_rx_cmd_arg;
  
@@@ -107,6 -147,29 +135,28 @@@ 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 uint16_t ble_hci_uart_max_acl_datalen;
+ 
+ /**
+  * Allocates a buffer (mbuf) for ACL operation.
+  *
+  * @return                      The allocated buffer on success;
+  *                              NULL on buffer exhaustion.
+  */
+ static struct os_mbuf *
+ ble_hci_trans_acl_buf_alloc(void)
+ {
+     struct os_mbuf *m;
+ 
+     /*
+      * XXX: note that for host only there would be no need to allocate
+      * a user header. Address this later.
+      */
+     m = os_mbuf_get_pkthdr(&ble_hci_uart_acl_mbuf_pool,
+                            sizeof(struct ble_mbuf_hdr));
+     return m;
+ }
+ 
  static int
  ble_hci_uart_acl_tx(struct os_mbuf *om)
  {
@@@ -248,6 -311,26 +298,26 @@@ ble_hci_uart_tx_char(void *arg
      return rc;
  }
  
+ #ifdef FEATURE_BLE_DEVICE
+ /**
 - * HCI uart sync loss.
++ * HCI uart sync lost.
+  *
+  * This occurs when the controller receives an invalid packet type or a length
+  * field that is out of range. The controller needs to send a HW error to the
+  * host and wait to find a LL reset command.
+  */
+ static void
+ ble_hci_uart_sync_lost(void)
+ {
+     ble_hci_uart_state.rx_cmd.len = 0;
+     ble_hci_uart_state.rx_cmd.cur = 0;
+     ble_hci_uart_state.rx_cmd.data =
+         ble_hci_trans_buf_alloc(BLE_HCI_TRANS_BUF_CMD);
+     ble_ll_hw_error(BLE_HW_ERR_HCI_SYNC_LOSS);
+     ble_hci_uart_state.rx_type = BLE_HCI_UART_H4_SYNC_LOSS;
+ }
+ #endif
+ 
  /**
   * @return                      The type of packet to follow success;
   *                              -1 if there is no valid packet to receive.
@@@ -682,26 -969,86 +953,91 @@@ ble_hci_trans_reset(void
   *                              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;
+     int acl_block_size;
 +    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,
 +                            BLE_HCI_UART_EVT_COUNT,
 +                            MYNEWT_VAL(BLE_HCI_UART_BUF_SIZE),
 +                            "ble_hci_uart_evt_pool",
 +                            &ble_hci_uart_evt_buf);
+ 
+     /*
+      * XXX: For now, we will keep the ACL buffer size such that it can
+      * accommodate BLE_MBUF_PAYLOAD_SIZE. It should be possible to make this
+      * user defined but more testing would need to be done in that case. The
+      * MBUF payload size must accommodate the HCI data header size plus the
+      * maximum ACL data packet length.
+      *
+      * XXX: Should the max acl data length be part of config?
+      */
+     acl_block_size = BLE_MBUF_PAYLOAD_SIZE + BLE_MBUF_MEMBLOCK_OVERHEAD;
+     acl_block_size = OS_ALIGN(acl_block_size, OS_ALIGNMENT);
+     ble_hci_uart_max_acl_datalen = BLE_MBUF_PAYLOAD_SIZE - BLE_HCI_DATA_HDR_SZ;
+     rc = mem_malloc_mempool(&ble_hci_uart_acl_pool,
 -                            cfg->num_acl_bufs,
++                            MYNEWT_VAL(BLE_HCI_ACL_BUF_COUNT),
+                             acl_block_size,
+                             "ble_hci_uart_acl_pool",
+                             &ble_hci_uart_acl_buf);
      if (rc != 0) {
          rc = ble_err_from_os(rc);
          goto err;
      }
  
-     /* Create memory pool of packet list nodes. */
+     rc = os_mbuf_pool_init(&ble_hci_uart_acl_mbuf_pool, &ble_hci_uart_acl_pool,
 -                           acl_block_size, cfg->num_acl_bufs);
++                           acl_block_size, MYNEWT_VAL(BLE_HCI_ACL_BUF_COUNT));
+     assert(rc == 0);
+ 
+     /*
+      * Create memory pool of HCI command buffers. NOTE: we currently dont
+      * allow this to be configured. The controller will only allow one
+      * outstanding command. We decided to keep this a pool in case we allow
+      * allow the controller to handle more than one outstanding command.
+      */
+     rc = mem_malloc_mempool(&ble_hci_uart_cmd_pool,
+                             1,
+                             BLE_HCI_TRANS_CMD_SZ,
+                             "ble_hci_uart_cmd_pool",
+                             &ble_hci_uart_cmd_buf);
+     if (rc != 0) {
+         rc = ble_err_from_os(rc);
+         goto err;
+     }
+ 
+     rc = mem_malloc_mempool(&ble_hci_uart_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_uart_evt_hi_pool",
+                             &ble_hci_uart_evt_hi_buf);
+     if (rc != 0) {
+         rc = ble_err_from_os(rc);
+         goto err;
+     }
+ 
+     rc = mem_malloc_mempool(&ble_hci_uart_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_uart_evt_lo_pool",
+                             &ble_hci_uart_evt_lo_buf);
+     if (rc != 0) {
+         rc = ble_err_from_os(rc);
+         goto err;
+     }
+ 
+     /*
+      * Create memory pool of packet list nodes. NOTE: the number of these
+      * buffers should be, at least, the total number of event buffers (hi
+      * and lo), the number of command buffers (currently 1) and the total
+      * number of buffers that the controller could possibly hand to the host.
+      */
      rc = mem_malloc_mempool(&ble_hci_uart_pkt_pool,
-                             BLE_HCI_UART_EVT_COUNT,
 -                            cfg->num_evt_hi_bufs + cfg->num_evt_lo_bufs + 1 +
 -                            cfg->num_msys_bufs,
++                            BLE_HCI_UART_EVT_COUNT + 1 +
++                                MYNEWT_VAL(BLE_HCI_ACL_OUT_COUNT),
                              sizeof (struct ble_hci_uart_pkt),
                              "ble_hci_uart_pkt_pool",
                              &ble_hci_uart_pkt_buf);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/sys/config/include/config/config.h
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/sys/config/src/config.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/sys/config/src/config_nmgr.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/sys/log/include/log/log.h
----------------------------------------------------------------------
diff --cc sys/log/include/log/log.h
index 1dc2a7e,80d61e6..48deb0b
--- a/sys/log/include/log/log.h
+++ b/sys/log/include/log/log.h
@@@ -178,17 -197,13 +193,13 @@@ int log_walk(struct log *log, log_walk_
  int log_flush(struct log *log);
  int log_rtr_erase(struct log *log, void *arg);
  
- 
- 
  /* Handler exports */
- int log_cbmem_handler_init(struct log_handler *, struct cbmem *);
- int log_console_handler_init(struct log_handler *);
- struct fcb;
- int log_fcb_handler_init(struct log_handler *, struct fcb *,
-                          uint8_t entries);
+ extern const struct log_handler log_console_handler;
+ extern const struct log_handler log_cbmem_handler;
+ extern const struct log_handler log_fcb_handler;
  
  /* 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/c5901fcc/sys/log/src/log.c
----------------------------------------------------------------------
diff --cc sys/log/src/log.c
index eb841e7,7bdc0fc..6160626
--- a/sys/log/src/log.c
+++ b/sys/log/src/log.c
@@@ -22,16 -24,17 +22,18 @@@
  #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
  
+ struct log_info g_log_info;
+ 
  static STAILQ_HEAD(, log) g_log_list = STAILQ_HEAD_INITIALIZER(g_log_list);
  static uint8_t log_inited;
  
@@@ -43,19 -46,25 +45,25 @@@ struct shell_cmd g_shell_log_cmd = 
  };
  #endif
  
+ char *log_modules[LOG_MODULE_MAX];
+ 
 -int
 +void
  log_init(void)
  {
 -#ifdef NEWTMGR_PRESENT
      int rc;
 -#endif
 +
 +    (void)rc;
  
      if (log_inited) {
 -        return (0);
 +        return;
      }
      log_inited = 1;
  
+     g_log_info.li_version = LOG_VERSION_V2;
+     g_log_info.li_index = 0;
+     g_log_info.li_timestamp = 0;
+ 
 -#ifdef SHELL_PRESENT
 +#if MYNEWT_VAL(LOG_CLI)
      shell_cmd_register(&g_shell_log_cmd);
  #endif
  

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/sys/log/src/log_fcb.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/sys/log/src/log_nmgr.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/sys/log/test/src/log_test.c
----------------------------------------------------------------------
diff --cc sys/log/test/src/log_test.c
index 6f83c57,0000000..b0f71ad
mode 100644,000000..100644
--- a/sys/log/test/src/log_test.c
+++ b/sys/log/test/src/log_test.c
@@@ -1,163 -1,0 +1,160 @@@
 +/**
 + * 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);
++    log_register("log", &my_log, &log_fcb_handler, &log_fcb);
 +}
 +
 +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/c5901fcc/sys/mn_socket/test/src/mn_sock_test.c
----------------------------------------------------------------------
diff --cc sys/mn_socket/test/src/mn_sock_test.c
index 4babb33,0000000..a1e7177
mode 100644,000000..100644
--- a/sys/mn_socket/test/src/mn_sock_test.c
+++ b/sys/mn_socket/test/src/mn_sock_test.c
@@@ -1,83 -1,0 +1,895 @@@
 +/**
 + * 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"
++#include "mn_socket/arch/sim/native_sock.h"
++
++#define TEST_STACK_SIZE 4096
++#define TEST_PRIO 22
++static os_stack_t test_stack[OS_STACK_ALIGN(TEST_STACK_SIZE)];
++static struct os_task test_task;
++
++static struct os_sem test_sem;
++
++#define MB_CNT 10
++#define MB_SZ  512
++static uint8_t test_mbuf_area[MB_CNT * MB_SZ];
++static struct os_mempool test_mbuf_mpool;
++static struct os_mbuf_pool test_mbuf_pool;
 +
 +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_CASE(inet_ntop_test)
++{
++    const char *rstr;
++    char addr[48];
++    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 } },
++        { "255.1.255.255", { 255, 1, 255, 255 } },
++        { "1.2.5.6", { 1, 2, 5, 6 } }
++    };
++    int i;
++
++    for (i = 0; i < sizeof(ok_vec) / sizeof(ok_vec[0]); i++) {
++        memset(addr, 0xa5, sizeof(addr));
++        rstr = mn_inet_ntop(MN_PF_INET, ok_vec[i].cmp, addr, sizeof(addr));
++        TEST_ASSERT(rstr);
++        TEST_ASSERT(!strcmp(ok_vec[i].str, addr));
++    }
++    rstr = mn_inet_ntop(MN_PF_INET, ok_vec[0].cmp, addr, 1);
++    TEST_ASSERT(rstr == NULL);
++
++    /* does not have space to null terminate */
++    rstr = mn_inet_ntop(MN_PF_INET, ok_vec[0].cmp, addr, 7);
++    TEST_ASSERT(rstr == NULL);
++}
++
++void
++sock_open_close(void)
++{
++    struct mn_socket *sock;
++    int rc;
++
++    rc = mn_socket(&sock, MN_PF_INET, MN_SOCK_DGRAM, 0);
++    TEST_ASSERT(sock);
++    TEST_ASSERT(rc == 0);
++    mn_close(sock);
++
++    rc = mn_socket(&sock, MN_PF_INET, MN_SOCK_STREAM, 0);
++    TEST_ASSERT(sock);
++    TEST_ASSERT(rc == 0);
++    mn_close(sock);
++
++    rc = mn_socket(&sock, MN_PF_INET6, MN_SOCK_DGRAM, 0);
++    TEST_ASSERT(sock);
++    TEST_ASSERT(rc == 0);
++    mn_close(sock);
++
++    rc = mn_socket(&sock, MN_PF_INET6, MN_SOCK_STREAM, 0);
++    TEST_ASSERT(sock);
++    TEST_ASSERT(rc == 0);
++    mn_close(sock);
++}
++
++void
++sock_listen(void)
++{
++    struct mn_socket *sock;
++    struct mn_sockaddr_in msin;
++    int rc;
++
++    rc = mn_socket(&sock, MN_PF_INET, MN_SOCK_STREAM, 0);
++    TEST_ASSERT(rc == 0);
++
++    msin.msin_family = MN_PF_INET;
++    msin.msin_len = sizeof(msin);
++    msin.msin_port = htons(12444);
++
++    mn_inet_pton(MN_PF_INET, "127.0.0.1", &msin.msin_addr);
++
++    rc = mn_bind(sock, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_listen(sock, 2);
++    TEST_ASSERT(rc == 0);
++
++    mn_close(sock);
++}
++
++void
++stc_writable(void *cb_arg, int err)
++{
++    int *i;
++
++    TEST_ASSERT(err == 0);
++    i = (int *)cb_arg;
++    *i = *i + 1;
++}
++
++int
++stc_newconn(void *cb_arg, struct mn_socket *new)
++{
++    struct mn_socket **r_sock;
++
++    r_sock = cb_arg;
++    *r_sock = new;
++
++    os_sem_release(&test_sem);
++    return 0;
++}
++
++void
++sock_tcp_connect(void)
++{
++    struct mn_socket *listen_sock;
++    struct mn_socket *sock;
++    struct mn_sockaddr_in msin;
++    struct mn_sockaddr_in msin2;
++    int rc;
++    union mn_socket_cb listen_cbs = {
++        .listen.newconn = stc_newconn,
++    };
++    union mn_socket_cb sock_cbs = {
++        .socket.writable = stc_writable
++    };
++    int connected = 0;
++    struct mn_socket *new_sock = NULL;
++
++    rc = mn_socket(&listen_sock, MN_PF_INET, MN_SOCK_STREAM, 0);
++    TEST_ASSERT(rc == 0);
++
++    msin.msin_family = MN_PF_INET;
++    msin.msin_len = sizeof(msin);
++    msin.msin_port = htons(12445);
++
++    mn_inet_pton(MN_PF_INET, "127.0.0.1", &msin.msin_addr);
++
++    mn_socket_set_cbs(listen_sock, &new_sock, &listen_cbs);
++    rc = mn_bind(listen_sock, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_listen(listen_sock, 2);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_socket(&sock, MN_PF_INET, MN_SOCK_STREAM, 0);
++    TEST_ASSERT(rc == 0);
++
++    mn_socket_set_cbs(sock, &connected, &sock_cbs);
++
++    rc = mn_connect(sock, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
++    TEST_ASSERT(rc == 0);
++    TEST_ASSERT(connected == 1);
++    TEST_ASSERT(new_sock != NULL);
++
++    /*
++     * Check endpoint data matches
++     */
++    rc = mn_getsockname(sock, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++    rc = mn_getpeername(new_sock, (struct mn_sockaddr *)&msin2);
++    TEST_ASSERT(rc == 0);
++    TEST_ASSERT(!memcmp(&msin, &msin2, sizeof(msin)));
++
++    rc = mn_getsockname(new_sock, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++    rc = mn_getpeername(sock, (struct mn_sockaddr *)&msin2);
++    TEST_ASSERT(rc == 0);
++    TEST_ASSERT(!memcmp(&msin, &msin2, sizeof(msin)));
++
++
++    if (new_sock) {
++        mn_close(new_sock);
++    }
++    mn_close(sock);
++    mn_close(listen_sock);
++}
++
++void
++sud_readable(void *cb_arg, int err)
++{
++    os_sem_release(&test_sem);
++}
++
++void
++sock_udp_data(void)
++{
++    struct mn_socket *sock1;
++    struct mn_socket *sock2;
++    struct mn_sockaddr_in msin;
++    struct mn_sockaddr_in msin2;
++    int rc;
++    union mn_socket_cb sock_cbs = {
++        .socket.readable = sud_readable
++    };
++    struct os_mbuf *m;
++    char data[] = "1234567890";
++
++    rc = mn_socket(&sock1, MN_PF_INET, MN_SOCK_DGRAM, 0);
++    TEST_ASSERT(rc == 0);
++    mn_socket_set_cbs(sock1, NULL, &sock_cbs);
++
++    rc = mn_socket(&sock2, MN_PF_INET, MN_SOCK_DGRAM, 0);
++    TEST_ASSERT(rc == 0);
++    mn_socket_set_cbs(sock2, NULL, &sock_cbs);
++
++    msin.msin_family = MN_PF_INET;
++    msin.msin_len = sizeof(msin);
++    msin.msin_port = htons(12445);
++
++    mn_inet_pton(MN_PF_INET, "127.0.0.1", &msin.msin_addr);
++
++    rc = mn_bind(sock1, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    msin2.msin_family = MN_PF_INET;
++    msin2.msin_len = sizeof(msin2);
++    msin2.msin_port = 0;
++    msin2.msin_addr.s_addr = 0;
++    rc = mn_bind(sock2, (struct mn_sockaddr *)&msin2);
++    TEST_ASSERT(rc == 0);
++
++    m = os_msys_get(sizeof(data), 0);
++    TEST_ASSERT(m);
++    rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
++    TEST_ASSERT(rc == 0);
++    rc = mn_sendto(sock2, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    /*
++     * Wait for the packet.
++     */
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_recvfrom(sock1, &m, (struct mn_sockaddr *)&msin2);
++    TEST_ASSERT(rc == 0);
++    TEST_ASSERT(m != NULL);
++    TEST_ASSERT(msin2.msin_family == MN_AF_INET);
++    TEST_ASSERT(msin2.msin_len == sizeof(msin2));
++    TEST_ASSERT(msin2.msin_port != 0);
++    TEST_ASSERT(msin2.msin_addr.s_addr != 0);
++
++    if (m) {
++        TEST_ASSERT(OS_MBUF_IS_PKTHDR(m));
++        TEST_ASSERT(OS_MBUF_PKTLEN(m) == sizeof(data));
++        TEST_ASSERT(m->om_len == sizeof(data));
++        TEST_ASSERT(!memcmp(m->om_data, data, sizeof(data)));
++    }
++
++    rc = mn_sendto(sock1, m, (struct mn_sockaddr *)&msin2);
++    TEST_ASSERT(rc == 0);
++
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_recvfrom(sock2, &m, (struct mn_sockaddr *)&msin2);
++    TEST_ASSERT(rc == 0);
++    TEST_ASSERT(m != NULL);
++    if (m) {
++        TEST_ASSERT(OS_MBUF_IS_PKTHDR(m));
++        TEST_ASSERT(OS_MBUF_PKTLEN(m) == sizeof(data));
++        TEST_ASSERT(m->om_len == sizeof(data));
++        TEST_ASSERT(!memcmp(m->om_data, data, sizeof(data)));
++        os_mbuf_free_chain(m);
++    }
++
++    mn_close(sock1);
++    mn_close(sock2);
++}
++
++void
++std_writable(void *cb_arg, int err)
++{
++    int *i;
++
++    TEST_ASSERT(err == 0);
++    i = (int *)cb_arg;
++    if (i) {
++        *i = *i + 1;
++    }
++}
++
++void
++std_readable(void *cb_arg, int err)
++{
++    os_sem_release(&test_sem);
++}
++
++static union mn_socket_cb sud_sock_cbs = {
++    .socket.writable = std_writable,
++    .socket.readable = std_readable
++};
++
++int
++std_newconn(void *cb_arg, struct mn_socket *new)
++{
++    struct mn_socket **r_sock;
++
++    r_sock = cb_arg;
++    *r_sock = new;
++
++    mn_socket_set_cbs(new, NULL, &sud_sock_cbs);
++
++    os_sem_release(&test_sem);
++    return 0;
++}
++
++void
++sock_tcp_data(void)
++{
++    struct mn_socket *listen_sock;
++    struct mn_socket *sock;
++    struct mn_sockaddr_in msin;
++    int rc;
++    union mn_socket_cb listen_cbs = {
++        .listen.newconn = std_newconn,
++    };
++    int connected = 0;
++    struct mn_socket *new_sock = NULL;
++    struct os_mbuf *m;
++    char data[] = "1234567890";
++
++    rc = mn_socket(&listen_sock, MN_PF_INET, MN_SOCK_STREAM, 0);
++    TEST_ASSERT(rc == 0);
++
++    msin.msin_family = MN_PF_INET;
++    msin.msin_len = sizeof(msin);
++    msin.msin_port = htons(12447);
++
++    mn_inet_pton(MN_PF_INET, "127.0.0.1", &msin.msin_addr);
++
++    mn_socket_set_cbs(listen_sock, &new_sock, &listen_cbs);
++    rc = mn_bind(listen_sock, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_listen(listen_sock, 2);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_socket(&sock, MN_PF_INET, MN_SOCK_STREAM, 0);
++    TEST_ASSERT(rc == 0);
++
++    mn_socket_set_cbs(sock, &connected, &sud_sock_cbs);
++
++    rc = mn_connect(sock, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
++    TEST_ASSERT(rc == 0);
++    TEST_ASSERT(connected == 1);
++    TEST_ASSERT(new_sock != NULL);
++
++    m = os_msys_get(sizeof(data), 0);
++    TEST_ASSERT(m);
++    rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
++    TEST_ASSERT(rc == 0);
++    rc = mn_sendto(new_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    /*
++     * Wait for the packet.
++     */
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
++    TEST_ASSERT(rc == 0);
++
++    memset(&msin, 0, sizeof(msin));
++    rc = mn_recvfrom(sock, &m, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++    TEST_ASSERT(m != NULL);
++    TEST_ASSERT(msin.msin_family == MN_AF_INET);
++    TEST_ASSERT(msin.msin_len == sizeof(msin));
++    TEST_ASSERT(msin.msin_port != 0);
++    TEST_ASSERT(msin.msin_addr.s_addr != 0);
++    os_mbuf_free_chain(m);
++
++    if (new_sock) {
++        mn_close(new_sock);
++    }
++    mn_close(sock);
++    mn_close(listen_sock);
++}
++
++void
++sock_itf_list(void)
++{
++    struct mn_itf itf;
++    struct mn_itf_addr itf_addr;
++    int if_cnt = 0;
++    int seen_127;
++    struct mn_in_addr addr127;
++    char addr_str[64];
++    int rc;
++
++    mn_inet_pton(MN_PF_INET, "127.0.0.1", &addr127);
++
++    memset(&itf, 0, sizeof(itf));
++
++    while (1) {
++        rc = mn_itf_getnext(&itf);
++        if (rc) {
++            break;
++        }
++        printf("%d: %x %s\n", itf.mif_idx, itf.mif_flags, itf.mif_name);
++        memset(&itf_addr, 0, sizeof(itf_addr));
++        while (1) {
++            rc = mn_itf_addr_getnext(&itf, &itf_addr);
++            if (rc) {
++                break;
++            }
++            if (itf_addr.mifa_family == MN_AF_INET &&
++              !memcmp(&itf_addr.mifa_addr, &addr127, sizeof(addr127))) {
++                seen_127 = 1;
++            }
++            addr_str[0] = '\0';
++            mn_inet_ntop(itf_addr.mifa_family, &itf_addr.mifa_addr,
++              addr_str, sizeof(addr_str));
++            printf(" %s/%d\n", addr_str, itf_addr.mifa_plen);
++        }
++        if_cnt++;
++    }
++    TEST_ASSERT(if_cnt > 0);
++    TEST_ASSERT(seen_127);
++}
++
++static int
++first_ll_addr(struct mn_sockaddr_in6 *ra)
++{
++    struct mn_itf itf;
++    struct mn_itf_addr itf_addr;
++    int rc;
++    struct mn_in6_addr *addr;
++
++    memset(&itf, 0, sizeof(itf));
++    addr = (struct mn_in6_addr *)&itf_addr.mifa_addr;
++    while (1) {
++        rc = mn_itf_getnext(&itf);
++        if (rc) {
++            break;
++        }
++        memset(&itf_addr, 0, sizeof(itf_addr));
++        while (1) {
++            rc = mn_itf_addr_getnext(&itf, &itf_addr);
++            if (rc) {
++                break;
++            }
++            if (itf_addr.mifa_family == MN_AF_INET6 &&
++              addr->s_addr[0] == 0xfe && addr->s_addr[1] == 0x80) {
++                memset(ra, 0, sizeof(*ra));
++                ra->msin6_family = MN_AF_INET6;
++                ra->msin6_len = sizeof(*ra);
++                ra->msin6_scope_id = itf.mif_idx;
++                memcpy(&ra->msin6_addr, addr, sizeof(*addr));
++                return 0;
++            }
++        }
++    }
++    return -1;
++}
++
++void
++sul_readable(void *cb_arg, int err)
++{
++    os_sem_release(&test_sem);
++}
++
++void
++sock_udp_ll(void)
++{
++    struct mn_socket *sock1;
++    struct mn_socket *sock2;
++    struct mn_sockaddr_in6 msin;
++    struct mn_sockaddr_in6 msin2;
++    int rc;
++    union mn_socket_cb sock_cbs = {
++        .socket.readable = sul_readable
++    };
++    struct os_mbuf *m;
++    char data[] = "1234567890";
++
++    rc = mn_socket(&sock1, MN_PF_INET6, MN_SOCK_DGRAM, 0);
++    TEST_ASSERT(rc == 0);
++    mn_socket_set_cbs(sock1, NULL, &sock_cbs);
++
++    rc = mn_socket(&sock2, MN_PF_INET6, MN_SOCK_DGRAM, 0);
++    TEST_ASSERT(rc == 0);
++    mn_socket_set_cbs(sock2, NULL, &sock_cbs);
++
++    rc = first_ll_addr(&msin);
++    if (rc != 0) {
++        printf("No ipv6 address present?\n");
++        return;
++    }
++    msin.msin6_port = htons(12445);
++
++    rc = mn_bind(sock1, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_getsockname(sock1, (struct mn_sockaddr *)&msin2);
++    TEST_ASSERT(rc == 0);
++
++    m = os_msys_get(sizeof(data), 0);
++    TEST_ASSERT(m);
++    rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
++    TEST_ASSERT(rc == 0);
++    rc = mn_sendto(sock2, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin2);
++    TEST_ASSERT(rc == 0);
++
++    /*
++     * Wait for the packet.
++     */
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_recvfrom(sock1, &m, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++    TEST_ASSERT(m != NULL);
++
++    if (m) {
++        TEST_ASSERT(OS_MBUF_IS_PKTHDR(m));
++        TEST_ASSERT(OS_MBUF_PKTLEN(m) == sizeof(data));
++        TEST_ASSERT(m->om_len == sizeof(data));
++        TEST_ASSERT(!memcmp(m->om_data, data, sizeof(data)));
++        os_mbuf_free_chain(m);
++    }
++
++    mn_close(sock1);
++    mn_close(sock2);
++}
++
++static int
++sock_find_multicast_if(void)
++{
++    struct mn_itf itf;
++
++    memset(&itf, 0, sizeof(itf));
++
++    while (1) {
++        if (mn_itf_getnext(&itf)) {
++            break;
++        }
++        if ((itf.mif_flags & MN_ITF_F_UP) == 0) {
++            continue;
++        }
++        if (itf.mif_flags & MN_ITF_F_MULTICAST) {
++            return itf.mif_idx;
++        }
++    }
++    return -1;
++}
++
++void
++sum4_readable(void *cb_arg, int err)
++{
++    os_sem_release(&test_sem);
++}
++
++static void
++sock_udp_mcast_v4(void)
++{
++    int loop_if_idx;
++    struct mn_socket *rx_sock;
++    struct mn_socket *tx_sock;
++    struct mn_sockaddr_in msin;
++    union mn_socket_cb sock_cbs = {
++        .socket.readable = sum4_readable
++    };
++    struct os_mbuf *m;
++    char data[] = "1234567890";
++    int rc;
++    struct mn_mreq mreq;
++    loop_if_idx = sock_find_multicast_if();
++    TEST_ASSERT(loop_if_idx > 0);
++
++    msin.msin_family = MN_AF_INET;
++    msin.msin_len = sizeof(msin);
++    msin.msin_port = htons(44344);
++    memset(&msin.msin_addr, 0, sizeof(msin.msin_addr));
++
++    rc = mn_socket(&rx_sock, MN_PF_INET, MN_SOCK_DGRAM, 0);
++    TEST_ASSERT(rc == 0);
++    mn_socket_set_cbs(rx_sock, NULL, &sock_cbs);
++
++    rc = mn_bind(rx_sock, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_socket(&tx_sock, MN_PF_INET, MN_SOCK_DGRAM, 0);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_setsockopt(tx_sock, MN_SO_LEVEL, MN_MCAST_IF, &loop_if_idx);
++    TEST_ASSERT(rc == 0);
++
++    m = os_msys_get(sizeof(data), 0);
++    rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
++    TEST_ASSERT(rc == 0);
++
++    /*
++     * multicast tgt
++     */
++    mn_inet_pton(MN_PF_INET, "224.0.2.241", &msin.msin_addr);
++
++    rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    /*
++     * RX socket has not joined group yet.
++     */
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC / 2);
++    TEST_ASSERT(rc == OS_TIMEOUT);
++
++    mreq.mm_idx = loop_if_idx;
++    mreq.mm_family = MN_AF_INET;
++    mreq.mm_addr.v4.s_addr = msin.msin_addr.s_addr;
++
++    /*
++     * Now join it.
++     */
++    rc = mn_setsockopt(rx_sock, MN_SO_LEVEL, MN_MCAST_JOIN_GROUP, &mreq);
++    TEST_ASSERT(rc == 0);
++
++    m = os_msys_get(sizeof(data), 0);
++    rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_recvfrom(rx_sock, &m, NULL);
++    TEST_ASSERT(rc == 0);
++    TEST_ASSERT(m != NULL);
++    TEST_ASSERT(!memcmp(m->om_data, data, sizeof(data)));
++    os_mbuf_free_chain(m);
++
++    /*
++     * Then leave
++     */
++    rc = mn_setsockopt(rx_sock, MN_SO_LEVEL, MN_MCAST_LEAVE_GROUP, &mreq);
++    TEST_ASSERT(rc == 0);
++
++    m = os_msys_get(sizeof(data), 0);
++    TEST_ASSERT(m);
++    rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin);
++    TEST_ASSERT(rc == 0);
++
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
++    TEST_ASSERT(rc == OS_TIMEOUT);
++
++    mn_close(rx_sock);
++    mn_close(tx_sock);
++}
++
++static void
++sock_udp_mcast_v6(void)
++{
++    int loop_if_idx;
++    struct mn_socket *rx_sock;
++    struct mn_socket *tx_sock;
++    struct mn_sockaddr_in6 msin6;
++    union mn_socket_cb sock_cbs = {
++        .socket.readable = sum4_readable
++    };
++    struct os_mbuf *m;
++    char data[] = "1234567890";
++    int rc;
++    struct mn_mreq mreq;
++    uint8_t mcast_addr[16] = {
++        0xff, 2, 0, 0,
++        0, 0, 0, 0,
++        0, 0, 0, 0,
++        0, 0, 0, 2
++    };
++
++    loop_if_idx = sock_find_multicast_if();
++    TEST_ASSERT(loop_if_idx > 0);
++
++    msin6.msin6_family = MN_AF_INET6;
++    msin6.msin6_len = sizeof(msin6);
++    msin6.msin6_port = htons(44344);
++    memset(&msin6.msin6_addr, 0, sizeof(msin6.msin6_addr));
++
++    rc = mn_socket(&rx_sock, MN_PF_INET6, MN_SOCK_DGRAM, 0);
++    TEST_ASSERT(rc == 0);
++    mn_socket_set_cbs(rx_sock, NULL, &sock_cbs);
++
++    rc = mn_bind(rx_sock, (struct mn_sockaddr *)&msin6);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_socket(&tx_sock, MN_PF_INET6, MN_SOCK_DGRAM, 0);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_setsockopt(tx_sock, MN_SO_LEVEL, MN_MCAST_IF, &loop_if_idx);
++    TEST_ASSERT(rc == 0);
++
++    m = os_msys_get(sizeof(data), 0);
++    rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
++    TEST_ASSERT(rc == 0);
++
++    /*
++     * multicast tgt
++     */
++    memcpy(&msin6.msin6_addr, mcast_addr, sizeof(mcast_addr));
++
++    rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin6);
++    TEST_ASSERT(rc == 0);
++
++    /*
++     * RX socket has not joined group yet.
++     */
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC / 2);
++    TEST_ASSERT(rc == OS_TIMEOUT);
++
++    mreq.mm_idx = loop_if_idx;
++    mreq.mm_family = MN_AF_INET6;
++    memcpy(&mreq.mm_addr.v6.s_addr, msin6.msin6_addr.s_addr,
++      sizeof(msin6.msin6_addr.s_addr));
++
++    /*
++     * Now join it.
++     */
++    rc = mn_setsockopt(rx_sock, MN_SO_LEVEL, MN_MCAST_JOIN_GROUP, &mreq);
++    TEST_ASSERT(rc == 0);
++
++    m = os_msys_get(sizeof(data), 0);
++    rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin6);
++    TEST_ASSERT(rc == 0);
++
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_recvfrom(rx_sock, &m, NULL);
++    TEST_ASSERT(rc == 0);
++    TEST_ASSERT(m != NULL);
++    TEST_ASSERT(!memcmp(m->om_data, data, sizeof(data)));
++    os_mbuf_free_chain(m);
++
++    /*
++     * Then leave
++     */
++    rc = mn_setsockopt(rx_sock, MN_SO_LEVEL, MN_MCAST_LEAVE_GROUP, &mreq);
++    TEST_ASSERT(rc == 0);
++
++    m = os_msys_get(sizeof(data), 0);
++    TEST_ASSERT(m);
++    rc = os_mbuf_copyinto(m, 0, data, sizeof(data));
++    TEST_ASSERT(rc == 0);
++
++    rc = mn_sendto(tx_sock, (struct os_mbuf *)m, (struct mn_sockaddr *)&msin6);
++    TEST_ASSERT(rc == 0);
++
++    rc = os_sem_pend(&test_sem, OS_TICKS_PER_SEC);
++    TEST_ASSERT(rc == OS_TIMEOUT);
++
++    mn_close(rx_sock);
++    mn_close(tx_sock);
++}
++
++void
++mn_socket_test_handler(void *arg)
++{
++    sock_open_close();
++    sock_listen();
++    sock_tcp_connect();
++    sock_udp_data();
++    sock_tcp_data();
++    sock_itf_list();
++    sock_udp_ll();
++    sock_udp_mcast_v4();
++    sock_udp_mcast_v6();
++    os_test_restart();
++}
++
++TEST_CASE(socket_tests)
++{
++    os_init();
++    native_sock_init();
++
++    os_sem_init(&test_sem, 0);
++
++    os_task_init(&test_task, "mn_socket_test", mn_socket_test_handler, NULL,
++      TEST_PRIO, OS_WAIT_FOREVER, test_stack, TEST_STACK_SIZE);
++    os_start();
++}
++
 +TEST_SUITE(mn_socket_test_all)
 +{
++    int rc;
++
++    rc = os_mempool_init(&test_mbuf_mpool, MB_CNT, MB_SZ, test_mbuf_area, "mb");
++    TEST_ASSERT(rc == 0);
++    rc = os_mbuf_pool_init(&test_mbuf_pool, &test_mbuf_mpool, MB_CNT, MB_CNT);
++    TEST_ASSERT(rc == 0);
++    rc = os_msys_register(&test_mbuf_pool);
++    TEST_ASSERT(rc == 0);
++
 +    inet_pton_test();
++    inet_ntop_test();
++
++    socket_tests();
 +}
 +
 +#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/c5901fcc/sys/reboot/src/log_reboot.c
----------------------------------------------------------------------
diff --cc sys/reboot/src/log_reboot.c
index c7c3bbe,8eff1e3..6a6bb63
--- a/sys/reboot/src/log_reboot.c
+++ b/sys/reboot/src/log_reboot.c
@@@ -17,24 -17,25 +17,23 @@@
   * 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;
+ static struct log_handler *reboot_log_handler;
  static struct log reboot_log;
  static uint16_t reboot_cnt;
  static uint16_t soft_reboot;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/sys/stats/src/stats.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/sys/stats/src/stats_nmgr.c
----------------------------------------------------------------------



[03/50] [abbrv] incubator-mynewt-core git commit: iotivity; fix handling multiple query arguments.

Posted by vi...@apache.org.
iotivity; fix handling multiple query arguments.


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

Branch: refs/heads/sterly_refactor
Commit: c1aab9858b80893d52eec2563ca0180f69af1e43
Parents: 5e92585
Author: Marko Kiiskila <ma...@runtime.io>
Authored: Tue Sep 20 14:08:00 2016 -0700
Committer: Marko Kiiskila <ma...@runtime.io>
Committed: Tue Sep 20 14:08:00 2016 -0700

----------------------------------------------------------------------
 libs/iotivity/src/api/oc_ri.c         | 2 +-
 libs/iotivity/src/api/oc_server_api.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c1aab985/libs/iotivity/src/api/oc_ri.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/api/oc_ri.c b/libs/iotivity/src/api/oc_ri.c
index 7af353e..76d7ed2 100644
--- a/libs/iotivity/src/api/oc_ri.c
+++ b/libs/iotivity/src/api/oc_ri.c
@@ -168,7 +168,7 @@ oc_ri_get_query_value(const char *query, int query_len, const char *key,
   int next_pos = 0, found = -1, kl, vl;
   char *k;
   while (next_pos < query_len) {
-    next_pos = oc_ri_get_query_nth_key_value(
+    next_pos += oc_ri_get_query_nth_key_value(
       query + next_pos, query_len - next_pos, &k, &kl, value, &vl, 1);
     if (next_pos == -1)
       return -1;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c1aab985/libs/iotivity/src/api/oc_server_api.c
----------------------------------------------------------------------
diff --git a/libs/iotivity/src/api/oc_server_api.c b/libs/iotivity/src/api/oc_server_api.c
index 8650d27..054a91c 100644
--- a/libs/iotivity/src/api/oc_server_api.c
+++ b/libs/iotivity/src/api/oc_server_api.c
@@ -219,7 +219,7 @@ oc_interate_query(oc_request_t *request, char **key, int *key_len, char **value,
 {
   if (query_iterator >= request->query_len)
     return -1;
-  query_iterator = oc_ri_get_query_nth_key_value(
+  query_iterator += oc_ri_get_query_nth_key_value(
     request->query + query_iterator, request->query_len - query_iterator, key,
     key_len, value, value_len, 1);
   return 1;


[48/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-391 STM32F4 ADC driver and ADC HAL support

Posted by vi...@apache.org.
MYNEWT-391 STM32F4 ADC driver and ADC HAL support

- STM32F4 ADC Initial Commit
- Adding driver


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

Branch: refs/heads/sterly_refactor
Commit: 082a033ed4ed393ee56eb22369e4d7c73ffc7c3e
Parents: 063a01b
Author: Vipul Rahane <vi...@runtime.io>
Authored: Fri Sep 2 13:38:23 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Thu Sep 22 20:21:03 2016 -0700

----------------------------------------------------------------------
 .../include/adc_stm32f4/adc_stm32f4.h           |  39 +++
 drivers/adc/adc_stm32f4/pkg.yml                 |  33 +++
 drivers/adc/adc_stm32f4/src/adc_stm32f4.c       | 245 +++++++++++++++++++
 3 files changed, 317 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/082a033e/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h b/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h
new file mode 100644
index 0000000..e5a494f
--- /dev/null
+++ b/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h
@@ -0,0 +1,39 @@
+/*
+ * 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 __ADC_STM32F4_H__
+#define __ADC_STM32F4_H__
+
+#include <adc/adc.h>
+
+struct stm32f4_adc_dev_cfg {
+    uint8_t sac_chan_count;
+    uint8_t *sac_chans;
+    ADC_InitTypeDef sac_init;
+};
+
+struct stm32f4_adc_dev {
+    struct adc_dev sad_dev;
+    ADC_TypeDef *sad_adc;
+    ADC_InitTypeDef *sad_init;
+};
+
+int stm32f4_adc_dev_init(struct os_dev *, void *);
+
+#endif /* __ADC_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/082a033e/drivers/adc/adc_stm32f4/pkg.yml
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/pkg.yml b/drivers/adc/adc_stm32f4/pkg.yml
new file mode 100644
index 0000000..205dfa8
--- /dev/null
+++ b/drivers/adc/adc_stm32f4/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: drivers/adc/adc_stm32f4
+pkg.description: ADC driver for the STM32F4 series of processors.
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+pkg.features: 
+    - ADC_STM32F4
+pkg.apis: 
+    - ADC_HW_IMPL
+pkg.deps.TEST:
+   - hw/hal
+   - hw/mcu/stm/stm32f4xx
+   - drivers/adc
+   - libs/testutil

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/082a033e/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/src/adc_stm32f4.c b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
new file mode 100644
index 0000000..95a068e
--- /dev/null
+++ b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
@@ -0,0 +1,245 @@
+/*
+ * 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 <hal/hal_bsp.h>
+#include <adc/adc.h>
+#include <assert.h>
+#include <os/os.h>
+#include <bsp/cmsis_nvic.h>
+
+struct stm32f4_adc_stats {
+    uint16_t adc_events;
+    uint16_t adc_events_failed;
+};
+static struct stm32f4_adc_stats stm32f4_adc_stats;
+
+
+/**
+ * Open the STM32F4 ADC device
+ *
+ * This function locks the device for access from other tasks.
+ *
+ * @param odev The OS device to open
+ * @param wait The time in MS to wait.  If 0 specified, returns immediately
+ *             if resource unavailable.  If OS_WAIT_FOREVER specified, blocks
+ *             until resource is available.
+ * @param arg  Argument provided by higher layer to open.
+ *
+ * @return 0 on success, non-zero on failure.
+ */
+static int
+stm32f4_adc_open(struct os_dev *odev, uint32_t wait, void *arg)
+{
+    struct adc_dev *dev;
+    int rc;
+
+    dev = (struct adc_dev *) odev;
+
+    if (os_started()) {
+        rc = os_mutex_pend(&dev->ad_lock, wait);
+        if (rc != OS_OK) {
+            goto err;
+        }
+    }
+
+    if (odev->od_status & OS_DEV_STATUS_OPEN) {
+        os_mutex_release(&dev->ad_lock);
+        rc = OS_EBUSY;
+        goto err;
+    }
+
+    return (0);
+err:
+    return (rc);
+}
+
+
+/**
+ * Close the STM32F4 ADC device.
+ *
+ * This function unlocks the device.
+ *
+ * @param odev The device to close.
+ */
+static int
+stm32f4_adc_close(struct os_dev *odev)
+{
+    struct adc_dev *dev;
+
+    dev = (struct adc_dev *) odev;
+
+    if (os_started()) {
+        os_mutex_release(&dev->ad_lock);
+    }
+
+    return (0);
+}
+
+/**
+ * Configure an ADC channel on the Nordic ADC.
+ *
+ * @param dev The ADC device to configure
+ * @param cnum The channel on the ADC device to configure
+ * @param cfgdata An opaque pointer to channel config, expected to be
+ *                a nrf_saadc_channel_config_t
+ *
+ * @return 0 on success, non-zero on failure.
+ */
+static int
+stm32f4_adc_configure_channel(struct adc_dev *dev, uint8_t cnum,
+        void *cfgdata)
+{
+    uint16_t refmv;
+    uint8_t res;
+    int rc;
+
+    /* XXX: Dummy values prior to implementation. */
+    res = 16;
+    refmv = 2800;
+
+    /* Store these values in channel definitions, for conversions to
+     * milivolts.
+     */
+    dev->ad_chans[cnum].c_res = res;
+    dev->ad_chans[cnum].c_refmv = refmv;
+    dev->ad_chans[cnum].c_configured = 1;
+
+    return (0);
+err:
+    return (rc);
+}
+
+/**
+ * Set buffer to read data into.  Implementation of setbuffer handler.
+ * Sets both the primary and secondary buffers for DMA.
+ */
+static int
+stm32f4_adc_set_buffer(struct adc_dev *dev, void *buf1, void *buf2,
+        int buf_len)
+{
+    int rc;
+
+    rc = OS_EINVAL;
+    goto err;
+
+    return (0);
+err:
+    return (rc);
+}
+
+static int
+stm32f4_adc_release_buffer(struct adc_dev *dev, void *buf, int buf_len)
+{
+    int rc;
+
+    rc = OS_EINVAL;
+    goto err;
+
+    return (0);
+err:
+    return (rc);
+}
+
+/**
+ * Trigger an ADC sample.
+ */
+static int
+stm32f4_adc_sample(struct adc_dev *dev)
+{
+    return (0);
+}
+
+/**
+ * Blocking read of an ADC channel, returns result as an integer.
+ */
+static int
+stm32f4_adc_read_channel(struct adc_dev *dev, uint8_t cnum, int *result)
+{
+    int rc;
+
+    rc = OS_EINVAL;
+    goto err;
+
+    return (0);
+err:
+    return (rc);
+}
+
+static int
+stm32f4_adc_read_buffer(struct adc_dev *dev, void *buf, int buf_len, int off,
+        int *result)
+{
+    return (0);
+}
+
+static int
+stm32f4_adc_size_buffer(struct adc_dev *dev, int chans, int samples)
+{
+    return (0 * chans * samples);
+}
+
+
+/**
+ * Callback to initialize an adc_dev structure from the os device
+ * initialization callback.  This sets up a nrf52_adc_device(), so
+ * that subsequent lookups to this device allow us to manipulate it.
+ */
+int
+stm32f4_adc_dev_init(struct os_dev *odev, void *arg)
+{
+    struct stm32f4_adc_dev *sad;
+    struct stm32f4_adc_dev_cfg *sac;
+    struct adc_dev *dev;
+    struct adc_driver_funcs *af;
+
+    sad = (struct stm32f4_adc_dev *) odev;
+    sac = (struct stm32f4_adc_dev_cfg *) arg;
+
+    assert(sad != NULL);
+    assert(sac != NULL);
+
+    dev = (struct adc_dev *) &sad->sad_dev;
+
+    os_mutex_init(&dev->ad_lock);
+
+    /* Have a pointer to the init typedef from the configured
+     * value.  This allows non-driver specific items to be configured.
+     */
+    sad->sad_init = &sac->sac_init;
+
+    dev->ad_chans = (void *) sac->sac_chans;
+    dev->ad_chan_count = sac->sac_chan_count;
+
+    OS_DEV_SETHANDLERS(odev, stm32f4_adc_open, stm32f4_adc_close);
+
+    af = &dev->ad_funcs;
+
+    af->af_configure_channel = stm32f4_adc_configure_channel;
+    af->af_sample = stm32f4_adc_sample;
+    af->af_read_channel = stm32f4_adc_read_channel;
+    af->af_set_buffer = stm32f4_adc_set_buffer;
+    af->af_release_buffer = stm32f4_adc_release_buffer;
+    af->af_read_buffer = stm32f4_adc_read_buffer;
+    af->af_size_buffer = stm32f4_adc_size_buffer;
+
+    return (0);
+}
+
+


[40/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-3 HAL SPI API changes

Posted by vi...@apache.org.
MYNEWT-3 HAL SPI API changes

This commit adds a simple spitest program along with the changes
to the HAL SPI API. The commit also contains changes for some of
the nordic BSPS (not all of them). The additional BSP's will be
added shortly.


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

Branch: refs/heads/sterly_refactor
Commit: 26dfea0fb142a9071c94d0d3bd69e5cfe96dfeae
Parents: 2454b8c
Author: William San Filippo <wi...@runtime.io>
Authored: Thu Sep 22 17:38:58 2016 -0700
Committer: William San Filippo <wi...@runtime.io>
Committed: Thu Sep 22 17:41:49 2016 -0700

----------------------------------------------------------------------
 apps/spitest/pkg.yml                        |  36 +++
 apps/spitest/src/main.c                     | 360 +++++++++++++++++++++
 hw/bsp/nrf51dk/include/bsp/bsp.h            |   5 +-
 hw/bsp/nrf51dk/include/bsp/nrf_drv_config.h |   2 -
 hw/bsp/nrf51dk/pkg.yml                      |  10 +-
 hw/bsp/nrf51dk/src/os_bsp.c                 |   5 +-
 hw/bsp/nrf52dk/include/bsp/bsp.h            |   5 +-
 hw/bsp/nrf52dk/include/bsp/nrf_drv_config.h |   2 -
 hw/bsp/nrf52dk/src/os_bsp.c                 |   6 +-
 hw/hal/include/hal/hal_spi.h                |  81 +++--
 hw/mcu/nordic/nrf51xxx/src/hal_spi.c        | 299 ++++++++++-------
 hw/mcu/nordic/nrf52xxx/src/hal_spi.c        | 396 +++++++++++++++--------
 12 files changed, 919 insertions(+), 288 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/apps/spitest/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/spitest/pkg.yml b/apps/spitest/pkg.yml
new file mode 100644
index 0000000..58a8872
--- /dev/null
+++ b/apps/spitest/pkg.yml
@@ -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.
+#
+
+pkg.name: apps/spitest
+pkg.type: app
+pkg.description: A board to board test for hal spi master/slave.
+pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+    - libs/console/full
+    - libs/os
+    - libs/shell
+    - sys/config
+    - sys/log
+    - sys/stats
+
+pkg.syscfg_vals:
+    - SHELL_TASK: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/apps/spitest/src/main.c
----------------------------------------------------------------------
diff --git a/apps/spitest/src/main.c b/apps/spitest/src/main.c
new file mode 100755
index 0000000..3e8fafa
--- /dev/null
+++ b/apps/spitest/src/main.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 "os/os.h"
+#include "bsp/bsp.h"
+#include "hal/hal_gpio.h"
+#include "hal/hal_spi.h"
+#include "stats/stats.h"
+#include "config/config.h"
+#include <os/os_dev.h>
+#include <assert.h>
+#include <string.h>
+#ifdef ARCH_sim
+#include <mcu/mcu_sim.h>
+#endif
+#include "app_util_platform.h"
+#include "app_error.h"
+
+/* The spi txrx callback */
+struct sblinky_spi_cb_arg
+{
+    int transfers;
+    int txlen;
+    uint32_t tx_rx_bytes;
+};
+struct sblinky_spi_cb_arg spi_cb_obj;
+void *spi_cb_arg;
+
+/* Init all tasks */
+volatile int tasks_initialized;
+int init_tasks(void);
+
+/* Task 1 */
+#define TASK1_PRIO (1)
+#define TASK1_STACK_SIZE    OS_STACK_ALIGN(1024)
+struct os_task task1;
+os_stack_t stack1[TASK1_STACK_SIZE];
+
+/* Global test semaphore */
+struct os_sem g_test_sem;
+
+/* For LED toggling */
+int g_led_pin;
+
+#if MYNEWT_VAL(SPI_MASTER)
+uint8_t g_spi_tx_buf[32];
+uint8_t g_spi_rx_buf[32];
+uint32_t g_spi_xfr_num;
+uint8_t g_spi_null_rx;
+
+void
+sblinky_spi_irqm_handler(void *arg, int len)
+{
+    int i;
+    uint8_t expval;
+    struct sblinky_spi_cb_arg *cb;
+
+    hal_gpio_set(SPI_SS_PIN);
+
+    assert(arg == spi_cb_arg);
+    if (spi_cb_arg) {
+        cb = (struct sblinky_spi_cb_arg *)arg;
+        assert(len == cb->txlen);
+        ++cb->transfers;
+    }
+
+    /* Make sure we get back the data we expect! */
+    if (g_spi_xfr_num == 1) {
+        /* The first time we expect entire buffer to be filled with 0x88 */
+        for (i = 0; i < len; ++i) {
+            if (g_spi_rx_buf[i] != 0x88) {
+                assert(0);
+            }
+        }
+    } else {
+        /*
+         * We expect the buffer to be filled with an incrementing pattern
+         * That starts at 31 less than the first value sent this time.
+         */
+        if (g_spi_null_rx == 0) {
+            expval = g_spi_tx_buf[0];
+            expval = (uint8_t)(expval - 31);
+            for (i = 0; i < len; ++i) {
+                if (g_spi_rx_buf[i] != expval) {
+                    assert(0);
+                }
+                ++expval;
+            }
+        }
+    }
+    ++g_spi_xfr_num;
+}
+
+void
+sblinky_spi_cfg(int spi_num)
+{
+    int spi_id;
+    struct hal_spi_settings my_spi;
+
+    my_spi.data_order = HAL_SPI_MSB_FIRST;
+    my_spi.data_mode = HAL_SPI_MODE0;
+    my_spi.baudrate = 8000;
+    my_spi.word_size = HAL_SPI_WORD_SIZE_8BIT;
+    spi_id = 0;
+    hal_spi_config(spi_id, &my_spi);
+}
+#endif
+
+#if MYNEWT_VAL(SPI_SLAVE)
+uint8_t g_spi_tx_buf[32];
+uint8_t g_spi_rx_buf[32];
+uint32_t g_spi_xfr_num;
+
+/* XXX: This is an ugly hack for now. */
+#ifdef NRF51
+#define SPI_SLAVE_ID    (1)
+#else
+#define SPI_SLAVE_ID    (0)
+#endif
+
+void
+sblinky_spi_irqs_handler(void *arg, int len)
+{
+    struct sblinky_spi_cb_arg *cb;
+
+    assert(arg == spi_cb_arg);
+    if (spi_cb_arg) {
+        cb = (struct sblinky_spi_cb_arg *)arg;
+        ++cb->transfers;
+        cb->tx_rx_bytes += len;
+    }
+
+    /* Post semaphore to task waiting for SPI slave */
+    os_sem_release(&g_test_sem);
+}
+
+void
+sblinky_spi_cfg(int spi_num)
+{
+    int spi_id;
+    struct hal_spi_settings my_spi;
+
+    my_spi.data_order = HAL_SPI_MSB_FIRST;
+    my_spi.data_mode = HAL_SPI_MODE0;
+    my_spi.baudrate = 0;
+    my_spi.word_size = HAL_SPI_WORD_SIZE_8BIT;
+    spi_id = SPI_SLAVE_ID;
+    hal_spi_config(spi_id, &my_spi);
+    hal_spi_set_txrx_cb(spi_num, sblinky_spi_irqs_handler, spi_cb_arg);
+}
+#endif
+
+#if MYNEWT_VAL(SPI_MASTER)
+void
+task1_handler(void *arg)
+{
+    int i;
+    int rc;
+    uint16_t rxval;
+    uint8_t last_val;
+    uint8_t spi_nb_cntr;
+    uint8_t spi_b_cntr;
+
+    /* Set the led pin for the E407 devboard */
+    g_led_pin = LED_BLINK_PIN;
+    hal_gpio_init_out(g_led_pin, 1);
+
+    /* Use SS pin for testing */
+    hal_gpio_init_out(SPI_SS_PIN, 1);
+    sblinky_spi_cfg(0);
+    hal_spi_set_txrx_cb(0, NULL, NULL);
+    hal_spi_enable(0);
+
+    /*
+     * Send some bytes in a non-blocking manner to SPI using tx val. The
+     * slave should send back 0x77.
+     */
+    g_spi_tx_buf[0] = 0xde;
+    g_spi_tx_buf[1] = 0xad;
+    g_spi_tx_buf[2] = 0xbe;
+    g_spi_tx_buf[3] = 0xef;
+    hal_gpio_clear(SPI_SS_PIN);
+    for (i = 0; i < 4; ++i) {
+        rxval = hal_spi_tx_val(0, g_spi_tx_buf[i]);
+        assert(rxval == 0x77);
+        g_spi_rx_buf[i] = (uint8_t)rxval;
+    }
+    hal_gpio_set(SPI_SS_PIN);
+    ++g_spi_xfr_num;
+
+    /* Set up the callback to use when non-blocking API used */
+    hal_spi_disable(0);
+    spi_cb_arg = &spi_cb_obj;
+    spi_cb_obj.txlen = 32;
+    hal_spi_set_txrx_cb(0, sblinky_spi_irqm_handler, spi_cb_arg);
+    hal_spi_enable(0);
+    spi_nb_cntr = 0;
+    spi_b_cntr = 0;
+
+    while (1) {
+        /* Wait one second */
+        os_time_delay(OS_TICKS_PER_SEC);
+
+        /* Toggle the LED */
+        hal_gpio_toggle(g_led_pin);
+
+        /*
+         * Send to a slave, using blocking and non-blocking calls. The
+         * slave should be sending back the last thing we sent it.
+         */
+        last_val = g_spi_tx_buf[31];
+        for (i = 0; i < 32; ++i) {
+            g_spi_tx_buf[i] = (uint8_t)(last_val + i);
+        }
+
+        if (g_spi_xfr_num & 1) {
+            /* Send non-blocking */
+            ++spi_nb_cntr;
+            assert(hal_gpio_read(SPI_SS_PIN) == 1);
+            hal_gpio_clear(SPI_SS_PIN);
+            if (spi_nb_cntr == 7) {
+                g_spi_null_rx = 1;
+                rc = hal_spi_txrx_noblock(0, g_spi_tx_buf, NULL, 32);
+            } else {
+                g_spi_null_rx = 0;
+                rc = hal_spi_txrx_noblock(0, g_spi_tx_buf, g_spi_rx_buf, 32);
+            }
+            assert(!rc);
+        } else {
+            /* Send blocking */
+            ++spi_b_cntr;
+            assert(hal_gpio_read(SPI_SS_PIN) == 1);
+            hal_gpio_clear(SPI_SS_PIN);
+            if (spi_b_cntr == 7) {
+                g_spi_null_rx = 1;
+                rc = hal_spi_txrx(0, g_spi_tx_buf, NULL, 32);
+                spi_b_cntr = 0;
+            } else {
+                g_spi_null_rx = 0;
+                rc = hal_spi_txrx(0, g_spi_tx_buf, g_spi_rx_buf, 32);
+            }
+            assert(!rc);
+            hal_gpio_set(SPI_SS_PIN);
+        }
+    }
+}
+#endif
+
+#if MYNEWT_VAL(SPI_SLAVE)
+void
+task1_handler(void *arg)
+{
+    int rc;
+
+    /* Set the led pin for the E407 devboard */
+    g_led_pin = LED_BLINK_PIN;
+    hal_gpio_init_out(g_led_pin, 1);
+
+    sblinky_spi_cfg(SPI_SLAVE_ID);
+    hal_spi_enable(SPI_SLAVE_ID);
+
+    /* Make the default character 0x77 */
+    hal_spi_slave_set_def_tx_val(SPI_SLAVE_ID, 0x77);
+
+    /*
+     * Fill buffer with 0x77 for first transfer. This should be a 0xdeadbeef
+     * transfer from master to start things off
+     */
+    memset(g_spi_tx_buf, 0x77, 32);
+    rc = hal_spi_txrx_noblock(SPI_SLAVE_ID, g_spi_tx_buf, g_spi_rx_buf,
+                              32);
+
+    while (1) {
+        /* Wait for semaphore from ISR */
+        os_sem_pend(&g_test_sem, OS_TIMEOUT_NEVER);
+
+        if (g_spi_xfr_num == 0) {
+            /* Since we dont know what master will send, we fill 0x88 */
+            memset(g_spi_tx_buf, 0x88, 32);
+            rc = hal_spi_txrx_noblock(SPI_SLAVE_ID, g_spi_tx_buf, g_spi_rx_buf,
+                                      32);
+        } else {
+            /* transmit back what we just received */
+            memcpy(g_spi_tx_buf, g_spi_rx_buf, 32);
+            rc = hal_spi_txrx_noblock(SPI_SLAVE_ID, g_spi_tx_buf, g_spi_rx_buf,
+                                      32);
+            assert(rc == 0);
+        }
+        ++g_spi_xfr_num;
+
+        /* Toggle the LED */
+        hal_gpio_toggle(g_led_pin);
+    }
+}
+#endif
+
+/**
+ * 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);
+
+    tasks_initialized = 1;
+    return 0;
+}
+
+/**
+ * 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.
+ *
+ * @return int NOTE: this function should never return!
+ */
+int
+main(int argc, char **argv)
+{
+    int rc;
+
+#ifdef ARCH_sim
+    mcu_sim_parse_args(argc, argv);
+#endif
+
+    os_init();
+    rc = init_tasks();
+    os_start();
+
+    /* os start should never return. If it does, this should be an error */
+    assert(0);
+
+    return rc;
+}
+

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/hw/bsp/nrf51dk/include/bsp/bsp.h
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/include/bsp/bsp.h b/hw/bsp/nrf51dk/include/bsp/bsp.h
index e5d73a5..205dad9 100644
--- a/hw/bsp/nrf51dk/include/bsp/bsp.h
+++ b/hw/bsp/nrf51dk/include/bsp/bsp.h
@@ -6,7 +6,7 @@
  * 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,
@@ -39,6 +39,9 @@ extern uint8_t _ram_start;
 /* LED pins */
 #define LED_BLINK_PIN   (21)
 
+/* SPI SS */
+#define SPI_SS_PIN      (24)
+
 /* UART info */
 #define CONSOLE_UART            "uart0"
 #define CONSOLE_UART_SPEED      115200

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/hw/bsp/nrf51dk/include/bsp/nrf_drv_config.h
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/include/bsp/nrf_drv_config.h b/hw/bsp/nrf51dk/include/bsp/nrf_drv_config.h
index f97fb5c..feac50b 100644
--- a/hw/bsp/nrf51dk/include/bsp/nrf_drv_config.h
+++ b/hw/bsp/nrf51dk/include/bsp/nrf_drv_config.h
@@ -209,7 +209,6 @@
 #if (SPI0_ENABLED == 1)
 #define SPI0_USE_EASY_DMA 0
 /* NOTE: SCK definitions match NRF51 DK product brief */
-#define SPI0_CONFIG_CSN_PIN         24 /* Note: not defined by SDK */
 #define SPI0_CONFIG_SCK_PIN         29
 #define SPI0_CONFIG_MOSI_PIN        25
 #define SPI0_CONFIG_MISO_PIN        28
@@ -261,7 +260,6 @@
 #define SPIS1_ENABLED 1
 
 #if (SPIS1_ENABLED == 1)
-#define SPIS1_CONFIG_CSN_PIN         24 /* Note: not defined by SDK */
 #define SPIS1_CONFIG_SCK_PIN         29
 #define SPIS1_CONFIG_MOSI_PIN        25
 #define SPIS1_CONFIG_MISO_PIN        28

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/hw/bsp/nrf51dk/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/pkg.yml b/hw/bsp/nrf51dk/pkg.yml
index 2be9fc2..8ad78e3 100644
--- a/hw/bsp/nrf51dk/pkg.yml
+++ b/hw/bsp/nrf51dk/pkg.yml
@@ -65,16 +65,10 @@ pkg.syscfg_defs:
 
     ADC_0:
         description: 'TBD'
-        value: 0
-    ADC_0_RESOLUTION:
-        description: 'TBD'
-        value: 'SAADC_CONFIG_RESOLUTION'
-    ADC_0_OVERSAMPLE:
-        description: 'TBD'
-        value: 'SAADC_CONFIG_OVERSAMPLE'
+        value: 1
     ADC_0_INTERRUPT_PRIORITY:
         description: 'TBD'
-        value: 'SAADC_CONFIG_IRQ_PRIORITY'
+        value: 'ADC_CONFIG_IRQ_PRIORITY'
 
     UART_0:
         description: 'TBD'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/hw/bsp/nrf51dk/src/os_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/src/os_bsp.c b/hw/bsp/nrf51dk/src/os_bsp.c
index fe1600f..dfdc22c 100644
--- a/hw/bsp/nrf51dk/src/os_bsp.c
+++ b/hw/bsp/nrf51dk/src/os_bsp.c
@@ -18,6 +18,7 @@
  */
 #include <assert.h>
 #include "syscfg/syscfg.h"
+#include "bsp.h"
 #include "hal/flash_map.h"
 #include "hal/hal_bsp.h"
 #include "hal/hal_spi.h"
@@ -124,14 +125,12 @@ bsp_init(void)
       sizeof(bsp_flash_areas) / sizeof(bsp_flash_areas[0]));
 
 #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
 
 #if MYNEWT_VAL(SPI_SLAVE)
-    /*  We initialize one SPI interface as a master. */
-    spi_cfg.csn_pin = SPIS1_CONFIG_CSN_PIN;
+    spi_cfg.csn_pin = SPI_SS_PIN;
     spi_cfg.csn_pullup = NRF_GPIO_PIN_PULLUP;
     rc = hal_spi_init(1, &spi_cfg, HAL_SPI_TYPE_SLAVE);
     assert(rc == 0);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/hw/bsp/nrf52dk/include/bsp/bsp.h
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf52dk/include/bsp/bsp.h b/hw/bsp/nrf52dk/include/bsp/bsp.h
index 85ba3c6..08ebe93 100644
--- a/hw/bsp/nrf52dk/include/bsp/bsp.h
+++ b/hw/bsp/nrf52dk/include/bsp/bsp.h
@@ -6,7 +6,7 @@
  * 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,
@@ -40,6 +40,9 @@ extern uint8_t _ram_start;
 /* LED pins */
 #define LED_BLINK_PIN   (17)
 
+/* SPI SS */
+#define SPI_SS_PIN      (22)
+
 /* UART info */
 #define CONSOLE_UART    "uart0"
 #define CONSOLE_UART_SPEED      115200

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/hw/bsp/nrf52dk/include/bsp/nrf_drv_config.h
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf52dk/include/bsp/nrf_drv_config.h b/hw/bsp/nrf52dk/include/bsp/nrf_drv_config.h
index 173193a..653a4de 100644
--- a/hw/bsp/nrf52dk/include/bsp/nrf_drv_config.h
+++ b/hw/bsp/nrf52dk/include/bsp/nrf_drv_config.h
@@ -208,7 +208,6 @@
 
 #if (SPI0_ENABLED == 1)
 #define SPI0_USE_EASY_DMA 1
-#define SPI0_CONFIG_CSN_PIN         22 /* Note: not defined by SDK */
 #define SPI0_CONFIG_SCK_PIN         23
 #define SPI0_CONFIG_MOSI_PIN        24
 #define SPI0_CONFIG_MISO_PIN        25
@@ -249,7 +248,6 @@
 #define SPIS0_ENABLED 1
 
 #if (SPIS0_ENABLED == 1)
-#define SPIS0_CONFIG_CSN_PIN         22 /* Note: not defined by SDK */
 #define SPIS0_CONFIG_SCK_PIN         23
 #define SPIS0_CONFIG_MOSI_PIN        24
 #define SPIS0_CONFIG_MISO_PIN        25

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/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 80f8bac..fc135ff 100644
--- a/hw/bsp/nrf52dk/src/os_bsp.c
+++ b/hw/bsp/nrf52dk/src/os_bsp.c
@@ -29,6 +29,7 @@
 #include "uart/uart.h"
 #include "uart_hal/uart_hal.h"
 #include "os/os_dev.h"
+#include "bsp.h"
 
 #if MYNEWT_VAL(SPI_MASTER)
 #include "nrf_drv_spi.h"
@@ -158,14 +159,13 @@ bsp_init(void)
     assert(rc == 0);
 
 #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
 
 #if MYNEWT_VAL(SPI_SLAVE)
-    /*  We initialize one SPI interface as a master. */
-    spi_cfg.csn_pin = SPIS0_CONFIG_CSN_PIN;
+    spi_cfg.csn_pin = SPI_SS_PIN;
+    spi_cfg.csn_pullup = NRF_GPIO_PIN_PULLUP;
     rc = hal_spi_init(0, &spi_cfg, HAL_SPI_TYPE_SLAVE);
     assert(rc == 0);
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/hw/hal/include/hal/hal_spi.h
----------------------------------------------------------------------
diff --git a/hw/hal/include/hal/hal_spi.h b/hw/hal/include/hal/hal_spi.h
index 09d0f18..c542123 100644
--- a/hw/hal/include/hal/hal_spi.h
+++ b/hw/hal/include/hal/hal_spi.h
@@ -51,13 +51,10 @@ typedef void (*hal_spi_txrx_cb)(void *arg, int len);
  * can be changed on the fly from the hal
  */
 struct hal_spi_settings {
-    uint8_t         spi_type;
     uint8_t         data_mode;
     uint8_t         data_order;
     uint8_t         word_size;
     uint32_t        baudrate;
-    hal_spi_txrx_cb txrx_cb_func;
-    void            *txrx_cb_arg;
 };
 
 /**
@@ -74,7 +71,11 @@ struct hal_spi_settings {
 int hal_spi_init(int spi_num, void *cfg, uint8_t spi_type);
 
 /**
- * Configure the spi.
+ * Configure the spi. Must be called after the spi is initialized (after
+ * hal_spi_init is called) and when the spi is disabled (user must call
+ * hal_spi_disable if the spi has been enabled through hal_spi_enable prior
+ * to calling this function). Can also be used to reconfigure an initialized
+ * SPI (assuming it is disabled as described previously).
  *
  * @param spi_num The number of the SPI to configure.
  * @param psettings The settings to configure this SPI with
@@ -85,10 +86,12 @@ int hal_spi_config(int spi_num, struct hal_spi_settings *psettings);
 
 /**
  * Sets the txrx callback (executed at interrupt context) when the
- * buffer is transferred by the master or the slave using the hal_spi_rxtx API.
- * Allowed to be called when the spi is enabled but not when a transfer is in
- * progress. This callback will also be called when chip select is de-asserted
- * on the slave.
+ * buffer is transferred by the master or the slave using the non-blocking API.
+ * Cannot be called when the spi is enabled. This callback will also be called
+ * when chip select is de-asserted on the slave.
+ *
+ * NOTE: This callback is only used for the non-blocking interface and must
+ * be called prior to using the non-blocking API.
  *
  * @param spi_num   SPI interface on which to set callback
  * @param txrx      Callback function
@@ -121,12 +124,12 @@ int hal_spi_disable(int spi_num);
 
 /**
  * Blocking call to send a value on the SPI. Returns the value received from the
- * SPI.
+ * SPI slave.
  *
  * MASTER: Sends the value and returns the received value from the slave.
  * SLAVE: Invalid API. Returns 0xFFFF
  *
- * @param spi_num   Spin interface to use
+ * @param spi_num   Spi interface to use
  * @param val       Value to send
  *
  * @return uint16_t Value received on SPI interface from slave. Returns 0xFFFF
@@ -135,32 +138,66 @@ int hal_spi_disable(int spi_num);
 uint16_t hal_spi_tx_val(int spi_num, uint16_t val);
 
 /**
- * Sends a buffer and also stores the received values. This is always a
- * non-blocking call for a spi slave. It is a blocking call if the callback is
- * NULL; non-blocking if callback is set. For both the master and slave, the
- * txrx callback is executed at interrupt context when the buffer is sent.
+ * Blocking interface to send a buffer and store the received values from the
+ * slave. The transmit and receive buffers are either arrays of 8-bit (uint8_t)
+ * values or 16-bit values depending on whether the spi is configured for 8 bit
+ * data or more than 8 bits per value. The 'cnt' parameter is the number of
+ * 8-bit or 16-bit values. Thus, if 'cnt' is 10, txbuf/rxbuf would point to an
+ * array of size 10 (in bytes) if the SPI is using 8-bit data; otherwise
+ * txbuf/rxbuf would point to an array of size 20 bytes (ten, uint16_t values).
+ *
+ * NOTE: these buffers are in the native endian-ness of the platform.
+ *
+ *     MASTER: master sends all the values in the buffer and stores the
+ *             stores the values in the receive buffer if rxbuf is not NULL.
+ *             The txbuf parameter cannot be NULL.
+ *     SLAVE: cannot be called for a slave; returns -1
+ *
+ * @param spi_num   SPI interface to use
+ * @param txbuf     Pointer to buffer where values to transmit are stored.
+ * @param rxbuf     Pointer to buffer to store values received from peer.
+ * @param cnt       Number of 8-bit or 16-bit values to be transferred.
+ *
+ * @return int 0 on success, non-zero error code on failure.
+ */
+int hal_spi_txrx(int spi_num, void *txbuf, void *rxbuf, int cnt);
+
+/**
+ * Non-blocking interface to send a buffer and store received values. Can be
+ * used for both master and slave SPI types. The user must configure the
+ * callback (using hal_spi_set_txrx_cb); the txrx callback is executed at
+ * interrupt context when the buffer is sent.
+ *
+ * The transmit and receive buffers are either arrays of 8-bit (uint8_t)
+ * values or 16-bit values depending on whether the spi is configured for 8 bit
+ * data or more than 8 bits per value. The 'cnt' parameter is the number of
+ * 8-bit or 16-bit values. Thus, if 'cnt' is 10, txbuf/rxbuf would point to an
+ * array of size 10 (in bytes) if the SPI is using 8-bit data; otherwise
+ * txbuf/rxbuf would point to an array of size 20 bytes (ten, uint16_t values).
+ *
+ * NOTE: these buffers are in the native endian-ness of the platform.
+ *
  *     MASTER: master sends all the values in the buffer and stores the
  *             stores the values in the receive buffer if rxbuf is not NULL.
  *             The txbuf parameter cannot be NULL
- *     SLAVE: Slave preloads the data to be sent to the master (values
- *            stored in txbuf) and places received data from master in rxbuf (if
- *            not NULL). The txrx callback occurs when len values are
+ *     SLAVE: Slave "preloads" the data to be sent to the master (values
+ *            stored in txbuf) and places received data from master in rxbuf
+ *            (if not NULL). The txrx callback occurs when len values are
  *            transferred or master de-asserts chip select. If txbuf is NULL,
  *            the slave transfers its default byte. Both rxbuf and txbuf cannot
  *            be NULL.
  *
  * @param spi_num   SPI interface to use
  * @param txbuf     Pointer to buffer where values to transmit are stored.
- * @param rxbuf     Pointer to buffer to store values received from peer. Can
- *                  be NULL.
- * @param len       Number of values to be transferred.
+ * @param rxbuf     Pointer to buffer to store values received from peer.
+ * @param cnt       Number of 8-bit or 16-bit values to be transferred.
  *
  * @return int 0 on success, non-zero error code on failure.
  */
-int hal_spi_txrx(int spi_num, void *txbuf, void *rxbuf, int len);
+int hal_spi_txrx_noblock(int spi_num, void *txbuf, void *rxbuf, int cnt);
 
 /**
- * Sets the default value transferred by the slave.
+ * Sets the default value transferred by the slave. Not valid for master
  *
  * @param spi_num SPI interface to use
  *

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/hw/mcu/nordic/nrf51xxx/src/hal_spi.c
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf51xxx/src/hal_spi.c b/hw/mcu/nordic/nrf51xxx/src/hal_spi.c
index 010bd8b..cfa4a6d 100644
--- a/hw/mcu/nordic/nrf51xxx/src/hal_spi.c
+++ b/hw/mcu/nordic/nrf51xxx/src/hal_spi.c
@@ -58,6 +58,7 @@
 #define HAL_SPI_SLAVE_STATE_READY       (2)
 
 struct nrf51_hal_spi {
+    uint8_t spi_type;
     uint8_t spi_xfr_flag;   /* Master only */
     uint8_t slave_state;    /* Slave only */
     uint16_t nhs_buflen;
@@ -69,8 +70,13 @@ struct nrf51_hal_spi {
         nrf_drv_spis_t  spis;
     } nhs_spi;
 
+    /* Pointers to tx/rx buffers */
     uint8_t *nhs_txbuf;
     uint8_t *nhs_rxbuf;
+
+    /* Callback and arguments */
+    hal_spi_txrx_cb txrx_cb_func;
+    void            *txrx_cb_arg;
 };
 
 #if SPI0_ENABLED
@@ -133,11 +139,11 @@ nrf51_irqm_handler(struct nrf51_hal_spi *spi)
         }
         ++spi->nhs_rxd_bytes;
         if (spi->nhs_rxd_bytes == spi->nhs_buflen) {
-            if (spi->spi_cfg.txrx_cb_func) {
-                spi->spi_cfg.txrx_cb_func(spi->spi_cfg.txrx_cb_arg,
-                                          spi->nhs_buflen);
+            if (spi->txrx_cb_func) {
+                spi->txrx_cb_func(spi->txrx_cb_arg, spi->nhs_buflen);
             }
             spi->spi_xfr_flag = 0;
+            nrf_spi_int_disable(p_spi, NRF_SPI_INT_READY_MASK);
         }
         if (spi->nhs_txd_bytes != spi->nhs_buflen) {
             nrf_spi_txd_set(p_spi, spi->nhs_txbuf[spi->nhs_txd_bytes]);
@@ -181,14 +187,14 @@ nrf51_irqs_handler(struct nrf51_hal_spi *spi)
     if (nrf_spis_event_check(p_spis, NRF_SPIS_EVENT_END)) {
         nrf_spis_event_clear(p_spis, NRF_SPIS_EVENT_END);
         if (spi->slave_state == HAL_SPI_SLAVE_STATE_READY) {
-            if (spi->spi_cfg.txrx_cb_func) {
+            if (spi->txrx_cb_func) {
                 /* Get transfer length */
                 if (spi->nhs_txbuf == NULL) {
                     xfr_len = nrf_spis_rx_amount_get(p_spis);
                 } else {
                     xfr_len = nrf_spis_tx_amount_get(p_spis);
                 }
-                spi->spi_cfg.txrx_cb_func(spi->spi_cfg.txrx_cb_arg, xfr_len);
+                spi->txrx_cb_func(spi->txrx_cb_arg, xfr_len);
             }
             spi->slave_state = HAL_SPI_SLAVE_STATE_IDLE;
         }
@@ -202,7 +208,7 @@ nrf51_irqs_handler(struct nrf51_hal_spi *spi)
 void
 nrf51_spi0_irq_handler(void)
 {
-    if (nrf51_hal_spi0.spi_cfg.spi_type == HAL_SPI_TYPE_MASTER) {
+    if (nrf51_hal_spi0.spi_type == HAL_SPI_TYPE_MASTER) {
         nrf51_irqm_handler(&nrf51_hal_spi0);
     } else {
         assert(0);
@@ -214,7 +220,7 @@ nrf51_spi0_irq_handler(void)
 void
 nrf51_spi1_irq_handler(void)
 {
-    if (nrf51_hal_spi1.spi_cfg.spi_type == HAL_SPI_TYPE_MASTER) {
+    if (nrf51_hal_spi1.spi_type == HAL_SPI_TYPE_MASTER) {
 #if SPI1_ENABLED
         nrf51_irqm_handler(&nrf51_hal_spi1);
 #else
@@ -326,6 +332,7 @@ hal_spi_config_slave(struct nrf51_hal_spi *spi,
     nrf_spis_bit_order_t bit_order;
 
     p_spis = spi->nhs_spi.spis.p_reg;
+    memcpy(&spi->spi_cfg, settings, sizeof(*settings));
 
     spi_mode = 0;
     switch (settings->data_mode) {
@@ -522,14 +529,15 @@ hal_spi_master_send_first(NRF_SPI_Type *p_spi, uint8_t txval)
 }
 
 /**
- * Initialize the SPI interface
+ * Initialize the SPI, given by spi_num.
  *
+ * @param spi_num The number of the SPI to initialize
+ * @param cfg HW/MCU specific configuration,
+ *            passed to the underlying implementation, providing extra
+ *            configuration.
+ * @param spi_type SPI type (master or slave)
  *
- * @param spi_num
- * @param cfg
- * @param spi_type
- *
- * @return int
+ * @return int 0 on success, non-zero error code on failure.
  */
 int
 hal_spi_init(int spi_num, void *cfg, uint8_t spi_type)
@@ -551,7 +559,7 @@ hal_spi_init(int spi_num, void *cfg, uint8_t spi_type)
     }
 
     irq_handler = NULL;
-    spi->spi_cfg.spi_type  = spi_type;
+    spi->spi_type  = spi_type;
     if (spi_num == 0) {
 #if SPI0_ENABLED
         irq_handler = nrf51_spi0_irq_handler;
@@ -602,6 +610,18 @@ err:
     return (rc);
 }
 
+/**
+ * Configure the spi. Must be called after the spi is initialized (after
+ * hal_spi_init is called) and when the spi is disabled (user must call
+ * hal_spi_disable if the spi has been enabled through hal_spi_enable prior
+ * to calling this function). Can also be used to reconfigure an initialized
+ * SPI (assuming it is disabled as described previously).
+ *
+ * @param spi_num The number of the SPI to configure.
+ * @param psettings The settings to configure this SPI with
+ *
+ * @return int 0 on success, non-zero error code on failure.
+ */
 int
 hal_spi_config(int spi_num, struct hal_spi_settings *settings)
 {
@@ -610,10 +630,7 @@ hal_spi_config(int spi_num, struct hal_spi_settings *settings)
 
     NRF51_HAL_SPI_RESOLVE(spi_num, spi);
 
-    spi->spi_cfg.txrx_cb_func = settings->txrx_cb_func;
-    spi->spi_cfg.txrx_cb_arg = settings->txrx_cb_arg;
-
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
         rc = hal_spi_config_master(spi, settings);
     } else {
         rc = hal_spi_config_slave(spi, settings);
@@ -642,16 +659,11 @@ hal_spi_enable(int spi_num)
 
     NRF51_HAL_SPI_RESOLVE(spi_num, spi);
 
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
         p_spi = (NRF_SPI_Type *)spi->nhs_spi.spim.p_registers;
-        /* We need to enable this in blocking or non-blocking mode */
-        if (spi->spi_cfg.txrx_cb_func) {
-            nrf_spi_event_clear(p_spi, NRF_SPI_INT_READY_MASK);
-            nrf_spi_int_enable(p_spi, NRF_SPI_INT_READY_MASK);
-        }
         nrf_spi_enable(p_spi);
     } else {
-        if (spi->spi_cfg.txrx_cb_func == NULL) {
+        if (spi->txrx_cb_func == NULL) {
             rc = EINVAL;
             goto err;
         }
@@ -686,7 +698,7 @@ hal_spi_disable(int spi_num)
 
     NRF51_HAL_SPI_RESOLVE(spi_num, spi);
 
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
         p_spi = (NRF_SPI_Type *)spi->nhs_spi.spim.p_registers;
         nrf_spi_int_disable(p_spi, NRF_SPI_INT_READY_MASK);
         spi->spi_xfr_flag = 0;
@@ -709,20 +721,19 @@ err:
 
 /**
  * Blocking call to send a value on the SPI. Returns the value received from the
- * SPI.
+ * SPI slave.
  *
  * MASTER: Sends the value and returns the received value from the slave.
- * SLAVE: Sets the value to send to the master when the master transfers a
- *        value. This value will be sent until another call to hal_spi_tx_val()
- *        is made. The return code is ignored for a slave.\ufffd
+ * SLAVE: Invalid API. Returns 0xFFFF
  *
- * @param spi_num
- * @param val
+ * @param spi_num   Spi interface to use
+ * @param val       Value to send
  *
  * @return uint16_t Value received on SPI interface from slave. Returns 0xFFFF
- * if called when SPI configured as a Slave.
+ * if called when the SPI is configured to be a slave
  */
-uint16_t hal_spi_tx_val(int spi_num, uint16_t val)
+uint16_t
+hal_spi_tx_val(int spi_num, uint16_t val)
 {
     int rc;
     uint16_t retval;
@@ -731,7 +742,7 @@ uint16_t hal_spi_tx_val(int spi_num, uint16_t val)
 
     NRF51_HAL_SPI_RESOLVE(spi_num, spi);
 
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
         p_spi = (NRF_SPI_Type *) spi->nhs_spi.spim.p_registers;
         nrf_spi_event_clear(p_spi, NRF_SPI_EVENT_READY);
         nrf_spi_txd_set(p_spi, (uint8_t) val);
@@ -750,20 +761,18 @@ err:
 
 /**
  * Sets the txrx callback (executed at interrupt context) when the
- * buffer is transferred by the master or the slave using the hal_spi_rxtx API.
- * This callback is also called when the SPI is a slave and chip select is
- * de-asserted and there is data available in the receive buffer.
- *
- * If the callback is NULL, the SPI will be in blocking mode; otherwise it is
- * in non-blocking mode.
+ * buffer is transferred by the master or the slave using the non-blocking API.
+ * Cannot be called when the spi is enabled. This callback will also be called
+ * when chip select is de-asserted on the slave.
  *
- * Cannot be called when the SPI is enabled.
+ * NOTE: This callback is only used for the non-blocking interface and must
+ * be called prior to using the non-blocking API.
  *
  * @param spi_num   SPI interface on which to set callback
- * @param txrx_cb   Pointer to callback function. NULL to set into blocking mode
- * @param arg       Argument passed to callback function.
+ * @param txrx      Callback function
+ * @param arg       Argument to be passed to callback function
  *
- * @return int 0 on success, -1 if spi is already enabled.
+ * @return int 0 on success, non-zero error code on failure.
  */
 int
 hal_spi_set_txrx_cb(int spi_num, hal_spi_txrx_cb txrx_cb, void *arg)
@@ -782,8 +791,8 @@ hal_spi_set_txrx_cb(int spi_num, hal_spi_txrx_cb txrx_cb, void *arg)
     if (p_spi->ENABLE != 0) {
         rc = -1;
     } else {
-        spi->spi_cfg.txrx_cb_func = txrx_cb;
-        spi->spi_cfg.txrx_cb_arg = arg;
+        spi->txrx_cb_func = txrx_cb;
+        spi->txrx_cb_arg = arg;
         rc = 0;
     }
 
@@ -792,24 +801,25 @@ err:
 }
 
 /**
- * Send a buffer and also store the received values. This call can be either
- * blocking or non-blocking for the master; it is always non-blocking for slave.
- * In non-blocking mode, the txrx callback is executed at interrupt context when
- * the buffer is sent.
+ * Blocking interface to send a buffer and store the received values from the
+ * slave. The transmit and receive buffers are either arrays of 8-bit (uint8_t)
+ * values or 16-bit values depending on whether the spi is configured for 8 bit
+ * data or more than 8 bits per value. The 'cnt' parameter is the number of
+ * 8-bit or 16-bit values. Thus, if 'cnt' is 10, txbuf/rxbuf would point to an
+ * array of size 10 (in bytes) if the SPI is using 8-bit data; otherwise
+ * txbuf/rxbuf would point to an array of size 20 bytes (ten, uint16_t values).
+ *
+ * NOTE: these buffers are in the native endian-ness of the platform.
+ *
  *     MASTER: master sends all the values in the buffer and stores the
  *             stores the values in the receive buffer if rxbuf is not NULL.
  *             The txbuf parameter cannot be NULL.
- *     SLAVE: Slave preloads the data to be sent to the master (values
- *            stored in txbuf) and places received data from master in rxbuf (if
- *            not NULL).  The txrx callback when len values are transferred or
- *            master de-asserts chip select. If txbuf is NULL, the slave
- *            transfers its default byte. Both rxbuf and txbuf cannot be NULL.
+ *     SLAVE: cannot be called for a slave; returns -1
  *
  * @param spi_num   SPI interface to use
  * @param txbuf     Pointer to buffer where values to transmit are stored.
- * @param rxbuf     Pointer to buffer to store values received from peer. Can
- *                  be NULL.
- * @param len       Number of values to be transferred.
+ * @param rxbuf     Pointer to buffer to store values received from peer.
+ * @param cnt       Number of 8-bit or 16-bit values to be transferred.
  *
  * @return int 0 on success, non-zero error code on failure.
  */
@@ -825,66 +835,133 @@ hal_spi_txrx(int spi_num, void *txbuf, void *rxbuf, int len)
     struct nrf51_hal_spi *spi;
 
     rc = EINVAL;
-    if (!len || (len > 255)) {
+    if (!len) {
         goto err;
     }
 
     NRF51_HAL_SPI_RESOLVE(spi_num, spi);
 
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
+        /* Must have a txbuf for master! */
+        if (txbuf == NULL) {
+            goto err;
+        }
+
         p_spi = (NRF_SPI_Type *) spi->nhs_spi.spim.p_registers;
-        if (spi->spi_cfg.txrx_cb_func) {
-            /* Must have a txbuf for master! */
-            if (txbuf == NULL) {
-                goto err;
-            }
+        nrf_spi_int_disable(p_spi, NRF_SPI_INT_READY_MASK);
 
-            /* Not allowed if transfer in progress */
-            if (spi->spi_xfr_flag) {
-                rc = -1;
-                goto err;
+        /* Blocking spi transfer */
+        txd = (uint8_t *)txbuf;
+        hal_spi_master_send_first(p_spi, txd[0]);
+        txcnt = len - 1;
+        rxd = (uint8_t *)rxbuf;
+        for (i = 0; i < len; ++i) {
+            if (txcnt) {
+                ++txd;
+                nrf_spi_txd_set(p_spi, *txd);
+                --txcnt;
             }
-            spi->spi_xfr_flag = 1;
-
-            spi->nhs_buflen = (uint16_t)len;
-            spi->nhs_txbuf = txbuf;
-            spi->nhs_rxbuf = rxbuf;
-            spi->nhs_rxd_bytes = 0;
-            txd = (uint8_t *)txbuf;
-            hal_spi_master_send_first(p_spi, txd[0]);
-            spi->nhs_txd_bytes = 1;
-            if (len > 1) {
-                nrf_spi_txd_set(p_spi, txd[1]);
-                ++spi->nhs_txd_bytes;
-            }
-            nrf_spi_int_enable(p_spi, NRF_SPI_INT_READY_MASK);
-        } else {
-            /* Blocking spi transfer */
-            txd = (uint8_t *)txbuf;
-            hal_spi_master_send_first(p_spi, txd[0]);
-            txcnt = len - 1;
-            rxd = (uint8_t *)rxbuf;
-            for (i = 0; i < len; ++i) {
-                if (txcnt) {
-                    ++txd;
-                    nrf_spi_txd_set(p_spi, *txd);
-                    --txcnt;
-                }
-                while (!nrf_spi_event_check(p_spi, NRF_SPI_EVENT_READY)) {}
-                nrf_spi_event_clear(p_spi, NRF_SPI_EVENT_READY);
-                rxval = nrf_spi_rxd_get(p_spi);
-                if (rxbuf) {
-                    *rxd = rxval;
-                    ++rxd;
-                }
+            while (!nrf_spi_event_check(p_spi, NRF_SPI_EVENT_READY)) {}
+            nrf_spi_event_clear(p_spi, NRF_SPI_EVENT_READY);
+            rxval = nrf_spi_rxd_get(p_spi);
+            if (rxbuf) {
+                *rxd = rxval;
+                ++rxd;
             }
         }
+        return 0;
+    }
+
+err:
+    return rc;
+}
+
+/**
+ * Non-blocking interface to send a buffer and store received values. Can be
+ * used for both master and slave SPI types. The user must configure the
+ * callback (using hal_spi_set_txrx_cb); the txrx callback is executed at
+ * interrupt context when the buffer is sent.
+ *
+ * The transmit and receive buffers are either arrays of 8-bit (uint8_t)
+ * values or 16-bit values depending on whether the spi is configured for 8 bit
+ * data or more than 8 bits per value. The 'cnt' parameter is the number of
+ * 8-bit or 16-bit values. Thus, if 'cnt' is 10, txbuf/rxbuf would point to an
+ * array of size 10 (in bytes) if the SPI is using 8-bit data; otherwise
+ * txbuf/rxbuf would point to an array of size 20 bytes (ten, uint16_t values).
+ *
+ * NOTE: these buffers are in the native endian-ness of the platform.
+ *
+ *     MASTER: master sends all the values in the buffer and stores the
+ *             stores the values in the receive buffer if rxbuf is not NULL.
+ *             The txbuf parameter cannot be NULL
+ *     SLAVE: Slave "preloads" the data to be sent to the master (values
+ *            stored in txbuf) and places received data from master in rxbuf
+ *            (if not NULL). The txrx callback occurs when len values are
+ *            transferred or master de-asserts chip select. If txbuf is NULL,
+ *            the slave transfers its default byte. Both rxbuf and txbuf cannot
+ *            be NULL.
+ *
+ * @param spi_num   SPI interface to use
+ * @param txbuf     Pointer to buffer where values to transmit are stored.
+ * @param rxbuf     Pointer to buffer to store values received from peer.
+ * @param cnt       Number of 8-bit or 16-bit values to be transferred.
+ *
+ * @return int 0 on success, non-zero error code on failure.
+ */
+int
+hal_spi_txrx_noblock(int spi_num, void *txbuf, void *rxbuf, int len)
+{
+    int rc;
+    uint8_t *txd;
+    NRF_SPI_Type *p_spi;
+    struct nrf51_hal_spi *spi;
+
+    rc = EINVAL;
+    NRF51_HAL_SPI_RESOLVE(spi_num, spi);
+
+    if ((spi->txrx_cb_func == NULL) || (len == 0)) {
+        goto err;
+    }
+
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
+        /* Must have a txbuf for master! */
+        if (txbuf == NULL) {
+            goto err;
+        }
+
+        /* Not allowed if transfer in progress */
+        if (spi->spi_xfr_flag) {
+            rc = -1;
+            goto err;
+        }
+
+        p_spi = (NRF_SPI_Type *) spi->nhs_spi.spim.p_registers;
+        nrf_spi_int_disable(p_spi, NRF_SPI_INT_READY_MASK);
+        spi->spi_xfr_flag = 1;
+
+        spi->nhs_buflen = (uint16_t)len;
+        spi->nhs_txbuf = txbuf;
+        spi->nhs_rxbuf = rxbuf;
+        spi->nhs_rxd_bytes = 0;
+        txd = (uint8_t *)txbuf;
+        hal_spi_master_send_first(p_spi, txd[0]);
+        spi->nhs_txd_bytes = 1;
+        if (len > 1) {
+            nrf_spi_txd_set(p_spi, txd[1]);
+            ++spi->nhs_txd_bytes;
+        }
+        nrf_spi_int_enable(p_spi, NRF_SPI_INT_READY_MASK);
     } else {
-        /* Must have txbuf and rxbuf */
+        /* Must have txbuf or rxbuf */
         if ((txbuf == NULL) && (rxbuf == NULL)) {
             goto err;
         }
 
+        /* XXX: not sure what to for length > 255 */
+        if (len > 255) {
+            goto err;
+        }
+
         /*
          * Ready the slave for a transfer. Do not allow this to be called
          * if the slave has already been readied or is requesting the
@@ -908,7 +985,7 @@ err:
 }
 
 /**
- * Sets the default value transferred by the slave.
+ * Sets the default value transferred by the slave. Not valid for master
  *
  * @param spi_num SPI interface to use
  *
@@ -922,9 +999,10 @@ hal_spi_slave_set_def_tx_val(int spi_num, uint16_t val)
     struct nrf51_hal_spi *spi;
 
     NRF51_HAL_SPI_RESOLVE(spi_num, spi);
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_SLAVE) {
+    if (spi->spi_type  == HAL_SPI_TYPE_SLAVE) {
         p_spis = spi->nhs_spi.spis.p_reg;
         nrf_spis_def_set(p_spis, (uint8_t) val);
+        nrf_spis_orc_set(p_spis, (uint8_t) val);
         rc = 0;
     } else {
         rc = EINVAL;
@@ -935,8 +1013,7 @@ err:
 }
 
 /**
- * This aborts the current transfer but keeps the spi enabled. Should only
- * be used when the SPI is in non-blocking mode.
+ * This aborts the current transfer but keeps the spi enabled.
  *
  * @param spi_num   SPI interface on which transfer should be aborted.
  *
@@ -954,11 +1031,7 @@ hal_spi_abort(int spi_num)
     NRF51_HAL_SPI_RESOLVE(spi_num, spi);
 
     rc = 0;
-    if (spi->spi_cfg.txrx_cb_func == NULL) {
-        goto err;
-    }
-
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
         p_spi = (NRF_SPI_Type *)spi->nhs_spi.spim.p_registers;
         if (spi->spi_xfr_flag) {
             nrf_spi_int_disable(p_spi, NRF_SPI_INT_READY_MASK);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/26dfea0f/hw/mcu/nordic/nrf52xxx/src/hal_spi.c
----------------------------------------------------------------------
diff --git a/hw/mcu/nordic/nrf52xxx/src/hal_spi.c b/hw/mcu/nordic/nrf52xxx/src/hal_spi.c
index 9b0e5c2..f2b0b1d 100644
--- a/hw/mcu/nordic/nrf52xxx/src/hal_spi.c
+++ b/hw/mcu/nordic/nrf52xxx/src/hal_spi.c
@@ -32,6 +32,10 @@
 #include <nrf_drv_common.h>
 #include <app_util_platform.h>
 
+#ifndef min
+#define min(a, b) ((a)<(b)?(a):(b))
+#endif
+
 /* XXX:
  * 1) what about stats?
  * 2) Dealing with errors (OVERFLOW, OVERREAD)
@@ -58,19 +62,25 @@
 #define HAL_SPI_SLAVE_STATE_READY       (2)
 
 struct nrf52_hal_spi {
+    uint8_t spi_type;
     uint8_t spi_xfr_flag;   /* Master only */
     uint8_t dummy_rx;       /* Master only */
     uint8_t slave_state;    /* Slave only */
-    uint8_t slave_buflen;   /* Slave only */
+    uint16_t nhs_buflen;
+    uint16_t nhs_bytes_txd;
     struct hal_spi_settings spi_cfg; /* Slave and master */
     union {
         nrf_drv_spi_t   spim;
         nrf_drv_spis_t  spis;
     } nhs_spi;
 
-    /* These are slave only */
-    uint8_t *slave_txbuf;
-    uint8_t *slave_rxbuf;
+    /* Pointers to tx/rx buffers */
+    uint8_t *nhs_txbuf;
+    uint8_t *nhs_rxbuf;
+
+    /* Callback and arguments */
+    hal_spi_txrx_cb txrx_cb_func;
+    void            *txrx_cb_arg;
 };
 
 #if SPI0_ENABLED || SPIS0_ENABLED
@@ -121,6 +131,8 @@ nrf_drv_spis_t inst_spi1_s = NRF_DRV_SPIS_INSTANCE(1);
 static void
 nrf52_irqm_handler(struct nrf52_hal_spi *spi)
 {
+    uint8_t xfr_bytes;
+    uint16_t len;
     NRF_SPIM_Type *p_spim;
 
     p_spim = spi->nhs_spi.spim.p_registers;
@@ -132,11 +144,29 @@ nrf52_irqm_handler(struct nrf52_hal_spi *spi)
             return;
         }
 
-        if (spi->spi_cfg.txrx_cb_func) {
-            spi->spi_cfg.txrx_cb_func(spi->spi_cfg.txrx_cb_arg,
-                                      p_spim->TXD.AMOUNT);
+        /* Are there more bytes to send? */
+        xfr_bytes = p_spim->TXD.AMOUNT;
+        spi->nhs_bytes_txd += xfr_bytes;
+        if (spi->nhs_bytes_txd < spi->nhs_buflen) {
+            spi->nhs_txbuf += xfr_bytes;
+            len = spi->nhs_buflen - spi->nhs_bytes_txd;
+            len = min(255, len);
+            nrf_spim_tx_buffer_set(p_spim, spi->nhs_txbuf, (uint8_t)len);
+
+            /* If no rxbuf, we need to set rxbuf and maxcnt to 1 */
+            if (spi->nhs_rxbuf) {
+                spi->nhs_rxbuf += xfr_bytes;
+                nrf_spim_rx_buffer_set(p_spim, spi->nhs_rxbuf, (uint8_t)len);
+            }
+            nrf_spim_task_trigger(p_spim, NRF_SPIM_TASK_START);
+        } else {
+            if (spi->txrx_cb_func) {
+                spi->txrx_cb_func(spi->txrx_cb_arg, spi->nhs_buflen);
+
+            }
+            spi->spi_xfr_flag = 0;
+            nrf_spim_int_disable(p_spim, NRF_SPIM_INT_END_MASK);
         }
-        spi->spi_xfr_flag = 0;
     }
 }
 #endif
@@ -155,16 +185,16 @@ nrf52_irqs_handler(struct nrf52_hal_spi *spi)
         nrf_spis_event_clear(p_spis, NRF_SPIS_EVENT_ACQUIRED);
 
         if (spi->slave_state == HAL_SPI_SLAVE_STATE_ACQ_SEM) {
-            if (spi->slave_txbuf == NULL) {
+            if (spi->nhs_txbuf == NULL) {
                 nrf_spis_tx_buffer_set(p_spis, 0, 0);
             } else {
-                nrf_spis_tx_buffer_set(p_spis, spi->slave_txbuf, spi->slave_buflen);
+                nrf_spis_tx_buffer_set(p_spis, spi->nhs_txbuf, spi->nhs_buflen);
             }
 
-            if (spi->slave_rxbuf == NULL) {
+            if (spi->nhs_rxbuf == NULL) {
                 nrf_spis_rx_buffer_set(p_spis, 0, 0);
             } else {
-                nrf_spis_rx_buffer_set(p_spis, spi->slave_rxbuf, spi->slave_buflen);
+                nrf_spis_rx_buffer_set(p_spis, spi->nhs_rxbuf, spi->nhs_buflen);
             }
             nrf_spis_task_trigger(p_spis, NRF_SPIS_TASK_RELEASE);
             spi->slave_state = HAL_SPI_SLAVE_STATE_READY;
@@ -175,14 +205,14 @@ nrf52_irqs_handler(struct nrf52_hal_spi *spi)
     if (nrf_spis_event_check(p_spis, NRF_SPIS_EVENT_END)) {
         nrf_spis_event_clear(p_spis, NRF_SPIS_EVENT_END);
         if (spi->slave_state == HAL_SPI_SLAVE_STATE_READY) {
-            if (spi->spi_cfg.txrx_cb_func) {
+            if (spi->txrx_cb_func) {
                 /* Get transfer length */
-                if (spi->slave_txbuf == NULL) {
+                if (spi->nhs_txbuf == NULL) {
                     xfr_len = nrf_spis_rx_amount_get(p_spis);
                 } else {
                     xfr_len = nrf_spis_tx_amount_get(p_spis);
                 }
-                spi->spi_cfg.txrx_cb_func(spi->spi_cfg.txrx_cb_arg, xfr_len);
+                spi->txrx_cb_func(spi->txrx_cb_arg, xfr_len);
             }
             spi->slave_state = HAL_SPI_SLAVE_STATE_IDLE;
         }
@@ -196,7 +226,7 @@ nrf52_irqs_handler(struct nrf52_hal_spi *spi)
 void
 nrf52_spi0_irq_handler(void)
 {
-    if (nrf52_hal_spi0.spi_cfg.spi_type == HAL_SPI_TYPE_MASTER) {
+    if (nrf52_hal_spi0.spi_type == HAL_SPI_TYPE_MASTER) {
         nrf52_irqm_handler(&nrf52_hal_spi0);
     } else {
         nrf52_irqs_handler(&nrf52_hal_spi0);
@@ -208,7 +238,7 @@ nrf52_spi0_irq_handler(void)
 void
 nrf52_spi1_irq_handler(void)
 {
-    if (nrf52_hal_spi1.spi_cfg.spi_type == HAL_SPI_TYPE_MASTER) {
+    if (nrf52_hal_spi1.spi_type == HAL_SPI_TYPE_MASTER) {
         nrf52_irqm_handler(&nrf52_hal_spi1);
     } else {
         nrf52_irqs_handler(&nrf52_hal_spi1);
@@ -508,14 +538,15 @@ hal_spi_init_slave(nrf_drv_spis_t *p_instance,
 }
 
 /**
- * Initialize the SPI interface
+ * Initialize the SPI, given by spi_num.
  *
+ * @param spi_num The number of the SPI to initialize
+ * @param cfg HW/MCU specific configuration,
+ *            passed to the underlying implementation, providing extra
+ *            configuration.
+ * @param spi_type SPI type (master or slave)
  *
- * @param spi_num
- * @param cfg
- * @param spi_type
- *
- * @return int
+ * @return int 0 on success, non-zero error code on failure.
  */
 int
 hal_spi_init(int spi_num, void *cfg, uint8_t spi_type)
@@ -537,7 +568,7 @@ hal_spi_init(int spi_num, void *cfg, uint8_t spi_type)
     }
 
     irq_handler = NULL;
-    spi->spi_cfg.spi_type  = spi_type;
+    spi->spi_type  = spi_type;
     if (spi_num == 0) {
 #if SPI0_ENABLED || SPIS0_ENABLED
         irq_handler = nrf52_spi0_irq_handler;
@@ -596,18 +627,37 @@ err:
     return (rc);
 }
 
+/**
+ * Configure the spi. Must be called after the spi is initialized (after
+ * hal_spi_init is called) and when the spi is disabled (user must call
+ * hal_spi_disable if the spi has been enabled through hal_spi_enable prior
+ * to calling this function). Can also be used to reconfigure an initialized
+ * SPI (assuming it is disabled as described previously).
+ *
+ * @param spi_num The number of the SPI to configure.
+ * @param psettings The settings to configure this SPI with
+ *
+ * @return int 0 on success, non-zero error code on failure.
+ */
 int
 hal_spi_config(int spi_num, struct hal_spi_settings *settings)
 {
     int rc;
     struct nrf52_hal_spi *spi;
+    NRF_SPIM_Type *p_spim;
 
     NRF52_HAL_SPI_RESOLVE(spi_num, spi);
 
-    spi->spi_cfg.txrx_cb_func = settings->txrx_cb_func;
-    spi->spi_cfg.txrx_cb_arg = settings->txrx_cb_arg;
+    /*
+     * This looks odd, but the ENABLE register is in the same location for
+     * SPIM, SPI and SPIS
+     */
+    p_spim = spi->nhs_spi.spim.p_registers;
+    if (p_spim->ENABLE != 0) {
+        return -1;
+    }
 
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
         rc = hal_spi_config_master(spi, settings);
     } else {
         rc = hal_spi_config_slave(spi, settings);
@@ -636,18 +686,12 @@ hal_spi_enable(int spi_num)
 
     NRF52_HAL_SPI_RESOLVE(spi_num, spi);
 
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
+        /* For now, enable this in normal SPI mode (not spim) */
         p_spim = spi->nhs_spi.spim.p_registers;
-        /* We need to enable this in blocking or non-blocking mode */
-        if (spi->spi_cfg.txrx_cb_func) {
-            nrf_spim_event_clear(p_spim, NRF_SPIM_INT_END_MASK);
-            nrf_spim_int_enable(p_spim, NRF_SPIM_INT_END_MASK);
-            nrf_spim_enable(p_spim);
-        } else {
-            nrf_spi_enable((NRF_SPI_Type *)p_spim);
-        }
+        nrf_spi_enable((NRF_SPI_Type *)p_spim);
     } else {
-        if (spi->spi_cfg.txrx_cb_func == NULL) {
+        if (spi->txrx_cb_func == NULL) {
             rc = EINVAL;
             goto err;
         }
@@ -682,7 +726,7 @@ hal_spi_disable(int spi_num)
 
     NRF52_HAL_SPI_RESOLVE(spi_num, spi);
 
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
         p_spim = spi->nhs_spi.spim.p_registers;
         nrf_spim_int_disable(p_spim, NRF_SPI_IRQ_DISABLE_ALL);
         if (spi->spi_xfr_flag) {
@@ -697,9 +741,13 @@ hal_spi_disable(int spi_num)
         nrf_spis_event_clear(p_spis, NRF_SPIS_EVENT_ACQUIRED);
         nrf_spis_disable(p_spis);
         spi->slave_state = HAL_SPI_SLAVE_STATE_IDLE;
-        spi->slave_txbuf = NULL;
-        spi->slave_rxbuf = NULL;
     }
+
+    spi->nhs_txbuf = NULL;
+    spi->nhs_rxbuf = NULL;
+    spi->nhs_buflen = 0;
+    spi->nhs_bytes_txd = 0;
+
     rc = 0;
 
 err:
@@ -708,18 +756,16 @@ err:
 
 /**
  * Blocking call to send a value on the SPI. Returns the value received from the
- * SPI.
+ * SPI slave.
  *
  * MASTER: Sends the value and returns the received value from the slave.
- * SLAVE: Sets the value to send to the master when the master transfers a
- *        value. This value will be sent until another call to hal_spi_tx_val()
- *        is made. The return code is ignored for a slave.\ufffd
+ * SLAVE: Invalid API. Returns 0xFFFF
  *
- * @param spi_num
- * @param val
+ * @param spi_num   Spi interface to use
+ * @param val       Value to send
  *
  * @return uint16_t Value received on SPI interface from slave. Returns 0xFFFF
- * if called when SPI configured as a Slave.
+ * if called when the SPI is configured to be a slave
  */
 uint16_t hal_spi_tx_val(int spi_num, uint16_t val)
 {
@@ -730,7 +776,7 @@ uint16_t hal_spi_tx_val(int spi_num, uint16_t val)
 
     NRF52_HAL_SPI_RESOLVE(spi_num, spi);
 
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
         p_spi = (NRF_SPI_Type *) spi->nhs_spi.spim.p_registers;
         nrf_spi_event_clear(p_spi, NRF_SPI_EVENT_READY);
         nrf_spi_txd_set(p_spi, (uint8_t) val);
@@ -749,20 +795,18 @@ err:
 
 /**
  * Sets the txrx callback (executed at interrupt context) when the
- * buffer is transferred by the master or the slave using the hal_spi_rxtx API.
- * This callback is also called when the SPI is a slave and chip select is
- * de-asserted and there is data available in the receive buffer.
- *
- * If the callback is NULL, the SPI will be in blocking mode; otherwise it is
- * in non-blocking mode.
+ * buffer is transferred by the master or the slave using the non-blocking API.
+ * Cannot be called when the spi is enabled. This callback will also be called
+ * when chip select is de-asserted on the slave.
  *
- * Cannot be called when the SPI is enabled.
+ * NOTE: This callback is only used for the non-blocking interface and must
+ * be called prior to using the non-blocking API.
  *
  * @param spi_num   SPI interface on which to set callback
- * @param txrx_cb   Pointer to callback function. NULL to set into blocking mode
- * @param arg       Argument passed to callback function.
+ * @param txrx      Callback function
+ * @param arg       Argument to be passed to callback function
  *
- * @return int 0 on success, -1 if spi is already enabled.
+ * @return int 0 on success, non-zero error code on failure.
  */
 int
 hal_spi_set_txrx_cb(int spi_num, hal_spi_txrx_cb txrx_cb, void *arg)
@@ -781,8 +825,8 @@ hal_spi_set_txrx_cb(int spi_num, hal_spi_txrx_cb txrx_cb, void *arg)
     if (p_spim->ENABLE != 0) {
         rc = -1;
     } else {
-        spi->spi_cfg.txrx_cb_func = txrx_cb;
-        spi->spi_cfg.txrx_cb_arg = arg;
+        spi->txrx_cb_func = txrx_cb;
+        spi->txrx_cb_arg = arg;
         rc = 0;
     }
 
@@ -791,24 +835,25 @@ err:
 }
 
 /**
- * Send a buffer and also store the received values. This call can be either
- * blocking or non-blocking for the master; it is always non-blocking for slave.
- * In non-blocking mode, the txrx callback is executed at interrupt context when
- * the buffer is sent.
+ * Blocking interface to send a buffer and store the received values from the
+ * slave. The transmit and receive buffers are either arrays of 8-bit (uint8_t)
+ * values or 16-bit values depending on whether the spi is configured for 8 bit
+ * data or more than 8 bits per value. The 'cnt' parameter is the number of
+ * 8-bit or 16-bit values. Thus, if 'cnt' is 10, txbuf/rxbuf would point to an
+ * array of size 10 (in bytes) if the SPI is using 8-bit data; otherwise
+ * txbuf/rxbuf would point to an array of size 20 bytes (ten, uint16_t values).
+ *
+ * NOTE: these buffers are in the native endian-ness of the platform.
+ *
  *     MASTER: master sends all the values in the buffer and stores the
  *             stores the values in the receive buffer if rxbuf is not NULL.
  *             The txbuf parameter cannot be NULL.
- *     SLAVE: Slave preloads the data to be sent to the master (values
- *            stored in txbuf) and places received data from master in rxbuf (if
- *            not NULL).  The txrx callback when len values are transferred or
- *            master de-asserts chip select. If txbuf is NULL, the slave
- *            transfers its default byte. Both rxbuf and txbuf cannot be NULL.
+ *     SLAVE: cannot be called for a slave; returns -1
  *
  * @param spi_num   SPI interface to use
  * @param txbuf     Pointer to buffer where values to transmit are stored.
- * @param rxbuf     Pointer to buffer to store values received from peer. Can
- *                  be NULL.
- * @param len       Number of values to be transferred.
+ * @param rxbuf     Pointer to buffer to store values received from peer.
+ * @param cnt       Number of 8-bit or 16-bit values to be transferred.
  *
  * @return int 0 on success, non-zero error code on failure.
  */
@@ -818,6 +863,7 @@ hal_spi_txrx(int spi_num, void *txbuf, void *rxbuf, int len)
     int i;
     int rc;
     int txcnt;
+    uint32_t enabled;
     uint8_t *txd, *rxd;
     uint8_t rxval;
     NRF_SPI_Type *p_spi;
@@ -825,73 +871,161 @@ hal_spi_txrx(int spi_num, void *txbuf, void *rxbuf, int len)
     struct nrf52_hal_spi *spi;
 
     rc = EINVAL;
-    if (!len || (len > 255)) {
+    if (!len) {
         goto err;
     }
 
     NRF52_HAL_SPI_RESOLVE(spi_num, spi);
 
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
-        if (spi->spi_cfg.txrx_cb_func) {
-            /* Must have a txbuf for master! */
-            if (txbuf == NULL) {
-                goto err;
-            }
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
+        /* Must have a txbuf for master! */
+        if (txbuf == NULL) {
+            goto err;
+        }
 
-            /* Not allowed if transfer in progress */
-            if (spi->spi_xfr_flag) {
-                rc = -1;
-                goto err;
-            }
-            spi->spi_xfr_flag = 1;
+        /*
+         * If SPIM is enabled, we want to stop, disable, then enable
+         * the legacy SPI interface.
+         */
+        p_spim = spi->nhs_spi.spim.p_registers;
+        enabled = p_spim->ENABLE;
+        if (enabled == SPIM_ENABLE_ENABLE_Enabled) {
+            nrf_spim_int_disable(p_spim, NRF_SPI_IRQ_DISABLE_ALL);
+            hal_spi_stop_transfer(p_spim);
+            nrf_spim_disable(p_spim);
+            enabled = 0;
+        }
 
-            p_spim = spi->nhs_spi.spim.p_registers;
-            nrf_spim_tx_buffer_set(p_spim, txbuf, len);
+        if (enabled == 0) {
+            nrf_spi_enable((NRF_SPI_Type *)p_spim);
+        }
 
-            /* If no rxbuf, we need to set rxbuf and maxcnt to 1 */
-            if (rxbuf == NULL) {
-                nrf_spim_rx_buffer_set(p_spim, &spi->dummy_rx, 1);
-            } else {
-                nrf_spim_rx_buffer_set(p_spim, rxbuf, len);
+        p_spi = (NRF_SPI_Type *) spi->nhs_spi.spim.p_registers;
+        while (nrf_spi_event_check(p_spi, NRF_SPI_EVENT_READY)) {
+            rxval = nrf_spi_rxd_get(p_spi);
+            nrf_spi_event_clear(p_spi, NRF_SPI_EVENT_READY);
+        }
+        txd = (uint8_t *)txbuf;
+        nrf_spi_txd_set(p_spi, *txd);
+        txcnt = len - 1;
+        rxd = (uint8_t *)rxbuf;
+        for (i = 0; i < len; ++i) {
+            if (txcnt) {
+                ++txd;
+                nrf_spi_txd_set(p_spi, *txd);
+                --txcnt;
+            }
+            while (!nrf_spi_event_check(p_spi, NRF_SPI_EVENT_READY)) {}
+            nrf_spi_event_clear(p_spi, NRF_SPI_EVENT_READY);
+            rxval = nrf_spi_rxd_get(p_spi);
+            if (rxbuf) {
+                *rxd = rxval;
+                ++rxd;
             }
+        }
+        return 0;
+    }
 
-            nrf_spim_event_clear(p_spim, NRF_SPIM_EVENT_END);
-            nrf_spim_event_clear(p_spim, NRF_SPIM_EVENT_STOPPED);
+err:
+    return rc;
+}
 
-            nrf_spim_task_trigger(p_spim, NRF_SPIM_TASK_START);
-            nrf_spim_int_enable(p_spim, NRF_SPIM_INT_END_MASK);
+/**
+ * Non-blocking interface to send a buffer and store received values. Can be
+ * used for both master and slave SPI types. The user must configure the
+ * callback (using hal_spi_set_txrx_cb); the txrx callback is executed at
+ * interrupt context when the buffer is sent.
+ *
+ * The transmit and receive buffers are either arrays of 8-bit (uint8_t)
+ * values or 16-bit values depending on whether the spi is configured for 8 bit
+ * data or more than 8 bits per value. The 'cnt' parameter is the number of
+ * 8-bit or 16-bit values. Thus, if 'cnt' is 10, txbuf/rxbuf would point to an
+ * array of size 10 (in bytes) if the SPI is using 8-bit data; otherwise
+ * txbuf/rxbuf would point to an array of size 20 bytes (ten, uint16_t values).
+ *
+ * NOTE: these buffers are in the native endian-ness of the platform.
+ *
+ *     MASTER: master sends all the values in the buffer and stores the
+ *             stores the values in the receive buffer if rxbuf is not NULL.
+ *             The txbuf parameter cannot be NULL
+ *     SLAVE: Slave "preloads" the data to be sent to the master (values
+ *            stored in txbuf) and places received data from master in rxbuf
+ *            (if not NULL). The txrx callback occurs when len values are
+ *            transferred or master de-asserts chip select. If txbuf is NULL,
+ *            the slave transfers its default byte. Both rxbuf and txbuf cannot
+ *            be NULL.
+ *
+ * @param spi_num   SPI interface to use
+ * @param txbuf     Pointer to buffer where values to transmit are stored.
+ * @param rxbuf     Pointer to buffer to store values received from peer.
+ * @param cnt       Number of 8-bit or 16-bit values to be transferred.
+ *
+ * @return int 0 on success, non-zero error code on failure.
+ */
+int
+hal_spi_txrx_noblock(int spi_num, void *txbuf, void *rxbuf, int len)
+{
+    int rc;
+    NRF_SPIM_Type *p_spim;
+    struct nrf52_hal_spi *spi;
+
+    rc = EINVAL;
+    NRF52_HAL_SPI_RESOLVE(spi_num, spi);
+
+    if ((spi->txrx_cb_func == NULL) || (len == 0)) {
+        goto err;
+    }
+
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
+        /* Must have a txbuf for master! */
+        if (txbuf == NULL) {
+            goto err;
+        }
+
+        /* Not allowed if transfer in progress */
+        if (spi->spi_xfr_flag) {
+            rc = -1;
+            goto err;
+        }
+        p_spim = spi->nhs_spi.spim.p_registers;
+        nrf_spim_int_disable(p_spim, NRF_SPIM_INT_END_MASK);
+        spi->spi_xfr_flag = 1;
+
+        /* Must be enabled for SPIM as opposed to SPI */
+        if (p_spim->ENABLE != SPIM_ENABLE_ENABLE_Enabled) {
+            p_spim->ENABLE = 0;
+            nrf_spim_enable(p_spim);
+        }
+
+        spi->nhs_bytes_txd = 0;
+        spi->nhs_buflen = len;
+        spi->nhs_txbuf = txbuf;
+        nrf_spim_tx_buffer_set(p_spim, txbuf, len);
+
+        /* If no rxbuf, we need to set rxbuf and maxcnt to 1 */
+        spi->nhs_rxbuf = rxbuf;
+        if (rxbuf == NULL) {
+            nrf_spim_rx_buffer_set(p_spim, &spi->dummy_rx, 1);
         } else {
-            /* Blocking spi transfer */
-            p_spi = (NRF_SPI_Type *) spi->nhs_spi.spim.p_registers;
-            while (nrf_spi_event_check(p_spi, NRF_SPI_EVENT_READY)) {
-                rxval = nrf_spi_rxd_get(p_spi);
-                nrf_spi_event_clear(p_spi, NRF_SPI_EVENT_READY);
-            }
-            txd = (uint8_t *)txbuf;
-            nrf_spi_txd_set(p_spi, *txd);
-            txcnt = len - 1;
-            rxd = (uint8_t *)rxbuf;
-            for (i = 0; i < len; ++i) {
-                if (txcnt) {
-                    ++txd;
-                    nrf_spi_txd_set(p_spi, *txd);
-                    --txcnt;
-                }
-                while (!nrf_spi_event_check(p_spi, NRF_SPI_EVENT_READY)) {}
-                nrf_spi_event_clear(p_spi, NRF_SPI_EVENT_READY);
-                rxval = nrf_spi_rxd_get(p_spi);
-                if (rxbuf) {
-                    *rxd = rxval;
-                    ++rxd;
-                }
-            }
+            nrf_spim_rx_buffer_set(p_spim, rxbuf, len);
         }
+
+        nrf_spim_event_clear(p_spim, NRF_SPIM_EVENT_END);
+        nrf_spim_event_clear(p_spim, NRF_SPIM_EVENT_STOPPED);
+
+        nrf_spim_task_trigger(p_spim, NRF_SPIM_TASK_START);
+        nrf_spim_int_enable(p_spim, NRF_SPIM_INT_END_MASK);
     } else {
-        /* Must have txbuf and rxbuf */
+        /* Must have txbuf or rxbuf */
         if ((txbuf == NULL) && (rxbuf == NULL)) {
             goto err;
         }
 
+        /* XXX: what to do here? */
+        if (len > 255) {
+            goto err;
+        }
+
         /*
          * Ready the slave for a transfer. Do not allow this to be called
          * if the slave has already been readied or is requesting the
@@ -902,9 +1036,9 @@ hal_spi_txrx(int spi_num, void *txbuf, void *rxbuf, int len)
             goto err;
         }
 
-        spi->slave_rxbuf = rxbuf;
-        spi->slave_txbuf = txbuf;
-        spi->slave_buflen = len;
+        spi->nhs_rxbuf = rxbuf;
+        spi->nhs_txbuf = txbuf;
+        spi->nhs_buflen = len;
         spi->slave_state = HAL_SPI_SLAVE_STATE_ACQ_SEM;
         nrf_spis_task_trigger(spi->nhs_spi.spis.p_reg, NRF_SPIS_TASK_ACQUIRE);
     }
@@ -915,7 +1049,7 @@ err:
 }
 
 /**
- * Sets the default value transferred by the slave.
+ * Sets the default value transferred by the slave. Not valid for master
  *
  * @param spi_num SPI interface to use
  *
@@ -929,9 +1063,10 @@ hal_spi_slave_set_def_tx_val(int spi_num, uint16_t val)
     struct nrf52_hal_spi *spi;
 
     NRF52_HAL_SPI_RESOLVE(spi_num, spi);
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_SLAVE) {
+    if (spi->spi_type  == HAL_SPI_TYPE_SLAVE) {
         p_spis = spi->nhs_spi.spis.p_reg;
         nrf_spis_def_set(p_spis, (uint8_t) val);
+        nrf_spis_orc_set(p_spis, (uint8_t) val);
         rc = 0;
     } else {
         rc = EINVAL;
@@ -942,8 +1077,7 @@ err:
 }
 
 /**
- * This aborts the current transfer but keeps the spi enabled. Should only
- * be used when the SPI is in non-blocking mode.
+ * This aborts the current transfer but keeps the spi enabled.
  *
  * @param spi_num   SPI interface on which transfer should be aborted.
  *
@@ -961,11 +1095,7 @@ hal_spi_abort(int spi_num)
     NRF52_HAL_SPI_RESOLVE(spi_num, spi);
 
     rc = 0;
-    if (spi->spi_cfg.txrx_cb_func == NULL) {
-        goto err;
-    }
-
-    if (spi->spi_cfg.spi_type  == HAL_SPI_TYPE_MASTER) {
+    if (spi->spi_type  == HAL_SPI_TYPE_MASTER) {
         p_spim = spi->nhs_spi.spim.p_registers;
         if (spi->spi_xfr_flag) {
             nrf_spim_int_disable(p_spim, NRF_SPI_IRQ_DISABLE_ALL);


[49/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-391 STM32F4 ADC driver and ADC HAL support

Posted by vi...@apache.org.
MYNEWT-391 STM32F4 ADC driver and ADC HAL support

- Addressing PR review


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

Branch: refs/heads/sterly_refactor
Commit: d4c58070df322dd5f13b085905d6799afb78e37f
Parents: dfca220
Author: Vipul Rahane <vi...@runtime.io>
Authored: Thu Sep 22 13:08:18 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Thu Sep 22 20:21:03 2016 -0700

----------------------------------------------------------------------
 drivers/adc/adc_stm32f4/src/adc_stm32f4.c | 43 +++++++++++++-------------
 drivers/adc/include/adc/adc.h             |  2 +-
 2 files changed, 22 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d4c58070/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/src/adc_stm32f4.c b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
index d95c4ea..efef0b4 100644
--- a/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
+++ b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
@@ -40,11 +40,11 @@ static struct adc_dev *adc_dma[5];
 struct stm32f4_adc_stats {
     uint16_t adc_events;
     uint16_t adc_error;
-    uint16_t adc_DMA_xfer_failed;
-    uint16_t adc_DMA_xfer_aborted;
-    uint16_t adc_DMA_xfer_complete;
-    uint16_t adc_DMA_start_error;
-    uint16_t adc_DMA_overrun;
+    uint16_t adc_dma_xfer_failed;
+    uint16_t adc_dma_xfer_aborted;
+    uint16_t adc_dma_xfer_complete;
+    uint16_t adc_dma_start_error;
+    uint16_t adc_dma_overrun;
     uint16_t adc_internal_error;
 };
 
@@ -276,10 +276,10 @@ HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
 
     if (hadc->ErrorCode & HAL_ADC_ERROR_DMA) {
         /* DMA transfer error */
-        ++stm32f4_adc_stats.adc_DMA_xfer_failed;
+        ++stm32f4_adc_stats.adc_dma_xfer_failed;
     } else if (hadc->ErrorCode & HAL_ADC_ERROR_OVR) {
         /* DMA transfer overrun */
-        ++stm32f4_adc_stats.adc_DMA_overrun;
+        ++stm32f4_adc_stats.adc_dma_overrun;
     } else if (hadc->ErrorCode & HAL_ADC_ERROR_INTERNAL) {
        /* ADC IP Internal Error */
         ++stm32f4_adc_stats.adc_internal_error;
@@ -304,10 +304,10 @@ HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
     assert(hadc);
     hdma = hadc->DMA_Handle;
 
-    ++stm32f4_adc_stats.adc_DMA_xfer_complete;
+    ++stm32f4_adc_stats.adc_dma_xfer_complete;
 
     adc = adc_dma[stm32f4_resolve_dma_handle_idx(hdma)];
-    cfg  = adc->adc_dev_cfg;
+    cfg  = adc->ad_dev_cfg;
 
     buf = cfg->primarybuf;
     /**
@@ -324,7 +324,7 @@ HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
         cfg->secondarybuf = buf;
 
         if (HAL_ADC_Start_DMA(hadc, cfg->primarybuf, cfg->buflen) != HAL_OK) {
-            ++stm32f4_adc_stats.adc_DMA_start_error;
+            ++stm32f4_adc_stats.adc_dma_start_error;
         }
     }
 
@@ -386,7 +386,7 @@ stm32f4_adc_uninit(struct adc_dev *dev)
     uint8_t cnum;
 
     assert(dev);
-    cfg  = dev->adc_dev_cfg;
+    cfg  = dev->ad_dev_cfg;
     hadc = cfg->sac_adc_handle;
     hdma = hadc->DMA_Handle;
     cnum = dev->ad_chans->c_cnum;
@@ -446,9 +446,9 @@ stm32f4_adc_open(struct os_dev *odev, uint32_t wait, void *arg)
     }
 
 
-    stm32f4_adc_init(dev->adc_dev_cfg);
+    stm32f4_adc_init(dev->ad_dev_cfg);
 
-    cfg  = dev->adc_dev_cfg;
+    cfg  = dev->ad_dev_cfg;
     hadc = cfg->sac_adc_handle;
     hdma = hadc->DMA_Handle;
 
@@ -504,18 +504,17 @@ stm32f4_adc_configure_channel(struct adc_dev *dev, uint8_t cnum,
     struct adc_chan_config *chan_cfg;
 
     assert(dev != NULL && IS_ADC_CHANNEL(cnum));
-    cfg  = dev->adc_dev_cfg;
+    cfg  = dev->ad_dev_cfg;
     hadc = cfg->sac_adc_handle;
     chan_cfg = cfg->sac_chans;
 
-    // Enable DMA, ADC and related GPIO ports clock
     gpio_td = stm32f4_resolve_adc_gpio(hadc, cnum);
     hal_gpio_init_stm(gpio_td.Pin, &gpio_td);
 
 
     cfgdata = (ADC_ChannelConfTypeDef *)cfgdata;
 
-    if((rc = HAL_ADC_ConfigChannel(hadc, cfgdata)) != HAL_OK) {
+    if ((rc = HAL_ADC_ConfigChannel(hadc, cfgdata)) != HAL_OK) {
         goto err;
     }
 
@@ -553,7 +552,7 @@ stm32f4_adc_set_buffer(struct adc_dev *dev, void *buf1, void *buf2,
     rc = OS_OK;
     buflen /= sizeof(uint16_t);
 
-    cfg  = dev->adc_dev_cfg;
+    cfg  = dev->ad_dev_cfg;
 
     cfg->primarybuf = buf1;
     cfg->secondarybuf = buf2;
@@ -569,7 +568,7 @@ stm32f4_adc_release_buffer(struct adc_dev *dev, void *buf, int buf_len)
     struct stm32f4_adc_dev_cfg *cfg;
 
     assert(dev);
-    cfg  = dev->adc_dev_cfg;
+    cfg  = dev->ad_dev_cfg;
     hadc = cfg->sac_adc_handle;
 
     HAL_ADC_Stop_DMA(hadc);
@@ -591,13 +590,13 @@ stm32f4_adc_sample(struct adc_dev *dev)
     struct stm32f4_adc_dev_cfg *cfg;
 
     assert(dev);
-    cfg  = dev->adc_dev_cfg;
+    cfg  = dev->ad_dev_cfg;
     hadc = cfg->sac_adc_handle;
 
     rc = OS_EINVAL;
 
     if (HAL_ADC_Start_DMA(hadc, cfg->primarybuf, cfg->buflen) != HAL_OK) {
-        ++stm32f4_adc_stats.adc_DMA_start_error;
+        ++stm32f4_adc_stats.adc_dma_start_error;
         goto err;
     }
 
@@ -621,7 +620,7 @@ stm32f4_adc_read_channel(struct adc_dev *dev, uint8_t cnum, int *result)
     struct stm32f4_adc_dev_cfg *cfg;
 
     assert(dev != NULL && result != NULL);
-    cfg  = dev->adc_dev_cfg;
+    cfg  = dev->ad_dev_cfg;
     hadc = cfg->sac_adc_handle;
 
     *result = HAL_ADC_GetValue(hadc);
@@ -697,7 +696,7 @@ stm32f4_adc_dev_init(struct os_dev *odev, void *arg)
     OS_DEV_SETHANDLERS(odev, stm32f4_adc_open, stm32f4_adc_close);
 
     af = &dev->ad_funcs;
-    dev->adc_dev_cfg = arg;
+    dev->ad_dev_cfg = arg;
 
     af->af_configure_channel = stm32f4_adc_configure_channel;
     af->af_sample = stm32f4_adc_sample;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/d4c58070/drivers/adc/include/adc/adc.h
----------------------------------------------------------------------
diff --git a/drivers/adc/include/adc/adc.h b/drivers/adc/include/adc/adc.h
index d81286e..60df1ad 100644
--- a/drivers/adc/include/adc/adc.h
+++ b/drivers/adc/include/adc/adc.h
@@ -154,7 +154,7 @@ struct adc_dev {
     struct os_mutex ad_lock;
     struct adc_driver_funcs ad_funcs;
     struct adc_chan_config *ad_chans;
-    void *adc_dev_cfg;
+    void *ad_dev_cfg;
     int ad_chan_count;
     adc_event_handler_func_t ad_event_handler_func;
     void *ad_event_handler_arg;


[22/50] [abbrv] incubator-mynewt-core git commit: Merge branch 'develop' into sterly_refactor

Posted by vi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/bootutil/test/src/boot_test.c
----------------------------------------------------------------------
diff --cc libs/bootutil/test/src/boot_test.c
index b86ca82,0000000..94c8554
mode 100644,000000..100644
--- a/libs/bootutil/test/src/boot_test.c
+++ b/libs/bootutil/test/src/boot_test.c
@@@ -1,1170 -1,0 +1,1203 @@@
 +/**
 + * 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 },
++    [7] = { 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) {
++#if 0
 +                TEST_ASSERT(buf[i] == 0xff);
++#endif
 +            }
 +        }
 +
 +        addr += chunk_sz;
 +    }
 +}
 +
 +static void
 +boot_test_util_verify_status_clear(void)
 +{
-     struct fs_file *file;
++    struct boot_img_trailer bit;
++    const struct flash_area *fap;
 +    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);
++    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
 +    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 = flash_area_read(fap, fap->fa_size - sizeof(bit), &bit, sizeof(bit));
++    TEST_ASSERT(rc == 0);
 +
-     rc = fs_open(BOOT_PATH_STATUS, FS_ACCESS_READ, &file);
-     TEST_ASSERT(rc == FS_ENOENT);
++    TEST_ASSERT(bit.bit_copy_start != BOOT_IMG_MAGIC ||
++      bit.bit_copy_done != 0xff);
 +}
 +
 +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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    boot_test_util_init_flash();
 +    boot_test_util_write_image(&hdr, 1);
 +    boot_test_util_write_hash(&hdr, 1);
 +
++    boot_vect_write_test(FLASH_AREA_IMAGE_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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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);
++    rc = boot_vect_write_test(FLASH_AREA_IMAGE_1);
 +    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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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);
++    rc = boot_vect_write_test(FLASH_AREA_IMAGE_1);
 +    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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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);
++    rc = boot_vect_write_test(FLASH_AREA_IMAGE_1);
 +    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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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);
- 
++#if 0
 +    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();
- 
++#else
++    (void)status;
++#endif
 +    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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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],
++    rc = boot_vect_write_test(FLASH_AREA_IMAGE_1);
++    boot_test_util_copy_area(5,
 +      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;
-     }
++    boot_req_set(&req);
++    status.idx = 0;
++    status.elem_sz = 1;
 +    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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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]);
++    rc = boot_vect_write_test(FLASH_AREA_IMAGE_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;
++    boot_test_util_swap_areas(2, 5);
++
++    status.idx = 1;
++    status.elem_sz = 1;
++    status.state = 0;
 +
 +    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)
 +{
++    const struct flash_area *fap;
++    struct boot_img_trailer bit;
 +    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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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_image(&hdr1, 1);
 +    boot_test_util_write_hash(&hdr1, 1);
 +
-     rc = boot_vect_write_main(&hdr0.ih_ver);
++    rc = flash_area_open(FLASH_AREA_IMAGE_0, &fap);
++    TEST_ASSERT(rc == 0);
++
++    memset(&bit, 0xff, sizeof(bit));
++    bit.bit_copy_start = BOOT_IMG_MAGIC;
++    bit.bit_copy_done = 0;
++    bit.bit_img_ok = 1;
++
++    rc = flash_area_write(fap, fap->fa_size - sizeof(bit), &bit, sizeof(bit));
 +    TEST_ASSERT(rc == 0);
 +
-     rc = boot_vect_write_test(&hdr1.ih_ver);
++    rc = boot_vect_write_test(FLASH_AREA_IMAGE_1);
 +    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();
++        boot_vect_write_main();
 +    }
 +}
 +
 +TEST_CASE(boot_test_no_hash)
 +{
 +    struct boot_rsp rsp;
 +    int rc;
 +
-     struct image_header hdr = {
++    struct image_header hdr0 = {
 +        .ih_magic = IMAGE_MAGIC,
-         .ih_tlv_size = 0,
++        .ih_tlv_size = 4 + 32,
 +        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
 +        .ih_img_size = 12 * 1024,
-         .ih_flags = 0,
++        .ih_flags = IMAGE_F_SHA256,
 +        .ih_ver = { 0, 2, 3, 4 },
 +    };
++    struct image_header hdr1 = {
++        .ih_magic = IMAGE_MAGIC,
++        .ih_tlv_size = 0,
++        .ih_hdr_size = BOOT_TEST_HEADER_SIZE,
++        .ih_img_size = 32 * 1024,
++        .ih_flags = 0,
++        .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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    boot_test_util_init_flash();
-     boot_test_util_write_image(&hdr, 0);
++    boot_test_util_write_image(&hdr0, 0);
++    boot_test_util_write_hash(&hdr0, 0);
++    boot_test_util_write_image(&hdr1, 1);
++
++    rc = boot_vect_write_test(FLASH_AREA_IMAGE_1);
++    TEST_ASSERT(rc == 0);
 +
 +    rc = boot_go(&req, &rsp);
-     TEST_ASSERT(rc != 0);
++    TEST_ASSERT(rc == 0);
 +
-     boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
++    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
++
++    boot_test_util_verify_flash(&hdr0, 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 = {
++    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 = 0,
++        .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 = 32 * 1024,
++        .ih_flags = 0,
++        .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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    boot_test_util_init_flash();
-     boot_test_util_write_image(&hdr, 0);
-     boot_test_util_write_hash(&hdr, 0);
++    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_test(FLASH_AREA_IMAGE_1);
++    TEST_ASSERT(rc == 0);
 +
 +    rc = boot_go(&req, &rsp);
-     TEST_ASSERT(rc != 0);
++    TEST_ASSERT(rc == 0);
 +
-     boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
++    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
++
++    boot_test_util_verify_flash(&hdr0, 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 = {
++    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 = 32 * 1024,
++        .ih_flags = 0,
++        .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,
++        .br_img_sz = (384 * 1024),
 +    };
 +
 +    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,
++    boot_test_util_write_image(&hdr0, 0);
++    boot_test_util_write_hash(&hdr0, 0);
++    boot_test_util_write_image(&hdr1, 1);
++    rc = hal_flash_write(boot_test_img_addrs[1].flash_id,
++      boot_test_img_addrs[1].address + hdr1.ih_hdr_size + hdr1.ih_img_size,
 +      &tlv, sizeof(tlv));
 +    TEST_ASSERT(rc == 0);
 +
++    rc = boot_vect_write_test(FLASH_AREA_IMAGE_1);
++    TEST_ASSERT(rc == 0);
++
 +    rc = boot_go(&req, &rsp);
-     TEST_ASSERT(rc != 0);
++    TEST_ASSERT(rc == 0);
 +
-     boot_test_util_verify_flash(&hdr, 0, NULL, 0xff);
++    TEST_ASSERT(memcmp(rsp.br_hdr, &hdr0, sizeof hdr0) == 0);
++
++    boot_test_util_verify_flash(&hdr0, 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)
++main(int argc, char **argv)
 +{
 +    tu_config.tc_print_results = 1;
++    tu_parse_args(argc, argv);
++
 +    tu_init();
 +
 +    boot_test_all();
 +
 +    return tu_any_failed;
 +}
 +
 +#endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/console/full/src/cons_tty.c
----------------------------------------------------------------------
diff --cc libs/console/full/src/cons_tty.c
index 49dfefc,83bc843..db8c88e
--- a/libs/console/full/src/cons_tty.c
+++ b/libs/console/full/src/cons_tty.c
@@@ -18,13 -18,15 +18,15 @@@
   */
  
  #include <inttypes.h>
 +#include <assert.h>
 +#include "sysinit/sysinit.h"
  #include "os/os.h"
 -#include "hal/hal_uart.h"
 +#include "uart/uart.h"
  #include "bsp/bsp.h"
+ 
  #include "console/console.h"
+ #include "console/prompt.h"
  
 -int g_console_is_init;
 -
  /** Indicates whether the previous line of output was completed. */
  int console_is_midline;
  
@@@ -376,40 -374,28 +379,42 @@@ in
  console_init(console_rx_cb rx_cb)
  {
      struct console_tty *ct = &console_tty;
 -    int rc;
 +    struct uart_conf uc = {
 +        .uc_speed = CONSOLE_UART_SPEED,
 +        .uc_databits = 8,
 +        .uc_stopbits = 1,
 +        .uc_parity = UART_PARITY_NONE,
 +        .uc_flow_ctl = UART_FLOW_CTL_NONE,
 +        .uc_tx_char = console_tx_char,
 +        .uc_rx_char = console_rx_char,
 +        .uc_cb_arg = ct
 +    };
  
 -    rc = hal_uart_init_cbs(CONSOLE_UART, console_tx_char, NULL,
 -            console_rx_char, ct);
 -    if (rc) {
 -        return rc;
 -    }
 -    ct->ct_tx.cr_size = CONSOLE_TX_BUF_SZ;
 -    ct->ct_tx.cr_buf = ct->ct_tx_buf;
 -    ct->ct_rx.cr_size = CONSOLE_RX_BUF_SZ;
 -    ct->ct_rx.cr_buf = ct->ct_rx_buf;
      ct->ct_rx_cb = rx_cb;
 -    ct->ct_write_char = console_queue_char;
 -
 -    rc = hal_uart_config(CONSOLE_UART, 115200, 8, 1, HAL_UART_PARITY_NONE,
 -      HAL_UART_FLOW_CTL_NONE);
 -    if (rc) {
 -        return rc;
 +    if (!ct->ct_dev) {
 +        ct->ct_tx.cr_size = CONSOLE_TX_BUF_SZ;
 +        ct->ct_tx.cr_buf = ct->ct_tx_buf;
 +        ct->ct_rx.cr_size = CONSOLE_RX_BUF_SZ;
 +        ct->ct_rx.cr_buf = ct->ct_rx_buf;
 +        ct->ct_write_char = console_queue_char;
 +
 +        ct->ct_dev = (struct uart_dev *)os_dev_open(CONSOLE_UART,
 +          OS_TIMEOUT_NEVER, &uc);
 +        if (!ct->ct_dev) {
 +            return -1;
 +        }
      }
  
 -    g_console_is_init = 1;
+     console_print_prompt();
+     
      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/c5901fcc/libs/imgmgr/include/imgmgr/imgmgr.h
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/imgmgr/pkg.yml
----------------------------------------------------------------------
diff --cc libs/imgmgr/pkg.yml
index ff7812e,dc214c1..5194768
--- a/libs/imgmgr/pkg.yml
+++ b/libs/imgmgr/pkg.yml
@@@ -24,23 -24,18 +24,28 @@@ pkg.homepage: "http://mynewt.apache.org
  pkg.keywords:
  
  pkg.deps:
-     - libs/newtmgr
      - libs/bootutil
      - libs/util
 -pkg.deps.FS:
 +
 +pkg.deps.IMGMGR_FS:
      - fs/fs
 -pkg.cflags.FS: -DFS_PRESENT
 -pkg.req_apis:
 -    - newtmgr
  
 -pkg.deps.COREDUMP:
 +pkg.deps.IMGMGR_COREDUMP:
      - sys/coredump
 -pkg.cflags.COREDUMP: -DCOREDUMP_PRESENT
  
 -pkg.deps.SHELL:
++pkg.deps.IMGMGR_SHELL:
+     - libs/shell
 -pkg.cflags.SHELL: -DSHELL_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'
++    IMGMGR_CLI:
++        description: 'TBD'
++        value: 'MYNEWT_PKG_LIBS_SHELL'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/imgmgr/src/imgmgr.c
----------------------------------------------------------------------
diff --cc libs/imgmgr/src/imgmgr.c
index 0cb7bc4,9a9b884..d204cc4
--- a/libs/imgmgr/src/imgmgr.c
+++ b/libs/imgmgr/src/imgmgr.c
@@@ -21,14 -21,14 +21,13 @@@
  #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 <bootutil/bootutil_misc.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"
@@@ -48,11 -47,11 +46,11 @@@ static const struct nmgr_handler imgr_n
          .nh_write = imgr_upload
      },
      [IMGMGR_NMGR_OP_BOOT] = {
-         .nh_read = imgr_boot_read,
-         .nh_write = imgr_boot_write
+         .nh_read = imgr_noop,
+         .nh_write = imgr_noop
      },
      [IMGMGR_NMGR_OP_FILE] = {
 -#ifdef FS_PRESENT
 +#if MYNEWT_VAL(IMGMGR_FS)
          .nh_read = imgr_file_download,
          .nh_write = imgr_file_upload
  #else
@@@ -494,5 -461,14 +460,12 @@@ imgmgr_module_init(void
      int rc;
  
      rc = nmgr_group_register(&imgr_nmgr_group);
 -    assert(rc == 0);
 +    SYSINIT_PANIC_ASSERT(rc == 0);
+ 
 -#ifdef SHELL_PRESENT
++#if MYNEWT_VAL(IMGMGR_CLI)
+     rc = imgr_cli_register();
 -    assert(rc == 0);
++    SYSINIT_PANIC_ASSERT(rc == 0);
+ #endif
+ 
+     boot_vect_write_main();
 -
 -    return rc;
  }

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/imgmgr/src/imgmgr_priv.h
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/json/test/src/test_json_simple.c
----------------------------------------------------------------------
diff --cc libs/json/test/src/test_json_simple.c
index 55d50f4,0000000..0f9e10e
mode 100644,000000..100644
--- a/libs/json/test/src/test_json_simple.c
+++ b/libs/json/test/src/test_json_simple.c
@@@ -1,360 -1,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);
++   TEST_ASSERT(uintarr[2] == 4294967295ULL);
++   TEST_ASSERT(uintarr[3] == 8589934590ULL);
++   TEST_ASSERT(uintarr[4] == 3451257ULL);
 +
 +    /*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/c5901fcc/libs/newtmgr/include/newtmgr/newtmgr.h
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/newtmgr/pkg.yml
----------------------------------------------------------------------
diff --cc libs/newtmgr/pkg.yml
index 2dbdb71,93fbae7..0afb997
--- a/libs/newtmgr/pkg.yml
+++ b/libs/newtmgr/pkg.yml
@@@ -27,24 -27,17 +27,28 @@@ pkg.deps
      - hw/hal
      - libs/os
      - libs/json
+     - libs/newtmgr/nmgr_os
      - libs/util
 -    - libs/testutil
      - libs/shell
      - sys/reboot
  
  pkg.deps.BLE_HOST:
      - libs/newtmgr/transport/ble
  
+ pkg.apis:
+     - newtmgr
+ 
 -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/c5901fcc/libs/newtmgr/src/newtmgr.c
----------------------------------------------------------------------
diff --cc libs/newtmgr/src/newtmgr.c
index 9b0d696,8ff2ea3..3697ae7
--- a/libs/newtmgr/src/newtmgr.c
+++ b/libs/newtmgr/src/newtmgr.c
@@@ -20,18 -20,12 +20,16 @@@
  #include <assert.h>
  #include <string.h>
  
 -#include <shell/shell.h>
 -#include <newtmgr/newtmgr.h>
 -#include <nmgr_os/nmgr_os.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"
++#include "nmgr_os/nmgr_os.h"
 +
 +os_stack_t newtmgr_stack[OS_STACK_ALIGN(MYNEWT_VAL(NEWTMGR_STACK_SIZE))];
  
  struct nmgr_transport g_nmgr_shell_transport;
  
@@@ -606,29 -526,8 +530,8 @@@ err
  }
  
  
- static int
- nmgr_default_groups_register(void)
- {
-     int rc;
- 
-     NMGR_GROUP_SET_HANDLERS(&nmgr_def_group,
-       (struct nmgr_handler *)nmgr_def_group_handlers);
-     nmgr_def_group.ng_group_id = NMGR_GROUP_ID_DEFAULT;
-     nmgr_def_group.ng_handlers_count =
-       sizeof(nmgr_def_group_handlers) / sizeof(nmgr_def_group_handlers[0]);
- 
-     rc = nmgr_group_register(&nmgr_def_group);
-     if (rc != 0) {
-         goto err;
-     }
- 
-     return (0);
- err:
-     return (rc);
- }
- 
  int
 -nmgr_task_init(uint8_t prio, os_stack_t *stack_ptr, uint16_t stack_len)
 +nmgr_task_init(void)
  {
      int rc;
  

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/os/test/src/eventq_test.c
----------------------------------------------------------------------
diff --cc libs/os/test/src/eventq_test.c
index cb1ed94,0000000..3fceb0e
mode 100644,000000..100644
--- a/libs/os/test/src/eventq_test.c
+++ b/libs/os/test/src/eventq_test.c
@@@ -1,416 -1,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);
++         os_time_delay(OS_TICKS_PER_SEC);
 +
 +        /* 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);
++        event = os_eventq_poll(eventqs, SIZE_MULTI_EVENT, OS_TICKS_PER_SEC / 5);
 +        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/c5901fcc/libs/os/test/src/mutex_test.c
----------------------------------------------------------------------
diff --cc libs/os/test/src/mutex_test.c
index ef6a08d,0000000..d23f099
mode 100644,000000..100644
--- a/libs/os/test/src/mutex_test.c
+++ b/libs/os/test/src/mutex_test.c
@@@ -1,407 -1,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);
++        os_time_delay(OS_TICKS_PER_SEC / 10);
 +
 +        g_task14_val = 1;
 +
-         err = os_mutex_pend(&g_mutex1, 100);
++        err = os_mutex_pend(&g_mutex1, OS_TICKS_PER_SEC / 10);
 +        TEST_ASSERT(err == OS_OK);
 +        TEST_ASSERT(g_task16_val == 1);
 +
-         os_time_delay(100);
++        os_time_delay(OS_TICKS_PER_SEC / 10);
 +    }
 +
 +    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);
++        os_time_delay(OS_TICKS_PER_SEC / 10);
 +
 +        /* 
 +         * 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_time_delay(OS_TICKS_PER_SEC / 10);
 +    }
 +
 +    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);
++            os_time_delay(OS_TICKS_PER_SEC / 20);
 +            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);
++            os_time_delay(OS_TICKS_PER_SEC / 2);
 +        } else if (g_mutex_test == 3) {
 +            /* Sleep for 3 seconds */
 +            t = os_sched_get_current_task();
-             os_time_delay(30);
++            os_time_delay(OS_TICKS_PER_SEC / 33);
 +        }
 +
 +        while (1) {
 +            t = os_sched_get_current_task();
 +            TEST_ASSERT(t->t_func == task15_handler);
 +
-             err = os_mutex_pend(&g_mutex1, 10000);
++            err = os_mutex_pend(&g_mutex1, OS_TICKS_PER_SEC * 10);
 +            if (g_mutex_test == 4) {
 +                TEST_ASSERT(err == OS_TIMEOUT);
 +            } else {
 +                TEST_ASSERT(err == OS_OK);
 +            }
 +
-             os_time_delay(100);
++            os_time_delay(OS_TICKS_PER_SEC / 10);
 +        }
 +    }
 +}
 +
 +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);
++            err = os_mutex_pend(&g_mutex1, OS_TIMEOUT_NEVER);
 +            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);
++            os_time_delay(OS_TICKS_PER_SEC / 33);
 +        } else if (g_mutex_test == 3) {
 +            /* Sleep for 3 seconds */
 +            t = os_sched_get_current_task();
-             os_time_delay(50);
++            os_time_delay(OS_TICKS_PER_SEC / 20);
 +        }
 +
 +        while (1) {
 +            t = os_sched_get_current_task();
 +            TEST_ASSERT(t->t_func == task16_handler);
 +
-             err = os_mutex_pend(&g_mutex1, 10000);
++            err = os_mutex_pend(&g_mutex1, OS_TICKS_PER_SEC * 10);
 +            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);
++            os_time_delay(OS_TICKS_PER_SEC * 10);
 +        }
 +    }
 +}
 +
 +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);
++            err = os_mutex_pend(&g_mutex1, OS_TICKS_PER_SEC / 10);
 +        } else {
-             err = os_mutex_pend(&g_mutex1, 10000);
++            err = os_mutex_pend(&g_mutex1, OS_TICKS_PER_SEC * 10);
 +            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);
++        os_time_delay(OS_TICKS_PER_SEC * 10);
 +    }
 +}
 +
 +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/c5901fcc/libs/os/test/src/os_test.c
----------------------------------------------------------------------
diff --cc libs/os/test/src/os_test.c
index 809ab9f,0000000..e9d041b
mode 100644,000000..100644
--- a/libs/os/test/src/os_test.c
+++ b/libs/os/test/src/os_test.c
@@@ -1,53 -1,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();
-     
++    os_callout_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/c5901fcc/libs/os/test/src/os_test_priv.h
----------------------------------------------------------------------
diff --cc libs/os/test/src/os_test_priv.h
index 5193c33,0000000..e923a6f
mode 100644,000000..100644
--- a/libs/os/test/src/os_test_priv.h
+++ b/libs/os/test/src/os_test_priv.h
@@@ -1,32 -1,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);
- 
++int os_callout_test_suite(void);
 +
 +#endif



[18/50] [abbrv] incubator-mynewt-core git commit: Merge branch 'develop' into sterly_refactor

Posted by vi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/test/src/ble_gatts_notify_test.c
----------------------------------------------------------------------
diff --cc net/nimble/host/test/src/ble_gatts_notify_test.c
index 4516e66,0000000..4e2d587
mode 100644,000000..100644
--- a/net/nimble/host/test/src/ble_gatts_notify_test.c
+++ b/net/nimble/host/test/src/ble_gatts_notify_test.c
@@@ -1,983 -1,0 +1,1074 @@@
 +/**
 + * 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)
++ble_gatts_notify_test_misc_try_enable_notify(uint16_t conn_handle,
++                                             uint16_t chr_def_handle,
++                                             uint16_t flags, int fail)
 +{
 +    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);
++    if (fail) {
++        TEST_ASSERT_FATAL(rc != 0);
++        ble_hs_test_util_verify_tx_err_rsp(BLE_ATT_OP_WRITE_REQ,
++                                           req.bawq_handle,
++                                           BLE_ATT_ERR_REQ_NOT_SUPPORTED);
++    } else {
++        TEST_ASSERT_FATAL(rc == 0);
++        ble_hs_test_util_verify_tx_write_rsp();
++    }
++}
++
++static void
++ble_gatts_notify_test_misc_enable_notify(uint16_t conn_handle,
++                                         uint16_t chr_def_handle,
++                                         uint16_t flags)
++{
++    ble_gatts_notify_test_misc_try_enable_notify(conn_handle,
++                                                 chr_def_handle,
++                                                 flags, 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_CASE(ble_gatts_notify_test_disallowed)
++{
++    uint16_t chr1_val_handle;
++    uint16_t chr2_val_handle;
++    uint16_t chr3_val_handle;
++    int rc;
++
++    const struct ble_gatt_svc_def svcs[] = { {
++        .type = BLE_GATT_SVC_TYPE_PRIMARY,
++        .uuid128 = BLE_UUID16(0x1234),
++        .characteristics = (struct ble_gatt_chr_def[]) { {
++            .uuid128 = BLE_UUID16(1),
++            .access_cb = ble_gatts_notify_test_misc_access,
++            .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY,
++            .val_handle = &chr1_val_handle,
++        }, {
++            .uuid128 = BLE_UUID16(2),
++            .access_cb = ble_gatts_notify_test_misc_access,
++            .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_INDICATE,
++            .val_handle = &chr2_val_handle,
++        }, {
++            .uuid128 = BLE_UUID16(3),
++            .access_cb = ble_gatts_notify_test_misc_access,
++            .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY |
++                     BLE_GATT_CHR_F_INDICATE,
++            .val_handle = &chr3_val_handle,
++        }, {
++            0
++        } },
++    }, {
++        0
++    } };
++
++    ble_hs_test_util_init();
++
++    rc = ble_gatts_register_svcs(svcs, NULL, NULL);
++    TEST_ASSERT_FATAL(rc == 0);
++    TEST_ASSERT_FATAL(chr1_val_handle != 0);
++    TEST_ASSERT_FATAL(chr2_val_handle != 0);
++    TEST_ASSERT_FATAL(chr3_val_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);
++
++    /* Attempt to enable notifications on chr1 should succeed. */
++    ble_gatts_notify_test_misc_try_enable_notify(
++        2, chr1_val_handle - 1, BLE_GATTS_CLT_CFG_F_NOTIFY, 0);
++
++    /* Attempt to enable indications on chr1 should fail. */
++    ble_gatts_notify_test_misc_try_enable_notify(
++        2, chr1_val_handle - 1, BLE_GATTS_CLT_CFG_F_INDICATE, 1);
++
++    /* Attempt to enable notifications on chr2 should fail. */
++    ble_gatts_notify_test_misc_try_enable_notify(
++        2, chr2_val_handle - 1, BLE_GATTS_CLT_CFG_F_NOTIFY, 1);
++
++    /* Attempt to enable indications on chr2 should succeed. */
++    ble_gatts_notify_test_misc_try_enable_notify(
++        2, chr2_val_handle - 1, BLE_GATTS_CLT_CFG_F_INDICATE, 0);
++
++    /* Attempt to enable notifications on chr3 should succeed. */
++    ble_gatts_notify_test_misc_try_enable_notify(
++        2, chr3_val_handle - 1, BLE_GATTS_CLT_CFG_F_NOTIFY, 0);
++
++    /* Attempt to enable indications on chr3 should succeed. */
++    ble_gatts_notify_test_misc_try_enable_notify(
++        2, chr3_val_handle - 1, BLE_GATTS_CLT_CFG_F_INDICATE, 0);
++}
++
 +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();
 +
++    ble_gatts_notify_test_disallowed();
++
 +    /* 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/c5901fcc/net/nimble/host/test/src/ble_hs_test_util.c
----------------------------------------------------------------------
diff --cc net/nimble/host/test/src/ble_hs_test_util.c
index eed1262,0000000..81e059a
mode 100644,000000..100644
--- a/net/nimble/host/test/src/ble_hs_test_util.c
+++ b/net/nimble/host/test/src/ble_hs_test_util.c
@@@ -1,1404 -1,0 +1,1604 @@@
 +/**
 + * 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_hs_test_util_create_conn(uint16_t handle, const 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)
 +{
++    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);
++}
++
++void
++ble_hs_test_util_set_ack_disconnect(uint8_t hci_status)
++{
 +    ble_hs_test_util_set_ack(
 +        ble_hs_hci_util_opcode_join(BLE_HCI_OGF_LINK_CTRL,
 +                                    BLE_HCI_OCF_DISCONNECT_CMD),
 +        hci_status);
++}
 +
++int
++ble_hs_test_util_conn_terminate(uint16_t conn_handle, uint8_t hci_status)
++{
++    int rc;
++
++    ble_hs_test_util_set_ack_disconnect(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_mtu_cmd(uint16_t conn_handle, int is_req, uint16_t mtu)
++{
++    struct ble_att_mtu_cmd cmd;
++    uint8_t buf[BLE_ATT_MTU_CMD_SZ];
++    int rc;
++
++    cmd.bamc_mtu = mtu;
++
++    if (is_req) {
++        ble_att_mtu_req_write(buf, sizeof buf, &cmd);
++    } else {
++        ble_att_mtu_rsp_write(buf, sizeof buf, &cmd);
++    }
++
++    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_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_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);
++}
++
++void
++ble_hs_test_util_verify_tx_mtu_cmd(int is_req, uint16_t mtu)
++{
++    struct ble_att_mtu_cmd cmd;
++    struct os_mbuf *om;
++
++    ble_hs_test_util_tx_all();
++
++    om = ble_hs_test_util_prev_tx_dequeue_pullup();
++    TEST_ASSERT_FATAL(om != NULL);
++
++    if (is_req) {
++        ble_att_mtu_req_parse(om->om_data, om->om_len, &cmd);
++    } else {
++        ble_att_mtu_rsp_parse(om->om_data, om->om_len, &cmd);
++    }
++
++    TEST_ASSERT(cmd.bamc_mtu == mtu);
++}
++
++void
++ble_hs_test_util_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 struct os_mbuf *
++ble_hs_test_util_verify_tx_l2cap_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.
++ */
++uint8_t
++ble_hs_test_util_verify_tx_l2cap_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;
++
++    ble_hs_test_util_tx_all();
++
++    om = ble_hs_test_util_verify_tx_l2cap_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;
++}
++
++int
++ble_hs_test_util_rx_l2cap_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_HS_TEST_UTIL_L2CAP_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;
++}
++
++void
++ble_hs_test_util_verify_tx_l2cap_update_rsp(uint8_t exp_id,
++                                            uint16_t exp_result)
++{
++    struct ble_l2cap_sig_update_rsp rsp;
++    struct os_mbuf *om;
++
++    om = ble_hs_test_util_verify_tx_l2cap_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);
++}
++
++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();
 +}



[41/50] [abbrv] incubator-mynewt-core git commit: boot - Remove unnecessary syscfg overrides.

Posted by vi...@apache.org.
boot - Remove unnecessary syscfg overrides.


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

Branch: refs/heads/sterly_refactor
Commit: 063a01b4fe681acc86a407170b2670182de9ba19
Parents: 26dfea0
Author: Christopher Collins <cc...@apache.org>
Authored: Thu Sep 22 18:03:34 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Thu Sep 22 18:03:34 2016 -0700

----------------------------------------------------------------------
 apps/boot/pkg.yml | 4 ----
 1 file changed, 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/063a01b4/apps/boot/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/boot/pkg.yml b/apps/boot/pkg.yml
index b8f8062..e2112a9 100644
--- a/apps/boot/pkg.yml
+++ b/apps/boot/pkg.yml
@@ -42,7 +42,3 @@ pkg.syscfg_defs:
     BOOT_SERIAL:
         description: 'TBD'
         value: 0
-
-pkg.syscfg_vals:
-    LOG_LEVEL: 255
-    STATS_NAMES: 0


[20/50] [abbrv] incubator-mynewt-core git commit: Merge branch 'develop' into sterly_refactor

Posted by vi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/test/src/ble_att_svr_test.c
----------------------------------------------------------------------
diff --cc net/nimble/host/test/src/ble_att_svr_test.c
index 1509b41,0000000..52a5669
mode 100644,000000..100644
--- a/net/nimble/host/test/src/ble_att_svr_test.c
+++ b/net/nimble/host/test/src/ble_att_svr_test.c
@@@ -1,2314 -1,0 +1,2274 @@@
 +/**
 + * 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_att_mtu_rsp_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,
++        ble_hs_test_util_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,
++        ble_hs_test_util_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_hs_test_util_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,
++    ble_hs_test_util_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_hs_test_util_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,
++    ble_hs_test_util_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,
++    ble_hs_test_util_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_hs_test_util_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,
++    ble_hs_test_util_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();
++    ble_hs_test_util_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,
++    ble_hs_test_util_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();
++    ble_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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_hs_test_util_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;
 +}


[23/50] [abbrv] incubator-mynewt-core git commit: Merge branch 'develop' into sterly_refactor

Posted by vi...@apache.org.
Merge branch 'develop' into sterly_refactor


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

Branch: refs/heads/sterly_refactor
Commit: c5901fcc12db4f4a689728a9a9ff74ff3d0f7d40
Parents: 0c0f3e1 12ef82c
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Sep 21 15:06:50 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Sep 21 15:06:50 2016 -0700

----------------------------------------------------------------------
 .gitignore                                      |    1 +
 apps/blecent/pkg.yml                            |    3 +-
 apps/blecent/src/main.c                         |   17 +-
 apps/blehci/src/main.c                          |    2 +-
 apps/bleprph/pkg.yml                            |    3 +-
 apps/bleprph/src/main.c                         |    8 +-
 apps/bletest/src/bletest_hci.c                  |    2 +-
 apps/bletest/src/main.c                         |   44 +-
 apps/bletiny/pkg.yml                            |    3 +-
 apps/bletiny/src/bletiny.h                      |    1 +
 apps/bletiny/src/cmd.c                          |   10 +-
 apps/bletiny/src/main.c                         |   22 +-
 apps/bletiny/src/parse.c                        |   16 +
 apps/bleuart/pkg.yml                            |    3 +-
 apps/bleuart/src/main.c                         |   19 +-
 apps/boot/pkg.yml                               |    1 -
 apps/boot/src/boot.c                            |   96 +-
 apps/ffs2native/src/main.c                      |   12 +-
 apps/ocf_sample/pkg.yml                         |   42 +
 apps/ocf_sample/src/main.c                      |  307 ++
 apps/slinky/pkg.yml                             |    2 +
 apps/slinky/src/main.c                          |   65 +-
 apps/splitty/README.md                          |   34 +
 apps/splitty/pkg.yml                            |   49 +
 apps/splitty/src/main.c                         |  375 ++
 compiler/gdbmacros/nffs.gdb                     |  116 +
 compiler/sim/compiler.yml                       |    1 -
 fs/nffs/src/nffs.c                              |   55 +-
 fs/nffs/src/nffs_block.c                        |    2 +
 fs/nffs/src/nffs_crc.c                          |    1 +
 fs/nffs/src/nffs_flash.c                        |    3 +
 fs/nffs/src/nffs_format.c                       |    1 +
 fs/nffs/src/nffs_gc.c                           |    2 +
 fs/nffs/src/nffs_hash.c                         |    4 +-
 fs/nffs/src/nffs_inode.c                        |   12 +-
 fs/nffs/src/nffs_misc.c                         |    1 +
 fs/nffs/src/nffs_priv.h                         |   34 +-
 fs/nffs/src/nffs_restore.c                      |    8 +-
 hw/bsp/arduino_primo_nrf52/pkg.yml              |    1 +
 hw/bsp/arduino_primo_nrf52/split-primo.ld       |  208 +
 .../src/arch/cortex_m4/gcc_startup_nrf52.s      |   10 +
 .../arch/cortex_m4/gcc_startup_nrf52_split.s    |  152 +
 hw/bsp/arduino_primo_nrf52/src/os_bsp.c         |   12 +-
 hw/bsp/arduino_primo_nrf52/src/sbrk.c           |   22 +-
 hw/bsp/bmd300eval/pkg.yml                       |    1 +
 hw/bsp/bmd300eval/split-bmd300eval.ld           |  208 +
 .../src/arch/cortex_m4/gcc_startup_nrf52.s      |   11 +
 .../arch/cortex_m4/gcc_startup_nrf52_split.s    |  152 +
 hw/bsp/bmd300eval/src/os_bsp.c                  |   12 +-
 hw/bsp/bmd300eval/src/sbrk.c                    |   22 +-
 hw/bsp/native/src/sbrk.c                        |    3 +-
 hw/bsp/nrf51-arduino_101/src/sbrk.c             |    4 -
 hw/bsp/nrf51-blenano/pkg.yml                    |    1 +
 hw/bsp/nrf51-blenano/split-nrf51dk.ld           |  185 +
 .../src/arch/cortex_m0/gcc_startup_nrf51.s      |    8 +
 .../arch/cortex_m0/gcc_startup_nrf51_split.s    |  168 +
 hw/bsp/nrf51-blenano/src/os_bsp.c               |   10 +-
 hw/bsp/nrf51-blenano/src/sbrk.c                 |   22 +-
 hw/bsp/nrf51dk-16kbram/pkg.yml                  |    1 +
 hw/bsp/nrf51dk-16kbram/split-nrf51dk-16kbram.ld |  185 +
 .../src/arch/cortex_m0/gcc_startup_nrf51.s      |   26 +-
 .../arch/cortex_m0/gcc_startup_nrf51_split.s    |  168 +
 hw/bsp/nrf51dk-16kbram/src/os_bsp.c             |    8 +-
 hw/bsp/nrf51dk-16kbram/src/sbrk.c               |   23 +-
 hw/bsp/nrf51dk/pkg.yml                          |    1 +
 hw/bsp/nrf51dk/split-nrf51dk.ld                 |  185 +
 .../src/arch/cortex_m0/gcc_startup_nrf51.s      |    8 +
 .../arch/cortex_m0/gcc_startup_nrf51_split.s    |  168 +
 hw/bsp/nrf51dk/src/os_bsp.c                     |    8 +-
 hw/bsp/nrf51dk/src/sbrk.c                       |   23 +-
 hw/bsp/nrf52dk/pkg.yml                          |    1 +
 hw/bsp/nrf52dk/split-nrf52dk.ld                 |  208 +
 .../src/arch/cortex_m4/gcc_startup_nrf52.s      |   10 +
 .../arch/cortex_m4/gcc_startup_nrf52_split.s    |  152 +
 hw/bsp/nrf52dk/src/os_bsp.c                     |   12 +-
 hw/bsp/nrf52dk/src/sbrk.c                       |   22 +-
 hw/bsp/nrf52pdk/src/sbrk.c                      |    3 -
 hw/bsp/nucleo-f401re/boot-nucleo-f401re.ld      |  192 +
 hw/bsp/nucleo-f401re/include/bsp/bsp.h          |   53 +
 hw/bsp/nucleo-f401re/include/bsp/bsp_sysid.h    |   36 +
 hw/bsp/nucleo-f401re/include/bsp/cmsis_nvic.h   |   29 +
 .../include/bsp/stm32f4xx_hal_conf.h            |  413 ++
 hw/bsp/nucleo-f401re/nucleo-f401re.ld           |  208 +
 hw/bsp/nucleo-f401re/nucleo-f401re_debug.sh     |   53 +
 hw/bsp/nucleo-f401re/nucleo-f401re_download.sh  |   53 +
 hw/bsp/nucleo-f401re/pkg.yml                    |   39 +
 .../src/arch/cortex_m4/startup_STM32F40x.s      |  343 ++
 hw/bsp/nucleo-f401re/src/hal_bsp.c              |   73 +
 hw/bsp/nucleo-f401re/src/libc_stubs.c           |   84 +
 hw/bsp/nucleo-f401re/src/os_bsp.c               |   82 +
 hw/bsp/nucleo-f401re/src/sbrk.c                 |   50 +
 hw/bsp/nucleo-f401re/src/system_stm32f4xx.c     |  351 ++
 .../stm32f4discovery/boot-stm32f4discovery.ld   |  202 +
 hw/bsp/stm32f4discovery/f407.cfg                |   82 +
 hw/bsp/stm32f4discovery/include/bsp/bsp.h       |   55 +
 hw/bsp/stm32f4discovery/include/bsp/bsp_sysid.h |   36 +
 .../stm32f4discovery/include/bsp/cmsis_nvic.h   |   29 +
 .../include/bsp/stm32f4xx_hal_conf.h            |  413 ++
 hw/bsp/stm32f4discovery/pkg.yml                 |   39 +
 hw/bsp/stm32f4discovery/run_from_flash.ld       |  204 +
 hw/bsp/stm32f4discovery/run_from_loader.ld      |  210 +
 hw/bsp/stm32f4discovery/run_from_sram.ld        |  202 +
 .../src/arch/cortex_m4/startup_STM32F40x.s      |  343 ++
 hw/bsp/stm32f4discovery/src/hal_bsp.c           |   77 +
 hw/bsp/stm32f4discovery/src/libc_stubs.c        |   84 +
 hw/bsp/stm32f4discovery/src/os_bsp.c            |   82 +
 hw/bsp/stm32f4discovery/src/sbrk.c              |   50 +
 hw/bsp/stm32f4discovery/src/system_stm32f4xx.c  |  351 ++
 hw/bsp/stm32f4discovery/stm32f4discovery.ld     |  213 +
 .../stm32f4discovery/stm32f4discovery_debug.sh  |   57 +
 .../stm32f4discovery_download.sh                |   54 +
 hw/hal/include/hal/hal_cputime.h                |   16 +
 hw/hal/include/hal/hal_watchdog.h               |   55 +
 hw/mcu/native/include/mcu/mcu_sim.h             |    2 +-
 hw/mcu/native/src/hal_uart.c                    |    4 +-
 hw/mcu/stm/stm32f4xx/include/mcu/stm32f401xe.h  | 4805 ++++++++++++++++++
 hw/mcu/stm/stm32f4xx/src/hal_gpio.c             |    2 +-
 hw/mcu/stm/stm32f4xx/src/hal_uart.c             |   18 +-
 libs/baselibc/include/inttypes.h                |    7 +-
 libs/baselibc/src/baselibc_test/unittests.h     |   24 +-
 libs/boot_serial/src/boot_serial.c              |    2 +-
 libs/bootutil/include/bootutil/bootutil_misc.h  |   11 +-
 libs/bootutil/include/bootutil/image.h          |   21 +-
 libs/bootutil/include/bootutil/loader.h         |   17 +-
 libs/bootutil/pkg.yml                           |    2 +
 libs/bootutil/signed_images.md                  |   18 +-
 libs/bootutil/src/bootutil_misc.c               |  381 +-
 libs/bootutil/src/bootutil_priv.h               |   53 +-
 libs/bootutil/src/image_validate.c              |   16 +-
 libs/bootutil/src/loader.c                      |  521 +-
 libs/bootutil/test/src/boot_test.c              |  215 +-
 libs/console/full/include/console/prompt.h      |   31 +
 libs/console/full/src/cons_tty.c                |    5 +
 libs/console/full/src/prompt.c                  |   40 +
 .../stub/include/console/console_prompt.h       |   35 +
 libs/imgmgr/include/imgmgr/imgmgr.h             |    2 +-
 libs/imgmgr/pkg.yml                             |    7 +-
 libs/imgmgr/src/imgmgr.c                        |   95 +-
 libs/imgmgr/src/imgmgr_boot.c                   |  123 +-
 libs/imgmgr/src/imgmgr_cli.c                    |  139 +
 libs/imgmgr/src/imgmgr_priv.h                   |    7 +-
 libs/iotivity/include/iotivity/oc_api.h         |  165 +
 libs/iotivity/include/iotivity/oc_buffer.h      |   32 +
 .../iotivity/include/iotivity/oc_client_state.h |   88 +
 libs/iotivity/include/iotivity/oc_constants.h   |   43 +
 libs/iotivity/include/iotivity/oc_core_res.h    |   52 +
 libs/iotivity/include/iotivity/oc_discovery.h   |   22 +
 libs/iotivity/include/iotivity/oc_helpers.h     |   78 +
 .../include/iotivity/oc_network_events.h        |   29 +
 libs/iotivity/include/iotivity/oc_rep.h         |  228 +
 libs/iotivity/include/iotivity/oc_ri.h          |  176 +
 libs/iotivity/include/iotivity/oc_uuid.h        |   31 +
 libs/iotivity/pkg.yml                           |   41 +
 libs/iotivity/src/api/oc_buffer.c               |  138 +
 libs/iotivity/src/api/oc_client_api.c           |  287 ++
 libs/iotivity/src/api/oc_core_res.c             |  280 +
 libs/iotivity/src/api/oc_discovery.c            |  298 ++
 libs/iotivity/src/api/oc_events.h               |   34 +
 libs/iotivity/src/api/oc_helpers.c              |  164 +
 libs/iotivity/src/api/oc_main.c                 |  119 +
 libs/iotivity/src/api/oc_network_events.c       |   57 +
 libs/iotivity/src/api/oc_rep.c                  |  301 ++
 libs/iotivity/src/api/oc_ri.c                   | 1016 ++++
 libs/iotivity/src/api/oc_server_api.c           |  291 ++
 libs/iotivity/src/api/oc_uuid.c                 |  119 +
 libs/iotivity/src/messaging/coap/coap.c         | 1186 +++++
 libs/iotivity/src/messaging/coap/coap.h         |  297 ++
 libs/iotivity/src/messaging/coap/conf.h         |   71 +
 libs/iotivity/src/messaging/coap/constants.h    |  158 +
 libs/iotivity/src/messaging/coap/engine.c       |  333 ++
 libs/iotivity/src/messaging/coap/engine.h       |   48 +
 libs/iotivity/src/messaging/coap/observe.c      |  319 ++
 libs/iotivity/src/messaging/coap/observe.h      |   80 +
 libs/iotivity/src/messaging/coap/oc_coap.h      |   39 +
 libs/iotivity/src/messaging/coap/separate.c     |  152 +
 libs/iotivity/src/messaging/coap/separate.h     |   71 +
 libs/iotivity/src/messaging/coap/transactions.c |  202 +
 libs/iotivity/src/messaging/coap/transactions.h |   75 +
 libs/iotivity/src/port/mynewt/abort.c           |   25 +
 libs/iotivity/src/port/mynewt/adaptor.c         |  223 +
 libs/iotivity/src/port/mynewt/adaptor.h         |   61 +
 libs/iotivity/src/port/mynewt/ble_adaptor.c     |   18 +
 libs/iotivity/src/port/mynewt/clock.c           |   41 +
 libs/iotivity/src/port/mynewt/config.h          |   59 +
 libs/iotivity/src/port/mynewt/ip_adaptor.c      |  315 ++
 libs/iotivity/src/port/mynewt/log.c             |   43 +
 libs/iotivity/src/port/mynewt/oc_loop.c         |   26 +
 libs/iotivity/src/port/mynewt/random.c          |   31 +
 libs/iotivity/src/port/mynewt/serial_adaptor.c  |  153 +
 libs/iotivity/src/port/mynewt/storage.c         |   38 +
 libs/iotivity/src/port/oc_assert.h              |   42 +
 libs/iotivity/src/port/oc_clock.h               |  102 +
 libs/iotivity/src/port/oc_connectivity.h        |   84 +
 libs/iotivity/src/port/oc_log.h                 |   74 +
 .../iotivity/src/port/oc_network_events_mutex.h |   26 +
 libs/iotivity/src/port/oc_random.h              |   56 +
 libs/iotivity/src/port/oc_signal_main_loop.h    |   22 +
 libs/iotivity/src/port/oc_storage.h             |   27 +
 libs/iotivity/src/security/oc_acl.c             |  412 ++
 libs/iotivity/src/security/oc_acl.h             |   63 +
 libs/iotivity/src/security/oc_cred.c            |  200 +
 libs/iotivity/src/security/oc_cred.h            |   40 +
 libs/iotivity/src/security/oc_doxm.c            |  128 +
 libs/iotivity/src/security/oc_doxm.h            |   45 +
 libs/iotivity/src/security/oc_dtls.c            |  372 ++
 libs/iotivity/src/security/oc_dtls.h            |   50 +
 libs/iotivity/src/security/oc_pstat.c           |  119 +
 libs/iotivity/src/security/oc_pstat.h           |   39 +
 libs/iotivity/src/security/oc_store.c           |  158 +
 libs/iotivity/src/security/oc_store.h           |   26 +
 libs/iotivity/src/security/oc_svr.c             |   51 +
 libs/iotivity/src/security/oc_svr.h             |   22 +
 libs/iotivity/src/util/oc_etimer.c              |  263 +
 libs/iotivity/src/util/oc_etimer.h              |  247 +
 libs/iotivity/src/util/oc_list.c                |  317 ++
 libs/iotivity/src/util/oc_list.h                |  144 +
 libs/iotivity/src/util/oc_memb.c                |  111 +
 libs/iotivity/src/util/oc_memb.h                |  127 +
 libs/iotivity/src/util/oc_mmem.c                |  154 +
 libs/iotivity/src/util/oc_mmem.h                |   53 +
 libs/iotivity/src/util/oc_process.c             |  346 ++
 libs/iotivity/src/util/oc_process.h             |  527 ++
 libs/iotivity/src/util/oc_timer.c               |  131 +
 libs/iotivity/src/util/oc_timer.h               |   88 +
 libs/iotivity/src/util/pt/lc-addrlabels.h       |   86 +
 libs/iotivity/src/util/pt/lc-switch.h           |   79 +
 libs/iotivity/src/util/pt/lc.h                  |  130 +
 libs/iotivity/src/util/pt/pt-sem.h              |  233 +
 libs/iotivity/src/util/pt/pt.h                  |  333 ++
 libs/json/src/json_decode.c                     |    3 +-
 libs/json/test/src/test_json_simple.c           |    6 +-
 libs/mbedtls/include/mbedtls/config_mynewt.h    |    2 +
 libs/mbedtls/src/x509_crl.c                     |    5 +-
 libs/newtmgr/include/newtmgr/newtmgr.h          |   14 +-
 libs/newtmgr/nmgr_os/include/nmgr_os/nmgr_os.h  |   36 +
 libs/newtmgr/nmgr_os/pkg.yml                    |   35 +
 libs/newtmgr/nmgr_os/src/newtmgr_os.c           |  326 ++
 libs/newtmgr/pkg.yml                            |    4 +
 libs/newtmgr/src/newtmgr.c                      |  109 +-
 libs/newtmgr/src/newtmgr_os.c                   |  238 -
 libs/newtmgr/src/newtmgr_priv.h                 |   31 -
 libs/newtmgr_oic/include/newtmgr/newtmgr.h      |  112 +
 libs/newtmgr_oic/pkg.yml                        |   44 +
 libs/newtmgr_oic/src/newtmgr.c                  |  500 ++
 libs/os/include/os/os_eventq.h                  |    2 +-
 libs/os/include/os/os_time.h                    |    5 +-
 libs/os/src/test/callout_test.c                 |  330 ++
 libs/os/test/src/eventq_test.c                  |    4 +-
 libs/os/test/src/mutex_test.c                   |   36 +-
 libs/os/test/src/os_test.c                      |    2 +-
 libs/os/test/src/os_test_priv.h                 |    2 +-
 libs/os/test/src/sem_test.c                     |   10 +-
 libs/shell/include/shell/shell.h                |    3 +
 libs/shell/include/shell/shell_prompt.h         |   25 +
 libs/shell/src/shell.c                          |   22 +-
 libs/shell/src/shell_prompt.c                   |   69 +
 libs/split/README.md                            |   63 +
 libs/split/include/split/split.h                |   59 +
 libs/split/include/split/split_priv.h           |   39 +
 libs/split/pkg.yml                              |   30 +
 libs/split/src/split.c                          |   74 +
 libs/split/src/split_config.c                   |   90 +
 libs/split/src/split_netmgr.c                   |  150 +
 libs/tinycbor/include/tinycbor/assert_p.h       |   29 +
 libs/tinycbor/include/tinycbor/cbor.h           |  479 ++
 .../tinycbor/include/tinycbor/cborconstants_p.h |   52 +
 libs/tinycbor/include/tinycbor/cborjson.h       |   62 +
 .../include/tinycbor/compilersupport_p.h        |  218 +
 .../include/tinycbor/extract_number_p.h         |   78 +
 libs/tinycbor/include/tinycbor/math_support_p.h |   47 +
 libs/tinycbor/pkg.yml                           |   26 +
 libs/tinycbor/src/cborencoder.c                 |  629 +++
 .../src/cborencoder_close_container_checked.c   |   82 +
 libs/tinycbor/src/cborerrorstrings.c            |  165 +
 libs/tinycbor/src/cborparser.c                  | 1293 +++++
 libs/tinycbor/src/cborparser_dup_string.c       |  113 +
 libs/tinycbor/src/cborpretty.c                  |  470 ++
 libs/tinycbor/src/cbortojson.c                  |  686 +++
 libs/tinycbor/src/open_memstream.c              |  117 +
 libs/tinycrypt/include/tinycrypt/constants.h    |    4 -
 libs/util/include/util/hex.h                    |   25 +
 libs/util/src/hex.c                             |  101 +
 libs/util/src/test/hex_test.c                   |  125 +
 libs/util/test/src/util_test.c                  |    9 +-
 libs/util/test/src/util_test_priv.h             |    1 +
 libs/wifi_mgmt/src/wifi_cli.c                   |    9 +-
 .../controller/include/controller/ble_ll.h      |   35 +-
 .../controller/include/controller/ble_ll_adv.h  |    3 +
 .../controller/include/controller/ble_ll_ctrl.h |    3 +
 .../controller/include/controller/ble_ll_hci.h  |    3 +
 .../include/controller/ble_ll_resolv.h          |    7 +-
 .../controller/include/controller/ble_ll_scan.h |    2 +-
 .../controller/include/controller/ble_phy.h     |    3 +
 net/nimble/controller/src/ble_ll.c              |  190 +-
 net/nimble/controller/src/ble_ll_adv.c          |   72 +-
 net/nimble/controller/src/ble_ll_conn.c         |  194 +-
 net/nimble/controller/src/ble_ll_conn_hci.c     |  200 +-
 net/nimble/controller/src/ble_ll_conn_priv.h    |   10 +-
 net/nimble/controller/src/ble_ll_hci.c          |    2 +
 net/nimble/controller/src/ble_ll_hci_ev.c       |   44 +
 net/nimble/controller/src/ble_ll_resolv.c       |  175 +-
 net/nimble/controller/src/ble_ll_rng.c          |   60 -
 net/nimble/controller/src/ble_ll_scan.c         |   24 +-
 net/nimble/controller/src/ble_ll_sched.c        |   14 +-
 net/nimble/drivers/native/src/ble_phy.c         |  161 +-
 net/nimble/drivers/nrf51/src/ble_phy.c          |  209 +-
 net/nimble/drivers/nrf52/src/ble_phy.c          |  205 +-
 net/nimble/host/include/host/ble_att.h          |    1 +
 net/nimble/host/include/host/ble_gap.h          |   22 +
 net/nimble/host/include/host/ble_hs_adv.h       |    3 +
 net/nimble/host/include/host/ble_l2cap.h        |    3 +-
 .../gap/include/services/gap/ble_svc_gap.h      |   39 +
 net/nimble/host/services/gap/pkg.yml            |   31 +
 net/nimble/host/services/gap/src/ble_svc_gap.c  |  167 +
 .../gatt/include/services/gatt/ble_svc_gatt.h   |   29 +
 net/nimble/host/services/gatt/pkg.yml           |   34 +
 .../host/services/gatt/src/ble_svc_gatt.c       |   90 +
 .../lls/include/services/lls/ble_svc_lls.h      |   44 +
 net/nimble/host/services/lls/pkg.yml            |   31 +
 net/nimble/host/services/lls/src/ble_svc_lls.c  |  201 +
 .../include/services/mandatory/ble_svc_gap.h    |   39 -
 .../include/services/mandatory/ble_svc_gatt.h   |   10 -
 net/nimble/host/services/mandatory/pkg.yml      |   35 -
 .../host/services/mandatory/src/ble_svc_gap.c   |  167 -
 .../host/services/mandatory/src/ble_svc_gatt.c  |   90 -
 net/nimble/host/src/ble_att.c                   |   49 +-
 net/nimble/host/src/ble_att_clt.c               |  118 +-
 net/nimble/host/src/ble_att_cmd.c               |   31 +-
 net/nimble/host/src/ble_att_cmd_priv.h          |    4 +-
 net/nimble/host/src/ble_att_priv.h              |    4 +-
 net/nimble/host/src/ble_att_svr.c               |  195 +-
 net/nimble/host/src/ble_gap.c                   |  424 +-
 net/nimble/host/src/ble_gap_priv.h              |    1 +
 net/nimble/host/src/ble_gattc.c                 |   10 +-
 net/nimble/host/src/ble_gatts.c                 |    2 +-
 net/nimble/host/src/ble_hs.c                    |   25 +-
 net/nimble/host/src/ble_hs_adv.c                |    2 -
 net/nimble/host/src/ble_hs_conn.c               |   11 +
 net/nimble/host/src/ble_hs_conn_priv.h          |    2 +-
 net/nimble/host/src/ble_hs_misc.c               |    6 +-
 net/nimble/host/src/ble_hs_priv.h               |    6 +-
 net/nimble/host/src/ble_l2cap_sig.c             |   79 +-
 net/nimble/host/src/ble_l2cap_sig_priv.h        |    1 +
 net/nimble/host/src/ble_sm.c                    |   83 +-
 net/nimble/host/src/ble_sm_cmd.c                |    9 +-
 net/nimble/host/src/ble_sm_lgcy.c               |    7 +-
 net/nimble/host/src/ble_sm_priv.h               |    8 +-
 net/nimble/host/src/ble_sm_sc.c                 |   59 +-
 net/nimble/host/src/test/ble_hs_hci_test.c      |   99 +
 net/nimble/host/test/src/ble_att_clt_test.c     |   31 +
 net/nimble/host/test/src/ble_att_svr_test.c     |  100 +-
 net/nimble/host/test/src/ble_gap_test.c         |  524 +-
 .../host/test/src/ble_gatts_notify_test.c       |   99 +-
 net/nimble/host/test/src/ble_hs_test_util.c     |  202 +-
 net/nimble/host/test/src/ble_hs_test_util.h     |   22 +-
 net/nimble/host/test/src/ble_l2cap_test.c       |  135 +-
 net/nimble/host/test/src/ble_sm_test_util.c     |   12 +-
 net/nimble/include/nimble/ble.h                 |    4 +
 net/nimble/include/nimble/hci_common.h          |    4 +
 net/nimble/transport/ram/src/ble_hci_ram.c      |   21 +-
 .../uart/include/transport/uart/ble_hci_uart.h  |   32 +-
 net/nimble/transport/uart/pkg.yml               |   16 +-
 net/nimble/transport/uart/src/ble_hci_uart.c    |  414 +-
 repository.yml                                  |   29 +
 sys/config/include/config/config.h              |    3 +-
 sys/config/src/config.c                         |   13 +-
 sys/config/src/config_nmgr.c                    |   18 +-
 sys/coredump/src/coredump.c                     |    2 +-
 sys/fcb/include/fcb/fcb.h                       |   10 +
 sys/log/include/log/log.h                       |   37 +-
 sys/log/src/log.c                               |   28 +-
 sys/log/src/log_cbmem.c                         |   33 +-
 sys/log/src/log_console.c                       |   26 +-
 sys/log/src/log_fcb.c                           |   50 +-
 sys/log/src/log_nmgr.c                          |   10 +-
 sys/log/test/src/log_test.c                     |    5 +-
 .../include/mn_socket/arch/sim/native_sock.h    |   25 +
 sys/mn_socket/include/mn_socket/mn_socket.h     |   76 +-
 sys/mn_socket/include/mn_socket/mn_socket_ops.h |    3 +
 sys/mn_socket/src/arch/sim/native_itf.c         |  212 +
 sys/mn_socket/src/arch/sim/native_sock.c        |  753 +++
 sys/mn_socket/src/arch/sim/native_sock_priv.h   |   32 +
 sys/mn_socket/src/mn_socket.c                   |   15 +
 sys/mn_socket/src/mn_socket_aconv.c             |   45 +-
 sys/mn_socket/test/src/mn_sock_test.c           |  812 +++
 sys/reboot/include/reboot/log_reboot.h          |    2 +-
 sys/reboot/src/log_reboot.c                     |   37 +-
 sys/stats/src/stats.c                           |    2 +-
 sys/stats/src/stats_nmgr.c                      |   20 +-
 389 files changed, 40937 insertions(+), 3161 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/blecent/pkg.yml
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/blecent/src/main.c
----------------------------------------------------------------------
diff --cc apps/blecent/src/main.c
index 1f9a424,c0f43a4..c9ae8a4
--- a/apps/blecent/src/main.c
+++ b/apps/blecent/src/main.c
@@@ -38,10 -42,24 +41,9 @@@
  /* 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))

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/blehci/src/main.c
----------------------------------------------------------------------
diff --cc apps/blehci/src/main.c
index 5c3f7f4,52aab99..1c75528
--- a/apps/blehci/src/main.c
+++ b/apps/blehci/src/main.c
@@@ -18,13 -18,35 +18,13 @@@
   */
  #include <assert.h>
  #include "os/os.h"
 -#include "hal/hal_cputime.h"
 -#include "hal/hal_uart.h"
 -
 -/* BLE */
 -#include "nimble/ble.h"
 -#include "nimble/hci_common.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[6] = { 0 };
+ uint8_t g_random_addr[BLE_DEV_ADDR_LEN] = { 0 };
  
 -os_membuf_t default_mbuf_mpool_data[MBUF_MEMPOOL_SIZE];
 -
 -struct os_mbuf_pool default_mbuf_pool;
 -struct os_mempool default_mbuf_mpool;
 -
  int
  main(void)
  {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/bleprph/pkg.yml
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --cc apps/bleprph/src/main.c
index 9af8006,a53cb29..6792b50
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@@ -37,12 -58,22 +37,11 @@@
  /* 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))

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/bletest/src/bletest_hci.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/bletest/src/main.c
----------------------------------------------------------------------
diff --cc apps/bletest/src/main.c
index 70b68dd,ffc0b92..88e19c9
--- a/apps/bletest/src/main.c
+++ b/apps/bletest/src/main.c
@@@ -699,12 -740,18 +711,18 @@@ 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);
-             bletest_send_ltk_req_reply(g_bletest_ltk_reply_handle);
-             g_bletest_ltk_reply_handle = 0;
+         mask = 1;
+         reply_handle = 1;
+         while (g_bletest_ltk_reply_handle && mask) {
+             if (g_bletest_ltk_reply_handle & mask) {
+                 bletest_send_ltk_req_reply(reply_handle);
+                 //bletest_send_ltk_req_neg_reply(reply_handle);
+                 g_bletest_ltk_reply_handle &= ~mask;
+             }
+             ++reply_handle;
+             mask <<= 1;
          }
  #endif
          if (g_bletest_current_conns) {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/bletiny/pkg.yml
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/bletiny/src/bletiny.h
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/bletiny/src/cmd.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/bletiny/src/main.c
----------------------------------------------------------------------
diff --cc apps/bletiny/src/main.c
index 76ec1c6,17b1ea0..c066511
--- a/apps/bletiny/src/main.c
+++ b/apps/bletiny/src/main.c
@@@ -91,10 -112,9 +91,9 @@@ struct os_eventq bletiny_evq
  struct os_task bletiny_task;
  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;
@@@ -1615,9 -1664,27 +1622,8 @@@ main(void
                           "bletiny_dsc_pool");
      assert(rc == 0);
  
-     /* Initialize the bletiny system. */
-     log_console_handler_init(&bletiny_log_console_handler);
-     log_register("bletiny", &bletiny_log, &bletiny_log_console_handler);
 -    /* 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();
+     log_register("bletiny", &bletiny_log, &log_console_handler, NULL);
  
      /* Initialize eventq for the application task. */
      os_eventq_init(&bletiny_evq);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/bleuart/pkg.yml
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/bleuart/src/main.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/boot/pkg.yml
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/boot/src/boot.c
----------------------------------------------------------------------
diff --cc apps/boot/src/boot.c
index 49d4083,fb2b6b8..fa0ba84
--- a/apps/boot/src/boot.c
+++ b/apps/boot/src/boot.c
@@@ -28,16 -28,7 +29,7 @@@
  #include <hal/hal_flash.h>
  #include <config/config.h>
  #include <config/config_file.h>
- #if MYNEWT_VAL(BOOT_NFFS)
- #include <fs/fs.h>
- #include <nffs/nffs.h>
- #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
 +#if MYNEWT_VAL(BOOT_SERIAL)
  #include <hal/hal_gpio.h>
  #include <boot_serial/boot_serial.h>
  #endif
@@@ -45,8 -37,8 +38,6 @@@
  #include "bootutil/loader.h"
  #include "bootutil/bootutil_misc.h"
  
--/* we currently need extra nffs_area_descriptors for booting since the
-- * boot code uses these to keep track of which block to write and copy.*/
  #define BOOT_AREA_DESC_MAX  (256)
  #define AREA_DESC_MAX       (BOOT_AREA_DESC_MAX)
  
@@@ -117,37 -62,25 +61,22 @@@ main(void
          .br_slot_areas = img_starts,
      };
  
-     os_init();
+     struct boot_rsp rsp;
+     int rc;
  
-     cnt = BOOT_AREA_DESC_MAX;
-     rc = flash_area_to_sectors(FLASH_AREA_IMAGE_0, &cnt, descs);
-     img_starts[0] = 0;
-     total = cnt;
 -
 -#ifdef BOOT_SERIAL
++#if MYNEWT_VAL(BOOT_SERIAL)
+     os_init();
+ #else
+     bsp_init();
+ #endif
  
-     cnt = BOOT_AREA_DESC_MAX - total;
-     assert(cnt >= 0);
-     rc = flash_area_to_sectors(FLASH_AREA_IMAGE_1, &cnt, &descs[total]);
+     rc = hal_flash_init();
      assert(rc == 0);
-     img_starts[1] = total;
-     total += cnt;
  
-     cnt = BOOT_AREA_DESC_MAX - total;
-     assert(cnt >= 0);
-     rc = flash_area_to_sectors(FLASH_AREA_IMAGE_SCRATCH, &cnt, &descs[total]);
+     rc = boot_build_request(&req, AREA_DESC_MAX);
      assert(rc == 0);
-     req.br_scratch_area_idx = total;
-     total += cnt;
- 
-     req.br_num_image_areas = total;
- 
- #if MYNEWT_VAL(BOOT_NFFS)
-     setup_for_nffs();
- #elif MYNEWT_VAL(BOOT_FCB)
-     setup_for_fcb();
- #endif
-     bootutil_cfg_register();
 -    
 -    conf_init();
  
 -#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.
@@@ -167,4 -102,4 +98,3 @@@
  
      return 0;
  }
--

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/ffs2native/src/main.c
----------------------------------------------------------------------
diff --cc apps/ffs2native/src/main.c
index 90bb3fb,6558f0d..50a8b27
--- a/apps/ffs2native/src/main.c
+++ b/apps/ffs2native/src/main.c
@@@ -666,9 -671,19 +671,8 @@@ 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);
  
-     log_console_handler_init(&nffs_log_console_handler);
-     log_register("nffs-log", &nffs_log, &nffs_log_console_handler);
 -    rc = nffs_init();
 -    assert(rc == 0);
 -
 -    log_init();
+     log_register("nffs-log", &nffs_log, &log_console_handler, NULL);
  
      file_scratch_idx = MAX_AREAS + 1;
  

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/slinky/pkg.yml
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/apps/slinky/src/main.c
----------------------------------------------------------------------
diff --cc apps/slinky/src/main.c
index 257a43e,5d607cf..02a5d34
--- a/apps/slinky/src/main.c
+++ b/apps/slinky/src/main.c
@@@ -29,7 -27,10 +29,10 @@@
  #include <config/config.h>
  #include <hal/flash_map.h>
  #include <hal/hal_system.h>
- #if MYNEWT_PKG_FS_FS
+ #if defined SPLIT_LOADER || defined SPLIT_APPLICATION
+ #include <split/split.h>
+ #endif
 -#ifdef NFFS_PRESENT
++#if MYNEWT_PKG_FS_NFFS
  #include <fs/fs.h>
  #include <nffs/nffs.h>
  #include <config/config_file.h>
@@@ -70,11 -70,17 +72,10 @@@ static volatile int g_task1_loops
  /* Task 2 */
  #define TASK2_PRIO (9)
  #define TASK2_STACK_SIZE    OS_STACK_ALIGN(128)
- struct os_task task2;
- os_stack_t stack2[TASK2_STACK_SIZE];
+ static struct os_task task2;
  
- struct log_handler log_cbmem_handler;
- struct log my_log;
 -#define SHELL_TASK_PRIO (3)
 -#define SHELL_MAX_INPUT_LEN     (256)
 -#define SHELL_TASK_STACK_SIZE (OS_STACK_ALIGN(384))
 -
 -#define NEWTMGR_TASK_PRIO (4)
 -#define NEWTMGR_TASK_STACK_SIZE (OS_STACK_ALIGN(896))
 -
+ static struct log my_log;
+ extern struct log nffs_log; /* defined in the OS module */
  
  static volatile int g_task2_loops;
  
@@@ -294,11 -373,32 +307,12 @@@ main(int argc, char **argv
      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);
+     cbmem_init(&nffs_cbmem, nffs_cbmem_buf, MAX_CBMEM_BUF);
+     log_register("log", &my_log, &log_cbmem_handler, &cbmem);
+     log_register("nffs", &nffs_log, &log_cbmem_handler, &nffs_cbmem);
  
 -    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
  
@@@ -310,6 -426,12 +324,10 @@@
  
      flash_test_init();
  
 -    reboot_init_handler(LOG_STORE_FCB, 11);
 -
+ #if defined SPLIT_LOADER || defined SPLIT_APPLICATION
+     split_app_init();
+ #endif
+ 
      conf_load();
  
      log_reboot(HARD_REBOOT);
@@@ -323,4 -455,4 +351,3 @@@
  
      return rc;
  }
--

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/fs/nffs/src/nffs.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/fs/nffs/src/nffs_misc.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/fs/nffs/src/nffs_priv.h
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/fs/nffs/src/nffs_restore.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/arduino_primo_nrf52/pkg.yml
----------------------------------------------------------------------
diff --cc hw/bsp/arduino_primo_nrf52/pkg.yml
index a2210d4,ea6aa29..724468b
--- a/hw/bsp/arduino_primo_nrf52/pkg.yml
+++ b/hw/bsp/arduino_primo_nrf52/pkg.yml
@@@ -30,14 -30,11 +30,15 @@@ pkg.keywords
  pkg.arch: cortex_m4
  pkg.compiler: compiler/arm-none-eabi-m4
  pkg.linkerscript: "primo.ld"
 -pkg.linkerscript.bootloader.OVERWRITE: "boot-primo.ld"
 +pkg.linkerscript.BOOT_LOADER.OVERWRITE: "boot-primo.ld"
+ pkg.part2linkerscript: "split-primo.ld"
  pkg.downloadscript: primo_download.sh
  pkg.debugscript: primo_debug.sh
 -pkg.cflags: -DNRF52
 +pkg.cflags:
 +    # Nordic SDK files require these defines.
 +    - '-DNRF52'
 +    - '-DSPI_MASTER_0_ENABLE'
 +
  pkg.deps:
      - hw/mcu/nordic/nrf52xxx
      - libs/baselibc

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/arduino_primo_nrf52/src/os_bsp.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/bmd300eval/pkg.yml
----------------------------------------------------------------------
diff --cc hw/bsp/bmd300eval/pkg.yml
index b363ce1,9b906ca..cd1af60
--- a/hw/bsp/bmd300eval/pkg.yml
+++ b/hw/bsp/bmd300eval/pkg.yml
@@@ -29,14 -29,11 +29,15 @@@ pkg.keywords
  pkg.arch: cortex_m4
  pkg.compiler: compiler/arm-none-eabi-m4
  pkg.linkerscript: "bmd300eval.ld"
 -pkg.linkerscript.bootloader.OVERWRITE: "boot-bmd300eval.ld"
 +pkg.linkerscript.BOOT_LOADER.OVERWRITE: "boot-bmd300eval.ld"
+ pkg.part2linkerscript: "split-bmd300eval.ld"
  pkg.downloadscript: bmd300eval_download.sh
  pkg.debugscript: bmd300eval_debug.sh
 -pkg.cflags: -DNRF52 -DBSP_HAS_32768_XTAL
 +pkg.cflags:
 +    # Nordic SDK files require these defines.
 +    - '-DNRF52'
 +    - '-DSPI_MASTER_0_ENABLE'
 +
  pkg.deps:
      - hw/mcu/nordic/nrf52xxx
      - libs/baselibc

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/bmd300eval/src/os_bsp.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/nrf51-blenano/pkg.yml
----------------------------------------------------------------------
diff --cc hw/bsp/nrf51-blenano/pkg.yml
index 8eb7ba5,6b45854..494d265
--- a/hw/bsp/nrf51-blenano/pkg.yml
+++ b/hw/bsp/nrf51-blenano/pkg.yml
@@@ -30,13 -30,11 +30,14 @@@ pkg.keywords
  pkg.arch: cortex_m0
  pkg.compiler: compiler/arm-none-eabi-m0
  pkg.linkerscript: "nrf51dk.ld"
 -pkg.linkerscript.bootloader.OVERWRITE: "boot-nrf51dk.ld"
 +pkg.linkerscript.BOOT_LOADER.OVERWRITE: "boot-nrf51dk.ld"
+ pkg.part2linkerscript: "split-nrf51dk.ld"
  pkg.downloadscript: nrf51dk_download.sh
  pkg.debugscript: nrf51dk_debug.sh
 -pkg.cflags: -DNRF51
 +pkg.cflags:
 +    # Nordic SDK files require these defines.
 +    - '-DNRF51'
 +
  pkg.deps:
      - hw/mcu/nordic/nrf51xxx
      - libs/baselibc

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/nrf51-blenano/src/os_bsp.c
----------------------------------------------------------------------
diff --cc hw/bsp/nrf51-blenano/src/os_bsp.c
index d64c265,1dd867b..b17be8f
--- a/hw/bsp/nrf51-blenano/src/os_bsp.c
+++ b/hw/bsp/nrf51-blenano/src/os_bsp.c
@@@ -84,12 -70,8 +90,12 @@@ void bsp_slot_init_split_application(vo
  }
  
  void
- os_bsp_init(void)
+ bsp_init(void)
  {
 +    int rc;
 +
 +    (void)rc;
 +
      /*
       * XXX this reference is here to keep this function in.
       */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/nrf51dk-16kbram/pkg.yml
----------------------------------------------------------------------
diff --cc hw/bsp/nrf51dk-16kbram/pkg.yml
index 07506db,a240551..6b65dfb
--- a/hw/bsp/nrf51dk-16kbram/pkg.yml
+++ b/hw/bsp/nrf51dk-16kbram/pkg.yml
@@@ -29,13 -29,11 +29,14 @@@ pkg.keywords
  pkg.arch: cortex_m0
  pkg.compiler: compiler/arm-none-eabi-m0
  pkg.linkerscript: "nrf51dk-16kbram.ld"
 -pkg.linkerscript.bootloader.OVERWRITE: "boot-nrf51dk-16kbram.ld"
 +pkg.linkerscript.BOOT_LOADER.OVERWRITE: "boot-nrf51dk-16kbram.ld"
+ pkg.part2linkerscript: "split-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

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/nrf51dk-16kbram/src/os_bsp.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/nrf51dk/pkg.yml
----------------------------------------------------------------------
diff --cc hw/bsp/nrf51dk/pkg.yml
index a6c12fa,56de75a..beee6fc
--- a/hw/bsp/nrf51dk/pkg.yml
+++ b/hw/bsp/nrf51dk/pkg.yml
@@@ -29,13 -29,11 +29,14 @@@ pkg.keywords
  pkg.arch: cortex_m0
  pkg.compiler: compiler/arm-none-eabi-m0
  pkg.linkerscript: "nrf51dk.ld"
 -pkg.linkerscript.bootloader.OVERWRITE: "boot-nrf51dk.ld"
 +pkg.linkerscript.BOOT_LOADER.OVERWRITE: "boot-nrf51dk.ld"
+ pkg.part2linkerscript: "split-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

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/nrf51dk/src/arch/cortex_m0/gcc_startup_nrf51.s
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/nrf51dk/src/os_bsp.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/nrf52dk/pkg.yml
----------------------------------------------------------------------
diff --cc hw/bsp/nrf52dk/pkg.yml
index 9c1ded3,b65d9e2..e80dea9
--- a/hw/bsp/nrf52dk/pkg.yml
+++ b/hw/bsp/nrf52dk/pkg.yml
@@@ -29,14 -29,11 +29,15 @@@ pkg.keywords
  pkg.arch: cortex_m4
  pkg.compiler: compiler/arm-none-eabi-m4
  pkg.linkerscript: "nrf52dk.ld"
 -pkg.linkerscript.bootloader.OVERWRITE: "boot-nrf52dk.ld"
 +pkg.linkerscript.BOOT_LOADER.OVERWRITE: "boot-nrf52dk.ld"
+ pkg.part2linkerscript: "split-nrf52dk.ld"
  pkg.downloadscript: nrf52dk_download.sh
  pkg.debugscript: nrf52dk_debug.sh
 -pkg.cflags: -DNRF52 -DBSP_HAS_32768_XTAL
 +pkg.cflags:
 +    # Nordic SDK files require these defines.
 +    - '-DNRF52'
 +    - '-DSPI_MASTER_0_ENABLE'
 +
  pkg.deps:
      - hw/mcu/nordic/nrf52xxx
      - libs/baselibc

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/bsp/nrf52dk/src/os_bsp.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/mcu/stm/stm32f4xx/src/hal_gpio.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/hw/mcu/stm/stm32f4xx/src/hal_uart.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/bootutil/pkg.yml
----------------------------------------------------------------------
diff --cc libs/bootutil/pkg.yml
index f9d2f70,f708f52..4d78647
--- a/libs/bootutil/pkg.yml
+++ b/libs/bootutil/pkg.yml
@@@ -26,24 -26,11 +26,26 @@@ pkg.keywords
      - bootloader
  
  pkg.deps: 
 -    - libs/os 
 -    - libs/testutil
 -    - libs/mbedtls
      - hw/hal
 +    - libs/mbedtls
 +    - libs/os 
      - sys/config
  
 +pkg.deps.BOOTUTIL_NFFS:
 +    - fs/nffs
 +
 +pkg.init_function: bootutil_pkg_init
 +pkg.init_stage: 5
 +
 +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'
+ pkg.cflags.IMAGE_KEYS_RSA: -DIMAGE_SIGNATURES_RSA
+ pkg.cflags.IMAGE_KEYS_EC: -DIMAGE_SIGNATURES_EC

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/bootutil/src/bootutil_misc.c
----------------------------------------------------------------------
diff --cc libs/bootutil/src/bootutil_misc.c
index 17052fe,55dc7ec..0f9a2ce
--- a/libs/bootutil/src/bootutil_misc.c
+++ b/libs/bootutil/src/bootutil_misc.c
@@@ -19,90 -19,35 +19,35 @@@
  
  #include <string.h>
  #include <inttypes.h>
 -#include <assert.h>
 -#include <hal/hal_flash.h>
 -#include <hal/flash_map.h>
 -#include <hal/hal_bsp.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/loader.h"
+ #include "bootutil/bootutil_misc.h"
  #include "bootutil_priv.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;
- #if !MYNEWT_VAL(BOOTUTIL_NFFS)
- static struct boot_status boot_saved;
- #endif
- 
- static struct conf_handler boot_conf_handler = {
-     .ch_name = "boot",
-     .ch_get = NULL,
-     .ch_set = boot_conf_set,
-     .ch_commit = NULL,
-     .ch_export = NULL,
- };
- 
+ /*
+  * Read the image trailer from a given slot.
+  */
  static int
- boot_conf_set(int argc, char **argv, char *val)
+ boot_vect_read_img_trailer(int slot, struct boot_img_trailer *bit)
  {
      int rc;
-     int len;
- 
-     if (argc == 1) {
-         if (!strcmp(argv[0], "main")) {
-             len = sizeof(boot_main);
-             if (val) {
-                 rc = conf_bytes_from_str(val, &boot_main, &len);
-             } else {
-                 memset(&boot_main, 0, len);
-                 rc = 0;
-             }
-         } else if (!strcmp(argv[0], "test")) {
-             len = sizeof(boot_test);
-             if (val) {
-                 rc = conf_bytes_from_str(val, &boot_test, &len);
-             } else {
-                 memset(&boot_test, 0, len);
-                 rc = 0;
-             }
- #if !MYNEWT_VAL(BOOTUTIL_NFFS)
-         } else if (!strcmp(argv[0], "status")) {
-             if (!val) {
-                 boot_saved.state = 0;
-                 rc = 0;
-             } else {
-                 rc = conf_value_from_str(val, CONF_INT32,
-                   &boot_saved.state, sizeof(boot_saved.state));
-             }
-         } else if (!strcmp(argv[0], "len")) {
-             conf_value_from_str(val, CONF_INT32, &boot_saved.length,
-               sizeof(boot_saved.length));
-             rc = 0;
- #endif
-         } else {
-             rc = OS_ENOENT;
-         }
-     } else {
-         rc = OS_ENOENT;
-     }
-     return rc;
- }
+     const struct flash_area *fap;
+     uint32_t off;
  
- static int
- boot_vect_read_one(struct image_version *dst, struct image_version *src)
- {
-     if (src->iv_major == 0 && src->iv_minor == 0 &&
-       src->iv_revision == 0 && src->iv_build_num == 0) {
-         return BOOT_EBADVECT;
+     rc = flash_area_open(slot, &fap);
+     if (rc) {
+         return rc;
      }
-     memcpy(dst, src, sizeof(*dst));
-     return 0;
+     off = fap->fa_size - sizeof(struct boot_img_trailer);
+     rc = flash_area_read(fap, off, bit, sizeof(*bit));
+     flash_area_close(fap);
+ 
+     return rc;
  }
  
  /**

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/bootutil/src/image_validate.c
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/libs/bootutil/src/loader.c
----------------------------------------------------------------------
diff --cc libs/bootutil/src/loader.c
index 6d08b9a,e2d4cfa..c8f419d
--- a/libs/bootutil/src/loader.c
+++ b/libs/bootutil/src/loader.c
@@@ -422,86 -572,81 +572,87 @@@ boot_go(const struct boot_req *req, str
          }
      }
  
-     /* Cache the flash address of each image slot. */
-     for (i = 0; i < BOOT_NUM_SLOTS; i++) {
-         boot_slot_addr(i, &image_addrs[i].bil_flash_id,
-                        &image_addrs[i].bil_address);
-     }
- 
-     /* Attempt to read an image header from each slot. */
-     boot_read_image_headers(boot_img_hdrs, image_addrs, BOOT_NUM_SLOTS);
- 
-     /* Build a boot status structure indicating the flash location of each
-      * image part.  This structure will need to be used if an image copy
-      * operation is required.
+     /*
+      * Check if we should initiate copy, or revert back to earlier image.
+      *
       */
-     boot_build_status();
- 
-     /* Determine which image the user wants to run, and where it is located. */
      slot = boot_select_image_slot();
      if (slot == -1) {
-         /* Either there is no image vector, or none of the requested images are
-          * present.  Just try booting from the first image slot.
-          */
-         if (boot_img_hdrs[0].ih_magic != IMAGE_MAGIC_NONE) {
-             slot = 0;
-         } else if (boot_img_hdrs[1].ih_magic != IMAGE_MAGIC_NONE) {
-             slot = 1;
-         } else {
-             /* No images present. */
-             return BOOT_EBADIMAGE;
-         }
+         return BOOT_EBADIMAGE;
      }
  
-     /*
-      * If the selected image fails integrity check, try the other one.
-      */
-     if (boot_image_check(&boot_img_hdrs[slot], &image_addrs[slot])) {
-         slot ^= 1;
-         if (boot_image_check(&boot_img_hdrs[slot], &image_addrs[slot])) {
-             return BOOT_EBADIMAGE;
-         }
-     }
-     switch (slot) {
-     case 0:
-         rsp->br_hdr = &boot_img_hdrs[0];
-         break;
- 
-     case 1:
-         /* The user wants to run the image in the secondary slot.  The contents
-          * of this slot need to moved to the primary slot.
-          */
+     if (slot) {
+         boot_state.idx = 0;
+         boot_state.state = 0;
          rc = boot_copy_image();
-         if (rc != 0) {
-             /* We failed to put the images back together; there is really no
-              * solution here.
-              */
+         if (rc) {
              return rc;
          }
+     }
+ 
+     /* Always boot from the primary slot. */
+     rsp->br_flash_id = boot_img[0].loc.bil_flash_id;
+     rsp->br_image_addr = boot_img[0].loc.bil_address;
+     rsp->br_hdr = &boot_img[slot].hdr;
+ 
+     return 0;
+ }
+ 
+ #define SPLIT_AREA_DESC_MAX     (255)
+ 
+ int
+ split_go(int loader_slot, int split_slot, void **entry)
+ {
+     int rc;
+     /** Areas representing the beginning of image slots. */
+     uint8_t img_starts[2];
+     struct flash_area *descs;
+     uint32_t entry_val;
+     struct boot_req req = {
+         .br_slot_areas = img_starts,
+     };
+ 
+     descs = calloc(SPLIT_AREA_DESC_MAX, sizeof(struct flash_area));
+     if (descs == NULL) {
+         return SPLIT_GO_ERR;
+     }
  
-         rsp->br_hdr = &boot_img_hdrs[1];
-         break;
+     req.br_area_descs = descs;
  
-     default:
-         assert(0);
-         break;
+     rc = boot_build_request(&req, SPLIT_AREA_DESC_MAX);
+     if (rc != 0) {
+         rc = SPLIT_GO_ERR;
+         goto split_app_go_end;
      }
  
-     /* Always boot from the primary slot. */
-     rsp->br_flash_id = image_addrs[0].bil_flash_id;
-     rsp->br_image_addr = image_addrs[0].bil_address;
+     boot_req = &req;
  
-     /* After successful boot, there should not be a status file. */
-     boot_clear_status();
+     boot_image_info();
  
-     /* If an image is being tested, it should only be booted into once. */
-     boot_vect_write_test(NULL);
+     /* Don't check the bootable image flag because we could really
+       * call a bootable or non-bootable image.  Just validate that
+       * the image check passes which is distinct from the normal check */
+     rc = split_image_check(&boot_img[split_slot].hdr,
+                            &boot_img[split_slot].loc,
+                            &boot_img[loader_slot].hdr,
+                            &boot_img[loader_slot].loc);
+     if (rc != 0) {
+         rc = SPLIT_GO_NON_MATCHING;
+         goto split_app_go_end;
+     }
  
-     return 0;
+     entry_val = (uint32_t) boot_img[split_slot].loc.bil_address +
+                          (uint32_t)  boot_img[split_slot].hdr.ih_hdr_size;
+     *entry = (void*) entry_val;
+     rc = SPLIT_GO_OK;
+ 
+ split_app_go_end:
+     free(descs);
+     return rc;
  }
 +
 +void
 +bootutil_pkg_init(void)
 +{
 +    bootutil_cfg_register();
 +}


[45/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-391 STM32F4 ADC driver and ADC HAL support

Posted by vi...@apache.org.
MYNEWT-391 STM32F4 ADC driver and ADC HAL support

- Cleaning up and correcting the dual buf mechanism


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

Branch: refs/heads/sterly_refactor
Commit: 28814b8d608a8e2a92a4284220b2c84db4dec9c8
Parents: 82767ae
Author: Vipul Rahane <vi...@runtime.io>
Authored: Wed Sep 21 16:12:20 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Thu Sep 22 20:21:03 2016 -0700

----------------------------------------------------------------------
 apps/sblinky/src/main.c                         |  29 ++--
 .../include/adc_stm32f4/adc_stm32f4.h           |   3 +
 drivers/adc/adc_stm32f4/src/adc_stm32f4.c       | 131 ++++++++------
 drivers/adc/include/adc/adc.h                   |   3 -
 hw/bsp/olimex_stm32-e407_devboard/pkg.yml       |   2 +-
 hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c  | 173 +++++++++++++------
 6 files changed, 221 insertions(+), 120 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28814b8d/apps/sblinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/sblinky/src/main.c b/apps/sblinky/src/main.c
index 280f410..4277b70 100755
--- a/apps/sblinky/src/main.c
+++ b/apps/sblinky/src/main.c
@@ -240,14 +240,14 @@ saadc_test(void)
 
 #if MYNEWT_VAL(ADC_3)
 
-#define STM32F4_ADC_DEFAULT_CHAN_CFG {\
+#define STM32F4_ADC3_DEFAULT_CHAN_CFG {\
     .Channel = ADC_CHANNEL_4,\
     .Rank = 1,\
     .SamplingTime = ADC_SAMPLETIME_144CYCLES,\
     .Offset = 0\
 }
 
-ADC_ChannelConfTypeDef adc_chan_cfg = STM32F4_ADC_DEFAULT_CHAN_CFG;
+ADC_ChannelConfTypeDef adc3_chan_cfg = STM32F4_ADC3_DEFAULT_CHAN_CFG;
 
 uint8_t *sample_buffer1;
 uint8_t *sample_buffer2;
@@ -280,14 +280,14 @@ err:
 
 #if MYNEWT_VAL(ADC_1)
 
-#define STM32F4_ADC_DEFAULT_CHAN_CFG10 {\
+#define STM32F4_ADC1_DEFAULT_CHAN_CFG {\
     .Channel = ADC_CHANNEL_10,\
     .Rank = 1,\
     .SamplingTime = ADC_SAMPLETIME_144CYCLES,\
     .Offset = 0\
 }
 
-ADC_ChannelConfTypeDef adc_chan_cfg10 = STM32F4_ADC_DEFAULT_CHAN_CFG10;
+ADC_ChannelConfTypeDef adc1_chan_cfg = STM32F4_ADC1_DEFAULT_CHAN_CFG;
 
 int adc1_result;
 int my_result_mv1[ADC_NUMBER_SAMPLES];
@@ -363,10 +363,10 @@ task1_handler(void *arg)
 
 #ifdef STM32F4
 #if MYNEWT_VAL(ADC_3)
-    adc_chan_config(adc3, ADC_CHANNEL_4, &adc_chan_cfg);
+    adc_chan_config(adc3, ADC_CHANNEL_4, &adc3_chan_cfg);
 #endif
 #if MYNEWT_VAL(ADC_1)
-    adc_chan_config(adc1, ADC_CHANNEL_10, &adc_chan_cfg10);
+    adc_chan_config(adc1, ADC_CHANNEL_10, &adc1_chan_cfg);
 #endif
 #endif
 
@@ -468,15 +468,16 @@ task1_handler(void *arg)
         int rc;
         rc = OS_OK;
 
-#if MYNEWT_VAL(ADC_3)
-        rc = adc_sample(adc3);
-        assert(rc == OS_OK);
-#endif
 
 #if MYNEWT_VAL(ADC_1)
         rc = adc_sample(adc1);
         assert(rc == OS_OK);
 #endif
+
+#if MYNEWT_VAL(ADC_3)
+        rc = adc_sample(adc3);
+        assert(rc == OS_OK);
+#endif
         ++g_task1_loops;
 
 #if MYNEWT_VAL(SPI_MASTER)
@@ -505,13 +506,15 @@ task1_handler(void *arg)
         /* Release semaphore to task 2 */
         os_sem_release(&g_test_sem);
     }
-#if MYNEWT_VAL(ADC_3)
-    os_dev_close((struct os_dev *) adc3);
-#endif
 
 #if MYNEWT_VAL(ADC_1)
     os_dev_close((struct os_dev *) adc1);
 #endif
+
+#if MYNEWT_VAL(ADC_3)
+    os_dev_close((struct os_dev *) adc3);
+#endif
+
 }
 
 void

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28814b8d/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h b/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h
index ad5bc86..31f0bb4 100644
--- a/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h
+++ b/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h
@@ -51,6 +51,9 @@
 struct stm32f4_adc_dev_cfg {
     uint8_t sac_chan_count;
     void *sac_chans;
+    void *primarybuf;
+    void *secondarybuf;
+    int buflen;
     ADC_HandleTypeDef *sac_adc_handle;
 };
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28814b8d/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/src/adc_stm32f4.c b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
index 029c267..d95c4ea 100644
--- a/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
+++ b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
@@ -39,10 +39,13 @@ static struct adc_dev *adc_dma[5];
 
 struct stm32f4_adc_stats {
     uint16_t adc_events;
+    uint16_t adc_error;
     uint16_t adc_DMA_xfer_failed;
     uint16_t adc_DMA_xfer_aborted;
     uint16_t adc_DMA_xfer_complete;
-    uint16_t adc_error;
+    uint16_t adc_DMA_start_error;
+    uint16_t adc_DMA_overrun;
+    uint16_t adc_internal_error;
 };
 
 static struct stm32f4_adc_stats stm32f4_adc_stats;
@@ -266,32 +269,37 @@ stm32f4_resolve_dma_handle_idx(DMA_HandleTypeDef *hdma)
     return ((stream_addr & 0xFF) - ((uintptr_t)DMA2_Stream0_BASE & 0xFF))/0x18;
 }
 
-static void
-stm32f4_xfer_error_cb(DMA_HandleTypeDef *hdma)
-{
-    /* DMA transfer error callback */
-    stm32f4_adc_stats.adc_DMA_xfer_failed++;
-}
-
-static void
-stm32f4_xfer_abort_cb(DMA_HandleTypeDef *hdma)
-{
-    /* DMA transfer Abort callback */
-    stm32f4_adc_stats.adc_DMA_xfer_aborted++;
-}
-
 void
 HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
 {
-    stm32f4_adc_stats.adc_error++;
+    ++stm32f4_adc_stats.adc_error;
+
+    if (hadc->ErrorCode & HAL_ADC_ERROR_DMA) {
+        /* DMA transfer error */
+        ++stm32f4_adc_stats.adc_DMA_xfer_failed;
+    } else if (hadc->ErrorCode & HAL_ADC_ERROR_OVR) {
+        /* DMA transfer overrun */
+        ++stm32f4_adc_stats.adc_DMA_overrun;
+    } else if (hadc->ErrorCode & HAL_ADC_ERROR_INTERNAL) {
+       /* ADC IP Internal Error */
+        ++stm32f4_adc_stats.adc_internal_error;
+    }
 }
 
+/**
+ * Callback that gets called by the HAL when ADC conversion is complete and
+ * the DMA buffer is full. If a secondary buffer exists it will the buffers.
+ *
+ * @param ADC Handle
+ */
 void
 HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
 {
     int rc;
     struct adc_dev *adc;
     DMA_HandleTypeDef *hdma;
+    struct stm32f4_adc_dev_cfg *cfg;
+    void *buf;
 
     assert(hadc);
     hdma = hadc->DMA_Handle;
@@ -299,16 +307,35 @@ HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
     ++stm32f4_adc_stats.adc_DMA_xfer_complete;
 
     adc = adc_dma[stm32f4_resolve_dma_handle_idx(hdma)];
+    cfg  = adc->adc_dev_cfg;
+
+    buf = cfg->primarybuf;
+    /**
+     * If primary buffer gets full and secondary buffer exists, swap the
+     * buffers and start ADC conversion with DMA with the now primary
+     * buffer(former secondary buffer)
+     * If the secondary buffer(former primary buffer) doesn't get processed
+     * by the application in sampling period required for the primary/secondary buffer
+     * i,e; (sample itvl * ADC_NUMBER_SAMPLES), the buffers would get swapped resulting
+     * in new sample data.
+     */
+    if (cfg->secondarybuf) {
+        cfg->primarybuf = cfg->secondarybuf;
+        cfg->secondarybuf = buf;
+
+        if (HAL_ADC_Start_DMA(hadc, cfg->primarybuf, cfg->buflen) != HAL_OK) {
+            ++stm32f4_adc_stats.adc_DMA_start_error;
+        }
+    }
+
+    rc = adc->ad_event_handler_func(adc, NULL, ADC_EVENT_RESULT, buf,
+                                    cfg->buflen);
 
-    rc = adc->ad_event_handler_func(adc, NULL, ADC_EVENT_RESULT, adc->primarybuf,
-                                    adc->buflen);
     if (rc) {
         ++stm32f4_adc_stats.adc_error;
     }
 }
 
-
-//void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
 static void
 stm32f4_adc_dma_init(ADC_HandleTypeDef* hadc)
 {
@@ -324,16 +351,6 @@ stm32f4_adc_dma_init(ADC_HandleTypeDef* hadc)
     HAL_DMA_Init(hdma);
     dma_handle[stm32f4_resolve_dma_handle_idx(hdma)] = hdma;
 
-    if (HAL_DMA_RegisterCallback(hdma, HAL_DMA_XFER_ERROR_CB_ID,
-                                 stm32f4_xfer_error_cb) != HAL_OK) {
-        assert(0);
-    }
-
-    if (HAL_DMA_RegisterCallback(hdma, HAL_DMA_XFER_ABORT_CB_ID,
-                                 stm32f4_xfer_abort_cb) != HAL_OK) {
-        assert(0);
-    }
-
     NVIC_SetPriority(stm32f4_resolve_adc_dma_irq(hdma),
                      NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
     NVIC_SetVector(stm32f4_resolve_adc_dma_irq(hdma),
@@ -528,16 +545,19 @@ static int
 stm32f4_adc_set_buffer(struct adc_dev *dev, void *buf1, void *buf2,
         int buflen)
 {
+    struct stm32f4_adc_dev_cfg *cfg;
     int rc;
 
+
+    assert(dev != NULL && buf1 != NULL);
     rc = OS_OK;
     buflen /= sizeof(uint16_t);
 
-    assert(dev != NULL && buf1 != NULL);
+    cfg  = dev->adc_dev_cfg;
 
-    dev->primarybuf = buf1;
-    dev->secondarybuf = buf2;
-    dev->buflen = buflen;
+    cfg->primarybuf = buf1;
+    cfg->secondarybuf = buf2;
+    cfg->buflen = buflen;
 
     return rc;
 }
@@ -554,18 +574,14 @@ stm32f4_adc_release_buffer(struct adc_dev *dev, void *buf, int buf_len)
 
     HAL_ADC_Stop_DMA(hadc);
 
-    if (dev->primarybuf == buf) {
-        if (dev->secondarybuf) {
-            dev->primarybuf = dev->secondarybuf;
-            dev->secondarybuf = buf;
-        }
-    }
-
     return (0);
 }
 
 /**
  * Trigger an ADC sample.
+ *
+ * @param ADC device structure
+ * @return OS_OK on success, non OS_OK on failure
  */
 static int
 stm32f4_adc_sample(struct adc_dev *dev)
@@ -574,13 +590,14 @@ stm32f4_adc_sample(struct adc_dev *dev)
     ADC_HandleTypeDef *hadc;
     struct stm32f4_adc_dev_cfg *cfg;
 
-    assert(dev != NULL && dev->primarybuf != NULL);
+    assert(dev);
     cfg  = dev->adc_dev_cfg;
     hadc = cfg->sac_adc_handle;
 
     rc = OS_EINVAL;
 
-    if (HAL_ADC_Start_DMA(hadc, dev->primarybuf, dev->buflen) != HAL_OK) {
+    if (HAL_ADC_Start_DMA(hadc, cfg->primarybuf, cfg->buflen) != HAL_OK) {
+        ++stm32f4_adc_stats.adc_DMA_start_error;
         goto err;
     }
 
@@ -592,6 +609,10 @@ err:
 
 /**
  * Blocking read of an ADC channel, returns result as an integer.
+ *
+ * @param1 ADC device structure
+ * @param2 channel number
+ * @param3 ADC result ptr
  */
 static int
 stm32f4_adc_read_channel(struct adc_dev *dev, uint8_t cnum, int *result)
@@ -615,11 +636,23 @@ stm32f4_adc_read_buffer(struct adc_dev *dev, void *buf, int buf_len, int off,
 
     assert(off < buf_len);
 
-    *result = *((uint32_t *) buf + off);
+    /*
+     * If secondary buffer exists the primary buf is going to be cached
+     * in the secondary buffer if the primary buffer is full and we
+     * would be reading that instead since the buffer is specified by
+     * the application
+     */
+    *result = *((uint32_t *)buf + off);
 
     return (OS_OK);
 }
 
+/**
+ * Callback to return size of buffer
+ *
+ * @param1 ADC device ptr
+ * @param2 
+ */
 static int
 stm32f4_adc_size_buffer(struct adc_dev *dev, int chans, int samples)
 {
@@ -638,6 +671,10 @@ void ADC_IRQHandler(void)
  * Callback to initialize an adc_dev structure from the os device
  * initialization callback.  This sets up a stm32f4_adc_device(), so
  * that subsequent lookups to this device allow us to manipulate it.
+ *
+ * @param1 os device ptr
+ * @param2 stm32f4 ADC device cfg ptr
+ * @return OS_OK on success
  */
 int
 stm32f4_adc_dev_init(struct os_dev *odev, void *arg)
@@ -654,10 +691,6 @@ stm32f4_adc_dev_init(struct os_dev *odev, void *arg)
 
     os_mutex_init(&dev->ad_lock);
 
-    /* Have a pointer to the init typedef from the configured
-     * value.  This allows non-driver specific items to be configured.
-     */
-
     dev->ad_chans = (void *) sac->sac_chans;
     dev->ad_chan_count = sac->sac_chan_count;
 
@@ -674,6 +707,6 @@ stm32f4_adc_dev_init(struct os_dev *odev, void *arg)
     af->af_read_buffer = stm32f4_adc_read_buffer;
     af->af_size_buffer = stm32f4_adc_size_buffer;
 
-    return (0);
+    return (OS_OK);
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28814b8d/drivers/adc/include/adc/adc.h
----------------------------------------------------------------------
diff --git a/drivers/adc/include/adc/adc.h b/drivers/adc/include/adc/adc.h
index f0d4ad5..d81286e 100644
--- a/drivers/adc/include/adc/adc.h
+++ b/drivers/adc/include/adc/adc.h
@@ -158,9 +158,6 @@ struct adc_dev {
     int ad_chan_count;
     adc_event_handler_func_t ad_event_handler_func;
     void *ad_event_handler_arg;
-    void *primarybuf;
-    void *secondarybuf;
-    int buflen;
 };
 
 int adc_chan_config(struct adc_dev *, uint8_t, void *);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28814b8d/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 41c0674..3cd2cc0 100644
--- a/hw/bsp/olimex_stm32-e407_devboard/pkg.yml
+++ b/hw/bsp/olimex_stm32-e407_devboard/pkg.yml
@@ -65,7 +65,7 @@ pkg.syscfg_defs:
         value:  1
     ADC_2:
         description: "ADC_2"
-        value:  1
+        value:  0
     ADC_3:
         description: "ADC_3"
         value:  1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/28814b8d/hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c b/hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c
index 39b0bac..3edb114 100644
--- a/hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c
+++ b/hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c
@@ -70,23 +70,53 @@ static struct uart_dev hal_uart0;
 #if MYNEWT_VAL(ADC_1)
 struct adc_dev my_dev_adc1;
 #endif
-
-struct stm32f4_uart_cfg;
+#if MYNEWT_VAL(ADC_2)
+struct adc_dev my_dev_adc2;
+#endif
 #if MYNEWT_VAL(ADC_3)
 struct adc_dev my_dev_adc3;
 #endif
 
+struct stm32f4_uart_cfg;
+
 extern struct stm32f4_uart_cfg *bsp_uart_config(int port);
 
 /*****************ADC3 Config ***************/
 
-#if MYNEWT_VAL(ADC_3)
+#if MYNEWT_VAL(ADC_1)
+/*
+ * adc_handle is defined earlier because the DMA handle's
+ * parent needs to be pointing to the adc_handle
+ */
+ADC_HandleTypeDef adc1_handle;
 
-ADC_HandleTypeDef adc_handle;
+#define STM32F4_DEFAULT_DMA40_HANDLE {\
+    .Instance = DMA2_Stream4,\
+    .Init.Channel = DMA_CHANNEL_0,\
+    .Init.Direction = DMA_PERIPH_TO_MEMORY,\
+    .Init.PeriphInc = DMA_PINC_DISABLE,\
+    .Init.MemInc = DMA_MINC_ENABLE,\
+    .Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD,\
+    .Init.MemDataAlignment = DMA_MDATAALIGN_WORD,\
+    .Init.Mode = DMA_CIRCULAR,\
+    .Init.Priority = DMA_PRIORITY_HIGH,\
+    .Init.FIFOMode = DMA_FIFOMODE_DISABLE,\
+    .Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL,\
+    .Init.MemBurst = DMA_MBURST_SINGLE,\
+    .Init.PeriphBurst = DMA_PBURST_SINGLE,\
+    .Parent = &adc1_handle,\
+}
 
-#define STM32F4_DEFAULT_DMA_HANDLE21 {\
-    .Instance = DMA2_Stream1,\
-    .Init.Channel = DMA_CHANNEL_2,\
+DMA_HandleTypeDef adc1_dma40_handle = STM32F4_DEFAULT_DMA40_HANDLE;
+#endif
+
+#if MYNEWT_VAL(ADC_2)
+
+ADC_HandleTypeDef adc2_handle;
+
+#define STM32F4_DEFAULT_DMA21_HANDLE {\
+    .Instance = DMA2_Stream2,\
+    .Init.Channel = DMA_CHANNEL_1,\
     .Init.Direction = DMA_PERIPH_TO_MEMORY,\
     .Init.PeriphInc = DMA_PINC_DISABLE,\
     .Init.MemInc = DMA_MINC_ENABLE,\
@@ -98,22 +128,20 @@ ADC_HandleTypeDef adc_handle;
     .Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL,\
     .Init.MemBurst = DMA_MBURST_SINGLE,\
     .Init.PeriphBurst = DMA_PBURST_SINGLE,\
-    .Parent = &adc_handle,\
+    .Parent = &adc2_handle,\
 }
+
+DMA_HandleTypeDef adc2_dma21_handle = STM32F4_DEFAULT_DMA21_HANDLE;
 #endif
 
-#if MYNEWT_VAL(ADC_1)
-/*
- * adc_handle is defined earlier because the DMA handle's
- * parent needs to be pointing to the adc_handle
- */
-ADC_HandleTypeDef adc_handle120;
 
-DMA_HandleTypeDef adc_dma_handle21 = STM32F4_DEFAULT_DMA_HANDLE21;
+#if MYNEWT_VAL(ADC_3)
+
+ADC_HandleTypeDef adc3_handle;
 
-#define STM32F4_DEFAULT_DMA_HANDLE20 {\
+#define STM32F4_DEFAULT_DMA02_HANDLE {\
     .Instance = DMA2_Stream0,\
-    .Init.Channel = DMA_CHANNEL_0,\
+    .Init.Channel = DMA_CHANNEL_2,\
     .Init.Direction = DMA_PERIPH_TO_MEMORY,\
     .Init.PeriphInc = DMA_PINC_DISABLE,\
     .Init.MemInc = DMA_MINC_ENABLE,\
@@ -125,13 +153,12 @@ DMA_HandleTypeDef adc_dma_handle21 = STM32F4_DEFAULT_DMA_HANDLE21;
     .Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL,\
     .Init.MemBurst = DMA_MBURST_SINGLE,\
     .Init.PeriphBurst = DMA_PBURST_SINGLE,\
-    .Parent = &adc_handle120,\
+    .Parent = &adc3_handle,\
 }
 
-DMA_HandleTypeDef adc_dma_handle20 = STM32F4_DEFAULT_DMA_HANDLE20;
+DMA_HandleTypeDef adc3_dma02_handle = STM32F4_DEFAULT_DMA02_HANDLE;
 #endif
 
-
 #define STM32F4_ADC_DEFAULT_INIT_TD {\
     .ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2,\
     .Resolution = ADC_RESOLUTION12b,\
@@ -147,74 +174,106 @@ DMA_HandleTypeDef adc_dma_handle20 = STM32F4_DEFAULT_DMA_HANDLE20;
     .DMAContinuousRequests = ENABLE\
 }
 
+#if MYNEWT_VAL(ADC_1)
 
-#if MYNEWT_VAL(ADC_3)
-
-#define STM32F4_DEFAULT_ADC_HANDLE {\
+/*****************ADC1 Config ***************/
+#define STM32F4_DEFAULT_ADC1_HANDLE {\
     .Init = STM32F4_ADC_DEFAULT_INIT_TD,\
-    .Instance = ADC3,\
+    .Instance = ADC1,\
     .NbrOfCurrentConversionRank = 0,\
-    .DMA_Handle = &adc_dma_handle21,\
+    .DMA_Handle = &adc1_dma40_handle,\
     .Lock = HAL_UNLOCKED,\
     .State = 0,\
     .ErrorCode = 0\
 }
 
-ADC_HandleTypeDef adc_handle = STM32F4_DEFAULT_ADC_HANDLE;
+ADC_HandleTypeDef adc1_handle = STM32F4_DEFAULT_ADC1_HANDLE;
 
-#define STM32F4_ADC_DEFAULT_SAC_CHAN {\
+#define STM32F4_ADC1_DEFAULT_SAC {\
     .c_refmv = 3300,\
     .c_res   = 12,\
     .c_configured = 1,\
-    .c_cnum = ADC_CHANNEL_4\
+    .c_cnum = ADC_CHANNEL_10\
 }
 
-struct adc_chan_config def_sac_chan = STM32F4_ADC_DEFAULT_SAC_CHAN;
+struct adc_chan_config adc1_chan10_config = STM32F4_ADC1_DEFAULT_SAC;
 
-#define STM32F4_ADC_DEFAULT_CONFIG {\
+#define STM32F4_ADC1_DEFAULT_CONFIG {\
     .sac_chan_count = 16,\
-    .sac_chans = &def_sac_chan,\
-    .sac_adc_handle = &adc_handle,\
+    .sac_chans = &adc1_chan10_config,\
+    .sac_adc_handle = &adc1_handle,\
 }
 
-struct stm32f4_adc_dev_cfg adc_config = STM32F4_ADC_DEFAULT_CONFIG;
+struct stm32f4_adc_dev_cfg adc1_config = STM32F4_ADC1_DEFAULT_CONFIG;
+/*********************************************/
 #endif
 
+#if MYNEWT_VAL(ADC_2)
 
-#if MYNEWT_VAL(ADC_1)
-
-/*****************ADC1 Config ***************/
-#define STM32F4_DEFAULT_ADC_HANDLE120 {\
+/*****************ADC2 Config ***************/
+#define STM32F4_DEFAULT_ADC2_HANDLE {\
     .Init = STM32F4_ADC_DEFAULT_INIT_TD,\
-    .Instance = ADC1,\
+    .Instance = ADC2,\
     .NbrOfCurrentConversionRank = 0,\
-    .DMA_Handle = &adc_dma_handle20,\
+    .DMA_Handle = &adc2_dma21_handle,\
     .Lock = HAL_UNLOCKED,\
     .State = 0,\
     .ErrorCode = 0\
 }
 
-ADC_HandleTypeDef adc_handle120 = STM32F4_DEFAULT_ADC_HANDLE120;
+ADC_HandleTypeDef adc2_handle = STM32F4_DEFAULT_ADC2_HANDLE;
 
-#define STM32F4_ADC_DEFAULT_SAC_CHAN10 {\
+#define STM32F4_ADC2_DEFAULT_SAC {\
     .c_refmv = 3300,\
     .c_res   = 12,\
     .c_configured = 1,\
-    .c_cnum = ADC_CHANNEL_10\
+    .c_cnum = ADC_CHANNEL_1\
 }
 
-struct adc_chan_config def_sac_chan10 = STM32F4_ADC_DEFAULT_SAC_CHAN10;
+struct adc_chan_config adc2_chan1_config = STM32F4_ADC2_DEFAULT_SAC;
 
-#define STM32F4_ADC_DEFAULT_CONFIG_1_2_0_10 {\
+#define STM32F4_ADC2_DEFAULT_CONFIG {\
     .sac_chan_count = 16,\
-    .sac_chans = &def_sac_chan10,\
-    .sac_adc_handle = &adc_handle120,\
+    .sac_chans = &adc2_chan1_config,\
+    .sac_adc_handle = &adc2_handle,\
 }
 
-struct stm32f4_adc_dev_cfg adc_config12010 = STM32F4_ADC_DEFAULT_CONFIG_1_2_0_10;
+struct stm32f4_adc_dev_cfg adc2_config = STM32F4_ADC2_DEFAULT_CONFIG;
 /*********************************************/
 #endif
 
+#if MYNEWT_VAL(ADC_3)
+
+#define STM32F4_DEFAULT_ADC3_HANDLE {\
+    .Init = STM32F4_ADC_DEFAULT_INIT_TD,\
+    .Instance = ADC3,\
+    .NbrOfCurrentConversionRank = 0,\
+    .DMA_Handle = &adc3_dma02_handle,\
+    .Lock = HAL_UNLOCKED,\
+    .State = 0,\
+    .ErrorCode = 0\
+}
+
+ADC_HandleTypeDef adc3_handle = STM32F4_DEFAULT_ADC3_HANDLE;
+
+#define STM32F4_ADC3_DEFAULT_SAC {\
+    .c_refmv = 3300,\
+    .c_res   = 12,\
+    .c_configured = 1,\
+    .c_cnum = ADC_CHANNEL_4\
+}
+
+struct adc_chan_config adc3_chan4_config = STM32F4_ADC3_DEFAULT_SAC;
+
+#define STM32F4_ADC3_DEFAULT_CONFIG {\
+    .sac_chan_count = 16,\
+    .sac_chans = &adc3_chan4_config,\
+    .sac_adc_handle = &adc3_handle,\
+}
+
+struct stm32f4_adc_dev_cfg adc3_config = STM32F4_ADC3_DEFAULT_CONFIG;
+#endif
+
 void _close(int fd);
 
 /*
@@ -247,18 +306,24 @@ bsp_init(void)
     rc = os_dev_create((struct os_dev *) &hal_uart0, CONSOLE_UART,
       OS_DEV_INIT_PRIMARY, 0, uart_hal_init, (void *)bsp_uart_config(0));
     assert(rc == 0);
-#if MYNEWT_VAL(ADC_3)
-    rc = os_dev_create((struct os_dev *) &my_dev_adc3, "adc3",
+#if MYNEWT_VAL(ADC_1)
+    rc = os_dev_create((struct os_dev *) &my_dev_adc1, "adc1",
             OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
-            stm32f4_adc_dev_init, &adc_config);
+            stm32f4_adc_dev_init, &adc1_config);
     assert(rc == 0);
 #endif
-
-#if MYNEWT_VAL(ADC_1)
-    rc = os_dev_create((struct os_dev *) &my_dev_adc1, "adc1",
+#if MYNEWT_VAL(ADC_2)
+    rc = os_dev_create((struct os_dev *) &my_dev_adc2, "adc2",
             OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
-            stm32f4_adc_dev_init, &adc_config12010);
+            stm32f4_adc_dev_init, &adc2_config);
     assert(rc == 0);
 #endif
+#if MYNEWT_VAL(ADC_3)
+    rc = os_dev_create((struct os_dev *) &my_dev_adc3, "adc3",
+            OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
+            stm32f4_adc_dev_init, &adc3_config);
+    assert(rc == 0);
+#endif
+
 }
 


[25/50] [abbrv] incubator-mynewt-core git commit: Conclude merge from sterly_refactor.

Posted by vi...@apache.org.
Conclude merge from sterly_refactor.

Additional changes to fix post-merge build failures.


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

Branch: refs/heads/sterly_refactor
Commit: 8dffea7d030262f574ce2c0f8e3d7dd2599b74f8
Parents: c5901fc
Author: Christopher Collins <cc...@apache.org>
Authored: Wed Sep 21 19:07:36 2016 -0700
Committer: Christopher Collins <cc...@apache.org>
Committed: Wed Sep 21 19:07:36 2016 -0700

----------------------------------------------------------------------
 apps/blecent/src/main.c                         |    3 +-
 apps/blehci/src/main.c                          |    6 -
 apps/bleprph/pkg.yml                            |    2 -
 apps/bleprph/src/main.c                         |    5 +-
 apps/bleuart/src/main.c                         |   14 -
 apps/slinky/src/main.c                          |   15 +-
 apps/splitty/src/main.c                         |  116 +-
 hw/bsp/native/include/bsp/bsp.h                 |    6 +-
 hw/bsp/native/pkg.yml                           |    1 +
 hw/bsp/nrf51dk/pkg.yml                          |    4 +-
 hw/mcu/native/src/hal_uart.c                    |    2 +
 libs/boot_serial/test/pkg.yml                   |    3 +
 libs/bootutil/pkg.yml                           |    3 -
 libs/bootutil/src/bootutil_misc.c               |    3 +
 libs/bootutil/src/loader.c                      |    6 -
 libs/bootutil/test/src/boot_serial_test.c       | 1171 ------------------
 .../stub/include/console/console_prompt.h       |   35 -
 libs/console/stub/include/console/prompt.h      |   34 +
 libs/imgmgr/src/imgmgr.c                        |    2 +
 libs/imgmgr/src/imgmgr_cli.c                    |    6 +-
 libs/newtmgr_oic/pkg.yml                        |   11 +-
 libs/os/src/test/callout_test.c                 |  330 -----
 libs/os/test/src/callout_test.c                 |  330 +++++
 libs/shell/src/shell.c                          |    5 +-
 libs/util/src/test/hex_test.c                   |  125 --
 libs/util/test/src/hex_test.c                   |  125 ++
 net/nimble/controller/pkg.yml                   |    2 +-
 net/nimble/controller/src/ble_ll_adv.c          |    8 +-
 net/nimble/controller/src/ble_ll_conn.c         |   56 +-
 net/nimble/controller/src/ble_ll_conn_hci.c     |    4 +-
 net/nimble/controller/src/ble_ll_scan.c         |    6 +-
 .../gap/include/services/gap/ble_svc_gap.h      |    2 +-
 net/nimble/host/services/gap/pkg.yml            |    3 +
 net/nimble/host/services/gap/src/ble_svc_gap.c  |   14 +-
 .../gatt/include/services/gatt/ble_svc_gatt.h   |    2 +-
 net/nimble/host/services/gatt/pkg.yml           |    2 +-
 .../host/services/gatt/src/ble_svc_gatt.c       |   13 +-
 .../services/mandatory/src/ble_svc_mandatory.c  |   36 -
 net/nimble/host/src/ble_gap.c                   |    2 +-
 net/nimble/host/src/ble_hs.c                    |   20 -
 net/nimble/host/src/test/ble_hs_hci_test.c      |   99 --
 net/nimble/host/test/src/ble_gap_test.c         |   28 +-
 net/nimble/host/test/src/ble_hs_test_util.c     |   17 -
 net/nimble/transport/ram/src/ble_hci_ram.c      |    2 +
 net/nimble/transport/uart/src/ble_hci_uart.c    |   28 +-
 sys/config/pkg.yml                              |    2 +-
 sys/config/test/pkg.yml                         |    4 +
 sys/mn_socket/test/src/mn_sock_test.c           |    2 +-
 sys/reboot/pkg.yml                              |    2 +-
 sys/stats/pkg.yml                               |    2 +-
 50 files changed, 630 insertions(+), 2089 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/apps/blecent/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blecent/src/main.c b/apps/blecent/src/main.c
index c9ae8a4..fbab587 100755
--- a/apps/blecent/src/main.c
+++ b/apps/blecent/src/main.c
@@ -504,8 +504,7 @@ main(void)
     os_init();
 
     /* Initialize the blecent log. */
-    log_console_handler_init(&blecent_log_console_handler);
-    log_register("blecent", &blecent_log, &blecent_log_console_handler);
+    log_register("blecent", &blecent_log, &log_console_handler, NULL);
 
     /* Initialize the eventq for the application task. */
     os_eventq_init(&blecent_evq);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/apps/blehci/src/main.c
----------------------------------------------------------------------
diff --git a/apps/blehci/src/main.c b/apps/blehci/src/main.c
index 1c75528..c2b894b 100755
--- a/apps/blehci/src/main.c
+++ b/apps/blehci/src/main.c
@@ -19,12 +19,6 @@
 #include <assert.h>
 #include "os/os.h"
 
-/* Our global device address (public) */
-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 };
-
 int
 main(void)
 {

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/apps/bleprph/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/bleprph/pkg.yml b/apps/bleprph/pkg.yml
index 89549f7..4e15338 100644
--- a/apps/bleprph/pkg.yml
+++ b/apps/bleprph/pkg.yml
@@ -52,5 +52,3 @@ pkg.syscfg_vals:
 
     # Disable unused eddystone feature.
     BLE_EDDYSTONE: 0
-
-    BLE_SM: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/apps/bleprph/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleprph/src/main.c b/apps/bleprph/src/main.c
index 6792b50..6d27e00 100755
--- a/apps/bleprph/src/main.c
+++ b/apps/bleprph/src/main.c
@@ -32,7 +32,7 @@
 /* BLE */
 #include "nimble/ble.h"
 #include "host/ble_hs.h"
-#include "services/mandatory/ble_svc_gap.h"
+#include "services/gap/ble_svc_gap.h"
 
 /* Application-specified header. */
 #include "bleprph.h"
@@ -305,8 +305,7 @@ main(void)
     os_init();
 
     /* Initialize the bleprph log. */
-    log_console_handler_init(&bleprph_log_console_handler);
-    log_register("bleprph", &bleprph_log, &bleprph_log_console_handler);
+    log_register("bleprph", &bleprph_log, &log_console_handler, NULL);
 
     /* Initialize eventq */
     os_eventq_init(&bleprph_evq);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/apps/bleuart/src/main.c
----------------------------------------------------------------------
diff --git a/apps/bleuart/src/main.c b/apps/bleuart/src/main.c
index d0df368..f5d9387 100755
--- a/apps/bleuart/src/main.c
+++ b/apps/bleuart/src/main.c
@@ -55,20 +55,6 @@
 #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))

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/apps/slinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/slinky/src/main.c b/apps/slinky/src/main.c
index 02a5d34..9cb9cdf 100755
--- a/apps/slinky/src/main.c
+++ b/apps/slinky/src/main.c
@@ -32,11 +32,11 @@
 #if defined SPLIT_LOADER || defined SPLIT_APPLICATION
 #include <split/split.h>
 #endif
-#if MYNEWT_PKG_FS_NFFS
+#if MYNEWT_VAL(CONFIG_NFFS)
 #include <fs/fs.h>
 #include <nffs/nffs.h>
 #include <config/config_file.h>
-#elif MYNEWT_PKG_SYS_FCB
+#elif MYNEWT_VAL(CONFIG_FCB)
 #include <fcb/fcb.h>
 #include <config/config_fcb.h>
 #else
@@ -95,6 +95,15 @@ static STATS_NAME_START(gpio_stats)
 STATS_NAME(gpio_stats, toggles)
 STATS_NAME_END(gpio_stats)
 
+#if !MYNEWT_VAL(CONFIG_NFFS)
+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
+
 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);
@@ -295,8 +304,6 @@ int
 main(int argc, char **argv)
 {
     int rc;
-    os_stack_t *pstack;
-
 
 #ifdef ARCH_sim
     mcu_sim_parse_args(argc, argv);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/apps/splitty/src/main.c
----------------------------------------------------------------------
diff --git a/apps/splitty/src/main.c b/apps/splitty/src/main.c
index acdc750..d050a62 100755
--- a/apps/splitty/src/main.c
+++ b/apps/splitty/src/main.c
@@ -30,11 +30,11 @@
 #if defined SPLIT_APPLICATION
 #include <split/split.h>
 #endif
-#ifdef NFFS_PRESENT
+#if MYNEWT_VAL(CONFIG_NFFS)
 #include <fs/fs.h>
 #include <nffs/nffs.h>
 #include <config/config_file.h>
-#elif FCB_PRESENT
+#elif MYNEWT_VAL(CONFIG_FCB)
 #include <fcb/fcb.h>
 #include <config/config_fcb.h>
 #else
@@ -71,13 +71,6 @@ static volatile int g_task1_loops;
 #define TASK2_STACK_SIZE    OS_STACK_ALIGN(128)
 static struct os_task task2;
 
-#define SHELL_TASK_PRIO (3)
-#define SHELL_MAX_INPUT_LEN     (256)
-#define SHELL_TASK_STACK_SIZE (OS_STACK_ALIGN(384))
-
-#define NEWTMGR_TASK_PRIO (4)
-#define NEWTMGR_TASK_STACK_SIZE (OS_STACK_ALIGN(896))
-
 static struct log my_log;
 
 static volatile int g_task2_loops;
@@ -94,21 +87,11 @@ STATS_SECT_END
 
 static STATS_SECT_DECL(gpio_stats) g_stats_gpio_toggle;
 
-static STATS_NAME_START(gpio_stats)
+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
+#if !MYNEWT_VAL(CONFIG_NFFS)
 struct flash_area conf_fcb_area[NFFS_AREA_MAX + 1];
 
 static struct conf_fcb my_conf = {
@@ -117,15 +100,6 @@ static struct conf_fcb my_conf = {
 };
 #endif
 
-#define DEFAULT_MBUF_MPOOL_BUF_LEN (256)
-#define DEFAULT_MBUF_MPOOL_NBUFS (9)
-
-static uint8_t default_mbuf_mpool_data[DEFAULT_MBUF_MPOOL_BUF_LEN *
-    DEFAULT_MBUF_MPOOL_NBUFS];
-
-static struct os_mbuf_pool default_mbuf_pool;
-static struct os_mempool default_mbuf_mpool;
-
 static uint32_t cbmem_buf[MAX_CBMEM_BUF];
 static struct cbmem cbmem;
 
@@ -199,6 +173,7 @@ int
 init_tasks(void)
 {
     os_stack_t *pstack;
+
     /* Initialize global test semaphore */
     os_sem_init(&g_test_sem, 0);
 
@@ -218,41 +193,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)
@@ -295,66 +236,26 @@ int
 main(int argc, char **argv)
 {
     int rc;
-    os_stack_t *pstack;
-
 
 #ifdef ARCH_sim
     mcu_sim_parse_args(argc, argv);
 #endif
 
-    conf_init();
+    os_init();
 
-    log_init();
     cbmem_init(&cbmem, cbmem_buf, MAX_CBMEM_BUF);
     log_register("log", &my_log, &log_cbmem_handler, &cbmem);
 
-    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();
-
-    pstack = malloc(sizeof(os_stack_t) * SHELL_TASK_STACK_SIZE);
-    assert(pstack);
-
-    shell_task_init(SHELL_TASK_PRIO, pstack, SHELL_TASK_STACK_SIZE,
-                    SHELL_MAX_INPUT_LEN);
-
-    pstack = malloc(sizeof(os_stack_t) * NEWTMGR_TASK_STACK_SIZE);
-    assert(pstack);
-#
-    nmgr_task_init(NEWTMGR_TASK_PRIO, pstack, NEWTMGR_TASK_STACK_SIZE);
-    imgmgr_module_init();
-
-    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));
 
     stats_register("gpio_toggle", STATS_HDR(g_stats_gpio_toggle));
 
-    reboot_init_handler(LOG_TYPE_STORAGE, 10);
-
 #if defined SPLIT_APPLICATION
     split_app_init();
 #endif
@@ -372,4 +273,3 @@ main(int argc, char **argv)
 
     return rc;
 }
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/hw/bsp/native/include/bsp/bsp.h
----------------------------------------------------------------------
diff --git a/hw/bsp/native/include/bsp/bsp.h b/hw/bsp/native/include/bsp/bsp.h
index 42a53f4..d4cedc3 100644
--- a/hw/bsp/native/include/bsp/bsp.h
+++ b/hw/bsp/native/include/bsp/bsp.h
@@ -34,9 +34,9 @@ extern "C" {
 /* LED pins */
 #define LED_BLINK_PIN   (0x1)
 
-/* Logical UART ports */
-#define UART_CNT	2
-#define CONSOLE_UART	0
+/* UART info */
+#define CONSOLE_UART    	"uart1"
+#define CONSOLE_UART_SPEED    	9600
 
 #define NFFS_AREA_MAX    (8)
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/hw/bsp/native/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/native/pkg.yml b/hw/bsp/native/pkg.yml
index ede1525..bbbb708 100644
--- a/hw/bsp/native/pkg.yml
+++ b/hw/bsp/native/pkg.yml
@@ -31,6 +31,7 @@ pkg.arch: sim
 pkg.compiler: compiler/sim
 pkg.deps:
     - hw/mcu/native
+    - drivers/uart/uart_hal
 
 pkg.deps.BLE_DEVICE:
     - net/nimble/drivers/native

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/hw/bsp/nrf51dk/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/bsp/nrf51dk/pkg.yml b/hw/bsp/nrf51dk/pkg.yml
index beee6fc..2be9fc2 100644
--- a/hw/bsp/nrf51dk/pkg.yml
+++ b/hw/bsp/nrf51dk/pkg.yml
@@ -65,7 +65,7 @@ pkg.syscfg_defs:
 
     ADC_0:
         description: 'TBD'
-        value: 1
+        value: 0
     ADC_0_RESOLUTION:
         description: 'TBD'
         value: 'SAADC_CONFIG_RESOLUTION'
@@ -94,7 +94,7 @@ pkg.syscfg_defs:
 
     SPI_MASTER:
         description: 'TBD'
-        value: 1
+        value: 0
     SPI_SLAVE:
         description: 'TBD'
         value: 0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/hw/mcu/native/src/hal_uart.c
----------------------------------------------------------------------
diff --git a/hw/mcu/native/src/hal_uart.c b/hw/mcu/native/src/hal_uart.c
index 58f6b96..0db339f 100644
--- a/hw/mcu/native/src/hal_uart.c
+++ b/hw/mcu/native/src/hal_uart.c
@@ -34,6 +34,8 @@
 #include <unistd.h>
 #include <string.h>
 
+#define UART_CNT                2
+
 #define UART_MAX_BYTES_PER_POLL	64
 #define UART_POLLER_STACK_SZ	OS_STACK_ALIGN(1024)
 #define UART_POLLER_PRIO	0

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/boot_serial/test/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/boot_serial/test/pkg.yml b/libs/boot_serial/test/pkg.yml
index e123ce1..561c94b 100644
--- a/libs/boot_serial/test/pkg.yml
+++ b/libs/boot_serial/test/pkg.yml
@@ -28,3 +28,6 @@ pkg.deps:
 
 pkg.deps.SELFTEST:
     - libs/console/stub
+
+pkg.syscfg_vals.SELFTEST:
+    CONFIG_FCB: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/bootutil/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/bootutil/pkg.yml b/libs/bootutil/pkg.yml
index 4d78647..1293ea8 100644
--- a/libs/bootutil/pkg.yml
+++ b/libs/bootutil/pkg.yml
@@ -34,9 +34,6 @@ pkg.deps:
 pkg.deps.BOOTUTIL_NFFS:
     - fs/nffs
 
-pkg.init_function: bootutil_pkg_init
-pkg.init_stage: 5
-
 pkg.syscfg_defs:
     BOOTUTIL_NFFS:
         description: 'TBD'

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/bootutil/src/bootutil_misc.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/bootutil_misc.c b/libs/bootutil/src/bootutil_misc.c
index 0f9a2ce..b6a1c31 100644
--- a/libs/bootutil/src/bootutil_misc.c
+++ b/libs/bootutil/src/bootutil_misc.c
@@ -17,11 +17,14 @@
  * under the License.
  */
 
+#include <assert.h>
 #include <string.h>
 #include <inttypes.h>
 
 #include "syscfg/syscfg.h"
+#include "hal/hal_bsp.h"
 #include "hal/hal_flash.h"
+#include "hal/flash_map.h"
 #include "config/config.h"
 #include "os/os.h"
 #include "bootutil/image.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/bootutil/src/loader.c
----------------------------------------------------------------------
diff --git a/libs/bootutil/src/loader.c b/libs/bootutil/src/loader.c
index c8f419d..e2d4cfa 100644
--- a/libs/bootutil/src/loader.c
+++ b/libs/bootutil/src/loader.c
@@ -650,9 +650,3 @@ split_app_go_end:
     free(descs);
     return rc;
 }
-
-void
-bootutil_pkg_init(void)
-{
-    bootutil_cfg_register();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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
deleted file mode 100644
index 420a802..0000000
--- a/libs/bootutil/test/src/boot_serial_test.c
+++ /dev/null
@@ -1,1171 +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 "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

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/console/stub/include/console/console_prompt.h
----------------------------------------------------------------------
diff --git a/libs/console/stub/include/console/console_prompt.h b/libs/console/stub/include/console/console_prompt.h
deleted file mode 100644
index 8cbda7c..0000000
--- a/libs/console/stub/include/console/console_prompt.h
+++ /dev/null
@@ -1,35 +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 __CONSOLE_H__
-#define __CONSOLE_H__
-
-#include <stdarg.h>
-
-
-/* print console prompt */
-void console_print_prompt(void);
-/* set the console prompt character */
-void console_set_prompt(char);
-
-
-extern char console_prompt[2];
-
-
-#endif /* __CONSOLE_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/console/stub/include/console/prompt.h
----------------------------------------------------------------------
diff --git a/libs/console/stub/include/console/prompt.h b/libs/console/stub/include/console/prompt.h
new file mode 100644
index 0000000..9d7f622
--- /dev/null
+++ b/libs/console/stub/include/console/prompt.h
@@ -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.
+ */
+
+#ifndef H_CONSOLE_PROMPT_
+#define H_CONSOLE_PROMPT_
+
+#include <stdarg.h>
+
+/* print console prompt */
+void console_print_prompt(void);
+/* set the console prompt character */
+void console_set_prompt(char);
+
+
+extern char console_prompt[2];
+
+
+#endif /* __CONSOLE_H__ */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/imgmgr/src/imgmgr.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr.c b/libs/imgmgr/src/imgmgr.c
index d204cc4..04ec43c 100644
--- a/libs/imgmgr/src/imgmgr.c
+++ b/libs/imgmgr/src/imgmgr.c
@@ -28,6 +28,8 @@
 #include "json/json.h"
 #include "util/base64.h"
 #include "bootutil/image.h"
+#include "bootutil/bootutil_misc.h"
+#include "newtmgr/newtmgr.h"
 
 #include "imgmgr/imgmgr.h"
 #include "imgmgr_priv.h"

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/imgmgr/src/imgmgr_cli.c
----------------------------------------------------------------------
diff --git a/libs/imgmgr/src/imgmgr_cli.c b/libs/imgmgr/src/imgmgr_cli.c
index 2b70890..da1c0bf 100644
--- a/libs/imgmgr/src/imgmgr_cli.c
+++ b/libs/imgmgr/src/imgmgr_cli.c
@@ -17,7 +17,9 @@
  * under the License.
  */
 
-#ifdef SHELL_PRESENT
+#include "syscfg/syscfg.h"
+
+#if MYNEWT_VAL(IMGMGR_CLI)
 
 #include <string.h>
 
@@ -136,4 +138,4 @@ imgr_cli_register(void)
 {
     return shell_cmd_register(&shell_imgr_cmd);
 }
-#endif
+#endif /* MYNEWT_VAL(IMGMGR_CLI) */

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/newtmgr_oic/pkg.yml
----------------------------------------------------------------------
diff --git a/libs/newtmgr_oic/pkg.yml b/libs/newtmgr_oic/pkg.yml
index 75d20f5..69cc00b 100644
--- a/libs/newtmgr_oic/pkg.yml
+++ b/libs/newtmgr_oic/pkg.yml
@@ -39,6 +39,11 @@ pkg.cflags:
 pkg.apis:
     - newtmgr
 
-pkg.features:
-    - NEWTMGR
-    - OC_SERVER
+pkg.syscfg_defs:
+    ### These should be renamed with a proper prefix.
+    NEWTMGR:
+        description: 'TBD'
+        value: 1
+    OC_SERVER:
+        description: 'TBD'
+        value: 1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/os/src/test/callout_test.c
----------------------------------------------------------------------
diff --git a/libs/os/src/test/callout_test.c b/libs/os/src/test/callout_test.c
deleted file mode 100644
index 4e3811d..0000000
--- a/libs/os/src/test/callout_test.c
+++ /dev/null
@@ -1,330 +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 "os/os_eventq.h"
-#include "os/os_callout.h"
-#include "os/os_time.h"
-
-/* Task 1 for sending */
-#define CALLOUT_STACK_SIZE        (5120)
-#define SEND_CALLOUT_TASK_PRIO        (1)
-struct os_task callout_task_struct_send;
-os_stack_t callout_task_stack_send[CALLOUT_STACK_SIZE];
-
-#define RECEIVE_CALLOUT_TASK_PRIO        (2)
-struct os_task callout_task_struct_receive;
-os_stack_t callout_task_stack_receive[CALLOUT_STACK_SIZE];
-
-/* Delearing variables for callout_func */
-struct os_callout_func callout_func_test;
-
-/* The event to be sent*/
-struct os_eventq callout_evq;
-struct os_event callout_ev;
-
-/* The callout_stop task */
-#define SEND_STOP_CALLOUT_TASK_PRIO        (3)
-struct os_task callout_task_struct_stop_send;
-os_stack_t callout_task_stack_stop_send[CALLOUT_STACK_SIZE];
-
-#define RECEIVE_STOP_CALLOUT_TASK_PRIO        (4)
-struct os_task callout_task_struct_stop_receive;
-os_stack_t callout_task_stack_stop_receive[CALLOUT_STACK_SIZE];
-
-/* Delearing variables for callout_stop_func */
-#define MULTI_SIZE    (2)
-struct os_callout_func callout_func_stop_test[MULTI_SIZE];
-
-/* The event to be sent*/
-struct os_eventq callout_stop_evq[MULTI_SIZE];
-struct os_event callout_stop_ev;
-
-/* Declearing varables for callout_speak */
-#define SPEAK_CALLOUT_TASK_PRIO        (5)
-struct os_task callout_task_struct_speak;
-os_stack_t callout_task_stack_speak[CALLOUT_STACK_SIZE];
-
-/* Declearing varaibles for listen */
-#define LISTEN_CALLOUT_TASK_PRIO        (6)
-struct os_task callout_task_struct_listen;
-os_stack_t callout_task_stack_listen[CALLOUT_STACK_SIZE];
-
-struct os_callout_func callout_func_speak;
-
-/* Global variables to be used by the callout functions */
-int p;
-int q;
-int t;
-/* This is the function for callout_init*/
-void
-my_callout_func(void *arg)
-{
-    p = 4;
-}
-
-/* This is the function for callout_init of stop test_case*/
-void
-my_callout_stop_func(void *arg)
-{
-    q = 1;
-}
-/* This is the function for callout_init for speak test_case*/
-void
-my_callout_speak_func(void *arg)
-{
-    t = 2;
-}
-
-/* This is a callout task to send data */
-void
-callout_task_send(void *arg )
-{
-   int i;
-    /* Should say whether callout is armed or not */
-    i= os_callout_queued(&callout_func_test.cf_c);
-    TEST_ASSERT(i == 0);
-
-    /* Arm the callout */
-    i = os_callout_reset(&callout_func_test.cf_c, OS_TICKS_PER_SEC/ 50);
-    TEST_ASSERT_FATAL(i == 0);
-
-    /* Should say whether callout is armed or not */
-    i = os_callout_queued(&callout_func_test.cf_c);
-    TEST_ASSERT(i == 1);
-
-    /* Send the callout */ 
-    os_time_delay(OS_TICKS_PER_SEC );
-}
-
-/* This is the callout to receive data */
-void
-callout_task_receive( void *arg)
-{
-    int i;
-    struct os_event *event;
-    struct os_callout_func *callout;
-    os_time_t now;
-    os_time_t tm;
-    os_sr_t sr; 
-    /* Recieve using the os_eventq_poll */
-    event = os_eventq_poll(&callout_func_test.cf_c.c_evq, 1, OS_WAIT_FOREVER);
-    TEST_ASSERT(event->ev_type ==  OS_EVENT_T_TIMER);
-    TEST_ASSERT(event->ev_arg == NULL);
-    callout = (struct os_callout_func *)event;
-    TEST_ASSERT(callout->cf_func == my_callout_func);
-
-    /* Should say whether callout is armed or not */
-    i = os_callout_queued(&callout_func_test.cf_c);
-    TEST_ASSERT(i == 0);
-
-    OS_ENTER_CRITICAL(sr);
-    now = os_time_get();
-    tm = os_callout_wakeup_ticks(now);
-    TEST_ASSERT(tm == OS_TIMEOUT_NEVER);
-    OS_EXIT_CRITICAL(sr);
-    
-    /* Finishes the test when OS has been started */
-    os_test_restart();
-
-}
-
-/* This is callout to send the stop_callout */
-void
-callout_task_stop_send( void *arg)
-{
-    int k;
-    int j;    
-     /* Should say whether callout is armed or not */
-    for(k = 0; k<MULTI_SIZE; k++){
-        j = os_callout_queued(&callout_func_stop_test[k].cf_c);
-        TEST_ASSERT(j == 0);
-    }
-
-
-    /* Show that  callout is not armed after calling callout_stop */
-    for(k = 0; k<MULTI_SIZE; k++){
-        os_callout_stop(&callout_func_stop_test[k].cf_c);
-        j = os_callout_queued(&callout_func_stop_test[k].cf_c);
-        TEST_ASSERT(j == 0);
-    }
-    /* Arm the callout */
-    for(k = 0; k<MULTI_SIZE; k++){
-        j = os_callout_reset(&callout_func_stop_test[k].cf_c, OS_TICKS_PER_SEC/ 50);
-        TEST_ASSERT_FATAL(j == 0);
-    }
-    os_time_delay( OS_TICKS_PER_SEC );
-}
-
-/* This is the callout to receive stop_callout data */
-void
-callout_task_stop_receive( void *arg )
-{
-    int k;
-    struct os_event *event;
-    struct os_callout_func *callout;
-    /* Recieving using the os_eventq_poll */
-    for(k=0; k<MULTI_SIZE; k++){
-        event = os_eventq_poll(&callout_func_stop_test[k].cf_c.c_evq, 1,
-           OS_WAIT_FOREVER);
-        TEST_ASSERT(event->ev_type ==  OS_EVENT_T_TIMER);
-        TEST_ASSERT(event->ev_arg == NULL);
-        callout = (struct os_callout_func *)event;
-        TEST_ASSERT(callout->cf_func == my_callout_stop_func);
-
-
-     }
-     
-    /* Show that event is removed from the queued after calling callout_stop */
-    for(k=0; k<MULTI_SIZE; k++){
-        os_callout_stop(&callout_func_stop_test[k].cf_c);
-        /* Testing that the event has been removed from queue */
-        TEST_ASSERT_FATAL(1); 
-     }
-    /* Finishes the test when OS has been started */
-    os_test_restart();
-
-}
-
-/* This is a callout task to send data */
-void
-callout_task_stop_speak( void *arg )
-{
-    int i;
-    /* Arm the callout */
-    i = os_callout_reset(&callout_func_speak.cf_c, OS_TICKS_PER_SEC/ 50);
-    TEST_ASSERT_FATAL(i == 0);
-
-    /* should say whether callout is armed or not */
-    i = os_callout_queued(&callout_func_speak.cf_c);
-    TEST_ASSERT(i == 1);
-
-    os_callout_stop(&callout_func_speak.cf_c);
-    
-    /* Send the callout */ 
-    os_time_delay(OS_TICKS_PER_SEC/ 100 );
-    /* Finishes the test when OS has been started */
-    os_test_restart();
-}
-
-void
-callout_task_stop_listen( void *arg )
-{
-    struct os_event *event;
-    struct os_callout_func *callout;
-    event = os_eventq_get(callout_func_speak.cf_c.c_evq);
-    TEST_ASSERT_FATAL(0);
-    callout = (struct os_callout_func *)event;
-    TEST_ASSERT(callout->cf_func == my_callout_speak_func);
-
-}
-
-/* Test case to test the basics of the callout */
-TEST_CASE(callout_test)
-{
-
-    /* Initializing the OS */
-    os_init();
-    
-    /* Initialize the sending task */
-    os_task_init(&callout_task_struct_send, "callout_task_send",
-        callout_task_send, NULL, SEND_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
-        callout_task_stack_send, CALLOUT_STACK_SIZE);
-
-    /* Initialize the receive task */
-    os_task_init(&callout_task_struct_receive, "callout_task_receive",
-        callout_task_receive, NULL, RECEIVE_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
-        callout_task_stack_receive, CALLOUT_STACK_SIZE);
-
-    os_eventq_init(&callout_evq);
-    
-    /* Initialize the callout function */
-    os_callout_func_init(&callout_func_test, &callout_evq,
-        my_callout_func, NULL);
-
-    /* Does not return until OS_restart is called */
-    os_start();
-}
-
-/* Test case of the callout_task_stop */
-TEST_CASE(callout_test_stop)
-{
-    int k;
-    /* Initializing the OS */
-    os_init();
-
-    /* Initialize the sending task */
-    os_task_init(&callout_task_struct_stop_send, "callout_task_stop_send",
-        callout_task_stop_send, NULL, SEND_STOP_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
-        callout_task_stack_stop_send, CALLOUT_STACK_SIZE);
-
-    /* Initialize the receiving task */
-    os_task_init(&callout_task_struct_stop_receive, "callout_task_stop_receive",
-        callout_task_stop_receive, NULL, RECEIVE_STOP_CALLOUT_TASK_PRIO,
-        OS_WAIT_FOREVER, callout_task_stack_stop_receive, CALLOUT_STACK_SIZE);
-
-    for(k = 0; k< MULTI_SIZE; k++){
-        os_eventq_init(&callout_stop_evq[k]);
-    }
-    
-    /* Initialize the callout function */
-    for(k = 0; k<MULTI_SIZE; k++){
-        os_callout_func_init(&callout_func_stop_test[k], &callout_stop_evq[k],
-           my_callout_stop_func, NULL);
-    }
-
-    /* Does not return until OS_restart is called */
-    os_start();
-
-}
-
-/* Test case to test case for speak and listen */
-TEST_CASE(callout_test_speak)
-{
-    /* Initializing the OS */
-    os_init();
-    
-    /* Initialize the sending task */
-    os_task_init(&callout_task_struct_speak, "callout_task_speak",
-        callout_task_stop_speak, NULL, SPEAK_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
-        callout_task_stack_speak, CALLOUT_STACK_SIZE);
-
-    /* Initialize the receive task */
-    os_task_init(&callout_task_struct_listen, "callout_task_listen",
-        callout_task_stop_listen, NULL, LISTEN_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
-        callout_task_stack_listen, CALLOUT_STACK_SIZE);
-
-    os_eventq_init(&callout_evq);
-    
-    /* Initialize the callout function */
-    os_callout_func_init(&callout_func_speak, &callout_evq,
-        my_callout_speak_func, NULL);    
-    /* Does not return until OS_restart is called */
-    os_start();
-
-}
-
-TEST_SUITE(os_callout_test_suite)
-{   
-    callout_test();
-    callout_test_stop();
-    callout_test_speak();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/os/test/src/callout_test.c
----------------------------------------------------------------------
diff --git a/libs/os/test/src/callout_test.c b/libs/os/test/src/callout_test.c
new file mode 100644
index 0000000..4e3811d
--- /dev/null
+++ b/libs/os/test/src/callout_test.c
@@ -0,0 +1,330 @@
+/**
+ * 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 "os/os_eventq.h"
+#include "os/os_callout.h"
+#include "os/os_time.h"
+
+/* Task 1 for sending */
+#define CALLOUT_STACK_SIZE        (5120)
+#define SEND_CALLOUT_TASK_PRIO        (1)
+struct os_task callout_task_struct_send;
+os_stack_t callout_task_stack_send[CALLOUT_STACK_SIZE];
+
+#define RECEIVE_CALLOUT_TASK_PRIO        (2)
+struct os_task callout_task_struct_receive;
+os_stack_t callout_task_stack_receive[CALLOUT_STACK_SIZE];
+
+/* Delearing variables for callout_func */
+struct os_callout_func callout_func_test;
+
+/* The event to be sent*/
+struct os_eventq callout_evq;
+struct os_event callout_ev;
+
+/* The callout_stop task */
+#define SEND_STOP_CALLOUT_TASK_PRIO        (3)
+struct os_task callout_task_struct_stop_send;
+os_stack_t callout_task_stack_stop_send[CALLOUT_STACK_SIZE];
+
+#define RECEIVE_STOP_CALLOUT_TASK_PRIO        (4)
+struct os_task callout_task_struct_stop_receive;
+os_stack_t callout_task_stack_stop_receive[CALLOUT_STACK_SIZE];
+
+/* Delearing variables for callout_stop_func */
+#define MULTI_SIZE    (2)
+struct os_callout_func callout_func_stop_test[MULTI_SIZE];
+
+/* The event to be sent*/
+struct os_eventq callout_stop_evq[MULTI_SIZE];
+struct os_event callout_stop_ev;
+
+/* Declearing varables for callout_speak */
+#define SPEAK_CALLOUT_TASK_PRIO        (5)
+struct os_task callout_task_struct_speak;
+os_stack_t callout_task_stack_speak[CALLOUT_STACK_SIZE];
+
+/* Declearing varaibles for listen */
+#define LISTEN_CALLOUT_TASK_PRIO        (6)
+struct os_task callout_task_struct_listen;
+os_stack_t callout_task_stack_listen[CALLOUT_STACK_SIZE];
+
+struct os_callout_func callout_func_speak;
+
+/* Global variables to be used by the callout functions */
+int p;
+int q;
+int t;
+/* This is the function for callout_init*/
+void
+my_callout_func(void *arg)
+{
+    p = 4;
+}
+
+/* This is the function for callout_init of stop test_case*/
+void
+my_callout_stop_func(void *arg)
+{
+    q = 1;
+}
+/* This is the function for callout_init for speak test_case*/
+void
+my_callout_speak_func(void *arg)
+{
+    t = 2;
+}
+
+/* This is a callout task to send data */
+void
+callout_task_send(void *arg )
+{
+   int i;
+    /* Should say whether callout is armed or not */
+    i= os_callout_queued(&callout_func_test.cf_c);
+    TEST_ASSERT(i == 0);
+
+    /* Arm the callout */
+    i = os_callout_reset(&callout_func_test.cf_c, OS_TICKS_PER_SEC/ 50);
+    TEST_ASSERT_FATAL(i == 0);
+
+    /* Should say whether callout is armed or not */
+    i = os_callout_queued(&callout_func_test.cf_c);
+    TEST_ASSERT(i == 1);
+
+    /* Send the callout */ 
+    os_time_delay(OS_TICKS_PER_SEC );
+}
+
+/* This is the callout to receive data */
+void
+callout_task_receive( void *arg)
+{
+    int i;
+    struct os_event *event;
+    struct os_callout_func *callout;
+    os_time_t now;
+    os_time_t tm;
+    os_sr_t sr; 
+    /* Recieve using the os_eventq_poll */
+    event = os_eventq_poll(&callout_func_test.cf_c.c_evq, 1, OS_WAIT_FOREVER);
+    TEST_ASSERT(event->ev_type ==  OS_EVENT_T_TIMER);
+    TEST_ASSERT(event->ev_arg == NULL);
+    callout = (struct os_callout_func *)event;
+    TEST_ASSERT(callout->cf_func == my_callout_func);
+
+    /* Should say whether callout is armed or not */
+    i = os_callout_queued(&callout_func_test.cf_c);
+    TEST_ASSERT(i == 0);
+
+    OS_ENTER_CRITICAL(sr);
+    now = os_time_get();
+    tm = os_callout_wakeup_ticks(now);
+    TEST_ASSERT(tm == OS_TIMEOUT_NEVER);
+    OS_EXIT_CRITICAL(sr);
+    
+    /* Finishes the test when OS has been started */
+    os_test_restart();
+
+}
+
+/* This is callout to send the stop_callout */
+void
+callout_task_stop_send( void *arg)
+{
+    int k;
+    int j;    
+     /* Should say whether callout is armed or not */
+    for(k = 0; k<MULTI_SIZE; k++){
+        j = os_callout_queued(&callout_func_stop_test[k].cf_c);
+        TEST_ASSERT(j == 0);
+    }
+
+
+    /* Show that  callout is not armed after calling callout_stop */
+    for(k = 0; k<MULTI_SIZE; k++){
+        os_callout_stop(&callout_func_stop_test[k].cf_c);
+        j = os_callout_queued(&callout_func_stop_test[k].cf_c);
+        TEST_ASSERT(j == 0);
+    }
+    /* Arm the callout */
+    for(k = 0; k<MULTI_SIZE; k++){
+        j = os_callout_reset(&callout_func_stop_test[k].cf_c, OS_TICKS_PER_SEC/ 50);
+        TEST_ASSERT_FATAL(j == 0);
+    }
+    os_time_delay( OS_TICKS_PER_SEC );
+}
+
+/* This is the callout to receive stop_callout data */
+void
+callout_task_stop_receive( void *arg )
+{
+    int k;
+    struct os_event *event;
+    struct os_callout_func *callout;
+    /* Recieving using the os_eventq_poll */
+    for(k=0; k<MULTI_SIZE; k++){
+        event = os_eventq_poll(&callout_func_stop_test[k].cf_c.c_evq, 1,
+           OS_WAIT_FOREVER);
+        TEST_ASSERT(event->ev_type ==  OS_EVENT_T_TIMER);
+        TEST_ASSERT(event->ev_arg == NULL);
+        callout = (struct os_callout_func *)event;
+        TEST_ASSERT(callout->cf_func == my_callout_stop_func);
+
+
+     }
+     
+    /* Show that event is removed from the queued after calling callout_stop */
+    for(k=0; k<MULTI_SIZE; k++){
+        os_callout_stop(&callout_func_stop_test[k].cf_c);
+        /* Testing that the event has been removed from queue */
+        TEST_ASSERT_FATAL(1); 
+     }
+    /* Finishes the test when OS has been started */
+    os_test_restart();
+
+}
+
+/* This is a callout task to send data */
+void
+callout_task_stop_speak( void *arg )
+{
+    int i;
+    /* Arm the callout */
+    i = os_callout_reset(&callout_func_speak.cf_c, OS_TICKS_PER_SEC/ 50);
+    TEST_ASSERT_FATAL(i == 0);
+
+    /* should say whether callout is armed or not */
+    i = os_callout_queued(&callout_func_speak.cf_c);
+    TEST_ASSERT(i == 1);
+
+    os_callout_stop(&callout_func_speak.cf_c);
+    
+    /* Send the callout */ 
+    os_time_delay(OS_TICKS_PER_SEC/ 100 );
+    /* Finishes the test when OS has been started */
+    os_test_restart();
+}
+
+void
+callout_task_stop_listen( void *arg )
+{
+    struct os_event *event;
+    struct os_callout_func *callout;
+    event = os_eventq_get(callout_func_speak.cf_c.c_evq);
+    TEST_ASSERT_FATAL(0);
+    callout = (struct os_callout_func *)event;
+    TEST_ASSERT(callout->cf_func == my_callout_speak_func);
+
+}
+
+/* Test case to test the basics of the callout */
+TEST_CASE(callout_test)
+{
+
+    /* Initializing the OS */
+    os_init();
+    
+    /* Initialize the sending task */
+    os_task_init(&callout_task_struct_send, "callout_task_send",
+        callout_task_send, NULL, SEND_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
+        callout_task_stack_send, CALLOUT_STACK_SIZE);
+
+    /* Initialize the receive task */
+    os_task_init(&callout_task_struct_receive, "callout_task_receive",
+        callout_task_receive, NULL, RECEIVE_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
+        callout_task_stack_receive, CALLOUT_STACK_SIZE);
+
+    os_eventq_init(&callout_evq);
+    
+    /* Initialize the callout function */
+    os_callout_func_init(&callout_func_test, &callout_evq,
+        my_callout_func, NULL);
+
+    /* Does not return until OS_restart is called */
+    os_start();
+}
+
+/* Test case of the callout_task_stop */
+TEST_CASE(callout_test_stop)
+{
+    int k;
+    /* Initializing the OS */
+    os_init();
+
+    /* Initialize the sending task */
+    os_task_init(&callout_task_struct_stop_send, "callout_task_stop_send",
+        callout_task_stop_send, NULL, SEND_STOP_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
+        callout_task_stack_stop_send, CALLOUT_STACK_SIZE);
+
+    /* Initialize the receiving task */
+    os_task_init(&callout_task_struct_stop_receive, "callout_task_stop_receive",
+        callout_task_stop_receive, NULL, RECEIVE_STOP_CALLOUT_TASK_PRIO,
+        OS_WAIT_FOREVER, callout_task_stack_stop_receive, CALLOUT_STACK_SIZE);
+
+    for(k = 0; k< MULTI_SIZE; k++){
+        os_eventq_init(&callout_stop_evq[k]);
+    }
+    
+    /* Initialize the callout function */
+    for(k = 0; k<MULTI_SIZE; k++){
+        os_callout_func_init(&callout_func_stop_test[k], &callout_stop_evq[k],
+           my_callout_stop_func, NULL);
+    }
+
+    /* Does not return until OS_restart is called */
+    os_start();
+
+}
+
+/* Test case to test case for speak and listen */
+TEST_CASE(callout_test_speak)
+{
+    /* Initializing the OS */
+    os_init();
+    
+    /* Initialize the sending task */
+    os_task_init(&callout_task_struct_speak, "callout_task_speak",
+        callout_task_stop_speak, NULL, SPEAK_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
+        callout_task_stack_speak, CALLOUT_STACK_SIZE);
+
+    /* Initialize the receive task */
+    os_task_init(&callout_task_struct_listen, "callout_task_listen",
+        callout_task_stop_listen, NULL, LISTEN_CALLOUT_TASK_PRIO, OS_WAIT_FOREVER,
+        callout_task_stack_listen, CALLOUT_STACK_SIZE);
+
+    os_eventq_init(&callout_evq);
+    
+    /* Initialize the callout function */
+    os_callout_func_init(&callout_func_speak, &callout_evq,
+        my_callout_speak_func, NULL);    
+    /* Does not return until OS_restart is called */
+    os_start();
+
+}
+
+TEST_SUITE(os_callout_test_suite)
+{   
+    callout_test();
+    callout_test_stop();
+    callout_test_speak();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/shell/src/shell.c
----------------------------------------------------------------------
diff --git a/libs/shell/src/shell.c b/libs/shell/src/shell.c
index 18f040a..1cb88de 100644
--- a/libs/shell/src/shell.c
+++ b/libs/shell/src/shell.c
@@ -24,6 +24,7 @@
 #include "sysinit/sysinit.h"
 #include "syscfg/syscfg.h"
 #include "console/console.h"
+#include "console/prompt.h"
 #include "os/os.h"
 #include "os/endian.h"
 #include "util/base64.h"
@@ -554,9 +555,7 @@ shell_init(void)
     SYSINIT_PANIC_ASSERT(rc == 0);
 
     rc = shell_cmd_register(&g_shell_prompt_cmd);
-    if (rc != 0) {
-        goto err;
-    }
+    SYSINIT_PANIC_ASSERT(rc == 0);
     
     rc = shell_cmd_register(&g_shell_os_tasks_display_cmd);
     SYSINIT_PANIC_ASSERT(rc == 0);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/util/src/test/hex_test.c
----------------------------------------------------------------------
diff --git a/libs/util/src/test/hex_test.c b/libs/util/src/test/hex_test.c
deleted file mode 100644
index 8183425..0000000
--- a/libs/util/src/test/hex_test.c
+++ /dev/null
@@ -1,125 +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/hex.h"
-
-TEST_CASE(hex2str)
-{
-    int i;
-    char *ret;
-    char cmp_data[8];
-
-    struct {
-        char *in;
-        int inlen;
-        char *out;
-        int outlen;
-    } test_data[] = {
-        [0] = {
-            .in = "\x01",
-            .inlen = 1,
-            .out = "01",
-            .outlen = 2,
-        },
-        [1] = {
-            .in = "\xaf\xf2",
-            .inlen = 2,
-            .out = "aff2",
-            .outlen = 4,
-        }
-    };
-
-    for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
-        ret = hex_format(test_data[i].in, test_data[i].inlen,
-          cmp_data, sizeof(cmp_data));
-        TEST_ASSERT(ret == cmp_data);
-        TEST_ASSERT(strlen(cmp_data) == test_data[i].outlen);
-        TEST_ASSERT(!strcmp(test_data[i].out, cmp_data));
-    }
-
-    /*
-     * Test not enough space. Must have space for '\0' at the end.
-     */
-    ret = hex_format("\x01\x02", 2, cmp_data, 1);
-    TEST_ASSERT(ret == NULL);
-
-    ret = hex_format("\x01\x02", 2, cmp_data, 2);
-    TEST_ASSERT(ret == NULL);
-}
-
-TEST_CASE(str2hex)
-{
-    int i;
-    char cmp_data[8];
-    int rc;
-
-    struct {
-        char *in;
-        int inlen;
-        char *out;
-        int outlen;
-    } test_data[] = {
-        [0] = {
-            .in = "01",
-            .inlen = 2,
-            .out = "\x01",
-            .outlen = 1,
-        },
-        [1] = {
-            .in = "AfF2",
-            .inlen = 4,
-            .out = "\xaf\xf2",
-            .outlen = 2,
-        }
-    };
-
-    for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
-        rc = hex_parse(test_data[i].in, test_data[i].inlen,
-          cmp_data, sizeof(cmp_data));
-        TEST_ASSERT(rc == test_data[i].outlen);
-        TEST_ASSERT(!memcmp(test_data[i].out, cmp_data, rc));
-    }
-
-    /*
-     * Test invalid input
-     */
-    rc = hex_parse("HJ", 2, cmp_data, sizeof(cmp_data));
-    TEST_ASSERT(rc < 0);
-
-    rc = hex_parse("a", 1, cmp_data, sizeof(cmp_data));
-    TEST_ASSERT(rc < 0);
-
-    rc = hex_parse("0102", 4, cmp_data, 1);
-    TEST_ASSERT(rc < 0);
-
-    /*
-     * This should be valid.
-     */
-    rc = hex_parse("0102", 4, cmp_data, 2);
-    TEST_ASSERT(rc == 2);
-}
-
-TEST_SUITE(hex_fmt_test_suite)
-{
-    hex2str();
-    str2hex();
-}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/libs/util/test/src/hex_test.c
----------------------------------------------------------------------
diff --git a/libs/util/test/src/hex_test.c b/libs/util/test/src/hex_test.c
new file mode 100644
index 0000000..8183425
--- /dev/null
+++ b/libs/util/test/src/hex_test.c
@@ -0,0 +1,125 @@
+/**
+ * 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/hex.h"
+
+TEST_CASE(hex2str)
+{
+    int i;
+    char *ret;
+    char cmp_data[8];
+
+    struct {
+        char *in;
+        int inlen;
+        char *out;
+        int outlen;
+    } test_data[] = {
+        [0] = {
+            .in = "\x01",
+            .inlen = 1,
+            .out = "01",
+            .outlen = 2,
+        },
+        [1] = {
+            .in = "\xaf\xf2",
+            .inlen = 2,
+            .out = "aff2",
+            .outlen = 4,
+        }
+    };
+
+    for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
+        ret = hex_format(test_data[i].in, test_data[i].inlen,
+          cmp_data, sizeof(cmp_data));
+        TEST_ASSERT(ret == cmp_data);
+        TEST_ASSERT(strlen(cmp_data) == test_data[i].outlen);
+        TEST_ASSERT(!strcmp(test_data[i].out, cmp_data));
+    }
+
+    /*
+     * Test not enough space. Must have space for '\0' at the end.
+     */
+    ret = hex_format("\x01\x02", 2, cmp_data, 1);
+    TEST_ASSERT(ret == NULL);
+
+    ret = hex_format("\x01\x02", 2, cmp_data, 2);
+    TEST_ASSERT(ret == NULL);
+}
+
+TEST_CASE(str2hex)
+{
+    int i;
+    char cmp_data[8];
+    int rc;
+
+    struct {
+        char *in;
+        int inlen;
+        char *out;
+        int outlen;
+    } test_data[] = {
+        [0] = {
+            .in = "01",
+            .inlen = 2,
+            .out = "\x01",
+            .outlen = 1,
+        },
+        [1] = {
+            .in = "AfF2",
+            .inlen = 4,
+            .out = "\xaf\xf2",
+            .outlen = 2,
+        }
+    };
+
+    for (i = 0; i < sizeof(test_data) / sizeof(test_data[0]); i++) {
+        rc = hex_parse(test_data[i].in, test_data[i].inlen,
+          cmp_data, sizeof(cmp_data));
+        TEST_ASSERT(rc == test_data[i].outlen);
+        TEST_ASSERT(!memcmp(test_data[i].out, cmp_data, rc));
+    }
+
+    /*
+     * Test invalid input
+     */
+    rc = hex_parse("HJ", 2, cmp_data, sizeof(cmp_data));
+    TEST_ASSERT(rc < 0);
+
+    rc = hex_parse("a", 1, cmp_data, sizeof(cmp_data));
+    TEST_ASSERT(rc < 0);
+
+    rc = hex_parse("0102", 4, cmp_data, 1);
+    TEST_ASSERT(rc < 0);
+
+    /*
+     * This should be valid.
+     */
+    rc = hex_parse("0102", 4, cmp_data, 2);
+    TEST_ASSERT(rc == 2);
+}
+
+TEST_SUITE(hex_fmt_test_suite)
+{
+    hex2str();
+    str2hex();
+}

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/net/nimble/controller/pkg.yml
----------------------------------------------------------------------
diff --git a/net/nimble/controller/pkg.yml b/net/nimble/controller/pkg.yml
index 19846bb..7b5a4eb 100644
--- a/net/nimble/controller/pkg.yml
+++ b/net/nimble/controller/pkg.yml
@@ -173,7 +173,7 @@ pkg.syscfg_defs:
             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'
+        value: 'MYNEWT_VAL_BLE_LL_CFG_FEAT_LE_ENCRYPTION'
 
     BLE_LL_CFG_FEAT_DATA_LEN_EXT:
         description: >

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/8dffea7d/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 64f5e23..34c1f04 100644
--- a/net/nimble/controller/src/ble_ll_adv.c
+++ b/net/nimble/controller/src/ble_ll_adv.c
@@ -1019,7 +1019,7 @@ ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
 {
     int valid;
     uint8_t pyld_len;
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
     uint8_t resolved;
 #endif
     uint8_t addr_type;
@@ -1030,7 +1030,7 @@ ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
 
     /* Check filter policy. */
     valid = 0;
-#if (BLE_LL_CFG_FEAT_LL_PRIVACY == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
     resolved = BLE_MBUF_HDR_RESOLVED(hdr);
 #endif
     advsm = &g_ble_ll_adv_sm;
@@ -1052,7 +1052,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 (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
             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;
@@ -1066,7 +1066,7 @@ ble_ll_adv_conn_req_rxd(uint8_t *rxbuf, struct ble_mbuf_hdr *hdr)
     }
 
     if (valid) {
-#if (MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY) == 1)
+#if MYNEWT_VAL(BLE_LL_CFG_FEAT_LL_PRIVACY)
         if (resolved) {
             /* Retain the resolvable private address that we received. */
             memcpy(advsm->adv_rpa, inita, BLE_DEV_ADDR_LEN);



[17/50] [abbrv] incubator-mynewt-core git commit: Merge branch 'develop' into sterly_refactor

Posted by vi...@apache.org.
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/c5901fcc/net/nimble/host/test/src/ble_hs_test_util.h
----------------------------------------------------------------------
diff --cc net/nimble/host/test/src/ble_hs_test_util.h
index 5a821cd,0000000..c798916
mode 100644,000000..100644
--- a/net/nimble/host/test/src/ble_hs_test_util.h
+++ b/net/nimble/host/test/src/ble_hs_test_util.h
@@@ -1,177 -1,0 +1,197 @@@
 +/**
 + * 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;
 +};
 +
++#define BLE_HS_TEST_UTIL_L2CAP_HCI_HDR(handle, pb, len) \
++    ((struct hci_data_hdr) {                            \
++        .hdh_handle_pb_bc = ((handle)  << 0) |          \
++                            ((pb)      << 12),          \
++        .hdh_len = (len)                                \
++    })
++
 +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,
++void ble_hs_test_util_create_conn(uint16_t handle, const 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);
++void ble_hs_test_util_set_ack_disconnect(uint8_t hci_status);
 +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_mtu_cmd(uint16_t conn_handle, int is_req,
++                                     uint16_t mtu);
 +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_verify_tx_write_rsp(void);
++void ble_hs_test_util_verify_tx_mtu_cmd(int is_req, uint16_t mtu);
++void ble_hs_test_util_verify_tx_err_rsp(uint8_t req_op, uint16_t handle,
++                                        uint8_t error_code);
++uint8_t ble_hs_test_util_verify_tx_l2cap_update_req(
++    struct ble_l2cap_sig_update_params *params);
++int ble_hs_test_util_rx_l2cap_update_rsp(uint16_t conn_handle,
++                                         uint8_t id, uint16_t result);
++void ble_hs_test_util_verify_tx_l2cap_update_rsp(uint8_t exp_id,
++                                                 uint16_t exp_result);
 +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/c5901fcc/net/nimble/host/test/src/ble_l2cap_test.c
----------------------------------------------------------------------
diff --cc net/nimble/host/test/src/ble_l2cap_test.c
index 69db2f8,0000000..9410b3d
mode 100644,000000..100644
--- a/net/nimble/host/test/src/ble_l2cap_test.c
+++ b/net/nimble/host/test/src/ble_l2cap_test.c
@@@ -1,690 -1,0 +1,587 @@@
 +/**
 + * 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 uint16_t ble_l2cap_test_update_conn_handle;
 +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_conn_handle = BLE_HS_CONN_HANDLE_NONE;
 +    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(
++    hci_hdr = BLE_HS_TEST_UTIL_L2CAP_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,
++    hci_hdr = BLE_HS_TEST_UTIL_L2CAP_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,
++    hci_hdr = BLE_HS_TEST_UTIL_L2CAP_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);
++    hci_hdr = BLE_HS_TEST_UTIL_L2CAP_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);
++    rc = ble_hs_test_util_rx_l2cap_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);
++    ble_hs_test_util_verify_tx_l2cap_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));
++        TEST_ASSERT(!ble_gap_dbg_update_active(2));
 +    }
 +}
 +
 +static void
- ble_l2cap_test_util_update_cb(int status, void *arg)
++ble_l2cap_test_util_update_cb(uint16_t conn_handle, int status, void *arg)
 +{
++    ble_l2cap_test_update_conn_handle = conn_handle;
 +    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);
++    id = ble_hs_test_util_verify_tx_l2cap_update_req(&params);
 +
 +    /* Receive response from peer. */
-     rc = ble_l2cap_test_util_rx_update_rsp(2, id, !peer_accepts);
++    rc = ble_hs_test_util_rx_l2cap_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);
++    id = ble_hs_test_util_verify_tx_l2cap_update_req(&params);
 +
 +    /* Receive response from peer with incorrect ID. */
-     rc = ble_l2cap_test_util_rx_update_rsp(2, id + 1, 0);
++    rc = ble_hs_test_util_rx_l2cap_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);
++    rc = ble_hs_test_util_rx_l2cap_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;
 +}


[46/50] [abbrv] incubator-mynewt-core git commit: MYNEWT-391 STM32F4 ADC driver and ADC HAL support

Posted by vi...@apache.org.
MYNEWT-391 STM32F4 ADC driver and ADC HAL support

- Changing driver to work with config
- Adding deinit gpio to STM32F4 HAL
- Adding support for ADCs to the bsp and adding a default config for
  them
- The linked script for the olimex used CCRAM for _ccram_start, we
  should be using CCM instead.
- Resolving some compiler warnings.


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

Branch: refs/heads/sterly_refactor
Commit: 82767ae9af389bc15f9769f1a02312923b52443f
Parents: 082a033
Author: Vipul Rahane <vi...@runtime.io>
Authored: Tue Sep 20 18:03:01 2016 -0700
Committer: Vipul Rahane <vi...@runtime.io>
Committed: Thu Sep 22 20:21:03 2016 -0700

----------------------------------------------------------------------
 apps/sblinky/pkg.yml                            |   4 -
 apps/sblinky/src/main.c                         | 176 +++++--
 .../include/adc_stm32f4/adc_stm32f4.h           |  36 +-
 drivers/adc/adc_stm32f4/pkg.yml                 |   4 +-
 drivers/adc/adc_stm32f4/src/adc_stm32f4.c       | 522 +++++++++++++++++--
 drivers/adc/include/adc/adc.h                   |   5 +
 drivers/adc/pkg.yml                             |   2 +-
 .../olimex_stm32-e407_devboard.ld               |   2 +-
 hw/bsp/olimex_stm32-e407_devboard/pkg.yml       |  33 +-
 hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c  | 163 ++++++
 .../include/mcu/stm32f4xx_mynewt_hal.h          |   1 +
 hw/mcu/stm/stm32f4xx/pkg.yml                    |   2 +-
 hw/mcu/stm/stm32f4xx/src/hal_gpio.c             |  33 +-
 hw/mcu/stm/stm32f4xx/src/hal_spi.c              |   4 +-
 14 files changed, 874 insertions(+), 113 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/apps/sblinky/pkg.yml
----------------------------------------------------------------------
diff --git a/apps/sblinky/pkg.yml b/apps/sblinky/pkg.yml
index 9bc7c96..a1bab7e 100644
--- a/apps/sblinky/pkg.yml
+++ b/apps/sblinky/pkg.yml
@@ -24,12 +24,8 @@ pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
 
-pkg.req_apis:
-    - ADC_HW_IMPL
-
 pkg.deps:
     - libs/newtmgr
-    - hw/mcu/nordic
     - drivers/adc
     - fs/nffs
     - libs/console/full

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/apps/sblinky/src/main.c
----------------------------------------------------------------------
diff --git a/apps/sblinky/src/main.c b/apps/sblinky/src/main.c
index f9799be..280f410 100755
--- a/apps/sblinky/src/main.c
+++ b/apps/sblinky/src/main.c
@@ -32,24 +32,33 @@
 #ifdef ARCH_sim
 #include <mcu/mcu_sim.h>
 #endif
-#include "nrf.h"
-#include "app_util_platform.h"
-#include "app_error.h"
 
 #ifdef NRF51
+#include "nrf.h"
 #include <adc_nrf51/adc_nrf51.h>
 #include "nrf_drv_adc.h"
+#include "app_util_platform.h"
+#include "app_error.h"
 nrf_drv_adc_config_t adc_config = NRF_DRV_ADC_DEFAULT_CONFIG;
 nrf_drv_adc_channel_t g_nrf_adc_chan =
     NRF_DRV_ADC_DEFAULT_CHANNEL(NRF_ADC_CONFIG_INPUT_2);
 #endif
 
 #ifdef NRF52
+#include "nrf.h"
 #include <adc_nrf52/adc_nrf52.h>
 #include "nrf_drv_saadc.h"
+#include "app_util_platform.h"
+#include "app_error.h"
 nrf_drv_saadc_config_t adc_config = NRF_DRV_SAADC_DEFAULT_CONFIG;
 #endif
 
+#ifdef STM32F4
+#include "stm32f4xx_hal_dma.h"
+#include "stm32f4xx_hal_adc.h"
+#include <adc_stm32f4/adc_stm32f4.h>
+#endif
+
 #if defined(NRF52) || defined(NRF51)
 /* The spi txrx callback */
 struct sblinky_spi_cb_arg
@@ -162,7 +171,7 @@ sblinky_spi_irqs_handler(void *arg, int len)
 void
 sblinky_spi_cfg(int spi_num)
 {
-    int spi_id;
+    int spi_id = 0;
     struct hal_spi_settings my_spi;
 
 #if MYNEWT_VAL(SPI_MASTER)
@@ -189,8 +198,6 @@ sblinky_spi_cfg(int spi_num)
     hal_spi_config(spi_id, &my_spi);
 }
 
-struct adc_dev my_dev;
-
 #if 0
 #define SAADC_SAMPLES_IN_BUFFER (4)
 static nrf_saadc_value_t       m_buffer_pool[2][SAADC_SAMPLES_IN_BUFFER];
@@ -227,29 +234,40 @@ saadc_test(void)
 }
 #endif
 
-#define ADC_NUMBER_SAMPLES (2)
+
+#define ADC_NUMBER_SAMPLES (16)
 #define ADC_NUMBER_CHANNELS (1)
 
+#if MYNEWT_VAL(ADC_3)
+
+#define STM32F4_ADC_DEFAULT_CHAN_CFG {\
+    .Channel = ADC_CHANNEL_4,\
+    .Rank = 1,\
+    .SamplingTime = ADC_SAMPLETIME_144CYCLES,\
+    .Offset = 0\
+}
+
+ADC_ChannelConfTypeDef adc_chan_cfg = STM32F4_ADC_DEFAULT_CHAN_CFG;
+
 uint8_t *sample_buffer1;
 uint8_t *sample_buffer2;
 
-int adc_result;
-int my_result_mv;
+int adc3_result;
+int my_result_mv3[ADC_NUMBER_SAMPLES];
 
 int
-adc_read_event(struct adc_dev *dev, void *arg, uint8_t etype,
+adc3_read_event(struct adc_dev *dev, void *arg, uint8_t etype,
         void *buffer, int buffer_len)
 {
     int i;
-    //int result;
     int rc;
 
     for (i = 0; i < ADC_NUMBER_SAMPLES; i++) {
-        rc = adc_buf_read(dev, buffer, buffer_len, i, &adc_result);
+        rc = adc_buf_read(dev, buffer, buffer_len, i, &adc3_result);
         if (rc != 0) {
             goto err;
         }
-        my_result_mv = adc_result_mv(dev, 0, adc_result);
+        my_result_mv3[i] = adc_result_mv(dev, ADC_CHANNEL_4, adc3_result);
     }
 
     adc_buf_release(dev, buffer, buffer_len);
@@ -258,13 +276,57 @@ adc_read_event(struct adc_dev *dev, void *arg, uint8_t etype,
 err:
     return (rc);
 }
+#endif
+
+#if MYNEWT_VAL(ADC_1)
+
+#define STM32F4_ADC_DEFAULT_CHAN_CFG10 {\
+    .Channel = ADC_CHANNEL_10,\
+    .Rank = 1,\
+    .SamplingTime = ADC_SAMPLETIME_144CYCLES,\
+    .Offset = 0\
+}
+
+ADC_ChannelConfTypeDef adc_chan_cfg10 = STM32F4_ADC_DEFAULT_CHAN_CFG10;
+
+int adc1_result;
+int my_result_mv1[ADC_NUMBER_SAMPLES];
+uint8_t *sample_buffer3;
+uint8_t *sample_buffer4;
+int
+adc1_read_event(struct adc_dev *dev, void *arg, uint8_t etype,
+        void *buffer, int buffer_len)
+{
+    int i;
+    int rc;
+
+    for (i = 0; i < ADC_NUMBER_SAMPLES; i++) {
+        rc = adc_buf_read(dev, buffer, buffer_len, i, &adc1_result);
+        if (rc != 0) {
+            goto err;
+        }
+        my_result_mv1[i] = adc_result_mv(dev, ADC_CHANNEL_10, adc1_result);
+    }
+
+    adc_buf_release(dev, buffer, buffer_len);
+
+    return (0);
+err:
+    return (rc);
+}
+#endif
 
 void
 task1_handler(void *arg)
 {
-    int rc;
     struct os_task *t;
-    struct adc_dev *adc;
+#if MYNEWT_VAL(ADC_3)
+    struct adc_dev *adc3;
+#endif
+#if MYNEWT_VAL(ADC_1)
+    struct adc_dev *adc1;
+#endif
+
 #if MYNEWT_VAL(SPI_MASTER)
     int i;
     uint8_t last_val;
@@ -281,14 +343,31 @@ task1_handler(void *arg)
     g_led_pin = LED_BLINK_PIN;
     hal_gpio_init_out(g_led_pin, 1);
 
-    adc = (struct adc_dev *) os_dev_open("adc0", 1, &adc_config);
-    assert(adc != NULL);
+#if MYNEWT_VAL(ADC_3)
+    adc3 = (struct adc_dev *) os_dev_open("adc3", 1, NULL);
+    assert(adc3 != NULL);
+#endif
+
+#if MYNEWT_VAL(ADC_1)
+    adc1 = (struct adc_dev *) os_dev_open("adc1", 1, NULL);
+    assert(adc1 != NULL);
+#endif
 
 #ifdef NRF51
-    adc_chan_config(adc, 0, &g_nrf_adc_chan);
+    adc_chan_config(adc3, 0, &g_nrf_adc_chan);
 #endif
+
 #ifdef NRF52
-    adc_chan_config(adc, 0, &cc);
+    adc_chan_config(adc3, 0, &cc);
+#endif
+
+#ifdef STM32F4
+#if MYNEWT_VAL(ADC_3)
+    adc_chan_config(adc3, ADC_CHANNEL_4, &adc_chan_cfg);
+#endif
+#if MYNEWT_VAL(ADC_1)
+    adc_chan_config(adc1, ADC_CHANNEL_10, &adc_chan_cfg10);
+#endif
 #endif
 
 #if MYNEWT_VAL(SPI_MASTER)
@@ -298,6 +377,7 @@ task1_handler(void *arg)
     hal_spi_enable(0);
 
 #if 1
+    int rc;
     /* Send some bytes in a non-blocking manner to SPI using tx val */
     g_spi_tx_buf[0] = 0xde;
     g_spi_tx_buf[1] = 0xad;
@@ -347,30 +427,56 @@ task1_handler(void *arg)
     rc = hal_spi_txrx(SPI_SLAVE_ID, g_spi_tx_buf, g_spi_rx_buf, 32);
     assert(rc == 0);
 #endif
+#if MYNEWT_VAL(ADC_3)
+    sample_buffer1 = malloc(adc_buf_size(adc3, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
+    sample_buffer2 = malloc(adc_buf_size(adc3, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
+    memset(sample_buffer1, 0, adc_buf_size(adc3, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
+    memset(sample_buffer2, 0, adc_buf_size(adc3, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
+#endif
 
-    sample_buffer1 = malloc(adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
-    sample_buffer2 = malloc(adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
-    memset(sample_buffer1, 0, adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
-    memset(sample_buffer2, 0, adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
+#if MYNEWT_VAL(ADC_1)
+    sample_buffer3 = malloc(adc_buf_size(adc1, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
+    sample_buffer4 = malloc(adc_buf_size(adc1, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
+    memset(sample_buffer3, 0, adc_buf_size(adc1, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
+    memset(sample_buffer4, 0, adc_buf_size(adc1, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
+#endif
 
 #if 1
-    adc_buf_set(adc, sample_buffer1, sample_buffer2,
-            adc_buf_size(adc, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
-    adc_event_handler_set(adc, adc_read_event, (void *) NULL);
+#if MYNEWT_VAL(ADC_3)
+    adc_buf_set(adc3, sample_buffer1, sample_buffer2,
+            adc_buf_size(adc3, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
+    adc_event_handler_set(adc3, adc3_read_event, NULL);
 #endif
 
-#if 0
-    rc = adc_chan_read(adc, 0, &g_result);
-    assert(rc == 0);
-    g_result_mv = adc_result_mv(adc, 0, g_result);
+#if MYNEWT_VAL(ADC_1)
+    adc_buf_set(adc1, sample_buffer3, sample_buffer4,
+            adc_buf_size(adc1, ADC_NUMBER_CHANNELS, ADC_NUMBER_SAMPLES));
+    adc_event_handler_set(adc1, adc1_read_event, NULL);
+#endif
 #endif
 
     while (1) {
         t = os_sched_get_current_task();
         assert(t->t_func == task1_handler);
 
-        adc_sample(adc);
+#if 0
+        int rc;
+        rc = adc_chan_read(adc, 4, &g_result);
+        assert(rc == 0);
+        g_result_mv = adc_result_mv(adc, 4, g_result);
+#endif
+        int rc;
+        rc = OS_OK;
+
+#if MYNEWT_VAL(ADC_3)
+        rc = adc_sample(adc3);
+        assert(rc == OS_OK);
+#endif
 
+#if MYNEWT_VAL(ADC_1)
+        rc = adc_sample(adc1);
+        assert(rc == OS_OK);
+#endif
         ++g_task1_loops;
 
 #if MYNEWT_VAL(SPI_MASTER)
@@ -399,8 +505,13 @@ task1_handler(void *arg)
         /* Release semaphore to task 2 */
         os_sem_release(&g_test_sem);
     }
+#if MYNEWT_VAL(ADC_3)
+    os_dev_close((struct os_dev *) adc3);
+#endif
 
-    os_dev_close((struct os_dev *) adc);
+#if MYNEWT_VAL(ADC_1)
+    os_dev_close((struct os_dev *) adc1);
+#endif
 }
 
 void
@@ -478,7 +589,6 @@ main(int argc, char **argv)
     /* Initialize the sblinky log. */
     log_console_handler_init(&log_console_handler);
     log_register("sblinky", &my_log, &log_console_handler);
-
 #if 0
     saadc_test();
 #endif

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h b/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h
index e5a494f..ad5bc86 100644
--- a/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h
+++ b/drivers/adc/adc_stm32f4/include/adc_stm32f4/adc_stm32f4.h
@@ -22,16 +22,36 @@
 
 #include <adc/adc.h>
 
+/* Mynewt HAL Port pin mapping */
+#define ADC123_CH0_PIN    0
+#define ADC123_CH1_PIN    1
+#define ADC123_CH2_PIN    2
+#define ADC123_CH3_PIN    3
+#define ADC12_CH4_PIN     4
+#define ADC12_CH5_PIN     5
+#define ADC12_CH6_PIN     6
+#define ADC12_CH7_PIN     7
+#define ADC12_CH8_PIN     16
+#define ADC12_CH9_PIN     17
+#define ADC3_CH4_PIN      86
+#define ADC3_CH5_PIN      87
+#define ADC3_CH6_PIN      88
+#define ADC3_CH7_PIN      89
+#define ADC3_CH8_PIN      90
+#define ADC3_CH9_PIN      83
+#define ADC123_CH10_PIN   32
+#define ADC123_CH11_PIN   33
+#define ADC123_CH12_PIN   34
+#define ADC123_CH13_PIN   35
+#define ADC12_CH14_PIN    36
+#define ADC12_CH15_PIN    37
+#define ADC3_CH14_PIN     84
+#define ADC3_CH15_PIN     85
+
 struct stm32f4_adc_dev_cfg {
     uint8_t sac_chan_count;
-    uint8_t *sac_chans;
-    ADC_InitTypeDef sac_init;
-};
-
-struct stm32f4_adc_dev {
-    struct adc_dev sad_dev;
-    ADC_TypeDef *sad_adc;
-    ADC_InitTypeDef *sad_init;
+    void *sac_chans;
+    ADC_HandleTypeDef *sac_adc_handle;
 };
 
 int stm32f4_adc_dev_init(struct os_dev *, void *);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/drivers/adc/adc_stm32f4/pkg.yml
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/pkg.yml b/drivers/adc/adc_stm32f4/pkg.yml
index 205dfa8..bb24fd7 100644
--- a/drivers/adc/adc_stm32f4/pkg.yml
+++ b/drivers/adc/adc_stm32f4/pkg.yml
@@ -22,9 +22,9 @@ pkg.description: ADC driver for the STM32F4 series of processors.
 pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
-pkg.features: 
+pkg.features:
     - ADC_STM32F4
-pkg.apis: 
+pkg.apis:
     - ADC_HW_IMPL
 pkg.deps.TEST:
    - hw/hal

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
----------------------------------------------------------------------
diff --git a/drivers/adc/adc_stm32f4/src/adc_stm32f4.c b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
index 95a068e..029c267 100644
--- a/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
+++ b/drivers/adc/adc_stm32f4/src/adc_stm32f4.c
@@ -23,13 +23,371 @@
 #include <assert.h>
 #include <os/os.h>
 #include <bsp/cmsis_nvic.h>
+#include "stm32f4xx_hal_dma.h"
+#include "stm32f4xx_hal_adc.h"
+#include "stm32f4xx_hal_rcc.h"
+#include "stm32f4xx_hal_cortex.h"
+#include "stm32f4xx_hal.h"
+#include "adc_stm32f4/adc_stm32f4.h"
+#include "stm32f4xx_hal_dma.h"
+#include "mcu/stm32f4xx_mynewt_hal.h"
+
+#define STM32F4_IS_DMA_ADC_CHANNEL(CHANNEL) ((CHANNEL) <= DMA_CHANNEL_2)
+
+static DMA_HandleTypeDef *dma_handle[5];
+static struct adc_dev *adc_dma[5];
 
 struct stm32f4_adc_stats {
     uint16_t adc_events;
-    uint16_t adc_events_failed;
+    uint16_t adc_DMA_xfer_failed;
+    uint16_t adc_DMA_xfer_aborted;
+    uint16_t adc_DMA_xfer_complete;
+    uint16_t adc_error;
 };
+
 static struct stm32f4_adc_stats stm32f4_adc_stats;
 
+static void
+stm32f4_adc_clk_enable(ADC_HandleTypeDef *hadc) {
+    uintptr_t adc_addr = (uintptr_t)hadc->Instance;
+
+    switch (adc_addr) {
+        case (uintptr_t)ADC1:
+            __HAL_RCC_ADC1_CLK_ENABLE();
+            break;
+        case (uintptr_t)ADC2:
+            __HAL_RCC_ADC2_CLK_ENABLE();
+            break;
+        case (uintptr_t)ADC3:
+            __HAL_RCC_ADC3_CLK_ENABLE();
+            break;
+        default:
+            assert(0);
+    }
+}
+
+static void
+stm32f4_adc_clk_disable(ADC_HandleTypeDef *hadc) {
+    uintptr_t adc_addr = (uintptr_t)hadc->Instance;
+
+    switch (adc_addr) {
+        case (uintptr_t)ADC1:
+            __HAL_RCC_ADC1_CLK_DISABLE();
+            break;
+        case (uintptr_t)ADC2:
+            __HAL_RCC_ADC2_CLK_DISABLE();
+            break;
+        case (uintptr_t)ADC3:
+            __HAL_RCC_ADC3_CLK_DISABLE();
+            break;
+        default:
+            assert(0);
+    }
+}
+
+static GPIO_InitTypeDef
+stm32f4_resolve_adc_gpio(ADC_HandleTypeDef *adc, uint8_t cnum)
+{
+    uintptr_t adc_addr = (uintptr_t)adc->Instance;
+    uint32_t pin;
+    GPIO_InitTypeDef gpio_td;
+
+    switch (adc_addr) {
+        case (uintptr_t)ADC1:
+        case (uintptr_t)ADC2:
+            switch(cnum) {
+                case ADC_CHANNEL_4:
+                    pin = ADC12_CH4_PIN;
+                    goto done;
+                case ADC_CHANNEL_5:
+                    pin = ADC12_CH5_PIN;
+                    goto done;
+                case ADC_CHANNEL_6:
+                    pin = ADC12_CH6_PIN;
+                    goto done;
+                case ADC_CHANNEL_7:
+                    pin = ADC12_CH7_PIN;
+                    goto done;
+                case ADC_CHANNEL_8:
+                    pin = ADC12_CH8_PIN;
+                    goto done;
+                case ADC_CHANNEL_9:
+                    pin = ADC12_CH9_PIN;
+                    goto done;
+                case ADC_CHANNEL_14:
+                    pin = ADC12_CH14_PIN;
+                    goto done;
+                case ADC_CHANNEL_15:
+                    pin = ADC12_CH15_PIN;
+                    goto done;
+            }
+        case (uintptr_t)ADC3:
+            switch(cnum) {
+                case ADC_CHANNEL_0:
+                    pin = ADC123_CH0_PIN;
+                    goto done;
+                case ADC_CHANNEL_1:
+                    pin = ADC123_CH1_PIN;
+                    goto done;
+                case ADC_CHANNEL_2:
+                    pin = ADC123_CH2_PIN;
+                    goto done;
+                case ADC_CHANNEL_3:
+                    pin = ADC123_CH3_PIN;
+                    goto done;
+                case ADC_CHANNEL_4:
+                    pin = ADC3_CH4_PIN;
+                    goto done;
+                case ADC_CHANNEL_5:
+                    pin = ADC3_CH5_PIN;
+                    goto done;
+                case ADC_CHANNEL_6:
+                    pin = ADC3_CH6_PIN;
+                    goto done;
+                case ADC_CHANNEL_7:
+                    pin = ADC3_CH7_PIN;
+                    goto done;
+                case ADC_CHANNEL_8:
+                    pin = ADC3_CH8_PIN;
+                    goto done;
+                case ADC_CHANNEL_9:
+                    pin = ADC3_CH9_PIN;
+                    goto done;
+                case ADC_CHANNEL_10:
+                    pin = ADC123_CH10_PIN;
+                    goto done;
+                case ADC_CHANNEL_11:
+                    pin = ADC123_CH11_PIN;
+                    goto done;
+                case ADC_CHANNEL_12:
+                    pin = ADC123_CH12_PIN;
+                    goto done;
+                case ADC_CHANNEL_13:
+                    pin = ADC123_CH13_PIN;
+                    goto done;
+                case ADC_CHANNEL_14:
+                    pin = ADC3_CH14_PIN;
+                    goto done;
+                case ADC_CHANNEL_15:
+                    pin = ADC3_CH15_PIN;
+                    goto done;
+            }
+        default:
+            assert(0);
+    }
+done:
+    gpio_td = (GPIO_InitTypeDef){
+        .Pin = pin,
+        .Mode = GPIO_MODE_ANALOG,
+        .Pull = GPIO_NOPULL,
+        .Alternate = pin
+    };
+    return gpio_td;
+}
+
+static IRQn_Type
+stm32f4_resolve_adc_dma_irq(DMA_HandleTypeDef *hdma)
+{
+    uintptr_t stream_addr = (uintptr_t)hdma->Instance;
+
+    assert(STM32F4_IS_DMA_ADC_CHANNEL(hdma->Init.Channel));
+
+    switch(stream_addr) {
+        /* DMA2 */
+        case (uintptr_t)DMA2_Stream0:
+            return DMA2_Stream0_IRQn;
+        case (uintptr_t)DMA2_Stream1:
+            return DMA2_Stream1_IRQn;
+        case (uintptr_t)DMA2_Stream2:
+            return DMA2_Stream2_IRQn;
+        case (uintptr_t)DMA2_Stream3:
+            return DMA2_Stream3_IRQn;
+        case (uintptr_t)DMA2_Stream4:
+            return DMA2_Stream4_IRQn;
+        default:
+            assert(0);
+    }
+}
+
+static void
+dma2_stream0_irq_handler(void)
+{
+    HAL_DMA_IRQHandler(dma_handle[0]);
+}
+
+static void
+dma2_stream1_irq_handler(void)
+{
+    HAL_DMA_IRQHandler(dma_handle[1]);
+}
+
+static void
+dma2_stream2_irq_handler(void)
+{
+    HAL_DMA_IRQHandler(dma_handle[2]);
+}
+
+static void
+dma2_stream3_irq_handler(void)
+{
+    HAL_DMA_IRQHandler(dma_handle[3]);
+}
+
+static void
+dma2_stream4_irq_handler(void)
+{
+    HAL_DMA_IRQHandler(dma_handle[4]);
+}
+
+uint32_t
+stm32f4_resolve_adc_dma_irq_handler(DMA_HandleTypeDef *hdma)
+{
+    switch((uintptr_t)hdma->Instance) {
+        /* DMA2 */
+        case (uintptr_t)DMA2_Stream0:
+            return (uint32_t)&dma2_stream0_irq_handler;
+        case (uintptr_t)DMA2_Stream1:
+            return (uint32_t)&dma2_stream1_irq_handler;
+        case (uintptr_t)DMA2_Stream2:
+            return (uint32_t)&dma2_stream2_irq_handler;
+        case (uintptr_t)DMA2_Stream3:
+            return (uint32_t)&dma2_stream3_irq_handler;
+        case (uintptr_t)DMA2_Stream4:
+            return (uint32_t)&dma2_stream4_irq_handler;
+        default:
+            assert(0);
+    }
+}
+
+static int
+stm32f4_resolve_dma_handle_idx(DMA_HandleTypeDef *hdma)
+{
+    uintptr_t stream_addr = (uintptr_t)hdma->Instance;
+    return ((stream_addr & 0xFF) - ((uintptr_t)DMA2_Stream0_BASE & 0xFF))/0x18;
+}
+
+static void
+stm32f4_xfer_error_cb(DMA_HandleTypeDef *hdma)
+{
+    /* DMA transfer error callback */
+    stm32f4_adc_stats.adc_DMA_xfer_failed++;
+}
+
+static void
+stm32f4_xfer_abort_cb(DMA_HandleTypeDef *hdma)
+{
+    /* DMA transfer Abort callback */
+    stm32f4_adc_stats.adc_DMA_xfer_aborted++;
+}
+
+void
+HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
+{
+    stm32f4_adc_stats.adc_error++;
+}
+
+void
+HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc)
+{
+    int rc;
+    struct adc_dev *adc;
+    DMA_HandleTypeDef *hdma;
+
+    assert(hadc);
+    hdma = hadc->DMA_Handle;
+
+    ++stm32f4_adc_stats.adc_DMA_xfer_complete;
+
+    adc = adc_dma[stm32f4_resolve_dma_handle_idx(hdma)];
+
+    rc = adc->ad_event_handler_func(adc, NULL, ADC_EVENT_RESULT, adc->primarybuf,
+                                    adc->buflen);
+    if (rc) {
+        ++stm32f4_adc_stats.adc_error;
+    }
+}
+
+
+//void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
+static void
+stm32f4_adc_dma_init(ADC_HandleTypeDef* hadc)
+{
+
+    DMA_HandleTypeDef *hdma;
+
+    assert(hadc);
+    hdma = hadc->DMA_Handle;
+
+    stm32f4_adc_clk_enable(hadc);
+    __HAL_RCC_DMA2_CLK_ENABLE();
+
+    HAL_DMA_Init(hdma);
+    dma_handle[stm32f4_resolve_dma_handle_idx(hdma)] = hdma;
+
+    if (HAL_DMA_RegisterCallback(hdma, HAL_DMA_XFER_ERROR_CB_ID,
+                                 stm32f4_xfer_error_cb) != HAL_OK) {
+        assert(0);
+    }
+
+    if (HAL_DMA_RegisterCallback(hdma, HAL_DMA_XFER_ABORT_CB_ID,
+                                 stm32f4_xfer_abort_cb) != HAL_OK) {
+        assert(0);
+    }
+
+    NVIC_SetPriority(stm32f4_resolve_adc_dma_irq(hdma),
+                     NVIC_EncodePriority(NVIC_GetPriorityGrouping(), 0, 0));
+    NVIC_SetVector(stm32f4_resolve_adc_dma_irq(hdma),
+                   stm32f4_resolve_adc_dma_irq_handler(hdma));
+    NVIC_EnableIRQ(stm32f4_resolve_adc_dma_irq(hdma));
+
+}
+
+static void
+stm32f4_adc_init(void *arg)
+{
+    struct stm32f4_adc_dev_cfg *adc_config = (struct stm32f4_adc_dev_cfg *)arg;
+    ADC_HandleTypeDef *hadc;
+
+    assert(adc_config);
+
+    hadc = adc_config->sac_adc_handle;
+
+    stm32f4_adc_dma_init(hadc);
+
+    if (HAL_ADC_Init(hadc) != HAL_OK) {
+        assert(0);
+    }
+}
+
+static void
+stm32f4_adc_uninit(struct adc_dev *dev)
+{
+    GPIO_InitTypeDef gpio_td;
+    DMA_HandleTypeDef *hdma;
+    ADC_HandleTypeDef *hadc;
+    struct stm32f4_adc_dev_cfg *cfg;
+    uint8_t cnum;
+
+    assert(dev);
+    cfg  = dev->adc_dev_cfg;
+    hadc = cfg->sac_adc_handle;
+    hdma = hadc->DMA_Handle;
+    cnum = dev->ad_chans->c_cnum;
+
+    gpio_td = stm32f4_resolve_adc_gpio(hadc, cnum);
+
+    if(hal_gpio_deinit_stm(gpio_td.Pin, &gpio_td)) {
+        assert(0);
+    }
+
+    __HAL_RCC_DMA2_CLK_DISABLE();
+    if (HAL_DMA_DeInit(hdma) != HAL_OK) {
+        assert(0);
+    }
+    stm32f4_adc_clk_disable(hadc);
+
+    NVIC_DisableIRQ(stm32f4_resolve_adc_dma_irq(hdma));
+}
 
 /**
  * Open the STM32F4 ADC device
@@ -47,9 +405,14 @@ static struct stm32f4_adc_stats stm32f4_adc_stats;
 static int
 stm32f4_adc_open(struct os_dev *odev, uint32_t wait, void *arg)
 {
+    DMA_HandleTypeDef *hdma;
+    ADC_HandleTypeDef *hadc;
+    struct stm32f4_adc_dev_cfg *cfg;
     struct adc_dev *dev;
     int rc;
 
+    assert(odev);
+    rc = OS_OK;
     dev = (struct adc_dev *) odev;
 
     if (os_started()) {
@@ -59,13 +422,22 @@ stm32f4_adc_open(struct os_dev *odev, uint32_t wait, void *arg)
         }
     }
 
-    if (odev->od_status & OS_DEV_STATUS_OPEN) {
+    if (odev->od_flags & OS_DEV_F_STATUS_OPEN) {
         os_mutex_release(&dev->ad_lock);
         rc = OS_EBUSY;
         goto err;
     }
 
-    return (0);
+
+    stm32f4_adc_init(dev->adc_dev_cfg);
+
+    cfg  = dev->adc_dev_cfg;
+    hadc = cfg->sac_adc_handle;
+    hdma = hadc->DMA_Handle;
+
+    adc_dma[stm32f4_resolve_dma_handle_idx(hdma)] = dev;
+
+    return (OS_OK);
 err:
     return (rc);
 }
@@ -85,20 +457,22 @@ stm32f4_adc_close(struct os_dev *odev)
 
     dev = (struct adc_dev *) odev;
 
+    stm32f4_adc_uninit(dev);
+
     if (os_started()) {
         os_mutex_release(&dev->ad_lock);
     }
 
-    return (0);
+    return (OS_OK);
 }
 
 /**
- * Configure an ADC channel on the Nordic ADC.
+ * Configure an ADC channel on the STM32F4 ADC.
  *
  * @param dev The ADC device to configure
  * @param cnum The channel on the ADC device to configure
  * @param cfgdata An opaque pointer to channel config, expected to be
- *                a nrf_saadc_channel_config_t
+ *                a ADC_ChannelConfTypeDef
  *
  * @return 0 on success, non-zero on failure.
  */
@@ -106,21 +480,38 @@ static int
 stm32f4_adc_configure_channel(struct adc_dev *dev, uint8_t cnum,
         void *cfgdata)
 {
-    uint16_t refmv;
-    uint8_t res;
     int rc;
+    ADC_HandleTypeDef *hadc;
+    struct stm32f4_adc_dev_cfg *cfg;
+    GPIO_InitTypeDef gpio_td;
+    struct adc_chan_config *chan_cfg;
 
-    /* XXX: Dummy values prior to implementation. */
-    res = 16;
-    refmv = 2800;
+    assert(dev != NULL && IS_ADC_CHANNEL(cnum));
+    cfg  = dev->adc_dev_cfg;
+    hadc = cfg->sac_adc_handle;
+    chan_cfg = cfg->sac_chans;
 
-    /* Store these values in channel definitions, for conversions to
-     * milivolts.
-     */
-    dev->ad_chans[cnum].c_res = res;
-    dev->ad_chans[cnum].c_refmv = refmv;
+    // Enable DMA, ADC and related GPIO ports clock
+    gpio_td = stm32f4_resolve_adc_gpio(hadc, cnum);
+    hal_gpio_init_stm(gpio_td.Pin, &gpio_td);
+
+
+    cfgdata = (ADC_ChannelConfTypeDef *)cfgdata;
+
+    if((rc = HAL_ADC_ConfigChannel(hadc, cfgdata)) != HAL_OK) {
+        goto err;
+    }
+
+    dev->ad_chans[cnum].c_res = chan_cfg->c_res;
+    dev->ad_chans[cnum].c_refmv = chan_cfg->c_refmv;
     dev->ad_chans[cnum].c_configured = 1;
 
+#if 0
+    if (HAL_ADC_Start_IT(hadc) != HAL_OK) {
+        assert(0);
+    }
+#endif
+
     return (0);
 err:
     return (rc);
@@ -129,32 +520,48 @@ err:
 /**
  * Set buffer to read data into.  Implementation of setbuffer handler.
  * Sets both the primary and secondary buffers for DMA.
+ *
+ * For our current implementation we are using DMA in circular mode
+ *
  */
 static int
 stm32f4_adc_set_buffer(struct adc_dev *dev, void *buf1, void *buf2,
-        int buf_len)
+        int buflen)
 {
     int rc;
 
-    rc = OS_EINVAL;
-    goto err;
+    rc = OS_OK;
+    buflen /= sizeof(uint16_t);
 
-    return (0);
-err:
-    return (rc);
+    assert(dev != NULL && buf1 != NULL);
+
+    dev->primarybuf = buf1;
+    dev->secondarybuf = buf2;
+    dev->buflen = buflen;
+
+    return rc;
 }
 
 static int
 stm32f4_adc_release_buffer(struct adc_dev *dev, void *buf, int buf_len)
 {
-    int rc;
+    ADC_HandleTypeDef *hadc;
+    struct stm32f4_adc_dev_cfg *cfg;
 
-    rc = OS_EINVAL;
-    goto err;
+    assert(dev);
+    cfg  = dev->adc_dev_cfg;
+    hadc = cfg->sac_adc_handle;
+
+    HAL_ADC_Stop_DMA(hadc);
+
+    if (dev->primarybuf == buf) {
+        if (dev->secondarybuf) {
+            dev->primarybuf = dev->secondarybuf;
+            dev->secondarybuf = buf;
+        }
+    }
 
     return (0);
-err:
-    return (rc);
 }
 
 /**
@@ -163,7 +570,24 @@ err:
 static int
 stm32f4_adc_sample(struct adc_dev *dev)
 {
-    return (0);
+    int rc;
+    ADC_HandleTypeDef *hadc;
+    struct stm32f4_adc_dev_cfg *cfg;
+
+    assert(dev != NULL && dev->primarybuf != NULL);
+    cfg  = dev->adc_dev_cfg;
+    hadc = cfg->sac_adc_handle;
+
+    rc = OS_EINVAL;
+
+    if (HAL_ADC_Start_DMA(hadc, dev->primarybuf, dev->buflen) != HAL_OK) {
+        goto err;
+    }
+
+    rc = OS_OK;
+
+err:
+    return rc;
 }
 
 /**
@@ -172,57 +596,67 @@ stm32f4_adc_sample(struct adc_dev *dev)
 static int
 stm32f4_adc_read_channel(struct adc_dev *dev, uint8_t cnum, int *result)
 {
-    int rc;
+    ADC_HandleTypeDef *hadc;
+    struct stm32f4_adc_dev_cfg *cfg;
 
-    rc = OS_EINVAL;
-    goto err;
+    assert(dev != NULL && result != NULL);
+    cfg  = dev->adc_dev_cfg;
+    hadc = cfg->sac_adc_handle;
 
-    return (0);
-err:
-    return (rc);
+    *result = HAL_ADC_GetValue(hadc);
+
+    return (OS_OK);
 }
 
 static int
 stm32f4_adc_read_buffer(struct adc_dev *dev, void *buf, int buf_len, int off,
-        int *result)
+                        int *result)
 {
-    return (0);
+
+    assert(off < buf_len);
+
+    *result = *((uint32_t *) buf + off);
+
+    return (OS_OK);
 }
 
 static int
 stm32f4_adc_size_buffer(struct adc_dev *dev, int chans, int samples)
 {
-    return (0 * chans * samples);
+    return (sizeof(uint32_t) * chans * samples);
 }
 
+#if 0
+void ADC_IRQHandler(void)
+{
+    HAL_ADC_IRQHandler(adc_handle);
+}
+#endif
+
 
 /**
  * Callback to initialize an adc_dev structure from the os device
- * initialization callback.  This sets up a nrf52_adc_device(), so
+ * initialization callback.  This sets up a stm32f4_adc_device(), so
  * that subsequent lookups to this device allow us to manipulate it.
  */
 int
 stm32f4_adc_dev_init(struct os_dev *odev, void *arg)
 {
-    struct stm32f4_adc_dev *sad;
     struct stm32f4_adc_dev_cfg *sac;
     struct adc_dev *dev;
     struct adc_driver_funcs *af;
 
-    sad = (struct stm32f4_adc_dev *) odev;
     sac = (struct stm32f4_adc_dev_cfg *) arg;
 
-    assert(sad != NULL);
     assert(sac != NULL);
 
-    dev = (struct adc_dev *) &sad->sad_dev;
+    dev = (struct adc_dev *)odev;
 
     os_mutex_init(&dev->ad_lock);
 
     /* Have a pointer to the init typedef from the configured
      * value.  This allows non-driver specific items to be configured.
      */
-    sad->sad_init = &sac->sac_init;
 
     dev->ad_chans = (void *) sac->sac_chans;
     dev->ad_chan_count = sac->sac_chan_count;
@@ -230,6 +664,7 @@ stm32f4_adc_dev_init(struct os_dev *odev, void *arg)
     OS_DEV_SETHANDLERS(odev, stm32f4_adc_open, stm32f4_adc_close);
 
     af = &dev->ad_funcs;
+    dev->adc_dev_cfg = arg;
 
     af->af_configure_channel = stm32f4_adc_configure_channel;
     af->af_sample = stm32f4_adc_sample;
@@ -242,4 +677,3 @@ stm32f4_adc_dev_init(struct os_dev *odev, void *arg)
     return (0);
 }
 
-

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/drivers/adc/include/adc/adc.h
----------------------------------------------------------------------
diff --git a/drivers/adc/include/adc/adc.h b/drivers/adc/include/adc/adc.h
index 01727c0..f0d4ad5 100644
--- a/drivers/adc/include/adc/adc.h
+++ b/drivers/adc/include/adc/adc.h
@@ -146,6 +146,7 @@ struct adc_chan_config {
     uint16_t c_refmv;
     uint8_t c_res;
     uint8_t c_configured;
+    uint8_t c_cnum;
 };
 
 struct adc_dev {
@@ -153,9 +154,13 @@ struct adc_dev {
     struct os_mutex ad_lock;
     struct adc_driver_funcs ad_funcs;
     struct adc_chan_config *ad_chans;
+    void *adc_dev_cfg;
     int ad_chan_count;
     adc_event_handler_func_t ad_event_handler_func;
     void *ad_event_handler_arg;
+    void *primarybuf;
+    void *secondarybuf;
+    int buflen;
 };
 
 int adc_chan_config(struct adc_dev *, uint8_t, void *);

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/drivers/adc/pkg.yml
----------------------------------------------------------------------
diff --git a/drivers/adc/pkg.yml b/drivers/adc/pkg.yml
index 94469bc..bf0205a 100644
--- a/drivers/adc/pkg.yml
+++ b/drivers/adc/pkg.yml
@@ -22,5 +22,5 @@ pkg.description: ADC driver interfaces
 pkg.author: "Apache Mynewt <de...@mynewt.incubator.apache.org>"
 pkg.homepage: "http://mynewt.apache.org/"
 pkg.keywords:
-pkg.req_apis: 
+pkg.req_apis:
     - ADC_HW_IMPL

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/hw/bsp/olimex_stm32-e407_devboard/olimex_stm32-e407_devboard.ld
----------------------------------------------------------------------
diff --git a/hw/bsp/olimex_stm32-e407_devboard/olimex_stm32-e407_devboard.ld b/hw/bsp/olimex_stm32-e407_devboard/olimex_stm32-e407_devboard.ld
index 4c1541d..b07d869 100755
--- a/hw/bsp/olimex_stm32-e407_devboard/olimex_stm32-e407_devboard.ld
+++ b/hw/bsp/olimex_stm32-e407_devboard/olimex_stm32-e407_devboard.ld
@@ -192,7 +192,7 @@ SECTIONS
     __HeapLimit = ORIGIN(RAM) + LENGTH(RAM);
 
     _ram_start = ORIGIN(RAM);
-    _ccram_start = ORIGIN(CCRAM);
+    _ccram_start = ORIGIN(CCM);
 
     /* .stack_dummy section doesn't contains any symbols. It is only
      * used for linker to calculate size of stack sections, and assign

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/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 a06aaf0..41c0674 100644
--- a/hw/bsp/olimex_stm32-e407_devboard/pkg.yml
+++ b/hw/bsp/olimex_stm32-e407_devboard/pkg.yml
@@ -36,13 +36,18 @@ pkg.debugscript: "olimex_stm32-e407_devboard_debug.sh"
 pkg.cflags:
     # STM SDK files require these defines.
     - '-DSTM32F407xx'
-
+    - '-DHAL_ADC_MODULE_ENABLED'
+    - '-DHAL_DMA_MODULE_ENABLED'
 pkg.deps:
     - hw/mcu/stm/stm32f4xx
     - libs/baselibc
 
-pkg.deps.ADC_0:
-    - drivers/adc/adc_nrf52
+pkg.deps.ADC_1:
+    - drivers/adc/adc_stm32f4
+pkg.deps.ADC_2:
+    - drivers/adc/adc_stm32f4
+pkg.deps.ADC_3:
+    - drivers/adc/adc_stm32f4
 
 pkg.deps.UART_0:
     - drivers/uart/uart_hal
@@ -55,19 +60,15 @@ pkg.syscfg_defs:
         description: 'TBD'
         value:  1000000
 
-    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'
-
+    ADC_1:
+        description: "ADC_1"
+        value:  1
+    ADC_2:
+        description: "ADC_2"
+        value:  1
+    ADC_3:
+        description: "ADC_3"
+        value:  1
     UART_0:
         description: 'TBD'
         value:  1

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c
----------------------------------------------------------------------
diff --git a/hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c b/hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c
index ec71236..39b0bac 100644
--- a/hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c
+++ b/hw/bsp/olimex_stm32-e407_devboard/src/os_bsp.c
@@ -30,6 +30,10 @@
 #include <uart_hal/uart_hal.h>
 
 #include "bsp/bsp.h"
+#include "syscfg/syscfg.h"
+#include "stm32f4xx_hal_dma.h"
+#include "stm32f4xx_hal_adc.h"
+#include <adc_stm32f4/adc_stm32f4.h>
 
 static struct flash_area bsp_flash_areas[] = {
     [FLASH_AREA_BOOTLOADER] = {
@@ -62,9 +66,155 @@ static struct flash_area bsp_flash_areas[] = {
 static struct uart_dev hal_uart0;
 
 /* XXX should not be here */
+
+#if MYNEWT_VAL(ADC_1)
+struct adc_dev my_dev_adc1;
+#endif
+
 struct stm32f4_uart_cfg;
+#if MYNEWT_VAL(ADC_3)
+struct adc_dev my_dev_adc3;
+#endif
+
 extern struct stm32f4_uart_cfg *bsp_uart_config(int port);
 
+/*****************ADC3 Config ***************/
+
+#if MYNEWT_VAL(ADC_3)
+
+ADC_HandleTypeDef adc_handle;
+
+#define STM32F4_DEFAULT_DMA_HANDLE21 {\
+    .Instance = DMA2_Stream1,\
+    .Init.Channel = DMA_CHANNEL_2,\
+    .Init.Direction = DMA_PERIPH_TO_MEMORY,\
+    .Init.PeriphInc = DMA_PINC_DISABLE,\
+    .Init.MemInc = DMA_MINC_ENABLE,\
+    .Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD,\
+    .Init.MemDataAlignment = DMA_MDATAALIGN_WORD,\
+    .Init.Mode = DMA_CIRCULAR,\
+    .Init.Priority = DMA_PRIORITY_HIGH,\
+    .Init.FIFOMode = DMA_FIFOMODE_DISABLE,\
+    .Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL,\
+    .Init.MemBurst = DMA_MBURST_SINGLE,\
+    .Init.PeriphBurst = DMA_PBURST_SINGLE,\
+    .Parent = &adc_handle,\
+}
+#endif
+
+#if MYNEWT_VAL(ADC_1)
+/*
+ * adc_handle is defined earlier because the DMA handle's
+ * parent needs to be pointing to the adc_handle
+ */
+ADC_HandleTypeDef adc_handle120;
+
+DMA_HandleTypeDef adc_dma_handle21 = STM32F4_DEFAULT_DMA_HANDLE21;
+
+#define STM32F4_DEFAULT_DMA_HANDLE20 {\
+    .Instance = DMA2_Stream0,\
+    .Init.Channel = DMA_CHANNEL_0,\
+    .Init.Direction = DMA_PERIPH_TO_MEMORY,\
+    .Init.PeriphInc = DMA_PINC_DISABLE,\
+    .Init.MemInc = DMA_MINC_ENABLE,\
+    .Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD,\
+    .Init.MemDataAlignment = DMA_MDATAALIGN_WORD,\
+    .Init.Mode = DMA_CIRCULAR,\
+    .Init.Priority = DMA_PRIORITY_HIGH,\
+    .Init.FIFOMode = DMA_FIFOMODE_DISABLE,\
+    .Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL,\
+    .Init.MemBurst = DMA_MBURST_SINGLE,\
+    .Init.PeriphBurst = DMA_PBURST_SINGLE,\
+    .Parent = &adc_handle120,\
+}
+
+DMA_HandleTypeDef adc_dma_handle20 = STM32F4_DEFAULT_DMA_HANDLE20;
+#endif
+
+
+#define STM32F4_ADC_DEFAULT_INIT_TD {\
+    .ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2,\
+    .Resolution = ADC_RESOLUTION12b,\
+    .DataAlign = ADC_DATAALIGN_RIGHT,\
+    .ScanConvMode = DISABLE,\
+    .EOCSelection = DISABLE,\
+    .ContinuousConvMode = ENABLE,\
+    .NbrOfConversion = 2,\
+    .DiscontinuousConvMode = DISABLE,\
+    .NbrOfDiscConversion = 0,\
+    .ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1,\
+    .ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE,\
+    .DMAContinuousRequests = ENABLE\
+}
+
+
+#if MYNEWT_VAL(ADC_3)
+
+#define STM32F4_DEFAULT_ADC_HANDLE {\
+    .Init = STM32F4_ADC_DEFAULT_INIT_TD,\
+    .Instance = ADC3,\
+    .NbrOfCurrentConversionRank = 0,\
+    .DMA_Handle = &adc_dma_handle21,\
+    .Lock = HAL_UNLOCKED,\
+    .State = 0,\
+    .ErrorCode = 0\
+}
+
+ADC_HandleTypeDef adc_handle = STM32F4_DEFAULT_ADC_HANDLE;
+
+#define STM32F4_ADC_DEFAULT_SAC_CHAN {\
+    .c_refmv = 3300,\
+    .c_res   = 12,\
+    .c_configured = 1,\
+    .c_cnum = ADC_CHANNEL_4\
+}
+
+struct adc_chan_config def_sac_chan = STM32F4_ADC_DEFAULT_SAC_CHAN;
+
+#define STM32F4_ADC_DEFAULT_CONFIG {\
+    .sac_chan_count = 16,\
+    .sac_chans = &def_sac_chan,\
+    .sac_adc_handle = &adc_handle,\
+}
+
+struct stm32f4_adc_dev_cfg adc_config = STM32F4_ADC_DEFAULT_CONFIG;
+#endif
+
+
+#if MYNEWT_VAL(ADC_1)
+
+/*****************ADC1 Config ***************/
+#define STM32F4_DEFAULT_ADC_HANDLE120 {\
+    .Init = STM32F4_ADC_DEFAULT_INIT_TD,\
+    .Instance = ADC1,\
+    .NbrOfCurrentConversionRank = 0,\
+    .DMA_Handle = &adc_dma_handle20,\
+    .Lock = HAL_UNLOCKED,\
+    .State = 0,\
+    .ErrorCode = 0\
+}
+
+ADC_HandleTypeDef adc_handle120 = STM32F4_DEFAULT_ADC_HANDLE120;
+
+#define STM32F4_ADC_DEFAULT_SAC_CHAN10 {\
+    .c_refmv = 3300,\
+    .c_res   = 12,\
+    .c_configured = 1,\
+    .c_cnum = ADC_CHANNEL_10\
+}
+
+struct adc_chan_config def_sac_chan10 = STM32F4_ADC_DEFAULT_SAC_CHAN10;
+
+#define STM32F4_ADC_DEFAULT_CONFIG_1_2_0_10 {\
+    .sac_chan_count = 16,\
+    .sac_chans = &def_sac_chan10,\
+    .sac_adc_handle = &adc_handle120,\
+}
+
+struct stm32f4_adc_dev_cfg adc_config12010 = STM32F4_ADC_DEFAULT_CONFIG_1_2_0_10;
+/*********************************************/
+#endif
+
 void _close(int fd);
 
 /*
@@ -97,5 +247,18 @@ bsp_init(void)
     rc = os_dev_create((struct os_dev *) &hal_uart0, CONSOLE_UART,
       OS_DEV_INIT_PRIMARY, 0, uart_hal_init, (void *)bsp_uart_config(0));
     assert(rc == 0);
+#if MYNEWT_VAL(ADC_3)
+    rc = os_dev_create((struct os_dev *) &my_dev_adc3, "adc3",
+            OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
+            stm32f4_adc_dev_init, &adc_config);
+    assert(rc == 0);
+#endif
+
+#if MYNEWT_VAL(ADC_1)
+    rc = os_dev_create((struct os_dev *) &my_dev_adc1, "adc1",
+            OS_DEV_INIT_KERNEL, OS_DEV_INIT_PRIO_DEFAULT,
+            stm32f4_adc_dev_init, &adc_config12010);
+    assert(rc == 0);
+#endif
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/hw/mcu/stm/stm32f4xx/include/mcu/stm32f4xx_mynewt_hal.h
----------------------------------------------------------------------
diff --git a/hw/mcu/stm/stm32f4xx/include/mcu/stm32f4xx_mynewt_hal.h b/hw/mcu/stm/stm32f4xx/include/mcu/stm32f4xx_mynewt_hal.h
index 33e82c6..c18decd 100644
--- a/hw/mcu/stm/stm32f4xx/include/mcu/stm32f4xx_mynewt_hal.h
+++ b/hw/mcu/stm/stm32f4xx/include/mcu/stm32f4xx_mynewt_hal.h
@@ -42,6 +42,7 @@
 
 
 int hal_gpio_init_stm(int pin, GPIO_InitTypeDef *cfg);
+int hal_gpio_deinit_stm(int pin, GPIO_InitTypeDef *cfg);
 
 struct stm32f4_hal_i2c_cfg {
     int sda_pin;

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/hw/mcu/stm/stm32f4xx/pkg.yml
----------------------------------------------------------------------
diff --git a/hw/mcu/stm/stm32f4xx/pkg.yml b/hw/mcu/stm/stm32f4xx/pkg.yml
index 952c6ab..83c0742 100644
--- a/hw/mcu/stm/stm32f4xx/pkg.yml
+++ b/hw/mcu/stm/stm32f4xx/pkg.yml
@@ -35,5 +35,5 @@ pkg.ign_dirs:
 
 pkg.deps: 
     - hw/hal 
-    - libs/cmsis-core 
+    - libs/cmsis-core
     - compiler/arm-none-eabi-m4

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/hw/mcu/stm/stm32f4xx/src/hal_gpio.c
----------------------------------------------------------------------
diff --git a/hw/mcu/stm/stm32f4xx/src/hal_gpio.c b/hw/mcu/stm/stm32f4xx/src/hal_gpio.c
index 8778229..9fcc3bd 100644
--- a/hw/mcu/stm/stm32f4xx/src/hal_gpio.c
+++ b/hw/mcu/stm/stm32f4xx/src/hal_gpio.c
@@ -60,7 +60,7 @@
  *          - Multiply by 16.
  *          - Add port pin number.
  *
- *      Ex: PE11 = (4 * 16) + 11 = 75.
+ *      Ex: PD11 = (3 * 16) + 11 = 59.
  *          PA0 = (0 * 16) + 0 = 0
  */
 #define GPIO_INDEX(pin)     ((pin) & 0x0F)
@@ -395,6 +395,37 @@ hal_gpio_init_stm(int pin, GPIO_InitTypeDef *cfg)
 }
 
 /**
+ * hal gpio deinit
+ *
+ * Called to deinitialize a gpio.
+ *
+ * @param pin
+ * @param cfg
+ *
+ * @return int
+ */
+int
+hal_gpio_deinit_stm(int pin, GPIO_InitTypeDef *cfg)
+{
+    int port;
+    uint32_t mcu_pin_mask;
+
+    /* Is this a valid pin? */
+    port = GPIO_PORT(pin);
+    if (port >= HAL_GPIO_NUM_PORTS) {
+        return -1;
+    }
+
+    mcu_pin_mask = GPIO_MASK(pin);
+    cfg->Pin = mcu_pin_mask;
+
+    /* Initialize pin as an input, setting proper mode */
+    HAL_GPIO_DeInit(portmap[port], cfg->Pin);
+
+    return 0;
+}
+
+/**
  * gpio init in
  *
  * Initializes the specified pin as an input

http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/82767ae9/hw/mcu/stm/stm32f4xx/src/hal_spi.c
----------------------------------------------------------------------
diff --git a/hw/mcu/stm/stm32f4xx/src/hal_spi.c b/hw/mcu/stm/stm32f4xx/src/hal_spi.c
index 314a24a..197cf2c 100644
--- a/hw/mcu/stm/stm32f4xx/src/hal_spi.c
+++ b/hw/mcu/stm/stm32f4xx/src/hal_spi.c
@@ -118,7 +118,7 @@ struct stm32f4_hal_spi *stm32f4_hal_spis[STM32F4_HAL_SPI_MAX] = {
     }
 
 int
-hal_spi_init(uint8_t spi_num, void *usercfg)
+hal_spi_init(int spi_num, void *usercfg, uint8_t spi_type)
 {
     struct stm32f4_hal_spi *spi;
     struct stm32f4_hal_spi_cfg *cfg;
@@ -238,7 +238,7 @@ stm32f4_spi_resolve_prescaler(uint8_t spi_num, uint32_t baudrate, uint32_t *pres
 }
 
 int
-hal_spi_config(uint8_t spi_num, struct hal_spi_settings *settings)
+hal_spi_config(int spi_num, struct hal_spi_settings *settings)
 {
     struct stm32f4_hal_spi *spi;
     SPI_InitTypeDef *init;