76 lines
1.7 KiB
C
76 lines
1.7 KiB
C
#include "device_list.h"
|
|
#include <string.h>
|
|
#include "esp_log.h"
|
|
|
|
static device_info_t device_list[DEVICE_LIST_MAX_SIZE];
|
|
static size_t device_count = 0;
|
|
static const char *TAG = "device_list";
|
|
|
|
void device_list_init(void)
|
|
{
|
|
memset(device_list, 0, sizeof(device_list));
|
|
device_count = 0;
|
|
|
|
ESP_LOGI(TAG, "Device list initialized");
|
|
}
|
|
|
|
device_info_t *device_list_find_by_mac(const struct eth_addr *mac)
|
|
{
|
|
if (mac == NULL)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
for (size_t i = 0; i < device_count; i++)
|
|
{
|
|
if (memcmp(device_list[i].mac.addr, mac->addr, ETH_HWADDR_LEN) == 0)
|
|
{
|
|
return &device_list[i];
|
|
}
|
|
}
|
|
|
|
return NULL;
|
|
}
|
|
|
|
bool device_list_add_or_update(const device_info_t *device)
|
|
{
|
|
device_info_t *existing_device = device_list_find_by_mac(&device->mac);
|
|
|
|
if (existing_device != NULL)
|
|
{
|
|
ip4_addr_copy(existing_device->ip, device->ip);
|
|
existing_device->last_seen = device->last_seen;
|
|
existing_device->active = device->active;
|
|
|
|
ESP_LOGI(TAG, "Device updated");
|
|
//ESP_LOGI(TAG, "Device updated: " MACSTR, MAC2STR(device->mac.addr));
|
|
return true;
|
|
}
|
|
|
|
if (device_count >= DEVICE_LIST_MAX_SIZE)
|
|
{
|
|
ESP_LOGW(TAG, "Device list is full");
|
|
return false;
|
|
}
|
|
|
|
device_list[device_count] = *device;
|
|
device_count++;
|
|
|
|
ESP_LOGI(TAG, "Device added (%u/%u)", (unsigned)device_count, DEVICE_LIST_MAX_SIZE);
|
|
return true;
|
|
}
|
|
|
|
size_t device_list_count(void)
|
|
{
|
|
return device_count;
|
|
}
|
|
|
|
const device_info_t *device_list_get_by_index(size_t index)
|
|
{
|
|
if (index >= device_count)
|
|
{
|
|
return NULL;
|
|
}
|
|
|
|
return &device_list[index];
|
|
} |