1 /**
2  * Copyright (c) 2019 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 /**
7  * @brief Service C.2
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 C.2
21  */
22 #define BT_UUID_SERVICE_C_2             BT_UUID_DECLARE_128( \
23 		0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, \
24 		0x00, 0x00, 0x00, 0x00, 0x0c, 0xa0, 0x00, 0x00)
25 
26 /**
27  *  @brief UUID for the Value V10 Characteristic
28  */
29 #define BT_UUID_VALUE_V10               BT_UUID_DECLARE_16(0xb00a)
30 
31 /**
32  *  @brief UUID for the Value V2 Characteristic
33  */
34 #define BT_UUID_VALUE_V2                BT_UUID_DECLARE_16(0xb002)
35 
36 /**
37  *  @brief UUID for the Long descriptor V2D1 Characteristic
38  */
39 #define BT_UUID_LONG_DES_V2D1           BT_UUID_DECLARE_16(0xb012)
40 
41 /**
42  *  @brief UUID for the Long descriptor V2D2 Characteristic
43  */
44 #define BT_UUID_LONG_DES_V2D2           BT_UUID_DECLARE_16(0xb013)
45 
46 /**
47  *  @brief UUID for the Long descriptor V2D3 Characteristic
48  */
49 #define BT_UUID_LONG_DES_V2D3           BT_UUID_DECLARE_16(0xb014)
50 
51 static uint8_t   value_v10_value = 0x0A;
52 static uint8_t   value_v2_value[] = {
53 	      '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '3', '3',
54 	      '3', '3', '4', '4', '4', '4', '4', '5', '\0'
55 };
56 static uint8_t   long_des_v2d1_value[] = {
57 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
58 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11
59 };
60 static uint8_t   value_v2_1_value[] = {
61 	      '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '4', '4', '4',
62 	      '4', '4', '5', '5', '5', '5', '5', '6', '6', '\0'
63 };
64 static uint8_t   long_des_v2d2_value[] = {
65 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
66 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22
67 };
68 static uint8_t   value_v2_2_value[] = {
69 	      '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '5', '5', '5',
70 	      '5', '5', '6', '6', '6', '6', '6', '7', '7', '7', '\0'
71 };
72 static uint8_t   long_des_v2d3_value[] = {
73 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
74 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22,
75 	      0x33
76 };
77 static uint8_t   value_v2_3_value[] = {
78 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
79 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
80 	      0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
81 	      0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33
82 };
83 static uint8_t   long_des_v2d1_1_value[] = {
84 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
85 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
86 	      0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
87 	      0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33
88 };
89 static uint8_t   value_v2_4_value[] = {
90 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
91 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
92 	      0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
93 	      0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44
94 };
95 static uint8_t   long_des_v2d2_1_value[] = {
96 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
97 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
98 	      0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
99 	      0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44
100 };
101 static uint8_t   value_v2_5_value[] = {
102 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
103 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
104 	      0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
105 	      0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44,
106 	      0x55
107 };
108 static uint8_t   long_des_v2d3_1_value[] = {
109 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
110 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
111 	      0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
112 	      0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44,
113 	      0x55
114 };
115 static uint8_t   value_v2_6_value[] = {
116 	      '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '3', '3',
117 	      '3', '3', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '6',
118 	      '6', '6', '6', '6', '7', '7', '7', '7', '7', '8', '8', '8', '8',
119 	      '8', '9', '9', '9', '\0'
120 };
121 static uint8_t   long_des_v2d1_2_value[] = {
122 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
123 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
124 	      0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
125 	      0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33
126 };
127 static uint8_t   value_v2_7_value[] = {
128 	      '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '4', '4', '4',
129 	      '4', '4', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '7',
130 	      '7', '7', '7', '7', '8', '8', '8', '8', '8', '9', '9', '9', '9',
131 	      '9', '0', '0', '0', '0', '\0'
132 };
133 static uint8_t   long_des_v2d2_2_value[] = {
134 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
135 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
136 	      0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
137 	      0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44
138 };
139 static uint8_t   value_v2_8_value[] = {
140 	      '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '5', '5', '5',
141 	      '5', '5', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '8',
142 	      '8', '8', '8', '8', '9', '9', '9', '9', '9', '0', '0', '0', '0',
143 	      '0', '1', '1', '1', '1', '1', '\0'
144 };
145 static uint8_t   long_des_v2d3_2_value[] = {
146 	      0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x12,
147 	      0x34, 0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x12, 0x34,
148 	      0x56, 0x78, 0x90, 0x12, 0x34, 0x56, 0x78, 0x90, 0x11, 0x22, 0x33,
149 	      0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0x00, 0x11, 0x22, 0x33, 0x44,
150 	      0x55
151 };
152 static bool   bAuthorized;
153 
154 /**
155  * @brief Attribute read call back for the Value V10 attribute
156  *
157  * @param conn   The connection that is requesting to read
158  * @param attr   The attribute that's being read
159  * @param buf    Buffer to place the read result in
160  * @param len    Length of data to read
161  * @param offset Offset to start reading from
162  *
163  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
164  *               with a specific ATT error code.
165  */
read_value_v10(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)166 static ssize_t read_value_v10(struct bt_conn *conn,
167 			      const struct bt_gatt_attr *attr, void *buf,
168 			      uint16_t len, uint16_t offset)
169 {
170 	const uint8_t *value = attr->user_data;
171 
172 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
173 				 sizeof(value_v10_value));
174 }
175 
176 /**
177  * @brief Attribute read call back for the Value V2 string attribute
178  *
179  * @param conn   The connection that is requesting to read
180  * @param attr   The attribute that's being read
181  * @param buf    Buffer to place the read result in
182  * @param len    Length of data to read
183  * @param offset Offset to start reading from
184  *
185  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
186  *               with a specific ATT error code.
187  */
read_str_value(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)188 static ssize_t read_str_value(struct bt_conn *conn,
189 			      const struct bt_gatt_attr *attr, void *buf,
190 			      uint16_t len, uint16_t offset)
191 {
192 	const char *value = attr->user_data;
193 
194 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
195 				 strlen(value));
196 }
197 
198 /**
199  * @brief Attribute write call back for the Value V2 attribute
200  *
201  * @param conn   The connection that is requesting to write
202  * @param attr   The attribute that's being written
203  * @param buf    Buffer with the data to write
204  * @param len    Number of bytes in the buffer
205  * @param offset Offset to start writing from
206  * @param flags  Flags (BT_GATT_WRITE_*)
207  *
208  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
209  *               with a specific ATT error code.
210  */
write_value_v2(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)211 static ssize_t write_value_v2(struct bt_conn *conn,
212 			      const struct bt_gatt_attr *attr, const void *buf,
213 			      uint16_t len, uint16_t offset, uint8_t flags)
214 {
215 	char *value = attr->user_data;
216 
217 	if (offset >= sizeof(value_v2_value)) {
218 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
219 	}
220 	if (offset + len > sizeof(value_v2_value)) {
221 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
222 	}
223 
224 	memcpy(value + offset, buf, len);
225 
226 	return len;
227 }
228 
229 /**
230  * @brief Attribute read call back for the Long descriptor V2D1 attribute
231  *
232  * @param conn   The connection that is requesting to read
233  * @param attr   The attribute that's being read
234  * @param buf    Buffer to place the read result in
235  * @param len    Length of data to read
236  * @param offset Offset to start reading from
237  *
238  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
239  *               with a specific ATT error code.
240  */
read_long_des_v2d1(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)241 static ssize_t read_long_des_v2d1(struct bt_conn *conn,
242 				  const struct bt_gatt_attr *attr, void *buf,
243 				  uint16_t len, uint16_t offset)
244 {
245 	const uint8_t *value = attr->user_data;
246 
247 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
248 				 sizeof(long_des_v2d1_value));
249 }
250 
251 /**
252  * @brief Attribute write call back for the Long descriptor V2D1 attribute
253  *
254  * @param conn   The connection that is requesting to write
255  * @param attr   The attribute that's being written
256  * @param buf    Buffer with the data to write
257  * @param len    Number of bytes in the buffer
258  * @param offset Offset to start writing from
259  * @param flags  Flags (BT_GATT_WRITE_*)
260  *
261  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
262  *               with a specific ATT error code.
263  */
write_long_des_v2d1(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)264 static ssize_t write_long_des_v2d1(struct bt_conn *conn,
265 				   const struct bt_gatt_attr *attr,
266 				   const void *buf, uint16_t len, uint16_t offset,
267 				   uint8_t flags)
268 {
269 	uint8_t *value = attr->user_data;
270 
271 	if (offset >= sizeof(long_des_v2d1_value)) {
272 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
273 	}
274 	if (offset + len > sizeof(long_des_v2d1_value)) {
275 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
276 	}
277 
278 	memcpy(value + offset, buf, len);
279 
280 	return len;
281 }
282 
283 /**
284  * @brief Attribute write call back for the Value V2 attribute
285  *
286  * @param conn   The connection that is requesting to write
287  * @param attr   The attribute that's being written
288  * @param buf    Buffer with the data to write
289  * @param len    Number of bytes in the buffer
290  * @param offset Offset to start writing from
291  * @param flags  Flags (BT_GATT_WRITE_*)
292  *
293  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
294  *               with a specific ATT error code.
295  */
write_value_v2_1(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)296 static ssize_t write_value_v2_1(struct bt_conn *conn,
297 				const struct bt_gatt_attr *attr,
298 				const void *buf, uint16_t len, uint16_t offset,
299 				uint8_t flags)
300 {
301 	char *value = attr->user_data;
302 
303 	if (offset >= sizeof(value_v2_1_value)) {
304 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
305 	}
306 	if (offset + len > sizeof(value_v2_1_value)) {
307 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
308 	}
309 
310 	memcpy(value + offset, buf, len);
311 
312 	return len;
313 }
314 
315 /**
316  * @brief Attribute read call back for the Long descriptor V2D2 attribute
317  *
318  * @param conn   The connection that is requesting to read
319  * @param attr   The attribute that's being read
320  * @param buf    Buffer to place the read result in
321  * @param len    Length of data to read
322  * @param offset Offset to start reading from
323  *
324  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
325  *               with a specific ATT error code.
326  */
read_long_des_v2d2(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)327 static ssize_t read_long_des_v2d2(struct bt_conn *conn,
328 				  const struct bt_gatt_attr *attr, void *buf,
329 				  uint16_t len, uint16_t offset)
330 {
331 	const uint8_t *value = attr->user_data;
332 
333 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
334 				 sizeof(long_des_v2d2_value));
335 }
336 
337 /**
338  * @brief Attribute write call back for the Long descriptor V2D2 attribute
339  *
340  * @param conn   The connection that is requesting to write
341  * @param attr   The attribute that's being written
342  * @param buf    Buffer with the data to write
343  * @param len    Number of bytes in the buffer
344  * @param offset Offset to start writing from
345  * @param flags  Flags (BT_GATT_WRITE_*)
346  *
347  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
348  *               with a specific ATT error code.
349  */
write_long_des_v2d2(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)350 static ssize_t write_long_des_v2d2(struct bt_conn *conn,
351 				   const struct bt_gatt_attr *attr,
352 				   const void *buf, uint16_t len, uint16_t offset,
353 				   uint8_t flags)
354 {
355 	uint8_t *value = attr->user_data;
356 
357 	if (offset >= sizeof(long_des_v2d2_value)) {
358 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
359 	}
360 	if (offset + len > sizeof(long_des_v2d2_value)) {
361 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
362 	}
363 
364 	memcpy(value + offset, buf, len);
365 
366 	return len;
367 }
368 
369 /**
370  * @brief Attribute write call back for the Value V2 attribute
371  *
372  * @param conn   The connection that is requesting to write
373  * @param attr   The attribute that's being written
374  * @param buf    Buffer with the data to write
375  * @param len    Number of bytes in the buffer
376  * @param offset Offset to start writing from
377  * @param flags  Flags (BT_GATT_WRITE_*)
378  *
379  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
380  *               with a specific ATT error code.
381  */
write_value_v2_2(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)382 static ssize_t write_value_v2_2(struct bt_conn *conn,
383 				const struct bt_gatt_attr *attr,
384 				const void *buf, uint16_t len, uint16_t offset,
385 				uint8_t flags)
386 {
387 	char *value = attr->user_data;
388 
389 	if (offset >= sizeof(value_v2_2_value)) {
390 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
391 	}
392 	if (offset + len > sizeof(value_v2_2_value)) {
393 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
394 	}
395 
396 	memcpy(value + offset, buf, len);
397 
398 	return len;
399 }
400 
401 /**
402  * @brief Attribute read call back for the Long descriptor V2D3 attribute
403  *
404  * @param conn   The connection that is requesting to read
405  * @param attr   The attribute that's being read
406  * @param buf    Buffer to place the read result in
407  * @param len    Length of data to read
408  * @param offset Offset to start reading from
409  *
410  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
411  *               with a specific ATT error code.
412  */
read_long_des_v2d3(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)413 static ssize_t read_long_des_v2d3(struct bt_conn *conn,
414 				  const struct bt_gatt_attr *attr, void *buf,
415 				  uint16_t len, uint16_t offset)
416 {
417 	const uint8_t *value = attr->user_data;
418 
419 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
420 				 sizeof(long_des_v2d3_value));
421 }
422 
423 /**
424  * @brief Attribute write call back for the Long descriptor V2D3 attribute
425  *
426  * @param conn   The connection that is requesting to write
427  * @param attr   The attribute that's being written
428  * @param buf    Buffer with the data to write
429  * @param len    Number of bytes in the buffer
430  * @param offset Offset to start writing from
431  * @param flags  Flags (BT_GATT_WRITE_*)
432  *
433  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
434  *               with a specific ATT error code.
435  */
write_long_des_v2d3(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)436 static ssize_t write_long_des_v2d3(struct bt_conn *conn,
437 				   const struct bt_gatt_attr *attr,
438 				   const void *buf, uint16_t len, uint16_t offset,
439 				   uint8_t flags)
440 {
441 	uint8_t *value = attr->user_data;
442 
443 	if (offset >= sizeof(long_des_v2d3_value)) {
444 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
445 	}
446 	if (offset + len > sizeof(long_des_v2d3_value)) {
447 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
448 	}
449 
450 	memcpy(value + offset, buf, len);
451 
452 	return len;
453 }
454 
455 /**
456  * @brief Attribute read call back for the Value V2 attribute
457  *
458  * @param conn   The connection that is requesting to read
459  * @param attr   The attribute that's being read
460  * @param buf    Buffer to place the read result in
461  * @param len    Length of data to read
462  * @param offset Offset to start reading from
463  *
464  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
465  *               with a specific ATT error code.
466  */
read_value_v2_3(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)467 static ssize_t read_value_v2_3(struct bt_conn *conn,
468 			       const struct bt_gatt_attr *attr, void *buf,
469 			       uint16_t len, uint16_t offset)
470 {
471 	const uint8_t *value = attr->user_data;
472 
473 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
474 				 sizeof(value_v2_3_value));
475 }
476 
477 /**
478  * @brief Attribute write call back for the Value V2 attribute
479  *
480  * @param conn   The connection that is requesting to write
481  * @param attr   The attribute that's being written
482  * @param buf    Buffer with the data to write
483  * @param len    Number of bytes in the buffer
484  * @param offset Offset to start writing from
485  * @param flags  Flags (BT_GATT_WRITE_*)
486  *
487  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
488  *               with a specific ATT error code.
489  */
write_value_v2_3(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)490 static ssize_t write_value_v2_3(struct bt_conn *conn,
491 				const struct bt_gatt_attr *attr,
492 				const void *buf, uint16_t len, uint16_t offset,
493 				uint8_t flags)
494 {
495 	uint8_t *value = attr->user_data;
496 
497 	if (offset >= sizeof(value_v2_3_value)) {
498 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
499 	}
500 	if (offset + len > sizeof(value_v2_3_value)) {
501 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
502 	}
503 
504 	memcpy(value + offset, buf, len);
505 
506 	return len;
507 }
508 
509 /**
510  * @brief Attribute read call back for the Long descriptor V2D1 attribute
511  *
512  * @param conn   The connection that is requesting to read
513  * @param attr   The attribute that's being read
514  * @param buf    Buffer to place the read result in
515  * @param len    Length of data to read
516  * @param offset Offset to start reading from
517  *
518  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
519  *               with a specific ATT error code.
520  */
read_long_des_v2d1_1(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)521 static ssize_t read_long_des_v2d1_1(struct bt_conn *conn,
522 				    const struct bt_gatt_attr *attr, void *buf,
523 				    uint16_t len, uint16_t offset)
524 {
525 	const uint8_t *value = attr->user_data;
526 
527 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
528 				 sizeof(long_des_v2d1_1_value));
529 }
530 
531 /**
532  * @brief Attribute write call back for the Long descriptor V2D1 attribute
533  *
534  * @param conn   The connection that is requesting to write
535  * @param attr   The attribute that's being written
536  * @param buf    Buffer with the data to write
537  * @param len    Number of bytes in the buffer
538  * @param offset Offset to start writing from
539  * @param flags  Flags (BT_GATT_WRITE_*)
540  *
541  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
542  *               with a specific ATT error code.
543  */
write_long_des_v2d1_1(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)544 static ssize_t write_long_des_v2d1_1(struct bt_conn *conn,
545 				     const struct bt_gatt_attr *attr,
546 				     const void *buf, uint16_t len, uint16_t offset,
547 				     uint8_t flags)
548 {
549 	uint8_t *value = attr->user_data;
550 
551 	if (offset >= sizeof(long_des_v2d1_1_value)) {
552 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
553 	}
554 	if (offset + len > sizeof(long_des_v2d1_1_value)) {
555 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
556 	}
557 
558 	memcpy(value + offset, buf, len);
559 
560 	return len;
561 }
562 
563 /**
564  * @brief Attribute read call back for the Value V2 attribute
565  *
566  * @param conn   The connection that is requesting to read
567  * @param attr   The attribute that's being read
568  * @param buf    Buffer to place the read result in
569  * @param len    Length of data to read
570  * @param offset Offset to start reading from
571  *
572  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
573  *               with a specific ATT error code.
574  */
read_value_v2_4(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)575 static ssize_t read_value_v2_4(struct bt_conn *conn,
576 			       const struct bt_gatt_attr *attr, void *buf,
577 			       uint16_t len, uint16_t offset)
578 {
579 	const uint8_t *value = attr->user_data;
580 
581 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
582 				 sizeof(value_v2_4_value));
583 }
584 
585 /**
586  * @brief Attribute write call back for the Value V2 attribute
587  *
588  * @param conn   The connection that is requesting to write
589  * @param attr   The attribute that's being written
590  * @param buf    Buffer with the data to write
591  * @param len    Number of bytes in the buffer
592  * @param offset Offset to start writing from
593  * @param flags  Flags (BT_GATT_WRITE_*)
594  *
595  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
596  *               with a specific ATT error code.
597  */
write_value_v2_4(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)598 static ssize_t write_value_v2_4(struct bt_conn *conn,
599 				const struct bt_gatt_attr *attr,
600 				const void *buf, uint16_t len, uint16_t offset,
601 				uint8_t flags)
602 {
603 	uint8_t *value = attr->user_data;
604 
605 	if (offset >= sizeof(value_v2_4_value)) {
606 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
607 	}
608 	if (offset + len > sizeof(value_v2_4_value)) {
609 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
610 	}
611 
612 	memcpy(value + offset, buf, len);
613 
614 	return len;
615 }
616 
617 /**
618  * @brief Attribute read call back for the Long descriptor V2D2 attribute
619  *
620  * @param conn   The connection that is requesting to read
621  * @param attr   The attribute that's being read
622  * @param buf    Buffer to place the read result in
623  * @param len    Length of data to read
624  * @param offset Offset to start reading from
625  *
626  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
627  *               with a specific ATT error code.
628  */
read_long_des_v2d2_1(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)629 static ssize_t read_long_des_v2d2_1(struct bt_conn *conn,
630 				    const struct bt_gatt_attr *attr, void *buf,
631 				    uint16_t len, uint16_t offset)
632 {
633 	const uint8_t *value = attr->user_data;
634 
635 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
636 				 sizeof(long_des_v2d2_1_value));
637 }
638 
639 /**
640  * @brief Attribute write call back for the Long descriptor V2D2 attribute
641  *
642  * @param conn   The connection that is requesting to write
643  * @param attr   The attribute that's being written
644  * @param buf    Buffer with the data to write
645  * @param len    Number of bytes in the buffer
646  * @param offset Offset to start writing from
647  * @param flags  Flags (BT_GATT_WRITE_*)
648  *
649  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
650  *               with a specific ATT error code.
651  */
write_long_des_v2d2_1(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)652 static ssize_t write_long_des_v2d2_1(struct bt_conn *conn,
653 				     const struct bt_gatt_attr *attr,
654 				     const void *buf, uint16_t len, uint16_t offset,
655 				     uint8_t flags)
656 {
657 	uint8_t *value = attr->user_data;
658 
659 	if (offset >= sizeof(long_des_v2d2_1_value)) {
660 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
661 	}
662 	if (offset + len > sizeof(long_des_v2d2_1_value)) {
663 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
664 	}
665 
666 	memcpy(value + offset, buf, len);
667 
668 	return len;
669 }
670 
671 /**
672  * @brief Attribute read call back for the Value V2 attribute
673  *
674  * @param conn   The connection that is requesting to read
675  * @param attr   The attribute that's being read
676  * @param buf    Buffer to place the read result in
677  * @param len    Length of data to read
678  * @param offset Offset to start reading from
679  *
680  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
681  *               with a specific ATT error code.
682  */
read_value_v2_5(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)683 static ssize_t read_value_v2_5(struct bt_conn *conn,
684 			       const struct bt_gatt_attr *attr, void *buf,
685 			       uint16_t len, uint16_t offset)
686 {
687 	const uint8_t *value = attr->user_data;
688 
689 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
690 				 sizeof(value_v2_5_value));
691 }
692 
693 /**
694  * @brief Attribute write call back for the Value V2 attribute
695  *
696  * @param conn   The connection that is requesting to write
697  * @param attr   The attribute that's being written
698  * @param buf    Buffer with the data to write
699  * @param len    Number of bytes in the buffer
700  * @param offset Offset to start writing from
701  * @param flags  Flags (BT_GATT_WRITE_*)
702  *
703  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
704  *               with a specific ATT error code.
705  */
write_value_v2_5(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)706 static ssize_t write_value_v2_5(struct bt_conn *conn,
707 				const struct bt_gatt_attr *attr,
708 				const void *buf, uint16_t len, uint16_t offset,
709 				uint8_t flags)
710 {
711 	uint8_t *value = attr->user_data;
712 
713 	if (offset >= sizeof(value_v2_5_value)) {
714 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
715 	}
716 	if (offset + len > sizeof(value_v2_5_value)) {
717 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
718 	}
719 
720 	memcpy(value + offset, buf, len);
721 
722 	return len;
723 }
724 
725 /**
726  * @brief Attribute read call back for the Long descriptor V2D3 attribute
727  *
728  * @param conn   The connection that is requesting to read
729  * @param attr   The attribute that's being read
730  * @param buf    Buffer to place the read result in
731  * @param len    Length of data to read
732  * @param offset Offset to start reading from
733  *
734  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
735  *               with a specific ATT error code.
736  */
read_long_des_v2d3_1(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)737 static ssize_t read_long_des_v2d3_1(struct bt_conn *conn,
738 				    const struct bt_gatt_attr *attr, void *buf,
739 				    uint16_t len, uint16_t offset)
740 {
741 	const uint8_t *value = attr->user_data;
742 
743 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
744 				 sizeof(long_des_v2d3_1_value));
745 }
746 
747 /**
748  * @brief Attribute write call back for the Long descriptor V2D3 attribute
749  *
750  * @param conn   The connection that is requesting to write
751  * @param attr   The attribute that's being written
752  * @param buf    Buffer with the data to write
753  * @param len    Number of bytes in the buffer
754  * @param offset Offset to start writing from
755  * @param flags  Flags (BT_GATT_WRITE_*)
756  *
757  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
758  *               with a specific ATT error code.
759  */
write_long_des_v2d3_1(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)760 static ssize_t write_long_des_v2d3_1(struct bt_conn *conn,
761 				     const struct bt_gatt_attr *attr,
762 				     const void *buf, uint16_t len, uint16_t offset,
763 				     uint8_t flags)
764 {
765 	uint8_t *value = attr->user_data;
766 
767 	if (offset >= sizeof(long_des_v2d3_1_value)) {
768 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
769 	}
770 	if (offset + len > sizeof(long_des_v2d3_1_value)) {
771 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
772 	}
773 
774 	memcpy(value + offset, buf, len);
775 
776 	return len;
777 }
778 
779 /**
780  * @brief Attribute write call back for the Value V2 attribute
781  *
782  * @param conn   The connection that is requesting to write
783  * @param attr   The attribute that's being written
784  * @param buf    Buffer with the data to write
785  * @param len    Number of bytes in the buffer
786  * @param offset Offset to start writing from
787  * @param flags  Flags (BT_GATT_WRITE_*)
788  *
789  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
790  *               with a specific ATT error code.
791  */
write_value_v2_6(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)792 static ssize_t write_value_v2_6(struct bt_conn *conn,
793 				const struct bt_gatt_attr *attr,
794 				const void *buf, uint16_t len, uint16_t offset,
795 				uint8_t flags)
796 {
797 	char *value = attr->user_data;
798 
799 	if (offset >= sizeof(value_v2_6_value)) {
800 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
801 	}
802 	if (offset + len > sizeof(value_v2_6_value)) {
803 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
804 	}
805 
806 	memcpy(value + offset, buf, len);
807 
808 	return len;
809 }
810 
811 /**
812  * @brief Attribute read call back for the Long descriptor V2D1 attribute
813  *
814  * @param conn   The connection that is requesting to read
815  * @param attr   The attribute that's being read
816  * @param buf    Buffer to place the read result in
817  * @param len    Length of data to read
818  * @param offset Offset to start reading from
819  *
820  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
821  *               with a specific ATT error code.
822  */
read_long_des_v2d1_2(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)823 static ssize_t read_long_des_v2d1_2(struct bt_conn *conn,
824 				    const struct bt_gatt_attr *attr, void *buf,
825 				    uint16_t len, uint16_t offset)
826 {
827 	const uint8_t *value = attr->user_data;
828 
829 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
830 				 sizeof(long_des_v2d1_2_value));
831 }
832 
833 /**
834  * @brief Attribute write call back for the Long descriptor V2D1 attribute
835  *
836  * @param conn   The connection that is requesting to write
837  * @param attr   The attribute that's being written
838  * @param buf    Buffer with the data to write
839  * @param len    Number of bytes in the buffer
840  * @param offset Offset to start writing from
841  * @param flags  Flags (BT_GATT_WRITE_*)
842  *
843  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
844  *               with a specific ATT error code.
845  */
write_long_des_v2d1_2(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)846 static ssize_t write_long_des_v2d1_2(struct bt_conn *conn,
847 				     const struct bt_gatt_attr *attr,
848 				     const void *buf, uint16_t len, uint16_t offset,
849 				     uint8_t flags)
850 {
851 	uint8_t *value = attr->user_data;
852 
853 	if (offset >= sizeof(long_des_v2d1_2_value)) {
854 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
855 	}
856 	if (offset + len > sizeof(long_des_v2d1_2_value)) {
857 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
858 	}
859 
860 	memcpy(value + offset, buf, len);
861 
862 	return len;
863 }
864 
865 /**
866  * @brief Attribute read call back for the Value V2 string attribute
867  *
868  * @param conn   The connection that is requesting to read
869  * @param attr   The attribute that's being read
870  * @param buf    Buffer to place the read result in
871  * @param len    Length of data to read
872  * @param offset Offset to start reading from
873  *
874  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
875  *               with a specific ATT error code.
876  */
read_str_auth_value(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)877 static ssize_t read_str_auth_value(struct bt_conn *conn,
878 				   const struct bt_gatt_attr *attr, void *buf,
879 				   uint16_t len, uint16_t offset)
880 {
881 	const char *value = attr->user_data;
882 
883 	if (!bAuthorized) {
884 		return BT_GATT_ERR(BT_ATT_ERR_AUTHORIZATION);
885 	}
886 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
887 				 strlen(value));
888 }
889 
890 /**
891  * @brief Attribute write call back for the Value V2 attribute
892  *
893  * @param conn   The connection that is requesting to write
894  * @param attr   The attribute that's being written
895  * @param buf    Buffer with the data to write
896  * @param len    Number of bytes in the buffer
897  * @param offset Offset to start writing from
898  * @param flags  Flags (BT_GATT_WRITE_*)
899  *
900  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
901  *               with a specific ATT error code.
902  */
write_value_v2_7(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)903 static ssize_t write_value_v2_7(struct bt_conn *conn,
904 				const struct bt_gatt_attr *attr,
905 				const void *buf, uint16_t len, uint16_t offset,
906 				uint8_t flags)
907 {
908 	char *value = attr->user_data;
909 
910 	if (offset >= sizeof(value_v2_7_value)) {
911 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
912 	}
913 	if (offset + len > sizeof(value_v2_7_value)) {
914 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
915 	}
916 	if (!bAuthorized) {
917 		return BT_GATT_ERR(BT_ATT_ERR_AUTHORIZATION);
918 	}
919 
920 	memcpy(value + offset, buf, len);
921 
922 	return len;
923 }
924 
925 /**
926  * @brief Attribute read call back for the Long descriptor V2D2 attribute
927  *
928  * @param conn   The connection that is requesting to read
929  * @param attr   The attribute that's being read
930  * @param buf    Buffer to place the read result in
931  * @param len    Length of data to read
932  * @param offset Offset to start reading from
933  *
934  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
935  *               with a specific ATT error code.
936  */
read_long_des_v2d2_2(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)937 static ssize_t read_long_des_v2d2_2(struct bt_conn *conn,
938 				    const struct bt_gatt_attr *attr, void *buf,
939 				    uint16_t len, uint16_t offset)
940 {
941 	const uint8_t *value = attr->user_data;
942 
943 	if (!bAuthorized) {
944 		return BT_GATT_ERR(BT_ATT_ERR_AUTHORIZATION);
945 	}
946 
947 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
948 				 sizeof(long_des_v2d2_2_value));
949 }
950 
951 /**
952  * @brief Attribute write call back for the Long descriptor V2D2 attribute
953  *
954  * @param conn   The connection that is requesting to write
955  * @param attr   The attribute that's being written
956  * @param buf    Buffer with the data to write
957  * @param len    Number of bytes in the buffer
958  * @param offset Offset to start writing from
959  * @param flags  Flags (BT_GATT_WRITE_*)
960  *
961  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
962  *               with a specific ATT error code.
963  */
write_long_des_v2d2_2(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)964 static ssize_t write_long_des_v2d2_2(struct bt_conn *conn,
965 				     const struct bt_gatt_attr *attr,
966 				     const void *buf, uint16_t len, uint16_t offset,
967 				     uint8_t flags)
968 {
969 	uint8_t *value = attr->user_data;
970 
971 	if (offset >= sizeof(long_des_v2d2_2_value)) {
972 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
973 	}
974 	if (offset + len > sizeof(long_des_v2d2_2_value)) {
975 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
976 	}
977 	if (!bAuthorized) {
978 		return BT_GATT_ERR(BT_ATT_ERR_AUTHORIZATION);
979 	}
980 
981 	memcpy(value + offset, buf, len);
982 
983 	return len;
984 }
985 
986 /**
987  * @brief Attribute write call back for the Value V2 attribute
988  *
989  * @param conn   The connection that is requesting to write
990  * @param attr   The attribute that's being written
991  * @param buf    Buffer with the data to write
992  * @param len    Number of bytes in the buffer
993  * @param offset Offset to start writing from
994  * @param flags  Flags (BT_GATT_WRITE_*)
995  *
996  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
997  *               with a specific ATT error code.
998  */
write_value_v2_8(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)999 static ssize_t write_value_v2_8(struct bt_conn *conn,
1000 				const struct bt_gatt_attr *attr,
1001 				const void *buf, uint16_t len, uint16_t offset,
1002 				uint8_t flags)
1003 {
1004 	char *value = attr->user_data;
1005 
1006 	if (offset >= sizeof(value_v2_8_value)) {
1007 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
1008 	}
1009 	if (offset + len > sizeof(value_v2_8_value)) {
1010 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
1011 	}
1012 
1013 	memcpy(value + offset, buf, len);
1014 
1015 	return len;
1016 }
1017 
1018 /**
1019  * @brief Attribute read call back for the Long descriptor V2D3 attribute
1020  *
1021  * @param conn   The connection that is requesting to read
1022  * @param attr   The attribute that's being read
1023  * @param buf    Buffer to place the read result in
1024  * @param len    Length of data to read
1025  * @param offset Offset to start reading from
1026  *
1027  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
1028  *               with a specific ATT error code.
1029  */
read_long_des_v2d3_2(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)1030 static ssize_t read_long_des_v2d3_2(struct bt_conn *conn,
1031 				    const struct bt_gatt_attr *attr, void *buf,
1032 				    uint16_t len, uint16_t offset)
1033 {
1034 	const uint8_t *value = attr->user_data;
1035 
1036 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
1037 				 sizeof(long_des_v2d3_2_value));
1038 }
1039 
1040 /**
1041  * @brief Attribute write call back for the Long descriptor V2D3 attribute
1042  *
1043  * @param conn   The connection that is requesting to write
1044  * @param attr   The attribute that's being written
1045  * @param buf    Buffer with the data to write
1046  * @param len    Number of bytes in the buffer
1047  * @param offset Offset to start writing from
1048  * @param flags  Flags (BT_GATT_WRITE_*)
1049  *
1050  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
1051  *               with a specific ATT error code.
1052  */
write_long_des_v2d3_2(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)1053 static ssize_t write_long_des_v2d3_2(struct bt_conn *conn,
1054 				     const struct bt_gatt_attr *attr,
1055 				     const void *buf, uint16_t len, uint16_t offset,
1056 				     uint8_t flags)
1057 {
1058 	uint8_t *value = attr->user_data;
1059 
1060 	if (offset >= sizeof(long_des_v2d3_2_value)) {
1061 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
1062 	}
1063 	if (offset + len > sizeof(long_des_v2d3_2_value)) {
1064 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
1065 	}
1066 
1067 	memcpy(value + offset, buf, len);
1068 
1069 	return len;
1070 }
1071 
1072 static struct bt_gatt_attr service_c_2_1_attrs[] = {
1073 	BT_GATT_H_PRIMARY_SERVICE(BT_UUID_SERVICE_C_2, 0xC0),
1074 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V10,
1075 		BT_GATT_CHRC_READ,
1076 		BT_GATT_PERM_READ,
1077 		read_value_v10, NULL, &value_v10_value, 0xC1),
1078 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
1079 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
1080 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1081 		read_str_value, write_value_v2, &value_v2_value, 0xC3),
1082 	BT_GATT_H_DESCRIPTOR(BT_UUID_LONG_DES_V2D1,
1083 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1084 		read_long_des_v2d1, write_long_des_v2d1, &long_des_v2d1_value,
1085 		0xC5),
1086 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
1087 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
1088 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1089 		read_str_value, write_value_v2_1, &value_v2_1_value, 0xC6),
1090 	BT_GATT_H_DESCRIPTOR(BT_UUID_LONG_DES_V2D2,
1091 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1092 		read_long_des_v2d2, write_long_des_v2d2, &long_des_v2d2_value,
1093 		0xC8),
1094 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
1095 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
1096 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1097 		read_str_value, write_value_v2_2, &value_v2_2_value, 0xC9),
1098 	BT_GATT_H_DESCRIPTOR(BT_UUID_LONG_DES_V2D3,
1099 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1100 		read_long_des_v2d3, write_long_des_v2d3, &long_des_v2d3_value,
1101 		0xCB),
1102 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
1103 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
1104 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1105 		read_value_v2_3, write_value_v2_3, &value_v2_3_value, 0xCC),
1106 	BT_GATT_H_DESCRIPTOR(BT_UUID_LONG_DES_V2D1,
1107 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1108 		read_long_des_v2d1_1, write_long_des_v2d1_1,
1109 		&long_des_v2d1_1_value, 0xCE),
1110 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
1111 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
1112 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1113 		read_value_v2_4, write_value_v2_4, &value_v2_4_value, 0xCF),
1114 	BT_GATT_H_DESCRIPTOR(BT_UUID_LONG_DES_V2D2,
1115 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1116 		read_long_des_v2d2_1, write_long_des_v2d2_1,
1117 		&long_des_v2d2_1_value, 0xD1),
1118 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
1119 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
1120 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1121 		read_value_v2_5, write_value_v2_5, &value_v2_5_value, 0xD2),
1122 	BT_GATT_H_DESCRIPTOR(BT_UUID_LONG_DES_V2D3,
1123 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1124 		read_long_des_v2d3_1, write_long_des_v2d3_1,
1125 		&long_des_v2d3_1_value, 0xD4),
1126 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
1127 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
1128 		BT_GATT_PERM_READ_AUTHEN | BT_GATT_PERM_WRITE_AUTHEN,
1129 		read_str_value, write_value_v2_6, &value_v2_6_value, 0xD5),
1130 	BT_GATT_H_DESCRIPTOR(BT_UUID_LONG_DES_V2D1,
1131 		BT_GATT_PERM_READ_AUTHEN | BT_GATT_PERM_WRITE_AUTHEN,
1132 		read_long_des_v2d1_2, write_long_des_v2d1_2,
1133 		&long_des_v2d1_2_value, 0xD7),
1134 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
1135 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
1136 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1137 		read_str_auth_value, write_value_v2_7, &value_v2_7_value, 0xD8),
1138 	BT_GATT_H_DESCRIPTOR(BT_UUID_LONG_DES_V2D2,
1139 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
1140 		read_long_des_v2d2_2, write_long_des_v2d2_2,
1141 		&long_des_v2d2_2_value, 0xDA),
1142 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
1143 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
1144 		BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT,
1145 		read_str_value, write_value_v2_8, &value_v2_8_value, 0xDB),
1146 	BT_GATT_H_DESCRIPTOR(BT_UUID_LONG_DES_V2D3,
1147 		BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT,
1148 		read_long_des_v2d3_2, write_long_des_v2d3_2,
1149 		&long_des_v2d3_2_value, 0xDD)
1150 };
1151 
1152 static struct bt_gatt_service service_c_2_1_svc =
1153 		    BT_GATT_SERVICE(service_c_2_1_attrs);
1154 
1155 /**
1156  * @brief Register the Service C.2 and all its Characteristics...
1157  */
service_c_2_1_init(void)1158 void service_c_2_1_init(void)
1159 {
1160 	bt_gatt_service_register(&service_c_2_1_svc);
1161 }
1162 
1163 /**
1164  * @brief Un-Register the Service C.2 and all its Characteristics...
1165  */
service_c_2_1_remove(void)1166 void service_c_2_1_remove(void)
1167 {
1168 	bt_gatt_service_unregister(&service_c_2_1_svc);
1169 }
1170 
1171 /**
1172  * @brief Set authorization for Characteristics and Descriptors in Service C.2.
1173  */
service_c_2_1_authorize(bool authorized)1174 void service_c_2_1_authorize(bool authorized)
1175 {
1176 	bAuthorized = authorized;
1177 }
1178