Home | History | Annotate | Download | only in location_api
      1 /* Copyright (c) 2017, The Linux Foundation. All rights reserved.
      2  *
      3  * Redistribution and use in source and binary forms, with or without
      4  * modification, are permitted provided that the following conditions are
      5  * met:
      6  *     * Redistributions of source code must retain the above copyright
      7  *       notice, this list of conditions and the following disclaimer.
      8  *     * Redistributions in binary form must reproduce the above
      9  *       copyright notice, this list of conditions and the following
     10  *       disclaimer in the documentation and/or other materials provided
     11  *       with the distribution.
     12  *     * Neither the name of The Linux Foundation, nor the names of its
     13  *       contributors may be used to endorse or promote products derived
     14  *       from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  *
     28  */
     29 
     30 #define LOG_NDDEBUG 0
     31 #define LOG_TAG "LocSvc_GnssMeasurementAPIClient"
     32 
     33 #include <log_util.h>
     34 #include <loc_cfg.h>
     35 
     36 #include "LocationUtil.h"
     37 #include "GnssMeasurementAPIClient.h"
     38 
     39 namespace android {
     40 namespace hardware {
     41 namespace gnss {
     42 namespace V1_0 {
     43 namespace implementation {
     44 
     45 static void convertGnssData(GnssMeasurementsNotification& in,
     46         IGnssMeasurementCallback::GnssData& out);
     47 static void convertGnssMeasurement(GnssMeasurementsData& in,
     48         IGnssMeasurementCallback::GnssMeasurement& out);
     49 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out);
     50 
     51 GnssMeasurementAPIClient::GnssMeasurementAPIClient() :
     52     mGnssMeasurementCbIface(nullptr),
     53     mLocationCapabilitiesMask(0)
     54 {
     55     LOC_LOGD("%s]: ()", __FUNCTION__);
     56     pthread_mutex_init(&mLock, nullptr);
     57     pthread_cond_init (&mCond, nullptr);
     58 
     59     // set default LocationOptions.
     60     memset(&mLocationOptions, 0, sizeof(LocationOptions));
     61     mLocationOptions.size = sizeof(LocationOptions);
     62     mLocationOptions.minInterval = 1000;
     63     mLocationOptions.minDistance = 0;
     64     mLocationOptions.mode = GNSS_SUPL_MODE_STANDALONE;
     65 }
     66 
     67 GnssMeasurementAPIClient::~GnssMeasurementAPIClient()
     68 {
     69     LOC_LOGD("%s]: ()", __FUNCTION__);
     70     pthread_cond_destroy(&mCond);
     71     pthread_mutex_destroy(&mLock);
     72 }
     73 
     74 // for GpsInterface
     75 Return<IGnssMeasurement::GnssMeasurementStatus>
     76 GnssMeasurementAPIClient::gnssMeasurementSetCallback(const sp<IGnssMeasurementCallback>& callback)
     77 {
     78     LOC_LOGD("%s]: (%p)", __FUNCTION__, &callback);
     79 
     80     mGnssMeasurementCbIface = callback;
     81 
     82     LocationCallbacks locationCallbacks;
     83     locationCallbacks.size = sizeof(LocationCallbacks);
     84 
     85     locationCallbacks.trackingCb = nullptr;
     86     locationCallbacks.batchingCb = nullptr;
     87     locationCallbacks.geofenceBreachCb = nullptr;
     88     locationCallbacks.geofenceStatusCb = nullptr;
     89     locationCallbacks.gnssLocationInfoCb = nullptr;
     90     locationCallbacks.gnssNiCb = nullptr;
     91     locationCallbacks.gnssSvCb = nullptr;
     92     locationCallbacks.gnssNmeaCb = nullptr;
     93 
     94     locationCallbacks.gnssMeasurementsCb = nullptr;
     95     if (mGnssMeasurementCbIface != nullptr) {
     96         locationCallbacks.gnssMeasurementsCb =
     97             [this](GnssMeasurementsNotification gnssMeasurementsNotification) {
     98                 onGnssMeasurementsCb(gnssMeasurementsNotification);
     99             };
    100     }
    101 
    102     locAPISetCallbacks(locationCallbacks);
    103 
    104     while (!mLocationCapabilitiesMask) {
    105         LOC_LOGD("%s]: wait for capabilities...", __FUNCTION__);
    106         pthread_mutex_lock(&mLock);
    107         pthread_cond_wait(&mCond, &mLock);
    108         pthread_mutex_unlock(&mLock);
    109     }
    110     if (mLocationCapabilitiesMask & LOCATION_CAPABILITIES_GNSS_MSB_BIT)
    111         mLocationOptions.mode = GNSS_SUPL_MODE_MSB;
    112     else if (mLocationCapabilitiesMask & LOCATION_CAPABILITIES_GNSS_MSA_BIT)
    113         mLocationOptions.mode = GNSS_SUPL_MODE_MSA;
    114     LOC_LOGD("%s]: start tracking session", __FUNCTION__);
    115     locAPIStartTracking(mLocationOptions);
    116 
    117     return IGnssMeasurement::GnssMeasurementStatus::SUCCESS;
    118 }
    119 
    120 // for GpsMeasurementInterface
    121 void GnssMeasurementAPIClient::gnssMeasurementClose() {
    122     LOC_LOGD("%s]: ()", __FUNCTION__);
    123     pthread_mutex_lock(&mLock);
    124     mGnssMeasurementCbIface = nullptr;
    125     pthread_mutex_unlock(&mLock);
    126 }
    127 
    128 // callbacks
    129 void GnssMeasurementAPIClient::onCapabilitiesCb(LocationCapabilitiesMask capabilitiesMask)
    130 {
    131     LOC_LOGD("%s]: (%02x)", __FUNCTION__, capabilitiesMask);
    132     mLocationCapabilitiesMask = capabilitiesMask;
    133     pthread_mutex_lock(&mLock);
    134     pthread_cond_signal(&mCond);
    135     pthread_mutex_unlock(&mLock);
    136 }
    137 
    138 void GnssMeasurementAPIClient::onGnssMeasurementsCb(
    139         GnssMeasurementsNotification gnssMeasurementsNotification)
    140 {
    141     LOC_LOGD("%s]: (count: %zu)", __FUNCTION__, gnssMeasurementsNotification.count);
    142     // we don't need to lock the mutext
    143     // if mGnssMeasurementCbIface is set to nullptr
    144     if (mGnssMeasurementCbIface != nullptr) {
    145         pthread_mutex_lock(&mLock);
    146         if (mGnssMeasurementCbIface != nullptr) {
    147             IGnssMeasurementCallback::GnssData gnssData;
    148             convertGnssData(gnssMeasurementsNotification, gnssData);
    149             mGnssMeasurementCbIface->GnssMeasurementCb(gnssData);
    150         }
    151         pthread_mutex_unlock(&mLock);
    152     }
    153 }
    154 
    155 static void convertGnssMeasurement(GnssMeasurementsData& in,
    156         IGnssMeasurementCallback::GnssMeasurement& out)
    157 {
    158     memset(&out, 0, sizeof(IGnssMeasurementCallback::GnssMeasurement));
    159     if (in.flags & GNSS_MEASUREMENTS_DATA_SIGNAL_TO_NOISE_RATIO_BIT)
    160         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_SNR;
    161     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_FREQUENCY_BIT)
    162         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_FREQUENCY;
    163     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_CYCLES_BIT)
    164         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_CYCLES;
    165     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_BIT)
    166         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE;
    167     if (in.flags & GNSS_MEASUREMENTS_DATA_CARRIER_PHASE_UNCERTAINTY_BIT)
    168         out.flags |= IGnssMeasurementCallback::GnssMeasurementFlags::HAS_CARRIER_PHASE_UNCERTAINTY;
    169     out.svid = in.svId;
    170     convertGnssConstellationType(in.svType, out.constellation);
    171     out.timeOffsetNs = in.timeOffsetNs;
    172     if (in.stateMask & GNSS_MEASUREMENTS_STATE_CODE_LOCK_BIT)
    173         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_CODE_LOCK;
    174     if (in.stateMask & GNSS_MEASUREMENTS_STATE_BIT_SYNC_BIT)
    175         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BIT_SYNC;
    176     if (in.stateMask & GNSS_MEASUREMENTS_STATE_SUBFRAME_SYNC_BIT)
    177         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SUBFRAME_SYNC;
    178     if (in.stateMask & GNSS_MEASUREMENTS_STATE_TOW_DECODED_BIT)
    179         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_TOW_DECODED;
    180     if (in.stateMask & GNSS_MEASUREMENTS_STATE_MSEC_AMBIGUOUS_BIT)
    181         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_MSEC_AMBIGUOUS;
    182     if (in.stateMask & GNSS_MEASUREMENTS_STATE_SYMBOL_SYNC_BIT)
    183         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SYMBOL_SYNC;
    184     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_STRING_SYNC_BIT)
    185         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_STRING_SYNC;
    186     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GLO_TOD_DECODED_BIT)
    187         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GLO_TOD_DECODED;
    188     if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_BIT_SYNC_BIT)
    189         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_BIT_SYNC;
    190     if (in.stateMask & GNSS_MEASUREMENTS_STATE_BDS_D2_SUBFRAME_SYNC_BIT)
    191         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_BDS_D2_SUBFRAME_SYNC;
    192     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1BC_CODE_LOCK_BIT)
    193         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1BC_CODE_LOCK;
    194     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1C_2ND_CODE_LOCK_BIT)
    195         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1C_2ND_CODE_LOCK;
    196     if (in.stateMask & GNSS_MEASUREMENTS_STATE_GAL_E1B_PAGE_SYNC_BIT)
    197         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_GAL_E1B_PAGE_SYNC;
    198     if (in.stateMask &  GNSS_MEASUREMENTS_STATE_SBAS_SYNC_BIT)
    199         out.state |= IGnssMeasurementCallback::GnssMeasurementState::STATE_SBAS_SYNC;
    200     out.receivedSvTimeInNs = in.receivedSvTimeNs;
    201     out.receivedSvTimeUncertaintyInNs = in.receivedSvTimeUncertaintyNs;
    202     out.cN0DbHz = in.carrierToNoiseDbHz;
    203     out.pseudorangeRateMps = in.pseudorangeRateMps;
    204     out.pseudorangeRateUncertaintyMps = in.pseudorangeRateUncertaintyMps;
    205     if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_VALID_BIT)
    206         out.accumulatedDeltaRangeState |=
    207             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_VALID;
    208     if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_RESET_BIT)
    209         out.accumulatedDeltaRangeState |=
    210             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_RESET;
    211     if (in.adrStateMask & GNSS_MEASUREMENTS_ACCUMULATED_DELTA_RANGE_STATE_CYCLE_SLIP_BIT)
    212         out.accumulatedDeltaRangeState |=
    213             IGnssMeasurementCallback::GnssAccumulatedDeltaRangeState::ADR_STATE_CYCLE_SLIP;
    214     out.accumulatedDeltaRangeM = in.adrMeters;
    215     out.accumulatedDeltaRangeUncertaintyM = in.adrUncertaintyMeters;
    216     out.carrierFrequencyHz = in.carrierFrequencyHz;
    217     out.carrierCycles = in.carrierCycles;
    218     out.carrierPhase = in.carrierPhase;
    219     out.carrierPhaseUncertainty = in.carrierPhaseUncertainty;
    220     uint8_t indicator =
    221         static_cast<uint8_t>(IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_UNKNOWN);
    222     if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_PRESENT)
    223         indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATOR_PRESENT;
    224     if (in.multipathIndicator & GNSS_MEASUREMENTS_MULTIPATH_INDICATOR_NOT_PRESENT)
    225         indicator |= IGnssMeasurementCallback::GnssMultipathIndicator::INDICATIOR_NOT_PRESENT;
    226     out.multipathIndicator =
    227         static_cast<IGnssMeasurementCallback::GnssMultipathIndicator>(indicator);
    228     out.snrDb = in.signalToNoiseRatioDb;
    229 }
    230 
    231 static void convertGnssClock(GnssMeasurementsClock& in, IGnssMeasurementCallback::GnssClock& out)
    232 {
    233     memset(&out, 0, sizeof(IGnssMeasurementCallback::GnssClock));
    234     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_LEAP_SECOND_BIT)
    235         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_LEAP_SECOND;
    236     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_TIME_UNCERTAINTY_BIT)
    237         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_TIME_UNCERTAINTY;
    238     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_FULL_BIAS_BIT)
    239         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_FULL_BIAS;
    240     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_BIT)
    241         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS;
    242     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_BIAS_UNCERTAINTY_BIT)
    243         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_BIAS_UNCERTAINTY;
    244     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_BIT)
    245         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT;
    246     if (in.flags & GNSS_MEASUREMENTS_CLOCK_FLAGS_DRIFT_UNCERTAINTY_BIT)
    247         out.gnssClockFlags |= IGnssMeasurementCallback::GnssClockFlags::HAS_DRIFT_UNCERTAINTY;
    248     out.leapSecond = in.leapSecond;
    249     out.timeNs = in.timeNs;
    250     out.timeUncertaintyNs = in.timeUncertaintyNs;
    251     out.fullBiasNs = in.fullBiasNs;
    252     out.biasNs = in.biasNs;
    253     out.biasUncertaintyNs = in.biasUncertaintyNs;
    254     out.driftNsps = in.driftNsps;
    255     out.driftUncertaintyNsps = in.driftUncertaintyNsps;
    256     out.hwClockDiscontinuityCount = in.hwClockDiscontinuityCount;
    257 }
    258 
    259 static void convertGnssData(GnssMeasurementsNotification& in,
    260         IGnssMeasurementCallback::GnssData& out)
    261 {
    262     out.measurementCount = in.count;
    263     if (out.measurementCount > static_cast<uint32_t>(GnssMax::SVS_COUNT)) {
    264         LOC_LOGW("%s]: Too many measurement %zd. Clamps to %d.",
    265                 __FUNCTION__,  out.measurementCount, GnssMax::SVS_COUNT);
    266         out.measurementCount = static_cast<uint32_t>(GnssMax::SVS_COUNT);
    267     }
    268     for (size_t i = 0; i < out.measurementCount; i++) {
    269         convertGnssMeasurement(in.measurements[i], out.measurements[i]);
    270     }
    271     convertGnssClock(in.clock, out.clock);
    272 }
    273 
    274 }  // namespace implementation
    275 }  // namespace V1_0
    276 }  // namespace gnss
    277 }  // namespace hardware
    278 }  // namespace android
    279