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 // first if can not be MPQ 88 if (TARGET_MPQ != loc_get_target()) { 89 if (NULL == (locApi = mLBSProxy->getLocApi(mMsgTask, exMask, this))) { 90 void *handle = NULL; 91 //try to see if LocApiV02 is present 92 if((handle = dlopen("libloc_api_v02.so", RTLD_NOW)) != NULL) { 93 LOC_LOGD("%s:%d]: libloc_api_v02.so is present", __func__, __LINE__); 94 getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi"); 95 if(getter != NULL) { 96 LOC_LOGD("%s:%d]: getter is not NULL for LocApiV02", __func__, __LINE__); 97 locApi = (*getter)(mMsgTask, exMask, this); 98 } 99 } 100 // only RPC is the option now 101 else { 102 LOC_LOGD("%s:%d]: libloc_api_v02.so is NOT present. Trying RPC", 103 __func__, __LINE__); 104 handle = dlopen("libloc_api-rpc-qc.so", RTLD_NOW); 105 if (NULL != handle) { 106 getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi"); 107 if (NULL != getter) { 108 LOC_LOGD("%s:%d]: getter is not NULL in RPC", __func__, __LINE__); 109 locApi = (*getter)(mMsgTask, exMask, this); 110 } 111 } 112 } 113 } 114 } 115 116 // locApi could still be NULL at this time 117 // we would then create a dummy one 118 if (NULL == locApi) { 119 locApi = new LocApiBase(mMsgTask, exMask, this); 120 } 121 122 return locApi; 123} 124 125ContextBase::ContextBase(const MsgTask* msgTask, 126 LOC_API_ADAPTER_EVENT_MASK_T exMask, 127 const char* libName) : 128 mLBSProxy(getLBSProxy(libName)), 129 mMsgTask(msgTask), 130 mLocApi(createLocApi(exMask)), 131 mLocApiProxy(mLocApi->getLocApiProxy()) 132{ 133} 134 135} 136