Init commit
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
idf_component_register(
|
||||
SRCS "device_list.c"
|
||||
INCLUDE_DIRS "include"
|
||||
)
|
||||
@@ -0,0 +1,76 @@
|
||||
#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];
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef DEVICE_LIST_H
|
||||
#define DEVICE_LIST_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "lwip/ip4_addr.h"
|
||||
#include "lwip/prot/ethernet.h"
|
||||
|
||||
#define DEVICE_LIST_MAX_SIZE 64
|
||||
|
||||
typedef struct
|
||||
{
|
||||
ip4_addr_t ip;
|
||||
struct eth_addr mac;
|
||||
TickType_t last_seen;
|
||||
bool active;
|
||||
} device_info_t;
|
||||
|
||||
void device_list_init(void);
|
||||
device_info_t *device_list_find_by_mac(const struct eth_addr *mac);
|
||||
bool device_list_add_or_update(const device_info_t *device);
|
||||
size_t device_list_count(void);
|
||||
const device_info_t *device_list_get_by_index(size_t index);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user