• 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 "AGnssRilJni"
19 
20 #include "AGnssRil.h"
21 
22 #include "Utils.h"
23 
24 using android::hardware::gnss::IAGnssRil;
25 using IAGnssRil_V1_0 = android::hardware::gnss::V1_0::IAGnssRil;
26 using IAGnssRil_V2_0 = android::hardware::gnss::V2_0::IAGnssRil;
27 
28 namespace android::gnss {
29 
30 // Implementation of AGnssRil (AIDL HAL)
31 
AGnssRil(const sp<IAGnssRil> & iAGnssRil)32 AGnssRil::AGnssRil(const sp<IAGnssRil>& iAGnssRil) : mIAGnssRil(iAGnssRil) {
33     assert(mIAGnssRil != nullptr);
34 }
35 
setCallback(const std::unique_ptr<AGnssRilCallback> & callback)36 jboolean AGnssRil::setCallback(const std::unique_ptr<AGnssRilCallback>& callback) {
37     auto status = mIAGnssRil->setCallback(callback->getAidl());
38     return checkAidlStatus(status, "IAGnssRilAidl setCallback() failed.");
39 }
40 
setSetId(jint type,const jstring & setid_string)41 jboolean AGnssRil::setSetId(jint type, const jstring& setid_string) {
42     JNIEnv* env = getJniEnv();
43     ScopedJniString jniSetId{env, setid_string};
44     auto status = mIAGnssRil->setSetId((IAGnssRil::SetIdType)type, jniSetId.c_str());
45     return checkAidlStatus(status, "IAGnssRilAidl setSetId() failed.");
46 }
47 
setRefLocation(jint type,jint mcc,jint mnc,jint lac,jlong cid,jint tac,jint pcid,jint arfcn)48 jboolean AGnssRil::setRefLocation(jint type, jint mcc, jint mnc, jint lac, jlong cid, jint tac,
49                                   jint pcid, jint arfcn) {
50     IAGnssRil::AGnssRefLocation location;
51     location.type = static_cast<IAGnssRil::AGnssRefLocationType>(type);
52 
53     switch (location.type) {
54         case IAGnssRil::AGnssRefLocationType::GSM_CELLID:
55         case IAGnssRil::AGnssRefLocationType::UMTS_CELLID:
56         case IAGnssRil::AGnssRefLocationType::LTE_CELLID:
57         case IAGnssRil::AGnssRefLocationType::NR_CELLID:
58             location.cellID.mcc = static_cast<int>(mcc);
59             location.cellID.mnc = static_cast<int>(mnc);
60             location.cellID.lac = static_cast<int>(lac);
61             location.cellID.cid = static_cast<long>(cid);
62             location.cellID.tac = static_cast<int>(tac);
63             location.cellID.pcid = static_cast<int>(pcid);
64             location.cellID.arfcn = static_cast<int>(arfcn);
65             break;
66         default:
67             ALOGE("Unknown cellid (%s:%d).", __FUNCTION__, __LINE__);
68             return JNI_FALSE;
69             break;
70     }
71 
72     auto status = mIAGnssRil->setRefLocation(location);
73     return checkAidlStatus(status, "IAGnssRilAidl dataConnClosed() failed.");
74 }
75 
updateNetworkState(jboolean connected,jint type,jboolean roaming,jboolean available,const jstring & apn,jlong networkHandle,jshort capabilities)76 jboolean AGnssRil::updateNetworkState(jboolean connected, jint type, jboolean roaming,
77                                       jboolean available, const jstring& apn, jlong networkHandle,
78                                       jshort capabilities) {
79     JNIEnv* env = getJniEnv();
80     ScopedJniString jniApn{env, apn};
81     IAGnssRil::NetworkAttributes networkAttributes;
82     networkAttributes.networkHandle = static_cast<int64_t>(networkHandle),
83     networkAttributes.isConnected = static_cast<bool>(connected),
84     networkAttributes.capabilities = static_cast<int32_t>(capabilities),
85     networkAttributes.apn = jniApn.c_str();
86 
87     auto status = mIAGnssRil->updateNetworkState(networkAttributes);
88     return checkAidlStatus(status, "IAGnssRilAidl updateNetworkState() failed.");
89 }
90 
injectNiSuplMessageData(const jbyteArray & msgData,jint length,jint slotIndex)91 jboolean AGnssRil::injectNiSuplMessageData(const jbyteArray& msgData, jint length, jint slotIndex) {
92     if (mIAGnssRil->getInterfaceVersion() <= 2) {
93         ALOGE("IAGnssRil does not support injectNiSuplMessageData().");
94         return JNI_FALSE;
95     }
96     JNIEnv* env = getJniEnv();
97     jbyte* bytes = reinterpret_cast<jbyte*>(env->GetPrimitiveArrayCritical(msgData, 0));
98     auto status = mIAGnssRil->injectNiSuplMessageData(std::vector<uint8_t>((const uint8_t*)bytes,
99                                                                            (const uint8_t*)bytes +
100                                                                                    length),
101                                                       static_cast<int>(slotIndex));
102     env->ReleasePrimitiveArrayCritical(msgData, bytes, JNI_ABORT);
103     return checkAidlStatus(status, "IAGnssRil injectNiSuplMessageData() failed.");
104 }
105 
106 // Implementation of AGnssRil_V1_0
107 
AGnssRil_V1_0(const sp<IAGnssRil_V1_0> & iAGnssRil)108 AGnssRil_V1_0::AGnssRil_V1_0(const sp<IAGnssRil_V1_0>& iAGnssRil) : mAGnssRil_V1_0(iAGnssRil) {
109     assert(mIAGnssRil_V1_0 != nullptr);
110 }
111 
setCallback(const std::unique_ptr<AGnssRilCallback> & callback)112 jboolean AGnssRil_V1_0::setCallback(const std::unique_ptr<AGnssRilCallback>& callback) {
113     auto result = mAGnssRil_V1_0->setCallback(callback->getV1_0());
114     return checkHidlReturn(result, "IAGnssRil_V1_0 setCallback() failed.");
115 }
116 
setSetId(jint type,const jstring & setid_string)117 jboolean AGnssRil_V1_0::setSetId(jint type, const jstring& setid_string) {
118     JNIEnv* env = getJniEnv();
119     ScopedJniString jniSetId{env, setid_string};
120     auto result = mAGnssRil_V1_0->setSetId((IAGnssRil_V1_0::SetIDType)type, jniSetId);
121     return checkHidlReturn(result, "IAGnssRil_V1_0 setSetId() failed.");
122 }
123 
setRefLocation(jint type,jint mcc,jint mnc,jint lac,jlong cid,jint tac,jint pcid,jint)124 jboolean AGnssRil_V1_0::setRefLocation(jint type, jint mcc, jint mnc, jint lac, jlong cid, jint tac,
125                                        jint pcid, jint) {
126     IAGnssRil_V1_0::AGnssRefLocation location;
127     location.type = static_cast<IAGnssRil_V1_0::AGnssRefLocationType>(type);
128 
129     switch (location.type) {
130         case IAGnssRil_V1_0::AGnssRefLocationType::GSM_CELLID:
131         case IAGnssRil_V1_0::AGnssRefLocationType::UMTS_CELLID:
132         case IAGnssRil_V1_0::AGnssRefLocationType::LTE_CELLID:
133             location.cellID.mcc = static_cast<uint16_t>(mcc);
134             location.cellID.mnc = static_cast<uint16_t>(mnc);
135             location.cellID.lac = static_cast<uint16_t>(lac);
136             location.cellID.cid = static_cast<uint32_t>(cid);
137             location.cellID.tac = static_cast<uint16_t>(tac);
138             location.cellID.pcid = static_cast<uint16_t>(pcid);
139             break;
140         default:
141             ALOGE("Unknown cellid (%s:%d).", __FUNCTION__, __LINE__);
142             return JNI_FALSE;
143             break;
144     }
145 
146     auto result = mAGnssRil_V1_0->setRefLocation(location);
147     return checkHidlReturn(result, "IAGnssRil_V1_0 setRefLocation() failed.");
148 }
149 
updateNetworkState(jboolean connected,jint type,jboolean roaming,jboolean available,const jstring & apn,jlong networkHandle,jshort capabilities)150 jboolean AGnssRil_V1_0::updateNetworkState(jboolean connected, jint type, jboolean roaming,
151                                            jboolean available, const jstring& apn,
152                                            jlong networkHandle, jshort capabilities) {
153     JNIEnv* env = getJniEnv();
154     ScopedJniString jniApn{env, apn};
155     hardware::hidl_string hidlApn{jniApn};
156     hardware::Return<bool> result(false);
157 
158     if (!hidlApn.empty()) {
159         result = mAGnssRil_V1_0->updateNetworkAvailability(available, hidlApn);
160         checkHidlReturn(result, "IAGnssRil_V1_0 updateNetworkAvailability() failed.");
161     }
162 
163     result = mAGnssRil_V1_0->updateNetworkState(connected,
164                                                 static_cast<IAGnssRil_V1_0::NetworkType>(type),
165                                                 roaming);
166     return checkHidlReturn(result, "IAGnssRil_V1_0 updateNetworkState() failed.");
167 }
168 
injectNiSuplMessageData(const jbyteArray &,jint,jint)169 jboolean AGnssRil_V1_0::injectNiSuplMessageData(const jbyteArray&, jint, jint) {
170     ALOGI("IAGnssRil_V1_0 interface does not support injectNiSuplMessageData.");
171     return JNI_FALSE;
172 }
173 
174 // Implementation of AGnssRil_V2_0
175 
AGnssRil_V2_0(const sp<IAGnssRil_V2_0> & iAGnssRil)176 AGnssRil_V2_0::AGnssRil_V2_0(const sp<IAGnssRil_V2_0>& iAGnssRil)
177       : AGnssRil_V1_0{iAGnssRil}, mAGnssRil_V2_0(iAGnssRil) {
178     assert(mIAGnssRil_V2_0 != nullptr);
179 }
180 
updateNetworkState(jboolean connected,jint type,jboolean roaming,jboolean available,const jstring & apn,jlong networkHandle,jshort capabilities)181 jboolean AGnssRil_V2_0::updateNetworkState(jboolean connected, jint type, jboolean roaming,
182                                            jboolean available, const jstring& apn,
183                                            jlong networkHandle, jshort capabilities) {
184     JNIEnv* env = getJniEnv();
185     ScopedJniString jniApn{env, apn};
186     IAGnssRil_V2_0::NetworkAttributes networkAttributes =
187             {.networkHandle = static_cast<uint64_t>(networkHandle),
188              .isConnected = static_cast<bool>(connected),
189              .capabilities = static_cast<uint16_t>(capabilities),
190              .apn = jniApn.c_str()};
191 
192     auto result = mAGnssRil_V2_0->updateNetworkState_2_0(networkAttributes);
193     return checkHidlReturn(result, "AGnssRil_V2_0 updateNetworkState_2_0() failed.");
194 }
195 
196 } // namespace android::gnss
197