1 /**
2  * Copyright (c) 2019 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 /**
7  * @brief Service B.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 B.2
21  */
22 #define BT_UUID_SERVICE_B_2             BT_UUID_DECLARE_16(0xa00b)
23 
24 /**
25  *  @brief UUID for the Value V5 Characteristic
26  */
27 #define BT_UUID_VALUE_V5                BT_UUID_DECLARE_16(0xb005)
28 
29 /**
30  *  @brief UUID for the Descriptor V5D4 (128-bit UUID) Characteristic
31  */
32 #define BT_UUID_DES_V5D4__128_BIT_UUID  BT_UUID_DECLARE_128( \
33 		0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01, \
34 		0x00, 0x00, 0x00, 0x00, 0xd4, 0xd5, 0x00, 0x00)
35 
36 static uint8_t   value_v5_value = 0x05;
37 static struct bt_gatt_cep cha_ext_pro_value = { 0x0003 };
38 static uint8_t   cha_user_des_value[] = {
39 	      'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
40 	      'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
41 	      '\0'
42 };
43 static const struct bt_gatt_cpf cha_format_value = {
44 	     0x04, 0x00, 0x3001, 0x01, 0x3111
45 };
46 static uint8_t   des_v5d4__128_bit_uuid_value = 0x44;
47 static bool   bAuthorized;
48 
49 /**
50  * @brief Attribute read call back for the Value V5 attribute
51  *
52  * @param conn   The connection that is requesting to read
53  * @param attr   The attribute that's being read
54  * @param buf    Buffer to place the read result in
55  * @param len    Length of data to read
56  * @param offset Offset to start reading from
57  *
58  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
59  *               with a specific ATT error code.
60  */
read_value_v5(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)61 static ssize_t read_value_v5(struct bt_conn *conn,
62 			     const struct bt_gatt_attr *attr, void *buf,
63 			     uint16_t len, uint16_t offset)
64 {
65 	const uint8_t *value = attr->user_data;
66 
67 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
68 				 sizeof(value_v5_value));
69 }
70 
71 /**
72  * @brief Attribute write call back for the Value V5 attribute
73  *
74  * @param conn   The connection that is requesting to write
75  * @param attr   The attribute that's being written
76  * @param buf    Buffer with the data to write
77  * @param len    Number of bytes in the buffer
78  * @param offset Offset to start writing from
79  * @param flags  Flags (BT_GATT_WRITE_*)
80  *
81  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
82  *               with a specific ATT error code.
83  */
write_value_v5(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)84 static ssize_t write_value_v5(struct bt_conn *conn,
85 			      const struct bt_gatt_attr *attr, const void *buf,
86 			      uint16_t len, uint16_t offset, uint8_t flags)
87 {
88 	uint8_t *value = attr->user_data;
89 
90 	if (offset >= sizeof(value_v5_value)) {
91 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
92 	}
93 	if (offset + len > sizeof(value_v5_value)) {
94 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
95 	}
96 	if (!bAuthorized) {
97 		return BT_GATT_ERR(BT_ATT_ERR_AUTHORIZATION);
98 	}
99 
100 	memcpy(value + offset, buf, len);
101 
102 	return len;
103 }
104 
105 /**
106  * @brief Attribute read call back for the Descriptor V5D4 (128-bit UUID)
107  *        attribute
108  *
109  * @param conn   The connection that is requesting to read
110  * @param attr   The attribute that's being read
111  * @param buf    Buffer to place the read result in
112  * @param len    Length of data to read
113  * @param offset Offset to start reading from
114  *
115  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
116  *               with a specific ATT error code.
117  */
read_des_v5d4__128_bit_uuid(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)118 static ssize_t read_des_v5d4__128_bit_uuid(struct bt_conn *conn,
119 					   const struct bt_gatt_attr *attr,
120 					   void *buf, uint16_t len, uint16_t offset)
121 {
122 	const uint8_t *value = attr->user_data;
123 
124 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
125 				 sizeof(des_v5d4__128_bit_uuid_value));
126 }
127 
128 static struct bt_gatt_attr service_b_2_1_attrs[] = {
129 	BT_GATT_H_PRIMARY_SERVICE(BT_UUID_SERVICE_B_2, 0x70),
130 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V5,
131 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE | BT_GATT_CHRC_EXT_PROP,
132 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
133 		read_value_v5, write_value_v5, &value_v5_value, 0x71),
134 	BT_GATT_H_CEP(&cha_ext_pro_value, 0x73),
135 	BT_GATT_H_CUD(cha_user_des_value, BT_GATT_PERM_READ, 0x74),
136 	BT_GATT_H_CPF(&cha_format_value, 0x75),
137 	BT_GATT_H_DESCRIPTOR(BT_UUID_DES_V5D4__128_BIT_UUID,
138 		BT_GATT_PERM_READ,
139 		read_des_v5d4__128_bit_uuid, NULL,
140 		&des_v5d4__128_bit_uuid_value, 0x76)
141 };
142 
143 static struct bt_gatt_service service_b_2_1_svc =
144 		    BT_GATT_SERVICE(service_b_2_1_attrs);
145 
146 /**
147  * @brief Register the Service B.2 and all its Characteristics...
148  */
service_b_2_1_init(void)149 void service_b_2_1_init(void)
150 {
151 	bt_gatt_service_register(&service_b_2_1_svc);
152 }
153 
154 /**
155  * @brief Un-Register the Service B.2 and all its Characteristics...
156  */
service_b_2_1_remove(void)157 void service_b_2_1_remove(void)
158 {
159 	bt_gatt_service_unregister(&service_b_2_1_svc);
160 }
161 
162 /**
163  * @brief Set authorization for Characteristics and Descriptors in Service B.2.
164  */
service_b_2_1_authorize(bool authorized)165 void service_b_2_1_authorize(bool authorized)
166 {
167 	bAuthorized = authorized;
168 }
169