在A20上示範老羅的Android硬體抽象層(HAL)概要介紹和學習計劃3–關於hal部分

來源:互聯網
上載者:User

關於/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/了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.