小編給大家分享一下Android驅(qū)動開發(fā)的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
10年積累的成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有蒲縣免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。以下均在Android Linux內(nèi)核目錄下操作, 在drivers目錄下創(chuàng)建驅(qū)動目錄hello
這個目下要創(chuàng)建3個文件,hello.c, Makefile and Kconfig
1.1 hello.ccd 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 endobj-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 endsource "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 addsource "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.
以下是需要操作的目錄在AOSP目錄下
2.1 create the application on externel
在external下創(chuàng)建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 applicationmmm external/hello/
Add it into the system.imgmake snod
2.3 Modify the authority of the applicationvim 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:
C:\Users\ylgi>adb devices List of devices attached 01059f9781509a67 device C:\Users\ylgi>adb -s 01059f9781509a67 shell bullhead:/ $ ls
cd /system/bin ./hello
運行效果如上圖,表示Linux驅(qū)動已經(jīng)成功加載并運行。
以下是需要操作的目錄在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
__BEGIN_DECLS
#define HELLO_HARDWARE_MODULE_ID "hello"http://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 filevim 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 modulemmm hardware/libhardware/modules/hello
then check the so file in the path:ll out/target/product/bullhead/system/lib/hw/hello.default.so
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.cppcd 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 modulemmm frameworks/base/services/core/jni
mmm frameworks/base/services/core/jni
make snod
out/target/product/bullhead/system/lib/libandroid_servers.so
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到servicemanagercd 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 實現(xiàn)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以后的系統(tǒng)引入了SELinux,要想讓JNI可以成功的訪問/dev/hello硬件就必須修改SELinux的策略,否則Android系統(tǒng)再啟動是就是出現(xiàn)
add_service ('hello',4e) uid=1000 - PERMISSION DENIED 的錯誤信息。
AOSP中,SELinux相關(guān)的策略配置文件保存在 /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 建議執(zhí)行一次 make update-api ,然后重新執(zhí)行 make 進行編譯make -j4
5.6 Double check the service
To double check if the serivce works:service list | grep hello
6.1 create the NfcPosTest applicatoin with button and edit box
copy android project NfcPosTest into the packages/experimental/
代碼中需要調(diào)用到:
import android.os.IHelloService;
//請求hello_service
private IHelloService helloService = null;
helloService = IHelloService.Stub.asInterface(
ServiceManager.getService("hello"));
//調(diào)用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
6.4 update system.img
make snod
以上是“Android驅(qū)動開發(fā)的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。