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

Move servo state and safety blocks to the command protocol

Various small fixes
Add command protocol to websocket
Add cross compiling with 'TARGET='
Fix some issues with ARM
parent cbe16b01
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
*.la *.la
*.dot *.dot
*.pdf *.pdf
*.svg
.libs .libs
bin bin
include include
...@@ -15,6 +15,11 @@ else ...@@ -15,6 +15,11 @@ else
FLEX=/usr/local/bin/flex FLEX=/usr/local/bin/flex
endif endif
ifdef TARGET
CC=${TARGET}-gcc
LIBTOOL=${TARGET}-libtool
endif
%.o : %.c %.o : %.c
@echo " CC $<" @echo " CC $<"
...@@ -41,9 +46,10 @@ endif ...@@ -41,9 +46,10 @@ endif
define LIB_LINK define LIB_LINK
@echo "LT LD $@" @echo "LT LD $@"
@${LIBTOOL} --quiet --mode=link gcc $(filter %.lo,$^) -o $@ $(LDFLAGS) $($@_LDFLAGS) -static-libtool-libs -rpath $(abspath $(@D)) @${LIBTOOL} --quiet --mode=link --tag=CC $(CC) $(filter %.lo,$^) -o $@ $(LDFLAGS) $($@_LDFLAGS) -static-libtool-libs -rpath $(abspath $(@D))
@echo "LT INST $@" @echo "LT INST $@"
@${LIBTOOL} --quiet --mode=install install $@ $(abspath $(@D)) @${LIBTOOL} --quiet --mode=install install $@ $(abspath $(@D))
@sed s\|=$(CURDIR)\|$(CURDIR)\|g -i -i $@
endef endef
%.so: %.so:
......
LIBDIR:=$(DIR)/lib LIBDIR:=$(DIR)/lib
CLEAN += $(DIR)/lib/.libs
$(eval $(call SUBDIR,log)) $(eval $(call SUBDIR,log))
$(eval $(call SUBDIR,utils)) $(eval $(call SUBDIR,utils))
$(eval $(call SUBDIR,command)) $(eval $(call SUBDIR,command))
......
...@@ -8,7 +8,7 @@ ARCHSRCS := $(DIR)/command.c $(DIR)/command_tcp.c ...@@ -8,7 +8,7 @@ ARCHSRCS := $(DIR)/command.c $(DIR)/command_tcp.c
ARCHOBJS := $(ARCHSRCS:.c=.lo) ARCHOBJS := $(ARCHSRCS:.c=.lo)
$(ARCHOBJS) += -Wall -O3 -fPIC $(ARCHOBJS): CFLAGS += -Wall -O3 -fPIC
$(LIBDIR)/libcommand.la: libutils.la liblog.la $(LIBDIR)/libcommand.la: libutils.la liblog.la
$(LIBDIR)/libcommand.la_LDFLAGS += -lutils -lm -llog $(LIBDIR)/libcommand.la_LDFLAGS += -lutils -lm -llog
$(LIBDIR)/libcommand.la: $(ARCHOBJS) $(LIBDIR)/libcommand.la: $(ARCHOBJS)
......
...@@ -21,7 +21,6 @@ ...@@ -21,7 +21,6 @@
#include <command/command.h> #include <command/command.h>
#include <command/command_def.h> #include <command/command_def.h>
#include <utils/tcp_connect.h>
#include <log/log.h> #include <log/log.h>
#include <stdio.h> #include <stdio.h>
...@@ -253,11 +252,9 @@ bool command_fd_set(struct command *command, fd_set *set, int *high) ...@@ -253,11 +252,9 @@ bool command_fd_set(struct command *command, fd_set *set, int *high)
log_send(LOG_T_DEBUG, "Attempt to recover command %s", log_send(LOG_T_DEBUG, "Attempt to recover command %s",
command->name); command->name);
command->fd = tcp_connect(command->host, command->port); command_connect(command);
if (command->fd >= 0) { if (command->fd >= 0) {
struct command_pkt *pkt; struct command_pkt *pkt;
ioctl(command->fd, FIONBIO, &(int){1});
if (command->name) { if (command->name) {
pkt = command_packet_new(); pkt = command_packet_new();
......
...@@ -122,6 +122,7 @@ void command_free(struct command *command); ...@@ -122,6 +122,7 @@ void command_free(struct command *command);
struct command *command_open(char *host, int port); struct command *command_open(char *host, int port);
void command_initialize(struct command *command, size_t buffer); void command_initialize(struct command *command, size_t buffer);
void command_initialize_fd(struct command *command, int fd); void command_initialize_fd(struct command *command, int fd);
int command_connect(struct command *command);
void command_close(struct command *command); void command_close(struct command *command);
struct command *command_open_simple(char *host, int port, char *name, char *id, struct command *command_open_simple(char *host, int port, char *name, char *id,
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
command tcp connection handling command tcp connection handling
Copyright Jeroen Vreeken (jeroen@vreeken.net), 2014 Copyright Jeroen Vreeken (jeroen@vreeken.net), 2014
trac
This program is free software: you can redistribute it and/or modify This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or the Free Software Foundation, either version 3 of the License, or
...@@ -27,7 +27,10 @@ trac ...@@ -27,7 +27,10 @@ trac
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h> #include <stdio.h>
#define COMMAND_BUF_BLOCK 4096 #define COMMAND_BUF_BLOCK 4096
...@@ -47,11 +50,9 @@ struct command *command_open(char *host, int port) ...@@ -47,11 +50,9 @@ struct command *command_open(char *host, int port)
command->host = strdup(host); command->host = strdup(host);
command->port = port; command->port = port;
command->fd = -1;
command->fd = tcp_connect(host, port); command_connect(command);
if (command->fd >= 0) {
ioctl(command->fd, FIONBIO, &(int){1});
}
return command; return command;
} }
...@@ -62,15 +63,37 @@ void command_initialize_fd(struct command *command, int fd) ...@@ -62,15 +63,37 @@ void command_initialize_fd(struct command *command, int fd)
command->rx_len = 0; command->rx_len = 0;
if (command->fd >= 0) { if (command->fd >= 0) {
ioctl(command->fd, FIONBIO, &(int){1}); int flags = fcntl(command->fd, F_GETFL, 0);
fcntl(command->fd, F_SETFL, flags | O_NONBLOCK);
}
}
int command_connect(struct command *command)
{
int fd = tcp_connect(command->host, command->port);
if (fd >= 0) {
command_initialize_fd(command, fd);
return 0;
} }
return -1;
} }
void command_close(struct command *command) void command_close(struct command *command)
{ {
log_send(LOG_T_DEBUG, "Close command on request"); log_send(LOG_T_DEBUG, "Close command on request");
if (command->fd >= 0) if (command->fd >= 0) {
struct linger so_linger;
so_linger.l_onoff = 1;
so_linger.l_linger = 10;
setsockopt(command->fd,
SOL_SOCKET,
SO_LINGER,
&so_linger,
sizeof(so_linger));
sleep(1);
close(command->fd); close(command->fd);
}
command->fd = -1; command->fd = -1;
command->recover = false; command->recover = false;
} }
......
...@@ -125,7 +125,7 @@ do { \ ...@@ -125,7 +125,7 @@ do { \
#define va_new(list) \ #define va_new(list) \
do { \ do { \
char *ptr = malloc(sizeof(char *)) + sizeof(char *)*2; \ char *ptr = malloc(sizeof(char *)*2) + sizeof(char *)*2; \
(list).__ap = ptr; \ (list).__ap = ptr; \
((char **)ptr)[-1] = ptr; \ ((char **)ptr)[-1] = ptr; \
} while (0) } while (0)
...@@ -145,6 +145,7 @@ do { \ ...@@ -145,6 +145,7 @@ do { \
clist = realloc(&clist[-2], \ clist = realloc(&clist[-2], \
size + fill + sizeof(type) + sizeof(char *)*2) + sizeof(char *)*2; \ size + fill + sizeof(type) + sizeof(char *)*2) + sizeof(char *)*2; \
(list).__ap = (void *)clist; \ (list).__ap = (void *)clist; \
clist[-1] = clist[0]; \
ptr = ((char *)clist + size); \ ptr = ((char *)clist + size); \
ptr += fill; \ ptr += fill; \
memcpy(ptr, &tmpval, sizeof(tmpval)); \ memcpy(ptr, &tmpval, sizeof(tmpval)); \
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include <trace/trace.h> #include <trace/trace.h>
#include <trace/trace_def.h> #include <trace/trace_def.h>
#include <utils/tcp_connect.h>
#include <log/log.h> #include <log/log.h>
#include <stdio.h> #include <stdio.h>
...@@ -351,12 +350,10 @@ bool trace_fd_set(struct trace *trace, fd_set *set, int *high) ...@@ -351,12 +350,10 @@ bool trace_fd_set(struct trace *trace, fd_set *set, int *high)
log_send(LOG_T_DEBUG, "Attempt to recover trace %s", log_send(LOG_T_DEBUG, "Attempt to recover trace %s",
trace->name); trace->name);
trace->fd = tcp_connect(trace->host, trace->port); trace_connect(trace);
if (trace->fd >= 0) { if (trace->fd >= 0) {
struct trace_pkt *pkt; struct trace_pkt *pkt;
ioctl(trace->fd, FIONBIO, &(int){1});
if (trace->name) { if (trace->name) {
pkt = trace_packet_new(); pkt = trace_packet_new();
trace_packet_name_set(pkt, trace->name); trace_packet_name_set(pkt, trace->name);
......
/* /*
trace support trace support
Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008, 2011 Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008, 2011
...@@ -139,6 +139,7 @@ struct trace *trace_alloc(void); ...@@ -139,6 +139,7 @@ struct trace *trace_alloc(void);
struct trace *trace_open(char *host, int port); struct trace *trace_open(char *host, int port);
void trace_initialize(struct trace *trace, size_t buffer); void trace_initialize(struct trace *trace, size_t buffer);
void trace_initialize_fd(struct trace *trace, int fd); void trace_initialize_fd(struct trace *trace, int fd);
int trace_connect(struct trace *trace);
int trace_interval_set(struct trace *trace, int trace_interval_set(struct trace *trace,
struct timespec *interval, enum trace_interval_type type); struct timespec *interval, enum trace_interval_type type);
int trace_name_set(struct trace *trace, char *name); int trace_name_set(struct trace *trace, char *name);
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <utils/tcp_connect.h> #include <utils/tcp_connect.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
...@@ -47,12 +48,10 @@ struct trace *trace_open(char *host, int port) ...@@ -47,12 +48,10 @@ struct trace *trace_open(char *host, int port)
trace->host = strdup(host); trace->host = strdup(host);
trace->port = port; trace->port = port;
trace->fd = -1;
trace->fd = tcp_connect(host, port); trace_connect(trace);
if (trace->fd >= 0) {
ioctl(trace->fd, FIONBIO, &(int){1});
}
return trace; return trace;
} }
...@@ -62,10 +61,21 @@ void trace_initialize_fd(struct trace *trace, int fd) ...@@ -62,10 +61,21 @@ void trace_initialize_fd(struct trace *trace, int fd)
trace->rx_len = 0; trace->rx_len = 0;
if (trace->fd >= 0) { if (trace->fd >= 0) {
ioctl(trace->fd, FIONBIO, &(int){1}); int flags = fcntl(trace->fd, F_GETFL, 0);
fcntl(trace->fd, F_SETFL, flags | O_NONBLOCK);
} }
} }
int trace_connect(struct trace *trace)
{
int fd = tcp_connect(trace->host, trace->port);
if (fd >= 0) {
trace_initialize_fd(trace, fd);
return 0;
}
return -1;
}
void trace_close(struct trace *trace) void trace_close(struct trace *trace)
{ {
log_send(LOG_T_DEBUG, "Close trace on request"); log_send(LOG_T_DEBUG, "Close trace on request");
......
...@@ -25,7 +25,8 @@ LU_CFLAGS := -Wall -g -fPIC ...@@ -25,7 +25,8 @@ LU_CFLAGS := -Wall -g -fPIC
LU_LDFLAGS := -lpthread -lrt -lm LU_LDFLAGS := -lpthread -lrt -lm
LU_CFLAGS += `pkg-config --cflags glib-2.0` LU_CFLAGS += `pkg-config --cflags glib-2.0`
LU_LDFLAGS += `pkg-config --libs glib-2.0` LU_LDFLAGS += -lglib-2.0
#`pkg-config --libs glib-2.0`
$(ARCHOBJS): CFLAGS += $(LU_CFLAGS) $(ARCHOBJS): CFLAGS += $(LU_CFLAGS)
$(LIBDIR)/libutils.la_LDFLAGS += $(LU_LDFLAGS) $(LIBDIR)/libutils.la_LDFLAGS += $(LU_LDFLAGS)
......
...@@ -38,6 +38,7 @@ ...@@ -38,6 +38,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include "tcp_listen.h" #include "tcp_listen.h"
...@@ -69,6 +70,7 @@ static int default_func(char *name, char *value) ...@@ -69,6 +70,7 @@ static int default_func(char *name, char *value)
struct command_server *command_server_create(int port, int local, int backlog) struct command_server *command_server_create(int port, int local, int backlog)
{ {
struct command_server *srv; struct command_server *srv;
int flags;
srv = calloc(1, sizeof(struct command_server)); srv = calloc(1, sizeof(struct command_server));
if (!srv) if (!srv)
...@@ -78,7 +80,8 @@ struct command_server *command_server_create(int port, int local, int backlog) ...@@ -78,7 +80,8 @@ struct command_server *command_server_create(int port, int local, int backlog)
if (srv->fd < 0) if (srv->fd < 0)
goto err_listen; goto err_listen;
ioctl(srv->fd, FIONBIO, &(int){ 1 }); flags = fcntl(srv->fd, F_GETFL, 0);
fcntl(srv->fd, F_SETFL, flags | O_NONBLOCK);
srv->handle_func = default_func; srv->handle_func = default_func;
...@@ -221,11 +224,13 @@ int command_server_fdset_handle(struct command_server *srv, fd_set *set) ...@@ -221,11 +224,13 @@ int command_server_fdset_handle(struct command_server *srv, fd_set *set)
if (FD_ISSET(srv->fd, set)) { if (FD_ISSET(srv->fd, set)) {
/* New client */ /* New client */
int client_fd; int client_fd;
int flags;
client_fd = tcp_accept(srv->fd); client_fd = tcp_accept(srv->fd);
if (client_fd < 0) if (client_fd < 0)
return -1; return -1;
ioctl(client_fd, FIONBIO, &(int){ 1 }); flags = fcntl(client_fd, F_GETFL, 0);
fcntl(client_fd, F_SETFL, flags | O_NONBLOCK);
client = calloc(1, sizeof(struct command_client)); client = calloc(1, sizeof(struct command_client));
if (!client) { if (!client) {
......
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include "tcp_listen.h" #include "tcp_listen.h"
...@@ -44,6 +45,7 @@ struct status_server { ...@@ -44,6 +45,7 @@ struct status_server {
struct status_server *status_server_create(int port, int local, int backlog) struct status_server *status_server_create(int port, int local, int backlog)
{ {
struct status_server *srv; struct status_server *srv;
int flags;
srv = calloc(1, sizeof(struct status_server)); srv = calloc(1, sizeof(struct status_server));
if (!srv) if (!srv)
...@@ -53,7 +55,8 @@ struct status_server *status_server_create(int port, int local, int backlog) ...@@ -53,7 +55,8 @@ struct status_server *status_server_create(int port, int local, int backlog)
if (srv->fd < 0) if (srv->fd < 0)
goto err_listen; goto err_listen;
ioctl(srv->fd, FIONBIO, &(int){ 1 }); flags = fcntl(srv->fd, F_GETFL, 0);
fcntl(srv->fd, F_SETFL, flags | O_NONBLOCK);
return srv; return srv;
...@@ -100,11 +103,13 @@ int status_server_fdset_handle(struct status_server *srv, fd_set *set) ...@@ -100,11 +103,13 @@ int status_server_fdset_handle(struct status_server *srv, fd_set *set)
if (FD_ISSET(srv->fd, set)) { if (FD_ISSET(srv->fd, set)) {
/* New client */ /* New client */
int client_fd; int client_fd;
int flags;
client_fd = tcp_accept(srv->fd); client_fd = tcp_accept(srv->fd);
if (client_fd < 0) if (client_fd < 0)
return -1; return -1;
ioctl(client_fd, FIONBIO, &(int){ 1 }); flags = fcntl(srv->fd, F_GETFL, 0);
fcntl(srv->fd, F_SETFL, flags | O_NONBLOCK);
client = calloc(1, sizeof(struct status_client)); client = calloc(1, sizeof(struct status_client));
if (!client) { if (!client) {
......
...@@ -21,7 +21,9 @@ ...@@ -21,7 +21,9 @@
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <netdb.h> #include <netdb.h>
...@@ -58,9 +60,12 @@ int tcp_connect(char *host, int port) ...@@ -58,9 +60,12 @@ int tcp_connect(char *host, int port)
} }
for (entry = result; entry; entry = entry->ai_next) { for (entry = result; entry; entry = entry->ai_next) {
int flags;
sock = socket(entry->ai_family, entry->ai_socktype, sock = socket(entry->ai_family, entry->ai_socktype,
entry->ai_protocol); entry->ai_protocol);
ioctl(sock, FIONBIO, &(int){1}); flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
if (sock >= 0) { if (sock >= 0) {
fd_set fdset_tx, fdset_err; fd_set fdset_tx, fdset_err;
struct timeval tv; struct timeval tv;
...@@ -87,7 +92,8 @@ int tcp_connect(char *host, int port) ...@@ -87,7 +92,8 @@ int tcp_connect(char *host, int port)
} }
if (sock >= 0) { if (sock >= 0) {
ioctl(sock, FIONBIO, &(int){0}); flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags & ~O_NONBLOCK);
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE,
&(int){1}, sizeof(int)); &(int){1}, sizeof(int));
...@@ -128,17 +134,18 @@ int tcp_flushwrite(int sock) ...@@ -128,17 +134,18 @@ int tcp_flushwrite(int sock)
usleep(100000); usleep(100000);
} }
return 0;
#endif #endif
return 0;
} }
int tcp_flushread(int sock) int tcp_flushread(int sock)
{ {
char buffer[1024]; char buffer[1024];
int retries = 20; int retries = 20;
int flags;
ioctl(sock, FIONBIO, &(int){1}); flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
while(retries--) { while(retries--) {
ssize_t r = read(sock, buffer, 1024); ssize_t r = read(sock, buffer, 1024);
......