Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
cmd_ifconfig.cpp
00001 /* 00002 * Copyright (c) 2018 ARM Limited. All rights reserved. 00003 * SPDX-License-Identifier: Apache-2.0 00004 * Licensed under the Apache License, Version 2.0 (the License); you may 00005 * not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an AS IS BASIS, WITHOUT 00012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 #include "NetworkStack.h" 00017 #include "NetworkInterface.h" 00018 00019 #include "mbed-client-cli/ns_cmdline.h" 00020 #include "mbed-trace/mbed_trace.h" 00021 00022 #include "ip4string.h" 00023 00024 #define WIFI 2 00025 #if !defined(MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE) || \ 00026 (MBED_CONF_TARGET_NETWORK_DEFAULT_INTERFACE_TYPE == WIFI && !defined(MBED_CONF_NSAPI_DEFAULT_WIFI_SSID)) 00027 #error [NOT_SUPPORTED] No network configuration found for this target. 00028 #endif 00029 00030 #include <string.h> 00031 00032 #define TRACE_GROUP "Aifc" 00033 00034 NetworkInterface *net; 00035 00036 NetworkInterface *get_interface(void) 00037 { 00038 return net; 00039 } 00040 00041 int cmd_ifup(int argc, char *argv[]); 00042 int cmd_ifdown(int argc, char *argv[]); 00043 int cmd_ifconfig(int argc, char *argv[]); 00044 00045 const char *MAN_IFCONFIG = " ifup interface up\r\n"\ 00046 " ifdown interface down\r\n"; 00047 00048 static void ifconfig_print() 00049 { 00050 if (!net) { 00051 cmd_printf("No interface configured\r\n"); 00052 return; 00053 } 00054 const char *str = net->get_ip_address(); 00055 if (str) { 00056 uint8_t buf[4]; 00057 if (stoip4(str, strlen(str), buf)) { 00058 cmd_printf("IPv4 if addr: %s\r\n", str); 00059 } else { 00060 cmd_printf("IPv6 if addr:\r\n [0]: %s\r\n", str); 00061 } 00062 } else { 00063 cmd_printf("No IP address\r\n"); 00064 } 00065 str = net->get_mac_address(); 00066 if (str) { 00067 cmd_printf("MAC-48: %s\r\n", str); 00068 } else { 00069 cmd_printf("MAC-48: unknown\r\n"); 00070 } 00071 } 00072 00073 00074 void cmd_ifconfig_init(void) 00075 { 00076 cmd_add("ifup", cmd_ifup, "ifconfig up", MAN_IFCONFIG); 00077 cmd_add("ifdown", cmd_ifdown, "ifconfig down", MAN_IFCONFIG); 00078 cmd_add("ifconfig", cmd_ifconfig, "ifconfig", MAN_IFCONFIG); 00079 } 00080 00081 int cmd_ifconfig(int argc, char *argv[]) 00082 { 00083 ifconfig_print(); 00084 return CMDLINE_RETCODE_SUCCESS; 00085 } 00086 00087 int cmd_ifup(int argc, char *argv[]) 00088 { 00089 if (!net) { 00090 net = NetworkInterface::get_default_instance(); 00091 } 00092 int err = net->connect(); 00093 if (err != NSAPI_ERROR_OK ) { 00094 return CMDLINE_RETCODE_FAIL; 00095 } 00096 00097 ifconfig_print(); 00098 return CMDLINE_RETCODE_SUCCESS; 00099 } 00100 00101 int cmd_ifdown(int argc, char *argv[]) 00102 { 00103 if (!net) { 00104 return CMDLINE_RETCODE_FAIL; 00105 } 00106 int err = net->disconnect(); 00107 if (err != NSAPI_ERROR_OK ) { 00108 return CMDLINE_RETCODE_FAIL; 00109 } 00110 00111 return CMDLINE_RETCODE_SUCCESS; 00112 } 00113 00114 00115 00116 const char *networkstack_error_to_str(int errorcode) 00117 { 00118 switch (errorcode) { 00119 case NSAPI_ERROR_OK : 00120 return "NSAPI_ERROR_OK"; 00121 case NSAPI_ERROR_WOULD_BLOCK : 00122 return "NSAPI_ERROR_WOULD_BLOCK"; 00123 case NSAPI_ERROR_UNSUPPORTED : 00124 return "NSAPI_ERROR_UNSUPPORTED"; 00125 case NSAPI_ERROR_PARAMETER : 00126 return "NSAPI_ERROR_PARAMETER"; 00127 case NSAPI_ERROR_NO_CONNECTION : 00128 return "NSAPI_ERROR_NO_CONNECTION"; 00129 case NSAPI_ERROR_NO_SOCKET : 00130 return "NSAPI_ERROR_NO_SOCKET"; 00131 case NSAPI_ERROR_NO_ADDRESS : 00132 return "NSAPI_ERROR_NO_ADDRESS"; 00133 case NSAPI_ERROR_NO_MEMORY : 00134 return "NSAPI_ERROR_NO_MEMORY"; 00135 case NSAPI_ERROR_NO_SSID : 00136 return "NSAPI_ERROR_NO_SSID"; 00137 case NSAPI_ERROR_DNS_FAILURE : 00138 return "NSAPI_ERROR_DNS_FAILURE"; 00139 case NSAPI_ERROR_DHCP_FAILURE : 00140 return "NSAPI_ERROR_DHCP_FAILURE"; 00141 case NSAPI_ERROR_AUTH_FAILURE : 00142 return "NSAPI_ERROR_AUTH_FAILURE"; 00143 case NSAPI_ERROR_DEVICE_ERROR : 00144 return "NSAPI_ERROR_DEVICE_ERROR"; 00145 case NSAPI_ERROR_IN_PROGRESS : 00146 return "NSAPI_ERROR_IN_PROGRESS"; 00147 case NSAPI_ERROR_ALREADY : 00148 return "NSAPI_ERROR_ALREADY"; 00149 case NSAPI_ERROR_IS_CONNECTED : 00150 return "NSAPI_ERROR_IS_CONNECTED"; 00151 case NSAPI_ERROR_CONNECTION_LOST : 00152 return "NSAPI_ERROR_CONNECTION_LOST"; 00153 case NSAPI_ERROR_CONNECTION_TIMEOUT : 00154 return "NSAPI_ERROR_CONNECTION_TIMEOUT"; 00155 default: 00156 return "unknown error code"; 00157 } 00158 }
Generated on Tue Aug 9 2022 00:37:04 by
1.7.2