Ethernet.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 defined DD360 ||defined DD360Audi || defined DD360ComBox
  92. if (pAlarmCode->AlarmEvents.bits.DisconnectedFromDo == NORMAL) {
  93. result = FAIL;
  94. } else {
  95. result = PASS;
  96. }
  97. return result;
  98. #endif //defined DD360 || defined DD360Audi
  99. memset(buf, 0x00, sizeof(buf));
  100. for (idx = 0; idx < ARRAY_SIZE(valid_Internet); idx++) {
  101. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  102. fp = popen(cmd, "r");
  103. if (fp != NULL) {
  104. while (fgets(buf, sizeof(buf), fp) != NULL) {
  105. if (strstr(buf, "transmitted") > 0) {
  106. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  107. if (strstr(buf, "100%") != NULL) {
  108. } else {
  109. result = PASS;
  110. }
  111. //DEBUG_INFO("%s",buf);
  112. //DEBUG_INFO("%s",tmp);
  113. }
  114. }
  115. }
  116. pclose(fp);
  117. }
  118. return result;
  119. }
  120. void InitEthernet(void)
  121. {
  122. char tmpbuf[256];
  123. bool ethResult = false;
  124. uint8_t cnt_pingDNS_Fail = 0;
  125. struct SysConfigData *pSysConfig = (struct SysConfigData *)GetShmSysConfigData();
  126. struct SysInfoData *pSysInfo = (struct SysInfoData *)GetShmSysInfoData();
  127. struct InfoCodeData *pInfoCode = (struct InfoCodeData *)GetShmInfoCodeData();
  128. system("ifconfig eth0 down");// eth0 down
  129. system("ifconfig eth1 down");// eth1 down
  130. sleep(2);
  131. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 down
  132. system("echo 1 > /sys/class/gpio/gpio110/value");//reset PHY
  133. sleep(2);
  134. //Init Eth0 for internet
  135. memset(tmpbuf, 0, 256);
  136. sprintf(tmpbuf, "/sbin/ifconfig eth0 %s netmask %s up",
  137. pSysConfig->Eth0Interface.EthIpAddress,
  138. pSysConfig->Eth0Interface.EthSubmaskAddress);
  139. //sprintf(tmpbuf,"/sbin/ifconfig eth0 192.168.100.10 netmask 255.255.255.0 up");
  140. system(tmpbuf);
  141. log_info("%s",tmpbuf);
  142. memset(tmpbuf, 0, 256);
  143. sprintf(tmpbuf, "route add default gw %s eth0 ",
  144. pSysConfig->Eth0Interface.EthGatewayAddress);
  145. //sprintf(tmpbuf,"route add default gw 192.168.100.1 eth0 ");
  146. system(tmpbuf);
  147. //system("ifconfig lo up");
  148. // /sbin/ifconfig eth0 192.168.1.10 netmask 255.255.255.0 up
  149. //Init Eth1 for administrator tool
  150. memset(tmpbuf, 0, 256);
  151. sprintf(tmpbuf, "/sbin/ifconfig eth1 %s netmask %s up",
  152. pSysConfig->Eth1Interface.EthIpAddress,
  153. pSysConfig->Eth1Interface.EthSubmaskAddress);
  154. log_info("%s",tmpbuf);
  155. system(tmpbuf);
  156. //system("/sbin/ifconfig lo up");
  157. //Run DHCP client if enabled
  158. system("killall udhcpc");
  159. system("rm -rf /etc/resolv.conf");
  160. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  161. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  162. //system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  163. if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
  164. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  165. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &",
  166. pSysConfig->SystemId);
  167. system(tmpbuf);
  168. }
  169. //Upgrade system id to /etc/hostname
  170. sprintf(tmpbuf, "echo %s > /etc/hostname", pSysConfig->SystemId);
  171. system(tmpbuf);
  172. pid_t pid = fork();
  173. if (pid == 0) {
  174. log_info("InitEthernet = %d", pid);
  175. for (;;) {
  176. #if defined DD360 || defined DD360Audi || defined DD360ComBox
  177. isReachableInternet();
  178. sleep(5);
  179. continue;
  180. #endif //!defined DD360 && !defined DD360
  181. if (isRouteFail()) {
  182. //log_info("eth0 not in route, restart eht0. ");
  183. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  184. if (pSysConfig->Eth0Interface.EthDhcpClient == 0) {
  185. InitialDHCP();
  186. }
  187. }
  188. if (isReachableInternet() == PASS) {
  189. pSysInfo->ethInternetConn = YES;
  190. cnt_pingDNS_Fail = 0;
  191. } else {
  192. if (++cnt_pingDNS_Fail > 3) {
  193. pSysInfo->ethInternetConn = NO;
  194. }
  195. }
  196. ethResult = pSysInfo->ethInternetConn;
  197. if (ethResult == YES) {
  198. system("/sbin/ifmetric eth0 0");
  199. if ((pSysConfig->ModelName[10] == 'W') ||
  200. (pSysConfig->ModelName[10] == 'D')) {
  201. system("/sbin/ifmetric mlan0 1");
  202. }
  203. if ((pSysConfig->ModelName[10] == 'T') ||
  204. (pSysConfig->ModelName[10] == 'D')) {
  205. system("/sbin/ifmetric ppp0 2");
  206. }
  207. }
  208. if (!ethResult &&
  209. pSysConfig->AthInterface.WifiMode != _SYS_WIFI_MODE_DISABLE &&
  210. (pSysConfig->ModelName[10] == 'W' ||
  211. pSysConfig->ModelName[10] == 'D')) {
  212. //ethResult = pSysConfig->AthInterface.WifiNetworkConn;
  213. ethResult = pInfoCode->InfoEvents.bits.InternetDisconnectViaWiFi == YES ? NO : YES;
  214. if (ethResult) {
  215. if ((pSysConfig->ModelName[10] == 'W') ||
  216. (pSysConfig->ModelName[10] == 'D')) {
  217. system("/sbin/ifmetric eth0 1");
  218. system("/sbin/ifmetric mlan0 0");
  219. }
  220. if ((pSysConfig->ModelName[10] == 'T') ||
  221. (pSysConfig->ModelName[10] == 'D')) {
  222. system("/sbin/ifmetric ppp0 2");
  223. }
  224. }
  225. }
  226. if (!ethResult &&
  227. pSysConfig->TelecomInterface.TelcomEnabled == YES &&
  228. (pSysConfig->ModelName[10] == 'T' ||
  229. pSysConfig->ModelName[10] == 'D')) {
  230. //ethResult = pSysConfig->TelecomInterface.TelcomNetworkConn;
  231. ethResult = pInfoCode->InfoEvents.bits.InternetDisconnectVia4Gi == YES ? NO : YES;
  232. if (ethResult) {
  233. if ((pSysConfig->ModelName[10] == 'W') ||
  234. (pSysConfig->ModelName[10] == 'D')) {
  235. system("/sbin/ifmetric mlan0 2");
  236. }
  237. if ((pSysConfig->ModelName[10] == 'T') ||
  238. (pSysConfig->ModelName[10] == 'D')) {
  239. system("/sbin/ifmetric eth0 1");
  240. system("/sbin/ifmetric ppp0 0");
  241. }
  242. }
  243. }
  244. pSysInfo->InternetConn = ethResult;
  245. sleep(5);
  246. }
  247. }
  248. log_info("Initial Ethernet OK");
  249. }