1 |
2 |
ZTEX |
/*!
|
2 |
3 |
ZTEX |
Java host software API of ZTEX SDK
|
3 |
|
|
Copyright (C) 2009-2014 ZTEX GmbH.
|
4 |
2 |
ZTEX |
http://www.ztex.de
|
5 |
|
|
|
6 |
|
|
This program is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License version 3 as
|
8 |
|
|
published by the Free Software Foundation.
|
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, but
|
11 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
13 |
|
|
General Public License for more details.
|
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License
|
16 |
|
|
along with this program; if not, see http://www.gnu.org/licenses/.
|
17 |
|
|
!*/
|
18 |
|
|
|
19 |
|
|
/*
|
20 |
|
|
Reads an ihx file with ZTEX descriptor 1
|
21 |
|
|
*/
|
22 |
|
|
package ztex;
|
23 |
|
|
|
24 |
|
|
import java.io.*;
|
25 |
|
|
import java.util.*;
|
26 |
|
|
|
27 |
|
|
/**
|
28 |
|
|
* Represents a firmware image with ZTEX descriptor 1 loaded from an ihx (Intel Hex format) file. <br>
|
29 |
|
|
* The ZTEX descriptor is usually located at the position 0x6x of the firmware image. <br>
|
30 |
|
|
* A description of the ZTEX descriptor 1 can be found in {@link ZtexDevice1}.
|
31 |
|
|
* @see ZtexDevice1
|
32 |
|
|
* @see Ztex1
|
33 |
|
|
*/
|
34 |
|
|
public class ZtexIhxFile1 extends IhxFile {
|
35 |
|
|
private static final int defaultZtexDescriptorOffs = 0x6c;
|
36 |
|
|
|
37 |
|
|
private int ztexDescriptorOffs = defaultZtexDescriptorOffs;
|
38 |
|
|
|
39 |
|
|
private byte productId[] = { 0,0,0,0 }; // product ID from the ZTEX descriptor, not the USB product ID
|
40 |
|
|
private byte fwVersion = 0;
|
41 |
|
|
private byte interfaceVersion = 0;
|
42 |
|
|
private byte interfaceCapabilities[] = { 0,0,0,0, 0,0 };
|
43 |
|
|
private byte moduleReserved[] = { 0,0,0,0, 0,0,0,0, 0,0,0,0 };
|
44 |
|
|
private char snString[] = new char[10];
|
45 |
|
|
|
46 |
|
|
// ******* ZtexIhxFile1 ********************************************************
|
47 |
|
|
/**
|
48 |
|
|
* Constructs an instance from a given file name and descriptor position.<br>
|
49 |
|
|
* This method can also read system resources, e.g. files from the current jar archive.
|
50 |
|
|
* @param in Input stream from which the ihx file is read.
|
51 |
|
|
* @param name Name of the input.
|
52 |
|
|
* @param pZtexDescriptorOffs The position of the descriptor in bytes. The default position is 0x6c.
|
53 |
|
|
* @throws IOException If an read error occurred.
|
54 |
|
|
* @throws IhxFileDamagedException If the ihx file is damaged.
|
55 |
|
|
* @throws IncompatibleFirmwareException If the firmware image contains no valid ZTEX descriptor 1 at the specified position.
|
56 |
|
|
*/
|
57 |
|
|
public ZtexIhxFile1( InputStream in, String name, int pZtexDescriptorOffs ) throws IOException, IhxFileDamagedException, IncompatibleFirmwareException {
|
58 |
|
|
super( in, name );
|
59 |
|
|
|
60 |
|
|
ztexDescriptorOffs = pZtexDescriptorOffs;
|
61 |
|
|
|
62 |
|
|
if ( ihxData[ztexDescriptorOffs]!=40 || ihxData[ztexDescriptorOffs+1]!=1 || ihxData[ztexDescriptorOffs+2]!='Z' || ihxData[ztexDescriptorOffs+3]!='T' || ihxData[ztexDescriptorOffs+4]!='E' || ihxData[ztexDescriptorOffs+5]!='X' )
|
63 |
|
|
throw new IncompatibleFirmwareException( "Invalid ZTEX descriptor" );
|
64 |
|
|
|
65 |
|
|
productId[0] = (byte) ihxData[ztexDescriptorOffs+6];
|
66 |
|
|
productId[1] = (byte) ihxData[ztexDescriptorOffs+7];
|
67 |
|
|
productId[2] = (byte) ihxData[ztexDescriptorOffs+8];
|
68 |
|
|
productId[3] = (byte) ihxData[ztexDescriptorOffs+9];
|
69 |
|
|
fwVersion = (byte) ihxData[ztexDescriptorOffs+10];
|
70 |
|
|
interfaceVersion = (byte) ihxData[ztexDescriptorOffs+11];
|
71 |
|
|
interfaceCapabilities[0] = (byte) ihxData[ztexDescriptorOffs+12];
|
72 |
|
|
interfaceCapabilities[1] = (byte) ihxData[ztexDescriptorOffs+13];
|
73 |
|
|
interfaceCapabilities[2] = (byte) ihxData[ztexDescriptorOffs+14];
|
74 |
|
|
interfaceCapabilities[3] = (byte) ihxData[ztexDescriptorOffs+15];
|
75 |
|
|
interfaceCapabilities[4] = (byte) ihxData[ztexDescriptorOffs+16];
|
76 |
|
|
interfaceCapabilities[5] = (byte) ihxData[ztexDescriptorOffs+17];
|
77 |
|
|
moduleReserved[0] = (byte) ihxData[ztexDescriptorOffs+18];
|
78 |
|
|
moduleReserved[1] = (byte) ihxData[ztexDescriptorOffs+19];
|
79 |
|
|
moduleReserved[2] = (byte) ihxData[ztexDescriptorOffs+20];
|
80 |
|
|
moduleReserved[3] = (byte) ihxData[ztexDescriptorOffs+21];
|
81 |
|
|
moduleReserved[4] = (byte) ihxData[ztexDescriptorOffs+22];
|
82 |
|
|
moduleReserved[5] = (byte) ihxData[ztexDescriptorOffs+23];
|
83 |
|
|
moduleReserved[6] = (byte) ihxData[ztexDescriptorOffs+24];
|
84 |
|
|
moduleReserved[7] = (byte) ihxData[ztexDescriptorOffs+25];
|
85 |
|
|
moduleReserved[8] = (byte) ihxData[ztexDescriptorOffs+26];
|
86 |
|
|
moduleReserved[9] = (byte) ihxData[ztexDescriptorOffs+27];
|
87 |
|
|
moduleReserved[10] = (byte) ihxData[ztexDescriptorOffs+28];
|
88 |
|
|
moduleReserved[11] = (byte) ihxData[ztexDescriptorOffs+29];
|
89 |
|
|
|
90 |
|
|
for (int i=0; i<10; i++ ) {
|
91 |
|
|
int b = ihxData[ztexDescriptorOffs+30+i];
|
92 |
|
|
if ( b>=0 && b<=255 ) {
|
93 |
|
|
snString[i] = (char) b;
|
94 |
|
|
}
|
95 |
|
|
else {
|
96 |
|
|
throw new IncompatibleFirmwareException( "Invalid serial number string" );
|
97 |
|
|
}
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
// ensure word aligned upload data
|
101 |
|
|
for ( int i=0; i+1<ihxData.length; i+=2 )
|
102 |
|
|
if ( ihxData[i]<0 && ihxData[i+1]>=0 )
|
103 |
|
|
ihxData[i] = 0;
|
104 |
|
|
}
|
105 |
|
|
|
106 |
|
|
/**
|
107 |
|
|
* Constructs an instance from a given file name and descriptor position.<br>
|
108 |
|
|
* The ZTEX descriptor 1 is expected to be at the position 0x6c of the firmware image.<br>
|
109 |
|
|
* This method can also read system resources, e.g. files from the current jar archive.
|
110 |
|
|
* @param in Input stream from which the ihx file is read.
|
111 |
|
|
* @param name Name of the input.
|
112 |
|
|
* @throws IOException If an read error occurred.
|
113 |
|
|
* @throws IhxFileDamagedException If the ihx file is damaged.
|
114 |
|
|
* @throws IncompatibleFirmwareException If the firmware image contains no valid ZTEX descriptor 1 at the specified position.
|
115 |
|
|
*/
|
116 |
|
|
public ZtexIhxFile1( InputStream in, String name ) throws IOException, IhxFileDamagedException, IncompatibleFirmwareException {
|
117 |
|
|
this( in, name, defaultZtexDescriptorOffs );
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
/**
|
121 |
|
|
* Constructs an instance from a given file name and descriptor position.<br>
|
122 |
|
|
* This method can also read system resources, e.g. files from the current jar archive.
|
123 |
|
|
* @param fileName The file name.
|
124 |
|
|
* @param pZtexDescriptorOffs The position of the descriptor in bytes. The default position is 0x6c.
|
125 |
|
|
* @throws IOException If an read error occurred.
|
126 |
|
|
* @throws IhxFileDamagedException If the ihx file is damaged.
|
127 |
|
|
* @throws IncompatibleFirmwareException If the firmware image contains no valid ZTEX descriptor 1 at the specified position.
|
128 |
|
|
*/
|
129 |
|
|
public ZtexIhxFile1( String fileName , int pZtexDescriptorOffs ) throws IOException, IhxFileDamagedException, IncompatibleFirmwareException {
|
130 |
|
|
this( JInputStream.getInputStream(fileName), fileName, pZtexDescriptorOffs );
|
131 |
|
|
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/**
|
135 |
|
|
* Constructs an instance from a given file name.
|
136 |
|
|
* The ZTEX descriptor 1 is expected to be at the position 0x6c of the firmware image.<br>
|
137 |
|
|
* This method can also read system resources, e.g. files from the current jar archive.
|
138 |
|
|
* @param fileName The file name.
|
139 |
|
|
* @throws IOException If an read error occurred.
|
140 |
|
|
* @throws IhxFileDamagedException If the ihx file is damaged.
|
141 |
|
|
* @throws IncompatibleFirmwareException If the firmware image contains no valid ZTEX descriptor 1 at the specified position.
|
142 |
|
|
*/
|
143 |
|
|
public ZtexIhxFile1( String fileName ) throws IOException, IhxFileDamagedException, IncompatibleFirmwareException {
|
144 |
|
|
this( JInputStream.getInputStream(fileName), fileName, defaultZtexDescriptorOffs );
|
145 |
|
|
}
|
146 |
|
|
|
147 |
|
|
// ******* productId ***********************************************************
|
148 |
|
|
/**
|
149 |
|
|
* Returns the product ID (all 4 bytes).
|
150 |
|
|
* @return PRODUCT_ID, see {@link ZtexDevice1}.
|
151 |
|
|
*/
|
152 |
|
|
public final byte[] productId() {
|
153 |
|
|
return productId;
|
154 |
|
|
}
|
155 |
|
|
|
156 |
|
|
/**
|
157 |
|
|
* Returns byte i of the product ID.
|
158 |
|
|
* @return PRODUCT_ID[i], see {@link ZtexDevice1}.
|
159 |
|
|
* @param i index
|
160 |
|
|
*/
|
161 |
|
|
public int productId( int i ) {
|
162 |
|
|
return productId[i] & 255;
|
163 |
|
|
}
|
164 |
|
|
|
165 |
|
|
// ******* fwVersion ***********************************************************
|
166 |
|
|
/**
|
167 |
|
|
* Returns the firmware version.
|
168 |
|
|
* @return FW_VERSION, see {@link ZtexDevice1}.
|
169 |
|
|
*/
|
170 |
|
|
public final int fwVersion() {
|
171 |
|
|
return fwVersion & 255;
|
172 |
|
|
}
|
173 |
|
|
|
174 |
|
|
// ******* interfaceVersion *****************************************************
|
175 |
|
|
/**
|
176 |
|
|
* Returns the interface version.
|
177 |
|
|
* @return INTERFACE_VERSION, see {@link ZtexDevice1}.
|
178 |
|
|
*/
|
179 |
|
|
public final int interfaceVersion() {
|
180 |
|
|
return interfaceVersion & 255;
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
// ******* interfaceCapabilities ************************************************
|
184 |
|
|
/**
|
185 |
|
|
* Returns the interface capabilities (all 6 bytes).
|
186 |
|
|
* @return INTERFACE_CAPABILITIES, see {@link ZtexDevice1}.
|
187 |
|
|
*/
|
188 |
|
|
public final byte[] interfaceCapabilities() {
|
189 |
|
|
return interfaceCapabilities;
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
/**
|
193 |
|
|
* Returns byte i of the interface capabilities.
|
194 |
|
|
* @return INTERFACE_CAPABILITIES[i], see {@link ZtexDevice1}.
|
195 |
|
|
* @param i index
|
196 |
|
|
*/
|
197 |
|
|
public final int interfaceCapabilities( int i ) {
|
198 |
|
|
return interfaceCapabilities[i] & 255;
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
// ******* moduleReserved ******************************************************
|
202 |
|
|
/**
|
203 |
|
|
* Returns the application specific information (all 12 bytes).
|
204 |
|
|
* @return MODULE_RESERVED, see {@link ZtexDevice1}.
|
205 |
|
|
*/
|
206 |
|
|
public final byte[] moduleReserved() {
|
207 |
|
|
return moduleReserved;
|
208 |
|
|
}
|
209 |
|
|
|
210 |
|
|
/**
|
211 |
|
|
* Returns byte i of the application specific information.
|
212 |
|
|
* @return MODULE_RESERVED[i], see {@link ZtexDevice1}.
|
213 |
|
|
* @param i index
|
214 |
|
|
*/
|
215 |
|
|
public final int moduleReserved( int i ) {
|
216 |
|
|
return moduleReserved[i] & 255;
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
// ******* snString ************************************************************
|
220 |
|
|
/**
|
221 |
|
|
* Returns the serial number string.
|
222 |
|
|
* @return SN_STRING, see {@link ZtexDevice1}.
|
223 |
|
|
*/
|
224 |
|
|
public final String snString() {
|
225 |
|
|
return new String( snString );
|
226 |
|
|
}
|
227 |
|
|
|
228 |
|
|
// ******* setSnString **********************************************************
|
229 |
|
|
/**
|
230 |
|
|
* Modifies the serial number string.
|
231 |
|
|
* @param s The new serial number string which must not be longer then 10 characters.
|
232 |
|
|
*/
|
233 |
|
|
public final void setSnString( String s ) throws IncompatibleFirmwareException {
|
234 |
|
|
if ( s.length()>10 )
|
235 |
|
|
throw new IncompatibleFirmwareException( "Serial number too long (max. 10 characters)" );
|
236 |
|
|
|
237 |
|
|
int i=0;
|
238 |
|
|
for (; i<s.length(); i++ ) {
|
239 |
|
|
ihxData[ztexDescriptorOffs+30+i] = (byte) s.charAt(i);
|
240 |
|
|
}
|
241 |
|
|
for (; i<10; i++ ) {
|
242 |
|
|
ihxData[ztexDescriptorOffs+30+i] = 0;
|
243 |
|
|
}
|
244 |
|
|
}
|
245 |
|
|
|
246 |
|
|
// ******* toString ************************************************************
|
247 |
|
|
/**
|
248 |
|
|
* Returns a string representation if the instance.
|
249 |
|
|
* @return a string representation if the instance.
|
250 |
|
|
*/
|
251 |
|
|
public String toString () {
|
252 |
|
|
return "productID=" + ZtexDevice1.byteArrayString(productId) + " fwVer="+(fwVersion & 255) + " ifVer="+(interfaceVersion & 255)+ " snString=\"" + snString() + "\"";
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
}
|