• 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 // Define LOG_TAG before <log/log.h> to overwrite the default value.
18 #define LOG_TAG "GnssPsdsJni"
19 
20 #include "GnssPsds.h"
21 
22 #include "Utils.h"
23 
24 using android::hardware::hidl_bitfield;
25 using android::hardware::gnss::PsdsType;
26 using IGnssPsdsAidl = android::hardware::gnss::IGnssPsds;
27 using IGnssPsdsHidl = android::hardware::gnss::V1_0::IGnssXtra;
28 
29 namespace android::gnss {
30 
31 // Implementation of GnssPsds (AIDL HAL)
32 
GnssPsdsAidl(const sp<IGnssPsdsAidl> & iGnssPsds)33 GnssPsdsAidl::GnssPsdsAidl(const sp<IGnssPsdsAidl>& iGnssPsds) : mIGnssPsds(iGnssPsds) {
34     assert(mIGnssPsds != nullptr);
35 }
36 
setCallback(const std::unique_ptr<GnssPsdsCallback> & callback)37 jboolean GnssPsdsAidl::setCallback(const std::unique_ptr<GnssPsdsCallback>& callback) {
38     auto status = mIGnssPsds->setCallback(callback->getAidl());
39     return checkAidlStatus(status, "IGnssPsdsAidl setCallback() failed.");
40 }
41 
injectPsdsData(const jbyteArray & data,const jint & length,const jint & psdsType)42 void GnssPsdsAidl::injectPsdsData(const jbyteArray& data, const jint& length,
43                                   const jint& psdsType) {
44     JNIEnv* env = getJniEnv();
45     jbyte* bytes = reinterpret_cast<jbyte*>(env->GetPrimitiveArrayCritical(data, 0));
46     auto status = mIGnssPsds->injectPsdsData(static_cast<PsdsType>(psdsType),
47                                              std::vector<uint8_t>((const uint8_t*)bytes,
48                                                                   (const uint8_t*)bytes + length));
49     checkAidlStatus(status, "IGnssPsdsAidl injectPsdsData() failed.");
50     env->ReleasePrimitiveArrayCritical(data, bytes, JNI_ABORT);
51 }
52 
53 // Implementation of GnssPsdsHidl
54 
GnssPsdsHidl(const sp<android::hardware::gnss::V1_0::IGnssXtra> & iGnssXtra)55 GnssPsdsHidl::GnssPsdsHidl(const sp<android::hardware::gnss::V1_0::IGnssXtra>& iGnssXtra)
56       : mIGnssXtra(iGnssXtra) {
57     assert(mIGnssXtra != nullptr);
58 }
59 
setCallback(const std::unique_ptr<GnssPsdsCallback> & callback)60 jboolean GnssPsdsHidl::setCallback(const std::unique_ptr<GnssPsdsCallback>& callback) {
61     auto result = mIGnssXtra->setCallback(callback->getHidl());
62     return checkHidlReturn(result, "IGnssPsdsHidl setCallback() failed.");
63 }
64 
injectPsdsData(const jbyteArray & data,const jint & length,const jint &)65 void GnssPsdsHidl::injectPsdsData(const jbyteArray& data, const jint& length, const jint&) {
66     JNIEnv* env = getJniEnv();
67     jbyte* bytes = reinterpret_cast<jbyte*>(env->GetPrimitiveArrayCritical(data, 0));
68     auto result = mIGnssXtra->injectXtraData(std::string((const char*)bytes, length));
69     checkHidlReturn(result, "IGnssXtra injectXtraData() failed.");
70     env->ReleasePrimitiveArrayCritical(data, bytes, JNI_ABORT);
71 }
72 
73 } // namespace android::gnss
74