ISO7816 序列通訊

1. 簡介

ISO/IEC7816 是一系列標準,用於指定積體電路卡 (ICC),也稱為智慧卡。

3. 核心中已有的資料結構

Linux 核心提供 serial_iso7816 結構(參見 [1])來處理 ISO7816 通訊。此資料結構用於在 ioctl 中設定和配置 ISO7816 引數。

任何能夠作為 RS232 和 ISO7816 工作的裝置的驅動程式都應在 uart_port 結構中實現 iso7816_config 回撥。serial_core 呼叫 iso7816_config 來執行裝置特定的部分,以響應 TIOCGISO7816 和 TIOCSISO7816 ioctl(見下文)。iso7816_config 回撥接收指向 struct serial_iso7816 的指標。

4. 來自使用者級別的用法

從使用者級別,可以使用先前的 ioctl 獲取/設定 ISO7816 配置。例如,要設定 ISO7816,可以使用以下程式碼

#include <linux/serial.h>

/* Include definition for ISO7816 ioctls: TIOCSISO7816 and TIOCGISO7816 */
#include <sys/ioctl.h>

/* Open your specific device (e.g., /dev/mydevice): */
int fd = open ("/dev/mydevice", O_RDWR);
if (fd < 0) {
        /* Error handling. See errno. */
}

struct serial_iso7816 iso7816conf;

/* Reserved fields as to be zeroed */
memset(&iso7816conf, 0, sizeof(iso7816conf));

/* Enable ISO7816 mode: */
iso7816conf.flags |= SER_ISO7816_ENABLED;

/* Select the protocol: */
/* T=0 */
iso7816conf.flags |= SER_ISO7816_T(0);
/* or T=1 */
iso7816conf.flags |= SER_ISO7816_T(1);

/* Set the guard time: */
iso7816conf.tg = 2;

/* Set the clock frequency*/
iso7816conf.clk = 3571200;

/* Set transmission factors: */
iso7816conf.sc_fi = 372;
iso7816conf.sc_di = 1;

if (ioctl(fd_usart, TIOCSISO7816, &iso7816conf) < 0) {
        /* Error handling. See errno. */
}

/* Use read() and write() syscalls here... */

/* Close the device when finished: */
if (close (fd) < 0) {
        /* Error handling. See errno. */
}

5. 參考資料

[1] include/uapi/linux/serial.h