📄 sci_prot.c
字号:
sci_parameters. clock_stop_polarity = SCI_CLOCK_STOP_LOW; break; case 2: case 3: sci_parameters. clock_stop_polarity = SCI_CLOCK_STOP_HIGH; break; } } } break; case TB: if (i == 1) { sc_cb[sc_id].sc_parameters.II = ((current & 0x60) >> 5); switch (sc_cb[sc_id].sc_parameters.II) { case 0: sci_parameters.I = 25; break; case 1: sci_parameters.I = 50; break; } sc_cb[sc_id].sc_parameters.PI1 = (current & 0x1F); sci_parameters.P = sc_cb[sc_id].sc_parameters.PI1; } else if (i == 2) { sc_cb[sc_id].sc_parameters.PI2 = current; /* if present PI2, overrides PI1 for the value of P */ /* PI2 is in decivolts, but P is in volts */ sci_parameters.P = sc_cb[sc_id].sc_parameters.PI2 / 10; } else if (i > 2) { /* TD(i-1) must indicate T=1 */ if (sc_cb[sc_id].currentT == 1) { /* extract CWI and BWI from TB(3) */ sc_cb[sc_id].sc_parameters.CWI = (current & 0x0F); sc_cb[sc_id].sc_parameters.BWI = ((current & 0xF0) >> 4); /* calculate CWT and BWT */ sci_parameters.CWT = 11+(1<<sc_cb[sc_id].sc_parameters.CWI); sci_parameters.BWT = 11+(1<<sc_cb[sc_id].sc_parameters.BWI)*960; } } break; case TC: if (i == 1) { /* extract N to determine EGT */ sc_cb[sc_id].sc_parameters.N = current; } else if (i == 2) { sc_cb[sc_id].sc_parameters.WI = current; sci_parameters.WWT = sc_cb[sc_id].sc_parameters.WI*960; } else if (i > 2) { /* TD(i-1) must indicate T=1 */ if (sc_cb[sc_id].currentT == 1) { /* determine error detection code */ if ((current & 0x01) == 0x00) { /* LRC checking is 1 byte (default) */ sci_parameters.check = 1; sc_cb[sc_id].sc_parameters. check = 1; } else { /* CRC checking is 2 bytes */ sci_parameters.check = 2; sc_cb[sc_id].sc_parameters. check = 2; } } } break; case TD: if (i == 1) { /* first offered protocol */ sc_cb[sc_id].firstT = current & 0x0F; /* assume card starts in negotiable mode using this protocol */ sci_parameters.T = sc_cb[sc_id].firstT; sc_cb[sc_id].sc_parameters.T = sci_parameters.T; } /* get current protocol T, add to mask */ sc_cb[sc_id].currentT = (current & 0x0F); sc_cb[sc_id].sc_parameters.maskT |= (1 << (sc_cb[sc_id].currentT)); /* check for presence of TA(i+1) */ if ((current & 0x10) == 0x10) { *p_ibytew = TA; p_ibytew++; } /* check for presence of TB(i+1) */ if ((current & 0x20) == 0x20) { *p_ibytew = TB; p_ibytew++; } /* check for presence of TC(i+1) */ if ((current & 0x40) == 0x40) { *p_ibytew = TC; p_ibytew++; } /* check for presence of TD(i+1) */ if ((current & 0x80) == 0x80) { *p_ibytew = TD; p_ibytew++; } else { /* this is the last TD, push any TK's and TCK into queue */ for (x = 0; x < sc_cb[sc_id].historical_size; x++) { *p_ibytew = TK; p_ibytew++; } if (sc_cb[sc_id].sc_parameters. maskT != 1) { /* If T=0 isn't the only offer, a check byte is present */ *p_ibytew = TCK; p_ibytew++; } } i++; break; case TK: if (sc_cb[sc_id].p_historical == 0) { sc_cb[sc_id].p_historical = (sc_cb[sc_id].ATR + index - 1); } break; case TCK: /* perform LRC check */ sc_cb[sc_id].TCK_present = 1; lrc = 0; for (x = 1; x < index; x++) { lrc = ((sc_cb[sc_id].ATR[x]) ^ lrc); } if (lrc != 0) { rc = SCI_ERROR_LRC_FAIL; } break; } p_ibyter++; } } if (rc == SCI_ERROR_OK) { /* set EGT based on N and protocol */ if ((sc_cb[sc_id].sc_parameters.N == 255) && (sc_cb[sc_id].sc_parameters.T == 0)) { /* for T=0, N=255 means minimum guard time in the SCI hardware */ sci_parameters.EGT = 0; } else { sci_parameters.EGT = sc_cb[sc_id].sc_parameters.N; } if (ioctl (fd[sc_id], IOCTL_SET_PARAMETERS, &sci_parameters) == 0) { rc = SCI_ERROR_OK; sc_cb[sc_id].atr_size = (unsigned char) index; } ioctl(fd[sc_id], IOCTL_SET_ATR_READY); } else { /* problem receiving ATR, so deactivate */ ioctl(fd[sc_id], IOCTL_SET_DEACTIVATE); } } else { rc = -1; ioctl(fd[sc_id], IOCTL_SET_DEACTIVATE); } } else { rc = -1; ioctl(fd[sc_id], IOCTL_SET_DEACTIVATE); } } else { rc = SCI_ERROR_CARD_NOT_PRESENT; } } else { rc = SCI_ERROR_PARAMETER_OUT_OF_RANGE; } return (rc);}/****************************************************************************** Name sc_pps**** Purpose: initiate a protocol and parameter selection request**** Parameters: sc_id: zero-based number to identify Smart Card controller** T: protocol to propose to the Smart Card** F: clock rate conversion factor to propose to the Smart Card** D: baud rate adjustment factor to propose to the Smart Card**** Returns: SCI_ERROR_OK: If the PPS exchange was successful** other error code if a failure occurs****************************************************************************/SCI_ERROR sc_pps(unsigned long sc_id, unsigned char T, unsigned char F, unsigned char D){ SCI_ERROR rc = SCI_ERROR_OK; SCI_PARAMETERS sci_parameters; unsigned char request[6], response[6]; unsigned long readit, index, left, i; unsigned long Fi = 0; unsigned long Di = 0; unsigned long f = 0; unsigned char current_T = 0; unsigned long current_f = 0; unsigned long current_ETU = 0; unsigned long card_actived; if (sc_id < SCI_NUMBER_OF_CONTROLLERS) { ioctl(fd[sc_id], IOCTL_GET_IS_CARD_ACTIVATED, &card_actived); if (card_actived == 1) { /* PPSS: initial byte-always 0xFF */ request[0] = 0xFF; /* PPS0: format byte-encodes T and PPS1 to follow */ request[1] = (0x10 | T); /* PPS1: parameter byte-encodes F and D */ request[2] = (D | (F << 4)); /* PPS2 and PPS3 are RFU, so ignore */ /* PCK : check byte-bitwise XOR of PPSS,PPS0,and PPS1 */ request[3] = (request[0] ^ request[1] ^ request[2]); response[0] = 0; response[1] = 0; response[2] = 0; response[3] = 0; response[4] = 0; response[5] = 0; index = 0; readit = 0; left = 3; if ((rc = write(fd[sc_id], request, 4)) > 0) { rc = SCI_ERROR_OK; /* make sure card responds and the response is good */ while ((rc == SCI_ERROR_OK) && (left > 0)) { if ((readit = read(fd[sc_id], (response + index), 1)) > 0) { rc = SCI_ERROR_OK; } else { rc = readit; } if (readit == 1) { left--; if (index == 1) { for (i = 0; i < 3; i++) { if ((response[1] & (0x10 << i)) == (0x10 << i)) { left++;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -