Skip to main content
added 139 characters in body
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

Majenko's answer explains things and shows how to fix your code, but the solution with static buffer inside the function is not common.

Common is to have int readBuffer(unit8_t* buffer, size_t size).

The parameters are the buffer provided by the caller and the size of that buffer. The return value is the count of bytes written by the function into the buffer.

Real example:

int WiFiEspAtBuffStream::read(uint8_t* data, size_t size) {
  if (size == 0 || !available())
    return 0;

  size_t l = rxBufferLength - rxBufferIndex;
  if (l == 0 && size > rxBufferSize) // internal buffer is empty and provided buffer is large
    return EspAtDrv.recvData(linkId, data, size); // fill the large provided buffer directly

  // copy from internal buffer
  fillRXbuffer();
  for (size_t i = 0; i < l && i < size; i++) {
    data[i] = rxBuffer[rxBufferIndex++];
  }
  if (size <= l) // provided buffer was filled
    return size;
  return l + read(data + l, size - l); // handle the rest of provided buffer
}

Common is to have int readBuffer(unit8_t* buffer, size_t size).

The parameters are the buffer provided by the caller and the size of that buffer. The return value is the count of bytes written by the function into the buffer.

Real example:

int WiFiEspAtBuffStream::read(uint8_t* data, size_t size) {
  if (size == 0 || !available())
    return 0;

  size_t l = rxBufferLength - rxBufferIndex;
  if (l == 0 && size > rxBufferSize) // internal buffer is empty and provided buffer is large
    return EspAtDrv.recvData(linkId, data, size); // fill the large provided buffer directly

  // copy from internal buffer
  fillRXbuffer();
  for (size_t i = 0; i < l && i < size; i++) {
    data[i] = rxBuffer[rxBufferIndex++];
  }
  if (size <= l) // provided buffer was filled
    return size;
  return l + read(data + l, size - l); // handle the rest of provided buffer
}

Majenko's answer explains things and shows how to fix your code, but the solution with static buffer inside the function is not common.

Common is to have int readBuffer(unit8_t* buffer, size_t size).

The parameters are the buffer provided by the caller and the size of that buffer. The return value is the count of bytes written by the function into the buffer.

Real example:

int WiFiEspAtBuffStream::read(uint8_t* data, size_t size) {
  if (size == 0 || !available())
    return 0;

  size_t l = rxBufferLength - rxBufferIndex;
  if (l == 0 && size > rxBufferSize) // internal buffer is empty and provided buffer is large
    return EspAtDrv.recvData(linkId, data, size); // fill the large provided buffer directly

  // copy from internal buffer
  fillRXbuffer();
  for (size_t i = 0; i < l && i < size; i++) {
    data[i] = rxBuffer[rxBufferIndex++];
  }
  if (size <= l) // provided buffer was filled
    return size;
  return l + read(data + l, size - l); // handle the rest of provided buffer
}
deleted 134 characters in body
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

Common is to have int readBuffer(unit8_t* buffer, size_t size).

The parameters are the buffer provided by the caller and the size of that buffer. The return value is the count of bytes written by the function into the buffer.

Note: This applies for function working with byte arrays. For strings represented as character arrays, termination with 0 is used.

Real example:

int WiFiEspAtBuffStream::read(uint8_t* data, size_t size) {
  if (size == 0 || !available())
    return 0;

  size_t l = rxBufferLength - rxBufferIndex;
  if (l == 0 && size > rxBufferSize) // internal buffer is empty and provided buffer is large
    return EspAtDrv.recvData(linkId, data, size); // fill the large provided buffer directly

  // copy from internal buffer
  fillRXbuffer();
  for (size_t i = 0; i < l && i < size; i++) {
    data[i] = rxBuffer[rxBufferIndex++];
  }
  if (size <= l) // provided buffer was filled
    return size;
  return l + read(data + l, size - l); // handle the rest of provided buffer
}

Common is to have int readBuffer(unit8_t* buffer, size_t size).

The parameters are the buffer provided by the caller and the size of that buffer. The return value is the count of bytes written by the function into the buffer.

Note: This applies for function working with byte arrays. For strings represented as character arrays, termination with 0 is used.

Real example:

int WiFiEspAtBuffStream::read(uint8_t* data, size_t size) {
  if (size == 0 || !available())
    return 0;

  size_t l = rxBufferLength - rxBufferIndex;
  if (l == 0 && size > rxBufferSize) // internal buffer is empty and provided buffer is large
    return EspAtDrv.recvData(linkId, data, size); // fill the large provided buffer directly

  // copy from internal buffer
  fillRXbuffer();
  for (size_t i = 0; i < l && i < size; i++) {
    data[i] = rxBuffer[rxBufferIndex++];
  }
  if (size <= l) // provided buffer was filled
    return size;
  return l + read(data + l, size - l); // handle the rest of provided buffer
}

Common is to have int readBuffer(unit8_t* buffer, size_t size).

The parameters are the buffer provided by the caller and the size of that buffer. The return value is the count of bytes written by the function into the buffer.

Real example:

int WiFiEspAtBuffStream::read(uint8_t* data, size_t size) {
  if (size == 0 || !available())
    return 0;

  size_t l = rxBufferLength - rxBufferIndex;
  if (l == 0 && size > rxBufferSize) // internal buffer is empty and provided buffer is large
    return EspAtDrv.recvData(linkId, data, size); // fill the large provided buffer directly

  // copy from internal buffer
  fillRXbuffer();
  for (size_t i = 0; i < l && i < size; i++) {
    data[i] = rxBuffer[rxBufferIndex++];
  }
  if (size <= l) // provided buffer was filled
    return size;
  return l + read(data + l, size - l); // handle the rest of provided buffer
}
Source Link
Juraj
  • 18.3k
  • 4
  • 32
  • 50

Common is to have int readBuffer(unit8_t* buffer, size_t size).

The parameters are the buffer provided by the caller and the size of that buffer. The return value is the count of bytes written by the function into the buffer.

Note: This applies for function working with byte arrays. For strings represented as character arrays, termination with 0 is used.

Real example:

int WiFiEspAtBuffStream::read(uint8_t* data, size_t size) {
  if (size == 0 || !available())
    return 0;

  size_t l = rxBufferLength - rxBufferIndex;
  if (l == 0 && size > rxBufferSize) // internal buffer is empty and provided buffer is large
    return EspAtDrv.recvData(linkId, data, size); // fill the large provided buffer directly

  // copy from internal buffer
  fillRXbuffer();
  for (size_t i = 0; i < l && i < size; i++) {
    data[i] = rxBuffer[rxBufferIndex++];
  }
  if (size <= l) // provided buffer was filled
    return size;
  return l + read(data + l, size - l); // handle the rest of provided buffer
}