Module_Wifi.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h> /*標準輸入輸出定義*/
  18. #include <stdlib.h> /*標準函數庫定義*/
  19. #include <unistd.h> /*Unix 標準函數定義*/
  20. #include <fcntl.h> /*檔控制定義*/
  21. #include <termios.h> /*PPSIX 終端控制定義*/
  22. #include <errno.h> /*錯誤號定義*/
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. //#include "define.h"
  29. #include "../Projects/define.h"
  30. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  31. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  32. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  33. #define Debug
  34. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  35. #define PASS 1
  36. #define FAIL -1
  37. #define true 1
  38. #define false 0
  39. #define IP_TYPE_DHCP 0
  40. #define IP_TYPE_STATIC 1
  41. #define AUTH_TYPE_NONE 0
  42. #define AUTH_TYPE_WEP 1
  43. #define AUTH_TYPE_WPAPSK 2
  44. #define AUTH_TYPE_WPA2PSK 3
  45. #define WIFI_MODE_DISABLE 0
  46. #define WIFI_MODE_STA 1
  47. #define WIFI_MODE_AP 2
  48. #define WIFI_MODE_ADHOC 3
  49. #define STA_STATE_INIT 0
  50. #define STA_STATE_DEVICE_DETECT 1
  51. #define STA_STATE_AP_CONNECTED 2
  52. #define STA_STATE_INTERNET_CHECKING 3
  53. #define AP_STATE_INIT 0
  54. #define AP_STATE_DEVICE_DETECT 1
  55. #define AP_STATE_AP_START 2
  56. #define AP_STATE_SERVER_LISTEN 3
  57. #define UBLOX
  58. //#define MT7601U
  59. struct interface_info
  60. {
  61. int ipType;
  62. int authType;
  63. int rssi;
  64. int cnt_InternetFail;
  65. char *currentInterface;
  66. char currentSSID[256];
  67. char currentPasswd[256];
  68. char currentMAC[24];
  69. char currentIPAddr[24];
  70. char currentNetmask[24];
  71. char currentGateway[24];
  72. char staticIPAddr[24];
  73. }Wifi;
  74. struct auth_info
  75. {
  76. int auth_type;
  77. unsigned char ssid[256];
  78. unsigned char passwd[256];
  79. }Wifi_A;
  80. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  81. struct StatusCodeData *ShmStatusCodeData;
  82. int isFindInterface();
  83. int getInterfaceInfo();
  84. int isReachableInternet();
  85. int getLinkQuality();
  86. char* getTimeString(void);
  87. int setWPAconf();
  88. int restartWPA();
  89. void refreshUSB();
  90. void getParameters();
  91. int Wifi_module_sts = STA_STATE_INIT;
  92. int isSetWPA_OK = false;
  93. int cnt_pingDNS_Fail;
  94. int cnt_getAP_Fail;
  95. int cnt_getIP_Fail;
  96. void trim(char *s);
  97. int mystrcmp(char *p1,char *p2);
  98. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  99. void split(char **arr, char *str, const char *del);
  100. char *Support_InterfaceSTA[2] = {"mlan0", "wlan0"};
  101. char *Support_InterfaceAP[1] = {"uap0"};
  102. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  103. int StoreLogMsg(const char *fmt, ...)
  104. {
  105. char Buf[4096+256];
  106. char buffer[4096];
  107. time_t CurrentTime;
  108. struct tm *tm;
  109. va_list args;
  110. va_start(args, fmt);
  111. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  112. va_end(args);
  113. memset(Buf,0,sizeof(Buf));
  114. CurrentTime = time(NULL);
  115. tm=localtime(&CurrentTime);
  116. sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/%04d-%02d_%s_%s_SystemLog",
  117. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  118. buffer,
  119. tm->tm_year+1900,tm->tm_mon+1,
  120. ShmSysConfigAndInfo->SysConfig.ModelName,
  121. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  122. #ifdef SystemLogMessage
  123. system(Buf);
  124. #endif
  125. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
  126. return rc;
  127. }
  128. int DiffTimeb(struct timeb ST, struct timeb ET)
  129. {
  130. //return milli-second
  131. unsigned int StartTime,StopTime;
  132. StartTime=(unsigned int)ST.time;
  133. StopTime=(unsigned int)ET.time;
  134. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  135. }
  136. //==========================================
  137. // Init all share memory
  138. //==========================================
  139. int InitShareMemory()
  140. {
  141. int result = PASS;
  142. int MeterSMId;
  143. //creat ShmSysConfigAndInfo
  144. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  145. {
  146. #ifdef SystemLogMessage
  147. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  148. #endif
  149. result = FAIL;
  150. }
  151. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  152. {
  153. #ifdef SystemLogMessage
  154. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  155. #endif
  156. result = FAIL;
  157. }
  158. else
  159. {}
  160. //creat ShmStatusCodeData
  161. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  162. {
  163. #ifdef SystemLogMessage
  164. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  165. #endif
  166. result = FAIL;
  167. }
  168. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  169. {
  170. #ifdef SystemLogMessage
  171. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  172. #endif
  173. result = FAIL;
  174. }
  175. else
  176. {}
  177. return result;
  178. }
  179. //==========================================
  180. // Get parameters from shared memory
  181. //==========================================
  182. void getParameters(void)
  183. {
  184. strcpy((char*)Wifi_A.ssid, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  185. DEBUG_INFO("Wifi SSID: %s\n", Wifi_A.ssid);
  186. strcpy((char*)Wifi_A.passwd, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  187. DEBUG_INFO("Wifi Password: %s\n", Wifi_A.passwd);
  188. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress != NULL)
  189. strcpy((char*)Wifi.staticIPAddr, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  190. DEBUG_INFO("Wifi Static IP: %s\n", Wifi.staticIPAddr);
  191. Wifi.ipType = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient;
  192. DEBUG_INFO("DHCP client flag: %d\n", Wifi.ipType);
  193. }
  194. //==========================================
  195. // Refresh USB interface
  196. //==========================================
  197. void refreshUSB(void)
  198. {
  199. FILE *fp;
  200. char cmd[256];
  201. // Get IP address & net mask
  202. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/unbind");
  203. fp = popen(cmd, "r");
  204. sleep(2);
  205. pclose(fp);
  206. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/bind");
  207. fp = popen(cmd, "r");
  208. sleep(5);
  209. pclose(fp);
  210. }
  211. //==========================================
  212. // Check wifi interface status
  213. //==========================================
  214. int isFindInterface()
  215. {
  216. int result = FAIL;
  217. struct ifaddrs *ifaddr, *ifa;
  218. //refreshUSB();
  219. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == WIFI_MODE_AP)
  220. {
  221. if (getifaddrs(&ifaddr) != FAIL)
  222. {
  223. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  224. {
  225. if (ifa->ifa_addr == NULL ||
  226. ifa->ifa_addr->sa_family != AF_PACKET) continue;
  227. for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceAP);idx++)
  228. {
  229. if(mystrcmp(ifa->ifa_name, Support_InterfaceAP[idx]) == PASS)
  230. {
  231. Wifi.currentInterface = ifa->ifa_name;
  232. result = PASS;
  233. }
  234. }
  235. }
  236. freeifaddrs(ifaddr);
  237. freeifaddrs(ifa);
  238. }
  239. }
  240. else
  241. {
  242. if (getifaddrs(&ifaddr) != FAIL)
  243. {
  244. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  245. {
  246. if (ifa->ifa_addr == NULL ||
  247. ifa->ifa_addr->sa_family != AF_PACKET) continue;
  248. for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceSTA);idx++)
  249. {
  250. if(mystrcmp(ifa->ifa_name, Support_InterfaceSTA[idx]) == PASS)
  251. {
  252. Wifi.currentInterface = ifa->ifa_name;
  253. result = PASS;
  254. }
  255. }
  256. }
  257. freeifaddrs(ifaddr);
  258. freeifaddrs(ifa);
  259. }
  260. }
  261. return result;
  262. }
  263. //==========================================
  264. // Get wifi interface info
  265. //==========================================
  266. int getInterfaceInfo()
  267. {
  268. int result = PASS;
  269. FILE *fp;
  270. char cmd[256];
  271. char buf[512];
  272. char tmp[512];
  273. // Get IP address & net mask
  274. strcpy(cmd, "ifconfig ");
  275. strcat(cmd, Wifi.currentInterface);
  276. fp = popen(cmd, "r");
  277. if(fp == NULL)
  278. result = FAIL;
  279. else
  280. {
  281. while(fgets(buf, sizeof(buf), fp) != NULL)
  282. {
  283. if(strstr(buf, "HWaddr") > 0)
  284. {
  285. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  286. strcpy(Wifi.currentMAC, tmp);
  287. }
  288. if(strstr(buf, "inet addr:") > 0)
  289. {
  290. sscanf(buf, "%*s%s", tmp);
  291. substr(Wifi.currentIPAddr, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  292. sscanf(buf, "%*s%*s%*s%s", tmp);
  293. substr(Wifi.currentNetmask, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  294. }
  295. }
  296. }
  297. pclose(fp);
  298. // Get gateway
  299. fp = popen("ip route", "r");
  300. if(fp == NULL)
  301. result = FAIL;
  302. else
  303. {
  304. while(fgets(buf, sizeof(buf), fp) != NULL)
  305. {
  306. if(strncmp(buf, "default", strlen("default")) == 0)
  307. break;
  308. }
  309. sscanf(buf, "%*s%*s%s", tmp);
  310. substr(Wifi.currentGateway, tmp, 0, strlen(tmp));
  311. }
  312. pclose(fp);
  313. DEBUG_INFO("MAC: %s\n", Wifi.currentMAC);
  314. DEBUG_INFO("IP address: %s\n", Wifi.currentIPAddr);
  315. DEBUG_INFO("Net mask: %s\n", Wifi.currentNetmask);
  316. DEBUG_INFO("Default gateway: %s\n", Wifi.currentGateway);
  317. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, (const char*)Wifi.currentIPAddr);
  318. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, (const char*)Wifi.currentNetmask);
  319. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, (const char*)Wifi.currentGateway);
  320. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, (const char*)Wifi.currentMAC);
  321. return result;
  322. }
  323. //==========================================
  324. // Check if module connect to wifi AP
  325. //==========================================
  326. int isConnectAP()
  327. {
  328. int result = PASS;
  329. FILE *fp;
  330. char cmd[256];
  331. char buf[512];
  332. // Get IP address & net mask
  333. strcpy(cmd, "/root/iwconfig ");
  334. strcat(cmd, Wifi.currentInterface);
  335. fp = popen(cmd, "r");
  336. if(fp == NULL)
  337. result = FAIL;
  338. else
  339. {
  340. while(fgets(buf, sizeof(buf), fp) != NULL)
  341. {
  342. if(strstr(buf, "Not-Associated") > 0)
  343. {
  344. result = FAIL;
  345. DEBUG_INFO("AP check: Not-Associated.\n");
  346. }
  347. }
  348. }
  349. pclose(fp);
  350. return result;
  351. }
  352. //==========================================
  353. // Check internet access status
  354. //==========================================
  355. int isReachableInternet()
  356. {
  357. int result = FAIL;
  358. FILE *fp;
  359. char cmd[256];
  360. char buf[512];
  361. //char tmp[512];
  362. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  363. {
  364. strcpy(cmd, "ping -c 1 -w 3 ");
  365. strcat(cmd, valid_Internet[idx]);
  366. fp = popen(cmd, "r");
  367. if(fp != NULL)
  368. {
  369. while(fgets(buf, sizeof(buf), fp) != NULL)
  370. {
  371. if(strstr(buf, "transmitted") > 0)
  372. {
  373. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  374. if(strstr(buf,"100%") != NULL)
  375. {
  376. }
  377. else
  378. {
  379. result = PASS;
  380. }
  381. //DEBUG_INFO("%s",buf);
  382. //DEBUG_INFO("%s\n",tmp);
  383. }
  384. }
  385. }
  386. pclose(fp);
  387. }
  388. return result;
  389. }
  390. //==========================================
  391. // Check module soft AP is start up
  392. //==========================================
  393. int isStartUpAP()
  394. {
  395. int result = FAIL;
  396. FILE *fp;
  397. char cmd[256];
  398. char buf[512];
  399. // Get IP address & net mask
  400. strcpy(cmd, "/root/uaputl sys_cfg_bss_status");
  401. fp = popen(cmd, "r");
  402. if(fp != NULL)
  403. {
  404. while(fgets(buf, sizeof(buf), fp) != NULL)
  405. {
  406. if(strstr(buf, "BSS status = started") > 0)
  407. {
  408. result = PASS;
  409. DEBUG_INFO("AP already start up.\n");
  410. }
  411. }
  412. }
  413. pclose(fp);
  414. return result;
  415. }
  416. //==========================================
  417. // Get wifi connection quality
  418. //==========================================
  419. int getLinkQuality()
  420. {
  421. int result = FAIL;
  422. FILE *fp;
  423. char cmd[256];
  424. char buf[512];
  425. char tmp[512];
  426. char *q[2];
  427. strcpy(cmd, "/root/iwconfig ");
  428. strcat(cmd, Wifi.currentInterface);
  429. fp = popen(cmd, "r");
  430. if(fp == NULL)
  431. result = FAIL;
  432. else
  433. {
  434. while(fgets(buf, sizeof(buf), fp) != NULL)
  435. {
  436. if(strstr(buf, "Quality") > 0)
  437. {
  438. sscanf(buf, "%*s%s", tmp);
  439. substr(tmp, tmp, strspn(tmp, "Quality="), strlen(buf)-strspn(tmp, "Quality="));
  440. split(q,tmp,"/");
  441. Wifi.rssi = (atoi(q[0])*100)/atoi(q[1]);
  442. result = PASS;
  443. }
  444. }
  445. }
  446. pclose(fp);
  447. return result;
  448. }
  449. //==========================================
  450. // WPA parameter set
  451. //==========================================
  452. int setWPAconf()
  453. {
  454. int result = FAIL;
  455. char cmdBuf[512];
  456. FILE *pFile;
  457. char buffer[500]="ctrl_interface=/var/run/wpa_supplicant\nupdate_config=1\n";
  458. // Stop dhcp client or server
  459. sprintf(cmdBuf, "pgrep -f \"udhcpc\" | xargs kill");
  460. system(cmdBuf);
  461. sprintf(cmdBuf, "pgrep -f \"udhcpd\" | xargs kill");
  462. system(cmdBuf);
  463. // Clear wifi configuration file
  464. system("cat /dev/null > /etc/wpa.conf");
  465. switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
  466. {
  467. case WIFI_MODE_STA:
  468. // Fill out configuration value depend on share memory setting
  469. if(strlen((const char*)Wifi_A.passwd)>0)
  470. {
  471. if(strlen((const char*)Wifi_A.passwd)>=8)
  472. {
  473. strcat(buffer, "network={\n ssid=\"");
  474. strcat(buffer, (char*)Wifi_A.ssid);
  475. strcat(buffer, "\"\n key_mgmt=WPA-EAP WPA-PSK \n");
  476. strcat(buffer, " psk=\"");
  477. strcat(buffer, (char*)Wifi_A.passwd);
  478. strcat(buffer, "\"\n}\n\n");
  479. }
  480. if(strlen((const char*)Wifi_A.passwd)==5 || strlen((const char*)Wifi_A.passwd)==13)
  481. {
  482. strcat(buffer, "network={\n ssid=\"");
  483. strcat(buffer, (char*)Wifi_A.ssid);
  484. strcat(buffer, "\"\n key_mgmt=NONE\n");
  485. strcat(buffer, " wep_key0=\"");
  486. strcat(buffer, (char*)Wifi_A.passwd);
  487. strcat(buffer, "\"\n auth_alg=OPEN SHARED\n");
  488. strcat(buffer, "\n}\n\n");
  489. }
  490. strcat(buffer, "network={\n ssid=\"");
  491. strcat(buffer, (char*)Wifi_A.ssid);
  492. strcat(buffer, "\"\n key_mgmt=NONE");
  493. strcat(buffer, "\n}");
  494. }
  495. else
  496. {
  497. strcat(buffer, "network={\n ssid=\"");
  498. strcat(buffer, (char*)Wifi_A.ssid);
  499. strcat(buffer, "\"\n key_mgmt=NONE");
  500. strcat(buffer, "\n}");
  501. }
  502. // Configuration value write to /etc/wpa.conf
  503. pFile = fopen("/etc/wpa.conf","w");
  504. fwrite(buffer,strlen(buffer), 1, pFile);
  505. fclose(pFile);
  506. // Restart wpa_supplicant
  507. sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
  508. system(cmdBuf);
  509. sprintf(cmdBuf, "/root/wpa_supplicant -i %s -c /etc/wpa.conf -B", Wifi.currentInterface);
  510. system(cmdBuf);
  511. // Stop wifi AP
  512. sprintf(cmdBuf, "/root/uaputl bss_stop");
  513. system(cmdBuf);
  514. sleep(5);
  515. // Wifi IP set by DHCP client or static
  516. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
  517. {
  518. sprintf(cmdBuf, "/sbin/udhcpc -i %s -s /root/dhcp_script/wifi.script > /dev/null &", Wifi.currentInterface);
  519. system(cmdBuf);
  520. }
  521. else
  522. {
  523. strcpy(cmdBuf, "ifconfig ");
  524. strcat(cmdBuf, Wifi.currentInterface);
  525. strcat(cmdBuf, " ");
  526. strcat(cmdBuf, Wifi.staticIPAddr);
  527. system(cmdBuf);
  528. DEBUG_INFO("Setting %s static IP to %s ...\n", Wifi.currentInterface, Wifi.staticIPAddr);
  529. }
  530. break;
  531. case WIFI_MODE_AP:
  532. // Stop wifi AP
  533. sprintf(cmdBuf, "/root/uaputl bss_stop");
  534. system(cmdBuf);
  535. // Set SSID to uaputl.conf
  536. sprintf(cmdBuf, "sed -i 's/ SSID=.*$/ SSID=\"%s\"/' /root/uaputl_config/uaputl.conf", Wifi_A.ssid);
  537. system(cmdBuf);
  538. // Set AP configuration by uaputl.conf
  539. sprintf(cmdBuf, "/root/uaputl sys_config /root/uaputl_config/uaputl.conf");
  540. system(cmdBuf);
  541. // Start up wifi AP
  542. sprintf(cmdBuf, "/root/uaputl bss_start");
  543. system(cmdBuf);
  544. sleep(5);
  545. // Set wifi AP ip address
  546. sprintf(cmdBuf, "ifconfig %s 192.168.10.10", Wifi.currentInterface);
  547. system(cmdBuf);
  548. DEBUG_INFO("Setting %s static IP to 192.168.10.10...\n", Wifi.currentInterface);
  549. // Start DHCP server
  550. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpServer==0)
  551. {
  552. sprintf(cmdBuf, "/usr/sbin/udhcpd /etc/udhcpd.conf > /dev/null &");
  553. system(cmdBuf);
  554. }
  555. break;
  556. case WIFI_MODE_ADHOC:
  557. break;
  558. case WIFI_MODE_DISABLE:
  559. default:
  560. break;
  561. }
  562. result = PASS;
  563. return result;
  564. }
  565. //=================================
  566. // Common routine
  567. //=================================
  568. char* getTimeString(void)
  569. {
  570. char *result=malloc(21);
  571. time_t timep;
  572. struct tm *p;
  573. time(&timep);
  574. p=gmtime(&timep);
  575. sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
  576. return result;
  577. }
  578. void trim(char *s)
  579. {
  580. int i=0, j, k, l=0;
  581. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  582. i++;
  583. j = strlen(s)-1;
  584. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  585. j--;
  586. if(i==0 && j==strlen(s)-1) { }
  587. else if(i==0) s[j+1] = '\0';
  588. else {
  589. for(k=i; k<=j; k++) s[l++] = s[k];
  590. s[l] = '\0';
  591. }
  592. }
  593. int mystrcmp(char *p1,char *p2)
  594. {
  595. while(*p1==*p2)
  596. {
  597. if(*p1=='\0' || *p2=='\0')
  598. break;
  599. p1++;
  600. p2++;
  601. }
  602. if(*p1=='\0' && *p2=='\0')
  603. return(PASS);
  604. else
  605. return(FAIL);
  606. }
  607. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  608. {
  609. strncpy(dest, src + start, cnt);
  610. dest[cnt] = 0;
  611. }
  612. void split(char **arr, char *str, const char *del)
  613. {
  614. char *s = strtok(str, del);
  615. while(s != NULL)
  616. {
  617. *arr++ = s;
  618. s = strtok(NULL, del);
  619. }
  620. }
  621. int checkIP(void)
  622. {
  623. // DHCP or static setting
  624. int isGetIP = FAIL;
  625. FILE *fp;
  626. char buf[512];
  627. char tmp[512];
  628. char cmd[512];
  629. if(Wifi.ipType == IP_TYPE_DHCP) {
  630. // Get IP address & net mask
  631. strcpy(cmd, "ifconfig ");
  632. strcat(cmd, Wifi.currentInterface);
  633. fp = popen(cmd, "r");
  634. if(fp == NULL)
  635. isGetIP = FAIL;
  636. else
  637. {
  638. while(fgets(buf, sizeof(buf), fp) != NULL)
  639. {
  640. if(strstr(buf, "addr:") > 0)
  641. {
  642. if(strstr(buf, "192.168.1.10") > 0)
  643. {
  644. DEBUG_INFO("IP cannot be same as default value of Ethernet...\n");
  645. }
  646. else
  647. {
  648. isGetIP = PASS;
  649. cnt_getIP_Fail = 0;
  650. }
  651. sscanf(buf, "%*s%s", tmp);
  652. substr(Wifi.currentIPAddr, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  653. sscanf(buf, "%*s%*s%*s%s", tmp);
  654. substr(Wifi.currentNetmask, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  655. }else {
  656. }
  657. }
  658. }
  659. if(isGetIP == FAIL && cnt_getIP_Fail<3) {
  660. sprintf(cmd, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  661. system(cmd);
  662. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
  663. {
  664. sprintf(cmd, "/sbin/udhcpc -i %s -s /root/dhcp_script/wifi.script > /dev/null &", Wifi.currentInterface);
  665. system(cmd);
  666. }
  667. DEBUG_INFO("Sending DHCP request...\n");
  668. cnt_getIP_Fail++;
  669. sleep(5);
  670. }
  671. pclose(fp);
  672. }else {
  673. strcpy(cmd, "ifconfig ");
  674. strcat(cmd, Wifi.currentInterface);
  675. strcat(cmd, " ");
  676. strcat(cmd, Wifi.staticIPAddr);
  677. fp = popen(cmd, "r");
  678. DEBUG_INFO("Setting static IP to %s ...\n", Wifi.staticIPAddr);
  679. pclose(fp);
  680. isGetIP = PASS;
  681. }
  682. cnt_getIP_Fail = 0;
  683. return isGetIP;
  684. }
  685. int restartWPA(void)
  686. {
  687. int result = FAIL;
  688. FILE *fp;
  689. char buf[512];
  690. char cmd[50];
  691. // Get IP address & net mask
  692. strcpy(cmd, "wpa_cli -i ");
  693. strcat(cmd, Wifi.currentInterface);
  694. strcat(cmd, " reconfigure");
  695. fp = popen(cmd, "r");
  696. DEBUG_INFO("WPA reconfigure %s ...\n", Wifi.currentInterface);
  697. if(fp == NULL)
  698. result = FAIL;
  699. else
  700. {
  701. while(fgets(buf, sizeof(buf), fp) != NULL)
  702. {
  703. if(strstr(buf, "OK") > 0)
  704. {
  705. DEBUG_INFO("reconfigure OK.\n");
  706. result = PASS;
  707. }else {
  708. DEBUG_INFO("reconfigure failed.\n");
  709. result = FAIL;
  710. }
  711. }
  712. }
  713. pclose(fp);
  714. if(result == PASS) {
  715. strcpy(cmd, "wpa_cli -i ");
  716. strcat(cmd, Wifi.currentInterface);
  717. strcat(cmd, " reconfigure");
  718. fp = popen(cmd, "r");
  719. DEBUG_INFO("WPA reconnect %s ...\n", Wifi.currentInterface);
  720. if(fp == NULL)
  721. result = FAIL;
  722. else{
  723. while(fgets(buf, sizeof(buf), fp) != NULL)
  724. {
  725. if(strstr(buf, "OK") > 0){
  726. DEBUG_INFO("reconnect OK.\n");
  727. result = PASS;
  728. }else {
  729. DEBUG_INFO("reconnect failed.\n");
  730. result = FAIL;
  731. }
  732. }
  733. }
  734. pclose(fp);
  735. }
  736. return result;
  737. }
  738. void proc_sta()
  739. {
  740. switch(Wifi_module_sts)
  741. {
  742. case STA_STATE_INIT:
  743. // get info from shared memory
  744. getParameters();
  745. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  746. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  747. isSetWPA_OK = false;
  748. // check interface
  749. if((strlen((const char*)Wifi_A.ssid)>0) && (isFindInterface() == PASS))
  750. {
  751. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  752. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  753. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
  754. DEBUG_INFO("=================[State 1]===================\n");
  755. }
  756. else
  757. {
  758. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
  759. DEBUG_ERROR("Wifi support interface valid result: Fail\n");
  760. sleep(30);
  761. }
  762. break;
  763. case STA_STATE_DEVICE_DETECT:
  764. if(!isSetWPA_OK && (setWPAconf() == PASS))
  765. {
  766. isSetWPA_OK = true;
  767. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
  768. sleep(5);
  769. }
  770. else if((isSetWPA_OK == true) && (isConnectAP() == PASS))
  771. {
  772. Wifi_module_sts = STA_STATE_AP_CONNECTED;
  773. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  774. DEBUG_INFO("=================[State 2]===================\n");
  775. }
  776. else
  777. {
  778. cnt_getAP_Fail++;
  779. DEBUG_INFO("Fail to connect to the AP %d times...\n",cnt_getAP_Fail);
  780. if(cnt_getAP_Fail>=3)
  781. {
  782. Wifi_module_sts = STA_STATE_INIT;
  783. cnt_getAP_Fail = 0;
  784. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
  785. DEBUG_INFO("=================[State 0]===================\n");
  786. }
  787. else
  788. {
  789. sleep(10);
  790. }
  791. }
  792. break;
  793. case STA_STATE_AP_CONNECTED:
  794. getInterfaceInfo();
  795. getLinkQuality();
  796. DEBUG_INFO("Wifi quality: %d\n", Wifi.rssi);
  797. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = Wifi.rssi;
  798. if(isReachableInternet() == PASS)
  799. {
  800. DEBUG_INFO("Wifi internet valid result: Pass\n");
  801. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=0;
  802. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=1;
  803. cnt_pingDNS_Fail = 0;
  804. sleep(30);
  805. }
  806. else
  807. {
  808. cnt_pingDNS_Fail++;
  809. DEBUG_INFO("Wifi internet valid result: Fail %d time\n", cnt_pingDNS_Fail);
  810. sleep(5);
  811. }
  812. if(cnt_pingDNS_Fail >= 3)
  813. {
  814. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  815. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  816. cnt_pingDNS_Fail = 0;
  817. DEBUG_INFO("Ping DNS failed...");
  818. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  819. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  820. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
  821. if(isFindInterface() == PASS)
  822. {
  823. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  824. isSetWPA_OK = false;
  825. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  826. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  827. DEBUG_INFO("=================[State 1]===================\n");
  828. }
  829. else
  830. {
  831. DEBUG_INFO("Wifi support interface valid result: Fail\n");
  832. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  833. Wifi_module_sts = STA_STATE_INIT;
  834. DEBUG_INFO("=================[State 0]===================\n");
  835. }
  836. }
  837. break;
  838. default:
  839. Wifi_module_sts = STA_STATE_INIT;
  840. break;
  841. }
  842. }
  843. void proc_ap()
  844. {
  845. switch(Wifi_module_sts)
  846. {
  847. case AP_STATE_INIT:
  848. // get info from shared memory
  849. getParameters();
  850. isSetWPA_OK = false;
  851. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  852. // check interface
  853. if((strlen((const char*)Wifi_A.ssid)>0) && (isFindInterface() == PASS))
  854. {
  855. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  856. Wifi_module_sts = AP_STATE_DEVICE_DETECT;
  857. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
  858. DEBUG_INFO("=================[State 1]===================\n");
  859. }
  860. else
  861. {
  862. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
  863. DEBUG_ERROR("Wifi support interface valid result: Fail\n");
  864. sleep(30);
  865. }
  866. break;
  867. case AP_STATE_DEVICE_DETECT:
  868. if(!isSetWPA_OK && (setWPAconf() == PASS))
  869. {
  870. isSetWPA_OK = true;
  871. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
  872. sleep(5);
  873. }
  874. else if(isSetWPA_OK && (isStartUpAP() == PASS))
  875. {
  876. Wifi_module_sts = AP_STATE_AP_START;
  877. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  878. DEBUG_INFO("=================[State 2]===================\n");
  879. }
  880. else
  881. {
  882. cnt_getAP_Fail++;
  883. DEBUG_INFO("Fail to start up AP %d times...\n",cnt_getAP_Fail);
  884. if(cnt_getAP_Fail>=3)
  885. {
  886. Wifi_module_sts = AP_STATE_INIT;
  887. cnt_getAP_Fail = 0;
  888. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
  889. DEBUG_INFO("=================[State 0]===================\n");
  890. }
  891. else
  892. {
  893. sleep(10);
  894. }
  895. }
  896. break;
  897. case AP_STATE_AP_START:
  898. getInterfaceInfo();
  899. if(isStartUpAP() == PASS)
  900. {
  901. DEBUG_INFO("Wifi AP start up valid result: Pass\n");
  902. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  903. cnt_pingDNS_Fail = 0;
  904. sleep(30);
  905. }
  906. else
  907. {
  908. cnt_pingDNS_Fail++;
  909. DEBUG_INFO("Wifi AP start up valid result: Fail %d time\n", cnt_pingDNS_Fail);
  910. sleep(5);
  911. }
  912. if(cnt_pingDNS_Fail >= 3)
  913. {
  914. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  915. cnt_pingDNS_Fail = 0;
  916. DEBUG_INFO("Wifi AP start up status failed...");
  917. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  918. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  919. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
  920. if(isFindInterface() == PASS)
  921. {
  922. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  923. isSetWPA_OK = false;
  924. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  925. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  926. DEBUG_INFO("=================[State 1]===================\n");
  927. }
  928. else
  929. {
  930. DEBUG_INFO("Wifi support interface valid result: Fail\n");
  931. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  932. Wifi_module_sts = STA_STATE_INIT;
  933. DEBUG_INFO("=================[State 0]===================\n");
  934. }
  935. }
  936. break;
  937. default:
  938. Wifi_module_sts = AP_STATE_INIT;
  939. break;
  940. }
  941. }
  942. int main(void)
  943. {
  944. if(InitShareMemory() == FAIL)
  945. {
  946. #ifdef SystemLogMessage
  947. DEBUG_ERROR("InitShareMemory NG\n");
  948. #endif
  949. if(ShmStatusCodeData!=NULL)
  950. {
  951. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  952. }
  953. sleep(5);
  954. return 0;
  955. }
  956. //=============================================
  957. // Install WIFI module driver
  958. //=============================================
  959. #ifdef UBLOX
  960. system("insmod /lib/modules/mlan.ko");
  961. system("insmod /lib/modules/usb8801.ko");
  962. sleep(5);
  963. system("ifconfig mlan0 up");
  964. system("ifconfig uap0 up");
  965. #endif
  966. #ifdef MT7601U
  967. system("insmod /lib/modules/mt7601u.ko");
  968. sleep(5);
  969. system("ifconfig wlan0 up");
  970. #endif
  971. DEBUG_INFO("=================[State 0]===================\n");
  972. for(;;)
  973. {
  974. switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
  975. {
  976. case WIFI_MODE_STA:
  977. proc_sta();
  978. break;
  979. case WIFI_MODE_AP:
  980. proc_ap();
  981. break;
  982. case WIFI_MODE_ADHOC:
  983. break;
  984. case WIFI_MODE_DISABLE:
  985. default:
  986. break;
  987. }
  988. }
  989. return 0;
  990. }