pfodParser  3.61.0
The pfodParser library is handles commands sent from the Android pfodApp, pfodApp supports WiFi, BLE, Bluetooth and SMS connections
pfodSecurity.h
Go to the documentation of this file.
1 #ifndef pfodSecurity_h
2 #define pfodSecurity_h
36 /*
37  (c)2014-2017 Forward Computing and Control Pty. Ltd.
38  NSW Australia, www.forward.com.au
39  This code is not warranted to be fit for any purpose. You may only use it at your own risk.
40  This code may be freely used for both private and commercial use
41  Provide this copyright is maintained.
42 */
43 
44 // comment out the next line to remove the pfodRawCmdParser if not needed, on a Mega2560 this saves about 296 bytes of RAM and some 580 bytes of code
45 #define PFOD_RAW_CMD_PARSER
46 
47 #include <Arduino.h>
48 #include "pfodStream.h"
49 #include "pfodParser.h"
50 #ifdef PFOD_RAW_CMD_PARSER
51 #include "pfodRawCmdParser.h"
52 #endif
53 #include "pfodMAC.h"
54 #include "pfod_Base.h"
55 #include "pfodDwgs.h"
56 #include "pfodControl.h"
57 #include "pfodDrawing.h"
58 #include "pfodLinkedList.h"
59 
60 // used to suppress warning
61 #define pfod_MAYBE_UNUSED(x) (void)(x)
62 
63 class pfodSecurity : public pfodParser { //Stream {
64  public:
66  pfodSecurity(const char *_version);
67 
68  // methods required for Print
69  size_t write(uint8_t);
70  size_t write(const uint8_t *buffer, size_t size);
71  void flush(); // may do nothing calls io->flush()
72 
73  // stream methods only useful to read the raw cmd
74  int read();
75  int peek();
76  int available();
77 #ifdef PFOD_RAW_CMD_PARSER
78  byte* getRawCmd();
79 #endif
80 
94  void setIdleTimeout(unsigned long timeout_in_seconds);
95  unsigned long getIdleTimeout();
96  bool isIdleTimeout(); // true if no new cmd within idle timeout
97 
98  void setDebugStream(Print* debugOut);
99 
117  void connect(Stream* io_arg);
118  void connect(Stream* io_arg, const __FlashStringHelper *hexKeyPgr, int eepromAddress = 0);
119  void connect(pfod_Base* _pfodBase);
120  void connect(pfod_Base* _pfodBase, const __FlashStringHelper *hexKeyPgr, int eepromAddress = 0);
121 
122  Stream* getPfodAppStream();
123 
124  void closeConnection(); // called when connection closed or DisconnectNow returned from parser
125  //static const byte DisconnectNow = '!'; // this is returned if pfodDevice should drop the connection
126 
127  byte parse(); // call this in loop() every loop, it will read bytes, if any, from the pfodAppStream and parse them
128  // returns 0 if message not complete, else returns the first char of a completed and verified message
129  void addDwg(pfodDrawing *dwgPtr); // add a pfodDrawing to the list of drawings to be automatically processed by parse()
130  void init();
131  // bool isEntry();
132  // bool isExit();
133 
134 
135  static int getBytesFromPassword(char *hexKey, int hexKeyLen, byte *keyBytes, int keyMaxLen);
136  static uint32_t decodePasswordBytes(byte* bytes, int idx, int bytesLen);
137  static uint8_t byte64ToByte(uint8_t b);
138 
139  private:
140  void connect(Stream* io_arg, Print* raw_io_arg, const __FlashStringHelper *hexKeyPgr, int eepromAddress = 0);
141  void connect(Stream* io_arg, Print* raw_io_arg);
142  void close_pfodSecurityConnection(); // called when {_} received while connection still active
143  size_t writeToPfodApp(uint8_t* idxPtr);
144  size_t writeToPfodApp(uint8_t b);
145  Stream *io;
146  pfodLinkedList<pfodDrawing> listOfDrawings;
147  Print *raw_io; // set to null on disconnect
148  Print *raw_io_connect_arg; // save for later resuse
149  Print *debugOut;
150  // pfodParser parser;
151  pfodMAC mac;
152  boolean parsing; // true when parsing, after disconnected() called and before returning 0xff, false between returning 0xff and disconnected being called
153  byte authorizing;
154  byte challenge[pfodMAC::challengeByteSize + 1]; // add one for hash identifier
155  unsigned long lastMillis; // holds the last read millis()
156  unsigned long connectionTimerStart; // holds the start millis()
157  uint32_t timeSinceLastConnection; // limited to 0x7fffffff
158  boolean failedHash; // true if any hash check failed last connection
159  unsigned long connectionTimer; // used for both authorizeation timeout and idletimeout
160  boolean noPassword;
161  void setAuthorizeState(int auth);
162  int msgHashCount;
163  static const byte Msg_Hash_Size = 8; // number of hex digits for msg hash
164  static const byte Msg_Hash_Size_Bytes = (Msg_Hash_Size >> 1); // number of hex bytes for msg hash i.e. 4
165  byte msgHashBytes[Msg_Hash_Size + 1]; // allow for null outgoing
166  uint32_t inMsgCount;
167  uint32_t outMsgCount;
168  byte outputParserState;
169  boolean initialization;
170  unsigned long idleTimeout;
171  void startIdleTimeoutTimer();
172  bool setIdleTimeoutCalled;
173  const __FlashStringHelper *hexKeyPgr;
174  int eepromAddress; // if < 0 then eeprom use disabled
175  pfod_Base* pfod_Base_set;
176  bool doFlush; // set to true for SMS /ESP-AT only, otherwise false
177  bool lastConnectionClosed; // set true by constructor and closeConnection(), set false by connect()
178  // this prevents call to closeConnection from connect provided last connection closed cleanly.
179  unsigned long timerDebug_ms;
180 #ifdef PFOD_RAW_CMD_PARSER
181  pfodRawCmdParser rawCmdParser;
182 #endif
183 };
184 
185 #endif // pfodSecurity_h
186 
pfod_Base for Arduino Base class for all pfod_Base_xxxx classes The subclasses pfod_Base_xxx must ove...
Definition: pfod_Base.h:18
static const uint8_t challengeByteSize
Definition: pfodMAC.h:91
void setIdleTimeout(unsigned long timeout_in_seconds)
Set the idle Timeout in sec i.e.
void addDwg(pfodDrawing *dwgPtr)
static uint8_t byte64ToByte(uint8_t b)
static uint32_t decodePasswordBytes(byte *bytes, int idx, int bytesLen)
size_t write(const uint8_t *buffer, size_t size)
static int getBytesFromPassword(char *hexKey, int hexKeyLen, byte *keyBytes, int keyMaxLen)
void setDebugStream(Print *debugOut)
void connect(pfod_Base *_pfodBase)
unsigned long getIdleTimeout()
bool isIdleTimeout()
byte * getRawCmd()
void connect(Stream *io_arg, const __FlashStringHelper *hexKeyPgr, int eepromAddress=0)
size_t write(uint8_t)
void connect(pfod_Base *_pfodBase, const __FlashStringHelper *hexKeyPgr, int eepromAddress=0)
Stream * getPfodAppStream()
pfodSecurity(const char *_version)
void closeConnection()
void connect(Stream *io_arg)
initialize the Security parser args io_arg the Stream pointer to read and write to for pfod messages