Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 1 | /* |
Julian Hall | 6e02acf | 2022-02-22 16:25:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2020-2022, 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 "dummy_caller.h" |
| 8 | #include <stdlib.h> |
| 9 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 10 | struct dummy_caller_context { |
| 11 | rpc_status_t rpc_status; |
| 12 | service_status_t service_status; |
| 13 | uint8_t *req_buf; |
| 14 | }; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 15 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 16 | rpc_status_t open_session(void *context, const struct rpc_uuid *service_uuid, uint16_t endpoint_id) |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 17 | { |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 18 | (void)context; |
| 19 | (void)service_uuid; |
| 20 | (void)endpoint_id; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 21 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 22 | return RPC_SUCCESS; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 23 | } |
| 24 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 25 | rpc_status_t find_and_open_session(void *context, const struct rpc_uuid *service_uuid) |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 26 | { |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 27 | (void)context; |
| 28 | (void)service_uuid; |
| 29 | |
| 30 | return RPC_SUCCESS; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 33 | static rpc_status_t close_session(void *context) |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 34 | { |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 35 | struct dummy_caller_context *caller_context = (struct dummy_caller_context *)context; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 36 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 37 | free(caller_context->req_buf); |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 38 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 39 | return RPC_SUCCESS; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 42 | static rpc_status_t create_shared_memory(void *context, size_t size, |
| 43 | struct rpc_caller_shared_memory *shared_memory) |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 44 | { |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 45 | struct dummy_caller_context *caller_context = (struct dummy_caller_context *)context; |
Julian Hall | 6e02acf | 2022-02-22 16:25:03 +0000 | [diff] [blame] | 46 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 47 | if (caller_context->req_buf) |
| 48 | return RPC_ERROR_INVALID_STATE; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 49 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 50 | caller_context->req_buf = calloc(1, size); |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 51 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 52 | shared_memory->id = 0; |
| 53 | shared_memory->buffer = caller_context->req_buf; |
| 54 | shared_memory->size = size; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 55 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 56 | return RPC_SUCCESS; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 57 | } |
| 58 | |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 59 | static rpc_status_t release_shared_memory(void *context, |
| 60 | struct rpc_caller_shared_memory *shared_memory) |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 61 | { |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 62 | struct dummy_caller_context *caller_context = (struct dummy_caller_context *)context; |
| 63 | |
| 64 | if (shared_memory->buffer != caller_context->req_buf) |
| 65 | return RPC_ERROR_INVALID_VALUE; |
| 66 | |
| 67 | if (!caller_context->req_buf) |
| 68 | return RPC_ERROR_INVALID_STATE; |
| 69 | |
| 70 | free(caller_context->req_buf); |
| 71 | caller_context->req_buf = NULL; |
| 72 | |
| 73 | return RPC_ERROR_INTERNAL; |
Julian Hall | f7f8495 | 2020-11-23 17:55:51 +0100 | [diff] [blame] | 74 | } |
Imre Kis | e4367bb | 2023-07-04 13:55:14 +0200 | [diff] [blame] | 75 | |
| 76 | static rpc_status_t call(void *context, uint16_t opcode, |
| 77 | struct rpc_caller_shared_memory *shared_memory, size_t request_length, |
| 78 | size_t *response_length, service_status_t *service_status) |
| 79 | { |
| 80 | struct dummy_caller_context *caller_context = (struct dummy_caller_context *)context; |
| 81 | |
| 82 | (void)opcode; |
| 83 | (void)shared_memory; |
| 84 | (void)request_length; |
| 85 | |
| 86 | *response_length = 0; |
| 87 | *service_status = caller_context->rpc_status; |
| 88 | |
| 89 | return caller_context->rpc_status; |
| 90 | } |
| 91 | |
| 92 | rpc_status_t dummy_caller_init(struct rpc_caller_interface *rpc_caller, rpc_status_t rpc_status, |
| 93 | service_status_t service_status) |
| 94 | { |
| 95 | struct dummy_caller_context *context = NULL; |
| 96 | |
| 97 | if (!rpc_caller || rpc_caller->context) |
| 98 | return RPC_ERROR_INVALID_VALUE; |
| 99 | |
| 100 | context = (struct dummy_caller_context *)calloc(1, sizeof(struct dummy_caller_context)); |
| 101 | if (!context) |
| 102 | return RPC_ERROR_INTERNAL; |
| 103 | |
| 104 | context->rpc_status = rpc_status; |
| 105 | context->service_status = service_status; |
| 106 | context->req_buf = NULL; |
| 107 | |
| 108 | rpc_caller->context = context; |
| 109 | rpc_caller->open_session = open_session; |
| 110 | rpc_caller->find_and_open_session = find_and_open_session; |
| 111 | rpc_caller->close_session = close_session; |
| 112 | rpc_caller->create_shared_memory = create_shared_memory; |
| 113 | rpc_caller->release_shared_memory = release_shared_memory; |
| 114 | rpc_caller->call = call; |
| 115 | |
| 116 | return RPC_SUCCESS; |
| 117 | } |
| 118 | |
| 119 | rpc_status_t dummy_caller_deinit(struct rpc_caller_interface *rpc_caller) |
| 120 | { |
| 121 | if (!rpc_caller || !rpc_caller->context) |
| 122 | return RPC_ERROR_INVALID_VALUE; |
| 123 | |
| 124 | free(rpc_caller->context); |
| 125 | |
| 126 | return RPC_SUCCESS; |
| 127 | } |