Reworked BLE according to v0.11 of the KTag Beacon Specification #2

Merged
Joe merged 13 commits from New_BLE into main 2025-06-08 21:52:30 +00:00
4 changed files with 76 additions and 27 deletions
Showing only changes of commit 908bdbba35 - Show all commits

View file

@ -257,7 +257,7 @@ void BLE_UpdateTagPacket(int16_t damage, color_t color, uint8_t target_BD_ADDR[B
}
}
void BLE_RespondToConfigurationPacket(const BLE_ConfigurationPacket_T *const packet, BLE_ConfigurationSubtype_T response)
void BLE_RespondToConfigurationPacket(const BLE_ParametersPacket_T *const packet, BLE_ConfigurationSubtype_T response)
{
static uint8_t EventNumber = 0;
@ -272,7 +272,7 @@ void BLE_RespondToConfigurationPacket(const BLE_ConfigurationPacket_T *const pac
Advertising_Data.data[5] = 'T';
Advertising_Data.data[6] = 'a';
Advertising_Data.data[7] = 'g';
Advertising_Data.data[8] = BLE_PACKET_TYPE_CONFIGURATION;
Advertising_Data.data[8] = BLE_PACKET_TYPE_PARAMETERS;
Advertising_Data.data[9] = EventNumber++;
Advertising_Data.data[10] = packet->BD_ADDR[0];
Advertising_Data.data[11] = packet->BD_ADDR[1];

View file

@ -64,7 +64,7 @@ typedef enum
BLE_PACKET_TYPE_TAG = 3,
BLE_PACKET_TYPE_CONSOLE = 4,
BLE_PACKET_TYPE_STATUS = 5,
BLE_PACKET_TYPE_CONFIGURATION = 6,
BLE_PACKET_TYPE_PARAMETERS = 6,
BLE_PACKET_TYPE_HELLO = 7,
BLE_LAST_VALID_PACKET_TYPE = BLE_PACKET_TYPE_HELLO,
BLE_PACKET_TYPE_UNKNOWN
@ -178,27 +178,50 @@ typedef enum
typedef enum
{
BLE_CONFIGURATION_KEY_NONE = 0x0000,
BLE_CONFIGURATION_KEY_TEAM_ID = 0x0001,
BLE_FIRST_VALID_CONFIGURATION_KEY = BLE_CONFIGURATION_KEY_TEAM_ID,
BLE_LAST_VALID_CONFIGURATION_KEY = BLE_CONFIGURATION_KEY_TEAM_ID,
BLE_DEVICE_TYPE_UNUSED = 0xFFFF
} BLE_ConfigurationKey_T;
BLE_PARAMETER_KEY_NONE = 0,
BLE_PARAMETER_KEY_TEAM_ID = 1,
BLE_PARAMETER_KEY_PLAYER_ID = 2,
BLE_PARAMETER_KEY_GAME_LENGTH = 3,
BLE_PARAMETER_KEY_MAX_HEALTH = 4,
BLE_PARAMETER_KEY_SECONDARY_COLOR = 5,
BLE_PARAMETER_KEY_SPECIAL_WEAPONS_ON_REENTRY = 6,
BLE_FIRST_VALID_CONFIGURATION_KEY = BLE_PARAMETER_KEY_TEAM_ID,
BLE_LAST_VALID_CONFIGURATION_KEY = BLE_PARAMETER_KEY_SPECIAL_WEAPONS_ON_REENTRY,
BLE_PARAMETER_KEY_SHOTS_FIRED_THIS_GAME = 10001,
BLE_PARAMETER_KEY_TAGS_RECEIVED_THIS_GAME = 10002,
BLE_PARAMETER_KEY_TIMES_TAGGED_OUT_THIS_GAME = 10003,
BLE_FIRST_VALID_GAME_RESULT_KEY = BLE_PARAMETER_KEY_SHOTS_FIRED_THIS_GAME,
BLE_LAST_VALID_GAME_RESULT_KEY = BLE_PARAMETER_KEY_TIMES_TAGGED_OUT_THIS_GAME,
BLE_PARAMETER_KEY_UNUSED = 65535
} BLE_ParameterKey_T;
inline BLE_ConfigurationKey_T BLE_GetValidConfigKey(uint16_t key)
inline BLE_ParameterKey_T BLE_GetValidConfigKey(uint16_t key)
{
BLE_ConfigurationKey_T result = BLE_CONFIGURATION_KEY_NONE;
BLE_ParameterKey_T result = BLE_PARAMETER_KEY_NONE;
if ((key >= BLE_FIRST_VALID_CONFIGURATION_KEY) &&
(key <= BLE_LAST_VALID_CONFIGURATION_KEY))
{
result = (BLE_ConfigurationKey_T)key;
result = (BLE_ParameterKey_T)key;
}
return result;
}
//! Contents of the BLE packet #BLE_PACKET_TYPE_CONFIGURATION.
inline BLE_ParameterKey_T BLE_GetValidGameResultKey(uint16_t key)
{
BLE_ParameterKey_T result = BLE_PARAMETER_KEY_NONE;
if ((key >= BLE_FIRST_VALID_GAME_RESULT_KEY) &&
(key <= BLE_LAST_VALID_GAME_RESULT_KEY))
{
result = (BLE_ParameterKey_T)key;
}
return result;
}
//! Contents of the BLE packet #BLE_PACKET_TYPE_PARAMETERS.
typedef struct
{
BLE_PacketType_T type;
@ -213,7 +236,7 @@ typedef struct
uint16_t key_two;
uint32_t value_two;
uint8_t unused[2];
} __attribute__((packed, aligned(1))) BLE_ConfigurationPacket_T;
} __attribute__((packed, aligned(1))) BLE_ParametersPacket_T;
typedef enum
{
@ -247,7 +270,7 @@ typedef union
BLE_TagPacket_T Tag;
BLE_ConsolePacket_T Console;
BLE_StatusPacket_T Status;
BLE_ConfigurationPacket_T Configuration;
BLE_ParametersPacket_T Configuration;
BLE_HelloPacket_T Hello;
} BLE_Packet_T;
@ -271,7 +294,7 @@ BLE_Packet_T *BLE_DecodeKTagPacket(const uint8_t *received_data, uint8_t receive
void BLE_UpdateInstigationPacket(uint32_t Game_Length_in_ms, uint32_t Time_Remaining_Until_Countdown_in_ms);
void BLE_UpdateStatusPacket(uint8_t current_state);
void BLE_UpdateTagPacket(int16_t damage, color_t color, uint8_t target_BD_ADDR[BD_ADDR_SIZE]);
void BLE_RespondToConfigurationPacket(const BLE_ConfigurationPacket_T *const packet, BLE_ConfigurationSubtype_T response);
void BLE_RespondToConfigurationPacket(const BLE_ParametersPacket_T *const packet, BLE_ConfigurationSubtype_T response);
void BLE_UpdateHelloPacket();
bool BLE_IsBLEPacketForMe(const uint8_t BD_ADDR[BD_ADDR_SIZE]);

View file

@ -89,6 +89,27 @@ __attribute__((always_inline)) inline TeamID_t Resolve_Common_Team_ID(uint8_t te
return (team_ID & COMMON_TEAM_ID_MASK);
}
__attribute__((always_inline)) static inline SystemKResult_T Set_Player_With_Audio_Feedback(uint8_t player_ID)
{
static uint8_t Player_ID = 0; // This is static because AUDIO_PRONOUNCE_NUMBER_0_TO_100 needs a *pointer*.
SystemKResult_T result = SYSTEMK_RESULT_SUCCESS;
Player_ID = player_ID;
result = SETTINGS_set_uint8_t(SYSTEMK_SETTING_PLAYERID, Player_ID);
if (result == SYSTEMK_RESULT_SUCCESS)
{
AudioAction_T audio_action = {.ID = AUDIO_PLAY_PLAYER_ID_PROMPT, .Play_To_Completion = true, .Data = (void *)0x00};
Perform_Audio_Action(&audio_action);
AudioAction_T volume_action = {.ID = AUDIO_PRONOUNCE_NUMBER_0_TO_100, .Play_To_Completion = true, .Data = (void *)&Player_ID};
Perform_Audio_Action(&volume_action);
}
return result;
}
__attribute__((always_inline)) inline bool Team_Can_Tag_Me(uint8_t tagging_team_ID)
{
bool can_tag = false;

View file

@ -26,7 +26,7 @@ static void Configuring_Entry(StateMachineContext_T *context);
static void Configuring_Do(StateMachineContext_T *context);
static void Configuring_Exit(StateMachineContext_T *context);
static void HandleBLEConfigurationPacket(const BLE_ConfigurationPacket_T *const packet);
static void HandleBLEConfigurationPacket(const BLE_ParametersPacket_T *const packet);
static TimerHandle_t BLEConfigurationResponseTimer = NULL;
static StaticTimer_t xBLEConfigurationResponseTimerBuffer;
@ -229,9 +229,9 @@ static void Configuring_Do(StateMachineContext_T *context)
break;
case KEVENT_BLE_PACKET_RECEIVED:
if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_CONFIGURATION)
if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_PARAMETERS)
{
HandleBLEConfigurationPacket((BLE_ConfigurationPacket_T *)Event.Data);
HandleBLEConfigurationPacket((BLE_ParametersPacket_T *)Event.Data);
}
else if (((BLE_Packet_T *)Event.Data)->Generic.type == BLE_PACKET_TYPE_EVENT)
{
@ -267,11 +267,11 @@ static void Configuring_Exit(StateMachineContext_T *context)
xQueueSend(xQueueNeoPixels, &neopixels_action, 0);
}
static SystemKResult_T HandleParameterChangeRequest(BLE_ConfigurationKey_T key, uint32_t value)
static SystemKResult_T HandleParameterChangeRequest(BLE_ParameterKey_T key, uint32_t value)
{
SystemKResult_T result = SYSTEMK_RESULT_UNSPECIFIED_FAILURE;
if (key == BLE_CONFIGURATION_KEY_TEAM_ID)
if (key == BLE_PARAMETER_KEY_TEAM_ID)
{
uint8_t team_ID = (uint8_t)value;
if (Is_Valid_Team_ID(team_ID))
@ -279,30 +279,35 @@ static SystemKResult_T HandleParameterChangeRequest(BLE_ConfigurationKey_T key,
result = Set_Team_With_Audio_Feedback(team_ID);
}
}
else if (key == BLE_PARAMETER_KEY_PLAYER_ID)
{
uint8_t player_ID = (uint8_t)value;
result = Set_Player_With_Audio_Feedback(player_ID);
}
return result;
}
void HandleBLEConfigurationPacket(const BLE_ConfigurationPacket_T *const packet)
void HandleBLEConfigurationPacket(const BLE_ParametersPacket_T *const packet)
{
if (BLE_IsBLEPacketForMe(packet->target_BD_ADDR))
{
if (BLE_IsPacketNew(packet->BD_ADDR, BLE_PACKET_TYPE_CONFIGURATION, packet->event_number))
if (BLE_IsPacketNew(packet->BD_ADDR, BLE_PACKET_TYPE_PARAMETERS, packet->event_number))
{
if (packet->subtype == BLE_REQUEST_PARAMETER_CHANGE)
{
SystemKResult_T result = SYSTEMK_RESULT_SUCCESS;
BLE_ConfigurationKey_T key_one = BLE_GetValidConfigKey(packet->key_one);
if (key_one != BLE_CONFIGURATION_KEY_NONE)
BLE_ParameterKey_T key_one = BLE_GetValidConfigKey(packet->key_one);
if (key_one != BLE_PARAMETER_KEY_NONE)
{
result = HandleParameterChangeRequest(key_one, packet->value_one);
}
if (result == SYSTEMK_RESULT_SUCCESS)
{
BLE_ConfigurationKey_T key_two = BLE_GetValidConfigKey(packet->key_two);
if (key_two != BLE_CONFIGURATION_KEY_NONE)
BLE_ParameterKey_T key_two = BLE_GetValidConfigKey(packet->key_two);
if (key_two != BLE_PARAMETER_KEY_NONE)
{
result = HandleParameterChangeRequest(key_two, packet->value_two);
}