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

pfodSecurityClient.h
Go to the documentation of this file.
1 #ifndef pfodSecurityClient_h
2 #define pfodSecurityClient_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 #include <Arduino.h>
45 #include "pfodStream.h"
46 #include "pfodParser.h"
47 #include "pfodMACClient.h"
48 #include "pfodRingBuffer.h"
49 #include "pfod_Base.h"
50
51
52 // used to suppress warning
53 #define pfod_MAYBE_UNUSED(x) (void)(x)
54
55 class pfodSecurityClient : public Stream {
56 public:
57 pfodSecurityClient();
58 Stream *getRawDataStream();
59
60 // from Stream
61 // These methods only access command responses i.e. { ... } msgs
62 // to pickup any raw data use the Stream returned by getRawDataStream()
63 int available();
64 int peek();
65 int read();
66 void flush();
67 size_t write(uint8_t b);
68 size_t write(const uint8_t *buffer, size_t size);
69
70
71 void setResponseTimeout(unsigned long timeout_mS); // time between sending command and getting respose else close connection
72 void setKeepAliveInterval(uint16_t _interval_mS); // defaults to 0 i.e. keepAlives not sent unless this method is called
73
74 void setDebugStream(Print* debugOut);
75
93 void connect(Stream* io_arg);
94 void connect(Stream* io_arg, const __FlashStringHelper *hexKeyPgr);
95 void connect(pfod_Base* _pfodBase);
96 void connect(pfod_Base* _pfodBase, const __FlashStringHelper *hexKeyPgr);
97 Stream* getLinkStream(); // access to the underlying link stream
98
99 void closeConnection(); // called when connection closed or DisconnectNow returned from parser
100 //static const byte DisconnectNow = '!'; // this is returned if pfodDevice should drop the connection
101 void setIdleTimeout(unsigned long timeout_in_seconds); // defaults to 10sec if pfodBase not used, else set from pfodBase.getDefaultTimeOut()
102 bool isIdleTimeout();
103 void init();
104 protected:
105 void pollIO();
106 bool startConnection();
107 void closeStream();
108
109 private:
110 void startAuthorization();
111 void closeConnectionAddEOF();
112 void resetKeepAliveTimer();
113 const static size_t MAX_CLIENT_CMD_LEN = 256 + 8 + 2; // max cmd is 256 + 8 for hash + 2 padding (for nulls)
114 const static size_t MAX_SERVER_RESPONSE_LEN = 1024 + 8 + 2; // max response is 1024 + 8 for hash + 2 padding;
115 const static size_t TX_BUFFER_SIZE = MAX_CLIENT_CMD_LEN; // temp storage for incoming outgoing msgs
116 const static size_t RX_BUFFER_SIZE = MAX_SERVER_RESPONSE_LEN; // temp storage for incoming msgs
117 uint8_t tx_buffer[TX_BUFFER_SIZE]; // for now temp storage for outgoing msgs
118 uint8_t rx_buffer[RX_BUFFER_SIZE]; // for now temp storage for incoming msgs
119 uint8_t rx_temp_buffer[RX_BUFFER_SIZE]; // for now temp storage for incoming msgs
120 uint8_t rx_raw_data_buffer[RX_BUFFER_SIZE]; // 1K ringbuffer for raw data
121 pfodRingBuffer txBuf;
122 pfodRingBuffer rxBuf;
123 pfodRingBuffer rxTempBuf;
124 pfodRingBuffer rxRawDataBuf; // incoming raw data is split off here
125 void clearTxRxBuffers();
126 uint16_t keepAliveIntervalValue; // keepAliveIntervalValue 0 means no keepAlives sent
127 uint16_t keepAliveInterval;
128 unsigned long keepAliveTimer;
129 bool setKeepAliveIntervalCalled;
130
131 size_t writeToIO(uint8_t b);
132 void readFromIO();
133 Stream *io;
134 pfod_Base* pfod_Base_set; // null if not set
135 Print *debugOut;
136 pfodMACClient mac;
137 byte authorizing;
138 byte challenge[pfodMAC::challengeByteSize + 1]; // add one for hash identifier save this for calculating msg hash
139 unsigned long responseTimer;
140 unsigned long responseTimeout;
141 unsigned long responseTimeoutValue;
142 void restartResponseTimer();
143 void stopResponseTimer();
144 bool waitingForResponse; // true once } sent to block further commands until response received or link closed
145 boolean noPassword;
146 void setAuthorizeState(int auth);
147 int msgHashCount;
148 bool collectCheckHash(uint8_t b);
149 void handleChallengeResponse();
150 static const byte Msg_Hash_Size = pfodMAC::msgHashByteSize * 2; // number of hex digits for msg hash
151 static const byte Msg_Hash_Size_Bytes = pfodMAC::msgHashByteSize; // number of hex bytes for msg hash i.e. 4
152 byte msgHashBytes[Msg_Hash_Size + 1]; // allow for null outgoing
153 byte incomingHashBytes[Msg_Hash_Size + 1];
154 uint32_t inMsgCount;
155 uint32_t outMsgCount;
156 uint8_t outputParserState; // state of output parser
157 boolean initialization;
158 const __FlashStringHelper *hexKeyPgr;
159 bool doFlush; // set to true for SMS /ESP-AT only, otherwise false
160 bool connectionClosed; // set true by constructor and closeConnection(), set true after authorization succeeds
161 bool closingConnectionOut; // sending {!}
162 bool closingConnectionIn; // receiving {!}
163 bool foundMsgStart; // true when find { set false on next char
164 bool connecting; // set true is in the process of authorizing
165 // this prevents call to closeConnection from connect provided last connection close cleanly.
166 unsigned long timerDebug_ms;
167 unsigned long idleTimeout;
168 unsigned long idleConnectionTimerStart; // holds the start millis()
169 unsigned long idleConnectionTimer; // used for both authorizeation timeout and idletimeout
170 bool setIdleTimeoutCalled;
171 void startIdleConnectionTimer();
172 void stopIdleConnectionTimer();
173 };
174
175 #endif // pfodSecurityClient_h
176
pfod_Base
pfod_Base for Arduino Base class for all pfod_Base_xxxx classes The subclasses pfod_Base_xxx must ove...
Definition: pfod_Base.h:18
pfodMACClient
pfodMACClient for Arduino Updates power cycles, builds the Challenge, keeps track in message counts a...
Definition: pfodMACClient.h:21
pfodMAC::challengeByteSize
static const uint8_t challengeByteSize
Definition: pfodMAC.h:91
pfodMAC::msgHashByteSize
static const uint8_t msgHashByteSize
Definition: pfodMAC.h:89
pfodRingBuffer
pfodRingBuffer for Arduino Implements a ring buffer implementation of an Arduino Stream upto 32K buff...
Definition: pfodRingBuffer.h:25
pfodSecurityClient
Definition: pfodSecurityClient.h:55
pfodSecurityClient::pollIO
void pollIO()
pfodSecurityClient::write
size_t write(uint8_t b)
pfodSecurityClient::peek
int peek()
pfodSecurityClient::read
int read()
pfodSecurityClient::connect
void connect(pfod_Base *_pfodBase)
pfodSecurityClient::getLinkStream
Stream * getLinkStream()
pfodSecurityClient::flush
void flush()
pfodSecurityClient::connect
void connect(pfod_Base *_pfodBase, const __FlashStringHelper *hexKeyPgr)
pfodSecurityClient::isIdleTimeout
bool isIdleTimeout()
pfodSecurityClient::setIdleTimeout
void setIdleTimeout(unsigned long timeout_in_seconds)
pfodSecurityClient::available
int available()
pfodSecurityClient::connect
void connect(Stream *io_arg)
initialize the Security parser args io_arg the Stream pointer to read and write to for pfod messages
pfodSecurityClient::getRawDataStream
Stream * getRawDataStream()
pfodSecurityClient::connect
void connect(Stream *io_arg, const __FlashStringHelper *hexKeyPgr)
pfodSecurityClient::closeStream
void closeStream()
pfodSecurityClient::setResponseTimeout
void setResponseTimeout(unsigned long timeout_mS)
pfodSecurityClient::setKeepAliveInterval
void setKeepAliveInterval(uint16_t _interval_mS)
pfodSecurityClient::setDebugStream
void setDebugStream(Print *debugOut)
pfodSecurityClient::pfodSecurityClient
pfodSecurityClient()
pfodSecurityClient::write
size_t write(const uint8_t *buffer, size_t size)
pfodSecurityClient::init
void init()
pfodSecurityClient::startConnection
bool startConnection()
pfodSecurityClient::closeConnection
void closeConnection()
pfod_Base.h
pfodMACClient.h
pfodParser.h
pfodRingBuffer.h
pfodStream.h