esp_802.11_tx demo

/* Simple WiFi Example

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include 
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "rom/ets_sys.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"

/* The examples use simple WiFi configuration that you can set via
   'make menuconfig'.

   If you'd rather not, just change the below entries to strings with
   the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
// #define EXAMPLE_ESP_WIFI_MODE_AP   CONFIG_ESP_WIFI_MODE_AP //TRUE:AP FALSE:STA
#define EXAMPLE_ESP_WIFI_SSID      "sunluxin_ap"
#define EXAMPLE_ESP_WIFI_PASS      "1234567890"
#define EXAMPLE_MAX_STA_CONN       4

/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t wifi_event_group;

/* The event group allows multiple bits for each event,
   but we only care about one event - are we connected
   to the AP with an IP? */
const int WIFI_CONNECTED_BIT = BIT0;

static const char *TAG = "simple wifi";

void send_pkt_test(void* pData);

#define AMSDU_LEN   (81)
uint8_t amsdu_hdr[50] = {
    0xec, 0xfa, 0xbc, 0x1d, 0x33, 0x2e,  // MSDU DA
    0xF0, 0x1F, 0xAF, 0x15, 0x59, 0x02,  // MSDU SA
    0x04, 0x66,//0x04, 0x63,  // MSUD length
    0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00, 0x08, 0x00, // 802.2 LLC header
    0x45, 0x00, 0x05, 0xDA, 0x5A, 0xC7, 0x00, 0x00, 0x40, 0x11, 0xB8, 0xA2, 0xC0, 0xA5, 0x70, 0x58, 0xC0, 0xA5, 0x70, 0x06,  // IP header
    0xC3, 0x4D, 0x13, 0x89, 0x00, 0x12, 0xD5, 0xA8  // UDP header    
};

uint8_t mac_hdr[26] = {
    0x88, 0x02,  // qos data, frame control
    0x2C, 0x00,  // duration
    0xec, 0xfa, 0xbc, 0x1d, 0x33, 0x2e,  // MSDU DA
    //0xD0, 0xC7, 0xC0, 0x84, 0xB6, 0x02,  // SA, to be replaced with wifi mac, ssid: FAST_WEN
    //0xD0, 0xC7, 0xC0, 0x84, 0xB6, 0x02,  // BSSID
    0x38, 0x37, 0x8b, 0x92, 0xd0, 0x34,  // SA, to be replaced with wifi mac, ssid: TEST001
    0x38, 0x37, 0x8b, 0x92, 0xd0, 0x34,  // BSSID
    0xC0, 0x29,  // seq
    0x80, 0x00  // qos control field
};

uint8_t probe_req[53] = {
    0x40, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x30, 0xae, 0xa4, 0x15, 0x2c, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x00, 0x00, 0x0B, 
    0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, 0x64, //ssid
    0x01, 0x08, 0x8B, 0x96, 0x82, 0x84, 0x0c, 0x18, 0x30, 0x60, 
    0x32, 0x04, 0x6c, 0x12, 0x24, 0x48
    // 0x25, 0xb2, 0xbc, 0x01   //checksum 
};


uint8_t probe_req_ie[81] = {
    0x40, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x30, 0xae, 0xa4, 0x15, 0x2c, 0x10, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
    0x00, 0x00, 0x00, 0x0B, 
    0x73, 0x75, 0x6E, 0x6C, 0x75, 0x78, 0x69, 0x6E, 0x5F, 0x61, 0x70, //ssid
    0x01, 0x08, 0x8B, 0x96, 0x82, 0x84, 0x0c, 0x18, 0x30, 0x60, 
    0x32, 0x04, 0x6c, 0x12, 0x24, 0x48, 
    0xdd, 0x1a, 0x00, 0x50, 0xf2, 0x01, 0x01, 0x00, 0x00, 0x50, 0xf2, 0x02, 0x02, 0x00, 0x00, 0x50, 0xf2, 0x02, 0x00, 0x50, 0xf2, 0x04, 0x01,
    0x00, 0x00, 0x50, 0xf2, 0x02,//ie
    // 0xAC, 0x88, 0x25, 0xFD    //checksum 
};

void send_pkt_test(void* pData)
{
    int ret = 0;
    uint8_t payload[AMSDU_LEN] = {0x00};
    uint16_t payload_len = AMSDU_LEN;
    uint8_t ifx = WIFI_IF_STA;

    // memcpy(&payload[0], mac_hdr, 26);
    // memcpy(&payload[26], amsdu_hdr, 50);
    // memcpy(&payload[86], amsdu_hdr, 50);//1166
    memcpy(&payload[0], probe_req_ie, 81);
    vTaskDelay(2000 / portTICK_RATE_MS);
    printf(">>>>>>>>>>\n");
    for (int i=0; ievent_info;
    
    switch(event->event_id) {
    case SYSTEM_EVENT_STA_START:
        esp_wifi_connect();
        break;
    case SYSTEM_EVENT_STA_GOT_IP:
        ESP_LOGI(TAG, "got ip:%s",
                 ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
        xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_BIT);
        break;
    case SYSTEM_EVENT_AP_STACONNECTED:
        ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d",
                 MAC2STR(event->event_info.sta_connected.mac),
                 event->event_info.sta_connected.aid);
        break;
    case SYSTEM_EVENT_AP_STADISCONNECTED:
        ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d",
                 MAC2STR(event->event_info.sta_disconnected.mac),
                 event->event_info.sta_disconnected.aid);
        break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
        ESP_LOGE(TAG, "Disconnect reason : %d", info->disconnected.reason);
        // if (info->disconnected.reason == WIFI_REASON_BASIC_RATE_NOT_SUPPORT) {
        //     /*Switch to 802.11 bgn mode */
        //     esp_wifi_set_protocol(ESP_IF_WIFI_STA, WIFI_PROTOCAL_11B | WIFI_PROTOCAL_11G | WIFI_PROTOCAL_11N);
        // }
        esp_wifi_connect();
        xEventGroupClearBits(wifi_event_group, WIFI_CONNECTED_BIT);
        break;
    default:
        break;
    }
    return ESP_OK;
}

void wifi_init_softap()
{
    wifi_event_group = xEventGroupCreate();

    tcpip_adapter_init();
    ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    wifi_config_t wifi_config = {
        .ap = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .ssid_len = strlen(EXAMPLE_ESP_WIFI_SSID),
            .password = EXAMPLE_ESP_WIFI_PASS,
            .max_connection = EXAMPLE_MAX_STA_CONN,
            .authmode = WIFI_AUTH_WPA_WPA2_PSK,
            .channel = 1
        },
    };
    if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {
        wifi_config.ap.authmode = WIFI_AUTH_OPEN;
    }
printf("________%d______%s_____\n",__LINE__,__func__);
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());
    xTaskCreate(&send_pkt_test, "send_pkt_test", 8192, NULL, 6, NULL);
    ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s",
             EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}

void wifi_init_sta()
{
    wifi_event_group = xEventGroupCreate();

    tcpip_adapter_init();
    ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .password = EXAMPLE_ESP_WIFI_PASS,
            .channel = 5
        },
    };

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
    ESP_ERROR_CHECK(esp_wifi_start() );

    //xTaskCreate(&send_pkt_test, "send_pkt_test", 8192, NULL, 6, NULL);
    printf("________%d______%s_____\n",__LINE__,__func__);

    ESP_LOGI(TAG, "wifi_init_sta finished.");
    ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
             EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
    xTaskCreate(&send_pkt_test, "send_pkt_test", 8192, NULL, 6, NULL);
}

void app_main()
{
    //Initialize NVS
    esp_err_t ret = nvs_flash_init();
    if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
      ESP_ERROR_CHECK(nvs_flash_erase());
      ret = nvs_flash_init();
    }
    ESP_ERROR_CHECK(ret);
    
#if EXAMPLE_ESP_WIFI_MODE_AP
    ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
    wifi_init_softap();
#else
    ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
    wifi_init_sta();
#endif /*EXAMPLE_ESP_WIFI_MODE_AP*/

}

你可能感兴趣的:(物联网)