blob: dfc086a8b8ad0c425b87935c26f9b874f63c7f93 [file] [log] [blame]
Yuchen He4eb8cf82021-08-30 22:20:30 +00001/*
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#include "DeviceFileReader.h"
17
18namespace android {
19namespace hardware {
20namespace gnss {
21namespace common {
22
23void DeviceFileReader::getDataFromDeviceFile(const std::string& command, int mMinIntervalMs) {
24 char inputBuffer[INPUT_BUFFER_SIZE];
Yuchen Hef46bb4a2022-01-20 22:57:09 +000025 std::string deviceFilePath = "";
26 if (command == CMD_GET_LOCATION) {
27 deviceFilePath = ReplayUtils::getFixedLocationPath();
28 } else if (command == CMD_GET_RAWMEASUREMENT) {
29 deviceFilePath = ReplayUtils::getGnssPath();
30 } else {
31 // Invalid command
32 return;
33 }
34
35 int mGnssFd = open(deviceFilePath.c_str(), O_RDWR | O_NONBLOCK);
Yuchen He4eb8cf82021-08-30 22:20:30 +000036
37 if (mGnssFd == -1) {
38 return;
39 }
40
41 int bytes_write = write(mGnssFd, command.c_str(), command.size());
42 if (bytes_write <= 0) {
43 close(mGnssFd);
44 return;
45 }
46
47 struct epoll_event ev, events[1];
48 ev.data.fd = mGnssFd;
49 ev.events = EPOLLIN;
50 int epoll_fd = epoll_create1(0);
51 epoll_ctl(epoll_fd, EPOLL_CTL_ADD, mGnssFd, &ev);
52 int bytes_read = -1;
53 std::string inputStr = "";
54 int epoll_ret = epoll_wait(epoll_fd, events, 1, mMinIntervalMs);
55
56 if (epoll_ret == -1) {
57 close(mGnssFd);
58 return;
59 }
60 while (true) {
61 memset(inputBuffer, 0, INPUT_BUFFER_SIZE);
62 bytes_read = read(mGnssFd, &inputBuffer, INPUT_BUFFER_SIZE);
63 if (bytes_read <= 0) {
64 break;
65 }
66 s_buffer_ += std::string(inputBuffer, bytes_read);
67 }
68 close(mGnssFd);
69
70 // Trim end of file mark(\n\n\n\n).
71 auto pos = s_buffer_.find("\n\n\n\n");
72 if (pos != std::string::npos) {
73 inputStr = s_buffer_.substr(0, pos);
74 s_buffer_ = s_buffer_.substr(pos + 4);
75 } else {
76 return;
77 }
78
79 // Cache the injected data.
Yuchen Hef46bb4a2022-01-20 22:57:09 +000080 if (command == CMD_GET_LOCATION) {
81 // TODO validate data
Yuchen He4eb8cf82021-08-30 22:20:30 +000082 data_[CMD_GET_LOCATION] = inputStr;
Yuchen Hef46bb4a2022-01-20 22:57:09 +000083 } else if (command == CMD_GET_RAWMEASUREMENT) {
84 if (ReplayUtils::isGnssRawMeasurement(inputStr)) {
85 data_[CMD_GET_RAWMEASUREMENT] = inputStr;
86 }
Yuchen He4eb8cf82021-08-30 22:20:30 +000087 }
88}
89
90std::string DeviceFileReader::getLocationData() {
91 std::unique_lock<std::mutex> lock(mMutex);
92 getDataFromDeviceFile(CMD_GET_LOCATION, 20);
93 return data_[CMD_GET_LOCATION];
94}
95
96std::string DeviceFileReader::getGnssRawMeasurementData() {
97 std::unique_lock<std::mutex> lock(mMutex);
98 getDataFromDeviceFile(CMD_GET_RAWMEASUREMENT, 20);
99 return data_[CMD_GET_RAWMEASUREMENT];
100}
101
102DeviceFileReader::DeviceFileReader() {}
103
104DeviceFileReader::~DeviceFileReader() {}
105
106} // namespace common
107} // namespace gnss
108} // namespace hardware
109} // namespace android