Added the profiles dir. Added Link Lost Service
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/e91a0d98 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/e91a0d98 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/e91a0d98 Branch: refs/heads/develop Commit: e91a0d980ef892f57dbe941ebdb6988958fd5917 Parents: a0810f7 Author: Brian Giori <[email protected]> Authored: Thu Jul 21 19:05:04 2016 -0700 Committer: Brian Giori <[email protected]> Committed: Wed Jul 27 16:28:18 2016 -0700 ---------------------------------------------------------------------- .../profiles/include/profiles/ble_svc_lls.h | 45 +++++ net/nimble/profiles/pkg.yml | 31 ++++ net/nimble/profiles/src/ble_svc_lls.c | 166 +++++++++++++++++++ 3 files changed, 242 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e91a0d98/net/nimble/profiles/include/profiles/ble_svc_lls.h ---------------------------------------------------------------------- diff --git a/net/nimble/profiles/include/profiles/ble_svc_lls.h b/net/nimble/profiles/include/profiles/ble_svc_lls.h new file mode 100644 index 0000000..dfc7878 --- /dev/null +++ b/net/nimble/profiles/include/profiles/ble_svc_lls.h @@ -0,0 +1,45 @@ +/** + * 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_ + +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); +uint8_t ble_svc_lls_alert_level_set(uint8_t alert_level); +void ble_svc_lls_on_gap_event(struct ble_gap_event *event, void *arg); + +int ble_svc_lls_register(void); +int ble_svc_lls_init(struct ble_hs_cfg *cfg, + uint8_t initial_alert_level, + ble_svc_lls_event_fn *cb); + +#endif + http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e91a0d98/net/nimble/profiles/pkg.yml ---------------------------------------------------------------------- diff --git a/net/nimble/profiles/pkg.yml b/net/nimble/profiles/pkg.yml new file mode 100644 index 0000000..89cd9d4 --- /dev/null +++ b/net/nimble/profiles/pkg.yml @@ -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. +# + +pkg.name: net/nimble/profiles +pkg.description: Adopted profiles/services of the nimble Bluetooth Smart stack. +pkg.author: "Apache Mynewt <[email protected]>" +pkg.homepage: "http://mynewt.apache.org/" +pkg.keywords: + - ble + - bluetooth + +pkg.deps: + - sys/stats + - libs/os + - net/nimble http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/e91a0d98/net/nimble/profiles/src/ble_svc_lls.c ---------------------------------------------------------------------- diff --git a/net/nimble/profiles/src/ble_svc_lls.c b/net/nimble/profiles/src/ble_svc_lls.c new file mode 100644 index 0000000..e94f192 --- /dev/null +++ b/net/nimble/profiles/src/ble_svc_lls.c @@ -0,0 +1,166 @@ +/** + * 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 <assert.h> +#include <string.h> +#include "host/ble_hs.h" +#include "profiles/ble_svc_lls.h" + +/* Callback function */ +ble_svc_lls_event_fn *cb_fn; +/* Alert level */ +uint8_t ble_svc_lls_alert_level; + +/* 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 Lost Service (LLS). */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid128 = BLE_UUID16(BLE_SVC_LLS_UUID16), + .characteristics = (struct ble_gatt_chr_def[]) { { + /*** Characteristic: Alert Level. */ + .uuid128 = BLE_UUID16(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. */ + }, +}; + +/** + * 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]); + switch(ctxt->op) { + case BLE_GATT_ACCESS_OP_READ_CHR: + ctxt->att->read.data = &ble_svc_lls_alert_level; + ctxt->att->read.len = sizeof ble_svc_lls_alert_level; + break; + + case BLE_GATT_ACCESS_OP_WRITE_CHR: + if (ctxt->att->write.len != sizeof ble_svc_lls_alert_level) { + return BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN; + } + memcpy(&ble_svc_lls_alert_level, ctxt->att->write.data, + sizeof ble_svc_lls_alert_level); + break; + + default: + assert(0); + break; + } + + return 0; +} + +/** + * This function is the crux of the link lost service. The application + * developer must call this function inside the gap event callback + * function when a BLE_GAP_EVENT_DISCONNECT event is received. 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 event The BLE GAP event from the GAP event + * callback. + * @param arg The BLE GAP event arg from the GAP + * event callback. + */ +void +ble_svc_lls_on_gap_event(struct ble_gap_event *event, void *arg) { + if(event->disconnect.reason == + BLE_HS_HCI_ERR(BLE_ERR_CONN_SPVN_TMO)) { + cb_fn(ble_svc_lls_alert_level); + } +} + +/** + * Get the current alert level. + */ +uint8_t +ble_svc_lls_alert_level_get(void) +{ + return ble_svc_lls_alert_level; +} + +/** + * Set the current alert level. + */ +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; +} + +/** + * Registers the LLS with the GATT server. + */ +int +ble_svc_lls_register(void) +{ + int rc; + + rc = ble_gatts_register_svcs(ble_svc_lls_defs, NULL, NULL); + return rc; +} + +/** + * Initialize the LLS. The developer must specify the event function + * callback for the LLS to function properly. + */ +int +ble_svc_lls_init(struct ble_hs_cfg *cfg, uint8_t initial_alert_level, + ble_svc_lls_event_fn *cb) +{ + if (*cb) { + return BLE_HS_EINVAL; + } + ble_svc_lls_alert_level = initial_alert_level; + cb_fn = cb; + + int rc; + rc = ble_gatts_count_cfg(ble_svc_lls_defs, cfg); + if (rc != 0) { + return rc; + } + + return 0; +}
