0% found this document useful (0 votes)
54 views69 pages

Android HAL

The document provides an overview of the Hardware Abstraction Layer (HAL) in Android, detailing its role in separating the Android framework from the Linux kernel and defining hardware control interfaces. It also discusses the Android Native Development Kit (NDK) and the Android Native Server, including how to run native applications on Android. Additionally, it covers the structure and implementation of HAL and NDK, along with examples of power control management in Android systems.

Uploaded by

smcorporation23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views69 pages

Android HAL

The document provides an overview of the Hardware Abstraction Layer (HAL) in Android, detailing its role in separating the Android framework from the Linux kernel and defining hardware control interfaces. It also discusses the Android Native Development Kit (NDK) and the Android Native Server, including how to run native applications on Android. Additionally, it covers the structure and implementation of HAL and NDK, along with examples of power control management in Android systems.

Uploaded by

smcorporation23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 69

Outline

• Hardware Abstraction Layer Introduction


• Hardware Native Development Kit
• Android Native Server
• LAB : Run Native Application on Android

2
• Hardware Abstraction Layer Introduction
• Hardware Native Development Kit
• Android Native Server
• LAB : Run Native Application on Android

3
Hardware Abstraction Layer Introduction

Java Native Interface

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

Process for c Application

3rd Library Some 3rd library like ffmpeg, vlc, etc

User Space Libc.so Standard C library


System Call:open(/dev/fb0,ioctl,…)

Kernel Space
Kernel driver Linux kernel

Hardware

7
Hardware Abstraction Layer Introduction
• Android System for legacy
Android
Process for java Application

Android Framework Android SDK API


Process for c
Handle
3rd Library Dalvik virtual machine thread and process
JNI
3rd Library
Some library
Libc.so Libc.so
*.so

Hardware Abstraction Layer Shared library


module

Kernel driver Kernel driver


Hardware Hardware
General linux Legacy android HAL
8
Hardware Abstraction Layer Introduction
• Android System for HAL Stub
Android
Process for java Application

Android Framework Android SDK API


Runtime service
Handle
Dalvik virtual machine thread and process
JNI
3rd Library
Some library
Libc.so
Native service *.so

Hardware Abstraction Layer Shared library


stub stub module

Kernel driver
Hardware

9
Hardware Abstraction Layer Introduction
• Android Layer Analysis

Layer Language Form Ship


Application JAVA *.apk *.apk/system.img
Framework JAVA *.jar system.img
Libraries C/C++ *.so system.img
HAL C/C++ *.so system.img
Kernel C/asm *.ko uImage

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

Application -> Dalvik virtual machine Application ->


JNI-> Android Framework ->
Lib -> JNI Runtime Service ->
Kernel Lib ->
3rd Library kernel
Libc.so

Hardware Abstraction Layer

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

goToSleep ()-> goToSleepWithReaso() -> goToSleepLocked() -> setPowerState() ->


updateNativePowerStateLocked();

private void updateNativePowerStateLocked() {


nativeSetPowerState(
(mPowerState & SCREEN_ON_BIT) != 0,
(mPowerState & SCREEN_BRIGHT) == SCREEN_BRIGHT);
}

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 },
};

static void android_server_PowerManagerService_nativeSetPowerState(JNIEnv* env, jobject serviceObj,


jboolean screenOn, jboolean screenBright) {
set_screen_stage(on);
}

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" };

int set_screen_state(int on) {


...
len = write(g_fds[REQUEST_STATE], buf, len);
if(len < 0) {
LOGE("Failed setting last user activity: g_error=%d\n", g_error);
}
...
}

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

JAVA type C type


jboolean boolean
jint int
jlong long
jdouble double
jfloat float
jchar char
Jstring string

static void android_server_PowerManagerService_nativeSetPowerState(JNIEnv* env, jobject serviceObj,


jboolean screenOn, jboolean screenBright) {
set_screen_stage(on);
}

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

Process for java


Application
Android Framework

Runtime service Dalvik virtual machine


JNI
Native service Binding 3rd Library
Libc.so

Native Native Native


service service service Hardware Abstraction Layer
stub stub
HAL Library HAL Library HAL Library
Kernel driver
Kernel driver Hardware
22
Hardware Abstraction Layer Introduction
• Each hardware must implement the interface in stub format for using
in android service
• Stub format
– defined in android/hardware/libhardware/include/hardware/hardware.h
• hw_module_t
• hw_module_method_t
• hw_device_t
– defined in android/hardware/libhardware/hardware.c
• Load()
– Use dlopen() to load *.so
• hw_get_module()
– Get module information and call load() function

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

Runtime Service JNI Table Native service HAL HAL Stub


1
2
Init function 3
Get system JNINativeMrthod
4
service
Hw_get_module() dlopen
Hw_module_t
Hw_module_t
SDK

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

static android_server_PowerManagerService_nativeInit(JNIEnv *env, jclass clazz)


{
module_t const * module;
hw_get_module(HARDWARE_MODULE_ID, (const hw_module_t**)&module);
return 0;
}
<android_source>/framework/base/services/jni
<android_source>/framework/base/core/jni

• Hal Stub

int hw_get_module(const char *id, const struct hw_module_t **module)


{
status = load(id, path, module);
return status;
}
<android_source>/hardware/libhardware/hardwar.c

27
Hardware Abstraction Layer Introduction
Android Activity1 Android Activity1 Android Activity1

Power Manger
<Android_src>/frameworks/base/core/java/android/os/PowerManager.java

Power Manger Service


<Android_src>/frameworks/base/services/java/com/android/server/PowerManagerService.java

Power Java Native Interface


<Android_src>/frameworks/base/core/jni/android_os_Power.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

5. Generate so file on “android-ndk-r6/samples/hello-jni/libs/armeabi”


34
Hardware Native Development Kit
• All parameters to 'ndk-build‘
– ndk-build
• rebuild required machine code.
– ndk-build clean
• clean all generated binaries.
– ndk-build V=1
• launch build, displaying build commands.
– ndk-build NDK_DEBUG=1
• generate debuggable native code.
• On java code

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

Package name Class name


Lab : Run Native Application on Android
• Use Android Native Development Kit
– Android.mk

– Application.mk
Lab : Run Native Application on Android
• Use Android Native Development Kit
– Build the library

Put the library into <android_fs>/system/lib/


Lab : Run Native Application on Android
• In eclipse, File >> New >> Android Project
Lab : Run Native Application on Android
• Use Android Native Development Kit
– Use in Apk source
Lab : Run Native Application on Android
• Click Right on android project >> Run as >> Android
Application
– Test Result : the text is returned from C code
Lab : Run Native Application on Android
• From /home/mmn/workspace/mmn/bin , you can find out
the mmn.apk .
• Copy the apk file to <android_fs>/system/app/
• Or Copy the apk file to android_fs/data/app/
• Run your apk on devkit8000 and play video!
• Note: if the apk is updated , you have to restart booting
devkit8000
Step1:Use NDK to build the native library for android

• Reference script
Step2:Put library into android filesystem

• Successful message

• Copy the library to android filesystem


– $sudo cp app/helloqq/project/libs/armeabi/libhelloqq.so
<android_fs>/system/lib/
Step3:Load library in apk source code
Step4:Put the modified apk and socket-server into android filesystem

• After compile the apk source the apk will appear under
<apk_project>/bin/xxx.apk
*Step5:Run Native Application on Android

• Compile and put the socket-server to


<android_fs>/system/bin/
• Write a native service in <android_fs>/init.rc
• Reboot devkit8000
• Run your apk to see whether the socket-server do
something or not.
(refer to system/etc/omap_android.sh)
• Introduction to Android
• Android Architecture
• Android Multimedia Framework
• Android Porting
• Android start-up programming
• LAB : Mount Android Filesystem

61
Android start-up program
• What happened during Android booting stage ?(con’d)

From Korea Android Community- www.kandroid.org

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

You might also like