Packet.h 1.58 KB
Newer Older
/jdi/'s avatar
basics  
/jdi/ committed
1
2
3
4
5
6
7
8
9
10
11
/*
 * Packet.h
 *
 *  Created on: 03.09.2015
 *      Author: jdi
 */

#ifndef PACKET_H_
#define PACKET_H_

#define HEADER_LEN 32
/jdi/'s avatar
/jdi/ committed
12
#define PACKET_END 0xFFFF0000
/jdi/'s avatar
basics  
/jdi/ committed
13

/jdi/'s avatar
/jdi/ committed
14
#include "Types.h"
/jdi/'s avatar
basics  
/jdi/ committed
15
16
17
18

class Packet {
public:
	enum OpCode {
/jdi/'s avatar
/jdi/ committed
19
		DISCOVERY, GET, SET, READ
/jdi/'s avatar
basics  
/jdi/ committed
20
21
	};
	Packet(OpCode);
/jdi/'s avatar
/jdi/ committed
22
23
	virtual ~Packet() {
	}
/jdi/'s avatar
basics  
/jdi/ committed
24
25
	void encode(bytes&);
	bytes getBytes();
/jdi/'s avatar
/jdi/ committed
26
	void parse(bytes);
/jdi/'s avatar
/jdi/ committed
27
28
	void printHeader();
	std::string opCodeToString();
/jdi/'s avatar
/jdi/ committed
29
	short getLength() const;
/jdi/'s avatar
basics  
/jdi/ committed
30
31
	int getCheckSum() const;
	short getSequenceId() const;
/jdi/'s avatar
/jdi/ committed
32
	const byteArray<6>& getSwitchMac() const;
/jdi/'s avatar
/jdi/ committed
33
34
	const bytes& getBody() const;
	const bytes& getHead() const;
/jdi/'s avatar
basics  
/jdi/ committed
35
36
	const datasets& getPayload() const;
	void setBody(bytes);
/jdi/'s avatar
/jdi/ committed
37
38
	void setHostMac(byteArray<6>);
	void setSwitchMac(byteArray<6>);
/jdi/'s avatar
basics  
/jdi/ committed
39
40
41
42
43
44
	void setCheckSum(int);
	void setSequenceId(short);
	void setPayload(const datasets& payload);

private:
	bytes head = bytes(32);
/jdi/'s avatar
/jdi/ committed
45
	bytes body = bytes(0);
/jdi/'s avatar
basics  
/jdi/ committed
46
47
48
49
	datasets payload;

	byte version = 1;
	byte opCode;
/jdi/'s avatar
/jdi/ committed
50
51
52
	byteArray<6> switchMac = { 0, 0, 0, 0, 0, 0 };
	byteArray<6> hostMac = { 0, 0, 0, 0, 0, 0 };
	byteArray<6> broadcastMac = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
/jdi/'s avatar
basics  
/jdi/ committed
53
54
55
	short sequenceId;
	short tokenId = 0;
	short fragmentOffset = 0;
/jdi/'s avatar
/jdi/ committed
56
	int errorCode = 0;
/jdi/'s avatar
basics  
/jdi/ committed
57
58
59
60
61
62
63
64
65
66
67
	int checkSum = 0;
	short flag = 0;

	void buildHead();
	void buildBody();

	void push(bytes&, int&, short);
	void push(bytes&, int&, int);
	void push(bytes&, int&, byte);
	void push(bytes&, int&, bytes);
	void push(bytes&, int&, dataset);
/jdi/'s avatar
/jdi/ committed
68
69
70
71
72
73

	void pull(bytes&, int&, short&);
	void pull(bytes&, int&, int&);
	void pull(bytes&, int&, byte&);
	void pull(bytes&, int&, bytes&, unsigned);
	void pull(bytes&, int&, dataset&);
/jdi/'s avatar
basics  
/jdi/ committed
74
75
76
};

#endif /* PACKET_H_ */