summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xMakefile5
-rw-r--r--dload.c50
-rw-r--r--test.c17
-rw-r--r--wifi.c153
-rw-r--r--wifi.h39
5 files changed, 242 insertions, 22 deletions
diff --git a/Makefile b/Makefile
index b720b35..358061b 100755
--- a/Makefile
+++ b/Makefile
@@ -23,8 +23,11 @@
dload: dload.c wifi.c wifi.h
zcc +zxn -subtype=dot -create-app -o dload dload.c wifi.c
+test: test.c wifi.c wifi.h
+ $(CC) -g -o test test.c wifi.c
+
clean:
- rm -f dload dload_CODE.bin
+ rm -f dload dload_CODE.bin test
# If you use this, you'll almost certainly have to edit it
devinstall: dload
diff --git a/dload.c b/dload.c
index 82236e0..9431ea3 100644
--- a/dload.c
+++ b/dload.c
@@ -25,6 +25,8 @@
#include <stdlib.h>
#include <stdio.h>
+#include "wifi.h"
+
/* ---------------------------------------- MACROS
*/
@@ -47,7 +49,7 @@ static const char *dload_usage =
"more details.\n"
"\n"
"usage:\n"
-"dload http_url [dest_file]\n";
+".dload http_url [dest_file]\n";
/* ---------------------------------------- TYPES
@@ -60,17 +62,59 @@ static const char *dload_usage =
/* ---------------------------------------- PRIVATE FUNCTIONS
*/
+static const char *StatusCode(WifiStatus status)
+{
+ switch(status)
+ {
+ case eWifiNotAvailable:
+ return "Wifi not available";
+
+ case eWifiNotConnected:
+ return "Wifi not connected";
+
+ case eWifiUnknownHost:
+ return "Unknown host";
+
+ case eWifiFailedToWrite:
+ return "Failed to write to network";
+
+ case eWifiFailedToReceive:
+ return "Failed to read from network";
+
+ case eWifiTimeout:
+ return "Timeout";
+
+ case eWifiInvalidURL:
+ return "Invalid URL";
+
+ case eWifiOK:
+ return "OK";
+
+ default:
+ return "Error - unknown status code";
+ }
+}
/* ---------------------------------------- MAIN
*/
int main(int argc, char *argv[])
{
- if (!argv[1])
+ WifiStatus status;
+
+ if (argc < 2)
{
fprintf(stderr,"%s\n", dload_usage);
- exit(EXIT_FAILURE);
+ return EXIT_FAILURE;
+ }
+
+ /*
+ if ((status = WifiConnect()) != eWifiOK)
+ {
+ fprintf(stderr, "%s\n", StatusCode(status));
+ return EXIT_FAILURE;
}
+ */
return EXIT_SUCCESS;
}
diff --git a/test.c b/test.c
new file mode 100644
index 0000000..f5e4a37
--- /dev/null
+++ b/test.c
@@ -0,0 +1,17 @@
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "wifi.h"
+
+int main(int argc, char *argv[])
+{
+ int f;
+
+ for(f = 1; f < argc; f++)
+ {
+ printf("URL = '%s'\n", argv[f]);
+ ConnectURL("GET", argv[f]);
+ }
+
+ return EXIT_SUCCESS;
+}
diff --git a/wifi.c b/wifi.c
index eea5b39..55453f9 100644
--- a/wifi.c
+++ b/wifi.c
@@ -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;
}
diff --git a/wifi.h b/wifi.h
index b0b184a..9a8ee71 100644
--- a/wifi.h
+++ b/wifi.h
@@ -26,23 +26,42 @@
#ifndef DOTDLOAD_WIFI_H
#define DOTDLOAD_WIFI_H
-/* Connect to the wifi modem and return true if connection is successful.
- If the connection fails diagnostics will be printed to stderr.
+#include <stdarg.h>
+
+/* Status type
+*/
+typedef enum
+{
+ eWifiOK,
+ eWifiNotAvailable,
+ eWifiNotConnected,
+ eWifiUnknownHost,
+ eWifiFailedToWrite,
+ eWifiFailedToReceive,
+ eWifiTimeout,
+ eWifiInvalidURL
+} WifiStatus;
+
+/* Connect to the wifi modem and return status.
+*/
+WifiStatus WifiConnect(void);
+
+/* Connect to a URL. Returns status.
*/
-int WifiConnect(void);
+WifiStatus ConnectURL(const char *method, const char *url);
-/* Connect to a URL. Returns true if the connection is made.
+/* Sends a formatted string to the connection. Returns status.
*/
-int ConnectURL(const char *url);
+WifiStatus SendFormatted(const char *format, ...);
-/* Send a byte to the connection. Returns true if no errors.
+/* Send a byte to the connection. Returns status.
*/
-int SendByte(unsigned char c);
+WifiStatus SendByte(unsigned char c);
-/* Get a byte from the connection. Returns true if no errors and updates the
- passed pointer.
+/* Get a byte from the connection. Returns status and updates the passed
+ pointer.
*/
-int GetByte(unsigned char *c);
+WifiStatus GetByte(unsigned char *c);
#endif