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",  &mGps_conf.USE_EMERGENCY_PDN_FOR_EMERGENCY_SUPL,          NULL, 'n'},
74   {"AGPS_CONFIG_INJECT",             &mGps_conf.AGPS_CONFIG_INJECT,             NULL, 'n'},
75   {"EXTERNAL_DR_ENABLED",            &mGps_conf.EXTERNAL_DR_ENABLED,                  NULL, 'n'},
76   {"SUPL_HOST",                      &mGps_conf.SUPL_HOST,                      NULL, 's'},
77   {"SUPL_PORT",                      &mGps_conf.SUPL_PORT,                      NULL, 'n'},
78   {"MODEM_TYPE",                     &mGps_conf.MODEM_TYPE,                     NULL, 'n' },
79   {"MO_SUPL_HOST",                   &mGps_conf.MO_SUPL_HOST,                   NULL, 's' },
80   {"MO_SUPL_PORT",                   &mGps_conf.MO_SUPL_PORT,                   NULL, 'n' },
81   {"CONSTRAINED_TIME_UNCERTAINTY_ENABLED",       &mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_ENABLED,      NULL, 'n'},
82   {"CONSTRAINED_TIME_UNCERTAINTY_THRESHOLD",     &mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_THRESHOLD,    NULL, 'f'},
83   {"CONSTRAINED_TIME_UNCERTAINTY_ENERGY_BUDGET", &mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_ENERGY_BUDGET, NULL, 'n'},
84   {"POSITION_ASSISTED_CLOCK_ESTIMATOR_ENABLED",  &mGps_conf.POSITION_ASSISTED_CLOCK_ESTIMATOR_ENABLED, NULL, 'n'},
85   {"PROXY_APP_PACKAGE_NAME",         &mGps_conf.PROXY_APP_PACKAGE_NAME,         NULL, 's' },
86   {"CP_MTLR_ES",                     &mGps_conf.CP_MTLR_ES,                     NULL, 'n' },
87   {"GNSS_DEPLOYMENT",  &mGps_conf.GNSS_DEPLOYMENT, NULL, 'n'},
88 };
89 
90 const loc_param_s_type ContextBase::mSap_conf_table[] =
91 {
92   {"GYRO_BIAS_RANDOM_WALK",          &mSap_conf.GYRO_BIAS_RANDOM_WALK,          &mSap_conf.GYRO_BIAS_RANDOM_WALK_VALID, 'f'},
93   {"ACCEL_RANDOM_WALK_SPECTRAL_DENSITY",     &mSap_conf.ACCEL_RANDOM_WALK_SPECTRAL_DENSITY,    &mSap_conf.ACCEL_RANDOM_WALK_SPECTRAL_DENSITY_VALID, 'f'},
94   {"ANGLE_RANDOM_WALK_SPECTRAL_DENSITY",     &mSap_conf.ANGLE_RANDOM_WALK_SPECTRAL_DENSITY,    &mSap_conf.ANGLE_RANDOM_WALK_SPECTRAL_DENSITY_VALID, 'f'},
95   {"RATE_RANDOM_WALK_SPECTRAL_DENSITY",      &mSap_conf.RATE_RANDOM_WALK_SPECTRAL_DENSITY,     &mSap_conf.RATE_RANDOM_WALK_SPECTRAL_DENSITY_VALID, 'f'},
96   {"VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY",  &mSap_conf.VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY, &mSap_conf.VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY_VALID, 'f'},
97   {"SENSOR_ACCEL_BATCHES_PER_SEC",   &mSap_conf.SENSOR_ACCEL_BATCHES_PER_SEC,   NULL, 'n'},
98   {"SENSOR_ACCEL_SAMPLES_PER_BATCH", &mSap_conf.SENSOR_ACCEL_SAMPLES_PER_BATCH, NULL, 'n'},
99   {"SENSOR_GYRO_BATCHES_PER_SEC",    &mSap_conf.SENSOR_GYRO_BATCHES_PER_SEC,    NULL, 'n'},
100   {"SENSOR_GYRO_SAMPLES_PER_BATCH",  &mSap_conf.SENSOR_GYRO_SAMPLES_PER_BATCH,  NULL, 'n'},
101   {"SENSOR_ACCEL_BATCHES_PER_SEC_HIGH",   &mSap_conf.SENSOR_ACCEL_BATCHES_PER_SEC_HIGH,   NULL, 'n'},
102   {"SENSOR_ACCEL_SAMPLES_PER_BATCH_HIGH", &mSap_conf.SENSOR_ACCEL_SAMPLES_PER_BATCH_HIGH, NULL, 'n'},
103   {"SENSOR_GYRO_BATCHES_PER_SEC_HIGH",    &mSap_conf.SENSOR_GYRO_BATCHES_PER_SEC_HIGH,    NULL, 'n'},
104   {"SENSOR_GYRO_SAMPLES_PER_BATCH_HIGH",  &mSap_conf.SENSOR_GYRO_SAMPLES_PER_BATCH_HIGH,  NULL, 'n'},
105   {"SENSOR_CONTROL_MODE",            &mSap_conf.SENSOR_CONTROL_MODE,            NULL, 'n'},
106   {"SENSOR_ALGORITHM_CONFIG_MASK",   &mSap_conf.SENSOR_ALGORITHM_CONFIG_MASK,   NULL, 'n'}
107 };
108 
readConfig()109 void ContextBase::readConfig()
110 {
111     static bool confReadDone = false;
112     if (!confReadDone) {
113         confReadDone = true;
114         /*Defaults for gps.conf*/
115         mGps_conf.INTERMEDIATE_POS = 0;
116         mGps_conf.ACCURACY_THRES = 0;
117         mGps_conf.NMEA_PROVIDER = 0;
118         mGps_conf.GPS_LOCK = GNSS_CONFIG_GPS_LOCK_MO_AND_NI;
119         mGps_conf.SUPL_VER = 0x10000;
120         mGps_conf.SUPL_MODE = 0x1;
121         mGps_conf.SUPL_ES = 0;
122         mGps_conf.CP_MTLR_ES = 0;
123         mGps_conf.SUPL_HOST[0] = 0;
124         mGps_conf.SUPL_PORT = 0;
125         mGps_conf.CAPABILITIES = 0x7;
126         /* LTE Positioning Profile configuration is disable by default*/
127         mGps_conf.LPP_PROFILE = 0;
128         /*By default no positioning protocol is selected on A-GLONASS system*/
129         mGps_conf.A_GLONASS_POS_PROTOCOL_SELECT = 0;
130         /*XTRA version check is disabled by default*/
131         mGps_conf.XTRA_VERSION_CHECK=0;
132         /*Use emergency PDN by default*/
133         mGps_conf.USE_EMERGENCY_PDN_FOR_EMERGENCY_SUPL = 1;
134         /* By default no LPPe CP technology is enabled*/
135         mGps_conf.LPPE_CP_TECHNOLOGY = 0;
136         /* By default no LPPe UP technology is enabled*/
137         mGps_conf.LPPE_UP_TECHNOLOGY = 0;
138         /* By default we use unknown modem type*/
139         mGps_conf.MODEM_TYPE = 2;
140 
141         /*Defaults for sap.conf*/
142         mSap_conf.GYRO_BIAS_RANDOM_WALK = 0;
143         mSap_conf.SENSOR_ACCEL_BATCHES_PER_SEC = 2;
144         mSap_conf.SENSOR_ACCEL_SAMPLES_PER_BATCH = 5;
145         mSap_conf.SENSOR_GYRO_BATCHES_PER_SEC = 2;
146         mSap_conf.SENSOR_GYRO_SAMPLES_PER_BATCH = 5;
147         mSap_conf.SENSOR_ACCEL_BATCHES_PER_SEC_HIGH = 4;
148         mSap_conf.SENSOR_ACCEL_SAMPLES_PER_BATCH_HIGH = 25;
149         mSap_conf.SENSOR_GYRO_BATCHES_PER_SEC_HIGH = 4;
150         mSap_conf.SENSOR_GYRO_SAMPLES_PER_BATCH_HIGH = 25;
151         mSap_conf.SENSOR_CONTROL_MODE = 0; /* AUTO */
152         mSap_conf.SENSOR_ALGORITHM_CONFIG_MASK = 0; /* INS Disabled = FALSE*/
153         /* Values MUST be set by OEMs in configuration for sensor-assisted
154           navigation to work. There are NO default values */
155         mSap_conf.ACCEL_RANDOM_WALK_SPECTRAL_DENSITY = 0;
156         mSap_conf.ANGLE_RANDOM_WALK_SPECTRAL_DENSITY = 0;
157         mSap_conf.RATE_RANDOM_WALK_SPECTRAL_DENSITY = 0;
158         mSap_conf.VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY = 0;
159         mSap_conf.GYRO_BIAS_RANDOM_WALK_VALID = 0;
160         mSap_conf.ACCEL_RANDOM_WALK_SPECTRAL_DENSITY_VALID = 0;
161         mSap_conf.ANGLE_RANDOM_WALK_SPECTRAL_DENSITY_VALID = 0;
162         mSap_conf.RATE_RANDOM_WALK_SPECTRAL_DENSITY_VALID = 0;
163         mSap_conf.VELOCITY_RANDOM_WALK_SPECTRAL_DENSITY_VALID = 0;
164 
165         /* None of the 10 slots for agps certificates are writable by default */
166         mGps_conf.AGPS_CERT_WRITABLE_MASK = 0;
167 
168         /* inject supl config to modem with config values from config.xml or gps.conf, default 1 */
169         mGps_conf.AGPS_CONFIG_INJECT = 1;
170 
171         /* default configuration value of constrained time uncertainty mode:
172            feature disabled, time uncertainty threshold defined by modem,
173            and unlimited power budget */
174         mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_ENABLED = 0;
175         mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_THRESHOLD = 0.0;
176         mGps_conf.CONSTRAINED_TIME_UNCERTAINTY_ENERGY_BUDGET = 0;
177         /* default configuration value of position assisted clock estimator mode */
178         mGps_conf.POSITION_ASSISTED_CLOCK_ESTIMATOR_ENABLED = 0;
179         /* default configuration QTI GNSS H/W */
180         mGps_conf.GNSS_DEPLOYMENT = 0;
181 
182         UTIL_READ_CONF(LOC_PATH_GPS_CONF, mGps_conf_table);
183         UTIL_READ_CONF(LOC_PATH_SAP_CONF, mSap_conf_table);
184 
185         LOC_LOGI("%s] GNSS Deployment: %s", __FUNCTION__,
186                 ((mGps_conf.GNSS_DEPLOYMENT == 1) ? "SS5" :
187                 ((mGps_conf.GNSS_DEPLOYMENT == 2) ? "QFUSION" : "QGNSS")));
188 
189         switch (getTargetGnssType(loc_get_target())) {
190           case GNSS_GSS:
191           case GNSS_AUTO:
192              // For APQ targets, MSA/MSB capabilities should be reset
193              mGps_conf.CAPABILITIES &= ~(LOC_GPS_CAPABILITY_MSA | LOC_GPS_CAPABILITY_MSB);
194              break;
195           default:
196              break;
197         }
198     }
199 }
200 
getCarrierCapabilities()201 uint32_t ContextBase::getCarrierCapabilities() {
202     #define carrierMSA (uint32_t)0x2
203     #define carrierMSB (uint32_t)0x1
204     #define gpsConfMSA (uint32_t)0x4
205     #define gpsConfMSB (uint32_t)0x2
206     uint32_t capabilities = mGps_conf.CAPABILITIES;
207     if ((mGps_conf.SUPL_MODE & carrierMSA) != carrierMSA) {
208         capabilities &= ~gpsConfMSA;
209     }
210     if ((mGps_conf.SUPL_MODE & carrierMSB) != carrierMSB) {
211         capabilities &= ~gpsConfMSB;
212     }
213 
214     LOC_LOGV("getCarrierCapabilities: CAPABILITIES %x, SUPL_MODE %x, carrier capabilities %x",
215              mGps_conf.CAPABILITIES, mGps_conf.SUPL_MODE, capabilities);
216     return capabilities;
217 }
218 
getLBSProxy(const char * libName)219 LBSProxyBase* ContextBase::getLBSProxy(const char* libName)
220 {
221     LBSProxyBase* proxy = NULL;
222     LOC_LOGD("%s:%d]: getLBSProxy libname: %s\n", __func__, __LINE__, libName);
223     void* lib = dlopen(libName, RTLD_NOW);
224 
225     if ((void*)NULL != lib) {
226         getLBSProxy_t* getter = (getLBSProxy_t*)dlsym(lib, "getLBSProxy");
227         if (NULL != getter) {
228             proxy = (*getter)();
229         }
230     }
231     else
232     {
233         LOC_LOGW("%s:%d]: FAILED TO LOAD libname: %s\n", __func__, __LINE__, libName);
234     }
235     if (NULL == proxy) {
236         proxy = new LBSProxyBase();
237     }
238     LOC_LOGD("%s:%d]: Exiting\n", __func__, __LINE__);
239     return proxy;
240 }
241 
createLocApi(LOC_API_ADAPTER_EVENT_MASK_T exMask)242 LocApiBase* ContextBase::createLocApi(LOC_API_ADAPTER_EVENT_MASK_T exMask)
243 {
244     LocApiBase* locApi = NULL;
245     const char* libname = LOC_APIV2_0_LIB_NAME;
246 
247     // Check the target
248     if (TARGET_NO_GNSS != loc_get_target()){
249 
250         if (NULL == (locApi = mLBSProxy->getLocApi(exMask, this))) {
251             void *handle = NULL;
252 
253             if (IS_SS5_HW_ENABLED == mGps_conf.GNSS_DEPLOYMENT) {
254                 libname = SLL_LOC_API_LIB_NAME;
255             }
256 
257             if ((handle = dlopen(libname, RTLD_NOW)) != NULL) {
258                 LOC_LOGD("%s:%d]: %s is present", __func__, __LINE__, libname);
259                 getLocApi_t* getter = (getLocApi_t*) dlsym(handle, "getLocApi");
260                 if (getter != NULL) {
261                     LOC_LOGD("%s:%d]: getter is not NULL of %s", __func__,
262                             __LINE__, libname);
263                     locApi = (*getter)(exMask, this);
264                 }
265             }
266             // only RPC is the option now
267             else {
268                 LOC_LOGD("%s:%d]: libloc_api_v02.so is NOT present. Trying RPC",
269                         __func__, __LINE__);
270                 handle = dlopen("libloc_api-rpc-qc.so", RTLD_NOW);
271                 if (NULL != handle) {
272                     getLocApi_t* getter = (getLocApi_t*) dlsym(handle, "getLocApi");
273                     if (NULL != getter) {
274                         LOC_LOGD("%s:%d]: getter is not NULL in RPC", __func__,
275                                 __LINE__);
276                         locApi = (*getter)(exMask, this);
277                     }
278                 }
279             }
280         }
281     }
282 
283     // locApi could still be NULL at this time
284     // we would then create a dummy one
285     if (NULL == locApi) {
286         locApi = new LocApiBase(exMask, this);
287     }
288 
289     return locApi;
290 }
291 
ContextBase(const MsgTask * msgTask,LOC_API_ADAPTER_EVENT_MASK_T exMask,const char * libName)292 ContextBase::ContextBase(const MsgTask* msgTask,
293                          LOC_API_ADAPTER_EVENT_MASK_T exMask,
294                          const char* libName) :
295     mLBSProxy(getLBSProxy(libName)),
296     mMsgTask(msgTask),
297     mLocApi(createLocApi(exMask)),
298     mLocApiProxy(mLocApi->getLocApiProxy())
299 {
300 }
301 
setEngineCapabilities(uint64_t supportedMsgMask,uint8_t * featureList,bool gnssMeasurementSupported)302 void ContextBase::setEngineCapabilities(uint64_t supportedMsgMask,
303        uint8_t *featureList, bool gnssMeasurementSupported) {
304 
305     if (ContextBase::sIsEngineCapabilitiesKnown == false) {
306         ContextBase::sSupportedMsgMask = supportedMsgMask;
307         ContextBase::sGnssMeasurementSupported = gnssMeasurementSupported;
308         if (featureList != NULL) {
309             memcpy((void *)ContextBase::sFeaturesSupported,
310                     (void *)featureList, sizeof(ContextBase::sFeaturesSupported));
311         }
312 
313         ContextBase::sIsEngineCapabilitiesKnown = true;
314     }
315 }
316 
317 
isFeatureSupported(uint8_t featureVal)318 bool ContextBase::isFeatureSupported(uint8_t featureVal)
319 {
320     uint8_t arrayIndex = featureVal >> 3;
321     uint8_t bitPos = featureVal & 7;
322 
323     if (arrayIndex >= MAX_FEATURE_LENGTH) return false;
324     return ((ContextBase::sFeaturesSupported[arrayIndex] >> bitPos ) & 0x1);
325 }
326 
gnssConstellationConfig()327 bool ContextBase::gnssConstellationConfig() {
328     return sGnssMeasurementSupported;
329 }
330 
331 }
332