# HG changeset patch # User Paul Boddie # Date 1587822256 -7200 # Node ID f1c43fe96a6059c6e92b30a0015b22d38fd629ec # Parent 47f0683550fad3fbd0e54d15f56f369a18ff84ae Added examples demonstrating C and C++ clients and servers. diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/Control --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/Control Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,3 @@ +requires: libc libipc +provides: calc calc++ +maintainer: paul@boddie.org.uk diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/Makefile Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,6 @@ +PKGDIR ?= . +L4DIR ?= $(PKGDIR)/../.. + +TARGET = $(wildcard [a-z]*) + +include $(L4DIR)/mk/subdir.mk diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/Control --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/Control Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,3 @@ +requires: libc libipc +provides: calc++ +maintainer: paul@boddie.org.uk diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/Makefile Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,49 @@ +PKGDIR ?= .. +L4DIR ?= $(PKGDIR)/../.. + +TARGET = calc_client++ calc_server++ calc_counter_client++ calc_counter_server++ +MODE = shared + +# Locations for interface input and generated output. + +IDL_DIR = $(PKGDIR)/idl +IDL_MK_DIR = $(L4DIR)/idl4re/mk +IDL_BUILD_DIR = . +IDL_EXPORT_DIR = . + +include $(IDL_MK_DIR)/idl.mk + +calc_counter_NAME = CalcCounter +calc_counter_INTERFACES = calc counter + +COMP_INTERFACES_CC = calc_counter + +# Individual interfaces. + +CLIENT_INTERFACES_CC = calc counter +SERVER_INTERFACES_CC = calc counter + +SRC_CC_calc_client++ = \ + $(call interfaces_to_client_cc,$(CLIENT_INTERFACES_CC)) \ + client.cc \ + calc_local.cc \ + +SRC_CC_calc_server++ = \ + $(call interfaces_to_server_cc,$(SERVER_INTERFACES_CC)) \ + server.cc \ + +SRC_CC_calc_counter_client++ = \ + $(call interfaces_to_client_cc,$(CLIENT_INTERFACES_CC)) \ + client_compound.cc \ + +SRC_CC_calc_counter_server++ = \ + $(call interfaces_to_server_cc,$(SERVER_INTERFACES_CC)) \ + $(call interfaces_to_server_cc,$(COMP_INTERFACES_CC)) \ + server_compound.cc \ + +REQUIRES_LIBS = l4re_c-util libipc + +PRIVATE_INCDIR = $(IDL_BUILD_DIR) $(IDL_EXPORT_DIR) + +include $(L4DIR)/mk/prog.mk +include $(IDL_MK_DIR)/interface_rules.mk diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/calc_local.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/calc_local.cc Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,60 @@ +/* + * Provide a calculation object for local use. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include + +#include + +#include "calc_local.h" + + + +/* Component methods. */ + +long local_Calc::add(int left, int right, int *result) +{ + *result = left + right; + return L4_EOK; +} + +long local_Calc::subtract(int left, int right, int *result) +{ + *result = left - right; + return L4_EOK; +} + +long local_Calc::multiply(int left, int right, int *result) +{ + *result = left * right; + return L4_EOK; +} + +long local_Calc::divide(int numerator, int denominator, int *result) +{ + *result = numerator / denominator; + return L4_EOK; +} + +long local_Calc::pow(double base, double exponent, double *result) +{ + *result = powf(base, exponent); + return L4_EOK; +} diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/calc_local.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/calc_local.h Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,36 @@ +/* + * Provide a calculation object for local use. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#pragma once + +#include "calc_interface.h" + +/* Interface for a local component. */ + +class local_Calc : public Calc +{ +public: + long add(int left, int right, int *result); + long subtract(int left, int right, int *result); + long multiply(int left, int right, int *result); + long divide(int numerator, int denominator, int *result); + long pow(double base, double exponent, double *result); +}; diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/client.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/client.cc Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,65 @@ +/* + * Access a calculation server. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include + +#include + +#include "calc_client.h" +#include "calc_local.h" + +/* A generic function that can invoke local and remote objects. */ + +static void invoke(Calc *obj, int *result) +{ + obj->add(789, 123, result); +} + +/* Main program. */ + +int main(void) +{ + l4_cap_idx_t server = l4re_env_get_cap("server"); + + /* Calculator encapsulation. */ + + client_Calc obj(server); + + /* Another calculator. */ + + local_Calc local_obj; + + /* Call the calculator. */ + + int result; + + /* Invoke the operation via the interface. */ + + invoke(&obj, &result); + printf("add 789, 123 -> %d\n", result); + + /* Invoke an operation on the local object via the interface. */ + + invoke(&local_obj, &result); + printf("add 789, 123 -> %d\n", result); + + return 0; +} diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/client_compound.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/client_compound.cc Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,58 @@ +/* + * Access a server offering a compound interface. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include + +#include + +#include "calc_client.h" +#include "counter_client.h" + +int main(void) +{ + l4_cap_idx_t server = l4re_env_get_cap("server"); + + /* Calculator encapsulation. */ + + client_Calc calc_obj(server); + + /* Counter encapsulation. */ + + client_Counter counter_obj(server); + + /* Call the calculator. */ + + int result; + + /* Invoke the operation via the interface. */ + + calc_obj.add(789, 123, &result); + printf("add 789, 123 -> %d\n", result); + + /* Call the counter. */ + + /* Invoke the operation via the interface. */ + + counter_obj.increment(&result); + printf("increment -> %d\n", result); + + return 0; +} diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/conf/calc++.cfg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/conf/calc++.cfg Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,23 @@ +-- vim:set ft=lua: + +local L4 = require("L4"); + +local l = L4.default_loader; + +local ipc = l:new_channel(); + +l:start({ + caps = { + server = ipc:svr(), + }, + log = { "calc_server++", "y" }, + }, + "rom/calc_server++"); + +l:start({ + caps = { + server = ipc, + }, + log = { "calc_client++", "g" }, + }, + "rom/calc_client++"); diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/conf/calc++.list --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/conf/calc++.list Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,24 @@ +entry calc++ +roottask moe rom/calc++.cfg +module calc++.cfg +module l4re +module ned +module calc_client++ +module calc_server++ +module lib4re-c.so +module lib4re-c-util.so +module lib4re.so +module lib4re-util.so +module libc_be_l4refile.so +module libc_be_l4re.so +module libc_be_socket_noop.so +module libc_support_misc.so +module libdl.so +module libipc.so +module libl4sys-direct.so +module libl4sys.so +module libl4util.so +module libld-l4.so +module libpthread.so +module libsupc++.so +module libuc_c.so diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/conf/calc_counter++.cfg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/conf/calc_counter++.cfg Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,23 @@ +-- vim:set ft=lua: + +local L4 = require("L4"); + +local l = L4.default_loader; + +local ipc = l:new_channel(); + +l:start({ + caps = { + server = ipc:svr(), + }, + log = { "calc_counter_server++", "y" }, + }, + "rom/calc_counter_server++"); + +l:start({ + caps = { + server = ipc, + }, + log = { "calc_counter_client++", "g" }, + }, + "rom/calc_counter_client++"); diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/conf/calc_counter++.list --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/conf/calc_counter++.list Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,24 @@ +entry calc_counter++ +roottask moe rom/calc_counter++.cfg +module calc_counter++.cfg +module l4re +module ned +module calc_counter_client++ +module calc_counter_server++ +module lib4re-c.so +module lib4re-c-util.so +module lib4re.so +module lib4re-util.so +module libc_be_l4refile.so +module libc_be_l4re.so +module libc_be_socket_noop.so +module libc_support_misc.so +module libdl.so +module libipc.so +module libl4sys-direct.so +module libl4sys.so +module libl4util.so +module libld-l4.so +module libpthread.so +module libsupc++.so +module libuc_c.so diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/server.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/server.cc Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,116 @@ +/* + * Provide a calculation server. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include +#include + +#include +#include + +#include + +#include "calc_server.h" + + + +/* Component interface. */ + +class server_Calc : public Calc +{ +public: + long add(int left, int right, int *result) + { + *result = left + right; + return L4_EOK; + } + + long subtract(int left, int right, int *result) + { + *result = left - right; + return L4_EOK; + } + + long multiply(int left, int right, int *result) + { + *result = left * right; + return L4_EOK; + } + + long divide(int numerator, int denominator, int *result) + { + *result = numerator / denominator; + return L4_EOK; + } + + long pow(double base, double exponent, double *result) + { + *result = powf(base, exponent); + return L4_EOK; + } +}; + + + +int main(void) +{ + /* Calculator encapsulation. */ + + server_Calc obj; + + /* Server capability, incoming message and label. */ + + l4_cap_idx_t server; + ipc_message_t msg; + l4_umword_t label; + Calc *target; + + /* Register a server associating it with the given object. */ + + long err = ipc_server_bind("server", (l4_umword_t) &obj, &server); + + if (err) + { + printf("Could not bind server: %s\n", l4sys_errtostr(err)); + return 1; + } + + /* Wait for messages, dispatching to the handler. */ + + while (1) + { + ipc_message_wait(&msg, &label); + + /* Ignore erroneous messages. */ + + if (l4_ipc_error(msg.tag, l4_utcb())) + continue; + + /* Handle messages for different server objects. */ + + target = (Calc *) (label & ~3UL); + + /* Dispatch to the object. */ + + handle_Calc(&msg, target); + } + + return 0; +} diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc++/server_compound.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc++/server_compound.cc Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,127 @@ +/* + * Provide a server offering a compound interface. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include +#include + +#include +#include + +#include + +#include "calc_counter_server.h" + + + +/* Component interface. */ + +class server_CalcCounter : public CalcCounter +{ + /* Component state . */ + + int counter = 0; + +public: + long add(int left, int right, int *result) + { + *result = left + right; + return L4_EOK; + } + + long subtract(int left, int right, int *result) + { + *result = left - right; + return L4_EOK; + } + + long multiply(int left, int right, int *result) + { + *result = left * right; + return L4_EOK; + } + + long divide(int numerator, int denominator, int *result) + { + *result = numerator / denominator; + return L4_EOK; + } + + long pow(double base, double exponent, double *result) + { + *result = powf(base, exponent); + return L4_EOK; + } + + long increment(int *result) + { + counter = counter + 1; + *result = counter; + return L4_EOK; + } +}; + + + +int main(void) +{ + /* An encapsulation. */ + + server_CalcCounter obj; + + /* Server capability, incoming message and label. */ + + l4_cap_idx_t server; + ipc_message_t msg; + l4_umword_t label; + CalcCounter *target; + + /* Register a server associating it with the given object. */ + + long err = ipc_server_bind("server", (l4_umword_t) &obj, &server); + + if (err) + { + printf("Could not bind server: %s\n", l4sys_errtostr(err)); + return 1; + } + + /* Wait for messages, dispatching to the handler. */ + + while (1) + { + ipc_message_wait(&msg, &label); + + /* Ignore erroneous messages. */ + + if (l4_ipc_error(msg.tag, l4_utcb())) + continue; + + /* Handle messages for different server objects. */ + + target = (CalcCounter *) (label & ~3UL); + + /* Dispatch to the object. */ + + handle_CalcCounter(&msg, target); + } + + return 0; +} diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/Control --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/Control Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,3 @@ +requires: libc libipc +provides: calc +maintainer: paul@boddie.org.uk diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/Makefile --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/Makefile Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,49 @@ +PKGDIR ?= .. +L4DIR ?= $(PKGDIR)/../.. + +TARGET = calc_client calc_server calc_counter_client calc_counter_server +MODE = shared + +# Locations for interface input and generated output. + +IDL_DIR = $(PKGDIR)/idl +IDL_MK_DIR = $(L4DIR)/idl4re/mk +IDL_BUILD_DIR = . +IDL_EXPORT_DIR = . + +include $(IDL_MK_DIR)/idl.mk + +calc_counter_NAME = CalcCounter +calc_counter_INTERFACES = calc counter + +COMP_INTERFACES_C = calc_counter + +# Individual interfaces. + +CLIENT_INTERFACES_C = calc counter +SERVER_INTERFACES_C = calc counter + +SRC_C_calc_client = \ + $(call interfaces_to_client_c,$(CLIENT_INTERFACES_C)) \ + client.c \ + calc_local.c \ + +SRC_C_calc_server = \ + $(call interfaces_to_server_c,$(SERVER_INTERFACES_C)) \ + server.c \ + +SRC_C_calc_counter_client = \ + $(call interfaces_to_client_c,$(CLIENT_INTERFACES_C)) \ + client_compound.c \ + +SRC_C_calc_counter_server = \ + $(call interfaces_to_server_c,$(SERVER_INTERFACES_C)) \ + $(call interfaces_to_server_c,$(COMP_INTERFACES_C)) \ + server_compound.c \ + +REQUIRES_LIBS = l4re_c-util libipc + +PRIVATE_INCDIR = $(IDL_BUILD_DIR) $(IDL_EXPORT_DIR) + +include $(L4DIR)/mk/prog.mk +include $(IDL_MK_DIR)/interface_rules.mk diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/calc_local.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/calc_local.c Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,77 @@ +/* + * Provide a calculation object for local use. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include + +#include + +#include "calc_interface.h" + + + +/* Component methods. */ + +static long calc_add(ref_Calc _self, int left, int right, int *result) +{ + (void) _self; + *result = left + right; + return L4_EOK; +} + +static long calc_subtract(ref_Calc _self, int left, int right, int *result) +{ + (void) _self; + *result = left - right; + return L4_EOK; +} + +static long calc_multiply(ref_Calc _self, int left, int right, int *result) +{ + (void) _self; + *result = left * right; + return L4_EOK; +} + +static long calc_divide(ref_Calc _self, int numerator, int denominator, int *result) +{ + (void) _self; + *result = numerator / denominator; + return L4_EOK; +} + +static long calc_pow(ref_Calc _self, double base, double exponent, double *result) +{ + (void) _self; + *result = powf(base, exponent); + return L4_EOK; +} + + + +/* Interface for this component. */ + +iface_Calc local_iface_Calc = { + .add=calc_add, + .subtract=calc_subtract, + .multiply=calc_multiply, + .divide=calc_divide, + .pow=calc_pow + }; diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/calc_local.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/calc_local.h Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,28 @@ +/* + * Provide a calculation object for local use. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#pragma once + +#include "calc_interface.h" + +/* Interface for a local component. */ + +extern iface_Calc local_iface_Calc; diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/client.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/client.c Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,63 @@ +/* + * Access a calculation server. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include + +#include + +#include "calc_client.h" +#include "calc_local.h" + +int main(void) +{ + l4_cap_idx_t server = l4re_env_get_cap("server"); + + /* Reference to a calculator and encapsulation. */ + + ref_Calc ref = {.cap=server}; + Calc obj = {.ref=ref, .iface=&client_iface_Calc}; + + /* Reference to another calculator. */ + + ref_Calc local_ref = {.ptr=0}; + Calc local_obj = {.ref=local_ref, .iface=&local_iface_Calc}; + + /* Call the calculator. */ + + int result; + + /* Invoke the operation function directly. */ + + Calc_add(ref, 123, 456, &result); + printf("add 123, 456 -> %d\n", result); + + /* Invoke the operation via the interface. */ + + obj.iface->add(obj.ref, 789, 123, &result); + printf("add 789, 123 -> %d\n", result); + + /* Invoke an operation on the local object via the interface. */ + + local_obj.iface->add(local_obj.ref, 789, 123, &result); + printf("add 789, 123 -> %d\n", result); + + return 0; +} diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/client_compound.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/client_compound.c Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,70 @@ +/* + * Access a server offering a compound interface. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include + +#include + +#include "calc_client.h" +#include "counter_client.h" + +int main(void) +{ + l4_cap_idx_t server = l4re_env_get_cap("server"); + + /* Reference to a calculator and encapsulation. */ + + ref_Calc calc_ref = {.cap=server}; + Calc calc_obj = {.ref=calc_ref, .iface=&client_iface_Calc}; + + /* Reference to a counter and encapsulation. */ + + ref_Counter counter_ref = {.cap=server}; + Counter counter_obj = {.ref=counter_ref, .iface=&client_iface_Counter}; + + /* Call the calculator. */ + + int result; + + /* Invoke the operation function directly. */ + + Calc_add(calc_ref, 123, 456, &result); + printf("add 123, 456 -> %d\n", result); + + /* Invoke the operation via the interface. */ + + calc_obj.iface->add(calc_obj.ref, 789, 123, &result); + printf("add 789, 123 -> %d\n", result); + + /* Call the counter. */ + + /* Invoke the operation function directly. */ + + Counter_increment(counter_ref, &result); + printf("increment -> %d\n", result); + + /* Invoke the operation via the interface. */ + + counter_obj.iface->increment(counter_obj.ref, &result); + printf("increment -> %d\n", result); + + return 0; +} diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/conf/calc.cfg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/conf/calc.cfg Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,23 @@ +-- vim:set ft=lua: + +local L4 = require("L4"); + +local l = L4.default_loader; + +local ipc = l:new_channel(); + +l:start({ + caps = { + server = ipc:svr(), + }, + log = { "calc_server", "y" }, + }, + "rom/calc_server"); + +l:start({ + caps = { + server = ipc, + }, + log = { "calc_client", "g" }, + }, + "rom/calc_client"); diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/conf/calc.list --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/conf/calc.list Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,24 @@ +entry calc +roottask moe rom/calc.cfg +module calc.cfg +module l4re +module ned +module calc_client +module calc_server +module lib4re-c.so +module lib4re-c-util.so +module lib4re.so +module lib4re-util.so +module libc_be_l4refile.so +module libc_be_l4re.so +module libc_be_socket_noop.so +module libc_support_misc.so +module libdl.so +module libipc.so +module libl4sys-direct.so +module libl4sys.so +module libl4util.so +module libld-l4.so +module libpthread.so +module libsupc++.so +module libuc_c.so diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/conf/calc_counter.cfg --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/conf/calc_counter.cfg Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,23 @@ +-- vim:set ft=lua: + +local L4 = require("L4"); + +local l = L4.default_loader; + +local ipc = l:new_channel(); + +l:start({ + caps = { + server = ipc:svr(), + }, + log = { "calc_counter_server", "y" }, + }, + "rom/calc_counter_server"); + +l:start({ + caps = { + server = ipc, + }, + log = { "calc_counter_client", "g" }, + }, + "rom/calc_counter_client"); diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/conf/calc_counter.list --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/conf/calc_counter.list Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,24 @@ +entry calc_counter +roottask moe rom/calc_counter.cfg +module calc_counter.cfg +module l4re +module ned +module calc_counter_client +module calc_counter_server +module lib4re-c.so +module lib4re-c-util.so +module lib4re.so +module lib4re-util.so +module libc_be_l4refile.so +module libc_be_l4re.so +module libc_be_socket_noop.so +module libc_support_misc.so +module libdl.so +module libipc.so +module libl4sys-direct.so +module libl4sys.so +module libl4util.so +module libld-l4.so +module libpthread.so +module libsupc++.so +module libuc_c.so diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/server.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/server.c Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,130 @@ +/* + * Provide a calculation server. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include +#include + +#include +#include + +#include + +#include "calc_server.h" + + + +/* Component methods. */ + +static long calc_add(ref_Calc _self, int left, int right, int *result) +{ + (void) _self; + *result = left + right; + return L4_EOK; +} + +static long calc_subtract(ref_Calc _self, int left, int right, int *result) +{ + (void) _self; + *result = left - right; + return L4_EOK; +} + +static long calc_multiply(ref_Calc _self, int left, int right, int *result) +{ + (void) _self; + *result = left * right; + return L4_EOK; +} + +static long calc_divide(ref_Calc _self, int numerator, int denominator, int *result) +{ + (void) _self; + *result = numerator / denominator; + return L4_EOK; +} + +static long calc_pow(ref_Calc _self, double base, double exponent, double *result) +{ + (void) _self; + *result = powf(base, exponent); + return L4_EOK; +} + + + +/* Interface for this component. */ + +iface_Calc server_iface_Calc = { + .add=calc_add, + .subtract=calc_subtract, + .multiply=calc_multiply, + .divide=calc_divide, + .pow=calc_pow + }; + + + +int main(void) +{ + /* Reference to a calculator and encapsulation. */ + + ref_Calc ref = {.ptr=0}; + Calc obj = {.ref=ref, .iface=&server_iface_Calc}; + + /* Server capability, incoming message and label. */ + + l4_cap_idx_t server; + ipc_message_t msg; + l4_umword_t label; + Calc *target; + + /* Register a server associating it with the given object. */ + + long err = ipc_server_bind("server", (l4_umword_t) &obj, &server); + + if (err) + { + printf("Could not bind server: %s\n", l4sys_errtostr(err)); + return 1; + } + + /* Wait for messages, dispatching to the handler. */ + + while (1) + { + ipc_message_wait(&msg, &label); + + /* Ignore erroneous messages. */ + + if (l4_ipc_error(msg.tag, l4_utcb())) + continue; + + /* Handle messages for different server objects. */ + + target = (Calc *) (label & ~3UL); + + /* Dispatch to the object. */ + + handle_Calc(&msg, target); + } + + return 0; +} diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/calc/server_compound.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/calc/server_compound.c Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,156 @@ +/* + * Provide a server offering a compound interface. + * + * Copyright (C) 2020 Paul Boddie + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301, USA + */ + +#include +#include + +#include +#include + +#include + +#include "calc_counter_server.h" + + + +/* Component methods. */ + +static long calc_add(ref_Calc _self, int left, int right, int *result) +{ + (void) _self; + *result = left + right; + return L4_EOK; +} + +static long calc_subtract(ref_Calc _self, int left, int right, int *result) +{ + (void) _self; + *result = left - right; + return L4_EOK; +} + +static long calc_multiply(ref_Calc _self, int left, int right, int *result) +{ + (void) _self; + *result = left * right; + return L4_EOK; +} + +static long calc_divide(ref_Calc _self, int numerator, int denominator, int *result) +{ + (void) _self; + *result = numerator / denominator; + return L4_EOK; +} + +static long calc_pow(ref_Calc _self, double base, double exponent, double *result) +{ + (void) _self; + *result = powf(base, exponent); + return L4_EOK; +} + + + +/* Component state and methods. */ + +static int counter = 0; + +static long counter_increment(ref_Counter _self, int *result) +{ + int *counter = (int *) (_self.ptr); + + *counter = *counter + 1; + *result = *counter; + return L4_EOK; +} + + + +/* Interfaces for this component. */ + +iface_Calc server_iface_Calc = { + .add=calc_add, + .subtract=calc_subtract, + .multiply=calc_multiply, + .divide=calc_divide, + .pow=calc_pow + }; + +iface_Counter server_iface_Counter = { + .increment=counter_increment + }; + +/* The compound interface referencing the individual interfaces. */ + +iface_CalcCounter server_iface_CalcCounter = { + .to_Calc=&server_iface_Calc, + .to_Counter=&server_iface_Counter + }; + + + +int main(void) +{ + /* Reference to state information and an encapsulation. */ + + ref_CalcCounter ref = {.ptr=&counter}; + CalcCounter obj = {.ref=ref, .iface=&server_iface_CalcCounter}; + + /* Server capability, incoming message and label. */ + + l4_cap_idx_t server; + ipc_message_t msg; + l4_umword_t label; + CalcCounter *target; + + /* Register a server associating it with the given object. */ + + long err = ipc_server_bind("server", (l4_umword_t) &obj, &server); + + if (err) + { + printf("Could not bind server: %s\n", l4sys_errtostr(err)); + return 1; + } + + /* Wait for messages, dispatching to the handler. */ + + while (1) + { + ipc_message_wait(&msg, &label); + + /* Ignore erroneous messages. */ + + if (l4_ipc_error(msg.tag, l4_utcb())) + continue; + + /* Handle messages for different server objects. */ + + target = (CalcCounter *) (label & ~3UL); + + /* Dispatch to the object. */ + + handle_CalcCounter(&msg, target); + } + + return 0; +} diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/idl/calc.idl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/idl/calc.idl Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,9 @@ +[protocol(0x1d1e)] +interface Calc +{ + void add(in int left, in int right, out int result); + void subtract(in int left, in int right, out int result); + void multiply(in int left, in int right, out int result); + void divide(in int numerator, in int denominator, out int result); + void pow(in double base, in double exponent, out double result); +}; diff -r 47f0683550fa -r f1c43fe96a60 pkg/idl4re-examples/idl/counter.idl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/idl4re-examples/idl/counter.idl Sat Apr 25 15:44:16 2020 +0200 @@ -0,0 +1,5 @@ +[protocol(0x1d1f)] +interface Counter +{ + void increment(out int result); +};