From ca3b76958ecde27a0a56cc608ceabd00879326ad Mon Sep 17 00:00:00 2001 From: Sterling Hughes Date: Wed, 28 Sep 2016 16:34:02 -0700 Subject: [PATCH] directory re-org X-Original-Commit: 6a7432f493900f8adad0fe9416b65d477fc5b316 --- nimble/controller/pkg.yml | 2 +- nimble/host/pkg.yml | 2 +- .../bleuart/include/bleuart/bleuart.h | 34 +++ nimble/host/services/bleuart/pkg.yml | 42 ++++ nimble/host/services/bleuart/src/bleuart.c | 201 ++++++++++++++++++ nimble/host/src/ble_hs.c | 5 +- nimble/pkg.yml | 2 +- nimble/transport/ram/pkg.yml | 2 +- nimble/transport/uart/pkg.yml | 2 +- 9 files changed, 284 insertions(+), 8 deletions(-) create mode 100644 nimble/host/services/bleuart/include/bleuart/bleuart.h create mode 100644 nimble/host/services/bleuart/pkg.yml create mode 100644 nimble/host/services/bleuart/src/bleuart.c diff --git a/nimble/controller/pkg.yml b/nimble/controller/pkg.yml index 7b5a4ebaa..ee5be8492 100644 --- a/nimble/controller/pkg.yml +++ b/nimble/controller/pkg.yml @@ -30,7 +30,7 @@ pkg.req_apis: - ble_transport pkg.deps: - - libs/os + - kernel/os - sys/stats - net/nimble diff --git a/nimble/host/pkg.yml b/nimble/host/pkg.yml index 923c13e01..97b713a9c 100644 --- a/nimble/host/pkg.yml +++ b/nimble/host/pkg.yml @@ -28,7 +28,7 @@ pkg.keywords: pkg.deps: - sys/log - sys/stats - - libs/os + - kernel/os - libs/util - net/nimble diff --git a/nimble/host/services/bleuart/include/bleuart/bleuart.h b/nimble/host/services/bleuart/include/bleuart/bleuart.h new file mode 100644 index 000000000..6639747b9 --- /dev/null +++ b/nimble/host/services/bleuart/include/bleuart/bleuart.h @@ -0,0 +1,34 @@ +/** + * 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. + */ + +#ifndef _BLEUART_H_ +#define _BLEUART_H_ + +void +bleuart_init(void); +int +bleuart_svc_register(void); +int +bleuart_gatt_svr_init(void); +void +bleuart_set_conn_handle(uint16_t conn_handle); + +extern const uint8_t gatt_svr_svc_uart[16]; + +#endif /* _BLEUART_H */ diff --git a/nimble/host/services/bleuart/pkg.yml b/nimble/host/services/bleuart/pkg.yml new file mode 100644 index 000000000..96d4c440c --- /dev/null +++ b/nimble/host/services/bleuart/pkg.yml @@ -0,0 +1,42 @@ +# +# 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: libs/bleuart +pkg.description: BLE uart service. +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + - uart + +pkg.deps: + - kernel/os + - net/nimble/host + +pkg.req_apis: + - console + +pkg.init_function: bleuart_init +pkg.init_stage: 5 + +pkg.syscfg_defs: + BLEUART_MAX_INPUT: + description: 'TBD' + value: 120 diff --git a/nimble/host/services/bleuart/src/bleuart.c b/nimble/host/services/bleuart/src/bleuart.c new file mode 100644 index 000000000..b7b259136 --- /dev/null +++ b/nimble/host/services/bleuart/src/bleuart.c @@ -0,0 +1,201 @@ +/** + * 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 +#include +#include + +#include "sysinit/sysinit.h" +#include "host/ble_hs.h" +#include "bleuart/bleuart.h" +#include "os/endian.h" +#include "console/console.h" + +/* ble uart attr read handle */ +uint16_t g_bleuart_attr_read_handle; + +/* ble uart attr write handle */ +uint16_t g_bleuart_attr_write_handle; + +/* Pointer to a console buffer */ +char *console_buf; + +uint16_t g_console_conn_handle; +/** + * The vendor specific "bleuart" service consists of one write no-rsp characteristic + * and one notification only read charateristic + * o "write no-rsp": a single-byte characteristic that can be written only + * over a non-encrypted connection + * o "read": a single-byte characteristic that can always be read only via + * notifications + */ + +/* {6E400001-B5A3-F393-E0A9-E50E24DCCA9E} */ +const uint8_t gatt_svr_svc_uart[16] = { + 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, + 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e +}; + +/* {6E400002-B5A3-F393-E0A9-E50E24DCCA9E} */ +const uint8_t gatt_svr_chr_uart_write[16] = { + 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, + 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e +}; + + +/* {6E400003-B5A3-F393-E0A9-E50E24DCCA9E} */ +const uint8_t gatt_svr_chr_uart_read[16] = { + 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, + 0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e +}; + +static int +gatt_svr_chr_access_uart_write(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: uart */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid128 = (void *)gatt_svr_svc_uart, + .characteristics = (struct ble_gatt_chr_def[]) { { + .uuid128 = gatt_svr_chr_uart_read, + .val_handle = &g_bleuart_attr_read_handle, + .access_cb = gatt_svr_chr_access_uart_write, + .flags = BLE_GATT_CHR_F_NOTIFY, + }, { + /* Characteristic: Write */ + .uuid128 = (void *)gatt_svr_chr_uart_write, + .access_cb = gatt_svr_chr_access_uart_write, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_NO_RSP, + .val_handle = &g_bleuart_attr_write_handle, + }, { + 0, /* No more characteristics in this service */ + } }, + }, + + { + 0, /* No more services */ + }, +}; + +static int +gatt_svr_chr_access_uart_write(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + struct os_mbuf *om = ctxt->om; + switch (ctxt->op) { + case BLE_GATT_ACCESS_OP_WRITE_CHR: + while(om) { + console_write((char *)om->om_data, om->om_len); + om = SLIST_NEXT(om, om_next); + } + console_write("\n", 1); + return 0; + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } +} + +/** + * bleuart GATT server initialization + * + * @param eventq + * @return 0 on success; non-zero on failure + */ +int +bleuart_gatt_svr_init(void) +{ + int rc; + + rc = ble_gatts_count_cfg(gatt_svr_svcs); + if (rc != 0) { + goto err; + } + + rc = ble_gatts_add_svcs(gatt_svr_svcs); + if (rc != 0) { + return rc; + } + +err: + return rc; +} + +/** + * Reads console and sends data over BLE + */ +static void +bleuart_uart_read(void) +{ + int rc; + int off; + int full_line; + struct os_mbuf *om; + + off = 0; + while (1) { + rc = console_read(console_buf + off, + MYNEWT_VAL(BLEUART_MAX_INPUT) - off, &full_line); + if (rc <= 0 && !full_line) { + continue; + } + off += rc; + if (!full_line) { + continue; + } + + om = ble_hs_mbuf_from_flat(console_buf, off); + if (!om) { + return; + } + ble_gattc_notify_custom(g_console_conn_handle, + g_bleuart_attr_read_handle, om); + off = 0; + break; + } +} + +/** + * Sets the global connection handle + * + * @param connection handle + */ +void +bleuart_set_conn_handle(uint16_t conn_handle) { + g_console_conn_handle = conn_handle; +} + +/** + * BLEuart console initialization + * + * @param Maximum input + */ +void +bleuart_init(void) +{ + int rc; + + rc = console_init(bleuart_uart_read); + SYSINIT_PANIC_ASSERT(rc == 0); + + console_buf = malloc(MYNEWT_VAL(BLEUART_MAX_INPUT)); + SYSINIT_PANIC_ASSERT(console_buf != NULL); +} diff --git a/nimble/host/src/ble_hs.c b/nimble/host/src/ble_hs.c index 3b3334112..57c3e70c7 100644 --- a/nimble/host/src/ble_hs.c +++ b/nimble/host/src/ble_hs.c @@ -23,7 +23,6 @@ #include "syscfg/syscfg.h" #include "bsp/bsp.h" #include "stats/stats.h" -#include "util/tpq.h" #include "os/os.h" #include "nimble/ble_hci_trans.h" #include "ble_hs_priv.h" @@ -61,11 +60,11 @@ static int ble_hs_reset_reason; #define BLE_HS_HEARTBEAT_OS_TICKS \ (MYNEWT_VAL(BLE_HS_HEARTBEAT_FREQ) * OS_TICKS_PER_SEC / 1000) -#define BLE_HS_SYNC_RETRY_RATE (OS_TICKS_PER_SEC / 10) +#define BLE_HS_SYNC_RETRY_RATE (OS_TICKS_PER_SEC / 10) static struct os_task *ble_hs_parent_task; -#define BLE_HS_SYNC_RETRY_RATE (OS_TICKS_PER_SEC / 10) +#define BLE_HS_SYNC_RETRY_RATE (OS_TICKS_PER_SEC / 10) /** * Handles unresponsive timeouts and periodic retries in case of resource diff --git a/nimble/pkg.yml b/nimble/pkg.yml index 10d20044f..bf398664d 100644 --- a/nimble/pkg.yml +++ b/nimble/pkg.yml @@ -26,7 +26,7 @@ pkg.keywords: - bluetooth pkg.deps: - - libs/os + - kernel/os pkg.syscfg_defs: # Supported GAP roles. By default, all four roles are enabled. diff --git a/nimble/transport/ram/pkg.yml b/nimble/transport/ram/pkg.yml index 3d551fe6e..6126116bf 100644 --- a/nimble/transport/ram/pkg.yml +++ b/nimble/transport/ram/pkg.yml @@ -27,7 +27,7 @@ pkg.keywords: pkg.deps: - net/nimble - - libs/os + - kernel/os pkg.apis: - ble_transport diff --git a/nimble/transport/uart/pkg.yml b/nimble/transport/uart/pkg.yml index 252feba6b..c48c16b91 100644 --- a/nimble/transport/uart/pkg.yml +++ b/nimble/transport/uart/pkg.yml @@ -27,7 +27,7 @@ pkg.keywords: pkg.deps: - hw/hal - - libs/os + - kernel/os - net/nimble pkg.apis: