1 /** @file
2  *  @brief GATT TX Power Service
3  */
4 
5 /*
6  * Copyright (c) 2020 Nordic Semiconductor ASA
7  *
8  * SPDX-License-Identifier: Apache-2.0
9  */
10 
11 #include <errno.h>
12 #include <zephyr/types.h>
13 
14 #include <zephyr/bluetooth/bluetooth.h>
15 #include <zephyr/bluetooth/conn.h>
16 #include <zephyr/bluetooth/gatt.h>
17 #include <zephyr/bluetooth/uuid.h>
18 #include <zephyr/bluetooth/hci.h>
19 
20 #define LOG_LEVEL CONFIG_BT_TPS_LOG_LEVEL
21 #include <zephyr/logging/log.h>
22 LOG_MODULE_REGISTER(tps);
23 
read_tx_power_level(struct bt_conn * conn,const struct bt_gatt_attr * attr,void * buf,uint16_t len,uint16_t offset)24 static ssize_t read_tx_power_level(struct bt_conn *conn,
25 				   const struct bt_gatt_attr *attr, void *buf,
26 				   uint16_t len, uint16_t offset)
27 {
28 	int err;
29 	struct bt_conn_le_tx_power tx_power_level = {0};
30 
31 	if (offset) {
32 		return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET);
33 	}
34 
35 	err = bt_conn_le_get_tx_power_level(conn, &tx_power_level);
36 	if (err) {
37 		LOG_ERR("Failed to read Tx Power Level over HCI: %d", err);
38 		return BT_GATT_ERR(BT_ATT_ERR_UNLIKELY);
39 	}
40 
41 	LOG_INF("TPS Tx Power Level read %d", tx_power_level.current_level);
42 
43 	return bt_gatt_attr_read(conn, attr, buf, len, offset,
44 				 &tx_power_level.current_level,
45 				 sizeof(tx_power_level.current_level));
46 }
47 
48 BT_GATT_SERVICE_DEFINE(tps_svc,
49 	BT_GATT_PRIMARY_SERVICE(BT_UUID_TPS),
50 	BT_GATT_CHARACTERISTIC(BT_UUID_TPS_TX_POWER_LEVEL,
51 			       BT_GATT_CHRC_READ, BT_GATT_PERM_READ,
52 			       read_tx_power_level, NULL, NULL),
53 );
54