Files
MicrOBU/obu-firmware/main/simple_ble.hpp
T

64 lines
2.9 KiB
C++
Raw Normal View History

#pragma once
#include "link_protocol.hpp"
#include <cstdint>
#include <functional>
namespace microbu::ble {
using Receiver = std::function<void(link::Bytes)>;
/// @brief A set of counters for tracking BLE message traffic.
struct Counters {
std::uint32_t rx_messages = 0;
std::uint32_t tx_messages = 0;
std::uint32_t tx_failed = 0;
std::uint32_t malformed = 0;
};
/**
* @brief Starts a simple, bonded BLE GATT peripheral on the NimBLE host.
* Uses structured ETSI C-ITS Station Service & Characteristics:
* Service: 0000C175-BA5E-4C17-8000-00805F9B34FB
* BTP-DATA.request: 0000C176-BA5E-4C17-8000-00805F9B34FB (Write, Authenticated/Encrypted)
* BTP-DATA.indication: 0000C177-BA5E-4C17-8000-00805F9B34FB (Notify, Authenticated/Encrypted)
* PoTi Fix Update: 0000C178-BA5E-4C17-8000-00805F9B34FB (Write, Authenticated/Encrypted)
* Access probe: 0000C179-BA5E-4C17-8000-00805F9B34FB (Empty Read, Authenticated/Encrypted)
* SF-SAP Identity: 0000C17A-BA5E-4C17-8000-00805F9B34FB (Notify, Authenticated/Encrypted)
* Station Configure: 0000C17B-BA5E-4C17-8000-00805F9B34FB (Write, Authenticated/Encrypted)
* Confirm: 0000C17C-BA5E-4C17-8000-00805F9B34FB (Notify, Authenticated/Encrypted)
*
* Station-specific compile-time passkey and one pinned owner bond, stored in NVS.
* Each GATT value carries one message or fragment, up to the negotiated ATT limit.
* @param receiver Invoked with each decoded station-link message written by the phone.
* @return false if the NimBLE host or GATT service failed to start.
*/
bool start(Receiver receiver);
/**
* @brief Sends one complete station-link message via GATT notification.
* Dispatches immediately without queuing delay.
* @param message Encoded station-link message to notify.
* @return false if not connected/subscribed or the notification could not be sent.
*/
bool write(const link::Bytes& message);
/// @return true if a phone is connected.
bool connected();
/// @brief Pauses (false) or resumes (true) advertising while no phone is connected. The C5 shares one radio
/// between BLE and ITS-G5: each advertising event leaves the 5.9 GHz receiver deaf, so a board measuring on air
/// (raw capture over USB) stops advertising (2026-09-29: 2-4 % of the frames missed by the receiving board).
void set_advertising(bool enabled);
/// @return Largest link message one notification carries: min(ATT MTU - 3, maximum_message).
std::size_t max_message();
/// @return true if the phone has subscribed to notifications.
bool is_subscribed();
/// @return Current message/error counters.
Counters counters();
/// @return esp_timer time (us) of the last GATT write from the phone, a connection-event anchor; 0 if none.
std::int64_t last_write_us();
/// @return Current connection interval in us; 0 if not connected.
std::uint32_t connection_interval_us();
} // namespace microbu::ble