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