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.1 KiB
111 lines
3.1 KiB
#pragma once
|
|
#include <map>
|
|
#include <string>
|
|
|
|
#include "sdk/os/zos.hpp"
|
|
#include "sdk\components\zprotocols\errorcode\errorcode.hpp"
|
|
|
|
namespace iflytop {
|
|
using namespace std;
|
|
|
|
class CmdScheduler {
|
|
public:
|
|
class Context {
|
|
public:
|
|
int argc;
|
|
char** argv;
|
|
|
|
int getInt(int index, int defaultvalue = 0) {
|
|
if (index >= argc) {
|
|
return defaultvalue;
|
|
}
|
|
return atoi(argv[index]);
|
|
}
|
|
|
|
bool getBool(int index, bool defaultvalue = false) {
|
|
if (index >= argc) {
|
|
return defaultvalue;
|
|
}
|
|
return atoi(argv[index]) != 0;
|
|
}
|
|
|
|
float getFloat(int index, float defaultvalue = 0) {
|
|
if (index >= argc) {
|
|
return defaultvalue;
|
|
}
|
|
return atof(argv[index]);
|
|
}
|
|
const char* getString(int index, const char* defaultvalue = "") {
|
|
if (index >= argc) {
|
|
return defaultvalue;
|
|
}
|
|
return argv[index];
|
|
}
|
|
};
|
|
|
|
typedef function<int32_t(Context* context)> call_cmd_t;
|
|
|
|
class CMD {
|
|
public:
|
|
call_cmd_t call_cmd;
|
|
string help_info;
|
|
int npara;
|
|
};
|
|
|
|
private:
|
|
map<string, CMD> m_cmdMap;
|
|
|
|
ZUART* m_uart;
|
|
char* rxbuf;
|
|
int32_t m_rxsize = 0;
|
|
|
|
uint32_t m_rxbufsize;
|
|
|
|
bool m_dataisready = false;
|
|
|
|
char cmdcache[1024] = {0};
|
|
|
|
public:
|
|
void initialize(UART_HandleTypeDef* huart, uint32_t rxbufsize);
|
|
void registerCmd(std::string cmd, const char* helpinfo, int npara, call_cmd_t call_cmd);
|
|
void tx(const char* data, int len);
|
|
|
|
void schedule();
|
|
|
|
private:
|
|
void regbasiccmd();
|
|
int32_t callcmd(const char* cmd);
|
|
void prase_cmd(char* input, int inputlen, int& argc, char* argv[]);
|
|
void remove_note(char* input, int inputlen);
|
|
};
|
|
|
|
#define DO_CMD(cond) \
|
|
{ \
|
|
int32_t ret = cond; \
|
|
if (ret != 0) { \
|
|
return ret; \
|
|
} \
|
|
}
|
|
#define IMPL_CMD(cmd, ...) \
|
|
DO_CMD(findmodule(con->getInt(1), &module)); \
|
|
DO_CMD(module->cmd(__VA_ARGS__)); \
|
|
return (int32_t)0;
|
|
|
|
#define IMPL_READ_STATE(cmd, ...) \
|
|
DO_CMD(findmodule(con->getInt(1), &module)); \
|
|
DO_CMD(module->cmd(__VA_ARGS__)); \
|
|
cmd_dump_ack(ack); \
|
|
return (int32_t)0;
|
|
|
|
#define REG_CMD___NO_ACK(prefix, cmd, para, npara, ...) /**/ \
|
|
m_cmdScheduler->registerCmd(prefix #cmd, para, npara, [this](CmdScheduler::Context* con) { /**/ \
|
|
IMPL_CMD(cmd, __VA_ARGS__); /**/ \
|
|
});
|
|
|
|
#define REG_CMD_WITH_ACK(prefix, cmd, para, npara, acktype, ...) /**/ \
|
|
m_cmdScheduler->registerCmd(prefix #cmd, para, npara, [this](CmdScheduler::Context* con) { /**/ \
|
|
acktype ack; /**/ \
|
|
IMPL_READ_STATE(cmd, __VA_ARGS__); /**/ \
|
|
});
|
|
|
|
} // namespace iflytop
|