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

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

Merge "Add subscribe to VHAL proto." into main

parents 8a844ae8 f1a86905
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -55,5 +55,10 @@
    {
      "name": "VehicleHalProtoMessageConverterTest"
    }
  ],
  "postsubmit": [
    {
      "name": "VehicleHalProtoMessageConverterTest"
    }
  ]
}
+2 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ genrule {
        "aprotoc",
        "protoc-gen-grpc-cpp-plugin",
    ],
    cmd: "$(location aprotoc) -I$$(dirname $(location proto/VehicleServer.proto)) -Ihardware/interfaces/automotive/vehicle/aidl/impl/proto -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(location proto/VehicleServer.proto) --grpc_out=$(genDir) --cpp_out=$(genDir)",
    cmd: "$(location aprotoc) -I$$(dirname $(location proto/VehicleServer.proto)) -Ihardware/interfaces/automotive/vehicle/aidl/impl/proto -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(location proto/VehicleServer.proto) --grpc_opt=generate_mock_code=true --grpc_out=$(genDir) --cpp_out=$(genDir)",
    srcs: [
        "proto/VehicleServer.proto",
        ":libprotobuf-internal-protos",
@@ -31,6 +31,7 @@ genrule {
    out: [
        "VehicleServer.pb.h",
        "VehicleServer.grpc.pb.h",
        "VehicleServer_mock.grpc.pb.h",
    ],
    visibility: ["//visibility:private"],
}
+25 −0
Original line number Diff line number Diff line
@@ -39,6 +39,13 @@ GRPCVehicleHardware::GRPCVehicleHardware(std::string service_addr)
      mGrpcStub(proto::VehicleServer::NewStub(mGrpcChannel)),
      mValuePollingThread([this] { ValuePollingLoop(); }) {}

// Only used for unit testing.
GRPCVehicleHardware::GRPCVehicleHardware(std::unique_ptr<proto::VehicleServer::StubInterface> stub)
    : mServiceAddr(""),
      mGrpcChannel(nullptr),
      mGrpcStub(std::move(stub)),
      mValuePollingThread([] {}) {}

GRPCVehicleHardware::~GRPCVehicleHardware() {
    {
        std::lock_guard lck(mShutdownMutex);
@@ -181,6 +188,24 @@ aidlvhal::StatusCode GRPCVehicleHardware::checkHealth() {
    return static_cast<aidlvhal::StatusCode>(protoStatus.status_code());
}

aidlvhal::StatusCode GRPCVehicleHardware::subscribe(aidlvhal::SubscribeOptions options) {
    proto::SubscribeRequest request;
    ::grpc::ClientContext context;
    proto::VehicleHalCallStatus protoStatus;
    proto_msg_converter::aidlToProto(options, request.mutable_options());
    auto grpc_status = mGrpcStub->Subscribe(&context, request, &protoStatus);
    if (!grpc_status.ok()) {
        if (grpc_status.error_code() == ::grpc::StatusCode::UNIMPLEMENTED) {
            // This is a legacy sever. It should handle updateSampleRate.
            LOG(INFO) << __func__ << ": GRPC Subscribe is not supported by the server";
            return aidlvhal::StatusCode::OK;
        }
        LOG(ERROR) << __func__ << ": GRPC Subscribe Failed: " << grpc_status.error_message();
        return aidlvhal::StatusCode::INTERNAL_ERROR;
    }
    return static_cast<aidlvhal::StatusCode>(protoStatus.status_code());
}

aidlvhal::StatusCode GRPCVehicleHardware::updateSampleRate(int32_t propId, int32_t areaId,
                                                           float sampleRate) {
    ::grpc::ClientContext context;
+6 −1
Original line number Diff line number Diff line
@@ -43,6 +43,9 @@ class GRPCVehicleHardware : public IVehicleHardware {
  public:
    explicit GRPCVehicleHardware(std::string service_addr);

    // Only used for unit testing.
    explicit GRPCVehicleHardware(std::unique_ptr<proto::VehicleServer::StubInterface> stub);

    ~GRPCVehicleHardware();

    // Get all the property configs.
@@ -80,6 +83,8 @@ class GRPCVehicleHardware : public IVehicleHardware {
    aidlvhal::StatusCode updateSampleRate(int32_t propId, int32_t areaId,
                                          float sampleRate) override;

    aidlvhal::StatusCode subscribe(aidlvhal::SubscribeOptions options) override;

    bool waitForConnected(std::chrono::milliseconds waitTime);

  protected:
@@ -91,7 +96,7 @@ class GRPCVehicleHardware : public IVehicleHardware {

    std::string mServiceAddr;
    std::shared_ptr<::grpc::Channel> mGrpcChannel;
    std::unique_ptr<proto::VehicleServer::Stub> mGrpcStub;
    std::unique_ptr<proto::VehicleServer::StubInterface> mGrpcStub;
    std::thread mValuePollingThread;

    std::unique_ptr<const PropertySetErrorCallback> mOnSetErr;
+11 −0
Original line number Diff line number Diff line
@@ -164,6 +164,17 @@ GrpcVehicleProxyServer::GrpcVehicleProxyServer(std::string serverAddr,
    return ::grpc::Status::OK;
}

::grpc::Status GrpcVehicleProxyServer::Subscribe(::grpc::ServerContext* context,
                                                 const proto::SubscribeRequest* request,
                                                 proto::VehicleHalCallStatus* status) {
    const auto& protoSubscribeOptions = request->options();
    aidlvhal::SubscribeOptions aidlSubscribeOptions = {};
    proto_msg_converter::protoToAidl(protoSubscribeOptions, &aidlSubscribeOptions);
    const auto status_code = mHardware->subscribe(aidlSubscribeOptions);
    status->set_status_code(static_cast<proto::StatusCode>(status_code));
    return ::grpc::Status::OK;
}

::grpc::Status GrpcVehicleProxyServer::CheckHealth(::grpc::ServerContext* context,
                                                   const ::google::protobuf::Empty*,
                                                   proto::VehicleHalCallStatus* status) {
Loading