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 2016/01/23 04:20:05 UTC

[8/8] incubator-mynewt-larva git commit: bleshell - Alert Notification Service.

bleshell - Alert Notification Service.


Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/commit/36de15d1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/36de15d1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/36de15d1

Branch: refs/heads/master
Commit: 36de15d1f38f4fed545bb493889afa517be40f89
Parents: e0cdc6f
Author: Christopher Collins <cc...@gmail.com>
Authored: Fri Jan 22 19:08:40 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Fri Jan 22 19:19:45 2016 -0800

----------------------------------------------------------------------
 project/bleshell/src/periph.c | 133 +++++++++++++++++++++++++++++++++----
 1 file changed, 119 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/36de15d1/project/bleshell/src/periph.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/periph.c b/project/bleshell/src/periph.c
index 48fc720..c51270f 100644
--- a/project/bleshell/src/periph.c
+++ b/project/bleshell/src/periph.c
@@ -5,20 +5,27 @@
 #include "host/ble_hs.h"
 #include "bleshell_priv.h"
 
-#define PERIPH_SVC1_UUID      0x1234
-#define PERIPH_SVC2_UUID      0x5678
-#define PERIPH_CHR1_UUID      0x1111
-#define PERIPH_CHR2_UUID      0x1112
-#define PERIPH_CHR3_UUID      0x5555
-
-#define CHR_F_FULL_ACCESS (BLE_GATT_CHR_F_READ              |   \
-                           BLE_GATT_CHR_F_WRITE_NO_RSP      |   \
-                           BLE_GATT_CHR_F_WRITE             |   \
-                           BLE_GATT_CHR_F_NOTIFY            |   \
-                           BLE_GATT_CHR_F_INDICATE)
+#define PERIPH_SVC1_UUID        0x1234
+#define PERIPH_SVC2_UUID        0x5678
+#define PERIPH_CHR1_UUID        0x1111
+#define PERIPH_CHR2_UUID        0x1112
+#define PERIPH_CHR3_UUID        0x5555
+
+#define CHR_F_FULL_ACCESS       (BLE_GATT_CHR_F_READ                |   \
+                                 BLE_GATT_CHR_F_WRITE_NO_RSP        |   \
+                                 BLE_GATT_CHR_F_WRITE               |   \
+                                 BLE_GATT_CHR_F_NOTIFY              |   \
+                                 BLE_GATT_CHR_F_INDICATE)
 
 #define PERIPH_CHR_MAX_LEN  16
 
+#define PERIPH_SVC_ALERT_UUID               0x1811
+#define PERIPH_CHR_SUP_NEW_ALERT_CAT_UUID   0x2A47
+#define PERIPH_CHR_NEW_ALERT                0x2A46
+#define PERIPH_CHR_SUP_UNR_ALERT_CAT_UUID   0x2A48
+#define PERIPH_CHR_UNR_ALERT_STAT_UUID      0x2A45
+#define PERIPH_CHR_ALERT_NOT_CTRL_PT        0x2A44
+
 
 static bssnz_t uint8_t periph_chr_data[3][PERIPH_CHR_MAX_LEN];
 static bssnz_t uint16_t periph_chr_lens[3];
@@ -29,7 +36,9 @@ periph_chr_access_gap(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
 static int
 periph_chr_access_gatt(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
                        union ble_gatt_access_ctxt *ctxt, void *arg);
-
+static int
+periph_chr_access_alert(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
+                        union ble_gatt_access_ctxt *ctxt, void *arg);
 static int
 periph_gatt_cb(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
                union ble_gatt_access_ctxt *ctxt, void *arg);
@@ -83,6 +92,34 @@ static const struct ble_gatt_svc_def periph_svcs[] = {
     },
 
     [2] = {
+        /*** Alert Notification Service. */
+        .type = BLE_GATT_SVC_TYPE_PRIMARY,
+        .uuid128 = BLE_UUID16(PERIPH_SVC_ALERT_UUID),
+        .characteristics = (struct ble_gatt_chr_def[]) { {
+            .uuid128 = BLE_UUID16(PERIPH_CHR_SUP_NEW_ALERT_CAT_UUID),
+            .access_cb = periph_chr_access_alert,
+            .flags = BLE_GATT_CHR_F_READ,
+        }, {
+            .uuid128 = BLE_UUID16(PERIPH_CHR_NEW_ALERT),
+            .access_cb = periph_chr_access_alert,
+            .flags = BLE_GATT_CHR_F_NOTIFY,
+        }, {
+            .uuid128 = BLE_UUID16(PERIPH_CHR_SUP_UNR_ALERT_CAT_UUID),
+            .access_cb = periph_chr_access_alert,
+            .flags = BLE_GATT_CHR_F_READ,
+        }, {
+            .uuid128 = BLE_UUID16(PERIPH_CHR_UNR_ALERT_STAT_UUID),
+            .access_cb = periph_chr_access_alert,
+            .flags = BLE_GATT_CHR_F_NOTIFY,
+        }, {
+            .uuid128 = BLE_UUID16(PERIPH_CHR_ALERT_NOT_CTRL_PT),
+            .access_cb = periph_chr_access_alert,
+            .flags = BLE_GATT_CHR_F_WRITE,
+        }, {
+            0, /* No more characteristics in this service. */
+        } },
+    },
+    [3] = {
         /*** Service 0x1234. */
         .type = BLE_GATT_SVC_TYPE_SECONDARY,
         .uuid128 = BLE_UUID16(PERIPH_SVC1_UUID),
@@ -101,12 +138,12 @@ static const struct ble_gatt_svc_def periph_svcs[] = {
         } },
     },
 
-    [3] = {
+    [4] = {
         /*** Service 0x5678. */
         .type = BLE_GATT_SVC_TYPE_PRIMARY,
         .uuid128 = BLE_UUID16(PERIPH_SVC2_UUID),
         .includes = (const struct ble_gatt_svc_def *[]) {
-            &periph_svcs[2],
+            &periph_svcs[3],
             NULL,
         },
         .characteristics = (struct ble_gatt_chr_def[]) { {
@@ -125,6 +162,23 @@ static const struct ble_gatt_svc_def periph_svcs[] = {
 };
 
 static int
+periph_chr_write(uint8_t op, union ble_gatt_access_ctxt *ctxt,
+                 uint16_t min_len, uint16_t max_len, void *dst, uint16_t *len)
+{
+    assert(op == BLE_GATT_ACCESS_OP_WRITE_CHR);
+    if (ctxt->chr_access.len < min_len ||
+        ctxt->chr_access.len > max_len) {
+        return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
+    }
+    memcpy(dst, ctxt->chr_access.data, ctxt->chr_access.len);
+    if (len != NULL) {
+        *len = ctxt->chr_access.len;
+    }
+
+    return 0;
+}
+
+static int
 periph_gatt_read(uint16_t attr_handle, union ble_gatt_access_ctxt *ctxt,
                  void *arg)
 {
@@ -270,6 +324,57 @@ periph_chr_access_gatt(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
     return 0;
 }
 
+#define PERIPH_NEW_ALERT_VAL_MAX_LEN    64
+
+static const uint8_t periph_new_alert_cat = 0x01; /* Simple alert. */
+static uint8_t periph_new_alert_val[PERIPH_NEW_ALERT_VAL_MAX_LEN];
+static uint16_t periph_new_alert_val_len;
+static const uint8_t periph_unr_alert_cat = 0x01; /* Simple alert. */
+static uint16_t periph_unr_alert_stat;
+static uint16_t periph_alert_not_ctrl_pt;
+
+static int
+periph_chr_access_alert(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
+                        union ble_gatt_access_ctxt *ctxt, void *arg)
+{
+    uint16_t uuid16;
+    int rc;
+
+    uuid16 = ble_uuid_128_to_16(ctxt->chr_access.chr->uuid128);
+    assert(uuid16 != 0);
+
+    switch (uuid16) {
+    case PERIPH_CHR_SUP_NEW_ALERT_CAT_UUID:
+        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+        ctxt->chr_access.data = (void *)&periph_new_alert_cat;
+        ctxt->chr_access.len = sizeof periph_new_alert_cat;
+        return 0;
+
+    case PERIPH_CHR_NEW_ALERT:
+        rc = periph_chr_write(op, ctxt, 0, sizeof periph_new_alert_val,
+                              periph_new_alert_val, &periph_new_alert_val_len);
+        return rc;
+
+    case PERIPH_CHR_SUP_UNR_ALERT_CAT_UUID:
+        assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+        ctxt->chr_access.data = (void *)&periph_unr_alert_cat;
+        ctxt->chr_access.len = sizeof periph_unr_alert_cat;
+        return 0;
+
+    case PERIPH_CHR_UNR_ALERT_STAT_UUID:
+        rc = periph_chr_write(op, ctxt, 2, 2, &periph_unr_alert_stat, NULL);
+        return rc;
+
+    case PERIPH_CHR_ALERT_NOT_CTRL_PT:
+        rc = periph_chr_write(op, ctxt, 2, 2, &periph_alert_not_ctrl_pt, NULL);
+        return rc;
+
+    default:
+        assert(0);
+        return BLE_ATT_ERR_UNLIKELY;
+    }
+}
+
 static int
 periph_gatt_cb(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
                union ble_gatt_access_ctxt *ctxt, void *arg)