net/ip; add stub of a CLI for managing interfaces.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/a95f14fb Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/a95f14fb Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/a95f14fb Branch: refs/heads/master Commit: a95f14fba512766d70fde088b437cbeff5b2a45c Parents: c700fc7 Author: Marko Kiiskila <[email protected]> Authored: Fri Apr 14 12:55:02 2017 -0700 Committer: Marko Kiiskila <[email protected]> Committed: Fri Apr 14 13:21:30 2017 -0700 ---------------------------------------------------------------------- net/ip/src/ip_init.c | 5 ++ net/ip/src/ip_priv.h | 3 + net/ip/src/lwip_cli.c | 140 +++++++++++++++++++++++++++++++++++++++++++++ net/ip/syscfg.yml | 26 +++++++++ 4 files changed, 174 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a95f14fb/net/ip/src/ip_init.c ---------------------------------------------------------------------- diff --git a/net/ip/src/ip_init.c b/net/ip/src/ip_init.c index 7916d64..c73df02 100644 --- a/net/ip/src/ip_init.c +++ b/net/ip/src/ip_init.c @@ -16,6 +16,8 @@ * specific language governing permissions and limitations * under the License. */ +#include "syscfg/syscfg.h" + #include <lwip/tcpip.h> #include "ip_priv.h" @@ -25,6 +27,9 @@ int ip_init(void) if (lwip_socket_init()) { return -1; } +#if MYNEWT_VAL(LWIP_CLI) + lwip_cli_init(); +#endif tcpip_init(NULL, NULL); return 0; } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a95f14fb/net/ip/src/ip_priv.h ---------------------------------------------------------------------- diff --git a/net/ip/src/ip_priv.h b/net/ip/src/ip_priv.h index dd50c9f..5189801 100644 --- a/net/ip/src/ip_priv.h +++ b/net/ip/src/ip_priv.h @@ -30,6 +30,9 @@ int lwip_itf_getnext(struct mn_itf *mi); int lwip_itf_addr_getnext(struct mn_itf *mi, struct mn_itf_addr *mia); int lwip_socket_init(void); +void lwip_cli_init(void); + +int lwip_err_to_mn_err(int rc); #ifdef __cplusplus } http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a95f14fb/net/ip/src/lwip_cli.c ---------------------------------------------------------------------- diff --git a/net/ip/src/lwip_cli.c b/net/ip/src/lwip_cli.c new file mode 100644 index 0000000..adfa2ef --- /dev/null +++ b/net/ip/src/lwip_cli.c @@ -0,0 +1,140 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +#include <syscfg/syscfg.h> + +#if MYNEWT_VAL(LWIP_CLI) +#include <string.h> + +#include <mn_socket/mn_socket.h> +#include <console/console.h> +#include <shell/shell.h> + +#include <lwip/tcpip.h> +#include <lwip/dhcp.h> + +#include "ip_priv.h" + +static void +lwip_nif_print(struct mn_itf *itf) +{ + int rc; + int flags = 0; + struct mn_itf_addr itf_addr; + char addr_str[48]; + + console_printf("%d: %s %x(", itf->mif_idx, itf->mif_name, itf->mif_flags); + if (itf->mif_flags & MN_ITF_F_UP) { + flags = 1; + console_printf("up"); + } + if (itf->mif_flags & MN_ITF_F_MULTICAST) { + if (flags) { + console_printf("|"); + } + flags = 1; + console_printf("mcast"); + } + if (itf->mif_flags & MN_ITF_F_LINK) { + if (flags) { + console_printf("|"); + } + flags = 1; + console_printf("link"); + } + console_printf(")\n"); + + memset(&itf_addr, 0, sizeof(itf_addr)); + while (1) { + rc = mn_itf_addr_getnext(itf, &itf_addr); + if (rc) { + break; + } + mn_inet_ntop(itf_addr.mifa_family, &itf_addr.mifa_addr, + addr_str, sizeof(addr_str)); + console_printf(" %s/%d\n", addr_str, itf_addr.mifa_plen); + } +} + +int +lwip_nif_up(const char *name) +{ + struct netif *nif; + err_t err; + + nif = netif_find(name); + if (!nif) { + return MN_EINVAL; + } + if (nif->flags & NETIF_FLAG_LINK_UP) { + netif_set_up(nif); + netif_set_default(nif); +#if LWIP_IPV6 + nif->ip6_autoconfig_enabled = 1; + netif_create_ip6_linklocal_address(nif, 1); +#endif + err = dhcp_start(nif); + return lwip_err_to_mn_err(err); + } + return 0; +} + +static int +lwip_cli(int argc, char **argv) +{ + int rc; + struct mn_itf itf; + + if (argc == 1 || !strcmp(argv[1], "listif")) { + memset(&itf, 0, sizeof(itf)); + while (1) { + rc = mn_itf_getnext(&itf); + if (rc) { + break; + } + lwip_nif_print(&itf); + } + } else if (mn_itf_get(argv[1], &itf) == 0) { + if (argc == 2) { + lwip_nif_print(&itf); + return 0; + } + if (!strcmp(argv[2], "up")) { + rc = lwip_nif_up(argv[1]); + console_printf("lwip_nif_up() = %d\n", rc); + } else if (!strcmp(argv[2], "down")) { + } else { + console_printf("unknown cmd\n"); + } + } else { + console_printf("unknown cmd\n"); + } + return 0; +} + +struct shell_cmd lwip_cli_cmd = { + .sc_cmd = "ip", + .sc_cmd_func = lwip_cli +}; + +void +lwip_cli_init(void) +{ + shell_cmd_register(&lwip_cli_cmd); +} +#endif http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a95f14fb/net/ip/syscfg.yml ---------------------------------------------------------------------- diff --git a/net/ip/syscfg.yml b/net/ip/syscfg.yml new file mode 100644 index 0000000..7ad651e --- /dev/null +++ b/net/ip/syscfg.yml @@ -0,0 +1,26 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. +# + +# Package: net/ip + +syscfg.defs: + LWIP_CLI: + description: 'CLI for interacting with LwIP' + value: 1 + restrictions: + - SHELL_TASK
