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 2015/12/28 20:39:22 UTC
[1/3] incubator-mynewt-larva git commit: Split hostctlrtest into
centtest, prphtest projects
Repository: incubator-mynewt-larva
Updated Branches:
refs/heads/master cc55da90a -> 41fbd980a
Split hostctlrtest into centtest,prphtest projects
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/23c28c05
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/23c28c05
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/23c28c05
Branch: refs/heads/master
Commit: 23c28c05af6a2bb59cc64b3e4786681c27eb783c
Parents: cc55da9
Author: Christopher Collins <cc...@gmail.com>
Authored: Mon Dec 28 10:59:23 2015 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Mon Dec 28 10:59:23 2015 -0800
----------------------------------------------------------------------
net/nimble/host/src/ble_gatts.c | 2 +-
project/centtest/centtest.yml | 7 +
project/centtest/src/main.c | 351 ++++++++++++++++++++
project/hostctlrtest/hostctlrtest.yml | 7 -
project/hostctlrtest/src/main.c | 503 -----------------------------
project/prphtest/prphtest.yml | 7 +
project/prphtest/src/main.c | 347 ++++++++++++++++++++
7 files changed, 713 insertions(+), 511 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23c28c05/net/nimble/host/src/ble_gatts.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index 48e0921..b5de5b0 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -210,7 +210,7 @@ ble_gatts_register_characteristic(const struct ble_gatt_chr_def *chr)
static int
ble_gatts_register_svc(const struct ble_gatt_svc_def *svc,
- uint16_t *out_handle)
+ uint16_t *out_handle)
{
const struct ble_gatt_svc_def *incl;
const struct ble_gatt_chr_def *chr;
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23c28c05/project/centtest/centtest.yml
----------------------------------------------------------------------
diff --git a/project/centtest/centtest.yml b/project/centtest/centtest.yml
new file mode 100644
index 0000000..65f2590
--- /dev/null
+++ b/project/centtest/centtest.yml
@@ -0,0 +1,7 @@
+project.name: centtest
+project.eggs:
+ - libs/os
+ - net/nimble/controller
+ - net/nimble/host
+ - libs/console/full
+ - libs/baselibc
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23c28c05/project/centtest/src/main.c
----------------------------------------------------------------------
diff --git a/project/centtest/src/main.c b/project/centtest/src/main.c
new file mode 100755
index 0000000..1fb76f3
--- /dev/null
+++ b/project/centtest/src/main.c
@@ -0,0 +1,351 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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 "os/os.h"
+#include "bsp/bsp.h"
+#include "hal/hal_gpio.h"
+#include "hal/hal_cputime.h"
+#include "console/console.h"
+
+/* BLE */
+#include "nimble/ble.h"
+#include "host/host_hci.h"
+#include "host/ble_hs.h"
+#include "host/ble_hs_uuid.h"
+#include "host/ble_att.h"
+#include "host/ble_gap.h"
+#include "host/ble_gatt.h"
+#include "controller/ble_ll.h"
+
+/* Init all tasks */
+volatile int tasks_initialized;
+
+/* Task 1 */
+#define HOST_TASK_PRIO (1)
+
+/* For LED toggling */
+int g_led_pin;
+
+/* Our global device address (public) */
+uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
+
+/* Our random address (in case we need it) */
+uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
+
+/* A buffer for host advertising data */
+uint8_t g_host_adv_data[BLE_HCI_MAX_ADV_DATA_LEN];
+uint8_t g_host_adv_len;
+
+static uint8_t centtest_slv_addr[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
+static uint8_t centtest_mst_addr[6] = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a};
+
+/* Create a mbuf pool of BLE mbufs */
+#define MBUF_NUM_MBUFS (8)
+#define MBUF_BUF_SIZE (256 + sizeof(struct hci_data_hdr))
+#define MBUF_MEMBLOCK_SIZE (MBUF_BUF_SIZE + BLE_MBUF_PKT_OVERHEAD)
+
+#define MBUF_MEMPOOL_SIZE OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
+
+struct os_mbuf_pool g_mbuf_pool;
+struct os_mempool g_mbuf_mempool;
+os_membuf_t g_mbuf_buffer[MBUF_MEMPOOL_SIZE];
+
+/* CENTTEST variables */
+#define CENTTEST_STACK_SIZE (256)
+#define CENTTEST_TASK_PRIO (HOST_TASK_PRIO + 1)
+uint32_t g_next_os_time;
+int g_centtest_state;
+struct os_eventq g_centtest_evq;
+struct os_task centtest_task;
+os_stack_t centtest_stack[CENTTEST_STACK_SIZE];
+
+void
+bletest_inc_adv_pkt_num(void) { }
+
+
+static int
+centtest_on_read(uint16_t conn_handle, int status,
+ struct ble_gatt_attr *attr, void *arg)
+{
+ uint8_t *u8ptr;
+ int i;
+
+ if (status != 0) {
+ console_printf("characteristic read failure: status=%d\n", status);
+ return 0;
+ }
+
+ console_printf("characteristic read: handle=%d value=", attr->handle);
+ u8ptr = attr->value;
+ for (i = 0; i < attr->value_len; i++) {
+ if (i != 0) {
+ console_printf(":");
+ }
+ console_printf("%02x", u8ptr[i]);
+ }
+ console_printf("\n");
+
+ return 0;
+}
+
+static int
+centtest_on_disc_c(uint16_t conn_handle, int status,
+ struct ble_gatt_chr *chr, void *arg)
+{
+ int i;
+
+ if (status != 0) {
+ console_printf("characteristic discovery failure: status=%d\n",
+ status);
+ return 0;
+ }
+
+ if (chr == NULL) {
+ console_printf("characteristic discovery complete.\n");
+ return 0;
+ }
+
+ console_printf("characteristic discovered: decl_handle=%d value_handle=%d "
+ "properties=%d uuid=", chr->decl_handle, chr->value_handle,
+ chr->properties);
+ for (i = 0; i < 16; i++) {
+ if (i != 0) {
+ console_printf(":");
+ }
+ console_printf("%02x", chr->uuid128[i]);
+ }
+ console_printf("\n");
+
+ ble_gatt_read(conn_handle, chr->value_handle, centtest_on_read, NULL);
+
+ return 0;
+}
+
+static int
+centtest_on_disc_s(uint16_t conn_handle, int status,
+ struct ble_gatt_service *service, void *arg)
+{
+ int i;
+
+ if (status != 0) {
+ console_printf("service discovery failure: status=%d\n", status);
+ return 0;
+ }
+
+ if (service == NULL) {
+ console_printf("service discovery complete.");
+ return 0;
+ }
+
+ console_printf("service discovered: start_handle=%d end_handle=%d, uuid=",
+ service->start_handle, service->end_handle);
+ for (i = 0; i < 16; i++) {
+ if (i != 0) {
+ console_printf(":");
+ }
+ console_printf("%02x", service->uuid128[i]);
+ }
+ console_printf("\n");
+
+ ble_gatt_disc_all_chars(conn_handle, service->start_handle,
+ service->end_handle, centtest_on_disc_c,
+ NULL);
+
+ return 0;
+}
+
+static void
+centtest_print_adv_rpt(struct ble_gap_conn_adv_rpt *adv)
+{
+ console_printf("Received advertisement report:\n");
+ console_printf(" addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
+ adv->addr[0], adv->addr[1], adv->addr[2],
+ adv->addr[3], adv->addr[4], adv->addr[5]);
+ console_printf(" flags=0x%02x\n", adv->fields.flags);
+ console_printf(" name=");
+ console_write((char *)adv->fields.name, adv->fields.name_len);
+ console_printf("%s\n");
+}
+
+static void
+centtest_on_connect(struct ble_gap_conn_event *event, void *arg)
+{
+ switch (event->type) {
+ case BLE_GAP_CONN_EVENT_TYPE_CONNECT:
+ console_printf("connection complete; handle=%d status=%d "
+ "peer_addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
+ event->conn.handle, event->conn.status,
+ event->conn.peer_addr[0], event->conn.peer_addr[1],
+ event->conn.peer_addr[2], event->conn.peer_addr[3],
+ event->conn.peer_addr[4], event->conn.peer_addr[5]);
+
+ if (event->conn.status == 0) {
+ ble_gatt_disc_all_services(event->conn.handle,
+ centtest_on_disc_s, NULL);
+ }
+
+ break;
+
+ case BLE_GAP_CONN_EVENT_TYPE_ADV_RPT:
+ centtest_print_adv_rpt(&event->adv);
+ break;
+
+ case BLE_GAP_CONN_EVENT_TYPE_SCAN_DONE:
+ console_printf("scan complete\n");
+ break;
+ }
+}
+
+/**
+ * BLE test task
+ *
+ * @param arg
+ */
+void
+centtest_task_handler(void *arg)
+{
+ struct os_event *ev;
+ struct os_callout_func *cf;
+ int rc;
+
+ /* We are initialized */
+ console_printf("Starting BLE test task\n");
+
+ /* Initialize eventq */
+ os_eventq_init(&g_centtest_evq);
+
+ /* Init centtest variables */
+ g_centtest_state = 0;
+ g_next_os_time = os_time_get();
+
+ ble_gap_conn_set_cb(centtest_on_connect, NULL);
+
+ console_printf("INITIATOR\n");
+ //rc = ble_gap_conn_disc(20000, BLE_GAP_DISC_MODE_GEN);
+ rc = ble_gap_conn_direct_connect(BLE_HCI_ADV_PEER_ADDR_PUBLIC,
+ centtest_slv_addr);
+ assert(rc == 0);
+
+ while (1) {
+ ev = os_eventq_get(&g_centtest_evq);
+ switch (ev->ev_type) {
+ case OS_EVENT_T_TIMER:
+ cf = (struct os_callout_func *)ev;
+ assert(cf->cf_func);
+ cf->cf_func(cf->cf_arg);
+ break;
+ default:
+ assert(0);
+ break;
+ }
+ }
+}
+
+/**
+ * init_tasks
+ *
+ * Called by main.c after os_init(). This function performs initializations
+ * that are required before tasks are running.
+ *
+ * @return int 0 success; error otherwise.
+ */
+static int
+init_tasks(void)
+{
+ int rc;
+
+ os_task_init(¢test_task, "centtest", centtest_task_handler,
+ NULL, CENTTEST_TASK_PRIO, OS_WAIT_FOREVER,
+ centtest_stack, CENTTEST_STACK_SIZE);
+
+ tasks_initialized = 1;
+
+ /* Initialize host HCI */
+ rc = ble_hs_init(HOST_TASK_PRIO);
+ assert(rc == 0);
+
+ /* Initialize the BLE LL */
+ ble_ll_init();
+
+ return 0;
+}
+
+/**
+ * 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 i;
+ int rc;
+ uint32_t seed;
+
+ /* Initialize OS */
+ os_init();
+
+ /* Set cputime to count at 1 usec increments */
+ rc = cputime_init(1000000);
+ assert(rc == 0);
+
+ rc = os_mempool_init(&g_mbuf_mempool, MBUF_NUM_MBUFS,
+ MBUF_MEMBLOCK_SIZE, &g_mbuf_buffer[0], "mbuf_pool");
+
+ rc = os_mbuf_pool_init(&g_mbuf_pool, &g_mbuf_mempool, MBUF_MEMBLOCK_SIZE,
+ MBUF_NUM_MBUFS);
+ assert(rc == 0);
+
+ /* Dummy device address */
+ memcpy(g_dev_addr, centtest_mst_addr, 6);
+
+ /*
+ * Seed random number generator with least significant bytes of device
+ * address.
+ */
+ seed = 0;
+ for (i = 0; i < 4; ++i) {
+ seed |= g_dev_addr[i];
+ seed <<= 8;
+ }
+ srand(seed);
+
+ /* Set the led pin as an output */
+ g_led_pin = LED_BLINK_PIN;
+ gpio_init_out(g_led_pin, 1);
+
+ /* Init the console */
+ rc = console_init(NULL);
+ assert(rc == 0);
+
+ /* Init tasks */
+ init_tasks();
+
+ /* Start the OS */
+ os_start();
+
+ /* os start should never return. If it does, this should be an error */
+ assert(0);
+
+ return rc;
+}
+
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23c28c05/project/hostctlrtest/hostctlrtest.yml
----------------------------------------------------------------------
diff --git a/project/hostctlrtest/hostctlrtest.yml b/project/hostctlrtest/hostctlrtest.yml
deleted file mode 100644
index af5cb9c..0000000
--- a/project/hostctlrtest/hostctlrtest.yml
+++ /dev/null
@@ -1,7 +0,0 @@
-project.name: hostctlrtest
-project.eggs:
- - libs/os
- - net/nimble/controller
- - net/nimble/host
- - libs/console/full
- - libs/baselibc
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23c28c05/project/hostctlrtest/src/main.c
----------------------------------------------------------------------
diff --git a/project/hostctlrtest/src/main.c b/project/hostctlrtest/src/main.c
deleted file mode 100755
index 87051a9..0000000
--- a/project/hostctlrtest/src/main.c
+++ /dev/null
@@ -1,503 +0,0 @@
-/**
- * Copyright (c) 2015 Runtime Inc.
- *
- * Licensed 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.
- */
-#define HOSTCTLRTEST_ROLE_INITIATOR (0)
-#define HOSTCTLRTEST_ROLE_SCANNER (1)
-#define HOSTCTLRTEST_ROLE_ADVERTISER (2)
-
-#define HOSTCTLRTEST_CFG_ROLE (2)
-
-#include <assert.h>
-#include <string.h>
-#include <stdio.h>
-#include "os/os.h"
-#include "bsp/bsp.h"
-#include "hal/hal_gpio.h"
-#include "hal/hal_cputime.h"
-#include "console/console.h"
-
-/* BLE */
-#include "nimble/ble.h"
-#include "host/host_hci.h"
-#include "host/ble_hs.h"
-#include "host/ble_hs_uuid.h"
-#include "host/ble_att.h"
-#include "host/ble_gap.h"
-#include "host/ble_gatt.h"
-#include "controller/ble_ll.h"
-
-/* Init all tasks */
-volatile int tasks_initialized;
-
-/* Task 1 */
-#define HOST_TASK_PRIO (1)
-
-/* For LED toggling */
-int g_led_pin;
-
-/* Our global device address (public) */
-uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
-
-/* Our random address (in case we need it) */
-uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
-
-/* A buffer for host advertising data */
-uint8_t g_host_adv_data[BLE_HCI_MAX_ADV_DATA_LEN];
-uint8_t g_host_adv_len;
-
-static uint8_t hostctlrtest_slv_addr[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
-#if HOSTCTLRTEST_CFG_ROLE == HOSTCTLRTEST_ROLE_INITIATOR
-static uint8_t hostctlrtest_mst_addr[6] = {0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a};
-//static uint8_t hostctlrtest_slv2_addr[6] = {0x82, 0x6a, 0xd0, 0x48, 0xb4, 0xb0};
-#endif
-
-/* Create a mbuf pool of BLE mbufs */
-#define MBUF_NUM_MBUFS (8)
-#define MBUF_BUF_SIZE (256 + sizeof(struct hci_data_hdr))
-#define MBUF_MEMBLOCK_SIZE (MBUF_BUF_SIZE + BLE_MBUF_PKT_OVERHEAD)
-
-#define MBUF_MEMPOOL_SIZE OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
-
-struct os_mbuf_pool g_mbuf_pool;
-struct os_mempool g_mbuf_mempool;
-os_membuf_t g_mbuf_buffer[MBUF_MEMPOOL_SIZE];
-
-/* HOSTCTLRTEST variables */
-#define HOSTCTLRTEST_STACK_SIZE (256)
-#define HOSTCTLRTEST_TASK_PRIO (HOST_TASK_PRIO + 1)
-uint32_t g_next_os_time;
-int g_hostctlrtest_state;
-struct os_eventq g_hostctlrtest_evq;
-struct os_task hostctlrtest_task;
-os_stack_t hostctlrtest_stack[HOSTCTLRTEST_STACK_SIZE];
-
-void
-bletest_inc_adv_pkt_num(void) { }
-
-
-#if HOSTCTLRTEST_CFG_ROLE == HOSTCTLRTEST_ROLE_INITIATOR
-
-static int
-hostctlrtest_on_read(uint16_t conn_handle, int status,
- struct ble_gatt_attr *attr, void *arg)
-{
- uint8_t *u8ptr;
- int i;
-
- if (status != 0) {
- console_printf("characteristic read failure: status=%d\n", status);
- return 0;
- }
-
- console_printf("characteristic read: handle=%d value=", attr->handle);
- u8ptr = attr->value;
- for (i = 0; i < attr->value_len; i++) {
- if (i != 0) {
- console_printf(":");
- }
- console_printf("%02x", u8ptr[i]);
- }
- console_printf("\n");
-
- return 0;
-}
-
-static int
-hostctlrtest_on_disc_c(uint16_t conn_handle, int status,
- struct ble_gatt_chr *chr, void *arg)
-{
- int i;
-
- if (status != 0) {
- console_printf("characteristic discovery failure: status=%d\n",
- status);
- return 0;
- }
-
- if (chr == NULL) {
- console_printf("characteristic discovery complete.\n");
- return 0;
- }
-
- console_printf("characteristic discovered: decl_handle=%d value_handle=%d "
- "properties=%d uuid=", chr->decl_handle, chr->value_handle,
- chr->properties);
- for (i = 0; i < 16; i++) {
- if (i != 0) {
- console_printf(":");
- }
- console_printf("%02x", chr->uuid128[i]);
- }
- console_printf("\n");
-
- ble_gatt_read(conn_handle, chr->value_handle, hostctlrtest_on_read, NULL);
-
- return 0;
-}
-
-static int
-hostctlrtest_on_disc_s(uint16_t conn_handle, int status,
- struct ble_gatt_service *service, void *arg)
-{
- int i;
-
- if (status != 0) {
- console_printf("service discovery failure: status=%d\n", status);
- return 0;
- }
-
- if (service == NULL) {
- console_printf("service discovery complete.");
- return 0;
- }
-
- console_printf("service discovered: start_handle=%d end_handle=%d, uuid=",
- service->start_handle, service->end_handle);
- for (i = 0; i < 16; i++) {
- if (i != 0) {
- console_printf(":");
- }
- console_printf("%02x", service->uuid128[i]);
- }
- console_printf("\n");
-
- ble_gatt_disc_all_chars(conn_handle, service->start_handle,
- service->end_handle, hostctlrtest_on_disc_c,
- NULL);
-
- return 0;
-}
-
-static void
-hostctlrtest_print_adv_rpt(struct ble_gap_conn_adv_rpt *adv)
-{
- console_printf("Received advertisement report:\n");
- console_printf(" addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
- adv->addr[0], adv->addr[1], adv->addr[2],
- adv->addr[3], adv->addr[4], adv->addr[5]);
- console_printf(" flags=0x%02x\n", adv->fields.flags);
- console_printf(" name=");
- console_write((char *)adv->fields.name, adv->fields.name_len);
- console_printf("%s\n");
-}
-
-#endif
-
-#if HOSTCTLRTEST_CFG_ROLE == HOSTCTLRTEST_ROLE_ADVERTISER
-static uint16_t hostctlrtest_service_handle;
-static uint16_t hostctlrtest_char1_handle;
-static uint16_t hostctlrtest_data1_handle;
-static uint16_t hostctlrtest_char2_handle;
-static uint16_t hostctlrtest_data2_handle;
-
-static int
-hostctlrtest_attr_cb(uint16_t handle_id, uint8_t *uuid128, uint8_t op,
- union ble_att_svr_access_ctxt *ctxt, void *arg)
-{
- static uint8_t buf[128];
-
- assert(op == BLE_ATT_ACCESS_OP_READ);
-
- if (handle_id == hostctlrtest_service_handle) {
- console_printf("reading service declaration");
- htole16(buf, 0x1234);
- ctxt->ahc_read.attr_data = buf;
- ctxt->ahc_read.attr_len = 2;
- } else if (handle_id == hostctlrtest_char1_handle) {
- console_printf("reading characteristic1 declaration");
-
- /* Properties. */
- buf[0] = 0;
-
- /* Value handle. */
- htole16(buf + 1, hostctlrtest_data1_handle);
-
- /* UUID. */
- htole16(buf + 3, 0x5656);
-
- ctxt->ahc_read.attr_data = buf;
- ctxt->ahc_read.attr_len = 5;
- } else if (handle_id == hostctlrtest_data1_handle) {
- console_printf("reading characteristic1 value");
- memcpy(buf, "char1", 5);
- ctxt->ahc_read.attr_data = buf;
- ctxt->ahc_read.attr_len = 5;
- } else if (handle_id == hostctlrtest_char2_handle) {
- console_printf("reading characteristic2 declaration");
-
- /* Properties. */
- buf[0] = 0;
-
- /* Value handle. */
- htole16(buf + 1, hostctlrtest_data2_handle);
-
- /* UUID. */
- htole16(buf + 3, 0x6767);
-
- ctxt->ahc_read.attr_data = buf;
- ctxt->ahc_read.attr_len = 5;
- } else if (handle_id == hostctlrtest_data2_handle) {
- console_printf("reading characteristic2 value");
- memcpy(buf, "char2", 5);
- ctxt->ahc_read.attr_data = buf;
- ctxt->ahc_read.attr_len = 5;
- } else {
- assert(0);
- }
-
- return 0;
-}
-
-static void
-hostctlrtest_register_attrs(void)
-{
- uint8_t uuid128[16];
- int rc;
-
- /* Service. */
- rc = ble_hs_uuid_from_16bit(BLE_ATT_UUID_PRIMARY_SERVICE, uuid128);
- assert(rc == 0);
- rc = ble_att_svr_register(uuid128, 0, &hostctlrtest_service_handle,
- hostctlrtest_attr_cb, NULL);
- assert(rc == 0);
-
- /* Characteristic 1 (UUID=0x5656).*/
- rc = ble_hs_uuid_from_16bit(BLE_ATT_UUID_CHARACTERISTIC, uuid128);
- assert(rc == 0);
- rc = ble_att_svr_register(uuid128, 0, &hostctlrtest_char1_handle,
- hostctlrtest_attr_cb, NULL);
- assert(rc == 0);
-
- rc = ble_hs_uuid_from_16bit(0x5656, uuid128);
- assert(rc == 0);
- rc = ble_att_svr_register(uuid128, 0, &hostctlrtest_data1_handle,
- hostctlrtest_attr_cb, NULL);
- assert(rc == 0);
-
- /* Characteristic 2 (UUID=0x6767).*/
- rc = ble_hs_uuid_from_16bit(BLE_ATT_UUID_CHARACTERISTIC, uuid128);
- assert(rc == 0);
- rc = ble_att_svr_register(uuid128, 0, &hostctlrtest_char2_handle,
- hostctlrtest_attr_cb, NULL);
- assert(rc == 0);
-
- rc = ble_hs_uuid_from_16bit(0x6767, uuid128);
- assert(rc == 0);
- rc = ble_att_svr_register(uuid128, 0, &hostctlrtest_data2_handle,
- hostctlrtest_attr_cb, NULL);
- assert(rc == 0);
-}
-#endif
-
-static void
-hostctlrtest_on_connect(struct ble_gap_conn_event *event, void *arg)
-{
- switch (event->type) {
- case BLE_GAP_CONN_EVENT_TYPE_CONNECT:
- console_printf("connection complete; handle=%d status=%d "
- "peer_addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
- event->conn.handle, event->conn.status,
- event->conn.peer_addr[0], event->conn.peer_addr[1],
- event->conn.peer_addr[2], event->conn.peer_addr[3],
- event->conn.peer_addr[4], event->conn.peer_addr[5]);
-
-#if HOSTCTLRTEST_CFG_ROLE == HOSTCTLRTEST_ROLE_INITIATOR
- if (event->conn.status == 0) {
- ble_gatt_disc_all_services(event->conn.handle,
- hostctlrtest_on_disc_s, NULL);
-#if 0
- int rc;
- if (memcmp(event->conn.peer_addr, hostctlrtest_slv_addr) == 0) {
- console_printf("CONNECTING TO DEVICE 2\n");
- rc = ble_gap_conn_direct_connect(BLE_HCI_ADV_PEER_ADDR_PUBLIC,
- hostctlrtest_slv2_addr);
- if (rc != 0) {
- console_printf("FAILED CONNECT; rc=%d\n", rc);
- }
- }
-#endif
- }
-
- break;
-
- case BLE_GAP_CONN_EVENT_TYPE_ADV_RPT:
- hostctlrtest_print_adv_rpt(&event->adv);
- break;
-
- case BLE_GAP_CONN_EVENT_TYPE_SCAN_DONE:
- console_printf("scan complete\n");
- break;
-#endif
- }
-}
-
-/**
- * BLE test task
- *
- * @param arg
- */
-void
-hostctlrtest_task_handler(void *arg)
-{
- struct os_event *ev;
- struct os_callout_func *cf;
- int rc;
-
- /* We are initialized */
- console_printf("Starting BLE test task\n");
-
- /* Initialize eventq */
- os_eventq_init(&g_hostctlrtest_evq);
-
- /* Init hostctlrtest variables */
- g_hostctlrtest_state = 0;
- g_next_os_time = os_time_get();
-
- ble_gap_conn_set_cb(hostctlrtest_on_connect, NULL);
-
-#if HOSTCTLRTEST_CFG_ROLE == HOSTCTLRTEST_ROLE_ADVERTISER
- struct ble_hs_adv_fields fields;
-
- hostctlrtest_register_attrs();
- console_printf("ADVERTISER\n");
-
- fields.name = (uint8_t *)"nimble";
- fields.name_len = 6;
- fields.name_is_complete = 1;
- rc = ble_gap_conn_set_adv_fields(&fields);
- assert(rc == 0);
-
- rc = ble_gap_conn_advertise(BLE_GAP_DISC_MODE_NON, BLE_GAP_CONN_MODE_UND,
- NULL, 0);
-#else
- console_printf("INITIATOR\n");
- //rc = ble_gap_conn_disc(20000, BLE_GAP_DISC_MODE_GEN);
- rc = ble_gap_conn_direct_connect(BLE_HCI_ADV_PEER_ADDR_PUBLIC,
- hostctlrtest_slv_addr);
-#endif
- assert(rc == 0);
-
- while (1) {
- ev = os_eventq_get(&g_hostctlrtest_evq);
- switch (ev->ev_type) {
- case OS_EVENT_T_TIMER:
- cf = (struct os_callout_func *)ev;
- assert(cf->cf_func);
- cf->cf_func(cf->cf_arg);
- break;
- default:
- assert(0);
- break;
- }
- }
-}
-
-/**
- * init_tasks
- *
- * Called by main.c after os_init(). This function performs initializations
- * that are required before tasks are running.
- *
- * @return int 0 success; error otherwise.
- */
-static int
-init_tasks(void)
-{
- int rc;
-
- os_task_init(&hostctlrtest_task, "hostctlrtest", hostctlrtest_task_handler,
- NULL, HOSTCTLRTEST_TASK_PRIO, OS_WAIT_FOREVER,
- hostctlrtest_stack, HOSTCTLRTEST_STACK_SIZE);
-
- tasks_initialized = 1;
-
- /* Initialize host HCI */
- rc = ble_hs_init(HOST_TASK_PRIO);
- assert(rc == 0);
-
- /* Initialize the BLE LL */
- ble_ll_init();
-
- return 0;
-}
-
-/**
- * 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 i;
- int rc;
- uint32_t seed;
-
- /* Initialize OS */
- os_init();
-
- /* Set cputime to count at 1 usec increments */
- rc = cputime_init(1000000);
- assert(rc == 0);
-
- rc = os_mempool_init(&g_mbuf_mempool, MBUF_NUM_MBUFS,
- MBUF_MEMBLOCK_SIZE, &g_mbuf_buffer[0], "mbuf_pool");
-
- rc = os_mbuf_pool_init(&g_mbuf_pool, &g_mbuf_mempool, MBUF_MEMBLOCK_SIZE,
- MBUF_NUM_MBUFS);
- assert(rc == 0);
-
- /* Dummy device address */
-#if HOSTCTLRTEST_CFG_ROLE == HOSTCTLRTEST_ROLE_ADVERTISER
- memcpy(g_dev_addr, hostctlrtest_slv_addr, 6);
-#else
- memcpy(g_dev_addr, hostctlrtest_mst_addr, 6);
-#endif
-
- /*
- * Seed random number generator with least significant bytes of device
- * address.
- */
- seed = 0;
- for (i = 0; i < 4; ++i) {
- seed |= g_dev_addr[i];
- seed <<= 8;
- }
- srand(seed);
-
- /* Set the led pin as an output */
- g_led_pin = LED_BLINK_PIN;
- gpio_init_out(g_led_pin, 1);
-
- /* Init the console */
- rc = console_init(NULL);
- assert(rc == 0);
-
- /* Init tasks */
- init_tasks();
-
- /* Start the OS */
- os_start();
-
- /* os start should never return. If it does, this should be an error */
- assert(0);
-
- return rc;
-}
-
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23c28c05/project/prphtest/prphtest.yml
----------------------------------------------------------------------
diff --git a/project/prphtest/prphtest.yml b/project/prphtest/prphtest.yml
new file mode 100644
index 0000000..7b4091b
--- /dev/null
+++ b/project/prphtest/prphtest.yml
@@ -0,0 +1,7 @@
+project.name: prphtest
+project.eggs:
+ - libs/os
+ - net/nimble/controller
+ - net/nimble/host
+ - libs/console/full
+ - libs/baselibc
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/23c28c05/project/prphtest/src/main.c
----------------------------------------------------------------------
diff --git a/project/prphtest/src/main.c b/project/prphtest/src/main.c
new file mode 100755
index 0000000..fff13de
--- /dev/null
+++ b/project/prphtest/src/main.c
@@ -0,0 +1,347 @@
+/**
+ * Copyright (c) 2015 Runtime Inc.
+ *
+ * Licensed 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 "os/os.h"
+#include "bsp/bsp.h"
+#include "hal/hal_gpio.h"
+#include "hal/hal_cputime.h"
+#include "console/console.h"
+
+/* BLE */
+#include "nimble/ble.h"
+#include "host/host_hci.h"
+#include "host/ble_hs.h"
+#include "host/ble_hs_uuid.h"
+#include "host/ble_att.h"
+#include "host/ble_gap.h"
+#include "host/ble_gatt.h"
+#include "controller/ble_ll.h"
+
+/* Init all tasks */
+volatile int tasks_initialized;
+
+/* Task 1 */
+#define HOST_TASK_PRIO (1)
+
+/* For LED toggling */
+int g_led_pin;
+
+/* Our global device address (public) */
+uint8_t g_dev_addr[BLE_DEV_ADDR_LEN];
+
+/* Our random address (in case we need it) */
+uint8_t g_random_addr[BLE_DEV_ADDR_LEN];
+
+/* A buffer for host advertising data */
+uint8_t g_host_adv_data[BLE_HCI_MAX_ADV_DATA_LEN];
+uint8_t g_host_adv_len;
+
+static uint8_t prphtest_slv_addr[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06};
+
+/* Create a mbuf pool of BLE mbufs */
+#define MBUF_NUM_MBUFS (8)
+#define MBUF_BUF_SIZE (256 + sizeof(struct hci_data_hdr))
+#define MBUF_MEMBLOCK_SIZE (MBUF_BUF_SIZE + BLE_MBUF_PKT_OVERHEAD)
+
+#define MBUF_MEMPOOL_SIZE OS_MEMPOOL_SIZE(MBUF_NUM_MBUFS, MBUF_MEMBLOCK_SIZE)
+
+struct os_mbuf_pool g_mbuf_pool;
+struct os_mempool g_mbuf_mempool;
+os_membuf_t g_mbuf_buffer[MBUF_MEMPOOL_SIZE];
+
+/* PRPHTEST variables */
+#define PRPHTEST_STACK_SIZE (256)
+#define PRPHTEST_TASK_PRIO (HOST_TASK_PRIO + 1)
+uint32_t g_next_os_time;
+int g_prphtest_state;
+struct os_eventq g_prphtest_evq;
+struct os_task prphtest_task;
+os_stack_t prphtest_stack[PRPHTEST_STACK_SIZE];
+
+void
+bletest_inc_adv_pkt_num(void) { }
+
+static uint16_t prphtest_service_handle;
+static uint16_t prphtest_char1_handle;
+static uint16_t prphtest_data1_handle;
+static uint16_t prphtest_char2_handle;
+static uint16_t prphtest_data2_handle;
+
+static int
+prphtest_attr_cb(uint16_t handle_id, uint8_t *uuid128, uint8_t op,
+ union ble_att_svr_access_ctxt *ctxt, void *arg)
+{
+ static uint8_t buf[128];
+
+ assert(op == BLE_ATT_ACCESS_OP_READ);
+
+ if (handle_id == prphtest_service_handle) {
+ console_printf("reading service declaration");
+ htole16(buf, 0x1234);
+ ctxt->ahc_read.attr_data = buf;
+ ctxt->ahc_read.attr_len = 2;
+ } else if (handle_id == prphtest_char1_handle) {
+ console_printf("reading characteristic1 declaration");
+
+ /* Properties. */
+ buf[0] = 0;
+
+ /* Value handle. */
+ htole16(buf + 1, prphtest_data1_handle);
+
+ /* UUID. */
+ htole16(buf + 3, 0x5656);
+
+ ctxt->ahc_read.attr_data = buf;
+ ctxt->ahc_read.attr_len = 5;
+ } else if (handle_id == prphtest_data1_handle) {
+ console_printf("reading characteristic1 value");
+ memcpy(buf, "char1", 5);
+ ctxt->ahc_read.attr_data = buf;
+ ctxt->ahc_read.attr_len = 5;
+ } else if (handle_id == prphtest_char2_handle) {
+ console_printf("reading characteristic2 declaration");
+
+ /* Properties. */
+ buf[0] = 0;
+
+ /* Value handle. */
+ htole16(buf + 1, prphtest_data2_handle);
+
+ /* UUID. */
+ htole16(buf + 3, 0x6767);
+
+ ctxt->ahc_read.attr_data = buf;
+ ctxt->ahc_read.attr_len = 5;
+ } else if (handle_id == prphtest_data2_handle) {
+ console_printf("reading characteristic2 value");
+ memcpy(buf, "char2", 5);
+ ctxt->ahc_read.attr_data = buf;
+ ctxt->ahc_read.attr_len = 5;
+ } else {
+ assert(0);
+ }
+
+ return 0;
+}
+
+static void
+prphtest_register_attrs(void)
+{
+ uint8_t uuid128[16];
+ int rc;
+
+ /* Service. */
+ rc = ble_hs_uuid_from_16bit(BLE_ATT_UUID_PRIMARY_SERVICE, uuid128);
+ assert(rc == 0);
+ rc = ble_att_svr_register(uuid128, 0, &prphtest_service_handle,
+ prphtest_attr_cb, NULL);
+ assert(rc == 0);
+
+ /* Characteristic 1 (UUID=0x5656).*/
+ rc = ble_hs_uuid_from_16bit(BLE_ATT_UUID_CHARACTERISTIC, uuid128);
+ assert(rc == 0);
+ rc = ble_att_svr_register(uuid128, 0, &prphtest_char1_handle,
+ prphtest_attr_cb, NULL);
+ assert(rc == 0);
+
+ rc = ble_hs_uuid_from_16bit(0x5656, uuid128);
+ assert(rc == 0);
+ rc = ble_att_svr_register(uuid128, 0, &prphtest_data1_handle,
+ prphtest_attr_cb, NULL);
+ assert(rc == 0);
+
+ /* Characteristic 2 (UUID=0x6767).*/
+ rc = ble_hs_uuid_from_16bit(BLE_ATT_UUID_CHARACTERISTIC, uuid128);
+ assert(rc == 0);
+ rc = ble_att_svr_register(uuid128, 0, &prphtest_char2_handle,
+ prphtest_attr_cb, NULL);
+ assert(rc == 0);
+
+ rc = ble_hs_uuid_from_16bit(0x6767, uuid128);
+ assert(rc == 0);
+ rc = ble_att_svr_register(uuid128, 0, &prphtest_data2_handle,
+ prphtest_attr_cb, NULL);
+ assert(rc == 0);
+}
+
+static void
+prphtest_on_connect(struct ble_gap_conn_event *event, void *arg)
+{
+ switch (event->type) {
+ case BLE_GAP_CONN_EVENT_TYPE_CONNECT:
+ console_printf("connection complete; handle=%d status=%d "
+ "peer_addr=%02x:%02x:%02x:%02x:%02x:%02x\n",
+ event->conn.handle, event->conn.status,
+ event->conn.peer_addr[0], event->conn.peer_addr[1],
+ event->conn.peer_addr[2], event->conn.peer_addr[3],
+ event->conn.peer_addr[4], event->conn.peer_addr[5]);
+ break;
+
+ default:
+ console_printf("unexpected connection event; type=%d\n", event->type);
+ break;
+ }
+}
+
+/**
+ * BLE test task
+ *
+ * @param arg
+ */
+void
+prphtest_task_handler(void *arg)
+{
+ struct os_event *ev;
+ struct os_callout_func *cf;
+ int rc;
+
+ /* We are initialized */
+ console_printf("Starting BLE test task\n");
+
+ /* Initialize eventq */
+ os_eventq_init(&g_prphtest_evq);
+
+ /* Init prphtest variables */
+ g_prphtest_state = 0;
+ g_next_os_time = os_time_get();
+
+ ble_gap_conn_set_cb(prphtest_on_connect, NULL);
+
+ struct ble_hs_adv_fields fields;
+
+ prphtest_register_attrs();
+ console_printf("ADVERTISER\n");
+
+ fields.name = (uint8_t *)"nimble";
+ fields.name_len = 6;
+ fields.name_is_complete = 1;
+ rc = ble_gap_conn_set_adv_fields(&fields);
+ assert(rc == 0);
+
+ rc = ble_gap_conn_advertise(BLE_GAP_DISC_MODE_NON, BLE_GAP_CONN_MODE_UND,
+ NULL, 0);
+ assert(rc == 0);
+
+ while (1) {
+ ev = os_eventq_get(&g_prphtest_evq);
+ switch (ev->ev_type) {
+ case OS_EVENT_T_TIMER:
+ cf = (struct os_callout_func *)ev;
+ assert(cf->cf_func);
+ cf->cf_func(cf->cf_arg);
+ break;
+ default:
+ assert(0);
+ break;
+ }
+ }
+}
+
+/**
+ * init_tasks
+ *
+ * Called by main.c after os_init(). This function performs initializations
+ * that are required before tasks are running.
+ *
+ * @return int 0 success; error otherwise.
+ */
+static int
+init_tasks(void)
+{
+ int rc;
+
+ os_task_init(&prphtest_task, "prphtest", prphtest_task_handler,
+ NULL, PRPHTEST_TASK_PRIO, OS_WAIT_FOREVER,
+ prphtest_stack, PRPHTEST_STACK_SIZE);
+
+ tasks_initialized = 1;
+
+ /* Initialize host HCI */
+ rc = ble_hs_init(HOST_TASK_PRIO);
+ assert(rc == 0);
+
+ /* Initialize the BLE LL */
+ ble_ll_init();
+
+ return 0;
+}
+
+/**
+ * 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 i;
+ int rc;
+ uint32_t seed;
+
+ /* Initialize OS */
+ os_init();
+
+ /* Set cputime to count at 1 usec increments */
+ rc = cputime_init(1000000);
+ assert(rc == 0);
+
+ rc = os_mempool_init(&g_mbuf_mempool, MBUF_NUM_MBUFS,
+ MBUF_MEMBLOCK_SIZE, &g_mbuf_buffer[0], "mbuf_pool");
+
+ rc = os_mbuf_pool_init(&g_mbuf_pool, &g_mbuf_mempool, MBUF_MEMBLOCK_SIZE,
+ MBUF_NUM_MBUFS);
+ assert(rc == 0);
+
+ /* Dummy device address */
+ memcpy(g_dev_addr, prphtest_slv_addr, 6);
+
+ /*
+ * Seed random number generator with least significant bytes of device
+ * address.
+ */
+ seed = 0;
+ for (i = 0; i < 4; ++i) {
+ seed |= g_dev_addr[i];
+ seed <<= 8;
+ }
+ srand(seed);
+
+ /* Set the led pin as an output */
+ g_led_pin = LED_BLINK_PIN;
+ gpio_init_out(g_led_pin, 1);
+
+ /* Init the console */
+ rc = console_init(NULL);
+ assert(rc == 0);
+
+ /* Init tasks */
+ init_tasks();
+
+ /* Start the OS */
+ os_start();
+
+ /* os start should never return. If it does, this should be an error */
+ assert(0);
+
+ return rc;
+}
[2/3] incubator-mynewt-larva git commit: Fix some GATT server crashes.
Posted by cc...@apache.org.
Fix some GATT server crashes.
(complete lack of testing for last week's commit!)
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/38d7d737
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/38d7d737
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/38d7d737
Branch: refs/heads/master
Commit: 38d7d7371b0fefaeb1d43151300ba73ec95afbf0
Parents: 23c28c0
Author: Christopher Collins <cc...@gmail.com>
Authored: Mon Dec 28 11:37:47 2015 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Mon Dec 28 11:39:06 2015 -0800
----------------------------------------------------------------------
net/nimble/host/include/host/ble_att.h | 3 +++
net/nimble/host/include/host/ble_gatt.h | 5 ++--
net/nimble/host/src/ble_att_svr.c | 21 ++++++++++++++++
net/nimble/host/src/ble_gatt.c | 6 +++++
net/nimble/host/src/ble_gatts.c | 37 +++++++++++++++++++++++-----
5 files changed, 64 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/38d7d737/net/nimble/host/include/host/ble_att.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_att.h b/net/nimble/host/include/host/ble_att.h
index 52046c5..7705f39 100644
--- a/net/nimble/host/include/host/ble_att.h
+++ b/net/nimble/host/include/host/ble_att.h
@@ -99,5 +99,8 @@ typedef int ble_att_svr_access_fn(uint16_t handle_id, uint8_t *uuid128,
int ble_att_svr_register(uint8_t *uuid, uint8_t flags, uint16_t *handle_id,
ble_att_svr_access_fn *cb, void *cb_arg);
+int ble_att_svr_register_uuid16(uint16_t uuid16, uint8_t flags,
+ uint16_t *handle_id, ble_att_svr_access_fn *cb,
+ void *cb_arg);
#endif
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/38d7d737/net/nimble/host/include/host/ble_gatt.h
----------------------------------------------------------------------
diff --git a/net/nimble/host/include/host/ble_gatt.h b/net/nimble/host/include/host/ble_gatt.h
index fbba391..6706613 100644
--- a/net/nimble/host/include/host/ble_gatt.h
+++ b/net/nimble/host/include/host/ble_gatt.h
@@ -80,15 +80,16 @@ struct ble_gatt_dsc_def {
};
struct ble_gatt_chr_def {
- uint8_t *uuid128;
+ uint8_t *uuid128; /* NULL if no more characteristics. */
ble_att_svr_access_fn *access_cb;
+ void *arg;
struct ble_gatt_dsc_def *descriptors;
uint8_t properties;
};
#define BLE_GATT_SVC_TYPE_END 0
#define BLE_GATT_SVC_TYPE_PRIMARY 1
-#define BLE_GATT_SVC_TYPE_SECONDAY 2
+#define BLE_GATT_SVC_TYPE_SECONDARY 2
struct ble_gatt_svc_def {
uint8_t type;
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/38d7d737/net/nimble/host/src/ble_att_svr.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_att_svr.c b/net/nimble/host/src/ble_att_svr.c
index 4975d65..40f99dd 100644
--- a/net/nimble/host/src/ble_att_svr.c
+++ b/net/nimble/host/src/ble_att_svr.c
@@ -157,6 +157,27 @@ ble_att_svr_register(uint8_t *uuid, uint8_t flags, uint16_t *handle_id,
return 0;
}
+int
+ble_att_svr_register_uuid16(uint16_t uuid16, uint8_t flags,
+ uint16_t *handle_id, ble_att_svr_access_fn *cb,
+ void *cb_arg)
+{
+ uint8_t uuid128[16];
+ int rc;
+
+ rc = ble_hs_uuid_from_16bit(uuid16, uuid128);
+ if (rc != 0) {
+ return rc;
+ }
+
+ rc = ble_att_svr_register(uuid128, flags, handle_id, cb, cb_arg);
+ if (rc != 0) {
+ return rc;
+ }
+
+ return 0;
+}
+
/**
* Walk the host attribute list, calling walk_func on each entry with argument.
* If walk_func wants to stop iteration, it returns 1. To continue iteration
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/38d7d737/net/nimble/host/src/ble_gatt.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatt.c b/net/nimble/host/src/ble_gatt.c
index de2766a..920e4e2 100644
--- a/net/nimble/host/src/ble_gatt.c
+++ b/net/nimble/host/src/ble_gatt.c
@@ -62,6 +62,12 @@ ble_gatt_connection_broken(uint16_t conn_handle)
}
int
+ble_gatt_register_services(const struct ble_gatt_svc_def *svcs)
+{
+ return ble_gatts_register_services(svcs);
+}
+
+int
ble_gatt_init(void)
{
int rc;
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/38d7d737/net/nimble/host/src/ble_gatts.c
----------------------------------------------------------------------
diff --git a/net/nimble/host/src/ble_gatts.c b/net/nimble/host/src/ble_gatts.c
index b5de5b0..d5010c8 100644
--- a/net/nimble/host/src/ble_gatts.c
+++ b/net/nimble/host/src/ble_gatts.c
@@ -106,6 +106,7 @@ ble_gatts_chr_access(uint16_t handle_id, uint8_t *uuid128, uint8_t op,
memcpy(buf + 3, chr->uuid128, 16);
ctxt->ahc_read.attr_len = 19;
}
+ ctxt->ahc_read.attr_data = buf;
return 0;
}
@@ -180,7 +181,7 @@ ble_gatts_register_dsc(const struct ble_gatt_dsc_def *dsc)
}
static int
-ble_gatts_register_characteristic(const struct ble_gatt_chr_def *chr)
+ble_gatts_register_chr(const struct ble_gatt_chr_def *chr)
{
struct ble_gatt_dsc_def *dsc;
uint16_t handle;
@@ -189,8 +190,18 @@ ble_gatts_register_characteristic(const struct ble_gatt_chr_def *chr)
/* Register characteristic declaration attribute (cast away const on
* callback arg).
*/
- rc = ble_att_svr_register(chr->uuid128, HA_FLAG_PERM_READ, &handle,
- ble_gatts_chr_access, (void *)chr);
+ rc = ble_att_svr_register_uuid16(BLE_ATT_UUID_CHARACTERISTIC,
+ HA_FLAG_PERM_READ, &handle,
+ ble_gatts_chr_access, (void *)chr);
+ if (rc != 0) {
+ return rc;
+ }
+
+ /* Register characteristic value attribute (cast away const on callback
+ * arg).
+ */
+ rc = ble_att_svr_register(chr->uuid128, HA_FLAG_PERM_READ /*XXX*/, &handle,
+ chr->access_cb, (void *)chr->arg);
if (rc != 0) {
return rc;
}
@@ -214,6 +225,7 @@ ble_gatts_register_svc(const struct ble_gatt_svc_def *svc,
{
const struct ble_gatt_svc_def *incl;
const struct ble_gatt_chr_def *chr;
+ uint16_t uuid16;
int idx;
int rc;
@@ -224,8 +236,21 @@ ble_gatts_register_svc(const struct ble_gatt_svc_def *svc,
/* Register service definition attribute (cast away const on callback
* arg).
*/
- rc = ble_att_svr_register(svc->uuid128, HA_FLAG_PERM_READ, out_handle,
- ble_gatts_svc_access, (void *)svc);
+ switch (svc->type) {
+ case BLE_GATT_SVC_TYPE_PRIMARY:
+ uuid16 = BLE_ATT_UUID_PRIMARY_SERVICE;
+ break;
+
+ case BLE_GATT_SVC_TYPE_SECONDARY:
+ uuid16 = BLE_ATT_UUID_SECONDARY_SERVICE;
+ break;
+
+ default:
+ return BLE_HS_EINVAL;
+ }
+
+ rc = ble_att_svr_register_uuid16(uuid16, HA_FLAG_PERM_READ, out_handle,
+ ble_gatts_svc_access, (void *)svc);
if (rc != 0) {
return rc;
}
@@ -246,7 +271,7 @@ ble_gatts_register_svc(const struct ble_gatt_svc_def *svc,
/* Register each characteristic. */
if (svc->characteristics != NULL) {
for (chr = svc->characteristics; chr->uuid128 != NULL; chr++) {
- rc = ble_gatts_register_characteristic(chr);
+ rc = ble_gatts_register_chr(chr);
if (rc != 0) {
return rc;
}
[3/3] incubator-mynewt-larva git commit: Use GATT server
functionality in host projects.
Posted by cc...@apache.org.
Use GATT server functionality in host projects.
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/41fbd980
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/tree/41fbd980
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/diff/41fbd980
Branch: refs/heads/master
Commit: 41fbd980a21af88a83c4462dc9fb3667f8c07780
Parents: 38d7d73
Author: Christopher Collins <cc...@gmail.com>
Authored: Mon Dec 28 11:38:33 2015 -0800
Committer: Christopher Collins <cc...@gmail.com>
Committed: Mon Dec 28 11:39:09 2015 -0800
----------------------------------------------------------------------
project/prphtest/src/main.c | 107 +++++++++------------------------------
1 file changed, 23 insertions(+), 84 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-mynewt-larva/blob/41fbd980/project/prphtest/src/main.c
----------------------------------------------------------------------
diff --git a/project/prphtest/src/main.c b/project/prphtest/src/main.c
index fff13de..8d74552 100755
--- a/project/prphtest/src/main.c
+++ b/project/prphtest/src/main.c
@@ -76,66 +76,36 @@ os_stack_t prphtest_stack[PRPHTEST_STACK_SIZE];
void
bletest_inc_adv_pkt_num(void) { }
-static uint16_t prphtest_service_handle;
-static uint16_t prphtest_char1_handle;
-static uint16_t prphtest_data1_handle;
-static uint16_t prphtest_char2_handle;
-static uint16_t prphtest_data2_handle;
+static int
+prphtest_attr_cb(uint16_t handle_id, uint8_t *uuid128, uint8_t op,
+ union ble_att_svr_access_ctxt *ctxt, void *arg);
+
+static const struct ble_gatt_svc_def prphtest_svcs[] = { {
+ .type = BLE_GATT_SVC_TYPE_PRIMARY,
+ .uuid128 = (uint8_t[]){ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 },
+ .characteristics = (struct ble_gatt_chr_def[]) { {
+ .uuid128 = (uint8_t[]){ 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 },
+ .access_cb = prphtest_attr_cb,
+ .properties = 0,
+ }, {
+ .uuid128 = NULL, /* No more characteristics in this service. */
+ } },
+}, {
+ .type = BLE_GATT_SVC_TYPE_END, /* No more services. */
+}, };
static int
prphtest_attr_cb(uint16_t handle_id, uint8_t *uuid128, uint8_t op,
- union ble_att_svr_access_ctxt *ctxt, void *arg)
+ union ble_att_svr_access_ctxt *ctxt, void *arg)
{
static uint8_t buf[128];
assert(op == BLE_ATT_ACCESS_OP_READ);
- if (handle_id == prphtest_service_handle) {
- console_printf("reading service declaration");
- htole16(buf, 0x1234);
- ctxt->ahc_read.attr_data = buf;
- ctxt->ahc_read.attr_len = 2;
- } else if (handle_id == prphtest_char1_handle) {
- console_printf("reading characteristic1 declaration");
-
- /* Properties. */
- buf[0] = 0;
-
- /* Value handle. */
- htole16(buf + 1, prphtest_data1_handle);
-
- /* UUID. */
- htole16(buf + 3, 0x5656);
-
- ctxt->ahc_read.attr_data = buf;
- ctxt->ahc_read.attr_len = 5;
- } else if (handle_id == prphtest_data1_handle) {
- console_printf("reading characteristic1 value");
- memcpy(buf, "char1", 5);
- ctxt->ahc_read.attr_data = buf;
- ctxt->ahc_read.attr_len = 5;
- } else if (handle_id == prphtest_char2_handle) {
- console_printf("reading characteristic2 declaration");
-
- /* Properties. */
- buf[0] = 0;
-
- /* Value handle. */
- htole16(buf + 1, prphtest_data2_handle);
-
- /* UUID. */
- htole16(buf + 3, 0x6767);
-
- ctxt->ahc_read.attr_data = buf;
- ctxt->ahc_read.attr_len = 5;
- } else if (handle_id == prphtest_data2_handle) {
- console_printf("reading characteristic2 value");
- memcpy(buf, "char2", 5);
- ctxt->ahc_read.attr_data = buf;
- ctxt->ahc_read.attr_len = 5;
- } else {
- assert(0);
- }
+ console_printf("reading characteristic1 value");
+ memcpy(buf, "char1", 5);
+ ctxt->ahc_read.attr_data = buf;
+ ctxt->ahc_read.attr_len = 5;
return 0;
}
@@ -143,40 +113,9 @@ prphtest_attr_cb(uint16_t handle_id, uint8_t *uuid128, uint8_t op,
static void
prphtest_register_attrs(void)
{
- uint8_t uuid128[16];
int rc;
- /* Service. */
- rc = ble_hs_uuid_from_16bit(BLE_ATT_UUID_PRIMARY_SERVICE, uuid128);
- assert(rc == 0);
- rc = ble_att_svr_register(uuid128, 0, &prphtest_service_handle,
- prphtest_attr_cb, NULL);
- assert(rc == 0);
-
- /* Characteristic 1 (UUID=0x5656).*/
- rc = ble_hs_uuid_from_16bit(BLE_ATT_UUID_CHARACTERISTIC, uuid128);
- assert(rc == 0);
- rc = ble_att_svr_register(uuid128, 0, &prphtest_char1_handle,
- prphtest_attr_cb, NULL);
- assert(rc == 0);
-
- rc = ble_hs_uuid_from_16bit(0x5656, uuid128);
- assert(rc == 0);
- rc = ble_att_svr_register(uuid128, 0, &prphtest_data1_handle,
- prphtest_attr_cb, NULL);
- assert(rc == 0);
-
- /* Characteristic 2 (UUID=0x6767).*/
- rc = ble_hs_uuid_from_16bit(BLE_ATT_UUID_CHARACTERISTIC, uuid128);
- assert(rc == 0);
- rc = ble_att_svr_register(uuid128, 0, &prphtest_char2_handle,
- prphtest_attr_cb, NULL);
- assert(rc == 0);
-
- rc = ble_hs_uuid_from_16bit(0x6767, uuid128);
- assert(rc == 0);
- rc = ble_att_svr_register(uuid128, 0, &prphtest_data2_handle,
- prphtest_attr_cb, NULL);
+ rc = ble_gatt_register_services(prphtest_svcs);
assert(rc == 0);
}