nfcsettings.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. /*!
  17. * \file
  18. * \brief Declaration of the NfcSettings class and related enumerations.
  19. *
  20. * This file contains the declaration of the NfcSettings class. The
  21. * NFC feature support, NFC mode and NFC error related enumerations
  22. * NfcSettings::NfcFeature, NfcSettings::NfcMode and NfcSettings::NfcError
  23. * are also declared here.
  24. */
  25. #ifndef NFCSETTINGS_H
  26. #define NFCSETTINGS_H
  27. #include <QtCore/QObject>
  28. class NfcSettingsPrivate;
  29. /*!
  30. * \brief A class for querying NFC support and monitoring the current NFC mode.
  31. *
  32. * An instance of this class can be used to query whether a device supports Near
  33. * Field Communication, and to retrieve the current NFC mode setting value.
  34. * Support for NFC requires both the presence of NFC hardware and up to date
  35. * firmware.
  36. *
  37. * On devices that support NFC features, the NFC mode value indicates both
  38. * whether the NFC hardware is currently powered on and the mode it is operating
  39. * in. In addition to retrieving the mode setting on demand, a signal is
  40. * provided for getting a notification whenever the mode setting changes.
  41. *
  42. * The following code snippet shows a simple <code>QObject</code> derived class
  43. * declaration that is used as the basis for the usage examples below:
  44. *
  45. * \code
  46. * #ifndef MYCLASS_H
  47. * #define MYCLASS_H
  48. *
  49. * #include <QtCore/QObject>
  50. *
  51. * #include "nfcsettings.h"
  52. *
  53. * class MyClass : public QObject
  54. * {
  55. * Q_OBJECT
  56. *
  57. * public:
  58. * explicit MyClass(QObject *parent = 0);
  59. * ~MyClass();
  60. *
  61. * private slots:
  62. * void handleNfcModeChange(NfcSettings::NfcMode nfcMode);
  63. * void handleNfcError(NfcSettings::NfcError nfcError, int error);
  64. *
  65. * private:
  66. * NfcSettings *m_nfcSettings;
  67. * };
  68. *
  69. * #endif // MYCLASS_H
  70. * \endcode
  71. *
  72. * The following code snippet shows how to create a new NfcSettings instance,
  73. * query whether NFC is supported by the device, get the current NFC mode
  74. * setting value and how establish the signal-slot connections to receive mode
  75. * change notifications:
  76. *
  77. * \code
  78. * void MyClass::MyClass(QObject *parent) : QObject(parent)
  79. * {
  80. * // Construct a new instance.
  81. * m_nfcSettings = new NfcSettings(this);
  82. *
  83. * // Retrieve the NFC feature support information.
  84. * NfcSettings::NfcFeature nfcFeature = m_nfcSettings->nfcFeature();
  85. *
  86. * if (nfcFeature == NfcSettings::NfcFeatureSupported) {
  87. * // Connect signals for receiving mode change and error notifications.
  88. * connect(m_nfcSettings, SIGNAL(nfcModeChanged(NfcSettings::NfcMode)), SLOT(handleNfcModeChange(NfcSettings::NfcMode)));
  89. * connect(m_nfcSettings, SIGNAL(nfcErrorOccurred(NfcSettings::NfcError, int)), SLOT(handleNfcError(NfcSettings::NfcError, int)));
  90. *
  91. * // Retrieve the initial value of the NFC mode setting.
  92. * NfcSettings::NfcMode nfcMode = m_nfcSettings->nfcMode();
  93. *
  94. * if (nfcMode != NfcSettings::NfcModeOn) {
  95. * // NFC is supported but not turned on, prompt the user to enable it.
  96. * }
  97. * }
  98. * else if (nfcFeature == NfcSettings::NfcFeatureSupportedViaFirmwareUpdate) {
  99. * // Display message to user to update device firmware.
  100. * } else {
  101. * // Display message informing the user that NFC is not supported by this device.
  102. * }
  103. * }
  104. * \endcode
  105. *
  106. * To receive mode change notifications, it is enough to implement a slot to
  107. * handle the nfcModeChanged() signal. However, it is recommended to also
  108. * implement a slot to handle the the nfcErrorOccurred() signal, as some error
  109. * situations may make the NFC mode reading unreliable, see nfcMode() and
  110. * nfcError() for details.
  111. *
  112. * The following code snippet shows the implementation of the
  113. * handleNfcModeChange() slot:
  114. *
  115. * \code
  116. * void MyClass::handleNfcModeChange(NfcSettings::NfcMode nfcMode)
  117. * {
  118. * switch (nfcMode) {
  119. * case NfcSettings::NfcModeNotSupported:
  120. * // NFC is not currently supported. It is not possible to distinguish
  121. * // whether a firmware update could enable NFC features based solely
  122. * // on the value of the nfcMode parameter. The return value of
  123. * // NfcSettings::nfcFeature() indicates whether a firmware update is
  124. * // applicable to this device.
  125. * break;
  126. * case case NfcSettings::NfcModeUnknown:
  127. * // NFC is supported, but the current mode is unknown at this time.
  128. * break;
  129. * case NfcSettings::NfcModeOn:
  130. * // NFC hardware is supported, and currently switched on.
  131. * break;
  132. * case NfcSettings::NfcModeCardOnly:
  133. * // NFC hardware is supported, and currently in card emulation mode.
  134. * break;
  135. * case NfcSettings::NfcModeOff:
  136. * // NFC hardware is supported, and currently switched off.
  137. * break;
  138. * default:
  139. * break;
  140. * }
  141. * }
  142. * \endcode
  143. *
  144. * The nfcModeChanged() signal will not be emitted if there an error occurs while
  145. * obtaining the new mode value, or while attempting to resubscribe to receive
  146. * further notifications. In these situations the nfcErrorOccurred() signal will
  147. * be emitted instead.
  148. *
  149. * An example implementation of a slot for handling the nfcErrorOccurred()
  150. * signal is shown below:
  151. *
  152. * \code
  153. * void Widget::handleNfcError(NfcSettings::NfcError nfcError, int error)
  154. * {
  155. * // The platform specific error code is ignored here.
  156. * Q_UNUSED(error)
  157. *
  158. * switch (nfcError) {
  159. * case NfcSettings::NfcErrorFeatureSupportQuery:
  160. * // Unable to query NFC feature support.
  161. * break;
  162. * case NfcSettings::NfcErrorSoftwareVersionQuery:
  163. * // Unable to query device software version.
  164. * break;
  165. * case NfcSettings::NfcErrorModeChangeNotificationRequest:
  166. * // Unable to request NFC mode change notifications.
  167. * break;
  168. * case NfcSettings::NfcErrorModeChangeNotification:
  169. * // NFC mode change notification was received, but caused an error.
  170. * break;
  171. * case NfcSettings::NfcErrorModeRetrieval:
  172. * // Unable to retrieve current NFC mode.
  173. * break;
  174. * default:
  175. * break;
  176. * }
  177. * }
  178. * \endcode
  179. *
  180. * The second parameter of the signal, <code>error</code>, is a platform
  181. * specific error code that can be used to determine the cause of the failure
  182. * in more detail.
  183. *
  184. * It is possible to try to reset the NfcSettings instance to recover from
  185. * transient error situations by calling the reset() function. However, this
  186. * must not be done directly from the slot connected to the nfcErrorOccurred()
  187. * signal, as calling reset() may cause further errors to be reported resulting
  188. * in endless recursion.
  189. *
  190. * The reset() call should be asynchronous, i.e. by starting a low priority
  191. * timer in the error handler slot instead of a direct call to reset().
  192. * Additionally there should be a limit on how many reset() calls are made. If
  193. * for example three reset() calls made at five second intervals do not clear
  194. * the error situation, the error is most likely non-recoverable.
  195. *
  196. * \note This class comprises the public part of the private implementation
  197. * design pattern, and as such it is the class intended to for use by clients of
  198. * this API. The corresponding platform specific implementation classes (named
  199. * NfcSettingsPrivate) are the classes containing the actual implementation of
  200. * the functionality. There are two versions of NfcSettingsPrivate, one for
  201. * Symbian devices (declared in nfcsettings_symbian_p.h) and a default
  202. * implementation for all other platforms (declared in nfcsettings_p.h). The
  203. * Symbian implementation is fully functional, the default implementation
  204. * reports NFC as not supported.
  205. *
  206. * \see NfcSettingsPrivate
  207. */
  208. class NfcSettings : public QObject
  209. {
  210. Q_OBJECT
  211. Q_ENUMS(NfcFeature)
  212. Q_ENUMS(NfcMode)
  213. Q_ENUMS(NfcError)
  214. /*!
  215. * \brief The current level of NFC feature support provided by the device.
  216. *
  217. * This read-only property provides information on the level of Near Field
  218. * Communication support currently provided by the device. See nfcFeature()
  219. * for a more detailed discussion.
  220. *
  221. * \see NfcFeatute, nfcFeature()
  222. */
  223. Q_PROPERTY(NfcFeature nfcFeature READ nfcFeature)
  224. /*!
  225. * \brief The current NFC mode the device is operating in.
  226. *
  227. * This read-only property provides information on the current active NFC
  228. * mode, i.e. whether the NFC radio hardware is powered on and if so, is it
  229. * in normal operation or card-only mode. See nfcMode() for a more
  230. * detailed discussion.
  231. *
  232. * \see NfcMode, nfcMode(), nfcModeChanged()
  233. */
  234. Q_PROPERTY(NfcMode nfcMode READ nfcMode NOTIFY nfcModeChanged)
  235. /*!
  236. * \brief The error reason for the latest error that has occurred.
  237. *
  238. * This property provides information on the latest NFC setting related
  239. * error that has taken place. See nfcError() for a more detailed discussion.
  240. *
  241. * \see NfcError, nfcError(), nfcErrorOccurred(), reset()
  242. */
  243. Q_PROPERTY(NfcError nfcError READ nfcError RESET reset NOTIFY nfcErrorOccurred)
  244. public:
  245. /*!
  246. * \brief Enumeration listing all NFC feature support values.
  247. *
  248. * The values of this enumeration represent the levels of support for NFC
  249. * that a device can have.
  250. *
  251. * \see nfcFeature()
  252. */
  253. enum NfcFeature
  254. {
  255. NfcFeatureNotSupported, /*!< The device does not support NFC. */
  256. NfcFeatureSupportedViaFirmwareUpdate, /*!< The device has NFC hardware, but requires a software update. */
  257. NfcFeatureSupported /*!< The device has full support for NFC features. */
  258. };
  259. /*!
  260. * \brief Enumeration listing all NFC mode setting values.
  261. *
  262. * The values of this enumeration represent the modes in which the NFC
  263. * hardware is operating. The modes are mutually exclusive, exactly one of
  264. * these modes is in effect at any given time.
  265. *
  266. * Not all modes are applicable on all devices and on all software versions,
  267. * e.g. the card emulation mode NfcSettings::NfcModeCardOnly may not be
  268. * supported by all NFC capable hardware. Similarly, the only mode reported
  269. * by devices that do not have NFC hardware is NfcSettings::NfcModeNotSupported.
  270. *
  271. * The mode value NfcSettings::NfcModeNotSupported is also used for situations
  272. * where NFC hardware is present, but the firmware version installed on the
  273. * device does not support it yet. In this case the function nfcFeature()
  274. * will return the value NfcSettings::NfcFeatureSupportedViaFirmwareUpdate
  275. * and NFC support may be enabled by simply updating the firmware.
  276. *
  277. * \see nfcFeature(), nfcMode(), nfcModeChanged()
  278. */
  279. enum NfcMode
  280. {
  281. NfcModeNotSupported, /*!< The device does not support NFC, hence no mode is applicable. */
  282. NfcModeUnknown, /*!< The device supports NFC, but is unable to report the current mode. */
  283. NfcModeOn, /*!< NFC is supported and the NFC radio is currently enabled. */
  284. NfcModeCardOnly, /*!< NFC is supported and the NFC radio is currently in card emulation mode. */
  285. NfcModeOff /*!< NFC is supported but the NFC radio is currently disabled. */
  286. };
  287. /*!
  288. * \brief Enumeration listing known NFC error states.
  289. *
  290. * The values of this enumeration represent the stages at which errors may
  291. * occur while trying to query the current NFC mode setting value.
  292. *
  293. * Not all errors are applicable on all devices. Devices not supporting
  294. * NFC will not report any of these errors, rather they will return
  295. * NfcModeNotSupported from nfcMode().
  296. *
  297. * \see nfcError(), nfcErrorOccurred()
  298. */
  299. enum NfcError
  300. {
  301. NfcErrorNone, /*!< No errors have occurred. */
  302. NfcErrorFeatureSupportQuery, /*!< Trying to query whether the device has NFC hardware caused an error. */
  303. NfcErrorSoftwareVersionQuery, /*!< Trying to obtain the device software version failed. */
  304. NfcErrorModeChangeNotificationRequest, /*!< Trying to subscribe to get NFC mode change notifications failed. */
  305. NfcErrorModeChangeNotification, /*!< An error occurred while handling the NFC mode change notification. */
  306. NfcErrorModeRetrieval /*!< Trying to query the current NFC mode setting value caused an error. */
  307. };
  308. /*!
  309. * \brief C++ constructor.
  310. *
  311. * Constructs a new NfcSettings instance by determining the availability of
  312. * NFC hardware and retrieving the initial NFC mode value.
  313. *
  314. * Since no connections can be in place when the constructor is called, the
  315. * signals emitted by a new mode value, or by possible error scenarios cannot
  316. * be received by any listener. Instead the initial feature support, mode and
  317. * error values should be obtained by directly calling the corresponding
  318. * getter functions.
  319. *
  320. * The newly constructed NfcSettings instance is fully operational if
  321. * nfcFeature() returns NfcSettings::NfcFeatureSupported and nfcError()
  322. * returns NfcSettings::NfcErrorNone. All other return value combinations
  323. * indicate that NFC is either not supported, or an error took place while
  324. * trying to determine the availability of NFC hardware or the mode value.
  325. *
  326. * \param parent The object that is to be the parent of this NfcSettings
  327. * instance. If there is no parent object (i.e. 0 is passed in as the
  328. * parent), the newly created instance must be deleted by whoever allocated
  329. * it.
  330. * \see nfcFeature(), nfcMode(), nfcError()
  331. */
  332. explicit NfcSettings(QObject *parent = 0);
  333. /*!
  334. * \brief C++ destructor.
  335. *
  336. * Releases any resources allocated by this NfcSettings instance.
  337. */
  338. virtual ~NfcSettings();
  339. /*!
  340. * \brief Returns the current level of NFC feature support provided by the
  341. * device.
  342. *
  343. * This function returns the level of support current provided by the device
  344. * and its software. The value returned by this function cannot change at
  345. * runtime, as the only way to enable NFC support for a device that does not
  346. * currently have it, is via a firmware update.
  347. *
  348. * \return The current level of NFC feature support: NfcSettings::NfcFeatureNotSupported
  349. * if the device does not have NFC hardware, NfcSettings::NfcFeatureSupportedViaFirmwareUpdate
  350. * if NFC hardware is present but requires a software update to be usable, or
  351. * NfcSettings::NfcFeatureSupported is NFC features are fully supported.
  352. * \see NfcFeature
  353. */
  354. NfcFeature nfcFeature() const;
  355. /*!
  356. * \brief Returns the current value of the NFC mode setting.
  357. *
  358. * This function returns the NFC mode setting value that was most recently
  359. * retrieved without any errors. The "without errors" statement means that
  360. * if nfcError() returns anything other than NfcSettings::NfcErrorNone, the
  361. * return value of this function will be NfcSettings::NfcModeUnknown, as the
  362. * the latest NFC mode reading cannot be trusted to correctly reflect the
  363. * current active NFC mode.
  364. *
  365. * \return The current NFC mode setting value, or NfcSettings::NfcModeNotSupported
  366. * if the device does not have NFC hardware.
  367. * \see NfcMode, nfcModeChanged()
  368. */
  369. NfcMode nfcMode() const;
  370. /*!
  371. * \brief Returns the error reason for the latest error that has occurred.
  372. *
  373. * This function returns the reason for the latest NFC error that has taken
  374. * place. Error reason codes are not stored - if more than one error has
  375. * occurred since this function was last called, only the reason for the
  376. * latest error is returned. The nfcErrorOccurred() signal is, however,
  377. * sent for each error separately.
  378. *
  379. * If NfcSettings::NfcErrorFeatureSupportQuery is returned, this instance is
  380. * completely inoperative, nfcMode() will not return meaningful values and
  381. * no mode change signals will be fired. An NfcSettings object in this state
  382. * should either be discarded, or reinitialized by calling reset().
  383. *
  384. * If NfcSettings::NfcErrorModeChangeNotificationRequest is returned no
  385. * further mode change notifications will be delivered until reset() is
  386. * called to try and resubscribe to the change notification.
  387. *
  388. * If everything is working normally this function returns
  389. * NfcSettings::NfcErrorNone. Any other status means that the reading of the
  390. * NFC mode value is unreliable. In these cases nfcMode() will return
  391. * NfcSettings::NfcModeUnknown.
  392. *
  393. * Note that the device having no NFC hardware is not deemed an error. In
  394. * this case nfcError() returns NfcSettings::NfcErrorNone and nfcMode()
  395. * returns NfcSettings::NfcModeNotSupported.
  396. *
  397. * \return The latest NFC error reason.
  398. * \see NfcError, nfcErrorOccurred(), reset()
  399. */
  400. NfcError nfcError() const;
  401. public slots:
  402. /*!
  403. * \brief Determines the availability of NFC hardware and retrieves the
  404. * initial NFC mode value.
  405. *
  406. * If the device supports NFC and no errors occur during the initialization,
  407. * the initial value of the mode setting is retrieved.
  408. *
  409. * It is valid to call reset() more than once. This will try to clear any
  410. * existing error condition and attempt to reinitialize the NfcSettings
  411. * instance to a pristine state. Calling reset() again can be used as a way
  412. * to recover from intermittent runtime errors that may occur for example
  413. * when there is a temporary low memory situation.
  414. *
  415. * \warning reset() must not be called from a slot connected to the
  416. * nfcErrorOccurred() signal. This is because a non-clearable error condition
  417. * will cause the slot to be called again, resulting in endless recursion.
  418. *
  419. * \see NfcSettings, nfcError(), nfcErrorOccurred()
  420. */
  421. void reset();
  422. signals:
  423. /*!
  424. * \brief Signals a change in the NFC mode setting.
  425. *
  426. * This signal is sent whenever a change in the NFC mode setting is detected.
  427. *
  428. * \note The nfcModeChanged() signal will never send the enumerated value
  429. * NfcSettings::NfcModeNotSupported as the value of the \a nfcMode parameter.
  430. * As the level of NFC support cannot change at runtime, no nfcModeChanged()
  431. * signals are emitted if the initial mode value is NfcSettings::NfcModeNotSupported.
  432. * Regardless of this, it is a good practice to implement the corresponding
  433. * slot to handle the NfcSettings::NfcModeNotSupported value as well, in
  434. * case the slot is also used as a normal function in the application.
  435. *
  436. * \param nfcMode The current value of the NFC mode setting.
  437. * \see NfcMode, nfcMode()
  438. */
  439. void nfcModeChanged(NfcSettings::NfcMode nfcMode);
  440. /*!
  441. * \brief Signals an error in some the NFC feature or mode related operation.
  442. *
  443. * This signal is sent whenever an error occurs while trying to query support
  444. * for NFC hardware, retrieve the current value of the NFC mode setting, or
  445. * subscribing to get NFC mode setting change notifications.
  446. *
  447. * Error states are permanent in that they don't get cleared until reset()
  448. * is explicitly called. A call to reset() may return the NfcSettings
  449. * instance to normal operation, but there are error states from which it
  450. * is not possible to recover.
  451. *
  452. * \param nfcError The general reason for the error. The value of this
  453. * parameter indicates the stage at which the error took place. E.g. the
  454. * value NfcSettings::NfcErrorFeatureSupportQuery means that an error occured
  455. * while trying to query whether the device has NFC hardware,
  456. * NfcSettings::NfcErrorModeRetrieval means NFC is supported, but there was
  457. * a problem retrieving the current value of the mode setting.
  458. * \param error A platform specific error code giving a more detailed
  459. * description of the error that took place. For example
  460. * <code>KErrNoMemory</code> (-4) on Symbian devices if there is not enough
  461. * memory to perform the requested operation.
  462. * \see NfcError, nfcError(), reset()
  463. */
  464. void nfcErrorOccurred(NfcSettings::NfcError nfcError, int error);
  465. private:
  466. Q_DISABLE_COPY(NfcSettings)
  467. /*! \brief Private implementation class. */
  468. friend class NfcSettingsPrivate;
  469. /*! \brief Pointer to the private class instance, owned. */
  470. NfcSettingsPrivate *d_ptr;
  471. };
  472. #endif // NFCSETTINGS_H