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