Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 "GnssMeasurement" |
| 18 | |
| 19 | #include "GnssMeasurement.h" |
| 20 | #include <log/log.h> |
| 21 | #include "Utils.h" |
| 22 | |
| 23 | namespace android { |
| 24 | namespace hardware { |
| 25 | namespace gnss { |
| 26 | |
| 27 | using common::Utils; |
| 28 | |
| 29 | namespace V2_1 { |
| 30 | namespace implementation { |
| 31 | |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 32 | sp<V2_1::IGnssMeasurementCallback> GnssMeasurement::sCallback_2_1 = nullptr; |
| 33 | sp<V2_0::IGnssMeasurementCallback> GnssMeasurement::sCallback_2_0 = nullptr; |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 34 | |
| 35 | GnssMeasurement::GnssMeasurement() : mMinIntervalMillis(1000) {} |
| 36 | |
| 37 | GnssMeasurement::~GnssMeasurement() { |
| 38 | stop(); |
| 39 | } |
| 40 | |
| 41 | // Methods from V1_0::IGnssMeasurement follow. |
| 42 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback( |
| 43 | const sp<V1_0::IGnssMeasurementCallback>&) { |
| 44 | // TODO implement |
| 45 | return V1_0::IGnssMeasurement::GnssMeasurementStatus{}; |
| 46 | } |
| 47 | |
| 48 | Return<void> GnssMeasurement::close() { |
| 49 | ALOGD("close"); |
| 50 | std::unique_lock<std::mutex> lock(mMutex); |
| 51 | stop(); |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 52 | sCallback_2_1 = nullptr; |
| 53 | sCallback_2_0 = nullptr; |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 54 | return Void(); |
| 55 | } |
| 56 | |
| 57 | // Methods from V1_1::IGnssMeasurement follow. |
| 58 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1( |
| 59 | const sp<V1_1::IGnssMeasurementCallback>&, bool) { |
| 60 | // TODO implement |
| 61 | return V1_0::IGnssMeasurement::GnssMeasurementStatus{}; |
| 62 | } |
| 63 | |
| 64 | // Methods from V2_0::IGnssMeasurement follow. |
| 65 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0( |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 66 | const sp<V2_0::IGnssMeasurementCallback>& callback, bool) { |
| 67 | ALOGD("setCallback_2_0"); |
| 68 | std::unique_lock<std::mutex> lock(mMutex); |
| 69 | sCallback_2_0 = callback; |
| 70 | |
| 71 | if (mIsActive) { |
| 72 | ALOGW("GnssMeasurement callback already set. Resetting the callback..."); |
| 73 | stop(); |
| 74 | } |
| 75 | start(); |
| 76 | |
| 77 | return V1_0::IGnssMeasurement::GnssMeasurementStatus::SUCCESS; |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Methods from V2_1::IGnssMeasurement follow. |
| 81 | Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_1( |
| 82 | const sp<V2_1::IGnssMeasurementCallback>& callback, bool) { |
| 83 | ALOGD("setCallback_2_1"); |
| 84 | std::unique_lock<std::mutex> lock(mMutex); |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 85 | sCallback_2_1 = callback; |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 86 | |
| 87 | if (mIsActive) { |
| 88 | ALOGW("GnssMeasurement callback already set. Resetting the callback..."); |
| 89 | stop(); |
| 90 | } |
| 91 | start(); |
| 92 | |
| 93 | return V1_0::IGnssMeasurement::GnssMeasurementStatus::SUCCESS; |
| 94 | } |
| 95 | |
| 96 | void GnssMeasurement::start() { |
| 97 | ALOGD("start"); |
| 98 | mIsActive = true; |
| 99 | mThread = std::thread([this]() { |
| 100 | while (mIsActive == true) { |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 101 | if (sCallback_2_1 != nullptr) { |
| 102 | auto measurement = Utils::getMockMeasurementV2_1(); |
| 103 | this->reportMeasurement(measurement); |
| 104 | } else { |
| 105 | auto measurement = Utils::getMockMeasurementV2_0(); |
| 106 | this->reportMeasurement(measurement); |
| 107 | } |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 108 | |
| 109 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis)); |
| 110 | } |
| 111 | }); |
| 112 | } |
| 113 | |
| 114 | void GnssMeasurement::stop() { |
| 115 | ALOGD("stop"); |
| 116 | mIsActive = false; |
| 117 | if (mThread.joinable()) { |
| 118 | mThread.join(); |
| 119 | } |
| 120 | } |
| 121 | |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 122 | void GnssMeasurement::reportMeasurement(const GnssDataV2_0& data) { |
| 123 | ALOGD("reportMeasurement()"); |
| 124 | std::unique_lock<std::mutex> lock(mMutex); |
| 125 | if (sCallback_2_0 == nullptr) { |
| 126 | ALOGE("%s: GnssMeasurement::sCallback_2_0 is null.", __func__); |
| 127 | return; |
| 128 | } |
| 129 | auto ret = sCallback_2_0->gnssMeasurementCb_2_0(data); |
| 130 | if (!ret.isOk()) { |
| 131 | ALOGE("%s: Unable to invoke callback", __func__); |
| 132 | } |
| 133 | } |
| 134 | |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 135 | void GnssMeasurement::reportMeasurement(const GnssDataV2_1& data) { |
| 136 | ALOGD("reportMeasurement()"); |
| 137 | std::unique_lock<std::mutex> lock(mMutex); |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 138 | if (sCallback_2_1 == nullptr) { |
| 139 | ALOGE("%s: GnssMeasurement::sCallback_2_1 is null.", __func__); |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 140 | return; |
| 141 | } |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 142 | auto ret = sCallback_2_1->gnssMeasurementCb_2_1(data); |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 143 | if (!ret.isOk()) { |
| 144 | ALOGE("%s: Unable to invoke callback", __func__); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | } // namespace implementation |
| 149 | } // namespace V2_1 |
| 150 | } // namespace gnss |
| 151 | } // namespace hardware |
| 152 | } // namespace android |