Skip to content

Commit

Permalink
Merge pull request #59 from ElectronicCats/refactor
Browse files Browse the repository at this point in the history
Refactor NDEF examples with new methods
  • Loading branch information
sabas1080 authored Dec 29, 2023
2 parents ae54185 + 1df0e56 commit d5a3fe7
Show file tree
Hide file tree
Showing 21 changed files with 1,318 additions and 179 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
src/sync.ffs_db

.DS_Store
.vscode/
.vscode/

# Firmware binaries
/examples/**/build/
312 changes: 301 additions & 11 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Include and instantiate the Electroniccats_PN7150 class. Creates a global NFC de
#include <Electroniccats_PN7150.h>

Electroniccats_PN7150 nfc(PN7150_IRQ, PN7150_VEN, PN7150_ADDR);

```
- `uint8_t PN7150_IRQ`: IRQ pin for data interrupt.
Expand Down Expand Up @@ -1255,12 +1254,12 @@ void setup() {
}
```

### Method: `getContentSize`
### Method: `getContentLength`

Get the content size of the message.
Get the content length of the message.

```cpp
static unsigned short getContentSize();
static unsigned short getContentLength();
```

### Method: `getContent`
Expand All @@ -1276,7 +1275,7 @@ static unsigned char *getContent();
Set the content of the message.

```cpp
static void setContent(unsigned char *content, unsigned short contentSize);
static void setContent(unsigned char *content, unsigned short contentLength);
```
### Method: `getRecord`
Expand All @@ -1293,7 +1292,7 @@ The record is a structure that contains the following properties:
typedef struct {
NdefRecordType_e recordType;
unsigned char *recordPayload;
unsigned int recordPayloadSize;
unsigned int recordPayloadLength;
} NdefRecord_t;
```

Expand Down Expand Up @@ -1351,6 +1350,78 @@ if (message.hasRecord()) {
}
```

### Method: `addTextRecord`

Adds a text record to the message.

```cpp
void addTextRecord(String text);
void addTextRecord(String text, String languageCode);
```
#### Example 1
```cpp
message.addTextRecord("Hello"); // English by default
```

#### Example 2

```cpp
// English explicitly, the library only supports two letter language codes (ISO 639-1) by now
message.addTextRecord("world", "en");
```

### Method: `addUriRecord`

Adds a URI record to the message.

```cpp
void addUriRecord(String uri);
```
#### Example
```cpp
message.addUriRecord("https://www.electroniccats.com/");
```

See [URI prefixes](#uri-prefixes) for a list of all the prefixes that can be used.

### Method: `addMimeMediaRecord`

Adds a MIME media record to the message.

```cpp
void addMimeMediaRecord(String mimeType, const char *payload, unsigned short payloadLength);
```
#### Example
```cpp
message.addMimeMediaRecord("text/plain", "Hello world!", 12); // 12 is the payload length
```

### Method: `addWiFiRecord`

Adds a WiFi record to the message.

```cpp
void addWiFiRecord(String ssid, String authenticationType, String encryptionType, String password);
```
#### Example
```cpp
String ssid = "Bomber Cat";
String authentificationType = "WPA2 PERSONAL";
String encryptionType = "AES";
String password = "Password";
message.addWiFiRecord(ssid, authentificationType, encryptionType, password);
```

See [WiFi authentication types](#wifi-authentication-types) and [WiFi encryption types](#wifi-encryption-types) for a list of all the types that can be used.

## Class NdefRecord

A `NdefRecord` object represents an NDEF record. An NDEF record is a data structure that contains data that is stored or transported in an NDEF message.
Expand Down Expand Up @@ -1436,12 +1507,12 @@ typedef enum {
} NdefRecordType_e;
```

### Method: `getPayloadSize`
### Method: `getPayloadLength`

Get the payload size of the record.
Get the payload length of the record.

```cpp
unsigned short getPayloadSize();
unsigned short getPayloadLength();
```

### Method: `getPayload`
Expand All @@ -1456,7 +1527,7 @@ unsigned char *getPayload();

```cpp
Serial.print("Payload: ");
for (int i = 0; i < record.getPayloadSize(); i++) {
for (int i = 0; i < record.getPayloadLength(); i++) {
Serial.print(record.getPayload()[i], HEX);
Serial.print(" ");
}
Expand Down Expand Up @@ -1596,4 +1667,223 @@ String getUri();
```cpp
Serial.print("URI: ");
Serial.println(record.getUri());
```
```

### Method: `setPayload`

Set the payload of the record.

```cpp
void setPayload(String payload);
void setPayload(const char *payload, unsigned short payloadLength);
```
#### Example 1
```cpp
record.setPayload("Hello world!");
```

#### Example 2

```cpp
record.setPayload("Hello world!", 12);
```

### Method: `setPayloadLength`

Set the payload length of the record.

```cpp
void setPayloadLength(uint8_t payloadLength);
```
#### Example
```cpp
record.setPayloadLength(12);
```

### Method: `setHeaderFlags`

Set the header flags of the record.

```cpp
void setHeaderFlags(uint8_t headerFlags);
```
#### Example
```cpp
record.setHeaderFlags(0x00);
```

### Method: `setTypeLength`

Set the type length of the record.

```cpp
void setTypeLength(uint8_t typeLength);
```
#### Example
```cpp
record.setTypeLength(0x00);
```

### Method: `setRecordType`

Set the record type of the record.

```cpp
void setRecordType(uint8_t wellKnownType);
void setRecordType(String type);
```
#### Example 1
```cpp
record.setRecordType(NDEF_TEXT_RECORD_TYPE);
```

#### Example 2

```cpp
record.setRecordType("application/vnd.wfa.wsc");
```

### Method: `setStatus`

Set the status of the record.

```cpp
void setStatus(uint8_t status);
```
#### Example
```cpp
record.setStatus(NDEF_STATUS);
```

### Method: `setLanguageCode`

Set the language code of the record.

```cpp
void setLanguageCode(String languageCode);
```
#### Example
```cpp
record.setLanguageCode("en");
```

### Method: `getContentLength`

Get the content length of the record.

```cpp
unsigned short getContentLength();
```

#### Example

```cpp
Serial.print("Content length: ");
Serial.println(record.getContentLength());
```

### Method: `getContent`

Get the content of the record.

```cpp
const char *getContent();
```

#### Example

```cpp
Serial.print("Content: ");
for (int i = 0; i < record.getContentLength(); i++) {
Serial.print(record.getContent()[i], HEX);
Serial.print(" ");
}
Serial.println();
```

## Appendix

### URI prefixes

Here is a list of all the prefixes that can be used with the [`addUriRecord`](#method-addurirecord) method.

| Prefix | Meaning |
| --- | --- |
| `0x00` | No prepending is done, the URI is encoded in its entirety in the record payload |
| `0x01` | `http://www.` |
| `0x02` | `https://www.` |
| `0x03` | `http://` |
| `0x04` | `https://` |
| `0x05` | `tel:` |
| `0x06` | `mailto:` |
| `0x07` | `ftp://anonymous:anonymous@` |
| `0x08` | `ftp://ftp.` |
| `0x09` | `ftps://` |
| `0x0A` | `sftp://` |
| `0x0B` | `smb://` |
| `0x0C` | `nfs://` |
| `0x0D` | `ftp://` |
| `0x0E` | `dav://` |
| `0x0F` | `news:` |
| `0x10` | `telnet://` |
| `0x11` | `imap:` |
| `0x12` | `rtsp:` |
| `0x13` | `urn:` |
| `0x14` | `pop:` |
| `0x15` | `sip:` |
| `0x16` | `sips:` |
| `0x17` | `tftp:` |
| `0x18` | `btspp://` |
| `0x19` | `btl2cap://` |
| `0x1A` | `btgoep://` |
| `0x1B` | `tcpobex://` |
| `0x1C` | `irdaobex://` |
| `0x1D` | `file://` |
| `0x1E` | `urn:epc:id:` |
| `0x1F` | `urn:epc:tag:` |
| `0x20` | `urn:epc:pat:` |
| `0x21` | `urn:epc:raw:` |
| `0x22` | `urn:epc:` |
| `0x23` | `urn:nfc:` |

### WiFi authentication types

Here is a list of all the authentication types that can be used with the [`addWiFiRecord`](#method-addwifirecord) method.

| Authentication type | Meaning |
| --- | --- |
| `"OPEN"` | Open |
| `"WPA PERSONAL"` | WPA Personal |
| `"SHARED"` | Shared |
| `"WPA ENTERPRISE"` | WPA Enterprise |
| `"WPA2 ENTERPRISE"` | WPA2 Enterprise |
| `"WPA2 PERSONAL"` | WPA2 Personal |

Any other value will be interpreted as `UNKNOWN`.

### WiFi encryption types

Here is a list of all the encryption types that can be used with the [`addWiFiRecord`](#method-addwifirecord) method.

| Encryption type | Meaning |
| --- | --- |
| `"NONE"` | None |
| `"WEP"` | WEP |
| `"TKIP"` | TKIP |
| `"AES"` | AES |

Any other value will be interpreted as `UNKNOWN`.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BOARD_TAG = electroniccats:mbed_rp2040:bombercat
# BOARD_TAG = rp2040:rp2040:generic
MONITOR_PORT = /dev/cu.usbmodem1101
MONITOR_PORT = /dev/cu.usbmodem11101

compile:
arduino-cli compile --fqbn $(BOARD_TAG) --warnings all
Expand Down
Loading

0 comments on commit d5a3fe7

Please sign in to comment.