SystemK/Events/KEvents.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

80 lines
2.3 KiB
C
Executable file

/*
* This program source code file is part of SystemK, a library in the KTag project.
*
* 🛡️ <https://ktag.clubk.club> 🃞
*
* Copyright © 2016-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 <stdbool.h>
#include <stdio.h>
#include "SystemK.h"
#include "Command_Mapping.h"
#define QUEUE_LENGTH 10
#define ITEM_SIZE sizeof(KEvent_T)
static StaticQueue_t StaticQueue;
static uint8_t QueueStorageArea[QUEUE_LENGTH * ITEM_SIZE];
static QueueHandle_t xQueueEvents;
static void Remap_Event(KEvent_T *event)
{
switch (event->ID)
{
case KEVENT_COMMAND_RECEIVED:
{
if (((DecodedPacket_T *)(event->Data))->Command.protocol == NEC_PROTOCOL)
{
uint32_t NEC_data = ((DecodedPacket_T *)(event->Data))->Command.data;
Remap_NEC_Command(event, NEC_data);
}
FreeDecodedPacketBuffer(event->Data);
}
break;
default:
// No remapping necessary.
break;
}
}
void Init_KEvents(void)
{
xQueueEvents = xQueueCreateStatic(QUEUE_LENGTH, ITEM_SIZE, QueueStorageArea, &StaticQueue);
}
void Post_KEvent(KEvent_T *event)
{
Remap_Event(event);
if (BLE_HandleCommonEvents(event) == SYSTEMK_RESULT_EVENT_NOT_HANDLED)
{
// If the event was not handled by the BLE subsystem, post it to the global event queue.
xQueueSend(xQueueEvents, event, 0);
}
}
void Post_KEvent_From_ISR(KEvent_T *event, portBASE_TYPE *xHigherPriorityTaskWoken)
{
Remap_Event(event);
xQueueSendFromISR(xQueueEvents, event, xHigherPriorityTaskWoken);
}
portBASE_TYPE Receive_KEvent(KEvent_T *event)
{
return xQueueReceive(xQueueEvents, event, 0);
}