Android驱动(一)硬件访问服务学习之(三)Android加入HAL层访问硬件

  • 硬件平台:tiny4412
  • 系统:Android  5.0.2
  • 编译器: arm-linux-gcc-4.5.1

上一节我们实现了硬件访问服务来操作硬件:http://blog.csdn.net/fengyuwuzu0519/article/details/55271199

当时我们把对硬件的操作放在了JNI层,但是Android并不是这样,google提出HAL层,即硬件封装层

google官方框架图:https://developer.android.com/guide/platform/index.html。

这一节我们把硬件的操作封装装HAL层。

Android驱动(一)硬件访问服务学习之(三)Android加入HAL层访问硬件_第1张图片

android HAL是什么?为什么有它?来看一下网上的说法:

硬件抽象层是介于android内核kernel和上层之间的抽象出来的一层结构。他是对linux驱动的一个封装,对上层提供统一接口,上层应用不必知道下层硬件具体怎么实现工作的,它屏蔽了底层的实现细节。

它在整个android架构中的位置如下图所示:

Android驱动(一)硬件访问服务学习之(三)Android加入HAL层访问硬件_第2张图片

传统的linux对硬件的操作基本上在内核空间的linux驱动程序中实现了,那么现在为什么那么多此一举把对硬件的操作分为两部分,hal和linux驱动呢?

而且hal属于用户空间,linux驱动属于内核空间。其实并不多余。那么为什么要高出这么个东西,理由是很多的:

1.谷歌搭好了hal的框架,为上层framework打通过jni调用hal提供了统一的api,硬件开发商或者移植人员只需要按照框架开发即可,无需话费精力在与上层的交互上的实现上,将精力放在hal层本身的实现上即可。

2.从商业角度,许多硬件厂商不愿意将自己硬件相关一些核心的东西开源出去,假如将对自己硬件的驱动程序全部放入内核空间驱动程序实现,那么必须遵循GPL协议,是必需开源的。有了HAL层之后,他们可以把一些核心的算法之类的东西的实现放在HAL层,而hal层位于用户空间,不属于linux内核,和android源码一样遵循的是appache协议,这个是可以开源或者不开的。

接下来我们学习一下HAL,即硬件抽象层

一、HAL的思路

1、整体思路

(1)应用程序不会直接访问硬件,硬件访问由SystemServer访问,SystemServer中由JNI访问HAL层。

(2)JNI 向上提供本地函数, 向下加载HAL文件并调用HAL的函数

(3)HAL 负责访问驱动程序执行硬件操作(更加保密安全,方便,避开GPL协议)

(4)JNI加载HAL的实质就是如何使用dlopen加载动态库。

(5)android对dlopen进行封装,使用hw_get_module。

2、详细分析

(1)搜索:hw_get_module打开如下

灯光系统的JNI文件:
com_android_server_lights_LightsService.cpp(frameworks\base\services\core\jni)


(2)找到hw_get_module


(3)双击进去hw_get_module,最终调用hw_get_module_by_class("led",NULL)

int hw_get_module(const char *id, const struct hw_module_t **module)
{
    return hw_get_module_by_class(id, NULL, module);
}

点击hw_get_module_by_class

hw_get_module_by_class("led",NULL)

--》strlcpy(name, class_id, PATH_MAX);所以name= "led"

int hw_get_module_by_class(const char *class_id, const char *inst,
                           const struct hw_module_t **module)
{
    int i;
    char prop[PATH_MAX];
    char path[PATH_MAX];
    char name[PATH_MAX];
    char prop_name[PATH_MAX];

    if (inst)
        snprintf(name, PATH_MAX, "%s.%s", class_id, inst);
    else
        strlcpy(name, class_id, PATH_MAX);                                                   //name=led

    /*
     * Here we rely on the fact that calling dlopen multiple times on
     * the same .so will simply increment a refcount (and not load
     * a new copy of the library).
     * We also assume that dlopen() is thread-safe.
     */

    /* First try a property specific to the class and possibly instance */
    snprintf(prop_name, sizeof(prop_name), "ro.hardware.%s", name);
    if (property_get(prop_name, prop, NULL) > 0) {
        if (hw_module_exists(path, sizeof(path), name, prop) == 0) {
            goto found;
        }
    }

    /* Loop through the configuration variants looking for a module */
    for (i=0 ; i

(4)主要用到了两个函数,property_gethw_module_exists

hw_module_exists:用来判断 "name"."subname".so在三个固定的目录中是否存在。

name是上层hw_get_module_by_class传进来的name=led,即判断led."subname".so是否存在。subname也是上层函数中提供的prop,即获取到的属性值。这里暂且认为判断某个SO是否存在。

a.获取环境变量路径:char *hal_library_path =getenv("HAL_LIBRARY_PATH");        

得到环境变量下的决对路径:snprintf(path, path_len, "%s/%s.%s.so",

                 hal_library_path, name, subname);

 判断是否存在if (access(path, R_OK) == 0)
            return 0;

b.#define HAL_LIBRARY_PATH2 "/vendor/lib/hw"

同理判断 /vendor/lib/hw/led."subname".so是否存在

c.#define HAL_LIBRARY_PATH1 "/system/lib/hw"

同理判断 /system/lib/hw/led."subname".so是否存在

static int hw_module_exists(char *path, size_t path_len, const char *name,
                            const char *subname)
{
    char *hal_library_path = getenv("HAL_LIBRARY_PATH");
    if (hal_library_path) {
        snprintf(path, path_len, "%s/%s.%s.so",
                 hal_library_path, name, subname);
        if (access(path, R_OK) == 0)
            return 0;
    }

    snprintf(path, path_len, "%s/%s.%s.so",
             HAL_LIBRARY_PATH2, name, subname);
    if (access(path, R_OK) == 0)
        return 0;

    snprintf(path, path_len, "%s/%s.%s.so",
             HAL_LIBRARY_PATH1, name, subname);
    if (access(path, R_OK) == 0)
        return 0;

    return -ENOENT;
}

Android驱动(一)硬件访问服务学习之(三)Android加入HAL层访问硬件_第3张图片Android驱动(一)硬件访问服务学习之(三)Android加入HAL层访问硬件_第4张图片

显然4412只会去/system/lib/hw下查找"name"."subname".so文件是否存在。

property_get:android的属性系统。属性就是键值对,根据名字获取一个值。

property_get(prop_name, prop, NULL):prop_name:键      prop:获取的值

hw_get_module_by_class中property_get被多次调用。

第一次prop_name="ro.hardware.%s", name

之后:prop_name=

static const char *variant_keys[] = {
    "ro.hardware",  /* This goes first so that it can pick up a different
                       file on the emulator. */
    "ro.product.board",
    "ro.board.platform",
    "ro.arch"
};

Android驱动(一)硬件访问服务学习之(三)Android加入HAL层访问硬件_第5张图片 Android驱动(一)硬件访问服务学习之(三)Android加入HAL层访问硬件_第6张图片

软属性存在,在判断led.prop.so是否在三个目录存在。

如果都没有存在,则判断:led.default.so是否存在。

通过最后的Android.mk编译文件,我们知道,我们的文件会被编译成led.default.so,且放在 /system/lib/hw目录下。

(5)hw_get_module_by_class最终会调用load加载C库

found:
    /* load the module, if this fails, we're doomed, and we should not try
     * to load a different variant. */
    return load(class_id, path, module);

(6)dlopen+dlsym获取hw_module_t结构体。

handle = dlopen(path, RTLD_NOW);//加载C库

const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
hmi = (struct hw_module_t *)dlsym(handle, sym);// 加载后用dlsym获取HMI符号,
这个符号转换为hw_module_t结构体。从So文件中,获取名为HMI的结构体,判断结构体名和"led"是否一致,一致则找到了这个模块,最后把模块赋给    *pHmi = hmi;变量 pHmi是调用者传进来的。

static int load(const char *id,
        const char *path,
        const struct hw_module_t **pHmi)
{
    int status;
    void *handle;
    struct hw_module_t *hmi;

    /*
     * load the symbols resolving undefined symbols before
     * dlopen returns. Since RTLD_GLOBAL is not or'd in with
     * RTLD_NOW the external symbols will not be global
     */
    handle = dlopen(path, RTLD_NOW);
    if (handle == NULL) {
        char const *err_str = dlerror();
        ALOGE("load: module=%s\n%s", path, err_str?err_str:"unknown");
        status = -EINVAL;
        goto done;
    }

    /* Get the address of the struct hal_module_info. */
    const char *sym = HAL_MODULE_INFO_SYM_AS_STR;
    hmi = (struct hw_module_t *)dlsym(handle, sym);
    if (hmi == NULL) {
        ALOGE("load: couldn't find symbol %s", sym);
        status = -EINVAL;
        goto done;
    }

    /* Check that the id matches */
    if (strcmp(id, hmi->id) != 0) {
        ALOGE("load: id=%s != hmi->id=%s", id, hmi->id);
        status = -EINVAL;
        goto done;
    }

    hmi->dso = handle;

    /* success */
    status = 0;

    done:
    if (status != 0) {
        hmi = NULL;
        if (handle != NULL) {
            dlclose(handle);
            handle = NULL;
        }
    } else {
        ALOGV("loaded HAL id=%s path=%s hmi=%p handle=%p",
                id, path, *pHmi, handle);
    }

    *pHmi = hmi;

    return status;
}
(7) JNI 怎么使用 HAL

a.    JNI 调用hw_get_module通过模块的名字"led"打开某个so文件获得一个hw_module_t结构体

b. 之后通过get_device(module,LIGHT_ID_BACKLIGHT);传入一个设备的名字,从hw_get_module中获取这个设备hw_device_t

调用module->methods->open(module, device_name, &device)获得一个hw_device_t结构体,并且把hw_device_t结构体转换为设备自定义的结构体light_device_t。

light_device_t第一个成员是hw_device_t,可以类型转换。

static light_device_t* get_device(hw_module_t* module, char const* name)
{
    int err;
    hw_device_t* device;
    err = module->methods->open(module, name, &device);
    if (err == 0) {
        return (light_device_t*)device;
    } else {
        return NULL;
    }
}

(8)HAL 怎么写

a. 实现一个名为HMI的hw_module_t结构体

b. 实现一个open函数, 它会根据device_name返回一个设备自定义的结构体

c.实现一个设备结构体,这个设备自定义的结构体的第1个成员是 hw_device_t结构体,还可以定义设备相关的成员。如:

struct led_device_t {
    struct hw_device_t common;
	int (*led_open)(struct led_device_t* dev);
	int (*led_ctrl)(struct led_device_t* dev, int which, int status);
};
(9)说了半天模块的结构体,下面来看一下HAL层重要的几个结构体

hw_module_t结构体、hw_module_methods_t结构体、hw_device_t结构体

hw_module_t:

表示硬件模块,它主要包含了一些硬件模块的信息,结构体的定义:

里面hw_module_methods_t,这个指针methods它指向的是与本硬件模块相关的方法的结构体

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;

#ifdef __LP64__
    uint64_t reserved[32-7];
#else
    /** padding to 128 bytes, reserved for future use */
    uint32_t reserved[32-7];
#endif

} hw_module_t;

hw_module_methods_t:

函数指针open它是打开硬件模块中硬件设备的函数

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;

hw_device_t:

来描述模块中硬件设备的属性信息。一个硬件模块可能有多个硬件设备。

比如说,传感器模块,sensor_module,是一个硬件模块,但是手机中的传感器就对应的有好多种,比如加速度acc_sensor,磁传感器M_sensor等,那么他们都属于sensor_module,但是他们有都有自己的hw_device_t结构体来描述。

module指向的是这个设备归属的硬件模块结构体。
函数指针close指向的是关闭设备的函数。

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 */
#ifdef __LP64__
    uint64_t reserved[12];
#else
    uint32_t reserved[12];
#endif

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

} hw_device_t;

二、代码实现:

(1)JNI文件实现(frameworks/base/services/core/jni/com_android_server_LedService.cpp

#define LOG_TAG "LedService"

#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"

#include 
#include 

#include 

#include 
#include 
#include 
#include 
#include 
#include 


namespace android
{

static led_device_t* led_device;

jint ledOpen(JNIEnv *env, jobject cls)
{
	jint err;
    hw_module_t* module;
	hw_device_t* device;

	ALOGI("native ledOpen ...");

	/* 1. hw_get_module */
    err = hw_get_module("led", (hw_module_t const**)&module);
    if (err == 0) {
		/* 2. get device : module->methods->open */
	    err = module->methods->open(module, NULL, &device);
	    if (err == 0) {
			/* 3. call led_open */
	        led_device = (led_device_t *)device;
			return led_device->led_open(led_device);
	    } else {
	        return -1;
    	}
    }
	
    return -1;	
}

void ledClose(JNIEnv *env, jobject cls)
{
	//ALOGI("native ledClose ...");
	//close(fd);
}


jint ledCtrl(JNIEnv *env, jobject cls, jint which, jint status)
{
	ALOGI("native ledCtrl %d, %d", which, status);
	return led_device->led_ctrl(led_device, which, status);
}


static const JNINativeMethod methods[] = {
	{"native_ledOpen", "()I", (void *)ledOpen},
	{"native_ledClose", "()V", (void *)ledClose},
	{"native_ledCtrl", "(II)I", (void *)ledCtrl},
};
	

int register_android_server_LedService(JNIEnv *env)
{
    return jniRegisterNativeMethods(env, "com/android/server/LedService",
            methods, NELEM(methods));
}

}
(2)HAL代码实现

hardware/libhardware/include/hardware/led_hal.h

hardware/libhardware/modules/led/led_hal.c

hardware/libhardware/modules/led/Android.mk

led_hal.c:

把硬件文件加入工程,参考振动器


拷贝头文件

Android驱动(一)硬件访问服务学习之(三)Android加入HAL层访问硬件_第7张图片

/* 1. 实现一个名为HMI的hw_module_t结构体 */

 

/* 2. 实现一个open函数, 它返回led_device_t结构体*/

 

/* 3. 实现led_device_t结构体 */

 

/* 参考hardware\libhardware\modules\vibrator\vibrator.c

 */

Android驱动(一)硬件访问服务学习之(三)Android加入HAL层访问硬件_第8张图片

static struct hw_module_methods_tled_module_methods = {

   .open = led_device_open,

};

 

struct hw_module_t HAL_MODULE_INFO_SYM = {

   .id = "led",

    .methods = &led_module_methods,

};


#define LOG_TAG "LedHal"


/* 1. 实现一个名为HMI的hw_module_t结构体 */

/* 2. 实现一个open函数, 它返回led_device_t结构体 */

/* 3. 实现led_device_t结构体 */

/* 参考 hardware\libhardware\modules\vibrator\vibrator.c
 */

#include 
#include 

#include 

#include 
#include 
#include 
#include 

#include 

#include 
#include 
#include 
#include 
#include 
#include 


static int fd;


/** Close this device */
static int led_close(struct hw_device_t* device)
{
	close(fd);
	return 0;
}

static int led_open(struct led_device_t* dev)
{
	fd = open("/dev/leds", O_RDWR);
	ALOGI("led_open : %d", fd);
	if (fd >= 0)
		return 0;
	else
		return -1;
}

static int led_ctrl(struct led_device_t* dev, int which, int status)
{
	int ret = ioctl(fd, status, which);
	ALOGI("led_ctrl : %d, %d, %d", which, status, ret);
	return ret;
}




static struct led_device_t led_dev = {
	.common = {
		.tag   = HARDWARE_DEVICE_TAG,
		.close = led_close,
	},
	.led_open  = led_open,
	.led_ctrl  = led_ctrl,
};

static int led_device_open(const struct hw_module_t* module, const char* id,
        struct hw_device_t** device)
{
	*device = &led_dev;
	return 0;
}


static struct hw_module_methods_t led_module_methods = {
    .open = led_device_open,
};

struct hw_module_t HAL_MODULE_INFO_SYM = {
	.tag = HARDWARE_MODULE_TAG,
    .id = "led",
    .methods = &led_module_methods,
};

led_hal.h:
#ifndef ANDROID_LED_INTERFACE_H
#define ANDROID_LED_INTERFACE_H

#include 
#include 
#include 

#include 

__BEGIN_DECLS

struct led_device_t {
    struct hw_device_t common;

	int (*led_open)(struct led_device_t* dev);
	int (*led_ctrl)(struct led_device_t* dev, int which, int status);
};


__END_DECLS

#endif  // ANDROID_LED_INTERFACE_H


Android.mk
# Copyright (C) 2012 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := led.default

# HAL module implementation stored in
# hw/.default.so
LOCAL_MODULE_RELATIVE_PATH := hw            // /system/lib/hw
LOCAL_C_INCLUDES := hardware/libhardware    //头文件位置
LOCAL_SRC_FILES := led_hal.c                //对应的C文件
LOCAL_SHARED_LIBRARIES := liblog            //用的这个库来打印调试信息
LOCAL_MODULE_TAGS := eng                    //开发版本

include $(BUILD_SHARED_LIBRARY)

三、上传编译


JNI: 重新上传

frameworks/base/services/core/jni/com_android_server_LedService.cpp

 

HAL: led_hal.h

        led_hal.c

把新文件上传到服务器, 所在目录:

hardware/libhardware/include/hardware/led_hal.h

hardware/libhardware/modules/led/led_hal.c

hardware/libhardware/modules/led/Android.mk


编译:

$ mmm frameworks/base/services

$ mmm hardware/libhardware/modules/led

$ make snod

$ ./gen-img.sh

四、代码下载:

以上涉及代码下载地址:

基于HAL的Android硬件访问服务操作LED基于tiny4412


http://download.csdn.net/detail/fengyuwuzu0519/9883410




你可能感兴趣的:(Android底层,Android驱动学习之旅)