Ethernet.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. #include <stdio.h> /*標準輸入輸出定義*/
  2. #include <stdlib.h> /*標準函數庫定義*/
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include "../Config.h"
  6. #include "../Log/log.h"
  7. #include "../Define/define.h"
  8. #include "../ShareMemory/shmMem.h"
  9. #include "../common.h"
  10. //------------------------------------------------------------------------------
  11. static void InitialDHCP(void)
  12. {
  13. char tmpbuf[256] = {0};
  14. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  15. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  16. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  17. pSysConfig->SystemId);
  18. system(tmpbuf);
  19. }
  20. void GetMacAddress(void)
  21. {
  22. uint8_t index = 0;
  23. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  24. for (index = 0; index < 2; index++) {
  25. int fd;
  26. struct ifreq ifr;
  27. char tarEth[5];
  28. char Mac[18];
  29. sprintf(tarEth, "eth%d", index);
  30. fd = socket(AF_INET, SOCK_DGRAM, 0);
  31. ifr.ifr_addr.sa_family = AF_INET;
  32. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  33. ioctl(fd, SIOCGIFHWADDR, &ifr);
  34. close(fd);
  35. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  36. ifr.ifr_hwaddr.sa_data[0],
  37. ifr.ifr_hwaddr.sa_data[1],
  38. ifr.ifr_hwaddr.sa_data[2],
  39. ifr.ifr_hwaddr.sa_data[3],
  40. ifr.ifr_hwaddr.sa_data[4],
  41. ifr.ifr_hwaddr.sa_data[5]
  42. );
  43. if (index == 0) {
  44. strcpy((char *) pSysConfig->Eth0Interface.EthMacAddress, Mac);
  45. } else {
  46. strcpy((char *) pSysConfig->Eth1Interface.EthMacAddress, Mac);
  47. }
  48. }
  49. }
  50. static int isRouteFail(void)
  51. {
  52. int result = YES;
  53. FILE *fp;
  54. char buf[512];
  55. fp = popen("route -n", "r");
  56. if (fp != NULL) {
  57. while (fgets(buf, sizeof(buf), fp) != NULL) {
  58. if (strstr(buf, "eth0") != NULL) {
  59. result = NO;
  60. }
  61. }
  62. }
  63. pclose(fp);
  64. return result;
  65. }
  66. static int isReachableInternet(void)
  67. {
  68. int result = FAIL;
  69. int idx = 0;
  70. FILE *fp;
  71. char cmd[256] = {0};
  72. char buf[512] = {0};
  73. char tmp[512] = {0};
  74. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  75. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  76. struct AlarmCodeData *pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  77. strcpy(cmd, "ifconfig eth0");
  78. fp = popen(cmd, "r");
  79. if (fp != NULL) {
  80. while (fgets(buf, sizeof(buf), fp) != NULL) {
  81. if (strstr(buf, "inet addr:") > 0) {
  82. sscanf(buf, "%*s%s", tmp);
  83. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(tmp) - strspn(tmp, "addr:"));
  84. if (strcmp(tmp, (char *)pSysConfig->Eth0Interface.EthIpAddress) != EQUAL) {
  85. strcpy((char *) pSysConfig->Eth0Interface.EthIpAddress, tmp);
  86. }
  87. }
  88. }
  89. }
  90. pclose(fp);
  91. if (pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == NORMAL) {
  92. result = FAIL;
  93. } else {
  94. result = PASS;
  95. }
  96. return result;
  97. }
  98. void InitEthernet(void)
  99. {
  100. char tmpbuf[256];
  101. bool ethResult = false;
  102. uint8_t cnt_pingDNS_Fail = 0;
  103. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  104. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  105. struct InfoCodeData *pInfoCode = (struct InfoCodeData *)GetShmInfoCodeData();
  106. system("ifconfig eth0 down");// eth0 down
  107. system("ifconfig eth1 down");// eth1 down
  108. sleep(2);
  109. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  110. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  111. sleep(2);
  112. //Init Eth0 for internet
  113. memset(tmpbuf, 0, 256);
  114. sprintf(tmpbuf, "/sbin/ifconfig eth0 %s netmask %s up",
  115. pSysConfig->Eth0Interface.EthIpAddress,
  116. pSysConfig->Eth0Interface.EthSubmaskAddress);
  117. //sprintf(tmpbuf,"/sbin/ifconfig eth0 192.168.100.10 netmask 255.255.255.0 up");
  118. system(tmpbuf);
  119. log_info("%s",tmpbuf);
  120. memset(tmpbuf, 0, 256);
  121. sprintf(tmpbuf, "route add default gw %s eth0 ",
  122. pSysConfig->Eth0Interface.EthGatewayAddress);
  123. //sprintf(tmpbuf,"route add default gw 192.168.100.1 eth0 ");
  124. system(tmpbuf);
  125. //system("ifconfig lo up");
  126. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  127. //Init Eth1 for administrator tool
  128. memset(tmpbuf, 0, 256);
  129. sprintf(tmpbuf, "/sbin/ifconfig eth1 %s netmask %s up",
  130. pSysConfig->Eth1Interface.EthIpAddress,
  131. pSysConfig->Eth1Interface.EthSubmaskAddress);
  132. log_info("%s",tmpbuf);
  133. system(tmpbuf);
  134. system("/sbin/ifconfig lo up");
  135. //Run DHCP client if enabled
  136. system("killall udhcpc");
  137. system("rm -rf /etc/resolv.conf");
  138. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  139. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  140. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  141. if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
  142. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  143. pSysConfig->SystemId);
  144. system(tmpbuf);
  145. }
  146. //Upgrade system id to /etc/hostname
  147. sprintf(tmpbuf, "echo %s > /etc/hostname", pSysConfig->SystemId);
  148. system(tmpbuf);
  149. pid_t pid = fork();
  150. if (pid == 0) {
  151. //log_info("InitEthernet = %d", pid);
  152. for (;;) {
  153. isReachableInternet();
  154. sleep(5);
  155. continue;
  156. }
  157. }
  158. log_info("Initial Ethernet OK");
  159. }