-
Notifications
You must be signed in to change notification settings - Fork 8
/
binarydata.h
109 lines (105 loc) · 2.78 KB
/
binarydata.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef BINARYDATA_H
#define BINARYDATA_H
#include <stdint.h>
#include "network.h"
class BinaryData {
public:
BinaryData(uint8_t len);
~BinaryData();
typedef enum STATUS {
OK,
INDEXOUTOFBOUNDS
} AccessStatus;
struct ByteResult {
AccessStatus state;
unsigned char data;
};
struct BoolResult {
AccessStatus state;
bool data;
};
struct LongResult {
AccessStatus state;
unsigned long int data;
};
AccessStatus toggleFlag(uint8_t index, unsigned char mask);
AccessStatus writeFlag(uint8_t index, unsigned char mask, bool value);
AccessStatus writeByte(uint8_t index, unsigned char value);
AccessStatus writeData(uint8_t index, BinaryData* data, uint8_t from, uint8_t to);
BoolResult readFlag(uint8_t index, unsigned char mask, unsigned char comparator);
ByteResult readByte(uint8_t index);
LongResult readLong(uint8_t index);
char* getData();
uint8_t getSize();
private:
uint8_t _len = 0;
char* _payload;
AccessStatus checkIndex(uint8_t index);
};
class BinaryBuffer {
public:
BinaryBuffer(uint8_t len) {
_data = new BinaryData(len);
}
~BinaryBuffer() {
delete _data;
}
int available() {
return _data->getSize() - _position;
}
BinaryData::ByteResult peek() {
return _data->readByte(_position);
}
BinaryData::ByteResult readByte() {
BinaryData::ByteResult result = _data->readByte(_position);
if (result.state == BinaryData::OK) {
_position++;
}
return result;
}
BinaryData::LongResult readLong() {
BinaryData::LongResult result = _data->readLong(_position);
if (result.state == BinaryData::OK) {
_position++;
}
return result;
}
BinaryData::AccessStatus write(unsigned char value) {
BinaryData::AccessStatus result = _data->writeByte(_position, value);
if (result == BinaryData::OK) {
_position++;
}
return result;
}
BinaryData::AccessStatus write(BinaryBuffer *otherBuffer) {
BinaryData::AccessStatus result = _data->writeData(_position, otherBuffer->_data, otherBuffer->_position, otherBuffer->_position + _data->getSize());
if (result == BinaryData::OK) {
_position+= _data->getSize() - 1;
}
return result;
}
boolean goTo(int index) {
if (index < _data->getSize() - 1) {
_position = index;
return true;
}
return false;
}
uint8_t getPosition() {
return _position;
}
boolean next() {
if (_position < _data->getSize() - 1) {
_position++;
return true;
}
return false;
}
uint8_t getSize() {
return _data->getSize();
}
private:
BinaryData* _data;
uint8_t _position = 0;
};
#endif