1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.diyefi.openlogviewer.decoder;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import org.diyefi.openlogviewer.genericlog.GenericLog;
29
30
31
32
33
34 public class FreeEMSByteLA implements Runnable {
35
36 private final short ESCAPE_BYTE = 0xBB;
37 private final short START_BYTE = 0xAA;
38 private final short STOP_BYTE = 0xCC;
39 private final short ESCAPED_ESCAPE_BYTE = 0x44;
40 private final short ESCAPED_START_BYTE = 0x55;
41 private final short ESCAPED_STOP_BYTE = 0x33;
42
43 private short[] wholePacket;
44 private File logFile;
45 private FileInputStream logStream;
46 private boolean startFound;
47 private GenericLog decodedLog;
48 private Thread t;
49
50 String[] headers = {
51 "PTIT", "T0", "T1", "T2", "T3", "T4", "T5", "T6", "T7"
52 };
53
54
55
56
57
58
59
60
61 public FreeEMSByteLA(String path) {
62
63 this(new File(path));
64
65
66 }
67
68
69
70
71
72 public FreeEMSByteLA(File f) {
73 logFile = f;
74 startFound = false;
75 wholePacket = new short[3000];
76 decodedLog = new GenericLog(headers);
77
78 t = new Thread(this, "FreeEMSByteLA Loading");
79 t.setPriority(Thread.MAX_PRIORITY);
80 t.start();
81
82 }
83
84
85
86
87
88
89
90
91 public void run() {
92 try {
93
94 byte[] readByte = new byte[1];
95 short uByte = 0;
96
97 startFound = false;
98 logStream = new FileInputStream(logFile);
99 decodedLog.setLogStatus(GenericLog.LOG_LOADING);
100 int packetLength = 0;
101 while (logStream.read(readByte) != -1) {
102 uByte = (short) (readByte[0] & 0xff);
103 if (uByte == START_BYTE) {
104 if (!startFound) {
105 startFound = true;
106 } else {
107
108 }
109 } else if (startFound) {
110
111 if (uByte == STOP_BYTE) {
112 if (checksum(wholePacket, packetLength)) {
113 decodePacket(wholePacket, packetLength);
114 startFound = false;
115 }
116 packetLength = 0;
117 } else if (uByte == ESCAPE_BYTE) {
118 if (logStream.read(readByte) != -1) {
119 uByte = unEscapeByte((short) (readByte[0] & 0xff));
120 if (uByte != (short) -1) {
121 wholePacket[packetLength] = uByte;
122 packetLength++;
123
124 } else {
125 startFound = false;
126 }
127 }
128 } else {
129 wholePacket[packetLength] = uByte;
130 packetLength++;
131 }
132 }
133
134
135 }
136 decodedLog.setLogStatus(1);
137
138
139 } catch (IOException IOE) {
140 System.out.println(IOE.getMessage());
141
142
143 }
144
145 }
146
147
148
149
150
151
152
153 private void decodePacket(short[] packet, int length) {
154 int PTIT = (int)packet[3];
155 int T0 = 0;
156 int T1 = 0;
157 int T2 = 0;
158 int T3 = 0;
159 int T4 = 0;
160 int T5 = 0;
161 int T6 = 0;
162 int T7 = 0;
163 decodedLog.addValue("PTIT", PTIT);
164
165 if((PTIT % 2) == 1){
166 T0 = 1;
167 PTIT -= 1;
168 }
169 if((PTIT % 4) == 2){
170 T1 = 1;
171 PTIT -= 2;
172 }
173 if((PTIT % 8) == 4){
174 T2 = 1;
175 PTIT -= 4;
176 }
177 if((PTIT % 16) == 8){
178 T3 = 1;
179 PTIT -= 8;
180 }
181 if((PTIT % 32) == 16){
182 T4 = 1;
183 PTIT -= 16;
184 }
185 if((PTIT % 64) == 32){
186 T5 = 1;
187 PTIT -= 32;
188 }
189 if((PTIT % 128) == 64){
190 T6 = 1;
191 PTIT -= 64;
192 }
193 if(PTIT == 128){
194 T7 = 1;
195 }
196
197 decodedLog.addValue("T0", T0);
198 decodedLog.addValue("T1", T1);
199 decodedLog.addValue("T2", T2);
200 decodedLog.addValue("T3", T3);
201 decodedLog.addValue("T4", T4);
202 decodedLog.addValue("T5", T5);
203 decodedLog.addValue("T6", T6);
204 decodedLog.addValue("T7", T7);
205 }
206
207
208
209
210
211
212
213 private boolean checksum(short[] packet, int length) {
214 if(length > 0){
215 short includedSum = packet[length -1];
216 long veryBIGsum = 0;
217 for(int x=0;x<length-1;x++){
218 veryBIGsum += packet[x];
219 }
220 short calculatedSum = (short)(veryBIGsum % 256);
221 return (calculatedSum == includedSum) ? true : false;
222 }else{
223 return false;
224 }
225 }
226
227
228
229
230
231
232 private short unEscapeByte(short uByte) {
233 if (uByte == ESCAPED_START_BYTE) {
234 return START_BYTE;
235 } else if (uByte == ESCAPED_STOP_BYTE) {
236 return STOP_BYTE;
237 } else if (uByte == ESCAPED_ESCAPE_BYTE) {
238 return ESCAPE_BYTE;
239 } else {
240 return (short) -1;
241 }
242 }
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263 @Override
264 public String toString() {
265 return super.toString();
266 }
267 }