Ejemplo n.º 1
0
int NetworkInputStream::readBufferData(void* data, __int32 len) {
	if (checkStatus() < 0) {
		return -1;
	}
	// wait until a data is available to be readed
	int readed = 0;
	while (readed < len) {
#ifdef DEBUG
		if (waitAvailable(60) < 0) {
#else 
		if (waitAvailable(10) < 0) {
#endif
			assert(false);
			return -1;
		}
		int read = (len - readed);
		if ((_bufferSize - _bufferPos) < read) {
			read = _bufferSize - _bufferPos;
		}
		memcpy((char*)data + readed, _buffer + _bufferPos, read);
		_bufferPos += read;
		//        int read = recv(_socket, data, len, 0);
		//        // the connection could be closed
		//        assert(read != 0);
		readed += read;
	}

	return readed;
}

int NetworkInputStream::waitAvailable(int timeout) {
	if (_bufferPos >= _bufferSize) {
		int result = fillBuffer(timeout);
		return result;
	}
	return 0;
}

int NetworkInputStream::setNonblocking() {
	int flags;

	/* If they have O_NONBLOCK, use the Posix way to do it */
#if defined(O_NONBLOCK)
	/* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */
	if (-1 == (flags = fcntl(_socket, F_GETFL, 0)))
		flags = 0;
	return fcntl(_socket, F_SETFL, flags | O_NONBLOCK);
#else
#ifndef WINDOWS
	/* Otherwise, use the old way of doing it */
	flags = 1;
	return ioctl(_socket, FIOBIO, &flags);
#else
	u_long f = 1;
	return ioctlsocket(_socket, FIONBIO, &f);
#endif
#endif
}
Ejemplo n.º 2
0
//0 for non-blocking (returns immediately), -1 for infinite blocking
/*virtual*/ int USBSerialStream::read(uint8_t* buf, size_t* pLength, size_t maxLength, uint32_t timeout/*=osWaitForever*/)
{
  uint8_t *bufPointerCopy = buf;
  DBG("Trying to read at most %d chars", maxLength);
  int ret = waitAvailable(timeout);
  if(ret)
  {
    WARN("Error %d while waiting for incoming data", ret);
    return ret;
  }
  int a = available(); //Prevent macro issues
  int readLen = MIN( a, maxLength );
  *pLength = readLen;
  
  
  setupReadableISR(false);
  while(readLen--)
  {
    m_inBuf.dequeue(buf);
    buf++;
  }
  setupReadableISR(true);
  //printBuffer(bufPointerCopy,*pLength,true); // XXX
  
  return OK;
}
Ejemplo n.º 3
0
int NetworkInputStream::checkStatus() {
	if (_open) {
		int res = waitAvailable(2000);
		if (res < 0) {
			close();
		}
		return res;
	}
	return 0;
}
Ejemplo n.º 4
0
bool PHN_Sim::sendATCommand(const char* command, char* respBuffer, uint16_t respBufferLength, long timeout) {
  if (!writeATCommand(command)) {
    // Failure to communicate the command
    //Serial.println("Command got no response!");
    return false;
  }

  // Prepare a buffer for storing the last bytes for checking for OK/ERROR status
  const int statusBufferLen = 9;
  char statusBuffer[statusBufferLen+1];
  memset(statusBuffer, 0, sizeof(statusBuffer));

  // Read the response from the command  
  uint16_t length = 0;
  bool ok = false, error = false;
  while (waitAvailable(Serial1, timeout)) {
    // Shift data into the status buffer
    memmove(statusBuffer, statusBuffer+1, statusBufferLen-1);
    statusBuffer[statusBufferLen-1] = Serial1.read();

    // Shift data into the response buffer if possible
    if (length+1 < respBufferLength) {
      respBuffer[length] = statusBuffer[statusBufferLen-1];
    }
    length++;

    // Check whether an OK was received
    if (!strncmp(statusBuffer+statusBufferLen-6,"\r\nOK\r\n",6)) {
      length -= 6;
      ok = true;
      break;
    }
    // Check whether an ERROR was received
    if (!strncmp(statusBuffer+statusBufferLen-9,"\r\nERROR\r\n",9)) {
      length -= 9;
      error = true;
      break;
    }
  }

  // Handle post-reading response buffer operations
  if (respBufferLength) {
    // Limit the length by buffer size
    length = min(respBufferLength-1, length);
    // Trim starting \r\n from the response
    if (length >= 2 && !strncmp(respBuffer, "\r\n", 2)) {
      length -= 2;
      memmove(respBuffer, respBuffer+2, length);
    }
    // Trim everything before the first space from the response
    uint16_t i;
    for (i = 1; i < length; i++) {
      if (respBuffer[i-1] == ' ') {
        length -= i;
        memmove(respBuffer, respBuffer+i, length);
        break;
      }
    }
    // Trim ending \r\n from the response
    if (length >= 2 && !strncmp(respBuffer+length-2, "\r\n", 2)) {
      length -= 2;
    }
    
    // Delimit end of String with a NULL character
    respBuffer[length] = 0;
  }
  
  // Debug
  if (!error && !ok) {
    //Serial.println("NO RESULTCODE!");
  }
  return ok;
}
Ejemplo n.º 5
0
inline bool PHN_Sim::waitRead() {
  return waitAvailable(Serial1, SIM_ATCOMMAND_TIMEOUT);
}