Julian Hall | f7f8495 | 2020-11-23 17:55:51 +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 | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include "direct_caller.h" |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 8 | #include <rpc/common/endpoint/rpc_interface.h> |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 9 | #include <protocols/rpc/common/packed-c/status.h> |
| 10 | #include <stdlib.h> |
| 11 | |
| 12 | #define DIRECT_CALLER_DEFAULT_REQ_BUF_SIZE (4096) |
| 13 | #define DIRECT_CALLER_DEFAULT_RESP_BUF_SIZE (4096) |
| 14 | |
| 15 | static rpc_call_handle call_begin(void *context, uint8_t **req_buf, size_t req_len); |
| 16 | static rpc_status_t call_invoke(void *context, rpc_call_handle handle, uint32_t opcode, |
Julian Hall | d1dfda5 | 2021-11-25 18:46:45 +0100 | [diff] [blame] | 17 | rpc_opstatus_t *opstatus, uint8_t **resp_buf, size_t *resp_len); |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 18 | static void call_end(void *context, rpc_call_handle handle); |
| 19 | |
| 20 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 21 | struct rpc_caller *direct_caller_init(struct direct_caller *s, struct rpc_interface *iface, |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 22 | size_t req_buf_size, size_t resp_buf_size) |
| 23 | { |
| 24 | struct rpc_caller *base = &s->rpc_caller; |
| 25 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 26 | rpc_caller_init(base, s); |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 27 | base->call_begin = call_begin; |
| 28 | base->call_invoke = call_invoke; |
| 29 | base->call_end = call_end; |
| 30 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 31 | s->rpc_interface = iface; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 32 | s->caller_id = 0; |
| 33 | s->is_call_transaction_in_progess = false; |
| 34 | s->req_len = 0; |
| 35 | s->req_buf_size = req_buf_size; |
| 36 | s->resp_buf_size = resp_buf_size; |
| 37 | s->req_buf = malloc(s->req_buf_size); |
| 38 | s->resp_buf = malloc(s->resp_buf_size); |
| 39 | |
| 40 | if (!s->req_buf || !s->resp_buf) { |
| 41 | |
| 42 | /* Buffer allocation failed */ |
| 43 | base = NULL; |
| 44 | direct_caller_deinit(s); |
| 45 | } |
| 46 | |
| 47 | return base; |
| 48 | } |
| 49 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 50 | struct rpc_caller *direct_caller_init_default(struct direct_caller *s, struct rpc_interface *iface) |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 51 | { |
| 52 | /* Initialise with default buffer sizes */ |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 53 | return direct_caller_init(s, iface, |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 54 | DIRECT_CALLER_DEFAULT_REQ_BUF_SIZE, |
| 55 | DIRECT_CALLER_DEFAULT_RESP_BUF_SIZE); |
| 56 | } |
| 57 | |
| 58 | void direct_caller_deinit(struct direct_caller *s) |
| 59 | { |
| 60 | free(s->req_buf); |
| 61 | s->req_buf = NULL; |
| 62 | free(s->resp_buf); |
| 63 | s->resp_buf = NULL; |
| 64 | } |
| 65 | |
| 66 | static rpc_call_handle call_begin(void *context, uint8_t **req_buf, size_t req_len) |
| 67 | { |
| 68 | struct direct_caller *this_context = (struct direct_caller*)context; |
| 69 | rpc_call_handle handle = NULL; |
| 70 | |
| 71 | if (!this_context->is_call_transaction_in_progess && |
| 72 | (req_len <= this_context->req_buf_size)) { |
| 73 | |
| 74 | this_context->is_call_transaction_in_progess = true; |
| 75 | |
| 76 | if (req_buf){ |
| 77 | *req_buf = this_context->req_buf; |
| 78 | this_context->req_len = req_len; |
| 79 | } |
| 80 | |
| 81 | handle = this_context; |
| 82 | } |
| 83 | |
| 84 | return handle; |
| 85 | } |
| 86 | |
| 87 | static rpc_status_t call_invoke(void *context, rpc_call_handle handle, uint32_t opcode, |
Julian Hall | d1dfda5 | 2021-11-25 18:46:45 +0100 | [diff] [blame] | 88 | rpc_opstatus_t *opstatus, uint8_t **resp_buf, size_t *resp_len) |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 89 | { |
| 90 | struct direct_caller *this_context = (struct direct_caller*)context; |
| 91 | rpc_status_t status = TS_RPC_ERROR_INVALID_TRANSACTION; |
| 92 | |
| 93 | if ((handle == this_context) && this_context->is_call_transaction_in_progess) { |
| 94 | |
| 95 | struct call_req req; |
| 96 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 97 | req.interface_id = 0; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 98 | req.opcode = opcode; |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 99 | req.encoding = this_context->rpc_caller.encoding; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 100 | req.caller_id = this_context->caller_id; |
| 101 | req.opstatus = 0; |
| 102 | req.req_buf = call_param_buf_init_full(this_context->req_buf, |
| 103 | this_context->req_buf_size, this_context->req_len); |
| 104 | req.resp_buf = call_param_buf_init_empty(this_context->resp_buf, |
| 105 | this_context->resp_buf_size); |
| 106 | |
julhal01 | c3f4e9a | 2020-12-15 13:39:01 +0000 | [diff] [blame] | 107 | status = rpc_interface_receive(this_context->rpc_interface, &req); |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 108 | |
| 109 | *resp_buf = this_context->resp_buf; |
| 110 | *resp_len = call_req_get_resp_buf(&req)->data_len; |
| 111 | *opstatus = call_req_get_opstatus(&req); |
| 112 | } |
| 113 | |
| 114 | return status; |
| 115 | } |
| 116 | |
| 117 | static void call_end(void *context, rpc_call_handle handle) |
| 118 | { |
| 119 | struct direct_caller *this_context = (struct direct_caller*)context; |
| 120 | |
| 121 | if ((handle == this_context) && this_context->is_call_transaction_in_progess) { |
| 122 | |
| 123 | this_context->req_len = 0; |
| 124 | this_context->is_call_transaction_in_progess = false; |
| 125 | } |
| 126 | } |