bluetooth/btstack/common/bt.h
changeset 0 29b1cd4cb562
child 45 99439b07e980
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 1999-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 // Define the interface to the Bluetooth protocol module
       
    15 // 
       
    16 //
       
    17 
       
    18 #ifndef BT_H
       
    19 #define BT_H
       
    20 
       
    21 #include <es_prot.h>
       
    22 #include "secman.h"
       
    23 #include "debug.h"
       
    24 #include "blogger.h"
       
    25 #include "btsockettimer.h"
       
    26 #include "codman.h"
       
    27 
       
    28 // This is the factory that ESOCK uses
       
    29 class CBluetoothProtocolBase;
       
    30 class CL2CAPProtocol;
       
    31 class CRfcommProtocol;
       
    32 class CBlogger;
       
    33 class CIncomingConnectionListener;
       
    34 
       
    35 typedef TLinkType TPhysicalLinkPort;
       
    36 
       
    37 enum TBTControlPlaneMessage
       
    38 	{
       
    39 	EOverridePark,
       
    40 	EUndoOverridePark,
       
    41 	EArbitrateForNew,
       
    42 	EArbitrateForClosed,
       
    43 	EPresent,
       
    44 	EPreauthoriseDevice, // used for multiple SAPs operating in one usecase (eg AVRCP and GAVDP);
       
    45 	ESubscribePhysicalLink,
       
    46 	EUnsubscribePhysicalLink,
       
    47 	EOverrideLPMWithTimeout,
       
    48 	EOverrideLPM,
       
    49 	EUndoOverrideLPM,
       
    50 	ETryToAndThenPreventHostEncryptionKeyRefresh,
       
    51 	};
       
    52 
       
    53 /**
       
    54 Struct used for protocol control-plane message EPreauthoriseDevice, ECancelPreauthoriseDevice
       
    55 Example: a headset may (in any order) connect AVCTP and AVDTP connections
       
    56 We do not want to have the user see a double-authorise.
       
    57 Care should be taken by protocol implementers when using this functionality
       
    58 */
       
    59 struct TOverrideAuthorise
       
    60 	{
       
    61 	TBTDevAddr	iPreauthorisedRemoteAddress;
       
    62 	TInt		iAuthorisingProtocol; // for checking
       
    63 	TInt		iAuthorisingPort;	// for checking
       
    64 	TBool		iPreauthorise;
       
    65 	};
       
    66 	
       
    67 enum TPhysicalLinkPresent
       
    68 	{
       
    69 	EYes,			// there is a non-parked, physical link to a device
       
    70 	EYesParked,		// there is a physical link, but it is parked
       
    71 	ENo,			// no physical link in place
       
    72 	ENoWay,			// no physical link and no way to create one
       
    73 	};
       
    74 
       
    75 enum TBluetoothFamilyPanic
       
    76 	{
       
    77 	EBTPrtProtocolCannotListen,
       
    78 	};
       
    79 
       
    80 class MBluetoothControlPlaneToken
       
    81 	{
       
    82 public:
       
    83 	static void Release(MBluetoothControlPlaneToken*& aToken);
       
    84 private:
       
    85 	virtual void Release() = 0;
       
    86 	};
       
    87 
       
    88 NONSHARABLE_CLASS(RBTControlPlane)
       
    89 	{
       
    90 public:
       
    91 // control-plane management
       
    92 	void AttachProtocolL(TInt aProtocolNum, CBluetoothProtocolBase& aProtocol);
       
    93 	void DetachProtocol(CBluetoothProtocolBase& aProtocol);
       
    94 	void Close();
       
    95 
       
    96 // control-plane services
       
    97 	TInt ModifyPhysicalLink(TBTControlPlaneMessage aMessage, const TBTDevAddr& aAddr) const;
       
    98 	void TryToAndThenPreventHostEncryptionKeyRefresh(const TBTDevAddr& aAddr, MBluetoothControlPlaneToken*& aOutToken);
       
    99 
       
   100 	TPhysicalLinkPresent PhysicalLinkPresent(const TBTDevAddr& aAddr) const;
       
   101 	TInt Preauthorise(TInt aTargetProtocolNum,
       
   102 						const TOverrideAuthorise& aSetPreauthorisation);
       
   103 	TInt SubscribePhysicalLink  (MPhysicalLinkObserver& aObserver, TBTDevAddr aAddr) const;
       
   104 	TInt UnsubscribePhysicalLink(MPhysicalLinkObserver& aObserver, TBTDevAddr aAddr) const;
       
   105 private:
       
   106 	CBluetoothProtocolBase* Protocol(TInt aProtocolNum) const;
       
   107 private:
       
   108 	struct TBTProtocol
       
   109 		{
       
   110 		CBluetoothProtocolBase*	iProtocol;	// non-owned
       
   111 		TInt					iProtocolNum;
       
   112 		};
       
   113 	RArray<TBTProtocol>	iProtocols;
       
   114 	};
       
   115 
       
   116 NONSHARABLE_CLASS(CBTProtocolFamily): public CProtocolFamilyBase
       
   117 	{
       
   118 private:
       
   119 	CBTProtocolFamily();	
       
   120 public:
       
   121 	static CBTProtocolFamily* NewL();
       
   122 	virtual TInt Install();
       
   123 	virtual TInt Remove();
       
   124 	virtual CProtocolBase* NewProtocolL(TUint aSockType,TUint aProtocol);
       
   125 	virtual TUint ProtocolList(TServerProtocolDesc*& aProtocolList);
       
   126 	virtual void Open(); // must always override Open and Close together
       
   127 	virtual void Close();
       
   128 	static TInt Destruct(TAny* aProtocolFamily); // callback
       
   129 	~CBTProtocolFamily();
       
   130 private:
       
   131 	void ConstructL();
       
   132 private:
       
   133 	CBTSecMan*	iSecurityMgr;	// owned by family for all protocols
       
   134 	SBtTls		iBTTls;
       
   135 	CAsyncCallBack* iCallback;
       
   136 	RBTControlPlane	iControlPlane;
       
   137 	TInt iBTRefCount;
       
   138 	CBTCodServiceMan*	iCodServiceMan;	
       
   139 	};
       
   140 
       
   141 NONSHARABLE_CLASS(CBluetoothProtocolBase) : public CProtocolBase
       
   142 /**
       
   143 	Abstract class forming the base classes for all Bluetooth protocol objects
       
   144 	
       
   145 	Provides the basics such as listening and security
       
   146 **/
       
   147 	{
       
   148 public:
       
   149 	virtual TInt BearerConnectComplete(const TBTDevAddr& aAddr, CServProviderBase* aSSP) =0;
       
   150 	virtual TInt ControlPlaneMessage(TBTControlPlaneMessage aMessage, TAny* aParam);
       
   151 
       
   152 	virtual TInt StartProtocolListening();
       
   153 	virtual void StopProtocolListening();
       
   154 
       
   155 	inline CBTSecMan& SecMan() const;
       
   156 	inline RBTControlPlane& ControlPlane() const;
       
   157 	inline TBool IsListening() const;
       
   158 	inline CIncomingConnectionListener& Listener() const;
       
   159 	inline CProtocolBase* LowerProtocol() const;
       
   160 	inline CBTCodServiceMan& CodMan() const;
       
   161 
       
   162 	TInt IncrementListeners(); // called by any object that is prepared to listen at layer n
       
   163 	void DecrementListeners(); // called by any object that is prepared to listen at layer n
       
   164 	virtual void Error(TInt aError,CProtocolBase* aSourceProtocol=NULL);
       
   165 	
       
   166 protected:
       
   167 	CBluetoothProtocolBase*			iLowerProtocol;
       
   168 	CIncomingConnectionListener*	iListener;
       
   169 
       
   170 protected:
       
   171 	TInt StartListening(TUint aPort, TUint aSockType, TUint aQueSize, TUid aUid);
       
   172 	CBluetoothProtocolBase(CBTSecMan& aSecMan, RBTControlPlane& aControlPlane, CBTCodServiceMan& aCodMan);
       
   173 	~CBluetoothProtocolBase();
       
   174 
       
   175 private:
       
   176 	void DoStartListeningL(TUint aPort, TUint aSockType, TUint aQueSize, TUid aUid);
       
   177 private:
       
   178 	CBTSecMan&			iSecMan;
       
   179 	RBTControlPlane&	iControlPlane;
       
   180 	TInt				iListeningEntities;
       
   181 	CBTCodServiceMan&	iCodMan;
       
   182 	};
       
   183 
       
   184 inline CBTSecMan& CBluetoothProtocolBase::SecMan() const
       
   185 	{
       
   186 	return iSecMan;
       
   187 	}
       
   188 
       
   189 inline RBTControlPlane& CBluetoothProtocolBase::ControlPlane() const
       
   190 	{
       
   191 	return iControlPlane;
       
   192 	}
       
   193 
       
   194 inline TBool CBluetoothProtocolBase::IsListening() const
       
   195 	{
       
   196 	return(iListeningEntities > 0);
       
   197 	}
       
   198 
       
   199 inline CProtocolBase* CBluetoothProtocolBase::LowerProtocol() const
       
   200 	{
       
   201 	return iLowerProtocol;
       
   202 	}
       
   203 
       
   204 inline CIncomingConnectionListener& CBluetoothProtocolBase::Listener() const
       
   205 	{
       
   206 	return *iListener;
       
   207 	}
       
   208 
       
   209 inline CBTCodServiceMan& CBluetoothProtocolBase::CodMan() const
       
   210 	{
       
   211 	return iCodMan;
       
   212 	}
       
   213 
       
   214 #endif