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