diff options
author | Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com> | 2017-01-27 11:31:51 +0100 |
---|---|---|
committer | Jukka Rissanen <jukka.rissanen@linux.intel.com> | 2017-02-03 15:59:14 +0200 |
commit | bc406fab08f95a2ca7721a37c23d7c8e6b0fa7bb (patch) | |
tree | 2b5f254431fbdfbfca7f241d81ad5a29682876d3 /samples | |
parent | d689c5ee97d700d04cd14a92d2d7bd607c9b4d0d (diff) |
samples/net: Add a simple telnet sample
It will setup IPv4/IPv6 addresses. Telnet is ran in the background,
along with net and kernel shell modules.
See the README.rst for more information how to start and use this
sample.
Change-Id: I3e935014c79b534aab43a6fa8256792b23abb38e
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
Diffstat (limited to 'samples')
-rw-r--r-- | samples/net/telnet/Makefile | 14 | ||||
-rw-r--r-- | samples/net/telnet/README.rst | 140 | ||||
-rw-r--r-- | samples/net/telnet/prj_frdm_k64f.conf | 44 | ||||
-rw-r--r-- | samples/net/telnet/prj_qemu_x86.conf | 43 | ||||
-rw-r--r-- | samples/net/telnet/prj_qemu_x86_iamcu.conf | 43 | ||||
-rw-r--r-- | samples/net/telnet/src/Makefile | 2 | ||||
-rw-r--r-- | samples/net/telnet/src/telnet.c | 150 | ||||
-rw-r--r-- | samples/net/telnet/testcase.ini | 5 |
8 files changed, 441 insertions, 0 deletions
diff --git a/samples/net/telnet/Makefile b/samples/net/telnet/Makefile new file mode 100644 index 000000000..e93c0f32f --- /dev/null +++ b/samples/net/telnet/Makefile @@ -0,0 +1,14 @@ +# Makefile - Telnet sample + +# +# Copyright (c) 2017 Intel Corporation +# +# SPDX-License-Identifier: Apache-2.0 +# + +BOARD ?= qemu_x86 +CONF_FILE ?= prj_$(BOARD).conf + +include $(ZEPHYR_BASE)/Makefile.inc + +include $(ZEPHYR_BASE)/samples/net/common/Makefile.ipstack diff --git a/samples/net/telnet/README.rst b/samples/net/telnet/README.rst new file mode 100644 index 000000000..11b1beee8 --- /dev/null +++ b/samples/net/telnet/README.rst @@ -0,0 +1,140 @@ +Sample TELNET console application +################################# + +Overview +******** + +This application will setup IPv4/IPv6 addresses on the default +network interface. The telnet console service is started transparently +by the kernel, along with the shell and two shell modules: net and kernel. +Once up and running, you can connect to the target over the network, +using a telnet client. + +Requirements +************ + +- net_tools: + + https://wiki.zephyrproject.org/view/Networking-with-Qemu + +Building and Running +******************** + +QEMU x86 +======== + +These are instructions for how to use this sample application using +QEMU on a Linux host connected to a network with DHCP service. + +Follow the net-tools readme from: + + Run 'loop_socat' and 'loop-slip-tap' scripts from net-tools. + + https://gerrit.zephyrproject.org/r/gitweb?p=net-tools.git;a=blob;f=README + +.. code-block:: console + + $ ./loop_socat.sh + +In another window: + +.. code-block:: console + + $ sudo ./loop-slip-tap.sh + +Run Zephyr samples/net/telnet application in QEMU: + +.. code-block:: console + + $ cd $ZEPHYR_BASE/samples/net/telnet + $ make pristine && make run + +Once started, you should see you IP address details for example: + +.. code-block:: console + + [Setup] [INF] main: Starting Telnet sample + [Setup] [INF] setup_ipv4: IPv4 address: 192.0.2.1 + [Setup] [INF] setup_ipv6: IPv6 address: 2001:db8::1 + +At this point, your QEMU guest is up and running. Connect to the telnet +console from your linux host this way: + +.. code-block:: console + + $ telnet 192.0.2.1 + Telnet escape character is '^]'. + Trying 192.0.2.1... + Connected to 192.0.2.1. + Escape character is '^]'. + +Now type enter, the shell prompt will appear and you can enter commands, +for example ``help``. + + +FRDM_K64F +========= + +These are instructions for how to use this sample application running on a +FRDM_K64F board. Unlike running it on QEMU, FRDM_K64F network configuration +for IPv4 will rely on DHCPv4. You cad modify the prj_frdm_k64f.conf to set +static IPv4 addresses if it is really needed. + +Instructions to follow building, flashing and observe serial console logs: + + https://wiki.zephyrproject.org/view/NXP_FRDM-K64F#Quick_Start + +Connect ethernet cable from FRDM_K64F board to a local network providing +IPv4 address configuration via DHCPv4. Creating your own dhcp server +on a local network is not in the scope of this README. + +Build Zephyr samples/net/telnet application: + +.. code-block:: console + + $ cd $ZEPHYR_BASE/samples/net/dhcpv4_client + $ make pristine && make BOARD=frdm_k64f + +Flash the resulting Zephyr binary following the FRDM-K64 board documentation +noted above. + +From your host computer, open a serial console to your board: + +.. code-block:: console + + $ sudo screen /dev/ttyACM0 115200 + +Plug the Ethernet cable to the FRDM_K64F board. +Reset the board, you should see first on the console: + +.. code-block:: console + + [dev/eth_mcux] [INF] eth_0_init: Enabled 100M full-duplex mode. + [dev/eth_mcux] [DBG] eth_0_init: MAC 00:04:9f:69:c7:36 + shell> [Setup] [INF] main: Starting Telnet sample + [Setup] [INF] setup_dhcpv4: Running dhcpv4 client... + [Setup] [INF] setup_ipv6: IPv6 address: 2001:db8::1 + +And if the DHCPv4 client succeeds, you will soon see something like: + +.. code-block:: console + + [Setup] [INF] ipv4_addr_add_handler: IPv4 address: 192.168.0.21 + [Setup] [INF] ipv4_addr_add_handler: Lease time: 86400 seconds + [Setup] [INF] ipv4_addr_add_handler: Subnet: 255.255.255.0 + [Setup] [INF] ipv4_addr_add_handler: Router: 192.168.0.1 + +The above result depends on your local network. +At this point you should be able to connect via telnet over the network. +On your linux host: + +.. code-block:: console + + $ telnet 192.168.0.21 + Telnet escape character is '^]'. + Trying 192.168.0.21... + Connected to 192.168.0.1. + Escape character is '^]'. + +You are now connected, and as for the UART console, you can type in +your commands and get the output through your telnet client. diff --git a/samples/net/telnet/prj_frdm_k64f.conf b/samples/net/telnet/prj_frdm_k64f.conf new file mode 100644 index 000000000..7c032fbaf --- /dev/null +++ b/samples/net/telnet/prj_frdm_k64f.conf @@ -0,0 +1,44 @@ +CONFIG_PRINTK=y + +CONFIG_NETWORKING=y +CONFIG_NET_IPV6=y +CONFIG_NET_IPV4=y +CONFIG_NET_ARP=y +CONFIG_NET_UDP=y +CONFIG_NET_TCP=y +CONFIG_NET_L2_ETHERNET=y + +CONFIG_NET_DHCPV4=y +CONFIG_NET_MGMT=y +CONFIG_NET_MGMT_EVENT=y + +CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 +CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2 +CONFIG_NET_MAX_CONTEXTS=10 + +CONFIG_NET_NBUF_RX_COUNT=10 +CONFIG_NET_NBUF_TX_COUNT=10 +CONFIG_NET_NBUF_DATA_COUNT=40 + +CONFIG_TEST_RANDOM_GENERATOR=y + +CONFIG_INIT_STACKS=y + +CONFIG_SYS_LOG=y +CONFIG_NET_BUF_LOG=n +CONFIG_SYS_LOG_NET_BUF_LEVEL=2 +CONFIG_NET_LOG=n +CONFIG_NET_LOG_GLOBAL=y +CONFIG_SYS_LOG_NET_LEVEL=2 +CONFIG_SYS_LOG_SHOW_COLOR=y + +CONFIG_SYS_LOG_ETHERNET_LEVEL=1 + +CONFIG_NET_SAMPLES_IP_ADDRESSES=y +CONFIG_NET_SAMPLES_MY_IPV6_ADDR="2001:db8::1" + +CONFIG_NET_SHELL=y +CONFIG_KERNEL_SHELL=y + +CONFIG_TELNET_CONSOLE=y +CONFIG_SYS_LOG_TELNET_CONSOLE_LEVEL=1
\ No newline at end of file diff --git a/samples/net/telnet/prj_qemu_x86.conf b/samples/net/telnet/prj_qemu_x86.conf new file mode 100644 index 000000000..9bd15bfc4 --- /dev/null +++ b/samples/net/telnet/prj_qemu_x86.conf @@ -0,0 +1,43 @@ +CONFIG_PRINTK=y + +CONFIG_NETWORKING=y +CONFIG_NET_IPV6=y +CONFIG_NET_IPV4=y +CONFIG_NET_ARP=y +CONFIG_NET_UDP=y +CONFIG_NET_TCP=y +CONFIG_NET_SLIP_TAP=y + +CONFIG_NET_DHCPV4=n +CONFIG_NET_MGMT=n +CONFIG_NET_MGMT_EVENT=n + +CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 +CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2 +CONFIG_NET_MAX_CONTEXTS=10 + +CONFIG_NET_NBUF_RX_COUNT=10 +CONFIG_NET_NBUF_TX_COUNT=10 +CONFIG_NET_NBUF_DATA_COUNT=40 + +CONFIG_TEST_RANDOM_GENERATOR=y + +CONFIG_INIT_STACKS=y + +CONFIG_SYS_LOG=y +CONFIG_NET_BUF_LOG=n +CONFIG_SYS_LOG_NET_BUF_LEVEL=2 +CONFIG_NET_LOG=n +CONFIG_NET_LOG_GLOBAL=y +CONFIG_SYS_LOG_NET_LEVEL=2 +CONFIG_SYS_LOG_SHOW_COLOR=y + +CONFIG_NET_SAMPLES_IP_ADDRESSES=y +CONFIG_NET_SAMPLES_MY_IPV6_ADDR="2001:db8::1" +CONFIG_NET_SAMPLES_MY_IPV4_ADDR="192.0.2.1" + +CONFIG_NET_SHELL=y +CONFIG_KERNEL_SHELL=y + +CONFIG_TELNET_CONSOLE=y +CONFIG_SYS_LOG_TELNET_CONSOLE_LEVEL=1 diff --git a/samples/net/telnet/prj_qemu_x86_iamcu.conf b/samples/net/telnet/prj_qemu_x86_iamcu.conf new file mode 100644 index 000000000..9bd15bfc4 --- /dev/null +++ b/samples/net/telnet/prj_qemu_x86_iamcu.conf @@ -0,0 +1,43 @@ +CONFIG_PRINTK=y + +CONFIG_NETWORKING=y +CONFIG_NET_IPV6=y +CONFIG_NET_IPV4=y +CONFIG_NET_ARP=y +CONFIG_NET_UDP=y +CONFIG_NET_TCP=y +CONFIG_NET_SLIP_TAP=y + +CONFIG_NET_DHCPV4=n +CONFIG_NET_MGMT=n +CONFIG_NET_MGMT_EVENT=n + +CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 +CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2 +CONFIG_NET_MAX_CONTEXTS=10 + +CONFIG_NET_NBUF_RX_COUNT=10 +CONFIG_NET_NBUF_TX_COUNT=10 +CONFIG_NET_NBUF_DATA_COUNT=40 + +CONFIG_TEST_RANDOM_GENERATOR=y + +CONFIG_INIT_STACKS=y + +CONFIG_SYS_LOG=y +CONFIG_NET_BUF_LOG=n +CONFIG_SYS_LOG_NET_BUF_LEVEL=2 +CONFIG_NET_LOG=n +CONFIG_NET_LOG_GLOBAL=y +CONFIG_SYS_LOG_NET_LEVEL=2 +CONFIG_SYS_LOG_SHOW_COLOR=y + +CONFIG_NET_SAMPLES_IP_ADDRESSES=y +CONFIG_NET_SAMPLES_MY_IPV6_ADDR="2001:db8::1" +CONFIG_NET_SAMPLES_MY_IPV4_ADDR="192.0.2.1" + +CONFIG_NET_SHELL=y +CONFIG_KERNEL_SHELL=y + +CONFIG_TELNET_CONSOLE=y +CONFIG_SYS_LOG_TELNET_CONSOLE_LEVEL=1 diff --git a/samples/net/telnet/src/Makefile b/samples/net/telnet/src/Makefile new file mode 100644 index 000000000..795ed41d2 --- /dev/null +++ b/samples/net/telnet/src/Makefile @@ -0,0 +1,2 @@ +ccflags-y += -I${ZEPHYR_BASE}/subsys/net/ip +obj-y = telnet.o diff --git a/samples/net/telnet/src/telnet.c b/samples/net/telnet/src/telnet.c new file mode 100644 index 000000000..f1f863b3a --- /dev/null +++ b/samples/net/telnet/src/telnet.c @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2017 Intel Corporation. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#if 1 +#define SYS_LOG_DOMAIN "Setup" +#define NET_SYS_LOG_LEVEL SYS_LOG_LEVEL_DEBUG +#define NET_LOG_ENABLED 1 +#endif + +#include <zephyr.h> +#include <sections.h> +#include <errno.h> +#include <stdio.h> + +#include <net/net_core.h> +#include <net/net_if.h> +#include <net/net_mgmt.h> + +#define STACKSIZE 2000 +char __noinit __stack thread_stack[STACKSIZE]; + +#if defined(CONFIG_NET_DHCPV4) +static struct net_mgmt_event_callback mgmt_cb; + +static void ipv4_addr_add_handler(struct net_mgmt_event_callback *cb, + uint32_t mgmt_event, + struct net_if *iface) +{ + char hr_addr[NET_IPV4_ADDR_LEN]; + int i = 0; + + for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) { + struct net_if_addr *if_addr = &iface->ipv4.unicast[i]; + + if (if_addr->addr_type != NET_ADDR_DHCP || !if_addr->is_used) { + continue; + } + + NET_INFO("IPv4 address: %s", + net_addr_ntop(AF_INET, &if_addr->address.in_addr, + hr_addr, NET_IPV4_ADDR_LEN)); + NET_INFO("Lease time: %u seconds", iface->dhcpv4.lease_time); + NET_INFO("Subnet: %s", + net_addr_ntop(AF_INET, &iface->ipv4.netmask, + hr_addr, NET_IPV4_ADDR_LEN)); + NET_INFO("Router: %s", + net_addr_ntop(AF_INET, &iface->ipv4.gw + hr_addr, NET_IPV4_ADDR_LEN)); + break; + } +} + +static void setup_dhcpv4(struct net_if *iface) +{ + NET_INFO("Running dhcpv4 client..."); + + net_mgmt_init_event_callback(&mgmt_cb, ipv4_addr_add_handler, + NET_EVENT_IPV4_ADDR_ADD); + net_mgmt_add_event_callback(&mgmt_cb); + + net_dhcpv4_start(iface); +} + +#else +#define setup_dhcpv4(...) +#endif /* CONFIG_NET_DHCPV4 */ + +#if defined(CONFIG_NET_IPV4) && !defined(CONFIG_NET_DHCPV4) + +#if !defined(CONFIG_NET_SAMPLES_MY_IPV4_ADDR) +#error "You need to define an IPv4 Address or enable DHCPv4!" +#endif + +static void setup_ipv4(struct net_if *iface) +{ + char hr_addr[NET_IPV4_ADDR_LEN]; + struct in_addr addr; + + if (net_addr_pton(AF_INET, CONFIG_NET_SAMPLES_MY_IPV4_ADDR, &addr)) { + NET_ERR("Invalid address: %s", CONFIG_NET_SAMPLES_MY_IPV4_ADDR); + return; + } + + net_if_ipv4_addr_add(iface, &addr, NET_ADDR_MANUAL, 0); + + NET_INFO("IPv4 address: %s", + net_addr_ntop(AF_INET, &addr, hr_addr, NET_IPV4_ADDR_LEN)); +} + +#else +#define setup_ipv4(...) +#endif /* CONFIG_NET_IPV4 && !CONFIG_NET_DHCPV4 */ + +#if defined(CONFIG_NET_IPV6) + +#define MCAST_IP6ADDR "ff84::2" + +#ifndef CONFIG_NET_SAMPLES_MY_IPV6_ADDR +#error "You need to define an IPv6 Address!" +#endif + +static void setup_ipv6(struct net_if *iface) +{ + char hr_addr[NET_IPV6_ADDR_LEN]; + struct in6_addr addr; + + if (net_addr_pton(AF_INET6, CONFIG_NET_SAMPLES_MY_IPV6_ADDR, &addr)) { + NET_ERR("Invalid address: %s", CONFIG_NET_SAMPLES_MY_IPV6_ADDR); + return; + } + + net_if_ipv6_addr_add(iface, &addr, NET_ADDR_MANUAL, 0); + + NET_INFO("IPv6 address: %s", + net_addr_ntop(AF_INET6, &addr, hr_addr, NET_IPV6_ADDR_LEN)); + + if (net_addr_pton(AF_INET6, MCAST_IP6ADDR, &addr)) { + NET_ERR("Invalid address: %s", MCAST_IP6ADDR); + return; + } + + net_if_ipv6_maddr_add(iface, &addr); +} + +#else +#define setup_ipv6(...) +#endif /* CONFIG_NET_IPV6 */ + +static void network_setup(void) +{ + struct net_if *iface = net_if_get_default(); + + setup_ipv4(iface); + + setup_dhcpv4(iface); + + setup_ipv6(iface); +} + +void main(void) +{ + NET_INFO("Starting Telnet sample"); + + k_thread_spawn(&thread_stack[0], STACKSIZE, + (k_thread_entry_t)network_setup, + NULL, NULL, NULL, K_PRIO_COOP(7), 0, 0); +} diff --git a/samples/net/telnet/testcase.ini b/samples/net/telnet/testcase.ini new file mode 100644 index 000000000..88377047b --- /dev/null +++ b/samples/net/telnet/testcase.ini @@ -0,0 +1,5 @@ +[test] +tags = net +build_only = true +arch_whitelist = x86 +platform_exclude = quark_d2000_crb # not enough RAM |