From aa33cb940bb31c74903fb7dc26e5d2f264064911 Mon Sep 17 00:00:00 2001 From: Ian C Date: Thu, 26 Dec 2024 19:05:07 +0000 Subject: Dev check in. Added tester. --- wifi.c | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 145 insertions(+), 8 deletions(-) (limited to 'wifi.c') diff --git a/wifi.c b/wifi.c index eea5b39..55453f9 100644 --- a/wifi.c +++ b/wifi.c @@ -25,34 +25,171 @@ #include #include +#include +#include #include "wifi.h" +#define MAX_PROTOCOL_LEN 10 +#define MAX_HOSTNAME_LEN 255 +#define MAX_PATH_LEN 255 + /* ---------------------------------------- PRIVATE ROUTINES */ +static WifiStatus ConnectToModem(void) +{ + return eWifiTimeout; +} + +static WifiStatus SendToModem(const char *buffer, size_t len) +{ + while(len) + { + WifiStatus status; + + if ((status = SendByte(*buffer)) != eWifiOK) + { + return status; + } + + buffer++; + len--; + } + + return eWifiOK; +} + +static void Copy(const char *from, const char *to, char *buff, size_t maxlen) +{ + while(maxlen && ((to && from != to) || (!to && *from))) + { + *buff++ = *from++; + maxlen--; + } + + if (maxlen) + { + *buff = 0; + } + else + { + *--buff = 0; + } +} + +static int ParseURL(const char *url, + char *hostname, + char *path, + int *port, + int *ssl) +{ + char protocol[MAX_PROTOCOL_LEN]; + const char *double_slash; + const char *colon; + const char *slash; + + double_slash = strstr(url, "//"); + + if (double_slash && double_slash != url) + { + Copy(url, double_slash - 1, protocol, MAX_PROTOCOL_LEN); + } + else + { + strcpy(protocol, "http"); + } + + colon = strchr(double_slash ? double_slash + 2 : url, ':'); + slash = strchr(double_slash ? double_slash + 2 : url, '/'); + + if (colon) + { + char buff[10]; + + Copy(colon + 1, slash, buff, 10); + *port = atoi(buff); + + Copy(double_slash ? double_slash + 2 : url, colon, + hostname, MAX_HOSTNAME_LEN); + } + else + { + if (strcmp(protocol, "http") == 0) + { + *port = 80; + } + else if (strcmp(protocol, "https") == 0) + { + *port = 443; + } + else + { + return 0; + } + + Copy(double_slash ? double_slash + 2 : url, slash, + hostname, MAX_HOSTNAME_LEN); + } + + *ssl = (strcmp(protocol, "https") == 0); + + if (slash) + { + Copy(slash, NULL, path, MAX_PATH_LEN); + } + else + { + strcpy(path, "/"); + } + + return 1; +} /* ---------------------------------------- PUBLIC ROUTINES */ -int WifiConnect(void) +WifiStatus WifiConnect(void) { - return 0; + return ConnectToModem(); } -int ConnectURL(const char *url) +WifiStatus ConnectURL(const char *method, const char *url) { - return 0; + char hostname[MAX_HOSTNAME_LEN]; + char path[MAX_PATH_LEN]; + int port; + int ssl; + WifiStatus status = ParseURL(url, hostname, path, &port, &ssl); + + printf("hostname='%s'\n", hostname); + printf("path='%s'\n", path); + printf("port=%d\n", port); + printf("ssl=%d\n", ssl); + + return eWifiTimeout; +} + +WifiStatus SendFormatted(const char *format, ...) +{ + char buff[256]; + va_list va; + + va_start(va, format); + vsnprintf(buff, sizeof buff, format, va); + va_end(va); + + return SendToModem(buff, strlen(buff)); } -int SendByte(unsigned char c) +WifiStatus SendByte(unsigned char c) { - return 0; + return eWifiTimeout; } -int GetByte(unsigned char *c) +WifiStatus GetByte(unsigned char *c) { *c = 0; - return 0; + return eWifiTimeout; } -- cgit v1.2.3