Line data Source code
1 0 : /* 2 : * Copyright (c) 2024 Croxel, Inc. 3 : * 4 : * SPDX-License-Identifier: Apache-2.0 5 : */ 6 : 7 : #ifndef ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_NUS_H_ 8 : #define ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_NUS_H_ 9 : 10 : #include <zephyr/bluetooth/uuid.h> 11 : #include <zephyr/bluetooth/services/nus/inst.h> 12 : #include <zephyr/sys/slist.h> 13 : 14 : #ifdef __cplusplus 15 : extern "C" { 16 : #endif 17 : 18 : /** @brief UUIDs of Nordic UART GATT Service. 19 : * Service: 6e400001-b5a3-f393-e0a9-e50e24dcca9e 20 : * RX Char: 6e400002-b5a3-f393-e0a9-e50e24dcca9e 21 : * TX Char: 6e400003-b5a3-f393-e0a9-e50e24dcca9e 22 : */ 23 1 : #define BT_UUID_NUS_SRV_VAL \ 24 : BT_UUID_128_ENCODE(0x6e400001, 0xb5a3, 0xf393, 0xe0a9, 0xe50e24dcca9e) 25 0 : #define BT_UUID_NUS_RX_CHAR_VAL \ 26 : BT_UUID_128_ENCODE(0x6e400002, 0xb5a3, 0xf393, 0xe0a9, 0xe50e24dcca9e) 27 0 : #define BT_UUID_NUS_TX_CHAR_VAL \ 28 : BT_UUID_128_ENCODE(0x6e400003, 0xb5a3, 0xf393, 0xe0a9, 0xe50e24dcca9e) 29 : 30 : /** @brief Macro to define instance of NUS Service. It allows users to 31 : * define multiple NUS instances, analogous to Serial endpoints, and use each 32 : * one for different purposes. A default NUS instance may be defined through 33 : * Kconfig. 34 : */ 35 1 : #define BT_NUS_INST_DEFINE(_name) \ 36 : Z_INTERNAL_BT_NUS_INST_DEFINE(_name) 37 : 38 : /** @brief Callbacks for getting notified on NUS Service occurrences */ 39 1 : struct bt_nus_cb { 40 : /** @brief Notifications subscription changed 41 : * 42 : * @param enabled Flag that is true if notifications were enabled, false 43 : * if they were disabled. 44 : * @param ctx User context provided in the callback structure. 45 : */ 46 1 : void (*notif_enabled)(bool enabled, void *ctx); 47 : 48 : /** @brief Received Data 49 : * 50 : * @param conn Peer Connection object. 51 : * @param data Pointer to buffer with data received. 52 : * @param len Size in bytes of data received. 53 : * @param ctx User context provided in the callback structure. 54 : */ 55 1 : void (*received)(struct bt_conn *conn, const void *data, uint16_t len, void *ctx); 56 : 57 : /** Internal member. Provided as a callback argument for user context */ 58 1 : void *ctx; 59 : 60 : /** Internal member to form a list of callbacks */ 61 : sys_snode_t _node; 62 : }; 63 : 64 : /** @brief NUS server Instance callback register 65 : * 66 : * This function registers callbacks that will be called in 67 : * certain events related to NUS. 68 : * 69 : * @param inst Pointer to instance of NUS service. NULL if using default instance. 70 : * @param cb Pointer to callbacks structure. Must be valid throughout the 71 : * lifetime of the application. 72 : * @param ctx User context to be provided through the callback. 73 : * 74 : * @return 0 on success 75 : * @return -EINVAL in case @p cb is NULL 76 : */ 77 1 : int bt_nus_inst_cb_register(struct bt_nus_inst *inst, struct bt_nus_cb *cb, void *ctx); 78 : 79 : /** @brief Send Data to NUS Instance 80 : * 81 : * @note This API sends the data to the specified peer. 82 : * 83 : * @param conn Connection object to send data to. NULL if notifying all peers. 84 : * @param inst Pointer to instance of NUS service. NULL if using default instance. 85 : * @param data Pointer to buffer with bytes to send. 86 : * @param len Length in bytes of data to send. 87 : * 88 : * @return 0 on success, negative error code if failed. 89 : * @return -EAGAIN when Bluetooth stack has not been enabled. 90 : * @return -ENOTCONN when either no connection has been established or no peers 91 : * have subscribed. 92 : */ 93 1 : int bt_nus_inst_send(struct bt_conn *conn, 94 : struct bt_nus_inst *inst, 95 : const void *data, 96 : uint16_t len); 97 : 98 : /** @brief NUS server callback register 99 : * 100 : * @param cb Pointer to callbacks structure. Must be valid throughout the 101 : * lifetime of the application. 102 : * @param ctx User context to be provided through the callback. 103 : * 104 : * @return 0 on success, negative error code if failed. 105 : * @return -EINVAL in case @p cb is NULL 106 : */ 107 1 : static inline int bt_nus_cb_register(struct bt_nus_cb *cb, void *ctx) 108 : { 109 : return bt_nus_inst_cb_register(NULL, cb, ctx); 110 : } 111 : 112 : /** @brief Send Data over NUS 113 : * 114 : * @note This API sends the data to the specified peer. 115 : * 116 : * @param conn Connection object to send data to. NULL if notifying all peers. 117 : * @param data Pointer to buffer with bytes to send. 118 : * @param len Length in bytes of data to send. 119 : * 120 : * @return 0 on success, negative error code if failed. 121 : * @return -EAGAIN when Bluetooth stack has not been enabled. 122 : * @return -ENOTCONN when either no connection has been established or no peers 123 : * have subscribed. 124 : */ 125 1 : static inline int bt_nus_send(struct bt_conn *conn, const void *data, uint16_t len) 126 : { 127 : return bt_nus_inst_send(conn, NULL, data, len); 128 : } 129 : 130 : #ifdef __cplusplus 131 : } 132 : #endif 133 : 134 : 135 : #endif /* ZEPHYR_INCLUDE_BLUETOOTH_SERVICES_NUS_H_ */