Mistake on this page?
Report an issue in GitHub or email us
NFCController.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2018 ARM Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MBED_NFC_CONTROLLER_H
18 #define MBED_NFC_CONTROLLER_H
19 
20 #include <stdint.h>
21 #include "events/EventQueue.h"
22 #include "platform/SharedPtr.h"
23 #include "drivers/Timer.h"
24 #include "drivers/Timeout.h"
25 
26 #include "NFCDefinitions.h"
27 #include "NFCControllerDriver.h"
28 
29 #include "platform/Span.h"
30 
31 namespace mbed {
32 namespace nfc {
33 
34 /** @addtogroup nfc
35  * @{
36  */
37 
38 class NFCRemoteInitiator;
39 class NFCRemoteTarget;
40 class NFCControllerDriver;
41 
42 /**
43  * This class represents a NFC Controller.
44  *
45  * A controller can be in one of three different states:
46  * * Idle/sleep state
47  * * Discovery state: The controller tries to discover a remote endpoint (initiator or target)
48  * * Connected state: The controller exchanges data with an endpoint (initiator or target)
49  *
50  * A NFCController instance needs to be initialized with a NFCControllerDriver instance which abstracts the specific controller being used.
51  * A delegate needs to be set by the user to receive discovery events.
52  */
54 public:
55 
56  /**
57  * The NFCController delegate. Users of the NFCController class need to implement this delegate's methods to receive events.
58  */
59  struct Delegate {
60  /**
61  * A enumeration of causes for the discovery process terminating.
62  */
64  nfc_discovery_terminated_completed = 0, ///< Process completed, at least one endpoint was discovered
65  nfc_discovery_terminated_canceled, ///< Process was canceled by the user
66  nfc_discovery_terminated_rf_error ///< An unexpected error was encountered during an exchange on the air interface
67  };
68 
69  /**
70  * The discovery process terminated.
71  * @param[in] reason the cause for the termination
72  */
74 
75  /**
76  * A remote initiator was discovered (the local controller is in target mode).
77  * @param[in] nfc_initiator the NFCRemoteInitiator instance
78  */
79  virtual void on_nfc_initiator_discovered(const SharedPtr<NFCRemoteInitiator> &nfc_initiator) {}
80 
81  /**
82  * A remote target was discovered (the local controller is in initiator mode).
83  * @param[in] nfc_target the NFCRemoteTarget instance
84  */
85  virtual void on_nfc_target_discovered(const SharedPtr<NFCRemoteTarget> &nfc_target) {}
86 
87  protected:
88  ~Delegate() { }
89  };
90 
91  /**
92  * Construct a NFCController instance.
93  *
94  * @param[in] driver a pointer to a NFCControllerDriver instance
95  * @param[in] queue a pointer to the events queue to use
96  * @param[in] ndef_buffer a bytes array used to store NDEF messages
97  */
98  NFCController(NFCControllerDriver *driver, events::EventQueue *queue, const Span<uint8_t> &ndef_buffer);
99 
100  /**
101  * Initialize the NFC controller
102  *
103  * This method must be called before any other method call.
104  *
105  * @return NFC_OK, or an error.
106  */
107  nfc_err_t initialize();
108 
109  /**
110  * Set the delegate that will receive events generated by this controller.
111  *
112  * @param[in] delegate the delegate instance to use
113  */
114  void set_delegate(Delegate *delegate);
115 
116  /**
117  * Get the list of RF protocols supported by this controller.
118  *
119  * @return a bitmask of RF protocols supported by the controller
120  */
122 
123  /**
124  * Set the list of RF protocols to look for during discovery.
125  *
126  * @param[in] rf_protocols the relevant bitmask
127  * @return NFC_OK on success, or
128  * NFC_ERR_UNSUPPORTED if a protocol is not supported by the controller,
129  * NFC_ERR_BUSY if the discovery process is already running
130  */
131  nfc_err_t configure_rf_protocols(nfc_rf_protocols_bitmask_t rf_protocols);
132 
133  /**
134  * Start the discovery process using the protocols configured previously.
135  *
136  * If remote endpoints are connected when this is called, they will be disconnected.
137  *
138  * @return NFC_OK on success, or
139  * NFC_ERR_BUSY if the discovery process is already running
140  */
141  nfc_err_t start_discovery();
142 
143  /**
144  * Cancel/stop a running discovery process.
145  *
146  * @return NFC_OK
147  */
148  nfc_err_t cancel_discovery();
149 
150 private:
151  // These two classes need to be friends to access the following transceiver() method
152  friend class NFCRemoteEndpoint;
153  friend class Type4RemoteInitiator;
154  nfc_transceiver_t *transceiver() const;
155 
156  void polling_callback(nfc_err_t ret);
157 
158  // NFC Stack scheduler
159  void scheduler_process(bool hw_interrupt);
160 
161  // Callbacks from NFC stack
162  static void s_polling_callback(nfc_transceiver_t *pTransceiver, nfc_err_t ret, void *pUserData);
163 
164  // Implementation of NFCControllerDriver::Delegate
165  virtual void on_hw_interrupt();
166 
167  // Triggers when scheduler must be run again
168  void on_timeout();
169 
170  NFCControllerDriver *_driver;
171  events::EventQueue *_queue;
172  nfc_transceiver_t *_transceiver;
173  nfc_scheduler_t *_scheduler;
174  Timer _timer;
175  Timeout _timeout;
176  Delegate *_delegate;
177  bool _discovery_running;
178  Span<uint8_t> _ndef_buffer;
179 };
180 /** @}*/
181 } // namespace nfc
182 } // namespace mbed
183 
184 #endif
nfc_err_t configure_rf_protocols(nfc_rf_protocols_bitmask_t rf_protocols)
Set the list of RF protocols to look for during discovery.
The abstraction for a NFC controller driver.
virtual void on_nfc_target_discovered(const SharedPtr< NFCRemoteTarget > &nfc_target)
A remote target was discovered (the local controller is in initiator mode).
Definition: NFCController.h:85
EventQueue.
Definition: EventQueue.h:52
Shared pointer class.
Definition: SharedPtr.h:68
virtual void on_discovery_terminated(nfc_discovery_terminated_reason_t reason)
The discovery process terminated.
Definition: NFCController.h:73
nfc_err_t initialize()
Initialize the NFC controller.
virtual void on_nfc_initiator_discovered(const SharedPtr< NFCRemoteInitiator > &nfc_initiator)
A remote initiator was discovered (the local controller is in target mode).
Definition: NFCController.h:79
This class represents a NFC Controller.
Definition: NFCController.h:53
The NFCController delegate.
Definition: NFCController.h:59
Process completed, at least one endpoint was discovered.
Definition: NFCController.h:64
The NFCControllerDriver delegate.
nfc_discovery_terminated_reason_t
A enumeration of causes for the discovery process terminating.
Definition: NFCController.h:63
An unexpected error was encountered during an exchange on the air interface.
Definition: NFCController.h:66
nfc_err_t cancel_discovery()
Cancel/stop a running discovery process.
nfc_err_t start_discovery()
Start the discovery process using the protocols configured previously.
This class is an implementation of the Type 4 tag application.
This is the base class for all remote endpoints (initiators and targets) addressable over the air int...
nfc_rf_protocols_bitmask_t get_supported_rf_protocols() const
Get the list of RF protocols supported by this controller.
void set_delegate(Delegate *delegate)
Set the delegate that will receive events generated by this controller.
A Timeout is used to call a function at a point in the future.
Definition: Timeout.h:58
NFCController(NFCControllerDriver *driver, events::EventQueue *queue, const Span< uint8_t > &ndef_buffer)
Construct a NFCController instance.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.