source: mainline/uspace/lib/c/generic/io/chardev_srv.c@ d5c1051

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d5c1051 was d5c1051, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

"Obviously harmless" error handling tweaks.

  • Property mode set to 100644
File size: 4.3 KB
Line 
1/*
2 * Copyright (c) 2014 Jiri Svoboda
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libc
30 * @{
31 */
32/**
33 * @file
34 * @brief Character device server stub
35 */
36#include <errno.h>
37#include <io/chardev_srv.h>
38#include <ipc/chardev.h>
39#include <macros.h>
40#include <stdlib.h>
41#include <stddef.h>
42
43static chardev_srv_t *chardev_srv_create(chardev_srvs_t *);
44
45static void chardev_read_srv(chardev_srv_t *srv, ipc_callid_t callid,
46 ipc_call_t *call)
47{
48 void *buf;
49 size_t size;
50 size_t nread;
51 int rc;
52 ipc_callid_t rcallid;
53
54 if (!async_data_read_receive(&rcallid, &size)) {
55 async_answer_0(callid, EINVAL);
56 return;
57 }
58
59 buf = malloc(size);
60 if (buf == NULL) {
61 async_answer_0(rcallid, ENOMEM);
62 async_answer_0(callid, ENOMEM);
63 return;
64 }
65
66 if (srv->srvs->ops->read == NULL) {
67 async_answer_0(rcallid, ENOTSUP);
68 async_answer_0(callid, ENOTSUP);
69 free(buf);
70 return;
71 }
72
73 rc = srv->srvs->ops->read(srv, buf, size, &nread);
74 if (rc != EOK && nread == 0) {
75 async_answer_0(rcallid, rc);
76 async_answer_0(callid, rc);
77 free(buf);
78 return;
79 }
80
81 async_data_read_finalize(rcallid, buf, nread);
82
83 free(buf);
84 async_answer_2(callid, EOK, (sysarg_t) rc, nread);
85}
86
87static void chardev_write_srv(chardev_srv_t *srv, ipc_callid_t callid,
88 ipc_call_t *call)
89{
90 void *data;
91 size_t size;
92 size_t nwr;
93 int rc;
94
95 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
96 if (rc != EOK) {
97 async_answer_0(callid, rc);
98 return;
99 }
100
101 if (srv->srvs->ops->write == NULL) {
102 async_answer_0(callid, ENOTSUP);
103 return;
104 }
105
106 rc = srv->srvs->ops->write(srv, data, size, &nwr);
107 free(data);
108 if (rc != EOK && nwr == 0) {
109 async_answer_0(callid, rc);
110 return;
111 }
112
113 async_answer_2(callid, EOK, (sysarg_t) rc, nwr);
114}
115
116static chardev_srv_t *chardev_srv_create(chardev_srvs_t *srvs)
117{
118 chardev_srv_t *srv;
119
120 srv = calloc(1, sizeof(chardev_srv_t));
121 if (srv == NULL)
122 return NULL;
123
124 srv->srvs = srvs;
125 return srv;
126}
127
128void chardev_srvs_init(chardev_srvs_t *srvs)
129{
130 srvs->ops = NULL;
131 srvs->sarg = NULL;
132}
133
134int chardev_conn(ipc_callid_t iid, ipc_call_t *icall, chardev_srvs_t *srvs)
135{
136 chardev_srv_t *srv;
137 int rc;
138
139 /* Accept the connection */
140 async_answer_0(iid, EOK);
141
142 srv = chardev_srv_create(srvs);
143 if (srv == NULL)
144 return ENOMEM;
145
146 if (srvs->ops->open != NULL) {
147 rc = srvs->ops->open(srvs, srv);
148 if (rc != EOK)
149 return rc;
150 }
151
152 while (true) {
153 ipc_call_t call;
154 ipc_callid_t callid = async_get_call(&call);
155 sysarg_t method = IPC_GET_IMETHOD(call);
156
157 if (!method) {
158 /* The other side has hung up */
159 async_answer_0(callid, EOK);
160 break;
161 }
162
163 switch (method) {
164 case CHARDEV_READ:
165 chardev_read_srv(srv, callid, &call);
166 break;
167 case CHARDEV_WRITE:
168 chardev_write_srv(srv, callid, &call);
169 break;
170 default:
171 if (srv->srvs->ops->def_handler != NULL)
172 srv->srvs->ops->def_handler(srv, callid, &call);
173 else
174 async_answer_0(callid, ENOTSUP);
175 }
176 }
177
178 if (srvs->ops->close != NULL)
179 rc = srvs->ops->close(srv);
180 else
181 rc = EOK;
182
183 free(srv);
184
185 return rc;
186}
187
188/** @}
189 */
Note: See TracBrowser for help on using the repository browser.