Skip to content

Commit 108bcfa

Browse files
committed
bluetooth: ANS: Add Alert Notification Service
Add alert notification service (ANS) to Bluetooth subsystem and accompanying sample. Signed-off-by: Sean Kyer <sean.actor@gmail.com>
1 parent 0c84cc5 commit 108bcfa

File tree

11 files changed

+716
-0
lines changed

11 files changed

+716
-0
lines changed

include/zephyr/bluetooth/att.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ extern "C" {
8686
*
8787
* Defined by the Supplement to the Bluetooth Core Specification (CSS), v11, Part B, Section 1.2.
8888
*/
89+
/** Command not supported */
90+
#define BT_ATT_ERR_CMD_NOT_SUP 0xa0
8991
/** Write Request Rejected */
9092
#define BT_ATT_ERR_WRITE_REQ_REJECTED 0xfc
9193
/** Client Characteristic Configuration Descriptor Improperly Configured */
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright (c) 2025 Sean Kyer
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#ifndef ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_ANS_H_
8+
#define ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_ANS_H_
9+
10+
/**
11+
* @brief Alert Notification Service (ANS)
12+
* @defgroup bt_ans Alert Notification Service (ANS)
13+
* @ingroup bluetooth
14+
* @{
15+
*/
16+
17+
#include <stdint.h>
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
/**
24+
* @brief ANS max text string size
25+
*
26+
* This is the max string length to be saved in a New Alert. Text longer than the max is truncated.
27+
*/
28+
#define BT_ANS_MAX_TEXT_STR_SIZE 18
29+
30+
/**
31+
* @brief ANS Category ID Type
32+
*/
33+
typedef uint8_t bt_ans_cat_t;
34+
35+
/**
36+
* @brief ANS Category ID Enum
37+
*
38+
* Enumeration for whether the category is supported.
39+
*/
40+
enum {
41+
bt_ans_cat_simple_alert,
42+
bt_ans_cat_email,
43+
bt_ans_cat_news,
44+
bt_ans_cat_call,
45+
bt_ans_cat_missed_call,
46+
bt_ans_cat_sms_mms,
47+
bt_ans_cat_voice_mail,
48+
bt_ans_cat_schedule,
49+
bt_ans_cat_high_pri_alert,
50+
bt_ans_cat_instant_message,
51+
/* Marker for number of categories */
52+
bt_ans_cat_num,
53+
/* 10–15 reserved */
54+
};
55+
56+
/**
57+
* @brief Send a new alert to central
58+
*
59+
* The new alert is transmitted to the central if notifications are enabled. Each category will save
60+
* the latest call to this function in case an immediate replay is requested via the ANS control
61+
* point.
62+
*
63+
* @param category The bt_ans_cat_t category the notification is for
64+
* @param num_new Number of new alerts since last alert
65+
* @param text Text brief of alert
66+
* @param text_len String length of text param. If greater than MAX_TEXT_STR_SIZE, text is truncated
67+
*
68+
* @return 0 on success
69+
* @return negative error codes on failure
70+
*/
71+
int bt_ans_notify_new_alert(bt_ans_cat_t category, uint8_t num_new, const char *text, int text_len);
72+
73+
/**
74+
* @brief Set the total unread count for a given category
75+
*
76+
* The unread count is transmitted to the central if notifications are enabled. Each category will
77+
* save the latest call to this function in case an immediate replay is requested via the ANS
78+
* control point.
79+
*
80+
* @param category The bt_ans_cat_t category the unread count is for
81+
* @param unread Total number of unread alerts
82+
*
83+
* @return 0 on success
84+
* @return negative error codes on failure
85+
*/
86+
int bt_ans_set_unread_count(bt_ans_cat_t category, uint8_t unread);
87+
88+
#ifdef __cplusplus
89+
}
90+
#endif
91+
92+
/**
93+
* @}
94+
*/
95+
96+
#endif /* ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_BT_ANS_H_ */
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
5+
project(peripheral_ans)
6+
7+
target_sources(app PRIVATE
8+
src/main.c
9+
)
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.. zephyr:code-sample:: ble_peripheral_ans
2+
:name: Peripheral ANS
3+
:relevant-api: bluetooth
4+
5+
Sample application for Alert Notification Service (ANS).
6+
7+
Overview
8+
********
9+
10+
This sample demonstrates the usage of ANS by acting as a peripheral periodically sending
11+
notifications to the connected central.
12+
13+
Requirements
14+
************
15+
16+
* A board with Bluetooth LE support
17+
* Smartphone with BLE app (ADI Attach, nRF Connect, etc.) or dedicated BLE sniffer
18+
19+
Building and Running
20+
********************
21+
22+
This sample can be found under :zephyr_file:`samples/bluetooth/peripheral_ans` in the
23+
Zephyr tree.
24+
25+
To start receiving alerts over the connection, refer to
26+
`GATT Specification Supplement <https://btprodspecificationrefs.blob.core.windows.net/gatt-specification-supplement/GATT_Specification_Supplement.pdf>`_
27+
section 3.12 for byte array to enable/disable notifications and control the service.
28+
29+
See :zephyr:code-sample-category:`bluetooth` samples for details.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CONFIG_LOG=y
2+
CONFIG_BT=y
3+
CONFIG_BT_PERIPHERAL=y
4+
CONFIG_BT_HCI_ERR_TO_STR=y
5+
CONFIG_BT_DEVICE_NAME="Zephyr Peripheral ANS Sample"
6+
CONFIG_BT_ANS=y
7+
CONFIG_BT_ANS_LOG_LEVEL_DBG=y
8+
CONFIG_BT_ANS_NALRT_CAT_SIMPLE_ALERT=y
9+
CONFIG_BT_ANS_UNALRT_CAT_SIMPLE_ALERT=y
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sample:
2+
name: Bluetooth Peripheral ANS
3+
description: Demonstrates the Alert Notification Service (ANS)
4+
tests:
5+
sample.bluetooth.peripheral_ans:
6+
harness: bluetooth
7+
integration_platforms:
8+
- qemu_cortex_m3
9+
- qemu_x86
10+
- nrf52840dk/nrf52840
11+
tags: bluetooth
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2025 Sean Kyer
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/kernel.h>
8+
#include <zephyr/bluetooth/bluetooth.h>
9+
#include <zephyr/bluetooth/hci.h>
10+
#include <zephyr/bluetooth/conn.h>
11+
#include <zephyr/bluetooth/uuid.h>
12+
#include <zephyr/bluetooth/gatt.h>
13+
#include <zephyr/bluetooth/services/ans.h>
14+
15+
static uint8_t num_unread;
16+
17+
static const struct bt_data ad[] = {
18+
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
19+
BT_DATA_BYTES(BT_DATA_UUID16_ALL, BT_UUID_16_ENCODE(BT_UUID_ANS_VAL))};
20+
21+
static const struct bt_data sd[] = {
22+
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1),
23+
};
24+
25+
static void connected(struct bt_conn *conn, uint8_t err)
26+
{
27+
if (err) {
28+
printk("Connection failed, err 0x%02x %s\n", err, bt_hci_err_to_str(err));
29+
} else {
30+
printk("Connected\n");
31+
}
32+
}
33+
34+
static void disconnected(struct bt_conn *conn, uint8_t reason)
35+
{
36+
printk("Disconnected, reason 0x%02x %s\n", reason, bt_hci_err_to_str(reason));
37+
}
38+
39+
BT_CONN_CB_DEFINE(conn_callbacks) = {
40+
.connected = connected,
41+
.disconnected = disconnected,
42+
};
43+
44+
int main(void)
45+
{
46+
int err;
47+
48+
printk("Sample - Bluetooth Peripheral ANS\n");
49+
50+
err = bt_enable(NULL);
51+
if (err) {
52+
printk("Failed to enable bluetooth: %d\n", err);
53+
return err;
54+
}
55+
56+
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
57+
if (err) {
58+
printk("Failed to start advertising: %d\n", err);
59+
return err;
60+
}
61+
62+
num_unread = 0;
63+
64+
while (true) {
65+
int ret;
66+
char msg[] = "Test Alert!\0";
67+
68+
ret = bt_ans_notify_new_alert(bt_ans_cat_simple_alert, 1, msg,
69+
strnlen(msg, BT_ANS_MAX_TEXT_STR_SIZE));
70+
if (ret) {
71+
printk("Failed to push new alert\n");
72+
}
73+
74+
ret = bt_ans_set_unread_count(bt_ans_cat_simple_alert, ++num_unread);
75+
if (ret) {
76+
printk("Failed to push new unread count\n");
77+
}
78+
79+
k_sleep(K_SECONDS(5));
80+
}
81+
82+
return 0;
83+
}

subsys/bluetooth/services/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3+
zephyr_sources_ifdef(CONFIG_BT_ANS ans.c)
34

45
zephyr_sources_ifdef(CONFIG_BT_DIS dis.c)
56

subsys/bluetooth/services/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
menu "GATT Services"
77
depends on BT_CONN
88

9+
rsource "Kconfig.ans"
10+
911
rsource "Kconfig.dis"
1012

1113
rsource "Kconfig.cts"
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Bluetooth GATT Alert Notification Service
2+
3+
# Copyright (c) 2025 Sean Kyer
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
menuconfig BT_ANS
8+
bool "GATT Alert Notification Service"
9+
10+
if BT_ANS
11+
12+
module = BT_ANS
13+
module-str = Alert Notification Service (ANS)
14+
source "subsys/logging/Kconfig.template.log_config"
15+
16+
menu "New Alert Categories"
17+
18+
config BT_ANS_NALRT_CAT_SIMPLE_ALERT
19+
bool "Support Simple Alert"
20+
21+
config BT_ANS_NALRT_CAT_EMAIL
22+
bool "Support Email"
23+
24+
config BT_ANS_NALRT_CAT_NEWS
25+
bool "Support News"
26+
27+
config BT_ANS_NALRT_CAT_CALL
28+
bool "Support Call"
29+
30+
config BT_ANS_NALRT_CAT_MISSED_CALL
31+
bool "Support Missed Call"
32+
33+
config BT_ANS_NALRT_CAT_SMS_MMS
34+
bool "Support SMS/MMS"
35+
36+
config BT_ANS_NALRT_CAT_VOICE_MAIL
37+
bool "Support Voice Mail"
38+
39+
config BT_ANS_NALRT_CAT_SCHEDULE
40+
bool "Support Schedule"
41+
42+
config BT_ANS_NALRT_CAT_HIGH_PRI_ALERT
43+
bool "Support High Prioritized Alert"
44+
45+
config BT_ANS_NALRT_CAT_INSTANT_MESSAGE
46+
bool "Support Instant Message"
47+
48+
endmenu
49+
50+
menu "Unread Alert Categories"
51+
52+
config BT_ANS_UNALRT_CAT_SIMPLE_ALERT
53+
bool "Support Simple Alert"
54+
55+
config BT_ANS_UNALRT_CAT_EMAIL
56+
bool "Support Email"
57+
58+
config BT_ANS_UNALRT_CAT_NEWS
59+
bool "Support News"
60+
61+
config BT_ANS_UNALRT_CAT_CALL
62+
bool "Support Call"
63+
64+
config BT_ANS_UNALRT_CAT_MISSED_CALL
65+
bool "Support Missed Call"
66+
67+
config BT_ANS_UNALRT_CAT_SMS_MMS
68+
bool "Support SMS/MMS"
69+
70+
config BT_ANS_UNALRT_CAT_VOICE_MAIL
71+
bool "Support Voice Mail"
72+
73+
config BT_ANS_UNALRT_CAT_SCHEDULE
74+
bool "Support Schedule"
75+
76+
config BT_ANS_UNALRT_CAT_HIGH_PRI_ALERT
77+
bool "Support High Prioritized Alert"
78+
79+
config BT_ANS_UNALRT_CAT_INSTANT_MESSAGE
80+
bool "Support Instant Message"
81+
82+
endmenu
83+
84+
endif # BT_ANS

0 commit comments

Comments
 (0)