sio is a library of routines for doing cross-platform serial I/O between most Unix varients and WIN32. WIN16 support is currently not in the code (as I don't have a need for it just at the moment).
sio is a collection of routines I've written at various times to handle serial comms that has been reworked into a much nicer interface on which portable application logic can be layered.
sio includes a test program (stest) designed for interfacing with low-level Smartcard readers. stest also has a SCAM low-level driver emulation mode for use with the dumbmouse reader. Using the perl-based SCAM code to probe cards is fairly straight forward.
The current documentation can be found at http://www.cryptsoft.com/scard/sio.html and the source code can be downloaded from ftp://ftp.cryptsoft.com/scard/.
Copyright 1993-1997, Tim Hudson. All rights reserved.
You can do what you like with this code except pretend that you wrote it provided that any derivative of this code includes the above comments unchanged. If you put this in a product then I would like to know about it. Credits in the documentation would be nice but are not essential.
Smartcard Handbook - Wolfgang Rankl, Wolfgang Effing ISBN 0471967203 pp 440 (Translation of Handbuch der Chipkarten - Carl Hanser Verlag)
To use sio simply include sio.h. Read the header file for the current up to date list of functions.
int getATR(char *devicename) { SIO_INFO *si; int ch; unsigned char achar; si=SIO_Open(devicename); if (si!=NULL) { /* setup for 9600, 8 data, 2 stop, even parity */ SIO_SetSpeed(s,9600); SIO_SetDataBits(s,8); SIO_SetStopBits(s,2); SIO_SetParity(s,SIO_PARITY_EVEN); SIO_SetIOMode(s,SIO_IOMODE_INDIRECT); /* now actually change the line settings */ SIO_WriteSettings(s); /* trigger ATR from a Smartcard */ SIO_DropRTS(s); SIO_RaiseRTS(s); /* read data until there is none available for * the timeout period */ while ((ch=SIO_ReadChar(s))!=-1) { achar=(unsigned char)(ch & 0xff); SIO_DumpBuffer(stderr,&achar,1,2); } /* finished ... cleanup time */ SIO_Close(si); } }
SIO_INFO holds all the relevant information related to a given serial connection. The state that is maintained is deliberately hidden from external view. If you stick to using the defined interfaces then your code will be portable (at least the part that talks to sio) between Unix and WIN32.
#define SIO_VERSION_MAJOR 1 #define SIO_VERSION_MINOR 1 int SIO_GetVersion(int *vmajor, int *vminor);
SIO_GetVersion returns the major and minor version number of the library.
SIO_INFO *SIO_Open(char *device);
int SIO_Close(SIO_INFO *s);
int SIO_SetLogFile(SIO_INFO *s,char *name);
All data read or written will be logged to the specified file. The file is created or it is truncated if it already exists. The data is logged before any data conversion when writing and after any data conversion or echo suppression when reading. This gives the logical application level view of the data rather than the low-level serial view.
void SIO_DumpBuffer(FILE *fp,char *buf,int len,int text);
Dump a data buffer in different formats.
int SIO_ReadSettings(SIO_INFO *s);
The current serial device parameters are fetched and converted into the sio view of the serial device.
int SIO_WriteSettings(SIO_INFO *s);
The current sio view of the serial device is used to update the serial device parameters. Don't forget to call this function otherwise any parameter updates that effect the serial device will not actually be in effect yet.
char *SIO_GetSettingsString(SIO_INFO *s);
Return a terse text representation of the current sio comms settings.
int SIO_SetSettingsString(SIO_INFO *s,char *str);
Using the same format returned by SIO_GetSettingsString update the internal sio comms settings. Don't forget to call SIO_WriteSettings to update the device settings to match.
int SIO_WaitForData(SIO_INFO *s,int timedelay);
Wait up to timedelay microseconds for data activity.
int SIO_ReadChar(SIO_INFO *s);
Read a single character. Returns -1 if no data available.
int SIO_ReadBuffer(SIO_INFO *s,char *buf,int len);
int SIO_WriteChar(SIO_INFO *s,int data);
int SIO_WriteBuffer(SIO_INFO *s,char *buf,int len);
sio also comes with a number of Smartcard related functions. To use these you need to also include sct0.h
int SCT0_Reset(SIO_INFO *s);
Reset the Smartcard - should respond with ATR
int SCT0_GetATR(SIO_INFO *s,char *buf,int bufsize);
Request ATR from smartcard. SCT0_GetATR calls SCT0_Reset to perform a standard Smartcard reset.
int SCT0_ScanForATR(SIO_INFO *s,char *buf, int bufsize);
Try all communication settings until a valid ATR is returned. Using this function you can rapidly locate what parameter combination is required for any Smartcard.
SCT0_ScanForATR returns the length of the ATR (or 0 if no valid setting is found). The valid ATR is copied into buf assuming that there is enough space.
int SCT0_ScanClasses(SIO_INFO *s, char *classlist);
Scan for valid command classes. classlist should be an array of 256 chars (one for each class) and on return each index in the array will be set to 1 if the Smartcard responded indicating that the class was valid or 0 if an error was reported.
int SCT0_ScanInstructions(SIO_INFO *s,char theclass,char *inslist);
Scan for valid instructions in a particular class. inslist should be an array of 256 chars (one for each instruction) and on return each index in the array will be set to 1 if the instruction is valid or 0 if it is not.
int SCT0_ScanFiles(SIO_INFO *s,char theclass,char thefile,char *filelist);
Scan for all files by FileID that start with the given byte of thefile. filelist should be an array of 256 chars (one for each file) and on return each index in the array will be set to 1 if the file thefileindex exists or 0 if it does not.