Commit 7f79ec8f by zacharyc

Ran indent on code to fix inconsistent style.


git-svn-id: https://bucket.mit.edu/svn/nilm/acquisition/ethstream@7298 ddd99763-3ecb-0310-9145-efcb8ce7c51f
parent 1dd09ea5
...@@ -3,43 +3,70 @@ ...@@ -3,43 +3,70 @@
#include "compat.h" #include "compat.h"
#include <windows.h> #include <windows.h>
unsigned int sleep(unsigned int seconds) unsigned int
sleep (unsigned int seconds)
{ {
Sleep(seconds * 1000); Sleep (seconds * 1000);
return 0; return 0;
} }
static struct { static struct
{
int num; int num;
char *msg; char *msg;
} win32_error[] = { } win32_error[] =
{
/* Errors that we might vaguely expect to see */ /* Errors that we might vaguely expect to see */
{ WSAEINTR, "Winsock: Interrupted system call" }, {
{ WSAEBADF, "Winsock: Bad file number" }, WSAEINTR, "Winsock: Interrupted system call"},
{ WSAEFAULT, "Winsock: Bad address" }, {
{ WSAEINVAL, "Winsock: Invalid argument" }, WSAEBADF, "Winsock: Bad file number"},
{ WSAEMFILE, "Winsock: Too many open files" }, {
{ WSAEWOULDBLOCK, "Winsock: Operation would block" }, WSAEFAULT, "Winsock: Bad address"},
{ WSAEINPROGRESS, "Winsock: Operation now in progress" }, {
{ WSAEALREADY, "Winsock: Operation already in progress" }, WSAEINVAL, "Winsock: Invalid argument"},
{ WSAENOTSOCK, "Winsock: Socket operation on nonsocket" }, {
{ WSAEADDRINUSE, "Winsock: Address already in use" }, WSAEMFILE, "Winsock: Too many open files"},
{ WSAEADDRNOTAVAIL, "Winsock: Cannot assign requested address" }, {
{ WSAENETDOWN, "Winsock: Network is down" }, WSAEWOULDBLOCK, "Winsock: Operation would block"},
{ WSAENETUNREACH, "Winsock: Network is unreachable" }, {
{ WSAENETRESET, "Winsock: Network dropped connection on reset" }, WSAEINPROGRESS, "Winsock: Operation now in progress"},
{ WSAECONNABORTED, "Winsock: Software caused connection abort" }, {
{ WSAECONNRESET, "Winsock: Connection reset by peer" }, WSAEALREADY, "Winsock: Operation already in progress"},
{ WSAETIMEDOUT, "Winsock: Connection timed out" }, {
{ WSAECONNREFUSED, "Winsock: Connection refused" }, WSAENOTSOCK, "Winsock: Socket operation on nonsocket"},
{ WSAEHOSTDOWN, "Winsock: Host is down" }, {
{ WSAEHOSTUNREACH, "Winsock: No route to host" }, WSAEADDRINUSE, "Winsock: Address already in use"},
{ WSAVERNOTSUPPORTED, "Winsock: Unsupported Winsock version" }, {
{ ETIMEDOUT, "Connection timed out" }, WSAEADDRNOTAVAIL, "Winsock: Cannot assign requested address"},
{ ENOTCONN, "Not connected" }, {
{ -1, NULL }, WSAENETDOWN, "Winsock: Network is down"},
}; {
char *compat_strerror(int errnum) WSAENETUNREACH, "Winsock: Network is unreachable"},
{
WSAENETRESET, "Winsock: Network dropped connection on reset"},
{
WSAECONNABORTED, "Winsock: Software caused connection abort"},
{
WSAECONNRESET, "Winsock: Connection reset by peer"},
{
WSAETIMEDOUT, "Winsock: Connection timed out"},
{
WSAECONNREFUSED, "Winsock: Connection refused"},
{
WSAEHOSTDOWN, "Winsock: Host is down"},
{
WSAEHOSTUNREACH, "Winsock: No route to host"},
{
WSAVERNOTSUPPORTED, "Winsock: Unsupported Winsock version"},
{
ETIMEDOUT, "Connection timed out"},
{
ENOTCONN, "Not connected"},
{
-1, NULL},};
char *
compat_strerror (int errnum)
{ {
int i; int i;
static char buf[128]; static char buf[128];
...@@ -47,11 +74,12 @@ char *compat_strerror(int errnum) ...@@ -47,11 +74,12 @@ char *compat_strerror(int errnum)
for (i = 0; win32_error[i].num != -1; i++) for (i = 0; win32_error[i].num != -1; i++)
if (errnum == win32_error[i].num) if (errnum == win32_error[i].num)
return win32_error[i].msg; return win32_error[i].msg;
if (errnum >= 10000) { if (errnum >= 10000)
sprintf(buf, "Winsock: unknown error %d\n", errnum); {
sprintf (buf, "Winsock: unknown error %d\n", errnum);
return buf; return buf;
} }
return strerror(errnum); return strerror (errnum);
} }
#ifdef __WIN32__ #ifdef __WIN32__
...@@ -80,4 +108,3 @@ char *compat_strerror(int errnum) ...@@ -80,4 +108,3 @@ char *compat_strerror(int errnum)
} }
*/ */
#endif #endif
...@@ -2,8 +2,8 @@ ...@@ -2,8 +2,8 @@
#define COMPAT_H #define COMPAT_H
#ifdef __WIN32__ #ifdef __WIN32__
unsigned int sleep(unsigned int seconds); unsigned int sleep (unsigned int seconds);
char *compat_strerror(int errnum); char *compat_strerror (int errnum);
//const char *inet_ntop(int af, void *src, const char *dst, socklen_t cnt); //const char *inet_ntop(int af, void *src, const char *dst, socklen_t cnt);
#define INET_ADDRSTRLEN 16 #define INET_ADDRSTRLEN 16
#define ETIMEDOUT 110 #define ETIMEDOUT 110
......
...@@ -4,15 +4,15 @@ ...@@ -4,15 +4,15 @@
int verb_count = 0; int verb_count = 0;
int func_fprintf(const char *func, FILE *stream, const char *format, ...) int
func_fprintf (const char *func, FILE * stream, const char *format, ...)
{ {
va_list ap; va_list ap;
int ret; int ret;
fprintf(stream, "%s: ", func); fprintf (stream, "%s: ", func);
va_start(ap, format); va_start (ap, format);
ret = vfprintf(stream, format, ap); ret = vfprintf (stream, format, ap);
va_end(ap); va_end (ap);
return ret; return ret;
} }
...@@ -14,7 +14,7 @@ extern int verb_count; ...@@ -14,7 +14,7 @@ extern int verb_count;
#include <stdio.h> #include <stdio.h>
int func_fprintf(const char *func, FILE *stream, const char *format, int func_fprintf (const char *func, FILE * stream, const char *format,
...) __attribute__ ((format (printf, 3, 4))); ...) __attribute__ ((format (printf, 3, 4)));
#define debug(x...) ({ \ #define debug(x...) ({ \
......
...@@ -36,56 +36,63 @@ ...@@ -36,56 +36,63 @@
#define UE9_DATA_PORT 52361 #define UE9_DATA_PORT 52361
struct callbackInfo { struct callbackInfo
{
struct ue9Calibration calib; struct ue9Calibration calib;
int convert; int convert;
int maxlines; int maxlines;
}; };
struct options opt[] = { struct options opt[] = {
{ 'a', "address", "string", "host/address of UE9 (192.168.1.209)" }, {'a', "address", "string", "host/address of UE9 (192.168.1.209)"},
{ 'n', "numchannels", "n", "sample the first N ADC channels (2)" }, {'n', "numchannels", "n", "sample the first N ADC channels (2)"},
{ 'N', "nerdjack", NULL, "Use NerdJack device instead" }, {'N', "nerdjack", NULL, "Use NerdJack device instead"},
{ 'd', "detect", NULL, "Detect NerdJack IP address" }, {'d', "detect", NULL, "Detect NerdJack IP address"},
{ 'p', "precision", "0-3", "Set precision on NerdJack (0 - max range, 1 - max precision)"}, {'p', "precision", "0-3",
{ 'C', "channels", "a,b,c", "sample channels a, b, and c" }, "Set precision on NerdJack (0 - max range, 1 - max precision)"},
{ 'r', "rate", "hz", "sample each channel at this rate (8000.0)" }, {'C', "channels", "a,b,c", "sample channels a, b, and c"},
{ 'o', "oneshot", NULL, "don't retry in case of errors" }, {'r', "rate", "hz", "sample each channel at this rate (8000.0)"},
{ 'f', "forceretry", NULL, "retry no matter what happens" }, {'o', "oneshot", NULL, "don't retry in case of errors"},
{ 'c', "convert", NULL, "convert output to volts" }, {'f', "forceretry", NULL, "retry no matter what happens"},
{ 'H', "converthex", NULL, "convert output to hex" }, {'c', "convert", NULL, "convert output to volts"},
{ 'm', "showmem", NULL, "output memory stats with data (NJ only)" }, {'H', "converthex", NULL, "convert output to hex"},
{ 'l', "lines", "num", "if set, output this many lines and quit" }, {'m', "showmem", NULL, "output memory stats with data (NJ only)"},
{ 'h', "help", NULL, "this help" }, {'l', "lines", "num", "if set, output this many lines and quit"},
{ 'v', "verbose", NULL, "be verbose" }, {'h', "help", NULL, "this help"},
{ 'V', "version", NULL, "show version number and exit" }, {'v', "verbose", NULL, "be verbose"},
{ 0, NULL, NULL, NULL } {'V', "version", NULL, "show version number and exit"},
{0, NULL, NULL, NULL}
}; };
int doStream(const char *address, uint8_t scanconfig, uint16_t scaninterval, int doStream (const char *address, uint8_t scanconfig, uint16_t scaninterval,
int *channel_list, int channel_count, int convert, int maxlines); int *channel_list, int channel_count, int convert,
int nerdDoStream(const char *address, int *channel_list, int channel_count, int precision, int maxlines);
unsigned long period, int convert, int lines, int showmem); int nerdDoStream (const char *address, int *channel_list, int channel_count,
int data_callback(int channels, uint16_t *data, void *context); int precision, unsigned long period, int convert, int lines,
int showmem);
int data_callback (int channels, uint16_t * data, void *context);
int columns_left = 0; int columns_left = 0;
void handle_sig(int sig) void
handle_sig (int sig)
{ {
while (columns_left--) { while (columns_left--)
printf(" 0"); {
printf (" 0");
} }
fflush(stdout); fflush (stdout);
exit(0); exit (0);
} }
int main(int argc, char *argv[]) int
main (int argc, char *argv[])
{ {
int optind; int optind;
char *optarg, *endp; char *optarg, *endp;
char c; char c;
int tmp, i; int tmp, i;
FILE *help = stderr; FILE *help = stderr;
char *address = strdup(DEFAULT_HOST); char *address = strdup (DEFAULT_HOST);
double desired_rate = 8000.0; double desired_rate = 8000.0;
int lines = 0; int lines = 0;
double actual_rate; double actual_rate;
...@@ -107,18 +114,21 @@ int main(int argc, char *argv[]) ...@@ -107,18 +114,21 @@ int main(int argc, char *argv[])
unsigned long period = NERDJACK_CLOCK_RATE / desired_rate; unsigned long period = NERDJACK_CLOCK_RATE / desired_rate;
/* Parse arguments */ /* Parse arguments */
opt_init(&optind); opt_init (&optind);
while ((c = opt_parse(argc, argv, &optind, &optarg, opt)) != 0) { while ((c = opt_parse (argc, argv, &optind, &optarg, opt)) != 0)
switch (c) { {
switch (c)
{
case 'a': case 'a':
free(address); free (address);
address = strdup(optarg); address = strdup (optarg);
break; break;
case 'n': case 'n':
channel_count = 0; channel_count = 0;
tmp = strtol(optarg, &endp, 0); tmp = strtol (optarg, &endp, 0);
if (*endp || tmp < 1 || tmp > UE9_CHANNELS) { if (*endp || tmp < 1 || tmp > UE9_CHANNELS)
info("bad number of channels: %s\n", optarg); {
info ("bad number of channels: %s\n", optarg);
goto printhelp; goto printhelp;
} }
for (i = 0; i < tmp; i++) for (i = 0; i < tmp; i++)
...@@ -126,48 +136,58 @@ int main(int argc, char *argv[]) ...@@ -126,48 +136,58 @@ int main(int argc, char *argv[])
break; break;
case 'C': case 'C':
channel_count = 0; channel_count = 0;
do { do
tmp = strtol(optarg, &endp, 0); {
if (*endp != '\0' && *endp != ',') { tmp = strtol (optarg, &endp, 0);
if (*endp != '\0' && *endp != ',')
{
//|| tmp < 0 || tmp >= UE9_CHANNELS) { //|| tmp < 0 || tmp >= UE9_CHANNELS) {
info("bad channel number: %s\n", optarg); info ("bad channel number: %s\n", optarg);
goto printhelp; goto printhelp;
} }
//We do not want to overflow channel_list, so we need the check here //We do not want to overflow channel_list, so we need the check here
//The rest of the sanity checking can come later after we know whether this is a //The rest of the sanity checking can come later after we know whether this is a
//LabJack or a NerdJack //LabJack or a NerdJack
#if UE9_CHANNELS > NERDJACK_CHANNELS #if UE9_CHANNELS > NERDJACK_CHANNELS
if (channel_count >= UE9_CHANNELS) { if (channel_count >= UE9_CHANNELS)
{
#else #else
if (channel_count >= NERDJACK_CHANNELS) { if (channel_count >= NERDJACK_CHANNELS)
{
#endif #endif
info("error: too many channels specified\n"); info ("error: too many channels specified\n");
goto printhelp; goto printhelp;
} }
channel_list[channel_count++] = tmp; channel_list[channel_count++] = tmp;
optarg = endp + 1; optarg = endp + 1;
} while (*endp); }
while (*endp);
break; break;
case 'r': case 'r':
desired_rate = strtod(optarg, &endp); desired_rate = strtod (optarg, &endp);
if(*endp || desired_rate <= 0) { if (*endp || desired_rate <= 0)
info("bad rate: %s\n", optarg); {
info ("bad rate: %s\n", optarg);
goto printhelp; goto printhelp;
} }
break; break;
case 'l': case 'l':
lines = strtol(optarg, &endp, 0); lines = strtol (optarg, &endp, 0);
if (*endp || lines <= 0) { if (*endp || lines <= 0)
info("bad number of lines: %s\n", optarg); {
info ("bad number of lines: %s\n", optarg);
goto printhelp; goto printhelp;
} }
break; break;
case 'p': case 'p':
tmp = strtol(optarg, &endp, 0); tmp = strtol (optarg, &endp, 0);
if (tmp <= 3 && tmp >= 0) { if (tmp <= 3 && tmp >= 0)
{
precision = tmp; precision = tmp;
} else { }
info("Bad argument to p: %s\n",optarg); else
{
info ("Bad argument to p: %s\n", optarg);
goto printhelp; goto printhelp;
} }
break; break;
...@@ -184,15 +204,17 @@ int main(int argc, char *argv[]) ...@@ -184,15 +204,17 @@ int main(int argc, char *argv[])
forceretry++; forceretry++;
break; break;
case 'c': case 'c':
if (convert != 0) { if (convert != 0)
info("specify only one conversion type\n"); {
info ("specify only one conversion type\n");
goto printhelp; goto printhelp;
} }
convert = CONVERT_VOLTS; convert = CONVERT_VOLTS;
break; break;
case 'H': case 'H':
if (convert != 0) { if (convert != 0)
info("specify only one conversion type\n"); {
info ("specify only one conversion type\n");
goto printhelp; goto printhelp;
} }
convert = CONVERT_HEX; convert = CONVERT_HEX;
...@@ -203,9 +225,9 @@ int main(int argc, char *argv[]) ...@@ -203,9 +225,9 @@ int main(int argc, char *argv[])
verb_count++; verb_count++;
break; break;
case 'V': case 'V':
printf("ljstream " VERSION "\n"); printf ("ljstream " VERSION "\n");
printf("Written by Jim Paris <jim@jtan.com>\n"); printf ("Written by Jim Paris <jim@jtan.com>\n");
printf("This program comes with no warranty and is " printf ("This program comes with no warranty and is "
"provided under the GPLv2.\n"); "provided under the GPLv2.\n");
return 0; return 0;
break; break;
...@@ -213,35 +235,45 @@ int main(int argc, char *argv[]) ...@@ -213,35 +235,45 @@ int main(int argc, char *argv[])
help = stdout; help = stdout;
default: default:
printhelp: printhelp:
fprintf(help, "Usage: %s [options]\n", *argv); fprintf (help, "Usage: %s [options]\n", *argv);
opt_help(opt, help); opt_help (opt, help);
fprintf(help, "Read data from the specified Labjack UE9" fprintf (help, "Read data from the specified Labjack UE9"
" via Ethernet. See README for details.\n"); " via Ethernet. See README for details.\n");
return (help == stdout) ? 0 : 1; return (help == stdout) ? 0 : 1;
} }
} }
doneparse: doneparse:
if (nerdjack) { if (nerdjack)
if (channel_count > NERDJACK_CHANNELS) { {
info("Too many channels for NerdJack\n"); if (channel_count > NERDJACK_CHANNELS)
{
info ("Too many channels for NerdJack\n");
goto printhelp; goto printhelp;
} }
for (i = 0; i < channel_count; i++) { for (i = 0; i < channel_count; i++)
if (channel_list[i] >= NERDJACK_CHANNELS) { {
info("Channel is out of NerdJack range: %d\n",channel_list[i]); if (channel_list[i] >= NERDJACK_CHANNELS)
{
info ("Channel is out of NerdJack range: %d\n",
channel_list[i]);
goto printhelp; goto printhelp;
} }
} }
} else { }
if (channel_count > UE9_CHANNELS) { else
info("Too many channels for LabJack\n"); {
if (channel_count > UE9_CHANNELS)
{
info ("Too many channels for LabJack\n");
goto printhelp; goto printhelp;
} }
for (i = 0; i < channel_count; i++) { for (i = 0; i < channel_count; i++)
if (channel_list[i] >= UE9_CHANNELS) { {
info("Channel is out of LabJack range: %d\n",channel_list[i]); if (channel_list[i] >= UE9_CHANNELS)
{
info ("Channel is out of LabJack range: %d\n", channel_list[i]);
goto printhelp; goto printhelp;
} }
} }
...@@ -249,81 +281,101 @@ int main(int argc, char *argv[]) ...@@ -249,81 +281,101 @@ int main(int argc, char *argv[])
if (optind < argc) { if (optind < argc)
info("error: too many arguments (%s)\n\n", argv[optind]); {
info ("error: too many arguments (%s)\n\n", argv[optind]);
goto printhelp; goto printhelp;
} }
if (forceretry && oneshot) { if (forceretry && oneshot)
info("forceretry and oneshot options are mutually exclusive\n"); {
info ("forceretry and oneshot options are mutually exclusive\n");
goto printhelp; goto printhelp;
} }
/* Two channels if none specified */ /* Two channels if none specified */
if (channel_count == 0) { if (channel_count == 0)
{
channel_list[channel_count++] = 0; channel_list[channel_count++] = 0;
channel_list[channel_count++] = 1; channel_list[channel_count++] = 1;
} }
if (verb_count) { if (verb_count)
info("Scanning channels:"); {
info ("Scanning channels:");
for (i = 0; i < channel_count; i++) for (i = 0; i < channel_count; i++)
info(" AIN%d", channel_list[i]); info (" AIN%d", channel_list[i]);
info("\n"); info ("\n");
} }
/* Figure out actual rate. */ /* Figure out actual rate. */
if (nerdjack) { if (nerdjack)
if (nerdjack_choose_scan(desired_rate, &actual_rate, &period) < 0) { {
info("error: can't achieve requested scan rate (%lf Hz)\n", if (nerdjack_choose_scan (desired_rate, &actual_rate, &period) < 0)
{
info ("error: can't achieve requested scan rate (%lf Hz)\n",
desired_rate); desired_rate);
//return 1; //return 1;
} }
} else { }
if (ue9_choose_scan(desired_rate, &actual_rate, else
&scanconfig, &scaninterval) < 0) { {
info("error: can't achieve requested scan rate (%lf Hz)\n", if (ue9_choose_scan (desired_rate, &actual_rate,
&scanconfig, &scaninterval) < 0)
{
info ("error: can't achieve requested scan rate (%lf Hz)\n",
desired_rate); desired_rate);
//return 1; //return 1;
} }
} }
if ((desired_rate != actual_rate) || verb_count){ if ((desired_rate != actual_rate) || verb_count)
info("Actual scanrate is %lf Hz\n", actual_rate); {
info("Period is %ld\n",period); info ("Actual scanrate is %lf Hz\n", actual_rate);
info ("Period is %ld\n", period);
} }
if (verb_count && lines) { if (verb_count && lines)
info("Stopping capture after %d lines\n", lines); {
info ("Stopping capture after %d lines\n", lines);
} }
signal(SIGINT, handle_sig); signal (SIGINT, handle_sig);
signal(SIGTERM, handle_sig); signal (SIGTERM, handle_sig);
if (detect) { if (detect)
info("Autodetecting NerdJack address\n"); {
free(address); info ("Autodetecting NerdJack address\n");
if(nerdjack_detect(address) < 0) { free (address);
info("Error with autodetection\n"); if (nerdjack_detect (address) < 0)
} else { {
info("Found NerdJack at address: %s\n",address); info ("Error with autodetection\n");
}
else
{
info ("Found NerdJack at address: %s\n", address);
} }
} }
for (;;) { for (;;)
{
int ret; int ret;
if(nerdjack) { if (nerdjack)
ret = nerdDoStream(address, channel_list, channel_count, precision, period, convert, lines, showmem); {
verb("nerdDoStream returned %d\n", ret); ret =
nerdDoStream (address, channel_list, channel_count, precision,
period, convert, lines, showmem);
verb ("nerdDoStream returned %d\n", ret);
} else { }
ret = doStream(address, scanconfig, scaninterval, else
channel_list, channel_count, convert, {
lines); ret = doStream (address, scanconfig, scaninterval,
verb("doStream returned %d\n", ret); channel_list, channel_count, convert, lines);
verb ("doStream returned %d\n", ret);
} }
if (oneshot) if (oneshot)
break; break;
...@@ -331,33 +383,40 @@ int main(int argc, char *argv[]) ...@@ -331,33 +383,40 @@ int main(int argc, char *argv[])
if (ret == 0) if (ret == 0)
break; break;
if (ret == -ENOTCONN && !nerdjack) { if (ret == -ENOTCONN && !nerdjack)
info("Could not connect LabJack...Trying NerdJack\n"); {
info ("Could not connect LabJack...Trying NerdJack\n");
nerdjack = 1; nerdjack = 1;
goto doneparse; goto doneparse;
} }
if (ret == -ENOTCONN && !forceretry) { if (ret == -ENOTCONN && !forceretry)
info("Initial connection failed, giving up\n"); {
info ("Initial connection failed, giving up\n");
break; break;
} }
if (ret == -EAGAIN || ret == -ENOTCONN) { if (ret == -EAGAIN || ret == -ENOTCONN)
{
/* Some transient error. Wait a tiny bit, then retry */ /* Some transient error. Wait a tiny bit, then retry */
info("Retrying in 5 secs.\n"); info ("Retrying in 5 secs.\n");
sleep(5); sleep (5);
} else { }
info("Retrying now.\n"); else
{
info ("Retrying now.\n");
} }
} }
debug("Done loop\n"); debug ("Done loop\n");
return 0; return 0;
} }
int nerdDoStream(const char *address, int *channel_list, int channel_count, int precision, int
unsigned long period, int convert, int lines, int showmem) nerdDoStream (const char *address, int *channel_list, int channel_count,
int precision, unsigned long period, int convert, int lines,
int showmem)
{ {
int retval = -EAGAIN; int retval = -EAGAIN;
int fd_data; int fd_data;
...@@ -368,22 +427,27 @@ int nerdDoStream(const char *address, int *channel_list, int channel_count, int ...@@ -368,22 +427,27 @@ int nerdDoStream(const char *address, int *channel_list, int channel_count, int
//usleep(1000000); //usleep(1000000);
if(first_call) { if (first_call)
if (nerd_generate_command(&command, channel_list, channel_count, precision, period) < 0) { {
info("Failed to create configuration command\n"); if (nerd_generate_command
(&command, channel_list, channel_count, precision, period) < 0)
{
info ("Failed to create configuration command\n");
goto out; goto out;
} }
if (nerd_send_command(address,"STOP", 4) < 0) { if (nerd_send_command (address, "STOP", 4) < 0)
{
if (first_call) if (first_call)
retval = -ENOTCONN; retval = -ENOTCONN;
info("Failed to send STOP command\n"); info ("Failed to send STOP command\n");
goto out; goto out;
} }
if (nerd_send_command(address,&command, sizeof(command)) < 0) { if (nerd_send_command (address, &command, sizeof (command)) < 0)
info("Failed to send GET command\n"); {
info ("Failed to send GET command\n");
goto out; goto out;
} }
} }
...@@ -392,11 +456,13 @@ int nerdDoStream(const char *address, int *channel_list, int channel_count, int ...@@ -392,11 +456,13 @@ int nerdDoStream(const char *address, int *channel_list, int channel_count, int
first_call = 0; first_call = 0;
//If we had a transmission in progress, send a command to resume from there //If we had a transmission in progress, send a command to resume from there
if(started == 1) { if (started == 1)
{
char cmdbuf[10]; char cmdbuf[10];
sprintf(cmdbuf,"SETC%05hd",currentcount); sprintf (cmdbuf, "SETC%05hd", currentcount);
if (nerd_send_command(address,cmdbuf,strlen(cmdbuf)) < 0) { if (nerd_send_command (address, cmdbuf, strlen (cmdbuf)) < 0)
info("Failed to send SETC command\n"); {
info ("Failed to send SETC command\n");
goto out; goto out;
} }
} }
...@@ -404,27 +470,32 @@ int nerdDoStream(const char *address, int *channel_list, int channel_count, int ...@@ -404,27 +470,32 @@ int nerdDoStream(const char *address, int *channel_list, int channel_count, int
started = 1; started = 1;
/* Open connection */ /* Open connection */
fd_data = nerd_open(address, NERDJACK_DATA_PORT); fd_data = nerd_open (address, NERDJACK_DATA_PORT);
if (fd_data < 0) { if (fd_data < 0)
info("Connect failed: %s:%d\n", address, NERDJACK_DATA_PORT); {
info ("Connect failed: %s:%d\n", address, NERDJACK_DATA_PORT);
goto out; goto out;
} }
if (nerd_data_stream(fd_data, channel_count, channel_list, precision, convert, lines, showmem, &currentcount,period) < 0) { if (nerd_data_stream
info("Failed to open data stream\n"); (fd_data, channel_count, channel_list, precision, convert, lines,
showmem, &currentcount, period) < 0)
{
info ("Failed to open data stream\n");
goto out1; goto out1;
} }
info("Stream finished\n"); info ("Stream finished\n");
retval = 0; retval = 0;
out1: out1:
nerd_close_conn(fd_data); nerd_close_conn (fd_data);
out: out:
return retval; return retval;
} }
int doStream(const char *address, uint8_t scanconfig, uint16_t scaninterval, int
doStream (const char *address, uint8_t scanconfig, uint16_t scaninterval,
int *channel_list, int channel_count, int convert, int lines) int *channel_list, int channel_count, int convert, int lines)
{ {
int retval = -EAGAIN; int retval = -EAGAIN;
...@@ -438,9 +509,10 @@ int doStream(const char *address, uint8_t scanconfig, uint16_t scaninterval, ...@@ -438,9 +509,10 @@ int doStream(const char *address, uint8_t scanconfig, uint16_t scaninterval,
/* Open command connection. If this fails, and this is the /* Open command connection. If this fails, and this is the
first attempt, return a different error code so we give up. */ first attempt, return a different error code so we give up. */
fd_cmd = ue9_open(address, UE9_COMMAND_PORT); fd_cmd = ue9_open (address, UE9_COMMAND_PORT);
if (fd_cmd < 0) { if (fd_cmd < 0)
info("Connect failed: %s:%d\n", address, UE9_COMMAND_PORT); {
info ("Connect failed: %s:%d\n", address, UE9_COMMAND_PORT);
if (first_call) if (first_call)
retval = -ENOTCONN; retval = -ENOTCONN;
goto out; goto out;
...@@ -448,90 +520,102 @@ int doStream(const char *address, uint8_t scanconfig, uint16_t scaninterval, ...@@ -448,90 +520,102 @@ int doStream(const char *address, uint8_t scanconfig, uint16_t scaninterval,
first_call = 0; first_call = 0;
/* Make sure nothing is left over from a previous stream */ /* Make sure nothing is left over from a previous stream */
if (ue9_stream_stop(fd_cmd) == 0) if (ue9_stream_stop (fd_cmd) == 0)
verb("Stopped previous stream.\n"); verb ("Stopped previous stream.\n");
ue9_buffer_flush(fd_cmd); ue9_buffer_flush (fd_cmd);
/* Open data connection */ /* Open data connection */
fd_data = ue9_open(address, UE9_DATA_PORT); fd_data = ue9_open (address, UE9_DATA_PORT);
if (fd_data < 0) { if (fd_data < 0)
info("Connect failed: %s:%d\n", address, UE9_DATA_PORT); {
info ("Connect failed: %s:%d\n", address, UE9_DATA_PORT);
goto out1; goto out1;
} }
/* Get calibration */ /* Get calibration */
if (ue9_get_calibration(fd_cmd, &ci.calib) < 0) { if (ue9_get_calibration (fd_cmd, &ci.calib) < 0)
info("Failed to get device calibration\n"); {
info ("Failed to get device calibration\n");
goto out2; goto out2;
} }
/* Set stream configuration */ /* Set stream configuration */
if (ue9_streamconfig_simple(fd_cmd, channel_list, channel_count, if (ue9_streamconfig_simple (fd_cmd, channel_list, channel_count,
scanconfig, scaninterval, scanconfig, scaninterval,
UE9_BIPOLAR_GAIN1) < 0) { UE9_BIPOLAR_GAIN1) < 0)
info("Failed to set stream configuration\n"); {
info ("Failed to set stream configuration\n");
goto out2; goto out2;
} }
/* Start stream */ /* Start stream */
if (ue9_stream_start(fd_cmd) < 0) { if (ue9_stream_start (fd_cmd) < 0)
info("Failed to start stream\n"); {
info ("Failed to start stream\n");
goto out2; goto out2;
} }
/* Stream data */ /* Stream data */
ret = ue9_stream_data(fd_data, channel_count, data_callback, (void *)&ci); ret = ue9_stream_data (fd_data, channel_count, data_callback, (void *) &ci);
if (ret < 0) { if (ret < 0)
info("Data stream failed with error %d\n", ret); {
info ("Data stream failed with error %d\n", ret);
goto out3; goto out3;
} }
info("Stream finished\n"); info ("Stream finished\n");
retval = 0; retval = 0;
out3: out3:
/* Stop stream and clean up */ /* Stop stream and clean up */
ue9_stream_stop(fd_cmd); ue9_stream_stop (fd_cmd);
ue9_buffer_flush(fd_cmd); ue9_buffer_flush (fd_cmd);
out2: out2:
ue9_close(fd_data); ue9_close (fd_data);
out1: out1:
ue9_close(fd_cmd); ue9_close (fd_cmd);
out: out:
return retval; return retval;
} }
int data_callback(int channels, uint16_t *data, void *context) int
data_callback (int channels, uint16_t * data, void *context)
{ {
int i; int i;
struct callbackInfo *ci = (struct callbackInfo *)context; struct callbackInfo *ci = (struct callbackInfo *) context;
static int lines = 0; static int lines = 0;
columns_left = channels; columns_left = channels;
for (i = 0; i < channels; i++) { for (i = 0; i < channels; i++)
switch (ci->convert) { {
switch (ci->convert)
{
case CONVERT_VOLTS: case CONVERT_VOLTS:
if (printf("%lf", ue9_binary_to_analog( if (printf
&ci->calib, UE9_BIPOLAR_GAIN1, 12, ("%lf",
ue9_binary_to_analog (&ci->calib, UE9_BIPOLAR_GAIN1, 12,
data[i])) < 0) data[i])) < 0)
goto bad; goto bad;
break; break;
case CONVERT_HEX: case CONVERT_HEX:
if (printf("%04X", data[i]) < 0) if (printf ("%04X", data[i]) < 0)
goto bad; goto bad;
break; break;
default: default:
case CONVERT_DEC: case CONVERT_DEC:
if (printf("%d", data[i]) < 0) if (printf ("%d", data[i]) < 0)
goto bad; goto bad;
break; break;
} }
columns_left--; columns_left--;
if (i < (channels - 1)) { if (i < (channels - 1))
if (ci->convert != CONVERT_HEX && putchar(' ') < 0) {
if (ci->convert != CONVERT_HEX && putchar (' ') < 0)
goto bad; goto bad;
} else { }
if (putchar('\n') < 0) else
{
if (putchar ('\n') < 0)
goto bad; goto bad;
lines++; lines++;
if (ci->maxlines && lines >= ci->maxlines) if (ci->maxlines && lines >= ci->maxlines)
...@@ -542,6 +626,6 @@ int data_callback(int channels, uint16_t *data, void *context) ...@@ -542,6 +626,6 @@ int data_callback(int channels, uint16_t *data, void *context)
return 0; return 0;
bad: bad:
info("Output error (disk full?)\n"); info ("Output error (disk full?)\n");
return -1; return -1;
} }
...@@ -25,38 +25,41 @@ ...@@ -25,38 +25,41 @@
#define UE9_COMMAND_PORT 52360 #define UE9_COMMAND_PORT 52360
struct options opt[] = { struct options opt[] = {
{ 'a', "address", "string", "host/address of UE9 (192.168.1.209)" }, {'a', "address", "string", "host/address of UE9 (192.168.1.209)"},
{ 'h', "help", NULL, "this help" }, {'h', "help", NULL, "this help"},
{ 'v', "verbose", NULL, "be verbose" }, {'v', "verbose", NULL, "be verbose"},
{ 'V', "version", NULL, "show version number and exit" }, {'V', "version", NULL, "show version number and exit"},
{ 0, NULL, NULL, NULL } {0, NULL, NULL, NULL}
}; };
int main(int argc, char *argv[]) int
main (int argc, char *argv[])
{ {
int optind; int optind;
char *optarg; char *optarg;
char c; char c;
FILE *help = stderr; FILE *help = stderr;
char *address = strdup(DEFAULT_HOST); char *address = strdup (DEFAULT_HOST);
int fd; int fd;
int ret; int ret;
/* Parse arguments */ /* Parse arguments */
opt_init(&optind); opt_init (&optind);
while ((c = opt_parse(argc, argv, &optind, &optarg, opt)) != 0) { while ((c = opt_parse (argc, argv, &optind, &optarg, opt)) != 0)
switch (c) { {
switch (c)
{
case 'a': case 'a':
free(address); free (address);
address = strdup(optarg); address = strdup (optarg);
break; break;
case 'v': case 'v':
verb_count++; verb_count++;
break; break;
case 'V': case 'V':
printf("ljconfig " VERSION "\n"); printf ("ljconfig " VERSION "\n");
printf("Written by Jim Paris <jim@jtan.com>\n"); printf ("Written by Jim Paris <jim@jtan.com>\n");
printf("This program comes with no warranty and is " printf ("This program comes with no warranty and is "
"provided under the GPLv2.\n"); "provided under the GPLv2.\n");
return 0; return 0;
break; break;
...@@ -64,24 +67,26 @@ int main(int argc, char *argv[]) ...@@ -64,24 +67,26 @@ int main(int argc, char *argv[])
help = stdout; help = stdout;
default: default:
printhelp: printhelp:
fprintf(help, "Usage: %s [options]\n", *argv); fprintf (help, "Usage: %s [options]\n", *argv);
opt_help(opt, help); opt_help (opt, help);
fprintf(help, "Displays/changes Labjack UE9 config.\n"); fprintf (help, "Displays/changes Labjack UE9 config.\n");
return (help == stdout) ? 0 : 1; return (help == stdout) ? 0 : 1;
} }
} }
if(optind<argc) { if (optind < argc)
info("Error: too many arguments (%s)\n\n", argv[optind]); {
info ("Error: too many arguments (%s)\n\n", argv[optind]);
goto printhelp; goto printhelp;
} }
ret = 1; ret = 1;
/* Open */ /* Open */
fd = ue9_open(address, UE9_COMMAND_PORT); fd = ue9_open (address, UE9_COMMAND_PORT);
if (fd < 0) { if (fd < 0)
info("Connect failed: %s:%d\n", address, UE9_COMMAND_PORT); {
info ("Connect failed: %s:%d\n", address, UE9_COMMAND_PORT);
goto out0; goto out0;
} }
...@@ -89,10 +94,9 @@ int main(int argc, char *argv[]) ...@@ -89,10 +94,9 @@ int main(int argc, char *argv[])
ret = 0; ret = 0;
out1: out1:
/* Close */ /* Close */
ue9_close(fd); ue9_close (fd);
out0: out0:
return ret; return ret;
} }
...@@ -16,52 +16,53 @@ ...@@ -16,52 +16,53 @@
#include "ue9.h" #include "ue9.h"
#include "compat.h" #include "compat.h"
int main(int argc, char *argv[]) int
main (int argc, char *argv[])
{ {
int fd_cmd; int fd_cmd;
struct ue9Calibration calib; struct ue9Calibration calib;
verb_count = 2; verb_count = 2;
fd_cmd = ue9_open("192.168.1.209", 52360); fd_cmd = ue9_open ("192.168.1.209", 52360);
if (fd_cmd < 0) { if (fd_cmd < 0)
fprintf(stderr, "ue9_open: %s\n", {
compat_strerror(errno)); fprintf (stderr, "ue9_open: %s\n", compat_strerror (errno));
return 1; return 1;
} }
if (ue9_get_calibration(fd_cmd, &calib) < 0) { if (ue9_get_calibration (fd_cmd, &calib) < 0)
fprintf(stderr, "ue9_get_calibration: %s\n", {
compat_strerror(errno)); fprintf (stderr, "ue9_get_calibration: %s\n", compat_strerror (errno));
return 1; return 1;
} }
printf("double unipolarSlope[0] = %lf\n", calib.unipolarSlope[0]); printf ("double unipolarSlope[0] = %lf\n", calib.unipolarSlope[0]);
printf("double unipolarSlope[1] = %lf\n", calib.unipolarSlope[1]); printf ("double unipolarSlope[1] = %lf\n", calib.unipolarSlope[1]);
printf("double unipolarSlope[2] = %lf\n", calib.unipolarSlope[2]); printf ("double unipolarSlope[2] = %lf\n", calib.unipolarSlope[2]);
printf("double unipolarSlope[3] = %lf\n", calib.unipolarSlope[3]); printf ("double unipolarSlope[3] = %lf\n", calib.unipolarSlope[3]);
printf("double unipolarOffset[0] = %lf\n", calib.unipolarOffset[0]); printf ("double unipolarOffset[0] = %lf\n", calib.unipolarOffset[0]);
printf("double unipolarOffset[1] = %lf\n", calib.unipolarOffset[1]); printf ("double unipolarOffset[1] = %lf\n", calib.unipolarOffset[1]);
printf("double unipolarOffset[2] = %lf\n", calib.unipolarOffset[2]); printf ("double unipolarOffset[2] = %lf\n", calib.unipolarOffset[2]);
printf("double unipolarOffset[3] = %lf\n", calib.unipolarOffset[3]); printf ("double unipolarOffset[3] = %lf\n", calib.unipolarOffset[3]);
printf("double bipolarSlope = %lf\n", calib.bipolarSlope); printf ("double bipolarSlope = %lf\n", calib.bipolarSlope);
printf("double bipolarOffset = %lf\n", calib.bipolarOffset); printf ("double bipolarOffset = %lf\n", calib.bipolarOffset);
printf("double DACSlope[0] = %lf\n", calib.DACSlope[0]); printf ("double DACSlope[0] = %lf\n", calib.DACSlope[0]);
printf("double DACSlope[1] = %lf\n", calib.DACSlope[1]); printf ("double DACSlope[1] = %lf\n", calib.DACSlope[1]);
printf("double DACOffset[0] = %lf\n", calib.DACOffset[0]); printf ("double DACOffset[0] = %lf\n", calib.DACOffset[0]);
printf("double DACOffset[1] = %lf\n", calib.DACOffset[1]); printf ("double DACOffset[1] = %lf\n", calib.DACOffset[1]);
printf("double tempSlope = %lf\n", calib.tempSlope); printf ("double tempSlope = %lf\n", calib.tempSlope);
printf("double tempSlopeLow = %lf\n", calib.tempSlopeLow); printf ("double tempSlopeLow = %lf\n", calib.tempSlopeLow);
printf("double calTemp = %lf\n", calib.calTemp); printf ("double calTemp = %lf\n", calib.calTemp);
printf("double Vref = %lf\n", calib.Vref); printf ("double Vref = %lf\n", calib.Vref);
printf("double VrefDiv2 = %lf\n", calib.VrefDiv2); printf ("double VrefDiv2 = %lf\n", calib.VrefDiv2);
printf("double VsSlope = %lf\n", calib.VsSlope); printf ("double VsSlope = %lf\n", calib.VsSlope);
printf("double hiResUnipolarSlope = %lf\n", calib.hiResUnipolarSlope); printf ("double hiResUnipolarSlope = %lf\n", calib.hiResUnipolarSlope);
printf("double hiResUnipolarOffset = %lf\n", calib.hiResUnipolarOffset); printf ("double hiResUnipolarOffset = %lf\n", calib.hiResUnipolarOffset);
printf("double hiResBipolarSlope = %lf\n", calib.hiResBipolarSlope); printf ("double hiResBipolarSlope = %lf\n", calib.hiResBipolarSlope);
printf("double hiResBipolarOffset = %lf\n", calib.hiResBipolarOffset); printf ("double hiResBipolarOffset = %lf\n", calib.hiResBipolarOffset);
ue9_close(fd_cmd); ue9_close (fd_cmd);
return 0; return 0;
} }
...@@ -28,12 +28,14 @@ ...@@ -28,12 +28,14 @@
#define NERDJACK_TIMEOUT 5 /* Timeout for connect/send/recv, in seconds */ #define NERDJACK_TIMEOUT 5 /* Timeout for connect/send/recv, in seconds */
//Struct holding information about how channels should be reordered for output //Struct holding information about how channels should be reordered for output
typedef struct { typedef struct
{
int numCopies; int numCopies;
int * destlist; int *destlist;
} deststruct; } deststruct;
typedef struct __attribute__((__packed__)) { typedef struct __attribute__ ((__packed__))
{
unsigned char headerone; unsigned char headerone;
unsigned char headertwo; unsigned char headertwo;
unsigned short packetNumber; unsigned short packetNumber;
...@@ -45,15 +47,18 @@ typedef struct __attribute__((__packed__)) { ...@@ -45,15 +47,18 @@ typedef struct __attribute__((__packed__)) {
/* Choose the best ScanConfig and ScanInterval parameters for the /* Choose the best ScanConfig and ScanInterval parameters for the
desired scanrate. Returns -1 if no valid config found */ desired scanrate. Returns -1 if no valid config found */
int nerdjack_choose_scan(double desired_rate, double *actual_rate, unsigned long *period) int
nerdjack_choose_scan (double desired_rate, double *actual_rate,
unsigned long *period)
{ {
//The ffffe is because of a silicon bug. The last bit is unusable in all //The ffffe is because of a silicon bug. The last bit is unusable in all
//devices so far. It is worked around on the chip, but giving it exactly //devices so far. It is worked around on the chip, but giving it exactly
//0xfffff would cause the workaround code to roll over. //0xfffff would cause the workaround code to roll over.
*period = floor((double) NERDJACK_CLOCK_RATE / desired_rate); *period = floor ((double) NERDJACK_CLOCK_RATE / desired_rate);
if(*period > 0x0ffffe) { if (*period > 0x0ffffe)
info("Cannot sample that slowly\n"); {
*actual_rate = (double)NERDJACK_CLOCK_RATE / (double) 0x0ffffe; info ("Cannot sample that slowly\n");
*actual_rate = (double) NERDJACK_CLOCK_RATE / (double) 0x0ffffe;
*period = 0x0ffffe; *period = 0x0ffffe;
//info("Sampling at slowest rate:%f\n",*actual_rate); //info("Sampling at slowest rate:%f\n",*actual_rate);
...@@ -61,14 +66,17 @@ int nerdjack_choose_scan(double desired_rate, double *actual_rate, unsigned long ...@@ -61,14 +66,17 @@ int nerdjack_choose_scan(double desired_rate, double *actual_rate, unsigned long
} }
//Period holds the period register for the NerdJack, so it needs to be right //Period holds the period register for the NerdJack, so it needs to be right
*actual_rate = (double) NERDJACK_CLOCK_RATE / (double) *period; *actual_rate = (double) NERDJACK_CLOCK_RATE / (double) *period;
if(*actual_rate != desired_rate) { if (*actual_rate != desired_rate)
{
//info("Sampling at nearest rate:%f\n",*actual_rate); //info("Sampling at nearest rate:%f\n",*actual_rate);
return -1; return -1;
} }
return 0; return 0;
} }
int nerdjack_detect(char * ipAddress) { int
nerdjack_detect (char *ipAddress)
{
int32_t sock, receivesock; int32_t sock, receivesock;
struct sockaddr_in sa, receiveaddr, sFromAddr; struct sockaddr_in sa, receiveaddr, sFromAddr;
int bytes_sent, buffer_length; int bytes_sent, buffer_length;
...@@ -76,33 +84,35 @@ int nerdjack_detect(char * ipAddress) { ...@@ -76,33 +84,35 @@ int nerdjack_detect(char * ipAddress) {
char incomingData[10]; char incomingData[10];
unsigned int lFromLen; unsigned int lFromLen;
sprintf(buffer, "TEST"); sprintf (buffer, "TEST");
buffer_length = strlen(buffer) + 1; buffer_length = strlen (buffer) + 1;
net_init(); net_init ();
sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); sock = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
receivesock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP); receivesock = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
/* Set nonblocking */ /* Set nonblocking */
if (soblock(sock, 0) < 0) { if (soblock (sock, 0) < 0)
verb("can't set nonblocking\n"); {
verb ("can't set nonblocking\n");
return -1; return -1;
} }
/* Set nonblocking */ /* Set nonblocking */
if (soblock(receivesock, 0) < 0) { if (soblock (receivesock, 0) < 0)
verb("can't set nonblocking\n"); {
verb ("can't set nonblocking\n");
return -1; return -1;
} }
int opt = 1; int opt = 1;
setsockopt(sock,SOL_SOCKET,SO_BROADCAST,(void *) &opt,sizeof(int)); setsockopt (sock, SOL_SOCKET, SO_BROADCAST, (void *) &opt, sizeof (int));
if((-1 == sock) || (-1 == receivesock)) /* if socket failed to initialize, exit */ if ((-1 == sock) || (-1 == receivesock)) /* if socket failed to initialize, exit */
{ {
verb("Error Creating Socket\n"); verb ("Error Creating Socket\n");
return -1; return -1;
} }
...@@ -111,69 +121,85 @@ int nerdjack_detect(char * ipAddress) { ...@@ -111,69 +121,85 @@ int nerdjack_detect(char * ipAddress) {
receiveaddr.sin_family = PF_INET; receiveaddr.sin_family = PF_INET;
//Setup ports to send on DATA and receive on RECEIVE //Setup ports to send on DATA and receive on RECEIVE
receiveaddr.sin_port = htons(NERDJACK_UDP_RECEIVE_PORT); receiveaddr.sin_port = htons (NERDJACK_UDP_RECEIVE_PORT);
sa.sin_port = htons(NERDJACK_DATA_PORT); sa.sin_port = htons (NERDJACK_DATA_PORT);
//Receive from any IP address, Will send to broadcast //Receive from any IP address, Will send to broadcast
receiveaddr.sin_addr.s_addr = INADDR_ANY; receiveaddr.sin_addr.s_addr = INADDR_ANY;
sa.sin_addr.s_addr = INADDR_BROADCAST; sa.sin_addr.s_addr = INADDR_BROADCAST;
bind(receivesock,(struct sockaddr*) &receiveaddr, sizeof(struct sockaddr_in)); bind (receivesock, (struct sockaddr *) &receiveaddr,
sizeof (struct sockaddr_in));
bytes_sent = sendto(sock, buffer, buffer_length, 0,(struct sockaddr*) &sa, sizeof(struct sockaddr_in) ); bytes_sent =
if(bytes_sent < 0) { sendto (sock, buffer, buffer_length, 0, (struct sockaddr *) &sa,
info("Error sending packet: %s\n", strerror(errno) ); sizeof (struct sockaddr_in));
if (bytes_sent < 0)
{
info ("Error sending packet: %s\n", strerror (errno));
return -1; return -1;
} }
lFromLen = sizeof(sFromAddr); lFromLen = sizeof (sFromAddr);
if(0 > recvfrom_timeout(receivesock, incomingData, sizeof(incomingData),0,(struct sockaddr *) &sFromAddr, &lFromLen, if (0 >
& (struct timeval) { .tv_sec = NERDJACK_TIMEOUT })) { recvfrom_timeout (receivesock, incomingData, sizeof (incomingData), 0,
(struct sockaddr *) &sFromAddr, &lFromLen,
&(struct timeval)
{
.tv_sec = NERDJACK_TIMEOUT}))
{
return -1; return -1;
} }
ipAddress = malloc(INET_ADDRSTRLEN); ipAddress = malloc (INET_ADDRSTRLEN);
//It isn't ipv6 friendly, but inet_ntop isn't on Windows... //It isn't ipv6 friendly, but inet_ntop isn't on Windows...
strcpy(ipAddress, inet_ntoa(sFromAddr.sin_addr)); strcpy (ipAddress, inet_ntoa (sFromAddr.sin_addr));
close(sock); /* close the socket */ close (sock); /* close the socket */
close(receivesock); close (receivesock);
return 0; return 0;
} }
int nerd_send_command(const char * address, void * command, int length) int
nerd_send_command (const char *address, void *command, int length)
{ {
int ret,fd_command; int ret, fd_command;
char buf[3]; char buf[3];
fd_command = nerd_open(address, NERDJACK_COMMAND_PORT); fd_command = nerd_open (address, NERDJACK_COMMAND_PORT);
if (fd_command < 0) { if (fd_command < 0)
info("Connect failed: %s:%d\n", address, NERDJACK_COMMAND_PORT); {
info ("Connect failed: %s:%d\n", address, NERDJACK_COMMAND_PORT);
return -2; return -2;
} }
/* Send request */ /* Send request */
ret = send_all_timeout(fd_command, command, length, 0, ret = send_all_timeout (fd_command, command, length, 0, &(struct timeval)
& (struct timeval) { .tv_sec = NERDJACK_TIMEOUT }); {
if (ret < 0 || ret != length) { .tv_sec = NERDJACK_TIMEOUT});
verb("short send %d\n", (int)ret); if (ret < 0 || ret != length)
{
verb ("short send %d\n", (int) ret);
return -1; return -1;
} }
ret = recv_all_timeout(fd_command,buf,3,0, ret = recv_all_timeout (fd_command, buf, 3, 0, &(struct timeval)
& (struct timeval) { .tv_sec = NERDJACK_TIMEOUT }); {
.tv_sec = NERDJACK_TIMEOUT});
nerd_close_conn(fd_command); nerd_close_conn (fd_command);
if (ret < 0 || ret != 3) { if (ret < 0 || ret != 3)
verb("Error receiving OK for command\n"); {
verb ("Error receiving OK for command\n");
return -1; return -1;
} }
if (0 != strcmp("OK",buf)){ if (0 != strcmp ("OK", buf))
verb("Did not receive OK. Received %s\n",buf); {
verb ("Did not receive OK. Received %s\n", buf);
return -3; return -3;
} }
...@@ -181,7 +207,10 @@ int nerd_send_command(const char * address, void * command, int length) ...@@ -181,7 +207,10 @@ int nerd_send_command(const char * address, void * command, int length)
} }
//Initialize the channel structure to distill how data should be displayed //Initialize the channel structure to distill how data should be displayed
static void nerd_init_channels(deststruct * destination, int numChannels, int numChannelsSampled, int *channel_list) { static void
nerd_init_channels (deststruct * destination, int numChannels,
int numChannelsSampled, int *channel_list)
{
int channelprocessing = 0; int channelprocessing = 0;
int currentalign = 0; //Index into sampled channels int currentalign = 0; //Index into sampled channels
...@@ -189,16 +218,21 @@ static void nerd_init_channels(deststruct * destination, int numChannels, int nu ...@@ -189,16 +218,21 @@ static void nerd_init_channels(deststruct * destination, int numChannels, int nu
int tempdestlist[NERDJACK_CHANNELS]; int tempdestlist[NERDJACK_CHANNELS];
//Clear out destination stuff //Clear out destination stuff
for(i=0; i < numChannelsSampled;i++) { for (i = 0; i < numChannelsSampled; i++)
{
destination[i].numCopies = 0; destination[i].numCopies = 0;
} }
for(channelprocessing = 0; channelprocessing < numChannelsSampled; channelprocessing++) { for (channelprocessing = 0; channelprocessing < numChannelsSampled;
channelprocessing++)
{
//Find out how many copies of each channel so we malloc the right things //Find out how many copies of each channel so we malloc the right things
currentalign = 0; currentalign = 0;
for(i = 0; i < numChannels; i++) { for (i = 0; i < numChannels; i++)
if(channelprocessing == channel_list[i]) { {
if (channelprocessing == channel_list[i])
{
tempdestlist[currentalign] = i; tempdestlist[currentalign] = i;
currentalign++; currentalign++;
} }
...@@ -207,10 +241,13 @@ static void nerd_init_channels(deststruct * destination, int numChannels, int nu ...@@ -207,10 +241,13 @@ static void nerd_init_channels(deststruct * destination, int numChannels, int nu
//If this channel is wanted, set it up. //If this channel is wanted, set it up.
if(currentalign > 0) { if (currentalign > 0)
{
destination[channelprocessing].numCopies = currentalign; destination[channelprocessing].numCopies = currentalign;
destination[channelprocessing].destlist = malloc( destination[channelprocessing].numCopies * sizeof(int) ); destination[channelprocessing].destlist =
memcpy(destination[channelprocessing].destlist, tempdestlist, destination[channelprocessing].numCopies * sizeof(int) ); malloc (destination[channelprocessing].numCopies * sizeof (int));
memcpy (destination[channelprocessing].destlist, tempdestlist,
destination[channelprocessing].numCopies * sizeof (int));
} }
} }
...@@ -219,7 +256,10 @@ static void nerd_init_channels(deststruct * destination, int numChannels, int nu ...@@ -219,7 +256,10 @@ static void nerd_init_channels(deststruct * destination, int numChannels, int nu
} }
int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precision, int convert, int lines, int showmem, unsigned short * currentcount, unsigned int period) int
nerd_data_stream (int data_fd, int numChannels, int *channel_list,
int precision, int convert, int lines, int showmem,
unsigned short *currentcount, unsigned int period)
{ {
//Variables that should persist across retries //Variables that should persist across retries
static dataPacket buf; static dataPacket buf;
...@@ -244,11 +284,13 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis ...@@ -244,11 +284,13 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis
int numgroups = 0; int numgroups = 0;
long double volts; long double volts;
unsigned int expectedtimeout = (period * NERDJACK_NUM_SAMPLES / NERDJACK_CLOCK_RATE) + 2; unsigned int expectedtimeout =
(period * NERDJACK_NUM_SAMPLES / NERDJACK_CLOCK_RATE) + 2;
//Check to see if we're trying to resume //Check to see if we're trying to resume
//Don't blow away linesleft in that case //Don't blow away linesleft in that case
if(lines != 0 && linesleft == 0) { if (lines != 0 && linesleft == 0)
{
linesleft = lines; linesleft = lines;
} }
...@@ -256,14 +298,16 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis ...@@ -256,14 +298,16 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis
int numChannelsSampled = channel_list[0] + 1; int numChannelsSampled = channel_list[0] + 1;
//The number sampled will be the highest channel requested plus 1 (i.e. channel 0 requested means 1 sampled) //The number sampled will be the highest channel requested plus 1 (i.e. channel 0 requested means 1 sampled)
for(i = 0; i < numChannels; i++) { for (i = 0; i < numChannels; i++)
{
if (channel_list[i] + 1 > numChannelsSampled) if (channel_list[i] + 1 > numChannelsSampled)
numChannelsSampled = channel_list[i] + 1; numChannelsSampled = channel_list[i] + 1;
} }
deststruct destination[numChannelsSampled]; deststruct destination[numChannelsSampled];
nerd_init_channels(destination,numChannels,numChannelsSampled, channel_list); nerd_init_channels (destination, numChannels, numChannelsSampled,
channel_list);
//Now destination structure array is set as well as numDuplicates. //Now destination structure array is set as well as numDuplicates.
...@@ -273,28 +317,36 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis ...@@ -273,28 +317,36 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis
//Loop forever to grab data //Loop forever to grab data
while((charsread = recv_all_timeout(data_fd,&buf,NERDJACK_PACKET_SIZE,0, while ((charsread =
& (struct timeval) { .tv_sec = expectedtimeout }))){ recv_all_timeout (data_fd, &buf, NERDJACK_PACKET_SIZE, 0,
&(struct timeval)
{
.tv_sec = expectedtimeout})))
{
//We want a complete packet, so take the chars so far and keep waiting //We want a complete packet, so take the chars so far and keep waiting
if(charsread != NERDJACK_PACKET_SIZE) { if (charsread != NERDJACK_PACKET_SIZE)
{
//There was a problem getting data. Probably a closed //There was a problem getting data. Probably a closed
//connection. //connection.
info("Packet timed out or was too short\n"); info ("Packet timed out or was too short\n");
return -2; return -2;
} }
//First check the header info //First check the header info
if(buf.headerone != 0xF0 || buf.headertwo != 0xAA) { if (buf.headerone != 0xF0 || buf.headertwo != 0xAA)
info("No Header info\n"); {
info ("No Header info\n");
return -1; return -1;
} }
//Check counter info to make sure not out of order //Check counter info to make sure not out of order
tempshort = ntohs(buf.packetNumber); tempshort = ntohs (buf.packetNumber);
//tempshort = (buf[2] << 8) | buf[3]; //tempshort = (buf[2] << 8) | buf[3];
if(tempshort != *currentcount ){ if (tempshort != *currentcount)
info("Count wrong. Expected %hd but got %hd\n", *currentcount, tempshort); {
info ("Count wrong. Expected %hd but got %hd\n", *currentcount,
tempshort);
return -1; return -1;
} }
...@@ -303,38 +355,51 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis ...@@ -303,38 +355,51 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis
//Process the rest of the header and update the index value to be pointing after it //Process the rest of the header and update the index value to be pointing after it
charsprocessed = 12; charsprocessed = 12;
memused = ntohl(buf.lwipmemoryused); memused = ntohl (buf.lwipmemoryused);
adcused = ntohs(buf.adcused); adcused = ntohs (buf.adcused);
packetsready = ntohs(buf.packetsready); packetsready = ntohs (buf.packetsready);
alignment = 0; alignment = 0;
numgroups = 0; numgroups = 0;
if(showmem) { if (showmem)
printf("%lX %hd %hd\n",memused, adcused, packetsready); {
printf ("%lX %hd %hd\n", memused, adcused, packetsready);
continue; continue;
} }
index = 0; index = 0;
//While there is still more data in the packet, process it //While there is still more data in the packet, process it
//use the destination structure to load the line before printing //use the destination structure to load the line before printing
while(charsread > charsprocessed) { while (charsread > charsprocessed)
datapoint = ntohs(buf.data[index]); {
if(destination[alignment].numCopies != 0) { datapoint = ntohs (buf.data[index]);
switch(convert) { if (destination[alignment].numCopies != 0)
{
switch (convert)
{
case CONVERT_VOLTS: case CONVERT_VOLTS:
if(alignment <= 5) { if (alignment <= 5)
volts = (long double) ( datapoint / 32767.0 ) * ((precision & 0x01) ? 5.0 : 10.0); {
} else { volts =
volts = (long double) (datapoint / 32767.0 ) * ((precision & 0x02) ? 5.0 : 10.0); (long double) (datapoint / 32767.0) *
((precision & 0x01) ? 5.0 : 10.0);
}
else
{
volts =
(long double) (datapoint / 32767.0) *
((precision & 0x02) ? 5.0 : 10.0);
} }
for(i = 0; i < destination[alignment].numCopies; i++) { for (i = 0; i < destination[alignment].numCopies; i++)
{
voltline[destination[alignment].destlist[i]] = volts; voltline[destination[alignment].destlist[i]] = volts;
} }
break; break;
default: default:
case CONVERT_HEX: case CONVERT_HEX:
case CONVERT_DEC: case CONVERT_DEC:
for(i = 0; i < destination[alignment].numCopies; i++) { for (i = 0; i < destination[alignment].numCopies; i++)
{
dataline[destination[alignment].destlist[i]] = dataline[destination[alignment].destlist[i]] =
(unsigned short) (datapoint - INT16_MIN); (unsigned short) (datapoint - INT16_MIN);
} }
...@@ -350,47 +415,59 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis ...@@ -350,47 +415,59 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis
alignment++; alignment++;
//Since channel data is packed, we need to know when to insert a newline //Since channel data is packed, we need to know when to insert a newline
if(alignment == numChannelsSampled){ if (alignment == numChannelsSampled)
if(linesdumped != 0){ {
switch(convert) { if (linesdumped != 0)
{
switch (convert)
{
case CONVERT_VOLTS: case CONVERT_VOLTS:
for(i = 0; i < numChannels; i++) { for (i = 0; i < numChannels; i++)
if (printf("%Lf ",voltline[i]) < 0) {
if (printf ("%Lf ", voltline[i]) < 0)
goto bad; goto bad;
} }
break; break;
case CONVERT_HEX: case CONVERT_HEX:
for(i = 0; i < numChannels; i++) { for (i = 0; i < numChannels; i++)
if (printf("%04hX",dataline[i]) < 0) {
if (printf ("%04hX", dataline[i]) < 0)
goto bad; goto bad;
} }
break; break;
default: default:
case CONVERT_DEC: case CONVERT_DEC:
for(i = 0; i < numChannels; i++) { for (i = 0; i < numChannels; i++)
if (printf("%hu ",dataline[i]) < 0) {
if (printf ("%hu ", dataline[i]) < 0)
goto bad; goto bad;
} }
break; break;
} }
if(printf("\n") < 0) if (printf ("\n") < 0)
goto bad; goto bad;
} else { }
else
{
linesdumped = linesdumped + 1; linesdumped = linesdumped + 1;
if(lines != 0) { if (lines != 0)
{
linesleft++; linesleft++;
} }
} }
alignment = 0; alignment = 0;
numgroups++; numgroups++;
if(lines != 0) { if (lines != 0)
{
linesleft--; linesleft--;
if(linesleft == 0) { if (linesleft == 0)
{
return 0; return 0;
} }
} }
//If numgroups so far is equal to the numGroups in a packet, this packet is done //If numgroups so far is equal to the numGroups in a packet, this packet is done
if(numgroups == numGroups) { if (numgroups == numGroups)
{
break; break;
} }
} }
...@@ -402,51 +479,59 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis ...@@ -402,51 +479,59 @@ int nerd_data_stream(int data_fd, int numChannels, int *channel_list, int precis
return 0; return 0;
bad: bad:
info("Output error (disk full?)\n"); info ("Output error (disk full?)\n");
return -1; return -1;
} }
int nerd_open(const char *address,int port) { int
nerd_open (const char *address, int port)
{
struct hostent *he; struct hostent *he;
net_init(); net_init ();
int32_t i32SocketFD = socket(PF_INET, SOCK_STREAM, 0); int32_t i32SocketFD = socket (PF_INET, SOCK_STREAM, 0);
if(-1 == i32SocketFD) if (-1 == i32SocketFD)
{ {
verb("cannot create socket"); verb ("cannot create socket");
return -1; return -1;
} }
/* Set nonblocking */ /* Set nonblocking */
if (soblock(i32SocketFD, 0) < 0) { if (soblock (i32SocketFD, 0) < 0)
verb("can't set nonblocking\n"); {
verb ("can't set nonblocking\n");
return -1; return -1;
} }
struct sockaddr_in stSockAddr; struct sockaddr_in stSockAddr;
memset(&stSockAddr, 0, sizeof(stSockAddr)); memset (&stSockAddr, 0, sizeof (stSockAddr));
stSockAddr.sin_family = AF_INET; stSockAddr.sin_family = AF_INET;
stSockAddr.sin_port = htons(port); stSockAddr.sin_port = htons (port);
he = gethostbyname(address); he = gethostbyname (address);
if (he == NULL) { if (he == NULL)
verb("gethostbyname(\"%s\") failed\n", address); {
verb ("gethostbyname(\"%s\") failed\n", address);
return -1; return -1;
} }
stSockAddr.sin_addr = *((struct in_addr *) he->h_addr); stSockAddr.sin_addr = *((struct in_addr *) he->h_addr);
debug("Resolved %s -> %s\n", address, inet_ntoa(stSockAddr.sin_addr)); debug ("Resolved %s -> %s\n", address, inet_ntoa (stSockAddr.sin_addr));
/* Connect */ /* Connect */
if (connect_timeout(i32SocketFD, (struct sockaddr *) &stSockAddr, sizeof(stSockAddr), if (connect_timeout
& (struct timeval) { .tv_sec = NERDJACK_TIMEOUT }) < 0) { (i32SocketFD, (struct sockaddr *) &stSockAddr, sizeof (stSockAddr),
verb("connection to %s:%d failed: %s\n", &(struct timeval)
inet_ntoa(stSockAddr.sin_addr), port, compat_strerror(errno)); {
.tv_sec = NERDJACK_TIMEOUT}) < 0)
{
verb ("connection to %s:%d failed: %s\n",
inet_ntoa (stSockAddr.sin_addr), port, compat_strerror (errno));
return -1; return -1;
} }
...@@ -454,13 +539,16 @@ int nerd_open(const char *address,int port) { ...@@ -454,13 +539,16 @@ int nerd_open(const char *address,int port) {
} }
//Generate an appropriate sample initiation command //Generate an appropriate sample initiation command
int nerd_generate_command(getPacket * command, int * channel_list, int channel_count, int precision, int
unsigned long period) { nerd_generate_command (getPacket * command, int *channel_list,
int channel_count, int precision, unsigned long period)
{
short channelbit = 0; short channelbit = 0;
int i; int i;
for( i = 0; i < channel_count; i++) { for (i = 0; i < channel_count; i++)
{
channelbit = channelbit | (0x1 << channel_list[i]); channelbit = channelbit | (0x1 << channel_list[i]);
} }
...@@ -469,9 +557,9 @@ int nerd_generate_command(getPacket * command, int * channel_list, int channel_c ...@@ -469,9 +557,9 @@ int nerd_generate_command(getPacket * command, int * channel_list, int channel_c
command->word[1] = 'E'; command->word[1] = 'E';
command->word[2] = 'T'; command->word[2] = 'T';
command->word[3] = 'D'; command->word[3] = 'D';
command->channelbit = htons(channelbit); command->channelbit = htons (channelbit);
command->precision = precision; command->precision = precision;
command->period = htonl(period); command->period = htonl (period);
command->prescaler = 0; command->prescaler = 0;
//sprintf(command,"GETD%3.3X%d%5.5d", channelbit,precision,period); //sprintf(command,"GETD%3.3X%d%5.5d", channelbit,precision,period);
...@@ -480,9 +568,10 @@ int nerd_generate_command(getPacket * command, int * channel_list, int channel_c ...@@ -480,9 +568,10 @@ int nerd_generate_command(getPacket * command, int * channel_list, int channel_c
} }
int nerd_close_conn(int data_fd) int
nerd_close_conn (int data_fd)
{ {
shutdown(data_fd, 2); shutdown (data_fd, 2);
close(data_fd); close (data_fd);
return 0; return 0;
} }
...@@ -24,7 +24,8 @@ ...@@ -24,7 +24,8 @@
#define NERDJACK_PACKET_SIZE 1460 #define NERDJACK_PACKET_SIZE 1460
#define NERDJACK_NUM_SAMPLES 724 #define NERDJACK_NUM_SAMPLES 724
typedef struct __attribute__((__packed__)) { typedef struct __attribute__ ((__packed__))
{
char word[4]; char word[4];
unsigned short channelbit; unsigned short channelbit;
unsigned char precision; unsigned char precision;
...@@ -33,24 +34,28 @@ typedef struct __attribute__((__packed__)) { ...@@ -33,24 +34,28 @@ typedef struct __attribute__((__packed__)) {
} getPacket; } getPacket;
/* Open/close TCP/IP connection to the NerdJack */ /* Open/close TCP/IP connection to the NerdJack */
int nerd_open(const char *address,int port); int nerd_open (const char *address, int port);
int nerd_close_conn(int data_fd); int nerd_close_conn (int data_fd);
/* Generate the command word for the NerdJack */ /* Generate the command word for the NerdJack */
int nerd_generate_command(getPacket * command, int * channel_list, int channel_count, int precision, int nerd_generate_command (getPacket * command, int *channel_list,
int channel_count, int precision,
unsigned long period); unsigned long period);
/* Send given command to NerdJack */ /* Send given command to NerdJack */
int nerd_send_command(const char * address, void * command, int length); int nerd_send_command (const char *address, void *command, int length);
/* Stream data out of the NerdJack */ /* Stream data out of the NerdJack */
int nerd_data_stream(int data_fd, int numChannels, int * channel_list, int precision, int convert, int lines, int showmem, unsigned short * currentcount, unsigned int period); int nerd_data_stream (int data_fd, int numChannels, int *channel_list,
int precision, int convert, int lines, int showmem,
unsigned short *currentcount, unsigned int period);
/* Detect the IP Address of the NerdJack and return in ipAddress */ /* Detect the IP Address of the NerdJack and return in ipAddress */
int nerdjack_detect(char * ipAddress); int nerdjack_detect (char *ipAddress);
/* Choose the best ScanConfig and ScanInterval parameters for the /* Choose the best ScanConfig and ScanInterval parameters for the
desired scanrate. Returns -1 if no valid config found */ desired scanrate. Returns -1 if no valid config found */
int nerdjack_choose_scan(double desired_rate, double *actual_rate, unsigned long *period); int nerdjack_choose_scan (double desired_rate, double *actual_rate,
unsigned long *period);
#endif #endif
...@@ -5,28 +5,31 @@ ...@@ -5,28 +5,31 @@
#include <stdio.h> #include <stdio.h>
/* Initialize networking */ /* Initialize networking */
void net_init(void) void
net_init (void)
{ {
#ifdef __WIN32__ #ifdef __WIN32__
WSADATA blah; WSADATA blah;
WSAStartup(0x0101, &blah); WSAStartup (0x0101, &blah);
#endif #endif
} }
/* Set socket blocking/nonblocking */ /* Set socket blocking/nonblocking */
int soblock(int socket, int blocking) int
soblock (int socket, int blocking)
{ {
#ifdef __WIN32__ #ifdef __WIN32__
unsigned long arg = blocking ? 0 : 1; unsigned long arg = blocking ? 0 : 1;
if (ioctlsocket(socket, FIONBIO, &arg) != 0) if (ioctlsocket (socket, FIONBIO, &arg) != 0)
return -1; return -1;
return 0; return 0;
#else #else
int sockopt; int sockopt;
/* Get flags */ /* Get flags */
sockopt = fcntl(socket, F_GETFL); sockopt = fcntl (socket, F_GETFL);
if (sockopt == -1) { if (sockopt == -1)
{
return -1; return -1;
} }
...@@ -37,7 +40,7 @@ int soblock(int socket, int blocking) ...@@ -37,7 +40,7 @@ int soblock(int socket, int blocking)
sockopt |= O_NONBLOCK; sockopt |= O_NONBLOCK;
/* Set flags */ /* Set flags */
if (fcntl(socket, F_SETFL, sockopt) != 0) if (fcntl (socket, F_SETFL, sockopt) != 0)
return -1; return -1;
return 0; return 0;
...@@ -46,7 +49,8 @@ int soblock(int socket, int blocking) ...@@ -46,7 +49,8 @@ int soblock(int socket, int blocking)
/* Like connect(2), but with a timeout. Socket must be non-blocking. */ /* Like connect(2), but with a timeout. Socket must be non-blocking. */
int connect_timeout(int s, const struct sockaddr *serv_addr, socklen_t addrlen, int
connect_timeout (int s, const struct sockaddr *serv_addr, socklen_t addrlen,
struct timeval *timeout) struct timeval *timeout)
{ {
int ret; int ret;
...@@ -56,16 +60,17 @@ int connect_timeout(int s, const struct sockaddr *serv_addr, socklen_t addrlen, ...@@ -56,16 +60,17 @@ int connect_timeout(int s, const struct sockaddr *serv_addr, socklen_t addrlen,
socklen_t optlen; socklen_t optlen;
/* Start connect */ /* Start connect */
ret = connect(s, serv_addr, addrlen); ret = connect (s, serv_addr, addrlen);
if (ret == 0) { if (ret == 0)
{
/* Success */ /* Success */
return 0; return 0;
} }
/* Check for immediate failure */ /* Check for immediate failure */
#ifdef __WIN32__ #ifdef __WIN32__
errno = WSAGetLastError(); errno = WSAGetLastError ();
if (ret < 0 && errno != WSAEWOULDBLOCK && errno != WSAEINVAL) if (ret < 0 && errno != WSAEWOULDBLOCK && errno != WSAEINVAL)
return -1; return -1;
#else #else
...@@ -74,27 +79,30 @@ int connect_timeout(int s, const struct sockaddr *serv_addr, socklen_t addrlen, ...@@ -74,27 +79,30 @@ int connect_timeout(int s, const struct sockaddr *serv_addr, socklen_t addrlen,
#endif #endif
/* In progress, wait for result. */ /* In progress, wait for result. */
FD_ZERO(&writefds); FD_ZERO (&writefds);
FD_SET(s, &writefds); FD_SET (s, &writefds);
FD_ZERO(&exceptfds); FD_ZERO (&exceptfds);
FD_SET(s, &exceptfds); FD_SET (s, &exceptfds);
ret = select(s + 1, NULL, &writefds, &exceptfds, timeout); ret = select (s + 1, NULL, &writefds, &exceptfds, timeout);
if (ret < 0) { if (ret < 0)
{
/* Error */ /* Error */
return -1; return -1;
} }
if (ret == 0) { if (ret == 0)
{
/* Timed out */ /* Timed out */
errno = ETIMEDOUT; errno = ETIMEDOUT;
return -1; return -1;
} }
/* Check the socket state */ /* Check the socket state */
optlen = sizeof(optval); optlen = sizeof (optval);
if (getsockopt(s, SOL_SOCKET, SO_ERROR, (void *)&optval, &optlen) != 0) if (getsockopt (s, SOL_SOCKET, SO_ERROR, (void *) &optval, &optlen) != 0)
return -1; return -1;
if (optval != 0) { if (optval != 0)
{
/* Connection failed. */ /* Connection failed. */
errno = optval; errno = optval;
return -1; return -1;
...@@ -102,7 +110,8 @@ int connect_timeout(int s, const struct sockaddr *serv_addr, socklen_t addrlen, ...@@ -102,7 +110,8 @@ int connect_timeout(int s, const struct sockaddr *serv_addr, socklen_t addrlen,
/* On Windows, SO_ERROR sometimes shows no error but the connection /* On Windows, SO_ERROR sometimes shows no error but the connection
still failed. Sigh. */ still failed. Sigh. */
if (FD_ISSET(s, &exceptfds) || !FD_ISSET(s, &writefds)) { if (FD_ISSET (s, &exceptfds) || !FD_ISSET (s, &writefds))
{
errno = EIO; errno = EIO;
return -1; return -1;
} }
...@@ -114,92 +123,104 @@ int connect_timeout(int s, const struct sockaddr *serv_addr, socklen_t addrlen, ...@@ -114,92 +123,104 @@ int connect_timeout(int s, const struct sockaddr *serv_addr, socklen_t addrlen,
/* Like send(2), but with a timeout. Socket must be non-blocking. /* Like send(2), but with a timeout. Socket must be non-blocking.
The timeout only applies if no data at all is sent -- this function The timeout only applies if no data at all is sent -- this function
may still send less than requested. */ may still send less than requested. */
ssize_t send_timeout(int s, const void *buf, size_t len, int flags, ssize_t
struct timeval *timeout) send_timeout (int s, const void *buf, size_t len, int flags,
struct timeval * timeout)
{ {
fd_set writefds; fd_set writefds;
int ret; int ret;
FD_ZERO(&writefds); FD_ZERO (&writefds);
FD_SET(s, &writefds); FD_SET (s, &writefds);
ret = select(s + 1, NULL, &writefds, NULL, timeout); ret = select (s + 1, NULL, &writefds, NULL, timeout);
if (ret == 0) { if (ret == 0)
{
/* Timed out */ /* Timed out */
errno = ETIMEDOUT; errno = ETIMEDOUT;
return -1; return -1;
} }
if (ret != 1) { if (ret != 1)
{
/* Error */ /* Error */
return -1; return -1;
} }
return send(s, buf, len, flags); return send (s, buf, len, flags);
} }
/* Like recv(2), but with a timeout. Socket must be non-blocking. /* Like recv(2), but with a timeout. Socket must be non-blocking.
The timeout only applies if no data at all is received -- this The timeout only applies if no data at all is received -- this
function may still return less than requested. */ function may still return less than requested. */
ssize_t recv_timeout(int s, void *buf, size_t len, int flags, ssize_t
struct timeval *timeout) recv_timeout (int s, void *buf, size_t len, int flags,
struct timeval * timeout)
{ {
fd_set readfds; fd_set readfds;
int ret; int ret;
FD_ZERO(&readfds); FD_ZERO (&readfds);
FD_SET(s, &readfds); FD_SET (s, &readfds);
ret = select(s + 1, &readfds, NULL, NULL, timeout); ret = select (s + 1, &readfds, NULL, NULL, timeout);
if (ret == 0) { if (ret == 0)
{
/* Timed out */ /* Timed out */
errno = ETIMEDOUT; errno = ETIMEDOUT;
return -1; return -1;
} }
if (ret != 1) { if (ret != 1)
{
/* Error */ /* Error */
return -1; return -1;
} }
return recv(s, buf, len, flags); return recv (s, buf, len, flags);
} }
/* Like recvfrom(2), but with a timeout. Socket must be non-blocking. /* Like recvfrom(2), but with a timeout. Socket must be non-blocking.
The timeout only applies if no data at all is received -- this The timeout only applies if no data at all is received -- this
function may still return less than requested. */ function may still return less than requested. */
ssize_t recvfrom_timeout(int s, void *buf, size_t len, int flags, struct sockaddr *address, socklen_t *address_len, ssize_t
struct timeval *timeout) recvfrom_timeout (int s, void *buf, size_t len, int flags,
struct sockaddr * address, socklen_t * address_len,
struct timeval * timeout)
{ {
fd_set readfds; fd_set readfds;
int ret; int ret;
FD_ZERO(&readfds); FD_ZERO (&readfds);
FD_SET(s, &readfds); FD_SET (s, &readfds);
ret = select(s + 1, &readfds, NULL, NULL, timeout); ret = select (s + 1, &readfds, NULL, NULL, timeout);
if (ret == 0) { if (ret == 0)
{
/* Timed out */ /* Timed out */
errno = ETIMEDOUT; errno = ETIMEDOUT;
return -1; return -1;
} }
if (ret != 1) { if (ret != 1)
{
/* Error */ /* Error */
return -1; return -1;
} }
return recvfrom(s, buf, len, flags, address, address_len); return recvfrom (s, buf, len, flags, address, address_len);
} }
/* Like send_timeout, but retries (with the same timeout) in case of /* Like send_timeout, but retries (with the same timeout) in case of
partial transfers. This is a stronger attempt to send all partial transfers. This is a stronger attempt to send all
requested data. */ requested data. */
ssize_t send_all_timeout(int s, const void *buf, size_t len, int flags, ssize_t
struct timeval *timeout) send_all_timeout (int s, const void *buf, size_t len, int flags,
struct timeval * timeout)
{ {
struct timeval tv; struct timeval tv;
size_t left = len; size_t left = len;
ssize_t ret; ssize_t ret;
while (left > 0) { while (left > 0)
{
tv.tv_sec = timeout->tv_sec; tv.tv_sec = timeout->tv_sec;
tv.tv_usec = timeout->tv_usec; tv.tv_usec = timeout->tv_usec;
ret = send_timeout(s, buf, left, flags, &tv); ret = send_timeout (s, buf, left, flags, &tv);
if (ret < 0) if (ret < 0)
return ret; return ret;
...@@ -217,17 +238,19 @@ ssize_t send_all_timeout(int s, const void *buf, size_t len, int flags, ...@@ -217,17 +238,19 @@ ssize_t send_all_timeout(int s, const void *buf, size_t len, int flags,
/* Like recv_timeout, but retries (with the same timeout) in case of /* Like recv_timeout, but retries (with the same timeout) in case of
partial transfers. This is a stronger attempt to recv all partial transfers. This is a stronger attempt to recv all
requested data. */ requested data. */
ssize_t recv_all_timeout(int s, void *buf, size_t len, int flags, ssize_t
struct timeval *timeout) recv_all_timeout (int s, void *buf, size_t len, int flags,
struct timeval * timeout)
{ {
struct timeval tv; struct timeval tv;
size_t left = len; size_t left = len;
ssize_t ret; ssize_t ret;
while (left > 0) { while (left > 0)
{
tv.tv_sec = timeout->tv_sec; tv.tv_sec = timeout->tv_sec;
tv.tv_usec = timeout->tv_usec; tv.tv_usec = timeout->tv_usec;
ret = recv_timeout(s, buf, left, flags, &tv); ret = recv_timeout (s, buf, left, flags, &tv);
if (ret < 0) if (ret < 0)
return ret; return ret;
......
...@@ -19,27 +19,28 @@ ...@@ -19,27 +19,28 @@
#endif #endif
/* Initialize networking */ /* Initialize networking */
void net_init(void); void net_init (void);
/* Set socket blocking/nonblocking */ /* Set socket blocking/nonblocking */
int soblock(int socket, int blocking); int soblock (int socket, int blocking);
/* Like send(2), recv(2), connect(2), but with timeouts. /* Like send(2), recv(2), connect(2), but with timeouts.
Socket must be O_NONBLOCK. */ Socket must be O_NONBLOCK. */
int connect_timeout(int s, const struct sockaddr *serv_addr, socklen_t addrlen, int connect_timeout (int s, const struct sockaddr *serv_addr,
socklen_t addrlen, struct timeval *timeout);
ssize_t send_timeout (int s, const void *buf, size_t len, int flags,
struct timeval *timeout); struct timeval *timeout);
ssize_t send_timeout(int s, const void *buf, size_t len, int flags, ssize_t recv_timeout (int s, void *buf, size_t len, int flags,
struct timeval *timeout); struct timeval *timeout);
ssize_t recv_timeout(int s, void *buf, size_t len, int flags, ssize_t recvfrom_timeout (int s, void *buf, size_t len, int flags,
struct timeval *timeout); struct sockaddr *address, socklen_t * address_len,
ssize_t recvfrom_timeout(int s, void *buf, size_t len, int flags, struct sockaddr *address, socklen_t *address_len,
struct timeval *timeout); struct timeval *timeout);
/* Like send_timeout and recv_timeout, but they retry (with the same timeout) /* Like send_timeout and recv_timeout, but they retry (with the same timeout)
in case of partial transfers, in order to try to transfer all data. */ in case of partial transfers, in order to try to transfer all data. */
ssize_t send_all_timeout(int s, const void *buf, size_t len, int flags, ssize_t send_all_timeout (int s, const void *buf, size_t len, int flags,
struct timeval *timeout); struct timeval *timeout);
ssize_t recv_all_timeout(int s, void *buf, size_t len, int flags, ssize_t recv_all_timeout (int s, void *buf, size_t len, int flags,
struct timeval *timeout); struct timeval *timeout);
#endif #endif
...@@ -11,85 +11,98 @@ ...@@ -11,85 +11,98 @@
#include <string.h> #include <string.h>
#include "opt.h" #include "opt.h"
void opt_init(int *optind) { void
*optind=0; opt_init (int *optind)
{
*optind = 0;
} }
char opt_parse(int argc, char **argv, int *optind, char **optarg, char
struct options *opt) { opt_parse (int argc, char **argv, int *optind, char **optarg,
struct options *opt)
{
char c; char c;
int i; int i;
(*optind)++; (*optind)++;
if(*optind>=argc) if (*optind >= argc)
return 0; return 0;
if(argv[*optind][0]=='-' && if (argv[*optind][0] == '-' &&
argv[*optind][1]!='-' && argv[*optind][1] != '-' && argv[*optind][1] != 0)
argv[*optind][1]!=0) { {
/* Short option (or a bunch of 'em) */ /* Short option (or a bunch of 'em) */
/* Save this and shift others over */ /* Save this and shift others over */
c=argv[*optind][1]; c = argv[*optind][1];
for(i=2;argv[*optind][i]!=0;i++) for (i = 2; argv[*optind][i] != 0; i++)
argv[*optind][i-1]=argv[*optind][i]; argv[*optind][i - 1] = argv[*optind][i];
argv[*optind][i-1]=0; argv[*optind][i - 1] = 0;
if(argv[*optind][1]!=0) if (argv[*optind][1] != 0)
(*optind)--; (*optind)--;
/* Now find it */ /* Now find it */
for(i=0;opt[i].shortopt!=0;i++) for (i = 0; opt[i].shortopt != 0; i++)
if(opt[i].shortopt==c) if (opt[i].shortopt == c)
break; break;
if(opt[i].shortopt==0) { if (opt[i].shortopt == 0)
fprintf(stderr,"Error: unknown option '-%c'\n",c); {
fprintf (stderr, "Error: unknown option '-%c'\n", c);
return '?'; return '?';
} }
if(opt[i].arg==NULL) if (opt[i].arg == NULL)
return c; return c;
(*optind)++; (*optind)++;
if(*optind>=argc || (argv[*optind][0]=='-' && if (*optind >= argc || (argv[*optind][0] == '-' &&
argv[*optind][1]!=0)) { argv[*optind][1] != 0))
fprintf(stderr,"Error: option '-%c' requires an " {
"argument\n",c); fprintf (stderr, "Error: option '-%c' requires an "
"argument\n", c);
return '?'; return '?';
} }
(*optarg)=argv[*optind]; (*optarg) = argv[*optind];
return c; return c;
} else if(argv[*optind][0]=='-' && }
argv[*optind][1]=='-' && else if (argv[*optind][0] == '-' &&
argv[*optind][2]!=0) { argv[*optind][1] == '-' && argv[*optind][2] != 0)
{
/* Long option */ /* Long option */
for(i=0;(c=opt[i].shortopt)!=0;i++) for (i = 0; (c = opt[i].shortopt) != 0; i++)
if(strcmp(opt[i].longopt,argv[*optind]+2)==0) if (strcmp (opt[i].longopt, argv[*optind] + 2) == 0)
break; break;
if(opt[i].shortopt==0) { if (opt[i].shortopt == 0)
fprintf(stderr,"Error: unknown option '%s'\n", {
argv[*optind]); fprintf (stderr, "Error: unknown option '%s'\n", argv[*optind]);
return '?'; return '?';
} }
if(opt[i].arg==NULL) if (opt[i].arg == NULL)
return c; return c;
(*optind)++; (*optind)++;
if(*optind>=argc || (argv[*optind][0]=='-' && if (*optind >= argc || (argv[*optind][0] == '-' &&
argv[*optind][1]!=0)) { argv[*optind][1] != 0))
fprintf(stderr,"Error: option '%s' requires an " {
"argument\n",argv[*optind-1]); fprintf (stderr, "Error: option '%s' requires an "
"argument\n", argv[*optind - 1]);
return '?'; return '?';
} }
(*optarg)=argv[*optind]; (*optarg) = argv[*optind];
return c; return c;
} else { }
else
{
/* End of options */ /* End of options */
return 0; return 0;
} }
} }
void opt_help(struct options *opt, FILE *out) { void
opt_help (struct options *opt, FILE * out)
{
int i; int i;
int printed; int printed;
for(i=0;opt[i].shortopt!=0;i++) { for (i = 0; opt[i].shortopt != 0; i++)
fprintf(out," -%c, --%s%n",opt[i].shortopt, {
opt[i].longopt,&printed); fprintf (out, " -%c, --%s%n", opt[i].shortopt,
fprintf(out," %-*s%s\n",30-printed, opt[i].longopt, &printed);
opt[i].arg?opt[i].arg:"",opt[i].help); fprintf (out, " %-*s%s\n", 30 - printed,
opt[i].arg ? opt[i].arg : "", opt[i].help);
} }
} }
...@@ -11,18 +11,19 @@ ...@@ -11,18 +11,19 @@
#include <stdlib.h> #include <stdlib.h>
struct options { struct options
{
char shortopt; char shortopt;
char *longopt; char *longopt;
char *arg; char *arg;
char *help; char *help;
}; };
void opt_init(int *optind); void opt_init (int *optind);
char opt_parse(int argc, char **argv, int *optind, char **optarg, char opt_parse (int argc, char **argv, int *optind, char **optarg,
struct options *opt); struct options *opt);
void opt_help(struct options *opt, FILE *out); void opt_help (struct options *opt, FILE * out);
#endif #endif
...@@ -28,13 +28,15 @@ ...@@ -28,13 +28,15 @@
#define UE9_TIMEOUT 5 /* Timeout for connect/send/recv, in seconds */ #define UE9_TIMEOUT 5 /* Timeout for connect/send/recv, in seconds */
/* Fill checksums in data buffers, with "normal" checksum format */ /* Fill checksums in data buffers, with "normal" checksum format */
void ue9_checksum_normal(uint8_t *buffer, size_t len) void
ue9_checksum_normal (uint8_t * buffer, size_t len)
{ {
uint16_t sum = 0; uint16_t sum = 0;
if (len < 1) { if (len < 1)
fprintf(stderr, "ue9_checksum_normal: len too short\n"); {
exit(1); fprintf (stderr, "ue9_checksum_normal: len too short\n");
exit (1);
} }
while (--len >= 1) while (--len >= 1)
...@@ -45,13 +47,15 @@ void ue9_checksum_normal(uint8_t *buffer, size_t len) ...@@ -45,13 +47,15 @@ void ue9_checksum_normal(uint8_t *buffer, size_t len)
} }
/* Fill checksums in data buffers, with "extended" checksum format */ /* Fill checksums in data buffers, with "extended" checksum format */
void ue9_checksum_extended(uint8_t *buffer, size_t len) void
ue9_checksum_extended (uint8_t * buffer, size_t len)
{ {
uint16_t sum = 0; uint16_t sum = 0;
if (len < 6) { if (len < 6)
fprintf(stderr, "ue9_checksum_extended: len too short\n"); {
exit(1); fprintf (stderr, "ue9_checksum_extended: len too short\n");
exit (1);
} }
/* 16-bit extended checksum */ /* 16-bit extended checksum */
...@@ -61,27 +65,29 @@ void ue9_checksum_extended(uint8_t *buffer, size_t len) ...@@ -61,27 +65,29 @@ void ue9_checksum_extended(uint8_t *buffer, size_t len)
buffer[5] = (uint8_t) (sum >> 8); buffer[5] = (uint8_t) (sum >> 8);
/* 8-bit normal checksum over first 6 bytes */ /* 8-bit normal checksum over first 6 bytes */
ue9_checksum_normal(buffer, 6); ue9_checksum_normal (buffer, 6);
} }
/* Verify checksums in data buffers, with "normal" checksum format. */ /* Verify checksums in data buffers, with "normal" checksum format. */
int ue9_verify_normal(uint8_t *buffer, size_t len) int
ue9_verify_normal (uint8_t * buffer, size_t len)
{ {
uint8_t saved, new; uint8_t saved, new;
if (len < 1) { if (len < 1)
fprintf(stderr, "ue9_verify_normal: len too short\n"); {
exit(1); fprintf (stderr, "ue9_verify_normal: len too short\n");
exit (1);
} }
saved = buffer[0]; saved = buffer[0];
ue9_checksum_normal(buffer, len); ue9_checksum_normal (buffer, len);
new = buffer[0]; new = buffer[0];
buffer[0] = saved; buffer[0] = saved;
if (new != saved) { if (new != saved)
verb("got %02x, expected %02x\n", {
saved, new); verb ("got %02x, expected %02x\n", saved, new);
return 0; return 0;
} }
...@@ -89,19 +95,21 @@ int ue9_verify_normal(uint8_t *buffer, size_t len) ...@@ -89,19 +95,21 @@ int ue9_verify_normal(uint8_t *buffer, size_t len)
} }
/* Verify checksums in data buffers, with "extended" checksum format. */ /* Verify checksums in data buffers, with "extended" checksum format. */
int ue9_verify_extended(uint8_t *buffer, size_t len) int
ue9_verify_extended (uint8_t * buffer, size_t len)
{ {
uint8_t saved[3], new[3]; uint8_t saved[3], new[3];
if (len < 6) { if (len < 6)
fprintf(stderr, "ue9_verify_extended: len too short\n"); {
exit(1); fprintf (stderr, "ue9_verify_extended: len too short\n");
exit (1);
} }
saved[0] = buffer[0]; saved[0] = buffer[0];
saved[1] = buffer[4]; saved[1] = buffer[4];
saved[2] = buffer[5]; saved[2] = buffer[5];
ue9_checksum_extended(buffer, len); ue9_checksum_extended (buffer, len);
new[0] = buffer[0]; new[0] = buffer[0];
new[1] = buffer[4]; new[1] = buffer[4];
new[2] = buffer[5]; new[2] = buffer[5];
...@@ -109,10 +117,9 @@ int ue9_verify_extended(uint8_t *buffer, size_t len) ...@@ -109,10 +117,9 @@ int ue9_verify_extended(uint8_t *buffer, size_t len)
buffer[4] = saved[1]; buffer[4] = saved[1];
buffer[5] = saved[2]; buffer[5] = saved[2];
if (saved[0] != new[0] || if (saved[0] != new[0] || saved[1] != new[1] || saved[2] != new[2])
saved[1] != new[1] || {
saved[2] != new[2]) { verb ("got %02x %02x %02x, expected %02x %02x %02x\n",
verb("got %02x %02x %02x, expected %02x %02x %02x\n",
saved[0], saved[1], saved[2], new[0], new[1], new[2]); saved[0], saved[1], saved[2], new[0], new[1], new[2]);
return 0; return 0;
} }
...@@ -121,41 +128,54 @@ int ue9_verify_extended(uint8_t *buffer, size_t len) ...@@ -121,41 +128,54 @@ int ue9_verify_extended(uint8_t *buffer, size_t len)
} }
/* Data conversion. If calib is NULL, use uncalibrated conversions. */ /* Data conversion. If calib is NULL, use uncalibrated conversions. */
double ue9_binary_to_analog(struct ue9Calibration *calib, double
ue9_binary_to_analog (struct ue9Calibration *calib,
uint8_t gain, uint8_t resolution, uint16_t data) uint8_t gain, uint8_t resolution, uint16_t data)
{ {
double slope = 0, offset; double slope = 0, offset;
if (calib == NULL) { if (calib == NULL)
{
double uncal[9] = { 5.08, 2.54, 1.27, 0.63, 0, 0, 0, 0, 10.25 }; double uncal[9] = { 5.08, 2.54, 1.27, 0.63, 0, 0, 0, 0, 10.25 };
if (gain >= ARRAY_SIZE(uncal) || uncal[gain] == 0) { if (gain >= ARRAY_SIZE (uncal) || uncal[gain] == 0)
fprintf(stderr, "ue9_binary_to_analog: bad gain\n"); {
exit(1); fprintf (stderr, "ue9_binary_to_analog: bad gain\n");
exit (1);
} }
return data * uncal[gain] / 65536.0; return data * uncal[gain] / 65536.0;
} }
if (resolution < 18) { if (resolution < 18)
if (gain <= 3) { {
if (gain <= 3)
{
slope = calib->unipolarSlope[gain]; slope = calib->unipolarSlope[gain];
offset = calib->unipolarOffset[gain]; offset = calib->unipolarOffset[gain];
} else if (gain == 8) { }
else if (gain == 8)
{
slope = calib->bipolarSlope; slope = calib->bipolarSlope;
offset = calib->bipolarOffset; offset = calib->bipolarOffset;
} }
} else { }
if (gain == 0) { else
{
if (gain == 0)
{
slope = calib->hiResUnipolarSlope; slope = calib->hiResUnipolarSlope;
offset = calib->hiResUnipolarOffset; offset = calib->hiResUnipolarOffset;
} else if (gain == 8) { }
else if (gain == 8)
{
slope = calib->hiResBipolarSlope; slope = calib->hiResBipolarSlope;
offset = calib->hiResBipolarOffset; offset = calib->hiResBipolarOffset;
} }
} }
if (slope == 0) { if (slope == 0)
fprintf(stderr, "ue9_binary_to_analog: bad gain\n"); {
exit(1); fprintf (stderr, "ue9_binary_to_analog: bad gain\n");
exit (1);
} }
return data * slope + offset; return data * slope + offset;
...@@ -165,7 +185,8 @@ double ue9_binary_to_analog(struct ue9Calibration *calib, ...@@ -165,7 +185,8 @@ double ue9_binary_to_analog(struct ue9Calibration *calib,
checksums on the outgoing packets, and verifies them on the checksums on the outgoing packets, and verifies them on the
incoming packets. Data in "out" is transmitted, data in "in" is incoming packets. Data in "out" is transmitted, data in "in" is
received. */ received. */
int ue9_command(int fd, uint8_t *out, uint8_t *in, int inlen) int
ue9_command (int fd, uint8_t * out, uint8_t * in, int inlen)
{ {
int extended = 0, outlen; int extended = 0, outlen;
uint8_t saved_1, saved_3; uint8_t saved_1, saved_3;
...@@ -175,19 +196,24 @@ int ue9_command(int fd, uint8_t *out, uint8_t *in, int inlen) ...@@ -175,19 +196,24 @@ int ue9_command(int fd, uint8_t *out, uint8_t *in, int inlen)
extended = 1; extended = 1;
/* Figure out length of data payload, and fill checksums. */ /* Figure out length of data payload, and fill checksums. */
if (extended) { if (extended)
{
outlen = 6 + (out[2]) * 2; outlen = 6 + (out[2]) * 2;
ue9_checksum_extended(out, outlen); ue9_checksum_extended (out, outlen);
} else { }
else
{
outlen = 2 + (out[1] & 7) * 2; outlen = 2 + (out[1] & 7) * 2;
ue9_checksum_normal(out, outlen); ue9_checksum_normal (out, outlen);
} }
/* Send request */ /* Send request */
ret = send_all_timeout(fd, out, outlen, 0, ret = send_all_timeout (fd, out, outlen, 0, &(struct timeval)
& (struct timeval) { .tv_sec = UE9_TIMEOUT }); {
if (ret < 0 || ret != outlen) { .tv_sec = UE9_TIMEOUT});
verb("short send %d\n", (int)ret); if (ret < 0 || ret != outlen)
{
verb ("short send %d\n", (int) ret);
return -1; return -1;
} }
...@@ -198,26 +224,28 @@ int ue9_command(int fd, uint8_t *out, uint8_t *in, int inlen) ...@@ -198,26 +224,28 @@ int ue9_command(int fd, uint8_t *out, uint8_t *in, int inlen)
saved_3 = out[3]; saved_3 = out[3];
/* Receive result */ /* Receive result */
ret = recv_all_timeout(fd, in, inlen, 0, ret = recv_all_timeout (fd, in, inlen, 0, &(struct timeval)
& (struct timeval) { .tv_sec = UE9_TIMEOUT }); {
if (ret < 0 || ret != inlen) { .tv_sec = UE9_TIMEOUT});
verb("short recv %d\n", (int)ret); if (ret < 0 || ret != inlen)
{
verb ("short recv %d\n", (int) ret);
return -1; return -1;
} }
/* Verify it */ /* Verify it */
if ((in[1] & 0xF8) != (saved_1 & 0xF8)) if ((in[1] & 0xF8) != (saved_1 & 0xF8))
verb("returned command doesn't match\n"); verb ("returned command doesn't match\n");
else if (extended && (in[3] != saved_3)) else if (extended && (in[3] != saved_3))
verb("extended command doesn't match\n"); verb ("extended command doesn't match\n");
else if (extended && (inlen != (6 + (in[2]) * 2))) else if (extended && (inlen != (6 + (in[2]) * 2)))
verb("returned extended data is the wrong len\n"); verb ("returned extended data is the wrong len\n");
else if (!extended && (inlen != (2 + (in[1] & 7) * 2))) else if (!extended && (inlen != (2 + (in[1] & 7) * 2)))
verb("returned data is the wrong len\n"); verb ("returned data is the wrong len\n");
else if (extended && !ue9_verify_extended(in, inlen)) else if (extended && !ue9_verify_extended (in, inlen))
verb("extended checksum is invalid\n"); verb ("extended checksum is invalid\n");
else if (!ue9_verify_normal(in, extended ? 6 : inlen)) else if (!ue9_verify_normal (in, extended ? 6 : inlen))
verb("normal checksum is invalid\n"); verb ("normal checksum is invalid\n");
else else
return 0; /* looks good */ return 0; /* looks good */
...@@ -226,13 +254,15 @@ int ue9_command(int fd, uint8_t *out, uint8_t *in, int inlen) ...@@ -226,13 +254,15 @@ int ue9_command(int fd, uint8_t *out, uint8_t *in, int inlen)
/* Read a memory block from the device. Returns -1 on error. */ /* Read a memory block from the device. Returns -1 on error. */
int ue9_memory_read(int fd, int blocknum, uint8_t *buffer, int len) int
ue9_memory_read (int fd, int blocknum, uint8_t * buffer, int len)
{ {
uint8_t sendbuf[8], recvbuf[136]; uint8_t sendbuf[8], recvbuf[136];
if (len != 128) { if (len != 128)
fprintf(stderr,"ue9_memory_read: buffer length must be 128\n"); {
exit(1); fprintf (stderr, "ue9_memory_read: buffer length must be 128\n");
exit (1);
} }
/* Request memory block */ /* Request memory block */
...@@ -242,19 +272,21 @@ int ue9_memory_read(int fd, int blocknum, uint8_t *buffer, int len) ...@@ -242,19 +272,21 @@ int ue9_memory_read(int fd, int blocknum, uint8_t *buffer, int len)
sendbuf[6] = 0x00; sendbuf[6] = 0x00;
sendbuf[7] = blocknum; sendbuf[7] = blocknum;
if (ue9_command(fd, sendbuf, recvbuf, sizeof(recvbuf)) < 0) { if (ue9_command (fd, sendbuf, recvbuf, sizeof (recvbuf)) < 0)
verb("command failed\n"); {
verb ("command failed\n");
return -1; return -1;
} }
/* Got it */ /* Got it */
memcpy(buffer, recvbuf + 8, len); memcpy (buffer, recvbuf + 8, len);
return 0; return 0;
} }
/* Convert 64-bit fixed point to double type */ /* Convert 64-bit fixed point to double type */
double ue9_fp64_to_double(uint8_t *data) double
ue9_fp64_to_double (uint8_t * data)
{ {
int32_t a; int32_t a;
uint32_t b; uint32_t b;
...@@ -262,143 +294,160 @@ double ue9_fp64_to_double(uint8_t *data) ...@@ -262,143 +294,160 @@ double ue9_fp64_to_double(uint8_t *data)
a = (data[7] << 24) | (data[6] << 16) | (data[5] << 8) | data[4]; a = (data[7] << 24) | (data[6] << 16) | (data[5] << 8) | data[4];
b = (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0]; b = (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
return (double)a + (double)b / (double)4294967296.0L; return (double) a + (double) b / (double) 4294967296.0L;
} }
/* Retrieve calibration data from the device. Returns -1 on error. */ /* Retrieve calibration data from the device. Returns -1 on error. */
int ue9_get_calibration(int fd, struct ue9Calibration *calib) int
ue9_get_calibration (int fd, struct ue9Calibration *calib)
{ {
uint8_t buf[128]; uint8_t buf[128];
/* Block 0 */ /* Block 0 */
if (ue9_memory_read(fd, 0, buf, 128) < 0) return -1; if (ue9_memory_read (fd, 0, buf, 128) < 0)
calib->unipolarSlope[0] = ue9_fp64_to_double(buf + 0); return -1;
calib->unipolarOffset[0] = ue9_fp64_to_double(buf + 8); calib->unipolarSlope[0] = ue9_fp64_to_double (buf + 0);
calib->unipolarSlope[1] = ue9_fp64_to_double(buf + 16); calib->unipolarOffset[0] = ue9_fp64_to_double (buf + 8);
calib->unipolarOffset[1] = ue9_fp64_to_double(buf + 24); calib->unipolarSlope[1] = ue9_fp64_to_double (buf + 16);
calib->unipolarSlope[2] = ue9_fp64_to_double(buf + 32); calib->unipolarOffset[1] = ue9_fp64_to_double (buf + 24);
calib->unipolarOffset[2] = ue9_fp64_to_double(buf + 40); calib->unipolarSlope[2] = ue9_fp64_to_double (buf + 32);
calib->unipolarSlope[3] = ue9_fp64_to_double(buf + 48); calib->unipolarOffset[2] = ue9_fp64_to_double (buf + 40);
calib->unipolarOffset[3] = ue9_fp64_to_double(buf + 56); calib->unipolarSlope[3] = ue9_fp64_to_double (buf + 48);
calib->unipolarOffset[3] = ue9_fp64_to_double (buf + 56);
/* Block 1 */ /* Block 1 */
if (ue9_memory_read(fd, 1, buf, 128) < 0) return -1; if (ue9_memory_read (fd, 1, buf, 128) < 0)
calib->bipolarSlope = ue9_fp64_to_double(buf + 0); return -1;
calib->bipolarOffset = ue9_fp64_to_double(buf + 8); calib->bipolarSlope = ue9_fp64_to_double (buf + 0);
calib->bipolarOffset = ue9_fp64_to_double (buf + 8);
/* Block 2 */ /* Block 2 */
if (ue9_memory_read(fd, 2, buf, 128) < 0) return -1; if (ue9_memory_read (fd, 2, buf, 128) < 0)
calib->DACSlope[0] = ue9_fp64_to_double(buf + 0); return -1;
calib->DACOffset[0] = ue9_fp64_to_double(buf + 8); calib->DACSlope[0] = ue9_fp64_to_double (buf + 0);
calib->DACSlope[1] = ue9_fp64_to_double(buf + 16); calib->DACOffset[0] = ue9_fp64_to_double (buf + 8);
calib->DACOffset[1] = ue9_fp64_to_double(buf + 24); calib->DACSlope[1] = ue9_fp64_to_double (buf + 16);
calib->tempSlope = ue9_fp64_to_double(buf + 32); calib->DACOffset[1] = ue9_fp64_to_double (buf + 24);
calib->tempSlopeLow = ue9_fp64_to_double(buf + 48); calib->tempSlope = ue9_fp64_to_double (buf + 32);
calib->calTemp = ue9_fp64_to_double(buf + 64); calib->tempSlopeLow = ue9_fp64_to_double (buf + 48);
calib->Vref = ue9_fp64_to_double(buf + 72); calib->calTemp = ue9_fp64_to_double (buf + 64);
calib->VrefDiv2 = ue9_fp64_to_double(buf + 88); calib->Vref = ue9_fp64_to_double (buf + 72);
calib->VsSlope = ue9_fp64_to_double(buf + 96); calib->VrefDiv2 = ue9_fp64_to_double (buf + 88);
calib->VsSlope = ue9_fp64_to_double (buf + 96);
/* Block 3 */ /* Block 3 */
if (ue9_memory_read(fd, 3, buf, 128) < 0) return -1; if (ue9_memory_read (fd, 3, buf, 128) < 0)
calib->hiResUnipolarSlope = ue9_fp64_to_double(buf + 0); return -1;
calib->hiResUnipolarOffset = ue9_fp64_to_double(buf + 8); calib->hiResUnipolarSlope = ue9_fp64_to_double (buf + 0);
calib->hiResUnipolarOffset = ue9_fp64_to_double (buf + 8);
/* Block 4 */ /* Block 4 */
if (ue9_memory_read(fd, 4, buf, 128) < 0) return -1; if (ue9_memory_read (fd, 4, buf, 128) < 0)
calib->hiResBipolarSlope = ue9_fp64_to_double(buf + 0); return -1;
calib->hiResBipolarOffset = ue9_fp64_to_double(buf + 8); calib->hiResBipolarSlope = ue9_fp64_to_double (buf + 0);
calib->hiResBipolarOffset = ue9_fp64_to_double (buf + 8);
/* All done */ /* All done */
return 1; return 1;
} }
/* Retrieve comm config, returns -1 on error */ /* Retrieve comm config, returns -1 on error */
int ue9_get_comm_config(int fd, struct ue9CommConfig *config) int
ue9_get_comm_config (int fd, struct ue9CommConfig *config)
{ {
uint8_t sendbuf[18]; uint8_t sendbuf[18];
uint8_t recvbuf[24]; uint8_t recvbuf[24];
memset(sendbuf, 0, sizeof(sendbuf)); memset (sendbuf, 0, sizeof (sendbuf));
memset(config, 0, sizeof(struct ue9CommConfig)); memset (config, 0, sizeof (struct ue9CommConfig));
sendbuf[1] = 0xf8; sendbuf[1] = 0xf8;
sendbuf[2] = 0x09; sendbuf[2] = 0x09;
sendbuf[3] = 0x08; sendbuf[3] = 0x08;
if (ue9_command(fd, sendbuf, recvbuf, sizeof(recvbuf)) < 0) { if (ue9_command (fd, sendbuf, recvbuf, sizeof (recvbuf)) < 0)
verb("command failed\n"); {
verb ("command failed\n");
return -1; return -1;
} }
verb("todo\n"); verb ("todo\n");
return -1; return -1;
} }
/* Retrieve control config, returns -1 on error */ /* Retrieve control config, returns -1 on error */
int ue9_get_control_config(int fd, struct ue9ControlConfig *config) int
ue9_get_control_config (int fd, struct ue9ControlConfig *config)
{ {
uint8_t sendbuf[18]; uint8_t sendbuf[18];
uint8_t recvbuf[24]; uint8_t recvbuf[24];
memset(sendbuf, 0, sizeof(sendbuf)); memset (sendbuf, 0, sizeof (sendbuf));
memset(config, 0, sizeof(struct ue9ControlConfig)); memset (config, 0, sizeof (struct ue9ControlConfig));
sendbuf[1] = 0xf8; sendbuf[1] = 0xf8;
sendbuf[2] = 0x06; sendbuf[2] = 0x06;
sendbuf[3] = 0x08; sendbuf[3] = 0x08;
if (ue9_command(fd, sendbuf, recvbuf, sizeof(recvbuf)) < 0) { if (ue9_command (fd, sendbuf, recvbuf, sizeof (recvbuf)) < 0)
verb("command failed\n"); {
verb ("command failed\n");
return -1; return -1;
} }
verb("todo\n"); verb ("todo\n");
return -1; return -1;
} }
/* Open TCP/IP connection to the UE9 */ /* Open TCP/IP connection to the UE9 */
int ue9_open(const char *host, int port) int
ue9_open (const char *host, int port)
{ {
int fd; int fd;
struct sockaddr_in address; struct sockaddr_in address;
struct hostent *he; struct hostent *he;
int window_size = 128 * 1024; int window_size = 128 * 1024;
net_init(); net_init ();
/* Create socket */ /* Create socket */
fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (fd < 0) { if (fd < 0)
verb("socket returned %d\n", fd); {
verb ("socket returned %d\n", fd);
return -1; return -1;
} }
/* Set nonblocking */ /* Set nonblocking */
if (soblock(fd, 0) < 0) { if (soblock (fd, 0) < 0)
verb("can't set nonblocking\n"); {
verb ("can't set nonblocking\n");
return -1; return -1;
} }
/* Set initial window size hint to workaround LabJack firmware bug */ /* Set initial window size hint to workaround LabJack firmware bug */
setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *)&window_size, setsockopt (fd, SOL_SOCKET, SO_SNDBUF, (void *) &window_size,
sizeof(window_size)); sizeof (window_size));
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *)&window_size, setsockopt (fd, SOL_SOCKET, SO_RCVBUF, (void *) &window_size,
sizeof(window_size)); sizeof (window_size));
/* Resolve host */ /* Resolve host */
address.sin_family = AF_INET; address.sin_family = AF_INET;
address.sin_port = htons(port); address.sin_port = htons (port);
he = gethostbyname(host); he = gethostbyname (host);
if (he == NULL) { if (he == NULL)
verb("gethostbyname(\"%s\") failed\n", host); {
verb ("gethostbyname(\"%s\") failed\n", host);
return -1; return -1;
} }
address.sin_addr = *((struct in_addr *) he->h_addr); address.sin_addr = *((struct in_addr *) he->h_addr);
debug("Resolved %s -> %s\n", host, inet_ntoa(address.sin_addr)); debug ("Resolved %s -> %s\n", host, inet_ntoa (address.sin_addr));
/* Connect */ /* Connect */
if (connect_timeout(fd, (struct sockaddr *) &address, sizeof(address), if (connect_timeout (fd, (struct sockaddr *) &address, sizeof (address),
& (struct timeval) { .tv_sec = UE9_TIMEOUT }) < 0) { &(struct timeval)
verb("connection to %s:%d failed: %s\n", {
inet_ntoa(address.sin_addr), port, compat_strerror(errno)); .tv_sec = UE9_TIMEOUT}) < 0)
{
verb ("connection to %s:%d failed: %s\n",
inet_ntoa (address.sin_addr), port, compat_strerror (errno));
return -1; return -1;
} }
...@@ -406,15 +455,17 @@ int ue9_open(const char *host, int port) ...@@ -406,15 +455,17 @@ int ue9_open(const char *host, int port)
} }
/* Close connection to the UE9 */ /* Close connection to the UE9 */
void ue9_close(int fd) void
ue9_close (int fd)
{ {
/* does anyone actually call shutdown these days? */ /* does anyone actually call shutdown these days? */
shutdown(fd, 2 /* SHUT_RDWR */); shutdown (fd, 2 /* SHUT_RDWR */ );
close(fd); close (fd);
} }
/* Compute scanrate based on the provided values. */ /* Compute scanrate based on the provided values. */
double ue9_compute_rate(uint8_t scanconfig, uint16_t scaninterval) double
ue9_compute_rate (uint8_t scanconfig, uint16_t scaninterval)
{ {
double clock; double clock;
...@@ -422,11 +473,20 @@ double ue9_compute_rate(uint8_t scanconfig, uint16_t scaninterval) ...@@ -422,11 +473,20 @@ double ue9_compute_rate(uint8_t scanconfig, uint16_t scaninterval)
a fixed rate, and not affected by the number of channels. a fixed rate, and not affected by the number of channels.
Channels are scanned as quickly as possible. */ Channels are scanned as quickly as possible. */
switch ((scanconfig >> 3) & 3) { switch ((scanconfig >> 3) & 3)
case 0: clock = 4e6; break; {
case 1: clock = 48e6; break; case 0:
case 2: clock = 750e3; break; clock = 4e6;
case 3: clock = 24e6; break; break;
case 1:
clock = 48e6;
break;
case 2:
clock = 750e3;
break;
case 3:
clock = 24e6;
break;
} }
if (scanconfig & 0x2) if (scanconfig & 0x2)
...@@ -440,39 +500,55 @@ double ue9_compute_rate(uint8_t scanconfig, uint16_t scaninterval) ...@@ -440,39 +500,55 @@ double ue9_compute_rate(uint8_t scanconfig, uint16_t scaninterval)
/* Choose the best ScanConfig and ScanInterval parameters for the /* Choose the best ScanConfig and ScanInterval parameters for the
desired scanrate. Returns -1 if no valid config found */ desired scanrate. Returns -1 if no valid config found */
int ue9_choose_scan(double desired_rate, double *actual_rate, int
uint8_t *scanconfig, uint16_t *scaninterval) ue9_choose_scan (double desired_rate, double *actual_rate,
uint8_t * scanconfig, uint16_t * scaninterval)
{ {
int i; int i;
struct { double clock; uint8_t config; } valid[] = { struct
{ 48e6, 0x08 }, {
{ 24e6, 0x18 }, double clock;
{ 4e6, 0x00 }, uint8_t config;
{ 750e3, 0x10 }, } valid[] =
{ 48e6 / 256, 0x0a }, {
{ 24e6 / 256, 0x1a }, {
{ 4e6 / 256, 0x02 }, 48e6, 0x08},
{ 750e3 / 256, 0x12 }, {
{ 0, 0 } }; 24e6, 0x18},
{
4e6, 0x00},
{
750e3, 0x10},
{
48e6 / 256, 0x0a},
{
24e6 / 256, 0x1a},
{
4e6 / 256, 0x02},
{
750e3 / 256, 0x12},
{
0, 0}};
/* Start with the fastest clock frequency. If the /* Start with the fastest clock frequency. If the
scaninterval would be too large, knock it down until it scaninterval would be too large, knock it down until it
fits. */ fits. */
for (i = 0; valid[i].clock != 0; i++) { for (i = 0; valid[i].clock != 0; i++)
{
double interval = valid[i].clock / desired_rate; double interval = valid[i].clock / desired_rate;
debug("Considering clock %lf (interval %lf)\n", debug ("Considering clock %lf (interval %lf)\n",
valid[i].clock, interval); valid[i].clock, interval);
if (interval >= 0.5 && interval < 65535.5) { if (interval >= 0.5 && interval < 65535.5)
{
*scaninterval = floor(interval + 0.5); *scaninterval = floor (interval + 0.5);
*scanconfig = valid[i].config; *scanconfig = valid[i].config;
*actual_rate = ue9_compute_rate( *actual_rate = ue9_compute_rate (*scanconfig, *scaninterval);
*scanconfig, *scaninterval);
debug("Config 0x%02x, desired %lf, actual %lf\n", debug ("Config 0x%02x, desired %lf, actual %lf\n",
*scanconfig, desired_rate, *actual_rate); *scanconfig, desired_rate, *actual_rate);
return 0; return 0;
...@@ -483,58 +559,65 @@ int ue9_choose_scan(double desired_rate, double *actual_rate, ...@@ -483,58 +559,65 @@ int ue9_choose_scan(double desired_rate, double *actual_rate,
} }
/* Flush data buffers */ /* Flush data buffers */
void ue9_buffer_flush(int fd) void
ue9_buffer_flush (int fd)
{ {
uint8_t sendbuf[2], recvbuf[2]; uint8_t sendbuf[2], recvbuf[2];
sendbuf[1] = 0x08; /* FlushBuffer */ sendbuf[1] = 0x08; /* FlushBuffer */
if (ue9_command(fd, sendbuf, recvbuf, sizeof(recvbuf)) < 0) { if (ue9_command (fd, sendbuf, recvbuf, sizeof (recvbuf)) < 0)
verb("command failed\n"); {
verb ("command failed\n");
} }
} }
/* Stop stream. Returns < 0 on failure. */ /* Stop stream. Returns < 0 on failure. */
int ue9_stream_stop(int fd) int
ue9_stream_stop (int fd)
{ {
uint8_t sendbuf[2], recvbuf[4]; uint8_t sendbuf[2], recvbuf[4];
sendbuf[1] = 0xB0; sendbuf[1] = 0xB0;
if (ue9_command(fd, sendbuf, recvbuf, sizeof(recvbuf)) < 0) { if (ue9_command (fd, sendbuf, recvbuf, sizeof (recvbuf)) < 0)
verb("command failed\n"); {
verb ("command failed\n");
return -1; return -1;
} }
if (recvbuf[2] == STREAM_NOT_RUNNING || recvbuf[2] == 0) if (recvbuf[2] == STREAM_NOT_RUNNING || recvbuf[2] == 0)
return 0; return 0;
debug("error %s\n", ue9_error(recvbuf[2])); debug ("error %s\n", ue9_error (recvbuf[2]));
return -recvbuf[2]; return -recvbuf[2];
} }
/* Start stream. Returns < 0 on failure. */ /* Start stream. Returns < 0 on failure. */
int ue9_stream_start(int fd) int
ue9_stream_start (int fd)
{ {
uint8_t sendbuf[2], recvbuf[4]; uint8_t sendbuf[2], recvbuf[4];
sendbuf[1] = 0xA8; sendbuf[1] = 0xA8;
if (ue9_command(fd, sendbuf, recvbuf, sizeof(recvbuf)) < 0) { if (ue9_command (fd, sendbuf, recvbuf, sizeof (recvbuf)) < 0)
verb("command failed\n"); {
verb ("command failed\n");
return -1; return -1;
} }
if (recvbuf[2] == 0) if (recvbuf[2] == 0)
return 0; return 0;
debug("error %s\n", ue9_error(recvbuf[2])); debug ("error %s\n", ue9_error (recvbuf[2]));
return -recvbuf[2]; return -recvbuf[2];
} }
/* "Simple" stream configuration, assumes the channels are all /* "Simple" stream configuration, assumes the channels are all
configured with the same gain. */ configured with the same gain. */
int ue9_streamconfig_simple(int fd, int *channel_list, int channel_count, int
ue9_streamconfig_simple (int fd, int *channel_list, int channel_count,
uint8_t scanconfig, uint16_t scaninterval, uint8_t scanconfig, uint16_t scaninterval,
uint8_t gain) uint8_t gain)
{ {
...@@ -552,19 +635,22 @@ int ue9_streamconfig_simple(int fd, int *channel_list, int channel_count, ...@@ -552,19 +635,22 @@ int ue9_streamconfig_simple(int fd, int *channel_list, int channel_count,
buf[10] = scaninterval & 0xff; buf[10] = scaninterval & 0xff;
buf[11] = scaninterval >> 8; buf[11] = scaninterval >> 8;
for (i = 0; i < channel_count; i++) { for (i = 0; i < channel_count; i++)
buf[12 + 2*i] = channel_list[i]; /* Channel number */ {
buf[13 + 2*i] = gain; /* Gain/bipolar setup */ buf[12 + 2 * i] = channel_list[i]; /* Channel number */
buf[13 + 2 * i] = gain; /* Gain/bipolar setup */
} }
/* Send StreamConfig */ /* Send StreamConfig */
if (ue9_command(fd, buf, buf, 8) < 0) { if (ue9_command (fd, buf, buf, 8) < 0)
debug("command failed\n"); {
debug ("command failed\n");
return -1; return -1;
} }
if (buf[6] != 0) { if (buf[6] != 0)
verb("returned error %s\n", ue9_error(buf[6])); {
verb ("returned error %s\n", ue9_error (buf[6]));
return -1; return -1;
} }
...@@ -573,7 +659,8 @@ int ue9_streamconfig_simple(int fd, int *channel_list, int channel_count, ...@@ -573,7 +659,8 @@ int ue9_streamconfig_simple(int fd, int *channel_list, int channel_count,
/* Stream data and pass it to the data callback. If callback returns /* Stream data and pass it to the data callback. If callback returns
negative, stops reading and returns 0. Returns < 0 on error. */ negative, stops reading and returns 0. Returns < 0 on error. */
int ue9_stream_data(int fd, int channels, int
ue9_stream_data (int fd, int channels,
ue9_stream_cb_t callback, void *context) ue9_stream_cb_t callback, void *context)
{ {
int ret; int ret;
...@@ -583,68 +670,77 @@ int ue9_stream_data(int fd, int channels, ...@@ -583,68 +670,77 @@ int ue9_stream_data(int fd, int channels,
int i; int i;
uint16_t data[channels]; uint16_t data[channels];
for (;;) { for (;;)
{
/* Receive data */ /* Receive data */
ret = recv_all_timeout(fd, buf, 46, 0, & (struct timeval) ret = recv_all_timeout (fd, buf, 46, 0, &(struct timeval)
{ .tv_sec = UE9_TIMEOUT }); {
.tv_sec = UE9_TIMEOUT});
/* Verify packet format */ /* Verify packet format */
if (ret != 46) { if (ret != 46)
verb("short recv %d\n", (int)ret); {
verb ("short recv %d\n", (int) ret);
return -1; return -1;
} }
if (!ue9_verify_extended(buf, 46) || if (!ue9_verify_extended (buf, 46) || !ue9_verify_normal (buf, 6))
!ue9_verify_normal(buf, 6)) { {
verb("bad checksum\n"); verb ("bad checksum\n");
return -2; return -2;
} }
if (buf[1] != 0xF9 || buf[2] != 0x14 || buf[3] != 0xC0) { if (buf[1] != 0xF9 || buf[2] != 0x14 || buf[3] != 0xC0)
verb("bad command bytes\n"); {
verb ("bad command bytes\n");
return -3; return -3;
} }
if (buf[11] != 0) { if (buf[11] != 0)
verb("stream error: %s\n", ue9_error(buf[11])); {
verb ("stream error: %s\n", ue9_error (buf[11]));
return -4; return -4;
} }
/* Check for dropped packets. */ /* Check for dropped packets. */
if (buf[10] != packet) { if (buf[10] != packet)
verb("expected packet %d, but received packet %d\n", {
verb ("expected packet %d, but received packet %d\n",
packet, buf[10]); packet, buf[10]);
return -5; return -5;
} }
packet++; packet++;
/* Check comm processor backlog (up to 512 kB) */ /* Check comm processor backlog (up to 512 kB) */
if (buf[45] & 0x80) { if (buf[45] & 0x80)
verb("buffer overflow in CommBacklog, aborting\n"); {
verb ("buffer overflow in CommBacklog, aborting\n");
return -6; return -6;
} }
if ((buf[45] & 0x7f) > 112) if ((buf[45] & 0x7f) > 112)
debug("warning: CommBacklog is high (%d bytes)\n", debug ("warning: CommBacklog is high (%d bytes)\n",
(buf[45] & 0x7f) * 4096); (buf[45] & 0x7f) * 4096);
/* Check control processor backlog (up to 256 bytes). */ /* Check control processor backlog (up to 256 bytes). */
if (buf[44] == 255) { if (buf[44] == 255)
verb("ControlBacklog is maxed out, aborting\n"); {
verb ("ControlBacklog is maxed out, aborting\n");
return -7; return -7;
} }
if (buf[44] > 224) if (buf[44] > 224)
debug("warning: ControlBacklog is high (%d bytes)\n", debug ("warning: ControlBacklog is high (%d bytes)\n", buf[44]);
buf[44]);
/* Read samples from the buffer */ /* Read samples from the buffer */
for (i = 12; i <= 42; i += 2) { for (i = 12; i <= 42; i += 2)
data[channel++] = buf[i] + (buf[i+1] << 8); {
data[channel++] = buf[i] + (buf[i + 1] << 8);
if (channel < channels) if (channel < channels)
continue; continue;
/* Received a full scan, send to callback */ /* Received a full scan, send to callback */
channel = 0; channel = 0;
if ((*callback)(channels, data, context) < 0) { if ((*callback) (channels, data, context) < 0)
{
/* We're done */ /* We're done */
return 0; return 0;
} }
......
...@@ -16,7 +16,8 @@ ...@@ -16,7 +16,8 @@
#include "netutil.h" #include "netutil.h"
/* Calibration data */ /* Calibration data */
struct ue9Calibration { struct ue9Calibration
{
double unipolarSlope[4]; double unipolarSlope[4];
double unipolarOffset[4]; double unipolarOffset[4];
double bipolarSlope; double bipolarSlope;
...@@ -36,7 +37,8 @@ struct ue9Calibration { ...@@ -36,7 +37,8 @@ struct ue9Calibration {
}; };
/* Comm config */ /* Comm config */
struct ue9CommConfig { struct ue9CommConfig
{
uint8_t local_id; uint8_t local_id;
uint8_t power_level; uint8_t power_level;
in_addr_t address; in_addr_t address;
...@@ -52,7 +54,8 @@ struct ue9CommConfig { ...@@ -52,7 +54,8 @@ struct ue9CommConfig {
}; };
/* Control config */ /* Control config */
struct ue9ControlConfig { struct ue9ControlConfig
{
uint8_t power_level; uint8_t power_level;
uint8_t reset_source; uint8_t reset_source;
double control_fw_version; double control_fw_version;
...@@ -77,65 +80,65 @@ struct ue9ControlConfig { ...@@ -77,65 +80,65 @@ struct ue9ControlConfig {
#define UE9_CHANNELS 14 #define UE9_CHANNELS 14
/* Fill checksums in data buffers */ /* Fill checksums in data buffers */
void ue9_checksum_normal(uint8_t *buffer, size_t len); void ue9_checksum_normal (uint8_t * buffer, size_t len);
void ue9_checksum_extended(uint8_t *buffer, size_t len); void ue9_checksum_extended (uint8_t * buffer, size_t len);
/* Verify checksums in data buffers. Returns 0 on error. */ /* Verify checksums in data buffers. Returns 0 on error. */
int ue9_verify_normal(uint8_t *buffer, size_t len); int ue9_verify_normal (uint8_t * buffer, size_t len);
int ue9_verify_extended(uint8_t *buffer, size_t len); int ue9_verify_extended (uint8_t * buffer, size_t len);
/* Open/close TCP/IP connection to the UE9 */ /* Open/close TCP/IP connection to the UE9 */
int ue9_open(const char *host, int port); int ue9_open (const char *host, int port);
void ue9_close(int fd); void ue9_close (int fd);
/* Read a memory block from the device. Returns -1 on error. */ /* Read a memory block from the device. Returns -1 on error. */
int ue9_memory_read(int fd, int blocknum, uint8_t *buffer, int len); int ue9_memory_read (int fd, int blocknum, uint8_t * buffer, int len);
/* Convert 64-bit fixed point to double type */ /* Convert 64-bit fixed point to double type */
double ue9_fp64_to_double(uint8_t *data); double ue9_fp64_to_double (uint8_t * data);
/* Retrieve calibration data or configuration from the device */ /* Retrieve calibration data or configuration from the device */
int ue9_get_calibration(int fd, struct ue9Calibration *calib); int ue9_get_calibration (int fd, struct ue9Calibration *calib);
int ue9_get_comm_config(int fd, struct ue9CommConfig *config); int ue9_get_comm_config (int fd, struct ue9CommConfig *config);
int ue9_get_control_config(int fd, struct ue9ControlConfig *config); int ue9_get_control_config (int fd, struct ue9ControlConfig *config);
/* Data conversion. If calib is NULL, use uncalibrated conversions. */ /* Data conversion. If calib is NULL, use uncalibrated conversions. */
double ue9_binary_to_analog(struct ue9Calibration *calib, double ue9_binary_to_analog (struct ue9Calibration *calib,
uint8_t gain, uint8_t resolution, uint16_t data); uint8_t gain, uint8_t resolution, uint16_t data);
/* Compute scanrate based on the provided values. */ /* Compute scanrate based on the provided values. */
double ue9_compute_rate(uint8_t scanconfig, uint16_t scaninterval); double ue9_compute_rate (uint8_t scanconfig, uint16_t scaninterval);
/* Choose the best ScanConfig and ScanInterval parameters for the /* Choose the best ScanConfig and ScanInterval parameters for the
desired scanrate. Returns 0 if nothing can be chosen. */ desired scanrate. Returns 0 if nothing can be chosen. */
int ue9_choose_scan(double desired_rate, double *actual_rate, int ue9_choose_scan (double desired_rate, double *actual_rate,
uint8_t *scanconfig, uint16_t *scaninterval); uint8_t * scanconfig, uint16_t * scaninterval);
/* Flush data buffers */ /* Flush data buffers */
void ue9_buffer_flush(int fd); void ue9_buffer_flush (int fd);
/* Stop stream. Returns < 0 on failure. */ /* Stop stream. Returns < 0 on failure. */
int ue9_stream_stop(int fd); int ue9_stream_stop (int fd);
/* Start stream. Returns < 0 on failure. */ /* Start stream. Returns < 0 on failure. */
int ue9_stream_start(int fd); int ue9_stream_start (int fd);
/* Execute a command on the UE9. Returns -1 on error. Fills the /* Execute a command on the UE9. Returns -1 on error. Fills the
checksums on the outgoing packets, and verifies them on the checksums on the outgoing packets, and verifies them on the
incoming packets. Data in "out" is transmitted, data in "in" is incoming packets. Data in "out" is transmitted, data in "in" is
received. */ received. */
int ue9_command(int fd, uint8_t *out, uint8_t *in, int inlen); int ue9_command (int fd, uint8_t * out, uint8_t * in, int inlen);
/* "Simple" stream configuration, assumes the channels are all /* "Simple" stream configuration, assumes the channels are all
configured with the same gain. */ configured with the same gain. */
int ue9_streamconfig_simple(int fd, int *channel_list, int channel_count, int ue9_streamconfig_simple (int fd, int *channel_list, int channel_count,
uint8_t scanconfig, uint16_t scaninterval, uint8_t scanconfig, uint16_t scaninterval,
uint8_t gain); uint8_t gain);
/* Stream data and pass it to the data callback. If callback returns /* Stream data and pass it to the data callback. If callback returns
negative, stops reading and returns 0. Returns < 0 on error. */ negative, stops reading and returns 0. Returns < 0 on error. */
typedef int (*ue9_stream_cb_t)(int channels, uint16_t *data, void *context); typedef int (*ue9_stream_cb_t) (int channels, uint16_t * data, void *context);
int ue9_stream_data(int fd, int channels, int ue9_stream_data (int fd, int channels,
ue9_stream_cb_t callback, void *context); ue9_stream_cb_t callback, void *context);
#endif #endif
...@@ -42,9 +42,10 @@ const char *ue9_error_text[] = { ...@@ -42,9 +42,10 @@ const char *ue9_error_text[] = {
[PLL_NOT_LOCKED] = "PLL_NOT_LOCKED" [PLL_NOT_LOCKED] = "PLL_NOT_LOCKED"
}; };
const char *ue9_error(int errorcode) const char *
ue9_error (int errorcode)
{ {
if (errorcode > ARRAY_SIZE(ue9_error_text)) if (errorcode > ARRAY_SIZE (ue9_error_text))
return "(invalid errorcode)"; return "(invalid errorcode)";
else else
return ue9_error_text[errorcode]; return ue9_error_text[errorcode];
......
...@@ -44,6 +44,6 @@ ...@@ -44,6 +44,6 @@
extern const char *ue9_error_text[]; extern const char *ue9_error_text[];
const char *ue9_error(int errorcode); const char *ue9_error (int errorcode);
#endif #endif
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 sign in to comment