-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Bluetooth: Immediate Alert Service #43574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright (c) 2022 Codecoup | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_IAS_H_ | ||
| #define ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_IAS_H_ | ||
|
|
||
| /** | ||
| * @brief Immediate Alert Service (IAS) | ||
| * @defgroup bt_ias Immediate Alert Service (IAS) | ||
| * @ingroup bluetooth | ||
| * @{ | ||
| * | ||
| * [Experimental] Users should note that the APIs can change | ||
| * as a part of ongoing development. | ||
| */ | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| /** @brief Immediate Alert Service callback structure. */ | ||
| struct bt_ias_cb { | ||
| /** | ||
| * @brief Callback function to stop alert. | ||
| * | ||
| * This callback is called when peer commands to disable alert. | ||
| */ | ||
| void (*no_alert)(void); | ||
|
|
||
| /** | ||
| * @brief Callback function for alert level value. | ||
| * | ||
| * This callback is called when peer commands to alert. | ||
| */ | ||
| void (*mild_alert)(void); | ||
|
|
||
| /** | ||
| * @brief Callback function for alert level value. | ||
| * | ||
| * This callback is called when peer commands to alert in the strongest possible way. | ||
| */ | ||
| void (*high_alert)(void); | ||
| }; | ||
|
|
||
| /** @brief Method for stopping alert locally | ||
| * | ||
| * @return Zero in case of success and error code in case of error. | ||
| */ | ||
| int bt_ias_local_alert_stop(void); | ||
|
|
||
| /** @def BT_IAS_CB_DEFINE | ||
| * | ||
| * @brief Register a callback structure for immediate alert events. | ||
| * | ||
| * @param _name Name of callback structure. | ||
| */ | ||
| #define BT_IAS_CB_DEFINE(_name) \ | ||
| static const STRUCT_SECTION_ITERABLE(bt_ias_cb, _CONCAT(bt_ias_cb_, _name)) | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_IAS_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,8 @@ rsource "Kconfig.hrs" | |
|
|
||
| rsource "Kconfig.tps" | ||
|
|
||
| rsource "Kconfig.ias" | ||
|
|
||
| rsource "ots/Kconfig" | ||
|
|
||
| endmenu | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Bluetooth GATT Immediate Alert Service | ||
|
|
||
| # Copyright (c) 2022 Codecoup | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| menuconfig BT_IAS | ||
| bool "Support for GATT Immediate Alert Service [EXPERIMENTAL]" | ||
| select EXPERIMENTAL | ||
|
|
||
| if BT_IAS | ||
|
|
||
| choice BT_IAS_SEC_CHOICE | ||
| prompt "Default permissions used for IAS characteristics" | ||
| default BT_IAS_SEC_NONE | ||
| help | ||
| Default write permissions for IAS characteristic attributes | ||
|
|
||
| config BT_IAS_SEC_NONE | ||
| bool "No security required" | ||
|
|
||
| config BT_IAS_SEC_ENC | ||
| bool "Require encryption for write access" | ||
|
|
||
| config BT_IAS_SEC_AUTH | ||
| bool "Require encryption and authentication for write access" | ||
|
|
||
| endchoice #BT_IAS_SEC_CHOICE | ||
|
|
||
| module = BT_IAS | ||
| module-str = IAS | ||
| source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config" | ||
|
|
||
| endif # BT_IAS |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| /** @file | ||
| * @brief Immediate Alert Service implementation | ||
| */ | ||
|
|
||
| /* | ||
| * Copyright (c) 2022 Codecoup | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <errno.h> | ||
| #include <init.h> | ||
| #include <stdint.h> | ||
| #include <zephyr.h> | ||
| #include <net/buf.h> | ||
| #include <logging/log.h> | ||
| #include <bluetooth/gatt.h> | ||
| #include <bluetooth/conn.h> | ||
| #include <bluetooth/services/ias.h> | ||
|
|
||
| #define LOG_LEVEL CONFIG_BT_IAS_LOG_LEVEL | ||
| LOG_MODULE_REGISTER(ias); | ||
|
|
||
| #define BT_IAS_ALERT_LVL_LEN 1 | ||
|
|
||
| #if defined(CONFIG_BT_IAS_SEC_AUTH) | ||
| #define IAS_ALERT_LEVEL_PERM BT_GATT_PERM_WRITE_AUTHEN | ||
| #elif defined(CONFIG_BT_IAS_SEC_ENC) | ||
| #define IAS_ALERT_LEVEL_PERM BT_GATT_PERM_WRITE_ENCRYPT | ||
| #else | ||
| #define IAS_ALERT_LEVEL_PERM BT_GATT_PERM_WRITE | ||
| #endif | ||
|
|
||
| enum bt_ias_alert_lvl { | ||
| BT_IAS_ALERT_LVL_NO_ALERT, | ||
| BT_IAS_ALERT_LVL_MILD_ALERT, | ||
| BT_IAS_ALERT_LVL_HIGH_ALERT, | ||
| }; | ||
|
|
||
| struct alerting_device { | ||
| enum bt_ias_alert_lvl alert_level; | ||
| }; | ||
|
|
||
| static struct alerting_device devices[CONFIG_BT_MAX_CONN]; | ||
| static enum bt_ias_alert_lvl curr_lvl; | ||
|
|
||
| static void set_alert_level(void) | ||
| { | ||
| enum bt_ias_alert_lvl alert_level; | ||
|
|
||
| alert_level = devices[0].alert_level; | ||
| for (int i = 1; i < CONFIG_BT_MAX_CONN; i++) { | ||
| if (alert_level < devices[i].alert_level) { | ||
| alert_level = devices[i].alert_level; | ||
| } | ||
| } | ||
|
|
||
| if (curr_lvl == alert_level) { | ||
| return; | ||
| } | ||
|
|
||
| if (alert_level == BT_IAS_ALERT_LVL_HIGH_ALERT) { | ||
| STRUCT_SECTION_FOREACH(bt_ias_cb, cb) { | ||
| if (cb->high_alert) { | ||
| cb->high_alert(); | ||
| } | ||
| } | ||
| LOG_DBG("High alert"); | ||
| } else if (alert_level == BT_IAS_ALERT_LVL_MILD_ALERT) { | ||
| STRUCT_SECTION_FOREACH(bt_ias_cb, cb) { | ||
| if (cb->mild_alert) { | ||
| cb->mild_alert(); | ||
| } | ||
| } | ||
| LOG_DBG("Mild alert"); | ||
| } else { | ||
| STRUCT_SECTION_FOREACH(bt_ias_cb, cb) { | ||
| if (cb->no_alert) { | ||
| cb->no_alert(); | ||
| } | ||
| } | ||
| LOG_DBG("No alert"); | ||
| } | ||
| curr_lvl = alert_level; | ||
| } | ||
|
|
||
| static void disconnected(struct bt_conn *conn, uint8_t reason) | ||
| { | ||
| devices[bt_conn_index(conn)].alert_level = BT_IAS_ALERT_LVL_NO_ALERT; | ||
| set_alert_level(); | ||
| } | ||
|
|
||
| int bt_ias_local_alert_stop(void) | ||
| { | ||
| if (curr_lvl == BT_IAS_ALERT_LVL_NO_ALERT) { | ||
| return -EALREADY; | ||
| } | ||
|
|
||
| for (int idx = 0; idx < CONFIG_BT_MAX_CONN; idx++) { | ||
| devices[idx].alert_level = BT_IAS_ALERT_LVL_NO_ALERT; | ||
| } | ||
| curr_lvl = BT_IAS_ALERT_LVL_NO_ALERT; | ||
| set_alert_level(); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| static ssize_t bt_ias_write_alert_lvl(struct bt_conn *conn, const struct bt_gatt_attr *attr, | ||
| const void *buf, uint16_t len, uint16_t offset, uint8_t flags) | ||
| { | ||
| struct net_buf_simple data; | ||
| enum bt_ias_alert_lvl alert_val; | ||
|
|
||
| if (offset > 0) { | ||
| return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); | ||
| } | ||
|
|
||
| if (len != BT_IAS_ALERT_LVL_LEN) { | ||
| return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN); | ||
| } | ||
|
|
||
| net_buf_simple_init_with_data(&data, (void *)buf, len); | ||
| alert_val = net_buf_simple_pull_u8(&data); | ||
| devices[bt_conn_index(conn)].alert_level = alert_val; | ||
|
|
||
| if (alert_val < BT_IAS_ALERT_LVL_NO_ALERT || alert_val > BT_IAS_ALERT_LVL_HIGH_ALERT) { | ||
| return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED); | ||
| } | ||
| set_alert_level(); | ||
|
|
||
| return len; | ||
| } | ||
|
|
||
| BT_CONN_CB_DEFINE(conn_callbacks) = { | ||
| .disconnected = disconnected, | ||
| }; | ||
|
|
||
| /* Immediate Alert Service Declaration */ | ||
| BT_GATT_SERVICE_DEFINE(ias_svc, | ||
| BT_GATT_PRIMARY_SERVICE(BT_UUID_IAS), | ||
| BT_GATT_CHARACTERISTIC(BT_UUID_ALERT_LEVEL, BT_GATT_CHRC_WRITE_WITHOUT_RESP, | ||
| IAS_ALERT_LEVEL_PERM, NULL, | ||
| bt_ias_write_alert_lvl, NULL)); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.