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:
Mon Sep 24 17:09:42 2018 +0100
Revision:
0:9a16c3f036b0
Child:
1:6c82b777db0b
Add mbed deploy into the SmartPoster instructions.
.
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 0:9a16c3f036b0 106 virtual void on_connected()
mbed_official 0:9a16c3f036b0 107 {
mbed_official 0:9a16c3f036b0 108 printf("Connected\r\n");
mbed_official 0:9a16c3f036b0 109 }
mbed_official 0:9a16c3f036b0 110
mbed_official 0:9a16c3f036b0 111 virtual void on_disconnected()
mbed_official 0:9a16c3f036b0 112 {
mbed_official 0:9a16c3f036b0 113 printf("Disconnected\r\n");
mbed_official 0:9a16c3f036b0 114
mbed_official 0:9a16c3f036b0 115 // reset the state of the remote initiator
mbed_official 0:9a16c3f036b0 116 _nfc_remote_initiator->set_delegate(NULL);
mbed_official 0:9a16c3f036b0 117 _nfc_remote_initiator.reset();
mbed_official 0:9a16c3f036b0 118
mbed_official 0:9a16c3f036b0 119 // restart peer discovery
mbed_official 0:9a16c3f036b0 120 _nfc_controller.start_discovery();
mbed_official 0:9a16c3f036b0 121 }
mbed_official 0:9a16c3f036b0 122
mbed_official 0:9a16c3f036b0 123 virtual void parse_ndef_message(const Span<const uint8_t> &buffer)
mbed_official 0:9a16c3f036b0 124 {
mbed_official 0:9a16c3f036b0 125 printf("Received an ndef message of size %d\r\n", buffer.size());
mbed_official 0:9a16c3f036b0 126 }
mbed_official 0:9a16c3f036b0 127
mbed_official 0:9a16c3f036b0 128 virtual size_t build_ndef_message(const Span<uint8_t> &buffer)
mbed_official 0:9a16c3f036b0 129 {
mbed_official 0:9a16c3f036b0 130 printf("Building SmartPoster message\r\n");
mbed_official 0:9a16c3f036b0 131
mbed_official 0:9a16c3f036b0 132 // build the smart poster object we want to send
mbed_official 0:9a16c3f036b0 133 SmartPoster smart_poster(
mbed_official 0:9a16c3f036b0 134 URI(URI::HTTPS_WWW, span_from_cstr("mbed.com"))
mbed_official 0:9a16c3f036b0 135 );
mbed_official 0:9a16c3f036b0 136 smart_poster.set_title(
mbed_official 0:9a16c3f036b0 137 Text(Text::UTF8, span_from_cstr("en-US"), span_from_cstr("mbed website"))
mbed_official 0:9a16c3f036b0 138 );
mbed_official 0:9a16c3f036b0 139 smart_poster.set_action(SmartPoster::EXECUTE);
mbed_official 0:9a16c3f036b0 140
mbed_official 0:9a16c3f036b0 141 // serialize the smart poster into an ndef message operating on the
mbed_official 0:9a16c3f036b0 142 // buffer in input.
mbed_official 0:9a16c3f036b0 143 MessageBuilder builder(buffer);
mbed_official 0:9a16c3f036b0 144 smart_poster.append_record(builder, /* last ? */ true);
mbed_official 0:9a16c3f036b0 145
mbed_official 0:9a16c3f036b0 146 return builder.get_message().size();
mbed_official 0:9a16c3f036b0 147 }
mbed_official 0:9a16c3f036b0 148
mbed_official 0:9a16c3f036b0 149 /* ------------------------------------------------------------------------
mbed_official 0:9a16c3f036b0 150 * Implementation of NFCController::Delegate
mbed_official 0:9a16c3f036b0 151 */
mbed_official 0:9a16c3f036b0 152 virtual void on_discovery_terminated(nfc_discovery_terminated_reason_t reason)
mbed_official 0:9a16c3f036b0 153 {
mbed_official 0:9a16c3f036b0 154 printf("Discovery terminated: %u\r\n", reason);
mbed_official 0:9a16c3f036b0 155 if(reason != nfc_discovery_terminated_completed) {
mbed_official 0:9a16c3f036b0 156 _nfc_controller.start_discovery();
mbed_official 0:9a16c3f036b0 157 }
mbed_official 0:9a16c3f036b0 158 }
mbed_official 0:9a16c3f036b0 159
mbed_official 0:9a16c3f036b0 160 virtual void on_nfc_initiator_discovered(const SharedPtr<NFCRemoteInitiator> &nfc_initiator)
mbed_official 0:9a16c3f036b0 161 {
mbed_official 0:9a16c3f036b0 162 printf("Initiator discovered\r\n");
mbed_official 0:9a16c3f036b0 163
mbed_official 0:9a16c3f036b0 164 // setup the local remote initiator
mbed_official 0:9a16c3f036b0 165 _nfc_remote_initiator = nfc_initiator;
mbed_official 0:9a16c3f036b0 166 _nfc_remote_initiator->set_delegate(this);
mbed_official 0:9a16c3f036b0 167 _nfc_remote_initiator->connect();
mbed_official 0:9a16c3f036b0 168 }
mbed_official 0:9a16c3f036b0 169
mbed_official 0:9a16c3f036b0 170 mbed::nfc::PN512SPITransportDriver _pn512_transport;
mbed_official 0:9a16c3f036b0 171 mbed::nfc::PN512Driver _pn512_driver;
mbed_official 0:9a16c3f036b0 172 EventQueue& _queue;
mbed_official 0:9a16c3f036b0 173 uint8_t _ndef_buffer[1024];
mbed_official 0:9a16c3f036b0 174 NFCController _nfc_controller;
mbed_official 0:9a16c3f036b0 175 SharedPtr<NFCRemoteInitiator> _nfc_remote_initiator;
mbed_official 0:9a16c3f036b0 176 };
mbed_official 0:9a16c3f036b0 177
mbed_official 0:9a16c3f036b0 178 int main()
mbed_official 0:9a16c3f036b0 179 {
mbed_official 0:9a16c3f036b0 180 events::EventQueue queue;
mbed_official 0:9a16c3f036b0 181 NFCProcess nfc_process(queue);
mbed_official 0:9a16c3f036b0 182
mbed_official 0:9a16c3f036b0 183 nfc_err_t ret = nfc_process.init();
mbed_official 0:9a16c3f036b0 184 printf("Initialize: ret = %u\r\n", ret);
mbed_official 0:9a16c3f036b0 185
mbed_official 0:9a16c3f036b0 186 ret = nfc_process.start_discovery();
mbed_official 0:9a16c3f036b0 187 printf("Start Discovery: ret = %u\r\n", ret);
mbed_official 0:9a16c3f036b0 188
mbed_official 0:9a16c3f036b0 189 queue.dispatch_forever();
mbed_official 0:9a16c3f036b0 190
mbed_official 0:9a16c3f036b0 191 return 0;
mbed_official 0:9a16c3f036b0 192 }
mbed_official 0:9a16c3f036b0 193