关闭

HAL开发全流程(一)

标签: android
561人阅读 评论(0) 收藏 举报
分类:

本文的源代码地址是:http://download.csdn.net/detail/yongyu_it/9544336


1、流程

1.0  HAL层模块开发-->本地访问模块开发-->系统级Android Service(java)开发-->应用程序开发

1.1  “HAL层模块”与“本地访问模块”交互要点:int hw_get_module(const char *id, const struct hw_module_t **module)(此函数定义在:源代码根/hardware/libhardware/hardware.c)

1.2  “本地访问模块”与“Android Service”交互要点:Jni

1.3  “Android Service”与“应用程序”交互要点:AIDL/Binder/跨进程

2、HAL层模块开发

2.1  HAL层模块命名

Android的HAL层是以模块(动态链接库文件)的形式管理各个硬件访问接口的。这些模块(动态链接库文件)的命名是有规定的。

命名规则如:<module_id>.<model_type>.so

2.2  硬件描述

android源代码为开发者预定义了两个结构体hw_module_t、hw_device_t来描述硬件(详见hardware/libhardware/include/hardware/hardware.h文件),其中HAL层模块用hw_module_t来描述,硬件设备用hw_device_t来描述。

2.3  HAL层开发(假设我们的硬件名称叫joffee)

joffee.h文件

/*
 * Copyright (C) 2008 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.
 */

#define  LOG_TAG    "libplasma"
#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)

#ifndef __ANDROID_JOFFEE_HW_INTERFACE__
#define __ANDROID_JOFFEE_HW_INTERFACE__

#include <stdint.h>
#include <sys/cdefs.h>
#include <sys/types.h>
#include <hardware/hardware.h>

__BEGIN_DECLS
#define JOFFEE_HARDWARE_MODULE_ID "joffee"
/**
 * The joffee device description structure;
 * First field must be the hw_device_t field;
 * Other fields can be function pointers and othe exported fields
 */
struct joffee_device_t {
  /* Will be used in HAL open method */
  struct hw_device_t common;

  /* Pointers to your HAL functions */
  int (*joffee_function)(void);
};
__END_DECLS
#endif //__ANDROID_JOFFEE_HW_INTERFACE__

joffee.cpp文件

/*
 * Copyright (C) 2014 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.
 *
 */

/**
 * @file   joffee.cpp
 * @brief  Implements Joffee's HW abstraction layer
 */
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <cutils/log.h>
#include <hardware/joffee.h>
#define TAG "JOFFEE!"

/**
 * The function(s) exported by this HAL
 */
int joffee_function_impl(){
  ALOGE(TAG, "Hello Android!!\n");
  /* Here you should interface with your HW devices... */

  return 0;
}

/** This is mandatory, and part of hw_device_t */
int close_joffee(hw_device_t* hw_dev) {
  //TODO
  return 0;
}

/**
 * A pointer to this method is stored in
 * hw_module_method_t.open;
 *
 * Once JNI loads the hw_module_method_t symbol, it
 * can call this function and "open" the HAL layer
 * receiving pointers to this module's additional methods
 */
static int open_joffee(const struct hw_module_t *module,
                       char const *name, struct hw_device_t **device) {
  struct joffee_device_t *dev = (struct joffee_device_t *)
    malloc(sizeof(*dev));

  if (NULL == dev) {
    ALOGE(TAG, "Unable to reserve memory for joffee: %s\n",
        strerror(errno));
        return -ENOMEM;
  }

  /* Store pointer to HAL function(s) */
  dev->joffee_function = joffee_function_impl;

  /* Initialize common fields */
  dev->common.tag = HARDWARE_DEVICE_TAG;
  dev->common.version = 0;
  dev->common.module = (struct hw_module_t *)module;
  dev->common.close = close_joffee;

  /* Store this module's structure in the output parameter 'device' */
  /* Remember the first field of your HAL device must be an hw_device_t */
  *device = (struct hw_device_t *)dev;
  return 0;
}
/*
 * The Joffee HAL description
 * Will be loaded using libhardware
 */
static struct hw_module_methods_t joffee_methods =
{
  .open =  open_joffee,
};

struct hw_module_t HAL_MODULE_INFO_SYM =
{
  .tag = HARDWARE_MODULE_TAG,
  .version_major = 0,
  .version_minor = 1,
  .id = JOFFEE_HARDWARE_MODULE_ID,
  .name = "Joffee HAL",
  .author = "Linaro",
  .methods = &joffee_methods,
};

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_LDLIBS    := -lm -llog

LOCAL_SRC_FILES := \
	joffee.cpp

LOCAL_MODULE_RELATIVE_PATH := hw

LOCAL_MODULE := joffee.default

LOCAL_MODULE_TAGS := optional

include $(BUILD_SHARED_LIBRARY)


2.4  编译

代码结构:

源代码根/hardware/libhardware--|

                                                           |--include/hardware--|

                                                           |                                     |--joffee.h

                                                           |--modules--|

                                                                                 |--joffee--|

                                                                                                 |--joffee.cpp

                                                                                                 |--Android.mk

在源代码根运行mmm ./hardware/libhardware/modules/joffee/ 即可编译,编译结果位于 根/out/target/product/generic/system/lib/hw/

待续...









1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:91075次
    • 积分:2851
    • 等级:
    • 排名:第12594名
    • 原创:192篇
    • 转载:43篇
    • 译文:1篇
    • 评论:8条
    最新评论