4 Writing the HAL code
Source code Download method
First time:
git clone https://github.com/weidongshan/SYS_0001_LEDDemo.git
Update:
Git pull origin
Remove the specified version:
git checkout v1//have JNI without HAL
Git checkout v2//have Jni,hal
git checkout v3//Add MODULE tag, DEVICE tag
JNI provides local functions up, loads the HAL file down and calls the HAL's function
HAL is responsible for accessing the driver to perform hardware operations
Dlopen
EXTERNAL\CHROMIUM_ORG\THIRD_PARTY\HWCPLUS\SRC\HARDWARE.C (Reference code)
Hw_get_module ("led")
1. module name ==> file name
Hw_get_module_by_class ("led", NULL)
Name = "LED"
Property_get XXX is a property
Hw_module_exists determine if there is led.xxx.so
It is used to determine "name". " SubName ". So file exists
Directory to find:
A. Hal_library_path Environment variables
B./vendor/lib/hw
C./system/lib/hw
/VENDOR/LIB/HW This directory does not have files, tiny4412 so can only go to/system/lib/hw this directory to find
2. Loading
Load
Dlopen (filename)
Dlsym ("HMI") obtains a hw_module_t structure named HMI from the so file
strcmp (ID, hmi->id) determine if the name is the same (Hmi->id, "led")
V2:
(3) JNI: Re-upload
Frameworks/base/services/core/jni/com_android_server_ledservice.cpp
(4) Hal:led_hal.h
Led_hal.c
Upload the new file to the server, in the directory where:
Hardware/libhardware/include/hardware/led_hal.h
Hardware/libhardware/modules Create a directory led in this directory, put LED_HAL.C and write yourself a android.mk
Hardware/libhardware/modules/led/led_hal.c
Hardware/libhardware/modules/led/android.mk
ANDROID.MK content is as follows:
Local_path: = $ (call My-dir)
Include $ (clear_vars)
Local_module: = Led.default
Local_module_relative_path: = HW
Local_c_includes: = Hardware/libhardware
Local_src_files: = led_hal.c
Local_shared_libraries: = Liblog
Local_module_tags: = Eng
Include $ (build_shared_library)
Compile:
$ mmm frameworks/base/services
$ mmm hardware/libhardware/modules/led
$ make Snod
$./gen-img.sh
Final system.img file generation
About printing information:
A. There are three types of printing information: app, System, radio
The program uses ALOGX, SLOGX, RLOGX to print
B. x represents 6 print levels, which are:
V Verbose
D Debug
I Info
W Warn
E Error
F Fatal
Like what:
#define LOG_TAG "Ledhal"
Alogi ("Led_open:%d", FD);
C. The printed format is:
I/ledhal (1987): led_open:65
Level Log_tag Process Number Printing information
D. Use the Logcat command to view
Logcat ledhal:i *:s
Choose the information you are interested in:
Compared with the previous chapter, the main changes are
Com_android_server_ledservice.cpp
Added led_hal.h led_hal.c android.mk
Com_android_server_ledservice.cpp
#defineLog_tag "Ledservice"#include"jni.h"#include"JNIHelp.h"#include"android_runtime/androidruntime.h"#include<utils/misc.h>#include<utils/Log.h>#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<sys/ioctl.h>#includenamespaceandroid{Staticled_device_t*led_device;jint Ledopen (jnienv*env, Jobject CLS) {Jint err; hw_module_t*module; hw_device_t*device; Alogi ("Native Ledopen ..."); /*1. Hw_get_module*/Err= Hw_get_module ("led", (hw_module_tConst* *) &module); if(Err = =0) { /*2. Get Device:module->methods->open*/Err= Module->methods->open (module, NULL, &device); if(Err = =0) { /*3. Call Led_open*/Led_device= (led_device_t *) device; returnLed_device->Led_open (Led_device); } Else { return-1; } } return-1; }voidLedclose (JNIENV *env, Jobject CLS) { //Alogi ("native ledclose ..."); //Close (FD);}jint Ledctrl (jnienv*env, Jobject CLS, jint which, jint status) {Alogi ("native Ledctrl%d,%d", which, status); returnLed_device->Led_ctrl (Led_device, which, status);}Static ConstJninativemethod methods[] = { {"Native_ledopen","() I", (void*) Ledopen}, {"Native_ledclose","() V", (void*) Ledclose}, {"Native_ledctrl","(II) I", (void*) Ledctrl},}; intRegister_android_server_ledservice (JNIENV *env) { returnJniregisternativemethods (ENV,"Com/android/server/ledservice", Methods, Nelem (methods));}}
Led_hal.c
#defineLog_tag "Ledhal"/*1. Implement a hw_module_t structure called the HMI*//*2. Implement an open function that returns the led_device_t struct*//*3. Implementing the LED_DEVICE_T structure*//*Reference HARDWARE\LIBHARDWARE\MODULES\VIBRATOR\VIBRATOR.C*/#include#include#include<cutils/log.h>#include<stdio.h>#include<unistd.h>#include<fcntl.h>#include<errno.h>#include#include<stdlib.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<sys/ioctl.h>#include<utils/Log.h>Static intFD;/** Close This device*/Static intLed_close (structhw_device_t*device) {Close (FD); return 0;}Static intLed_open (structled_device_t*Dev) {FD= Open ("/dev/leds", O_RDWR); Alogi ("Led_open:%d", FD); if(FD >=0) return 0; Else return-1;}Static intLed_ctrl (structled_device_t* Dev,intWhich,intstatus) { intRET =IOCTL (FD, status, which); Alogi ("Led_ctrl:%d,%d,%d", which, status, ret); returnret;}Static structled_device_t Led_dev ={. Common={. Tag=Hardware_device_tag,. Close=Led_close,},. Led_open=Led_open,. Led_ctrl=Led_ctrl,};Static intLed_device_open (Const structhw_module_t* module,Const Char*ID,structhw_device_t**device) { *device = &Led_dev; return 0;}Static structhw_module_methods_t Led_module_methods ={. Open=Led_device_open,};structhw_module_t Hal_module_info_sym ={. Tag=Hardware_module_tag,. ID="led",. Methods= &Led_module_methods,};
Led_hal.h
#ifndef Android_led_interface_h#defineAndroid_led_interface_h#include<stdint.h>#include<sys/cdefs.h>#include<sys/types.h>#include__begin_declsstructled_device_t {structhw_device_t Common; int(*led_open) (structled_device_t*Dev); int(*led_ctrl) (structled_device_t* Dev,intWhich,intstatus);}; __end_decls#endif //Android_led_interface_h
Lesson 1th Section 4.4 _android Hardware Access Service writing HAL code