Skip to content

Commit

Permalink
refactor: add Field::UriPayload
Browse files Browse the repository at this point in the history
  • Loading branch information
pmuller committed Feb 1, 2024
1 parent 9d7f965 commit 7b4e3df
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 250 deletions.
2 changes: 2 additions & 0 deletions src/ArduinoNDEF/field/payload.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include "../field.hpp"

namespace ArduinoNDEF
Expand Down
76 changes: 29 additions & 47 deletions src/ArduinoNDEF/uri.cpp → src/ArduinoNDEF/field/uri_payload.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
#include "uri.hpp"
#include "uri_payload.hpp"

#include "macros.hpp"

#include <string.h>
#include "../macros.hpp"

namespace ArduinoNDEF
{
namespace Field
{

/**
* @brief NDEF URI identifier codes
* @ref NFCForum-TS-RTD_URI_1.0.pdf - Table 3. Abbreviation Table
*/
#define NDEF_URI_CODE_IDENTIFIER_COUNT 36
struct NdefUriPrefix
#define ARDUINONDEF_FIELD_URI_PAYLOAD_CODE_IDENTIFIER_COUNT 36
struct UriAbbreviation
{
uint8_t code;
const char *prefix;
};
static const NdefUriPrefix
NDEF_URI_IDENTIFIER_CODE_IDENTIFIERS[NDEF_URI_CODE_IDENTIFIER_COUNT] = {
static const UriAbbreviation ARDUINONDEF_FIELD_URI_PAYLOAD_IDENTIFIER_CODE_IDENTIFIERS
[ARDUINONDEF_FIELD_URI_PAYLOAD_CODE_IDENTIFIER_COUNT] = {
{0x00, ""},
{0x01, "http://www."},
{0x02, "https://www."},
Expand Down Expand Up @@ -57,23 +57,32 @@ static const NdefUriPrefix
{0x23, "urn:nfc:"},
};

NdefUriPayload::NdefUriPayload(const char *uri)
UriPayload *UriPayload::from_uri(const char *uri)
{
if (uri == nullptr)
return nullptr;

size_t uri_length = strlen(uri);

// 1 byte URIs obviously can't be valid,
// but I'm not sure what the minimum length should be
if (uri_length < 1)
return nullptr;

const char *prefix;
uint8_t prefix_length;
uint8_t uri_offset = 0;
uint8_t code = 0x00; // No prefix

// Check if the URI starts with a known prefix
for (uint8_t i = 1; i < NDEF_URI_CODE_IDENTIFIER_COUNT; i++)
for (uint8_t i = 1; i < ARDUINONDEF_FIELD_URI_PAYLOAD_CODE_IDENTIFIER_COUNT; i++)
{
prefix = NDEF_URI_IDENTIFIER_CODE_IDENTIFIERS[i].prefix;
prefix = ARDUINONDEF_FIELD_URI_PAYLOAD_IDENTIFIER_CODE_IDENTIFIERS[i].prefix;
prefix_length = strlen(prefix);

if (strncmp(uri, prefix, prefix_length) == 0)
{
code = NDEF_URI_IDENTIFIER_CODE_IDENTIFIERS[i].code;
code = ARDUINONDEF_FIELD_URI_PAYLOAD_IDENTIFIER_CODE_IDENTIFIERS[i].code;
uri_length -= prefix_length;
uri_offset = prefix_length;
break;
Expand All @@ -85,54 +94,26 @@ NdefUriPayload::NdefUriPayload(const char *uri)
uint8_t *data = new uint8_t[length];

if (data == nullptr)
return;
return nullptr;

data[0] = code;
memcpy(data + 1, &uri[uri_offset], uri_length);

_data = data;
_length = length;
return new UriPayload(data, length);
}

bool NdefUriPayload::is_valid() const
const char *UriPayload::to_uri() const
{
// Check if the data is valid
if (_data == nullptr || _length < 2)
return false;

// Check if the identifier code is valid
uint8_t code = _data[0];

for (uint8_t i = 0; i < NDEF_URI_CODE_IDENTIFIER_COUNT; i++)
if (code == NDEF_URI_IDENTIFIER_CODE_IDENTIFIERS[i].code)
return true;

return false;
}

uint8_t NdefUriPayload::code() const
{
if (!is_valid())
return 0;

return _data[0];
}

const char *NdefUriPayload::uri() const
{
if (!is_valid())
return nullptr;

const char *prefix = nullptr;
uint8_t prefix_length = 0;
char *uri = nullptr;
size_t uri_length = 0;
uint8_t code_ = code();
uint8_t code_ = _data[0];

for (uint8_t i = 0; i < NDEF_URI_CODE_IDENTIFIER_COUNT; i++)
if (code_ == NDEF_URI_IDENTIFIER_CODE_IDENTIFIERS[i].code)
for (uint8_t i = 0; i < ARDUINONDEF_FIELD_URI_PAYLOAD_CODE_IDENTIFIER_COUNT; i++)
if (code_ == ARDUINONDEF_FIELD_URI_PAYLOAD_IDENTIFIER_CODE_IDENTIFIERS[i].code)
{
prefix = NDEF_URI_IDENTIFIER_CODE_IDENTIFIERS[i].prefix;
prefix = ARDUINONDEF_FIELD_URI_PAYLOAD_IDENTIFIER_CODE_IDENTIFIERS[i].prefix;
prefix_length = strlen(prefix);
break;
}
Expand All @@ -154,4 +135,5 @@ const char *NdefUriPayload::uri() const
return uri;
}

} // namespace Field
} // namespace ArduinoNDEF
36 changes: 36 additions & 0 deletions src/ArduinoNDEF/field/uri_payload.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once

#include "payload.hpp"

namespace ArduinoNDEF
{
namespace Field
{

class UriPayload : public Payload
{
public:
/**
* @brief Create a NDEF URI payload from an URI C-string
* @param uri The URI C-string
* @return A pointer to a new NDEF URI payload object
*/
static UriPayload *from_uri(const char *uri);

/**
* @brief Get the URI
* @return The URI C-string
* @note Memory is allocated for the URI string and it is the responsibility of the
* caller to free it
*/
const char *to_uri() const;

private:
UriPayload(const uint8_t *data, uint8_t length) :
Payload(data, length, OwnershipUnique)
{
}
};

} // namespace Field
} // namespace ArduinoNDEF
15 changes: 9 additions & 6 deletions src/ArduinoNDEF/record/uri.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "../uri.hpp"

#include "uri.hpp"

#include "../field/uri_payload.hpp"

namespace ArduinoNDEF
{
namespace Record
Expand All @@ -11,15 +11,18 @@ Uri *Uri::create(
const char *uri, bool is_message_begin, bool is_message_end, const Field::Id &id
)
{
NdefUriPayload payload(uri);
if (!payload.is_valid())
auto payload_field = Field::UriPayload::from_uri(uri);

if (payload_field == nullptr)
return nullptr;

auto payload_field = new Field::Payload(payload.data(), payload.length());
auto type_field = new Field::Type(Field::Type::RTD_URI);

if (payload_field == nullptr || type_field == nullptr)
if (type_field == nullptr)
{
delete payload_field;
return nullptr;
}

return new Uri(*type_field, *payload_field, is_message_begin, is_message_end, id);
}
Expand Down
3 changes: 2 additions & 1 deletion src/ArduinoNDEF/record/uri.hpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "../field/uri_payload.hpp"
#include "../record.hpp"

namespace ArduinoNDEF
Expand Down Expand Up @@ -50,7 +51,7 @@ class Uri : public Record

private:
Uri(const Field::Type &type,
const Field::Payload &payload,
const Field::UriPayload &payload,
bool is_message_begin,
bool is_message_end,
const Field::Id &id) :
Expand Down
78 changes: 0 additions & 78 deletions src/ArduinoNDEF/uri.hpp

This file was deleted.

Loading

0 comments on commit 7b4e3df

Please sign in to comment.