29
|
1 |
/*
|
31
|
2 |
* Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description: Server class for handling commands from clients, and the
|
29
|
15 |
* central class in btnotif thread.
|
|
16 |
*
|
|
17 |
*/
|
|
18 |
|
|
19 |
#include "btnotifserver.h"
|
|
20 |
#include <btservices/btdevrepository.h>
|
|
21 |
#include "btnotifsession.h"
|
|
22 |
#include "btnotifconnectiontracker.h"
|
|
23 |
#include "btnotifsettingstracker.h"
|
|
24 |
#include "btnotificationmanager.h"
|
|
25 |
#include "btnotifdeviceselector.h"
|
57
|
26 |
#include "btnotifgeninfonotifier.h"
|
29
|
27 |
#include "btnotifserversecpolicy.h"
|
|
28 |
#include "btnotifclientserver.h"
|
57
|
29 |
#include "btnotifpowernotifier.h"
|
29
|
30 |
|
|
31 |
/** Panic category */
|
|
32 |
_LIT( KBTNotifPanic, "BTNotif panic" );
|
|
33 |
|
|
34 |
/** Timeout (10 sec) for shutting down the server
|
|
35 |
* (when BT power is off and no clients connected). */
|
|
36 |
const TInt KBTNtoifShutdownTimeout = 10 * 1000 * 1000;
|
|
37 |
|
|
38 |
// ======== LOCAL FUNCTIONS ========
|
|
39 |
|
|
40 |
// ---------------------------------------------------------------------------
|
|
41 |
// Start the server.
|
|
42 |
// ---------------------------------------------------------------------------
|
|
43 |
//
|
|
44 |
static void RunServerL()
|
|
45 |
{
|
|
46 |
BOstraceFunctionEntry0( DUMMY_DEVLIST );
|
|
47 |
|
|
48 |
(void) User::RenameThread( KBTNotifServerName );
|
|
49 |
// Create and install the active scheduler for this thread.
|
|
50 |
CActiveScheduler* scheduler = new( ELeave ) CActiveScheduler();
|
|
51 |
CleanupStack::PushL( scheduler );
|
|
52 |
CActiveScheduler::Install( scheduler );
|
|
53 |
// create the server (and leave it on the cleanup stack)
|
|
54 |
CBTNotifServer* notifServer = CBTNotifServer::NewLC();
|
|
55 |
// Initialisation complete, now signal the client
|
|
56 |
RProcess::Rendezvous( KErrNone );
|
|
57 |
// The server is now up and running.
|
70
|
58 |
BOstrace0( TRACE_NORMAL, DUMMY_DEVLIST, _L("[BTNOTIF]\t BTNotif server now up and running" ));
|
29
|
59 |
// The active scheduler runs during the lifetime of this thread.
|
|
60 |
CActiveScheduler::Start();
|
|
61 |
// Stopping the active scheduler means terminating the thread.
|
|
62 |
// Cleanup the server and scheduler.
|
|
63 |
CleanupStack::PopAndDestroy( notifServer );
|
|
64 |
CleanupStack::PopAndDestroy( scheduler );
|
|
65 |
BOstraceFunctionExit0( DUMMY_DEVLIST );
|
|
66 |
}
|
|
67 |
|
|
68 |
// ---------------------------------------------------------------------------
|
|
69 |
// Panic the server.
|
|
70 |
// ---------------------------------------------------------------------------
|
|
71 |
//
|
|
72 |
void PanicServer( TInt aReason )
|
|
73 |
{
|
|
74 |
User::Panic( KBTNotifPanic, aReason );
|
|
75 |
}
|
|
76 |
|
|
77 |
// ---------------------------------------------------------------------------
|
|
78 |
// Panic the client through the client-side message.
|
|
79 |
// ---------------------------------------------------------------------------
|
|
80 |
//
|
|
81 |
void PanicClient( const RMessage2& aMessage, TInt aReason )
|
|
82 |
{
|
|
83 |
aMessage.Panic( KBTNotifPanic, aReason );
|
|
84 |
}
|
|
85 |
|
|
86 |
// ======== MEMBER FUNCTIONS ========
|
|
87 |
|
|
88 |
// ---------------------------------------------------------------------------
|
|
89 |
// C++ default constructor
|
|
90 |
// ---------------------------------------------------------------------------
|
|
91 |
//
|
|
92 |
CBTNotifServer::CBTNotifServer()
|
|
93 |
: CPolicyServer( EPriorityUserInput, KBTNotifServerPolicy )
|
|
94 |
{
|
|
95 |
}
|
|
96 |
|
|
97 |
// ---------------------------------------------------------------------------
|
|
98 |
// Symbian 2nd-phase constructor
|
|
99 |
// ---------------------------------------------------------------------------
|
|
100 |
//
|
|
101 |
void CBTNotifServer::ConstructL()
|
|
102 |
{
|
|
103 |
// Add the server to the active scheduler (from CServer2):
|
|
104 |
StartL( KBTNotifServerName );
|
|
105 |
iAsyncCb = new( ELeave ) CAsyncCallBack( EPriorityHigh );
|
|
106 |
TCallBack cb( AsyncConstructCb, this );
|
|
107 |
iAsyncCb->Set( cb );
|
|
108 |
iAsyncCb->CallBack();
|
|
109 |
}
|
|
110 |
|
|
111 |
// ---------------------------------------------------------------------------
|
|
112 |
// Asynchronous 3rd-phase constructor
|
|
113 |
// ---------------------------------------------------------------------------
|
|
114 |
//
|
|
115 |
void CBTNotifServer::AsyncConstructL()
|
|
116 |
{
|
31
|
117 |
// The server class owns this registry object and provides it
|
|
118 |
// as a singleton in the whole server process.
|
|
119 |
// Server itself does not handle any registry events.
|
|
120 |
// Classes that want to receive these events must register
|
|
121 |
// observers via CBtDevRepository interface.
|
|
122 |
iDevRep = CBtDevRepository::NewL();
|
|
123 |
iNotificationMgr = CBTNotificationManager::NewL( this );
|
29
|
124 |
iSettingsTracker = CBTNotifSettingsTracker::NewL( this );
|
31
|
125 |
iConnectionTracker = CBTNotifConnectionTracker::NewL( this );
|
57
|
126 |
iGenInfoNotifier = CBTNotifGenInfoNotifier::NewL( this );
|
|
127 |
iBTPowerNotifier = CBTNotifPowerNotifier::NewL( this );
|
29
|
128 |
iTimer = CDeltaTimer::NewL(CActive::EPriorityLow);
|
|
129 |
TCallBack shutdownCb( ShutdownTimeout, this );
|
|
130 |
iShutdownTimerEntry.Set( shutdownCb );
|
|
131 |
}
|
|
132 |
|
|
133 |
// ---------------------------------------------------------------------------
|
|
134 |
// Callback for asynchronous construction.
|
|
135 |
// ---------------------------------------------------------------------------
|
|
136 |
//
|
|
137 |
TInt CBTNotifServer::AsyncConstructCb( TAny* aPtr )
|
|
138 |
{
|
|
139 |
TRAPD( err, ( (CBTNotifServer*) aPtr )->AsyncConstructL() );
|
|
140 |
return err;
|
|
141 |
}
|
|
142 |
|
|
143 |
|
|
144 |
// ---------------------------------------------------------------------------
|
|
145 |
// NewLC.
|
|
146 |
// ---------------------------------------------------------------------------
|
|
147 |
//
|
|
148 |
CBTNotifServer* CBTNotifServer::NewLC()
|
|
149 |
{
|
|
150 |
CBTNotifServer* self = new( ELeave ) CBTNotifServer();
|
|
151 |
CleanupStack::PushL( self );
|
|
152 |
self->ConstructL();
|
|
153 |
return self;
|
|
154 |
}
|
|
155 |
|
|
156 |
|
|
157 |
// ---------------------------------------------------------------------------
|
|
158 |
// Destructor
|
|
159 |
// ---------------------------------------------------------------------------
|
|
160 |
//
|
|
161 |
CBTNotifServer::~CBTNotifServer()
|
|
162 |
{
|
|
163 |
delete iDevSelector;
|
|
164 |
delete iSettingsTracker;
|
|
165 |
delete iConnectionTracker;
|
|
166 |
delete iNotificationMgr;
|
57
|
167 |
delete iGenInfoNotifier;
|
|
168 |
delete iBTPowerNotifier;
|
29
|
169 |
delete iAsyncCb;
|
|
170 |
delete iTimer;
|
|
171 |
delete iDevRep;
|
|
172 |
}
|
|
173 |
|
|
174 |
// ---------------------------------------------------------------------------
|
|
175 |
// Handle a change in BT power state.
|
|
176 |
// ---------------------------------------------------------------------------
|
|
177 |
//
|
|
178 |
void CBTNotifServer::HandlePowerStateChangeL( TBTPowerStateValue aState )
|
|
179 |
{
|
|
180 |
CheckIdle( aState );
|
|
181 |
}
|
|
182 |
|
|
183 |
// ---------------------------------------------------------------------------
|
|
184 |
// Increase the session count.
|
|
185 |
// ---------------------------------------------------------------------------
|
|
186 |
//
|
|
187 |
void CBTNotifServer::AddSession()
|
|
188 |
{
|
|
189 |
++iSessionCount;
|
|
190 |
iTimer->Remove( iShutdownTimerEntry );
|
|
191 |
}
|
|
192 |
|
|
193 |
|
|
194 |
// ---------------------------------------------------------------------------
|
|
195 |
// Decrease the session count.
|
|
196 |
// ---------------------------------------------------------------------------
|
|
197 |
//
|
|
198 |
void CBTNotifServer::RemoveSession()
|
|
199 |
{
|
|
200 |
if ( iSessionCount > 0 )
|
|
201 |
{
|
|
202 |
// session counter can't be less than 0
|
|
203 |
--iSessionCount;
|
|
204 |
}
|
|
205 |
CheckIdle( iSettingsTracker->GetPowerState() );
|
|
206 |
}
|
|
207 |
|
|
208 |
// ---------------------------------------------------------------------------
|
|
209 |
// get the singleton instance of device repository
|
|
210 |
// ---------------------------------------------------------------------------
|
|
211 |
//
|
|
212 |
CBtDevRepository& CBTNotifServer::DevRepository()
|
|
213 |
{
|
|
214 |
return *iDevRep;
|
|
215 |
}
|
|
216 |
|
|
217 |
// ---------------------------------------------------------------------------
|
|
218 |
// get the singleton instance of device search notifier
|
|
219 |
// ---------------------------------------------------------------------------
|
|
220 |
//
|
|
221 |
CBTNotifDeviceSelector& CBTNotifServer::DeviceSelectorL()
|
|
222 |
{
|
|
223 |
if ( ! iDevSelector )
|
|
224 |
{
|
|
225 |
iDevSelector = CBTNotifDeviceSelector::NewL( *this );
|
|
226 |
}
|
|
227 |
return *iDevSelector;
|
|
228 |
}
|
|
229 |
|
|
230 |
// ---------------------------------------------------------------------------
|
|
231 |
// From class CPolicyServer.
|
|
232 |
// Create a new session object.
|
|
233 |
// ---------------------------------------------------------------------------
|
|
234 |
//
|
|
235 |
CSession2* CBTNotifServer::NewSessionL( const TVersion& aVersion,
|
|
236 |
const RMessage2& aMessage ) const
|
|
237 |
{
|
|
238 |
(void) aMessage;
|
|
239 |
// Compare our version with client-side version, CServer2 requires that
|
|
240 |
// we leave if they are not compatible.
|
|
241 |
TVersion srvVersion( KBTNotifServerVersionMajor, KBTNotifServerVersionMinor,
|
|
242 |
KBTNotifServerVersionBuild );
|
|
243 |
|
|
244 |
if( !User::QueryVersionSupported( aVersion, srvVersion ) )
|
|
245 |
{
|
|
246 |
// EFalse is returned if our version is not less than or
|
|
247 |
// equal to the client version.
|
|
248 |
User::Leave( KErrNotSupported );
|
|
249 |
}
|
|
250 |
return CBTNotifSession::NewL();
|
|
251 |
}
|
|
252 |
|
|
253 |
void CBTNotifServer::CheckIdle( TBTPowerStateValue aState )
|
|
254 |
{
|
|
255 |
// In special scenarios, we do not have to remove the timer and queue it
|
|
256 |
// again, but these scenarios rarely happen in end-user use cases.
|
|
257 |
iTimer->Remove( iShutdownTimerEntry );
|
|
258 |
if ( iSessionCount == 0 && aState == EBTPowerOff )
|
|
259 |
{
|
|
260 |
// BT power is off, start the shutdown timer.
|
|
261 |
TTimeIntervalMicroSeconds32 interval = KBTNtoifShutdownTimeout;
|
|
262 |
iTimer->Queue( interval, iShutdownTimerEntry );
|
|
263 |
}
|
|
264 |
}
|
|
265 |
|
|
266 |
TInt CBTNotifServer::ShutdownTimeout( TAny* aPtr )
|
|
267 |
{
|
|
268 |
(void) aPtr;
|
|
269 |
CActiveScheduler::Stop();
|
|
270 |
return KErrNone;
|
|
271 |
}
|
|
272 |
|
|
273 |
// ======== GLOBAL FUNCTIONS ========
|
|
274 |
|
|
275 |
// ---------------------------------------------------------------------------
|
|
276 |
// Main function of the executable.
|
|
277 |
// ---------------------------------------------------------------------------
|
|
278 |
//
|
|
279 |
GLDEF_C TInt E32Main()
|
|
280 |
{
|
42
|
281 |
// Disabled until memory leak in QT/Open C are fixed
|
|
282 |
// __UHEAP_MARK;
|
29
|
283 |
CTrapCleanup* cleanup = CTrapCleanup::New();
|
|
284 |
TInt err = KErrNoMemory;
|
|
285 |
if ( cleanup )
|
|
286 |
{
|
|
287 |
TRAP( err, RunServerL() );
|
|
288 |
delete cleanup;
|
|
289 |
}
|
42
|
290 |
// Disabled until memory leak in QT/Open C are fixed
|
|
291 |
// __UHEAP_MARKEND;
|
29
|
292 |
return err;
|
|
293 |
}
|
|
294 |
|
|
295 |
|