在A20上演示老罗的Android硬件抽象层(HAL)概要介绍和学习计划3--关于hal部分

来源:互联网 发布:esxi mac os 10.11 编辑:程序博客网 时间:2024/05/17 07:10

关于/dev/hello的权限,修改的地方的方法有很多,我现在把它放在device/softwinner/wing-xxxx/下的ueventd.sun7i.rc修改:

diff --git a/ueventd.sun7i.rc b/ueventd.sun7i.rc
index d79557e..0bb9fb0 100644
--- a/ueventd.sun7i.rc
+++ b/ueventd.sun7i.rc
@@ -16,5 +16,6 @@
 /dev/ttyACM2                     0777   system     system
 /dev/gps                  0777   system     system
 /dev/pa_dev                              0777   system     system
+/dev/hello                               0777   system     system
 /dev/sunxi_mem           0666   media      media
 /dev/g2d                                 0666   system     system


----------------------------------------------------------------------------------------------------

关于 HAL层的代码我也习惯喜欢放在/device/softwinner/common/hardware下面,一般不去hardware/libhardware下增加模块,但道理是一样的。

现在在device/softwinner/common/hardware/下增加hello的文件夹,这个hello文件夹放下面三个文件:

1,Android.mk:

# 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.
LOCAL_PATH := $(call my-dir)
# HAL module implemenation, not prelinked, and stored in
# hw/<SENSORS_HARDWARE_MODULE_ID>.<ro.product.board>.so
include $(CLEAR_VARS)
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_MODULE := hello.default
LOCAL_MODULE_TAGS := eng optional


LOCAL_SRC_FILES := hello.c


LOCAL_SHARED_LIBRARIES := liblog libcutils libdl
include $(BUILD_SHARED_LIBRARY)


2,hello.h:

#ifndef ANDROID_HELLO_INTERFACE_H  
#define ANDROID_HELLO_INTERFACE_H  
#include <hardware/hardware.h>  


__BEGIN_DECLS  


/*定义模块ID*/  
#define HELLO_HARDWARE_MODULE_ID "hello"  


/*硬件模块结构体*/  
struct hello_module_t {  
    struct hw_module_t common;  
};  


/*硬件接口结构体*/  
struct hello_device_t {  
    struct hw_device_t common;  
    int fd;  
    int (*set_val)(struct hello_device_t* dev, int val);  
    int (*get_val)(struct hello_device_t* dev, int* val);  
};  


__END_DECLS  


#endif  


3:hello.c:

#define LOG_TAG "HelloStub"  


#include <hardware/hardware.h>  
#include "hello.h" 
#include <fcntl.h> 
#include <errno.h> 


#include <utils/Atomic.h>
#include <utils/Log.h>
#include <cutils/log.h>


#include <cutils/properties.h>
#include <linux/input.h>
#include <dirent.h>
#include <math.h>
#include <stdlib.h>




#define DEVICE_NAME "/dev/hello"  
#define MODULE_NAME "Hello"  
#define MODULE_AUTHOR "shyluo@gmail.com"  


/*设备打开和关闭接口*/  
static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device);  
//static int hello_device_close(struct hw_device_t* device);  


/*设备访问接口*/  
//static int hello_set_val(struct hello_device_t* dev, int val);  
//static int hello_get_val(struct hello_device_t* dev, int* val);  


/*模块方法表*/  
static struct hw_module_methods_t hello_module_methods = {  
          open: hello_device_open  
};  


/*模块实例变量*/  
struct hello_module_t HAL_MODULE_INFO_SYM = {  
    common: {  
    tag: HARDWARE_MODULE_TAG,  
     version_major: 1,  
     version_minor: 0,  
     id: HELLO_HARDWARE_MODULE_ID,  
     name: MODULE_NAME,  
     author: MODULE_AUTHOR,  
     methods: &hello_module_methods,  
       }  
};  








static int hello_device_close(struct hw_device_t* device) {  


    struct hello_device_t* hello_device = (struct hello_device_t*)device;  


    if(hello_device) {  
        close(hello_device->fd);  
        free(hello_device);  
    }  


    return 0;  
}  


static int hello_set_val(struct hello_device_t* dev, int val) {  


    ALOGI("Hello Stub: set value %d to device.", val);  
    write(dev->fd, &val, sizeof(val));  


    return 0;  
}  


static int hello_get_val(struct hello_device_t* dev, int* val) {  


    if(!val) {  
        ALOGE("Hello Stub: error val pointer");  
        return -EFAULT;  
    }  
    read(dev->fd, val, sizeof(*val));  
    ALOGI("Hello Stub: get value %d from device", *val);  


    return 0;  
}  


static int hello_device_open(const struct hw_module_t* module, const char* name, struct hw_device_t** device) {  


    struct hello_device_t* dev;


    dev = (struct hello_device_t*)malloc(sizeof(struct hello_device_t));  


    if(!dev) {  
        ALOGE("Hello Stub: failed to alloc space");  
        return -EFAULT;  
    }  


    memset(dev, 0, sizeof(struct hello_device_t));  
    dev->common.tag = HARDWARE_DEVICE_TAG;  
    dev->common.version = 0;  
    dev->common.module = (hw_module_t*)module;  
    dev->common.close = hello_device_close;  
    dev->set_val = hello_set_val;
    dev->get_val = hello_get_val;  


    if((dev->fd = open(DEVICE_NAME, O_RDWR)) == -1) {  
        ALOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev);  
        return -EFAULT;  
    }  


    *device = &(dev->common);  
    ALOGI("Hello Stub: open /dev/hello successfully.");  


    return 0;  
}  


这样就可以在编译的时候把它编译进system/lib/hw/了。