Files
sics/site_ansto/hardsup/Monitor/params.c
Douglas Clowes 8b3816b612 Initial load of driver
r1083 | dcl | 2006-08-25 14:46:51 +1000 (Fri, 25 Aug 2006) | 2 lines
2012-11-15 12:46:27 +11:00

385 lines
10 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 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"
static struct param_command_t {
int cmd;
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},
{0, NULL}
};
bool param_set(pPARAMETERS pp, char* name, char* value)
{
bool result = false;
switch (name[0])
{
case 'd':
case 'D':
if (strcasecmp(name, TXT_DIRECTION) == 0)
{
result = true;
dprintf(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");
}
break;
case 'i':
case 'I':
if (strcasecmp(name, TXT_INITIAL) == 0)
{
result = true;
dprintf(0, "Initial=%llu", pp->initial_count);
pp->initial_count = strtoull(value, NULL, 10);
dprintf(0, "=>%llu\n", pp->initial_count);
}
break;
case 'r':
case 'R':
if (strcasecmp(name, TXT_REPORT) == 0)
{
result = true;
dprintf(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);
}
else if (strcasecmp(name, TXT_RANGE_MODE) == 0)
{
result = true;
dprintf(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);
}
else if (strcasecmp(name, TXT_RANGE_GATE) == 0)
{
result = true;
dprintf(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",
pp->range_gate_enable ? "Enabled" : "Disabled");
}
else if (strcasecmp(name, TXT_RE_CHECK) == 0)
{
result = true;
dprintf(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",
pp->range_check_enable ? "Enabled" : "Disabled");
}
else if (strcasecmp(name, TXT_RANGE_HIGH) == 0)
{
result = true;
dprintf(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);
}
else if (strcasecmp(name, TXT_RANGE_LOW) == 0)
{
result = true;
dprintf(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);
}
break;
case 's':
case 'S':
if (strcasecmp(name, TXT_SCAN) == 0)
{
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);
}
else if (strcasecmp(name, TXT_SAMPLE) == 0)
{
result = true;
dprintf(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);
}
break;
case 't':
case 'T':
if (strcasecmp(name, TXT_TERMINAL) == 0)
{
result = true;
dprintf(0, "Sample=%llu", pp->terminal_count);
pp->terminal_count = strtoull(value, NULL, 10);
dprintf(0, "=>%llu\n", pp->terminal_count);
}
else if (strcasecmp(name, TXT_TE_CHECK) == 0)
{
result = true;
dprintf(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;
dprintf(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);
break;
}
return result;
}
bool param_set_cmd(pPARAMETERS pp, 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, 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[0])
{
case 'a':
case 'A':
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;
}
break;
case 'd':
case 'D':
if (strcasecmp(name, TXT_DIRECTION) == 0)
{
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%s", name,
pp->direction ? "Down" : "Up");
}
break;
case 'i':
case 'I':
if (strcasecmp(name, TXT_INITIAL) == 0)
{
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%llu", name,
pp->initial_count);
}
break;
case 'r':
case 'R':
if (strcasecmp(name, TXT_REPORT) == 0)
{
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%d", name,
pp->report_period);
}
else if (strcasecmp(name, TXT_RANGE_GATE) == 0)
{
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%s", name,
pp->range_gate_enable ? "Enabled" : "Disabled");
}
else if (strcasecmp(name, TXT_RE_CHECK) == 0)
{
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%s", name,
pp->range_check_enable ? "Enabled" : "Disabled");
}
else if (strcasecmp(name, TXT_RANGE_MODE) == 0)
{
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%d", name,
pp->range_mode);
}
else if (strcasecmp(name, TXT_RANGE_HIGH) == 0)
{
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%g", name,
pp->range_high);
}
else if (strcasecmp(name, TXT_RANGE_LOW) == 0)
{
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%g", name,
pp->range_low);
}
break;
case 's':
case 'S':
if (strcasecmp(name, TXT_SCAN) == 0)
{
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%d", name,
pp->poll_period);
}
else if (strcasecmp(name, TXT_SAMPLE) == 0)
{
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%d", name,
pp->sample_period);
}
break;
case 't':
case 'T':
if (strcasecmp(name, TXT_TERMINAL) == 0)
{
result = true;
snprintf(buffer.body, sizeof(buffer.body),
"GET %s=%llu", name,
pp->terminal_count);
}
else if (strcasecmp(name, TXT_TE_CHECK) == 0)
{
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:
dprintf(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;
}