[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>
|
---|
| 41 | #include <sys/types.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 | size_t size = IPC_GET_ARG1(*call);
|
---|
| 49 | int rc;
|
---|
| 50 |
|
---|
| 51 | if (srv->srvs->ops->read == NULL) {
|
---|
| 52 | async_answer_0(callid, ENOTSUP);
|
---|
| 53 | return;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | if (size <= 4 * sizeof(sysarg_t)) {
|
---|
| 57 | sysarg_t message[4] = {};
|
---|
| 58 |
|
---|
| 59 | rc = srv->srvs->ops->read(srv, (char *)message, size);
|
---|
| 60 | async_answer_4(callid, rc, message[0], message[1],
|
---|
| 61 | message[2], message[3]);
|
---|
| 62 | } else {
|
---|
| 63 | async_answer_0(callid, ELIMIT);
|
---|
| 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | static void chardev_write_srv(chardev_srv_t *srv, ipc_callid_t callid,
|
---|
| 68 | ipc_call_t *call)
|
---|
| 69 | {
|
---|
| 70 | size_t size = IPC_GET_ARG1(*call);
|
---|
| 71 | int rc;
|
---|
| 72 |
|
---|
| 73 | if (srv->srvs->ops->write == NULL) {
|
---|
| 74 | async_answer_0(callid, ENOTSUP);
|
---|
| 75 | return;
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | if (size <= 3 * sizeof(sysarg_t)) {
|
---|
| 79 | const sysarg_t message[3] = {
|
---|
| 80 | IPC_GET_ARG2(*call),
|
---|
| 81 | IPC_GET_ARG3(*call),
|
---|
| 82 | IPC_GET_ARG4(*call)
|
---|
| 83 | };
|
---|
| 84 |
|
---|
| 85 | rc = srv->srvs->ops->write(srv, (char *)message, size);
|
---|
| 86 | async_answer_0(callid, rc);
|
---|
| 87 | } else {
|
---|
| 88 | async_answer_0(callid, ELIMIT);
|
---|
| 89 | }
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | static chardev_srv_t *chardev_srv_create(chardev_srvs_t *srvs)
|
---|
| 93 | {
|
---|
| 94 | chardev_srv_t *srv;
|
---|
| 95 |
|
---|
| 96 | srv = calloc(1, sizeof(chardev_srv_t));
|
---|
| 97 | if (srv == NULL)
|
---|
| 98 | return NULL;
|
---|
| 99 |
|
---|
| 100 | srv->srvs = srvs;
|
---|
| 101 | return srv;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | void chardev_srvs_init(chardev_srvs_t *srvs)
|
---|
| 105 | {
|
---|
| 106 | srvs->ops = NULL;
|
---|
| 107 | srvs->sarg = NULL;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | int chardev_conn(ipc_callid_t iid, ipc_call_t *icall, chardev_srvs_t *srvs)
|
---|
| 111 | {
|
---|
| 112 | chardev_srv_t *srv;
|
---|
| 113 | int rc;
|
---|
| 114 |
|
---|
| 115 | /* Accept the connection */
|
---|
| 116 | async_answer_0(iid, EOK);
|
---|
| 117 |
|
---|
| 118 | srv = chardev_srv_create(srvs);
|
---|
| 119 | if (srv == NULL)
|
---|
| 120 | return ENOMEM;
|
---|
| 121 |
|
---|
| 122 | if (srvs->ops->open != NULL) {
|
---|
| 123 | rc = srvs->ops->open(srvs, srv);
|
---|
| 124 | if (rc != EOK)
|
---|
| 125 | return rc;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | while (true) {
|
---|
| 129 | ipc_call_t call;
|
---|
| 130 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 131 | sysarg_t method = IPC_GET_IMETHOD(call);
|
---|
| 132 |
|
---|
| 133 | if (!method) {
|
---|
| 134 | /* The other side has hung up */
|
---|
| 135 | async_answer_0(callid, EOK);
|
---|
| 136 | break;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | switch (method) {
|
---|
| 140 | case CHARDEV_READ:
|
---|
| 141 | chardev_read_srv(srv, callid, &call);
|
---|
| 142 | break;
|
---|
| 143 | case CHARDEV_WRITE:
|
---|
| 144 | chardev_write_srv(srv, callid, &call);
|
---|
| 145 | break;
|
---|
| 146 | default:
|
---|
| 147 | async_answer_0(callid, EINVAL);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | if (srvs->ops->close != NULL)
|
---|
| 152 | rc = srvs->ops->close(srv);
|
---|
| 153 | else
|
---|
| 154 | rc = EOK;
|
---|
| 155 |
|
---|
| 156 | free(srv);
|
---|
| 157 |
|
---|
| 158 | return rc;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /** @}
|
---|
| 162 | */
|
---|