rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #ifndef android_hardware_gnss_V1_1_Gnss_H_ |
| 18 | #define android_hardware_gnss_V1_1_Gnss_H_ |
| 19 | |
| 20 | #include <AGnss.h> |
| 21 | #include <AGnssRil.h> |
| 22 | #include <GnssBatching.h> |
| 23 | #include <GnssConfiguration.h> |
| 24 | #include <GnssDebug.h> |
| 25 | #include <GnssGeofencing.h> |
| 26 | #include <GnssMeasurement.h> |
| 27 | #include <GnssNavigationMessage.h> |
| 28 | #include <GnssNi.h> |
| 29 | #include <GnssXtra.h> |
| 30 | |
| 31 | #include <ThreadCreationWrapper.h> |
| 32 | #include <android/hardware/gnss/1.1/IGnss.h> |
| 33 | #include <hardware/fused_location.h> |
| 34 | #include <hidl/Status.h> |
| 35 | #include "hardware/gps_mtk.h" |
| 36 | |
| 37 | #include <semaphore.h> |
| 38 | |
| 39 | namespace android { |
| 40 | namespace hardware { |
| 41 | namespace gnss { |
| 42 | namespace V1_1 { |
| 43 | namespace implementation { |
| 44 | |
| 45 | using ::android::hardware::Return; |
| 46 | using ::android::hardware::Void; |
| 47 | using ::android::hardware::hidl_vec; |
| 48 | using ::android::hardware::hidl_string; |
| 49 | using ::android::sp; |
| 50 | |
| 51 | using LegacyGnssSystemInfo = ::GnssSystemInfo; |
| 52 | using GnssConstellationType = V1_0::GnssConstellationType; |
| 53 | using GnssLocation = V1_0::GnssLocation; |
| 54 | |
| 55 | /* |
| 56 | * Represents the standard GNSS interface. Also contains wrapper methods to allow methods from |
| 57 | * IGnssCallback interface to be passed into the conventional implementation of the GNSS HAL. |
| 58 | */ |
| 59 | class Gnss : public IGnss { |
| 60 | public: |
| 61 | Gnss(gps_device_t_ext* gnss_device); |
| 62 | ~Gnss(); |
| 63 | |
| 64 | // Methods from ::android::hardware::gnss::V1_0::IGnss follow. |
| 65 | Return<bool> setCallback( |
| 66 | const sp<V1_0::IGnssCallback>& callback) override; |
| 67 | Return<bool> start() override; |
| 68 | Return<bool> stop() override; |
| 69 | Return<void> cleanup() override; |
| 70 | Return<bool> injectTime(int64_t timeMs, int64_t timeReferenceMs, |
| 71 | int32_t uncertaintyMs) override; |
| 72 | Return<bool> injectLocation(double latitudeDegrees, double longitudeDegrees, |
| 73 | float accuracyMeters) override; |
| 74 | Return<void> deleteAidingData(V1_0::IGnss::GnssAidingData aidingDataFlags) override; |
| 75 | Return<bool> setPositionMode( |
| 76 | V1_0::IGnss::GnssPositionMode mode, |
| 77 | V1_0::IGnss::GnssPositionRecurrence recurrence, |
| 78 | uint32_t minIntervalMs, uint32_t preferredAccuracyMeters, |
| 79 | uint32_t preferredTimeMs) override; |
| 80 | Return<sp<V1_0::IAGnssRil>> getExtensionAGnssRil() override; |
| 81 | Return<sp<V1_0::IGnssGeofencing>> getExtensionGnssGeofencing() override; |
| 82 | Return<sp<V1_0::IAGnss>> getExtensionAGnss() override; |
| 83 | Return<sp<V1_0::IGnssNi>> getExtensionGnssNi() override; |
| 84 | Return<sp<V1_0::IGnssMeasurement>> getExtensionGnssMeasurement() override; |
| 85 | Return<sp<V1_0::IGnssNavigationMessage>> |
| 86 | getExtensionGnssNavigationMessage() override; |
| 87 | Return<sp<V1_0::IGnssXtra>> getExtensionXtra() override; |
| 88 | Return<sp<V1_0::IGnssConfiguration>> getExtensionGnssConfiguration() override; |
| 89 | Return<sp<V1_0::IGnssDebug>> getExtensionGnssDebug() override; |
| 90 | Return<sp<V1_0::IGnssBatching>> getExtensionGnssBatching() override; |
| 91 | |
| 92 | // Methods from ::android::hardware::gnss::V1_1::IGnss follow. |
| 93 | Return<bool> setCallback_1_1( |
| 94 | const sp<V1_1::IGnssCallback>& callback) override; |
| 95 | Return<bool> setPositionMode_1_1( |
| 96 | V1_0::IGnss::GnssPositionMode mode, |
| 97 | V1_0::IGnss::GnssPositionRecurrence recurrence, |
| 98 | uint32_t minIntervalMs, uint32_t preferredAccuracyMeters, uint32_t preferredTimeMs, |
| 99 | bool lowPowerMode) override; |
| 100 | Return<sp<V1_1::IGnssConfiguration>> |
| 101 | getExtensionGnssConfiguration_1_1() override; |
| 102 | Return<sp<V1_1::IGnssMeasurement>> getExtensionGnssMeasurement_1_1() override; |
| 103 | Return<bool> injectBestLocation(const V1_0::GnssLocation& location) override; |
| 104 | |
| 105 | |
| 106 | /* |
| 107 | * Callback methods to be passed into the conventional GNSS HAL by the default |
| 108 | * implementation. These methods are not part of the IGnss base class. |
| 109 | */ |
| 110 | static void locationCb(GpsLocation_ext* location); |
| 111 | static void statusCb(GpsStatus* gnss_status); |
| 112 | static void nmeaCb(GpsUtcTime timestamp, const char* nmea, int length); |
| 113 | static void setCapabilitiesCb(uint32_t capabilities); |
| 114 | static void acquireWakelockCb(); |
| 115 | static void releaseWakelockCb(); |
| 116 | static void requestUtcTimeCb(); |
| 117 | static pthread_t createThreadCb(const char* name, void (*start)(void*), void* arg); |
| 118 | static void gnssSvStatusCb(GnssSvStatus_ext* status); |
| 119 | /* |
| 120 | * Deprecated callback added for backward compatibility to devices that do |
| 121 | * not support GnssSvStatus. |
| 122 | */ |
| 123 | static void gpsSvStatusCb(GpsSvStatus* status); |
| 124 | static void setSystemInfoCb(const LegacyGnssSystemInfo* info); |
| 125 | |
| 126 | /* |
| 127 | * Wakelock consolidation, only needed for dual use of a gps.h & fused_location.h HAL |
| 128 | * |
| 129 | * Ensures that if the last call from either legacy .h was to acquire a wakelock, that a |
| 130 | * wakelock is held. Otherwise releases it. |
| 131 | */ |
| 132 | static void acquireWakelockFused(); |
| 133 | static void releaseWakelockFused(); |
| 134 | |
| 135 | static void setNameCb(const char* name, int length); |
| 136 | static void requestLocationCb(bool independentFromGnss); |
| 137 | |
| 138 | /* |
| 139 | * Holds function pointers to the callback methods. |
| 140 | */ |
| 141 | static GpsCallbacks_ext sGnssCb; |
| 142 | |
| 143 | private: |
| 144 | /* |
| 145 | * For handling system-server death while GNSS service lives on. |
| 146 | */ |
| 147 | class GnssHidlDeathRecipient : public hidl_death_recipient { |
| 148 | public: |
| 149 | GnssHidlDeathRecipient(const sp<Gnss> gnss) : mGnss(gnss) { |
| 150 | } |
| 151 | |
| 152 | virtual void serviceDied(uint64_t /*cookie*/, |
| 153 | const wp<::android::hidl::base::V1_0::IBase>& /*who*/) { |
| 154 | mGnss->handleHidlDeath(); |
| 155 | } |
| 156 | private: |
| 157 | sp<Gnss> mGnss; |
| 158 | }; |
| 159 | |
| 160 | // for wakelock consolidation, see above |
| 161 | static void acquireWakelockGnss(); |
| 162 | static void releaseWakelockGnss(); |
| 163 | static void updateWakelock(); |
| 164 | static bool sWakelockHeldGnss; |
| 165 | static bool sWakelockHeldFused; |
| 166 | |
| 167 | /* |
| 168 | * Cleanup for death notification |
| 169 | */ |
| 170 | void handleHidlDeath(); |
| 171 | |
| 172 | sp<V1_0::implementation::GnssXtra> mGnssXtraIface = nullptr; |
| 173 | sp<V1_0::implementation::AGnssRil> mGnssRil = nullptr; |
| 174 | sp<V1_0::implementation::GnssGeofencing> mGnssGeofencingIface = nullptr; |
| 175 | sp<V1_0::implementation::AGnss> mAGnssIface = nullptr; |
| 176 | sp<V1_0::implementation::GnssNi> mGnssNi = nullptr; |
| 177 | sp<V1_1::implementation::GnssMeasurement> mGnssMeasurement = nullptr; |
| 178 | sp<V1_0::implementation::GnssNavigationMessage> mGnssNavigationMessage = nullptr; |
| 179 | sp<V1_0::implementation::GnssDebug> mGnssDebug = nullptr; |
| 180 | sp<V1_1::implementation::GnssConfiguration> mGnssConfig = nullptr; |
| 181 | sp<V1_0::implementation::GnssBatching> mGnssBatching = nullptr; |
| 182 | |
| 183 | ///M: add semphore protection |
| 184 | static sem_t sSem; |
| 185 | |
| 186 | sp<GnssHidlDeathRecipient> mDeathRecipient; |
| 187 | const GpsInterface_ext* mGnssIface = nullptr; |
| 188 | static sp<V1_1::IGnssCallback> sGnssCbIface; |
| 189 | static std::vector<std::unique_ptr<ThreadFuncArgs>> sThreadFuncArgsList; |
| 190 | static bool sInterfaceExists; |
| 191 | |
| 192 | // Values saved for resend |
| 193 | static uint32_t sCapabilitiesCached; |
| 194 | static uint16_t sYearOfHwCached; |
| 195 | }; |
| 196 | |
| 197 | extern "C" IGnss* HIDL_FETCH_IGnss(const char* name); |
| 198 | |
| 199 | } // namespace implementation |
| 200 | } // namespace V1_1 |
| 201 | } // namespace gnss |
| 202 | } // namespace hardware |
| 203 | } // namespace android |
| 204 | |
| 205 | #endif // android_hardware_gnss_V1_1_Gnss_H_ |