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.

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