Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: M24SR NDefLib mbed
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 mOnCloseSession=0; 00076 00077 tag->read(&mMsg); 00078 } 00079 00080 /** 00081 * request to close the session 00082 * @param tag tag where close the session 00083 * @param success true if the message is correctly wrote 00084 * @param message wrote 00085 */ 00086 virtual void on_message_read(NDefLib::NDefNfcTag *tag,bool success, 00087 const NDefLib::Message*){ 00088 00089 if (!success) { 00090 printf("Error Reading tag!\r\n"); 00091 } else { 00092 const uint32_t nRecords =mMsg.get_N_records(); 00093 printf("Read %d records!\r\n",nRecords); 00094 for (uint32_t i=0;i<nRecords;i++) { 00095 if (mMsg[i]->get_type()== NDefLib::Record::TYPE_URI) { 00096 NDefLib::RecordURI *rUri = (NDefLib::RecordURI *)mMsg[i]; 00097 printf("UriType: %x\r\nUriContent: %s\r\n", 00098 rUri->get_uri_id(), 00099 rUri->get_content().c_str()); 00100 }//if 00101 }//for 00102 NDefLib::Message::remove_and_delete_all_record(mMsg); 00103 mOnRead=1; 00104 }//if-else 00105 tag->close_session(); 00106 } 00107 00108 /** 00109 * switch on the led 00110 * @param tag where the session is closed 00111 * @param success true if the session is correctly close 00112 */ 00113 virtual void on_session_close(NDefLib::NDefNfcTag*, bool success) { 00114 if (success) { 00115 printf("Session closed\r\n"); 00116 mOnCloseSession=1; 00117 mOnOpenSession=0; 00118 mOnRead=0; 00119 } else { 00120 printf("Error opening the session\r\n"); 00121 } 00122 } 00123 };
Generated on Mon Jul 18 2022 12:47:12 by
1.7.2