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 
41 namespace loc_core {
42 
getLBSProxy(const char * libName)43 LBSProxyBase* ContextBase::getLBSProxy(const char* libName)
44 {
45     LBSProxyBase* proxy = NULL;
46     LOC_LOGD("%s:%d]: getLBSProxy libname: %s\n", __func__, __LINE__, libName);
47     void* lib = dlopen(libName, RTLD_NOW);
48 
49     if ((void*)NULL != lib) {
50         getLBSProxy_t* getter = (getLBSProxy_t*)dlsym(lib, "getLBSProxy");
51         if (NULL != getter) {
52             proxy = (*getter)();
53         }
54     }
55     if (NULL == proxy) {
56         proxy = new LBSProxyBase();
57     }
58     LOC_LOGD("%s:%d]: Exiting\n", __func__, __LINE__);
59     return proxy;
60 }
61 
createLocApi(LOC_API_ADAPTER_EVENT_MASK_T exMask)62 LocApiBase* ContextBase::createLocApi(LOC_API_ADAPTER_EVENT_MASK_T exMask)
63 {
64     LocApiBase* locApi = NULL;
65 
66     // first if can not be MPQ
67     if (TARGET_MPQ != loc_get_target()) {
68         if (NULL == (locApi = mLBSProxy->getLocApi(mMsgTask, exMask, this))) {
69             void *handle = NULL;
70             //try to see if LocApiV02 is present
71             if((handle = dlopen("libloc_api_v02.so", RTLD_NOW)) != NULL) {
72                 LOC_LOGD("%s:%d]: libloc_api_v02.so is present", __func__, __LINE__);
73                 getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi");
74                 if(getter != NULL) {
75                     LOC_LOGD("%s:%d]: getter is not NULL for LocApiV02", __func__, __LINE__);
76                     locApi = (*getter)(mMsgTask, exMask, this);
77                 }
78             }
79             // only RPC is the option now
80             else {
81                 LOC_LOGD("%s:%d]: libloc_api_v02.so is NOT present. Trying RPC",
82                          __func__, __LINE__);
83                 handle = dlopen("libloc_api-rpc-qc.so", RTLD_NOW);
84                 if (NULL != handle) {
85                     getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi");
86                     if (NULL != getter) {
87                         LOC_LOGD("%s:%d]: getter is not NULL in RPC", __func__, __LINE__);
88                         locApi = (*getter)(mMsgTask, exMask, this);
89                     }
90                 }
91             }
92         }
93     }
94 
95     // locApi could still be NULL at this time
96     // we would then create a dummy one
97     if (NULL == locApi) {
98         locApi = new LocApiBase(mMsgTask, exMask, this);
99     }
100 
101     return locApi;
102 }
103 
ContextBase(const MsgTask * msgTask,LOC_API_ADAPTER_EVENT_MASK_T exMask,const char * libName)104 ContextBase::ContextBase(const MsgTask* msgTask,
105                          LOC_API_ADAPTER_EVENT_MASK_T exMask,
106                          const char* libName) :
107     mLBSProxy(getLBSProxy(libName)),
108     mMsgTask(msgTask),
109     mLocApi(createLocApi(exMask)),
110     mLocApiProxy(mLocApi->getLocApiProxy())
111 {
112 }
113 
114 }
115