1 /*
2 * Copyright (C) 2021 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_AGNSSCALLBACK_H
18 #define _ANDROID_SERVER_GNSS_AGNSSCALLBACK_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/1.0/IAGnss.h>
27 #include <android/hardware/gnss/2.0/IAGnss.h>
28 #include <android/hardware/gnss/BnAGnssCallback.h>
29 #include <arpa/inet.h>
30 #include <log/log.h>
31
32 #include <vector>
33
34 #include "Utils.h"
35 #include "jni.h"
36
37 namespace android::gnss {
38
39 namespace {
40
41 extern jmethodID method_reportAGpsStatus;
42
43 }
44
45 void AGnss_class_init_once(JNIEnv* env, jclass clazz);
46
47 /*
48 * AGnssCallbackAidl class implements the callback methods required by the
49 * android::hardware::gnss::IAGnss interface.
50 */
51 class AGnssCallbackAidl : public android::hardware::gnss::BnAGnssCallback {
52 public:
53 binder::Status agnssStatusCb(AGnssType type, AGnssStatusValue status) override;
54 };
55
56 /*
57 * AGnssCallback_V1_0 implements callback methods required by the IAGnssCallback 1.0 interface.
58 */
59 class AGnssCallback_V1_0 : public android::hardware::gnss::V1_0::IAGnssCallback {
60 public:
61 // Methods from ::android::hardware::gps::V1_0::IAGnssCallback follow.
62 hardware::Return<void> agnssStatusIpV6Cb(
63 const android::hardware::gnss::V1_0::IAGnssCallback::AGnssStatusIpV6& agps_status)
64 override;
65
66 hardware::Return<void> agnssStatusIpV4Cb(
67 const android::hardware::gnss::V1_0::IAGnssCallback::AGnssStatusIpV4& agps_status)
68 override;
69
70 private:
71 jbyteArray convertToIpV4(uint32_t ip);
72 };
73
74 /*
75 * AGnssCallback_V2_0 implements callback methods required by the IAGnssCallback 2.0 interface.
76 */
77 class AGnssCallback_V2_0 : public android::hardware::gnss::V2_0::IAGnssCallback {
78 public:
79 // Methods from ::android::hardware::gps::V2_0::IAGnssCallback follow.
80 hardware::Return<void> agnssStatusCb(
81 android::hardware::gnss::V2_0::IAGnssCallback::AGnssType type,
82 android::hardware::gnss::V2_0::IAGnssCallback::AGnssStatusValue status) override;
83 };
84
85 class AGnssCallback {
86 public:
AGnssCallback()87 AGnssCallback() {}
getAidl()88 sp<AGnssCallbackAidl> getAidl() {
89 if (callbackAidl == nullptr) {
90 callbackAidl = sp<AGnssCallbackAidl>::make();
91 }
92 return callbackAidl;
93 }
94
getV1_0()95 sp<AGnssCallback_V1_0> getV1_0() {
96 if (callbackV1_0 == nullptr) {
97 callbackV1_0 = sp<AGnssCallback_V1_0>::make();
98 }
99 return callbackV1_0;
100 }
101
getV2_0()102 sp<AGnssCallback_V2_0> getV2_0() {
103 if (callbackV2_0 == nullptr) {
104 callbackV2_0 = sp<AGnssCallback_V2_0>::make();
105 }
106 return callbackV2_0;
107 }
108
109 private:
110 sp<AGnssCallbackAidl> callbackAidl;
111 sp<AGnssCallback_V1_0> callbackV1_0;
112 sp<AGnssCallback_V2_0> callbackV2_0;
113 };
114
115 struct AGnssCallbackUtil {
116 template <class T, class U>
117 static void agnssStatusCbImpl(const T& type, const U& status);
118
119 private:
120 AGnssCallbackUtil() = delete;
121 };
122
123 template <class T, class U>
agnssStatusCbImpl(const T & type,const U & status)124 void AGnssCallbackUtil::agnssStatusCbImpl(const T& type, const U& status) {
125 ALOGD("%s. type: %d, status:%d", __func__, static_cast<int32_t>(type),
126 static_cast<int32_t>(status));
127 JNIEnv* env = getJniEnv();
128 env->CallVoidMethod(mCallbacksObj, method_reportAGpsStatus, type, status, nullptr);
129 checkAndClearExceptionFromCallback(env, __FUNCTION__);
130 }
131
132 } // namespace android::gnss
133
134 #endif // _ANDROID_SERVER_GNSS_AGNSSCALLBACK_H