Ethernet Speed am trying to get from driver through ioctl is constant say 57872Mbps,why?
am trying to read the speed of ethernet that is up on my device though am connecting 100mbps cable its reading same constant value 57872 or some junk value or 0 always.... the program logic i made is as follows it wont consist all the code only bit part of it:
void get_link_config_res(void)
{
int i=0;
FILE *fp;
int ret_val;
char in_buf[200];
char in_buf1[64];
unsigned char interface_name[10];
int sock_id;
struct ifreq ir;
struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
struct sockaddr_in *nmsk = (void *) &ir.ifr_ifru.ifru_netmask;
//struct sockaddr_in *brdcast = (void *) &ir.ifr_ifru.ifru_broadaddr;
static int_net_cfg_t int_eth,int_wifi;
int_netcfg_array_t temp_netcfg,wifi_netcfg;
static __u8 mac_address[6];
char *temp_buf;
struct ethtool_cmd edata;
if(verify_host_link_status(ETH_0) == SET_FLAG)
{
/* get Ethernet link status */
memset(&ir, 0, sizeof(ir));
memset(in_buf, 0, sizeof(in_buf));
memset(in_buf1, 0, sizeof(in_buf1));
mem开发者_StackOverflow社区set(mac_address, 0, sizeof(mac_address));
memset(interface_name, 0, sizeof(interface_name));
strcpy(ir.ifr_name, ETHERNET);
/* socket creation (IPV4) */
sock_id = socket(AF_INET, SOCK_DGRAM, 0);
if(sock_id < 0)
{
SPX_DEBUG_3("cannot open socket \n");
}
strcpy(ir.ifr_ifrn.ifrn_name, ir.ifr_name);
ir.ifr_data = (char *)&edata;
//printf("Ethernet speed 1=",ir.ifr_data);
ret_val = ioctl(sock_id, SIOCGIFHWADDR, &ir);
//printf("retval of ioctl is = %d\n",ret_val);
memcpy(mac_address, &ir.ifr_ifru.ifru_hwaddr.sa_data, 6);
for(i=0; i<6; i++)
{
temp_netcfg.hwaddr[i] = mac_address[i];
}
mac2string(temp_netcfg.hwaddr, &in_buf1);
sprintf(in_buf,"echo mac_address=%s > %s", in_buf1, ETHERNET_PARAMS_FILE);
system(in_buf);
edata.cmd = ETHTOOL_GSET;
/*eth speed*/
ret_val = ioctl(sock_id, SIOCETHTOOL, &ir);
memset(in_buf, 0, sizeof(in_buf));
//printf("Ethernet speed=%d",edata.speed);
switch (edata.speed)
{
case SPEED_10:
sprintf(in_buf,"echo speed=10Mbps >> %s",ETHERNET_PARAMS_FILE);
system(in_buf);
//printf("10Mbps\n");
break;
case SPEED_100:
//printf("100Mbps\n");
sprintf(in_buf,"echo speed=100Mbps >> %s",ETHERNET_PARAMS_FILE);
system(in_buf);
break;
case SPEED_1000:
//printf("1Gbps\n");
sprintf(in_buf,"echo speed=1Gbps >> %s",ETHERNET_PARAMS_FILE);
system(in_buf);
break;
case SPEED_10000:
//printf("10Gbps\n");
sprintf(in_buf,"echo speed=10Gbps >> %s",ETHERNET_PARAMS_FILE);
system(in_buf);
break;
default:
//printf("Speed returned is ____%d\n", edata.speed);
sprintf(in_buf,"echo speed=%dMbps >> %s",edata.speed, ETHERNET_PARAMS_FILE);
system(in_buf);
break;
}
memset(in_buf, 0, sizeof(in_buf));
/* get the IP address */
ret_val = ioctl(sock_id, SIOCGIFADDR, &ir);
int_eth.ipaddr.s_addr = sin->sin_addr.s_addr;
/*To get Ip address in ASCII format*/
temp_buf = inet_ntoa(int_eth.ipaddr);
strcpy (in_buf, temp_buf);
sprintf(in_buf,"echo ip_address=%s >> %s",temp_buf, ETHERNET_PARAMS_FILE);
system(in_buf);
memset(in_buf, 0, sizeof(in_buf));
/*subnet mask*/
ret_val = ioctl(sock_id, SIOCGIFNETMASK, &ir);
int_eth.netmask.s_addr = nmsk->sin_addr.s_addr;
/*To get subnet mask in ASCII format*/
temp_buf = inet_ntoa(int_eth.netmask);
strcpy (in_buf, temp_buf);
sprintf(in_buf,"echo subnet_mask=%s >> %s",temp_buf, ETHERNET_PARAMS_FILE);
system(in_buf);
close(sock_id);
}
else
{
/*Host is not connected, to the Ethernet interface.*/
//printf("Ethernet Interface is not connected...\n");
memset(in_buf1, 0, sizeof(in_buf1));
strcpy(in_buf1, "NONE");
sprintf(in_buf,"echo mac_address=%s > %s", in_buf1, ETHERNET_PARAMS_FILE);
system(in_buf);
sprintf(in_buf,"echo speed=%s >> %s", in_buf1, ETHERNET_PARAMS_FILE);
system(in_buf);
sprintf(in_buf,"echo ip_address=%s >> %s",in_buf1, ETHERNET_PARAMS_FILE);
system(in_buf);
sprintf(in_buf,"echo subnet_mask=%s >> %s",in_buf1, ETHERNET_PARAMS_FILE);
system(in_buf);
//status = CLEAR_FLAG;
}
}
please help me out over this..... thanks in advance.....
ret_val = ioctl(sock_id, SIOCETHTOOL, &ir);
You're halfway there, at least. You appear to recognise the fact that the ioctl
may fail but you don't carry through and actually check the return value.
Given that your text indicates the value is always wrong (" 57872 or some junk value or 0 always"), I'd say it's a safe bet that you're getting back a consistent error code, such as EOPNOTSUPP
.
Modify the code to check that first, then run it again.
精彩评论