/************************************************************************************
*
* 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 14.04(desktop amd 64) nginx 安装启动停止

    sudo apt-get install nginx 关闭: sudo service nginx stop 启动: sudo nginx

  2. PHP概率,抽奖

    随机数,游标卡尺. 一个大饼,随机到哪个坑里,就中哪个奖. 大转盘! 公平公正! // 获取锦鲤 public function getGoldPig() { $pig_gift_bag = [ [ ...

  3. [原]关于phycis集成到osgearth的(瞎写写)

    基于全球的物理系统集成技术 引言 随着····· 概述 基于osgEarth渲染引擎,引入先进的物理引擎physics. 本篇主要讲述:原理和解决思路. 原理要点: 空间坐标转换 物理引擎与渲染引擎同 ...

  4. Xcode集成POD教程

    http://www.cocoachina.com/ios/20150410/11526.html COCOAPODS的网站上有很多非常好用的资源,这里来说一下如何把POD集成到我们的Xcode项目中 ...

  5. 第一次学习 CG( c for graphic) 遇到的一大推坑

    1.CG开发环境的配置: 具体的工具包下载及整体的配置过程可以参考:https://blog.csdn.net/seamanj/article/details/8300936. 上面网址的内容是对VS ...

  6. 通过IIS寄宿WCF服务

    WCF全面解析一书中的例子S104,直接将Service目录部署到iis是无法得到服务相应的,需要在项目中新建一个web项目,删除掉自动生成的所有文件之后,把Service目录下的Calculator ...

  7. LeetCode--070--爬楼梯

    problem description: 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 ...

  8. codeforces 555b//Case of Fugitive// Codeforces Round #310(Div. 1)

    题意:有n-1个缝隙,在上面搭桥,每个缝隙有个ll,rr值,ll<=长度<=rr的才能搭上去.求一种搭桥组合. 经典问题,应列入acm必背300题中.属于那种不可能自己想得出来的题.将二元 ...

  9. php--------使用js生成二维码

    php生成二维码有多种方式,可以在JS中,也可以使用php库,今天写的这个小案例是使用JS生成二维码. 其他方式可以看下一篇文章:php--------php库生成二维码和有logo的二维码 网站开发 ...

  10. ccf画图

    问题描述 在一个定义了直角坐标系的纸上,画一个(x1,y1)到(x2,y2)的矩形指将横坐标范围从x1到x2,纵坐标范围从y1到y2之间的区域涂上颜色. 下图给出了一个画了两个矩形的例子.第一个矩形是 ...