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 |
// GetTRKVersion.cpp : Defines the entry point for the DLL application.
|
|
18 |
//
|
|
19 |
|
|
20 |
#include "stdafx.h"
|
|
21 |
#include "GetTRKVersion.h"
|
|
22 |
#include "com_nokia_carbide_trk_support_onDeviceSetup_ui_CheckExistingTRKPage.h"
|
|
23 |
#include "com_nokia_carbide_trk_support_service_TRKConnectedService.h"
|
|
24 |
|
|
25 |
|
|
26 |
BOOL APIENTRY DllMain( HANDLE hModule,
|
|
27 |
DWORD ul_reason_for_call,
|
|
28 |
LPVOID lpReserved
|
|
29 |
)
|
|
30 |
{
|
|
31 |
switch (ul_reason_for_call)
|
|
32 |
{
|
|
33 |
case DLL_PROCESS_ATTACH:
|
|
34 |
case DLL_THREAD_ATTACH:
|
|
35 |
case DLL_THREAD_DETACH:
|
|
36 |
case DLL_PROCESS_DETACH:
|
|
37 |
break;
|
|
38 |
}
|
|
39 |
return TRUE;
|
|
40 |
}
|
|
41 |
|
|
42 |
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
|
|
43 |
|
|
44 |
static const DWORD kNoPingError = -1;
|
|
45 |
static const DWORD kNoVersionError = -2;
|
|
46 |
|
|
47 |
const DWORD kBaudRates[] = { 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400 };
|
|
48 |
enum { FlowHardware = 1, FlowSoftware = 2 };
|
|
49 |
|
|
50 |
static void SetDCB(DCB* ioDCB, int baudIndex, int dataBits, int parity, int stopBits, int flowControl)
|
|
51 |
{
|
|
52 |
ioDCB->DCBlength = sizeof( DCB );
|
|
53 |
|
|
54 |
ioDCB->BaudRate = kBaudRates[baudIndex];
|
|
55 |
|
|
56 |
ioDCB->ByteSize = dataBits+4;
|
|
57 |
ioDCB->Parity = parity;
|
|
58 |
ioDCB->StopBits = stopBits;
|
|
59 |
|
|
60 |
// setup hardware flow control
|
|
61 |
if (flowControl == FlowHardware)
|
|
62 |
ioDCB->fRtsControl = RTS_CONTROL_HANDSHAKE;
|
|
63 |
else
|
|
64 |
ioDCB->fRtsControl = RTS_CONTROL_DISABLE;
|
|
65 |
|
|
66 |
ioDCB->fOutxCtsFlow = (flowControl == FlowHardware);
|
|
67 |
ioDCB->fDtrControl = DTR_CONTROL_ENABLE;
|
|
68 |
ioDCB->fDsrSensitivity = false;
|
|
69 |
|
|
70 |
|
|
71 |
// setup software flow control
|
|
72 |
|
|
73 |
if (flowControl == FlowSoftware)
|
|
74 |
{
|
|
75 |
ioDCB->fInX = ioDCB->fOutX = 1;
|
|
76 |
ioDCB->XonChar = '\021'; // Ctrl-Q;
|
|
77 |
ioDCB->XoffChar = '\023'; // Ctrl-S;
|
|
78 |
ioDCB->XonLim = 100;
|
|
79 |
ioDCB->XoffLim = 100;
|
|
80 |
}
|
|
81 |
else
|
|
82 |
{
|
|
83 |
ioDCB->fInX = ioDCB->fOutX = 0;
|
|
84 |
}
|
|
85 |
|
|
86 |
// other various settings
|
|
87 |
|
|
88 |
ioDCB->fBinary = TRUE;
|
|
89 |
ioDCB->fParity = TRUE;
|
|
90 |
ioDCB->fNull = FALSE;
|
|
91 |
ioDCB->fAbortOnError = FALSE;
|
|
92 |
|
|
93 |
}
|
|
94 |
|
|
95 |
static DWORD OpenSerialPort(const char* inPortName, int baudIndex, int dataBits, int parity, int stopBits, int flowControl, HANDLE& serialPortHandle)
|
|
96 |
{
|
|
97 |
serialPortHandle = CreateFile(inPortName,
|
|
98 |
GENERIC_READ|GENERIC_WRITE,
|
|
99 |
0, // lock the port so no one else can get it
|
|
100 |
NULL, // no attributes
|
|
101 |
OPEN_EXISTING,
|
|
102 |
0,
|
|
103 |
NULL );
|
|
104 |
|
|
105 |
|
|
106 |
if (serialPortHandle == (HANDLE)-1)
|
|
107 |
{
|
|
108 |
return GetLastError();
|
|
109 |
}
|
|
110 |
|
|
111 |
if (!SetCommMask(serialPortHandle, EV_RXCHAR)) // WaitCommEvent notified by RX Events
|
|
112 |
{
|
|
113 |
CloseHandle(serialPortHandle);
|
|
114 |
return GetLastError();
|
|
115 |
}
|
|
116 |
|
|
117 |
if (!SetupComm(serialPortHandle, 4096, 4096)) // 4K Tx and Rx Buffers
|
|
118 |
{
|
|
119 |
CloseHandle(serialPortHandle);
|
|
120 |
return GetLastError();
|
|
121 |
}
|
|
122 |
|
|
123 |
// Get rid of any junk that might be sitting there.
|
|
124 |
PurgeComm(serialPortHandle, PURGE_TXABORT | PURGE_RXABORT |
|
|
125 |
PURGE_TXCLEAR | PURGE_RXCLEAR );
|
|
126 |
|
|
127 |
// Using these settings, the ReadFile command will return immediately
|
|
128 |
// rather than waiting for a timeout.
|
|
129 |
|
|
130 |
COMMTIMEOUTS lclCommTimeOuts;
|
|
131 |
lclCommTimeOuts.ReadIntervalTimeout = 0xFFFFFFFF;
|
|
132 |
lclCommTimeOuts.ReadTotalTimeoutMultiplier = 0;
|
|
133 |
lclCommTimeOuts.ReadTotalTimeoutConstant = 0;
|
|
134 |
lclCommTimeOuts.WriteTotalTimeoutMultiplier = 0;
|
|
135 |
lclCommTimeOuts.WriteTotalTimeoutConstant = 2001UL;
|
|
136 |
|
|
137 |
if (!SetCommTimeouts(serialPortHandle, &lclCommTimeOuts))
|
|
138 |
{
|
|
139 |
CloseHandle(serialPortHandle);
|
|
140 |
return GetLastError();
|
|
141 |
}
|
|
142 |
|
|
143 |
if (baudIndex >= 0)
|
|
144 |
{
|
|
145 |
DCB dcb;
|
|
146 |
|
|
147 |
if (!GetCommState(serialPortHandle, &dcb))
|
|
148 |
{
|
|
149 |
CloseHandle(serialPortHandle);
|
|
150 |
return GetLastError();
|
|
151 |
}
|
|
152 |
|
|
153 |
SetDCB(&dcb, baudIndex, dataBits, parity, stopBits, flowControl);
|
|
154 |
|
|
155 |
if (!SetCommState(serialPortHandle, &dcb))
|
|
156 |
{
|
|
157 |
CloseHandle(serialPortHandle);
|
|
158 |
return GetLastError();
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
return ERROR_SUCCESS;
|
|
163 |
}
|
|
164 |
|
|
165 |
static void Disconnect(HANDLE serialPortHandle)
|
|
166 |
{
|
|
167 |
// disable event notification
|
|
168 |
SetCommMask(serialPortHandle, 0);
|
|
169 |
|
|
170 |
// drop DTR
|
|
171 |
EscapeCommFunction(serialPortHandle, CLRDTR);
|
|
172 |
|
|
173 |
// purge any outstanding reads/writes and close device handle
|
|
174 |
PurgeComm(serialPortHandle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR);
|
|
175 |
|
|
176 |
CloseHandle(serialPortHandle);
|
|
177 |
}
|
|
178 |
|
|
179 |
// inData: Pointer to data buffer to send
|
|
180 |
// inSize: Size of data to send
|
|
181 |
static DWORD SendData(HANDLE serialPortHandle, const void* inData, unsigned long inSize)
|
|
182 |
{
|
|
183 |
DWORD lclNumBytes;
|
|
184 |
|
|
185 |
if (WriteFile(serialPortHandle, inData, inSize, &lclNumBytes, NULL))
|
|
186 |
return ERROR_SUCCESS;
|
|
187 |
|
|
188 |
return GetLastError();
|
|
189 |
}
|
|
190 |
|
|
191 |
// inSize: Size of data to read in bytes
|
|
192 |
// outData: Pointer to data buffer to read data into
|
|
193 |
// outSize: Size actually read in bytes
|
|
194 |
static DWORD ReadData(HANDLE serialPortHandle, unsigned long inSize, void* outData, unsigned long &outSize)
|
|
195 |
{
|
|
196 |
// Init our number of bytes read to zero
|
|
197 |
outSize = 0;
|
|
198 |
|
|
199 |
COMSTAT lclComStat;
|
|
200 |
DWORD lclErrorFlags;
|
|
201 |
DWORD lclLength;
|
|
202 |
|
|
203 |
// clear out any errors in the channel and get the length of the buffer
|
|
204 |
ClearCommError(serialPortHandle, &lclErrorFlags, &lclComStat);
|
|
205 |
lclLength = MIN(inSize, lclComStat.cbInQue );
|
|
206 |
|
|
207 |
if (lclLength > 0)
|
|
208 |
{
|
|
209 |
// Read lclLength number of bytes into outData.
|
|
210 |
if (!ReadFile(serialPortHandle, outData, lclLength, &outSize, NULL))
|
|
211 |
return GetLastError();
|
|
212 |
}
|
|
213 |
|
|
214 |
return ERROR_SUCCESS;
|
|
215 |
}
|
|
216 |
|
|
217 |
|
|
218 |
static DWORD ReceiveData(HANDLE serialPortHandle, unsigned long timeout, unsigned long inSize, void* outData, unsigned long &outSize)
|
|
219 |
{
|
|
220 |
const unsigned long kSleepMillis = 10;
|
|
221 |
|
|
222 |
DWORD error = 0;
|
|
223 |
|
|
224 |
int maxIters = timeout/kSleepMillis;
|
|
225 |
int i = 0;
|
|
226 |
for (; i < maxIters; i++)
|
|
227 |
{
|
|
228 |
error = ReadData(serialPortHandle, inSize, outData, outSize);
|
|
229 |
if (error != ERROR_SUCCESS)
|
|
230 |
return error;
|
|
231 |
|
|
232 |
if (outSize > 0)
|
|
233 |
break;
|
|
234 |
else
|
|
235 |
Sleep(kSleepMillis);
|
|
236 |
}
|
|
237 |
if (i == maxIters)
|
|
238 |
return kNoPingError;
|
|
239 |
|
|
240 |
return ERROR_SUCCESS;
|
|
241 |
}
|
|
242 |
|
|
243 |
|
|
244 |
__declspec(dllexport)
|
|
245 |
DWORD GetTRKVersion(const char* inPortName, int baudIndex, int dataBits, int parity, int stopBits, int flowControl, long version[3])
|
|
246 |
{
|
|
247 |
// open the serial port
|
|
248 |
HANDLE serialPortHandle = NULL;
|
|
249 |
DWORD error = OpenSerialPort(inPortName, baudIndex, dataBits, parity, stopBits, flowControl, serialPortHandle);
|
|
250 |
if (error != ERROR_SUCCESS)
|
|
251 |
return error;
|
|
252 |
|
|
253 |
// send a ping command
|
|
254 |
unsigned char pingTxBuf[] = { 0x7e, 0x00, 0x00, 0xff, 0x7e };
|
|
255 |
error = SendData(serialPortHandle, &pingTxBuf, 5);
|
|
256 |
if (error != ERROR_SUCCESS) {
|
|
257 |
Disconnect(serialPortHandle);
|
|
258 |
return error;
|
|
259 |
}
|
|
260 |
|
|
261 |
// receive response
|
|
262 |
unsigned char pingRxBuf[16];
|
|
263 |
unsigned long pingRxSize = 0;
|
|
264 |
error = ReceiveData(serialPortHandle, 2001, 16, pingRxBuf, pingRxSize);
|
|
265 |
if (error != ERROR_SUCCESS) {
|
|
266 |
Disconnect(serialPortHandle);
|
|
267 |
return error;
|
|
268 |
}
|
|
269 |
|
|
270 |
// send get version command
|
|
271 |
unsigned char versTxBuf[] = { 0x7e, 0x08, 0x01, 0xf6, 0x7e };
|
|
272 |
error = SendData(serialPortHandle, &versTxBuf, 5);
|
|
273 |
if (error != ERROR_SUCCESS) {
|
|
274 |
Disconnect(serialPortHandle);
|
|
275 |
return error;
|
|
276 |
}
|
|
277 |
|
|
278 |
// receive response
|
|
279 |
unsigned char versRxBuf[16];
|
|
280 |
unsigned long versRxSize = 0;
|
|
281 |
error = ReceiveData(serialPortHandle, 2001, 16, versRxBuf, versRxSize);
|
|
282 |
if (error != ERROR_SUCCESS) {
|
|
283 |
Disconnect(serialPortHandle);
|
|
284 |
if (error == kNoPingError)
|
|
285 |
return kNoVersionError; // ping ok, but no version
|
|
286 |
return error;
|
|
287 |
}
|
|
288 |
|
|
289 |
if (versRxSize >= 9)
|
|
290 |
{
|
|
291 |
version[0] = versRxBuf[4];
|
|
292 |
version[1] = versRxBuf[5];
|
|
293 |
version[2] = versRxBuf[8];
|
|
294 |
}
|
|
295 |
|
|
296 |
Disconnect(serialPortHandle);
|
|
297 |
return ERROR_SUCCESS;
|
|
298 |
}
|
|
299 |
|
|
300 |
static const char* GetErrorText(DWORD error)
|
|
301 |
{
|
|
302 |
if (error == kNoPingError)
|
|
303 |
return "TRK did not respond";
|
|
304 |
|
|
305 |
else if (error == kNoVersionError)
|
|
306 |
return "TRK responded to PING, but not to GETVERSION command";
|
|
307 |
|
|
308 |
else if (error == ERROR_FILE_NOT_FOUND)
|
|
309 |
return "Could not open the serial port";
|
|
310 |
|
|
311 |
static char msg[256];
|
|
312 |
FormatMessage(
|
|
313 |
FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
|
|
314 |
NULL,
|
|
315 |
error,
|
|
316 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
|
|
317 |
(LPTSTR) &msg,
|
|
318 |
sizeof(msg) - 1,
|
|
319 |
NULL);
|
|
320 |
|
|
321 |
return msg;
|
|
322 |
}
|
|
323 |
|
|
324 |
|
|
325 |
/*
|
|
326 |
* Class: com_nokia_carbide_trk_support_onDeviceSetup_ui_CheckExistingTRKPage
|
|
327 |
* Method: getTRKVersion
|
|
328 |
* Signature: (Ljava/lang/String;[I)V
|
|
329 |
*/
|
|
330 |
JNIEXPORT void JNICALL Java_com_nokia_carbide_trk_support_onDeviceSetup_ui_CheckExistingTRKPage_getTRKVersion
|
|
331 |
(JNIEnv* env, jclass, jstring jPortName, jintArray jVersionInts)
|
|
332 |
{
|
|
333 |
const char* portName = env->GetStringUTFChars(jPortName, NULL);
|
|
334 |
|
|
335 |
jint versionInts[3];
|
|
336 |
DWORD error = GetTRKVersion(portName, -1, 0, 0, 0, 0, versionInts);
|
|
337 |
env->SetIntArrayRegion(jVersionInts, 0, 3, versionInts);
|
|
338 |
|
|
339 |
env->ReleaseStringUTFChars(jPortName, portName);
|
|
340 |
|
|
341 |
if (error > ERROR_SUCCESS)
|
|
342 |
{
|
|
343 |
|
|
344 |
jclass clazz = env->FindClass("Ljava/lang/Exception;");
|
|
345 |
env->ThrowNew(clazz, GetErrorText(error));
|
|
346 |
}
|
|
347 |
|
|
348 |
}
|
|
349 |
|
|
350 |
/*
|
|
351 |
* Class: com_nokia_carbide_trk_support_service_TRKConnectedService
|
|
352 |
* Method: getTRKVersionFromSerial
|
|
353 |
* Signature: (Ljava/lang/String;IIIII[I)V
|
|
354 |
*/
|
|
355 |
JNIEXPORT void JNICALL Java_com_nokia_carbide_trk_support_service_TRKConnectedService_getTRKVersionFromSerial
|
|
356 |
(JNIEnv* env, jclass, jstring jPortName, jint jBaud, jint jDataBits, jint jParity, jint jStopBits, jint jFlowControl, jintArray jVersionInts)
|
|
357 |
{
|
|
358 |
const char* portName = env->GetStringUTFChars(jPortName, NULL);
|
|
359 |
|
|
360 |
jint versionInts[3];
|
|
361 |
DWORD error = GetTRKVersion(portName, jBaud, jDataBits, jParity, jStopBits, jFlowControl, versionInts);
|
|
362 |
env->SetIntArrayRegion(jVersionInts, 0, 3, versionInts);
|
|
363 |
|
|
364 |
env->ReleaseStringUTFChars(jPortName, portName);
|
|
365 |
|
|
366 |
if (error > ERROR_SUCCESS)
|
|
367 |
{
|
|
368 |
|
|
369 |
jclass clazz = env->FindClass("Ljava/lang/Exception;");
|
|
370 |
env->ThrowNew(clazz, GetErrorText(error));
|
|
371 |
}
|
|
372 |
}
|
|
373 |
|
|
374 |
|