[75751db6] | 1 | /*
|
---|
[cdd6fc9] | 2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
[75751db6] | 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 |
|
---|
[edeee9f] | 29 | /** @addtogroup libdevice
|
---|
[75751db6] | 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 |
|
---|
| 43 | static chardev_srv_t *chardev_srv_create(chardev_srvs_t *);
|
---|
| 44 |
|
---|
[984a9ba] | 45 | static void chardev_read_srv(chardev_srv_t *srv, ipc_call_t *icall)
|
---|
[75751db6] | 46 | {
|
---|
[19ea61d] | 47 | void *buf;
|
---|
| 48 | size_t size;
|
---|
[677cad5] | 49 | size_t nread;
|
---|
[f2d88f3] | 50 | chardev_flags_t flags;
|
---|
[b7fd2a0] | 51 | errno_t rc;
|
---|
[19ea61d] | 52 |
|
---|
[fafb8e5] | 53 | flags = ipc_get_arg1(icall);
|
---|
[f2d88f3] | 54 |
|
---|
[984a9ba] | 55 | ipc_call_t call;
|
---|
| 56 | if (!async_data_read_receive(&call, &size)) {
|
---|
[cdd6fc9] | 57 | async_answer_0(&call, EINVAL);
|
---|
[984a9ba] | 58 | async_answer_0(icall, EINVAL);
|
---|
[19ea61d] | 59 | return;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | buf = malloc(size);
|
---|
| 63 | if (buf == NULL) {
|
---|
[984a9ba] | 64 | async_answer_0(&call, ENOMEM);
|
---|
| 65 | async_answer_0(icall, ENOMEM);
|
---|
[19ea61d] | 66 | return;
|
---|
| 67 | }
|
---|
[75751db6] | 68 |
|
---|
| 69 | if (srv->srvs->ops->read == NULL) {
|
---|
[984a9ba] | 70 | async_answer_0(&call, ENOTSUP);
|
---|
| 71 | async_answer_0(icall, ENOTSUP);
|
---|
[19ea61d] | 72 | free(buf);
|
---|
[75751db6] | 73 | return;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[f2d88f3] | 76 | rc = srv->srvs->ops->read(srv, buf, size, &nread, flags);
|
---|
[677cad5] | 77 | if (rc != EOK && nread == 0) {
|
---|
[984a9ba] | 78 | async_answer_0(&call, rc);
|
---|
| 79 | async_answer_0(icall, rc);
|
---|
[19ea61d] | 80 | free(buf);
|
---|
| 81 | return;
|
---|
[75751db6] | 82 | }
|
---|
[19ea61d] | 83 |
|
---|
[984a9ba] | 84 | async_data_read_finalize(&call, buf, nread);
|
---|
[19ea61d] | 85 |
|
---|
| 86 | free(buf);
|
---|
[984a9ba] | 87 | async_answer_2(icall, EOK, (sysarg_t) rc, nread);
|
---|
[75751db6] | 88 | }
|
---|
| 89 |
|
---|
[984a9ba] | 90 | static void chardev_write_srv(chardev_srv_t *srv, ipc_call_t *icall)
|
---|
[75751db6] | 91 | {
|
---|
[19ea61d] | 92 | void *data;
|
---|
| 93 | size_t size;
|
---|
[677cad5] | 94 | size_t nwr;
|
---|
[b7fd2a0] | 95 | errno_t rc;
|
---|
[75751db6] | 96 |
|
---|
[19ea61d] | 97 | rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
|
---|
| 98 | if (rc != EOK) {
|
---|
[984a9ba] | 99 | async_answer_0(icall, rc);
|
---|
[19ea61d] | 100 | return;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[75751db6] | 103 | if (srv->srvs->ops->write == NULL) {
|
---|
[984a9ba] | 104 | async_answer_0(icall, ENOTSUP);
|
---|
[75751db6] | 105 | return;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[677cad5] | 108 | rc = srv->srvs->ops->write(srv, data, size, &nwr);
|
---|
[19ea61d] | 109 | free(data);
|
---|
[677cad5] | 110 | if (rc != EOK && nwr == 0) {
|
---|
[984a9ba] | 111 | async_answer_0(icall, rc);
|
---|
[677cad5] | 112 | return;
|
---|
| 113 | }
|
---|
[19ea61d] | 114 |
|
---|
[984a9ba] | 115 | async_answer_2(icall, EOK, (sysarg_t) rc, nwr);
|
---|
[75751db6] | 116 | }
|
---|
| 117 |
|
---|
| 118 | static chardev_srv_t *chardev_srv_create(chardev_srvs_t *srvs)
|
---|
| 119 | {
|
---|
| 120 | chardev_srv_t *srv;
|
---|
| 121 |
|
---|
| 122 | srv = calloc(1, sizeof(chardev_srv_t));
|
---|
| 123 | if (srv == NULL)
|
---|
| 124 | return NULL;
|
---|
| 125 |
|
---|
| 126 | srv->srvs = srvs;
|
---|
| 127 | return srv;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | void chardev_srvs_init(chardev_srvs_t *srvs)
|
---|
| 131 | {
|
---|
| 132 | srvs->ops = NULL;
|
---|
| 133 | srvs->sarg = NULL;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[984a9ba] | 136 | errno_t chardev_conn(ipc_call_t *icall, chardev_srvs_t *srvs)
|
---|
[75751db6] | 137 | {
|
---|
| 138 | chardev_srv_t *srv;
|
---|
[b7fd2a0] | 139 | errno_t rc;
|
---|
[75751db6] | 140 |
|
---|
| 141 | /* Accept the connection */
|
---|
[beb83c1] | 142 | async_accept_0(icall);
|
---|
[75751db6] | 143 |
|
---|
| 144 | srv = chardev_srv_create(srvs);
|
---|
| 145 | if (srv == NULL)
|
---|
| 146 | return ENOMEM;
|
---|
| 147 |
|
---|
| 148 | if (srvs->ops->open != NULL) {
|
---|
| 149 | rc = srvs->ops->open(srvs, srv);
|
---|
| 150 | if (rc != EOK)
|
---|
| 151 | return rc;
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | while (true) {
|
---|
| 155 | ipc_call_t call;
|
---|
[984a9ba] | 156 | async_get_call(&call);
|
---|
[fafb8e5] | 157 | sysarg_t method = ipc_get_imethod(&call);
|
---|
[75751db6] | 158 |
|
---|
| 159 | if (!method) {
|
---|
| 160 | /* The other side has hung up */
|
---|
[984a9ba] | 161 | async_answer_0(&call, EOK);
|
---|
[75751db6] | 162 | break;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | switch (method) {
|
---|
| 166 | case CHARDEV_READ:
|
---|
[984a9ba] | 167 | chardev_read_srv(srv, &call);
|
---|
[75751db6] | 168 | break;
|
---|
| 169 | case CHARDEV_WRITE:
|
---|
[984a9ba] | 170 | chardev_write_srv(srv, &call);
|
---|
[75751db6] | 171 | break;
|
---|
| 172 | default:
|
---|
[74017ce] | 173 | if (srv->srvs->ops->def_handler != NULL)
|
---|
[984a9ba] | 174 | srv->srvs->ops->def_handler(srv, &call);
|
---|
[74017ce] | 175 | else
|
---|
[984a9ba] | 176 | async_answer_0(&call, ENOTSUP);
|
---|
[75751db6] | 177 | }
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | if (srvs->ops->close != NULL)
|
---|
| 181 | rc = srvs->ops->close(srv);
|
---|
| 182 | else
|
---|
| 183 | rc = EOK;
|
---|
| 184 |
|
---|
| 185 | free(srv);
|
---|
| 186 |
|
---|
| 187 | return rc;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | /** @}
|
---|
| 191 | */
|
---|