Repository: incubator-mynewt-core Updated Branches: refs/heads/develop 65f8fe3d9 -> 50963cc29
blesplit - New split app that supports BLE. 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/5a7ba4ec Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/5a7ba4ec Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/5a7ba4ec Branch: refs/heads/develop Commit: 5a7ba4ec1338770af21c1e27907ed34a8b08b25c Parents: 65f8fe3 Author: Christopher Collins <[email protected]> Authored: Tue Jan 24 10:31:13 2017 -0800 Committer: Christopher Collins <[email protected]> Committed: Tue Jan 24 10:31:13 2017 -0800 ---------------------------------------------------------------------- apps/blesplit/README.md | 55 ++++++++ apps/blesplit/pkg.yml | 44 ++++++ apps/blesplit/src/blesplit.h | 59 ++++++++ apps/blesplit/src/main.c | 285 ++++++++++++++++++++++++++++++++++++++ apps/blesplit/src/misc.c | 43 ++++++ apps/blesplit/syscfg.yml | 48 +++++++ 6 files changed, 534 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5a7ba4ec/apps/blesplit/README.md ---------------------------------------------------------------------- diff --git a/apps/blesplit/README.md b/apps/blesplit/README.md new file mode 100644 index 0000000..adb1be3 --- /dev/null +++ b/apps/blesplit/README.md @@ -0,0 +1,55 @@ +<!-- +# +# 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. +# +--> + + +<img src="http://mynewt.apache.org/img/logo.svg" width="250" alt="Apache Mynewt"> + +## Overview + +`apps/splitty` is an example split application. It can be paired with slinky to form an application. + +## Split Image + +Split applications allow the user to build the application content separate from the library content by splitting an application into two pieces: + +* A "loader" which contains a separate application that can perform upgrades and manage split images +* A "split app" which contains the main application content and references the libraries in the loader by static linkage + +See [split image architecture](http://mynewt.apache.org/latest/os/modules/split/split/) for the details of split image design. + +## Contents + +splitty is just a simply app that has newtmgr, shell and blinkys the LED at a differernt rate than slinky, so its visually evident that its running. + +## Usage + +You can use splitty as part of a split app by setting up your target. + +``` +targets/app + app=@apache-mynewt-core/apps/splitty + loader=@apache-mynewt-core/apps/slinky + bsp=@apache-mynewt-core/hw/bsp/nrf52dk + build_profile=optimized +``` + + + http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5a7ba4ec/apps/blesplit/pkg.yml ---------------------------------------------------------------------- diff --git a/apps/blesplit/pkg.yml b/apps/blesplit/pkg.yml new file mode 100644 index 0000000..534bd40 --- /dev/null +++ b/apps/blesplit/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: apps/blesplit +pkg.type: app +pkg.description: "Example BLE application which runs as a split app." +pkg.author: "Apache Mynewt <[email protected]>" +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + +pkg.deps: + - boot/split + - boot/split_app + - kernel/os + - mgmt/imgmgr + - mgmt/newtmgr + - mgmt/newtmgr/transport/ble + - net/nimble/host + - net/nimble/host/services/ans + - net/nimble/host/services/gap + - net/nimble/host/services/gatt + - net/nimble/host/store/ram + - net/nimble/transport/ram + - sys/console/full + - sys/id + - sys/log/full + - sys/shell + - sys/stats/full http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5a7ba4ec/apps/blesplit/src/blesplit.h ---------------------------------------------------------------------- diff --git a/apps/blesplit/src/blesplit.h b/apps/blesplit/src/blesplit.h new file mode 100644 index 0000000..8a04e3e --- /dev/null +++ b/apps/blesplit/src/blesplit.h @@ -0,0 +1,59 @@ +/* + * 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_BLESPLIT_ +#define H_BLESPLIT_ + +#include "log/log.h" +#ifdef __cplusplus +extern "C" { +#endif + +struct ble_hs_cfg; +struct ble_gatt_register_ctxt; + +extern struct log blesplit_log; + +/* blesplit uses the first "peruser" log module. */ +#define BLESPLIT_LOG_MODULE (LOG_MODULE_PERUSER + 0) + +/* Convenience macro for logging to the blesplit module. */ +#define BLESPLIT_LOG(lvl, ...) \ + LOG_ ## lvl(&blesplit_log, BLESPLIT_LOG_MODULE, __VA_ARGS__) + +/** GATT server. */ +#define GATT_SVR_SVC_ALERT_UUID 0x1811 +#define GATT_SVR_CHR_SUP_NEW_ALERT_CAT_UUID 0x2A47 +#define GATT_SVR_CHR_NEW_ALERT 0x2A46 +#define GATT_SVR_CHR_SUP_UNR_ALERT_CAT_UUID 0x2A48 +#define GATT_SVR_CHR_UNR_ALERT_STAT_UUID 0x2A45 +#define GATT_SVR_CHR_ALERT_NOT_CTRL_PT 0x2A44 + +void gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg); +int gatt_svr_init(void); + +/** Misc. */ +void print_bytes(const uint8_t *bytes, int len); +void print_addr(const void *addr); + +#ifdef __cplusplus +} +#endif + +#endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5a7ba4ec/apps/blesplit/src/main.c ---------------------------------------------------------------------- diff --git a/apps/blesplit/src/main.c b/apps/blesplit/src/main.c new file mode 100755 index 0000000..e805c7c --- /dev/null +++ b/apps/blesplit/src/main.c @@ -0,0 +1,285 @@ +/* + * 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 <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" + +/* Application-specified header. */ +#include "blesplit.h" + +/** Log data. */ +struct log blesplit_log; + +static int blesplit_gap_event(struct ble_gap_event *event, void *arg); + +/** + * Logs information about a connection to the console. + */ +static void +blesplit_print_conn_desc(struct ble_gap_conn_desc *desc) +{ + BLESPLIT_LOG(INFO, "handle=%d our_ota_addr_type=%d our_ota_addr=", + desc->conn_handle, desc->our_ota_addr_type); + print_addr(desc->our_ota_addr); + BLESPLIT_LOG(INFO, " our_id_addr_type=%d our_id_addr=", + desc->our_id_addr_type); + print_addr(desc->our_id_addr); + BLESPLIT_LOG(INFO, " peer_ota_addr_type=%d peer_ota_addr=", + desc->peer_ota_addr_type); + print_addr(desc->peer_ota_addr); + BLESPLIT_LOG(INFO, " peer_id_addr_type=%d peer_id_addr=", + desc->peer_id_addr_type); + print_addr(desc->peer_id_addr); + BLESPLIT_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 +blesplit_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); + + /* Indicate that the flags field should be included; specify a value of 0 + * to instruct the stack to fill the value in for us. + */ + fields.flags_is_present = 1; + fields.flags = 0; + + /* Indicate that the TX power level field should be included; have the + * stack fill this one automatically as well. 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; + + fields.uuids16 = (uint16_t[]){ GATT_SVR_SVC_ALERT_UUID }; + fields.num_uuids16 = 1; + fields.uuids16_is_complete = 1; + + rc = ble_gap_adv_set_fields(&fields); + if (rc != 0) { + BLESPLIT_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_ADDR_TYPE_PUBLIC, 0, NULL, BLE_HS_FOREVER, + &adv_params, blesplit_gap_event, NULL); + if (rc != 0) { + BLESPLIT_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. + * blesplit 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 + * blesplit. + * + * @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 +blesplit_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. */ + BLESPLIT_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); + blesplit_print_conn_desc(&desc); + } + BLESPLIT_LOG(INFO, "\n"); + + if (event->connect.status != 0) { + /* Connection failed; resume advertising. */ + blesplit_advertise(); + } + return 0; + + case BLE_GAP_EVENT_DISCONNECT: + BLESPLIT_LOG(INFO, "disconnect; reason=%d ", event->disconnect.reason); + blesplit_print_conn_desc(&event->disconnect.conn); + BLESPLIT_LOG(INFO, "\n"); + + /* Connection terminated; resume advertising. */ + blesplit_advertise(); + return 0; + + case BLE_GAP_EVENT_CONN_UPDATE: + /* The central has updated the connection parameters. */ + BLESPLIT_LOG(INFO, "connection updated; status=%d ", + event->conn_update.status); + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + blesplit_print_conn_desc(&desc); + BLESPLIT_LOG(INFO, "\n"); + return 0; + + case BLE_GAP_EVENT_ENC_CHANGE: + /* Encryption has been enabled or disabled for this connection. */ + BLESPLIT_LOG(INFO, "encryption change event; status=%d ", + event->enc_change.status); + rc = ble_gap_conn_find(event->connect.conn_handle, &desc); + assert(rc == 0); + blesplit_print_conn_desc(&desc); + BLESPLIT_LOG(INFO, "\n"); + return 0; + + case BLE_GAP_EVENT_SUBSCRIBE: + BLESPLIT_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: + BLESPLIT_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 +blesplit_on_reset(int reason) +{ + BLESPLIT_LOG(ERROR, "Resetting state; reason=%d\n", reason); +} + +static void +blesplit_on_sync(void) +{ + /* Begin advertising. */ + blesplit_advertise(); +} + +/** + * 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(void) +{ + int rc; + + /* Initialize OS */ + sysinit(); + + /* Set initial BLE device address. */ + memcpy(g_dev_addr, (uint8_t[6]){0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a}, 6); + + /* Initialize the blesplit log. */ + log_register("blesplit", &blesplit_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 = blesplit_on_reset; + ble_hs_cfg.sync_cb = blesplit_on_sync; + + /* Set the default device name. */ + rc = ble_svc_gap_device_name_set("nimble-blesplit"); + assert(rc == 0); + + conf_load(); + + /* + * As the last thing, process events from default event queue. + */ + while (1) { + os_eventq_run(os_eventq_dflt_get()); + } + return 0; +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5a7ba4ec/apps/blesplit/src/misc.c ---------------------------------------------------------------------- diff --git a/apps/blesplit/src/misc.c b/apps/blesplit/src/misc.c new file mode 100644 index 0000000..43f62ae --- /dev/null +++ b/apps/blesplit/src/misc.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 "blesplit.h" + +/** + * Utility function to log an array of bytes. + */ +void +print_bytes(const uint8_t *bytes, int len) +{ + int i; + + for (i = 0; i < len; i++) { + BLESPLIT_LOG(INFO, "%s0x%02x", i != 0 ? ":" : "", bytes[i]); + } +} + +void +print_addr(const void *addr) +{ + const uint8_t *u8p; + + u8p = addr; + BLESPLIT_LOG(INFO, "%02x:%02x:%02x:%02x:%02x:%02x", + u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]); +} http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/5a7ba4ec/apps/blesplit/syscfg.yml ---------------------------------------------------------------------- diff --git a/apps/blesplit/syscfg.yml b/apps/blesplit/syscfg.yml new file mode 100644 index 0000000..9ceab25 --- /dev/null +++ b/apps/blesplit/syscfg.yml @@ -0,0 +1,48 @@ +# 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. +# + +# Package: apps/blesplit + +syscfg.vals: + # Use INFO log level to reduce code size. DEBUG is too large for nRF51. + LOG_LEVEL: 1 + + # Disable central and observer roles. + BLE_ROLE_BROADCASTER: 1 + BLE_ROLE_CENTRAL: 0 + BLE_ROLE_OBSERVER: 0 + BLE_ROLE_PERIPHERAL: 1 + + # Disable unused eddystone feature. + BLE_EDDYSTONE: 0 + + # Log reboot messages to a flash circular buffer. + REBOOT_LOG_FCB: 1 + LOG_FCB: 1 + CONFIG_FCB: 1 + + # Enable newtmgr commands. + STATS_NEWTMGR: 1 + LOG_NEWTMGR: 1 + + # Enable Config. + CONFIG_NEWTMGR: 1 + + # OS main/default task + OS_MAIN_TASK_PRIO: 1 + OS_MAIN_STACK_SIZE: 428
