news 2026/8/7 14:18:04

传统 Hal 开发笔记4----实现 Hal 模块

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
传统 Hal 开发笔记4----实现 Hal 模块

目录

  • 一、代码编写
  • 二、编译系统配置
实现 Hal 模块

一、代码编写

接下来就来为上节实现的驱动写一个简单的 HAL 模块。

hardware/libhardware/include/hardware目录下添加hello_hal.h

#ifndef_HARDWARE_HELLO_HAL_H#define_HARDWARE_HELLO_HAL_H#include<hardware/hardware.h>__BEGIN_DECLS#defineHELLO_HAL_API_VERSIONHARDWARE_MODULE_API_VERSION(1,0)#defineHELLO_HAL_HARDWARE_MODULE_ID"hello_hal"#defineHELLO_HAL_DEVICE_ID_MAIN"main_hello_hal"structhello_hal_device_t;typedefstructhello_hal_device_t{structhw_device_tcommon;intfd;// 存储/dev/hello的文件描int(*hello_hal_open)(structhello_hal_device_t*hello_hal_dev);int(*hello_hal_read)(structhello_hal_device_t*hello_hal_dev,char*str);int(*hello_hal_write)(structhello_hal_device_t*hello_hal_dev,constchar*str);}hello_hal_device_t;staticinlineinthello_hal_methods_open(conststructhw_module_t*module,hello_hal_device_t**device){returnmodule->methods->open(module,HELLO_HAL_DEVICE_ID_MAIN,(structhw_device_t**)device);}__END_DECLS#endif// _HARDWARE_HELLO_HAL_H

这里的核心是:

  • 实现一个hello_hal_device_t结构体,这个结构体用于操作具体的硬件
  • 实现hello_hal_methods_open函数,这个函数用于从hw_module_t中找到hello_hal_device_t结构体实例
    接着在hardware/libhardware/modules/目录下添加hello_hal目录,并在hello_hal目录下添加源码文件hello_hal.c
#include<hardware/hello_hal.h>#include<hardware/hardware.h>#include<cutils/log.h>#include<malloc.h>#include<stdio.h>#include<unistd.h>#include<fcntl.h>#include<errno.h>#include<string.h>inthello_open(structhello_hal_device_t*hello_hal_dev __unused){// 打开/dev/hello,fd存入设备结构体hello_hal_dev->fd=open("/dev/hello",O_RDWR);if(hello_hal_dev->fd==-1){ALOGE("hello_hal: can not open file /dev/hello, errno=%d",errno);// 用ALOGE打印日志return-1;}return0;}inthello_close(structhw_device_t*dev){hello_hal_device_t*hello_hal_dev=(hello_hal_device_t*)dev;if(hello_hal_dev->fd>=0){close(hello_hal_dev->fd);// 关闭结构体中的fd}free(hello_hal_dev);// 释放设备结构体内存return0;}inthello_read(structhello_hal_device_t*hello_hal_dev __unused,char*str){charbuf[1024]={0};// 初始化缓冲区,避免脏数据intlen=read(hello_hal_dev->fd,buf,sizeof(buf)-1);if(len>0){buf[len]='\0';strcpy(str,buf);returnlen;}elseif(len==0){ALOGW("hello_hal: read 0 bytes from /dev/hello");return0;}else{ALOGE("hello_hal: read failed, errno=%d",errno);return-1;}}inthello_write(structhello_hal_device_t*hello_hal_dev __unused,constchar*str){if(!str){// 空指针校验ALOGE("hello_hal: write str is NULL");return-EINVAL;}intlen=strlen(str)+1;len=len<1024?len:1024;intret=write(hello_hal_dev->fd,str,len);if(ret<0){ALOGE("hello_hal: write failed, errno=%d",errno);return-1;}returnret;}// HAL模块的open函数(创建设备实例)staticinthello_hal_open(consthw_module_t*module,constchar*id __unused,hw_device_t**device){// 分配设备结构体内存(calloc自动初始化0)hello_hal_device_t*hello_hal_dev=calloc(1,sizeof(hello_hal_device_t));if(!hello_hal_dev){ALOGE("hello_hal: Can not allocate memory for hello hal device");return-ENOMEM;}// 初始化hw_device_t(HAL框架要求)hello_hal_dev->common.tag=HARDWARE_DEVICE_TAG;hello_hal_dev->common.module=(hw_module_t*)module;hello_hal_dev->common.version=HARDWARE_DEVICE_API_VERSION(1,0);hello_hal_dev->common.close=hello_close;// 现在类型匹配// 绑定自定义函数指针hello_hal_dev->hello_hal_open=hello_open;hello_hal_dev->hello_hal_write=hello_write;hello_hal_dev->hello_hal_read=hello_read;// 输出设备指针给上层*device=(hw_device_t*)hello_hal_dev;return0;}// HAL模块方法结构体staticstructhw_module_methods_thello_hal_module_methods={.open=hello_hal_open,};// HAL模块入口(必须命名为HAL_MODULE_INFO_SYM)structhw_module_tHAL_MODULE_INFO_SYM={.tag=HARDWARE_MODULE_TAG,.module_api_version=HELLO_HAL_API_VERSION,.hal_api_version=HARDWARE_HAL_API_VERSION,.id=HELLO_HAL_HARDWARE_MODULE_ID,.name="Default Hello HAL",.author="sixian",.methods=&hello_hal_module_methods,};
  • 声明一个hw_module_t结构体实例
  • 声明一个hw_module_methods_t结构体实例,其中open函数指针指向hello_hal_open
  • hello_hal_open> 会构建一个hello_hal_device_t结构体实例,对其成员赋值,核心的成员主要是hello_hal_open 、hello_hal_write、 hello_hal_read三个函数指针

二、编译系统配置

接着在hardware/libhardware/modules/hello_hal目录下添加 Android.mk

LOCAL_PATH:=$(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE:=hello_hal.default#HAL module implementation stored in#hw/<VIBRATOR_HARDWARE_MODULE_ID>.default.soLOCAL_MODULE_RELATIVE_PATH:=hw LOCAL_C_INCLUDES:=hardware/libhardware LOCAL_SRC_FILES:=hello_hal.c LOCAL_SHARED_LIBRARIES:=liblog LOCAL_MODULE_TAGS:=optional include $(BUILD_SHARED_LIBRARY)

接着在hardware/libhardware/modules/Android.mk中添加hello_hal,这样编译系统才会去编译hello_hal模块。

hardware_modules:=\ camera \ gralloc \ sensors \ hello_hal include $(call all-named-subdir-makefiles,$(hardware_modules))

接着在 product 配置文件device/xxxx/xxxx/device.mk中添加hello_hal.defaultso库:

PRODUCT_PACKAGES:=\ audio.primary.goldfish \ vibrator.goldfish \ hello_hal.default\

至此,hal 层任务完工

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/7 8:18:31

当开题报告成为科研长征的“第一道关卡”,Paperzz的AI开题报告功能是如何把“选题迷茫”变成“逻辑闭环”的

Paperzz-AI官网免费论文查重复率AIGC检测/开题报告/文献综述/论文初稿 paperzz - 开题报告https://www.paperzz.cc/proposal 前言&#xff1a;在导师的“灵魂拷问”面前&#xff0c;我点开了那个橙色按钮 “研究问题不聚焦。” “理论基础薄弱&#xff0c;缺乏文献支撑。” “…

作者头像 李华
网站建设 2026/8/7 1:03:39

2、黑客必备:Kali Linux 安装与基础入门

黑客必备:Kali Linux 安装与基础入门 一、Kali Linux 简介与下载 1.1 未来趋势与选择 从市场占比来看,Microsoft Windows 在移动设备市场仅占 7%,而 Unix/Linux 才是未来趋势。对于想要进入黑客领域的人来说,选择合适的操作系统至关重要,Kali Linux 就是不错的选择。 …

作者头像 李华
网站建设 2026/8/4 17:26:03

4、Linux 文件与目录操作及文本处理指南

Linux 文件与目录操作及文本处理指南 1. 文件与目录的基本操作 在 Linux 系统中,当你找到所需的文件和目录后,就需要对它们执行各种操作,如创建、复制、重命名和删除等。 1.1 创建文件 在 Linux 中有多种创建文件的方法,这里介绍两种简单的方式。 - 使用 cat 命令 :…

作者头像 李华
网站建设 2026/8/6 0:17:40

SAE J1939协议文档库:重载车辆通信核心技术规范

SAE J1939协议文档库&#xff1a;重载车辆通信核心技术规范 【免费下载链接】SAEJ1939协议文档库完整版双语对照 SAE J1939协议文档库&#xff08;完整版&#xff0c;双语对照&#xff09;欢迎来到SAE J1939协议的综合资源库&#xff01;本仓库致力于为汽车电子、车联网领域的开…

作者头像 李华
网站建设 2026/8/6 19:55:33

当期刊投稿成为学术生涯的“高风险动作”,Paperzz 的 AI 期刊论文写作功能是如何把“科研孤岛”变成“协作网络”的

Paperzz-AI官网免费论文查重复率AIGC检测/开题报告/文献综述/论文初稿 paperzz - 期刊论文https://www.paperzz.cc/journalArticle 引子&#xff1a;被拒稿邮件击穿的夜晚&#xff0c;我点开了那个橙色按钮 凌晨两点&#xff0c;邮箱弹出一封来自《中国农村经济》的拒稿通知&…

作者头像 李华
网站建设 2026/8/6 17:49:48

Tacotron-2中文语音合成实战:从技术原理到商业应用深度解析

Tacotron-2中文语音合成实战&#xff1a;从技术原理到商业应用深度解析 【免费下载链接】Tacotron-2-Chinese 项目地址: https://gitcode.com/gh_mirrors/ta/Tacotron-2-Chinese 还在为智能语音交互系统的开发而困扰吗&#xff1f;Tacotron-2-Chinese作为专为中文优化的…

作者头像 李华