Module_Wifi.c 34 KB

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