Skip to content

Commit

Permalink
6: Use WiFi MAC to request 'self' device
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ngc7293 committed Dec 9, 2023
1 parent 9e96c38 commit 73fac08
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
6 changes: 0 additions & 6 deletions src/app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions src/app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
32 changes: 29 additions & 3 deletions src/app/poll.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <time.h>

#include <esp_log.h>
#include <esp_mac.h>
#include <esp_timer.h>
#include <esp_wifi.h>

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions src/app/poll.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
#define APP__POLL_H_

int app_poll_init();
void poll_request_refresh();

#endif

0 comments on commit 73fac08

Please sign in to comment.