1 /**
2 * Copyright (c) 2019 Oticon A/S
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 /**
7 * @brief Service B.1
8 *
9 * This code is auto-generated from the Excel Workbook
10 * 'GATT_Test_Databases.xlsm' Sheet: 'Large Database 1'
11 */
12 #include <zephyr/sys/byteorder.h>
13 #include <zephyr/sys/printk.h>
14
15 #include <zephyr/bluetooth/gatt.h>
16
17 #include "gatt_macs.h"
18
19 /**
20 * @brief UUID for the Service B.1
21 */
22 #define BT_UUID_SERVICE_B_1 BT_UUID_DECLARE_16(0xa00b)
23
24 /**
25 * @brief UUID for the Value V4 Characteristic
26 */
27 #define BT_UUID_VALUE_V4 BT_UUID_DECLARE_16(0xb004)
28
29 /**
30 * @brief UUID for the Long descriptor V2D1 Characteristic
31 */
32 #define BT_UUID_LONG_DES_V2D1 BT_UUID_DECLARE_16(0xb012)
33
34 static uint8_t value_v4_value = 0x04;
35 static uint8_t value_v4_1_value = 0x04;
36 static uint16_t server_cha_con_value;
37 static uint8_t value_v4_2_value = 0x04;
38 static uint8_t long_des_v2d1_value[] = {
39 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
40 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
41 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
42 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33
43 };
44 static uint8_t value_v4_3_value[] = {
45 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
46 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
47 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
48 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33
49 };
50 static uint8_t long_des_v2d1_1_value[] = {
51 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
52 0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
53 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
54 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33
55 };
56
57 /**
58 * @brief Attribute read call back for the Value V4 attribute
59 *
60 * @param conn The connection that is requesting to read
61 * @param attr The attribute that's being read
62 * @param buf Buffer to place the read result in
63 * @param len Length of data to read
64 * @param offset Offset to start reading from
65 *
66 * @return Number of bytes read, or in case of an error - BT_GATT_ERR()
67 * with a specific ATT error code.
68 */
read_value_v4(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)69 static ssize_t read_value_v4(struct bt_conn *conn,
70 const struct bt_gatt_attr *attr, void *buf,
71 uint16_t len, uint16_t offset)
72 {
73 const uint8_t *value = attr->user_data;
74
75 return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
76 sizeof(value_v4_value));
77 }
78
79 /**
80 * @brief Attribute write call back for the Value V4 attribute
81 *
82 * @param conn The connection that is requesting to write
83 * @param attr The attribute that's being written
84 * @param buf Buffer with the data to write
85 * @param len Number of bytes in the buffer
86 * @param offset Offset to start writing from
87 * @param flags Flags (BT_GATT_WRITE_*)
88 *
89 * @return Number of bytes written, or in case of an error - BT_GATT_ERR()
90 * with a specific ATT error code.
91 */
write_value_v4(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)92 static ssize_t write_value_v4(struct bt_conn *conn,
93 const struct bt_gatt_attr *attr, const void *buf,
94 uint16_t len, uint16_t offset, uint8_t flags)
95 {
96 uint8_t *value = attr->user_data;
97
98 if (offset >= sizeof(value_v4_value)) {
99 return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
100 }
101 if (offset + len > sizeof(value_v4_value)) {
102 return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
103 }
104
105 memcpy(value + offset, buf, len);
106
107 return len;
108 }
109
110 /**
111 * @brief Attribute read call back for the Value V4 attribute
112 *
113 * @param conn The connection that is requesting to read
114 * @param attr The attribute that's being read
115 * @param buf Buffer to place the read result in
116 * @param len Length of data to read
117 * @param offset Offset to start reading from
118 *
119 * @return Number of bytes read, or in case of an error - BT_GATT_ERR()
120 * with a specific ATT error code.
121 */
read_value_v4_1(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)122 static ssize_t read_value_v4_1(struct bt_conn *conn,
123 const struct bt_gatt_attr *attr, void *buf,
124 uint16_t len, uint16_t offset)
125 {
126 const uint8_t *value = attr->user_data;
127
128 return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
129 sizeof(value_v4_1_value));
130 }
131
132 /**
133 * @brief Attribute write call back for the Value V4 attribute
134 *
135 * @param conn The connection that is requesting to write
136 * @param attr The attribute that's being written
137 * @param buf Buffer with the data to write
138 * @param len Number of bytes in the buffer
139 * @param offset Offset to start writing from
140 * @param flags Flags (BT_GATT_WRITE_*)
141 *
142 * @return Number of bytes written, or in case of an error - BT_GATT_ERR()
143 * with a specific ATT error code.
144 */
write_value_v4_1(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)145 static ssize_t write_value_v4_1(struct bt_conn *conn,
146 const struct bt_gatt_attr *attr,
147 const void *buf, uint16_t len, uint16_t offset,
148 uint8_t flags)
149 {
150 uint8_t *value = attr->user_data;
151
152 if (offset >= sizeof(value_v4_1_value)) {
153 return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
154 }
155 if (offset + len > sizeof(value_v4_1_value)) {
156 return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
157 }
158
159 memcpy(value + offset, buf, len);
160
161 return len;
162 }
163
164 /**
165 * @brief Attribute read call back for the Server Characteristic Configuration
166 * attribute
167 *
168 * @param conn The connection that is requesting to read
169 * @param attr The attribute that's being read
170 * @param buf Buffer to place the read result in
171 * @param len Length of data to read
172 * @param offset Offset to start reading from
173 *
174 * @return Number of bytes read, or in case of an error - BT_GATT_ERR()
175 * with a specific ATT error code.
176 */
read_server_cha_con(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)177 static ssize_t read_server_cha_con(struct bt_conn *conn,
178 const struct bt_gatt_attr *attr, void *buf,
179 uint16_t len, uint16_t offset)
180 {
181 const uint16_t *value = attr->user_data;
182 uint16_t server_cha_con_conv = sys_cpu_to_le16(*value);
183
184 return bt_gatt_attr_read(conn, attr, buf, len, offset,
185 &server_cha_con_conv,
186 sizeof(server_cha_con_conv));
187 }
188
189 /**
190 * @brief Attribute write call back for the Server Characteristic Configuration
191 * attribute
192 *
193 * @param conn The connection that is requesting to write
194 * @param attr The attribute that's being written
195 * @param buf Buffer with the data to write
196 * @param len Number of bytes in the buffer
197 * @param offset Offset to start writing from
198 * @param flags Flags (BT_GATT_WRITE_*)
199 *
200 * @return Number of bytes written, or in case of an error - BT_GATT_ERR()
201 * with a specific ATT error code.
202 */
write_server_cha_con(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)203 static ssize_t write_server_cha_con(struct bt_conn *conn,
204 const struct bt_gatt_attr *attr,
205 const void *buf, uint16_t len, uint16_t offset,
206 uint8_t flags)
207 {
208 uint16_t *value = attr->user_data;
209 uint16_t server_cha_con_conv = sys_cpu_to_le16(*value);
210
211 if (offset >= sizeof(server_cha_con_value)) {
212 return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
213 }
214 if (offset + len > sizeof(server_cha_con_value)) {
215 return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
216 }
217
218 memcpy((uint8_t *)&server_cha_con_conv + offset, buf, len);
219
220 *value = sys_le16_to_cpu(server_cha_con_conv);
221
222 return len;
223 }
224
225 /**
226 * @brief Attribute read call back for the Value V4 attribute
227 *
228 * @param conn The connection that is requesting to read
229 * @param attr The attribute that's being read
230 * @param buf Buffer to place the read result in
231 * @param len Length of data to read
232 * @param offset Offset to start reading from
233 *
234 * @return Number of bytes read, or in case of an error - BT_GATT_ERR()
235 * with a specific ATT error code.
236 */
read_value_v4_3(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)237 static ssize_t read_value_v4_3(struct bt_conn *conn,
238 const struct bt_gatt_attr *attr, void *buf,
239 uint16_t len, uint16_t offset)
240 {
241 const uint8_t *value = attr->user_data;
242
243 return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
244 sizeof(value_v4_3_value));
245 }
246
247 /**
248 * @brief Attribute read call back for the Long descriptor V2D1 attribute
249 *
250 * @param conn The connection that is requesting to read
251 * @param attr The attribute that's being read
252 * @param buf Buffer to place the read result in
253 * @param len Length of data to read
254 * @param offset Offset to start reading from
255 *
256 * @return Number of bytes read, or in case of an error - BT_GATT_ERR()
257 * with a specific ATT error code.
258 */
read_long_des_v2d1_1(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)259 static ssize_t read_long_des_v2d1_1(struct bt_conn *conn,
260 const struct bt_gatt_attr *attr, void *buf,
261 uint16_t len, uint16_t offset)
262 {
263 const uint8_t *value = attr->user_data;
264
265 return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
266 sizeof(long_des_v2d1_1_value));
267 }
268
269 #define BT_GATT_CHRC_NONE 0
270
271 static struct bt_gatt_attr service_b_1_1_attrs[] = {
272 BT_GATT_H_PRIMARY_SERVICE(BT_UUID_SERVICE_B_1, 0x60),
273 BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V4,
274 BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
275 BT_GATT_PERM_READ | BT_GATT_PERM_WRITE_AUTHEN,
276 read_value_v4, write_value_v4, &value_v4_value, 0x61),
277 BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V4,
278 BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
279 BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
280 read_value_v4_1, write_value_v4_1, &value_v4_1_value, 0x63),
281 BT_GATT_H_DESCRIPTOR(BT_UUID_GATT_SCC,
282 BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
283 read_server_cha_con, write_server_cha_con,
284 &server_cha_con_value, 0x65),
285 BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V4,
286 BT_GATT_CHRC_NONE,
287 BT_GATT_PERM_NONE,
288 NULL, NULL, &value_v4_2_value, 0x66),
289 BT_GATT_H_DESCRIPTOR(BT_UUID_LONG_DES_V2D1,
290 BT_GATT_PERM_NONE,
291 NULL, NULL, &long_des_v2d1_value, 0x68),
292 BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V4,
293 BT_GATT_CHRC_READ,
294 BT_GATT_PERM_READ,
295 read_value_v4_3, NULL, &value_v4_3_value, 0x69),
296 BT_GATT_H_DESCRIPTOR(BT_UUID_LONG_DES_V2D1,
297 BT_GATT_PERM_READ,
298 read_long_des_v2d1_1, NULL, &long_des_v2d1_1_value, 0x6B)
299 };
300
301 static struct bt_gatt_service service_b_1_1_svc =
302 BT_GATT_SERVICE(service_b_1_1_attrs);
303
304 /**
305 * @brief Register the Service B.1 and all its Characteristics...
306 */
service_b_1_1_init(void)307 void service_b_1_1_init(void)
308 {
309 bt_gatt_service_register(&service_b_1_1_svc);
310 }
311
312 /**
313 * @brief Un-Register the Service B.1 and all its Characteristics...
314 */
service_b_1_1_remove(void)315 void service_b_1_1_remove(void)
316 {
317 bt_gatt_service_unregister(&service_b_1_1_svc);
318 }
319