[a72f3b8] | 1 | /*
|
---|
[f35749e] | 2 | * Copyright (c) 2025 Jiri Svoboda
|
---|
[a72f3b8] | 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 |
|
---|
[f35749e] | 57 | static void system_poweroff_srv(system_srv_t *srv, ipc_call_t *icall)
|
---|
[a72f3b8] | 58 | {
|
---|
| 59 | errno_t rc;
|
---|
| 60 |
|
---|
[f35749e] | 61 | if (srv->ops->poweroff == NULL) {
|
---|
[a72f3b8] | 62 | async_answer_0(icall, ENOTSUP);
|
---|
| 63 | return;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
[f35749e] | 66 | rc = srv->ops->poweroff(srv->arg);
|
---|
| 67 | async_answer_0(icall, rc);
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | static void system_restart_srv(system_srv_t *srv, ipc_call_t *icall)
|
---|
| 71 | {
|
---|
| 72 | errno_t rc;
|
---|
| 73 |
|
---|
| 74 | if (srv->ops->restart == NULL) {
|
---|
| 75 | async_answer_0(icall, ENOTSUP);
|
---|
| 76 | return;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | rc = srv->ops->restart(srv->arg);
|
---|
[a72f3b8] | 80 | async_answer_0(icall, rc);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | void system_conn(ipc_call_t *icall, system_srv_t *srv)
|
---|
| 84 | {
|
---|
| 85 | /* Accept the connection */
|
---|
| 86 | async_accept_0(icall);
|
---|
| 87 |
|
---|
| 88 | while (true) {
|
---|
| 89 | ipc_call_t call;
|
---|
| 90 |
|
---|
| 91 | async_get_call(&call);
|
---|
| 92 | sysarg_t method = ipc_get_imethod(&call);
|
---|
| 93 |
|
---|
| 94 | if (!method) {
|
---|
| 95 | /* The other side has hung up */
|
---|
| 96 | async_answer_0(&call, EOK);
|
---|
| 97 | break;
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | switch (method) {
|
---|
| 101 | case SYSTEM_CALLBACK_CREATE:
|
---|
| 102 | system_callback_create_srv(srv, &call);
|
---|
| 103 | break;
|
---|
[f35749e] | 104 | case SYSTEM_POWEROFF:
|
---|
| 105 | system_poweroff_srv(srv, &call);
|
---|
| 106 | break;
|
---|
| 107 | case SYSTEM_RESTART:
|
---|
| 108 | system_restart_srv(srv, &call);
|
---|
[a72f3b8] | 109 | break;
|
---|
| 110 | default:
|
---|
| 111 | async_answer_0(&call, ENOTSUP);
|
---|
| 112 | }
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | /* Hang up callback session */
|
---|
| 116 | if (srv->client_sess != NULL) {
|
---|
| 117 | async_hangup(srv->client_sess);
|
---|
| 118 | srv->client_sess = NULL;
|
---|
| 119 | }
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | /** Initialize system server structure
|
---|
| 123 | *
|
---|
| 124 | * @param srv System server structure to initialize
|
---|
| 125 | */
|
---|
| 126 | void system_srv_initialize(system_srv_t *srv)
|
---|
| 127 | {
|
---|
| 128 | memset(srv, 0, sizeof(*srv));
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /** Send 'shutdown complete' event to client.
|
---|
| 132 | *
|
---|
| 133 | * @param srv System server structure
|
---|
| 134 | */
|
---|
| 135 | void system_srv_shutdown_complete(system_srv_t *srv)
|
---|
| 136 | {
|
---|
| 137 | async_exch_t *exch;
|
---|
| 138 |
|
---|
| 139 | exch = async_exchange_begin(srv->client_sess);
|
---|
| 140 | async_msg_0(exch, SYSTEM_SHUTDOWN_COMPLETE);
|
---|
| 141 | async_exchange_end(exch);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /** Send 'shutdown failed' event to client.
|
---|
| 145 | *
|
---|
| 146 | * @param srv System server structure
|
---|
| 147 | */
|
---|
| 148 | void system_srv_shutdown_failed(system_srv_t *srv)
|
---|
| 149 | {
|
---|
| 150 | async_exch_t *exch;
|
---|
| 151 |
|
---|
| 152 | exch = async_exchange_begin(srv->client_sess);
|
---|
| 153 | async_msg_0(exch, SYSTEM_SHUTDOWN_FAILED);
|
---|
| 154 | async_exchange_end(exch);
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | /** @}
|
---|
| 158 | */
|
---|