bluetoothengine/btnotif/btnotifsrv/inc/btnotifconnectiontracker.h
changeset 31 a0ea99b6fa53
parent 29 48ae3789ce00
child 40 997690c3397a
equal deleted inserted replaced
30:df7a93ede42e 31:a0ea99b6fa53
     1 /*
     1 /*
     2 * ============================================================================
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
     3 *  Name        : btnotifconnectiontracker.h
     3 * All rights reserved.
     4 *  Part of     : bluetoothengine / btnotif
     4 * This component and the accompanying materials are made available
     5 *  Description : Bluetooth connection tracker and manager.
     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".
     6 *
     8 *
     7 *  Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies).
     9 * Initial Contributors:
     8 *  All rights reserved.
    10 * Nokia Corporation - initial contribution.
     9 *  This component and the accompanying materials are made available
       
    10 *  under the terms of "Eclipse Public License v1.0"
       
    11 *  which accompanies this distribution, and is available
       
    12 *  at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
    13 *
    11 *
    14 *  Initial Contributors:
    12 * Contributors:
    15 *  Nokia Corporation - initial contribution.
       
    16 *
    13 *
    17 *  Contributors:
    14 * Description: Bluetooth connection tracker and manager.
    18 *  Nokia Corporation
    15 *
    19 * ============================================================================
       
    20 * Template version: 4.2
       
    21 */
    16 */
    22 
    17 
    23 #ifndef BTNOTIFCONNECTIONTRACKER_H
    18 #ifndef BTNOTIFCONNECTIONTRACKER_H
    24 #define BTNOTIFCONNECTIONTRACKER_H
    19 #define BTNOTIFCONNECTIONTRACKER_H
    25 
    20 
    26 
    21 
    27 #include <e32property.h>
    22 #include <e32property.h>
    28 #include <btmanclient.h>
    23 #include <btmanclient.h>
    29 #include <bluetooth/pairing.h>
    24 #include <bluetooth/pairing.h>
    30 #include <btengconnman.h>
       
    31 #include <btservices/btsimpleactive.h>
       
    32 #include "btnotifserver.h"
    25 #include "btnotifserver.h"
    33 
    26 
    34 class CBTNotifConnection;
       
    35 class CBTNotificationManager;
    27 class CBTNotificationManager;
    36 class CbtnotifConnectionTrackerTest;
    28 class CbtnotifConnectionTrackerTest;
       
    29 class CBTNotifPairingManager;
    37 
    30 
    38 
    31 
    39 
    32 
    40 /**
    33 /**
    41  *  CBTNotifConnectionTracker keeps track of remote device connections
    34  *  CBTNotifConnectionTracker keeps track of remote device connections
    42  *  
    35  *  
    43  *  @since Symbian^4
    36  *  @since Symbian^4
    44  */
    37  */
    45 NONSHARABLE_CLASS( CBTNotifConnectionTracker ) : public CBase,
    38 NONSHARABLE_CLASS( CBTNotifConnectionTracker ) : public CBase
    46                                                  public MBluetoothPhysicalLinksNotifier,
       
    47                                                  public MBTEngConnObserver,
       
    48                                                  public MBtSimpleActiveObserver
       
    49     {
    39     {
    50 
    40 
    51 public:
    41 public:
    52 
    42 
    53     /**
    43     /**
    97      * @since Symbian^4
    87      * @since Symbian^4
    98      * @return Session with the socket server.
    88      * @return Session with the socket server.
    99      */
    89      */
   100     inline RSocketServ& SocketServerSession()
    90     inline RSocketServ& SocketServerSession()
   101         { return iSockServ; }
    91         { return iSockServ; }
   102 
    92   
   103     /**
    93     /**
   104      * Get the handle to the Bluetooth pairing server.
    94      * Processes a message for notifiers related to pairings.
   105      * This handle can be used for creating subsessions.
    95      * ( These are usually issued by BT stack. However, any application
   106      *
    96      * is not restricted to do so (e.g., for testing purpose).
   107      * @since Symbian^4
       
   108      * @return Session with the socket server or NULL.
       
   109      */
       
   110     inline RBluetoothPairingServer* PairingServerSession()
       
   111         { return iPairingServ; }
       
   112 
       
   113     /**
       
   114      * Processes a message from a notifier client related to connections.
       
   115      *
    97      *
   116      * @since Symbian^4
    98      * @since Symbian^4
   117      * @param aMessage The message containing the details of the client request.
    99      * @param aMessage The message containing the details of the client request.
   118      */
   100      */
   119     void DispatchNotifierMessageL( const RMessage2& aMessage );
   101     void HandlePairingNotifierRequestL( const RMessage2& aMessage );
   120 
   102     
   121     /**
   103     /**
   122      * Handle a request related to pairing.
   104      * Handle a request related to pairing.
   123      *
   105      *
   124      * @since Symbian^4
   106      * @since Symbian^4
   125      * @param aUid The UID of the notification request.
   107      * @param aMessage The message containing the details of the client request.
   126      */
   108      */
   127     void HandleBondingRequestL( const RMessage2& aMessage );
   109     void HandleBondingRequestL( const RMessage2& aMessage );
   128 
       
   129     /**
       
   130      * Handle a change in the number of physical connections.
       
   131      * This also handles the case where a connection monitor has finished
       
   132      * its processing, and is now ready for removal.
       
   133      *
       
   134      * @since Symbian^4
       
   135      */
       
   136     void HandleLinkCountChangeL();
       
   137 
   110 
   138     /**
   111     /**
   139      * Check repeated connection attempts, and record rejected/accepted queries.
   112      * Check repeated connection attempts, and record rejected/accepted queries.
   140      *
   113      *
   141      * @since Symbian^4
   114      * @since Symbian^4
   144      * @return ETrue if the user should be queried for blocking this device,
   117      * @return ETrue if the user should be queried for blocking this device,
   145      *         EFalse if no query should be launched by the caller.
   118      *         EFalse if no query should be launched by the caller.
   146      */
   119      */
   147     TBool UpdateBlockingHistoryL( const CBTDevice* aDevice, TBool aAccepted );
   120     TBool UpdateBlockingHistoryL( const CBTDevice* aDevice, TBool aAccepted );
   148 
   121 
   149 // from base class MBluetoothPhysicalLinksNotifier
       
   150     /** Notification of a requested connection coming up.
       
   151      * If no error is reported, then that connection is ready for use.
       
   152      * 
       
   153      * @since Symbian^4
       
   154      * @param aErr the returned error
       
   155     */
       
   156     virtual void HandleCreateConnectionCompleteL( TInt aErr );
       
   157 
       
   158     /** Notification of a requested disconnection having taken place.
       
   159      * If no error is reported, then that connection has been closed.
       
   160      * 
       
   161      * @since Symbian^4
       
   162      * @param aErr the returned error
       
   163     */
       
   164     virtual void HandleDisconnectCompleteL( TInt aErr );
       
   165 
       
   166     /** Notification that all existing connections have been torn down.
       
   167      * If no error is reported, then there are no Bluetooth connections existing.
       
   168      * 
       
   169      * @since Symbian^4
       
   170      * @param aErr the returned error
       
   171     */
       
   172     virtual void HandleDisconnectAllCompleteL( TInt aErr );
       
   173 
       
   174 // from base class MBTEngConnObserver
       
   175 
       
   176     /**
       
   177      * From MBTEngConnObserver.
       
   178      * Indicates to the caller that a service-level connection has completed.
       
   179      * This function is called for both incoming and outgoing connections. 
       
   180      * This function is also called when an outgoing connection request fails, 
       
   181      * e.g. with error code KErrCouldNotConnect.
       
   182      * When this function is called, new commands can be issued to the 
       
   183      * CBTEngConnMan API immediately.
       
   184      *
       
   185      * @since  S60 v3.2
       
   186      * @param  aAddr The address of the remote device.
       
   187      * @param  aErr Status information of the connection. KErrNone if the
       
   188      *              connection succeeded, otherwise the error code with 
       
   189      *              which the outgoing connection failed. KErrAlreadyExists 
       
   190      *              is returned if there already is an existing connection 
       
   191      *              for the selected profile(s), or otherwise e.g. 
       
   192      *              KErrCouldNotConnect or KErrDisconnected for indicating 
       
   193      *              connection problems.
       
   194      * @param  aConflicts If there already is a connection for the selected 
       
   195      *                    profile(s) of an outgoing connection request (the 
       
   196      *                    selection is performed by BTEng), then this array 
       
   197      *                    contains the bluetooth device addresses of the 
       
   198      *                    remote devices for those connections.
       
   199      */
       
   200     virtual void ConnectComplete( TBTDevAddr& aAddr, TInt aErr, 
       
   201                                    RBTDevAddrArray* aConflicts = NULL );
       
   202 
       
   203     /**
       
   204      * From MBTEngConnObserver.
       
   205      * Indicates to the caller that a service-level connection has disconnected.
       
   206      * When this function is called, new commands can be issued to the 
       
   207      * CBTEngConnMan API immediately.
       
   208      *
       
   209      * @since  S60 v3.2
       
   210      * @param  aAddr The address of the remote device.
       
   211      * @param  aErr The error code with which the disconnection occured. 
       
   212      *              KErrNone for a normal disconnection, 
       
   213      *              or e.g. KErrDisconnected if the connection was lost.
       
   214      */
       
   215     virtual void DisconnectComplete( TBTDevAddr& aAddr, TInt aErr );
       
   216 
       
   217 // from base class MBtSimpleActiveObserver
       
   218 
       
   219     /**
       
   220      * From MBtSimpleActiveObserver.
       
   221      * Callback to notify that an outstanding request has completed.
       
   222      *
       
   223      * @since Symbian^4
       
   224      * @param aActive The active object helper that completed this request.
       
   225      * @param aStatus The status of the completed request.
       
   226      */
       
   227     virtual void RequestCompletedL( CBtSimpleActive* aActive, TInt aStatus );
       
   228 
       
   229     /**
       
   230      * From MBtSimpleActiveObserver.
       
   231      * Callback for handling cancelation of an outstanding request.
       
   232      *
       
   233      * @since Symbian^4
       
   234      * @param aId The ID that identifies the outstanding request.
       
   235      */
       
   236     virtual void CancelRequest( TInt aRequestId );
       
   237     
       
   238     /**
       
   239      * Callback to notify that an error has occurred in RunL.
       
   240      *
       
   241      * @param aActive Pointer to the active object that completed.
       
   242      * @param sError The error occured in RunL
       
   243      */
       
   244     virtual void HandleError( CBtSimpleActive* aActive,
       
   245                                         TInt aError );
       
   246     
       
   247 private:
   122 private:
   248 
   123 
   249     CBTNotifConnectionTracker( CBTNotifServer* aServer );
   124     CBTNotifConnectionTracker( CBTNotifServer* aServer );
   250 
   125 
   251     void ConstructL();
   126     void ConstructL();
   252 
       
   253     /**
       
   254      * Parse a client message and find the correct connection handler
       
   255      * for  dispatching a request.
       
   256      * The parsed client message may be a request that is being served,
       
   257      * in case a cancel request has come in. The opcode identifies the
       
   258      * client message that is currently being dispatched.
       
   259      *
       
   260      * @since Symbian^4
       
   261      * @param aOpcode The opcode of the original request.
       
   262      * @param aMessage The client message to parse.
       
   263      * @param aBuffer On return this contains the parameters read from the message.
       
   264      * @return The connection that is identified by the address in the
       
   265      *         message, or NULL if none found.
       
   266      */
       
   267     CBTNotifConnection* FindConnectionFromMessageL( TInt aOpcode, 
       
   268                 const RMessage2& aMessage, TDes8& aBuffer );
       
   269 
       
   270     /**
       
   271      * Parse the Bluetooth address from a request for some notification.
       
   272      *
       
   273      * @since Symbian^4
       
   274      * @param aUid The UID of the notification request.
       
   275      * @param aParamsBuf Descriptor containing the parameter read from the
       
   276      *        client message.
       
   277      * @return Bluetooth address parsed from the descriptor.
       
   278      */
       
   279     TBTDevAddr ParseAddressL( TInt aUid, const TDesC8& aParamsBuf ) const;
       
   280 
       
   281     /**
       
   282      * Find the handler of a specific connection.
       
   283      *
       
   284      * @since Symbian^4
       
   285      * @param aAddr The remote device address identifying the connection.
       
   286      * @return Connnection handler or NULL;
       
   287      */
       
   288     CBTNotifConnection* FindConnectionHandler( const TBTDevAddr& aAddr ) const;
       
   289 
   127 
   290     /**
   128     /**
   291      * Record and check the time between connection attempts.
   129      * Record and check the time between connection attempts.
   292      *
   130      *
   293      * @since Symbian^4
   131      * @since Symbian^4
   296      *         otherwise ETrue.
   134      *         otherwise ETrue.
   297      */
   135      */
   298     TBool RecordConnectionAttempts( TBool aAccepted );
   136     TBool RecordConnectionAttempts( TBool aAccepted );
   299 
   137 
   300 private: // data
   138 private: // data
   301 
       
   302     /**
       
   303      * PubSub key for tracking the number of connections.
       
   304      */
       
   305     RProperty iLinkCount;
       
   306 
       
   307 // ToDo: remove this when registry notifications API is available!!
       
   308     /**
       
   309      * PubSub key for tracking registry changes.
       
   310      */
       
   311     RProperty iRegistryChange;
       
   312 
       
   313     /**
       
   314      * Helper active object for observing the link count.
       
   315      * Own.
       
   316      */
       
   317     CBtSimpleActive* iRegistryActive;
       
   318 // End ToDo
       
   319 
       
   320     /**
       
   321      * Array of pointers to observers of individual connections.
       
   322      */
       
   323     RPointerArray<CBTNotifConnection> iConnArray;
       
   324 //    RHashMap<TBTDevAddr,CBTNotifConnection*> iConnArray;
       
   325 
   139 
   326     /**
   140     /**
   327      * Time of the last denied connection attempt.
   141      * Time of the last denied connection attempt.
   328      */
   142      */
   329     TInt64 iLastReject;
   143     TInt64 iLastReject;
   342      * Single session with the socket server, to be used for subsessions.
   156      * Single session with the socket server, to be used for subsessions.
   343      */
   157      */
   344     RSocketServ iSockServ;
   158     RSocketServ iSockServ;
   345 
   159 
   346     /**
   160     /**
   347      * Address placeholder for receiving secure simple pairing results.
       
   348      */
       
   349     TBTDevAddr iSspResultAddr;
       
   350 
       
   351     /**
       
   352      * Session with pairing server for receiving secure simple pairing results.
       
   353      */
       
   354     RBluetoothPairingResult iSspResultSession;
       
   355 
       
   356     /**
       
   357      * Single session with the pairing server, to be used for subsessions.
       
   358      * Own.
       
   359      */
       
   360     RBluetoothPairingServer* iPairingServ;
       
   361 
       
   362     /**
       
   363      * Object for managing the piconet.
       
   364      * Own.
       
   365      */
       
   366     CBTEngConnMan* iConnMan;
       
   367 
       
   368     /**
       
   369      * Object for managing the piconet.
       
   370      * Own.
       
   371      */
       
   372     CBluetoothPhysicalLinks* iPhyLinks;
       
   373 
       
   374     /**
       
   375      * Helper active object for observing the link count.
       
   376      * Own.
       
   377      */
       
   378     CBtSimpleActive* iLinkCountActive;
       
   379 
       
   380     /**
       
   381      * Helper active object for receiving secure simple pairing results.
       
   382      * Own.
       
   383      */
       
   384     CBtSimpleActive* iSspResultActive;
       
   385 
       
   386     /**
       
   387      * Reference to our parent the server class.
   161      * Reference to our parent the server class.
   388      * Not own.
   162      * Not own.
   389      */
   163      */
   390     CBTNotifServer* iServer;
   164     CBTNotifServer* iServer;
       
   165     
       
   166     /**
       
   167      * Object for managing the application pairing.
       
   168      * Own.
       
   169      */
       
   170     CBTNotifPairingManager* iPairingManager;
   391 
   171 
   392     BTUNITTESTHOOK
   172     BTUNITTESTHOOK
   393 
   173 
   394     };
   174     };
   395 
   175