Compare commits
5 commits
1e247d1c89
...
b8cbb8a7a0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8cbb8a7a0 | ||
|
|
5c6aae5fdb | ||
|
|
15a8c657f2 | ||
|
|
ce9718a550 | ||
|
|
7f728c698f |
1 changed files with 57 additions and 40 deletions
99
irena.c
99
irena.c
|
|
@ -26,26 +26,15 @@ const char version[] = "$Id$";
|
||||||
#include <sys/un.h>
|
#include <sys/un.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
|
||||||
#define DEBUG
|
|
||||||
|
|
||||||
FILE *mout;
|
FILE *mout;
|
||||||
|
|
||||||
int verbosity = 1;
|
int maintainance = 0;
|
||||||
int next_verbosity = 1;
|
|
||||||
static inline void silent(int v) {
|
int verbosity[2] = {1,1};
|
||||||
if (verbosity < v)
|
static inline void silent(int v) { if (verbosity[0] < v) verbosity[1] = 0; }
|
||||||
next_verbosity = 0;
|
static inline void unsilent() { verbosity[1] = verbosity[0]; }
|
||||||
}
|
static inline void loud(int v) { if (v > verbosity[0]) verbosity[1] = v; }
|
||||||
static inline void loud(int v) {
|
static inline int verbose(int v) { v = verbosity[1] >= v; unsilent(); return v; }
|
||||||
if (v > verbosity)
|
|
||||||
next_verbosity = v;
|
|
||||||
}
|
|
||||||
static inline int verbose(int v)
|
|
||||||
{
|
|
||||||
v = next_verbosity >= v;
|
|
||||||
next_verbosity = verbosity;
|
|
||||||
return v;
|
|
||||||
}
|
|
||||||
|
|
||||||
// replacement for perror()
|
// replacement for perror()
|
||||||
void merror(const char *s)
|
void merror(const char *s)
|
||||||
|
|
@ -55,8 +44,9 @@ void merror(const char *s)
|
||||||
fprintf(mout, "%s: %s\n", s, strerror(e));
|
fprintf(mout, "%s: %s\n", s, strerror(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define DEBUG 3
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
# define debug(fmt, ...) fprintf(mout, fmt, __VA_ARGS__)
|
# define debug(fmt, ...) if (verbose(DEBUG)) fprintf(mout, fmt, __VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
# define debug(fmt, ...)
|
# define debug(fmt, ...)
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -1961,8 +1951,11 @@ int send_irena_command(const char *c)
|
||||||
}
|
}
|
||||||
no_uart=0;
|
no_uart=0;
|
||||||
time_t t = time(0);
|
time_t t = time(0);
|
||||||
if (last_irena_command_sent+1 >= t)
|
if (last_irena_command_sent+1 >= t) {
|
||||||
|
if (verbose(3))
|
||||||
|
fprintf(mout, "IRENA CMD postponed: %s\n", c);
|
||||||
return -2;
|
return -2;
|
||||||
|
}
|
||||||
size_t sl = strlen(c);
|
size_t sl = strlen(c);
|
||||||
int n = write(i_buf.fd, c, sl);
|
int n = write(i_buf.fd, c, sl);
|
||||||
last_irena_command_sent = t;
|
last_irena_command_sent = t;
|
||||||
|
|
@ -2003,6 +1996,7 @@ void send_fct(time_t t)
|
||||||
fct_ack = -1;
|
fct_ack = -1;
|
||||||
fct_status = fct_size;
|
fct_status = fct_size;
|
||||||
}
|
}
|
||||||
|
unsilent();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
snprintf(fct_cmd, sizeof(fct_cmd), "u/f=%d", fct_size);
|
snprintf(fct_cmd, sizeof(fct_cmd), "u/f=%d", fct_size);
|
||||||
|
|
@ -2020,6 +2014,7 @@ void send_fct(time_t t)
|
||||||
fct_got_data = 0;
|
fct_got_data = 0;
|
||||||
fct_sent++;
|
fct_sent++;
|
||||||
}
|
}
|
||||||
|
unsilent();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2100,7 +2095,7 @@ int send_file(const char *fn)
|
||||||
for (int i=0; i < n; ) {
|
for (int i=0; i < n; ) {
|
||||||
char cmd[64];
|
char cmd[64];
|
||||||
int retry = 0;
|
int retry = 0;
|
||||||
int c = snprintf(cmd, 64, "v f[%d]=:", i>>2);
|
int c = snprintf(cmd, 64, "v f[%d]=:=", i>>2);
|
||||||
while (c<58) {
|
while (c<58) {
|
||||||
int nn = -i & 3;
|
int nn = -i & 3;
|
||||||
if (c < 54 || nn > n-i)
|
if (c < 54 || nn > n-i)
|
||||||
|
|
@ -2227,6 +2222,20 @@ int close_cmd_socket()
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void print_asocket(FILE *mout)
|
||||||
|
{
|
||||||
|
char *mode = cmd_socket_flags & SOCK_SHALL_BE_MOUT ? "interactive " : "";
|
||||||
|
if (acmd_socket_addr.g.sa_family == AF_INET) {
|
||||||
|
unsigned int ip = ntohl(acmd_socket_addr.i.sin_addr.s_addr);
|
||||||
|
fprintf(mout, "%ssocket connection from %d.%d.%d.%d:%d\n",
|
||||||
|
mode,
|
||||||
|
(ip>>24) & 0xff, (ip>>16) & 0xff, (ip>>8) & 0xff, ip & 0xff,
|
||||||
|
ntohs(acmd_socket_addr.i.sin_port) );
|
||||||
|
}
|
||||||
|
else if (acmd_socket_addr.g.sa_family == AF_UNIX)
|
||||||
|
fprintf(mout, "%sunix socket connection\n", mode);
|
||||||
|
}
|
||||||
|
|
||||||
int open_cmd_socket(int na, char **av)
|
int open_cmd_socket(int na, char **av)
|
||||||
{
|
{
|
||||||
if (!strcmp(av[na-1], "close")) {
|
if (!strcmp(av[na-1], "close")) {
|
||||||
|
|
@ -2263,6 +2272,7 @@ int open_cmd_socket(int na, char **av)
|
||||||
else if (na==1) {
|
else if (na==1) {
|
||||||
is_open:
|
is_open:
|
||||||
fprintf(mout, "command socket is %s«%s»\n", mode, cmd_socket_name());
|
fprintf(mout, "command socket is %s«%s»\n", mode, cmd_socket_name());
|
||||||
|
print_asocket(mout);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -2282,6 +2292,7 @@ int open_cmd_socket(int na, char **av)
|
||||||
setsockopt(cmd_socket_fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
|
setsockopt(cmd_socket_fd, SOL_SOCKET, SO_REUSEADDR, &(int){1}, sizeof(int));
|
||||||
|
|
||||||
if (bind(cmd_socket_fd, &cmd_socket_addr.g, sizeof(cmd_socket_addr.u))<0) {
|
if (bind(cmd_socket_fd, &cmd_socket_addr.g, sizeof(cmd_socket_addr.u))<0) {
|
||||||
|
|
||||||
merror("bind");
|
merror("bind");
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
|
|
@ -2327,10 +2338,7 @@ int accept_cmd_socket(struct line_buffer *b, int fd)
|
||||||
buffer_reset(b);
|
buffer_reset(b);
|
||||||
b->fd = sfd;
|
b->fd = sfd;
|
||||||
|
|
||||||
char *mode = "";
|
|
||||||
|
|
||||||
if (cmd_socket_flags & SOCK_SHALL_BE_MOUT) {
|
if (cmd_socket_flags & SOCK_SHALL_BE_MOUT) {
|
||||||
mode = "interactive ";
|
|
||||||
int fd = dup(sfd);
|
int fd = dup(sfd);
|
||||||
if (fd<0) {
|
if (fd<0) {
|
||||||
merror("dup socket");
|
merror("dup socket");
|
||||||
|
|
@ -2347,15 +2355,9 @@ int accept_cmd_socket(struct line_buffer *b, int fd)
|
||||||
fprintf(mout, "irena %s\n", version);
|
fprintf(mout, "irena %s\n", version);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (acmd_socket_addr.g.sa_family == AF_INET) {
|
if (verbose(0))
|
||||||
unsigned int ip = ntohl(acmd_socket_addr.i.sin_addr.s_addr);
|
print_asocket(stderr);
|
||||||
fprintf(stderr, "accepting %ssocket connection from %d.%d.%d.%d:%d\n",
|
|
||||||
mode,
|
|
||||||
(ip>>24) & 0xff, (ip>>16) & 0xff, (ip>>8) & 0xff, ip & 0xff,
|
|
||||||
ntohs(acmd_socket_addr.i.sin_port) );
|
|
||||||
}
|
|
||||||
else
|
|
||||||
fprintf(stderr, "accepting %sunix socket connection\n", mode);
|
|
||||||
return sfd;
|
return sfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2368,12 +2370,17 @@ int close_acmd_socket(struct line_buffer *b)
|
||||||
if (r)
|
if (r)
|
||||||
merror("fclose socket");
|
merror("fclose socket");
|
||||||
}
|
}
|
||||||
if (b->fd >= 0) {
|
if (verbose(0)) {
|
||||||
close(b->fd);
|
print_asocket(stderr);
|
||||||
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, b->fd, 0);
|
|
||||||
fprintf(stderr, "closing socket connection\n");
|
fprintf(stderr, "closing socket connection\n");
|
||||||
}
|
}
|
||||||
|
if (b->fd >= 0) {
|
||||||
|
close(b->fd);
|
||||||
|
if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, b->fd, 0))
|
||||||
|
merror("EPOLL_CTL_DEL acmd socket");
|
||||||
|
}
|
||||||
b->fd = -1;
|
b->fd = -1;
|
||||||
|
cmd_socket_addr.g.sa_family = AF_UNSPEC;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2464,7 +2471,8 @@ struct numbers {
|
||||||
{.name="time_resolution", .doc="seconds", .i=&time_resolution, .flags=NU_INT},
|
{.name="time_resolution", .doc="seconds", .i=&time_resolution, .flags=NU_INT},
|
||||||
{.name="highgain", .doc="ch#", .i=high_gain_ch, .flags=NU_INT, .size=18},
|
{.name="highgain", .doc="ch#", .i=high_gain_ch, .flags=NU_INT, .size=18},
|
||||||
{.name="lowgain", .doc="ch#", .i=low_gain_ch, .flags=NU_INT, .size=18},
|
{.name="lowgain", .doc="ch#", .i=low_gain_ch, .flags=NU_INT, .size=18},
|
||||||
{.name="verbosity", .doc="int", .i=&verbosity, .flags=NU_INT},
|
{.name="verbosity", .doc="int", .i=verbosity, .flags=NU_INT, .size=2},
|
||||||
|
{.name="maintainance", .doc="int", .i=&maintainance, .flags=NU_INT},
|
||||||
{.name=0}
|
{.name=0}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -3003,7 +3011,7 @@ int main(int argc, char **argv)
|
||||||
close_acmd_socket(&s_buf);
|
close_acmd_socket(&s_buf);
|
||||||
while (l) {
|
while (l) {
|
||||||
process_cmd(l);
|
process_cmd(l);
|
||||||
l = get_line(&c_buf, 0);
|
l = get_line(&s_buf, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (eev[i].data.fd) {
|
else if (eev[i].data.fd) {
|
||||||
|
|
@ -3013,14 +3021,23 @@ int main(int argc, char **argv)
|
||||||
char *l = get_line(&c_buf, eev[i].events);
|
char *l = get_line(&c_buf, eev[i].events);
|
||||||
while (l) {
|
while (l) {
|
||||||
process_cmd(l);
|
process_cmd(l);
|
||||||
if (prompt)
|
if (prompt) {
|
||||||
fprintf(stderr, prompt);
|
if (maintainance)
|
||||||
|
fprintf(stderr, "M:%s", prompt);
|
||||||
|
else
|
||||||
|
fprintf(stderr, prompt);
|
||||||
|
}
|
||||||
l = get_line(&c_buf, 0);
|
l = get_line(&c_buf, 0);
|
||||||
}
|
}
|
||||||
if (eev[i].events & EPOLLHUP)
|
if (eev[i].events & EPOLLHUP)
|
||||||
epoll_ctl(epoll_fd, EPOLL_CTL_DEL, 0, 0);
|
if (epoll_ctl(epoll_fd, EPOLL_CTL_DEL, 0, 0))
|
||||||
|
merror("EPOLL_CTL_DEL <stdin>");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (maintainance)
|
||||||
|
continue;
|
||||||
|
|
||||||
int tick = t/time_resolution;
|
int tick = t/time_resolution;
|
||||||
if (tick != last_tick) {
|
if (tick != last_tick) {
|
||||||
sync_clocks(t);
|
sync_clocks(t);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue