|
| 1 | +#include <string.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <errno.h> |
| 4 | +#include <sys/types.h> |
| 5 | +#include <netdb.h> |
| 6 | + |
| 7 | +#ifndef UNIX |
| 8 | + #include "FreeRTOS.h" |
| 9 | + #include "task.h" |
| 10 | + |
| 11 | + #include "espressif/esp_common.h" |
| 12 | + #include "espressif/sdk_private.h" |
| 13 | + #include "FreeRTOS.h" |
| 14 | + #include "task.h" |
| 15 | + #include "queue.h" |
| 16 | + |
| 17 | + #include "ssid_config.h" |
| 18 | + |
| 19 | + #include "lwip/err.h" |
| 20 | + #include "lwip/sockets.h" |
| 21 | + #include "lwip/sys.h" |
| 22 | + #include "lwip/netdb.h" |
| 23 | + #include "lwip/dns.h" |
| 24 | +#endif |
| 25 | + |
| 26 | +#ifdef UNIX |
| 27 | + #include <sys/socket.h> |
| 28 | +#endif |
| 29 | + |
| 30 | +#include "compat.h" |
| 31 | + |
| 32 | +int http_get(char* buff, int size, char* url, char* server) { |
| 33 | + int successes = 0, failures = 0; |
| 34 | + |
| 35 | + printf("HTTP get task starting...\r\n"); |
| 36 | + |
| 37 | + const struct addrinfo hints = { |
| 38 | + .ai_family = AF_INET, |
| 39 | + .ai_socktype = SOCK_STREAM, |
| 40 | + }; |
| 41 | + struct addrinfo *res; |
| 42 | + |
| 43 | + printf("Running DNS lookup for %s...\r\n", url); |
| 44 | + int err = getaddrinfo(server, "80", &hints, &res); |
| 45 | + |
| 46 | + if (err != 0 || res == NULL) { |
| 47 | + printf("DNS lookup failed err=%d res=%p\r\n", err, res); |
| 48 | + if (res) |
| 49 | + freeaddrinfo(res); |
| 50 | + failures++; |
| 51 | + return 1; |
| 52 | + } |
| 53 | + |
| 54 | + /* Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */ |
| 55 | + struct in_addr *addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; |
| 56 | + printf("DNS lookup succeeded. IP=%s\r\n", inet_ntoa(*addr)); |
| 57 | + |
| 58 | + int s = socket(res->ai_family, res->ai_socktype, 0); |
| 59 | + if(s < 0) { |
| 60 | + printf("... Failed to allocate socket.\r\n"); |
| 61 | + freeaddrinfo(res); |
| 62 | + failures++; |
| 63 | + return 2; |
| 64 | + } |
| 65 | + |
| 66 | + printf("... allocated socket\r\n"); |
| 67 | + |
| 68 | + if(connect(s, res->ai_addr, res->ai_addrlen) != 0) { |
| 69 | + close(s); |
| 70 | + freeaddrinfo(res); |
| 71 | + printf("... socket connect failed.\r\n"); |
| 72 | + failures++; |
| 73 | + return 3; |
| 74 | + } |
| 75 | + |
| 76 | + printf("... connected\r\n"); |
| 77 | + freeaddrinfo(res); |
| 78 | + |
| 79 | + // TODO: not efficient? |
| 80 | + #define WRITE(msg) (write((s), (msg), strlen(msg)) < 0) |
| 81 | + if (WRITE("GET ") || |
| 82 | + WRITE(url) || |
| 83 | + WRITE("\r\n") || |
| 84 | + WRITE("User-Agent: esp-open-rtos/0.1 esp8266\r\n\r\n")) |
| 85 | + #undef WRITE |
| 86 | + { |
| 87 | + printf("... socket send failed\r\n"); |
| 88 | + close(s); |
| 89 | + failures++; |
| 90 | + return 4; |
| 91 | + } |
| 92 | + printf("... socket send success\r\n"); |
| 93 | + |
| 94 | + int r; |
| 95 | + char* p = buff; |
| 96 | + size--; // remove one for \0 |
| 97 | + do { |
| 98 | + bzero(buff, size); |
| 99 | + r = read(s, p, size); |
| 100 | + if (r > 0) { |
| 101 | + p += r; |
| 102 | + size -= r; |
| 103 | + // printf("%s", recv_buf); |
| 104 | + } |
| 105 | + } while (r > 0); |
| 106 | + |
| 107 | + printf("... done reading from socket. Last read return=%d errno=%d\r\n", r, errno); |
| 108 | + if (r != 0) |
| 109 | + failures++; |
| 110 | + else |
| 111 | + successes++; |
| 112 | + close(s); |
| 113 | + return 0; |
| 114 | +} |
0 commit comments