Android HAL
Android HAL
2
• Hardware Abstraction Layer Introduction
• Hardware Native Development Kit
• Android Native Server
• LAB : Run Native Application on Android
3
Hardware Abstraction Layer Introduction
4
Hardware Abstraction Layer Introduction
• Mentioned in Google I/O,2008
• HAL is used to separate :
– Android framework
– Linux kernel
• Define the hardware control interface
• Not having a standard format in Android development
5
Hardware Abstraction Layer Introduction
• what should we be concerned about user space and
kernel space on android base on Linux kernel
– general Linux operation system
• Include standard lib.so
• Dynamic library: *.so
• Static Library: *.a
– Android system
• Legacy android Hardware Abstraction Layer
– Define on <android_source>/hardware/libhardware_legacy
• HAL Stub android Hardware Abstraction Layer
– Define on <android_source>/hardware/libhardware
6
Hardware Abstraction Layer Introduction
• General Linux operation system
– Use c code
Kernel Space
Kernel driver Linux kernel
Hardware
7
Hardware Abstraction Layer Introduction
• Android System for legacy
Android
Process for java Application
Kernel driver
Hardware
9
Hardware Abstraction Layer Introduction
• Android Layer Analysis
10
Hardware Abstraction Layer Introduction
• Legacy android Hardware Abstraction Layer
– Define on <android_source>/hardware/libhardware_legacy
– The controlling hardware library compiler to *.so file which will be used
as shared library
– java call directly
• HAL Stub android Hardware Abstraction Layer
– Define on <android_source>/hardware/libhardware
– Use HAL module to direct function call
– libhardware/include
» Design interface, harader file
– libhardware/module
» Design reuse, override
– harware.c
» Load(), hw_get_module()
– libhardware/include/hardware/hardware.h
» Some structure
11
Hardware Abstraction Layer Introduction
• Legacy android Hardware Abstraction Layer
– many methods
Process for java
Android Framework
Kernel driver
Hardware
12
Hardware Abstraction Layer Introduction
• Example: Power Control In android
– Reference to http://developer.android.com/reference/android/os/PowerManager.html
– There is a set of API for Power Management
13
Hardware Abstraction Layer Introduction
• Example: Power Control In android
• Using the Power Management API
– In eclipse
14
Hardware Abstraction Layer Introduction
• Example: Power Control In android framework
• Trace the power management code:
– Android/frameworks/base/core/java/android/os/PowerManager.java
15
Hardware Abstraction Layer Introduction
• Example: Power Control In android runtime service
• Trace the power management code:
– Android/frameworks/base/services/java/com/android/server/PowerManagerService.java
16
Hardware Abstraction Layer Introduction
• Example: Power Control In JNI Table
• Trace the power management code:
– Android/frameworks/base/services/services/jni/com_android_server_PowerManagerServi
ce.cpp
static JNINativeMethod gPowerManagerServiceMethods[] = {
/* name, signature, funcPtr */
{ "nativeInit", "()V“, (void*) android_server_PowerManagerService_nativeInit },
{ "nativeSetPowerState", "(ZZ)V“, (void*) android_server_PowerManagerService_nativeSetPowerState },
{ "nativeStartSurfaceFlingerAnimation", "(I)V“, (void*)
android_server_PowerManagerService_nativeStartSurfaceFlingerAnimation },
};
Android/hardware/libhardware_legacy/power/power.c(HAL)
17
Hardware Abstraction Layer Introduction
• Example: Power Control In JNI Table
Java類型 符號
Boolean Z
Byte B
Char C
Short S
Int I
Long L
Float F
Double D
Void V
18
Hardware Abstraction Layer Introduction
• Example: Power Control In android (Legacy_HAL)
<in power.c>
const char * const NEW_PATHS[] = { "/sys/power/wake_lock", "/sys/power/wake_unlock", "/sys/power/state" };
Android/hardware/libhardware_legacy/power/power.c(HAL)
19
Hardware Abstraction Layer Introduction
• Example: Power Control In android
Service
Android Runtime
-JNI Table
-Function mapping
JNI
HAL
20
Hardware Native Development Kit
• Use Android Native Development Kit
– Type conversion from Java to C
21
Hardware Abstraction Layer Introduction
• HAL Stub android Hardware Abstraction Layer
– Stub provide operations and callbacks of hardware
– Services use hardware module ID to get information and methods
23
Hardware Abstraction Layer Introduction
• Each hardware must implement the interface in stub format for using
in android service
typedef struct hw_module_t {
uint32_t tag;
uint16_t version_major;
uint16_t version_minor;
const char *id;
const char *name;
const char *author; typedef struct hw_device_t {
/** Modules methods */ uint32_t tag;
struct hw_module_methods_t* methods; uint32_t version;
} hw_module_t; struct hw_module_t* module;
uint32_t reserved[12];
int (*close)(struct hw_device_t* device);
typedef struct hw_module_methods_t { } hw_device_t;
/** Open a specific device */
int (*open)(const struct hw_module_t* module, const
char* id,
struct hw_device_t** device);
} hw_module_methods_t;
24
Hardware Abstraction Layer Introduction
• HAL Stub android Hardware Abstraction Layer
Control method()
Control system
call
Return status
Return status
25
Hardware Abstraction Layer Introduction
• runtime service
– Java code
– Define on
<android_source>/framework/base/services/java/com/android/server
– Define “private static native”
private native void nativeInit();
private native void nativeSetPowerState(boolean screenOn, boolean screenBright);
private native void nativeStartSurfaceFlingerAnimation(int mode);
• JNI Table
static JNINativeMethod gPowerManagerServiceMethods[] = {
/* name, signature, funcPtr */
{ "nativeInit", "()V“, (void*) android_server_PowerManagerService_nativeInit },
{ "nativeSetPowerState", "(ZZ)V“, (void*) android_server_PowerManagerService_nativeSetPowerState },
{ "nativeStartSurfaceFlingerAnimation", "(I)V“, (void*)
android_server_PowerManagerService_nativeStartSurfaceFlingerAnimation },
};
<android_source>/framework/base/services/jni
<android_source>/framework/base/core/jni
26
Hardware Abstraction Layer Introduction
• Native service
• Hal Stub
27
Hardware Abstraction Layer Introduction
Android Activity1 Android Activity1 Android Activity1
Power Manger
<Android_src>/frameworks/base/core/java/android/os/PowerManager.java
HAL
<Android_src>/hardware/libhardware/hardward.c
HAL Stub
<Android_src>/hardware/libhardware/module/xxxx.c
Linux kernel power management
<kernel_src>/arch/arm/mach-omap2/pm.c
28
• Hardware Abstraction Layer Introduction
• Hardware Native Development Kit
• Android Native Server
• LAB : Run Native Application on Android
29
Hardware Native Development Kit
• Android Native Development Kit
– Android NDK is a companion tool to the Android SDK that lets
you build performance-critical portions of your apps in native
code
library
C code Android Java
NDK
30
Hardware Native Development Kit
• NDK
– Generate JNI dynamic Libraries (*.so)
– Put the file to correct path on android filesystem
• A set of tools and build files used to generate native code
libraries from C and C++ sources
• way to embed the corresponding native libraries into an
application package file
• The latest release of the NDK supports these ARM instruction
sets
– ARMv5TE
– ARMv7-A
– x86 instructions
31
Hardware Native Development Kit
• Download Android NDK
• http://developer.android.com/sdk/ndk/index.html
32
Hardware Native Development Kit
• NDK
– The NDK includes a set of cross-toolchains and sample
application
– Use Android,mk
.
|-- GNUmakefile
|-- README.TXT
|-- RELEASE.TXT
|-- build
|-- docs
|-- documentation.html
|-- ndk-build
|-- ndk-gdb
|-- ndk-stack
|-- platforms
|-- samples
|-- sources
|-- tests
|-- tmp
`-- toolchains
33
Hardware Native Development Kit
• Develop in NDK
1. Set Env “PATH”, “NDK_ROOT”, “NDK_Sample”
2. Edit your JNI code on android-ndk-r6/samples/hello-jni
3. Edit Android.mk file on android-ndk-r6/samples/hello-jni
• LOCAL_PATH
• LOCAL_MODULE
• LOCAL_SRC_FILES
• LOCAL_LDLIBS
• BUILD_SHARED_LIBRARY
4. Host$ ndk-build
Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
Gdbsetup : libs/armeabi/gdb.setup
Compile thumb : hello-jni <= hello-jni.c
SharedLibrary : libhello-jni.so
Install : libhello-jni.so => libs/armeabi/libhello-jni.so
static {
System.loadLibrary("hello-jni");
}
35
• Hardware Abstraction Layer Introduction
• Hardware Native Development Kit
• Android Native Server
• LAB : Run Native Application on Android
36
Android Native Server
• The android native service are defined in
<android_rootfs>/init.rc
• init.rc includes the startup services which run in android
background
– ex: mediaserver、network daemon 、bluetooth daemon、adb
daemon…etc.
37
Android Native Server
• The init.rc file will be loaded by <android_fs>/init as
filesystem mounted
• This is why the bootargs is like:
38
Android Native Server
• How to add service in init.rc ?
• Follow Init.rc format
– Actions
– Commands
– Services
– Options
39
Android Native Server
• Actions
– on <trigger>
– <command>
– <command>
• EX:
– on boot
– setprop persist.sys.keylayout gpio-keys
– mkdir /data/misc/dhcp 0770 dhcp dhcp
– chmod 0770 /data/misc/dhcp
40
Android Native Server
• Triggers
– Boot
– <name>=<value>
– service-exited-<name>
– device-added-<path>
– device-removed-<path>
41
Android Native Server
• Commands
– setprop <name> <value>
– trigger <event>
– chmod <octal-mode> <path>
– export <name> <value>
– class_start <serviceclass>
– class_stop <serviceclass>
– mkdir <path> [mode] [owner] [group]
– start <service>
– stop <service>
– insmod <modules>
42
Android Native Server
• Services
– service <name> <pathname> [ <argument> ]*
– <option>
– <option>
• EX:
– service sgx /system/etc/init.sgx.sh
– oneshot
43
Android Native Server
• Options
– critical
– user <username>
– oneshot
– class <name>
– onrestart <command>
44
Android Native Server
• Actual init.rc in <android_fs>/init.rc
45
• Android Framework Review
• Hardware Abstraction Layer Introduction
• Power Control Example
• In Progress HAL
• Android Native Development Kit
• Android Application Issue
• Android Native Services (init.rc)
• LAB : Run Native Application on Android
46
Lab files
• *socket.tar.gz
– Client.c
• Reference to this C code and build as library via NDK
– Server.c
• Build as an executable binary and define in init.rc
• android_ndk.tar.gz
– android-ndk-1.5_r1
Lab : Run Native Application on Android
• Use Android Native Development Kit
– Write a C code to be built as library
Include jni.h
Parameters passed in
Function name
Return type
– Application.mk
Lab : Run Native Application on Android
• Use Android Native Development Kit
– Build the library
• Reference script
Step2:Put library into android filesystem
• Successful message
• After compile the apk source the apk will appear under
<apk_project>/bin/xxx.apk
*Step5:Run Native Application on Android
61
Android start-up program
• What happened during Android booting stage ?(con’d)
62
Android start-up program
• What happened during Android booting stage ?(con’d)
• Kernel will execute “init” for starting android initialization
• “init” will read the init.rc file to set up the environment variable or
properties and start android services
• “init” is the first process after kernel started. The
corresponding source code lies in
<android_src>/system/core/init
63
Android start-up program
• Init.rc (under android_src/system/core/rootdir/init.rc)
events
64
Android start-up program
• “init” does the following tasks step by step:
• 1.Initialize log system.
• 2.Parse /init.rc
• 3.Execute early-init action parsed in step 2.
<init.c>
65
Android start-up program
• “init” does the following tasks step by step(con’d):
– 4.Device specific initialize. For example, make all device node in
/dev
– 5.Initialize property system. Actually the property system is
working as a share memory. Logically it looks like a registry
under Windows system.
– 6.Execute init action in the two files parsed in step 2.
66
Android start-up program
• “init” does the following tasks step by step(con’d):
• 7.Start property service.
• 8.Execute early-boot and boot actions in the two files parsed
in step 2.
• 9.Execute property action in init.rc parsed in step 2.
• 10.Enter into an indefinite loop to wait for device/property
set/child process exit events.
67
Android start-up program
• After init process , there are two main functions (Zygote,
System server)in booting:
• Zygote does the following tasks step by step:
• 1.Create JAVA VM.
• 2.Register android native function for JAVA VM.
• 3.Call the main function in the JAVA class named
com.android.internal.os.ZygoteInit
– Call Zygote::forkSystemServer (implemented in
dalvik/vm/native/dalvik_system_Zygote.c)to fork a new process.
• 4. call IPCThreadState::self()->joinThreadPool() to enter into service
dispatcher.
68
Android start-up program
• SystemServer will start a new thread to start all JAVA services as follows:
• Core Services:
– 1.Starting Power Manager
– 2.Creating Activity Manager
– 3.Starting Telephony Registry
– 4.Starting Package Manager
– 5.Set Activity Manager Service as System Process
– 6.Starting Context Manager
– 7.Starting System Context Providers
– 8.Starting Battery Service
– 9.Starting Alarm Manager
– 10.Starting Sensor Service
– 11.Starting Window Manager
– 12.Starting Bluetooth Service
– 13.Starting Mount Service
69
Android start-up program
• Booting diagram
70
Android start-up program
• Service in android
Services
Entropy Power Activity Telephony
Service Manager Manager Registry
Package Account Content Sys Cont.
Manager Manager Manager Provider
Battery Hardware Alarm Sensor
Service Service Manager Service
Window Bluetooth Clipboard
Status Bar
Manager Service Servce
Inp. Method NetStat Connectivity Accessibility
Service Service Service Manager
Notification Mount Dev. Storage Location
Manager Service Monitor Manager
Search Checkin Wallpaper Audio
Service Service Service Service
Headset Dock Backup AppWidget
Observer Observer Service Service
71