aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeungjae Yoo <seungjaeyoo@google.com>2023-03-24 16:44:07 +0900
committerSeungjae Yoo <seungjaeyoo@google.com>2023-04-17 15:37:24 +0900
commitbc21ed09e5f19133942a5812fb2adddda55bfce4 (patch)
treed8e022ccc9c73b5f1b321ec14b46c8879ca3affb
parentbc6f9b4a5dce5d40abe377d7f3254f22a4e95b4b (diff)
downloadwmediumd-bc21ed09e5f19133942a5812fb2adddda55bfce4.tar.gz
Add grpc service in wmediumd binary
Bug: 273384914 Test: cvd env cvd-1 call WmediumdService Empty "" Change-Id: I30a5bbac78f90b5cbda5701d19192d961e4a6bdc
-rw-r--r--Android.bp75
-rw-r--r--main.cc32
-rw-r--r--wmediumd_server/wmediumd.proto29
-rw-r--r--wmediumd_server/wmediumd_server.cc65
-rw-r--r--wmediumd_server/wmediumd_server.h19
5 files changed, 216 insertions, 4 deletions
diff --git a/Android.bp b/Android.bp
index aabc81a..c53fea0 100644
--- a/Android.bp
+++ b/Android.bp
@@ -29,10 +29,72 @@ cc_binary_host {
],
}
+cc_library_host_static {
+ name: "libwmediumd_server",
+ shared_libs: [
+ "libgflags",
+ "libgrpc++_unsecure",
+ "libprotobuf-cpp-full",
+ ],
+ cflags: [
+ "-Wno-unused-parameter",
+ ],
+ generated_headers: [
+ "WmediumdServerProto_h",
+ ],
+ generated_sources: [
+ "WmediumdServerProto_cc",
+ ],
+ export_generated_headers: [
+ "WmediumdServerProto_h",
+ ],
+}
+
+filegroup {
+ name: "WmediumdServerProto",
+ srcs: [
+ "wmediumd_server/wmediumd.proto",
+ ],
+}
+
+// TODO(278065934): Add a module type for grpc service
+genrule {
+ name: "WmediumdServerProto_h",
+ tools: [
+ "aprotoc",
+ "protoc-gen-grpc-cpp-plugin",
+ ],
+ cmd: "$(location aprotoc) -Iexternal/wmediumd/wmediumd_server -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(in) --grpc_out=$(genDir) --cpp_out=$(genDir)",
+ srcs: [
+ ":WmediumdServerProto",
+ ],
+ out: [
+ "wmediumd.grpc.pb.h",
+ "wmediumd.pb.h",
+ ],
+}
+
+genrule {
+ name: "WmediumdServerProto_cc",
+ tools: [
+ "aprotoc",
+ "protoc-gen-grpc-cpp-plugin",
+ ],
+ cmd: "$(location aprotoc) -Iexternal/wmediumd/wmediumd_server -Iexternal/protobuf/src --plugin=protoc-gen-grpc=$(location protoc-gen-grpc-cpp-plugin) $(in) --grpc_out=$(genDir) --cpp_out=$(genDir)",
+ srcs: [
+ ":WmediumdServerProto",
+ ],
+ out: [
+ "wmediumd.grpc.pb.cc",
+ "wmediumd.pb.cc",
+ ],
+}
+
cc_binary_host {
name: "wmediumd",
srcs: [
"main.cc",
+ "wmediumd_server/wmediumd_server.cc",
"wmediumd/*.c",
"wmediumd/lib/*.c",
],
@@ -50,15 +112,22 @@ cc_binary_host {
"-Wno-gnu-variable-sized-type-not-at-end",
"-Wno-unused-function",
],
- static_libs: [
+ shared_libs: [
+ "libbase",
"libnl",
+ "libgflags",
+ "libgrpc++_unsecure",
+ "libprotobuf-cpp-full",
+ ],
+ static_libs: [
"libconfig",
+ "libgrpc++_reflection",
+ "libwmediumd_server",
],
+ cpp_std: "c++17",
visibility: [
"//device/google/cuttlefish/build",
],
- stl: "none",
- static_executable: true,
}
cc_binary_host {
diff --git a/main.cc b/main.cc
index d07e575..25f8298 100644
--- a/main.cc
+++ b/main.cc
@@ -16,10 +16,40 @@
*
*/
+#include <string>
+#include <thread>
+#include <vector>
+
+#include <android-base/logging.h>
+#include <android-base/strings.h>
+
#include <wmediumd/wmediumd.h>
+#include <wmediumd_server/wmediumd_server.h>
+
+constexpr char kGrpcUdsPathOption[] = "--grpc_uds_path=";
int main(int argc, char* argv[]) {
- wmediumd_main(argc, argv);
+ std::vector<char*> wmediumd_args;
+ std::string grpc_uds_path;
+ for (int i = 0; i < argc; i++) {
+ if (android::base::StartsWith(argv[i], kGrpcUdsPathOption)) {
+ std::string current_arg(argv[i]);
+ grpc_uds_path = current_arg.substr(strlen(kGrpcUdsPathOption));
+ } else {
+ wmediumd_args.push_back(argv[i]);
+ }
+ }
+
+ std::thread wmediumd_server_thread;
+ if (!grpc_uds_path.empty()) {
+ wmediumd_server_thread = std::thread(RunWmediumdServer, grpc_uds_path);
+ }
+
+ wmediumd_main(wmediumd_args.size(), wmediumd_args.data());
+
+ if (!grpc_uds_path.empty()) {
+ wmediumd_server_thread.join();
+ }
return 0;
} \ No newline at end of file
diff --git a/wmediumd_server/wmediumd.proto b/wmediumd_server/wmediumd.proto
new file mode 100644
index 0000000..c25b8c8
--- /dev/null
+++ b/wmediumd_server/wmediumd.proto
@@ -0,0 +1,29 @@
+// 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.
+
+syntax = "proto3";
+
+package wmediumdserver;
+
+service WmediumdService {
+ rpc Wmediumd (WmediumdRequest) returns (WmediumdReply) {}
+}
+
+message WmediumdRequest {
+ string message = 1;
+}
+
+message WmediumdReply {
+ string message = 1;
+} \ No newline at end of file
diff --git a/wmediumd_server/wmediumd_server.cc b/wmediumd_server/wmediumd_server.cc
new file mode 100644
index 0000000..48388d5
--- /dev/null
+++ b/wmediumd_server/wmediumd_server.cc
@@ -0,0 +1,65 @@
+/*
+ *
+ * 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 <iostream>
+#include <memory>
+#include <string>
+
+#include <gflags/gflags.h>
+#include <grpcpp/ext/proto_server_reflection_plugin.h>
+#include <grpcpp/grpcpp.h>
+#include <grpcpp/health_check_service_interface.h>
+
+#include "wmediumd.grpc.pb.h"
+
+using wmediumdserver::WmediumdReply;
+using wmediumdserver::WmediumdRequest;
+using wmediumdserver::WmediumdService;
+using grpc::Server;
+using grpc::ServerBuilder;
+using grpc::ServerContext;
+using grpc::Status;
+
+class WmediumdServiceImpl final : public WmediumdService::Service {
+ Status Wmediumd(ServerContext* context, const WmediumdRequest* request,
+ WmediumdReply* reply) override {
+ reply->set_message(request->message());
+ return Status::OK;
+ }
+};
+
+void RunWmediumdServer(std::string grpc_uds_path) {
+ std::string server_address("unix:" + grpc_uds_path);
+ WmediumdServiceImpl service;
+
+ grpc::EnableDefaultHealthCheckService(true);
+ grpc::reflection::InitProtoReflectionServerBuilderPlugin();
+ ServerBuilder builder;
+ // Listen on the given address without any authentication mechanism.
+ builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
+ // Register "service" as the instance through which we'll communicate with
+ // clients. In this case it corresponds to an *synchronous* service.
+ builder.RegisterService(&service);
+ // Finally assemble the server.
+ std::unique_ptr<Server> server(builder.BuildAndStart());
+ std::cout << "Server listening on " << server_address << std::endl;
+
+ // Wait for the server to shutdown. Note that some other thread must be
+ // responsible for shutting down the server for this call to ever return.
+ server->Wait();
+}
diff --git a/wmediumd_server/wmediumd_server.h b/wmediumd_server/wmediumd_server.h
new file mode 100644
index 0000000..21e3c84
--- /dev/null
+++ b/wmediumd_server/wmediumd_server.h
@@ -0,0 +1,19 @@
+/*
+ *
+ * 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.
+ *
+ */
+
+void RunWmediumdServer(std::string grpc_uds_path); \ No newline at end of file