From be2cfce3c056beba4250c969df8c715eb1780ce0 Mon Sep 17 00:00:00 2001 From: phuc Date: Tue, 30 Jan 2024 16:12:29 +0700 Subject: [PATCH 1/2] add matrix_multiply example for CPP --- examples/matrix_multiply_cpp/CMakeLists.txt | 45 ++++++++++++++++ examples/matrix_multiply_cpp/src/client.cpp | 53 ++++++++++++++++++ .../src/matrix_multiply.erpc | 24 +++++++++ examples/matrix_multiply_cpp/src/server.cpp | 54 +++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 examples/matrix_multiply_cpp/CMakeLists.txt create mode 100644 examples/matrix_multiply_cpp/src/client.cpp create mode 100644 examples/matrix_multiply_cpp/src/matrix_multiply.erpc create mode 100644 examples/matrix_multiply_cpp/src/server.cpp diff --git a/examples/matrix_multiply_cpp/CMakeLists.txt b/examples/matrix_multiply_cpp/CMakeLists.txt new file mode 100644 index 000000000..a5cba708a --- /dev/null +++ b/examples/matrix_multiply_cpp/CMakeLists.txt @@ -0,0 +1,45 @@ +cmake_minimum_required(VERSION 3.10) +project(HelloWorldERPC) + + +# Generate eRPC code from IDL file +function(add_erpcgen_target target_name) + # Construct file names based on the provided target name + set(OUTPUT_FILES + "${CMAKE_CURRENT_BINARY_DIR}/${target_name}_interface.cpp" + "${CMAKE_CURRENT_BINARY_DIR}/${target_name}_client.cpp" + "${CMAKE_CURRENT_BINARY_DIR}/${target_name}_server.cpp" + ) + + # The custom command + add_custom_command( + OUTPUT ${OUTPUT_FILES} + COMMAND erpcgen -g c ${CMAKE_CURRENT_SOURCE_DIR}/${target_name}.erpc + DEPENDS src/${target_name}.erpc + ) + + add_library(${target_name}_client_lib + ${CMAKE_CURRENT_BINARY_DIR}/${target_name}_client.cpp + ${CMAKE_CURRENT_BINARY_DIR}/${target_name}_interface.cpp) + + add_library(${target_name}_server_lib + ${CMAKE_CURRENT_BINARY_DIR}/${target_name}_server.cpp + ${CMAKE_CURRENT_BINARY_DIR}/${target_name}_interface.cpp) +endfunction() + + +add_erpcgen_target("matrix_multiply") + + +add_compile_definitions(TCP_HOST="localhost" TCP_PORT=8085) + +include_directories("/usr/local/include/erpc" + "${CMAKE_CURRENT_BINARY_DIR}") + +# Server executable +add_executable(server src/server.cpp) +target_link_libraries(server erpc matrix_multiply_server_lib) + +# Client executable +add_executable(client src/client.cpp) +target_link_libraries(client erpc matrix_multiply_client_lib) diff --git a/examples/matrix_multiply_cpp/src/client.cpp b/examples/matrix_multiply_cpp/src/client.cpp new file mode 100644 index 000000000..0be5fa8be --- /dev/null +++ b/examples/matrix_multiply_cpp/src/client.cpp @@ -0,0 +1,53 @@ +#include + +#include "erpc_client_setup.h" +#include "erpc_transport_setup.h" +#include "matrix_multiply_client.hpp" + +Matrix A = {{3, 3, 3, 7, 6}, + {8, 1, 3, 8, 8}, + {4, 6, 3, 4, 7}, + {4, 6, 7, 2, 1}, + {4, 2, 9, 9, 6}}; + +Matrix B = {{4, 1, 7, 1, 4}, + {1, 5, 7, 2, 5}, + {6, 4, 2, 1, 6}, + {5, 9, 5, 8, 7}, + {3, 7, 1, 9, 4}}; + +Matrix C = {}; + +void printMatrix(const Matrix M) { + for (int i = 0; i < matrix_size; i++) { + for (int j = 0; j < matrix_size; j++) { + std::cout << M[i][j] << "\t"; + } + std::cout << std::endl; + } +} + +int main() { + // Initialize client + erpc_transport_t transport = + erpc_transport_tcp_init(TCP_HOST, TCP_PORT, false); + auto client = erpc_client_init(transport, erpc_mbf_dynamic_init()); + + // Create client stub + erpcShim::MatrixMultiplyService_client hello_client{ + reinterpret_cast(client)}; + + for (auto i = 0; i < 10; ++i) { + // Call server function + hello_client.erpcMatrixMultiply(A, B, C); + printMatrix(C); + + auto greet = hello_client.hello("Alice"); + std::cout << "hello result: " << greet << std::endl; + } + + // De-initialize client + erpc_client_deinit(client); + + return 0; +} diff --git a/examples/matrix_multiply_cpp/src/matrix_multiply.erpc b/examples/matrix_multiply_cpp/src/matrix_multiply.erpc new file mode 100644 index 000000000..ab25ddd5b --- /dev/null +++ b/examples/matrix_multiply_cpp/src/matrix_multiply.erpc @@ -0,0 +1,24 @@ +/*! + * Copyright 2017 NXP + * All rights reserved. + * + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +program matrix_multiply + +/*! This const defines the matrix size. The value has to be the same as the + Matrix array dimension. Do not forget to re-generate the erpc code once the + matrix size is changed in the erpc file */ +const int32 matrix_size = 5; + +/*! This is the matrix array type. The dimension has to be the same as the + matrix size const. Do not forget to re-generate the erpc code once the + matrix size is changed in the erpc file */ +type Matrix = int32[matrix_size][matrix_size]; + +interface MatrixMultiplyService { + erpcMatrixMultiply(in Matrix matrix1, in Matrix matrix2, out Matrix result_matrix) -> void + hello(in string name) -> string +} diff --git a/examples/matrix_multiply_cpp/src/server.cpp b/examples/matrix_multiply_cpp/src/server.cpp new file mode 100644 index 000000000..8ef93b317 --- /dev/null +++ b/examples/matrix_multiply_cpp/src/server.cpp @@ -0,0 +1,54 @@ +#include + +#include "erpc_port.h" +#include "erpc_server_setup.h" +#include "matrix_multiply_server.hpp" + +// Implementation of the "MatrixMultiply" service +class MatrixMultiplyServiceInterface + : public erpcShim::MatrixMultiplyService_interface { + public: + void erpcMatrixMultiply(Matrix A, Matrix B, Matrix C) override { + std::cout << "handle erpcMatrixMultiply" << std::endl; + for (int i = 0; i < matrix_size; i++) { + for (int j = 0; j < matrix_size; j++) { + C[i][j] = 0; + for (int k = 0; k < matrix_size; k++) { + C[i][j] += A[i][k] * B[k][j]; + } + } + } + } + + char* hello(const char* name) override { + std::cout << "handle hello" << std::endl; + std::string input_name{name}; + std::string greet = "hello " + input_name + ", from server"; + char* ret = reinterpret_cast(erpc_malloc(greet.length() + 1)); + strcpy(ret, greet.c_str()); + + return ret; + } +}; + +int main() { + // Initialize server + erpc_transport_t transport = + erpc_transport_tcp_init(TCP_HOST, TCP_PORT, true); + auto server = erpc_server_init(transport, erpc_mbf_dynamic_init()); + + // Register service + MatrixMultiplyServiceInterface interface {}; + erpcShim::MatrixMultiplyService_service service{&interface}; + + erpc_add_service_to_server(server, &service); + + // Run server + while (1) { + erpc_server_run(server); + } + + erpc_server_stop(server); + + return 0; +} From d177607f3bf654d84327634e9c2e22c877292a4a Mon Sep 17 00:00:00 2001 From: phuc Date: Tue, 20 Feb 2024 16:43:19 +0700 Subject: [PATCH 2/2] Build for android --- erpc_c/transports/erpc_tcp_transport.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/erpc_c/transports/erpc_tcp_transport.cpp b/erpc_c/transports/erpc_tcp_transport.cpp index 9712ce6c7..fcc1e9fe8 100644 --- a/erpc_c/transports/erpc_tcp_transport.cpp +++ b/erpc_c/transports/erpc_tcp_transport.cpp @@ -32,6 +32,9 @@ extern "C" { #else #include #include +#ifdef ANDROID +#include +#endif #include #endif #include