/* check interface connected to internet or not
* if connected return 1, else return 0;
*/
/********************************************************************************************
@fn device_check_if_conn
@brief check the interface connected to internet or not
@author taoxp
@param interface name
@return 0 or 1, 1:interface connect to internet success 0:interface connect to internet fail
********************************************************************************************/
static uint8_t device_get_net_mode(){
pid_t status;
uint8_t ret=0, i=0;
char cmd[256];
char serverip[3][32]={"114.114.114.114","8.8.8.8","223.5.5.5"};
for(i=0;i<3;i++){
sprintf(cmd,"ping -I %s -w 1 -c 1 %s 1>/dev/null 2>&1", ifname, serverip[i]);
status=system(cmd);
if(status==-1){
GLOG_ERROR("run system command error");
}else{
if(WIFEXITED(status)){
if(0==WEXITSTATUS(status)){
GLOG_DEBUG("%s connect to internet ok", ifname);
ret=1;
break;
}else{
//GLOG_DEBUG("%s connect to internet fail", ifname);
}
}else{
GLOG_ERROR("exit Code status %d", WEXITSTATUS(status));
}
}
}
return ret;
#endif
}
/***************************************************************************************************
* @fn device_get_net_mode
*
* @brief get interface connected to internet
*
* @author taoxp
*
* @param none
*
* @return 0/1/3/7 0:eth, 1:wifi, 3:4g
****************************************************************************************************/
uint8_t device_get_net_mode(){
uint8_t netmode=ETH_MODE, status=0;
status=device_check_if_conn(GW_DEFAULT_ETH_NAME);
if(status==1){
netmode=ETH_MODE;
}
else{
status=device_check_if_conn(GW_DEFAULT_WIFI_NAME);
if(status==1){
netmode=WIFI_MODE;
}else{
status=device_check_if_conn(GW_DEFAULT_PPP_NAME);
if(status==1){
netmode=PPP_MODE;
}
}
}
return netmode;
}
C 检查网络连通性
最新推荐文章于 2024-07-11 18:56:56 发布