• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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 // Define LOG_TAG before <log/log.h> to overwrite the default value.
18 #define LOG_TAG "GnssMeasurementJni"
19 
20 #include "GnssMeasurement.h"
21 #include "Utils.h"
22 
23 using android::hardware::hidl_vec;
24 using android::hardware::Return;
25 
26 using IGnssMeasurementInterface = android::hardware::gnss::IGnssMeasurementInterface;
27 using IGnssMeasurement_V1_0 = android::hardware::gnss::V1_0::IGnssMeasurement;
28 using IGnssMeasurement_V1_1 = android::hardware::gnss::V1_1::IGnssMeasurement;
29 using IGnssMeasurement_V2_0 = android::hardware::gnss::V2_0::IGnssMeasurement;
30 using IGnssMeasurement_V2_1 = android::hardware::gnss::V2_1::IGnssMeasurement;
31 
32 namespace {
checkGnssMeasurementStatus(const IGnssMeasurement_V1_0::GnssMeasurementStatus & status)33 jboolean checkGnssMeasurementStatus(const IGnssMeasurement_V1_0::GnssMeasurementStatus& status) {
34     if (status != IGnssMeasurement_V1_0::GnssMeasurementStatus::SUCCESS) {
35         ALOGE("An error has been found on GnssMeasurementInterface::init, status=%d",
36               static_cast<int32_t>(status));
37         return JNI_FALSE;
38     } else {
39         ALOGD("gnss measurement infc has been enabled");
40         return JNI_TRUE;
41     }
42 }
43 } // anonymous namespace
44 
45 namespace android::gnss {
46 
47 // Implementation of GnssMeasurement
48 
GnssMeasurement(const sp<IGnssMeasurementInterface> & iGnssMeasurement)49 GnssMeasurement::GnssMeasurement(const sp<IGnssMeasurementInterface>& iGnssMeasurement)
50       : mIGnssMeasurement(iGnssMeasurement) {}
51 
setCallback(const std::unique_ptr<GnssMeasurementCallback> & callback,const IGnssMeasurementInterface::Options & options)52 jboolean GnssMeasurement::setCallback(const std::unique_ptr<GnssMeasurementCallback>& callback,
53                                       const IGnssMeasurementInterface::Options& options) {
54     if (mIGnssMeasurement->getInterfaceVersion() >= 2) {
55         auto status = mIGnssMeasurement->setCallbackWithOptions(callback->getAidl(), options);
56         if (checkAidlStatus(status, "IGnssMeasurement setCallbackWithOptions() failed.")) {
57             return true;
58         }
59     }
60     auto status = mIGnssMeasurement->setCallback(callback->getAidl(), options.enableFullTracking,
61                                                  options.enableCorrVecOutputs);
62     return checkAidlStatus(status, "IGnssMeasurement setCallback() failed.");
63 }
64 
close()65 jboolean GnssMeasurement::close() {
66     auto status = mIGnssMeasurement->close();
67     return checkAidlStatus(status, "IGnssMeasurement close() failed.");
68 }
69 
70 // Implementation of GnssMeasurement_V1_0
71 
GnssMeasurement_V1_0(const sp<IGnssMeasurement_V1_0> & iGnssMeasurement)72 GnssMeasurement_V1_0::GnssMeasurement_V1_0(const sp<IGnssMeasurement_V1_0>& iGnssMeasurement)
73       : mIGnssMeasurement_V1_0(iGnssMeasurement) {}
74 
setCallback(const std::unique_ptr<GnssMeasurementCallback> & callback,const IGnssMeasurementInterface::Options & options)75 jboolean GnssMeasurement_V1_0::setCallback(const std::unique_ptr<GnssMeasurementCallback>& callback,
76                                            const IGnssMeasurementInterface::Options& options) {
77     if (options.enableFullTracking == true) {
78         ALOGW("Full tracking mode is not supported in 1.0 GNSS HAL.");
79     }
80     if (options.enableCorrVecOutputs == true) {
81         ALOGW("Correlation vector output is not supported in 1.0 GNSS HAL.");
82     }
83     if (options.intervalMs > 1000) {
84         ALOGW("Measurement interval is not supported in 1.0 GNSS HAL.");
85     }
86     auto status = mIGnssMeasurement_V1_0->setCallback(callback->getHidl());
87     if (!checkHidlReturn(status, "IGnssMeasurement setCallback() failed.")) {
88         return JNI_FALSE;
89     }
90 
91     return checkGnssMeasurementStatus(status);
92 }
93 
close()94 jboolean GnssMeasurement_V1_0::close() {
95     auto result = mIGnssMeasurement_V1_0->close();
96     return checkHidlReturn(result, "IGnssMeasurement close() failed.");
97 }
98 
99 // Implementation of GnssMeasurement_V1_1
100 
GnssMeasurement_V1_1(const sp<IGnssMeasurement_V1_1> & iGnssMeasurement)101 GnssMeasurement_V1_1::GnssMeasurement_V1_1(const sp<IGnssMeasurement_V1_1>& iGnssMeasurement)
102       : GnssMeasurement_V1_0{iGnssMeasurement}, mIGnssMeasurement_V1_1(iGnssMeasurement) {}
103 
setCallback(const std::unique_ptr<GnssMeasurementCallback> & callback,const IGnssMeasurementInterface::Options & options)104 jboolean GnssMeasurement_V1_1::setCallback(const std::unique_ptr<GnssMeasurementCallback>& callback,
105                                            const IGnssMeasurementInterface::Options& options) {
106     if (options.enableCorrVecOutputs == true) {
107         ALOGW("Correlation vector output is not supported in 1.1 GNSS HAL.");
108     }
109     if (options.intervalMs > 1000) {
110         ALOGW("Measurement interval is not supported in 1.0 GNSS HAL.");
111     }
112     auto status = mIGnssMeasurement_V1_1->setCallback_1_1(callback->getHidl(),
113                                                           options.enableFullTracking);
114     if (!checkHidlReturn(status, "IGnssMeasurement setCallback_V1_1() failed.")) {
115         return JNI_FALSE;
116     }
117 
118     return checkGnssMeasurementStatus(status);
119 }
120 
121 // Implementation of GnssMeasurement_V2_0
122 
GnssMeasurement_V2_0(const sp<IGnssMeasurement_V2_0> & iGnssMeasurement)123 GnssMeasurement_V2_0::GnssMeasurement_V2_0(const sp<IGnssMeasurement_V2_0>& iGnssMeasurement)
124       : GnssMeasurement_V1_1{iGnssMeasurement}, mIGnssMeasurement_V2_0(iGnssMeasurement) {}
125 
setCallback(const std::unique_ptr<GnssMeasurementCallback> & callback,const IGnssMeasurementInterface::Options & options)126 jboolean GnssMeasurement_V2_0::setCallback(const std::unique_ptr<GnssMeasurementCallback>& callback,
127                                            const IGnssMeasurementInterface::Options& options) {
128     if (options.enableCorrVecOutputs == true) {
129         ALOGW("Correlation vector output is not supported in 2.0 GNSS HAL.");
130     }
131     if (options.intervalMs > 1000) {
132         ALOGW("Measurement interval is not supported in 1.0 GNSS HAL.");
133     }
134     auto status = mIGnssMeasurement_V2_0->setCallback_2_0(callback->getHidl(),
135                                                           options.enableFullTracking);
136     if (!checkHidlReturn(status, "IGnssMeasurement setCallback_2_0() failed.")) {
137         return JNI_FALSE;
138     }
139 
140     return checkGnssMeasurementStatus(status);
141 }
142 
143 // Implementation of GnssMeasurement_V2_1
144 
GnssMeasurement_V2_1(const sp<IGnssMeasurement_V2_1> & iGnssMeasurement)145 GnssMeasurement_V2_1::GnssMeasurement_V2_1(const sp<IGnssMeasurement_V2_1>& iGnssMeasurement)
146       : GnssMeasurement_V2_0{iGnssMeasurement}, mIGnssMeasurement_V2_1(iGnssMeasurement) {}
147 
setCallback(const std::unique_ptr<GnssMeasurementCallback> & callback,const IGnssMeasurementInterface::Options & options)148 jboolean GnssMeasurement_V2_1::setCallback(const std::unique_ptr<GnssMeasurementCallback>& callback,
149                                            const IGnssMeasurementInterface::Options& options) {
150     if (options.enableCorrVecOutputs == true) {
151         ALOGW("Correlation vector output is not supported in 2.1 GNSS HAL.");
152     }
153     if (options.intervalMs > 1000) {
154         ALOGW("Measurement interval is not supported in 1.0 GNSS HAL.");
155     }
156     auto status = mIGnssMeasurement_V2_1->setCallback_2_1(callback->getHidl(),
157                                                           options.enableFullTracking);
158     if (!checkHidlReturn(status, "IGnssMeasurement setCallback_2_1() failed.")) {
159         return JNI_FALSE;
160     }
161 
162     return checkGnssMeasurementStatus(status);
163 }
164 
165 } // namespace android::gnss