1. 定义 AIDL
1.1 创建 aidl 文件 frameworks/base/core/java/android/hardware/hello/IHelloHalManager.aidl:
package android.hardware.hello;
/**
* {@hide}
*/
interface IHelloHalManager {
void hellohal_write(String str);
String hellohal_read();
}
1.2 接着编译:
source build/envsetup.sh
lunch sdk_phone64_x86_64-trunk_staging-eng
make update-api
make -j32
2. 硬件服务 Server 端实现
2.1 在 frameworks/base/services/core/java/com/android/server 目录下添加:
hello/
└── HelloService.java
2.2 其中 HelloService.java 的内容如下:
package com.android.server.hello;
import android.Manifest;
import android.annotation.Nullable;
import android.app.ActivityManager;
import android.content.Context;
import android.hardware.hello.*;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.Trace;
import android.provider.Settings;
import android.util.Slog;
import android.util.SparseArray;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.display.BrightnessSynchronizer;
import com.android.internal.util.DumpUtils;
import com.android.internal.util.Preconditions;
import com.android.server.SystemService;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.annotation.NonNull;
import android.hardware.hello.IHelloHalManager;
import android.hardware.hello.IHelloHal;
public class HelloService extends SystemService {
// aidl hal 服务
private IHelloHal mVintfHelloHal = null;
// SystemServer 中的硬件服务
private IHelloHalManager mHelloManager = null;
private final class HelloManagerService extends IHelloHalManager.Stub {
@Override
public void hellohal_write( @NonNull String str) throws android.os.RemoteException {
try {
if(mVintfHelloHal != null) {
mVintfHelloHal.hello_write(str);
}
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
}
@Override
public String hellohal_read() throws android.os.RemoteException {
try {
if(mVintfHelloHal != null) {
return mVintfHelloHal.hello_read();
}
} catch (RemoteException e) {
throw e.rethrowFromSystemServer();
}
return "";
}
}
public HelloService( Context context ) {
super(context);
IBinder binder = Binder.allowBlocking(
ServiceManager.waitForDeclaredService(IHelloHal.DESCRIPTOR + "/default"));
if (binder != null) {
mVintfHelloHal = IHelloHal.Stub.asInterface(binder);
}
mHelloManager = new HelloManagerService();
}
@Override
public void onStart() {
publishBinderService("hello", (IBinder)mHelloManager);
}
@Override
public void onBootPhase(int phase) {
}
}
2.3 接着在 SystemServer 中添加启动服务的代码:
// 不要忘了导包
import com.android.server.hello.HelloService;
private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) {
// ......
t.traceBegin("StartHelloHalService");
mSystemServiceManager.startService(HelloService.class);
t.traceEnd();
//......
}
3. 硬件服务客户端辅助代码实现
3.1 接着创建客户端的接口:
// frameworks/base/core/java/android/hardware/hello/HelloServiceManager.java
package android.hardware.hello;
import android.annotation.SystemService;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Singleton;
import android.os.ServiceManager;
import android.annotation.Nullable;
/**
* { @hide }
*/
public final class HelloServiceManager {
final Context mContext;
/*
* @hide
*/
public HelloServiceManager (Context systemContext) {
mContext = systemContext;
}
/**
* @hide 只能系统调用
*/
public static IHelloHalManager getServerice(){
return I_HELLOHAL_MANAGER_SINGLETON.get();
}
//限制framework中的定义无法被外部应用访问
@UnsupportedAppUsage
private static final Singleton<IHelloHalManager> I_HELLOHAL_MANAGER_SINGLETON =
new Singleton<IHelloHalManager>() {
@Override
protected IHelloHalManager create() {
final IBinder b= ServiceManager.getService("hello");
final IHelloHalManager m = IHelloHalManager.Stub.asInterface(b);
return m;
}
};
public void hellohal_write( @Nullable String str){
try{
getServerice().hellohal_write(str);
return;
}catch (RemoteException e){
throw e.rethrowFromSystemServer();
}
}
@Nullable
public String hellohal_read() {
try{
return getServerice().hellohal_read();
}catch (RemoteException e){
throw e.rethrowFromSystemServer();
}
}
}
3.2 接着修改 frameworks/base/core/java/android/app/SystemServiceRegistry.java:
// 不要忘了 import 包
import android.hardware.hello.HelloServiceManager;
static {
//......
registerService("hello", HelloServiceManager.class,
new CachedServiceFetcher<HelloServiceManager>() {
@Override
public HelloServiceManager createService(ContextImpl ctx)
throws ServiceNotFoundException {
return new HelloServiceManager(ctx);
}});
//......
}
3.3 接着编译:
source build/envsetup.sh
lunch sdk_phone64_x86_64-trunk_staging-eng
make update-api
make -j32
4. App 访问硬件服务
简单起见,这里我们直接在 Launcher 中加访问硬件服务的代码。
// packages/apps/Launcher3/src/com/android/launcher3/Launcher.java
// 不要忘了导包
import android.hardware.hello.HelloServiceManager;
protected void onCreate(Bundle savedInstanceState) {
HelloServiceManager manager = (HelloServiceManager) getSystemService("hello");
manager.hellohal_write("nihao");
}
5. Selinux 配置
5.1 在 system/sepolicy/prebuilts/api/34.0/private/service.te system/sepolicy/private/service.te system/sepolicy/prebuilts/api/202404/private/service.te中添加:
hello_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
5.2 在 system/sepolicy/prebuilts/api/34.0/private/service_contexts system/sepolicy/prebuilts/api/202404/private/service_contexts system/sepolicy/private/service_contexts 中添加:
hello u:object_r:hello_service:s0
5.3 在 system/sepolicy/prebuilts/api/34.0/private/system_app.te system/sepolicy/prebuilts/api/202404/private/system_app.te和 system/sepolicy/private/system_app.te:
allow system_app hello_service:service_manager find;
5.4 在/system/sepolicy/build/soong/service_fuzzer_bindings.go文件中添加如下代码
"hello": []string{},
"*": EXCEPTION_NO_FUZZER,
}
)
6. 编译运行
6.1 接着编译
source build/envsetup.sh
lunch sdk_phone64_x86_64-trunk_staging-eng
make update-api
make -j32
6.2 查看日志
wj071842154@wj071842154:~/android/aosp$ adb logcat | grep hello
09-25 22:52:49.781 0 0 I init : Parsing file /vendor/etc/init/android.hardware.hello.rc...
09-25 22:52:54.518 0 0 I init : starting service 'vendor.hellohal-default'...
09-25 22:52:54.523 0 0 I init : ... started service 'vendor.hellohal-default' has pid 335
09-25 22:52:54.462 335 335 D HelloHalImpl: HelloHalImpl instance =android.hardware.hello.IHelloHal/default sde =0x74aba1271450
09-25 22:52:54.590 0 0 I servicemanager: Caller(pid=335,uid=0,sid=u:r:hal_hellohal_default:s0) Found android.hardware.hello.IHelloHal/default in device VINTF manifest.
09-25 22:53:43.380 622 622 I SystemServiceManager: Starting com.android.server.hello.HelloService
09-25 22:53:43.485 0 0 I servicemanager: Caller(pid=622,uid=1000,sid=u:r:system_server:s0) Found android.hardware.hello.IHelloHal/default in device VINTF manifest.
09-25 22:53:43.496 622 622 D SystemServerTiming: OnBootPhase_480_com.android.server.hello.HelloService
09-25 22:53:43.496 622 622 V SystemServerTiming: OnBootPhase_480_com.android.server.hello.HelloService took to complete: 0ms
09-25 22:53:44.038 622 622 D SystemServerTiming: OnBootPhase_500_com.android.server.hello.HelloService
09-25 22:53:44.038 622 622 V SystemServerTiming: OnBootPhase_500_com.android.server.hello.HelloService took to complete: 0ms
09-25 22:53:44.309 622 622 D SystemServerTiming: OnBootPhase_520_com.android.server.hello.HelloService
09-25 22:53:44.309 622 622 V SystemServerTiming: OnBootPhase_520_com.android.server.hello.HelloService took to complete: 0ms
09-25 22:53:45.950 622 622 D SystemServerTiming: OnBootPhase_550_com.android.server.hello.HelloService
09-25 22:53:45.950 622 622 V SystemServerTiming: OnBootPhase_550_com.android.server.hello.HelloService took to complete: 0ms
09-25 22:53:47.493 622 622 D SystemServerTiming: OnBootPhase_600_com.android.server.hello.HelloService
09-25 22:53:47.493 622 622 V SystemServerTiming: OnBootPhase_600_com.android.server.hello.HelloService took to complete: 0ms
09-25 22:53:48.895 622 622 D SystemServerTimingAsync: ssm.onStartUser-0_com.android.server.hello.HelloService
09-25 22:53:48.895 622 622 V SystemServerTimingAsync: ssm.onStartUser-0_com.android.server.hello.HelloService took to complete: 0ms
09-25 22:54:01.207 622 656 D ActivityManagerTiming: OnBootPhase_1000_com.android.server.hello.HelloService
09-25 22:54:01.207 622 656 V ActivityManagerTiming: OnBootPhase_1000_com.android.server.hello.HelloService took to complete: 0ms
09-25 22:54:03.877 622 665 D SystemServerTimingAsync: ssm.onUnlockingUser-0_com.android.server.hello.HelloService
09-25 22:54:03.877 622 665 V SystemServerTimingAsync: ssm.onUnlockingUser-0_com.android.server.hello.HelloService took to complete: 0ms
09-25 22:54:05.118 622 665 D SystemServerTimingAsync: ssm.onUnlockedUser-0_com.android.server.hello.HelloService
09-25 22:54:05.118 622 665 V SystemServerTimingAsync: ssm.onUnlockedUser-0_com.android.server.hello.HelloService took to complete: 0ms
09-25 22:54:10.247 622 1758 D SystemServerTimingAsync: ssm.onCompletedEventUser-0_{|Unlocked|}_com.android.server.hello.HelloService
09-25 22:54:10.248 622 1758 V SystemServerTimingAsync: ssm.onCompletedEventUser-0_{|Unlocked|}_com.android.server.hello.HelloService took to complete: 1ms