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;
|
|
|
|
|
|
|
|
void
|
|
|
|
set_blocking (int fd, int should_block)
|
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
shellmatta_cmd_t doSomeCmd = {"adoSome2", "adof2", "Function does something", "use me, please", doSome, NULL};
|
|
|
|
|
|
|
|
|
|
|
|
static shellmatta_retCode_t quit(shellmatta_handle_t handle, const char *arguments, uint32_t length)
|
|
|
|
{
|
|
|
|
exitRequest = true;
|
|
|
|
|
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
shellmatta_cmd_t quitCommand = {"quit", "q", "Function quits the shell", "", quit, NULL};
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
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;
|
|
|
|
|
2019-07-28 22:28:54 +02:00
|
|
|
f = open("/dev/pts/3", O_RDWR | O_SYNC);
|
2019-06-25 23:37:13 +02:00
|
|
|
|
|
|
|
if (f < 0)
|
|
|
|
{
|
|
|
|
printf("failure %d\n", errno);
|
|
|
|
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);
|
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;
|
|
|
|
}
|