bluetoothengine/btmac/src/BTMonoCmdHandler/btmcbattery.cpp
changeset 0 f63038272f30
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2005 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:  General Active Object offering asynchronous service
       
    15 *
       
    16 */
       
    17 
       
    18 #include "atcodec.h"
       
    19 #include "btmcbattery.h"
       
    20 #include "btmcphonestatus.h"
       
    21 #include "debug.h"
       
    22 #include <e32math.h>
       
    23 
       
    24 const TInt8 KMaxPhoneStrength = 7;
       
    25 const TInt8 KMaxHFPStrength = 5;
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CBtmcBattery::NewL
       
    29 // -----------------------------------------------------------------------------
       
    30 CBtmcBattery* CBtmcBattery::NewL(CBtmcPhoneStatus& aParent)
       
    31     {
       
    32     CBtmcBattery* self = CBtmcBattery::NewLC(aParent);
       
    33     CleanupStack::Pop(self);
       
    34     return self;
       
    35     }
       
    36 
       
    37 // -----------------------------------------------------------------------------
       
    38 // CBtmcBattery::NewLC
       
    39 // -----------------------------------------------------------------------------
       
    40 CBtmcBattery* CBtmcBattery::NewLC(CBtmcPhoneStatus& aParent)
       
    41     {
       
    42     CBtmcBattery* self = new (ELeave) CBtmcBattery(aParent);
       
    43     CleanupStack::PushL(self);
       
    44     self->ConstructL();
       
    45     return self;
       
    46     }
       
    47 
       
    48 // -----------------------------------------------------------------------------
       
    49 // CBtmcBattery::~CBtmcBattery
       
    50 // -----------------------------------------------------------------------------
       
    51 CBtmcBattery::~CBtmcBattery()
       
    52     {
       
    53     TRACE_FUNC_ENTRY
       
    54     Cancel();
       
    55     iProperty.Close();
       
    56     TRACE_FUNC_EXIT
       
    57     }
       
    58 
       
    59 // -------------------------------------------------------------------------------
       
    60 // CBtmcBattery::GoActive
       
    61 // -------------------------------------------------------------------------------
       
    62 void CBtmcBattery::GoActive()
       
    63     {
       
    64     TRACE_ASSERT(!IsActive(), KErrGeneral);
       
    65     iProperty.Subscribe(iStatus);
       
    66     SetActive();
       
    67     TRACE_FUNC
       
    68     }
       
    69 
       
    70 
       
    71 // -------------------------------------------------------------------------------
       
    72 // CBtmcBattery::RunL
       
    73 // -------------------------------------------------------------------------------
       
    74 void CBtmcBattery::RunL()
       
    75     {
       
    76     TRACE_FUNC_ENTRY
       
    77     if (iStatus == KErrNone)
       
    78         {    
       
    79         TInt temp = 0;
       
    80         TInt err = iProperty.Get(temp);
       
    81         if (!err)
       
    82             {
       
    83             ConvertToHFPScale(temp);
       
    84             iCharge = temp;
       
    85             TRACE_INFO((_L("iStatus %d, battery strength %d"), iStatus.Int(), temp))
       
    86             iParent.HandleBatteryChangeL(temp);            
       
    87             }
       
    88         GoActive();
       
    89         }
       
    90     else
       
    91         {
       
    92         TRACE_ERROR((_L("ERROR %d"), iStatus.Int()))
       
    93         }        
       
    94     TRACE_FUNC_EXIT
       
    95     }
       
    96 
       
    97 // -------------------------------------------------------------------------------
       
    98 // CBtmcBattery::DoCancel
       
    99 // -------------------------------------------------------------------------------
       
   100 void CBtmcBattery::DoCancel()
       
   101     {
       
   102     iProperty.Cancel();
       
   103     TRACE_FUNC
       
   104     }
       
   105 
       
   106 
       
   107 // -------------------------------------------------------------------------------
       
   108 // CBtmcBattery::RunError
       
   109 // -------------------------------------------------------------------------------
       
   110 TInt CBtmcBattery::RunError(TInt /*aErr*/)
       
   111     {
       
   112     iProperty.Close();
       
   113     TRACE_FUNC
       
   114     return KErrNone;
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CBtmcBattery::CBtmcBattery
       
   119 // -----------------------------------------------------------------------------
       
   120 CBtmcBattery::CBtmcBattery(CBtmcPhoneStatus& aParent)
       
   121     : CActive(EPriorityNormal), iParent(aParent)
       
   122     {
       
   123     CActiveScheduler::Add(this);
       
   124     }
       
   125 
       
   126 void CBtmcBattery::ConstructL()
       
   127     {
       
   128     TRACE_FUNC_ENTRY
       
   129     LEAVE_IF_ERROR(iProperty.Attach(KPSUidHWRMPowerState, KHWRMBatteryLevel));
       
   130     LEAVE_IF_ERROR(iProperty.Get(iCharge));
       
   131     ConvertToHFPScale(iCharge);
       
   132     TRACE_FUNC_EXIT
       
   133     }
       
   134 
       
   135 void CBtmcBattery::ConvertToHFPScale(TInt &aStrength)
       
   136     {
       
   137     TReal result;
       
   138     Math::Round( result, TReal(TReal(KMaxHFPStrength)/TReal(KMaxPhoneStrength) *TReal(aStrength)),0);
       
   139     aStrength = TInt8(result);    
       
   140     }
       
   141 
       
   142 TInt CBtmcBattery::GetBatteryCharge()
       
   143     {
       
   144     return iCharge;
       
   145     }
       
   146 
       
   147 
       
   148 // End of File