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 @@
*.la
*.dot
*.pdf
*.svg
.libs
bin
include
......@@ -15,6 +15,11 @@ else
FLEX=/usr/local/bin/flex
endif
ifdef TARGET
CC=${TARGET}-gcc
LIBTOOL=${TARGET}-libtool
endif
%.o : %.c
@echo " CC $<"
......@@ -41,9 +46,10 @@ endif
define LIB_LINK
@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 $@"
@${LIBTOOL} --quiet --mode=install install $@ $(abspath $(@D))
@sed s\|=$(CURDIR)\|$(CURDIR)\|g -i -i $@
endef
%.so:
......
LIBDIR:=$(DIR)/lib
CLEAN += $(DIR)/lib/.libs
$(eval $(call SUBDIR,log))
$(eval $(call SUBDIR,utils))
$(eval $(call SUBDIR,command))
......
......@@ -8,7 +8,7 @@ ARCHSRCS := $(DIR)/command.c $(DIR)/command_tcp.c
ARCHOBJS := $(ARCHSRCS:.c=.lo)
$(ARCHOBJS) += -Wall -O3 -fPIC
$(ARCHOBJS): CFLAGS += -Wall -O3 -fPIC
$(LIBDIR)/libcommand.la: libutils.la liblog.la
$(LIBDIR)/libcommand.la_LDFLAGS += -lutils -lm -llog
$(LIBDIR)/libcommand.la: $(ARCHOBJS)
......
......@@ -21,7 +21,6 @@
#include <command/command.h>
#include <command/command_def.h>
#include <utils/tcp_connect.h>
#include <log/log.h>
#include <stdio.h>
......@@ -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",
command->name);
command->fd = tcp_connect(command->host, command->port);
command_connect(command);
if (command->fd >= 0) {
struct command_pkt *pkt;
ioctl(command->fd, FIONBIO, &(int){1});
if (command->name) {
pkt = command_packet_new();
......
......@@ -122,6 +122,7 @@ void command_free(struct command *command);
struct command *command_open(char *host, int port);
void command_initialize(struct command *command, size_t buffer);
void command_initialize_fd(struct command *command, int fd);
int command_connect(struct command *command);
void command_close(struct command *command);
struct command *command_open_simple(char *host, int port, char *name, char *id,
......
......@@ -19,6 +19,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
......
......@@ -2,7 +2,7 @@
command tcp connection handling
Copyright Jeroen Vreeken (jeroen@vreeken.net), 2014
trac
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
the Free Software Foundation, either version 3 of the License, or
......@@ -27,7 +27,10 @@ trac
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#define COMMAND_BUF_BLOCK 4096
......@@ -47,11 +50,9 @@ struct command *command_open(char *host, int port)
command->host = strdup(host);
command->port = port;
command->fd = -1;
command->fd = tcp_connect(host, port);
if (command->fd >= 0) {
ioctl(command->fd, FIONBIO, &(int){1});
}
command_connect(command);
return command;
}
......@@ -62,15 +63,37 @@ void command_initialize_fd(struct command *command, int fd)
command->rx_len = 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)
{
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);
}
command->fd = -1;
command->recover = false;
}
......
......@@ -125,7 +125,7 @@ do { \
#define va_new(list) \
do { \
char *ptr = malloc(sizeof(char *)) + sizeof(char *)*2; \
char *ptr = malloc(sizeof(char *)*2) + sizeof(char *)*2; \
(list).__ap = ptr; \
((char **)ptr)[-1] = ptr; \
} while (0)
......@@ -145,6 +145,7 @@ do { \
clist = realloc(&clist[-2], \
size + fill + sizeof(type) + sizeof(char *)*2) + sizeof(char *)*2; \
(list).__ap = (void *)clist; \
clist[-1] = clist[0]; \
ptr = ((char *)clist + size); \
ptr += fill; \
memcpy(ptr, &tmpval, sizeof(tmpval)); \
......
......@@ -22,7 +22,6 @@
#include <trace/trace.h>
#include <trace/trace_def.h>
#include <utils/tcp_connect.h>
#include <log/log.h>
#include <stdio.h>
......@@ -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",
trace->name);
trace->fd = tcp_connect(trace->host, trace->port);
trace_connect(trace);
if (trace->fd >= 0) {
struct trace_pkt *pkt;
ioctl(trace->fd, FIONBIO, &(int){1});
if (trace->name) {
pkt = trace_packet_new();
trace_packet_name_set(pkt, trace->name);
......
/*
/*
trace support
Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008, 2011
......@@ -139,6 +139,7 @@ struct trace *trace_alloc(void);
struct trace *trace_open(char *host, int port);
void trace_initialize(struct trace *trace, size_t buffer);
void trace_initialize_fd(struct trace *trace, int fd);
int trace_connect(struct trace *trace);
int trace_interval_set(struct trace *trace,
struct timespec *interval, enum trace_interval_type type);
int trace_name_set(struct trace *trace, char *name);
......
......@@ -19,6 +19,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
......
......@@ -24,6 +24,7 @@
#include <utils/tcp_connect.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
......@@ -47,12 +48,10 @@ struct trace *trace_open(char *host, int port)
trace->host = strdup(host);
trace->port = port;
trace->fd = -1;
trace->fd = tcp_connect(host, port);
if (trace->fd >= 0) {
ioctl(trace->fd, FIONBIO, &(int){1});
}
trace_connect(trace);
return trace;
}
......@@ -62,10 +61,21 @@ void trace_initialize_fd(struct trace *trace, int fd)
trace->rx_len = 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)
{
log_send(LOG_T_DEBUG, "Close trace on request");
......
......@@ -25,7 +25,8 @@ LU_CFLAGS := -Wall -g -fPIC
LU_LDFLAGS := -lpthread -lrt -lm
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)
$(LIBDIR)/libutils.la_LDFLAGS += $(LU_LDFLAGS)
......
......@@ -38,6 +38,7 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "tcp_listen.h"
......@@ -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 *srv;
int flags;
srv = calloc(1, sizeof(struct command_server));
if (!srv)
......@@ -78,7 +80,8 @@ struct command_server *command_server_create(int port, int local, int backlog)
if (srv->fd < 0)
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;
......@@ -221,11 +224,13 @@ int command_server_fdset_handle(struct command_server *srv, fd_set *set)
if (FD_ISSET(srv->fd, set)) {
/* New client */
int client_fd;
int flags;
client_fd = tcp_accept(srv->fd);
if (client_fd < 0)
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));
if (!client) {
......
......@@ -24,6 +24,7 @@
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include "tcp_listen.h"
......@@ -44,6 +45,7 @@ struct status_server {
struct status_server *status_server_create(int port, int local, int backlog)
{
struct status_server *srv;
int flags;
srv = calloc(1, sizeof(struct status_server));
if (!srv)
......@@ -53,7 +55,8 @@ struct status_server *status_server_create(int port, int local, int backlog)
if (srv->fd < 0)
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;
......@@ -100,11 +103,13 @@ int status_server_fdset_handle(struct status_server *srv, fd_set *set)
if (FD_ISSET(srv->fd, set)) {
/* New client */
int client_fd;
int flags;
client_fd = tcp_accept(srv->fd);
if (client_fd < 0)
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));
if (!client) {
......
......@@ -21,7 +21,9 @@
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
......@@ -58,9 +60,12 @@ int tcp_connect(char *host, int port)
}
for (entry = result; entry; entry = entry->ai_next) {
int flags;
sock = socket(entry->ai_family, entry->ai_socktype,
entry->ai_protocol);
ioctl(sock, FIONBIO, &(int){1});
flags = fcntl(sock, F_GETFL, 0);
fcntl(sock, F_SETFL, flags | O_NONBLOCK);
if (sock >= 0) {
fd_set fdset_tx, fdset_err;
struct timeval tv;
......@@ -87,7 +92,8 @@ int tcp_connect(char *host, int port)
}
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,
&(int){1}, sizeof(int));
......@@ -128,17 +134,18 @@ int tcp_flushwrite(int sock)
usleep(100000);
}
return 0;
#endif
return 0;
}
int tcp_flushread(int sock)
{
char buffer[1024];
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--) {
ssize_t r = read(sock, buffer, 1024);
......
......@@ -24,6 +24,7 @@
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
......@@ -33,6 +34,7 @@
int tcp_listen(int port, int local, int backlog)
{
int fdlisten, optval;
int flags;
struct sockaddr_in sin;
if ((fdlisten = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
......@@ -63,8 +65,8 @@ int tcp_listen(int port, int local, int backlog)
return -1;
}
optval = 1;
ioctl(fdlisten, FIONBIO, &optval);
flags = fcntl(fdlisten, F_GETFL, 0);
fcntl(fdlisten, F_SETFL, flags | O_NONBLOCK);
return fdlisten;
}
......@@ -74,11 +76,11 @@ int tcp_accept(int fd_listen)
struct sockaddr_in sin;
socklen_t addrlen = sizeof(struct sockaddr_in);
int fd;
int optval;
int flags;
if ((fd = accept(fd_listen, (struct sockaddr *)&sin, &addrlen)) >= 0) {
optval = 1;
ioctl(fd, FIONBIO, &optval);
flags = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, flags | O_NONBLOCK);
}
return fd;
......
all:
@$(MAKE) --no-print-directory -C ../.. targets_console/console
LIBDIR:=$(DIR)/lib
CLEAN += $(DIR)/lib/.libs
$(eval $(call SUBDIR,aalib))
$(eval $(call SUBDIR,predictlib))
......
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