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 <platform_lib_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     LOC_API_ADAPTER_BIT_GNSS_MEASUREMENT);
55
56const MsgTask* LocDualContext::mMsgTask = NULL;
57ContextBase* LocDualContext::mFgContext = NULL;
58ContextBase* LocDualContext::mBgContext = NULL;
59ContextBase* LocDualContext::mInjectContext = NULL;
60// the name must be shorter than 15 chars
61const char* LocDualContext::mLocationHalName = "Loc_hal_worker";
62#ifndef USE_GLIB
63const char* LocDualContext::mLBSLibName = "liblbs_core.so";
64#else
65const char* LocDualContext::mLBSLibName = "liblbs_core.so.1";
66#endif
67
68pthread_mutex_t LocDualContext::mGetLocContextMutex = PTHREAD_MUTEX_INITIALIZER;
69
70const MsgTask* LocDualContext::getMsgTask(LocThread::tCreate tCreator,
71                                          const char* name, bool joinable)
72{
73    if (NULL == mMsgTask) {
74        mMsgTask = new MsgTask(tCreator, name, joinable);
75    }
76    return mMsgTask;
77}
78
79inline
80const MsgTask* LocDualContext::getMsgTask(const char* name, bool joinable) {
81    return getMsgTask((LocThread::tCreate)NULL, name, joinable);
82}
83
84ContextBase* LocDualContext::getLocFgContext(LocThread::tCreate tCreator,
85            LocMsg* firstMsg, const char* name, bool joinable)
86{
87    pthread_mutex_lock(&LocDualContext::mGetLocContextMutex);
88    LOC_LOGD("%s:%d]: querying ContextBase with tCreator", __func__, __LINE__);
89    if (NULL == mFgContext) {
90        LOC_LOGD("%s:%d]: creating msgTask with tCreator", __func__, __LINE__);
91        const MsgTask* msgTask = getMsgTask(tCreator, name, joinable);
92        mFgContext = new LocDualContext(msgTask,
93                                        mFgExclMask);
94    }
95    if(NULL == mInjectContext) {
96        LOC_LOGD("%s:%d]: mInjectContext is FgContext", __func__, __LINE__);
97        mInjectContext = mFgContext;
98        injectFeatureConfig(mInjectContext);
99    }
100    pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex);
101
102    if (firstMsg) {
103        mFgContext->sendMsg(firstMsg);
104    }
105
106    return mFgContext;
107}
108
109ContextBase* LocDualContext::getLocBgContext(LocThread::tCreate tCreator,
110            LocMsg* firstMsg, const char* name, bool joinable)
111{
112    pthread_mutex_lock(&LocDualContext::mGetLocContextMutex);
113    LOC_LOGD("%s:%d]: querying ContextBase with tCreator", __func__, __LINE__);
114    if (NULL == mBgContext) {
115        LOC_LOGD("%s:%d]: creating msgTask with tCreator", __func__, __LINE__);
116        const MsgTask* msgTask = getMsgTask(tCreator, name, joinable);
117        mBgContext = new LocDualContext(msgTask,
118                                        mBgExclMask);
119    }
120    if(NULL == mInjectContext) {
121        LOC_LOGD("%s:%d]: mInjectContext is BgContext", __func__, __LINE__);
122        mInjectContext = mBgContext;
123        injectFeatureConfig(mInjectContext);
124    }
125    pthread_mutex_unlock(&LocDualContext::mGetLocContextMutex);
126
127    if (firstMsg) {
128        mBgContext->sendMsg(firstMsg);
129    }
130
131    return mBgContext;
132}
133
134void LocDualContext :: injectFeatureConfig(ContextBase *curContext)
135{
136    LOC_LOGD("%s:%d]: Enter", __func__, __LINE__);
137    if(curContext == mInjectContext) {
138        LOC_LOGD("%s:%d]: Calling LBSProxy (%p) to inject feature config",
139                 __func__, __LINE__, ((LocDualContext *)mInjectContext)->mLBSProxy);
140        ((LocDualContext *)mInjectContext)->mLBSProxy->injectFeatureConfig(curContext);
141    }
142    LOC_LOGD("%s:%d]: Exit", __func__, __LINE__);
143}
144
145LocDualContext::LocDualContext(const MsgTask* msgTask,
146                               LOC_API_ADAPTER_EVENT_MASK_T exMask) :
147    ContextBase(msgTask, exMask, mLBSLibName)
148{
149}
150
151}
152