Esempio n. 1
0
int tty_read_section(int fd, char *buf, char stop_char, int timeout, int *nbytes_read)
{
    if (fd == -1)
           return TTY_ERRNO;

 int bytesRead = 0;
 int err = TTY_OK;
 *nbytes_read = 0;

 for (;;)
 {
         if ( (err = tty_timeout(fd, timeout)) )
	   return err;

         bytesRead = read(fd, buf, 1);

         if (bytesRead < 0 )
            return TTY_READ_ERROR;

        if (bytesRead)
          (*nbytes_read)++;

        if (*buf == stop_char)
	   return TTY_OK;

        buf += bytesRead;
  }

  return TTY_TIME_OUT;
}
Esempio n. 2
0
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read)
{
    if (fd == -1)
           return TTY_ERRNO;

 int bytesRead = 0;
 int err = 0;
 *nbytes_read =0;

  if (nbytes <=0)
	return TTY_PARAM_ERROR;

  while (nbytes > 0)
  {
     if ( (err = tty_timeout(fd, timeout)) )
      return err;

     bytesRead = read(fd, buf, ((unsigned) nbytes));

     if (bytesRead < 0 )
      return TTY_READ_ERROR;

     buf += bytesRead;
     *nbytes_read += bytesRead;
     nbytes -= bytesRead;
  }

  return TTY_OK;
}
Esempio n. 3
0
int tty_nread_section(int fd, char *buf, int nsize, char stop_char, int timeout, int *nbytes_read)
{
#ifdef _WIN32
    return TTY_ERRNO;
#else

    if (fd == -1)
        return TTY_ERRNO;

    int bytesRead = 0;
    int err       = TTY_OK;
    *nbytes_read  = 0;

    uint8_t *read_char = 0;

    if (tty_debug)
        IDLog("%s: Request to read until stop char '%#02X' with %d timeout for fd %d\n", __FUNCTION__, stop_char, timeout, fd);

    for (;;)
    {
        if ((err = tty_timeout(fd, timeout)))
            return err;

        read_char = (uint8_t*)(buf + *nbytes_read);
        bytesRead = read(fd, read_char, 1);

        if (bytesRead < 0)
            return TTY_READ_ERROR;

        if (tty_debug)
            IDLog("%s: buffer[%d]=%#X (%c)\n", __FUNCTION__, (*nbytes_read), *read_char, *read_char);

        (*nbytes_read)++;

        if (*read_char == stop_char)
            return TTY_OK;
        else if (*nbytes_read >= nsize)
            return TTY_OVERFLOW;
    }

    return TTY_TIME_OUT;

#endif
}
Esempio n. 4
0
int tty_read_section(int fd, char *buf, char stop_char, int timeout, int *nbytes_read)
{
#ifdef _WIN32
    return TTY_ERRNO;
#else

    char readBuffer[257]={0};

    if (fd == -1)
        return TTY_ERRNO;

    int bytesRead = 0;
    int err       = TTY_OK;
    *nbytes_read  = 0;

    uint8_t *read_char = 0;

    if (tty_debug)
        IDLog("%s: Request to read until stop char '%#02X' with %d timeout for fd %d\n", __FUNCTION__, stop_char, timeout, fd);

    if (ttyGeminiUdpFormat)
    {
        bytesRead = read(fd, readBuffer, 255);

        if (bytesRead < 0)
            return TTY_READ_ERROR;

        int *intSizedBuffer = (int *)readBuffer;
        if (intSizedBuffer[0] != sequenceNumber)
        {
            // Not the right reply just do the read again.
            return tty_read_section(fd, buf, stop_char, timeout, nbytes_read);
        }

        for (int index = 8; index < bytesRead; index++)
        {
            (*nbytes_read)++;

            if (*(readBuffer+index) == stop_char)
            {
                strncpy(buf, readBuffer+8, *nbytes_read);
                return TTY_OK;
            }
        }

    }
    else
    {
        for (;;)
        {
            if ((err = tty_timeout(fd, timeout)))
                return err;

            read_char = (uint8_t*)(buf + *nbytes_read);
            bytesRead = read(fd, read_char, 1);

            if (bytesRead < 0)
                return TTY_READ_ERROR;

            if (tty_debug)
                IDLog("%s: buffer[%d]=%#X (%c)\n", __FUNCTION__, (*nbytes_read), *read_char, *read_char);

            (*nbytes_read)++;

            if (*read_char == stop_char)
                return TTY_OK;
        }
    }

    return TTY_TIME_OUT;

#endif
}
Esempio n. 5
0
int tty_read(int fd, char *buf, int nbytes, int timeout, int *nbytes_read)
{
#ifdef _WIN32
    return TTY_ERRNO;
#else

    if (fd == -1)
        return TTY_ERRNO;

    int numBytesToRead =  nbytes;
    int bytesRead = 0;
    int err       = 0;
    *nbytes_read  = 0;

    if (nbytes <= 0)
        return TTY_PARAM_ERROR;

    if (tty_debug)
        IDLog("%s: Request to read %d bytes with %d timeout for fd %d\n", __FUNCTION__, nbytes, timeout, fd);

    char geminiBuffer[257]={0};
    char* buffer = buf;

    if (ttyGeminiUdpFormat)
    {
        numBytesToRead = nbytes + 8;
        buffer = geminiBuffer;
    }

    while (numBytesToRead > 0)
    {
        if ((err = tty_timeout(fd, timeout)))
            return err;

        bytesRead = read(fd, buffer + (*nbytes_read), ((uint32_t)numBytesToRead));

        if (bytesRead < 0)
            return TTY_READ_ERROR;

        if (tty_debug)
        {
            IDLog("%d bytes read and %d bytes remaining...\n", bytesRead, numBytesToRead - bytesRead);
            int i = 0;
            for (i = *nbytes_read; i < (*nbytes_read + bytesRead); i++)
                IDLog("%s: buffer[%d]=%#X (%c)\n", __FUNCTION__, i, (unsigned char)buf[i], buf[i]);
        }

        *nbytes_read += bytesRead;
        numBytesToRead -= bytesRead;
    }

    if (ttyGeminiUdpFormat)
    {
        int *intSizedBuffer = (int *)geminiBuffer;
        if (intSizedBuffer[0] != sequenceNumber)
        {
            // Not the right reply just do the read again.
            return tty_read(fd, buf, nbytes, timeout, nbytes_read);
        }

        *nbytes_read -= 8;
        memcpy(buf, geminiBuffer+8, *nbytes_read);
    }

    return TTY_OK;

#endif
}