• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 gnssStatusCb(const GnssStatusValue status) override;
65     binder::Status gnssSvStatusCb(const std::vector<GnssSvInfo>& svInfoList) override;
66     binder::Status gnssLocationCb(const hardware::gnss::GnssLocation& location) override;
67     binder::Status gnssNmeaCb(const int64_t timestamp, const std::string& nmea) override;
68     binder::Status gnssAcquireWakelockCb() override;
69     binder::Status gnssReleaseWakelockCb() override;
70     binder::Status gnssSetSystemInfoCb(const GnssSystemInfo& info) override;
71     binder::Status gnssRequestTimeCb() override;
72     binder::Status gnssRequestLocationCb(const bool independentFromGnss,
73                                          const bool isUserEmergency) override;
74 };
75 
76 /*
77  * GnssCallbackHidl class implements the callback methods for HIDL IGnssCallback interface.
78  */
79 struct GnssCallbackHidl : public hardware::gnss::V2_1::IGnssCallback {
80     hardware::Return<void> gnssLocationCb(
81             const hardware::gnss::V1_0::GnssLocation& location) override;
82     hardware::Return<void> gnssStatusCb(
83             const hardware::gnss::V1_0::IGnssCallback::GnssStatusValue status) override;
gnssSvStatusCbGnssCallbackHidl84     hardware::Return<void> gnssSvStatusCb(
85             const hardware::gnss::V1_0::IGnssCallback::GnssSvStatus& svStatus) override {
86         return gnssSvStatusCbImpl<hardware::gnss::V1_0::IGnssCallback::GnssSvStatus,
87                                   hardware::gnss::V1_0::IGnssCallback::GnssSvInfo>(svStatus);
88     }
89     hardware::Return<void> gnssNmeaCb(int64_t timestamp,
90                                       const hardware::hidl_string& nmea) override;
91     hardware::Return<void> gnssSetCapabilitesCb(uint32_t capabilities) override;
92     hardware::Return<void> gnssAcquireWakelockCb() override;
93     hardware::Return<void> gnssReleaseWakelockCb() override;
94     hardware::Return<void> gnssRequestTimeCb() override;
95     hardware::Return<void> gnssRequestLocationCb(const bool independentFromGnss) override;
96 
97     hardware::Return<void> gnssSetSystemInfoCb(
98             const hardware::gnss::V1_0::IGnssCallback::GnssSystemInfo& info) override;
99 
100     // New in 1.1
101     hardware::Return<void> gnssNameCb(const hardware::hidl_string& name) override;
102 
103     // New in 2.0
104     hardware::Return<void> gnssRequestLocationCb_2_0(const bool independentFromGnss,
105                                                      const bool isUserEmergency) override;
106     hardware::Return<void> gnssSetCapabilitiesCb_2_0(uint32_t capabilities) override;
107     hardware::Return<void> gnssLocationCb_2_0(
108             const hardware::gnss::V2_0::GnssLocation& location) override;
gnssSvStatusCb_2_0GnssCallbackHidl109     hardware::Return<void> gnssSvStatusCb_2_0(
110             const hardware::hidl_vec<hardware::gnss::V2_0::IGnssCallback::GnssSvInfo>& svInfoList)
111             override {
112         return gnssSvStatusCbImpl<
113                 hardware::hidl_vec<hardware::gnss::V2_0::IGnssCallback::GnssSvInfo>,
114                 hardware::gnss::V1_0::IGnssCallback::GnssSvInfo>(svInfoList);
115     }
116 
117     // New in 2.1
gnssSvStatusCb_2_1GnssCallbackHidl118     hardware::Return<void> gnssSvStatusCb_2_1(
119             const hardware::hidl_vec<hardware::gnss::V2_1::IGnssCallback::GnssSvInfo>& svInfoList)
120             override {
121         return gnssSvStatusCbImpl<
122                 hardware::hidl_vec<hardware::gnss::V2_1::IGnssCallback::GnssSvInfo>,
123                 hardware::gnss::V1_0::IGnssCallback::GnssSvInfo>(svInfoList);
124     }
125     hardware::Return<void> gnssSetCapabilitiesCb_2_1(uint32_t capabilities) override;
126 
127     // TODO: Reconsider allocation cost vs threadsafety on these statics
128     static const char* sNmeaString;
129     static size_t sNmeaStringLength;
130 
131     template <class T>
132     static hardware::Return<void> gnssLocationCbImpl(const T& location);
133 
134     template <class T_list, class T_sv_info>
135     static hardware::Return<void> gnssSvStatusCbImpl(const T_list& svStatus);
136 
137 private:
138     template <class T>
getHasBasebandCn0DbHzFlagGnssCallbackHidl139     static uint32_t getHasBasebandCn0DbHzFlag(const T& svStatus) {
140         return 0;
141     }
142 
143     template <class T>
getBasebandCn0DbHzGnssCallbackHidl144     static double getBasebandCn0DbHz(const T& svStatus, size_t i) {
145         return 0.0;
146     }
147 
148     template <class T>
getGnssSvInfoListSizeGnssCallbackHidl149     static uint32_t getGnssSvInfoListSize(const T& svInfoList) {
150         return svInfoList.size();
151     }
152 
getGnssSvInfoOfIndexGnssCallbackHidl153     static const hardware::gnss::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex(
154             const std::vector<hardware::gnss::IGnssCallback::GnssSvInfo>& svInfoList, size_t i) {
155         return svInfoList[i];
156     }
157 
getGnssSvInfoOfIndexGnssCallbackHidl158     static const hardware::gnss::V1_0::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex(
159             const hardware::gnss::V1_0::IGnssCallback::GnssSvStatus& svStatus, size_t i) {
160         return svStatus.gnssSvList.data()[i];
161     }
162 
getGnssSvInfoOfIndexGnssCallbackHidl163     static const hardware::gnss::V1_0::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex(
164             const hardware::hidl_vec<hardware::gnss::V2_0::IGnssCallback::GnssSvInfo>& svInfoList,
165             size_t i) {
166         return svInfoList[i].v1_0;
167     }
168 
getGnssSvInfoOfIndexGnssCallbackHidl169     static const hardware::gnss::V1_0::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex(
170             const hardware::hidl_vec<hardware::gnss::V2_1::IGnssCallback::GnssSvInfo>& svInfoList,
171             size_t i) {
172         return svInfoList[i].v2_0.v1_0;
173     }
174 
175     template <class T>
getConstellationTypeGnssCallbackHidl176     static uint32_t getConstellationType(const T& svInfoList, size_t i) {
177         return static_cast<uint32_t>(svInfoList[i].constellation);
178     }
179 };
180 
181 } // namespace android::gnss
182 
183 #endif // _ANDROID_SERVER_GNSS_GNSSCALLBACK_H