-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathOLCB_Datagram_Handler.h
64 lines (50 loc) · 1.87 KB
/
OLCB_Datagram_Handler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef __OLCB_DATAGRAM_HANDLER__
#define __OLCB_DATAGRAM_HANDLER__
#include <string.h>
#include "Arduino.h"
#include "OLCB_Link.h"
#include "OLCB_Virtual_Node.h"
#include "OLCB_Datagram.h"
#define DATAGRAM_ERROR_OK 0xFFF0 //assume this won't get used as an error code. HACK!
#define DATAGRAM_ACK_TIMEOUT 5000
#define DATAGRAM_ERROR_ABORTED 0x1001
#define DATAGRAM_ERROR_ACK_TIMEOUT 0x1002
// An abstract class for handling the datagram protocol. Must be subclassed to be useful.
class OLCB_Datagram_Handler : public OLCB_Virtual_Node
{
public:
OLCB_Datagram_Handler() : _rxDatagramBufferFree(true), _txDatagramBufferFree(true), _sentTime(0), _txFlag(false), _loc(0), _ackReason(0xFFFF)
{
#if defined(__arm__)
_rxDatagramBuffer = new OLCB_Datagram;
_txDatagramBuffer = new OLCB_Datagram;
#elif defined(__AVR__)
_rxDatagramBuffer = (OLCB_Datagram*)malloc(sizeof(OLCB_Datagram));
_txDatagramBuffer = (OLCB_Datagram*)malloc(sizeof(OLCB_Datagram));
#endif
}
virtual bool handleMessage(OLCB_Buffer *frame);
virtual void update(void);
//This method, and maybe the next, must be fleshed out in a derived class to be of much use.
virtual uint16_t processDatagram(void) {return false;}
virtual void datagramResult(bool accepted, uint16_t errorcode) {return;}
bool sendDatagram(OLCB_Datagram *datagram);
bool isDatagramSent(void);
//clears any pending communications between this node and nodeid
void clearBuffer(OLCB_NodeID *nodeid);
protected:
void sendAck(OLCB_NodeID *dest);
void sendNak(OLCB_NodeID *dest, uint16_t reason);
//TODO condense all these bools into a bitfield
// bool _initialized;
bool _rxDatagramBufferFree;
bool _txDatagramBufferFree;
uint32_t _sentTime;
bool _txFlag;
uint8_t _loc;
OLCB_Datagram *_rxDatagramBuffer;
OLCB_Datagram *_txDatagramBuffer;
OLCB_NodeID _ackDest;
uint16_t _ackReason;
};
#endif