blob: fcc1f986cc08346992304d8deca2d8f4bee13e34 [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>
22#include "Utils.h"
23
24namespace aidl::android::hardware::gnss {
25
26using Utils = ::android::hardware::gnss::common::Utils;
27
28std::shared_ptr<IGnssMeasurementCallback> GnssMeasurementInterface::sCallback = nullptr;
29
30GnssMeasurementInterface::GnssMeasurementInterface() : mMinIntervalMillis(1000) {}
31
32GnssMeasurementInterface::~GnssMeasurementInterface() {
33 stop();
34}
35
36ndk::ScopedAStatus GnssMeasurementInterface::setCallback(
Shinru Han4abab502020-12-09 15:07:18 +080037 const std::shared_ptr<IGnssMeasurementCallback>& callback, const bool enableFullTracking,
38 const bool enableCorrVecOutputs) {
39 ALOGD("setCallback: enableFullTracking: %d enableCorrVecOutputs: %d", (int)enableFullTracking,
40 (int)enableCorrVecOutputs);
Yu-Han Yang04832302020-11-20 09:51:18 -080041 std::unique_lock<std::mutex> lock(mMutex);
42 sCallback = callback;
43
44 if (mIsActive) {
45 ALOGW("GnssMeasurement callback already set. Resetting the callback...");
46 stop();
47 }
Shinru Han4abab502020-12-09 15:07:18 +080048 start(enableCorrVecOutputs);
Yu-Han Yang04832302020-11-20 09:51:18 -080049
50 return ndk::ScopedAStatus::ok();
51}
52
53ndk::ScopedAStatus GnssMeasurementInterface::close() {
54 ALOGD("close");
55 stop();
56 std::unique_lock<std::mutex> lock(mMutex);
57 sCallback = nullptr;
58 return ndk::ScopedAStatus::ok();
59}
60
Shinru Han4abab502020-12-09 15:07:18 +080061void GnssMeasurementInterface::start(const bool enableCorrVecOutputs) {
Yu-Han Yang04832302020-11-20 09:51:18 -080062 ALOGD("start");
63 mIsActive = true;
Shinru Han4abab502020-12-09 15:07:18 +080064 mThread = std::thread([this, enableCorrVecOutputs]() {
Yu-Han Yang04832302020-11-20 09:51:18 -080065 while (mIsActive == true) {
Shinru Han4abab502020-12-09 15:07:18 +080066 auto measurement = Utils::getMockMeasurement(enableCorrVecOutputs);
Yu-Han Yang04832302020-11-20 09:51:18 -080067 this->reportMeasurement(measurement);
68
69 std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis));
70 }
71 });
Yu-Han Yang26c76352021-05-11 13:16:35 -070072 mThread.detach();
Yu-Han Yang04832302020-11-20 09:51:18 -080073}
74
75void GnssMeasurementInterface::stop() {
76 ALOGD("stop");
77 mIsActive = false;
Yu-Han Yang04832302020-11-20 09:51:18 -080078}
79
80void GnssMeasurementInterface::reportMeasurement(const GnssData& data) {
81 ALOGD("reportMeasurement()");
Yu-Han Yang266c36a2021-05-27 16:00:59 -070082 std::shared_ptr<IGnssMeasurementCallback> callbackCopy;
83 {
84 std::unique_lock<std::mutex> lock(mMutex);
85 if (sCallback == nullptr) {
86 ALOGE("%s: GnssMeasurement::sCallback is null.", __func__);
87 return;
88 }
89 callbackCopy = sCallback;
Yu-Han Yang04832302020-11-20 09:51:18 -080090 }
Yu-Han Yang266c36a2021-05-27 16:00:59 -070091 callbackCopy->gnssMeasurementCb(data);
Yu-Han Yang04832302020-11-20 09:51:18 -080092}
93
94} // namespace aidl::android::hardware::gnss