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/frontend/webrtc/gpx_locations_handler.h"
18 
19 #include <unistd.h>
20 
21 #include <iostream>
22 #include <string>
23 
24 #include <android-base/logging.h>
25 
26 #include "host/libs/config/cuttlefish_config.h"
27 #include "host/libs/location/GnssClient.h"
28 #include "host/libs/location/GpxParser.h"
29 
30 namespace cuttlefish::webrtc_streaming {
31 
GpxLocationsHandler(std::function<void (const uint8_t *,size_t)> send_to_client)32 GpxLocationsHandler::GpxLocationsHandler(
33     std::function<void(const uint8_t *, size_t)> send_to_client) {}
34 
~GpxLocationsHandler()35 GpxLocationsHandler::~GpxLocationsHandler() {}
36 
HandleMessage(const uint8_t * msg,size_t len)37 void GpxLocationsHandler::HandleMessage(const uint8_t *msg, size_t len) {
38   LOG(DEBUG) << "ENTER GpxLocationsHandler handleMessage , size: " << len;
39   std::string error;
40   GpsFixArray coordinates;
41   if (!GpxParser::parseString((const char *)&msg[0], len, &coordinates,
42                               &error)) {
43     LOG(ERROR) << " Parsing Error: " << error << std::endl;
44     return;
45   }
46 
47   LOG(DEBUG) << "Number of parsed points: " << coordinates.size() << std::endl;
48   auto config = CuttlefishConfig::Get();
49   if (!config) {
50     LOG(ERROR) << "Failed to obtain config object";
51     return;
52   }
53   auto instance = config->ForDefaultInstance();
54   std::string socket_name =
55       fmt::format("unix:{}.sock",
56                   instance.PerInstanceGrpcSocketPath("GnssGrpcProxyServer"));
57   GnssClient gpsclient(
58       grpc::CreateChannel(socket_name, grpc::InsecureChannelCredentials()));
59 
60   Result<void> reply = gpsclient.SendGpsLocations(1000, coordinates);
61   if (!reply.ok()) {
62     LOG(ERROR) << reply.error().FormatForEnv();
63   }
64 }
65 
66 }  // namespace cuttlefish::webrtc_streaming
67