Ethernet.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. void GetMacAddress(void)
  12. {
  13. uint8_t index = 0;
  14. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  15. for (index = 0; index < 2; index++) {
  16. int fd;
  17. struct ifreq ifr;
  18. char tarEth[5];
  19. char Mac[18];
  20. sprintf(tarEth, "eth%d", index);
  21. fd = socket(AF_INET, SOCK_DGRAM, 0);
  22. ifr.ifr_addr.sa_family = AF_INET;
  23. strncpy(ifr.ifr_name, tarEth, IFNAMSIZ - 1);
  24. ioctl(fd, SIOCGIFHWADDR, &ifr);
  25. close(fd);
  26. sprintf(Mac, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  27. ifr.ifr_hwaddr.sa_data[0],
  28. ifr.ifr_hwaddr.sa_data[1],
  29. ifr.ifr_hwaddr.sa_data[2],
  30. ifr.ifr_hwaddr.sa_data[3],
  31. ifr.ifr_hwaddr.sa_data[4],
  32. ifr.ifr_hwaddr.sa_data[5]
  33. );
  34. if (index == 0) {
  35. strcpy((char *) pSysConfig->Eth0Interface.EthMacAddress, Mac);
  36. } else {
  37. strcpy((char *) pSysConfig->Eth1Interface.EthMacAddress, Mac);
  38. }
  39. }
  40. }
  41. static int isRouteFail(void)
  42. {
  43. int result = YES;
  44. FILE *fp;
  45. char buf[512];
  46. fp = popen("route -n", "r");
  47. if (fp != NULL) {
  48. while (fgets(buf, sizeof(buf), fp) != NULL) {
  49. if (strstr(buf, "eth0") != NULL) {
  50. result = NO;
  51. }
  52. }
  53. }
  54. pclose(fp);
  55. return result;
  56. }
  57. static int isReachableInternet(void)
  58. {
  59. int result = FAIL;
  60. FILE *fp;
  61. char cmd[256];
  62. char buf[512];
  63. char tmp[512];
  64. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  65. struct AlarmCodeData *pAlarmCode = (struct AlarmCodeData *)GetShmAlarmCodeData();
  66. strcpy(cmd, "ifconfig eth0");
  67. fp = popen(cmd, "r");
  68. if (fp != NULL) {
  69. while (fgets(buf, sizeof(buf), fp) != NULL) {
  70. if (strstr(buf, "inet addr:") > 0) {
  71. sscanf(buf, "%*s%s", tmp);
  72. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf) - strspn(tmp, "addr:"));
  73. if (strcmp(tmp, (char *)pSysConfig->Eth0Interface.EthIpAddress) != EQUAL) {
  74. strcpy((char *) pSysConfig->Eth0Interface.EthIpAddress, tmp);
  75. }
  76. }
  77. }
  78. }
  79. pclose(fp);
  80. #if defined DD360 ||defined DD360Audi || defined DD360ComBox
  81. if (pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == NORMAL) {
  82. result = FAIL;
  83. } else {
  84. result = PASS;
  85. }
  86. #else
  87. memset(buf, 0x00, sizeof(buf));
  88. for (int idx = 0; idx < ARRAY_SIZE(valid_Internet); idx++) {
  89. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  90. fp = popen(cmd, "r");
  91. if (fp != NULL) {
  92. while (fgets(buf, sizeof(buf), fp) != NULL) {
  93. if (strstr(buf, "transmitted") > 0) {
  94. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  95. if (strstr(buf, "100%") != NULL) {
  96. } else {
  97. result = PASS;
  98. }
  99. //DEBUG_INFO("%s",buf);
  100. //DEBUG_INFO("%s\n",tmp);
  101. }
  102. }
  103. }
  104. pclose(fp);
  105. }
  106. #endif //defined DD360 || defined DD360Audi
  107. return result;
  108. }
  109. void InitEthernet(void)
  110. {
  111. char tmpbuf[256];
  112. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  113. bool ethResult = false;
  114. uint8_t cnt_pingDNS_Fail = 0;
  115. #endif //!defined DD360 && !defined DD360Audi
  116. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  117. system("ifconfig eth0 down");// eth0 down
  118. system("ifconfig eth1 down");// eth1 down
  119. sleep(2);
  120. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  121. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  122. sleep(2);
  123. //Init Eth0 for internet
  124. memset(tmpbuf, 0, 256);
  125. sprintf(tmpbuf, "/sbin/ifconfig eth0 %s netmask %s up",
  126. pSysConfig->Eth0Interface.EthIpAddress,
  127. pSysConfig->Eth0Interface.EthSubmaskAddress);
  128. //sprintf(tmpbuf,"/sbin/ifconfig eth0 192.168.100.10 netmask 255.255.255.0 up");
  129. system(tmpbuf);
  130. memset(tmpbuf, 0, 256);
  131. sprintf(tmpbuf, "route add default gw %s eth0 ",
  132. pSysConfig->Eth0Interface.EthGatewayAddress);
  133. //sprintf(tmpbuf,"route add default gw 192.168.100.1 eth0 ");
  134. system(tmpbuf);
  135. //system("ifconfig lo up");
  136. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  137. //Init Eth1 for administrator tool
  138. memset(tmpbuf, 0, 256);
  139. sprintf(tmpbuf, "/sbin/ifconfig eth1 %s netmask %s up",
  140. pSysConfig->Eth1Interface.EthIpAddress,
  141. pSysConfig->Eth1Interface.EthSubmaskAddress);
  142. system(tmpbuf);
  143. //Run DHCP client if enabled
  144. system("killall udhcpc");
  145. system("rm -rf /etc/resolv.conf");
  146. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  147. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  148. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  149. if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
  150. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  151. pSysConfig->SystemId);
  152. system(tmpbuf);
  153. }
  154. //Upgrade system id to /etc/hostname
  155. sprintf(tmpbuf, "echo %s > /etc/hostname", pSysConfig->SystemId);
  156. system(tmpbuf);
  157. pid_t pid = fork();
  158. if (pid == 0) {
  159. log_info("InitEthernet = %d\r\n", pid);
  160. for (;;) {
  161. #if !defined DD360 && !defined DD360Audi && !defined DD360ComBox
  162. if (isRouteFail()) {
  163. //log_info("eth0 not in route, restart eht0. \n");
  164. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  165. if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
  166. InitialDHCP();
  167. }
  168. }
  169. if (isReachableInternet() == PASS) {
  170. pSysInfo->ethInternetConn = YES;
  171. cnt_pingDNS_Fail = 0;
  172. } else {
  173. if (++cnt_pingDNS_Fail > 3) {
  174. pSysInfo->ethInternetConn = NO;
  175. }
  176. }
  177. ethResult = pSysInfo->ethInternetConn;
  178. if (ethResult == YES) {
  179. system("/sbin/ifmetric eth0 0");
  180. if ((pSysConfig->ModelName[10] == 'W') ||
  181. (pSysConfig->ModelName[10] == 'D')) {
  182. system("/sbin/ifmetric mlan0 1");
  183. }
  184. if ((pSysConfig->ModelName[10] == 'T') ||
  185. (pSysConfig->ModelName[10] == 'D')) {
  186. system("/sbin/ifmetric ppp0 2");
  187. }
  188. }
  189. if (!ethResult &&
  190. pSysConfig->AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  191. (pSysConfig->ModelName[10] == 'W' ||
  192. pSysConfig->ModelName[10] == 'D')) {
  193. //ethResult = pSysConfig->AthInterface.WifiNetworkConn;
  194. ethResult = pInfoCode->InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  195. if (ethResult) {
  196. if ((pSysConfig->ModelName[10] == 'W') ||
  197. (pSysConfig->ModelName[10] == 'D')) {
  198. system("/sbin/ifmetric eth0 1");
  199. system("/sbin/ifmetric mlan0 0");
  200. }
  201. if ((pSysConfig->ModelName[10] == 'T') ||
  202. (pSysConfig->ModelName[10] == 'D')) {
  203. system("/sbin/ifmetric ppp0 2");
  204. }
  205. }
  206. }
  207. if (!ethResult &&
  208. pSysConfig->TelecomInterface.TelcomEnabled == YES &&
  209. (pSysConfig->ModelName[10] == 'T' ||
  210. pSysConfig->ModelName[10] == 'D')) {
  211. //ethResult = pSysConfig->TelecomInterface.TelcomNetworkConn;
  212. ethResult = pInfoCode->InfoEvents.bits.InternetDisconnectVia4Gi == YES ? NO : YES;
  213. if (ethResult) {
  214. if ((pSysConfig->ModelName[10] == 'W') ||
  215. (pSysConfig->ModelName[10] == 'D')) {
  216. system("/sbin/ifmetric mlan0 2");
  217. }
  218. if ((pSysConfig->ModelName[10] == 'T') ||
  219. (pSysConfig->ModelName[10] == 'D')) {
  220. system("/sbin/ifmetric eth0 1");
  221. system("/sbin/ifmetric ppp0 0");
  222. }
  223. }
  224. }
  225. pSysInfo->InternetConn = ethResult;
  226. #else
  227. isReachableInternet();
  228. #endif //!defined DD360 && !defined DD360
  229. sleep(5);
  230. }
  231. }
  232. log_info("Initial Ethernet OK\r\n");
  233. }