Created
March 26, 2018 02:11
-
-
Save piquan/6a9cdebbd86a9febf8f1b9ddb76d44d1 to your computer and use it in GitHub Desktop.
Failure repro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* HTTP GET Example using plain POSIX sockets | |
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 <string.h> | |
#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 "esp_log.h" | |
#include "nvs_flash.h" | |
#include "lwip/err.h" | |
#include "lwip/sockets.h" | |
#include "lwip/sys.h" | |
#include "lwip/netdb.h" | |
#include "lwip/dns.h" | |
#include "mbedtls/md.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_WIFI_SSID CONFIG_WIFI_SSID | |
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD | |
/* FreeRTOS event group to signal when we are connected & ready to make a request */ | |
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 CONNECTED_BIT = BIT0; | |
#define RECV_BUF_SIZE 16384 | |
//#define SIMPLE_READER | |
#define HEADERS_ON_UART | |
#define PROGRESS_ON_UART | |
//#define PROGRESS_ON_LED | |
//#define LED_GPIO_NUM GPIO_NUM_5 | |
//#define BYTES_PER_TICK 0x24 | |
/* Constants that aren't configurable in menuconfig */ | |
#define WEB_SERVER "s3-us-west-2.amazonaws.com" | |
#define WEB_PORT 80 | |
#define WEB_URL "/13c554ee-e4f7-4932-9183-0684b27f9bf8/testfile" | |
static const char *TAG = "example"; | |
static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n" | |
"Host: "WEB_SERVER"\r\n" | |
"User-Agent: esp-idf/1.0 esp32\r\n" | |
"\r\n"; | |
#ifdef PROGRESS_ON_LED | |
static bool led_state = false; | |
static void led_on(void) | |
{ | |
led_state = true; | |
ESP_ERROR_CHECK(gpio_set_level(LED_GPIO_NUM, true)); | |
} | |
static void led_off(void) | |
{ | |
led_state = false; | |
ESP_ERROR_CHECK(gpio_set_level(LED_GPIO_NUM, false)); | |
} | |
static void led_toggle(void) | |
{ | |
led_state = !led_state; | |
ESP_ERROR_CHECK(gpio_set_level(LED_GPIO_NUM, led_state)); | |
} | |
#endif | |
static esp_err_t event_handler(void *ctx, system_event_t *event) | |
{ | |
switch(event->event_id) { | |
case SYSTEM_EVENT_STA_START: | |
esp_wifi_connect(); | |
break; | |
case SYSTEM_EVENT_STA_GOT_IP: | |
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); | |
break; | |
case SYSTEM_EVENT_STA_DISCONNECTED: | |
/* This is a workaround as ESP32 WiFi libs don't currently | |
auto-reassociate. */ | |
esp_wifi_connect(); | |
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); | |
break; | |
default: | |
break; | |
} | |
return ESP_OK; | |
} | |
static void initialise_wifi(void) | |
{ | |
tcpip_adapter_init(); | |
wifi_event_group = xEventGroupCreate(); | |
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) ); | |
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) ); | |
wifi_config_t wifi_config = { | |
.sta = { | |
.ssid = EXAMPLE_WIFI_SSID, | |
.password = EXAMPLE_WIFI_PASS, | |
}, | |
}; | |
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid); | |
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() ); | |
} | |
static void http_get_task(void *pvParameters) | |
{ | |
const struct addrinfo hints = { | |
.ai_family = AF_INET, | |
.ai_socktype = SOCK_STREAM, | |
}; | |
struct addrinfo *res; | |
struct in_addr *addr; | |
int s, r; | |
char recv_buf[RECV_BUF_SIZE]; | |
while(1) { | |
/* Wait for the callback to set the CONNECTED_BIT in the | |
event group. | |
*/ | |
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, | |
false, true, portMAX_DELAY); | |
#ifdef PROGRESS_ON_LED | |
led_on(); | |
#endif | |
ESP_LOGI(TAG, "Connected to AP"); | |
int err = getaddrinfo(WEB_SERVER, "80", &hints, &res); | |
if(err != 0 || res == NULL) { | |
ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res); | |
vTaskDelay(1000 / portTICK_PERIOD_MS); | |
continue; | |
} | |
/* Code to print the resolved IP. | |
Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */ | |
addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; | |
ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", inet_ntoa(*addr)); | |
s = socket(res->ai_family, res->ai_socktype, 0); | |
if(s < 0) { | |
ESP_LOGE(TAG, "... Failed to allocate socket."); | |
freeaddrinfo(res); | |
vTaskDelay(1000 / portTICK_PERIOD_MS); | |
continue; | |
} | |
ESP_LOGI(TAG, "... allocated socket"); | |
if(connect(s, res->ai_addr, res->ai_addrlen) != 0) { | |
ESP_LOGE(TAG, "... socket connect failed errno=%d", errno); | |
close(s); | |
freeaddrinfo(res); | |
vTaskDelay(4000 / portTICK_PERIOD_MS); | |
continue; | |
} | |
ESP_LOGI(TAG, "... connected"); | |
freeaddrinfo(res); | |
if (write(s, REQUEST, strlen(REQUEST)) < 0) { | |
ESP_LOGE(TAG, "... socket send failed"); | |
close(s); | |
vTaskDelay(4000 / portTICK_PERIOD_MS); | |
continue; | |
} | |
ESP_LOGI(TAG, "... socket send success"); | |
struct timeval receiving_timeout; | |
receiving_timeout.tv_sec = 5; | |
receiving_timeout.tv_usec = 0; | |
if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &receiving_timeout, | |
sizeof(receiving_timeout)) < 0) { | |
ESP_LOGE(TAG, "... failed to set socket receiving timeout"); | |
close(s); | |
vTaskDelay(4000 / portTICK_PERIOD_MS); | |
continue; | |
} | |
ESP_LOGI(TAG, "... set socket receiving timeout success"); | |
#ifdef SIMPLE_READER | |
/* Read HTTP response */ | |
do { | |
bzero(recv_buf, sizeof(recv_buf)); | |
r = read(s, recv_buf, sizeof(recv_buf)-1); | |
for(int i = 0; i < r; i++) { | |
//putchar(recv_buf[i]); | |
} | |
} while(r > 0); | |
#else | |
const mbedtls_md_info_t *hash_info = | |
mbedtls_md_info_from_type(MBEDTLS_MD_MD5); | |
mbedtls_md_context_t hash_ctx; | |
mbedtls_md_setup(&hash_ctx, hash_info, 0); | |
mbedtls_md_starts(&hash_ctx); | |
/* Read HTTP response */ | |
enum { | |
kInHeader, | |
kLineEnd, | |
kHeadersEnd | |
} http_state = kInHeader; | |
int i; | |
do { | |
bzero(recv_buf, sizeof(recv_buf)); | |
r = read(s, recv_buf, sizeof(recv_buf)-1); | |
for (i = 0; i < r && http_state != kHeadersEnd; i++) { | |
#ifdef HEADERS_ON_UART | |
putchar(recv_buf[i]); | |
#endif | |
if (recv_buf[i] == '\n') { | |
http_state = (http_state == kInHeader ? kLineEnd : | |
http_state == kLineEnd ? kHeadersEnd : | |
0 /* canthappen */); | |
} else if (recv_buf[i] == '\r') { | |
// No state change | |
} else { | |
http_state = kInHeader; | |
} | |
} | |
} while(r > 0 && http_state != kHeadersEnd); | |
#ifdef HEADERS_ON_UART | |
fflush(stdout); | |
#endif | |
if (r > 0 && i < r) { | |
mbedtls_md_update(&hash_ctx, (uint8_t*)recv_buf + i, r - i); | |
} | |
do { | |
#ifdef PROGRESS_ON_LED | |
led_toggle(); | |
#endif | |
bzero(recv_buf, sizeof(recv_buf)); | |
while (1) { | |
r = read(s, recv_buf, sizeof(recv_buf)-1); | |
if (r < 0 && errno == EAGAIN) { | |
#ifdef PROGRESS_ON_UART | |
putchar('_'); | |
fflush(stdout); | |
#endif | |
} else { | |
break; | |
} | |
} | |
if (r > 0) { | |
mbedtls_md_update(&hash_ctx, (uint8_t*)recv_buf, r); | |
#ifdef PROGRESS_ON_UART | |
putchar(r < 512 ? '.' : // 1-511 | |
r < 1024 ? ',' : // 512-1023 | |
r < 2048 ? ':' : // 1024-2047 | |
r < 4096 ? ';' : // 2048-4095 | |
r < 8192 ? '!' : // 4096-8191 | |
r < 16384 ? '|' : // 8192-16383 | |
'*'); // 16834- | |
fflush(stdout); | |
#endif | |
#ifdef BYTES_PER_TICK | |
vTaskDelay(r / BYTES_PER_TICK); | |
#endif | |
} | |
} while(r > 0); | |
uint8_t hash_result[mbedtls_md_get_size(hash_info)]; | |
mbedtls_md_finish(&hash_ctx, hash_result); | |
mbedtls_md_free(&hash_ctx); | |
fputs("\n... hash of HTTP body: ", stdout); | |
for (int i = 0; i < sizeof(hash_result); i++) { | |
printf("%02x", hash_result[i]); | |
} | |
putchar('\n'); | |
fflush(stdout); | |
#endif // SIMPLE_READER | |
ESP_LOGI(TAG, "... done reading from socket. Last read return=%d errno=%d\r\n", r, errno); | |
#ifdef PROGRESS_ON_LED | |
led_off(); | |
#endif | |
close(s); | |
for(int countdown = 10; countdown >= 0; countdown--) { | |
ESP_LOGI(TAG, "%d... ", countdown); | |
vTaskDelay(1000 / portTICK_PERIOD_MS); | |
} | |
ESP_LOGI(TAG, "Starting again!"); | |
} | |
} | |
void app_main() | |
{ | |
#ifdef PROGRESS_ON_LED | |
const gpio_config_t led_cfg = { | |
.pin_bit_mask = ((uint64_t)1 << LED_GPIO_NUM), | |
.mode = GPIO_MODE_OUTPUT, | |
.pull_up_en = GPIO_PULLUP_DISABLE, | |
.pull_down_en = GPIO_PULLDOWN_DISABLE, | |
.intr_type = GPIO_INTR_DISABLE, | |
}; | |
ESP_ERROR_CHECK(gpio_config(&led_cfg)); | |
#endif | |
ESP_ERROR_CHECK( nvs_flash_init() ); | |
initialise_wifi(); | |
xTaskCreate(&http_get_task, "http_get_task", 65536, NULL, 5, NULL); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment