1 /*
2  * Copyright (c) 2024 Croxel, Inc.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_NUS_INST_H_
8 #define ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_NUS_INST_H_
9 
10 #include <zephyr/bluetooth/gatt.h>
11 #include <zephyr/sys/iterable_sections.h>
12 #include <zephyr/sys/slist.h>
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 struct bt_nus_inst {
19 	/** Pointer to the NUS Service Instance  */
20 	const struct bt_gatt_service_static *svc;
21 
22 	/** List of subscribers to invoke callbacks on asynchronous events */
23 	sys_slist_t *cbs;
24 };
25 
26 #define BT_UUID_NUS_SERVICE BT_UUID_DECLARE_128(BT_UUID_NUS_SRV_VAL)
27 #define BT_UUID_NUS_TX_CHAR BT_UUID_DECLARE_128(BT_UUID_NUS_TX_CHAR_VAL)
28 #define BT_UUID_NUS_RX_CHAR BT_UUID_DECLARE_128(BT_UUID_NUS_RX_CHAR_VAL)
29 
30 /** Required as the service may be instantiated outside of the module */
31 ssize_t nus_bt_chr_write(struct bt_conn *conn, const struct bt_gatt_attr *attr,
32 	const void *buf, uint16_t len, uint16_t offset, uint8_t flags);
33 void nus_ccc_cfg_changed(const struct bt_gatt_attr *attr, uint16_t value);
34 
35 #define Z_INTERNAL_BT_NUS_INST_DEFINE(_name)							   \
36 												   \
37 BT_GATT_SERVICE_DEFINE(_name##_svc,								   \
38 	BT_GATT_PRIMARY_SERVICE(BT_UUID_NUS_SERVICE),						   \
39 	BT_GATT_CHARACTERISTIC(BT_UUID_NUS_TX_CHAR,						   \
40 		BT_GATT_CHRC_NOTIFY,								   \
41 		BT_GATT_PERM_NONE,								   \
42 		NULL, NULL, NULL),								   \
43 	BT_GATT_CCC(nus_ccc_cfg_changed,							   \
44 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE),					   \
45 	BT_GATT_CHARACTERISTIC(BT_UUID_NUS_RX_CHAR,						   \
46 		BT_GATT_CHRC_WRITE |								   \
47 		BT_GATT_CHRC_WRITE_WITHOUT_RESP,						   \
48 		BT_GATT_PERM_WRITE,								   \
49 		NULL, nus_bt_chr_write, NULL),							   \
50 );												   \
51 												   \
52 sys_slist_t _name##_cbs = SYS_SLIST_STATIC_INIT(&_name##_cbs);					   \
53 												   \
54 STRUCT_SECTION_ITERABLE(bt_nus_inst, _name) = {							   \
55 	.svc = &_name##_svc,									   \
56 	.cbs = &_name##_cbs,									   \
57 }
58 
59 #ifdef __cplusplus
60 }
61 #endif
62 
63 #endif /* ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_NUS_INST_H_ */
64