Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/serial/serial.c: adapt to the iovec-based api #14898

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions fs/vfs/fs_uio.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,97 @@ void uio_init(FAR struct uio *uio)
{
memset(uio, 0, sizeof(*uio));
}

/****************************************************************************
* Name: uio_copyfrom
*
* Description:
* Copy data from the linear buffer to uio.
*
****************************************************************************/

void uio_copyfrom(FAR struct uio *uio, size_t offset, FAR const void *buf,
size_t len)
{
FAR const struct iovec *iov = uio->uio_iov;

DEBUGASSERT(uio_resid(uio) >= 0);
DEBUGASSERT(len <= uio_resid(uio));
DEBUGASSERT(offset <= uio_resid(uio) - len);
DEBUGASSERT(SSIZE_MAX - offset >= uio->uio_offset_in_iov);

offset += uio->uio_offset_in_iov;
while (offset > iov->iov_len)
{
offset -= iov->iov_len;
iov++;
}

while (len > 0)
{
size_t blen = len;
if (blen > iov->iov_len - offset)
{
blen = iov->iov_len - offset;
}

memcpy((FAR uint8_t *)iov->iov_base + offset, buf, blen);

len -= blen;
if (len == 0)
{
break;
}

buf = (const uint8_t *)buf + blen;
iov++;
offset = 0;
}
}

/****************************************************************************
* Name: uio_copyto
*
* Description:
* Copy data to the linear buffer from uio.
*
****************************************************************************/

void uio_copyto(FAR struct uio *uio, size_t offset, FAR void *buf,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how to support call uio_copyto/from mulitple time with the same uio and the partial buffer

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'm not sure what you meant. can you rephrase?

size_t len)
{
FAR const struct iovec *iov = uio->uio_iov;

DEBUGASSERT(uio_resid(uio) >= 0);
DEBUGASSERT(len <= uio_resid(uio));
DEBUGASSERT(offset <= uio_resid(uio) - len);
DEBUGASSERT(SSIZE_MAX - offset >= uio->uio_offset_in_iov);

offset += uio->uio_offset_in_iov;
while (offset > iov->iov_len)
{
offset -= iov->iov_len;
iov++;
}

while (len > 0)
{
size_t blen = len;
if (blen > iov->iov_len - offset)
{
blen = iov->iov_len - offset;
}

memcpy(buf, (FAR const uint8_t *)iov->iov_base + offset, blen);

len -= blen;
if (len == 0)
{
break;
}

buf = (uint8_t *)buf + blen;
iov++;
offset = 0;
}
}
22 changes: 22 additions & 0 deletions include/nuttx/fs/uio.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,26 @@ void uio_advance(FAR struct uio *uio, size_t sz);

void uio_init(FAR struct uio *uio);

/****************************************************************************
* Name: uio_copyto
*
* Description:
* Copy data to the linear buffer from uio.
*
****************************************************************************/

void uio_copyfrom(FAR struct uio *uio, size_t offset, FAR const void *buf,
size_t len);

/****************************************************************************
* Name: uio_copyto
*
* Description:
* Copy data to the linear buffer from uio.
*
****************************************************************************/

void uio_copyto(FAR struct uio *uio, size_t offset, FAR void *buf,
size_t len);

#endif /* __INCLUDE_NUTTX_FS_UIO_H */