summaryrefslogtreecommitdiff
path: root/tags/host/0.8.1/src/org/reprap/comms/snap/SNAPPacket.java
blob: 8ee7ed86f6990ddb47fd22ddc5d17ec119190d8c (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
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;
	
	/**
	 * 
	 */
	SNAPPacket() {
		buffer = new byte[maxSize];
	}
	
	/**
	 * @param srcAddress
	 * @param destAddress
	 * @param payload
	 */
	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();
	}
	
	/**
	 * @return the packet type
     */
	public byte getPacketType() {
		return buffer[0]; // TODO fix offset
	}
	
    /**
     * @return the payload
     */
    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;
	}
	
	/**
	 * @return the raw data of the packet
	 */
	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;
	}
	
	/**
	 * @return true if the packet passed validation
	 */
	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;
	}
	
	/**
	 * @return the source address of the packet
	 */
	public SNAPAddress getSourceAddress() {
		return new SNAPAddress(buffer[offset_sab]);
	}
	
	/**
	 * @return the destination address for the packet
	 */
	public SNAPAddress getDestinationAddress() {
		return new SNAPAddress(buffer[offset_dab]);
	}

	/**
	 * @param length
	 */
	private void setLength(int length) {
		buffer[offset_hdb1] = (byte)((buffer[offset_hdb1] & 0xf0) |
				(length > 7 ? 8 : length));
	}
	
	/**
	 * @return the lenght of the packet
	 */
	public int getLength() {
		int l = buffer[offset_hdb1] & 0x0f;
		if ((l & 8) != 0)
			return 8 << (l & 7);
		return l;
	}

	/**
	 * @return NAK packet
	 */
	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;
	}

	/**
	 * @return ACK packet
	 */
	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);
	}
}