nfcsettings_symbian_p.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2011 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: Part of Qt NFC Setting sample application.
  15. */
  16. #include <centralrepository.h>
  17. #include <featdiscovery.h>
  18. #include <hal.h>
  19. #include "nfcsettings_symbian_p.h"
  20. // The following Central Repository UID, setting key and setting values are not
  21. // available in the Nokia Symbian^3 SDK v1.0. Hence there is no header we could
  22. // include, and we have to define the relevant values here.
  23. const TUid KNfcCentralRepositoryUid = { 0x20021390 };
  24. const TUint32 KNfcModeSetting = 0x01;
  25. const TInt KNfcModeUnknown = -9999;
  26. const TInt KNfcModeOn = 0;
  27. const TInt KNfcModeCardOnly = 2;
  28. const TInt KNfcModeOff = 1;
  29. #define KFeatureIdNfc 117
  30. // Device ID constant used to determine whether the current device is a
  31. // Nokia C7-00 (RM-675), the only model for which NFC support is available
  32. // as a firmware update.
  33. const TInt KModelRm675 = 0x2002bf92;
  34. NfcSettingsPrivate::NfcSettingsPrivate( NfcSettings *q ) :
  35. CActive( CActive::EPriorityLow ),
  36. q_ptr( q ),
  37. iNfcFeature( NfcSettings::NfcFeatureNotSupported ),
  38. iNfcMode( NfcSettings::NfcModeNotSupported ),
  39. iNfcError( NfcSettings::NfcErrorNone )
  40. {
  41. CActiveScheduler::Add( this );
  42. InitializeNfcFeatureSupport();
  43. }
  44. NfcSettingsPrivate::~NfcSettingsPrivate()
  45. {
  46. Cancel();
  47. delete iRepository;
  48. }
  49. NfcSettings::NfcFeature NfcSettingsPrivate::nfcFeature() const
  50. {
  51. return iNfcFeature;
  52. }
  53. NfcSettings::NfcMode NfcSettingsPrivate::nfcMode() const
  54. {
  55. return iNfcMode;
  56. }
  57. NfcSettings::NfcError NfcSettingsPrivate::nfcError() const
  58. {
  59. return iNfcError;
  60. }
  61. void NfcSettingsPrivate::reset()
  62. {
  63. iNfcFeature = NfcSettings::NfcFeatureNotSupported;
  64. iNfcMode = NfcSettings::NfcModeNotSupported;
  65. iNfcError = NfcSettings::NfcErrorNone;
  66. InitializeNfcFeatureSupport();
  67. }
  68. void NfcSettingsPrivate::DoCancel()
  69. {
  70. if ( iRepository )
  71. {
  72. iRepository->NotifyCancelAll();
  73. }
  74. }
  75. void NfcSettingsPrivate::RunL()
  76. {
  77. const TInt status( iStatus.Int() );
  78. User::LeaveIfError( status );
  79. // A successfully completed CRepository::NotifyRequest() will set the key
  80. // of the modified repository value as the TRequestStatus value. As the
  81. // repository key is a TUint32, it is explicitly converted to a TInt
  82. // to compare it to the TInt status value. This supresses a GCCE compilation
  83. // warning. The value of KNfcModeSetting is 0x01, so it can be safely
  84. // converted to a TInt without any loss of information.
  85. if ( status == TInt(KNfcModeSetting) )
  86. {
  87. NotifyRequest();
  88. }
  89. }
  90. TInt NfcSettingsPrivate::RunError( TInt aError )
  91. {
  92. ReportError( NfcSettings::NfcErrorModeChangeNotification, aError );
  93. return KErrNone;
  94. }
  95. void NfcSettingsPrivate::InitializeNfcFeatureSupport()
  96. {
  97. TBool nfcAvailable = EFalse;
  98. TRAPD(error, nfcAvailable = CFeatureDiscovery::IsFeatureSupportedL(KFeatureIdNfc));
  99. if ( nfcAvailable )
  100. {
  101. iNfcFeature = NfcSettings::NfcFeatureSupported;
  102. if ( !iRepository )
  103. {
  104. TRAP( error, iRepository = CRepository::NewL(KNfcCentralRepositoryUid) );
  105. }
  106. if ( error == KErrNone )
  107. {
  108. Cancel();
  109. NotifyRequest();
  110. }
  111. else
  112. {
  113. ReportError( NfcSettings::NfcErrorModeRetrieval, error );
  114. }
  115. }
  116. else if ( ProductSupportsNfcViaFirmwareUpdate() )
  117. {
  118. iNfcFeature = NfcSettings::NfcFeatureSupportedViaFirmwareUpdate;
  119. }
  120. }
  121. void NfcSettingsPrivate::NotifyRequest()
  122. {
  123. if ( !IsActive() && iRepository )
  124. {
  125. const TInt error( iRepository->NotifyRequest(KNfcModeSetting, iStatus) );
  126. if ( (error == KErrNone) && (iStatus == KRequestPending) )
  127. {
  128. SetActive();
  129. RetrieveSetting();
  130. }
  131. else if ( error != KErrNone )
  132. {
  133. ReportError( NfcSettings::NfcErrorModeChangeNotificationRequest, error );
  134. }
  135. }
  136. }
  137. void NfcSettingsPrivate::RetrieveSetting()
  138. {
  139. if ( iRepository )
  140. {
  141. TInt nfcMode( KNfcModeUnknown );
  142. const TInt error( iRepository->Get(KNfcModeSetting, nfcMode) );
  143. if ( error == KErrNone )
  144. {
  145. switch ( nfcMode )
  146. {
  147. case KNfcModeUnknown:
  148. iNfcMode = NfcSettings::NfcModeUnknown;
  149. break;
  150. case KNfcModeOn:
  151. iNfcMode = NfcSettings::NfcModeOn;
  152. break;
  153. case KNfcModeCardOnly:
  154. iNfcMode = NfcSettings::NfcModeCardOnly;
  155. break;
  156. case KNfcModeOff:
  157. iNfcMode = NfcSettings::NfcModeOff;
  158. break;
  159. default:
  160. iNfcMode = NfcSettings::NfcModeUnknown;
  161. break;
  162. }
  163. if ( q_ptr )
  164. {
  165. emit q_ptr->nfcModeChanged( iNfcMode );
  166. }
  167. }
  168. else
  169. {
  170. ReportError( NfcSettings::NfcErrorModeRetrieval, error );
  171. }
  172. }
  173. }
  174. void NfcSettingsPrivate::ReportError( NfcSettings::NfcError aNfcError, TInt aError )
  175. {
  176. iNfcError = aNfcError;
  177. // If NFC is supported, but the mode setting value cannot be retrieved,
  178. // reset the mode to NfcSettings::NfcModeUnknown. This prevents a situation
  179. // where nfcFeature() reports NFC as supported but the return value
  180. // of nfcMode() reports it as unsupported.
  181. if ( iNfcFeature == NfcSettings::NfcFeatureSupported )
  182. {
  183. iNfcMode = NfcSettings::NfcModeUnknown;
  184. }
  185. if ( q_ptr )
  186. {
  187. emit q_ptr->nfcErrorOccurred( iNfcError, aError );
  188. }
  189. }
  190. TBool NfcSettingsPrivate::ProductSupportsNfcViaFirmwareUpdate()
  191. {
  192. TBool supportsNfcViaFirmwareUpdate( EFalse );
  193. TInt model( 0 );
  194. const TInt error( HAL::Get(HALData::EModel, model) );
  195. if ( error == KErrNone )
  196. {
  197. if ( model == KModelRm675 )
  198. {
  199. supportsNfcViaFirmwareUpdate = ETrue;
  200. }
  201. }
  202. else
  203. {
  204. ReportError( NfcSettings::NfcErrorSoftwareVersionQuery, error );
  205. }
  206. return supportsNfcViaFirmwareUpdate;
  207. }