-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSystemCommanding.h
More file actions
253 lines (206 loc) · 6.01 KB
/
SystemCommanding.h
File metadata and controls
253 lines (206 loc) · 6.01 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
This file is part of the DSP-Crowd project
https://www.dsp-crowd.com
Author(s):
- Johannes Natter, office@dsp-crowd.com
File created on 30.11.2019
Copyright (C) 2019, Johannes Natter
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.
*/
#ifndef SYSTEM_COMMANDING_H
#define SYSTEM_COMMANDING_H
#include <string>
#include <list>
#include <functional>
#include "Processing.h"
#include "TcpTransfering.h"
// Banana optimization
using FuncCommand = std::function<void (char *pArgs, char *pBuf, char *pBufEnd)>;
#define BIND_MEMBER_FN(fn) [this](auto&&... args) -> decltype(auto) { return this->fn(std::forward<decltype(args)>(args)...); }
struct SystemCommand
{
std::string id;
FuncCommand func;
std::string shortcut;
std::string desc;
std::string group;
};
const std::string cInternalCmdCls = "dbg";
void cmdReg(
const std::string &id,
FuncCommand cmdFunc,
const std::string &shortcut = "",
const std::string &desc = "",
const std::string &group = "");
//void procReg(const std::string &group, const std::string &id, const std::string &shortcut, COMMANDING PROCESS CREATE FUNCTION, const std::string &desc);
#ifndef CONFIG_CMD_SIZE_HISTORY
#define CONFIG_CMD_SIZE_HISTORY 5
#endif
#ifndef CONFIG_CMD_SIZE_BUFFER_IN
#define CONFIG_CMD_SIZE_BUFFER_IN 29
#endif
#ifndef CONFIG_CMD_SIZE_BUFFER_OUT
#define CONFIG_CMD_SIZE_BUFFER_OUT 507
#endif
const int16_t cNumCmdInBuffer = 1 + CONFIG_CMD_SIZE_HISTORY;
const size_t cSizeBufCmdIn = CONFIG_CMD_SIZE_BUFFER_IN;
const size_t cSizeBufCmdOut = CONFIG_CMD_SIZE_BUFFER_OUT;
const size_t cIdxColMax = cSizeBufCmdIn - 1;
class SystemCommanding : public Processing
{
public:
static SystemCommanding *create(SOCKET fd)
{
return new (std::nothrow) SystemCommanding(fd);
}
void modeAutoSet() { mModeAuto = true; }
protected:
virtual ~SystemCommanding() {}
private:
SystemCommanding(SOCKET fd);
SystemCommanding()
: Processing("")
, mSocketFd(INVALID_SOCKET)
, mpTrans(NULL)
, mStateKey(0)
, mStartMs(0)
, mModeAuto(false)
, mTermChanged(false)
, mDone(false)
, mLastKeyWasTab(false)
, mIdxLineEdit(0)
, mIdxLineView(0)
#if CONFIG_CMD_SIZE_HISTORY
, mIdxLineLast(-1)
#endif
, mIdxColCursor(0)
, mIdxColLineEnd(0)
{
mBufOut[0] = 0;
mState = 0;
for (size_t i = 0; i < cNumCmdInBuffer; ++i)
mCmdInBuf[i][0] = 0;
}
SystemCommanding(const SystemCommanding &)
: Processing("")
, mSocketFd(INVALID_SOCKET)
, mpTrans(NULL)
, mStateKey(0)
, mStartMs(0)
, mModeAuto(false)
, mTermChanged(false)
, mDone(false)
, mLastKeyWasTab(false)
, mIdxLineEdit(0)
, mIdxLineView(0)
#if CONFIG_CMD_SIZE_HISTORY
, mIdxLineLast(-1)
#endif
, mIdxColCursor(0)
, mIdxColLineEnd(0)
{
mBufOut[0] = 0;
mState = 0;
for (size_t i = 0; i < cNumCmdInBuffer; ++i)
mCmdInBuf[i][0] = 0;
}
SystemCommanding &operator=(const SystemCommanding &)
{
mSocketFd = INVALID_SOCKET;
mpTrans = NULL;
mStateKey = 0;
mStartMs = 0;
mModeAuto = false;
mTermChanged = false;
mDone = false;
mLastKeyWasTab = false;
mIdxLineEdit = 0;
mIdxLineView = 0;
#if CONFIG_CMD_SIZE_HISTORY
mIdxLineLast = -1;
#endif
mIdxColCursor = 0;
mIdxColLineEnd = 0;
mBufOut[0] = 0;
mState = 0;
for (size_t i = 0; i < cNumCmdInBuffer; ++i)
mCmdInBuf[i][0] = 0;
return *this;
}
/*
* Naming of functions: objectVerb()
* Example: peerAdd()
*/
/* member functions */
Success process();
Success shutdown();
Success autoCommandReceive();
void dataReceive();
void tabProcess();
void cmdAutoComplete();
void cmdCandidatesShow();
void cmdCandidatesGet(std::list<const char *> &listCandidates);
void lineAck();
void commandExecute();
#if CONFIG_CMD_SIZE_HISTORY
void historyInsert();
bool historyNavigate(uint16_t key);
#endif
bool bufferEdit(uint16_t key);
bool chRemove(uint16_t key);
bool chInsert(uint16_t key);
bool cursorJump(uint16_t key);
void promptSend(bool cursor = true, bool preNewLine = false, bool postNewLine = false);
bool keyIsInsert(uint16_t key);
bool keyIsAlphaNum(uint16_t key);
void lfToCrLf(char *pBuf, std::string &str);
void processInfo(char *pBuf, char *pBufEnd);
void globalInit();
Success ansiFilter(uint8_t key, uint16_t *pKeyOut);
/* member variables */
SOCKET mSocketFd;
TcpTransfering *mpTrans;
uint32_t mStateKey;
uint32_t mStartMs;
bool mModeAuto;
bool mTermChanged;
bool mDone;
bool mLastKeyWasTab;
char mCmdInBuf[cNumCmdInBuffer][cSizeBufCmdIn];
int16_t mIdxLineEdit;
int16_t mIdxLineView;
#if CONFIG_CMD_SIZE_HISTORY
int16_t mIdxLineLast;
#endif
uint16_t mIdxColCursor;
uint16_t mIdxColLineEnd;
char mBufOut[cSizeBufCmdOut];
/* static functions */
static uint32_t millis();
static void cmdHelpPrint(char *pArgs, char *pBuf, char *pBufEnd);
static void cmdHexDump(char *pArgs, char *pBuf, char *pBufEnd);
static size_t hexDumpPrint(char *pBuf, char *pBufEnd,
const void *pData, size_t len,
const char *pName = NULL, size_t colWidth = 0x10);
/* static variables */
static bool globalInitDone;
#if CONFIG_PROC_HAVE_DRIVERS
static std::mutex mtxGlobalInit;
#endif
/* constants */
};
#endif