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.

110 lines
3.1 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
  1. #pragma once
  2. #include <map>
  3. #include <string>
  4. #include "sdk/os/zos.hpp"
  5. #include "sdk\components\zprotocols\errorcode\errorcode.hpp"
  6. namespace iflytop {
  7. using namespace std;
  8. class CmdScheduler {
  9. public:
  10. class Context {
  11. public:
  12. int argc;
  13. char** argv;
  14. int getInt(int index, int defaultvalue = 0) {
  15. if (index >= argc) {
  16. return defaultvalue;
  17. }
  18. return atoi(argv[index]);
  19. }
  20. bool getBool(int index, bool defaultvalue = false) {
  21. if (index >= argc) {
  22. return defaultvalue;
  23. }
  24. return atoi(argv[index]) != 0;
  25. }
  26. float getFloat(int index, float defaultvalue = 0) {
  27. if (index >= argc) {
  28. return defaultvalue;
  29. }
  30. return atof(argv[index]);
  31. }
  32. const char* getString(int index, const char* defaultvalue = "") {
  33. if (index >= argc) {
  34. return defaultvalue;
  35. }
  36. return argv[index];
  37. }
  38. };
  39. typedef function<int32_t(Context* context)> call_cmd_t;
  40. class CMD {
  41. public:
  42. call_cmd_t call_cmd;
  43. string help_info;
  44. int npara;
  45. };
  46. private:
  47. map<string, CMD> m_cmdMap;
  48. ZUART* m_uart;
  49. char* rxbuf;
  50. int32_t m_rxsize = 0;
  51. uint32_t m_rxbufsize;
  52. bool m_dataisready = false;
  53. char cmdcache[1024] = {0};
  54. public:
  55. void initialize(UART_HandleTypeDef* huart, uint32_t rxbufsize);
  56. void registerCmd(std::string cmd, const char* helpinfo, int npara, call_cmd_t call_cmd);
  57. void tx(const char* data, int len);
  58. void schedule();
  59. private:
  60. void regbasiccmd();
  61. int32_t callcmd(const char* cmd);
  62. void prase_cmd(char* input, int inputlen, int& argc, char* argv[]);
  63. void remove_note(char* input, int inputlen);
  64. };
  65. #define DO_CMD(cond) \
  66. { \
  67. int32_t ret = cond; \
  68. if (ret != 0) { \
  69. return ret; \
  70. } \
  71. }
  72. #define IMPL_CMD(cmd, ...) \
  73. DO_CMD(findmodule(con->getInt(1), &module)); \
  74. DO_CMD(module->cmd(__VA_ARGS__)); \
  75. return (int32_t)0;
  76. #define IMPL_READ_STATE(cmd, ...) \
  77. DO_CMD(findmodule(con->getInt(1), &module)); \
  78. DO_CMD(module->cmd(__VA_ARGS__)); \
  79. cmd_dump_ack(ack); \
  80. return (int32_t)0;
  81. #define REG_CMD___NO_ACK(prefix, cmd, para, npara, ...) /**/ \
  82. m_cmdScheduler->registerCmd(prefix #cmd, para, npara, [this](CmdScheduler::Context* con) { /**/ \
  83. IMPL_CMD(cmd, __VA_ARGS__); /**/ \
  84. });
  85. #define REG_CMD_WITH_ACK(prefix, cmd, para, npara, acktype, ...) /**/ \
  86. m_cmdScheduler->registerCmd(prefix #cmd, para, npara, [this](CmdScheduler::Context* con) { /**/ \
  87. acktype ack; /**/ \
  88. IMPL_READ_STATE(cmd, __VA_ARGS__); /**/ \
  89. });
  90. } // namespace iflytop