Android驱动开发---Linux Kernel/HAL Layer/Jni Layer实例全集

本人在开发Android Nfc POS之初,探索调试了一番驱动,目前在Nexus 5X 7.1.1上已经调成,之前的步骤可以参考我发的文章,Android驱动开发经验分享如下。

1. Linux 内核驱动实例

以下均在Android Linux内核目录下操作, 在drivers目录下创建驱动目录hello

这个目下要创建3个文件,hello.c, Makefile and Kconfig

1.1 hello.c

cd drivers

mkdir hello

vim hello.c

code as follows:

/*linux kernel driver: hello.c => /dev/hello */

/create the device file: /sys/class/hello/hello/

please find the code in the below. here skip the code to make the page clean and clear.

** 1.2 Makefile**

Create the Makefile and add:

obj-y += hello.o

1.3 Kconfig

Create Kconfig and add:


config HELLO

    tristate "Eric: First Android Driver"

default n

help

  This is the first Android driver.

this file is used when we make menuconfig.

1.4 Modify drivers/Makefile

Add following in the end

obj-y += hello.o

1.5 Add the driver into system configuration

Before we build the kernal, we need to config the system.

1.5.1 Modify arch/arm64/Kconfig

Add following in the end

source "drivers/hello/Kconfig"

It seems that this config not work, may be skipped.

1.5.2 Modify drivers/Kconfig

Following the menu:

menu "Device Drivers"

Please add

source "drivers/hello/Kconfig"

1.5.3 Modify drivers/Kconfig

make menuconfig

To enble the menu 'Eric: First Android Driver' in the "Device Drivers" item.

And save , then to build the linux kernel code.

2. 测试驱动

以下是需要操作的目录在AOSP目录下

2.1 create the application on externel

在external下创建hello目录

目录下将有两个文件:hello.c and Android.mk

cd external

mkdir hello

vim hello.c

/* AOSP app : ./external/hello.c =>/system/bin/hello*/

Android.mk as follows:


LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_MODULE := hello

LOCAL_SRC_FILES := $(call all-subdir-c-files)

include $(BUILD_EXECUTABLE)

2.2 build the application

To build the hello application

mmm external/hello/

Add it into the system.img

make snod

2.3 Modify the authority of the application

vim system/core/rootdir/ueventd.rc

Add ‘/dev/hello 0666 root root’ in the end of the file.

Build the AOSP before flash.

2.4 Test/Debug the driver hello

Before flash, we can build the AOSP again, of cource, don't forget replace the linux kernel file 'Image.gz-dtb'.

After flash into the phone, reboot the phone, and then do follows (on Windows command line):


C:\Users\ylgi>adb devices

List of devices attached

01059f9781509a67        device

C:\Users\ylgi>adb -s 01059f9781509a67 shell

bullhead:/ $ ls

image.png

cd /system/bin

./hello

image.png

运行效果如上图,表示Linux驱动已经成功加载并运行。

3. 驱动的HAL层实例

以下是需要操作的目录在AOSP目录下

3.1 create hello.c file


cd hardware/libhardware/modules

mkdir hello

vim hello.c

/* to implement the HAL*/

3.2 create hello.h file

vim hardware/libhardware/include/hardware/hello.h

/*add the implement */

#ifndef ANDROID_HELLO_INTERFACE_H

#define ANDROID_HELLO_INTERFACE_H

#include <hardware/hardware.h>

__BEGIN_DECLS

#define HELLO_HARDWARE_MODULE_ID "hello"//ID

struct hello_module_t {

    struct hw_module_t common;

};//hw_module_t的继承者

struct hello_device_t {

    struct hw_device_t common;

    int fd;

    int (*set_val)(struct hello_device_t* dev, int val);

    int (*get_val)(struct hello_device_t* dev, int* val);

};//hw_device_t的继承者

__END_DECLS

#endif

..............................

3.3 create Android.mk file

vim Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := hello.default

LOCAL_MODULE_RELATIVE_PATH := hw

LOCAL_SRC_FILES := hello.c

LOCAL_SHARED_LIBRARIES := liblog

LOCAL_MODULE_TAGS := optional

include $(BUILD_SHARED_LIBRARY)

3.4 build the hello module

mmm hardware/libhardware/modules/hello

then check the so file in the path:

ll out/target/product/bullhead/system/lib/hw/hello.default.so

4. HAL Layer之上Java的Jni硬件接口封装。

4.1 create com_android_server_HelloService.cpp

cd frameworks/base/services/core/jni

vim com_android_server_HelloService.cpp

其中:

  • HELLO_HARDWARE_MODULE_ID为hardware/libhardware/include/hardware/hello.h中定义的hello模块名字,

  • jniRegisterNativeMethods注册HelloService包路径为com.android.server.HelloService

4.2 Modify onload.cpp

cd frameworks/base/services/core/jni/

vim onload.cpp


namespace android {

........

int register_android_server_HelloService(JNIEnv *env);//added by eric.y

};

extern "C" jint JNI_OnLoad(JavaVM* vm, void* /* reserved */)

{

..............

register_android_server_HelloService(env);//add by eric

return JNI_VERSION_1_4;

}

4.3 Modify Android.mk

cd frameworks/base/services/core/jni/

vim Android.mk


LOCAL_SRC_FILES += \

    ....................

    $(LOCAL_REL_DIR)/com_android_server_HelloService.cpp \

    $(LOCAL_REL_DIR)/onload.cpp

4.4 build the jni module

mmm frameworks/base/services/core/jni

mmm frameworks/base/services/core/jni

make snod

out/target/product/bullhead/system/lib/libandroid_servers.so

5. JNI上层Service实例的实现,Framework接口

5.1 建立aidl通信接口;

AOSP目录下操作

cd frameworks/base/core/java/android/os/

vim IHelloService.aidl


package android.os;

interface IHelloService {

    void setVal(int val);

    int getVal();

}

**5.2 在system_server中注册hello_service到servicemanager **

cd frameworks/base/services/java/com/android/server/

vim SystemServer.java

try {

Slog.i(TAG, "Service: hello");

ServiceManager.addService("hello", new HelloService());//

} catch (Throwable e) {

Slog.e(TAG, "Failure starting Service: hello", e);

}

5.3 实现hello_service

create the service file

cd frameworks/base/services/core/java/com/android/server/

vim HelloService.java

/------Eric: hello service-----/


package com.android.server;

import android.content.Context;

import android.os.IHelloService;

public class HelloService extends IHelloService.Stub {

private static final String TAG = "HelloService";

HelloService() {

init_native();

}

public void setVal(int val) {

setVal_native(val);

}

public int getVal() {

return getVal_native();

}

//native implement from HAL layer

private static native boolean init_native();

private static native void setVal_native(int val);

private static native int getVal_native();

};

5.4 modify the Android.mk

cd frameworks/base/

vim Android.mk

add follow into LOCAL_SRC_FILES +=

core/java/android/os/IHelloService.aidl \

5.5 Build the service

mmm frameworks/base

5.6 Add into the SELinux

android5.0以后的系统引入了SELinux,要想让JNI可以成功的访问/dev/hello硬件就必须修改SELinux的策略,否则Android系统再启动是就是出现

add_service ('hello',4e) uid=1000 - PERMISSION DENIED 的错误信息。

AOSP中,SELinux相关的策略配置文件保存在 /external/sepolicy/中,为了完成我们这次实验,需要修改5个 .te 文件.

modify: system/sepolicy/service.te

Add following:

type hello_service, system_api_service, system_server_service, service_manager_type;

modify: external\sepolicy\service_contexts

Add following:

hello u:object_r:hello_service:s0

为了确保修改后的 .te 文件被成功的编译进system.img 建议执行一次 make update-api ,然后重新执行 make 进行编译

make -j4

5.6 Double check the service

To double check if the serivce works:

service list | grep hello

image.png

6. Application on the UI

6.1 create the NfcPosTest applicatoin with button and edit box

copy android project NfcPosTest into the packages/experimental/

代码中需要调用到:

import android.os.IHelloService;

//请求hello_service

private IHelloService helloService = null;

helloService = IHelloService.Stub.asInterface(

ServiceManager.getService("hello"));

//调用HAL接口

int val = helloService.getVal();

String text = String.valueOf(val);

valueText.setText(text);

6.2 Add the Android.mk

vim packages/experimental/NfcPosTest/Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := optional

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := NfcPosTest

include $(BUILD_PACKAGE)

6.3 build the application

build the demo:

mmm packages/experimental/NfcPosTest

succeeded check the apk file in the path:

ll out/target/product/bullhead/system/app/NfcPosDemo.apk

image.png

6.4 update system.img

make snod

7. Check NfcPosTest application in Android phone after flash succeeded.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,185评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,445评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,684评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,564评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,681评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,874评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,025评论 3 408
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,761评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,217评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,545评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,694评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,351评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,988评论 3 315
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,778评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,007评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,427评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,580评论 2 349

推荐阅读更多精彩内容