编写android HAL代码

很重要的一点,android代码是运行在linux应用层的,包括HAL层的代码。

HAL的三个结构体:hw_module_t, hw_module_methods_t, hw_device_t。

hardware\libhardware\include\hardware\Hardware.h:

struct hw_module_t;
struct hw_module_methods_t;
struct hw_device_t;

/**
 * Every hardware module must have a data structure named HAL_MODULE_INFO_SYM
 * and the fields of this data structure must begin with hw_module_t
 * followed by module specific information.
 */
typedef struct hw_module_t {
    /** tag must be initialized to HARDWARE_MODULE_TAG */
    uint32_t tag;

    /**
     * The API version of the implemented module. The module owner is
     * responsible for updating the version when a module interface has
     * changed.
     *
     * The derived modules such as gralloc and audio own and manage this field.
     * The module user must interpret the version field to decide whether or
     * not to inter-operate with the supplied module implementation.
     * For example, SurfaceFlinger is responsible for making sure that
     * it knows how to manage different versions of the gralloc-module API,
     * and AudioFlinger must know how to do the same for audio-module API.
     *
     * The module API version should include a major and a minor component.
     * For example, version 1.0 could be represented as 0x0100. This format
     * implies that versions 0x0100-0x01ff are all API-compatible.
     *
     * In the future, libhardware will expose a hw_get_module_version()
     * (or equivalent) function that will take minimum/maximum supported
     * versions as arguments and would be able to reject modules with
     * versions outside of the supplied range.
     */
    uint16_t module_api_version;
#define version_major module_api_version
    /**
     * version_major/version_minor defines are supplied here for temporary
     * source code compatibility. They will be removed in the next version.
     * ALL clients must convert to the new version format.
     */

    /**
     * The API version of the HAL module interface. This is meant to
     * version the hw_module_t, hw_module_methods_t, and hw_device_t
     * structures and definitions.
     *
     * The HAL interface owns this field. Module users/implementations
     * must NOT rely on this value for version information.
     *
     * Presently, 0 is the only valid value.
     */
    uint16_t hal_api_version;
#define version_minor hal_api_version

    /** Identifier of module */
    const char *id;

    /** Name of this module */
    const char *name;

    /** Author/owner/implementor of the module */
    const char *author;

    /** Modules methods */
    struct hw_module_methods_t* methods;

    /** module‘s dso */
    void* dso;

    /** padding to 128 bytes, reserved for future use */
    uint32_t reserved[32-7];

} hw_module_t;

typedef struct hw_module_methods_t {
    /** Open a specific device */
    int (*open)(const struct hw_module_t* module, const char* id,
            struct hw_device_t** device);

} hw_module_methods_t;

/**
 * Every device data structure must begin with hw_device_t
 * followed by module specific public methods and attributes.
 */
typedef struct hw_device_t {
    /** tag must be initialized to HARDWARE_DEVICE_TAG */
    uint32_t tag;

    /**
     * Version of the module-specific device API. This value is used by
     * the derived-module user to manage different device implementations.
     *
     * The module user is responsible for checking the module_api_version
     * and device version fields to ensure that the user is capable of
     * communicating with the specific module implementation.
     *
     * One module can support multiple devices with different versions. This
     * can be useful when a device interface changes in an incompatible way
     * but it is still necessary to support older implementations at the same
     * time. One such example is the Camera 2.0 API.
     *
     * This field is interpreted by the module user and is ignored by the
     * HAL interface itself.
     */
    uint32_t version;

    /** reference to the module this device belongs to */
    struct hw_module_t* module;

    /** padding reserved for future use */
    uint32_t reserved[12];

    /** Close this device */
    int (*close)(struct hw_device_t* device);

} hw_device_t;

HAL源文件中要定义一个结构体hw_module_t, 它里面包含了hw_module_methods_t。

hw_module_methods_t只有一个成员函数open:

int (*open)(const struct hw_module_t* module, const char* id,
            struct hw_device_t** device);

在函数内部,需要返回一个hw_device_t结构体。

或者定义扩展的hw_device_t,例如:

struct xxx_device_t {
    struct hw_device_t common;
        //以下成员是HAL对上层提供的接口或一些属性
        int fd;
    int (*set_val)(struct xxx_device_t* dev, int val);
    int (*get_val)(struct xxx_device_t* dev, int* val);
};

在open函数给device返回值的时候,返回指向xxx_device_t结构指针。

定义结构体时,变量名必须为HAL_MODULE_INFO_SYM,例如:

/*模块实例变量*/
struct xxx_module_t HAL_MODULE_INFO_SYM = {    

        common: {
        tag: HARDWARE_MODULE_TAG,
        version_major: 1,
        version_minor: 0,
        id: XXX_HARDWARE_MODULE_ID,    //头文件中有定义
        name: MODULE_NAME,
        author: MODULE_AUTHOR,
        methods: &xxx_module_methods,
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-10 00:04:00

编写android HAL代码的相关文章

第1课第4.4节_Android硬件访问服务编写HAL代码

4 编写HAL代码 源码下载方法 第一次: git clone https://github.com/weidongshan/SYS_0001_LEDDemo.git 更新: git pull origin 取出指定版本: git checkout v1 // 有JNI没有HAL git checkout v2 // 有JNI,HAL git checkout v3 // add MODULE TAG, DEVICE TAG JNI 向上提供本地函数, 向下加载HAL文件并调用HAL的函数HAL

03_Android NDK中C语言调用Java代码,javah的使用,javap的使用以及生成签名,Android.mk的编写,C代码的编写

?? 1  案例场景,通过C语言回调Java的代码,案例的最终界面: 2  案例的代码结构如下: 3 编写DataProvider的代码: package com.example.ndkcallback; public class DataProvider { //C调用java空方法 public void helloFromJava(){ System.out.println("哈哈哈  我被调用了"); } //C调用java中的带两个int参数的方法 public int Ad

Android HAL模块实现(转)

Android的HAL(Hardware Abstract Layer硬件抽象层)是为了保护一些硬件提供商的知识产权而提出的,是为了避开linux的GPL束缚.思路是把控制硬件的动作都放到了Android HAL中,而linux driver仅仅完成一些简单的数据交互作用,甚至把硬件寄存器空间直接映射到user space.而Android是基于Aparch的license,因此硬件厂商可以只提供二进制代码,所以说Android只是一个开放的平台,并不是 一个开源的平台. 总结下来,Androi

Android HAL实例解析

一.概述 本文希望通过分析台湾的Jollen的mokoid 工程代码,和在s5pc100平台上实现过程种遇到的问题,解析Andorid HAL的开发方法. 二.HAL介绍 现有HAL架构由Patrick Brady (Google) 在2008 Google I/O演讲中提出的,如下图. Android的HAL是为了保护一些硬件提供商的知识产权而提出的,是为了避开linux的GPL束缚.思路是把控制硬件的动作都放到了Android HAL中,而linux driver仅仅完成一些简单的数据交互作

Android HAL模块实现

1. HAL介绍 Android的HAL(Hardware Abstract Layer硬件抽象层)是为了保护一些硬件提供商的知识产权而提出的.是为了避开linux的GPL束缚. 思路是把控制硬件的动作都放到了Android HAL中,而linux driver仅仅完毕一些简单的数据交互作用,甚至把硬件寄存器空间直接映射到user space.而Android是基于Aparch的license,因此硬件厂商能够仅仅提供二进制代码,所以说Android仅仅是一个开放的平台,并非一个开源的平台. 总

【转】Android HAL实例解析

原文网址:http://www.embedu.org/Column/Column339.htm 作者:刘老师,华清远见嵌入式学院讲师. 一.概述 本文希望通过分析台湾的Jollen的mokoid 工程代码,和在s5pc100平台上实现过程种遇到的问题,解析Andorid HAL的开发方法. 二.HAL介绍 现有HAL架构由Patrick Brady (Google) 在2008 Google I/O演讲中提出的,如下图. Android的HAL是为了保护一些硬件提供商的知识产权而提出的,是为了避

代码质量优先——《编写高质量代码:改善c程序代码的125个建议》

高质量的代码不但可以促进团队合作.减少bug处理.降低维护成本,对程序员自身的成长也是至关重要的.很难想象一个参考<如何编写无法维护的代码>写代码的程序员技术成长的上限有多么低.为了写出高质量的代码,我们需要听取过来人的改善代码质量的经验,<编写高质量代码:改善c程序代码的125个建议>就是一本能让人写出高质量代码的好书. 本书的第三章<程序控制语句应该保持简洁高效>首先用简练的语言介绍了流程控制结构的概念,然后提供了对if.else.for.do-while.swit

第7章 Android HAL实例解析

第7章 Android HAL实例解析 通过本章介绍Android的HAL知道HAL是为了一些硬件提供商提出的保护专利的驱动程序而产生的,是为了避开Linux的GPL束缚.HAL主要的存储目录有:1.libhardware_legany  2.libhardware:3.ril   4.msm7k.主要包含以下一些模块:Gps.Vibrator.Wifi.Copybit.Audio.Camera.Lights.Ril.Overlay等.大概了解了Android  HAL. 第1中方法 直接调用s

Android技术16:编写Android中直接可运行的二进制文件

我们都知道Android中所有应用程序都运行在Android的Dalvik虚拟机上,一般程序不直接与操作系统打交道,即便调用底层的方法也通过JNI技术.不过我们可以直接使用C语言编写二进制文件,直接在底层运行.下面演示其步骤. 1.安装下载编译器和链接器软件.Sourcery G++ Lite Edition for ARM. arm-none-linux-gnueabi-gcc.exe是编译命令 bin/arm-none-linux-gnueabi-ld.exe是链接命令 2.编写C代码 为了