Permalink
Browse files

\x escape sequence support

This patch adds \x.. escape sequence in freeform strings and in trigger
filter.
  • Loading branch information...
Baldanos committed Apr 11, 2017
1 parent b76fe69 commit 361418545e2e7ef6db09b1268e1489f50b75bb7a
Showing with 61 additions and 18 deletions.
  1. +51 −0 common/common.c
  2. +1 −0 common/common.h
  3. +7 −11 hydrabus/hydrabus_mode.c
  4. +0 −5 hydrabus/hydrabus_mode_uart.c
  5. +2 −2 hydrabus/hydrabus_trigger.c
View
@@ -558,3 +558,54 @@ uint64_t get_cyclecounter64I(void)
return cyclecounter64;
}
static uint8_t hexchartonibble(char hex)
{
if (hex >= '0' && hex <= '9') return hex - '0';
if (hex >= 'a' && hex <='f') return hex - 'a' + 10;
if (hex >= 'A' && hex <='F') return hex - 'A' + 10;
return 0;
}
static uint8_t hex2byte(char * hex)
{
uint8_t val = 0;
val = (hexchartonibble(hex[0]) << 4);
val += hexchartonibble(hex[1]);
return val;
}
uint8_t parse_escaped_string(char * input, uint8_t * output)
{
uint8_t count=0;
uint8_t i=0;
uint8_t num_bytes=0;
unsigned char c;
/* Poor man's strlen() */
while (input[count] != '\0') {
count++;
}
while(i<count) {
if(input[i] == '\\'){
switch(input[i+1]) {
case '\\':
output[num_bytes++] = '\\';
i++;
break;
case 'x':
c = (char)hex2byte(&(input[i+2]));
output[num_bytes++] = c;
i+=3;
break;
default:
i++;
break;
}
} else {
output[num_bytes++] = input[i];
}
i++;
}
return num_bytes;
}
View
@@ -172,5 +172,6 @@ void cprintf(t_hydra_console *con, const char *fmt, ...);
void print_dbg(const char *data, const uint32_t size);
void printf_dbg(const char *fmt, ...);
void print_hex(t_hydra_console *con, uint8_t* data, uint8_t size);
uint8_t parse_escaped_string(char * input, uint8_t * output);
#endif /* _COMMON_H_ */
View
@@ -24,6 +24,7 @@
#include "hydrabus.h"
#include "hydrabus_mode.h"
#include "hydrabus_trigger.h"
#include "mode_config.h"
#include "bsp_rng.h"
@@ -256,6 +257,9 @@ int cmd_mode_exec(t_hydra_console *con, t_tokenline_parsed *p)
*/
t += tokens_used - 1;
break;
case T_TRIGGER:
t += cmd_trigger(con, p, t + 1);
break;
case T_EXIT:
MAYBE_CALL(con->mode->exec->cleanup);
mode_exit(con, p);
@@ -323,23 +327,15 @@ static int chomp_strings(t_hydra_console *con, t_tokenline_parsed *p,
int token_pos, unsigned int *num_bytes)
{
mode_config_proto_t* p_proto = &con->mode->proto;
int count, t, i;
int t;
char * str;
t = token_pos;
t++;
*num_bytes = 0;
str = p->buf + p->tokens[t++];
count=0;
/* Poor man's strlen() */
while (str[count] != '\0') {
count++;
}
i=0;
while(i<count) {
p_proto->buffer_tx[(*num_bytes)++] = str[i];
i++;
}
*num_bytes = parse_escaped_string(str, p_proto->buffer_tx);
return t - token_pos;
}
@@ -19,7 +19,6 @@
#include "common.h"
#include "hydrabus_mode_uart.h"
#include "bsp_uart.h"
#include "hydrabus_trigger.h"
#include <string.h>
#define UART_DEFAULT_SPEED (9600)
@@ -229,10 +228,6 @@ static int exec(t_hydra_console *con, t_tokenline_parsed *p, int token_pos)
case T_BRIDGE:
bridge(con);
break;
case T_TRIGGER:
t++;
t += cmd_trigger(con, p, t);
break;
default:
return t - token_pos;
}
@@ -87,8 +87,7 @@ int cmd_trigger(t_hydra_console *con, t_tokenline_parsed *p, int token_pos)
switch (p->tokens[t]) {
case T_FILTER:
t += 2;
trigger_length = strlen(p->buf + p->tokens[t]);
memcpy(trigger_data, p->buf + p->tokens[t], trigger_length);
trigger_length = parse_escaped_string(p->buf + p->tokens[t], trigger_data);
show_params(con);
break;
@@ -99,6 +98,7 @@ int cmd_trigger(t_hydra_console *con, t_tokenline_parsed *p, int token_pos)
break;
case T_SHOW:
t += show(con, p, t);
break;
default:
return t - token_pos;
}

0 comments on commit 3614185

Please sign in to comment.