2019-06-25 23:37:13 +02:00
|
|
|
/*
|
2020-03-28 12:08:01 +01:00
|
|
|
* Copyright (c) 2019 Stefan Strobel <stefan.strobel@shimatta.net>
|
2019-06-25 23:37:13 +02:00
|
|
|
*
|
2020-03-28 12:08:01 +01:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
2019-06-25 23:37:13 +02:00
|
|
|
*/
|
|
|
|
|
2020-03-28 12:08:01 +01:00
|
|
|
/**
|
|
|
|
* @file main.c
|
|
|
|
* @brief main module to demonstrate use of the shellmatta.
|
|
|
|
* @author Stefan Strobel <stefan.strobel@shimatta.net>
|
|
|
|
*/
|
2019-06-25 23:37:13 +02:00
|
|
|
|
|
|
|
#include "shellmatta.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
|
|
static bool exitRequest = false;
|
|
|
|
int f;
|
|
|
|
shellmatta_handle_t handle;
|
|
|
|
|
|
|
|
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-27 18:34:43 +01:00
|
|
|
shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
|
2020-03-08 19:56:04 +01:00
|
|
|
|
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-27 18:34:43 +01:00
|
|
|
shellmatta_configure(handle, SHELLMATTA_MODE_OVERWRITE, true, '\r');
|
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)
|
|
|
|
{
|
2020-03-28 12:08:01 +01:00
|
|
|
shellmatta_retCode_t ret;
|
2020-03-01 21:07:08 +01:00
|
|
|
(void)arguments;
|
|
|
|
(void)length;
|
2020-03-28 12:08:01 +01:00
|
|
|
char option;
|
|
|
|
char *argument;
|
|
|
|
uint32_t argLen;
|
|
|
|
bool printPrompt = false;
|
2020-03-01 21:07:08 +01:00
|
|
|
|
2020-03-28 12:08:01 +01:00
|
|
|
static const shellmatta_opt_long_t options[] =
|
2020-03-01 21:07:08 +01:00
|
|
|
{
|
2020-03-28 12:08:01 +01:00
|
|
|
{"prompt", 'p', SHELLMATTA_OPT_ARG_REQUIRED},
|
|
|
|
{NULL, '\0', SHELLMATTA_OPT_ARG_NONE}
|
|
|
|
};
|
|
|
|
|
|
|
|
ret = shellmatta_opt_long(handle, options, &option, &argument, &argLen);
|
|
|
|
while(SHELLMATTA_OK == ret)
|
2020-03-01 21:07:08 +01:00
|
|
|
{
|
2020-03-28 12:08:01 +01:00
|
|
|
switch(option)
|
|
|
|
{
|
|
|
|
case 'p':
|
|
|
|
if(NULL != argument)
|
|
|
|
{
|
|
|
|
if(0 == strncmp("true", argument, 4u))
|
|
|
|
{
|
|
|
|
printPrompt = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
shellmatta_printf(handle, "Unknown option: %c\r\n", option);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ret = shellmatta_opt_long(handle, options, &option, &argument, &argLen);
|
2020-03-01 21:07:08 +01:00
|
|
|
}
|
|
|
|
|
2020-03-28 12:08:01 +01:00
|
|
|
shellmatta_resetShell(handle, printPrompt);
|
2020-03-27 18:34:43 +01:00
|
|
|
shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, true, '\r');
|
2020-03-08 19:56:04 +01:00
|
|
|
|
2020-03-01 21:07:08 +01:00
|
|
|
return SHELLMATTA_OK;
|
|
|
|
}
|
2020-03-28 12:08:01 +01:00
|
|
|
shellmatta_cmd_t resetCommand = {"reset", NULL, "resets the shellmatta instance", "reset [--prompt true/false]", reset, NULL};
|
2020-03-01 21:07:08 +01:00
|
|
|
|
2020-03-22 21:27:18 +01:00
|
|
|
static shellmatta_retCode_t continuous(shellmatta_handle_t handle, const char *arguments, uint32_t length)
|
|
|
|
{
|
|
|
|
(void)arguments;
|
|
|
|
(void)length;
|
|
|
|
|
|
|
|
shellmatta_retCode_t ret = SHELLMATTA_CONTINUE;
|
|
|
|
uint32_t stdinLength;
|
|
|
|
char *stdinData;
|
|
|
|
|
|
|
|
shellmatta_read(handle, &stdinData, &stdinLength);
|
|
|
|
if(NULL != stdinData)
|
|
|
|
{
|
|
|
|
if('x' == stdinData[0u])
|
|
|
|
{
|
|
|
|
ret = SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
stdinData[0u] ++;
|
|
|
|
shellmatta_write(handle, stdinData, stdinLength);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
shellmatta_cmd_t continuousCommand = {"continuous", "cont", "prints continously all input bytes", "continuous", continuous, NULL};
|
|
|
|
|
2020-03-28 12:08:01 +01:00
|
|
|
static shellmatta_retCode_t busy(shellmatta_handle_t handle, const char *arguments, uint32_t length)
|
|
|
|
{
|
|
|
|
(void)arguments;
|
|
|
|
(void)length;
|
|
|
|
static uint32_t callCnt = 0u;
|
|
|
|
shellmatta_retCode_t ret = SHELLMATTA_BUSY;
|
|
|
|
|
|
|
|
if(callCnt < 10u)
|
|
|
|
{
|
|
|
|
callCnt ++;
|
|
|
|
shellmatta_printf(handle, "%s - length %u - callCnt %u\r\n", arguments, length, callCnt);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
callCnt = 0u;
|
|
|
|
ret = SHELLMATTA_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
shellmatta_cmd_t busyCommand = {"busy", NULL, NULL, NULL, busy, 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2020-03-22 21:27:18 +01:00
|
|
|
shellmatta_addCmd(handle, &continuousCommand);
|
2020-03-28 12:08:01 +01:00
|
|
|
shellmatta_addCmd(handle, &busyCommand);
|
2019-06-25 23:37:13 +02:00
|
|
|
|
|
|
|
while(exitRequest == false)
|
|
|
|
{
|
|
|
|
char c;
|
2020-03-28 12:08:01 +01:00
|
|
|
shellmatta_retCode_t ret;
|
2019-06-25 23:37:13 +02:00
|
|
|
int res = 0;
|
|
|
|
res = read (f, &c, 1);
|
|
|
|
|
|
|
|
fprintf(stdout, "0x%02x \n", c);
|
|
|
|
fflush(stdout);
|
|
|
|
|
2020-03-28 12:08:01 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
ret = shellmatta_processData(handle, &c, res);
|
|
|
|
if(SHELLMATTA_BUSY == ret)
|
|
|
|
{
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
} while(SHELLMATTA_BUSY == ret);
|
2019-06-25 23:37:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
close(f);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|