#include "esp8266.h"

void esp_8266_task(void)
{
    INT8U              err = 0;
    UartFrame_TypeDef *msg;
    INT8U              esp_state = 0;
    while (1) //等待倒计时
    {
        iwdg_feed(UART6_DOG);
        switch (esp_state)
        {
        case ESP_CWMODE:
            uart6_dma_send("AT+CWMODE=1\r\n", sizeof("AT+CWMODE=1\r\n"));
            OSTimeDly(1000);
            break;
        case ESP_RST:
            uart6_dma_send("AT+RST\r\n", sizeof("AT+RST\r\n"));
            OSTimeDly(1000);
            OSTimeDly(1000);
            OSTimeDly(1000);
            break;
        case ESP_CWJAP:
            uart6_dma_send("AT+CWJAP=\"FCC\",\"347470868\"\r\n", sizeof("AT+CWJAP=\"FCC\",\"347470868\"\r\n"));
            OSTimeDly(100);
            break;
        case ESP_MUX:
            uart6_dma_send("AT+CIPMUX=1\r\n", sizeof("AT+CIPMUX=1\r\n"));
            OSTimeDly(100);
            break;
        case ESP_SERVER:
            uart6_dma_send("AT+CIPSERVER=1,8086\r\n", sizeof("AT+CIPSERVER=1,8086\r\n"));
            OSTimeDly(100);
            break;
        case ESP_SEND_STATE:
            uart6_dma_send("AT+CIPSEND=0,25\r\n", sizeof("AT+CIPSEND=0,25\r\n"));
            OSTimeDly(200);
            break;
        case ESP_SEND_WORK:
            uart6_dma_send("dsadsadsadsadsadasds\r\n", sizeof("sadsadsadsadsadasds\r\n"));
            OSTimeDly(100);
            break;
        default:
            break;
        }

        msg = (UartFrame_TypeDef *)OSMboxPend(uart6_mbox, 500, &err);
        if ((err == OS_ERR_NONE) && (msg->len >= 0))
        {
            if (strstr((const char *)msg, (const char *)"OK") && esp_state == ESP_CWMODE)
            {
                esp_state = ESP_RST;
            }
            else if (strstr((const char *)msg, (const char *)"OK") && esp_state == ESP_RST)
            {
                // printf(msg->buf);
                esp_state = ESP_CWJAP;
            }
            else if (strstr((const char *)msg, (const char *)"WIFI GOT IP") && esp_state == ESP_CWJAP)
            {
                printf(msg->buf);
                esp_state = ESP_MUX;
            }
            else if (strstr((const char *)msg, (const char *)"OK") && esp_state == ESP_MUX)
            {
                printf(msg->buf);
                esp_state = ESP_SERVER;
            }
            else if (strstr((const char *)msg, (const char *)"OK") && esp_state == ESP_SERVER)
            {
                printf(msg->buf);
                esp_state = ESP_SEND_STATE;
            }
            else if (strstr((const char *)msg, (const char *)"OK") && esp_state == ESP_SEND_STATE)
            {
                printf(msg->buf);
                esp_state = ESP_SEND_WORK;
            }
            else if (strstr((const char *)msg, (const char *)"OK") && esp_state == ESP_SEND_WORK)
            {
                printf(msg->buf);
                esp_state = ESP_SEND_STATE;
            }
            else
            {
                printf(msg->buf);
                // return;
            }
        }
        OSTimeDly(100);
    }
}
