Initial public release of SystemK.
This commit is contained in:
parent
387f57cdda
commit
6f51f5b006
129 changed files with 11654 additions and 2 deletions
29
BLE/BLE_HW_Interface.h
Normal file
29
BLE/BLE_HW_Interface.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#ifndef BLE_HW_INTERFACE_H
|
||||
#define BLE_HW_INTERFACE_H
|
||||
|
||||
SystemKResult_T BLE_GetMyAddress(uint8_t * BD_ADDR);
|
||||
SystemKResult_T BLE_ScanAndAdvertise(void);
|
||||
SystemKResult_T BLE_SetAdvertisingData(BLE_AdvertisingData_T * data);
|
||||
|
||||
#endif // BLE_HW_INTERFACE_H
|
98
BLE/BLE_Packet_Tracker.c
Normal file
98
BLE/BLE_Packet_Tracker.c
Normal file
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
* 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 <string.h>
|
||||
#include "SystemK.h"
|
||||
#include "BLE_Packets.h"
|
||||
#include "BLE_Packet_Tracker.h"
|
||||
|
||||
#define MAX_REMEMBERED_PACKETS 10
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t sender_BD_ADDR[BD_ADDR_SIZE];
|
||||
BLE_PacketType_T packet_type;
|
||||
uint8_t event_number;
|
||||
} TrackedPacket_T;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
TrackedPacket_T packets[MAX_REMEMBERED_PACKETS];
|
||||
// Number of unique packets we've seen so far. This value saturates at MAX_REMEMBERED_PACKETS.
|
||||
uint8_t count;
|
||||
// Index of the oldest packet.
|
||||
uint8_t head;
|
||||
} PacketTracker_T;
|
||||
|
||||
static PacketTracker_T Tracker =
|
||||
{
|
||||
.count = 0,
|
||||
.head = 0,
|
||||
};
|
||||
|
||||
// A packet is _new_ if the combination of BLE adress, type, and event number have not been seen recently.
|
||||
bool BLE_IsPacketNew(const uint8_t *sender_BD_ADDR,
|
||||
BLE_PacketType_T packet_type,
|
||||
uint8_t event_number)
|
||||
{
|
||||
// Check if the packet already exists in the tracker.
|
||||
for (int i = 0; i < Tracker.count; i++)
|
||||
{
|
||||
int index = (Tracker.head + i) % MAX_REMEMBERED_PACKETS;
|
||||
if ((memcmp(Tracker.packets[index].sender_BD_ADDR, sender_BD_ADDR, BD_ADDR_SIZE) == 0) &&
|
||||
(Tracker.packets[index].packet_type == packet_type) &&
|
||||
(Tracker.packets[index].event_number == event_number))
|
||||
{
|
||||
// We've already seen this packet.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// The packet is new--find the location to store it.
|
||||
int new_index;
|
||||
if (Tracker.count < MAX_REMEMBERED_PACKETS)
|
||||
{
|
||||
// There's space, so just add the new packet at the end.
|
||||
new_index = (Tracker.head + Tracker.count) % MAX_REMEMBERED_PACKETS;
|
||||
Tracker.count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
// No space, so overwrite the oldest packet.
|
||||
new_index = Tracker.head;
|
||||
Tracker.head = (Tracker.head + 1) % MAX_REMEMBERED_PACKETS;
|
||||
}
|
||||
|
||||
// Add the new packet to the tracker.
|
||||
memcpy(Tracker.packets[new_index].sender_BD_ADDR, sender_BD_ADDR, BD_ADDR_SIZE);
|
||||
Tracker.packets[new_index].packet_type = packet_type;
|
||||
Tracker.packets[new_index].event_number = event_number;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Initialize the MessageTracker
|
||||
void BLE_ClearPacketTracker(void)
|
||||
{
|
||||
Tracker.count = 0;
|
||||
Tracker.head = 0;
|
||||
}
|
42
BLE/BLE_Packet_Tracker.h
Normal file
42
BLE/BLE_Packet_Tracker.h
Normal file
|
@ -0,0 +1,42 @@
|
|||
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/** \file
|
||||
* \brief This file defines a tracker for Bluetooth Low Energy advertising packets used by KTag.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BLE_PACKET_TRACKER_H
|
||||
#define BLE_PACKET_TRACKER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
bool BLE_IsPacketNew(const uint8_t* sender_BD_ADDR, BLE_PacketType_T packet_type, uint8_t event_number);
|
||||
void BLE_ClearPacketTracker(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // BLE_PACKET_TRACKER_H
|
301
BLE/BLE_Packets.c
Normal file
301
BLE/BLE_Packets.c
Normal file
|
@ -0,0 +1,301 @@
|
|||
|
||||
/*
|
||||
* 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 <string.h>
|
||||
#include "SystemK.h"
|
||||
|
||||
|
||||
#define N_PACKET_BUFFERS 10
|
||||
static BLE_Packet_T Packet_Buffers[N_PACKET_BUFFERS];
|
||||
static BLE_AdvertisingData_T Advertising_Data;
|
||||
static uint8_t my_BD_ADDR[6];
|
||||
static bool is_my_BD_ADDR_initialized = false;
|
||||
|
||||
static const char *KLOG_TAG = "BLE";
|
||||
|
||||
|
||||
void BLE_InitPacketBuffers(void)
|
||||
{
|
||||
for (uint_fast8_t i = 0; i < N_PACKET_BUFFERS; i++)
|
||||
{
|
||||
Packet_Buffers[i].Generic.type = BLE_PACKET_TYPE_BUFFER_FREE;
|
||||
}
|
||||
}
|
||||
|
||||
//! This function always returns a buffer, but it will clobber data if there are no free buffers.
|
||||
static inline BLE_Packet_T * BLE_GetPacketBuffer(void)
|
||||
{
|
||||
for (uint_fast8_t i = 0; i < N_PACKET_BUFFERS; i++)
|
||||
{
|
||||
if (Packet_Buffers[i].Generic.type == BLE_PACKET_TYPE_BUFFER_FREE)
|
||||
{
|
||||
return &Packet_Buffers[i];
|
||||
}
|
||||
}
|
||||
|
||||
KLOG_ERROR(KLOG_TAG, "Overwrote a BLE packet buffer: consider increasing N_PACKET_BUFFERS.");
|
||||
|
||||
// Just use the last one.
|
||||
return &Packet_Buffers[N_PACKET_BUFFERS - 1];
|
||||
}
|
||||
|
||||
BLE_Packet_T * BLE_DecodeKTagPacket(const uint8_t * received_data, uint8_t received_data_length, uint8_t peer_BD_ADDR[BD_ADDR_SIZE], int8_t rssi_in_dBm)
|
||||
{
|
||||
BLE_Packet_T * result = NULL;
|
||||
|
||||
// All KTag packets are 31 bytes long.
|
||||
if (received_data_length == BLE_KTAG_PACKET_TOTAL_SIZE)
|
||||
{
|
||||
// Is this a KTag packet?
|
||||
if ( (received_data[0] == 0x1E) &&
|
||||
(received_data[1] == 0xFF) &&
|
||||
(received_data[2] == 0xFF) &&
|
||||
(received_data[3] == 0xFF) &&
|
||||
(received_data[4] == 'K') &&
|
||||
(received_data[5] == 'T') &&
|
||||
(received_data[6] == 'a') &&
|
||||
(received_data[7] == 'g') )
|
||||
{
|
||||
result = BLE_GetPacketBuffer();
|
||||
memcpy(result->Generic.BD_ADDR, peer_BD_ADDR, BD_ADDR_SIZE);
|
||||
result->Generic.RSSI = rssi_in_dBm;
|
||||
|
||||
uint8_t packet_type = received_data[8];
|
||||
|
||||
// Validate the packet type.
|
||||
if ( (packet_type < BLE_FIRST_VALID_PACKET_TYPE) ||
|
||||
(packet_type > BLE_LAST_VALID_PACKET_TYPE) )
|
||||
{
|
||||
packet_type = BLE_PACKET_TYPE_UNKNOWN;
|
||||
}
|
||||
result->Generic.type = packet_type;
|
||||
|
||||
uint8_t event_number = received_data[9];
|
||||
result->Generic.event_number = event_number;
|
||||
|
||||
// Copy the remaining MAX_BLE_PACKET_DATA_SIZE bytes of data over.
|
||||
memcpy(result->Generic.data, &received_data[10], BLE_KTAG_PACKET_DATA_SIZE);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void BLE_UpdateInstigationPacket(uint32_t Game_Length_in_ms, uint32_t Time_Remaining_Until_Countdown_in_ms)
|
||||
{
|
||||
static uint8_t EventNumber = 0;
|
||||
|
||||
Advertising_Data.length = BLE_KTAG_PACKET_TOTAL_SIZE;
|
||||
|
||||
// Manufacturer Specific Data
|
||||
Advertising_Data.data[0] = 0x1E;
|
||||
Advertising_Data.data[1] = 0xFF;
|
||||
Advertising_Data.data[2] = 0xFF;
|
||||
Advertising_Data.data[3] = 0xFF;
|
||||
Advertising_Data.data[4] = 'K';
|
||||
Advertising_Data.data[5] = 'T';
|
||||
Advertising_Data.data[6] = 'a';
|
||||
Advertising_Data.data[7] = 'g';
|
||||
Advertising_Data.data[8] = BLE_PACKET_TYPE_INSTIGATE_GAME;
|
||||
Advertising_Data.data[9] = EventNumber++;
|
||||
Advertising_Data.data[10] = (Game_Length_in_ms >> 0) & 0xFF;
|
||||
Advertising_Data.data[11] = (Game_Length_in_ms >> 8) & 0xFF;
|
||||
Advertising_Data.data[12] = (Game_Length_in_ms >> 16) & 0xFF;
|
||||
Advertising_Data.data[13] = (Game_Length_in_ms >> 24) & 0xFF;
|
||||
Advertising_Data.data[14] = (Time_Remaining_Until_Countdown_in_ms >> 0) & 0xFF;
|
||||
Advertising_Data.data[15] = (Time_Remaining_Until_Countdown_in_ms >> 8) & 0xFF;
|
||||
Advertising_Data.data[16] = (Time_Remaining_Until_Countdown_in_ms >> 16) & 0xFF;
|
||||
Advertising_Data.data[17] = (Time_Remaining_Until_Countdown_in_ms >> 24) & 0xFF;
|
||||
Advertising_Data.data[18] = 0x00;
|
||||
Advertising_Data.data[19] = 0xFF;
|
||||
Advertising_Data.data[20] = 0xFF;
|
||||
Advertising_Data.data[21] = 0xFF;
|
||||
Advertising_Data.data[22] = 0xFF;
|
||||
Advertising_Data.data[23] = 0xFF;
|
||||
Advertising_Data.data[24] = 0xFF;
|
||||
Advertising_Data.data[25] = 0xFF;
|
||||
Advertising_Data.data[26] = 0xFF;
|
||||
Advertising_Data.data[27] = 0xFF;
|
||||
Advertising_Data.data[28] = 0xFF;
|
||||
Advertising_Data.data[29] = 0xFF;
|
||||
Advertising_Data.data[30] = 0xFF;
|
||||
|
||||
SystemKResult_T result = BLE_SetAdvertisingData(&Advertising_Data);
|
||||
|
||||
if (result != SYSTEMK_RESULT_SUCCESS)
|
||||
{
|
||||
KLOG_ERROR(KLOG_TAG, "Error updating instigation packet!");
|
||||
}
|
||||
}
|
||||
|
||||
void BLE_UpdateStatusPacket()
|
||||
{
|
||||
static uint8_t EventNumber = 0;
|
||||
|
||||
uint8_t team_ID;
|
||||
uint8_t player_ID;
|
||||
uint8_t weapon_ID;
|
||||
Protocol_T protocol;
|
||||
(void) SETTINGS_get_uint8_t(SYSTEMK_SETTING_TEAMID, &team_ID);
|
||||
(void) SETTINGS_get_uint8_t(SYSTEMK_SETTING_PLAYERID, &player_ID);
|
||||
(void) SETTINGS_get_uint8_t(SYSTEMK_SETTING_WEAPONID, &weapon_ID);
|
||||
protocol = GetWeaponFromID(weapon_ID).Protocol;
|
||||
uint32_t Team_Color = (uint32_t)PROTOCOLS_GetColor(protocol, team_ID, player_ID);
|
||||
|
||||
Advertising_Data.length = BLE_KTAG_PACKET_TOTAL_SIZE;
|
||||
|
||||
// Manufacturer Specific Data
|
||||
Advertising_Data.data[0] = 0x1E;
|
||||
Advertising_Data.data[1] = 0xFF;
|
||||
Advertising_Data.data[2] = 0xFF;
|
||||
Advertising_Data.data[3] = 0xFF;
|
||||
Advertising_Data.data[4] = 'K';
|
||||
Advertising_Data.data[5] = 'T';
|
||||
Advertising_Data.data[6] = 'a';
|
||||
Advertising_Data.data[7] = 'g';
|
||||
Advertising_Data.data[8] = BLE_PACKET_TYPE_STATUS;
|
||||
Advertising_Data.data[9] = EventNumber++;
|
||||
Advertising_Data.data[10] = 4; // Tx Power in dBm
|
||||
Advertising_Data.data[11] = DUBUQUE_PROTOCOL;
|
||||
Advertising_Data.data[12] = team_ID;
|
||||
Advertising_Data.data[13] = player_ID;
|
||||
Advertising_Data.data[14] = Get_Health();
|
||||
Advertising_Data.data[15] = 0x00;
|
||||
Advertising_Data.data[16] = MAX_HEALTH;
|
||||
Advertising_Data.data[17] = 0x00;
|
||||
Advertising_Data.data[18] = (Team_Color >> 0) & 0xFF;
|
||||
Advertising_Data.data[19] = (Team_Color >> 8) & 0xFF;
|
||||
Advertising_Data.data[20] = (Team_Color >> 16) & 0xFF;
|
||||
Advertising_Data.data[21] = (Team_Color >> 24) & 0xFF;
|
||||
Advertising_Data.data[22] = (Team_Color >> 0) & 0xFF; // Secondary Color
|
||||
Advertising_Data.data[23] = (Team_Color >> 8) & 0xFF; // Secondary Color
|
||||
Advertising_Data.data[24] = (Team_Color >> 16) & 0xFF; // Secondary Color
|
||||
Advertising_Data.data[25] = (Team_Color >> 24) & 0xFF; // Secondary Color
|
||||
Advertising_Data.data[26] = 0xFF;
|
||||
Advertising_Data.data[27] = 0xFF;
|
||||
Advertising_Data.data[28] = 0xFF;
|
||||
Advertising_Data.data[29] = 0xFF;
|
||||
Advertising_Data.data[30] = 0xFF;
|
||||
|
||||
SystemKResult_T result = BLE_SetAdvertisingData(&Advertising_Data);
|
||||
|
||||
if (result != SYSTEMK_RESULT_SUCCESS)
|
||||
{
|
||||
KLOG_ERROR(KLOG_TAG, "Error updating status packet!");
|
||||
}
|
||||
}
|
||||
|
||||
void BLE_UpdateTagPacket(int16_t damage, color_t color, uint8_t target_BD_ADDR[BD_ADDR_SIZE])
|
||||
{
|
||||
static uint8_t EventNumber = 0;
|
||||
|
||||
uint8_t team_ID;
|
||||
uint8_t player_ID;
|
||||
|
||||
(void) SETTINGS_get_uint8_t(SYSTEMK_SETTING_TEAMID, &team_ID);
|
||||
(void) SETTINGS_get_uint8_t(SYSTEMK_SETTING_PLAYERID, &player_ID);
|
||||
|
||||
Advertising_Data.length = BLE_KTAG_PACKET_TOTAL_SIZE;
|
||||
|
||||
// Manufacturer Specific Data
|
||||
Advertising_Data.data[0] = 0x1E;
|
||||
Advertising_Data.data[1] = 0xFF;
|
||||
Advertising_Data.data[2] = 0xFF;
|
||||
Advertising_Data.data[3] = 0xFF;
|
||||
Advertising_Data.data[4] = 'K';
|
||||
Advertising_Data.data[5] = 'T';
|
||||
Advertising_Data.data[6] = 'a';
|
||||
Advertising_Data.data[7] = 'g';
|
||||
Advertising_Data.data[8] = BLE_PACKET_TYPE_TAG;
|
||||
Advertising_Data.data[9] = EventNumber++;
|
||||
Advertising_Data.data[10] = 4; // Tx Power in dBm
|
||||
Advertising_Data.data[11] = DUBUQUE_PROTOCOL;
|
||||
Advertising_Data.data[12] = team_ID;
|
||||
Advertising_Data.data[13] = player_ID;
|
||||
Advertising_Data.data[14] = (uint8_t)((damage >> 0) & 0xFF);
|
||||
Advertising_Data.data[15] = (uint8_t)((damage >> 8) & 0xFF);
|
||||
Advertising_Data.data[16] = (color >> 0) & 0xFF;
|
||||
Advertising_Data.data[17] = (color >> 8) & 0xFF;
|
||||
Advertising_Data.data[18] = (color >> 16) & 0xFF;
|
||||
Advertising_Data.data[19] = (color >> 24) & 0xFF;
|
||||
Advertising_Data.data[20] = target_BD_ADDR[0],
|
||||
Advertising_Data.data[21] = target_BD_ADDR[1],
|
||||
Advertising_Data.data[22] = target_BD_ADDR[2],
|
||||
Advertising_Data.data[23] = target_BD_ADDR[3],
|
||||
Advertising_Data.data[24] = target_BD_ADDR[4],
|
||||
Advertising_Data.data[25] = target_BD_ADDR[5],
|
||||
Advertising_Data.data[26] = 0xFF;
|
||||
Advertising_Data.data[27] = 0xFF;
|
||||
Advertising_Data.data[28] = 0xFF;
|
||||
Advertising_Data.data[29] = 0xFF;
|
||||
Advertising_Data.data[30] = 0xFF;
|
||||
|
||||
SystemKResult_T result = BLE_SetAdvertisingData(&Advertising_Data);
|
||||
|
||||
if (result != SYSTEMK_RESULT_SUCCESS)
|
||||
{
|
||||
KLOG_ERROR(KLOG_TAG, "Error updating status packet!");
|
||||
}
|
||||
}
|
||||
|
||||
bool BLE_IsBLEPacketForMe(const uint8_t BD_ADDR[6])
|
||||
{
|
||||
bool for_me = false;
|
||||
|
||||
if (is_my_BD_ADDR_initialized == false)
|
||||
{
|
||||
if (BLE_GetMyAddress(my_BD_ADDR) == SYSTEMK_RESULT_SUCCESS)
|
||||
{
|
||||
is_my_BD_ADDR_initialized = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
KLOG_ERROR(KLOG_TAG, "Couldn't get my BD_ADDR!");
|
||||
}
|
||||
}
|
||||
|
||||
// Is this my address?
|
||||
if ((is_my_BD_ADDR_initialized == true) &&
|
||||
(BD_ADDR[0] == my_BD_ADDR[0]) &&
|
||||
(BD_ADDR[1] == my_BD_ADDR[1]) &&
|
||||
(BD_ADDR[2] == my_BD_ADDR[2]) &&
|
||||
(BD_ADDR[3] == my_BD_ADDR[3]) &&
|
||||
(BD_ADDR[4] == my_BD_ADDR[4]) &&
|
||||
(BD_ADDR[5] == my_BD_ADDR[5]))
|
||||
{
|
||||
for_me = true;
|
||||
}
|
||||
|
||||
// Is this the broadcast address?
|
||||
if ((BD_ADDR[0] == 0xFF) &&
|
||||
(BD_ADDR[1] == 0xFF) &&
|
||||
(BD_ADDR[2] == 0xFF) &&
|
||||
(BD_ADDR[3] == 0xFF) &&
|
||||
(BD_ADDR[4] == 0xFF) &&
|
||||
(BD_ADDR[5] == 0xFF))
|
||||
{
|
||||
for_me = true;
|
||||
}
|
||||
|
||||
return for_me;
|
||||
}
|
||||
|
191
BLE/BLE_Packets.h
Normal file
191
BLE/BLE_Packets.h
Normal file
|
@ -0,0 +1,191 @@
|
|||
|
||||
/*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
/** \file
|
||||
* \brief This file defines the Bluetooth Low Energy advertising packets used by KTag.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef BLE_PACKETS_H
|
||||
#define BLE_PACKETS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Preprocessor and Type Definitions */
|
||||
#include <stdint.h>
|
||||
|
||||
#define BLE_MAX_ADVERTISING_BYTES 31
|
||||
#define BLE_MAX_SCAN_RESPONSE_BYTES 31
|
||||
|
||||
#define BD_ADDR_SIZE 6
|
||||
#define BLE_KTAG_PACKET_TOTAL_SIZE 31
|
||||
#define BLE_KTAG_PACKET_DATA_SIZE 21
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t data[BLE_MAX_ADVERTISING_BYTES];
|
||||
uint8_t length;
|
||||
} BLE_AdvertisingData_T;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t data[BLE_MAX_SCAN_RESPONSE_BYTES];
|
||||
uint8_t length;
|
||||
} BLE_ScanResponseData_T;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
BLE_PACKET_TYPE_BUFFER_FREE = 0,
|
||||
BLE_PACKET_TYPE_INSTIGATE_GAME = 1,
|
||||
BLE_FIRST_VALID_PACKET_TYPE = BLE_PACKET_TYPE_INSTIGATE_GAME,
|
||||
BLE_PACKET_TYPE_JOIN_NOW = 2,
|
||||
BLE_PACKET_TYPE_TAG = 3,
|
||||
BLE_PACKET_TYPE_CONSOLE = 4,
|
||||
BLE_PACKET_TYPE_STATUS = 5,
|
||||
BLE_LAST_VALID_PACKET_TYPE = BLE_PACKET_TYPE_STATUS,
|
||||
BLE_PACKET_TYPE_UNKNOWN
|
||||
} BLE_PacketType_T;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
BLE_PacketType_T type;
|
||||
uint8_t BD_ADDR[BD_ADDR_SIZE];
|
||||
int8_t RSSI;
|
||||
uint8_t event_number;
|
||||
|
||||
uint8_t data[BLE_KTAG_PACKET_DATA_SIZE];
|
||||
} __attribute__((packed, aligned(1))) BLE_GenericPacketType_T;
|
||||
|
||||
//! Contents of the BLE packet #BLE_PACKET_TYPE_INSTIGATE_GAME.
|
||||
typedef struct
|
||||
{
|
||||
BLE_PacketType_T type;
|
||||
uint8_t BD_ADDR[BD_ADDR_SIZE];
|
||||
int8_t RSSI;
|
||||
uint8_t event_number;
|
||||
|
||||
uint32_t game_length_in_ms;
|
||||
uint32_t time_remaining_until_countdown_in_ms;
|
||||
uint8_t random_time_after_countdown_in_ms_x100;
|
||||
uint8_t unused[12];
|
||||
} __attribute__((packed, aligned(1))) BLE_InstigationPacket_T;
|
||||
|
||||
//! Contents of the BLE packet #BLE_PACKET_TYPE_JOIN_NOW.
|
||||
typedef struct
|
||||
{
|
||||
BLE_PacketType_T type;
|
||||
uint8_t BD_ADDR[BD_ADDR_SIZE];
|
||||
int8_t RSSI;
|
||||
uint8_t event_number;
|
||||
|
||||
uint32_t game_length_in_ms;
|
||||
uint32_t time_remaining_in_game_in_ms;
|
||||
uint8_t unused[13];
|
||||
} __attribute__((packed, aligned(1))) BLE_JoinNowPacket_T;
|
||||
|
||||
//! Contents of the BLE packet #BLE_PACKET_TYPE_TAG.
|
||||
typedef struct
|
||||
{
|
||||
BLE_PacketType_T type;
|
||||
uint8_t BD_ADDR[BD_ADDR_SIZE];
|
||||
int8_t RSSI;
|
||||
uint8_t event_number;
|
||||
|
||||
uint8_t tx_power_level;
|
||||
uint8_t protocol;
|
||||
uint8_t team_ID;
|
||||
uint8_t player_ID;
|
||||
int16_t damage;
|
||||
color_t color;
|
||||
uint8_t target_BD_ADDR[BD_ADDR_SIZE];
|
||||
uint8_t unused[5];
|
||||
} __attribute__((packed, aligned(1)))BLE_TagPacket_T;
|
||||
|
||||
//! Contents of the BLE packet #BLE_PACKET_TYPE_CONSOLE.
|
||||
typedef struct
|
||||
{
|
||||
BLE_PacketType_T type;
|
||||
uint8_t BD_ADDR[BD_ADDR_SIZE];
|
||||
int8_t RSSI;
|
||||
uint8_t event_number;
|
||||
|
||||
uint8_t console_data[BLE_KTAG_PACKET_DATA_SIZE];
|
||||
} __attribute__((packed, aligned(1)))BLE_ConsolePacket_T;
|
||||
|
||||
//! Contents of the BLE packet #BLE_PACKET_TYPE_STATUS.
|
||||
typedef struct
|
||||
{
|
||||
BLE_PacketType_T type;
|
||||
uint8_t BD_ADDR[BD_ADDR_SIZE];
|
||||
int8_t RSSI;
|
||||
uint8_t event_number;
|
||||
|
||||
int8_t tx_power_level;
|
||||
uint8_t protocol;
|
||||
uint8_t team_ID;
|
||||
uint8_t player_ID;
|
||||
uint16_t health;
|
||||
uint16_t maximum_health;
|
||||
color_t primary_color;
|
||||
color_t secondary_color;
|
||||
uint8_t unused[5];
|
||||
} __attribute__((packed, aligned(1)))BLE_StatusPacket_T;
|
||||
|
||||
typedef union
|
||||
{
|
||||
BLE_GenericPacketType_T Generic;
|
||||
BLE_InstigationPacket_T Instigation;
|
||||
BLE_JoinNowPacket_T JoinNow;
|
||||
BLE_TagPacket_T Tag;
|
||||
BLE_ConsolePacket_T Console;
|
||||
BLE_StatusPacket_T Status;
|
||||
} BLE_Packet_T;
|
||||
|
||||
/* Include Files */
|
||||
|
||||
/* Public Variables */
|
||||
|
||||
/* Public Functions */
|
||||
|
||||
inline void BLE_FreePacketBuffer(void * buffer)
|
||||
{
|
||||
if (buffer != NULL)
|
||||
{
|
||||
((BLE_GenericPacketType_T *)buffer)->type = BLE_PACKET_TYPE_BUFFER_FREE;
|
||||
}
|
||||
}
|
||||
|
||||
void BLE_InitPacketBuffers(void);
|
||||
|
||||
BLE_Packet_T * BLE_DecodeKTagPacket(const uint8_t * received_data, uint8_t received_data_length, uint8_t peer_BD_ADDR[BD_ADDR_SIZE], int8_t rssi_in_dBm);
|
||||
void BLE_UpdateInstigationPacket(uint32_t Game_Length_in_ms, uint32_t Time_Remaining_Until_Countdown_in_ms);
|
||||
void BLE_UpdateStatusPacket();
|
||||
void BLE_UpdateTagPacket(int16_t damage, color_t color, uint8_t target_BD_ADDR[BD_ADDR_SIZE]);
|
||||
bool BLE_IsBLEPacketForMe(const uint8_t BD_ADDR[BD_ADDR_SIZE]);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // BLE_PACKETS_H
|
Loading…
Add table
Add a link
Reference in a new issue