Android使用HAL操作硬件

    xiaoxiao2021-03-26  18

    1.新建hardware/libhardware/include/hardware/led_hal.h文件: #ifndef ANDROID_LED_INTERFACE_H #define ANDROID_LED_INTERFACE_H #include <stdint.h> #include <sys/cdefs.h> #include <sys/types.h> #include <hardware/hardware.h> #define LED_HARDWARE_MODULE_ID "led" struct led_module_t {     struct hw_module_t common; }; struct led_device_t {     struct hw_device_t common;     int fd;     int (*led_ctrl)(struct led_device_t* dev, int val); }; #endif  // ANDROID_LED_INTERFACE_H 2.新建hardware/libhardware/modules/led/led_hal.c文件: #define LOG_TAG "led_device" #include <cutils/log.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <fcntl.h> #include <sys/types.h> #include <hardware/led.h> int led_ctrl (struct led_device_t *dev, int val) {     int ret;     val = !!val;     ret = write(dev->fd, &val, 1);     if (ret < 0) {         ALOGE("write to /dev/led-dev failed");         return -EBUSY;     }     return 0; } int close_led (struct hw_device_t *dev) {     struct led_device_t *led_dev = (struct led_device_t *)dev;     close(led_dev->fd);     free(led_dev);     return 0; } static int open_led (const struct hw_module_t *module, char const *name,         struct hw_device_t **device) {     int fd;     struct led_device_t *dev;     fd = open("/dev/led-dev", O_RDWR);     if (fd < 0) {         ALOGE("open /dev/led-dev failed");         return -EBUSY;     }     dev = malloc(sizeof(struct led_device_t));     if (!dev) {         ALOGE("malloc failed");         return -ENOMEM;     }     memset(dev, 0, sizeof(*dev));     dev->fd = fd;     dev->common.tag = HARDWARE_DEVICE_TAG;     dev->common.version = 0;     dev->common.module = (struct hw_module_t*)module;     dev->common.close = (int (*)(struct hw_device_t*))close_led;     dev->led_ctrl = led_ctrl;     *device = (struct hw_device_t *)dev;     return 0; } static struct hw_module_methods_t led_module_methods = {     .open =  open_led, }; struct led_module_t HAL_MODULE_INFO_SYM = {     common: {         tag:  HARDWARE_MODULE_TAG,         version_major:  1,         version_minor:  0,         id:  LED_HARDWARE_MODULE_ID,         name:  "led Module",         author:  "cary.he",         methods:  &led_module_methods,     } }; 3.新建hardware/libhardware/modules/led/Android.mk: 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 := userdebug编译:mmm hardware/libhardware/modules/led/ 在out/target/product/msm8909/system/lib/hw/下会生成led.default.so文件。 4.新建frameworks/base/services/core/jni/com_android_server_LedService.cpp文件: #define LOG_TAG "LedServiceJNI" #include "utils/Log.h" #include "jni.h" #include "JNIHelp.h" #include "android_runtime/AndroidRuntime.h" #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <hardware/hardware.h>   #include <hardware/led.h> namespace android { struct led_device_t *led_dev; int led_init(JNIEnv *env, jclass clazz) {     int ret;     struct led_module_t *led_module;     ret = hw_get_module(LED_HARDWARE_MODULE_ID, (const struct hw_module_t **)&led_module);     if (ret) {         ALOGE("hw_get_module failed");         return -EBUSY;     }     ret = led_module->common.methods->open((const struct hw_module_t *)led_module,             NULL, (struct hw_device_t **)&led_dev);     if (ret) {         ALOGE("led module open failed");         return -EBUSY;     }     return 0; } int led_ctrl(JNIEnv *env, jclass clazz, jint val) {     int ret;     ret = led_dev->led_ctrl(led_dev, val);     if (ret) {         ALOGE("led_ctrl failed");         return -EBUSY;     }     return 0; } void led_close(JNIEnv *env, jclass clazz) {     led_dev->common.close((struct hw_device_t *)led_dev); } static const JNINativeMethod method_table[] = {     {"init_native",  "()I",  (void*)led_init },     {"ctrl_native",  "(I)I", (void*)led_ctrl },     {"deinit_native", "()V", (void*)led_close}, }; int register_android_server_LedService(JNIEnv *env) {         return jniRegisterNativeMethods(env, "com/android/server/LedService", method_table, NELEM(method_table)); } }; 5.修改frameworks/base/services/core/jni/onload.cpp文件,增加函数声明和调用:     int register_android_server_Watchdog(JNIEnv* env);      int register_android_server_LedService(JNIEnv *env);    register_android_server_Watchdog(env);      register_android_server_LedService(env); 6.修改frameworks/base/services/core/jni/Android.mk文件,增加一行:     $(LOCAL_REL_DIR)/com_android_server_PersistentDataBlockService.cpp \      $(LOCAL_REL_DIR)/com_android_server_LedService.cpp \     $(LOCAL_REL_DIR)/onload.cpp 编译:mmm frameworks/base/services 在out/target/product/msm8909/system/lib/下会生成libandroid_servers.so文件。
    转载请注明原文地址: https://ju.6miu.com/read-658788.html

    最新回复(0)