• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
3  * Not a Contribution
4  */
5 /*
6  * Copyright (C) 2016 The Android Open Source Project
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #define LOG_TAG "LocSvc_AGnssInterface"
22 
23 #include <log_util.h>
24 #include "Gnss.h"
25 #include "AGnss.h"
26 
27 namespace android {
28 namespace hardware {
29 namespace gnss {
30 namespace V2_1 {
31 namespace implementation {
32 
33 static AGnss* spAGnss = nullptr;
34 
AGnss(Gnss * gnss)35 AGnss::AGnss(Gnss* gnss) : mGnss(gnss), mType(LOC_AGPS_TYPE_INVALID) {
36     spAGnss = this;
37 }
38 
~AGnss()39 AGnss::~AGnss() {
40     spAGnss = nullptr;
41 }
42 
agnssStatusIpV4Cb(AGnssExtStatusIpV4 status)43 void AGnss::agnssStatusIpV4Cb(AGnssExtStatusIpV4 status) {
44     if (nullptr != spAGnss) {
45         spAGnss->statusCb(status.type, status.status);
46     }
47 }
48 
statusCb(AGpsExtType type,LocAGpsStatusValue status)49 void AGnss::statusCb(AGpsExtType type, LocAGpsStatusValue status) {
50 
51     V2_0::IAGnssCallback::AGnssType  aType;
52     IAGnssCallback::AGnssStatusValue aStatus;
53 
54     // cache the AGps Type
55     mType = type;
56 
57     switch (type) {
58     case LOC_AGPS_TYPE_SUPL:
59         aType = IAGnssCallback::AGnssType::SUPL;
60         break;
61     case LOC_AGPS_TYPE_SUPL_ES:
62         aType = IAGnssCallback::AGnssType::SUPL_EIMS;
63         break;
64     default:
65         LOC_LOGE("invalid type: %d", type);
66         return;
67     }
68 
69     switch (status) {
70     case LOC_GPS_REQUEST_AGPS_DATA_CONN:
71         aStatus = IAGnssCallback::AGnssStatusValue::REQUEST_AGNSS_DATA_CONN;
72         break;
73     case LOC_GPS_RELEASE_AGPS_DATA_CONN:
74         aStatus = IAGnssCallback::AGnssStatusValue::RELEASE_AGNSS_DATA_CONN;
75         break;
76     case LOC_GPS_AGPS_DATA_CONNECTED:
77         aStatus = IAGnssCallback::AGnssStatusValue::AGNSS_DATA_CONNECTED;
78         break;
79     case LOC_GPS_AGPS_DATA_CONN_DONE:
80         aStatus = IAGnssCallback::AGnssStatusValue::AGNSS_DATA_CONN_DONE;
81         break;
82     case LOC_GPS_AGPS_DATA_CONN_FAILED:
83         aStatus = IAGnssCallback::AGnssStatusValue::AGNSS_DATA_CONN_FAILED;
84         break;
85     default:
86         LOC_LOGE("invalid status: %d", status);
87         return;
88     }
89 
90     if (mAGnssCbIface != nullptr) {
91         auto r = mAGnssCbIface->agnssStatusCb(aType, aStatus);
92         if (!r.isOk()) {
93             LOC_LOGw("Error invoking AGNSS status cb %s", r.description().c_str());
94         }
95     }
96     else {
97         LOC_LOGw("setCallback has not been called yet");
98     }
99 }
100 
setCallback(const sp<V2_0::IAGnssCallback> & callback)101 Return<void> AGnss::setCallback(const sp<V2_0::IAGnssCallback>& callback) {
102 
103     if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
104         LOC_LOGE("Null GNSS interface");
105         return Void();
106     }
107 
108     // Save the interface
109     mAGnssCbIface = callback;
110 
111     AgpsCbInfo cbInfo = {};
112     cbInfo.statusV4Cb = (void*)agnssStatusIpV4Cb;
113     cbInfo.atlType = AGPS_ATL_TYPE_SUPL | AGPS_ATL_TYPE_SUPL_ES;
114 
115     mGnss->getGnssInterface()->agpsInit(cbInfo);
116     return Void();
117 }
118 
dataConnClosed()119 Return<bool> AGnss::dataConnClosed() {
120 
121     if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
122         LOC_LOGE("Null GNSS interface");
123         return false;
124     }
125 
126     mGnss->getGnssInterface()->agpsDataConnClosed(LOC_AGPS_TYPE_SUPL);
127     return true;
128 }
129 
dataConnFailed()130 Return<bool> AGnss::dataConnFailed() {
131 
132     if(mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
133         LOC_LOGE("Null GNSS interface");
134         return false;
135     }
136 
137     mGnss->getGnssInterface()->agpsDataConnFailed(LOC_AGPS_TYPE_SUPL);
138     return true;
139 }
140 
dataConnOpen(uint64_t,const hidl_string & apn,V2_0::IAGnss::ApnIpType apnIpType)141 Return<bool> AGnss::dataConnOpen(uint64_t /*networkHandle*/, const hidl_string& apn,
142         V2_0::IAGnss::ApnIpType apnIpType) {
143 
144     if (mGnss == nullptr || mGnss->getGnssInterface() == nullptr){
145         LOC_LOGE("Null GNSS interface");
146         return false;
147     }
148 
149     std::string apnString(apn.c_str());
150     // During Emergency SUPL, an apn name of "sos" means that no
151     // apn was found, like in the simless case, so apn is cleared
152     if (LOC_AGPS_TYPE_SUPL_ES == mType && "sos" == apnString) {
153         LOC_LOGD("dataConnOpen APN name = [sos] cleared");
154         apnString.clear();
155     }
156 
157     LOC_LOGD("dataConnOpen APN name = [%s]", apnString.c_str());
158 
159     AGpsBearerType bearerType;
160     switch (apnIpType) {
161     case IAGnss::ApnIpType::IPV4:
162         bearerType = AGPS_APN_BEARER_IPV4;
163         break;
164     case IAGnss::ApnIpType::IPV6:
165         bearerType = AGPS_APN_BEARER_IPV6;
166         break;
167     case IAGnss::ApnIpType::IPV4V6:
168         bearerType = AGPS_APN_BEARER_IPV4V6;
169         break;
170     default:
171         bearerType = AGPS_APN_BEARER_IPV4;
172         break;
173     }
174 
175     mGnss->getGnssInterface()->agpsDataConnOpen(
176         LOC_AGPS_TYPE_SUPL, apnString.c_str(), apnString.size(), (int)bearerType);
177     return true;
178 }
179 
setServer(V2_0::IAGnssCallback::AGnssType type,const hidl_string & hostname,int32_t port)180 Return<bool> AGnss::setServer(V2_0::IAGnssCallback::AGnssType type,
181                               const hidl_string& hostname,
182                               int32_t port) {
183     if (mGnss == nullptr) {
184         LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__);
185         return false;
186     }
187 
188     GnssConfig config;
189     memset(&config, 0, sizeof(GnssConfig));
190     config.size = sizeof(GnssConfig);
191     config.flags = GNSS_CONFIG_FLAGS_SET_ASSISTANCE_DATA_VALID_BIT;
192     config.assistanceServer.size = sizeof(GnssConfigSetAssistanceServer);
193     if (type == IAGnssCallback::AGnssType::SUPL) {
194         config.assistanceServer.type = GNSS_ASSISTANCE_TYPE_SUPL;
195     } else if (type == IAGnssCallback::AGnssType::C2K) {
196         config.assistanceServer.type = GNSS_ASSISTANCE_TYPE_C2K;
197     } else if (type == IAGnssCallback::AGnssType::SUPL_EIMS) {
198         config.assistanceServer.type = GNSS_ASSISTANCE_TYPE_SUPL_EIMS;
199     } else if (type == IAGnssCallback::AGnssType::SUPL_IMS) {
200         config.assistanceServer.type = GNSS_ASSISTANCE_TYPE_SUPL_IMS;
201     } else {
202         LOC_LOGE("%s]: invalid AGnssType: %d", __FUNCTION__, static_cast<uint8_t>(type));
203         return false;
204     }
205     config.assistanceServer.hostName = strdup(hostname.c_str());
206     config.assistanceServer.port = port;
207     return mGnss->updateConfiguration(config);
208 }
209 
210 }  // namespace implementation
211 }  // namespace V2_1
212 }  // namespace gnss
213 }  // namespace hardware
214 }  // namespace android
215