Commit a9121c8a authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Update console_httpd to new libwebsockets

Make a few servers conditional on ctrl file
Improve some socket logging.
parent 0135f3f1
...@@ -212,7 +212,7 @@ static void *log_server(void *arg) ...@@ -212,7 +212,7 @@ static void *log_server(void *arg)
fd_accept = socket(AF_INET, SOCK_STREAM, 0); fd_accept = socket(AF_INET, SOCK_STREAM, 0);
if (fd_accept < 0) { if (fd_accept < 0) {
perror("socket() failed"); printf("ERROR: log_server: socket() failed: %d\n", errno);
return NULL; return NULL;
} }
...@@ -224,13 +224,13 @@ static void *log_server(void *arg) ...@@ -224,13 +224,13 @@ static void *log_server(void *arg)
if (bind(fd_accept, if (bind(fd_accept,
(struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) { (struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) {
perror("bind() failed"); printf("ERROR: log_server: bind() failed: %d\n", errno);
close(fd_accept); close(fd_accept);
fd_accept = -1; fd_accept = -1;
} }
if (fd_accept >= 0 && listen(fd_accept, 4) < 0) { if (fd_accept >= 0 && listen(fd_accept, 4) < 0) {
perror("listen() failed"); printf("ERROR: log_server: listen() failed: %d\n", errno);
close(fd_accept); close(fd_accept);
fd_accept = -1; fd_accept = -1;
} }
......
...@@ -46,8 +46,7 @@ struct writebuf { ...@@ -46,8 +46,7 @@ struct writebuf {
}; };
struct ws_client { struct ws_client {
struct libwebsocket_context *context; struct lws *wsi;
struct libwebsocket *wsi;
struct writebuf *writeq; struct writebuf *writeq;
...@@ -102,7 +101,7 @@ struct writebuf *writebuf_next(struct ws_client *client) ...@@ -102,7 +101,7 @@ struct writebuf *writebuf_next(struct ws_client *client)
struct ws_client *ws_client_list = NULL; struct ws_client *ws_client_list = NULL;
struct ws_client *ws_client_add(struct libwebsocket_context *context, struct libwebsocket *wsi) struct ws_client *ws_client_add(struct lws *wsi)
{ {
struct ws_client *client; struct ws_client *client;
...@@ -110,7 +109,6 @@ struct ws_client *ws_client_add(struct libwebsocket_context *context, struct lib ...@@ -110,7 +109,6 @@ struct ws_client *ws_client_add(struct libwebsocket_context *context, struct lib
if (!client) if (!client)
return NULL; return NULL;
client->context = context;
client->wsi = wsi; client->wsi = wsi;
client->next = ws_client_list; client->next = ws_client_list;
...@@ -137,7 +135,7 @@ void ws_client_remove(struct ws_client *client) ...@@ -137,7 +135,7 @@ void ws_client_remove(struct ws_client *client)
} }
} }
struct ws_client *ws_client_get_by_wsi(struct libwebsocket *wsi) struct ws_client *ws_client_get_by_wsi(struct lws *wsi)
{ {
struct ws_client *entry; struct ws_client *entry;
...@@ -159,13 +157,13 @@ void ws_client_flush(struct ws_client *client) ...@@ -159,13 +157,13 @@ void ws_client_flush(struct ws_client *client)
wb = writebuf_next(client); wb = writebuf_next(client);
libwebsocket_write(client->wsi, (unsigned char *)wb->msg, wb->msg_len, LWS_WRITE_TEXT); lws_write(client->wsi, (unsigned char *)wb->msg, wb->msg_len, LWS_WRITE_TEXT);
writebuf_free(wb); writebuf_free(wb);
} }
if (client->writeq) { if (client->writeq) {
libwebsocket_callback_on_writable(client->context, client->wsi); lws_callback_on_writable(client->wsi);
} }
} }
...@@ -213,7 +211,7 @@ static void poll_set(int fd, short events) ...@@ -213,7 +211,7 @@ static void poll_set(int fd, short events)
#define LINEBUF_SIZE 8192 #define LINEBUF_SIZE 8192
struct status { struct status {
char *ident; char *ident;
struct libwebsocket *wsi; struct lws *wsi;
int fd; int fd;
char *host; char *host;
int port; int port;
...@@ -230,7 +228,7 @@ struct status { ...@@ -230,7 +228,7 @@ struct status {
struct status *status_list; struct status *status_list;
static void start_status(struct libwebsocket *wsi, char *ident) static void start_status(struct lws *wsi, char *ident)
{ {
struct status *status = calloc(sizeof(struct status), 1); struct status *status = calloc(sizeof(struct status), 1);
char *host; char *host;
...@@ -338,7 +336,7 @@ static int status_reconnect(struct status *status) ...@@ -338,7 +336,7 @@ static int status_reconnect(struct status *status)
return 0; return 0;
} }
static int status_remove(struct libwebsocket *wsi) static int status_remove(struct lws *wsi)
{ {
struct status **status; struct status **status;
struct status **next; struct status **next;
...@@ -366,7 +364,7 @@ static int status_remove(struct libwebsocket *wsi) ...@@ -366,7 +364,7 @@ static int status_remove(struct libwebsocket *wsi)
struct trace *traces[MAX_TRACE_ELEMENTS]; struct trace *traces[MAX_TRACE_ELEMENTS];
int count_traces = 0; int count_traces = 0;
static int trace_remove(struct libwebsocket *wsi) static int trace_remove(struct lws *wsi)
{ {
int n; int n;
for (n = 0; n < count_traces; n++) { for (n = 0; n < count_traces; n++) {
...@@ -388,7 +386,7 @@ static int trace_remove(struct libwebsocket *wsi) ...@@ -388,7 +386,7 @@ static int trace_remove(struct libwebsocket *wsi)
static void handler_trace_value(struct trace *trace, int channel, static void handler_trace_value(struct trace *trace, int channel,
struct trace_value *value) struct trace_value *value)
{ {
struct libwebsocket *wsi; struct lws *wsi;
struct ws_client *ws_client; struct ws_client *ws_client;
struct writebuf *wb; struct writebuf *wb;
...@@ -442,7 +440,7 @@ static void handler_trace_value(struct trace *trace, int channel, ...@@ -442,7 +440,7 @@ static void handler_trace_value(struct trace *trace, int channel,
ws_client_flush(ws_client); ws_client_flush(ws_client);
} }
static void start_trace(struct libwebsocket *wsi, int freq, char *variable) static void start_trace(struct lws *wsi, int freq, char *variable)
{ {
struct trace *trace; struct trace *trace;
struct timespec t_int; struct timespec t_int;
...@@ -593,7 +591,7 @@ err: ...@@ -593,7 +591,7 @@ err:
#define READ_SIZE 4096 #define READ_SIZE 4096
int exec_cgi(struct libwebsocket *wsi, char *requested_uri, char *resource_path) int exec_cgi(struct lws *wsi, char *requested_uri, char *resource_path)
{ {
FILE *fpipe; FILE *fpipe;
unsigned char *outdata = NULL; unsigned char *outdata = NULL;
...@@ -617,7 +615,7 @@ int exec_cgi(struct libwebsocket *wsi, char *requested_uri, char *resource_path) ...@@ -617,7 +615,7 @@ int exec_cgi(struct libwebsocket *wsi, char *requested_uri, char *resource_path)
} while (r > 0); } while (r > 0);
if (!lws_send_pipe_choked(wsi)) if (!lws_send_pipe_choked(wsi))
libwebsocket_write(wsi, outdata, pos, LWS_WRITE_HTTP); lws_write(wsi, outdata, pos, LWS_WRITE_HTTP);
pclose(fpipe); pclose(fpipe);
free(outdata); free(outdata);
...@@ -626,31 +624,31 @@ int exec_cgi(struct libwebsocket *wsi, char *requested_uri, char *resource_path) ...@@ -626,31 +624,31 @@ int exec_cgi(struct libwebsocket *wsi, char *requested_uri, char *resource_path)
return -1; return -1;
} }
int list_dir(struct libwebsocket_context *context, struct libwebsocket *wsi, char *requested_uri, char *resource_path) int list_dir(struct lws *wsi, char *requested_uri, char *resource_path)
{ {
unsigned char *outdata = malloc(1000); unsigned char *outdata = malloc(1000);
size_t pos = 0; size_t pos = 0;
struct dirent **namelist; struct dirent **namelist;
int n, i; int n, i;
unsigned char *h = outdata; unsigned char *h = outdata;
char *server = "console_httpd libwebsockets"; char *server = "console_httpd lws";
char *type = "text/html"; char *type = "text/html";
if (lws_add_http_header_status(context, wsi, 200, &h, outdata + 1000)) if (lws_add_http_header_status(wsi, 200, &h, outdata + 1000))
return 1; return 1;
if (lws_add_http_header_by_token(context, wsi, if (lws_add_http_header_by_token(wsi,
WSI_TOKEN_HTTP_SERVER, WSI_TOKEN_HTTP_SERVER,
(unsigned char *)server, strlen(server), &h, outdata + 1000)) (unsigned char *)server, strlen(server), &h, outdata + 1000))
return 1; return 1;
if (lws_add_http_header_by_token(context, wsi, if (lws_add_http_header_by_token(wsi,
WSI_TOKEN_HTTP_CONTENT_TYPE, WSI_TOKEN_HTTP_CONTENT_TYPE,
(unsigned char *)type, strlen(type), &h, outdata + 1000)) (unsigned char *)type, strlen(type), &h, outdata + 1000))
return 1; return 1;
if (lws_finalize_http_header(context, wsi, &h, outdata + 1000)) if (lws_finalize_http_header(wsi, &h, outdata + 1000))
return 1; return 1;
pos += h - outdata; pos += h - outdata;
libwebsocket_write(wsi, outdata, pos, LWS_WRITE_HTTP_HEADERS); lws_write(wsi, outdata, pos, LWS_WRITE_HTTP_HEADERS);
free(outdata); free(outdata);
outdata = NULL; outdata = NULL;
pos = 0; pos = 0;
...@@ -677,7 +675,7 @@ int list_dir(struct libwebsocket_context *context, struct libwebsocket *wsi, cha ...@@ -677,7 +675,7 @@ int list_dir(struct libwebsocket_context *context, struct libwebsocket *wsi, cha
free(namelist); free(namelist);
if (!lws_send_pipe_choked(wsi)) if (!lws_send_pipe_choked(wsi))
libwebsocket_write(wsi, outdata, pos, LWS_WRITE_HTTP); lws_write(wsi, outdata, pos, LWS_WRITE_HTTP);
free(outdata); free(outdata);
...@@ -686,9 +684,8 @@ int list_dir(struct libwebsocket_context *context, struct libwebsocket *wsi, cha ...@@ -686,9 +684,8 @@ int list_dir(struct libwebsocket_context *context, struct libwebsocket *wsi, cha
static int callback_http(struct libwebsocket_context *context, static int callback_http(struct lws *wsi,
struct libwebsocket *wsi, enum lws_callback_reasons reason, void *user,
enum libwebsocket_callback_reasons reason, void *user,
void *in, size_t len) void *in, size_t len)
{ {
int r = 0; int r = 0;
...@@ -701,7 +698,7 @@ static int callback_http(struct libwebsocket_context *context, ...@@ -701,7 +698,7 @@ static int callback_http(struct libwebsocket_context *context,
ws_client = ws_client_get_by_wsi(wsi); ws_client = ws_client_get_by_wsi(wsi);
if (!ws_client) { if (!ws_client) {
ws_client = ws_client_add(context, wsi); ws_client = ws_client_add(wsi);
} }
if (!strncmp(rcv, "status ", 7)) { if (!strncmp(rcv, "status ", 7)) {
...@@ -814,7 +811,7 @@ static int callback_http(struct libwebsocket_context *context, ...@@ -814,7 +811,7 @@ static int callback_http(struct libwebsocket_context *context,
stat(resource_path, &statbuf); stat(resource_path, &statbuf);
if (S_ISDIR(statbuf.st_mode)) { if (S_ISDIR(statbuf.st_mode)) {
r = list_dir(context, wsi, requested_uri, resource_path); r = list_dir(wsi, requested_uri, resource_path);
} else if (!strcmp( } else if (!strcmp(
resource_path + strlen(resource_path) - strlen(".cgi"), resource_path + strlen(resource_path) - strlen(".cgi"),
".cgi")) { ".cgi")) {
...@@ -832,7 +829,7 @@ static int callback_http(struct libwebsocket_context *context, ...@@ -832,7 +829,7 @@ static int callback_http(struct libwebsocket_context *context,
if (!mime) if (!mime)
mime = "application/octet-stream"; mime = "application/octet-stream";
r = libwebsockets_serve_http_file(context, wsi, r = lws_serve_http_file(wsi,
resource_path, mime, NULL, 0); resource_path, mime, NULL, 0);
} }
...@@ -841,19 +838,19 @@ static int callback_http(struct libwebsocket_context *context, ...@@ -841,19 +838,19 @@ static int callback_http(struct libwebsocket_context *context,
} }
case LWS_CALLBACK_ADD_POLL_FD: { case LWS_CALLBACK_ADD_POLL_FD: {
struct libwebsocket_pollargs *args = in; struct lws_pollargs *args = in;
poll_add(args->fd, args->events); poll_add(args->fd, args->events);
break; break;
} }
case LWS_CALLBACK_DEL_POLL_FD: { case LWS_CALLBACK_DEL_POLL_FD: {
struct libwebsocket_pollargs *args = in; struct lws_pollargs *args = in;
poll_remove(args->fd); poll_remove(args->fd);
break; break;
} }
case LWS_CALLBACK_CHANGE_MODE_POLL_FD: { case LWS_CALLBACK_CHANGE_MODE_POLL_FD: {
struct libwebsocket_pollargs *args = in; struct lws_pollargs *args = in;
poll_set(args->fd, args->events); poll_set(args->fd, args->events);
break; break;
...@@ -878,7 +875,7 @@ static int callback_http(struct libwebsocket_context *context, ...@@ -878,7 +875,7 @@ static int callback_http(struct libwebsocket_context *context,
return r; return r;
} }
static struct libwebsocket_protocols protocols[] = { static struct lws_protocols protocols[] = {
// first protocol must always be HTTP handler // first protocol must always be HTTP handler
{ {
"http-only", // name "http-only", // name
...@@ -895,7 +892,7 @@ int main(void) { ...@@ -895,7 +892,7 @@ int main(void) {
if (magic_load(magic, NULL)) if (magic_load(magic, NULL))
printf("magic_load failed\n"); printf("magic_load failed\n");
struct libwebsocket_context *context; struct lws_context *context;
struct lws_context_creation_info creation_info = { struct lws_context_creation_info creation_info = {
.port = dt_host_htdocs_port(), .port = dt_host_htdocs_port(),
.iface = NULL, .iface = NULL,
...@@ -904,7 +901,7 @@ int main(void) { ...@@ -904,7 +901,7 @@ int main(void) {
.uid = -1, .uid = -1,
}; };
context = libwebsocket_create_context(&creation_info); context = lws_create_context(&creation_info);
if (context == NULL) { if (context == NULL) {
fprintf(stderr, "libwebsocket init failed\n"); fprintf(stderr, "libwebsocket init failed\n");
...@@ -947,7 +944,7 @@ int main(void) { ...@@ -947,7 +944,7 @@ int main(void) {
break; break;
} }
} }
libwebsocket_service_fd(context, &pollfds[n]); lws_service_fd(context, &pollfds[n]);
} }
now = time(NULL); now = time(NULL);
...@@ -970,7 +967,7 @@ int main(void) { ...@@ -970,7 +967,7 @@ int main(void) {
} }
} }
libwebsocket_context_destroy(context); lws_context_destroy(context);
magic_close(magic); magic_close(magic);
return 0; return 0;
......
...@@ -82,3 +82,5 @@ params { ...@@ -82,3 +82,5 @@ params {
} }
} }
} }
set trace_server false
...@@ -42,3 +42,5 @@ params { ...@@ -42,3 +42,5 @@ params {
(int) { $[COMMAND_ID_NONE], 123, 234, 567, 666, 777, $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE] } (int) { $[COMMAND_ID_NONE], 123, 234, 567, 666, 777, $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE] }
} }
} }
set trace_server false
...@@ -43,3 +43,5 @@ params { ...@@ -43,3 +43,5 @@ params {
(int) { $[COMMAND_ID_NONE], 123, 234, 567, 666, 777, $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE] } (int) { $[COMMAND_ID_NONE], 123, 234, 567, 666, 777, $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE] }
} }
} }
set trace_server false
...@@ -42,3 +42,5 @@ params { ...@@ -42,3 +42,5 @@ params {
(int) { $[COMMAND_ID_NONE], 123, 234, 567, 666, 777, $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE] } (int) { $[COMMAND_ID_NONE], 123, 234, 567, 666, 777, $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE], $[COMMAND_ID_NONE] }
} }
} }
set trace_server false
...@@ -44,3 +44,4 @@ params { ...@@ -44,3 +44,4 @@ params {
{ "gain", "gain", (float) 0.1 } { "gain", "gain", (float) 0.1 }
} }
set trace_server false
...@@ -23,3 +23,5 @@ params { ...@@ -23,3 +23,5 @@ params {
(float) { 0.0, 0.0, 0.0, 0.0 } (float) { 0.0, 0.0, 0.0, 0.0 }
} }
} }
set trace_server false
...@@ -30,3 +30,5 @@ params { ...@@ -30,3 +30,5 @@ params {
(float) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } (float) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }
} }
} }
set trace_server false
...@@ -67,3 +67,5 @@ params { ...@@ -67,3 +67,5 @@ params {
} }
} }
} }
set trace_server false
...@@ -29,3 +29,5 @@ params { ...@@ -29,3 +29,5 @@ params {
(float) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } (float) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }
} }
} }
set trace_server false
...@@ -87,3 +87,5 @@ params { ...@@ -87,3 +87,5 @@ params {
} }
} }
} }
set trace_server false
...@@ -35,3 +35,5 @@ params { ...@@ -35,3 +35,5 @@ params {
(float) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } (float) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }
} }
} }
set trace_server false
...@@ -25,3 +25,5 @@ params { ...@@ -25,3 +25,5 @@ params {
false, false, false, false, false false, false, false, false, false
}} }}
} }
set trace_server false
...@@ -42,3 +42,5 @@ params { ...@@ -42,3 +42,5 @@ params {
(float) { 0.0, 0.0, 0.0, 0.0 } (float) { 0.0, 0.0, 0.0, 0.0 }
} }
} }
set trace_server false
...@@ -34,3 +34,5 @@ params { ...@@ -34,3 +34,5 @@ params {
(float) { 0.0, 0.0, 0.0001, 0.0 } (float) { 0.0, 0.0, 0.0001, 0.0 }
} }
} }
set trace_server false
...@@ -19,3 +19,5 @@ params { ...@@ -19,3 +19,5 @@ params {
{ "test_input", "value", 2, (int) { false, true } } { "test_input", "value", 2, (int) { false, true } }
{ "test_output", "value", 2, (int) { true, false } } { "test_output", "value", 2, (int) { true, false } }
} }
set trace_server false
...@@ -43,3 +43,5 @@ params { ...@@ -43,3 +43,5 @@ params {
} }
} }
} }
set trace_server false
...@@ -59,3 +59,5 @@ params { ...@@ -59,3 +59,5 @@ params {
(float) { 0.0, 0.0, 0.0, 0.0, inf, inf, inf, inf, inf, 0.0} (float) { 0.0, 0.0, 0.0, 0.0, inf, inf, inf, inf, inf, 0.0}
} }
} }
set trace_server false
...@@ -117,7 +117,7 @@ static void calculate(struct controller_block *block) ...@@ -117,7 +117,7 @@ static void calculate(struct controller_block *block)
if ((INPUT(home) == true) && (VAR(last_home) == false)) { if ((INPUT(home) == true) && (VAR(last_home) == false)) {
// rising edge on 'home' input // rising edge on 'home' input
if (pos != 0) { if (pos != 0) {
log_send(LOG_T_DEBUG, "homing @ %d\n", pos); log_send(LOG_T_DEBUG, "homing @ %d", pos);