-
Notifications
You must be signed in to change notification settings - Fork 12
/
microBox.h
141 lines (121 loc) · 3.79 KB
/
microBox.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
138
139
140
141
/*
microBox.h - Library for Linux-Shell like interface for Arduino.
Created by Sebastian Duell, 06.02.2015.
More info under http://sebastian-duell.de
Released under GPLv3.
*/
#ifndef _BASHCMD_H_
#define _BASHCMD_H_
#define __PROG_TYPES_COMPAT__
#include <Arduino.h>
#define MAX_CMD_NUM 20
#define MAX_CMD_BUF_SIZE 40
#define MAX_PATH_LEN 10
#define PARTYPE_INT 0x01
#define PARTYPE_DOUBLE 0x02
#define PARTYPE_STRING 0x04
#define PARTYPE_RW 0x10
#define PARTYPE_RO 0x00
#define ESC_STATE_NONE 0
#define ESC_STATE_START 1
#define ESC_STATE_CODE 2
#define TELNET_IAC 255
#define TELNET_WILL 251
#define TELNET_WONT 252
#define TELNET_DO 253
#define TELNET_DONT 254
#define TELNET_OPTION_ECHO 1
#define TELNET_OPTION_SGA 3
#define TELNET_STATE_NORMAL 0
#define TELNET_STATE_IAC 1
#define TELNET_STATE_WILL 2
#define TELNET_STATE_WONT 3
#define TELNET_STATE_DO 4
#define TELNET_STATE_DONT 5
#define TELNET_STATE_CLOSE 6
typedef struct
{
const char *cmdName;
void (*cmdFunc)(char **param, uint8_t parCnt);
}CMD_ENTRY;
typedef struct
{
const char *paramName;
void *pParam;
uint8_t parType;
uint8_t len;
void (*setFunc)(uint8_t id);
void (*getFunc)(uint8_t id);
uint8_t id;
}PARAM_ENTRY;
class microBox
{
public:
microBox();
~microBox();
void begin(PARAM_ENTRY *pParams, const char* hostName, bool localEcho=true, char *histBuf=NULL, int historySize=0);
void cmdParser();
bool isTimeout(unsigned long *lastTime, unsigned long intervall);
bool AddCommand(const char *cmdName, void (*cmdFunc)(char **param, uint8_t parCnt));
private:
static void ListDirCB(char **pParam, uint8_t parCnt);
static void ListLongCB(char **pParam, uint8_t parCnt);
static void ChangeDirCB(char **pParam, uint8_t parCnt);
static void EchoCB(char **pParam, uint8_t parCnt);
static void CatCB(char** pParam, uint8_t parCnt);
static void watchCB(char** pParam, uint8_t parCnt);
static void watchcsvCB(char** pParam, uint8_t parCnt);
static void LoadParCB(char **pParam, uint8_t parCnt);
static void SaveParCB(char **pParam, uint8_t parCnt);
void ListDir(char **pParam, uint8_t parCnt, bool listLong=false);
void ChangeDir(char **pParam, uint8_t parCnt);
void Echo(char **pParam, uint8_t parCnt);
void Cat(char** pParam, uint8_t parCnt);
void watch(char** pParam, uint8_t parCnt);
void watchcsv(char** pParam, uint8_t parCnt);
private:
void ShowPrompt();
uint8_t ParseCmdParams(char *pParam);
void ErrorDir(const __FlashStringHelper *cmd);
char *GetDir(char *pParam, bool useFile);
char *GetFile(char *pParam);
void PrintParam(uint8_t idx);
int8_t GetParamIdx(char* pParam, bool partStr = false, int8_t startIdx = 0);
int8_t GetCmdIdx(char* pCmd, int8_t startIdx = 0);
uint8_t Cat_int(char* pParam);
void ListDirHlp(bool dir, bool rw = true, int len=4096);
uint8_t ParCmp(uint8_t idx1, uint8_t idx2, bool cmd=false);
void HandleTab();
void HistoryUp();
void HistoryDown();
void HistoryPrintHlpr();
void AddToHistory(char *buf);
void ExecCommand();
void handleTelnet(uint8_t ch);
void sendTelnetOpt(uint8_t option, uint8_t value);
double parseFloat(char *pBuf);
bool HandleEscSeq(unsigned char ch);
void ReadWriteParamEE(bool write);
private:
char currentDir[MAX_PATH_LEN];
char cmdBuf[MAX_CMD_BUF_SIZE];
char dirBuf[15];
char *ParmPtr[10];
uint8_t bufPos;
bool watchMode;
bool csvMode;
uint8_t escSeq;
unsigned long watchTimeout;
const char* machName;
int historyBufSize;
char *historyBuf;
int historyWrPos;
int historyCursorPos;
bool locEcho;
uint8_t stateTelnet;
static CMD_ENTRY Cmds[MAX_CMD_NUM];
PARAM_ENTRY *Params;
static const char dirList[][5] PROGMEM;
};
extern microBox microbox;
#endif