From bdc10744fb338ae197692713a0b48a7ccc36f566 Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 26 Apr 2020 10:25:59 +0200 Subject: Add Nimble in libs directory --- .../ans/include/services/ans/ble_svc_ans.h | 85 ++++ .../mynewt-nimble/nimble/host/services/ans/pkg.yml | 34 ++ .../nimble/host/services/ans/src/ble_svc_ans.c | 463 +++++++++++++++++++++ .../nimble/host/services/ans/syscfg.yml | 30 ++ .../bas/include/services/bas/ble_svc_bas.h | 31 ++ .../mynewt-nimble/nimble/host/services/bas/pkg.yml | 34 ++ .../nimble/host/services/bas/src/ble_svc_bas.c | 127 ++++++ .../nimble/host/services/bas/syscfg.yml | 34 ++ .../services/bleuart/include/bleuart/bleuart.h | 42 ++ .../nimble/host/services/bleuart/pkg.yml | 37 ++ .../nimble/host/services/bleuart/src/bleuart.c | 203 +++++++++ .../nimble/host/services/bleuart/syscfg.yml | 28 ++ .../dis/include/services/dis/ble_svc_dis.h | 113 +++++ .../mynewt-nimble/nimble/host/services/dis/pkg.yml | 34 ++ .../nimble/host/services/dis/src/ble_svc_dis.c | 331 +++++++++++++++ .../nimble/host/services/dis/syscfg.yml | 109 +++++ .../gap/include/services/gap/ble_svc_gap.h | 54 +++ .../mynewt-nimble/nimble/host/services/gap/pkg.yml | 34 ++ .../nimble/host/services/gap/src/ble_svc_gap.c | 298 +++++++++++++ .../nimble/host/services/gap/syscfg.yml | 83 ++++ .../gatt/include/services/gatt/ble_svc_gatt.h | 40 ++ .../nimble/host/services/gatt/pkg.yml | 34 ++ .../nimble/host/services/gatt/src/ble_svc_gatt.c | 108 +++++ .../nimble/host/services/gatt/syscfg.yml | 24 ++ .../ias/include/services/ias/ble_svc_ias.h | 38 ++ .../mynewt-nimble/nimble/host/services/ias/pkg.yml | 34 ++ .../nimble/host/services/ias/src/ble_svc_ias.c | 149 +++++++ .../nimble/host/services/ias/syscfg.yml | 23 + .../ipss/include/services/ipss/ble_svc_ipss.h | 38 ++ .../nimble/host/services/ipss/pkg.yml | 35 ++ .../nimble/host/services/ipss/src/ble_svc_ipss.c | 51 +++ .../nimble/host/services/ipss/syscfg.yml | 24 ++ .../lls/include/services/lls/ble_svc_lls.h | 51 +++ .../mynewt-nimble/nimble/host/services/lls/pkg.yml | 34 ++ .../nimble/host/services/lls/src/ble_svc_lls.c | 192 +++++++++ .../nimble/host/services/lls/syscfg.yml | 22 + .../tps/include/services/tps/ble_svc_tps.h | 31 ++ .../mynewt-nimble/nimble/host/services/tps/pkg.yml | 34 ++ .../nimble/host/services/tps/src/ble_svc_tps.c | 107 +++++ .../nimble/host/services/tps/syscfg.yml | 23 + 40 files changed, 3296 insertions(+) create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ans/include/services/ans/ble_svc_ans.h create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ans/pkg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ans/src/ble_svc_ans.c create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ans/syscfg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/bas/include/services/bas/ble_svc_bas.h create mode 100644 src/libs/mynewt-nimble/nimble/host/services/bas/pkg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/bas/src/ble_svc_bas.c create mode 100644 src/libs/mynewt-nimble/nimble/host/services/bas/syscfg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/bleuart/include/bleuart/bleuart.h create mode 100644 src/libs/mynewt-nimble/nimble/host/services/bleuart/pkg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/bleuart/src/bleuart.c create mode 100644 src/libs/mynewt-nimble/nimble/host/services/bleuart/syscfg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/dis/include/services/dis/ble_svc_dis.h create mode 100644 src/libs/mynewt-nimble/nimble/host/services/dis/pkg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/dis/src/ble_svc_dis.c create mode 100644 src/libs/mynewt-nimble/nimble/host/services/dis/syscfg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/gap/include/services/gap/ble_svc_gap.h create mode 100644 src/libs/mynewt-nimble/nimble/host/services/gap/pkg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/gap/src/ble_svc_gap.c create mode 100644 src/libs/mynewt-nimble/nimble/host/services/gap/syscfg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h create mode 100644 src/libs/mynewt-nimble/nimble/host/services/gatt/pkg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/gatt/src/ble_svc_gatt.c create mode 100644 src/libs/mynewt-nimble/nimble/host/services/gatt/syscfg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ias/include/services/ias/ble_svc_ias.h create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ias/pkg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ias/src/ble_svc_ias.c create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ias/syscfg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ipss/include/services/ipss/ble_svc_ipss.h create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ipss/pkg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ipss/src/ble_svc_ipss.c create mode 100644 src/libs/mynewt-nimble/nimble/host/services/ipss/syscfg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/lls/include/services/lls/ble_svc_lls.h create mode 100644 src/libs/mynewt-nimble/nimble/host/services/lls/pkg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/lls/src/ble_svc_lls.c create mode 100644 src/libs/mynewt-nimble/nimble/host/services/lls/syscfg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/tps/include/services/tps/ble_svc_tps.h create mode 100644 src/libs/mynewt-nimble/nimble/host/services/tps/pkg.yml create mode 100644 src/libs/mynewt-nimble/nimble/host/services/tps/src/ble_svc_tps.c create mode 100644 src/libs/mynewt-nimble/nimble/host/services/tps/syscfg.yml (limited to 'src/libs/mynewt-nimble/nimble/host/services') diff --git a/src/libs/mynewt-nimble/nimble/host/services/ans/include/services/ans/ble_svc_ans.h b/src/libs/mynewt-nimble/nimble/host/services/ans/include/services/ans/ble_svc_ans.h new file mode 100644 index 00000000..435e9e11 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ans/include/services/ans/ble_svc_ans.h @@ -0,0 +1,85 @@ +/** + * 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 H_BLE_SVC_ANS_ +#define H_BLE_SVC_ANS_ + +struct ble_hs_cfg; + +/* 16 Bit Alert Notification Service UUID */ +#define BLE_SVC_ANS_UUID16 0x1811 + +/* 16 Bit Alert Notification Service Characteristic UUIDs */ +#define BLE_SVC_ANS_CHR_UUID16_SUP_NEW_ALERT_CAT 0x2a47 +#define BLE_SVC_ANS_CHR_UUID16_NEW_ALERT 0x2a46 +#define BLE_SVC_ANS_CHR_UUID16_SUP_UNR_ALERT_CAT 0x2a48 +#define BLE_SVC_ANS_CHR_UUID16_UNR_ALERT_STAT 0x2a45 +#define BLE_SVC_ANS_CHR_UUID16_ALERT_NOT_CTRL_PT 0x2a44 + +/* Alert Notification Service Category ID Bit Masks + * + * TODO: Add remaining 2 optional categories */ +#define BLE_SVC_ANS_CAT_BM_NONE 0x00 +#define BLE_SVC_ANS_CAT_BM_SIMPLE_ALERT 0x01 +#define BLE_SVC_ANS_CAT_BM_EMAIL 0x02 +#define BLE_SVC_ANS_CAT_BM_NEWS 0x04 +#define BLE_SVC_ANS_CAT_BM_CALL 0x08 +#define BLE_SVC_ANS_CAT_BM_MISSED_CALL 0x10 +#define BLE_SVC_ANS_CAT_BM_SMS 0x20 +#define BLE_SVC_ANS_CAT_BM_VOICE_MAIL 0x40 +#define BLE_SVC_ANS_CAT_BM_SCHEDULE 0x80 + +/* Alert Notification Service Category IDs + * + * TODO: Add remaining 2 optional categories */ +#define BLE_SVC_ANS_CAT_ID_SIMPLE_ALERT 0 +#define BLE_SVC_ANS_CAT_ID_EMAIL 1 +#define BLE_SVC_ANS_CAT_ID_NEWS 2 +#define BLE_SVC_ANS_CAT_ID_CALL 3 +#define BLE_SVC_ANS_CAT_ID_MISSED_CALL 4 +#define BLE_SVC_ANS_CAT_ID_SMS 5 +#define BLE_SVC_ANS_CAT_ID_VOICE_MAIL 6 +#define BLE_SVC_ANS_CAT_ID_SCHEDULE 7 + +/* Number of valid ANS categories + * + * TODO: Add remaining 2 optional categories */ +#define BLE_SVC_ANS_CAT_NUM 8 + +/* Alert Notification Control Point Command IDs */ +#define BLE_SVC_ANS_CMD_EN_NEW_ALERT_CAT 0 +#define BLE_SVC_ANS_CMD_EN_UNR_ALERT_CAT 1 +#define BLE_SVC_ANS_CMD_DIS_NEW_ALERT_CAT 2 +#define BLE_SVC_ANS_CMD_DIS_UNR_ALERT_CAT 3 +#define BLE_SVC_ANS_CMD_NOT_NEW_ALERT_IMMEDIATE 4 +#define BLE_SVC_ANS_CMD_NOT_UNR_ALERT_IMMEDIATE 5 + +/* Error Definitions */ +#define BLE_SVC_ANS_ERR_CMD_NOT_SUPPORTED 0xA0 + +void ble_svc_ans_on_gap_connect(uint16_t conn_handle); + +int ble_svc_ans_new_alert_add(uint8_t cat_id, + const char * info_str); +int ble_svc_ans_unr_alert_add(uint8_t cat_id); + +void ble_svc_ans_init(void); + +#endif + diff --git a/src/libs/mynewt-nimble/nimble/host/services/ans/pkg.yml b/src/libs/mynewt-nimble/nimble/host/services/ans/pkg.yml new file mode 100644 index 00000000..691e566e --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ans/pkg.yml @@ -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. +# + +pkg.name: nimble/host/services/ans +pkg.description: Alert Notification Service Server. +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + - ans + - nimble + +pkg.deps: + - nimble/host + +pkg.init: + ble_svc_ans_init: 'MYNEWT_VAL(BLE_SVC_ANS_SYSINIT_STAGE)' diff --git a/src/libs/mynewt-nimble/nimble/host/services/ans/src/ble_svc_ans.c b/src/libs/mynewt-nimble/nimble/host/services/ans/src/ble_svc_ans.c new file mode 100644 index 00000000..5b64f18c --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ans/src/ble_svc_ans.c @@ -0,0 +1,463 @@ +/** + * 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 "sysinit/sysinit.h" +#include "syscfg/syscfg.h" +#include "host/ble_hs.h" +#include "host/ble_gap.h" +#include "services/ans/ble_svc_ans.h" + +/* Max length of new alert info string */ +#define BLE_SVC_ANS_INFO_STR_MAX_LEN 18 +/* Max length of a new alert notification, max string length + 2 bytes + * for category ID and count. */ +#define BLE_SVC_ANS_NEW_ALERT_MAX_LEN (BLE_SVC_ANS_INFO_STR_MAX_LEN + 2) + +/* Supported categories bitmasks */ +static uint8_t ble_svc_ans_new_alert_cat; +static uint8_t ble_svc_ans_unr_alert_cat; + +/* Characteristic values */ +static uint8_t ble_svc_ans_new_alert_val[BLE_SVC_ANS_NEW_ALERT_MAX_LEN]; +static uint16_t ble_svc_ans_new_alert_val_len; +static uint8_t ble_svc_ans_unr_alert_stat[2]; +static uint8_t ble_svc_ans_alert_not_ctrl_pt[2]; + +/* Alert counts, one value for each category */ +static uint8_t ble_svc_ans_new_alert_cnt[BLE_SVC_ANS_CAT_NUM]; +static uint8_t ble_svc_ans_unr_alert_cnt[BLE_SVC_ANS_CAT_NUM]; + +/* Characteristic value handles */ +static uint16_t ble_svc_ans_new_alert_val_handle; +static uint16_t ble_svc_ans_unr_alert_val_handle; + +/* Connection handle + * + * TODO: In order to support multiple connections we would need to save + * the handles for every connection, not just the most recent. Then + * we would need to notify each connection when needed. + * */ +static uint16_t ble_svc_ans_conn_handle; + +/* Access function */ +static int +ble_svc_ans_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg); + +/* Notify new alert */ +static int +ble_svc_ans_new_alert_notify(uint8_t cat_id, const char * info_str); + +/* Notify unread alert */ +static int +ble_svc_ans_unr_alert_notify(uint8_t cat_id); + +/* Save written value to local characteristic value */ +static int +ble_svc_ans_chr_write(struct os_mbuf *om, uint16_t min_len, uint16_t max_len, + void *dst, uint16_t *len); + +static const struct ble_gatt_svc_def ble_svc_ans_defs[] = { + { + /*** Alert Notification Service. */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_UUID16), + .characteristics = (struct ble_gatt_chr_def[]) { { + /** Supported New Alert Catagory + * + * This characteristic exposes what categories of new + * alert are supported in the server. + */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_SUP_NEW_ALERT_CAT), + .access_cb = ble_svc_ans_access, + .flags = BLE_GATT_CHR_F_READ, + }, { + /** New Alert + * + * This characteristic exposes information about + * the count of new alerts (for a given category). + */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_NEW_ALERT), + .access_cb = ble_svc_ans_access, + .val_handle = &ble_svc_ans_new_alert_val_handle, + .flags = BLE_GATT_CHR_F_NOTIFY, + }, { + /** Supported Unread Alert Catagory + * + * This characteristic exposes what categories of + * unread alert are supported in the server. + */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_SUP_UNR_ALERT_CAT), + .access_cb = ble_svc_ans_access, + .flags = BLE_GATT_CHR_F_READ, + }, { + /** Unread Alert Status + * + * This characteristic exposes the count of unread + * alert events existing in the server. + */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_UNR_ALERT_STAT), + .access_cb = ble_svc_ans_access, + .val_handle = &ble_svc_ans_unr_alert_val_handle, + .flags = BLE_GATT_CHR_F_NOTIFY, + }, { + /** Alert Notification Control Point + * + * This characteristic allows the peer device to + * enable/disable the alert notification of new alert + * and unread event more selectively than can be done + * by setting or clearing the notification bit in the + * Client Characteristic Configuration for each alert + * characteristic. + */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_ANS_CHR_UUID16_ALERT_NOT_CTRL_PT), + .access_cb = ble_svc_ans_access, + .flags = BLE_GATT_CHR_F_WRITE, + }, { + 0, /* No more characteristics in this service. */ + } }, + }, + + { + 0, /* No more services. */ + }, +}; + +/** + * ANS access function + */ +static int +ble_svc_ans_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg) +{ + uint16_t uuid16; + int rc; + + /* ANS Control point command and catagory variables */ + uint8_t cmd_id; + uint8_t cat_id; + uint8_t cat_bit_mask; + int i; + + uuid16 = ble_uuid_u16(ctxt->chr->uuid); + assert(uuid16 != 0); + + switch (uuid16) { + case BLE_SVC_ANS_CHR_UUID16_SUP_NEW_ALERT_CAT: + assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR); + rc = os_mbuf_append(ctxt->om, &ble_svc_ans_new_alert_cat, + sizeof ble_svc_ans_new_alert_cat); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + + case BLE_SVC_ANS_CHR_UUID16_NEW_ALERT: + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + rc = ble_svc_ans_chr_write(ctxt->om, 0, + sizeof ble_svc_ans_new_alert_val, + ble_svc_ans_new_alert_val, + &ble_svc_ans_new_alert_val_len); + return rc; + + } else if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + rc = os_mbuf_append(ctxt->om, &ble_svc_ans_new_alert_val, + sizeof ble_svc_ans_new_alert_val); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + + assert(0); + return BLE_ATT_ERR_UNLIKELY; + case BLE_SVC_ANS_CHR_UUID16_SUP_UNR_ALERT_CAT: + assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR); + rc = os_mbuf_append(ctxt->om, &ble_svc_ans_unr_alert_cat, + sizeof ble_svc_ans_unr_alert_cat); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + + case BLE_SVC_ANS_CHR_UUID16_UNR_ALERT_STAT: + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + rc = ble_svc_ans_chr_write(ctxt->om, + sizeof ble_svc_ans_unr_alert_stat, + sizeof ble_svc_ans_unr_alert_stat, + &ble_svc_ans_unr_alert_stat, + NULL); + return rc; + } else { + rc = os_mbuf_append(ctxt->om, &ble_svc_ans_unr_alert_stat, + sizeof ble_svc_ans_unr_alert_stat); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + + case BLE_SVC_ANS_CHR_UUID16_ALERT_NOT_CTRL_PT: + if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + rc = ble_svc_ans_chr_write(ctxt->om, + sizeof ble_svc_ans_alert_not_ctrl_pt, + sizeof ble_svc_ans_alert_not_ctrl_pt, + &ble_svc_ans_alert_not_ctrl_pt, + NULL); + if (rc != 0) { + return rc; + } + + /* Get command ID and category ID */ + cmd_id = ble_svc_ans_alert_not_ctrl_pt[0]; + cat_id = ble_svc_ans_alert_not_ctrl_pt[1]; + + + /* Set cat_bit_mask to the appropriate bitmask based on cat_id */ + if (cat_id < BLE_SVC_ANS_CAT_NUM) { + cat_bit_mask = (1 << cat_id); + } else if (cat_id == 0xff) { + cat_bit_mask = cat_id; + } else { + /* invalid category ID */ + return BLE_ATT_ERR_UNLIKELY; + } + + switch (cmd_id) { + case BLE_SVC_ANS_CMD_EN_NEW_ALERT_CAT: + ble_svc_ans_new_alert_cat |= cat_bit_mask; + break; + case BLE_SVC_ANS_CMD_EN_UNR_ALERT_CAT: + ble_svc_ans_unr_alert_cat |= cat_bit_mask; + break; + case BLE_SVC_ANS_CMD_DIS_NEW_ALERT_CAT: + ble_svc_ans_new_alert_cat &= ~cat_bit_mask; + break; + case BLE_SVC_ANS_CMD_DIS_UNR_ALERT_CAT: + ble_svc_ans_unr_alert_cat &= ~cat_bit_mask; + break; + case BLE_SVC_ANS_CMD_NOT_NEW_ALERT_IMMEDIATE: + if (cat_id == 0xff) { + /* If cat_id is 0xff, notify on all enabled categories */ + for (i = BLE_SVC_ANS_CAT_NUM - 1; i > 0; --i) { + if ((ble_svc_ans_new_alert_cat >> i) & 0x01) { + ble_svc_ans_new_alert_notify(i, NULL); + } + } + } else { + ble_svc_ans_new_alert_notify(cat_id, NULL); + } + break; + case BLE_SVC_ANS_CMD_NOT_UNR_ALERT_IMMEDIATE: + if (cat_id == 0xff) { + /* If cat_id is 0xff, notify on all enabled categories */ + for (i = BLE_SVC_ANS_CAT_NUM - 1; i > 0; --i) { + if ((ble_svc_ans_unr_alert_cat >> i) & 0x01) { + ble_svc_ans_unr_alert_notify(i); + } + } + } else { + ble_svc_ans_unr_alert_notify(cat_id); + } + break; + default: + return BLE_SVC_ANS_ERR_CMD_NOT_SUPPORTED; + } + return 0; + } else { + rc = BLE_ATT_ERR_UNLIKELY; + } + return rc; + + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } +} + +/** + * This function must be called with the connection handlewhen a gap + * connect event is received in order to send notifications to the + * client. + * + * @params conn_handle The connection handle for the current + * connection. + */ +void +ble_svc_ans_on_gap_connect(uint16_t conn_handle) +{ + ble_svc_ans_conn_handle = conn_handle; +} + +/** + * Adds a new alert to the given category then notifies the client + * if the given category is valid and enabled. + * + * @param cat_flag The id for the category which should + * should be incremented and notified + * @param info_str The info string to be sent to the client + * with the notification. + * + * @return 0 on success, non-zero error code otherwise. + */ +int +ble_svc_ans_new_alert_add(uint8_t cat_id, const char * info_str) +{ + uint8_t cat_bit_mask; + + if (cat_id < BLE_SVC_ANS_CAT_NUM) { + cat_bit_mask = (1 << cat_id); + } else { + return BLE_HS_EINVAL; + } + + if ((cat_bit_mask & ble_svc_ans_new_alert_cat) == 0) { + return BLE_HS_EINVAL; + } + + ble_svc_ans_new_alert_cnt[cat_id] += 1; + return ble_svc_ans_new_alert_notify(cat_id, info_str); +} + +/** + * Adds an unread alert to the given category then notifies the client + * if the given category is valid and enabled. + * + * @param cat_flag The flag for the category which should + * should be incremented and notified + * + * @return 0 on success, non-zero error code otherwise. + */ +int +ble_svc_ans_unr_alert_add(uint8_t cat_id) +{ + uint8_t cat_bit_mask; + + if (cat_id < BLE_SVC_ANS_CAT_NUM) { + cat_bit_mask = 1 << cat_id; + } else { + return BLE_HS_EINVAL; + } + + if ((cat_bit_mask & ble_svc_ans_unr_alert_cat) == 0) { + return BLE_HS_EINVAL; + } + + ble_svc_ans_unr_alert_cnt[cat_id] += 1; + return ble_svc_ans_unr_alert_notify(cat_id); +} + +/** + * Send a new alert notification to the given category with the + * given info string. + * + * @param cat_id The ID of the category to send the + * notification to. + * @param info_str The info string to send with the + * notification + * + * @return 0 on success, non-zero error code otherwise. + */ +static int +ble_svc_ans_new_alert_notify(uint8_t cat_id, const char * info_str) +{ + int info_str_len; + + /* Clear notification to remove old infomation that may persist */ + memset(&ble_svc_ans_new_alert_val, '\0', + BLE_SVC_ANS_NEW_ALERT_MAX_LEN); + + /* Set ID and count values */ + ble_svc_ans_new_alert_val[0] = cat_id; + ble_svc_ans_new_alert_val[1] = ble_svc_ans_new_alert_cnt[cat_id]; + + if (info_str) { + info_str_len = strlen(info_str); + if (info_str_len > BLE_SVC_ANS_INFO_STR_MAX_LEN) { + /* If info_str is longer than the max string length only + * write up to the maximum length */ + memcpy(&ble_svc_ans_new_alert_val[2], info_str, + BLE_SVC_ANS_INFO_STR_MAX_LEN); + } else { + memcpy(&ble_svc_ans_new_alert_val[2], info_str, info_str_len); + } + } + return ble_gattc_notify(ble_svc_ans_conn_handle, + ble_svc_ans_new_alert_val_handle); +} + +/** + * Send an unread alert notification to the given category. + * + * @param cat_id The ID of the category to send the + * notificaiton to. + * + * @return 0 on success, non-zer0 error code otherwise. + */ +static int +ble_svc_ans_unr_alert_notify(uint8_t cat_id) +{ + ble_svc_ans_unr_alert_stat[0] = cat_id; + ble_svc_ans_unr_alert_stat[1] = ble_svc_ans_unr_alert_cnt[cat_id]; + return ble_gattc_notify(ble_svc_ans_conn_handle, + ble_svc_ans_unr_alert_val_handle); +} + +/** + * Writes the received value from a characteristic write to + * the given destination. + */ +static int +ble_svc_ans_chr_write(struct os_mbuf *om, uint16_t min_len, + uint16_t max_len, void *dst, + uint16_t *len) +{ + uint16_t om_len; + int rc; + + om_len = OS_MBUF_PKTLEN(om); + if (om_len < min_len || om_len > max_len) { + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; + } + + rc = ble_hs_mbuf_to_flat(om, dst, max_len, len); + if (rc != 0) { + return BLE_ATT_ERR_UNLIKELY; + } + + return 0; +} + +/** + * Initialize the ANS with initial values for enabled categories + * for new and unread alert characteristics. Bitwise or the + * catagory bitmasks to enable multiple catagories. + * + * XXX: We should technically be able to change the new alert and + * unread alert catagories when we have no active connections. + */ +void +ble_svc_ans_init(void) +{ + int rc; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + rc = ble_gatts_count_cfg(ble_svc_ans_defs); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = ble_gatts_add_svcs(ble_svc_ans_defs); + SYSINIT_PANIC_ASSERT(rc == 0); + + ble_svc_ans_new_alert_cat = MYNEWT_VAL(BLE_SVC_ANS_NEW_ALERT_CAT); + ble_svc_ans_unr_alert_cat = MYNEWT_VAL(BLE_SVC_ANS_UNR_ALERT_CAT); +} diff --git a/src/libs/mynewt-nimble/nimble/host/services/ans/syscfg.yml b/src/libs/mynewt-nimble/nimble/host/services/ans/syscfg.yml new file mode 100644 index 00000000..74de8d96 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ans/syscfg.yml @@ -0,0 +1,30 @@ +# 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. + +syscfg.defs: + BLE_SVC_ANS_NEW_ALERT_CAT: + description: "Initial supported new alert category bitmask." + value: 0 + + BLE_SVC_ANS_UNR_ALERT_CAT: + description: "Initial supported unread alert category bitmask." + value: 0 + + BLE_SVC_ANS_SYSINIT_STAGE: + description: > + Sysinit stage for the alert notification service. + value: 303 diff --git a/src/libs/mynewt-nimble/nimble/host/services/bas/include/services/bas/ble_svc_bas.h b/src/libs/mynewt-nimble/nimble/host/services/bas/include/services/bas/ble_svc_bas.h new file mode 100644 index 00000000..2706b5d3 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/bas/include/services/bas/ble_svc_bas.h @@ -0,0 +1,31 @@ +/** + * 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 H_BLE_SVC_BAS_ +#define H_BLE_SVC_BAS_ + +/* 16 Bit Battery Service UUID */ +#define BLE_SVC_BAS_UUID16 0x180F + +/* 16 Bit Battery Service Characteristic UUIDs */ +#define BLE_SVC_BAS_CHR_UUID16_BATTERY_LEVEL 0x2A19 + +int ble_svc_bas_battery_level_set(uint8_t level); + +#endif diff --git a/src/libs/mynewt-nimble/nimble/host/services/bas/pkg.yml b/src/libs/mynewt-nimble/nimble/host/services/bas/pkg.yml new file mode 100644 index 00000000..afdc6942 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/bas/pkg.yml @@ -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. +# + +pkg.name: nimble/host/services/bas +pkg.description: Battery Service +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + - bas + - nimble + +pkg.deps: + - nimble/host + +pkg.init: + ble_svc_bas_init: 'MYNEWT_VAL(BLE_SVC_BAS_SYSINIT_STAGE)' diff --git a/src/libs/mynewt-nimble/nimble/host/services/bas/src/ble_svc_bas.c b/src/libs/mynewt-nimble/nimble/host/services/bas/src/ble_svc_bas.c new file mode 100644 index 00000000..631519cf --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/bas/src/ble_svc_bas.c @@ -0,0 +1,127 @@ +/** + * 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 "sysinit/sysinit.h" +#include "syscfg/syscfg.h" +#include "host/ble_hs.h" +#include "host/ble_gap.h" +#include "services/bas/ble_svc_bas.h" + +/* Characteristic value handles */ +#if MYNEWT_VAL(BLE_SVC_BAS_BATTERY_LEVEL_NOTIFY_ENABLE) > 0 +static uint16_t ble_svc_bas_battery_handle; +#endif + +/* Battery level */ +uint8_t ble_svc_bas_battery_level; + +/* Access function */ +static int +ble_svc_bas_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg); + +static const struct ble_gatt_svc_def ble_svc_bas_defs[] = { + { + /*** Battery Service. */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BLE_SVC_BAS_UUID16), + .characteristics = (struct ble_gatt_chr_def[]) { { + /*** Battery level characteristic */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_BAS_CHR_UUID16_BATTERY_LEVEL), + .access_cb = ble_svc_bas_access, +#if MYNEWT_VAL(BLE_SVC_BAS_BATTERY_LEVEL_NOTIFY_ENABLE) > 0 + .val_handle = &ble_svc_bas_battery_handle, +#endif + .flags = BLE_GATT_CHR_F_READ | +#if MYNEWT_VAL(BLE_SVC_BAS_BATTERY_LEVEL_NOTIFY_ENABLE) > 0 + BLE_GATT_CHR_F_NOTIFY | +#endif + MYNEWT_VAL(BLE_SVC_BAS_BATTERY_LEVEL_READ_PERM), + }, { + 0, /* No more characteristics in this service. */ + } }, + }, + + { + 0, /* No more services. */ + }, +}; + +/** + * BAS access function + */ +static int +ble_svc_bas_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, + void *arg) +{ + uint16_t uuid16 = ble_uuid_u16(ctxt->chr->uuid); + int rc; + + switch (uuid16) { + case BLE_SVC_BAS_CHR_UUID16_BATTERY_LEVEL: + assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR); + rc = os_mbuf_append(ctxt->om, &ble_svc_bas_battery_level, + sizeof ble_svc_bas_battery_level); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } +} + + +/** + * Set the battery level, must be between 0 and 100. + * If greater than 100, it will be silently set to 100. + */ +int +ble_svc_bas_battery_level_set(uint8_t level) { + if (level > 100) + level = 100; + if (ble_svc_bas_battery_level != level) { + ble_svc_bas_battery_level = level; +#if MYNEWT_VAL(BLE_SVC_BAS_BATTERY_LEVEL_NOTIFY_ENABLE) > 0 + ble_gatts_chr_updated(ble_svc_bas_battery_handle); +#endif + } + return 0; +} + +/** + * Initialize the Battery Service. + */ +void +ble_svc_bas_init(void) +{ + int rc; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + rc = ble_gatts_count_cfg(ble_svc_bas_defs); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = ble_gatts_add_svcs(ble_svc_bas_defs); + SYSINIT_PANIC_ASSERT(rc == 0); +} diff --git a/src/libs/mynewt-nimble/nimble/host/services/bas/syscfg.yml b/src/libs/mynewt-nimble/nimble/host/services/bas/syscfg.yml new file mode 100644 index 00000000..279930f1 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/bas/syscfg.yml @@ -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. + +syscfg.defs: + BLE_SVC_BAS_BATTERY_LEVEL_READ_PERM: + description: > + Defines permissions for reading "Battery Level" characteristics. Can + be zero to allow read without extra permissions or combination of: + BLE_GATT_CHR_F_READ_ENC + BLE_GATT_CHR_F_READ_AUTHEN + BLE_GATT_CHR_F_READ_AUTHOR + value: 0 + BLE_SVC_BAS_BATTERY_LEVEL_NOTIFY_ENABLE: + description: > + Set to 1 to support notification or 0 to disable it. + value: 1 + BLE_SVC_BAS_SYSINIT_STAGE: + description: > + Sysinit stage for the battery level service. + value: 303 diff --git a/src/libs/mynewt-nimble/nimble/host/services/bleuart/include/bleuart/bleuart.h b/src/libs/mynewt-nimble/nimble/host/services/bleuart/include/bleuart/bleuart.h new file mode 100644 index 00000000..ab64b9c0 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/bleuart/include/bleuart/bleuart.h @@ -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. + */ + +#ifndef _BLEUART_H_ +#define _BLEUART_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +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 ble_uuid128_t gatt_svr_svc_uart_uuid; + +#ifdef __cplusplus +} +#endif + +#endif /* _BLEUART_H */ diff --git a/src/libs/mynewt-nimble/nimble/host/services/bleuart/pkg.yml b/src/libs/mynewt-nimble/nimble/host/services/bleuart/pkg.yml new file mode 100644 index 00000000..36fe2bc7 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/bleuart/pkg.yml @@ -0,0 +1,37 @@ +# +# 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: nimble/host/services/bleuart +pkg.description: BLE uart service. +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + - uart + +pkg.deps: + - "@apache-mynewt-core/kernel/os" + - nimble/host + +pkg.req_apis: + - console + +pkg.init: + bleuart_init: 'MYNEWT_VAL(BLEUART_SYSINIT_STAGE)' diff --git a/src/libs/mynewt-nimble/nimble/host/services/bleuart/src/bleuart.c b/src/libs/mynewt-nimble/nimble/host/services/bleuart/src/bleuart.c new file mode 100644 index 00000000..15856352 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/bleuart/src/bleuart.c @@ -0,0 +1,203 @@ +/* + * 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 + +#include "sysinit/sysinit.h" +#include "host/ble_hs.h" +#include "host/ble_uuid.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 ble_uuid128_t gatt_svr_svc_uart_uuid = + BLE_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, + 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e); + +/* {6E400002-B5A3-F393-E0A9-E50E24DCCA9E} */ +const ble_uuid128_t gatt_svr_chr_uart_write_uuid = + BLE_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, + 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e); + + +/* {6E400003-B5A3-F393-E0A9-E50E24DCCA9E} */ +const ble_uuid128_t gatt_svr_chr_uart_read_uuid = + BLE_UUID128_INIT(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, + .uuid = &gatt_svr_svc_uart_uuid.u, + .characteristics = (struct ble_gatt_chr_def[]) { { + .uuid = &gatt_svr_chr_uart_read_uuid.u, + .val_handle = &g_bleuart_attr_read_handle, + .access_cb = gatt_svr_chr_access_uart_write, + .flags = BLE_GATT_CHR_F_NOTIFY, + }, { + /* Characteristic: Write */ + .uuid = &gatt_svr_chr_uart_write_uuid.u, + .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; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + 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/src/libs/mynewt-nimble/nimble/host/services/bleuart/syscfg.yml b/src/libs/mynewt-nimble/nimble/host/services/bleuart/syscfg.yml new file mode 100644 index 00000000..b0ed73e5 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/bleuart/syscfg.yml @@ -0,0 +1,28 @@ +# 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. +# + +syscfg.defs: + BLEUART_MAX_INPUT: + description: > + The size of the largest line that can be received over the UART + service. + value: 120 + BLEUART_SYSINIT_STAGE: + description: > + Sysinit stage for the BLE UART service. + value: 500 diff --git a/src/libs/mynewt-nimble/nimble/host/services/dis/include/services/dis/ble_svc_dis.h b/src/libs/mynewt-nimble/nimble/host/services/dis/include/services/dis/ble_svc_dis.h new file mode 100644 index 00000000..d095e959 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/dis/include/services/dis/ble_svc_dis.h @@ -0,0 +1,113 @@ +/** + * 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 H_BLE_SVC_DIS_ +#define H_BLE_SVC_DIS_ + +/** + * Example: + * + * char firmware_revision[20] = '?.?.?'; + * struct image_version iv; + * if (!imgr_my_version(&iv)) { + * snprintf(firmware_revision, sizeof(firmware_revision), + * "%u.%u.%u", iv.iv_major, iv.iv_minor, iv.iv_revision); + * } + * ble_svc_dis_manufacturer_name_set("MyNewt"); + * ble_svc_dis_firmware_revision_set(firmware_revision); + * + */ + +#define BLE_SVC_DIS_UUID16 0x180A +#define BLE_SVC_DIS_CHR_UUID16_SYSTEM_ID 0x2A23 +#define BLE_SVC_DIS_CHR_UUID16_MODEL_NUMBER 0x2A24 +#define BLE_SVC_DIS_CHR_UUID16_SERIAL_NUMBER 0x2A25 +#define BLE_SVC_DIS_CHR_UUID16_FIRMWARE_REVISION 0x2A26 +#define BLE_SVC_DIS_CHR_UUID16_HARDWARE_REVISION 0x2A27 +#define BLE_SVC_DIS_CHR_UUID16_SOFTWARE_REVISION 0x2A28 +#define BLE_SVC_DIS_CHR_UUID16_MANUFACTURER_NAME 0x2A29 + +/** + * Structure holding data for the main characteristics + */ +struct ble_svc_dis_data { + /** + * Model number. + * Represent the model number that is assigned by the device vendor. + */ + const char *model_number; + /** + * Serial number. + * Represent the serial number for a particular instance of the device. + */ + const char *serial_number; + /** + * Firmware revision. + * Represent the firmware revision for the firmware within the device. + */ + const char *firmware_revision; + /** + * Hardware revision. + * Represent the hardware revision for the hardware within the device. + */ + const char *hardware_revision; + /** + * Software revision. + * Represent the software revision for the software within the device. + */ + const char *software_revision; + /** + * Manufacturer name. + * Represent the name of the manufacturer of the device. + */ + const char *manufacturer_name; + /** + * System ID. + * Represent the System Id of the device. + */ + const char *system_id; +}; + +/** + * Variable holding data for the main characteristics. + */ +extern struct ble_svc_dis_data ble_svc_dis_data; + +/** + * Service initialisation. + * Automatically called during package initialisation. + */ +void ble_svc_dis_init(void); + +const char *ble_svc_dis_model_number(void); +int ble_svc_dis_model_number_set(const char *value); +const char *ble_svc_dis_serial_number(void); +int ble_svc_dis_serial_number_set(const char *value); +const char *ble_svc_dis_firmware_revision(void); +int ble_svc_dis_firmware_revision_set(const char *value); +const char *ble_svc_dis_hardware_revision(void); +int ble_svc_dis_hardware_revision_set(const char *value); +const char *ble_svc_dis_software_revision(void); +int ble_svc_dis_software_revision_set(const char *value); +const char *ble_svc_dis_manufacturer_name(void); +int ble_svc_dis_manufacturer_name_set(const char *value); +const char *ble_svc_dis_system_id(void); +int ble_svc_dis_system_id_set(const char *value); + +#endif diff --git a/src/libs/mynewt-nimble/nimble/host/services/dis/pkg.yml b/src/libs/mynewt-nimble/nimble/host/services/dis/pkg.yml new file mode 100644 index 00000000..b914ca90 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/dis/pkg.yml @@ -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. +# + +pkg.name: nimble/host/services/dis +pkg.description: Device Information Service Implementation. +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + - dis + - nimble + +pkg.deps: + - nimble/host + +pkg.init: + ble_svc_dis_init: 'MYNEWT_VAL(BLE_SVC_DIS_SYSINIT_STAGE)' diff --git a/src/libs/mynewt-nimble/nimble/host/services/dis/src/ble_svc_dis.c b/src/libs/mynewt-nimble/nimble/host/services/dis/src/ble_svc_dis.c new file mode 100644 index 00000000..0079a04c --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/dis/src/ble_svc_dis.c @@ -0,0 +1,331 @@ +/** + * 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 "sysinit/sysinit.h" +#include "host/ble_hs.h" +#include "services/dis/ble_svc_dis.h" + +/* Device information */ +struct ble_svc_dis_data ble_svc_dis_data = { + .model_number = MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_DEFAULT), + .serial_number = MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_DEFAULT), + .firmware_revision = MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_DEFAULT), + .hardware_revision = MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_DEFAULT), + .software_revision = MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_DEFAULT), + .manufacturer_name = MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_DEFAULT), + .system_id = MYNEWT_VAL(BLE_SVC_DIS_SYSTEM_ID_DEFAULT), +}; + +/* Access function */ +#if (MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_SYSTEM_ID_READ_PERM) >= 0) +static int +ble_svc_dis_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg); +#endif + +static const struct ble_gatt_svc_def ble_svc_dis_defs[] = { + { /*** Service: Device Information Service (DIS). */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_UUID16), + .characteristics = (struct ble_gatt_chr_def[]) { { +#if (MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_READ_PERM) >= 0) + /*** Characteristic: Model Number String */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_MODEL_NUMBER), + .access_cb = ble_svc_dis_access, + .flags = BLE_GATT_CHR_F_READ | + MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_READ_PERM), + }, { +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM) >= 0) + /*** Characteristic: Serial Number String */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_SERIAL_NUMBER), + .access_cb = ble_svc_dis_access, + .flags = BLE_GATT_CHR_F_READ | + MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM), + }, { +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM) >= 0) + /*** Characteristic: Hardware Revision String */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_HARDWARE_REVISION), + .access_cb = ble_svc_dis_access, + .flags = BLE_GATT_CHR_F_READ | + MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM), + }, { +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM) >= 0) + /*** Characteristic: Firmware Revision String */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_FIRMWARE_REVISION), + .access_cb = ble_svc_dis_access, + .flags = BLE_GATT_CHR_F_READ | + MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM), + }, { +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM) >= 0) + /*** Characteristic: Software Revision String */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_SOFTWARE_REVISION), + .access_cb = ble_svc_dis_access, + .flags = BLE_GATT_CHR_F_READ | + MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM), + }, { +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM) >= 0) + /*** Characteristic: Manufacturer Name */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_MANUFACTURER_NAME), + .access_cb = ble_svc_dis_access, + .flags = BLE_GATT_CHR_F_READ | + MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM), + }, { +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_SYSTEM_ID_READ_PERM) >= 0) + /*** Characteristic: System Id */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_DIS_CHR_UUID16_SYSTEM_ID), + .access_cb = ble_svc_dis_access, + .flags = BLE_GATT_CHR_F_READ | + MYNEWT_VAL(BLE_SVC_DIS_SYSTEM_ID_READ_PERM), + }, { +#endif + + 0, /* No more characteristics in this service */ + }, } + }, + + { + 0, /* No more services. */ + }, +}; + +/** + * Simple read access callback for the device information service + * characteristic. + */ +#if (MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM) >= 0) || \ + (MYNEWT_VAL(BLE_SVC_DIS_SYSTEM_ID_READ_PERM) >= 0) +static int +ble_svc_dis_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + uint16_t uuid = ble_uuid_u16(ctxt->chr->uuid); + const char *info = NULL; + + switch(uuid) { +#if (MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_READ_PERM) >= 0) + case BLE_SVC_DIS_CHR_UUID16_MODEL_NUMBER: + info = ble_svc_dis_data.model_number; +#ifdef MYNEWT_VAL_BLE_SVC_DIS_MODEL_NUMBER_NAME_DEFAULT + if (info == NULL) { + info = MYNEWT_VAL(BLE_SVC_DIS_MODEL_NUMBER_DEFAULT); + } +#endif + break; +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM) >= 0) + case BLE_SVC_DIS_CHR_UUID16_SERIAL_NUMBER: + info = ble_svc_dis_data.serial_number; +#ifdef MYNEWT_VAL_BLE_SVC_DIS_SERIAL_NUMBER_DEFAULT + if (info == NULL) { + info = MYNEWT_VAL(BLE_SVC_DIS_SERIAL_NUMBER_DEFAULT); + } +#endif + break; +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM) >= 0) + case BLE_SVC_DIS_CHR_UUID16_FIRMWARE_REVISION: + info = ble_svc_dis_data.firmware_revision; +#ifdef MYNEWT_VAL_BLE_SVC_DIS_FIRMWARE_REVISION_DEFAULT + if (info == NULL) { + info = MYNEWT_VAL(BLE_SVC_DIS_FIRMWARE_REVISION_DEFAULT); + } +#endif + break; +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM) >= 0) + case BLE_SVC_DIS_CHR_UUID16_HARDWARE_REVISION: + info = ble_svc_dis_data.hardware_revision; +#ifdef MYNEWT_VAL_BLE_SVC_DIS_HARDWARE_REVISION_DEFAULT + if (info == NULL) { + info = MYNEWT_VAL(BLE_SVC_DIS_HARDWARE_REVISION_DEFAULT); + } +#endif + break; +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM) >= 0) + case BLE_SVC_DIS_CHR_UUID16_SOFTWARE_REVISION: + info = ble_svc_dis_data.software_revision; +#ifdef MYNEWT_VAL_BLE_SVC_DIS_SOFTWARE_REVISION_DEFAULT + if (info == NULL) { + info = MYNEWT_VAL(BLE_SVC_DIS_SOFTWARE_REVISION_DEFAULT); + } +#endif + break; +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM) >= 0) + case BLE_SVC_DIS_CHR_UUID16_MANUFACTURER_NAME: + info = ble_svc_dis_data.manufacturer_name; +#ifdef MYNEWT_VAL_BLE_SVC_DIS_MANUFACTURER_NAME_DEFAULT + if (info == NULL) { + info = MYNEWT_VAL(BLE_SVC_DIS_MANUFACTURER_NAME_DEFAULT); + } +#endif + break; +#endif +#if (MYNEWT_VAL(BLE_SVC_DIS_SYSTEM_ID_READ_PERM) >= 0) + case BLE_SVC_DIS_CHR_UUID16_SYSTEM_ID: + info = ble_svc_dis_data.system_id; +#ifdef MYNEWT_VAL_BLE_SVC_DIS_SYSTEM_ID_DEFAULT + if (info == NULL) { + info = MYNEWT_VAL(BLE_SVC_DIS_SYSTEM_ID_DEFAULT); + } +#endif + break; +#endif + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } + + if (info != NULL) { + int rc = os_mbuf_append(ctxt->om, info, strlen(info)); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + } + + return 0; +} +#endif + +const char * +ble_svc_dis_model_number(void) +{ + return ble_svc_dis_data.model_number; +} + +int +ble_svc_dis_model_number_set(const char *value) +{ + ble_svc_dis_data.model_number = value; + return 0; +} + +const char * +ble_svc_dis_serial_number(void) +{ + return ble_svc_dis_data.serial_number; +} + +int +ble_svc_dis_serial_number_set(const char *value) +{ + ble_svc_dis_data.serial_number = value; + return 0; +} + +const char * +ble_svc_dis_firmware_revision(void) +{ + return ble_svc_dis_data.firmware_revision; +} + +int +ble_svc_dis_firmware_revision_set(const char *value) +{ + ble_svc_dis_data.firmware_revision = value; + return 0; +} + +const char * +ble_svc_dis_hardware_revision(void) +{ + return ble_svc_dis_data.hardware_revision; +} + +int +ble_svc_dis_hardware_revision_set(const char *value) +{ + ble_svc_dis_data.hardware_revision = value; + return 0; +} + +const char * +ble_svc_dis_software_revision(void) +{ + return ble_svc_dis_data.software_revision; +} + +int +ble_svc_dis_software_revision_set(const char *value) +{ + ble_svc_dis_data.software_revision = value; + return 0; +} + +const char * +ble_svc_dis_manufacturer_name(void) +{ + return ble_svc_dis_data.manufacturer_name; +} + +int +ble_svc_dis_manufacturer_name_set(const char *value) +{ + ble_svc_dis_data.manufacturer_name = value; + return 0; +} + +const char * +ble_svc_dis_system_id(void) +{ + return ble_svc_dis_data.system_id; +} + +int +ble_svc_dis_system_id_set(const char *value) +{ + ble_svc_dis_data.system_id = value; + return 0; +} + +/** + * Initialize the DIS package. + */ +void +ble_svc_dis_init(void) +{ + int rc; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + rc = ble_gatts_count_cfg(ble_svc_dis_defs); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = ble_gatts_add_svcs(ble_svc_dis_defs); + SYSINIT_PANIC_ASSERT(rc == 0); +} diff --git a/src/libs/mynewt-nimble/nimble/host/services/dis/syscfg.yml b/src/libs/mynewt-nimble/nimble/host/services/dis/syscfg.yml new file mode 100644 index 00000000..b306d3bb --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/dis/syscfg.yml @@ -0,0 +1,109 @@ +# 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. +# + +syscfg.defs: + BLE_SVC_DIS_DEFAULT_READ_PERM: + description: > + Defines default permissions for reading characteristics. Can be + zero to allow read without extra permissions or combination of: + BLE_GATT_CHR_F_READ_ENC + BLE_GATT_CHR_F_READ_AUTHEN + BLE_GATT_CHR_F_READ_AUTHOR + Set to '-1' to remove this characteristic. + value: -1 + BLE_SVC_DIS_MODEL_NUMBER_READ_PERM: + description: > + Defines permissions for reading "Model Number" characteristics. + Can be set to BLE_SVC_DIS_DEFAULT_READ_PERM or use any of the + possible values defined for that setting. + value: 0 + BLE_SVC_DIS_MODEL_NUMBER_DEFAULT: + description: > + Defines a default value for "Model number" if not set with + 'ble_svc_dis_model_number_set'. + value: '"Apache Mynewt NimBLE"' + BLE_SVC_DIS_SERIAL_NUMBER_READ_PERM: + description: > + Defines permissions for reading "Serial Number" characteristics. + Can be set to BLE_SVC_DIS_DEFAULT_READ_PERM or use any of the + possible values defined for that setting. + value: MYNEWT_VAL(BLE_SVC_DIS_DEFAULT_READ_PERM) + BLE_SVC_DIS_SERIAL_NUMBER_DEFAULT: + description: > + Defines a default value for "Serial number" if not set with + 'ble_svc_dis_serial_number_set'. + value: NULL + BLE_SVC_DIS_HARDWARE_REVISION_READ_PERM: + description: > + Defines permissions for reading "Hardware Revision" characteristics. + Can be set to BLE_SVC_DIS_DEFAULT_READ_PERM or use any of the + possible values defined for that setting. + value: MYNEWT_VAL(BLE_SVC_DIS_DEFAULT_READ_PERM) + BLE_SVC_DIS_HARDWARE_REVISION_DEFAULT: + description: > + Defines a default value for "Hardware revision" if not set with + 'ble_svc_dis_hardware_revision_set'. + value: NULL + BLE_SVC_DIS_FIRMWARE_REVISION_READ_PERM: + description: > + Defines permissions for reading "Firmware Revision" characteristics. + Can be set to BLE_SVC_DIS_DEFAULT_READ_PERM or use any of the + possible values defined for that setting. + value: MYNEWT_VAL(BLE_SVC_DIS_DEFAULT_READ_PERM) + BLE_SVC_DIS_FIRMWARE_REVISION_DEFAULT: + description: > + Defines a default value for "Software revision" if not set with + 'ble_svc_dis_firmware_revision_set'. + value: NULL + BLE_SVC_DIS_SOFTWARE_REVISION_READ_PERM: + description: > + Defines permissions for reading "Software Revision" characteristics. + Can be set to BLE_SVC_DIS_DEFAULT_READ_PERM or use any of the + possible values defined for that setting. + value: MYNEWT_VAL(BLE_SVC_DIS_DEFAULT_READ_PERM) + BLE_SVC_DIS_SOFTWARE_REVISION_DEFAULT: + description: > + Defines a default value for "Software revision" if not set with + 'ble_svc_dis_software_revision_set'. + value: NULL + BLE_SVC_DIS_MANUFACTURER_NAME_READ_PERM: + description: > + Defines permissions for reading "Manufacturer name" characteristics. + Can be set to BLE_SVC_DIS_DEFAULT_READ_PERM or use any of the + possible values defined for that setting. + value: MYNEWT_VAL(BLE_SVC_DIS_DEFAULT_READ_PERM) + BLE_SVC_DIS_MANUFACTURER_NAME_DEFAULT: + description: > + Defines a default value for "Manufacturer name" if not set with + 'ble_svc_dis_manufacturer_name_set'. + value: NULL + BLE_SVC_DIS_SYSTEM_ID_READ_PERM: + description: > + Defines permissions for reading "System ID" characteristics. + Can be set to BLE_SVC_DIS_DEFAULT_READ_PERM or use any of the + possible values defined for that setting. + value: MYNEWT_VAL(BLE_SVC_DIS_DEFAULT_READ_PERM) + BLE_SVC_DIS_SYSTEM_ID_DEFAULT: + description: > + Defines a default value for "System ID" if not set with + 'ble_svc_dis_manufacturer_name_set'. + value: NULL + BLE_SVC_DIS_SYSINIT_STAGE: + description: > + Sysinit stage for the device information BLE service. + value: 303 diff --git a/src/libs/mynewt-nimble/nimble/host/services/gap/include/services/gap/ble_svc_gap.h b/src/libs/mynewt-nimble/nimble/host/services/gap/include/services/gap/ble_svc_gap.h new file mode 100644 index 00000000..d7b60df4 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/gap/include/services/gap/ble_svc_gap.h @@ -0,0 +1,54 @@ +/* + * 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 H_BLE_SVC_GAP_ +#define H_BLE_SVC_GAP_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_SVC_GAP_UUID16 0x1800 +#define BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME 0x2a00 +#define BLE_SVC_GAP_CHR_UUID16_APPEARANCE 0x2a01 +#define BLE_SVC_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS 0x2a04 +#define BLE_SVC_GAP_CHR_UUID16_CENTRAL_ADDRESS_RESOLUTION 0x2aa6 + +#define BLE_SVC_GAP_APPEARANCE_GEN_UNKNOWN 0 +#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); +int ble_svc_gap_device_appearance_set(uint16_t appearance); + +void ble_svc_gap_init(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/libs/mynewt-nimble/nimble/host/services/gap/pkg.yml b/src/libs/mynewt-nimble/nimble/host/services/gap/pkg.yml new file mode 100644 index 00000000..a2ef756e --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/gap/pkg.yml @@ -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. +# + +pkg.name: nimble/host/services/gap +pkg.description: Implements the GAP Service. +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + - nimble + - gap + +pkg.deps: + - nimble/host + +pkg.init: + ble_svc_gap_init: 'MYNEWT_VAL(BLE_SVC_GAP_SYSINIT_STAGE)' diff --git a/src/libs/mynewt-nimble/nimble/host/services/gap/src/ble_svc_gap.c b/src/libs/mynewt-nimble/nimble/host/services/gap/src/ble_svc_gap.c new file mode 100644 index 00000000..e79b2b87 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/gap/src/ble_svc_gap.c @@ -0,0 +1,298 @@ +/* + * 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 "sysinit/sysinit.h" +#include "host/ble_hs.h" +#include "services/gap/ble_svc_gap.h" +#include "os/endian.h" + +#define PPCP_ENABLED \ + MYNEWT_VAL(BLE_ROLE_PERIPHERAL) && \ + (MYNEWT_VAL(BLE_SVC_GAP_PPCP_MIN_CONN_INTERVAL) || \ + MYNEWT_VAL(BLE_SVC_GAP_PPCP_MAX_CONN_INTERVAL) || \ + MYNEWT_VAL(BLE_SVC_GAP_PPCP_SLAVE_LATENCY) || \ + MYNEWT_VAL(BLE_SVC_GAP_PPCP_SUPERVISION_TMO)) + +#define BLE_SVC_GAP_NAME_MAX_LEN \ + MYNEWT_VAL(BLE_SVC_GAP_DEVICE_NAME_MAX_LENGTH) + +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 uint16_t ble_svc_gap_appearance = MYNEWT_VAL(BLE_SVC_GAP_APPEARANCE); + +static int +ble_svc_gap_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg); + +static const struct ble_gatt_svc_def ble_svc_gap_defs[] = { + { + /*** Service: GAP. */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BLE_SVC_GAP_UUID16), + .characteristics = (struct ble_gatt_chr_def[]) { { + /*** 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 | +#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), + .access_cb = ble_svc_gap_access, + .flags = BLE_GATT_CHR_F_READ | +#if MYNEWT_VAL(BLE_SVC_GAP_APPEARANCE_WRITE_PERM) >= 0 + BLE_GATT_CHR_F_WRITE | + MYNEWT_VAL(BLE_SVC_GAP_APPEARANCE_WRITE_PERM) | +#endif + 0, + }, { +#if PPCP_ENABLED + /*** Characteristic: Peripheral Preferred Connection Parameters. */ + .uuid = + BLE_UUID16_DECLARE(BLE_SVC_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS), + .access_cb = ble_svc_gap_access, + .flags = BLE_GATT_CHR_F_READ, + }, { +#endif +#if MYNEWT_VAL(BLE_SVC_GAP_CENTRAL_ADDRESS_RESOLUTION) >= 0 + /*** Characteristic: Central Address Resolution. */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_GAP_CHR_UUID16_CENTRAL_ADDRESS_RESOLUTION), + .access_cb = ble_svc_gap_access, + .flags = BLE_GATT_CHR_F_READ, + }, { +#endif + 0, /* No more characteristics in this service. */ + } }, + }, + + { + 0, /* No more services. */ + }, +}; + +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_appearance_read_access(struct ble_gatt_access_ctxt *ctxt) +{ + uint16_t appearance = htole16(ble_svc_gap_appearance); + int rc; + + rc = os_mbuf_append(ctxt->om, &appearance, sizeof(appearance)); + + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +} + +static int +ble_svc_gap_appearance_write_access(struct ble_gatt_access_ctxt *ctxt) +{ +#if MYNEWT_VAL(BLE_SVC_GAP_APPEARANCE_WRITE_PERM) < 0 + assert(0); + return 0; +#else + uint16_t om_len; + int rc; + + om_len = OS_MBUF_PKTLEN(ctxt->om); + if (om_len != sizeof(ble_svc_gap_appearance)) { + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; + } + + rc = ble_hs_mbuf_to_flat(ctxt->om, &ble_svc_gap_appearance, om_len, NULL); + if (rc != 0) { + return BLE_ATT_ERR_UNLIKELY; + } + + ble_svc_gap_appearance = le16toh(ble_svc_gap_appearance); + + if (ble_svc_gap_chr_changed_cb_fn) { + ble_svc_gap_chr_changed_cb_fn(BLE_SVC_GAP_CHR_UUID16_APPEARANCE); + } + + 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) +{ + uint16_t uuid16; +#if MYNEWT_VAL(BLE_SVC_GAP_CENTRAL_ADDRESS_RESOLUTION) >= 0 + uint8_t central_ar = MYNEWT_VAL(BLE_SVC_GAP_CENTRAL_ADDRESS_RESOLUTION); +#endif +#if PPCP_ENABLED + uint16_t ppcp[4] = { + htole16(MYNEWT_VAL(BLE_SVC_GAP_PPCP_MIN_CONN_INTERVAL)), + htole16(MYNEWT_VAL(BLE_SVC_GAP_PPCP_MAX_CONN_INTERVAL)), + htole16(MYNEWT_VAL(BLE_SVC_GAP_PPCP_SLAVE_LATENCY)), + htole16(MYNEWT_VAL(BLE_SVC_GAP_PPCP_SUPERVISION_TMO)) + }; +#endif + int rc; + + uuid16 = ble_uuid_u16(ctxt->chr->uuid); + assert(uuid16 != 0); + + switch (uuid16) { + case BLE_SVC_GAP_CHR_UUID16_DEVICE_NAME: + 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); + rc = BLE_ATT_ERR_UNLIKELY; + } + return rc; + + case BLE_SVC_GAP_CHR_UUID16_APPEARANCE: + if (ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR) { + rc = ble_svc_gap_appearance_read_access(ctxt); + } else if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { + rc = ble_svc_gap_appearance_write_access(ctxt); + } else { + assert(0); + rc = BLE_ATT_ERR_UNLIKELY; + } + return rc; + +#if PPCP_ENABLED + case BLE_SVC_GAP_CHR_UUID16_PERIPH_PREF_CONN_PARAMS: + assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR); + rc = os_mbuf_append(ctxt->om, &ppcp, sizeof(ppcp)); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +#endif + +#if MYNEWT_VAL(BLE_SVC_GAP_CENTRAL_ADDRESS_RESOLUTION) >= 0 + case BLE_SVC_GAP_CHR_UUID16_CENTRAL_ADDRESS_RESOLUTION: + assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR); + rc = os_mbuf_append(ctxt->om, ¢ral_ar, sizeof(central_ar)); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; +#endif + + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } +} + +const char * +ble_svc_gap_device_name(void) +{ + return ble_svc_gap_name; +} + +int +ble_svc_gap_device_name_set(const char *name) +{ + int len; + + len = strlen(name); + if (len > BLE_SVC_GAP_NAME_MAX_LEN) { + return BLE_HS_EINVAL; + } + + memcpy(ble_svc_gap_name, name, len); + ble_svc_gap_name[len] = '\0'; + + return 0; +} + +uint16_t +ble_svc_gap_device_appearance(void) +{ + return ble_svc_gap_appearance; +} + +int +ble_svc_gap_device_appearance_set(uint16_t appearance) +{ + ble_svc_gap_appearance = appearance; + + return 0; +} + +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; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + rc = ble_gatts_count_cfg(ble_svc_gap_defs); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = ble_gatts_add_svcs(ble_svc_gap_defs); + SYSINIT_PANIC_ASSERT(rc == 0); +} diff --git a/src/libs/mynewt-nimble/nimble/host/services/gap/syscfg.yml b/src/libs/mynewt-nimble/nimble/host/services/gap/syscfg.yml new file mode 100644 index 00000000..ad6aa7ef --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/gap/syscfg.yml @@ -0,0 +1,83 @@ +# 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. +# + +syscfg.defs: + BLE_SVC_GAP_DEVICE_NAME: + description: > + 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_DEVICE_NAME_MAX_LENGTH: + description: Maximum length for "Device Name" characteristics + value: 31 + BLE_SVC_GAP_APPEARANCE: + description: 'Device appearance' + value: 0 + BLE_SVC_GAP_APPEARANCE_WRITE_PERM: + description: > + Defines permissions for writing "Appearance" 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 + + # Setting all values for PPCP to '0' will disable characteristic! + BLE_SVC_GAP_PPCP_MIN_CONN_INTERVAL: + description: > + Value of "minimum connection interval" of PPCP characteristic as + defined by Core specification 5.0, Vol 3, Part C, section 12.3. + value: 0 + BLE_SVC_GAP_PPCP_MAX_CONN_INTERVAL: + description: > + Value of "maximum connection interval" of PPCP characteristic as + defined by Core specification 5.0, Vol 3, Part C, section 12.3. + value: 0 + BLE_SVC_GAP_PPCP_SLAVE_LATENCY: + description: > + Value of "slave latency" of PPCP characteristic as defined by Core + specification 5.0, Vol 3, Part C, section 12.3. + value: 0 + BLE_SVC_GAP_PPCP_SUPERVISION_TMO: + description: > + Value of "connection supervision timeout multiplier" of PPCP + characteristic as defined by Core specification 5.0, Vol 3, Part C, + section 12.3. + value: 0 + + BLE_SVC_GAP_CENTRAL_ADDRESS_RESOLUTION: + description: > + Value of "Central Address Resolution" characteristics, as defined + by Core specification 5.0, Vol 3, Part C, section 12. + Set to '-1' to disable. + value: -1 + + BLE_SVC_GAP_SYSINIT_STAGE: + description: > + Sysinit stage for the GAP BLE service. + value: 301 diff --git a/src/libs/mynewt-nimble/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h b/src/libs/mynewt-nimble/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h new file mode 100644 index 00000000..06c44784 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/gatt/include/services/gatt/ble_svc_gatt.h @@ -0,0 +1,40 @@ +/* + * 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 H_BLE_SVC_GATT_ +#define H_BLE_SVC_GATT_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct ble_hs_cfg; + +#define BLE_SVC_GATT_CHR_SERVICE_CHANGED_UUID16 0x2a05 + +void ble_svc_gatt_changed(uint16_t start_handle, uint16_t end_handle); +void ble_svc_gatt_init(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/libs/mynewt-nimble/nimble/host/services/gatt/pkg.yml b/src/libs/mynewt-nimble/nimble/host/services/gatt/pkg.yml new file mode 100644 index 00000000..e3704bc1 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/gatt/pkg.yml @@ -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. +# + +pkg.name: nimble/host/services/gatt +pkg.description: Implements the GATT service. +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + - nimble + - gatt + +pkg.deps: + - nimble/host + +pkg.init: + ble_svc_gatt_init: 'MYNEWT_VAL(BLE_SVC_GATT_SYSINIT_STAGE)' diff --git a/src/libs/mynewt-nimble/nimble/host/services/gatt/src/ble_svc_gatt.c b/src/libs/mynewt-nimble/nimble/host/services/gatt/src/ble_svc_gatt.c new file mode 100644 index 00000000..78b4a068 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/gatt/src/ble_svc_gatt.c @@ -0,0 +1,108 @@ +/* + * 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 "sysinit/sysinit.h" +#include "host/ble_hs.h" +#include "services/gatt/ble_svc_gatt.h" + +static uint16_t ble_svc_gatt_changed_val_handle; +static uint16_t ble_svc_gatt_start_handle; +static uint16_t ble_svc_gatt_end_handle; + +static int +ble_svc_gatt_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg); + +static const struct ble_gatt_svc_def ble_svc_gatt_defs[] = { + { + /*** Service: GATT */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BLE_GATT_SVC_UUID16), + .characteristics = (struct ble_gatt_chr_def[]) { { + .uuid = BLE_UUID16_DECLARE(BLE_SVC_GATT_CHR_SERVICE_CHANGED_UUID16), + .access_cb = ble_svc_gatt_access, + .val_handle = &ble_svc_gatt_changed_val_handle, + .flags = BLE_GATT_CHR_F_INDICATE, + }, { + 0, /* No more characteristics in this service. */ + } }, + }, + + { + 0, /* No more services. */ + }, +}; + +static int +ble_svc_gatt_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + uint8_t *u8p; + + /* The only operation allowed for this characteristic is indicate. This + * access callback gets called by the stack when it needs to read the + * characteristic value to populate the outgoing indication command. + * Therefore, this callback should only get called during an attempt to + * read the characteristic. + */ + assert(ctxt->op == BLE_GATT_ACCESS_OP_READ_CHR); + assert(ctxt->chr == &ble_svc_gatt_defs[0].characteristics[0]); + + u8p = os_mbuf_extend(ctxt->om, 4); + if (u8p == NULL) { + return BLE_HS_ENOMEM; + } + + put_le16(u8p + 0, ble_svc_gatt_start_handle); + put_le16(u8p + 2, ble_svc_gatt_end_handle); + + return 0; +} + +/** + * Indicates a change in attribute assignment to all subscribed peers. + * Unconnected bonded peers receive an indication when they next connect. + * + * @param start_handle The start of the affected handle range. + * @param end_handle The end of the affected handle range. + */ +void +ble_svc_gatt_changed(uint16_t start_handle, uint16_t end_handle) +{ + ble_svc_gatt_start_handle = start_handle; + ble_svc_gatt_end_handle = end_handle; + ble_gatts_chr_updated(ble_svc_gatt_changed_val_handle); +} + +void +ble_svc_gatt_init(void) +{ + int rc; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + rc = ble_gatts_count_cfg(ble_svc_gatt_defs); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = ble_gatts_add_svcs(ble_svc_gatt_defs); + SYSINIT_PANIC_ASSERT(rc == 0); +} diff --git a/src/libs/mynewt-nimble/nimble/host/services/gatt/syscfg.yml b/src/libs/mynewt-nimble/nimble/host/services/gatt/syscfg.yml new file mode 100644 index 00000000..6ba1b333 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/gatt/syscfg.yml @@ -0,0 +1,24 @@ +# +# 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. +# + +syscfg.defs: + BLE_SVC_GATT_SYSINIT_STAGE: + description: > + Sysinit stage for the GATT BLE service + value: 302 diff --git a/src/libs/mynewt-nimble/nimble/host/services/ias/include/services/ias/ble_svc_ias.h b/src/libs/mynewt-nimble/nimble/host/services/ias/include/services/ias/ble_svc_ias.h new file mode 100644 index 00000000..26c18d01 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ias/include/services/ias/ble_svc_ias.h @@ -0,0 +1,38 @@ +/** + * 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 H_BLE_IAS_TPS_ +#define H_BLE_IAS_TPS_ + +#define BLE_SVC_IAS_UUID16 0x1802 +#define BLE_SVC_IAS_CHR_UUID16_ALERT_LEVEL 0x2a06 + +/* Alert level definitions */ +#define BLE_SVC_IAS_ALERT_LEVEL_NO_ALERT 0 +#define BLE_SVC_IAS_ALERT_LEVEL_MILD_ALERT 1 +#define BLE_SVC_IAS_ALERT_LEVEL_HIGH_ALERT 2 + +typedef int ble_svc_ias_event_fn(uint8_t alert_level); + +void ble_svc_ias_set_cb(ble_svc_ias_event_fn *cb); +void ble_svc_ias_init(void); + +#endif + + diff --git a/src/libs/mynewt-nimble/nimble/host/services/ias/pkg.yml b/src/libs/mynewt-nimble/nimble/host/services/ias/pkg.yml new file mode 100644 index 00000000..3b0ca074 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ias/pkg.yml @@ -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. +# + +pkg.name: nimble/host/services/ias +pkg.description: Immediate Alert Service Implementation. +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + - ias + - nimble + +pkg.deps: + - nimble/host + +pkg.init: + ble_svc_ias_init: 'MYNEWT_VAL(BLE_SVC_IAS_SYSINIT_STAGE)' diff --git a/src/libs/mynewt-nimble/nimble/host/services/ias/src/ble_svc_ias.c b/src/libs/mynewt-nimble/nimble/host/services/ias/src/ble_svc_ias.c new file mode 100644 index 00000000..9f5da148 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ias/src/ble_svc_ias.c @@ -0,0 +1,149 @@ +/** + * 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 "sysinit/sysinit.h" +#include "host/ble_hs.h" +#include "services/ias/ble_svc_ias.h" + +/* Callback function */ +static ble_svc_ias_event_fn *ble_svc_ias_cb_fn; + +/* Alert level */ +static uint8_t ble_svc_ias_alert_level; + +/* Write characteristic function */ +static int +ble_svc_ias_chr_write(struct os_mbuf *om, uint16_t min_len, + uint16_t max_len, void *dst, + uint16_t *len); + +/* Access function */ +static int +ble_svc_ias_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg); + +static const struct ble_gatt_svc_def ble_svc_ias_defs[] = { + { + /*** Service: Immediate Alert Service (IAS). */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BLE_SVC_IAS_UUID16), + .characteristics = (struct ble_gatt_chr_def[]) { { + /*** Characteristic: Alert Level. */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_IAS_CHR_UUID16_ALERT_LEVEL), + .access_cb = ble_svc_ias_access, + .flags = BLE_GATT_CHR_F_WRITE_NO_RSP, + }, { + 0, /* No more characteristics in this service. */ + } }, + }, + + { + 0, /* No more services. */ + }, +}; + +/** + * Writes the received value from a characteristic write to + * the given destination. + */ +static int +ble_svc_ias_chr_write(struct os_mbuf *om, uint16_t min_len, + uint16_t max_len, void *dst, + uint16_t *len) +{ + uint16_t om_len; + int rc; + + om_len = OS_MBUF_PKTLEN(om); + if (om_len < min_len || om_len > max_len) { + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; + } + + rc = ble_hs_mbuf_to_flat(om, dst, max_len, len); + if (rc != 0) { + return BLE_ATT_ERR_UNLIKELY; + } + + return 0; +} + +/** + * Simple write access callback for the alert level + * characteristic. + */ +static int +ble_svc_ias_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + int rc; + + assert(ctxt->chr == &ble_svc_ias_defs[0].characteristics[0]); + + switch (ctxt->op) { + case BLE_GATT_ACCESS_OP_WRITE_CHR: + rc = ble_svc_ias_chr_write(ctxt->om, + sizeof ble_svc_ias_alert_level, + sizeof ble_svc_ias_alert_level, + &ble_svc_ias_alert_level, NULL); + /* Call the IAS event function */ + if (ble_svc_ias_cb_fn) { + ble_svc_ias_cb_fn(ble_svc_ias_alert_level); + } + return rc; + + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } + + return 0; +} + +/** + * Designates the specified function as the IAS callback. This callback is + * necessary for this service to function properly. + * + * @param cb The callback function to call when + * the client signals an alert. + */ +void +ble_svc_ias_set_cb(ble_svc_ias_event_fn *cb) +{ + ble_svc_ias_cb_fn = cb; +} + +/** + * Initialize the IAS package. + */ +void +ble_svc_ias_init(void) +{ + int rc; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + rc = ble_gatts_count_cfg(ble_svc_ias_defs); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = ble_gatts_add_svcs(ble_svc_ias_defs); + SYSINIT_PANIC_ASSERT(rc == 0); +} diff --git a/src/libs/mynewt-nimble/nimble/host/services/ias/syscfg.yml b/src/libs/mynewt-nimble/nimble/host/services/ias/syscfg.yml new file mode 100644 index 00000000..2cbed3ab --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ias/syscfg.yml @@ -0,0 +1,23 @@ +# 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. +# + +syscfg.defs: + BLE_SVC_IAS_SYSINIT_STAGE: + description: > + Sysinit stage for the immediate alert BLE service. + value: 303 diff --git a/src/libs/mynewt-nimble/nimble/host/services/ipss/include/services/ipss/ble_svc_ipss.h b/src/libs/mynewt-nimble/nimble/host/services/ipss/include/services/ipss/ble_svc_ipss.h new file mode 100644 index 00000000..d894732c --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ipss/include/services/ipss/ble_svc_ipss.h @@ -0,0 +1,38 @@ +/* + * 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 H_BLE_SVC_IPSS_ +#define H_BLE_SVC_IPSS_ + +#ifdef __cplusplus +extern "C" { +#endif + +#define BLE_SVC_IPSS_UUID16 0x1820 + +/** + * @brief Initialize the IPSS service + */ +void ble_svc_ipss_init(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/libs/mynewt-nimble/nimble/host/services/ipss/pkg.yml b/src/libs/mynewt-nimble/nimble/host/services/ipss/pkg.yml new file mode 100644 index 00000000..55be157d --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ipss/pkg.yml @@ -0,0 +1,35 @@ +# +# 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: nimble/host/services/ipss +pkg.description: Implements the IPSS service for IPSP suppoort. +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + - nimble + - ipsp + - ipss + +pkg.deps: + - nimble/host + +pkg.init: + ble_svc_ipss_init: 'MYNEWT_VAL(BLE_SVC_IPSS_SYSINIT_STAGE)' diff --git a/src/libs/mynewt-nimble/nimble/host/services/ipss/src/ble_svc_ipss.c b/src/libs/mynewt-nimble/nimble/host/services/ipss/src/ble_svc_ipss.c new file mode 100644 index 00000000..f42ca1e9 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ipss/src/ble_svc_ipss.c @@ -0,0 +1,51 @@ +/* + * 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 "sysinit/sysinit.h" +#include "host/ble_hs.h" +#include "services/ipss/ble_svc_ipss.h" + +static const struct ble_gatt_svc_def ble_svc_ipss_defs[] = { + { + /*** Service: GATT */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BLE_SVC_IPSS_UUID16), + .characteristics = NULL, + }, + { + 0, /* No more services. */ + }, +}; + +void +ble_svc_ipss_init(void) +{ + int rc; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + rc = ble_gatts_count_cfg(ble_svc_ipss_defs); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = ble_gatts_add_svcs(ble_svc_ipss_defs); + SYSINIT_PANIC_ASSERT(rc == 0); +} diff --git a/src/libs/mynewt-nimble/nimble/host/services/ipss/syscfg.yml b/src/libs/mynewt-nimble/nimble/host/services/ipss/syscfg.yml new file mode 100644 index 00000000..dd89608e --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/ipss/syscfg.yml @@ -0,0 +1,24 @@ +# +# 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. +# + +syscfg.defs: + BLE_SVC_IPSS_SYSINIT_STAGE: + description: > + Sysinit stage for the IPSS BLE service + value: 303 diff --git a/src/libs/mynewt-nimble/nimble/host/services/lls/include/services/lls/ble_svc_lls.h b/src/libs/mynewt-nimble/nimble/host/services/lls/include/services/lls/ble_svc_lls.h new file mode 100644 index 00000000..28cf607d --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/lls/include/services/lls/ble_svc_lls.h @@ -0,0 +1,51 @@ +/* + * 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 H_BLE_SVC_LLS_ +#define H_BLE_SVC_LLS_ + +#ifdef __cplusplus +extern "C" { +#endif + +struct ble_hs_cfg; + +#define BLE_SVC_LLS_UUID16 0x1803 +#define BLE_SVC_LLS_CHR_UUID16_ALERT_LEVEL 0x2a06 + +/* Alert level definitions */ +#define BLE_SVC_LLS_ALERT_LEVEL_NO_ALERT 0 +#define BLE_SVC_LLS_ALERT_LEVEL_MILD_ALERT 1 +#define BLE_SVC_LLS_ALERT_LEVEL_HIGH_ALERT 2 + +typedef int ble_svc_lls_event_fn(uint8_t alert_level); + +uint8_t ble_svc_lls_alert_level_get(void); +int ble_svc_lls_alert_level_set(uint8_t alert_level); +void ble_svc_lls_on_gap_disconnect(int reason); + +void ble_svc_lls_set_cb(ble_svc_lls_event_fn *cb); +void ble_svc_lls_init(void); + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/src/libs/mynewt-nimble/nimble/host/services/lls/pkg.yml b/src/libs/mynewt-nimble/nimble/host/services/lls/pkg.yml new file mode 100644 index 00000000..6160f020 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/lls/pkg.yml @@ -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. +# + +pkg.name: nimble/host/services/lls +pkg.description: Link Loss Service Implementation. +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + - lls + - nimble + +pkg.deps: + - nimble/host + +pkg.init: + ble_svc_lls_init: 'MYNEWT_VAL(BLE_SVC_LLS_SYSINIT_STAGE)' diff --git a/src/libs/mynewt-nimble/nimble/host/services/lls/src/ble_svc_lls.c b/src/libs/mynewt-nimble/nimble/host/services/lls/src/ble_svc_lls.c new file mode 100644 index 00000000..b5f7f9bd --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/lls/src/ble_svc_lls.c @@ -0,0 +1,192 @@ +/* + * 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 "sysinit/sysinit.h" +#include "host/ble_hs.h" +#include "services/lls/ble_svc_lls.h" + +/* Callback function */ +static ble_svc_lls_event_fn *ble_svc_lls_cb_fn; + +/* Alert level */ +static uint8_t ble_svc_lls_alert_level; + +/* Write characteristic function */ +static int +ble_svc_lls_chr_write(struct os_mbuf *om, uint16_t min_len, + uint16_t max_len, void *dst, + uint16_t *len); + +/* Access function */ +static int +ble_svc_lls_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg); + +static const struct ble_gatt_svc_def ble_svc_lls_defs[] = { + { + /*** Service: Link Loss Service (LLS). */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BLE_SVC_LLS_UUID16), + .characteristics = (struct ble_gatt_chr_def[]) { { + /*** Characteristic: Alert Level. */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_LLS_CHR_UUID16_ALERT_LEVEL), + .access_cb = ble_svc_lls_access, + .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_WRITE, + }, { + 0, /* No more characteristics in this service. */ + } }, + }, + + { + 0, /* No more services. */ + }, +}; + +/** + * Writes the received value from a characteristic write to + * the given destination. + */ +static int +ble_svc_lls_chr_write(struct os_mbuf *om, uint16_t min_len, + uint16_t max_len, void *dst, + uint16_t *len) +{ + uint16_t om_len; + int rc; + + om_len = OS_MBUF_PKTLEN(om); + if (om_len < min_len || om_len > max_len) { + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; + } + + rc = ble_hs_mbuf_to_flat(om, dst, max_len, len); + if (rc != 0) { + return BLE_ATT_ERR_UNLIKELY; + } + + return 0; +} + +/** + * Simple read/write access callback for the alert level + * characteristic. + */ +static int +ble_svc_lls_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + assert(ctxt->chr == &ble_svc_lls_defs[0].characteristics[0]); + int rc; + switch (ctxt->op) { + case BLE_GATT_ACCESS_OP_READ_CHR: + rc = os_mbuf_append(ctxt->om, &ble_svc_lls_alert_level, + sizeof ble_svc_lls_alert_level); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + + case BLE_GATT_ACCESS_OP_WRITE_CHR: + rc = ble_svc_lls_chr_write(ctxt->om, + sizeof ble_svc_lls_alert_level, + sizeof ble_svc_lls_alert_level, + &ble_svc_lls_alert_level, NULL); + return rc; + + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } + + return 0; +} + +/** + * This function is the crux of the link loss service. The application + * developer must call this function inside the gap event callback + * function when a BLE_GAP_EVENT_DISCONNECT event is received and + * pass the disconnect reason into this function. + * + * Here, we then check if the disconnect reason is due to a timout, and if + * so, we call the ble_svc_lls_event_fn callback with the current + * alert level. The actual alert implementation is left up to the + * developer. + * + * @param reason The reason attatched to the GAP disconnect + * event. + */ +void +ble_svc_lls_on_gap_disconnect(int reason) +{ + if (reason == BLE_HS_HCI_ERR(BLE_ERR_CONN_SPVN_TMO)) { + ble_svc_lls_cb_fn(ble_svc_lls_alert_level); + } +} + +/** + * Gets the current alert level. + * + * @return The current alert level + */ +uint8_t +ble_svc_lls_alert_level_get(void) +{ + return ble_svc_lls_alert_level; +} + +/** + * Sets the current alert level. + * + * @return 0 on success, BLE_HS_EINVAL if the given alert level is not valid. + */ +int +ble_svc_lls_alert_level_set(uint8_t alert_level) +{ + if (alert_level > BLE_SVC_LLS_ALERT_LEVEL_HIGH_ALERT) { + return BLE_HS_EINVAL; + } + + memcpy(&ble_svc_lls_alert_level, &alert_level, + sizeof alert_level); + + return 0; +} + +void +ble_svc_lls_set_cb(ble_svc_lls_event_fn *cb) +{ + ble_svc_lls_cb_fn = cb; +} + +/** + * Initialize the IAS package. + */ +void +ble_svc_lls_init(void) +{ + int rc; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + rc = ble_gatts_count_cfg(ble_svc_lls_defs); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = ble_gatts_add_svcs(ble_svc_lls_defs); + SYSINIT_PANIC_ASSERT(rc == 0); +} diff --git a/src/libs/mynewt-nimble/nimble/host/services/lls/syscfg.yml b/src/libs/mynewt-nimble/nimble/host/services/lls/syscfg.yml new file mode 100644 index 00000000..312b08a2 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/lls/syscfg.yml @@ -0,0 +1,22 @@ +# 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. + +syscfg.defs: + BLE_SVC_LLS_SYSINIT_STAGE: + description: > + Sysinit stage for the link loss BLE service. + value: 303 diff --git a/src/libs/mynewt-nimble/nimble/host/services/tps/include/services/tps/ble_svc_tps.h b/src/libs/mynewt-nimble/nimble/host/services/tps/include/services/tps/ble_svc_tps.h new file mode 100644 index 00000000..ec4cd790 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/tps/include/services/tps/ble_svc_tps.h @@ -0,0 +1,31 @@ +/** + * 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 H_BLE_SVC_TPS_ +#define H_BLE_SVC_TPS_ + +struct ble_hs_cfg; + +#define BLE_SVC_TPS_UUID16 0x1804 +#define BLE_SVC_TPS_CHR_UUID16_TX_POWER_LEVEL 0x2a07 + +void ble_svc_tps_init(void); + +#endif + diff --git a/src/libs/mynewt-nimble/nimble/host/services/tps/pkg.yml b/src/libs/mynewt-nimble/nimble/host/services/tps/pkg.yml new file mode 100644 index 00000000..3d4c5e98 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/tps/pkg.yml @@ -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. +# + +pkg.name: nimble/host/services/tps +pkg.description: Tx Power Service adopted specification. +pkg.author: "Apache Mynewt " +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + - tps + - nimble + +pkg.deps: + - nimble/host + +pkg.init: + ble_svc_tps_init: 'MYNEWT_VAL(BLE_SVC_TPS_SYSINIT_STAGE)' diff --git a/src/libs/mynewt-nimble/nimble/host/services/tps/src/ble_svc_tps.c b/src/libs/mynewt-nimble/nimble/host/services/tps/src/ble_svc_tps.c new file mode 100644 index 00000000..9885a921 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/tps/src/ble_svc_tps.c @@ -0,0 +1,107 @@ +/** + * 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 "sysinit/sysinit.h" +#include "syscfg/syscfg.h" +#include "host/ble_hs.h" +#include "services/tps/ble_svc_tps.h" + +/* XXX: We shouldn't be including the host's private header files. The host + * API needs to be updated with a function to query the advertising transmit + * power. + */ +#include "../src/ble_hs_hci_priv.h" + +int8_t ble_svc_tps_tx_power_level; + +/* Access function */ +static int +ble_svc_tps_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg); + +static const struct ble_gatt_svc_def ble_svc_tps_defs[] = { + { + /*** Service: Tx Power Service. */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = BLE_UUID16_DECLARE(BLE_SVC_TPS_UUID16), + .characteristics = (struct ble_gatt_chr_def[]) { { + /*** Characteristic: Tx Power Level. */ + .uuid = BLE_UUID16_DECLARE(BLE_SVC_TPS_CHR_UUID16_TX_POWER_LEVEL), + .access_cb = ble_svc_tps_access, + .flags = BLE_GATT_CHR_F_READ, + }, { + 0, /* No more characteristics in this service. */ + } }, + }, + + { + 0, /* No more services. */ + }, +}; + +/** + * Simple read access callback for the tx power level + * characteristic. + */ +static int +ble_svc_tps_access(uint16_t conn_handle, uint16_t attr_handle, + struct ble_gatt_access_ctxt *ctxt, void *arg) +{ + int rc; + + assert(ctxt->chr == &ble_svc_tps_defs[0].characteristics[0]); + + switch (ctxt->op) { + case BLE_GATT_ACCESS_OP_READ_CHR: + rc = ble_hs_hci_util_read_adv_tx_pwr(&ble_svc_tps_tx_power_level); + if (rc != 0) { + return BLE_ATT_ERR_UNLIKELY; + } + + rc = os_mbuf_append(ctxt->om, &ble_svc_tps_tx_power_level, + sizeof ble_svc_tps_tx_power_level); + return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; + + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } + + return 0; +} + +/** + * Initialize the TPS + */ +void +ble_svc_tps_init(void) +{ + int rc; + + /* Ensure this function only gets called by sysinit. */ + SYSINIT_ASSERT_ACTIVE(); + + rc = ble_gatts_count_cfg(ble_svc_tps_defs); + SYSINIT_PANIC_ASSERT(rc == 0); + + rc = ble_gatts_add_svcs(ble_svc_tps_defs); + SYSINIT_PANIC_ASSERT(rc == 0); +} diff --git a/src/libs/mynewt-nimble/nimble/host/services/tps/syscfg.yml b/src/libs/mynewt-nimble/nimble/host/services/tps/syscfg.yml new file mode 100644 index 00000000..0391e8b1 --- /dev/null +++ b/src/libs/mynewt-nimble/nimble/host/services/tps/syscfg.yml @@ -0,0 +1,23 @@ +# 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. + +syscfg.defs: + BLE_SVC_TPS_SYSINIT_STAGE: + description: > + Sysinit stage for the transmit power BLE service. + value: 303 + -- cgit v1.2.3