blob: fc4c477ae8096484f3d4dc186c0ef3ecc6c6b56a [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
George Burgess IVf2493a32021-07-07 09:59:32 -070024std::string ReplayUtils::getGnssPath() {
Yuchen He14a30182021-06-10 17:10:15 -070025 char devname_value[PROPERTY_VALUE_MAX] = "";
26 if (property_get("debug.location.gnss.devname", devname_value, NULL) > 0) {
George Burgess IVf2493a32021-07-07 09:59:32 -070027 return devname_value;
Yuchen He14a30182021-06-10 17:10:15 -070028 }
George Burgess IVf2493a32021-07-07 09:59:32 -070029 return GNSS_PATH;
Yuchen He14a30182021-06-10 17:10:15 -070030}
31
32bool ReplayUtils::hasGnssDeviceFile() {
33 struct stat sb;
George Burgess IVf2493a32021-07-07 09:59:32 -070034 return stat(getGnssPath().c_str(), &sb) != -1;
Yuchen He14a30182021-06-10 17:10:15 -070035}
36
37bool ReplayUtils::isGnssRawMeasurement(const std::string& inputStr) {
38 // TODO: add more logic check to by pass invalid data.
39 return !inputStr.empty() && (inputStr.find("Raw") != std::string::npos);
40}
41
42bool ReplayUtils::isNMEA(const std::string& inputStr) {
43 return !inputStr.empty() &&
44 (inputStr.rfind("$GPRMC,", 0) == 0 || inputStr.rfind("$GPRMA,", 0) == 0);
45}
46
47std::string ReplayUtils::getDataFromDeviceFile(const std::string& command, int mMinIntervalMs) {
48 char inputBuffer[INPUT_BUFFER_SIZE];
George Burgess IVf2493a32021-07-07 09:59:32 -070049 int mGnssFd = open(getGnssPath().c_str(), O_RDWR | O_NONBLOCK);
Yuchen He14a30182021-06-10 17:10:15 -070050
51 if (mGnssFd == -1) {
52 return "";
53 }
54
55 int bytes_write = write(mGnssFd, command.c_str(), command.size());
56 if (bytes_write <= 0) {
57 return "";
58 }
59
60 struct epoll_event ev, events[1];
61 ev.data.fd = mGnssFd;
62 ev.events = EPOLLIN;
63 int epoll_fd = epoll_create1(0);
64 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, mGnssFd, &ev);
65 int bytes_read = -1;
66 std::string inputStr = "";
67 int epoll_ret = epoll_wait(epoll_fd, events, 1, mMinIntervalMs);
68
69 if (epoll_ret == -1) {
70 return "";
71 }
72 while (true) {
73 memset(inputBuffer, 0, INPUT_BUFFER_SIZE);
74 bytes_read = read(mGnssFd, &inputBuffer, INPUT_BUFFER_SIZE);
75 if (bytes_read <= 0) {
76 break;
77 }
78 inputStr += std::string(inputBuffer, bytes_read);
79 }
80
81 return inputStr;
82}
83
84} // namespace common
85} // namespace gnss
86} // namespace hardware
87} // namespace android