pfodParser  3.61.0
The pfodParser library is handles commands sent from the Android pfodApp, pfodApp supports WiFi, BLE, Bluetooth and SMS connections
pfodSMS_SIM5320.h
Go to the documentation of this file.
1 #ifndef pfodSMS_SIM5320_h
2 #define pfodSMS_SIM5320_h
3 
4 /*
5  (c)2014-2018 Forward Computing and Control Pty. Ltd.
6  NSW Australia, www.forward.com.au
7  This code is not warranted to be fit for any purpose. You may only use it at your own risk.
8  This code may be freely used for both private and commercial use
9  Provide this copyright is maintained.
10 */
11 /*
12  This library uses about 22K of ROM (program space) and 3234 bytes of RAM
13  and so is NOT suitable for use on an UNO
14  Use a Mega2560 or similar with more then 4K of RAM
15 */
16 
17 #include "Arduino.h"
18 
19 #include "pfodStream.h"
20 #include "pfod_Base.h"
21 #include "pfod_rawOutput.h"
22 
23 #define SMS_MAX_SIZE 160
24 #define SMS_RX_BUFFER_SIZE 267
25 // max msg size after decode 256 + + 8(hash) + 2(extra) +null
26 // max pfod msg size
27 #define SMS_TX_MSG_SIZE 1026
28 // 1024 + 2
29 #define SMS_TX_BUFFER_SIZE (SMS_TX_MSG_SIZE+8+2)
30 // add a little 1026 + 8(hash) + 2(extra) + null
31 
32 class pfodSMS_SIM5320: public pfod_Base {
33 
34  public:
36  void init(Stream *_gprs, int resetPin = -1, int powerOnOffPin = -1, int powerStatusPin = -1); // use -1 if not used
37  void setDebugStream(Stream* out);
38  unsigned long getDefaultTimeOut();
39  Print* getRawDataOutput();
40  int available(void);
41  int peek(void);
42  int read(void);
43  void flush(void);
44  size_t write(uint8_t);
45  size_t writeRawData(uint8_t c);
46  Stream* getPfodAppStream(); // use pfodSecurity.getPfodAppStream() instead get the command response stream we are reading from and writing to
48 
49 
50  private:
51  int powerResetPin;
52  int powerStatusPin;
53  int resetPin;
54  const static int MAX_PHNO_LEN = 20;
55  const static int MAX_PHNO_CHECK = 6; // from end
56  struct connection {
57  int connectionNo; // = 0; is valid
58  int msgNo_rx; // = 0; // first expected
59  int msgNo_tx; // = 0; // next msgNo for next SMS being sent
60  int msgNo_tx_newMsg; // = 0; next msgNo for start of new response
61  // the msgNo_tx is set to this on a new msg
62  int msgNo_tx_resend; // = 0; // starting msgNo for last response
63  // the msgNo_tx is set to this on resend
64  boolean timedOut;
65  //knownConnection conPtr*;
66  char returnPhNo[MAX_PHNO_LEN + 1]; // plus null idx runs from 0 to MAX_PHNO_LEN
67  };
68 
69  struct knownConnection {
70  int connectionNo; // = 0; is valid
71  char returnPhNo[MAX_PHNO_LEN + 1]; // plus null idx runs from 0 to MAX_PHNO_LEN
72  };
73 
74  static const int MAX_KNOWN_CONNECTION = 10;
75  struct knownConnection knownConnectionsArray[MAX_KNOWN_CONNECTION]; // this allocate space for structs as well
76  struct knownConnection* knownArrayPtrs[MAX_KNOWN_CONNECTION]; // allocate space for ptrs
77 
78  void init();
79  void initPowerUp();
80  int findEmptyKnownConnection();
81  void updateMsgNoForStartNewCommand();
82  void updateMsgNoForStartResend();
83 
84  void stopResponseSends();
85  void stopAllSends();
86 
87  void clearKnownConnection(knownConnection * kCon);
88  void clearKnownConnection(int idx);
89  void printKnownConnections();
90  void printKnownConnection(knownConnection * kCon);
91  void swapKnownConnections();
92  void setTopKnownConnectionFromCurrent();
93  void moveKnownConnectionToTop(int idx);
94  void updateKnownConnectionNo(connection * con); // always succeeds
95  void updateKnownConnectionNoAndMoveToTop(connection * con); // always succeeds
96  void setNextExpectedConnectionMsgNo(connection * con, boolean foundEndParens);
97 
98  typedef enum decode_sms_return_t {
99  IGNORE, // invalid msg, wrong msgNo etc
100  RESEND_LAST_RESPONSE, // ignore msg and resend last response
101  CLOSE_CONNECTION, // close current connection no change in connectionNo
102  START_NEW_COMMAND, // start a new command no new connection
103  PROCESS_NEXT_PART_OF_COMMAND, // process continuation SMS of a command for connection expectedMsgNo has been updated
104  CLOSE_CONNECTION_AND_UPDATE_CONNECTION, // close current and update for new connectionNo
105  NEW_CONNECTION_SAME_PHONE_NO, // new connect but with same phoneNo
106  IGNORE_CONNECTION, // ignore connection attempt as current connection not timed out but let pfodApp retry
107  START_NEW_CONNECTION, // start a new connection from a different phone no
108  START_NEW_CONNECTION_AND_CLOSE // start new connection but msg is {!} so no reply but send alert to current and process msg
109  } decode_sms_return_t;
110 
111  decode_sms_return_t decodeSMS(connection * current, connection * next, const char* smsChars, byte * decodedSMS,
112  size_t* currentDecodedLen);
113 
114  bool havePowerResetPins;
115  bool haveResetPin;
116  void clearParser();
117  void clearResponseSet();
118  boolean decodeSMScmd(const char*smsChars, byte * decodedSMS, size_t* currentDecodedLen);
119  int checkSMSmsgForValidNewConnection(int connectionNo, int msgNo, const char* decodedSMS, boolean foundCmdClose);
120  size_t encodeSMS(int connectionNo, int msgNo, const byte * msgBytes, size_t msgBytesIdx, char* encodedSMS);
121  //boolean isResendStoredSMS();
122 
123  const char* findStr(const char* msg, const __FlashStringHelper * ifsh);
124  const char *findStr(const char* str, const char* target);
125 
126  const static size_t MAX_BYTES_TO_CONVERT = 117;
127 
128  const static int MAX_MSG_NO_LEN = 3;
129  const static int MAX_MSG_LEN = 256; // max msg len then null
130  char gprsMsg[MAX_MSG_LEN + 1]; // plus null idx runs from 0 to MAX_MSG_LEN
131  int gprsMsgIdx; // = 0;
132  unsigned char gprsLastChar; // = '\0';
133  // keep track of the next 10 incoming SMS sim card msg numbers are reported by +CMTI:
134  const static int MAX_INCOMING_MSG_NOS = 10; // = 10+1 for head != tail
135 
136  uint8_t incomingMsgNos_head; // from 0 to MAX_INCOMING_MSG_NOS;
137  uint8_t incomingMsgNos_tail; // from 0 to MAX_INCOMING_MSG_NOS;
138  // add to head take from tail
139  // stop adding when head+1%MAX_INCOMING_MSG_NOS+1 == tail
140  // stop taking when tail == head
141  // start tail==head == 0;
142  // take (tail == head) so empty
143  // add test (head+1%MAX_INCOMING_MSG_NOS != tail) add to [head] and head++
144  // take tail!= head take[tail++] now tail == head
145  // add 3 [0],[1]
146  char incomingMsgNos[MAX_INCOMING_MSG_NOS + 1][MAX_MSG_NO_LEN + 1];
147  void requestIncomingSmsMsg(const char *msgNo);
148 
149  size_t rxBufferLen; // runs from 0 to SMS_RX_BUFFER_SIZE
150  size_t txBufferLen; // runs from 0 to SMS_TX_BUFFER_SIZE
151  size_t rxBufferIdx; // runs from 0 to rxBufferLen
152  size_t txBufferIdx; // runs from 0 to txBufferLen
153  byte rxBuffer[SMS_RX_BUFFER_SIZE + 1]; // allow for terminating null
154  byte txBuffer[SMS_TX_BUFFER_SIZE + 1]; // allow for terminating null
155  char responseSMSbuffer[SMS_MAX_SIZE + 1]; // only 160 + 1
156  static const int SMS_RAWDATA_TX_BUFFER_SIZE = MAX_BYTES_TO_CONVERT - 1; //117
157  byte rawdataTxBuffer[SMS_RAWDATA_TX_BUFFER_SIZE + 1]; // allow for terminating null and force null at end
158  size_t rawdataTxBufferLen; // runs from 0 to SMS_RAWDATA_TX_BUFFER_SIZE
159  size_t rawdataTxBufferIdx; // runs from 0 to rawdataTxBufferLen
160  char rawdataSMSbuffer[SMS_MAX_SIZE + 1]; // only 160 + 1
161  void flushRawData();
162  void clearRawdataTxBuffer();
163  boolean isSendingResponse();
164  boolean haveRequestedIncomingMsg;
165  void requestNextIncomingMsg();
166 
167  boolean expectingMessageLines; // note msg from pfodApp may have embedded new line
168  boolean sendingRawdata; // true if processing and sending SMS response to command
169  boolean sendingResponse; // true if processing and sending SMS response to command
170  boolean sendingResend; // true if processing and sending SMS resend of previous response to command
171  boolean resendLastResponse; // true if should start resend of last response
172  // if sendingResponse then canSendRawData() return false and any write is just dropped
173  boolean responseSet; // true if tx_buffer holds respons upto } and no new cmd recieved
174  boolean deleteReadSmsMsgs; // true if should delete read msgs
175  boolean foundOK; // true when OK was last gprs msg
176 
177  struct connection connection_A;
178  struct connection connection_B;
179  struct connection *currentConnection;
180  struct connection *nextConnection;
181  void pollSMSboard();
182  void sendSMSrawData();
183 
184  const char* collectGprsMsgs();
185  void clearGprsLine();
186  void processGprsLine(const char* line);
187  // maxLen includes null at end
188  // void getProgStr(const __FlashStringHelper *ifsh, char*str, int maxLen);
189  int decodeInt(const char*smsChars, size_t idx);
190  size_t encodeInt(int number, char*rtn, size_t idx);
191  size_t convertThree8Bytes(long three8Bytes, char*rtn, size_t idx);
192  byte decodeSMSChar(byte encodedChar);
193  byte encodeSMSChar(byte encodedChar);
194  //Stream *console;
195  Stream *gprs; // sms shield serial connection
196  const static byte invalidSMSCharReturn = 0xff; // == 255,
197  byte bytesToConvert[MAX_BYTES_TO_CONVERT + 1]; // max bytes to convert + null
198 
199  void closeConnection(connection * con);
200  void clearConnection(connection * con);
201 
202  boolean newConnectionNumberGreater(int current, int next);
203  boolean validSMSmsgLen(const char* smsChars);
204  void printIgnoreOutOfSequence();
205  //unsigned long timedOutTimer;
206  //unsigned long start_mS;
207  int findMatchingKnownPhoneNo(const char *phNo);
208  boolean phoneNoMatchesCurrent(const char*newPhNo);
209  boolean phoneNoMatches(const char*oldPhNo, const char*newPhNo);
210  boolean phoneNoHas6Digits(const char *phNo);
211  void checkGPRSpoweredUp();
212  int powerCycleGPRS();
213  boolean setupSMS();
214  boolean gprsReady;
215  boolean powerUpGPRS();
216  void clearTxBuffer();
217 
218  void printConnection(connection * con);
219  void swapConnections();
220  void setPhoneNo(connection * con, const char* phNo);
221 
222  void printCurrentConnection();
223  void printNextConnection();
224  // void startTimer();
225 
226  void startSMS(const char* phoneNo);
227  void sendNextSMS(); // send next part of txbuffer
228  void clearReadSmsMsgs();
229  void clearAllSmsMsgs();
230  bool clearAllSmsMsgsOnInit();
231  void emptyIncomingMsgNo();
232 
233  void gprsPrint(const char* str);
234  void gprsPrint(char c);
235  void gprsPrint(const __FlashStringHelper * ifsh);
236 
237  boolean haveIncomingMsgNo();
238  boolean addIncomingMsgNo(const char* msgNo);
239  const char* getIncomingMsgNo();
240  void deleteIncomingMsg(const char *newMsgNo);
241 
242  void turnEchoOn();
243  void turnEchoOff();
244 
245  pfod_rawOutput raw_io;
246  pfod_rawOutput* raw_io_ptr;
247 
248 
249  private:
250  Stream* debugOut;
251 
252 };
253 
254 #endif // pfodSMS_SIM5320_h
255 
pfod_Base for Arduino Base class for all pfod_Base_xxxx classes The subclasses pfod_Base_xxx must ove...
Definition: pfod_Base.h:18
Print * getRawDataOutput()
size_t write(uint8_t)
int peek(void)
Stream * getPfodAppStream()
void init(Stream *_gprs, int resetPin=-1, int powerOnOffPin=-1, int powerStatusPin=-1)
unsigned long getDefaultTimeOut()
size_t writeRawData(uint8_t c)
int available(void)
int read(void)
void setDebugStream(Stream *out)
void _closeCurrentConnection()
void flush(void)
#define SMS_TX_BUFFER_SIZE
#define SMS_MAX_SIZE
#define SMS_RX_BUFFER_SIZE