fix indents
This commit is contained in:
parent
4b2b56dd79
commit
33aaa589f6
@ -35,7 +35,7 @@ int64_t pw_sw_off_async (alarm_id_t id, void * user_data) {
|
|||||||
// hander fn to read from the power state
|
// hander fn to read from the power state
|
||||||
bool update_current_state_async (repeating_timer_t * rt) {
|
bool update_current_state_async (repeating_timer_t * rt) {
|
||||||
current_state.voltage = adc_read() * 3.3f / (1 << 12);
|
current_state.voltage = adc_read() * 3.3f / (1 << 12);
|
||||||
current_state.tempC = 27.0f - (current_state.voltage - 0.706f) / 0.001721f;
|
current_state.tempC = 27.0f - (current_state.voltage - 0.706f) / 0.001721f;
|
||||||
current_state.power_state = gpio_get(PW_STATE_PIN) ^ PW_STATE_INV;
|
current_state.power_state = gpio_get(PW_STATE_PIN) ^ PW_STATE_INV;
|
||||||
return true; // continue repeating alarm
|
return true; // continue repeating alarm
|
||||||
}
|
}
|
||||||
|
444
http_serv.h
444
http_serv.h
@ -13,300 +13,300 @@
|
|||||||
extern CURRENT_STATE_T current_state;
|
extern CURRENT_STATE_T current_state;
|
||||||
|
|
||||||
typedef struct TCP_SERVER_T_ {
|
typedef struct TCP_SERVER_T_ {
|
||||||
struct tcp_pcb * server_pcb;
|
struct tcp_pcb * server_pcb;
|
||||||
bool complete;
|
bool complete;
|
||||||
ip_addr_t gw;
|
ip_addr_t gw;
|
||||||
} TCP_SERVER_T;
|
} TCP_SERVER_T;
|
||||||
|
|
||||||
typedef struct TCP_CONNECT_STATE_T_ {
|
typedef struct TCP_CONNECT_STATE_T_ {
|
||||||
struct tcp_pcb * pcb;
|
struct tcp_pcb * pcb;
|
||||||
int sent_len;
|
int sent_len;
|
||||||
char method[8];
|
char method[8];
|
||||||
char request[128];
|
char request[128];
|
||||||
char params[128];
|
char params[128];
|
||||||
char protocol[8];
|
char protocol[8];
|
||||||
char headers[1024];
|
char headers[1024];
|
||||||
char body[128];
|
char body[128];
|
||||||
char result[128];
|
char result[128];
|
||||||
int header_len;
|
int header_len;
|
||||||
int result_len;
|
int result_len;
|
||||||
ip_addr_t * gw;
|
ip_addr_t * gw;
|
||||||
} TCP_CONNECT_STATE_T;
|
} TCP_CONNECT_STATE_T;
|
||||||
|
|
||||||
TCP_SERVER_T * http_serv_state = NULL;
|
TCP_SERVER_T * http_serv_state = NULL;
|
||||||
|
|
||||||
static err_t tcp_close_client_connection(TCP_CONNECT_STATE_T * con_state, struct tcp_pcb * client_pcb, err_t close_err) {
|
static err_t tcp_close_client_connection(TCP_CONNECT_STATE_T * con_state, struct tcp_pcb * client_pcb, err_t close_err) {
|
||||||
if (client_pcb) {
|
if (client_pcb) {
|
||||||
assert(con_state && con_state->pcb == client_pcb);
|
assert(con_state && con_state->pcb == client_pcb);
|
||||||
tcp_arg(client_pcb, NULL);
|
tcp_arg(client_pcb, NULL);
|
||||||
tcp_poll(client_pcb, NULL, 0);
|
tcp_poll(client_pcb, NULL, 0);
|
||||||
tcp_sent(client_pcb, NULL);
|
tcp_sent(client_pcb, NULL);
|
||||||
tcp_recv(client_pcb, NULL);
|
tcp_recv(client_pcb, NULL);
|
||||||
tcp_err(client_pcb, NULL);
|
tcp_err(client_pcb, NULL);
|
||||||
err_t err = tcp_close(client_pcb);
|
err_t err = tcp_close(client_pcb);
|
||||||
if (err != ERR_OK) {
|
if (err != ERR_OK) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Close failed %d, calling abort\n", err);
|
DEBUG_printf("[HTTP] [ERR] Close failed %d, calling abort\n", err);
|
||||||
tcp_abort(client_pcb);
|
tcp_abort(client_pcb);
|
||||||
close_err = ERR_ABRT;
|
close_err = ERR_ABRT;
|
||||||
}
|
}
|
||||||
if (con_state) {
|
if (con_state) {
|
||||||
free(con_state);
|
free(con_state);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return close_err;
|
return close_err;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tcp_server_close(TCP_SERVER_T * state) {
|
static void tcp_server_close(TCP_SERVER_T * state) {
|
||||||
if (state->server_pcb) {
|
if (state->server_pcb) {
|
||||||
tcp_arg(state->server_pcb, NULL);
|
tcp_arg(state->server_pcb, NULL);
|
||||||
tcp_close(state->server_pcb);
|
tcp_close(state->server_pcb);
|
||||||
state->server_pcb = NULL;
|
state->server_pcb = NULL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static err_t tcp_server_sent(void * arg, struct tcp_pcb * pcb, u16_t len) {
|
static err_t tcp_server_sent(void * arg, struct tcp_pcb * pcb, u16_t len) {
|
||||||
TCP_CONNECT_STATE_T * con_state = (TCP_CONNECT_STATE_T *)arg;
|
TCP_CONNECT_STATE_T * con_state = (TCP_CONNECT_STATE_T *)arg;
|
||||||
DEBUG_printf("[HTTP] [OK ] Sent %u\n", len);
|
DEBUG_printf("[HTTP] [OK ] Sent %u\n", len);
|
||||||
con_state->sent_len += len;
|
con_state->sent_len += len;
|
||||||
if (con_state->sent_len >= con_state->header_len + con_state->result_len) {
|
if (con_state->sent_len >= con_state->header_len + con_state->result_len) {
|
||||||
DEBUG_printf("[HTTP] [OK ] Send done\n");
|
DEBUG_printf("[HTTP] [OK ] Send done\n");
|
||||||
return tcp_close_client_connection(con_state, pcb, ERR_OK);
|
return tcp_close_client_connection(con_state, pcb, ERR_OK);
|
||||||
}
|
}
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_status_get (const char * request, const char * params, const char * body, char * result, size_t max_result_len) {
|
static int handle_status_get (const char * request, const char * params, const char * body, char * result, size_t max_result_len) {
|
||||||
return snprintf(result, max_result_len, "{volt: %f, temp: %f, power: %d}", current_state.voltage, current_state.tempC, current_state.power_state);
|
return snprintf(result, max_result_len, "{volt: %f, temp: %f, power: %d}", current_state.voltage, current_state.tempC, current_state.power_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int handle_power_post (const char * request, const char * params, const char * body, char * result, size_t max_result_len) {
|
static int handle_power_post (const char * request, const char * params, const char * body, char * result, size_t max_result_len) {
|
||||||
if (strstr(body, "requested_state")) {
|
if (strstr(body, "requested_state")) {
|
||||||
int requested_power_state_int;
|
int requested_power_state_int;
|
||||||
int led_param = sscanf(body, "requested_state=%d", &requested_power_state_int);
|
int led_param = sscanf(body, "requested_state=%d", &requested_power_state_int);
|
||||||
if (led_param) {
|
if (led_param) {
|
||||||
if (requested_power_state_int == 0 || requested_power_state_int == 1) {
|
if (requested_power_state_int == 0 || requested_power_state_int == 1) {
|
||||||
bmc_power_handler((bool) requested_power_state_int);
|
bmc_power_handler((bool) requested_power_state_int);
|
||||||
return snprintf(result, max_result_len, "{}");
|
return snprintf(result, max_result_len, "{}");
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return snprintf(result, max_result_len, "{error: true, description: \"invalid requested state, must be 0 or 1\"}");
|
return snprintf(result, max_result_len, "{error: true, description: \"invalid requested state, must be 0 or 1\"}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return snprintf(result, max_result_len, "{error: true, description: \"invalid requested state, must be 0 or 1\"}");
|
return snprintf(result, max_result_len, "{error: true, description: \"invalid requested state, must be 0 or 1\"}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return snprintf(result, max_result_len, "{error: true, description: \"missing required parameter requested_state\"}");
|
return snprintf(result, max_result_len, "{error: true, description: \"missing required parameter requested_state\"}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int parse_content (struct pbuf * p, TCP_CONNECT_STATE_T * con_state) {
|
int parse_content (struct pbuf * p, TCP_CONNECT_STATE_T * con_state) {
|
||||||
char content[2048] = {0};
|
char content[2048] = {0};
|
||||||
pbuf_copy_partial(p, &content, 2048 - 1, 0);
|
pbuf_copy_partial(p, &content, 2048 - 1, 0);
|
||||||
|
|
||||||
char * headers_start = content; // First header line containing method request?params HTTP/protocol
|
char * headers_start = content; // First header line containing method request?params HTTP/protocol
|
||||||
char * headers_other = strstr(content, "\r\n") + 2; // remaining headers follow the first line
|
char * headers_other = strstr(content, "\r\n") + 2; // remaining headers follow the first line
|
||||||
char * body_start = strstr(content, "\r\n\r\n") + 4; // body begins at the end of headers
|
char * body_start = strstr(content, "\r\n\r\n") + 4; // body begins at the end of headers
|
||||||
|
|
||||||
*(headers_other - 2) = 0;
|
*(headers_other - 2) = 0;
|
||||||
*(body_start - 4) = 0;
|
*(body_start - 4) = 0;
|
||||||
|
|
||||||
char request_params_comb[256];
|
char request_params_comb[256];
|
||||||
if (sscanf(headers_start, "%s %s HTTP/%s", con_state->method, request_params_comb, con_state->protocol) != 3) {
|
if (sscanf(headers_start, "%s %s HTTP/%s", con_state->method, request_params_comb, con_state->protocol) != 3) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
char * params = strchr(request_params_comb, '?');
|
char * params = strchr(request_params_comb, '?');
|
||||||
if (params) {
|
if (params) {
|
||||||
*params++ = 0;
|
*params++ = 0;
|
||||||
strcpy(con_state->request, request_params_comb);
|
strcpy(con_state->request, request_params_comb);
|
||||||
strcpy(con_state->params, params);
|
strcpy(con_state->params, params);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
strcpy(con_state->request, request_params_comb);
|
strcpy(con_state->request, request_params_comb);
|
||||||
}
|
}
|
||||||
|
|
||||||
strcpy(con_state->headers, headers_other);
|
strcpy(con_state->headers, headers_other);
|
||||||
strcpy(con_state->body, body_start);
|
strcpy(con_state->body, body_start);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int send_response (TCP_CONNECT_STATE_T * con_state, struct tcp_pcb * pcb) {
|
int send_response (TCP_CONNECT_STATE_T * con_state, struct tcp_pcb * pcb) {
|
||||||
// Check result buffer size
|
// Check result buffer size
|
||||||
if (con_state->result_len > sizeof(con_state->result) - 1) {
|
if (con_state->result_len > sizeof(con_state->result) - 1) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Too much result data %d\n", con_state->result_len);
|
DEBUG_printf("[HTTP] [ERR] Too much result data %d\n", con_state->result_len);
|
||||||
return tcp_close_client_connection(con_state, pcb, ERR_CLSD);
|
return tcp_close_client_connection(con_state, pcb, ERR_CLSD);
|
||||||
}
|
}
|
||||||
// Check header buffer size
|
// Check header buffer size
|
||||||
if (con_state->header_len > sizeof(con_state->headers) - 1) {
|
if (con_state->header_len > sizeof(con_state->headers) - 1) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Too much header data %d\n", con_state->header_len);
|
DEBUG_printf("[HTTP] [ERR] Too much header data %d\n", con_state->header_len);
|
||||||
return tcp_close_client_connection(con_state, pcb, ERR_CLSD);
|
return tcp_close_client_connection(con_state, pcb, ERR_CLSD);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send the headers to the client
|
// Send the headers to the client
|
||||||
con_state->sent_len = 0;
|
con_state->sent_len = 0;
|
||||||
err_t err = tcp_write(pcb, con_state->headers, con_state->header_len, 0);
|
err_t err = tcp_write(pcb, con_state->headers, con_state->header_len, 0);
|
||||||
if (err != ERR_OK) {
|
if (err != ERR_OK) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Failed to write header data %d\n", err);
|
DEBUG_printf("[HTTP] [ERR] Failed to write header data %d\n", err);
|
||||||
return tcp_close_client_connection(con_state, pcb, err);
|
return tcp_close_client_connection(con_state, pcb, err);
|
||||||
}
|
}
|
||||||
// Send the body to the client
|
// Send the body to the client
|
||||||
if (con_state->result_len) {
|
if (con_state->result_len) {
|
||||||
err = tcp_write(pcb, con_state->result, con_state->result_len, 0);
|
err = tcp_write(pcb, con_state->result, con_state->result_len, 0);
|
||||||
if (err != ERR_OK) {
|
if (err != ERR_OK) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Failed to write result data %d\n", err);
|
DEBUG_printf("[HTTP] [ERR] Failed to write result data %d\n", err);
|
||||||
return tcp_close_client_connection(con_state, pcb, err);
|
return tcp_close_client_connection(con_state, pcb, err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
err_t tcp_server_recv(void * arg, struct tcp_pcb * pcb, struct pbuf * p, err_t err) {
|
err_t tcp_server_recv(void * arg, struct tcp_pcb * pcb, struct pbuf * p, err_t err) {
|
||||||
TCP_CONNECT_STATE_T * con_state = (TCP_CONNECT_STATE_T *)arg;
|
TCP_CONNECT_STATE_T * con_state = (TCP_CONNECT_STATE_T *)arg;
|
||||||
if (!p) {
|
if (!p) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Client connection closed\n");
|
DEBUG_printf("[HTTP] [ERR] Client connection closed\n");
|
||||||
return tcp_close_client_connection(con_state, pcb, ERR_OK);
|
return tcp_close_client_connection(con_state, pcb, ERR_OK);
|
||||||
}
|
}
|
||||||
assert(con_state && con_state->pcb == pcb);
|
assert(con_state && con_state->pcb == pcb);
|
||||||
if (p->tot_len > 0) {
|
if (p->tot_len > 0) {
|
||||||
DEBUG_printf("[HTTP] [OK ] Server recieved %d err %d\n", p->tot_len, err);
|
DEBUG_printf("[HTTP] [OK ] Server recieved %d err %d\n", p->tot_len, err);
|
||||||
// Copy the request into the buffer
|
// Copy the request into the buffer
|
||||||
|
|
||||||
if (!parse_content(p, con_state)) {
|
if (!parse_content(p, con_state)) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Failed to parse header\n");
|
DEBUG_printf("[HTTP] [ERR] Failed to parse header\n");
|
||||||
return tcp_close_client_connection(con_state, pcb, ERR_OK);
|
return tcp_close_client_connection(con_state, pcb, ERR_OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
// print request
|
// print request
|
||||||
DEBUG_printf("[HTTP] [OK ] Request: %s %s?%s %s\n", con_state->method, con_state->request, con_state->params, con_state->body);
|
DEBUG_printf("[HTTP] [OK ] Request: %s %s?%s %s\n", con_state->method, con_state->request, con_state->params, con_state->body);
|
||||||
|
|
||||||
int response_code;
|
int response_code;
|
||||||
|
|
||||||
// parse request depending on method and request
|
// parse request depending on method and request
|
||||||
if (strncmp(con_state->method, HTTP_GET, sizeof(HTTP_GET) - 1) == 0 && strncmp(con_state->request, "/status", sizeof("/status") - 1) == 0) {
|
if (strncmp(con_state->method, HTTP_GET, sizeof(HTTP_GET) - 1) == 0 && strncmp(con_state->request, "/status", sizeof("/status") - 1) == 0) {
|
||||||
con_state->result_len = handle_status_get(con_state->request, con_state->params, con_state->body, con_state->result, sizeof(con_state->result));
|
con_state->result_len = handle_status_get(con_state->request, con_state->params, con_state->body, con_state->result, sizeof(con_state->result));
|
||||||
response_code = 200;
|
response_code = 200;
|
||||||
con_state->header_len = snprintf(con_state->headers, sizeof(con_state->headers), HTTP_RESPONSE_HEADER, response_code, con_state->result_len);
|
con_state->header_len = snprintf(con_state->headers, sizeof(con_state->headers), HTTP_RESPONSE_HEADER, response_code, con_state->result_len);
|
||||||
}
|
}
|
||||||
else if (strncmp(con_state->method, HTTP_POST, sizeof(HTTP_POST) - 1) == 0 && strncmp(con_state->request, "/power", sizeof("/power") - 1) == 0) {
|
else if (strncmp(con_state->method, HTTP_POST, sizeof(HTTP_POST) - 1) == 0 && strncmp(con_state->request, "/power", sizeof("/power") - 1) == 0) {
|
||||||
con_state->result_len = handle_power_post(con_state->request, con_state->params, con_state->body, con_state->result, sizeof(con_state->result));
|
con_state->result_len = handle_power_post(con_state->request, con_state->params, con_state->body, con_state->result, sizeof(con_state->result));
|
||||||
response_code = 200;
|
response_code = 200;
|
||||||
con_state->header_len = snprintf(con_state->headers, sizeof(con_state->headers), HTTP_RESPONSE_HEADER, response_code, con_state->result_len);
|
con_state->header_len = snprintf(con_state->headers, sizeof(con_state->headers), HTTP_RESPONSE_HEADER, response_code, con_state->result_len);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
con_state->result_len = 0;
|
con_state->result_len = 0;
|
||||||
response_code = 501;
|
response_code = 501;
|
||||||
con_state->header_len = snprintf(con_state->headers, sizeof(con_state->headers), HTTP_RESPONSE_HEADER, response_code, con_state->result_len);
|
con_state->header_len = snprintf(con_state->headers, sizeof(con_state->headers), HTTP_RESPONSE_HEADER, response_code, con_state->result_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
// print result
|
// print result
|
||||||
DEBUG_printf("[HTTP] [OK ] Result: %d %s\n", response_code, con_state->result);
|
DEBUG_printf("[HTTP] [OK ] Result: %d %s\n", response_code, con_state->result);
|
||||||
|
|
||||||
if (send_response(con_state, pcb)) {
|
if (send_response(con_state, pcb)) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Failure in send\n");
|
DEBUG_printf("[HTTP] [ERR] Failure in send\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
tcp_recved(pcb, p->tot_len);
|
tcp_recved(pcb, p->tot_len);
|
||||||
}
|
}
|
||||||
pbuf_free(p);
|
pbuf_free(p);
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static err_t tcp_server_poll(void * arg, struct tcp_pcb * pcb) {
|
static err_t tcp_server_poll(void * arg, struct tcp_pcb * pcb) {
|
||||||
TCP_CONNECT_STATE_T * con_state = (TCP_CONNECT_STATE_T *)arg;
|
TCP_CONNECT_STATE_T * con_state = (TCP_CONNECT_STATE_T *)arg;
|
||||||
return tcp_close_client_connection(con_state, pcb, ERR_OK); // Just disconnect clent?
|
return tcp_close_client_connection(con_state, pcb, ERR_OK); // Just disconnect clent?
|
||||||
}
|
}
|
||||||
|
|
||||||
static void tcp_server_err(void * arg, err_t err) {
|
static void tcp_server_err(void * arg, err_t err) {
|
||||||
TCP_CONNECT_STATE_T * con_state = (TCP_CONNECT_STATE_T *)arg;
|
TCP_CONNECT_STATE_T * con_state = (TCP_CONNECT_STATE_T *)arg;
|
||||||
if (err != ERR_ABRT) {
|
if (err != ERR_ABRT) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Error %d\n", err);
|
DEBUG_printf("[HTTP] [ERR] Error %d\n", err);
|
||||||
tcp_close_client_connection(con_state, con_state->pcb, err);
|
tcp_close_client_connection(con_state, con_state->pcb, err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static err_t tcp_server_accept(void * arg, struct tcp_pcb * client_pcb, err_t err) {
|
static err_t tcp_server_accept(void * arg, struct tcp_pcb * client_pcb, err_t err) {
|
||||||
TCP_SERVER_T * state = (TCP_SERVER_T *)arg;
|
TCP_SERVER_T * state = (TCP_SERVER_T *)arg;
|
||||||
if (err != ERR_OK || client_pcb == NULL) {
|
if (err != ERR_OK || client_pcb == NULL) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Failure accepting client connection\n");
|
DEBUG_printf("[HTTP] [ERR] Failure accepting client connection\n");
|
||||||
return ERR_VAL;
|
return ERR_VAL;
|
||||||
}
|
}
|
||||||
DEBUG_printf("[HTTP] [OK ] Client connected\n");
|
DEBUG_printf("[HTTP] [OK ] Client connected\n");
|
||||||
|
|
||||||
// Create the state for the connection
|
// Create the state for the connection
|
||||||
TCP_CONNECT_STATE_T * con_state = calloc(1, sizeof(TCP_CONNECT_STATE_T));
|
TCP_CONNECT_STATE_T * con_state = calloc(1, sizeof(TCP_CONNECT_STATE_T));
|
||||||
if (!con_state) {
|
if (!con_state) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Failed to allocate connect state\n");
|
DEBUG_printf("[HTTP] [ERR] Failed to allocate connect state\n");
|
||||||
return ERR_MEM;
|
return ERR_MEM;
|
||||||
}
|
}
|
||||||
con_state->pcb = client_pcb; // for checking
|
con_state->pcb = client_pcb; // for checking
|
||||||
con_state->gw = &state->gw;
|
con_state->gw = &state->gw;
|
||||||
|
|
||||||
// setup connection to client
|
// setup connection to client
|
||||||
tcp_arg(client_pcb, con_state);
|
tcp_arg(client_pcb, con_state);
|
||||||
tcp_sent(client_pcb, tcp_server_sent);
|
tcp_sent(client_pcb, tcp_server_sent);
|
||||||
tcp_recv(client_pcb, tcp_server_recv);
|
tcp_recv(client_pcb, tcp_server_recv);
|
||||||
tcp_poll(client_pcb, tcp_server_poll, POLL_TIME_S * 2);
|
tcp_poll(client_pcb, tcp_server_poll, POLL_TIME_S * 2);
|
||||||
tcp_err(client_pcb, tcp_server_err);
|
tcp_err(client_pcb, tcp_server_err);
|
||||||
|
|
||||||
return ERR_OK;
|
return ERR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool tcp_server_open(void * arg) {
|
static bool tcp_server_open(void * arg) {
|
||||||
TCP_SERVER_T * state = (TCP_SERVER_T *)arg;
|
TCP_SERVER_T * state = (TCP_SERVER_T *)arg;
|
||||||
DEBUG_printf("[HTTP] [OK ] Starting server at %s on port %d\n", ip4addr_ntoa(netif_ip4_addr(netif_list)), TCP_PORT);
|
DEBUG_printf("[HTTP] [OK ] Starting server at %s on port %d\n", ip4addr_ntoa(netif_ip4_addr(netif_list)), TCP_PORT);
|
||||||
|
|
||||||
struct tcp_pcb * pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
|
struct tcp_pcb * pcb = tcp_new_ip_type(IPADDR_TYPE_ANY);
|
||||||
if (!pcb) {
|
if (!pcb) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Failed to create pcb\n");
|
DEBUG_printf("[HTTP] [ERR] Failed to create pcb\n");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
err_t err = tcp_bind(pcb, IP_ANY_TYPE, TCP_PORT);
|
err_t err = tcp_bind(pcb, IP_ANY_TYPE, TCP_PORT);
|
||||||
if (err) {
|
if (err) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Failed to bind to port %d\n", TCP_PORT);
|
DEBUG_printf("[HTTP] [ERR] Failed to bind to port %d\n", TCP_PORT);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
state->server_pcb = tcp_listen_with_backlog(pcb, 1);
|
state->server_pcb = tcp_listen_with_backlog(pcb, 1);
|
||||||
if (!state->server_pcb) {
|
if (!state->server_pcb) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Failed to listen on port %d\n", TCP_PORT);
|
DEBUG_printf("[HTTP] [ERR] Failed to listen on port %d\n", TCP_PORT);
|
||||||
if (pcb) {
|
if (pcb) {
|
||||||
tcp_close(pcb);
|
tcp_close(pcb);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
tcp_arg(state->server_pcb, state);
|
tcp_arg(state->server_pcb, state);
|
||||||
tcp_accept(state->server_pcb, tcp_server_accept);
|
tcp_accept(state->server_pcb, tcp_server_accept);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int http_serv_init () {
|
int http_serv_init () {
|
||||||
http_serv_state = calloc(1, sizeof(TCP_SERVER_T));
|
http_serv_state = calloc(1, sizeof(TCP_SERVER_T));
|
||||||
if (!http_serv_state) {
|
if (!http_serv_state) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Failed to allocate state\n");
|
DEBUG_printf("[HTTP] [ERR] Failed to allocate state\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!tcp_server_open(http_serv_state)) {
|
if (!tcp_server_open(http_serv_state)) {
|
||||||
DEBUG_printf("[HTTP] [ERR] Failed to open server\n");
|
DEBUG_printf("[HTTP] [ERR] Failed to open server\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEBUG_printf("[HTTP] [OK ] Sucessfully initialized http server\n");
|
DEBUG_printf("[HTTP] [OK ] Sucessfully initialized http server\n");
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int http_serv_deinit () {
|
int http_serv_deinit () {
|
||||||
tcp_server_close(http_serv_state);
|
tcp_server_close(http_serv_state);
|
||||||
free(http_serv_state);
|
free(http_serv_state);
|
||||||
http_serv_state = NULL;
|
http_serv_state = NULL;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
18
main.c
18
main.c
@ -7,16 +7,16 @@
|
|||||||
#include "secret.h"
|
#include "secret.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
stdio_init_all();
|
stdio_init_all();
|
||||||
|
|
||||||
if (cyw43_arch_init()) {
|
if (cyw43_arch_init()) {
|
||||||
DEBUG_printf("[INIT] [ERR] Failed to initialise cyw43\n");
|
DEBUG_printf("[INIT] [ERR] Failed to initialise cyw43\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
cyw43_arch_enable_sta_mode();
|
cyw43_arch_enable_sta_mode();
|
||||||
|
|
||||||
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASS, CYW43_AUTH_WPA2_AES_PSK, 30000)){
|
if (cyw43_arch_wifi_connect_timeout_ms(WIFI_SSID, WIFI_PASS, CYW43_AUTH_WPA2_AES_PSK, 30000)){
|
||||||
DEBUG_printf("[INIT] [ERR] Wi-Fi failed to connect\n");
|
DEBUG_printf("[INIT] [ERR] Wi-Fi failed to connect\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -30,12 +30,12 @@ int main() {
|
|||||||
// set LED to on to indicate it has connected and initialized
|
// set LED to on to indicate it has connected and initialized
|
||||||
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1);
|
||||||
|
|
||||||
while (!http_serv_state->complete) {
|
while (!http_serv_state->complete) {
|
||||||
sleep_ms(1000);
|
sleep_ms(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
bmc_handler_deinit();
|
bmc_handler_deinit();
|
||||||
http_serv_deinit();
|
http_serv_deinit();
|
||||||
cyw43_arch_deinit();
|
cyw43_arch_deinit();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user