Files
mellifera_firmware/firmware/app/src/peripheral_gatt_write.c
Brendan Haines 4574ee3088
Some checks failed
Build / Build (push) Failing after 3m29s
Build / build (ubuntu-22.04) (push) Failing after 20s
Documentation / build (push) Failing after -1m6s
report battery levels with bluetooth
2025-05-31 19:56:32 -06:00

127 lines
2.6 KiB
C

/*
* Copyright (c) 2022 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <zephyr/kernel.h>
#include <zephyr/sys/printk.h>
#include <zephyr/bluetooth/bluetooth.h>
#include <zephyr/bluetooth/conn.h>
#include <zephyr/bluetooth/gatt.h>
#include <zephyr/bluetooth/hci.h>
#include <zephyr/bluetooth/services/bas.h>
extern int mtu_exchange(struct bt_conn *conn);
extern int write_cmd(struct bt_conn *conn);
extern struct bt_conn *conn_connected;
extern uint32_t last_write_rate;
static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA_BYTES(BT_DATA_UUID16_ALL,
BT_UUID_16_ENCODE(BT_UUID_BAS_VAL), // battery service
),
BT_DATA(BT_DATA_NAME_COMPLETE, CONFIG_BT_DEVICE_NAME, sizeof(CONFIG_BT_DEVICE_NAME) - 1), // -1 to strip \0
};
static unsigned char url_data[] = "\x17//brendanhaines.com";
static const struct bt_data sd[] = {
BT_DATA(BT_DATA_URI, url_data, sizeof(url_data) - 1), // -1 to strip \0
};
static void mtu_updated(struct bt_conn *conn, uint16_t tx, uint16_t rx)
{
printk("Updated MTU: TX: %d RX: %d bytes\n", tx, rx);
}
#if defined(CONFIG_BT_SMP)
static void auth_cancel(struct bt_conn *conn)
{
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
printk("Pairing cancelled: %s\n", addr);
}
static struct bt_conn_auth_cb auth_callbacks = {
.cancel = auth_cancel,
};
#endif /* CONFIG_BT_SMP */
static struct bt_gatt_cb gatt_callbacks = {
.att_mtu_updated = mtu_updated};
uint32_t peripheral_gatt_write(uint32_t count)
{
int err;
err = bt_enable(NULL);
if (err)
{
printk("Bluetooth init failed (err %d)\n", err);
return 0U;
}
printk("Bluetooth initialized\n");
bt_gatt_cb_register(&gatt_callbacks);
#if defined(CONFIG_BT_SMP)
(void)bt_conn_auth_cb_register(&auth_callbacks);
#endif /* CONFIG_BT_SMP */
err = bt_le_adv_start(BT_LE_ADV_CONN_FAST_1, ad, ARRAY_SIZE(ad), sd, ARRAY_SIZE(sd));
if (err)
{
printk("Advertising failed to start (err %d)\n", err);
return 0U;
}
printk("Advertising successfully started\n");
conn_connected = NULL;
last_write_rate = 0U;
while (true)
{
struct bt_conn *conn = NULL;
if (conn_connected)
{
/* Get a connection reference to ensure that a
* reference is maintained in case disconnected
* callback is called while we perform GATT Write
* command.
*/
conn = bt_conn_ref(conn_connected);
}
if (conn)
{
write_cmd(conn);
bt_conn_unref(conn);
if (count)
{
count--;
if (!count)
{
break;
}
}
k_yield();
}
else
{
k_sleep(K_SECONDS(1));
}
}
return last_write_rate;
}