SystemK/BLE/BLE_Packet_Tracker.c
2025-01-25 13:45:14 -06:00

98 lines
3.1 KiB
C

/*
* 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;
}