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 3'
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 static uint8_t   value_v10_value = 0x0A;
37 static uint8_t   value_v2_value[] = {
38 	      '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '3', '3',
39 	      '3', '3', '4', '4', '4', '4', '4', '5', '\0'
40 };
41 static uint8_t   value_v2_1_value[] = {
42 	      '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '4', '4', '4',
43 	      '4', '4', '5', '5', '5', '5', '5', '6', '6', '\0'
44 };
45 static uint8_t   value_v2_2_value[] = {
46 	      '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '5', '5', '5',
47 	      '5', '5', '6', '6', '6', '6', '6', '7', '7', '7', '\0'
48 };
49 static uint8_t   value_v2_3_value[] = {
50 	      '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '3', '3',
51 	      '3', '3', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '6',
52 	      '6', '6', '6', '6', '7', '7', '7', '7', '7', '8', '8', '8', '8',
53 	      '8', '9', '9', '9', '\0'
54 };
55 static uint8_t   value_v2_4_value[] = {
56 	      '2', '2', '2', '2', '2', '3', '3', '3', '3', '3', '4', '4', '4',
57 	      '4', '4', '5', '5', '5', '5', '5', '6', '6', '6', '6', '6', '7',
58 	      '7', '7', '7', '7', '8', '8', '8', '8', '8', '9', '9', '9', '9',
59 	      '9', '0', '0', '0', '0', '\0'
60 };
61 static uint8_t   value_v2_5_value[] = {
62 	      '3', '3', '3', '3', '3', '4', '4', '4', '4', '4', '5', '5', '5',
63 	      '5', '5', '6', '6', '6', '6', '6', '7', '7', '7', '7', '7', '8',
64 	      '8', '8', '8', '8', '9', '9', '9', '9', '9', '0', '0', '0', '0',
65 	      '0', '1', '1', '1', '1', '1', '\0'
66 };
67 
68 /**
69  * @brief Attribute read call back for the Value V10 attribute
70  *
71  * @param conn   The connection that is requesting to read
72  * @param attr   The attribute that's being read
73  * @param buf    Buffer to place the read result in
74  * @param len    Length of data to read
75  * @param offset Offset to start reading from
76  *
77  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
78  *               with a specific ATT error code.
79  */
read_value_v10(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)80 static ssize_t read_value_v10(struct bt_conn *conn,
81 			      const struct bt_gatt_attr *attr, void *buf,
82 			      uint16_t len, uint16_t offset)
83 {
84 	const uint8_t *value = attr->user_data;
85 
86 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
87 				 sizeof(value_v10_value));
88 }
89 
90 /**
91  * @brief Attribute read call back for the Value V2 string attribute
92  *
93  * @param conn   The connection that is requesting to read
94  * @param attr   The attribute that's being read
95  * @param buf    Buffer to place the read result in
96  * @param len    Length of data to read
97  * @param offset Offset to start reading from
98  *
99  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
100  *               with a specific ATT error code.
101  */
read_str_value(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)102 static ssize_t read_str_value(struct bt_conn *conn,
103 			      const struct bt_gatt_attr *attr, void *buf,
104 			      uint16_t len, uint16_t offset)
105 {
106 	const char *value = attr->user_data;
107 
108 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
109 				 strlen(value));
110 }
111 
112 /**
113  * @brief Attribute write call back for the Value V2 attribute
114  *
115  * @param conn   The connection that is requesting to write
116  * @param attr   The attribute that's being written
117  * @param buf    Buffer with the data to write
118  * @param len    Number of bytes in the buffer
119  * @param offset Offset to start writing from
120  * @param flags  Flags (BT_GATT_WRITE_*)
121  *
122  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
123  *               with a specific ATT error code.
124  */
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)125 static ssize_t write_value_v2(struct bt_conn *conn,
126 			      const struct bt_gatt_attr *attr, const void *buf,
127 			      uint16_t len, uint16_t offset, uint8_t flags)
128 {
129 	char *value = attr->user_data;
130 
131 	if (offset >= sizeof(value_v2_value)) {
132 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
133 	}
134 	if (offset + len > sizeof(value_v2_value)) {
135 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
136 	}
137 
138 	memcpy(value + offset, buf, len);
139 
140 	return len;
141 }
142 
143 /**
144  * @brief Attribute write call back for the Value V2 attribute
145  *
146  * @param conn   The connection that is requesting to write
147  * @param attr   The attribute that's being written
148  * @param buf    Buffer with the data to write
149  * @param len    Number of bytes in the buffer
150  * @param offset Offset to start writing from
151  * @param flags  Flags (BT_GATT_WRITE_*)
152  *
153  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
154  *               with a specific ATT error code.
155  */
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)156 static ssize_t write_value_v2_1(struct bt_conn *conn,
157 				const struct bt_gatt_attr *attr,
158 				const void *buf, uint16_t len, uint16_t offset,
159 				uint8_t flags)
160 {
161 	char *value = attr->user_data;
162 
163 	if (offset >= sizeof(value_v2_1_value)) {
164 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
165 	}
166 	if (offset + len > sizeof(value_v2_1_value)) {
167 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
168 	}
169 
170 	memcpy(value + offset, buf, len);
171 
172 	return len;
173 }
174 
175 /**
176  * @brief Attribute write call back for the Value V2 attribute
177  *
178  * @param conn   The connection that is requesting to write
179  * @param attr   The attribute that's being written
180  * @param buf    Buffer with the data to write
181  * @param len    Number of bytes in the buffer
182  * @param offset Offset to start writing from
183  * @param flags  Flags (BT_GATT_WRITE_*)
184  *
185  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
186  *               with a specific ATT error code.
187  */
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)188 static ssize_t write_value_v2_2(struct bt_conn *conn,
189 				const struct bt_gatt_attr *attr,
190 				const void *buf, uint16_t len, uint16_t offset,
191 				uint8_t flags)
192 {
193 	char *value = attr->user_data;
194 
195 	if (offset >= sizeof(value_v2_2_value)) {
196 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
197 	}
198 	if (offset + len > sizeof(value_v2_2_value)) {
199 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
200 	}
201 
202 	memcpy(value + offset, buf, len);
203 
204 	return len;
205 }
206 
207 /**
208  * @brief Attribute write call back for the Value V2 attribute
209  *
210  * @param conn   The connection that is requesting to write
211  * @param attr   The attribute that's being written
212  * @param buf    Buffer with the data to write
213  * @param len    Number of bytes in the buffer
214  * @param offset Offset to start writing from
215  * @param flags  Flags (BT_GATT_WRITE_*)
216  *
217  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
218  *               with a specific ATT error code.
219  */
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)220 static ssize_t write_value_v2_3(struct bt_conn *conn,
221 				const struct bt_gatt_attr *attr,
222 				const void *buf, uint16_t len, uint16_t offset,
223 				uint8_t flags)
224 {
225 	char *value = attr->user_data;
226 
227 	if (offset >= sizeof(value_v2_3_value)) {
228 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
229 	}
230 	if (offset + len > sizeof(value_v2_3_value)) {
231 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
232 	}
233 
234 	memcpy(value + offset, buf, len);
235 
236 	return len;
237 }
238 
239 /**
240  * @brief Attribute write call back for the Value V2 attribute
241  *
242  * @param conn   The connection that is requesting to write
243  * @param attr   The attribute that's being written
244  * @param buf    Buffer with the data to write
245  * @param len    Number of bytes in the buffer
246  * @param offset Offset to start writing from
247  * @param flags  Flags (BT_GATT_WRITE_*)
248  *
249  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
250  *               with a specific ATT error code.
251  */
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)252 static ssize_t write_value_v2_4(struct bt_conn *conn,
253 				const struct bt_gatt_attr *attr,
254 				const void *buf, uint16_t len, uint16_t offset,
255 				uint8_t flags)
256 {
257 	char *value = attr->user_data;
258 
259 	if (offset >= sizeof(value_v2_4_value)) {
260 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
261 	}
262 	if (offset + len > sizeof(value_v2_4_value)) {
263 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
264 	}
265 
266 	memcpy(value + offset, buf, len);
267 
268 	return len;
269 }
270 
271 /**
272  * @brief Attribute write call back for the Value V2 attribute
273  *
274  * @param conn   The connection that is requesting to write
275  * @param attr   The attribute that's being written
276  * @param buf    Buffer with the data to write
277  * @param len    Number of bytes in the buffer
278  * @param offset Offset to start writing from
279  * @param flags  Flags (BT_GATT_WRITE_*)
280  *
281  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
282  *               with a specific ATT error code.
283  */
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)284 static ssize_t write_value_v2_5(struct bt_conn *conn,
285 				const struct bt_gatt_attr *attr,
286 				const void *buf, uint16_t len, uint16_t offset,
287 				uint8_t flags)
288 {
289 	char *value = attr->user_data;
290 
291 	if (offset >= sizeof(value_v2_5_value)) {
292 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
293 	}
294 	if (offset + len > sizeof(value_v2_5_value)) {
295 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
296 	}
297 
298 	memcpy(value + offset, buf, len);
299 
300 	return len;
301 }
302 
303 static struct bt_gatt_attr service_c_2_3_attrs[] = {
304 	BT_GATT_H_PRIMARY_SERVICE(BT_UUID_SERVICE_C_2, 0xA0),
305 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V10,
306 		BT_GATT_CHRC_READ,
307 		BT_GATT_PERM_READ,
308 		read_value_v10, NULL, &value_v10_value, 0xA1),
309 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
310 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
311 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
312 		read_str_value, write_value_v2, &value_v2_value, 0xA3),
313 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
314 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
315 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
316 		read_str_value, write_value_v2_1, &value_v2_1_value, 0xA5),
317 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
318 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
319 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
320 		read_str_value, write_value_v2_2, &value_v2_2_value, 0xA7),
321 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
322 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
323 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
324 		read_str_value, write_value_v2_3, &value_v2_3_value, 0xA9),
325 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
326 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
327 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
328 		read_str_value, write_value_v2_4, &value_v2_4_value, 0xAB),
329 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
330 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
331 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
332 		read_str_value, write_value_v2_5, &value_v2_5_value, 0xAD)
333 };
334 
335 static struct bt_gatt_service service_c_2_3_svc =
336 		    BT_GATT_SERVICE(service_c_2_3_attrs);
337 
338 /**
339  * @brief Register the Service C.2 and all its Characteristics...
340  */
service_c_2_3_init(void)341 void service_c_2_3_init(void)
342 {
343 	bt_gatt_service_register(&service_c_2_3_svc);
344 }
345 
346 /**
347  * @brief Un-Register the Service C.2 and all its Characteristics...
348  */
service_c_2_3_remove(void)349 void service_c_2_3_remove(void)
350 {
351 	bt_gatt_service_unregister(&service_c_2_3_svc);
352 }
353