Make C++ compiler friendly and handle all eight counters in one process.
r1171 | dcl | 2006-10-24 15:44:25 +1000 (Tue, 24 Oct 2006) | 2 lines
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
SHELL = /bin/sh
|
||||
|
||||
CC = gcc
|
||||
CXX = g++
|
||||
LD = $(CXX)
|
||||
LIBS = nidaqmx
|
||||
LIBFLAGS = -l$(LIBS)
|
||||
TARGET = Monitor
|
||||
@@ -11,6 +14,7 @@ CDEBUG = -ggdb3 -Wall
|
||||
LDFLAGS += -g
|
||||
|
||||
CFLAGS += $(CDEBUG)
|
||||
CXXFLAGS += $(CDEBUG)
|
||||
|
||||
all: $(TARGET) $(PULSER)
|
||||
|
||||
@@ -18,7 +22,7 @@ clean:
|
||||
rm -f $(OBJS) $(TARGET) $(PULSER).o $(PULSER) core
|
||||
|
||||
$(TARGET) : $(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 cntr.h
|
||||
|
||||
@@ -35,4 +39,9 @@ sock.o: sock.c sock.h utility.h display.h cntr.h
|
||||
utility.o: utility.c utility.h
|
||||
|
||||
$(PULSER) : $(PULSER).o
|
||||
$(CC) $(LDFLAGS) -o $@ $(PULSER).o $(LIBFLAGS) -ggdb3
|
||||
$(LD) $(LDFLAGS) -o $@ $(PULSER).o $(LIBFLAGS) -ggdb3
|
||||
|
||||
%.o : %.c
|
||||
$(CXX) -c -o $@ $< $(CXXFLAGS)
|
||||
|
||||
# vim: noexpandtab ts=8 sts=8
|
||||
|
||||
@@ -54,8 +54,15 @@
|
||||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
int usage(int argc, char* argv[])
|
||||
#define MAX_COUNTERS 8
|
||||
|
||||
int usage(int argc, char* argv[], const char* reason)
|
||||
{
|
||||
int i;
|
||||
fprintf(stderr, "%s", argv[0]);
|
||||
for (i = 1; i < argc; ++i)
|
||||
fprintf(stderr, " %s", argv[i]);
|
||||
fprintf(stderr, "\n%s\n", reason);
|
||||
fprintf(stderr, "usage: %s <DEVn/CTRn> <PORT>\n",
|
||||
argv[0]);
|
||||
return EXIT_FAILURE;
|
||||
@@ -68,11 +75,13 @@ int main(int argc, char* argv[])
|
||||
struct timeval now;
|
||||
uint64 last_poll;
|
||||
char* device = DEFAULT_COUNTER_DEVICE;
|
||||
uint dev_no;
|
||||
int port = DEFAULT_LISTEN_PORT;
|
||||
int idx = 1;
|
||||
COUNTER* counters[MAX_COUNTERS];
|
||||
|
||||
if (idx >= argc)
|
||||
return usage(argc, argv);
|
||||
return usage(argc, argv, "no args");
|
||||
if (argv[idx][0] == '-')
|
||||
{
|
||||
if (tolower(argv[idx][1]) == 'd')
|
||||
@@ -86,7 +95,7 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
|
||||
if (idx >= argc)
|
||||
return usage(argc, argv);
|
||||
return usage(argc, argv, "no device");
|
||||
if (tolower(argv[idx][0]) == 'd' &&
|
||||
tolower(argv[idx][1]) == 'e' &&
|
||||
tolower(argv[idx][2]) == 'v' &&
|
||||
@@ -95,38 +104,56 @@ int main(int argc, char* argv[])
|
||||
device = argv[idx];
|
||||
++idx;
|
||||
}
|
||||
else
|
||||
return usage(argc, argv);
|
||||
|
||||
if (idx >= argc)
|
||||
return usage(argc, argv);
|
||||
if (isdigit(argv[idx][0]))
|
||||
else if (tolower(argv[idx][0]) == 'p' &&
|
||||
tolower(argv[idx][1]) == 'x' &&
|
||||
tolower(argv[idx][2]) == 'i' &&
|
||||
isdigit(argv[idx][3]))
|
||||
{
|
||||
port = atoi(argv[idx]);
|
||||
device = argv[idx];
|
||||
++idx;
|
||||
}
|
||||
else
|
||||
return usage(argc, argv);
|
||||
return usage(argc, argv, "bad device");
|
||||
|
||||
memset(&counter, 0, sizeof(counter));
|
||||
if (idx >= argc)
|
||||
return usage(argc, argv, "no port");
|
||||
if (isdigit(argv[idx][0]))
|
||||
{
|
||||
port = 10 * (atoi(argv[idx]) / 10);
|
||||
++idx;
|
||||
}
|
||||
else
|
||||
return usage(argc, argv, "bad port");
|
||||
|
||||
CNTR_CHK(cntr_init(&counter, device));
|
||||
// CNTR_CHK(cntr_start(&counter));
|
||||
sock_init(port);
|
||||
dev_no = atoi(&device[3]);
|
||||
sock_init();
|
||||
gettimeofday(&now, NULL);
|
||||
for (idx = 0; idx < MAX_COUNTERS; ++idx)
|
||||
{
|
||||
sprintf(device, "dev%d/ctr%d", dev_no, idx);
|
||||
CNTR_CHK(cntr_init(&counters[idx], device));
|
||||
sock_listen(port + idx, cntr_command, counters[idx]);
|
||||
counters[idx]->current_time = now;
|
||||
}
|
||||
|
||||
printf("Continuously polling. Press Ctrl+C to interrupt\n");
|
||||
gettimeofday(&now, NULL);
|
||||
last_poll = 1000 * (uint64) now.tv_sec + now.tv_usec / 1000;
|
||||
while (1)
|
||||
{
|
||||
COUNTER* cp = &counter;
|
||||
PARAMETERS* pp = &cp->params;
|
||||
uint64 timeofday;
|
||||
int timeout = 0;
|
||||
int next_timeout = 0;
|
||||
do {
|
||||
next_timeout = 1000;
|
||||
sock_check(timeout);
|
||||
gettimeofday(&now, NULL);
|
||||
timeofday = 1000 * (uint64) now.tv_sec + now.tv_usec / 1000;
|
||||
for (idx = 0; idx < MAX_COUNTERS; ++idx)
|
||||
{
|
||||
COUNTER* cp = counters[idx];
|
||||
PARAMETERS* pp = &cp->params;
|
||||
last_poll = 1000 * (uint64) cp->current_time.tv_sec
|
||||
+ cp->current_time.tv_usec / 1000;
|
||||
if (timeofday / pp->poll_period > last_poll / pp->poll_period)
|
||||
timeout = 0;
|
||||
else
|
||||
@@ -135,15 +162,18 @@ int main(int argc, char* argv[])
|
||||
if (timeout < 0)
|
||||
{
|
||||
if (timeout < 0)
|
||||
dprintf(0, "Poll timeout < 0 at %d\n", timeout);
|
||||
dbg_printf(0, "Poll timeout < 0 at %d\n", timeout);
|
||||
timeout = 0;
|
||||
}
|
||||
}
|
||||
//dprintf(0, "Poll timeout = %d\n", timeout);
|
||||
} while (timeout > 0);
|
||||
if (timeout < next_timeout)
|
||||
next_timeout = timeout;
|
||||
if (timeout == 0)
|
||||
{
|
||||
cp->current_time = now;
|
||||
#if 1
|
||||
dprintf(0, "-%s %s %.3f %s %.3f %4d\n",
|
||||
dbg_printf(0, "%d:-%s %s %.3f %s %.3f %4d\n",
|
||||
idx,
|
||||
make_timestamp(&cp->current_time),
|
||||
make_timestamp(&cp->sample_timer),
|
||||
cntr_time_to_next_sample(cp),
|
||||
@@ -152,7 +182,13 @@ int main(int argc, char* argv[])
|
||||
cp->sample_index);
|
||||
#endif
|
||||
CNTR_CHK(cntr_poll(cp));
|
||||
last_poll = timeofday;
|
||||
}
|
||||
}
|
||||
if (next_timeout > 999)
|
||||
next_timeout = 1;
|
||||
timeout = next_timeout;
|
||||
//dbg_printf(0, "Poll timeout = %d\n", timeout);
|
||||
} while (timeout > 0);
|
||||
}
|
||||
|
||||
Error:
|
||||
@@ -162,7 +198,8 @@ Error:
|
||||
cntr_errmsg(errBuff, sizeof(errBuff));
|
||||
printf("DAQmx Error: %s\n", errBuff);
|
||||
}
|
||||
cntr_term(&counter);
|
||||
for (idx = 0; idx < MAX_COUNTERS; ++idx)
|
||||
cntr_term(counters[idx]);
|
||||
printf("End of program\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -36,7 +36,7 @@ static SAMPLE* prv_sample(COUNTER* cp, int num)
|
||||
|
||||
void make_report(COUNTER* cp)
|
||||
{
|
||||
dprintf(0, "make_report\n");
|
||||
dbg_printf(0, "make_report\n");
|
||||
int i;
|
||||
SAMPLE* sp = cur_sample(cp);
|
||||
cp->report = *sp;
|
||||
@@ -156,7 +156,7 @@ void cntr_print(COUNTER* cp, FILE* fd)
|
||||
void cntr_sample(COUNTER* cp)
|
||||
{
|
||||
SAMPLE* psp = cur_sample(cp);
|
||||
dprintf(0, "cntr_sample: %4d\r\n"
|
||||
dbg_printf(0, "cntr_sample: %4d\r\n"
|
||||
" polls: %4d\r\n"
|
||||
" time: %4s\r\n"
|
||||
" counter: %10llu\r\n"
|
||||
@@ -187,7 +187,7 @@ void cntr_sample(COUNTER* cp)
|
||||
|
||||
void cntr_report(COUNTER* cp)
|
||||
{
|
||||
dprintf(0, "cntr_report\n");
|
||||
dbg_printf(0, "cntr_report\n");
|
||||
/*
|
||||
* Set the time for this report
|
||||
*/
|
||||
@@ -229,10 +229,13 @@ void cntr_report(COUNTER* cp)
|
||||
*
|
||||
* Create a 64-bit physical counter and start it.
|
||||
*/
|
||||
int cntr_init(COUNTER* cp, char* name)
|
||||
int cntr_init(COUNTER** cpp, char* name)
|
||||
{
|
||||
int error = 0;
|
||||
char errBuff[2048]={'\0'};
|
||||
SAMPLE* sp = NULL;
|
||||
COUNTER* cp = (COUNTER*) malloc(sizeof(COUNTER));
|
||||
*cpp = cp;
|
||||
memset(cp, 0, sizeof(COUNTER));
|
||||
strncpy(cp->name, name, sizeof(cp->name));
|
||||
cp->params.poll_period = 1000; /* milliseconds between polls */
|
||||
@@ -246,7 +249,7 @@ int cntr_init(COUNTER* cp, char* name)
|
||||
cp->sample_timer = now;
|
||||
cp->report_timer = now;
|
||||
HCTR_TEST(hctr_ctor(name, &cp->private_data));
|
||||
SAMPLE* sp = cur_sample(cp);
|
||||
sp = cur_sample(cp);
|
||||
sp->timestamp = now;
|
||||
sp->counter_value = cp->current_count;
|
||||
sp->valid = true;
|
||||
@@ -270,6 +273,8 @@ int cntr_start(COUNTER *cp)
|
||||
char errBuff[2048]={'\0'};
|
||||
struct timeval now;
|
||||
int value;
|
||||
SAMPLE* sp = NULL;
|
||||
SAMPLE* psp = NULL;
|
||||
PARAMETERS* pp = &cp->params;
|
||||
/* start the counter object */
|
||||
gettimeofday(&now, NULL);
|
||||
@@ -284,10 +289,10 @@ int cntr_start(COUNTER *cp)
|
||||
cp->state = counter_running;
|
||||
cp->previous_time = cp->current_time;
|
||||
HCTR_TEST(hctr_read(cp->private_data, &cp->count64));
|
||||
SAMPLE* sp = cur_sample(cp);
|
||||
sp = cur_sample(cp);
|
||||
sp->timestamp = cp->current_time;
|
||||
sp->counter_value = cp->current_count;
|
||||
SAMPLE* psp = prv_sample(cp, 1);
|
||||
psp = prv_sample(cp, 1);
|
||||
psp->timestamp = cp->current_time;
|
||||
psp->counter_value = cp->current_count;
|
||||
make_report(cp);
|
||||
@@ -297,7 +302,7 @@ int cntr_start(COUNTER *cp)
|
||||
value = 0;
|
||||
else
|
||||
value = -1;
|
||||
dprintf(0, "Setting output line to %d\n", value);
|
||||
dbg_printf(0, "Setting output line to %d\n", value);
|
||||
HCTR_TEST(hctr_outp(cp->private_data, value));
|
||||
return error;
|
||||
Error:
|
||||
@@ -320,7 +325,7 @@ int cntr_stop(COUNTER *cp)
|
||||
value = 1;
|
||||
else
|
||||
value = -1;
|
||||
dprintf(0, "Setting output line to %d\n", value);
|
||||
dbg_printf(0, "Setting output line to %d\n", value);
|
||||
HCTR_TEST(hctr_outp(cp->private_data, value));
|
||||
return error;
|
||||
Error:
|
||||
@@ -343,6 +348,22 @@ int cntr_resume(COUNTER *cp)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int cntr_command(void* counter, const char* command)
|
||||
{
|
||||
COUNTER* cp = static_cast<COUNTER*>(counter);
|
||||
if (strncasecmp(command, "pause", 5) == 0)
|
||||
return cntr_pause(cp);
|
||||
else if (strncasecmp(command, "continue", 8) == 0)
|
||||
return cntr_resume(cp);
|
||||
else if (strncasecmp(command, "resume", 6) == 0)
|
||||
return cntr_resume(cp);
|
||||
else if (strncasecmp(command, "start", 6) == 0)
|
||||
return cntr_start(cp);
|
||||
else if (strncasecmp(command, "stop", 4) == 0)
|
||||
return cntr_stop(cp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void cntr_event(COUNTER *cp, char* event)
|
||||
{
|
||||
BUFFER buffer;
|
||||
@@ -350,7 +371,7 @@ static void cntr_event(COUNTER *cp, char* event)
|
||||
event,
|
||||
make_timestamp(&cp->current_time));
|
||||
buffer.length = strlen(buffer.body);
|
||||
dprintf(0, "%s", buffer.body);
|
||||
dbg_printf(0, "%s", buffer.body);
|
||||
//sock_report(&buffer, 0);
|
||||
sock_report(&buffer, 1);
|
||||
sock_report(&buffer, 2);
|
||||
@@ -457,7 +478,7 @@ static void cntr_test_term(COUNTER* cp)
|
||||
}
|
||||
else if (pp->terminal_check_type == 2)
|
||||
{
|
||||
if (cp->accumulated.tv_sec >= pp->terminal_count)
|
||||
if ((uint64) cp->accumulated.tv_sec >= pp->terminal_count)
|
||||
cp->terminal_due = true;
|
||||
}
|
||||
if (cp->terminal_due)
|
||||
@@ -479,16 +500,18 @@ int cntr_poll(COUNTER* cp)
|
||||
unsigned long long current_count_local;
|
||||
int count_delta_local;
|
||||
int error=0;
|
||||
SAMPLE* sp = NULL;
|
||||
SAMPLE* psp = NULL;
|
||||
|
||||
/* read the value from the hardware counter to a temp */
|
||||
++cp->poll_counter;
|
||||
HCTR_TEST(hctr_read(cp->private_data, ¤t_count_local));
|
||||
dprintf(0, "cntr_poll = %llu @ %s\n",
|
||||
dbg_printf(0, "cntr_poll = %llu @ %s\n",
|
||||
current_count_local,
|
||||
make_timestamp(&cp->current_time));
|
||||
|
||||
SAMPLE* sp = cur_sample(cp);
|
||||
SAMPLE* psp = prv_sample(cp, 1);
|
||||
sp = cur_sample(cp);
|
||||
psp = prv_sample(cp, 1);
|
||||
|
||||
/* calculate the number since last time and save new value */
|
||||
count_delta_local = current_count_local - cp->count64;
|
||||
|
||||
@@ -96,23 +96,23 @@ typedef struct counter_t
|
||||
struct counter_private_t* private_data;
|
||||
} COUNTER, *pCOUNTER;
|
||||
|
||||
extern COUNTER counter;
|
||||
|
||||
void make_report(COUNTER* cp);
|
||||
void cntr_sample(COUNTER* cp);
|
||||
void cntr_send(COUNTER* cp, int n);
|
||||
void cntr_read(COUNTER* cp, int n);
|
||||
void cntr_print(COUNTER* cp, FILE* fd);
|
||||
void cntr_report(COUNTER* cp);
|
||||
int cntr_init(COUNTER* cp, char* name);
|
||||
int cntr_start(COUNTER *cp);
|
||||
int cntr_stop(COUNTER *cp);
|
||||
int cntr_pause(COUNTER *cp);
|
||||
int cntr_resume(COUNTER *cp);
|
||||
int cntr_init(COUNTER** cp, char* name);
|
||||
int cntr_start(COUNTER* cp);
|
||||
int cntr_stop(COUNTER* cp);
|
||||
int cntr_pause(COUNTER* cp);
|
||||
int cntr_resume(COUNTER* cp);
|
||||
int cntr_command(void* cp, const char* cmd);
|
||||
int cntr_poll(COUNTER* cp);
|
||||
void cntr_term(COUNTER* cp);
|
||||
bool cntr_fatal(int error);
|
||||
void cntr_errmsg(char* buff, int len);
|
||||
double cntr_time_to_next_sample(COUNTER* cp);
|
||||
double cntr_time_to_next_report(COUNTER* cp);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -159,10 +159,11 @@ static void add_counter(BUFFER* buffer, char* title, char* name, uint64 value)
|
||||
*/
|
||||
void put_form(int n)
|
||||
{
|
||||
dprintf(0, "put_form\n");
|
||||
dbg_printf(0, "put_form\n");
|
||||
BUFFER html;
|
||||
BUFFER buffer;
|
||||
PARAMETERS* pp = &counter.params;
|
||||
COUNTER* cp = (COUNTER*) sock_device(n);
|
||||
PARAMETERS* pp = &cp->params;
|
||||
char *bp;
|
||||
buffer.length = 0;
|
||||
bp = &buffer.body[buffer.length];
|
||||
@@ -237,7 +238,7 @@ void put_form(int n)
|
||||
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];
|
||||
snprintf(bp, sizeof(buffer->body) - buffer->length,
|
||||
@@ -313,17 +314,17 @@ static void show_time(BUFFER* buffer,
|
||||
*/
|
||||
void put_page(int n)
|
||||
{
|
||||
dprintf(0, "put_page\n");
|
||||
dbg_printf(0, "put_page\n");
|
||||
BUFFER html;
|
||||
BUFFER buffer;
|
||||
COUNTER* cp = &counter;
|
||||
PARAMETERS* pp = &counter.params;
|
||||
COUNTER* cp = (COUNTER*) sock_device(n);
|
||||
PARAMETERS* pp = &cp->params;
|
||||
SAMPLE* sp;
|
||||
char *bp;
|
||||
int refresh;
|
||||
#if 1
|
||||
{
|
||||
refresh = cntr_time_to_next_report(cp) + 1;
|
||||
refresh = (int) cntr_time_to_next_report(cp) + 1;
|
||||
}
|
||||
#else
|
||||
make_report(cp);
|
||||
@@ -431,54 +432,55 @@ void put_page(int n)
|
||||
*/
|
||||
void process_form(int n, BUFFER* bp)
|
||||
{
|
||||
dprintf(0, "process_form\n");
|
||||
dbg_printf(0, "process_form\n");
|
||||
int i, j;
|
||||
int state;
|
||||
char name[80];
|
||||
char value[80];
|
||||
char hex_str[3];
|
||||
unsigned int hex_val;
|
||||
char* cp;
|
||||
char* tp;
|
||||
COUNTER* cp = (COUNTER*) sock_device(n);
|
||||
state = 0;
|
||||
j = 0;
|
||||
for (i = 0; i < bp->length; ++i)
|
||||
{
|
||||
cp = &bp->body[i];
|
||||
tp = &bp->body[i];
|
||||
if (state == 0)
|
||||
{
|
||||
if (*cp == '=')
|
||||
if (*tp == '=')
|
||||
{
|
||||
name[j++] = '\0';
|
||||
j = 0;
|
||||
state = 1;
|
||||
}
|
||||
else if (j < 80 - 1)
|
||||
name[j++] = tolower(*cp);
|
||||
name[j++] = tolower(*tp);
|
||||
}
|
||||
else if (state == 1)
|
||||
{
|
||||
if (*cp == '&')
|
||||
if (*tp == '&')
|
||||
{
|
||||
value[j++] = '\0';
|
||||
j = 0;
|
||||
state = 9;
|
||||
}
|
||||
else if (*cp == '%')
|
||||
else if (*tp == '%')
|
||||
{
|
||||
hex_str[0] = '\0';
|
||||
state = 2;
|
||||
}
|
||||
else if (j < 80 - 1)
|
||||
value[j++] = tolower(*cp);
|
||||
value[j++] = tolower(*tp);
|
||||
}
|
||||
else if (state == 2)
|
||||
{
|
||||
hex_str[0] = tolower(*cp);
|
||||
hex_str[0] = tolower(*tp);
|
||||
state = 3;
|
||||
}
|
||||
else if (state == 3)
|
||||
{
|
||||
hex_str[1] = tolower(*cp);
|
||||
hex_str[1] = tolower(*tp);
|
||||
hex_str[2] = '\0';
|
||||
hex_val = strtoul(hex_str, NULL, 16);
|
||||
if (hex_val < ' ' || hex_val > '~')
|
||||
@@ -489,8 +491,8 @@ void process_form(int n, BUFFER* bp)
|
||||
}
|
||||
if (state == 9)
|
||||
{
|
||||
dprintf(0, "name=\"%s\", value=\"%s\"\n", name, value);
|
||||
if (param_set(&counter.params, name, value))
|
||||
dbg_printf(0, "name=\"%s\", value=\"%s\"\n", name, value);
|
||||
if (param_set(&cp->params, name, value))
|
||||
;
|
||||
else if (strcmp(name, "xxx"))
|
||||
{
|
||||
@@ -510,19 +512,19 @@ void process_form(int n, BUFFER* bp)
|
||||
*/
|
||||
void put_page_refresh(int n)
|
||||
{
|
||||
dprintf(0, "put_page_refresh\n");
|
||||
dbg_printf(0, "put_page_refresh\n");
|
||||
BUFFER html;
|
||||
BUFFER buffer;
|
||||
char* bp;
|
||||
COUNTER* cp = &counter;
|
||||
COUNTER* cp = (COUNTER*) sock_device(n);
|
||||
|
||||
int refresh;
|
||||
#if 1
|
||||
{
|
||||
refresh = cntr_time_to_next_report(cp) + 1;
|
||||
refresh = (int) cntr_time_to_next_report(cp) + 1;
|
||||
}
|
||||
#else
|
||||
PARAMETERS* pp = &counter.params;
|
||||
PARAMETERS* pp = &cp->params;
|
||||
make_report(cp);
|
||||
refresh = (pp->poll_period * pp->sample_period * pp->report_period + 500) / 1000;
|
||||
#endif
|
||||
@@ -563,19 +565,19 @@ void put_page_refresh(int n)
|
||||
*/
|
||||
void put_form_refresh(int n)
|
||||
{
|
||||
dprintf(0, "put_form_refresh\n");
|
||||
dbg_printf(0, "put_form_refresh\n");
|
||||
BUFFER html;
|
||||
BUFFER buffer;
|
||||
char* bp;
|
||||
COUNTER* cp = &counter;
|
||||
COUNTER* cp = (COUNTER*) sock_device(n);
|
||||
|
||||
int refresh;
|
||||
#if 1
|
||||
{
|
||||
refresh = cntr_time_to_next_report(cp) + 1;
|
||||
refresh = (int) cntr_time_to_next_report(cp) + 1;
|
||||
}
|
||||
#else
|
||||
PARAMETERS* pp = &counter.params;
|
||||
PARAMETERS* pp = &cp->params;
|
||||
make_report(cp);
|
||||
refresh = (pp->poll_period * pp->sample_period * pp->report_period + 500) / 1000;
|
||||
#endif
|
||||
@@ -616,47 +618,48 @@ void put_form_refresh(int n)
|
||||
*/
|
||||
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);
|
||||
bool sics = false;
|
||||
int error = 1;
|
||||
char command[80];
|
||||
char param[80];
|
||||
int len = 0;
|
||||
char* cp = bp->body;
|
||||
while (isspace(*cp))
|
||||
++cp;
|
||||
char* tp = bp->body;
|
||||
COUNTER* cp = static_cast<COUNTER*>(sock_device(n));
|
||||
while (isspace(*tp))
|
||||
++tp;
|
||||
len = 0;
|
||||
while (*cp && !isspace(*cp))
|
||||
while (*tp && !isspace(*tp))
|
||||
{
|
||||
if (len < 80 - 1)
|
||||
command[len++] = *cp;
|
||||
++cp;
|
||||
command[len++] = *tp;
|
||||
++tp;
|
||||
}
|
||||
command[len] = '\0';
|
||||
if (strcasecmp(command, "SICS") == 0)
|
||||
{
|
||||
sics = true;
|
||||
while (isspace(*cp))
|
||||
++cp;
|
||||
while (isspace(*tp))
|
||||
++tp;
|
||||
len = 0;
|
||||
while (*cp && !isspace(*cp))
|
||||
while (*tp && !isspace(*tp))
|
||||
{
|
||||
if (len < 80 - 1)
|
||||
command[len++] = *cp;
|
||||
++cp;
|
||||
command[len++] = *tp;
|
||||
++tp;
|
||||
}
|
||||
command[len] = '\0';
|
||||
}
|
||||
while (isspace(*cp))
|
||||
++cp;
|
||||
while (isspace(*tp))
|
||||
++tp;
|
||||
if (strcasecmp(command, "START") == 0)
|
||||
error = cntr_start(&counter);
|
||||
error = cntr_start(cp);
|
||||
else if (strcasecmp(command, "STOP") == 0)
|
||||
error = cntr_stop(&counter);
|
||||
error = cntr_stop(cp);
|
||||
else if (strcasecmp(command, "PAUSE") == 0)
|
||||
error = cntr_pause(&counter);
|
||||
error = cntr_pause(cp);
|
||||
else if (strcasecmp(command, "RESUME") == 0)
|
||||
error = cntr_resume(&counter);
|
||||
error = cntr_resume(cp);
|
||||
else if (strcasecmp(command, "REPORT") == 0)
|
||||
{
|
||||
int match = 1;
|
||||
@@ -664,11 +667,11 @@ void process_command(int n, BUFFER* bp)
|
||||
if (sics)
|
||||
match = 2;
|
||||
len = 0;
|
||||
while (*cp && !isspace(*cp))
|
||||
while (*tp && !isspace(*tp))
|
||||
{
|
||||
if (len < 80 - 1)
|
||||
param[len++] = *cp;
|
||||
++cp;
|
||||
param[len++] = *tp;
|
||||
++tp;
|
||||
}
|
||||
param[len] = '\0';
|
||||
if (strcasecmp(param, "OFF") == 0)
|
||||
@@ -678,24 +681,24 @@ void process_command(int n, BUFFER* bp)
|
||||
else if (strcasecmp(command, "SET") == 0)
|
||||
{
|
||||
/* set parameter */
|
||||
dprintf(0, "SET %s\n", cp);
|
||||
if (param_set_cmd(&counter.params, cp))
|
||||
dbg_printf(0, "SET %s\n", tp);
|
||||
if (param_set_cmd(&cp->params, tp))
|
||||
error = 0;
|
||||
}
|
||||
else if (strcasecmp(command, "GET") == 0)
|
||||
{
|
||||
/* get parameter */
|
||||
param_get_cmd(&counter.params, cp, n);
|
||||
param_get_cmd(&cp->params, tp, n);
|
||||
return;
|
||||
}
|
||||
else if (strcasecmp(command, "READ") == 0)
|
||||
{
|
||||
cntr_read(&counter, n);
|
||||
cntr_read(cp, n);
|
||||
return;
|
||||
}
|
||||
else if (!sics)
|
||||
{
|
||||
cntr_send(&counter, n);
|
||||
cntr_send(cp, n);
|
||||
return;
|
||||
}
|
||||
if (error == 0)
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#define DAQmxErrChk(functionCall) \
|
||||
do { if( DAQmxFailed(error=(functionCall)) ) \
|
||||
goto Error; } while(0);
|
||||
goto Error; } while(0)
|
||||
|
||||
/**
|
||||
* This structure encapsulates the data that is private to
|
||||
@@ -159,6 +159,7 @@ int make_dout_task(pHCTR ptr)
|
||||
// Start the DAQmx task
|
||||
/*********************************************/
|
||||
DAQmxErrChk (DAQmxStartTask(ptr->taskHandle_dout));
|
||||
return error;
|
||||
|
||||
Error:
|
||||
return error;
|
||||
|
||||
@@ -9,7 +9,10 @@
|
||||
#ifndef _HCTR_H_
|
||||
#define _HCTR_H_
|
||||
|
||||
#ifdef __cplusplus
|
||||
#else
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
struct counter_private_t;
|
||||
typedef struct counter_private_t* pHCTR;
|
||||
|
||||
@@ -53,7 +53,7 @@ static struct param_command_t {
|
||||
{CMD_SYNC, TXT_SYNC},
|
||||
{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)
|
||||
{
|
||||
@@ -74,22 +74,22 @@ bool param_set(pPARAMETERS pp, char* name, char* value)
|
||||
{
|
||||
case CMD_DIRECTION:
|
||||
result = true;
|
||||
dprintf(0, "Direction=%s", pp->direction ? "Down" : "Up");
|
||||
dbg_printf(0, "Direction=%s", pp->direction ? "Down" : "Up");
|
||||
if (strcasecmp(value, "down") == 0)
|
||||
pp->direction = COUNT_DOWN;
|
||||
else
|
||||
pp->direction = COUNT_UP;
|
||||
dprintf(0, "=>%s\n", pp->direction ? "Down" : "Up");
|
||||
dbg_printf(0, "=>%s\n", pp->direction ? "Down" : "Up");
|
||||
break;
|
||||
case CMD_INITIAL:
|
||||
result = true;
|
||||
dprintf(0, "Initial=%llu", pp->initial_count);
|
||||
dbg_printf(0, "Initial=%llu", pp->initial_count);
|
||||
pp->initial_count = strtoull(value, NULL, 10);
|
||||
dprintf(0, "=>%llu\n", pp->initial_count);
|
||||
dbg_printf(0, "=>%llu\n", pp->initial_count);
|
||||
break;
|
||||
case CMD_OUTPUT:
|
||||
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");
|
||||
if (strcasecmp(value, "enabled") == 0)
|
||||
pp->output_line = 1;
|
||||
@@ -97,105 +97,105 @@ bool param_set(pPARAMETERS pp, char* name, char* value)
|
||||
pp->output_line = 2;
|
||||
else
|
||||
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");
|
||||
break;
|
||||
case CMD_REPORT:
|
||||
result = true;
|
||||
dprintf(0, "Report=%d", pp->report_period);
|
||||
dbg_printf(0, "Report=%d", pp->report_period);
|
||||
pp->report_period = strtol(value, NULL, 10);
|
||||
if (pp->report_period < 1)
|
||||
pp->report_period = 1;
|
||||
else if (pp->report_period > 1000)
|
||||
pp->report_period = 1000;
|
||||
dprintf(0, "=>%d\n", pp->report_period);
|
||||
dbg_printf(0, "=>%d\n", pp->report_period);
|
||||
break;
|
||||
case CMD_RANGE_MODE:
|
||||
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);
|
||||
if (pp->range_mode < 0)
|
||||
pp->range_mode = 0;
|
||||
else if (pp->range_mode > 2)
|
||||
pp->range_mode = 0;
|
||||
dprintf(0, "=>%d\n", pp->range_mode);
|
||||
dbg_printf(0, "=>%d\n", pp->range_mode);
|
||||
break;
|
||||
case CMD_RANGE_GATE:
|
||||
result = true;
|
||||
dprintf(0, "range_gate=%s",
|
||||
dbg_printf(0, "range_gate=%s",
|
||||
pp->range_gate_enable ? "Enabled" : "Disabled");
|
||||
if (strcasecmp(value, "enabled") == 0)
|
||||
pp->range_gate_enable = true;
|
||||
else
|
||||
pp->range_gate_enable = false;
|
||||
dprintf(0, "=>%s\n",
|
||||
dbg_printf(0, "=>%s\n",
|
||||
pp->range_gate_enable ? "Enabled" : "Disabled");
|
||||
break;
|
||||
case CMD_RE_CHECK:
|
||||
result = true;
|
||||
dprintf(0, "RE_Check=%s",
|
||||
dbg_printf(0, "RE_Check=%s",
|
||||
pp->range_check_enable ? "Enabled" : "Disabled");
|
||||
if (strcasecmp(value, "enabled") == 0)
|
||||
pp->range_check_enable = true;
|
||||
else
|
||||
pp->range_check_enable = false;
|
||||
dprintf(0, "=>%s\n",
|
||||
dbg_printf(0, "=>%s\n",
|
||||
pp->range_check_enable ? "Enabled" : "Disabled");
|
||||
break;
|
||||
case CMD_RANGE_HIGH:
|
||||
result = true;
|
||||
dprintf(0, "RangeHigh=%g", pp->range_high);
|
||||
dbg_printf(0, "RangeHigh=%g", pp->range_high);
|
||||
pp->range_high = strtod(value, NULL);
|
||||
if (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;
|
||||
case CMD_RANGE_LOW:
|
||||
result = true;
|
||||
dprintf(0, "RangeLow=%g", pp->range_low);
|
||||
dbg_printf(0, "RangeLow=%g", pp->range_low);
|
||||
pp->range_low = strtod(value, NULL);
|
||||
if (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);
|
||||
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;
|
||||
dprintf(0, "=>%d\n", pp->poll_period);
|
||||
dbg_printf(0, "=>%d\n", pp->poll_period);
|
||||
break;
|
||||
case CMD_SAMPLE:
|
||||
result = true;
|
||||
dprintf(0, "Sample=%d", pp->sample_period);
|
||||
dbg_printf(0, "Sample=%d", pp->sample_period);
|
||||
pp->sample_period = strtol(value, NULL, 10);
|
||||
if (pp->sample_period < 1)
|
||||
pp->sample_period = 1;
|
||||
else if (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_SYNC:
|
||||
result = true;
|
||||
dprintf(0, "Sync=%s", pp->sync ? "External" : "Internal");
|
||||
dbg_printf(0, "Sync=%s", pp->sync ? "External" : "Internal");
|
||||
if (strcasecmp(value, "external") == 0)
|
||||
pp->sync = true;
|
||||
else
|
||||
pp->sync = false;
|
||||
dprintf(0, "=>%s\n", pp->sync ? "External" : "Internal");
|
||||
dbg_printf(0, "=>%s\n", pp->sync ? "External" : "Internal");
|
||||
break;
|
||||
case CMD_TERMINAL:
|
||||
result = true;
|
||||
dprintf(0, "Sample=%llu", pp->terminal_count);
|
||||
dbg_printf(0, "Sample=%llu", pp->terminal_count);
|
||||
pp->terminal_count = strtoull(value, NULL, 10);
|
||||
dprintf(0, "=>%llu\n", pp->terminal_count);
|
||||
dbg_printf(0, "=>%llu\n", pp->terminal_count);
|
||||
break;
|
||||
case CMD_TE_CHECK:
|
||||
result = true;
|
||||
dprintf(0, "TE_Check=%s",
|
||||
dbg_printf(0, "TE_Check=%s",
|
||||
pp->terminal_check_type == 0 ? "None" :
|
||||
pp->terminal_check_type == 1 ? "Counter" :
|
||||
pp->terminal_check_type == 2 ? "Timer" : "Unknown");
|
||||
@@ -205,13 +205,13 @@ bool param_set(pPARAMETERS pp, char* name, char* value)
|
||||
pp->terminal_check_type = 2;
|
||||
else
|
||||
pp->terminal_check_type = 0;
|
||||
dprintf(0, "=>%s\n",
|
||||
dbg_printf(0, "=>%s\n",
|
||||
pp->terminal_check_type == 0 ? "None" :
|
||||
pp->terminal_check_type == 1 ? "Counter" :
|
||||
pp->terminal_check_type == 2 ? "Timer" : "Unknown");
|
||||
break;
|
||||
default:
|
||||
dprintf(0, "Unknown Parameter: \"%s\" = \"%s\"\n", name, value);
|
||||
dbg_printf(0, "Unknown Parameter: \"%s\" = \"%s\"\n", name, value);
|
||||
break;
|
||||
}
|
||||
return result;
|
||||
@@ -364,7 +364,7 @@ bool param_get_cmd(pPARAMETERS pp, char* cmd, int n)
|
||||
result = true;
|
||||
}
|
||||
else
|
||||
dprintf(0, "Unknown GET Parameter: \"%s\"\n", name);
|
||||
dbg_printf(0, "Unknown GET Parameter: \"%s\"\n", name);
|
||||
break;
|
||||
}
|
||||
if (!result)
|
||||
|
||||
@@ -6,13 +6,15 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
typedef enum { COUNT_UP = 0, COUNT_DOWN } DIRECTION;
|
||||
|
||||
/**
|
||||
* Counter Control Parameters
|
||||
*/
|
||||
typedef struct parameter_t
|
||||
{
|
||||
/** direction of counting for the logical counter */
|
||||
enum { COUNT_UP = 0, COUNT_DOWN } direction;
|
||||
DIRECTION direction;
|
||||
/** counter read period in milliseconds */
|
||||
int poll_period;
|
||||
/** sample period in number of reads per sample */
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "sock.h"
|
||||
#include "utility.h"
|
||||
#include "display.h"
|
||||
#include "cntr.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
@@ -17,7 +16,7 @@
|
||||
#include <netinet/ip.h> /* superset of previous */
|
||||
|
||||
#define LINE_LEN 1024
|
||||
#define MAX_SOCK 50
|
||||
#define MAX_SOCK 200
|
||||
|
||||
/**
|
||||
* Mode of the socket
|
||||
@@ -65,6 +64,9 @@ typedef struct terminal_t
|
||||
char url[LINE_LEN];
|
||||
/** value from Content-Length header */
|
||||
int content_length;
|
||||
/** associated device */
|
||||
int(*command)(void* device, const char* cmd);
|
||||
void* device;
|
||||
} TERMINAL, *pTERMINAL;
|
||||
|
||||
/**
|
||||
@@ -89,20 +91,25 @@ static int sock_l;
|
||||
*
|
||||
* \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 one = 1;
|
||||
struct sockaddr_in my_addr;
|
||||
dbg_printf(0, "sock_init\n");
|
||||
memset(fdv, 0, sizeof(fdv));
|
||||
for (i = 0; i < MAX_SOCK; ++i)
|
||||
{
|
||||
fdv[i].fd = -1;
|
||||
}
|
||||
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);
|
||||
if (status < 0)
|
||||
{
|
||||
@@ -130,10 +137,12 @@ void sock_init(int addr)
|
||||
perror("listen");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fds[0].fd = sock_l;
|
||||
fds[0].events = POLLIN | POLLOUT;
|
||||
fdv[0].fd = sock_l;
|
||||
fdv[0].input = sock_accept;
|
||||
fds[num_fds].fd = sock_l;
|
||||
fds[num_fds].events = POLLIN | POLLOUT;
|
||||
fdv[num_fds].fd = sock_l;
|
||||
fdv[num_fds].input = sock_accept;
|
||||
fdv[num_fds].command = command;
|
||||
fdv[num_fds].device = device;
|
||||
++num_fds;
|
||||
}
|
||||
|
||||
@@ -157,7 +166,7 @@ void sock_check(int timeout)
|
||||
}
|
||||
if (ready == 0)
|
||||
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)
|
||||
{
|
||||
if (fds[i].revents)
|
||||
@@ -177,7 +186,7 @@ void sock_check(int timeout)
|
||||
*/
|
||||
void sock_close(int n)
|
||||
{
|
||||
dprintf(0, "sock_close\n");
|
||||
dbg_printf(0, "sock_close\n");
|
||||
shutdown(fdv[n].fd, SHUT_RDWR);
|
||||
close(fdv[n].fd);
|
||||
if (n != num_fds)
|
||||
@@ -198,9 +207,9 @@ void sock_line(int n)
|
||||
{
|
||||
char *cp = &fdv[n].line[0];
|
||||
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')
|
||||
dprintf(0, "\n");
|
||||
dbg_printf(0, "\n");
|
||||
switch(fdv[n].state)
|
||||
{
|
||||
case 0:
|
||||
@@ -276,16 +285,7 @@ void sock_line(int n)
|
||||
put_form(n);
|
||||
else if (strncasecmp(fdv[n].url, "/cmd=", 5) == 0)
|
||||
{
|
||||
if (strncasecmp(fdv[n].url, "/cmd=pause", 10) == 0)
|
||||
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);
|
||||
fdv[n].command(fdv[n].device, &fdv[n].url[5]);
|
||||
put_page_refresh(n);
|
||||
}
|
||||
else
|
||||
@@ -311,14 +311,14 @@ void sock_line(int n)
|
||||
(cp = strchr(&cp[14], ':')))
|
||||
{
|
||||
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;
|
||||
case 4:
|
||||
/*
|
||||
* 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;
|
||||
if (fdv[n].content_length <= 0)
|
||||
@@ -338,7 +338,7 @@ void sock_line(int n)
|
||||
*/
|
||||
if (fdv[n].state == 5)
|
||||
{
|
||||
put_form(n);
|
||||
put_form_refresh(n);
|
||||
fdv[n].state = 0;
|
||||
}
|
||||
}
|
||||
@@ -350,7 +350,7 @@ void sock_line(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;
|
||||
char buffer[1024];
|
||||
sz = recv(fdv[n].fd, &buffer, sizeof(buffer), 0);
|
||||
@@ -362,9 +362,9 @@ void sock_input(int n)
|
||||
if (sz < 0)
|
||||
{
|
||||
if (errno == EAGAIN) /* AKA EWOULDBLOCK */
|
||||
dprintf(0, "EAGAIN:");
|
||||
dbg_printf(0, "EAGAIN:");
|
||||
else if (errno == ESPIPE) /* Illegal seek (on pipe or socket) */
|
||||
dprintf(0, "ESPIPE:");
|
||||
dbg_printf(0, "ESPIPE:");
|
||||
else
|
||||
perror("recv");
|
||||
return;
|
||||
@@ -392,7 +392,7 @@ void sock_input(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;
|
||||
struct sockaddr_in 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].addr = my_addr;
|
||||
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].state = 0;
|
||||
fds[num_fds].fd = sock_n;
|
||||
@@ -502,3 +504,8 @@ void sock_err(int n)
|
||||
buffer.length = strlen(buffer.body);
|
||||
sock_send(n, &buffer);
|
||||
}
|
||||
|
||||
void* sock_device(int n)
|
||||
{
|
||||
return fdv[n].device;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
#define _SOCK_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_close(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_ok(int n);
|
||||
void sock_err(int n);
|
||||
void* sock_device(int n);
|
||||
#endif
|
||||
|
||||
@@ -95,8 +95,10 @@ void time_adv(struct timeval* tmr, int msec)
|
||||
|
||||
#define DEBUG_FILE "debug_file.txt"
|
||||
#include <stdarg.h>
|
||||
#ifndef dprintf
|
||||
#ifndef dbg_printf
|
||||
static int debug_level = -1;
|
||||
static bool new_line = true;
|
||||
|
||||
int set_debug_level(int new_level)
|
||||
{
|
||||
int temp = debug_level;
|
||||
@@ -104,11 +106,12 @@ int set_debug_level(int new_level)
|
||||
return temp;
|
||||
}
|
||||
|
||||
int dprintf(int level, const char* format, ...)
|
||||
int dbg_printf(int level, const char* format, ...)
|
||||
{
|
||||
int result = 0;
|
||||
int iRet = 0;
|
||||
va_list ap;
|
||||
char buffer[2048];
|
||||
|
||||
if (level <= debug_level)
|
||||
{
|
||||
@@ -116,11 +119,19 @@ int dprintf(int level, const char* format, ...)
|
||||
if (debug_file)
|
||||
{
|
||||
va_start(ap, format);
|
||||
iRet = fprintf(debug_file, "%s ", make_timestamp(NULL));
|
||||
result = iRet;
|
||||
iRet = vfprintf(debug_file, format, ap);
|
||||
result += iRet;
|
||||
iRet = vsnprintf(buffer, sizeof(buffer), format, 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,11 @@
|
||||
#define _UTILITY_H_
|
||||
|
||||
#include <sys/time.h>
|
||||
#ifdef __cplusplus
|
||||
#else
|
||||
#include <stdbool.h>
|
||||
#endif
|
||||
|
||||
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);
|
||||
#if 1
|
||||
int set_debug_level(int new_level);
|
||||
int dprintf(int level, const char* format, ...);
|
||||
int dbg_printf(int level, const char* format, ...);
|
||||
#else
|
||||
#define dprintf fprintf
|
||||
#define dbg_printf fprintf
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user