// Perform 'select' on the specified handler(s), returning a single byte // if it could be read (plus the object ID in the upper 8 bits) and -1 // otherwise int ser_select_byte( ser_handler *pobjects, unsigned nobjects, int timeout ) { int i; DWORD readbytes, dwRes; int res = -1; unsigned num_wait = 0; ser_handler hnd; if( nobjects >= MAXIMUM_WAIT_OBJECTS ) return -1; // Try to read directly first for( i = 0; i < nobjects; i ++ ) { if( !pobjects[ i ]->fWaitingOnRead ) { init_ov( &pobjects[ i ]->o ); if( ReadFile( pobjects[ i ]->hnd, &pobjects[ i ]->databuf, 1, &readbytes, &pobjects[ i ]->o ) ) return readbytes != 1 ? -1 : pobjects[ i ]->databuf | ( i << 8 ); if( GetLastError() != ERROR_IO_PENDING ) return -1; pobjects[ i ]->fWaitingOnRead = TRUE; } sel_handler_map[ num_wait ] = i; sel_handlers[ num_wait ++ ] = pobjects[ i ]->o.hEvent; } if( num_wait == 0 ) return -1; dwRes = WaitForMultipleObjects( num_wait, sel_handlers, FALSE, timeout == SER_INF_TIMEOUT ? INFINITE : timeout ); if( dwRes >= WAIT_OBJECT_0 && dwRes < WAIT_OBJECT_0 + num_wait ) { i = dwRes - WAIT_OBJECT_0; hnd = pobjects[ sel_handler_map[ i ] ]; hnd->fWaitingOnRead = FALSE; if( GetOverlappedResult( hnd->hnd, &hnd->o, &readbytes, TRUE ) && readbytes == 1 ) res = hnd->databuf | ( sel_handler_map[ i ] << 8 ); ResetEvent( hnd->o.hEvent ); } else if( dwRes == WAIT_TIMEOUT ) { for( i = 0; i < num_wait; i ++ ) { hnd = pobjects[ sel_handler_map[ i ] ]; hnd->fWaitingOnRead = FALSE; CancelIo( hnd->hnd ); } WaitForMultipleObjects( num_wait, sel_handlers, TRUE, INFINITE ); for( i = 0; i < num_wait; i ++ ) ResetEvent( pobjects[ sel_handler_map[ i ] ]->o.hEvent ); } return res; }
// Write up to the specified number of bytes, return bytes actually written u32 ser_write( ser_handler id, const u8 *src, u32 size ) { HANDLE hComm = id->hnd; DWORD written; init_ov( &id->o_wr ); if( WriteFile( hComm, src, size, &written, &id->o_wr ) ) return written; if( GetLastError() != ERROR_IO_PENDING ) return 0; if( !GetOverlappedResult( hComm, &id->o_wr, &written, TRUE ) ) written = 0; ResetEvent( id->o_wr.hEvent ); return written; }
// Read up to the specified number of bytes, return bytes actually read u32 ser_read( ser_handler id, u8* dest, u32 maxsize, u32 timeout ) { DWORD readbytes = 0; if( id->fWaitingOnRead ) { readbytes = ser_read_internal( id, timeout ); dest[0] = id->databuf; } else { init_ov( &id->o ); if( ReadFile( id->hnd, dest, maxsize, &readbytes, &id->o ) ) return readbytes; if( GetLastError() != ERROR_IO_PENDING ) return 0; id->fWaitingOnRead = TRUE; // XXX: consider removing statement readbytes = ser_read_internal( id, timeout ); } id->fWaitingOnRead = FALSE; return readbytes; }
int main(int argc, char *argv[]) { if (argc < 2) return (-1); const char *mod = argv[1]; int fd = -1; init_modules(); polysome *p = get_module(mod); if (p == NULL) { printf("%s invalid\n", mod); } else { printf("%s valid\n", p->name); ovvar_t ov; memset(&ov, 0, sizeof(ov)); init_ov(&ov); printf("path %s\n", ov.path); p->ss(&ov); printf("path %s be read\n", ov.fpath); printf("ov.a: %d, ov.b: %d\n", ov.a, ov.b); p->gs(&ov); for (rci_t i = 0; i < (ov.a + ov.b); i ++) printf("%ld ", ov.signature[i]); printf("\n"); free_ov(&ov); } free_modules(); return (0); }