zoukankan      html  css  js  c++  java
  • 嵌入式 hi3518平台以太网网络模块设计包括重连机制和网线检测机制

    [html] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. <span style="font-family:Courier New;">  
    2. #include <sys/types.h>    
    3. #include <string.h>    
    4. #include <stdlib.h>    
    5. #include <sys/ioctl.h>    
    6. #include <sys/stat.h>    
    7. #include <stdio.h>    
    8. #include <string.h>    
    9. #include <errno.h>    
    10. #include <net/if.h>    
    11. #include <sys/utsname.h>    
    12. #include <limits.h>    
    13. #include <ctype.h>       
    14. #include <sys/socket.h>    
    15. #include <arpa/inet.h>       
    16. #include <linux/sockios.h>    
    17. #include <sys/socket.h>     
    18. #include <netinet/ip.h>     
    19. #include <netinet/ip_icmp.h>     
    20. #include <netdb.h>    
    21. #include <netinet/in.h>  
    22.   
    23. #define JOSEPH_LOOP_INTERFACE           "lo"  
    24. #define JOSEPH_ETH_INTERFACE            "eth0"  
    25. #define JOSEPH_WIRLESS_INTERFACE        "wlan0"  
    26.   
    27. #define ETHTOOL_GLINK                   0x0000000a   /* Get link status (ethtool_value) */    
    28. #define JSOEPH_NET_CHECK_PACKET_SIZE    4096     
    29. #define JOSEPH_NET_CHECK_TIME           3000  
    30.   
    31. typedef struct Joseph_Net_Interface_Info  
    32. {  
    33.     int net_device_type;// 0 ~ eth0 ; 1 ~ wlan ;3 ~ ppp0  
    34.     int net_device_priority;// 0 ~ eth0 ; 1 ~ wlan ;3 ~ ppp0  
    35.     int net_device_status;//0 ~ down; 1 ~ up  
    36.     int net_device_link_status;//0 ~ no ;1 ~ yes  
    37.     char net_device_name[8];  
    38.     char net_device_ip[16];  
    39.     char net_device_mac_info[32];  
    40.     char net_device_gw_info[16];  
    41.     char net_device_mask_info[16];  
    42.     char net_device_broadcast_info[16];  
    43.           
    44. }JOSEPH_NET_INTERFACE_INFO;  
    45.   
    46. typedef struct Joseph_Ethtool_Value {    
    47.     unsigned int   cmd;    
    48.     unsigned int   data;     
    49. }JOSEPH_ETHTOOL_VALUE;  
    50.   
    51. enum Joseph_Net_Device_Type  
    52. {  
    53.     JOSEPH_ETH = 0,  
    54.     JOSEPH_WIFI = 1,  
    55.     JOSEPH_3G = 2,  
    56. }JOSEPH_NET_DEVICE_TYPE;  
    57.   
    58. unsigned short Joseph_Cal_Chksum(unsigned short *addr, int len)   
    59. {    
    60.     int nleft=len;    
    61.     int sum=0;    
    62.     unsigned short *w=addr;    
    63.     unsigned short answer=0;      
    64.   
    65.     while(nleft > 1)   
    66.     {    
    67.       sum += *w++;    
    68.       nleft -= 2;    
    69.     }    
    70.   
    71.     if( nleft == 1)    
    72.     {         
    73.       *(unsigned char *)(&answer) = *(unsigned char *)w;    
    74.       sum += answer;    
    75.     }    
    76.   
    77.     sum = (sum >> 16) + (sum & 0xffff);    
    78.     sum += (sum >> 16);    
    79.     answer = ~sum;    
    80.   
    81.     return answer;    
    82. }    
    83.   
    84. int Joseph_Ping( char *ips,char *srcip , int timeout)  
    85. {        
    86.   
    87.     int n;  
    88.     pid_t pid;    
    89.     int maxfds = 0;    
    90.     fd_set readfds;    
    91.   
    92.     struct ip *iph;    
    93.     struct icmp *icmp;    
    94.     struct timeval *tval;          
    95.     struct sockaddr_in addr;        
    96.     struct sockaddr_in from;  
    97.     struct ifreq ifr;  
    98.       
    99.     bzero(&addr,sizeof(addr));  
    100.     addr.sin_family = AF_INET;    
    101.     addr.sin_addr.s_addr = inet_addr(ips);    
    102.   
    103.     int sockfd;    
    104.     sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);   
    105.     if (sockfd 0)     
    106.     {      
    107.       printf("ip:%s,socket error ",ips);      
    108.       return -1;      
    109.     }      
    110.   
    111.     struct timeval timeo;  
    112.     timeo.tv_sec = timeout / 1000;  
    113.     timeo.tv_usec = timeout % 1000;    
    114.   
    115. #if 0  
    116.   
    117.     /*set src ip*/  
    118.     bzero(&from,sizeof(from));  /* 设定Ip信息 */  
    119.     from.sin_family = AF_INET;    
    120.     from.sin_addr.s_addr = inet_addr(srcip);      
    121.   
    122.     if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_IF,(struct sockaddr *)&from, sizeof(from)) == -1)      
    123.     {      
    124.       printf("ip:%s,setsockopt error  ",srcip);       
    125.       return ERROR;  
    126.     }    
    127.     bind(sockfd,(struct sockaddr *)&addr, sizeof(addr));  
    128. #else  
    129.   
    130.     strcpy(ifr.ifr_name, srcip);  
    131.     if (setsockopt(sockfd, SOL_SOCKET, SO_BINDTODEVICE, &ifr, sizeof(ifr)) == -1)  
    132.     {  
    133.         printf("can't bind to interface %s ",ifr.ifr_name);  
    134.     }  
    135.       
    136. #endif  
    137.   
    138.     if (setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, &timeo, sizeof(timeo)) == -1)      
    139.     {      
    140.         printf("ip:%s,setsockopt error ",ips);    
    141.         return -1;      
    142.     }  
    143.     else  
    144.     {  
    145.         ;  
    146.     }  
    147.   
    148.     char sendpacket[JSOEPH_NET_CHECK_PACKET_SIZE];      
    149.     char recvpacket[JSOEPH_NET_CHECK_PACKET_SIZE];      
    150.   
    151.     memset(sendpacket, 0, sizeof(sendpacket));    
    152.   
    153.     pid = getpid();     
    154.   
    155.     icmp=(struct icmp*)sendpacket;   
    156.     icmp->icmp_type = ICMP_ECHO;  
    157.     icmp->icmp_code = 0;   
    158.     icmp->icmp_cksum = 0;       
    159.     icmp->icmp_seq = 0;      
    160.     icmp->icmp_id = pid;     
    161.     tval = (struct timeval *)icmp->icmp_data;        
    162.     gettimeofday(tval,NULL);        
    163.     icmp->icmp_cksum=Joseph_Cal_Chksum((unsigned short *)icmp,sizeof(struct icmp));  
    164.   
    165.     n = sendto(sockfd, (char *)&sendpacket, sizeof(struct icmp), 0, (struct sockaddr *)&addr, sizeof(addr));        
    166.     if (n 1)     
    167.     {      
    168.         printf("ip:%s,sendto error ",ips);    
    169.         return -1;      
    170.     }      
    171.          
    172.     while(1)   
    173.     {      
    174.       FD_ZERO(&readfds);  
    175.       FD_SET(sockfd, &readfds);      
    176.       maxfds = sockfd + 1;      
    177.       n = select(maxfds, &readfds, NULL, NULL, &timeo);      
    178.       if (n <= 0)       
    179.       {      
    180.           printf("ip:%s,Time out error ",ips);      
    181.           close(sockfd);      
    182.           return -1;      
    183.       }      
    184.           
    185.       memset(recvpacket, 0, sizeof(recvpacket));      
    186.       int fromlen = sizeof(from);  
    187.       n = recvfrom(sockfd, recvpacket, sizeof(recvpacket), 0, (struct sockaddr *)&from, (socklen_t *)&fromlen);      
    188.       if (n 1)  
    189.       {     
    190.           return -1;  
    191.       }      
    192.               
    193.       char *from_ip = (char *)inet_ntoa(from.sin_addr);        
    194.       if (strcmp(from_ip,ips) != 0)   
    195.       {      
    196.           printf("NowPingip:%s Fromip:%s NowPingip is not same to Fromip,so Joseph_Ping wrong! ",ips,from_ip);       
    197.           continue;    
    198.       }      
    199.           
    200.       iph = (struct ip *)recvpacket;          
    201.       icmp = (struct icmp *)(recvpacket + (iph->ip_hl <2));  
    202.   
    203.       if (icmp->icmp_type == ICMP_ECHOREPLY && icmp->icmp_id == pid)  
    204.           return 0;  
    205.       else         
    206.           continue;    
    207.     }   
    208.   
    209.     return 0;  
    210. }  
    211.     
    212. int Joseph_Check_Net_Status(char *if_name)    
    213. {    
    214.     int Qy_Ret = 0;  
    215.     if(strlen(if_name) <= 0)  
    216.     {  
    217.         Qy_Ret = -1;  
    218.         return Qy_Ret;  
    219.     }  
    220.       
    221.     char aPing[16]="202.108.22.5";  /* Joseph_Ping form ip  */  
    222.   
    223.     if(Joseph_Ping(aPing,if_name,JOSEPH_NET_CHECK_TIME) == 0)    
    224.     {    
    225.         printf("Network is Ok! ");   
    226.         Qy_Ret = 0;  
    227.     }    
    228.     else      
    229.     {    
    230.         printf("Network is Bad! ");  
    231.         Qy_Ret = -1;  
    232.     }  
    233.   
    234.     return Qy_Ret;  
    235. }  
    236.   
    237.   
    238. /*  
    239. Author : kj  
    240. Time : 2014-08-09  
    241. Function :  
    242.     get ip gw mac broadcast  
    243. */  
    244. int Joseph_Get_Net_Device_Info(JOSEPH_NET_INTERFACE_INFO *Joseph_Net_Interface_Info_In)  
    245. {  
    246.     int Qy_Ret = 0;  
    247.     int device_itertion = 0;  
    248.     int Joseph_Net_Interface_Sfd = 0;  
    249.     int Joseph_Net_Interface_exist = 0;  
    250.     struct ifreq Joseph_Net_Ifr;  
    251.     struct ifconf Joseph_Ifc;  
    252.     struct sockaddr_in *sin = (struct sockaddr_in*)&Joseph_Net_Ifr.ifr_addr;  
    253.     struct sockaddr_in *broadcast = (struct sockaddr_in*)&Joseph_Net_Ifr.ifr_broadaddr;  
    254.   
    255.     if(Joseph_Net_Interface_Info_In == NULL)  
    256.     {  
    257.         Qy_Ret = -1;  
    258.         return Qy_Ret;  
    259.     }  
    260.       
    261.     Joseph_Net_Interface_Sfd = socket(AF_INET,SOCK_DGRAM,0);  
    262.     if(Joseph_Net_Interface_Sfd 0){  
    263.         perror("socket error");  
    264.         return -1;  
    265.     }  
    266.       
    267.     memset(&Joseph_Net_Ifr,0,sizeof(Joseph_Net_Ifr));  
    268.   
    269.     Joseph_Ifc.ifc_len = sizeof(Joseph_Net_Ifr);  
    270.     Joseph_Ifc.ifc_buf = (caddr_t)&Joseph_Net_Ifr;  
    271.   
    272.               
    273.     for(device_itertion = 1;device_itertion 5;device_itertion++)  
    274.     {  
    275.         Joseph_Net_Ifr.ifr_ifindex = device_itertion;  
    276.         Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFNAME,&Joseph_Net_Ifr);  
    277.         if(Qy_Ret)  
    278.         {  
    279.             Joseph_Net_Interface_exist = 0;  
    280.         }  
    281.         else  
    282.         {  
    283.             if(strcmp(Joseph_Net_Ifr.ifr_name,Joseph_Net_Interface_Info_In->net_device_name) == 0)  
    284.             {  
    285.                 printf("The %dst net device is : %s ",Joseph_Net_Ifr.ifr_ifindex,Joseph_Net_Ifr.ifr_name);  
    286.                 Joseph_Net_Interface_exist = 1;  
    287.   
    288.                 /*Judge card type of net device*/  
    289.                 Qy_Ret = ioctl (Joseph_Net_Interface_Sfd, SIOCGIFFLAGS,&Joseph_Net_Ifr);  
    290.   
    291.                 if(!Qy_Ret)  
    292.                 {  
    293.                     /*judge the status of net device*/  
    294.                     if (Joseph_Net_Ifr.ifr_flags & IFF_UP)  
    295.                     {  
    296.                         puts("the interface status is UP");  
    297.                       
    298.                     }  
    299.                     else  
    300.                     {  
    301.                         puts("the interface status is DOWN");  
    302.                     }  
    303.                 }  
    304.                 break;  
    305.             }  
    306.         }  
    307.   
    308.     }  
    309.     if(Joseph_Net_Interface_exist == 0)  
    310.     {  
    311.         printf("%s:[%d] No Such Device of %s ! ",__FUNCTION__,__LINE__,Joseph_Net_Interface_Info_In->net_device_name);  
    312.         return -1;  
    313.     }  
    314.   
    315.   
    316.     /*get net device mac addr*/  
    317.     Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFHWADDR,&Joseph_Net_Ifr);  
    318.     if(!Qy_Ret)  
    319.     {  
    320.         sprintf(Joseph_Net_Interface_Info_In->net_device_mac_info,"%02x:%02x:%02x:%02x:%02x:%02x",  
    321.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[0],  
    322.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[1],  
    323.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[2],  
    324.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[3],  
    325.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[4],  
    326.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[5]);  
    327.           
    328.         printf("Mac address is : %02x:%02x:%02x:%02x:%02x:%02x ",  
    329.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[0],  
    330.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[1],  
    331.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[2],  
    332.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[3],  
    333.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[4],  
    334.             (unsigned char)Joseph_Net_Ifr.ifr_hwaddr.sa_data[5]);  
    335.   
    336.     }  
    337.     else  
    338.     {  
    339.         printf("Mac address is : 00:00:00:00:00:00 ");  
    340.     }  
    341.   
    342.     /*get net device ip */  
    343.     memset(Joseph_Net_Interface_Info_In->net_device_ip,0,16);  
    344.     Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFADDR,&Joseph_Net_Ifr);  
    345.     if(!Qy_Ret)  
    346.     {  
    347.         inet_ntop(AF_INET,&sin->sin_addr.s_addr,Joseph_Net_Interface_Info_In->net_device_ip,16);  
    348.         printf("IP address is : %s ",Joseph_Net_Interface_Info_In->net_device_ip);  
    349.     }else  
    350.     {  
    351.         printf("IP address is : 0.0.0.0 ");  
    352.     }  
    353.   
    354.     /*get broadcast addr*/  
    355.     memset(Joseph_Net_Interface_Info_In->net_device_broadcast_info,0,16);  
    356.     Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFBRDADDR,&Joseph_Net_Ifr);  
    357.     if(!Qy_Ret)  
    358.     {  
    359.         inet_ntop(AF_INET,&broadcast->sin_addr.s_addr,Joseph_Net_Interface_Info_In->net_device_broadcast_info,16);  
    360.         printf("BROADCAST IP is : %s ",Joseph_Net_Interface_Info_In->net_device_broadcast_info);  
    361.     }else  
    362.     {  
    363.         printf("BROADCAST IP is : 0.0.0.0 ");  
    364.     }  
    365.   
    366.     /*get mask info*/  
    367.     Qy_Ret = ioctl(Joseph_Net_Interface_Sfd,SIOCGIFNETMASK,&Joseph_Net_Ifr);  
    368.   
    369.     if (!Qy_Ret)      
    370.     {    
    371.         inet_ntop(AF_INET,&sin->sin_addr.s_addr,Joseph_Net_Interface_Info_In->net_device_mask_info,16);  
    372.         printf("NetMask is : %s ",Joseph_Net_Interface_Info_In->net_device_mask_info);   
    373.     }    
    374.   
    375.     return Qy_Ret;  
    376. }  
    377.   
    378. /****************************************************************   
    379.    return value:    
    380.    -1 -- error , details can check errno    
    381.    1  -- interface link up    
    382.    0  -- interface link down.    
    383. ****************************************************************/  
    384. int Joseph_Get_Netlink_Status(const char *if_name)    
    385. {    
    386.     int skfd;    
    387.     struct ifreq ifr;    
    388.     JOSEPH_ETHTOOL_VALUE edata;    
    389.     edata.cmd = ETHTOOL_GLINK;    
    390.     edata.data = 0;    
    391.     memset(&ifr, 0, sizeof(ifr));    
    392.     strncpy(ifr.ifr_name, if_name, sizeof(ifr.ifr_name) - 1);    
    393.     ifr.ifr_data = (char *)&edata;    
    394.     if ((skfd = socket(AF_INET, SOCK_DGRAM, 0)) == 0)    
    395.         return -1;    
    396.     if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1)    
    397.     {    
    398.        close(skfd);         
    399.        return -1;    
    400.     }    
    401.     close(skfd);    
    402.     return edata.data;    
    403. }    
    404.   
    405. int main(int argc,char *argv[])  
    406. {  
    407.     int Qy_Ret = 0;  
    408.   
    409.     JOSEPH_NET_INTERFACE_INFO Joseph_Net_Interface_Info;  
    410.     memset(&Joseph_Net_Interface_Info,0,sizeof(JOSEPH_NET_INTERFACE_INFO));  
    411.   
    412.     if(argc 2)  
    413.     {  
    414.         Qy_Ret = -1;  
    415.         return Qy_Ret;  
    416.     }  
    417.       
    418.     system("ifconfig eth0 up");  
    419.     sleep(3);  
    420.   
    421.     strcpy(Joseph_Net_Interface_Info.net_device_name,argv[1]);  
    422.   
    423.   
    424.     Qy_Ret = Joseph_Get_Netlink_Status(argv[1]);  
    425.     if(Qy_Ret == 1)  
    426.     {  
    427.         printf("%s:[%d] The netlink is up ! ",__FUNCTION__,__LINE__);  
    428.     }  
    429.     else  
    430.     {  
    431.         printf("%s:[%d] The netlink is down , Begin go to wifi ! ",__FUNCTION__,__LINE__);  
    432.         return -1;  
    433.     }  
    434.   
    435. NET_INIT:  
    436.   
    437.     /*after 5s , if no ip ,then killall udhcpc ,then go to wifi*/  
    438.     system("killall -9 udhcpc");  
    439.       
    440.     system("udhcpc -i eth0");  
    441.   
    442.     Qy_Ret = Joseph_Check_Net_Status(argv[1]);  
    443.     if(Qy_Ret == 0)  
    444.     {  
    445.         Joseph_Get_Net_Device_Info(&Joseph_Net_Interface_Info);       
    446.     }  
    447.   
    448. NET_RUN:  
    449.     while(1)  
    450.     {  
    451.         Qy_Ret = Joseph_Get_Netlink_Status(argv[1]);  
    452.         if(Qy_Ret == 1)  
    453.         {  
    454.             printf("Net link status: %s ", Qy_Ret == 1 ? "up" : "down");  
    455.             Qy_Ret = Joseph_Check_Net_Status(argv[1]);  
    456.             if(Qy_Ret 0)  
    457.             {  
    458.                 break;//do nothing  
    459.             }  
    460.               
    461.         }  
    462.         else  
    463.         {  
    464.             printf("%s:[%d] The netlink is down ! ",__FUNCTION__,__LINE__);      
    465.         }  
    466.         sleep(1);  
    467.     }  
    468.       
    469.     goto NET_INIT;  
    470.   
    471.     return Qy_Ret;  
    472. }  
    473.   
    474. </span>  
  • 相关阅读:
    python,selenium遇到的问题
    python环境配置
    性能测试函数
    性能测试的关注点
    环境配置
    性能监控工具使用
    linux路径
    linux权限
    自动化测试工具
    书籍
  • 原文地址:https://www.cnblogs.com/lidabo/p/5383959.html
Copyright © 2011-2022 走看看