1 /**
2  * Copyright (c) 2019 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 /**
7  * @brief Service F
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 F
21  */
22 #define BT_UUID_SERVICE_F               BT_UUID_DECLARE_16(0xa00f)
23 
24 /**
25  *  @brief UUID for the Value V14 Characteristic
26  */
27 #define BT_UUID_VALUE_V14               BT_UUID_DECLARE_16(0xb00e)
28 
29 /**
30  *  @brief UUID for the Value V15 Characteristic
31  */
32 #define BT_UUID_VALUE_V15               BT_UUID_DECLARE_16(0xb00f)
33 
34 /**
35  *  @brief UUID for the Value V6 Characteristic
36  */
37 #define BT_UUID_VALUE_V6                BT_UUID_DECLARE_16(0xb006)
38 
39 /**
40  *  @brief UUID for the Value V7 Characteristic
41  */
42 #define BT_UUID_VALUE_V7                BT_UUID_DECLARE_16(0xb007)
43 
44 /**
45  *  @brief UUID for the Value V16 Characteristic
46  */
47 #define BT_UUID_VALUE_V16               BT_UUID_DECLARE_16(0xb010)
48 
49 /**
50  *  @brief UUID for the Aggregate Format Characteristic
51  */
52 #define BT_UUID_AGG_FORMAT              BT_UUID_DECLARE_16(0x2905)
53 
54 /**
55  *  @brief UUID for the Value V17 Characteristic
56  */
57 #define BT_UUID_VALUE_V17               BT_UUID_DECLARE_16(0xb011)
58 
59 static uint8_t   value_v14_value[] = {
60 	      'L', 'e', 'n', 'g', 't', 'h', ' ', 'i', 's', ' ', '\0'
61 };
62 static const struct bt_gatt_cpf cha_format_value = {
63 	     0x19, 0x00, 0x3000, 0x01, 0x0000
64 };
65 static uint8_t   value_v15_value = 0x65;
66 static const struct bt_gatt_cpf cha_format_1_value = {
67 	     0x04, 0x00, 0x2701, 0x01, 0x0001
68 };
69 static uint16_t  value_v6_value = 0x1234;
70 static const struct bt_gatt_cpf cha_format_2_value = {
71 	     0x06, 0x00, 0x2710, 0x01, 0x0002
72 };
73 static uint32_t  value_v7_value = 0x01020304;
74 static const struct bt_gatt_cpf cha_format_3_value = {
75 	     0x08, 0x00, 0x2717, 0x01, 0x0003
76 };
77 static struct __packed value_v16_t {
78 	uint8_t  field_a;
79 	uint16_t field_b;
80 	uint32_t field_c;
81 } value_v16_value = { 0x65, 0x1234, 0x01020304 };
82 static struct __packed agg_format_t {
83 	uint16_t field_a;
84 	uint16_t field_b;
85 	uint16_t field_c;
86 } agg_format_value = { 0x00A6, 0x00A9, 0x00AC };
87 static uint8_t   value_v17_value = 0x12;
88 
89 /**
90  * @brief Attribute read call back for the Value V14 string attribute
91  *
92  * @param conn   The connection that is requesting to read
93  * @param attr   The attribute that's being read
94  * @param buf    Buffer to place the read result in
95  * @param len    Length of data to read
96  * @param offset Offset to start reading from
97  *
98  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
99  *               with a specific ATT error code.
100  */
read_str_value(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)101 static ssize_t read_str_value(struct bt_conn *conn,
102 			      const struct bt_gatt_attr *attr, void *buf,
103 			      uint16_t len, uint16_t offset)
104 {
105 	const char *value = attr->user_data;
106 
107 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
108 				 strlen(value));
109 }
110 
111 /**
112  * @brief Attribute read call back for the Value V15 attribute
113  *
114  * @param conn   The connection that is requesting to read
115  * @param attr   The attribute that's being read
116  * @param buf    Buffer to place the read result in
117  * @param len    Length of data to read
118  * @param offset Offset to start reading from
119  *
120  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
121  *               with a specific ATT error code.
122  */
read_value_v15(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)123 static ssize_t read_value_v15(struct bt_conn *conn,
124 			      const struct bt_gatt_attr *attr, void *buf,
125 			      uint16_t len, uint16_t offset)
126 {
127 	const uint8_t *value = attr->user_data;
128 
129 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
130 				 sizeof(value_v15_value));
131 }
132 
133 /**
134  * @brief Attribute write call back for the Value V15 attribute
135  *
136  * @param conn   The connection that is requesting to write
137  * @param attr   The attribute that's being written
138  * @param buf    Buffer with the data to write
139  * @param len    Number of bytes in the buffer
140  * @param offset Offset to start writing from
141  * @param flags  Flags (BT_GATT_WRITE_*)
142  *
143  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
144  *               with a specific ATT error code.
145  */
write_value_v15(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)146 static ssize_t write_value_v15(struct bt_conn *conn,
147 			       const struct bt_gatt_attr *attr, const void *buf,
148 			       uint16_t len, uint16_t offset, uint8_t flags)
149 {
150 	uint8_t *value = attr->user_data;
151 
152 	if (offset >= sizeof(value_v15_value)) {
153 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
154 	}
155 	if (offset + len > sizeof(value_v15_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 Value V6 attribute
166  *
167  * @param conn   The connection that is requesting to read
168  * @param attr   The attribute that's being read
169  * @param buf    Buffer to place the read result in
170  * @param len    Length of data to read
171  * @param offset Offset to start reading from
172  *
173  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
174  *               with a specific ATT error code.
175  */
read_value_v6(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)176 static ssize_t read_value_v6(struct bt_conn *conn,
177 			     const struct bt_gatt_attr *attr, void *buf,
178 			     uint16_t len, uint16_t offset)
179 {
180 	const uint16_t *value = attr->user_data;
181 	uint16_t value_v6_conv = sys_cpu_to_le16(*value);
182 
183 	return bt_gatt_attr_read(conn, attr, buf, len, offset, &value_v6_conv,
184 				 sizeof(value_v6_conv));
185 }
186 
187 /**
188  * @brief Attribute write call back for the Value V6 attribute
189  *
190  * @param conn   The connection that is requesting to write
191  * @param attr   The attribute that's being written
192  * @param buf    Buffer with the data to write
193  * @param len    Number of bytes in the buffer
194  * @param offset Offset to start writing from
195  * @param flags  Flags (BT_GATT_WRITE_*)
196  *
197  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
198  *               with a specific ATT error code.
199  */
write_value_v6(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)200 static ssize_t write_value_v6(struct bt_conn *conn,
201 			      const struct bt_gatt_attr *attr, const void *buf,
202 			      uint16_t len, uint16_t offset, uint8_t flags)
203 {
204 	uint16_t *value = attr->user_data;
205 	uint16_t value_v6_conv = sys_cpu_to_le16(*value);
206 
207 	if (offset >= sizeof(value_v6_value)) {
208 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
209 	}
210 	if (offset + len > sizeof(value_v6_value)) {
211 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
212 	}
213 
214 	memcpy((uint8_t *)&value_v6_conv + offset, buf, len);
215 
216 	*value = sys_le16_to_cpu(value_v6_conv);
217 
218 	return len;
219 }
220 
221 /**
222  * @brief Attribute read call back for the Value V7 attribute
223  *
224  * @param conn   The connection that is requesting to read
225  * @param attr   The attribute that's being read
226  * @param buf    Buffer to place the read result in
227  * @param len    Length of data to read
228  * @param offset Offset to start reading from
229  *
230  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
231  *               with a specific ATT error code.
232  */
read_value_v7(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)233 static ssize_t read_value_v7(struct bt_conn *conn,
234 			     const struct bt_gatt_attr *attr, void *buf,
235 			     uint16_t len, uint16_t offset)
236 {
237 	const uint32_t *value = attr->user_data;
238 	uint32_t value_v7_conv = sys_cpu_to_le32(*value);
239 
240 	return bt_gatt_attr_read(conn, attr, buf, len, offset, &value_v7_conv,
241 				 sizeof(value_v7_conv));
242 }
243 
244 /**
245  * @brief Attribute write call back for the Value V7 attribute
246  *
247  * @param conn   The connection that is requesting to write
248  * @param attr   The attribute that's being written
249  * @param buf    Buffer with the data to write
250  * @param len    Number of bytes in the buffer
251  * @param offset Offset to start writing from
252  * @param flags  Flags (BT_GATT_WRITE_*)
253  *
254  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
255  *               with a specific ATT error code.
256  */
write_value_v7(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)257 static ssize_t write_value_v7(struct bt_conn *conn,
258 			      const struct bt_gatt_attr *attr, const void *buf,
259 			      uint16_t len, uint16_t offset, uint8_t flags)
260 {
261 	uint32_t *value = attr->user_data;
262 	uint32_t value_v7_conv = sys_cpu_to_le32(*value);
263 
264 	if (offset >= sizeof(value_v7_value)) {
265 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
266 	}
267 	if (offset + len > sizeof(value_v7_value)) {
268 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
269 	}
270 
271 	memcpy((uint8_t *)&value_v7_conv + offset, buf, len);
272 
273 	*value = sys_le32_to_cpu(value_v7_conv);
274 
275 	return len;
276 }
277 
278 /**
279  * @brief Attribute read call back for the Value V16 attribute
280  *
281  * @param conn   The connection that is requesting to read
282  * @param attr   The attribute that's being read
283  * @param buf    Buffer to place the read result in
284  * @param len    Length of data to read
285  * @param offset Offset to start reading from
286  *
287  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
288  *               with a specific ATT error code.
289  */
read_value_v16(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)290 static ssize_t read_value_v16(struct bt_conn *conn,
291 			      const struct bt_gatt_attr *attr, void *buf,
292 			      uint16_t len, uint16_t offset)
293 {
294 	const struct value_v16_t *value = attr->user_data;
295 	struct value_v16_t value_v16_conv;
296 
297 	value_v16_conv.field_a = value->field_a;
298 	value_v16_conv.field_b = sys_cpu_to_le16(value->field_b);
299 	value_v16_conv.field_c = sys_cpu_to_le32(value->field_c);
300 
301 	return bt_gatt_attr_read(conn, attr, buf, len, offset, &value_v16_conv,
302 				 sizeof(value_v16_conv));
303 }
304 
305 /**
306  * @brief Attribute read call back for the Aggregate Format attribute
307  *
308  * @param conn   The connection that is requesting to read
309  * @param attr   The attribute that's being read
310  * @param buf    Buffer to place the read result in
311  * @param len    Length of data to read
312  * @param offset Offset to start reading from
313  *
314  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
315  *               with a specific ATT error code.
316  */
read_agg_format(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)317 static ssize_t read_agg_format(struct bt_conn *conn,
318 			       const struct bt_gatt_attr *attr, void *buf,
319 			       uint16_t len, uint16_t offset)
320 {
321 	const struct agg_format_t *value = attr->user_data;
322 	struct agg_format_t agg_format_conv;
323 
324 	agg_format_conv.field_a = sys_cpu_to_le16(value->field_a);
325 	agg_format_conv.field_b = sys_cpu_to_le16(value->field_b);
326 	agg_format_conv.field_c = sys_cpu_to_le16(value->field_c);
327 
328 	return bt_gatt_attr_read(conn, attr, buf, len, offset, &agg_format_conv,
329 				 sizeof(agg_format_conv));
330 }
331 
332 /**
333  * @brief Attribute read call back for the Value V17 attribute
334  *
335  * @param conn   The connection that is requesting to read
336  * @param attr   The attribute that's being read
337  * @param buf    Buffer to place the read result in
338  * @param len    Length of data to read
339  * @param offset Offset to start reading from
340  *
341  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
342  *               with a specific ATT error code.
343  */
read_value_v17(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)344 static ssize_t read_value_v17(struct bt_conn *conn,
345 			      const struct bt_gatt_attr *attr, void *buf,
346 			      uint16_t len, uint16_t offset)
347 {
348 	const uint8_t *value = attr->user_data;
349 
350 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
351 				 sizeof(value_v17_value));
352 }
353 
354 static struct bt_gatt_attr service_f_1_attrs[] = {
355 	BT_GATT_H_PRIMARY_SERVICE(BT_UUID_SERVICE_F, 0xA0),
356 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V14,
357 		BT_GATT_CHRC_READ,
358 		BT_GATT_PERM_READ,
359 		read_str_value, NULL, &value_v14_value, 0xA1),
360 	BT_GATT_H_CPF(&cha_format_value, 0xA3),
361 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V15,
362 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
363 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
364 		read_value_v15, write_value_v15, &value_v15_value, 0xA4),
365 	BT_GATT_H_CPF(&cha_format_1_value, 0xA6),
366 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V6,
367 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
368 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
369 		read_value_v6, write_value_v6, &value_v6_value, 0xA7),
370 	BT_GATT_H_CPF(&cha_format_2_value, 0xA9),
371 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V7,
372 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
373 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
374 		read_value_v7, write_value_v7, &value_v7_value, 0xAA),
375 	BT_GATT_H_CPF(&cha_format_3_value, 0xAC),
376 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V16,
377 		BT_GATT_CHRC_READ,
378 		BT_GATT_PERM_READ,
379 		read_value_v16, NULL, &value_v16_value, 0xAD),
380 	BT_GATT_H_DESCRIPTOR(BT_UUID_AGG_FORMAT,
381 		BT_GATT_PERM_READ,
382 		read_agg_format, NULL, &agg_format_value, 0xAF),
383 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V17,
384 		BT_GATT_CHRC_READ | BT_GATT_CHRC_AUTH,
385 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
386 		read_value_v17, NULL, &value_v17_value, 0xB0)
387 };
388 
389 static struct bt_gatt_service service_f_1_svc =
390 		    BT_GATT_SERVICE(service_f_1_attrs);
391 
392 /**
393  * @brief Register the Service F and all its Characteristics...
394  */
service_f_1_init(void)395 void service_f_1_init(void)
396 {
397 	bt_gatt_service_register(&service_f_1_svc);
398 }
399 
400 /**
401  * @brief Un-Register the Service F and all its Characteristics...
402  */
service_f_1_remove(void)403 void service_f_1_remove(void)
404 {
405 	bt_gatt_service_unregister(&service_f_1_svc);
406 }
407