blob: 2c7241b82e0aacddb40eda245f89a696888434e1 [file] [log] [blame]
Yu-Han Yang04832302020-11-20 09:51:18 -08001/*
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>
Yuchen He3cbf5f32021-08-30 22:20:30 +000022#include "DeviceFileReader.h"
23#include "GnssRawMeasurementParser.h"
Yuchen He14a30182021-06-10 17:10:15 -070024#include "GnssReplayUtils.h"
Yu-Han Yang04832302020-11-20 09:51:18 -080025#include "Utils.h"
26
27namespace aidl::android::hardware::gnss {
28
29using Utils = ::android::hardware::gnss::common::Utils;
Yuchen He14a30182021-06-10 17:10:15 -070030using ReplayUtils = ::android::hardware::gnss::common::ReplayUtils;
Yuchen He3cbf5f32021-08-30 22:20:30 +000031using GnssRawMeasurementParser = ::android::hardware::gnss::common::GnssRawMeasurementParser;
32using DeviceFileReader = ::android::hardware::gnss::common::DeviceFileReader;
Yu-Han Yang04832302020-11-20 09:51:18 -080033
34std::shared_ptr<IGnssMeasurementCallback> GnssMeasurementInterface::sCallback = nullptr;
35
36GnssMeasurementInterface::GnssMeasurementInterface() : mMinIntervalMillis(1000) {}
37
38GnssMeasurementInterface::~GnssMeasurementInterface() {
39 stop();
40}
41
42ndk::ScopedAStatus GnssMeasurementInterface::setCallback(
Shinru Han4abab502020-12-09 15:07:18 +080043 const std::shared_ptr<IGnssMeasurementCallback>& callback, const bool enableFullTracking,
44 const bool enableCorrVecOutputs) {
45 ALOGD("setCallback: enableFullTracking: %d enableCorrVecOutputs: %d", (int)enableFullTracking,
46 (int)enableCorrVecOutputs);
Yu-Han Yang04832302020-11-20 09:51:18 -080047 std::unique_lock<std::mutex> lock(mMutex);
48 sCallback = callback;
49
50 if (mIsActive) {
51 ALOGW("GnssMeasurement callback already set. Resetting the callback...");
52 stop();
53 }
Shinru Han4abab502020-12-09 15:07:18 +080054 start(enableCorrVecOutputs);
Yu-Han Yang04832302020-11-20 09:51:18 -080055
56 return ndk::ScopedAStatus::ok();
57}
58
Yu-Han Yang11c0c242021-12-15 11:22:09 -080059ndk::ScopedAStatus GnssMeasurementInterface::setCallbackWithOptions(
60 const std::shared_ptr<IGnssMeasurementCallback>& callback, const Options& options) {
61 ALOGD("setCallbackWithOptions: fullTracking:%d, corrVec:%d, intervalMs:%d",
62 (int)options.enableFullTracking, (int)options.enableCorrVecOutputs, options.intervalMs);
63 std::unique_lock<std::mutex> lock(mMutex);
64 sCallback = callback;
65
66 if (mIsActive) {
67 ALOGW("GnssMeasurement callback already set. Resetting the callback...");
68 stop();
69 }
70 mMinIntervalMillis = options.intervalMs;
71 start(options.enableCorrVecOutputs);
72
73 return ndk::ScopedAStatus::ok();
74}
75
Yu-Han Yang04832302020-11-20 09:51:18 -080076ndk::ScopedAStatus GnssMeasurementInterface::close() {
77 ALOGD("close");
78 stop();
79 std::unique_lock<std::mutex> lock(mMutex);
80 sCallback = nullptr;
Yu-Han Yang11c0c242021-12-15 11:22:09 -080081 mMinIntervalMillis = 1000;
Yu-Han Yang04832302020-11-20 09:51:18 -080082 return ndk::ScopedAStatus::ok();
83}
84
Shinru Han4abab502020-12-09 15:07:18 +080085void GnssMeasurementInterface::start(const bool enableCorrVecOutputs) {
Yu-Han Yang04832302020-11-20 09:51:18 -080086 ALOGD("start");
87 mIsActive = true;
Shinru Han4abab502020-12-09 15:07:18 +080088 mThread = std::thread([this, enableCorrVecOutputs]() {
Yu-Han Yang04832302020-11-20 09:51:18 -080089 while (mIsActive == true) {
Yuchen He14a30182021-06-10 17:10:15 -070090 std::string rawMeasurementStr = "";
91 if (ReplayUtils::hasGnssDeviceFile() &&
92 ReplayUtils::isGnssRawMeasurement(
Yuchen He3cbf5f32021-08-30 22:20:30 +000093 rawMeasurementStr =
94 DeviceFileReader::Instance().getGnssRawMeasurementData())) {
Yuchen He14a30182021-06-10 17:10:15 -070095 ALOGD("rawMeasurementStr(size: %zu) from device file: %s", rawMeasurementStr.size(),
96 rawMeasurementStr.c_str());
Yuchen He3cbf5f32021-08-30 22:20:30 +000097 auto measurement =
98 GnssRawMeasurementParser::getMeasurementFromStrs(rawMeasurementStr);
99 if (measurement != nullptr) {
100 this->reportMeasurement(*measurement);
101 }
Yuchen He14a30182021-06-10 17:10:15 -0700102 } else {
103 auto measurement = Utils::getMockMeasurement(enableCorrVecOutputs);
104 this->reportMeasurement(measurement);
105 }
Yu-Han Yang04832302020-11-20 09:51:18 -0800106 std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis));
107 }
108 });
Sam Dubey160e4a12022-04-22 13:35:08 +0000109 mThread.detach();
Yu-Han Yang04832302020-11-20 09:51:18 -0800110}
111
112void GnssMeasurementInterface::stop() {
113 ALOGD("stop");
114 mIsActive = false;
Yu-Han Yang04832302020-11-20 09:51:18 -0800115}
116
117void GnssMeasurementInterface::reportMeasurement(const GnssData& data) {
118 ALOGD("reportMeasurement()");
Yu-Han Yang266c36a2021-05-27 16:00:59 -0700119 std::shared_ptr<IGnssMeasurementCallback> callbackCopy;
120 {
121 std::unique_lock<std::mutex> lock(mMutex);
122 if (sCallback == nullptr) {
123 ALOGE("%s: GnssMeasurement::sCallback is null.", __func__);
124 return;
125 }
126 callbackCopy = sCallback;
Yu-Han Yang04832302020-11-20 09:51:18 -0800127 }
Yu-Han Yang266c36a2021-05-27 16:00:59 -0700128 callbackCopy->gnssMeasurementCb(data);
Yu-Han Yang04832302020-11-20 09:51:18 -0800129}
130
131} // namespace aidl::android::hardware::gnss