summaryrefslogtreecommitdiff
path: root/wifi.c
blob: 55453f9b244e98e7cf109fe631edbb19607c5a07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*

    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 <http://www.gnu.org/licenses/>.

    --------------------------------------------------------------------------

    Wifi interface

*/

#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
*/
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
*/