123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- /*
- * 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)
- {
- shellmatta_printf(handle, "%s - length: %u", arguments, 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);
- (void)arguments;
- (void)length;
- return SHELLMATTA_OK;
- }
- shellmatta_cmd_t doSomeCmd = {"adoSome2", "adof2", "Function does something", "use me, please", doSome, NULL};
- 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);
- (void)arguments;
- (void)length;
- return SHELLMATTA_OK;
- }
- shellmatta_cmd_t removeCommand = {"remove", "r", "Function removes a command", "", removeCmdFct, NULL};
- static shellmatta_retCode_t quit(shellmatta_handle_t handle, const char *arguments, uint32_t length)
- {
- exitRequest = true;
- (void)handle;
- (void)arguments;
- (void)length;
- 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)
- {
- write(f, data, length);
- return SHELLMATTA_OK;
- }
- int main(int argc, char **argv)
- {
- static char buffer[1024];
- static char historyBuffer[4096];
- static shellmatta_instance_t instance;
- if(2 != argc)
- {
- printf("%s <serial device>\n", argv[0u]);
- return -1;
- }
- f = open(argv[1u], O_RDWR | O_SYNC);
- if (f < 0)
- {
- printf("failure opening device %s %d\n", argv[1u], errno);
- return f;
- }
- set_blocking (f, 1);
- shellmatta_doInit( &instance,
- &handle,
- buffer,
- sizeof(buffer),
- historyBuffer,
- sizeof(historyBuffer),
- "shellmatta->",
- NULL,
- writeFct);
- shellmatta_addCmd(handle, &doSomethingCmd);
- shellmatta_addCmd(handle, &doSomeCmd);
- shellmatta_addCmd(handle, &quitCommand);
- shellmatta_addCmd(handle, &removeCommand);
- while(exitRequest == false)
- {
- char c;
- int res = 0;
- res = read (f, &c, 1);
- fprintf(stdout, "0x%02x \n", c);
- fflush(stdout);
- shellmatta_processData(handle, &c, res);
- }
- close(f);
- return 0;
- }
|