|
1 /* |
|
2 * Copyright (c) 2009 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 the License "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 */ |
|
17 |
|
18 package com.nokia.carbide.cpp.pi.power; |
|
19 |
|
20 import java.io.ByteArrayInputStream; |
|
21 import java.io.DataInputStream; |
|
22 import java.io.EOFException; |
|
23 import java.io.File; |
|
24 import java.io.FileInputStream; |
|
25 import java.util.Vector; |
|
26 |
|
27 import com.nokia.carbide.cpp.internal.pi.model.GenericTrace; |
|
28 import com.nokia.carbide.cpp.internal.pi.model.ParsedTraceData; |
|
29 import com.nokia.carbide.cpp.internal.pi.model.Parser; |
|
30 import com.nokia.carbide.cpp.pi.util.GeneralMessages; |
|
31 |
|
32 |
|
33 public class PwrTraceParser extends Parser |
|
34 { |
|
35 private boolean debug = false; |
|
36 private PwrSample[] traceData; |
|
37 |
|
38 public PwrTraceParser() throws Exception |
|
39 { |
|
40 } |
|
41 |
|
42 public PwrTraceParser(File file) throws Exception |
|
43 { |
|
44 if (!file.exists() || file.isDirectory()) |
|
45 { |
|
46 throw new Exception(Messages.getString("PwrTraceParser.cannotOpenTraceFile")); //$NON-NLS-1$ |
|
47 } |
|
48 |
|
49 FileInputStream fis = new FileInputStream(file); |
|
50 byte[] data = new byte[(int)file.length()]; |
|
51 fis.read(data); |
|
52 this.traceVersion = this.getVersion(data); |
|
53 System.out.println(Messages.getString("PwrTraceParser.powerVersion") + this.traceVersion); //$NON-NLS-1$ |
|
54 if (traceVersion.indexOf("V1.57") != -1) //$NON-NLS-1$ |
|
55 this.parse(file); |
|
56 else |
|
57 System.out.println(Messages.getString("PwrTraceParser.unsupportedVersion") + this.traceVersion); //$NON-NLS-1$ |
|
58 fis.close(); |
|
59 } |
|
60 |
|
61 public ParsedTraceData parse(File file) throws Exception |
|
62 { |
|
63 if (!file.exists() || file.isDirectory()) |
|
64 { |
|
65 throw new Exception(Messages.getString("PwrTraceParser.cannotOpenTraceFile")); //$NON-NLS-1$ |
|
66 } |
|
67 |
|
68 Vector intermediateTraceData = new Vector(); |
|
69 |
|
70 try |
|
71 { |
|
72 FileInputStream fis = new FileInputStream(file); |
|
73 byte[] data = new byte[(int)file.length()]; |
|
74 fis.read(data); |
|
75 |
|
76 this.traceVersion = this.getVersion(data); |
|
77 System.out.println(Messages.getString("PwrTraceParser.powerVersion") + this.traceVersion); //$NON-NLS-1$ |
|
78 if (traceVersion.indexOf("V1.57") == -1) //$NON-NLS-1$ |
|
79 { |
|
80 System.out.println(Messages.getString("PwrTraceParser.unsupportedVersion") + this.traceVersion); //$NON-NLS-1$ |
|
81 fis.close(); |
|
82 return null; |
|
83 } |
|
84 |
|
85 ByteArrayInputStream bais = new ByteArrayInputStream(data); |
|
86 DataInputStream dis = new DataInputStream(bais); |
|
87 |
|
88 // read the length of the header |
|
89 int length = dis.readByte(); |
|
90 // skip the header |
|
91 for (int i = 0; i < length; i++) |
|
92 dis.readByte(); |
|
93 |
|
94 long voltage = 0; |
|
95 long current = 0; |
|
96 long capacity = 0; |
|
97 long sampleTime = 0; |
|
98 |
|
99 try |
|
100 { |
|
101 while(true) |
|
102 { |
|
103 capacity = readTUint16(dis); |
|
104 voltage = readTUint16(dis); |
|
105 current = readTUint(dis); |
|
106 sampleTime = readTUint(dis); |
|
107 |
|
108 PwrSample sample = new PwrSample(sampleTime, current, voltage, capacity); |
|
109 |
|
110 intermediateTraceData.add(sample); |
|
111 } |
|
112 } |
|
113 catch (EOFException eof) |
|
114 { |
|
115 //return; |
|
116 } |
|
117 catch (Exception e) |
|
118 { |
|
119 GeneralMessages.showErrorMessage(Messages.getString("PwrTraceParser.errorReadingTraceFile")); //$NON-NLS-1$ |
|
120 throw e; |
|
121 } |
|
122 } |
|
123 |
|
124 catch (Exception e) |
|
125 { |
|
126 GeneralMessages.showErrorMessage(Messages.getString("PwrTraceParser.errorReadingTraceFile")); //$NON-NLS-1$ |
|
127 throw e; |
|
128 } |
|
129 |
|
130 if (debug) System.out.println(Messages.getString("PwrTraceParser.traceFileParsed") + intermediateTraceData.size()); //$NON-NLS-1$ |
|
131 |
|
132 // all samples have been parsed |
|
133 intermediateTraceData.trimToSize(); |
|
134 this.traceData = new PwrSample[intermediateTraceData.size()]; |
|
135 |
|
136 // store the trace data into an array |
|
137 intermediateTraceData.toArray(this.traceData); |
|
138 |
|
139 ParsedTraceData ptd = new ParsedTraceData(); |
|
140 ptd.traceData = this.getTrace(); |
|
141 return ptd; |
|
142 } |
|
143 |
|
144 private String getVersion(byte[] data) |
|
145 { |
|
146 int length = data[0]; |
|
147 |
|
148 String ver = Messages.getString("PwrTraceParser.unknown"); //$NON-NLS-1$ |
|
149 String verString = new String(data, 1, length); |
|
150 if (debug) |
|
151 System.out.println(Messages.getString("PwrTraceParser.versionStringDebug") + verString); //$NON-NLS-1$ |
|
152 |
|
153 if(verString.indexOf("Bappea") != -1) //$NON-NLS-1$ |
|
154 if(verString.indexOf("PWR") != -1) //$NON-NLS-1$ |
|
155 { |
|
156 int index = verString.indexOf("_"); //$NON-NLS-1$ |
|
157 ver = verString.substring(index + 1, length); |
|
158 } |
|
159 return ver; |
|
160 } |
|
161 |
|
162 private long readTUint(DataInputStream dis) throws Exception |
|
163 { |
|
164 long result = dis.readUnsignedByte(); |
|
165 result += dis.readUnsignedByte() << 8; |
|
166 result += dis.readUnsignedByte() << 16; |
|
167 result += dis.readUnsignedByte() << 24; |
|
168 return result; |
|
169 } |
|
170 |
|
171 private long readTUint16(DataInputStream dis) throws Exception |
|
172 { |
|
173 int result = dis.readUnsignedByte(); |
|
174 result += dis.readUnsignedByte() << 8; |
|
175 return result; |
|
176 } |
|
177 |
|
178 private GenericTrace getTrace() |
|
179 { |
|
180 PwrTrace trace = new PwrTrace(); |
|
181 for (int i = 0; i < traceData.length; i++) |
|
182 { |
|
183 trace.addSample(this.traceData[i]); |
|
184 } |
|
185 return trace; |
|
186 } |
|
187 |
|
188 public static void main( String[] args ) |
|
189 { |
|
190 try { |
|
191 PwrTraceParser p = new PwrTraceParser(new File(Messages.getString("PwrTraceParser.tmpFilePath"))); //$NON-NLS-1$ |
|
192 } catch ( Exception e ) { |
|
193 System.out.println(Messages.getString("PwrTraceParser.exceptionMessage") + e.getMessage() ); //$NON-NLS-1$ |
|
194 } |
|
195 } |
|
196 } |