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> |
Imre Kis | 82ba61b | 2023-07-04 13:53:39 +0200 | [diff] [blame] | 10 | #include <string.h> |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 11 | |
| 12 | static const struct service_handler *find_handler(const struct service_provider *sp, |
Imre Kis | 82ba61b | 2023-07-04 13:53:39 +0200 | [diff] [blame] | 13 | uint32_t opcode) |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 14 | { |
| 15 | const struct service_handler *handler = NULL; |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 16 | |
Julian Hall | 65ff905 | 2021-07-27 09:08:32 +0100 | [diff] [blame] | 17 | if ((opcode >= sp->opcode_range_lo) && (opcode <= sp->opcode_range_hi)) { |
Julian Hall | 65ff905 | 2021-07-27 09:08:32 +0100 | [diff] [blame] | 18 | size_t index = 0; |
| 19 | |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 20 | while (index < sp->num_handlers) { |
| 21 | if (service_handler_get_opcode(&sp->handlers[index]) == opcode) { |
| 22 | handler = &sp->handlers[index]; |
| 23 | break; |
| 24 | } |
| 25 | ++index; |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | return handler; |
| 30 | } |
| 31 | |
Julian Hall | 65ff905 | 2021-07-27 09:08:32 +0100 | [diff] [blame] | 32 | static void set_opcode_range(struct service_provider *sp) |
| 33 | { |
| 34 | uint32_t lo = UINT32_MAX; |
| 35 | uint32_t hi = 0; |
| 36 | |
| 37 | /* Determine opcode range so that this service may be skipped |
| 38 | * without having to iterate over all handlers. This reduces |
| 39 | * the time to find a qualifying handler when multiple service |
| 40 | * providers are chained. |
| 41 | */ |
| 42 | for (size_t index = 0; index < sp->num_handlers; index++) { |
Julian Hall | 65ff905 | 2021-07-27 09:08:32 +0100 | [diff] [blame] | 43 | uint32_t opcode = service_handler_get_opcode(&sp->handlers[index]); |
| 44 | |
| 45 | if (opcode < lo) lo = opcode; |
| 46 | if (opcode > hi) hi = opcode; |
| 47 | } |
| 48 | |
| 49 | sp->opcode_range_lo = lo; |
| 50 | sp->opcode_range_hi = hi; |
| 51 | } |
| 52 | |
Imre Kis | 82ba61b | 2023-07-04 13:53:39 +0200 | [diff] [blame] | 53 | static rpc_status_t receive(void *context, struct rpc_request *req) |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 54 | { |
Imre Kis | 82ba61b | 2023-07-04 13:53:39 +0200 | [diff] [blame] | 55 | struct rpc_service_interface *rpc_iface = (struct rpc_service_interface *)context; |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 56 | rpc_status_t rpc_status; |
| 57 | struct service_provider *sp = NULL; |
| 58 | const struct service_handler *handler = NULL; |
| 59 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 60 | sp = (struct service_provider*)((char*)rpc_iface - offsetof(struct service_provider, iface)); |
Imre Kis | 82ba61b | 2023-07-04 13:53:39 +0200 | [diff] [blame] | 61 | handler = find_handler(sp, req->opcode); |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 62 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 63 | if (handler) { |
Imre Kis | 82ba61b | 2023-07-04 13:53:39 +0200 | [diff] [blame] | 64 | rpc_status = service_handler_invoke(handler, rpc_iface->context, req); |
| 65 | } else if (sp->successor) { |
| 66 | rpc_status = rpc_service_receive(sp->successor, req); |
| 67 | } else { |
| 68 | rpc_status = RPC_ERROR_INVALID_VALUE; |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 69 | } |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 70 | |
| 71 | return rpc_status; |
| 72 | } |
| 73 | |
| 74 | void service_provider_init(struct service_provider *sp, void *context, |
Imre Kis | 82ba61b | 2023-07-04 13:53:39 +0200 | [diff] [blame] | 75 | const struct rpc_uuid *service_uuid, |
| 76 | const struct service_handler *handlers, |
| 77 | size_t num_handlers) |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 78 | { |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 79 | sp->iface.receive = receive; |
| 80 | sp->iface.context = context; |
Imre Kis | 82ba61b | 2023-07-04 13:53:39 +0200 | [diff] [blame] | 81 | memcpy(&sp->iface.uuid, service_uuid, sizeof(sp->iface.uuid)); |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 82 | |
| 83 | sp->handlers = handlers; |
| 84 | sp->num_handlers = num_handlers; |
| 85 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 86 | sp->successor = NULL; |
Julian Hall | 65ff905 | 2021-07-27 09:08:32 +0100 | [diff] [blame] | 87 | |
| 88 | set_opcode_range(sp); |
Julian Hall | 2dcd69c | 2020-11-23 18:05:04 +0100 | [diff] [blame] | 89 | } |
Julian Hall | 13e7695 | 2021-07-13 12:17:09 +0100 | [diff] [blame] | 90 | |
| 91 | void service_provider_extend(struct service_provider *context, |
Imre Kis | 82ba61b | 2023-07-04 13:53:39 +0200 | [diff] [blame] | 92 | struct service_provider *sub_provider) |
Julian Hall | 13e7695 | 2021-07-13 12:17:09 +0100 | [diff] [blame] | 93 | { |
| 94 | sub_provider->successor = context->successor; |
| 95 | context->successor = &sub_provider->iface; |
| 96 | } |