You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
4.7 KiB
169 lines
4.7 KiB
#include "app_cmd_parser.h"
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
static int parse_x_v_addr(const char *line, uint8_t *addr)
|
|
{
|
|
if (!line || !addr) return 0;
|
|
if (!(line[0] == 'x' || line[0] == 'X')) return 0;
|
|
if (!isdigit((unsigned char)line[1]) || !isdigit((unsigned char)line[2])) return 0;
|
|
if (!(line[3] == 'v' || line[3] == 'V')) return 0;
|
|
if (line[4] != '\0') return 0;
|
|
|
|
*addr = (uint8_t)((line[1] - '0') * 10 + (line[2] - '0'));
|
|
return 1;
|
|
}
|
|
|
|
static int find_payload_pos(const char *line)
|
|
{
|
|
const char *p = strchr(line, ':');
|
|
if (!p) return -1;
|
|
return (int)(p - line + 1);
|
|
}
|
|
|
|
static uint8_t hex_nibble(char c)
|
|
{
|
|
c = (char)toupper((unsigned char)c);
|
|
if (c >= '0' && c <= '9') return (uint8_t)(c - '0');
|
|
if (c >= 'A' && c <= 'F') return (uint8_t)(10 + (c - 'A'));
|
|
return 0xFFu;
|
|
}
|
|
|
|
static int hex_pair_to_u8(char hi, char lo, uint8_t *out)
|
|
{
|
|
uint8_t h = hex_nibble(hi);
|
|
uint8_t l = hex_nibble(lo);
|
|
if (h == 0xFFu || l == 0xFFu || !out) return 0;
|
|
*out = (uint8_t)((h << 4) | l);
|
|
return 1;
|
|
}
|
|
|
|
int app_cmd_parse_line(app_cmd_src_t src, const char *line, app_job_t *job)
|
|
{
|
|
uint8_t addr = 0;
|
|
int payload_pos;
|
|
int len;
|
|
int pos;
|
|
unsigned int k;
|
|
|
|
if (!line || !job) return 0;
|
|
|
|
memset(job, 0, sizeof(*job));
|
|
job->src = src;
|
|
job->check_on = 1;
|
|
|
|
len = (int)strlen(line);
|
|
|
|
/* �Ϲ� ���� */
|
|
if (line[0] != 'x' && line[0] != 'X') {
|
|
job->type = APP_JOB_LOCAL_EXEC;
|
|
strncpy(job->line, line, sizeof(job->line) - 1);
|
|
job->line[sizeof(job->line) - 1] = '\0';
|
|
return 1;
|
|
}
|
|
|
|
/* xNNv */
|
|
if (parse_x_v_addr(line, &addr)) {
|
|
job->type = APP_JOB_SCAN_ADDR;
|
|
job->addr = addr;
|
|
|
|
strncpy(job->line, line, sizeof(job->line) - 1);
|
|
job->line[sizeof(job->line) - 1] = '\0';
|
|
|
|
return 1;
|
|
}
|
|
|
|
payload_pos = find_payload_pos(line);
|
|
if (payload_pos < 0) {
|
|
job->type = APP_JOB_FORWARD_LINE;
|
|
strncpy(job->line, line, sizeof(job->line) - 1);
|
|
job->line[sizeof(job->line) - 1] = '\0';
|
|
return 1;
|
|
}
|
|
|
|
if (len < payload_pos) return 0;
|
|
if (!isdigit((unsigned char)line[1]) || !isdigit((unsigned char)line[2])) return 0;
|
|
|
|
job->addr = (uint8_t)((line[1] - '0') * 10 + (line[2] - '0'));
|
|
|
|
/* xNNc_001101:... ���ؿ��� channel = 001 */
|
|
if (payload_pos >= 8 &&
|
|
isdigit((unsigned char)line[5]) &&
|
|
isdigit((unsigned char)line[6]) &&
|
|
isdigit((unsigned char)line[7])) {
|
|
job->channel = (uint8_t)((line[5] - '0') * 100 +
|
|
(line[6] - '0') * 10 +
|
|
(line[7] - '0'));
|
|
}
|
|
|
|
job->mode = (char)toupper((unsigned char)line[3]);
|
|
|
|
if (payload_pos >= 11) {
|
|
job->hash_on = (uint8_t)(line[8] == '1');
|
|
job->anaout_on = (uint8_t)(line[9] == '1');
|
|
job->check_on = (uint8_t)(line[10] == '1');
|
|
}
|
|
|
|
pos = payload_pos;
|
|
if (pos + 1 >= len) return 0;
|
|
|
|
if ((line[pos] == 'o' || line[pos] == 'O') &&
|
|
(line[pos + 1] == 'w' || line[pos + 1] == 'W')) {
|
|
job->type = APP_JOB_PROTO_OW;
|
|
pos += 2;
|
|
|
|
if (pos < len && (line[pos] == 't' || line[pos] == 'T')) {
|
|
job->proto = APP_PROTO_OWIT;
|
|
pos++;
|
|
} else {
|
|
job->proto = APP_PROTO_OWIW;
|
|
}
|
|
}
|
|
else if ((line[pos] == 'o' || line[pos] == 'O') &&
|
|
(line[pos + 1] == 'r' || line[pos + 1] == 'R')) {
|
|
job->type = APP_JOB_PROTO_OR;
|
|
job->proto = APP_PROTO_OWIR;
|
|
pos += 2;
|
|
}
|
|
else {
|
|
job->type = APP_JOB_FORWARD_LINE;
|
|
strncpy(job->line, line, sizeof(job->line) - 1);
|
|
job->line[sizeof(job->line) - 1] = '\0';
|
|
return 1;
|
|
}
|
|
|
|
if (pos < len && (line[pos] == '_' || line[pos] == ':')) pos++;
|
|
|
|
if (pos + 1 >= len) return 0;
|
|
if (!hex_pair_to_u8(line[pos], line[pos + 1], &job->id)) return 0;
|
|
pos += 2;
|
|
|
|
if (pos + 2 >= len ||
|
|
!isdigit((unsigned char)line[pos]) ||
|
|
!isdigit((unsigned char)line[pos + 1]) ||
|
|
!isdigit((unsigned char)line[pos + 2])) {
|
|
return 0;
|
|
}
|
|
|
|
job->len = (uint16_t)(100 * (line[pos] - '0') +
|
|
10 * (line[pos + 1] - '0') +
|
|
(line[pos + 2] - '0'));
|
|
pos += 3;
|
|
|
|
if (job->type == APP_JOB_PROTO_OW) {
|
|
if (job->len == 0 || job->len > APP_JOB_PAYLOAD_MAX) return 0;
|
|
if (pos + ((int)job->len * 2) > len) return 0;
|
|
|
|
for (k = 0; k < job->len; k++) {
|
|
if (!hex_pair_to_u8(line[pos + (int)(2 * k)],
|
|
line[pos + (int)(2 * k + 1)],
|
|
&job->payload[k])) {
|
|
return 0;
|
|
}
|
|
}
|
|
} else {
|
|
if (job->len == 0 || job->len > APP_JOB_PAYLOAD_MAX) return 0;
|
|
}
|
|
|
|
return 1;
|
|
}
|