147ad5e4cf2f6810db3c0e7ec79696496a94b6f0dDante Russo/* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
2e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *
3e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * Redistribution and use in source and binary forms, with or without
4e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * modification, are permitted provided that the following conditions are
5e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * met:
6e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *     * Redistributions of source code must retain the above copyright
7e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *       notice, this list of conditions and the following disclaimer.
8e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *     * Redistributions in binary form must reproduce the above
9e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *       copyright notice, this list of conditions and the following
10e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *       disclaimer in the documentation and/or other materials provided
11e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *       with the distribution.
12e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *     * Neither the name of The Linux Foundation, nor the names of its
13e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *       contributors may be used to endorse or promote products derived
14e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *       from this software without specific prior written permission.
15e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *
16e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
17e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
23e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo *
28e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo */
29e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo#define LOG_NDDEBUG 0
30e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo#define LOG_TAG "LocSvc_CtxBase"
31e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo
32e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo#include <dlfcn.h>
33e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo#include <cutils/sched_policy.h>
34e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo#include <unistd.h>
35e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo#include <ContextBase.h>
36e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo#include <msg_q.h>
37e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo#include <loc_target.h>
38e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo#include <log_util.h>
39e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo#include <loc_log.h>
40e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo
41e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russonamespace loc_core {
42e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo
4347ad5e4cf2f6810db3c0e7ec79696496a94b6f0dDante RussoLBSProxyBase* ContextBase::getLBSProxy(const char* libName)
44e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo{
4547ad5e4cf2f6810db3c0e7ec79696496a94b6f0dDante Russo    LBSProxyBase* proxy = NULL;
4647ad5e4cf2f6810db3c0e7ec79696496a94b6f0dDante Russo    LOC_LOGD("%s:%d]: getLBSProxy libname: %s\n", __func__, __LINE__, libName);
47e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    void* lib = dlopen(libName, RTLD_NOW);
48e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo
49e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    if ((void*)NULL != lib) {
5047ad5e4cf2f6810db3c0e7ec79696496a94b6f0dDante Russo        getLBSProxy_t* getter = (getLBSProxy_t*)dlsym(lib, "getLBSProxy");
51e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo        if (NULL != getter) {
52e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo            proxy = (*getter)();
53e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo        }
54e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    }
55e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    if (NULL == proxy) {
5647ad5e4cf2f6810db3c0e7ec79696496a94b6f0dDante Russo        proxy = new LBSProxyBase();
57e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    }
5847ad5e4cf2f6810db3c0e7ec79696496a94b6f0dDante Russo    LOC_LOGD("%s:%d]: Exiting\n", __func__, __LINE__);
59e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    return proxy;
60e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo}
61e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo
62e14a6c846df2ce4bb1847e4250991f7c52fd793dDante RussoLocApiBase* ContextBase::createLocApi(LOC_API_ADAPTER_EVENT_MASK_T exMask)
63e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo{
64e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    LocApiBase* locApi = NULL;
65e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo
66e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    // first if can not be MPQ
675cf20a0736f15de30eb88b6f499937f7d8f06e13Dante Russo    if (TARGET_MPQ != loc_get_target()) {
685cf20a0736f15de30eb88b6f499937f7d8f06e13Dante Russo        if (NULL == (locApi = mLBSProxy->getLocApi(mMsgTask, exMask, this))) {
695527c156039207c49331142783c66b742f8464e5Mekala Natarajan            void *handle = NULL;
705527c156039207c49331142783c66b742f8464e5Mekala Natarajan            //try to see if LocApiV02 is present
715527c156039207c49331142783c66b742f8464e5Mekala Natarajan            if((handle = dlopen("libloc_api_v02.so", RTLD_NOW)) != NULL) {
725527c156039207c49331142783c66b742f8464e5Mekala Natarajan                LOC_LOGD("%s:%d]: libloc_api_v02.so is present", __func__, __LINE__);
73e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo                getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi");
745527c156039207c49331142783c66b742f8464e5Mekala Natarajan                if(getter != NULL) {
755527c156039207c49331142783c66b742f8464e5Mekala Natarajan                    LOC_LOGD("%s:%d]: getter is not NULL for LocApiV02", __func__, __LINE__);
76bfff6343845ad9ff062c5fd97bb3b9be1053340eDante Russo                    locApi = (*getter)(mMsgTask, exMask, this);
775527c156039207c49331142783c66b742f8464e5Mekala Natarajan                }
785527c156039207c49331142783c66b742f8464e5Mekala Natarajan            }
795527c156039207c49331142783c66b742f8464e5Mekala Natarajan            // only RPC is the option now
805527c156039207c49331142783c66b742f8464e5Mekala Natarajan            else {
815527c156039207c49331142783c66b742f8464e5Mekala Natarajan                LOC_LOGD("%s:%d]: libloc_api_v02.so is NOT present. Trying RPC",
825527c156039207c49331142783c66b742f8464e5Mekala Natarajan                         __func__, __LINE__);
835527c156039207c49331142783c66b742f8464e5Mekala Natarajan                handle = dlopen("libloc_api-rpc-qc.so", RTLD_NOW);
845527c156039207c49331142783c66b742f8464e5Mekala Natarajan                if (NULL != handle) {
855527c156039207c49331142783c66b742f8464e5Mekala Natarajan                    getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi");
865527c156039207c49331142783c66b742f8464e5Mekala Natarajan                    if (NULL != getter) {
87bfff6343845ad9ff062c5fd97bb3b9be1053340eDante Russo                        LOC_LOGD("%s:%d]: getter is not NULL in RPC", __func__, __LINE__);
885cf20a0736f15de30eb88b6f499937f7d8f06e13Dante Russo                        locApi = (*getter)(mMsgTask, exMask, this);
895527c156039207c49331142783c66b742f8464e5Mekala Natarajan                    }
90e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo                }
91e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo            }
92e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo        }
93e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    }
94e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo
95e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    // locApi could still be NULL at this time
96e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    // we would then create a dummy one
97e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    if (NULL == locApi) {
9847ad5e4cf2f6810db3c0e7ec79696496a94b6f0dDante Russo        locApi = new LocApiBase(mMsgTask, exMask, this);
99e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    }
100e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo
101e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    return locApi;
102e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo}
103e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo
104e14a6c846df2ce4bb1847e4250991f7c52fd793dDante RussoContextBase::ContextBase(const MsgTask* msgTask,
105e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo                         LOC_API_ADAPTER_EVENT_MASK_T exMask,
106e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo                         const char* libName) :
10747ad5e4cf2f6810db3c0e7ec79696496a94b6f0dDante Russo    mLBSProxy(getLBSProxy(libName)),
108e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo    mMsgTask(msgTask),
10947ad5e4cf2f6810db3c0e7ec79696496a94b6f0dDante Russo    mLocApi(createLocApi(exMask)),
11047ad5e4cf2f6810db3c0e7ec79696496a94b6f0dDante Russo    mLocApiProxy(mLocApi->getLocApiProxy())
111e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo{
112e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo}
113e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo
114e14a6c846df2ce4bb1847e4250991f7c52fd793dDante Russo}
115