📄 usrusbtool.c
字号:
txBfrCount = fread (txBfr, sizeof (char), sizeof (txBfr), txFile);
txBfrIndex = 0;
}
if (txCharCount == 0 || txBfrCount == 0)
{
closeTxFile ();
txCharCount = 0;
}
if (txBfrCount == 0)
return ERROR;
*txChar = txBfr [txBfrIndex++];
--txBfrCount;
}
return OK;
}
/*************************************************************************
*
* prnAttachCallback - receives attach callbacks from printer SIO driver
*
* RETURNS: N/A
*/
LOCAL SIO_CHAN *pPrnSioChan;
LOCAL VOID prnAttachCallback
(
pVOID arg, /* caller-defined argument */
SIO_CHAN *pChan, /* pointer to affected SIO_CHAN */
UINT16 attachCode /* defined as USB_KBD_xxxx */
)
{
FILE *fout = (FILE *) arg;
fprintf (fout, "pChan = %p, attach code = %s\n", pChan,
(attachCode == USB_PRN_ATTACH) ? "USB_PRN_ATTACH" : "USB_PRN_REMOVE");
if (attachCode == USB_PRN_ATTACH)
{
if (pPrnSioChan == NULL)
{
if (usbPrinterSioChanLock (pChan) != OK)
fprintf (fout, "usbPrinterSioChanLock() returned ERROR\n");
else
{
pPrnSioChan = pChan;
if ((*pPrnSioChan->pDrvFuncs->callbackInstall) (pPrnSioChan,
SIO_CALLBACK_GET_TX_CHAR, prnTxCallback, NULL) != OK)
{
fprintf (fout, "callbackInstall() returned ERROR.\n");
}
}
}
else
{
fprintf (fout, "Another channel already in use, ignored.\n");
}
}
else
{
if (pChan == pPrnSioChan)
{
if (usbPrinterSioChanUnlock (pChan) != OK)
fprintf (fout, "usbPrinterSioChanUnlock() returned ERROR\n");
pPrnSioChan = NULL;
}
}
}
/*************************************************************************
*
* cmdPrnInit - initializes USB printer SIO driver
*
* RETURNS: RET_CONTINUE
*/
LOCAL BOOL prnInitialized = FALSE;
LOCAL UINT16 cmdPrnInit
(
pVOID Param, /* Generic parameter passed down */
char **ppCmd, /* Ptr to remainder of cmd line */
FILE *fin, /* stream for input (if any) */
FILE *fout /* stream for output (if any) */
)
{
if (prnInitialized)
{
fprintf (fout, "USB printer SIO driver already initialized.\n");
return RET_CONTINUE;
}
if (usbPrinterDevInit () == OK)
{
fprintf (fout, "usbPrinterDevInit() returned OK\n");
prnInitialized = TRUE;
/* Register for attach notification */
if (usbPrinterDynamicAttachRegister (prnAttachCallback, (pVOID) fout) != OK)
{
fprintf (fout, "usbPrinterDynamicAttachRegister() returned ERROR\n");
return RET_CONTINUE;
}
}
else
fprintf (fout, "usbPrinterDevInit() returned ERROR\n");
return RET_CONTINUE;
}
/*************************************************************************
*
* cmdPrnDown - shuts down USB printer SIO driver
*
* RETURNS: RET_CONTINUE
*/
LOCAL UINT16 cmdPrnDown
(
pVOID Param, /* Generic parameter passed down */
char **ppCmd, /* Ptr to remainder of cmd line */
FILE *fin, /* stream for input (if any) */
FILE *fout /* stream for output (if any) */
)
{
if (!prnInitialized)
{
fprintf (fout, "USB printer SIO driver not initialized.\n");
return RET_CONTINUE;
}
prnInitialized = FALSE;
pPrnSioChan = NULL;
/* unregister */
if (usbPrinterDynamicAttachUnRegister (prnAttachCallback, (pVOID) fout) != OK)
fprintf (fout, "usbPrinterDynamicAttachUnRegister() returned ERROR\n");
if (usbPrinterDevShutdown () == OK)
fprintf (fout, "usbPrinterDevShutdown() returned OK\n");
else
fprintf (fout, "usbPrinterDevShutdown() returned ERROR\n");
return RET_CONTINUE;
}
/*************************************************************************
*
* waitForPrinter - waits for a printer to be connected
*
* RETURNS: OK if printer connected, else ERROR
*/
LOCAL STATUS waitForPrinter
(
FILE *fout
)
{
THREAD_HANDLE thread;
UINT8 bfr [USB_PRN_MAX_DEVICE_ID_LEN];
pUSB_PRINTER_CAPABILITIES pCaps = (pUSB_PRINTER_CAPABILITIES) bfr;
UINT16 idLen;
UINT8 protocol;
UINT16 i;
/* Create thread to watch for keypress */
enterPressed = FALSE;
if (OSS_THREAD_CREATE (enterThread, (pVOID) fout, OSS_PRIORITY_INHERIT, "tEnter",
&thread) != OK)
{
fprintf (fout, "Error creating thread.\n");
return ERROR;
}
/* Wait for a printer to be attached. */
if (pPrnSioChan == NULL)
{
fprintf (fout, "Waiting for printer to be attached...\n");
while (!enterPressed && pPrnSioChan == NULL)
OSS_THREAD_SLEEP (1);
}
/* kill keypress thread */
OSS_THREAD_DESTROY (thread);
if (enterPressed)
return ERROR;
/* Display the printer characteristics. */
if ((*pPrnSioChan->pDrvFuncs->ioctl) (pPrnSioChan, SIO_USB_PRN_DEVICE_ID_GET,
(void *) &bfr) != OK)
fprintf (fout, "ioctl (SIO_USB_PRN_DEVICE_ID_GET) returned ERROR.\n");
else
{
idLen = FROM_BIGW (pCaps->length);
fprintf (fout, "Device ID length = %d\n", idLen);
fprintf (fout, "Device ID = ");
for (i = 0; i < idLen - 2; i++)
fprintf (fout, "%c", pCaps->caps [i]);
fprintf (fout, "\n");
}
if ((*pPrnSioChan->pDrvFuncs->ioctl) (pPrnSioChan, SIO_USB_PRN_PROTOCOL_GET,
(void *) &protocol) != OK)
fprintf (fout, "ioctl (SIO_USB_PRN_PROTOCOL_GET) returned ERROR.\n");
else
{
fprintf (fout, "protocol = 0x%x ", protocol);
switch (protocol)
{
case USB_PROTOCOL_PRINTER_UNIDIR:
fprintf (fout, "(USB_PROTOCOL_PRINTER_UNIDIR)\n");
break;
case USB_PROTOCOL_PRINTER_BIDIR:
fprintf (fout, "(USB_PROTOCOL_PRINTER_BIDIR)\n");
break;
default:
fprintf (fout, "(unknown)\n");
break;
}
}
return OK;
}
/*************************************************************************
*
* cmdPrint - performs printer test
*
* RETURNS: RET_CONTINUE
*/
LOCAL UINT16 cmdPrint
(
pVOID Param, /* Generic parameter passed down */
char **ppCmd, /* Ptr to remainder of cmd line */
FILE *fin, /* stream for input (if any) */
FILE *fout /* stream for output (if any) */
)
{
long blocks;
/* Get parameters */
*ppCmd = GetHexToken (*ppCmd, &blocks, 1);
/* Wait for a printer to be connected */
if (waitForPrinter (fout) == OK)
{
/* trigger a transmission. */
patternTest = TRUE;
nextCharVal = 0;
txCharCount = blocks * 4096;
fprintf (fout, "sending %d 4k blocks to printer...\n", (UINT16) blocks);
if ((*pPrnSioChan->pDrvFuncs->txStartup) (pPrnSioChan) != OK)
fprintf (fout, "txStartup() returned ERROR.\n");
}
return RET_CONTINUE;
}
/*************************************************************************
*
* cmdPrintFnm - sends file to printer
*
* RETURNS: RET_CONTINUE
*/
LOCAL UINT16 cmdPrintFnm
(
pVOID Param, /* Generic parameter passed down */
char **ppCmd, /* Ptr to remainder of cmd line */
FILE *fin, /* stream for input (if any) */
FILE *fout /* stream for output (if any) */
)
{
char fnm [MAX_CMD_LEN];
FILE *f;
long fsize;
/* Get filename parameter */
*ppCmd = GetNextToken (*ppCmd, fnm, sizeof (fnm));
/* Terminate any current dump test */
closeTxFile ();
/* Attempt to open file */
if ((f = fopen (fnm, "rb")) == NULL)
{
fprintf (fout, "Unable to open '%s'.\n", fnm);
return RET_CONTINUE;
}
/* Determine file size */
fseek (f, 0, SEEK_END);
fsize = ftell (f);
fprintf (fout, "file '%s' is %ld bytes.\n", fnm, fsize);
fseek (f, 0, SEEK_SET);
/* Wait for printer */
if (waitForPrinter (fout) == OK)
{
/* Initiate transmission */
patternTest = FALSE;
txFile = f;
txBfrCount = 0;
txCharCount = fsize;
fprintf (fout, "sending %ld bytes to printer...\n", fsize);
if ((*pPrnSioChan->pDrvFuncs->txStartup) (pPrnSioChan) != OK)
{
fprintf (fout, "txStartup() returned ERROR.\n");
closeTxFile ();
}
/* NOTE: If this path is successful, the file will be closed by
* the prnTxCallback().
*/
}
else
{
/* Close input file */
fclose (f);
}
return RET_CONTINUE;
}
#endif /*INCLUDE_USB_PRINTER*/
#ifdef INCLUDE_USB_SPEAKER
/*************************************************************************
*
* spkrAttachCallback - receives attach callbacks from speaker SEQ_DEV driver
*
* RETURNS: N/A
*/
LOCAL SEQ_DEV *pSpkrSeqDev = NULL;
LOCAL VOID spkrAttachCallback
(
pVOID arg, /* caller-defined argument */
SEQ_DEV *pSeqDev, /* pointer to affected SEQ_DEV */
UINT16 attachCode /* defined as USB_KBD_xxxx */
)
{
FILE *fout = (FILE *) arg;
fprintf (fout, "pSeqDev = %p, attach code = %s\n", pSeqDev,
(attachCode == USB_SPKR_ATTACH) ? "USB_SPKR_ATTACH" : "USB_SPKR_REMOVE");
if (attachCode == USB_SPKR_ATTACH)
{
if (pSpkrSeqDev == NULL)
{
if (usbSpeakerSeqDevLock (pSeqDev) != OK)
fprintf (fout, "usbSpeakerSeqDevLock() returned ERROR\n");
else
{
pSpkrSeqDev = pSeqDev;
}
}
else
{
fprintf (fout, "Another channel already in use, ignored.\n");
}
}
else
{
if (pSeqDev == pSpkrSeqDev)
{
if (usbSpeakerSeqDevUnlock (pSeqDev) != OK)
fprintf (fout, "usbSpeakerSeqDevUnlock() returned ERROR\n");
pSpkrSeqDev = NULL;
}
}
}
/***************************************************************************
*
* audioThread - Dumps audio data to usbSpeakerLib
*
* By convention, <param> is the file handle for the file to be played and
* the global "wavDataLength" should be the length of the data chunk. The
* file position should be set to the beginning of the data in the data chunk.
*
* This thread closes the file after reading all data.
*
* RETURNS: N/A
*/
LOCAL BOOL audioThreadBusy = FALSE;
LOCAL UINT32 wavDataLen;
LOCAL VOID audioThread
(
pVOID param
)
{
FILE *wavFile = (FILE *) param;
pUINT8 pBfr;
UINT32 remDataLen = wavDataLen;
UINT32 actLen;
/* Create a buffer for audio data */
if ((pBfr = OSS_MALLOC (AUDIO_BFR_SIZE)) == NULL)
{
printf ("Out of memory creating audio buffer.\n");
}
else
{
/* open the audio stream. */
if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev,
USB_SPKR_IOCTL_OPEN_AUDIO_STREAM, 0) != OK)
{
printf ("IOCTL OPEN_AUDIO_STREAM returned ERROR.\n");
}
else
{
/* Read audio data and pass it to usbSpeakerLib. */
while (remDataLen > 0 &&
(actLen = fread (pBfr, 1, min (remDataLen, AUDIO_BFR_SIZE),
wavFile)) > 0)
{
if ((*pSpkrSeqDev->sd_seqWrt) (pSpkrSeqDev, actLen,
pBfr, FALSE) != OK)
{
printf ("sd_seqWrt() returned ERROR.\n");
break;
}
else
{
remDataLen -= actLen;
}
}
/* Mark the end of the audio stream. */
if ((*pSpkrSeqDev->sd_ioctl) (pSpkrSeqDev,
USB_SPKR_IOCTL_CLOSE_AUDIO_STREAM, 0) != OK)
{
printf ("IOCTL CLOSE_AUDIO_STREAM returned ERROR.\n");
}
}
OSS_FREE (pBfr);
}
/* Close the input file. */
fclose (wavFile);
audioThreadBusy = FALSE;
}
/*************************************************************************
*
* cmdSpkrInit - initializes USB speaker SEQ_DEV driver
*
* RETURNS: RET_CONTINUE
*/
LOCAL BOOL spkrInitialized = FALSE;
LOCAL UINT16 cmdSpkrInit
(
pVOID Param, /* Generic parameter passed down */
char **ppCmd, /* Ptr to remainder of cmd line */
FILE *fin, /* stream for input (if any) */
FILE *fout /* stream for output (if any) */
)
{
if (spkrInitialized)
{
fprintf (fout, "USB speaker SEQ_DEV driver already initialized.\n");
return RET_CONTINUE;
}
if (usbSpeakerDevInit () == OK)
{
fprintf (fout, "usbSpeakerDevInit() returned OK\n");
spkrInitialized = TRUE;
/* Register for attach notification */
if (usbSpeakerDynamicAttachRegister (spkrAttachCallback, (pVOID) fout) != OK)
{
fprintf (fout, "usbSpeakerDynamicAttachRegister() returned ERROR\n");
return RET_CONTINUE;
}
}
el
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -