diff options
author | Ian C <ianc@noddybox.co.uk> | 2024-12-26 19:05:07 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2024-12-26 19:05:07 +0000 |
commit | aa33cb940bb31c74903fb7dc26e5d2f264064911 (patch) | |
tree | b9c5603ad71080b4f23a6b0473b33160e039f058 /wifi.c | |
parent | 80b1b51911e8c4221e918b50039caf7533751e4c (diff) |
Dev check in. Added tester.
Diffstat (limited to 'wifi.c')
-rw-r--r-- | wifi.c | 153 |
1 files changed, 145 insertions, 8 deletions
@@ -25,34 +25,171 @@ #include <stdlib.h> #include <stdio.h> +#include <string.h> +#include <stdarg.h> #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; } |