/* dload - Dot command for downloading files on the Spectrum Next Copyright (C) 2025 Ian Cowburn (ianc@noddybox.co.uk) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . -------------------------------------------------------------------------- Wifi interface */ #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 */ WifiStatus WifiConnect(void) { return ConnectToModem(); } WifiStatus ConnectURL(const char *method, const char *url) { 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)); } WifiStatus SendByte(unsigned char c) { return eWifiTimeout; } WifiStatus GetByte(unsigned char *c) { *c = 0; return eWifiTimeout; } /* vim: ai sw=4 ts=8 expandtab */