ContextBase.cpp revision e7c98642e1e156ea6cde1238cd0006f669cfb696
133873d2b41a5cd2597b20a4e88eb8942944c0f23Tyler Schultz/* Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
26eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz *
3563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher * Redistribution and use in source and binary forms, with or without
4563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher * modification, are permitted provided that the following conditions are
536c402b852afe30b13d9301e689d9d325db147baRyan Richard * met:
66eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz *     * Redistributions of source code must retain the above copyright
76eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz *       notice, this list of conditions and the following disclaimer.
822c22c9aa4ca68c2deac6164edc1d82bc9645310Christian Williams & Phil Goodwin *     * Redistributions in binary form must reproduce the above
922c22c9aa4ca68c2deac6164edc1d82bc9645310Christian Williams & Phil Goodwin *       copyright notice, this list of conditions and the following
1022c22c9aa4ca68c2deac6164edc1d82bc9645310Christian Williams & Phil Goodwin *       disclaimer in the documentation and/or other materials provided
116eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz *       with the distribution.
12688ca9c73aa3aa245ea231a7c94079c872568a91Christian Williams *     * Neither the name of The Linux Foundation, nor the names of its
13688ca9c73aa3aa245ea231a7c94079c872568a91Christian Williams *       contributors may be used to endorse or promote products derived
14688ca9c73aa3aa245ea231a7c94079c872568a91Christian Williams *       from this software without specific prior written permission.
1564dc72d042bc77f6d23c84a17711d5146eea66d8Aaron VonderHaar & Ryan Richard *
1664dc72d042bc77f6d23c84a17711d5146eea66d8Aaron VonderHaar & Ryan Richard * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
176eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180b378afcde0200166e546c46a7642015accfce49Christian Williams & Ian Fisher * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
19afe0a89d904a7fe2f5980b9deb26cc3240192459Christian Williams * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20f65d62587329aa2dbf2ecc3d73b70526f8f3b0f3Ryan Richard * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21f65d62587329aa2dbf2ecc3d73b70526f8f3b0f3Ryan Richard * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22f65d62587329aa2dbf2ecc3d73b70526f8f3b0f3Ryan Richard * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
234280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
244280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
25fe999f5d0538ad8a1abb0fc473020f1e6048c23eChristian Williams & Joe Moore * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
2685af7906879a68c99b1878dac22605fa32454f3fRich Humphrey * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
274280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky *
286eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz */
296eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz#define LOG_NDDEBUG 0
304280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky#define LOG_TAG "LocSvc_CtxBase"
31563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher
32964491a5d5046523f1ca8e9a3b30779420d79034Ryan Richard#include <dlfcn.h>
3385af7906879a68c99b1878dac22605fa32454f3fRich Humphrey#include <cutils/sched_policy.h>
346eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz#include <unistd.h>
35688ca9c73aa3aa245ea231a7c94079c872568a91Christian Williams#include <ContextBase.h>
36688ca9c73aa3aa245ea231a7c94079c872568a91Christian Williams#include <msg_q.h>
37de9feed5ddf1d91a3e76cbc71712c36a7652201eChristian Williams#include <loc_target.h>
386eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz#include <log_util.h>
396eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz#include <loc_log.h>
40ca8b4e7c96507b66cb816a3383c93b02d79a7685Tyler Schultz
41ca8b4e7c96507b66cb816a3383c93b02d79a7685Tyler Schultznamespace loc_core {
42ca8b4e7c96507b66cb816a3383c93b02d79a7685Tyler Schultz
43417f2e9fdde43fc6ad8a9a51750a8f4ccef343e7Rich HumphreyLBSProxyBase* ContextBase::getLBSProxy(const char* libName)
44563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher{
45563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher    LBSProxyBase* proxy = NULL;
46964491a5d5046523f1ca8e9a3b30779420d79034Ryan Richard    LOC_LOGD("%s:%d]: getLBSProxy libname: %s\n", __func__, __LINE__, libName);
47563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher    void* lib = dlopen(libName, RTLD_NOW);
484280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky
4985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    if ((void*)NULL != lib) {
5085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        getLBSProxy_t* getter = (getLBSProxy_t*)dlsym(lib, "getLBSProxy");
514280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky        if (NULL != getter) {
524280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky            proxy = (*getter)();
53800e363213bad2720ac3952bd84ec8a9b0142354Adam Cohen-Rose        }
54563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher    }
552d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonow    if (NULL == proxy) {
562d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonow        proxy = new LBSProxyBase();
574280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky    }
582d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonow    LOC_LOGD("%s:%d]: Exiting\n", __func__, __LINE__);
592d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonow    return proxy;
602d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonow}
612d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonow
622d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonowLocApiBase* ContextBase::createLocApi(LOC_API_ADAPTER_EVENT_MASK_T exMask)
632d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonow{
642d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonow    LocApiBase* locApi = NULL;
652d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonow
662d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonow    // first if can not be MPQ
672d79a2c39d1a2724536ace69697318b288eb05bdpivotal-intonow    if (TARGET_MPQ != loc_get_target()) {
6885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey        if (NULL == (locApi = mLBSProxy->getLocApi(mMsgTask, exMask, this))) {
69563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher            void *handle = NULL;
70563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher            //try to see if LocApiV02 is present
71563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher            if((handle = dlopen("libloc_api_v02.so", RTLD_NOW)) != NULL) {
7285af7906879a68c99b1878dac22605fa32454f3fRich Humphrey                LOC_LOGD("%s:%d]: libloc_api_v02.so is present", __func__, __LINE__);
734c2870226638c7af1cb5e228ee780d6655f52ec8Mike Stam & Phil Goodwin                getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi");
744c2870226638c7af1cb5e228ee780d6655f52ec8Mike Stam & Phil Goodwin                if(getter != NULL) {
754c2870226638c7af1cb5e228ee780d6655f52ec8Mike Stam & Phil Goodwin                    LOC_LOGD("%s:%d]: getter is not NULL for LocApiV02", __func__, __LINE__);
764280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky                    locApi = (*getter)(mMsgTask, exMask, this);
774c2870226638c7af1cb5e228ee780d6655f52ec8Mike Stam & Phil Goodwin                }
7838a4b26671ef6412959610ba8846d8c5cd69b108Robert Taylor            }
794c2870226638c7af1cb5e228ee780d6655f52ec8Mike Stam & Phil Goodwin            // only RPC is the option now
804c2870226638c7af1cb5e228ee780d6655f52ec8Mike Stam & Phil Goodwin            else {
81563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher                LOC_LOGD("%s:%d]: libloc_api_v02.so is NOT present. Trying RPC",
82563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher                         __func__, __LINE__);
83563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher                handle = dlopen("libloc_api-rpc-qc.so", RTLD_NOW);
84563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher                if (NULL != handle) {
85563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher                    getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi");
86563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher                    if (NULL != getter) {
87563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher                        LOC_LOGD("%s:%d]: getter is not NULL in RPC", __func__, __LINE__);
88563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher                        locApi = (*getter)(mMsgTask, exMask, this);
89563821eb6f69ee8158b44465d227b0ce35ad2ed4Christian Williams & Ian Fisher                    }
906eedf728138e6f79fa898ff3e31bf61c9b1151dChristian Williams & Tyler Schulz                }
914280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky            }
9238a4b26671ef6412959610ba8846d8c5cd69b108Robert Taylor        }
9338a4b26671ef6412959610ba8846d8c5cd69b108Robert Taylor    }
9438a4b26671ef6412959610ba8846d8c5cd69b108Robert Taylor
9538a4b26671ef6412959610ba8846d8c5cd69b108Robert Taylor    // locApi could still be NULL at this time
9685af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    // we would then create a dummy one
974280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky    if (NULL == locApi) {
984280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky        locApi = new LocApiBase(mMsgTask, exMask, this);
994280391c722888459a19312922860f34e0cff820Greg Choi & Lenny Turetsky    }
10085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
10185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    return locApi;
10285af7906879a68c99b1878dac22605fa32454f3fRich Humphrey}
10385af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
10485af7906879a68c99b1878dac22605fa32454f3fRich HumphreyContextBase::ContextBase(const MsgTask* msgTask,
10585af7906879a68c99b1878dac22605fa32454f3fRich Humphrey                         LOC_API_ADAPTER_EVENT_MASK_T exMask,
10685af7906879a68c99b1878dac22605fa32454f3fRich Humphrey                         const char* libName) :
10785af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    mLBSProxy(getLBSProxy(libName)),
10885af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    mMsgTask(msgTask),
10985af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    mLocApi(createLocApi(exMask)),
11085af7906879a68c99b1878dac22605fa32454f3fRich Humphrey    mLocApiProxy(mLocApi->getLocApiProxy())
11185af7906879a68c99b1878dac22605fa32454f3fRich Humphrey{
11285af7906879a68c99b1878dac22605fa32454f3fRich Humphrey}
11385af7906879a68c99b1878dac22605fa32454f3fRich Humphrey
11485af7906879a68c99b1878dac22605fa32454f3fRich Humphrey}
11585af7906879a68c99b1878dac22605fa32454f3fRich Humphrey