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:
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 
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()
virtual bool dwgCmdEquals(const char dwgCmd)
virtual bool isUp()
virtual const char * getVersion()
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:136
static const int CLICK
Definition: pfodParser.h:138
virtual void setVersion(const char *version)
virtual byte getArgsCount()
static const int DOWN
Definition: pfodParser.h:135
virtual bool isDrag()
virtual int getTouchedY()
virtual byte parse(byte in)
virtual byte getParserState()
pfodWaitingForStart if outside msg pfodMsgStarted if just seen opening { pfodInMsg in msg after { pfo...
static const int UP
Definition: pfodParser.h:137
static const int PRESS
Definition: pfodParser.h:139
virtual const byte * getDwgCmd()
virtual byte * getNextArg(byte *start)
virtual byte * getCmd()
virtual byte parse()
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:97
virtual bool isClick()
virtual bool cmdEquals(const char *cmdStr)
virtual int getTouchedX()
virtual int getTouchedRow()
virtual void sendRefreshAndVersion(unsigned long refresh_mS)
pfodParser(const char *version)
virtual void setDebug(Print *out)
Definition: pfodParser.h:101
static const int TOUCH
Definition: pfodParser.h:134
virtual void sendVersion()
virtual void setDebugOut(Print *out)
Definition: pfodParser.h:100
virtual void ignoreSeqNum()
virtual bool cmdEquals(const char cmdChar)
static const int DOWN_UP
Definition: pfodParser.h:142
static const byte pfodWaitingForStart
Definition: pfodParser.h:94
static const byte pfodMaxMsgLen
Definition: pfodParser.h:63
virtual byte * getFirstArg()
static const byte pfodRefresh
Definition: pfodParser.h:96
virtual byte * parseLong(byte *idxPtr, long *result)
virtual bool dwgCmdEquals(pfodAutoCmd &a_Cmd)
virtual int available()
virtual byte parseDwgCmd()
virtual int getTouchedCol()
virtual void addDwg(pfodDrawing *dwgPtr)
virtual const char * getVersionRequested()
static const byte pfodMsgStarted
Definition: pfodParser.h:95
virtual void setIdleTimeout(unsigned long timeout)
virtual int read()
static const int TOUCH_DISABLED
Definition: pfodParser.h:143
static const byte pfodMsgEnd
Definition: pfodParser.h:98
virtual uint8_t getTouchType()
void init()
virtual void flush()