Utility library to read and write Ndef messages from/to a Type4 NFC tag
Dependents: NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more
Fork of NDefLib by
NDEF NFC library
This library provides an abstract API to create NDEF formatted messages and records and to read/write them from/to a Type4 NFC Tag.
Implementations
At the moment, the NDEF API is implemented by X_NUCLEO_NFC01A1 and X_NUCLEO_NFC02A1 Dynamic NFC Tag libraries respectively driving the X-NUCLEO-NFC01A1 and X-NUCLEO-NFC02A1 boards.
NDefNfcTag.h@14:ba0c186ae6d6, 2016-01-28 (annotated)
- Committer:
- giovannivisentini
- Date:
- Thu Jan 28 14:01:07 2016 +0000
- Revision:
- 14:ba0c186ae6d6
- Parent:
- 12:ed4d9b8d1410
- Child:
- 15:01fc5a4b8366
add async api
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giovannivisentini | 8:473f6e0b03df | 1 | /** |
giovannivisentini | 8:473f6e0b03df | 2 | ****************************************************************************** |
giovannivisentini | 8:473f6e0b03df | 3 | * @file Type4NfcTag.h |
giovannivisentini | 8:473f6e0b03df | 4 | * @author ST / Central Labs |
giovannivisentini | 8:473f6e0b03df | 5 | * @version V1.0.0 |
giovannivisentini | 14:ba0c186ae6d6 | 6 | * @date 21 Jan 2016 |
giovannivisentini | 8:473f6e0b03df | 7 | * @brief Generic interface that a device must implement for use the NDefLib |
giovannivisentini | 14:ba0c186ae6d6 | 8 | * with async communication |
giovannivisentini | 8:473f6e0b03df | 9 | ****************************************************************************** |
giovannivisentini | 8:473f6e0b03df | 10 | * @attention |
giovannivisentini | 8:473f6e0b03df | 11 | * |
giovannivisentini | 8:473f6e0b03df | 12 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
giovannivisentini | 8:473f6e0b03df | 13 | * |
giovannivisentini | 8:473f6e0b03df | 14 | * Redistribution and use in source and binary forms, with or without modification, |
giovannivisentini | 8:473f6e0b03df | 15 | * are permitted provided that the following conditions are met: |
giovannivisentini | 8:473f6e0b03df | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
giovannivisentini | 8:473f6e0b03df | 17 | * this list of conditions and the following disclaimer. |
giovannivisentini | 8:473f6e0b03df | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
giovannivisentini | 8:473f6e0b03df | 19 | * this list of conditions and the following disclaimer in the documentation |
giovannivisentini | 8:473f6e0b03df | 20 | * and/or other materials provided with the distribution. |
giovannivisentini | 8:473f6e0b03df | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
giovannivisentini | 8:473f6e0b03df | 22 | * may be used to endorse or promote products derived from this software |
giovannivisentini | 8:473f6e0b03df | 23 | * without specific prior written permission. |
giovannivisentini | 8:473f6e0b03df | 24 | * |
giovannivisentini | 8:473f6e0b03df | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
giovannivisentini | 8:473f6e0b03df | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
giovannivisentini | 8:473f6e0b03df | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
giovannivisentini | 8:473f6e0b03df | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
giovannivisentini | 8:473f6e0b03df | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
giovannivisentini | 8:473f6e0b03df | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
giovannivisentini | 8:473f6e0b03df | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
giovannivisentini | 8:473f6e0b03df | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
giovannivisentini | 8:473f6e0b03df | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
giovannivisentini | 8:473f6e0b03df | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
giovannivisentini | 8:473f6e0b03df | 35 | * |
giovannivisentini | 8:473f6e0b03df | 36 | ****************************************************************************** |
giovannivisentini | 8:473f6e0b03df | 37 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 38 | |
giovannivisentini | 14:ba0c186ae6d6 | 39 | #ifndef NDEFLIB_NDEFNFCTAG_H_ |
giovannivisentini | 14:ba0c186ae6d6 | 40 | #define NDEFLIB_NDEFNFCTAG_H_ |
giovannivisentini | 8:473f6e0b03df | 41 | |
giovannivisentini | 8:473f6e0b03df | 42 | #include "NDefLib/Message.h" |
giovannivisentini | 8:473f6e0b03df | 43 | |
giovannivisentini | 8:473f6e0b03df | 44 | namespace NDefLib { |
giovannivisentini | 8:473f6e0b03df | 45 | |
giovannivisentini | 8:473f6e0b03df | 46 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 47 | * Abstract class used to write/read NDef messages to/from a nfc tag. |
giovannivisentini | 14:ba0c186ae6d6 | 48 | * This class is made for handle also asynchronous communication with the nfc component. |
giovannivisentini | 14:ba0c186ae6d6 | 49 | * All the function in this class will return immediately, when the operation end a proper callback function will |
giovannivisentini | 14:ba0c186ae6d6 | 50 | * be called. |
giovannivisentini | 8:473f6e0b03df | 51 | */ |
giovannivisentini | 8:473f6e0b03df | 52 | class NDefNfcTag { |
giovannivisentini | 14:ba0c186ae6d6 | 53 | |
giovannivisentini | 8:473f6e0b03df | 54 | public: |
giovannivisentini | 8:473f6e0b03df | 55 | |
giovannivisentini | 14:ba0c186ae6d6 | 56 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 57 | * Class that contains all the function called when a command finish. |
giovannivisentini | 14:ba0c186ae6d6 | 58 | * The default implementation is an empty function. |
giovannivisentini | 14:ba0c186ae6d6 | 59 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 60 | class Callback { |
giovannivisentini | 14:ba0c186ae6d6 | 61 | public: |
giovannivisentini | 14:ba0c186ae6d6 | 62 | |
giovannivisentini | 14:ba0c186ae6d6 | 63 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 64 | * Called when a session is open. |
giovannivisentini | 14:ba0c186ae6d6 | 65 | * @param tag tag where the session is open. |
giovannivisentini | 14:ba0c186ae6d6 | 66 | * @param success true if the operation has success. |
giovannivisentini | 14:ba0c186ae6d6 | 67 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 68 | virtual void onSessionOpen(NDefNfcTag *tag,bool success){ |
giovannivisentini | 14:ba0c186ae6d6 | 69 | (void)tag;(void)success; |
giovannivisentini | 14:ba0c186ae6d6 | 70 | }; |
giovannivisentini | 14:ba0c186ae6d6 | 71 | |
giovannivisentini | 14:ba0c186ae6d6 | 72 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 73 | * Called when a message is write. |
giovannivisentini | 14:ba0c186ae6d6 | 74 | * @param tag tag where the message is wrote. |
giovannivisentini | 14:ba0c186ae6d6 | 75 | * @param success true if the operation has success. |
giovannivisentini | 14:ba0c186ae6d6 | 76 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 77 | virtual void onMessageWrite(NDefNfcTag *tag,bool success, |
giovannivisentini | 14:ba0c186ae6d6 | 78 | const Message &msg){ |
giovannivisentini | 14:ba0c186ae6d6 | 79 | (void)tag;(void)success; (void)msg; |
giovannivisentini | 14:ba0c186ae6d6 | 80 | |
giovannivisentini | 14:ba0c186ae6d6 | 81 | }; |
giovannivisentini | 14:ba0c186ae6d6 | 82 | |
giovannivisentini | 14:ba0c186ae6d6 | 83 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 84 | * Called when a message is read. |
giovannivisentini | 14:ba0c186ae6d6 | 85 | * @param tag tag where the message is read. |
giovannivisentini | 14:ba0c186ae6d6 | 86 | * @param success true if the operation has success. |
giovannivisentini | 14:ba0c186ae6d6 | 87 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 88 | virtual void onMessageRead(NDefNfcTag *tag,bool success, |
giovannivisentini | 14:ba0c186ae6d6 | 89 | const Message *msg){ |
giovannivisentini | 14:ba0c186ae6d6 | 90 | (void)tag;(void)success; (void)msg; |
giovannivisentini | 14:ba0c186ae6d6 | 91 | }; |
giovannivisentini | 14:ba0c186ae6d6 | 92 | |
giovannivisentini | 14:ba0c186ae6d6 | 93 | |
giovannivisentini | 14:ba0c186ae6d6 | 94 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 95 | * Called when a session is close. |
giovannivisentini | 14:ba0c186ae6d6 | 96 | * @param tag tag where the session is close. |
giovannivisentini | 14:ba0c186ae6d6 | 97 | * @param success true if the operation has success. |
giovannivisentini | 14:ba0c186ae6d6 | 98 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 99 | virtual void onSessionClose(NDefNfcTag *tag,bool success){ |
giovannivisentini | 14:ba0c186ae6d6 | 100 | (void)tag;(void)success; |
giovannivisentini | 14:ba0c186ae6d6 | 101 | }; |
giovannivisentini | 14:ba0c186ae6d6 | 102 | |
giovannivisentini | 14:ba0c186ae6d6 | 103 | virtual ~Callback(){}; |
giovannivisentini | 14:ba0c186ae6d6 | 104 | }; |
giovannivisentini | 14:ba0c186ae6d6 | 105 | |
giovannivisentini | 14:ba0c186ae6d6 | 106 | private: |
giovannivisentini | 14:ba0c186ae6d6 | 107 | |
giovannivisentini | 14:ba0c186ae6d6 | 108 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 109 | * Data used for store the callback status. |
giovannivisentini | 14:ba0c186ae6d6 | 110 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 111 | struct CallbackStatus{ |
giovannivisentini | 14:ba0c186ae6d6 | 112 | /**object that start the callback */ |
giovannivisentini | 14:ba0c186ae6d6 | 113 | NDefNfcTag *callOwner; |
giovannivisentini | 14:ba0c186ae6d6 | 114 | /** message that the callback is manage */ |
giovannivisentini | 14:ba0c186ae6d6 | 115 | Message *msg; |
giovannivisentini | 14:ba0c186ae6d6 | 116 | }; |
giovannivisentini | 14:ba0c186ae6d6 | 117 | |
giovannivisentini | 14:ba0c186ae6d6 | 118 | public: |
giovannivisentini | 14:ba0c186ae6d6 | 119 | |
giovannivisentini | 14:ba0c186ae6d6 | 120 | NDefNfcTag():mCallBack(&mDefaultCallBack){} |
giovannivisentini | 14:ba0c186ae6d6 | 121 | |
giovannivisentini | 14:ba0c186ae6d6 | 122 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 123 | * Set the callback. |
giovannivisentini | 14:ba0c186ae6d6 | 124 | * @param c object contains the callback. |
giovannivisentini | 14:ba0c186ae6d6 | 125 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 126 | void setCallBack(Callback *c){ |
giovannivisentini | 14:ba0c186ae6d6 | 127 | mCallBack=c; |
giovannivisentini | 14:ba0c186ae6d6 | 128 | }//setCallBack |
giovannivisentini | 14:ba0c186ae6d6 | 129 | |
giovannivisentini | 8:473f6e0b03df | 130 | |
giovannivisentini | 8:473f6e0b03df | 131 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 132 | * Open the communication session with the nfc tag. |
giovannivisentini | 12:ed4d9b8d1410 | 133 | * @param force Force to open a communication. |
giovannivisentini | 8:473f6e0b03df | 134 | * @return true if success |
giovannivisentini | 8:473f6e0b03df | 135 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 136 | virtual bool openSession(bool force = false)=0; |
giovannivisentini | 8:473f6e0b03df | 137 | |
giovannivisentini | 8:473f6e0b03df | 138 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 139 | * Close the communication with the nfc tag. |
giovannivisentini | 8:473f6e0b03df | 140 | * @return true if success |
giovannivisentini | 8:473f6e0b03df | 141 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 142 | virtual bool closeSession()=0; |
giovannivisentini | 14:ba0c186ae6d6 | 143 | |
giovannivisentini | 14:ba0c186ae6d6 | 144 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 145 | * Returns true if a communication with the nfc tag is open. |
giovannivisentini | 14:ba0c186ae6d6 | 146 | * @return true if a communication with the nfc tag is open |
giovannivisentini | 14:ba0c186ae6d6 | 147 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 148 | virtual bool isSessionOpen()=0; |
giovannivisentini | 8:473f6e0b03df | 149 | |
giovannivisentini | 8:473f6e0b03df | 150 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 151 | * Write a message in the nfc tag. |
giovannivisentini | 12:ed4d9b8d1410 | 152 | * @par This call will delete the previous message. |
giovannivisentini | 12:ed4d9b8d1410 | 153 | * @param msg Message to write. |
giovannivisentini | 8:473f6e0b03df | 154 | * @return true if success |
giovannivisentini | 8:473f6e0b03df | 155 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 156 | virtual bool write(Message &msg) { |
giovannivisentini | 14:ba0c186ae6d6 | 157 | if(!isSessionOpen()){ |
giovannivisentini | 14:ba0c186ae6d6 | 158 | mCallBack->onMessageWrite(this,false,msg); |
giovannivisentini | 8:473f6e0b03df | 159 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 160 | } |
giovannivisentini | 8:473f6e0b03df | 161 | |
giovannivisentini | 8:473f6e0b03df | 162 | const uint16_t length = msg.getByteLength(); |
giovannivisentini | 8:473f6e0b03df | 163 | uint8_t *buffer = new uint8_t[length]; |
giovannivisentini | 14:ba0c186ae6d6 | 164 | if(buffer==NULL){ //impossible to allocate the buffer |
giovannivisentini | 14:ba0c186ae6d6 | 165 | mCallBack->onMessageWrite(this,false,msg); |
giovannivisentini | 8:473f6e0b03df | 166 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 167 | } |
giovannivisentini | 14:ba0c186ae6d6 | 168 | |
giovannivisentini | 8:473f6e0b03df | 169 | msg.write(buffer); |
giovannivisentini | 14:ba0c186ae6d6 | 170 | |
giovannivisentini | 14:ba0c186ae6d6 | 171 | mCallBackStatus.callOwner=this; |
giovannivisentini | 14:ba0c186ae6d6 | 172 | mCallBackStatus.msg=&msg; |
giovannivisentini | 14:ba0c186ae6d6 | 173 | |
giovannivisentini | 14:ba0c186ae6d6 | 174 | return writeByte(buffer, length,0,NDefNfcTag::onWriteMessageCallback,&mCallBackStatus); |
giovannivisentini | 8:473f6e0b03df | 175 | } |
giovannivisentini | 8:473f6e0b03df | 176 | |
giovannivisentini | 8:473f6e0b03df | 177 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 178 | * Read a message from the tag. |
giovannivisentini | 12:ed4d9b8d1410 | 179 | * @param[in,out] msg Message object the read records are added to. |
giovannivisentini | 8:473f6e0b03df | 180 | * @return true if success |
giovannivisentini | 8:473f6e0b03df | 181 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 182 | virtual bool read(Message *msg) { |
giovannivisentini | 14:ba0c186ae6d6 | 183 | if(!isSessionOpen()){ |
giovannivisentini | 14:ba0c186ae6d6 | 184 | mCallBack->onMessageRead(this,false,msg); |
giovannivisentini | 8:473f6e0b03df | 185 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 186 | } |
giovannivisentini | 8:473f6e0b03df | 187 | |
giovannivisentini | 14:ba0c186ae6d6 | 188 | uint8_t *buffer = new uint8_t[2]; |
giovannivisentini | 14:ba0c186ae6d6 | 189 | if(buffer==NULL){ |
giovannivisentini | 14:ba0c186ae6d6 | 190 | mCallBack->onMessageRead(this,false,msg); |
giovannivisentini | 14:ba0c186ae6d6 | 191 | return false; |
giovannivisentini | 8:473f6e0b03df | 192 | } |
giovannivisentini | 14:ba0c186ae6d6 | 193 | |
giovannivisentini | 14:ba0c186ae6d6 | 194 | mCallBackStatus.callOwner=this; |
giovannivisentini | 14:ba0c186ae6d6 | 195 | mCallBackStatus.msg=msg; |
giovannivisentini | 14:ba0c186ae6d6 | 196 | return readByte(0,2,buffer,NDefNfcTag::onReadMessageLength,&mCallBackStatus); |
giovannivisentini | 8:473f6e0b03df | 197 | } |
giovannivisentini | 8:473f6e0b03df | 198 | |
giovannivisentini | 8:473f6e0b03df | 199 | virtual ~NDefNfcTag() {} |
giovannivisentini | 8:473f6e0b03df | 200 | |
giovannivisentini | 14:ba0c186ae6d6 | 201 | protected: |
giovannivisentini | 14:ba0c186ae6d6 | 202 | |
giovannivisentini | 14:ba0c186ae6d6 | 203 | typedef struct CallbackStatus CallbackStatus_t; |
giovannivisentini | 14:ba0c186ae6d6 | 204 | |
giovannivisentini | 8:473f6e0b03df | 205 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 206 | * Function that the component has to call when it finish a read/write operation |
giovannivisentini | 14:ba0c186ae6d6 | 207 | * @param interalState callback internal state data |
giovannivisentini | 14:ba0c186ae6d6 | 208 | * @param status true if the operation success |
giovannivisentini | 14:ba0c186ae6d6 | 209 | * @param buffer buffer write/read |
giovannivisentini | 14:ba0c186ae6d6 | 210 | * @param length number of byte read/write |
giovannivisentini | 14:ba0c186ae6d6 | 211 | * @return true if the operation had success |
giovannivisentini | 8:473f6e0b03df | 212 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 213 | typedef bool(*byteOperationCallback_t)(CallbackStatus_t *interalState, |
giovannivisentini | 14:ba0c186ae6d6 | 214 | bool status,const uint8_t *buffer, uint16_t length); |
giovannivisentini | 8:473f6e0b03df | 215 | |
giovannivisentini | 8:473f6e0b03df | 216 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 217 | * Write a sequence of bytes to the NDEF file. |
giovannivisentini | 14:ba0c186ae6d6 | 218 | * @param buffer buffer to write |
giovannivisentini | 14:ba0c186ae6d6 | 219 | * @param length number of bytes to write |
giovannivisentini | 14:ba0c186ae6d6 | 220 | * @param offset offset where start to write |
giovannivisentini | 14:ba0c186ae6d6 | 221 | * @param callback function to call when the operation ended |
giovannivisentini | 14:ba0c186ae6d6 | 222 | * @param callbackStatus parameter to pass to the callback function |
giovannivisentini | 14:ba0c186ae6d6 | 223 | * @return true if the operation has success |
giovannivisentini | 8:473f6e0b03df | 224 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 225 | virtual bool writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset, |
giovannivisentini | 14:ba0c186ae6d6 | 226 | byteOperationCallback_t callback,CallbackStatus_t *callbackStatus)=0; |
giovannivisentini | 8:473f6e0b03df | 227 | |
giovannivisentini | 8:473f6e0b03df | 228 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 229 | * Read a sequence of bytes from the NDEF file. |
giovannivisentini | 14:ba0c186ae6d6 | 230 | * @param byteOffset Read offset in bytes. |
giovannivisentini | 12:ed4d9b8d1410 | 231 | * @param byteLength Number of bytes to read. |
giovannivisentini | 12:ed4d9b8d1410 | 232 | * @param[out] buffer Buffer to store the read data into. |
giovannivisentini | 14:ba0c186ae6d6 | 233 | * @param callback function to call when the operation ended |
giovannivisentini | 14:ba0c186ae6d6 | 234 | * @param callbackStatus parameter to pass to the callback function |
giovannivisentini | 8:473f6e0b03df | 235 | * @return true if success |
giovannivisentini | 8:473f6e0b03df | 236 | */ |
giovannivisentini | 8:473f6e0b03df | 237 | virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength, |
giovannivisentini | 14:ba0c186ae6d6 | 238 | uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus)=0; |
giovannivisentini | 14:ba0c186ae6d6 | 239 | |
giovannivisentini | 8:473f6e0b03df | 240 | |
giovannivisentini | 14:ba0c186ae6d6 | 241 | /** object with the user callback */ |
giovannivisentini | 14:ba0c186ae6d6 | 242 | Callback *mCallBack; |
giovannivisentini | 8:473f6e0b03df | 243 | private: |
giovannivisentini | 12:ed4d9b8d1410 | 244 | |
giovannivisentini | 14:ba0c186ae6d6 | 245 | /** object with the current callback status */ |
giovannivisentini | 14:ba0c186ae6d6 | 246 | CallbackStatus_t mCallBackStatus; |
giovannivisentini | 14:ba0c186ae6d6 | 247 | /** default callback object, all the function are empty */ |
giovannivisentini | 14:ba0c186ae6d6 | 248 | Callback mDefaultCallBack; |
giovannivisentini | 14:ba0c186ae6d6 | 249 | |
giovannivisentini | 8:473f6e0b03df | 250 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 251 | * function that the user will call when a write end, it will invoke onMessageWrite |
giovannivisentini | 14:ba0c186ae6d6 | 252 | * @param internalState object that invoke the write operation |
giovannivisentini | 14:ba0c186ae6d6 | 253 | * @param status true if the operation had success |
giovannivisentini | 14:ba0c186ae6d6 | 254 | * @param buffer buffer wrote |
giovannivisentini | 14:ba0c186ae6d6 | 255 | * @param length number of byte wrote |
giovannivisentini | 14:ba0c186ae6d6 | 256 | * @return true if the write had success |
giovannivisentini | 8:473f6e0b03df | 257 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 258 | static bool onWriteMessageCallback(CallbackStatus_t *internalState, |
giovannivisentini | 14:ba0c186ae6d6 | 259 | bool status,const uint8_t *buffer, uint16_t ){ |
giovannivisentini | 14:ba0c186ae6d6 | 260 | delete [] buffer; |
giovannivisentini | 14:ba0c186ae6d6 | 261 | |
giovannivisentini | 14:ba0c186ae6d6 | 262 | internalState->callOwner->mCallBack-> |
giovannivisentini | 14:ba0c186ae6d6 | 263 | onMessageWrite(internalState->callOwner,status,*internalState->msg); |
giovannivisentini | 14:ba0c186ae6d6 | 264 | return status; |
giovannivisentini | 14:ba0c186ae6d6 | 265 | } |
giovannivisentini | 8:473f6e0b03df | 266 | |
giovannivisentini | 8:473f6e0b03df | 267 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 268 | * function that the use will call after a read operation. |
giovannivisentini | 14:ba0c186ae6d6 | 269 | * in this case we read the message length, this function will read all the message |
giovannivisentini | 14:ba0c186ae6d6 | 270 | * @param internalState object that invoke the write operation |
giovannivisentini | 14:ba0c186ae6d6 | 271 | * @param status true if the operation had success |
giovannivisentini | 14:ba0c186ae6d6 | 272 | * @param buffer buffer read |
giovannivisentini | 14:ba0c186ae6d6 | 273 | * @param length number of byte read |
giovannivisentini | 14:ba0c186ae6d6 | 274 | * @return true if the read had success |
giovannivisentini | 8:473f6e0b03df | 275 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 276 | static bool onReadMessageLength(CallbackStatus_t *internalState, |
giovannivisentini | 14:ba0c186ae6d6 | 277 | bool status,const uint8_t *buffer, uint16_t length){ |
giovannivisentini | 14:ba0c186ae6d6 | 278 | |
giovannivisentini | 14:ba0c186ae6d6 | 279 | if(!status || length!=2){ |
giovannivisentini | 14:ba0c186ae6d6 | 280 | internalState->callOwner->mCallBack-> |
giovannivisentini | 14:ba0c186ae6d6 | 281 | onMessageRead(internalState->callOwner,false,internalState->msg); |
giovannivisentini | 14:ba0c186ae6d6 | 282 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 283 | }//if |
giovannivisentini | 14:ba0c186ae6d6 | 284 | |
giovannivisentini | 14:ba0c186ae6d6 | 285 | length = (((uint16_t) buffer[0]) << 8 | buffer[1]); |
giovannivisentini | 14:ba0c186ae6d6 | 286 | delete [] buffer; |
giovannivisentini | 14:ba0c186ae6d6 | 287 | |
giovannivisentini | 14:ba0c186ae6d6 | 288 | uint8_t *readBuffer = new uint8_t[length]; |
giovannivisentini | 14:ba0c186ae6d6 | 289 | if(readBuffer==NULL){ |
giovannivisentini | 14:ba0c186ae6d6 | 290 | internalState->callOwner->mCallBack-> |
giovannivisentini | 14:ba0c186ae6d6 | 291 | onMessageRead(internalState->callOwner,false,internalState->msg); |
giovannivisentini | 14:ba0c186ae6d6 | 292 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 293 | }//readBuffer |
giovannivisentini | 14:ba0c186ae6d6 | 294 | |
giovannivisentini | 14:ba0c186ae6d6 | 295 | internalState->callOwner->readByte(2,length,readBuffer, |
giovannivisentini | 14:ba0c186ae6d6 | 296 | &NDefNfcTag::onReadMessageCallback,internalState); |
giovannivisentini | 14:ba0c186ae6d6 | 297 | return status; |
giovannivisentini | 14:ba0c186ae6d6 | 298 | } |
giovannivisentini | 14:ba0c186ae6d6 | 299 | |
giovannivisentini | 8:473f6e0b03df | 300 | |
giovannivisentini | 14:ba0c186ae6d6 | 301 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 302 | * function that the user will call when it read all the message |
giovannivisentini | 14:ba0c186ae6d6 | 303 | * @param internalState object that invoke the write operation |
giovannivisentini | 14:ba0c186ae6d6 | 304 | * @param status true if the operation had success |
giovannivisentini | 14:ba0c186ae6d6 | 305 | * @param buffer buffer read |
giovannivisentini | 14:ba0c186ae6d6 | 306 | * @param length number of byte read |
giovannivisentini | 14:ba0c186ae6d6 | 307 | * @return true if the read had success |
giovannivisentini | 14:ba0c186ae6d6 | 308 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 309 | static bool onReadMessageCallback(CallbackStatus_t *internalState, |
giovannivisentini | 14:ba0c186ae6d6 | 310 | bool status,const uint8_t *buffer, uint16_t length){ |
giovannivisentini | 14:ba0c186ae6d6 | 311 | if(!status){ |
giovannivisentini | 14:ba0c186ae6d6 | 312 | internalState->callOwner->mCallBack-> |
giovannivisentini | 14:ba0c186ae6d6 | 313 | onMessageRead(internalState->callOwner,false,internalState->msg); |
giovannivisentini | 14:ba0c186ae6d6 | 314 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 315 | } |
giovannivisentini | 14:ba0c186ae6d6 | 316 | Message::parseMessage(buffer, length, internalState->msg); |
giovannivisentini | 14:ba0c186ae6d6 | 317 | delete [] buffer; |
giovannivisentini | 14:ba0c186ae6d6 | 318 | internalState->callOwner->mCallBack-> |
giovannivisentini | 14:ba0c186ae6d6 | 319 | onMessageRead(internalState->callOwner,true,internalState->msg); |
giovannivisentini | 14:ba0c186ae6d6 | 320 | return status ; |
giovannivisentini | 14:ba0c186ae6d6 | 321 | } |
giovannivisentini | 8:473f6e0b03df | 322 | |
giovannivisentini | 14:ba0c186ae6d6 | 323 | }; //class NDefNfcTagASync |
giovannivisentini | 8:473f6e0b03df | 324 | |
giovannivisentini | 14:ba0c186ae6d6 | 325 | }// namespace NDefLib |
giovannivisentini | 14:ba0c186ae6d6 | 326 | |
giovannivisentini | 14:ba0c186ae6d6 | 327 | #endif /* NDEFLIB_NDEFNFCTAG_H_ */ |