|
1 /* |
|
2 * Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Base class for OST Readers |
|
17 * |
|
18 */ |
|
19 package com.nokia.traceviewer.ost; |
|
20 |
|
21 import java.io.IOException; |
|
22 |
|
23 import com.nokia.traceviewer.engine.BaseDataReader; |
|
24 import com.nokia.traceviewer.engine.TraceProperties; |
|
25 |
|
26 /** |
|
27 * Base class for OST Readers |
|
28 * |
|
29 */ |
|
30 public abstract class OstBaseReader extends BaseDataReader implements OstConsts { |
|
31 |
|
32 /** |
|
33 * Splits data from byte buffer into messages |
|
34 */ |
|
35 protected OstMessageProcessor messageProcessor; |
|
36 |
|
37 /** |
|
38 * Tells if this reader is paused or not |
|
39 */ |
|
40 protected boolean paused; |
|
41 |
|
42 /** |
|
43 * Main loop running boolean |
|
44 */ |
|
45 protected boolean running; |
|
46 |
|
47 /** |
|
48 * Byte array to store trace data get from buffer |
|
49 */ |
|
50 private byte[] traceData; |
|
51 |
|
52 /** |
|
53 * Constructor |
|
54 */ |
|
55 OstBaseReader() { |
|
56 // BaseDataReader constructor |
|
57 super(); |
|
58 traceData = new byte[MAX_MESSAGE_SIZE]; |
|
59 } |
|
60 |
|
61 /** |
|
62 * Parses a OST Simple Trace |
|
63 * |
|
64 * @param msgStart |
|
65 * start of message |
|
66 * @param headerLen |
|
67 * length of header |
|
68 */ |
|
69 boolean processOstSimpleTrace(int msgStart, int headerLen) { |
|
70 boolean valid = true; |
|
71 int componentId = 0; |
|
72 int groupId = 0; |
|
73 int traceId = 0; |
|
74 |
|
75 // Get component id |
|
76 componentId |= receiveBuffer.getInt(msgStart + headerLen |
|
77 + OST_SIMPLE_TRACE_COMPONENTID_OFFSET); |
|
78 |
|
79 // Get group id |
|
80 groupId |= receiveBuffer.getShort(msgStart + headerLen |
|
81 + OST_SIMPLE_TRACE_GROUPID_OFFSET) |
|
82 & SHORT_MASK; |
|
83 |
|
84 // Get trace id |
|
85 traceId |= receiveBuffer.getShort(msgStart + headerLen |
|
86 + OST_SIMPLE_TRACE_TRACEID_OFFSET) |
|
87 & SHORT_MASK; |
|
88 |
|
89 trace.binaryTrace = true; |
|
90 trace.traceString = null; |
|
91 |
|
92 // Set trace information |
|
93 trace.information.setComponentId(componentId); |
|
94 trace.information.setGroupId(groupId); |
|
95 trace.information.setTraceId(traceId); |
|
96 |
|
97 int bTraceHeaderStart = msgStart + headerLen |
|
98 + OST_SIMPLE_TRACE_TRACEID_OFFSET + TRACE_ID_LENGTH; |
|
99 |
|
100 // Parse BTrace variables |
|
101 parseBTraceVariables(receiveBuffer, bTraceHeaderStart, trace); |
|
102 |
|
103 // Handle possible multipart trace |
|
104 if (trace.bTraceInformation.getMultiPart() != 0) { |
|
105 valid = handleMultiPart(trace, trace.bTraceInformation |
|
106 .getMultiPart(), bTraceHeaderStart); |
|
107 } |
|
108 |
|
109 return valid; |
|
110 } |
|
111 |
|
112 /** |
|
113 * Parses a OST Ascii Trace |
|
114 * |
|
115 * @param msgStart |
|
116 * start of message |
|
117 * @param msgLen |
|
118 * length of message |
|
119 * @param headerLen |
|
120 * length of OST header |
|
121 */ |
|
122 void processOstAsciiTrace(int msgStart, int msgLen, int headerLen) { |
|
123 |
|
124 // Data length |
|
125 int dataLength = msgLen - headerLen - OST_ASCII_TRACE_TIMESTAMP_LENGTH; |
|
126 if (dataLength < 0) { |
|
127 dataLength = 0; |
|
128 } |
|
129 int position = receiveBuffer.position(); |
|
130 int limit = receiveBuffer.limit(); |
|
131 receiveBuffer.position(msgStart + headerLen |
|
132 + OST_ASCII_TRACE_TIMESTAMP_LENGTH); |
|
133 receiveBuffer.limit(msgStart + headerLen |
|
134 + OST_ASCII_TRACE_TIMESTAMP_LENGTH + dataLength); |
|
135 |
|
136 receiveBuffer.get(traceData, 0, dataLength); |
|
137 |
|
138 // Remove line breaks and ASCII end characters from the buffer |
|
139 boolean stringEndsRemoved = false; |
|
140 for (int i = dataLength - 1; i >= 0; i--) { |
|
141 byte b = traceData[i]; |
|
142 if (!stringEndsRemoved && b == '\0') { |
|
143 dataLength--; |
|
144 } else if (b == '\n' || b == '\r' || b == '\0') { |
|
145 traceData[i] = ' '; |
|
146 } else { |
|
147 stringEndsRemoved = true; |
|
148 } |
|
149 } |
|
150 |
|
151 trace.traceString = new String(traceData, 0, dataLength); |
|
152 trace.dataStart = msgStart + headerLen |
|
153 + OST_ASCII_TRACE_TIMESTAMP_LENGTH; |
|
154 |
|
155 receiveBuffer.limit(limit); |
|
156 receiveBuffer.position(position); |
|
157 |
|
158 trace.binaryTrace = false; |
|
159 } |
|
160 |
|
161 /* |
|
162 * (non-Javadoc) |
|
163 * |
|
164 * @seecom.nokia.traceviewer.engine.BaseDataReader# |
|
165 * setProtocolSpecificStuffToMultiPartTrace(byte[], |
|
166 * com.nokia.traceviewer.engine.TraceProperties) |
|
167 */ |
|
168 @Override |
|
169 protected byte[] setProtocolSpecificStuffToMultiPartTrace(byte[] byteArr, |
|
170 TraceProperties trace) { |
|
171 int totalMessageLength = byteArr.length; |
|
172 int versionNr = byteArr[OST_VERSION_OFFSET]; |
|
173 |
|
174 // OST version 0.1 or 0.0 |
|
175 if (versionNr == OST_V01 || versionNr == OST_V00) { |
|
176 totalMessageLength -= OST_V01_HEADER_LENGTH; |
|
177 byteArr[OST_V01_LENGTH_OFFSET_2] = (byte) (totalMessageLength & BYTE_MASK); |
|
178 byteArr[OST_V01_LENGTH_OFFSET_1] = (byte) ((totalMessageLength >> 8) & BYTE_MASK); |
|
179 |
|
180 // OST version 0.5 or 1.0 |
|
181 } else if (versionNr == OST_V05 || versionNr == OST_V10) { |
|
182 int messageLengthWithoutHeader = totalMessageLength |
|
183 - OST_V05_HEADER_LENGTH; |
|
184 |
|
185 // Length can fit to one byte |
|
186 if (messageLengthWithoutHeader <= 0xFF) { |
|
187 byteArr[OST_V05_LENGTH_OFFSET] = (byte) messageLengthWithoutHeader; |
|
188 |
|
189 // Use extended length |
|
190 } else { |
|
191 byteArr[OST_V05_LENGTH_OFFSET] = 0; |
|
192 totalMessageLength += OST_V05_EXT_LENGTH_LENGTH; |
|
193 |
|
194 // Array needs to be extended by 4 bytes |
|
195 byte[] newArray = new byte[totalMessageLength]; |
|
196 System.arraycopy(byteArr, 0, newArray, 0, |
|
197 OST_V05_EXT_LENGTH_LENGTH); |
|
198 |
|
199 newArray[OST_V05_EXT_LENGTH_OFFSET] = (byte) (messageLengthWithoutHeader & BYTE_MASK); |
|
200 newArray[OST_V05_EXT_LENGTH_OFFSET + 1] = (byte) ((messageLengthWithoutHeader >> 8) & BYTE_MASK); |
|
201 newArray[OST_V05_EXT_LENGTH_OFFSET + 2] = (byte) ((messageLengthWithoutHeader >> 16) & BYTE_MASK); |
|
202 newArray[OST_V05_EXT_LENGTH_OFFSET + 3] = (byte) ((messageLengthWithoutHeader >> 24) & BYTE_MASK); |
|
203 |
|
204 // Copy rest of the array to offset 8 |
|
205 System.arraycopy(byteArr, OST_V05_EXT_LENGTH_LENGTH, newArray, |
|
206 OST_V05_EXT_LENGTH_OFFSET + OST_V05_EXT_LENGTH_LENGTH, |
|
207 totalMessageLength - OST_V05_EXT_LENGTH_OFFSET |
|
208 - OST_V05_EXT_LENGTH_LENGTH); |
|
209 |
|
210 byteArr = newArray; |
|
211 |
|
212 // Data start must be moved by 4 bytes |
|
213 trace.dataStart += OST_V05_EXT_LENGTH_LENGTH; |
|
214 trace.messageLength += OST_V05_EXT_LENGTH_LENGTH; |
|
215 } |
|
216 } |
|
217 return byteArr; |
|
218 } |
|
219 |
|
220 /* |
|
221 * (non-Javadoc) |
|
222 * |
|
223 * @see com.nokia.traceviewer.engine.DataReader#isPaused() |
|
224 */ |
|
225 public boolean isPaused() { |
|
226 return paused; |
|
227 } |
|
228 |
|
229 /* |
|
230 * (non-Javadoc) |
|
231 * |
|
232 * @see com.nokia.traceviewer.engine.DataReader#pause(boolean) |
|
233 */ |
|
234 public void pause(boolean pause) { |
|
235 // Default implementation |
|
236 paused = pause; |
|
237 } |
|
238 |
|
239 /* |
|
240 * (non-Javadoc) |
|
241 * |
|
242 * @see com.nokia.traceviewer.engine.DataReader#shutdown() |
|
243 */ |
|
244 public void shutdown() { |
|
245 running = false; |
|
246 |
|
247 // Close the message processor |
|
248 if (messageProcessor != null) { |
|
249 messageProcessor.close(); |
|
250 } |
|
251 |
|
252 try { |
|
253 if (sourceChannel != null) { |
|
254 sourceChannel.close(); |
|
255 sourceChannel = null; |
|
256 } |
|
257 } catch (IOException e) { |
|
258 } |
|
259 } |
|
260 } |