Home | History | Annotate | Download | only in default
      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 
     20 #include <log/log.h>
     21 #include <utils/SystemClock.h>
     22 
     23 namespace android {
     24 namespace hardware {
     25 namespace gnss {
     26 namespace V2_0 {
     27 namespace implementation {
     28 
     29 using GnssConstellationType = V2_0::GnssConstellationType;
     30 using GnssMeasurementFlags = V1_0::IGnssMeasurementCallback::GnssMeasurementFlags;
     31 using GnssMeasurementState = V2_0::IGnssMeasurementCallback::GnssMeasurementState;
     32 
     33 sp<V2_0::IGnssMeasurementCallback> GnssMeasurement::sCallback = nullptr;
     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();
     52     sCallback = nullptr;
     53     return Void();
     54 }
     55 
     56 // Methods from V1_1::IGnssMeasurement follow.
     57 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_1_1(
     58     const sp<V1_1::IGnssMeasurementCallback>&, bool) {
     59     // TODO implement
     60     return V1_0::IGnssMeasurement::GnssMeasurementStatus{};
     61 }
     62 
     63 // Methods from V2_0::IGnssMeasurement follow.
     64 Return<V1_0::IGnssMeasurement::GnssMeasurementStatus> GnssMeasurement::setCallback_2_0(
     65     const sp<V2_0::IGnssMeasurementCallback>& callback, bool) {
     66     ALOGD("setCallback_2_0");
     67     std::unique_lock<std::mutex> lock(mMutex);
     68     sCallback = callback;
     69 
     70     if (mIsActive) {
     71         ALOGW("GnssMeasurement callback already set. Resetting the callback...");
     72         stop();
     73     }
     74     start();
     75 
     76     return V1_0::IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
     77 }
     78 
     79 void GnssMeasurement::start() {
     80     ALOGD("start");
     81     mIsActive = true;
     82     mThread = std::thread([this]() {
     83         while (mIsActive == true) {
     84             auto measurement = this->getMockMeasurement();
     85             this->reportMeasurement(measurement);
     86 
     87             std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMillis));
     88         }
     89     });
     90 }
     91 
     92 void GnssMeasurement::stop() {
     93     ALOGD("stop");
     94     mIsActive = false;
     95     if (mThread.joinable()) {
     96         mThread.join();
     97     }
     98 }
     99 
    100 GnssData GnssMeasurement::getMockMeasurement() {
    101     V1_0::IGnssMeasurementCallback::GnssMeasurement measurement_1_0 = {
    102             .flags = (uint32_t)GnssMeasurementFlags::HAS_CARRIER_FREQUENCY,
    103             .svid = (int16_t)6,
    104             .constellation = V1_0::GnssConstellationType::UNKNOWN,
    105             .timeOffsetNs = 0.0,
    106             .receivedSvTimeInNs = 8195997131077,
    107             .receivedSvTimeUncertaintyInNs = 15,
    108             .cN0DbHz = 30.0,
    109             .pseudorangeRateMps = -484.13739013671875,
    110             .pseudorangeRateUncertaintyMps = 1.0379999876022339,
    111             .accumulatedDeltaRangeState = (uint32_t)V1_0::IGnssMeasurementCallback::
    112                     GnssAccumulatedDeltaRangeState::ADR_STATE_UNKNOWN,
    113             .accumulatedDeltaRangeM = 0.0,
    114             .accumulatedDeltaRangeUncertaintyM = 0.0,
    115             .carrierFrequencyHz = 1.59975e+09,
    116             .multipathIndicator =
    117                     V1_0::IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_UNKNOWN};
    118     V1_1::IGnssMeasurementCallback::GnssMeasurement measurement_1_1 = {.v1_0 = measurement_1_0};
    119     V2_0::IGnssMeasurementCallback::GnssMeasurement measurement_2_0 = {
    120             .v1_1 = measurement_1_1,
    121             .codeType = "C",
    122             .constellation = GnssConstellationType::GLONASS,
    123             .state = GnssMeasurementState::STATE_CODE_LOCK | GnssMeasurementState::STATE_BIT_SYNC |
    124                      GnssMeasurementState::STATE_SUBFRAME_SYNC |
    125                      GnssMeasurementState::STATE_TOW_DECODED |
    126                      GnssMeasurementState::STATE_GLO_STRING_SYNC |
    127                      GnssMeasurementState::STATE_GLO_TOD_DECODED};
    128 
    129     hidl_vec<IGnssMeasurementCallback::GnssMeasurement> measurements(1);
    130     measurements[0] = measurement_2_0;
    131     V1_0::IGnssMeasurementCallback::GnssClock clock = {.timeNs = 2713545000000,
    132                                                        .fullBiasNs = -1226701900521857520,
    133                                                        .biasNs = 0.59689998626708984,
    134                                                        .biasUncertaintyNs = 47514.989972114563,
    135                                                        .driftNsps = -51.757811607455452,
    136                                                        .driftUncertaintyNsps = 310.64968328491528,
    137                                                        .hwClockDiscontinuityCount = 1};
    138 
    139     ElapsedRealtime timestamp = {
    140             .flags = ElapsedRealtimeFlags::HAS_TIMESTAMP_NS |
    141                      ElapsedRealtimeFlags::HAS_TIME_UNCERTAINTY_NS,
    142             .timestampNs = static_cast<uint64_t>(::android::elapsedRealtimeNano()),
    143             // This is an hardcoded value indicating a 1ms of uncertainty between the two clocks.
    144             // In an actual implementation provide an estimate of the synchronization uncertainty
    145             // or don't set the field.
    146             .timeUncertaintyNs = 1000000};
    147 
    148     GnssData gnssData = {
    149             .measurements = measurements, .clock = clock, .elapsedRealtime = timestamp};
    150     return gnssData;
    151 }
    152 
    153 void GnssMeasurement::reportMeasurement(const GnssData& data) {
    154     ALOGD("reportMeasurement()");
    155     std::unique_lock<std::mutex> lock(mMutex);
    156     if (sCallback == nullptr) {
    157         ALOGE("%s: GnssMeasurement::sCallback is null.", __func__);
    158         return;
    159     }
    160     sCallback->gnssMeasurementCb_2_0(data);
    161 }
    162 
    163 }  // namespace implementation
    164 }  // namespace V2_0
    165 }  // namespace gnss
    166 }  // namespace hardware
    167 }  // namespace android
    168