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