Commit ab63b0f0 authored by Alessandro Rubini's avatar Alessandro Rubini

on-swtich-tests/wr_mon: cleanup (no functional change)

parent fa59201e
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <termios.h> #include <termios.h>
...@@ -10,80 +11,77 @@ ...@@ -10,80 +11,77 @@
struct termios oldkey, newkey; struct termios oldkey, newkey;
int term_restore() void term_restore(void)
{ {
tcsetattr(STDIN_FILENO,TCSANOW,&oldkey); tcsetattr(STDIN_FILENO,TCSANOW,&oldkey);
} }
void term_init() void term_init(void)
{ {
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK);
tcgetattr(STDIN_FILENO,&oldkey); tcgetattr(STDIN_FILENO,&oldkey);
memcpy(&newkey, &oldkey, sizeof(struct termios)); memcpy(&newkey, &oldkey, sizeof(struct termios));
newkey.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD; newkey.c_cflag = B9600 | CRTSCTS | CS8 | CLOCAL | CREAD;
newkey.c_iflag = IGNPAR; newkey.c_iflag = IGNPAR;
newkey.c_oflag = OPOST |ONLCR; newkey.c_oflag = OPOST |ONLCR;
newkey.c_lflag = 0; newkey.c_lflag = 0;
newkey.c_cc[VMIN]=1; newkey.c_cc[VMIN]=1;
newkey.c_cc[VTIME]=0; newkey.c_cc[VTIME]=0;
tcflush(STDIN_FILENO, TCIFLUSH); tcflush(STDIN_FILENO, TCIFLUSH);
tcsetattr(STDIN_FILENO,TCSANOW,&newkey); tcsetattr(STDIN_FILENO,TCSANOW,&newkey);
atexit(term_restore); atexit(term_restore);
} }
int term_poll(void)
int term_poll()
{ {
struct pollfd pfd; struct pollfd pfd;
pfd.fd = STDIN_FILENO; pfd.fd = STDIN_FILENO;
pfd.events = POLLIN | POLLPRI; pfd.events = POLLIN | POLLPRI;
if(poll(&pfd,1,0)>0)return 1; if(poll(&pfd,1,0)>0)return 1;
return 0; return 0;
} }
int term_get() int term_get(void)
{ {
unsigned char c; unsigned char c;
int q; int q;
if(read(STDIN_FILENO, &c, 1 ) == 1) if(read(STDIN_FILENO, &c, 1 ) == 1)
{ {
q=c; q=c;
} else q=-1; } else q=-1;
return q;
return q;
} }
void term_cprintf(int color, const char *fmt, ...) void term_cprintf(int color, const char *fmt, ...)
{ {
va_list ap; va_list ap;
printf("\033[0%d;3%dm",color & C_DIM ? 2:1, color&0x7f); printf("\033[0%d;3%dm",color & C_DIM ? 2:1, color&0x7f);
// printf("\033[01;%dm",color); // printf("\033[01;%dm",color);
va_start(ap, fmt); va_start(ap, fmt);
vprintf(fmt, ap); vprintf(fmt, ap);
va_end(ap); va_end(ap);
fflush(stdout); fflush(stdout);
} }
void term_pcprintf(int row, int col, int color, const char *fmt, ...) void term_pcprintf(int row, int col, int color, const char *fmt, ...)
{ {
va_list ap; va_list ap;
printf("\033[%d;%df", row, col); printf("\033[%d;%df", row, col);
printf("\033[0%d;3%dm",color & C_DIM ? 2:1, color&0x7f); printf("\033[0%d;3%dm",color & C_DIM ? 2:1, color&0x7f);
va_start(ap, fmt); va_start(ap, fmt);
vprintf(fmt, ap); vprintf(fmt, ap);
va_end(ap); va_end(ap);
fflush(stdout); fflush(stdout);
} }
void term_clear() void term_clear(void)
{ {
printf("\033[2J\033[1;1H"); printf("\033[2J\033[1;1H");
} }
...@@ -10,10 +10,10 @@ ...@@ -10,10 +10,10 @@
#define C_GREEN 2 #define C_GREEN 2
#define C_BLUE 4 #define C_BLUE 4
int term_restore(); void term_restore(void);
void term_init(); void term_init(void);
int term_poll(); int term_poll(void);
int term_get(); int term_get(void);
void term_cprintf(int color, const char *fmt, ...); void term_cprintf(int color, const char *fmt, ...);
void term_pcprintf(int row, int col, int color, const char *fmt, ...); void term_pcprintf(int row, int col, int color, const char *fmt, ...);
void term_clear(); void term_clear(void);
...@@ -14,16 +14,16 @@ wripc_handle_t h_ptp; ...@@ -14,16 +14,16 @@ wripc_handle_t h_ptp;
void init() void init()
{ {
halexp_client_init(); halexp_client_init();
if( (h_ptp = wripc_connect("ptpd")) < 0) if( (h_ptp = wripc_connect("ptpd")) < 0)
{ {
fprintf(stderr,"Can't establish WRIPC connection to the PTP daemon!\n"); fprintf(stderr,"Can't establish WRIPC connection "
"to the PTP daemon!\n");
exit(-1); exit(-1);
} }
term_init(); term_init();
halexp_query_ports(&port_list); halexp_query_ports(&port_list);
} }
...@@ -36,59 +36,98 @@ void show_ports() ...@@ -36,59 +36,98 @@ void show_ports()
{ {
hexp_port_state_t state; hexp_port_state_t state;
halexp_get_port_state(&state, port_list.port_names[i]); halexp_get_port_state(&state, port_list.port_names[i]);
term_pcprintf(5+i, 1, C_WHITE, "%s: ", port_list.port_names[i]); term_pcprintf(5+i, 1, C_WHITE, "%s: ", port_list.port_names[i]);
if(state.up) term_cprintf(C_GREEN, "Link up "); else term_cprintf(C_RED, "Link down "); if(state.up)
term_cprintf(C_GREEN, "Link up ");
else
term_cprintf(C_RED, "Link down ");
term_cprintf(C_GREY, "mode: "); term_cprintf(C_GREY, "mode: ");
switch(state.mode) switch(state.mode)
{ {
case HEXP_PORT_MODE_WR_MASTER:term_cprintf(C_WHITE, "WR Master ");break; case HEXP_PORT_MODE_WR_MASTER:
case HEXP_PORT_MODE_WR_SLAVE:term_cprintf(C_WHITE, "WR Slave ");break; term_cprintf(C_WHITE, "WR Master ");
break;
case HEXP_PORT_MODE_WR_SLAVE:
term_cprintf(C_WHITE, "WR Slave ");
break;
} }
if(state.is_locked) term_cprintf(C_GREEN, "Locked "); else term_cprintf(C_RED, "NoLock "); if(state.is_locked)
if(state.rx_calibrated && state.tx_calibrated) term_cprintf(C_GREEN, "Calibrated "); else term_cprintf(C_RED, "Uncalibrated "); term_cprintf(C_GREEN, "Locked ");
} else
term_cprintf(C_RED, "NoLock ");
if(state.rx_calibrated && state.tx_calibrated)
term_cprintf(C_GREEN, "Calibrated ");
else
term_cprintf(C_RED, "Uncalibrated ");
}
} }
void show_servo() void show_servo()
{ {
ptpdexp_sync_state_t ss; ptpdexp_sync_state_t ss;
wripc_call(h_ptp, "ptpdexp_get_sync_state", &ss, 0); wripc_call(h_ptp, "ptpdexp_get_sync_state", &ss, 0);
term_cprintf(C_BLUE, "\n\nSynchronization status:\n\n"); term_cprintf(C_BLUE, "\n\nSynchronization status:\n\n");
if(!ss.valid) if(!ss.valid)
{ {
term_cprintf(C_RED, "Master mode or sync info not valid\n\n"); term_cprintf(C_RED, "Master mode or sync info not valid\n\n");
return; return;
} }
term_cprintf(C_GREY, "Servo state: "); term_cprintf(C_WHITE, "%s\n", ss.slave_servo_state); term_cprintf(C_GREY, "Servo state: ");
term_cprintf(C_GREY, "Phase tracking: "); if(ss.tracking_enabled) term_cprintf(C_GREEN, "ON\n"); else term_cprintf(C_RED,"OFF\n"); term_cprintf(C_WHITE, "%s\n", ss.slave_servo_state);
term_cprintf(C_GREY, "Synchronization source: "); term_cprintf(C_WHITE, "%s\n", ss.sync_source);
term_cprintf(C_GREY, "Phase tracking: ");
if(ss.tracking_enabled)
term_cprintf(C_GREEN, "ON\n");
else
term_cprintf(C_RED,"OFF\n");
term_cprintf(C_GREY, "Synchronization source: ");
term_cprintf(C_WHITE, "%s\n", ss.sync_source);
term_cprintf(C_BLUE, "\nTiming parameters:\n\n"); term_cprintf(C_BLUE, "\nTiming parameters:\n\n");
term_cprintf(C_GREY, "Round-trip time (mu): "); term_cprintf(C_WHITE, "%.2f nsec\n", ss.mu/1000.0); term_cprintf(C_GREY, "Round-trip time (mu): ");
term_cprintf(C_GREY, "Master-slave delay: "); term_cprintf(C_WHITE, "%.2f nsec\n", ss.delay_ms/1000.0); term_cprintf(C_WHITE, "%.2f nsec\n", ss.mu/1000.0);
term_cprintf(C_GREY, "Link length: "); term_cprintf(C_WHITE, "%.0f meters \n", ss.delay_ms/1e12 * 300e6 / 1.55);
term_cprintf(C_GREY, "Master PHY delays: "); term_cprintf(C_WHITE, "TX: %.2f nsec, RX: %.2f nsec\n", ss.delta_tx_m/1000.0, ss.delta_rx_m/1000.0); term_cprintf(C_GREY, "Master-slave delay: ");
term_cprintf(C_GREY, "Slave PHY delays: "); term_cprintf(C_WHITE, "TX: %.2f nsec, RX: %.2f nsec\n", ss.delta_tx_s/1000.0, ss.delta_rx_s/1000.0); term_cprintf(C_WHITE, "%.2f nsec\n", ss.delay_ms/1000.0);
term_cprintf(C_GREY, "Total link asymmetry: "); term_cprintf(C_WHITE, "%.2f nsec\n", ss.total_asymmetry/1000.0);
// term_cprintf(C_GREY, "Fiber asymmetry: "); term_cprintf(C_WHITE, "%.2f nsec\n", ss.fiber_asymmetry/1000.0); term_cprintf(C_GREY, "Link length: ");
term_cprintf(C_WHITE, "%.0f meters \n",
ss.delay_ms/1e12 * 300e6 / 1.55);
term_cprintf(C_GREY, "Master PHY delays: ");
term_cprintf(C_WHITE, "TX: %.2f nsec, RX: %.2f nsec\n",
ss.delta_tx_m/1000.0, ss.delta_rx_m/1000.0);
term_cprintf(C_GREY, "Clock offset: "); term_cprintf(C_WHITE, "%.2f nsec\n", ss.cur_offset/1000.0); term_cprintf(C_GREY, "Slave PHY delays: ");
term_cprintf(C_GREY, "Phase setpoint: "); term_cprintf(C_WHITE, "%.2f nsec\n", ss.cur_setpoint/1000.0); term_cprintf(C_WHITE, "TX: %.2f nsec, RX: %.2f nsec\n",
term_cprintf(C_GREY, "Skew: "); term_cprintf(C_WHITE, "%.2f nsec\n", ss.cur_skew/1000.0); ss.delta_tx_s/1000.0, ss.delta_rx_s/1000.0);
term_cprintf(C_GREY, "Total link asymmetry: ");
term_cprintf(C_WHITE, "%.2f nsec\n", ss.total_asymmetry/1000.0);
if (0) {
term_cprintf(C_GREY, "Fiber asymmetry: ");
term_cprintf(C_WHITE, "%.2f nsec\n", ss.fiber_asymmetry/1000.0);
}
term_cprintf(C_GREY, "Clock offset: ");
term_cprintf(C_WHITE, "%.2f nsec\n", ss.cur_offset/1000.0);
term_cprintf(C_GREY, "Phase setpoint: ");
term_cprintf(C_WHITE, "%.2f nsec\n", ss.cur_setpoint/1000.0);
term_cprintf(C_GREY, "Skew: ");
term_cprintf(C_WHITE, "%.2f nsec\n", ss.cur_skew/1000.0);
} }
void show_menu() void show_menu()
...@@ -102,40 +141,42 @@ void show_screen() ...@@ -102,40 +141,42 @@ void show_screen()
{ {
term_clear(); term_clear();
term_pcprintf(1, 1, C_BLUE, "WR Switch Sync Monitor v 0.1"); term_pcprintf(1, 1, C_BLUE, "WR Switch Sync Monitor v 0.1");
show_ports(); show_ports();
show_servo(); show_servo();
show_menu(); show_menu();
// handle_toggle(); // handle_toggle();
} }
main() main()
{ {
init(); init();
for(;;) for(;;)
{ {
if(term_poll()) if(term_poll())
{ {
int c = term_get(); int c = term_get();
if(c=='q') break; if(c=='q')
else if(c=='t') { break;
int rval;
if(c=='t') {
int rval;
track_onoff = 1-track_onoff; track_onoff = 1-track_onoff;
wripc_call(h_ptp, "ptpdexp_cmd", &rval, 2, A_INT32(PTPDEXP_COMMAND_TRACKING), A_INT32(track_onoff)); wripc_call(h_ptp, "ptpdexp_cmd", &rval, 2,
A_INT32(PTPDEXP_COMMAND_TRACKING),
A_INT32(track_onoff));
} }
} }
show_screen(); show_screen();
usleep(500000); usleep(500000);
} }
term_cprintf(C_GREY,"bye...\n\n"); term_cprintf(C_GREY,"bye...\n\n");
term_clear(); term_clear();
term_restore(); term_restore();
} }
\ No newline at end of file
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