1 /*
2  * Copyright (C) 2022 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_SERVER_GNSS_GNSSCALLBACK_H
18 #define _ANDROID_SERVER_GNSS_GNSSCALLBACK_H
19 
20 #pragma once
21 
22 #ifndef LOG_TAG
23 #error LOG_TAG must be defined before including this file.
24 #endif
25 
26 #include <android/hardware/gnss/2.1/IGnss.h>
27 #include <android/hardware/gnss/BnGnssCallback.h>
28 #include <log/log.h>
29 
30 #include <vector>
31 
32 #include "Utils.h"
33 #include "jni.h"
34 
35 namespace android::gnss {
36 
37 namespace {
38 
39 extern jmethodID method_reportLocation;
40 extern jmethodID method_reportStatus;
41 extern jmethodID method_reportSvStatus;
42 extern jmethodID method_reportNmea;
43 extern jmethodID method_setTopHalCapabilities;
44 extern jmethodID method_setGnssYearOfHardware;
45 extern jmethodID method_setGnssHardwareModelName;
46 extern jmethodID method_requestLocation;
47 extern jmethodID method_requestUtcTime;
48 
49 } // anonymous namespace
50 
51 extern bool isSvStatusRegistered;
52 extern bool isNmeaRegistered;
53 
54 extern jmethodID method_reportGnssServiceDied;
55 
56 void Gnss_class_init_once(JNIEnv* env, jclass& clazz);
57 
58 /*
59  * GnssCallbackAidl class implements the callback methods for AIDL IGnssCallback interface.
60  */
61 class GnssCallbackAidl : public hardware::gnss::BnGnssCallback {
62 public:
63     binder::Status gnssSetCapabilitiesCb(const int capabilities) override;
64     binder::Status gnssSetSignalTypeCapabilitiesCb(
65             const std::vector<android::hardware::gnss::GnssSignalType>& signalTypes) override;
66     binder::Status gnssStatusCb(const GnssStatusValue status) override;
67     binder::Status gnssSvStatusCb(const std::vector<GnssSvInfo>& svInfoList) override;
68     binder::Status gnssLocationCb(const hardware::gnss::GnssLocation& location) override;
69     binder::Status gnssNmeaCb(const int64_t timestamp, const std::string& nmea) override;
70     binder::Status gnssAcquireWakelockCb() override;
71     binder::Status gnssReleaseWakelockCb() override;
72     binder::Status gnssSetSystemInfoCb(const GnssSystemInfo& info) override;
73     binder::Status gnssRequestTimeCb() override;
74     binder::Status gnssRequestLocationCb(const bool independentFromGnss,
75                                          const bool isUserEmergency) override;
76 };
77 
78 /*
79  * GnssCallbackHidl class implements the callback methods for HIDL IGnssCallback interface.
80  */
81 struct GnssCallbackHidl : public hardware::gnss::V2_1::IGnssCallback {
82     hardware::Return<void> gnssLocationCb(
83             const hardware::gnss::V1_0::GnssLocation& location) override;
84     hardware::Return<void> gnssStatusCb(
85             const hardware::gnss::V1_0::IGnssCallback::GnssStatusValue status) override;
gnssSvStatusCbGnssCallbackHidl86     hardware::Return<void> gnssSvStatusCb(
87             const hardware::gnss::V1_0::IGnssCallback::GnssSvStatus& svStatus) override {
88         return gnssSvStatusCbImpl<hardware::gnss::V1_0::IGnssCallback::GnssSvStatus,
89                                   hardware::gnss::V1_0::IGnssCallback::GnssSvInfo>(svStatus);
90     }
91     hardware::Return<void> gnssNmeaCb(int64_t timestamp,
92                                       const hardware::hidl_string& nmea) override;
93     hardware::Return<void> gnssSetCapabilitesCb(uint32_t capabilities) override;
94     hardware::Return<void> gnssAcquireWakelockCb() override;
95     hardware::Return<void> gnssReleaseWakelockCb() override;
96     hardware::Return<void> gnssRequestTimeCb() override;
97     hardware::Return<void> gnssRequestLocationCb(const bool independentFromGnss) override;
98 
99     hardware::Return<void> gnssSetSystemInfoCb(
100             const hardware::gnss::V1_0::IGnssCallback::GnssSystemInfo& info) override;
101 
102     // New in 1.1
103     hardware::Return<void> gnssNameCb(const hardware::hidl_string& name) override;
104 
105     // New in 2.0
106     hardware::Return<void> gnssRequestLocationCb_2_0(const bool independentFromGnss,
107                                                      const bool isUserEmergency) override;
108     hardware::Return<void> gnssSetCapabilitiesCb_2_0(uint32_t capabilities) override;
109     hardware::Return<void> gnssLocationCb_2_0(
110             const hardware::gnss::V2_0::GnssLocation& location) override;
gnssSvStatusCb_2_0GnssCallbackHidl111     hardware::Return<void> gnssSvStatusCb_2_0(
112             const hardware::hidl_vec<hardware::gnss::V2_0::IGnssCallback::GnssSvInfo>& svInfoList)
113             override {
114         return gnssSvStatusCbImpl<
115                 hardware::hidl_vec<hardware::gnss::V2_0::IGnssCallback::GnssSvInfo>,
116                 hardware::gnss::V1_0::IGnssCallback::GnssSvInfo>(svInfoList);
117     }
118 
119     // New in 2.1
gnssSvStatusCb_2_1GnssCallbackHidl120     hardware::Return<void> gnssSvStatusCb_2_1(
121             const hardware::hidl_vec<hardware::gnss::V2_1::IGnssCallback::GnssSvInfo>& svInfoList)
122             override {
123         return gnssSvStatusCbImpl<
124                 hardware::hidl_vec<hardware::gnss::V2_1::IGnssCallback::GnssSvInfo>,
125                 hardware::gnss::V1_0::IGnssCallback::GnssSvInfo>(svInfoList);
126     }
127     hardware::Return<void> gnssSetCapabilitiesCb_2_1(uint32_t capabilities) override;
128 
129     // TODO: Reconsider allocation cost vs threadsafety on these statics
130     static const char* sNmeaString;
131     static size_t sNmeaStringLength;
132 
133     template <class T>
134     static hardware::Return<void> gnssLocationCbImpl(const T& location);
135 
136     template <class T_list, class T_sv_info>
137     static hardware::Return<void> gnssSvStatusCbImpl(const T_list& svStatus);
138 
139 private:
140     template <class T>
getHasBasebandCn0DbHzFlagGnssCallbackHidl141     static uint32_t getHasBasebandCn0DbHzFlag(const T& svStatus) {
142         return 0;
143     }
144 
145     template <class T>
getBasebandCn0DbHzGnssCallbackHidl146     static double getBasebandCn0DbHz(const T& svStatus, size_t i) {
147         return 0.0;
148     }
149 
150     template <class T>
getGnssSvInfoListSizeGnssCallbackHidl151     static uint32_t getGnssSvInfoListSize(const T& svInfoList) {
152         return svInfoList.size();
153     }
154 
getGnssSvInfoOfIndexGnssCallbackHidl155     static const hardware::gnss::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex(
156             const std::vector<hardware::gnss::IGnssCallback::GnssSvInfo>& svInfoList, size_t i) {
157         return svInfoList[i];
158     }
159 
getGnssSvInfoOfIndexGnssCallbackHidl160     static const hardware::gnss::V1_0::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex(
161             const hardware::gnss::V1_0::IGnssCallback::GnssSvStatus& svStatus, size_t i) {
162         return svStatus.gnssSvList.data()[i];
163     }
164 
getGnssSvInfoOfIndexGnssCallbackHidl165     static const hardware::gnss::V1_0::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex(
166             const hardware::hidl_vec<hardware::gnss::V2_0::IGnssCallback::GnssSvInfo>& svInfoList,
167             size_t i) {
168         return svInfoList[i].v1_0;
169     }
170 
getGnssSvInfoOfIndexGnssCallbackHidl171     static const hardware::gnss::V1_0::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex(
172             const hardware::hidl_vec<hardware::gnss::V2_1::IGnssCallback::GnssSvInfo>& svInfoList,
173             size_t i) {
174         return svInfoList[i].v2_0.v1_0;
175     }
176 
177     template <class T>
getConstellationTypeGnssCallbackHidl178     static uint32_t getConstellationType(const T& svInfoList, size_t i) {
179         return static_cast<uint32_t>(svInfoList[i].constellation);
180     }
181 };
182 
183 } // namespace android::gnss
184 
185 #endif // _ANDROID_SERVER_GNSS_GNSSCALLBACK_H
186