Loading...
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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 | /* echo-client.c - Send and receive unicast or multicast packets */
/*
* Copyright (c) 2015 Intel Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* The echo-client application is acting as a client that is run in Zephyr OS,
* and echo-server is run in the host acting as a server. The client will send
* either unicast or multicast packets to the server which will reply the packet
* back to the originator.
*/
#if defined(CONFIG_STDOUT_CONSOLE)
#include <stdio.h>
#define PRINT printf
#else
#include <misc/printk.h>
#define PRINT printk
#endif
#include <zephyr.h>
#include <sections.h>
#include <errno.h>
#include <drivers/rand32.h>
#include <net/ip_buf.h>
#include <net/net_core.h>
#include <net/net_socket.h>
#include <bluetooth/bluetooth.h>
#include <gatt/ipss.h>
#if defined(CONFIG_NET_TESTING)
#include <net_testing.h>
#else
#if defined(CONFIG_NETWORKING_WITH_IPV6)
#include <contiki/ipv6/uip-ds6.h>
#endif
#endif
/* Generated by http://www.lipsum.com/
* 3 paragraphs, 176 words, 1230 bytes of Lorem Ipsum
*/
static const char *lorem_ipsum =
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum id cursus felis, sit amet suscipit velit. Integer facilisis malesuada porta. Nunc at accumsan mauris. Etiam vehicula, arcu consequat feugiat venenatis, tellus velit gravida ligula, quis posuere sem leo eget urna. Curabitur condimentum leo nec orci mattis, nec faucibus dui rutrum. Ut mollis orci in iaculis consequat. Nulla volutpat nibh eu velit sagittis, a iaculis dui aliquam."
"\n"
"Quisque interdum consequat eros a eleifend. Fusce dapibus nisl sit amet velit posuere imperdiet. Quisque accumsan tempor massa sit amet tincidunt. Integer sollicitudin vehicula tristique. Nulla sagittis massa turpis, ac ultricies neque posuere eu. Nulla et imperdiet ex. Etiam venenatis sed lacus tincidunt hendrerit. In libero nisl, congue id tellus vitae, tincidunt tristique mauris. Nullam sed porta massa. Sed condimentum sem eu convallis euismod. Suspendisse lobortis purus faucibus, gravida turpis id, mattis velit. Maecenas eleifend sapien eu tincidunt lobortis. Sed elementum sapien id enim laoreet consequat."
"\n"
"Aenean et neque aliquam, lobortis lectus in, consequat leo. Sed quis egestas nulla. Quisque ac risus quis elit mollis finibus. Phasellus efficitur imperdiet metus."
"\n";
static int expecting;
static int ipsum_len;
#if defined(CONFIG_NETWORKING_WITH_IPV6)
/* admin-local, dynamically allocated multicast address */
#define MCAST_IPADDR { { { 0xff, 0x84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2 } } }
/* Define the peer IP address where to send messages */
#if !defined(CONFIG_NET_TESTING)
#define PEER_IPADDR { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x1 } } }
#define MY_IPADDR { { { 0x20, 0x01, 0x0d, 0xb8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x2 } } }
#define MY_PREFIX_LEN 64
#endif
static const struct in6_addr in6addr_my = MY_IPADDR;
static const struct in6_addr in6addr_peer = PEER_IPADDR;
#else /* CONFIG_NETWORKING_WITH_IPV6 */
/* Organization-local 239.192.0.0/14 */
#define MCAST_IPADDR { { { 239, 192, 0, 2 } } }
#define UIP_IPADDR0 192
#define UIP_IPADDR1 0
#define UIP_IPADDR2 2
#define UIP_IPADDR3 2
#define UIP_IPADDR3_PEER 1
#define UIP_DRIPADDR0 UIP_IPADDR0
#define UIP_DRIPADDR1 UIP_IPADDR1
#define UIP_DRIPADDR2 UIP_IPADDR2
#define UIP_DRIPADDR3 42
uip_ipaddr_t uip_hostaddr = { { UIP_IPADDR0, UIP_IPADDR1,
UIP_IPADDR2, UIP_IPADDR3 } };
uip_ipaddr_t uip_draddr = { { UIP_DRIPADDR0, UIP_DRIPADDR1,
UIP_DRIPADDR2, UIP_DRIPADDR3 } };
uip_ipaddr_t uip_netmask = { { 255, 255, 255, 0 } };
#if !defined(CONFIG_NET_TESTING)
#define PEER_IPADDR { { { UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, \
UIP_IPADDR3_PEER } } }
#define MY_IPADDR { { { UIP_IPADDR0, UIP_IPADDR1, UIP_IPADDR2, \
UIP_IPADDR3 } } }
#endif
static const struct in_addr in4addr_my = MY_IPADDR;
#endif /* CONFIG_NETWORKING_WITH_IPV6 */
#define MY_PORT 8484
#define PEER_PORT 4242
static struct net_context *unicast, *multicast;
static inline void init_app(void)
{
PRINT("%s: run mcast tester\n", __func__);
sys_rand32_init();
#if defined(CONFIG_NET_TESTING)
net_testing_setup();
#else
#if defined(CONFIG_NETWORKING_WITH_IPV6)
uip_ds6_prefix_add((uip_ipaddr_t *)&in6addr_peer, MY_PREFIX_LEN, 0);
#endif
#endif
}
static inline void reverse(unsigned char *buf, int len)
{
int i, last = len - 1;
for (i = 0; i < len / 2; i++) {
unsigned char tmp = buf[i];
buf[i] = buf[last - i];
buf[last - i] = tmp;
}
}
#if 1
#define WAIT_TIME 2
#define WAIT_TICKS (WAIT_TIME * sys_clock_ticks_per_sec)
#else
#define WAIT_TICKS TICKS_UNLIMITED
#endif
#define ONE_SEC (1 * sys_clock_ticks_per_sec)
static inline int send_packet(const char *name,
struct net_context *ctx,
int ipsum_len,
int pos)
{
static struct net_buf *buf;
int ret = 0;
if (buf) {
/* We have a pending packet that needs to be sent
* first.
*/
PRINT("%s: Trying to re-send %p len %d\n", __func__, buf,
buf->len);
ret = net_send(buf);
if (ret == -EAGAIN || ret == -EINPROGRESS) {
PRINT("%s: packet %p needs to be re-sent (%d)\n",
name, buf, ret);
return ret;
} else if (ret < 0) {
PRINT("%s: returned %d pending buffer discarded %p\n",
name, ret, buf);
ip_buf_unref(buf);
buf = NULL;
return ret;
} else {
PRINT("%s: returned %d pending buffer cleared %p\n",
name, ret, buf);
buf = NULL;
return 0;
}
}
buf = ip_buf_get_tx(ctx);
if (buf) {
uint8_t *ptr;
int sending_len = ipsum_len - pos;
ptr = net_buf_add(buf, sending_len);
memcpy(ptr, lorem_ipsum + pos, sending_len);
ip_buf_appdatalen(buf) = sending_len;
PRINT("%s: Trying to send %p buflen %d datalen %d\n",
__func__, buf, buf->len, ip_buf_appdatalen(buf));
ret = net_send(buf);
if (ret < 0) {
if (ret == -EINPROGRESS) {
PRINT("%s: no connection yet, try again\n",
__func__);
} else if (ret == -EAGAIN || ret == -ECONNRESET) {
PRINT("%s: no connection, try again later\n",
__func__);
} else {
PRINT("%s: sending %d bytes failed\n",
__func__, sending_len);
ip_buf_unref(buf);
buf = NULL;
}
} else {
PRINT("%s: sent %d bytes\n", __func__,
sending_len);
buf = NULL;
}
}
return ret;
}
static inline bool wait_reply(const char *name,
struct net_context *ctx,
int ipsum_len,
int pos)
{
struct net_buf *buf;
bool fail = false;
int expected_len = ipsum_len - pos;
/* Wait for the answer */
#if defined(CONFIG_NETWORKING_WITH_TCP)
again:
buf = net_receive(ctx, WAIT_TICKS);
if (buf) {
int total_len = expected_len, sum = 0, count = 0;
if (ip_buf_appdatalen(buf) < expected_len) {
if (memcmp(lorem_ipsum + pos, ip_buf_appdata(buf),
ip_buf_appdatalen(buf))) {
PRINT("%s: received %d bytes (total %d), "
"partial data mismatch\n",
name, ip_buf_appdatalen(buf),
expected_len);
fail = true;
goto free_buf;
}
/* So far everything is ok, more data to come,
* wait it all.
*/
pos += ip_buf_appdatalen(buf);
expected_len -= ip_buf_appdatalen(buf);
sum += ip_buf_appdatalen(buf);
ip_buf_unref(buf);
count++;
goto again;
} else {
/* Did we get all the data back?
*/
sum += ip_buf_appdatalen(buf);
if (memcmp(lorem_ipsum + pos, ip_buf_appdata(buf),
expected_len)) {
PRINT("%s: received data mismatch in "
"last packet.\n", name);
fail = true;
goto free_buf;
}
if (total_len != sum) {
PRINT("Received %d bytes, expected %d\n",
sum, total_len);
} else {
PRINT("Received %d bytes (in %d messages)\n",
total_len, count + 1);
}
}
#else
buf = net_receive(ctx, WAIT_TICKS);
if (buf) {
if (ip_buf_appdatalen(buf) != expected_len) {
PRINT("%s: received %d bytes, expected %d\n",
name, ip_buf_appdatalen(buf), expected_len);
fail = true;
goto free_buf;
}
/* In this test we reverse the received bytes.
* We could just pass the data back as is but
* this way it is possible to see how the app
* can manipulate the received data.
*/
reverse(ip_buf_appdata(buf), ip_buf_appdatalen(buf));
/* Did we get all the data back?
*/
if (memcmp(lorem_ipsum + pos, ip_buf_appdata(buf),
expected_len)) {
PRINT("%s: received data mismatch.\n", name);
fail = true;
goto free_buf;
}
PRINT("%s: received %d bytes\n", __func__,
expected_len);
#endif
free_buf:
ip_buf_unref(buf);
} else {
PRINT("%s: expected data, got none\n", name);
fail = true;
}
return fail;
}
static inline bool get_context(struct net_context **unicast,
struct net_context **multicast)
{
static struct net_addr mcast_addr;
static struct net_addr any_addr;
static struct net_addr peer_addr;
static struct net_addr my_addr;
int proto = IPPROTO_UDP;
#if defined(CONFIG_NETWORKING_WITH_IPV6)
static const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT;
static const struct in6_addr in6addr_mcast = MCAST_IPADDR;
mcast_addr.in6_addr = in6addr_mcast;
mcast_addr.family = AF_INET6;
peer_addr.in6_addr = in6addr_peer;
peer_addr.family = AF_INET6;
any_addr.in6_addr = in6addr_any;
any_addr.family = AF_INET6;
my_addr.in6_addr = in6addr_my;
my_addr.family = AF_INET6;
#else
static const struct in_addr in4addr_any = { { { 0 } } };
static struct in_addr in4addr_mcast = MCAST_IPADDR;
static struct in_addr in4addr_peer = PEER_IPADDR;
mcast_addr.in_addr = in4addr_mcast;
mcast_addr.family = AF_INET;
peer_addr.in_addr = in4addr_peer;
peer_addr.family = AF_INET;
any_addr.in_addr = in4addr_any;
any_addr.family = AF_INET;
my_addr.in_addr = in4addr_my;
my_addr.family = AF_INET;
#endif
#ifdef CONFIG_NETWORKING_WITH_TCP
proto = IPPROTO_TCP;
#endif /* CONFIG_NETWORKING_WITH_TCP */
*unicast = net_context_get(proto,
&peer_addr, PEER_PORT,
&my_addr, MY_PORT);
if (!*unicast) {
PRINT("%s: Cannot get sending network context\n",
__func__);
return false;
}
#if !defined(CONFIG_NETWORKING_WITH_TCP)
*multicast = net_context_get(IPPROTO_UDP,
&mcast_addr, PEER_PORT,
&my_addr, MY_PORT);
if (!*multicast) {
PRINT("%s: Cannot get mcast sending network context\n",
__func__);
return false;
}
#else
*multicast = NULL;
#endif
return true;
}
#if defined(CONFIG_NANOKERNEL)
#define STACKSIZE 2000
static char __noinit __stack stack_receiving[STACKSIZE];
#endif
static bool sending(int resend)
{
static bool send_unicast = true;
int ret;
#if defined(CONFIG_NETWORKING_WITH_TCP)
static const char *type = "TCP";
#else
static const char *type = "Unicast";
#endif
PRINT("%s: Sending packet\n", __func__);
if (resend) {
expecting = resend;
} else {
expecting = sys_rand32_get() % ipsum_len;
}
if (send_unicast) {
again:
ret = send_packet(__func__, unicast, ipsum_len,
expecting);
if (ret == -EAGAIN || ret == -EINPROGRESS) {
fiber_sleep(10);
PRINT("retrying...\n");
goto again;
} else if (ret == -ETIMEDOUT) {
PRINT("Connection timed out\n");
return false;
} else if (ret < 0) {
PRINT("%s sending %d bytes FAIL\n", type,
ipsum_len - expecting);
} else {
PRINT("%s sent %d bytes\n", type,
ipsum_len - expecting);
}
#if !defined(CONFIG_NETWORKING_WITH_TCP)
} else {
if (send_packet(__func__, multicast, ipsum_len,
expecting)) {
PRINT("Multicast sending %d bytes FAIL\n",
ipsum_len - expecting);
}
#endif
}
return true;
}
void receiving(void)
{
int expecting_len = 0;
while (sending(expecting_len)) {
PRINT("Waiting packet\n");
if (wait_reply(__func__, unicast,
ipsum_len, expecting)) {
if (expecting_len > 0) {
PRINT("Resend %d bytes -> FAIL\n",
ipsum_len - expecting);
expecting_len = 0;
} else {
PRINT("Waiting %d bytes -> resending\n",
ipsum_len - expecting);
expecting_len = expecting;
}
}
fiber_sleep(10);
}
}
void main(void)
{
net_init();
init_app();
ipsum_len = strlen(lorem_ipsum);
#if defined(CONFIG_NETWORKING_WITH_BT)
if (bt_enable(NULL)) {
PRINT("Bluetooth init failed\n");
return;
}
ipss_init();
ipss_advertise();
#endif
if (!get_context(&unicast, &multicast)) {
PRINT("%s: Cannot get network context\n", __func__);
return;
}
#if defined(CONFIG_MICROKERNEL)
receiving();
#else
task_fiber_start(&stack_receiving[0], STACKSIZE,
(nano_fiber_entry_t)receiving, 0, 0, 7, 0);
#endif
}
|