Android 驅動測試程式H-M-S

weixin_34377065發表於2014-06-05

今天把hook狀態的值進行了讀取,並且往上傳遞.

步驟如下:

<1> : 新建一個android應用工程,然後在工程目錄下新建一個jni目錄,拷貝以前開發的onload.h和onload.cpp檔案,然後自行新建Android.mk,Application.mk指令碼檔案,然後新建getHook.cpp;

getHook.cpp內容如下:

#include<jni.h>
#include<fcntl.h>
#include<stdio.h>
#include<android/log.h>
#include<stdlib.h>
#include "onload.h"

#define TAG "READ_HOOK"
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO,TAG,__VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN,TAG,__VA_ARGS__))
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__))

#define DEVICE_NAME "/sys/class/hostpower/hpower/hook"
#define PACKAGE_NAME "org/HookClass"

namespace android {

bool getHookStatus() {

    int fd = -1;
    char* val;
    char buf[3];

    fd = open(DEVICE_NAME, O_RDWR);
    if (fd == -1) {

        LOGI("failed to open device %s.\n", DEVICE_NAME);
        return -1;

    }

    read(fd, buf, 4);

    LOGI("read value : %s\n", buf);

    close(fd);

    LOGI("read value : %c\n", buf[2]);

    if (buf[2] == '1') {
        return true;
    } else {
        return false;
    }

}

int getHookData() {

    int fd = -1;
    char* val;
    char buf[3];

    fd = open(DEVICE_NAME, O_RDWR);
    if (fd == -1) {

        LOGI("failed to open device %s.\n", DEVICE_NAME);
        return -1;

    }

    read(fd, buf, 4);
    LOGI("read value : %s\n", buf);
    close(fd);
    LOGI("read value : %c\n", buf[2]);

    if (buf[2] == '1') {
        return 1;
    } else {
        return 0;
    }

}

}

using namespace android;

static const JNINativeMethod method_table[] = {

{ "org_getHookStatus", "()Z", (void*) getHookStatus }, { "org_getHookData",
        "()I", (void*) getHookData }

};

int register_android_jni_demo_android(JNIEnv *env) {

    return jniRegisterNativeMethods(env, PACKAGE_NAME, method_table,
            sizeof(method_table) / sizeof(method_table[0]));

}

Android.mk檔案內容:

LOCAL_PATH:=$(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE:=gethooklib
LOCAL_SRC_FILES:=onload.cpp getHook.cpp
LOCAL_SHARED_LIBRARY := libnativehelper liblog
LOCAL_LDLIBS:=-llog

include $(BUILD_SHARED_LIBRARY)


<2> : 新建一個與jni相關介面的類:HookClass.java:

package org;

public class HookClass {

    static {
        System.loadLibrary("gethooklib");
    }

    public HookClass() {

    }

    public native boolean org_getHookStatus();

    public native int org_getHookData();

}

<3> : 新建一個getHook.cpp的檔案:

#include<jni.h>
#include<fcntl.h>
#include<stdio.h>
#include<android/log.h>
#include<stdlib.h>
#include "onload.h"

#define TAG "READ_HOOK"
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO,TAG,__VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN,TAG,__VA_ARGS__))
#define LOGE(...) ((void)__android_log_print(ANDROID_LOG_ERROR,TAG,__VA_ARGS__))

#define DEVICE_NAME "/sys/class/hostpower/hpower/hook"
#define PACKAGE_NAME "org/HookClass"

namespace android {

bool getHookStatus() {

    int fd = -1;
    char* val;
    char buf[3];

    fd = open(DEVICE_NAME, O_RDWR);
    if (fd == -1) {

        LOGI("failed to open device %s.\n", DEVICE_NAME);
        return -1;

    }

    read(fd, buf, 4);

    LOGI("read value : %s\n", buf);

    close(fd);

    LOGI("read value : %c\n", buf[2]);

    if (buf[2] == '1') {
        return true;
    } else {
        return false;
    }

}

int getHookData() {

    int fd = -1;
    char* val;
    char buf[3];

    fd = open(DEVICE_NAME, O_RDWR);
    if (fd == -1) {

        LOGI("failed to open device %s.\n", DEVICE_NAME);
        return -1;

    }

    read(fd, buf, 4);
    LOGI("read value : %s\n", buf);
    close(fd);
    LOGI("read value : %c\n", buf[2]);

    if (buf[2] == '1') {
        return 1;
    } else {
        return 0;
    }

}

}

using namespace android;

static const JNINativeMethod method_table[] = {

{ "org_getHookStatus", "()Z", (void*) getHookStatus }, { "org_getHookData",
        "()I", (void*) getHookData }

};

int register_android_jni_demo_android(JNIEnv *env) {

    return jniRegisterNativeMethods(env, PACKAGE_NAME, method_table,
            sizeof(method_table) / sizeof(method_table[0]));

}

<4> : 應用工程:

package com.example.androidgethookdemo;

import org.HookClass;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private Button mBtn;
    private HookClass mHook;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtn = (Button) findViewById(R.id.show);
        mBtn.setOnClickListener(this);

        mHook = new HookClass();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        int idn = arg0.getId();
        switch (idn) {
        case R.id.show:
            if (mHook.org_getHookStatus()) {
                Toast.makeText(
                        this,
                        "hook status : press !\nhook status value : "
                                + mHook.org_getHookData(), Toast.LENGTH_SHORT)
                        .show();
            } else {
                Toast.makeText(
                        this,
                        "hook status : release !\nhook status value : "
                                + mHook.org_getHookData(), Toast.LENGTH_SHORT)
                        .show();
            }
            break;
        }
    }

}

<5> : ndk-build以後,看看結果吧... ...

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相關文章