2019-06-25 23:37:13 +02:00
|
|
|
/*
|
|
|
|
* main.c
|
|
|
|
*
|
|
|
|
* Created on: Jun 10, 2019
|
|
|
|
* Author: stefan
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "shellmatta.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ncurses.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
|
|
static bool exitRequest = false;
|
|
|
|
int f;
|
|
|
|
shellmatta_handle_t handle;
|
|
|
|
|
2019-07-29 01:30:16 +02:00
|
|
|
void set_blocking (int fd, int should_block)
|
2019-06-25 23:37:13 +02:00
|
|
|
{
|
|
|
|
struct termios tty;
|
|
|
|
memset (&tty, 0, sizeof tty);
|
|
|
|
if (tcgetattr (fd, &tty) != 0)
|
|
|
|
{
|
|
|
|
printf ("error %d from tggetattr", errno);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
tty.c_cc[VMIN] = should_block ? 1 : 0;
|
|
|
|
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
|
|
|
|
|
|
|
|
if (tcsetattr (fd, TCSANOW, &tty) != 0)
|
|
|
|
printf ("error %d setting term attributes", errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
|
|
|
|
{
|
2019-07-30 23:55:12 +02:00
|
|
|
shellmatta_printf(handle, "%s - length: %u", arguments, length);
|
2019-06-25 23:37:13 +02:00
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
shellmatta_cmd_t doSomethingCmd = {"doSomething", "do", "Function does something", "use me, please", doSomething, NULL};
|
|
|
|
|
|
|
|
static shellmatta_retCode_t doSome(shellmatta_handle_t handle, const char *arguments, uint32_t length)
|
|
|
|
{
|
|
|
|
|
|
|
|
shellmatta_write(handle, "blubb\r\n", 7u);
|
|
|
|
|
2020-03-08 19:56:04 +01:00
|
|
|
shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false);
|
|
|
|
|
2020-03-01 17:53:27 +01:00
|
|
|
(void)arguments;
|
|
|
|
(void)length;
|
|
|
|
|
2019-06-25 23:37:13 +02:00
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
shellmatta_cmd_t doSomeCmd = {"adoSome2", "adof2", "Function does something", "use me, please", doSome, NULL};
|
|
|
|
|
2020-02-03 21:35:20 +01:00
|
|
|
static shellmatta_retCode_t removeCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
|
|
|
|
{
|
|
|
|
shellmatta_printf(handle, "removing command: %s\r\n", doSomeCmd.cmd);
|
|
|
|
|
|
|
|
shellmatta_removeCmd(handle, &doSomeCmd);
|
|
|
|
|
2020-03-01 17:53:27 +01:00
|
|
|
(void)arguments;
|
|
|
|
(void)length;
|
|
|
|
|
2020-02-03 21:35:20 +01:00
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
shellmatta_cmd_t removeCommand = {"remove", "r", "Function removes a command", "", removeCmdFct, NULL};
|
|
|
|
|
2019-06-25 23:37:13 +02:00
|
|
|
|
|
|
|
static shellmatta_retCode_t quit(shellmatta_handle_t handle, const char *arguments, uint32_t length)
|
|
|
|
{
|
|
|
|
exitRequest = true;
|
|
|
|
|
2020-03-01 17:53:27 +01:00
|
|
|
(void)handle;
|
|
|
|
(void)arguments;
|
|
|
|
(void)length;
|
|
|
|
|
2019-06-25 23:37:13 +02:00
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
shellmatta_cmd_t quitCommand = {"quit", "q", "Function quits the shell", "", quit, NULL};
|
|
|
|
|
2020-03-01 18:45:30 +01:00
|
|
|
static shellmatta_retCode_t empty(shellmatta_handle_t handle, const char *arguments, uint32_t length)
|
|
|
|
{
|
|
|
|
(void)arguments;
|
|
|
|
(void)length;
|
|
|
|
|
|
|
|
shellmatta_printf(handle, "empty function called\r\n");
|
2020-03-08 19:56:04 +01:00
|
|
|
shellmatta_configure(handle, SHELLMATTA_MODE_OVERWRITE, true);
|
2020-03-01 18:45:30 +01:00
|
|
|
|
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
shellmatta_cmd_t emptyCommand = {"empty", NULL, NULL, NULL, empty, NULL};
|
|
|
|
|
2020-03-01 21:07:08 +01:00
|
|
|
static shellmatta_retCode_t reset(shellmatta_handle_t handle, const char *arguments, uint32_t length)
|
|
|
|
{
|
|
|
|
(void)arguments;
|
|
|
|
(void)length;
|
|
|
|
|
|
|
|
if(0 == strncmp(arguments, "prompt", length))
|
|
|
|
{
|
|
|
|
shellmatta_resetShell(handle, true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
shellmatta_resetShell(handle, false);
|
|
|
|
}
|
|
|
|
|
2020-03-08 19:56:04 +01:00
|
|
|
shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, true);
|
|
|
|
|
2020-03-01 21:07:08 +01:00
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
shellmatta_cmd_t resetCommand = {"reset", NULL, "resets the shellmatta instance", "reset [prompt]", reset, NULL};
|
|
|
|
|
2019-06-25 23:37:13 +02:00
|
|
|
|
|
|
|
shellmatta_retCode_t writeFct(const char* data, uint32_t length)
|
|
|
|
{
|
2019-07-28 22:28:54 +02:00
|
|
|
write(f, data, length);
|
2019-06-25 23:37:13 +02:00
|
|
|
|
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
|
2020-03-01 17:53:27 +01:00
|
|
|
int main(int argc, char **argv)
|
2019-06-25 23:37:13 +02:00
|
|
|
{
|
2019-07-27 16:31:19 +02:00
|
|
|
static char buffer[1024];
|
2019-06-27 22:56:03 +02:00
|
|
|
static char historyBuffer[4096];
|
2019-06-25 23:37:13 +02:00
|
|
|
static shellmatta_instance_t instance;
|
|
|
|
|
2020-03-01 17:53:27 +01:00
|
|
|
if(2 != argc)
|
|
|
|
{
|
|
|
|
printf("%s <serial device>\n", argv[0u]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
f = open(argv[1u], O_RDWR | O_SYNC);
|
2019-06-25 23:37:13 +02:00
|
|
|
|
|
|
|
if (f < 0)
|
|
|
|
{
|
2020-03-01 17:53:27 +01:00
|
|
|
printf("failure opening device %s %d\n", argv[1u], errno);
|
2019-06-25 23:37:13 +02:00
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_blocking (f, 1);
|
|
|
|
|
|
|
|
shellmatta_doInit( &instance,
|
|
|
|
&handle,
|
|
|
|
buffer,
|
|
|
|
sizeof(buffer),
|
|
|
|
historyBuffer,
|
|
|
|
sizeof(historyBuffer),
|
|
|
|
"shellmatta->",
|
|
|
|
NULL,
|
|
|
|
writeFct);
|
2019-07-28 22:28:54 +02:00
|
|
|
shellmatta_addCmd(handle, &doSomethingCmd);
|
|
|
|
shellmatta_addCmd(handle, &doSomeCmd);
|
|
|
|
shellmatta_addCmd(handle, &quitCommand);
|
2020-02-03 21:35:20 +01:00
|
|
|
shellmatta_addCmd(handle, &removeCommand);
|
2020-03-01 18:45:30 +01:00
|
|
|
shellmatta_addCmd(handle, &emptyCommand);
|
2020-03-01 21:07:08 +01:00
|
|
|
shellmatta_addCmd(handle, &resetCommand);
|
2019-06-25 23:37:13 +02:00
|
|
|
|
|
|
|
while(exitRequest == false)
|
|
|
|
{
|
|
|
|
char c;
|
|
|
|
|
|
|
|
int res = 0;
|
|
|
|
res = read (f, &c, 1);
|
|
|
|
|
|
|
|
fprintf(stdout, "0x%02x \n", c);
|
|
|
|
fflush(stdout);
|
|
|
|
|
2019-07-28 22:28:54 +02:00
|
|
|
shellmatta_processData(handle, &c, res);
|
2019-06-25 23:37:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(f);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|