Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 19 | namespace android { |
| 20 | namespace hardware { |
| 21 | namespace gnss { |
| 22 | namespace common { |
| 23 | |
George Burgess IV | f2493a3 | 2021-07-07 09:59:32 -0700 | [diff] [blame^] | 24 | std::string ReplayUtils::getGnssPath() { |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 25 | char devname_value[PROPERTY_VALUE_MAX] = ""; |
| 26 | if (property_get("debug.location.gnss.devname", devname_value, NULL) > 0) { |
George Burgess IV | f2493a3 | 2021-07-07 09:59:32 -0700 | [diff] [blame^] | 27 | return devname_value; |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 28 | } |
George Burgess IV | f2493a3 | 2021-07-07 09:59:32 -0700 | [diff] [blame^] | 29 | return GNSS_PATH; |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | bool ReplayUtils::hasGnssDeviceFile() { |
| 33 | struct stat sb; |
George Burgess IV | f2493a3 | 2021-07-07 09:59:32 -0700 | [diff] [blame^] | 34 | return stat(getGnssPath().c_str(), &sb) != -1; |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | bool 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 | |
| 42 | bool ReplayUtils::isNMEA(const std::string& inputStr) { |
| 43 | return !inputStr.empty() && |
| 44 | (inputStr.rfind("$GPRMC,", 0) == 0 || inputStr.rfind("$GPRMA,", 0) == 0); |
| 45 | } |
| 46 | |
| 47 | std::string ReplayUtils::getDataFromDeviceFile(const std::string& command, int mMinIntervalMs) { |
| 48 | char inputBuffer[INPUT_BUFFER_SIZE]; |
George Burgess IV | f2493a3 | 2021-07-07 09:59:32 -0700 | [diff] [blame^] | 49 | int mGnssFd = open(getGnssPath().c_str(), O_RDWR | O_NONBLOCK); |
Yuchen He | 14a3018 | 2021-06-10 17:10:15 -0700 | [diff] [blame] | 50 | |
| 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 |