Commit 29fd1ec8 authored by Michel Roelofs's avatar Michel Roelofs
Browse files

Merge commit 'cc2393f1' into michel

parents cc6d1a4d cc2393f1
......@@ -28,6 +28,8 @@
#define CONSOLE_SUN_CMD_PORT 11071
#define CONSOLE_MOON_STAT_PORT 11080
#define CONSOLE_MOON_CMD_PORT 11081
#define CONSOLE_DT_MODEL_STAT_PORT 11090
#define CONSOLE_DT_MODEL_CMD_PORT 11091
#define CONSOLE_LOG_PORT 11200
......
......@@ -88,7 +88,7 @@ do { \
/* endif __x86_64__ */
#elif defined(__i386__)
#elif defined(__i386__) || (defined(__arm__) && !defined(__ARM_EABI__))
......
......@@ -5,7 +5,8 @@ LDFLAGS= -lpthread
CFLAGS+=`pkg-config --cflags glib-2.0`
LDFLAGS+=`pkg-config --libs glib-2.0`
UTILSRCS= tcp_connect.c tcp_listen.c config.c weather.c dt_model.c location.c dt_host.c
UTILSRCS= tcp_connect.c tcp_listen.c command_server.c status_server.c \
config.c weather.c dt_model.c location.c dt_host.c
LIBUTILOBJS= $(UTILSRCS:.c=.lo)
all: $(LIBUTILOBJS) weather_test libutils.la libutils.la_install
......
/*
Command server
Copyright Jeroen Vreeken (jeroen@vreeken.net), 2013
Copyright Stichting C.A. Muller Radioastronomiestation, 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
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/>.
*/
/*
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>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include "tcp_listen.h"
struct command_client {
struct command_client *next;
int fd;
char buffer[200];
int buflen;
};
struct command_server {
int fd;
struct command_client *clients;
int (*handle_func)(char *name, char *value);
int (*handle_raw_func)(char *command);
};
static int default_func(char *name, char *value)
{
printf("No command handler installed!!!");
return -1;
}
struct command_server *command_server_create(int port, int local, int backlog)
{
struct command_server *srv;
srv = calloc(1, sizeof(struct command_server));
if (!srv)
goto err_alloc;
srv->fd = tcp_listen(port, local, backlog);
if (srv->fd < 0)
goto err_listen;
ioctl(srv->fd, FIONBIO, &(int){ 1 });
srv->handle_func = default_func;
return srv;
err_listen:
close(srv->fd);
err_alloc:
return NULL;
}
void command_server_destroy(struct command_server *srv)
{
struct command_client *client;
if (srv) {
while (srv->clients) {
client = srv->clients;
srv->clients = client->next;
close(client->fd);
free(client);
}
close(srv->fd);
free(srv);
}
}
int command_server_handler_set(struct command_server *srv,
int (*func)(char *name, char *value))
{
/* Remember who to call when a command is received */
srv->handle_func = func;
return 0;
}
int command_server_handler_raw_set(struct command_server *srv,
int (*func)(char *command))
{
/* Remember who to call when a command is received */
srv->handle_raw_func = func;
return 0;
}
int command_server_fdset_add(struct command_server *srv, fd_set *set, int *high)
{
/* Add all our sockets to the set
*/
struct command_client *client;
for (client = srv->clients; client; client = client->next) {
FD_SET(client->fd, set);
if (client->fd >= *high)
*high = client->fd + 1;
}
FD_SET(srv->fd, set);
if (srv->fd >= *high)
*high = srv->fd + 1;
return 0;
}
void command_server_handle(struct command_server *srv, struct command_client *client)
{
/* A command has been received.
Split it in a key, value pair and pass it to application handler
Some applications have a command format which is not (completly) in
a key=value format. They can get the 'raw' string and still enjoy
the other benefits of this lib.
*/
char *namevalue;
char *name;
char *value;
char *ptr;
if (srv->handle_raw_func) {
srv->handle_raw_func(client->buffer);
} else {
do {
namevalue = client->buffer;
name = strtok_r(namevalue, ",=", &ptr);
value = strtok_r(NULL, ",=", &ptr);
if (name && value)
srv->handle_func(name, value);
} while (name && value);
}
}
int command_server_fdset_handle(struct command_server *srv, fd_set *set)
{
struct command_client *client, **clientp, **nextp;
for (clientp = &srv->clients; *clientp; clientp = nextp) {
nextp = &(*clientp)->next;
if (FD_ISSET((*clientp)->fd, set)) {
/* received something */
int ret;
do {
ret = read((*clientp)->fd, (*clientp)->buffer+(*clientp)->buflen, 1);
if ((ret < 0 && errno != EAGAIN) || ret == 0) {
/* Fatal error on socket to client.
Clean up client
*/
client = *clientp;
close(client->fd);
*clientp = (*clientp)->next;
free(client);
nextp = clientp;
} else if (ret == 1) {
/* Data from client received.
*/
(*clientp)->buflen += 1;
if ((*clientp)->buffer[(*clientp)->buflen-1] == '\n' ||
(*clientp)->buffer[(*clientp)->buflen-1] == '\r') {
if ((*clientp)->buflen > 1) {
(*clientp)->buffer[(*clientp)->buflen] = 0;
command_server_handle(srv, *clientp);
(*clientp)->buffer[0] = '9';
}
(*clientp)->buflen = 0;
}
if ((*clientp)->buflen >= 200)
(*clientp)->buflen = 0;
}
} while (ret > 0);
}
}
if (FD_ISSET(srv->fd, set)) {
/* New client */
int client_fd;
client_fd = tcp_accept(srv->fd);
if (client_fd < 0)
return -1;
ioctl(client_fd, FIONBIO, &(int){ 1 });
client = calloc(1, sizeof(struct command_client));
if (!client) {
close(client_fd);
} else {
client->fd = client_fd;
client->buflen = 0;
client->next = srv->clients;
srv->clients = client;
}
}
return 0;
}
/*
Command server
Copyright Jeroen Vreeken (jeroen@vreeken.net), 2013
Copyright Stichting C.A. Muller Radioastronomiestation, 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
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/>.
*/
#ifndef _INCLUDE_COMMAND_SERVER_
#define _INCLUDE_COMMAND_SERVER_
#include <sys/select.h>
struct command_server;
struct command_server *command_server_create(int port, int local, int backlog);
void command_server_destroy(struct command_server *srv);
int command_server_handler_set(struct command_server *srv,
int (*func)(char *name, char *value));
int command_server_handler_raw_set(struct command_server *srv,
int (*func)(char *command));
int command_server_fdset_add(struct command_server *srv, fd_set *set, int *high);
int command_server_fdset_handle(struct command_server *srv, fd_set *set);
#endif /* _INCLUDE_COMMAND_SERVER_ */
......@@ -22,6 +22,11 @@
#include <unistd.h>
#include <math.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include "dt_model.h"
......@@ -48,40 +53,26 @@ r: refractie
za: scheefstand rail
aa: kompasrichting scheefstand
Gevonden parameters (in graden):
a0: -0.012 +/- 0.012
c1: 0.058 +/- 0.017
c2: 0.015 +/- 0.014
e0: -0.041 +/- 0.003
b: -0.046 +/- 0.004
za: -0.008 +/- 0.010
aa: -2.9 +/- 3.6
De gemiddelde pointing-error was 0.11 graden, en wordt hiermee teruggebracht naar 0.007 graden.
*/
static double a0 = -0.012;
static double c1 = 0.058;
static double c2 = 0.015;
static double e0 = -0.041;
static double b = -0.046;
static double za = -0.008;
static double aa = -2.900;
static double r = 0.0;
static struct dt_model_params dt_model_default;
struct dt_model_params *params = &dt_model_default;
double dt_model_azimuth_delta(double az, double el)
{
double delta_az;
double c1_div_cos_el = c1 / cos(el);
double c1_div_cos_el = params->c1 / cos(el);
if (isnan(c1_div_cos_el))
c1_div_cos_el = 0.0;
delta_az =
a0
+ c1 / cos(el)
- c2 * tan(el)
- za * sin(az - aa) * tan(el);
params->a0
+ params->c1 / cos(el)
- params->c2 * tan(el)
- params->za * sin(az - params->aa) * tan(el);
return delta_az;
}
......@@ -95,16 +86,11 @@ double dt_model_azimuth_delta_rev(double az, double el)
double dt_model_elevation_delta(double az, double el)
{
double delta_el;
double r_div_sin_el = r / sin(el);
if (isnan(r_div_sin_el))
r_div_sin_el = 0.0;
delta_el =
e0
+ b * cos(el)
- r_div_sin_el
- za * cos(az - aa);
params->e0
+ params->b * cos(el)
- params->za * cos(az - params->aa);
return delta_el;
}
......@@ -114,3 +100,62 @@ double dt_model_elevation_delta_rev(double az, double el)
/* No reverse model available, (ab)use the forward model */
return -dt_model_elevation_delta(az, el);
}
int dt_model_init(void)
{
int fd;
fd = shm_open("/dt_model",
O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0)
return -1;
if((ftruncate(fd, sizeof(struct dt_model_params))) == -1){
printf("ftruncate failure\n");
return -1;
}
params = mmap(NULL, sizeof(struct dt_model_params),
PROT_READ,
MAP_SHARED, fd, 0);
return params != NULL;
}
int dt_model_init_server(void)
{
int fd;
fd = shm_open("/dt_model",
O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0)
return -1;
if((ftruncate(fd, sizeof(struct dt_model_params))) == -1){
printf("ftruncate failure\n");
return -1;
}
params = mmap(NULL, sizeof(struct dt_model_params),
PROT_READ | PROT_WRITE,
MAP_SHARED, fd, 0);
return params != NULL;
}
int dt_model_params_set(struct dt_model_params *newparams)
{
memcpy(params, newparams, sizeof(struct dt_model_params));
return 0;
}
int dt_model_params_get(struct dt_model_params *getparams)
{
memcpy(getparams, params, sizeof(struct dt_model_params));
}
......@@ -23,7 +23,26 @@
#include <unistd.h>
#include <math.h>
int dt_model_init(void);
double dt_model_azimuth_delta(double az, double el);
double dt_model_azimuth_delta_rev(double az, double el);
double dt_model_elevation_delta(double az, double el);
double dt_model_elevation_delta_rev(double az, double el);
#define DT_MODEL_NAME_SIZE 256
struct dt_model_params {
double a0;
double c1;
double c2;
double e0;
double b;
double za;
double aa;
char name[DT_MODEL_NAME_SIZE];
};
int dt_model_init_server(void);
int dt_model_params_set(struct dt_model_params *params);
int dt_model_params_get(struct dt_model_params *params);
/*
status server
Copyright Jeroen Vreeken (jeroen@vreeken.net), 2013
Copyright Stichting C.A. Muller Radioastronomiestation, 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
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 <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include "tcp_listen.h"
struct status_client {
struct status_client *next;
int fd;
};
struct status_server {
int fd;
struct status_client *clients;
};
struct status_server *status_server_create(int port, int local, int backlog)
{
struct status_server *srv;
srv = calloc(1, sizeof(struct status_server));
if (!srv)
goto err_alloc;
srv->fd = tcp_listen(port, local, backlog);
if (srv->fd < 0)
goto err_listen;
ioctl(srv->fd, FIONBIO, &(int){ 1 });
return srv;
err_listen:
close(srv->fd);
err_alloc:
return NULL;
}
void status_server_destroy(struct status_server *srv)
{
struct status_client *client;
if (srv) {
while (srv->clients) {
client = srv->clients;
srv->clients = client->next;
close(client->fd);
free(client);
}
close(srv->fd);
free(srv);
}
}
int status_server_fdset_add(struct status_server *srv, fd_set *set, int *high)
{
FD_SET(srv->fd, set);
if (srv->fd >= *high)
*high = srv->fd + 1;
return 0;
}
int status_server_fdset_handle(struct status_server *srv, fd_set *set)
{
struct status_client *client;
if (FD_ISSET(srv->fd, set)) {
/* New client */
int client_fd;
client_fd = tcp_accept(srv->fd);
if (client_fd < 0)
return -1;
ioctl(client_fd, FIONBIO, &(int){ 1 });
client = calloc(1, sizeof(struct status_client));
if (!client) {
close(client_fd);
} else {
client->fd = client_fd;
client->next = srv->clients;
srv->clients = client;
}
}
return 0;
}
int status_server_send(struct status_server *srv, char *status)
{
struct status_client **clientp, **nextp;
for (clientp = &srv->clients; *clientp; clientp = nextp){
int ret;
ret = write((*clientp)->fd, status, strlen(status));
if (ret <= 0) {
struct status_client *tmp;
tmp = *clientp;
close(tmp->fd);
*clientp = (*clientp)->next;
free(tmp);
nextp = clientp;