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@20:31f727872290, 2017-07-12 (annotated)
- Committer:
- Davidroid
- Date:
- Wed Jul 12 12:33:42 2017 +0000
- Revision:
- 20:31f727872290
- Parent:
- 19:13d84b136a62
- Child:
- 21:72c86cbd49be
Updated to fit ARM mbed coding style.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giovannivisentini | 8:473f6e0b03df | 1 | /** |
giovannivisentini | 8:473f6e0b03df | 2 | ****************************************************************************** |
giovannivisentini | 15:01fc5a4b8366 | 3 | * @file NDefNfcTag.h |
giovannivisentini | 8:473f6e0b03df | 4 | * @author ST / Central Labs |
giovannivisentini | 19:13d84b136a62 | 5 | * @version V2.0.0 |
giovannivisentini | 19:13d84b136a62 | 6 | * @date 28 Apr 2017 |
giovannivisentini | 16:030e7ffdf512 | 7 | * @brief Generic interface that a device must implement to 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 | 16:030e7ffdf512 | 48 | * This class is made to handle also asynchronous communication with the nfc component. |
giovannivisentini | 16:030e7ffdf512 | 49 | * All the functions in this class will return immediately, when a command ends 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 | 16:030e7ffdf512 | 57 | * Class that contains all the function called when a command finished. |
giovannivisentini | 14:ba0c186ae6d6 | 58 | * The default implementation is an empty function. |
giovannivisentini | 14:ba0c186ae6d6 | 59 | */ |
giovannivisentini | 16:030e7ffdf512 | 60 | class Callbacks { |
giovannivisentini | 14:ba0c186ae6d6 | 61 | public: |
giovannivisentini | 14:ba0c186ae6d6 | 62 | |
giovannivisentini | 14:ba0c186ae6d6 | 63 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 64 | * Called when a session is open. |
giovannivisentini | 16:030e7ffdf512 | 65 | * @param tag Tag where the session is open. |
giovannivisentini | 16:030e7ffdf512 | 66 | * @param success True if the operation has success. |
giovannivisentini | 14:ba0c186ae6d6 | 67 | */ |
giovannivisentini | 19:13d84b136a62 | 68 | virtual void on_session_open(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 | 16:030e7ffdf512 | 73 | * Called when a message is written. |
giovannivisentini | 16:030e7ffdf512 | 74 | * @param tag Tag where the message is written. |
giovannivisentini | 16:030e7ffdf512 | 75 | * @param success True if the operation has success. |
giovannivisentini | 14:ba0c186ae6d6 | 76 | */ |
giovannivisentini | 19:13d84b136a62 | 77 | virtual void on_message_write(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 | 16:030e7ffdf512 | 85 | * @param tag Tag where the message is read. |
giovannivisentini | 16:030e7ffdf512 | 86 | * @param success True if the operation has success. |
giovannivisentini | 14:ba0c186ae6d6 | 87 | */ |
giovannivisentini | 19:13d84b136a62 | 88 | virtual void on_message_read(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 | 16:030e7ffdf512 | 95 | * Called when a session is closed. |
giovannivisentini | 16:030e7ffdf512 | 96 | * @param tag Tag where the session is closed. |
giovannivisentini | 16:030e7ffdf512 | 97 | * @param success True if the operation has success. |
giovannivisentini | 14:ba0c186ae6d6 | 98 | */ |
giovannivisentini | 19:13d84b136a62 | 99 | virtual void on_session_close(NDefNfcTag *tag,bool success){ |
giovannivisentini | 14:ba0c186ae6d6 | 100 | (void)tag;(void)success; |
giovannivisentini | 14:ba0c186ae6d6 | 101 | }; |
giovannivisentini | 14:ba0c186ae6d6 | 102 | |
giovannivisentini | 16:030e7ffdf512 | 103 | virtual ~Callbacks(){}; |
giovannivisentini | 14:ba0c186ae6d6 | 104 | }; |
giovannivisentini | 14:ba0c186ae6d6 | 105 | |
giovannivisentini | 14:ba0c186ae6d6 | 106 | private: |
giovannivisentini | 14:ba0c186ae6d6 | 107 | |
giovannivisentini | 14:ba0c186ae6d6 | 108 | /** |
giovannivisentini | 16:030e7ffdf512 | 109 | * Data used to store the callback status during a read/write operation. |
giovannivisentini | 14:ba0c186ae6d6 | 110 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 111 | struct CallbackStatus{ |
giovannivisentini | 16:030e7ffdf512 | 112 | |
giovannivisentini | 16:030e7ffdf512 | 113 | /** Object that triggers the callback */ |
giovannivisentini | 14:ba0c186ae6d6 | 114 | NDefNfcTag *callOwner; |
giovannivisentini | 16:030e7ffdf512 | 115 | |
giovannivisentini | 16:030e7ffdf512 | 116 | /** Message that the callback is writing/reading */ |
giovannivisentini | 14:ba0c186ae6d6 | 117 | Message *msg; |
giovannivisentini | 14:ba0c186ae6d6 | 118 | }; |
giovannivisentini | 14:ba0c186ae6d6 | 119 | |
giovannivisentini | 14:ba0c186ae6d6 | 120 | public: |
giovannivisentini | 14:ba0c186ae6d6 | 121 | |
giovannivisentini | 14:ba0c186ae6d6 | 122 | NDefNfcTag():mCallBack(&mDefaultCallBack){} |
giovannivisentini | 14:ba0c186ae6d6 | 123 | |
giovannivisentini | 14:ba0c186ae6d6 | 124 | /** |
giovannivisentini | 16:030e7ffdf512 | 125 | * Set the callback object. |
giovannivisentini | 16:030e7ffdf512 | 126 | * @param c Object containing the callback. |
giovannivisentini | 14:ba0c186ae6d6 | 127 | */ |
giovannivisentini | 19:13d84b136a62 | 128 | void set_callback(Callbacks *c){ |
Davidroid | 20:31f727872290 | 129 | if (c!=NULL) { |
giovannivisentini | 16:030e7ffdf512 | 130 | mCallBack=c; |
Davidroid | 20:31f727872290 | 131 | } else { |
giovannivisentini | 16:030e7ffdf512 | 132 | mCallBack=&mDefaultCallBack; |
Davidroid | 20:31f727872290 | 133 | } |
giovannivisentini | 14:ba0c186ae6d6 | 134 | }//setCallBack |
giovannivisentini | 14:ba0c186ae6d6 | 135 | |
giovannivisentini | 8:473f6e0b03df | 136 | |
giovannivisentini | 8:473f6e0b03df | 137 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 138 | * Open the communication session with the nfc tag. |
giovannivisentini | 12:ed4d9b8d1410 | 139 | * @param force Force to open a communication. |
giovannivisentini | 8:473f6e0b03df | 140 | * @return true if success |
giovannivisentini | 8:473f6e0b03df | 141 | */ |
giovannivisentini | 19:13d84b136a62 | 142 | virtual bool open_session(bool force = false)=0; |
giovannivisentini | 8:473f6e0b03df | 143 | |
giovannivisentini | 8:473f6e0b03df | 144 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 145 | * Close the communication with the nfc tag. |
giovannivisentini | 8:473f6e0b03df | 146 | * @return true if success |
giovannivisentini | 8:473f6e0b03df | 147 | */ |
giovannivisentini | 19:13d84b136a62 | 148 | virtual bool close_session()=0; |
giovannivisentini | 14:ba0c186ae6d6 | 149 | |
giovannivisentini | 14:ba0c186ae6d6 | 150 | /** |
giovannivisentini | 14:ba0c186ae6d6 | 151 | * Returns true if a communication with the nfc tag is open. |
giovannivisentini | 14:ba0c186ae6d6 | 152 | * @return true if a communication with the nfc tag is open |
giovannivisentini | 14:ba0c186ae6d6 | 153 | */ |
giovannivisentini | 19:13d84b136a62 | 154 | virtual bool is_session_open()=0; |
giovannivisentini | 8:473f6e0b03df | 155 | |
giovannivisentini | 8:473f6e0b03df | 156 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 157 | * Write a message in the nfc tag. |
giovannivisentini | 12:ed4d9b8d1410 | 158 | * @par This call will delete the previous message. |
giovannivisentini | 12:ed4d9b8d1410 | 159 | * @param msg Message to write. |
giovannivisentini | 8:473f6e0b03df | 160 | * @return true if success |
giovannivisentini | 8:473f6e0b03df | 161 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 162 | virtual bool write(Message &msg) { |
Davidroid | 20:31f727872290 | 163 | if (!is_session_open()) { |
giovannivisentini | 19:13d84b136a62 | 164 | mCallBack->on_message_write(this,false,msg); |
giovannivisentini | 8:473f6e0b03df | 165 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 166 | } |
giovannivisentini | 8:473f6e0b03df | 167 | |
giovannivisentini | 19:13d84b136a62 | 168 | const uint16_t length = msg.get_byte_length(); |
giovannivisentini | 8:473f6e0b03df | 169 | uint8_t *buffer = new uint8_t[length]; |
Davidroid | 20:31f727872290 | 170 | if (buffer==NULL){ //impossible to allocate the buffer |
giovannivisentini | 19:13d84b136a62 | 171 | mCallBack->on_message_write(this,false,msg); |
giovannivisentini | 8:473f6e0b03df | 172 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 173 | } |
giovannivisentini | 14:ba0c186ae6d6 | 174 | |
giovannivisentini | 8:473f6e0b03df | 175 | msg.write(buffer); |
giovannivisentini | 14:ba0c186ae6d6 | 176 | |
giovannivisentini | 14:ba0c186ae6d6 | 177 | mCallBackStatus.callOwner=this; |
giovannivisentini | 14:ba0c186ae6d6 | 178 | mCallBackStatus.msg=&msg; |
giovannivisentini | 14:ba0c186ae6d6 | 179 | |
giovannivisentini | 14:ba0c186ae6d6 | 180 | return writeByte(buffer, length,0,NDefNfcTag::onWriteMessageCallback,&mCallBackStatus); |
giovannivisentini | 8:473f6e0b03df | 181 | } |
giovannivisentini | 8:473f6e0b03df | 182 | |
giovannivisentini | 8:473f6e0b03df | 183 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 184 | * Read a message from the tag. |
giovannivisentini | 12:ed4d9b8d1410 | 185 | * @param[in,out] msg Message object the read records are added to. |
giovannivisentini | 8:473f6e0b03df | 186 | * @return true if success |
giovannivisentini | 8:473f6e0b03df | 187 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 188 | virtual bool read(Message *msg) { |
Davidroid | 20:31f727872290 | 189 | if (!is_session_open()) { |
giovannivisentini | 19:13d84b136a62 | 190 | mCallBack->on_message_read(this,false,msg); |
giovannivisentini | 8:473f6e0b03df | 191 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 192 | } |
giovannivisentini | 8:473f6e0b03df | 193 | |
giovannivisentini | 14:ba0c186ae6d6 | 194 | uint8_t *buffer = new uint8_t[2]; |
Davidroid | 20:31f727872290 | 195 | if (buffer==NULL) { |
giovannivisentini | 19:13d84b136a62 | 196 | mCallBack->on_message_read(this,false,msg); |
giovannivisentini | 14:ba0c186ae6d6 | 197 | return false; |
giovannivisentini | 8:473f6e0b03df | 198 | } |
giovannivisentini | 14:ba0c186ae6d6 | 199 | |
giovannivisentini | 14:ba0c186ae6d6 | 200 | mCallBackStatus.callOwner=this; |
giovannivisentini | 14:ba0c186ae6d6 | 201 | mCallBackStatus.msg=msg; |
giovannivisentini | 14:ba0c186ae6d6 | 202 | return readByte(0,2,buffer,NDefNfcTag::onReadMessageLength,&mCallBackStatus); |
giovannivisentini | 8:473f6e0b03df | 203 | } |
giovannivisentini | 8:473f6e0b03df | 204 | |
giovannivisentini | 8:473f6e0b03df | 205 | virtual ~NDefNfcTag() {} |
giovannivisentini | 8:473f6e0b03df | 206 | |
giovannivisentini | 14:ba0c186ae6d6 | 207 | protected: |
giovannivisentini | 14:ba0c186ae6d6 | 208 | |
giovannivisentini | 14:ba0c186ae6d6 | 209 | typedef struct CallbackStatus CallbackStatus_t; |
giovannivisentini | 14:ba0c186ae6d6 | 210 | |
giovannivisentini | 8:473f6e0b03df | 211 | /** |
giovannivisentini | 16:030e7ffdf512 | 212 | * Function that the component will call when a read/write operation is completed |
giovannivisentini | 16:030e7ffdf512 | 213 | * @param internalState Callback internal state data. |
giovannivisentini | 16:030e7ffdf512 | 214 | * @param status True if the operation succeed. |
giovannivisentini | 16:030e7ffdf512 | 215 | * @param buffer Buffer to read/write. |
giovannivisentini | 16:030e7ffdf512 | 216 | * @param length Number of byte read/write. |
giovannivisentini | 14:ba0c186ae6d6 | 217 | * @return true if the operation had success |
giovannivisentini | 8:473f6e0b03df | 218 | */ |
giovannivisentini | 16:030e7ffdf512 | 219 | typedef bool(*byteOperationCallback_t)(CallbackStatus_t *internalState, |
Davidroid | 20:31f727872290 | 220 | bool status,const uint8_t *buffer, uint16_t length); |
giovannivisentini | 8:473f6e0b03df | 221 | |
giovannivisentini | 8:473f6e0b03df | 222 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 223 | * Write a sequence of bytes to the NDEF file. |
giovannivisentini | 16:030e7ffdf512 | 224 | * @param buffer Buffer to write. |
giovannivisentini | 16:030e7ffdf512 | 225 | * @param length Number of bytes to write. |
giovannivisentini | 16:030e7ffdf512 | 226 | * @param offset Offset where start to write. |
giovannivisentini | 16:030e7ffdf512 | 227 | * @param callback Function to call when the operation ended. |
giovannivisentini | 16:030e7ffdf512 | 228 | * @param callbackStatus Parameter to pass to the callback function. |
giovannivisentini | 14:ba0c186ae6d6 | 229 | * @return true if the operation has success |
giovannivisentini | 8:473f6e0b03df | 230 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 231 | virtual bool writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset, |
Davidroid | 20:31f727872290 | 232 | byteOperationCallback_t callback,CallbackStatus_t *callbackStatus)=0; |
giovannivisentini | 8:473f6e0b03df | 233 | |
giovannivisentini | 8:473f6e0b03df | 234 | /** |
giovannivisentini | 12:ed4d9b8d1410 | 235 | * Read a sequence of bytes from the NDEF file. |
giovannivisentini | 14:ba0c186ae6d6 | 236 | * @param byteOffset Read offset in bytes. |
giovannivisentini | 12:ed4d9b8d1410 | 237 | * @param byteLength Number of bytes to read. |
giovannivisentini | 12:ed4d9b8d1410 | 238 | * @param[out] buffer Buffer to store the read data into. |
giovannivisentini | 16:030e7ffdf512 | 239 | * @param callback Function to call when the operation ended. |
giovannivisentini | 16:030e7ffdf512 | 240 | * @param callbackStatus Parameter to pass to the callback function. |
giovannivisentini | 16:030e7ffdf512 | 241 | * @return true if the operation has success |
giovannivisentini | 8:473f6e0b03df | 242 | */ |
giovannivisentini | 8:473f6e0b03df | 243 | virtual bool readByte(const uint16_t byteOffset, const uint16_t byteLength, |
Davidroid | 20:31f727872290 | 244 | uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus)=0; |
giovannivisentini | 14:ba0c186ae6d6 | 245 | |
giovannivisentini | 8:473f6e0b03df | 246 | |
giovannivisentini | 14:ba0c186ae6d6 | 247 | /** object with the user callback */ |
giovannivisentini | 16:030e7ffdf512 | 248 | Callbacks *mCallBack; |
giovannivisentini | 8:473f6e0b03df | 249 | private: |
giovannivisentini | 12:ed4d9b8d1410 | 250 | |
giovannivisentini | 14:ba0c186ae6d6 | 251 | /** object with the current callback status */ |
giovannivisentini | 14:ba0c186ae6d6 | 252 | CallbackStatus_t mCallBackStatus; |
giovannivisentini | 16:030e7ffdf512 | 253 | /** default callback object, all the functions are empty */ |
giovannivisentini | 16:030e7ffdf512 | 254 | Callbacks mDefaultCallBack; |
giovannivisentini | 14:ba0c186ae6d6 | 255 | |
giovannivisentini | 8:473f6e0b03df | 256 | /** |
giovannivisentini | 19:13d84b136a62 | 257 | * Function called when a write operation completes, it will invoke on_message_write |
giovannivisentini | 16:030e7ffdf512 | 258 | * @param internalState Object that invokes the write operation. |
giovannivisentini | 15:01fc5a4b8366 | 259 | * @param status True if the operation had success. |
giovannivisentini | 16:030e7ffdf512 | 260 | * @param buffer Buffer written. |
giovannivisentini | 16:030e7ffdf512 | 261 | * @param length Number of bytes written. |
giovannivisentini | 14:ba0c186ae6d6 | 262 | * @return true if the write had success |
giovannivisentini | 8:473f6e0b03df | 263 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 264 | static bool onWriteMessageCallback(CallbackStatus_t *internalState, |
Davidroid | 20:31f727872290 | 265 | bool status,const uint8_t *buffer, uint16_t ){ |
giovannivisentini | 14:ba0c186ae6d6 | 266 | delete [] buffer; |
giovannivisentini | 14:ba0c186ae6d6 | 267 | |
Davidroid | 20:31f727872290 | 268 | internalState->callOwner->mCallBack->on_message_write(internalState->callOwner,status,*internalState->msg); |
Davidroid | 20:31f727872290 | 269 | |
giovannivisentini | 14:ba0c186ae6d6 | 270 | return status; |
giovannivisentini | 14:ba0c186ae6d6 | 271 | } |
giovannivisentini | 8:473f6e0b03df | 272 | |
giovannivisentini | 8:473f6e0b03df | 273 | /** |
giovannivisentini | 16:030e7ffdf512 | 274 | * Function called when a read operation completes. |
giovannivisentini | 16:030e7ffdf512 | 275 | * In this case we read the message length, this function will read all the message |
giovannivisentini | 16:030e7ffdf512 | 276 | * @param internalState Object that invokes the write operation. |
giovannivisentini | 15:01fc5a4b8366 | 277 | * @param status true If the operation had success. |
giovannivisentini | 15:01fc5a4b8366 | 278 | * @param buffer Buffer read. |
giovannivisentini | 16:030e7ffdf512 | 279 | * @param length Number of bytes read. |
giovannivisentini | 14:ba0c186ae6d6 | 280 | * @return true if the read had success |
giovannivisentini | 8:473f6e0b03df | 281 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 282 | static bool onReadMessageLength(CallbackStatus_t *internalState, |
Davidroid | 20:31f727872290 | 283 | bool status,const uint8_t *buffer, uint16_t length){ |
giovannivisentini | 14:ba0c186ae6d6 | 284 | |
Davidroid | 20:31f727872290 | 285 | if (!status || length!=2) { |
Davidroid | 20:31f727872290 | 286 | internalState->callOwner->mCallBack->on_message_read(internalState->callOwner,false,internalState->msg); |
giovannivisentini | 14:ba0c186ae6d6 | 287 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 288 | }//if |
giovannivisentini | 14:ba0c186ae6d6 | 289 | |
giovannivisentini | 14:ba0c186ae6d6 | 290 | length = (((uint16_t) buffer[0]) << 8 | buffer[1]); |
giovannivisentini | 14:ba0c186ae6d6 | 291 | delete [] buffer; |
giovannivisentini | 14:ba0c186ae6d6 | 292 | |
giovannivisentini | 14:ba0c186ae6d6 | 293 | uint8_t *readBuffer = new uint8_t[length]; |
Davidroid | 20:31f727872290 | 294 | if (readBuffer==NULL) { |
Davidroid | 20:31f727872290 | 295 | internalState->callOwner->mCallBack->on_message_read(internalState->callOwner,false,internalState->msg); |
giovannivisentini | 14:ba0c186ae6d6 | 296 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 297 | }//readBuffer |
giovannivisentini | 14:ba0c186ae6d6 | 298 | |
Davidroid | 20:31f727872290 | 299 | internalState->callOwner->readByte(2,length,readBuffer, &NDefNfcTag::onReadMessageCallback,internalState); |
giovannivisentini | 14:ba0c186ae6d6 | 300 | return status; |
giovannivisentini | 14:ba0c186ae6d6 | 301 | } |
giovannivisentini | 14:ba0c186ae6d6 | 302 | |
giovannivisentini | 14:ba0c186ae6d6 | 303 | /** |
giovannivisentini | 16:030e7ffdf512 | 304 | * Function called when all messages have been read |
giovannivisentini | 16:030e7ffdf512 | 305 | * @param internalState Object that invokes the write operation. |
giovannivisentini | 15:01fc5a4b8366 | 306 | * @param status True if the operation had success. |
giovannivisentini | 15:01fc5a4b8366 | 307 | * @param buffer Buffer read. |
giovannivisentini | 16:030e7ffdf512 | 308 | * @param length Number of bytes read. |
giovannivisentini | 14:ba0c186ae6d6 | 309 | * @return true if the read had success |
giovannivisentini | 14:ba0c186ae6d6 | 310 | */ |
giovannivisentini | 14:ba0c186ae6d6 | 311 | static bool onReadMessageCallback(CallbackStatus_t *internalState, |
Davidroid | 20:31f727872290 | 312 | bool status,const uint8_t *buffer, uint16_t length){ |
Davidroid | 20:31f727872290 | 313 | if(!status) { |
Davidroid | 20:31f727872290 | 314 | internalState->callOwner->mCallBack->on_message_read(internalState->callOwner,false,internalState->msg); |
giovannivisentini | 14:ba0c186ae6d6 | 315 | return false; |
giovannivisentini | 14:ba0c186ae6d6 | 316 | } |
giovannivisentini | 19:13d84b136a62 | 317 | Message::parse_message(buffer, length, internalState->msg); |
giovannivisentini | 14:ba0c186ae6d6 | 318 | delete [] buffer; |
Davidroid | 20:31f727872290 | 319 | internalState->callOwner->mCallBack-> on_message_read(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_ */ |
Davidroid | 20:31f727872290 | 328 | |
Davidroid | 20:31f727872290 | 329 | |
Davidroid | 20:31f727872290 | 330 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |