1 /**
2  * Copyright (c) 2019 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 /**
7  * @brief Service B.3
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.3
21  */
22 #define BT_UUID_SERVICE_B_3             BT_UUID_DECLARE_16(0xa00b)
23 
24 /**
25  *  @brief UUID for the Value V6 Characteristic
26  */
27 #define BT_UUID_VALUE_V6                BT_UUID_DECLARE_16(0xb006)
28 
29 static uint8_t   value_v6_value = 0x06;
30 static bool   value_v6_ntf_active;
31 
32 /**
33  * @brief Attribute read call back for the Value V6 attribute
34  *
35  * @param conn   The connection that is requesting to read
36  * @param attr   The attribute that's being read
37  * @param buf    Buffer to place the read result in
38  * @param len    Length of data to read
39  * @param offset Offset to start reading from
40  *
41  * @return       Number of bytes read, or in case of an error - BT_GATT_ERR()
42  *               with a specific ATT error code.
43  */
read_value_v6(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)44 static ssize_t read_value_v6(struct bt_conn *conn,
45 			     const struct bt_gatt_attr *attr, void *buf,
46 			     uint16_t len, uint16_t offset)
47 {
48 	const uint8_t *value = attr->user_data;
49 
50 	return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
51 				 sizeof(value_v6_value));
52 }
53 
54 /**
55  * @brief Attribute write call back for the Value V6 attribute
56  *
57  * @param conn   The connection that is requesting to write
58  * @param attr   The attribute that's being written
59  * @param buf    Buffer with the data to write
60  * @param len    Number of bytes in the buffer
61  * @param offset Offset to start writing from
62  * @param flags  Flags (BT_GATT_WRITE_*)
63  *
64  * @return       Number of bytes written, or in case of an error - BT_GATT_ERR()
65  *               with a specific ATT error code.
66  */
write_value_v6(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)67 static ssize_t write_value_v6(struct bt_conn *conn,
68 			      const struct bt_gatt_attr *attr, const void *buf,
69 			      uint16_t len, uint16_t offset, uint8_t flags)
70 {
71 	uint8_t *value = attr->user_data;
72 
73 	if (offset >= sizeof(value_v6_value)) {
74 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
75 	}
76 	if (offset + len > sizeof(value_v6_value)) {
77 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
78 	}
79 
80 	memcpy(value + offset, buf, len);
81 
82 	return len;
83 }
84 
85 /**
86  * @brief Descriptor configuration change call back for the Value V6 attribute
87  *
88  * @param attr  The attribute whose descriptor configuration has changed
89  * @param value The new value of the descriptor configuration
90  */
value_v6_ccc_cfg_changed(const struct bt_gatt_attr * attr,uint16_t value)91 static void value_v6_ccc_cfg_changed(const struct bt_gatt_attr *attr,
92 				     uint16_t value)
93 {
94 	value_v6_ntf_active = value == BT_GATT_CCC_NOTIFY;
95 }
96 
97 static struct bt_gatt_attr service_b_3_1_attrs[] = {
98 	BT_GATT_H_PRIMARY_SERVICE(BT_UUID_SERVICE_B_3, 0x50),
99 	BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V6,
100 		BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE_WITHOUT_RESP |
101 		BT_GATT_CHRC_WRITE | BT_GATT_CHRC_NOTIFY,
102 		BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
103 		read_value_v6, write_value_v6, &value_v6_value, 0x51),
104 	BT_GATT_H_CCC(value_v6_ccc_cfg, value_v6_ccc_cfg_changed, 0x53)
105 };
106 
107 static struct bt_gatt_service service_b_3_1_svc =
108 		    BT_GATT_SERVICE(service_b_3_1_attrs);
109 
110 /**
111  * @brief Register the Service B.3 and all its Characteristics...
112  */
service_b_3_1_init(void)113 void service_b_3_1_init(void)
114 {
115 	bt_gatt_service_register(&service_b_3_1_svc);
116 }
117 
118 /**
119  * @brief Un-Register the Service B.3 and all its Characteristics...
120  */
service_b_3_1_remove(void)121 void service_b_3_1_remove(void)
122 {
123 	bt_gatt_service_unregister(&service_b_3_1_svc);
124 }
125 
126 /**
127  * @brief Generate notification for 'Value V6' attribute, if notifications are
128  *        enabled.
129  */
service_b_3_1_value_v6_notify(void)130 void service_b_3_1_value_v6_notify(void)
131 {
132 	if (!value_v6_ntf_active) {
133 		return;
134 	}
135 
136 	bt_gatt_notify(NULL, &service_b_3_1_attrs[1], &value_v6_value,
137 		       sizeof(value_v6_value));
138 }
139