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 "Utils.h"
23 
24 namespace aidl::android::hardware::gnss {
25 
26 using Utils = ::android::hardware::gnss::common::Utils;
27 
28 std::shared_ptr<IGnssMeasurementCallback> GnssMeasurementInterface::sCallback = nullptr;
29 
GnssMeasurementInterface()30 GnssMeasurementInterface::GnssMeasurementInterface() : mMinIntervalMillis(1000) {}
31 
~GnssMeasurementInterface()32 GnssMeasurementInterface::~GnssMeasurementInterface() {
33     stop();
34 }
35 
setCallback(const std::shared_ptr<IGnssMeasurementCallback> & callback,const bool enableFullTracking,const bool enableCorrVecOutputs)36 ndk::ScopedAStatus GnssMeasurementInterface::setCallback(
37         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);
41     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     }
48     start(enableCorrVecOutputs);
49 
50     return ndk::ScopedAStatus::ok();
51 }
52 
close()53 ndk::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 
start(const bool enableCorrVecOutputs)61 void GnssMeasurementInterface::start(const bool enableCorrVecOutputs) {
62     ALOGD("start");
63     mIsActive = true;
64     mThread = std::thread([this, enableCorrVecOutputs]() {
65         while (mIsActive == true) {
66             auto measurement = Utils::getMockMeasurement(enableCorrVecOutputs);
67             this->reportMeasurement(measurement);
68 
69             std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis));
70         }
71     });
72     mThread.detach();
73 }
74 
stop()75 void GnssMeasurementInterface::stop() {
76     ALOGD("stop");
77     mIsActive = false;
78 }
79 
reportMeasurement(const GnssData & data)80 void GnssMeasurementInterface::reportMeasurement(const GnssData& data) {
81     ALOGD("reportMeasurement()");
82     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;
90     }
91     callbackCopy->gnssMeasurementCb(data);
92 }
93 
94 }  // namespace aidl::android::hardware::gnss
95