From 68f33af73d67785a2c6ecba6859d65145bc6ee95 Mon Sep 17 00:00:00 2001 From: Ian C Date: Mon, 30 Dec 2024 12:41:33 +0000 Subject: Development over Christmas. --- dload.c | 16 ++++++++++--- wifi.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- wifi.h | 10 +++++--- 3 files changed, 94 insertions(+), 17 deletions(-) diff --git a/dload.c b/dload.c index 9431ea3..ae78e87 100644 --- a/dload.c +++ b/dload.c @@ -87,6 +87,12 @@ static const char *StatusCode(WifiStatus status) case eWifiInvalidURL: return "Invalid URL"; + case eWifiFailedToSendCommand: + return "Failed to send command to modem"; + + case eWifiBadHTTPStatusCode: + return "Bad HTTP status code"; + case eWifiOK: return "OK"; @@ -108,13 +114,17 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - /* if ((status = WifiConnect()) != eWifiOK) { - fprintf(stderr, "%s\n", StatusCode(status)); + fprintf(stderr, "WifiConnect: %s\n", StatusCode(status)); + return EXIT_FAILURE; + } + + if ((status = ConnectURL(0, argv[1], 0)) != eWifiOK) + { + fprintf(stderr, "ConnectURL: %s\n", StatusCode(status)); return EXIT_FAILURE; } - */ return EXIT_SUCCESS; } diff --git a/wifi.c b/wifi.c index 55453f9..1891359 100644 --- a/wifi.c +++ b/wifi.c @@ -36,11 +36,6 @@ /* ---------------------------------------- PRIVATE ROUTINES */ -static WifiStatus ConnectToModem(void) -{ - return eWifiTimeout; -} - static WifiStatus SendToModem(const char *buffer, size_t len) { while(len) @@ -59,6 +54,11 @@ static WifiStatus SendToModem(const char *buffer, size_t len) return eWifiOK; } +static WifiStatus ConnectToModem(void) +{ + return eWifiTimeout; +} + static void Copy(const char *from, const char *to, char *buff, size_t maxlen) { while(maxlen && ((to && from != to) || (!to && *from))) @@ -142,9 +142,67 @@ static int ParseURL(const char *url, strcpy(path, "/"); } + if (!hostname[0]) + { + return 0; + } + return 1; } +static void Chomp(char *p) +{ + size_t l = strlen(p); + + while(l && (p[l - 1] == '\r' || p[l - 1] == '\n')) + { + p[--l] = 0; + } +} + +static WifiStatus ReadLine(char *buff, size_t maxlen) +{ + size_t len = 0; + WifiStatus status; + unsigned char c; + + while(len < maxlen && ((status = GetByte(&c)) == eWifiOK)) + { + buff[len++] = c; + buff[len] = 0; + + if (len > 1) + { + if (buff[len - 1] == '\n' && buff[len - 2] == '\r') + { + Chomp(buff); + return eWifiOK; + } + } + } + + return eWifiFailedToReceive; +} + +static WifiStatus SendATCommand(const char *command) +{ + char line[80]; + + WifiStatus status; + + if((status = SendToModem(command, strlen(command))) != eWifiOK) + { + return status; + } + + if((status = ReadLine(line, sizeof line)) != eWifiOK) + { + return status; + } + + return strcmp("OK", line) == 0 ? eWifiOK : eWifiFailedToSendCommand; +} + /* ---------------------------------------- PUBLIC ROUTINES */ @@ -153,18 +211,23 @@ WifiStatus WifiConnect(void) return ConnectToModem(); } -WifiStatus ConnectURL(const char *method, const char *url) +WifiStatus ConnectURL(int is_post, const char *url, size_t *content_length) { char hostname[MAX_HOSTNAME_LEN]; char path[MAX_PATH_LEN]; int port; int ssl; - WifiStatus status = ParseURL(url, hostname, path, &port, &ssl); + WifiStatus status; - printf("hostname='%s'\n", hostname); - printf("path='%s'\n", path); - printf("port=%d\n", port); - printf("ssl=%d\n", ssl); + if (!ParseURL(url, hostname, path, &port, &ssl)) + { + return eWifiInvalidURL; + } + + if ((status = SendATCommand("AT+CIPMUX=0\r\n")) != eWifiOK) + { + return status; + }` return eWifiTimeout; } diff --git a/wifi.h b/wifi.h index 9a8ee71..92a48a3 100644 --- a/wifi.h +++ b/wifi.h @@ -39,16 +39,20 @@ typedef enum eWifiFailedToWrite, eWifiFailedToReceive, eWifiTimeout, - eWifiInvalidURL + eWifiInvalidURL, + eWifiFailedToSendCommand, + eWifiBadHTTPStatusCode } WifiStatus; /* Connect to the wifi modem and return status. */ WifiStatus WifiConnect(void); -/* Connect to a URL. Returns status. +/* Connect to a URL. Returns status. If it is a post then *content_len holds + the length of the posted file. If it is not a post (a get) then + *content_len holds the content length from the respose. */ -WifiStatus ConnectURL(const char *method, const char *url); +WifiStatus ConnectURL(int is_post, const char *url, size_t *content_len); /* Sends a formatted string to the connection. Returns status. */ -- cgit v1.2.3