作者:mobiledu2502929447 | 来源:互联网 | 2023-09-04 18:47
android从应用层到HRL层调用流程:1.实现自己的LED灯之HAL层代码编写(1)编写Android.mkLOCAL_PATH:$(callmy-dir)inc
android从应用层到HRL层调用流程:
1.实现自己的LED灯之HAL层代码编写
(1)编写Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw //动态库会放在system/lib/hw文件夹中。
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := led_hal.c
LOCAL_MODULE := led.default
include $(BUILD_SHARED_LIBRARY)
(2)编写led_hal.h
#ifndef ANDROID_LED_INTERFACE_H
#define ANDROID_LED_INTERFACE_H
#include
#include
#include
#include
__BEGIN_DECLS
struct led_device_t {
struct hw_device_t common;
int (*led_open)(struct led_device_t* dev);
int (*led_ctrl)(struct led_device_t* dev, int which, int status); //对led 灯进行操作接口。
};
__END_DECLS
#endif // ANDROID_LED_INTERFACE_H
(3)编写led_hal.c
#define LOG_TAG "LedHal"
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
static int fd;
/** Close this device */
static int led_close(struct hw_device_t* device)
{
close(fd);
return 0;
}
/** open this device */
static int led_open(struct led_device_t* dev)
{
fd = open("/dev/leds", O_RDWR);
ALOGI("led_open : %d", fd);
if (fd >= 0)
return 0;
else
return -1;
}
//操作ioctl 驱动led 灯。
static int led_ctrl(struct led_device_t* dev, int which, int status)
{
int ret = ioctl(fd, status, which);
ALOGI("led_ctrl : %d, %d, %d", which, status, ret);
return ret;
}
//操作驱动所有的接口。
static struct led_device_t led_dev = {
.common = {
.tag = HARDWARE_DEVICE_TAG,
.close = led_close,
},
.led_open = led_open,
.led_ctrl = led_ctrl,
};
//hal默认方法。
static int led_device_open(const struct hw_module_t* module, const char* id,
struct hw_device_t** device)
{
*device = &led_dev;
return 0;
}
static struct hw_module_methods_t led_module_methods = {
.open = led_device_open,
};
struct hw_module_t HAL_MODULE_INFO_SYM = {
.tag = HARDWARE_MODULE_TAG,
.id = "led",
.methods = &led_module_methods,
};
//把新文件上传到服务器, 所在目录:
hardware/libhardware/include/hardware/led_hal.h
hardware/libhardware/modules/led/led_hal.c
hardware/libhardware/modules/led/Android.mk
(4)编译动态库
$ mmm hardware/libhardware/modules/led
2.实现自己的HAL Led灯之JNI层代码编写:
(1)编写JNI代码
com_android_server_LedService.cpp
#define LOG_TAG "LedService"
#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
namespace android
{
static led_device_t* led_device; //led灯设备。
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_t const**)&module); //id ="led"与 led_hal.c 类的方法HAL_MODULE_INFO_SYM 属性 .id = "led"一致
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;
return led_device->led_open(led_device);
} else {
return -1;
}
}
return -1;
}
void ledClose(JNIEnv *env, jobject cls)
{
//ALOGI("native ledClose ...");
//close(fd);
}
//操作led灯
jint ledCtrl(JNIEnv *env, jobject cls, jint which, jint status)
{
ALOGI("native ledCtrl %d, %d", which, status);
return led_device->led_ctrl(led_device, which, status);
}
static const JNINativeMethod methods[] = {
{"native_ledOpen", "()I", (void *)ledOpen},
{"native_ledClose", "()V", (void *)ledClose},
{"native_ledCtrl", "(II)I", (void *)ledCtrl},
};
int register_android_server_LedService(JNIEnv *env)
{
return jniRegisterNativeMethods(env, "com/android/server/LedService",
methods, NELEM(methods));
}
}
(2)修改frameworks/base/services/core/jni/onload.cpp
1)在namespace android {}方法中添加
int register_android_server_LedService(JNIEnv *env);
2)extern "C" jint JNI_OnLoad(JavaVM* vm, void* reserved){}方法中添加
register_android_server_LedService(env);
(3)修改 frameworks/base/services/core/jni/Android.mk :
添加 $(LOCAL_REL_DIR)/com_android_server_LedService.cpp \
(4)把新文件上传到服务器, 所在目录:
frameworks/base/services/core/jni/com_android_server_LedService.cpp
(5)编译:
$ mmm frameworks/base/services //重新打包libandroid_servers.so动态库。
3.实现自己的HAL Led灯之Service层代码编写
(1) 编写AIDL
1) ILedService.aidl
package android.os;
/** {@hide} */
interface ILedService
{
int ledCtrl(int which, int status);
}
2)把 ILedService.aidl 放入 frameworks/base/core/java/android/os
3)修改 frameworks/base/Android.mk 添加一行
core/java/android/os/IVibratorService.aidl \
+ core/java/android/os/ILedService.aidl \
(2)编译:mmm frameworks/base
它会生成: ./out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/src/core/java/android/os/ILedService.java
(3) 编写LedService.java