blob: 6dcf6ea252d73d56e62c383a0f0dfd1f8f796754 [file] [log] [blame]
Yuchen He14a30182021-06-10 17:10:15 -07001/*
2 * Copyright (C) 2021 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 "GnssReplayUtils.h"
18
19namespace android {
20namespace hardware {
21namespace gnss {
22namespace common {
23
24const char* ReplayUtils::getGnssPath() {
25 const char* gnss_dev_path = GNSS_PATH;
26 char devname_value[PROPERTY_VALUE_MAX] = "";
27 if (property_get("debug.location.gnss.devname", devname_value, NULL) > 0) {
28 gnss_dev_path = devname_value;
29 }
30 return gnss_dev_path;
31}
32
33bool ReplayUtils::hasGnssDeviceFile() {
34 struct stat sb;
35 return stat(getGnssPath(), &sb) != -1;
36}
37
38bool ReplayUtils::isGnssRawMeasurement(const std::string& inputStr) {
39 // TODO: add more logic check to by pass invalid data.
40 return !inputStr.empty() && (inputStr.find("Raw") != std::string::npos);
41}
42
43bool ReplayUtils::isNMEA(const std::string& inputStr) {
44 return !inputStr.empty() &&
45 (inputStr.rfind("$GPRMC,", 0) == 0 || inputStr.rfind("$GPRMA,", 0) == 0);
46}
47
48std::string ReplayUtils::getDataFromDeviceFile(const std::string& command, int mMinIntervalMs) {
49 char inputBuffer[INPUT_BUFFER_SIZE];
50 int mGnssFd = open(getGnssPath(), O_RDWR | O_NONBLOCK);
51
52 if (mGnssFd == -1) {
53 return "";
54 }
55
56 int bytes_write = write(mGnssFd, command.c_str(), command.size());
57 if (bytes_write <= 0) {
58 return "";
59 }
60
61 struct epoll_event ev, events[1];
62 ev.data.fd = mGnssFd;
63 ev.events = EPOLLIN;
64 int epoll_fd = epoll_create1(0);
65 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, mGnssFd, &ev);
66 int bytes_read = -1;
67 std::string inputStr = "";
68 int epoll_ret = epoll_wait(epoll_fd, events, 1, mMinIntervalMs);
69
70 if (epoll_ret == -1) {
71 return "";
72 }
73 while (true) {
74 memset(inputBuffer, 0, INPUT_BUFFER_SIZE);
75 bytes_read = read(mGnssFd, &inputBuffer, INPUT_BUFFER_SIZE);
76 if (bytes_read <= 0) {
77 break;
78 }
79 inputStr += std::string(inputBuffer, bytes_read);
80 }
81
82 return inputStr;
83}
84
85} // namespace common
86} // namespace gnss
87} // namespace hardware
88} // namespace android