| 1 | /*
|
|---|
| 2 | * Copyright (c) 2018 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 ipc-test
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | * @brief IPC test service
|
|---|
| 35 | *
|
|---|
| 36 | * If run as an initial task, this can be used to test sharing areas
|
|---|
| 37 | * backed by the ELF backend.
|
|---|
| 38 | */
|
|---|
| 39 |
|
|---|
| 40 | #include <as.h>
|
|---|
| 41 | #include <async.h>
|
|---|
| 42 | #include <errno.h>
|
|---|
| 43 | #include <str_error.h>
|
|---|
| 44 | #include <io/log.h>
|
|---|
| 45 | #include <ipc/ipc_test.h>
|
|---|
| 46 | #include <ipc/services.h>
|
|---|
| 47 | #include <loc.h>
|
|---|
| 48 | #include <mem.h>
|
|---|
| 49 | #include <stdio.h>
|
|---|
| 50 | #include <task.h>
|
|---|
| 51 |
|
|---|
| 52 | #define NAME "ipc-test"
|
|---|
| 53 |
|
|---|
| 54 | static service_id_t svc_id;
|
|---|
| 55 |
|
|---|
| 56 | /** Object in read-only memory area that will be shared.
|
|---|
| 57 | *
|
|---|
| 58 | * If the server is run as an initial task, the area should be backed
|
|---|
| 59 | * by ELF backend.
|
|---|
| 60 | */
|
|---|
| 61 | static const char *ro_data = "Hello, world!";
|
|---|
| 62 |
|
|---|
| 63 | /** Object in read-write memory area that will be shared.
|
|---|
| 64 | *
|
|---|
| 65 | * If the server is run as an initial task, the area should be backed
|
|---|
| 66 | * by ELF backend.
|
|---|
| 67 | */
|
|---|
| 68 | static char rw_data[] = "Hello, world!";
|
|---|
| 69 |
|
|---|
| 70 | static void ipc_test_get_ro_area_size_srv(ipc_call_t *icall)
|
|---|
| 71 | {
|
|---|
| 72 | errno_t rc;
|
|---|
| 73 | as_area_info_t info;
|
|---|
| 74 |
|
|---|
| 75 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ipc_test_get_ro_area_size_srv");
|
|---|
| 76 |
|
|---|
| 77 | rc = as_area_get_info((void *)ro_data, &info);
|
|---|
| 78 | if (rc != EOK) {
|
|---|
| 79 | async_answer_0(icall, EIO);
|
|---|
| 80 | log_msg(LOG_DEFAULT, LVL_ERROR, "as_area_get_info failed");
|
|---|
| 81 | return;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | async_answer_1(icall, EOK, info.size);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | static void ipc_test_get_rw_area_size_srv(ipc_call_t *icall)
|
|---|
| 88 | {
|
|---|
| 89 | errno_t rc;
|
|---|
| 90 | as_area_info_t info;
|
|---|
| 91 |
|
|---|
| 92 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ipc_test_get_rw_area_size_srv");
|
|---|
| 93 |
|
|---|
| 94 | rc = as_area_get_info(rw_data, &info);
|
|---|
| 95 | if (rc != EOK) {
|
|---|
| 96 | async_answer_0(icall, EIO);
|
|---|
| 97 | log_msg(LOG_DEFAULT, LVL_ERROR, "as_area_get_info failed");
|
|---|
| 98 | return;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | async_answer_1(icall, EOK, info.size);
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | static void ipc_test_share_in_ro_srv(ipc_call_t *icall)
|
|---|
| 105 | {
|
|---|
| 106 | ipc_call_t call;
|
|---|
| 107 | errno_t rc;
|
|---|
| 108 | size_t size;
|
|---|
| 109 | as_area_info_t info;
|
|---|
| 110 |
|
|---|
| 111 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ipc_test_share_in_ro_srv");
|
|---|
| 112 | if (!async_share_in_receive(&call, &size)) {
|
|---|
| 113 | async_answer_0(icall, EINVAL);
|
|---|
| 114 | log_msg(LOG_DEFAULT, LVL_ERROR, "share_in_receive failed");
|
|---|
| 115 | return;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | rc = as_area_get_info((void *)ro_data, &info);
|
|---|
| 119 | if (rc != EOK) {
|
|---|
| 120 | async_answer_0(icall, EINVAL);
|
|---|
| 121 | log_msg(LOG_DEFAULT, LVL_ERROR, "as_area_get_info failed");
|
|---|
| 122 | return;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | if (size != info.size) {
|
|---|
| 126 | log_msg(LOG_DEFAULT, LVL_ERROR, "size(%zu) != %zu",
|
|---|
| 127 | size, info.size);
|
|---|
| 128 | async_answer_0(icall, EINVAL);
|
|---|
| 129 | return;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | rc = async_share_in_finalize(&call, (void *) info.start_addr,
|
|---|
| 133 | AS_AREA_READ);
|
|---|
| 134 | if (rc != EOK) {
|
|---|
| 135 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 136 | " - async_share_in_finalize failed");
|
|---|
| 137 | async_answer_0(icall, EINVAL);
|
|---|
| 138 | return;
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | async_answer_0(icall, EOK);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | static void ipc_test_share_in_rw_srv(ipc_call_t *icall)
|
|---|
| 145 | {
|
|---|
| 146 | ipc_call_t call;
|
|---|
| 147 | errno_t rc;
|
|---|
| 148 | size_t size;
|
|---|
| 149 | as_area_info_t info;
|
|---|
| 150 |
|
|---|
| 151 | log_msg(LOG_DEFAULT, LVL_DEBUG, "ipc_test_share_in_ro_srv");
|
|---|
| 152 | if (!async_share_in_receive(&call, &size)) {
|
|---|
| 153 | async_answer_0(icall, EINVAL);
|
|---|
| 154 | log_msg(LOG_DEFAULT, LVL_ERROR, "share_in_receive failed");
|
|---|
| 155 | return;
|
|---|
| 156 | }
|
|---|
| 157 |
|
|---|
| 158 | rc = as_area_get_info(rw_data, &info);
|
|---|
| 159 | if (rc != EOK) {
|
|---|
| 160 | async_answer_0(icall, EINVAL);
|
|---|
| 161 | log_msg(LOG_DEFAULT, LVL_ERROR, "as_area_get_info failed");
|
|---|
| 162 | return;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | if (size != info.size) {
|
|---|
| 166 | log_msg(LOG_DEFAULT, LVL_ERROR, " size(%zu) != %zu",
|
|---|
| 167 | size, info.size);
|
|---|
| 168 | async_answer_0(icall, EINVAL);
|
|---|
| 169 | return;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | rc = async_share_in_finalize(&call, (void *) info.start_addr,
|
|---|
| 173 | AS_AREA_READ | AS_AREA_WRITE);
|
|---|
| 174 | if (rc != EOK) {
|
|---|
| 175 | log_msg(LOG_DEFAULT, LVL_ERROR,
|
|---|
| 176 | "async_share_in_finalize failed");
|
|---|
| 177 | async_answer_0(icall, EINVAL);
|
|---|
| 178 | return;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | async_answer_0(icall, EOK);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | static void ipc_test_connection(ipc_call_t *icall, void *arg)
|
|---|
| 185 | {
|
|---|
| 186 | /* Accept connection */
|
|---|
| 187 | async_accept_0(icall);
|
|---|
| 188 |
|
|---|
| 189 | while (true) {
|
|---|
| 190 | ipc_call_t call;
|
|---|
| 191 | async_get_call(&call);
|
|---|
| 192 |
|
|---|
| 193 | if (!ipc_get_imethod(&call)) {
|
|---|
| 194 | async_answer_0(&call, EOK);
|
|---|
| 195 | break;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | switch (ipc_get_imethod(&call)) {
|
|---|
| 199 | case IPC_TEST_PING:
|
|---|
| 200 | async_answer_0(&call, EOK);
|
|---|
| 201 | break;
|
|---|
| 202 | case IPC_TEST_GET_RO_AREA_SIZE:
|
|---|
| 203 | ipc_test_get_ro_area_size_srv(&call);
|
|---|
| 204 | break;
|
|---|
| 205 | case IPC_TEST_GET_RW_AREA_SIZE:
|
|---|
| 206 | ipc_test_get_rw_area_size_srv(&call);
|
|---|
| 207 | break;
|
|---|
| 208 | case IPC_TEST_SHARE_IN_RO:
|
|---|
| 209 | ipc_test_share_in_ro_srv(&call);
|
|---|
| 210 | break;
|
|---|
| 211 | case IPC_TEST_SHARE_IN_RW:
|
|---|
| 212 | ipc_test_share_in_rw_srv(&call);
|
|---|
| 213 | break;
|
|---|
| 214 | default:
|
|---|
| 215 | async_answer_0(&call, ENOTSUP);
|
|---|
| 216 | break;
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | int main(int argc, char *argv[])
|
|---|
| 222 | {
|
|---|
| 223 | errno_t rc;
|
|---|
| 224 |
|
|---|
| 225 | printf("%s: IPC test service\n", NAME);
|
|---|
| 226 | async_set_fallback_port_handler(ipc_test_connection, NULL);
|
|---|
| 227 |
|
|---|
| 228 | rc = log_init(NAME);
|
|---|
| 229 | if (rc != EOK) {
|
|---|
| 230 | printf(NAME ": Failed to initialize log.\n");
|
|---|
| 231 | return rc;
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | rc = loc_server_register(NAME);
|
|---|
| 235 | if (rc != EOK) {
|
|---|
| 236 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server. (%s)\n",
|
|---|
| 237 | str_error(rc));
|
|---|
| 238 | return rc;
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | rc = loc_service_register(SERVICE_NAME_IPC_TEST, &svc_id);
|
|---|
| 242 | if (rc != EOK) {
|
|---|
| 243 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service. (%s)\n",
|
|---|
| 244 | str_error(rc));
|
|---|
| 245 | return rc;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | printf("%s: Accepting connections\n", NAME);
|
|---|
| 249 | task_retval(0);
|
|---|
| 250 | async_manager();
|
|---|
| 251 |
|
|---|
| 252 | /* Not reached */
|
|---|
| 253 | return 0;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | /**
|
|---|
| 257 | * @}
|
|---|
| 258 | */
|
|---|