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