1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/conf/idl4re-examples/nested_counter.cfg Wed Mar 16 00:03:07 2022 +0100
1.3 @@ -0,0 +1,23 @@
1.4 +-- vim:set ft=lua:
1.5 +
1.6 +local L4 = require("L4");
1.7 +
1.8 +local l = L4.default_loader;
1.9 +
1.10 +local ipc = l:new_channel();
1.11 +
1.12 +l:start({
1.13 + caps = {
1.14 + server = ipc:svr(),
1.15 + },
1.16 + log = { "nested_counter_server", "y" },
1.17 + },
1.18 + "rom/nested_counter_server");
1.19 +
1.20 +l:start({
1.21 + caps = {
1.22 + server = ipc,
1.23 + },
1.24 + log = { "nested_counter_client", "g" },
1.25 + },
1.26 + "rom/nested_counter_client");
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
2.2 +++ b/conf/idl4re-examples/nested_counter.list Wed Mar 16 00:03:07 2022 +0100
2.3 @@ -0,0 +1,24 @@
2.4 +entry nested_counter
2.5 +roottask moe rom/nested_counter.cfg
2.6 +module nested_counter.cfg
2.7 +module l4re
2.8 +module ned
2.9 +module nested_counter_client
2.10 +module nested_counter_server
2.11 +module lib4re-c.so
2.12 +module lib4re-c-util.so
2.13 +module lib4re.so
2.14 +module lib4re-util.so
2.15 +module libc_be_l4refile.so
2.16 +module libc_be_l4re.so
2.17 +module libc_be_socket_noop.so
2.18 +module libc_support_misc.so
2.19 +module libdl.so
2.20 +module libipc.so
2.21 +module libl4sys-direct.so
2.22 +module libl4sys.so
2.23 +module libl4util.so
2.24 +module libld-l4.so
2.25 +module libpthread.so
2.26 +module libsupc++.so
2.27 +module libuc_c.so
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/pkg/idl4re-examples/idl/nested_counter.idl Wed Mar 16 00:03:07 2022 +0100
3.3 @@ -0,0 +1,7 @@
3.4 +[protocol(0x1d1d)]
3.5 +interface NestedCounter
3.6 +{
3.7 + void increment(out int result);
3.8 +
3.9 + void next(out cap counter);
3.10 +};
4.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
4.2 +++ b/pkg/idl4re-examples/nested/Control Wed Mar 16 00:03:07 2022 +0100
4.3 @@ -0,0 +1,3 @@
4.4 +requires: libc libipc
4.5 +provides: nested
4.6 +maintainer: paul@boddie.org.uk
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
5.2 +++ b/pkg/idl4re-examples/nested/Makefile Wed Mar 16 00:03:07 2022 +0100
5.3 @@ -0,0 +1,47 @@
5.4 +PKGDIR ?= ..
5.5 +L4DIR ?= $(PKGDIR)/../..
5.6 +
5.7 +TARGET = nested_counter_client nested_counter_server
5.8 +MODE = shared
5.9 +
5.10 +# Locations for interface input and generated output.
5.11 +
5.12 +IDL_DIR = $(PKGDIR)/idl
5.13 +IDL_MK_DIR = $(L4DIR)/idl4re/mk
5.14 +IDL_BUILD_DIR = .
5.15 +IDL_EXPORT_DIR = .
5.16 +
5.17 +include $(IDL_MK_DIR)/idl.mk
5.18 +
5.19 +# Individual interfaces.
5.20 +
5.21 +CLIENT_INTERFACES_C = nested_counter
5.22 +SERVER_INTERFACES_C = nested_counter
5.23 +
5.24 +# Generated and plain source files.
5.25 +
5.26 +CLIENT_INTERFACES_SRC_C_nested_counter_client = $(call interfaces_to_client_c,nested_counter)
5.27 +SERVER_INTERFACES_SRC_C_nested_counter_server = $(call interfaces_to_server_c,nested_counter)
5.28 +
5.29 +PLAIN_SRC_C_nested_counter_client = client.c
5.30 +PLAIN_SRC_C_nested_counter_server = server.c
5.31 +
5.32 +# Normal definitions.
5.33 +
5.34 +SRC_C_nested_counter_client = \
5.35 + $(CLIENT_INTERFACES_SRC_C_nested_counter_client) \
5.36 + $(PLAIN_SRC_C_nested_counter_client)
5.37 +
5.38 +SRC_C_nested_counter_server = \
5.39 + $(SERVER_INTERFACES_SRC_C_nested_counter_server) \
5.40 + $(PLAIN_SRC_C_nested_counter_server)
5.41 +
5.42 +REQUIRES_LIBS = l4re_c-util libipc
5.43 +
5.44 +PRIVATE_INCDIR = $(IDL_BUILD_DIR) $(IDL_EXPORT_DIR)
5.45 +
5.46 +include $(L4DIR)/mk/prog.mk
5.47 +include $(IDL_MK_DIR)/interface_rules.mk
5.48 +
5.49 +$(PLAIN_SRC_C_nested_counter_client): $(CLIENT_INTERFACES_SRC_C_nested_counter_client)
5.50 +$(PLAIN_SRC_C_nested_counter_server): $(SERVER_INTERFACES_SRC_C_nested_counter_server)
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
6.2 +++ b/pkg/idl4re-examples/nested/client.c Wed Mar 16 00:03:07 2022 +0100
6.3 @@ -0,0 +1,75 @@
6.4 +/*
6.5 + * Access a server offering a nested counter.
6.6 + *
6.7 + * Copyright (C) 2020, 2022 Paul Boddie <paul@boddie.org.uk>
6.8 + *
6.9 + * This program is free software; you can redistribute it and/or
6.10 + * modify it under the terms of the GNU General Public License as
6.11 + * published by the Free Software Foundation; either version 2 of
6.12 + * the License, or (at your option) any later version.
6.13 + *
6.14 + * This program is distributed in the hope that it will be useful,
6.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
6.17 + * GNU General Public License for more details.
6.18 + *
6.19 + * You should have received a copy of the GNU General Public License
6.20 + * along with this program; if not, write to the Free Software
6.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
6.22 + * Boston, MA 02110-1301, USA
6.23 + */
6.24 +
6.25 +#include <l4/re/env.h>
6.26 +
6.27 +#include <stdio.h>
6.28 +
6.29 +#include "nested_counter_client.h"
6.30 +
6.31 +int main(void)
6.32 +{
6.33 + l4_cap_idx_t server = l4re_env_get_cap("server");
6.34 +
6.35 + /* Reference to a counter and encapsulation. */
6.36 +
6.37 + ref_NestedCounter counter_ref = {.cap=server};
6.38 + NestedCounter counter_obj = {.ref=counter_ref, .iface=&client_iface_NestedCounter};
6.39 +
6.40 + /* Reference to another counter. */
6.41 +
6.42 + ref_NestedCounter next_counter_ref = {.cap=L4_INVALID_CAP};
6.43 + NestedCounter next_counter_obj;
6.44 +
6.45 + /* Call the calculator. */
6.46 +
6.47 + int result;
6.48 +
6.49 + /* Call the counter. */
6.50 +
6.51 + /* Invoke the operation function directly. */
6.52 +
6.53 + NestedCounter_increment(counter_ref, &result);
6.54 + printf("increment -> %d\n", result);
6.55 +
6.56 + /* Invoke the operation via the interface. */
6.57 +
6.58 + counter_obj.iface->increment(counter_obj.ref, &result);
6.59 + printf("increment -> %d\n", result);
6.60 +
6.61 + /* Make a new counter. */
6.62 +
6.63 + counter_obj.iface->next(counter_obj.ref, &next_counter_ref.cap);
6.64 + printf("new counter: %lx\n", next_counter_ref.cap);
6.65 + next_counter_obj = (NestedCounter) {.ref=next_counter_ref, .iface=&client_iface_NestedCounter};
6.66 +
6.67 + /* Increment the next counter. */
6.68 +
6.69 + next_counter_obj.iface->increment(next_counter_obj.ref, &result);
6.70 + printf("increment (next) -> %d\n", result);
6.71 +
6.72 + /* Increment the original counter. */
6.73 +
6.74 + counter_obj.iface->increment(counter_obj.ref, &result);
6.75 + printf("increment -> %d\n", result);
6.76 +
6.77 + return 0;
6.78 +}
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/pkg/idl4re-examples/nested/server.c Wed Mar 16 00:03:07 2022 +0100
7.3 @@ -0,0 +1,130 @@
7.4 +/*
7.5 + * Provide a server offering a nested counter.
7.6 + *
7.7 + * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk>
7.8 + *
7.9 + * This program is free software; you can redistribute it and/or
7.10 + * modify it under the terms of the GNU General Public License as
7.11 + * published by the Free Software Foundation; either version 2 of
7.12 + * the License, or (at your option) any later version.
7.13 + *
7.14 + * This program is distributed in the hope that it will be useful,
7.15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7.16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7.17 + * GNU General Public License for more details.
7.18 + *
7.19 + * You should have received a copy of the GNU General Public License
7.20 + * along with this program; if not, write to the Free Software
7.21 + * Foundation, Inc., 51 Franklin Street, Fifth Floor,
7.22 + * Boston, MA 02110-1301, USA
7.23 + */
7.24 +
7.25 +#include <l4/re/env.h>
7.26 +#include <l4/sys/err.h>
7.27 +
7.28 +#include <math.h>
7.29 +#include <stdio.h>
7.30 +#include <stdlib.h>
7.31 +
7.32 +#include <pthread.h>
7.33 +#include <pthread-l4.h>
7.34 +#include <ipc/server.h>
7.35 +
7.36 +#include "nested_counter_server.h"
7.37 +
7.38 +
7.39 +
7.40 +/* Component state and methods. */
7.41 +
7.42 +struct counter
7.43 +{
7.44 + int counter;
7.45 + NestedCounter *next;
7.46 + ipc_server_config_type config;
7.47 +};
7.48 +
7.49 +static struct counter main_counter;
7.50 +
7.51 +static long counter_increment(ref_NestedCounter _self, int *result)
7.52 +{
7.53 + struct counter *counter = (struct counter *) (_self.ptr);
7.54 +
7.55 + counter->counter += 1;
7.56 + *result = counter->counter;
7.57 + return L4_EOK;
7.58 +}
7.59 +
7.60 +static void init_NestedCounter(NestedCounter *obj, struct counter *counter);
7.61 +
7.62 +static long counter_next(ref_NestedCounter _self, l4_cap_idx_t *counter)
7.63 +{
7.64 + struct counter *this_counter = (struct counter *) (_self.ptr);
7.65 + struct counter *next_counter;
7.66 + long err;
7.67 +
7.68 + if (this_counter->next == NULL)
7.69 + {
7.70 + this_counter->next = (NestedCounter *) malloc(sizeof(NestedCounter));
7.71 + if (this_counter->next == NULL)
7.72 + return -L4_ENOMEM;
7.73 +
7.74 + next_counter = (struct counter *) malloc(sizeof(struct counter));
7.75 + if (next_counter == NULL)
7.76 + {
7.77 + free(this_counter->next);
7.78 + return -L4_ENOMEM;
7.79 + }
7.80 +
7.81 + init_NestedCounter(this_counter->next, next_counter);
7.82 +
7.83 + /* Create a new server endpoint in the same thread. */
7.84 +
7.85 + err = ipc_server_add_config(&this_counter->config, NestedCounter,
7.86 + this_counter->next,
7.87 + pthread_l4_cap(pthread_self()));
7.88 +
7.89 + if (err)
7.90 + return err;
7.91 + }
7.92 +
7.93 + *counter = this_counter->config.server;
7.94 + return L4_EOK;
7.95 +}
7.96 +
7.97 +
7.98 +
7.99 +/* Interfaces for this component. */
7.100 +
7.101 +iface_NestedCounter server_iface_NestedCounter = {
7.102 + .increment=counter_increment,
7.103 + .next=counter_next
7.104 + };
7.105 +
7.106 +/* Initialisation. */
7.107 +
7.108 +static void init_NestedCounter(NestedCounter *obj, struct counter *counter)
7.109 +{
7.110 + counter->counter = 0;
7.111 + counter->next = NULL;
7.112 +
7.113 + /* Reference to state information and an encapsulation. */
7.114 +
7.115 + ref_NestedCounter ref = {.ptr=counter};
7.116 + obj->ref = ref;
7.117 + obj->iface = &server_iface_NestedCounter;
7.118 +}
7.119 +
7.120 +
7.121 +
7.122 +int main(void)
7.123 +{
7.124 + NestedCounter obj;
7.125 +
7.126 + init_NestedCounter(&obj, &main_counter);
7.127 +
7.128 + /* Wait for messages, dispatching to the handler. */
7.129 +
7.130 + ipc_server_loop_for(NestedCounter, &obj, "server");
7.131 +
7.132 + return 0;
7.133 +}