代码总体结构:
hardware/libhardware/include/hardware/hello.h
hardware/libhardware/modules/hello/Android.bp
hardware/libhardware/modules/hello/hello.c
hardware/libhardware/modules/hello/HelloTest.cpp
hello.h
#include <sys/cdefs.h> #include <sys/types.h> #include <hardware/hardware.h> //HAL模块名 #define HELLO_HARDWARE_MODULE_ID "hello" //HAL版本号 #define HELLO_MODULE_API_VERSION_1_0 HARDWARE_MODULE_API_VERSION(0, 1) //设备名 #define HARDWARE_HELLO "hello" #define SPRD_BACKLIGHT_PATH "/sys/class/backlight/sprd_backlight/brightness" #define ERR_BUF_MAX 64 //自定义HAL模块结构体 typedef struct hello_module { struct hw_module_t common; } hello_module_t; //自定义HAL设备结构体 typedef struct hello_device { struct hw_device_t common; //加法函数 int (*additionTest)(const struct hello_device *dev,int a,int b,int* total); int (*sysfsWrite)(const char * path,char * buf); } hello_device_t; //给外部调用提供打开设备的函数 static inline int _hello_open(const struct hw_module_t *module, hello_device_t **device) { return module->methods->open(module, HARDWARE_HELLO, (struct hw_device_t **) device); }
hardware/libhardware/modules/hello/Android.bp
cc_library_shared { name: "hello.default", relative_install_path: "hw", proprietary: true, srcs: ["hello.c"], header_libs: ["libhardware_headers"], shared_libs: ["liblog"], } cc_binary { name: "hardware_HelloTest", srcs: ["HelloTest.cpp"], shared_libs: [ "liblog", "libhardware", ], }
hardware/libhardware/modules/hello/hello.c
#define LOG_TAG "HelloHal" #include <malloc.h> #include <stdint.h> #include <string.h> #include <log/log.h> #include <hardware/hello.h> #include <hardware/hardware.h> static int sysfsWrite(const char * path,char * buf) { char err_buf[ERR_BUF_MAX]; int size; int fd; //int errno; fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); if (fd < 0) { //strerror_r(errno, err_buf, sizeof(err_buf)); ALOGE("Error open %s: %s\n", path, err_buf); return -1 ; } //LOGE(" %s -> buf = %s ",__FUNCTION__,buf); size = write(fd, buf, strlen(buf)); if (size < 0) { //strerror_r(errno, err_buf, sizeof(err_buf)); ALOGE("Error write %s: %s\n", path, err_buf); } close(fd); return size; } //加法函数实现 static int additionTest(const struct hello_device *dev,int a,int b,int *total) { if(!dev){ return -1; } *total = a + b; return 0; } //关闭设备函数 static int hello_close(hw_device_t *dev) { if (dev) { free(dev); return 0; } else { return -1; } } //打开设备函数 static int hello_open(const hw_module_t* module,const char __unused *id, hw_device_t** device) { if (device == NULL) { ALOGE("NULL hello device on open"); return -1; } hello_device_t *dev = malloc(sizeof(hello_device_t)); memset(dev, 0, sizeof(hello_device_t)); dev->common.tag = HARDWARE_DEVICE_TAG; dev->common.version = HELLO_MODULE_API_VERSION_1_0; dev->common.module = (struct hw_module_t*) module; dev->common.close = hello_close; dev->additionTest = additionTest; dev->sysfsWrite = sysfsWrite; *device = &(dev->common); return 0; } static struct hw_module_methods_t hello_module_methods = { .open = hello_open, }; //导出符号HAL_MODULE_INFO_SYM,指向自定义模块 hello_module_t HAL_MODULE_INFO_SYM = { .common = { .tag = HARDWARE_MODULE_TAG, .module_api_version = HELLO_MODULE_API_VERSION_1_0, .hal_api_version = HARDWARE_HAL_API_VERSION, .id = HELLO_HARDWARE_MODULE_ID, .name = "Hello HAL", .author = "Hello", .methods = &hello_module_methods, }, };
hardware/libhardware/modules/hello/HelloTest.cpp
#include <hardware/hardware.h> #include <hardware/hello.h> #include <log/log.h> #define TAG "HelloHal" int main(){ hello_device_t* hello_device = NULL; const hello_module_t * module = NULL; int ret = hw_get_module(HELLO_HARDWARE_MODULE_ID,(const struct hw_module_t**)&module); if (!ret) { ALOGD("get hello-hal failed....... 1"); ret = _hello_open((const struct hw_module_t*)module,&hello_device); } if (ret < 0) { ALOGD("get hello-hal failed....... 2"); return -1; } int total = 0; char val[3] = {'2','5','5'}; hello_device->additionTest(hello_device,3,5,&total); hello_device->sysfsWrite(SPRD_BACKLIGHT_PATH,val); ALOGD("success start hello-hal....total = %d",total); return 0; }
成果演示:
/system/bin # ./hardware_HelloTest
07-19 22:02:52.073 5265 5265 D hardware_HelloTest: get hello-hal failed....... 1
07-19 22:02:52.074 5265 5265 D hardware_HelloTest: success start hello-hal....total = 8
07-19 22:03:09.656 5328 5328 D hardware_HelloTest: get hello-hal failed....... 1
07-19 22:03:09.657 5328 5328 D hardware_HelloTest: success start hello-hal....total = 8
07-19 22:03:11.853 5329 5329 D hardware_HelloTest: get hello-hal failed....... 1
07-19 22:03:11.853 5329 5329 D hardware_HelloTest: success start hello-hal....total = 8
07-19 22:03:37.192 5397 5397 D hardware_HelloTest: get hello-hal failed....... 1
07-19 22:03:37.193 5397 5397 D hardware_HelloTest: success start hello-hal....total = 8