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/22 01:21:44 UTC
[2/3] incubator-mynewt-larva git commit: GAP service.
GAP 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/430030c1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/430030c1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/430030c1
Branch: refs/heads/master
Commit: 430030c104c2ed22ab7c6550ca25cdf559b68cdf
Parents: 88dc6a6
Author: Christopher Collins <cc...@gmail.com>
Authored: Thu Jan 21 15:38:07 2016 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Thu Jan 21 16:20:52 2016 -0800
----------------------------------------------------------------------
net/nimble/host/include/host/ble_gap.h | 54 +++++++++++++++
net/nimble/host/src/ble_gap_conn.c | 43 +-----------
net/nimble/include/nimble/hci_common.h | 1 +
project/bleshell/src/bleshell_priv.h | 6 ++
project/bleshell/src/main.c | 14 ++++
project/bleshell/src/periph.c | 100 ++++++++++++++++++++++++++--
6 files changed, 172 insertions(+), 46 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/430030c1/net/nimble/host/include/host/ble_gap.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gap.h b/net/nimble/host/include/host/ble_gap.h
index e66adf6..d2598a7 100644
--- a/net/nimble/host/include/host/ble_gap.h
+++ b/net/nimble/host/include/host/ble_gap.h
@@ -22,6 +22,60 @@
struct hci_le_conn_complete;
struct hci_conn_update;
+/** 30 ms. */
+#define BLE_GAP_ADV_FAST_INTERVAL1_MIN (30 * 1000 / BLE_HCI_ADV_ITVL)
+
+/** 60 ms. */
+#define BLE_GAP_ADV_FAST_INTERVAL1_MAX (60 * 1000 / BLE_HCI_ADV_ITVL)
+
+/** 100 ms. */
+#define BLE_GAP_ADV_FAST_INTERVAL2_MIN (100 * 1000 / BLE_HCI_ADV_ITVL)
+
+/** 150 ms. */
+#define BLE_GAP_ADV_FAST_INTERVAL2_MAX (150 * 1000 / BLE_HCI_ADV_ITVL)
+
+/** 30 ms; active scanning. */
+#define BLE_GAP_SCAN_FAST_INTERVAL_MIN (30 * 1000 / BLE_HCI_ADV_ITVL)
+
+/** 60 ms; active scanning. */
+#define BLE_GAP_SCAN_FAST_INTERVAL_MAX (60 * 1000 / BLE_HCI_ADV_ITVL)
+
+/** 30 ms; active scanning. */
+#define BLE_GAP_SCAN_FAST_WINDOW (30 * 1000 / BLE_HCI_SCAN_ITVL)
+
+/* 30.72 seconds; active scanning. */
+#define BLE_GAP_SCAN_FAST_PERIOD (30.72 * 1000)
+
+/** 1.28 seconds; background scanning. */
+#define BLE_GAP_SCAN_SLOW_INTERVAL1 (1280 * 1000 / BLE_HCI_SCAN_ITVL)
+
+/** 11.25 ms; background scanning. */
+#define BLE_GAP_SCAN_SLOW_WINDOW1 (11.25 * 1000 / BLE_HCI_SCAN_ITVL)
+
+/** 10.24 seconds. */
+#define BLE_GAP_GEN_DISC_SCAN_MIN (10.24 * 1000)
+
+/** 1 second. */
+#define BLE_GAP_CONN_PAUSE_CENTRAL (1 * 1000)
+
+/** 5 seconds. */
+#define BLE_GAP_CONN_PAUSE_PERIPHERAL (5 * 1000)
+
+/* 30 ms. */
+#define BLE_GAP_INITIAL_CONN_ITVL_MIN (30 * 1000 / BLE_HCI_CONN_ITVL)
+
+/* 50 ms. */
+#define BLE_GAP_INITIAL_CONN_ITVL_MAX (50 * 1000 / BLE_HCI_CONN_ITVL)
+
+#define BLE_GAP_SVC_UUID16 0x1800
+#define BLE_GAP_CHR_UUID16_DEVICE_NAME 0x2a00
+#define BLE_GAP_CHR_UUID16_APPEARANCE 0x2a01
+#define BLE_GAP_CHR_UUID16_PERIPH_PRIV_FLAG 0x2a02
+#define BLE_GAP_CHR_UUID16_RECONNECT_ADDR 0x2a03
+#define BLE_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS 0x2a04
+
+#define BLE_GAP_APPEARANCE_GEN_COMPUTER 128
+
#define BLE_GAP_ADDR_TYPE_WL 0xff
#define BLE_GAP_EVENT_CONN 0
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/430030c1/net/nimble/host/src/ble_gap_conn.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gap_conn.c b/net/nimble/host/src/ble_gap_conn.c
index ad6c328..21c80be 100644
--- a/net/nimble/host/src/ble_gap_conn.c
+++ b/net/nimble/host/src/ble_gap_conn.c
@@ -74,45 +74,6 @@
#define BLE_GAP_CONN_STATE_U_REPLY_ACKED 3
#define BLE_GAP_CONN_STATE_U_NEG_REPLY 4
-/** 30 ms. */
-#define BLE_GAP_ADV_FAST_INTERVAL1_MIN (30 * 1000 / BLE_HCI_ADV_ITVL)
-
-/** 60 ms. */
-#define BLE_GAP_ADV_FAST_INTERVAL1_MAX (60 * 1000 / BLE_HCI_ADV_ITVL)
-
-/** 100 ms. */
-#define BLE_GAP_ADV_FAST_INTERVAL2_MIN (100 * 1000 / BLE_HCI_ADV_ITVL)
-
-/** 150 ms. */
-#define BLE_GAP_ADV_FAST_INTERVAL2_MAX (150 * 1000 / BLE_HCI_ADV_ITVL)
-
-/** 30 ms; active scanning. */
-#define BLE_GAP_SCAN_FAST_INTERVAL_MIN (30 * 1000 / BLE_HCI_ADV_ITVL)
-
-/** 60 ms; active scanning. */
-#define BLE_GAP_SCAN_FAST_INTERVAL_MAX (60 * 1000 / BLE_HCI_ADV_ITVL)
-
-/** 30 ms; active scanning. */
-#define BLE_GAP_SCAN_FAST_WINDOW (30 * 1000 / BLE_HCI_SCAN_ITVL)
-
-/* 30.72 seconds; active scanning. */
-#define BLE_GAP_SCAN_FAST_PERIOD (30.72 * 1000)
-
-/** 1.28 seconds; background scanning. */
-#define BLE_GAP_SCAN_SLOW_INTERVAL1 (1280 * 1000 / BLE_HCI_SCAN_ITVL)
-
-/** 11.25 ms; background scanning. */
-#define BLE_GAP_SCAN_SLOW_WINDOW1 (11.25 * 1000 / BLE_HCI_SCAN_ITVL)
-
-/** 10.24 seconds. */
-#define BLE_GAP_GEN_DISC_SCAN_MIN (10.24 * 1000)
-
-/** 1 second. */
-#define BLE_GAP_CONN_PAUSE_CENTRAL (1 * 1000)
-
-/** 5 seconds. */
-#define BLE_GAP_CONN_PAUSE_PERIPHERAL (5 * 1000)
-
/**
* The maximum amount of user data that can be put into the advertising data.
* Six bytes are reserved at the end for the flags field and the transmit power
@@ -1638,8 +1599,8 @@ ble_gap_conn_create_tx(void *arg)
sizeof hcc.peer_addr);
}
hcc.own_addr_type = BLE_HCI_ADV_OWN_ADDR_PUBLIC;
- hcc.conn_itvl_min = 24;
- hcc.conn_itvl_max = 40;
+ hcc.conn_itvl_min = BLE_GAP_INITIAL_CONN_ITVL_MIN;
+ hcc.conn_itvl_max = BLE_GAP_INITIAL_CONN_ITVL_MAX;
hcc.conn_latency = 0;
hcc.supervision_timeout = 0x0100; // XXX
hcc.min_ce_len = 0x0010; // XXX
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/430030c1/net/nimble/include/nimble/hci_common.h
----------------------------------------------------------------------
diff --git a/net/nimble/include/nimble/hci_common.h b/net/nimble/include/nimble/hci_common.h
index 528fbeb..607be2e 100644
--- a/net/nimble/include/nimble/hci_common.h
+++ b/net/nimble/include/nimble/hci_common.h
@@ -238,6 +238,7 @@
/* Create Connection */
#define BLE_HCI_CREATE_CONN_LEN (25)
+#define BLE_HCI_CONN_ITVL (1250) /* usecs */
#define BLE_HCI_CONN_FILT_NO_WL (0)
#define BLE_HCI_CONN_FILT_USE_WL (1)
#define BLE_HCI_CONN_FILT_MAX (1)
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/430030c1/project/bleshell/src/bleshell_priv.h
----------------------------------------------------------------------
diff --git a/project/bleshell/src/bleshell_priv.h b/project/bleshell/src/bleshell_priv.h
index 73219b0..76073f7 100644
--- a/project/bleshell/src/bleshell_priv.h
+++ b/project/bleshell/src/bleshell_priv.h
@@ -53,6 +53,12 @@ struct bleshell_conn {
extern struct bleshell_conn bleshell_conns[BLESHELL_MAX_CONNS];
extern int bleshell_num_conns;
+extern const char *bleshell_device_name;
+extern const uint16_t bleshell_appearance;
+extern const uint8_t bleshell_privacy_flag;
+extern uint8_t bleshell_reconnect_addr[6];
+extern uint8_t bleshell_pref_conn_params[8];
+
void print_addr(void *addr);
void print_uuid(void *uuid128);
struct cmd_entry *parse_cmd_find(struct cmd_entry *cmds, char *name);
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/430030c1/project/bleshell/src/main.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/main.c b/project/bleshell/src/main.c
index 46ea49b..324d0da 100755
--- a/project/bleshell/src/main.c
+++ b/project/bleshell/src/main.c
@@ -35,6 +35,8 @@
#include "host/ble_gatt.h"
#include "controller/ble_ll.h"
+#define BSWAP16(x) ((uint16_t)(((x) << 8) | (((x) & 0xff00) >> 8)))
+
/* Task 1 */
#define HOST_TASK_PRIO (1)
@@ -100,6 +102,12 @@ static struct os_mempool bleshell_chr_pool;
static void *bleshell_dsc_mem;
static struct os_mempool bleshell_dsc_pool;
+const char *bleshell_device_name = "mynewt nimble";
+const uint16_t bleshell_appearance = BSWAP16(BLE_GAP_APPEARANCE_GEN_COMPUTER);
+const uint8_t bleshell_privacy_flag = 0;
+uint8_t bleshell_reconnect_addr[6];
+uint8_t bleshell_pref_conn_params[8];
+
static void
bleshell_print_error(char *msg, uint16_t conn_handle,
struct ble_gatt_error *error)
@@ -979,6 +987,12 @@ main(void)
rc = cmd_init();
assert(rc == 0);
+ /* Initialize the preferred parameters. */
+ htole16(bleshell_pref_conn_params + 0, BLE_GAP_INITIAL_CONN_ITVL_MIN);
+ htole16(bleshell_pref_conn_params + 2, BLE_GAP_INITIAL_CONN_ITVL_MAX);
+ htole16(bleshell_pref_conn_params + 4, 0);
+ htole16(bleshell_pref_conn_params + 6, BSWAP16(0x100));
+
/* Start the OS */
os_start();
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/430030c1/project/bleshell/src/periph.c
----------------------------------------------------------------------
diff --git a/project/bleshell/src/periph.c b/project/bleshell/src/periph.c
index ce814c1..debd3f0 100644
--- a/project/bleshell/src/periph.c
+++ b/project/bleshell/src/periph.c
@@ -2,6 +2,7 @@
#include <string.h>
#include "console/console.h"
#include "host/ble_hs.h"
+#include "bleshell_priv.h"
#define PERIPH_SVC1_UUID 0x1234
#define PERIPH_SVC2_UUID 0x5678
@@ -22,11 +23,49 @@ static uint8_t periph_chr_data[3][PERIPH_CHR_MAX_LEN];
static uint16_t periph_chr_lens[3];
static int
+periph_chr_access_gap(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);
static const struct ble_gatt_svc_def periph_svcs[] = {
[0] = {
+ /*** Service: GAP. */
+ .type = BLE_GATT_SVC_TYPE_PRIMARY,
+ .uuid128 = BLE_UUID16(BLE_GAP_SVC_UUID16),
+ .characteristics = (struct ble_gatt_chr_def[]) { {
+ /*** Characteristic: Device Name. */
+ .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_DEVICE_NAME),
+ .access_cb = periph_chr_access_gap,
+ .flags = BLE_GATT_CHR_F_READ,
+ }, {
+ /*** Characteristic: Appearance. */
+ .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_APPEARANCE),
+ .access_cb = periph_chr_access_gap,
+ .flags = BLE_GATT_CHR_F_READ,
+ }, {
+ /*** Characteristic: Peripheral Privacy Flag. */
+ .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_PERIPH_PRIV_FLAG),
+ .access_cb = periph_chr_access_gap,
+ .flags = BLE_GATT_CHR_F_READ,
+ }, {
+ /*** Characteristic: Reconnection Address. */
+ .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_RECONNECT_ADDR),
+ .access_cb = periph_chr_access_gap,
+ .flags = BLE_GATT_CHR_F_WRITE,
+ }, {
+ /*** Characteristic: Peripheral Preferred Connection Parameters. */
+ .uuid128 = BLE_UUID16(BLE_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS),
+ .access_cb = periph_chr_access_gap,
+ .flags = BLE_GATT_CHR_F_READ,
+ }, {
+ 0, /* No more characteristics in this service. */
+ } },
+ },
+
+ [1] = {
/*** Service 0x1234. */
.type = BLE_GATT_SVC_TYPE_SECONDARY,
.uuid128 = BLE_UUID16(PERIPH_SVC1_UUID),
@@ -41,16 +80,16 @@ static const struct ble_gatt_svc_def periph_svcs[] = {
.access_cb = periph_gatt_cb,
.flags = BLE_GATT_CHR_F_READ,
}, {
- .uuid128 = NULL, /* No more characteristics in this service. */
+ 0, /* No more characteristics in this service. */
} },
},
- [1] = {
+ [2] = {
/*** Service 0x5678. */
.type = BLE_GATT_SVC_TYPE_PRIMARY,
.uuid128 = BLE_UUID16(PERIPH_SVC2_UUID),
.includes = (const struct ble_gatt_svc_def *[]) {
- &periph_svcs[0],
+ &periph_svcs[1],
NULL,
},
.characteristics = (struct ble_gatt_chr_def[]) { {
@@ -59,12 +98,12 @@ static const struct ble_gatt_svc_def periph_svcs[] = {
.access_cb = periph_gatt_cb,
.flags = CHR_F_FULL_ACCESS,
}, {
- .uuid128 = NULL, /* No more characteristics in this service. */
+ 0, /* No more characteristics in this service. */
} },
},
{
- .type = BLE_GATT_SVC_TYPE_END, /* No more services. */
+ 0, /* No more services. */
},
};
@@ -137,6 +176,57 @@ periph_gatt_write(uint16_t attr_handle, union ble_gatt_access_ctxt *ctxt,
}
static int
+periph_chr_access_gap(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
+ union ble_gatt_access_ctxt *ctxt, void *arg)
+{
+ uint16_t uuid16;
+
+ uuid16 = ble_uuid_128_to_16(ctxt->chr_access.chr->uuid128);
+ assert(uuid16 != 0);
+
+ switch (uuid16) {
+ case BLE_GAP_CHR_UUID16_DEVICE_NAME:
+ assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+ ctxt->chr_access.data = (void *)bleshell_device_name;
+ ctxt->chr_access.len = strlen(bleshell_device_name);
+ break;
+
+ case BLE_GAP_CHR_UUID16_APPEARANCE:
+ assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+ ctxt->chr_access.data = (void *)&bleshell_appearance;
+ ctxt->chr_access.len = sizeof bleshell_appearance;
+ break;
+
+ case BLE_GAP_CHR_UUID16_PERIPH_PRIV_FLAG:
+ assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+ ctxt->chr_access.data = (void *)&bleshell_privacy_flag;
+ ctxt->chr_access.len = sizeof bleshell_privacy_flag;
+ break;
+
+ case BLE_GAP_CHR_UUID16_RECONNECT_ADDR:
+ assert(op == BLE_GATT_ACCESS_OP_WRITE_CHR);
+ if (ctxt->chr_access.len != sizeof bleshell_reconnect_addr) {
+ return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN;
+ }
+ memcpy(bleshell_reconnect_addr, ctxt->chr_access.data,
+ sizeof bleshell_reconnect_addr);
+ break;
+
+ case BLE_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS:
+ assert(op == BLE_GATT_ACCESS_OP_READ_CHR);
+ ctxt->chr_access.data = (void *)&bleshell_pref_conn_params;
+ ctxt->chr_access.len = sizeof bleshell_pref_conn_params;
+ break;
+
+ default:
+ assert(0);
+ break;
+ }
+
+ return 0;
+}
+
+static int
periph_gatt_cb(uint16_t conn_handle, uint16_t attr_handle, uint8_t op,
union ble_gatt_access_ctxt *ctxt, void *arg)
{