From 73fac0893a03bf36bcecc5c97240a06dc35548af Mon Sep 17 00:00:00 2001 From: David Bourgault Date: Fri, 8 Dec 2023 22:23:19 -0500 Subject: [PATCH] 6: Use WiFi MAC to request 'self' device Uses `esp_read_mac(..., WIFI_STA)` to get the base MAC address of the ESP device and send it to the server in the GetDevice call. Removes the GANYMEDE_DEVICE_ID config element. --- src/app/Kconfig | 6 ------ src/app/main.c | 6 ++++-- src/app/poll.c | 32 +++++++++++++++++++++++++++++--- src/app/poll.h | 1 + 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/app/Kconfig b/src/app/Kconfig index c770b03..6c32973 100644 --- a/src/app/Kconfig +++ b/src/app/Kconfig @@ -13,10 +13,4 @@ menu "Poll" help Server name expected by the nginx ingress (if any). This could differ from GANYMEDE_HOST when deploying the server on a local network. - - config GANYMEDE_DEVICE_ID - string "Ganymede Device ID" - help - MAC-address lookup is not yet supported, so the Device ID must be - hard-coded into the code. endmenu \ No newline at end of file diff --git a/src/app/main.c b/src/app/main.c index 96b7fe0..23a96fb 100644 --- a/src/app/main.c +++ b/src/app/main.c @@ -84,10 +84,12 @@ void app_main(void) if (strcmp(linebuf, "register") == 0) { auth_request_register(); } - - if (strcmp(linebuf, "memory") == 0) { + else if (strcmp(linebuf, "memory") == 0) { report_memory(); } + else if (strcmp(linebuf, "poll") == 0) { + poll_request_refresh(); + } } else { linebuf[cursor++] = (char) c; } diff --git a/src/app/poll.c b/src/app/poll.c index 86e6290..37ce542 100644 --- a/src/app/poll.c +++ b/src/app/poll.c @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -72,12 +73,32 @@ static size_t _pack_protobuf(ProtobufCMessage* request, uint8_t* buffer) return length; } +static esp_err_t _poll_get_mac(char* dest) +{ + uint8_t mac[6] = { 0 }; + + if (esp_read_mac(mac, ESP_MAC_WIFI_STA) != ESP_OK) { + ESP_LOGE(TAG, "Could not read WiFi MAC address"); + return ESP_FAIL; + } + + snprintf(dest, 18, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + ESP_LOGD(TAG, "MAC %s", dest); + return ESP_OK; +} + static Ganymede__V2__Device* poll_fetch_device(http2_session_t* session, const struct http_perform_options* options) { + char mac[17] = { 0 }; + Ganymede__V2__GetDeviceRequest request; ganymede__v2__get_device_request__init(&request); - request.device_uid = CONFIG_GANYMEDE_DEVICE_ID; - request.filter_case = GANYMEDE__V2__GET_DEVICE_REQUEST__FILTER_DEVICE_UID; + request.filter_case = GANYMEDE__V2__GET_DEVICE_REQUEST__FILTER_DEVICE_MAC; + request.device_mac = mac; + + if (_poll_get_mac(mac) != ESP_OK) { + return NULL; + } uint32_t length = _pack_protobuf((ProtobufCMessage*)&request, _payload_buffer); @@ -237,5 +258,10 @@ int app_poll_init() } xEventGroupSetBits(_poll_event_group, POLL_REFRESH_REQUEST_BIT); - return esp_timer_start_periodic(_poll_refresh_timer, 60e6); + return esp_timer_start_periodic(_poll_refresh_timer, 3600 * 10e6); +} + +void poll_request_refresh() +{ + xEventGroupSetBits(_poll_event_group, POLL_REFRESH_REQUEST_BIT); } \ No newline at end of file diff --git a/src/app/poll.h b/src/app/poll.h index 6457516..2d67558 100644 --- a/src/app/poll.h +++ b/src/app/poll.h @@ -2,5 +2,6 @@ #define APP__POLL_H_ int app_poll_init(); +void poll_request_refresh(); #endif \ No newline at end of file