Module_Wifi.c 29 KB

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