NFC EEPROM sample application
NFC EEPROM Application based on X-NUCLEO-NFC04A1 expansion board
The application will write an URL to the EEPROM of the NFC tag on your board. This will be able to be read by any NFC device capable of reading NFC tags.
The example uses the EEPROM driver provided for the X-NUCLEO-NFC04A1 expansion board based on the ST25DV04K dynamic NFC/RFID tag.
source/target/NfcControllerToEEPROMAdapter.cpp@3:3b37cd0a18e4, 2021-01-11 (annotated)
- Committer:
- apalmieri
- Date:
- Mon Jan 11 11:33:23 2021 +0000
- Revision:
- 3:3b37cd0a18e4
- Parent:
- 0:f3d7ca3d45de
Update Mbed version block (mbed-os-6.6.0)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
apalmieri | 0:f3d7ca3d45de | 1 | /* mbed Microcontroller Library |
apalmieri | 0:f3d7ca3d45de | 2 | * Copyright (c) 2018-2018 ARM Limited |
apalmieri | 0:f3d7ca3d45de | 3 | * |
apalmieri | 0:f3d7ca3d45de | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
apalmieri | 0:f3d7ca3d45de | 5 | * you may not use this file except in compliance with the License. |
apalmieri | 0:f3d7ca3d45de | 6 | * You may obtain a copy of the License at |
apalmieri | 0:f3d7ca3d45de | 7 | * |
apalmieri | 0:f3d7ca3d45de | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
apalmieri | 0:f3d7ca3d45de | 9 | * |
apalmieri | 0:f3d7ca3d45de | 10 | * Unless required by applicable law or agreed to in writing, software |
apalmieri | 0:f3d7ca3d45de | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
apalmieri | 0:f3d7ca3d45de | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
apalmieri | 0:f3d7ca3d45de | 13 | * See the License for the specific language governing permissions and |
apalmieri | 0:f3d7ca3d45de | 14 | * limitations under the License. |
apalmieri | 0:f3d7ca3d45de | 15 | */ |
apalmieri | 0:f3d7ca3d45de | 16 | |
apalmieri | 0:f3d7ca3d45de | 17 | #include "NfcControllerToEEPROMAdapter.h" |
apalmieri | 0:f3d7ca3d45de | 18 | |
apalmieri | 0:f3d7ca3d45de | 19 | #include <algorithm> |
apalmieri | 0:f3d7ca3d45de | 20 | |
apalmieri | 0:f3d7ca3d45de | 21 | #include "nfc/NFCController.h" |
apalmieri | 0:f3d7ca3d45de | 22 | #include "nfc/NFCEEPROMDriver.h" |
apalmieri | 0:f3d7ca3d45de | 23 | #include "nfc/NFCRemoteInitiator.h" |
apalmieri | 0:f3d7ca3d45de | 24 | |
apalmieri | 0:f3d7ca3d45de | 25 | namespace mbed { |
apalmieri | 0:f3d7ca3d45de | 26 | namespace nfc { |
apalmieri | 0:f3d7ca3d45de | 27 | |
apalmieri | 0:f3d7ca3d45de | 28 | ControllerToEEPROMDriverAdapter::ControllerToEEPROMDriverAdapter( |
apalmieri | 0:f3d7ca3d45de | 29 | NFCController &controller, |
apalmieri | 0:f3d7ca3d45de | 30 | const Span<uint8_t> &buffer |
apalmieri | 0:f3d7ca3d45de | 31 | ) : |
apalmieri | 0:f3d7ca3d45de | 32 | _nfc_controller(controller), |
apalmieri | 0:f3d7ca3d45de | 33 | _eeprom_buffer(buffer), |
apalmieri | 0:f3d7ca3d45de | 34 | _current_eeprom_size(buffer.size()), |
apalmieri | 0:f3d7ca3d45de | 35 | _session_opened(false) |
apalmieri | 0:f3d7ca3d45de | 36 | { |
apalmieri | 0:f3d7ca3d45de | 37 | } |
apalmieri | 0:f3d7ca3d45de | 38 | |
apalmieri | 0:f3d7ca3d45de | 39 | ControllerToEEPROMDriverAdapter::~ControllerToEEPROMDriverAdapter() |
apalmieri | 0:f3d7ca3d45de | 40 | { |
apalmieri | 0:f3d7ca3d45de | 41 | if (_nfc_remote_initiator.get()) { |
apalmieri | 0:f3d7ca3d45de | 42 | _nfc_remote_initiator->set_delegate(NULL); |
apalmieri | 0:f3d7ca3d45de | 43 | } |
apalmieri | 0:f3d7ca3d45de | 44 | _nfc_controller.set_delegate(NULL); |
apalmieri | 0:f3d7ca3d45de | 45 | _nfc_controller.cancel_discovery(); |
apalmieri | 0:f3d7ca3d45de | 46 | } |
apalmieri | 0:f3d7ca3d45de | 47 | |
apalmieri | 0:f3d7ca3d45de | 48 | void ControllerToEEPROMDriverAdapter::reset() |
apalmieri | 0:f3d7ca3d45de | 49 | { |
apalmieri | 0:f3d7ca3d45de | 50 | _current_eeprom_size = _eeprom_buffer.size(); |
apalmieri | 0:f3d7ca3d45de | 51 | memset(_eeprom_buffer.data(), 0, _eeprom_buffer.size()); |
apalmieri | 0:f3d7ca3d45de | 52 | |
apalmieri | 0:f3d7ca3d45de | 53 | if (_nfc_remote_initiator.get()) { |
apalmieri | 0:f3d7ca3d45de | 54 | _nfc_remote_initiator->set_delegate(NULL); |
apalmieri | 0:f3d7ca3d45de | 55 | _nfc_remote_initiator.reset(); |
apalmieri | 0:f3d7ca3d45de | 56 | } |
apalmieri | 0:f3d7ca3d45de | 57 | |
apalmieri | 0:f3d7ca3d45de | 58 | _nfc_controller.initialize(); |
apalmieri | 0:f3d7ca3d45de | 59 | _nfc_controller.set_delegate(this); |
apalmieri | 0:f3d7ca3d45de | 60 | |
apalmieri | 0:f3d7ca3d45de | 61 | nfc_rf_protocols_bitmask_t protocols = { 0 }; |
apalmieri | 0:f3d7ca3d45de | 62 | protocols.target_iso_dep = 1; |
apalmieri | 0:f3d7ca3d45de | 63 | _nfc_controller.configure_rf_protocols(protocols); |
apalmieri | 0:f3d7ca3d45de | 64 | |
apalmieri | 0:f3d7ca3d45de | 65 | _nfc_controller.start_discovery(); |
apalmieri | 0:f3d7ca3d45de | 66 | } |
apalmieri | 0:f3d7ca3d45de | 67 | |
apalmieri | 0:f3d7ca3d45de | 68 | size_t ControllerToEEPROMDriverAdapter::read_max_size() |
apalmieri | 0:f3d7ca3d45de | 69 | { |
apalmieri | 0:f3d7ca3d45de | 70 | return _eeprom_buffer.size(); |
apalmieri | 0:f3d7ca3d45de | 71 | } |
apalmieri | 0:f3d7ca3d45de | 72 | |
apalmieri | 0:f3d7ca3d45de | 73 | void ControllerToEEPROMDriverAdapter::start_session(bool force) |
apalmieri | 0:f3d7ca3d45de | 74 | { |
apalmieri | 0:f3d7ca3d45de | 75 | _session_opened = true; |
apalmieri | 0:f3d7ca3d45de | 76 | delegate()->on_session_started(true); |
apalmieri | 0:f3d7ca3d45de | 77 | } |
apalmieri | 0:f3d7ca3d45de | 78 | |
apalmieri | 0:f3d7ca3d45de | 79 | void ControllerToEEPROMDriverAdapter::end_session() |
apalmieri | 0:f3d7ca3d45de | 80 | { |
apalmieri | 0:f3d7ca3d45de | 81 | _session_opened = false; |
apalmieri | 0:f3d7ca3d45de | 82 | delegate()->on_session_ended(true); |
apalmieri | 0:f3d7ca3d45de | 83 | } |
apalmieri | 0:f3d7ca3d45de | 84 | |
apalmieri | 0:f3d7ca3d45de | 85 | void ControllerToEEPROMDriverAdapter::read_bytes( |
apalmieri | 0:f3d7ca3d45de | 86 | uint32_t address, uint8_t *bytes, size_t count |
apalmieri | 0:f3d7ca3d45de | 87 | ) { |
apalmieri | 0:f3d7ca3d45de | 88 | if (address >= _current_eeprom_size) { |
apalmieri | 0:f3d7ca3d45de | 89 | delegate()->on_bytes_read(0); |
apalmieri | 0:f3d7ca3d45de | 90 | return; |
apalmieri | 0:f3d7ca3d45de | 91 | } |
apalmieri | 0:f3d7ca3d45de | 92 | |
apalmieri | 0:f3d7ca3d45de | 93 | size_t size = std::min(count, (size_t) (_current_eeprom_size - address)); |
apalmieri | 0:f3d7ca3d45de | 94 | memcpy(bytes, _eeprom_buffer.data() + address, size); |
apalmieri | 0:f3d7ca3d45de | 95 | delegate()->on_bytes_read(size); |
apalmieri | 0:f3d7ca3d45de | 96 | } |
apalmieri | 0:f3d7ca3d45de | 97 | |
apalmieri | 0:f3d7ca3d45de | 98 | void ControllerToEEPROMDriverAdapter::write_bytes( |
apalmieri | 0:f3d7ca3d45de | 99 | uint32_t address, const uint8_t *bytes, size_t count |
apalmieri | 0:f3d7ca3d45de | 100 | ) { |
apalmieri | 0:f3d7ca3d45de | 101 | if (address >= _current_eeprom_size) { |
apalmieri | 0:f3d7ca3d45de | 102 | delegate()->on_bytes_written(0); |
apalmieri | 0:f3d7ca3d45de | 103 | return; |
apalmieri | 0:f3d7ca3d45de | 104 | } |
apalmieri | 0:f3d7ca3d45de | 105 | |
apalmieri | 0:f3d7ca3d45de | 106 | size_t size = std::min(count, (size_t) (_current_eeprom_size - address)); |
apalmieri | 0:f3d7ca3d45de | 107 | memcpy(_eeprom_buffer.data() + address, bytes, size); |
apalmieri | 0:f3d7ca3d45de | 108 | delegate()->on_bytes_written(size); |
apalmieri | 0:f3d7ca3d45de | 109 | } |
apalmieri | 0:f3d7ca3d45de | 110 | |
apalmieri | 0:f3d7ca3d45de | 111 | void ControllerToEEPROMDriverAdapter::read_size() |
apalmieri | 0:f3d7ca3d45de | 112 | { |
apalmieri | 0:f3d7ca3d45de | 113 | delegate()->on_size_read(true, _current_eeprom_size); |
apalmieri | 0:f3d7ca3d45de | 114 | } |
apalmieri | 0:f3d7ca3d45de | 115 | |
apalmieri | 0:f3d7ca3d45de | 116 | void ControllerToEEPROMDriverAdapter::write_size(size_t count) |
apalmieri | 0:f3d7ca3d45de | 117 | { |
apalmieri | 0:f3d7ca3d45de | 118 | if (count > (size_t) _eeprom_buffer.size()) { |
apalmieri | 0:f3d7ca3d45de | 119 | delegate()->on_size_written(false); |
apalmieri | 0:f3d7ca3d45de | 120 | } else { |
apalmieri | 0:f3d7ca3d45de | 121 | _current_eeprom_size = count; |
apalmieri | 0:f3d7ca3d45de | 122 | delegate()->on_size_written(true); |
apalmieri | 0:f3d7ca3d45de | 123 | } |
apalmieri | 0:f3d7ca3d45de | 124 | } |
apalmieri | 0:f3d7ca3d45de | 125 | |
apalmieri | 0:f3d7ca3d45de | 126 | void ControllerToEEPROMDriverAdapter::erase_bytes(uint32_t address, size_t count) |
apalmieri | 0:f3d7ca3d45de | 127 | { |
apalmieri | 0:f3d7ca3d45de | 128 | if (address >= _current_eeprom_size) { |
apalmieri | 0:f3d7ca3d45de | 129 | delegate()->on_bytes_erased(0); |
apalmieri | 0:f3d7ca3d45de | 130 | return; |
apalmieri | 0:f3d7ca3d45de | 131 | } |
apalmieri | 0:f3d7ca3d45de | 132 | |
apalmieri | 0:f3d7ca3d45de | 133 | size_t size = std::min(count, (size_t) (_current_eeprom_size - address)); |
apalmieri | 0:f3d7ca3d45de | 134 | memset(_eeprom_buffer.data() + address, 0, size); |
apalmieri | 0:f3d7ca3d45de | 135 | delegate()->on_bytes_erased(size); |
apalmieri | 0:f3d7ca3d45de | 136 | } |
apalmieri | 0:f3d7ca3d45de | 137 | |
apalmieri | 0:f3d7ca3d45de | 138 | /* ------------------------------------------------------------------------ |
apalmieri | 0:f3d7ca3d45de | 139 | * Implementation of NFCRemoteInitiator::Delegate |
apalmieri | 0:f3d7ca3d45de | 140 | */ |
apalmieri | 0:f3d7ca3d45de | 141 | void ControllerToEEPROMDriverAdapter::on_connected() { } |
apalmieri | 0:f3d7ca3d45de | 142 | |
apalmieri | 0:f3d7ca3d45de | 143 | void ControllerToEEPROMDriverAdapter::on_disconnected() |
apalmieri | 0:f3d7ca3d45de | 144 | { |
apalmieri | 0:f3d7ca3d45de | 145 | // reset the state of the remote initiator |
apalmieri | 0:f3d7ca3d45de | 146 | _nfc_remote_initiator->set_delegate(NULL); |
apalmieri | 0:f3d7ca3d45de | 147 | _nfc_remote_initiator.reset(); |
apalmieri | 0:f3d7ca3d45de | 148 | |
apalmieri | 0:f3d7ca3d45de | 149 | // restart peer discovery |
apalmieri | 0:f3d7ca3d45de | 150 | _nfc_controller.start_discovery(); |
apalmieri | 0:f3d7ca3d45de | 151 | } |
apalmieri | 0:f3d7ca3d45de | 152 | |
apalmieri | 0:f3d7ca3d45de | 153 | void ControllerToEEPROMDriverAdapter::parse_ndef_message(const Span<const uint8_t> &buffer) |
apalmieri | 0:f3d7ca3d45de | 154 | { |
apalmieri | 0:f3d7ca3d45de | 155 | if (_session_opened) { |
apalmieri | 0:f3d7ca3d45de | 156 | return; |
apalmieri | 0:f3d7ca3d45de | 157 | } |
apalmieri | 0:f3d7ca3d45de | 158 | |
apalmieri | 0:f3d7ca3d45de | 159 | if (buffer.size() > _eeprom_buffer.size()) { |
apalmieri | 0:f3d7ca3d45de | 160 | return; |
apalmieri | 0:f3d7ca3d45de | 161 | } |
apalmieri | 0:f3d7ca3d45de | 162 | |
apalmieri | 0:f3d7ca3d45de | 163 | _current_eeprom_size = buffer.size(); |
apalmieri | 0:f3d7ca3d45de | 164 | memcpy(_eeprom_buffer.data(), buffer.data(), _current_eeprom_size); |
apalmieri | 0:f3d7ca3d45de | 165 | } |
apalmieri | 0:f3d7ca3d45de | 166 | |
apalmieri | 0:f3d7ca3d45de | 167 | size_t ControllerToEEPROMDriverAdapter::build_ndef_message(const Span<uint8_t> &buffer) |
apalmieri | 0:f3d7ca3d45de | 168 | { |
apalmieri | 0:f3d7ca3d45de | 169 | if (_session_opened) { |
apalmieri | 0:f3d7ca3d45de | 170 | return 0; |
apalmieri | 0:f3d7ca3d45de | 171 | } |
apalmieri | 0:f3d7ca3d45de | 172 | |
apalmieri | 0:f3d7ca3d45de | 173 | if ((size_t) buffer.size() < _current_eeprom_size) { |
apalmieri | 0:f3d7ca3d45de | 174 | return 0; |
apalmieri | 0:f3d7ca3d45de | 175 | } |
apalmieri | 0:f3d7ca3d45de | 176 | |
apalmieri | 0:f3d7ca3d45de | 177 | memcpy(buffer.data(), _eeprom_buffer.data(), _current_eeprom_size); |
apalmieri | 0:f3d7ca3d45de | 178 | return _current_eeprom_size; |
apalmieri | 0:f3d7ca3d45de | 179 | } |
apalmieri | 0:f3d7ca3d45de | 180 | |
apalmieri | 0:f3d7ca3d45de | 181 | /* ------------------------------------------------------------------------ |
apalmieri | 0:f3d7ca3d45de | 182 | * Implementation of NFCController::Delegate |
apalmieri | 0:f3d7ca3d45de | 183 | */ |
apalmieri | 0:f3d7ca3d45de | 184 | void ControllerToEEPROMDriverAdapter::on_discovery_terminated( |
apalmieri | 0:f3d7ca3d45de | 185 | nfc_discovery_terminated_reason_t reason |
apalmieri | 0:f3d7ca3d45de | 186 | ) { |
apalmieri | 0:f3d7ca3d45de | 187 | if(reason != nfc_discovery_terminated_completed) { |
apalmieri | 0:f3d7ca3d45de | 188 | _nfc_controller.start_discovery(); |
apalmieri | 0:f3d7ca3d45de | 189 | } |
apalmieri | 0:f3d7ca3d45de | 190 | } |
apalmieri | 0:f3d7ca3d45de | 191 | |
apalmieri | 0:f3d7ca3d45de | 192 | void ControllerToEEPROMDriverAdapter::on_nfc_initiator_discovered( |
apalmieri | 0:f3d7ca3d45de | 193 | const SharedPtr<NFCRemoteInitiator> &nfc_initiator |
apalmieri | 0:f3d7ca3d45de | 194 | ) { |
apalmieri | 0:f3d7ca3d45de | 195 | // setup the local remote initiator |
apalmieri | 0:f3d7ca3d45de | 196 | _nfc_remote_initiator = nfc_initiator; |
apalmieri | 0:f3d7ca3d45de | 197 | _nfc_remote_initiator->set_delegate(this); |
apalmieri | 0:f3d7ca3d45de | 198 | _nfc_remote_initiator->connect(); |
apalmieri | 0:f3d7ca3d45de | 199 | } |
apalmieri | 0:f3d7ca3d45de | 200 | |
apalmieri | 0:f3d7ca3d45de | 201 | } // namespace nfc |
apalmieri | 0:f3d7ca3d45de | 202 | } // namespace mbed |
apalmieri | 0:f3d7ca3d45de | 203 |