telephonyserverplugins/multimodetsy/Multimode/Mbattery.cpp
changeset 0 3553901f7fa8
child 24 6638e7f4bd8f
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 // Copyright (c) 1997-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 // GBATTERY.CPP
       
    15 // Queries the ME battery. Retrieves charge level and battery status
       
    16 // 
       
    17 //
       
    18 
       
    19 #include <et_phone.h>
       
    20 #include "Mbattery.h"
       
    21 #include "mSLOGGER.H"
       
    22 #include "ATIO.H"
       
    23 #include "Matstd.h"
       
    24 
       
    25 CATGetBattery* CATGetBattery::NewL(CATIO* aIo,CTelObject* aTelObject,CATInit* aInit,CPhoneGlobals* aPhoneGlobals)
       
    26 	{
       
    27 	CATGetBattery* battery = new(ELeave) CATGetBattery(aIo, aTelObject, aInit, aPhoneGlobals);
       
    28 	CleanupStack::PushL(battery);
       
    29 	battery->ConstructL();
       
    30 	CleanupStack::Pop();
       
    31 	return battery;
       
    32 	}
       
    33 
       
    34 CATGetBattery::CATGetBattery(CATIO* aIo, CTelObject* aTelObject, CATInit* aInit,CPhoneGlobals* aPhoneGlobals)
       
    35 : CATCommands(aIo,aTelObject,aInit,aPhoneGlobals),iPhoneGlobals(aPhoneGlobals)
       
    36 {}
       
    37 
       
    38 void CATGetBattery::ConstructL()
       
    39 	{
       
    40 	CATCommands::ConstructL();
       
    41 	}
       
    42 
       
    43 CATGetBattery::~CATGetBattery()
       
    44 	{
       
    45 	iIo->RemoveExpectStrings(this);
       
    46 	iOKExpectString=NULL;
       
    47 	iErrorExpectString=NULL;
       
    48 	}
       
    49 
       
    50 void CATGetBattery::Start(TTsyReqHandle aTsyReqHandle,TAny* aParams)
       
    51 	{
       
    52 	LOGTEXT(_L8("MMTsy:\tCATGetBattery:\tStarting ME Battery query."));
       
    53 	iReqHandle = aTsyReqHandle;
       
    54 	iBatteryInfo = (RMobilePhone::TMobilePhoneBatteryInfoV1*) aParams;
       
    55 	iTxBuffer.Format(KGetBatteryChargeCommand);
       
    56 	iIo->Write(this, iTxBuffer);
       
    57 	iIo->SetTimeOut(this, 5000);
       
    58 	iState=EATBatteryWaitForWriteComplete;
       
    59 	}
       
    60 
       
    61 void CATGetBattery::EventSignal(TEventSource aSource)
       
    62 	{
       
    63 	if (aSource==ETimeOutCompletion)
       
    64 		{
       
    65 		LOGTEXT(_L8("MMTsy:\tCATGetBattery:\tTimeout Error during battery-strength query"));
       
    66 		RemoveStdExpectStrings();
       
    67 		Complete(KErrTimedOut,aSource);
       
    68 		return;
       
    69 		}
       
    70 
       
    71 	switch(iState)
       
    72 		{
       
    73 		case EATBatteryWaitForWriteComplete:
       
    74 		__ASSERT_ALWAYS(aSource==EWriteCompletion,Panic(EATCommand_IllegalCompletionWriteExpected));
       
    75 			{
       
    76 			iIo->WriteAndTimerCancel(this);
       
    77 			StandardWriteCompletionHandler(aSource, 5);
       
    78 			iState=EATBatteryReadComplete;
       
    79 			}
       
    80 		break;
       
    81 
       
    82 		case EATBatteryReadComplete:
       
    83 		__ASSERT_ALWAYS(aSource==EReadCompletion,Panic(EATCommand_IllegalCompletionReadExpected));
       
    84 			{	
       
    85 			iIo->WriteAndTimerCancel(this);
       
    86 			TInt ret(ValidateExpectString());
       
    87 			RemoveStdExpectStrings();
       
    88 			if (ret)
       
    89 				{
       
    90 				iBatteryInfo->iStatus = RMobilePhone::EPowerStatusUnknown;
       
    91 				Complete(ret,aSource);
       
    92 				return;
       
    93 				}
       
    94 			TRAP(ret,ParseBatteryResponseL());
       
    95 			Complete(ret,aSource);
       
    96 			}
       
    97 		break;
       
    98 
       
    99 		default:
       
   100 		break;
       
   101 		}//switch
       
   102 	}//EventSignal
       
   103 
       
   104 void CATGetBattery::Stop(TTsyReqHandle aTsyReqHandle)
       
   105 	{
       
   106 	__ASSERT_ALWAYS(aTsyReqHandle == iReqHandle, Panic(EIllegalTsyReqHandle));
       
   107 	LOGTEXT2(_L8("CATGetBattery::Stop - Cancelling from state %d"), iState);
       
   108 	
       
   109 	if (iState==EATBatteryWaitForWriteComplete)
       
   110 		{
       
   111 		Complete(KErrCancel, EReadCompletion);
       
   112 		}
       
   113 	// else do nothing because we have already sent the AT command
       
   114 	// or have not actually started doing anything!
       
   115 	}
       
   116 
       
   117 void CATGetBattery::Complete(TInt aError,TEventSource aSource)
       
   118 	{
       
   119 	iIo->WriteAndTimerCancel(this);
       
   120 	iIo->RemoveExpectStrings(this);
       
   121 	iOKExpectString = NULL;
       
   122 	iErrorExpectString = NULL;
       
   123 
       
   124 	CATCommands::Complete(aError,aSource);
       
   125 	iTelObject->ReqCompleted(iReqHandle, aError);
       
   126 
       
   127 	LOGTEXT2(_L8("MMTsy:CATGetBattery:\tCATGetBattery completed with error code : %d"), aError);
       
   128 	iState = EATNotInProgress;
       
   129 	}
       
   130 
       
   131 void CATGetBattery::CompleteWithIOError(TEventSource /*aSource*/,TInt aStatus)
       
   132 	{
       
   133 	if (iState!=EATNotInProgress)
       
   134 		{
       
   135 		iIo->WriteAndTimerCancel(this);
       
   136 		iTelObject->ReqCompleted(iReqHandle, aStatus);
       
   137 		iState = EATNotInProgress;
       
   138 		}
       
   139 	}
       
   140 
       
   141 void CATGetBattery::ParseBatteryResponseL()
       
   142 	{
       
   143 	ParseBufferLC();
       
   144 	CATParamListEntry* entry;
       
   145 	TDblQueIter<CATParamListEntry> iter(iRxResults);
       
   146 	TBool first = ETrue;
       
   147 	TUint iCounter = 0;
       
   148 	TInt ret;
       
   149 
       
   150 	while (entry = iter++, entry != NULL)
       
   151 		{
       
   152 		if (first)
       
   153 			{
       
   154 			first = EFalse;
       
   155 			while (entry->iResultPtr != KCBCResponseString)
       
   156 				{
       
   157 				entry->Deque();
       
   158 				delete entry;
       
   159 				entry = iter++;
       
   160 				if (entry == NULL)
       
   161 					{
       
   162 					CleanupStack::PopAndDestroy();
       
   163 					User::Leave(KErrNotFound);
       
   164 					}
       
   165 				} //while
       
   166 			LOGTEXT(_L8("MMTsy:\tCATGetBattery:\tFound +CBC string!"));
       
   167 			iCounter = 1;
       
   168 			}
       
   169 		else
       
   170 			{
       
   171 			TLex8 aLex((entry->iResultPtr).Ptr());
       
   172 			if (iCounter == 1)
       
   173 				{	
       
   174 				TInt batteryStatus;
       
   175 				ret = aLex.Val(batteryStatus);
       
   176 				if (ret == KErrNone)
       
   177 					// Have to convert the answer from the AT command format
       
   178 					// to multimode representation.
       
   179 					iBatteryInfo->iStatus = ConvertBatteryStatus(batteryStatus);
       
   180 
       
   181 				}
       
   182 			else if (iCounter == 2)
       
   183 				ret = aLex.Val(iBatteryInfo->iChargeLevel);
       
   184 			iCounter++;
       
   185 			}//else
       
   186 		entry->Deque();
       
   187 		delete entry;
       
   188 		}//while
       
   189 	CleanupStack::PopAndDestroy();
       
   190 	}//ParseBatteryResponseL
       
   191 
       
   192 RMobilePhone::TMobilePhoneBatteryStatus CATGetBattery::ConvertBatteryStatus(TInt aBatteryStatus)
       
   193 
       
   194 /** * Converts the batterey status from ETSI standard to multimode representation *
       
   195  * This method returns the proper multimode representation for the battery status. 
       
   196  * @param aBatteryStatus points to a value representing the battery status the ETSI way(0 - 3, ).
       
   197  * @return  Returns the battery status in multimode representation.
       
   198 */
       
   199 
       
   200 	{
       
   201 	switch (aBatteryStatus)
       
   202 		{		
       
   203 	case 0:
       
   204 		return RMobilePhone::EPoweredByBattery; // equal to 1
       
   205 	case 1:
       
   206 		return RMobilePhone::EBatteryConnectedButExternallyPowered; // equal to 2
       
   207 	case 2:
       
   208 		return RMobilePhone::ENoBatteryConnected; // equal to 3
       
   209 	case 3:
       
   210 		return RMobilePhone::EPowerFault; // equal to 4
       
   211 	default:
       
   212 		return RMobilePhone::EPowerStatusUnknown; //equal to 0
       
   213 		}
       
   214 	}