//Pascal &or the FreePascal use of nintendo 2ds, 3ds regime // // Copyright (c) 2013, 2015, 2017 Kenneth Dwayne Lee Bsc. // all rights reserved. // {* * @file nfc.h * @brief NFC service. This can only be used with system-version >=9.3.0-X. } {/ This is returned when the current state is invalid for this command. } const NFC_ERR_INVALID_STATE = $C8A17600; {/ This is returned by nfcOpenAppData() when the appdata is uninitialized since nfcInitializeWriteAppData() wasn't used previously. } NFC_ERR_APPDATA_UNINITIALIZED = $C8A17620; {/ This is returned by nfcGetAmiiboSettings() when the amiibo wasn't setup by the amiibo Settings applet. } NFC_ERR_AMIIBO_NOTSETUP = $C8A17628; {/ This is returned by nfcOpenAppData() when the input AppID doesn't match the actual amiibo AppID. } NFC_ERR_APPID_MISMATCH = $C8A17638; {/ "Returned for HMAC-hash mismatch(data corruption), with HMAC-calculation input_buffer_size=0x34." } NFC_ERR_DATACORRUPTION0 = $C8C1760C; {/ HMAC-hash mismatch with input_buffer_size=0x1DF, see here: https://www.3dbrew.org/wiki/Amiibo } NFC_ERR_DATACORRUPTION1 = $C8A17618; {/ This can be used for nfcStartScanning(). } NFC_STARTSCAN_DEFAULTINPUT = 0; {/ NFC operation type. } {/ Unknown. } {/ This is the default. } type NFC_OpType = (NFC_OpType_1 := 1,NFC_OpType_NFCTag := 2 ); {/ nfcInit() was not used yet. } {/ Not currently scanning for NFC tags. Set by nfcStopScanning() and nfcInit(), when successful. } {/ Currently scanning for NFC tags. Set by nfcStartScanning() when successful. } {/ NFC tag is in range. The state automatically changes to this when the state was previously value 2, without using any NFC service commands. } {/ 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 } {/ NFC tag data was successfully loaded. This is set by nfcLoadAmiiboData() when successful. } NFC_TagState = (NFC_TagState_Uninitialized := 0,NFC_TagState_ScanningStopped := 1, NFC_TagState_Scanning := 2,NFC_TagState_InRange := 3, NFC_TagState_OutOfRange := 4,NFC_TagState_DataReady := 5 ); PNFC_TagState = ^NFC_TagState; {/ Bit4-7 are always clear with nfcGetAmiiboSettings() due to "& 0xF". } const NFC_amiiboFlag_Setup = 1 shl 4; NFC_amiiboFlag_AppDataSetup = 1 shl 5; {/ This indicates that the amiibo was setup with amiibo Settings. nfcGetAmiiboSettings() will return an all-zero struct when this is not set. } {/ This indicates that the AppData was previously initialized via nfcInitializeWriteAppData(), that function can't be used again with this flag already set. } {/ "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+. I } {"Unknown u8, normally 0x0." } {"Unknown u8, normally 0x2." } {"ID data. When the above size field is 0x7, this is the 7-byte NFC tag UID, followed by all-zeros." } type NFC_TagInfo = record id_offset_size : u16; unk_x2 : u8; unk_x3 : u8; id : array[0..39] of u8; end; PNFC_TagInfo = ^NFC_TagInfo; {/ AmiiboSettings structure, see also here: https://3dbrew.org/wiki/NFC:GetAmiiboSettings } {/ "Owner Mii." } {/ "UTF-16BE Amiibo nickname." } {/ "This is plaintext_amiibosettingsdata[0] & 0xF." See also the NFC_amiiboFlag enums. } {/ "This is plaintext_amiibosettingsdata[1]." "Country Code ID, from the system which setup this amiibo." } {Normally all-zero? } NFC_AmiiboSettings = record mii : array[0..95] of u8; nickname : array[0..10] of u16; flags : u8; countrycodeid : u8; setupdate_year : u16; setupdate_month : u8; setupdate_day : u8; unk_x7c : array[0..43] of u8; end; PNFC_AmiiboSettings = ^NFC_AmiiboSettings; {/ AmiiboConfig structure, see also here: https://3dbrew.org/wiki/NFC:GetAmiiboConfig } {/ the first element is the collection ID, the second the character in this collection, the third the variant } {/ ID of the series } {/ ID shared by all exact same amiibo. Some amiibo are only distinguished by this one like regular SMB Series Mario and the gold one } {/ Type of amiibo 0 = figure, 1 = card, 2 = plush } {/ "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. ..." } {/ "Unused / reserved: this is cleared by NFC module but never written after that." } NFC_AmiiboConfig = record lastwritedate_year : u16; lastwritedate_month : u8; lastwritedate_day : u8; write_counter : u16; characterID : array[0..2] of u8; series : u8; amiiboID : u16; _type : u8; pagex4_byte3 : u8; appdata_size : u16; zeros : array[0..47] of u8; end; PNFC_AmiiboConfig = ^NFC_AmiiboConfig; {/ Used by nfcInitializeWriteAppData() internally, see also here: https://3dbrew.org/wiki/NFC:GetAppDataInitStruct } {/ "The data starting at struct offset 0xC is the 0x30-byte struct used by NFC:InitializeWriteAppData, sent by the user-process." } NFC_AppDataInitStruct = record data_x0 : array[0..11] of u8; data_xc : array[0..47] of u8; end; {/ Used by nfcWriteAppData() internally, see also: https://3dbrew.org/wiki/NFC:WriteAppData } {7-byte UID normally. } NFC_AppDataWriteStruct = record id : array[0..9] of u8; id_size : u8; unused_xb : array[0..20] of u8; end; {* * @brief Initializes NFC. * @param type See the NFC_OpType enum. } function nfcInit(_type:NFC_OpType):s32;cdecl;external; {* * @brief Shuts down NFC. } procedure nfcExit;cdecl;external; {* * @brief Gets the NFC service handle. * @return The NFC service handle. } function nfcGetSessionHandle:Handle;cdecl;external; {* * @brief Starts scanning for NFC tags. * @param inval Unknown. See NFC_STARTSCAN_DEFAULTINPUT. } function nfcStartScanning(inval:u16):s32;cdecl;external; {* * @brief Stops scanning for NFC tags. } procedure nfcStopScanning;cdecl;external; {* * @brief Read amiibo NFC data and load in memory. } function nfcLoadAmiiboData:s32;cdecl;external; {* * @brief If the tagstate is valid(NFC_TagState_DataReady or 6), it then sets the current tagstate to NFC_TagState_InRange. } function nfcResetTagScanState:s32;cdecl;external; {* * @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. } function nfcUpdateStoredAmiiboData:s32;cdecl;external; {* * @brief Returns the current NFC tag state. * @param state Pointer to write NFC tag state. } function nfcGetTagState(state:PNFC_TagState):s32;cdecl;external; {* * @brief Returns the current TagInfo. * @param out Pointer to write the output TagInfo. } function nfcGetTagInfo(out:PNFC_TagInfo):s32;cdecl;external; {* * @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 * @param amiibo_appid Amiibo AppID. See here: https://www.3dbrew.org/wiki/Amiibo } function nfcOpenAppData(amiibo_appid:u32):s32;cdecl;external; {* * @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 * @param amiibo_appid amiibo AppID. See also nfcOpenAppData(). * @param buf Input buffer. * @param size Buffer size. } function nfcInitializeWriteAppData(amiibo_appid:u32; buf:pointer; size:u32):s32;cdecl;external; {* * @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. * @param buf Output buffer. * @param size Buffer size. } function nfcReadAppData(buf:pointer; size:u32):s32;cdecl;external; {* * @brief Writes the appdata, after nfcOpenAppData() was used successfully. The size should be <=0xD8-bytes. See also: https://3dbrew.org/wiki/NFC:WriteAppData * @param buf Input buffer. * @param size Buffer size. * @param taginfo TagInfo from nfcGetTagInfo(). } function nfcWriteAppData(buf:pointer; size:u32; taginfo:PNFC_TagInfo):s32;cdecl;external; {* * @brief Returns the current AmiiboSettings. * @param out Pointer to write the output AmiiboSettings. } function nfcGetAmiiboSettings(out:PNFC_AmiiboSettings):s32;cdecl;external; {* * @brief Returns the current AmiiboConfig. * @param out Pointer to write the output AmiiboConfig. } function nfcGetAmiiboConfig(out:PNFC_AmiiboConfig):s32;cdecl;external;