| 1 |
| RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. |
| 2 |
| RXTX is a native interface to serial ports in java. |
| 3 |
| Copyright 1997-2007 by Trent Jarvi [email protected] and others who |
| 4 |
| Copyright 2002-2004 Michal Hobot [email protected] |
| 5 |
/*------------------------------------------------------------------------- |
| 6 |
| actually wrote it. See individual source files for more information. |
| 7 |
| |
| 8 |
| A copy of the LGPL v 2.1 may be found at |
| 9 |
| http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is |
| 10 |
| here for your convenience. |
| 11 |
| |
| 12 |
| This library is free software; you can redistribute it and/or |
| 13 |
| modify it under the terms of the GNU Lesser General Public |
| 14 |
| License as published by the Free Software Foundation; either |
| 15 |
| version 2.1 of the License, or (at your option) any later version. |
| 16 |
| |
| 17 |
| This library is distributed in the hope that it will be useful, |
| 18 |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
| Lesser General Public License for more details. |
| 21 |
| |
| 22 |
| An executable that contains no derivative of any portion of RXTX, but |
| 23 |
| is designed to work with RXTX by being dynamically linked with it, |
| 24 |
| is considered a "work that uses the Library" subject to the terms and |
| 25 |
| conditions of the GNU Lesser General Public License. |
| 26 |
| |
| 27 |
| The following has been added to the RXTX License to remove |
| 28 |
| any confusion about linking to RXTX. We want to allow in part what |
| 29 |
| section 5, paragraph 2 of the LGPL does not permit in the special |
| 30 |
| case of linking over a controlled interface. The intent is to add a |
| 31 |
| Java Specification Request or standards body defined interface in the |
| 32 |
| future as another exception but one is not currently available. |
| 33 |
| |
| 34 |
| http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface |
| 35 |
| |
| 36 |
| As a special exception, the copyright holders of RXTX give you |
| 37 |
| permission to link RXTX with independent modules that communicate with |
| 38 |
| RXTX solely through the Sun Microsytems CommAPI interface version 2, |
| 39 |
| regardless of the license terms of these independent modules, and to copy |
| 40 |
| and distribute the resulting combined work under terms of your choice, |
| 41 |
| provided that every copy of the combined work is accompanied by a complete |
| 42 |
| copy of the source code of RXTX (the version of RXTX used to produce the |
| 43 |
| combined work), being distributed under the terms of the GNU Lesser General |
| 44 |
| Public License plus this exception. An independent module is a |
| 45 |
| module which is not derived from or based on RXTX. |
| 46 |
| |
| 47 |
| Note that people who make modified versions of RXTX are not obligated |
| 48 |
| to grant this special exception for their modified versions; it is |
| 49 |
| their choice whether to do so. The GNU Lesser General Public License |
| 50 |
| gives permission to release a modified version without this exception; this |
| 51 |
| exception also makes it possible to release a modified version which |
| 52 |
| carries forward this exception. |
| 53 |
| |
| 54 |
| You should have received a copy of the GNU Lesser General Public |
| 55 |
| License along with this library; if not, write to the Free |
| 56 |
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 57 |
| All trademarks belong to their respective owners. |
| 58 |
--------------------------------------------------------------------------*/ |
| 59 |
#include "StdAfx.h" |
| 60 |
#include <stdio.h> |
| 61 |
#include <stdlib.h> |
| 62 |
#include "rxtxHelpers.h" |
| 63 |
|
| 64 |
/*---------------------------------------------------------- |
| 65 |
get_java_int_var |
| 66 |
|
| 67 |
accept: env (keyhole to java) |
| 68 |
jobj (java RXTXPort object) |
| 69 |
return: the jint field from the java object, casted to long |
| 70 |
exceptions: none |
| 71 |
comments: |
| 72 |
----------------------------------------------------------*/ |
| 73 |
long get_java_int_var( JNIEnv *env, jobject jobj, char *id) |
| 74 |
{ |
| 75 |
long result = 0; |
| 76 |
jclass jclazz = env->GetObjectClass(jobj); |
| 77 |
jfieldID jfd = env->GetFieldID(jclazz, id, "I"); |
| 78 |
|
| 79 |
if( !jfd ) { |
| 80 |
IF_DEBUG |
| 81 |
( |
| 82 |
env->ExceptionDescribe(); |
| 83 |
) |
| 84 |
env->ExceptionClear(); |
| 85 |
env->DeleteLocalRef(jclazz); |
| 86 |
return result; |
| 87 |
} |
| 88 |
result = (long)( env->GetIntField(jobj, jfd) ); |
| 89 |
env->DeleteLocalRef(jclazz); |
| 90 |
return result; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
/*---------------------------------------------------------- |
| 95 |
get_java_boolean_var |
| 96 |
|
| 97 |
accept: env (keyhole to java) |
| 98 |
jobj (java RXTXPort object) |
| 99 |
return: the jboolean field from the java object, converted to bool |
| 100 |
exceptions: none |
| 101 |
comments: |
| 102 |
----------------------------------------------------------*/ |
| 103 |
bool get_java_boolean_var( JNIEnv *env, jobject jobj, char *id) |
| 104 |
{ |
| 105 |
bool result = FALSE; |
| 106 |
jclass jclazz = env->GetObjectClass(jobj); |
| 107 |
jfieldID jfd = env->GetFieldID(jclazz, id, "Z"); |
| 108 |
|
| 109 |
if( !jfd ) { |
| 110 |
IF_DEBUG |
| 111 |
( |
| 112 |
env->ExceptionDescribe(); |
| 113 |
) |
| 114 |
env->ExceptionClear(); |
| 115 |
env->DeleteLocalRef(jclazz); |
| 116 |
return result; |
| 117 |
} |
| 118 |
result = (env->GetBooleanField(jobj, jfd)) != JNI_FALSE; |
| 119 |
env->DeleteLocalRef(jclazz); |
| 120 |
return result; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
/*---------------------------------------------------------- |
| 125 |
get_java_boolean_var2 |
| 126 |
|
| 127 |
accept: env (keyhole to java) |
| 128 |
jobj (java RXTXPort object) |
| 129 |
jclazz (class of jobj) |
| 130 |
return: the jboolean field from the java object, converted to bool |
| 131 |
exceptions: none |
| 132 |
comments: can be faster for fetching many variables one by one |
| 133 |
----------------------------------------------------------*/ |
| 134 |
bool get_java_boolean_var2(JNIEnv *env, jobject jobj, jclass jclazz, char *id) |
| 135 |
{ |
| 136 |
bool result = FALSE; |
| 137 |
jfieldID jfd = env->GetFieldID(jclazz, id, "Z"); |
| 138 |
|
| 139 |
if( !jfd ) { |
| 140 |
IF_DEBUG |
| 141 |
( |
| 142 |
env->ExceptionDescribe(); |
| 143 |
) |
| 144 |
env->ExceptionClear(); |
| 145 |
env->DeleteLocalRef(jclazz); |
| 146 |
return result; |
| 147 |
} |
| 148 |
result = (env->GetBooleanField(jobj, jfd)) != JNI_FALSE; |
| 149 |
return result; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
/*---------------------------------------------------------- |
| 154 |
throw_java_exception |
| 155 |
|
| 156 |
accept: env (keyhole to java) |
| 157 |
*exc (exception class name) |
| 158 |
*foo (function name) |
| 159 |
*msg (error message) |
| 160 |
perform: Throw a new java exception |
| 161 |
return: none |
| 162 |
exceptions: haha! |
| 163 |
comments: |
| 164 |
----------------------------------------------------------*/ |
| 165 |
void throw_java_exception(JNIEnv *env, const char *exc, const char *foo, const char *msg) |
| 166 |
{ |
| 167 |
char buf[250]; |
| 168 |
jclass clazz = env->FindClass(exc); |
| 169 |
if( !clazz ) { |
| 170 |
IF_DEBUG |
| 171 |
( |
| 172 |
env->ExceptionDescribe(); |
| 173 |
) |
| 174 |
env->ExceptionClear(); |
| 175 |
return; |
| 176 |
} |
| 177 |
_snprintf(buf, 60, "%s: %s in %s", exc, msg, foo); |
| 178 |
env->ThrowNew(clazz, buf); |
| 179 |
env->DeleteLocalRef(clazz); |
| 180 |
} |
| 181 |
|
| 182 |
// Unicode version: |
| 183 |
void throw_java_exceptionW(JNIEnv *env, const char *exc, const wchar_t *foo, const wchar_t *msg) |
| 184 |
{ |
| 185 |
wchar_t buf[500]; |
| 186 |
char *lpcBuf; |
| 187 |
int msgLen; |
| 188 |
|
| 189 |
jclass clazz = env->FindClass(exc); |
| 190 |
if( !clazz ) { |
| 191 |
env->ExceptionDescribe(); |
| 192 |
env->ExceptionClear(); |
| 193 |
return; |
| 194 |
} |
| 195 |
msgLen = swprintf(buf, L"%S: %s in %s", exc, msg, foo); |
| 196 |
|
| 197 |
IF_DEBUG |
| 198 |
( |
| 199 |
printj(env, L"!!!!! Throwing %s\n", buf); |
| 200 |
//MessageBox(NULL, buf, L"throw_java_exceptionW"), MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND); |
| 201 |
) |
| 202 |
|
| 203 |
lpcBuf = (char *)malloc((msgLen+1)*sizeof(wchar_t)); // Will be ok - mbs not longer than wcs |
| 204 |
lpcBuf[msgLen] = '\0'; |
| 205 |
wcstombs(lpcBuf, buf, msgLen); |
| 206 |
env->ThrowNew(clazz, lpcBuf); |
| 207 |
free(lpcBuf); |
| 208 |
env->DeleteLocalRef(clazz); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
/*---------------------------------------------------------- |
| 213 |
get_fd |
| 214 |
|
| 215 |
accept: env (keyhole to java) |
| 216 |
jobj (java RXTXPort object) |
| 217 |
return: "fd" field from the java object, as HANDLE |
| 218 |
exceptions: none |
| 219 |
comments: |
| 220 |
----------------------------------------------------------*/ |
| 221 |
HANDLE get_fd(JNIEnv *env, jobject jobj) |
| 222 |
{ |
| 223 |
HANDLE fd = (HANDLE)get_java_int_var(env, jobj, "fd"); |
| 224 |
return fd!=0?fd:INVALID_HANDLE_VALUE; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
/*---------------------------------------------------------- |
| 229 |
get_eis |
| 230 |
|
| 231 |
accept: env (keyhole to java) |
| 232 |
jobj (java RXTXPort object) |
| 233 |
return: "eis" field from the java object, as (EventInfoStruct *) |
| 234 |
exceptions: none |
| 235 |
comments: |
| 236 |
----------------------------------------------------------*/ |
| 237 |
EventInfoStruct *get_eis(JNIEnv *env, jobject jobj) |
| 238 |
{ |
| 239 |
return (EventInfoStruct *)get_java_int_var(env, jobj, "eis"); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
/*---------------------------------------------------------- |
| 244 |
printj |
| 245 |
|
| 246 |
accept: like vwprintf() |
| 247 |
return: number of jchars written or -1 |
| 248 |
exceptions: none |
| 249 |
comments: prints data using System.out.print() |
| 250 |
----------------------------------------------------------*/ |
| 251 |
int printj(JNIEnv *env, wchar_t *fmt, ...) |
| 252 |
{ |
| 253 |
wchar_t buf[1024]; |
| 254 |
int retval; |
| 255 |
jstring jsBuf; |
| 256 |
jclass clsSystem, clsOut; |
| 257 |
jfieldID jfid; |
| 258 |
jobject objOut; |
| 259 |
jmethodID midPrint; |
| 260 |
|
| 261 |
va_list ap; |
| 262 |
va_start(ap, fmt); |
| 263 |
retval = _vsnwprintf(buf, 1024, fmt, ap); |
| 264 |
va_end(ap); |
| 265 |
buf[1023] = '\0'; |
| 266 |
|
| 267 |
if((clsSystem = env->FindClass("java/lang/System")) == NULL) |
| 268 |
{ |
| 269 |
IF_DEBUG |
| 270 |
( |
| 271 |
env->ExceptionDescribe(); |
| 272 |
) |
| 273 |
env->ExceptionClear(); |
| 274 |
return -1; |
| 275 |
} |
| 276 |
|
| 277 |
if((jfid = env->GetStaticFieldID(clsSystem, "out", "Ljava/io/PrintStream;")) == NULL) |
| 278 |
{ |
| 279 |
IF_DEBUG |
| 280 |
( |
| 281 |
env->ExceptionDescribe(); |
| 282 |
) |
| 283 |
env->ExceptionClear(); |
| 284 |
env->DeleteLocalRef(clsSystem); |
| 285 |
return -1; |
| 286 |
} |
| 287 |
|
| 288 |
objOut = env->GetStaticObjectField(clsSystem, jfid); |
| 289 |
clsOut = env->GetObjectClass(objOut); |
| 290 |
|
| 291 |
if((midPrint = env->GetMethodID(clsOut, "print", "(Ljava/lang/String;)V")) == NULL) |
| 292 |
{ |
| 293 |
IF_DEBUG |
| 294 |
( |
| 295 |
env->ExceptionDescribe(); |
| 296 |
) |
| 297 |
env->ExceptionClear(); |
| 298 |
env->DeleteLocalRef(clsOut); |
| 299 |
env->DeleteLocalRef(clsSystem); |
| 300 |
return -1; |
| 301 |
} |
| 302 |
|
| 303 |
jsBuf = env->NewString(buf, wcslen(buf)); |
| 304 |
|
| 305 |
env->CallVoidMethod(objOut, midPrint, jsBuf); |
| 306 |
|
| 307 |
env->DeleteLocalRef(jsBuf); |
| 308 |
env->DeleteLocalRef(clsOut); |
| 309 |
env->DeleteLocalRef(clsSystem); |
| 310 |
|
| 311 |
return retval; |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
/*---------------------------------------------------------- |
| 317 |
CommEventThread |
| 318 |
|
| 319 |
accept: communication structure ptr |
| 320 |
return: 0 - normal finish, other - error |
| 321 |
exceptions: none |
| 322 |
comments: runs as separate thread |
| 323 |
----------------------------------------------------------*/ |
| 324 |
DWORD __stdcall CommEventThread(LPVOID lpEventInfo) |
| 325 |
{ |
| 326 |
DWORD dwErr; |
| 327 |
EventInfoStruct *EventInfo = (EventInfoStruct *)lpEventInfo; |
| 328 |
HANDLE hPort = EventInfo->fd; |
| 329 |
|
| 330 |
// Specify a set of events to be monitored for the port. |
| 331 |
if(!SetCommMask(hPort, EventInfo->ef)) |
| 332 |
{ |
| 333 |
dwErr = GetLastError(); |
| 334 |
IF_DEBUG |
| 335 |
( |
| 336 |
LPCWSTR lpMsgBuf; |
| 337 |
CreateErrorMsg(dwErr, lpMsgBuf); |
| 338 |
MessageBoxW(NULL, lpMsgBuf, L"!!! CommEventThread - SetCommMask() error", MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND); |
| 339 |
ReleaseErrorMsg(lpMsgBuf); |
| 340 |
) |
| 341 |
return dwErr; |
| 342 |
} |
| 343 |
|
| 344 |
// Thread is ready to work |
| 345 |
EventInfo->eventThreadReady = true; |
| 346 |
|
| 347 |
do |
| 348 |
{ |
| 349 |
// Wait for an event to occur for the port. |
| 350 |
if(!WaitCommEvent(hPort, &(EventInfo->event), NULL)) |
| 351 |
{ |
| 352 |
dwErr = GetLastError(); |
| 353 |
if(dwErr == ERROR_INVALID_PARAMETER) |
| 354 |
{ // Mask is empty - let's wait for a moment and continue |
| 355 |
MessageBeep(MB_ICONQUESTION); |
| 356 |
Sleep(200); |
| 357 |
continue; |
| 358 |
} |
| 359 |
|
| 360 |
if(dwErr == ERROR_INVALID_HANDLE) |
| 361 |
{ // Port was closed |
| 362 |
//MessageBox(NULL, L"--- CommEventThread - Port closed", L"ERROR_INVALID_HANDLE", MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND); |
| 363 |
free(EventInfo); |
| 364 |
return 0; |
| 365 |
} |
| 366 |
|
| 367 |
IF_DEBUG |
| 368 |
( |
| 369 |
LPCWSTR lpMsgBuf; |
| 370 |
WCHAR MsgBuf2[1000]; |
| 371 |
CreateErrorMsg(dwErr, lpMsgBuf); |
| 372 |
wsprintfW(MsgBuf2, L"%ld %s, CommMask==%ld", dwErr, lpMsgBuf, EventInfo->ef); |
| 373 |
MessageBoxW(NULL, MsgBuf2, L"!!! CommEventThread - WaitCommEvent() error", MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND); |
| 374 |
ReleaseErrorMsg(lpMsgBuf); |
| 375 |
) |
| 376 |
return dwErr; |
| 377 |
} |
| 378 |
|
| 379 |
IF_DEBUG |
| 380 |
( |
| 381 |
MessageBeep(MB_ICONEXCLAMATION); |
| 382 |
//MessageBoxW(NULL, L"--- RXTXPort.CommEventThread() - CommEvent", L"CommEventThread", MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND); |
| 383 |
) |
| 384 |
|
| 385 |
// Re-specify the set of events to be monitored for the port. |
| 386 |
if(!SetCommMask(hPort, EventInfo->ef)) |
| 387 |
{ |
| 388 |
dwErr = GetLastError(); |
| 389 |
IF_DEBUG |
| 390 |
( |
| 391 |
LPCWSTR lpMsgBuf; |
| 392 |
CreateErrorMsg(dwErr, lpMsgBuf); |
| 393 |
MessageBoxW(NULL, lpMsgBuf, L"!!! CommEventThread - SetCommMask() error", MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND); |
| 394 |
ReleaseErrorMsg(lpMsgBuf); |
| 395 |
) |
| 396 |
return dwErr; |
| 397 |
} |
| 398 |
|
| 399 |
if(!SetEvent(EventInfo->eventHandle)) |
| 400 |
{ |
| 401 |
dwErr = GetLastError(); |
| 402 |
if(dwErr == ERROR_INVALID_HANDLE) // Event was closed |
| 403 |
{ |
| 404 |
//MessageBoxW(NULL, L"--- CommEventThread - Event closed", L"ERROR_INVALID_HANDLE", MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND); |
| 405 |
free(EventInfo); |
| 406 |
return 0; |
| 407 |
} |
| 408 |
|
| 409 |
IF_DEBUG |
| 410 |
( |
| 411 |
LPCWSTR lpMsgBuf; |
| 412 |
CreateErrorMsg(dwErr, lpMsgBuf); |
| 413 |
MessageBoxW(NULL, lpMsgBuf, L"!!! CommEventThread - SetEvent() error", MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND); |
| 414 |
ReleaseErrorMsg(lpMsgBuf); |
| 415 |
) |
| 416 |
return dwErr; |
| 417 |
} |
| 418 |
|
| 419 |
// Wait for receiving and clearing event |
| 420 |
while(EventInfo->event != 0) |
| 421 |
Sleep(0); |
| 422 |
|
| 423 |
} while(TRUE); |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
/*---------------------------------------------------------- |
| 428 |
setEventFlags |
| 429 |
|
| 430 |
accept: EventFlag table |
| 431 |
return: none |
| 432 |
exceptions: none |
| 433 |
comments: sets EventFlag table according to Java |
| 434 |
MonitorThread fields |
| 435 |
----------------------------------------------------------*/ |
| 436 |
/*void setEventFlags(JNIEnv *env, jobject jobjPort, bool ef[]) |
| 437 |
{ |
| 438 |
jfieldID jfid; |
| 439 |
jclass cls; |
| 440 |
jobject jobj; |
| 441 |
jclass clsPort = env->GetObjectClass(jobjPort); |
| 442 |
|
| 443 |
if((jfid = env->GetFieldID(clsPort, "monThread", "Lgnu/io/RXTXPort$MonitorThread;")) == NULL) |
| 444 |
{ |
| 445 |
IF_DEBUG |
| 446 |
( |
| 447 |
env->ExceptionDescribe(); |
| 448 |
) |
| 449 |
env->ExceptionClear(); |
| 450 |
env->DeleteLocalRef(clsPort); |
| 451 |
return; |
| 452 |
} |
| 453 |
jobj = env->GetObjectField(jobjPort, jfid); |
| 454 |
cls = env->GetObjectClass(jobj); |
| 455 |
|
| 456 |
ef[SPE_DATA_AVAILABLE] = get_java_boolean_var2(env, jobj, cls, "Data"); |
| 457 |
ef[SPE_OUTPUT_BUFFER_EMPTY] = get_java_boolean_var2(env, jobj, cls, "Output"); |
| 458 |
ef[SPE_CTS] = get_java_boolean_var2(env, jobj, cls, "CTS"); |
| 459 |
ef[SPE_DSR] = get_java_boolean_var2(env, jobj, cls, "DSR"); |
| 460 |
ef[SPE_RI] = get_java_boolean_var2(env, jobj, cls, "RI"); |
| 461 |
ef[SPE_CD] = get_java_boolean_var2(env, jobj, cls, "CD"); |
| 462 |
ef[SPE_OE] = get_java_boolean_var2(env, jobj, cls, "OE"); |
| 463 |
ef[SPE_PE] = get_java_boolean_var2(env, jobj, cls, "PE"); |
| 464 |
ef[SPE_FE] = get_java_boolean_var2(env, jobj, cls, "FE"); |
| 465 |
ef[SPE_BI] = get_java_boolean_var2(env, jobj, cls, "BI"); |
| 466 |
|
| 467 |
env->DeleteLocalRef(clsPort); |
| 468 |
env->DeleteLocalRef(cls); |
| 469 |
env->DeleteLocalRef(jobj); |
| 470 |
} |
| 471 |
*/ |
| 472 |
|
| 473 |
/*---------------------------------------------------------- |
| 474 |
InitialiseEventInfoStruct |
| 475 |
|
| 476 |
accept: Port handle |
| 477 |
return: 0 - OK, other - error number |
| 478 |
exceptions: none |
| 479 |
comments: Structure for communication with thread |
| 480 |
----------------------------------------------------------*/ |
| 481 |
int InitialiseEventInfoStruct(HANDLE hPort, EventInfoStruct **EventInfoPtr) |
| 482 |
{ |
| 483 |
DWORD dwErr; |
| 484 |
WCHAR wsEventName[MAX_PATH]; |
| 485 |
|
| 486 |
(*EventInfoPtr) = (EventInfoStruct *)malloc(sizeof(EventInfoStruct)); |
| 487 |
if(*EventInfoPtr == NULL) |
| 488 |
return ERROR_NOT_ENOUGH_MEMORY; |
| 489 |
|
| 490 |
memset(*EventInfoPtr, 0, sizeof(EventInfoStruct)); |
| 491 |
(*EventInfoPtr)->fd = hPort; |
| 492 |
wsprintfW(wsEventName, L"rxtxPort.SerEvt%lx", hPort); |
| 493 |
(*EventInfoPtr)->eventHandle = CreateEventW(NULL, FALSE, FALSE, wsEventName); |
| 494 |
dwErr = GetLastError(); |
| 495 |
if((*EventInfoPtr)->eventHandle == NULL || dwErr == ERROR_ALREADY_EXISTS) |
| 496 |
{ |
| 497 |
IF_DEBUG |
| 498 |
( |
| 499 |
LPCWSTR lpMsgBuf; |
| 500 |
CreateErrorMsg(dwErr, lpMsgBuf); |
| 501 |
MessageBoxW(NULL, lpMsgBuf, L"!!! InitialiseEventInfoStruct - CreateEvent() error", MB_OK | MB_ICONEXCLAMATION | MB_SETFOREGROUND); |
| 502 |
ReleaseErrorMsg(lpMsgBuf); |
| 503 |
) |
| 504 |
free(*EventInfoPtr); |
| 505 |
return dwErr; |
| 506 |
} |
| 507 |
|
| 508 |
// Set initial event flags: It should be not empty, but unused here and relatively rare event |
| 509 |
(*EventInfoPtr)->ef = EV_POWER; //In WinNT you can use EV_EVENT2 instead |
| 510 |
return 0; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
/*---------------------------------------------------------- |
| 515 |
SendEvents |
| 516 |
|
| 517 |
accept: |
| 518 |
return: |
| 519 |
exceptions: none |
| 520 |
comments: Sends events needed |
| 521 |
----------------------------------------------------------*/ |
| 522 |
int SendEvents(JNIEnv *env, jobject jobj, DWORD dwEvent, EventInfoStruct *EventInfo, jmethodID jmSendEvent) |
| 523 |
{ |
| 524 |
DWORD dwErrors; |
| 525 |
COMSTAT Stat; |
| 526 |
|
| 527 |
IF_DEBUG |
| 528 |
( |
| 529 |
printj(env, L"--- SendEvents(): event %#lx\n", dwEvent); |
| 530 |
) |
| 531 |
|
| 532 |
if(!ClearCommError(EventInfo->fd, &dwErrors, &Stat)) |
| 533 |
{ |
| 534 |
IF_DEBUG |
| 535 |
( |
| 536 |
LPCWSTR lpMsgBuf; |
| 537 |
CreateErrorMsg(GetLastError(), lpMsgBuf); |
| 538 |
printj(env, L"!!! SendEvents - ClearCommError() error: %s\n", lpMsgBuf); |
| 539 |
ReleaseErrorMsg(lpMsgBuf); |
| 540 |
) |
| 541 |
return -1; |
| 542 |
} |
| 543 |
|
| 544 |
// Ignore not subscribed events |
| 545 |
dwEvent &= EventInfo->ef; |
| 546 |
|
| 547 |
if(dwEvent & EV_RXCHAR) |
| 548 |
{ |
| 549 |
IF_DEBUG |
| 550 |
( |
| 551 |
printj(env, L"--- SendEvents(): EV_RXCHAR\n"); |
| 552 |
) |
| 553 |
if(env->CallBooleanMethod(jobj, jmSendEvent, SPE_DATA_AVAILABLE, JNI_TRUE) == JNI_TRUE) |
| 554 |
return SPE_DATA_AVAILABLE; |
| 555 |
} |
| 556 |
|
| 557 |
if(dwEvent & EV_TXEMPTY) |
| 558 |
{ |
| 559 |
IF_DEBUG |
| 560 |
( |
| 561 |
printj(env, L"--- SendEvents(): EV_TXEMPTY\n"); |
| 562 |
) |
| 563 |
if(env->CallBooleanMethod(jobj, jmSendEvent, SPE_OUTPUT_BUFFER_EMPTY, JNI_TRUE) == JNI_TRUE) |
| 564 |
return SPE_OUTPUT_BUFFER_EMPTY; |
| 565 |
} |
| 566 |
|
| 567 |
if(dwEvent & EV_CTS) |
| 568 |
{ |
| 569 |
IF_DEBUG |
| 570 |
( |
| 571 |
printj(env, L"--- SendEvents(): EV_CTS\n"); |
| 572 |
) |
| 573 |
if(env->CallBooleanMethod(jobj, jmSendEvent, SPE_CTS, Stat.fCtsHold?JNI_TRUE:JNI_TRUE) == JNI_TRUE) |
| 574 |
return SPE_CTS; |
| 575 |
} |
| 576 |
|
| 577 |
if(dwEvent & EV_DSR) |
| 578 |
{ |
| 579 |
IF_DEBUG |
| 580 |
( |
| 581 |
printj(env, L"--- SendEvents(): EV_DSR\n"); |
| 582 |
) |
| 583 |
if(env->CallBooleanMethod(jobj, jmSendEvent, SPE_DSR, Stat.fDsrHold?JNI_TRUE:JNI_TRUE) == JNI_TRUE) |
| 584 |
return SPE_DSR; |
| 585 |
} |
| 586 |
|
| 587 |
if(dwEvent & EV_RING) |
| 588 |
{ |
| 589 |
IF_DEBUG |
| 590 |
( |
| 591 |
printj(env, L"--- SendEvents(): EV_RING\n"); |
| 592 |
) |
| 593 |
if(env->CallBooleanMethod(jobj, jmSendEvent, SPE_RI, JNI_TRUE) == JNI_TRUE) |
| 594 |
return SPE_RI; |
| 595 |
} |
| 596 |
|
| 597 |
if(dwEvent & EV_RLSD) |
| 598 |
{ |
| 599 |
IF_DEBUG |
| 600 |
( |
| 601 |
printj(env, L"--- SendEvents(): EV_RLSD\n"); |
| 602 |
) |
| 603 |
if(env->CallBooleanMethod(jobj, jmSendEvent, SPE_CD, Stat.fRlsdHold?JNI_TRUE:JNI_TRUE) == JNI_TRUE) |
| 604 |
return SPE_CD; |
| 605 |
} |
| 606 |
|
| 607 |
if(dwEvent & EV_ERR) |
| 608 |
{ |
| 609 |
if(dwErrors & CE_OVERRUN) |
| 610 |
{ |
| 611 |
IF_DEBUG |
| 612 |
( |
| 613 |
printj(env, L"--- SendEvents(): CE_OVERRUN\n"); |
| 614 |
) |
| 615 |
if(env->CallBooleanMethod(jobj, jmSendEvent, SPE_OE, JNI_TRUE) == JNI_TRUE) |
| 616 |
return SPE_OE; |
| 617 |
} |
| 618 |
|
| 619 |
if(dwErrors & CE_RXPARITY) |
| 620 |
{ |
| 621 |
IF_DEBUG |
| 622 |
( |
| 623 |
printj(env, L"--- SendEvents(): CE_RXPARITY\n"); |
| 624 |
) |
| 625 |
if(env->CallBooleanMethod(jobj, jmSendEvent, SPE_PE, JNI_TRUE) == JNI_TRUE) |
| 626 |
return SPE_PE; |
| 627 |
} |
| 628 |
|
| 629 |
if(dwErrors & CE_FRAME) |
| 630 |
{ |
| 631 |
IF_DEBUG |
| 632 |
( |
| 633 |
printj(env, L"--- SendEvents(): CE_FRAME\n"); |
| 634 |
) |
| 635 |
if(env->CallBooleanMethod(jobj, jmSendEvent, SPE_FE, JNI_TRUE) == JNI_TRUE) |
| 636 |
return SPE_FE; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
if(dwEvent & EV_BREAK) |
| 641 |
{ |
| 642 |
IF_DEBUG |
| 643 |
( |
| 644 |
printj(env, L"--- SendEvents(): EV_BREAK\n"); |
| 645 |
) |
| 646 |
if(env->CallBooleanMethod(jobj, jmSendEvent, SPE_BI, JNI_TRUE) == JNI_TRUE) |
| 647 |
return SPE_BI; |
| 648 |
} |
| 649 |
|
| 650 |
return 0; |
| 651 |
} |
| 652 |
|