You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2017/10/30 20:41:05 UTC
[mynewt-core] 01/03: apps/blehr: Added heart-rate sensor app
This is an automated email from the ASF dual-hosted git repository.
ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
commit aacacde347fdc239629e0edc1716decf3336138f
Author: szymon-czapracki <sz...@gmail.com>
AuthorDate: Fri Sep 29 14:09:40 2017 +0200
apps/blehr: Added heart-rate sensor app
---
apps/blehr/README.md | 9 +
apps/blehr/pkg.yml | 40 +++
.../ble_hs_id.h => apps/blehr/src/blehr_sens.h | 31 ++-
apps/blehr/src/gatt_svr.c | 177 ++++++++++++++
apps/blehr/src/main.c | 267 +++++++++++++++++++++
apps/blehr/syscfg.yml | 35 +++
net/nimble/host/include/host/ble_hs_id.h | 3 +
7 files changed, 554 insertions(+), 8 deletions(-)
diff --git a/apps/blehr/README.md b/apps/blehr/README.md
new file mode 100644
index 0000000..d06d95d
--- /dev/null
+++ b/apps/blehr/README.md
@@ -0,0 +1,9 @@
+# BLE Heart Rate peripheral app.
+
+The source files are located in the src/ directory.
+
+pkg.yml contains the base definition of the app.
+
+syscfg.yml contains setting definitions and overrides.
+
+
diff --git a/apps/blehr/pkg.yml b/apps/blehr/pkg.yml
new file mode 100644
index 0000000..6c67270
--- /dev/null
+++ b/apps/blehr/pkg.yml
@@ -0,0 +1,40 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: apps/blehr
+pkg.type: app
+pkg.description: BLE peripheral heartrate sensor.
+pkg.author: "Szymon Czapracki"
+pkg.email: "szymon.czapracki@codecoup.pl"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+
+pkg.deps:
+ - boot/bootutil
+ - kernel/os
+ - net/nimble/controller
+ - net/nimble/host
+ - net/nimble/host/services/gap
+ - net/nimble/host/services/gatt
+ - net/nimble/host/store/config
+ - net/nimble/transport/ram
+ - sys/console/full
+ - sys/log/full
+ - sys/stats/full
+ - sys/sysinit
+ - sys/id
diff --git a/net/nimble/host/include/host/ble_hs_id.h b/apps/blehr/src/blehr_sens.h
similarity index 54%
copy from net/nimble/host/include/host/ble_hs_id.h
copy to apps/blehr/src/blehr_sens.h
index cb2593a..2eec9dd 100644
--- a/net/nimble/host/include/host/ble_hs_id.h
+++ b/apps/blehr/src/blehr_sens.h
@@ -17,21 +17,36 @@
* under the License.
*/
-#ifndef H_BLE_HS_ID_
-#define H_BLE_HS_ID_
+#ifndef H_BLEHR_SENSOR_
+#define H_BLEHR_SENSOR_
-#include <inttypes.h>
+#include "log/log.h"
#include "nimble/ble.h"
#ifdef __cplusplus
extern "C" {
#endif
-int ble_hs_id_gen_rnd(int nrpa, ble_addr_t *out_addr);
-int ble_hs_id_set_rnd(const uint8_t *rnd_addr);
-int ble_hs_id_copy_addr(uint8_t id_addr_type, uint8_t *out_id_addr,
- int *out_is_nrpa);
-int ble_hs_id_infer_auto(int privacy, uint8_t *out_addr_type);
+extern struct log blehr_log;
+
+/* blehr uses the first "peruser" log module */
+#define BLEHR_LOG_MODULE (LOG_MODULE_PERUSER + 0)
+
+/* Convenience macro for logging to the blerh module */
+#define BLEHR_LOG(lvl, ...) \
+ LOG_ ## lvl(&blehr_log, BLEHR_LOG_MODULE, __VA_ARGS__)
+
+/* Heart-rate configuration */
+#define GATT_HRS_UUID 0x180D
+#define GATT_HRS_MEASUREMENT_UUID 0x2A37
+#define GATT_HRS_BODY_SENSOR_LOC_UUID 0x2A38
+#define GATT_DEVICE_INFO_UUID 0x180A
+#define GATT_MANUFACTURER_NAME_UUID 0x2A29
+#define GATT_MODEL_NUMBER_UUID 0x2A24
+
+extern uint16_t hrs_hrm_handle;
+
+int gatt_svr_init(void);
#ifdef __cplusplus
}
diff --git a/apps/blehr/src/gatt_svr.c b/apps/blehr/src/gatt_svr.c
new file mode 100644
index 0000000..eac843e
--- /dev/null
+++ b/apps/blehr/src/gatt_svr.c
@@ -0,0 +1,177 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+#include "host/ble_hs.h"
+#include "host/ble_uuid.h"
+#include "blehr_sens.h"
+
+static const char *manuf_name = "Apache Mynewt";
+static const char *model_num = "Mynewt HR Sensor";
+uint16_t hrs_hrm_handle;
+
+static int
+gatt_svr_chr_access_heart_rate(uint16_t conn_handle, uint16_t attr_handle,
+ struct ble_gatt_access_ctxt *ctxt, void *arg);
+
+static int
+gatt_svr_chr_access_device_info(uint16_t conn_handle, uint16_t attr_handle,
+ struct ble_gatt_access_ctxt *ctxt, void *arg);
+
+static const struct ble_gatt_svc_def gatt_svr_svcs[] = {
+ {
+ /* Service: Heart-rate */
+ .type = BLE_GATT_SVC_TYPE_PRIMARY,
+ .uuid = BLE_UUID16_DECLARE(GATT_HRS_UUID),
+ .characteristics = (struct ble_gatt_chr_def[]) { {
+ /* Characteristic: Heart-rate measurement */
+ .uuid = BLE_UUID16_DECLARE(GATT_HRS_MEASUREMENT_UUID),
+ .access_cb = gatt_svr_chr_access_heart_rate,
+ .val_handle = &hrs_hrm_handle,
+ .flags = BLE_GATT_CHR_F_NOTIFY,
+ }, {
+ /* Characteristic: Body sensor location */
+ .uuid = BLE_UUID16_DECLARE(GATT_HRS_BODY_SENSOR_LOC_UUID),
+ .access_cb = gatt_svr_chr_access_heart_rate,
+ .flags = BLE_GATT_CHR_F_READ,
+ }, {
+ 0, /* No more characteristics in this service */
+ }, }
+ },
+
+ {
+ /* Service: Device Information */
+ .type = BLE_GATT_SVC_TYPE_PRIMARY,
+ .uuid = BLE_UUID16_DECLARE(GATT_DEVICE_INFO_UUID),
+ .characteristics = (struct ble_gatt_chr_def[]) { {
+ /* Characteristic: * Manufacturer name */
+ .uuid = BLE_UUID16_DECLARE(GATT_MANUFACTURER_NAME_UUID),
+ .access_cb = gatt_svr_chr_access_device_info,
+ .flags = BLE_GATT_CHR_F_READ,
+ }, {
+ /* Characteristic: Model number string */
+ .uuid = BLE_UUID16_DECLARE(GATT_MODEL_NUMBER_UUID),
+ .access_cb = gatt_svr_chr_access_device_info,
+ .flags = BLE_GATT_CHR_F_READ,
+ }, {
+ 0, /* No more characteristics in this service */
+ }, }
+ },
+
+ {
+ 0, /* No more services */
+ },
+};
+
+static int
+gatt_svr_chr_access_heart_rate(uint16_t conn_handle, uint16_t attr_handle,
+ struct ble_gatt_access_ctxt *ctxt, void *arg)
+{
+ /* Sensor location, set to "Chest" */
+ static uint8_t body_sens_loc = 0x01;
+ uint16_t uuid;
+ int rc;
+
+ uuid = ble_uuid_u16(ctxt->chr->uuid);
+
+ if (uuid == GATT_HRS_BODY_SENSOR_LOC_UUID) {
+ rc = os_mbuf_append(ctxt->om, &body_sens_loc, sizeof(body_sens_loc));
+
+ return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+ }
+
+ assert(0);
+ return BLE_ATT_ERR_UNLIKELY;
+}
+
+static int
+gatt_svr_chr_access_device_info(uint16_t conn_handle, uint16_t attr_handle,
+ struct ble_gatt_access_ctxt *ctxt, void *arg)
+{
+ uint16_t uuid;
+ int rc;
+
+ uuid = ble_uuid_u16(ctxt->chr->uuid);
+
+ if (uuid == GATT_MODEL_NUMBER_UUID) {
+ rc = os_mbuf_append(ctxt->om, model_num, strlen(model_num));
+ return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+ }
+
+ if (uuid == GATT_MANUFACTURER_NAME_UUID) {
+ rc = os_mbuf_append(ctxt->om, manuf_name, strlen(manuf_name));
+ return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+ }
+
+ assert(0);
+ return BLE_ATT_ERR_UNLIKELY;
+}
+
+void
+gatt_svr_register_cb(struct ble_gatt_register_ctxt *ctxt, void *arg)
+{
+ char buf[BLE_UUID_STR_LEN];
+
+ switch (ctxt->op) {
+ case BLE_GATT_REGISTER_OP_SVC:
+ BLEHR_LOG(DEBUG, "registered service %s with handle=%d\n",
+ ble_uuid_to_str(ctxt->svc.svc_def->uuid, buf),
+ ctxt->svc.handle);
+ break;
+
+ case BLE_GATT_REGISTER_OP_CHR:
+ BLEHR_LOG(DEBUG, "registering characteristic %s with "
+ "def_handle=%d val_handle=%d\n",
+ ble_uuid_to_str(ctxt->chr.chr_def->uuid, buf),
+ ctxt->chr.def_handle,
+ ctxt->chr.val_handle);
+ break;
+
+ case BLE_GATT_REGISTER_OP_DSC:
+ BLEHR_LOG(DEBUG, "registering descriptor %s with handle=%d\n",
+ ble_uuid_to_str(ctxt->dsc.dsc_def->uuid, buf),
+ ctxt->dsc.handle);
+ break;
+
+ default:
+ assert(0);
+ break;
+ }
+}
+
+int
+gatt_svr_init(void)
+{
+ int rc;
+
+ rc = ble_gatts_count_cfg(gatt_svr_svcs);
+ if (rc != 0) {
+ return rc;
+ }
+
+ rc = ble_gatts_add_svcs(gatt_svr_svcs);
+ if (rc != 0) {
+ return rc;
+ }
+
+ return 0;
+}
+
diff --git a/apps/blehr/src/main.c b/apps/blehr/src/main.c
new file mode 100644
index 0000000..150e134
--- /dev/null
+++ b/apps/blehr/src/main.c
@@ -0,0 +1,267 @@
+/*
+ * 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 "os/os.h"
+#include "console/console.h"
+#include "config/config.h"
+#include "nimble/ble.h"
+#include "host/ble_hs.h"
+#include "services/gap/ble_svc_gap.h"
+#include "blehr_sens.h"
+
+/* Log data */
+struct log blehr_log;
+
+static bool notify_state;
+
+static const char *device_name = "blehr_sensor";
+
+static int blehr_gap_event(struct ble_gap_event *event, void *arg);
+
+static uint8_t addr_type;
+
+/* Sending notify data timer */
+static struct os_callout blehr_tx_timer;
+
+/* Variable to simulate heart beats */
+static uint8_t heartrate = 90;
+
+/*
+ * Enables advertising with parameters:
+ * o General discoverable mode
+ * o Undirected connectable mode
+ */
+static void
+blehr_advertise(void)
+{
+ struct ble_gap_adv_params adv_params;
+ struct ble_hs_adv_fields fields;
+ 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
+ */
+ 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 assigning 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;
+
+ fields.name = (uint8_t *)device_name;
+ fields.name_len = strlen(device_name);
+ fields.name_is_complete = 1;
+
+ rc = ble_gap_adv_set_fields(&fields);
+ if (rc != 0) {
+ BLEHR_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(addr_type, NULL, BLE_HS_FOREVER,
+ &adv_params, blehr_gap_event, NULL);
+ if (rc != 0) {
+ BLEHR_LOG(ERROR, "error enabling advertisement; rc=%d\n", rc);
+ return;
+ }
+}
+
+static void
+blehr_tx_hrate_stop(void)
+{
+ os_callout_stop(&blehr_tx_timer);
+}
+
+/* Reset heartrate measurment */
+static void
+blehr_tx_hrate_reset(void)
+{
+ int rc;
+
+ rc = os_callout_reset(&blehr_tx_timer, OS_TICKS_PER_SEC);
+ assert(rc == 0);
+}
+
+/* This functions simulates heart beat and notifies it to the client */
+static void
+blehr_tx_hrate(struct os_event *ev)
+{
+ static uint8_t hrm[2];
+ int rc;
+ struct os_mbuf *om;
+
+ if (!notify_state) {
+ blehr_tx_hrate_stop();
+ heartrate = 90;
+ return;
+ }
+
+ hrm[0] = 0x06; /* contact of a sensor */
+ hrm[1] = heartrate; /* storing dummy data */
+
+ /* Simulation of heart beats */
+ heartrate++;
+ if (heartrate == 160) {
+ heartrate = 90;
+ }
+
+ om = ble_hs_mbuf_from_flat(hrm, sizeof(hrm));
+
+ rc = ble_gattc_notify_custom(notify_state, hrs_hrm_handle, om);
+
+ assert(rc == 0);
+ blehr_tx_hrate_reset();
+}
+
+static int
+blehr_gap_event(struct ble_gap_event *event, void *arg)
+{
+ switch (event->type) {
+ case BLE_GAP_EVENT_CONNECT:
+ /* A new connection was established or a connection attempt failed */
+ BLEHR_LOG(INFO, "connection %s; status=%d\n",
+ event->connect.status == 0 ? "established" : "failed",
+ event->connect.status);
+
+ if (event->connect.status != 0) {
+ /* Connection failed; resume advertising */
+ blehr_advertise();
+ }
+ break;
+
+ case BLE_GAP_EVENT_DISCONNECT:
+ BLEHR_LOG(INFO, "disconnect; reason=%d\n", event->disconnect.reason);
+
+ /* Connection terminated; resume advertising */
+ blehr_advertise();
+ break;
+
+ case BLE_GAP_EVENT_ADV_COMPLETE:
+ BLEHR_LOG(INFO, "adv complete\n");
+ blehr_advertise();
+ break;
+
+ case BLE_GAP_EVENT_SUBSCRIBE:
+ BLEHR_LOG(INFO, "subscribe event; cur_notify=%d\n value handle; "
+ "val_handle=%d\n",
+ event->subscribe.cur_notify,
+ hrs_hrm_handle);
+ if (event->subscribe.attr_handle == hrs_hrm_handle) {
+ notify_state = event->subscribe.cur_notify;
+ blehr_tx_hrate_reset();
+ } else if (event->subscribe.attr_handle != hrs_hrm_handle) {
+ notify_state = event->subscribe.cur_notify;
+ blehr_tx_hrate_stop();
+ }
+ break;
+
+ case BLE_GAP_EVENT_MTU:
+ BLEHR_LOG(INFO, "mtu update event; conn_handle=%d mtu=%d\n",
+ event->mtu.conn_handle,
+ event->mtu.value);
+ break;
+
+ }
+
+ return 0;
+}
+
+static void
+blehr_on_sync(void)
+{
+ int rc;
+ uint8_t addr_type;
+
+ /* Use privacy */
+ rc = ble_hs_id_infer_auto(1, &addr_type);
+ assert(rc == 0);
+
+ rc = ble_hs_id_use_addr(addr_type);
+ assert(rc == 0);
+
+ /* Begin advertising */
+ blehr_advertise();
+}
+
+/*
+ * main
+ *
+ * The main task for the project. This function initializes the packages,
+ * then starts serving events from default event queue.
+ *
+ * @return int NOTE: this function should never return!
+ */
+int
+main(void)
+{
+ int rc;
+
+ /* Initialize OS */
+ sysinit();
+
+ /* Initialize the blehr log */
+ log_register("blehr_sens_log", &blehr_log, &log_console_handler, NULL,
+ LOG_SYSLEVEL);
+
+ /* Initialize the NimBLE host configuration */
+ log_register("blehr_sens", &ble_hs_log, &log_console_handler, NULL,
+ LOG_SYSLEVEL);
+ ble_hs_cfg.sync_cb = blehr_on_sync;
+
+ os_callout_init(&blehr_tx_timer, os_eventq_dflt_get(),
+ blehr_tx_hrate, NULL);
+
+ rc = gatt_svr_init();
+ assert(rc == 0);
+
+ /* Set the default device name */
+ rc = ble_svc_gap_device_name_set(device_name);
+ assert(rc == 0);
+
+ /* As the last thing, process events from default event queue */
+ while (1) {
+ os_eventq_run(os_eventq_dflt_get());
+ }
+ return 0;
+}
+
diff --git a/apps/blehr/syscfg.yml b/apps/blehr/syscfg.yml
new file mode 100644
index 0000000..eaabd6e
--- /dev/null
+++ b/apps/blehr/syscfg.yml
@@ -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.
+
+
+syscfg.vals:
+ # 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
+
+ # Set public device address.
+ BLE_PUBLIC_DEV_ADDR: ((uint8_t[6]){0xcc, 0xbb, 0xaa, 0x33, 0x22, 0x11})
diff --git a/net/nimble/host/include/host/ble_hs_id.h b/net/nimble/host/include/host/ble_hs_id.h
index cb2593a..737bc39 100644
--- a/net/nimble/host/include/host/ble_hs_id.h
+++ b/net/nimble/host/include/host/ble_hs_id.h
@@ -33,6 +33,9 @@ int ble_hs_id_copy_addr(uint8_t id_addr_type, uint8_t *out_id_addr,
int *out_is_nrpa);
int ble_hs_id_infer_auto(int privacy, uint8_t *out_addr_type);
+int ble_hs_id_infer_auto(int privacy, uint8_t *out_addr_type);
+int ble_hs_id_use_addr(uint8_t addr_type);
+
#ifdef __cplusplus
}
#endif
--
To stop receiving notification emails like this one, please contact
"commits@mynewt.apache.org" <co...@mynewt.apache.org>.