| 1 |
2 |
ZTEX |
/*!
|
| 2 |
5 |
ZTEX |
Java host software API of ZTEX EZ-USB FX2 SDK
|
| 3 |
|
|
Copyright (C) 2009-2011 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 fileName The file name.
|
| 51 |
|
|
* @param pZtexDescriptorOffs The position of the descriptor in bytes. The default position is 0x6c.
|
| 52 |
|
|
* @throws IOException If an read error occurred.
|
| 53 |
|
|
* @throws IhxFileDamagedException If the ihx file is damaged.
|
| 54 |
|
|
* @throws IncompatibleFirmwareException If the firmware image contains no valid ZTEX descriptor 1 at the specified position.
|
| 55 |
|
|
*/
|
| 56 |
|
|
public ZtexIhxFile1( String fileName, int pZtexDescriptorOffs ) throws IOException, IhxFileDamagedException, IncompatibleFirmwareException {
|
| 57 |
|
|
super( fileName );
|
| 58 |
|
|
|
| 59 |
|
|
ztexDescriptorOffs = pZtexDescriptorOffs;
|
| 60 |
|
|
|
| 61 |
|
|
if ( ihxData[ztexDescriptorOffs]!=40 || ihxData[ztexDescriptorOffs+1]!=1 || ihxData[ztexDescriptorOffs+2]!='Z' || ihxData[ztexDescriptorOffs+3]!='T' || ihxData[ztexDescriptorOffs+4]!='E' || ihxData[ztexDescriptorOffs+5]!='X' )
|
| 62 |
|
|
throw new IncompatibleFirmwareException( "Invalid ZTEX descriptor" );
|
| 63 |
|
|
|
| 64 |
|
|
productId[0] = (byte) ihxData[ztexDescriptorOffs+6];
|
| 65 |
|
|
productId[1] = (byte) ihxData[ztexDescriptorOffs+7];
|
| 66 |
|
|
productId[2] = (byte) ihxData[ztexDescriptorOffs+8];
|
| 67 |
|
|
productId[3] = (byte) ihxData[ztexDescriptorOffs+9];
|
| 68 |
|
|
fwVersion = (byte) ihxData[ztexDescriptorOffs+10];
|
| 69 |
|
|
interfaceVersion = (byte) ihxData[ztexDescriptorOffs+11];
|
| 70 |
|
|
interfaceCapabilities[0] = (byte) ihxData[ztexDescriptorOffs+12];
|
| 71 |
|
|
interfaceCapabilities[1] = (byte) ihxData[ztexDescriptorOffs+13];
|
| 72 |
|
|
interfaceCapabilities[2] = (byte) ihxData[ztexDescriptorOffs+14];
|
| 73 |
|
|
interfaceCapabilities[3] = (byte) ihxData[ztexDescriptorOffs+15];
|
| 74 |
|
|
interfaceCapabilities[4] = (byte) ihxData[ztexDescriptorOffs+16];
|
| 75 |
|
|
interfaceCapabilities[5] = (byte) ihxData[ztexDescriptorOffs+17];
|
| 76 |
|
|
moduleReserved[0] = (byte) ihxData[ztexDescriptorOffs+18];
|
| 77 |
|
|
moduleReserved[1] = (byte) ihxData[ztexDescriptorOffs+19];
|
| 78 |
|
|
moduleReserved[2] = (byte) ihxData[ztexDescriptorOffs+20];
|
| 79 |
|
|
moduleReserved[3] = (byte) ihxData[ztexDescriptorOffs+21];
|
| 80 |
|
|
moduleReserved[4] = (byte) ihxData[ztexDescriptorOffs+22];
|
| 81 |
|
|
moduleReserved[5] = (byte) ihxData[ztexDescriptorOffs+23];
|
| 82 |
|
|
moduleReserved[6] = (byte) ihxData[ztexDescriptorOffs+24];
|
| 83 |
|
|
moduleReserved[7] = (byte) ihxData[ztexDescriptorOffs+25];
|
| 84 |
|
|
moduleReserved[8] = (byte) ihxData[ztexDescriptorOffs+26];
|
| 85 |
|
|
moduleReserved[9] = (byte) ihxData[ztexDescriptorOffs+27];
|
| 86 |
|
|
moduleReserved[10] = (byte) ihxData[ztexDescriptorOffs+28];
|
| 87 |
|
|
moduleReserved[11] = (byte) ihxData[ztexDescriptorOffs+29];
|
| 88 |
|
|
|
| 89 |
|
|
for (int i=0; i<10; i++ ) {
|
| 90 |
|
|
int b = ihxData[ztexDescriptorOffs+30+i];
|
| 91 |
|
|
if ( b>=0 && b<=255 ) {
|
| 92 |
|
|
snString[i] = (char) b;
|
| 93 |
|
|
}
|
| 94 |
|
|
else {
|
| 95 |
|
|
throw new IncompatibleFirmwareException( "Invalid serial number string" );
|
| 96 |
|
|
}
|
| 97 |
|
|
}
|
| 98 |
|
|
|
| 99 |
|
|
// ensure word aligned upload data
|
| 100 |
|
|
for ( int i=0; i+1<ihxData.length; i+=2 )
|
| 101 |
|
|
if ( ihxData[i]<0 && ihxData[i+1]>=0 )
|
| 102 |
|
|
ihxData[i] = 0;
|
| 103 |
|
|
}
|
| 104 |
|
|
|
| 105 |
|
|
/**
|
| 106 |
|
|
* Constructs an instance from a given file name.
|
| 107 |
|
|
* The ZTEX descriptor 1 is expected to be at the position 0x6c of the firmware image.<br>
|
| 108 |
|
|
* This method can also read system resources, e.g. files from the current jar archive.
|
| 109 |
|
|
* @param fileName The file name.
|
| 110 |
|
|
* @throws IOException If an read error occurred.
|
| 111 |
|
|
* @throws IhxFileDamagedException If the ihx file is damaged.
|
| 112 |
|
|
* @throws IncompatibleFirmwareException If the firmware image contains no valid ZTEX descriptor 1 at the specified position.
|
| 113 |
|
|
*/
|
| 114 |
|
|
public ZtexIhxFile1( String fileName ) throws IOException, IhxFileDamagedException, IncompatibleFirmwareException {
|
| 115 |
|
|
this( fileName, defaultZtexDescriptorOffs );
|
| 116 |
|
|
}
|
| 117 |
|
|
|
| 118 |
|
|
// ******* productId ***********************************************************
|
| 119 |
|
|
/**
|
| 120 |
|
|
* Returns the product ID (all 4 bytes).
|
| 121 |
|
|
* @return PRODUCT_ID, see {@link ZtexDevice1}.
|
| 122 |
|
|
*/
|
| 123 |
|
|
public final byte[] productId() {
|
| 124 |
|
|
return productId;
|
| 125 |
|
|
}
|
| 126 |
|
|
|
| 127 |
|
|
/**
|
| 128 |
|
|
* Returns byte i of the product ID.
|
| 129 |
|
|
* @return PRODUCT_ID[i], see {@link ZtexDevice1}.
|
| 130 |
|
|
* @param i index
|
| 131 |
|
|
*/
|
| 132 |
|
|
public int productId( int i ) {
|
| 133 |
|
|
return productId[i] & 255;
|
| 134 |
|
|
}
|
| 135 |
|
|
|
| 136 |
|
|
// ******* fwVersion ***********************************************************
|
| 137 |
|
|
/**
|
| 138 |
|
|
* Returns the firmware version.
|
| 139 |
|
|
* @return FW_VERSION, see {@link ZtexDevice1}.
|
| 140 |
|
|
*/
|
| 141 |
|
|
public final int fwVersion() {
|
| 142 |
|
|
return fwVersion & 255;
|
| 143 |
|
|
}
|
| 144 |
|
|
|
| 145 |
|
|
// ******* interfaceVersion *****************************************************
|
| 146 |
|
|
/**
|
| 147 |
|
|
* Returns the interface version.
|
| 148 |
|
|
* @return INTERFACE_VERSION, see {@link ZtexDevice1}.
|
| 149 |
|
|
*/
|
| 150 |
|
|
public final int interfaceVersion() {
|
| 151 |
|
|
return interfaceVersion & 255;
|
| 152 |
|
|
}
|
| 153 |
|
|
|
| 154 |
|
|
// ******* interfaceCapabilities ************************************************
|
| 155 |
|
|
/**
|
| 156 |
|
|
* Returns the interface capabilities (all 6 bytes).
|
| 157 |
|
|
* @return INTERFACE_CAPABILITIES, see {@link ZtexDevice1}.
|
| 158 |
|
|
*/
|
| 159 |
|
|
public final byte[] interfaceCapabilities() {
|
| 160 |
|
|
return interfaceCapabilities;
|
| 161 |
|
|
}
|
| 162 |
|
|
|
| 163 |
|
|
/**
|
| 164 |
|
|
* Returns byte i of the interface capabilities.
|
| 165 |
|
|
* @return INTERFACE_CAPABILITIES[i], see {@link ZtexDevice1}.
|
| 166 |
|
|
* @param i index
|
| 167 |
|
|
*/
|
| 168 |
|
|
public final int interfaceCapabilities( int i ) {
|
| 169 |
|
|
return interfaceCapabilities[i] & 255;
|
| 170 |
|
|
}
|
| 171 |
|
|
|
| 172 |
|
|
// ******* moduleReserved ******************************************************
|
| 173 |
|
|
/**
|
| 174 |
|
|
* Returns the application specific information (all 12 bytes).
|
| 175 |
|
|
* @return MODULE_RESERVED, see {@link ZtexDevice1}.
|
| 176 |
|
|
*/
|
| 177 |
|
|
public final byte[] moduleReserved() {
|
| 178 |
|
|
return moduleReserved;
|
| 179 |
|
|
}
|
| 180 |
|
|
|
| 181 |
|
|
/**
|
| 182 |
|
|
* Returns byte i of the application specific information.
|
| 183 |
|
|
* @return MODULE_RESERVED[i], see {@link ZtexDevice1}.
|
| 184 |
|
|
* @param i index
|
| 185 |
|
|
*/
|
| 186 |
|
|
public final int moduleReserved( int i ) {
|
| 187 |
|
|
return moduleReserved[i] & 255;
|
| 188 |
|
|
}
|
| 189 |
|
|
|
| 190 |
|
|
// ******* snString ************************************************************
|
| 191 |
|
|
/**
|
| 192 |
|
|
* Returns the serial number string.
|
| 193 |
|
|
* @return SN_STRING, see {@link ZtexDevice1}.
|
| 194 |
|
|
*/
|
| 195 |
|
|
public final String snString() {
|
| 196 |
|
|
return new String( snString );
|
| 197 |
|
|
}
|
| 198 |
|
|
|
| 199 |
|
|
// ******* setSnString **********************************************************
|
| 200 |
|
|
/**
|
| 201 |
|
|
* Modifies the serial number string.
|
| 202 |
|
|
* @param s The new serial number string which must not be longer then 10 characters.
|
| 203 |
|
|
*/
|
| 204 |
|
|
public final void setSnString( String s ) throws IncompatibleFirmwareException {
|
| 205 |
|
|
if ( s.length()>10 )
|
| 206 |
|
|
throw new IncompatibleFirmwareException( "Serial number too long (max. 10 characters)" );
|
| 207 |
|
|
|
| 208 |
|
|
int i=0;
|
| 209 |
|
|
for (; i<s.length(); i++ ) {
|
| 210 |
5 |
ZTEX |
ihxData[ztexDescriptorOffs+30+i] = (byte) s.charAt(i);
|
| 211 |
2 |
ZTEX |
}
|
| 212 |
|
|
for (; i<10; i++ ) {
|
| 213 |
5 |
ZTEX |
ihxData[ztexDescriptorOffs+30+i] = 0;
|
| 214 |
2 |
ZTEX |
}
|
| 215 |
|
|
}
|
| 216 |
|
|
|
| 217 |
|
|
// ******* toString ************************************************************
|
| 218 |
|
|
/**
|
| 219 |
|
|
* Returns a string representation if the instance.
|
| 220 |
|
|
* @return a string representation if the instance.
|
| 221 |
|
|
*/
|
| 222 |
|
|
public String toString () {
|
| 223 |
|
|
return "productID=" + ZtexDevice1.byteArrayString(productId) + " fwVer="+(fwVersion & 255) + " ifVer="+(interfaceVersion & 255)+ " snString=\"" + snString() + "\"";
|
| 224 |
|
|
}
|
| 225 |
|
|
|
| 226 |
|
|
}
|