fotaapplication/fotaserver/src/fotaupdate.cpp
branchRCL_3
changeset 26 19bba8228ff0
parent 25 b183ec05bd8c
child 27 5cc2995847ea
equal deleted inserted replaced
25:b183ec05bd8c 26:19bba8228ff0
     1 /*
       
     2  * Copyright (c) 2005-2006 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:   starts update sequence
       
    15  *
       
    16  */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include <StringLoader.h>
       
    20 #include <centralrepository.h>
       
    21 #include <AknUtils.h>
       
    22 #include <AknBidiTextUtils.h> 
       
    23 #include <biditext.h>
       
    24 #include <gdi.h>
       
    25 #include <fotaengine.h>
       
    26 #include <apgtask.h>
       
    27 #include <aknradiobuttonsettingpage.h> 
       
    28 #include <akntitle.h>
       
    29 #include <schtime.h>
       
    30 #include <csch_cli.h>
       
    31 #include <fotaengine.h>
       
    32 #include <XQConversions>
       
    33 #include "fotaupdate.h"
       
    34 #include "fmsclient.h"
       
    35 #include "FotasrvSession.h"
       
    36 #include "fotaserverPrivateCRKeys.h"
       
    37 #include "fotaserverPrivatePSKeys.h"
       
    38 #include "fotanotifiers.h"
       
    39 
       
    40 #include <usbman.h>
       
    41 #include <usbstates.h>
       
    42 // ============================= MEMBER FUNCTIONS ============================
       
    43 
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // CFotaUpdate::CFotaUpdate()
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CFotaUpdate::CFotaUpdate() :
       
    50     CActive(EPriorityNormal)
       
    51     {
       
    52     CActiveScheduler::Add(this);
       
    53     }
       
    54 
       
    55 // ---------------------------------------------------------------------------
       
    56 // CFotaUpdate::~CFotaUpdate()
       
    57 // ---------------------------------------------------------------------------
       
    58 //
       
    59 CFotaUpdate::~CFotaUpdate()
       
    60     {
       
    61     FLOG(_L("CFotaUpdate::~CFotaUpdate >>"));
       
    62     iFotaServer = NULL;
       
    63 
       
    64     CancelMonitor();
       
    65 
       
    66     if (iProperty.Handle())
       
    67         iProperty.Close();
       
    68 
       
    69     FLOG(_L("CFotaUpdate::~CFotaUpdate <<"));
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // CFotaUpdate::NewL 
       
    74 // ---------------------------------------------------------------------------
       
    75 //
       
    76 CFotaUpdate* CFotaUpdate::NewL(CFotaServer* aServer)
       
    77     {
       
    78     CFotaUpdate* ao = new (ELeave) CFotaUpdate();
       
    79     ao->iFotaServer = aServer;
       
    80     return ao;
       
    81     }
       
    82 
       
    83 // ---------------------------------------------------------------------------
       
    84 // CFotaUpdate::CheckUpdateResults
       
    85 // Checks if there is update result file available (meaning that update just
       
    86 // took place)
       
    87 // ---------------------------------------------------------------------------
       
    88 //
       
    89 TBool CFotaUpdate::CheckUpdateResults(RFs& aRfs)
       
    90     {
       
    91     RFile f;
       
    92     TInt err;
       
    93     err = f.Open(aRfs, KUpdateResultFile, EFileShareAny);
       
    94     f.Close();
       
    95     if (err != KErrNone)
       
    96         {
       
    97         return EFalse;
       
    98         }
       
    99     FLOG(_L(" CFotaUpdate::CheckUpdateResults  update result file Found! "));
       
   100     return ETrue;
       
   101     }
       
   102 
       
   103 // ---------------------------------------------------------------------------
       
   104 // CFotaUpdate::ExecuteUpdateResultFileL
       
   105 // Read result code from update result file and update state accordingly. 
       
   106 // Show notifier "Update succesful". Do some cleanup.
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 void CFotaUpdate::ExecuteUpdateResultFileL()
       
   110     {
       
   111     FLOG(_L("CFotaUpdate::ExecuteUpdateResultFileL >>"));
       
   112 
       
   113     TInt err;
       
   114     RFileReadStream rstr;
       
   115     TInt result;
       
   116     TInt msglen;
       
   117     HBufC8* message = NULL;
       
   118     HBufC16* message16 = NULL;
       
   119     TBool deleteData(EFalse);
       
   120 
       
   121     // Open update result file
       
   122     FLOG(_L("opening rstr 1/2  "));
       
   123     err = rstr.Open(iFotaServer->iFs, KUpdateResultFile, EFileRead
       
   124             | EFileStream);
       
   125     FLOG(_L("opened  rstr 2/2  "));
       
   126     if (err)
       
   127     	{
       
   128         FLOG(_L("   update result file open err %d"), err);
       
   129       }
       
   130     User::LeaveIfError(err);
       
   131     FLOG(_L("       0.1"));
       
   132     CleanupClosePushL(rstr);
       
   133     FLOG(_L("       0.2"));
       
   134 
       
   135     // Read resultcode 
       
   136     result = rstr.ReadUint32L();
       
   137     msglen = rstr.ReadUint32L();
       
   138     if (msglen != 0)
       
   139         {
       
   140         message = HBufC8::NewLC(msglen + 1);
       
   141         TPtr8 ptrdesc = message->Des();
       
   142         TRAPD ( err2, rstr.ReadL(ptrdesc) );
       
   143         if (err2 != KErrNone && err2 != KErrEof)
       
   144             {
       
   145             FLOG(_L("  file read err %d"), err2);
       
   146             User::Leave(err2);
       
   147             }
       
   148 
       
   149         message16 = HBufC16::NewLC(message->Des().Length());
       
   150         message16->Des().Copy(*message);
       
   151         FLOG(_L("   1 update result: %d"), result);
       
   152         FLOG(_L("   2 dbg msg: %S"), message16);
       
   153         CleanupStack::PopAndDestroy(message16);
       
   154         CleanupStack::PopAndDestroy(message);
       
   155         }
       
   156 
       
   157     FLOG(_L("       0.6 "));
       
   158     CleanupStack::PopAndDestroy(&rstr);
       
   159 
       
   160     // Map resultcode to FUMO result code
       
   161     RFotaEngineSession::TState fstate = RFotaEngineSession::EUpdateFailed;
       
   162     RFotaEngineSession::TResult fresult =
       
   163             RFotaEngineSession::EResUpdateFailed;
       
   164 
       
   165     FLOG(_L("   3"));
       
   166     switch (result)
       
   167         {
       
   168         case UPD_OK:
       
   169             {
       
   170             fstate = RFotaEngineSession::EUpdateSuccessfulNoData;
       
   171             fresult = RFotaEngineSession::EResSuccessful;
       
   172             deleteData = ETrue;
       
   173             ShowDialogL(EFwUpdSuccess);
       
   174             }
       
   175             break;
       
   176         case UPD_INSTALL_REQUEST_IS_INVALID:
       
   177             {
       
   178             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   179             fresult = RFotaEngineSession::EResUpdateFailed;
       
   180             deleteData = ETrue;
       
   181             ShowDialogL(EFwUpdNotCompatible);
       
   182             }
       
   183             break;
       
   184         case UPD_UPDATE_PACKAGE_IS_ABSENT:
       
   185             {
       
   186             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   187             fresult = RFotaEngineSession::EResUpdateFailed;
       
   188             ShowDialogL(EFwUpdNotCompatible);
       
   189             }
       
   190             break;
       
   191         case UPD_UPDATE_PACKAGE_IS_CORRUPTED:
       
   192             {
       
   193             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   194             fresult = RFotaEngineSession::EResCorruptedFWUPD;
       
   195             deleteData = ETrue;
       
   196             ShowDialogL(EFwUpdNotCompatible);
       
   197             }
       
   198             break;
       
   199         case UPD_UPDATE_PACKAGE_CONTENTS_IS_INVALID:
       
   200             {
       
   201             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   202             fresult = RFotaEngineSession::EResCorruptedFWUPD;
       
   203             deleteData = ETrue;
       
   204             ShowDialogL(EFwUpdNotCompatible);
       
   205             }
       
   206             break;
       
   207         case UPD_UPDATE_PACKAGE_IS_NOT_COMPATIBLE_WITH_CURRENT_MOBILE_DEVICE:
       
   208             {
       
   209             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   210             fresult = RFotaEngineSession::EResPackageMismatch;
       
   211             deleteData = ETrue;
       
   212             ShowDialogL(EFwUpdNotCompatible);
       
   213             }
       
   214             break;
       
   215         case UPD_FATAL_ERROR:
       
   216             {
       
   217             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   218             fresult = RFotaEngineSession::EResUpdateFailed;
       
   219             deleteData = ETrue;
       
   220             ShowDialogL(EFwUpdNotCompatible);
       
   221             }
       
   222             break;
       
   223 
       
   224         default:
       
   225             {
       
   226             FLOG(_L("   3.1 invalid result: %d"), result);
       
   227             fstate = RFotaEngineSession::EUpdateFailedNoData;
       
   228             fresult = RFotaEngineSession::EResUpdateFailed;
       
   229             deleteData = ETrue;
       
   230             ShowDialogL(EFwUpdNotCompatible);
       
   231             }
       
   232             break;
       
   233         }
       
   234 
       
   235     // Find the state 60 (update  progressing) -> 100 (etc)
       
   236     RArray<TInt> states;
       
   237     TPackageState state;
       
   238     CleanupClosePushL(states);
       
   239     iFotaServer->iDatabase->OpenDBL();
       
   240     iFotaServer->iDatabase->GetAllL(states);
       
   241 
       
   242     FLOG(_L("   4.1 found %d states "), states.Count());
       
   243     for (TInt i = 0; i < states.Count(); ++i)
       
   244         {
       
   245         TPackageState tmp;
       
   246         TBuf8<KMaxFileName> tmpurl;
       
   247         tmp = iFotaServer->iDatabase->GetStateL(states[i], tmpurl);
       
   248         FLOG(_L("   5 got state "));
       
   249         if (tmp.iState == RFotaEngineSession::EUpdateProgressing)
       
   250             {
       
   251             state = tmp;
       
   252             state.iState = fstate;
       
   253             state.iResult = fresult;
       
   254             FLOG(_L("   6 Updating state id %d  to %d ,result %d  "),
       
   255                     state.iPkgId, state.iState, state.iResult);
       
   256             iFotaServer->iDatabase->SetStateL(state, KNullDesC8, EFDBState
       
   257                     | EFDBResult);
       
   258             }
       
   259         }
       
   260     iFotaServer->iDatabase->CloseAndCommitDB();
       
   261     CleanupStack::PopAndDestroy(&states);
       
   262 
       
   263     // Delete request file
       
   264     err = BaflUtils::DeleteFile(iFotaServer->iFs, KUpdateRequestFile);
       
   265     if (err != KErrNone && err != KErrNotFound)
       
   266         {
       
   267         FLOG(_L("   6.1  req file deleted, err %d"), err);
       
   268         User::Leave(err);
       
   269         }
       
   270 
       
   271     // Write timestamp (shown to user in device management ui)
       
   272     if (fresult == RFotaEngineSession::EResSuccessful)
       
   273         {
       
   274         RFileWriteStream wstr;
       
   275         TTime time;
       
   276         User::LeaveIfError(wstr.Replace(iFotaServer->iFs,
       
   277                 KUpdateTimeStampFileName, EFileWrite));
       
   278         CleanupClosePushL(wstr);
       
   279         time.HomeTime();
       
   280         TInt year = time.DateTime().Year();
       
   281         TInt month = time.DateTime().Month();
       
   282         TInt day = time.DateTime().Day();
       
   283         TInt hour = time.DateTime().Hour();
       
   284         TInt minute = time.DateTime().Minute();
       
   285         wstr.WriteInt32L(year);
       
   286         wstr.WriteInt32L(month);
       
   287         wstr.WriteInt32L(day);
       
   288         wstr.WriteInt32L(hour);
       
   289         wstr.WriteInt32L(minute);
       
   290         CleanupStack::PopAndDestroy(1); // wstr
       
   291         }
       
   292     // Delete package content
       
   293     if (deleteData && state.iPkgId > KErrNotFound)
       
   294         {
       
   295         iFotaServer->DeletePackageL(state.iPkgId);
       
   296         DeleteUpdateResultFileL();
       
   297         }
       
   298 
       
   299     // Report state back to syncml server
       
   300     if (state.iPkgId >= 0)
       
   301         {
       
   302         FLOG(_L("   6.1 creating device mgmt session for profile %d"),
       
   303                 state.iProfileId);
       
   304         iFotaServer->ReportFwUpdateStatusL(state);
       
   305         }
       
   306     else
       
   307         {
       
   308         FLOG(_L(" No state found in 'update progress' mode! cannot report status to DM server "));
       
   309         }
       
   310 
       
   311     FLOG(_L("CFotaUpdate::ExecuteUpdateResultFileL <<"));
       
   312     }
       
   313 
       
   314 // ---------------------------------------------------------------------------
       
   315 // CFotaUpdate::DeleteUpdateResultsL
       
   316 // ---------------------------------------------------------------------------
       
   317 //
       
   318 void CFotaUpdate::DeleteUpdateResultFileL()
       
   319     {
       
   320     FLOG(_L("CFotaUpdate::DeleteUpdateResultsL >>"));
       
   321     // Delete result file
       
   322     RFs fs;
       
   323     __LEAVE_IF_ERROR(fs.Connect());
       
   324     BaflUtils::DeleteFile(fs, KUpdateResultFile);
       
   325     BaflUtils::DeleteFile(fs, KUpdateRequestFile);
       
   326     BaflUtils::DeleteFile(fs, KUpdateBitmap);
       
   327     BaflUtils::DeleteFile(fs, KRestartingBitmap);
       
   328     fs.Close();
       
   329     FLOG(_L("CFotaUpdate::DeleteUpdateResultsL <<"));
       
   330     }
       
   331 
       
   332 // ---------------------------------------------------------------------------
       
   333 // CFotaUpdate::UpdateL
       
   334 // Updates the fw: Creates input files for update agent and boots device to 
       
   335 // update mode.
       
   336 // ---------------------------------------------------------------------------
       
   337 void CFotaUpdate::UpdateL()
       
   338     {
       
   339     FLOG(_L("CFotaUpdate::UpdateL() >>"));
       
   340     // Set state ........................................
       
   341     iFotaServer->iPackageState.iState
       
   342             = RFotaEngineSession::EUpdateProgressing;
       
   343     iFotaServer->iDatabase->OpenDBL();
       
   344     iFotaServer->iDatabase->SetStateL(iFotaServer->iPackageState, KNullDesC8,
       
   345             EFDBState);
       
   346     iFotaServer->iDatabase->CloseAndCommitDB();
       
   347 
       
   348     // Write update request for update agent..............
       
   349     FLOG(_L("CFotaUpdate::UpdateL  1 writing update.req "));
       
   350     TBuf16<KMaxFileName> dp2filepath;
       
   351     HBufC16* dp2;
       
   352     RFileWriteStream wstr;
       
   353     CleanupClosePushL(wstr);
       
   354     FLOG(_L("CFotaUpdate::UpdateL  2 getting pkg location"));
       
   355     iFotaServer->DownloaderL()->GetUpdatePackageLocation(dp2filepath);
       
   356 
       
   357     FLOG(_L("CFotaUpdate::UpdateL  3 craeting update.req"));
       
   358     User::LeaveIfError(wstr.Replace(iFotaServer->iFs, KUpdateRequestFile,
       
   359             EFileWrite));
       
   360     wstr.WriteInt16L(1); // version number is  1
       
   361     wstr.WriteInt32L(1); // count   of cmds is 1
       
   362     wstr.WriteInt16L(0); // requestid is 0
       
   363 
       
   364     dp2 = HBufC16::NewLC(dp2filepath.Length());
       
   365     dp2->Des().Copy(dp2filepath);
       
   366     wstr.WriteInt32L(dp2->Des().Length() + 1); // length of filename + null
       
   367     wstr.WriteL(dp2->Des());
       
   368     wstr.WriteInt16L(0); // null character
       
   369     CleanupStack::PopAndDestroy(dp2);
       
   370     CleanupStack::PopAndDestroy(&wstr); // wstr
       
   371 
       
   372     FLOG(_L("CFotaUpdate::UpdateL  4 craeting update.bmp"));
       
   373 
       
   374 
       
   375     // Write update graphic for update agent ...............
       
   376     HBufC* updatetxt;
       
   377     HBufC* restarttxt;
       
   378     QString installstr = hbTrId("txt_device_update_info_installing");
       
   379     QString rebootstr = hbTrId("txt_device_update_info_rebooting");
       
   380     updatetxt = XQConversions::qStringToS60Desc(installstr);
       
   381     restarttxt = XQConversions::qStringToS60Desc(rebootstr);
       
   382     
       
   383     WriteUpdateBitmapL( updatetxt->Des(), KUpdateBitmap );
       
   384     WriteUpdateBitmapL( restarttxt->Des(), KRestartingBitmap );
       
   385     delete updatetxt;
       
   386     delete restarttxt;
       
   387     
       
   388     // Simulate update agent by writing result file.
       
   389     CRepository* centrep(NULL);
       
   390     TInt err = KErrNone;
       
   391     TRAP(err, centrep = CRepository::NewL( KCRUidFotaServer ) );
       
   392     TInt simulate(KErrNotFound);
       
   393     if (centrep)
       
   394         {
       
   395         err = centrep->Get(KSimulateUpdateAgent, simulate);
       
   396         }
       
   397     delete centrep;
       
   398     if (simulate > 0)
       
   399         {
       
   400         FLOG(_L("CFotaUpdate::UpdateL  5  - writing update.resp"));
       
   401         RFileWriteStream respstr;
       
   402         CleanupClosePushL(respstr);
       
   403         User::LeaveIfError(respstr.Replace(iFotaServer->iFs,
       
   404                 KUpdateResultFile, EFileWrite));
       
   405 
       
   406         respstr.WriteUint32L(UPD_OK);
       
   407         respstr.WriteUint32L(15);
       
   408         respstr.WriteL(_L("UPDATE DONE!"));
       
   409         CleanupStack::PopAndDestroy(&respstr);
       
   410         }
       
   411 
       
   412     iFotaServer->SetStartupReason(EFotaDefault);
       
   413     
       
   414     RStarterSession starter;
       
   415     FLOG(_L("           starter->Connect"));
       
   416     User::LeaveIfError(starter.Connect());
       
   417     starter.Reset(RStarterSession::EFirmwareUpdate);
       
   418     starter.Close();
       
   419     FLOG(_L("CFotaUpdate::UpdateL() <<"));
       
   420     }
       
   421 
       
   422 // ---------------------------------------------------------------------------
       
   423 // CFotaUpdate::CheckBatteryL()
       
   424 // Cheks if there's enough battery power to update
       
   425 // ---------------------------------------------------------------------------
       
   426 //
       
   427 TBool CFotaUpdate::CheckBatteryL()
       
   428     {
       
   429     FLOG(_L("CFotaUpdate::CheckBatteryL >>"));
       
   430 
       
   431     TInt chargingstatus(EChargingStatusError);
       
   432     TInt batterylevel(EBatteryLevelUnknown);
       
   433     TBool enoughPower(EFalse);
       
   434 
       
   435     // Read battery level
       
   436 
       
   437     RProperty pw;
       
   438     User::LeaveIfError(pw.Attach(KPSUidHWRMPowerState, KHWRMBatteryLevel));
       
   439     User::LeaveIfError(pw.Get(batterylevel));
       
   440 
       
   441     if (batterylevel >= BatteryLevelAccepted)
       
   442         {
       
   443         FLOG(_L("Battery charge is above acceptable level!"));
       
   444         enoughPower = ETrue;
       
   445         }
       
   446     else
       
   447         {
       
   448 
       
   449         //Read charger status
       
   450         User::LeaveIfError(pw.Attach(KPSUidHWRMPowerState,
       
   451                 KHWRMChargingStatus));
       
   452         User::LeaveIfError(pw.Get(chargingstatus));
       
   453         pw.Close();
       
   454 
       
   455         // But charger is connected, power sufficient
       
   456         if (chargingstatus == EChargingStatusCharging || chargingstatus
       
   457                 == EChargingStatusAlmostComplete || chargingstatus
       
   458                 == EChargingStatusChargingComplete || chargingstatus
       
   459                 == EChargingStatusChargingContinued)
       
   460             {
       
   461             FLOG(
       
   462                     _L("Battery charge is below acceptable level, but charger is connected!"));
       
   463             enoughPower = ETrue;
       
   464             }
       
   465         }
       
   466 
       
   467     FLOG(_L("CFotaUpdate::CheckBattery, current charge = %d, ret = %d <<"),
       
   468             batterylevel, enoughPower);
       
   469     return enoughPower;
       
   470     }
       
   471 
       
   472 void CFotaUpdate::MonitorBatteryChargeLevel()
       
   473     {
       
   474     FLOG(_L("CFotaUpdate::MonitorBatteryChargeLevel >>"));
       
   475 
       
   476     CancelMonitor();
       
   477 
       
   478     iProperty.Attach(KPSUidHWRMPowerState, KHWRMBatteryLevel);
       
   479     TInt value = 0;
       
   480 
       
   481     iProperty.Get(KPSUidHWRMPowerState, KHWRMBatteryLevel, value);
       
   482 
       
   483     FLOG(_L("Battery level at this time is %d, err = %d"), value);
       
   484     iStatus = KRequestPending;
       
   485     iProperty.Subscribe(iStatus);
       
   486 
       
   487     iMonitorType = BatteryLevel;
       
   488     SetActive();
       
   489 
       
   490     FLOG(_L("CFotaUpdate::MonitorBatteryChargeLevel <<"));
       
   491     }
       
   492 
       
   493 void CFotaUpdate::MonitorBatteryChargingStatus()
       
   494     {
       
   495     FLOG(_L("CFotaUpdate::MonitorBatteryChargingStatus >>"));
       
   496 
       
   497     CancelMonitor();
       
   498 
       
   499     iProperty.Attach(KPSUidHWRMPowerState, KHWRMChargingStatus);
       
   500     TInt value = 0;
       
   501 
       
   502     iProperty.Get(KPSUidHWRMPowerState, KHWRMChargingStatus, value);
       
   503 
       
   504     FLOG(_L("Charging status at this time is %d, err = %d"), value);
       
   505     iStatus = KRequestPending;
       
   506     iProperty.Subscribe(iStatus);
       
   507 
       
   508     iMonitorType = ChargingStatus;
       
   509     SetActive();
       
   510 
       
   511     FLOG(_L("CFotaUpdate::MonitorBatteryChargingStatus <<"));
       
   512     }
       
   513 
       
   514 void CFotaUpdate::CancelMonitor()
       
   515     {
       
   516     FLOG(_L("CFotaUpdate::CancelMonitor >>"));
       
   517     if (IsActive())
       
   518         {
       
   519         FLOG(_L("Cancelling...."));
       
   520         Cancel();
       
   521         }
       
   522     FLOG(_L("CFotaUpdate::CancelMonitor <<"));
       
   523     }
       
   524 
       
   525 TBool CFotaUpdate::IsUSBConnected()
       
   526     {
       
   527     FLOG(_L("CFotaUpdate::IsUSBConnected >>"));
       
   528     
       
   529     TBool ret (EFalse);
       
   530     TUsbDeviceState state(EUsbDeviceStateUndefined);
       
   531     RUsb usbman;
       
   532     TInt err = usbman.Connect();
       
   533     if (err == KErrNone)
       
   534         {
       
   535         usbman.GetDeviceState(state);
       
   536         usbman.Close();
       
   537         }
       
   538     if (state != EUsbDeviceStateUndefined)
       
   539     	{
       
   540         ret = ETrue;
       
   541       }
       
   542     
       
   543     FLOG(_L("CFotaUpdate::IsUSBConnected, value = %d, ret = %d <<"), state, ret);
       
   544     return ret;
       
   545     }
       
   546 
       
   547 void CFotaUpdate::RunL()
       
   548     {
       
   549     FLOG(_L("CFotaUpdate::RunL >>"));
       
   550 
       
   551     if (iStatus.Int() == KErrNone)
       
   552         {
       
   553         TInt value = 0;
       
   554 
       
   555         if (iMonitorType == BatteryLevel)
       
   556             {
       
   557             iProperty.Get(KPSUidHWRMPowerState, KHWRMBatteryLevel,
       
   558                     value);
       
   559 
       
   560             FLOG(_L("Battery level has changed to %d"), value);
       
   561 
       
   562             iFotaServer->UpdateBatteryLowInfo(
       
   563                     (value >= BatteryLevelAccepted) ? EFalse : ETrue);
       
   564 
       
   565             MonitorBatteryChargeLevel();
       
   566             }
       
   567         else if (iMonitorType == ChargingStatus)
       
   568             {
       
   569             iProperty.Get(KPSUidHWRMPowerState, KHWRMChargingStatus,
       
   570                     value);
       
   571 
       
   572             FLOG(_L("Charging status has changed to %d"), value);
       
   573 
       
   574             if (value == EChargingStatusCharging || value
       
   575                     == EChargingStatusAlmostComplete || value
       
   576                     == EChargingStatusChargingComplete || value
       
   577                     == EChargingStatusChargingContinued)
       
   578                 {
       
   579                 if (IsUSBConnected())
       
   580                     {
       
   581                     ShowDialogL(EFwUpdNotEnoughBattery);
       
   582                     }
       
   583                 else
       
   584                     {
       
   585                     iFotaServer->UpdateBatteryLowInfo(EFalse);
       
   586                     }
       
   587                 }
       
   588             else
       
   589                 {
       
   590                 iFotaServer->UpdateBatteryLowInfo(ETrue);
       
   591                 }
       
   592             }
       
   593 
       
   594         }
       
   595     FLOG(_L("CFotaUpdate::RunL, iStatus = %d <<"), iStatus.Int());
       
   596     }
       
   597 
       
   598 // ---------------------------------------------------------------------------
       
   599 // CFotaUpdate::StartUpdateL
       
   600 // Starts fw updating (shows a notifier to user). 
       
   601 // ---------------------------------------------------------------------------
       
   602 //
       
   603 void CFotaUpdate::StartUpdateL(const TDownloadIPCParams &aParams)
       
   604     {
       
   605     FLOG(_L("CFotaUpdate::StartUpdateL,  pkig:%d >>"), aParams.iPkgId);
       
   606 
       
   607     //First cancel any ongoing battery monitoring.
       
   608     CancelMonitor();
       
   609 
       
   610     UpdateL();
       
   611 
       
   612     FLOG(_L("CFotaUpdate::StartUpdateL <<"));
       
   613     }
       
   614 
       
   615 TInt CFotaUpdate::RunError(TInt aError)
       
   616     {
       
   617     FLOG(_L("CFotaUpdate::RunError, error = %d >>"), aError);
       
   618     return aError;
       
   619     }
       
   620 
       
   621 void CFotaUpdate::DoCancel()
       
   622     {
       
   623     FLOG(_L("CFotaUpdate::DoCancel >>"));
       
   624 
       
   625     iProperty.Cancel();
       
   626 
       
   627     FLOG(_L("CFotaUpdate::DoCancel <<"));
       
   628     }
       
   629 
       
   630 void CFotaUpdate::ShowDialogL(TFwUpdNoteTypes aDialogid)
       
   631     {
       
   632     FLOG(_L("CFotaUpdate::ShowDialogL, dialogid = %d >>"), aDialogid);
       
   633     iFotaServer->ServerCanShut(EFalse);
       
   634 
       
   635     if (iFotaServer->FullScreenDialog())
       
   636         iFotaServer->FullScreenDialog()->Close();
       
   637 
       
   638     iNotifParams = CHbSymbianVariantMap::NewL();
       
   639 
       
   640     HBufC* keyDialog = HBufC::NewL(10);
       
   641     CleanupStack::PushL(keyDialog);
       
   642     *keyDialog = KKeyDialog;
       
   643 
       
   644     CHbSymbianVariant* dialogId = CHbSymbianVariant::NewL(&aDialogid,
       
   645             CHbSymbianVariant::EInt);
       
   646     iNotifParams->Add(*keyDialog, dialogId);
       
   647     iNotifier = CFotaDownloadNotifHandler::NewL(this);
       
   648 
       
   649     iNotifier->LaunchNotifierL(iNotifParams, aDialogid);
       
   650 
       
   651     CleanupStack::PopAndDestroy();
       
   652     FLOG(_L("CFotaUpdate::ShowDialogL <<"));
       
   653     }
       
   654 
       
   655 void CFotaUpdate::HandleDialogResponse(int response, TInt aDialogId)
       
   656     {
       
   657     FLOG(_L("CFotaUpdate::HandleDialogResponse, dialogid = %d response = %d >>"), aDialogId, response);
       
   658 
       
   659     if (aDialogId == EFwUpdNotEnoughBattery)
       
   660         {
       
   661         iFotaServer->FinalizeUpdate();
       
   662         }
       
   663 
       
   664     FLOG(_L("CFotaUpdate::HandleDialogResponse <<"));
       
   665     }
       
   666 
       
   667 TInt CFotaUpdate::WriteUpdateBitmapL( const TDesC& aText, const TDesC& aFile)
       
   668     {
       
   669     FLOG(_L("WriteUpdateBitmapL writing %S to %S w/ txtdir"),&aText,&aFile);
       
   670 
       
   671     TSize   screensize = CCoeEnv::Static()->ScreenDevice()->SizeInPixels();
       
   672     TInt                width  = screensize.iWidth - KBmpMargin*2;
       
   673     TInt                height =  screensize.iHeight;
       
   674 
       
   675     CArrayFixSeg<TPtrC>*   lines = new CArrayFixSeg<TPtrC>(5);
       
   676     CleanupStack::PushL(lines);
       
   677     CFbsBitmap*         bitmap = new ( ELeave ) CFbsBitmap;
       
   678     CleanupStack::PushL( bitmap );
       
   679     bitmap->Create(  TSize(width,height), EColor64K );
       
   680     CFbsBitmapDevice*   device = CFbsBitmapDevice::NewL( bitmap );
       
   681     CleanupStack::PushL( device );
       
   682     const CFont* font = AknLayoutUtils::FontFromId(EAknLogicalFontPrimaryFont);
       
   683     CFbsBitGc*          context;
       
   684     User::LeaveIfError( device->CreateContext( context ) );
       
   685     CleanupStack::PushL( context );
       
   686     TInt                ascent = font->AscentInPixels();
       
   687     TInt                descent = font->DescentInPixels();
       
   688     context->UseFont ( font );
       
   689     context->Clear(); // bg color
       
   690 
       
   691     // Visually ordered text
       
   692     HBufC* wrappedstring = AknBidiTextUtils::ConvertToVisualAndWrapToArrayL(
       
   693             aText, width,*font, *lines);
       
   694     CleanupStack::PushL ( wrappedstring );
       
   695     TBool dirfound (ETrue);
       
   696     // direction of text, affects alignemnt
       
   697     TBidiText::TDirectionality direction = TBidiText::TextDirectionality(
       
   698             *wrappedstring, &dirfound );
       
   699 
       
   700     // Print visual text to bitmap
       
   701     for ( TInt i=0; i<lines->Count(); ++i ) 
       
   702         {
       
   703         TPtrC l = (*lines)[i];
       
   704         TInt top = (ascent+descent)*(i);
       
   705         TInt bot = (ascent+descent)*(i+1);
       
   706         TRect rect (0, top ,width, bot );
       
   707         CGraphicsContext::TTextAlign alignment = 
       
   708         direction==TBidiText::ELeftToRight ? CGraphicsContext::ELeft 
       
   709             : CGraphicsContext::ERight;
       
   710         context->DrawText(l, rect, ascent, alignment);
       
   711         }
       
   712     height = (ascent+descent)*lines->Count() + descent;
       
   713     bitmap->Resize( TSize(width,height));
       
   714     bitmap->Save( aFile );
       
   715     context->DiscardFont();
       
   716     CleanupStack::PopAndDestroy( wrappedstring );
       
   717     CleanupStack::PopAndDestroy( context );
       
   718     CleanupStack::PopAndDestroy( device );
       
   719     CleanupStack::PopAndDestroy( bitmap );
       
   720     CleanupStack::PopAndDestroy( lines );
       
   721     return 1;
       
   722     }