實作自己的HAL2 HAL helloworld 之HAL層代碼的撰寫
撰寫目標
- 撰寫一個實作簡單功能(加法運算)的HAL代碼,不涉及驅動操作
- 編譯通過并打包到system.ing中
撰寫頭檔案helloworld.h
#define ANDROID_HELLOWORLD_INTERFACE_H
#include <hardware/hardware.h>
__BEGIN_DECLS
//定義模塊ID
#define HELLOWORLD_HARDWARE_MODULE_ID "helloworld"
//硬體模塊結構
struct helloworld_module_t
{
struct hw_module_t common;
char * description;
int methodsNum;
};
//硬體介面結構體
struct helloworld_device_t
{
struct hw_device_t common;
int (*helloworld_add) (struct helloworld_device_t *dev, int a , int b, int *c);
};
__END_DECLS
撰寫helloworld.c
- 在系統原始碼的這個路徑 Z:\itop-3399_8.1\hardware\libhardware\modules ,下新建helloworld 檔案夾
- 在helloworld檔案夾下創建helloworld.c檔案
代碼
#define LOG_TAG "HelloStub"
#include <hardware/hardware.h>
#include <malloc.h>
#include <fcntl.h>
#include <errno.h>
#include <cutils/log.h>
#include <hardware/helloworld.h>
#define MODULE_NAME "helloworld"
#define MODULE_DES "HelloWorld : Implement Add function"
#define MODULE_AUTHOR "963416867@qq.com"
static int helloworld_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device);
static int helloworld_close(struct hw_device_t* device);
static int helloworld_add(struct helloworld_device_t *dev, int a , int b, int *c);
static int helloworld_open(const struct hw_module_t* module, const char* id, struct hw_device_t** device){
__android_log_print(ANDROID_LOG_INFO, LOG_TAG, "helloworld_open");
struct helloworld_device_t *dev;
dev = (struct helloworld_device_t *)malloc(sizeof(struct helloworld_device_t));
if(!dev){
ALOGE("Helloworld open: faild to alloc device space");
return -EFAULT;
}
memset(dev, 0, sizeof(struct helloworld_device_t));
dev->common.tag = HARDWARE_MODULE_TAG;
dev->common.version = 0;
dev->common.module = (hw_module_t *)module;
dev->common.close = helloworld_close;
dev->helloworld_add = helloworld_add;
* device = &(dev->common);
ALOGE("helloworld open: driver file successfully");
return 0;
}
static struct hw_module_methods_t helloworld_module_methods_t = {
.open = helloworld_open
};
//module struct 注意這里必須是 HAL_MODULE_INFO_SYM 變數名
struct helloworld_module_t HAL_MODULE_INFO_SYM = {
.common = {
.tag = HARDWARE_MODULE_TAG,
.module_api_version = 1,
.hal_api_version = 0,
.id = HELLOWORLD_HARDWARE_MODULE_ID,
.name = MODULE_NAME,
.author = MODULE_AUTHOR,
.methods = &helloworld_module_methods_t
},
.description = MODULE_DES,
.methodsNum = 3
};
static int helloworld_close(struct hw_device_t* device){
struct helloworld_device_t* helloworld_device= (struct helloworld_device_t *)device;
if(helloworld_device){
free(helloworld_device);
}
return 0;
}
static int helloworld_add(struct helloworld_device_t *dev, int a , int b, int *c){
ALOGI("helloworld add: success %d + %d", a, b);
*c = a + b;
return 0;
}
撰寫Android.mk 編譯檔案
- 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 stored in
# hw/<OVERLAY_HARDWARE_MODULE_ID>.<ro.product.board>.so
include $(CLEAR_VARS)
#用于列印的庫
LOCAL_LDLIBS := -llog
LOCAL_MODULE_RELATIVE_PATH := hw
LOCAL_PROPRIETARY_MODULE := true
LOCAL_SRC_FILES := helloworld.c
LOCAL_HEADER_LIBRARIES := libhardware_headers
LOCAL_MODULE := helloworld.default
LOCAL_CFLAGS:= -DLOG_TAG=\"helloworld\"
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)
編譯自己寫的helloworld hal
- 在編譯這個模塊前先將系統原始碼編譯通過,為編譯系統原始碼會生成好多工具,這些工具可以用來單獨模塊編譯
- 配置執行檔案
source build//envsetup.sh
- 選擇在什么環境編譯
lunch

- 編譯自己撰寫的模塊
mmm hardware/libhardware/modules/helloworld/
- 打包system.img 鏡像
make snod
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/286620.html
標籤:其他
上一篇:Android 計算器頁面設計
