ESP32/main/broadcast_beacon.c

176 lines
5.6 KiB
C

/*
* broadcast_beacon.c
*
* Copyright (c) 2025 Umber Networks & Robert McMahon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted under the terms of the BSD 3-Clause License.
*
* Sends periodic UDP broadcast so a Linux laptop can detect the device.
* Advertises: IP, mask, gw, dhcp, MAC, and whether fiwi-telemetry file exists on SD.
*/
#include <string.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_log.h"
#include "esp_event.h"
#include "esp_netif.h"
#include "esp_wifi.h"
#include "sd_card.h"
#include "sdkconfig.h"
#define BEACON_PORT 5555
#ifndef CONFIG_BEACON_INTERVAL_SEC
#define CONFIG_BEACON_INTERVAL_SEC 5
#endif
#define BEACON_PAYLOAD_MAX 256
static const char *TAG = "BEACON";
static esp_event_handler_instance_t s_instance_got_ip = NULL;
static esp_event_handler_instance_t s_instance_disconnected = NULL;
static TaskHandle_t s_beacon_task = NULL;
static EventGroupHandle_t s_beacon_events = NULL;
#define BEACON_IP_READY_BIT (1 << 0)
#define BEACON_STOP_BIT (1 << 1)
static void beacon_task(void *arg) {
(void)arg;
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) {
ESP_LOGE(TAG, "socket: %s", strerror(errno));
vTaskDelete(NULL);
return;
}
int enable = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &enable, sizeof(enable)) != 0) {
ESP_LOGW(TAG, "setsockopt SO_BROADCAST: %s", strerror(errno));
}
struct sockaddr_in dst = {0};
dst.sin_family = AF_INET;
dst.sin_port = htons(BEACON_PORT);
dst.sin_addr.s_addr = htonl(INADDR_BROADCAST);
esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
if (!netif) {
ESP_LOGE(TAG, "no WIFI_STA netif");
close(sock);
vTaskDelete(NULL);
return;
}
char payload[BEACON_PAYLOAD_MAX];
while (1) {
EventBits_t bits = xEventGroupWaitBits(s_beacon_events,
BEACON_STOP_BIT, pdTRUE, pdFALSE,
pdMS_TO_TICKS(CONFIG_BEACON_INTERVAL_SEC * 1000));
if (bits & BEACON_STOP_BIT) {
break;
}
esp_netif_ip_info_t ip_info;
if (esp_netif_get_ip_info(netif, &ip_info) != ESP_OK || ip_info.ip.addr == 0) {
continue;
}
esp_netif_dhcp_status_t dhcp_status;
esp_netif_dhcpc_get_status(netif, &dhcp_status);
bool dhcp_on = (dhcp_status == ESP_NETIF_DHCP_STARTED);
uint8_t mac[6] = {0};
esp_netif_get_mac(netif, mac);
bool fiwi_telemetry = sd_card_is_ready() && sd_card_file_exists("fiwi-telemetry");
int n = snprintf(payload, sizeof(payload),
"{\"ip\":\"" IPSTR "\",\"mask\":\"" IPSTR "\",\"gw\":\"" IPSTR "\","
"\"dhcp\":\"%s\",\"mac\":\"%02x:%02x:%02x:%02x:%02x:%02x\","
"\"fiwi_telemetry\":%s}\n",
IP2STR(&ip_info.ip), IP2STR(&ip_info.netmask), IP2STR(&ip_info.gw),
dhcp_on ? "ON" : "OFF",
mac[0], mac[1], mac[2], mac[3], mac[4], mac[5],
fiwi_telemetry ? "true" : "false");
if (n < 0 || n >= (int)sizeof(payload)) {
continue;
}
ssize_t sent = sendto(sock, payload, (size_t)n, 0,
(struct sockaddr *)&dst, sizeof(dst));
if (sent < 0) {
ESP_LOGD(TAG, "sendto: %s", strerror(errno));
}
}
close(sock);
s_beacon_task = NULL;
vTaskDelete(NULL);
}
static void beacon_event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
(void)arg;
(void)event_data;
if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
xEventGroupClearBits(s_beacon_events, BEACON_STOP_BIT);
xEventGroupSetBits(s_beacon_events, BEACON_IP_READY_BIT);
if (s_beacon_task == NULL) {
xTaskCreate(beacon_task, "beacon", 4096, NULL, 5, &s_beacon_task);
}
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
xEventGroupClearBits(s_beacon_events, BEACON_IP_READY_BIT);
xEventGroupSetBits(s_beacon_events, BEACON_STOP_BIT);
/* task will exit and set s_beacon_task = NULL */
}
}
esp_err_t broadcast_beacon_init(void) {
if (s_beacon_events == NULL) {
s_beacon_events = xEventGroupCreate();
if (s_beacon_events == NULL) {
return ESP_ERR_NO_MEM;
}
}
esp_err_t err;
err = esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
&beacon_event_handler, NULL, &s_instance_got_ip);
if (err != ESP_OK) return err;
err = esp_event_handler_instance_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED,
&beacon_event_handler, NULL, &s_instance_disconnected);
if (err != ESP_OK) {
esp_event_handler_instance_unregister(IP_EVENT, IP_EVENT_STA_GOT_IP, s_instance_got_ip);
return err;
}
/* If already connected, start beacon immediately */
esp_netif_t *netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");
if (netif) {
esp_netif_ip_info_t ip_info;
if (esp_netif_get_ip_info(netif, &ip_info) == ESP_OK && ip_info.ip.addr != 0) {
xEventGroupSetBits(s_beacon_events, BEACON_IP_READY_BIT);
if (s_beacon_task == NULL) {
xTaskCreate(beacon_task, "beacon", 4096, NULL, 5, &s_beacon_task);
}
}
}
ESP_LOGI(TAG, "Beacon init: UDP broadcast port %d, interval %d s", BEACON_PORT, CONFIG_BEACON_INTERVAL_SEC);
return ESP_OK;
}