58 lines
1.5 KiB
C
58 lines
1.5 KiB
C
#include "client_handler.h"
|
|
|
|
#include "esp_log.h"
|
|
#include "freertos/task.h"
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
int get_client_id(ClientList *list, const uint8_t *client_mac) {
|
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
|
if (memcmp(client_mac, list->Clients[i].macAddr, MAC_LENGTH) == 0) {
|
|
return i;
|
|
}
|
|
}
|
|
return CLIENT_DOES_NOT_EXISTS;
|
|
}
|
|
|
|
// TODO: Sanity check when list full then list->count should be MAX_CLIENTS
|
|
int get_next_free_slot(ClientList *list) {
|
|
for (int i = 0; i < MAX_CLIENTS; i++) {
|
|
// if slot is not used return index
|
|
if (!list->Clients[i].slotIsUsed) {
|
|
return i;
|
|
}
|
|
}
|
|
// list is full
|
|
return CLIENT_LIST_FULL;
|
|
}
|
|
|
|
int add_client(ClientList *list, const uint8_t *client_mac) {
|
|
if (get_client_id(list, client_mac) >= 0) {
|
|
// Client already exists dont add to list
|
|
return CLIENT_EXISTS;
|
|
}
|
|
|
|
int slot = get_next_free_slot(list);
|
|
if (slot < 0) {
|
|
// Client list full
|
|
return CLIENT_LIST_FULL;
|
|
}
|
|
|
|
list->Clients[slot].slotIsUsed = true;
|
|
list->Clients[slot].isAvailable = true;
|
|
list->Clients[slot].last_seen = xTaskGetTickCount();
|
|
list->Clients[slot].retry_counter = 0;
|
|
memcpy(list->Clients[slot].macAddr, client_mac, MAC_LENGTH);
|
|
list->ClientCount++;
|
|
return CLIENT_OK;
|
|
}
|
|
|
|
int remove_client(ClientList *list, const uint8_t client_id) {
|
|
if (client_id >= MAX_CLIENTS)
|
|
return CLIENT_INVALID_ID; // invalid index
|
|
list->Clients[client_id].slotIsUsed = false;
|
|
list->ClientCount--;
|
|
return CLIENT_OK;
|
|
}
|