bluetoothengine/btaudioman/src/basrvpowercontrolagent.cpp
changeset 0 f63038272f30
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "basrvpowercontrolagent.h"
       
    21 #include "debug.h"
       
    22 
       
    23 enum TActiveRequestId
       
    24     {
       
    25     ESniffDelayTimer = 80,
       
    26     EBbEventSubscribe = 81,
       
    27     };
       
    28 
       
    29 const TInt KSniffRequesterDelay = 5000000; // 5 secs
       
    30     
       
    31 CBasrvPowerControlAgent* CBasrvPowerControlAgent::NewL(const TBTDevAddr& aAddr)
       
    32     {
       
    33     CBasrvPowerControlAgent* self = new CBasrvPowerControlAgent(aAddr);
       
    34     CleanupStack::PushL(self);
       
    35     self->ConstructL();
       
    36     CleanupStack::Pop(self);
       
    37     return self;    
       
    38     }
       
    39 
       
    40 CBasrvPowerControlAgent::~CBasrvPowerControlAgent()
       
    41     {
       
    42     TRACE_FUNC
       
    43     delete iBbEventer;
       
    44     delete iSniffDelayTimer;
       
    45     iTimer.Close();
       
    46     iPlAda.Close();
       
    47     iSockServ.Close();
       
    48     }
       
    49 
       
    50 void CBasrvPowerControlAgent::RequestSniffMode()
       
    51     {
       
    52     TRACE_FUNC
       
    53     if (iState == ESniffModeRequested || iState == ESniffModeTiming)
       
    54         {
       
    55         return;
       
    56         }
       
    57     if (iState == EActiveModeRequested)
       
    58         {
       
    59         iPlAda.CancelLowPowerModeRequester();
       
    60         }
       
    61     iState = ESniffModeTiming;
       
    62     iTimer.After(iSniffDelayTimer->iStatus, KSniffRequesterDelay);
       
    63     iSniffDelayTimer->GoActive();
       
    64     TRACE_INFO(_L("CBasrvPowerControlAgent, [Sniff mode Timing]"))
       
    65     }
       
    66 
       
    67 void CBasrvPowerControlAgent::RequestActiveMode()
       
    68     {
       
    69     TRACE_FUNC
       
    70     if (iState == EActiveModeRequested)
       
    71         {
       
    72         return;
       
    73         }
       
    74     if (iState == ESniffModeTiming)
       
    75         {
       
    76         iSniffDelayTimer->Cancel();
       
    77         }
       
    78     TInt err = iPlAda.ActivateActiveRequester();
       
    79     if (!err)
       
    80         {
       
    81         iState = EActiveModeRequested;
       
    82         TRACE_INFO(_L("CBasrvPowerControlAgent, [Active mode requested]"))
       
    83         }
       
    84     else
       
    85         {
       
    86         TRACE_INFO((_L("CBasrvPowerControlAgent, ActivateActiveRequester %d"), err))
       
    87         iState = EPowerModeDefault;
       
    88         }
       
    89     }
       
    90 
       
    91 void CBasrvPowerControlAgent::CancelPowerModeControl()
       
    92     {
       
    93     TRACE_FUNC
       
    94     if (iState == ESniffModeTiming)
       
    95         {
       
    96         iSniffDelayTimer->Cancel();
       
    97         }
       
    98     else if (iState == ESniffModeRequested || iState == EActiveModeRequested)
       
    99         {
       
   100         iPlAda.CancelLowPowerModeRequester();
       
   101         }
       
   102     iState = EPowerModeDefault;
       
   103     TRACE_INFO(_L("CBasrvPowerControlAgent, [Power mode default]"))
       
   104     }
       
   105 
       
   106 void CBasrvPowerControlAgent::PreventLowPowerMode()
       
   107     {
       
   108     TRACE_FUNC
       
   109     (void) iPlAda.PreventLowPowerModes(EAnyLowPowerMode);
       
   110     }
       
   111     
       
   112 void CBasrvPowerControlAgent::AllowLowPowerMode()
       
   113     {
       
   114     TRACE_FUNC
       
   115     (void) iPlAda.AllowLowPowerModes(EAnyLowPowerMode);
       
   116     }
       
   117 
       
   118 
       
   119 void CBasrvPowerControlAgent::RequestCompletedL(CBasrvActive& aActive)
       
   120     {
       
   121     TRACE_FUNC
       
   122     switch (aActive.RequestId())
       
   123         {
       
   124         case ESniffDelayTimer:
       
   125             {
       
   126             DosniffRequest();
       
   127             break;
       
   128             }
       
   129         case EBbEventSubscribe:
       
   130             {
       
   131         	TRACE_INFO((_L("CBasrvPowerControlAgent, err %d, event 0x%02X"), aActive.iStatus.Int(), iBbEvent().EventType()))
       
   132             if (aActive.iStatus != KErrNone)
       
   133                 return;
       
   134         	switch(iBbEvent().EventType())
       
   135         		{
       
   136         		case ENotifyActiveMode://0x04
       
   137         		    {
       
   138                     if (iState == ESniffModeTiming || iState == ESniffModeRequested)
       
   139                         {
       
   140                         CancelPowerModeControl();
       
   141                         }
       
   142         			break;
       
   143         		    }
       
   144         		case ENotifySniffMode: //0x08
       
   145         		    {
       
   146                     if (iState == ESniffModeTiming)
       
   147                         {
       
   148                         iSniffDelayTimer->Cancel();
       
   149                         DosniffRequest();
       
   150                         }
       
   151         			break;
       
   152         		    }
       
   153         		default:
       
   154         			{
       
   155         			TRACE_INFO((_L("CBasrvPowerControlAgent, Uninteresting event (0x%08X)"), iBbEvent().EventType()))
       
   156         			}
       
   157         		}
       
   158             iPlAda.NotifyNextBasebandChangeEvent(iBbEvent, iBbEventer->iStatus, ENotifyAnyPowerMode);
       
   159             iBbEventer->GoActive();
       
   160             }
       
   161         }
       
   162     }
       
   163     
       
   164 void CBasrvPowerControlAgent::CancelRequest(CBasrvActive& aActive)
       
   165     {
       
   166     TRACE_FUNC
       
   167     if (aActive.RequestId() == ESniffDelayTimer)
       
   168         {
       
   169         iTimer.Cancel();
       
   170         }
       
   171     else if (aActive.RequestId() == EBbEventSubscribe)
       
   172         {
       
   173         iPlAda.CancelNextBasebandChangeEventNotifier();
       
   174         }
       
   175     }
       
   176 
       
   177 CBasrvPowerControlAgent::CBasrvPowerControlAgent(const TBTDevAddr& aAddr)
       
   178     : iAddr(aAddr)
       
   179     {
       
   180     TRACE_FUNC
       
   181     }
       
   182 
       
   183 void CBasrvPowerControlAgent::ConstructL()
       
   184     {
       
   185     LEAVE_IF_ERROR(iSockServ.Connect())
       
   186     LEAVE_IF_ERROR(iPlAda.Open(iSockServ, iAddr))
       
   187     iBbEventer = CBasrvActive::NewL(*this, CActive::EPriorityStandard, EBbEventSubscribe);
       
   188     iSniffDelayTimer = CBasrvActive::NewL(*this, CActive::EPriorityStandard, ESniffDelayTimer);
       
   189     LEAVE_IF_ERROR(iTimer.CreateLocal())
       
   190     iPlAda.NotifyNextBasebandChangeEvent(iBbEvent, iBbEventer->iStatus, ENotifyAnyPowerMode);
       
   191     iBbEventer->GoActive();
       
   192     }
       
   193 
       
   194 void CBasrvPowerControlAgent::DosniffRequest()
       
   195     {
       
   196     TRACE_FUNC  
       
   197     (void) iPlAda.AllowLowPowerModes(EAnyLowPowerMode);
       
   198     (void) iPlAda.CancelLowPowerModeRequester();
       
   199     TInt err = iPlAda.ActivateSniffRequester();
       
   200     if (!err)
       
   201         {
       
   202         TRACE_INFO(_L("CBasrvPowerControlAgent, [Sniff mode requested]"))
       
   203         iState = ESniffModeRequested;
       
   204         }
       
   205     else
       
   206         {
       
   207         TRACE_INFO((_L("CBasrvPowerControlAgent, ActivateSniffRequester %d"), err))
       
   208         iState = EPowerModeDefault;
       
   209         }
       
   210     }