• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 #define LOG_TAG "GnssMeasIfaceAidl"
18 
19 #include "GnssMeasurementInterface.h"
20 #include <aidl/android/hardware/gnss/BnGnss.h>
21 #include <log/log.h>
22 #include "DeviceFileReader.h"
23 #include "GnssRawMeasurementParser.h"
24 #include "GnssReplayUtils.h"
25 #include "Utils.h"
26 
27 namespace aidl::android::hardware::gnss {
28 
29 using Utils = ::android::hardware::gnss::common::Utils;
30 using ReplayUtils = ::android::hardware::gnss::common::ReplayUtils;
31 using GnssRawMeasurementParser = ::android::hardware::gnss::common::GnssRawMeasurementParser;
32 using DeviceFileReader = ::android::hardware::gnss::common::DeviceFileReader;
33 
34 std::shared_ptr<IGnssMeasurementCallback> GnssMeasurementInterface::sCallback = nullptr;
35 
GnssMeasurementInterface()36 GnssMeasurementInterface::GnssMeasurementInterface()
37     : mIntervalMs(1000), mLocationIntervalMs(1000), mFutures(std::vector<std::future<void>>()) {}
38 
~GnssMeasurementInterface()39 GnssMeasurementInterface::~GnssMeasurementInterface() {
40     waitForStoppingThreads();
41 }
42 
setCallback(const std::shared_ptr<IGnssMeasurementCallback> & callback,const bool enableFullTracking,const bool enableCorrVecOutputs)43 ndk::ScopedAStatus GnssMeasurementInterface::setCallback(
44         const std::shared_ptr<IGnssMeasurementCallback>& callback, const bool enableFullTracking,
45         const bool enableCorrVecOutputs) {
46     ALOGD("setCallback: enableFullTracking: %d enableCorrVecOutputs: %d", (int)enableFullTracking,
47           (int)enableCorrVecOutputs);
48     {
49         std::unique_lock<std::mutex> lock(mMutex);
50         sCallback = callback;
51     }
52 
53     if (mIsActive) {
54         ALOGW("GnssMeasurement callback already set. Resetting the callback...");
55         stop();
56     }
57     start(enableCorrVecOutputs);
58 
59     return ndk::ScopedAStatus::ok();
60 }
61 
setCallbackWithOptions(const std::shared_ptr<IGnssMeasurementCallback> & callback,const Options & options)62 ndk::ScopedAStatus GnssMeasurementInterface::setCallbackWithOptions(
63         const std::shared_ptr<IGnssMeasurementCallback>& callback, const Options& options) {
64     ALOGD("setCallbackWithOptions: fullTracking:%d, corrVec:%d, intervalMs:%d",
65           (int)options.enableFullTracking, (int)options.enableCorrVecOutputs, options.intervalMs);
66     {
67         std::unique_lock<std::mutex> lock(mMutex);
68         sCallback = callback;
69     }
70 
71     if (mIsActive) {
72         ALOGW("GnssMeasurement callback already set. Resetting the callback...");
73         stop();
74     }
75     mIntervalMs = std::max(options.intervalMs, 1000);
76     start(options.enableCorrVecOutputs);
77 
78     return ndk::ScopedAStatus::ok();
79 }
80 
close()81 ndk::ScopedAStatus GnssMeasurementInterface::close() {
82     ALOGD("close");
83     if (mIsActive) {
84         stop();
85     }
86     {
87         std::unique_lock<std::mutex> lock(mMutex);
88         sCallback = nullptr;
89     }
90     mIntervalMs = 1000;
91     return ndk::ScopedAStatus::ok();
92 }
93 
start(const bool enableCorrVecOutputs)94 void GnssMeasurementInterface::start(const bool enableCorrVecOutputs) {
95     ALOGD("start");
96 
97     if (mIsActive) {
98         ALOGD("restarting since measurement has started");
99         stop();
100     }
101     // Wait for stopping previous thread.
102     waitForStoppingThreads();
103 
104     mIsActive = true;
105     mThreadBlocker.reset();
106     mThread = std::thread([this, enableCorrVecOutputs]() {
107         int intervalMs;
108         do {
109             if (!mIsActive) {
110                 break;
111             }
112             std::string rawMeasurementStr = "";
113             if (ReplayUtils::hasGnssDeviceFile() &&
114                 ReplayUtils::isGnssRawMeasurement(
115                         rawMeasurementStr =
116                                 DeviceFileReader::Instance().getGnssRawMeasurementData())) {
117                 ALOGD("rawMeasurementStr(size: %zu) from device file: %s", rawMeasurementStr.size(),
118                       rawMeasurementStr.c_str());
119                 auto measurement =
120                         GnssRawMeasurementParser::getMeasurementFromStrs(rawMeasurementStr);
121                 if (measurement != nullptr) {
122                     this->reportMeasurement(*measurement);
123                 }
124             } else {
125                 auto measurement = Utils::getMockMeasurement(enableCorrVecOutputs);
126                 this->reportMeasurement(measurement);
127             }
128             intervalMs =
129                     (mLocationEnabled) ? std::min(mLocationIntervalMs, mIntervalMs) : mIntervalMs;
130         } while (mIsActive && mThreadBlocker.wait_for(std::chrono::milliseconds(intervalMs)));
131     });
132 }
133 
stop()134 void GnssMeasurementInterface::stop() {
135     ALOGD("stop");
136     mIsActive = false;
137     mThreadBlocker.notify();
138     if (mThread.joinable()) {
139         mFutures.push_back(std::async(std::launch::async, [this] { mThread.join(); }));
140     }
141 }
142 
reportMeasurement(const GnssData & data)143 void GnssMeasurementInterface::reportMeasurement(const GnssData& data) {
144     ALOGD("reportMeasurement()");
145     std::shared_ptr<IGnssMeasurementCallback> callbackCopy;
146     {
147         std::unique_lock<std::mutex> lock(mMutex);
148         if (sCallback == nullptr) {
149             ALOGE("%s: GnssMeasurement::sCallback is null.", __func__);
150             return;
151         }
152         callbackCopy = sCallback;
153     }
154     callbackCopy->gnssMeasurementCb(data);
155 }
156 
setLocationInterval(const int intervalMs)157 void GnssMeasurementInterface::setLocationInterval(const int intervalMs) {
158     mLocationIntervalMs = intervalMs;
159 }
160 
setLocationEnabled(const bool enabled)161 void GnssMeasurementInterface::setLocationEnabled(const bool enabled) {
162     mLocationEnabled = enabled;
163 }
164 
waitForStoppingThreads()165 void GnssMeasurementInterface::waitForStoppingThreads() {
166     for (auto& future : mFutures) {
167         ALOGD("Stopping previous thread.");
168         future.wait();
169         ALOGD("Done stopping thread.");
170     }
171     mFutures.clear();
172 }
173 
174 }  // namespace aidl::android::hardware::gnss
175