66 * Change Logs:
77 * Date Author Notes
88 * 2019-12-13 qiyongzhong first version
9+ * 2023-06-03 CX support netstat command
910 */
1011
1112#include <stdio.h>
@@ -658,7 +659,81 @@ static int ec200x_netdev_ping(struct netdev *netdev, const char *host,
658659}
659660#endif /* NETDEV_USING_PING */
660661
662+ #ifdef NETDEV_USING_NETSTAT
663+ void ec200x_netdev_netstat (struct netdev * netdev )
664+ {
665+ #define EC200X_NETSTAT_RESP_SIZE 320
666+ #define EC200X_NETSTAT_TYPE_SIZE 4
667+ #define EC200X_NETSTAT_IPADDR_SIZE 17
668+ #define EC200X_NETSTAT_EXPRESSION "+QISTATE:%*d,\"%[^\"]\",\"%[^\"]\",%d,%*d,%d"
669+
670+ at_response_t resp = RT_NULL ;
671+ struct at_device * device = RT_NULL ;
672+ int remote_port , link_sta , i ;
673+ char * type = RT_NULL ;
674+ char * ipaddr = RT_NULL ;
675+
676+ device = at_device_get_by_name (AT_DEVICE_NAMETYPE_NETDEV , netdev -> name );
677+ if (device == RT_NULL )
678+ {
679+ LOG_E ("get device(%s) failed." , netdev -> name );
680+ return ;
681+ }
682+
683+ type = (char * ) rt_calloc (1 , EC200X_NETSTAT_TYPE_SIZE );
684+ ipaddr = (char * ) rt_calloc (1 , EC200X_NETSTAT_IPADDR_SIZE );
685+ if ((type && ipaddr ) == RT_NULL )
686+ {
687+ LOG_E ("no memory for ipaddr create." );
688+ goto __exit ;
689+ }
690+
691+ resp = at_create_resp (EC200X_NETSTAT_RESP_SIZE , 0 , 5 * RT_TICK_PER_SECOND );
692+ if (resp == RT_NULL )
693+ {
694+ LOG_E ("no memory for resp create." , device -> name );
695+ goto __exit ;
696+ }
697+
698+ /* send network connection information commond "AT+QISTATE?" and wait response */
699+ if (at_obj_exec_cmd (device -> client , resp , "AT+QISTATE?" ) < 0 )
700+ {
701+ goto __exit ;
702+ }
703+
704+ for (i = 1 ; i <= resp -> line_counts ; i ++ )
705+ {
706+ if (strstr (at_resp_get_line (resp , i ), "+QISTATE:" ))
707+ {
708+ /* parse the third line of response data, get the network connection information */
709+ if (at_resp_parse_line_args (resp , i , EC200X_NETSTAT_EXPRESSION , type , ipaddr , & remote_port , & link_sta ) < 0 )
710+ {
711+ goto __exit ;
712+ }
713+ else
714+ {
715+ /* link_sta==2?"LINK_INTNET_UP":"LINK_INTNET_DOWN" */
716+ LOG_RAW ("%s: %s ==> %s:%d\n" , type , inet_ntoa (netdev -> ip_addr ), ipaddr , remote_port );
717+ }
718+ }
719+ }
661720
721+ __exit :
722+ if (resp )
723+ {
724+ at_delete_resp (resp );
725+ }
726+
727+ if (type )
728+ {
729+ rt_free (type );
730+ }
731+
732+ if (ipaddr )
733+ {
734+ rt_free (ipaddr );
735+ }
736+ }
662737
663738const struct netdev_ops ec200x_netdev_ops =
664739{
@@ -672,7 +747,11 @@ const struct netdev_ops ec200x_netdev_ops =
672747#ifdef NETDEV_USING_PING
673748 ec200x_netdev_ping ,
674749#endif
675- RT_NULL ,
750+
751+ #ifdef NETDEV_USING_NETSTAT
752+ ec200x_netdev_netstat ,
753+ #endif
754+
676755};
677756
678757static struct netdev * ec200x_netdev_add (const char * netdev_name )
0 commit comments