Module_Wifi.c 32 KB

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