pfodParser  3.61.0
The pfodParser library is handles commands sent from the Android pfodApp, pfodApp supports WiFi, BLE, Bluetooth and SMS connections
pfodRadio.h
Go to the documentation of this file.
1 #ifndef pfodRadio_h
2 #define pfodRadio_h
7 /*
8  (c)2014-2018 Forward Computing and Control Pty. Ltd.
9  NSW Australia, www.forward.com.au
10  This code is not warranted to be fit for any purpose. You may only use it at your own risk.
11  This code may be freely used for both private and commercial use
12  Provide this copyright is maintained.
13 */
14 #include <Arduino.h>
15 #include "pfodStream.h"
16 #include "pfod_Base.h"
17 #include "pfodRadioMsg.h"
18 #include "pfodRingBuffer.h"
19 #include "pfodRadioDriver.h"
20 
22 #define RH_DEFAULT_TIMEOUT 500
23 
25 #define RH_DEFAULT_RETRIES 5
26 
35 
36 class pfodRadio: public pfod_Base {
37  public:
38 
39  pfodRadio(pfodRadioDriver *_driver, uint8_t _thisAddress);
40  void setDebugStream(Print* _debugOut);
42 
43  void connectTo(uint8_t _to); // seting up as client and connect
44 
45  void listen(); // setting up as server
46 
47  // max timeout is 32.7sec (32767mS)
48  void setAckTimeout(uint16_t _timeout_mS);
49 
50  void setNoOfRetries(uint8_t _noOfRetries);
51 
52  void setMaxWriteBufLen(size_t _maxLen); // NOTE: this setting is ignored for the moment
53  // max len of data written to this radio in one block.
54  // this data block is stored and then sent in smaller radio sized chunks
55  // defaults to 255 for Clients
56  // and 1023 for Server (pfodDevice)
57  // you can set a smaller value based on your menus and commands. If using pfodDwgs leave server setting at 1023.
58  // code adds 8 for security has and 2 for padding
59 
60  bool recvMsg(); //uint8_t* _buf, uint8_t* _len, uint8_t* _receivedFrom, uint8_t* _addressedTo, uint8_t* _incomingMsgSeqNo, uint8_t* _ackMsgSeqNo);
61  bool isNewMsg();
63  // if sendMsg with ack use last arg
64  // else ommit last arg to just send message without an ack included
65  // size_t sendMsg(uint8_t *_buf, uint8_t _len);
66  size_t sendMsg(); // send from txBuf is any data waiting
67 
69  uint16_t getLastRSSI();
70 
71  bool notInTxMode();
72  bool inTxMode();
74  void sendAck();
75  uint8_t getThisAddress();
76  bool isServer(); // true is running as server else false
77  bool init(); // calls driver init
78  // from Stream
79  int available();
80  int peek();
81  int read();
82  void flush();
83  size_t write(uint8_t b);
84  unsigned long getDefaultTimeOut();
85  void _closeCurrentConnection(); // called from pfodSecurity/pfodSecurityClient does NOT push 0xff into rx buffer after clearing buffers
86  size_t writeRawData(uint8_t c);
87  Print* getRawDataOutput();
88 
89  protected:
90  bool sendTo(uint8_t* _buf, uint8_t _len, uint8_t _address);
93  void sendMsg(pfodRadioMsg * radioMsg); // returns
94  void pollRadio();
97  unsigned long timeLastMsgSent;
102  uint16_t getRandomTimeout();
104  Print* debugOut;
107 
108 
109 
110  private:
111  void resetTargetAndSeqNos();
112  bool isPureAck();
113  void resetLinkConnectTimeout(); // set to (ackTimeout*1.5) * noOfRetries, timesout resends of msgSeqNo 0 ack
114 
115  pfodRingBuffer rawDataBuf;
116  pfodRingBuffer txBuf;
117  pfodRingBuffer rxBuf;
118  unsigned long lastWriteTime; // delay send for 50mS to let multiple writes accumulate.
119  static const unsigned long SEND_MSG_DELAY_TIME = 50; // ms
120  static const uint8_t sizeOfLong = (uint8_t) (sizeof(long) * 8); //bits per byte
121 
122  bool canAcceptNewConnections;
123  bool newConnection; // set true on accepting new connection msgSeqNo 0
124  uint8_t retryCount; // retry send for current msg waiting to be acked
125  uint8_t thisAddress; // address of this node
126  uint8_t targetAddress; // where to send the to the other connection,
127  bool isServerNode;
130  uint16_t ackTimeout; // in milliseconds
131  uint16_t randomTimeout; // == timeout + random()*timeout
132  uint8_t outGoingSeqNo; // last outgoing seq no (expecting ack for this one
133  uint8_t inComingSeqNo; // next expected incoming seqNo
134  size_t maxWriteBufLen;
135  const static size_t MAX_CLIENT_CMD_LEN = 255 + 8 + 2; // max cmd is 255 + 8 for hash + 2 padding (for nulls)
136  const static size_t MAX_SERVER_RESPONSE_LEN = 1023 + 8 + 2; // max response is 1023 + 8 for hash + 2 padding;
137  const static size_t BUFFER_SIZE_256 = MAX_CLIENT_CMD_LEN; // temp storage for incoming msgs
138  const static size_t BUFFER_SIZE_1024 = MAX_SERVER_RESPONSE_LEN; // temp storage for incoming msgs
139  uint8_t rawDataBuffer_1024[BUFFER_SIZE_1024]; // for now temp storage for outgoing msgs
140  uint8_t buffer_1024[BUFFER_SIZE_1024]; // for now temp storage for outgoing msgs
141  uint8_t buffer_256[BUFFER_SIZE_256]; // for now temp storage for incoming msgs
142  bool needToAckReceivedMsg;
143  uint8_t noOfRetries; // how many retrys to do before link broken
144  uint8_t retriesCount;
145  pfodRadioMsg* receivedMsgPtr;
146  pfodRadioMsg* lastMsgSentPtr;
147  pfodRadioDriver* driver;
148  uint16_t lastRSSI;
149  unsigned long linkConnectionTimeoutTimer;
150  unsigned long linkConnectionTimeout; // 0 if not running
151  unsigned long debug_mS;
152  unsigned long sendTimeStart_mS; // time to tx msg
153  bool sendTimerRunning; // true if timing send time
154 
155 };
156 
157 #endif
158 
pfod_Base for Arduino Base class for all pfod_Base_xxxx classes The subclasses pfod_Base_xxx must ove...
Definition: pfod_Base.h:18
pfodRadioDriver for Arduino This class abstracts the low level radio functions from pfodParser.
An ack consists of a message with:
Definition: pfodRadio.h:36
void clearTxRxBuffers()
bool isServer()
size_t write(uint8_t b)
bool inTxMode()
void setNoOfRetries(uint8_t _noOfRetries)
uint8_t getThisAddress()
bool notInTxMode()
bool isAckForLastMsgSent()
int available()
void checkIfNeedToConnect()
int read()
bool waitingForAckOfLastMsgSent
Definition: pfodRadio.h:96
pfodRadioMsg * getReceivedMsg()
pfodRadio(pfodRadioDriver *_driver, uint8_t _thisAddress)
void _closeCurrentConnection()
unsigned long timeLastMsgSent
Definition: pfodRadio.h:97
bool recvMsg()
void checkForAllowableNewConnection()
bool isConnectionClosed()
void resendLastMsg()
void pollRadio()
void setAckTimeout(uint16_t _timeout_mS)
Print * getRawDataOutput()
Print * debugOut
Definition: pfodRadio.h:104
int peek()
void listen()
bool isNewMsg()
uint16_t getLastRSSI()
void debugPfodRadioMsg(pfodRadioMsg *msg)
bool connectionClosed
Definition: pfodRadio.h:98
size_t sendMsg()
void setMaxWriteBufLen(size_t _maxLen)
void sendMsg(pfodRadioMsg *radioMsg)
void connectTo(uint8_t _to)
size_t writeRawData(uint8_t c)
void closeConnection()
void sendAck()
void setTimeLastMsgSent()
pfodRadioMsg receivedMsg
Definition: pfodRadio.h:91
void setDebugStream(Print *_debugOut)
uint16_t getRandomTimeout()
bool init()
pfodRadioMsg lastMsgSent
Definition: pfodRadio.h:92
void flush()
bool isNewConnectionRequest()
bool sendTo(uint8_t *_buf, uint8_t _len, uint8_t _address)
unsigned long getDefaultTimeOut()
pfodRadioMsg for Arduino Holds a radio msg
Definition: pfodRadioMsg.h:22
pfodRingBuffer for Arduino Implements a ring buffer implementation of an Arduino Stream upto 32K buff...