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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c8afd5a was a46e56b, checked in by Jakub Jermar <jakub@…>, 7 years ago

Prefer handle over ID in naming handle variables

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[75751db6]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>
[8d2dd7f2]41#include <stddef.h>
[75751db6]42
43static chardev_srv_t *chardev_srv_create(chardev_srvs_t *);
44
[a46e56b]45static void chardev_read_srv(chardev_srv_t *srv, cap_call_handle_t chandle,
[75751db6]46 ipc_call_t *call)
47{
[19ea61d]48 void *buf;
49 size_t size;
[677cad5]50 size_t nread;
[b7fd2a0]51 errno_t rc;
[a46e56b]52 cap_call_handle_t rcall_handle;
[19ea61d]53
[a46e56b]54 if (!async_data_read_receive(&rcall_handle, &size)) {
55 async_answer_0(chandle, EINVAL);
[19ea61d]56 return;
57 }
58
59 buf = malloc(size);
60 if (buf == NULL) {
[a46e56b]61 async_answer_0(rcall_handle, ENOMEM);
62 async_answer_0(chandle, ENOMEM);
[19ea61d]63 return;
64 }
[75751db6]65
66 if (srv->srvs->ops->read == NULL) {
[a46e56b]67 async_answer_0(rcall_handle, ENOTSUP);
68 async_answer_0(chandle, ENOTSUP);
[19ea61d]69 free(buf);
[75751db6]70 return;
71 }
72
[677cad5]73 rc = srv->srvs->ops->read(srv, buf, size, &nread);
74 if (rc != EOK && nread == 0) {
[a46e56b]75 async_answer_0(rcall_handle, rc);
76 async_answer_0(chandle, rc);
[19ea61d]77 free(buf);
78 return;
[75751db6]79 }
[19ea61d]80
[a46e56b]81 async_data_read_finalize(rcall_handle, buf, nread);
[19ea61d]82
83 free(buf);
[a46e56b]84 async_answer_2(chandle, EOK, (sysarg_t) rc, nread);
[75751db6]85}
86
[a46e56b]87static void chardev_write_srv(chardev_srv_t *srv, cap_call_handle_t chandle,
[75751db6]88 ipc_call_t *call)
89{
[19ea61d]90 void *data;
91 size_t size;
[677cad5]92 size_t nwr;
[b7fd2a0]93 errno_t rc;
[75751db6]94
[19ea61d]95 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
96 if (rc != EOK) {
[a46e56b]97 async_answer_0(chandle, rc);
[19ea61d]98 return;
99 }
100
[75751db6]101 if (srv->srvs->ops->write == NULL) {
[a46e56b]102 async_answer_0(chandle, ENOTSUP);
[75751db6]103 return;
104 }
105
[677cad5]106 rc = srv->srvs->ops->write(srv, data, size, &nwr);
[19ea61d]107 free(data);
[677cad5]108 if (rc != EOK && nwr == 0) {
[a46e56b]109 async_answer_0(chandle, rc);
[677cad5]110 return;
111 }
[19ea61d]112
[a46e56b]113 async_answer_2(chandle, EOK, (sysarg_t) rc, nwr);
[75751db6]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
[a46e56b]134errno_t chardev_conn(cap_call_handle_t icall_handle, ipc_call_t *icall, chardev_srvs_t *srvs)
[75751db6]135{
136 chardev_srv_t *srv;
[b7fd2a0]137 errno_t rc;
[75751db6]138
139 /* Accept the connection */
[a46e56b]140 async_answer_0(icall_handle, EOK);
[75751db6]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;
[a46e56b]154 cap_call_handle_t chandle = async_get_call(&call);
[75751db6]155 sysarg_t method = IPC_GET_IMETHOD(call);
156
157 if (!method) {
158 /* The other side has hung up */
[a46e56b]159 async_answer_0(chandle, EOK);
[75751db6]160 break;
161 }
162
163 switch (method) {
164 case CHARDEV_READ:
[a46e56b]165 chardev_read_srv(srv, chandle, &call);
[75751db6]166 break;
167 case CHARDEV_WRITE:
[a46e56b]168 chardev_write_srv(srv, chandle, &call);
[75751db6]169 break;
170 default:
[74017ce]171 if (srv->srvs->ops->def_handler != NULL)
[a46e56b]172 srv->srvs->ops->def_handler(srv, chandle, &call);
[74017ce]173 else
[a46e56b]174 async_answer_0(chandle, ENOTSUP);
[75751db6]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.