zoukankan      html  css  js  c++  java
  • 嵌入式 hi3518平台检测网线是否插上

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1.   
    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. /********************************** (C) COPYRIGHT *******************************  
    2. * File Name          : linkstatus_check.c  
    3. * Author             : skdkjzz  
    4. * Date               : 2014/08/07  
    5. * Description        : 网线是否插上  
    6. *********************************************************************************/  
    7.   
    8.   
    9. #include <sys/types.h>    
    10. #include <string.h>    
    11. #include <stdlib.h>    
    12. #include <sys/types.h>    
    13. #include <sys/ioctl.h>    
    14. #include <sys/stat.h>    
    15. #include <stdio.h>    
    16. #include <string.h>    
    17. #include <errno.h>    
    18. #include <net/if.h>    
    19. #include <sys/utsname.h>    
    20. #include <limits.h>    
    21. #include <ctype.h>       
    22. #include <sys/socket.h>    
    23. #include <netinet/in.h>    
    24. #include <arpa/inet.h>       
    25. #include <linux/sockios.h>    
    26.     
    27. #define ETHTOOL_GLINK   0x0000000a   /* Get link status (ethtool_value) */    
    28. struct ethtool_value {    
    29.                       unsigned int   cmd;    
    30.                       unsigned int   data;     
    31.                      };      
    32.     
    33. int get_netlink_status(const char *if_name);    
    34.      
    35.   
    36.   
    37. /****************************************************************   
    38.    return value:    
    39.    -1 -- error , details can check errno    
    40.    1  -- interface link up    
    41.    0  -- interface link down.    
    42. ****************************************************************/  
    43. int get_netlink_status(const char *if_name)    
    44. {    
    45.     int skfd;    
    46.     struct ifreq ifr;    
    47.     struct ethtool_value edata;    
    48.     edata.cmd = ETHTOOL_GLINK;    
    49.     edata.data = 0;    
    50.     memset(&ifr, 0, sizeof(ifr));    
    51.     strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1);    
    52.     ifr.ifr_data = (char *)&edata;    
    53.     if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) == 0)    
    54.         return -1;    
    55.     if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1)    
    56.     {    
    57.        close(skfd);         
    58.        return -1;    
    59.     }    
    60.     close(skfd);    
    61.     return edata.data;    
    62. }    
    63.   
    64.   
    65. int main()    
    66. {    
    67.     char net_buf[10]="eth0";   
    68.     printf("Net link status: %s ", get_netlink_status(net_buf) == 1 ? "up" : "down");    
    69.     return 0;    
    70. }    
    71.    
    72.   
    73.   
    74.    </span>  
  • 相关阅读:
    小程序方法-小程序获取上一页的数据修改上一个页面的数据
    小程序方法-上传多上图片
    小程序方法-时间转换年月日,时间转换几天前几个小时前刚刚
    opencv函数学习:LUT()的使用
    opencv函数学习:cvtColor()的使用
    opencv函数学习:convertTo()的使用
    BITMAPFILEHEADER、BITMAPINFOHEADER及BMP结构详解
    单通道图和三通道图
    计算机存储单位与宽带单位
    大端模式和小端模式
  • 原文地址:https://www.cnblogs.com/lidabo/p/5383982.html
Copyright © 2011-2022 走看看