本文是接续上文,针对于HAL层的接口封装Framework层的接口
HAL层框架搭建:https://blog.csdn.net/m0_50408097/article/details/151148637?spm=1001.2014.3001.5502
在 Android 系统架构中,Framework 层(框架层) 位于 HAL 层(硬件抽象层)之上,是连接底层硬件能力与上层应用的核心中间层。它基于 HAL 层提供的硬件抽象接口,封装出更易用、更贴近应用开发的标准化服务和 API,供应用层调用。Framework框架搭建
Step1. 创建framework service(用于与hal层通信)

1.1    定义aidl接口(用于与manager进程间通信)
路径:/frameworks/base/core/java/android/app/ITestService.aidl

package android.app;

/** {@hide} */
interface ITestService {
    int additionTest(int a, int b);
}

注意:这里的接口要与.hal中定义的接口相同(函数名、返回值类型、参数类型和数量)

1.2    实现service
路径:/frameworks/base/services/core/java/com/android/server/TestService.java

package com.android.server;

import android.app.ITestService;
import android.content.Context;

public class TestService extends ITestService.Stub {

    private static final String TAG = " TestService ";
    private Context mContext;

    public TestService(Context context) {
      android.util.Log.d(TAG,"Start TestService...");
      mContext = context;
    }

    @Override
    public int additionTest(int a, int b) {
        android.util.Log.d(TAG,"yuan TestService additionTest()...");
        return additionTest_native(a, b);
    }

    private static native int additionTest_native(int a, int b);

}

1.3    在Context.java中定义新规的service
路径:/frameworks/base/core/java/android/content/Context.java

public static final String TEST_SERVICE = "test";

1.4    注册 Service 到 SystemServer
路径:frameworks/base/services/java/com/android/server/SystemServer.java#startOtherServices()

TestService testService = null;

traceBeginAndSlog("yuan StartTestService");
try {
    testService = new TestService(context);
    ServiceManager.addService(Context.TEST_SERVICE, testService);
} catch (Throwable e) {
    reportWtf("starting TestService", e);
}
traceEnd();


Step2. 创建JNI桥接代码(连接framework service和hal)
具体的JNI框架搭建可以参考:https://blog.csdn.net/m0_50408097/article/details/151115313?spm=1001.2014.3001.5506
2.1 定义jni文件
路径:frameworks/base/services/core/jni/com_android_server_Test.cpp

#include <jni.h>
#include <nativehelper/JNIHelp.h>
#include <binder/IServiceManager.h>
#include <vendor/xxx/hardware/test/1.0/ITest.h>
#include <log/log.h>
#include <android_runtime/AndroidRuntime.h>
#include <android_runtime/Log.h>

using android::hardware::Return;
using android::hardware::Void;
using android::sp;
using vendor::xxx::hardware::test::V1_0::ITest;

namespace android {

sp<ITest> rw_device;
std::string asyncResult;

void serialNoCallback(const ::android::hardware::hidl_string& serialNo) {
    asyncResult = serialNo.c_str();
}

static jint additionTest_native(JNIEnv *env, jobject /* thiz */, jint a, jint b) {
    ALOGI("additionTest_native.....");

    sp<IServiceManager> sm = defaultServiceManager();
    if (sm == nullptr) {
        ALOGE("failed to get ServiceManager");
        return -1;
    }

    rw_device = ITest::getService();
    if (rw_device == nullptr) {
        ALOGE("failed to get ITest service");
        return -1;
    }
    return rw_device->additionTest(a, b);
}

static const JNINativeMethod method_table[] = {
    { "additionTest_native", "(II)I", (void *)additionTest_native },
};

int register_android_server_TestService(JNIEnv* env) {
    return jniRegisterNativeMethods(env, "com/android/server/TestService",
            method_table, NELEM(method_table));
}

}; // namespace android

在additionTest_native()函数中直接调用HAL层的函数additionTest()

2.2 在 Android.bp 中添加编译依赖
① 新增JNI文件
路径:frameworks/base/services/core/jni/Android.bp#cc_library_static

"com_android_server_Test.cpp",

② 添加HAL依赖
路径:frameworks/base/services/core/jni/Android.bp# cc_defaults

"vendor.xxx.hardware.test@1.0",

2.3 注册 JNI 到 Android Runtime
① 声明一个 JNI 注册函数,用于将 TestService 的 Native 方法(C++ 实现)绑定到 Java 层
路径:frameworks/base/services/core/jni/onload.cpp

int register_android_server_TestService(JNIEnv* env);

② 在 JNI_OnLoad 函数中调用注册方法,完成 TestService 的 JNI 方法绑定

register_android_server_TestService(env);


Step3. 创建framework manager(给app提供API)
3.1 定义manager类
路径:frameworks/base/core/java/android/os/TestManager.java

package android.os;

import android.annotation.SuppressLint;
import android.annotation.SystemService;
import android.app.ITestService;
import android.content.Context;
import android.util.Log;

@SystemService(Context.TEST_SERVICE)
public class TestManager {

    private ITestService mService;
    private Context mContext;
    private static final String TAG = " TestManager ";
    private static final boolean Debug = true;

    /** @hide */
    public TestManager(Context context, ITestService service){
        mContext = context;
        mService = service;
    }

    @SuppressLint({"MissingNullability", "StartWithLower"})
    public int additionTest(int a, int b){
        Log.d(TAG, "yuan additionTest");
        try {
            return mService.additionTest(a, b);
        } catch (RemoteException e) {
            if (Debug) Log.e(TAG, e.getMessage());
        }
        return 0;
    }
}

在additionTest()方法中调用 mService.additionTest() 会通过 Binder IPC 将请求发送到系统服务端

3.2 注册 Manager 到 SystemServiceRegistry
路径:frameworks/base/core/java/android/app/SystemServiceRegistry.java

registerService(Context.TEST_SERVICE, TestManager.class,
                new CachedServiceFetcher<TestManager>() {
                    @Override
                    public TestManager createService(ContextImpl ctx)
                            throws ServiceNotFoundException {
                        IBinder b = ServiceManager.getServiceOrThrow(
                                Context.TEST_SERVICE);
                        return new TestManager(
                                ctx.getOuterContext(),ITestService.Stub.asInterface(b));
                    }});

Step4. 配置 SELinux 策略
4.1 允许 system_server 注册服务
路径:/system/sepolicy/private/system_server.te

#test service
allow system_server test_service:service_manager add;

4.2 允许普通应用查找服务
路径:/system/sepolicy/private/untrusted_app_all.te

allow untrusted_app_all test_service:service_manager find;

4.3 定义新的服务类型
路径:/system/sepolicy/public/service.te

type test_service, system_api_service, system_server_service, service_manager_type;

注意:以下te文件都需要添加4.1-4.3
/system/sepolicy/prebuilts/api/下所有的system_server.te && untrusted_app_all.te && service.te
/system/sepolicy/private/system_server.te
/system/sepolicy/private/untrusted_app_all.te
/system/sepolicy/public/service.te

4.4 允许 system_server 访问 HAL
路径:/device/xxx/xxx/sepolicy/non_plat/hal_test.te

allow system_app hal_test_hwservice:hwservice_manager { find };

4.5 允许 system_server 查找 hal_test_hwservice
路径:/device/xxx/xxx/sepolicy/non_plat/system_sever.te

allow system_server hal_test_hwservice:hwservice_manager find;

Step5. 更新framework 的公开 API 定义文件
用source和lunch加载一下环境变量

make update-api

执行后,自动更新current.txt
可以在frameworks/base库下,用git diff查看一下
 

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐