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