1 /**
2  * Copyright (c) 2019 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 /**
7  * @brief Service A
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 extern struct bt_gatt_attr service_d_3_attrs[];
20 extern struct bt_gatt_attr service_c_1_3_attrs[];
21 
22 /**
23  *  @brief UUID for the Service A
24  */
25 #define BT_UUID_SERVICE_A               BT_UUID_DECLARE_16(0xa00a)
26 
27 /**
28  *  @brief UUID for the Value V1 Characteristic
29  */
30 #define BT_UUID_VALUE_V1                BT_UUID_DECLARE_16(0xb001)
31 
32 /**
33  *  @brief UUID for the Value V2 Characteristic
34  */
35 #define BT_UUID_VALUE_V2                BT_UUID_DECLARE_16(0xb002)
36 
37 /**
38  *  @brief UUID for the Value V3 Characteristic
39  */
40 #define BT_UUID_VALUE_V3                BT_UUID_DECLARE_16(0xb003)
41 
42 static uint8_t   value_v1_value = 0x01;
43 static uint8_t   value_v2_value[] = {
44 	      '1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '3', '3',
45 	      '3', '3', '4', '4', '4', '4', '4', '5', '5', '5', '5', '5', '6',
46 	      '6', '6', '6', '6', '7', '7', '7', '7', '7', '8', '8', '8', '8',
47 	      '8', '9', '9', '9', '9', '9', '0', '0', '0', '0', '0', '\0'
48 };
49 static uint8_t   value_v3_value = 0x03;
50 
51 /**
52  * @brief Attribute read call back for the Value V1 attribute
53  *
54  * @param conn   The connection that is requesting to read
55  * @param attr   The attribute that's being read
56  * @param buf    Buffer to place the read result in
57  * @param len    Length of data to read
58  * @param offset Offset to start reading from
59  *
60  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
61  *               with a specific ATT error code.
62  */
read_value_v1(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)63 static ssize_t read_value_v1(struct bt_conn *conn,
64 			     const struct bt_gatt_attr *attr, void *buf,
65 			     uint16_t len, uint16_t offset)
66 {
67 	const uint8_t *value = attr->user_data;
68 
69 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
70 				 sizeof(value_v1_value));
71 }
72 
73 /**
74  * @brief Attribute read call back for the Value V2 string attribute
75  *
76  * @param conn   The connection that is requesting to read
77  * @param attr   The attribute that's being read
78  * @param buf    Buffer to place the read result in
79  * @param len    Length of data to read
80  * @param offset Offset to start reading from
81  *
82  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
83  *               with a specific ATT error code.
84  */
read_str_value(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)85 static ssize_t read_str_value(struct bt_conn *conn,
86 			      const struct bt_gatt_attr *attr, void *buf,
87 			      uint16_t len, uint16_t offset)
88 {
89 	const char *value = attr->user_data;
90 
91 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
92 				 strlen(value));
93 }
94 
95 /**
96  * @brief Attribute write call back for the Value V2 attribute
97  *
98  * @param conn   The connection that is requesting to write
99  * @param attr   The attribute that's being written
100  * @param buf    Buffer with the data to write
101  * @param len    Number of bytes in the buffer
102  * @param offset Offset to start writing from
103  * @param flags  Flags (BT_GATT_WRITE_*)
104  *
105  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
106  *               with a specific ATT error code.
107  */
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)108 static ssize_t write_value_v2(struct bt_conn *conn,
109 			      const struct bt_gatt_attr *attr, const void *buf,
110 			      uint16_t len, uint16_t offset, uint8_t flags)
111 {
112 	char *value = attr->user_data;
113 
114 	if (offset >= sizeof(value_v2_value)) {
115 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
116 	}
117 	if (offset + len > sizeof(value_v2_value)) {
118 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
119 	}
120 
121 	memcpy(value + offset, buf, len);
122 
123 	return len;
124 }
125 
126 /**
127  * @brief Attribute write call back for the Value V3 attribute
128  *
129  * @param conn   The connection that is requesting to write
130  * @param attr   The attribute that's being written
131  * @param buf    Buffer with the data to write
132  * @param len    Number of bytes in the buffer
133  * @param offset Offset to start writing from
134  * @param flags  Flags (BT_GATT_WRITE_*)
135  *
136  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
137  *               with a specific ATT error code.
138  */
write_value_v3(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)139 static ssize_t write_value_v3(struct bt_conn *conn,
140 			      const struct bt_gatt_attr *attr, const void *buf,
141 			      uint16_t len, uint16_t offset, uint8_t flags)
142 {
143 	uint8_t *value = attr->user_data;
144 
145 	if (offset >= sizeof(value_v3_value)) {
146 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
147 	}
148 	if (offset + len > sizeof(value_v3_value)) {
149 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
150 	}
151 
152 	memcpy(value + offset, buf, len);
153 
154 	return len;
155 }
156 
157 static struct bt_gatt_attr service_a_3_attrs[] = {
158 	BT_GATT_H_PRIMARY_SERVICE(BT_UUID_SERVICE_A, 0x70),
159 	BT_GATT_H_INCLUDE_SERVICE(service_d_3_attrs, 0x71),
160 	BT_GATT_H_INCLUDE_SERVICE(service_c_1_3_attrs, 0x72),
161 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V1,
162 		BT_GATT_CHRC_READ,
163 		BT_GATT_PERM_READ,
164 		read_value_v1, NULL, &value_v1_value, 0x73),
165 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V2,
166 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
167 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
168 		read_str_value, write_value_v2, &value_v2_value, 0x75),
169 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V3,
170 		BT_GATT_CHRC_WRITE,
171 		BT_GATT_PERM_WRITE,
172 		NULL, write_value_v3, &value_v3_value, 0x77)
173 };
174 
175 static struct bt_gatt_service service_a_3_svc =
176 		    BT_GATT_SERVICE(service_a_3_attrs);
177 
178 /**
179  * @brief Register the Service A and all its Characteristics...
180  */
service_a_3_init(void)181 void service_a_3_init(void)
182 {
183 	bt_gatt_service_register(&service_a_3_svc);
184 }
185 
186 /**
187  * @brief Un-Register the Service A and all its Characteristics...
188  */
service_a_3_remove(void)189 void service_a_3_remove(void)
190 {
191 	bt_gatt_service_unregister(&service_a_3_svc);
192 }
193