From ffbb3d63b870923d4a6ff03eadaf42df88b0625c Mon Sep 17 00:00:00 2001 From: Michael Ihde Date: Sat, 22 Jul 2023 14:42:36 -0400 Subject: [PATCH 1/9] fix: IdentifyNDEF to properly set PayloadBufferAdd --- src/libNDEF/lib_NDEF.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libNDEF/lib_NDEF.cpp b/src/libNDEF/lib_NDEF.cpp index 682203c..e95bf6d 100755 --- a/src/libNDEF/lib_NDEF.cpp +++ b/src/libNDEF/lib_NDEF.cpp @@ -417,7 +417,7 @@ uint16_t NDEF::NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF) pRecordStruct->PayloadOffset = SizeOfRecordHeader; } - pRecordStruct->PayloadBufferAdd = pNDEF; + pRecordStruct->PayloadBufferAdd = &pNDEF[pRecordStruct->PayloadOffset]; NDEF_ParseRecordHeader(pRecordStruct); From 4f24ebce872ab3948217d5135d6e3e22c8e634af Mon Sep 17 00:00:00 2001 From: Michael Ihde Date: Sat, 22 Jul 2023 14:44:31 -0400 Subject: [PATCH 2/9] refact: have IdentifyNDEF use IdentifyBuffer to reduce duplicate code --- src/libNDEF/lib_NDEF.cpp | 59 +--------------------------------------- 1 file changed, 1 insertion(+), 58 deletions(-) diff --git a/src/libNDEF/lib_NDEF.cpp b/src/libNDEF/lib_NDEF.cpp index e95bf6d..2dfe373 100755 --- a/src/libNDEF/lib_NDEF.cpp +++ b/src/libNDEF/lib_NDEF.cpp @@ -364,64 +364,7 @@ uint16_t NDEF::NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF) /* Read the NDEF file */ NfcTag_ReadNDEF(pNDEF); - /* Is ID length field present */ - if ((*pNDEF) & IL_Mask) { - IDLengthField = ID_LENGTH_FIELD; - } else { - IDLengthField = 0; - } - - /* it's a SR */ - if ((*pNDEF) & SR_Mask) { - /* Analyse short record layout */ - TypeNbByte = pNDEF[1]; - PayloadLengthField = 1; - if (IDLengthField == ID_LENGTH_FIELD) { - IDNbByte = pNDEF[3]; - } else { - IDNbByte = 0; - } - } else { - /* Analyse normal record layout */ - TypeNbByte = pNDEF[1]; - PayloadLengthField = 4; - if (IDLengthField == ID_LENGTH_FIELD) { - IDNbByte = pNDEF[6]; - } else { - IDNbByte = 0; - } - } - - SizeOfRecordHeader = RECORD_FLAG_FIELD + TYPE_LENGTH_FIELD + PayloadLengthField + IDLengthField + TypeNbByte + IDNbByte; - - /* Read record header */ - /* it's a SR */ - if (pNDEF[0] & SR_Mask) { - pRecordStruct->RecordFlags = pNDEF[0]; - pRecordStruct->TypeLength = TypeNbByte; - pRecordStruct->PayloadLength = pNDEF[2]; - pRecordStruct->IDLength = IDNbByte; - memcpy(pRecordStruct->Type, &pNDEF[3 + IDNbByte], TypeNbByte); - memcpy(pRecordStruct->ID, &pNDEF[3 + IDNbByte + TypeNbByte], IDNbByte); - pRecordStruct->PayloadOffset = SizeOfRecordHeader; - } else { - pRecordStruct->RecordFlags = pNDEF[0]; - pRecordStruct->TypeLength = TypeNbByte; - pRecordStruct->PayloadLength = (((uint32_t)pNDEF[2]) << 24) | - (((uint32_t)pNDEF[3]) << 16) | - (((uint32_t)pNDEF[4]) << 8) - | pNDEF[5] ; - pRecordStruct->IDLength = IDNbByte; - memcpy(pRecordStruct->Type, &pNDEF[6 + IDNbByte], TypeNbByte); - memcpy(pRecordStruct->ID, &pNDEF[6 + IDNbByte + TypeNbByte], IDNbByte); - pRecordStruct->PayloadOffset = SizeOfRecordHeader; - } - - pRecordStruct->PayloadBufferAdd = &pNDEF[pRecordStruct->PayloadOffset]; - - NDEF_ParseRecordHeader(pRecordStruct); - - return NDEF_OK; + return NDEF_IdentifyBuffer(pRecordStruct, pNDEF); } /** From b5481adae5fccd651337fee04215f85cf7a2cfa3 Mon Sep 17 00:00:00 2001 From: Michael Ihde Date: Sat, 22 Jul 2023 14:45:18 -0400 Subject: [PATCH 3/9] fix: allow IdentifyNDEF to work with external buffer This allows IdentifyNDEF to work when the NDEF is larger than the default NDEF_MAX_SIZE --- src/libNDEF/NDEF_class.h | 3 +++ src/libNDEF/lib_NDEF.cpp | 28 ++++++++++++++++++++++++++-- src/libNDEF/tagtype5_wrapper.cpp | 16 +++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/libNDEF/NDEF_class.h b/src/libNDEF/NDEF_class.h index 1f39697..c1c984a 100755 --- a/src/libNDEF/NDEF_class.h +++ b/src/libNDEF/NDEF_class.h @@ -60,7 +60,9 @@ class NDEF { uint16_t begin(); //lib_NDEF + uint16_t NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct); uint16_t NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF); + uint16_t NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF, uint16_t bufferLength); uint16_t NDEF_IdentifyBuffer(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF); uint16_t NDEF_ReadNDEF(uint8_t *pNDEF); uint16_t NDEF_WriteNDEF(uint16_t NDEF_Size, uint8_t *pNDEF); @@ -131,6 +133,7 @@ class NDEF { //lib_wrapper uint16_t NfcTag_ReadNDEF(uint8_t *pData); + uint16_t NfcTag_ReadNDEF(uint8_t *pData, uint16_t MaxLength); uint16_t NfcTag_WriteNDEF(uint16_t Length, uint8_t *pData); uint16_t NfcTag_WriteProprietary(uint16_t Length, uint8_t *pData); uint16_t NfcTag_GetLength(uint16_t *Length); diff --git a/src/libNDEF/lib_NDEF.cpp b/src/libNDEF/lib_NDEF.cpp index 2dfe373..647112f 100755 --- a/src/libNDEF/lib_NDEF.cpp +++ b/src/libNDEF/lib_NDEF.cpp @@ -344,15 +344,39 @@ void NDEF::NDEF_ParseSP(sRecordInfo_t *pRecordStruct) * @{ */ +/** + * @brief This function identify the NDEF message stored in tag. + * @param pRecordStruct : Structure to fill with record information. + * @param pNDEF : pointer on the NDEF message data. + * @retval NDEF_OK : record struct filled. + * @retval NDEF_ERROR : record struct not updated. + */ +uint16_t NDEF::NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct) +{ + return NDEF_IdentifyNDEF(pRecordStruct, NDEF_Buffer, NDEF_Buffer_size); +} /** * @brief This function identify the NDEF message stored in tag. + * @deprecated use one-arg or three-arg variant * @param pRecordStruct : Structure to fill with record information. * @param pNDEF : pointer on the NDEF message data. * @retval NDEF_OK : record struct filled. * @retval NDEF_ERROR : record struct not updated. */ uint16_t NDEF::NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF) +{ + return NDEF_IdentifyNDEF(pRecordStruct, pNDEF, NDEF_MAX_SIZE); +} + +/** + * @brief This function identify the NDEF message stored in tag. + * @param pRecordStruct : Structure to fill with record information. + * @param pNDEF : pointer on the NDEF message data. + * @retval NDEF_OK : record struct filled. + * @retval NDEF_ERROR : record struct not updated. + */ +uint16_t NDEF::NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF, uint16_t bufferLength) { uint16_t SizeOfRecordHeader, TypeNbByte, PayloadLengthField, IDLengthField, IDNbByte; @@ -361,8 +385,8 @@ uint16_t NDEF::NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF) return NDEF_ERROR; } - /* Read the NDEF file */ - NfcTag_ReadNDEF(pNDEF); + /* Read the NDEF file up to the max length of the record header*/ + NfcTag_ReadNDEF(pNDEF, bufferLength); return NDEF_IdentifyBuffer(pRecordStruct, pNDEF); } diff --git a/src/libNDEF/tagtype5_wrapper.cpp b/src/libNDEF/tagtype5_wrapper.cpp index e7867ca..c366ccb 100755 --- a/src/libNDEF/tagtype5_wrapper.cpp +++ b/src/libNDEF/tagtype5_wrapper.cpp @@ -79,6 +79,19 @@ * @retval NDEF_OK NDEF message successfully read. */ uint16_t NDEF::NfcTag_ReadNDEF(uint8_t *pData) +{ + return NfcTag_ReadNDEF(pData, NDEF_MAX_SIZE); +} + +/** + * @brief This function reads the data stored in the NDEF message. + * @param pData Pointer on the buffer used to store the read data. + * @retval NDEF_ERROR_MEMORY_INTERNAL The buffer is too small for the NDEF message. + * @retval NDEF_ERROR_NOT_FORMATED No Capability Container detected. + * @retval NDEF_ERROR Error when reading the NDEF message. + * @retval NDEF_OK NDEF message successfully read. + */ +uint16_t NDEF::NfcTag_ReadNDEF(uint8_t *pData, uint16_t bufferLength) { uint16_t status = NDEF_ERROR; TT5_TLV_t tlv; @@ -105,8 +118,9 @@ uint16_t NDEF::NfcTag_ReadNDEF(uint8_t *pData) tlv_size = 2; DataLength = tlv.Length; } + /* If too many data to write return error */ - if (DataLength > NDEF_MAX_SIZE) { + if (DataLength > bufferLength) { return NDEF_ERROR_MEMORY_INTERNAL; } From 41bbf0dee33b53b4ee765809242b9b94755c5b90 Mon Sep 17 00:00:00 2001 From: Michael Ihde Date: Sat, 22 Jul 2023 15:57:00 -0400 Subject: [PATCH 4/9] fix: allow ndef buffer to be supplied by begin() The default buffer of 100 bytes is too small for Wifi and VCard. Further, using the default buffer prevents a user from picking a buffer size more suited to their processor environment and tag needs. This change retains backwards API compatibility but allows a user to provider their own buffer as part of begin() --- src/ST25DVSensor.cpp | 9 +++++++-- src/ST25DVSensor.h | 1 + src/libNDEF/NDEF_class.h | 7 +++++-- src/libNDEF/lib_NDEF.cpp | 14 ++++++++++++++ src/libNDEF/lib_NDEF.h | 2 +- 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/ST25DVSensor.cpp b/src/ST25DVSensor.cpp index d659feb..0df8860 100755 --- a/src/ST25DVSensor.cpp +++ b/src/ST25DVSensor.cpp @@ -20,6 +20,11 @@ #include "ST25DVSensor.h" int ST25DV::begin() +{ + return begin(NULL, 0); +} + +int ST25DV::begin(uint8_t* buffer, uint16_t bufferLength) { uint8_t nfctag_id = 0; @@ -39,13 +44,13 @@ int ST25DV::begin() return NFCTAG_ERROR; } - int ret = ndef.begin(); + int ret = ndef.begin(buffer, bufferLength); if (ret != NDEF_OK) { return ret; } } return NFCTAG_OK; -} +}; int ST25DV::writeURI(String protocol, String uri, String info) { diff --git a/src/ST25DVSensor.h b/src/ST25DVSensor.h index 6f31fd8..36f4a2e 100755 --- a/src/ST25DVSensor.h +++ b/src/ST25DVSensor.h @@ -35,6 +35,7 @@ class ST25DV { ST25DV(int32_t gpo, int32_t lpd, TwoWire *i2c, Stream *serial = NULL) : st25dv_io(gpo, lpd, i2c, serial), ndef(&st25dv_io) {} int begin(); + int begin(uint8_t* buffer, uint16_t bufferLength); int writeURI(String protocol, String uri, String info); int readURI(String *s); NDEF *getNDEF(); diff --git a/src/libNDEF/NDEF_class.h b/src/libNDEF/NDEF_class.h index c1c984a..0355bc1 100755 --- a/src/libNDEF/NDEF_class.h +++ b/src/libNDEF/NDEF_class.h @@ -58,6 +58,7 @@ class NDEF { NDEF(ST25DV_IO *dev); uint16_t begin(); + uint16_t begin(uint8_t* buffer, uint16_t bufferLength); //lib_NDEF uint16_t NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct); @@ -185,10 +186,12 @@ class NDEF { void NDEF_Read_WifiToken(struct sRecordInfo *pRecordStruct, sWifiTokenInfo *pWifiTokenStruct); //libNDEF.c + /** @brief This buffer is used if begin isn't called with a buffer. */ + uint8_t NDEF_Default_Buffer[NDEF_MAX_SIZE]; /** @brief This buffer is used to store the data sent/received by the TAG. */ - uint8_t NDEF_Buffer [NDEF_MAX_SIZE]; + uint8_t* NDEF_Buffer; /** @brief Size of the buffer used to build the NDEF messages. */ - uint32_t NDEF_Buffer_size = NDEF_MAX_SIZE; + uint32_t NDEF_Buffer_size; /** @brief This buffer is used when it's required to prepare a record before adding it to the NDEF_Buffer. */ uint8_t NDEF_Record_Buffer [NDEF_RECORD_MAX_SIZE]; /** @brief Size of the buffer used when a record has to be prepared. */ diff --git a/src/libNDEF/lib_NDEF.cpp b/src/libNDEF/lib_NDEF.cpp index 647112f..64b62d3 100755 --- a/src/libNDEF/lib_NDEF.cpp +++ b/src/libNDEF/lib_NDEF.cpp @@ -67,9 +67,23 @@ NDEF::NDEF(ST25DV_IO *dev) } uint16_t NDEF::begin() +{ + return begin(NULL, 0); +} + +uint16_t NDEF::begin(uint8_t* buffer, uint16_t bufferLength) { int ret = NDEF_OK; + if (buffer == NULL) { + NDEF_Buffer = NDEF_Default_Buffer; + NDEF_Buffer_size = NDEF_MAX_SIZE; + } else { + // TODO should we check minimum buffer length? + NDEF_Buffer = buffer; + NDEF_Buffer_size = bufferLength; + } + if (NfcType5_NDEFDetection() != NDEF_OK) { CCFileStruct.MagicNumber = NFCT5_MAGICNUMBER_E1_CCFILE; CCFileStruct.Version = NFCT5_VERSION_V1_0; diff --git a/src/libNDEF/lib_NDEF.h b/src/libNDEF/lib_NDEF.h index 49bd702..aa2f840 100755 --- a/src/libNDEF/lib_NDEF.h +++ b/src/libNDEF/lib_NDEF.h @@ -209,7 +209,7 @@ #define URI_ID_0x23_STRING "urn:nfc:\0" // exported variables -extern uint8_t NDEF_Buffer[NDEF_MAX_SIZE]; +extern uint8_t* NDEF_Buffer; extern uint32_t NDEF_Buffer_size; extern uint8_t NDEF_Record_Buffer [NDEF_RECORD_MAX_SIZE]; extern uint32_t NDEF_Record_Buffer_size; From 0f8dc3bef54568a354c05814d2b022edcbc3c68b Mon Sep 17 00:00:00 2001 From: Michael Ihde Date: Sat, 22 Jul 2023 16:00:39 -0400 Subject: [PATCH 5/9] refact: ReadNDEF to allow using common buffer --- src/libNDEF/NDEF_class.h | 2 ++ src/libNDEF/lib_NDEF.cpp | 30 +++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/libNDEF/NDEF_class.h b/src/libNDEF/NDEF_class.h index 0355bc1..ba271b9 100755 --- a/src/libNDEF/NDEF_class.h +++ b/src/libNDEF/NDEF_class.h @@ -65,7 +65,9 @@ class NDEF { uint16_t NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF); uint16_t NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF, uint16_t bufferLength); uint16_t NDEF_IdentifyBuffer(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF); + uint16_t NDEF_ReadNDEF(); uint16_t NDEF_ReadNDEF(uint8_t *pNDEF); + uint16_t NDEF_ReadNDEF(uint8_t *pNDEF, uint16_t bufferLength); uint16_t NDEF_WriteNDEF(uint16_t NDEF_Size, uint8_t *pNDEF); uint16_t NDEF_ClearNDEF(void); uint16_t NDEF_getNDEFSize(uint16_t *Size); diff --git a/src/libNDEF/lib_NDEF.cpp b/src/libNDEF/lib_NDEF.cpp index 64b62d3..f434ac0 100755 --- a/src/libNDEF/lib_NDEF.cpp +++ b/src/libNDEF/lib_NDEF.cpp @@ -405,6 +405,20 @@ uint16_t NDEF::NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF, u return NDEF_IdentifyBuffer(pRecordStruct, pNDEF); } +/** + * @brief This function read the NDEF content of the TAG. + * @retval NDEF_OK : NDEF file data retrieve and store in the buffer. + * @retval NDEF_ERROR : not able to read NDEF from tag. + * @retval NDEF_ERROR_MEMORY_INTERNAL : Cannot read tag. + * @retval NDEF_ERROR_NOT_FORMATED : CCFile data not supported or not present. + * @retval NDEF_ERROR_MEMORY_TAG : Size not compatible with memory. + * @retval NDEF_ERROR_LOCKED : Tag locked, cannot be read. + */ +uint16_t NDEF::NDEF_ReadNDEF() +{ + return NfcTag_ReadNDEF(NDEF_Buffer, NDEF_Buffer_size); +} + /** * @brief This function read the NDEF content of the TAG. * @param pNDEF : pointer on the buffer to store NDEF data. @@ -417,9 +431,23 @@ uint16_t NDEF::NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct, uint8_t *pNDEF, u */ uint16_t NDEF::NDEF_ReadNDEF(uint8_t *pNDEF) { - return NfcTag_ReadNDEF(pNDEF); + return NfcTag_ReadNDEF(pNDEF, NDEF_MAX_SIZE); } +/** + * @brief This function read the NDEF content of the TAG. + * @param pNDEF : pointer on the buffer to store NDEF data. + * @retval NDEF_OK : NDEF file data retrieve and store in the buffer. + * @retval NDEF_ERROR : not able to read NDEF from tag. + * @retval NDEF_ERROR_MEMORY_INTERNAL : Cannot read tag. + * @retval NDEF_ERROR_NOT_FORMATED : CCFile data not supported or not present. + * @retval NDEF_ERROR_MEMORY_TAG : Size not compatible with memory. + * @retval NDEF_ERROR_LOCKED : Tag locked, cannot be read. + */ +uint16_t NDEF::NDEF_ReadNDEF(uint8_t *pNDEF, uint16_t bufferLength) +{ + return NfcTag_ReadNDEF(pNDEF, bufferLength); +} /** From 269d2519816eb33a2937994603c66e81ed503eda Mon Sep 17 00:00:00 2001 From: Michael Ihde Date: Sat, 22 Jul 2023 16:01:00 -0400 Subject: [PATCH 6/9] refact: ReadURI to use common buffer --- src/ST25DVSensor.cpp | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/ST25DVSensor.cpp b/src/ST25DVSensor.cpp index 0df8860..b70fc58 100755 --- a/src/ST25DVSensor.cpp +++ b/src/ST25DVSensor.cpp @@ -67,15 +67,8 @@ int ST25DV::readURI(String *s) uint16_t ret; sURI_Info uri = {"", "", ""}; sRecordInfo_t recordInfo; - uint8_t NDEF_Buffer[100]; - // increase buffer size for bigger messages - ret = ndef.NDEF_ReadNDEF(NDEF_Buffer); - if (ret) { - return ret; - } - - ret = ndef.NDEF_IdentifyBuffer(&recordInfo, NDEF_Buffer); + ret = ndef.NDEF_IdentifyNDEF(&recordInfo); if (ret) { return ret; } From 2d5ae4c8cc78293aadb5f06830bfc54e502fe647 Mon Sep 17 00:00:00 2001 From: Michael Ihde Date: Wed, 26 Jul 2023 22:43:16 -0400 Subject: [PATCH 7/9] feat: add support for high level API on ST25DVSensor Add the following features: * The ability to read/write unabridged URIs per NDEF specifcation * High-level API for SMS, GEO, and EMail * Each API to read NDEF type --- src/ST25DVSensor.cpp | 211 ++++++++++++++++++++++++++++++++++- src/ST25DVSensor.h | 9 ++ src/libNDEF/lib_NDEF.cpp | 2 +- src/libNDEF/lib_NDEF.h | 1 + src/libNDEF/lib_NDEF_URI.cpp | 5 +- 5 files changed, 225 insertions(+), 3 deletions(-) diff --git a/src/ST25DVSensor.cpp b/src/ST25DVSensor.cpp index b70fc58..60f0e7d 100755 --- a/src/ST25DVSensor.cpp +++ b/src/ST25DVSensor.cpp @@ -55,6 +55,13 @@ int ST25DV::begin(uint8_t* buffer, uint16_t bufferLength) int ST25DV::writeURI(String protocol, String uri, String info) { sURI_Info _URI; + + // Unabridged protocols must be written using + // `writeUnabridgedURI()` + if (protocol.equals("")) { + return NDEF_ERROR; + } + strcpy(_URI.protocol, protocol.c_str()); strcpy(_URI.URI_Message, uri.c_str()); strcpy(_URI.Information, info.c_str()); @@ -82,10 +89,212 @@ int ST25DV::readURI(String *s) return 0; } +/* + * @brief Writes an unabbrieved URI + * + * The NFC NDEF format uses URI identifer code 0x00 + * to indicate a URI that is not abbreviated. + * + * @param uri the uri to write + * @param info to write + * @retval success or failure + */ +int ST25DV::writeUnabridgedURI(String uri, String info) +{ + sURI_Info _URI; + + strcpy(_URI.protocol, ""); + strcpy(_URI.URI_Message, uri.c_str()); + strcpy(_URI.Information, info.c_str()); + + return ndef.NDEF_WriteURI(&_URI); +} + +/* + * @brief Reads an unabbrieved URI + * @param s the uri read + * @retval success or failure + */ +int ST25DV::readUnabridgedURI(String *s) +{ + uint16_t ret; + sURI_Info uri = {"", "", ""}; + sRecordInfo_t recordInfo; + + ret = ndef.NDEF_IdentifyNDEF(&recordInfo); + if (ret) { + return ret; + } + + ret = ndef.NDEF_ReadURI(&recordInfo, &uri); + if (ret) { + return ret; + } + + // If the URI is abbreivated return error + if (strncmp("", uri.protocol, 1) != 0) { + return ret; //NDEF_ERROR; + } + + *s = String(uri.URI_Message); + + return 0; +} + +/* + * @brief Writes an SMS record + * + * @param phoneNumber + * @param message + * @param info + * @retval success or failure + */ +int ST25DV::writeSMS(String phoneNumber, String message, String info) +{ + sSMSInfo _SMS; + + strncpy(_SMS.PhoneNumber, phoneNumber.c_str(), 16); + strncpy(_SMS.Message, message.c_str(), 400); + strncpy(_SMS.Information, info.c_str(), 400); + + return ndef.NDEF_WriteSMS(&_SMS); +} + +/* + * @brief Reads an SMS record + * + * @param phoneNumber + * @param message + * @retval success or failure + */ +int ST25DV::readSMS(String *phoneNumber, String *message) +{ + uint16_t ret; + sSMSInfo _SMS; + sRecordInfo_t recordInfo; + + ret = ndef.NDEF_IdentifyNDEF(&recordInfo); + if (ret) { + return ret; + } + + ret = ndef.NDEF_ReadSMS(&recordInfo, &_SMS); + if (ret) { + return ret; + } + + *phoneNumber = String(_SMS.PhoneNumber); + *message = String(_SMS.Message); + + return NDEF_OK; +} + +/* + * @brief Writes a GEO record + * + * @param latitude + * @param longitude + * @param info + * @retval success or failure + */ +int ST25DV::writeGEO(String latitude, String longitude, String info) +{ + sGeoInfo _GEO; + + strncpy(_GEO.Latitude, latitude.c_str(), 20); + strncpy(_GEO.Longitude, longitude.c_str(), 20); + strncpy(_GEO.Information, info.c_str(), 100); + + return ndef.NDEF_WriteGeo(&_GEO); +} + +/* + * @brief Reads a GEO record + * + * @param latitude + * @param longitude + * @retval success or failure + */ +int ST25DV::readGEO(String *latitude, String *longitude) +{ + uint16_t ret; + sGeoInfo _GEO; + sRecordInfo_t recordInfo; + + ret = ndef.NDEF_IdentifyNDEF(&recordInfo); + if (ret) { + return ret; + } + + ret = ndef.NDEF_ReadGeo(&recordInfo, &_GEO); + if (ret) { + return ret; + } + + *latitude = String(_GEO.Latitude); + *longitude = String(_GEO.Longitude); + + return NDEF_OK; +} + + +int ST25DV::writeEMail(String emailAdd, String subject, String message, String info) +{ + sEmailInfo _EMAIL; + + strncpy(_EMAIL.EmailAdd, emailAdd.c_str(), 64); + strncpy(_EMAIL.Subject, subject.c_str(), 100); + strncpy(_EMAIL.Message, message.c_str(), 2000); + strncpy(_EMAIL.Information, info.c_str(), 400); + + return ndef.NDEF_WriteEmail(&_EMAIL); +} + +int ST25DV::readEMail(String *emailAdd, String *subject, String *message) +{ + uint16_t ret; + sEmailInfo _EMAIL; + sRecordInfo_t recordInfo; + + ret = ndef.NDEF_IdentifyNDEF(&recordInfo); + if (ret) { + return ret; + } + + ret = ndef.NDEF_ReadEmail(&recordInfo, &_EMAIL); + if (ret) { + return ret; + } + + *emailAdd = String(_EMAIL.EmailAdd); + *subject = String(_EMAIL.Subject); + *message = String(_EMAIL.Message); + + return NDEF_OK; +} + +/** + * @brief reads the type of NDEF on the tag + * @param None + * @retval the type or UNKNOWN_TYPE if errors occur + */ +NDEF_TypeDef ST25DV::readNDEFType(void) +{ + uint16_t ret; + sRecordInfo_t recordInfo; + + ret = ndef.NDEF_IdentifyNDEF(&recordInfo); + if (ret) { + return UNKNOWN_TYPE; + } + + return recordInfo.NDEF_Type; +} + /** * @brief Returns the NDEF class instance used by the component * @param None - * @retval NDEF class + * @retval success or failure */ NDEF *ST25DV::getNDEF(void) { diff --git a/src/ST25DVSensor.h b/src/ST25DVSensor.h index 36f4a2e..d8a799f 100755 --- a/src/ST25DVSensor.h +++ b/src/ST25DVSensor.h @@ -38,6 +38,15 @@ class ST25DV { int begin(uint8_t* buffer, uint16_t bufferLength); int writeURI(String protocol, String uri, String info); int readURI(String *s); + int writeUnabridgedURI(String uri, String info); + int readUnabridgedURI(String *s); + int writeSMS(String phoneNumber, String message, String info); + int readSMS(String *phoneNumber, String *message); + int writeGEO(String latitude, String longitude, String info); + int readGEO(String *latitude, String *longitude); + int writeEMail(String emailAdd, String subject, String message, String info); + int readEMail(String *emailAdd, String *subject, String *message); + NDEF_TypeDef readNDEFType(); NDEF *getNDEF(); protected: diff --git a/src/libNDEF/lib_NDEF.cpp b/src/libNDEF/lib_NDEF.cpp index f434ac0..e1116e9 100755 --- a/src/libNDEF/lib_NDEF.cpp +++ b/src/libNDEF/lib_NDEF.cpp @@ -303,7 +303,7 @@ void NDEF::NDEF_ParseURI(sRecordInfo_t *pRecordStruct) } else if (!memcmp(pPayload, GEO_TYPE_STRING, strlen(GEO_TYPE_STRING))) { pRecordStruct->NDEF_Type = URI_GEO_TYPE; } else { - pRecordStruct->NDEF_Type = UNKNOWN_TYPE; + pRecordStruct->NDEF_Type = UNABRIDGED_URI_TYPE; } } diff --git a/src/libNDEF/lib_NDEF.h b/src/libNDEF/lib_NDEF.h index aa2f840..543e105 100755 --- a/src/libNDEF/lib_NDEF.h +++ b/src/libNDEF/lib_NDEF.h @@ -217,6 +217,7 @@ extern uint32_t NDEF_Record_Buffer_size; typedef enum { UNKNOWN_TYPE = 0, VCARD_TYPE, + UNABRIDGED_URI_TYPE, WELL_KNOWN_ABRIDGED_URI_TYPE, URI_SMS_TYPE, URI_GEO_TYPE, diff --git a/src/libNDEF/lib_NDEF_URI.cpp b/src/libNDEF/lib_NDEF_URI.cpp index bf77b9b..4e7c796 100755 --- a/src/libNDEF/lib_NDEF_URI.cpp +++ b/src/libNDEF/lib_NDEF_URI.cpp @@ -283,7 +283,10 @@ uint16_t NDEF::NDEF_ReadURI(sRecordInfo_t *pRecordStruct, sURI_Info *pURI) uint32_t PayloadSize, RecordPosition; uint8_t *pData; - if (pRecordStruct->NDEF_Type == WELL_KNOWN_ABRIDGED_URI_TYPE) { + if (pRecordStruct->NDEF_Type == UNABRIDGED_URI_TYPE) { + NDEF_Parse_WellKnowType(pRecordStruct, pURI); + status = NDEF_OK; + } else if (pRecordStruct->NDEF_Type == WELL_KNOWN_ABRIDGED_URI_TYPE) { NDEF_Parse_WellKnowType(pRecordStruct, pURI); status = NDEF_OK; } else if (pRecordStruct->NDEF_Type == SMARTPOSTER_TYPE) { From 12c9a6b8090dfdaadf77bdb2f3e22f3744a6901e Mon Sep 17 00:00:00 2001 From: Michael Ihde Date: Fri, 28 Jul 2023 17:08:46 -0400 Subject: [PATCH 8/9] fix: spelling error --- src/ST25DVSensor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ST25DVSensor.cpp b/src/ST25DVSensor.cpp index 60f0e7d..40f4ca7 100755 --- a/src/ST25DVSensor.cpp +++ b/src/ST25DVSensor.cpp @@ -92,7 +92,7 @@ int ST25DV::readURI(String *s) /* * @brief Writes an unabbrieved URI * - * The NFC NDEF format uses URI identifer code 0x00 + * The NFC NDEF format uses URI identifier code 0x00 * to indicate a URI that is not abbreviated. * * @param uri the uri to write From 0aa72e1cb663661d7d6ab1fa3d23f305ec444d98 Mon Sep 17 00:00:00 2001 From: Michael Ihde Date: Fri, 28 Jul 2023 17:12:43 -0400 Subject: [PATCH 9/9] fix: astyle errors --- src/ST25DVSensor.cpp | 16 ++++++++-------- src/ST25DVSensor.h | 2 +- src/libNDEF/NDEF_class.h | 4 ++-- src/libNDEF/lib_NDEF.cpp | 2 +- src/libNDEF/lib_NDEF.h | 2 +- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/ST25DVSensor.cpp b/src/ST25DVSensor.cpp index 40f4ca7..d7ca549 100755 --- a/src/ST25DVSensor.cpp +++ b/src/ST25DVSensor.cpp @@ -24,7 +24,7 @@ int ST25DV::begin() return begin(NULL, 0); } -int ST25DV::begin(uint8_t* buffer, uint16_t bufferLength) +int ST25DV::begin(uint8_t *buffer, uint16_t bufferLength) { uint8_t nfctag_id = 0; @@ -91,10 +91,10 @@ int ST25DV::readURI(String *s) /* * @brief Writes an unabbrieved URI - * + * * The NFC NDEF format uses URI identifier code 0x00 * to indicate a URI that is not abbreviated. - * + * * @param uri the uri to write * @param info to write * @retval success or failure @@ -102,7 +102,7 @@ int ST25DV::readURI(String *s) int ST25DV::writeUnabridgedURI(String uri, String info) { sURI_Info _URI; - + strcpy(_URI.protocol, ""); strcpy(_URI.URI_Message, uri.c_str()); strcpy(_URI.Information, info.c_str()); @@ -143,7 +143,7 @@ int ST25DV::readUnabridgedURI(String *s) /* * @brief Writes an SMS record - * + * * @param phoneNumber * @param message * @param info @@ -162,7 +162,7 @@ int ST25DV::writeSMS(String phoneNumber, String message, String info) /* * @brief Reads an SMS record - * + * * @param phoneNumber * @param message * @retval success or failure @@ -191,7 +191,7 @@ int ST25DV::readSMS(String *phoneNumber, String *message) /* * @brief Writes a GEO record - * + * * @param latitude * @param longitude * @param info @@ -210,7 +210,7 @@ int ST25DV::writeGEO(String latitude, String longitude, String info) /* * @brief Reads a GEO record - * + * * @param latitude * @param longitude * @retval success or failure diff --git a/src/ST25DVSensor.h b/src/ST25DVSensor.h index d8a799f..bf6300c 100755 --- a/src/ST25DVSensor.h +++ b/src/ST25DVSensor.h @@ -35,7 +35,7 @@ class ST25DV { ST25DV(int32_t gpo, int32_t lpd, TwoWire *i2c, Stream *serial = NULL) : st25dv_io(gpo, lpd, i2c, serial), ndef(&st25dv_io) {} int begin(); - int begin(uint8_t* buffer, uint16_t bufferLength); + int begin(uint8_t *buffer, uint16_t bufferLength); int writeURI(String protocol, String uri, String info); int readURI(String *s); int writeUnabridgedURI(String uri, String info); diff --git a/src/libNDEF/NDEF_class.h b/src/libNDEF/NDEF_class.h index ba271b9..021b2a0 100755 --- a/src/libNDEF/NDEF_class.h +++ b/src/libNDEF/NDEF_class.h @@ -58,7 +58,7 @@ class NDEF { NDEF(ST25DV_IO *dev); uint16_t begin(); - uint16_t begin(uint8_t* buffer, uint16_t bufferLength); + uint16_t begin(uint8_t *buffer, uint16_t bufferLength); //lib_NDEF uint16_t NDEF_IdentifyNDEF(sRecordInfo_t *pRecordStruct); @@ -191,7 +191,7 @@ class NDEF { /** @brief This buffer is used if begin isn't called with a buffer. */ uint8_t NDEF_Default_Buffer[NDEF_MAX_SIZE]; /** @brief This buffer is used to store the data sent/received by the TAG. */ - uint8_t* NDEF_Buffer; + uint8_t *NDEF_Buffer; /** @brief Size of the buffer used to build the NDEF messages. */ uint32_t NDEF_Buffer_size; /** @brief This buffer is used when it's required to prepare a record before adding it to the NDEF_Buffer. */ diff --git a/src/libNDEF/lib_NDEF.cpp b/src/libNDEF/lib_NDEF.cpp index e1116e9..e115924 100755 --- a/src/libNDEF/lib_NDEF.cpp +++ b/src/libNDEF/lib_NDEF.cpp @@ -71,7 +71,7 @@ uint16_t NDEF::begin() return begin(NULL, 0); } -uint16_t NDEF::begin(uint8_t* buffer, uint16_t bufferLength) +uint16_t NDEF::begin(uint8_t *buffer, uint16_t bufferLength) { int ret = NDEF_OK; diff --git a/src/libNDEF/lib_NDEF.h b/src/libNDEF/lib_NDEF.h index 543e105..4e46045 100755 --- a/src/libNDEF/lib_NDEF.h +++ b/src/libNDEF/lib_NDEF.h @@ -209,7 +209,7 @@ #define URI_ID_0x23_STRING "urn:nfc:\0" // exported variables -extern uint8_t* NDEF_Buffer; +extern uint8_t *NDEF_Buffer; extern uint32_t NDEF_Buffer_size; extern uint8_t NDEF_Record_Buffer [NDEF_RECORD_MAX_SIZE]; extern uint32_t NDEF_Record_Buffer_size;