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

Commit f1a86905 authored by Yu Shan's avatar Yu Shan
Browse files

Add subscribe to VHAL proto.

The latest IVehicleHardware adds Subscribe/Unsubscribe function
to replace the existing updateSampleRate. This CL adds the
Subscribe function to protobuf definition and implements it.

Test: atest GRPCVehicleHardwareUnitTest GRPCVehicleProxyServerUnitTest
Flag: EXEMPT hal change
Bug: 328316981
Change-Id: I4f02885b77f21a215a8b282be583f76118e400ba
parent 9d351388
Loading
Loading
Loading
Loading
+5 −0
Original line number Original line Diff line number Diff line
@@ -55,5 +55,10 @@
    {
    {
      "name": "VehicleHalProtoMessageConverterTest"
      "name": "VehicleHalProtoMessageConverterTest"
    }
    }
  ],
  "postsubmit": [
    {
      "name": "VehicleHalProtoMessageConverterTest"
    }
  ]
  ]
}
}
+2 −1
Original line number Original line Diff line number Diff line
@@ -22,7 +22,7 @@ genrule {
        "aprotoc",
        "aprotoc",
        "protoc-gen-grpc-cpp-plugin",
        "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: [
    srcs: [
        "proto/VehicleServer.proto",
        "proto/VehicleServer.proto",
        ":libprotobuf-internal-protos",
        ":libprotobuf-internal-protos",
@@ -31,6 +31,7 @@ genrule {
    out: [
    out: [
        "VehicleServer.pb.h",
        "VehicleServer.pb.h",
        "VehicleServer.grpc.pb.h",
        "VehicleServer.grpc.pb.h",
        "VehicleServer_mock.grpc.pb.h",
    ],
    ],
    visibility: ["//visibility:private"],
    visibility: ["//visibility:private"],
}
}
+25 −0
Original line number Original line Diff line number Diff line
@@ -39,6 +39,13 @@ GRPCVehicleHardware::GRPCVehicleHardware(std::string service_addr)
      mGrpcStub(proto::VehicleServer::NewStub(mGrpcChannel)),
      mGrpcStub(proto::VehicleServer::NewStub(mGrpcChannel)),
      mValuePollingThread([this] { ValuePollingLoop(); }) {}
      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() {
GRPCVehicleHardware::~GRPCVehicleHardware() {
    {
    {
        std::lock_guard lck(mShutdownMutex);
        std::lock_guard lck(mShutdownMutex);
@@ -181,6 +188,24 @@ aidlvhal::StatusCode GRPCVehicleHardware::checkHealth() {
    return static_cast<aidlvhal::StatusCode>(protoStatus.status_code());
    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,
aidlvhal::StatusCode GRPCVehicleHardware::updateSampleRate(int32_t propId, int32_t areaId,
                                                           float sampleRate) {
                                                           float sampleRate) {
    ::grpc::ClientContext context;
    ::grpc::ClientContext context;
+6 −1
Original line number Original line Diff line number Diff line
@@ -43,6 +43,9 @@ class GRPCVehicleHardware : public IVehicleHardware {
  public:
  public:
    explicit GRPCVehicleHardware(std::string service_addr);
    explicit GRPCVehicleHardware(std::string service_addr);


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

    ~GRPCVehicleHardware();
    ~GRPCVehicleHardware();


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


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

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


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


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


    std::unique_ptr<const PropertySetErrorCallback> mOnSetErr;
    std::unique_ptr<const PropertySetErrorCallback> mOnSetErr;
+11 −0
Original line number Original line Diff line number Diff line
@@ -164,6 +164,17 @@ GrpcVehicleProxyServer::GrpcVehicleProxyServer(std::string serverAddr,
    return ::grpc::Status::OK;
    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,
::grpc::Status GrpcVehicleProxyServer::CheckHealth(::grpc::ServerContext* context,
                                                   const ::google::protobuf::Empty*,
                                                   const ::google::protobuf::Empty*,
                                                   proto::VehicleHalCallStatus* status) {
                                                   proto::VehicleHalCallStatus* status) {
Loading