Progressive refinement

r1199 | dcl | 2006-10-26 18:38:34 +1000 (Thu, 26 Oct 2006) | 2 lines
This commit is contained in:
Douglas Clowes
2006-10-26 18:38:34 +10:00
parent 7dfa37057b
commit 4623b25ccd
8 changed files with 261 additions and 172 deletions

View File

@@ -1,34 +1,60 @@
SHELL = /bin/sh SHELL = /bin/sh
LIBS = nidaqmx CC = gcc
LIBFLAGS = -l$(LIBS) CXX = g++
TARGET = Digital LD = $(CXX)
OBJS = $(TARGET).o params.o utility.o sock.o display.o dio.o hdio.o
CDEBUG = -ggdb3 -Wall CDEBUG = -ggdb3 -Wall
LDFLAGS += -g LDFLAGS += -g
TARGETS = Monitor
CFLAGS += $(CDEBUG) OBJS = Monitor.o params.o utility.o sock.o display.o device.o hware.o
ifeq ($(MAKECMDGOALS),rlp)
OBJECTS =
LIBS =
LIBFLAGS =
DDK_DIR = /home/dcl/nitest/nimhddk_linux26
OBJECTS += $(DDK_DIR)/osiBus.o
OBJECTS += $(DDK_DIR)/Linux26/osiUserCode.o
TIO_DIR = /home/dcl/nitest/NI660x
OBJECTS += $(TIO_DIR)/tTIO.o
IFLAGS = -I$(DDK_DIR)
IFLAGS += -I$(TIO_DIR)/ChipObjects/
CXXFLAGS += -DkLittleEndian=1 -DREGISTER_LEVEL_PROGRAMMING=1
LIBFLAGS += $(OBJECTS)
CXXFLAGS += $(IFLAGS)
else
LIBS = nidaqmx
LIBFLAGS = $(foreach lib,$(LIBS),-l$(lib))
endif
all: $(TARGET) all: $(TARGETS)
rlp: all
clean: clean:
rm -f $(OBJS) $(TARGET) core rm -f $(OBJS) Monitor core
$(TARGET) : $(OBJS) Monitor : $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBFLAGS) -ggdb3 $(LD) $(LDFLAGS) -o $@ $(OBJS) $(LIBFLAGS) -ggdb3
$(TARGET).o: $(TARGET).c $(TARGET).h params.h utility.h sock.h dio.h Monitor.o: Monitor.c Monitor.h params.h utility.h sock.h device.h
dio.o: dio.c dio.h params.h sock.h device.o: device.c device.h params.h sock.h
display.o: display.c display.h utility.h params.h sock.h dio.h display.o: display.c display.h utility.h params.h sock.h device.h
hdio.o: hdio.c hdio.h hware.o: hware.c hware.h
params.o: params.c params.h sock.h params.o: params.c params.h sock.h
sock.o: sock.c sock.h utility.h display.h dio.h sock.o: sock.c sock.h utility.h display.h device.h
utility.o: utility.c utility.h utility.o: utility.c utility.h
%.o : %.c
$(CXX) -c -o $@ $< $(CXXFLAGS) $(CDEBUG)
%.o : %.cpp
$(CXX) -c -o $@ $< $(CXXFLAGS) $(CDEBUG)
# vim: noexpandtab ts=8 sts=8

View File

@@ -2,7 +2,7 @@
#include "utility.h" #include "utility.h"
#include "params.h" #include "params.h"
#include "sock.h" #include "sock.h"
#include "dio.h" #include "device.h"
#include <ctype.h> #include <ctype.h>
@@ -159,10 +159,11 @@ static void add_counter(BUFFER* buffer, char* title, char* name, uint64 value)
*/ */
void put_form(int n) void put_form(int n)
{ {
dprintf(0, "put_form\n"); dbg_printf(0, "put_form\n");
BUFFER html; BUFFER html;
BUFFER buffer; BUFFER buffer;
PARAMETERS* pp = &counter.params; DEVICE* device = (DEVICE*) sock_device(n);
PARAMETERS* pp = &device->params;
char *bp; char *bp;
buffer.length = 0; buffer.length = 0;
bp = &buffer.body[buffer.length]; bp = &buffer.body[buffer.length];
@@ -203,6 +204,12 @@ void put_form(int n)
add_option(&buffer, "Sample", "1", pp->range_mode == 1); add_option(&buffer, "Sample", "1", pp->range_mode == 1);
add_option(&buffer, "Poll", "2", pp->range_mode == 2); add_option(&buffer, "Poll", "2", pp->range_mode == 2);
add_select_end(&buffer); add_select_end(&buffer);
add_select_begin(&buffer, "Source", "source");
add_option(&buffer, "External", "0", pp->source == 0);
add_option(&buffer, "Clock_1 20MHz", "1", pp->source == 1);
add_option(&buffer, "Clock_2 100KHz", "2", pp->source == 2);
add_option(&buffer, "Clock_3 80MHz", "3", pp->source == 3);
add_select_end(&buffer);
add_select_begin(&buffer, "Output Line", "output"); add_select_begin(&buffer, "Output Line", "output");
add_option(&buffer, "Disabled", "disabled", pp->output_line == 0); add_option(&buffer, "Disabled", "disabled", pp->output_line == 0);
add_option(&buffer, "Enabled", "enabled", pp->output_line == 1); add_option(&buffer, "Enabled", "enabled", pp->output_line == 1);
@@ -237,7 +244,7 @@ void put_form(int n)
sock_send(n, &html); sock_send(n, &html);
} }
static void show_text(BUFFER* buffer, char* title, char* value) static void show_text(BUFFER* buffer, const char* title, const char* value)
{ {
char* bp = &buffer->body[buffer->length]; char* bp = &buffer->body[buffer->length];
snprintf(bp, sizeof(buffer->body) - buffer->length, snprintf(bp, sizeof(buffer->body) - buffer->length,
@@ -313,27 +320,27 @@ static void show_time(BUFFER* buffer,
*/ */
void put_page(int n) void put_page(int n)
{ {
dprintf(0, "put_page\n"); dbg_printf(0, "put_page\n");
BUFFER html; BUFFER html;
BUFFER buffer; BUFFER buffer;
COUNTER* cp = &counter; DEVICE* device = (DEVICE*) sock_device(n);
PARAMETERS* pp = &counter.params; PARAMETERS* pp = &device->params;
SAMPLE* sp; SAMPLE* sp;
char *bp; char *bp;
int refresh; int refresh;
#if 1 #if 1
{ {
refresh = cntr_time_to_next_report(cp) + 1; refresh = (int) device_time_to_next_report(device) + 1;
} }
#else #else
make_report(cp); make_report(device);
refresh = (pp->poll_period * pp->sample_period * pp->report_period + 500) / 1000; refresh = (pp->poll_period * pp->sample_period * pp->report_period + 500) / 1000;
#endif #endif
if (refresh < MIN_REFRESH) if (refresh < MIN_REFRESH)
refresh = MIN_REFRESH; refresh = MIN_REFRESH;
if (refresh > MAX_REFRESH) if (refresh > MAX_REFRESH)
refresh = MAX_REFRESH; refresh = MAX_REFRESH;
sp = &cp->report; sp = &device->report;
buffer.length = 0; buffer.length = 0;
bp = &buffer.body[buffer.length]; bp = &buffer.body[buffer.length];
snprintf(bp, sizeof(buffer.body) - buffer.length, snprintf(bp, sizeof(buffer.body) - buffer.length,
@@ -344,25 +351,25 @@ void put_page(int n)
"<tr><th align=\"right\">Field</th>\r\n" "<tr><th align=\"right\">Field</th>\r\n"
"<th align=\"left\">Value</th></tr>\r\n"); "<th align=\"left\">Value</th></tr>\r\n");
buffer.length += strlen(bp); buffer.length += strlen(bp);
show_text(&buffer, "State", cp->state == counter_stopped ? "STOPPED" : show_text(&buffer, "State", device->state == counter_stopped ? "STOPPED" :
cp->state == counter_running ? "RUNNING" : device->state == counter_running ? "RUNNING" :
cp->state == counter_paused ? "PAUSED" : "IDLE"); device->state == counter_paused ? "PAUSED" : "IDLE");
show_text(&buffer, "Direction", pp->direction == COUNT_UP ? "UP" : "DOWN"); show_text(&buffer, "Direction", pp->direction == COUNT_UP ? "UP" : "DOWN");
show_int(&buffer, "Scan", pp->poll_period); show_int(&buffer, "Scan", pp->poll_period);
show_int(&buffer, "Sample", pp->sample_period); show_int(&buffer, "Sample", pp->sample_period);
show_int(&buffer, "Report", pp->report_period); show_int(&buffer, "Report", pp->report_period);
show_time(&buffer, "Start Time", &cp->start_time, false); show_time(&buffer, "Start Time", &device->start_time, false);
show_time(&buffer, "Current Time", &cp->current_time, false); show_time(&buffer, "Current Time", &device->current_time, false);
{ {
struct timeval tv = cp->stop_time; struct timeval tv = device->stop_time;
if (cp->state == counter_running || cp->state == counter_paused) if (device->state == counter_running || device->state == counter_paused)
{ {
tv = cp->current_time; tv = device->current_time;
} }
time_sub(&tv, &cp->start_time); time_sub(&tv, &device->start_time);
show_time(&buffer, "Elapsed", &tv, true); show_time(&buffer, "Elapsed", &tv, true);
} }
show_time(&buffer, "Runtime", &cp->accumulated, true); show_time(&buffer, "Runtime", &device->accumulated, true);
show_time(&buffer, "Report Time", &sp->timestamp, false); show_time(&buffer, "Report Time", &sp->timestamp, false);
show_counter(&buffer, "Counter", sp->counter_value); show_counter(&buffer, "Counter", sp->counter_value);
show_int(&buffer, "Num Polls", sp->num_polls); show_int(&buffer, "Num Polls", sp->num_polls);
@@ -375,9 +382,9 @@ void put_page(int n)
if (pp->range_check_enable) if (pp->range_check_enable)
{ {
char* result; char* result;
if (cp->range_error == 1) if (device->range_error == 1)
result = "<font color=\"BLUE\">LOW</font>"; result = "<font color=\"BLUE\">LOW</font>";
else if (cp->range_error == 2) else if (device->range_error == 2)
result = "<font color=\"RED\">HIGH</font>"; result = "<font color=\"RED\">HIGH</font>";
else else
result = "<font color=\"GREEN\">OK</font>"; result = "<font color=\"GREEN\">OK</font>";
@@ -431,54 +438,55 @@ void put_page(int n)
*/ */
void process_form(int n, BUFFER* bp) void process_form(int n, BUFFER* bp)
{ {
dprintf(0, "process_form\n"); dbg_printf(0, "process_form\n");
DEVICE* device = (DEVICE*) sock_device(n);
int i, j; int i, j;
int state; int state;
char name[80]; char name[80];
char value[80]; char value[80];
char hex_str[3]; char hex_str[3];
unsigned int hex_val; unsigned int hex_val;
char* cp; char* tp;
state = 0; state = 0;
j = 0; j = 0;
for (i = 0; i < bp->length; ++i) for (i = 0; i < bp->length; ++i)
{ {
cp = &bp->body[i]; tp = &bp->body[i];
if (state == 0) if (state == 0)
{ {
if (*cp == '=') if (*tp == '=')
{ {
name[j++] = '\0'; name[j++] = '\0';
j = 0; j = 0;
state = 1; state = 1;
} }
else if (j < 80 - 1) else if (j < 80 - 1)
name[j++] = tolower(*cp); name[j++] = tolower(*tp);
} }
else if (state == 1) else if (state == 1)
{ {
if (*cp == '&') if (*tp == '&')
{ {
value[j++] = '\0'; value[j++] = '\0';
j = 0; j = 0;
state = 9; state = 9;
} }
else if (*cp == '%') else if (*tp == '%')
{ {
hex_str[0] = '\0'; hex_str[0] = '\0';
state = 2; state = 2;
} }
else if (j < 80 - 1) else if (j < 80 - 1)
value[j++] = tolower(*cp); value[j++] = tolower(*tp);
} }
else if (state == 2) else if (state == 2)
{ {
hex_str[0] = tolower(*cp); hex_str[0] = tolower(*tp);
state = 3; state = 3;
} }
else if (state == 3) else if (state == 3)
{ {
hex_str[1] = tolower(*cp); hex_str[1] = tolower(*tp);
hex_str[2] = '\0'; hex_str[2] = '\0';
hex_val = strtoul(hex_str, NULL, 16); hex_val = strtoul(hex_str, NULL, 16);
if (hex_val < ' ' || hex_val > '~') if (hex_val < ' ' || hex_val > '~')
@@ -489,8 +497,8 @@ void process_form(int n, BUFFER* bp)
} }
if (state == 9) if (state == 9)
{ {
dprintf(0, "name=\"%s\", value=\"%s\"\n", name, value); dbg_printf(0, "name=\"%s\", value=\"%s\"\n", name, value);
if (param_set(&counter.params, name, value)) if (param_set(&device->params, name, value))
; ;
else if (strcmp(name, "xxx")) else if (strcmp(name, "xxx"))
{ {
@@ -510,20 +518,20 @@ void process_form(int n, BUFFER* bp)
*/ */
void put_page_refresh(int n) void put_page_refresh(int n)
{ {
dprintf(0, "put_page_refresh\n"); dbg_printf(0, "put_page_refresh\n");
BUFFER html; BUFFER html;
BUFFER buffer; BUFFER buffer;
char* bp; char* bp;
COUNTER* cp = &counter; DEVICE* device = (DEVICE*) sock_device(n);
int refresh; int refresh;
#if 1 #if 1
{ {
refresh = cntr_time_to_next_report(cp) + 1; refresh = (int) device_time_to_next_report(device) + 1;
} }
#else #else
PARAMETERS* pp = &counter.params; PARAMETERS* pp = &device->params;
make_report(cp); make_report(device);
refresh = (pp->poll_period * pp->sample_period * pp->report_period + 500) / 1000; refresh = (pp->poll_period * pp->sample_period * pp->report_period + 500) / 1000;
#endif #endif
if (refresh < MIN_REFRESH) if (refresh < MIN_REFRESH)
@@ -563,20 +571,20 @@ void put_page_refresh(int n)
*/ */
void put_form_refresh(int n) void put_form_refresh(int n)
{ {
dprintf(0, "put_form_refresh\n"); dbg_printf(0, "put_form_refresh\n");
DEVICE* device = (DEVICE*) sock_device(n);
BUFFER html; BUFFER html;
BUFFER buffer; BUFFER buffer;
char* bp; char* bp;
COUNTER* cp = &counter;
int refresh; int refresh;
#if 1 #if 1
{ {
refresh = cntr_time_to_next_report(cp) + 1; refresh = (int) device_time_to_next_report(device) + 1;
} }
#else #else
PARAMETERS* pp = &counter.params; PARAMETERS* pp = &device->params;
make_report(cp); make_report(device);
refresh = (pp->poll_period * pp->sample_period * pp->report_period + 500) / 1000; refresh = (pp->poll_period * pp->sample_period * pp->report_period + 500) / 1000;
#endif #endif
if (refresh < MIN_REFRESH) if (refresh < MIN_REFRESH)
@@ -616,47 +624,48 @@ void put_form_refresh(int n)
*/ */
void process_command(int n, BUFFER* bp) void process_command(int n, BUFFER* bp)
{ {
dprintf(0, "process_command(%d, %s)\n", n, bp->body); dbg_printf(0, "process_command(%d, %s)\n", n, bp->body);
DEVICE* device = (DEVICE*) sock_device(n);
bool sics = false; bool sics = false;
int error = 1; int error = 1;
char command[80]; char command[80];
char param[80]; char param[80];
int len = 0; int len = 0;
char* cp = bp->body; char* tp = bp->body;
while (isspace(*cp)) while (isspace(*tp))
++cp; ++tp;
len = 0; len = 0;
while (*cp && !isspace(*cp)) while (*tp && !isspace(*tp))
{ {
if (len < 80 - 1) if (len < 80 - 1)
command[len++] = *cp; command[len++] = *tp;
++cp; ++tp;
} }
command[len] = '\0'; command[len] = '\0';
if (strcasecmp(command, "SICS") == 0) if (strcasecmp(command, "SICS") == 0)
{ {
sics = true; sics = true;
while (isspace(*cp)) while (isspace(*tp))
++cp; ++tp;
len = 0; len = 0;
while (*cp && !isspace(*cp)) while (*tp && !isspace(*tp))
{ {
if (len < 80 - 1) if (len < 80 - 1)
command[len++] = *cp; command[len++] = *tp;
++cp; ++tp;
} }
command[len] = '\0'; command[len] = '\0';
} }
while (isspace(*cp)) while (isspace(*tp))
++cp; ++tp;
if (strcasecmp(command, "START") == 0) if (strcasecmp(command, "START") == 0)
error = cntr_start(&counter); error = device_start(device);
else if (strcasecmp(command, "STOP") == 0) else if (strcasecmp(command, "STOP") == 0)
error = cntr_stop(&counter); error = device_stop(device);
else if (strcasecmp(command, "PAUSE") == 0) else if (strcasecmp(command, "PAUSE") == 0)
error = cntr_pause(&counter); error = device_pause(device);
else if (strcasecmp(command, "RESUME") == 0) else if (strcasecmp(command, "RESUME") == 0)
error = cntr_resume(&counter); error = device_resume(device);
else if (strcasecmp(command, "REPORT") == 0) else if (strcasecmp(command, "REPORT") == 0)
{ {
int match = 1; int match = 1;
@@ -664,11 +673,11 @@ void process_command(int n, BUFFER* bp)
if (sics) if (sics)
match = 2; match = 2;
len = 0; len = 0;
while (*cp && !isspace(*cp)) while (*tp && !isspace(*tp))
{ {
if (len < 80 - 1) if (len < 80 - 1)
param[len++] = *cp; param[len++] = *tp;
++cp; ++tp;
} }
param[len] = '\0'; param[len] = '\0';
if (strcasecmp(param, "OFF") == 0) if (strcasecmp(param, "OFF") == 0)
@@ -678,24 +687,24 @@ void process_command(int n, BUFFER* bp)
else if (strcasecmp(command, "SET") == 0) else if (strcasecmp(command, "SET") == 0)
{ {
/* set parameter */ /* set parameter */
dprintf(0, "SET %s\n", cp); dbg_printf(0, "SET %s\n", tp);
if (param_set_cmd(&counter.params, cp)) if (param_set_cmd(&device->params, tp))
error = 0; error = 0;
} }
else if (strcasecmp(command, "GET") == 0) else if (strcasecmp(command, "GET") == 0)
{ {
/* get parameter */ /* get parameter */
param_get_cmd(&counter.params, cp, n); param_get_cmd(&device->params, tp, n);
return; return;
} }
else if (strcasecmp(command, "READ") == 0) else if (strcasecmp(command, "READ") == 0)
{ {
cntr_read(&counter, n); device_read(device, n);
return; return;
} }
else if (!sics) else if (!sics)
{ {
cntr_send(&counter, n); device_send(device, n);
return; return;
} }
if (error == 0) if (error == 0)

View File

@@ -17,6 +17,7 @@
#define CMD_RANGE_MODE 12 #define CMD_RANGE_MODE 12
#define CMD_OUTPUT 13 #define CMD_OUTPUT 13
#define CMD_SYNC 14 #define CMD_SYNC 14
#define CMD_SOURCE 15
#define TXT_DIRECTION "DIRECTION" #define TXT_DIRECTION "DIRECTION"
#define TXT_SCAN "SCAN" #define TXT_SCAN "SCAN"
@@ -32,6 +33,7 @@
#define TXT_RANGE_MODE "RANGE_MODE" #define TXT_RANGE_MODE "RANGE_MODE"
#define TXT_OUTPUT "OUTPUT" #define TXT_OUTPUT "OUTPUT"
#define TXT_SYNC "SYNC" #define TXT_SYNC "SYNC"
#define TXT_SOURCE "SOURCE"
static struct param_command_t { static struct param_command_t {
int cmd; int cmd;
@@ -51,9 +53,10 @@ static struct param_command_t {
{CMD_RANGE_MODE, TXT_RANGE_MODE}, {CMD_RANGE_MODE, TXT_RANGE_MODE},
{CMD_OUTPUT, TXT_OUTPUT}, {CMD_OUTPUT, TXT_OUTPUT},
{CMD_SYNC, TXT_SYNC}, {CMD_SYNC, TXT_SYNC},
{CMD_SOURCE, TXT_SOURCE},
{0, NULL} {0, NULL}
}; };
#define NUM_CMDS (sizeof(param_command)/sizeof(param_command[0])) #define NUM_CMDS ((int) ((sizeof(param_command)/sizeof(param_command[0]))))
static int name_to_command(char* name) static int name_to_command(char* name)
{ {
@@ -74,22 +77,22 @@ bool param_set(pPARAMETERS pp, char* name, char* value)
{ {
case CMD_DIRECTION: case CMD_DIRECTION:
result = true; result = true;
dprintf(0, "Direction=%s", pp->direction ? "Down" : "Up"); dbg_printf(0, "Direction=%s", pp->direction ? "Down" : "Up");
if (strcasecmp(value, "down") == 0) if (strcasecmp(value, "down") == 0)
pp->direction = COUNT_DOWN; pp->direction = COUNT_DOWN;
else else
pp->direction = COUNT_UP; pp->direction = COUNT_UP;
dprintf(0, "=>%s\n", pp->direction ? "Down" : "Up"); dbg_printf(0, "=>%s\n", pp->direction ? "Down" : "Up");
break; break;
case CMD_INITIAL: case CMD_INITIAL:
result = true; result = true;
dprintf(0, "Initial=%llu", pp->initial_count); dbg_printf(0, "Initial=%llu", pp->initial_count);
pp->initial_count = strtoull(value, NULL, 10); pp->initial_count = strtoull(value, NULL, 10);
dprintf(0, "=>%llu\n", pp->initial_count); dbg_printf(0, "=>%llu\n", pp->initial_count);
break; break;
case CMD_OUTPUT: case CMD_OUTPUT:
result = true; result = true;
dprintf(0, "Direction=%s", pp->output_line == 2 ? "Inverted": dbg_printf(0, "Output=%s", pp->output_line == 2 ? "Inverted":
pp->output_line == 1 ? "Enabled" : "Disabled"); pp->output_line == 1 ? "Enabled" : "Disabled");
if (strcasecmp(value, "enabled") == 0) if (strcasecmp(value, "enabled") == 0)
pp->output_line = 1; pp->output_line = 1;
@@ -97,105 +100,115 @@ bool param_set(pPARAMETERS pp, char* name, char* value)
pp->output_line = 2; pp->output_line = 2;
else else
pp->output_line = 0; pp->output_line = 0;
dprintf(0, "=>%s\n", pp->output_line == 2 ? "Inverted": dbg_printf(0, "=>%s\n", pp->output_line == 2 ? "Inverted":
pp->output_line == 1 ? "Enabled" : "Disabled"); pp->output_line == 1 ? "Enabled" : "Disabled");
break; break;
case CMD_REPORT: case CMD_REPORT:
result = true; result = true;
dprintf(0, "Report=%d", pp->report_period); dbg_printf(0, "Report=%d", pp->report_period);
pp->report_period = strtol(value, NULL, 10); pp->report_period = strtol(value, NULL, 10);
if (pp->report_period < 1) if (pp->report_period < 1)
pp->report_period = 1; pp->report_period = 1;
else if (pp->report_period > 1000) else if (pp->report_period > 1000)
pp->report_period = 1000; pp->report_period = 1000;
dprintf(0, "=>%d\n", pp->report_period); dbg_printf(0, "=>%d\n", pp->report_period);
break; break;
case CMD_RANGE_MODE: case CMD_RANGE_MODE:
result = true; result = true;
dprintf(0, "range_mode=%d", pp->range_mode); dbg_printf(0, "range_mode=%d", pp->range_mode);
pp->range_mode = strtol(value, NULL, 10); pp->range_mode = strtol(value, NULL, 10);
if (pp->range_mode < 0) if (pp->range_mode < 0)
pp->range_mode = 0; pp->range_mode = 0;
else if (pp->range_mode > 2) else if (pp->range_mode > 2)
pp->range_mode = 0; pp->range_mode = 0;
dprintf(0, "=>%d\n", pp->range_mode); dbg_printf(0, "=>%d\n", pp->range_mode);
break; break;
case CMD_RANGE_GATE: case CMD_RANGE_GATE:
result = true; result = true;
dprintf(0, "range_gate=%s", dbg_printf(0, "range_gate=%s",
pp->range_gate_enable ? "Enabled" : "Disabled"); pp->range_gate_enable ? "Enabled" : "Disabled");
if (strcasecmp(value, "enabled") == 0) if (strcasecmp(value, "enabled") == 0)
pp->range_gate_enable = true; pp->range_gate_enable = true;
else else
pp->range_gate_enable = false; pp->range_gate_enable = false;
dprintf(0, "=>%s\n", dbg_printf(0, "=>%s\n",
pp->range_gate_enable ? "Enabled" : "Disabled"); pp->range_gate_enable ? "Enabled" : "Disabled");
break; break;
case CMD_RE_CHECK: case CMD_RE_CHECK:
result = true; result = true;
dprintf(0, "RE_Check=%s", dbg_printf(0, "RE_Check=%s",
pp->range_check_enable ? "Enabled" : "Disabled"); pp->range_check_enable ? "Enabled" : "Disabled");
if (strcasecmp(value, "enabled") == 0) if (strcasecmp(value, "enabled") == 0)
pp->range_check_enable = true; pp->range_check_enable = true;
else else
pp->range_check_enable = false; pp->range_check_enable = false;
dprintf(0, "=>%s\n", dbg_printf(0, "=>%s\n",
pp->range_check_enable ? "Enabled" : "Disabled"); pp->range_check_enable ? "Enabled" : "Disabled");
break; break;
case CMD_RANGE_HIGH: case CMD_RANGE_HIGH:
result = true; result = true;
dprintf(0, "RangeHigh=%g", pp->range_high); dbg_printf(0, "RangeHigh=%g", pp->range_high);
pp->range_high = strtod(value, NULL); pp->range_high = strtod(value, NULL);
if (pp->range_high < 0.0) if (pp->range_high < 0.0)
pp->range_high = 0.0; pp->range_high = 0.0;
dprintf(0, "=>%g\n", pp->range_high); dbg_printf(0, "=>%g\n", pp->range_high);
break; break;
case CMD_RANGE_LOW: case CMD_RANGE_LOW:
result = true; result = true;
dprintf(0, "RangeLow=%g", pp->range_low); dbg_printf(0, "RangeLow=%g", pp->range_low);
pp->range_low = strtod(value, NULL); pp->range_low = strtod(value, NULL);
if (pp->range_low < 0.0) if (pp->range_low < 0.0)
pp->range_low = 0.0; pp->range_low = 0.0;
dprintf(0, "=>%g\n", pp->range_low); dbg_printf(0, "=>%g\n", pp->range_low);
break;
case CMD_SCAN:
result = true;
dprintf(0, "Scan=%d", pp->poll_period);
pp->poll_period = strtol(value, NULL, 10);
if (pp->poll_period < 1)
pp->poll_period = 1;
else if (pp->poll_period > 1000)
pp->poll_period = 1000;
dprintf(0, "=>%d\n", pp->poll_period);
break; break;
case CMD_SAMPLE: case CMD_SAMPLE:
result = true; result = true;
dprintf(0, "Sample=%d", pp->sample_period); dbg_printf(0, "Sample=%d", pp->sample_period);
pp->sample_period = strtol(value, NULL, 10); pp->sample_period = strtol(value, NULL, 10);
if (pp->sample_period < 1) if (pp->sample_period < 1)
pp->sample_period = 1; pp->sample_period = 1;
else if (pp->sample_period > 1000) else if (pp->sample_period > 1000)
pp->sample_period = 1000; pp->sample_period = 1000;
dprintf(0, "=>%d\n", pp->sample_period); dbg_printf(0, "=>%d\n", pp->sample_period);
break;
case CMD_SCAN:
result = true;
dbg_printf(0, "Scan=%d", pp->poll_period);
pp->poll_period = strtol(value, NULL, 10);
if (pp->poll_period < 1)
pp->poll_period = 1;
else if (pp->poll_period > 1000)
pp->poll_period = 1000;
dbg_printf(0, "=>%d\n", pp->poll_period);
break;
case CMD_SOURCE:
result = true;
dbg_printf(0, "Source=%d", pp->source);
pp->source = strtol(value, NULL, 10);
if (pp->source < 1)
pp->source = 0;
else if (pp->source > 1000)
pp->source = 0;
dbg_printf(0, "=>%d\n", pp->source);
break; break;
case CMD_SYNC: case CMD_SYNC:
result = true; result = true;
dprintf(0, "Sync=%s", pp->sync ? "External" : "Internal"); dbg_printf(0, "Sync=%s", pp->sync ? "External" : "Internal");
if (strcasecmp(value, "external") == 0) if (strcasecmp(value, "external") == 0)
pp->sync = true; pp->sync = true;
else else
pp->sync = false; pp->sync = false;
dprintf(0, "=>%s\n", pp->sync ? "External" : "Internal"); dbg_printf(0, "=>%s\n", pp->sync ? "External" : "Internal");
break; break;
case CMD_TERMINAL: case CMD_TERMINAL:
result = true; result = true;
dprintf(0, "Sample=%llu", pp->terminal_count); dbg_printf(0, "Terminal=%llu", pp->terminal_count);
pp->terminal_count = strtoull(value, NULL, 10); pp->terminal_count = strtoull(value, NULL, 10);
dprintf(0, "=>%llu\n", pp->terminal_count); dbg_printf(0, "=>%llu\n", pp->terminal_count);
break; break;
case CMD_TE_CHECK: case CMD_TE_CHECK:
result = true; result = true;
dprintf(0, "TE_Check=%s", dbg_printf(0, "TE_Check=%s",
pp->terminal_check_type == 0 ? "None" : pp->terminal_check_type == 0 ? "None" :
pp->terminal_check_type == 1 ? "Counter" : pp->terminal_check_type == 1 ? "Counter" :
pp->terminal_check_type == 2 ? "Timer" : "Unknown"); pp->terminal_check_type == 2 ? "Timer" : "Unknown");
@@ -205,13 +218,13 @@ bool param_set(pPARAMETERS pp, char* name, char* value)
pp->terminal_check_type = 2; pp->terminal_check_type = 2;
else else
pp->terminal_check_type = 0; pp->terminal_check_type = 0;
dprintf(0, "=>%s\n", dbg_printf(0, "=>%s\n",
pp->terminal_check_type == 0 ? "None" : pp->terminal_check_type == 0 ? "None" :
pp->terminal_check_type == 1 ? "Counter" : pp->terminal_check_type == 1 ? "Counter" :
pp->terminal_check_type == 2 ? "Timer" : "Unknown"); pp->terminal_check_type == 2 ? "Timer" : "Unknown");
break; break;
default: default:
dprintf(0, "Unknown Parameter: \"%s\" = \"%s\"\n", name, value); dbg_printf(0, "Unknown Parameter: \"%s\" = \"%s\"\n", name, value);
break; break;
} }
return result; return result;
@@ -328,17 +341,29 @@ bool param_get_cmd(pPARAMETERS pp, char* cmd, int n)
"GET %s=%g", name, "GET %s=%g", name,
pp->range_low); pp->range_low);
break; break;
case CMD_SAMPLE:
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%d", name,
pp->sample_period);
break;
case CMD_SCAN: case CMD_SCAN:
result = true; result = true;
snprintf(buffer.body, sizeof(buffer.body), snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%d", name, "GET %s=%d", name,
pp->poll_period); pp->poll_period);
break; break;
case CMD_SAMPLE: case CMD_SOURCE:
result = true; result = true;
snprintf(buffer.body, sizeof(buffer.body), snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%d", name, "GET %s=%d", name,
pp->sample_period); pp->source);
break;
case CMD_SYNC:
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%s", name,
pp->sync ? "External" : "Internal");
break; break;
case CMD_TERMINAL: case CMD_TERMINAL:
result = true; result = true;
@@ -364,7 +389,7 @@ bool param_get_cmd(pPARAMETERS pp, char* cmd, int n)
result = true; result = true;
} }
else else
dprintf(0, "Unknown GET Parameter: \"%s\"\n", name); dbg_printf(0, "Unknown GET Parameter: \"%s\"\n", name);
break; break;
} }
if (!result) if (!result)

View File

@@ -6,13 +6,15 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
typedef enum { COUNT_UP = 0, COUNT_DOWN } DIRECTION;
/** /**
* Counter Control Parameters * Counter Control Parameters
*/ */
typedef struct parameter_t typedef struct parameter_t
{ {
/** direction of counting for the logical counter */ /** direction of counting for the logical counter */
enum { COUNT_UP = 0, COUNT_DOWN } direction; DIRECTION direction;
/** counter read period in milliseconds */ /** counter read period in milliseconds */
int poll_period; int poll_period;
/** sample period in number of reads per sample */ /** sample period in number of reads per sample */
@@ -39,6 +41,8 @@ typedef struct parameter_t
int output_line; int output_line;
/** hardware_sync false:internal, true:external */ /** hardware_sync false:internal, true:external */
bool sync; bool sync;
/** source 0:default, 1:timebase_1, 2:timebase_2, 3:timebase_3 */
int source;
} PARAMETERS, *pPARAMETERS; } PARAMETERS, *pPARAMETERS;

View File

@@ -1,7 +1,6 @@
#include "sock.h" #include "sock.h"
#include "utility.h" #include "utility.h"
#include "display.h" #include "display.h"
#include "dio.h"
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
@@ -17,7 +16,7 @@
#include <netinet/ip.h> /* superset of previous */ #include <netinet/ip.h> /* superset of previous */
#define LINE_LEN 1024 #define LINE_LEN 1024
#define MAX_SOCK 50 #define MAX_SOCK 200
/** /**
* Mode of the socket * Mode of the socket
@@ -65,6 +64,9 @@ typedef struct terminal_t
char url[LINE_LEN]; char url[LINE_LEN];
/** value from Content-Length header */ /** value from Content-Length header */
int content_length; int content_length;
/** associated device */
int(*command)(void* device, const char* cmd);
void* device;
} TERMINAL, *pTERMINAL; } TERMINAL, *pTERMINAL;
/** /**
@@ -89,20 +91,25 @@ static int sock_l;
* *
* \param addr the TCP/IP port number on which to listen * \param addr the TCP/IP port number on which to listen
*/ */
void sock_init(int addr) void sock_init(void)
{ {
dprintf(0, "sock_init\n");
int status;
long flags;
int i; int i;
int one = 1; dbg_printf(0, "sock_init\n");
struct sockaddr_in my_addr;
memset(fdv, 0, sizeof(fdv)); memset(fdv, 0, sizeof(fdv));
for (i = 0; i < MAX_SOCK; ++i) for (i = 0; i < MAX_SOCK; ++i)
{ {
fdv[i].fd = -1; fdv[i].fd = -1;
} }
memset(fds, 0, sizeof(fds)); memset(fds, 0, sizeof(fds));
num_fds = 0;
}
void sock_listen(int addr, int (*command)(void* device, const char* cmd), void* device)
{
int status;
long flags;
int one = 1;
struct sockaddr_in my_addr;
status = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); status = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (status < 0) if (status < 0)
{ {
@@ -130,10 +137,12 @@ void sock_init(int addr)
perror("listen"); perror("listen");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
fds[0].fd = sock_l; fds[num_fds].fd = sock_l;
fds[0].events = POLLIN | POLLOUT; fds[num_fds].events = POLLIN | POLLOUT;
fdv[0].fd = sock_l; fdv[num_fds].fd = sock_l;
fdv[0].input = sock_accept; fdv[num_fds].input = sock_accept;
fdv[num_fds].command = command;
fdv[num_fds].device = device;
++num_fds; ++num_fds;
} }
@@ -157,7 +166,7 @@ void sock_check(int timeout)
} }
if (ready == 0) if (ready == 0)
return; return;
dprintf(0, "sock_check, ready=%d\n", ready); dbg_printf(0, "sock_check, ready=%d\n", ready);
for (i = 0; i < MAX_SOCK; ++i) for (i = 0; i < MAX_SOCK; ++i)
{ {
if (fds[i].revents) if (fds[i].revents)
@@ -177,7 +186,7 @@ void sock_check(int timeout)
*/ */
void sock_close(int n) void sock_close(int n)
{ {
dprintf(0, "sock_close\n"); dbg_printf(0, "sock_close\n");
shutdown(fdv[n].fd, SHUT_RDWR); shutdown(fdv[n].fd, SHUT_RDWR);
close(fdv[n].fd); close(fdv[n].fd);
if (n != num_fds) if (n != num_fds)
@@ -198,9 +207,9 @@ void sock_line(int n)
{ {
char *cp = &fdv[n].line[0]; char *cp = &fdv[n].line[0];
char *up; char *up;
dprintf(0, "%3d: %s", fdv[n].state, fdv[n].line); dbg_printf(0, "%3d: %s", fdv[n].state, fdv[n].line);
if (fdv[n].line[fdv[n].line_len - 1] != '\n') if (fdv[n].line[fdv[n].line_len - 1] != '\n')
dprintf(0, "\n"); dbg_printf(0, "\n");
switch(fdv[n].state) switch(fdv[n].state)
{ {
case 0: case 0:
@@ -276,16 +285,7 @@ void sock_line(int n)
put_form(n); put_form(n);
else if (strncasecmp(fdv[n].url, "/cmd=", 5) == 0) else if (strncasecmp(fdv[n].url, "/cmd=", 5) == 0)
{ {
if (strncasecmp(fdv[n].url, "/cmd=pause", 10) == 0) fdv[n].command(fdv[n].device, &fdv[n].url[5]);
cntr_pause(&counter);
else if (strncasecmp(fdv[n].url, "/cmd=continue", 10) == 0)
cntr_resume(&counter);
else if (strncasecmp(fdv[n].url, "/cmd=resume", 10) == 0)
cntr_resume(&counter);
else if (strncasecmp(fdv[n].url, "/cmd=start", 10) == 0)
cntr_start(&counter);
else if (strncasecmp(fdv[n].url, "/cmd=stop", 10) == 0)
cntr_stop(&counter);
put_page_refresh(n); put_page_refresh(n);
} }
else else
@@ -311,14 +311,14 @@ void sock_line(int n)
(cp = strchr(&cp[14], ':'))) (cp = strchr(&cp[14], ':')))
{ {
fdv[n].content_length = atoi(&cp[1]); fdv[n].content_length = atoi(&cp[1]);
dprintf(0, "Content Length = %d\n", fdv[n].content_length); dbg_printf(0, "Content Length = %d\n", fdv[n].content_length);
} }
break; break;
case 4: case 4:
/* /*
* we are scanning the body of a POST * we are scanning the body of a POST
*/ */
dprintf(0, "Content-Length: %d, Line-Length: %d\n", dbg_printf(0, "Content-Length: %d, Line-Length: %d\n",
fdv[n].content_length, fdv[n].line_len); fdv[n].content_length, fdv[n].line_len);
fdv[n].content_length -= fdv[n].line_len; fdv[n].content_length -= fdv[n].line_len;
if (fdv[n].content_length <= 0) if (fdv[n].content_length <= 0)
@@ -338,7 +338,7 @@ void sock_line(int n)
*/ */
if (fdv[n].state == 5) if (fdv[n].state == 5)
{ {
put_form(n); put_form_refresh(n);
fdv[n].state = 0; fdv[n].state = 0;
} }
} }
@@ -350,7 +350,7 @@ void sock_line(int n)
*/ */
void sock_input(int n) void sock_input(int n)
{ {
dprintf(0, "sock_input(%d)\n", n); dbg_printf(0, "sock_input(%d)\n", n);
ssize_t sz; ssize_t sz;
char buffer[1024]; char buffer[1024];
sz = recv(fdv[n].fd, &buffer, sizeof(buffer), 0); sz = recv(fdv[n].fd, &buffer, sizeof(buffer), 0);
@@ -362,9 +362,9 @@ void sock_input(int n)
if (sz < 0) if (sz < 0)
{ {
if (errno == EAGAIN) /* AKA EWOULDBLOCK */ if (errno == EAGAIN) /* AKA EWOULDBLOCK */
dprintf(0, "EAGAIN:"); dbg_printf(0, "EAGAIN:");
else if (errno == ESPIPE) /* Illegal seek (on pipe or socket) */ else if (errno == ESPIPE) /* Illegal seek (on pipe or socket) */
dprintf(0, "ESPIPE:"); dbg_printf(0, "ESPIPE:");
else else
perror("recv"); perror("recv");
return; return;
@@ -392,7 +392,7 @@ void sock_input(int n)
*/ */
void sock_accept(int n) void sock_accept(int n)
{ {
dprintf(0, "sock_accept(%d)\n", n); dbg_printf(0, "sock_accept(%d)\n", n);
int sock_n; int sock_n;
struct sockaddr_in my_addr; struct sockaddr_in my_addr;
socklen_t my_len = sizeof(my_addr); socklen_t my_len = sizeof(my_addr);
@@ -407,6 +407,8 @@ void sock_accept(int n)
fdv[num_fds].fd = sock_n; fdv[num_fds].fd = sock_n;
fdv[num_fds].addr = my_addr; fdv[num_fds].addr = my_addr;
fdv[num_fds].input = sock_input; fdv[num_fds].input = sock_input;
fdv[num_fds].command = fdv[n].command;
fdv[num_fds].device = fdv[n].device;
fdv[num_fds].line_len = 0; fdv[num_fds].line_len = 0;
fdv[num_fds].state = 0; fdv[num_fds].state = 0;
fds[num_fds].fd = sock_n; fds[num_fds].fd = sock_n;
@@ -502,3 +504,8 @@ void sock_err(int n)
buffer.length = strlen(buffer.body); buffer.length = strlen(buffer.body);
sock_send(n, &buffer); sock_send(n, &buffer);
} }
void* sock_device(int n)
{
return fdv[n].device;
}

View File

@@ -2,7 +2,8 @@
#define _SOCK_H_ #define _SOCK_H_
#include "utility.h" #include "utility.h"
void sock_init(int addr); void sock_init(void);
void sock_listen(int addr, int (*method)(void* arg, const char* cmd), void* arg);
void sock_check(int timeout); void sock_check(int timeout);
void sock_close(int n); void sock_close(int n);
void sock_line(int n); void sock_line(int n);
@@ -13,4 +14,5 @@ void sock_report(BUFFER* bp, int match);
void sock_set_match(int n, int match); void sock_set_match(int n, int match);
void sock_ok(int n); void sock_ok(int n);
void sock_err(int n); void sock_err(int n);
void* sock_device(int n);
#endif #endif

View File

@@ -35,7 +35,7 @@ double time_diff(struct timeval* later, struct timeval* earlier)
{ {
double delta = double delta =
((double) later->tv_sec - (double) earlier->tv_sec) ((double) later->tv_sec - (double) earlier->tv_sec)
+ 0.000001 * ((double) later->tv_usec - (double) earlier->tv_usec); + 0.000001 * ((double) later->tv_usec - (double) earlier->tv_usec);
return delta; return delta;
} }
@@ -64,7 +64,7 @@ int time_sub(struct timeval* later, struct timeval* earlier)
int time_cmp(struct timeval* later, struct timeval* earlier) int time_cmp(struct timeval* later, struct timeval* earlier)
{ {
double delta = (1.0 * later->tv_sec + 0.000001 * later->tv_usec) - double delta = (1.0 * later->tv_sec + 0.000001 * later->tv_usec) -
(1.0 * earlier->tv_sec + 0.000001 * earlier->tv_usec); (1.0 * earlier->tv_sec + 0.000001 * earlier->tv_usec);
if (delta == 0.0) if (delta == 0.0)
return 0; return 0;
else if (delta > 0.0) else if (delta > 0.0)
@@ -95,8 +95,10 @@ void time_adv(struct timeval* tmr, int msec)
#define DEBUG_FILE "debug_file.txt" #define DEBUG_FILE "debug_file.txt"
#include <stdarg.h> #include <stdarg.h>
#ifndef dprintf #ifndef dbg_printf
static int debug_level = -1; static int debug_level = -1;
static bool new_line = true;
int set_debug_level(int new_level) int set_debug_level(int new_level)
{ {
int temp = debug_level; int temp = debug_level;
@@ -104,11 +106,12 @@ int set_debug_level(int new_level)
return temp; return temp;
} }
int dprintf(int level, const char* format, ...) int dbg_printf(int level, const char* format, ...)
{ {
int result = 0; int result = 0;
int iRet = 0; int iRet = 0;
va_list ap; va_list ap;
char buffer[2048];
if (level <= debug_level) if (level <= debug_level)
{ {
@@ -116,11 +119,19 @@ int dprintf(int level, const char* format, ...)
if (debug_file) if (debug_file)
{ {
va_start(ap, format); va_start(ap, format);
iRet = fprintf(debug_file, "%s ", make_timestamp(NULL)); iRet = vsnprintf(buffer, sizeof(buffer), format, ap);
result = iRet;
iRet = vfprintf(debug_file, format, ap);
result += iRet;
va_end(ap); va_end(ap);
if (iRet > 0)
{
if (new_line)
result = fprintf(debug_file, "%s ", make_timestamp(NULL));
result += iRet;
fputs(buffer, debug_file);
if (buffer[iRet - 1] == '\n')
new_line = true;
else
new_line = false;
}
fclose(debug_file); fclose(debug_file);
} }
} }

View File

@@ -6,7 +6,11 @@
#define _UTILITY_H_ #define _UTILITY_H_
#include <sys/time.h> #include <sys/time.h>
#ifdef __cplusplus
#else
#include <stdbool.h> #include <stdbool.h>
#endif
typedef unsigned long long uint64; typedef unsigned long long uint64;
/** /**
@@ -43,8 +47,9 @@ int time_cmp(struct timeval* later, struct timeval* earlier);
void time_adv(struct timeval* tmr, int msec); void time_adv(struct timeval* tmr, int msec);
#if 1 #if 1
int set_debug_level(int new_level); int set_debug_level(int new_level);
int dprintf(int level, const char* format, ...); int dbg_printf(int level, const char* format, ...);
#else #else
#define dprintf fprintf #define dbg_printf fprintf
#endif #endif
#endif #endif