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