#include "RFIDInterface.hpp" #include #include #include #include #include #include "WebInterface.hpp" static const char* TAG = "rfid"; static char lastTagId[26]; static std::function legacyCallback; static std::function ndefCallback; static char* getLastTagId() { return lastTagId; } static void onUidCallback(uint8_t* sn) { if(sn == NULL) { ESP_LOGI(TAG, "Tag removed"); lastTagId[0] = '\0'; if(legacyCallback) legacyCallback(NULL); } else { snprintf(lastTagId, sizeof(lastTagId), "%#x_%#x_%#x_%#x_%#x", sn[0], sn[1], sn[2], sn[3], sn[4]); ESP_LOGI(TAG, "Tag UID: %s", lastTagId); if(legacyCallback) legacyCallback(lastTagId); } } static void onNdefCallback(uint8_t* sn, const char* url) { if(!ndefCallback) return; if(sn == NULL) { ndefCallback(NULL, NULL); return; } /* Format UID same way as above */ char uid[26]; snprintf(uid, sizeof(uid), "%#x_%#x_%#x_%#x_%#x", sn[0], sn[1], sn[2], sn[3], sn[4]); if(url) { ESP_LOGI(TAG, "NDEF URL on %s: %s", uid, url); } else { ESP_LOGI(TAG, "No NDEF on %s", uid); } ndefCallback(uid, url); } RFIDInterface::RFIDInterface() { startConfig = (rc522_start_args_t){ .miso_io = 12, .mosi_io = 13, .sck_io = 23, .sda_io = 18, .spi_host_id = (spi_host_device_t)0, .callback = onUidCallback, .ndef_callback = onNdefCallback, }; } void RFIDInterface::start() { rc522_start(startConfig); } void RFIDInterface::registerWebResources(WebInterface* interface) { httpd_uri_t config = { .uri = "/rfid", .method = HTTP_GET, .handler = [](httpd_req_t* req) { httpd_resp_send(req, getLastTagId(), HTTPD_RESP_USE_STRLEN); return ESP_OK; } }; interface->registerResource(&config); } void RFIDInterface::registerTagCallback(std::function cb) { ndefCallback = cb; } void RFIDInterface::registerTagChangeCallback(std::function cb) { legacyCallback = cb; }