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 #define LOG_TAG "AGnssCbJni"
18
19 #include "AGnssCallback.h"
20
21 namespace android::gnss {
22
23 using binder::Status;
24 using hardware::Return;
25 using hardware::Void;
26 using IAGnssCallback_V1_0 = android::hardware::gnss::V1_0::IAGnssCallback;
27 using IAGnssCallback_V2_0 = android::hardware::gnss::V2_0::IAGnssCallback;
28
29 namespace {
30
31 jmethodID method_reportAGpsStatus;
32
33 }
34
AGnss_class_init_once(JNIEnv * env,jclass clazz)35 void AGnss_class_init_once(JNIEnv* env, jclass clazz) {
36 method_reportAGpsStatus = env->GetMethodID(clazz, "reportAGpsStatus", "(II[B)V");
37 }
38
agnssStatusCb(AGnssType type,AGnssStatusValue status)39 Status AGnssCallbackAidl::agnssStatusCb(AGnssType type, AGnssStatusValue status) {
40 AGnssCallbackUtil::agnssStatusCbImpl(type, status);
41 return Status::ok();
42 }
43
agnssStatusIpV6Cb(const IAGnssCallback_V1_0::AGnssStatusIpV6 & agps_status)44 Return<void> AGnssCallback_V1_0::agnssStatusIpV6Cb(
45 const IAGnssCallback_V1_0::AGnssStatusIpV6& agps_status) {
46 JNIEnv* env = getJniEnv();
47 jbyteArray byteArray = nullptr;
48
49 byteArray = env->NewByteArray(16);
50 if (byteArray != nullptr) {
51 env->SetByteArrayRegion(byteArray, 0, 16, (const jbyte*)(agps_status.ipV6Addr.data()));
52 } else {
53 ALOGE("Unable to allocate byte array for IPv6 address.");
54 }
55
56 IF_ALOGD() {
57 // log the IP for reference in case there is a bogus value pushed by HAL
58 char str[INET6_ADDRSTRLEN];
59 inet_ntop(AF_INET6, agps_status.ipV6Addr.data(), str, INET6_ADDRSTRLEN);
60 ALOGD("AGPS IP is v6: %s", str);
61 }
62
63 jsize byteArrayLength = byteArray != nullptr ? env->GetArrayLength(byteArray) : 0;
64 ALOGV("Passing AGPS IP addr: size %d", byteArrayLength);
65 env->CallVoidMethod(mCallbacksObj, method_reportAGpsStatus, agps_status.type,
66 agps_status.status, byteArray);
67
68 checkAndClearExceptionFromCallback(env, __FUNCTION__);
69
70 if (byteArray) {
71 env->DeleteLocalRef(byteArray);
72 }
73
74 return Void();
75 }
76
agnssStatusIpV4Cb(const IAGnssCallback_V1_0::AGnssStatusIpV4 & agps_status)77 Return<void> AGnssCallback_V1_0::agnssStatusIpV4Cb(
78 const IAGnssCallback_V1_0::AGnssStatusIpV4& agps_status) {
79 JNIEnv* env = getJniEnv();
80 jbyteArray byteArray = nullptr;
81
82 uint32_t ipAddr = agps_status.ipV4Addr;
83 byteArray = convertToIpV4(ipAddr);
84
85 IF_ALOGD() {
86 /*
87 * log the IP for reference in case there is a bogus value pushed by
88 * HAL.
89 */
90 char str[INET_ADDRSTRLEN];
91 inet_ntop(AF_INET, &ipAddr, str, INET_ADDRSTRLEN);
92 ALOGD("AGPS IP is v4: %s", str);
93 }
94
95 jsize byteArrayLength = byteArray != nullptr ? env->GetArrayLength(byteArray) : 0;
96 ALOGV("Passing AGPS IP addr: size %d", byteArrayLength);
97 env->CallVoidMethod(mCallbacksObj, method_reportAGpsStatus, agps_status.type,
98 agps_status.status, byteArray);
99
100 checkAndClearExceptionFromCallback(env, __FUNCTION__);
101
102 if (byteArray) {
103 env->DeleteLocalRef(byteArray);
104 }
105 return Void();
106 }
107
convertToIpV4(uint32_t ip)108 jbyteArray AGnssCallback_V1_0::convertToIpV4(uint32_t ip) {
109 if (INADDR_NONE == ip) {
110 return nullptr;
111 }
112
113 JNIEnv* env = getJniEnv();
114 jbyteArray byteArray = env->NewByteArray(4);
115 if (byteArray == nullptr) {
116 ALOGE("Unable to allocate byte array for IPv4 address");
117 return nullptr;
118 }
119
120 jbyte ipv4[4];
121 ALOGV("Converting IPv4 address byte array (net_order) %x", ip);
122 memcpy(ipv4, &ip, sizeof(ipv4));
123 env->SetByteArrayRegion(byteArray, 0, 4, (const jbyte*)ipv4);
124 return byteArray;
125 }
126
agnssStatusCb(IAGnssCallback_V2_0::AGnssType type,IAGnssCallback_V2_0::AGnssStatusValue status)127 Return<void> AGnssCallback_V2_0::agnssStatusCb(IAGnssCallback_V2_0::AGnssType type,
128 IAGnssCallback_V2_0::AGnssStatusValue status) {
129 AGnssCallbackUtil::agnssStatusCbImpl(type, status);
130 return Void();
131 }
132
133 } // namespace android::gnss
134