From db80898fcb2231ae7d45e2798a8d3180f85529f3 Mon Sep 17 00:00:00 2001 From: Brian Giori Date: Wed, 27 Jul 2016 17:07:11 -0700 Subject: [PATCH 1/6] added tx power service to profiles X-Original-Commit: 626b219e0d0f6cb6e79802ed7e6612b3856363bb --- .../tps/include/profiles/tps/ble_svc_tps.h | 31 ++++++ nimble/host/profiles/tps/pkg.yml | 32 +++++++ nimble/host/profiles/tps/src/ble_svc_tps.c | 96 +++++++++++++++++++ 3 files changed, 159 insertions(+) create mode 100644 nimble/host/profiles/tps/include/profiles/tps/ble_svc_tps.h create mode 100644 nimble/host/profiles/tps/pkg.yml create mode 100644 nimble/host/profiles/tps/src/ble_svc_tps.c diff --git a/nimble/host/profiles/tps/include/profiles/tps/ble_svc_tps.h b/nimble/host/profiles/tps/include/profiles/tps/ble_svc_tps.h new file mode 100644 index 000000000..c1797ae55 --- /dev/null +++ b/nimble/host/profiles/tps/include/profiles/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 + +int ble_svc_tps_init(struct ble_hs_cfg *cfg); + +#endif + diff --git a/nimble/host/profiles/tps/pkg.yml b/nimble/host/profiles/tps/pkg.yml new file mode 100644 index 000000000..92f43bb51 --- /dev/null +++ b/nimble/host/profiles/tps/pkg.yml @@ -0,0 +1,32 @@ + +# 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: net/nimble/host/profiles/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: + - net/nimble/host + diff --git a/nimble/host/profiles/tps/src/ble_svc_tps.c b/nimble/host/profiles/tps/src/ble_svc_tps.c new file mode 100644 index 000000000..abcd11e84 --- /dev/null +++ b/nimble/host/profiles/tps/src/ble_svc_tps.c @@ -0,0 +1,96 @@ +/** + * 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 "host/ble_hs.h" +#include "profiles/tps/ble_svc_tps.h" +#include "../../../src/ble_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, + .uuid128 = BLE_UUID16(BLE_SVC_TPS_UUID16), + .characteristics = (struct ble_gatt_chr_def[]) { { + /*** Characteristic: Alert Level. */ + .uuid128 = BLE_UUID16(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) +{ + assert(ctxt->chr == &ble_svc_tps_defs[0].characteristics[0]); + switch(ctxt->op) { + case BLE_GATT_ACCESS_OP_READ_CHR: + ble_hci_util_read_adv_tx_pwr(&ble_svc_tps_tx_power_level); + 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); + break; + } + + return 0; +} + +/** + * Initialize the TPS. The developer must specify the event function + * callback for the TPS to function properly. + */ +int +ble_svc_tps_init(struct ble_hs_cfg *cfg) +{ + int rc; + rc = ble_gatts_count_cfg(ble_svc_tps_defs, cfg); + if (rc != 0) { + return rc; + } + + rc = ble_gatts_add_svcs(ble_svc_lls_defs); + if (rc != 0) { + return rc; + } + + return 0; +} From fddb82fcee07382de8912e74297ebd7470218847 Mon Sep 17 00:00:00 2001 From: Brian Giori Date: Thu, 28 Jul 2016 09:37:02 -0700 Subject: [PATCH 2/6] Tx Power Service implementation X-Original-Commit: b4ae19f09222d57f5d446d0b163d79ace8725f11 --- nimble/host/profiles/tps/src/ble_svc_tps.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nimble/host/profiles/tps/src/ble_svc_tps.c b/nimble/host/profiles/tps/src/ble_svc_tps.c index abcd11e84..7a5bd0828 100644 --- a/nimble/host/profiles/tps/src/ble_svc_tps.c +++ b/nimble/host/profiles/tps/src/ble_svc_tps.c @@ -59,6 +59,7 @@ ble_svc_tps_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { assert(ctxt->chr == &ble_svc_tps_defs[0].characteristics[0]); + int rc; switch(ctxt->op) { case BLE_GATT_ACCESS_OP_READ_CHR: ble_hci_util_read_adv_tx_pwr(&ble_svc_tps_tx_power_level); @@ -87,7 +88,7 @@ ble_svc_tps_init(struct ble_hs_cfg *cfg) return rc; } - rc = ble_gatts_add_svcs(ble_svc_lls_defs); + rc = ble_gatts_add_svcs(ble_svc_tps_defs); if (rc != 0) { return rc; } From 19bb3c69a2cbb7c04a27e56d4b6eb828ae7c7f30 Mon Sep 17 00:00:00 2001 From: Brian Giori Date: Thu, 28 Jul 2016 09:42:43 -0700 Subject: [PATCH 3/6] updated comments and error return value X-Original-Commit: d0b6d35a879e02dc5bb9c734091c111d1dc10c07 --- nimble/host/profiles/tps/src/ble_svc_tps.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/nimble/host/profiles/tps/src/ble_svc_tps.c b/nimble/host/profiles/tps/src/ble_svc_tps.c index 7a5bd0828..3e2bf42d5 100644 --- a/nimble/host/profiles/tps/src/ble_svc_tps.c +++ b/nimble/host/profiles/tps/src/ble_svc_tps.c @@ -36,7 +36,7 @@ static const struct ble_gatt_svc_def ble_svc_tps_defs[] = { .type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid128 = BLE_UUID16(BLE_SVC_TPS_UUID16), .characteristics = (struct ble_gatt_chr_def[]) { { - /*** Characteristic: Alert Level. */ + /*** Characteristic: Tx Power Level. */ .uuid128 = BLE_UUID16(BLE_SVC_TPS_CHR_UUID16_TX_POWER_LEVEL), .access_cb = ble_svc_tps_access, .flags = BLE_GATT_CHR_F_READ, @@ -69,15 +69,14 @@ ble_svc_tps_access(uint16_t conn_handle, uint16_t attr_handle, default: assert(0); - break; + return BLE_ATT_ERR_UNLIKELY; } return 0; } /** - * Initialize the TPS. The developer must specify the event function - * callback for the TPS to function properly. + * Initialize the TPS */ int ble_svc_tps_init(struct ble_hs_cfg *cfg) From e00789e4047c32dd6cb2dd8b96aba17e61ec3cea Mon Sep 17 00:00:00 2001 From: Brian Giori Date: Thu, 28 Jul 2016 14:33:34 -0700 Subject: [PATCH 4/6] coding style changes X-Original-Commit: 98caf9f298499df9571c7ad96bcccbd75586f1e0 --- nimble/host/profiles/tps/src/ble_svc_tps.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nimble/host/profiles/tps/src/ble_svc_tps.c b/nimble/host/profiles/tps/src/ble_svc_tps.c index 3e2bf42d5..e354eec43 100644 --- a/nimble/host/profiles/tps/src/ble_svc_tps.c +++ b/nimble/host/profiles/tps/src/ble_svc_tps.c @@ -60,7 +60,7 @@ ble_svc_tps_access(uint16_t conn_handle, uint16_t attr_handle, { assert(ctxt->chr == &ble_svc_tps_defs[0].characteristics[0]); int rc; - switch(ctxt->op) { + switch (ctxt->op) { case BLE_GATT_ACCESS_OP_READ_CHR: ble_hci_util_read_adv_tx_pwr(&ble_svc_tps_tx_power_level); rc = os_mbuf_append(ctxt->om, &ble_svc_tps_tx_power_level, From 17b457bb1ddc3840cb90da5fadf9c684bf7cbf86 Mon Sep 17 00:00:00 2001 From: Brian Giori Date: Mon, 1 Aug 2016 14:03:38 -0700 Subject: [PATCH 5/6] variables declared at top of function scope X-Original-Commit: ef3024f99d6b805e37b1a8e91057e4992e98b845 --- nimble/host/profiles/tps/src/ble_svc_tps.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/nimble/host/profiles/tps/src/ble_svc_tps.c b/nimble/host/profiles/tps/src/ble_svc_tps.c index e354eec43..fbbd0b5fd 100644 --- a/nimble/host/profiles/tps/src/ble_svc_tps.c +++ b/nimble/host/profiles/tps/src/ble_svc_tps.c @@ -58,8 +58,10 @@ static int ble_svc_tps_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg) { - assert(ctxt->chr == &ble_svc_tps_defs[0].characteristics[0]); int rc; + + assert(ctxt->chr == &ble_svc_tps_defs[0].characteristics[0]); + switch (ctxt->op) { case BLE_GATT_ACCESS_OP_READ_CHR: ble_hci_util_read_adv_tx_pwr(&ble_svc_tps_tx_power_level); From 042f02971e974a0e008f8143b6d062276516c0ac Mon Sep 17 00:00:00 2001 From: Brian Giori Date: Mon, 1 Aug 2016 17:20:30 -0700 Subject: [PATCH 6/6] moved tps to services dir. X-Original-Commit: df94f0101803639aa412193a8eb6f0677ac94c27 --- .../tps/include/services}/tps/ble_svc_tps.h | 0 nimble/host/{profiles => services}/tps/pkg.yml | 2 +- nimble/host/{profiles => services}/tps/src/ble_svc_tps.c | 2 +- 3 files changed, 2 insertions(+), 2 deletions(-) rename nimble/host/{profiles/tps/include/profiles => services/tps/include/services}/tps/ble_svc_tps.h (100%) rename nimble/host/{profiles => services}/tps/pkg.yml (96%) rename nimble/host/{profiles => services}/tps/src/ble_svc_tps.c (98%) diff --git a/nimble/host/profiles/tps/include/profiles/tps/ble_svc_tps.h b/nimble/host/services/tps/include/services/tps/ble_svc_tps.h similarity index 100% rename from nimble/host/profiles/tps/include/profiles/tps/ble_svc_tps.h rename to nimble/host/services/tps/include/services/tps/ble_svc_tps.h diff --git a/nimble/host/profiles/tps/pkg.yml b/nimble/host/services/tps/pkg.yml similarity index 96% rename from nimble/host/profiles/tps/pkg.yml rename to nimble/host/services/tps/pkg.yml index 92f43bb51..45d85b6ec 100644 --- a/nimble/host/profiles/tps/pkg.yml +++ b/nimble/host/services/tps/pkg.yml @@ -17,7 +17,7 @@ # under the License. # -pkg.name: net/nimble/host/profiles/tps +pkg.name: net/nimble/host/services/tps pkg.description: Tx Power Service adopted specification. pkg.author: "Apache Mynewt " pkg.homepage: "http://mynewt.apache.org/" diff --git a/nimble/host/profiles/tps/src/ble_svc_tps.c b/nimble/host/services/tps/src/ble_svc_tps.c similarity index 98% rename from nimble/host/profiles/tps/src/ble_svc_tps.c rename to nimble/host/services/tps/src/ble_svc_tps.c index fbbd0b5fd..3f241b01c 100644 --- a/nimble/host/profiles/tps/src/ble_svc_tps.c +++ b/nimble/host/services/tps/src/ble_svc_tps.c @@ -20,7 +20,7 @@ #include #include #include "host/ble_hs.h" -#include "profiles/tps/ble_svc_tps.h" +#include "services/tps/ble_svc_tps.h" #include "../../../src/ble_hci_priv.h" int8_t ble_svc_tps_tx_power_level;