1 /**
2 * Copyright (c) 2019 Oticon A/S
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 /**
7 * @brief Service B.1
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 B.1
21 */
22 #define BT_UUID_SERVICE_B_1 BT_UUID_DECLARE_16(0xa00b)
23
24 /**
25 * @brief UUID for the Value V4 Characteristic
26 */
27 #define BT_UUID_VALUE_V4 BT_UUID_DECLARE_16(0xb004)
28
29 static uint8_t value_v4_value = 0x04;
30
31 /**
32 * @brief Attribute read call back for the Value V4 attribute
33 *
34 * @param conn The connection that is requesting to read
35 * @param attr The attribute that's being read
36 * @param buf Buffer to place the read result in
37 * @param len Length of data to read
38 * @param offset Offset to start reading from
39 *
40 * @return Number of bytes read, or in case of an error - BT_GATT_ERR()
41 * with a specific ATT error code.
42 */
read_value_v4(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)43 static ssize_t read_value_v4(struct bt_conn *conn,
44 const struct bt_gatt_attr *attr, void *buf,
45 uint16_t len, uint16_t offset)
46 {
47 const uint8_t *value = attr->user_data;
48
49 return bt_gatt_attr_read(conn, attr, buf, len, offset, value,
50 sizeof(value_v4_value));
51 }
52
53 /**
54 * @brief Attribute write call back for the Value V4 attribute
55 *
56 * @param conn The connection that is requesting to write
57 * @param attr The attribute that's being written
58 * @param buf Buffer with the data to write
59 * @param len Number of bytes in the buffer
60 * @param offset Offset to start writing from
61 * @param flags Flags (BT_GATT_WRITE_*)
62 *
63 * @return Number of bytes written, or in case of an error - BT_GATT_ERR()
64 * with a specific ATT error code.
65 */
write_value_v4(struct bt_conn * conn,const struct bt_gatt_attr * attr,const void * buf,uint16_t len,uint16_t offset,uint8_t flags)66 static ssize_t write_value_v4(struct bt_conn *conn,
67 const struct bt_gatt_attr *attr, const void *buf,
68 uint16_t len, uint16_t offset, uint8_t flags)
69 {
70 uint8_t *value = attr->user_data;
71
72 if (offset >= sizeof(value_v4_value)) {
73 return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
74 }
75 if (offset + len > sizeof(value_v4_value)) {
76 return BT_GATT_ERR(BT_ATT_ERR_INVALID_ATTRIBUTE_LEN);
77 }
78
79 memcpy(value + offset, buf, len);
80
81 return len;
82 }
83
84 static struct bt_gatt_attr service_b_1_3_attrs[] = {
85 BT_GATT_H_PRIMARY_SERVICE(BT_UUID_SERVICE_B_1, 0x10),
86 BT_GATT_H_CHARACTERISTIC(BT_UUID_VALUE_V4,
87 BT_GATT_CHRC_READ | BT_GATT_CHRC_WRITE,
88 BT_GATT_PERM_READ | BT_GATT_PERM_WRITE,
89 read_value_v4, write_value_v4, &value_v4_value, 0x11)
90 };
91
92 static struct bt_gatt_service service_b_1_3_svc =
93 BT_GATT_SERVICE(service_b_1_3_attrs);
94
95 /**
96 * @brief Register the Service B.1 and all its Characteristics...
97 */
service_b_1_3_init(void)98 void service_b_1_3_init(void)
99 {
100 bt_gatt_service_register(&service_b_1_3_svc);
101 }
102
103 /**
104 * @brief Un-Register the Service B.1 and all its Characteristics...
105 */
service_b_1_3_remove(void)106 void service_b_1_3_remove(void)
107 {
108 bt_gatt_service_unregister(&service_b_1_3_svc);
109 }
110