1. 程式人生 > >Android HAL:helloworld例程

Android HAL:helloworld例程

傳一個Android HAL helloworld例程,這個例程參考“老羅的android之旅”的(http://blog.csdn.net/luoshengyang/article/details/6573809)。

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

hello.c

#define LOG_TAG "HelloStub"

#include <hardware/hardware.h>
//#include <hardware/hello.h>    //commented by vincent.wuyuwei
#include "hello.h"               //added by vincent.wuyuwei
#include <fcntl.h>
#include <errno.h>
#include <cutils/log.h>
#include <cutils/atomic.h>

#define DEVICE_NAME "/dev/hello"
#define MODULE_NAME "Hello"
#define MODULE_AUTHOR "
[email protected]
" /*裝置開啟和關閉介面*/ 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_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) { LOGE("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) { LOGE("Hello Stub: failed to open /dev/hello -- %s.", strerror(errno));free(dev); return -EFAULT; } *device = &(dev->common); LOGI("Hello Stub: open /dev/hello successfully."); return 0; } 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) { LOGI("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) { LOGE("Hello Stub: error val pointer"); return -EFAULT; } read(dev->fd, val, sizeof(*val)); LOGI("Hello Stub: get value %d from device", *val); return 0; }


Android.mk

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_SHARED_LIBRARIES := liblog
LOCAL_SRC_FILES := hello.c
LOCAL_MODULE := hello.default
include $(BUILD_SHARED_LIBRARY)

在驗證以上HAL例程中,筆者的過程是:

1.在 hardware/libhardware/modules中新建一個hello資料夾,然後將以上三個檔案放置在hello資料夾中。

2.在android原始碼根目錄下執行:source build/envsetup.sh 啟用mmm命令。

3.執行命令:mmm hardware/libhardware/modules/hello

如果不出錯則編譯提示如下:

target thumb C: hello.default <= hardware/libhardware/modules/hello/hello.c
target SharedLib: hello.default (out/target/product/generic/obj/SHARED_LIBRARIES/hello.default_intermediates/LINKED/hello.default.so)
target Symbolic: hello.default (out/target/product/generic/symbols/system/lib/hw/hello.default.so)
target Strip: hello.default (out/target/product/generic/obj/lib/hello.default.so)
Install: out/target/product/generic/system/lib/hw/hello.default.so

說明已經生成了.so檔案在out/target/product/generic/system/lib/hw/

4.執行命令make snod .重新打包system.img,新打包的system.img包含hello.default.so 庫。

如果提示“make: *** No rule to make target `out/target/product/generic/obj/lib/liblog.so'”

關於HAL編譯後的庫命名

從上面編譯知道,這個HAL例程編譯出來的庫命名為hello.default.so,而這個命名是由Android.mk的LOCAL_MODULE定義的,那麼在可不可以隨意定義編譯後庫的名字呢?答案是否定的。

Native部分會通過函式int hw_get_module(...)來得到我們的HAL模組,而這個函式根據模組id和路徑,並結合android的product和board等資訊查詢模組,最終會通過dlopen該so庫,所以上面庫檔案的命名定製最好是xxx.default.so或xxx.yyboard.so,關於命名的注意部分可以參考android的hardware.c檔案。

如果隨意命名HAL so名字,會容易導致沒法通過hw_get_module找到該so!!

相關推薦

Android HALhelloworld

傳一個Android HAL helloworld例程,這個例程參考“老羅的android之旅”的(http://blog.csdn.net/luoshengyang/article/details/6573809)。 hello.h #ifndef ANDROID_HE

dsp6657的helloworld測試-第二篇-CFG文件

world nbsp rate .com 第二篇 make *** names emp 1. 上一篇疑問,int StackTest()這個函數是怎麽運行的,後來在.cfg文件找到了答案,.cfg包含豐富的信息,對於用慣C語言的,確實不太習慣 1 var Memory

MT7688開發板/openwrt系統-helloworld

第一個例程,helloworld 驅動程式碼如下: #include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); MODULE_AUTHOR("Hanson He

QT使用2QT

目錄 test 官方文檔 快捷 流程 href 網站 擴展 time 轉至:http://blog.51cto.com/9291927/2138876 Qt開發學習教程 一、Qt開發基礎學習教程 本部分博客主要根據狄泰學院唐老師的《QT實驗分析教程》創作,同時根據天山老妖自

TensorFlow 從入門到精通(二)MNIST 原始碼分析

按照上節步驟, TensorFlow 預設安裝在 /usr/lib/python/site-packages/tensorflow/ (也有可能是 /usr/local/lib……)下,檢視目錄結構: # tree -d -L 3 /usr/lib/pyt

【邊做項目邊學Android】異常處理android.os.NetworkOnMainThreadException--多線問題

不能 timeout throws extend dex com order trace res 一切搞定。以為高枕無憂了,結果還是有問題! log開始報錯了,獲取更新信息異常。。!debug一下。發現Exception:android.os.NetworkOnM

aNDROID動態桌布交流

music lis and android hao123 androi app http 5% %E7%AC%AC%E4%B8%89%E6%96%B9app%E7%94%A8%E5%BE%AE%E4%BF%A1%E8%B4%A6%E5%8F%B7%E7%99%BB%E5%B

《JAVA多線核心技術》 筆記第六章模式與多線

會有 isp left sync con 多線程編程 鎖機制 數據 range 一、立即加載/"餓漢模式"和延遲加載/"懶漢模式" 立即加載(又稱餓漢模式):在使用類的時候已經將對象創建完畢,常見實現方法是直接new實例化 延遲加載(又稱懶漢模式):在調用get

Android Jni

1.Jni的作用 Java是一種比較高階的語言,Java呼叫c庫,呼叫c++庫是必不可少的,所以Jni就應運而生了。 NDK:NDK是Native Development Kit的縮寫,是Google提供的一套工具集,可以讓你其他語言(C、C++或彙編)開發 Androi

安卓學習筆記 -- (安裝環境) Android Studio安裝配置、環境搭建詳細步驟及基本使用 Android Studio和SDK官方開發工具下載 Android Studio教程從入門到精通 Android開發-之第一個程式HelloWorld

1、下載Android Studio安裝配置、環境搭建詳細步驟及基本使用    https://www.cnblogs.com/yanglh6-jyx/p/Android_AS_Configuration.html https://blog.csdn.net/k491022087/ar

NeuralTalk一種基於Python+numpy使用語句描述影象的多模態遞迴神經網路的

NeuralTalk工程的流程如下: The pipeline for the project looks as follows: 輸入資料使用Amazon Mechanical Turk收集的影象和5組語句描述的資料集。 The input is a dataset of im

本地系統服務Nt和Zw系列函式

Windows本地作業系統服務API由一系列以Nt或Zw為字首的函式實現的,這些函式以核心模式執行,核心驅動可以直接呼叫這些函式,而使用者層程式只能通過系統進行呼叫。通常情況下使用者層應用程式不會直接呼叫Nt和Zw系函式,更多的是通過直接呼叫Win32函式,這些Win32函式內部會呼叫Nt和Zw系函式,但也僅

OpenCV3圖片融合——addWeighted(),createTrackbar()函式使用

滑動條的建立和使用: addWeighted(),createTrackbar()函式 //---------------------【滑動條的建立和使用】----------------------- #include "pch.h" //#include <

C++C語言實現HTTP的GET和POST請求參考 C++C語言實現HTTP的GET和POST請求

C++:C語言實現HTTP的GET和POST請求   閱讀目錄   HTTP請求和IP/TCP   實現GET請求   實現POST請求:   參考: 回到頂部

Android開發入門之第一個安卓工程HelloWorld!

前提: 已安裝完畢AndroidStudio,安裝指導可以參考:https://www.jianshu.com/p/a0e0e11cac1f 開始第一個安卓工程 新建工程 第一步中如果是Kotlin,就勾選“Include Kotlin support”;否則就不勾選

無鎖執行緒通訊(2)與分析

 首先,我們來看一個例子。   1 //    互動通道“沒有貨”狀態  2 #define COMMUNICATIONCHANNEL_STATE_THINGSNOTEXISTS 0  3 //    互動通道“有貨”狀態  4 #define COMMUNICATIONCHANNEL_STATE_

Android HAL hardware module分析 以GPS module為

準備在S5PV210的android系統中實現GPS功能,程式已經寫好了,也可以編譯出gps.s5pc110.so的庫,但是不知道上層怎麼呼叫這個庫,在GPS相關的Android.mk中也沒有找到,很是納悶。通過分析hardware module才知道是怎麼回事,之前並沒有詳細的瞭解hardware modu

Python使用adodbapi存取二進位制資料

昨天嘗試了adodbapi的常用操作,今天寫一個存取二進位制圖片的程式試試,ado裡面存取二進位制資料還是要稍複雜點,不知道adodbapi表現又怎麼樣。 例程功能: 1. 新建一個測試表,用於儲存圖片資料 2. 讀取源圖片資料,插入一條記錄到測試表 3. 讀取新插入的表記錄

編譯ffmpeg時,報錯undefined reference

編譯ffmpeg例程,報了未定義錯誤: main.cpp:(.text+0x3a): undefined reference to avcodec_send_frame(AVCodecContext*, AVFrame const*)' main.

scanf函式總結注意事項及

一、scanf函式小結 1>scanf函式的說明及其注意事 scanf函式說明:     a. 如果使用這個函式,要包含一個頭檔案 stdio.h。     b.scanf函式是一個阻塞式函式:函式執行後,會等待使用者的輸入,如果使用者不輸入任何內容,它會一直等待。 scanf函式的作用: