Module_Wifi.c 29 KB

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