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 ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ReadUriCallbacks.h Source File

ReadUriCallbacks.h

00001 /**
00002   ******************************************************************************
00003   * @file       ReadUriCallbacks.cpp
00004   * @date       12/07/2017
00005   * @brief      Class to read and print 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 read a NDef Message and print all the
00040  * record of type URI.
00041  * After each operation the class will switch on a led
00042  */
00043 class ReadUriCallbacks : public NDefLib::NDefNfcTag::Callbacks {
00044  
00045     DigitalOut &mOnOpenSession;
00046     DigitalOut &mOnRead;
00047     DigitalOut &mOnCloseSession;
00048  
00049     NDefLib::Message mMsg;
00050  
00051 public:
00052  
00053     /**
00054      * create the callback chain
00055      * @param onOpenSession led to switch on when the session open
00056      * @param onWrite led to switch on when the write end
00057      * @param onCloseSession led to switch on when the session end
00058      */
00059     ReadUriCallbacks(DigitalOut &onOpenSession,DigitalOut &onRead,
00060         DigitalOut &onCloseSession):mOnOpenSession(onOpenSession),
00061         mOnRead(onRead),mOnCloseSession(onCloseSession){};
00062  
00063     /**
00064      * crate the new message and write it
00065      * @param tag tag where write the message
00066      * @param success true if the session correctly open
00067      */
00068     virtual void on_session_open(NDefLib::NDefNfcTag *tag,bool success){
00069         if (!success) {
00070             printf("Error opening the session\r\n");
00071         }//else
00072         printf("Session opened\r\n");
00073         //ask to have an interrupt when the command finish
00074         mOnOpenSession=1;
00075         
00076         tag->read(&mMsg);
00077     }
00078  
00079     /**
00080      * request to close the session
00081      * @param tag tag where close the session
00082      * @param success true if the message is correctly wrote
00083      * @param message wrote
00084      */
00085     virtual void on_message_read(NDefLib::NDefNfcTag *tag,bool success,
00086         const NDefLib::Message*){
00087  
00088         if (!success) {
00089             printf("Error Reading tag!\r\n");
00090         } else {
00091             const uint32_t nRecords =mMsg.get_N_records();
00092             printf("Read %d records!\r\n",nRecords);
00093             for (uint32_t i=0;i<nRecords;i++) {
00094                 if (mMsg[i]->get_type()== NDefLib::Record::TYPE_URI) {
00095                     NDefLib::RecordURI *rUri = (NDefLib::RecordURI *)mMsg[i];
00096                     printf("UriType: %x\r\nUriContent: %s\r\n",
00097                         rUri->get_uri_id(),
00098                         rUri->get_content().c_str());
00099                 }//if
00100             }//for
00101             NDefLib::Message::remove_and_delete_all_record(mMsg);
00102             mOnRead=1;
00103         }//if-else
00104         tag->close_session();
00105     }
00106  
00107     /**
00108      * switch on the led
00109      * @param tag where the session is closed
00110      * @param success true if the session is correctly close
00111      */
00112     virtual void on_session_close(NDefLib::NDefNfcTag*, bool success) {
00113         if (success) {
00114             printf("Session closed\r\n");
00115             mOnCloseSession=1;
00116         } else {
00117             printf("Error opening the session\r\n");
00118         }
00119     }
00120 };