libctru  v2.4.1
nfc.h
Go to the documentation of this file.
1 /**
2  * @file nfc.h
3  * @brief NFC service. This can only be used with system-version >=9.3.0-X.
4  */
5 #pragma once
6 
7 /// This is returned when the current state is invalid for this command.
8 #define NFC_ERR_INVALID_STATE 0xC8A17600
9 
10 /// This is returned by nfcOpenAppData() when the appdata is uninitialized since nfcInitializeWriteAppData() wasn't used previously.
11 #define NFC_ERR_APPDATA_UNINITIALIZED 0xC8A17620
12 
13 /// This is returned by nfcGetAmiiboSettings() when the amiibo wasn't setup by the amiibo Settings applet.
14 #define NFC_ERR_AMIIBO_NOTSETUP 0xC8A17628
15 
16 /// This is returned by nfcOpenAppData() when the input AppID doesn't match the actual amiibo AppID.
17 #define NFC_ERR_APPID_MISMATCH 0xC8A17638
18 
19 /// "Returned for HMAC-hash mismatch(data corruption), with HMAC-calculation input_buffer_size=0x34."
20 #define NFC_ERR_DATACORRUPTION0 0xC8C1760C
21 
22 /// HMAC-hash mismatch with input_buffer_size=0x1DF, see here: https://www.3dbrew.org/wiki/Amiibo
23 #define NFC_ERR_DATACORRUPTION1 0xC8A17618
24 
25 /// This can be used for nfcStartScanning().
26 #define NFC_STARTSCAN_DEFAULTINPUT 0
27 
28 /// NFC operation type.
29 typedef enum {
30  NFC_OpType_1 = 1, /// Unknown.
31  NFC_OpType_NFCTag = 2, /// This is the default.
32  NFC_OpType_RawNFC = 3 /// Use Raw NFC tag commands. Only available with >=10.0.0-X.
34 
35 typedef enum {
36  NFC_TagState_Uninitialized = 0, /// nfcInit() was not used yet.
37  NFC_TagState_ScanningStopped = 1, /// Not currently scanning for NFC tags. Set by nfcStopScanning() and nfcInit(), when successful.
38  NFC_TagState_Scanning = 2, /// Currently scanning for NFC tags. Set by nfcStartScanning() when successful.
39  NFC_TagState_InRange = 3, /// NFC tag is in range. The state automatically changes to this when the state was previously value 2, without using any NFC service commands.
40  NFC_TagState_OutOfRange = 4, /// NFC tag is now out of range, where the NFC tag was previously in range. This occurs automatically without using any NFC service commands. Once this state is entered, it won't automatically change to anything else when the tag is moved in range again. Hence, if you want to keep doing tag scanning after this, you must stop+start scanning.
41  NFC_TagState_DataReady = 5 /// NFC tag data was successfully loaded. This is set by nfcLoadAmiiboData() when successful.
43 
44 /// Bit4-7 are always clear with nfcGetAmiiboSettings() due to "& 0xF".
45 enum {
46  NFC_amiiboFlag_Setup = BIT(4), /// This indicates that the amiibo was setup with amiibo Settings. nfcGetAmiiboSettings() will return an all-zero struct when this is not set.
47  NFC_amiiboFlag_AppDataSetup = BIT(5) /// This indicates that the AppData was previously initialized via nfcInitializeWriteAppData(), that function can't be used again with this flag already set.
48 };
49 
50 typedef struct {
51  u16 id_offset_size;/// "u16 size/offset of the below ID data. Normally this is 0x7. When this is <=10, this field is the size of the below ID data. When this is >10, this is the offset of the 10-byte ID data, relative to structstart+4+<offsetfield-10>. It's unknown in what cases this 10-byte ID data is used."
52  u8 unk_x2;//"Unknown u8, normally 0x0."
53  u8 unk_x3;//"Unknown u8, normally 0x2."
54  u8 id[0x28];//"ID data. When the above size field is 0x7, this is the 7-byte NFC tag UID, followed by all-zeros."
55 } NFC_TagInfo;
56 
57 /// AmiiboSettings structure, see also here: https://3dbrew.org/wiki/NFC:GetAmiiboSettings
58 typedef struct {
59  u8 mii[0x60];/// "Owner Mii."
60  u16 nickname[11];/// "UTF-16BE Amiibo nickname."
61  u8 flags;/// "This is plaintext_amiibosettingsdata[0] & 0xF." See also the NFC_amiiboFlag enums.
62  u8 countrycodeid;/// "This is plaintext_amiibosettingsdata[1]." "Country Code ID, from the system which setup this amiibo."
64  u8 setupdate_month;
65  u8 setupdate_day;
66  u8 unk_x7c[0x2c];//Normally all-zero?
68 
69 /// AmiiboConfig structure, see also here: https://3dbrew.org/wiki/NFC:GetAmiiboConfig
70 typedef struct {
71  u16 lastwritedate_year;
72  u8 lastwritedate_month;
73  u8 lastwritedate_day;
74  u16 write_counter;
75  u8 characterID[3];/// the first element is the collection ID, the second the character in this collection, the third the variant
76  u8 series;/// ID of the series
77  u16 amiiboID;/// ID shared by all exact same amiibo. Some amiibo are only distinguished by this one like regular SMB Series Mario and the gold one
78  u8 type;/// Type of amiibo 0 = figure, 1 = card, 2 = plush
80  u16 appdata_size;/// "NFC module writes hard-coded u8 value 0xD8 here. This is the size of the Amiibo AppData, apps can use this with the AppData R/W commands. ..."
81  u8 zeros[0x30];/// "Unused / reserved: this is cleared by NFC module but never written after that."
83 
84 /// Used by nfcInitializeWriteAppData() internally, see also here: https://3dbrew.org/wiki/NFC:GetAppDataInitStruct
85 typedef struct {
86  u8 data_x0[0xC];
87  u8 data_xc[0x30];/// "The data starting at struct offset 0xC is the 0x30-byte struct used by NFC:InitializeWriteAppData, sent by the user-process."
89 
90 /// Used by nfcWriteAppData() internally, see also: https://3dbrew.org/wiki/NFC:WriteAppData
91 typedef struct {
92  u8 id[10];//7-byte UID normally.
93  u8 id_size;
94  u8 unused_xb[0x15];
96 
97 /**
98  * @brief Initializes NFC.
99  * @param type See the NFC_OpType enum.
100  */
102 
103 /**
104  * @brief Shuts down NFC.
105  */
106 void nfcExit(void);
107 
108 /**
109  * @brief Gets the NFC service handle.
110  * @return The NFC service handle.
111  */
113 
114 /**
115  * @brief Starts scanning for NFC tags.
116  * @param inval Unknown. See NFC_STARTSCAN_DEFAULTINPUT.
117  */
119 
120 /**
121  * @brief Stops scanning for NFC tags.
122  */
123 void nfcStopScanning(void);
124 
125 /**
126  * @brief Read amiibo NFC data and load in memory.
127  */
129 
130 /**
131  * @brief If the tagstate is valid(NFC_TagState_DataReady or 6), it then sets the current tagstate to NFC_TagState_InRange.
132  */
134 
135 /**
136  * @brief This writes the amiibo data stored in memory to the actual amiibo data storage(which is normally the NFC data pages). This can only be used if NFC_LoadAmiiboData() was used previously.
137  */
139 
140 /**
141  * @brief Returns the current NFC tag state.
142  * @param state Pointer to write NFC tag state.
143  */
145 
146 /**
147  * @brief Returns the current TagInfo.
148  * @param out Pointer to write the output TagInfo.
149  */
151 
152 /**
153  * @brief Opens the appdata, when the amiibo appdata was previously initialized. This must be used before reading/writing the appdata. See also: https://3dbrew.org/wiki/NFC:OpenAppData
154  * @param amiibo_appid Amiibo AppID. See here: https://www.3dbrew.org/wiki/Amiibo
155  */
156 Result nfcOpenAppData(u32 amiibo_appid);
157 
158 /**
159  * @brief This initializes the appdata using the specified input, when the appdata previously wasn't initialized. If the appdata is already initialized, you must first use the amiibo Settings applet menu option labeled "Delete amiibo Game Data". This automatically writes the amiibo data into the actual data storage(normally NFC data pages). See also nfcWriteAppData().
160  * @param amiibo_appid amiibo AppID. See also nfcOpenAppData().
161  * @param buf Input buffer.
162  * @param size Buffer size.
163  */
164 Result nfcInitializeWriteAppData(u32 amiibo_appid, const void *buf, size_t size);
165 
166 /**
167  * @brief Reads the appdata. The size must be >=0xD8-bytes, but the actual used size is hard-coded to 0xD8. Note that areas of appdata which were never written to by applications are uninitialized in this output buffer.
168  * @param buf Output buffer.
169  * @param size Buffer size.
170  */
171 Result nfcReadAppData(void *buf, size_t size);
172 
173 /**
174  * @brief Writes the appdata, after nfcOpenAppData() was used successfully. The size should be <=0xD8-bytes. See also: https://3dbrew.org/wiki/NFC:WriteAppData
175  * @param buf Input buffer.
176  * @param size Buffer size.
177  * @param taginfo TagInfo from nfcGetTagInfo().
178  */
179 Result nfcWriteAppData(const void *buf, size_t size, NFC_TagInfo *taginfo);
180 
181 /**
182  * @brief Returns the current AmiiboSettings.
183  * @param out Pointer to write the output AmiiboSettings.
184  */
186 
187 /**
188  * @brief Returns the current AmiiboConfig.
189  * @param out Pointer to write the output AmiiboConfig.
190  */
192 
193 /**
194  * @brief Starts scanning for NFC tags when initialized with NFC_OpType_RawNFC. See also: https://www.3dbrew.org/wiki/NFC:StartOtherTagScanning
195  * @param unk0 Same as nfcStartScanning() input.
196  * @param unk1 Unknown.
197  */
199 
200 /**
201  * @brief This sends a raw NFC command to the tag. This can only be used when initialized with NFC_OpType_RawNFC, and when the TagState is NFC_TagState_InRange. See also: https://www.3dbrew.org/wiki/NFC:SendTagCommand
202  * @param inbuf Input buffer.
203  * @param insize Size of the input buffer.
204  * @param outbuf Output buffer.
205  * @param outsize Size of the output buffer.
206  * @param actual_transfer_size Optional output ptr to write the actual output-size to, can be NULL.
207  * @param microseconds Timing-related field in microseconds.
208  */
209 Result nfcSendTagCommand(const void *inbuf, size_t insize, void *outbuf, size_t outsize, size_t *actual_transfer_size, u64 microseconds);
210 
211 /**
212  * @brief Unknown. This can only be used when initialized with NFC_OpType_RawNFC, and when the TagState is NFC_TagState_InRange.
213  */
215 
216 /**
217  * @brief Unknown. This can only be used when initialized with NFC_OpType_RawNFC, and when the TagState is NFC_TagState_InRange.
218  */
220 
Result nfcCmd22(void)
Unknown.
Result nfcGetTagInfo(NFC_TagInfo *out)
Returns the current TagInfo.
Result nfcCmd21(void)
Unknown.
Result nfcUpdateStoredAmiiboData(void)
This writes the amiibo data stored in memory to the actual amiibo data storage(which is normally the ...
Result nfcGetAmiiboConfig(NFC_AmiiboConfig *out)
Returns the current AmiiboConfig.
void nfcExit(void)
Shuts down NFC.
Result nfcLoadAmiiboData(void)
Read amiibo NFC data and load in memory.
NFC_TagState
Definition: nfc.h:35
@ NFC_TagState_DataReady
NFC tag is now out of range, where the NFC tag was previously in range. This occurs automatically wit...
Definition: nfc.h:41
@ NFC_TagState_OutOfRange
NFC tag is in range. The state automatically changes to this when the state was previously value 2,...
Definition: nfc.h:40
@ NFC_TagState_Scanning
Not currently scanning for NFC tags. Set by nfcStopScanning() and nfcInit(), when successful.
Definition: nfc.h:38
@ NFC_TagState_ScanningStopped
nfcInit() was not used yet.
Definition: nfc.h:37
@ NFC_TagState_InRange
Currently scanning for NFC tags. Set by nfcStartScanning() when successful.
Definition: nfc.h:39
Result nfcInit(NFC_OpType type)
Initializes NFC.
Result nfcGetTagState(NFC_TagState *state)
Returns the current NFC tag state.
Result nfcSendTagCommand(const void *inbuf, size_t insize, void *outbuf, size_t outsize, size_t *actual_transfer_size, u64 microseconds)
This sends a raw NFC command to the tag.
@ NFC_amiiboFlag_AppDataSetup
This indicates that the amiibo was setup with amiibo Settings. nfcGetAmiiboSettings() will return an ...
Definition: nfc.h:47
Result nfcReadAppData(void *buf, size_t size)
Reads the appdata.
Result nfcOpenAppData(u32 amiibo_appid)
Opens the appdata, when the amiibo appdata was previously initialized.
Result nfcResetTagScanState(void)
If the tagstate is valid(NFC_TagState_DataReady or 6), it then sets the current tagstate to NFC_TagSt...
NFC_OpType
NFC operation type.
Definition: nfc.h:29
@ NFC_OpType_NFCTag
Unknown.
Definition: nfc.h:31
@ NFC_OpType_RawNFC
This is the default.
Definition: nfc.h:32
Result nfcGetAmiiboSettings(NFC_AmiiboSettings *out)
Returns the current AmiiboSettings.
Result nfcStartOtherTagScanning(u16 unk0, u32 unk1)
Starts scanning for NFC tags when initialized with NFC_OpType_RawNFC.
Result nfcStartScanning(u16 inval)
Starts scanning for NFC tags.
Handle nfcGetSessionHandle(void)
Gets the NFC service handle.
Result nfcInitializeWriteAppData(u32 amiibo_appid, const void *buf, size_t size)
This initializes the appdata using the specified input, when the appdata previously wasn't initialize...
Result nfcWriteAppData(const void *buf, size_t size, NFC_TagInfo *taginfo)
Writes the appdata, after nfcOpenAppData() was used successfully.
void nfcStopScanning(void)
Stops scanning for NFC tags.
AmiiboConfig structure, see also here: https://3dbrew.org/wiki/NFC:GetAmiiboConfig.
Definition: nfc.h:70
u8 type
ID shared by all exact same amiibo. Some amiibo are only distinguished by this one like regular SMB S...
Definition: nfc.h:78
u16 amiiboID
ID of the series.
Definition: nfc.h:77
u8 series
the first element is the collection ID, the second the character in this collection,...
Definition: nfc.h:76
u8 pagex4_byte3
Type of amiibo 0 = figure, 1 = card, 2 = plush.
Definition: nfc.h:79
AmiiboSettings structure, see also here: https://3dbrew.org/wiki/NFC:GetAmiiboSettings.
Definition: nfc.h:58
u8 flags
"UTF-16BE Amiibo nickname."
Definition: nfc.h:61
u16 setupdate_year
"This is plaintext_amiibosettingsdata[1]." "Country Code ID, from the system which setup this amiibo....
Definition: nfc.h:63
u8 countrycodeid
"This is plaintext_amiibosettingsdata[0] & 0xF." See also the NFC_amiiboFlag enums.
Definition: nfc.h:62
Used by nfcInitializeWriteAppData() internally, see also here: https://3dbrew.org/wiki/NFC:GetAppData...
Definition: nfc.h:85
Used by nfcWriteAppData() internally, see also: https://3dbrew.org/wiki/NFC:WriteAppData.
Definition: nfc.h:91
Definition: nfc.h:50
u8 unk_x2
"u16 size/offset of the below ID data. Normally this is 0x7. When this is <=10, this field is the siz...
Definition: nfc.h:52
#define BIT(n)
Creates a bitmask from a bit number.
Definition: types.h:47
uint64_t u64
64-bit unsigned integer
Definition: types.h:24
uint8_t u8
would be nice if newlib had this already
Definition: types.h:21
u32 Handle
Resource handle.
Definition: types.h:41
s32 Result
Function result.
Definition: types.h:42
uint16_t u16
16-bit unsigned integer
Definition: types.h:22
uint32_t u32
32-bit unsigned integer
Definition: types.h:23