Commit 56d3de7d authored by Jeroen Vreeken's avatar Jeroen Vreeken
Browse files

Use command_server library in spg_auth

Add comments to command_server lib
Remove now obsolete ec_el_test
Fix controller_sample shell newlines
parent 6021486a
......@@ -17,7 +17,21 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
*/
/*
command_server.c
library for opening a tcp listening socket on which clients can
send commands.
commands can be in the form of 'key=value,key=value,key=value' which
can be handled by registering a handler function with
command_server_handler_set().
If commands are in another format they can be handled by registering
a handler function with command_server_handler_raw_set()
*/
#include <stdlib.h>
#include <stdio.h>
......
/*
Command authoriser
Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008, 2009
Copyright Stichting C.A. Muller Radioastronomiestation, 2008, 2009
Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2008, 2009, 2013
Copyright Stichting C.A. Muller Radioastronomiestation, 2008, 2009, 2013
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
......@@ -39,18 +39,12 @@
#include "setpoint.h"
#include "tcp_listen.h"
#include "block_setpoint_generator.h"
#include "status_server.h"
#include "command_server.h"
#include "dt_port_numbers.h"
#include "dt_host.h"
struct console_client {
struct console_client *next;
int fd;
char inbuf[100];
int inlen;
};
struct console_client *consoles = NULL;
struct spg_client {
struct spg_client *next;
......@@ -84,29 +78,11 @@ struct spg spgs[] = {
struct spg_client *newclients = NULL;
struct status_client {
struct status_client *next;
int fd;
};
struct status_client *statuss = NULL;
struct offset_client {
struct offset_client *next;
int fd;
size_t inlen;
char inbuf[65536];
char *offsetline;
size_t inpos;
int done;
};
typedef union {
float f;
uint32_t u;
} float32_t;
struct offset_client *offsets = NULL;
char *command_host = "localhost";
......@@ -123,7 +99,6 @@ char current_auth[100] = { "none" };
int fd_listen;
int fd_console;
int fd_status;
int fd_offset;
time_t lastcmd;
......@@ -208,36 +183,12 @@ void new_connection(void)
}
void handle_offsets(void)
int handle_offsets(char *command)
{
struct offset_client *offset, *next;
sscanf(command, "%f %f", &az_offset, &el_offset);
printf("New offset: %e %e\n", az_offset, el_offset);
for (offset = offsets; offset; offset = next) {
int i;
next = offset->next;
if (offset->inlen)
{
for (i = 0; i < offset->inlen; i++) {
if (offset->inbuf[i] == '\r' ||
offset->inbuf[i] == '\n' ) {
offset->inbuf[i] = 0;
if (strlen(offset->inbuf)) {
sscanf(offset->inbuf, "%f %f", &az_offset, &el_offset);
az_offset = az_offset;
el_offset = el_offset;
printf("New offset: %e %e\n", az_offset, el_offset);
}
memmove(offset->inbuf, offset->inbuf+i +1, offset->inlen - i -1);
offset->inlen -= i;
i = -1;
}
}
}
if (offset->fd < 0) {
offset->done = 1;
}
}
return 0;
}
void try_command(struct spg_client *client)
......@@ -423,144 +374,31 @@ void handle_client(struct spg_client *client, struct spg_client **clientp)
}
}
void new_console(void)
{
int fd;
struct console_client *new_client;
fd = tcp_accept(fd_console);
if (fd < 0)
return;
ioctl(fd, FIONBIO, &(int){ 1 });
new_client = malloc(sizeof(struct console_client));
if (!new_client) {
close(fd);
return;
}
new_client->fd = fd;
new_client->inlen = 0;
new_client->next = consoles;
consoles = new_client;
printf("New console\n");
}
void new_offset(void)
{
int fd;
struct offset_client *new_client;
fd = tcp_accept(fd_offset);
if (fd < 0)
return;
ioctl(fd, FIONBIO, &(int){ 1 });
new_client = malloc(sizeof(struct offset_client));
if (!new_client) {
close(fd);
return;
}
new_client->fd = fd;
new_client->inlen = 0;
new_client->inpos = 0;
new_client->offsetline = new_client->inbuf;
new_client->next = offsets;
new_client->done = 0;
offsets = new_client;
printf("New offset client\n");
}
void handle_offset_client(struct offset_client *offset_client)
{
int ret;
do {
ret = read(offset_client->fd, offset_client->inbuf + offset_client->inlen, 1);
if (ret == 1) {
offset_client->inlen++;
handle_offsets();
} else {
if (ret == 0 || errno != EAGAIN) {
close(offset_client->fd);
offset_client->fd = -1;
printf("Offset connection dropped\n");
}
}
} while (ret == 1);
}
void handle_con_client(struct console_client *con_client)
int handle_con_client(char *command)
{
int ret;
do {
ret = read(con_client->fd, con_client->inbuf + con_client->inlen, 1);
if (ret == 1) {
con_client->inlen++;
if (con_client->inbuf[con_client->inlen - 1] == '\n' ||
con_client->inbuf[con_client->inlen - 1] == '\r') {
con_client->inbuf[con_client->inlen - 1] = 0;
if (con_client->inlen > 1) {
if (strcmp(current_auth, con_client->inbuf)) {
int i;
if (strcmp(current_auth, command)) {
int i;
strcpy(current_auth, con_client->inbuf);
for (i = 0; spgs[i].name; i++) {
setpoint_command_speed(spgs[i].spg_cmd, 0.0);
}
strcpy(current_auth, command);
while (current_auth[strlen(current_auth)-1] == '\n' ||
current_auth[strlen(current_auth)-1] == '\r')
current_auth[strlen(current_auth)-1] = 0;
printf("New authorised user: %s\n",
current_auth);
}
}
con_client->inlen = 0;
}
} else {
if (ret == 0 || errno != EAGAIN) {
close(con_client->fd);
con_client->fd = -1;
printf("Console connection dropped\n");
}
for (i = 0; spgs[i].name; i++) {
setpoint_command_speed(spgs[i].spg_cmd, 0.0);
}
if (con_client->inlen == 100)
con_client->inlen = 0;
} while (ret == 1);
}
void new_status(void)
{
int fd;
struct status_client *new_client;
fd = tcp_accept(fd_status);
if (fd < 0)
return;
ioctl(fd, FIONBIO, &(int){ 1 });
new_client = malloc(sizeof(struct status_client));
if (!new_client) {
close(fd);
return;
printf("New authorised user: %s\n",
current_auth);
}
new_client->fd = fd;
new_client->next = statuss;
statuss = new_client;
printf("New status client\n");
return 0;
}
void update_status(void)
void update_status(struct status_server *stat_srv)
{
static time_t last = 0;
time_t now;
struct status_client **clientp, **next = NULL;
char status[100];
now = time(NULL);
......@@ -572,27 +410,15 @@ void update_status(void)
snprintf(status, 99, "%s %e %e\n", current_auth, az_offset, el_offset);
status[99] = 0;
for (clientp = &statuss, next = clientp; *clientp; clientp = next) {
int ret = 1;
ret = write((*clientp)->fd, status, strlen(status));
if (ret <= 0) {
struct status_client *oldclient;
oldclient = *clientp;
*clientp = oldclient->next;
close(oldclient->fd);
free (oldclient);
printf("Lost status client\n");
} else {
next = &(*clientp)->next;
}
}
status_server_send(stat_srv, status);
}
int main (int argc, char **argv)
{
int i;
struct status_server *stat_srv;
struct command_server *offset_srv;
struct command_server *console_srv;
signal(SIGPIPE, SIG_IGN);
......@@ -614,25 +440,22 @@ int main (int argc, char **argv)
}
ioctl(fd_listen, FIONBIO, &(int){ 1 });
fd_console = tcp_listen(CONSOLE_ACTIVE_PORT, 0, 100);
if (fd_console < 0) {
console_srv = command_server_create(CONSOLE_ACTIVE_PORT, 0, 100);
if (!console_srv) {
printf("Could not open listen port for consoles\n");
} else {
ioctl(fd_console, FIONBIO, &(int){ 1 });
}
fd_status = tcp_listen(CONSOLE_STATUS_PORT, 0, 100);
if (fd_status < 0) {
stat_srv = status_server_create(CONSOLE_STATUS_PORT, 0, 100);
if (!stat_srv) {
printf("Could not open listen port for status\n");
} else {
ioctl(fd_status, FIONBIO, &(int){ 1 });
}
fd_offset = tcp_listen(CONSOLE_OFFSET_PORT, 0, 100);
if (fd_offset < 0) {
offset_srv = command_server_create(CONSOLE_OFFSET_PORT, 0, 100);
if (!offset_srv) {
printf("Could not open listen port for offset\n");
} else {
ioctl(fd_offset, FIONBIO, &(int){ 1 });
}
command_server_handler_raw_set(offset_srv, handle_offsets);
command_server_handler_raw_set(console_srv, handle_con_client);
while (1) {
fd_set fd_rd;
......@@ -640,8 +463,6 @@ int main (int argc, char **argv)
int high;
struct spg_client *client, **clientp;
int i;
struct console_client *con_client, **con_clientp;
struct offset_client *offset_client, **offset_clientp;
timeout.tv_usec = 0;
timeout.tv_sec = 1;
......@@ -649,15 +470,10 @@ int main (int argc, char **argv)
FD_ZERO(&fd_rd);
high = fd_listen + 1;
FD_SET(fd_listen, &fd_rd);
FD_SET(fd_console, &fd_rd);
if (fd_console >= high)
high = fd_console + 1;
FD_SET(fd_status, &fd_rd);
if (fd_status >= high)
high = fd_status + 1;
FD_SET(fd_offset, &fd_rd);
if (fd_offset >= high)
high = fd_offset + 1;
status_server_fdset_add(stat_srv, &fd_rd, &high);
command_server_fdset_add(offset_srv, &fd_rd, &high);
command_server_fdset_add(console_srv, &fd_rd, &high);
for (i = 0; spgs[i].name; i++) {
for (client = spgs[i].clients; client; client = client->next) {
......@@ -673,33 +489,17 @@ int main (int argc, char **argv)
if (client->fd >= high)
high = client->fd + 1;
}
for (con_client = consoles; con_client; con_client = con_client->next) {
FD_SET(con_client->fd, &fd_rd);
if (con_client->fd >= high)
high = con_client->fd + 1;
}
for (offset_client = offsets; offset_client; offset_client = offset_client->next) {
if (offset_client->fd < 0)
continue;
FD_SET(offset_client->fd, &fd_rd);
if (offset_client->fd >= high)
high = offset_client->fd + 1;
}
select(high, &fd_rd, NULL, NULL, &timeout);
if (FD_ISSET(fd_listen, &fd_rd)) {
new_connection();
}
if (FD_ISSET(fd_console, &fd_rd)) {
new_console();
}
if (FD_ISSET(fd_status, &fd_rd)) {
new_status();
}
if (FD_ISSET(fd_offset, &fd_rd)) {
new_offset();
}
status_server_fdset_handle(stat_srv, &fd_rd);
command_server_fdset_handle(offset_srv, &fd_rd);
command_server_fdset_handle(console_srv, &fd_rd);
for (i = 0; spgs[i].name; i++) {
for (client = spgs[i].clients; client; client = client->next) {
handle_client(client, NULL);
......@@ -726,32 +526,7 @@ int main (int argc, char **argv)
clientp = &client->next;
}
}
for (con_client = consoles; con_client; con_client = con_client->next) {
handle_con_client(con_client);
}
for (con_client = consoles, con_clientp = &consoles; con_client; con_client = *con_clientp) {
if (con_client->fd < 0) {
*con_clientp = con_client->next;
free(con_client);
printf("Removed console client\n");
} else {
con_clientp = &con_client->next;
}
}
for (offset_client = offsets; offset_client; offset_client = offset_client->next) {
if (offset_client->fd < 0)
continue;
handle_offset_client(offset_client);
}
for (offset_client = offsets, offset_clientp = &offsets; offset_client; offset_client = *offset_clientp) {
if (offset_client->fd < 0 && offset_client->done) {
*offset_clientp = offset_client->next;
free(offset_client);
printf("Removed offset client\n");
} else {
offset_clientp = &offset_client->next;
}
}
update_status();
update_status(stat_srv);
}
}
......@@ -13,7 +13,7 @@ screen -S console -X screen -t log_proxy log_proxy
sleep 1
screen -S console -X screen -t console_idle console_idle
screen -S console -X screen -t console_dt_model
screen -S console -X screen -t console_dt_model console_dt_model
screen -S console -X screen -t console_j2000tracker console_j2000tracker
screen -S console -X screen -t console_j2000_indi indiserver console_j2000_indi
screen -S console -X screen -t console_moontracker console_moontracker
......
......@@ -52,16 +52,6 @@
<div style="border: thin solid black; background: lightgrey"
id="Azimuth_Enabled">
Azimuth Drive<br>
<ul>
<div style="border: thin solid black; background: lightgrey"
id="Azimuth_Drive_Safety">
Safety
</div>
<br>
</ul>
</div>
</td>
......@@ -193,20 +183,6 @@
<div style="border: thin solid black; background: lightgrey"
id="Elevation_Enabled">
Elevation Drives<br>
<ul>
<div style="border: thin solid black; background: lightgrey"
id="Elevation_Drive_Safety_Right">
Safety Right
</div>
<div style="border: thin solid black; background: lightgrey"
id="Elevation_Drive_Safety_Left">
Safety Left
</div>
<br>
</ul>
</div>
</td>
......@@ -559,9 +535,6 @@ for (i = 0; i < traceval_list.length; i++) {
Azimuth Drive
*/
Azimuth_Drive_Safety_trace = new trace();
trace_bool('Azimuth_Drive_Safety', Azimuth_Drive_Safety_trace);
Azimuth_Enabled_trace = new trace();
trace_bool('Azimuth_Enabled', Azimuth_Enabled_trace);
......@@ -588,12 +561,6 @@ trace_bool('Elevation_Safe', Elevation_Safe_trace);
Elevation Drives
*/
Elevation_Drive_Safety_Right_trace = new trace();
trace_bool('Elevation_Drive_Safety_Right', Elevation_Drive_Safety_Right_trace);
Elevation_Drive_Safety_Left_trace = new trace();
trace_bool('Elevation_Drive_Safety_Left', Elevation_Drive_Safety_Left_trace);
Elevation_Enabled_trace = new trace();
trace_bool('Elevation_Enabled', Elevation_Enabled_trace);
......
......@@ -244,9 +244,13 @@ static ssize_t controller_sample_timing_snprintf(char *dest, size_t n)
ssize_t used = 0;
used += sample_timing_snprintf(&st_start, dest, n);
used += sprintf(dest + used, "\n");
used += sample_timing_snprintf(&st_end, dest + used, n - used);
used += sprintf(dest + used, "\n");
used += sample_timing_snprintf(&st_io, dest + used, n - used);
used += sprintf(dest + used, "\n");
used += sample_timing_snprintf(&st_sample, dest + used, n - used);
used += sprintf(dest + used, "\n");
return used;
}
......
......@@ -34,7 +34,7 @@ BLOCKSRCS= \
BLOCKS=$(BLOCKSRCS:.c=.lo)
SRCS=$(BLOCKSRCS) $(LIBSRCS)
all: ec_test.o libethercat.la_install ec_enum ec_el_test
all: ec_test.o libethercat.la_install ec_enum
libethercat.la_LDFLAGS=-rpath ${CURDIR}/../lib -lcontroller
libethercat.la: $(LIBOBJS) $(BLOCKS)
......@@ -50,10 +50,7 @@ ec_enum: ec_enum.o
ec_enum.o: esc_id.h
ec_el_test_LDFLAGS= `../controller/block_list.sh ../lib/lib*.a` -Wl,-E -lcontroller -lethercat -llog
ec_el_test: ec_el_test.o libethercat.la_install
clean:
rm -rf *.o *.a *.la *.lo .libs *.d ec_enum ec_el_test
rm -rf *.o *.a *.la *.lo .libs *.d ec_enum
include ../build.mk
......@@ -743,7 +743,7 @@ int ec_init(char *ifname, bool single_cycle)
single_pdo = single_cycle;
if (single_pdo) {
tx_pdo = &rx_pdo;
printf("Using a single PDO for rx and tx.\n");
log_send(LOG_T_DEBUG, "Using a single PDO for rx and tx.");
}
log_send(LOG_T_DEBUG,
......
/*
Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2009
Copyright Stichting C.A. Muller Radioastronomiestation, 2009
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
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include <controller/controller_block.h>
#include <controller/controller_trace.h>
#include <controller/controller_sample.h>
#include <controller/controller_load.h>
#include <controller/controller_dumpdot.h>
#include "ec.h"
#include "block_ec.h"
int main(int argc, char **argv)
{
controller_block_param_init();
/* Create and link blocks */
if (controller_load("ec_el_test.ctrl")) {
printf("Could not create or link blocks\n");
return 1;
}
controller_trace_server_start(