瀏覽代碼

use lwip httpd instead of bitty http

Thomas Buck 11 月之前
父節點
當前提交
c145989048
共有 12 個文件被更改,包括 29 次插入583 次删除
  1. 0
    3
      .gitmodules
  2. 0
    1
      BittyHTTP
  3. 10
    11
      CMakeLists.txt
  4. 1
    1
      README.md
  5. 0
    47
      conf/Options.h
  6. 8
    0
      conf/lwipopts.h
  7. 8
    0
      fs/index.html
  8. 0
    2
      include/http.h
  9. 2
    18
      src/http.c
  10. 0
    101
      src/http_files.c
  11. 0
    395
      src/http_socket.c
  12. 0
    4
      src/main.c

+ 0
- 3
.gitmodules 查看文件

@@ -13,6 +13,3 @@
13 13
 [submodule "picowota"]
14 14
 	path = picowota
15 15
 	url = https://github.com/usedbytes/picowota
16
-[submodule "BittyHTTP"]
17
-	path = BittyHTTP
18
-	url = https://github.com/TheBeef/BittyHTTP

+ 0
- 1
BittyHTTP

@@ -1 +0,0 @@
1
-Subproject commit 0c531a062fe44ce9ee372361e14906924737ced1

+ 10
- 11
CMakeLists.txt 查看文件

@@ -24,10 +24,15 @@ execute_process(COMMAND make
24 24
     WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/mcufont/fonts
25 25
 )
26 26
 
27
-# remove Options.h from BittyHTTP so we can use our own (TODO ugly)
28
-execute_process(COMMAND rm Options.h
29
-    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/BittyHTTP/src
27
+# build lwip httpd fs
28
+set(MAKE_FS_DATA_SCRIPT ${CMAKE_CURRENT_SOURCE_DIR}/pico-sdk/lib/lwip/src/apps/http/makefsdata/makefsdata)
29
+execute_process(
30
+    COMMAND perl ${MAKE_FS_DATA_SCRIPT}
31
+    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
32
+    ECHO_OUTPUT_VARIABLE
33
+    ECHO_ERROR_VARIABLE
30 34
 )
35
+file(RENAME ${CMAKE_CURRENT_SOURCE_DIR}/fsdata.c ${CMAKE_CURRENT_BINARY_DIR}/httpd_fsdata.c)
31 36
 
32 37
 # initialize pico-sdk from submodule
33 38
 include(pico-sdk/pico_sdk_init.cmake)
@@ -107,8 +112,6 @@ target_sources(gadget PUBLIC
107 112
     src/state_wifi_edit.c
108 113
     src/state_string.c
109 114
     src/http.c
110
-    src/http_socket.c
111
-    src/http_files.c
112 115
 
113 116
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ff.c
114 117
     ${CMAKE_CURRENT_BINARY_DIR}/fatfs/ffunicode.c
@@ -123,8 +126,6 @@ target_sources(gadget PUBLIC
123 126
     mcufont/decoder/mf_bwfont.c
124 127
     mcufont/decoder/mf_scaledfont.c
125 128
     mcufont/decoder/mf_wordwrap.c
126
-
127
-    BittyHTTP/src/WebServer.c
128 129
 )
129 130
 
130 131
 # external dependency include directories
@@ -136,7 +137,7 @@ target_include_directories(gadget PUBLIC ${CMAKE_CURRENT_LIST_DIR}/st7789/interf
136 137
 target_include_directories(gadget PUBLIC ${CMAKE_CURRENT_LIST_DIR}/mcufont/decoder)
137 138
 target_include_directories(gadget PUBLIC ${CMAKE_CURRENT_LIST_DIR}/data)
138 139
 target_include_directories(gadget PUBLIC ${CMAKE_CURRENT_LIST_DIR}/build)
139
-target_include_directories(gadget PUBLIC ${CMAKE_CURRENT_LIST_DIR}/BittyHTTP/src)
140
+target_include_directories(gadget PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
140 141
 
141 142
 # compress source code and stuff we want to include
142 143
 add_custom_target(pack bash -c "./pack_data.sh"
@@ -179,6 +180,7 @@ target_link_libraries(gadget
179 180
     hardware_flash
180 181
     pico_flash
181 182
     picowota_reboot
183
+    pico_lwip_http
182 184
 )
183 185
 
184 186
 pico_set_linker_script(gadget ${CMAKE_CURRENT_SOURCE_DIR}/conf/memmap_custom.ld)
@@ -190,14 +192,11 @@ set(PICOWOTA_WIFI_SSID "WIFI_SSID_HERE")
190 192
 set(PICOWOTA_WIFI_PASS "WIFI_PASS_HERE")
191 193
 set(PICOWOTA_WIFI_AP 0)
192 194
 
193
-# TODO set DOCVER to hash of static http files?
194
-
195 195
 target_compile_definitions(gadget PUBLIC
196 196
     RUNNING_AS_CLIENT=1
197 197
     DEFAULT_WIFI_SSID="${PICOWOTA_WIFI_SSID}"
198 198
     DEFAULT_WIFI_PASS="${PICOWOTA_WIFI_PASS}"
199 199
     CYW43_HOST_NAME="pico-volcano"
200
-    DOCVER="1.0.0.0"
201 200
 )
202 201
 
203 202
 add_subdirectory(picowota)

+ 1
- 1
README.md 查看文件

@@ -8,7 +8,7 @@ Supports:
8 8
 
9 9
 For use with Raspberry Pi Pico W boards with the [Waveshare Pico LCD 1.3](https://www.waveshare.com/wiki/Pico-LCD-1.3) and the [Pimoroni Pico Lipo Shim](https://shop.pimoroni.com/products/pico-lipo-shim).
10 10
 
11
-Adapted from the [tinyusb-cdc-example](https://github.com/hathach/tinyusb/blob/master/examples/device/cdc_msc/src/main.c), [adc example](https://github.com/raspberrypi/pico-examples/tree/master/adc/read_vsys), [standalone client example](https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/standalone/client.c) and my [Trackball firmware](https://git.xythobuz.de/thomas/Trackball).
11
+Adapted from the [tinyusb-cdc-example](https://github.com/hathach/tinyusb/blob/master/examples/device/cdc_msc/src/main.c), [adc example](https://github.com/raspberrypi/pico-examples/tree/master/adc/read_vsys), [standalone client example](https://github.com/raspberrypi/pico-examples/blob/master/pico_w/bt/standalone/client.c), [webserver example](https://github.com/krzmaz/pico-w-webserver-example) and my [Trackball firmware](https://git.xythobuz.de/thomas/Trackball).
12 12
 
13 13
 `python-test` contains a similar app to the C version in the top level of the repo, but instead written for MicroPython on the Pico W.
14 14
 Unfortunately I had many performance and space problems with this, so I decided to rewrite it.

+ 0
- 47
conf/Options.h 查看文件

@@ -1,47 +0,0 @@
1
-/*
2
- * Options.h
3
- *
4
- * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
5
- * Copyright (c) 2019 Paul Hutchinson
6
- *
7
- * Permission is hereby granted, free of charge, to any person obtaining a copy
8
- * of this software and associated documentation files (the "Software"), to deal
9
- * in the Software without restriction, including without limitation the rights
10
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- * copies of the Software, and to permit persons to whom the Software is
12
- * furnished to do so, subject to the following conditions:
13
- *
14
- * The above copyright notice and this permission notice shall be included in all
15
- * copies or substantial portions of the Software.
16
- *
17
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
- * SOFTWARE.
24
- */
25
-
26
-#ifndef __OPTIONS_H_
27
-#define __OPTIONS_H_
28
-
29
-#ifndef DOCVER
30
-#define DOCVER "1.0.0.0"
31
-#endif
32
-
33
-// The max number of connections we can handle at the same time
34
-// (this will include buffers needed for each connection)
35
-#define WS_OPT_MAX_CONNECTIONS              2
36
-
37
-// The memory block to use to store the cookies, get args, and post args
38
-#define WS_OPT_ARG_MEMORY_SIZE              100
39
-
40
-// How many seconds to wait after a connection stops sending to us before we hang up
41
-#define WS_SECONDS_UNTIL_CONNECTION_RELEASE 5
42
-
43
-// The max number of bytes we can handle a single header line can be (including the GET line).
44
-// This is normally in the order of 16K - 128K (we default to a lot less)
45
-#define WS_LINE_BUFFER_SIZE                 256
46
-
47
-#endif // __OPTIONS_H_

+ 8
- 0
conf/lwipopts.h 查看文件

@@ -73,4 +73,12 @@
73 73
 #define SLIP_DEBUG                  LWIP_DBG_OFF
74 74
 #define DHCP_DEBUG                  LWIP_DBG_OFF
75 75
 
76
+#define LWIP_HTTPD 1
77
+#define LWIP_HTTPD_SSI 1
78
+#define LWIP_HTTPD_CGI 1
79
+// don't include the tag comment - less work for the CPU, but may be harder to debug
80
+#define LWIP_HTTPD_SSI_INCLUDE_TAG 0
81
+// use generated fsdata
82
+#define HTTPD_FSDATA_FILE "httpd_fsdata.c"
83
+
76 84
 #endif /* __LWIPOPTS_H__ */

+ 8
- 0
fs/index.html 查看文件

@@ -0,0 +1,8 @@
1
+<html>
2
+    <head>
3
+        <title>Volcano Remote</title>
4
+    </head>
5
+    <body>
6
+        <h1>Hello World</h1>
7
+    </body>
8
+</html>

+ 0
- 2
include/http.h 查看文件

@@ -20,7 +20,5 @@
20 20
 #define __HTTP_H__
21 21
 
22 22
 void http_init(void);
23
-void http_deinit(void);
24
-void http_run(void);
25 23
 
26 24
 #endif // __HTTP_H__

+ 2
- 18
src/http.c 查看文件

@@ -16,28 +16,12 @@
16 16
  * See <http://www.gnu.org/licenses/>.
17 17
  */
18 18
 
19
-#include "WebServer.h"
19
+#include "lwip/apps/httpd.h"
20 20
 
21 21
 #include "config.h"
22 22
 #include "log.h"
23 23
 #include "http.h"
24 24
 
25 25
 void http_init(void) {
26
-    SocketsCon_InitSocketConSystem();
27
-    WS_Init();
28
-
29
-    if (!WS_Start(80)) {
30
-        debug("failed to start web server");
31
-    } else {
32
-        debug("listening on :80");
33
-    }
34
-}
35
-
36
-void http_deinit(void) {
37
-    WS_Shutdown();
38
-    SocketsCon_ShutdownSocketConSystem();
39
-}
40
-
41
-void http_run(void) {
42
-    WS_Tick();
26
+    httpd_init();
43 27
 }

+ 0
- 101
src/http_files.c 查看文件

@@ -1,101 +0,0 @@
1
-/*
2
- * http_files.c
3
- *
4
- * Based on BittyHTTP example.
5
- *
6
- * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
7
- * Copyright (c) 2019 Paul Hutchinson
8
- *
9
- * Permission is hereby granted, free of charge, to any person obtaining a copy
10
- * of this software and associated documentation files (the "Software"), to deal
11
- * in the Software without restriction, including without limitation the rights
12
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
- * copies of the Software, and to permit persons to whom the Software is
14
- * furnished to do so, subject to the following conditions:
15
- *
16
- * The above copyright notice and this permission notice shall be included in all
17
- * copies or substantial portions of the Software.
18
- *
19
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
- * SOFTWARE.
26
- */
27
-
28
-#include <string.h>
29
-
30
-#include "WebServer.h"
31
-
32
-#include "config.h"
33
-#include "log.h"
34
-#include "http.h"
35
-
36
-struct FileInfo {
37
-    const char *Filename; // With Path
38
-    bool Dynamic;
39
-    const char **Cookies;
40
-    const char **Gets;
41
-    const char **Posts;
42
-    void (*WriteFile)(struct WebServer *Web);
43
-};
44
-
45
-void File_Root(struct WebServer *Web);
46
-
47
-static struct FileInfo m_Files[] =  {
48
-    // Filename, Dynamic, Cookies, Gets, Posts, Callback
49
-    {       "/",   false,    NULL, NULL,  NULL, File_Root },
50
-};
51
-
52
-bool FS_GetFileProperties(const char *Filename, struct WSPageProp *PageProp) {
53
-    PageProp->FileID = 0;
54
-
55
-    for (size_t r = 0; r < sizeof(m_Files) / sizeof(struct FileInfo); r++) {
56
-        if (strcmp(Filename, m_Files[r].Filename) != 0) {
57
-            continue;
58
-        }
59
-
60
-        PageProp->FileID = (uintptr_t)&m_Files[r];
61
-        PageProp->DynamicFile = m_Files[r].Dynamic;
62
-        PageProp->Cookies = m_Files[r].Cookies;
63
-        PageProp->Gets = m_Files[r].Gets;
64
-        PageProp->Posts = m_Files[r].Posts;
65
-
66
-        debug("serving '%s'", Filename);
67
-        return true;
68
-    }
69
-
70
-    debug("unknown file '%s'", Filename);
71
-    return false;
72
-}
73
-
74
-void FS_SendFile(struct WebServer *Web, uintptr_t FileID) {
75
-    struct FileInfo *File = (struct FileInfo *)FileID;
76
-    if ((Web == NULL) || (File == NULL)) {
77
-        debug("invalid param");
78
-        return;
79
-    }
80
-
81
-    File->WriteFile(Web);
82
-}
83
-
84
-t_ElapsedTime ReadElapsedClock(void) {
85
-    return to_ms_since_boot(get_absolute_time()) / 1000;
86
-}
87
-
88
-const char RootHTML[]=
89
-"<!DOCTYPE html>"
90
-"<html>"
91
-"<head>"
92
-    "<title>Volcano Remote</title>"
93
-"</head>"
94
-"<body>"
95
-    "<h1>Hello World</h1>"
96
-"</body>"
97
-"</html>";
98
-
99
-void File_Root(struct WebServer *Web) {
100
-    WS_WriteWhole(Web,RootHTML, sizeof(RootHTML) - 1);
101
-}

+ 0
- 395
src/http_socket.c 查看文件

@@ -1,395 +0,0 @@
1
-/*
2
- * http_socket.c
3
- *
4
- * Based on BittyHTTP example socket interface.
5
- *
6
- * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
7
- *
8
- * This program is free software: you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation, either version 3 of the License, or
11
- * (at your option) any later version.
12
- *
13
- * This program is distributed in the hope that it will be useful,
14
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
- * GNU General Public License for more details.
17
- *
18
- * See <http://www.gnu.org/licenses/>.
19
- */
20
-
21
-#include <string.h>
22
-
23
-#include "lwip/tcp.h"
24
-#include "lwip/dns.h"
25
-#include "SocketsCon.h"
26
-
27
-#include "config.h"
28
-#include "log.h"
29
-#include "main.h"
30
-#include "ring.h"
31
-#include "http.h"
32
-
33
-#define MAX_SOCK 4
34
-#define SOCK_RECV_BUFF 512
35
-
36
-#define HTTP_DNS_TIMEOUT_MS 5000
37
-#define HTTP_CONNECT_TIMEOUT_MS 5000
38
-
39
-struct tcp_sock {
40
-    bool set;
41
-    struct tcp_pcb *pcb;
42
-
43
-    // TODO listening server socket has unused buffer
44
-    uint8_t rx_buf[SOCK_RECV_BUFF];
45
-    struct ring_buffer rx_rb;
46
-
47
-    struct tcp_pcb *child_buf[MAX_SOCK];
48
-    struct ring_buffer child_rb;
49
-};
50
-
51
-static struct tcp_sock sock[MAX_SOCK] = {0};
52
-
53
-bool SocketsCon_InitSocketConSystem(void) {
54
-    return true;
55
-}
56
-
57
-void SocketsCon_ShutdownSocketConSystem(void) { }
58
-
59
-static void tcp_server_err(void *arg, err_t err) {
60
-    debug("tcp error %d", err);
61
-
62
-    struct SocketCon *Con = arg;
63
-    Con->ErrorCode = e_ConnectErrorMAX;
64
-    Con->State = e_ConnectState_Error;
65
-}
66
-
67
-static err_t tcp_server_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err) {
68
-    (void)tpcb;
69
-    debug("tcp recv %d %d", p->len, err);
70
-
71
-    struct SocketCon *Con = arg;
72
-    size_t idx = Con->SocketFD;
73
-
74
-    if (rb_space(&sock[idx].rx_rb) < p->len) {
75
-        debug("not enough space (%d < %d)", rb_space(&sock[idx].rx_rb), p->len);
76
-        tcp_abort(sock[idx].pcb);
77
-        Con->ErrorCode = e_ConnectErrorMAX;
78
-        Con->State = e_ConnectState_Error;
79
-        return ERR_ABRT;
80
-    }
81
-
82
-    rb_add(&sock[idx].rx_rb, p->payload, p->len);
83
-    return ERR_OK;
84
-}
85
-
86
-bool SocketsCon_InitSockCon(struct SocketCon *Con) {
87
-    if (!Con) {
88
-        debug("invalid param");
89
-        return false;
90
-    }
91
-
92
-    ssize_t next = -1;
93
-    for (size_t i = 0; i < MAX_SOCK; i++) {
94
-        if (!sock[i].set) {
95
-            next = i;
96
-            break;
97
-        }
98
-    }
99
-
100
-    if (next < 0) {
101
-        debug("error: too many sockets");
102
-        return false;
103
-    }
104
-
105
-    debug("new socket at %d", next);
106
-
107
-    sock[next].pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
108
-    if (sock[next].pcb == NULL) {
109
-        debug("error allocating new socket");
110
-        return false;
111
-    }
112
-
113
-    tcp_arg(sock[next].pcb, Con);
114
-    tcp_err(sock[next].pcb, tcp_server_err);
115
-    tcp_recv(sock[next].pcb, tcp_server_recv);
116
-
117
-    Con->SocketFD = next;
118
-    sock[next].set = true;
119
-
120
-    struct ring_buffer tmp = RB_INIT(sock[next].rx_buf, SOCK_RECV_BUFF, sizeof(uint8_t));
121
-    sock[next].rx_rb = tmp;
122
-
123
-    struct ring_buffer tmp2 = RB_INIT(sock[next].child_buf, MAX_SOCK, sizeof(struct tcp_pcb *));
124
-    sock[next].child_rb = tmp2;
125
-
126
-    Con->ErrorCode = e_ConnectError_AllOk;
127
-    Con->State = e_ConnectState_Idle;
128
-    Con->ReadInProgress = false;
129
-
130
-    return true;
131
-}
132
-
133
-static err_t tcp_server_connected(void *arg, struct tcp_pcb *tpcb, err_t err) {
134
-    (void)tpcb;
135
-    debug("tcp connected");
136
-
137
-    struct SocketCon *Con = arg;
138
-    Con->State = (err == ERR_OK) ? e_ConnectState_Connected : e_ConnectState_Idle;
139
-    return ERR_OK;
140
-}
141
-
142
-bool SocketsCon_Connect(struct SocketCon *Con,
143
-                        const char *ServerName, int portNo) {
144
-    if ((!Con) || (!ServerName)) {
145
-        debug("invalid param");
146
-        return false;
147
-    }
148
-
149
-    ip_addr_t ipaddr;
150
-    err_t err;
151
-    uint32_t start_time = to_ms_since_boot(get_absolute_time());
152
-    do {
153
-        err = dns_gethostbyname(ServerName, &ipaddr, NULL, NULL);
154
-        main_loop_hw();
155
-
156
-        uint32_t now = to_ms_since_boot(get_absolute_time());
157
-        if ((now - start_time) >= HTTP_DNS_TIMEOUT_MS) {
158
-            break;
159
-        }
160
-    } while (err == ERR_INPROGRESS);
161
-    if (err != ERR_OK) {
162
-        debug("error getting IP for '%s'", ServerName);
163
-        return false;
164
-    } else {
165
-        debug("IP %s for '%s'", ip4addr_ntoa(&ipaddr), ServerName);
166
-    }
167
-
168
-    Con->State = e_ConnectState_Connecting;
169
-
170
-    size_t idx = Con->SocketFD;
171
-    err = tcp_connect(sock[idx].pcb,
172
-                            &ipaddr, portNo,
173
-                            tcp_server_connected);
174
-    if (err != ERR_OK) {
175
-        debug("error connecting (%d)", err);
176
-        Con->State = e_ConnectState_Idle;
177
-        return false;
178
-    }
179
-
180
-    start_time = to_ms_since_boot(get_absolute_time());
181
-    while (Con->State == e_ConnectState_Connecting) {
182
-        main_loop_hw();
183
-
184
-        uint32_t now = to_ms_since_boot(get_absolute_time());
185
-        if ((now - start_time) >= HTTP_CONNECT_TIMEOUT_MS) {
186
-            break;
187
-        }
188
-    }
189
-
190
-    // TODO ?
191
-    //if (Con->State == e_ConnectState_Error) {
192
-    //    Con->State = e_ConnectState_Idle;
193
-    //}
194
-
195
-    return (Con->State == e_ConnectState_Connected);
196
-}
197
-
198
-bool SocketsCon_EnableAddressReuse(struct SocketCon *Con, bool Enable) {
199
-    (void)Con;
200
-    (void)Enable;
201
-    return true;
202
-}
203
-
204
-void SocketsCon_Tick(struct SocketCon *Con) { (void)Con; }
205
-
206
-bool SocketsCon_Write(struct SocketCon *Con, const void *buf, int num) {
207
-    if ((!Con) || (!buf) || (num <= 0)) {
208
-        debug("invalid param");
209
-        return false;
210
-    }
211
-
212
-    size_t idx = Con->SocketFD;
213
-    err_t err = tcp_write(sock[idx].pcb,
214
-                          buf, num, TCP_WRITE_FLAG_COPY);
215
-    if (err != ERR_OK) {
216
-        debug("error writing to socket");
217
-        return false;
218
-    }
219
-
220
-    return true;
221
-}
222
-
223
-int SocketsCon_Read(struct SocketCon *Con, void *buf, int num) {
224
-    if ((!Con) || (!buf) || (num <= 0)) {
225
-        debug("invalid param");
226
-        return false;
227
-    }
228
-
229
-    // TODO irq disable?
230
-
231
-    size_t idx = Con->SocketFD;
232
-    size_t len = rb_get(&sock[idx].rx_rb, buf, num);
233
-
234
-    // TODO irq enable?
235
-
236
-    return len;
237
-}
238
-
239
-void SocketsCon_Close(struct SocketCon *Con) {
240
-    if (!Con) {
241
-        debug("invalid param");
242
-        return;
243
-    }
244
-
245
-    size_t idx = Con->SocketFD;
246
-    sock[idx].set = false;
247
-
248
-    err_t err = tcp_close(sock[idx].pcb);
249
-    if (err != ERR_OK) {
250
-        debug("error closing socket (%d)", err);
251
-        // TODO retry?
252
-    }
253
-}
254
-
255
-static err_t tcp_server_accept(void *arg, struct tcp_pcb *newpcb, err_t err) {
256
-    if (err != ERR_OK) {
257
-        debug("ignoring failed accept");
258
-        return ERR_OK;
259
-    }
260
-
261
-    struct SocketCon *Con = arg;
262
-    size_t idx = Con->SocketFD;
263
-    if (rb_space(&sock[idx].child_rb) <= 0) {
264
-        debug("no space for new connection");
265
-        tcp_abort(newpcb);
266
-        return ERR_OK; // ERR_ABRT ?
267
-    }
268
-
269
-    debug("new connection (%d)", err);
270
-
271
-    rb_push(&sock[idx].child_rb, &newpcb);
272
-    return ERR_OK;
273
-}
274
-
275
-bool SocketsCon_Listen(struct SocketCon *Con, const char *bindadd, int PortNo) {
276
-    if (!Con) {
277
-        debug("invalid param");
278
-        return false;
279
-    }
280
-
281
-    ip_addr_t ipaddr;
282
-    err_t err;
283
-    if (bindadd) {
284
-        uint32_t start_time = to_ms_since_boot(get_absolute_time());
285
-        do {
286
-            err = dns_gethostbyname(bindadd, &ipaddr, NULL, NULL);
287
-            main_loop_hw();
288
-
289
-            uint32_t now = to_ms_since_boot(get_absolute_time());
290
-            if ((now - start_time) >= HTTP_DNS_TIMEOUT_MS) {
291
-                break;
292
-            }
293
-        } while (err == ERR_INPROGRESS);
294
-        if (err != ERR_OK) {
295
-            debug("error getting IP for '%s'", bindadd);
296
-            return false;
297
-        } else {
298
-            debug("IP %s for '%s'", ip4addr_ntoa(&ipaddr), bindadd);
299
-        }
300
-    }
301
-
302
-    size_t idx = Con->SocketFD;
303
-    err = tcp_bind(sock[idx].pcb,
304
-                   bindadd ? &ipaddr : IP_ANY_TYPE,
305
-                   PortNo);
306
-    if (err != ERR_OK) {
307
-        debug("error binding to '%s'", bindadd);
308
-        return false;
309
-    }
310
-
311
-    struct tcp_pcb *tmp = tcp_listen(sock[idx].pcb);
312
-    if (tmp == NULL) {
313
-        debug("error listening on socket");
314
-        return false;
315
-    }
316
-    sock[idx].pcb = tmp;
317
-
318
-    tcp_accept(sock[idx].pcb, tcp_server_accept);
319
-
320
-    return true;
321
-}
322
-
323
-bool SocketsCon_Accept(struct SocketCon *Con, struct SocketCon *NewCon) {
324
-    if ((!Con) || (!NewCon)) {
325
-        debug("invalid param");
326
-        return false;
327
-    }
328
-
329
-    size_t idx = Con->SocketFD;
330
-    size_t new_idx = NewCon->SocketFD;
331
-
332
-    if (rb_len(&sock[idx].child_rb) <= 0) {
333
-        return false;
334
-    }
335
-
336
-    debug("accepting new connection");
337
-
338
-    struct tcp_pcb *new_pcb;
339
-    rb_pop(&sock[idx].child_rb, &new_pcb);
340
-
341
-    err_t err = tcp_close(sock[new_idx].pcb);
342
-    if (err != ERR_OK) {
343
-        debug("error closing prev new socket");
344
-    }
345
-
346
-    sock[new_idx].pcb = new_pcb;
347
-    return true;
348
-}
349
-
350
-bool SocketsCon_HasError(struct SocketCon *Con) {
351
-    if (!Con) {
352
-        debug("invalid param");
353
-        return true;
354
-    }
355
-    return Con->State == e_ConnectState_Error;
356
-}
357
-
358
-bool SocketsCon_IsConnected(struct SocketCon *Con) {
359
-    if (!Con) {
360
-        debug("invalid param");
361
-        return false;
362
-    }
363
-    return Con->State == e_ConnectState_Connected;
364
-}
365
-
366
-int SocketsCon_GetLastErrNo(struct SocketCon *Con) {
367
-    if (!Con) {
368
-        debug("invalid param");
369
-        return -1;
370
-    }
371
-    return Con->Last_errno;
372
-}
373
-
374
-e_ConnectErrorType SocketsCon_GetErrorCode(struct SocketCon *Con) {
375
-    if (!Con) {
376
-        debug("invalid param");
377
-        return e_ConnectErrorMAX;
378
-    }
379
-    return Con->ErrorCode;
380
-}
381
-
382
-bool SocketsCon_GetSocketHandle(struct SocketCon *Con,
383
-                                t_ConSocketHandle *RetHandle) {
384
-    if (!Con) {
385
-        debug("invalid param");
386
-        return false;
387
-    }
388
-
389
-    if (Con->SocketFD < 0) {
390
-        return false;
391
-    }
392
-
393
-    *RetHandle = Con->SocketFD;
394
-    return true;
395
-}

+ 0
- 4
src/main.c 查看文件

@@ -64,15 +64,11 @@ void networking_init(void) {
64 64
 }
65 65
 
66 66
 void networking_deinit(void) {
67
-    debug("http_deinit");
68
-    http_deinit();
69
-
70 67
     debug("wifi_deinit");
71 68
     wifi_deinit();
72 69
 }
73 70
 
74 71
 void networking_run(void) {
75
-    http_run();
76 72
     wifi_run();
77 73
 }
78 74
 

Loading…
取消
儲存