Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 7 → Rev 8

/JavaSerial/NativeCode/com_rm5248_serial_SerialPort_SerialInputStream.h
0,0 → 1,31
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_rm5248_serial_SerialPort_SerialInputStream */
 
#ifndef _Included_com_rm5248_serial_SerialPort_SerialInputStream
#define _Included_com_rm5248_serial_SerialPort_SerialInputStream
#ifdef __cplusplus
extern "C" {
#endif
#undef com_rm5248_serial_SerialPort_SerialInputStream_SKIP_BUFFER_SIZE
#define com_rm5248_serial_SerialPort_SerialInputStream_SKIP_BUFFER_SIZE 2048L
/*
* Class: com_rm5248_serial_SerialPort_SerialInputStream
* Method: readByte
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_00024SerialInputStream_readByte
(JNIEnv *, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort_SerialInputStream
* Method: getAvailable
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_00024SerialInputStream_getAvailable
(JNIEnv *, jobject);
 
#ifdef __cplusplus
}
#endif
#endif
/JavaSerial/NativeCode/com_rm5248_serial_SerialPort_SerialOutputStream.h
0,0 → 1,21
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_rm5248_serial_SerialPort_SerialOutputStream */
 
#ifndef _Included_com_rm5248_serial_SerialPort_SerialOutputStream
#define _Included_com_rm5248_serial_SerialPort_SerialOutputStream
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_rm5248_serial_SerialPort_SerialOutputStream
* Method: writeByte
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_rm5248_serial_SerialPort_00024SerialOutputStream_writeByte
(JNIEnv *, jobject, jint);
 
#ifdef __cplusplus
}
#endif
#endif
/JavaSerial/NativeCode/SerialPortImpl.c
0,0 → 1,408
#ifdef __WIN32
 
#else
#include <termio.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#define SPEED_SWITCH(SPD,io) case SPD: cfsetospeed( &io, B##SPD ); break;
#endif
#include <stdlib.h>
#include <errno.h>
#include <string.h>
 
//
// Local Includes
//
#include "com_rm5248_serial_SerialPort.h"
#include "com_rm5248_serial_SerialPort_SerialInputStream.h"
#include "com_rm5248_serial_SerialPort_SerialOutputStream.h"
 
//
// Struct Definitions
//
struct port_descriptor{
#ifdef __WIN32
HANDLE port;
#else
int port;
#endif
};
 
//
// Local Variables
//
struct port_descriptor** port_list = NULL;
int port_list_size;
 
//
// Helper Methods
//
static jint get_handle
(JNIEnv * env, jobject obj){
jfieldID fid;
jint array_pos;
jclass cls = (*env)->GetObjectClass( env, obj );
 
fid = (*env)->GetFieldID( env, cls, "handle", "I" );
if( fid == 0 ){
return -1;
}
 
array_pos = (*env)->GetIntField( env, cls, fid );
 
return array_pos;
}
 
//
// JNI Methods
//
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: openPort
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_openPort
(JNIEnv * env, jobject obj, jstring port){
struct port_descriptor* new_port;
int list_pos;
int found = 0;
const char* port_to_open;
jboolean iscopy;
 
port_to_open = (*env)->GetStringUTFChars( env, port, &iscopy );
 
if( port_list == NULL ){
port_list = malloc( sizeof( struct port_descriptor* ) * 10 );
port_list_size = 10;
for( list_pos = 0; list_pos < port_list_size; ++list_pos ){
port_list[ list_pos ] = NULL;
}
}
 
//Search thru the port_list, find the first one that's not NULL
for( list_pos = 0; list_pos < port_list_size; ++list_pos ){
if( port_list[ list_pos ] != NULL ){
found = 1;
break;
}
}
 
if( !found ){
//no free slots. Expand our array by 10 elements
struct port_descriptor** tmpPortDesc;
tmpPortDesc = malloc( sizeof( struct port_descriptor* ) * ( port_list_size + 10 ) );
 
//put all elements into the new array
for( list_pos = 0; list_pos < port_list_size; ++list_pos ){
tmpPortDesc[ list_pos ] = port_list[ list_pos ];
}
++list_pos; //put the place to insert the new record one past the old records
 
port_list_size += 10;
//free the old array, set it to the new one
free( port_list );
port_list = tmpPortDesc;
}
 
//at this point, list_pos points to a NULL location in our array
new_port = malloc( sizeof( struct port_descriptor ) );
 
//Now, let's get to the actual opening of our port
#ifdef __WIN32
#else
new_port->port = open( port_to_open, O_RDWR );
if( new_port->port < 0 && errno == ENOENT ){
//That's not a valid serial port, error out
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "com/rm5248/serial/NoSuchPortException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
free( new_port );
return;
}else if( new_port->port < 0 ){
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "com/rm5248/serial/NoSuchPortException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
free( new_port );
return;
}
#endif /* __WIN32 */
 
port_list[ list_pos ] = new_port;
 
return list_pos;
}
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setBaudRate
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setBaudRate
(JNIEnv * env, jobject obj, jint baud_rate ){
jint array_pos;
struct port_descriptor* desc;
#ifdef __WIN32
#else
struct termios newio;
#endif /* __WIN32 */
 
array_pos = get_handle( env, obj );
 
if( array_pos < 0 ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return;
}
 
desc = port_list[ array_pos ];
 
if( desc == NULL ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return;
}
 
#ifdef __WIN32
#else
tcgetattr( desc->port, &newio );
 
switch( baud_rate ){
SPEED_SWITCH(0,newio);
SPEED_SWITCH(50,newio);
SPEED_SWITCH(75,newio);
SPEED_SWITCH(110,newio);
SPEED_SWITCH(134,newio);
SPEED_SWITCH(150,newio);
SPEED_SWITCH(200,newio);
SPEED_SWITCH(300,newio);
SPEED_SWITCH(600,newio);
SPEED_SWITCH(1200,newio);
SPEED_SWITCH(1800,newio);
SPEED_SWITCH(2400,newio);
SPEED_SWITCH(4800,newio);
SPEED_SWITCH(9600,newio);
SPEED_SWITCH(19200,newio);
SPEED_SWITCH(38400,newio);
SPEED_SWITCH(115200,newio);
}
 
if( tcsetattr( desc->port, TCSANOW, &newio ) < 0 ){
return 0;
}
#endif /* __WIN32 */
 
return 1;
 
}
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: doClose
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_rm5248_serial_SerialPort_doClose
(JNIEnv * env, jobject obj){
jint array_pos;
struct port_descriptor* desc;
 
array_pos = get_handle( env, obj );
 
desc = port_list[ array_pos ];
if( desc == NULL ){
return;
}
 
close( desc->port );
free( desc );
port_list[ array_pos ] = NULL;
}
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setStopBits
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setStopBits
(JNIEnv * env, jobject obj, jint bits){
return 1;
}
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setCharSize
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setCharSize
(JNIEnv * env, jobject obj, jint size){
 
return 1;
}
 
/*
* Class: com_rm5248_serial_SerialPort_SerialInputStream
* Method: readByte
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_00024SerialInputStream_readByte
(JNIEnv * env, jobject obj){
int stat;
char ret_val;
jint array_pos;
struct port_descriptor* desc;
 
array_pos = get_handle( env, obj );
if( array_pos < 0 ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return;
}
 
desc = port_list[ array_pos ];
 
if( desc == NULL ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return;
}
 
#ifdef __WIN32
#else
stat = read( desc->port, &ret_val, sizeof( ret_val ) );
if( stat < 0 ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return;
}
#endif
return ret_val;
}
 
/*
* Class: com_rm5248_serial_SerialPort_SerialInputStream
* Method: getAvailable
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_00024SerialInputStream_getAvailable
(JNIEnv * env, jobject obj){
jint ret_val;
jint array_pos;
struct port_descriptor* desc;
 
array_pos = get_handle( env, obj );
if( array_pos < 0 ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return;
}
 
desc = port_list[ array_pos ];
 
if( desc == NULL ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return;
}
 
#ifdef __WIN32
#else
if( ioctl( desc->port, FIONREAD, &ret_val ) < 0 ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return;
}
#endif
return ret_val;
}
 
/*
* Class: com_rm5248_serial_SerialPort_SerialOutputStream
* Method: writeByte
* Signature: (I)V
*/
JNIEXPORT void JNICALL Java_com_rm5248_serial_SerialPort_00024SerialOutputStream_writeByte
(JNIEnv * env, jobject obj, jint byte){
struct port_descriptor* desc;
jint array_pos;
char byte_write;
 
byte_write = byte;
 
array_pos = get_handle( env, obj );
if( array_pos < 0 ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return;
}
 
desc = port_list[ array_pos ];
 
if( desc == NULL ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return;
}
 
#ifdef __WIN32
#else
if( write( desc->port, &byte_write, sizeof( byte_write ) ) < 0 ){
//throw new exception
jclass exception_class;
(*env)->ExceptionDescribe( env );
(*env)->ExceptionClear( env );
exception_class = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exception_class, strerror( errno ) );
return;
}
#endif
 
}
 
/JavaSerial/NativeCode/com_rm5248_serial_SerialPort.h
0,0 → 1,53
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_rm5248_serial_SerialPort */
 
#ifndef _Included_com_rm5248_serial_SerialPort
#define _Included_com_rm5248_serial_SerialPort
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_rm5248_serial_SerialPort
* Method: openPort
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_com_rm5248_serial_SerialPort_openPort
(JNIEnv *, jobject, jstring);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setBaudRate
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setBaudRate
(JNIEnv *, jobject, jint);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: doClose
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_rm5248_serial_SerialPort_doClose
(JNIEnv *, jobject);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setStopBits
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setStopBits
(JNIEnv *, jobject, jint);
 
/*
* Class: com_rm5248_serial_SerialPort
* Method: setCharSize
* Signature: (I)Z
*/
JNIEXPORT jboolean JNICALL Java_com_rm5248_serial_SerialPort_setCharSize
(JNIEnv *, jobject, jint);
 
#ifdef __cplusplus
}
#endif
#endif
/JavaSerial/NativeCode/Makefile
0,0 → 1,94
#
# Created by gmakemake x86 (Feb 24 2012) on Sat Aug 4 19:31:04 2012
#
 
#
# Definitions
#
 
.SUFFIXES:
.SUFFIXES: .a .o .c .C .cpp .s .S
.c.o:
$(COMPILE.c) $< -o $@
.C.o:
$(COMPILE.cc) $< -o $@
.cpp.o:
$(COMPILE.cc) $< -o $@
.S.s:
$(CPP) -o $*.s $<
.s.o:
$(COMPILE.cc) $< -o $@
.c.a:
$(COMPILE.c) -o $% $<
$(AR) $(ARFLAGS) $@ $%
.C.a:
$(COMPILE.cc) -o $% $<
$(AR) $(ARFLAGS) $@ $%
.cpp.a:
$(COMPILE.cc) -o $% $<
$(AR) $(ARFLAGS) $@ $%
 
CC = gcc
CXX = g++
 
RM = rm -f
AR = ar
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
LINK.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS)
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) -c
COMPILE.cc = $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c
 
########## Default flags (redefine these with a header.mak file if desired)
CPPFLAGS = -DPIC -I$(JNI_INCLUDE_DIR) -I$(JNI_INCLUDE_DIR)/linux
CXXFLAGS =
CFLAGS = -FPIC
CLIBFLAGS = -lm
CCLIBFLAGS =
########## End of default flags
 
 
CPP_FILES =
C_FILES = SerialPortImpl.c
PS_FILES =
S_FILES =
H_FILES = com_rm5248_serial_SerialPort.h com_rm5248_serial_SerialPort_SerialInputStream.h com_rm5248_serial_SerialPort_SerialOutputStream.h
SOURCEFILES = $(H_FILES) $(CPP_FILES) $(C_FILES) $(S_FILES)
.PRECIOUS: $(SOURCEFILES)
OBJFILES = SerialPortImpl.o
 
JNI_INCLUDE_DIR = /usr/lib/jvm/java-6-sun/include
 
#
# Main targets
#
linux: SerialPortImpl.o
gcc -o librmserial.so -shared -Wl,-soname,librmserial.so -I$(JNI_INCLUDE_DIR) -I$(JNI_INCLUDE_DIR)/linux -static -lc
#
# Dependencies
#
 
SerialPortImpl.o: SerialPortImpl.c com_rm5248_serial_SerialPort.h \
com_rm5248_serial_SerialPort_SerialInputStream.h \
com_rm5248_serial_SerialPort_SerialOutputStream.h
 
#
# Installation Targets
#
 
install:
/usr/bin/install /usr/bin
 
#
# Housekeeping
#
 
Archive: archive.tgz
 
archive.tgz: $(SOURCEFILES) Makefile
tar cf - $(SOURCEFILES) Makefile | gzip > archive.tgz
 
clean:
-/bin/rm $(OBJFILES) core 2> /dev/null
 
realclean: clean
-/bin/rm -rf