• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_GNSSBATCHCALLBACK_H
18 #define _ANDROID_SERVER_GNSS_GNSSBATCHCALLBACK_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/IGnssBatching.h>
27 #include <android/hardware/gnss/2.0/IGnssBatching.h>
28 #include <android/hardware/gnss/BnGnssBatchingCallback.h>
29 #include <log/log.h>
30 
31 #include <vector>
32 
33 #include "Utils.h"
34 #include "jni.h"
35 
36 namespace android::gnss {
37 
38 namespace {
39 
40 extern jmethodID method_reportLocationBatch;
41 
42 } // anonymous namespace
43 
44 void GnssBatching_class_init_once(JNIEnv* env, jclass clazz);
45 
46 class GnssBatchingCallbackAidl : public hardware::gnss::BnGnssBatchingCallback {
47 public:
GnssBatchingCallbackAidl()48     GnssBatchingCallbackAidl() {}
49     android::binder::Status gnssLocationBatchCb(
50             const std::vector<android::hardware::gnss::GnssLocation>& locations) override;
51 };
52 
53 class GnssBatchingCallback_V1_0 : public hardware::gnss::V1_0::IGnssBatchingCallback {
54 public:
GnssBatchingCallback_V1_0()55     GnssBatchingCallback_V1_0() {}
56     hardware::Return<void> gnssLocationBatchCb(
57             const hardware::hidl_vec<hardware::gnss::V1_0::GnssLocation>& locations) override;
58 };
59 
60 class GnssBatchingCallback_V2_0 : public hardware::gnss::V2_0::IGnssBatchingCallback {
61 public:
GnssBatchingCallback_V2_0()62     GnssBatchingCallback_V2_0() {}
63     hardware::Return<void> gnssLocationBatchCb(
64             const hardware::hidl_vec<hardware::gnss::V2_0::GnssLocation>& locations) override;
65 };
66 
67 class GnssBatchingCallback {
68 public:
GnssBatchingCallback()69     GnssBatchingCallback() {}
getAidl()70     sp<GnssBatchingCallbackAidl> getAidl() {
71         if (callbackAidl == nullptr) {
72             callbackAidl = sp<GnssBatchingCallbackAidl>::make();
73         }
74         return callbackAidl;
75     }
76 
getV1_0()77     sp<GnssBatchingCallback_V1_0> getV1_0() {
78         if (callbackV1_0 == nullptr) {
79             callbackV1_0 = sp<GnssBatchingCallback_V1_0>::make();
80         }
81         return callbackV1_0;
82     }
83 
getV2_0()84     sp<GnssBatchingCallback_V2_0> getV2_0() {
85         if (callbackV2_0 == nullptr) {
86             callbackV2_0 = sp<GnssBatchingCallback_V2_0>::make();
87         }
88         return callbackV2_0;
89     }
90 
91 private:
92     sp<GnssBatchingCallbackAidl> callbackAidl;
93     sp<GnssBatchingCallback_V1_0> callbackV1_0;
94     sp<GnssBatchingCallback_V2_0> callbackV2_0;
95 };
96 
97 struct GnssBatchingCallbackUtil {
98     template <class T>
99     static hardware::Return<void> gnssLocationBatchCbImpl(const hardware::hidl_vec<T>& locations);
100 
101 private:
102     GnssBatchingCallbackUtil() = delete;
103 };
104 
105 template <class T>
gnssLocationBatchCbImpl(const hardware::hidl_vec<T> & locations)106 hardware::Return<void> GnssBatchingCallbackUtil::gnssLocationBatchCbImpl(
107         const hardware::hidl_vec<T>& locations) {
108     JNIEnv* env = getJniEnv();
109 
110     jobjectArray jLocations = env->NewObjectArray(locations.size(), class_location, nullptr);
111 
112     for (uint16_t i = 0; i < locations.size(); ++i) {
113         jobject jLocation = translateGnssLocation(env, locations[i]);
114         env->SetObjectArrayElement(jLocations, i, jLocation);
115         env->DeleteLocalRef(jLocation);
116     }
117 
118     env->CallVoidMethod(android::getCallbacksObj(), method_reportLocationBatch, jLocations);
119     checkAndClearExceptionFromCallback(env, __FUNCTION__);
120 
121     env->DeleteLocalRef(jLocations);
122 
123     return hardware::Void();
124 }
125 
126 } // namespace android::gnss
127 
128 #endif // _ANDROID_SERVER_GNSS_GNSSBATCHCALLBACK_H