MYNEWT-711 testbench - Add BLE peripheral support
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/9624a798 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/9624a798 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/9624a798 Branch: refs/heads/master Commit: 9624a7988c1bb23dcd528deaa0c39a8ec5745252 Parents: bcaf2ff Author: Christopher Collins <ccoll...@apache.org> Authored: Wed Apr 5 14:44:24 2017 -0700 Committer: Christopher Collins <ccoll...@apache.org> Committed: Wed Apr 5 14:44:24 2017 -0700 ---------------------------------------------------------------------- apps/testbench/pkg.yml | 56 ++++---- apps/testbench/src/tbb.c | 280 ++++++++++++++++++++++++++++++++++++ apps/testbench/src/tbb.h | 43 ++++++ apps/testbench/src/testbench.c | 19 +-- apps/testbench/syscfg.yml | 16 ++- 5 files changed, 370 insertions(+), 44 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9624a798/apps/testbench/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/testbench/pkg.yml b/apps/testbench/pkg.yml index 28a25e5..b10bb0f 100644 --- a/apps/testbench/pkg.yml +++ b/apps/testbench/pkg.yml @@ -25,34 +25,36 @@ pkg.homepage: "http://mynewt.apache.org/" pkg.keywords: pkg.deps: - - sys/console/full - - mgmt/imgmgr - - mgmt/newtmgr - - mgmt/newtmgr/transport/nmgr_shell - - kernel/os - - boot/bootutil - - boot/split_app - - encoding/json/test -# - sys/shell - - sys/config - - sys/id - - sys/log/full - - sys/stats/full - - sys/flash_map - - sys/flash_map/test - - test/testutil - - kernel/os/test - - encoding/json/test - - test/crash_test - - test/runtest + - "@apache-mynewt-core/encoding/json/test" + - "@apache-mynewt-core/kernel/os" + - "@apache-mynewt-core/kernel/os/test" + - "@apache-mynewt-core/mgmt/imgmgr" + - "@apache-mynewt-core/mgmt/newtmgr" + - "@apache-mynewt-core/mgmt/newtmgr/transport/nmgr_shell" + - "@apache-mynewt-core/sys/config" + - "@apache-mynewt-core/sys/console/stub" + - "@apache-mynewt-core/sys/flash_map" + - "@apache-mynewt-core/sys/flash_map/test" + - "@apache-mynewt-core/sys/id" + - "@apache-mynewt-core/sys/log/full" + - "@apache-mynewt-core/sys/stats/full" + - "@apache-mynewt-core/test/crash_test" + - "@apache-mynewt-core/test/runtest" + - "@apache-mynewt-core/test/testutil" + +pkg.deps.TESTBENCH_BLE: + - "@apache-mynewt-core/mgmt/newtmgr/transport/ble" + - "@apache-mynewt-core/net/nimble/controller" + - "@apache-mynewt-core/net/nimble/host" + - "@apache-mynewt-core/net/nimble/host/services/gap" + - "@apache-mynewt-core/net/nimble/host/services/gatt" + - "@apache-mynewt-core/net/nimble/host/store/ram" + - "@apache-mynewt-core/net/nimble/transport/ram" pkg.deps.CONFIG_NFFS: - - fs/nffs - - fs/nffs/test + - "@apache-mynewt-core/fs/nffs" + - "@apache-mynewt-core/fs/nffs/test" pkg.deps.CONFIG_FCB: - - fs/fcb - - fs/fcb/test - - -# pkg.cflags: -DDEBUG_MALLOC + - "@apache-mynewt-core/fs/fcb" + - "@apache-mynewt-core/fs/fcb/test" http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9624a798/apps/testbench/src/tbb.c ---------------------------------------------------------------------- diff --git a/apps/testbench/src/tbb.c b/apps/testbench/src/tbb.c new file mode 100644 index 0000000..e0f4d8d --- /dev/null +++ b/apps/testbench/src/tbb.c @@ -0,0 +1,280 @@ +/* + * 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. + */ + +/** tbb - test bench BLE. */ + +#include "syscfg/syscfg.h" + +#if MYNEWT_VAL(TESTBENCH_BLE) + +#include <assert.h> +#include <string.h> +#include <stdio.h> +#include <errno.h> +#include "sysinit/sysinit.h" +#include "bsp/bsp.h" +#include "os/os.h" +#include "bsp/bsp.h" +#include "hal/hal_gpio.h" +#include "console/console.h" +#include "hal/hal_system.h" +#include "config/config.h" +#include "split/split.h" + +/* BLE */ +#include "nimble/ble.h" +#include "host/ble_hs.h" +#include "services/gap/ble_svc_gap.h" + +#include "tbb.h" + +static int tbb_gap_event(struct ble_gap_event *event, void *arg); + +static struct log tbb_log; + +/* tbb uses the first "peruser" log module. */ +#define TBB_LOG_MODULE (LOG_MODULE_PERUSER + 0) + +/* Convenience macro for logging to the tbb module. */ +#define TBB_LOG(lvl, ...) \ + LOG_ ## lvl(&tbb_log, TBB_LOG_MODULE, __VA_ARGS__) + +void +tbb_print_addr(const void *addr) +{ + const uint8_t *u8p; + + u8p = addr; + TBB_LOG(INFO, "%02x:%02x:%02x:%02x:%02x:%02x", + u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]); +} + +/** + * Logs information about a connection to the console. + */ +static void +tbb_print_conn_desc(struct ble_gap_conn_desc *desc) +{ + TBB_LOG(INFO, "handle=%d our_ota_addr_type=%d our_ota_addr=", + desc->conn_handle, desc->our_ota_addr.type); + tbb_print_addr(desc->our_ota_addr.val); + TBB_LOG(INFO, " our_id_addr_type=%d our_id_addr=", + desc->our_id_addr.type); + tbb_print_addr(desc->our_id_addr.val); + TBB_LOG(INFO, " peer_ota_addr_type=%d peer_ota_addr=", + desc->peer_ota_addr.type); + tbb_print_addr(desc->peer_ota_addr.val); + TBB_LOG(INFO, " peer_id_addr_type=%d peer_id_addr=", + desc->peer_id_addr.type); + tbb_print_addr(desc->peer_id_addr.val); + TBB_LOG(INFO, " conn_itvl=%d conn_latency=%d supervision_timeout=%d " + "encrypted=%d authenticated=%d bonded=%d\n", + desc->conn_itvl, desc->conn_latency, + desc->supervision_timeout, + desc->sec_state.encrypted, + desc->sec_state.authenticated, + desc->sec_state.bonded); +} + +/** + * Enables advertising with the following parameters: + * o General discoverable mode. + * o Undirected connectable mode. + */ +static void +tbb_advertise(void) +{ + struct ble_gap_adv_params adv_params; + struct ble_hs_adv_fields fields; + const char *name; + int rc; + + /** + * Set the advertisement data included in our advertisements: + * o Flags (indicates advertisement type and other general info). + * o Advertising tx power. + * o Device name. + * o 16-bit service UUIDs (alert notifications). + */ + + memset(&fields, 0, sizeof fields); + + /* Advertise two flags: + * o Discoverability in forthcoming advertisement (general) + * o BLE-only (BR/EDR unsupported). + */ + fields.flags = BLE_HS_ADV_F_DISC_GEN | + BLE_HS_ADV_F_BREDR_UNSUP; + + /* Indicate that the TX power level field should be included; have the + * stack fill this value automatically. This is done by assiging the + * special value BLE_HS_ADV_TX_PWR_LVL_AUTO. + */ + fields.tx_pwr_lvl_is_present = 1; + fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO; + + name = ble_svc_gap_device_name(); + fields.name = (uint8_t *)name; + fields.name_len = strlen(name); + fields.name_is_complete = 1; + + rc = ble_gap_adv_set_fields(&fields); + if (rc != 0) { + TBB_LOG(ERROR, "error setting advertisement data; rc=%d\n", rc); + return; + } + + /* Begin advertising. */ + memset(&adv_params, 0, sizeof adv_params); + adv_params.conn_mode = BLE_GAP_CONN_MODE_UND; + adv_params.disc_mode = BLE_GAP_DISC_MODE_GEN; + rc = ble_gap_adv_start(BLE_OWN_ADDR_PUBLIC, NULL, BLE_HS_FOREVER, + &adv_params, tbb_gap_event, NULL); + if (rc != 0) { + TBB_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc); + return; + } +} + +/** + * The nimble host executes this callback when a GAP event occurs. The + * application associates a GAP event callback with each connection that forms. + * tbb uses the same callback for all connections. + * + * @param event The type of event being signalled. + * @param ctxt Various information pertaining to the event. + * @param arg Application-specified argument; unuesd by + * tbb. + * + * @return 0 if the application successfully handled the + * event; nonzero on failure. The semantics + * of the return code is specific to the + * particular GAP event being signalled. + */ +static int +tbb_gap_event(struct ble_gap_event *event, void *arg) +{ + struct ble_gap_conn_desc desc; + int rc; + + switch (event->type) { + case BLE_GAP_EVENT_CONNECT: + /* A new connection was established or a connection attempt failed. */ + TBB_LOG(INFO, "connection %s; status=%d ", + event->connect.status == 0 ? "established" : "failed", + event->connect.status); + if (event->connect.status == 0) { + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + tbb_print_conn_desc(&desc); + } + TBB_LOG(INFO, "\n"); + + if (event->connect.status != 0) { + /* Connection failed; resume advertising. */ + tbb_advertise(); + } + return 0; + + case BLE_GAP_EVENT_DISCONNECT: + TBB_LOG(INFO, "disconnect; reason=%d ", event->disconnect.reason); + tbb_print_conn_desc(&event->disconnect.conn); + TBB_LOG(INFO, "\n"); + + /* Connection terminated; resume advertising. */ + tbb_advertise(); + return 0; + + case BLE_GAP_EVENT_CONN_UPDATE: + /* The central has updated the connection parameters. */ + TBB_LOG(INFO, "connection updated; status=%d ", + event->conn_update.status); + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + tbb_print_conn_desc(&desc); + TBB_LOG(INFO, "\n"); + return 0; + + case BLE_GAP_EVENT_ENC_CHANGE: + /* Encryption has been enabled or disabled for this connection. */ + TBB_LOG(INFO, "encryption change event; status=%d ", + event->enc_change.status); + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + tbb_print_conn_desc(&desc); + TBB_LOG(INFO, "\n"); + return 0; + + case BLE_GAP_EVENT_SUBSCRIBE: + TBB_LOG(INFO, "subscribe event; conn_handle=%d attr_handle=%d " + "reason=%d prevn=%d curn=%d previ=%d curi=%d\n", + event->subscribe.conn_handle, + event->subscribe.attr_handle, + event->subscribe.reason, + event->subscribe.prev_notify, + event->subscribe.cur_notify, + event->subscribe.prev_indicate, + event->subscribe.cur_indicate); + return 0; + + case BLE_GAP_EVENT_MTU: + TBB_LOG(INFO, "mtu update event; conn_handle=%d cid=%d mtu=%d\n", + event->mtu.conn_handle, + event->mtu.channel_id, + event->mtu.value); + return 0; + } + + return 0; +} + +static void +tbb_on_reset(int reason) +{ + TBB_LOG(ERROR, "Resetting state; reason=%d\n", reason); +} + +static void +tbb_on_sync(void) +{ + /* Begin advertising. */ + tbb_advertise(); +} + +void +tbb_init(void) +{ + int rc; + + /* Initialize the tbb log. */ + log_register("tbb", &tbb_log, &log_console_handler, NULL, + LOG_SYSLEVEL); + + /* Initialize the NimBLE host configuration. */ + log_register("ble_hs", &ble_hs_log, &log_console_handler, NULL, + LOG_SYSLEVEL); + ble_hs_cfg.reset_cb = tbb_on_reset; + ble_hs_cfg.sync_cb = tbb_on_sync; + + /* Set the default device name. */ + rc = ble_svc_gap_device_name_set(MYNEWT_VAL(TESTBENCH_BLE_NAME)); + assert(rc == 0); +} + +#endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9624a798/apps/testbench/src/tbb.h ---------------------------------------------------------------------- diff --git a/apps/testbench/src/tbb.h b/apps/testbench/src/tbb.h new file mode 100644 index 0000000..cb8edf1 --- /dev/null +++ b/apps/testbench/src/tbb.h @@ -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. + */ + +#ifndef H_TBB_ +#define H_TBB_ + +#include "syscfg/syscfg.h" +#include "log/log.h" +#include "nimble/ble.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#if MYNEWT_VAL(TESTBENCH_BLE) + +void tbb_init(void); + +#else +#define tbb_init() +#endif + +#ifdef __cplusplus +} +#endif + +#endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9624a798/apps/testbench/src/testbench.c ---------------------------------------------------------------------- diff --git a/apps/testbench/src/testbench.c b/apps/testbench/src/testbench.c index ef76988..1c792aa 100644 --- a/apps/testbench/src/testbench.c +++ b/apps/testbench/src/testbench.c @@ -73,6 +73,7 @@ #endif #include "testbench.h" +#include "tbb.h" struct os_timeval tv; struct os_timezone tz; @@ -150,20 +151,6 @@ testbench_ts_fail(char *msg, void *arg) testbench_ts_result(msg, arg, false); } -#if 0 -void -testbench_tc_pretest(void* arg) -{ - return; -} - -void -testbench_tc_postest(void* arg) -{ - return; -} -#endif - void testbench_test_init() { @@ -360,6 +347,10 @@ main(int argc, char **argv) cbmem_init(&cbmem, cbmem_buf, MAX_CBMEM_BUF); log_register("testlog", &testlog, &log_cbmem_handler, &cbmem, LOG_SYSLEVEL); +#if MYNEWT_VAL(TESTBENCH_BLE) + tbb_init(); +#endif + conf_load(); reboot_start(hal_reset_cause()); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/9624a798/apps/testbench/syscfg.yml ---------------------------------------------------------------------- diff --git a/apps/testbench/syscfg.yml b/apps/testbench/syscfg.yml index 55b2ca3..0746b44 100644 --- a/apps/testbench/syscfg.yml +++ b/apps/testbench/syscfg.yml @@ -18,12 +18,23 @@ # Package: apps/testbench +syscfg.defs: + TESTBENCH_BLE: + description: Enables BLE support in the testbench app. + value: 0 + + TESTBENCH_BLE_NAME: + description: The BLE name to use. + value: '"testbench-ble"' + syscfg.vals: # Enable the shell task. SHELL_TASK: 1 # Include names for statistics. STATS_NAMES: 1 + STATS_CLI: 1 + STATS_NEWTMGR: 1 # Log reboot messages to a flash circular buffer. REBOOT_LOG_FCB: 1 @@ -33,11 +44,9 @@ syscfg.vals: OS_COREDUMP: 1 IMGMGR_COREDUMP: 1 -# CONFIG_NFFS: 1 CONFIG_FCB: 1 # Enable shell commands. - # STATS_CLI: 1 LOG_CLI: 1 CONFIG_CLI: 1 @@ -48,7 +57,8 @@ syscfg.vals: RUNTEST_CLI: 1 RUNTEST_NEWTMGR: 1 -# BSP_ARDUINO_ZERO_PRO: 1 + CRASH_TEST_CLI: 1 + IMGMGR_CLI: 1 # Default task settings OS_MAIN_STACK_SIZE: 768