debuggercdi/com.nokia.carbide.trk.support/src/com/nokia/carbide/trk/support/service/TRKConnectedService.java
branchRCL_2_4
changeset 857 d66843399035
parent 663 0ede5f1bb87e
child 898 b8f39c88d4f1
equal deleted inserted replaced
855:3f37e327885c 857:d66843399035
    16 */
    16 */
    17 
    17 
    18 
    18 
    19 package com.nokia.carbide.trk.support.service;
    19 package com.nokia.carbide.trk.support.service;
    20 
    20 
       
    21 import java.io.IOException;
       
    22 import java.text.MessageFormat;
       
    23 import java.util.Arrays;
       
    24 import java.util.List;
       
    25 
       
    26 import org.eclipse.core.runtime.IProgressMonitor;
       
    27 import org.osgi.framework.Version;
       
    28 
    21 import com.freescale.cdt.debug.cw.core.SerialConnectionSettings;
    29 import com.freescale.cdt.debug.cw.core.SerialConnectionSettings;
    22 import com.nokia.carbide.remoteconnections.interfaces.*;
    30 import com.nokia.carbide.remoteconnections.interfaces.AbstractConnectedService;
       
    31 import com.nokia.carbide.remoteconnections.interfaces.AbstractSynchronizedConnection;
       
    32 import com.nokia.carbide.remoteconnections.interfaces.IConnectionType;
       
    33 import com.nokia.carbide.remoteconnections.interfaces.IService;
    23 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
    34 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
    24 import com.nokia.carbide.remoteconnections.interfaces.IRemoteAgentInstallerProvider.IRemoteAgentInstaller;
    35 import com.nokia.carbide.remoteconnections.interfaces.IRemoteAgentInstallerProvider.IRemoteAgentInstaller;
    25 import com.nokia.carbide.trk.support.Messages;
    36 import com.nokia.carbide.trk.support.Messages;
    26 import com.nokia.carbide.trk.support.connection.*;
    37 import com.nokia.carbide.trk.support.connection.SerialConnectionType;
       
    38 import com.nokia.carbide.trk.support.connection.TCPIPConnectionFactory;
       
    39 import com.nokia.carbide.trk.support.connection.TCPIPConnectionType;
       
    40 import com.nokia.carbide.trk.support.connection.USBConnectionType;
    27 import com.nokia.cpp.internal.api.utils.core.Check;
    41 import com.nokia.cpp.internal.api.utils.core.Check;
    28 import com.nokia.cpp.internal.api.utils.core.Pair;
    42 import com.nokia.cpp.internal.api.utils.core.Pair;
    29 import com.nokia.tcf.api.*;
    43 import com.nokia.tcf.api.ITCAPIConnection;
    30 
    44 import com.nokia.tcf.api.ITCConnection;
    31 import org.eclipse.core.runtime.IProgressMonitor;
    45 import com.nokia.tcf.api.ITCMessage;
    32 import org.osgi.framework.Version;
    46 import com.nokia.tcf.api.ITCMessageIds;
    33 
    47 import com.nokia.tcf.api.ITCMessageInputStream;
    34 import java.io.IOException;
    48 import com.nokia.tcf.api.ITCMessageOptions;
    35 import java.text.MessageFormat;
    49 import com.nokia.tcf.api.TCFClassFactory;
    36 import java.util.List;
       
    37 
    50 
    38 /**
    51 /**
    39  *
    52  *
    40  */
    53  */
    41 public class TRKConnectedService extends AbstractConnectedService {
    54 public class TRKConnectedService extends AbstractConnectedService {
    42 	
    55 	
       
    56 	public static final String PROP_SYS_TRK = "is-system-trk"; //$NON-NLS-1$
       
    57 	
    43 	static {
    58 	static {
    44 		System.loadLibrary("GetTRKVersion"); //$NON-NLS-1$
    59 		try {
       
    60 			System.loadLibrary("GetTRKVersion"); //$NON-NLS-1$
       
    61 		} catch (UnsatisfiedLinkError e) {
       
    62 			// no such library, e.g., not on Windows or in a misconfigured dev layout
       
    63 			e.printStackTrace();
       
    64 		}
    45 	}
    65 	}
    46 	
    66 	
    47 	public native static void getTRKVersionFromSerial(String portName, 
    67 	public native static void getTRKVersionFromSerial(String portName, 
    48 			int baud, int dataBits, int parity, int stopBits, int flowControl,
    68 			int baud, int dataBits, int parity, int stopBits, int flowControl,
    49 			int[] version) throws Exception;
    69 			int[] version) throws Exception;
    60 	private static final String PORT_IN_USE =
    80 	private static final String PORT_IN_USE =
    61 		Messages.getString("TRKConnectedService.PortInUseStatus"); //$NON-NLS-1$
    81 		Messages.getString("TRKConnectedService.PortInUseStatus"); //$NON-NLS-1$
    62 	private static final String TCPIP_IN_USE =
    82 	private static final String TCPIP_IN_USE =
    63 		Messages.getString("TRKConnectedService.TCPIPInUseStatus"); //$NON-NLS-1$
    83 		Messages.getString("TRKConnectedService.TCPIPInUseStatus"); //$NON-NLS-1$
    64 	
    84 	
       
    85 	private static final Version VERSIONS3_VERSION = new Version(3, 2, 4);
    65 	private static final byte[] TRK_PING = {0x7e, 0x0, 0x0, (byte) 0xff, 0x7e};
    86 	private static final byte[] TRK_PING = {0x7e, 0x0, 0x0, (byte) 0xff, 0x7e};
    66 	private static final byte[] TRK_VERSION = {0x7e, 0x08, 0x01, (byte) 0xf6, 0x7e};
    87 	private static final byte[] TRK_VERSION = {0x7e, 0x08, 0x01, (byte) 0xf6, 0x7e};
       
    88 	private static final byte[] TRK_VERSIONS3 = {0x7e, 0x51, 0x02, (byte) 0xac, 0x7e};
       
    89 	private static final byte[] SYS_TRK_RESPONSE = 
       
    90 		{0x7e, (byte) 0x80, 0x02, 0x00, 0x03, 0x02, 0x03, 0x06, 0x04, 0x00, 0x0a, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x54, 0x52, 0x4b, (byte) 0xcb, 0x7e};
    67 	
    91 	
    68 	private Pair<String, Version> deviceOS;
    92 	private Pair<String, Version> deviceOS;
    69 	private TRKService trkService;
    93 	private TRKService trkService;
    70 	static Class<?> startTCFServer = TCFClassFactory.class; // force the tcf plugin to load
    94 	static Class<?> startTCFServer = TCFClassFactory.class; // force the tcf plugin to load
    71 
    95 
   137 		ITCMessage tcMsgPing = TCFClassFactory.createITCMessage(TRK_PING);
   161 		ITCMessage tcMsgPing = TCFClassFactory.createITCMessage(TRK_PING);
   138 		tcMsgPing.setUseMyMessageId(true, trkRequestId);
   162 		tcMsgPing.setUseMyMessageId(true, trkRequestId);
   139 		
   163 		
   140 		ITCMessage tcMsgVersion = TCFClassFactory.createITCMessage(TRK_VERSION);
   164 		ITCMessage tcMsgVersion = TCFClassFactory.createITCMessage(TRK_VERSION);
   141 		tcMsgVersion.setUseMyMessageId(true, trkRequestId);
   165 		tcMsgVersion.setUseMyMessageId(true, trkRequestId);
       
   166 		
       
   167 		ITCMessage tcMsgVersions3 = TCFClassFactory.createITCMessage(TRK_VERSIONS3);
       
   168 		tcMsgVersions3.setUseMyMessageId(true, trkRequestId);
   142 		
   169 		
   143 		// connect
   170 		// connect
   144 		ITCAPIConnection api = TCFClassFactory.createITCAPIConnection();
   171 		ITCAPIConnection api = TCFClassFactory.createITCAPIConnection();
   145 		org.eclipse.core.runtime.IStatus connStatus = api.connect(conn, options, ids);
   172 		org.eclipse.core.runtime.IStatus connStatus = api.connect(conn, options, ids);
   146 
   173 
   167 						ITCMessage tcMessage = stream.readMessage(); // version response
   194 						ITCMessage tcMessage = stream.readMessage(); // version response
   168 						byte[] message = tcMessage.getMessage();
   195 						byte[] message = tcMessage.getMessage();
   169 //						printMessage(message);
   196 //						printMessage(message);
   170 						if (message != null && message.length == 11) {
   197 						if (message != null && message.length == 11) {
   171 							version = new Version(message[4], message[5], message[8]);
   198 							version = new Version(message[4], message[5], message[8]);
       
   199 							if (version.compareTo(VERSIONS3_VERSION) >= 0) {
       
   200 								// send trk versions3
       
   201 								sendStatus = api.sendMessage(tcMsgVersions3);
       
   202 								
       
   203 								waitForSingleTCMessage(stream);
       
   204 								if (stream.peekMessages() > 0) {
       
   205 									tcMessage = stream.readMessage(); // version response
       
   206 									message = tcMessage.getMessage();
       
   207 //									printMessage(message);
       
   208 									boolean isSysTrk = Arrays.equals(message, SYS_TRK_RESPONSE);
       
   209 									getProperties().put(PROP_SYS_TRK, Boolean.valueOf(isSysTrk).toString());
       
   210 								}
       
   211 							}
   172 						}
   212 						}
   173 						else
   213 						else
   174 							throw new ConnectionFailException(Messages.getString("TRKConnectedService.BadVersionResponseError")); //$NON-NLS-1$
   214 							throw new ConnectionFailException(Messages.getString("TRKConnectedService.BadVersionResponseError")); //$NON-NLS-1$
   175 					}
   215 					}
   176 					else
   216 					else
   192 		
   232 		
   193 		return version;
   233 		return version;
   194 	}
   234 	}
   195 
   235 
   196 //	private void printMessage(byte[] message) {
   236 //	private void printMessage(byte[] message) {
   197 //	for (int i = 0; i < message.length; i++) {
   237 //		for (int i = 0; i < message.length; i++) {
   198 //		String hexString = Integer.toHexString(message[i]);
   238 //			String hexString = Integer.toHexString(message[i]);
   199 //		if (hexString.length() == 1)
   239 //			if (hexString.length() == 1)
   200 //			hexString = "0" + hexString;
   240 //				hexString = "0" + hexString;
   201 //		else if (hexString.length() > 2)
   241 //			else if (hexString.length() > 2)
   202 //			hexString = hexString.substring(hexString.length() - 2);
   242 //				hexString = hexString.substring(hexString.length() - 2);
   203 //		System.out.print(hexString);
   243 //			System.out.print(hexString);
   204 //		if (i + 1 < message.length)
   244 //			if (i + 1 < message.length)
   205 //			System.out.print('-');
   245 //				System.out.print('-');
       
   246 //		}
       
   247 //		System.out.println();
   206 //	}
   248 //	}
   207 //	System.out.println();
       
   208 //}
       
   209 
   249 
   210 	private void waitForSingleTCMessage(ITCMessageInputStream stream) throws IOException {
   250 	private void waitForSingleTCMessage(ITCMessageInputStream stream) throws IOException {
   211 		int timeout = TIMEOUT;
   251 		int timeout = TIMEOUT;
   212 		while (stream.peekMessages() == 0) {
   252 		while (stream.peekMessages() == 0) {
   213 			try {
   253 			try {
   276 					Version deviceVersion = getTRKVersionFromDevice();
   316 					Version deviceVersion = getTRKVersionFromDevice();
   277 					if (deviceVersion != null) {
   317 					if (deviceVersion != null) {
   278 						estatus = EStatus.UP;
   318 						estatus = EStatus.UP;
   279 						Version serverVersion = getLatestVersionFromServer();
   319 						Version serverVersion = getLatestVersionFromServer();
   280 						if (serverVersion == null) {
   320 						if (serverVersion == null) {
   281 							message = MessageFormat.format(OK_STATUS, deviceVersion);
   321 							StringBuilder trkVersionString = new StringBuilder();
       
   322 							String sysTrkProp = getProperties().get(PROP_SYS_TRK);
       
   323 							if (sysTrkProp != null)
       
   324 								trkVersionString.append(Boolean.parseBoolean(sysTrkProp) ? 
       
   325 										Messages.getString("TRKConnectedService.SysTRKName") : //$NON-NLS-1$
       
   326 											Messages.getString("TRKConnectedService.AppTRKName")); //$NON-NLS-1$
       
   327 							trkVersionString.append(deviceVersion.toString());
       
   328 							message = MessageFormat.format(OK_STATUS, trkVersionString);
   282 						}
   329 						}
   283 						else if (deviceVersion.compareTo(serverVersion) >= 0) {
   330 						else if (deviceVersion.compareTo(serverVersion) >= 0) {
   284 							message = MessageFormat.format(IS_LASTEST, deviceVersion);
   331 							message = MessageFormat.format(IS_LASTEST, deviceVersion.toString());
   285 						}
   332 						}
   286 						else {
   333 						else {
   287 							message = MessageFormat.format(NEEDS_INSTALL,
   334 							message = MessageFormat.format(NEEDS_INSTALL,
   288 											deviceVersion, serverVersion);
   335 											deviceVersion, serverVersion);
   289 						}
   336 						}
   301 		}
   348 		}
   302 		monitor.done();
   349 		monitor.done();
   303 		
   350 		
   304 		return new TestResult(estatus, getShortDescription(estatus), message);
   351 		return new TestResult(estatus, getShortDescription(estatus), message);
   305 	}
   352 	}
   306 
   353 	
   307 	public void setDeviceOS(String familyName, Version version) {
   354 	public void setDeviceOS(String familyName, Version version) {
   308 		deviceOS = new Pair<String, Version>(familyName, version);
   355 		deviceOS = new Pair<String, Version>(familyName, version);
   309 	}
   356 	}
   310 }
   357 }