private async Task <ApduResponse> Transceive(ApduCommand apduCommand) { ApduResponse apduRes = Activator.CreateInstance(apduCommand.ApduResponseType) as ApduResponse; byte[] dataIn = apduCommand.Serialize(); bool debugOut = true; if (debugOut) { StringBuilder sb = new StringBuilder(); sb.AppendLine("********************************************************************************************************"); sb.AppendLine(apduCommand.ToString()); sb.Append("Raw Data Sent:" + Formatting.ByteArrayToHexString(dataIn)); Logger.Log(sb.ToString()); } byte[] dataOut = await CardInterfaceManger.TransmitAsync(dataIn); if (debugOut) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Raw Data Received:" + Formatting.ByteArrayToHexString(dataOut)); sb.AppendLine("********************************************************************************************************"); Logger.Log(sb.ToString()); } apduRes.Deserialize(dataOut); if (apduRes.SW1 == 0x61) { GetResponseRequest getResponseRequest = new GetResponseRequest(apduRes.SW2); ApduResponse getResponseResponse = await Transceive(getResponseRequest); if (getResponseResponse.Succeeded || (getResponseResponse.SW1 == 0x62 && getResponseResponse.SW2 == 0x83)) { apduRes.ResponseData = Formatting.ConcatArrays(apduRes.ResponseData, getResponseResponse.ResponseData, new byte[] { 0x90, 0x00 }); apduRes.Deserialize(apduRes.ResponseData); } else { throw new Exception("GetResponse failed"); } } if (apduRes.SW1 == 0x6C) { //repeat command with correct Le apduCommand.Le = apduRes.SW2; apduRes = await Transceive(apduCommand); } return(apduRes); }
private async Task StartServicingeQ() { //run for as long as card reader is active while (1 == 1) { try { CardRequest cardRequest = CardQ.DequeueFromInput(false, true); if (cardRequest == null) //timeout { if (cancellationTokenForCardInterfaceManager.Token.IsCancellationRequested) { cancellationTokenForCardInterfaceManager.Dispose(); break; } else { continue; } } CardResponse response; try { response = new CardResponse(await Transceive(cardRequest.ApduCommand), CardInterfaceServiceResponseEnum.RA); } catch { ApduResponse apduRes = new ApduResponse(); byte[] dataOut = new byte[] { 0x00, 0x00 }; apduRes.Deserialize(dataOut); response = new CardResponse(apduRes, CardInterfaceServiceResponseEnum.L1RSP); } CardQ.EnqueueToOutput(response); } catch (Exception ex) { StopServiceQProcess(); OnExceptionOccured(ex); } } }