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);
41 void debugPfodRadioMsg(pfodRadioMsg* msg);
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();
62 bool isAckForLastMsgSent();
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
68 bool isConnectionClosed();
69 uint16_t getLastRSSI();
70
71 bool notInTxMode();
72 bool inTxMode();
73 pfodRadioMsg* getReceivedMsg();
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);
91 pfodRadioMsg receivedMsg;
92 pfodRadioMsg lastMsgSent;
93 void sendMsg(pfodRadioMsg * radioMsg); // returns
94 void pollRadio();
95 void clearTxRxBuffers();
96 bool waitingForAckOfLastMsgSent;
97 unsigned long timeLastMsgSent;
98 bool connectionClosed;
99 void checkIfNeedToConnect();
100 void resendLastMsg();
101 bool isNewConnectionRequest();
102 uint16_t getRandomTimeout();
103 void setTimeLastMsgSent();
104 Print* debugOut;
105 void checkForAllowableNewConnection();
106 void closeConnection();
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
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
pfodRadioDriver for Arduino This class abstracts the low level radio functions from pfodParser.
Definition: pfodRadioDriver.h:16
pfodRadio
An ack consists of a message with:
Definition: pfodRadio.h:36
pfodRadio::clearTxRxBuffers
void clearTxRxBuffers()
pfodRadio::isServer
bool isServer()
pfodRadio::write
size_t write(uint8_t b)
pfodRadio::inTxMode
bool inTxMode()
pfodRadio::setNoOfRetries
void setNoOfRetries(uint8_t _noOfRetries)
pfodRadio::getThisAddress
uint8_t getThisAddress()
pfodRadio::notInTxMode
bool notInTxMode()
pfodRadio::isAckForLastMsgSent
bool isAckForLastMsgSent()
pfodRadio::available
int available()
pfodRadio::checkIfNeedToConnect
void checkIfNeedToConnect()
pfodRadio::read
int read()
pfodRadio::waitingForAckOfLastMsgSent
bool waitingForAckOfLastMsgSent
Definition: pfodRadio.h:96
pfodRadio::getReceivedMsg
pfodRadioMsg * getReceivedMsg()
pfodRadio::pfodRadio
pfodRadio(pfodRadioDriver *_driver, uint8_t _thisAddress)
pfodRadio::_closeCurrentConnection
void _closeCurrentConnection()
pfodRadio::timeLastMsgSent
unsigned long timeLastMsgSent
Definition: pfodRadio.h:97
pfodRadio::recvMsg
bool recvMsg()
pfodRadio::checkForAllowableNewConnection
void checkForAllowableNewConnection()
pfodRadio::isConnectionClosed
bool isConnectionClosed()
pfodRadio::resendLastMsg
void resendLastMsg()
pfodRadio::pollRadio
void pollRadio()
pfodRadio::setAckTimeout
void setAckTimeout(uint16_t _timeout_mS)
pfodRadio::getRawDataOutput
Print * getRawDataOutput()
pfodRadio::debugOut
Print * debugOut
Definition: pfodRadio.h:104
pfodRadio::peek
int peek()
pfodRadio::listen
void listen()
pfodRadio::isNewMsg
bool isNewMsg()
pfodRadio::getLastRSSI
uint16_t getLastRSSI()
pfodRadio::debugPfodRadioMsg
void debugPfodRadioMsg(pfodRadioMsg *msg)
pfodRadio::connectionClosed
bool connectionClosed
Definition: pfodRadio.h:98
pfodRadio::sendMsg
size_t sendMsg()
pfodRadio::setMaxWriteBufLen
void setMaxWriteBufLen(size_t _maxLen)
pfodRadio::sendMsg
void sendMsg(pfodRadioMsg *radioMsg)
pfodRadio::connectTo
void connectTo(uint8_t _to)
pfodRadio::writeRawData
size_t writeRawData(uint8_t c)
pfodRadio::closeConnection
void closeConnection()
pfodRadio::sendAck
void sendAck()
pfodRadio::setTimeLastMsgSent
void setTimeLastMsgSent()
pfodRadio::receivedMsg
pfodRadioMsg receivedMsg
Definition: pfodRadio.h:91
pfodRadio::setDebugStream
void setDebugStream(Print *_debugOut)
pfodRadio::getRandomTimeout
uint16_t getRandomTimeout()
pfodRadio::init
bool init()
pfodRadio::lastMsgSent
pfodRadioMsg lastMsgSent
Definition: pfodRadio.h:92
pfodRadio::flush
void flush()
pfodRadio::isNewConnectionRequest
bool isNewConnectionRequest()
pfodRadio::sendTo
bool sendTo(uint8_t *_buf, uint8_t _len, uint8_t _address)
pfodRadio::getDefaultTimeOut
unsigned long getDefaultTimeOut()
pfodRadioMsg
pfodRadioMsg for Arduino Holds a radio msg
Definition: pfodRadioMsg.h:22
pfodRingBuffer
pfodRingBuffer for Arduino Implements a ring buffer implementation of an Arduino Stream upto 32K buff...
Definition: pfodRingBuffer.h:25
pfod_Base.h
pfodRadioDriver.h
pfodRadioMsg.h
pfodRingBuffer.h
pfodStream.h