main.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * main.c
  3. *
  4. * Created on: Jun 10, 2019
  5. * Author: stefan
  6. */
  7. #include "shellmatta.h"
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <ncurses.h>
  11. #include <stdbool.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>
  14. #include <termios.h>
  15. #include <string.h>
  16. #include <errno.h>
  17. static bool exitRequest = false;
  18. int f;
  19. shellmatta_handle_t handle;
  20. void set_blocking (int fd, int should_block)
  21. {
  22. struct termios tty;
  23. memset (&tty, 0, sizeof tty);
  24. if (tcgetattr (fd, &tty) != 0)
  25. {
  26. printf ("error %d from tggetattr", errno);
  27. return;
  28. }
  29. tty.c_cc[VMIN] = should_block ? 1 : 0;
  30. tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
  31. if (tcsetattr (fd, TCSANOW, &tty) != 0)
  32. printf ("error %d setting term attributes", errno);
  33. }
  34. static shellmatta_retCode_t doSomething(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  35. {
  36. shellmatta_printf(handle, "%s - length: %u", arguments, length);
  37. return SHELLMATTA_OK;
  38. }
  39. shellmatta_cmd_t doSomethingCmd = {"doSomething", "do", "Function does something", "use me, please", doSomething, NULL};
  40. static shellmatta_retCode_t doSome(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  41. {
  42. shellmatta_write(handle, "blubb\r\n", 7u);
  43. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, false, '\r');
  44. (void)arguments;
  45. (void)length;
  46. return SHELLMATTA_OK;
  47. }
  48. shellmatta_cmd_t doSomeCmd = {"adoSome2", "adof2", "Function does something", "use me, please", doSome, NULL};
  49. static shellmatta_retCode_t removeCmdFct(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  50. {
  51. shellmatta_printf(handle, "removing command: %s\r\n", doSomeCmd.cmd);
  52. shellmatta_removeCmd(handle, &doSomeCmd);
  53. (void)arguments;
  54. (void)length;
  55. return SHELLMATTA_OK;
  56. }
  57. shellmatta_cmd_t removeCommand = {"remove", "r", "Function removes a command", "", removeCmdFct, NULL};
  58. static shellmatta_retCode_t quit(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  59. {
  60. exitRequest = true;
  61. (void)handle;
  62. (void)arguments;
  63. (void)length;
  64. return SHELLMATTA_OK;
  65. }
  66. shellmatta_cmd_t quitCommand = {"quit", "q", "Function quits the shell", "", quit, NULL};
  67. static shellmatta_retCode_t empty(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  68. {
  69. (void)arguments;
  70. (void)length;
  71. shellmatta_printf(handle, "empty function called\r\n");
  72. shellmatta_configure(handle, SHELLMATTA_MODE_OVERWRITE, true, '\r');
  73. return SHELLMATTA_OK;
  74. }
  75. shellmatta_cmd_t emptyCommand = {"empty", NULL, NULL, NULL, empty, NULL};
  76. static shellmatta_retCode_t reset(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  77. {
  78. (void)arguments;
  79. (void)length;
  80. if(0 == strncmp(arguments, "prompt", length))
  81. {
  82. shellmatta_resetShell(handle, true);
  83. }
  84. else
  85. {
  86. shellmatta_resetShell(handle, false);
  87. }
  88. shellmatta_configure(handle, SHELLMATTA_MODE_INSERT, true, '\r');
  89. return SHELLMATTA_OK;
  90. }
  91. shellmatta_cmd_t resetCommand = {"reset", NULL, "resets the shellmatta instance", "reset [prompt]", reset, NULL};
  92. static shellmatta_retCode_t continuous(shellmatta_handle_t handle, const char *arguments, uint32_t length)
  93. {
  94. (void)arguments;
  95. (void)length;
  96. shellmatta_retCode_t ret = SHELLMATTA_CONTINUE;
  97. uint32_t stdinLength;
  98. char *stdinData;
  99. shellmatta_read(handle, &stdinData, &stdinLength);
  100. if(NULL != stdinData)
  101. {
  102. if('x' == stdinData[0u])
  103. {
  104. ret = SHELLMATTA_OK;
  105. }
  106. stdinData[0u] ++;
  107. shellmatta_write(handle, stdinData, stdinLength);
  108. }
  109. return ret;
  110. }
  111. shellmatta_cmd_t continuousCommand = {"continuous", "cont", "prints continously all input bytes", "continuous", continuous, NULL};
  112. shellmatta_retCode_t writeFct(const char* data, uint32_t length)
  113. {
  114. write(f, data, length);
  115. return SHELLMATTA_OK;
  116. }
  117. int main(int argc, char **argv)
  118. {
  119. static char buffer[1024];
  120. static char historyBuffer[4096];
  121. static shellmatta_instance_t instance;
  122. if(2 != argc)
  123. {
  124. printf("%s <serial device>\n", argv[0u]);
  125. return -1;
  126. }
  127. f = open(argv[1u], O_RDWR | O_SYNC);
  128. if (f < 0)
  129. {
  130. printf("failure opening device %s %d\n", argv[1u], errno);
  131. return f;
  132. }
  133. set_blocking (f, 1);
  134. shellmatta_doInit( &instance,
  135. &handle,
  136. buffer,
  137. sizeof(buffer),
  138. historyBuffer,
  139. sizeof(historyBuffer),
  140. "shellmatta->",
  141. NULL,
  142. writeFct);
  143. shellmatta_addCmd(handle, &doSomethingCmd);
  144. shellmatta_addCmd(handle, &doSomeCmd);
  145. shellmatta_addCmd(handle, &quitCommand);
  146. shellmatta_addCmd(handle, &removeCommand);
  147. shellmatta_addCmd(handle, &emptyCommand);
  148. shellmatta_addCmd(handle, &resetCommand);
  149. shellmatta_addCmd(handle, &continuousCommand);
  150. while(exitRequest == false)
  151. {
  152. char c;
  153. int res = 0;
  154. res = read (f, &c, 1);
  155. fprintf(stdout, "0x%02x \n", c);
  156. fflush(stdout);
  157. shellmatta_processData(handle, &c, res);
  158. }
  159. close(f);
  160. return 0;
  161. }