1 /* Copyright (c) 2011-2014,2016-2017 The Linux Foundation. All rights reserved.
2  *
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *     * Redistributions of source code must retain the above copyright
7  *       notice, this list of conditions and the following disclaimer.
8  *     * Redistributions in binary form must reproduce the above
9  *       copyright notice, this list of conditions and the following
10  *       disclaimer in the documentation and/or other materials provided
11  *       with the distribution.
12  *     * Neither the name of The Linux Foundation, nor the names of its
13  *       contributors may be used to endorse or promote products derived
14  *       from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 #define LOG_NDEBUG 0
30 #define LOG_TAG "LocSvc_CtxBase"
31 
32 #include <dlfcn.h>
33 #include <cutils/sched_policy.h>
34 #include <unistd.h>
35 #include <ContextBase.h>
36 #include <msg_q.h>
37 #include <loc_target.h>
38 #include <loc_pla.h>
39 #include <loc_log.h>
40 
41 namespace loc_core {
42 
43 #define SLL_LOC_API_LIB_NAME "libsynergy_loc_api.so"
44 #define LOC_APIV2_0_LIB_NAME "libloc_api_v02.so"
45 #define IS_SS5_HW_ENABLED  1
46 
47 loc_gps_cfg_s_type ContextBase::mGps_conf {};
48 loc_sap_cfg_s_type ContextBase::mSap_conf {};
49 bool ContextBase::sIsEngineCapabilitiesKnown = false;
50 uint64_t ContextBase::sSupportedMsgMask = 0;
51 bool ContextBase::sGnssMeasurementSupported = false;
52 uint8_t ContextBase::sFeaturesSupported[MAX_FEATURE_LENGTH];
53 
54 const loc_param_s_type ContextBase::mGps_conf_table[] =
55 {
56   {"GPS_LOCK",                       &mGps_conf.GPS_LOCK,                       NULL, 'n'},
57   {"SUPL_VER",                       &mGps_conf.SUPL_VER,                       NULL, 'n'},
58   {"LPP_PROFILE",                    &mGps_conf.LPP_PROFILE,                    NULL, 'n'},
59   {"A_GLONASS_POS_PROTOCOL_SELECT",  &mGps_conf.A_GLONASS_POS_PROTOCOL_SELECT,  NULL, 'n'},
60   {"LPPE_CP_TECHNOLOGY",             &mGps_conf.LPPE_CP_TECHNOLOGY,             NULL, 'n'},
61   {"LPPE_UP_TECHNOLOGY",             &mGps_conf.LPPE_UP_TECHNOLOGY,             NULL, 'n'},
62   {"AGPS_CERT_WRITABLE_MASK",        &mGps_conf.AGPS_CERT_WRITABLE_MASK,        NULL, 'n'},
63   {"SUPL_MODE",                      &mGps_conf.SUPL_MODE,                      NULL, 'n'},
64   {"SUPL_ES",                        &mGps_conf.SUPL_ES,                        NULL, 'n'},
65   {"INTERMEDIATE_POS",               &mGps_conf.INTERMEDIATE_POS,               NULL, 'n'},
66   {"ACCURACY_THRES",                 &mGps_conf.ACCURACY_THRES,                 NULL, 'n'},
67   {"NMEA_PROVIDER",                  &mGps_conf.NMEA_PROVIDER,                  NULL, 'n'},
68   {"CAPABILITIES",                   &mGps_conf.CAPABILITIES,                   NULL, 'n'},
69   {"XTRA_VERSION_CHECK",             &mGps_conf.XTRA_VERSION_CHECK,             NULL, 'n'},
70   {"XTRA_SERVER_1",                  &mGps_conf.XTRA_SERVER_1,                  NULL, 's'},
71   {"XTRA_SERVER_2",                  &mGps_conf.XTRA_SERVER_2,                  NULL, 's'},
72   {"XTRA_SERVER_3",                  &mGps_conf.XTRA_SERVER_3,                  NULL, 's'},
73   {"USE_EMERGENCY_PDN_FOR_EMERGENCY_SUPL",
74            &mGps_conf.USE_EMERGENCY_PDN_FOR_EMERGENCY_SUPL,          NULL, 'n'},
75   {"AGPS_CONFIG_INJECT",             &mGps_conf.AGPS_CONFIG_INJECT,             NULL, 'n'},
76   {"EXTERNAL_DR_ENABLED",            &mGps_conf.EXTERNAL_DR_ENABLED,                  NULL, 'n'},
77   {"SUPL_HOST",                      &mGps_conf.SUPL_HOST,                      NULL, 's'},
78   {"SUPL_PORT",                      &mGps_conf.SUPL_PORT,                      NULL, 'n'},
79   {"MODEM_TYPE",                     &mGps_conf.MODEM_TYPE,                     NULL, 'n' },
80   {"MO_SUPL_HOST",                   &mGps_conf.MO_SUPL_HOST,                   NULL, 's' },
81   {"MO_SUPL_PORT",                   &mGps_conf.MO_SUPL_PORT,                   NULL, 'n' },
82   {"CONSTRAINED_TIME_UNCERTAINTY_ENABLED",
83            &mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_ENABLED,      NULL, 'n'},
84   {"CONSTRAINED_TIME_UNCERTAINTY_THRESHOLD",
85            &mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_THRESHOLD,    NULL, 'f'},
86   {"CONSTRAINED_TIME_UNCERTAINTY_ENERGY_BUDGET",
87            &mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_ENERGY_BUDGET, NULL, 'n'},
88   {"POSITION_ASSISTED_CLOCK_ESTIMATOR_ENABLED",
89            &mGps_conf.POSITION_ASSISTED_CLOCK_ESTIMATOR_ENABLED, NULL, 'n'},
90   {"PROXY_APP_PACKAGE_NAME",         &mGps_conf.PROXY_APP_PACKAGE_NAME,         NULL, 's' },
91   {"CP_MTLR_ES",                     &mGps_conf.CP_MTLR_ES,                     NULL, 'n' },
92   {"GNSS_DEPLOYMENT",  &mGps_conf.GNSS_DEPLOYMENT, NULL, 'n'},
93   {"CUSTOM_NMEA_GGA_FIX_QUALITY_ENABLED",
94            &mGps_conf.CUSTOM_NMEA_GGA_FIX_QUALITY_ENABLED, NULL, 'n'},
95   {"NI_SUPL_DENY_ON_NFW_LOCKED",  &mGps_conf.NI_SUPL_DENY_ON_NFW_LOCKED, NULL, 'n'},
96 };
97 
98 const loc_param_s_type ContextBase::mSap_conf_table[] =
99 {
100   {"GYRO_BIAS_RANDOM_WALK",          &mSap_conf.GYRO_BIAS_RANDOM_WALK,          &mSap_conf.GYRO_BIAS_RANDOM_WALK_VALID, 'f'},
101   {"ACCEL_RANDOM_WALK_SPECTRAL_DENSITY",     &mSap_conf.ACCEL_RANDOM_WALK_SPECTRAL_DENSITY,    &mSap_conf.ACCEL_RANDOM_WALK_SPECTRAL_DENSITY_VALID, 'f'},
102   {"ANGLE_RANDOM_WALK_SPECTRAL_DENSITY",     &mSap_conf.ANGLE_RANDOM_WALK_SPECTRAL_DENSITY,    &mSap_conf.ANGLE_RANDOM_WALK_SPECTRAL_DENSITY_VALID, 'f'},
103   {"RATE_RANDOM_WALK_SPECTRAL_DENSITY",      &mSap_conf.RATE_RANDOM_WALK_SPECTRAL_DENSITY,     &mSap_conf.RATE_RANDOM_WALK_SPECTRAL_DENSITY_VALID, 'f'},
104   {"VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY",  &mSap_conf.VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY, &mSap_conf.VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY_VALID, 'f'},
105   {"SENSOR_ACCEL_BATCHES_PER_SEC",   &mSap_conf.SENSOR_ACCEL_BATCHES_PER_SEC,   NULL, 'n'},
106   {"SENSOR_ACCEL_SAMPLES_PER_BATCH", &mSap_conf.SENSOR_ACCEL_SAMPLES_PER_BATCH, NULL, 'n'},
107   {"SENSOR_GYRO_BATCHES_PER_SEC",    &mSap_conf.SENSOR_GYRO_BATCHES_PER_SEC,    NULL, 'n'},
108   {"SENSOR_GYRO_SAMPLES_PER_BATCH",  &mSap_conf.SENSOR_GYRO_SAMPLES_PER_BATCH,  NULL, 'n'},
109   {"SENSOR_ACCEL_BATCHES_PER_SEC_HIGH",   &mSap_conf.SENSOR_ACCEL_BATCHES_PER_SEC_HIGH,   NULL, 'n'},
110   {"SENSOR_ACCEL_SAMPLES_PER_BATCH_HIGH", &mSap_conf.SENSOR_ACCEL_SAMPLES_PER_BATCH_HIGH, NULL, 'n'},
111   {"SENSOR_GYRO_BATCHES_PER_SEC_HIGH",    &mSap_conf.SENSOR_GYRO_BATCHES_PER_SEC_HIGH,    NULL, 'n'},
112   {"SENSOR_GYRO_SAMPLES_PER_BATCH_HIGH",  &mSap_conf.SENSOR_GYRO_SAMPLES_PER_BATCH_HIGH,  NULL, 'n'},
113   {"SENSOR_CONTROL_MODE",            &mSap_conf.SENSOR_CONTROL_MODE,            NULL, 'n'},
114   {"SENSOR_ALGORITHM_CONFIG_MASK",   &mSap_conf.SENSOR_ALGORITHM_CONFIG_MASK,   NULL, 'n'}
115 };
116 
readConfig()117 void ContextBase::readConfig()
118 {
119     static bool confReadDone = false;
120     if (!confReadDone) {
121         confReadDone = true;
122         /*Defaults for gps.conf*/
123         mGps_conf.INTERMEDIATE_POS = 0;
124         mGps_conf.ACCURACY_THRES = 0;
125         mGps_conf.NMEA_PROVIDER = 0;
126         mGps_conf.GPS_LOCK = GNSS_CONFIG_GPS_LOCK_MO_AND_NI;
127         mGps_conf.SUPL_VER = 0x10000;
128         mGps_conf.SUPL_MODE = 0x1;
129         mGps_conf.SUPL_ES = 0;
130         mGps_conf.CP_MTLR_ES = 0;
131         mGps_conf.SUPL_HOST[0] = 0;
132         mGps_conf.SUPL_PORT = 0;
133         mGps_conf.CAPABILITIES = 0x7;
134         /* LTE Positioning Profile configuration is disable by default*/
135         mGps_conf.LPP_PROFILE = 0;
136         /*By default no positioning protocol is selected on A-GLONASS system*/
137         mGps_conf.A_GLONASS_POS_PROTOCOL_SELECT = 0;
138         /*XTRA version check is disabled by default*/
139         mGps_conf.XTRA_VERSION_CHECK=0;
140         /*Use emergency PDN by default*/
141         mGps_conf.USE_EMERGENCY_PDN_FOR_EMERGENCY_SUPL = 1;
142         /* By default no LPPe CP technology is enabled*/
143         mGps_conf.LPPE_CP_TECHNOLOGY = 0;
144         /* By default no LPPe UP technology is enabled*/
145         mGps_conf.LPPE_UP_TECHNOLOGY = 0;
146         /* By default we use unknown modem type*/
147         mGps_conf.MODEM_TYPE = 2;
148 
149         /*Defaults for sap.conf*/
150         mSap_conf.GYRO_BIAS_RANDOM_WALK = 0;
151         mSap_conf.SENSOR_ACCEL_BATCHES_PER_SEC = 2;
152         mSap_conf.SENSOR_ACCEL_SAMPLES_PER_BATCH = 5;
153         mSap_conf.SENSOR_GYRO_BATCHES_PER_SEC = 2;
154         mSap_conf.SENSOR_GYRO_SAMPLES_PER_BATCH = 5;
155         mSap_conf.SENSOR_ACCEL_BATCHES_PER_SEC_HIGH = 4;
156         mSap_conf.SENSOR_ACCEL_SAMPLES_PER_BATCH_HIGH = 25;
157         mSap_conf.SENSOR_GYRO_BATCHES_PER_SEC_HIGH = 4;
158         mSap_conf.SENSOR_GYRO_SAMPLES_PER_BATCH_HIGH = 25;
159         mSap_conf.SENSOR_CONTROL_MODE = 0; /* AUTO */
160         mSap_conf.SENSOR_ALGORITHM_CONFIG_MASK = 0; /* INS Disabled = FALSE*/
161         /* Values MUST be set by OEMs in configuration for sensor-assisted
162           navigation to work. There are NO default values */
163         mSap_conf.ACCEL_RANDOM_WALK_SPECTRAL_DENSITY = 0;
164         mSap_conf.ANGLE_RANDOM_WALK_SPECTRAL_DENSITY = 0;
165         mSap_conf.RATE_RANDOM_WALK_SPECTRAL_DENSITY = 0;
166         mSap_conf.VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY = 0;
167         mSap_conf.GYRO_BIAS_RANDOM_WALK_VALID = 0;
168         mSap_conf.ACCEL_RANDOM_WALK_SPECTRAL_DENSITY_VALID = 0;
169         mSap_conf.ANGLE_RANDOM_WALK_SPECTRAL_DENSITY_VALID = 0;
170         mSap_conf.RATE_RANDOM_WALK_SPECTRAL_DENSITY_VALID = 0;
171         mSap_conf.VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY_VALID = 0;
172 
173         /* None of the 10 slots for agps certificates are writable by default */
174         mGps_conf.AGPS_CERT_WRITABLE_MASK = 0;
175 
176         /* inject supl config to modem with config values from config.xml or gps.conf, default 1 */
177         mGps_conf.AGPS_CONFIG_INJECT = 1;
178 
179         /* default configuration value of constrained time uncertainty mode:
180            feature disabled, time uncertainty threshold defined by modem,
181            and unlimited power budget */
182 #ifdef FEATURE_AUTOMOTIVE
183         mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_ENABLED = 1;
184 #else
185         mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_ENABLED = 0;
186 #endif
187         mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_THRESHOLD = 0.0;
188         mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_ENERGY_BUDGET = 0;
189 
190         /* default configuration value of position assisted clock estimator mode */
191         mGps_conf.POSITION_ASSISTED_CLOCK_ESTIMATOR_ENABLED = 0;
192         /* default configuration QTI GNSS H/W */
193         mGps_conf.GNSS_DEPLOYMENT = 0;
194         mGps_conf.CUSTOM_NMEA_GGA_FIX_QUALITY_ENABLED = 0;
195         /* default configuration for NI_SUPL_DENY_ON_NFW_LOCKED */
196         mGps_conf.NI_SUPL_DENY_ON_NFW_LOCKED = 1;
197 
198         UTIL_READ_CONF(LOC_PATH_GPS_CONF, mGps_conf_table);
199         UTIL_READ_CONF(LOC_PATH_SAP_CONF, mSap_conf_table);
200 
201         LOC_LOGI("%s] GNSS Deployment: %s", __FUNCTION__,
202                 ((mGps_conf.GNSS_DEPLOYMENT == 1) ? "SS5" :
203                 ((mGps_conf.GNSS_DEPLOYMENT == 2) ? "QFUSION" : "QGNSS")));
204 
205         switch (getTargetGnssType(loc_get_target())) {
206           case GNSS_GSS:
207           case GNSS_AUTO:
208              // For APQ targets, MSA/MSB capabilities should be reset
209              mGps_conf.CAPABILITIES &= ~(LOC_GPS_CAPABILITY_MSA | LOC_GPS_CAPABILITY_MSB);
210              break;
211           default:
212              break;
213         }
214     }
215 }
216 
getCarrierCapabilities()217 uint32_t ContextBase::getCarrierCapabilities() {
218     #define carrierMSA (uint32_t)0x2
219     #define carrierMSB (uint32_t)0x1
220     #define gpsConfMSA (uint32_t)0x4
221     #define gpsConfMSB (uint32_t)0x2
222     uint32_t capabilities = mGps_conf.CAPABILITIES;
223     if ((mGps_conf.SUPL_MODE & carrierMSA) != carrierMSA) {
224         capabilities &= ~gpsConfMSA;
225     }
226     if ((mGps_conf.SUPL_MODE & carrierMSB) != carrierMSB) {
227         capabilities &= ~gpsConfMSB;
228     }
229 
230     LOC_LOGV("getCarrierCapabilities: CAPABILITIES %x, SUPL_MODE %x, carrier capabilities %x",
231              mGps_conf.CAPABILITIES, mGps_conf.SUPL_MODE, capabilities);
232     return capabilities;
233 }
234 
getLBSProxy(const char * libName)235 LBSProxyBase* ContextBase::getLBSProxy(const char* libName)
236 {
237     LBSProxyBase* proxy = NULL;
238     LOC_LOGD("%s:%d]: getLBSProxy libname: %s\n", __func__, __LINE__, libName);
239     void* lib = dlopen(libName, RTLD_NOW);
240 
241     if ((void*)NULL != lib) {
242         getLBSProxy_t* getter = (getLBSProxy_t*)dlsym(lib, "getLBSProxy");
243         if (NULL != getter) {
244             proxy = (*getter)();
245         }
246     }
247     else
248     {
249         LOC_LOGW("%s:%d]: FAILED TO LOAD libname: %s\n", __func__, __LINE__, libName);
250     }
251     if (NULL == proxy) {
252         proxy = new LBSProxyBase();
253     }
254     LOC_LOGD("%s:%d]: Exiting\n", __func__, __LINE__);
255     return proxy;
256 }
257 
createLocApi(LOC_API_ADAPTER_EVENT_MASK_T exMask)258 LocApiBase* ContextBase::createLocApi(LOC_API_ADAPTER_EVENT_MASK_T exMask)
259 {
260     LocApiBase* locApi = NULL;
261     const char* libname = LOC_APIV2_0_LIB_NAME;
262 
263     // Check the target
264     if (TARGET_NO_GNSS != loc_get_target()){
265 
266         if (NULL == (locApi = mLBSProxy->getLocApi(exMask, this))) {
267             void *handle = NULL;
268 
269             if (IS_SS5_HW_ENABLED == mGps_conf.GNSS_DEPLOYMENT) {
270                 libname = SLL_LOC_API_LIB_NAME;
271             }
272 
273             if ((handle = dlopen(libname, RTLD_NOW)) != NULL) {
274                 LOC_LOGD("%s:%d]: %s is present", __func__, __LINE__, libname);
275                 getLocApi_t* getter = (getLocApi_t*) dlsym(handle, "getLocApi");
276                 if (getter != NULL) {
277                     LOC_LOGD("%s:%d]: getter is not NULL of %s", __func__,
278                             __LINE__, libname);
279                     locApi = (*getter)(exMask, this);
280                 }
281             }
282             // only RPC is the option now
283             else {
284                 LOC_LOGD("%s:%d]: libloc_api_v02.so is NOT present. Trying RPC",
285                         __func__, __LINE__);
286                 handle = dlopen("libloc_api-rpc-qc.so", RTLD_NOW);
287                 if (NULL != handle) {
288                     getLocApi_t* getter = (getLocApi_t*) dlsym(handle, "getLocApi");
289                     if (NULL != getter) {
290                         LOC_LOGD("%s:%d]: getter is not NULL in RPC", __func__,
291                                 __LINE__);
292                         locApi = (*getter)(exMask, this);
293                     }
294                 }
295             }
296         }
297     }
298 
299     // locApi could still be NULL at this time
300     // we would then create a dummy one
301     if (NULL == locApi) {
302         locApi = new LocApiBase(exMask, this);
303     }
304 
305     return locApi;
306 }
307 
ContextBase(const MsgTask * msgTask,LOC_API_ADAPTER_EVENT_MASK_T exMask,const char * libName)308 ContextBase::ContextBase(const MsgTask* msgTask,
309                          LOC_API_ADAPTER_EVENT_MASK_T exMask,
310                          const char* libName) :
311     mLBSProxy(getLBSProxy(libName)),
312     mMsgTask(msgTask),
313     mLocApi(createLocApi(exMask)),
314     mLocApiProxy(mLocApi->getLocApiProxy())
315 {
316 }
317 
setEngineCapabilities(uint64_t supportedMsgMask,uint8_t * featureList,bool gnssMeasurementSupported)318 void ContextBase::setEngineCapabilities(uint64_t supportedMsgMask,
319        uint8_t *featureList, bool gnssMeasurementSupported) {
320 
321     if (ContextBase::sIsEngineCapabilitiesKnown == false) {
322         ContextBase::sSupportedMsgMask = supportedMsgMask;
323         ContextBase::sGnssMeasurementSupported = gnssMeasurementSupported;
324         if (featureList != NULL) {
325             memcpy((void *)ContextBase::sFeaturesSupported,
326                     (void *)featureList, sizeof(ContextBase::sFeaturesSupported));
327         }
328 
329         ContextBase::sIsEngineCapabilitiesKnown = true;
330     }
331 }
332 
333 
isFeatureSupported(uint8_t featureVal)334 bool ContextBase::isFeatureSupported(uint8_t featureVal)
335 {
336     uint8_t arrayIndex = featureVal >> 3;
337     uint8_t bitPos = featureVal & 7;
338 
339     if (arrayIndex >= MAX_FEATURE_LENGTH) return false;
340     return ((ContextBase::sFeaturesSupported[arrayIndex] >> bitPos ) & 0x1);
341 }
342 
gnssConstellationConfig()343 bool ContextBase::gnssConstellationConfig() {
344     return sGnssMeasurementSupported;
345 }
346 
347 }
348