forked from space-ros/docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.c
208 lines (182 loc) · 5.4 KB
/
hello.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <assert.h>
#include <rtems.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sysexits.h>
#include <rtems/bsd/bsd.h>
#include <rtems/bsd/iface.h>
#include <rtems/printer.h>
#include <rtems/stackchk.h>
#include <rtems/bspIo.h>
#include <machine/rtems-bsd-commands.h>
#include <zenoh-pico.h>
static void my_on_exit(int exit_code, void *arg)
{
rtems_printer printer;
(void)arg; // suppress warning
rtems_print_printer_printf(&printer);
rtems_stack_checker_report_usage_with_plugin(&printer);
}
static void
default_wait_for_link_up( const char *name )
{
size_t seconds = 0;
while ( true ) {
bool link_active = false;
assert(rtems_bsd_iface_link_state( name, &link_active ) == 0);
if (link_active) {
return;
}
sleep( 1 );
++seconds;
if (seconds > 10) {
printf("error: %s: no active link\n", name);
assert(seconds < 10);
}
}
}
void print_zid(const z_id_t *id, void *ctx) {
(void)(ctx);
printf(" ");
for (int i = 15; i >= 0; i--) {
printf("%02X", id->id[i]);
}
printf("\n");
}
rtems_task Init(
rtems_task_argument ignored
)
{
rtems_bsd_setlogpriority("debug");
printk("\nInit() start\n\n");
on_exit(my_on_exit, NULL);
rtems_status_code sc;
// set ourselves as minimum priority
rtems_task_priority priority = RTEMS_MAXIMUM_PRIORITY - 1;
sc = rtems_task_set_priority(RTEMS_SELF, priority, &priority);
assert(sc == RTEMS_SUCCESSFUL);
sc = rtems_bsd_initialize();
assert(sc == RTEMS_SUCCESSFUL);
sc = rtems_task_wake_after(2); // not sure of the units... maybe 10ms ticks?
rtems_bsd_ifconfig_lo0();
int exit_code = 0;
char *ifcfg_print[] = {
"ifconfig",
NULL
};
exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(ifcfg_print), ifcfg_print);
if (exit_code != EX_OK)
{
printk("ifconfig exit code: %d\n", exit_code);
exit(1);
}
const char *iface_name = "cgem3";
char *ifcfg_set[] = {
"ifconfig",
iface_name,
"inet",
"10.0.42.100",
"netmask",
"255.255.255.0",
NULL
};
exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(ifcfg_set), ifcfg_set);
exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(ifcfg_print), ifcfg_print);
if (exit_code != EX_OK)
{
printk("ifconfig exit code: %d\n", exit_code);
exit(1);
}
assert(exit_code == EX_OK);
sc = rtems_task_wake_after(100); // not sure of the units... maybe 10ms ticks?
char *route_print[] = {
"netstat",
"-r",
NULL
};
exit_code = rtems_bsd_command_netstat(RTEMS_BSD_ARGC(route_print), route_print);
if (exit_code != EX_OK)
{
printk("print routes exit code: %d\n", exit_code);
exit(1);
}
char *route_default[] = {
"route",
"add",
"default",
"10.0.42.1",
NULL
};
exit_code = rtems_bsd_command_route(RTEMS_BSD_ARGC(route_default), route_default);
if (exit_code != EX_OK)
{
printk("set routes exit code: %d\n", exit_code);
exit(1);
}
exit_code = rtems_bsd_command_netstat(RTEMS_BSD_ARGC(route_print), route_print);
if (exit_code != EX_OK)
{
printk("print routes exit code: %d\n", exit_code);
exit(1);
}
z_owned_config_t config = z_config_default();
zp_config_insert(
z_config_loan(&config),
Z_CONFIG_MODE_KEY,
z_string_make("client"));
zp_config_insert(
z_config_loan(&config),
Z_CONFIG_PEER_KEY,
z_string_make("tcp/10.0.42.1:7447"));
printk("Opening zenoh session...\n");
z_owned_session_t s = z_open(z_move(config));
if (!z_check(s)) {
printk("Unable to open session!\n");
exit(2);
}
printk("Zenoh session opened.\n");
// Start read and lease tasks for zenoh-pico
if (zp_start_read_task(z_session_loan(&s), NULL) < 0 || zp_start_lease_task(z_session_loan(&s), NULL) < 0) {
printf("Unable to start read and lease tasks");
exit(1);
}
z_id_t self_id = z_info_zid(z_session_loan(&s));
printf("Own ID:");
print_zid(&self_id, NULL);
printf("Routers IDs:\n");
z_owned_closure_zid_t callback = z_closure(print_zid);
z_info_routers_zid(z_loan(s), z_move(callback));
// `callback` has been `z_move`d just above, so it's safe to reuse the variable,
// we'll just have to make sure we `z_move` it again to avoid mem-leaks.
printf("Peers IDs:\n");
z_owned_closure_zid_t callback2 = z_closure(print_zid);
z_info_peers_zid(z_loan(s), z_move(callback2));
z_owned_publisher_t pub = z_declare_publisher(z_loan(s), z_keyexpr("example"), NULL);
if (!z_check(pub)) {
printf("Unable to declare publisher\n");
exit(1);
}
// wait a bit for our publisher registration to take effect
// (we need to let some kernel switches happen)
sleep(1);
printf("sending a few messages...\n");
char msg_buf[100] = {};
for (int i = 0; i < 10; i++) {
snprintf(msg_buf, sizeof(msg_buf), "Hello, world! %d", i);
z_publisher_put_options_t options = z_publisher_put_options_default();
options.encoding = z_encoding(Z_ENCODING_PREFIX_TEXT_PLAIN, NULL);
z_publisher_put(z_loan(pub), (const uint8_t *)msg_buf, strlen(msg_buf), &options);
printf("publishing: %s\n", msg_buf);
usleep(100000);
}
z_undeclare_publisher(z_move(pub));
// Stop read and lease tasks for zenoh-pico
printf("Stopping read and lease tasks...\n");
zp_stop_read_task(z_session_loan(&s));
zp_stop_lease_task(z_session_loan(&s));
printf("Closing zenoh session...\n");
//z_close(z_move(s)); // not sure why, but this hangs...
printf("Done. Goodbye.\n");
exit(0);
}