• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Beken Corporation
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "bk_cli.h"
17 #include "wlan_defs_pub.h"
18 #include "bk_private/bk_wifi_wrapper.h"
19 #if CONFIG_LWIP
20 #if CONFIG_HARMONY_LWIP
21 //#include "ping.h"
22 #else
23 #include "lwip/ping.h"
24 #endif
25 
26 #endif
27 #include <components/netif.h>
28 #include "cli.h"
29 
30 extern void make_tcp_server_command(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv);
31 
32 #define CLI_DUMP_IP(_prompt, _ifx, _cfg) do {\
33 	CLI_LOGI("%s netif(%s) ip4=%s mask=%s gate=%s dns=%s\n", (_prompt),\
34 			(_ifx) == NETIF_IF_STA ? "sta" : "ap",\
35 			(_cfg)->ip, (_cfg)->mask, (_cfg)->gateway, (_cfg)->dns);\
36 } while(0)
37 
38 #if (CLI_CFG_NETIF == 1)
39 
ip_cmd_show_ip(int ifx)40 static void ip_cmd_show_ip(int ifx)
41 {
42 	netif_ip4_config_t config;
43 
44 	if (ifx == NETIF_IF_STA || ifx == NETIF_IF_AP) {
45 		BK_LOG_ON_ERR(bk_netif_get_ip4_config(ifx, &config));
46 		CLI_DUMP_IP(" ", ifx, &config);
47 	} else {
48 		BK_LOG_ON_ERR(bk_netif_get_ip4_config(NETIF_IF_STA, &config));
49 		CLI_DUMP_IP(" ", NETIF_IF_STA, &config);
50 		BK_LOG_ON_ERR(bk_netif_get_ip4_config(NETIF_IF_AP, &config));
51 		CLI_DUMP_IP(" ", NETIF_IF_AP, &config);
52 	}
53 }
54 
cli_ip_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)55 void cli_ip_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
56 {
57         netif_ip4_config_t config = {0};
58         int ifx = NETIF_IF_COUNT;
59 
60         if (argc > 1) {
61                 if (os_strcmp("sta", argv[1]) == 0) {
62                         ifx = NETIF_IF_STA;
63                 } else if (os_strcmp("ap", argv[1]) == 0) {
64                         ifx = NETIF_IF_AP;
65                 } else {
66                         CLI_LOGE("invalid netif name\n");
67                         return;
68                 }
69         }
70 
71         if (argc == 1) {
72                 ip_cmd_show_ip(NETIF_IF_COUNT);
73         } else if (argc == 2) {
74                 ip_cmd_show_ip(ifx);
75         } else if (argc == 6) {
76                 os_strncpy(config.ip, argv[2], NETIF_IP4_STR_LEN);
77                 os_strncpy(config.mask, argv[3], NETIF_IP4_STR_LEN);
78                 os_strncpy(config.gateway, argv[4], NETIF_IP4_STR_LEN);
79                 os_strncpy(config.dns, argv[5], NETIF_IP4_STR_LEN);
80                 BK_LOG_ON_ERR(bk_netif_set_ip4_config(ifx, &config));
81                 CLI_DUMP_IP("set static ip, ", ifx, &config);
82         } else {
83                 CLI_LOGE("usage: ip [sta|ap][{ip}{mask}{gate}{dns}]\n");
84         }
85 }
86 
87 #if CONFIG_IPV6
ip6_cmd_show_ip(int ifx)88 static void ip6_cmd_show_ip(int ifx)
89 {
90        if (ifx == NETIF_IF_STA || ifx == NETIF_IF_AP) {
91                bk_netif_get_ip6_addr_info(ifx);
92        } else {
93                CLI_LOGI("[sta]\n");
94                bk_netif_get_ip6_addr_info(NETIF_IF_STA);
95                CLI_LOGI("[ap]\n");
96                bk_netif_get_ip6_addr_info(NETIF_IF_AP);
97        }
98 }
99 
cli_ip6_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)100 void cli_ip6_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
101 
102 {
103        int ifx = NETIF_IF_COUNT;
104 
105        if (argc > 1) {
106                if (os_strcmp("sta", argv[1]) == 0) {
107                        ifx = NETIF_IF_STA;
108                } else if (os_strcmp("ap", argv[1]) == 0) {
109                        ifx = NETIF_IF_AP;
110                } else {
111                        CLI_LOGE("invalid netif name\n");
112                        return;
113                }
114        }
115 
116        if (argc == 1) {
117                ip6_cmd_show_ip(NETIF_IF_COUNT);
118        } else if (argc == 2) {
119                ip6_cmd_show_ip(ifx);
120        }
121 }
122 #endif
123 
cli_dhcpc_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)124 void cli_dhcpc_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
125 {
126 	BK_LOG_ON_ERR(bk_netif_dhcpc_start(NETIF_IF_STA));
127 	CLI_LOGI("STA start dhcp client\n");
128 }
129 
arp_Command(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)130 void arp_Command(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
131 {
132 	os_printf("arp_Command\r\n");
133 }
134 
cli_ping_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)135 void cli_ping_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
136 {
137 #if 0 //CONFIG_LWIP
138 	uint32_t cnt = 4;
139 	if (argc == 1) {
140 		os_printf("Please input: ping <host address>\n");
141 		return;
142 	}
143 	if (argc == 2 && (os_strcmp("--stop", argv[1]) == 0)) {
144 		ping_stop();
145 		return;
146 	}
147 	if (argc > 2)
148 		cnt = os_strtoul(argv[2], NULL, 10);
149 
150 	os_printf("ping IP address:%s\n", argv[1]);
151 	ping_start(argv[1], cnt, 0);
152 #endif
153 }
154 
155 #if CONFIG_ALI_MQTT
156 extern void test_mqtt_start(const char *host_name, const char *username,
157                      		const char *password, const char *topic);
cli_ali_mqtt_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)158 void cli_ali_mqtt_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
159 {
160 	os_printf("start test mqtt...\n");
161 	if (argc == 4) {
162 		test_mqtt_start(argv[1], argv[2], argv[3], NULL);
163 	} else if (argc == 5) {
164 		test_mqtt_start(argv[1], argv[2], argv[3], argv[4]);
165 	} else {
166 		// mqttali 222.71.10.2 aclsemi ****** /aclsemi/bk7256/cmd/1234
167 	    CLI_LOGE("usage: mqttali [host name|ip] [username] [password] [topic]\n");
168 	}
169 }
170 #endif
171 
172 #if CONFIG_HTTP
173 extern void LITE_openlog(const char *ident);
174 extern void LITE_closelog(void);
cli_http_debug_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)175 void cli_http_debug_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
176 {
177 	uint32 http_log = 0;
178 
179 	if (argc == 2) {
180 		http_log = os_strtoul(argv[1], NULL, 10);
181 		if (1 == http_log ) {
182 			LITE_openlog("http");
183 		} else {
184 			LITE_closelog();
185 		}
186 	} else {
187 		CLI_LOGE("usage: httplog [1|0].\n");
188 	}
189 }
190 #endif
191 
192 uint32_t g_per_packet_info_output_bitmap = 0;
193 
set_per_packet_info_output_bitmap(const char * bitmap)194 void set_per_packet_info_output_bitmap(const char *bitmap)
195 {
196     g_per_packet_info_output_bitmap = os_strtoul(bitmap, NULL, 16);
197     CLI_LOGI("set per_packet_info_output_bitmap:0x%x\n",g_per_packet_info_output_bitmap);
198 }
199 
cli_per_packet_info_output_cmd(char * pcWriteBuffer,int xWriteBufferLen,int argc,char ** argv)200 void cli_per_packet_info_output_cmd(char *pcWriteBuffer, int xWriteBufferLen, int argc, char **argv)
201 {
202 	if (argc == 2) {
203 		set_per_packet_info_output_bitmap(argv[1]);
204 	} else {
205 	    CLI_LOGE("usage: per_packet_info [per_packet_info_output_bitmap(base 16)]\n");
206 	}
207 }
208 
209 
210 #define NETIF_CMD_CNT (sizeof(s_netif_commands) / sizeof(struct cli_command))
211 static const struct cli_command s_netif_commands[] = {
212 	{"ip", "ip [sta|ap][{ip}{mask}{gate}{dns}]", cli_ip_cmd},
213 	{"dhcpc", "dhcpc", cli_dhcpc_cmd},
214 	{"ping", "ping <ip>", cli_ping_cmd},
215 #ifdef CONFIG_IPV6
216 	{"ping6",       "ping6 xxx",             cli_ping_cmd},
217 	{"ip6", "ip6 [sta|ap][{ip}{state}]", cli_ip6_cmd},
218 #endif
219 #ifdef TCP_CLIENT_DEMO
220 	{"tcp_cont", "tcp_cont [ip] [port]", tcp_make_connect_server_command},
221 #endif
222 
223 #if CONFIG_TCP_SERVER_TEST
224 	{"tcp_server", "tcp_server [ip] [port]", make_tcp_server_command },
225 #endif
226 #if CONFIG_ALI_MQTT
227 	{"mqttali", "paho mqtt test", cli_ali_mqtt_cmd},
228 #endif
229 #if CONFIG_OTA_HTTP
230 	{"httplog", "httplog [1|0].", cli_http_debug_cmd},
231 #endif
232     {"per_packet_info", "per_packet_info [per_packet_info_output_bitmap(base 16)]", cli_per_packet_info_output_cmd},
233 
234 };
235 
cli_netif_init(void)236 int cli_netif_init(void)
237 {
238 	return cli_register_commands(s_netif_commands, NETIF_CMD_CNT);
239 }
240 
241 #endif //#if (CLI_CFG_NETIF == 1)
242