Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5fd7fa7b authored by Yu Shan's avatar Yu Shan Committed by Android (Google) Code Review
Browse files

Merge changes I7d338eef,I95c9f796,I56b160b6 into udc-dev

* changes:
  Change BindToDeviceSocketMutator to lib.
  Specify a eth interface for grpc connection.
  Add trace to measure VHAL performance.
parents 6ced2e42 2a096c49
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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 {
    default_applicable_licenses: ["Android-Apache-2.0"],
}

cc_defaults {
    name: "BindToDeviceSocketMutatorDefaults",
    static_libs: [
        "android.hardware.automotive.can@libnetdevice",
        "libnl++",
    ],
    shared_libs: [
        "libbase",
        "liblog",
        "libgrpc++",
    ],
    cflags: [
        "-Wno-unused-parameter",
    ],
}

cc_library {
    name: "BindToDeviceSocketMutatorLib",
    vendor_available: true,
    srcs: ["src/*"],
    export_include_dirs: ["include"],
    defaults: ["BindToDeviceSocketMutatorDefaults"],
}
+35 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.
 */

#pragma once

#include <grpc++/grpc++.h>
#include <src/core/lib/iomgr/socket_mutator.h>
#include <string>

namespace android::hardware::automotive::remoteaccess {

class BindToDeviceSocketMutator final : public grpc_socket_mutator {
  public:
    BindToDeviceSocketMutator(const std::string_view& interface_name);

    bool mutateFd(int fd);

  private:
    std::string mIfname;
};

}  // namespace android::hardware::automotive::remoteaccess
+57 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.
 */

#include "BindToDeviceSocketMutator.h"

#include <android-base/logging.h>
#include <errno.h>
#include <sys/socket.h>

namespace android::hardware::automotive::remoteaccess {

bool BindToDeviceSocketMutator::mutateFd(int fd) {
    int ret = setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, mIfname.c_str(), mIfname.size());
    if (ret != 0) {
        PLOG(ERROR) << "Can't bind socket to interface " << mIfname;
        return false;
    }
    return true;
}

bool bind_to_device_mutator_mutate_fd(int fd, grpc_socket_mutator* mutator) {
    BindToDeviceSocketMutator* bsm = (BindToDeviceSocketMutator*)mutator;
    return bsm->mutateFd(fd);
}

int bind_to_device_mutator_compare(grpc_socket_mutator* a, grpc_socket_mutator* b) {
    return ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0));
}

void bind_to_device_mutator_destroy(grpc_socket_mutator* mutator) {
    BindToDeviceSocketMutator* bsm = (BindToDeviceSocketMutator*)mutator;
    delete bsm;
}

grpc_socket_mutator_vtable bind_to_device_mutator_vtable = {bind_to_device_mutator_mutate_fd,
                                                            bind_to_device_mutator_compare,
                                                            bind_to_device_mutator_destroy};

BindToDeviceSocketMutator::BindToDeviceSocketMutator(const std::string_view& interface_name) {
    mIfname = interface_name;
    grpc_socket_mutator_init(this, &bind_to_device_mutator_vtable);
}

}  // namespace android::hardware::automotive::remoteaccess
+10 −4
Original line number Diff line number Diff line
@@ -22,22 +22,27 @@ cc_defaults {
    name: "remote-access-hal-defaults",
    vendor: true,
    relative_install_path: "hw",
    srcs: ["src/RemoteAccessImpl.cpp"],
    srcs: [
        "src/RemoteAccessImpl.cpp",
    ],
    whole_static_libs: [
        "RemoteAccessService",
    ],
    static_libs: [
        "BindToDeviceSocketMutatorLib",
    ],
    shared_libs: [
        "libbase",
        "libbinder_ndk",
        "liblog",
        "libutils",
        "libgrpc++",
        "libprotobuf-cpp-full",
    ],
    defaults: [
        "vhalclient_defaults",
        "BindToDeviceSocketMutatorDefaults",
    ],
    cflags: [
        // This is already included in BindToDeviceSocketMutatorDefaults but
        // might be overridden by vhalclient_defaults.
        "-Wno-unused-parameter",
    ],
}
@@ -59,6 +64,7 @@ cc_binary {
    init_rc: ["remoteaccess-tcu-test-service.rc"],
    cflags: [
        "-DGRPC_SERVICE_ADDRESS=\"10.10.10.1:50051\"",
        "-DGRPC_SERVICE_IFNAME=\"eth1\"",
    ],
}

+25 −10
Original line number Diff line number Diff line
@@ -18,45 +18,60 @@

#include "RemoteAccessService.h"

#include "BindToDeviceSocketMutator.h"

#include <android-base/logging.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>
#include <grpcpp/create_channel.h>
#include <libnetdevice/libnetdevice.h>
#include <stdlib.h>
#include <utils/Log.h>

constexpr char SERVICE_NAME[] = "android.hardware.automotive.remoteaccess.IRemoteAccess/default";

int main(int /* argc */, char* /* argv */[]) {
    ALOGI("Registering RemoteAccessService as service...");
    LOG(INFO) << "Registering RemoteAccessService as service...";

#ifndef GRPC_SERVICE_ADDRESS
    ALOGE("GRPC_SERVICE_ADDRESS is not defined, exiting");
    LOG(ERROR) << "GRPC_SERVICE_ADDRESS is not defined, exiting";
    exit(1);
#endif
    auto channel = grpc::CreateChannel(GRPC_SERVICE_ADDRESS, grpc::InsecureChannelCredentials());
    grpc::ChannelArguments grpcargs = {};

#ifdef GRPC_SERVICE_IFNAME
    grpcargs.SetSocketMutator(
            new android::hardware::automotive::remoteaccess::BindToDeviceSocketMutator(
                    GRPC_SERVICE_IFNAME));
    LOG(DEBUG) << "GRPC_SERVICE_IFNAME specified as: " << GRPC_SERVICE_IFNAME;
    LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME;
    android::netdevice::waitFor({GRPC_SERVICE_IFNAME},
                                android::netdevice::WaitCondition::PRESENT_AND_UP);
    LOG(INFO) << "Waiting for interface: " << GRPC_SERVICE_IFNAME << " done";
#endif
    auto channel = grpc::CreateCustomChannel(GRPC_SERVICE_ADDRESS,
                                             grpc::InsecureChannelCredentials(), grpcargs);
    auto clientStub = android::hardware::automotive::remoteaccess::WakeupClient::NewStub(channel);
    auto service = ndk::SharedRefBase::make<
            android::hardware::automotive::remoteaccess::RemoteAccessService>(clientStub.get());

    binder_exception_t err = AServiceManager_addService(service->asBinder().get(), SERVICE_NAME);
    if (err != EX_NONE) {
        ALOGE("failed to register android.hardware.automotive.remote.IRemoteAccess service, "
              "exception: %d",
              err);
        LOG(ERROR) << "failed to register android.hardware.automotive.remote.IRemoteAccess service"
                   << ", exception: " << err;
        exit(1);
    }

    if (!ABinderProcess_setThreadPoolMaxThreadCount(1)) {
        ALOGE("%s", "failed to set thread pool max thread count");
        LOG(ERROR) << "failed to set thread pool max thread count";
        exit(1);
    }
    ABinderProcess_startThreadPool();

    ALOGI("RemoteAccess service Ready");
    LOG(INFO) << "RemoteAccess service Ready";

    ABinderProcess_joinThreadPool();

    ALOGW("Should not reach here");
    LOG(ERROR) << "Should not reach here";

    return 0;
}
Loading