CPU:RK3288
系统:Android 5.1
功能:上层 app 控制 led 亮灭
开发板:Firefly RK3288
led_hal.c
path:hardware/rockchip/firefly/led_hal/led_hal.c
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <string.h> #include <errno.h>
#include <hardware/led_hal.h> //日志的标签 #define LOG_TAG "led_hal" #include <utils/Log.h> static int fd; int led_hal_dev_close(struct hw_device_t *device) { if(device != NULL) { struct led_hw_device_t *temp = (struct led_hw_device_t *)device; free(temp); } close(fd); return 0; } int led_hal_open_dev(void) { ALOGD("--%s--", __func__); fd = open("/dev/firefly_leds", O_RDWR); if(fd < 0) { ALOGE("open failed : %s", strerror(errno)); return fd; } return 0; } int led_hal_control_dev(int on) { ALOGD("--%s--", __func__); int ret; ret = write(fd, &on, 4); if(ret < 0) { ALOGE("control failed : %s", strerror(errno)); return ret; } return 0; } //该函数正确返回0 int led_hal_module_open(const struct hw_module_t *module, const char *id, struct hw_device_t **device) { ALOGD("--%s--", __func__); struct led_hw_device_t *led_dev = NULL; led_dev = (struct led_hw_device_t *)malloc(sizeof(struct led_hw_device_t)); if (led_dev == NULL) { ALOGE("malloc failed"); return -1; } ALOGD("malloc success"); //初始化device对象 led_dev->common.tag = HARDWARE_DEVICE_TAG; led_dev->common.version = 1; led_dev->common.module = module; led_dev->common.close = led_hal_dev_close; led_dev->open = led_hal_open_dev; led_dev->control = led_hal_control_dev; //将当前的led_dev传递给jni层 *device = (struct hw_device_t *)led_dev; return 0; } struct hw_module_methods_t led_hal_methods = { open : led_hal_module_open, }; //结构体名称必须为HMI struct led_hw_module_t HMI = { common : { tag : HARDWARE_MODULE_TAG, version_major : 1, version_minor : 0, //id必须与jni层的一致 id : LED_HAL_MODULE_ID, name : "firefly led hal module", methods : &led_hal_methods, }, };
led_hal.h
path:hardware/libhardware/include/hardware/led_hal.h
#ifndef _LED_HAL_H_ #define _LED_HAL_H_ #include <hardware/hardware.h> #define LED_HAL_MODULE_ID "firefly_led_hal" //自定义module对象,一定要继承父类, //可以扩展 struct led_hw_module_t { //一定要在结构体第一位,名称必须是common struct hw_module_t common; //下面是扩展,用于测试 int test; }; //自定义device对象,一定要继承父类 //一定要扩展 struct led_hw_device_t { //一定要在结构体第一位,名称必须是common struct hw_device_t common; //下面为扩展 int (*open)(void); int (*control)(int on); }; #endif
Android.mk
path:hardware/rockchip/firefly/led_hal/led_hal.c
语法参照 Android.mk语法解析
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := firefly_led_hal.default LOCAL_SRC_FILES := led_hal.c LOCAL_LDLIBS := -llog LOCAL_MODULE_TAGS := optional LOCAL_SHARED_LIBRARIES := liblog libcutils LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw include $(BUILD_SHARED_LIBRARY)