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 Nicola Capovilla

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WriteUriCallbacks.h Source File

WriteUriCallbacks.h

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file       WriteUriCallbacks.h
00004   * @date       12/07/2017
00005   * @brief      Class to write a URI tag.
00006   ******************************************************************************
00007   *
00008   * COPYRIGHT(c) 2017 STMicroelectronics
00009   *
00010   * Redistribution and use in source and binary forms, with or without modification,
00011   * are permitted provided that the following conditions are met:
00012   *   1. Redistributions of source code must retain the above copyright notice,
00013   *      this list of conditions and the following disclaimer.
00014   *   2. Redistributions in binary form must reproduce the above copyright notice,
00015   *      this list of conditions and the following disclaimer in the documentation
00016   *      and/or other materials provided with the distribution.
00017   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00018   *      may be used to endorse or promote products derived from this software
00019   *      without specific prior written permission.
00020   *
00021   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00022   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00025   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00027   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00029   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00030   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031   *
00032   ******************************************************************************
00033   */
00034 
00035 #include "mbed.h"
00036 #include "NDefLib/RecordType/RecordURI.h"
00037  
00038 /**
00039  * Chain of callback that will crate a Uri record and write it.
00040  * After each operation the class will switch on a led
00041  */
00042 class WriteUriCallbacks : public NDefLib::NDefNfcTag::Callbacks {
00043  
00044     DigitalOut &mOnOpenSession;
00045     DigitalOut &mOnWrite;
00046     DigitalOut &mOnCloseSession;
00047     NDefLib::Message *msg;
00048  
00049 public:
00050  
00051     /**
00052      * create the callback chain
00053      * @param onOpenSession led to switch on when the session open
00054      * @param onWrite led to switch on when the write end
00055      * @param onCloseSession led to switch on when the session end
00056      */
00057     WriteUriCallbacks(DigitalOut &onOpenSession,DigitalOut &onWrite,
00058         DigitalOut &onCloseSession):mOnOpenSession(onOpenSession),
00059         mOnWrite(onWrite),mOnCloseSession(onCloseSession){};
00060  
00061     /**
00062      * crate the new message and write it
00063      * @param tag tag where write the message
00064      * @param success true if the session correctly open
00065      */
00066     virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success) {
00067         if (!success) {
00068             printf("Error opening the session\r\n");
00069         }//else
00070         printf("Session opened\r\n");
00071         //ask to have an interrupt when the command finish
00072         mOnOpenSession=1;
00073         mOnCloseSession=0;
00074         
00075         NDefLib::RecordURI *rUri = new NDefLib::RecordURI(NDefLib::RecordURI::HTTP_WWW,"http://www.st.com");
00076         msg = new NDefLib::Message();
00077         msg->add_record(rUri);
00078  
00079         tag->write(*msg);
00080     }
00081  
00082     /**
00083      * request to close the session
00084      * @param tag tag where close the session
00085      * @param success true if the message is correctly wrote
00086      * @param message wrote
00087      */
00088     virtual void on_message_write(NDefLib::NDefNfcTag *tag,bool success,
00089         const NDefLib::Message&) {
00090  
00091         if (!success) {
00092             printf("Error writing tag!\r\n");
00093         } else {
00094             printf("Tag written!\r\n");
00095             mOnWrite=1;
00096         }//if-else
00097             
00098         NDefLib::Message::remove_and_delete_all_record(*msg);
00099         delete msg;
00100         tag->close_session();
00101     }
00102  
00103     /**
00104      * switch on the led
00105      * @param tag where the session is closed
00106      * @param success true if the session is correctly close
00107      */
00108     virtual void on_session_close(NDefLib::NDefNfcTag*, bool success) {
00109         if (success) {
00110             printf("Session closed\r\n");
00111             mOnCloseSession=1;
00112             mOnOpenSession=0;
00113             mOnWrite=0;
00114         } else {
00115             printf("Error closing the session\r\n");
00116         }
00117     }
00118 };