diff --git a/samples/net/loopback_test/Makefile b/samples/net/loopback_test/Makefile index 957fe8a5adc..0ffb151342a 100644 --- a/samples/net/loopback_test/Makefile +++ b/samples/net/loopback_test/Makefile @@ -1,4 +1,4 @@ -# Makefile - Simple loopback network app Makefile for nanokernel +# Makefile - Simple loopback network app Makefile # # Copyright (c) 2015 Intel Corporation @@ -17,8 +17,8 @@ # MDEF_FILE = prj.mdef -KERNEL_TYPE = nano +KERNEL_TYPE ?= nano BOARD ?= qemu_x86 -CONF_FILE = prj_$(ARCH).conf +CONF_FILE ?= prj.conf include $(ZEPHYR_BASE)/Makefile.inc diff --git a/samples/net/loopback_test/prj.conf b/samples/net/loopback_test/prj.conf new file mode 100644 index 00000000000..cce64e41b79 --- /dev/null +++ b/samples/net/loopback_test/prj.conf @@ -0,0 +1,5 @@ +CONFIG_NETWORKING=y +CONFIG_NETWORKING_WITH_LOOPBACK=y +CONFIG_NETWORKING_IPV6_NO_ND=y +CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_STDOUT_CONSOLE=y diff --git a/samples/net/loopback_test/prj.mdef b/samples/net/loopback_test/prj.mdef index bce99aa8be8..423da7d8899 100644 --- a/samples/net/loopback_test/prj.mdef +++ b/samples/net/loopback_test/prj.mdef @@ -1,5 +1,5 @@ -% Application : Bluetooth Shell +% Application : Network loopback test % TASK NAME PRIO ENTRY STACK GROUPS % =================================================== - TASK MAIN 7 mainloop 2048 [EXE] + TASK MAIN 7 main 2048 [EXE] diff --git a/samples/net/loopback_test/prj_arm.conf b/samples/net/loopback_test/prj_arm.conf deleted file mode 100644 index 943d4a680a6..00000000000 --- a/samples/net/loopback_test/prj_arm.conf +++ /dev/null @@ -1,3 +0,0 @@ -CONFIG_NETWORKING=y -CONFIG_NETWORKING_WITH_LOGGING=y -CONFIG_NETWORKING_WITH_LOOPBACK=y diff --git a/samples/net/loopback_test/prj_x86.conf b/samples/net/loopback_test/prj_x86.conf deleted file mode 100644 index 943d4a680a6..00000000000 --- a/samples/net/loopback_test/prj_x86.conf +++ /dev/null @@ -1,3 +0,0 @@ -CONFIG_NETWORKING=y -CONFIG_NETWORKING_WITH_LOGGING=y -CONFIG_NETWORKING_WITH_LOOPBACK=y diff --git a/samples/net/loopback_test/src/network.c b/samples/net/loopback_test/src/network.c index 1b266481a60..329bdfc7b18 100644 --- a/samples/net/loopback_test/src/network.c +++ b/samples/net/loopback_test/src/network.c @@ -35,7 +35,6 @@ /* Longer packet sending works only if fragmentation is supported * by network stack. */ -#if 0 /* Generated by http://www.lipsum.com/ * 2 paragraphs, 185 words, 1231 bytes of Lorem Ipsum * The main() will add one null byte at the end so the maximum @@ -45,46 +44,16 @@ static const char *lorem_ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam congue non neque vel tempor. In id porta nibh, ut cursus tortor. Morbi eleifend tristique vehicula. Nunc vitae risus mauris. Praesent vel imperdiet dolor, et ultricies nibh. Aliquam erat volutpat. Maecenas pellentesque dolor vitae dictum tincidunt. Fusce vel nibh nec leo tristique auctor eu a massa. Nam et tellus ac tortor sollicitudin semper vitae nec tortor. Aliquam nec lacus velit. Maecenas ornare ullamcorper justo non auctor. Donec aliquam feugiat turpis, quis elementum sem rutrum ut. Sed eu ullamcorper libero, ut suscipit magna." "\n" "Donec vehicula magna ut varius aliquam. Ut vitae commodo nulla, quis ornare dolor. Nulla tortor sem, venenatis eu iaculis id, commodo ut massa. Sed est lorem, euismod vitae enim sed, hendrerit gravida felis. Donec eros lacus, auctor ut ultricies eget, lobortis quis nisl. Aliquam sit amet blandit eros. Interdum et malesuada fames ac ante ipsum primis in faucibus. Quisque egestas nisl leo, sed consectetur leo ornare eu. Suspendisse vitae urna vel purus maximus finibus. Proin sed sollicitudin turpis. Mauris interdum neque eu tellus pellentesque, id fringilla nisi fermentum. Suspendisse gravida pharetra sodales orci aliquam."; -#else -static const char *lorem_ipsum = - "Lorem ipsum dolor sit amet, consectetur adipiscing elit."; -#endif - -#ifdef CONFIG_MICROKERNEL - -#error "Microkernel version not supported yet." - -/* - * Microkernel version of hello world demo has two tasks that utilize - * semaphores and sleeps to take turns printing a greeting message at - * a controlled rate. - */ - /* specify delay between greetings (in ms); compute equivalent in ticks */ -#define SLEEPTIME 500 -#define SLEEPTICKS (SLEEPTIME * sys_clock_ticks_per_sec / 1000) - -#else /* CONFIG_NANOKERNEL */ - -/* - * Nanokernel version of hello world demo has a task and a fiber that utilize - * semaphores and timers to take turns printing a greeting message at - * a controlled rate. - */ - -/* specify delay between greetings (in ms); compute equivalent in ticks */ - -#define SLEEPTIME 500 +#define SLEEPTIME 1000 #define SLEEPTICKS (SLEEPTIME * sys_clock_ticks_per_sec / 1000) #define STACKSIZE 2000 -char fiberStack[STACKSIZE]; - -struct nano_sem nanoSemTask; -struct nano_sem nanoSemFiber; +static char fiberReceiverStack[STACKSIZE]; +static char fiberSenderStack[STACKSIZE]; const struct in6_addr in6addr_any = IN6ADDR_ANY_INIT; /* :: */ const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; /* ::1 */ @@ -92,7 +61,16 @@ const struct in6_addr in6addr_loopback = IN6ADDR_LOOPBACK_INIT; /* ::1 */ static struct net_addr any_addr; static struct net_addr loopback_addr; -void fiberEntry(void) +static int sent; +static int received; + +static nano_thread_id_t sender_id; +static nano_thread_id_t receiver_id; + +static bool failure; +static int appdatalen; + +void fiber_receiver(void) { struct nano_timer timer; uint32_t data[2] = {0, 0}; @@ -107,39 +85,105 @@ void fiberEntry(void) return; } - nano_sem_init (&nanoSemFiber); - nano_timer_init (&timer, data); + nano_timer_init(&timer, data); - while (1) { - /* wait for task to let us have a turn */ - nano_fiber_sem_take(&nanoSemFiber, TICKS_UNLIMITED); - - buf = net_receive(ctx, TICKS_NONE); + while (!failure) { + buf = net_receive(ctx, TICKS_UNLIMITED); if (buf) { - PRINT("%s: received %d bytes\n", __func__, - ip_buf_appdatalen(buf)); + PRINT("%d: %s: received %d bytes\n", received++, + __func__, ip_buf_appdatalen(buf)); + + if (appdatalen != ip_buf_appdatalen(buf)) { + failure = true; + PRINT("Received %d bytes but was sent " + "%d bytes\n", + ip_buf_appdatalen(buf), appdatalen); + } else if (memcmp(lorem_ipsum, ip_buf_appdata(buf), + appdatalen - 1)) { + failure = true; + PRINT("Sent and received data do not match.\n"); + PRINT(" Sent: %.*s\n", appdatalen, lorem_ipsum); + PRINT("Received: %.*s\n", appdatalen, ip_buf_appdata(buf)); + } ip_buf_unref(buf); } - /* wait a while, then let task have a turn */ - nano_fiber_timer_start (&timer, SLEEPTICKS); - nano_fiber_timer_test(&timer, TICKS_UNLIMITED); - nano_fiber_sem_give (&nanoSemTask); + + fiber_wakeup(sender_id); + fiber_sleep(SLEEPTICKS); } } -void main(void) +void fiber_sender(void) { struct nano_timer timer; uint32_t data[2] = {0, 0}; struct net_context *ctx; struct net_buf *buf; - int len = strlen(lorem_ipsum); + int len; + int ipsum_len = strlen(lorem_ipsum); + ctx = net_context_get(IPPROTO_UDP, + &loopback_addr, 4242, + &any_addr, 0); + if (!ctx) { + PRINT("Cannot get network context\n"); + return; + } + + nano_timer_init(&timer, data); + + while (!failure) { + buf = ip_buf_get_tx(ctx); + if (buf) { + uint8_t *ptr; + uint16_t sent_len; + + len = sys_rand32_get() % ipsum_len; + + ptr = net_buf_add(buf, 0); + memcpy(ptr, lorem_ipsum, len); + ptr = net_buf_add(buf, len); + ptr = net_buf_add(buf, 1); /* add \0 */ + *ptr = '\0'; + sent_len = buf->len; + appdatalen = 0; + + if (net_send(buf) < 0) { + PRINT("%s: sending %d bytes failed\n", + __func__, len); + ip_buf_unref(buf); + } else { + appdatalen = sent_len - ip_buf_reserve(buf); + PRINT("%d: %s: sent %d bytes\n", sent++, + __func__, appdatalen); + } + } + + fiber_wakeup(receiver_id); + fiber_sleep(SLEEPTICKS); + + if (sent != received) { + failure = true; + } + + } + + if (failure) { + PRINT("ERROR TEST FAILED\n"); + } else { + PRINT("TEST PASSED\n"); + } +} + +void main(void) +{ /* Pretend to be ethernet with 6 byte mac */ uint8_t mac[] = { 0x0a, 0xbe, 0xef, 0x15, 0xf0, 0x0d }; - PRINT("%s: run net_loopback_test\n", __func__); + PRINT("%s: run network loopback test\n", __func__); + + sys_rand32_init(); net_init(); net_driver_loopback_init(); @@ -152,50 +196,11 @@ void main(void) net_set_mac(mac, sizeof(mac)); - ctx = net_context_get(IPPROTO_UDP, - &loopback_addr, 4242, - &any_addr, 0); - if (!ctx) { - PRINT("Cannot get network context\n"); - return; - } + receiver_id = task_fiber_start(&fiberReceiverStack[0], STACKSIZE, + (nano_fiber_entry_t)fiber_receiver, + 0, 0, 7, 0); - task_fiber_start (&fiberStack[0], STACKSIZE, - (nano_fiber_entry_t) fiberEntry, 0, 0, 7, 0); - - nano_sem_init(&nanoSemTask); - nano_timer_init(&timer, data); - - while (1) { - buf = ip_buf_get_tx(ctx); - if (buf) { - uint8_t *ptr; - uint16_t sent_len; - - ptr = net_buf_add(buf, 0); - memcpy(ptr, lorem_ipsum, len); - ptr = net_buf_add(buf, len); - ptr = net_buf_add(buf, 1); /* add \0 */ - *ptr = '\0'; - sent_len = buf->len; - - if (net_send(buf) < 0) { - PRINT("%s: sending %d bytes failed\n", - __func__, len); - ip_buf_unref(buf); - } else - PRINT("%s: sent %d bytes\n", __func__, - sent_len); - } - - /* wait a while, then let fiber have a turn */ - nano_task_timer_start(&timer, SLEEPTICKS); - nano_task_timer_test(&timer, TICKS_UNLIMITED); - nano_task_sem_give(&nanoSemFiber); - - /* now wait for fiber to let us have a turn */ - nano_task_sem_take(&nanoSemTask, TICKS_UNLIMITED); - } + sender_id = task_fiber_start(&fiberSenderStack[0], STACKSIZE, + (nano_fiber_entry_t)fiber_sender, + 0, 0, 7, 0); } - -#endif /* CONFIG_MICROKERNEL || CONFIG_NANOKERNEL */