1 /*
2  * Copyright (c) 2017-2019, 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_GnssConfigurationInterface"
22 
23 #include <log_util.h>
24 #include "Gnss.h"
25 #include "GnssConfiguration.h"
26 #include "ContextBase.h"
27 #include <android/hardware/gnss/1.0/types.h>
28 
29 namespace android {
30 namespace hardware {
31 namespace gnss {
32 namespace V2_0 {
33 namespace implementation {
34 
35 using ::android::hardware::gnss::V1_0::GnssConstellationType;
36 using namespace loc_core;
37 
GnssConfiguration(Gnss * gnss)38 GnssConfiguration::GnssConfiguration(Gnss* gnss) : mGnss(gnss) {
39 }
40 
41 // Methods from ::android::hardware::gps::V1_0::IGnssConfiguration follow.
setSuplEs(bool enabled)42 Return<bool> GnssConfiguration::setSuplEs(bool enabled)  {
43     // deprecated function. Must return false to pass VTS
44     return false;
45 }
46 
setSuplVersion(uint32_t version)47 Return<bool> GnssConfiguration::setSuplVersion(uint32_t version)  {
48     if (mGnss == nullptr) {
49         LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__);
50         return false;
51     }
52 
53     GnssConfig config;
54     memset(&config, 0, sizeof(GnssConfig));
55     config.size = sizeof(GnssConfig);
56     config.flags = GNSS_CONFIG_FLAGS_SUPL_VERSION_VALID_BIT;
57     switch (version) {
58         case 0x00020004:
59             config.suplVersion = GNSS_CONFIG_SUPL_VERSION_2_0_4;
60             break;
61         case 0x00020002:
62             config.suplVersion = GNSS_CONFIG_SUPL_VERSION_2_0_2;
63             break;
64         case 0x00020000:
65             config.suplVersion = GNSS_CONFIG_SUPL_VERSION_2_0_0;
66             break;
67         case 0x00010000:
68             config.suplVersion = GNSS_CONFIG_SUPL_VERSION_1_0_0;
69             break;
70         default:
71             LOC_LOGE("%s]: invalid version: 0x%x.", __FUNCTION__, version);
72             return false;
73             break;
74     }
75 
76     return mGnss->updateConfiguration(config);
77 }
78 
setSuplMode(uint8_t mode)79 Return<bool> GnssConfiguration::setSuplMode(uint8_t mode)  {
80     if (mGnss == nullptr) {
81         LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__);
82         return false;
83     }
84 
85     GnssConfig config;
86     memset(&config, 0, sizeof(GnssConfig));
87     config.size = sizeof(GnssConfig);
88     config.flags = GNSS_CONFIG_FLAGS_SUPL_MODE_BIT;
89     switch (mode) {
90         case 0:
91             config.suplModeMask = 0; // STANDALONE ONLY
92             break;
93         case 1:
94             config.suplModeMask = GNSS_CONFIG_SUPL_MODE_MSB_BIT;
95             break;
96         case 2:
97             config.suplModeMask = GNSS_CONFIG_SUPL_MODE_MSA_BIT;
98             break;
99         case 3:
100             config.suplModeMask = GNSS_CONFIG_SUPL_MODE_MSB_BIT | GNSS_CONFIG_SUPL_MODE_MSA_BIT;
101             break;
102         default:
103             LOC_LOGE("%s]: invalid mode: %d.", __FUNCTION__, mode);
104             return false;
105             break;
106     }
107 
108     return mGnss->updateConfiguration(config);
109 }
110 
setLppProfile(uint8_t lppProfile)111 Return<bool> GnssConfiguration::setLppProfile(uint8_t lppProfile) {
112     if (mGnss == nullptr) {
113         LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__);
114         return false;
115     }
116 
117     GnssConfig config = {};
118     config.size = sizeof(GnssConfig);
119     config.flags = GNSS_CONFIG_FLAGS_LPP_PROFILE_VALID_BIT;
120     switch (lppProfile) {
121         case 0:
122             config.lppProfile = GNSS_CONFIG_LPP_PROFILE_RRLP_ON_LTE;
123             break;
124         case 1:
125             config.lppProfile = GNSS_CONFIG_LPP_PROFILE_USER_PLANE;
126             break;
127         case 2:
128             config.lppProfile = GNSS_CONFIG_LPP_PROFILE_CONTROL_PLANE;
129             break;
130         case 3:
131             config.lppProfile = GNSS_CONFIG_LPP_PROFILE_USER_PLANE_AND_CONTROL_PLANE;
132             break;
133         default:
134             LOC_LOGE("%s]: invalid lppProfile: %d.", __FUNCTION__, lppProfile);
135             return false;
136             break;
137     }
138 
139     return mGnss->updateConfiguration(config);
140 }
141 
setGlonassPositioningProtocol(uint8_t protocol)142 Return<bool> GnssConfiguration::setGlonassPositioningProtocol(uint8_t protocol) {
143     if (mGnss == nullptr) {
144         LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__);
145         return false;
146     }
147 
148     GnssConfig config;
149     memset(&config, 0, sizeof(GnssConfig));
150     config.size = sizeof(GnssConfig);
151 
152     config.flags = GNSS_CONFIG_FLAGS_AGLONASS_POSITION_PROTOCOL_VALID_BIT;
153     if (protocol & (1<<0)) {
154         config.aGlonassPositionProtocolMask |= GNSS_CONFIG_RRC_CONTROL_PLANE_BIT;
155     }
156     if (protocol & (1<<1)) {
157         config.aGlonassPositionProtocolMask |= GNSS_CONFIG_RRLP_USER_PLANE_BIT;
158     }
159     if (protocol & (1<<2)) {
160         config.aGlonassPositionProtocolMask |= GNSS_CONFIG_LLP_USER_PLANE_BIT;
161     }
162     if (protocol & (1<<3)) {
163         config.aGlonassPositionProtocolMask |= GNSS_CONFIG_LLP_CONTROL_PLANE_BIT;
164     }
165 
166     return mGnss->updateConfiguration(config);
167 }
168 
setGpsLock(uint8_t lock)169 Return<bool> GnssConfiguration::setGpsLock(uint8_t lock) {
170 
171     if (mGnss == nullptr) {
172         LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__);
173         return false;
174     }
175 
176     GnssConfig config = {};
177     config.size = sizeof(GnssConfig);
178     config.flags = GNSS_CONFIG_FLAGS_GPS_LOCK_VALID_BIT;
179     switch (lock) {
180     case 0:
181         config.gpsLock = GNSS_CONFIG_GPS_LOCK_NONE;
182         break;
183     case 1:
184         config.gpsLock = GNSS_CONFIG_GPS_LOCK_MO;
185         break;
186     case 2:
187         config.gpsLock = GNSS_CONFIG_GPS_LOCK_NI;
188         break;
189     case 3:
190         config.gpsLock = GNSS_CONFIG_GPS_LOCK_MO_AND_NI;
191         break;
192     default:
193         LOC_LOGE("%s]: invalid lock: %d.", __FUNCTION__, lock);
194         return false;
195         break;
196     }
197 
198     mGnss->updateConfiguration(config);
199     // Must return false to pass VTS
200     return false;
201 }
202 
setEmergencySuplPdn(bool enabled)203 Return<bool> GnssConfiguration::setEmergencySuplPdn(bool enabled) {
204     if (mGnss == nullptr) {
205         LOC_LOGE("%s]: mGnss is nullptr", __FUNCTION__);
206         return false;
207     }
208 
209     GnssConfig config;
210     memset(&config, 0, sizeof(GnssConfig));
211     config.size = sizeof(GnssConfig);
212     config.flags = GNSS_CONFIG_FLAGS_EM_PDN_FOR_EM_SUPL_VALID_BIT;
213     config.emergencyPdnForEmergencySupl = (enabled ?
214             GNSS_CONFIG_EMERGENCY_PDN_FOR_EMERGENCY_SUPL_YES :
215             GNSS_CONFIG_EMERGENCY_PDN_FOR_EMERGENCY_SUPL_NO);
216 
217     return mGnss->updateConfiguration(config);
218 }
219 
220 // Methods from ::android::hardware::gnss::V1_1::IGnssConfiguration follow.
setBlacklist(const hidl_vec<GnssConfiguration::BlacklistedSource> & blacklist)221 Return<bool> GnssConfiguration::setBlacklist(
222             const hidl_vec<GnssConfiguration::BlacklistedSource>& blacklist) {
223 
224     ENTRY_LOG_CALLFLOW();
225     if (nullptr == mGnss) {
226         LOC_LOGe("mGnss is null");
227         return false;
228     }
229 
230     // blValid is true if blacklist is empty, i.e. clearing the BL;
231     // if blacklist is not empty, blValid is initialied to false, and later
232     // updated in the for loop to become true only if there is at least
233     // one {constellation, svid} in the list that is valid.
234     bool blValid = (0 == blacklist.size());
235     GnssConfig config;
236     memset(&config, 0, sizeof(GnssConfig));
237     config.size = sizeof(GnssConfig);
238     config.flags = GNSS_CONFIG_FLAGS_BLACKLISTED_SV_IDS_BIT;
239     config.blacklistedSvIds.clear();
240 
241     GnssSvIdSource source = {};
242     for (int idx = 0; idx < (int)blacklist.size(); idx++) {
243         // Set blValid true if any one source is valid
244         blValid = setBlacklistedSource(source, blacklist[idx]) || blValid;
245         config.blacklistedSvIds.push_back(source);
246     }
247 
248     // Update configuration only if blValid is true
249     // i.e. only if atleast one source is valid for blacklisting
250     return (blValid && mGnss->updateConfiguration(config));
251 }
252 
setBlacklistedSource(GnssSvIdSource & copyToSource,const GnssConfiguration::BlacklistedSource & copyFromSource)253 bool GnssConfiguration::setBlacklistedSource(
254         GnssSvIdSource& copyToSource,
255         const GnssConfiguration::BlacklistedSource& copyFromSource) {
256 
257     bool retVal = true;
258     uint16_t svIdOffset = 0;
259     copyToSource.size = sizeof(GnssSvIdSource);
260     copyToSource.svId = copyFromSource.svid;
261 
262     switch(copyFromSource.constellation) {
263     case GnssConstellationType::GPS:
264         copyToSource.constellation = GNSS_SV_TYPE_GPS;
265         LOC_LOGe("GPS SVs can't be blacklisted.");
266         retVal = false;
267         break;
268     case GnssConstellationType::SBAS:
269         copyToSource.constellation = GNSS_SV_TYPE_SBAS;
270         LOC_LOGe("SBAS SVs can't be blacklisted.");
271         retVal = false;
272         break;
273     case GnssConstellationType::GLONASS:
274         copyToSource.constellation = GNSS_SV_TYPE_GLONASS;
275         svIdOffset = GNSS_SV_CONFIG_GLO_INITIAL_SV_ID - 1;
276         break;
277     case GnssConstellationType::QZSS:
278         copyToSource.constellation = GNSS_SV_TYPE_QZSS;
279         svIdOffset = 0;
280         break;
281     case GnssConstellationType::BEIDOU:
282         copyToSource.constellation = GNSS_SV_TYPE_BEIDOU;
283         svIdOffset = GNSS_SV_CONFIG_BDS_INITIAL_SV_ID - 1;
284         break;
285     case GnssConstellationType::GALILEO:
286         copyToSource.constellation = GNSS_SV_TYPE_GALILEO;
287         svIdOffset = GNSS_SV_CONFIG_GAL_INITIAL_SV_ID - 1;
288         break;
289     default:
290         copyToSource.constellation = GNSS_SV_TYPE_UNKNOWN;
291         LOC_LOGe("Invalid constellation %hhu", copyFromSource.constellation);
292         retVal = false;
293         break;
294     }
295 
296     if (copyToSource.svId > 0 && svIdOffset > 0) {
297         copyToSource.svId += svIdOffset;
298     }
299 
300     return retVal;
301 }
302 
303 // Methods from ::android::hardware::gnss::V2_0::IGnssConfiguration follow.
setEsExtensionSec(uint32_t emergencyExtensionSeconds)304 Return<bool> GnssConfiguration::setEsExtensionSec(uint32_t emergencyExtensionSeconds) {
305     ENTRY_LOG_CALLFLOW();
306     if (mGnss == nullptr) {
307         LOC_LOGe("mGnss is nullptr");
308         return false;
309     }
310 
311     GnssConfig config;
312     memset(&config, 0, sizeof(GnssConfig));
313     config.size = sizeof(GnssConfig);
314     config.flags = GNSS_CONFIG_FLAGS_EMERGENCY_EXTENSION_SECONDS_BIT;
315     config.emergencyExtensionSeconds = emergencyExtensionSeconds;
316 
317     return mGnss->updateConfiguration(config);
318 }
319 
320 }  // namespace implementation
321 }  // namespace V2_0
322 }  // namespace gnss
323 }  // namespace hardware
324 }  // namespace android
325