sysstatemgmt/systemstateplugins/cmncustomcmd/src/cmdpersistreboots.cpp
changeset 70 653a8b91b95e
parent 64 61992147389a
child 72 d25e1504e8e9
child 73 d38941471f1c
equal deleted inserted replaced
64:61992147389a 70:653a8b91b95e
     1 // Copyright (c) 2008-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  @released
       
    20 */
       
    21 
       
    22 #include <bautils.h>
       
    23 
       
    24 #include "cmdpersistreboots.h"
       
    25 #include "ssmdebug.h"
       
    26 #include "ssmpanic.h"
       
    27 
       
    28 _LIT(KBootUpFolder, ":\\private\\2000d75b\\bootupinfo\\");
       
    29 _LIT(KBootUpFile, "bootupcount.bin");
       
    30 
       
    31 /**
       
    32  * Constructs and returns a new custom command, leaving on errors.
       
    33  * 
       
    34  * @internalComponent
       
    35  */
       
    36 CCustomCmdPersistReboots* CCustomCmdPersistReboots::NewL()
       
    37 	{
       
    38 	CCustomCmdPersistReboots* self = new (ELeave) CCustomCmdPersistReboots();
       
    39 	CleanupStack::PushL(self);
       
    40 	self->ConstructL();
       
    41 	CleanupStack::Pop(self);
       
    42 	return self;
       
    43 	}
       
    44 
       
    45 void CCustomCmdPersistReboots::ConstructL()
       
    46 	{
       
    47 	const TChar drive = RFs::GetSystemDriveChar();
       
    48 	TInt length = KBootUpFolder().Length() + KBootUpFile().Length() + 1 /* for RFs::GetSystemDriveChar() */;
       
    49 	iBootupInfoPath.CreateL(length);
       
    50 	iBootupInfoPath.Append(drive);
       
    51 	iBootupInfoPath.Append(KBootUpFolder());
       
    52 	iBootupInfoPath.Append(KBootUpFile());
       
    53 	
       
    54 	}
       
    55 
       
    56 void CCustomCmdPersistReboots::CreateLogIfNotExistL()
       
    57 	{
       
    58 	__ASSERT_DEBUG(NULL != iFs.Handle(),PanicNow(KPanicSsmCustCmdRfs, EInvalidRFs));
       
    59 	const TBool found = BaflUtils::FileExists(iFs, iBootupInfoPath);
       
    60 
       
    61 	if(!found)
       
    62 		{
       
    63 		TInt err = iFs.MkDirAll(iBootupInfoPath);
       
    64 		if(KErrAlreadyExists != err && KErrNone != err)
       
    65 			User::Leave(err);
       
    66 		// create log file to store the number for bootup attempts
       
    67 		RFile file;
       
    68 		User::LeaveIfError(file.Create(iFs, iBootupInfoPath, EFileShareExclusive | EFileWrite | EFileRead));
       
    69 		file.Close();
       
    70 		LogBootupCountL(0);
       
    71 		}	
       
    72 	}
       
    73 
       
    74 CCustomCmdPersistReboots::CCustomCmdPersistReboots()
       
    75 	{
       
    76 	}
       
    77 
       
    78 CCustomCmdPersistReboots::~CCustomCmdPersistReboots()
       
    79 	{
       
    80 	iBootupInfoPath.Close();
       
    81 	}
       
    82 
       
    83 /**
       
    84  * Initializes this custom command.
       
    85  * 
       
    86  * @internalComponent
       
    87  */
       
    88 TInt CCustomCmdPersistReboots::Initialize(CSsmCustomCommandEnv* aCmdEnv)
       
    89 	{
       
    90 	iFs = aCmdEnv->Rfs();
       
    91 	TRAPD(ret, CreateLogIfNotExistL());
       
    92 	return ret;
       
    93 	}
       
    94 
       
    95 /**
       
    96  * Destory this object and any resources allocated to it.
       
    97  * 
       
    98  * @internalComponent
       
    99  */
       
   100 void CCustomCmdPersistReboots::Close()
       
   101 	{
       
   102 	}
       
   103 
       
   104 void CCustomCmdPersistReboots::Release()
       
   105 	{
       
   106 	delete this;
       
   107 	}
       
   108 
       
   109 /**
       
   110  * Issues the required rendezvous.
       
   111  * 
       
   112  * @internalComponent
       
   113  */
       
   114 void CCustomCmdPersistReboots::Execute(const TDesC8& aParams, TRequestStatus& aStatus)
       
   115 	{
       
   116 	aStatus = KRequestPending;
       
   117 	TCustCmdPersistRebootExecuteOption commandOption = EUndefined;
       
   118 
       
   119 	TInt err = KErrNone;
       
   120 	TRAP(err, commandOption = ExtractExecuteOptionL(aParams));
       
   121 
       
   122 	if(KErrNone != err)
       
   123 		{
       
   124 		TRequestStatus* statusPtr = &aStatus;
       
   125 		User::RequestComplete(statusPtr, err);
       
   126 		return;
       
   127 		}
       
   128 
       
   129 	switch (commandOption)
       
   130 		{
       
   131 		case EIncrementBootCount:
       
   132 			{
       
   133 			TRAP(err, IncrementBootCountL());
       
   134 			break;
       
   135 			}
       
   136 		case EResetBootCount:
       
   137 			{
       
   138 			TRAP(err, ResetBootCountL());
       
   139 			break;
       
   140 			}
       
   141 		default:
       
   142 			{
       
   143 			err = KErrArgument;
       
   144 			break;	
       
   145 			}
       
   146 		}
       
   147 
       
   148 	TRequestStatus* statusPtr = &aStatus;
       
   149 	User::RequestComplete(statusPtr, err);
       
   150 	}
       
   151 
       
   152 /**
       
   153  * Cancels the requested execute
       
   154  * 
       
   155  * @internalComponent
       
   156  */
       
   157 void CCustomCmdPersistReboots::ExecuteCancel()
       
   158 	{
       
   159 	// Nothing to do as request already completed in Execute()
       
   160 	}
       
   161 
       
   162 TUint8 CCustomCmdPersistReboots::BootCountFromLogL()
       
   163 	{
       
   164 	__ASSERT_DEBUG(NULL != iFs.Handle(),PanicNow(KPanicSsmCustCmdRfs, EInvalidRFs));
       
   165 	RFileReadStream file;
       
   166 	CleanupClosePushL(file);
       
   167 	User::LeaveIfError(file.Open(iFs, iBootupInfoPath, EFileRead));
       
   168 	TUint8 bootCount = file.ReadUint8L();
       
   169 	CleanupStack::PopAndDestroy(&file);
       
   170 	return bootCount;
       
   171 	}
       
   172 
       
   173 void CCustomCmdPersistReboots::IncrementBootCountL()
       
   174 	{
       
   175 	TUint8 bootCount = BootCountFromLogL();
       
   176 	LogBootupCountL(++bootCount);
       
   177 	DEBUGPRINT2A(" Tried booting the device (%d) time(s)", bootCount);
       
   178 	}
       
   179 
       
   180 void CCustomCmdPersistReboots::ResetBootCountL()
       
   181 	{
       
   182 	LogBootupCountL(0);	//always reset to 0
       
   183 	DEBUGPRINT1A(" Boot count reset to (0)");
       
   184 	}
       
   185 
       
   186 TCustCmdPersistRebootExecuteOption CCustomCmdPersistReboots::ExtractExecuteOptionL(const TDesC8& aParams)
       
   187 	{
       
   188 	RDesReadStream readStream(aParams);
       
   189 	CleanupClosePushL(readStream);
       
   190 	TCustCmdPersistRebootExecuteOption executeOption = (TCustCmdPersistRebootExecuteOption)readStream.ReadInt8L();
       
   191 	CleanupStack::PopAndDestroy(&readStream);
       
   192 	return executeOption;
       
   193 	}
       
   194 
       
   195 void CCustomCmdPersistReboots::LogBootupCountL(TUint8 aCount)
       
   196 	{
       
   197 	__ASSERT_DEBUG(NULL != iFs.Handle(),PanicNow(KPanicSsmCustCmdRfs, EInvalidRFs));
       
   198 	RFileWriteStream file;
       
   199 	CleanupClosePushL(file);
       
   200 	User::LeaveIfError(file.Open(iFs, iBootupInfoPath, EFileWrite));
       
   201 	file.WriteUint8L(aCount);
       
   202 	file.CommitL();
       
   203 	CleanupStack::PopAndDestroy(&file);
       
   204 	}