
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.
WriteUriCallbacks.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 WriteUriCallbacks.h |
Davidroid | 7:cd34abf10350 | 4 | * @date 12/07/2017 |
Davidroid | 7:cd34abf10350 | 5 | * @brief Class to write 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 crate a Uri record and write it. |
Davidroid | 7:cd34abf10350 | 40 | * After each operation the class will switch on a led |
Davidroid | 7:cd34abf10350 | 41 | */ |
Davidroid | 7:cd34abf10350 | 42 | class WriteUriCallbacks : public NDefLib::NDefNfcTag::Callbacks { |
Davidroid | 7:cd34abf10350 | 43 | |
Davidroid | 7:cd34abf10350 | 44 | DigitalOut &mOnOpenSession; |
Davidroid | 7:cd34abf10350 | 45 | DigitalOut &mOnWrite; |
Davidroid | 7:cd34abf10350 | 46 | DigitalOut &mOnCloseSession; |
giovannivisentini | 10:a7834e6dd638 | 47 | NDefLib::Message mMsg; |
Davidroid | 7:cd34abf10350 | 48 | |
Davidroid | 7:cd34abf10350 | 49 | public: |
Davidroid | 7:cd34abf10350 | 50 | |
Davidroid | 7:cd34abf10350 | 51 | /** |
Davidroid | 7:cd34abf10350 | 52 | * create the callback chain |
Davidroid | 7:cd34abf10350 | 53 | * @param onOpenSession led to switch on when the session open |
Davidroid | 7:cd34abf10350 | 54 | * @param onWrite led to switch on when the write end |
Davidroid | 7:cd34abf10350 | 55 | * @param onCloseSession led to switch on when the session end |
Davidroid | 7:cd34abf10350 | 56 | */ |
Davidroid | 7:cd34abf10350 | 57 | WriteUriCallbacks(DigitalOut &onOpenSession,DigitalOut &onWrite, |
Davidroid | 7:cd34abf10350 | 58 | DigitalOut &onCloseSession):mOnOpenSession(onOpenSession), |
Davidroid | 7:cd34abf10350 | 59 | mOnWrite(onWrite),mOnCloseSession(onCloseSession){}; |
Davidroid | 7:cd34abf10350 | 60 | |
Davidroid | 7:cd34abf10350 | 61 | /** |
Davidroid | 7:cd34abf10350 | 62 | * crate the new message and write it |
Davidroid | 7:cd34abf10350 | 63 | * @param tag tag where write the message |
Davidroid | 7:cd34abf10350 | 64 | * @param success true if the session correctly open |
Davidroid | 7:cd34abf10350 | 65 | */ |
Davidroid | 7:cd34abf10350 | 66 | virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success) { |
Davidroid | 7:cd34abf10350 | 67 | if (!success) { |
Davidroid | 7:cd34abf10350 | 68 | printf("Error opening the session\r\n"); |
Davidroid | 7:cd34abf10350 | 69 | }//else |
Davidroid | 7:cd34abf10350 | 70 | printf("Session opened\r\n"); |
Davidroid | 7:cd34abf10350 | 71 | //ask to have an interrupt when the command finish |
Davidroid | 7:cd34abf10350 | 72 | mOnOpenSession=1; |
Davidroid | 7:cd34abf10350 | 73 | |
giovannivisentini | 10:a7834e6dd638 | 74 | NDefLib::RecordURI *rUri = |
giovannivisentini | 10:a7834e6dd638 | 75 | new NDefLib::RecordURI(NDefLib::RecordURI::HTTP_WWW,"http://www.st.com"); |
giovannivisentini | 10:a7834e6dd638 | 76 | mMsg.add_record(rUri); |
Davidroid | 7:cd34abf10350 | 77 | |
giovannivisentini | 10:a7834e6dd638 | 78 | tag->write(mMsg); |
Davidroid | 7:cd34abf10350 | 79 | } |
Davidroid | 7:cd34abf10350 | 80 | |
Davidroid | 7:cd34abf10350 | 81 | /** |
Davidroid | 7:cd34abf10350 | 82 | * request to close the session |
Davidroid | 7:cd34abf10350 | 83 | * @param tag tag where close the session |
Davidroid | 7:cd34abf10350 | 84 | * @param success true if the message is correctly wrote |
Davidroid | 7:cd34abf10350 | 85 | * @param message wrote |
Davidroid | 7:cd34abf10350 | 86 | */ |
giovannivisentini | 10:a7834e6dd638 | 87 | virtual void on_message_write(NDefLib::NDefNfcTag *tag,bool success) { |
giovannivisentini | 10:a7834e6dd638 | 88 | NDefLib::Message::remove_and_delete_all_record(mMsg); |
Davidroid | 7:cd34abf10350 | 89 | if (!success) { |
Davidroid | 7:cd34abf10350 | 90 | printf("Error writing tag!\r\n"); |
Davidroid | 7:cd34abf10350 | 91 | } else { |
Davidroid | 7:cd34abf10350 | 92 | printf("Tag written!\r\n"); |
Davidroid | 7:cd34abf10350 | 93 | mOnWrite=1; |
Davidroid | 7:cd34abf10350 | 94 | }//if-else |
Davidroid | 7:cd34abf10350 | 95 | tag->close_session(); |
Davidroid | 7:cd34abf10350 | 96 | } |
Davidroid | 7:cd34abf10350 | 97 | |
Davidroid | 7:cd34abf10350 | 98 | /** |
Davidroid | 7:cd34abf10350 | 99 | * switch on the led |
Davidroid | 7:cd34abf10350 | 100 | * @param tag where the session is closed |
Davidroid | 7:cd34abf10350 | 101 | * @param success true if the session is correctly close |
Davidroid | 7:cd34abf10350 | 102 | */ |
Davidroid | 7:cd34abf10350 | 103 | virtual void on_session_close(NDefLib::NDefNfcTag*, bool success) { |
Davidroid | 7:cd34abf10350 | 104 | if (success) { |
Davidroid | 7:cd34abf10350 | 105 | printf("Session closed\r\n"); |
Davidroid | 7:cd34abf10350 | 106 | mOnCloseSession=1; |
Davidroid | 7:cd34abf10350 | 107 | } else { |
Davidroid | 7:cd34abf10350 | 108 | printf("Error closing the session\r\n"); |
Davidroid | 7:cd34abf10350 | 109 | } |
Davidroid | 7:cd34abf10350 | 110 | } |
Davidroid | 7:cd34abf10350 | 111 | }; |