iso7816_test.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include <assert.h>
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/ioctl.h>
  11. #include "dsmart_card_interface.h"
  12. #include "ioctl.h"
  13. int main()
  14. {
  15. int ret, i;
  16. struct dsmart_card_baud baud_data;
  17. unsigned int protocol = DSMART_CARD_PROTOCOL_T0;
  18. struct dsmart_card_atr atr = { 0 };
  19. struct dsmart_card_rcv rcv_data = {};
  20. struct dsmart_card_xmt xmt_data = {};
  21. struct dsmart_card_timing timing_data;
  22. int fd = open("/dev/dsmart_card", O_RDWR);
  23. if (fd < 0) {
  24. perror("failed to open iso7816 smart card\n");
  25. exit(1);
  26. }
  27. printf("dsmart card cmd = 0x%x\n", DSMART_CARD_IOCTL_COLD_RESET);
  28. ret = ioctl(fd, DSMART_CARD_IOCTL_COLD_RESET, &atr);
  29. if (ret < 0) {
  30. printf("failed to get atr from slave card(%d)\n", ret);
  31. exit(1);
  32. }
  33. ret = ioctl(fd, DSMART_CARD_IOCTL_ATR_RCV, (unsigned long)&atr);
  34. if (ret < 0) {
  35. printf("failed to get atr data from slave card(%d)\n", ret);
  36. exit(1);
  37. }
  38. printf("\nATR data length: %d, result: %d, ATR DATA:\n", atr.len, atr.errval);
  39. for (i = 0; i < atr.len; i++)
  40. printf("0x%02x ", atr.atr_buffer[i]);
  41. printf("\n\nset transmision protocol T0\n");
  42. ret = ioctl(fd, DSMART_CARD_IOCTL_SET_PROTOCOL, &protocol);
  43. if (ret < 0) {
  44. printf("failed to set transmision protocol(%d)\n", ret);
  45. exit(1);
  46. }
  47. baud_data.di = 1;
  48. baud_data.fi = 1;
  49. printf("\nset baud rate, fi: %d, di: %d\n", baud_data.fi, baud_data.di);
  50. ret = ioctl(fd, DSMART_CARD_IOCTL_SET_BAUD, &baud_data);
  51. if (ret < 0) {
  52. printf("failed to set baud rate(%d)\n", ret);
  53. exit(1);
  54. }
  55. timing_data.wwt = 9600;
  56. timing_data.bgt = 0;
  57. timing_data.cwt = 0;
  58. timing_data.bwt = 0;
  59. timing_data.egt = 0;
  60. printf("\nset timming window, wwt: %d, bgt: %d, cwt: %d, bwt: %d, egt: %d\n", timing_data.wwt, timing_data.bgt, timing_data.cwt, timing_data.bwt, timing_data.egt);
  61. ret = ioctl(fd, DSMART_CARD_IOCTL_SET_TIMING, &timing_data);
  62. if (ret < 0) {
  63. printf("failed to set timing window(%d)\n", ret);
  64. exit(1);
  65. }
  66. printf("\nget data from sim card ");
  67. ret = ioctl(fd, DSMART_CARD_IOCTL_RCV, &rcv_data);
  68. if (ret < 0) {
  69. printf("failed to receive data from sim card\n");
  70. exit(1);
  71. }
  72. printf(", len: %d\n", rcv_data.rcv_length);
  73. for (i = 0; i < rcv_data.rcv_length; i++) {
  74. if (i % 8 == 0)
  75. printf("\n");
  76. printf("0x%x ", rcv_data.rcv_buffer[i]);
  77. }
  78. printf("\nreset the smart card\n");
  79. ret = ioctl(fd, DSMART_CARD_IOCTL_WARM_RESET, NULL);
  80. if (ret < 0) {
  81. printf("failed to reset the smart card(%d)\n", ret);
  82. exit(1);
  83. }
  84. ret = ioctl(fd, DSMART_CARD_IOCTL_ATR_RCV, (unsigned long)&atr);
  85. if (ret < 0) {
  86. printf("failed to get atr data from slave card(%d)\n", ret);
  87. exit(1);
  88. }
  89. printf("\n\nATR data length after warm reset: %d, result: %d, ATR DATA:\n", atr.len, atr.errval);
  90. for (i = 0; i < atr.len; i++)
  91. printf("0x%02x ", atr.atr_buffer[i]);
  92. printf("\n\nterminate the session\n");
  93. ret = ioctl(fd, DSMART_CARD_IOCTL_DEACTIVATE, NULL);
  94. if (ret < 0) {
  95. printf("failed to terminate the session\n");
  96. exit(1);
  97. }
  98. printf("\nsucceed to access smart card\n");
  99. close(fd);
  100. return 0;
  101. }