/************************************************************************************
*
* 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); }

最新文章

  1. 点击按钮跳出隐藏的div层,并设背景。
  2. 自动运维:Ansible -ansible tower
  3. 每天一个linux命令--locate
  4. 解决Selenium Webdriver执行测试时,每个测试方法都打开一个浏览器窗口的问题
  5. iOS开发项目之一 [ 项目流程]
  6. tomcat 禁用不安全的http请求模式 .
  7. Bootstrap-select:美化原生select
  8. 配置错误 在唯一密钥属性“fileExtension”设置为“.log”时,无法添加类型为“mimeMap”的重复集合项
  9. (译)如何在ASP.NET中安全使用ViewState
  10. python成长笔记
  11. and then set HOMEBREW_GITHUB_API_TOKEN.
  12. canvas绘图详解-06-绘制一个五角星-常用绘图原理
  13. [HDOJ2572]终曲
  14. XStream实现缓存
  15. 上海嘉韦思杯部分writeup
  16. navicat连接不上Linux服务器上的MySQL
  17. 总结 Linux 下安装 PHP 扩展步骤
  18. Vue.js 3.0 新特性预览
  19. jQuery开发工具
  20. bash编程-cut、printf

热门文章

  1. Qt5.3.2_CentOS6.4_x86_调试源码关联【勿删,简洁】
  2. 《剑指offer》第十五题(二进制中1的个数)
  3. RabbitMQ入门_06_深入了解ack
  4. CSS——图片替换方法:Fahrner图片替换法(FIR)
  5. C#代码安装、卸载、监控Windows服务
  6. C#实现在应用程序间发送消息的方法示例
  7. codefroces 450B矩阵快速幂
  8. XML Publisher Using API’s(转)
  9. 小议常被忽略的a标签:visited属性的特殊用法
  10. HDU 1936 区间贪心