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: GnssCallbackAidl(int version)63 GnssCallbackAidl(int version) : interfaceVersion(version){}; 64 binder::Status gnssSetCapabilitiesCb(const int capabilities) override; 65 binder::Status gnssSetSignalTypeCapabilitiesCb( 66 const std::vector<android::hardware::gnss::GnssSignalType>& signalTypes) override; 67 binder::Status gnssStatusCb(const GnssStatusValue status) override; 68 binder::Status gnssSvStatusCb(const std::vector<GnssSvInfo>& svInfoList) override; 69 binder::Status gnssLocationCb(const hardware::gnss::GnssLocation& location) override; 70 binder::Status gnssNmeaCb(const int64_t timestamp, const std::string& nmea) override; 71 binder::Status gnssAcquireWakelockCb() override; 72 binder::Status gnssReleaseWakelockCb() override; 73 binder::Status gnssSetSystemInfoCb(const GnssSystemInfo& info) override; 74 binder::Status gnssRequestTimeCb() override; 75 binder::Status gnssRequestLocationCb(const bool independentFromGnss, 76 const bool isUserEmergency) override; 77 78 private: 79 const int interfaceVersion; 80 }; 81 82 /* 83 * GnssCallbackHidl class implements the callback methods for HIDL IGnssCallback interface. 84 */ 85 struct GnssCallbackHidl : public hardware::gnss::V2_1::IGnssCallback { 86 hardware::Return<void> gnssLocationCb( 87 const hardware::gnss::V1_0::GnssLocation& location) override; 88 hardware::Return<void> gnssStatusCb( 89 const hardware::gnss::V1_0::IGnssCallback::GnssStatusValue status) override; gnssSvStatusCbGnssCallbackHidl90 hardware::Return<void> gnssSvStatusCb( 91 const hardware::gnss::V1_0::IGnssCallback::GnssSvStatus& svStatus) override { 92 return gnssSvStatusCbImpl<hardware::gnss::V1_0::IGnssCallback::GnssSvStatus, 93 hardware::gnss::V1_0::IGnssCallback::GnssSvInfo>(svStatus); 94 } 95 hardware::Return<void> gnssNmeaCb(int64_t timestamp, 96 const hardware::hidl_string& nmea) override; 97 hardware::Return<void> gnssSetCapabilitesCb(uint32_t capabilities) override; 98 hardware::Return<void> gnssAcquireWakelockCb() override; 99 hardware::Return<void> gnssReleaseWakelockCb() override; 100 hardware::Return<void> gnssRequestTimeCb() override; 101 hardware::Return<void> gnssRequestLocationCb(const bool independentFromGnss) override; 102 103 hardware::Return<void> gnssSetSystemInfoCb( 104 const hardware::gnss::V1_0::IGnssCallback::GnssSystemInfo& info) override; 105 106 // New in 1.1 107 hardware::Return<void> gnssNameCb(const hardware::hidl_string& name) override; 108 109 // New in 2.0 110 hardware::Return<void> gnssRequestLocationCb_2_0(const bool independentFromGnss, 111 const bool isUserEmergency) override; 112 hardware::Return<void> gnssSetCapabilitiesCb_2_0(uint32_t capabilities) override; 113 hardware::Return<void> gnssLocationCb_2_0( 114 const hardware::gnss::V2_0::GnssLocation& location) override; gnssSvStatusCb_2_0GnssCallbackHidl115 hardware::Return<void> gnssSvStatusCb_2_0( 116 const hardware::hidl_vec<hardware::gnss::V2_0::IGnssCallback::GnssSvInfo>& svInfoList) 117 override { 118 return gnssSvStatusCbImpl< 119 hardware::hidl_vec<hardware::gnss::V2_0::IGnssCallback::GnssSvInfo>, 120 hardware::gnss::V1_0::IGnssCallback::GnssSvInfo>(svInfoList); 121 } 122 123 // New in 2.1 gnssSvStatusCb_2_1GnssCallbackHidl124 hardware::Return<void> gnssSvStatusCb_2_1( 125 const hardware::hidl_vec<hardware::gnss::V2_1::IGnssCallback::GnssSvInfo>& svInfoList) 126 override { 127 return gnssSvStatusCbImpl< 128 hardware::hidl_vec<hardware::gnss::V2_1::IGnssCallback::GnssSvInfo>, 129 hardware::gnss::V1_0::IGnssCallback::GnssSvInfo>(svInfoList); 130 } 131 hardware::Return<void> gnssSetCapabilitiesCb_2_1(uint32_t capabilities) override; 132 133 // TODO: Reconsider allocation cost vs threadsafety on these statics 134 static const char* sNmeaString; 135 static size_t sNmeaStringLength; 136 137 template <class T> 138 static hardware::Return<void> gnssLocationCbImpl(const T& location); 139 140 template <class T_list, class T_sv_info> 141 static hardware::Return<void> gnssSvStatusCbImpl(const T_list& svStatus); 142 143 private: 144 template <class T> getHasBasebandCn0DbHzFlagGnssCallbackHidl145 static uint32_t getHasBasebandCn0DbHzFlag(const T& svStatus) { 146 return 0; 147 } 148 149 template <class T> getBasebandCn0DbHzGnssCallbackHidl150 static double getBasebandCn0DbHz(const T& svStatus, size_t i) { 151 return 0.0; 152 } 153 154 template <class T> getGnssSvInfoListSizeGnssCallbackHidl155 static uint32_t getGnssSvInfoListSize(const T& svInfoList) { 156 return svInfoList.size(); 157 } 158 getGnssSvInfoOfIndexGnssCallbackHidl159 static const hardware::gnss::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex( 160 const std::vector<hardware::gnss::IGnssCallback::GnssSvInfo>& svInfoList, size_t i) { 161 return svInfoList[i]; 162 } 163 getGnssSvInfoOfIndexGnssCallbackHidl164 static const hardware::gnss::V1_0::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex( 165 const hardware::gnss::V1_0::IGnssCallback::GnssSvStatus& svStatus, size_t i) { 166 return svStatus.gnssSvList.data()[i]; 167 } 168 getGnssSvInfoOfIndexGnssCallbackHidl169 static const hardware::gnss::V1_0::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex( 170 const hardware::hidl_vec<hardware::gnss::V2_0::IGnssCallback::GnssSvInfo>& svInfoList, 171 size_t i) { 172 return svInfoList[i].v1_0; 173 } 174 getGnssSvInfoOfIndexGnssCallbackHidl175 static const hardware::gnss::V1_0::IGnssCallback::GnssSvInfo& getGnssSvInfoOfIndex( 176 const hardware::hidl_vec<hardware::gnss::V2_1::IGnssCallback::GnssSvInfo>& svInfoList, 177 size_t i) { 178 return svInfoList[i].v2_0.v1_0; 179 } 180 181 template <class T> getConstellationTypeGnssCallbackHidl182 static uint32_t getConstellationType(const T& svInfoList, size_t i) { 183 return static_cast<uint32_t>(svInfoList[i].constellation); 184 } 185 }; 186 187 } // namespace android::gnss 188 189 #endif // _ANDROID_SERVER_GNSS_GNSSCALLBACK_H 190