You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by an...@apache.org on 2018/03/12 13:37:44 UTC

[mynewt-nimble] 10/32: nimble/gap: Make device name optionally writable

This is an automated email from the ASF dual-hosted git repository.

andk pushed a commit to branch new-master
in repository https://gitbox.apache.org/repos/asf/mynewt-nimble.git

commit 5f5b248e34116179e879f6bf5d47cc7854194329
Author: Andrzej Kaczmarek <an...@codecoup.pl>
AuthorDate: Fri Feb 23 22:26:00 2018 +0100

    nimble/gap: Make device name optionally writable
    
    GAP service takes care of updating characteristic value. Application
    can register callback to be notified of value change and can then read
    this value and store somewhere (and restore on next boot).
    
    X-Original-Commit: 46e768f451bc4c7a0222348fee384b4d1c87aff1
---
 .../gap/include/services/gap/ble_svc_gap.h         |  4 ++
 nimble/host/services/gap/src/ble_svc_gap.c         | 67 ++++++++++++++++++++--
 nimble/host/services/gap/syscfg.yml                |  9 +++
 3 files changed, 75 insertions(+), 5 deletions(-)

diff --git a/nimble/host/services/gap/include/services/gap/ble_svc_gap.h b/nimble/host/services/gap/include/services/gap/ble_svc_gap.h
index f4bc213..29bbf48 100644
--- a/nimble/host/services/gap/include/services/gap/ble_svc_gap.h
+++ b/nimble/host/services/gap/include/services/gap/ble_svc_gap.h
@@ -34,6 +34,10 @@ extern "C" {
 #define BLE_SVC_GAP_APPEARANCE_GEN_COMPUTER                        128
 #define BLE_SVC_GAP_APPEARANCE_CYC_SPEED_AND_CADENCE_SENSOR        1157
 
+typedef void (ble_svc_gap_chr_changed_fn) (uint16_t uuid);
+
+void ble_svc_gap_set_chr_changed_cb(ble_svc_gap_chr_changed_fn *cb);
+
 const char *ble_svc_gap_device_name(void);
 int ble_svc_gap_device_name_set(const char *name);
 uint16_t ble_svc_gap_device_appearance(void);
diff --git a/nimble/host/services/gap/src/ble_svc_gap.c b/nimble/host/services/gap/src/ble_svc_gap.c
index 2445b9a..7f2818c 100644
--- a/nimble/host/services/gap/src/ble_svc_gap.c
+++ b/nimble/host/services/gap/src/ble_svc_gap.c
@@ -35,6 +35,8 @@
 /* XXX: This should be configurable. */
 #define BLE_SVC_GAP_NAME_MAX_LEN    31
 
+static ble_svc_gap_chr_changed_fn *ble_svc_gap_chr_changed_cb_fn;
+
 static char ble_svc_gap_name[BLE_SVC_GAP_NAME_MAX_LEN + 1] =
         MYNEWT_VAL(BLE_SVC_GAP_DEVICE_NAME);
 static const uint16_t ble_svc_gap_appearance =
@@ -53,7 +55,12 @@ static const struct ble_gatt_svc_def ble_svc_gap_defs[] = {
             /*** Characteristic: Device Name. */
             .uuid = BLE_UUID16_DECLARE(BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME),
             .access_cb = ble_svc_gap_access,
-            .flags = BLE_GATT_CHR_F_READ,
+            .flags = BLE_GATT_CHR_F_READ |
+#if MYNEWT_VAL(BLE_SVC_GAP_DEVICE_NAME_WRITE_PERM) >= 0
+                     BLE_GATT_CHR_F_WRITE |
+                     MYNEWT_VAL(BLE_SVC_GAP_DEVICE_NAME_WRITE_PERM) |
+#endif
+                     0,
         }, {
             /*** Characteristic: Appearance. */
             .uuid = BLE_UUID16_DECLARE(BLE_SVC_GAP_CHR_UUID16_APPEARANCE),
@@ -85,6 +92,46 @@ static const struct ble_gatt_svc_def ble_svc_gap_defs[] = {
 };
 
 static int
+ble_svc_gap_device_name_read_access(struct ble_gatt_access_ctxt *ctxt)
+{
+    int rc;
+
+    rc = os_mbuf_append(ctxt->om, ble_svc_gap_name, strlen(ble_svc_gap_name));
+
+    return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
+}
+
+static int
+ble_svc_gap_device_name_write_access(struct ble_gatt_access_ctxt *ctxt)
+{
+#if MYNEWT_VAL(BLE_SVC_GAP_DEVICE_NAME_WRITE_PERM) < 0
+    assert(0);
+    return 0;
+#else
+    uint16_t om_len;
+    int rc;
+
+    om_len = OS_MBUF_PKTLEN(ctxt->om);
+    if (om_len > BLE_SVC_GAP_NAME_MAX_LEN) {
+        return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
+    }
+
+    rc = ble_hs_mbuf_to_flat(ctxt->om, ble_svc_gap_name, om_len, NULL);
+    if (rc != 0) {
+        return BLE_ATT_ERR_UNLIKELY;
+    }
+
+    ble_svc_gap_name[om_len] = '\0';
+
+    if (ble_svc_gap_chr_changed_cb_fn) {
+        ble_svc_gap_chr_changed_cb_fn(BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME);
+    }
+
+    return rc;
+#endif
+}
+
+static int
 ble_svc_gap_access(uint16_t conn_handle, uint16_t attr_handle,
                    struct ble_gatt_access_ctxt *ctxt, void *arg)
 {
@@ -108,10 +155,14 @@ ble_svc_gap_access(uint16_t conn_handle, uint16_t attr_handle,
 
     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;
+        if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) {
+            rc = ble_svc_gap_device_name_read_access(ctxt);
+        } else if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) {
+            rc = ble_svc_gap_device_name_write_access(ctxt);
+        } else {
+            assert(0);
+        }
+        return rc;
 
     case BLE_SVC_GAP_CHR_UUID16_APPEARANCE:
         assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR);
@@ -167,6 +218,12 @@ ble_svc_gap_device_appearance(void)
 }
 
 void
+ble_svc_gap_set_chr_changed_cb(ble_svc_gap_chr_changed_fn *cb)
+{
+    ble_svc_gap_chr_changed_cb_fn = cb;
+}
+
+void
 ble_svc_gap_init(void)
 {
     int rc;
diff --git a/nimble/host/services/gap/syscfg.yml b/nimble/host/services/gap/syscfg.yml
index 25597e9..c9c9380 100644
--- a/nimble/host/services/gap/syscfg.yml
+++ b/nimble/host/services/gap/syscfg.yml
@@ -24,6 +24,15 @@ syscfg.defs:
             Default value for "Device Name" characteristics, unless overwritten
             by application.
         value: '"nimble"'
+    BLE_SVC_GAP_DEVICE_NAME_WRITE_PERM:
+        description: >
+            Defines permissions for writing "Device Name" characteristics. Can
+            be zero to allow write without extra permissions or combination of:
+                BLE_GATT_CHR_F_WRITE_ENC
+                BLE_GATT_CHR_F_WRITE_AUTHEN
+                BLE_GATT_CHR_F_WRITE_AUTHOR
+            Set to '-1' to make characteristic read only.
+        value: -1
     BLE_SVC_GAP_APPEARANCE:
         description: 'Device appearance'
         value: 0

-- 
To stop receiving notification emails like this one, please contact
andk@apache.org.