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.

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?

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