/* Function: rtIOStreamRecv ================================================
 * Abstract: receive data
 *
 */
int rtIOStreamRecv(
    int      streamID,
    void   * const dst,
    size_t   size,
    size_t * sizeRecvd)
{
    int retVal = RTIOSTREAM_NO_ERROR;
    SerialCommsData *sd = getSerialData(streamID);
    if (sd == NULL) {
        retVal = RTIOSTREAM_ERROR;
        return retVal;
    }
    retVal = serialDataGet( sd, (char *)dst, size, sizeRecvd);
    if (sd->verbosity) {
        printf("rtIOStreamRecv (connection id %d): size = %lu, sizeRecvd = %lu",
               streamID, (unsigned long) size, (unsigned long) *sizeRecvd);
        if (sd->verbosity >= VERBOSITY_LEVEL_2) {
            size_t currElement;
            printf(": ");
            for (currElement = 0; currElement < *sizeRecvd; currElement++) {
                printf("%u ", ((unsigned char *) dst)[currElement]);
            }
        }
        printf("\n");
    }
    return retVal;
}
/* Function: serialDataFlush =================================================
 * Abstract:
 *  Utility function to flush a port
 */
static int serialDataFlush(
    SerialCommsData *sd)
{
    char tmpBuf[TMP_BUF_SIZ];
    size_t numRecvd = 0;
    int pending = 0;
    int error;
    static const char *fnName = "serialDataFlush:";

    do {
        error = serialDataPending( sd, &pending);
        if ( (pending > 0) && (error==RTIOSTREAM_NO_ERROR) ) {
            if (sd->verbosity) {
                printf("serialDataFlush: pending = %d\n", pending);
            }
            error = serialDataGet( sd, tmpBuf, sizeof( tmpBuf), &numRecvd);
            if (sd->verbosity) {
                size_t currElement;
                printf("serialDataFlush: sizeRecvd = %lu: ", (unsigned long) numRecvd);
                for (currElement = 0; currElement < numRecvd; currElement++) {
                    printf("%u ", (unsigned char) tmpBuf[currElement]);
                }
                printf("\n");
            }
        }
    }  while (  (pending > 0) && (error==RTIOSTREAM_NO_ERROR) && numRecvd > 0);
    if (error == RTIOSTREAM_ERROR) {
        printf( "%s Flushing returned RTIOSTREAM_ERROR\n", fnName);
    }
    return error;
}
/* Function: rtIOStreamRecv ================================================
 * Abstract: receive data
 *
 */
int rtIOStreamRecv(
    int      streamID,
    void   * dst, 
    size_t   size,
    size_t * sizeRecvd) 
{
    int retVal = RTIOSTREAM_NO_ERROR;
    SerialCommsData *sd;

    sd = &SerialData[streamID];

    retVal = serialDataGet( sd, (char *)dst, size, sizeRecvd);

    return retVal;
}
/* Function: serialDataFlush =================================================
 * Abstract:
 *  Utility function to flush a port
 */
static int serialDataFlush( 
    SerialCommsData *sd)
{
    char tmpBuf[TMP_BUF_SIZ];
    int numRecvd;
    int pending = 0;
    int error;
    static const char *fnName = "serialDataFlush:";

    do {
        error = serialDataPending( sd, &pending);

        if ( (pending > 0) && (error==RTIOSTREAM_NO_ERROR) ) {
            error = serialDataGet( sd, tmpBuf, sizeof( tmpBuf), (size_t *) (&numRecvd));
        }
    }  while (  (pending > 0) && (error==RTIOSTREAM_NO_ERROR) && numRecvd > 0);
    if (error == RTIOSTREAM_ERROR) {
        printf( "%s Flushing returned RTIOSTREAM_ERROR\n", fnName);
    }
    return error;
}