bluetoothcommsprofiles/btpan/bnep/CBTAddrSubscriber.cpp
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 // Copyright (c) 2004-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 //
       
    15 
       
    16 /**
       
    17  @file
       
    18  @internalComponent
       
    19 */
       
    20 
       
    21 #include <bluetooth/logger.h>
       
    22 #include <es_prot.h>
       
    23 #include <bt_subscribe.h>
       
    24 #include "CBTAddrSubscriber.h"
       
    25 
       
    26 #ifdef __FLOG_ACTIVE
       
    27 _LIT8(KLogComponent, LOG_COMPONENT_PAN_BNEP);
       
    28 #endif
       
    29 
       
    30 CBTAddrSubscriber::CBTAddrSubscriber (TBTDevAddr& aAddr, CBnepLocalDevice& aLocal)
       
    31         : CActive(EPriorityStandard), iAddr(aAddr), iOwner(aLocal)
       
    32     {
       
    33     LOG_FUNC
       
    34     CActiveScheduler::Add(this);
       
    35     }
       
    36 
       
    37 CBTAddrSubscriber::~CBTAddrSubscriber()
       
    38     {
       
    39     LOG_FUNC
       
    40     Cancel();
       
    41     }
       
    42 
       
    43 /**
       
    44    Create an address subscriber and queue the first request.
       
    45    @param aAddr The address to update with published data.
       
    46    @param aLocal The local device node which will be informed when the device
       
    47    address changes.
       
    48 */
       
    49 CBTAddrSubscriber* CBTAddrSubscriber::NewL (TBTDevAddr& aAddr, CBnepLocalDevice& aLocal)
       
    50     {
       
    51     LOG_STATIC_FUNC
       
    52     CBTAddrSubscriber* self = new(ELeave) CBTAddrSubscriber(aAddr, aLocal);
       
    53     CleanupStack::PushL(self);
       
    54     User::LeaveIfError(self->iDevAddrProperty.Attach(KPropertyUidBluetoothCategory, KPropertyKeyBluetoothGetLocalDeviceAddress));
       
    55 
       
    56     // Attempt to fetch the device address property and activate the AO subscription on 
       
    57     // the property so that the upper layers can be notified when it is initialised by 
       
    58     // the BT stack. If the property hasn't been initialised yet, SyncFetch will 
       
    59     // return KErrUnderflow. In this context it is alright to ignore that return value.
       
    60     self->SyncFetch(ETrue);
       
    61     LOG(_L8("CBTAddrSubscriber requesting notifications"));
       
    62     CleanupStack::Pop(self);
       
    63     return self;
       
    64     }
       
    65 
       
    66 /**
       
    67    Having been notified of a BDADDR change, notify owner if we
       
    68    have an address at the moment.
       
    69 */
       
    70 void CBTAddrSubscriber::RunL ()
       
    71     {
       
    72     LOG_FUNC
       
    73     // Don't plan to restart the active object since the device address
       
    74     // is unlikely to change after this runs once. However, the AO
       
    75     // will reactivate if the property could not be fetched
       
    76     // properly (i.e. err != KErrNone)
       
    77     TInt err = SyncFetch(); 
       
    78     if(KErrNone == err)
       
    79         {
       
    80         // Notify the upper layers of the address update.
       
    81         iOwner.BDADDRChanged();
       
    82         }
       
    83     else
       
    84         {
       
    85         iDevAddrProperty.Subscribe(iStatus);
       
    86         SetActive();
       
    87         }
       
    88     }
       
    89 
       
    90 void CBTAddrSubscriber::DoCancel ()
       
    91     {
       
    92     LOG_FUNC
       
    93     iDevAddrProperty.Cancel();
       
    94     }
       
    95 
       
    96 /**
       
    97    Fetches the device address property synchronously if it exists.
       
    98    Conditionally queue a request to be notified of future BDADDR changes.
       
    99    By default, a request is not queued automatically.
       
   100    @param aRestart specify whether to queue request and schedule the active object. Default is EFalse.
       
   101    @return KErrUnderflow The property has zero length (i.e. is uninitialised)
       
   102    @internalComponent
       
   103 */
       
   104 TInt CBTAddrSubscriber::SyncFetch (TBool aRestart)
       
   105     {
       
   106     LOG_FUNC
       
   107     TInt retval = KErrNone;
       
   108     TBuf8<KBTDevAddrSize> addr;
       
   109     iDevAddrProperty.Get(addr);
       
   110     if (addr.Length())
       
   111         {
       
   112         iAddr = TBTDevAddr(addr);
       
   113         LOG_BT(_L8("Setting address to %S"), iAddr);
       
   114         }
       
   115     else
       
   116         {
       
   117         // The property hasn't been initialised yet.
       
   118         retval = KErrUnderflow;
       
   119         }
       
   120 
       
   121     if(aRestart)
       
   122         {
       
   123 		if(retval == KErrNone)
       
   124 			{
       
   125 			// The BT stack was already up when BNEP started.  Call the
       
   126 			// BDADDRChanged method to inform the NIF that the link layer 
       
   127 			// is up.
       
   128 			iOwner.BDADDRChanged();
       
   129 			}
       
   130 		else
       
   131 			{
       
   132 	        // Watch out for any future updates on the property.
       
   133 	        iDevAddrProperty.Subscribe(iStatus);
       
   134 	        SetActive();
       
   135 			}
       
   136         }
       
   137     return retval;
       
   138     }