blob: 034135c7b98c9ad683b77f06b97e746a819e780a (
plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
package org.reprap.comms.snap;
import java.io.IOException;
public class SNAPPacket {
private final int offset_sync = 0;
private final int offset_hdb2 = 1;
private final int offset_hdb1 = 2;
private final int offset_dab = 3;
private final int offset_sab = 4;
private final int offset_payload = 5;
private final byte syncMarker = 0x54;
private final int maxSize = 64;
// Full raw packet contents including all headers
private byte [] buffer;
private int receiveLength = 0;
private boolean complete = false;
public SNAPPacket() {
buffer = new byte[maxSize];
}
public SNAPPacket(SNAPAddress srcAddress, SNAPAddress destAddress, byte [] payload) {
buffer = new byte[payload.length + offset_payload + 1];
buffer[offset_sync] = syncMarker;
buffer[offset_hdb2] = 0x51;
buffer[offset_hdb1] = 0x30;
buffer[offset_dab] = (byte)destAddress.getAddress();
buffer[offset_sab] = (byte)srcAddress.getAddress();
setLength(payload.length);
for(int i = 0; i < payload.length; i++)
buffer[i + offset_payload] = payload[i];
generateChecksum();
complete = true;
}
private void generateChecksum() {
int length = getLength() + offset_payload;
SNAPChecksum crc = new SNAPChecksum();
for(int i = 1; i < length; i++)
crc.addData(buffer[i]);
buffer[length] = crc.getResult();
}
public byte getPacketType() {
return buffer[0]; // TODO fix offset
}
public byte [] getPayload() {
int length = getLength();
byte [] payload = new byte[length];
for(int i = 0; i < length; i++)
payload[i] = buffer[i + offset_payload];
return payload;
}
public byte [] getRawData() {
return buffer;
}
/**
*
* @param data
* @return true is the packet is now complete, otherwise false
* @throws IOException
*/
public boolean receiveByte(byte data) throws IOException {
if (complete)
throw new IOException("Received data beyond end of packet");
if (receiveLength >= maxSize)
throw new IOException("Received too much data");
buffer[receiveLength++] = data;
if (receiveLength > 4) {
int expectedLength = getLength() + offset_payload + 1;
if (receiveLength >= expectedLength)
return true;
}
return false;
}
public boolean validate() {
if (receiveLength < offset_payload)
return false;
int expectedLength = getLength() + offset_payload + 1;
if (receiveLength != expectedLength)
return false;
SNAPChecksum crc = new SNAPChecksum();
for(int i = offset_hdb2; i < receiveLength - 1; i++)
crc.addData(buffer[i]);
byte expectedCRC = buffer[receiveLength - 1];
return crc.getResult() == expectedCRC;
}
public SNAPAddress getSourceAddress() {
return new SNAPAddress(buffer[offset_sab]);
}
public SNAPAddress getDestinationAddress() {
return new SNAPAddress(buffer[offset_dab]);
}
private void setLength(int length) {
buffer[offset_hdb1] = (byte)((buffer[offset_hdb1] & 0xf0) |
(length > 7 ? 8 : length));
}
public int getLength() {
int l = buffer[offset_hdb1] & 0x0f;
if ((l & 8) != 0)
return 8 << (l & 7);
return l;
}
public SNAPPacket generateNAK() {
SNAPPacket resp = new SNAPPacket(getDestinationAddress(), getSourceAddress(), new byte [] {});
resp.buffer[offset_hdb2] = (byte)((resp.buffer[offset_hdb2] & 0xfc) | 3);
resp.generateChecksum();
return resp;
}
public SNAPPacket generateACK() {
SNAPPacket resp = new SNAPPacket(getDestinationAddress(), getSourceAddress(), new byte [] {});
resp.buffer[offset_hdb2] = (byte)((resp.buffer[offset_hdb2] & 0xfc) | 2);
resp.generateChecksum();
return resp;
}
/**
* @return true if the packet represents an ACK
*/
public boolean isAck() {
return ((buffer[offset_hdb2] & 3) == 2);
}
/**
* @return true if the packet represents a NAK
*/
public boolean isNak() {
return ((buffer[offset_hdb2] & 3) == 3);
}
}
|