diff options
author | Ian C <ianc@noddybox.co.uk> | 2024-12-30 12:41:33 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2024-12-30 12:41:33 +0000 |
commit | 68f33af73d67785a2c6ecba6859d65145bc6ee95 (patch) | |
tree | 2f277cfdabec8d9a8f7f63cc224b649a4010f63c | |
parent | aa33cb940bb31c74903fb7dc26e5d2f264064911 (diff) |
-rw-r--r-- | dload.c | 16 | ||||
-rw-r--r-- | wifi.c | 85 | ||||
-rw-r--r-- | wifi.h | 10 |
3 files changed, 94 insertions, 17 deletions
@@ -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; } @@ -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; } @@ -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. */ |