Skip to content

Commit

Permalink
\x escape sequence support
Browse files Browse the repository at this point in the history
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 3614185
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
51 changes: 51 additions & 0 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
18 changes: 7 additions & 11 deletions hydrabus/hydrabus_mode.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "hydrabus.h"
#include "hydrabus_mode.h"
#include "hydrabus_trigger.h"
#include "mode_config.h"

#include "bsp_rng.h"
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down
5 changes: 0 additions & 5 deletions hydrabus/hydrabus_mode_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 2 additions & 2 deletions hydrabus/hydrabus_trigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down

0 comments on commit 3614185

Please sign in to comment.