SystemK/BLE/BLE_Utils.c
Joe Kearney 430aec54b8 Reworked BLE according to v0.12 of the KTag Beacon Specification (#5)
This change to SystemK implements version 0.12 of the KTag Beacon Specification.

The spec. is here: [KTag Beacon Specification v0.12](https://ktag.clubk.club/Technology/BLE/KTag%20Beacon%20Specification%20v0.12.pdf)
This change also includes the 31AUG2025 changes to the State Machine (now documented [here](https://ktag.clubk.club/Technology/SystemK/SystemKStateMachine.drawio.png)), as well as changes to support automated testing.

**All projects should update to this version.**

Co-authored-by: Joe Kearney <joe@clubk.club>
Reviewed-on: #5
2025-09-01 17:44:10 +00:00

127 lines
No EOL
4.8 KiB
C

/*
* This program source code file is part of SystemK, a library in the KTag project.
*
* 🛡️ <https://ktag.clubk.club> 🃞
*
* Copyright © 2025 Joseph P. Kearney and the KTag developers.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* There should be a copy of the GNU Affero General Public License in the LICENSE
* file in the root of this repository. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "SystemK.h"
const uint8_t BLE_BROADCAST_ADDRESS[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
//! Converts a 6-byte Bluetooth Low Energy address to a human-readable string (for logging).
/*!
* \param bd_addr The Bluetooth address as a 6-byte array
* \return Pointer to a static string containing the formatted address
*
* Output format: "XX:XX:XX:XX:XX:XX" where XX are uppercase hexadecimal values
* \note This function is not reentrant as it uses a static buffer!
*/
const char *BLE_ADDR_To_Str(const uint8_t bd_addr[6])
{
static char str_addr[18]; // 17 characters + null terminator
// Convert each byte and add separating colons.
for (int i = 0; i < 6; i++)
{
snprintf(&str_addr[i * 3], sizeof(str_addr) - (i * 3), "%02X", bd_addr[5-i]);
// Add colon separator except after the last byte.
if (i < 5)
{
str_addr[(i * 3) + 2] = ':';
}
}
str_addr[17] = '\0'; // Ensure null termination.
return str_addr;
}
SystemKResult_T BLE_HandleCommonEvents(KEvent_T *event)
{
SystemKResult_T result = SYSTEMK_RESULT_EVENT_NOT_HANDLED;
if (event->ID == KEVENT_BLE_PACKET_RECEIVED)
{
BLE_Packet_T * packet = (BLE_Packet_T *)event->Data;
if (packet->Generic.type == BLE_PACKET_TYPE_EVENT)
{
BLE_EventPacket_T * event_packet = (BLE_EventPacket_T *)packet;
if (BLE_IsBLEPacketForMe(event_packet->target_BD_ADDR))
{
if (BLE_IsPacketNew(event_packet->BD_ADDR, BLE_PACKET_TYPE_EVENT, event_packet->event_number))
{
if (event_packet->event_ID == BLE_EVENT_QUIET)
{
uint32_t duration_ms = event_packet->event_data;
if (BLE_Quiet(duration_ms) == SYSTEMK_RESULT_SUCCESS)
{
if (duration_ms > 0)
{
KLOG_INFO("BLE", "Quiet mode activated for %lu ms.", duration_ms);
}
else
{
KLOG_INFO("BLE", "Quiet mode activated until further notice.");
}
}
else
{
KLOG_ERROR("BLE", "Failed to activate quiet mode.");
}
result = SYSTEMK_RESULT_SUCCESS;
}
else if (event_packet->event_ID == BLE_EVENT_UNQUIET)
{
if (BLE_Unquiet() == SYSTEMK_RESULT_SUCCESS)
{
KLOG_INFO("BLE", "Quiet mode deactivated.");
}
else
{
KLOG_ERROR("BLE", "Failed to deactivate quiet mode.");
}
result = SYSTEMK_RESULT_SUCCESS;
}
else if (event_packet->event_ID == BLE_EVENT_FORCE_STATE)
{
uint32_t requested_state = event_packet->event_data;
if (requested_state < STATE_IS_OUT_OF_RANGE)
{
Transition_For_Event(GetContext(), (StateID_T)requested_state, event);
KLOG_INFO("BLE", "State %lu forced.", requested_state);
result = SYSTEMK_RESULT_SUCCESS;
}
else
{
KLOG_ERROR("BLE", "Attempted to force unknown state %lu.", requested_state);
result = SYSTEMK_RESULT_OVERFLOW;
}
}
}
}
}
}
return result;
}