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