NFC: Integrate nfc targets with Span

The goal is to play nicely with the NDEF message framework.
pull/7822/head
Vincent Coubard 2018-08-24 16:18:27 +01:00 committed by Donatien Garnier
parent dcf38eec50
commit b985b2c00b
12 changed files with 36 additions and 32 deletions

View File

@ -26,6 +26,8 @@
#include "NFCDefinitions.h"
#include "NFCControllerDriver.h"
#include "platform/Span.h"
namespace mbed {
namespace nfc {
@ -93,9 +95,8 @@ public:
* @param[in] driver a pointer to a NFCControllerDriver instance
* @param[in] queue a pointer to the events queue to use
* @param[in] ndef_buffer a bytes array used to store NDEF messages
* @param[in] ndef_buffer_sz the array size in bytes
*/
NFCController(NFCControllerDriver *driver, events::EventQueue *queue, uint8_t *ndef_buffer, size_t ndef_buffer_sz);
NFCController(NFCControllerDriver *driver, events::EventQueue *queue, const Span<uint8_t> &ndef_buffer);
/**
* Initialize the NFC controller
@ -174,8 +175,7 @@ private:
Timeout _timeout;
Delegate *_delegate;
bool _discovery_running;
uint8_t *_ndef_buffer;
size_t _ndef_buffer_sz;
Span<uint8_t> _ndef_buffer;
};
/**

View File

@ -51,9 +51,8 @@ public:
* @param[in] driver a pointer to a NFCEEPROMDriver instance
* @param[in] queue a pointer to the events queue to use
* @param[in] ndef_buffer a bytes array used to store NDEF messages
* @param[in] ndef_buffer_sz the array size in bytes
*/
NFCEEPROM(NFCEEPROMDriver *driver, events::EventQueue *queue, uint8_t *ndef_buffer, size_t ndef_buffer_sz);
NFCEEPROM(NFCEEPROMDriver *driver, events::EventQueue *queue, const Span<uint8_t> &ndef_buffer);
/**
* The NFCEEPROM delegate. Users of the NFCEEPROM class need to implement this delegate's methods to receive events.

View File

@ -19,6 +19,8 @@
#include <stdint.h>
#include "platform/Span.h"
#include "NFCDefinitions.h"
#include "nfc/stack/ndef/ndef.h"
@ -42,9 +44,8 @@ public:
/**
* Construct a NFCNDEFCapable instance.
* @param[in] buffer a bytes array used to store NDEF messages
* @param[in] buffer_size the array size in bytes
*/
NFCNDEFCapable(uint8_t *buffer, size_t buffer_size);
NFCNDEFCapable(const Span<uint8_t>& buffer);
/**
* Check if this instance actually supports NDEF content.
@ -61,18 +62,17 @@ public:
* Parse a NDEF message.
*
* @param[in] buffer a buffer containing the message to parse
* @param[in] size the buffer's size
*/
virtual void parse_ndef_message(const uint8_t *buffer, size_t size) { }
virtual void parse_ndef_message(const Span<const uint8_t> &buffer) { }
/**
* Build a NDEF message.
*
* @param[in] buffer a mutable buffer in which the message should be stored
* @param[in] capacity the buffer's capacity
*
* @return the number of bytes actually used
*/
virtual size_t build_ndef_message(uint8_t *buffer, size_t capacity)
virtual size_t build_ndef_message(const Span<uint8_t> &buffer)
{
return 0;
}

View File

@ -24,6 +24,8 @@
#include "NFCNDEFCapable.h"
#include "stack/tech/iso7816/iso7816_app.h"
#include "platform/Span.h"
namespace mbed {
namespace nfc {
@ -47,7 +49,7 @@ public:
* @param[in] buffer a bytes array used to store NDEF messages
* @param[in] buffer_size the array size in bytes
*/
NFCRemoteInitiator(NFCController *controller, uint8_t *buffer, size_t buffer_size);
NFCRemoteInitiator(NFCController *controller, const Span<uint8_t>& buffer);
virtual ~NFCRemoteInitiator();
/**

View File

@ -22,6 +22,8 @@
#include "NFCDefinitions.h"
#include "NFCNDEFCapable.h"
#include "platform/Span.h"
namespace mbed {
namespace nfc {
@ -41,9 +43,8 @@ public:
* Create a NFCTarget.
*
* @param[in] buffer a bytes array used to store NDEF messages
* @param[in] buffer_size the array size in bytes
*/
NFCTarget(uint8_t *buffer, size_t buffer_size);
NFCTarget(const Span<uint8_t> &buffer);
/**
* NFCTarget destructor

View File

@ -28,6 +28,8 @@
#include "nfc/acore/acore/ac_buffer_builder.h"
#include "nfc/stack/tech/type4/type4_target.h"
#include "platform/Span.h"
namespace mbed {
namespace nfc {
@ -48,7 +50,7 @@ public:
* @param[in] buffer a bytes array used to store NDEF messages
* @param[in] buffer_size the array size in bytes
*/
Type4RemoteInitiator(NFCController *controller, uint8_t *buffer, size_t buffer_size);
Type4RemoteInitiator(NFCController *controller, const Span<uint8_t> &buffer);
/**
* Type4RemoteInitiator destructor.

View File

@ -23,8 +23,8 @@
using namespace mbed;
using namespace mbed::nfc;
NFCController::NFCController(NFCControllerDriver *driver, events::EventQueue *queue, uint8_t *ndef_buffer, size_t ndef_buffer_sz) :
_driver(driver), _queue(queue), _transceiver(NULL), _scheduler(NULL), _delegate(NULL), _discovery_running(false), _ndef_buffer(ndef_buffer), _ndef_buffer_sz(ndef_buffer_sz)
NFCController::NFCController(NFCControllerDriver *driver, events::EventQueue *queue, const Span<uint8_t> &ndef_buffer) :
_driver(driver), _queue(queue), _transceiver(NULL), _scheduler(NULL), _delegate(NULL), _discovery_running(false), _ndef_buffer(ndef_buffer)
{
_driver->set_delegate(this);
}
@ -146,7 +146,7 @@ void NFCController::polling_callback(nfc_err_t ret)
if (!transceiver_is_initiator_mode(_transceiver)) {
nfc_tech_t active_tech = transceiver_get_active_techs(_transceiver);
if (active_tech.nfc_iso_dep_a || active_tech.nfc_iso_dep_b) {
Type4RemoteInitiator *type4_remote_initiator_ptr = new (std::nothrow) Type4RemoteInitiator(this, _ndef_buffer, _ndef_buffer_sz);
Type4RemoteInitiator *type4_remote_initiator_ptr = new (std::nothrow) Type4RemoteInitiator(this, _ndef_buffer);
if (type4_remote_initiator_ptr != NULL) {
SharedPtr<NFCRemoteInitiator> type4_remote_initiator(type4_remote_initiator_ptr);
if (_delegate != NULL) {

View File

@ -20,7 +20,7 @@
using namespace mbed;
using namespace mbed::nfc;
NFCEEPROM::NFCEEPROM(NFCEEPROMDriver *driver, events::EventQueue *queue, uint8_t *ndef_buffer, size_t ndef_buffer_sz) : NFCTarget(ndef_buffer, ndef_buffer_sz),
NFCEEPROM::NFCEEPROM(NFCEEPROMDriver *driver, events::EventQueue *queue, const Span<uint8_t> &ndef_buffer) : NFCTarget(ndef_buffer),
_delegate(NULL), _driver(driver), _initialized(false), _current_op(nfc_eeprom_idle), _ndef_buffer_read_sz(0), _eeprom_address(0), _operation_result(NFC_ERR_UNKNOWN)
{
_driver->set_delegate(this);

View File

@ -25,9 +25,9 @@
using namespace mbed;
using namespace mbed::nfc;
NFCNDEFCapable::NFCNDEFCapable(uint8_t *buffer, size_t buffer_size) : _delegate(NULL)
NFCNDEFCapable::NFCNDEFCapable(const Span<uint8_t>& buffer) : _delegate(NULL)
{
ndef_msg_init(&_ndef_message, s_ndef_encode, s_ndef_decode, buffer, buffer_size, this);
ndef_msg_init(&_ndef_message, s_ndef_encode, s_ndef_decode, buffer.data(), buffer.size(), this);
}
void NFCNDEFCapable::set_ndef_delegate(NFCNDEFCapable::Delegate *delegate)
@ -40,14 +40,14 @@ void NFCNDEFCapable::parse_ndef_message(const ac_buffer_t &buffer)
ac_buffer_t reader;
ac_buffer_dup(&reader, &buffer);
if (_delegate != NULL) {
_delegate->parse_ndef_message(ac_buffer_reader_current_buffer_pointer(&reader), ac_buffer_reader_current_buffer_length(&reader));
_delegate->parse_ndef_message(make_const_Span(ac_buffer_reader_current_buffer_pointer(&reader), ac_buffer_reader_current_buffer_length(&reader)));
}
}
void NFCNDEFCapable::build_ndef_message(ac_buffer_builder_t &buffer_builder)
{
if (_delegate != NULL) {
size_t count = _delegate->build_ndef_message(ac_buffer_builder_write_position(&buffer_builder), ac_buffer_builder_writable(&buffer_builder));
size_t count = _delegate->build_ndef_message(make_Span(ac_buffer_builder_write_position(&buffer_builder), ac_buffer_builder_writable(&buffer_builder)));
ac_buffer_builder_write_n_skip(&buffer_builder, count);
}
}

View File

@ -19,8 +19,8 @@
using namespace mbed;
using namespace mbed::nfc;
NFCRemoteInitiator::NFCRemoteInitiator(NFCController *controller, uint8_t *buffer, size_t buffer_size) :
NFCRemoteEndpoint(controller), NFCNDEFCapable(buffer, buffer_size)
NFCRemoteInitiator::NFCRemoteInitiator(NFCController *controller, const Span<uint8_t> &buffer) :
NFCRemoteEndpoint(controller), NFCNDEFCapable(buffer)
{
}

View File

@ -19,8 +19,8 @@
using namespace mbed;
using namespace mbed::nfc;
NFCTarget::NFCTarget(uint8_t *buffer, size_t buffer_size) :
NFCNDEFCapable(buffer, buffer_size)
NFCTarget::NFCTarget(const Span<uint8_t> &buffer) :
NFCNDEFCapable(buffer)
{
}

View File

@ -29,8 +29,8 @@
using namespace mbed;
using namespace mbed::nfc;
Type4RemoteInitiator::Type4RemoteInitiator(NFCController *controller, uint8_t *buffer, size_t buffer_size) :
NFCRemoteInitiator(controller, buffer, buffer_size),
Type4RemoteInitiator::Type4RemoteInitiator(NFCController *controller, const Span<uint8_t> &buffer) :
NFCRemoteInitiator(controller, buffer),
_is_connected(false), _is_disconnected(false)
{
// Init ISO7816