1 /* 2 * Copyright (C) 2018 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 #define LOG_TAG "GnssMeasurement" 17 18 #include "GnssMeasurement.h" 19 #include "Utils.h" 20 21 #include <log/log.h> 22 #include <utils/SystemClock.h> 23 24 namespace android { 25 namespace hardware { 26 namespace gnss { 27 namespace V2_0 { 28 namespace implementation { 29 30 using GnssConstellationType = V2_0::GnssConstellationType; 31 using GnssMeasurementFlags = V1_0::IGnssMeasurementCallback::GnssMeasurementFlags; 32 using GnssMeasurementState = V2_0::IGnssMeasurementCallback::GnssMeasurementState; 33 using Utils = common::Utils; 34 35 sp<V2_0::IGnssMeasurementCallback> GnssMeasurement::sCallback = nullptr; 36 GnssMeasurement()37GnssMeasurement::GnssMeasurement() : mMinIntervalMillis(1000) {} 38 ~GnssMeasurement()39GnssMeasurement::~GnssMeasurement() { 40 stop(); 41 } 42 43 // Methods from V1_0::IGnssMeasurement follow. setCallback(const sp<V1_0::IGnssMeasurementCallback> &)44Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback( 45 const sp<V1_0::IGnssMeasurementCallback>&) { 46 // TODO implement 47 return V1_0::IGnssMeasurement::GnssMeasurementStatus{}; 48 } 49 close()50Return<void> GnssMeasurement::close() { 51 ALOGD("close"); 52 stop(); 53 std::unique_lock<std::mutex> lock(mMutex); 54 sCallback = nullptr; 55 return Void(); 56 } 57 58 // Methods from V1_1::IGnssMeasurement follow. setCallback_1_1(const sp<V1_1::IGnssMeasurementCallback> &,bool)59Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1( 60 const sp<V1_1::IGnssMeasurementCallback>&, bool) { 61 // TODO implement 62 return V1_0::IGnssMeasurement::GnssMeasurementStatus{}; 63 } 64 65 // Methods from V2_0::IGnssMeasurement follow. setCallback_2_0(const sp<V2_0::IGnssMeasurementCallback> & callback,bool)66Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0( 67 const sp<V2_0::IGnssMeasurementCallback>& callback, bool) { 68 ALOGD("setCallback_2_0"); 69 std::unique_lock<std::mutex> lock(mMutex); 70 sCallback = callback; 71 72 if (mIsActive) { 73 ALOGW("GnssMeasurement callback already set. Resetting the callback..."); 74 stop(); 75 } 76 start(); 77 78 return V1_0::IGnssMeasurement::GnssMeasurementStatus::SUCCESS; 79 } 80 start()81void GnssMeasurement::start() { 82 ALOGD("start"); 83 mIsActive = true; 84 mThread = std::thread([this]() { 85 while (mIsActive == true) { 86 auto measurement = Utils::getMockMeasurementV2_0(); 87 this->reportMeasurement(measurement); 88 89 std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis)); 90 } 91 }); 92 } 93 stop()94void GnssMeasurement::stop() { 95 ALOGD("stop"); 96 mIsActive = false; 97 if (mThread.joinable()) { 98 mThread.join(); 99 } 100 } 101 reportMeasurement(const GnssData & data)102void GnssMeasurement::reportMeasurement(const GnssData& data) { 103 ALOGD("reportMeasurement()"); 104 std::unique_lock<std::mutex> lock(mMutex); 105 if (sCallback == nullptr) { 106 ALOGE("%s: GnssMeasurement::sCallback is null.", __func__); 107 return; 108 } 109 sCallback->gnssMeasurementCb_2_0(data); 110 } 111 112 } // namespace implementation 113 } // namespace V2_0 114 } // namespace gnss 115 } // namespace hardware 116 } // namespace android 117