基于Android 12的 oemnetd 定制化实例

本文介绍了Android 12S版本中oemnetd的定制方法,它作为网络管理后台进程的接口,允许OEM厂商在不修改主要netd模块的情况下添加自定义功能。通过分析AIDL接口和示例代码,展示了如何注册OEM未公开事件监听器,并在框架中使用这些接口。此外,还提到了在Android构建系统中添加依赖和权限声明的步骤。
摘要由CSDN通过智能技术生成

oem netd 的定制

netd 作为网络管理与控制的后台守护进程。 在接口定制中随着Android 平台的升级 控制的更加严格
平台为了达到stable 的效果。
对我们来说 ,应该在可控的前提下进行开发
从代码中 我们看到预留了oem定制的接口 不受netd版本的限制
基于没有太多资料介绍oemnetd开发的内容
此文基于Android 12 S版本对此进行了简单介绍

平台预留接口

aidl_interface {
    // This interface is for OEM calls to netd and vice versa that do not exist in AOSP.
    // Those calls cannot be part of INetd.aidl and INetdUnsolicitedEventListener.aidl
    // because those interfaces are versioned.
    // These interfaces must never be versioned or OEMs will not be able to change them.
    name: "oemnetd_aidl_interface",
    unstable: true,
    local_include_dir: "binder",
    srcs: [
        "binder/com/android/internal/net/IOemNetd.aidl",
        "binder/com/android/internal/net/IOemNetdUnsolicitedEventListener.aidl",
    ],
}

需要定制的接口 可以在 IOemNetd.aidl中声明

/**
 * Copyright (c) 2019, 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.
 */

package com.android.internal.net;

import com.android.internal.net.IOemNetdUnsolicitedEventListener;

/** {@hide} */
interface IOemNetd {
   /**
    * Returns true if the service is responding.
    */
    boolean isAlive();

   /**
    * Register oem unsolicited event listener
    *
    * @param listener oem unsolicited event listener to register
    */
    void registerOemUnsolicitedEventListener(IOemNetdUnsolicitedEventListener listener);
}

分析

关于omented 如何使用 。在一个测试文件中给出了demo

TEST_F(NetdBinderTest, OemNetdRelated) {
    sp<IBinder> binder;
    binder::Status status = mNetd->getOemNetd(&binder);
    EXPECT_TRUE(status.isOk()) << status.exceptionMessage();
    sp<com::android::internal::net::IOemNetd> oemNetd;
    if (binder != nullptr) {
        oemNetd = android::interface_cast<com::android::internal::net::IOemNetd>(binder);
    }
    ASSERT_NE(nullptr, oemNetd.get());

    TimedOperation t("OemNetd isAlive RPC");
    bool isAlive = false;
    oemNetd->isAlive(&isAlive);
    ASSERT_TRUE(isAlive);

    class TestOemUnsolListener
        : public com::android::internal::net::BnOemNetdUnsolicitedEventListener {
      public:
        android::binder::Status onRegistered() override {
            std::lock_guard lock(mCvMutex);
            mCv.notify_one();
            return android::binder::Status::ok();
        }
        std::condition_variable& getCv() { return mCv; }
        std::mutex& getCvMutex() { return mCvMutex; }

      private:
        std::mutex mCvMutex;
        std::condition_variable mCv;
    };

    // Start the Binder thread pool.
    android::ProcessState::self()->startThreadPool();

    android::sp<TestOemUnsolListener> testListener = new TestOemUnsolListener();

    auto& cv = testListener->getCv();
    auto& cvMutex = testListener->getCvMutex();

    {
        std::unique_lock lock(cvMutex);

        status = oemNetd->registerOemUnsolicitedEventListener(
                ::android::interface_cast<
                        com::android::internal::net::IOemNetdUnsolicitedEventListener>(
                        testListener));
        EXPECT_TRUE(status.isOk()) << status.exceptionMessage();

        // Wait for receiving expected events.
        EXPECT_EQ(std::cv_status::no_timeout, cv.wait_for(lock, std::chrono::seconds(2)));
    }
}

其中涉及的函数如下

binder::Status NetdNativeService::getOemNetd(android::sp<android::IBinder>* listener) {
    ENFORCE_NETWORK_STACK_PERMISSIONS();
    *listener = com::android::internal::net::OemNetdListener::getListener();

    return binder::Status::ok();
}

可以看出 我们通过lister实现功能即可 在这里既是:OemNetdListener.cpp

基于Android12平台:
如果framework 需要使用netd接口 需要将接口提供出去

--- a/netd/server/Android.bp
+++ b/netd/server/Android.bp
@@ -161,6 +161,18 @@ aidl_interface {
         "binder/com/android/internal/net/IOemNetd.aidl",
         "binder/com/android/internal/net/IOemNetdUnsolicitedEventListener.aidl",
     ],
+    backend: {
+
+        java: {
+            apex_available: [
+                "//apex_available:platform",
+                "com.android.bluetooth.updatable",
+                "com.android.wifi",
+                "com.android.tethering",
+            ],
+            min_sdk_version: "30",
+        },
+    },
 }

Framework中使用 需要将其加入依赖

diff --git a/libs/net/client-libs/Android.bp b/libs/net/client-libs/Android.bp
index 8c79d2a..1944ba1 100644
--- a/libs/net/client-libs/Android.bp
+++ b/libs/net/client-libs/Android.bp
@@ -20,6 +20,7 @@ java_library {
    libs: ["androidx.annotation_annotation"],
    static_libs: [
        "netd_aidl_interface-lateststable-java",
-        "netd_event_listener_interface-lateststable-java"
+        "netd_event_listener_interface-lateststable-java",
+        "oemnetd_aidl_interface-java"
    ]
}

在NetworkManagementService 中使用举例

--- a/base/services/core/java/com/android/server/NetworkManagementService.java
+++ b/base/services/core/java/com/android/server/NetworkManagementService.java
@@ -41,6 +41,8 @@ import static android.net.TrafficStats.UID_TETHERING;
 
 import static com.android.server.NetworkManagementSocketTagger.PROP_QTAGUID_ENABLED;
 
+import com.android.internal.net.IOemNetd;
+
 import android.annotation.NonNull;
 import android.app.ActivityManager;
 import android.content.Context;
  ...
+  IOemNetd.Stub.asInterface(mNetdService.getOemNetd());

基于Android 12的要求

需要在系统添加如下声明

--- a/common/build/allowed_deps.txt
+++ b/common/build/allowed_deps.txt
@@ -522,6 +522,7 @@ neuralnetworks_utils_hal_aidl(minSdkVersion:30)
 neuralnetworks_utils_hal_common(minSdkVersion:30)
 neuralnetworks_utils_hal_service(minSdkVersion:30)
 note_memtag_heap_async(minSdkVersion:16)
+oemnetd_aidl_interface-java(minSdkVersion:30)
 okhttp(minSdkVersion:31)
 OsuLoginGoogle(minSdkVersion:30)
 perfetto_trace_protos(minSdkVersion:S)

总结

oemnetd的优点即注释所描述
不需要改变可能被mainline的 netd模块


    // This interface is for OEM calls to netd and vice versa that do not exist in AOSP.
    // Those calls cannot be part of INetd.aidl and INetdUnsolicitedEventListener.aidl
    // because those interfaces are versioned.
    // These interfaces must never be versioned or OEMs will not be able to change them.
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值