[a72f3b8] | 1 | /*
|
---|
| 2 | * Copyright (c) 2024 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 libsystem
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief System control protocol server stub
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <errno.h>
|
---|
| 38 | #include <io/log.h>
|
---|
| 39 | #include <ipc/system.h>
|
---|
| 40 | #include <mem.h>
|
---|
| 41 | #include <stdlib.h>
|
---|
| 42 | #include <stddef.h>
|
---|
| 43 | #include <system_srv.h>
|
---|
| 44 |
|
---|
| 45 | static void system_callback_create_srv(system_srv_t *srv, ipc_call_t *call)
|
---|
| 46 | {
|
---|
| 47 | async_sess_t *sess = async_callback_receive(EXCHANGE_SERIALIZE);
|
---|
| 48 | if (sess == NULL) {
|
---|
| 49 | async_answer_0(call, ENOMEM);
|
---|
| 50 | return;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | srv->client_sess = sess;
|
---|
| 54 | async_answer_0(call, EOK);
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | static void system_shutdown_srv(system_srv_t *srv, ipc_call_t *icall)
|
---|
| 58 | {
|
---|
| 59 | errno_t rc;
|
---|
| 60 |
|
---|
| 61 | if (srv->ops->shutdown == NULL) {
|
---|
| 62 | async_answer_0(icall, ENOTSUP);
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | rc = srv->ops->shutdown(srv->arg);
|
---|
| 67 | async_answer_0(icall, rc);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | void system_conn(ipc_call_t *icall, system_srv_t *srv)
|
---|
| 71 | {
|
---|
| 72 | /* Accept the connection */
|
---|
| 73 | async_accept_0(icall);
|
---|
| 74 |
|
---|
| 75 | while (true) {
|
---|
| 76 | ipc_call_t call;
|
---|
| 77 |
|
---|
| 78 | async_get_call(&call);
|
---|
| 79 | sysarg_t method = ipc_get_imethod(&call);
|
---|
| 80 |
|
---|
| 81 | if (!method) {
|
---|
| 82 | /* The other side has hung up */
|
---|
| 83 | async_answer_0(&call, EOK);
|
---|
| 84 | break;
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | switch (method) {
|
---|
| 88 | case SYSTEM_CALLBACK_CREATE:
|
---|
| 89 | system_callback_create_srv(srv, &call);
|
---|
| 90 | break;
|
---|
| 91 | case SYSTEM_SHUTDOWN:
|
---|
| 92 | system_shutdown_srv(srv, &call);
|
---|
| 93 | break;
|
---|
| 94 | default:
|
---|
| 95 | async_answer_0(&call, ENOTSUP);
|
---|
| 96 | }
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | /* Hang up callback session */
|
---|
| 100 | if (srv->client_sess != NULL) {
|
---|
| 101 | async_hangup(srv->client_sess);
|
---|
| 102 | srv->client_sess = NULL;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /** Initialize system server structure
|
---|
| 107 | *
|
---|
| 108 | * @param srv System server structure to initialize
|
---|
| 109 | */
|
---|
| 110 | void system_srv_initialize(system_srv_t *srv)
|
---|
| 111 | {
|
---|
| 112 | memset(srv, 0, sizeof(*srv));
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /** Send 'shutdown complete' event to client.
|
---|
| 116 | *
|
---|
| 117 | * @param srv System server structure
|
---|
| 118 | */
|
---|
| 119 | void system_srv_shutdown_complete(system_srv_t *srv)
|
---|
| 120 | {
|
---|
| 121 | async_exch_t *exch;
|
---|
| 122 |
|
---|
| 123 | exch = async_exchange_begin(srv->client_sess);
|
---|
| 124 | async_msg_0(exch, SYSTEM_SHUTDOWN_COMPLETE);
|
---|
| 125 | async_exchange_end(exch);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /** Send 'shutdown failed' event to client.
|
---|
| 129 | *
|
---|
| 130 | * @param srv System server structure
|
---|
| 131 | */
|
---|
| 132 | void system_srv_shutdown_failed(system_srv_t *srv)
|
---|
| 133 | {
|
---|
| 134 | async_exch_t *exch;
|
---|
| 135 |
|
---|
| 136 | exch = async_exchange_begin(srv->client_sess);
|
---|
| 137 | async_msg_0(exch, SYSTEM_SHUTDOWN_FAILED);
|
---|
| 138 | async_exchange_end(exch);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /** @}
|
---|
| 142 | */
|
---|