Module_Wifi.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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/simple.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/simple.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. cnt_pingDNS_Fail = 0;
  803. sleep(30);
  804. }
  805. else
  806. {
  807. cnt_pingDNS_Fail++;
  808. DEBUG_INFO("Wifi internet valid result: Fail %d time\n", cnt_pingDNS_Fail);
  809. sleep(5);
  810. }
  811. if(cnt_pingDNS_Fail >= 3)
  812. {
  813. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  814. cnt_pingDNS_Fail = 0;
  815. DEBUG_INFO("Ping DNS failed...");
  816. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  817. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  818. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
  819. if(isFindInterface() == PASS)
  820. {
  821. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  822. isSetWPA_OK = false;
  823. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  824. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  825. DEBUG_INFO("=================[State 1]===================\n");
  826. }
  827. else
  828. {
  829. DEBUG_INFO("Wifi support interface valid result: Fail\n");
  830. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  831. Wifi_module_sts = STA_STATE_INIT;
  832. DEBUG_INFO("=================[State 0]===================\n");
  833. }
  834. }
  835. break;
  836. default:
  837. Wifi_module_sts = STA_STATE_INIT;
  838. break;
  839. }
  840. }
  841. void proc_ap()
  842. {
  843. switch(Wifi_module_sts)
  844. {
  845. case AP_STATE_INIT:
  846. // get info from shared memory
  847. getParameters();
  848. isSetWPA_OK = false;
  849. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  850. // check interface
  851. if((strlen((const char*)Wifi_A.ssid)>0) && (isFindInterface() == PASS))
  852. {
  853. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  854. Wifi_module_sts = AP_STATE_DEVICE_DETECT;
  855. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
  856. DEBUG_INFO("=================[State 1]===================\n");
  857. }
  858. else
  859. {
  860. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
  861. DEBUG_ERROR("Wifi support interface valid result: Fail\n");
  862. sleep(30);
  863. }
  864. break;
  865. case AP_STATE_DEVICE_DETECT:
  866. if(!isSetWPA_OK && (setWPAconf() == PASS))
  867. {
  868. isSetWPA_OK = true;
  869. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
  870. sleep(5);
  871. }
  872. else if(isSetWPA_OK && (isStartUpAP() == PASS))
  873. {
  874. Wifi_module_sts = AP_STATE_AP_START;
  875. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  876. DEBUG_INFO("=================[State 2]===================\n");
  877. }
  878. else
  879. {
  880. cnt_getAP_Fail++;
  881. DEBUG_INFO("Fail to start up AP %d times...\n",cnt_getAP_Fail);
  882. if(cnt_getAP_Fail>=3)
  883. {
  884. Wifi_module_sts = AP_STATE_INIT;
  885. cnt_getAP_Fail = 0;
  886. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
  887. DEBUG_INFO("=================[State 0]===================\n");
  888. }
  889. else
  890. {
  891. sleep(10);
  892. }
  893. }
  894. break;
  895. case AP_STATE_AP_START:
  896. getInterfaceInfo();
  897. if(isStartUpAP() == PASS)
  898. {
  899. DEBUG_INFO("Wifi AP start up valid result: Pass\n");
  900. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  901. cnt_pingDNS_Fail = 0;
  902. sleep(30);
  903. }
  904. else
  905. {
  906. cnt_pingDNS_Fail++;
  907. DEBUG_INFO("Wifi AP start up valid result: Fail %d time\n", cnt_pingDNS_Fail);
  908. sleep(5);
  909. }
  910. if(cnt_pingDNS_Fail >= 3)
  911. {
  912. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  913. cnt_pingDNS_Fail = 0;
  914. DEBUG_INFO("Wifi AP start up status failed...");
  915. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  916. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  917. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
  918. if(isFindInterface() == PASS)
  919. {
  920. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  921. isSetWPA_OK = false;
  922. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  923. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  924. DEBUG_INFO("=================[State 1]===================\n");
  925. }
  926. else
  927. {
  928. DEBUG_INFO("Wifi support interface valid result: Fail\n");
  929. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  930. Wifi_module_sts = STA_STATE_INIT;
  931. DEBUG_INFO("=================[State 0]===================\n");
  932. }
  933. }
  934. break;
  935. default:
  936. Wifi_module_sts = AP_STATE_INIT;
  937. break;
  938. }
  939. }
  940. int main(void)
  941. {
  942. if(InitShareMemory() == FAIL)
  943. {
  944. #ifdef SystemLogMessage
  945. DEBUG_ERROR("InitShareMemory NG\n");
  946. #endif
  947. if(ShmStatusCodeData!=NULL)
  948. {
  949. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  950. }
  951. sleep(5);
  952. return 0;
  953. }
  954. //=============================================
  955. // Install WIFI module driver
  956. //=============================================
  957. #ifdef UBLOX
  958. system("insmod /lib/modules/mlan.ko");
  959. system("insmod /lib/modules/usb8801.ko");
  960. sleep(5);
  961. system("ifconfig mlan0 up");
  962. system("ifconfig uap0 up");
  963. #endif
  964. #ifdef MT7601U
  965. system("insmod /lib/modules/mt7601u.ko");
  966. sleep(5);
  967. system("ifconfig wlan0 up");
  968. #endif
  969. DEBUG_INFO("=================[State 0]===================\n");
  970. for(;;)
  971. {
  972. switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
  973. {
  974. case WIFI_MODE_STA:
  975. proc_sta();
  976. break;
  977. case WIFI_MODE_AP:
  978. proc_ap();
  979. break;
  980. case WIFI_MODE_ADHOC:
  981. break;
  982. case WIFI_MODE_DISABLE:
  983. default:
  984. break;
  985. }
  986. }
  987. return 0;
  988. }