You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.8 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  1. #include "cmd_scheduler.hpp"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "sdk\components\zprotocols\errorcode\errorcode.hpp"
  5. using namespace iflytop;
  6. #define TAG "CmdScheduler"
  7. void CmdScheduler::registerCmd(std::string cmd, const char* helpinfo, int npara, call_cmd_t call_cmd) {
  8. CMD cmdinfo;
  9. cmdinfo.call_cmd = call_cmd;
  10. cmdinfo.help_info = helpinfo;
  11. cmdinfo.npara = npara;
  12. m_cmdMap[cmd] = cmdinfo;
  13. }
  14. void CmdScheduler::regbasiccmd() {
  15. this->registerCmd("help", "", 0, [this](Context* context) {
  16. ZLOGI(TAG, "help");
  17. ZLOGI(TAG, "cmdlist:");
  18. for (auto it = m_cmdMap.begin(); it != m_cmdMap.end(); it++) {
  19. ZLOGI(TAG, " %s %s", it->first.c_str(), it->second.help_info.c_str());
  20. }
  21. return (int32_t)0;
  22. });
  23. }
  24. void CmdScheduler::initialize(UART_HandleTypeDef* huart, uint32_t rxbufsize) {
  25. m_rxbufsize = rxbufsize;
  26. m_uart = new ZUART();
  27. ZASSERT(m_uart != NULL);
  28. ZUART::cfg_t cfg;
  29. cfg.huart = huart;
  30. cfg.rxbuffersize = rxbufsize;
  31. cfg.rxovertime_ms = 3;
  32. cfg.name = "CmdSchedulerUart";
  33. rxbuf = new char[rxbufsize + 1];
  34. ZASSERT(rxbuf != NULL);
  35. m_uart->initialize(&cfg);
  36. ZASSERT(m_uart->startRxIt());
  37. m_uart->setrxcb([this](uint8_t* data, size_t len) {
  38. if (m_dataisready) {
  39. return;
  40. }
  41. memcpy(rxbuf, data, len);
  42. rxbuf[len] = '\0';
  43. m_rxsize = len;
  44. m_dataisready = true;
  45. // on data ,in irq context
  46. });
  47. regbasiccmd();
  48. }
  49. void CmdScheduler::schedule() {
  50. if (!m_dataisready) {
  51. return;
  52. }
  53. for (int i = 0; i < m_rxsize; i++) {
  54. if (rxbuf[i] == '\r' || rxbuf[i] == '\n') {
  55. rxbuf[i] = '\0';
  56. }
  57. }
  58. for (int i = 0; i < m_rxsize; i++) {
  59. if (rxbuf[i] != '\0') {
  60. ZLOGI(TAG, "docmd: %s", &rxbuf[i]);
  61. int inext = strlen(&rxbuf[i]) + i;
  62. int32_t ecode = callcmd(&rxbuf[i]);
  63. i = inext;
  64. if (ecode != 0) {
  65. ZLOGE(TAG, "callcmd %s fail:%s(%d)", &rxbuf[i], err::error2str(ecode), ecode);
  66. break;
  67. }
  68. }
  69. }
  70. ZLOGI(TAG, "process_rx_cmd:end");
  71. m_dataisready = false;
  72. }
  73. void CmdScheduler::tx(const char* data, int len) { m_uart->tx((uint8_t*)data, len); }
  74. int32_t CmdScheduler::callcmd(const char* cmd) {
  75. int argc = 0;
  76. char* argv[10] = {0};
  77. memset(cmdcache, 0, sizeof(cmdcache));
  78. argc = 0;
  79. memset(argv, 0, sizeof(argv));
  80. strcpy(cmdcache, cmd);
  81. remove_note(cmdcache, strlen(cmdcache));
  82. prase_cmd(cmdcache, strlen(cmdcache), argc, argv);
  83. if (argc == 0) {
  84. return (int32_t)0;
  85. }
  86. // printf("argc:%d\n", argc);
  87. // for (size_t i = 0; i < argc; i++) {
  88. // printf("argv[%d]:%s\n", i, argv[i]);
  89. // }
  90. /**
  91. * @brief ָ
  92. */
  93. auto cmder = m_cmdMap.find(string(argv[0]));
  94. if (cmder != m_cmdMap.end()) {
  95. Context context;
  96. context.argc = argc;
  97. context.argv = argv;
  98. // ZLOGI(TAG, "callcmd:argc %d %d", argc, cmder->second.npara);
  99. if (cmder->second.npara != context.argc - 1) return err::kce_cmd_param_num_error;
  100. int32_t ret = cmder->second.call_cmd(&context);
  101. if (ret == 0) {
  102. ZLOGI(TAG, "success", argv[0]);
  103. }
  104. return ret;
  105. } else {
  106. return err::kce_cmd_not_found;
  107. }
  108. }
  109. void CmdScheduler::remove_note(char* input, int inputlen) {
  110. bool detect_note = false;
  111. for (size_t i = 0; i < inputlen; i++) {
  112. if (!detect_note && input[i] == '#') {
  113. detect_note = true;
  114. }
  115. if (detect_note) {
  116. input[i] = 0;
  117. }
  118. }
  119. }
  120. void CmdScheduler::prase_cmd(char* input, int inputlen, int& argc, char* argv[]) {
  121. for (size_t i = 0; input[i] == 0 || i < inputlen; i++) {
  122. if (input[i] == ' ' || input[i] == '\r' || input[i] == '\n') {
  123. input[i] = 0;
  124. }
  125. }
  126. int j = 0;
  127. for (int i = 0; input[i] == 0 || i < inputlen; i++) {
  128. if (input[i] != 0 && j == 0) {
  129. argv[argc++] = &input[i];
  130. j = 1;
  131. continue;
  132. }
  133. if (input[i] == 0 && j == 1) {
  134. j = 0;
  135. continue;
  136. }
  137. }
  138. }