NFC EEPROM sample application

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers NfcControllerToEEPROMAdapter.cpp Source File

NfcControllerToEEPROMAdapter.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2018-2018 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "NfcControllerToEEPROMAdapter.h"
00018 
00019 #include <algorithm>
00020 
00021 #include "nfc/NFCController.h"
00022 #include "nfc/NFCEEPROMDriver.h"
00023 #include "nfc/NFCRemoteInitiator.h"
00024 
00025 namespace mbed {
00026 namespace nfc {
00027 
00028 ControllerToEEPROMDriverAdapter::ControllerToEEPROMDriverAdapter(
00029     NFCController &controller,
00030     const Span<uint8_t> &buffer
00031 ) :
00032     _nfc_controller(controller),
00033     _eeprom_buffer(buffer),
00034     _current_eeprom_size(buffer.size()),
00035     _session_opened(false)
00036 {
00037 }
00038 
00039 ControllerToEEPROMDriverAdapter::~ControllerToEEPROMDriverAdapter()
00040 {
00041     if (_nfc_remote_initiator.get()) {
00042         _nfc_remote_initiator->set_delegate(NULL);
00043     }
00044     _nfc_controller.set_delegate(NULL);
00045     _nfc_controller.cancel_discovery();
00046 }
00047 
00048 void ControllerToEEPROMDriverAdapter::reset()
00049 {
00050     _current_eeprom_size = _eeprom_buffer.size();
00051     memset(_eeprom_buffer.data(), 0, _eeprom_buffer.size());
00052 
00053     if (_nfc_remote_initiator.get()) {
00054         _nfc_remote_initiator->set_delegate(NULL);
00055         _nfc_remote_initiator.reset();
00056     }
00057 
00058     _nfc_controller.initialize();
00059     _nfc_controller.set_delegate(this);
00060 
00061     nfc_rf_protocols_bitmask_t protocols = { 0 };
00062     protocols.target_iso_dep = 1;
00063     _nfc_controller.configure_rf_protocols(protocols);
00064 
00065     _nfc_controller.start_discovery();
00066 }
00067 
00068 size_t ControllerToEEPROMDriverAdapter::read_max_size()
00069 {
00070     return _eeprom_buffer.size();
00071 }
00072 
00073 void ControllerToEEPROMDriverAdapter::start_session(bool force)
00074 {
00075     _session_opened = true;
00076     delegate()->on_session_started(true);
00077 }
00078 
00079 void ControllerToEEPROMDriverAdapter::end_session()
00080 {
00081     _session_opened = false;
00082     delegate()->on_session_ended(true);
00083 }
00084 
00085 void ControllerToEEPROMDriverAdapter::read_bytes(
00086     uint32_t address, uint8_t *bytes, size_t count
00087 ) {
00088     if (address >= _current_eeprom_size) {
00089         delegate()->on_bytes_read(0);
00090         return;
00091     }
00092 
00093     size_t size = std::min(count, (size_t) (_current_eeprom_size - address));
00094     memcpy(bytes, _eeprom_buffer.data() + address, size);
00095     delegate()->on_bytes_read(size);
00096 }
00097 
00098 void ControllerToEEPROMDriverAdapter::write_bytes(
00099     uint32_t address, const uint8_t *bytes, size_t count
00100 ) {
00101     if (address >= _current_eeprom_size) {
00102         delegate()->on_bytes_written(0);
00103         return;
00104     }
00105 
00106     size_t size = std::min(count, (size_t) (_current_eeprom_size - address));
00107     memcpy(_eeprom_buffer.data() + address, bytes, size);
00108     delegate()->on_bytes_written(size);
00109 }
00110 
00111 void ControllerToEEPROMDriverAdapter::read_size()
00112 {
00113     delegate()->on_size_read(true, _current_eeprom_size);
00114 }
00115 
00116 void ControllerToEEPROMDriverAdapter::write_size(size_t count)
00117 {
00118     if (count > (size_t) _eeprom_buffer.size()) {
00119         delegate()->on_size_written(false);
00120     } else {
00121         _current_eeprom_size = count;
00122         delegate()->on_size_written(true);
00123     }
00124 }
00125 
00126 void ControllerToEEPROMDriverAdapter::erase_bytes(uint32_t address, size_t count)
00127 {
00128     if (address >= _current_eeprom_size) {
00129         delegate()->on_bytes_erased(0);
00130         return;
00131     }
00132 
00133     size_t size = std::min(count, (size_t) (_current_eeprom_size - address));
00134     memset(_eeprom_buffer.data() + address, 0, size);
00135     delegate()->on_bytes_erased(size);
00136 }
00137 
00138 /* ------------------------------------------------------------------------
00139  * Implementation of NFCRemoteInitiator::Delegate
00140  */
00141 void ControllerToEEPROMDriverAdapter::on_connected() { }
00142 
00143 void ControllerToEEPROMDriverAdapter::on_disconnected()
00144 {
00145     // reset the state of the remote initiator
00146     _nfc_remote_initiator->set_delegate(NULL);
00147     _nfc_remote_initiator.reset();
00148 
00149     // restart peer discovery
00150     _nfc_controller.start_discovery();
00151 }
00152 
00153 void ControllerToEEPROMDriverAdapter::parse_ndef_message(const Span<const uint8_t> &buffer)
00154 {
00155     if (_session_opened) {
00156         return;
00157     }
00158 
00159     if (buffer.size() > _eeprom_buffer.size()) {
00160         return;
00161     }
00162 
00163     _current_eeprom_size = buffer.size();
00164     memcpy(_eeprom_buffer.data(), buffer.data(), _current_eeprom_size);
00165 }
00166 
00167 size_t ControllerToEEPROMDriverAdapter::build_ndef_message(const Span<uint8_t> &buffer)
00168 {
00169     if (_session_opened) {
00170         return 0;
00171     }
00172 
00173     if ((size_t) buffer.size() < _current_eeprom_size) {
00174         return 0;
00175     }
00176 
00177     memcpy(buffer.data(), _eeprom_buffer.data(), _current_eeprom_size);
00178     return _current_eeprom_size;
00179 }
00180 
00181 /* ------------------------------------------------------------------------
00182  * Implementation of NFCController::Delegate
00183  */
00184 void ControllerToEEPROMDriverAdapter::on_discovery_terminated(
00185     nfc_discovery_terminated_reason_t reason
00186 ) {
00187     if(reason != nfc_discovery_terminated_completed) {
00188         _nfc_controller.start_discovery();
00189     }
00190 }
00191 
00192 void ControllerToEEPROMDriverAdapter::on_nfc_initiator_discovered(
00193     const SharedPtr<NFCRemoteInitiator> &nfc_initiator
00194 ) {
00195     // setup the local remote initiator
00196     _nfc_remote_initiator = nfc_initiator;
00197     _nfc_remote_initiator->set_delegate(this);
00198     _nfc_remote_initiator->connect();
00199 }
00200 
00201 }  // namespace nfc
00202 }  // namespace mbed
00203