-
Notifications
You must be signed in to change notification settings - Fork 1
/
ELCommand.h
137 lines (105 loc) · 3.44 KB
/
ELCommand.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#ifndef _ELCOMMAND_H_
#define _ELCOMMAND_H_
/*
Author: Brent Pease (embeddedlibraryfeedback@gmail.com)
The MIT License (MIT)
Copyright (c) 2015-FOREVER Brent Pease
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
ABOUT
This module provides a common mechanism to register commands coming over a generalized port in a similar fashion to command line programs
*/
#include "ELModule.h"
#include "ELOutput.h"
enum
{
eCmd_Failed = 0,
eCmd_Succeeded,
eCmd_Pending,
eCmd_MaxNameLen = 15,
eCmd_MaxCommands = 64,
eCmd_MaxCommandArgs = 64,
};
// This is a convenience macro for registering commands
#define MCommandRegister(inCommandName, inCommandMethod, inDescription) gCommandModule->RegisterCommand(inCommandName, this, static_cast<TCmdHandlerMethod>(&inCommandMethod), inDescription)
// A dummy class for the command handler method object
class ICmdHandler
{
public:
};
// The typedef for the command handler method
typedef uint8_t
(ICmdHandler::*TCmdHandlerMethod)(
IOutputDirector* inOutput,
int inArgC,
char const* inArgV[]);
class CModule_SerialCmdHandler : public CModule
{
public:
MModule_Declaration(CModule_SerialCmdHandler)
private:
CModule_SerialCmdHandler(
);
void
Update(
uint32_t inDeltaTimeUS);
char charBuffer[256];
int curIndex;
};
class CModule_Command : public CModule, public ICmdHandler
{
public:
MModule_Declaration(CModule_Command)
// Register a command handler
void
RegisterCommand(
char const* inCmdName, // The name of the command, must be a static string
ICmdHandler* inCmdHandler, // The object of the command handler
TCmdHandlerMethod inMethod, // The method of the command handler
char const* inDescription = NULL); // An optional description, must be a static string
// This will process the given command args
uint8_t
ProcessCommand(
IOutputDirector* inOutput,
int inArgC,
char const* inArgV[]);
// This will process the given command string
uint8_t
ProcessCommand(
IOutputDirector* inOutput,
char* inCmdStr); // This input command string must be writable in order to break up into discrete arg strings
private:
CModule_Command(
);
uint8_t
HelpCommand(
IOutputDirector* inOutput,
int inArgC,
char const* inArgV[]);
struct SCommand
{
char const* name;
char const* description;
ICmdHandler* handler;
TCmdHandlerMethod method;
};
int handlerCount;
SCommand commandList[eCmd_MaxCommands];
};
extern CModule_Command* gCommandModule;
#endif /* _ELCOMMAND_H_ */