// =============================================================================
//  system/hwid.cpp -- Hardware ID from ESP32 eFuse (Sprint 8)
// =============================================================================

#include "hwid.h"

#include <esp_efuse.h>
#include <esp_mac.h>
#include <cstring>
#include <cstdio>

namespace arautopilot::system {

bool hwid_get_mac(uint8_t out[6]) {
    return esp_efuse_mac_get_default(out) == ESP_OK;
}

uint32_t hwid_hash() {
    uint8_t mac[6] = {};
    hwid_get_mac(mac);
    // Simple FNV-32 hash of the 6 bytes.
    uint32_t h = 2166136261U;
    for (int i = 0; i < 6; ++i) {
        h ^= (uint32_t)mac[i];
        h *= 16777619U;
    }
    return h;
}

void hwid_format(uint8_t mac[6], char buf[18]) {
    snprintf(buf, 18, "%02X:%02X:%02X:%02X:%02X:%02X",
             mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
}

}  // namespace arautopilot::system
