Files
warnemuende-lighthouses/firmware/components/remote_control/uart_service.c
Peter Siegmund 8a2c0a60d5
Some checks failed
ESP-IDF Build / build (esp32, latest) (push) Failing after 46s
ESP-IDF Build / build (esp32, release-v5.4) (push) Failing after 46s
ESP-IDF Build / build (esp32, release-v5.5) (push) Failing after 45s
ESP-IDF Build / build (esp32c3, latest) (push) Failing after 45s
ESP-IDF Build / build (esp32c3, release-v5.4) (push) Failing after 45s
ESP-IDF Build / build (esp32c3, release-v5.5) (push) Failing after 45s
ESP-IDF Build / build (esp32c5, latest) (push) Failing after 45s
ESP-IDF Build / build (esp32c5, release-v5.4) (push) Failing after 45s
ESP-IDF Build / build (esp32c5, release-v5.5) (push) Failing after 45s
ESP-IDF Build / build (esp32c6, latest) (push) Failing after 45s
ESP-IDF Build / build (esp32c6, release-v5.4) (push) Failing after 45s
ESP-IDF Build / build (esp32c6, release-v5.5) (push) Failing after 45s
ESP-IDF Build / build (esp32h2, latest) (push) Failing after 46s
ESP-IDF Build / build (esp32h2, release-v5.4) (push) Failing after 45s
ESP-IDF Build / build (esp32h2, release-v5.5) (push) Failing after 45s
ESP-IDF Build / build (esp32p4, latest) (push) Failing after 45s
ESP-IDF Build / build (esp32p4, release-v5.4) (push) Failing after 45s
ESP-IDF Build / build (esp32p4, release-v5.5) (push) Failing after 52s
ESP-IDF Build / build (esp32s3, latest) (push) Failing after 56s
ESP-IDF Build / build (esp32s3, release-v5.4) (push) Failing after 46s
ESP-IDF Build / build (esp32s3, release-v5.5) (push) Failing after 45s
add BLE bonding
Signed-off-by: Peter Siegmund <mars3142@users.noreply.github.com>
2025-09-20 20:26:23 +02:00

96 lines
3.1 KiB
C

#include "include/uart_service.h"
#include "esp_log.h"
#include "include/remote_control.h"
#include "sdkconfig.h"
static const char *TAG = "uart_service";
// Service UUID: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E
const ble_uuid128_t gatt_svr_svc_uart_uuid =
BLE_UUID128_INIT(0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x01, 0x00, 0x40, 0x6E);
// RX Characteristic UUID: 6E400002-B5A3-F393-E0A9-E50E24DCCA9E
const ble_uuid128_t gatt_svr_chr_uart_rx_uuid =
BLE_UUID128_INIT(0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x02, 0x00, 0x40, 0x6E);
// TX Characteristic UUID: 6E400003-B5A3-F393-E0A9-E50E24DCCA9E
const ble_uuid128_t gatt_svr_chr_uart_tx_uuid =
BLE_UUID128_INIT(0x9E, 0xCA, 0xDC, 0x24, 0x0E, 0xE5, 0xA9, 0xE0, 0x93, 0xF3, 0xA3, 0xB5, 0x03, 0x00, 0x40, 0x6E);
uint16_t tx_chr_val_handle;
// Callback function for GATT events (read/write on characteristics)
int gatt_svr_chr_uart_access(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt *ctxt, void *arg)
{
switch (ctxt->op)
{
case BLE_GATT_ACCESS_OP_WRITE_CHR:
// Check if the RX characteristic is being written to
// if (ble_uuid_cmp((const ble_uuid_t *)&ctxt->chr->uuid, (const ble_uuid_t *)&gatt_svr_chr_uart_rx_uuid) == 0)
{
// Get data from the buffer
uint16_t data_len = OS_MBUF_PKTLEN(ctxt->om);
char *data = (char *)malloc(data_len + 1);
if (data)
{
int rc = ble_hs_mbuf_to_flat(ctxt->om, data, data_len, NULL);
if (rc == 0)
{
data[data_len] = '\0';
ESP_LOGI(TAG, "Received data: %s", data);
}
free(data);
}
return 0;
}
break;
default:
break;
}
return BLE_ATT_ERR_UNLIKELY;
}
// Function to send data via the TX characteristic
void send_ble_data(const char *data)
{
ESP_LOGI(TAG, "Preparing to send data: %s", data);
struct os_mbuf *om;
for (int i = 0; i < CONFIG_BT_NIMBLE_MAX_CONNECTIONS; i++)
{
if (g_connections[i].is_connected)
{
om = ble_hs_mbuf_from_flat(data, strlen(data));
if (om)
{
int rc = ble_gatts_notify_custom(g_connections[i].conn_handle, tx_chr_val_handle, om);
if (rc == 0)
{
ESP_LOGI(TAG, "Sent data to conn_handle %d: %s", g_connections[i].conn_handle, data);
}
else if (rc != BLE_HS_ENOTCONN) // Ignore "not connected" errors if a device just disconnected
{
ESP_LOGE(TAG, "Error sending data to conn_handle %d: %d", g_connections[i].conn_handle, rc);
}
}
}
}
}
void uart_tx_task(void *param)
{
char buffer[50];
int count = 0;
while (1)
{
vTaskDelay(pdMS_TO_TICKS(2000));
if (is_any_device_connected())
{
ESP_LOGI(TAG, "Sending data over BLE UART TX");
sprintf(buffer, "Hello World #%d", count++);
send_ble_data(buffer);
}
}
}