#include #include #include #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "freertos/event_groups.h" #include "esp_system.h" #include "esp_wifi.h" #include "esp_event_loop.h" #include "sdkconfig.h" #include "esp_log.h" #include "esp_err.h" #include "nvs_flash.h" #include "lwip/esp_netif_net_stack.h" #include "lwip/err.h" #include "lwip/sockets.h" #include "lwip/sys.h" #include "lwip/netdb.h" #include "lwip/inet.h" #include "lwip/esp_pbuf_ref.h" #include "lwip/pbuf.h" #include "lwip/opt.h" #include "esp_netif_net_stack.h" #include "netif/etharp.h" #include "lwip/etharp.h" #include "lwip/netif.h" #include "esp_netif_types.h" char *ssid = "Riolu-Love"; char *psk = "HomageToTheDoctor^$^"; char *payload = "\xff\xff\xff\xff\xff\xff,\xcfg\ti\xf1\x08\x06\x00\x01\x08\x00\x06\x04\x00\x01,\xcfg\ti\xf1\n\x00\x00\xca\x00\x00\x00\x00\x00\x00\n\x00\x00\xff"; uint8_t mac[8] = {0x01,0x02,0x03,0x04,0x05,0x06}; static const char *TAG = "PrincessPiARPMessage"; wifi_config_t wifi_sta_config; int espIp; size_t payloadSize = sizeof(payload); void wifi_sta_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data) { if (event_base == WIFI_EVENT) { switch (event_id) { case WIFI_EVENT_STA_START: ESP_LOGI(TAG, "WIFI_EVENT_STA_START"); esp_wifi_connect(); break; case WIFI_EVENT_STA_DISCONNECTED: ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED"); esp_wifi_connect(); break; } } else if (event_base == IP_EVENT) { switch (event_id) { case IP_EVENT_STA_GOT_IP: { ip_event_got_ip_t* event = event_data; ESP_LOGI(TAG, "Station connected with IP: "IPSTR", GW: "IPSTR", Mask: "IPSTR".", IP2STR(&event->ip_info.ip), IP2STR(&event->ip_info.gw), IP2STR(&event->ip_info.netmask)); espIp = IP2STR(&event->ip_info.ip); break; } } } } void wifiInit() { // NVS: Required by WiFi Driver esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); ret = nvs_flash_init(); } ESP_ERROR_CHECK(ret); ESP_ERROR_CHECK(esp_netif_init()); ESP_ERROR_CHECK(esp_event_loop_create_default()); } esp_netif_t* wifiClientInit(char *ssid, char *psk) { esp_netif_t* netif = esp_netif_create_default_wifi_sta(); ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_sta_event_handler, NULL, NULL)); ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &wifi_sta_event_handler, NULL, NULL)); strcpy((char *)wifi_sta_config.sta.ssid, ssid); strcpy((char *)wifi_sta_config.sta.password, psk); return(netif); } void wifiRun() { wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT(); ESP_ERROR_CHECK(esp_wifi_init(&cfg)); ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA)); ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_sta_config)); ESP_ERROR_CHECK(esp_wifi_start()); } void executeLowLevelPayloads(esp_netif_t* netif, char *payload, uint8_t mac[]) { //struct pbuf* packetBuf = pbuf_alloc(PBUF_RAW, payloadSize, PBUF_RAM); //ESP_ERROR_CHECK(wlanif_init(netif)); //ESP_ERROR_CHECK(esp_netif_set_mac(netif, mac)); // memcpy(packetBuf->payload, payload, payloadSize); ESP_ERROR_CHECK(esp_netif_transmit(netif, payload, payloadSize)); //low_level_output(netif, packetBuf); } void app_main(void) { wifiInit(); esp_netif_t* netif = wifiClientInit(ssid, psk); wifiRun(); executeLowLevelPayloads(netif, payload, mac); }