/// <summary> /// Read record comand /// </summary> /// <param name="fileNumber"></param> /// <param name="offset"></param> /// <param name="numberOfRecords"></param> /// <returns> /// byte array of read data /// </returns> public async Task <byte[]> ReadRecordAsync(byte fileNumber, ulong offset, ulong numberOfRecords) { var args = new byte[7]; args[0] = fileNumber; args[1] = (byte)(offset & 0xFF); args[2] = (byte)((offset >> 8) & 0xFF); args[3] = (byte)((offset >> 16) & 0xFF); args[4] = (byte)(numberOfRecords & 0xFF); args[5] = (byte)((numberOfRecords >> 8) & 0xFF); args[6] = (byte)((numberOfRecords >> 16) & 0xFF); desfireCommand.Command = (byte)DesfireCommand.CommandType.ReadRecord; desfireCommand.Data = args; DesfireResponse desfireRes = await connectionObject.TransceiveAsync(desfireCommand) as DesfireResponse; if (desfireRes.BoundaryError) { // Boundary error, the record doesn't exist return(null); } if (!desfireRes.Succeeded) { throw new Exception("Failure selecting application, SW=" + desfireResponse.SW + " (" + desfireResponse.SWTranslation + ")"); } return(desfireRes.ResponseData); }
/// <summary> /// Read card details commands /// </summary> /// <returns> /// returns Desfire CardDetails object /// </returns> public async Task <CardDetails> ReadCardDetailsAsync() { desfireCommand.Command = (byte)DesfireCommand.CommandType.GetVersion; desfireCommand.Data = null; DesfireResponse desfireRes = await connectionObject.TransceiveAsync(desfireCommand) as DesfireResponse; if (!desfireRes.SubsequentFrame || desfireRes.ResponseData.Length != 7) { return(null); } CardDetails card = new CardDetails(); card.HardwareVendorID = desfireRes.ResponseData[0]; card.HardwareType = desfireRes.ResponseData[1]; card.HardwareSubType = desfireRes.ResponseData[2]; card.HardwareMajorVersion = desfireRes.ResponseData[3]; card.HardwareMinorVersion = desfireRes.ResponseData[4]; card.HardwareStorageSize = desfireRes.ResponseData[5]; card.HardwareProtocolType = desfireRes.ResponseData[6]; desfireCommand.Command = (byte)DesfireCommand.CommandType.GetAdditionalFrame; desfireRes = await connectionObject.TransceiveAsync(desfireCommand) as DesfireResponse; if (!desfireRes.SubsequentFrame || desfireRes.ResponseData.Length != 7) { // Not expected return(null); } card.SoftwareVendorID = desfireRes.ResponseData[0]; card.SoftwareType = desfireRes.ResponseData[1]; card.SoftwareSubType = desfireRes.ResponseData[2]; card.SoftwareMajorVersion = desfireRes.ResponseData[3]; card.SoftwareMinorVersion = desfireRes.ResponseData[4]; card.SoftwareStorageSize = desfireRes.ResponseData[5]; card.SoftwareProtocolType = desfireRes.ResponseData[6]; desfireRes = await connectionObject.TransceiveAsync(desfireCommand) as DesfireResponse; if (!desfireRes.Succeeded || desfireRes.ResponseData.Length != 14) { // Not expected return(null); } card.UID = new byte[7]; System.Buffer.BlockCopy(desfireRes.ResponseData, 0, card.UID, 0, 7); card.GUID = desfireRes.ResponseData[7]; card.ProductionBatchNumber = new byte[5]; System.Buffer.BlockCopy(desfireRes.ResponseData, 7, card.ProductionBatchNumber, 0, 5); card.WeekOfProduction = desfireRes.ResponseData[12]; card.YearOfProduction = desfireRes.ResponseData[13]; return(card); }
/// <summary> /// Select ICC application by AID /// </summary> /// <param name="aid"> /// application id /// </param> public async Task SelectApplicationAsync(byte[] aid) { if (aid.Length != 3) { throw new NotSupportedException(); } desfireCommand.Command = (byte)DesfireCommand.CommandType.SelectApplication; desfireCommand.Data = aid; DesfireResponse desfireRes = await connectionObject.TransceiveAsync(desfireCommand) as DesfireResponse; if (!desfireResponse.Succeeded) { throw new Exception("Failure selecting application, SW=" + desfireResponse.SW + " (" + desfireResponse.SWTranslation + ")"); } }
/// <summary> /// Read data command /// </summary> /// <param name="fileNumber"> /// </param> /// <param name="offset"> /// </param> /// <param name="length"> /// </param> /// <returns> /// byte array of read data /// </returns> public async Task <byte[]> ReadDataAsync(byte fileNumber, ulong offset, ulong length) { var args = new byte[7]; args[0] = fileNumber; args[1] = (byte)(offset & 0xFF); args[2] = (byte)((offset >> 8) & 0xFF); args[3] = (byte)((offset >> 16) & 0xFF); args[4] = (byte)(length & 0xFF); args[5] = (byte)((length >> 8) & 0xFF); args[6] = (byte)((length >> 16) & 0xFF); desfireCommand.Command = (byte)DesfireCommand.CommandType.ReadData; desfireCommand.Data = args; DesfireResponse desfireRes = await connectionObject.TransceiveAsync(desfireCommand) as DesfireResponse; if (!desfireRes.Succeeded) { throw new Exception("Failure selecting application, SW=" + desfireResponse.SW + " (" + desfireResponse.SWTranslation + ")"); } return(desfireRes.ResponseData); }