Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Android driver Development

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you the example analysis of Android driver development, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

1. Linux kernel driver example

The following all operate under the Android Linux kernel directory and create the driver directory hello under the drivers directory

To create three files, hello.c and 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   nhelp 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.

two。 Test driven

The following is the directory to be operated under the AOSP directory

2.1 create the application on externel

Create a hello directory under external

There will be two files in the directory: 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: = optionalLOCAL_MODULE: = helloLOCAL_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:

C:\ Users\ ylgi > adb devicesList of devices attached01059f9781509a67 deviceC:\ Users\ ylgi > adb-s 01059f9781509a67 shellbullhead:/ $ls

Cd / system/bin./hello

The running effect is shown in the figure above, which indicates that the Linux driver has been loaded and run successfully.

3. Driven HAL layer instance

The following is the directory to be operated under the AOSP directory

3.1 create hello.c file

Cd hardware/libhardware/modulesmkdir hellovim 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" / / ID

Struct hello_module_t {

Struct hw_module_t common

}; / / successor to 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)

}; / / successor to 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. Jni hardware interface encapsulation of Java on HAL Layer.

4.1 create com_android_server_HelloService.cpp

Cd frameworks/base/services/core/jni

Vim com_android_server_HelloService.cpp

Where:

HELLO_HARDWARE_MODULE_ID is the name of the hello module defined in hardware/libhardware/include/hardware/hello.h

JniRegisterNativeMethods registers the HelloService package path as 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. Implementation of Service instance in the upper layer of JNI, Framework interface

5.1 Establishment of aidl communication interface

Operation under AOSP directory

Cd frameworks/base/core/java/android/os/

Vim IHelloService.aidl

Package android.os;interface IHelloService {void setVal (int val); int getVal ();}

5.2 registering hello_service to servicemanager in system_server

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 implement 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 layerprivate 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

SELinux is introduced into the system after android5.0. In order for JNI to successfully access / dev/hello hardware, the policy of SELinux must be modified, otherwise the reboot of Android system will appear.

Error message for add_service ('hello',4e) uid=1000-PERMISSION DENIED.

In AOSP, the policy configuration files related to SELinux are saved in / external/sepolicy/. In order to complete our experiment, we need to modify 5 .te files.

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

In order to ensure that the modified .te file is successfully compiled into system.img, it is recommended to execute make update-api once, and then re-execute make to compile

Make-J4

5.6 Double check the service

To double check if the serivce works:

Service list | grep hello

6. Application on the UI

6.1 create the NfcPosTest applicatoin with button and edit box

Copy android project NfcPosTest into the packages/experimental/

The code needs to call to:

Import android.os.IHelloService

/ / request hello_service

Private IHelloService helloService = null

HelloService = IHelloService.Stub.asInterface (

ServiceManager.getService ("hello"))

/ / call HAL API

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

7. Check NfcPosTest application in Android phone after flash succeeded. The above is all the contents of the article "sample Analysis of Android-driven Development". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report