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