Skip to content

Commit

Permalink
Scripting support (#53)
Browse files Browse the repository at this point in the history
* Initial SD script support

* Comments in script files are ignored

* Added startup script option

* Indent fix
  • Loading branch information
Baldanos authored and bvernoux committed May 17, 2016
1 parent fbc20de commit 47f181a
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 4 deletions.
3 changes: 2 additions & 1 deletion common/common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ COMMONSRC = common/common.c \
common/microsd.c \
common/usb1cfg.c \
common/usb2cfg.c \
common/fault_handler.c
common/fault_handler.c \
common/script.c

COMMONSRC_ASM = common/fault_handler_asm.s

Expand Down
2 changes: 1 addition & 1 deletion common/ffconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
/ 3: f_lseek() function is removed in addition to 2. */


#define _USE_STRFUNC 0 /* 0:Disable or 1-2:Enable */
#define _USE_STRFUNC 1 /* 0:Disable or 1-2:Enable */
/* To enable string functions, set _USE_STRFUNC to 1 or 2. */


Expand Down
40 changes: 38 additions & 2 deletions common/microsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
#include "microsd.h"
#include "common.h"

#define SDC_BURST_SIZE 4 /* how many sectors reads at once */
#define IN_OUT_BUF_SIZE (MMCSD_BLOCK_SIZE * SDC_BURST_SIZE)
#include "script.h"

static uint8_t outbuf[IN_OUT_BUF_SIZE+8];
static uint8_t inbuf[IN_OUT_BUF_SIZE+8];

Expand Down Expand Up @@ -61,6 +61,23 @@ bool is_fs_ready(void)
return fs_ready;
}

bool is_file_present(char * filename)
{
FRESULT err;
if (!fs_ready) {
err = mount();
if(err) {
return FALSE;
}
}

err = f_stat(filename, NULL);
if (err == FR_OK) {
return TRUE;
}
return FALSE;
}

/**
* @brief Parody of UNIX badblocks program.
*
Expand Down Expand Up @@ -948,6 +965,22 @@ int cmd_sd_mkdir(t_hydra_console *con, t_tokenline_parsed *p)
return TRUE;
}

int cmd_sd_script(t_hydra_console *con, t_tokenline_parsed *p)
{
#define MAX_FILE_SIZE (524288)
int str_offset;

if (p->tokens[2] != T_ARG_STRING || p->tokens[4] != 0)
return FALSE;

memcpy(&str_offset, &p->tokens[3], sizeof(int));
snprintf(filename, FILENAME_SIZE, "0:%s", p->buf + str_offset);

execute_script(con, filename);

return TRUE;
}

int cmd_sd(t_hydra_console *con, t_tokenline_parsed *p)
{
int ret;
Expand Down Expand Up @@ -991,6 +1024,9 @@ int cmd_sd(t_hydra_console *con, t_tokenline_parsed *p)
case T_MKDIR:
ret = cmd_sd_mkdir(con, p);
break;
case T_SCRIPT:
ret = cmd_sd_script(con, p);
break;
default:
return FALSE;
}
Expand Down
4 changes: 4 additions & 0 deletions common/microsd.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
#ifndef _MICROSD_H_
#define _MICROSD_H_

#define SDC_BURST_SIZE 4 /* how many sectors reads at once */
#define IN_OUT_BUF_SIZE (MMCSD_BLOCK_SIZE * SDC_BURST_SIZE)

#include "common.h"

typedef struct {
char filename[255];
} filename_t;

bool is_fs_ready(void);
bool is_file_present(char * filename);

int write_file(uint8_t* buffer, uint32_t size);
void write_file_get_last_filename(filename_t* out_filename);
Expand Down
58 changes: 58 additions & 0 deletions common/script.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* HydraBus/HydraNFC
*
* Copyright (C) 2016 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://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 "ff.h"
#include "microsd.h"

static uint8_t inbuf[IN_OUT_BUF_SIZE+8];

int execute_script(t_hydra_console *con, char *filename)
{
FRESULT err;
FIL fp;
int i;
if (!is_fs_ready()) {
err = mount();
if(err) {
cprintf(con, "Mount failed: error %d.\r\n", err);
return FALSE;
}
}

err = f_open(&fp, (TCHAR *)filename, FA_READ | FA_OPEN_EXISTING);
if (err != FR_OK) {
cprintf(con, "Failed to open file %s: error %d.\r\n", filename, err);
return FALSE;
}

/* Clear any input in tokenline buffer */
tl_input(con->tl, 0x03);

while(!f_eof(&fp)) {
f_gets((TCHAR *)inbuf, IN_OUT_BUF_SIZE, &fp);
i=0;
if(inbuf[0] == '#') {
continue;
}
while(inbuf[i] != '\0') {
tl_input(con->tl, inbuf[i]);
i++;
}
}
return TRUE;
}
19 changes: 19 additions & 0 deletions common/script.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* HydraBus/HydraNFC
*
* Copyright (C) 2016 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://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.
*/

int execute_script(t_hydra_console *con, char *filename);
6 changes: 6 additions & 0 deletions hydrabus/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ t_token_dict tl_dict[] = {
{ T_LOW, "low" },
{ T_HIGH, "high" },
{ T_THREEWIRE, "3-wire" },
{ T_SCRIPT, "script" },

{ T_LEFT_SQ, "[" },
{ T_RIGHT_SQ, "]" },
Expand Down Expand Up @@ -1226,6 +1227,11 @@ t_token tokens_sd[] = {
.arg_type = T_ARG_STRING,
.help = "Create new directory"
},
{
T_SCRIPT,
.arg_type = T_ARG_STRING,
.help = "Execute script from file"
},
{ }
};

Expand Down
1 change: 1 addition & 0 deletions hydrabus/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ enum {
T_LOW,
T_HIGH,
T_THREEWIRE,
T_SCRIPT,

/* BP-compatible commands */
T_LEFT_SQ,
Expand Down
8 changes: 8 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

#include "bsp.h"

#include "script.h"

#define INIT_SCRIPT_NAME "initscript"

volatile int nb_console = 0;

/* USB1: Virtual serial port over USB. */
Expand Down Expand Up @@ -72,6 +76,10 @@ THD_FUNCTION(console, arg)
tl_set_prompt(con->tl, PROMPT);
tl_set_callback(con->tl, execute);

if(is_file_present(INIT_SCRIPT_NAME)) {
execute_script(con,INIT_SCRIPT_NAME);
}

while (1) {
input = get_char(con);
if(input == 0) {
Expand Down

0 comments on commit 47f181a

Please sign in to comment.