Mbed OS Reference
Loading...
Searching...
No Matches
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
32namespace mbed {
33namespace nfc {
34
35/** @defgroup nfc NFC
36 * @ingroup connectivity-public-api
37 * @{
38 */
39
40class NFCRemoteInitiator;
41class NFCRemoteTarget;
42class NFCControllerDriver;
43
44/**
45 * This class represents a NFC Controller.
46 *
47 * A controller can be in one of three different states:
48 * * Idle/sleep state
49 * * Discovery state: The controller tries to discover a remote endpoint (initiator or target)
50 * * Connected state: The controller exchanges data with an endpoint (initiator or target)
51 *
52 * A NFCController instance needs to be initialized with a NFCControllerDriver instance which abstracts the specific controller being used.
53 * A delegate needs to be set by the user to receive discovery events.
54 */
56public:
57
58 /**
59 * The NFCController delegate. Users of the NFCController class need to implement this delegate's methods to receive events.
60 */
61 struct Delegate {
62 /**
63 * A enumeration of causes for the discovery process terminating.
64 */
66 nfc_discovery_terminated_completed = 0, ///< Process completed, at least one endpoint was discovered
67 nfc_discovery_terminated_canceled, ///< Process was canceled by the user
68 nfc_discovery_terminated_rf_error ///< An unexpected error was encountered during an exchange on the air interface
69 };
70
71 /**
72 * The discovery process terminated.
73 * @param[in] reason the cause for the termination
74 */
76
77 /**
78 * A remote initiator was discovered (the local controller is in target mode).
79 * @param[in] nfc_initiator the NFCRemoteInitiator instance
80 */
81 virtual void on_nfc_initiator_discovered(const SharedPtr<NFCRemoteInitiator> &nfc_initiator) {}
82
83 /**
84 * A remote target was discovered (the local controller is in initiator mode).
85 * @param[in] nfc_target the NFCRemoteTarget instance
86 */
87 virtual void on_nfc_target_discovered(const SharedPtr<NFCRemoteTarget> &nfc_target) {}
88
89 protected:
90 ~Delegate() { }
91 };
92
93 /**
94 * Construct a NFCController instance.
95 *
96 * @param[in] driver a pointer to a NFCControllerDriver instance
97 * @param[in] queue a pointer to the events queue to use
98 * @param[in] ndef_buffer a bytes array used to store NDEF messages
99 */
101
102 /**
103 * Initialize the NFC controller
104 *
105 * This method must be called before any other method call.
106 *
107 * @return NFC_OK, or an error.
108 */
110
111 /**
112 * Set the delegate that will receive events generated by this controller.
113 *
114 * @param[in] delegate the delegate instance to use
115 */
116 void set_delegate(Delegate *delegate);
117
118 /**
119 * Get the list of RF protocols supported by this controller.
120 *
121 * @return a bitmask of RF protocols supported by the controller
122 */
124
125 /**
126 * Set the list of RF protocols to look for during discovery.
127 *
128 * @param[in] rf_protocols the relevant bitmask
129 * @return NFC_OK on success, or
130 * NFC_ERR_UNSUPPORTED if a protocol is not supported by the controller,
131 * NFC_ERR_BUSY if the discovery process is already running
132 */
134
135 /**
136 * Start the discovery process using the protocols configured previously.
137 *
138 * If remote endpoints are connected when this is called, they will be disconnected.
139 *
140 * @return NFC_OK on success, or
141 * NFC_ERR_BUSY if the discovery process is already running
142 */
144
145 /**
146 * Cancel/stop a running discovery process.
147 *
148 * @return NFC_OK
149 */
151
152private:
153 // These two classes need to be friends to access the following transceiver() method
154 friend class NFCRemoteEndpoint;
155 friend class Type4RemoteInitiator;
156 nfc_transceiver_t *transceiver() const;
157
158 void polling_callback(nfc_err_t ret);
159
160 // NFC Stack scheduler
161 void scheduler_process(bool hw_interrupt);
162
163 // Callbacks from NFC stack
164 static void s_polling_callback(nfc_transceiver_t *pTransceiver, nfc_err_t ret, void *pUserData);
165
166 // Implementation of NFCControllerDriver::Delegate
167 virtual void on_hw_interrupt();
168
169 // Triggers when scheduler must be run again
170 void on_timeout();
171
172 NFCControllerDriver *_driver;
173 events::EventQueue *_queue;
174 nfc_transceiver_t *_transceiver;
175 nfc_scheduler_t *_scheduler;
176 Timer _timer;
177 Timeout _timeout;
178 Delegate *_delegate;
179 bool _discovery_running;
180 Span<uint8_t> _ndef_buffer;
181};
182/** @}*/
183} // namespace nfc
184} // namespace mbed
185
186#endif
EventQueue.
Definition: EventQueue.h:62
Shared pointer class.
Definition: SharedPtr.h:68
The abstraction for a NFC controller driver.
This class represents a NFC Controller.
Definition: NFCController.h:55
nfc_err_t start_discovery()
Start the discovery process using the protocols configured previously.
nfc_err_t cancel_discovery()
Cancel/stop a running discovery process.
NFCController(NFCControllerDriver *driver, events::EventQueue *queue, const Span< uint8_t > &ndef_buffer)
Construct a NFCController instance.
nfc_rf_protocols_bitmask_t get_supported_rf_protocols() const
Get the list of RF protocols supported by this controller.
nfc_err_t configure_rf_protocols(nfc_rf_protocols_bitmask_t rf_protocols)
Set the list of RF protocols to look for during discovery.
nfc_err_t initialize()
Initialize the NFC controller.
void set_delegate(Delegate *delegate)
Set the delegate that will receive events generated by this controller.
This is the base class for all remote endpoints (initiators and targets) addressable over the air int...
This class is an implementation of the Type 4 tag application.
int nfc_err_t
Type for NFC errors.
Definition: nfc_errors.h:60
Nonowning view to a sequence of contiguous elements.
Definition: Span.h:215
The NFCController delegate.
Definition: NFCController.h:61
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:81
nfc_discovery_terminated_reason_t
A enumeration of causes for the discovery process terminating.
Definition: NFCController.h:65
@ nfc_discovery_terminated_canceled
Process was canceled by the user.
Definition: NFCController.h:67
@ nfc_discovery_terminated_completed
Process completed, at least one endpoint was discovered.
Definition: NFCController.h:66
@ nfc_discovery_terminated_rf_error
An unexpected error was encountered during an exchange on the air interface.
Definition: NFCController.h:68
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:87
virtual void on_discovery_terminated(nfc_discovery_terminated_reason_t reason)
The discovery process terminated.
Definition: NFCController.h:75
The NFCControllerDriver delegate.