Color Oled(SSD1331) connect to STMicroelectronics Nucleo-F466

Dependencies:   ssd1331

Committer:
kadonotakashi
Date:
Thu Oct 11 02:27:46 2018 +0000
Revision:
3:f3764f852aa8
Parent:
0:8fdf9a60065b
Nucreo 446 + SSD1331 test version;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kadonotakashi 0:8fdf9a60065b 1 # Near-field communication in Mbed OS
kadonotakashi 0:8fdf9a60065b 2
kadonotakashi 0:8fdf9a60065b 3 ## Table of contents
kadonotakashi 0:8fdf9a60065b 4
kadonotakashi 0:8fdf9a60065b 5 - [Near-field communication in Mbed OS](#near-field-communication-in-mbed-os)
kadonotakashi 0:8fdf9a60065b 6 - [Table of contents](#table-of-contents)
kadonotakashi 0:8fdf9a60065b 7 - [Revision history](#revision-history)
kadonotakashi 0:8fdf9a60065b 8 - [Introduction](#introduction)
kadonotakashi 0:8fdf9a60065b 9 - [Overview and background](#overview-and-background)
kadonotakashi 0:8fdf9a60065b 10 - [Use cases](#use-cases)
kadonotakashi 0:8fdf9a60065b 11 - [Commissioning](#commissioning)
kadonotakashi 0:8fdf9a60065b 12 - [Identification](#identification)
kadonotakashi 0:8fdf9a60065b 13 - [Transport](#transport)
kadonotakashi 0:8fdf9a60065b 14 - [BLE pairing](#ble-pairing)
kadonotakashi 0:8fdf9a60065b 15 - [System architecture and high-level design](#system-architecture-and-high-level-design)
kadonotakashi 0:8fdf9a60065b 16 - [Compliance with NFC forum specifications](#compliance-with-nfc-forum-specifications)
kadonotakashi 0:8fdf9a60065b 17 - [User-facing API](#user-facing-api)
kadonotakashi 0:8fdf9a60065b 18 - [Phase 1: MicroNFC stack integration](#phase-1-micronfc-stack-integration)
kadonotakashi 0:8fdf9a60065b 19 - [Phase 2: NFC host/controller split, NCI and NFC HAL API](#phase-2-nfc-hostcontroller-split-nci-and-nfc-hal-api)
kadonotakashi 0:8fdf9a60065b 20 - [Detailed design](#detailed-design)
kadonotakashi 0:8fdf9a60065b 21 - [User-facing APIs](#user-facing-apis)
kadonotakashi 0:8fdf9a60065b 22 - [NFC controller](#nfc-controller)
kadonotakashi 0:8fdf9a60065b 23 - [Endpoints](#endpoints)
kadonotakashi 0:8fdf9a60065b 24 - [NFC remote endpoint](#nfc-remote-endpoint)
kadonotakashi 0:8fdf9a60065b 25 - [NFC NDEF capable](#nfc-ndef-capable)
kadonotakashi 0:8fdf9a60065b 26 - [NFC remote initiator](#nfc-remote-initiator)
kadonotakashi 0:8fdf9a60065b 27 - [NFC target](#nfc-target)
kadonotakashi 0:8fdf9a60065b 28 - [NFC EEPROM](#nfc-eeprom)
kadonotakashi 0:8fdf9a60065b 29 - [NFC remote target](#nfc-remote-target)
kadonotakashi 0:8fdf9a60065b 30 - [NDEF API](#ndef-api)
kadonotakashi 0:8fdf9a60065b 31 - [Common objects](#common-objects)
kadonotakashi 0:8fdf9a60065b 32 - [Parsing](#parsing)
kadonotakashi 0:8fdf9a60065b 33 - [ndef::MessageParser](#ndefmessageparser)
kadonotakashi 0:8fdf9a60065b 34 - [ndef::MessageParser::Delegate](#ndefmessageparserdelegate)
kadonotakashi 0:8fdf9a60065b 35 - [NDEF record parsing](#ndef-record-parsing)
kadonotakashi 0:8fdf9a60065b 36 - [ndef::RecordParser](#ndefrecordparser)
kadonotakashi 0:8fdf9a60065b 37 - [ndef::RecordParserChain](#ndefrecordparserchain)
kadonotakashi 0:8fdf9a60065b 38 - [ndef::GenericRecordParser<ParserImplementation, ParsingResult>](#ndefgenericrecordparserparserimplementation-parsingresult)
kadonotakashi 0:8fdf9a60065b 39 - [ndef::GenericRecordParser<ParserImplementation, ParsingResult>::Delegate](#ndefgenericrecordparserparserimplementation-parsingresultdelegate)
kadonotakashi 0:8fdf9a60065b 40 - [Common parsers](#common-parsers)
kadonotakashi 0:8fdf9a60065b 41 - [Simple parser](#simple-parser)
kadonotakashi 0:8fdf9a60065b 42 - [Delegate](#delegate)
kadonotakashi 0:8fdf9a60065b 43 - [Serialization](#serialization)
kadonotakashi 0:8fdf9a60065b 44 - [HAL APIs](#hal-apis)
kadonotakashi 0:8fdf9a60065b 45 - [NFC EEPROM API](#nfc-eeprom-api)
kadonotakashi 0:8fdf9a60065b 46 - [NCI driver APIs](#nci-driver-apis)
kadonotakashi 0:8fdf9a60065b 47 - [Testing strategy](#testing-strategy)
kadonotakashi 0:8fdf9a60065b 48 - [NFC forum compliance](#nfc-forum-compliance)
kadonotakashi 0:8fdf9a60065b 49 - [Interoperability](#interoperability)
kadonotakashi 0:8fdf9a60065b 50 - [HAL testing](#hal-testing)
kadonotakashi 0:8fdf9a60065b 51 - [Dependencies](#dependencies)
kadonotakashi 0:8fdf9a60065b 52
kadonotakashi 0:8fdf9a60065b 53 ## Revision history
kadonotakashi 0:8fdf9a60065b 54
kadonotakashi 0:8fdf9a60065b 55 | Revision | Date | Authors | Mbed OS version | Comments |
kadonotakashi 0:8fdf9a60065b 56 |---------- |---------------- |-------------------------------------------------------- |----------------- |------------------ |
kadonotakashi 0:8fdf9a60065b 57 | 1.0 | 24 August 2018 | Donatien Garnier ([@donatieng](https://github.com/donatieng/)); Vincent Coubard ([@pan-](https://github.com/pan-/)) | 5.10+ | Initial revision |
kadonotakashi 0:8fdf9a60065b 58
kadonotakashi 0:8fdf9a60065b 59 # Introduction
kadonotakashi 0:8fdf9a60065b 60
kadonotakashi 0:8fdf9a60065b 61 ## Overview and background
kadonotakashi 0:8fdf9a60065b 62
kadonotakashi 0:8fdf9a60065b 63 NFC offers a straightforward and secure way of commissioning IoT devices in the field, and we are seeing increasing demand for this from prospective customers. We have a plan to introduce NFC into Mbed OS. This is the first phase to add a reference implementation of card emulation mode.
kadonotakashi 0:8fdf9a60065b 64
kadonotakashi 0:8fdf9a60065b 65 NFC offers three modes:
kadonotakashi 0:8fdf9a60065b 66
kadonotakashi 0:8fdf9a60065b 67 1. NFC card emulation.
kadonotakashi 0:8fdf9a60065b 68 2. NFC reader/writer.
kadonotakashi 0:8fdf9a60065b 69 3. NFC peer to peer.
kadonotakashi 0:8fdf9a60065b 70
kadonotakashi 0:8fdf9a60065b 71 To support new use cases, such as commissioning, BLE pairing and identification and authentication of NFC-enabled IoT endpoints, Mbed OS should support the card emulation mode.
kadonotakashi 0:8fdf9a60065b 72
kadonotakashi 0:8fdf9a60065b 73 However, the architecture should be future-proofed and should also be extendable to support other NFC modes in the future.
kadonotakashi 0:8fdf9a60065b 74
kadonotakashi 0:8fdf9a60065b 75 ## Use cases
kadonotakashi 0:8fdf9a60065b 76
kadonotakashi 0:8fdf9a60065b 77 ### Commissioning
kadonotakashi 0:8fdf9a60065b 78
kadonotakashi 0:8fdf9a60065b 79 NFC is a medium that can support commissioning requirements.
kadonotakashi 0:8fdf9a60065b 80
kadonotakashi 0:8fdf9a60065b 81 #### Identification
kadonotakashi 0:8fdf9a60065b 82
kadonotakashi 0:8fdf9a60065b 83 You can use an NDEF message to carry a device's unique identifier. This eases identification before handing over to another transport medium, such as BLE.
kadonotakashi 0:8fdf9a60065b 84
kadonotakashi 0:8fdf9a60065b 85 #### Transport
kadonotakashi 0:8fdf9a60065b 86
kadonotakashi 0:8fdf9a60065b 87 If the NFC controller can emulate a smartcard, no handover is necessary, and the full commissioning flow can happen over NFC.
kadonotakashi 0:8fdf9a60065b 88
kadonotakashi 0:8fdf9a60065b 89 ### BLE pairing
kadonotakashi 0:8fdf9a60065b 90
kadonotakashi 0:8fdf9a60065b 91 You can use a specifically crafted NDEF message to facilitate out-of-band pairing with man-in-the-middle protection as specified in the [Bluetooth® Secure Simple Pairing Using NFC](https://members.nfc-forum.org/apps/group_public/download.php/18688/NFCForum-AD-BTSSP_1_1.pdf) document.
kadonotakashi 0:8fdf9a60065b 92
kadonotakashi 0:8fdf9a60065b 93 # System architecture and high-level design
kadonotakashi 0:8fdf9a60065b 94
kadonotakashi 0:8fdf9a60065b 95 ## Compliance with NFC forum specifications
kadonotakashi 0:8fdf9a60065b 96
kadonotakashi 0:8fdf9a60065b 97 The NFC Forum is one the bodies producing NFC standards. Most smartphones supporting NFC today are compliant with the NFC Forum's specifications. In that consideration, the NFC component in Mbed OS should map with the relevant standards from the NFC Forum, and NFC Forum terminology should be used where possible.
kadonotakashi 0:8fdf9a60065b 98
kadonotakashi 0:8fdf9a60065b 99 ## User-facing API
kadonotakashi 0:8fdf9a60065b 100
kadonotakashi 0:8fdf9a60065b 101 The NFC API exposed to the user should provide high-level, object-oriented C++ APIs for the following:
kadonotakashi 0:8fdf9a60065b 102
kadonotakashi 0:8fdf9a60065b 103 - Starting/Stopping a discovery loop.
kadonotakashi 0:8fdf9a60065b 104 - Listing wired targets (NFC EEPROMs).
kadonotakashi 0:8fdf9a60065b 105 - Exchanging NDEF messages with an initiator or a wired target.
kadonotakashi 0:8fdf9a60065b 106 - Emulate ISO7816-4 applications if supported.
kadonotakashi 0:8fdf9a60065b 107
kadonotakashi 0:8fdf9a60065b 108 ## Phase 1: MicroNFC stack integration
kadonotakashi 0:8fdf9a60065b 109
kadonotakashi 0:8fdf9a60065b 110 The first step toward adding NFC to Mbed OS is the integration of the MicroNFC stack, which has drivers for the PN512 and derivatives.
kadonotakashi 0:8fdf9a60065b 111
kadonotakashi 0:8fdf9a60065b 112 Architecture:
kadonotakashi 0:8fdf9a60065b 113
kadonotakashi 0:8fdf9a60065b 114 ![phase_1_architecture]
kadonotakashi 0:8fdf9a60065b 115
kadonotakashi 0:8fdf9a60065b 116 ## Phase 2: NFC host/controller split, NCI and NFC HAL API
kadonotakashi 0:8fdf9a60065b 117
kadonotakashi 0:8fdf9a60065b 118 At the moment, the MicroNFC stack is split into two components:
kadonotakashi 0:8fdf9a60065b 119
kadonotakashi 0:8fdf9a60065b 120 - Applications protocols and upper stack.
kadonotakashi 0:8fdf9a60065b 121 - Transceiver-specific polling loop and drivers.
kadonotakashi 0:8fdf9a60065b 122
kadonotakashi 0:8fdf9a60065b 123 To more closely match with the NFC Forum standard and add a well-defined way for Partners to add support for their transceivers, we will amend that split to be compliant with the NFC Forum's NFC Communication Interface (NCI) protocol.
kadonotakashi 0:8fdf9a60065b 124
kadonotakashi 0:8fdf9a60065b 125 The generic part of the controller stack will be clearly separated, so Partners can use it if they wish (approach 1).
kadonotakashi 0:8fdf9a60065b 126
kadonotakashi 0:8fdf9a60065b 127 For NFC controllers natively supporting the NCI protocol, Partners would only have to write a transport driver (approach 2).
kadonotakashi 0:8fdf9a60065b 128
kadonotakashi 0:8fdf9a60065b 129 ![phase_2_architecture]
kadonotakashi 0:8fdf9a60065b 130
kadonotakashi 0:8fdf9a60065b 131 Examples of NCI-compliant controllers:
kadonotakashi 0:8fdf9a60065b 132
kadonotakashi 0:8fdf9a60065b 133 - ST ST21NFC.
kadonotakashi 0:8fdf9a60065b 134 - NXP PN7120 and PN7150.
kadonotakashi 0:8fdf9a60065b 135
kadonotakashi 0:8fdf9a60065b 136 Examples of transceivers that are *not* NCI-compliant:
kadonotakashi 0:8fdf9a60065b 137
kadonotakashi 0:8fdf9a60065b 138 - NXP PN512.
kadonotakashi 0:8fdf9a60065b 139 - NXP PN5180.
kadonotakashi 0:8fdf9a60065b 140 - AMS AS395x series.
kadonotakashi 0:8fdf9a60065b 141
kadonotakashi 0:8fdf9a60065b 142 # Detailed design
kadonotakashi 0:8fdf9a60065b 143
kadonotakashi 0:8fdf9a60065b 144 ## User-facing APIs
kadonotakashi 0:8fdf9a60065b 145
kadonotakashi 0:8fdf9a60065b 146 We designed the user-facing APIs with the following principles:
kadonotakashi 0:8fdf9a60065b 147
kadonotakashi 0:8fdf9a60065b 148 - Abstracting the underlying complexities of NFC from the user.
kadonotakashi 0:8fdf9a60065b 149 - Offering a high-level C++ object-oriented API to the user.
kadonotakashi 0:8fdf9a60065b 150 - Ensuring compliance with the NFC Forum's standards and terminology.
kadonotakashi 0:8fdf9a60065b 151 - Ensuring consistency with the Mbed OS codebase.
kadonotakashi 0:8fdf9a60065b 152
kadonotakashi 0:8fdf9a60065b 153 Class diagram:
kadonotakashi 0:8fdf9a60065b 154
kadonotakashi 0:8fdf9a60065b 155 ###  NFC controller
kadonotakashi 0:8fdf9a60065b 156
kadonotakashi 0:8fdf9a60065b 157 ![nfc_controller_diagram]
kadonotakashi 0:8fdf9a60065b 158
kadonotakashi 0:8fdf9a60065b 159 The `NFCController` class is the entrypoint into NFC for the user.
kadonotakashi 0:8fdf9a60065b 160
kadonotakashi 0:8fdf9a60065b 161 When NCI integration is complete (phase 2), this class will be able to drive a `NCIDriver` instance. For now, the one controller we support is the PN512, which implements the `NFCControllerDriver` class. This class is specific to the current MicroNFC release.
kadonotakashi 0:8fdf9a60065b 162
kadonotakashi 0:8fdf9a60065b 163 ```cpp
kadonotakashi 0:8fdf9a60065b 164 NFCController(NFCControllerDriver *driver, events::EventQueue *queue, const Span<uint8_t> &ndef_buffer);
kadonotakashi 0:8fdf9a60065b 165 ```
kadonotakashi 0:8fdf9a60065b 166
kadonotakashi 0:8fdf9a60065b 167 The user instantiates the `NFCController` class using a driver, an event queue used for asynchronous operations and a scratch buffer used for NDEF processing.
kadonotakashi 0:8fdf9a60065b 168
kadonotakashi 0:8fdf9a60065b 169 It offers the following methods:
kadonotakashi 0:8fdf9a60065b 170
kadonotakashi 0:8fdf9a60065b 171 ```cpp
kadonotakashi 0:8fdf9a60065b 172 void set_delegate(Delegate *delegate);
kadonotakashi 0:8fdf9a60065b 173 ```
kadonotakashi 0:8fdf9a60065b 174
kadonotakashi 0:8fdf9a60065b 175 Set the instance's delegate.
kadonotakashi 0:8fdf9a60065b 176
kadonotakashi 0:8fdf9a60065b 177 ```cpp
kadonotakashi 0:8fdf9a60065b 178 struct nfc_rf_protocols_bitmask_t
kadonotakashi 0:8fdf9a60065b 179 {
kadonotakashi 0:8fdf9a60065b 180 uint8_t initiator_t1t : 1;
kadonotakashi 0:8fdf9a60065b 181 uint8_t initiator_t2t : 1;
kadonotakashi 0:8fdf9a60065b 182 uint8_t initiator_t3t : 1;
kadonotakashi 0:8fdf9a60065b 183 uint8_t initiator_iso_dep : 1;
kadonotakashi 0:8fdf9a60065b 184 uint8_t initiator_nfc_dep : 1;
kadonotakashi 0:8fdf9a60065b 185 uint8_t initiator_t5t : 1;
kadonotakashi 0:8fdf9a60065b 186
kadonotakashi 0:8fdf9a60065b 187 uint8_t target_t1t : 1;
kadonotakashi 0:8fdf9a60065b 188 uint8_t target_t2t : 1;
kadonotakashi 0:8fdf9a60065b 189 uint8_t target_t3t : 1;
kadonotakashi 0:8fdf9a60065b 190 uint8_t target_iso_dep : 1;
kadonotakashi 0:8fdf9a60065b 191 uint8_t target_nfc_dep : 1;
kadonotakashi 0:8fdf9a60065b 192 uint8_t target_t5t : 1;
kadonotakashi 0:8fdf9a60065b 193 };
kadonotakashi 0:8fdf9a60065b 194
kadonotakashi 0:8fdf9a60065b 195 nfc_rf_protocols_bitmask_t get_supported_rf_protocols() const;
kadonotakashi 0:8fdf9a60065b 196 ```
kadonotakashi 0:8fdf9a60065b 197
kadonotakashi 0:8fdf9a60065b 198 Retrieve the list of supported RF protocols. These are mapped against NFC Forum-defined protocols.
kadonotakashi 0:8fdf9a60065b 199
kadonotakashi 0:8fdf9a60065b 200 - T1T is based on ISO/IEC 14443A-3 and commonly known as Topaz (Innovision).
kadonotakashi 0:8fdf9a60065b 201 - T2T is based on ISO/IEC 14443A-3 and commonly known as Mifare Ultralight/NTAG (NXP).
kadonotakashi 0:8fdf9a60065b 202 - T3T is based on JIS X6319-4, also known as Felica (Sony).
kadonotakashi 0:8fdf9a60065b 203 - ISO-DEP is based on ISO/IEC 14443-4 and is the common interface for contactless smartcards. The underlying radio protocol can either be ISO/IEC 14443A or ISO/IEC 14443B.
kadonotakashi 0:8fdf9a60065b 204 - NFC-DEP is based on ISO/IEC 18092/FIXME and is the basis for NFC peer-to-peer communication.
kadonotakashi 0:8fdf9a60065b 205 - T5T is also known as ISO/IEC 15963.
kadonotakashi 0:8fdf9a60065b 206
kadonotakashi 0:8fdf9a60065b 207 ```cpp
kadonotakashi 0:8fdf9a60065b 208 nfc_err_t initialize();
kadonotakashi 0:8fdf9a60065b 209 ```
kadonotakashi 0:8fdf9a60065b 210
kadonotakashi 0:8fdf9a60065b 211 Initialize the NFC controller.
kadonotakashi 0:8fdf9a60065b 212
kadonotakashi 0:8fdf9a60065b 213 ```cpp
kadonotakashi 0:8fdf9a60065b 214 nfc_err_t configure_rf_protocols(nfc_rf_protocols_bitmask_t rf_protocols);
kadonotakashi 0:8fdf9a60065b 215 ```
kadonotakashi 0:8fdf9a60065b 216
kadonotakashi 0:8fdf9a60065b 217 Configure which protocols should be enabled during the discovery process.
kadonotakashi 0:8fdf9a60065b 218
kadonotakashi 0:8fdf9a60065b 219 ```cpp
kadonotakashi 0:8fdf9a60065b 220 nfc_err_t start_discovery();
kadonotakashi 0:8fdf9a60065b 221 ```
kadonotakashi 0:8fdf9a60065b 222
kadonotakashi 0:8fdf9a60065b 223 Start the discovery process.
kadonotakashi 0:8fdf9a60065b 224
kadonotakashi 0:8fdf9a60065b 225 ```cpp
kadonotakashi 0:8fdf9a60065b 226 nfc_err_t cancel_discovery();
kadonotakashi 0:8fdf9a60065b 227 ```
kadonotakashi 0:8fdf9a60065b 228
kadonotakashi 0:8fdf9a60065b 229 Cancel the discovery process (if running).
kadonotakashi 0:8fdf9a60065b 230
kadonotakashi 0:8fdf9a60065b 231 **Delegate**
kadonotakashi 0:8fdf9a60065b 232
kadonotakashi 0:8fdf9a60065b 233 A `NFCController` instance needs to be configured with a delegate to receive events.
kadonotakashi 0:8fdf9a60065b 234
kadonotakashi 0:8fdf9a60065b 235 ```cpp
kadonotakashi 0:8fdf9a60065b 236 enum nfc_discovery_terminated_reason_t {
kadonotakashi 0:8fdf9a60065b 237 nfc_discovery_terminated_completed = 0
kadonotakashi 0:8fdf9a60065b 238 nfc_discovery_terminated_canceled,
kadonotakashi 0:8fdf9a60065b 239 nfc_discovery_terminated_rf_error
kadonotakashi 0:8fdf9a60065b 240 };
kadonotakashi 0:8fdf9a60065b 241
kadonotakashi 0:8fdf9a60065b 242 void on_discovery_terminated(nfc_discovery_terminated_reason_t reason);
kadonotakashi 0:8fdf9a60065b 243 ```
kadonotakashi 0:8fdf9a60065b 244
kadonotakashi 0:8fdf9a60065b 245 Let the user know when a discovery loop has been terminated (either because endpoints have been found, the user canceled it or an error occurred).
kadonotakashi 0:8fdf9a60065b 246
kadonotakashi 0:8fdf9a60065b 247 ```cpp
kadonotakashi 0:8fdf9a60065b 248 void on_nfc_initiator_discovered(const mbed::SharedPtr<NFCRemoteInitiator> &nfc_initiator);
kadonotakashi 0:8fdf9a60065b 249
kadonotakashi 0:8fdf9a60065b 250 void on_nfc_target_discovered(const mbed::SharedPtr<NFCRemoteTarget> &nfc_target);
kadonotakashi 0:8fdf9a60065b 251 ```
kadonotakashi 0:8fdf9a60065b 252
kadonotakashi 0:8fdf9a60065b 253 These methods called when a remote initiator (the local controller is acting as a target) or a remote target (the local controller is acting as an initiator) is detected.
kadonotakashi 0:8fdf9a60065b 254
kadonotakashi 0:8fdf9a60065b 255 These methods use shared pointers, so the user does not have to maintain the lifetime of these objects. The `NFCController` instance releases its reference when the endpoint is lost (see below).
kadonotakashi 0:8fdf9a60065b 256
kadonotakashi 0:8fdf9a60065b 257 ### Endpoints
kadonotakashi 0:8fdf9a60065b 258
kadonotakashi 0:8fdf9a60065b 259 ![nfc_endpoints_diagram]
kadonotakashi 0:8fdf9a60065b 260
kadonotakashi 0:8fdf9a60065b 261 #### NFC remote endpoint
kadonotakashi 0:8fdf9a60065b 262
kadonotakashi 0:8fdf9a60065b 263 A remote endpoint is a generic NFC-enabled device with which the controller is communicating over the air interface:
kadonotakashi 0:8fdf9a60065b 264
kadonotakashi 0:8fdf9a60065b 265 ```cpp
kadonotakashi 0:8fdf9a60065b 266 nfc_err_t connect();
kadonotakashi 0:8fdf9a60065b 267 ```
kadonotakashi 0:8fdf9a60065b 268
kadonotakashi 0:8fdf9a60065b 269 Establish a connection with the remote endpoint.
kadonotakashi 0:8fdf9a60065b 270
kadonotakashi 0:8fdf9a60065b 271 ```cpp
kadonotakashi 0:8fdf9a60065b 272 nfc_err_t disconnect();
kadonotakashi 0:8fdf9a60065b 273 ```
kadonotakashi 0:8fdf9a60065b 274
kadonotakashi 0:8fdf9a60065b 275 Drop the connection with the remote endpoint.
kadonotakashi 0:8fdf9a60065b 276
kadonotakashi 0:8fdf9a60065b 277 ```cpp
kadonotakashi 0:8fdf9a60065b 278 bool is_connected() const;
kadonotakashi 0:8fdf9a60065b 279 ```
kadonotakashi 0:8fdf9a60065b 280
kadonotakashi 0:8fdf9a60065b 281 Set to true when the remote endpoint activates the connection and selects it.
kadonotakashi 0:8fdf9a60065b 282
kadonotakashi 0:8fdf9a60065b 283 ```cpp
kadonotakashi 0:8fdf9a60065b 284 bool is_disconnected() const;
kadonotakashi 0:8fdf9a60065b 285 ```
kadonotakashi 0:8fdf9a60065b 286
kadonotakashi 0:8fdf9a60065b 287 Set to true when the remote endpoint is lost and the `NFCController` instance releases its reference to the shared pointer.
kadonotakashi 0:8fdf9a60065b 288
kadonotakashi 0:8fdf9a60065b 289 ```cpp
kadonotakashi 0:8fdf9a60065b 290 nfc_rf_protocols_bitmask_t rf_protocols() const;
kadonotakashi 0:8fdf9a60065b 291 ```
kadonotakashi 0:8fdf9a60065b 292
kadonotakashi 0:8fdf9a60065b 293 List the RF protocols that have been activated to communicate with that endpoint.
kadonotakashi 0:8fdf9a60065b 294
kadonotakashi 0:8fdf9a60065b 295 **Delegate**
kadonotakashi 0:8fdf9a60065b 296
kadonotakashi 0:8fdf9a60065b 297 ```cpp
kadonotakashi 0:8fdf9a60065b 298 virtual void on_connected();
kadonotakashi 0:8fdf9a60065b 299 ```
kadonotakashi 0:8fdf9a60065b 300
kadonotakashi 0:8fdf9a60065b 301 This is called when a connection to this endpoint is succesfully established.
kadonotakashi 0:8fdf9a60065b 302
kadonotakashi 0:8fdf9a60065b 303 ```cpp
kadonotakashi 0:8fdf9a60065b 304 virtual void on_disconnected();
kadonotakashi 0:8fdf9a60065b 305 ```
kadonotakashi 0:8fdf9a60065b 306
kadonotakashi 0:8fdf9a60065b 307 This is called when this endpoint is lost and the controller instance is about to release the reference to the shared pointer.
kadonotakashi 0:8fdf9a60065b 308
kadonotakashi 0:8fdf9a60065b 309 #### NFC NDEF capable
kadonotakashi 0:8fdf9a60065b 310
kadonotakashi 0:8fdf9a60065b 311 This class is the ancestor class for all endpoints which have the capability of handling NDEF data.
kadonotakashi 0:8fdf9a60065b 312
kadonotakashi 0:8fdf9a60065b 313 User-facing API:
kadonotakashi 0:8fdf9a60065b 314
kadonotakashi 0:8fdf9a60065b 315 ```cpp
kadonotakashi 0:8fdf9a60065b 316 NFCNDEFCapable(const Span<uint8_t> &buffer);
kadonotakashi 0:8fdf9a60065b 317 ```
kadonotakashi 0:8fdf9a60065b 318
kadonotakashi 0:8fdf9a60065b 319 The class is constructed using a scratch buffer which is used to encode and/or decode NDEF messages.
kadonotakashi 0:8fdf9a60065b 320
kadonotakashi 0:8fdf9a60065b 321 ```cpp
kadonotakashi 0:8fdf9a60065b 322 bool is_ndef_supported() const;
kadonotakashi 0:8fdf9a60065b 323 ```
kadonotakashi 0:8fdf9a60065b 324
kadonotakashi 0:8fdf9a60065b 325 API used by descendant classes:
kadonotakashi 0:8fdf9a60065b 326
kadonotakashi 0:8fdf9a60065b 327 ```cpp
kadonotakashi 0:8fdf9a60065b 328 void parse_ndef_message(const ac_buffer_t &buffer);
kadonotakashi 0:8fdf9a60065b 329 void build_ndef_message(ac_buffer_builder_t &buffer_builder);
kadonotakashi 0:8fdf9a60065b 330 ndef_msg_t *ndef_message();
kadonotakashi 0:8fdf9a60065b 331 ```
kadonotakashi 0:8fdf9a60065b 332
kadonotakashi 0:8fdf9a60065b 333 API implemented by descendant classes:
kadonotakashi 0:8fdf9a60065b 334 ```cpp
kadonotakashi 0:8fdf9a60065b 335 virtual NFCNDEFCapable::Delegate *ndef_capable_delegate();
kadonotakashi 0:8fdf9a60065b 336 ```
kadonotakashi 0:8fdf9a60065b 337
kadonotakashi 0:8fdf9a60065b 338 **Delegate**
kadonotakashi 0:8fdf9a60065b 339
kadonotakashi 0:8fdf9a60065b 340 The instance receives requests to encode and decode NDEF messages, and the user can choose how to handle them using the relevant builders and parsers.
kadonotakashi 0:8fdf9a60065b 341
kadonotakashi 0:8fdf9a60065b 342 ```cpp
kadonotakashi 0:8fdf9a60065b 343 void parse_ndef_message(const Span<const uint8_t> &buffer);
kadonotakashi 0:8fdf9a60065b 344 ```
kadonotakashi 0:8fdf9a60065b 345
kadonotakashi 0:8fdf9a60065b 346 The user receives the encoded NDEF message for processing.
kadonotakashi 0:8fdf9a60065b 347
kadonotakashi 0:8fdf9a60065b 348 ```cpp
kadonotakashi 0:8fdf9a60065b 349 size_t build_ndef_message(const Span<uint8_t> &buffer);
kadonotakashi 0:8fdf9a60065b 350 ```
kadonotakashi 0:8fdf9a60065b 351
kadonotakashi 0:8fdf9a60065b 352 The user can encode a NDEF message in the buffer provided and return its size (or 0).
kadonotakashi 0:8fdf9a60065b 353
kadonotakashi 0:8fdf9a60065b 354 #### NFC remote initiator
kadonotakashi 0:8fdf9a60065b 355
kadonotakashi 0:8fdf9a60065b 356 This class derives from the base `NFCRemoteEndpoint` and `NFCNDEFCapable` classes.
kadonotakashi 0:8fdf9a60065b 357
kadonotakashi 0:8fdf9a60065b 358 ```cpp
kadonotakashi 0:8fdf9a60065b 359 enum nfc_tag_type_t {
kadonotakashi 0:8fdf9a60065b 360 nfc_tag_type_1,
kadonotakashi 0:8fdf9a60065b 361 nfc_tag_type_2,
kadonotakashi 0:8fdf9a60065b 362 nfc_tag_type_3,
kadonotakashi 0:8fdf9a60065b 363 nfc_tag_type_4a,
kadonotakashi 0:8fdf9a60065b 364 nfc_tag_type_4b,
kadonotakashi 0:8fdf9a60065b 365 nfc_tag_type_5
kadonotakashi 0:8fdf9a60065b 366 };
kadonotakashi 0:8fdf9a60065b 367 ```
kadonotakashi 0:8fdf9a60065b 368
kadonotakashi 0:8fdf9a60065b 369 ```cpp
kadonotakashi 0:8fdf9a60065b 370 nfc_tag_type_t nfc_tag_type();
kadonotakashi 0:8fdf9a60065b 371 ```
kadonotakashi 0:8fdf9a60065b 372
kadonotakashi 0:8fdf9a60065b 373 Additionally, the user can recover the type of NFC tag (1 to 5) being emulated. Type 4 is implemented on either one of two technologies; therefore, this enum both includes type 4a and type 4b to identify the underlying technology.
kadonotakashi 0:8fdf9a60065b 374
kadonotakashi 0:8fdf9a60065b 375 *Note: ISO7816 is only used internally for the initial release*
kadonotakashi 0:8fdf9a60065b 376
kadonotakashi 0:8fdf9a60065b 377 ```cpp
kadonotakashi 0:8fdf9a60065b 378 bool is_iso7816_supported();
kadonotakashi 0:8fdf9a60065b 379 void add_iso7816_application(nfc_tech_iso7816_app_t *application);
kadonotakashi 0:8fdf9a60065b 380 ```
kadonotakashi 0:8fdf9a60065b 381
kadonotakashi 0:8fdf9a60065b 382 If the underlying technology supports it (ISO-DEP), the user can emulate a contactless smartcard and register ISO7816-4 applications using this API.
kadonotakashi 0:8fdf9a60065b 383
kadonotakashi 0:8fdf9a60065b 384 **Delegate**
kadonotakashi 0:8fdf9a60065b 385
kadonotakashi 0:8fdf9a60065b 386 The delegate derives from delegates of `NFCRemoteEndpoint` and `NFCNDEFCapable`.
kadonotakashi 0:8fdf9a60065b 387
kadonotakashi 0:8fdf9a60065b 388 #### NFC target
kadonotakashi 0:8fdf9a60065b 389
kadonotakashi 0:8fdf9a60065b 390 This is the base class for NFC targets that can be of two types:
kadonotakashi 0:8fdf9a60065b 391
kadonotakashi 0:8fdf9a60065b 392 - NFC EEPROMs (Dual-interface wired devices).
kadonotakashi 0:8fdf9a60065b 393 - Remote NFC targets (NFC devices over NFC RF interface).
kadonotakashi 0:8fdf9a60065b 394
kadonotakashi 0:8fdf9a60065b 395 Apart from the actual transport (wired or NFC), the use is similar, which explains why these methods are shared across these devices types.
kadonotakashi 0:8fdf9a60065b 396
kadonotakashi 0:8fdf9a60065b 397 This class derives from `NFCNDEFCapable`.
kadonotakashi 0:8fdf9a60065b 398
kadonotakashi 0:8fdf9a60065b 399 ```cpp
kadonotakashi 0:8fdf9a60065b 400 void write_ndef_message();
kadonotakashi 0:8fdf9a60065b 401 void erase_ndef_message();
kadonotakashi 0:8fdf9a60065b 402 void read_ndef_message();
kadonotakashi 0:8fdf9a60065b 403 ```
kadonotakashi 0:8fdf9a60065b 404
kadonotakashi 0:8fdf9a60065b 405 The user can trigger the appropriate NDEF parsing/building process using these methods if handlers are registered in the `NFCNDEFCapable` instance.
kadonotakashi 0:8fdf9a60065b 406
kadonotakashi 0:8fdf9a60065b 407 **Delegate**
kadonotakashi 0:8fdf9a60065b 408
kadonotakashi 0:8fdf9a60065b 409 ```cpp
kadonotakashi 0:8fdf9a60065b 410 void on_ndef_message_erased(nfc_err_t result);
kadonotakashi 0:8fdf9a60065b 411 void on_ndef_message_written(nfc_err_t result);
kadonotakashi 0:8fdf9a60065b 412 void on_ndef_message_read(nfc_err_t result);
kadonotakashi 0:8fdf9a60065b 413 ```
kadonotakashi 0:8fdf9a60065b 414
kadonotakashi 0:8fdf9a60065b 415 #### NFC EEPROM
kadonotakashi 0:8fdf9a60065b 416
kadonotakashi 0:8fdf9a60065b 417 The `NFCEEPROM` class derives from `NFCTarget` and shares the same API. The user must pass a pointer to a `NFCEEPROMDriver` instance (see below) in the constructor.
kadonotakashi 0:8fdf9a60065b 418
kadonotakashi 0:8fdf9a60065b 419 #### NFC remote target
kadonotakashi 0:8fdf9a60065b 420
kadonotakashi 0:8fdf9a60065b 421 *Note: This is out of scope for the initial release*
kadonotakashi 0:8fdf9a60065b 422
kadonotakashi 0:8fdf9a60065b 423 The `NFCRemoteTarget` class derives from `NFCTarget` and additionally from `NFCRemoteEndpoint`.
kadonotakashi 0:8fdf9a60065b 424
kadonotakashi 0:8fdf9a60065b 425 ## NDEF API
kadonotakashi 0:8fdf9a60065b 426
kadonotakashi 0:8fdf9a60065b 427 The NDEF API is constructed with these requirements:
kadonotakashi 0:8fdf9a60065b 428
kadonotakashi 0:8fdf9a60065b 429 - Minimizing memory allocation and copies.
kadonotakashi 0:8fdf9a60065b 430 - NFC Forum compliance.
kadonotakashi 0:8fdf9a60065b 431 - Ease of use.
kadonotakashi 0:8fdf9a60065b 432 - Extensibility.
kadonotakashi 0:8fdf9a60065b 433
kadonotakashi 0:8fdf9a60065b 434 ### Common objects
kadonotakashi 0:8fdf9a60065b 435
kadonotakashi 0:8fdf9a60065b 436 We will provide multiple helpers to make it easy to create and parse common record types:
kadonotakashi 0:8fdf9a60065b 437
kadonotakashi 0:8fdf9a60065b 438 - URI.
kadonotakashi 0:8fdf9a60065b 439 - Text.
kadonotakashi 0:8fdf9a60065b 440 - Smart poster.
kadonotakashi 0:8fdf9a60065b 441 - MIME data.
kadonotakashi 0:8fdf9a60065b 442
kadonotakashi 0:8fdf9a60065b 443 For instance, the `URI`'s class API is:
kadonotakashi 0:8fdf9a60065b 444
kadonotakashi 0:8fdf9a60065b 445 ```cpp
kadonotakashi 0:8fdf9a60065b 446 void set_uri(uri_identifier_code_t id, const Span<const uint8_t> &uri_field)
kadonotakashi 0:8fdf9a60065b 447
kadonotakashi 0:8fdf9a60065b 448 uri_identifier_code_t get_id() const;
kadonotakashi 0:8fdf9a60065b 449 Span<const uint8_t> get_uri_field() const;
kadonotakashi 0:8fdf9a60065b 450 ```
kadonotakashi 0:8fdf9a60065b 451
kadonotakashi 0:8fdf9a60065b 452 **Note:** These types can be replaced by user defined ones if parsing and serialization logic is provided.
kadonotakashi 0:8fdf9a60065b 453
kadonotakashi 0:8fdf9a60065b 454 ### Parsing
kadonotakashi 0:8fdf9a60065b 455
kadonotakashi 0:8fdf9a60065b 456 #### ndef::MessageParser
kadonotakashi 0:8fdf9a60065b 457
kadonotakashi 0:8fdf9a60065b 458 ![ndef_message_parser_diagram]
kadonotakashi 0:8fdf9a60065b 459
kadonotakashi 0:8fdf9a60065b 460 A `MessageParser`, which produces `Record` instances to its client, parses messages incoming from the peer. The parsing operation is event-driven: A message parser client registers a delegate inside the message parser. This delegate is notified whenever an interesting event happens during the parsing.
kadonotakashi 0:8fdf9a60065b 461
kadonotakashi 0:8fdf9a60065b 462 ```cpp
kadonotakashi 0:8fdf9a60065b 463 void set_delegate(Delegate *delegate);
kadonotakashi 0:8fdf9a60065b 464 void parse(const Span<const uint8_t> &data_buffer);
kadonotakashi 0:8fdf9a60065b 465 ```
kadonotakashi 0:8fdf9a60065b 466
kadonotakashi 0:8fdf9a60065b 467 It is important to note that the data_buffer in the entry of the parse function must contain the entire NDEF message.
kadonotakashi 0:8fdf9a60065b 468
kadonotakashi 0:8fdf9a60065b 469 ##### ndef::MessageParser::Delegate
kadonotakashi 0:8fdf9a60065b 470
kadonotakashi 0:8fdf9a60065b 471 ```cpp
kadonotakashi 0:8fdf9a60065b 472 virtual void on_parsing_started() { }
kadonotakashi 0:8fdf9a60065b 473 virtual void on_record_parsed(const Record &record) { }
kadonotakashi 0:8fdf9a60065b 474 virtual void on_parsing_terminated() { }
kadonotakashi 0:8fdf9a60065b 475 virtual void on_parsing_error(MessageParser::error_t error) { }
kadonotakashi 0:8fdf9a60065b 476 ```
kadonotakashi 0:8fdf9a60065b 477
kadonotakashi 0:8fdf9a60065b 478 The delegate is notified by the parser when the parsing starts or ends, when an error is encountered or when an NDEF `Record` has been parsed.
kadonotakashi 0:8fdf9a60065b 479
kadonotakashi 0:8fdf9a60065b 480 To reduce memory consumption, `Record` instances generated by the parser are short lived. They are only valid during the callback invocation. If a client is interested in the content of a message parsed and wants to use it after the parsing callback, then it must make a copy of the record object.
kadonotakashi 0:8fdf9a60065b 481
kadonotakashi 0:8fdf9a60065b 482 #### NDEF record parsing
kadonotakashi 0:8fdf9a60065b 483
kadonotakashi 0:8fdf9a60065b 484 ![ndef_record_parser_diagram]
kadonotakashi 0:8fdf9a60065b 485
kadonotakashi 0:8fdf9a60065b 486 NDEF records can contain any type of content. Therefore, parsing of records is specific to the application. To help the developer, an optional NDEF record parsing framework is included. It follows the _chain-of-responsibility_ design pattern that facilitates the integration of record parsers defined by client code.
kadonotakashi 0:8fdf9a60065b 487
kadonotakashi 0:8fdf9a60065b 488 ##### ndef::RecordParser
kadonotakashi 0:8fdf9a60065b 489
kadonotakashi 0:8fdf9a60065b 490 It is the base building block of the record parsing frame working. It parses a record then returns true if the record has been parsed or false otherwise.
kadonotakashi 0:8fdf9a60065b 491
kadonotakashi 0:8fdf9a60065b 492 ```cpp
kadonotakashi 0:8fdf9a60065b 493 virtual bool parse(const Record&);
kadonotakashi 0:8fdf9a60065b 494 ```
kadonotakashi 0:8fdf9a60065b 495
kadonotakashi 0:8fdf9a60065b 496 ##### ndef::RecordParserChain
kadonotakashi 0:8fdf9a60065b 497
kadonotakashi 0:8fdf9a60065b 498 It aggregates `RecordParser` instances and defers parsing to the instances it contains.
kadonotakashi 0:8fdf9a60065b 499
kadonotakashi 0:8fdf9a60065b 500 ```cpp
kadonotakashi 0:8fdf9a60065b 501 bool parse(const Record &record);
kadonotakashi 0:8fdf9a60065b 502 void set_next_parser(RecordParser *parser);
kadonotakashi 0:8fdf9a60065b 503 ```
kadonotakashi 0:8fdf9a60065b 504
kadonotakashi 0:8fdf9a60065b 505 ##### ndef::GenericRecordParser<ParserImplementation, ParsingResult>
kadonotakashi 0:8fdf9a60065b 506
kadonotakashi 0:8fdf9a60065b 507 This is a partial implementation of the `RecordParser` interface. It exposes a delegate type that clients of this parser can implement and register. This delegate expects objects of the parsing result type.
kadonotakashi 0:8fdf9a60065b 508
kadonotakashi 0:8fdf9a60065b 509 ```cpp
kadonotakashi 0:8fdf9a60065b 510 bool parse(const Record&)
kadonotakashi 0:8fdf9a60065b 511 void set_delegate(Delegate *delegate)
kadonotakashi 0:8fdf9a60065b 512 ```
kadonotakashi 0:8fdf9a60065b 513
kadonotakashi 0:8fdf9a60065b 514 Implementation of this class must expose the following nonvirtual function:
kadonotakashi 0:8fdf9a60065b 515
kadonotakashi 0:8fdf9a60065b 516 ```c++
kadonotakashi 0:8fdf9a60065b 517 bool do_parse(const Record &record, ParsingResult &parsing_result);
kadonotakashi 0:8fdf9a60065b 518 ```
kadonotakashi 0:8fdf9a60065b 519
kadonotakashi 0:8fdf9a60065b 520 If the parsing is successful, then it should return true and fill `parsing_result`; otherwise, it should return false and leave `parsing_result` untouched.
kadonotakashi 0:8fdf9a60065b 521
kadonotakashi 0:8fdf9a60065b 522 **Note:** The Curiously recurring template pattern (CRTP) is used to implement the delegation mechanism in a type-safe fashion. This is not achievable with _regular_ polymorphism.
kadonotakashi 0:8fdf9a60065b 523
kadonotakashi 0:8fdf9a60065b 524 ###### ndef::GenericRecordParser<ParserImplementation, ParsingResult>::Delegate
kadonotakashi 0:8fdf9a60065b 525
kadonotakashi 0:8fdf9a60065b 526 Clients of this class must implement this delegate. It receives the objects parsed.
kadonotakashi 0:8fdf9a60065b 527
kadonotakashi 0:8fdf9a60065b 528 ```cpp
kadonotakashi 0:8fdf9a60065b 529 virtual void on_record_parsed(const ParsingResult &record, const RecordID *id);
kadonotakashi 0:8fdf9a60065b 530 ```
kadonotakashi 0:8fdf9a60065b 531
kadonotakashi 0:8fdf9a60065b 532 **Note:** Usually, clients are client of an implementation of an ndef::GenericRecordParser<ParserImplementation, ParsingResult> . They can refer to the delegate as `ImplementationName::Delegate`.
kadonotakashi 0:8fdf9a60065b 533
kadonotakashi 0:8fdf9a60065b 534 #### Common parsers
kadonotakashi 0:8fdf9a60065b 535
kadonotakashi 0:8fdf9a60065b 536 ![ndef_common_parsers_diagram]
kadonotakashi 0:8fdf9a60065b 537
kadonotakashi 0:8fdf9a60065b 538 Parsers for each common record type exist. They inherit from the `GenericRecordParser` to exposes a common delegate interface:
kadonotakashi 0:8fdf9a60065b 539
kadonotakashi 0:8fdf9a60065b 540 ```cpp
kadonotakashi 0:8fdf9a60065b 541 virtual void on_record_parsed(const <ParsedType> &result, const ndef::RecordID *id)
kadonotakashi 0:8fdf9a60065b 542 ```
kadonotakashi 0:8fdf9a60065b 543
kadonotakashi 0:8fdf9a60065b 544 #### Simple parser
kadonotakashi 0:8fdf9a60065b 545
kadonotakashi 0:8fdf9a60065b 546 The APIs provide a class named `SimpleMessageParser` that glues together a `MessageParser` and a chain `RecordParser` containing the parsers for the common types.
kadonotakashi 0:8fdf9a60065b 547
kadonotakashi 0:8fdf9a60065b 548 ![ndef_simple_parser_diagram]
kadonotakashi 0:8fdf9a60065b 549
kadonotakashi 0:8fdf9a60065b 550 Clients of the class can register a delegate, parse a message or add a new `RecordParser` in the parsing chain.
kadonotakashi 0:8fdf9a60065b 551
kadonotakashi 0:8fdf9a60065b 552 ```cpp
kadonotakashi 0:8fdf9a60065b 553 void set_delegate(Delegate *delegate);
kadonotakashi 0:8fdf9a60065b 554 void parse(const Span<const uint8_t> &data_buffer);
kadonotakashi 0:8fdf9a60065b 555 void add_record_parser(ndef::RecordParser *parser);
kadonotakashi 0:8fdf9a60065b 556 ```
kadonotakashi 0:8fdf9a60065b 557
kadonotakashi 0:8fdf9a60065b 558 ##### Delegate
kadonotakashi 0:8fdf9a60065b 559
kadonotakashi 0:8fdf9a60065b 560 Clients of this class must implement this delegate. It receives events from the parsing process:
kadonotakashi 0:8fdf9a60065b 561
kadonotakashi 0:8fdf9a60065b 562 ```cpp
kadonotakashi 0:8fdf9a60065b 563 virtual void on_parsing_error(ndef::MessageParser::error_t error);
kadonotakashi 0:8fdf9a60065b 564 virtual void on_parsing_started();
kadonotakashi 0:8fdf9a60065b 565 virtual void on_text_parsed(const Text& text, const ndef::RecordID &id);
kadonotakashi 0:8fdf9a60065b 566 virtual void on_mime_parsed(const Mime& text, const ndef::RecordID &id);
kadonotakashi 0:8fdf9a60065b 567 virtual void on_uri_parsed(const URI& uri, const ndef::RecordID &id);
kadonotakashi 0:8fdf9a60065b 568 virtual void on_unknown_record_parsed(const ndef::Record &record);
kadonotakashi 0:8fdf9a60065b 569 virtual void on_parsing_terminated();
kadonotakashi 0:8fdf9a60065b 570 ```
kadonotakashi 0:8fdf9a60065b 571
kadonotakashi 0:8fdf9a60065b 572 ### Serialization
kadonotakashi 0:8fdf9a60065b 573
kadonotakashi 0:8fdf9a60065b 574 The class `MessageBuilder` is used to map a record into an NDEF message. It includes a data buffer that contains the _raw_ message. Client code uses the function `append_record` to append a new record into the message being built.
kadonotakashi 0:8fdf9a60065b 575
kadonotakashi 0:8fdf9a60065b 576 ![ndef_message_builder_diagram]
kadonotakashi 0:8fdf9a60065b 577
kadonotakashi 0:8fdf9a60065b 578 For convenience, serialization functions for common types are provided in the common types we provide.
kadonotakashi 0:8fdf9a60065b 579
kadonotakashi 0:8fdf9a60065b 580 ## HAL APIs
kadonotakashi 0:8fdf9a60065b 581
kadonotakashi 0:8fdf9a60065b 582 ### NFC EEPROM API
kadonotakashi 0:8fdf9a60065b 583
kadonotakashi 0:8fdf9a60065b 584 To create the hardware-specific APIs to add support for a new NFC EEPROM, vendors need to derive from `NFCEEPROMDriver` and implement its virtual methods.
kadonotakashi 0:8fdf9a60065b 585
kadonotakashi 0:8fdf9a60065b 586 From the upper layer's point of view, the EEPROM is a byte array that can be read from or written to. Long operations (reads, writes, erasures) must happen asynchronously. Booleans indicate whether a particular operation was succesful.
kadonotakashi 0:8fdf9a60065b 587
kadonotakashi 0:8fdf9a60065b 588 Address 0 means the start of the NDEF buffer (not necessarily at address 0 in the EEPROM).
kadonotakashi 0:8fdf9a60065b 589
kadonotakashi 0:8fdf9a60065b 590 When a buffer is passed to the backend, the reference remains valid until the corresponding event is called.
kadonotakashi 0:8fdf9a60065b 591
kadonotakashi 0:8fdf9a60065b 592 The `set_size()` command is called to change the size of the NDEF buffer (within the limits set by `get_max_size()`). Inversely, that buffer size can be read using `get_size()`.
kadonotakashi 0:8fdf9a60065b 593
kadonotakashi 0:8fdf9a60065b 594 `start_session()` and `end_session()` are used before a series of memory operations to allow the driver to lock or unlock the RF interface during these operations to avoid having concurrent access to the memory.
kadonotakashi 0:8fdf9a60065b 595
kadonotakashi 0:8fdf9a60065b 596 ```cpp
kadonotakashi 0:8fdf9a60065b 597 void reset();
kadonotakashi 0:8fdf9a60065b 598 size_t get_max_size();
kadonotakashi 0:8fdf9a60065b 599 void start_session(bool force = true);
kadonotakashi 0:8fdf9a60065b 600 void end_session();
kadonotakashi 0:8fdf9a60065b 601 void read_bytes(uint32_t address, size_t count);
kadonotakashi 0:8fdf9a60065b 602 void write_bytes(uint32_t address, const uint8_t *bytes, size_t count);
kadonotakashi 0:8fdf9a60065b 603 void read_size(size_t count);
kadonotakashi 0:8fdf9a60065b 604 void write_size();
kadonotakashi 0:8fdf9a60065b 605 void erase_bytes(uint32_t address, size_t size)
kadonotakashi 0:8fdf9a60065b 606 ```
kadonotakashi 0:8fdf9a60065b 607
kadonotakashi 0:8fdf9a60065b 608 The following events must be called to signal completion of long operations:
kadonotakashi 0:8fdf9a60065b 609
kadonotakashi 0:8fdf9a60065b 610 ```cpp
kadonotakashi 0:8fdf9a60065b 611 void on_session_started(bool success);
kadonotakashi 0:8fdf9a60065b 612 void on_session_ended(bool success);
kadonotakashi 0:8fdf9a60065b 613 void on_bytes_read(size_t count);
kadonotakashi 0:8fdf9a60065b 614 void on_bytes_written(size_t count);
kadonotakashi 0:8fdf9a60065b 615 void on_size_read(bool success, size_t size);
kadonotakashi 0:8fdf9a60065b 616 void on_size_written(bool success);
kadonotakashi 0:8fdf9a60065b 617 void on_bytes_erased(size_t count);
kadonotakashi 0:8fdf9a60065b 618 ```
kadonotakashi 0:8fdf9a60065b 619
kadonotakashi 0:8fdf9a60065b 620 The implementation also has access to an event queue in case asynchronous operations need to be run:
kadonotakashi 0:8fdf9a60065b 621
kadonotakashi 0:8fdf9a60065b 622 ```cpp
kadonotakashi 0:8fdf9a60065b 623 Delegate *delegate();
kadonotakashi 0:8fdf9a60065b 624 events::EventQueue *event_queue();
kadonotakashi 0:8fdf9a60065b 625 ```
kadonotakashi 0:8fdf9a60065b 626
kadonotakashi 0:8fdf9a60065b 627 ### NCI driver APIs
kadonotakashi 0:8fdf9a60065b 628
kadonotakashi 0:8fdf9a60065b 629 This API will be defined in phase 2.
kadonotakashi 0:8fdf9a60065b 630
kadonotakashi 0:8fdf9a60065b 631 # Testing strategy
kadonotakashi 0:8fdf9a60065b 632
kadonotakashi 0:8fdf9a60065b 633 ## NFC forum compliance
kadonotakashi 0:8fdf9a60065b 634
kadonotakashi 0:8fdf9a60065b 635 A dongle driven by [PyNFC](https://nfcpy.readthedocs.io/en/latest/index.html) will be used to run Greentea-based tests to ensure that the implementation behaves correctly for a range of system tests.
kadonotakashi 0:8fdf9a60065b 636
kadonotakashi 0:8fdf9a60065b 637 Unit tests will cover all internal logic and NFC endpoints can be mocked/emulated where possible.
kadonotakashi 0:8fdf9a60065b 638
kadonotakashi 0:8fdf9a60065b 639 In the future, we could run NFC Forum test suites using approved testing equipment.
kadonotakashi 0:8fdf9a60065b 640
kadonotakashi 0:8fdf9a60065b 641 ## Interoperability
kadonotakashi 0:8fdf9a60065b 642
kadonotakashi 0:8fdf9a60065b 643 Interoperability is important with a technology such as NFC. Therefore, our testing rig will include a selection of smartphones and NFC tags that can be connected using analog switches to the relevant NFC-enabled platform running Mbed OS.
kadonotakashi 0:8fdf9a60065b 644
kadonotakashi 0:8fdf9a60065b 645 ![interop_test_rig]
kadonotakashi 0:8fdf9a60065b 646
kadonotakashi 0:8fdf9a60065b 647 ## HAL testing
kadonotakashi 0:8fdf9a60065b 648
kadonotakashi 0:8fdf9a60065b 649 Greentea tests will be provided to Partners to ensure compliance with the NFC EEPROM backend API.
kadonotakashi 0:8fdf9a60065b 650
kadonotakashi 0:8fdf9a60065b 651 # Dependencies
kadonotakashi 0:8fdf9a60065b 652
kadonotakashi 0:8fdf9a60065b 653 - Event Queue
kadonotakashi 0:8fdf9a60065b 654
kadonotakashi 0:8fdf9a60065b 655 There are currently at least four event queues (Plaftorm, BLE, USB and IP) in Mbed OS, and NFC will also require an event queing mechanism. We should try to reuse one of these existing queues with the longterm goal of unifying these code bases.
kadonotakashi 0:8fdf9a60065b 656
kadonotakashi 0:8fdf9a60065b 657 [phase_1_architecture]: phase_1_architecture.png
kadonotakashi 0:8fdf9a60065b 658 [phase_2_architecture]: phase_2_architecture.png
kadonotakashi 0:8fdf9a60065b 659 [nfc_controller_diagram]: uml_diagram_controller.png
kadonotakashi 0:8fdf9a60065b 660 [nfc_endpoints_diagram]: uml_diagram_endpoints.png
kadonotakashi 0:8fdf9a60065b 661 [interop_test_rig]: interop_test_rig.png
kadonotakashi 0:8fdf9a60065b 662 [ndef_message_parser_diagram]: uml_diagram_ndef_message_parser.png
kadonotakashi 0:8fdf9a60065b 663 [ndef_record_parser_diagram]: uml_diagram_ndef_record_parser.png
kadonotakashi 0:8fdf9a60065b 664 [ndef_common_parsers_diagram]: uml_diagram_ndef_common_parsers.png
kadonotakashi 0:8fdf9a60065b 665 [ndef_simple_parser_diagram]: uml_diagram_ndef_simple_parser.png
kadonotakashi 0:8fdf9a60065b 666 [ndef_message_builder_diagram]: uml_diagram_ndef_message_builder_diagram.png