• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     // Check the target
67     if (TARGET_NO_GNSS != loc_get_target()){
68 
69         if (NULL == (locApi = mLBSProxy->getLocApi(mMsgTask, exMask, this))) {
70             void *handle = NULL;
71             //try to see if LocApiV02 is present
72             if((handle = dlopen("libloc_api_v02.so", RTLD_NOW)) != NULL) {
73                 LOC_LOGD("%s:%d]: libloc_api_v02.so is present", __func__, __LINE__);
74                 getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi");
75                 if(getter != NULL) {
76                     LOC_LOGD("%s:%d]: getter is not NULL for LocApiV02", __func__, __LINE__);
77                     locApi = (*getter)(mMsgTask, exMask, this);
78                 }
79             }
80             // only RPC is the option now
81             else {
82                 LOC_LOGD("%s:%d]: libloc_api_v02.so is NOT present. Trying RPC",
83                          __func__, __LINE__);
84                 handle = dlopen("libloc_api-rpc-qc.so", RTLD_NOW);
85                 if (NULL != handle) {
86                     getLocApi_t* getter = (getLocApi_t*)dlsym(handle, "getLocApi");
87                     if (NULL != getter) {
88                         LOC_LOGD("%s:%d]: getter is not NULL in RPC", __func__, __LINE__);
89                         locApi = (*getter)(mMsgTask, exMask, this);
90                     }
91                 }
92             }
93         }
94     }
95 
96     // locApi could still be NULL at this time
97     // we would then create a dummy one
98     if (NULL == locApi) {
99         locApi = new LocApiBase(mMsgTask, exMask, this);
100     }
101 
102     return locApi;
103 }
104 
ContextBase(const MsgTask * msgTask,LOC_API_ADAPTER_EVENT_MASK_T exMask,const char * libName)105 ContextBase::ContextBase(const MsgTask* msgTask,
106                          LOC_API_ADAPTER_EVENT_MASK_T exMask,
107                          const char* libName) :
108     mLBSProxy(getLBSProxy(libName)),
109     mMsgTask(msgTask),
110     mLocApi(createLocApi(exMask)),
111     mLocApiProxy(mLocApi->getLocApiProxy())
112 {
113 }
114 
115 }
116