blob: 0e489c59e43c39b50fc4298d5db1b4d84ffee8d1 [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 He14a30182021-06-10 17:10:15 -070022#include "GnssReplayUtils.h"
Yu-Han Yang04832302020-11-20 09:51:18 -080023#include "Utils.h"
24
25namespace aidl::android::hardware::gnss {
26
27using Utils = ::android::hardware::gnss::common::Utils;
Yuchen He14a30182021-06-10 17:10:15 -070028using ReplayUtils = ::android::hardware::gnss::common::ReplayUtils;
Yu-Han Yang04832302020-11-20 09:51:18 -080029
30std::shared_ptr<IGnssMeasurementCallback> GnssMeasurementInterface::sCallback = nullptr;
31
32GnssMeasurementInterface::GnssMeasurementInterface() : mMinIntervalMillis(1000) {}
33
34GnssMeasurementInterface::~GnssMeasurementInterface() {
35 stop();
36}
37
38ndk::ScopedAStatus GnssMeasurementInterface::setCallback(
Shinru Han4abab502020-12-09 15:07:18 +080039 const std::shared_ptr<IGnssMeasurementCallback>& callback, const bool enableFullTracking,
40 const bool enableCorrVecOutputs) {
41 ALOGD("setCallback: enableFullTracking: %d enableCorrVecOutputs: %d", (int)enableFullTracking,
42 (int)enableCorrVecOutputs);
Yu-Han Yang04832302020-11-20 09:51:18 -080043 std::unique_lock<std::mutex> lock(mMutex);
44 sCallback = callback;
45
46 if (mIsActive) {
47 ALOGW("GnssMeasurement callback already set. Resetting the callback...");
48 stop();
49 }
Shinru Han4abab502020-12-09 15:07:18 +080050 start(enableCorrVecOutputs);
Yu-Han Yang04832302020-11-20 09:51:18 -080051
52 return ndk::ScopedAStatus::ok();
53}
54
55ndk::ScopedAStatus GnssMeasurementInterface::close() {
56 ALOGD("close");
57 stop();
58 std::unique_lock<std::mutex> lock(mMutex);
59 sCallback = nullptr;
60 return ndk::ScopedAStatus::ok();
61}
62
Shinru Han4abab502020-12-09 15:07:18 +080063void GnssMeasurementInterface::start(const bool enableCorrVecOutputs) {
Yu-Han Yang04832302020-11-20 09:51:18 -080064 ALOGD("start");
65 mIsActive = true;
Shinru Han4abab502020-12-09 15:07:18 +080066 mThread = std::thread([this, enableCorrVecOutputs]() {
Yu-Han Yang04832302020-11-20 09:51:18 -080067 while (mIsActive == true) {
Yuchen He14a30182021-06-10 17:10:15 -070068 std::string rawMeasurementStr = "";
69 if (ReplayUtils::hasGnssDeviceFile() &&
70 ReplayUtils::isGnssRawMeasurement(
71 rawMeasurementStr = ReplayUtils::getDataFromDeviceFile(
72 std::string(
73 ::android::hardware::gnss::common::CMD_GET_RAWMEASUREMENT),
74 mMinIntervalMillis))) {
75 // TODO: implement rawMeasurementStr parser and report measurement.
76 ALOGD("rawMeasurementStr(size: %zu) from device file: %s", rawMeasurementStr.size(),
77 rawMeasurementStr.c_str());
78 auto measurement = Utils::getMockMeasurement(enableCorrVecOutputs);
79 this->reportMeasurement(measurement);
80 } else {
81 auto measurement = Utils::getMockMeasurement(enableCorrVecOutputs);
82 this->reportMeasurement(measurement);
83 }
Yu-Han Yang04832302020-11-20 09:51:18 -080084 std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis));
85 }
86 });
Yu-Han Yang26c76352021-05-11 13:16:35 -070087 mThread.detach();
Yu-Han Yang04832302020-11-20 09:51:18 -080088}
89
90void GnssMeasurementInterface::stop() {
91 ALOGD("stop");
92 mIsActive = false;
Yu-Han Yang04832302020-11-20 09:51:18 -080093}
94
95void GnssMeasurementInterface::reportMeasurement(const GnssData& data) {
96 ALOGD("reportMeasurement()");
Yu-Han Yang266c36a2021-05-27 16:00:59 -070097 std::shared_ptr<IGnssMeasurementCallback> callbackCopy;
98 {
99 std::unique_lock<std::mutex> lock(mMutex);
100 if (sCallback == nullptr) {
101 ALOGE("%s: GnssMeasurement::sCallback is null.", __func__);
102 return;
103 }
104 callbackCopy = sCallback;
Yu-Han Yang04832302020-11-20 09:51:18 -0800105 }
Yu-Han Yang266c36a2021-05-27 16:00:59 -0700106 callbackCopy->gnssMeasurementCb(data);
Yu-Han Yang04832302020-11-20 09:51:18 -0800107}
108
109} // namespace aidl::android::hardware::gnss