標籤:
在數字科技日新月異的今天,軟體和硬體的完美結合,造就了智能行動裝置的流行。今天大家對iOS和Android系統的趨之若鶩,一定程度上是由於這兩個系統上有著豐富多彩的各種應用軟體。因此,軟體和硬體的關係,在一定程度上可以說,硬體是為軟體服務的。硬體工程師研發出一款硬體裝置,自然少了軟體工程師為其編寫驅動程式;而驅動程式的最終目的,是為了使得最上層的應用程式能夠使用這些硬體提供的服務來為使用者提供軟體功能。對Android系統上的應用軟體來說,就是要在系統的Application Frameworks層為其提供硬體服務。在前面的幾篇文章中,我們著重介紹了Linux核心層、硬體抽象層和執行階段程式庫層提供的自訂硬體服務介面,這些介面都是通過C或者C++語言來實現的。在這一篇文章中,我們將介紹如何在Android系統的Application Frameworks層提供Java介面的硬體服務。
一. 參照在Ubuntu為Android硬體抽象層(HAL)模組編寫JNI方法提供Java訪問硬體服務介面一文所示,為硬體抽象層模組準備好JNI方法調用層。
二. 在Android系統中,硬體服務一般是運行在一個獨立的進程中為各種應用程式提供服務。因此,調用這些硬體服務的應用程式與這些硬體服務之間的通訊需要通過代理來進行。為此,我們要先定義好通訊介面。進入到frameworks/base/core/java/android/os目錄,新增IHelloService.aidl介面定義檔案:
[email protected]:~/Android$ cd frameworks/base/core/java/android/os
[email protected]:~/Android/frameworks/base/core/java/android/os$ vi IHelloService.aidl
IHelloService.aidl定義了IHelloService介面:
package android.os; interface IHelloService { void setVal(int val); int getVal(); }
IHelloService介面主要提供了裝置和擷取硬體寄存器val的值的功能,分別通過setVal和getVal兩個函數來實現。
三.返回到frameworks/base目錄,開啟Android.mk檔案,修改LOCAL_SRC_FILES變數的值,增加IHelloService.aidl源檔案:
## READ ME: ########################################################
##
## When updating this list of aidl files, consider if that aidl is
## part of the SDK API. If it is, also add it to the list below that
## is preprocessed and distributed with the SDK. This list should
## not contain any aidl files for parcelables, but the one below should
## if you intend for 3rd parties to be able to send those objects
## across process boundaries.
##
## READ ME: ########################################################
LOCAL_SRC_FILES += /
....................................................................
core/java/android/os/IVibratorService.aidl /
core/java/android/os/IHelloService.aidl /
core/java/android/service/urlrenderer/IUrlRendererService.aidl /
.....................................................................
四. 編譯IHelloService.aidl介面:
[email protected]:~/Android$ mmm frameworks/base
這樣,就會根據IHelloService.aidl產生相應的IHelloService.Stub介面。
五.進入到frameworks/base/services/java/com/android/server目錄,新增HelloService.java檔案:
package com.android.server; //服務包import android.content.Context; //匯入上下文import android.os.IHelloService; //匯入hello介面import android.util.Slog; //匯入log日誌public class HelloService extends IHelloService.Stub { private static final String TAG = "HelloService"; //該類中的私人部分 HelloService() { init_native(); } public void setVal(int val) { //公用部分 setVal_native(val); } public int getVal() { return getVal_native(); } private static native boolean init_native(); private static native void setVal_native(int val); private static native int getVal_native(); };
HelloService主要是通過調用JNI方法init_native、setVal_native和getVal_native(見在Ubuntu為Android硬體抽象層(HAL)模組編寫JNI方法提供Java訪問硬體服務介面一文)來提供硬體服務。
六. 修改同目錄的SystemServer.java檔案,在ServerThread::run函數中增加載入HelloService的代碼:
@Override
public void run() {
....................................................................................
try {
Slog.i(TAG, "DiskStats Service");
ServiceManager.addService("diskstats", new DiskStatsService(context));
} catch (Throwable e) {
Slog.e(TAG, "Failure starting DiskStats Service", e);
}
try {
Slog.i(TAG, "Hello Service");
ServiceManager.addService("hello", new HelloService());
} catch (Throwable e) {
Slog.e(TAG, "Failure starting Hello Service", e);
}
......................................................................................
}
七. 編譯HelloService和重新打包system.img:
[email protected]:~/Android$ mmm frameworks/base/services/java
[email protected]:~/Android$ make snod
這樣,重新打包後的system.img系統鏡像檔案就在Application Frameworks層中包含了我們自訂的硬體服務HelloService了,並且會在系統啟動的時候,自動載入HelloService。這時,應用程式就可以通過Java介面來訪問Hello硬體服務了。我們將在下一篇文章中描述如何編寫一個Java應用程式來調用這個HelloService介面來訪問硬體,敬請期待。
在Ubuntu上為Android系統的Application Frameworks層增加硬體訪問服務(老羅學習筆記5)