source: mainline/uspace/lib/c/generic/io/chardev.c@ c657bd7

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c657bd7 was c657bd7, checked in by Jiri Svoboda <jiri@…>, 8 years ago

Less is sometimes more. Need chardev_read to be able to return less bytes than requested if less is available. Otherwise cannot read variable-sized packets except yte-by-byte.

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2011 Jan Vesely
3 * Copyright (c) 2017 Jiri Svoboda
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/** @addtogroup libc
31 * @{
32 */
33/**
34 * @file
35 * @brief Character device client interface
36 */
37
38#include <errno.h>
39#include <mem.h>
40#include <io/chardev.h>
41#include <ipc/chardev.h>
42#include <stddef.h>
43#include <stdlib.h>
44
45/** Open character device.
46 *
47 * @param sess Session with the character device
48 * @param rchardev Place to store pointer to the new character device structure
49 *
50 * @return EOK on success, ENOMEM if out of memory, EIO on I/O error
51 */
52int chardev_open(async_sess_t *sess, chardev_t **rchardev)
53{
54 chardev_t *chardev;
55
56 chardev = calloc(1, sizeof(chardev_t));
57 if (chardev == NULL)
58 return ENOMEM;
59
60 chardev->sess = sess;
61 *rchardev = chardev;
62
63 /* EIO might be used in a future implementation */
64 return EOK;
65}
66
67/** Close character device.
68 *
69 * Frees the character device structure. The underlying session is
70 * not affected.
71 *
72 * @param chardev Character device or @c NULL
73 */
74void chardev_close(chardev_t *chardev)
75{
76 free(chardev);
77}
78
79/** Read from character device.
80 *
81 * Read as much data as is available from character device up to @a size
82 * bytes into @a buf. On success EOK is returned and at least one byte
83 * is read (if no byte is available the function blocks). The number
84 * of bytes read is stored in @a *nread.
85 *
86 * On error a non-zero error code is returned and @a *nread is filled with
87 * the number of bytes that were successfully transferred.
88 *
89 * @param chardev Character device
90 * @param buf Destination buffer
91 * @param size Maximum number of bytes to read
92 * @param nread Place to store actual number of bytes read
93 *
94 * @return EOK on success or non-zero error code
95 */
96int chardev_read(chardev_t *chardev, void *buf, size_t size, size_t *nread)
97{
98 if (size > 4 * sizeof(sysarg_t))
99 return ELIMIT;
100
101 async_exch_t *exch = async_exchange_begin(chardev->sess);
102 sysarg_t message[4] = { 0 };
103 const ssize_t ret = async_req_1_4(exch, CHARDEV_READ, size,
104 &message[0], &message[1], &message[2], &message[3]);
105 async_exchange_end(exch);
106 if (ret > 0 && (size_t)ret <= size)
107 memcpy(buf, message, size);
108
109 if (ret < 0) {
110 *nread = 0;
111 return ret;
112 }
113
114 *nread = ret;
115 return EOK;
116}
117
118/** Write to character device.
119 *
120 * Write @a size bytes from @a data to character device. On success EOK
121 * is returned, all bytes were written and @a *nwritten is set to @a size.
122 *
123 * On error a non-zero error code is returned and @a *nwritten is filled with
124 * the number of bytes that were successfully transferred.
125 *
126 * @param chardev Character device
127 * @param buf Destination buffer
128 * @param size Maximum number of bytes to read
129 * @param nwritten Place to store actual number of bytes written
130 *
131 * @return EOK on success or non-zero error code
132 */
133int chardev_write(chardev_t *chardev, const void *data, size_t size,
134 size_t *nwritten)
135{
136 int ret;
137
138 if (size > 3 * sizeof(sysarg_t))
139 return ELIMIT;
140
141 async_exch_t *exch = async_exchange_begin(chardev->sess);
142 sysarg_t message[3] = { 0 };
143 memcpy(message, data, size);
144 ret = async_req_4_0(exch, CHARDEV_WRITE, size,
145 message[0], message[1], message[2]);
146 async_exchange_end(exch);
147
148 if (ret < 0) {
149 *nwritten = 0;
150 return ret;
151 }
152
153 *nwritten = ret;
154 return EOK;
155}
156
157/** @}
158 */
Note: See TracBrowser for help on using the repository browser.