android_serialport_api hacking
/************************************************************************************
*
* 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的更多相关文章
- ★Kali信息收集~ 1.Google Hacking + Github Hacking
一.google hacking site site:cnblogs.com 毒逆天 intitle intitle:login allintitle allintitle:index of alli ...
- 狗汪汪玩转无线电 -- GPS Hacking
狗汪汪玩转无线电 -- GPS Hacking Kevin2600 · 2015/12/09 10:12 0x00 序 GPS Hacking 在过去几年的安全会议上一直都是很受关注的议题. 但往往因 ...
- GnuRadio Hacking②:使用SDR嗅探北欧芯片无线键盘鼠标数据包
0×00 前言 上半年的时候安全公司Bastille Networks(巴士底狱)安全研究员发现大多数无线鼠标和接收器之间的通信信号是不加密的,黑客可对一两百米范围内存在漏洞的无线键鼠进行嗅探甚至劫持 ...
- GnuRadio Hacking①:使用GnuRadio+SDR破解固定码无线遥控
0×01 信号捕获 在这篇文章中,我们将使用GnuRadio+SDR硬件对某品牌型号的无线跳蛋进行无线重放攻击的演示. 市面上常见的无线遥控工作的频段,通常工作在315Mhz.433Mhz,也有少数的 ...
- GSM Hacking Part② :使用SDR捕获GSM网络数据并解密
0×00 在文章第一部分 GSM Hacking Part① :使用SDR扫描嗅探GSM网络 搭建了嗅探GSM流量的环境,在第二部中,我们来讨论如何捕获发短信以及通话过程中的流量,从捕获到的数据中解密 ...
- 移动安全初探:窃取微信聊天记录、Hacking Android with Metasploit
在这篇文章中我们将讨论如何获取安卓.苹果设备中的微信聊天记录,并演示如何利用后门通过Metasploit对安卓设备进行控制.文章比较基础.可动手性强,有设备的童鞋不妨边阅读文章边操作,希望能激发大家对 ...
- Redis代码阅读之Hacking Strings
Hacking Strings The implementation of Redis strings is contained in sds.c ( sds stands for Simple Dy ...
- RFID Hacking④:使用ProxMark3 破解门禁
文中提及的部分技术可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使用! 0×00 前言 国际黑客大会Defcon传统之一:开锁!因为黑客认为锁也是一种安全挑战.我们在黑客题材电影.电视剧中也常常 ...
- Rootkit Hacking Technology && Defence Strategy Research
目录 . The Purpose Of Rootkit . Syscall Hijack . LKM Module Hidden . Network Communication Hidden . Fi ...
随机推荐
- [设计模式][c++]状态切换模式
转自:http://blog.csdn.net/yongh701/article/details/49154439 状态模式也是设计模式的一种,这种设计模式思想不复杂,就是实现起来的代码有点复杂.主要 ...
- Codeforces 496D - Tennis Game
496D - Tennis Game 思路:枚举每个t,求出对应的满足条件的s. 代码: #include<bits/stdc++.h> using namespace std; #def ...
- C# DataTable按指定列排序
C#提供的内置对象DataTable功能特别的强大,如果我们需要对DataTable中的某一列进行排序怎么处理呢,具体代码如下: DataTable dt = new DataTable(); dt. ...
- Ubuntu 16.04下docker ce的安装
卸载版本的docker sudo apt-get remove docker docker-engine docker.io 安装可选内核模块 从 Ubuntu 14.04 开始,一部分内核模块移到了 ...
- jsp post/get中接处理
jsp post/get中接处理 以参数:username为便 post接收中文比get接收中文要方便多了. <%@ page contentType="text/html;chars ...
- android--------动画之进度条
Android开发中在处理耗时工作的时候,例如:列表加载,大多数会有一个精度条加载的框,里面有一个像gif的图片在旋转一样. 效果图: <!-- 根标签为animation-list ...
- UVA-12558 Egyptian Fractions (HARD version) (IDA* 或 迭代加深搜索)
题目大意:经典的埃及分数问题. 代码如下: # include<iostream> # include<cstdio> # include<cstring> # i ...
- JavaScript In OA Framework
原文地址:JavaScript In OA Framework (需FQ) “To be or not to be…… is the question…..!” The famous soliloqu ...
- Arrow function restore
Arrow function restore 为什么叫Arrow Function?因为它的定义用的就是一个箭头: x => x * x 上面的箭头函数相当于: function (x) { r ...
- [转]MVC Html.AntiForgeryToken() 防止CSRF攻击
[转]MVC Html.AntiForgeryToken() 防止CSRF攻击 本文转自:http://blog.csdn.net/luck901229/article/details/8261640 ...