Demonstration of possible usage of the the NFCController class.

NFC SmartPoster example

Demonstration of possible usage of the the NFCController class.

The application creates a smart poster record and sends it when a connected peer requests it. The smart poster record generated contains:

  • A URI: https://www.mbed.com
  • A title: "mbed website"
  • An action: EXECUTE which asks the peer to open the URI.

Running the application

Verification of the sample application can be seen on any a smartphone with an NFC reader. After running you will be able to read the tag with an NFC tag reader application.

Information about activity is also printed over the serial connection - please have a client open. You may use:

Tera Term - https://ttssh2.osdn.jp/index.html.en

This example is known to work on boards connected to a PN512 shield.

Committer:
mbed_official
Date:
Wed Feb 27 13:03:20 2019 +0000
Revision:
5:48c9ea2e0991
Parent:
1:6c82b777db0b
Updating mbed-os to mbed-os-5.11.5

.
Commit copied from https://github.com/ARMmbed/mbed-os-example-nfc

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:9a16c3f036b0 1 /* mbed Microcontroller Library
mbed_official 0:9a16c3f036b0 2 * Copyright (c) 2018-2018 ARM Limited
mbed_official 0:9a16c3f036b0 3 *
mbed_official 0:9a16c3f036b0 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 0:9a16c3f036b0 5 * you may not use this file except in compliance with the License.
mbed_official 0:9a16c3f036b0 6 * You may obtain a copy of the License at
mbed_official 0:9a16c3f036b0 7 *
mbed_official 0:9a16c3f036b0 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:9a16c3f036b0 9 *
mbed_official 0:9a16c3f036b0 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 0:9a16c3f036b0 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 0:9a16c3f036b0 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 0:9a16c3f036b0 13 * See the License for the specific language governing permissions and
mbed_official 0:9a16c3f036b0 14 * limitations under the License.
mbed_official 0:9a16c3f036b0 15 */
mbed_official 0:9a16c3f036b0 16
mbed_official 0:9a16c3f036b0 17 #include <stdint.h>
mbed_official 0:9a16c3f036b0 18
mbed_official 0:9a16c3f036b0 19 #include "events/EventQueue.h"
mbed_official 0:9a16c3f036b0 20
mbed_official 0:9a16c3f036b0 21 #include "nfc/controllers/PN512Driver.h"
mbed_official 0:9a16c3f036b0 22 #include "nfc/controllers/PN512SPITransportDriver.h"
mbed_official 0:9a16c3f036b0 23 #include "nfc/NFCRemoteInitiator.h"
mbed_official 0:9a16c3f036b0 24 #include "nfc/NFCController.h"
mbed_official 0:9a16c3f036b0 25
mbed_official 0:9a16c3f036b0 26 #include "nfc/ndef/MessageBuilder.h"
mbed_official 0:9a16c3f036b0 27 #include "nfc/ndef/common/util.h"
mbed_official 0:9a16c3f036b0 28
mbed_official 0:9a16c3f036b0 29 #include "SmartPoster.h"
mbed_official 0:9a16c3f036b0 30
mbed_official 0:9a16c3f036b0 31 using events::EventQueue;
mbed_official 0:9a16c3f036b0 32
mbed_official 0:9a16c3f036b0 33 using mbed::Span;
mbed_official 0:9a16c3f036b0 34 using mbed::nfc::NFCRemoteInitiator;
mbed_official 0:9a16c3f036b0 35 using mbed::nfc::NFCController;
mbed_official 0:9a16c3f036b0 36 using mbed::nfc::nfc_rf_protocols_bitmask_t;
mbed_official 0:9a16c3f036b0 37 using mbed::nfc::ndef::MessageBuilder;
mbed_official 0:9a16c3f036b0 38 using mbed::nfc::ndef::common::Text;
mbed_official 0:9a16c3f036b0 39 using mbed::nfc::ndef::common::URI;
mbed_official 0:9a16c3f036b0 40 using mbed::nfc::ndef::common::span_from_cstr;
mbed_official 0:9a16c3f036b0 41
mbed_official 0:9a16c3f036b0 42 /**
mbed_official 0:9a16c3f036b0 43 * Manage the NFC discovery process and the local device operating in target mode.
mbed_official 0:9a16c3f036b0 44 *
mbed_official 0:9a16c3f036b0 45 * When a remote initiator has been discovered, it connects to it then reply
mbed_official 0:9a16c3f036b0 46 * to its ndef message request with a smart poster message that contains:
mbed_official 0:9a16c3f036b0 47 * - A URI: https://www.mbed.com
mbed_official 0:9a16c3f036b0 48 * - A title: mbed website
mbed_official 0:9a16c3f036b0 49 * - An action: EXECUTE which opens the browser of the peer with the URI
mbed_official 0:9a16c3f036b0 50 * transmitted.
mbed_official 0:9a16c3f036b0 51 */
mbed_official 0:9a16c3f036b0 52 class NFCProcess : NFCRemoteInitiator::Delegate, NFCController::Delegate {
mbed_official 0:9a16c3f036b0 53 public:
mbed_official 0:9a16c3f036b0 54 /**
mbed_official 0:9a16c3f036b0 55 * Construct a new NFCProcess objects.
mbed_official 0:9a16c3f036b0 56 *
mbed_official 0:9a16c3f036b0 57 * This function construct the NFC controller and wires it with the PN512
mbed_official 0:9a16c3f036b0 58 * driver.
mbed_official 0:9a16c3f036b0 59 *
mbed_official 0:9a16c3f036b0 60 * @param queue The event queue that will be used by the NFCController.
mbed_official 0:9a16c3f036b0 61 */
mbed_official 0:9a16c3f036b0 62 NFCProcess(events::EventQueue &queue) :
mbed_official 0:9a16c3f036b0 63 _pn512_transport(D11, D12, D13, D10, A1, A0),
mbed_official 0:9a16c3f036b0 64 _pn512_driver(&_pn512_transport),
mbed_official 0:9a16c3f036b0 65 _queue(queue),
mbed_official 0:9a16c3f036b0 66 _ndef_buffer(),
mbed_official 0:9a16c3f036b0 67 _nfc_controller(&_pn512_driver, &queue, _ndef_buffer)
mbed_official 0:9a16c3f036b0 68 { }
mbed_official 0:9a16c3f036b0 69
mbed_official 0:9a16c3f036b0 70 /**
mbed_official 0:9a16c3f036b0 71 * Initialise and configure the NFC controller.
mbed_official 0:9a16c3f036b0 72 *
mbed_official 0:9a16c3f036b0 73 * @return NFC_OK in case of success or a meaningful error code in case of
mbed_official 0:9a16c3f036b0 74 * failure.
mbed_official 0:9a16c3f036b0 75 */
mbed_official 0:9a16c3f036b0 76 nfc_err_t init()
mbed_official 0:9a16c3f036b0 77 {
mbed_official 0:9a16c3f036b0 78 nfc_err_t err = _nfc_controller.initialize();
mbed_official 0:9a16c3f036b0 79 if (err) {
mbed_official 0:9a16c3f036b0 80 return err;
mbed_official 0:9a16c3f036b0 81 }
mbed_official 0:9a16c3f036b0 82
mbed_official 0:9a16c3f036b0 83 // register callbacks
mbed_official 0:9a16c3f036b0 84 _nfc_controller.set_delegate(this);
mbed_official 0:9a16c3f036b0 85
mbed_official 0:9a16c3f036b0 86 nfc_rf_protocols_bitmask_t protocols = { 0 };
mbed_official 0:9a16c3f036b0 87 protocols.target_iso_dep = 1;
mbed_official 0:9a16c3f036b0 88 return _nfc_controller.configure_rf_protocols(protocols);
mbed_official 0:9a16c3f036b0 89 }
mbed_official 0:9a16c3f036b0 90
mbed_official 0:9a16c3f036b0 91 /**
mbed_official 0:9a16c3f036b0 92 * Start the discovery of peers.
mbed_official 0:9a16c3f036b0 93 *
mbed_official 0:9a16c3f036b0 94 * @return NFC_OK in case of success or a meaningful error code in case of
mbed_official 0:9a16c3f036b0 95 * failure.
mbed_official 0:9a16c3f036b0 96 */
mbed_official 0:9a16c3f036b0 97 nfc_err_t start_discovery()
mbed_official 0:9a16c3f036b0 98 {
mbed_official 0:9a16c3f036b0 99 return _nfc_controller.start_discovery();
mbed_official 0:9a16c3f036b0 100 }
mbed_official 0:9a16c3f036b0 101
mbed_official 0:9a16c3f036b0 102 private:
mbed_official 0:9a16c3f036b0 103 /* ------------------------------------------------------------------------
mbed_official 0:9a16c3f036b0 104 * Implementation of NFCRemoteInitiator::Delegate
mbed_official 0:9a16c3f036b0 105 */
mbed_official 1:6c82b777db0b 106 virtual void on_connected() { }
mbed_official 0:9a16c3f036b0 107
mbed_official 0:9a16c3f036b0 108 virtual void on_disconnected()
mbed_official 0:9a16c3f036b0 109 {
mbed_official 0:9a16c3f036b0 110 // reset the state of the remote initiator
mbed_official 0:9a16c3f036b0 111 _nfc_remote_initiator->set_delegate(NULL);
mbed_official 0:9a16c3f036b0 112 _nfc_remote_initiator.reset();
mbed_official 0:9a16c3f036b0 113
mbed_official 0:9a16c3f036b0 114 // restart peer discovery
mbed_official 0:9a16c3f036b0 115 _nfc_controller.start_discovery();
mbed_official 0:9a16c3f036b0 116 }
mbed_official 0:9a16c3f036b0 117
mbed_official 1:6c82b777db0b 118 virtual void parse_ndef_message(const Span<const uint8_t> &buffer) { }
mbed_official 0:9a16c3f036b0 119
mbed_official 0:9a16c3f036b0 120 virtual size_t build_ndef_message(const Span<uint8_t> &buffer)
mbed_official 0:9a16c3f036b0 121 {
mbed_official 0:9a16c3f036b0 122 // build the smart poster object we want to send
mbed_official 0:9a16c3f036b0 123 SmartPoster smart_poster(
mbed_official 0:9a16c3f036b0 124 URI(URI::HTTPS_WWW, span_from_cstr("mbed.com"))
mbed_official 0:9a16c3f036b0 125 );
mbed_official 0:9a16c3f036b0 126 smart_poster.set_title(
mbed_official 0:9a16c3f036b0 127 Text(Text::UTF8, span_from_cstr("en-US"), span_from_cstr("mbed website"))
mbed_official 0:9a16c3f036b0 128 );
mbed_official 0:9a16c3f036b0 129 smart_poster.set_action(SmartPoster::EXECUTE);
mbed_official 0:9a16c3f036b0 130
mbed_official 0:9a16c3f036b0 131 // serialize the smart poster into an ndef message operating on the
mbed_official 0:9a16c3f036b0 132 // buffer in input.
mbed_official 0:9a16c3f036b0 133 MessageBuilder builder(buffer);
mbed_official 0:9a16c3f036b0 134 smart_poster.append_record(builder, /* last ? */ true);
mbed_official 0:9a16c3f036b0 135
mbed_official 0:9a16c3f036b0 136 return builder.get_message().size();
mbed_official 0:9a16c3f036b0 137 }
mbed_official 0:9a16c3f036b0 138
mbed_official 0:9a16c3f036b0 139 /* ------------------------------------------------------------------------
mbed_official 0:9a16c3f036b0 140 * Implementation of NFCController::Delegate
mbed_official 0:9a16c3f036b0 141 */
mbed_official 0:9a16c3f036b0 142 virtual void on_discovery_terminated(nfc_discovery_terminated_reason_t reason)
mbed_official 0:9a16c3f036b0 143 {
mbed_official 0:9a16c3f036b0 144 if(reason != nfc_discovery_terminated_completed) {
mbed_official 0:9a16c3f036b0 145 _nfc_controller.start_discovery();
mbed_official 0:9a16c3f036b0 146 }
mbed_official 0:9a16c3f036b0 147 }
mbed_official 0:9a16c3f036b0 148
mbed_official 0:9a16c3f036b0 149 virtual void on_nfc_initiator_discovered(const SharedPtr<NFCRemoteInitiator> &nfc_initiator)
mbed_official 0:9a16c3f036b0 150 {
mbed_official 0:9a16c3f036b0 151 // setup the local remote initiator
mbed_official 0:9a16c3f036b0 152 _nfc_remote_initiator = nfc_initiator;
mbed_official 0:9a16c3f036b0 153 _nfc_remote_initiator->set_delegate(this);
mbed_official 0:9a16c3f036b0 154 _nfc_remote_initiator->connect();
mbed_official 0:9a16c3f036b0 155 }
mbed_official 0:9a16c3f036b0 156
mbed_official 0:9a16c3f036b0 157 mbed::nfc::PN512SPITransportDriver _pn512_transport;
mbed_official 0:9a16c3f036b0 158 mbed::nfc::PN512Driver _pn512_driver;
mbed_official 0:9a16c3f036b0 159 EventQueue& _queue;
mbed_official 0:9a16c3f036b0 160 uint8_t _ndef_buffer[1024];
mbed_official 0:9a16c3f036b0 161 NFCController _nfc_controller;
mbed_official 0:9a16c3f036b0 162 SharedPtr<NFCRemoteInitiator> _nfc_remote_initiator;
mbed_official 0:9a16c3f036b0 163 };
mbed_official 0:9a16c3f036b0 164
mbed_official 0:9a16c3f036b0 165 int main()
mbed_official 0:9a16c3f036b0 166 {
mbed_official 0:9a16c3f036b0 167 events::EventQueue queue;
mbed_official 0:9a16c3f036b0 168 NFCProcess nfc_process(queue);
mbed_official 0:9a16c3f036b0 169
mbed_official 0:9a16c3f036b0 170 nfc_err_t ret = nfc_process.init();
mbed_official 0:9a16c3f036b0 171 printf("Initialize: ret = %u\r\n", ret);
mbed_official 0:9a16c3f036b0 172
mbed_official 0:9a16c3f036b0 173 ret = nfc_process.start_discovery();
mbed_official 0:9a16c3f036b0 174 printf("Start Discovery: ret = %u\r\n", ret);
mbed_official 0:9a16c3f036b0 175
mbed_official 0:9a16c3f036b0 176 queue.dispatch_forever();
mbed_official 0:9a16c3f036b0 177
mbed_official 0:9a16c3f036b0 178 return 0;
mbed_official 0:9a16c3f036b0 179 }
mbed_official 0:9a16c3f036b0 180