Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hello world example #392

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ mdk
lpcx
mcux
tags
shim

format-files-to-commit.rb
language\.settings\.xml
Expand Down
4 changes: 4 additions & 0 deletions examples/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#define ERPC_HOSTNAME "localhost"
#define ERPC_PORT 8811
15 changes: 15 additions & 0 deletions examples/config.py
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions examples/hello_world/README.md
Original file line number Diff line number Diff line change
@@ -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.
29 changes: 29 additions & 0 deletions examples/hello_world/c/main_client.c
Original file line number Diff line number Diff line change
@@ -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);
}
31 changes: 31 additions & 0 deletions examples/hello_world/c/main_client.cpp
Original file line number Diff line number Diff line change
@@ -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);
}
34 changes: 34 additions & 0 deletions examples/hello_world/c/main_server.c
Original file line number Diff line number Diff line change
@@ -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 <cstdio>

/* 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);
}
37 changes: 37 additions & 0 deletions examples/hello_world/c/main_server.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstdio>

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);
}
4 changes: 4 additions & 0 deletions examples/hello_world/hello_world.erpc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface TextService
{
oneway printText(string text)
}
27 changes: 27 additions & 0 deletions examples/hello_world/py/main_client.py
Original file line number Diff line number Diff line change
@@ -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()
38 changes: 38 additions & 0 deletions examples/hello_world/py/main_server.py
Original file line number Diff line number Diff line change
@@ -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()
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.