1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
|
/*-------------------------------------------------------------------------
| RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface.
| RXTX is a native interface to serial ports in java.
| Copyright 1997-2007 by Trent Jarvi tjarvi@qbang.org and others who
| actually wrote it. See individual source files for more information.
|
| A copy of the LGPL v 2.1 may be found at
| http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is
| here for your convenience.
|
| This library is free software; you can redistribute it and/or
| modify it under the terms of the GNU Lesser General Public
| License as published by the Free Software Foundation; either
| version 2.1 of the License, or (at your option) any later version.
|
| This library is distributed in the hope that it will be useful,
| but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
| Lesser General Public License for more details.
|
| An executable that contains no derivative of any portion of RXTX, but
| is designed to work with RXTX by being dynamically linked with it,
| is considered a "work that uses the Library" subject to the terms and
| conditions of the GNU Lesser General Public License.
|
| The following has been added to the RXTX License to remove
| any confusion about linking to RXTX. We want to allow in part what
| section 5, paragraph 2 of the LGPL does not permit in the special
| case of linking over a controlled interface. The intent is to add a
| Java Specification Request or standards body defined interface in the
| future as another exception but one is not currently available.
|
| http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface
|
| As a special exception, the copyright holders of RXTX give you
| permission to link RXTX with independent modules that communicate with
| RXTX solely through the Sun Microsytems CommAPI interface version 2,
| regardless of the license terms of these independent modules, and to copy
| and distribute the resulting combined work under terms of your choice,
| provided that every copy of the combined work is accompanied by a complete
| copy of the source code of RXTX (the version of RXTX used to produce the
| combined work), being distributed under the terms of the GNU Lesser General
| Public License plus this exception. An independent module is a
| module which is not derived from or based on RXTX.
|
| Note that people who make modified versions of RXTX are not obligated
| to grant this special exception for their modified versions; it is
| their choice whether to do so. The GNU Lesser General Public License
| gives permission to release a modified version without this exception; this
| exception also makes it possible to release a modified version which
| carries forward this exception.
|
| You should have received a copy of the GNU Lesser General Public
| License along with this library; if not, write to the Free
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
| All trademarks belong to their respective owners.
--------------------------------------------------------------------------*/
#if defined(__MWERKS__) /* dima */
#include "RS485.h"
#else /* dima */
#include "config.h"
#include "gnu_io_RS485.h"
#endif /* dima */
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#ifndef WIN32
#include <sys/ioctl.h>
#include <sys/param.h>
#include <sys/utsname.h>
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#endif
#ifdef HAVE_SYS_SIGNAL_H
# include <sys/signal.h>
#endif
#else
# include <win32termios.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#ifdef HAVE_SYS_FCNTL_H
# include <sys/fcntl.h>
#endif
#ifdef HAVE_SYS_FILE_H
# include <sys/file.h>
#endif
#if defined(__linux__)
# include <linux/types.h> /* fix for linux-2.3.4? kernels */
# include <linux/serial.h>
# include <linux/version.h>
#endif
extern int errno;
#include "RS485Imp.h"
/* #define DEBUG_TIMEOUT */
/*----------------------------------------------------------
RS485Port.Initialize
accept: none
perform: Initialize the native library
return: none
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_Initialize(
JNIEnv *env,
jclass jclazz
)
{
#ifndef WIN32
struct utsname name;
/* This bit of code checks to see if there is a signal handler installed
for SIGIO, and installs SIG_IGN if there is not. This is necessary
for the native threads jdk, but we don't want to do it with green
threads, because it slows things down. Go figure. */
/* POSIX signal handling functions */
#if !defined(__FreeBSD_kernel___)
struct sigaction handler;
sigaction( SIGIO, NULL, &handler );
if( !handler.sa_handler ) signal( SIGIO, SIG_IGN );
#endif /* !__FreeBSD_kernel__ */
#if defined(__linux__)
/* Lets let people who upgraded kernels know they may have problems */
/*
if (uname (&name) == -1)
{
fprintf(stderr,"RXTX WARNING: cannot get system name\n");
return;
}
if(strcmp(name.release,UTS_RELEASE)!=0)
{
fprintf(stderr, "\n\n\nRXTX WARNING: This library was compiled to run with OS release %s and you are currently running OS release %s. In some cases this can be a problem. Try recompiling RXTX if you notice strange behavior. If you just compiled RXTX make sure /usr/include/linux is a symbolic link to the include files that came with the kernel source and not an older copy.\n\n\npress enter to continue\n",UTS_RELEASE,name.release);
getchar();
}
*/
#endif /* __linux__ */
#endif /* WIN32 */
}
/*----------------------------------------------------------
RS485Port.open
accept: The device to open. ie "/dev/ttyS0"
perform: open the device, set the termios struct to sane settings and
return the filedescriptor
return: fd
exceptions: IOExcepiton
comments: Very often people complain about not being able to get past
this function and it turns out to be permissions on the
device file or bios has the device disabled.
----------------------------------------------------------*/
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_open(
JNIEnv *env,
jobject jobj,
jstring jstr
)
{
struct termios ttyset;
int fd;
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 );
do {
fd=open (filename, O_RDWR | O_NOCTTY | O_NONBLOCK );
} while (fd < 0 && errno==EINTR);
(*env)->ReleaseStringUTFChars( env, jstr, NULL );
if( fd < 0 ) goto fail;
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail;
ttyset.c_iflag = INPCK;
ttyset.c_lflag = 0;
ttyset.c_oflag = 0;
ttyset.c_cflag = CREAD | CS8 | CLOCAL;
ttyset.c_cc[ VMIN ] = 0;
ttyset.c_cc[ VTIME ] = 0;
#ifdef __FreeBSD_kernel__
if( cfsetspeed( &ttyset, B9600 ) < 0 ) goto fail;
#else
if( cfsetispeed( &ttyset, B9600 ) < 0 ) goto fail;
if( cfsetospeed( &ttyset, B9600 ) < 0 ) goto fail;
#endif
if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail;
#ifndef WIN32
fcntl( fd, F_SETOWN, getpid() );
fcntl( fd, F_SETFL, FASYNC );
#endif /* WIN32 */
return (jint)fd;
fail:
throw_java_exception( env, IO_EXCEPTION, "open", strerror( errno ) );
return -1;
}
/*----------------------------------------------------------
RS485Port.nativeClose
accept: none
perform: get the fd from the java end and close it
return: none
exceptions: none
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_nativeClose( JNIEnv *env,
jobject jobj )
{
int result;
int fd = get_java_var( env, jobj,"fd","I" );
do {
result=close (fd);
} while (result < 0 && errno==EINTR);
return;
}
/*----------------------------------------------------------
RS485Port.nativeSetRS485PortParams
accept: speed, data bits, stop bits, parity
perform: set the RS485 port parameters
return: void
exceptions: UnsupportedCommOperationException
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_nativeSetRS485PortParams(
JNIEnv *env, jobject jobj, jint speed, jint dataBits, jint stopBits,
jint parity )
{
struct termios ttyset;
int fd = get_java_var( env, jobj,"fd","I" );
int cspeed = translate_speed( env, speed );
if( !cspeed ) return;
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail;
if( !translate_data_bits( env, (int *)&(ttyset.c_cflag), dataBits ) ) return; /* dima c_cflag in darwin is unsigned long */
if( !translate_stop_bits( env, (int *)&(ttyset.c_cflag), stopBits ) ) return; /* dima c_cflag in darwin is unsigned long */
if( !translate_parity( env, (int *)&(ttyset.c_cflag), parity ) ) return;/* dima c_cflag in darwin is unsigned long */
#ifdef __FreeBSD_kernel__
if( cfsetspeed( &ttyset, cspeed ) < 0 ) goto fail;
#else
if( cfsetispeed( &ttyset, cspeed ) < 0 ) goto fail;
if( cfsetospeed( &ttyset, cspeed ) < 0 ) goto fail;
#endif
if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail;
/* dump_termios("set",*ttyset); */
return;
fail:
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
"nativeSetRS485PortParams", strerror( errno ) );
}
/*----------------------------------------------------------
translate_speed
accept: speed in bits-per-second
perform: convert bits-per-second to a speed_t constant
return: speed_t constant
exceptions: UnsupportedCommOperationException
comments: Only the lowest level code should know about
the magic constants.
----------------------------------------------------------*/
int translate_speed( JNIEnv *env, jint speed )
{
switch( speed ) {
case 0: return B0;
case 50: return B50;
case 75: return B75;
case 110: return B110;
case 134: return B134;
case 150: return B150;
case 200: return B200;
case 300: return B300;
case 600: return B600;
case 1200: return B1200;
case 1800: return B1800;
case 2400: return B2400;
case 4800: return B4800;
case 9600: return B9600;
case 19200: return B19200;
case 38400: return B38400;
case 57600: return B57600;
case 115200: return B115200;
case 230400: return B230400;
#ifdef B460800
case 460800: return B460800;
#endif
}
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
"translate_speed", "speed" );
return 0;
}
/*----------------------------------------------------------
translate_data_bits
accept: gnu.io.RS485Port.DATABITS_* constant
perform: set proper termios c_cflag bits
return: 1 if successful
0 if an exception is thrown
exceptions: UnsupportedCommOperationException
----------------------------------------------------------*/
int translate_data_bits( JNIEnv *env, int *cflag, jint dataBits )
{
int temp = (*cflag) & ~CSIZE;
switch( dataBits ) {
case DATABITS_5:
(*cflag) = temp | CS5;
return 1;
case DATABITS_6:
(*cflag) = temp | CS6;
return 1;
case DATABITS_7:
(*cflag) = temp | CS7;
return 1;
case DATABITS_8:
(*cflag) = temp | CS8;
return 1;
}
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
"translate_data_bits", "data bits" );
return 0;
}
/*----------------------------------------------------------
translate_stop_bits
accept: gnu.io.RS485Port.STOPBITS_* constant
perform: set proper termios c_cflag bits
return: 1 if successful
0 if an exception is thrown
exceptions: UnsupportedCommOperationException
comments: If you specify 5 data bits and 2 stop bits, the port will
allegedly use 1.5 stop bits. Does anyone care?
----------------------------------------------------------*/
int translate_stop_bits( JNIEnv *env, int *cflag, jint stopBits )
{
switch( stopBits ) {
case STOPBITS_1:
(*cflag) &= ~CSTOPB;
return 1;
case STOPBITS_2:
(*cflag) |= CSTOPB;
return 1;
}
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
"translate_stop_bits", "stop bits" );
return 0;
}
/*----------------------------------------------------------
translate_parity
accept: gnu.io.RS485Port.PARITY_* constant
perform: set proper termios c_cflag bits
return: 1 if successful
0 if an exception is thrown
exceptions: UnsupportedCommOperationException
comments: The CMSPAR bit should be used for 'mark' and 'space' parity,
but it's not in glibc's includes. Oh well, rarely used anyway.
----------------------------------------------------------*/
int translate_parity( JNIEnv *env, int *cflag, jint parity )
{
(*cflag) &= ~(PARENB | PARODD);
switch( parity ) {
case PARITY_NONE:
return 1;
#ifdef CMSPAR
case PARITY_EVEN:
(*cflag) |= PARENB;
return 1;
case PARITY_ODD:
(*cflag) |= PARENB | PARODD;
return 1;
case PARITY_MARK:
(*cflag) |= PARENB | PARODD | CMSPAR;
return 1;
case PARITY_SPACE:
(*cflag) |= PARENB | CMSPAR;
return 1;
#else
case PARITY_EVEN:
(*cflag) |= PARENB;
return 1;
case PARITY_ODD:
(*cflag) |= PARENB | PARODD;
return 1;
#endif
}
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION,
"translate_parity", "parity" );
return 0;
}
/*----------------------------------------------------------
RS485Port.writeByte
accept: byte to write (passed as int)
perform: write a single byte to the port
return: none
exceptions: IOException
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_writeByte( JNIEnv *env,
jobject jobj, jint ji )
{
unsigned char byte = (unsigned char)ji;
int fd = get_java_var( env, jobj,"fd","I" );
int result=0,count=0;
/* turn on the DTR */
ioctl(fd, TIOCMGET, &result);
result |= TIOCM_DTR;
ioctl(fd, TIOCMSET, &result);
/* send the data */
do {
result=write (fd, &byte, sizeof(unsigned char));
} while (result < 0 && errno==EINTR);
if(result < 0) goto fail;
/* wait for the last bit to go
* see get_lsr_info in linux/driver/char/serial.c
* */
#if defined(TIOCSERGETLSR) /* dima ????????? I did it in exactly the same way as in SerialImp.c */
do
{
result=ioctl(fd, TIOCSERGETLSR);
/* FIXME this should work but its a hack */
if (result != TIOCSER_TEMT)
{
usleep(100);
}
} while(result != TIOCSER_TEMT);
#endif /* TIOCSERGETLSR */
/* shut down the DTR */
ioctl(fd, TIOCMGET, &result);
result &= ~TIOCM_DTR;
ioctl(fd, TIOCMSET, &result);
/* flush input (we dont want to get our own message */
do {
result=tcflush(fd, TCIFLUSH);
} while (result && errno==EINTR && count <5);
if(result) goto fail;
return;
fail:
throw_java_exception( env, IO_EXCEPTION, "writeByte",
strerror( errno ) );
}
/*----------------------------------------------------------
RS485Port.writeArray
accept: jbarray: bytes used for writing
offset: offset in array to start writing
count: Number of bytes to write
perform: write length bytes of jbarray
return: none
exceptions: IOException
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_writeArray( JNIEnv *env,
jobject jobj, jbyteArray jbarray, jint offset, jint count )
{
int fd = get_java_var( env, jobj,"fd","I" );
int result=0,total=0,i;
unsigned char *bytes = (unsigned char *)malloc( count );
jbyte *body = (*env)->GetByteArrayElements( env, jbarray, 0 );
for( i = 0; i < count; i++ ) bytes[ i ] = body[ i + offset ];
(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 );
/* turn on the DTR */
ioctl(fd, TIOCMGET, &result);
result |= TIOCM_DTR;
ioctl(fd, TIOCMSET, &result);
/* send the data */
do {
result=write (fd, bytes + total, count - total);
if(result >0){
total += result;
}
} while ((total<count)||(result < 0 && errno==EINTR));
if (result < 0) goto fail;
/* wait for the last bit to go
* see get_lsr_info in linux/driver/char/serial.c
* */
#if defined(TIOCSERGETLSR) /* dima ????????? I did it in exactly the same way as in SerialImp.c */
do
{
result=ioctl(fd, TIOCSERGETLSR);
/* FIXME this should work but its a hack */
if (result != TIOCSER_TEMT)
{
usleep(100);
}
} while(result != TIOCSER_TEMT);
#endif /* TIOCSERGETLSR */
/* shut down the DTR */
ioctl(fd, TIOCMGET, &result);
result &= ~TIOCM_DTR;
ioctl(fd, TIOCMSET, &result);
/* flush input (we dont want to get our own message */
do {
result=tcflush(fd, TCIFLUSH);
} while (result && errno==EINTR && count <5);
if(result) goto fail;
free( bytes );
return;
fail:
free( bytes );
throw_java_exception( env, IO_EXCEPTION, "writeArray",
strerror( errno ) );
}
/*----------------------------------------------------------
RS485Port.drain
accept: none
perform: wait until all data is transmitted
return: none
exceptions: IOException
comments: java.io.OutputStream.flush() is equivalent to tcdrain,
not tcflush, which throws away unsent bytes
count logic added to avoid infinite loops when EINTR is
true... Thread.yeild() was suggested.
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_drain( JNIEnv *env,
jobject jobj )
{
int fd = get_java_var( env, jobj,"fd","I" );
int result, count=0;
do {
result=tcdrain (fd);
count++;
} while (result && errno==EINTR && count <5);
if( result ) throw_java_exception( env, IO_EXCEPTION, "drain",
strerror( errno ) );
}
/*----------------------------------------------------------
RS485Port.sendBreak
accept: duration in milliseconds.
perform: send break for actual time. not less than 0.25 seconds.
exceptions: none
comments: not very precise
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_sendBreak( JNIEnv *env,
jobject jobj, jint duration )
{
int fd = get_java_var( env, jobj,"fd","I" );
tcsendbreak( fd, (int)( duration / 250 ) );
}
/*----------------------------------------------------------
RS485Port.NativegetReceiveTimeout
accept: none
perform: get termios.c_cc[VTIME]
return: VTIME
comments: see NativeEnableReceiveTimeoutThreshold
----------------------------------------------------------*/
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_NativegetReceiveTimeout(
JNIEnv *env,
jobject jobj
)
{
int fd = get_java_var( env, jobj,"fd","I" );
struct termios ttyset;
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail;
return(ttyset.c_cc[ VTIME ] * 100);
fail:
throw_java_exception( env, IO_EXCEPTION, "getReceiveTimeout", strerror( errno ) );
return -1;
}
/*----------------------------------------------------------
RS485Port.NativeisReceiveTimeoutEnabled
accept: none
perform: determine if VTIME is none 0
return: JNI_TRUE if VTIME > 0 else JNI_FALSE
comments: see NativeEnableReceiveTimeoutThreshold
----------------------------------------------------------*/
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_NativeisReceiveTimeoutEnabled(
JNIEnv *env,
jobject jobj
)
{
int fd = get_java_var( env, jobj,"fd","I" );
struct termios ttyset;
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail;
return(ttyset.c_cc[ VTIME ] > 0 ? JNI_TRUE:JNI_FALSE);
fail:
throw_java_exception( env, IO_EXCEPTION, "isReceiveTimeoutEnabled", strerror( errno ) );
return JNI_FALSE;
}
/*----------------------------------------------------------
RS485Port.isDSR
accept: none
perform: check status of DSR
return: true if TIOCM_DSR is set
false if TIOCM_DSR is not set
exceptions: none
comments: DSR stands for Data Set Ready
----------------------------------------------------------*/
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isDSR( JNIEnv *env,
jobject jobj )
{
unsigned int result = 0;
int fd = get_java_var( env, jobj,"fd","I" );
ioctl( fd, TIOCMGET, &result );
if( result & TIOCM_DSR ) return JNI_TRUE;
else return JNI_FALSE;
}
/*----------------------------------------------------------
RS485Port.isCD
accept: none
perform: check status of CD
return: true if TIOCM_CD is set
false if TIOCM_CD is not set
exceptions: none
comments: CD stands for Carrier Detect
The following comment has been made...
"well, it works, there might ofcourse be a bug, but making DCD
permanently on fixed it for me so I don't care"
----------------------------------------------------------*/
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isCD( JNIEnv *env,
jobject jobj )
{
unsigned int result = 0;
int fd = get_java_var( env, jobj,"fd","I" );
ioctl( fd, TIOCMGET, &result );
if( result & TIOCM_CD ) return JNI_TRUE;
else return JNI_FALSE;
}
/*----------------------------------------------------------
RS485Port.isCTS
accept: none
perform: check status of CTS
return: true if TIOCM_CTS is set
false if TIOCM_CTS is not set
exceptions: none
comments: CTS stands for Clear To Send.
----------------------------------------------------------*/
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isCTS( JNIEnv *env,
jobject jobj )
{
unsigned int result = 0;
int fd = get_java_var( env, jobj,"fd","I" );
ioctl( fd, TIOCMGET, &result );
if( result & TIOCM_CTS ) return JNI_TRUE;
else return JNI_FALSE;
}
/*----------------------------------------------------------
RS485Port.isRI
accept: none
perform: check status of RI
return: true if TIOCM_RI is set
false if TIOCM_RI is not set
exceptions: none
comments: RI stands for Ring Indicator
----------------------------------------------------------*/
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isRI( JNIEnv *env,
jobject jobj )
{
unsigned int result = 0;
int fd = get_java_var( env, jobj,"fd","I" );
ioctl( fd, TIOCMGET, &result );
if( result & TIOCM_RI ) return JNI_TRUE;
else return JNI_FALSE;
}
/*----------------------------------------------------------
RS485Port.isRTS
accept: none
perform: check status of RTS
return: true if TIOCM_RTS is set
false if TIOCM_RTS is not set
exceptions: none
comments: tcgetattr with c_cflag CRTS_IFLOW
----------------------------------------------------------*/
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isRTS( JNIEnv *env,
jobject jobj )
{
unsigned int result = 0;
int fd = get_java_var( env, jobj,"fd","I" );
ioctl( fd, TIOCMGET, &result );
if( result & TIOCM_RTS ) return JNI_TRUE;
else return JNI_FALSE;
}
/*----------------------------------------------------------
RS485Port.setRTS
accept: state flag to set/unset.
perform: depends on the state flag
if true TIOCM_RTS is set
if false TIOCM_RTS is unset
return: none
exceptions: none
comments: tcsetattr with c_cflag CRTS_IFLOW
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setRTS( JNIEnv *env,
jobject jobj, jboolean state )
{
unsigned int result = 0;
int fd = get_java_var( env, jobj,"fd","I" );
ioctl( fd, TIOCMGET, &result );
if( state == JNI_TRUE ) result |= TIOCM_RTS;
else result &= ~TIOCM_RTS;
ioctl( fd, TIOCMSET, &result );
return;
}
/*----------------------------------------------------------
RS485Port.setDSR
accept: state flag to set/unset.
perform: depends on the state flag
if true TIOCM_DSR is set
if false TIOCM_DSR is unset
return: none
exceptions: none
comments: tcsetattr with c_cflag CRTS_IFLOW
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setDSR( JNIEnv *env,
jobject jobj, jboolean state )
{
unsigned int result = 0;
int fd = get_java_var( env, jobj,"fd","I" );
ioctl( fd, TIOCMGET, &result );
if( state == JNI_TRUE ) result |= TIOCM_DSR;
else result &= ~TIOCM_DSR;
ioctl( fd, TIOCMSET, &result );
return;
}
/*----------------------------------------------------------
RS485Port.isDTR
accept: none
perform: check status of DTR
return: true if TIOCM_DTR is set
false if TIOCM_DTR is not set
exceptions: none
comments: DTR stands for Data Terminal Ready
----------------------------------------------------------*/
JNIEXPORT jboolean JNICALL Java_gnu_io_RS485Port_isDTR( JNIEnv *env,
jobject jobj )
{
unsigned int result = 0;
int fd = get_java_var( env, jobj,"fd","I" );
ioctl( fd, TIOCMGET, &result );
if( result & TIOCM_DTR ) return JNI_TRUE;
else return JNI_FALSE;
}
/*----------------------------------------------------------
RS485Port.setDTR
accept: new DTR state
perform: if state is true, TIOCM_DTR is set
if state is false, TIOCM_DTR is unset
return: none
exceptions: none
comments: DTR stands for Data Terminal Ready
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setDTR( JNIEnv *env,
jobject jobj, jboolean state )
{
unsigned int result = 0;
int fd = get_java_var( env, jobj,"fd","I" );
ioctl( fd, TIOCMGET, &result );
if( state == JNI_TRUE ) result |= TIOCM_DTR;
else result &= ~TIOCM_DTR;
ioctl( fd, TIOCMSET, &result );
return;
}
/*----------------------------------------------------------
read_byte_array
accept: int fd file descriptor to read from
unsigned char *buffer buffer to read data into
int length number of bytes to read
int timeout milliseconds to wait before returning
perform: read bytes from the port into a buffer
return: status of read
-1 fail (IOException)
0 timeout
>0 number of bytes read
comments: According to the Communications API spec, a receive threshold
of 1 is the same as having the threshold disabled.
The nuts and bolts are documented in
NativeEnableReceiveTimeoutThreshold()
----------------------------------------------------------*/
int read_byte_array( int fd, unsigned char *buffer, int length, int timeout )
{
int ret, left, bytes = 0;
fd_set rfds;
struct timeval sleep;
struct timeval *psleep=&sleep;
left = length;
FD_ZERO( &rfds );
FD_SET( fd, &rfds );
if( timeout != 0 )
{
sleep.tv_sec = timeout / 1000;
sleep.tv_usec = 1000 * ( timeout % 1000 );
}
while( bytes < length )
{
/* FIXME: In Linux, select updates the timeout automatically, so
other OSes will need to update it manually if they want to have
the same behavior. For those OSes, timeouts will occur after no
data AT ALL is received for the timeout duration. No big deal. */
do {
if( timeout == 0 ) psleep = NULL;
ret=select( fd + 1, &rfds, NULL, NULL, psleep );
} while (ret < 0 && errno==EINTR);
if( ret == 0 ) break;
if( ret < 0 ) return -1;
ret = read( fd, buffer + bytes, left );
if( ret == 0 ) break;
if( ret < 0 ) return -1;
bytes += ret;
left -= ret;
}
return bytes;
}
/*----------------------------------------------------------
NativeEnableReceiveTimeoutThreshold
accept: int threshold, int vtime,int buffer
perform: Set c_cc->VMIN to threshold and c_cc=>VTIME to vtime
return: void
exceptions: IOException
comments: This is actually all handled in read with select in
canonical input mode.
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_NativeEnableReceiveTimeoutThreshold(JNIEnv *env, jobject jobj, jint vtime, jint threshold, jint buffer)
{
int fd = get_java_var( env, jobj,"fd","I" );
struct termios ttyset;
if( tcgetattr( fd, &ttyset ) < 0 ) goto fail;
ttyset.c_cc[ VMIN ] = threshold;
ttyset.c_cc[ VTIME ] = vtime/100;
if( tcsetattr( fd, TCSANOW, &ttyset ) < 0 ) goto fail;
return;
fail:
throw_java_exception( env, IO_EXCEPTION, "TimeoutThreshold", strerror( errno ) );
return;
}
/*----------------------------------------------------------
RS485Port.readByte
accept: none
perform: Read a single byte from the port
return: The byte read
exceptions: IOException
----------------------------------------------------------*/
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_readByte( JNIEnv *env,
jobject jobj )
{
int bytes;
unsigned char buffer[ 1 ];
int fd = get_java_var( env, jobj,"fd","I" );
int timeout = get_java_var( env, jobj, "timeout", "I" );
bytes = read_byte_array( fd, buffer, 1, timeout );
if( bytes < 0 ) {
throw_java_exception( env, IO_EXCEPTION, "readByte",
strerror( errno ) );
return -1;
}
return (bytes ? (jint)buffer[ 0 ] : -1);
}
/*----------------------------------------------------------
RS485Port.readArray
accept: offset (offset to start storing data in the jbarray) and
Length (bytes to read)
perform: read bytes from the port into a byte array
return: bytes read on success
0 on read timeout
exceptions: IOException
comments: throws ArrayIndexOutOfBoundsException if asked to
read more than SSIZE_MAX bytes
----------------------------------------------------------*/
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_readArray( JNIEnv *env,
jobject jobj, jbyteArray jbarray, jint offset, jint length )
{
int bytes;
jbyte *body;
unsigned char *buffer;
int fd = get_java_var( env, jobj, "fd", "I" );
int timeout = get_java_var( env, jobj, "timeout", "I" );
if( (size_t) length > SSIZE_MAX || (size_t) length < 0 ) {
throw_java_exception( env, ARRAY_INDEX_OUT_OF_BOUNDS,
"readArray", "Invalid length" );
return -1;
}
buffer = (unsigned char *)malloc( sizeof( unsigned char ) * length );
if( buffer == 0 ) {
throw_java_exception( env, OUT_OF_MEMORY, "readArray",
"Unable to allocate buffer" );
return -1;
}
bytes = read_byte_array( fd, buffer, length, timeout );
if( bytes < 0 ) {
free( buffer );
throw_java_exception( env, IO_EXCEPTION, "readArray",
strerror( errno ) );
return -1;
}
body = (*env)->GetByteArrayElements( env, jbarray, 0 );
memcpy(body + offset, buffer, bytes);
(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 );
free( buffer );
return (bytes ? bytes : -1);
}
/*----------------------------------------------------------
RS485Port.nativeavailable
accept: none
perform: find out the number of bytes available for reading
return: available bytes
-1 on error
exceptions: none
----------------------------------------------------------*/
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_nativeavailable( JNIEnv *env,
jobject jobj )
{
int fd = get_java_var( env, jobj,"fd","I" );
int result;
if( ioctl( fd, FIONREAD, &result ) )
{
throw_java_exception( env, IO_EXCEPTION, "nativeavailable", strerror( errno ) );
return -1;
}
else return (jint)result;
}
/*----------------------------------------------------------
RS485Port.setflowcontrol
accept: flowmode
FLOWCONTROL_NONE none
FLOWCONTROL_RTSCTS_IN hardware flow control
FLOWCONTROL_RTSCTS_OUT ""
FLOWCONTROL_XONXOFF_IN input software flow control
FLOWCONTROL_XONXOFF_OUT output software flow control
perform: set flow control to flowmode
return: none
exceptions: IOException
comments: there is no differentiation between input and output hardware
flow control
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setflowcontrol( JNIEnv *env,
jobject jobj, jint flowmode )
{
struct termios ttyset;
int fd = get_java_var( env, jobj,"fd","I" );
if( tcgetattr( fd, &ttyset ) ) goto fail;
if ( flowmode & ( FLOWCONTROL_RTSCTS_IN | FLOWCONTROL_RTSCTS_OUT ) )
ttyset.c_cflag |= HARDWARE_FLOW_CONTROL;
else ttyset.c_cflag &= ~HARDWARE_FLOW_CONTROL;
ttyset.c_iflag &= ~IXANY;
if ( flowmode & FLOWCONTROL_XONXOFF_IN )
ttyset.c_iflag |= IXOFF;
else ttyset.c_iflag &= ~IXOFF;
if ( flowmode & FLOWCONTROL_XONXOFF_OUT )
ttyset.c_iflag |= IXON;
else ttyset.c_iflag &= ~IXON;
if( tcsetattr( fd, TCSANOW, &ttyset ) ) goto fail;
return;
fail:
throw_java_exception( env, IO_EXCEPTION, "setHWFC",
strerror( errno ) );
return;
}
/*----------------------------------------------------------
RS485Port.eventLoop
accept: none
perform: periodically check for RS485PortEvents
return: none
exceptions: none
comments: FIXME This is probably wrong on bsd.
----------------------------------------------------------*/
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_eventLoop( JNIEnv *env,
jobject jobj )
{
int fd, ret, change;
fd_set rfds;
struct timeval tv_sleep;
unsigned int mflags;
#if defined(TIOCGICOUNT)
struct serial_icounter_struct sis, osis;
#endif /* TIOCGICOUNT */
unsigned int omflags;
jmethodID method, interrupt;
jboolean interrupted = 0;
jclass jclazz, jthread;
jclazz = (*env)->GetObjectClass( env, jobj );
fd = get_java_var(env, jobj, "fd", "I");
method = (*env)->GetMethodID( env, jclazz, "sendEvent", "(IZ)V" );
jthread = (*env)->FindClass( env, "java/lang/Thread" );
interrupt = (*env)->GetStaticMethodID( env, jthread, "interrupted", "()Z" );
/* Some multiport RS485 cards do not implement TIOCGICOUNT ... */
#if defined(TIOCGICOUNT)
if( ioctl( fd, TIOCGICOUNT, &osis ) < 0 ) {
fprintf( stderr, "Port does not support TIOCGICOUNT events\n" );
return;
}
#else
fprintf( stderr, "Port does not support all Hardware events\n" );
#endif /* TIOCGICOUNT */
if( ioctl( fd, TIOCMGET, &omflags) <0 ) {
fprintf( stderr, "Port does not support events\n" );
return;
}
FD_ZERO( &rfds );
while( !interrupted ) {
FD_SET( fd, &rfds );
/* Check every 1 second, or on receive data */
tv_sleep.tv_sec = 1;
tv_sleep.tv_usec = 0;
do {
ret=select( fd + 1, &rfds, NULL, NULL, &tv_sleep );
} while (ret < 0 && errno==EINTR);
if( ret < 0 ) {
fprintf( stderr, "select() Failed\n" );
break;
}
#if defined TIOCSERGETLSR
if( ioctl( fd, TIOCSERGETLSR, &change ) ) {
fprintf( stderr, "TIOCSERGETLSR Failed\n" );
break;
}
else if( change ) {
(*env)->CallVoidMethod( env, jobj, method,
(jint)SPE_OUTPUT_BUFFER_EMPTY, JNI_TRUE );
}
#endif /* TIOCSERGETLSR */
#if defined(TIOCGICOUNT)
/* wait for RNG, DSR, CD or CTS but not DataAvailable
* The drawback here is it never times out so if someone
* reads there will be no chance to try again.
* This may make sense if the program does not want to
* be notified of data available or errors.
* ret=ioctl(fd,TIOCMIWAIT);
*/
if( ioctl( fd, TIOCGICOUNT, &sis ) ) {
fprintf( stderr, "TIOCGICOUNT Failed\n" );
break;
}
while( sis.frame != osis.frame ) {
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_FE, JNI_TRUE );
osis.frame++;
}
while( sis.overrun != osis.overrun ) {
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_OE, JNI_TRUE );
osis.overrun++;
}
while( sis.parity != osis.parity ) {
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_PE, JNI_TRUE );
osis.parity++;
}
while( sis.brk != osis.brk ) {
(*env)->CallVoidMethod( env, jobj, method, (jint)SPE_BI, JNI_TRUE );
osis.brk++;
}
osis = sis;
#endif /* TIOCGICOUNT */
if( ioctl( fd, TIOCMGET, &mflags ) ) {
fprintf( stderr, "TIOCMGET Failed\n" );
break;
}
interrupted = (*env)->CallStaticBooleanMethod( env, jthread, interrupt );
/* A Portable implementation */
change = (mflags&TIOCM_CTS) - (omflags&TIOCM_CTS);
if( change ) {
fprintf(stderr, "Sending SPE_CTS\n");
(*env)->CallVoidMethod( env, jobj, method,
(jint)SPE_CTS, JNI_TRUE );
}
change = (mflags&TIOCM_DSR) - (omflags&TIOCM_DSR);
if( change ) {
fprintf(stderr, "Sending SPE_DSR\n");
(*env)->CallVoidMethod( env, jobj, method,
(jint)SPE_DSR, JNI_TRUE );
}
change = (mflags&TIOCM_RNG) - (omflags&TIOCM_RNG);
if( change ) {
fprintf(stderr, "Sending SPE_RI\n");
(*env)->CallVoidMethod( env, jobj, method,
(jint)SPE_RI, JNI_TRUE );
}
change = (mflags&TIOCM_CD) - (omflags&TIOCM_CD);
if( change ) {
fprintf(stderr, "Sending SPE_CD\n");
(*env)->CallVoidMethod( env, jobj, method,
(jint)SPE_CD, JNI_TRUE );
}
omflags = mflags;
if( ioctl( fd, FIONREAD, &change ) ) {
fprintf( stderr, "FIONREAD Failed\n" );
}
else if( change ) {
(*env)->CallVoidMethod( env, jobj, method,
(jint)SPE_DATA_AVAILABLE, JNI_TRUE );
usleep(1000); /* select wont block */
}
}
return;
}
/*----------------------------------------------------------
send_modem_events
accept: int event RS485PortEvent constant
int change Number of times this event happened
int state current state: 0 is false, nonzero is true
perform: Send the necessary events
return: none
exceptions: none
comments: Since the interrupt counters tell us how many times the
state has changed, we can send a RS485PortEvent for each
interrupt (change) that has occured. If we don't do this,
we'll miss a whole bunch of events.
----------------------------------------------------------*/
void send_modem_events( JNIEnv *env, jobject jobj, jmethodID method,
int event, int change, int state )
{
int i, s;
jboolean flag;
if( state ) s = 1;
else s = 0;
for( i = 0; i < change; i++ ) {
if( ( change + s + i ) % 2 ) flag = JNI_FALSE;
else flag = JNI_TRUE;
(*env)->CallVoidMethod( env, jobj, method, (jint)event, flag );
}
}
/*----------------------------------------------------------
get_java_fd
accept: env (keyhole to java)
jobj (java RS485Port object)
return: the fd field from the java object
exceptions: none
comments:
----------------------------------------------------------*/
int get_java_var( JNIEnv *env, jobject jobj, char *id, char *type )
{
int result = 0;
jclass jclazz = (*env)->GetObjectClass( env, jobj );
jfieldID jfd = (*env)->GetFieldID( env, jclazz, id, type );
if( !jfd ) {
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
return result;
}
result = (int)( (*env)->GetIntField( env, jobj, jfd ) );
/* ct7 & gel * Added DeleteLocalRef */
(*env)->DeleteLocalRef( env, jclazz );
return result;
}
/*----------------------------------------------------------
throw_java_exception
accept: env (keyhole to java)
*exc (exception class name)
*foo (function name)
*msg (error message)
perform: Throw a new java exception
return: none
exceptions: haha!
comments:
----------------------------------------------------------*/
void throw_java_exception( JNIEnv *env, char *exc, char *foo, char *msg )
{
char buf[ 60 ];
jclass clazz = (*env)->FindClass( env, exc );
if( !clazz ) {
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
return;
}
#if defined(_GNU_SOURCE)
snprintf( buf, 60, "%s in %s", msg, foo );
#else
sprintf( buf,"%s in %s", msg, foo );
#endif /* _GNU_SOURCE */
(*env)->ThrowNew( env, clazz, buf );
/* ct7 * Added DeleteLocalRef */
(*env)->DeleteLocalRef( env, clazz );
}
JNIEXPORT jboolean JNICALL Java_gnu_io_RXTXCommDriver_IsDeviceGood(JNIEnv *env,
jobject jobj, jstring tty_name){
jboolean result;
static struct stat mystat;
char teststring[256];
int fd,i;
const char *name = (*env)->GetStringUTFChars(env, tty_name, 0);
#if defined(__linux__)
if(!strcmp(name,"tty0")|| !strcmp(name,"ttyd")||
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")||
!strcmp(name,"ttyf")|| !strcmp(name,"cuaa")
)
{
#ifdef DEBUG
fprintf(stderr,"DEBUG: Ignoring Port %s\*\n",name);
#endif
return(JNI_FALSE);
}
#endif
#if defined(__FreeBSD_kernel__)
if(!strcmp(name,"tty0")|| !strcmp(name,"ttyd")||
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")||
!strcmp(name,"ttyf")|| !strcmp(name,"ttyS")||
!strcmp(name,"ttyI")|| !strcmp(name,"ttyW")||
!strcmp(name,"ttyC")|| !strcmp(name,"ttyR")
)
{
#ifdef DEBUG
fprintf(stderr,"DEBUG: Ignoring Port %s*\n",name);
#endif
return(JNI_FALSE);
}
#endif
#if defined(__NetBSD__)
if( !strcmp(name,"ttyd")||
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")||
!strcmp(name,"ttyf")|| !strcmp(name,"ttyS")||
!strcmp(name,"ttyI")|| !strcmp(name,"ttyW")||
!strcmp(name,"ttyq")|| !strcmp(name,"ttym")||
!strcmp(name,"ttyf")|| !strcmp(name,"cuaa")||
!strcmp(name,"ttyC")|| !strcmp(name,"ttyR")||
!strcmp(name,"ttyM")
)
{
#ifdef DEBUG
fprintf(stderr,"DEBUG: Ignoring Port %s*\n",name);
#endif
return(JNI_FALSE);
}
#endif
for(i=0;i<64;i++){
sprintf(teststring,"/dev/%s%i",name, i);
stat(teststring,&mystat);
if(S_ISCHR(mystat.st_mode)){
fd=open(teststring,O_RDONLY|O_NONBLOCK);
if (fd>0){
close(fd);
result=JNI_TRUE;
break;
}
result=JNI_FALSE;
}
else result=JNI_FALSE;
}
sprintf(teststring,"/dev/%s",name);
stat(teststring,&mystat);
if(S_ISCHR(mystat.st_mode)){
fd=open(teststring,O_RDONLY|O_NONBLOCK);
if (fd>0){
close(fd);
result=JNI_TRUE;
}
}
(*env)->ReleaseStringUTFChars(env, tty_name, name);
return(result);
}
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setInputBufferSize(JNIEnv *env, jobject jobj, jint size )
{
#ifdef DEBUG
fprintf(stderr,"setInputBufferSize is not implemented\n");
#endif
}
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_getInputBufferSize(JNIEnv *env, jobject jobj)
{
#ifdef DEBUG
fprintf(stderr,"getInputBufferSize is not implemented\n");
#endif
return(1);
}
JNIEXPORT void JNICALL Java_gnu_io_RS485Port_setOutputBufferSize(JNIEnv *env, jobject jobj, jint size )
{
#ifdef DEBUG
fprintf(stderr,"setOutputBufferSize is not implemented\n");
#endif
}
JNIEXPORT jint JNICALL Java_gnu_io_RS485Port_getOutputBufferSize(JNIEnv *env, jobject jobj)
{
#ifdef DEBUG
fprintf(stderr,"getOutputBufferSize is not implemented\n");
#endif
return(1);
}
void dump_termios(char *foo,struct termios *ttyset)
{
int i;
fprintf(stderr,"%s %o\n",foo,ttyset->c_iflag);
fprintf(stderr,"%s %o\n",foo,ttyset->c_lflag);
fprintf(stderr,"%s %o\n",foo,ttyset->c_oflag);
fprintf(stderr,"%s %o\n",foo,ttyset->c_cflag);
for(i=0;i<NCCS;i++)
{
fprintf(stderr,"%s %o ",foo,ttyset->c_cc[i]);
}
fprintf(stderr,"\n");
}
|