/************************************************************************************
*
* android_serialport_api hacking
*
* 声明:
* 1. 这是android_serialport_api的jni源代码解读;
* 2. 源代码url: https://code.google.com/p/android-serialport-api/
* 3. 可以从中知道JNI是如何查找类,创建对象,访问对象的属性等等内容;
*
*
* Copyright 2009-2011 Cedric Priscal
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
*
* 相关参考:
* 1. 访问JAVA中的字段(jfieldID): http://www.cnblogs.com/lijunamneg/archive/2012/12/22/2829023.html
* 2. JNIEnv解析: http://blog.csdn.net/freechao/article/details/7692239
* 3. The Java™ Native Interface Programmer’s Guide and Specification
*
*****************************************************************************/ #include <termios.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <jni.h> #include "SerialPort.h" #include "android/log.h" /**
* 定义一些宏,方便写调试代码
*/
static const char *TAG="serial_port";
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args) static speed_t getBaudrate(jint baudrate)
{
switch(baudrate) {
case : return B0;
case : return B50;
case : return B75;
case : return B110;
case : return B134;
case : return B150;
case : return B200;
case : return B300;
case : return B600;
case : return B1200;
case : return B1800;
case : return B2400;
case : return B4800;
case : return B9600;
case : return B19200;
case : return B38400;
case : return B57600;
case : return B115200;
case : return B230400;
case : return B460800;
case : return B500000;
case : return B576000;
case : return B921600;
case : return B1000000;
case : return B1152000;
case : return B1500000;
case : return B2000000;
case : return B2500000;
case : return B3000000;
case : return B3500000;
case : return B4000000;
default: return -;
}
} /*
* Class: com_android_aplex_SerialPort
* Method: open
* Signature: (Ljava/lang/String;II)Ljava/io/FileDescriptor;
*/
JNIEXPORT jobject JNICALL Java_com_android_aplex_SerialPort_open
(JNIEnv *env, jclass thiz, jstring path, jint baudrate, jint flags)
{ int fd;
speed_t speed;
jobject mFileDescriptor; //保存文件描述符的对象引用 // Check arguments
{
speed = getBaudrate(baudrate);
if (speed == -) {
// TODO: throw an exception
LOGE("Invalid baudrate");
return NULL;
}
} // Opening device
{
jboolean iscopy;
/**
* 将Java的字符串转换成C中的字符串
*/
const char *path_utf = (*env)->GetStringUTFChars(env, path, &iscopy);
LOGD("Opening serial port %s with flags 0x%x", path_utf, O_RDWR | flags);
fd = open(path_utf, O_RDWR | flags);
LOGD("open() fd = %d", fd);
/**
* 和前面的GetStringUTFChars一对用法,相当于malloc和free
*/
(*env)->ReleaseStringUTFChars(env, path, path_utf);
if (fd == -)
{
// Throw an exception
LOGE("Cannot open port");
// TODO: throw an exception
return NULL;
}
} // Configure device
{
struct termios cfg;
LOGD("Configuring serial port");
if (tcgetattr(fd, &cfg))
{
LOGE("tcgetattr() failed");
close(fd);
// TODO: throw an exception
return NULL;
} cfmakeraw(&cfg);
cfsetispeed(&cfg, speed);
cfsetospeed(&cfg, speed); if (tcsetattr(fd, TCSANOW, &cfg))
{
LOGE("tcsetattr() failed");
close(fd);
// TODO: throw an exception
return NULL;
}
} // Create a corresponding file descriptor
{
/**
* Returns a reference to the named class or interface.
* 这个相当于在当前虚拟机加载的所有的类中找这个类:java/io/FileDescriptor
*/
jclass cFileDescriptor = (*env)->FindClass(env, "java/io/FileDescriptor");
/**
* To obtain the method ID of a constructor, supply "<init>" as the method name and “V” as the return type.
* 获取类中的无参构造函数
*/
jmethodID iFileDescriptor = (*env)->GetMethodID(env, cFileDescriptor, "<init>", "()V");
jfieldID descriptorID = (*env)->GetFieldID(env, cFileDescriptor, "descriptor", "I");
/**
* Constructs a new object. The method ID indicates which constructor method to invoke. This ID may be obtained by calling
* GetMethodID with "<init>" as the method name and “V” as the return type. The constructor must be defined in the class
* referred to by clazz, not one of its superclasses.
* 生成一个类对象
*/
mFileDescriptor = (*env)->NewObject(env, cFileDescriptor, iFileDescriptor);
/**
* Sets the value of an instance field of an object. The obj reference must not be NULL.
* 设置对象的值
*/
(*env)->SetIntField(env, mFileDescriptor, descriptorID, (jint)fd);
} return mFileDescriptor; } /*
* Class: com_android_aplex_SerialPort
* Method: close
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_android_aplex_SerialPort_close
(JNIEnv *env, jobject thiz)
{
/**
* Returns the class of an object. The obj reference must not be NULL.
* thiz为java层传入的对象,GetObjectClass相当于获得这个对象的类,名字取得不错
*/
jclass SerialPortClass = (*env)->GetObjectClass(env, thiz);
/**
* FindClass initializes the class or interface it returns.
* 这个相当于在当前虚拟机加载的所有的类中找这个类:java/io/FileDescriptor
*/
jclass FileDescriptorClass = (*env)->FindClass(env, "java/io/FileDescriptor");
/**
* Returns the field ID for an instance field of a class
* 通过域名、域类型获取类对应的域ID号
*/
jfieldID mFdID = (*env)->GetFieldID(env, SerialPortClass, "mFd", "Ljava/io/FileDescriptor;");
jfieldID descriptorID = (*env)->GetFieldID(env, FileDescriptorClass, "descriptor", "I");
/**
* Returns the value of a field of an instance. The field to access is specified by a field ID.
* 通过对象对应域的ID号获取域对象,或者值
*/
jobject mFd = (*env)->GetObjectField(env, thiz, mFdID);
jint descriptor = (*env)->GetIntField(env, mFd, descriptorID); LOGD("close(fd = %d)", descriptor);
close(descriptor); }