/// <summary> /// Depending on the command, serialize the needed data /// Authentication will serialize the command, the concerned key and /// the serial number /// Reading data will just serialize the command /// Writing data will serialize the data as well /// </summary> /// <returns>The serialized bits</returns> private byte[] Serialize() { byte[] ser = null; switch (Command) { case MifareCardCommand.AuthenticationA: ser = new byte[2 + KeyA.Length + SerialNumber.Length]; ser[0] = (byte)Command; ser[1] = BlockNumber; if (KeyA.Length > 0) { KeyA.CopyTo(ser, 2); } if (SerialNumber.Length > 0) { SerialNumber.CopyTo(ser, 2 + KeyA.Length); } return(ser); case MifareCardCommand.AuthenticationB: ser = new byte[2 + KeyB.Length + SerialNumber.Length]; ser[0] = (byte)Command; ser[1] = BlockNumber; if (KeyB.Length > 0) { KeyB.CopyTo(ser, 2); } if (SerialNumber.Length > 0) { SerialNumber.CopyTo(ser, 2 + KeyB.Length); } return(ser); case MifareCardCommand.Write16Bytes: case MifareCardCommand.Write4Bytes: ser = new byte[2 + Data.Length]; ser[0] = (byte)Command; ser[1] = BlockNumber; if (Data.Length > 0) { Data.CopyTo(ser, 2); } return(ser); case MifareCardCommand.Incrementation: case MifareCardCommand.Decrementation: case MifareCardCommand.Transfer: case MifareCardCommand.Restore: case MifareCardCommand.Read16Bytes: default: ser = new byte[2]; ser[0] = (byte)Command; ser[1] = BlockNumber; return(ser); } }
/// <summary> /// Depending on the command, serialize the needed data /// Authentication will serialize the command, the concerned key and /// the serial number /// Reading data will just serialize the command /// Writing data will serialize the data as well /// </summary> /// <returns>The serialized bits</returns> private byte[] Serialize() { byte[]? ser = null; switch (Command) { case MifareCardCommand.AuthenticationA: if (KeyA is null || SerialNumber is null) { throw new Exception($"Card is not configured for {nameof(MifareCardCommand.AuthenticationA)}."); } ser = new byte[2 + KeyA.Length + SerialNumber.Length]; ser[0] = (byte)Command; ser[1] = BlockNumber; if (KeyA.Length > 0) { KeyA.CopyTo(ser, 2); } if (SerialNumber.Length > 0) { SerialNumber.CopyTo(ser, 2 + KeyA.Length); } return(ser); case MifareCardCommand.AuthenticationB: if (KeyB is null || SerialNumber is null) { throw new Exception($"Card is not configured for {nameof(MifareCardCommand.AuthenticationB)}."); } ser = new byte[2 + KeyB.Length + SerialNumber.Length]; ser[0] = (byte)Command; ser[1] = BlockNumber; if (KeyB.Length > 0) { KeyB.CopyTo(ser, 2); } if (SerialNumber.Length > 0) { SerialNumber.CopyTo(ser, 2 + KeyB.Length); } return(ser); case MifareCardCommand.Write16Bytes: case MifareCardCommand.Write4Bytes: if (Data is null) { throw new Exception($"Card is not configured for {nameof(MifareCardCommand.Write4Bytes)}."); } ser = new byte[2 + Data.Length]; ser[0] = (byte)Command; ser[1] = BlockNumber; if (Data.Length > 0) { Data.CopyTo(ser, 2); } return(ser); case MifareCardCommand.Incrementation: case MifareCardCommand.Decrementation: case MifareCardCommand.Transfer: case MifareCardCommand.Restore: case MifareCardCommand.Read16Bytes: default: ser = new byte[2]; ser[0] = (byte)Command; ser[1] = BlockNumber; return(ser); } }