Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4bd3e453 authored by Wyatt Riley's avatar Wyatt Riley Committed by Android (Google) Code Review
Browse files

Merge "Mock blacklisting satellites in default implementation" into pi-dev

parents 8c7a0309 a2f77322
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ cc_binary {
    vendor: true,
    srcs: [
        "Gnss.cpp",
        "GnssDebug.cpp",
        "GnssConfiguration.cpp",
        "GnssMeasurement.cpp",
        "service.cpp",
+77 −24
Original line number Diff line number Diff line
#define LOG_TAG "Gnss"

#include <android/hardware/gnss/1.0/types.h>
#include <log/log.h>

#include "Gnss.h"
#include "GnssConfiguration.h"
#include "GnssConstants.h"
#include "GnssDebug.h"
#include "GnssMeasurement.h"

namespace android {
@@ -12,10 +14,12 @@ namespace gnss {
namespace V1_1 {
namespace implementation {

using GnssSvFlags = IGnssCallback::GnssSvFlags;

const uint32_t MIN_INTERVAL_MILLIS = 100;
sp<::android::hardware::gnss::V1_1::IGnssCallback> Gnss::sGnssCallback = nullptr;

Gnss::Gnss() : mMinIntervalMs(1000) {}
Gnss::Gnss() : mMinIntervalMs(1000), mGnssConfiguration{new GnssConfiguration()} {}

Gnss::~Gnss() {
    stop();
@@ -36,7 +40,10 @@ Return<bool> Gnss::start() {
    mIsActive = true;
    mThread = std::thread([this]() {
        while (mIsActive == true) {
            V1_0::GnssLocation location = this->getMockLocation();
            auto svStatus = this->getMockSvStatus();
            this->reportSvStatus(svStatus);

            auto location = this->getMockLocation();
            this->reportLocation(location);

            std::this_thread::sleep_for(std::chrono::milliseconds(mMinIntervalMs));
@@ -70,7 +77,6 @@ Return<bool> Gnss::injectLocation(double, double, float) {
}

Return<void> Gnss::deleteAidingData(::android::hardware::gnss::V1_0::IGnss::GnssAidingData) {
    // TODO implement
    return Void();
}

@@ -124,8 +130,7 @@ Gnss::getExtensionGnssConfiguration() {
}

Return<sp<::android::hardware::gnss::V1_0::IGnssDebug>> Gnss::getExtensionGnssDebug() {
    // TODO implement
    return ::android::sp<::android::hardware::gnss::V1_0::IGnssDebug>{};
    return new GnssDebug();
}

Return<sp<::android::hardware::gnss::V1_0::IGnssBatching>> Gnss::getExtensionGnssBatching() {
@@ -175,8 +180,7 @@ Return<bool> Gnss::setPositionMode_1_1(

Return<sp<::android::hardware::gnss::V1_1::IGnssConfiguration>>
Gnss::getExtensionGnssConfiguration_1_1() {
    // TODO implement
    return new GnssConfiguration();
    return mGnssConfiguration;
}

Return<sp<::android::hardware::gnss::V1_1::IGnssMeasurement>>
@@ -185,27 +189,66 @@ Gnss::getExtensionGnssMeasurement_1_1() {
    return new GnssMeasurement();
}

Return<bool> Gnss::injectBestLocation(const ::android::hardware::gnss::V1_0::GnssLocation&) {
    // TODO implement
    return bool{};
Return<bool> Gnss::injectBestLocation(const GnssLocation&) {
    return true;
}

Return<V1_0::GnssLocation> Gnss::getMockLocation() {
    V1_0::GnssLocation location = {.gnssLocationFlags = 0xFF,
                                   .latitudeDegrees = 37.4219999,
                                   .longitudeDegrees = -122.0840575,
                                   .altitudeMeters = 1.60062531,
                                   .speedMetersPerSec = 0,
                                   .bearingDegrees = 0,
                                   .horizontalAccuracyMeters = 5,
                                   .verticalAccuracyMeters = 5,
                                   .speedAccuracyMetersPerSecond = 1,
                                   .bearingAccuracyDegrees = 90,
                                   .timestamp = 1519930775453L};
Return<GnssLocation> Gnss::getMockLocation() const {
    GnssLocation location = {.gnssLocationFlags = 0xFF,
                             .latitudeDegrees = kMockLatitudeDegrees,
                             .longitudeDegrees = kMockLongitudeDegrees,
                             .altitudeMeters = kMockAltitudeMeters,
                             .speedMetersPerSec = kMockSpeedMetersPerSec,
                             .bearingDegrees = kMockBearingDegrees,
                             .horizontalAccuracyMeters = kMockHorizontalAccuracyMeters,
                             .verticalAccuracyMeters = kMockVerticalAccuracyMeters,
                             .speedAccuracyMetersPerSecond = kMockSpeedAccuracyMetersPerSecond,
                             .bearingAccuracyDegrees = kMockBearingAccuracyDegrees,
                             .timestamp = kMockTimestamp};
    return location;
}

Return<void> Gnss::reportLocation(const V1_0::GnssLocation& location) {
Return<GnssSvInfo> Gnss::getSvInfo(int16_t svid, GnssConstellationType type, float cN0DbHz,
                                   float elevationDegrees, float azimuthDegrees) const {
    GnssSvInfo svInfo = {.svid = svid,
                         .constellation = type,
                         .cN0Dbhz = cN0DbHz,
                         .elevationDegrees = elevationDegrees,
                         .azimuthDegrees = azimuthDegrees,
                         .svFlag = GnssSvFlags::USED_IN_FIX | GnssSvFlags::HAS_EPHEMERIS_DATA |
                                   GnssSvFlags::HAS_ALMANAC_DATA};
    return svInfo;
}

Return<GnssSvStatus> Gnss::getMockSvStatus() const {
    std::unique_lock<std::recursive_mutex> lock(mGnssConfiguration->getMutex());
    GnssSvInfo mockGnssSvInfoList[] = {
        getSvInfo(3, GnssConstellationType::GPS, 32.5, 59.1, 166.5),
        getSvInfo(5, GnssConstellationType::GPS, 27.0, 29.0, 56.5),
        getSvInfo(17, GnssConstellationType::GPS, 30.5, 71.0, 77.0),
        getSvInfo(26, GnssConstellationType::GPS, 24.1, 28.0, 253.0),
        getSvInfo(30, GnssConstellationType::GPS, 20.5, 11.5, 116.0),
        getSvInfo(10, GnssConstellationType::GLONASS, 25.0, 66.0, 247.0)};

    GnssSvStatus svStatus = {.numSvs = sizeof(mockGnssSvInfoList) / sizeof(GnssSvInfo)};
    for (uint32_t i = 0; i < svStatus.numSvs; i++) {
        if (mGnssConfiguration->isBlacklisted(mockGnssSvInfoList[i])) {
            /**
             * Note well, this is a simple, mock emulation of not using a satellite by changing the
             * used bit.  Simply blanking the used bit, as is done here, is *not* an acceptable
             * actual device implementation - actual devices *must not* use the satellite in the
             * position calculation, as specified in IGnssConfiguration.hal.
             */
            mockGnssSvInfoList[i].svFlag &=
                ~static_cast<uint8_t>(IGnssCallback::GnssSvFlags::USED_IN_FIX);
        }
        svStatus.gnssSvList[i] = mockGnssSvInfoList[i];
    }

    return svStatus;
}

Return<void> Gnss::reportLocation(const GnssLocation& location) const {
    std::unique_lock<std::mutex> lock(mMutex);
    if (sGnssCallback == nullptr) {
        ALOGE("%s: sGnssCallback is null.", __func__);
@@ -215,6 +258,16 @@ Return<void> Gnss::reportLocation(const V1_0::GnssLocation& location) {
    return Void();
}

Return<void> Gnss::reportSvStatus(const GnssSvStatus& svStatus) const {
    std::unique_lock<std::mutex> lock(mMutex);
    if (sGnssCallback == nullptr) {
        ALOGE("%s: sGnssCallback is null.", __func__);
        return Void();
    }
    sGnssCallback->gnssSvStatusCb(svStatus);
    return Void();
}

}  // namespace implementation
}  // namespace V1_1
}  // namespace gnss
+16 −5
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@
#include <atomic>
#include <mutex>
#include <thread>
#include "GnssConfiguration.h"

namespace android {
namespace hardware {
@@ -22,6 +23,11 @@ using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;

using GnssConstellationType = V1_0::GnssConstellationType;
using GnssLocation = V1_0::GnssLocation;
using GnssSvInfo = V1_0::IGnssCallback::GnssSvInfo;
using GnssSvStatus = V1_0::IGnssCallback::GnssSvStatus;

/**
 * Unlike the gnss/1.0/default implementation, which is a shim layer to the legacy gps.h, this
 * default implementation serves as a mock implementation for emulators
@@ -78,14 +84,19 @@ struct Gnss : public IGnss {

    // Methods from ::android::hidl::base::V1_0::IBase follow.
   private:
    Return<V1_0::GnssLocation> getMockLocation();
    Return<void> reportLocation(const V1_0::GnssLocation& location);
    Return<GnssLocation> getMockLocation() const;
    Return<GnssSvStatus> getMockSvStatus() const;
    Return<GnssSvInfo> getSvInfo(int16_t svid, GnssConstellationType type, float cN0DbHz,
                                 float elevationDegress, float azimuthDegress) const;
    Return<void> reportLocation(const GnssLocation&) const;
    Return<void> reportSvStatus(const GnssSvStatus&) const;

    static sp<::android::hardware::gnss::V1_1::IGnssCallback> sGnssCallback;
    static sp<IGnssCallback> sGnssCallback;
    std::atomic<long> mMinIntervalMs;
    sp<GnssConfiguration> mGnssConfiguration;
    std::atomic<bool> mIsActive;
    std::thread mThread;
    std::mutex mMutex;
    std::atomic<long> mMinIntervalMs;
    mutable std::mutex mMutex;
};

}  // namespace implementation
+30 −4
Original line number Diff line number Diff line
#define LOG_TAG "GnssConfiguration"

#include "GnssConfiguration.h"
#include <log/log.h>

namespace android {
namespace hardware {
@@ -43,10 +46,33 @@ Return<bool> GnssConfiguration::setEmergencySuplPdn(bool) {
}

// Methods from ::android::hardware::gnss::V1_1::IGnssConfiguration follow.
Return<bool> GnssConfiguration::setBlacklist(
    const hidl_vec<::android::hardware::gnss::V1_1::IGnssConfiguration::BlacklistedSource>&) {
    // TODO implement
    return bool{};
Return<bool> GnssConfiguration::setBlacklist(const hidl_vec<BlacklistedSource>& sourceList) {
    std::unique_lock<std::recursive_mutex> lock(mMutex);
    mBlacklistedConstellationSet.clear();
    mBlacklistedSourceSet.clear();
    for (auto source : sourceList) {
        if (source.svid == 0) {
            // Wildcard blacklist, i.e., blacklist entire constellation.
            mBlacklistedConstellationSet.insert(source.constellation);
        } else {
            mBlacklistedSourceSet.insert(source);
        }
    }
    return true;
}

Return<bool> GnssConfiguration::isBlacklisted(const GnssSvInfo& gnssSvInfo) const {
    std::unique_lock<std::recursive_mutex> lock(mMutex);
    if (mBlacklistedConstellationSet.find(gnssSvInfo.constellation) !=
        mBlacklistedConstellationSet.end()) {
        return true;
    }
    BlacklistedSource source = {.constellation = gnssSvInfo.constellation, .svid = gnssSvInfo.svid};
    return (mBlacklistedSourceSet.find(source) != mBlacklistedSourceSet.end());
}

std::recursive_mutex& GnssConfiguration::getMutex() const {
    return mMutex;
}

// Methods from ::android::hidl::base::V1_0::IBase follow.
+31 −4
Original line number Diff line number Diff line
#ifndef ANDROID_HARDWARE_GNSS_V1_1_GNSSCONFIGURATION_H
#define ANDROID_HARDWARE_GNSS_V1_1_GNSSCONFIGURATION_H

#include <android/hardware/gnss/1.1/IGnssCallback.h>
#include <android/hardware/gnss/1.1/IGnssConfiguration.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>
#include <mutex>
#include <unordered_set>

namespace android {
namespace hardware {
@@ -19,6 +22,26 @@ using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::sp;

using BlacklistedSource = ::android::hardware::gnss::V1_1::IGnssConfiguration::BlacklistedSource;
using GnssConstellationType = V1_0::GnssConstellationType;
using GnssSvInfo = V1_0::IGnssCallback::GnssSvInfo;

struct BlacklistedSourceHash {
    inline int operator()(const BlacklistedSource& source) const {
        return int(source.constellation) * 1000 + int(source.svid);
    }
};

struct BlacklistedSourceEqual {
    inline bool operator()(const BlacklistedSource& s1, const BlacklistedSource& s2) const {
        return (s1.constellation == s2.constellation) && (s1.svid == s2.svid);
    }
};

using BlacklistedSourceSet =
    std::unordered_set<BlacklistedSource, BlacklistedSourceHash, BlacklistedSourceEqual>;
using BlacklistedConstellationSet = std::unordered_set<GnssConstellationType>;

struct GnssConfiguration : public IGnssConfiguration {
    // Methods from ::android::hardware::gnss::V1_0::IGnssConfiguration follow.
    Return<bool> setSuplEs(bool enabled) override;
@@ -30,11 +53,15 @@ struct GnssConfiguration : public IGnssConfiguration {
    Return<bool> setEmergencySuplPdn(bool enable) override;

    // Methods from ::android::hardware::gnss::V1_1::IGnssConfiguration follow.
    Return<bool> setBlacklist(
        const hidl_vec<::android::hardware::gnss::V1_1::IGnssConfiguration::BlacklistedSource>&
            blacklist) override;
    Return<bool> setBlacklist(const hidl_vec<BlacklistedSource>& blacklist) override;

    Return<bool> isBlacklisted(const GnssSvInfo& gnssSvInfo) const;
    std::recursive_mutex& getMutex() const;

    // Methods from ::android::hidl::base::V1_0::IBase follow.
   private:
    BlacklistedSourceSet mBlacklistedSourceSet;
    BlacklistedConstellationSet mBlacklistedConstellationSet;
    mutable std::recursive_mutex mMutex;
};

}  // namespace implementation
Loading