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)
fd_accept = socket(AF_INET, SOCK_STREAM, 0);
if (fd_accept < 0) {
perror("socket() failed");
printf("ERROR: log_server: socket() failed: %d\n", errno);
return NULL;
}
......@@ -224,13 +224,13 @@ static void *log_server(void *arg)
if (bind(fd_accept,
(struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) {
perror("bind() failed");
printf("ERROR: log_server: bind() failed: %d\n", errno);
close(fd_accept);
fd_accept = -1;
}
if (fd_accept >= 0 && listen(fd_accept, 4) < 0) {
perror("listen() failed");
printf("ERROR: log_server: listen() failed: %d\n", errno);
close(fd_accept);
fd_accept = -1;
}
......
......@@ -46,8 +46,7 @@ struct writebuf {
};
struct ws_client {
struct libwebsocket_context *context;
struct libwebsocket *wsi;
struct lws *wsi;
struct writebuf *writeq;
......@@ -102,7 +101,7 @@ struct writebuf *writebuf_next(struct ws_client *client)
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;
......@@ -110,7 +109,6 @@ struct ws_client *ws_client_add(struct libwebsocket_context *context, struct lib
if (!client)
return NULL;
client->context = context;
client->wsi = wsi;
client->next = ws_client_list;
......@@ -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;
......@@ -159,13 +157,13 @@ void ws_client_flush(struct ws_client *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);
}
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)
#define LINEBUF_SIZE 8192
struct status {
char *ident;
struct libwebsocket *wsi;
struct lws *wsi;
int fd;
char *host;
int port;
......@@ -230,7 +228,7 @@ struct status {
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);
char *host;
......@@ -338,7 +336,7 @@ static int status_reconnect(struct status *status)
return 0;
}
static int status_remove(struct libwebsocket *wsi)
static int status_remove(struct lws *wsi)
{
struct status **status;
struct status **next;
......@@ -366,7 +364,7 @@ static int status_remove(struct libwebsocket *wsi)
struct trace *traces[MAX_TRACE_ELEMENTS];
int count_traces = 0;
static int trace_remove(struct libwebsocket *wsi)
static int trace_remove(struct lws *wsi)
{
int n;
for (n = 0; n < count_traces; n++) {
......@@ -388,7 +386,7 @@ static int trace_remove(struct libwebsocket *wsi)
static void handler_trace_value(struct trace *trace, int channel,
struct trace_value *value)
{
struct libwebsocket *wsi;
struct lws *wsi;
struct ws_client *ws_client;
struct writebuf *wb;
......@@ -442,7 +440,7 @@ static void handler_trace_value(struct trace *trace, int channel,
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 timespec t_int;
......@@ -593,7 +591,7 @@ err:
#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;
unsigned char *outdata = NULL;
......@@ -617,7 +615,7 @@ int exec_cgi(struct libwebsocket *wsi, char *requested_uri, char *resource_path)
} while (r > 0);
if (!lws_send_pipe_choked(wsi))
libwebsocket_write(wsi, outdata, pos, LWS_WRITE_HTTP);
lws_write(wsi, outdata, pos, LWS_WRITE_HTTP);
pclose(fpipe);
free(outdata);
......@@ -626,31 +624,31 @@ int exec_cgi(struct libwebsocket *wsi, char *requested_uri, char *resource_path)
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);
size_t pos = 0;
struct dirent **namelist;
int n, i;
unsigned char *h = outdata;
char *server = "console_httpd libwebsockets";
char *server = "console_httpd lws";
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;
if (lws_add_http_header_by_token(context, wsi,
if (lws_add_http_header_by_token(wsi,
WSI_TOKEN_HTTP_SERVER,
(unsigned char *)server, strlen(server), &h, outdata + 1000))
return 1;
if (lws_add_http_header_by_token(context, wsi,
if (lws_add_http_header_by_token(wsi,
WSI_TOKEN_HTTP_CONTENT_TYPE,
(unsigned char *)type, strlen(type), &h, outdata + 1000))
return 1;
if (lws_finalize_http_header(context, wsi, &h, outdata + 1000))
if (lws_finalize_http_header(wsi, &h, outdata + 1000))
return 1;
pos += h - outdata;
libwebsocket_write(wsi, outdata, pos, LWS_WRITE_HTTP_HEADERS);
lws_write(wsi, outdata, pos, LWS_WRITE_HTTP_HEADERS);
free(outdata);
outdata = NULL;
pos = 0;
......@@ -677,7 +675,7 @@ int list_dir(struct libwebsocket_context *context, struct libwebsocket *wsi, cha
free(namelist);
if (!lws_send_pipe_choked(wsi))
libwebsocket_write(wsi, outdata, pos, LWS_WRITE_HTTP);
lws_write(wsi, outdata, pos, LWS_WRITE_HTTP);
free(outdata);
......@@ -686,9 +684,8 @@ int list_dir(struct libwebsocket_context *context, struct libwebsocket *wsi, cha
static int callback_http(struct libwebsocket_context *context,
struct libwebsocket *wsi,
enum libwebsocket_callback_reasons reason, void *user,
static int callback_http(struct lws *wsi,
enum lws_callback_reasons reason, void *user,
void *in, size_t len)
{
int r = 0;
......@@ -701,7 +698,7 @@ static int callback_http(struct libwebsocket_context *context,
ws_client = ws_client_get_by_wsi(wsi);
if (!ws_client) {
ws_client = ws_client_add(context, wsi);
ws_client = ws_client_add(wsi);
}
if (!strncmp(rcv, "status ", 7)) {
......@@ -814,7 +811,7 @@ static int callback_http(struct libwebsocket_context *context,
stat(resource_path, &statbuf);
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(
resource_path + strlen(resource_path) - strlen(".cgi"),
".cgi")) {
......@@ -832,7 +829,7 @@ static int callback_http(struct libwebsocket_context *context,
if (!mime)
mime = "application/octet-stream";
r = libwebsockets_serve_http_file(context, wsi,
r = lws_serve_http_file(wsi,
resource_path, mime, NULL, 0);
}
......@@ -841,19 +838,19 @@ static int callback_http(struct libwebsocket_context *context,
}
case LWS_CALLBACK_ADD_POLL_FD: {
struct libwebsocket_pollargs *args = in;
struct lws_pollargs *args = in;
poll_add(args->fd, args->events);
break;
}
case LWS_CALLBACK_DEL_POLL_FD: {
struct libwebsocket_pollargs *args = in;
struct lws_pollargs *args = in;
poll_remove(args->fd);
break;
}
case LWS_CALLBACK_CHANGE_MODE_POLL_FD: {
struct libwebsocket_pollargs *args = in;
struct lws_pollargs *args = in;
poll_set(args->fd, args->events);
break;
......@@ -878,7 +875,7 @@ static int callback_http(struct libwebsocket_context *context,
return r;
}
static struct libwebsocket_protocols protocols[] = {
static struct lws_protocols protocols[] = {
// first protocol must always be HTTP handler
{
"http-only", // name
......@@ -895,7 +892,7 @@ int main(void) {
if (magic_load(magic, NULL))
printf("magic_load failed\n");
struct libwebsocket_context *context;
struct lws_context *context;
struct lws_context_creation_info creation_info = {
.port = dt_host_htdocs_port(),
.iface = NULL,
......@@ -904,7 +901,7 @@ int main(void) {
.uid = -1,
};
context = libwebsocket_create_context(&creation_info);
context = lws_create_context(&creation_info);
if (context == NULL) {
fprintf(stderr, "libwebsocket init failed\n");
......@@ -947,7 +944,7 @@ int main(void) {
break;
}
}
libwebsocket_service_fd(context, &pollfds[n]);
lws_service_fd(context, &pollfds[n]);
}
now = time(NULL);
......@@ -970,7 +967,7 @@ int main(void) {
}
}
libwebsocket_context_destroy(context);
lws_context_destroy(context);
magic_close(magic);
return 0;
......
......@@ -82,3 +82,5 @@ params {
}
}
}
set trace_server false
......@@ -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] }
}
}
set trace_server false
......@@ -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] }
}
}
set trace_server false
......@@ -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] }
}
}
set trace_server false
......@@ -44,3 +44,4 @@ params {
{ "gain", "gain", (float) 0.1 }
}
set trace_server false
......@@ -23,3 +23,5 @@ params {
(float) { 0.0, 0.0, 0.0, 0.0 }
}
}
set trace_server false
......@@ -30,3 +30,5 @@ params {
(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 {
}
}
}
set trace_server false
......@@ -29,3 +29,5 @@ params {
(float) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }
}
}
set trace_server false
......@@ -87,3 +87,5 @@ params {
}
}
}
set trace_server false
......@@ -35,3 +35,5 @@ params {
(float) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }
}
}
set trace_server false
......@@ -25,3 +25,5 @@ params {
false, false, false, false, false
}}
}
set trace_server false
......@@ -42,3 +42,5 @@ params {
(float) { 0.0, 0.0, 0.0, 0.0 }
}
}
set trace_server false
......@@ -34,3 +34,5 @@ params {
(float) { 0.0, 0.0, 0.0001, 0.0 }
}
}
set trace_server false
......@@ -19,3 +19,5 @@ params {
{ "test_input", "value", 2, (int) { false, true } }
{ "test_output", "value", 2, (int) { true, false } }
}
set trace_server false
......@@ -43,3 +43,5 @@ params {
}
}
}
set trace_server false
......@@ -59,3 +59,5 @@ params {
(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)
if ((INPUT(home) == true) && (VAR(last_home) == false)) {
// rising edge on 'home' input
if (pos != 0) {
log_send(LOG_T_DEBUG, "homing @ %d\n", pos);
log_send(LOG_T_DEBUG, "homing @ %d", pos);
}
pos = 0;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment