Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 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 | */ |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 16 | #define LOG_TAG "GnssMeasurement" |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 17 | |
| 18 | #include "GnssMeasurement.h" |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 19 | #include "Utils.h" |
Pierre Fite-Georgel | 12ac2b5 | 2019-01-17 16:56:17 -0800 | [diff] [blame] | 20 | |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 21 | #include <log/log.h> |
Pierre Fite-Georgel | 12ac2b5 | 2019-01-17 16:56:17 -0800 | [diff] [blame] | 22 | #include <utils/SystemClock.h> |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | namespace hardware { |
| 26 | namespace gnss { |
| 27 | namespace V2_0 { |
| 28 | namespace implementation { |
| 29 | |
Yu-Han Yang | 6999a0f | 2019-03-14 11:08:43 -0700 | [diff] [blame] | 30 | using GnssConstellationType = V2_0::GnssConstellationType; |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 31 | using GnssMeasurementFlags = V1_0::IGnssMeasurementCallback::GnssMeasurementFlags; |
Yu-Han Yang | 031738b | 2019-02-01 19:58:04 -0800 | [diff] [blame] | 32 | using GnssMeasurementState = V2_0::IGnssMeasurementCallback::GnssMeasurementState; |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 33 | using Utils = common::Utils; |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 34 | |
| 35 | sp<V2_0::IGnssMeasurementCallback> GnssMeasurement::sCallback = nullptr; |
| 36 | |
| 37 | GnssMeasurement::GnssMeasurement() : mMinIntervalMillis(1000) {} |
| 38 | |
| 39 | GnssMeasurement::~GnssMeasurement() { |
| 40 | stop(); |
| 41 | } |
| 42 | |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 43 | // Methods from V1_0::IGnssMeasurement follow. |
| 44 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback( |
| 45 | const sp<V1_0::IGnssMeasurementCallback>&) { |
| 46 | // TODO implement |
| 47 | return V1_0::IGnssMeasurement::GnssMeasurementStatus{}; |
| 48 | } |
| 49 | |
| 50 | Return<void> GnssMeasurement::close() { |
Yu-Han Yang | 6999a0f | 2019-03-14 11:08:43 -0700 | [diff] [blame] | 51 | ALOGD("close"); |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 52 | std::unique_lock<std::mutex> lock(mMutex); |
| 53 | stop(); |
| 54 | sCallback = nullptr; |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 55 | return Void(); |
| 56 | } |
| 57 | |
| 58 | // Methods from V1_1::IGnssMeasurement follow. |
| 59 | Return<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. |
| 66 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0( |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 67 | const sp<V2_0::IGnssMeasurementCallback>& callback, bool) { |
Yu-Han Yang | 6999a0f | 2019-03-14 11:08:43 -0700 | [diff] [blame] | 68 | ALOGD("setCallback_2_0"); |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 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; |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 79 | } |
| 80 | |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 81 | void GnssMeasurement::start() { |
Yu-Han Yang | 6999a0f | 2019-03-14 11:08:43 -0700 | [diff] [blame] | 82 | ALOGD("start"); |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 83 | mIsActive = true; |
| 84 | mThread = std::thread([this]() { |
| 85 | while (mIsActive == true) { |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 86 | auto measurement = Utils::getMockMeasurementV2_0(); |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 87 | this->reportMeasurement(measurement); |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 88 | |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 89 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis)); |
| 90 | } |
| 91 | }); |
| 92 | } |
| 93 | |
| 94 | void GnssMeasurement::stop() { |
Yu-Han Yang | 6999a0f | 2019-03-14 11:08:43 -0700 | [diff] [blame] | 95 | ALOGD("stop"); |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 96 | mIsActive = false; |
| 97 | if (mThread.joinable()) { |
| 98 | mThread.join(); |
| 99 | } |
| 100 | } |
| 101 | |
Yu-Han Yang | 030d033 | 2018-12-09 10:47:42 -0800 | [diff] [blame] | 102 | void 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 | |
Yu-Han Yang | 9c6c20b | 2018-11-06 14:12:49 -0800 | [diff] [blame] | 112 | } // namespace implementation |
| 113 | } // namespace V2_0 |
| 114 | } // namespace gnss |
| 115 | } // namespace hardware |
| 116 | } // namespace android |