417 lines
12 KiB
C
417 lines
12 KiB
C
#include "params.h"
|
|
#include "sock.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
#define CMD_DIRECTION 1
|
|
#define CMD_SCAN 2
|
|
#define CMD_SAMPLE 3
|
|
#define CMD_REPORT 4
|
|
#define CMD_INITIAL 5
|
|
#define CMD_TERMINAL 6
|
|
#define CMD_RANGE_LOW 7
|
|
#define CMD_RANGE_HIGH 8
|
|
#define CMD_TE_CHECK 9
|
|
#define CMD_RE_CHECK 10
|
|
#define CMD_RANGE_GATE 11
|
|
#define CMD_RANGE_MODE 12
|
|
#define CMD_OUTPUT 13
|
|
#define CMD_SYNC 14
|
|
#define CMD_SOURCE 15
|
|
#define CMD_FILTER 16
|
|
|
|
#define TXT_DIRECTION "DIRECTION"
|
|
#define TXT_SCAN "SCAN"
|
|
#define TXT_SAMPLE "SAMPLE"
|
|
#define TXT_REPORT "REPORT"
|
|
#define TXT_INITIAL "INITIAL"
|
|
#define TXT_TERMINAL "TERMINAL"
|
|
#define TXT_RANGE_LOW "RANGE_LOW"
|
|
#define TXT_RANGE_HIGH "RANGE_HIGH"
|
|
#define TXT_TE_CHECK "TE_CHECK"
|
|
#define TXT_RE_CHECK "RE_CHECK"
|
|
#define TXT_RANGE_GATE "RANGE_GATE"
|
|
#define TXT_RANGE_MODE "RANGE_MODE"
|
|
#define TXT_OUTPUT "OUTPUT"
|
|
#define TXT_SYNC "SYNC"
|
|
#define TXT_SOURCE "SOURCE"
|
|
#define TXT_FILTER "FILTER"
|
|
|
|
static struct param_command_t {
|
|
const int cmd;
|
|
const char* txt;
|
|
} param_command[] = {
|
|
{CMD_DIRECTION, TXT_DIRECTION},
|
|
{CMD_SCAN, TXT_SCAN},
|
|
{CMD_SAMPLE, TXT_SAMPLE},
|
|
{CMD_REPORT, TXT_REPORT},
|
|
{CMD_INITIAL, TXT_INITIAL},
|
|
{CMD_TERMINAL, TXT_TERMINAL},
|
|
{CMD_RANGE_LOW, TXT_RANGE_LOW},
|
|
{CMD_RANGE_HIGH, TXT_RANGE_HIGH},
|
|
{CMD_TE_CHECK, TXT_TE_CHECK},
|
|
{CMD_RE_CHECK, TXT_RE_CHECK},
|
|
{CMD_RANGE_GATE, TXT_RANGE_GATE},
|
|
{CMD_RANGE_MODE, TXT_RANGE_MODE},
|
|
{CMD_OUTPUT, TXT_OUTPUT},
|
|
{CMD_SYNC, TXT_SYNC},
|
|
{CMD_SOURCE, TXT_SOURCE},
|
|
{CMD_FILTER, TXT_FILTER},
|
|
{0, NULL}
|
|
};
|
|
#define NUM_CMDS ((int) ((sizeof(param_command)/sizeof(param_command[0]))))
|
|
|
|
static int name_to_command(const char* name)
|
|
{
|
|
int i;
|
|
for (i = 0; i < NUM_CMDS; ++i) {
|
|
if (param_command[i].txt == NULL)
|
|
return -1;
|
|
if (strcasecmp(name, param_command[i].txt) == 0)
|
|
return param_command[i].cmd;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
bool param_set(pPARAMETERS pp, const char* name, const char* value)
|
|
{
|
|
bool result = false;
|
|
switch (name_to_command(name))
|
|
{
|
|
case CMD_DIRECTION:
|
|
result = true;
|
|
dbg_printf(0, "Direction=%s", pp->direction ? "Down" : "Up");
|
|
if (strcasecmp(value, "down") == 0)
|
|
pp->direction = COUNT_DOWN;
|
|
else
|
|
pp->direction = COUNT_UP;
|
|
dbg_printf(0, "=>%s\n", pp->direction ? "Down" : "Up");
|
|
break;
|
|
case CMD_INITIAL:
|
|
result = true;
|
|
dbg_printf(0, "Initial=%llu", pp->initial_count);
|
|
pp->initial_count = strtoull(value, NULL, 10);
|
|
dbg_printf(0, "=>%llu\n", pp->initial_count);
|
|
break;
|
|
case CMD_OUTPUT:
|
|
result = true;
|
|
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;
|
|
else if (strcasecmp(value, "inverted") == 0)
|
|
pp->output_line = 2;
|
|
else
|
|
pp->output_line = 0;
|
|
dbg_printf(0, "=>%s\n", pp->output_line == 2 ? "Inverted":
|
|
pp->output_line == 1 ? "Enabled" : "Disabled");
|
|
break;
|
|
case CMD_REPORT:
|
|
result = true;
|
|
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;
|
|
dbg_printf(0, "=>%d\n", pp->report_period);
|
|
break;
|
|
case CMD_RANGE_MODE:
|
|
result = true;
|
|
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;
|
|
dbg_printf(0, "=>%d\n", pp->range_mode);
|
|
break;
|
|
case CMD_RANGE_GATE:
|
|
result = true;
|
|
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;
|
|
dbg_printf(0, "=>%s\n",
|
|
pp->range_gate_enable ? "Enabled" : "Disabled");
|
|
break;
|
|
case CMD_RE_CHECK:
|
|
result = true;
|
|
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;
|
|
dbg_printf(0, "=>%s\n",
|
|
pp->range_check_enable ? "Enabled" : "Disabled");
|
|
break;
|
|
case CMD_RANGE_HIGH:
|
|
result = true;
|
|
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;
|
|
dbg_printf(0, "=>%g\n", pp->range_high);
|
|
break;
|
|
case CMD_RANGE_LOW:
|
|
result = true;
|
|
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;
|
|
dbg_printf(0, "=>%g\n", pp->range_low);
|
|
break;
|
|
case CMD_SAMPLE:
|
|
result = true;
|
|
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;
|
|
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;
|
|
#ifndef SIMULATE_HARDWARE
|
|
else if (pp->source > 3)
|
|
pp->source = 0;
|
|
#endif
|
|
dbg_printf(0, "=>%d\n", pp->source);
|
|
break;
|
|
case CMD_FILTER:
|
|
result = true;
|
|
dbg_printf(0, "Filter=%d", pp->filter);
|
|
pp->filter = strtol(value, NULL, 10);
|
|
if (pp->filter < 0)
|
|
pp->filter = 0;
|
|
else if (pp->filter > 6)
|
|
pp->filter = 0;
|
|
dbg_printf(0, "=>%d\n", pp->filter);
|
|
break;
|
|
case CMD_SYNC:
|
|
result = true;
|
|
dbg_printf(0, "Sync=%s", pp->sync ? "External" : "Internal");
|
|
if (strcasecmp(value, "external") == 0)
|
|
pp->sync = true;
|
|
else
|
|
pp->sync = false;
|
|
dbg_printf(0, "=>%s\n", pp->sync ? "External" : "Internal");
|
|
break;
|
|
case CMD_TERMINAL:
|
|
result = true;
|
|
dbg_printf(0, "Terminal=%llu", pp->terminal_count);
|
|
pp->terminal_count = strtoull(value, NULL, 10);
|
|
dbg_printf(0, "=>%llu\n", pp->terminal_count);
|
|
break;
|
|
case CMD_TE_CHECK:
|
|
result = true;
|
|
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");
|
|
if (strcasecmp(value, "counter") == 0)
|
|
pp->terminal_check_type = 1;
|
|
else if (strcasecmp(value, "timer") == 0)
|
|
pp->terminal_check_type = 2;
|
|
else
|
|
pp->terminal_check_type = 0;
|
|
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:
|
|
dbg_printf(0, "Unknown Parameter: \"%s\" = \"%s\"\n", name, value);
|
|
break;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
bool param_set_cmd(pPARAMETERS pp, const char* cmd)
|
|
{
|
|
char name[100];
|
|
char value[100];
|
|
int len;
|
|
while (isspace(*cmd))
|
|
++cmd;
|
|
len = 0;
|
|
while (*cmd && !isspace(*cmd) && *cmd != '=')
|
|
{
|
|
if (len < 100 - 1)
|
|
name[len++] = *cmd;
|
|
++cmd;
|
|
}
|
|
name[len] = '\0';
|
|
|
|
while (isspace(*cmd))
|
|
++cmd;
|
|
if (*cmd != '=')
|
|
return false;
|
|
++cmd;
|
|
while (isspace(*cmd))
|
|
++cmd;
|
|
len = 0;
|
|
while (*cmd && !isspace(*cmd))
|
|
{
|
|
if (len < 100 - 1)
|
|
value[len++] = *cmd;
|
|
++cmd;
|
|
}
|
|
value[len] = '\0';
|
|
|
|
return param_set(pp, name, value);
|
|
}
|
|
|
|
bool param_get_cmd(pPARAMETERS pp, const char* cmd, int n)
|
|
{
|
|
char name[100];
|
|
int len;
|
|
bool result = false;
|
|
BUFFER buffer;
|
|
buffer.length = 0;
|
|
|
|
while (isspace(*cmd))
|
|
++cmd;
|
|
len = 0;
|
|
while (*cmd && !isspace(*cmd) && *cmd != '=')
|
|
{
|
|
if (len < 100 - 1)
|
|
name[len++] = *cmd;
|
|
++cmd;
|
|
}
|
|
name[len] = '\0';
|
|
|
|
switch (name_to_command(name))
|
|
{
|
|
case CMD_DIRECTION:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%s", name,
|
|
pp->direction ? "Down" : "Up");
|
|
break;
|
|
case CMD_INITIAL:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%llu", name,
|
|
pp->initial_count);
|
|
break;
|
|
case CMD_OUTPUT:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%s", name,
|
|
pp->output_line == 2 ? "Inverted":
|
|
pp->output_line == 1 ? "Enabled" : "Disabled");
|
|
break;
|
|
case CMD_REPORT:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%d", name,
|
|
pp->report_period);
|
|
break;
|
|
case CMD_RANGE_GATE:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%s", name,
|
|
pp->range_gate_enable ? "Enabled" : "Disabled");
|
|
break;
|
|
case CMD_RE_CHECK:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%s", name,
|
|
pp->range_check_enable ? "Enabled" : "Disabled");
|
|
break;
|
|
case CMD_RANGE_MODE:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%d", name,
|
|
pp->range_mode);
|
|
break;
|
|
case CMD_RANGE_HIGH:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%g", name,
|
|
pp->range_high);
|
|
break;
|
|
case CMD_RANGE_LOW:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%g", name,
|
|
pp->range_low);
|
|
break;
|
|
case CMD_SAMPLE:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%d", name,
|
|
pp->sample_period);
|
|
break;
|
|
case CMD_SCAN:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%d", name,
|
|
pp->poll_period);
|
|
break;
|
|
case CMD_SOURCE:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%d", name,
|
|
pp->source);
|
|
break;
|
|
case CMD_SYNC:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%s", name,
|
|
pp->sync ? "External" : "Internal");
|
|
break;
|
|
case CMD_TERMINAL:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%llu", name,
|
|
pp->terminal_count);
|
|
break;
|
|
case CMD_TE_CHECK:
|
|
result = true;
|
|
snprintf(buffer.body, sizeof(buffer.body),
|
|
"GET %s=%s", name,
|
|
pp->terminal_check_type == 0 ? "None" :
|
|
pp->terminal_check_type == 1 ? "Counter" :
|
|
pp->terminal_check_type == 2 ? "Timer" : "Unknown");
|
|
break;
|
|
default:
|
|
if (strcasecmp(name, "ALL") == 0)
|
|
{
|
|
int i;
|
|
for (i = 0; param_command[i].txt; ++i)
|
|
param_get_cmd(pp, param_command[i].txt, n);
|
|
strcpy(buffer.body, "OK");
|
|
result = true;
|
|
}
|
|
else
|
|
dbg_printf(0, "Unknown GET Parameter: \"%s\"\n", name);
|
|
break;
|
|
}
|
|
if (!result)
|
|
snprintf(buffer.body, sizeof(buffer.body), "GET ERR");
|
|
strcat(buffer.body, "\r\n");
|
|
buffer.length = strlen(buffer.body);
|
|
sock_send(n, &buffer);
|
|
return result;
|
|
}
|