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