#include #include #include #include #include #include #include #include // ---------------------------------------------------------------------------- void SETDTR(int fd) { // sets the SC8in1 in command mode int lineData; ioctl(fd, TIOCMGET, &lineData); lineData |= TIOCM_DTR; ioctl(fd, TIOCMSET, &lineData); } // ---------------------------------------------------------------------------- void RESETDTR(int fd) { // sets the SC8in1 in normal mode int lineData; ioctl(fd, TIOCMGET, &lineData); lineData &= ~TIOCM_DTR; lineData &= ~TIOCM_RTS; ioctl(fd, TIOCMSET, &lineData); } // ---------------------------------------------------------------------------- int GETCTS(int fd) { // returns the SC8in1 Status // 0= no card was changed (inserted or removed) // 1= one ore more cards were changed (inserted or removed) int result; int lineData; ioctl(fd, TIOCMGET, &lineData); result= (lineData & TIOCM_CTS) / TIOCM_CTS; return(result-1); } // ---------------------------------------------------------------------------- int readsc8in1( int fd) { // Reads the card status // // the bits in the return bytes: // bit0=1 means Slot1=Smartcard inside // bit1=1 means Slot2=Smartcard inside // bit2=1 means Slot3=Smartcard inside // bit3=1 means Slot4=Smartcard inside // bit4=1 means Slot5=Smartcard inside // bit5=1 means Slot6=Smartcard inside // bit6=1 means Slot7=Smartcard inside // bit7=1 means Slot8=Smartcard inside int res,i; unsigned char tmp[128]; struct termios termio, termiobackup; // backup data tcgetattr(fd,&termio); memcpy(&termiobackup,&termio,sizeof(termio)); // switch SC8in1 to command mode RESETDTR(fd); SETDTR(fd); // set communication parameters termio.c_oflag = 0; termio.c_lflag = 0; termio.c_cc[VTIME] = 1; // working termio.c_cflag = B9600|CS8|CREAD|CLOCAL; if (tcsetattr(fd,TCSANOW,&termio) < 0) { printf("ERROR: RS232 attributes\n"); return(-1); } // get SC8in1 info tmp[0]=0x47; res=write(fd,tmp,1); usleep(50000); res=read(fd,tmp,10); if ( res==0 ) return(-1); // ERROR ! // switch SC8in1 to normal mode RESETDTR(fd); // restore data memcpy(&termio,&termiobackup,sizeof(termio)); if (tcsetattr(fd,TCSANOW,&termio) < 0) { printf("ERROR: RS232 attributes\n"); return(-1); } if (tmp[res-8]!=0x90) return(-1); // ERROR ! // return result byte return(tmp[res-7]); } // ---------------------------------------------------------------------------- int selectslot(int fd, int slot) { // selects the Smartcard Socket "slot" // int res; unsigned char tmp[128]; struct termios termio, termiobackup; // backup rs232 data tcgetattr(fd,&termio); memcpy(&termiobackup,&termio,sizeof(termio)); // switch SC8in1 to command mode SETDTR(fd); // set communication parameters termio.c_cc[VTIME] = 1; // working termio.c_cflag = B9600|CS8|CREAD|CLOCAL; if (tcsetattr(fd,TCSANOW,&termio) < 0) { printf("ERROR: RS232 attributes\n"); return(-1); } // selecd select slot command to SC8in1 tmp[0]=0x53; tmp[1]=slot&0x0F; res=write(fd,tmp,2); // switch SC8in1 to normal mode RESETDTR(fd); // restore rs232 data memcpy(&termio,&termiobackup,sizeof(termio)); if (tcsetattr(fd,TCSANOW,&termio) < 0) { printf("ERROR: RS232 attributes\n"); return(-1); } return(0); } // ---------------------------------------------------------------------------- int main (void) { int fd,result; printf("sc8in1 test utility 1.01\n"); // open com port if((fd=open("/dev/ttyS0",O_RDWR | O_NOCTTY|O_NONBLOCK )) <= 0) { int err = errno; printf("ERROR: open rs232 port: %s\n", strerror(err)); return(-1); } result=readsc8in1(fd); if ( result==-1 ) { printf("No sc8in1 found\n"); return(-1); } if ( result==0 ) { printf("No Smartcard inside sc8in1\n"); } else { if ((result&0x01)!=0) printf("Slot1=Smartcard inside\n"); if ((result&0x02)!=0) printf("Slot2=Smartcard inside\n"); if ((result&0x04)!=0) printf("Slot3=Smartcard inside\n"); if ((result&0x08)!=0) printf("Slot4=Smartcard inside\n"); if ((result&0x10)!=0) printf("Slot5=Smartcard inside\n"); if ((result&0x20)!=0) printf("Slot6=Smartcard inside\n"); if ((result&0x40)!=0) printf("Slot7=Smartcard inside\n"); if ((result&0x80)!=0) printf("Slot8=Smartcard inside\n"); } return(0); }