1 //
2 // Copyright (C) 2019 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "host/commands/run_cvd/launch/launch.h"
17 
18 #include <string>
19 #include <vector>
20 
21 #include <fruit/fruit.h>
22 
23 #include "common/libs/fs/shared_fd.h"
24 #include "common/libs/utils/in_sandbox.h"
25 #include "common/libs/utils/result.h"
26 #include "host/libs/config/command_source.h"
27 #include "host/libs/config/known_paths.h"
28 
29 namespace cuttlefish {
30 
GnssGrpcProxyServer(const CuttlefishConfig::InstanceSpecific & instance,GrpcSocketCreator & grpc_socket)31 Result<std::optional<MonitorCommand>> GnssGrpcProxyServer(
32     const CuttlefishConfig::InstanceSpecific& instance,
33     GrpcSocketCreator& grpc_socket) {
34   if (!instance.enable_gnss_grpc_proxy()) {
35     return {};
36   }
37   std::vector<SharedFD> fifos;
38   std::vector<std::string> fifo_paths = {
39       instance.PerInstanceInternalPath("gnsshvc_fifo_vm.in"),
40       instance.PerInstanceInternalPath("gnsshvc_fifo_vm.out"),
41       instance.PerInstanceInternalPath("locationhvc_fifo_vm.in"),
42       instance.PerInstanceInternalPath("locationhvc_fifo_vm.out"),
43   };
44   for (const auto& path : fifo_paths) {
45     fifos.emplace_back(CF_EXPECT(SharedFD::Fifo(path, 0660)));
46   }
47 
48   auto gnss_grpc_proxy_cmd =
49       Command(GnssGrpcProxyBinary())
50           .AddParameter("--gnss_in_fd=", fifos[0])
51           .AddParameter("--gnss_out_fd=", fifos[1])
52           .AddParameter("--fixed_location_in_fd=", fifos[2])
53           .AddParameter("--fixed_location_out_fd=", fifos[3])
54           .AddParameter("--gnss_grpc_socket=",
55                         grpc_socket.CreateGrpcSocket("GnssGrpcProxyServer"));
56   if (!InSandbox()) {
57     gnss_grpc_proxy_cmd.AddParameter("--gnss_grpc_port=",
58                                      instance.gnss_grpc_proxy_server_port());
59   }
60   if (!instance.gnss_file_path().empty()) {
61     // If path is provided, proxy will start as local mode.
62     gnss_grpc_proxy_cmd.AddParameter("--gnss_file_path=",
63                                      instance.gnss_file_path());
64   }
65   if (!instance.fixed_location_file_path().empty()) {
66     gnss_grpc_proxy_cmd.AddParameter("--fixed_location_file_path=",
67                                      instance.fixed_location_file_path());
68   }
69   return gnss_grpc_proxy_cmd;
70 }
71 
72 }  // namespace cuttlefish
73