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