58 lines
1.3 KiB
C++
58 lines
1.3 KiB
C++
// Device data store: CRUD helpers
|
|
|
|
#include "device.hpp"
|
|
|
|
// Find a device by MAC address, returns nullptr if not found
|
|
internal device_t *find_device(const char *mac)
|
|
{
|
|
for (int i = 0; i < MAX_DEVICES; i++)
|
|
{
|
|
if (g_Devices[i].active && strcmp(g_Devices[i].mac, mac) == 0)
|
|
{
|
|
return &g_Devices[i];
|
|
}
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
// Register a device by MAC. Returns pointer to device (existing or new).
|
|
// Sets *was_new to true if it was freshly registered.
|
|
internal device_t *register_device(const char *mac, bool *was_new)
|
|
{
|
|
*was_new = false;
|
|
|
|
// Check for existing
|
|
device_t *existing = find_device(mac);
|
|
if (existing)
|
|
{
|
|
return existing;
|
|
}
|
|
|
|
// Find a free slot
|
|
for (int i = 0; i < MAX_DEVICES; i++)
|
|
{
|
|
if (!g_Devices[i].active)
|
|
{
|
|
strlcpy(g_Devices[i].mac, mac, sizeof(g_Devices[i].mac));
|
|
g_Devices[i].active = true;
|
|
strlcpy(g_Devices[i].xml_layout, kDefaultLayoutXml, sizeof(g_Devices[i].xml_layout));
|
|
*was_new = true;
|
|
return &g_Devices[i];
|
|
}
|
|
}
|
|
|
|
return nullptr; // All slots full
|
|
}
|
|
|
|
// Update the XML layout for a device. Returns true on success.
|
|
internal bool update_device_layout(const char *mac, const char *xml)
|
|
{
|
|
device_t *dev = find_device(mac);
|
|
if (!dev)
|
|
{
|
|
return false;
|
|
}
|
|
strlcpy(dev->xml_layout, xml, sizeof(dev->xml_layout));
|
|
return true;
|
|
}
|