connectivity/com.nokia.tcf.test/src/com/nokia/tcf/test/TestOSTVersionRequest.java
author timkelly
Tue, 17 Nov 2009 14:11:48 -0600
changeset 585 8303e1e3d7f3
parent 59 c892c53c664e
permissions -rw-r--r--
Fix PKG part for bug 9052.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
/*
cawthron
parents:
diff changeset
     2
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
cawthron
parents:
diff changeset
     3
* All rights reserved.
cawthron
parents:
diff changeset
     4
* This component and the accompanying materials are made available
cawthron
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
cawthron
parents:
diff changeset
     6
* which accompanies this distribution, and is available
cawthron
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
cawthron
parents:
diff changeset
     8
*
cawthron
parents:
diff changeset
     9
* Initial Contributors:
cawthron
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
cawthron
parents:
diff changeset
    11
*
cawthron
parents:
diff changeset
    12
* Contributors:
cawthron
parents:
diff changeset
    13
*
cawthron
parents:
diff changeset
    14
* Description: 
cawthron
parents:
diff changeset
    15
*
cawthron
parents:
diff changeset
    16
*/
cawthron
parents:
diff changeset
    17
package com.nokia.tcf.test;
cawthron
parents:
diff changeset
    18
cawthron
parents:
diff changeset
    19
import java.io.IOException;
cawthron
parents:
diff changeset
    20
cawthron
parents:
diff changeset
    21
import org.eclipse.core.runtime.IStatus;
cawthron
parents:
diff changeset
    22
cawthron
parents:
diff changeset
    23
import com.nokia.tcf.api.ITCAPIConnection;
cawthron
parents:
diff changeset
    24
import com.nokia.tcf.api.ITCMessage;
cawthron
parents:
diff changeset
    25
import com.nokia.tcf.api.ITCMessageIds;
cawthron
parents:
diff changeset
    26
import com.nokia.tcf.api.ITCMessageInputStream;
cawthron
parents:
diff changeset
    27
import com.nokia.tcf.api.ITCMessageOptions;
cawthron
parents:
diff changeset
    28
import com.nokia.tcf.api.ITCVirtualSerialConnection;
cawthron
parents:
diff changeset
    29
import com.nokia.tcf.api.TCFClassFactory;
cawthron
parents:
diff changeset
    30
import com.nokia.tcf.impl.TCAPIConnection;
cawthron
parents:
diff changeset
    31
cawthron
parents:
diff changeset
    32
import junit.framework.TestCase;
cawthron
parents:
diff changeset
    33
cawthron
parents:
diff changeset
    34
/**
cawthron
parents:
diff changeset
    35
 * Tests using OST v0.3 Version Protocol
cawthron
parents:
diff changeset
    36
 */
cawthron
parents:
diff changeset
    37
public class TestOSTVersionRequest extends TestCase {
cawthron
parents:
diff changeset
    38
59
c892c53c664e committing to 2.1.0
chpeckha
parents: 2
diff changeset
    39
	private boolean startServer = true; // use false when doing Junit plugin test - use true when doing non-plugin junit test
c892c53c664e committing to 2.1.0
chpeckha
parents: 2
diff changeset
    40
2
cawthron
parents:
diff changeset
    41
	public void testOSTVersionRequest() {
cawthron
parents:
diff changeset
    42
		// for non-plugin junit test
cawthron
parents:
diff changeset
    43
		TCAPIConnection api2 = new TCAPIConnection();
59
c892c53c664e committing to 2.1.0
chpeckha
parents: 2
diff changeset
    44
		if (startServer) {
c892c53c664e committing to 2.1.0
chpeckha
parents: 2
diff changeset
    45
			api2.nativeStartServer();
c892c53c664e committing to 2.1.0
chpeckha
parents: 2
diff changeset
    46
		}
2
cawthron
parents:
diff changeset
    47
cawthron
parents:
diff changeset
    48
		// connection
59
c892c53c664e committing to 2.1.0
chpeckha
parents: 2
diff changeset
    49
		ITCVirtualSerialConnection conn = (ITCVirtualSerialConnection)TCFClassFactory.createITCVirtualSerialConnection("COM27");
2
cawthron
parents:
diff changeset
    50
		// set OST decoding for this connection
cawthron
parents:
diff changeset
    51
		conn.setDecodeFormat("ost");
cawthron
parents:
diff changeset
    52
		
cawthron
parents:
diff changeset
    53
		// message options
cawthron
parents:
diff changeset
    54
		ITCMessageOptions options = TCFClassFactory.createITCMessageOptions();
cawthron
parents:
diff changeset
    55
		// Ask TCF to not format the protocol (we're sending entire message)
cawthron
parents:
diff changeset
    56
		options.setMessageEncodeFormat(ITCMessageOptions.ENCODE_NO_FORMAT);
cawthron
parents:
diff changeset
    57
		// ask TCF to leave the protocol headers on incoming messages
cawthron
parents:
diff changeset
    58
		options.setUnWrapFormat(ITCMessageOptions.UNWRAP_LEAVE_HEADERS);
cawthron
parents:
diff changeset
    59
		options.setInputStreamSize(64);
cawthron
parents:
diff changeset
    60
		
cawthron
parents:
diff changeset
    61
		// message Ids to capture
cawthron
parents:
diff changeset
    62
		ITCMessageIds ids = TCFClassFactory.createITCMessageIds();
cawthron
parents:
diff changeset
    63
		// This is the protocol ID to capture
cawthron
parents:
diff changeset
    64
		ids.addMessageId((byte)0x00); // OST version protocol byte = 0x00
cawthron
parents:
diff changeset
    65
//		ids.addMessageId((byte)0x02); // OST version protocol byte = 0x00
cawthron
parents:
diff changeset
    66
cawthron
parents:
diff changeset
    67
		// OST AlignVersionRequest message (including header)
cawthron
parents:
diff changeset
    68
		// RAW message     ver   00    len   len  trans  req    version to check (0.1)   
cawthron
parents:
diff changeset
    69
		byte[] alignreq = {0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x01};
cawthron
parents:
diff changeset
    70
		// convert to ITCMessage
cawthron
parents:
diff changeset
    71
		ITCMessage tcalignreq = TCFClassFactory.createITCMessage(alignreq);
cawthron
parents:
diff changeset
    72
		// Ask TCF to use the OST msg ID for TRK requests (0x90) per DSS
cawthron
parents:
diff changeset
    73
//		tcMsgPing.setUseMyMessageId(true, (byte)0x90);
cawthron
parents:
diff changeset
    74
		
cawthron
parents:
diff changeset
    75
		for (int iconnect = 0; iconnect < 1; iconnect++) {
cawthron
parents:
diff changeset
    76
			// connect
cawthron
parents:
diff changeset
    77
			ITCAPIConnection api = TCFClassFactory.createITCAPIConnection();
cawthron
parents:
diff changeset
    78
			IStatus connStatus = api.connect(conn, options, ids);
cawthron
parents:
diff changeset
    79
			System.out.printf("iconnect=%d connStatus=%d\n", iconnect, connStatus.getCode());
cawthron
parents:
diff changeset
    80
			
cawthron
parents:
diff changeset
    81
			// get stream reference
cawthron
parents:
diff changeset
    82
			ITCMessageInputStream stream = null;
cawthron
parents:
diff changeset
    83
			if (connStatus.isOK()) {
cawthron
parents:
diff changeset
    84
				stream = api.getInputStream();
cawthron
parents:
diff changeset
    85
				System.out.printf("stream=%s\n", stream.toString());
cawthron
parents:
diff changeset
    86
			}
cawthron
parents:
diff changeset
    87
			
cawthron
parents:
diff changeset
    88
			// send align req
cawthron
parents:
diff changeset
    89
			if (connStatus.isOK()) {
cawthron
parents:
diff changeset
    90
				IStatus sendStatus = api.sendMessage(tcalignreq);
cawthron
parents:
diff changeset
    91
				if (sendStatus.isOK()) {
cawthron
parents:
diff changeset
    92
					// wait for response
cawthron
parents:
diff changeset
    93
					int numberMessages = 0;
cawthron
parents:
diff changeset
    94
					int numberPeeks = 0;
cawthron
parents:
diff changeset
    95
					int numberSleeps = 0;
cawthron
parents:
diff changeset
    96
					for (int i = 0; i < 100; i ++) {
cawthron
parents:
diff changeset
    97
						try {
cawthron
parents:
diff changeset
    98
							if (stream != null)
cawthron
parents:
diff changeset
    99
								numberMessages = stream.peekMessages();
cawthron
parents:
diff changeset
   100
							numberPeeks++;
cawthron
parents:
diff changeset
   101
							if (numberMessages < 1) {
cawthron
parents:
diff changeset
   102
								try {
cawthron
parents:
diff changeset
   103
									Thread.sleep(100);
cawthron
parents:
diff changeset
   104
									numberSleeps++;
cawthron
parents:
diff changeset
   105
								} catch (InterruptedException e) {
cawthron
parents:
diff changeset
   106
									e.printStackTrace();
cawthron
parents:
diff changeset
   107
								}
cawthron
parents:
diff changeset
   108
							}
cawthron
parents:
diff changeset
   109
						} catch (IOException e) {
cawthron
parents:
diff changeset
   110
							e.printStackTrace();
cawthron
parents:
diff changeset
   111
						}
cawthron
parents:
diff changeset
   112
						if (numberMessages == 1) {
cawthron
parents:
diff changeset
   113
							break;
cawthron
parents:
diff changeset
   114
						}
cawthron
parents:
diff changeset
   115
					}
cawthron
parents:
diff changeset
   116
					System.out.printf("NumberMessages = %d numberPeeks = %d numberSleeps = %d\n", numberMessages, numberPeeks, numberSleeps);
cawthron
parents:
diff changeset
   117
					if (numberMessages > 0) {
cawthron
parents:
diff changeset
   118
						try {
cawthron
parents:
diff changeset
   119
							
cawthron
parents:
diff changeset
   120
							ITCMessage message = stream.readMessage();
cawthron
parents:
diff changeset
   121
							byte[] b = message.getMessage();
cawthron
parents:
diff changeset
   122
							System.out.printf("msg: length=%d\n", b.length);
cawthron
parents:
diff changeset
   123
							for (int i = 0; i < b.length; i++) {
cawthron
parents:
diff changeset
   124
								System.out.printf("%x ", b[i]);
cawthron
parents:
diff changeset
   125
							}
cawthron
parents:
diff changeset
   126
							System.out.printf("\n");
cawthron
parents:
diff changeset
   127
							numberMessages = stream.peekMessages();
cawthron
parents:
diff changeset
   128
						} catch (IOException e) {
cawthron
parents:
diff changeset
   129
							e.printStackTrace();
cawthron
parents:
diff changeset
   130
						}
cawthron
parents:
diff changeset
   131
					}
cawthron
parents:
diff changeset
   132
				} else {
cawthron
parents:
diff changeset
   133
					System.out.printf("Error on send = %s\n", sendStatus.getMessage());
cawthron
parents:
diff changeset
   134
				}
cawthron
parents:
diff changeset
   135
			}
cawthron
parents:
diff changeset
   136
			if (connStatus.isOK()) {
cawthron
parents:
diff changeset
   137
				api.disconnect();
cawthron
parents:
diff changeset
   138
			}
cawthron
parents:
diff changeset
   139
		}
cawthron
parents:
diff changeset
   140
		// for non-plugin junit test
59
c892c53c664e committing to 2.1.0
chpeckha
parents: 2
diff changeset
   141
		if (startServer) {
c892c53c664e committing to 2.1.0
chpeckha
parents: 2
diff changeset
   142
			api2.nativeStopServer();
c892c53c664e committing to 2.1.0
chpeckha
parents: 2
diff changeset
   143
		}
2
cawthron
parents:
diff changeset
   144
	}
cawthron
parents:
diff changeset
   145
}