connectivity/com.nokia.tcf.test/src/com/nokia/tcf/test/TestVirtualSerial.java
changeset 59 c892c53c664e
parent 44 e4eb00aa1a3f
child 60 9d2210c8eed2
equal deleted inserted replaced
44:e4eb00aa1a3f 59:c892c53c664e
     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 package com.nokia.tcf.test;
       
    18 
       
    19 import java.io.IOException;
       
    20 
       
    21 import org.eclipse.core.runtime.IStatus;
       
    22 
       
    23 import com.nokia.tcf.api.ITCAPIConnection;
       
    24 import com.nokia.tcf.api.ITCMessage;
       
    25 import com.nokia.tcf.api.ITCMessageIds;
       
    26 import com.nokia.tcf.api.ITCMessageInputStream;
       
    27 import com.nokia.tcf.api.ITCMessageOptions;
       
    28 import com.nokia.tcf.api.ITCVirtualSerialConnection;
       
    29 import com.nokia.tcf.api.ITCConnection;
       
    30 import com.nokia.tcf.api.TCFClassFactory;
       
    31 import junit.framework.TestCase;
       
    32 
       
    33 /**
       
    34  * 1 client 1 connection RAW TRK (non-TraceCore TRK) over USB
       
    35  */
       
    36 public class TestVirtualSerial extends TestCase {
       
    37 
       
    38 	public void testVirtualSerial() {
       
    39 //		TCFClassFactory factory = new TCFClassFactory();
       
    40 		
       
    41 		// connection
       
    42 		ITCVirtualSerialConnection conn = (ITCVirtualSerialConnection)TCFClassFactory.createITCVirtualSerialConnection("COM13");
       
    43 		conn.setRetryInterval(5);
       
    44 		conn.setRetryTimeout(60);
       
    45 		String desc = conn.getConnectionDescription();
       
    46 		System.out.println(desc);
       
    47 		assertTrue("description != null", desc != null);
       
    48 		
       
    49 		// messageIds
       
    50 		ITCMessageIds messageIds = TCFClassFactory.createITCMessageIds();
       
    51 		messageIds.addMessageId((byte)0x7E);
       
    52 		
       
    53 		// messageOptions - all default
       
    54 		ITCMessageOptions messageOptions = TCFClassFactory.createITCMessageOptions();
       
    55 		conn.setDecodeFormat("rawtrk");
       
    56 		messageOptions.setMessageEncodeFormat(ITCMessageOptions.ENCODE_NO_FORMAT);
       
    57 		ITCAPIConnection api = TCFClassFactory.createITCAPIConnection();
       
    58 		IStatus connStatus = api.connect(conn, messageOptions, messageIds);
       
    59 		assertTrue("connect is not ok", connStatus.isOK());
       
    60 
       
    61 		// message
       
    62 		int[] trkpingi = {0x7e, 00, 00, 0xff, 0x7e};
       
    63 		byte[] trkping = new byte[5];
       
    64 		for (int i = 0; i < 5; i++) trkping[i] = (byte)(trkpingi[i] & 0xff);
       
    65 		ITCMessage tcMsgPing = TCFClassFactory.createITCMessage(trkping);
       
    66 		System.out.println("ping message:");
       
    67 		for (int i = 0; i < tcMsgPing.size(); i++) {
       
    68 			System.out.printf(" %x", tcMsgPing.getMessage()[i]);			
       
    69 		}
       
    70 		System.out.printf("\n");
       
    71 		tcMsgPing.setUseMyMessageId(false, (byte)0);
       
    72 		
       
    73 		int[] trkversioni = {0x7e, 0x08, 0x01, 0xf6, 0x7e};
       
    74 		byte[] trkversion = new byte[5];
       
    75 		for (int i = 0; i < 5; i++) trkversion[i] = (byte)(trkversioni[i] & 0xff);
       
    76 		ITCMessage tcMsgVersion = TCFClassFactory.createITCMessage(trkversion);
       
    77 		System.out.println("version message:");
       
    78 		for (int i = 0; i < tcMsgVersion.size(); i++) {
       
    79 			System.out.printf(" %x", tcMsgVersion.getMessage()[i]);			
       
    80 		}
       
    81 		System.out.printf("\n");
       
    82 		tcMsgVersion.setUseMyMessageId(false, (byte)0);
       
    83 	
       
    84 		
       
    85 		IStatus sendStatus = null;
       
    86 
       
    87 		if (connStatus.isOK()) {
       
    88 			sendStatus = api.sendMessage(tcMsgPing);
       
    89 			sendStatus = api.sendMessage(tcMsgVersion);
       
    90 		}
       
    91 
       
    92 		ITCMessageInputStream stream = api.getInputStream();
       
    93 		ITCMessage msg = null;
       
    94 		int numberMessages = 0;
       
    95 		try {
       
    96 			numberMessages = stream.peekMessages();
       
    97 		} catch (IOException e) {
       
    98 			e.printStackTrace();
       
    99 		}
       
   100 		System.out.printf("numberMessages = %d\n", numberMessages);
       
   101 		if (numberMessages > 0) {
       
   102 			try {
       
   103 				msg = stream.readMessage();
       
   104 				System.out.printf("messageSize = %d\n", msg.size());
       
   105 				if (msg.size() > 0) {
       
   106 					for (int i = 0; i < msg.size(); i++) {
       
   107 						System.out.printf(" %x", msg.getMessage()[i]);
       
   108 					}
       
   109 					System.out.printf("\n");
       
   110 				}
       
   111 			} catch (IOException e) {
       
   112 				e.printStackTrace();
       
   113 			}
       
   114 		}
       
   115 
       
   116 		// shutdown BT on phone then execute this
       
   117 		if (connStatus.isOK()) {
       
   118 			sendStatus = api.sendMessage(tcMsgPing);
       
   119 			sendStatus = api.sendMessage(tcMsgVersion);
       
   120 		}
       
   121 		
       
   122 		numberMessages = 0;
       
   123 		try {
       
   124 			numberMessages = stream.peekMessages();
       
   125 		} catch (IOException e) {
       
   126 			e.printStackTrace();
       
   127 		}
       
   128 		System.out.printf("numberMessages = %d\n", numberMessages);
       
   129 		if (numberMessages > 0) {
       
   130 			try {
       
   131 				msg = stream.readMessage();
       
   132 				System.out.printf("messageSize = %d\n", msg.size());
       
   133 				if (msg.size() > 0) {
       
   134 					for (int i = 0; i < msg.size(); i++) {
       
   135 						System.out.printf(" %x", msg.getMessage()[i]);
       
   136 					}
       
   137 					System.out.printf("\n");
       
   138 				}
       
   139 			} catch (IOException e) {
       
   140 				e.printStackTrace();
       
   141 			}
       
   142 		}
       
   143 		if (connStatus.isOK()) {
       
   144 			api.disconnect();
       
   145 		}
       
   146 		
       
   147 	}
       
   148 }