From b5bfcc3fcb342f37a2a1099b115d86301d35c40f Mon Sep 17 00:00:00 2001 From: Donatien Garnier Date: Tue, 28 Aug 2018 17:32:39 +0100 Subject: [PATCH] Address @paul-szczepanek-arm's comments --- features/nfc/nfc/NFCControllerDriver.h | 3 ++- features/nfc/nfc/NFCEEPROM.h | 3 +++ features/nfc/nfc/NFCNDEFCapable.h | 15 ++++++------ features/nfc/nfc/NFCRemoteInitiator.h | 3 +++ features/nfc/source/nfc/NFCEEPROM.cpp | 6 ++++- features/nfc/source/nfc/NFCNDEFCapable.cpp | 23 +++++++++++-------- .../nfc/source/nfc/NFCRemoteInitiator.cpp | 8 +++++-- 7 files changed, 39 insertions(+), 22 deletions(-) diff --git a/features/nfc/nfc/NFCControllerDriver.h b/features/nfc/nfc/NFCControllerDriver.h index f454d40040..db78cfb8d1 100644 --- a/features/nfc/nfc/NFCControllerDriver.h +++ b/features/nfc/nfc/NFCControllerDriver.h @@ -82,13 +82,14 @@ public: * @param[in] delegate the delegate instance to use */ void set_delegate(Delegate *delegate); -protected: +protected: /** * An implementation must call this function (can be called from interrupt context) * when the controller asserts its interrupt line */ void hw_interrupt(); + private: Delegate *_delegate; }; diff --git a/features/nfc/nfc/NFCEEPROM.h b/features/nfc/nfc/NFCEEPROM.h index 312af06f3d..459ce15d0f 100644 --- a/features/nfc/nfc/NFCEEPROM.h +++ b/features/nfc/nfc/NFCEEPROM.h @@ -119,6 +119,9 @@ private: void continue_read(); void continue_erase(); + // NFCNDEFCapable implementation + virtual NFCNDEFCapable::Delegate *ndef_capable_delegate(); + enum nfc_eeprom_operation_t { nfc_eeprom_idle, diff --git a/features/nfc/nfc/NFCNDEFCapable.h b/features/nfc/nfc/NFCNDEFCapable.h index a5df6d061b..66b3cd5af8 100644 --- a/features/nfc/nfc/NFCNDEFCapable.h +++ b/features/nfc/nfc/NFCNDEFCapable.h @@ -81,13 +81,6 @@ public: ~Delegate() {} }; - /** - * Set the delegate that will receive events generated by this class. - * - * @param[in] delegate the delegate instance to use - */ - void set_ndef_delegate(Delegate *delegate); - protected: /** * Parse a NDEF message. @@ -111,13 +104,19 @@ protected: ndef_msg_t *ndef_message(); private: + /** + * Get the delegate that will receive events generated by this class. + * + * @return the delegate instance to use + */ + virtual Delegate *ndef_capable_delegate(); + // Callbacks from NDEF stack static nfc_err_t s_ndef_encode(ndef_msg_t *pTag, ac_buffer_builder_t *pBufferBldr, void *pUserData); static nfc_err_t s_ndef_decode(ndef_msg_t *pTag, ac_buffer_t *pBuffer, void *pUserData); nfc_err_t ndef_encode(ac_buffer_builder_t *pBufferBldr); nfc_err_t ndef_decode(ac_buffer_t *pBuffer); - Delegate *_delegate; ndef_msg_t _ndef_message; }; diff --git a/features/nfc/nfc/NFCRemoteInitiator.h b/features/nfc/nfc/NFCRemoteInitiator.h index 0ceeccd1e6..b8cde883ab 100644 --- a/features/nfc/nfc/NFCRemoteInitiator.h +++ b/features/nfc/nfc/NFCRemoteInitiator.h @@ -92,6 +92,9 @@ protected: virtual void disconnected(); private: + // NFCNDEFCapable implementation + virtual NFCNDEFCapable::Delegate *ndef_capable_delegate(); + Delegate *_delegate; }; diff --git a/features/nfc/source/nfc/NFCEEPROM.cpp b/features/nfc/source/nfc/NFCEEPROM.cpp index 1ad9486b32..32ce0167e4 100644 --- a/features/nfc/source/nfc/NFCEEPROM.cpp +++ b/features/nfc/source/nfc/NFCEEPROM.cpp @@ -40,7 +40,6 @@ nfc_err_t NFCEEPROM::initialize() void NFCEEPROM::set_delegate(NFCEEPROM::Delegate *delegate) { _delegate = delegate; - set_ndef_delegate(delegate); } void NFCEEPROM::write_ndef_message() @@ -419,3 +418,8 @@ void NFCEEPROM::handle_error(nfc_err_t ret) } } } + +NFCNDEFCapable::Delegate *NFCEEPROM::ndef_capable_delegate() +{ + return _delegate; +} \ No newline at end of file diff --git a/features/nfc/source/nfc/NFCNDEFCapable.cpp b/features/nfc/source/nfc/NFCNDEFCapable.cpp index f4034f775e..901c5f4166 100644 --- a/features/nfc/source/nfc/NFCNDEFCapable.cpp +++ b/features/nfc/source/nfc/NFCNDEFCapable.cpp @@ -25,29 +25,27 @@ using namespace mbed; using namespace mbed::nfc; -NFCNDEFCapable::NFCNDEFCapable(const Span &buffer) : _delegate(NULL) +NFCNDEFCapable::NFCNDEFCapable(const Span &buffer) { ndef_msg_init(&_ndef_message, s_ndef_encode, s_ndef_decode, buffer.data(), buffer.size(), this); } -void NFCNDEFCapable::set_ndef_delegate(NFCNDEFCapable::Delegate *delegate) -{ - _delegate = delegate; -} - 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(make_const_Span(ac_buffer_reader_current_buffer_pointer(&reader), ac_buffer_reader_current_buffer_length(&reader))); + + Delegate *delegate = ndef_capable_delegate(); + if (delegate != NULL) { + 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(make_Span(ac_buffer_builder_write_position(&buffer_builder), ac_buffer_builder_writable(&buffer_builder))); + Delegate *delegate = ndef_capable_delegate(); + if (delegate != NULL) { + 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); } } @@ -80,3 +78,8 @@ ndef_msg_t *NFCNDEFCapable::ndef_message() { return &_ndef_message; } + +NFCNDEFCapable::Delegate *NFCNDEFCapable::ndef_capable_delegate() +{ + return NULL; +} \ No newline at end of file diff --git a/features/nfc/source/nfc/NFCRemoteInitiator.cpp b/features/nfc/source/nfc/NFCRemoteInitiator.cpp index e3ca435166..9c8e9679c0 100644 --- a/features/nfc/source/nfc/NFCRemoteInitiator.cpp +++ b/features/nfc/source/nfc/NFCRemoteInitiator.cpp @@ -20,7 +20,7 @@ using namespace mbed; using namespace mbed::nfc; NFCRemoteInitiator::NFCRemoteInitiator(NFCController *controller, const Span &buffer) : - NFCRemoteEndpoint(controller), NFCNDEFCapable(buffer) + NFCRemoteEndpoint(controller), NFCNDEFCapable(buffer), _delegate(NULL) { } @@ -33,7 +33,6 @@ NFCRemoteInitiator::~NFCRemoteInitiator() void NFCRemoteInitiator::set_delegate(Delegate *delegate) { _delegate = delegate; - set_ndef_delegate(delegate); } void NFCRemoteInitiator::connected() @@ -49,3 +48,8 @@ void NFCRemoteInitiator::disconnected() _delegate->on_disconnected(); } } + +NFCNDEFCapable::Delegate *NFCRemoteInitiator::ndef_capable_delegate() +{ + return _delegate; +} \ No newline at end of file