|
1 // Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // Defines the avdtp logical channel factory |
|
15 // which creates the logical (L2CAP) channels for transport and signalling channels |
|
16 // |
|
17 // |
|
18 |
|
19 /** |
|
20 @file |
|
21 @internalComponent |
|
22 */ |
|
23 |
|
24 #include <bt_sock.h> |
|
25 #include <es_prot.h> |
|
26 |
|
27 #ifndef AVDTPLOGICALCHANNELFACTORY_H |
|
28 #define AVDTPLOGICALCHANNELFACTORY_H |
|
29 |
|
30 #include "avdtpAllocators.h" |
|
31 |
|
32 class XLogicalChannelFactoryClient; |
|
33 class CManagedLogicalChannel; |
|
34 class CProtocolBase; |
|
35 struct TLogicalChannelRecord; |
|
36 class CLogicalChannelFactory; |
|
37 class CBluetoothProtocolBase; |
|
38 |
|
39 const TInt KInitialSequenceNumber = 1; |
|
40 const TInt KAvdtpChannelArraySize = 3; |
|
41 |
|
42 typedef TInt8 TLogicalChannelFactoryRequestId; |
|
43 |
|
44 |
|
45 NONSHARABLE_CLASS(TRequestIdManager) : public TBitFieldAllocator |
|
46 { |
|
47 public: |
|
48 inline TInt GetId(TLogicalChannelFactoryRequestId& aId); |
|
49 inline void FreeId(TLogicalChannelFactoryRequestId aId); |
|
50 }; |
|
51 |
|
52 // abstract |
|
53 NONSHARABLE_CLASS(CLogicalChannelFactoryRequest) : public CBase |
|
54 { |
|
55 friend class CLogicalChannelFactory; |
|
56 protected: |
|
57 CLogicalChannelFactoryRequest(XLogicalChannelFactoryClient& aClient, TLogicalChannelFactoryRequestId aId); |
|
58 XLogicalChannelFactoryClient& iClient; |
|
59 TSglQueLink iFactoryQLink; |
|
60 TLogicalChannelFactoryRequestId iId; |
|
61 TInt iNumChannelsRequired; // those left to connect |
|
62 }; |
|
63 |
|
64 // abstract |
|
65 NONSHARABLE_CLASS(CLogicalChannelFactoryPassiveRequest) : public CLogicalChannelFactoryRequest |
|
66 { |
|
67 public: |
|
68 ~CLogicalChannelFactoryPassiveRequest(); |
|
69 protected: |
|
70 CLogicalChannelFactoryPassiveRequest(XLogicalChannelFactoryClient& aClient, |
|
71 TLogicalChannelFactoryRequestId aId, |
|
72 CBluetoothProtocolBase& aAvdtp); |
|
73 void BaseConstructL(); |
|
74 private: |
|
75 CBluetoothProtocolBase& iAvdtp; |
|
76 }; |
|
77 |
|
78 NONSHARABLE_CLASS(CExpectSignallingLogicalChannel) : public CLogicalChannelFactoryPassiveRequest |
|
79 { |
|
80 public: |
|
81 static CExpectSignallingLogicalChannel* NewL(XLogicalChannelFactoryClient& aClient, TLogicalChannelFactoryRequestId aId, CBluetoothProtocolBase& aAvdtp); |
|
82 private: |
|
83 void ConstructL(); |
|
84 CExpectSignallingLogicalChannel(XLogicalChannelFactoryClient& aClient, TLogicalChannelFactoryRequestId aId, CBluetoothProtocolBase& aAvdtp); |
|
85 }; |
|
86 /** |
|
87 Use to tell factory to expect explicitly sequenced logical channels, typically used for Direct Transport channels |
|
88 */ |
|
89 NONSHARABLE_CLASS(CExpectSessionLogicalChannels) : public CLogicalChannelFactoryPassiveRequest |
|
90 { |
|
91 public: |
|
92 static CExpectSessionLogicalChannels* NewL(XLogicalChannelFactoryClient& aClient, TLogicalChannelFactoryRequestId aId, |
|
93 TInt aNumRequired, CBluetoothProtocolBase& aAvdtp); |
|
94 private: |
|
95 void ConstructL(); |
|
96 CExpectSessionLogicalChannels(XLogicalChannelFactoryClient& aClient, TLogicalChannelFactoryRequestId aId, |
|
97 TInt aNumRequired, CBluetoothProtocolBase& aAvdtp); |
|
98 }; |
|
99 |
|
100 /** |
|
101 For clients to issue requests to the ChannelFactory |
|
102 */ |
|
103 NONSHARABLE_CLASS(CLogicalChannelFactoryActiveRequest) : public CLogicalChannelFactoryRequest |
|
104 { |
|
105 friend class CLogicalChannelFactory; |
|
106 public: |
|
107 ~CLogicalChannelFactoryActiveRequest(); |
|
108 protected: |
|
109 CLogicalChannelFactoryActiveRequest(const TBTDevAddr& aAddr, XLogicalChannelFactoryClient& aClient, TLogicalChannelFactoryRequestId aId); |
|
110 protected: |
|
111 TFixedArray<CManagedLogicalChannel*, KAvdtpChannelArraySize> iLogicalChannels; |
|
112 TBTDevAddr iRemoteDev; |
|
113 }; |
|
114 |
|
115 NONSHARABLE_CLASS(CCreateSignallingLogicalChannel) : public CLogicalChannelFactoryActiveRequest |
|
116 { |
|
117 public: |
|
118 static CCreateSignallingLogicalChannel* NewL(const TBTDevAddr&, |
|
119 XLogicalChannelFactoryClient& aClient, |
|
120 TLogicalChannelFactoryRequestId aId, |
|
121 CLogicalChannelFactory& aLogicalChannelFactory); |
|
122 |
|
123 static CCreateSignallingLogicalChannel* NewLC(const TBTDevAddr&, |
|
124 XLogicalChannelFactoryClient& aClient, |
|
125 TLogicalChannelFactoryRequestId aId, |
|
126 CLogicalChannelFactory& aLogicalChannelFactory); |
|
127 private: |
|
128 CCreateSignallingLogicalChannel(const TBTDevAddr&, XLogicalChannelFactoryClient& aClient, TLogicalChannelFactoryRequestId aId); |
|
129 void ConstructL(CLogicalChannelFactory& aLogicalChannelFactory); |
|
130 }; |
|
131 |
|
132 |
|
133 /** |
|
134 To create n logical channels |
|
135 */ |
|
136 NONSHARABLE_CLASS(CCreateSessionLogicalChannels) : public CLogicalChannelFactoryActiveRequest |
|
137 { |
|
138 public: |
|
139 static CCreateSessionLogicalChannels* NewL(const TBTDevAddr& aAddr, XLogicalChannelFactoryClient& aClient, |
|
140 TLogicalChannelFactoryRequestId aId, TInt aNumRequired); |
|
141 static CCreateSessionLogicalChannels* NewLC(const TBTDevAddr& aAddr, XLogicalChannelFactoryClient& aClient, |
|
142 TLogicalChannelFactoryRequestId aId, TInt aNumRequired); |
|
143 private: |
|
144 CCreateSessionLogicalChannels(const TBTDevAddr& aAddr, XLogicalChannelFactoryClient& aClient, |
|
145 TLogicalChannelFactoryRequestId aId, TInt aNumRequired); |
|
146 }; |
|
147 |
|
148 /** |
|
149 To close logical channels |
|
150 */ |
|
151 NONSHARABLE_CLASS(CCloseSessionLogicalChannels) : public CLogicalChannelFactoryRequest |
|
152 { |
|
153 friend class CLogicalChannelFactory; |
|
154 public: |
|
155 static CCloseSessionLogicalChannels* NewL(XLogicalChannelFactoryClient& aClient, |
|
156 TLogicalChannelFactoryRequestId aId); |
|
157 static CCloseSessionLogicalChannels* NewLC(XLogicalChannelFactoryClient& aClient, |
|
158 TLogicalChannelFactoryRequestId aId); |
|
159 ~CCloseSessionLogicalChannels(); |
|
160 |
|
161 void StartJob(TInt aTimeout); |
|
162 void ChannelClosed(CManagedLogicalChannel* aChannel); |
|
163 private: |
|
164 CCloseSessionLogicalChannels(XLogicalChannelFactoryClient& aClient, |
|
165 TLogicalChannelFactoryRequestId aId); |
|
166 static TInt WatchdogBarked(TAny* aCloseLogicalChannels); |
|
167 void CloseChannels(TBool aNotifyCompletion); |
|
168 |
|
169 private: |
|
170 TFixedArray<CManagedLogicalChannel*, KAvdtpChannelArraySize> iLogicalChannels; |
|
171 TDeltaTimerEntry iTimerEntry; |
|
172 }; |
|
173 |
|
174 #ifdef _DEBUG |
|
175 #define DEBUG_STORE_FACTORY_REQUEST iChannelFactoryRequest = &\ |
|
176 |
|
177 #else |
|
178 #define DEBUG_STORE_FACTORY_REQUEST |
|
179 #endif |
|
180 |
|
181 /** |
|
182 Class representing the job the factory has undertaken |
|
183 This is always returned synchronously - the client should inspect if the job has been completed synchronously |
|
184 via the State() method. |
|
185 If the job is process asynchronously then a new one is returned upon completion |
|
186 */ |
|
187 NONSHARABLE_CLASS(TLogicalChannelFactoryTicket) |
|
188 { |
|
189 friend class CLogicalChannelFactory; |
|
190 public: |
|
191 enum TLogicalChannelFactoryRequestState |
|
192 { |
|
193 ERequestIdle, |
|
194 ERequestOutstanding, |
|
195 ERequestComplete, |
|
196 ERequestErrored |
|
197 }; |
|
198 |
|
199 TLogicalChannelFactoryTicket(CLogicalChannelFactory* aFactory, TLogicalChannelFactoryRequestId aId); |
|
200 TLogicalChannelFactoryTicket(); |
|
201 TLogicalChannelRecord GetLogicalChannel(TInt aSequenceNumber=1); |
|
202 |
|
203 inline TLogicalChannelFactoryRequestState State() const { return iState; } |
|
204 inline TLogicalChannelFactoryRequestId Id() const { return iId; } |
|
205 |
|
206 private: |
|
207 void SetState(TLogicalChannelFactoryRequestState aNewState); |
|
208 |
|
209 private: |
|
210 CLogicalChannelFactory* iFactory; // non-owned, cannot be reference in default ctor |
|
211 TLogicalChannelFactoryRequestId iId; |
|
212 TLogicalChannelFactoryRequestState iState; |
|
213 }; |
|
214 |
|
215 |
|
216 /* |
|
217 The class provides a callback for someone that asked for logical channels |
|
218 @note this is an X class (see Programming DB) as it is effectively an M-class |
|
219 but must have a member to be que-able. |
|
220 */ |
|
221 NONSHARABLE_CLASS(XLogicalChannelFactoryClient) |
|
222 { |
|
223 public: |
|
224 virtual void LogicalChannelFactoryRequestComplete(TLogicalChannelFactoryTicket, TInt aResult)=0; |
|
225 TSglQueLink iFactoryQLink; |
|
226 }; |
|
227 |
|
228 |
|
229 /** |
|
230 Knows about ordering of L2CAP channels |
|
231 Hands ownership of newly established channels to clients |
|
232 Provides them with the TransportSession to |
|
233 a) remind them |
|
234 b) to assert it's ok |
|
235 |
|
236 The use of this class will to some extent be by someone who |
|
237 additionally knows (or is implcitily designed as such) of the |
|
238 channel order - for we cannot go adding a reporting bearer merely |
|
239 to obtain a recovery bearer....(presumably :o) |
|
240 |
|
241 Once ownership is transferred the caller will synchronously have to set |
|
242 itself as the SocketNotify of the L2CAP SAP |
|
243 @internalComponent |
|
244 */ |
|
245 class CDirectChannel; |
|
246 NONSHARABLE_CLASS(CLogicalChannelFactory) : public CBase, public MSocketNotify, public XLogicalChannelFactoryClient |
|
247 { |
|
248 friend class TLogicalChannelFactoryTicket; |
|
249 public: |
|
250 static CLogicalChannelFactory* NewL(CBluetoothProtocolBase& iProtocol, CProtocolBase& aSAPFactory); |
|
251 |
|
252 TLogicalChannelFactoryTicket CreateSignallingLogicalChannelL(const TBTDevAddr& aAddr, |
|
253 XLogicalChannelFactoryClient& aClient); |
|
254 |
|
255 TLogicalChannelFactoryTicket CreateSessionLogicalChannelsL(const TBTDevAddr& aAddr, |
|
256 XLogicalChannelFactoryClient& aClient, TInt aNumRequired); |
|
257 |
|
258 void CloseSessionLogicalChannelsL(TArray<CDirectChannel*>& aChannels, |
|
259 TInt aTimeout); |
|
260 |
|
261 TLogicalChannelFactoryTicket ExpectSignallingLogicalChannelL(XLogicalChannelFactoryClient& aClient); |
|
262 |
|
263 TLogicalChannelFactoryTicket ExpectSessionLogicalChannelsL(XLogicalChannelFactoryClient& aClient, |
|
264 TInt aNumRequired); |
|
265 |
|
266 void LogicalChannelLost(CManagedLogicalChannel* aChannel); |
|
267 void Cancel(TLogicalChannelFactoryTicket& aJobSpec); |
|
268 ~CLogicalChannelFactory(); |
|
269 inline CProtocolBase& SAPFactory() const; |
|
270 TInt BearerConnectComplete(const TBTDevAddr& /*aAddr*/, |
|
271 CServProviderBase* aSAP); // forward from AVDTP protocol when listen complete |
|
272 private: |
|
273 // from MSocketNotify |
|
274 virtual void NewData(TUint aCount); |
|
275 virtual void CanSend(); |
|
276 virtual void ConnectComplete(); |
|
277 virtual void ConnectComplete(const TDesC8& aConnectData); |
|
278 virtual void ConnectComplete(CServProviderBase& aSSP); |
|
279 virtual void ConnectComplete(CServProviderBase& aSSP,const TDesC8& aConnectData); |
|
280 virtual void CanClose(TDelete aDelete=EDelete); |
|
281 virtual void CanClose(const TDesC8& aDisconnectData,TDelete aDelete=EDelete); |
|
282 virtual void Error(TInt aError,TUint aOperationMask=EErrorAllOperations); |
|
283 virtual void Disconnect(); |
|
284 virtual void Disconnect(TDesC8& aDisconnectData); |
|
285 virtual void IoctlComplete(TDesC8* aBuf); |
|
286 virtual void NoBearer(const TDesC8& aConnectionInfo); |
|
287 virtual void Bearer(const TDesC8& aConnectionInfo); |
|
288 |
|
289 private: |
|
290 // from XLogicalChannelFactoryClient |
|
291 virtual void LogicalChannelFactoryRequestComplete(TLogicalChannelFactoryTicket, TInt aResult); |
|
292 |
|
293 private: |
|
294 CLogicalChannelFactory(CBluetoothProtocolBase& iProtocol, CProtocolBase& aSAPFactory); |
|
295 void ConstructL(); |
|
296 void DoObtainChannelL(); |
|
297 static TInt TryNextJob(TAny* aAny); |
|
298 void TryNextActiveJob(); |
|
299 TBool CheckActiveJobComplete(CLogicalChannelFactoryActiveRequest& aJob); |
|
300 void CompleteActiveJob(TInt aError); |
|
301 void NotifyComplete(TInt aError, CLogicalChannelFactoryRequest& aRequest); |
|
302 void SetId(CLogicalChannelFactoryActiveRequest& aRequest); |
|
303 static void FreeId(TAny* aId); |
|
304 void DeleteRequest(CLogicalChannelFactoryRequest *aRequest); |
|
305 |
|
306 TLogicalChannelRecord ClaimLogicalChannel(TInt aSequenceNumber, TLogicalChannelFactoryRequestId aId, TBool& aFinished); |
|
307 CManagedLogicalChannel* FindUnclaimedLogicalChannel(const TBTDevAddr& aAddr, |
|
308 TInt aSequenceNumber, |
|
309 TLogicalChannelFactoryRequestId& aId); |
|
310 TInt TryToTakeConnection(const TBTDevAddr& aRemote, CServProviderBase* aSAP, |
|
311 TSglQue<CLogicalChannelFactoryPassiveRequest>& aJobQueue); |
|
312 |
|
313 private: |
|
314 CBluetoothProtocolBase& iProtocol; //AVDTP |
|
315 CProtocolBase& iBearerSAPFactory; //L2CAP |
|
316 TDblQue<CManagedLogicalChannel> iUnclaimedLogicalChannels; |
|
317 TSglQue<CLogicalChannelFactoryActiveRequest> iPendingActiveJobs; |
|
318 TSglQue<CLogicalChannelFactoryPassiveRequest> iPendingPassiveSignallingJobs; |
|
319 TSglQue<CLogicalChannelFactoryPassiveRequest> iPendingPassiveSessionJobs; |
|
320 TSglQue<CCloseSessionLogicalChannels> iCloseChannelJobs; |
|
321 CLogicalChannelFactoryActiveRequest* iCurrentActiveJob; |
|
322 TBool iCurrentJobCancelled; |
|
323 TRequestIdManager iIdManager; |
|
324 TLogicalChannelFactoryRequestId iId; // used for cleaning up if the got (getid) is to be lost due to a leave |
|
325 CAsyncCallBack* iAsyncTryNextJob; |
|
326 }; |
|
327 |
|
328 |
|
329 NONSHARABLE_CLASS(CManagedLogicalChannel) : public CBase, public MSocketNotify |
|
330 /** |
|
331 for queuing inbound unclaimed SAPs. needs to be a socket so that SAP |
|
332 can declare newdata, disconnection etc. |
|
333 */ |
|
334 { |
|
335 friend class CLogicalChannelFactory; //for quing |
|
336 public: |
|
337 static CManagedLogicalChannel* NewL(CLogicalChannelFactory& aFactory, |
|
338 const TBTDevAddr& aAddr, |
|
339 TInt aSequenceNumber, |
|
340 TLogicalChannelFactoryRequestId aId, |
|
341 CServProviderBase* aSAP = NULL); |
|
342 static CManagedLogicalChannel* NewL(CLogicalChannelFactory& aFactory, |
|
343 TLogicalChannelFactoryRequestId aId); |
|
344 |
|
345 ~CManagedLogicalChannel(); |
|
346 |
|
347 void Shutdown(); |
|
348 |
|
349 private: |
|
350 // from MSocketNotify |
|
351 virtual void NewData(TUint aCount); |
|
352 virtual void CanSend(); |
|
353 virtual void ConnectComplete(); |
|
354 virtual void ConnectComplete(const TDesC8& aConnectData); |
|
355 virtual void ConnectComplete(CServProviderBase& aSSP); |
|
356 virtual void ConnectComplete(CServProviderBase& aSSP,const TDesC8& aConnectData); |
|
357 virtual void CanClose(TDelete aDelete=EDelete); |
|
358 virtual void CanClose(const TDesC8& aDisconnectData,TDelete aDelete=EDelete); |
|
359 virtual void Error(TInt aError,TUint aOperationMask=EErrorAllOperations); |
|
360 virtual void Disconnect(); |
|
361 virtual void Disconnect(TDesC8& aDisconnectData); |
|
362 virtual void IoctlComplete(TDesC8* aBuf); |
|
363 virtual void NoBearer(const TDesC8& aConnectionInfo); |
|
364 virtual void Bearer(const TDesC8& aConnectionInfo); |
|
365 |
|
366 private: |
|
367 CManagedLogicalChannel(CLogicalChannelFactory& aFactory, |
|
368 const TBTDevAddr& aAddr, |
|
369 TInt aSequenceNumber, |
|
370 TLogicalChannelFactoryRequestId aId); |
|
371 |
|
372 CManagedLogicalChannel(CLogicalChannelFactory& aFactory, |
|
373 TLogicalChannelFactoryRequestId aId); |
|
374 |
|
375 void ConstructL(CServProviderBase* aPrecreatedSAP); |
|
376 CServProviderBase* ObtainSAP(); |
|
377 void ProvideSAP(CServProviderBase* aSAP); |
|
378 |
|
379 private: |
|
380 CLogicalChannelFactory& iFactory; |
|
381 TBTDevAddr iRemoteAddress; |
|
382 TInt iSequenceNumber; // for sequence creations/MCs |
|
383 CServProviderBase* iLogicalChannelSAP; |
|
384 TUint iDataCount; |
|
385 TBool iEndOfData; // bit annoying, but safer that bittwiddling on iDataCount |
|
386 TDblQueLink iFactoryQLink; |
|
387 TLogicalChannelFactoryRequestId iId; // the request this was part of |
|
388 }; |
|
389 |
|
390 inline TInt TRequestIdManager::GetId(TLogicalChannelFactoryRequestId& aId) |
|
391 { |
|
392 TInt val, res; |
|
393 res = Get(val, 30, 1); // 0 is "invalid" |
|
394 aId = static_cast<TLogicalChannelFactoryRequestId>(val); |
|
395 return res; |
|
396 } |
|
397 |
|
398 inline void TRequestIdManager::FreeId(TLogicalChannelFactoryRequestId aId) |
|
399 { |
|
400 Free(aId); |
|
401 } |
|
402 |
|
403 inline CProtocolBase& CLogicalChannelFactory::SAPFactory() const |
|
404 { |
|
405 return iBearerSAPFactory; |
|
406 } |
|
407 |
|
408 NONSHARABLE_CLASS(TLogicalChannelRecord) |
|
409 /* |
|
410 Effectively a struct for transferring ownership of resulting logical channels |
|
411 Binds together the SAP, and any data count that has appeared whilst the logical channel |
|
412 lay unclaimed. |
|
413 */ |
|
414 { |
|
415 public: |
|
416 inline TLogicalChannelRecord(); |
|
417 inline void Reset(); |
|
418 public: |
|
419 CServProviderBase* iLogicalChannelSAP; // non-owned |
|
420 TUint iDataCount; |
|
421 TBool iEndOfData; // bit annoying, but safer that bittwiddling on iDataCount |
|
422 }; |
|
423 |
|
424 /** |
|
425 To help claim logical channels |
|
426 */ |
|
427 NONSHARABLE_CLASS(TLogicalChannelFactoryTicketInspector) |
|
428 { |
|
429 public: |
|
430 TLogicalChannelFactoryTicketInspector(TLogicalChannelFactoryTicket& aTicket, |
|
431 TBool aRequireReporting, |
|
432 TBool aRequireRecovery, |
|
433 TBool aMuxed); |
|
434 TLogicalChannelRecord GetLogicalChannel(TAvdtpTransportSessionType aType); |
|
435 private: |
|
436 TLogicalChannelFactoryTicket& iTicket; |
|
437 const TInt iSignallingSequenceNumber; |
|
438 const TInt iMediaSequenceNumber; // always first whether muxed or not |
|
439 TInt iReportingSequenceNumber; |
|
440 TInt iRecoverySequenceNumber; |
|
441 TLogicalChannelRecord iCachedRecord; // eg for Reporting which is the same LC as media |
|
442 TBool iCached; |
|
443 #ifdef _DEBUG |
|
444 TBool iRequireReporting; |
|
445 TBool iRequireRecovery; |
|
446 #endif |
|
447 }; |
|
448 |
|
449 |
|
450 inline TLogicalChannelRecord::TLogicalChannelRecord() |
|
451 { |
|
452 Reset(); |
|
453 } |
|
454 |
|
455 inline void TLogicalChannelRecord::Reset() |
|
456 { |
|
457 iLogicalChannelSAP = NULL; |
|
458 iDataCount = 0; |
|
459 iEndOfData = EFalse; |
|
460 } |
|
461 |
|
462 #endif //AVDTPLOGICALCHANNELFACTORY_H |