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