Packet.h 1.73 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
/jdi/ committed
15
16
#include "Types/bytes.h"
#include "Types/datasets.h"
/jdi/'s avatar
basics  
/jdi/ committed
17
18
19
20

class Packet {
public:
	enum OpCode {
/jdi/'s avatar
/jdi/ committed
21
		DISCOVERY, GET, SET, READ4,RECEIVE, READ5
/jdi/'s avatar
basics  
/jdi/ committed
22
23
24
25
	};
	Packet(OpCode);
	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
	macAddr 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(macAddr);
	void setSwitchMac(macAddr);
/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
	macAddr switchMac {{ 0, 0, 0, 0, 0, 0 }};
	macAddr hostMac {{ 0, 0, 0, 0, 0, 0 }};
	macAddr 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
	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);
/jdi/'s avatar
/jdi/ committed
67
68
	void push(bytes&, int&, ipAddr);
	void push(bytes&, int&, macAddr);
/jdi/'s avatar
basics  
/jdi/ committed
69
	void push(bytes&, int&, dataset);
/jdi/'s avatar
/jdi/ committed
70
71
72
73
74

	void pull(bytes&, int&, short&);
	void pull(bytes&, int&, int&);
	void pull(bytes&, int&, byte&);
	void pull(bytes&, int&, bytes&, unsigned);
/jdi/'s avatar
/jdi/ committed
75
	void pull(bytes&, int&, ipAddr&);
/jdi/'s avatar
/jdi/ committed
76
	void pull(bytes&, int&, macAddr&);
/jdi/'s avatar
/jdi/ committed
77
	void pull(bytes&, int&, dataset&);
/jdi/'s avatar
basics  
/jdi/ committed
78
79
80
};

#endif /* PACKET_H_ */