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:
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
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 
virtual void setCmd(byte cmd)
virtual Stream * getPfodAppStream()
virtual void connect(Stream *ioPtr)
virtual void setDebugStream(Print *debugOut)
virtual bool isRefresh()
virtual bool isDown()
virtual void closeConnection()
void slider(pfodAutoCmd c)
virtual bool dwgCmdEquals(const char dwgCmd)
virtual bool isUp()
virtual const char * getVersion()
void menuUpdate()
void endOfMsg()
virtual size_t write(const uint8_t *buffer, size_t size)
virtual bool cmdEquals(pfodAutoCmd &a_Cmd)
virtual bool isPress()
virtual bool isTouch()
static const int DRAG
Definition: pfodParser.h:148
static const int CLICK
Definition: pfodParser.h:150
static void addDwg(pfodDrawing *dwgPtr)
virtual void setVersion(const char *version)
virtual byte getArgsCount()
static const int DOWN
Definition: pfodParser.h:147
virtual bool isDrag()
virtual int getTouchedY()
virtual byte parse(byte in)
void button(pfodAutoCmd c)
virtual byte getParserState()
pfodWaitingForStart if outside msg pfodMsgStarted if just seen opening { pfodInMsg in msg after { pfo...
static const int UP
Definition: pfodParser.h:149
static const int PRESS
Definition: pfodParser.h:151
virtual const byte * getDwgCmd()
static pfodLinkedList< pfodDrawing > & listOfDrawings()
virtual byte * getNextArg(byte *start)
void menu()
virtual byte * getCmd()
virtual void setIdleTimeout(unsigned long timeout_in_secs)
virtual byte parse()
void onOffDisplay(pfodAutoCmd c)
virtual bool dwgCmdEquals(const char *dwgCmdStr)
virtual const byte * getEditedText()
virtual int peek()
virtual size_t write(uint8_t c)
static const byte pfodInMsg
Definition: pfodParser.h:107
virtual bool isClick()
virtual bool cmdEquals(const char *cmdStr)
virtual int getTouchedX()
virtual int getTouchedRow()
static const int DOWN_DRAG_UP
Definition: pfodParser.h:155
virtual void sendRefreshAndVersion(unsigned long refresh_mS)
pfodParser(const char *version)
virtual void setDebug(Print *out)
Definition: pfodParser.h:111
static const int TOUCH
Definition: pfodParser.h:146
virtual void sendVersion()
virtual void setDebugOut(Print *out)
Definition: pfodParser.h:110
virtual void ignoreSeqNum()
virtual bool cmdEquals(const char cmdChar)
static const int DOWN_UP
Definition: pfodParser.h:154
static const byte pfodWaitingForStart
Definition: pfodParser.h:104
static const byte pfodMaxMsgLen
Definition: pfodParser.h:65
virtual byte * getFirstArg()
static const byte pfodRefresh
Definition: pfodParser.h:106
virtual byte * parseLong(byte *idxPtr, long *result)
virtual bool dwgCmdEquals(pfodAutoCmd &a_Cmd)
virtual int available()
virtual byte parseDwgCmd()
virtual int getTouchedCol()
virtual const char * getVersionRequested()
static const byte pfodMsgStarted
Definition: pfodParser.h:105
virtual int read()
static const int TOUCH_DISABLED
Definition: pfodParser.h:156
void printDwgCmdReceived(Print *outPtr)
static const byte pfodMsgEnd
Definition: pfodParser.h:108
virtual uint8_t getTouchType()
void init()
void label(pfodAutoCmd c)
virtual void flush()