123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158 |
- /*
- * Module_Wifi.c
- *
- * Created on: 2020-01-14
- * Author: Jerry Wang
- * Version: D0.01
- */
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/ioctl.h>
- #include <sys/socket.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/shm.h>
- #include <sys/mman.h>
- #include <linux/wireless.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <stdio.h> /*標準輸入輸出定義*/
- #include <stdlib.h> /*標準函數庫定義*/
- #include <unistd.h> /*Unix 標準函數定義*/
- #include <fcntl.h> /*檔控制定義*/
- #include <termios.h> /*PPSIX 終端控制定義*/
- #include <errno.h> /*錯誤號定義*/
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #include <ifaddrs.h>
- //#include "define.h"
- #include "../Projects/define.h"
- #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
- #define Debug
- #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
- #define PASS 1
- #define FAIL -1
- #define true 1
- #define false 0
- #define IP_TYPE_DHCP 0
- #define IP_TYPE_STATIC 1
- #define AUTH_TYPE_NONE 0
- #define AUTH_TYPE_WEP 1
- #define AUTH_TYPE_WPAPSK 2
- #define AUTH_TYPE_WPA2PSK 3
- #define WIFI_MODE_DISABLE 0
- #define WIFI_MODE_STA 1
- #define WIFI_MODE_AP 2
- #define WIFI_MODE_ADHOC 3
- #define STA_STATE_INIT 0
- #define STA_STATE_DEVICE_DETECT 1
- #define STA_STATE_AP_CONNECTED 2
- #define STA_STATE_INTERNET_CHECKING 3
- #define AP_STATE_INIT 0
- #define AP_STATE_DEVICE_DETECT 1
- #define AP_STATE_AP_START 2
- #define AP_STATE_SERVER_LISTEN 3
- #define UBLOX
- //#define MT7601U
- struct interface_info
- {
- int ipType;
- int authType;
- int rssi;
- int cnt_InternetFail;
- char *currentInterface;
- char currentSSID[256];
- char currentPasswd[256];
- char currentMAC[24];
- char currentIPAddr[24];
- char currentNetmask[24];
- char currentGateway[24];
- char staticIPAddr[24];
- }Wifi;
- struct Display_Request
- {
- unsigned char isShowed_IF_info:1;
- unsigned char isShowed_IP_info:1;
- }dispReq;
- struct auth_info
- {
- int auth_type;
- unsigned char ssid[256];
- unsigned char passwd[256];
- }Wifi_A;
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- int isFindInterface();
- int getInterfaceInfo();
- int isReachableInternet();
- int getLinkQuality();
- char* getTimeString(void);
- int setWPAconf();
- int restartWPA();
- void refreshUSB();
- void getParameters();
- int Wifi_module_sts = STA_STATE_INIT;
- int isSetWPA_OK = false;
- int cnt_pingDNS_Fail;
- int cnt_getAP_Fail;
- int cnt_getIP_Fail;
- void trim(char *s);
- int mystrcmp(char *p1,char *p2);
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
- void split(char **arr, char *str, const char *del);
- char *Support_InterfaceSTA[2] = {"mlan0", "wlan0"};
- char *Support_InterfaceAP[1] = {"uap0"};
- char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
- int StoreLogMsg(const char *fmt, ...)
- {
- char Buf[4096+256];
- char buffer[4096];
- time_t CurrentTime;
- struct tm *tm;
- va_list args;
- va_start(args, fmt);
- int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
- va_end(args);
- memset(Buf,0,sizeof(Buf));
- CurrentTime = time(NULL);
- tm=localtime(&CurrentTime);
- sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]Wifi_SystemLog",
- tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
- buffer,
- tm->tm_year+1900,tm->tm_mon+1);
- #ifdef SystemLogMessage
- system(Buf);
- #endif
- 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);
- return rc;
- }
- int DiffTimeb(struct timeb ST, struct timeb ET)
- {
- //return milli-second
- unsigned int StartTime,StopTime;
- StartTime=(unsigned int)ST.time;
- StopTime=(unsigned int)ET.time;
- return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
- }
- //==========================================
- // Init all share memory
- //==========================================
- int InitShareMemory()
- {
- int result = PASS;
- int MeterSMId;
- //creat ShmSysConfigAndInfo
- if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
- #endif
- result = FAIL;
- }
- else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
- #endif
- result = FAIL;
- }
- else
- {}
- //creat ShmStatusCodeData
- if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
- #endif
- result = FAIL;
- }
- else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
- #endif
- result = FAIL;
- }
- else
- {}
- return result;
- }
- //==========================================
- // Get parameters from shared memory
- //==========================================
- void getParameters(void)
- {
- DEBUG_INFO("============= Configuration ===============\r\n");
- strcpy((char*)Wifi_A.ssid, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
- DEBUG_INFO("Wifi SSID: %s\n", Wifi_A.ssid);
- strcpy((char*)Wifi_A.passwd, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
- DEBUG_INFO("Wifi Password: %s\n", Wifi_A.passwd);
- if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress != NULL)
- strcpy((char*)Wifi.staticIPAddr, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
- DEBUG_INFO("Wifi Static IP: %s\n", Wifi.staticIPAddr);
- Wifi.ipType = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient;
- DEBUG_INFO("DHCP client flag: %d\n", Wifi.ipType);
- DEBUG_INFO("===========================================\r\n");
- }
- //==========================================
- // Refresh USB interface
- //==========================================
- void refreshUSB(void)
- {
- FILE *fp;
- char cmd[256];
- // Get IP address & net mask
- strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/unbind");
- fp = popen(cmd, "r");
- sleep(2);
- pclose(fp);
- strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/bind");
- fp = popen(cmd, "r");
- sleep(5);
- pclose(fp);
- }
- //==========================================
- // Check wifi interface status
- //==========================================
- int isFindInterface()
- {
- int result = FAIL;
- struct ifaddrs *ifaddr, *ifa;
- //refreshUSB();
- if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == WIFI_MODE_AP)
- {
- if (getifaddrs(&ifaddr) != FAIL)
- {
- for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
- {
- if (ifa->ifa_addr == NULL ||
- ifa->ifa_addr->sa_family != AF_PACKET) continue;
- for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceAP);idx++)
- {
- if(mystrcmp(ifa->ifa_name, Support_InterfaceAP[idx]) == PASS)
- {
- Wifi.currentInterface = ifa->ifa_name;
- result = PASS;
- }
- }
- }
- freeifaddrs(ifaddr);
- freeifaddrs(ifa);
- }
- }
- else
- {
- if (getifaddrs(&ifaddr) != FAIL)
- {
- for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
- {
- if (ifa->ifa_addr == NULL ||
- ifa->ifa_addr->sa_family != AF_PACKET) continue;
- for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceSTA);idx++)
- {
- if(mystrcmp(ifa->ifa_name, Support_InterfaceSTA[idx]) == PASS)
- {
- Wifi.currentInterface = ifa->ifa_name;
- result = PASS;
- }
- }
- }
- freeifaddrs(ifaddr);
- freeifaddrs(ifa);
- }
- }
- return result;
- }
- //==========================================
- // Get wifi interface info
- //==========================================
- int getInterfaceInfo()
- {
- int result = PASS;
- FILE *fp;
- char cmd[256];
- char buf[512];
- char tmp[512];
- // Get IP address & net mask
- strcpy(cmd, "ifconfig ");
- strcat(cmd, Wifi.currentInterface);
- fp = popen(cmd, "r");
- if(fp == NULL)
- result = FAIL;
- else
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "HWaddr") > 0)
- {
- sscanf(buf, "%*s%*s%*s%*s%s", tmp);
- strcpy(Wifi.currentMAC, tmp);
- }
- if(strstr(buf, "inet addr:") > 0)
- {
- sscanf(buf, "%*s%s", tmp);
- substr(Wifi.currentIPAddr, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
- sscanf(buf, "%*s%*s%*s%s", tmp);
- substr(Wifi.currentNetmask, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
- }
- }
- }
- pclose(fp);
- // Get gateway
- fp = popen("ip route", "r");
- if(fp == NULL)
- result = FAIL;
- else
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strncmp(buf, "default", strlen("default")) == 0)
- break;
- }
- sscanf(buf, "%*s%*s%s", tmp);
- substr(Wifi.currentGateway, tmp, 0, strlen(tmp));
- }
- pclose(fp);
- if(!dispReq.isShowed_IF_info)
- {
- DEBUG_INFO("MAC: %s\n", Wifi.currentMAC);
- dispReq.isShowed_IF_info = true;
- }
- if(!dispReq.isShowed_IP_info)
- {
- DEBUG_INFO("IP address: %s\n", Wifi.currentIPAddr);
- DEBUG_INFO("Net mask: %s\n", Wifi.currentNetmask);
- DEBUG_INFO("Default gateway: %s\n", Wifi.currentGateway);
- dispReq.isShowed_IP_info = true;
- }
- strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, (const char*)Wifi.currentIPAddr);
- strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, (const char*)Wifi.currentNetmask);
- strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, (const char*)Wifi.currentGateway);
- strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, (const char*)Wifi.currentMAC);
- return result;
- }
- //==========================================
- // Check if module connect to wifi AP
- //==========================================
- int isConnectAP()
- {
- int result = PASS;
- FILE *fp;
- char cmd[256];
- char buf[512];
- // Get IP address & net mask
- strcpy(cmd, "/root/iwconfig ");
- strcat(cmd, Wifi.currentInterface);
- fp = popen(cmd, "r");
- if(fp == NULL)
- result = FAIL;
- else
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "Not-Associated") > 0)
- {
- result = FAIL;
- DEBUG_INFO("AP check: Not-Associated.\n");
- }
- }
- }
- pclose(fp);
- return result;
- }
- //==========================================
- // Check internet access status
- //==========================================
- int isReachableInternet()
- {
- int result = FAIL;
- FILE *fp;
- char cmd[256];
- char buf[512];
- //char tmp[512];
- for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
- {
- strcpy(cmd, "ping -c 1 -w 3 ");
- strcat(cmd, valid_Internet[idx]);
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "transmitted") > 0)
- {
- //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
- if(strstr(buf,"100%") != NULL)
- {
- }
- else
- {
- result = PASS;
- }
- //DEBUG_INFO("%s",buf);
- //DEBUG_INFO("%s\n",tmp);
- }
- }
- }
- pclose(fp);
- }
- return result;
- }
- //==========================================
- // Check module soft AP is start up
- //==========================================
- int isStartUpAP()
- {
- int result = FAIL;
- FILE *fp;
- char cmd[256];
- char buf[512];
- // Get IP address & net mask
- strcpy(cmd, "/root/uaputl sys_cfg_bss_status");
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "BSS status = started") > 0)
- {
- result = PASS;
- DEBUG_INFO("AP already start up.\n");
- }
- }
- }
- pclose(fp);
- return result;
- }
- //==========================================
- // Get wifi connection quality
- //==========================================
- int getLinkQuality()
- {
- int result = FAIL;
- FILE *fp;
- char cmd[256];
- char buf[512];
- char tmp[512];
- strcpy(cmd, "/root/iwconfig ");
- strcat(cmd, Wifi.currentInterface);
- fp = popen(cmd, "r");
- if(fp == NULL)
- result = FAIL;
- else
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "Signal level") > 0)
- {
- sscanf(buf, "%*s%*s%*s%s", tmp);
- substr(tmp, tmp, strspn(tmp, "Signal level="), strlen(buf)-strspn(tmp, "Signal level="));
- Wifi.rssi = atoi(tmp);
- result = PASS;
- }
- }
- }
- pclose(fp);
- return result;
- }
- //==========================================
- // WPA parameter set
- //==========================================
- int setWPAconf()
- {
- int result = FAIL;
- char cmdBuf[512];
- FILE *pFile;
- char buffer[500]="ctrl_interface=/var/run/wpa_supplicant\nupdate_config=1\n";
- // Stop dhcp client or server
- sprintf(cmdBuf, "pgrep -f \"udhcpc\" | xargs kill");
- system(cmdBuf);
- sprintf(cmdBuf, "pgrep -f \"udhcpd\" | xargs kill");
- system(cmdBuf);
- // Clear wifi configuration file
- system("cat /dev/null > /etc/wpa.conf");
- switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
- {
- case WIFI_MODE_STA:
- // Fill out configuration value depend on share memory setting
- if(strlen((const char*)Wifi_A.passwd)>0)
- {
- if(strlen((const char*)Wifi_A.passwd)>=8)
- {
- strcat(buffer, "network={\n ssid=\"");
- strcat(buffer, (char*)Wifi_A.ssid);
- strcat(buffer, "\"\n key_mgmt=WPA-EAP WPA-PSK \n");
- strcat(buffer, " psk=\"");
- strcat(buffer, (char*)Wifi_A.passwd);
- strcat(buffer, "\"\n}\n\n");
- }
- if(strlen((const char*)Wifi_A.passwd)==5 || strlen((const char*)Wifi_A.passwd)==13)
- {
- strcat(buffer, "network={\n ssid=\"");
- strcat(buffer, (char*)Wifi_A.ssid);
- strcat(buffer, "\"\n key_mgmt=NONE\n");
- strcat(buffer, " wep_key0=\"");
- strcat(buffer, (char*)Wifi_A.passwd);
- strcat(buffer, "\"\n auth_alg=OPEN SHARED\n");
- strcat(buffer, "\n}\n\n");
- }
- strcat(buffer, "network={\n ssid=\"");
- strcat(buffer, (char*)Wifi_A.ssid);
- strcat(buffer, "\"\n key_mgmt=NONE");
- strcat(buffer, "\n}");
- }
- else
- {
- strcat(buffer, "network={\n ssid=\"");
- strcat(buffer, (char*)Wifi_A.ssid);
- strcat(buffer, "\"\n key_mgmt=NONE");
- strcat(buffer, "\n}");
- }
- // Configuration value write to /etc/wpa.conf
- pFile = fopen("/etc/wpa.conf","w");
- fwrite(buffer,strlen(buffer), 1, pFile);
- fclose(pFile);
- // Restart wpa_supplicant
- sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
- system(cmdBuf);
- sprintf(cmdBuf, "/root/wpa_supplicant -i %s -c /etc/wpa.conf -B", Wifi.currentInterface);
- system(cmdBuf);
- // Stop wifi AP
- sprintf(cmdBuf, "/root/uaputl bss_stop");
- system(cmdBuf);
- sleep(5);
- // Wifi IP set by DHCP client or static
- if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
- {
- sprintf(cmdBuf, "/sbin/udhcpc -i %s -s /root/dhcp_script/wifi.script > /dev/null &", Wifi.currentInterface);
- system(cmdBuf);
- }
- else
- {
- strcpy(cmdBuf, "ifconfig ");
- strcat(cmdBuf, Wifi.currentInterface);
- strcat(cmdBuf, " ");
- strcat(cmdBuf, Wifi.staticIPAddr);
- system(cmdBuf);
- DEBUG_INFO("Setting %s static IP to %s ...\n", Wifi.currentInterface, Wifi.staticIPAddr);
- }
- break;
- case WIFI_MODE_AP:
- // Stop wifi AP
- sprintf(cmdBuf, "/root/uaputl bss_stop");
- system(cmdBuf);
- // Set SSID to uaputl.conf
- sprintf(cmdBuf, "sed -i 's/ SSID=.*$/ SSID=\"%s\"/' /root/uaputl_config/uaputl.conf", Wifi_A.ssid);
- system(cmdBuf);
- // Set AP configuration by uaputl.conf
- sprintf(cmdBuf, "/root/uaputl sys_config /root/uaputl_config/uaputl.conf");
- system(cmdBuf);
- // Start up wifi AP
- sprintf(cmdBuf, "/root/uaputl bss_start");
- system(cmdBuf);
- sleep(5);
- // Set wifi AP ip address
- sprintf(cmdBuf, "ifconfig %s 192.168.10.10", Wifi.currentInterface);
- system(cmdBuf);
- DEBUG_INFO("Setting %s static IP to 192.168.10.10...\n", Wifi.currentInterface);
- // Start DHCP server
- if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpServer==0)
- {
- sprintf(cmdBuf, "/usr/sbin/udhcpd /etc/udhcpd.conf > /dev/null &");
- system(cmdBuf);
- }
- break;
- case WIFI_MODE_ADHOC:
- break;
- case WIFI_MODE_DISABLE:
- default:
- break;
- }
- result = PASS;
- return result;
- }
- //=================================
- // Common routine
- //=================================
- char* getTimeString(void)
- {
- char *result=malloc(21);
- time_t timep;
- struct tm *p;
- time(&timep);
- p=gmtime(&timep);
- 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);
- return result;
- }
- void trim(char *s)
- {
- int i=0, j, k, l=0;
- while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
- i++;
- j = strlen(s)-1;
- while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
- j--;
- if(i==0 && j==strlen(s)-1) { }
- else if(i==0) s[j+1] = '\0';
- else {
- for(k=i; k<=j; k++) s[l++] = s[k];
- s[l] = '\0';
- }
- }
- int mystrcmp(char *p1,char *p2)
- {
- while(*p1==*p2)
- {
- if(*p1=='\0' || *p2=='\0')
- break;
- p1++;
- p2++;
- }
- if(*p1=='\0' && *p2=='\0')
- return(PASS);
- else
- return(FAIL);
- }
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
- {
- strncpy(dest, src + start, cnt);
- dest[cnt] = 0;
- }
- void split(char **arr, char *str, const char *del)
- {
- char *s = strtok(str, del);
- while(s != NULL)
- {
- *arr++ = s;
- s = strtok(NULL, del);
- }
- }
- int checkIP(void)
- {
- // DHCP or static setting
- int isGetIP = FAIL;
- FILE *fp;
- char buf[512];
- char tmp[512];
- char cmd[512];
- if(Wifi.ipType == IP_TYPE_DHCP) {
- // Get IP address & net mask
- strcpy(cmd, "ifconfig ");
- strcat(cmd, Wifi.currentInterface);
- fp = popen(cmd, "r");
- if(fp == NULL)
- isGetIP = FAIL;
- else
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "addr:") > 0)
- {
- if(strstr(buf, "192.168.1.10") > 0)
- {
- DEBUG_INFO("IP cannot be same as default value of Ethernet...\n");
- }
- else
- {
- isGetIP = PASS;
- cnt_getIP_Fail = 0;
- }
- sscanf(buf, "%*s%s", tmp);
- substr(Wifi.currentIPAddr, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
- sscanf(buf, "%*s%*s%*s%s", tmp);
- substr(Wifi.currentNetmask, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
- }else {
- }
- }
- }
- if(isGetIP == FAIL && cnt_getIP_Fail<3) {
- sprintf(cmd, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
- system(cmd);
- if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
- {
- sprintf(cmd, "/sbin/udhcpc -i %s -s /root/dhcp_script/wifi.script > /dev/null &", Wifi.currentInterface);
- system(cmd);
- }
- DEBUG_INFO("Sending DHCP request...\n");
- cnt_getIP_Fail++;
- sleep(5);
- }
- pclose(fp);
- }else {
- strcpy(cmd, "ifconfig ");
- strcat(cmd, Wifi.currentInterface);
- strcat(cmd, " ");
- strcat(cmd, Wifi.staticIPAddr);
- fp = popen(cmd, "r");
- DEBUG_INFO("Setting static IP to %s ...\n", Wifi.staticIPAddr);
- pclose(fp);
- isGetIP = PASS;
- }
- cnt_getIP_Fail = 0;
- return isGetIP;
- }
- int restartWPA(void)
- {
- int result = FAIL;
- FILE *fp;
- char buf[512];
- char cmd[50];
- // Get IP address & net mask
- strcpy(cmd, "wpa_cli -i ");
- strcat(cmd, Wifi.currentInterface);
- strcat(cmd, " reconfigure");
- fp = popen(cmd, "r");
- DEBUG_INFO("WPA reconfigure %s ...\n", Wifi.currentInterface);
- if(fp == NULL)
- result = FAIL;
- else
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "OK") > 0)
- {
- DEBUG_INFO("reconfigure OK.\n");
- result = PASS;
- }else {
- DEBUG_INFO("reconfigure failed.\n");
- result = FAIL;
- }
- }
- }
- pclose(fp);
- if(result == PASS) {
- strcpy(cmd, "wpa_cli -i ");
- strcat(cmd, Wifi.currentInterface);
- strcat(cmd, " reconfigure");
- fp = popen(cmd, "r");
- DEBUG_INFO("WPA reconnect %s ...\n", Wifi.currentInterface);
- if(fp == NULL)
- result = FAIL;
- else{
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "OK") > 0){
- DEBUG_INFO("reconnect OK.\n");
- result = PASS;
- }else {
- DEBUG_INFO("reconnect failed.\n");
- result = FAIL;
- }
- }
- }
- pclose(fp);
- }
- return result;
- }
- void proc_sta()
- {
- switch(Wifi_module_sts)
- {
- case STA_STATE_INIT:
- dispReq.isShowed_IF_info = false;
- // get info from shared memory
- getParameters();
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
- isSetWPA_OK = false;
- // check interface
- if((strlen((const char*)Wifi_A.ssid)>0) && (isFindInterface() == PASS))
- {
- DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
- Wifi_module_sts = STA_STATE_DEVICE_DETECT;
- ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
- DEBUG_INFO("=================[State 1]===================\n");
- }
- else
- {
- ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
- DEBUG_ERROR("Wifi support interface valid result: Fail\n");
- sleep(30);
- }
- break;
- case STA_STATE_DEVICE_DETECT:
- dispReq.isShowed_IP_info = false;
- if(!isSetWPA_OK && (setWPAconf() == PASS))
- {
- isSetWPA_OK = true;
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
- sleep(5);
- }
- else if((isSetWPA_OK == true) && (isConnectAP() == PASS))
- {
- Wifi_module_sts = STA_STATE_AP_CONNECTED;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
- DEBUG_INFO("=================[State 2]===================\n");
- }
- else
- {
- cnt_getAP_Fail++;
- DEBUG_INFO("Fail to connect to the AP %d times...\n",cnt_getAP_Fail);
- if(cnt_getAP_Fail>=3)
- {
- Wifi_module_sts = STA_STATE_INIT;
- cnt_getAP_Fail = 0;
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
- DEBUG_INFO("=================[State 0]===================\n");
- }
- else
- {
- sleep(10);
- }
- }
- break;
- case STA_STATE_AP_CONNECTED:
- getInterfaceInfo();
- getLinkQuality();
- DEBUG_INFO("Wifi quality: %d dBm\n", Wifi.rssi);
- ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = Wifi.rssi;
- if(isReachableInternet() == PASS)
- {
- DEBUG_INFO("Wifi internet valid result: Pass\n");
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=0;
- ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=1;
- ShmSysConfigAndInfo->SysInfo.InternetConn=1;
- cnt_pingDNS_Fail = 0;
- sleep(30);
- }
- else
- {
- cnt_pingDNS_Fail++;
- DEBUG_INFO("Wifi internet valid result: Fail %d time\n", cnt_pingDNS_Fail);
- sleep(5);
- }
- if(cnt_pingDNS_Fail >= 3)
- {
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
- ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
- ShmSysConfigAndInfo->SysInfo.InternetConn=0;
- cnt_pingDNS_Fail = 0;
- DEBUG_INFO("Ping DNS failed...");
- memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
- memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
- memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
- if(isFindInterface() == PASS)
- {
- DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
- isSetWPA_OK = false;
- ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
- Wifi_module_sts = STA_STATE_DEVICE_DETECT;
- DEBUG_INFO("=================[State 1]===================\n");
- }
- else
- {
- DEBUG_INFO("Wifi support interface valid result: Fail\n");
- ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
- Wifi_module_sts = STA_STATE_INIT;
- DEBUG_INFO("=================[State 0]===================\n");
- }
- }
- break;
- default:
- Wifi_module_sts = STA_STATE_INIT;
- break;
- }
- }
- void proc_ap()
- {
- switch(Wifi_module_sts)
- {
- case AP_STATE_INIT:
- // get info from shared memory
- getParameters();
- isSetWPA_OK = false;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
- // check interface
- if((strlen((const char*)Wifi_A.ssid)>0) && (isFindInterface() == PASS))
- {
- DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
- Wifi_module_sts = AP_STATE_DEVICE_DETECT;
- ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
- DEBUG_INFO("=================[State 1]===================\n");
- }
- else
- {
- ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
- DEBUG_ERROR("Wifi support interface valid result: Fail\n");
- sleep(30);
- }
- break;
- case AP_STATE_DEVICE_DETECT:
- if(!isSetWPA_OK && (setWPAconf() == PASS))
- {
- isSetWPA_OK = true;
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
- sleep(5);
- }
- else if(isSetWPA_OK && (isStartUpAP() == PASS))
- {
- Wifi_module_sts = AP_STATE_AP_START;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
- DEBUG_INFO("=================[State 2]===================\n");
- }
- else
- {
- cnt_getAP_Fail++;
- DEBUG_INFO("Fail to start up AP %d times...\n",cnt_getAP_Fail);
- if(cnt_getAP_Fail>=3)
- {
- Wifi_module_sts = AP_STATE_INIT;
- cnt_getAP_Fail = 0;
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
- DEBUG_INFO("=================[State 0]===================\n");
- }
- else
- {
- sleep(10);
- }
- }
- break;
- case AP_STATE_AP_START:
- getInterfaceInfo();
- if(isStartUpAP() == PASS)
- {
- DEBUG_INFO("Wifi AP start up valid result: Pass\n");
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
- cnt_pingDNS_Fail = 0;
- sleep(30);
- }
- else
- {
- cnt_pingDNS_Fail++;
- DEBUG_INFO("Wifi AP start up valid result: Fail %d time\n", cnt_pingDNS_Fail);
- sleep(5);
- }
- if(cnt_pingDNS_Fail >= 3)
- {
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
- cnt_pingDNS_Fail = 0;
- DEBUG_INFO("Wifi AP start up status failed...");
- memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
- memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
- memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
- if(isFindInterface() == PASS)
- {
- DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
- isSetWPA_OK = false;
- ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
- Wifi_module_sts = STA_STATE_DEVICE_DETECT;
- DEBUG_INFO("=================[State 1]===================\n");
- }
- else
- {
- DEBUG_INFO("Wifi support interface valid result: Fail\n");
- ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
- Wifi_module_sts = STA_STATE_INIT;
- DEBUG_INFO("=================[State 0]===================\n");
- }
- }
- break;
- default:
- Wifi_module_sts = AP_STATE_INIT;
- break;
- }
- }
- int main(void)
- {
- if(InitShareMemory() == FAIL)
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("InitShareMemory NG\n");
- #endif
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
- }
- sleep(5);
- return 0;
- }
- //=============================================
- // Install WIFI module driver
- //=============================================
- #ifdef UBLOX
- system("insmod /lib/modules/mlan.ko");
- system("insmod /lib/modules/usb8801.ko");
- sleep(5);
- system("ifconfig mlan0 up");
- system("ifconfig uap0 up");
- #endif
- #ifdef MT7601U
- system("insmod /lib/modules/mt7601u.ko");
- sleep(5);
- system("ifconfig wlan0 up");
- #endif
- DEBUG_INFO("=================[State 0]===================\n");
- for(;;)
- {
- switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
- {
- case WIFI_MODE_STA:
- proc_sta();
- break;
- case WIFI_MODE_AP:
- proc_ap();
- break;
- case WIFI_MODE_ADHOC:
- break;
- case WIFI_MODE_DISABLE:
- default:
- break;
- }
- }
- return 0;
- }
|