-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathmain_client.cpp
155 lines (122 loc) · 3.76 KB
/
main_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Copyright 2024 NXP
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_matrix_multiply_client.hpp"
#include "erpc_matrix_multiply_interface.hpp"
#include "erpc_basic_codec.hpp"
#include "erpc_client_manager.h"
#include "erpc_tcp_transport.hpp"
#include "erpc_error_handler.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace erpcShim;
using namespace erpc;
////////////////////////////////////////////////////////////////////////////////
// Definitions
////////////////////////////////////////////////////////////////////////////////
#define MATRIX_ITEM_MAX_VALUE (50)
////////////////////////////////////////////////////////////////////////////////
// Classes
////////////////////////////////////////////////////////////////////////////////
class MyMessageBufferFactory : public MessageBufferFactory
{
public:
virtual MessageBuffer create()
{
uint8_t *buf = new uint8_t[1024];
return MessageBuffer(buf, 1024);
}
virtual void dispose(MessageBuffer *buf)
{
erpc_assert(buf);
if (*buf)
{
delete[] buf->get();
}
}
};
/*!
* @brief Fill matrices by random values
*/
static void fill_matrices(Matrix matrix1_ptr, Matrix matrix2_ptr)
{
int32_t a, b;
/* Fill both matrices by random values */
for (a = 0; a < matrix_size; ++a)
{
for (b = 0; b < matrix_size; ++b)
{
matrix1_ptr[a][b] = rand() % MATRIX_ITEM_MAX_VALUE;
matrix2_ptr[a][b] = rand() % MATRIX_ITEM_MAX_VALUE;
}
}
}
/*!
* @brief Printing a matrix to the console
*/
static void print_matrix(Matrix matrix_ptr)
{
int32_t a, b;
for (a = 0; a < matrix_size; ++a)
{
for (b = 0; b < matrix_size; ++b)
{
std::cout << std::setw(4) << matrix_ptr[a][b] << " ";
}
std::cout << std::endl;
}
}
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
int main()
{
Matrix matrix1 = {0}, matrix2 = {0}, result_matrix = {0};
erpc_status_t status;
/* Init eRPC client components */
TCPTransport transport(EXAMPLE_TCP_HOST, EXAMPLE_TCP_PORT, false);
MyMessageBufferFactory msgFactory;
BasicCodecFactory basicCodecFactory;
Crc16 crc16;
ClientManager clientManager;
/* Init transport */
transport.setCrc16(&crc16);
status = transport.open();
if (status != kErpcStatus_Success)
{
/* print error description */
erpc_error_handler(status, 0);
return -1;
}
/* Init client manager */
clientManager.setMessageBufferFactory(&msgFactory);
clientManager.setTransport(&transport);
clientManager.setCodecFactory(&basicCodecFactory);
/* Init eRPC client MatrixMultiply service */
MatrixMultiplyService_client client(&clientManager);
std::string choice;
while (true)
{
fill_matrices(matrix1, matrix2);
std::cout << "Matrix #1" << std::endl << "=========" << std::endl;
print_matrix(matrix1);
std::cout << std::endl << "Matrix #2" << std::endl << "=========" << std::endl;
print_matrix(matrix2);
/* Do eRPC call */
std::cout << "eRPC request is sent to the server" << std::endl;
client.erpcMatrixMultiply(matrix1, matrix2, result_matrix);
std::cout << std::endl << "Result matrix" << std::endl << "=========" << std::endl;
print_matrix(result_matrix);
std::cout << "Press Enter to initiate the next matrix multiplication or 'q' to quit: ";
std::getline(std::cin, choice);
if (choice == "q") {
break;
}
}
client.quitServer();
/* Deinit objects */
transport.close();
}