1 /*
2  * Copyright (C) 2022 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 
17 #include "host/libs/location/GnssClient.h"
18 
19 #include <memory>
20 
21 #include <android-base/logging.h>
22 #include <grpcpp/channel.h>
23 #include <grpcpp/support/status.h>
24 
25 #include "common/libs/utils/result.h"
26 
27 using gnss_grpc_proxy::GnssGrpcProxy;
28 using gnss_grpc_proxy::GpsCoordinates;
29 using gnss_grpc_proxy::SendGpsCoordinatesReply;
30 using gnss_grpc_proxy::SendGpsCoordinatesRequest;
31 using grpc::ClientContext;
32 
33 namespace cuttlefish {
34 
GnssClient(const std::shared_ptr<grpc::Channel> & channel)35 GnssClient::GnssClient(const std::shared_ptr<grpc::Channel>& channel)
36     : stub_(GnssGrpcProxy::NewStub(channel)) {}
37 
SendGpsLocations(int delay,const GpsFixArray & coordinates)38 Result<void> GnssClient::SendGpsLocations(int delay,
39                                           const GpsFixArray& coordinates) {
40   // Data we are sending to the server.
41   SendGpsCoordinatesRequest request;
42   request.set_delay(delay);
43   for (const auto& loc : coordinates) {
44     GpsCoordinates* curr = request.add_coordinates();
45     curr->set_longitude(loc.longitude);
46     curr->set_latitude(loc.latitude);
47     curr->set_elevation(loc.elevation);
48   }
49 
50   // Container for the data we expect from the server.
51   SendGpsCoordinatesReply reply;
52   // Context for the client. It could be used to convey extra information to
53   // the server and/or tweak certain RPC behaviors.
54   ClientContext context;
55   // The actual RPC.
56   grpc::Status status = stub_->SendGpsVector(&context, request, &reply);
57   // Act upon its status.
58   CF_EXPECTF(status.ok(), "GPS data sending failed: {} ({})",
59              status.error_message(),
60              static_cast<std::uint32_t>(status.error_code()));
61 
62   LOG(DEBUG) << reply.status();
63 
64   return {};
65 }
66 
67 }  // namespace cuttlefish
68