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 "Gnss" |
| 18 | |
| 19 | #include "Gnss.h" |
| 20 | #include "GnssMeasurement.h" |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 21 | #include "GnssMeasurementCorrections.h" |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 22 | #include "Utils.h" |
| 23 | |
| 24 | #include <log/log.h> |
| 25 | |
| 26 | using ::android::hardware::gnss::common::Utils; |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 27 | using ::android::hardware::gnss::measurement_corrections::V1_0::implementation:: |
| 28 | GnssMeasurementCorrections; |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | namespace hardware { |
| 32 | namespace gnss { |
| 33 | namespace V2_1 { |
| 34 | namespace implementation { |
| 35 | |
| 36 | sp<V2_1::IGnssCallback> Gnss::sGnssCallback_2_1 = nullptr; |
Sasha Kuznetsov | c1c257b | 2019-12-13 13:08:16 -0800 | [diff] [blame] | 37 | sp<V2_0::IGnssCallback> Gnss::sGnssCallback_2_0 = nullptr; |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 38 | |
Sasha Kuznetsov | 845f6d5 | 2019-12-04 12:17:50 -0800 | [diff] [blame] | 39 | Gnss::Gnss() : mMinIntervalMs(1000), mGnssConfiguration{new GnssConfiguration()} {} |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 40 | |
| 41 | Gnss::~Gnss() { |
| 42 | stop(); |
| 43 | } |
| 44 | |
| 45 | Return<bool> Gnss::start() { |
| 46 | ALOGD("start"); |
| 47 | if (mIsActive) { |
| 48 | ALOGW("Gnss has started. Restarting..."); |
| 49 | stop(); |
| 50 | } |
| 51 | |
| 52 | mIsActive = true; |
| 53 | mThread = std::thread([this]() { |
| 54 | while (mIsActive == true) { |
Sasha Kuznetsov | 845f6d5 | 2019-12-04 12:17:50 -0800 | [diff] [blame] | 55 | auto svStatus = filterBlacklistedSatellitesV2_1(Utils::getMockSvInfoListV2_1()); |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 56 | this->reportSvStatus(svStatus); |
| 57 | |
| 58 | const auto location = Utils::getMockLocationV2_0(); |
| 59 | this->reportLocation(location); |
| 60 | |
| 61 | std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMs)); |
| 62 | } |
| 63 | }); |
| 64 | return true; |
| 65 | } |
| 66 | |
Sasha Kuznetsov | 845f6d5 | 2019-12-04 12:17:50 -0800 | [diff] [blame] | 67 | hidl_vec<GnssSvInfo> Gnss::filterBlacklistedSatellitesV2_1(hidl_vec<GnssSvInfo> gnssSvInfoList) { |
| 68 | for (uint32_t i = 0; i < gnssSvInfoList.size(); i++) { |
| 69 | if (mGnssConfiguration->isBlacklistedV2_1(gnssSvInfoList[i])) { |
| 70 | gnssSvInfoList[i].v2_0.v1_0.svFlag &= |
| 71 | ~static_cast<uint8_t>(V1_0::IGnssCallback::GnssSvFlags::USED_IN_FIX); |
| 72 | } |
| 73 | } |
| 74 | return gnssSvInfoList; |
| 75 | } |
| 76 | |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 77 | Return<bool> Gnss::stop() { |
| 78 | ALOGD("stop"); |
| 79 | mIsActive = false; |
| 80 | if (mThread.joinable()) { |
| 81 | mThread.join(); |
| 82 | } |
| 83 | return true; |
| 84 | } |
| 85 | |
| 86 | // Methods from V1_0::IGnss follow. |
| 87 | Return<bool> Gnss::setCallback(const sp<V1_0::IGnssCallback>&) { |
| 88 | // TODO implement |
| 89 | return bool{}; |
| 90 | } |
| 91 | |
| 92 | Return<void> Gnss::cleanup() { |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 93 | sGnssCallback_2_1 = nullptr; |
| 94 | sGnssCallback_2_0 = nullptr; |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 95 | return Void(); |
| 96 | } |
| 97 | |
| 98 | Return<bool> Gnss::injectTime(int64_t, int64_t, int32_t) { |
| 99 | // TODO implement |
| 100 | return bool{}; |
| 101 | } |
| 102 | |
| 103 | Return<bool> Gnss::injectLocation(double, double, float) { |
| 104 | // TODO implement |
| 105 | return bool{}; |
| 106 | } |
| 107 | |
| 108 | Return<void> Gnss::deleteAidingData(V1_0::IGnss::GnssAidingData) { |
| 109 | // TODO implement |
| 110 | return Void(); |
| 111 | } |
| 112 | |
| 113 | Return<bool> Gnss::setPositionMode(V1_0::IGnss::GnssPositionMode, |
| 114 | V1_0::IGnss::GnssPositionRecurrence, uint32_t, uint32_t, |
| 115 | uint32_t) { |
| 116 | // TODO implement |
| 117 | return bool{}; |
| 118 | } |
| 119 | |
| 120 | Return<sp<V1_0::IAGnssRil>> Gnss::getExtensionAGnssRil() { |
| 121 | // TODO implement |
| 122 | return ::android::sp<V1_0::IAGnssRil>{}; |
| 123 | } |
| 124 | |
| 125 | Return<sp<V1_0::IGnssGeofencing>> Gnss::getExtensionGnssGeofencing() { |
| 126 | // TODO implement |
| 127 | return ::android::sp<V1_0::IGnssGeofencing>{}; |
| 128 | } |
| 129 | |
| 130 | Return<sp<V1_0::IAGnss>> Gnss::getExtensionAGnss() { |
| 131 | // TODO implement |
| 132 | return ::android::sp<V1_0::IAGnss>{}; |
| 133 | } |
| 134 | |
| 135 | Return<sp<V1_0::IGnssNi>> Gnss::getExtensionGnssNi() { |
| 136 | // TODO implement |
| 137 | return ::android::sp<V1_0::IGnssNi>{}; |
| 138 | } |
| 139 | |
| 140 | Return<sp<V1_0::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement() { |
| 141 | // TODO implement |
| 142 | return ::android::sp<V1_0::IGnssMeasurement>{}; |
| 143 | } |
| 144 | |
| 145 | Return<sp<V1_0::IGnssNavigationMessage>> Gnss::getExtensionGnssNavigationMessage() { |
| 146 | // TODO implement |
| 147 | return ::android::sp<V1_0::IGnssNavigationMessage>{}; |
| 148 | } |
| 149 | |
| 150 | Return<sp<V1_0::IGnssXtra>> Gnss::getExtensionXtra() { |
| 151 | // TODO implement |
| 152 | return ::android::sp<V1_0::IGnssXtra>{}; |
| 153 | } |
| 154 | |
| 155 | Return<sp<V1_0::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration() { |
| 156 | // TODO implement |
| 157 | return ::android::sp<V1_0::IGnssConfiguration>{}; |
| 158 | } |
| 159 | |
| 160 | Return<sp<V1_0::IGnssDebug>> Gnss::getExtensionGnssDebug() { |
| 161 | // TODO implement |
| 162 | return ::android::sp<V1_0::IGnssDebug>{}; |
| 163 | } |
| 164 | |
| 165 | Return<sp<V1_0::IGnssBatching>> Gnss::getExtensionGnssBatching() { |
| 166 | // TODO implement |
| 167 | return ::android::sp<V1_0::IGnssBatching>{}; |
| 168 | } |
| 169 | |
| 170 | // Methods from V1_1::IGnss follow. |
| 171 | Return<bool> Gnss::setCallback_1_1(const sp<V1_1::IGnssCallback>&) { |
| 172 | // TODO implement |
| 173 | return bool{}; |
| 174 | } |
| 175 | |
| 176 | Return<bool> Gnss::setPositionMode_1_1(V1_0::IGnss::GnssPositionMode, |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 177 | V1_0::IGnss::GnssPositionRecurrence, uint32_t minIntervalMs, |
| 178 | uint32_t, uint32_t, bool) { |
| 179 | mMinIntervalMs = minIntervalMs; |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 180 | return true; |
| 181 | } |
| 182 | |
| 183 | Return<sp<V1_1::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration_1_1() { |
| 184 | // TODO implement |
| 185 | return ::android::sp<V1_1::IGnssConfiguration>{}; |
| 186 | } |
| 187 | |
| 188 | Return<sp<V1_1::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement_1_1() { |
| 189 | // TODO implement |
| 190 | return ::android::sp<V1_1::IGnssMeasurement>{}; |
| 191 | } |
| 192 | |
| 193 | Return<bool> Gnss::injectBestLocation(const V1_0::GnssLocation&) { |
| 194 | // TODO implement |
| 195 | return bool{}; |
| 196 | } |
| 197 | |
| 198 | // Methods from V2_0::IGnss follow. |
Sasha Kuznetsov | c1c257b | 2019-12-13 13:08:16 -0800 | [diff] [blame] | 199 | Return<bool> Gnss::setCallback_2_0(const sp<V2_0::IGnssCallback>& callback) { |
| 200 | ALOGD("Gnss::setCallback_2_0"); |
| 201 | if (callback == nullptr) { |
| 202 | ALOGE("%s: Null callback ignored", __func__); |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | sGnssCallback_2_0 = callback; |
| 207 | |
| 208 | using Capabilities = V2_0::IGnssCallback::Capabilities; |
| 209 | const auto capabilities = Capabilities::MEASUREMENTS | Capabilities::MEASUREMENT_CORRECTIONS | |
| 210 | Capabilities::LOW_POWER_MODE | Capabilities::SATELLITE_BLACKLIST; |
| 211 | auto ret = sGnssCallback_2_0->gnssSetCapabilitiesCb_2_0(capabilities); |
| 212 | if (!ret.isOk()) { |
| 213 | ALOGE("%s: Unable to invoke callback", __func__); |
| 214 | } |
| 215 | |
| 216 | V1_1::IGnssCallback::GnssSystemInfo gnssInfo = {.yearOfHw = 2019}; |
| 217 | |
| 218 | ret = sGnssCallback_2_0->gnssSetSystemInfoCb(gnssInfo); |
| 219 | if (!ret.isOk()) { |
| 220 | ALOGE("%s: Unable to invoke callback", __func__); |
| 221 | } |
| 222 | |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 223 | auto gnssName = "Google Mock GNSS Implementation v2.1"; |
Sasha Kuznetsov | c1c257b | 2019-12-13 13:08:16 -0800 | [diff] [blame] | 224 | ret = sGnssCallback_2_0->gnssNameCb(gnssName); |
| 225 | if (!ret.isOk()) { |
| 226 | ALOGE("%s: Unable to invoke callback", __func__); |
| 227 | } |
| 228 | |
| 229 | return true; |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | Return<sp<V2_0::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration_2_0() { |
Sasha Kuznetsov | c1c257b | 2019-12-13 13:08:16 -0800 | [diff] [blame] | 233 | ALOGD("Gnss::getExtensionGnssConfiguration_2_0"); |
| 234 | return mGnssConfiguration; |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | Return<sp<V2_0::IGnssDebug>> Gnss::getExtensionGnssDebug_2_0() { |
| 238 | // TODO implement |
| 239 | return ::android::sp<V2_0::IGnssDebug>{}; |
| 240 | } |
| 241 | |
| 242 | Return<sp<V2_0::IAGnss>> Gnss::getExtensionAGnss_2_0() { |
| 243 | // TODO implement |
| 244 | return ::android::sp<V2_0::IAGnss>{}; |
| 245 | } |
| 246 | |
| 247 | Return<sp<V2_0::IAGnssRil>> Gnss::getExtensionAGnssRil_2_0() { |
| 248 | // TODO implement |
| 249 | return ::android::sp<V2_0::IAGnssRil>{}; |
| 250 | } |
| 251 | |
| 252 | Return<sp<V2_0::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement_2_0() { |
Sasha Kuznetsov | c1c257b | 2019-12-13 13:08:16 -0800 | [diff] [blame] | 253 | ALOGD("Gnss::getExtensionGnssMeasurement_2_0"); |
| 254 | return new GnssMeasurement(); |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | Return<sp<measurement_corrections::V1_0::IMeasurementCorrections>> |
| 258 | Gnss::getExtensionMeasurementCorrections() { |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 259 | ALOGD("Gnss::getExtensionMeasurementCorrections()"); |
| 260 | return new GnssMeasurementCorrections(); |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | Return<sp<visibility_control::V1_0::IGnssVisibilityControl>> Gnss::getExtensionVisibilityControl() { |
| 264 | // TODO implement |
| 265 | return ::android::sp<visibility_control::V1_0::IGnssVisibilityControl>{}; |
| 266 | } |
| 267 | |
| 268 | Return<sp<V2_0::IGnssBatching>> Gnss::getExtensionGnssBatching_2_0() { |
| 269 | // TODO implement |
| 270 | return ::android::sp<V2_0::IGnssBatching>{}; |
| 271 | } |
| 272 | |
| 273 | Return<bool> Gnss::injectBestLocation_2_0(const V2_0::GnssLocation&) { |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 274 | // TODO(b/124012850): Implement function. |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 275 | return bool{}; |
| 276 | } |
| 277 | |
| 278 | // Methods from V2_1::IGnss follow. |
| 279 | Return<bool> Gnss::setCallback_2_1(const sp<V2_1::IGnssCallback>& callback) { |
| 280 | ALOGD("Gnss::setCallback_2_1"); |
| 281 | if (callback == nullptr) { |
| 282 | ALOGE("%s: Null callback ignored", __func__); |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | sGnssCallback_2_1 = callback; |
| 287 | |
| 288 | using Capabilities = V2_0::IGnssCallback::Capabilities; |
| 289 | const auto capabilities = Capabilities::MEASUREMENTS | Capabilities::MEASUREMENT_CORRECTIONS | |
| 290 | Capabilities::LOW_POWER_MODE | Capabilities::SATELLITE_BLACKLIST; |
| 291 | auto ret = sGnssCallback_2_1->gnssSetCapabilitiesCb_2_0(capabilities); |
| 292 | if (!ret.isOk()) { |
| 293 | ALOGE("%s: Unable to invoke callback", __func__); |
| 294 | } |
| 295 | |
| 296 | V1_1::IGnssCallback::GnssSystemInfo gnssInfo = {.yearOfHw = 2020}; |
| 297 | |
| 298 | ret = sGnssCallback_2_1->gnssSetSystemInfoCb(gnssInfo); |
| 299 | if (!ret.isOk()) { |
| 300 | ALOGE("%s: Unable to invoke callback", __func__); |
| 301 | } |
| 302 | |
| 303 | auto gnssName = "Android Mock GNSS Implementation v2.1"; |
| 304 | ret = sGnssCallback_2_1->gnssNameCb(gnssName); |
| 305 | if (!ret.isOk()) { |
| 306 | ALOGE("%s: Unable to invoke callback", __func__); |
| 307 | } |
| 308 | |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | Return<sp<V2_1::IGnssMeasurement>> Gnss::getExtensionGnssMeasurement_2_1() { |
| 313 | ALOGD("Gnss::getExtensionGnssMeasurement_2_1"); |
| 314 | return new GnssMeasurement(); |
| 315 | } |
| 316 | |
Sasha Kuznetsov | 845f6d5 | 2019-12-04 12:17:50 -0800 | [diff] [blame] | 317 | Return<sp<V2_1::IGnssConfiguration>> Gnss::getExtensionGnssConfiguration_2_1() { |
Sasha Kuznetsov | c1c257b | 2019-12-13 13:08:16 -0800 | [diff] [blame] | 318 | ALOGD("Gnss::getExtensionGnssConfiguration_2_1"); |
Sasha Kuznetsov | 845f6d5 | 2019-12-04 12:17:50 -0800 | [diff] [blame] | 319 | return mGnssConfiguration; |
| 320 | } |
| 321 | |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 322 | void Gnss::reportSvStatus(const hidl_vec<GnssSvInfo>& svInfoList) const { |
| 323 | std::unique_lock<std::mutex> lock(mMutex); |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 324 | // TODO(skz): update this to call 2_0 callback if non-null |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 325 | if (sGnssCallback_2_1 == nullptr) { |
| 326 | ALOGE("%s: sGnssCallback v2.1 is null.", __func__); |
| 327 | return; |
| 328 | } |
| 329 | auto ret = sGnssCallback_2_1->gnssSvStatusCb_2_1(svInfoList); |
| 330 | if (!ret.isOk()) { |
| 331 | ALOGE("%s: Unable to invoke callback", __func__); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void Gnss::reportLocation(const V2_0::GnssLocation& location) const { |
| 336 | std::unique_lock<std::mutex> lock(mMutex); |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 337 | if (sGnssCallback_2_1 != nullptr) { |
| 338 | auto ret = sGnssCallback_2_1->gnssLocationCb_2_0(location); |
| 339 | if (!ret.isOk()) { |
| 340 | ALOGE("%s: Unable to invoke callback v2.1", __func__); |
| 341 | } |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 342 | return; |
| 343 | } |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 344 | if (sGnssCallback_2_0 == nullptr) { |
| 345 | ALOGE("%s: No non-null callback", __func__); |
| 346 | return; |
| 347 | } |
| 348 | auto ret = sGnssCallback_2_0->gnssLocationCb_2_0(location); |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 349 | if (!ret.isOk()) { |
Sasha Kuznetsov | 7fd5cc3 | 2019-12-09 17:11:08 -0800 | [diff] [blame^] | 350 | ALOGE("%s: Unable to invoke callback v2.0", __func__); |
Yu-Han Yang | c06b536 | 2019-10-25 14:14:35 -0700 | [diff] [blame] | 351 | } |
| 352 | } |
| 353 | |
| 354 | } // namespace implementation |
| 355 | } // namespace V2_1 |
| 356 | } // namespace gnss |
| 357 | } // namespace hardware |
| 358 | } // namespace android |