| 1 |
/*------------------------------------------------------------------------- |
| 2 |
| RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface. |
| 3 |
| RXTX is a native interface to serial ports in java. |
| 4 |
| Copyright 2002-2004 Michal Hobot [email protected] |
| 5 |
| Copyright 1997-2007 by Trent Jarvi [email protected] and others who |
| 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 "rxtxHelpers.h" |
| 61 |
|
| 62 |
/* |
| 63 |
nativeGetVersion |
| 64 |
|
| 65 |
accept: none |
| 66 |
perform: return the current version |
| 67 |
return: version |
| 68 |
exceptions: none |
| 69 |
comments: This is used to avoid mixing versions of the .jar and |
| 70 |
native library. |
| 71 |
First introduced in rxtx-1.5-9 |
| 72 |
* Class: gnu_io_RXTXCommDriver |
| 73 |
* Method: nativeGetVersion |
| 74 |
* Signature: ()Ljava/lang/String; |
| 75 |
*/ |
| 76 |
JNIEXPORT jstring JNICALL Java_gnu_io_RXTXCommDriver_nativeGetVersion(JNIEnv *env, jclass cls) |
| 77 |
{ |
| 78 |
return env->NewStringUTF("RXTX-2.0-1"); |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
registerKnownPorts |
| 83 |
|
| 84 |
accept: the type of port |
| 85 |
perform: register any ports of the desired type a priori known to this OS |
| 86 |
return: JNI_TRUE if any such ports were registered otherwise JNI_FALSE |
| 87 |
exceptions: none |
| 88 |
comments: |
| 89 |
* Class: gnu_io_RXTXCommDriver |
| 90 |
* Method: registerKnownPorts |
| 91 |
* Signature: (I)Z |
| 92 |
*/ |
| 93 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXCommDriver_registerKnownPorts(JNIEnv *env, jobject jobj, jint portType) |
| 94 |
{ |
| 95 |
enum {PORT_TYPE_SERIAL = 1, |
| 96 |
PORT_TYPE_PARALLEL, |
| 97 |
PORT_TYPE_I2C, |
| 98 |
PORT_TYPE_RS485, |
| 99 |
PORT_TYPE_RAW}; |
| 100 |
jboolean result = JNI_FALSE; |
| 101 |
|
| 102 |
switch(portType) { |
| 103 |
case PORT_TYPE_SERIAL: |
| 104 |
// We could check here what COMs are on system and register them. |
| 105 |
break; |
| 106 |
|
| 107 |
case PORT_TYPE_PARALLEL: |
| 108 |
case PORT_TYPE_I2C: |
| 109 |
case PORT_TYPE_RS485: |
| 110 |
case PORT_TYPE_RAW: |
| 111 |
break; |
| 112 |
|
| 113 |
default: |
| 114 |
// Wrong port type - I'd raise exception, but there's no defined for Java |
| 115 |
IF_DEBUG |
| 116 |
( |
| 117 |
printj(env, L"!!! RXTXCommDriver.registerKnownPorts(%ld): Wrong port type\n", portType); |
| 118 |
//MessageBox(NULL, TEXT("RXTXCommDriver.registerKnownPorts(): Wrong port type"), TEXT("Error"), MB_OK); |
| 119 |
) |
| 120 |
break; |
| 121 |
} |
| 122 |
return result; |
| 123 |
} |
| 124 |
|
| 125 |
/* |
| 126 |
isPortPrefixValid |
| 127 |
|
| 128 |
accept: a port prefix |
| 129 |
perform: see if the port prefix matches a port that is valid on this OS. |
| 130 |
return: JNI_TRUE if it exists otherwise JNI_FALSE |
| 131 |
exceptions: none |
| 132 |
comments: |
| 133 |
* Class: gnu_io_RXTXCommDriver |
| 134 |
* Method: isPortPrefixValid |
| 135 |
* Signature: (Ljava/lang/String;)Z |
| 136 |
*/ |
| 137 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXCommDriver_isPortPrefixValid(JNIEnv *env, jobject jobj, jstring dev) |
| 138 |
{ |
| 139 |
jboolean retVal; |
| 140 |
const char *szDev = env->GetStringUTFChars(dev, NULL); |
| 141 |
if( strncmp(szDev, "COM", 3) == 0 || strncmp(szDev, "LPT", 3) == 0) // is first 3 chars OK? |
| 142 |
retVal = JNI_TRUE; |
| 143 |
else |
| 144 |
retVal = JNI_FALSE; |
| 145 |
|
| 146 |
env->ReleaseStringUTFChars(dev, szDev); |
| 147 |
|
| 148 |
return retVal; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
/* |
| 153 |
testRead |
| 154 |
|
| 155 |
accept: dev The device to be tested |
| 156 |
perform: test if the device can be read from |
| 157 |
return: JNI_TRUE if the device can be read from |
| 158 |
exceptions: none |
| 159 |
comments: From Wayne Roberts [email protected] |
| 160 |
check tcget/setattr returns. |
| 161 |
support for non serial ports Trent |
| 162 |
* Class: gnu_io_RXTXCommDriver |
| 163 |
* Method: testRead |
| 164 |
* Signature: (Ljava/lang/String;I)Z |
| 165 |
*/ |
| 166 |
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXCommDriver_testRead(JNIEnv *env, jobject jobj, jstring dev, jint type) |
| 167 |
{ |
| 168 |
jboolean retVal; |
| 169 |
DWORD dwError; |
| 170 |
|
| 171 |
if ( type == PORT_SERIAL ) |
| 172 |
{ |
| 173 |
const WCHAR *wszDev = env->GetStringChars(dev, NULL); |
| 174 |
HANDLE hPort = CreateFileW(wszDev, // Pointer to the name of the port |
| 175 |
GENERIC_READ | GENERIC_WRITE, |
| 176 |
// Access (read-write) mode |
| 177 |
0, // Share mode |
| 178 |
NULL, // Pointer to the security attribute |
| 179 |
OPEN_EXISTING,// How to open the serial port |
| 180 |
0, // Port attributes |
| 181 |
NULL); // Handle to port with attribute |
| 182 |
// to copy |
| 183 |
// If it fails to open the port, return FALSE. |
| 184 |
if ( hPort == INVALID_HANDLE_VALUE ) |
| 185 |
{ // Could not open the port. |
| 186 |
IF_DEBUG |
| 187 |
( |
| 188 |
;//printj(env, TEXT("!!! RXTXCommDriver.testRead(%s, %ld): cannot open port\n"), wszDev, type); |
| 189 |
//MessageBox(NULL, TEXT("RXTXCommDriver.testRead(): cannot open port"), wszDev /*TEXT("Error")*/, MB_OK | MB_SETFOREGROUND); |
| 190 |
) |
| 191 |
dwError = GetLastError(); |
| 192 |
retVal = JNI_FALSE; |
| 193 |
} |
| 194 |
else |
| 195 |
{ // Port open OK - let's close it and return TRUE |
| 196 |
if (!CloseHandle(hPort)) |
| 197 |
dwError = GetLastError(); |
| 198 |
IF_DEBUG |
| 199 |
( |
| 200 |
;//printj(env, TEXT("--- RXTXCommDriver.testRead(%s, %ld): port open OK\n"), wszDev, type); |
| 201 |
//MessageBox(NULL, TEXT("RXTXCommDriver.testRead(): port open OK"), wszDev /*TEXT("Success")*/, MB_OK | MB_SETFOREGROUND); |
| 202 |
) |
| 203 |
retVal = JNI_TRUE; |
| 204 |
} |
| 205 |
env->ReleaseStringChars(dev, wszDev); |
| 206 |
} |
| 207 |
else |
| 208 |
retVal = JNI_FALSE; |
| 209 |
|
| 210 |
return retVal; |
| 211 |
} |
| 212 |
|
| 213 |
/* |
| 214 |
getDeviceDirectory |
| 215 |
|
| 216 |
accept: |
| 217 |
perform: |
| 218 |
return: the directory containing the device files |
| 219 |
exceptions: |
| 220 |
comments: we need it only for Unix |
| 221 |
* Class: gnu_io_RXTXCommDriver |
| 222 |
* Method: getDeviceDirectory |
| 223 |
* Signature: ()Ljava/lang/String; |
| 224 |
*/ |
| 225 |
JNIEXPORT jstring JNICALL Java_gnu_io_RXTXCommDriver_getDeviceDirectory(JNIEnv *env, jobject jobj) |
| 226 |
{ |
| 227 |
return env->NewStringUTF(""); |
| 228 |
} |