1/* Copyright (c) 2011-2014, 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 <log_util.h>
39#include <loc_log.h>
40
41namespace loc_core {
42
43loc_gps_cfg_s_type ContextBase::mGps_conf {0};
44loc_sap_cfg_s_type ContextBase::mSap_conf {0};
45
46uint32_t ContextBase::getCarrierCapabilities() {
47    #define carrierMSA (uint32_t)0x2
48    #define carrierMSB (uint32_t)0x1
49    #define gpsConfMSA (uint32_t)0x4
50    #define gpsConfMSB (uint32_t)0x2
51    uint32_t capabilities = mGps_conf.CAPABILITIES;
52    if ((mGps_conf.SUPL_MODE & carrierMSA) != carrierMSA) {
53        capabilities &= ~gpsConfMSA;
54    }
55    if ((mGps_conf.SUPL_MODE & carrierMSB) != carrierMSB) {
56        capabilities &= ~gpsConfMSB;
57    }
58
59    LOC_LOGV("getCarrierCapabilities: CAPABILITIES %x, SUPL_MODE %x, carrier capabilities %x",
60             mGps_conf.CAPABILITIES, mGps_conf.SUPL_MODE, capabilities);
61    return capabilities;
62}
63
64LBSProxyBase* ContextBase::getLBSProxy(const char* libName)
65{
66    LBSProxyBase* proxy = NULL;
67    LOC_LOGD("%s:%d]: getLBSProxy libname: %s\n", __func__, __LINE__, libName);
68    void* lib = dlopen(libName, RTLD_NOW);
69
70    if ((void*)NULL != lib) {
71        getLBSProxy_t* getter = (getLBSProxy_t*)dlsym(lib, "getLBSProxy");
72        if (NULL != getter) {
73            proxy = (*getter)();
74        }
75    }
76    if (NULL == proxy) {
77        proxy = new LBSProxyBase();
78    }
79    LOC_LOGD("%s:%d]: Exiting\n", __func__, __LINE__);
80    return proxy;
81}
82
83LocApiBase* ContextBase::createLocApi(LOC_API_ADAPTER_EVENT_MASK_T exMask)
84{
85    LocApiBase* locApi = NULL;
86
87    // Check the target
88    if (TARGET_NO_GNSS != loc_get_target()){
89
90        if (NULL == (locApi = mLBSProxy->getLocApi(mMsgTask, exMask, this))) {
91            void *handle = NULL;
92            //try to see if LocApiV02 is present
93            if((handle = dlopen("libloc_api_v02.so", RTLD_NOW)) != NULL) {
94                LOC_LOGD("%s:%d]: libloc_api_v02.so is present", __func__, __LINE__);
95                getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi");
96                if(getter != NULL) {
97                    LOC_LOGD("%s:%d]: getter is not NULL for LocApiV02", __func__, __LINE__);
98                    locApi = (*getter)(mMsgTask, exMask, this);
99                }
100            }
101            // only RPC is the option now
102            else {
103                LOC_LOGD("%s:%d]: libloc_api_v02.so is NOT present. Trying RPC",
104                         __func__, __LINE__);
105                handle = dlopen("libloc_api-rpc-qc.so", RTLD_NOW);
106                if (NULL != handle) {
107                    getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi");
108                    if (NULL != getter) {
109                        LOC_LOGD("%s:%d]: getter is not NULL in RPC", __func__, __LINE__);
110                        locApi = (*getter)(mMsgTask, exMask, this);
111                    }
112                }
113            }
114        }
115    }
116
117    // locApi could still be NULL at this time
118    // we would then create a dummy one
119    if (NULL == locApi) {
120        locApi = new LocApiBase(mMsgTask, exMask, this);
121    }
122
123    return locApi;
124}
125
126ContextBase::ContextBase(const MsgTask* msgTask,
127                         LOC_API_ADAPTER_EVENT_MASK_T exMask,
128                         const char* libName) :
129    mLBSProxy(getLBSProxy(libName)),
130    mMsgTask(msgTask),
131    mLocApi(createLocApi(exMask)),
132    mLocApiProxy(mLocApi->getLocApiProxy())
133{
134}
135
136}
137