1 /*
2  * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stddef.h>
8 #include <protocols/rpc/common/packed-c/status.h>
9 #include "logging_caller.h"
10 
11 static rpc_call_handle call_begin(void *context, uint8_t **req_buf, size_t req_len);
12 static rpc_status_t call_invoke(void *context, rpc_call_handle handle, uint32_t opcode,
13 			 	rpc_opstatus_t *opstatus, uint8_t **resp_buf, size_t *resp_len);
14 static void call_end(void *context, rpc_call_handle handle);
15 
16 
logging_caller_init(struct logging_caller * this_instance,FILE * log_file)17 void logging_caller_init(
18 	struct logging_caller *this_instance,
19 	FILE *log_file)
20 {
21 	struct rpc_caller *base = &this_instance->rpc_caller;
22 
23 	rpc_caller_init(base, this_instance);
24 	base->call_begin = call_begin;
25 	base->call_invoke = call_invoke;
26 	base->call_end = call_end;
27 
28 	this_instance->attached_caller = NULL;
29 	this_instance->log_file = log_file;
30 	this_instance->call_index = 0;
31 }
32 
logging_caller_deinit(struct logging_caller * this_instance)33 void logging_caller_deinit(
34 	struct logging_caller *this_instance)
35 {
36 	this_instance->attached_caller = NULL;
37 }
38 
logging_caller_attach(struct logging_caller * this_instance,struct rpc_caller * attached_caller)39 struct rpc_caller *logging_caller_attach(
40 	struct logging_caller *this_instance,
41 	struct rpc_caller *attached_caller)
42 {
43 	this_instance->attached_caller = attached_caller;
44 	return &this_instance->rpc_caller;
45 }
46 
call_begin(void * context,uint8_t ** req_buf,size_t req_len)47 static rpc_call_handle call_begin(void *context, uint8_t **req_buf, size_t req_len)
48 {
49 	struct logging_caller *this_instance = (struct logging_caller*)context;
50 	rpc_call_handle handle = NULL;
51 
52 	if (this_instance->attached_caller) {
53 
54 		handle = rpc_caller_begin(this_instance->attached_caller, req_buf, req_len);
55 	}
56 
57 	fprintf(this_instance->log_file, "========================\n");
58 	fprintf(this_instance->log_file, "index: %d\n", this_instance->call_index);
59 	fprintf(this_instance->log_file, "req_len: %ld\n", req_len);
60 
61 	if (!handle) {
62 
63 		fprintf(this_instance->log_file, "ERROR: call_begin failed\n");
64 	}
65 
66 	++this_instance->call_index;
67 
68 	return handle;
69 }
70 
call_invoke(void * context,rpc_call_handle handle,uint32_t opcode,rpc_opstatus_t * opstatus,uint8_t ** resp_buf,size_t * resp_len)71 static rpc_status_t call_invoke(void *context, rpc_call_handle handle, uint32_t opcode,
72 			 	rpc_opstatus_t *opstatus, uint8_t **resp_buf, size_t *resp_len)
73 {
74 	struct logging_caller *this_instance = (struct logging_caller*)context;
75 	rpc_status_t status = TS_RPC_ERROR_INVALID_TRANSACTION;
76 
77 	if (this_instance->attached_caller) {
78 
79 
80 		status = rpc_caller_invoke(this_instance->attached_caller,
81 			handle, opcode, opstatus,
82 			resp_buf, resp_len);
83 	}
84 
85 	fprintf(this_instance->log_file, "opcode: %d\n", opcode);
86 	fprintf(this_instance->log_file, "rpc_status: %d\n", status);
87 
88 	if (status == TS_RPC_CALL_ACCEPTED) {
89 
90 		fprintf(this_instance->log_file, "op_status: %ld\n", *opstatus);
91 		fprintf(this_instance->log_file, "resp_len: %ld\n", *resp_len);
92 	}
93 
94 	fprintf(this_instance->log_file, "------------------------\n");
95 
96 	return status;
97 }
98 
call_end(void * context,rpc_call_handle handle)99 static void call_end(void *context, rpc_call_handle handle)
100 {
101 	struct logging_caller *this_instance = (struct logging_caller*)context;
102 
103 	if (this_instance->attached_caller) {
104 
105 		rpc_caller_end(this_instance->attached_caller, handle);
106 	}
107 }
108