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;
}
......
......@@ -49,3 +49,5 @@ params {
}
}
}
set trace_server false
......@@ -27,3 +27,5 @@ params {
(float) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }
}
}
set trace_server false
......@@ -33,3 +33,5 @@ params {
{ false, true, false, false, false, false, true }
}
}
set trace_server false
......@@ -67,3 +67,5 @@ params {
{ "reset", "value", 5, (int) { true, true, true, false, true } }
{ "enable", "value", 5, (int) { false, false, true, true, false } }
}
set trace_server false
......@@ -71,3 +71,5 @@ params {
(int)0xffffffff, 201,(int)0xffffffff,(int)0xffffffff,(int)0xffffffff,(int)0xffffffff,(int)0xffffffff,(int)0xffffffff,(int)0xffffffff,(int)0xffffffff}
}
}
set trace_server false
......@@ -118,3 +118,5 @@ params {
301, (int)0xffffffff }
}
}
set trace_server false
......@@ -102,3 +102,4 @@ params {
1, 0, 1, 0, 1, 0, 1, 0, 0, 1 } }
}
set trace_server false
......@@ -221,7 +221,7 @@ static void *controller_command_server(void *arg)
listen_sock = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sock < 0) {
perror("socket() failed");
log_send(LOG_T_ERROR, "controller_command_server: socket() failed: %d", errno);
return NULL;
}
......@@ -234,13 +234,13 @@ static void *controller_command_server(void *arg)
if (bind(listen_sock,
(struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) {
perror("bind() failed");
log_send(LOG_T_ERROR, "controller_command_server: bind() failed: %d", errno);
close(listen_sock);
return NULL;
}
if (listen(listen_sock, 8) < 0) {
perror("listen() failed");
log_send(LOG_T_ERROR, "controller_command_server: listen() failed: %d", errno);
close(listen_sock);
return NULL;
}
......
......@@ -30,4 +30,11 @@ int controller_load(char *filename);
int controller_load_check_arg(char **expected_types, int supplied_argc,
char **supplied_types);
char * controller_load_variable_string_get(char *varname);
void controller_load_variable_string_set(char *varname, char *val);
int controller_load_variable_int_get(char *varname);
void controller_load_variable_int_set(char *varname, int i);
#endif /* _INCLUDE_CONTROLLER_LOAD_H_ */
......@@ -387,7 +387,7 @@ static void *controller_trace_server(void *arg)
listen_sock = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sock < 0) {
perror("socket() failed");
log_send(LOG_T_ERROR, "controller_trace: socket() failed: %d", errno);
return NULL;
}
......@@ -400,13 +400,13 @@ static void *controller_trace_server(void *arg)
if (bind(listen_sock,
(struct sockaddr *)&sockaddr, sizeof(sockaddr)) < 0) {
perror("bind() failed");
log_send(LOG_T_ERROR, "controller_trace: bind() failed: %d", errno);
close(listen_sock);
return NULL;
}
if (listen(listen_sock, 8) < 0) {
perror("listen() failed");
log_send(LOG_T_ERROR, "controller_trace: listen() failed: %d", errno);
close(listen_sock);
return NULL;
}
......
/*
Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007 - 2013
Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2007 - 2016
Copyright Stichting C.A. Muller Radioastronomiestation, 2007 - 2013
This program is free software: you can redistribute it and/or modify
......@@ -17,7 +17,7 @@
*/
/*
dt_ctr.c
dt_ctrl.c
Main loop for the DT controller.
Here the controller network is initialized.
......@@ -74,6 +74,12 @@ int main(int argc, char **argv)
ctrl_filename = argv[1];
controller_load_variable_int_set("shell", 0);
controller_load_variable_string_set("shell_intro", shell_intro);
controller_load_variable_string_set("shell_prompt", shell_prompt);
controller_load_variable_int_set("trace_server", 1);
/* Create and link blocks */
log_send(LOG_T_DEBUG, "Going to load controller file '%s'",
ctrl_filename);
......@@ -91,17 +97,20 @@ int main(int argc, char **argv)
for (i = 0; i < blocks; i++) {
outputs += controller_block_get(i)->outputs;
}
controller_trace_server_start(CTRL_TRACE_PORT, outputs);
if (controller_load_variable_int_get("trace_server"))
controller_trace_server_start(CTRL_TRACE_PORT, outputs);
/* Start command shell */
controller_sample_shell_add();
controller_load_shell_add();
if (controller_load_variable_int_get("shell")) {
controller_sample_shell_add();
controller_load_shell_add();
shell_intro = "Dwingeloo Telescope controller\n";
shell_prompt = "DT>";
shell_intro = controller_load_variable_string_get("shell_intro");
shell_prompt = controller_load_variable_string_get("shell_prompt");
shell_server_start(CTRL_SHELL_PORT, 10);
shell_server_start(CTRL_SHELL_PORT, 10);
}
asprintf(&dot_filename, "%s.dot", ctrl_filename);
controller_dumpdot(dot_filename);
......
......@@ -25,13 +25,13 @@ trigger {
# Uncomment either the real network, or the sim network.
# But never both!
import "dt_ctrl_el.ctrl"
#import "dt_ctrl_el_sim.ctrl"
#import "dt_ctrl_el.ctrl"
import "dt_ctrl_el_sim.ctrl"
import "dt_ctrl_az.ctrl"
#import "dt_ctrl_az_sim.ctrl"
#import "dt_ctrl_az.ctrl"
import "dt_ctrl_az_sim.ctrl"
#import "dt_ctrl_ec_sim.ctrl"
import "dt_ctrl_ec_sim.ctrl"
......@@ -468,6 +468,10 @@ params {
{ "elevation_safety_hw_bottom", "init", true }
}
set shell true
set shell_intro "Dwingeloo Telescope controller"
set shell_prompt "DT>"
# Load file with calibration parameters.