2
|
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.ITCConnection;
|
|
25 |
import com.nokia.tcf.api.ITCMessage;
|
|
26 |
import com.nokia.tcf.api.ITCMessageIds;
|
|
27 |
import com.nokia.tcf.api.ITCMessageInputStream;
|
|
28 |
import com.nokia.tcf.api.ITCMessageOptions;
|
|
29 |
import com.nokia.tcf.api.ITCVirtualSerialConnection;
|
|
30 |
import com.nokia.tcf.api.TCFClassFactory;
|
|
31 |
import com.nokia.tcf.impl.TCAPIConnection;
|
|
32 |
|
|
33 |
import junit.framework.TestCase;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* This is a basic test to ping TRK using Debug/Trace USB (OST)
|
|
37 |
* OST headers are included
|
|
38 |
*/
|
|
39 |
public class TestOSTTRKNoFormat extends TestCase {
|
|
40 |
|
59
|
41 |
private boolean startServer = true; // use false when doing Junit plugin test - use true when doing non-plugin junit test
|
|
42 |
|
2
|
43 |
public void testOstTRK() {
|
|
44 |
// for non-plugin junit test
|
|
45 |
TCAPIConnection api2 = new TCAPIConnection();
|
59
|
46 |
if (startServer) {
|
|
47 |
api2.nativeStartServer();
|
|
48 |
}
|
2
|
49 |
|
|
50 |
// connection
|
59
|
51 |
ITCVirtualSerialConnection conn = (ITCVirtualSerialConnection)TCFClassFactory.createITCVirtualSerialConnection("COM27");
|
2
|
52 |
// set OST decoding for this connection
|
|
53 |
conn.setDecodeFormat("ost");
|
|
54 |
|
|
55 |
// message options
|
|
56 |
ITCMessageOptions options = TCFClassFactory.createITCMessageOptions();
|
|
57 |
// Ask TCF to encode the protocol (we will send raw TRK messages)
|
|
58 |
options.setMessageEncodeFormat(ITCMessageOptions.ENCODE_NO_FORMAT);
|
|
59 |
// ask TCF to delete the protocol on incoming messages
|
|
60 |
options.setUnWrapFormat(ITCMessageOptions.UNWRAP_LEAVE_HEADERS);
|
|
61 |
options.setInputStreamSize(64);
|
|
62 |
|
|
63 |
// message Ids to capture
|
|
64 |
ITCMessageIds ids = TCFClassFactory.createITCMessageIds();
|
|
65 |
// The OST msg ID for TRK responses is 0x90 per DSS
|
|
66 |
ids.addMessageId((byte)0x90); // TRK response
|
|
67 |
|
|
68 |
// TRK ping message
|
|
69 |
// RAW message
|
|
70 |
byte[] trkping = {0x01, (byte)0x90, 0x00, 0x05, 0x7e, 0, 0, (byte)0xff, 0x7e};
|
|
71 |
// convert to ITCMessage
|
|
72 |
ITCMessage tcMsgPing = TCFClassFactory.createITCMessage(trkping);
|
|
73 |
// Ask TCF to use the OST msg ID for TRK requests (0x90) per DSS
|
|
74 |
// tcMsgPing.setUseMyMessageId(true, (byte)0x90);
|
|
75 |
|
|
76 |
// TRK version message
|
|
77 |
// RAW message
|
|
78 |
byte[] trkversion = {0x01, (byte)0x90, 0x00, 0x05, 0x7e, 0x08, 0x01, (byte)0xf6, 0x7e};
|
|
79 |
// convert to ITCMessage
|
|
80 |
ITCMessage tcMsgVersion = TCFClassFactory.createITCMessage(trkversion);
|
|
81 |
// Ask TCF to use the OST msg ID for TRK requests (0x90) per DSS
|
|
82 |
// tcMsgVersion.setUseMyMessageId(true, (byte)0x90);
|
|
83 |
|
|
84 |
for (int iconnect = 0; iconnect < 1; iconnect++) {
|
|
85 |
// connect
|
|
86 |
ITCAPIConnection api = TCFClassFactory.createITCAPIConnection();
|
|
87 |
IStatus connStatus = api.connect(conn, options, ids);
|
|
88 |
System.out.printf("iconnect=%d connStatus=%d\n", iconnect, connStatus.getCode());
|
|
89 |
|
|
90 |
// get stream reference
|
|
91 |
ITCMessageInputStream stream = null;
|
|
92 |
if (connStatus.isOK()) {
|
|
93 |
stream = api.getInputStream();
|
|
94 |
System.out.printf("stream=%s\n", stream.toString());
|
|
95 |
}
|
|
96 |
|
|
97 |
// send trk ping
|
|
98 |
if (connStatus.isOK()) {
|
|
99 |
IStatus sendStatus = api.sendMessage(tcMsgPing);
|
|
100 |
if (sendStatus.isOK()) {
|
|
101 |
// wait for response
|
|
102 |
int numberMessages = 0;
|
|
103 |
int numberPeeks = 0;
|
|
104 |
int numberSleeps = 0;
|
|
105 |
for (int i = 0; i < 100; i ++) {
|
|
106 |
try {
|
|
107 |
if (stream != null)
|
|
108 |
numberMessages = stream.peekMessages();
|
|
109 |
numberPeeks++;
|
|
110 |
if (numberMessages < 1) {
|
|
111 |
try {
|
|
112 |
Thread.sleep(100);
|
|
113 |
numberSleeps++;
|
|
114 |
} catch (InterruptedException e) {
|
|
115 |
e.printStackTrace();
|
|
116 |
}
|
|
117 |
}
|
|
118 |
} catch (IOException e) {
|
|
119 |
e.printStackTrace();
|
|
120 |
}
|
|
121 |
if (numberMessages == 1) {
|
|
122 |
break;
|
|
123 |
}
|
|
124 |
}
|
|
125 |
System.out.printf("NumberMessages = %d numberPeeks = %d numberSleeps = %d\n", numberMessages, numberPeeks, numberSleeps);
|
|
126 |
if (numberMessages > 0) {
|
|
127 |
try {
|
|
128 |
|
|
129 |
ITCMessage message = stream.readMessage();
|
|
130 |
byte[] b = message.getMessage();
|
|
131 |
System.out.printf("msg: length=%d\n", b.length);
|
|
132 |
for (int i = 0; i < b.length; i++) {
|
|
133 |
System.out.printf("%x ", b[i]);
|
|
134 |
}
|
|
135 |
System.out.printf("\n");
|
|
136 |
numberMessages = stream.peekMessages();
|
|
137 |
} catch (IOException e) {
|
|
138 |
e.printStackTrace();
|
|
139 |
}
|
|
140 |
}
|
|
141 |
}
|
|
142 |
}
|
|
143 |
// send trk version
|
|
144 |
if (connStatus.isOK()) {
|
|
145 |
IStatus sendStatus = api.sendMessage(tcMsgVersion);
|
|
146 |
if (sendStatus.isOK()) {
|
|
147 |
// wait for response
|
|
148 |
int numberMessages = 0;
|
|
149 |
int numberPeeks = 0;
|
|
150 |
int numberSleeps = 0;
|
|
151 |
for (int i = 0; i < 100; i ++) {
|
|
152 |
try {
|
|
153 |
if (stream != null)
|
|
154 |
numberMessages = stream.peekMessages();
|
|
155 |
numberPeeks++;
|
|
156 |
if (numberMessages < 1) {
|
|
157 |
try {
|
|
158 |
Thread.sleep(100);
|
|
159 |
numberSleeps++;
|
|
160 |
} catch (InterruptedException e) {
|
|
161 |
e.printStackTrace();
|
|
162 |
}
|
|
163 |
}
|
|
164 |
} catch (IOException e) {
|
|
165 |
e.printStackTrace();
|
|
166 |
}
|
|
167 |
if (numberMessages == 1) {
|
|
168 |
break;
|
|
169 |
}
|
|
170 |
}
|
|
171 |
System.out.printf("NumberMessages = %d numberPeeks = %d numberSleeps = %d\n", numberMessages, numberPeeks, numberSleeps);
|
|
172 |
if (numberMessages > 0) {
|
|
173 |
try {
|
|
174 |
|
|
175 |
ITCMessage message = stream.readMessage();
|
|
176 |
byte[] b = message.getMessage();
|
|
177 |
System.out.printf("msg: length=%d\n", b.length);
|
|
178 |
for (int i = 0; i < b.length; i++) {
|
|
179 |
System.out.printf("%x ", b[i]);
|
|
180 |
}
|
|
181 |
System.out.printf("\n");
|
|
182 |
numberMessages = stream.peekMessages();
|
|
183 |
} catch (IOException e) {
|
|
184 |
e.printStackTrace();
|
|
185 |
}
|
|
186 |
}
|
|
187 |
}
|
|
188 |
}
|
|
189 |
|
|
190 |
if (connStatus.isOK()) {
|
|
191 |
api.disconnect();
|
|
192 |
}
|
|
193 |
}
|
|
194 |
// for non-plugin junit test
|
59
|
195 |
if (startServer) {
|
|
196 |
api2.nativeStopServer();
|
|
197 |
}
|
2
|
198 |
}
|
|
199 |
|
|
200 |
}
|