
A simple application providing an example of asynchronous access to the X-NUCLEO_NFC01A1 Dynamic NFC Tag board.
Dependencies: NDefLib X_NUCLEO_NFC01A1 mbed
Fork of HelloWord_Async_NFC01A1 by
X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board Firmware Package
Introduction
This firmware package includes Components Device Drivers, Board Support Package and example applications for STMicroelectronics X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board based on M24SR.
Example Application
The application provides a simple example of asynchronous access to the X-NUCLEO-NFC01A1 Dynamic NFC Tag Expansion Board. The program writes a URI link to the M24SR dynamic tag using the asynchronous programming model. The URI can then be retrieved from an NFC enabled smartphone/tablet.
A simpler example providing synchronous access to the tag is also available.
ReadUriCallbacks.h@10:a7834e6dd638, 2017-08-21 (annotated)
- Committer:
- giovannivisentini
- Date:
- Mon Aug 21 12:20:40 2017 +0000
- Revision:
- 10:a7834e6dd638
- Parent:
- 7:cd34abf10350
update NDefLib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Davidroid | 7:cd34abf10350 | 1 | /** |
Davidroid | 7:cd34abf10350 | 2 | ****************************************************************************** |
Davidroid | 7:cd34abf10350 | 3 | * @file ReadUriCallbacks.cpp |
Davidroid | 7:cd34abf10350 | 4 | * @date 12/07/2017 |
Davidroid | 7:cd34abf10350 | 5 | * @brief Class to read and print a URI tag. |
Davidroid | 7:cd34abf10350 | 6 | ****************************************************************************** |
Davidroid | 7:cd34abf10350 | 7 | * |
Davidroid | 7:cd34abf10350 | 8 | * COPYRIGHT(c) 2017 STMicroelectronics |
Davidroid | 7:cd34abf10350 | 9 | * |
Davidroid | 7:cd34abf10350 | 10 | * Redistribution and use in source and binary forms, with or without modification, |
Davidroid | 7:cd34abf10350 | 11 | * are permitted provided that the following conditions are met: |
Davidroid | 7:cd34abf10350 | 12 | * 1. Redistributions of source code must retain the above copyright notice, |
Davidroid | 7:cd34abf10350 | 13 | * this list of conditions and the following disclaimer. |
Davidroid | 7:cd34abf10350 | 14 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
Davidroid | 7:cd34abf10350 | 15 | * this list of conditions and the following disclaimer in the documentation |
Davidroid | 7:cd34abf10350 | 16 | * and/or other materials provided with the distribution. |
Davidroid | 7:cd34abf10350 | 17 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
Davidroid | 7:cd34abf10350 | 18 | * may be used to endorse or promote products derived from this software |
Davidroid | 7:cd34abf10350 | 19 | * without specific prior written permission. |
Davidroid | 7:cd34abf10350 | 20 | * |
Davidroid | 7:cd34abf10350 | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Davidroid | 7:cd34abf10350 | 22 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Davidroid | 7:cd34abf10350 | 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
Davidroid | 7:cd34abf10350 | 24 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
Davidroid | 7:cd34abf10350 | 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
Davidroid | 7:cd34abf10350 | 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
Davidroid | 7:cd34abf10350 | 27 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
Davidroid | 7:cd34abf10350 | 28 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
Davidroid | 7:cd34abf10350 | 29 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
Davidroid | 7:cd34abf10350 | 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Davidroid | 7:cd34abf10350 | 31 | * |
Davidroid | 7:cd34abf10350 | 32 | ****************************************************************************** |
Davidroid | 7:cd34abf10350 | 33 | */ |
Davidroid | 7:cd34abf10350 | 34 | |
Davidroid | 7:cd34abf10350 | 35 | #include "mbed.h" |
Davidroid | 7:cd34abf10350 | 36 | #include "NDefLib/RecordType/RecordURI.h" |
Davidroid | 7:cd34abf10350 | 37 | |
Davidroid | 7:cd34abf10350 | 38 | /** |
Davidroid | 7:cd34abf10350 | 39 | * Chain of callback that will read a NDef Message and print all the |
Davidroid | 7:cd34abf10350 | 40 | * record of type URI. |
Davidroid | 7:cd34abf10350 | 41 | * After each operation the class will switch on a led |
Davidroid | 7:cd34abf10350 | 42 | */ |
Davidroid | 7:cd34abf10350 | 43 | class ReadUriCallbacks : public NDefLib::NDefNfcTag::Callbacks { |
Davidroid | 7:cd34abf10350 | 44 | |
Davidroid | 7:cd34abf10350 | 45 | DigitalOut &mOnOpenSession; |
Davidroid | 7:cd34abf10350 | 46 | DigitalOut &mOnRead; |
Davidroid | 7:cd34abf10350 | 47 | DigitalOut &mOnCloseSession; |
Davidroid | 7:cd34abf10350 | 48 | |
Davidroid | 7:cd34abf10350 | 49 | NDefLib::Message mMsg; |
Davidroid | 7:cd34abf10350 | 50 | |
Davidroid | 7:cd34abf10350 | 51 | public: |
Davidroid | 7:cd34abf10350 | 52 | |
Davidroid | 7:cd34abf10350 | 53 | /** |
Davidroid | 7:cd34abf10350 | 54 | * create the callback chain |
Davidroid | 7:cd34abf10350 | 55 | * @param onOpenSession led to switch on when the session open |
Davidroid | 7:cd34abf10350 | 56 | * @param onWrite led to switch on when the write end |
Davidroid | 7:cd34abf10350 | 57 | * @param onCloseSession led to switch on when the session end |
Davidroid | 7:cd34abf10350 | 58 | */ |
Davidroid | 7:cd34abf10350 | 59 | ReadUriCallbacks(DigitalOut &onOpenSession,DigitalOut &onRead, |
Davidroid | 7:cd34abf10350 | 60 | DigitalOut &onCloseSession):mOnOpenSession(onOpenSession), |
Davidroid | 7:cd34abf10350 | 61 | mOnRead(onRead),mOnCloseSession(onCloseSession){}; |
Davidroid | 7:cd34abf10350 | 62 | |
Davidroid | 7:cd34abf10350 | 63 | /** |
Davidroid | 7:cd34abf10350 | 64 | * crate the new message and write it |
Davidroid | 7:cd34abf10350 | 65 | * @param tag tag where write the message |
Davidroid | 7:cd34abf10350 | 66 | * @param success true if the session correctly open |
Davidroid | 7:cd34abf10350 | 67 | */ |
Davidroid | 7:cd34abf10350 | 68 | virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success){ |
Davidroid | 7:cd34abf10350 | 69 | if (!success) { |
Davidroid | 7:cd34abf10350 | 70 | printf("Error opening the session\r\n"); |
Davidroid | 7:cd34abf10350 | 71 | }//else |
Davidroid | 7:cd34abf10350 | 72 | printf("Session opened\r\n"); |
Davidroid | 7:cd34abf10350 | 73 | //ask to have an interrupt when the command finish |
Davidroid | 7:cd34abf10350 | 74 | mOnOpenSession=1; |
Davidroid | 7:cd34abf10350 | 75 | |
Davidroid | 7:cd34abf10350 | 76 | tag->read(&mMsg); |
Davidroid | 7:cd34abf10350 | 77 | } |
Davidroid | 7:cd34abf10350 | 78 | |
Davidroid | 7:cd34abf10350 | 79 | /** |
Davidroid | 7:cd34abf10350 | 80 | * request to close the session |
Davidroid | 7:cd34abf10350 | 81 | * @param tag tag where close the session |
Davidroid | 7:cd34abf10350 | 82 | * @param success true if the message is correctly wrote |
Davidroid | 7:cd34abf10350 | 83 | * @param message wrote |
Davidroid | 7:cd34abf10350 | 84 | */ |
Davidroid | 7:cd34abf10350 | 85 | virtual void on_message_read(NDefLib::NDefNfcTag *tag,bool success, |
Davidroid | 7:cd34abf10350 | 86 | const NDefLib::Message*){ |
Davidroid | 7:cd34abf10350 | 87 | |
Davidroid | 7:cd34abf10350 | 88 | if (!success) { |
Davidroid | 7:cd34abf10350 | 89 | printf("Error Reading tag!\r\n"); |
Davidroid | 7:cd34abf10350 | 90 | } else { |
Davidroid | 7:cd34abf10350 | 91 | const uint32_t nRecords =mMsg.get_N_records(); |
Davidroid | 7:cd34abf10350 | 92 | printf("Read %d records!\r\n",nRecords); |
Davidroid | 7:cd34abf10350 | 93 | for (uint32_t i=0;i<nRecords;i++) { |
Davidroid | 7:cd34abf10350 | 94 | if (mMsg[i]->get_type()== NDefLib::Record::TYPE_URI) { |
Davidroid | 7:cd34abf10350 | 95 | NDefLib::RecordURI *rUri = (NDefLib::RecordURI *)mMsg[i]; |
Davidroid | 7:cd34abf10350 | 96 | printf("UriType: %x\r\nUriContent: %s\r\n", |
Davidroid | 7:cd34abf10350 | 97 | rUri->get_uri_id(), |
Davidroid | 7:cd34abf10350 | 98 | rUri->get_content().c_str()); |
Davidroid | 7:cd34abf10350 | 99 | }//if |
Davidroid | 7:cd34abf10350 | 100 | }//for |
Davidroid | 7:cd34abf10350 | 101 | NDefLib::Message::remove_and_delete_all_record(mMsg); |
Davidroid | 7:cd34abf10350 | 102 | mOnRead=1; |
Davidroid | 7:cd34abf10350 | 103 | }//if-else |
Davidroid | 7:cd34abf10350 | 104 | tag->close_session(); |
Davidroid | 7:cd34abf10350 | 105 | } |
Davidroid | 7:cd34abf10350 | 106 | |
Davidroid | 7:cd34abf10350 | 107 | /** |
Davidroid | 7:cd34abf10350 | 108 | * switch on the led |
Davidroid | 7:cd34abf10350 | 109 | * @param tag where the session is closed |
Davidroid | 7:cd34abf10350 | 110 | * @param success true if the session is correctly close |
Davidroid | 7:cd34abf10350 | 111 | */ |
Davidroid | 7:cd34abf10350 | 112 | virtual void on_session_close(NDefLib::NDefNfcTag*, bool success) { |
Davidroid | 7:cd34abf10350 | 113 | if (success) { |
Davidroid | 7:cd34abf10350 | 114 | printf("Session closed\r\n"); |
Davidroid | 7:cd34abf10350 | 115 | mOnCloseSession=1; |
Davidroid | 7:cd34abf10350 | 116 | } else { |
Davidroid | 7:cd34abf10350 | 117 | printf("Error opening the session\r\n"); |
Davidroid | 7:cd34abf10350 | 118 | } |
Davidroid | 7:cd34abf10350 | 119 | } |
Davidroid | 7:cd34abf10350 | 120 | }; |