1 /* Copyright (c) 2011-2014, 2016-2017 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_NDEBUG 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 <platform_lib_log_util.h>
37 #include <loc_log.h>
38 #include <SystemStatus.h>
39
40 namespace loc_core {
41
42 // nothing exclude for foreground
43 const LOC_API_ADAPTER_EVENT_MASK_T
44 LocDualContext::mFgExclMask = 0;
45 // excluded events for background clients
46 const LOC_API_ADAPTER_EVENT_MASK_T
47 LocDualContext::mBgExclMask =
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 SystemStatus* LocDualContext::mSystemStatus = NULL;
61 // the name must be shorter than 15 chars
62 const char* LocDualContext::mLocationHalName = "Loc_hal_worker";
63 #ifndef USE_GLIB
64 const char* LocDualContext::mLBSLibName = "liblbs_core.so";
65 #else
66 const char* LocDualContext::mLBSLibName = "liblbs_core.so.1";
67 #endif
68
69 pthread_mutex_t LocDualContext::mGetLocContextMutex = PTHREAD_MUTEX_INITIALIZER;
70
getMsgTask(LocThread::tCreate tCreator,const char * name,bool joinable)71 const MsgTask* LocDualContext::getMsgTask(LocThread::tCreate tCreator,
72 const char* name, bool joinable)
73 {
74 if (NULL == mMsgTask) {
75 mMsgTask = new MsgTask(tCreator, name, joinable);
76 }
77 return mMsgTask;
78 }
79
80 inline
getMsgTask(const char * name,bool joinable)81 const MsgTask* LocDualContext::getMsgTask(const char* name, bool joinable) {
82 return getMsgTask((LocThread::tCreate)NULL, name, joinable);
83 }
84
getLocFgContext(LocThread::tCreate tCreator,LocMsg * firstMsg,const char * name,bool joinable)85 ContextBase* LocDualContext::getLocFgContext(LocThread::tCreate tCreator,
86 LocMsg* firstMsg, const char* name, bool joinable)
87 {
88 pthread_mutex_lock(&LocDualContext::mGetLocContextMutex);
89 LOC_LOGD("%s:%d]: querying ContextBase with tCreator", __func__, __LINE__);
90 if (NULL == mFgContext) {
91 LOC_LOGD("%s:%d]: creating msgTask with tCreator", __func__, __LINE__);
92 const MsgTask* msgTask = getMsgTask(tCreator, name, joinable);
93 mFgContext = new LocDualContext(msgTask,
94 mFgExclMask);
95 }
96 if(NULL == mInjectContext) {
97 LOC_LOGD("%s:%d]: mInjectContext is FgContext", __func__, __LINE__);
98 mInjectContext = mFgContext;
99 injectFeatureConfig(mInjectContext);
100 }
101 pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex);
102
103 if (firstMsg) {
104 mFgContext->sendMsg(firstMsg);
105 }
106
107 return mFgContext;
108 }
109
getLocBgContext(LocThread::tCreate tCreator,LocMsg * firstMsg,const char * name,bool joinable)110 ContextBase* LocDualContext::getLocBgContext(LocThread::tCreate tCreator,
111 LocMsg* firstMsg, const char* name, bool joinable)
112 {
113 pthread_mutex_lock(&LocDualContext::mGetLocContextMutex);
114 LOC_LOGD("%s:%d]: querying ContextBase with tCreator", __func__, __LINE__);
115 if (NULL == mBgContext) {
116 LOC_LOGD("%s:%d]: creating msgTask with tCreator", __func__, __LINE__);
117 const MsgTask* msgTask = getMsgTask(tCreator, name, joinable);
118 mBgContext = new LocDualContext(msgTask,
119 mBgExclMask);
120 }
121 if(NULL == mInjectContext) {
122 LOC_LOGD("%s:%d]: mInjectContext is BgContext", __func__, __LINE__);
123 mInjectContext = mBgContext;
124 injectFeatureConfig(mInjectContext);
125 }
126 pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex);
127
128 if (firstMsg) {
129 mBgContext->sendMsg(firstMsg);
130 }
131
132 return mBgContext;
133 }
134
injectFeatureConfig(ContextBase * curContext)135 void LocDualContext :: injectFeatureConfig(ContextBase *curContext)
136 {
137 LOC_LOGD("%s:%d]: Enter", __func__, __LINE__);
138 if(curContext == mInjectContext) {
139 LOC_LOGD("%s:%d]: Calling LBSProxy (%p) to inject feature config",
140 __func__, __LINE__, ((LocDualContext *)mInjectContext)->mLBSProxy);
141 ((LocDualContext *)mInjectContext)->mLBSProxy->injectFeatureConfig(curContext);
142 }
143 LOC_LOGD("%s:%d]: Exit", __func__, __LINE__);
144 }
145
LocDualContext(const MsgTask * msgTask,LOC_API_ADAPTER_EVENT_MASK_T exMask)146 LocDualContext::LocDualContext(const MsgTask* msgTask,
147 LOC_API_ADAPTER_EVENT_MASK_T exMask) :
148 ContextBase(msgTask, exMask, mLBSLibName)
149 {
150 }
151
getSystemStatus(void)152 SystemStatus* LocDualContext::getSystemStatus(void)
153 {
154 pthread_mutex_lock(&LocDualContext::mGetLocContextMutex);
155 if (NULL == mSystemStatus) {
156 mSystemStatus = new SystemStatus();
157 }
158 pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex);
159 return mSystemStatus;
160 }
161
162 }
163