incubator-nuttx/mm/iob/iob_copyout.c

114 lines
3.3 KiB
C
Raw Normal View History

2014-06-04 02:41:34 +08:00
/****************************************************************************
* mm/iob/iob_copyout.c
2014-06-04 02:41:34 +08:00
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
2014-06-04 02:41:34 +08:00
*
* http://www.apache.org/licenses/LICENSE-2.0
2014-06-04 02:41:34 +08:00
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
2014-06-04 02:41:34 +08:00
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <sys/param.h>
2014-06-04 02:41:34 +08:00
#include <string.h>
#include <assert.h>
#include <nuttx/mm/iob.h>
2014-06-04 02:41:34 +08:00
#include "iob.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: iob_copyout
*
* Description:
* Copy data 'len' bytes of data into the user buffer starting at 'offset'
* in the I/O buffer, returning that actual number of bytes copied out.
2014-06-04 02:41:34 +08:00
*
****************************************************************************/
int iob_copyout(FAR uint8_t *dest, FAR const struct iob_s *iob,
unsigned int len, int offset)
2014-06-04 02:41:34 +08:00
{
FAR const uint8_t *src;
unsigned int ncopy;
unsigned int avail;
unsigned int remaining;
2014-06-04 02:41:34 +08:00
/* The offset must applied to data that is in the I/O buffer chain */
if ((int)(offset + iob->io_offset) < 0)
{
ioberr("ERROR: offset is before the start of data: %d < %d\n",
offset, -(int)iob->io_offset);
return -ESPIPE;
}
2014-06-04 02:41:34 +08:00
/* Skip to the I/O buffer containing the offset */
while ((int)(offset - iob->io_len) >= 0)
2014-06-04 02:41:34 +08:00
{
offset -= iob->io_len;
iob = iob->io_flink;
if (iob == NULL)
{
/* We have no requested data in iob chain */
return 0;
}
2014-06-04 02:41:34 +08:00
}
/* Then loop until all of the I/O data is copied to the user buffer */
remaining = len;
while (iob && remaining > 0)
2014-06-04 02:41:34 +08:00
{
/* Get the source I/O buffer offset address and the amount of data
* available from that address.
*/
2014-06-04 23:03:11 +08:00
src = &iob->io_data[iob->io_offset + offset];
2014-06-04 02:41:34 +08:00
avail = iob->io_len - offset;
2014-06-04 04:28:16 +08:00
/* Copy the from the I/O buffer in to the user buffer */
2014-06-04 02:41:34 +08:00
ncopy = MIN(avail, remaining);
2014-06-04 02:41:34 +08:00
memcpy(dest, src, ncopy);
/* Adjust the total length of the copy and the destination address in
* the user buffer.
*/
remaining -= ncopy;
2014-06-04 02:41:34 +08:00
dest += ncopy;
/* Skip to the next I/O buffer in the chain */
iob = iob->io_flink;
2014-06-04 02:41:34 +08:00
offset = 0;
}
return len - remaining;
2014-06-04 02:41:34 +08:00
}