Prusa MINI Firmware overview
|
#include <cstring>
#include "dbg.h"
#include "lwip/def.h"
#include "lwsapi.h"
#include "connection.hpp"
#include "lwip/timeouts.h"
|
static bool | empty_message (const Message_t &msg) |
| Check when message is empty - all data was processed yet. More...
|
|
static size_t | lwsapi_write (Context *ctx, const uint8_t *data, size_t len) |
| Write data to TCP output buffer. More...
|
|
static size_t | lwsapi_write (Context *ctx, const char *data) |
|
static err_t | lwsapi_poll (void *arg, struct tcp_pcb *pcb) |
| The poll function is called every 2nd second. More...
|
|
static err_t | close_conn (struct tcp_pcb *pcb, Context *ctx=nullptr) |
| Close connection and clean callbacks and other connection environment. More...
|
|
static void | lwsapi_call (Context *ctx, const struct pbuf *input=nullptr) |
| Process message from IResponse::generator, and call generator for next work. More...
|
|
static err_t | lwsapi_recv (void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err) |
| Process http request and call application. More...
|
|
static void | lwsapi_err (void *arg, err_t err) |
| tcp_err callback defined in LwIP More...
|
|
static err_t | lwsapi_sent (void *arg, struct tcp_pcb *pcb, u16_t len) |
| tcp_sent callback defined in LwIP More...
|
|
static err_t | lwsapi_accept (void *arg, struct tcp_pcb *pcb, err_t err) |
| tcp_accept callback defined in LwIP More...
|
|
err_t | lwsapi_init (void) |
| Init (start) the LwIP WSAPI server. More...
|
|
IHeader * | dynamics_header_factory (const char *key, const char *value, size_t value_length) |
| Return new DynamicsHeader. More...
|
|
IHeader * | number_header_factory (const char *key, const char *value, size_t value_length) |
| Response new NumberHeader. More...
|
|
◆ LIGHT_WSAPI_PORT
#define LIGHT_WSAPI_PORT 80 |
◆ LIGHT_WSAPI_RETRIES
#define LIGHT_WSAPI_RETRIES 4 |
◆ LIGHT_WSAPI_POLL_INTERVAL
#define LIGHT_WSAPI_POLL_INTERVAL 4 |
◆ empty_message()
Check when message is empty - all data was processed yet.
◆ lwsapi_write() [1/2]
Write data to TCP output buffer.
While there is place in output TCP buffer, lwsapi_write copy data from Context.buffer to TCP buffer. When TCP buffer is full, stop.
- Parameters
-
ctx | pointer to connection context |
data | pointer to array of data which must be copied to output tcp buffer |
len | length of data, which must be copied |
- Returns
- size of data, which was be copied to output tcp buffer
183 if (ctx ==
nullptr) {
190 size_t snd_len = len;
191 while (offset < len) {
193 max_len = tcp_sndbuf(ctx->
pcb);
194 if (max_len < snd_len) {
203 max_len = ((
u16_t)(2 * tcp_mss(ctx->
pcb)));
204 if (max_len < snd_len) {
211 err = tcp_write(ctx->
pcb,
data + offset, snd_len, TCP_WRITE_FLAG_COPY);
220 }
while ((err ==
ERR_MEM) && (snd_len > 1));
225 lwsapi_dbg(
"[%p] lwsapi_write: tcp_write error: %d\n", ctx->
pcb, err);
◆ lwsapi_write() [2/2]
static size_t lwsapi_write |
( |
Context * |
ctx, |
|
|
const char * |
data |
|
) |
| |
|
static |
◆ lwsapi_poll()
static err_t lwsapi_poll |
( |
void * |
arg, |
|
|
struct tcp_pcb * |
pcb |
|
) |
| |
|
static |
◆ close_conn()
static err_t close_conn |
( |
struct tcp_pcb * |
pcb, |
|
|
Context * |
ctx = nullptr |
|
) |
| |
|
static |
Close connection and clean callbacks and other connection environment.
56 tcp_arg(pcb,
nullptr);
57 tcp_recv(pcb,
nullptr);
58 tcp_err(pcb,
nullptr);
59 tcp_poll(pcb,
nullptr, 0);
60 tcp_sent(pcb,
nullptr);
64 if (tcp_close(pcb) !=
ERR_OK) {
◆ lwsapi_call()
Process message from IResponse::generator, and call generator for next work.
While all data from Message_t is not processed, call lwsapi_write to response data to http client. This function is called first time after application_fn return IResponse, and next time from lwsapi_sent callback.
When all data in Message_t is processed, generator is called. When Message_t.length is EOF, all data from generator is processed.
121 if (ctx->
buffer !=
nullptr) {
128 }
else if (send == 0) {
133 tcp_output(ctx->
pcb);
165 tcp_output(ctx->
pcb);
◆ lwsapi_recv()
Process http request and call application.
This is callback defined in LwIP documentation http://www.nongnu.org/lwip/2_0_x/group__tcp__raw.html#ga8afd0b316a87a5eeff4726dc95006ed0
This function try to parse http request, fill Context attributes and call defined application_fn.
TODO: there is missing process big http request - that means with more (big) headers, which could be more than 1024 bytes and request payload typical for POST|PUT|PATCH requests.
- Parameters
-
arg | is pointer to connection Context |
250 if ((err !=
ERR_OK) || (p ==
nullptr) || (arg ==
nullptr)) {
257 return close_conn(pcb, static_cast<Context *>(arg));
263 Context *ctx = static_cast<Context *>(arg);
265 if (ctx->
response.get() !=
nullptr) {
275 ctx->
state = Context::State::WAIT_FOR_EOH;
285 }
else if (ctx->
state == Context::State::WAIT_FOR_EOH) {
304 ctx->
state = Context::State::PAYLOAD;
307 if (ctx->
state != Context::State::PAYLOAD)
315 if (ctx->
response.get() ==
nullptr) {
318 return close_conn(pcb, static_cast<Context *>(arg));
◆ lwsapi_err()
tcp_err callback defined in LwIP
348 lwsapi_dbg(
"lwsapi_err: connection reset by remote host");
357 if (arg !=
nullptr) {
358 Context *ctx = static_cast<Context *>(arg);
◆ lwsapi_sent()
static err_t lwsapi_sent |
( |
void * |
arg, |
|
|
struct tcp_pcb * |
pcb, |
|
|
u16_t |
len |
|
) |
| |
|
static |
tcp_sent callback defined in LwIP
This callback is called after http client confirms the part of tcp response. So next data could be send by lwsapi_call function.
369 if (arg !=
nullptr) {
370 Context *ctx = static_cast<Context *>(arg);
◆ lwsapi_accept()
static err_t lwsapi_accept |
( |
void * |
arg, |
|
|
struct tcp_pcb * |
pcb, |
|
|
err_t |
err |
|
) |
| |
|
static |
◆ lwsapi_init()
Init (start) the LwIP WSAPI server.
Start LwIP WSAPI http server, which means set TCP priority for new protocol control block, bind on TCP port and set callback for accepting new connection.
419 if (pcb ==
nullptr) {
423 tcp_setprio(pcb, TCP_PRIO_MIN);
430 pcb = tcp_listen(pcb);
431 if (pcb ==
nullptr) {
◆ dynamics_header_factory()
IHeader* dynamics_header_factory |
( |
const char * |
key, |
|
|
const char * |
value, |
|
|
size_t |
value_length |
|
) |
| |
◆ number_header_factory()
IHeader* number_header_factory |
( |
const char * |
key, |
|
|
const char * |
value, |
|
|
size_t |
value_length |
|
) |
| |
◆ http_bad_request
const char* http_bad_request |
|
static |
Initial value:= "HTTP/1.0 400 Bad Request\n"
"Server: LwIP WSAPI\n"
"Content-Type: text/plain\n"
"\n"
"Bed Request"
Full Bad Request response.
◆ http_internal_server
const char* http_internal_server |
|
static |
Initial value:= "HTTP/1.0 500 Internal Server Error\n"
"Server: LwIP WSAPI\n"
"Content-Type: text/plain\n"
"\n"
"Internal Server Error"
Full Internal Server Error response.
u16_t len
Definition: pbuf.h:159
const char * response
Definition: lwsapi_app.hpp:212
State state
Definition: connection.hpp:30
Message_t message
Definition: connection.hpp:32
static err_t lwsapi_accept(void *arg, struct tcp_pcb *pcb, err_t err)
tcp_accept callback defined in LwIP
Definition: lwsapi.cpp:387
#define LIGHT_WSAPI_RETRIES
Definition: lwsapi.cpp:11
uint16_t u16_t
Definition: arch.h:121
static const char * http_bad_request
Full Bad Request response.
Definition: lwsapi.cpp:15
netif_input_fn input
Definition: netif.h:244
u16_t tot_len
Definition: pbuf.h:156
#define IP_ANY_TYPE
Definition: ip_addr.h:400
#define TCP_SND_QUEUELEN
Definition: opt.h:1212
uint8_t data[8]
Definition: masstorage.h:49
err_t fill_request_buffer(const struct pbuf *p)
Append pbuf to request_buffer.
Definition: connection.cpp:17
struct pbuf * next
Definition: pbuf.h:144
u8_t pbuf_free(struct pbuf *p)
Definition: pbuf.c:715
#define LIGHT_WSAPI_PORT
Definition: lwsapi.cpp:10
#define lwsapi_error
Definition: lwsapi_app.hpp:42
static err_t close_conn(struct tcp_pcb *pcb, Context *ctx=nullptr)
Close connection and clean callbacks and other connection environment.
Definition: lwsapi.cpp:51
IResponse::unique_ptr_t application(Environment &env)
application_fn callback, which is called from LwIP WSAPI http server
Definition: connect.cpp:306
err_t prepare_response()
Process message.response to internal buffer.
Definition: connection.cpp:152
Internal connection structure which is used in LwIP tcp_ callbacks as arg.
Definition: connection.hpp:20
err_t prepare_header()
Process message.headers to internal buffer.
Definition: connection.cpp:171
Definition: connect.cpp:77
static void lwsapi_call(Context *ctx, const struct pbuf *input=nullptr)
Process message from IResponse::generator, and call generator for next work.
Definition: lwsapi.cpp:107
uint8_t retries
Definition: connection.hpp:29
size_t find_eoh(const void *data=nullptr, size_t length=0)
Try to find End Of Header (\r \r ) in buffer.
Definition: connection.cpp:39
static err_t lwsapi_sent(void *arg, struct tcp_pcb *pcb, u16_t len)
tcp_sent callback defined in LwIP
Definition: lwsapi.cpp:368
#define lwsapi_dbg
Definition: lwsapi_app.hpp:41
#define LIGHT_WSAPI_POLL_INTERVAL
Definition: lwsapi.cpp:12
int length
Definition: lwsapi_app.hpp:215
struct tcp_pcb * pcb
Definition: connection.hpp:28
IResponse::unique_ptr_t response
Definition: connection.hpp:37
void free_buffer()
Definition: connection.hpp:94
const uint8_t[]
Definition: 404_html.c:3
static err_t lwsapi_poll(void *arg, struct tcp_pcb *pcb)
The poll function is called every 2nd second.
Definition: lwsapi.cpp:83
s8_t err_t
Definition: err.h:57
const uint8_t * payload
Definition: lwsapi_app.hpp:214
static err_t lwsapi_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *p, err_t err)
Process http request and call application.
Definition: lwsapi.cpp:249
static const char * http_internal_server
Full Internal Server Error response.
Definition: lwsapi.cpp:22
static void lwsapi_err(void *arg, err_t err)
tcp_err callback defined in LwIP
Definition: lwsapi.cpp:344
size_t m_position
Definition: connection.hpp:34
Environment env
Definition: connection.hpp:31
void * mem_malloc(mem_size_t size)
Definition: mem.c:603
static bool empty_message(const Message_t &msg)
Check when message is empty - all data was processed yet.
Definition: lwsapi.cpp:39
#define EOF
Definition: ff.h:286
void mem_free(void *rmem)
Definition: mem.c:419
static size_t lwsapi_write(Context *ctx, const uint8_t *data, size_t len)
Write data to TCP output buffer.
Definition: lwsapi.cpp:182
err_t parse_request(const void *data=nullptr, size_t length=0)
parste the request header (request line + request headers)
Definition: connection.cpp:110
#define memshift(ptr, size)
Definition: connection.hpp:10
void * payload
Definition: pbuf.h:147
const IHeader * headers
Definition: lwsapi_app.hpp:213
static png_bytep size_t size
Definition: pngwrite.c:2170
char * buffer
Definition: connection.hpp:33