Skip to content

Commit

Permalink
Initial trigger support (#74)
Browse files Browse the repository at this point in the history
This commit adds the possibility to trigger a pin (PB3) when a certain
string has been sniffed on the corresponding protocol.
  • Loading branch information
Baldanos authored and bvernoux committed Mar 13, 2017
1 parent 06aba46 commit a982d6b
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 0 deletions.
49 changes: 49 additions & 0 deletions hydrabus/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ t_token_dict tl_dict[] = {
{ T_FILE, "filename" },
{ T_ONEWIRE, "1-wire" },
{ T_FLASH, "flash" },
{ T_TRIGGER, "trigger" },

{ T_LEFT_SQ, "[" },
{ T_RIGHT_SQ, "]" },
Expand Down Expand Up @@ -299,6 +300,24 @@ t_token tokens_mode_nfc_sniff[] = {
{ }
};

t_token tokens_mode_trigger[] = {
{
T_SHOW,
.subtokens = tokens_mode_show,
.help = "Show trigger parameters"
},
{
T_FILTER,
.arg_type = T_ARG_STRING,
.help = "Trigger data"
},
{
T_START,
.help = "Arm the trigger"
},
{ }
};

#define NFC_PARAMETERS \
{\
T_TYPEA,\
Expand Down Expand Up @@ -410,6 +429,11 @@ t_token tokens_mode_uart[] = {
.subtokens = tokens_mode_show,
.help = "Show UART parameters"
},
{
T_TRIGGER,
.subtokens = tokens_mode_trigger,
.help = "Setup UART trigger"
},
UART_PARAMETERS
/* UART-specific commands */
{
Expand Down Expand Up @@ -559,6 +583,11 @@ t_token tokens_mode_i2c[] = {
.subtokens = tokens_mode_show,
.help = "Show I2C parameters"
},
{
T_TRIGGER,
.subtokens = tokens_mode_trigger,
.help = "Setup I2C trigger"
},
I2C_PARAMETERS
/* I2C-specific commands */
{
Expand Down Expand Up @@ -665,6 +694,11 @@ t_token tokens_mode_spi[] = {
.subtokens = tokens_mode_show,
.help = "Show SPI parameters"
},
{
T_TRIGGER,
.subtokens = tokens_mode_trigger,
.help = "Setup SPI trigger"
},
SPI_PARAMETERS
/* SPI-specific commands */
{
Expand Down Expand Up @@ -900,6 +934,11 @@ t_token tokens_mode_onewire[] = {
.subtokens = tokens_mode_show,
.help = "Show 1-wire parameters"
},
{
T_TRIGGER,
.subtokens = tokens_mode_trigger,
.help = "Setup 1-wire trigger"
},
ONEWIRE_PARAMETERS
/* 1-wire-specific commands */
{
Expand Down Expand Up @@ -993,6 +1032,11 @@ t_token tokens_mode_twowire[] = {
.subtokens = tokens_mode_show,
.help = "Show 2-wire parameters"
},
{
T_TRIGGER,
.subtokens = tokens_mode_trigger,
.help = "Setup 2-wire trigger"
},
TWOWIRE_PARAMETERS
/* 2-wire-specific commands */
{
Expand Down Expand Up @@ -1099,6 +1143,11 @@ t_token tokens_mode_threewire[] = {
.subtokens = tokens_mode_show,
.help = "Show 3-wire parameters"
},
{
T_TRIGGER,
.subtokens = tokens_mode_trigger,
.help = "Setup 3-wire trigger"
},
THREEWIRE_PARAMETERS
/* 3-wire-specific commands */
{
Expand Down
1 change: 1 addition & 0 deletions hydrabus/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ enum {
T_FILE,
T_ONEWIRE,
T_FLASH,
T_TRIGGER,

/* BP-compatible commands */
T_LEFT_SQ,
Expand Down
1 change: 1 addition & 0 deletions hydrabus/hydrabus.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ HYDRABUSSRC = hydrabus/hydrabus.c \
hydrabus/hydrabus_bbio_onewire.c \
hydrabus/hydrabus_bbio_flash.c \
hydrabus/hydrabus_sd.c \
hydrabus/hydrabus_trigger.c \

# Required include directories
HYDRABUSINC = ./hydrabus
5 changes: 5 additions & 0 deletions hydrabus/hydrabus_mode_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#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 @@ -223,6 +224,10 @@ 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
110 changes: 110 additions & 0 deletions hydrabus/hydrabus_trigger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* HydraBus/HydraNFC
*
* Copyright (C) 2014-2017 Benjamin VERNOUX
* Copyright (C) 2017 Nicolas OBERLI
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "common.h"
#include "tokenline.h"
#include "hydrabus.h"
#include "bsp.h"
#include "bsp_gpio.h"
#include "hydrabus_mode.h"
#include "hydrabus_trigger.h"

#include <string.h>

static uint8_t trigger_data[256];
static uint32_t trigger_length;

static void show_params(t_hydra_console *con)
{
cprintf(con, "Current trigger data :\r\n");
print_hex(con, trigger_data, trigger_length);

}

static int show(t_hydra_console *con, t_tokenline_parsed *p, int token_pos)
{
int tokens_used;

tokens_used = 0;
switch (p->tokens[++token_pos]) {
case T_PINS:
tokens_used++;
cprintf(con, "Trigger pin : PB%d\r\n", TRIGGER_PIN);
break;
default:
show_params(con);
break;
}

return tokens_used;
}

static int trigger_run(t_hydra_console *con)
{
uint32_t i=0;
uint8_t rx_data;
bsp_gpio_init(TRIGGER_PORT, TRIGGER_PIN, MODE_CONFIG_DEV_GPIO_OUT_PUSHPULL,
MODE_CONFIG_DEV_GPIO_NOPULL);
bsp_gpio_clr(TRIGGER_PORT, TRIGGER_PIN);
while(!USER_BUTTON) {
if(con->mode->exec->read) {
con->mode->exec->dump(con,&rx_data,1);
}
if(rx_data == trigger_data[i]) {
i++;
} else {
i=0;
}
if(i==trigger_length) {
bsp_gpio_set(TRIGGER_PORT, TRIGGER_PIN);
return 1;
}
}
return 0;
}

int cmd_trigger(t_hydra_console *con, t_tokenline_parsed *p, int token_pos)
{
int t;
uint32_t length;
for (t = token_pos; p->tokens[t]; t++) {
switch (p->tokens[t]) {
case T_FILTER:
t += 2;
trigger_length = strlen(p->buf + p->tokens[t]);
if(length<=255) {
memcpy(trigger_data, p->buf + p->tokens[t], trigger_length);
}

show_params(con);
break;
case T_START:
cprintf(con, "Interrupt by pressing user button.\r\n");
cprint(con, "\r\n", 2);
trigger_run(con);
break;
case T_SHOW:
t += show(con, p, t);
default:
return t - token_pos;
}
}
return t - token_pos;
}

22 changes: 22 additions & 0 deletions hydrabus/hydrabus_trigger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* HydraBus/HydraNFC
*
* Copyright (C) 2017 Nicolas OBERLI
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#define TRIGGER_PORT BSP_GPIO_PORTB
#define TRIGGER_PIN 3

int cmd_trigger(t_hydra_console *con, t_tokenline_parsed *p, int token_pos);

0 comments on commit a982d6b

Please sign in to comment.