-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTLVReceiver.cpp
More file actions
60 lines (54 loc) · 1.39 KB
/
TLVReceiver.cpp
File metadata and controls
60 lines (54 loc) · 1.39 KB
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
#include "TLVReceiver.h"
TLVReceiver::TLVReceiver(): currentByte(0)
, currentSize(0)
, currentType(0)
, currentSection(E_PacketSections::E_Type)
{
}
void TLVReceiver::addByte(byte b)
{
switch(currentSection)
{
case E_PacketSections::E_Type:
currentType = b;
currentSection = E_PacketSections::E_Size;
break;
case E_PacketSections::E_Size:
currentSize = b;
if (currentSize == 0)
{
finishedPacket();
currentSection = E_PacketSections::E_Type;
}
else
{
currentSection = E_PacketSections::E_Value;
currentByte = 0;
}
break;
case E_PacketSections::E_Value:
buffer[currentByte] = b;
++currentByte;
if (currentSize == currentByte)
{
finishedPacket();
currentSection = E_PacketSections::E_Type;
}
break;
}
}
void TLVReceiver::connectionReset()
{
currentByte = 0;
currentSize = 0;
currentType = 0;
currentSection = E_PacketSections::E_Type;
}
void TLVReceiver::finishedPacket()
{
ptr(currentType, currentSize, buffer);
}
void TLVReceiver::setCallback(CallbackFunctionPtr p)
{
ptr = p;
}