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