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_DualCtx"
31 
32 #include <cutils/sched_policy.h>
33 #include <unistd.h>
34 #include <LocDualContext.h>
35 #include <msg_q.h>
36 #include <log_util.h>
37 #include <loc_log.h>
38 
39 namespace loc_core {
40 
41 // nothing exclude for foreground
42 const LOC_API_ADAPTER_EVENT_MASK_T
43 LocDualContext::mFgExclMask = 0;
44 // excluded events for background clients
45 const LOC_API_ADAPTER_EVENT_MASK_T
46 LocDualContext::mBgExclMask =
47     (LOC_API_ADAPTER_BIT_PARSED_POSITION_REPORT |
48      LOC_API_ADAPTER_BIT_SATELLITE_REPORT |
49      LOC_API_ADAPTER_BIT_NMEA_1HZ_REPORT |
50      LOC_API_ADAPTER_BIT_NMEA_POSITION_REPORT |
51      LOC_API_ADAPTER_BIT_IOCTL_REPORT |
52      LOC_API_ADAPTER_BIT_STATUS_REPORT |
53      LOC_API_ADAPTER_BIT_GEOFENCE_GEN_ALERT |
54      LOC_API_ADAPTER_BIT_GNSS_MEASUREMENT);
55 
56 const MsgTask* LocDualContext::mMsgTask = NULL;
57 ContextBase* LocDualContext::mFgContext = NULL;
58 ContextBase* LocDualContext::mBgContext = NULL;
59 ContextBase* LocDualContext::mInjectContext = NULL;
60 // the name must be shorter than 15 chars
61 const char* LocDualContext::mLocationHalName = "Loc_hal_worker";
62 const char* LocDualContext::mLBSLibName = "liblbs_core.so";
63 
getMsgTask(MsgTask::tCreate tCreator,const char * name)64 const MsgTask* LocDualContext::getMsgTask(MsgTask::tCreate tCreator,
65                                           const char* name)
66 {
67     if (NULL == mMsgTask) {
68         mMsgTask = new MsgTask(tCreator, name);
69     }
70     return mMsgTask;
71 }
72 
getMsgTask(MsgTask::tAssociate tAssociate,const char * name)73 const MsgTask* LocDualContext::getMsgTask(MsgTask::tAssociate tAssociate,
74                                           const char* name)
75 {
76     if (NULL == mMsgTask) {
77         mMsgTask = new MsgTask(tAssociate, name);
78     } else if (tAssociate) {
79         mMsgTask->associate(tAssociate);
80     }
81     return mMsgTask;
82 }
83 
getLocFgContext(MsgTask::tCreate tCreator,const char * name)84 ContextBase* LocDualContext::getLocFgContext(MsgTask::tCreate tCreator,
85                                              const char* name)
86 {
87     if (NULL == mFgContext) {
88         const MsgTask* msgTask = getMsgTask(tCreator, name);
89         mFgContext = new LocDualContext(msgTask,
90                                         mFgExclMask);
91     }
92     if(NULL == mInjectContext) {
93         mInjectContext = mFgContext;
94         injectFeatureConfig(mInjectContext);
95     }
96     return mFgContext;
97 }
98 
getLocFgContext(MsgTask::tAssociate tAssociate,const char * name)99 ContextBase* LocDualContext::getLocFgContext(MsgTask::tAssociate tAssociate,
100                                         const char* name)
101 {
102     if (NULL == mFgContext) {
103         const MsgTask* msgTask = getMsgTask(tAssociate, name);
104         mFgContext = new LocDualContext(msgTask,
105                                         mFgExclMask);
106     }
107     if(NULL == mInjectContext) {
108         LOC_LOGD("%s:%d]: mInjectContext is FgContext", __func__, __LINE__);
109         mInjectContext = mFgContext;
110         injectFeatureConfig(mInjectContext);
111     }
112     return mFgContext;
113 
114 }
115 
getLocBgContext(MsgTask::tCreate tCreator,const char * name)116 ContextBase* LocDualContext::getLocBgContext(MsgTask::tCreate tCreator,
117                                              const char* name)
118 {
119     if (NULL == mBgContext) {
120         const MsgTask* msgTask = getMsgTask(tCreator, name);
121         mBgContext = new LocDualContext(msgTask,
122                                         mBgExclMask);
123     }
124     if(NULL == mInjectContext) {
125         LOC_LOGD("%s:%d]: mInjectContext is BgContext", __func__, __LINE__);
126         mInjectContext = mBgContext;
127         injectFeatureConfig(mInjectContext);
128     }
129     return mBgContext;
130 }
131 
getLocBgContext(MsgTask::tAssociate tAssociate,const char * name)132 ContextBase* LocDualContext::getLocBgContext(MsgTask::tAssociate tAssociate,
133                                              const char* name)
134 {
135     if (NULL == mBgContext) {
136         const MsgTask* msgTask = getMsgTask(tAssociate, name);
137         mBgContext = new LocDualContext(msgTask,
138                                         mBgExclMask);
139     }
140     if(NULL == mInjectContext) {
141         LOC_LOGD("%s:%d]: mInjectContext is BgContext", __func__, __LINE__);
142         mInjectContext = mBgContext;
143         injectFeatureConfig(mInjectContext);
144     }
145     return mBgContext;
146 }
147 
injectFeatureConfig(ContextBase * curContext)148 void LocDualContext :: injectFeatureConfig(ContextBase *curContext)
149 {
150     LOC_LOGD("%s:%d]: Enter", __func__, __LINE__);
151     if(curContext == mInjectContext) {
152         LOC_LOGD("%s:%d]: Calling LBSProxy (%p) to inject feature config",
153                  __func__, __LINE__, ((LocDualContext *)mInjectContext)->mLBSProxy);
154         ((LocDualContext *)mInjectContext)->mLBSProxy->injectFeatureConfig(curContext);
155     }
156     LOC_LOGD("%s:%d]: Exit", __func__, __LINE__);
157 }
158 
LocDualContext(const MsgTask * msgTask,LOC_API_ADAPTER_EVENT_MASK_T exMask)159 LocDualContext::LocDualContext(const MsgTask* msgTask,
160                                LOC_API_ADAPTER_EVENT_MASK_T exMask) :
161     ContextBase(msgTask, exMask, mLBSLibName)
162 {
163 }
164 
165 }
166