Archives du projet VélIoT

Dependencies:   mbed NDefLib M24SR BSP_B-L475E-IOT01

Committer:
galaadleconte
Date:
Wed Oct 28 07:57:12 2020 +0000
Revision:
0:8ef2057f72b4
iot

Who changed what in which revision?

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