1 /**
2  * Copyright (c) 2019 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 /**
7  * @brief Service B.5
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.5
21  */
22 #define BT_UUID_SERVICE_B_5             BT_UUID_DECLARE_16(0xa00b)
23 
24 /**
25  *  @brief UUID for the Value V8 Characteristic
26  */
27 #define BT_UUID_VALUE_V8                BT_UUID_DECLARE_16(0xb008)
28 
29 /**
30  *  @brief UUID for the Descriptor V8D1 Characteristic
31  */
32 #define BT_UUID_DES_V8D1                BT_UUID_DECLARE_16(0xb015)
33 
34 /**
35  *  @brief UUID for the Descriptor V8D2 Characteristic
36  */
37 #define BT_UUID_DES_V8D2                BT_UUID_DECLARE_16(0xb016)
38 
39 /**
40  *  @brief UUID for the Descriptor V8D3 Characteristic
41  */
42 #define BT_UUID_DES_V8D3                BT_UUID_DECLARE_16(0xb017)
43 
44 static uint8_t   value_v8_value = 0x08;
45 static uint8_t   des_v8d1_value = 0x01;
46 static uint8_t   des_v8d2_value = 0x02;
47 static uint8_t   des_v8d3_value = 0x03;
48 static bool   bAuthorized;
49 
50 /**
51  * @brief Attribute read call back for the Value V8 attribute
52  *
53  * @param conn   The connection that is requesting to read
54  * @param attr   The attribute that's being read
55  * @param buf    Buffer to place the read result in
56  * @param len    Length of data to read
57  * @param offset Offset to start reading from
58  *
59  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
60  *               with a specific ATT error code.
61  */
read_value_v8(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)62 static ssize_t read_value_v8(struct bt_conn *conn,
63 			     const struct bt_gatt_attr *attr, void *buf,
64 			     uint16_t len, uint16_t offset)
65 {
66 	const uint8_t *value = attr->user_data;
67 
68 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
69 				 sizeof(value_v8_value));
70 }
71 
72 /**
73  * @brief Attribute write call back for the Value V8 attribute
74  *
75  * @param conn   The connection that is requesting to write
76  * @param attr   The attribute that's being written
77  * @param buf    Buffer with the data to write
78  * @param len    Number of bytes in the buffer
79  * @param offset Offset to start writing from
80  * @param flags  Flags (BT_GATT_WRITE_*)
81  *
82  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
83  *               with a specific ATT error code.
84  */
write_value_v8(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)85 static ssize_t write_value_v8(struct bt_conn *conn,
86 			      const struct bt_gatt_attr *attr, const void *buf,
87 			      uint16_t len, uint16_t offset, uint8_t flags)
88 {
89 	uint8_t *value = attr->user_data;
90 
91 	if (offset >= sizeof(value_v8_value)) {
92 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
93 	}
94 	if (offset + len > sizeof(value_v8_value)) {
95 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
96 	}
97 
98 	memcpy(value + offset, buf, len);
99 
100 	return len;
101 }
102 
103 /**
104  * @brief Attribute read call back for the Descriptor V8D1 attribute
105  *
106  * @param conn   The connection that is requesting to read
107  * @param attr   The attribute that's being read
108  * @param buf    Buffer to place the read result in
109  * @param len    Length of data to read
110  * @param offset Offset to start reading from
111  *
112  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
113  *               with a specific ATT error code.
114  */
read_des_v8d1(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)115 static ssize_t read_des_v8d1(struct bt_conn *conn,
116 			     const struct bt_gatt_attr *attr, void *buf,
117 			     uint16_t len, uint16_t offset)
118 {
119 	const uint8_t *value = attr->user_data;
120 
121 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
122 				 sizeof(des_v8d1_value));
123 }
124 
125 /**
126  * @brief Attribute write call back for the Descriptor V8D1 attribute
127  *
128  * @param conn   The connection that is requesting to write
129  * @param attr   The attribute that's being written
130  * @param buf    Buffer with the data to write
131  * @param len    Number of bytes in the buffer
132  * @param offset Offset to start writing from
133  * @param flags  Flags (BT_GATT_WRITE_*)
134  *
135  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
136  *               with a specific ATT error code.
137  */
write_des_v8d1(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)138 static ssize_t write_des_v8d1(struct bt_conn *conn,
139 			      const struct bt_gatt_attr *attr, const void *buf,
140 			      uint16_t len, uint16_t offset, uint8_t flags)
141 {
142 	uint8_t *value = attr->user_data;
143 
144 	if (offset >= sizeof(des_v8d1_value)) {
145 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
146 	}
147 	if (offset + len > sizeof(des_v8d1_value)) {
148 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
149 	}
150 
151 	memcpy(value + offset, buf, len);
152 
153 	return len;
154 }
155 
156 /**
157  * @brief Attribute read call back for the Descriptor V8D2 attribute
158  *
159  * @param conn   The connection that is requesting to read
160  * @param attr   The attribute that's being read
161  * @param buf    Buffer to place the read result in
162  * @param len    Length of data to read
163  * @param offset Offset to start reading from
164  *
165  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
166  *               with a specific ATT error code.
167  */
read_des_v8d2(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)168 static ssize_t read_des_v8d2(struct bt_conn *conn,
169 			     const struct bt_gatt_attr *attr, void *buf,
170 			     uint16_t len, uint16_t offset)
171 {
172 	const uint8_t *value = attr->user_data;
173 
174 	if (!bAuthorized) {
175 		return BT_GATT_ERR(BT_ATT_ERR_AUTHORIZATION);
176 	}
177 
178 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
179 				 sizeof(des_v8d2_value));
180 }
181 
182 /**
183  * @brief Attribute write call back for the Descriptor V8D2 attribute
184  *
185  * @param conn   The connection that is requesting to write
186  * @param attr   The attribute that's being written
187  * @param buf    Buffer with the data to write
188  * @param len    Number of bytes in the buffer
189  * @param offset Offset to start writing from
190  * @param flags  Flags (BT_GATT_WRITE_*)
191  *
192  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
193  *               with a specific ATT error code.
194  */
write_des_v8d2(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)195 static ssize_t write_des_v8d2(struct bt_conn *conn,
196 			      const struct bt_gatt_attr *attr, const void *buf,
197 			      uint16_t len, uint16_t offset, uint8_t flags)
198 {
199 	uint8_t *value = attr->user_data;
200 
201 	if (offset >= sizeof(des_v8d2_value)) {
202 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
203 	}
204 	if (offset + len > sizeof(des_v8d2_value)) {
205 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
206 	}
207 	if (!bAuthorized) {
208 		return BT_GATT_ERR(BT_ATT_ERR_AUTHORIZATION);
209 	}
210 
211 	memcpy(value + offset, buf, len);
212 
213 	return len;
214 }
215 
216 /**
217  * @brief Attribute read call back for the Descriptor V8D3 attribute
218  *
219  * @param conn   The connection that is requesting to read
220  * @param attr   The attribute that's being read
221  * @param buf    Buffer to place the read result in
222  * @param len    Length of data to read
223  * @param offset Offset to start reading from
224  *
225  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
226  *               with a specific ATT error code.
227  */
read_des_v8d3(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)228 static ssize_t read_des_v8d3(struct bt_conn *conn,
229 			     const struct bt_gatt_attr *attr, void *buf,
230 			     uint16_t len, uint16_t offset)
231 {
232 	const uint8_t *value = attr->user_data;
233 
234 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
235 				 sizeof(des_v8d3_value));
236 }
237 
238 /**
239  * @brief Attribute write call back for the Descriptor V8D3 attribute
240  *
241  * @param conn   The connection that is requesting to write
242  * @param attr   The attribute that's being written
243  * @param buf    Buffer with the data to write
244  * @param len    Number of bytes in the buffer
245  * @param offset Offset to start writing from
246  * @param flags  Flags (BT_GATT_WRITE_*)
247  *
248  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
249  *               with a specific ATT error code.
250  */
write_des_v8d3(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)251 static ssize_t write_des_v8d3(struct bt_conn *conn,
252 			      const struct bt_gatt_attr *attr, const void *buf,
253 			      uint16_t len, uint16_t offset, uint8_t flags)
254 {
255 	uint8_t *value = attr->user_data;
256 
257 	if (offset >= sizeof(des_v8d3_value)) {
258 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
259 	}
260 	if (offset + len > sizeof(des_v8d3_value)) {
261 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
262 	}
263 
264 	memcpy(value + offset, buf, len);
265 
266 	return len;
267 }
268 
269 struct bt_gatt_attr service_b_5_1_attrs[] = {
270 	BT_GATT_H_PRIMARY_SERVICE(BT_UUID_SERVICE_B_5, 0x80),
271 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V8,
272 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
273 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE_ENCRYPT,
274 		read_value_v8, write_value_v8, &value_v8_value, 0x81),
275 	BT_GATT_H_DESCRIPTOR(BT_UUID_DES_V8D1,
276 		BT_GATT_PERM_READ_AUTHEN | BT_GATT_PERM_WRITE_AUTHEN,
277 		read_des_v8d1, write_des_v8d1, &des_v8d1_value, 0x83),
278 	BT_GATT_H_DESCRIPTOR(BT_UUID_DES_V8D2,
279 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
280 		read_des_v8d2, write_des_v8d2, &des_v8d2_value, 0x84),
281 	BT_GATT_H_DESCRIPTOR(BT_UUID_DES_V8D3,
282 		BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT,
283 		read_des_v8d3, write_des_v8d3, &des_v8d3_value, 0x85)
284 };
285 
286 static struct bt_gatt_service service_b_5_1_svc =
287 		    BT_GATT_SERVICE(service_b_5_1_attrs);
288 
289 /**
290  * @brief Register the Service B.5 and all its Characteristics...
291  */
service_b_5_1_init(void)292 void service_b_5_1_init(void)
293 {
294 	bt_gatt_service_register(&service_b_5_1_svc);
295 }
296 
297 /**
298  * @brief Un-Register the Service B.5 and all its Characteristics...
299  */
service_b_5_1_remove(void)300 void service_b_5_1_remove(void)
301 {
302 	bt_gatt_service_unregister(&service_b_5_1_svc);
303 }
304 
305 /**
306  * @brief Set authorization for Characteristics and Descriptors in Service B.5.
307  */
service_b_5_1_authorize(bool authorized)308 void service_b_5_1_authorize(bool authorized)
309 {
310 	bAuthorized = authorized;
311 }
312