pfodParser 5.0.1 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 void printDwgCmdReceived(Print * outPtr); // debug current dwg cmd
53
54 // methods required for Print
55 virtual size_t write(uint8_t c);
56 virtual size_t write(const uint8_t *buffer, size_t size);
57 virtual void flush(); // may do nothing calls io->flush()
58
59 // stream methods only useful to read the raw cmd
60 virtual int read();
61 virtual int peek();
62 virtual int available();
63 // 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
64 // but never increase it.
65 static const byte pfodMaxMsgLen = 0xff; // == 255, if no closing } by now ignore msg
66
67 virtual void connect(Stream* ioPtr);
68 virtual void closeConnection();
69 virtual byte parse(); // call this in loop() every loop, it will read bytes, if any, from the pfodAppStream and parse them
70 // returns 0 if message not complete, else returns the first char of a completed and verified message
71 virtual bool isRefresh(); // starts with {version: and the version matches this parser's version
72 virtual const char *getVersionRequested(); // the version asked for in the command i.e. {versionRequested:...}
73 virtual const char* getVersion();
74 virtual void setVersion(const char* version); // no usually needed
75 virtual void sendVersion(); // send ~ version to parser.print
76 virtual void sendRefreshAndVersion(unsigned long refresh_mS); // send `refresh_mS ~ version to parser.print
77 virtual byte* getCmd();
78 virtual byte* getFirstArg();
79 virtual byte* getNextArg(byte *start);
80 virtual byte getArgsCount();
81 virtual byte* parseLong(byte* idxPtr, long *result);
82 virtual bool cmdEquals(const char* cmdStr); // returns true if parser cmd, as returned by getCmd() == cmdStr
83 virtual bool cmdEquals(const char cmdChar); // returns true if parser cmd as returned by getCmd() is just once char and == cmdChar
84 virtual bool cmdEquals(pfodAutoCmd &a_Cmd); // for load dwg cmds
85 static void addDwg(pfodDrawing *dwgPtr); // add a pfodDrawing to the list of drawings to be automatically processed by parse()
86
87 void menu();
88 void menuUpdate();
89 void endOfMsg();
90 // menu items, these method just start the item, still need to output the button/label text etc
91 void label(pfodAutoCmd c);
92 void onOffDisplay(pfodAutoCmd c);
93 void button(pfodAutoCmd c);
94 void slider(pfodAutoCmd c);
95
102 virtual byte getParserState();
103 virtual void setCmd(byte cmd);
104 static const byte pfodWaitingForStart = 0xff;
105 static const byte pfodMsgStarted = '{';
106 static const byte pfodRefresh = ':';
107 static const byte pfodInMsg = 0;
108 static const byte pfodMsgEnd = '}';
109 virtual void setDebugStream(Print* debugOut); // does nothing
110 virtual void setDebugOut(Print* out) { setDebugStream(out);}
111 virtual void setDebug(Print* out) { setDebugStream(out);}
112
113 virtual void setIdleTimeout(unsigned long timeout_in_secs); // does set timeout in parser
114 virtual Stream* getPfodAppStream(); // get the command response stream we are writing to
115 // for pfodParser this is also the rawData stream
116
117 // this is returned if pfodDevice should drop the connection
118 // only returned by pfodParser in read() returns -1
119 void init(); // for now do NOT make this virtual!!
120 virtual byte parse(byte in); // for now
121 virtual void ignoreSeqNum(); // for pfodSecurity so hash does not accidently drop a command.
122 virtual byte parseDwgCmd(); // returns the first byte of the dwgCmd str, often only one char long
123 virtual const byte* getDwgCmd(); // valid only after parseDwgCmd() called on image cmd
124 // returns true if dwgCmd string == cmdStr, uses strcmp( ) internally
125 virtual bool dwgCmdEquals(const char* dwgCmdStr); // valid only after parseDwgCmd() called on image cmd
126 virtual bool dwgCmdEquals(pfodAutoCmd &a_Cmd); // valid only after parseDwgCmd() called on image cmd
127 virtual bool dwgCmdEquals(const char dwgCmd); // valid only after parseDwgCmd() called on image cmd
128 virtual bool isTouch(); // default TOUCH even if not parsed
129 virtual bool isClick();
130 virtual bool isDown();
131 virtual bool isDrag();
132 virtual bool isUp();
133 virtual bool isPress();
134 // bool isEntry();
135 // bool isExit();
136 virtual const byte* getEditedText(); // [0] = '\0' if no editedText returned
137
138
139 virtual uint8_t getTouchType();
140 virtual int getTouchedCol(); // default 0
141 virtual int getTouchedRow(); // default 0
142 virtual int getTouchedY(); // default 0
143 virtual int getTouchedX(); // default 0
144 // these are also defined in pfodDwgsBase.h
145 // defined here for use by parser menu processing.
146 const static int TOUCH = 0;
147 const static int DOWN = 1;
148 const static int DRAG = 2;
149 const static int UP = 4;
150 const static int CLICK = 8;
151 const static int PRESS = 16;
152 // const static int ENTRY = 32;
153 // const static int EXIT = 64;
154 const static int DOWN_UP = 256; // only for touchZone filter send, never recieved by parser
155 const static int DOWN_DRAG_UP = 256; // alias for DOWN_UP only for touchZone filter send, never recieved by parser
156 const static int TOUCH_DISABLED = 512; // only for touchZone filter send, never recieved by parser
157 // construct-on-first-use accessor for the global list of drawings — see pfodParser.cpp
158 // for why this must be a function-local static and not a static data member
159 static pfodLinkedList<pfodDrawing>& listOfDrawings();
160
161 private:
162 void constructInit();
163 // findStr length MUST be >= replacePtr length!!
164 // input is const byte* BUT recast as (char*) and is modified by this method
165 // const byte* is to match getEditedText() return
166 void replace(const char* findStr, const char *replacePtr, char* buffer);
167 //static const byte DisconnectNow = '!';
168 Stream* io;
169 char emptyVersion[1];
170 byte emptyBytes[1];
171 byte missingEditedText[1];
172 byte argsCount; // no of arguments found in msg
173 byte argsIdx;
174 byte parserState;
175 byte args[pfodMaxMsgLen + 1]; // allow for trailing null
176 byte *versionStart;
177 const byte *activeCmdStart;
178 byte *editedText;
179 byte encodingProcessed;
180 uint8_t seqNum; // 0 if not set else last char before leading {
181 uint8_t lastSeqNum; // 0 if not set else last char before leading {
182 byte ignoreCmdSeqNum; // != 0 if should ignore them
183 uint8_t touchType;
184 int col;
185 int row;
186 byte *cmdStart;
187 bool refresh;
188 const char *version;
189 static const byte pfodBar = (byte)'|';
190 static const byte pfodTilda = (byte)'~';
191 static const byte pfodAccent = (byte)'`';
192 static const byte pfodArgStarted = 0xfe;
193 unsigned long parserTimeout; // 0 if not set;
194 unsigned long lastMsgTime; // 0 if not set;
195 };
196
197 #endif // pfodParser_h
198
pfodDrawing
Definition: pfodDrawing.h:47
pfodLinkedList
Definition: pfodLinkedList.h:50
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::slider
void slider(pfodAutoCmd c)
pfodParser::dwgCmdEquals
virtual bool dwgCmdEquals(const char dwgCmd)
pfodParser::isUp
virtual bool isUp()
pfodParser::getVersion
virtual const char * getVersion()
pfodParser::menuUpdate
void menuUpdate()
pfodParser::endOfMsg
void endOfMsg()
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:148
pfodParser::CLICK
static const int CLICK
Definition: pfodParser.h:150
pfodParser::addDwg
static void addDwg(pfodDrawing *dwgPtr)
pfodParser::setVersion
virtual void setVersion(const char *version)
pfodParser::getArgsCount
virtual byte getArgsCount()
pfodParser::DOWN
static const int DOWN
Definition: pfodParser.h:147
pfodParser::isDrag
virtual bool isDrag()
pfodParser::getTouchedY
virtual int getTouchedY()
pfodParser::parse
virtual byte parse(byte in)
pfodParser::button
void button(pfodAutoCmd c)
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:149
pfodParser::PRESS
static const int PRESS
Definition: pfodParser.h:151
pfodParser::getDwgCmd
virtual const byte * getDwgCmd()
pfodParser::listOfDrawings
static pfodLinkedList< pfodDrawing > & listOfDrawings()
pfodParser::getNextArg
virtual byte * getNextArg(byte *start)
pfodParser::menu
void menu()
pfodParser::getCmd
virtual byte * getCmd()
pfodParser::setIdleTimeout
virtual void setIdleTimeout(unsigned long timeout_in_secs)
pfodParser::parse
virtual byte parse()
pfodParser::onOffDisplay
void onOffDisplay(pfodAutoCmd c)
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:107
pfodParser::isClick
virtual bool isClick()
pfodParser::cmdEquals
virtual bool cmdEquals(const char *cmdStr)
pfodParser::getTouchedX
virtual int getTouchedX()
pfodParser::getTouchedRow
virtual int getTouchedRow()
pfodParser::DOWN_DRAG_UP
static const int DOWN_DRAG_UP
Definition: pfodParser.h:155
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:111
pfodParser::TOUCH
static const int TOUCH
Definition: pfodParser.h:146
pfodParser::sendVersion
virtual void sendVersion()
pfodParser::setDebugOut
virtual void setDebugOut(Print *out)
Definition: pfodParser.h:110
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:154
pfodParser::pfodWaitingForStart
static const byte pfodWaitingForStart
Definition: pfodParser.h:104
pfodParser::pfodMaxMsgLen
static const byte pfodMaxMsgLen
Definition: pfodParser.h:65
pfodParser::getFirstArg
virtual byte * getFirstArg()
pfodParser::pfodRefresh
static const byte pfodRefresh
Definition: pfodParser.h:106
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::getVersionRequested
virtual const char * getVersionRequested()
pfodParser::pfodMsgStarted
static const byte pfodMsgStarted
Definition: pfodParser.h:105
pfodParser::read
virtual int read()
pfodParser::TOUCH_DISABLED
static const int TOUCH_DISABLED
Definition: pfodParser.h:156
pfodParser::printDwgCmdReceived
void printDwgCmdReceived(Print *outPtr)
pfodParser::pfodMsgEnd
static const byte pfodMsgEnd
Definition: pfodParser.h:108
pfodParser::getTouchType
virtual uint8_t getTouchType()
pfodParser::init
void init()
pfodParser::label
void label(pfodAutoCmd c)
pfodParser::flush
virtual void flush()
pfodDelay.h
pfodDrawing.h
pfodDwgs.h
pfodLinkedList.h
pfodParserUtils.h
pfodStream.h