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