Permalink
Browse files

Scripting support (#53)

* Initial SD script support

* Comments in script files are ignored

* Added startup script option

* Indent fix
  • Loading branch information...
1 parent fbc20de commit 47f181a9b8d8ed39f27322309dec532256e9fe77 @Baldanos Baldanos committed with bvernoux May 17, 2016
Showing with 137 additions and 4 deletions.
  1. +2 −1 common/common.mk
  2. +1 −1 common/ffconf.h
  3. +38 −2 common/microsd.c
  4. +4 −0 common/microsd.h
  5. +58 −0 common/script.c
  6. +19 −0 common/script.h
  7. +6 −0 hydrabus/commands.c
  8. +1 −0 hydrabus/commands.h
  9. +8 −0 main.c
View
@@ -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
View
@@ -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. */
View
@@ -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];
@@ -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.
*
@@ -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;
@@ -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;
}
View
@@ -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);
View
@@ -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;
+}
View
@@ -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);
View
@@ -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, "]" },
@@ -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"
+ },
{ }
};
View
@@ -128,6 +128,7 @@ enum {
T_LOW,
T_HIGH,
T_THREEWIRE,
+ T_SCRIPT,
/* BP-compatible commands */
T_LEFT_SQ,
View
@@ -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. */
@@ -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) {

0 comments on commit 47f181a

Please sign in to comment.