Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: support using ReadURI with SMS and GEO type #30

Merged
merged 9 commits into from
Jul 31, 2023
223 changes: 215 additions & 8 deletions src/ST25DVSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -39,17 +44,24 @@ 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)
{
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());
Expand All @@ -62,15 +74,54 @@ 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);
ret = ndef.NDEF_IdentifyNDEF(&recordInfo);
if (ret) {
return ret;
}

ret = ndef.NDEF_IdentifyBuffer(&recordInfo, NDEF_Buffer);
ret = ndef.NDEF_ReadURI(&recordInfo, &uri);
if (ret) {
return ret;
}
*s = String(uri.protocol) + String(uri.URI_Message);

return 0;
}

/*
* @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
*/
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;
}
Expand All @@ -79,15 +130,171 @@ int ST25DV::readURI(String *s)
if (ret) {
return ret;
}
*s = String(uri.protocol) + String(uri.URI_Message);

// 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)
{
Expand Down
10 changes: 10 additions & 0 deletions src/ST25DVSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,18 @@ 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);
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:
Expand Down
12 changes: 10 additions & 2 deletions src/libNDEF/NDEF_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ 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);
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);
Expand Down Expand Up @@ -131,6 +136,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);
Expand Down Expand Up @@ -182,10 +188,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. */
Expand Down
Loading