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