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

android_serialport_api hacking的更多相关文章

  1. ★Kali信息收集~ 1.Google Hacking + Github Hacking

    一.google hacking site site:cnblogs.com 毒逆天 intitle intitle:login allintitle allintitle:index of alli ...

  2. 狗汪汪玩转无线电 -- GPS Hacking

    狗汪汪玩转无线电 -- GPS Hacking Kevin2600 · 2015/12/09 10:12 0x00 序 GPS Hacking 在过去几年的安全会议上一直都是很受关注的议题. 但往往因 ...

  3. GnuRadio Hacking②:使用SDR嗅探北欧芯片无线键盘鼠标数据包

    0×00 前言 上半年的时候安全公司Bastille Networks(巴士底狱)安全研究员发现大多数无线鼠标和接收器之间的通信信号是不加密的,黑客可对一两百米范围内存在漏洞的无线键鼠进行嗅探甚至劫持 ...

  4. GnuRadio Hacking①:使用GnuRadio+SDR破解固定码无线遥控

    0×01 信号捕获 在这篇文章中,我们将使用GnuRadio+SDR硬件对某品牌型号的无线跳蛋进行无线重放攻击的演示. 市面上常见的无线遥控工作的频段,通常工作在315Mhz.433Mhz,也有少数的 ...

  5. GSM Hacking Part② :使用SDR捕获GSM网络数据并解密

    0×00 在文章第一部分 GSM Hacking Part① :使用SDR扫描嗅探GSM网络 搭建了嗅探GSM流量的环境,在第二部中,我们来讨论如何捕获发短信以及通话过程中的流量,从捕获到的数据中解密 ...

  6. 移动安全初探:窃取微信聊天记录、Hacking Android with Metasploit

    在这篇文章中我们将讨论如何获取安卓.苹果设备中的微信聊天记录,并演示如何利用后门通过Metasploit对安卓设备进行控制.文章比较基础.可动手性强,有设备的童鞋不妨边阅读文章边操作,希望能激发大家对 ...

  7. Redis代码阅读之Hacking Strings

    Hacking Strings The implementation of Redis strings is contained in sds.c ( sds stands for Simple Dy ...

  8. RFID Hacking④:使用ProxMark3 破解门禁

    文中提及的部分技术可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使用! 0×00 前言 国际黑客大会Defcon传统之一:开锁!因为黑客认为锁也是一种安全挑战.我们在黑客题材电影.电视剧中也常常 ...

  9. Rootkit Hacking Technology && Defence Strategy Research

    目录 . The Purpose Of Rootkit . Syscall Hijack . LKM Module Hidden . Network Communication Hidden . Fi ...

随机推荐

  1. Ubuntu更新软件源出现GPG error

    原因 在sources.list文件中加入了非ubuntu官方源,所以认为加入源是不可信任的. 解决方法导入该源公钥.E084DAB9为PUBKEY后八位 gpg --keyserver keyser ...

  2. 【转】VMware网络连接模式—桥接、NAT以及仅主机模式的详细介绍和区别

    ☞ 本文主要介绍软件『VMware Workstation(虚拟机)』的相关内容:VMware网络连接模式—桥接.NAT以及仅主机模式的详细介绍和区别. 其下列版本/分支可以参考本文: 全部版本/分支 ...

  3. Thunder团队--Alpha发布用户报告

    用户数量:12人 以下为用户评论:(注:为了保护用户的姓名权,以下用户名以昵称形式给出.) 用户名(昵称) 用户使用频次 用户评论(以图片展示) 小王 3次 米线 2次 孔小姐 5次 乌乌鸟 2次 永 ...

  4. Java JDK5新特性-可变参数

    2017-10-31 00:19:07 可变参数:定义方法的时候不知道该定义多少个参数 格式:修饰符 返回值类型 方法名(数据类型... 变量名){} 注意:这里的变量其实是一个数组          ...

  5. 关于Dos命令中存在中文的解决方法

    问题:当我们直接打开dos窗口并在里面写入中文时是没有问题的,但是当我们把这些命令放在bat文件中执行时就出问题了解决:1.首先可以通过pause命令来查看错误原因2.记事本默认是UTF-8格式的,而 ...

  6. php程序突然不能用file_get_contents()访问远程网址了?

    php程序用file_get_contents("http://www.***.com"),一直以来好好的,突然间就不能链接远程网址了,在shell下可以ping通远程网址,可是用 ...

  7. C#实现在应用程序间发送消息的方法示例

    本文实例讲述了C#实现在应用程序间发送消息的方法.分享给大家供大家参考,具体如下: 首先建立两个C#应用程序项目. 第一个项目包含一个Windows Form(Form1),在Form1上有一个But ...

  8. Professional layer CodeForces - 1103D (状压,gcd)

    大意: 给定$n$元素序列$a$, 现在想要让$gcd(a_1,a_2,...,a_n)=1$. 对于每个$a_i$可以除以一个不超过$k$的因子, 代价为$e_i$, 假设一共选择了$x$个元素去除 ...

  9. order by having group by

    1.group by 和having 的使用 SELECT *, count(`sku_quantity`)  as quantity FROM products  group by sku  hav ...

  10. JS-图片控制-动画管理模块

    animateManage.js ;(function(window,document,undefined){ var _aniQueue = [], //动画队列 --- ani:动画,Queue: ...