2014-06-03 20:41:34 +02:00
|
|
|
/****************************************************************************
|
2017-05-09 15:34:59 +02:00
|
|
|
* mm/iob/iob_copyout.c
|
2014-06-03 20:41:34 +02:00
|
|
|
*
|
2024-09-12 16:44:55 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* 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-03 20:41:34 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-06-03 20:41:34 +02:00
|
|
|
*
|
2021-02-08 13:50:10 +01: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-03 20:41:34 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2023-02-01 14:41:12 +01:00
|
|
|
#include <sys/param.h>
|
2014-06-03 20:41:34 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2017-05-09 15:34:59 +02:00
|
|
|
#include <nuttx/mm/iob.h>
|
2014-06-03 20:41:34 +02:00
|
|
|
|
|
|
|
#include "iob.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: iob_copyout
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Copy data 'len' bytes of data into the user buffer starting at 'offset'
|
2014-06-05 18:43:20 +02:00
|
|
|
* in the I/O buffer, returning that actual number of bytes copied out.
|
2014-06-03 20:41:34 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-06-05 18:43:20 +02:00
|
|
|
int iob_copyout(FAR uint8_t *dest, FAR const struct iob_s *iob,
|
2023-01-18 12:58:52 +01:00
|
|
|
unsigned int len, int offset)
|
2014-06-03 20:41:34 +02:00
|
|
|
{
|
|
|
|
FAR const uint8_t *src;
|
|
|
|
unsigned int ncopy;
|
|
|
|
unsigned int avail;
|
2014-06-05 18:43:20 +02:00
|
|
|
unsigned int remaining;
|
2014-06-03 20:41:34 +02:00
|
|
|
|
2023-01-18 12:58:52 +01: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-03 20:41:34 +02:00
|
|
|
/* Skip to the I/O buffer containing the offset */
|
|
|
|
|
2023-01-18 12:58:52 +01:00
|
|
|
while ((int)(offset - iob->io_len) >= 0)
|
2014-06-03 20:41:34 +02:00
|
|
|
{
|
|
|
|
offset -= iob->io_len;
|
2014-06-06 17:35:31 +02:00
|
|
|
iob = iob->io_flink;
|
2015-08-11 15:55:40 +02:00
|
|
|
if (iob == NULL)
|
|
|
|
{
|
|
|
|
/* We have no requested data in iob chain */
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-03 20:41:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Then loop until all of the I/O data is copied to the user buffer */
|
|
|
|
|
2014-06-05 18:43:20 +02:00
|
|
|
remaining = len;
|
|
|
|
while (iob && remaining > 0)
|
2014-06-03 20:41:34 +02:00
|
|
|
{
|
|
|
|
/* Get the source I/O buffer offset address and the amount of data
|
|
|
|
* available from that address.
|
|
|
|
*/
|
|
|
|
|
2014-06-04 17:03:11 +02:00
|
|
|
src = &iob->io_data[iob->io_offset + offset];
|
2014-06-03 20:41:34 +02:00
|
|
|
avail = iob->io_len - offset;
|
|
|
|
|
2014-06-03 22:28:16 +02:00
|
|
|
/* Copy the from the I/O buffer in to the user buffer */
|
2014-06-03 20:41:34 +02:00
|
|
|
|
2014-06-05 18:43:20 +02:00
|
|
|
ncopy = MIN(avail, remaining);
|
2014-06-03 20:41:34 +02:00
|
|
|
memcpy(dest, src, ncopy);
|
|
|
|
|
|
|
|
/* Adjust the total length of the copy and the destination address in
|
|
|
|
* the user buffer.
|
|
|
|
*/
|
|
|
|
|
2014-06-05 18:43:20 +02:00
|
|
|
remaining -= ncopy;
|
2014-06-03 20:41:34 +02:00
|
|
|
dest += ncopy;
|
|
|
|
|
|
|
|
/* Skip to the next I/O buffer in the chain */
|
|
|
|
|
2014-06-06 17:35:31 +02:00
|
|
|
iob = iob->io_flink;
|
2014-06-03 20:41:34 +02:00
|
|
|
offset = 0;
|
|
|
|
}
|
2014-06-05 18:43:20 +02:00
|
|
|
|
|
|
|
return len - remaining;
|
2014-06-03 20:41:34 +02:00
|
|
|
}
|