From 0e3a410addcb5129ca6b8a44da79d7a513eb518a Mon Sep 17 00:00:00 2001 From: Cervenka Dusan Date: Sun, 19 Nov 2023 14:34:21 +0100 Subject: [PATCH] Add hello world example Signed-off-by: Cervenka Dusan --- .gitignore | 1 + examples/config.h | 4 +++ examples/config.py | 15 ++++++++++ examples/hello_world/README.md | 25 +++++++++++++++++ examples/hello_world/c/main_client.c | 29 ++++++++++++++++++++ examples/hello_world/c/main_client.cpp | 31 +++++++++++++++++++++ examples/hello_world/c/main_server.c | 34 +++++++++++++++++++++++ examples/hello_world/c/main_server.cpp | 37 +++++++++++++++++++++++++ examples/hello_world/hello_world.erpc | 4 +++ examples/hello_world/py/main_client.py | 27 ++++++++++++++++++ examples/hello_world/py/main_server.py | 38 ++++++++++++++++++++++++++ examples/{ => idl}/ble/ble_common.erpc | 0 examples/{ => idl}/ble/ble_gap.erpc | 0 examples/{ => idl}/ble/ble_gatt.erpc | 0 examples/{ => idl}/ble/bluetooth.erpc | 0 examples/{ => idl}/smac.erpc | 0 examples/{ => idl}/temp_alarm.erpc | 0 17 files changed, 245 insertions(+) create mode 100644 examples/config.h create mode 100644 examples/config.py create mode 100644 examples/hello_world/README.md create mode 100644 examples/hello_world/c/main_client.c create mode 100644 examples/hello_world/c/main_client.cpp create mode 100644 examples/hello_world/c/main_server.c create mode 100644 examples/hello_world/c/main_server.cpp create mode 100644 examples/hello_world/hello_world.erpc create mode 100644 examples/hello_world/py/main_client.py create mode 100644 examples/hello_world/py/main_server.py rename examples/{ => idl}/ble/ble_common.erpc (100%) rename examples/{ => idl}/ble/ble_gap.erpc (100%) rename examples/{ => idl}/ble/ble_gatt.erpc (100%) rename examples/{ => idl}/ble/bluetooth.erpc (100%) rename examples/{ => idl}/smac.erpc (100%) rename examples/{ => idl}/temp_alarm.erpc (100%) diff --git a/.gitignore b/.gitignore index 8e4ecc71..837cce87 100644 --- a/.gitignore +++ b/.gitignore @@ -102,6 +102,7 @@ mdk lpcx mcux tags +shim format-files-to-commit.rb language\.settings\.xml diff --git a/examples/config.h b/examples/config.h new file mode 100644 index 00000000..2b86e6d7 --- /dev/null +++ b/examples/config.h @@ -0,0 +1,4 @@ +#pragma once + +#define ERPC_HOSTNAME "localhost" +#define ERPC_PORT 8811 diff --git a/examples/config.py b/examples/config.py new file mode 100644 index 00000000..fbef6a9c --- /dev/null +++ b/examples/config.py @@ -0,0 +1,15 @@ +#!/bin/python3 + +def loadConfig(path: str = "config.h"): + ''' Load config macros from C header file''' + retVal = {} + + with open(path, "r") as file: + configContext = file.read().split("#define ")[1:] + for c in configContext: + cfg = c.split(" ") + if len(cfg) > 1: + val = cfg[1][:-1].replace("\"","") + retVal[cfg[0]] = val + + return retVal diff --git a/examples/hello_world/README.md b/examples/hello_world/README.md new file mode 100644 index 00000000..0dda7c96 --- /dev/null +++ b/examples/hello_world/README.md @@ -0,0 +1,25 @@ +# Hello world example + +This example shows basic usage of eRPC framework. + +## Prerequisites + +Run these commands in this folder based on needs with correct path to erpcgen application + +C/C++ shim code: + +```bash +erpcgen -gc -o shim/py hello_world.erpc +``` + +Python shim code: + +```bash +erpcgen -gpy -o shim/py hello_world.erpc +``` + +In case of C/C++ build application + +## Run example + +First run server, then client. diff --git a/examples/hello_world/c/main_client.c b/examples/hello_world/c/main_client.c new file mode 100644 index 00000000..dbb54f1b --- /dev/null +++ b/examples/hello_world/c/main_client.c @@ -0,0 +1,29 @@ +#include "erpc_c/setup/erpc_client_setup.h" +#include "erpc_c/setup/erpc_transport_setup.h" +#include "erpc_c/setup/erpc_mbf_setup.h" +#include "examples/hello_world/shim/c/c_hello_world_client.h" +#include "examples/config.h" + +int main() +{ + erpc_transport_t transport; + erpc_mbf_t message_buffer_factory; + erpc_client_t client_manager; + + /* Init eRPC client infrastructure */ + transport = erpc_transport_tcp_init(ERPC_HOSTNAME, ERPC_PORT, false); + message_buffer_factory = erpc_mbf_dynamic_init(); + client_manager = erpc_client_init(transport, message_buffer_factory); + + /* init eRPC client TextService service */ + initTextService_client(client_manager); + + /* do eRPC call */ + printText("Hello world!"); + + /* deinit objects */ + deinitTextService_client(); + erpc_client_deinit(client_manager); + erpc_mbf_dynamic_deinit(message_buffer_factory); + erpc_transport_tcp_deinit(transport); +} diff --git a/examples/hello_world/c/main_client.cpp b/examples/hello_world/c/main_client.cpp new file mode 100644 index 00000000..e461742f --- /dev/null +++ b/examples/hello_world/c/main_client.cpp @@ -0,0 +1,31 @@ +#include "erpc_c/setup/erpc_client_setup.h" +#include "erpc_c/setup/erpc_transport_setup.h" +#include "erpc_c/setup/erpc_mbf_setup.h" +#include "examples/hello_world/shim/c/hello_world_client.hpp" +#include "examples/config.h" + +int main() +{ + erpc_transport_t transport; + erpc_mbf_t message_buffer_factory; + erpc_client_t client_manager; + + /* Init eRPC client infrastructure */ + transport = erpc_transport_tcp_init(ERPC_HOSTNAME, ERPC_PORT, false); + message_buffer_factory = erpc_mbf_dynamic_init(); + client_manager = erpc_client_init(transport, message_buffer_factory); + + /* scope for client service */ + { + /* init eRPC client TextService service */ + TextService_client client(client_manager); + + /* do eRPC call */ + client.printText("Hello world!"); + } + + /* deinit objects */ + erpc_client_deinit(client_manager); + erpc_mbf_dynamic_deinit(message_buffer_factory); + erpc_transport_tcp_deinit(transport); +} diff --git a/examples/hello_world/c/main_server.c b/examples/hello_world/c/main_server.c new file mode 100644 index 00000000..04421593 --- /dev/null +++ b/examples/hello_world/c/main_server.c @@ -0,0 +1,34 @@ +#include "erpc_c/setup/erpc_server_setup.h" +#include "erpc_c/setup/erpc_transport_setup.h" +#include "erpc_c/setup/erpc_mbf_setup.h" +#include "examples/hello_world/shim/c/c_hello_world_server.h" +#include "examples/config.h" +#include + +/* eRPC call definition */ +void printText(const char *text) { printf("%s", text); } + +int main() +{ + erpc_transport_t transport; + erpc_mbf_t message_buffer_factory; + erpc_server_t server; + erpc_service_t service = create_TextService_service(); + + /* Init eRPC server infrastructure */ + transport = erpc_transport_tcp_init(ERPC_HOSTNAME, ERPC_PORT, true); + message_buffer_factory = erpc_mbf_dynamic_init(); + server = erpc_server_init(transport, message_buffer_factory); + + /* add custom service implementation to the server */ + erpc_add_service_to_server(server, service); + + /* poll for requests */ + erpc_status_t err = server.poll(); + + /* deinit objects */ + destroy_TextService_service(service); + erpc_server_deinit(server); + erpc_mbf_dynamic_deinit(message_buffer_factory); + erpc_transport_tcp_deinit(transport); +} diff --git a/examples/hello_world/c/main_server.cpp b/examples/hello_world/c/main_server.cpp new file mode 100644 index 00000000..98d7a19d --- /dev/null +++ b/examples/hello_world/c/main_server.cpp @@ -0,0 +1,37 @@ +#include "erpc_c/setup/erpc_server_setup.h" +#include "erpc_c/setup/erpc_transport_setup.h" +#include "erpc_c/setup/erpc_mbf_setup.h" +#include "examples/hello_world/shim/c/hello_world_server.hpp" +#include "examples/config.h" +#include + +class TextService : public TextService_interface +{ + /* eRPC call definition */ + void printText(const char *text) override { printf("%s", text); } +} + +int main() +{ + erpc_transport_t transport; + erpc_mbf_t message_buffer_factory; + erpc_server_t server; + TextService textServiceImpl; + TextService_service textService(&textServiceImpl); + + /* Init eRPC server infrastructure */ + transport = erpc_transport_tcp_init(ERPC_HOSTNAME, ERPC_PORT, true); + message_buffer_factory = erpc_mbf_dynamic_init(); + server = erpc_server_init(transport, message_buffer_factory); + + /* add custom service implementation to the server */ + erpc_add_service_to_server(server, &textService); + + /* poll for requests */ + erpc_status_t err = server.poll(); + + /* deinit objects */ + erpc_server_deinit(server); + erpc_mbf_dynamic_deinit(message_buffer_factory); + erpc_transport_tcp_deinit(transport); +} diff --git a/examples/hello_world/hello_world.erpc b/examples/hello_world/hello_world.erpc new file mode 100644 index 00000000..7adc70a3 --- /dev/null +++ b/examples/hello_world/hello_world.erpc @@ -0,0 +1,4 @@ +interface TextService +{ + oneway printText(string text) +} diff --git a/examples/hello_world/py/main_client.py b/examples/hello_world/py/main_client.py new file mode 100644 index 00000000..d42e723c --- /dev/null +++ b/examples/hello_world/py/main_client.py @@ -0,0 +1,27 @@ +#!/bin/python3 +import sys + +sys.path.insert(1, '../../../erpc_python') # nopep8 +sys.path.insert(2, '../../') # nopep8 +sys.path.insert(3, '../') # nopep8 + +import erpc +import config +from shim.py import hello_world + + +def main(): + # load configuration macros from C header file + cfg = config.loadConfig("../../config.h") + + # init eRPC client infrastructure + transport = erpc.transport.TCPTransport(cfg["ERPC_HOSTNAME"], int(cfg["ERPC_PORT"]), False) + clientManager = erpc.client.ClientManager(transport, erpc.basic_codec.BasicCodec) + + # init eRPC client + client = hello_world.client.TextServiceClient(clientManager) + + # do eRPC call + client.printText("Hello world!") + +main() diff --git a/examples/hello_world/py/main_server.py b/examples/hello_world/py/main_server.py new file mode 100644 index 00000000..cec8ea8b --- /dev/null +++ b/examples/hello_world/py/main_server.py @@ -0,0 +1,38 @@ +#!/bin/python3 +import sys + +sys.path.insert(1, '../../../erpc_python') # nopep8 +sys.path.insert(2, '../../') # nopep8 +sys.path.insert(3, '../') # nopep8 + +import erpc +import config +from shim.py import hello_world + +class MatrixMultiplyServiceHandler(hello_world.interface.ITextService): + def printText(self, text): + '''eRPC call definition''' + + print(text) + + +def main(): + # load configuration macros from C header file + cfg = config.loadConfig("../../config.h") + + # init service + handler = MatrixMultiplyServiceHandler() + service = hello_world.server.TextServiceService(handler) + + # init eRPC server infrastructure + transport = erpc.transport.TCPTransport(cfg["ERPC_HOSTNAME"], int(cfg["ERPC_PORT"]), True) + server = erpc.simple_server.SimpleServer(transport, erpc.basic_codec.BasicCodec) + server.add_service(service) + + # run server + try: + server.run() + except: + pass + +main() diff --git a/examples/ble/ble_common.erpc b/examples/idl/ble/ble_common.erpc similarity index 100% rename from examples/ble/ble_common.erpc rename to examples/idl/ble/ble_common.erpc diff --git a/examples/ble/ble_gap.erpc b/examples/idl/ble/ble_gap.erpc similarity index 100% rename from examples/ble/ble_gap.erpc rename to examples/idl/ble/ble_gap.erpc diff --git a/examples/ble/ble_gatt.erpc b/examples/idl/ble/ble_gatt.erpc similarity index 100% rename from examples/ble/ble_gatt.erpc rename to examples/idl/ble/ble_gatt.erpc diff --git a/examples/ble/bluetooth.erpc b/examples/idl/ble/bluetooth.erpc similarity index 100% rename from examples/ble/bluetooth.erpc rename to examples/idl/ble/bluetooth.erpc diff --git a/examples/smac.erpc b/examples/idl/smac.erpc similarity index 100% rename from examples/smac.erpc rename to examples/idl/smac.erpc diff --git a/examples/temp_alarm.erpc b/examples/idl/temp_alarm.erpc similarity index 100% rename from examples/temp_alarm.erpc rename to examples/idl/temp_alarm.erpc