STMicroelectronics' M24SR NFC Dynamic Tag Library.
Dependencies: ST_INTERFACES
Fork of M24SR by
NDefNfcTagM24SR.cpp
00001 /** 00002 ****************************************************************************** 00003 * @file NdefNfcTagSTM24SR.cpp 00004 * @author ST Central Labs 00005 * @version V2.0.0 00006 * @date 28 Apr 2017 00007 * @brief Wrapper class of the NDefLib library to write/read NDEF messages 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> 00012 * 00013 * Redistribution and use in source and binary forms, with or without modification, 00014 * are permitted provided that the following conditions are met: 00015 * 1. Redistributions of source code must retain the above copyright notice, 00016 * this list of conditions and the following disclaimer. 00017 * 2. Redistributions in binary form must reproduce the above copyright notice, 00018 * this list of conditions and the following disclaimer in the documentation 00019 * and/or other materials provided with the distribution. 00020 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00021 * may be used to endorse or promote products derived from this software 00022 * without specific prior written permission. 00023 * 00024 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00025 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00026 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00028 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00029 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00030 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00031 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00032 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00033 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 ****************************************************************************** 00036 */ 00037 00038 #include <cmath> 00039 00040 #include "NDefNfcTagM24SR.h" 00041 00042 00043 /* wait 1sec, driver is configured to let 200ms for command to complete */ 00044 /* which is enough for all commands except GetSession if RF session is already opened */ 00045 /* Smartphone generally releases the session within the second, anyway the user can modify this value */ 00046 #define OPENSESSION_NTRIALS 5 00047 00048 #define CC_FILE_LENGTH_BYTE 15 00049 ////////////////////////////START OpenSessionCallBack///////////////////////// 00050 NDefNfcTagM24SR::OpenSessionCallBack:: 00051 OpenSessionCallBack(NDefNfcTagM24SR& sender): 00052 mSender(sender), mNTrials(OPENSESSION_NTRIALS){} 00053 00054 void NDefNfcTagM24SR::OpenSessionCallBack::on_session_open(M24SR *nfc, M24SR::StatusTypeDef status) { 00055 if (status==M24SR::M24SR_SUCCESS) { 00056 nfc->select_application(); 00057 } else { 00058 mSender.mCallBack->on_session_open(&mSender,false); 00059 } 00060 } 00061 00062 void NDefNfcTagM24SR::OpenSessionCallBack::on_selected_application(M24SR *nfc, M24SR::StatusTypeDef status) { 00063 if (status==M24SR::M24SR_SUCCESS) { 00064 nfc->select_CC_file(); 00065 } else { 00066 if (mNTrials==0) { 00067 mSender.mCallBack->on_session_open(&mSender,false); 00068 } else { 00069 mNTrials--; 00070 nfc->select_application(); 00071 }//if-else 00072 }//if-else 00073 } 00074 00075 void NDefNfcTagM24SR::OpenSessionCallBack::on_selected_CC_file(M24SR *nfc, M24SR::StatusTypeDef status) { 00076 if (status==M24SR::M24SR_SUCCESS) { 00077 nfc->read_binary(0x0000, CC_FILE_LENGTH_BYTE, CCFile); 00078 } else { 00079 mSender.mCallBack->on_session_open(&mSender,false); 00080 } 00081 } 00082 00083 void NDefNfcTagM24SR::OpenSessionCallBack::on_read_byte(M24SR *nfc, M24SR::StatusTypeDef status, uint16_t offset, uint8_t *readByte, uint16_t nReadByte) { 00084 (void)offset; 00085 if (status!=M24SR::M24SR_SUCCESS || nReadByte!=CC_FILE_LENGTH_BYTE) { 00086 return mSender.mCallBack->on_session_open(&mSender,false); 00087 }//else 00088 uint16_t NDefFileId = (uint16_t) ((readByte[0x09] << 8) | readByte[0x0A]); 00089 mSender.mMaxReadBytes = (uint16_t) ((readByte[0x03] << 8) | readByte[0x04]); 00090 mSender.mMaxWriteBytes = (uint16_t) ((readByte[0x05] << 8) | readByte[0x06]); 00091 nfc->select_NDEF_file(NDefFileId); 00092 } 00093 00094 void NDefNfcTagM24SR::OpenSessionCallBack::on_selected_NDEF_file(M24SR *nfc, M24SR::StatusTypeDef status){ 00095 (void)nfc; 00096 00097 mSender.mIsSessionOpen = status==M24SR::M24SR_SUCCESS; 00098 mSender.mCallBack->on_session_open(&mSender,mSender.mIsSessionOpen); 00099 } 00100 ////////////////////////////END OpenSessionCallBack///////////////////////// 00101 00102 bool NDefNfcTagM24SR::open_session(bool force) { 00103 00104 if (is_session_open()) { 00105 mCallBack->on_session_open(this,true); 00106 return true; 00107 } 00108 00109 mDevice.set_callback(&mOpenSessionCallback); 00110 if (force) { 00111 return mDevice.force_get_session() == M24SR::M24SR_SUCCESS; 00112 } else { 00113 return mDevice.get_session() == M24SR::M24SR_SUCCESS; 00114 } 00115 } 00116 00117 bool NDefNfcTagM24SR::close_session() { 00118 mDevice.set_callback(&mCloseSessionCallback); 00119 return mDevice.deselect() == M24SR::M24SR_SUCCESS; 00120 } 00121 00122 void NDefNfcTagM24SR::WriteByteCallback::on_updated_binary(M24SR *nfc, 00123 M24SR::StatusTypeDef status,uint16_t startOffset, uint8_t *writeByte,uint16_t nWriteByte){ 00124 00125 if (status!=M24SR::M24SR_SUCCESS){ // error -> finish to write 00126 mCallback(mCallbackParam,false,mByteToWrite,mNByteToWrite); 00127 return; 00128 }//else 00129 00130 mByteWrote+=nWriteByte; 00131 if (mByteWrote==mNByteToWrite) { //write all -> finish 00132 mCallback(mCallbackParam,true,mByteToWrite,mNByteToWrite); 00133 } else { //else write another slice 00134 uint16_t tempLenght = std::min(mSender.mMaxWriteBytes, (uint16_t)(mNByteToWrite-mByteWrote)); 00135 nfc->update_binary(startOffset+nWriteByte,tempLenght,writeByte+nWriteByte); 00136 }//if-else 00137 } 00138 00139 bool NDefNfcTagM24SR::writeByte(const uint8_t *buffer, uint16_t length,uint16_t offset, 00140 byteOperationCallback_t callback,CallbackStatus_t *callbackStatus){ 00141 if (!is_session_open()) 00142 callback(callbackStatus,false,buffer,length); 00143 //else 00144 mWriteByteCallback.set_task(buffer,length,callback,callbackStatus); 00145 mDevice.set_callback(&mWriteByteCallback); 00146 00147 if (length > mMaxWriteBytes) { 00148 return mDevice.update_binary(offset, mMaxWriteBytes,(uint8_t*) buffer) == M24SR::M24SR_SUCCESS; 00149 } else { 00150 return mDevice.update_binary(offset,length,(uint8_t*)buffer) == M24SR::M24SR_SUCCESS; 00151 }//if-else 00152 } 00153 00154 void NDefNfcTagM24SR::ReadByteCallback::on_read_byte(M24SR *nfc, 00155 M24SR::StatusTypeDef status,uint16_t startOffset, uint8_t *readBffer,uint16_t nReadByte){ 00156 00157 if (status!=M24SR::M24SR_SUCCESS) { // error -> finish to write 00158 mCallback(mCallbackParam,false,mBuffer,mNByteToRead); 00159 return; 00160 }//else 00161 00162 mByteRead += nReadByte; 00163 if (mByteRead==mNByteToRead) { //read all -> finish 00164 mCallback(mCallbackParam,true,mBuffer,mNByteToRead); 00165 } else { //else write another slice 00166 uint16_t tempLenght = std::min(mSender.mMaxReadBytes, (uint16_t)(mNByteToRead-mByteRead)); 00167 nfc->read_binary(startOffset+nReadByte,tempLenght,readBffer+nReadByte); 00168 }//if-else 00169 } 00170 00171 bool NDefNfcTagM24SR::readByte(const uint16_t byteOffset, const uint16_t length, 00172 uint8_t *buffer, byteOperationCallback_t callback,CallbackStatus_t *callbackStatus) { 00173 if (!is_session_open()) { 00174 return callback(callbackStatus,false,buffer,length); 00175 } 00176 //else 00177 mReadByteCallback.set_task(buffer,length,callback,callbackStatus); 00178 mDevice.set_callback(&mReadByteCallback); 00179 00180 if (length > mMaxReadBytes) { 00181 return mDevice.read_binary(byteOffset, mMaxReadBytes,buffer)== M24SR::M24SR_SUCCESS;; 00182 } else { 00183 return mDevice.read_binary(byteOffset,length,buffer)== M24SR::M24SR_SUCCESS;; 00184 }//if-else 00185 }
Generated on Wed Jul 13 2022 05:23:50 by
1.7.2
