Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 1 | /* |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2020-2021, Arm Limited and Contributors. All rights reserved. |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include "service_provider.h" |
| 8 | #include <protocols/rpc/common/packed-c/status.h> |
| 9 | #include <stddef.h> |
| 10 | |
| 11 | static const struct service_handler *find_handler(const struct service_provider *sp, |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame^] | 12 | uint32_t opcode) |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 13 | { |
| 14 | const struct service_handler *handler = NULL; |
| 15 | size_t index = 0; |
| 16 | |
| 17 | if (sp->num_handlers) { |
| 18 | while (index < sp->num_handlers) { |
| 19 | if (service_handler_get_opcode(&sp->handlers[index]) == opcode) { |
| 20 | handler = &sp->handlers[index]; |
| 21 | break; |
| 22 | } |
| 23 | ++index; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | return handler; |
| 28 | } |
| 29 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame^] | 30 | static rpc_status_t receive(struct rpc_interface *rpc_iface, struct call_req *req) |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 31 | { |
| 32 | rpc_status_t rpc_status; |
| 33 | struct service_provider *sp = NULL; |
| 34 | const struct service_handler *handler = NULL; |
| 35 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame^] | 36 | sp = (struct service_provider*)((char*)rpc_iface - offsetof(struct service_provider, iface)); |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 37 | handler = find_handler(sp, call_req_get_opcode(req)); |
| 38 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame^] | 39 | if (handler) { |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 40 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame^] | 41 | rpc_status = service_handler_invoke(handler, rpc_iface->context, req); |
| 42 | } |
| 43 | else if (sp->successor) { |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 44 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame^] | 45 | rpc_status = rpc_interface_receive(sp->successor, req); |
| 46 | } |
| 47 | else { |
| 48 | |
| 49 | rpc_status = TS_RPC_ERROR_INVALID_OPCODE; |
| 50 | } |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 51 | |
| 52 | return rpc_status; |
| 53 | } |
| 54 | |
| 55 | void service_provider_init(struct service_provider *sp, void *context, |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame^] | 56 | const struct service_handler *handlers, |
| 57 | size_t num_handlers) |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 58 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame^] | 59 | sp->iface.receive = receive; |
| 60 | sp->iface.context = context; |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 61 | |
| 62 | sp->handlers = handlers; |
| 63 | sp->num_handlers = num_handlers; |
| 64 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame^] | 65 | sp->successor = NULL; |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 66 | } |