blob: 9e4f7c7f2520ce8a60303dacfcdc52e812543c57 [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
59ndk::ScopedAStatus GnssMeasurementInterface::close() {
60 ALOGD("close");
61 stop();
62 std::unique_lock<std::mutex> lock(mMutex);
63 sCallback = nullptr;
64 return ndk::ScopedAStatus::ok();
65}
66
Shinru Han4abab502020-12-09 15:07:18 +080067void GnssMeasurementInterface::start(const bool enableCorrVecOutputs) {
Yu-Han Yang04832302020-11-20 09:51:18 -080068 ALOGD("start");
69 mIsActive = true;
Shinru Han4abab502020-12-09 15:07:18 +080070 mThread = std::thread([this, enableCorrVecOutputs]() {
Yu-Han Yang04832302020-11-20 09:51:18 -080071 while (mIsActive == true) {
Yuchen He14a30182021-06-10 17:10:15 -070072 std::string rawMeasurementStr = "";
73 if (ReplayUtils::hasGnssDeviceFile() &&
74 ReplayUtils::isGnssRawMeasurement(
Yuchen He3cbf5f32021-08-30 22:20:30 +000075 rawMeasurementStr =
76 DeviceFileReader::Instance().getGnssRawMeasurementData())) {
Yuchen He14a30182021-06-10 17:10:15 -070077 ALOGD("rawMeasurementStr(size: %zu) from device file: %s", rawMeasurementStr.size(),
78 rawMeasurementStr.c_str());
Yuchen He3cbf5f32021-08-30 22:20:30 +000079 auto measurement =
80 GnssRawMeasurementParser::getMeasurementFromStrs(rawMeasurementStr);
81 if (measurement != nullptr) {
82 this->reportMeasurement(*measurement);
83 }
Yuchen He14a30182021-06-10 17:10:15 -070084 } else {
85 auto measurement = Utils::getMockMeasurement(enableCorrVecOutputs);
86 this->reportMeasurement(measurement);
87 }
Yu-Han Yang04832302020-11-20 09:51:18 -080088 std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis));
89 }
90 });
Yu-Han Yang26c76352021-05-11 13:16:35 -070091 mThread.detach();
Yu-Han Yang04832302020-11-20 09:51:18 -080092}
93
94void GnssMeasurementInterface::stop() {
95 ALOGD("stop");
96 mIsActive = false;
Yu-Han Yang04832302020-11-20 09:51:18 -080097}
98
99void GnssMeasurementInterface::reportMeasurement(const GnssData& data) {
100 ALOGD("reportMeasurement()");
Yu-Han Yang266c36a2021-05-27 16:00:59 -0700101 std::shared_ptr<IGnssMeasurementCallback> callbackCopy;
102 {
103 std::unique_lock<std::mutex> lock(mMutex);
104 if (sCallback == nullptr) {
105 ALOGE("%s: GnssMeasurement::sCallback is null.", __func__);
106 return;
107 }
108 callbackCopy = sCallback;
Yu-Han Yang04832302020-11-20 09:51:18 -0800109 }
Yu-Han Yang266c36a2021-05-27 16:00:59 -0700110 callbackCopy->gnssMeasurementCb(data);
Yu-Han Yang04832302020-11-20 09:51:18 -0800111}
112
113} // namespace aidl::android::hardware::gnss