android hal学习——编写hal代码

一、参考说明
http://blog.csdn.net/liuhaoyutz/article/details/9147877

下面这个是关于android的mk文件的变量的说明。
http://android.cloudchou.com/build/core/clear_vars.php

代码是拷贝http://blog.csdn.net/liuhaoyutz/article/details/9147877来的。
修改的地方是:
将example.cpp中的LOGE修改为ALOGE,LOGI修改为ALOGI.
在Android.mk文件中修改:
1、增加:LOCAL_LDLIBS+=-llog
2、注释:#LOCAL_MODULE_PATH

二、代码
1、在hardware/libhardware/include/hardware/增加:
example.h

#ifndef ANDROID_EXAMPLE_INTERFACE_H
#define ANDROID_EXAMPLE_INTERFACE_H

#include <hardware/hardware.h>

__BEGIN_DECLS

/**
 * The id of this module
 */
#define EXAMPLE_HARDWARE_MODULE_ID "example"

/**
 * The id of this device
 */
#define EXAMPLE_HARDWARE_DEVICE_ID "example"

struct example_module_t {
    struct hw_module_t common;
};

struct example_device_t {
    struct hw_device_t common;
    int fd;
    int (*set_val)(struct example_device_t* dev, int val);
    int (*get_val)(struct example_device_t* dev, int* val);
};

__END_DECLS

#endif

2、在hardware/libhardware/modules/新增文件夹:example
进入该文件夹,编写2个文件:
1)example.cpp

#define LOG_TAG "ExampleHALStub"

#include <hardware/hardware.h>
#include <hardware/example.h>

#include <fcntl.h>
#include <errno.h>

#include <cutils/log.h>
#include <cutils/atomic.h>

#define DEVICE_NAME "/dev/example"
#define MODULE_NAME "Example"
#define MODULE_AUTHOR "haoyu"

#include <string.h>
#include <stdlib.h>

 static int example_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device);
 static int example_device_close(struct hw_device_t* device);
 static int example_set_val(struct example_device_t* dev, int val);
 static int example_get_val(struct example_device_t* dev, int* val);

 static struct hw_module_methods_t example_module_methods = {
     .open= example_device_open
 };

 struct example_module_t HAL_MODULE_INFO_SYM = {
     .common= {
         .tag= HARDWARE_MODULE_TAG,
         .version_major= 1,
         .version_minor= 0,
         .id= EXAMPLE_HARDWARE_MODULE_ID,
         .name= MODULE_NAME,
         .author= MODULE_AUTHOR,
         .methods=&example_module_methods,
     }
 };

 static int example_device_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device) {
     if(!strcmp(id, EXAMPLE_HARDWARE_DEVICE_ID)) {
         struct example_device_t* dev;

         dev = (struct example_device_t*)malloc(sizeof(struct example_device_t));
         if(!dev) {
             ALOGE("Failed to alloc space for example_device_t.");
             return -EFAULT;
         }

         memset(dev, 0, sizeof(struct example_device_t));

         dev->common.tag = HARDWARE_DEVICE_TAG;
         dev->common.version = 0;
         dev->common.module = (hw_module_t*)module;
         dev->common.close = example_device_close;
         dev->set_val = example_set_val;
         dev->get_val = example_get_val;

         if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {
             ALOGE("Failed to open device file /dev/example -- %s.", strerror(errno));
             free(dev);
             return -EFAULT;
         }

         *device = &(dev->common);

         ALOGI("Open device file /dev/example successfully.");

         return 0;
     }

     return -EFAULT;
 }

 static int example_device_close(struct hw_device_t* device) {
     struct example_device_t* example_device = (struct example_device_t*)device;
     if(example_device) {
         close(example_device->fd);
         free(example_device);
     }

     return 0;
 }
static int example_set_val(struct example_device_t* dev, int val) {
     if(!dev) {
         ALOGE("Null dev pointer.");
         return -EFAULT;
     }

     ALOGI("Set value %d to device file /dev/example.", val);
     write(dev->fd, &val, sizeof(val));

     return 0;
 }

 static int example_get_val(struct example_device_t* dev, int* val) {
     if(!dev) {
         ALOGE("Null dev pointer.");
         return -EFAULT;
     }

    if(!val) {
        ALOGE("Null val pointer.");
        return -EFAULT;
    }

    read(dev->fd, val, sizeof(*val));

    ALOGI("Get value %d from device file /dev/example.", *val);

    return 0;
}


2)Android.mk

LOCAL_PATH :=$(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS :=optional
LOCAL_PRELINK_MODULE:=false

#LOCAL_MODULE_PATH :=$(TARGET_OUT_SHARED_LIBRARYS)/hw
LOCAL_SHARED_LIBRARYS :=liblog

LOCAL_LDLIBS +=-llog

LOCAL_SRC_FILES :=example.cpp
LOCAL_MODULE :=example.default

include $(BUILD_SHARED_LIBRARY)

三、执行
[gumh@localhost android-src]$ mmm hardware/libhardware/modules/example/

Starting build with ninja
ninja: Entering directory .'
[100% 3/3] Install: out/target/product/generic/system/lib/example.default.so
make: Leaving directory
/home/zzz/opensource/android-src’

[gumh@localhost android-src]$ make snod

-l 1610612736 -a system out/target/product/generic/system.img out/target/product/generic/system out/target/product/generic/system
Creating filesystem with parameters:
Size: 1610612736
Block size: 4096
Blocks per group: 32768
Inodes per group: 8192
Inode size: 256
Journal blocks: 6144
Label: system
Blocks: 393216
Block groups: 12
Reserved block group size: 95
Created filesystem with 1751/98304 inodes and 146360/393216 blocks
out/target/product/generic/system.img maxsize=1644333504 blocksize=2112 total=1610612736 reserve=16610880

make completed successfully (21 seconds)

system.img就是新的了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值