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 |
|
---|
43 | static chardev_srv_t *chardev_srv_create(chardev_srvs_t *);
|
---|
44 |
|
---|
45 | static 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 | int rc;
|
---|
51 | ipc_callid_t rcallid;
|
---|
52 |
|
---|
53 | if (!async_data_read_receive(&rcallid, &size)) {
|
---|
54 | async_answer_0(callid, EINVAL);
|
---|
55 | return;
|
---|
56 | }
|
---|
57 |
|
---|
58 | buf = malloc(size);
|
---|
59 | if (buf == NULL) {
|
---|
60 | async_answer_0(rcallid, ENOMEM);
|
---|
61 | async_answer_0(callid, ENOMEM);
|
---|
62 | return;
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (srv->srvs->ops->read == NULL) {
|
---|
66 | async_answer_0(rcallid, ENOTSUP);
|
---|
67 | async_answer_0(callid, ENOTSUP);
|
---|
68 | free(buf);
|
---|
69 | return;
|
---|
70 | }
|
---|
71 |
|
---|
72 | rc = srv->srvs->ops->read(srv, buf, size);
|
---|
73 | if (rc < 0) {
|
---|
74 | async_answer_0(rcallid, rc);
|
---|
75 | async_answer_0(callid, rc);
|
---|
76 | free(buf);
|
---|
77 | return;
|
---|
78 | }
|
---|
79 |
|
---|
80 | async_data_read_finalize(rcallid, buf, size);
|
---|
81 |
|
---|
82 | free(buf);
|
---|
83 | async_answer_2(callid, EOK, EOK, rc /* nread */);
|
---|
84 | }
|
---|
85 |
|
---|
86 | static void chardev_write_srv(chardev_srv_t *srv, ipc_callid_t callid,
|
---|
87 | ipc_call_t *call)
|
---|
88 | {
|
---|
89 | void *data;
|
---|
90 | size_t size;
|
---|
91 | int rc;
|
---|
92 |
|
---|
93 | rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
|
---|
94 | if (rc != EOK) {
|
---|
95 | async_answer_0(callid, rc);
|
---|
96 | return;
|
---|
97 | }
|
---|
98 |
|
---|
99 | if (srv->srvs->ops->write == NULL) {
|
---|
100 | async_answer_0(callid, ENOTSUP);
|
---|
101 | return;
|
---|
102 | }
|
---|
103 |
|
---|
104 | rc = srv->srvs->ops->write(srv, data, size);
|
---|
105 | free(data);
|
---|
106 | if (rc < 0)
|
---|
107 | async_answer_0(callid, rc);
|
---|
108 |
|
---|
109 | async_answer_2(callid, EOK, EOK, rc /* nwritten */);
|
---|
110 | }
|
---|
111 |
|
---|
112 | static chardev_srv_t *chardev_srv_create(chardev_srvs_t *srvs)
|
---|
113 | {
|
---|
114 | chardev_srv_t *srv;
|
---|
115 |
|
---|
116 | srv = calloc(1, sizeof(chardev_srv_t));
|
---|
117 | if (srv == NULL)
|
---|
118 | return NULL;
|
---|
119 |
|
---|
120 | srv->srvs = srvs;
|
---|
121 | return srv;
|
---|
122 | }
|
---|
123 |
|
---|
124 | void chardev_srvs_init(chardev_srvs_t *srvs)
|
---|
125 | {
|
---|
126 | srvs->ops = NULL;
|
---|
127 | srvs->sarg = NULL;
|
---|
128 | }
|
---|
129 |
|
---|
130 | int chardev_conn(ipc_callid_t iid, ipc_call_t *icall, chardev_srvs_t *srvs)
|
---|
131 | {
|
---|
132 | chardev_srv_t *srv;
|
---|
133 | int rc;
|
---|
134 |
|
---|
135 | /* Accept the connection */
|
---|
136 | async_answer_0(iid, EOK);
|
---|
137 |
|
---|
138 | srv = chardev_srv_create(srvs);
|
---|
139 | if (srv == NULL)
|
---|
140 | return ENOMEM;
|
---|
141 |
|
---|
142 | if (srvs->ops->open != NULL) {
|
---|
143 | rc = srvs->ops->open(srvs, srv);
|
---|
144 | if (rc != EOK)
|
---|
145 | return rc;
|
---|
146 | }
|
---|
147 |
|
---|
148 | while (true) {
|
---|
149 | ipc_call_t call;
|
---|
150 | ipc_callid_t callid = async_get_call(&call);
|
---|
151 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
152 |
|
---|
153 | if (!method) {
|
---|
154 | /* The other side has hung up */
|
---|
155 | async_answer_0(callid, EOK);
|
---|
156 | break;
|
---|
157 | }
|
---|
158 |
|
---|
159 | switch (method) {
|
---|
160 | case CHARDEV_READ:
|
---|
161 | chardev_read_srv(srv, callid, &call);
|
---|
162 | break;
|
---|
163 | case CHARDEV_WRITE:
|
---|
164 | chardev_write_srv(srv, callid, &call);
|
---|
165 | break;
|
---|
166 | default:
|
---|
167 | async_answer_0(callid, EINVAL);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | if (srvs->ops->close != NULL)
|
---|
172 | rc = srvs->ops->close(srv);
|
---|
173 | else
|
---|
174 | rc = EOK;
|
---|
175 |
|
---|
176 | free(srv);
|
---|
177 |
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /** @}
|
---|
182 | */
|
---|