pfodParser 3.61.0 The pfodParser library is handles commands sent from the Android pfodApp, pfodApp supports WiFi, BLE, Bluetooth and SMS connections

pfodParser.h
Go to the documentation of this file.
1 #ifndef pfodParser_h
2 #define pfodParser_h
25 /*
26 (c)2014-2017 Forward Computing and Control Pty. Ltd.
27 NSW Australia, www.forward.com.au
28 This code is not warranted to be fit for any purpose. You may only use it at your own risk.
29 This code may be freely used for both private and commercial use
30 Provide this copyright is maintained.
31 */
32
33 #include <Arduino.h>
34 #include "pfodStream.h"
35 #include "pfodParserUtils.h"
36 #include "pfodDelay.h"
37 #include "pfodDwgs.h"
38 #include "pfodControl.h"
39 #include "pfodDrawing.h"
40 #include "pfodLinkedList.h"
41 //#include "pfodSecurity.h"
42
43 class pfodDrawing;
44
45 // used to suppress warning
46 #define pfod_MAYBE_UNUSED(x) (void)(x)
47
48 class pfodParser: public Print {
49 public:
50 pfodParser();
51 pfodParser(const char* version);
52 // methods required for Print
53 virtual size_t write(uint8_t c);
54 virtual size_t write(const uint8_t *buffer, size_t size);
55 virtual void flush(); // may do nothing calls io->flush()
56
57 // stream methods only useful to read the raw cmd
58 virtual int read();
59 virtual int peek();
60 virtual int available();
61 // you can reduce this value if you are sending shorter commands. Most pfod commands are very short <20 bytes, but depends on the pfod menu items you serve
62 // but never increase it.
63 static const byte pfodMaxMsgLen = 0xff; // == 255, if no closing } by now ignore msg
64
65 virtual void connect(Stream* ioPtr);
66 virtual void closeConnection();
67 virtual byte parse(); // call this in loop() every loop, it will read bytes, if any, from the pfodAppStream and parse them
68 // returns 0 if message not complete, else returns the first char of a completed and verified message
69 virtual bool isRefresh(); // starts with {version: and the version matches this parser's version
70 virtual const char *getVersionRequested(); // the version asked for in the command i.e. {versionRequested:...}
71 virtual const char* getVersion();
72 virtual void setVersion(const char* version); // no usually needed
73 virtual void sendVersion(); // send ~ version to parser.print
74 virtual void sendRefreshAndVersion(unsigned long refresh_mS); // send `refresh_mS ~ version to parser.print
75 virtual byte* getCmd();
76 virtual byte* getFirstArg();
77 virtual byte* getNextArg(byte *start);
78 virtual byte getArgsCount();
79 virtual byte* parseLong(byte* idxPtr, long *result);
80 virtual bool cmdEquals(const char* cmdStr); // returns true if parser cmd, as returned by getCmd() == cmdStr
81 virtual bool cmdEquals(const char cmdChar); // returns true if parser cmd as returned by getCmd() is just once char and == cmdChar
82 virtual bool cmdEquals(pfodAutoCmd &a_Cmd); // for load dwg cmds
83 virtual void addDwg(pfodDrawing *dwgPtr); // add a pfodDrawing to the list of drawings to be automatically processed by parse()
84
85
92 virtual byte getParserState();
93 virtual void setCmd(byte cmd);
94 static const byte pfodWaitingForStart = 0xff;
95 static const byte pfodMsgStarted = '{';
96 static const byte pfodRefresh = ':';
97 static const byte pfodInMsg = 0;
98 static const byte pfodMsgEnd = '}';
99 virtual void setDebugStream(Print* debugOut); // does nothing
100 virtual void setDebugOut(Print* out) { setDebugStream(out);}
101 virtual void setDebug(Print* out) { setDebugStream(out);}
102
103 virtual void setIdleTimeout(unsigned long timeout); // does nothing in parser
104 virtual Stream* getPfodAppStream(); // get the command response stream we are writing to
105 // for pfodParser this is also the rawData stream
106
107 // this is returned if pfodDevice should drop the connection
108 // only returned by pfodParser in read() returns -1
109 void init(); // for now do NOT make this virtual!!
110 virtual byte parse(byte in); // for now
111 virtual void ignoreSeqNum(); // for pfodSecurity so hash does not accidently drop a command.
112 virtual byte parseDwgCmd(); // returns the first byte of the dwgCmd str, often only one char long
113 virtual const byte* getDwgCmd(); // valid only after parseDwgCmd() called on image cmd
114 // returns true if dwgCmd string == cmdStr, uses strcmp( ) internally
115 virtual bool dwgCmdEquals(const char* dwgCmdStr); // valid only after parseDwgCmd() called on image cmd
116 virtual bool dwgCmdEquals(pfodAutoCmd &a_Cmd); // valid only after parseDwgCmd() called on image cmd
117 virtual bool dwgCmdEquals(const char dwgCmd); // valid only after parseDwgCmd() called on image cmd
118 virtual bool isTouch(); // default TOUCH even if not parsed
119 virtual bool isClick();
120 virtual bool isDown();
121 virtual bool isDrag();
122 virtual bool isUp();
123 virtual bool isPress();
124 // bool isEntry();
125 // bool isExit();
126 virtual const byte* getEditedText(); // [0] = '\0' if no editedText returned
127
128
129 virtual uint8_t getTouchType();
130 virtual int getTouchedCol(); // default 0
131 virtual int getTouchedRow(); // default 0
132 virtual int getTouchedY(); // default 0
133 virtual int getTouchedX(); // default 0
134 const static int TOUCH = 0;
135 const static int DOWN = 1;
136 const static int DRAG = 2;
137 const static int UP = 4;
138 const static int CLICK = 8;
139 const static int PRESS = 16;
140 // const static int ENTRY = 32;
141 // const static int EXIT = 64;
142 const static int DOWN_UP = 256; // only for touchZone filter send, never recieved by parser
143 const static int TOUCH_DISABLED = 512; // only for touchZone filter send, never recieved by parser
144
145 private:
146 void constructInit();
147 // findStr length MUST be >= replacePtr length!!
148 // input is const byte* BUT recast as (char*) and is modified by this method
149 // const byte* is to match getEditedText() return
150 void replace(const char* findStr, const char *replacePtr, char* buffer);
151 //static const byte DisconnectNow = '!';
152 Stream* io;
153 pfodLinkedList<pfodDrawing> listOfDrawings;
154 char emptyVersion[1];
155 byte emptyBytes[1];
156 byte missingEditedText[1];
157 byte argsCount; // no of arguments found in msg
158 byte argsIdx;
159 byte parserState;
160 byte args[pfodMaxMsgLen + 1]; // allow for trailing null
161 byte *versionStart;
162 const byte *activeCmdStart;
163 byte *editedText;
164 byte encodingProcessed;
165 uint8_t seqNum; // 0 if not set else last char before leading {
166 uint8_t lastSeqNum; // 0 if not set else last char before leading {
167 byte ignoreCmdSeqNum; // != 0 if should ignore them
168 uint8_t touchType;
169 int col;
170 int row;
171 byte *cmdStart;
172 bool refresh;
173 const char *version;
174 static const byte pfodBar = (byte)'|';
175 static const byte pfodTilda = (byte)'~';
176 static const byte pfodAccent = (byte)'`';
177 static const byte pfodArgStarted = 0xfe;
178 };
179
180 #endif // pfodParser_h
181
pfodDrawing
Definition: pfodDrawing.h:17
pfodLinkedList< pfodDrawing >
pfodParser
Definition: pfodParser.h:48
pfodParser::setCmd
virtual void setCmd(byte cmd)
pfodParser::getPfodAppStream
virtual Stream * getPfodAppStream()
pfodParser::connect
virtual void connect(Stream *ioPtr)
pfodParser::setDebugStream
virtual void setDebugStream(Print *debugOut)
pfodParser::isRefresh
virtual bool isRefresh()
pfodParser::isDown
virtual bool isDown()
pfodParser::closeConnection
virtual void closeConnection()
pfodParser::dwgCmdEquals
virtual bool dwgCmdEquals(const char dwgCmd)
pfodParser::isUp
virtual bool isUp()
pfodParser::getVersion
virtual const char * getVersion()
pfodParser::write
virtual size_t write(const uint8_t *buffer, size_t size)
pfodParser::cmdEquals
virtual bool cmdEquals(pfodAutoCmd &a_Cmd)
pfodParser::isPress
virtual bool isPress()
pfodParser::isTouch
virtual bool isTouch()
pfodParser::DRAG
static const int DRAG
Definition: pfodParser.h:136
pfodParser::CLICK
static const int CLICK
Definition: pfodParser.h:138
pfodParser::setVersion
virtual void setVersion(const char *version)
pfodParser::getArgsCount
virtual byte getArgsCount()
pfodParser::DOWN
static const int DOWN
Definition: pfodParser.h:135
pfodParser::isDrag
virtual bool isDrag()
pfodParser::getTouchedY
virtual int getTouchedY()
pfodParser::parse
virtual byte parse(byte in)
pfodParser::getParserState
virtual byte getParserState()
pfodWaitingForStart if outside msg pfodMsgStarted if just seen opening { pfodInMsg in msg after { pfo...
pfodParser::UP
static const int UP
Definition: pfodParser.h:137
pfodParser::PRESS
static const int PRESS
Definition: pfodParser.h:139
pfodParser::getDwgCmd
virtual const byte * getDwgCmd()
pfodParser::getNextArg
virtual byte * getNextArg(byte *start)
pfodParser::getCmd
virtual byte * getCmd()
pfodParser::parse
virtual byte parse()
pfodParser::dwgCmdEquals
virtual bool dwgCmdEquals(const char *dwgCmdStr)
pfodParser::getEditedText
virtual const byte * getEditedText()
pfodParser::peek
virtual int peek()
pfodParser::write
virtual size_t write(uint8_t c)
pfodParser::pfodInMsg
static const byte pfodInMsg
Definition: pfodParser.h:97
pfodParser::isClick
virtual bool isClick()
pfodParser::cmdEquals
virtual bool cmdEquals(const char *cmdStr)
pfodParser::getTouchedX
virtual int getTouchedX()
pfodParser::getTouchedRow
virtual int getTouchedRow()
pfodParser::sendRefreshAndVersion
virtual void sendRefreshAndVersion(unsigned long refresh_mS)
pfodParser::pfodParser
pfodParser(const char *version)
pfodParser::setDebug
virtual void setDebug(Print *out)
Definition: pfodParser.h:101
pfodParser::TOUCH
static const int TOUCH
Definition: pfodParser.h:134
pfodParser::sendVersion
virtual void sendVersion()
pfodParser::setDebugOut
virtual void setDebugOut(Print *out)
Definition: pfodParser.h:100
pfodParser::ignoreSeqNum
virtual void ignoreSeqNum()
pfodParser::pfodParser
pfodParser()
pfodParser::cmdEquals
virtual bool cmdEquals(const char cmdChar)
pfodParser::DOWN_UP
static const int DOWN_UP
Definition: pfodParser.h:142
pfodParser::pfodWaitingForStart
static const byte pfodWaitingForStart
Definition: pfodParser.h:94
pfodParser::pfodMaxMsgLen
static const byte pfodMaxMsgLen
Definition: pfodParser.h:63
pfodParser::getFirstArg
virtual byte * getFirstArg()
pfodParser::pfodRefresh
static const byte pfodRefresh
Definition: pfodParser.h:96
pfodParser::parseLong
virtual byte * parseLong(byte *idxPtr, long *result)
pfodParser::dwgCmdEquals
virtual bool dwgCmdEquals(pfodAutoCmd &a_Cmd)
pfodParser::available
virtual int available()
pfodParser::parseDwgCmd
virtual byte parseDwgCmd()
pfodParser::getTouchedCol
virtual int getTouchedCol()
pfodParser::addDwg
virtual void addDwg(pfodDrawing *dwgPtr)
pfodParser::getVersionRequested
virtual const char * getVersionRequested()
pfodParser::pfodMsgStarted
static const byte pfodMsgStarted
Definition: pfodParser.h:95
pfodParser::setIdleTimeout
virtual void setIdleTimeout(unsigned long timeout)
pfodParser::read
virtual int read()
pfodParser::TOUCH_DISABLED
static const int TOUCH_DISABLED
Definition: pfodParser.h:143
pfodParser::pfodMsgEnd
static const byte pfodMsgEnd
Definition: pfodParser.h:98
pfodParser::getTouchType
virtual uint8_t getTouchType()
pfodParser::init
void init()
pfodParser::flush
virtual void flush()
pfodControl.h
pfodDelay.h
pfodDrawing.h
pfodDwgs.h
pfodLinkedList.h
pfodParserUtils.h
pfodStream.h