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 <platform_lib_includes.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 else 77 { 78 LOC_LOGW("%s:%d]: FAILED TO LOAD libname: %s\n", __func__, __LINE__, libName); 79 } 80 if (NULL == proxy) { 81 proxy = new LBSProxyBase(); 82 } 83 LOC_LOGD("%s:%d]: Exiting\n", __func__, __LINE__); 84 return proxy; 85} 86 87LocApiBase* ContextBase::createLocApi(LOC_API_ADAPTER_EVENT_MASK_T exMask) 88{ 89 LocApiBase* locApi = NULL; 90 91 // Check the target 92 if (TARGET_NO_GNSS != loc_get_target()){ 93 94 if (NULL == (locApi = mLBSProxy->getLocApi(mMsgTask, exMask, this))) { 95 void *handle = NULL; 96 //try to see if LocApiV02 is present 97 if ((handle = dlopen("libloc_api_v02.so", RTLD_NOW)) != NULL) { 98 LOC_LOGD("%s:%d]: libloc_api_v02.so is present", __func__, __LINE__); 99 getLocApi_t* getter = (getLocApi_t*) dlsym(handle, "getLocApi"); 100 if (getter != NULL) { 101 LOC_LOGD("%s:%d]: getter is not NULL for LocApiV02", __func__, 102 __LINE__); 103 locApi = (*getter)(mMsgTask, exMask, this); 104 } 105 } 106 // only RPC is the option now 107 else { 108 LOC_LOGD("%s:%d]: libloc_api_v02.so is NOT present. Trying RPC", 109 __func__, __LINE__); 110 handle = dlopen("libloc_api-rpc-qc.so", RTLD_NOW); 111 if (NULL != handle) { 112 getLocApi_t* getter = (getLocApi_t*) dlsym(handle, "getLocApi"); 113 if (NULL != getter) { 114 LOC_LOGD("%s:%d]: getter is not NULL in RPC", __func__, 115 __LINE__); 116 locApi = (*getter)(mMsgTask, exMask, this); 117 } 118 } 119 } 120 } 121 } 122 123 // locApi could still be NULL at this time 124 // we would then create a dummy one 125 if (NULL == locApi) { 126 locApi = new LocApiBase(mMsgTask, exMask, this); 127 } 128 129 return locApi; 130} 131 132ContextBase::ContextBase(const MsgTask* msgTask, 133 LOC_API_ADAPTER_EVENT_MASK_T exMask, 134 const char* libName) : 135 mLBSProxy(getLBSProxy(libName)), 136 mMsgTask(msgTask), 137 mLocApi(createLocApi(exMask)), 138 mLocApiProxy(mLocApi->getLocApiProxy()) 139{ 140} 141 142} 143