Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 1 | /* ---------------------------------------------------------------------------- |
| 2 | * ATMEL Microcontroller Software Support |
| 3 | * ---------------------------------------------------------------------------- |
| 4 | * Copyright (c) 2009, Atmel Corporation |
| 5 | * |
| 6 | * All rights reserved. |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
| 11 | * - Redistributions of source code must retain the above copyright notice, |
| 12 | * this list of conditions and the disclaimer below. |
| 13 | * |
| 14 | * Atmel's name may not be used to endorse or promote products derived from |
| 15 | * this software without specific prior written permission. |
| 16 | * |
| 17 | * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 19 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
| 20 | * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, |
| 23 | * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 24 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 25 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 26 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | * ---------------------------------------------------------------------------- |
| 28 | */ |
| 29 | |
| 30 | /*------------------------------------------------------------------------------ |
| 31 | * Headers |
| 32 | *------------------------------------------------------------------------------*/ |
| 33 | |
| 34 | #include "board.h" |
| 35 | |
| 36 | #include <string.h> |
| 37 | |
| 38 | /*------------------------------------------------------------------------------ |
| 39 | * Internal definitions |
| 40 | *------------------------------------------------------------------------------*/ |
| 41 | |
| 42 | /** Maximum ucSize in bytes of the smartcard answer to a command.*/ |
| 43 | #define MAX_ANSWER_SIZE 10 |
| 44 | |
| 45 | /** Maximum ATR ucSize in bytes.*/ |
| 46 | #define MAX_ATR_SIZE 55 |
| 47 | |
| 48 | /** USB states */ |
| 49 | /// Use for power management |
| 50 | #define STATE_IDLE 0 |
| 51 | /// The USB device is in suspend state |
| 52 | #define STATE_SUSPEND 4 |
| 53 | /// The USB device is in resume state |
| 54 | #define STATE_RESUME 5 |
| 55 | |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 56 | /* WTX (Wait time extension): |
| 57 | * R-block PCB begins with (msb) 10 , ends with 000011 for WTX req, 100011 for WTX resp |
| 58 | * |
| 59 | * The standard says: |
| 60 | * Rule 3 β If the card requires more than BWT to process the previously received I-block, it transmits S(WTX |
| 61 | * request) where INF conveys one byte encoding an integer multiplier of the BWT value. The interface device |
| 62 | * shall acknowledge by S(WTX response) with the same INF. |
| 63 | * The time allocated starts at the leading edge of the last character of S(WTX response). |
| 64 | */ |
Christina Quast | a4d1d16 | 2015-04-06 20:41:50 +0200 | [diff] [blame] | 65 | // FIXME: Two times the same name for the define, which one is right? |
| 66 | //#define WTX_req 0b10000011 |
| 67 | //#define WTX_req 0b10100011 |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 68 | // Alternatively: |
| 69 | /* For T = 0 Protocol: The firmware on receiving the NULL (0x60) Procedure byte from the card, notifies |
| 70 | it to the driver using the RDR_to_PC_DataBlock response. During this period, the reception of bytes |
| 71 | from the smart card is still in progress and hence the device cannot indefinitely wait for IN tokens on |
| 72 | the USB bulk-in endpoint. Hence, it is required of the driver to readily supply βINβ tokens on the USB |
| 73 | bulk-in endpoint. On failure to do so, some of the wait time extension responses, will not be queued to |
| 74 | the driver. |
| 75 | */ |
| 76 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 77 | /*------------------------------------------------------------------------------ |
| 78 | * Internal variables |
| 79 | *------------------------------------------------------------------------------*/ |
| 80 | /** USB state: suspend, resume, idle */ |
| 81 | unsigned char USBState = STATE_IDLE; |
| 82 | |
| 83 | /** ISO7816 pins */ |
| 84 | static const Pin pinsISO7816_PHONE[] = {PINS_ISO7816_PHONE}; |
Christina Quast | 8043fdd | 2015-03-04 18:45:30 +0100 | [diff] [blame] | 85 | /** Bus switch pins */ |
Christina Quast | 3041854 | 2015-04-05 10:08:06 +0200 | [diff] [blame] | 86 | static const Pin pins_bus[] = {PINS_BUS_DEFAULT}; |
Christina Quast | 4bcc023 | 2015-03-24 21:59:32 +0100 | [diff] [blame] | 87 | // FIXME: temporary enable bus switch |
Christina Quast | 3041854 | 2015-04-05 10:08:06 +0200 | [diff] [blame] | 88 | //static const Pin pins_bus[] = {PINS_BUS_SNIFF}; |
Christina Quast | 4bcc023 | 2015-03-24 21:59:32 +0100 | [diff] [blame] | 89 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 90 | /** ISO7816 RST pin */ |
| 91 | static const Pin pinIso7816RstMC = PIN_ISO7816_RST_PHONE; |
| 92 | static uint8_t sim_inserted = 0; |
| 93 | |
| 94 | static const Pin pPwr[] = { |
| 95 | /* Enable power converter 4.5-6V to 3.3V; low: off */ |
| 96 | {SIM_PWEN, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}, |
| 97 | |
| 98 | /* Enable second power converter: VCC_PHONE to VCC_SIM; high: off */ |
| 99 | {VCC_FWD, PIOA, ID_PIOA, PIO_OUTPUT_1, PIO_DEFAULT} |
| 100 | }; |
| 101 | |
| 102 | |
Christina Quast | 8e5381c | 2015-04-03 11:37:17 +0200 | [diff] [blame] | 103 | static const Pin pinPhoneRST = PIN_ISO7816_RST_PHONE; |
| 104 | |
| 105 | #define PR TRACE_INFO |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 106 | |
| 107 | /* ===================================================*/ |
| 108 | /* Taken from iso7816_4.c */ |
| 109 | /* ===================================================*/ |
| 110 | /** Flip flop for send and receive char */ |
| 111 | #define USART_SEND 0 |
| 112 | #define USART_RCV 1 |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 113 | |
| 114 | #define NONE 9 |
| 115 | #define RST_RCVD 10 |
| 116 | #define WAIT_CMD_PHONE 11 |
| 117 | #define WAIT_CMD_PC 12 |
| 118 | #define WAIT_ATR 13 |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 119 | /*----------------------------------------------------------------------------- |
| 120 | * Internal variables |
| 121 | *-----------------------------------------------------------------------------*/ |
| 122 | /** Variable for state of send and receive froom USART */ |
| 123 | static uint8_t StateUsartGlobal = USART_RCV; |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 124 | |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 125 | static uint32_t state; |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 126 | extern uint8_t rcvdChar; |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 127 | |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 128 | extern volatile uint8_t timeout_occured; |
| 129 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 130 | /*----------------------------------------------------------------------------- |
| 131 | * Interrupt routines |
| 132 | *-----------------------------------------------------------------------------*/ |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 133 | #define RESET 'R' |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 134 | static void ISR_PhoneRST( const Pin *pPin) |
| 135 | { |
Christina Quast | d3630cc | 2015-04-03 11:38:24 +0200 | [diff] [blame] | 136 | printf("+++ Int!! %x\n\r", pinPhoneRST.pio->PIO_ISR); |
| 137 | if ( ((pinPhoneRST.pio->PIO_ISR & pinPhoneRST.mask) != 0) ) |
| 138 | { |
| 139 | if(PIO_Get( &pinPhoneRST ) == 0) { |
| 140 | printf(" 0 "); |
| 141 | } else { |
| 142 | printf(" 1 "); |
| 143 | } |
| 144 | } |
Christina Quast | 4bcc023 | 2015-03-24 21:59:32 +0100 | [diff] [blame] | 145 | state = RST_RCVD; |
| 146 | |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 147 | // FIXME: What to do on reset? |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 148 | // FIXME: It seems like the phone is constantly sending a lot of these RSTs |
Christina Quast | bdaa954 | 2015-04-06 00:52:08 +0200 | [diff] [blame] | 149 | PIO_DisableIt( &pinPhoneRST ) ; |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static void Config_PhoneRST_IrqHandler() |
| 153 | { |
| 154 | PIO_Configure( &pinPhoneRST, 1); |
| 155 | // PIO_Configure( &pinPhoneClk, 1); |
| 156 | PIO_ConfigureIt( &pinPhoneRST, ISR_PhoneRST ) ; |
| 157 | // PIO_ConfigureIt( &pinPhoneClk, ISR_PhoneRST ) ; |
| 158 | PIO_EnableIt( &pinPhoneRST ) ; |
| 159 | // PIO_EnableIt( &pinPhoneClk ) ; |
| 160 | NVIC_EnableIRQ( PIOA_IRQn ); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get a character from ISO7816 |
| 165 | * \param pCharToReceive Pointer for store the received char |
| 166 | * \return 0: if timeout else status of US_CSR |
| 167 | */ |
| 168 | /* FIXME: This code is taken from cciddriver.c |
| 169 | --> Reuse the code!!! */ |
| 170 | uint32_t _ISO7816_GetChar( uint8_t *pCharToReceive ) |
| 171 | { |
| 172 | uint32_t status; |
| 173 | uint32_t timeout=0; |
| 174 | |
| 175 | TRACE_DEBUG("--"); |
| 176 | |
| 177 | if( StateUsartGlobal == USART_SEND ) { |
| 178 | while((USART_PHONE->US_CSR & US_CSR_TXEMPTY) == 0) {} |
| 179 | USART_PHONE->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
| 180 | StateUsartGlobal = USART_RCV; |
| 181 | } |
| 182 | |
| 183 | /* Wait USART ready for reception */ |
| 184 | while( ((USART_PHONE->US_CSR & US_CSR_RXRDY) == 0) ) { |
| 185 | if(timeout++ > 12000 * (BOARD_MCK/1000000)) { |
| 186 | TRACE_DEBUG("TimeOut\n\r"); |
| 187 | return( 0 ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* At least one complete character has been received and US_RHR has not yet been read. */ |
| 192 | |
| 193 | /* Get a char */ |
| 194 | *pCharToReceive = ((USART_PHONE->US_RHR) & 0xFF); |
| 195 | |
| 196 | status = (USART_PHONE->US_CSR&(US_CSR_OVRE|US_CSR_FRAME| |
| 197 | US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK| |
| 198 | (1<<10))); |
| 199 | |
| 200 | if (status != 0 ) { |
| 201 | TRACE_DEBUG("R:0x%X\n\r", status); |
| 202 | TRACE_DEBUG("R:0x%X\n\r", USART_PHONE->US_CSR); |
| 203 | TRACE_DEBUG("Nb:0x%X\n\r", USART_PHONE->US_NER ); |
| 204 | USART_PHONE->US_CR = US_CR_RSTSTA; |
| 205 | } |
| 206 | |
| 207 | /* Return status */ |
| 208 | return( status ); |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Send a char to ISO7816 |
| 213 | * \param CharToSend char to be send |
| 214 | * \return status of US_CSR |
| 215 | */ |
| 216 | uint32_t _ISO7816_SendChar( uint8_t CharToSend ) |
| 217 | { |
| 218 | uint32_t status; |
| 219 | |
| 220 | TRACE_DEBUG("********** Send char: %c (0x%X)\n\r", CharToSend, CharToSend); |
| 221 | |
| 222 | if( StateUsartGlobal == USART_RCV ) { |
| 223 | USART_PHONE->US_CR = US_CR_RSTSTA | US_CR_RSTIT | US_CR_RSTNACK; |
| 224 | StateUsartGlobal = USART_SEND; |
| 225 | } |
| 226 | |
| 227 | /* Wait USART ready for transmit */ |
| 228 | while((USART_PHONE->US_CSR & US_CSR_TXRDY) == 0) {} |
| 229 | /* There is no character in the US_THR */ |
| 230 | |
| 231 | /* Transmit a char */ |
| 232 | USART_PHONE->US_THR = CharToSend; |
| 233 | |
| 234 | status = (USART_PHONE->US_CSR&(US_CSR_OVRE|US_CSR_FRAME| |
| 235 | US_CSR_PARE|US_CSR_TIMEOUT|US_CSR_NACK| |
| 236 | (1<<10))); |
| 237 | |
| 238 | if (status != 0 ) { |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 239 | TRACE_DEBUG("******* status: 0x%X (Overrun: %lu, NACK: %lu, Timeout: %lu, underrun: %lu)\n\r", |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 240 | status, ((status & US_CSR_OVRE)>> 5), ((status & US_CSR_NACK) >> 13), |
| 241 | ((status & US_CSR_TIMEOUT) >> 8), ((status & (1 << 10)) >> 10)); |
| 242 | |
| 243 | TRACE_DEBUG("E (USART CSR reg):0x%X\n\r", USART_PHONE->US_CSR); |
| 244 | TRACE_DEBUG("Nb (Number of errors):0x%X\n\r", USART_PHONE->US_NER ); |
| 245 | USART_PHONE->US_CR = US_CR_RSTSTA; |
| 246 | } |
| 247 | |
| 248 | /* Return status */ |
| 249 | return( status ); |
| 250 | } |
| 251 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 252 | void Phone_Master_Init( void ) { |
| 253 | |
| 254 | PIO_Configure( pinsISO7816_PHONE, PIO_LISTSIZE( pinsISO7816_PHONE ) ) ; |
Christina Quast | 8043fdd | 2015-03-04 18:45:30 +0100 | [diff] [blame] | 255 | PIO_Configure( pins_bus, PIO_LISTSIZE( pins_bus) ) ; |
| 256 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 257 | Config_PhoneRST_IrqHandler(); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 258 | |
Christina Quast | c0aa769 | 2015-02-25 14:02:01 +0100 | [diff] [blame] | 259 | _ISO7816_Init(); |
| 260 | |
| 261 | USART_SetTransmitterEnabled(USART_PHONE, 1); |
| 262 | USART_SetReceiverEnabled(USART_PHONE, 1); |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 263 | |
| 264 | /* Configure ISO7816 driver */ |
| 265 | // FIXME: PIO_Configure(pPwr, PIO_LISTSIZE( pPwr )); |
| 266 | |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 267 | state = NONE; |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 268 | |
| 269 | |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 270 | // FIXME: Or do I need to call VBUS_CONFIGURE() here instead, which will call USBD_Connect() later? |
| 271 | // USBD_Connect(); |
| 272 | // FIXME: USB clock? USB PMC? |
| 273 | // NVIC_EnableIRQ( UDP_IRQn ); |
| 274 | |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 275 | USART_EnableIt( USART_PHONE, US_IER_RXRDY) ; |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 276 | |
| 277 | // FIXME: At some point USBD_IrqHandler() should get called and set USBD_STATE_CONFIGURED |
| 278 | /* while (USBD_GetState() < USBD_STATE_CONFIGURED) { |
| 279 | int i = 1; |
| 280 | if ((i%10000) == 0) { |
| 281 | TRACE_DEBUG("%d: USB State: %x\n\r", i, USBD_GetState()); |
| 282 | } |
| 283 | i++; |
| 284 | } |
| 285 | */ |
| 286 | |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 287 | Timer_Init(); |
Christina Quast | 32906bb | 2015-02-24 11:35:19 +0100 | [diff] [blame] | 288 | } |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 289 | |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 290 | void send_ATR(uint8_t *ATR, uint8_t status, uint32_t transferred, uint32_t remaining) |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 291 | { |
Christina Quast | 2f27c7c | 2015-04-06 20:46:40 +0200 | [diff] [blame^] | 292 | uint32_t i; |
Christina Quast | 14fbf9c | 2015-04-06 00:36:12 +0200 | [diff] [blame] | 293 | PR("Send %x %x .. %x (tr: %d, st: %x)", ATR[0], ATR[1], ATR[transferred-1], transferred, status); |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 294 | for ( i = 0; i < transferred; i++ ) { |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 295 | _ISO7816_SendChar(*(ATR++)); |
| 296 | } |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 297 | state = WAIT_CMD_PHONE; |
Christina Quast | bdaa954 | 2015-04-06 00:52:08 +0200 | [diff] [blame] | 298 | PIO_EnableIt( &pinPhoneRST ) ; |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 299 | } |
| 300 | |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 301 | void sendResponse( uint8_t *pArg, uint8_t status, uint32_t transferred, uint32_t remaining) |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 302 | { |
Christina Quast | 2f27c7c | 2015-04-06 20:46:40 +0200 | [diff] [blame^] | 303 | uint32_t i; |
Christina Quast | 1d80ef2 | 2015-04-03 11:39:38 +0200 | [diff] [blame] | 304 | PR("sendResp, stat: %X, trnsf: %x, rem: %x\n\r", status, transferred, remaining); |
| 305 | PR("Resp: %x %x %x .. %x", pArg[0], pArg[1], pArg[2], pArg[transferred-1]); |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 306 | |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 307 | for ( i = 0; i < transferred; i++ ) { |
| 308 | _ISO7816_SendChar(*(pArg++)); |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 309 | } |
Christina Quast | 4bcc023 | 2015-03-24 21:59:32 +0100 | [diff] [blame] | 310 | /* |
| 311 | if (*(pArg-1) == 0x8A) { |
| 312 | for (i=0; i<20000; i++) ; |
| 313 | _ISO7816_SendChar(0x90); |
| 314 | _ISO7816_SendChar(0x00); |
| 315 | } |
| 316 | */ |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 317 | state = WAIT_CMD_PHONE; |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 320 | extern ring_buffer buf; |
| 321 | #define MAX_MSG_LEN 64 |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 322 | #define PR printf |
| 323 | |
| 324 | void wait_for_response(uint8_t pBuffer[]) { |
| 325 | int ret = 0; |
| 326 | // uint8_t msg[] = {0xa0, 0xa4, 0x0, 0x0, 0x2}; |
| 327 | if (rcvdChar != 0) { |
| 328 | printf(" rr "); |
| 329 | /* DATA_IN for host side is data_out for simtrace side */ |
| 330 | /* FIXME: Performancewise sending a USB packet for every byte is a disaster */ |
Christina Quast | e90dece | 2015-04-03 11:40:22 +0200 | [diff] [blame] | 331 | ret = USBD_Write( PHONE_DATAIN, buf.buf, BUFLEN, 0, 0 ); |
| 332 | //USBD_Write( PHONE_DATAIN, msg, BUFLEN, 0, 0 ); |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 333 | PR("b:%x %x %x %x %x.\n\r", buf.buf[0], buf.buf[1],buf.buf[2], buf.buf[3], buf.buf[4]); |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 334 | |
| 335 | rcvdChar = 0; |
Christina Quast | 4bcc023 | 2015-03-24 21:59:32 +0100 | [diff] [blame] | 336 | } else if (timeout_occured && buf.idx != 0) { |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 337 | printf(" to "); |
Christina Quast | e90dece | 2015-04-03 11:40:22 +0200 | [diff] [blame] | 338 | ret = USBD_Write( PHONE_DATAIN, buf.buf, buf.idx, 0, 0 ); |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 339 | timeout_occured = 0; |
| 340 | buf.idx = 0; |
| 341 | rcvdChar = 0; |
| 342 | PR("b:%x %x %x %x %x.\n\r", buf.buf[0], buf.buf[1],buf.buf[2], buf.buf[3], buf.buf[4]); |
| 343 | } else { |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 344 | return; |
| 345 | } |
Christina Quast | e90dece | 2015-04-03 11:40:22 +0200 | [diff] [blame] | 346 | if ((ret = USBD_Read(PHONE_DATAOUT, pBuffer, MAX_MSG_LEN, |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 347 | (TransferCallback)&sendResponse, pBuffer)) == USBD_STATUS_SUCCESS) { |
Christina Quast | 1d80ef2 | 2015-04-03 11:39:38 +0200 | [diff] [blame] | 348 | PR("wait_rsp\n\r"); |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 349 | // state = WAIT_CMD_PC; |
| 350 | buf.idx = 0; |
| 351 | TC0_Counter_Reset(); |
| 352 | } else { |
Christina Quast | 1d80ef2 | 2015-04-03 11:39:38 +0200 | [diff] [blame] | 353 | PR("USB Err: %X", ret); |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 354 | return; |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
| 358 | // Sniffed Phone to SIM card communication: |
| 359 | // phone > sim : RST |
| 360 | // phone < sim : ATR |
| 361 | // phone > sim : A0 A4 00 00 02 (Select File) |
| 362 | // phone < sim : A4 (INS repeated) |
| 363 | // phone > sim : 7F 02 (= ??) |
| 364 | // phone < sim : 9F 16 (9F: success, can deliver 0x16 (=22) byte) |
| 365 | // phone > sim : ?? (A0 C0 00 00 16) |
| 366 | // phone < sim : C0 (INS repeated) |
| 367 | // phone < sim : 00 00 00 00 7F 20 02 00 00 00 00 00 09 91 00 17 04 00 83 8A (data of length 22 -2) |
| 368 | // phone <? sim : 90 00 (OK, everything went fine) |
| 369 | // phone ? sim : 00 (??) |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 370 | |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 371 | void Phone_run( void ) |
| 372 | { |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 373 | int ret; |
| 374 | uint8_t pBuffer[MAX_MSG_LEN]; |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 375 | int msg = RESET; |
| 376 | // FIXME: remove: |
| 377 | // uint8_t ATR[] = {0x3B, 0x9A, 0x94, 0x00, 0x92, 0x02, 0x75, 0x93, 0x11, 0x00, 0x01, 0x02, 0x02, 0x19}; |
| 378 | // send_ATR(ATR, (sizeof(ATR)/sizeof(ATR[0]))); |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 379 | switch (state) { |
| 380 | case RST_RCVD: |
Christina Quast | 6355ece | 2015-04-04 10:15:38 +0200 | [diff] [blame] | 381 | if ((ret = USBD_Write( PHONE_INT, &msg, 1, 0, 0 )) != USBD_STATUS_SUCCESS) { |
| 382 | PR("USB Error: %X", ret); |
| 383 | } |
Christina Quast | e90dece | 2015-04-03 11:40:22 +0200 | [diff] [blame] | 384 | //buf.idx = 0; |
| 385 | //rcvdChar = 0; |
| 386 | // TC0_Counter_Reset(); |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 387 | // send_ATR sets state to WAIT_CMD |
Christina Quast | e90dece | 2015-04-03 11:40:22 +0200 | [diff] [blame] | 388 | if ((ret = USBD_Read(PHONE_DATAOUT, pBuffer, MAX_MSG_LEN, (TransferCallback)&send_ATR, pBuffer)) == USBD_STATUS_SUCCESS) { |
| 389 | PR("Reading started sucessfully (ATR)"); |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 390 | state = WAIT_ATR; |
| 391 | } else { |
Christina Quast | 1d80ef2 | 2015-04-03 11:39:38 +0200 | [diff] [blame] | 392 | // PR("USB Error: %X", ret); |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 393 | //FIXME: state = ERR; |
| 394 | } |
| 395 | break; |
| 396 | case WAIT_CMD_PHONE: |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 397 | // FIXME: TC0_Counter_Reset(); |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 398 | wait_for_response(pBuffer); |
| 399 | break; |
Christina Quast | 0ca8390 | 2015-03-22 19:05:23 +0100 | [diff] [blame] | 400 | case NONE: |
| 401 | break; |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 402 | default: |
Christina Quast | 1d80ef2 | 2015-04-03 11:39:38 +0200 | [diff] [blame] | 403 | // PR(":("); |
Christina Quast | ce296b9 | 2015-03-18 18:41:19 +0100 | [diff] [blame] | 404 | break; |
Christina Quast | 578daaa | 2015-03-13 23:46:01 +0100 | [diff] [blame] | 405 | } |
| 406 | |
Christina Quast | fb524b9 | 2015-02-27 13:39:45 +0100 | [diff] [blame] | 407 | // FIXME: Function Phone_run not implemented yet |
| 408 | |
| 409 | /* Send and receive chars */ |
| 410 | // ISO7816_GetChar(&rcv_char); |
| 411 | // ISO7816_SendChar(char_to_send); |
| 412 | } |