123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827 |
- /*
- * Module_4g.c
- *
- * Created on: 2019-11-29
- * Update on: 2021-12-06
- * Author: Eason Yang / Folus Wen
- * Version: V0.12
- */
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/time.h>
- #include <sys/timeb.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #include <sys/mman.h>
- #include <unistd.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <termios.h>
- #include <errno.h>
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #include <stdint.h>
- #include "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 ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
- #define PASS 1
- #define FAIL -1
- enum DONGLE_MODEL
- {
- DONGLE_QUECTEL=0,
- DONGLE_THALES,
- DONGLE_UBLOX
- };
- enum CONNECT_STEP
- {
- CONNECT_STEP_CHECK_MODULE=0,
- CONNECT_STEP_READ_MODULE_INFO,
- CONNECT_STEP_READ_SIM_INFO,
- CONNECT_STEP_CONNECTING,
- CONNECT_STEP_CONNECTED,
- CONNECT_STEP_DISCONNECTED
- };
- enum NETWORK_MODE
- {
- NETWORK_MODE_AUTO=0,
- NETWORK_MODE_GSM,
- NETWORK_MODE_WCDMA,
- NETWORK_MODE_LTE,
- NETWORK_MODE_TDSCDMA,
- NETWORK_MODE_UMTS,
- NETWORK_MODE_CDMA,
- NETWORK_MODE_HDR,
- NETWORK_MODE_CDMA_HDR
- };
- // Define Module network mode
- #define NET_MODE_NO_SERVICE 0
- #define NET_MODE_CDMA 1
- #define NET_MODE_GMS_GPRS 2
- #define NET_MODE_WCDMA 3
- #define NET_MODE_GSM_WCDMA 4
- #define NET_MODE_TD_SCDMA 5
- #define NET_MODE_HSPA 6
- #define NET_MODE_LTE 7
- #define NET_MODE_UNKNOWN 9
- // Define interval
- #define DefaultInterval 5 // Seconds
- #define SystemInterval 30 // Seconds
- #define CheckModemInterval 30 // Seconds
- #define CheckSimInterval 30 // Seconds
- #define CheckModemInfoInterval 30 // Seconds
- #define CheckConnectionInterval 60 // Seconds
- #define CheckInternetInterval 30 // Seconds
- #define DisconnInterval 60 // Seconds
- // Define Telecom mode
- #define DISABLE_4G_MODULE 0
- #define ENABLE_4G 1
- int Check4GModem(void);
- int isPppUp(void);
- int isReadInfo(int connectStep);
- int isReadSimInfo(void);
- int isReachableInternet(void);
- int isModuleUnbind(void);
- int isModuleBind(void);
- int rstModule(void);
- int Load4gConfiguration(void);
- int CheckSignalRssi(void);
- int at_command(int uart, char* cmd, char* rx);
- int openPort(char *tty);
- int set_interface_attribs (int fd, int speed, int parity);
- int set_blocking (int fd, int should_block);
- void trim_s(char *s, unsigned char len);
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
- char *portName[3] = {"/dev/ttyUSB2", "/dev/ttyACM2", "/dev/ttyACM2"};
- char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
- char *Version_And_Date[2] = {"V0.14","2022-09-12"};
- pid_t pid;
- struct dongle_info
- {
- int Model;
- char ICCID[20];
- char IMSI[16];
- char IMEI[16];
- char MANUFACTURER[16];
- char MODELNAME[10];
- char REVISION[18];
- unsigned char MODE;
- char act[16];
- char operator[16];
- char band[16];
- int channel;
- int CSQ;
- int cnt_InternetFail;
- int cnt_ReadInfoFail;
- int cnt_pppFail;
- int cnt_SearchModuleFail;
- int cnt_ReadSimInfoFail;
- uint64_t bytesTx;
- uint64_t bytesRx;
- uint32_t lastCounter;
- }Dongle;
- struct SysConfigAndInfo *ShmSysConfigAndInfo;
- struct StatusCodeData *ShmStatusCodeData;
- struct FanModuleData *ShmFanModuleData;
- struct OCPP16Data *ShmOCPP16Data;
- 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 -n \"[%04d.%02d.%02d %02d:%02d:%02d]%s\" >> /Storage/SystemLog/[%04d.%02d]4g_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
- #ifdef ConsloePrintLog
- 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);
- #endif
- 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)
- {
- DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
- result = FAIL;
- }
- else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
- result = FAIL;
- }
- else
- {}
- //creat ShmStatusCodeData
- if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
- result = FAIL;
- }
- else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
- result = FAIL;
- }
- else
- {}
- //creat ShmOCPP16Data
- if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
- {
- DEBUG_ERROR("shmget ShmOCPP16Data NG");
- result = FAIL;
- }
- else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
- {
- DEBUG_ERROR("shmat ShmOCPP16Data NG");
- result = FAIL;
- }
- else
- {}
- return result;
- }
- //==========================================
- // Check dongle model
- //==========================================
- int Check4GModem(void)
- {
- int result = FAIL;
- if((access("/dev/ttyUSB0", F_OK) != -1) &&
- (access("/dev/ttyUSB1", F_OK) != -1) &&
- (access("/dev/ttyUSB2", F_OK) != -1) &&
- (access("/dev/ttyUSB3", F_OK) != -1))
- {
- result = DONGLE_QUECTEL;
- }
- else if((access("/dev/ttyACM0", F_OK) != -1) &&
- (access("/dev/ttyACM1", F_OK) != -1) &&
- (access("/dev/ttyACM2", F_OK) != -1) &&
- (access("/dev/ttyACM3", F_OK) != -1))
- {
- if(access("/dev/ttyACM4", F_OK) != -1)
- result = DONGLE_UBLOX;
- else
- result = DONGLE_THALES;
- }
- else
- {}
- switch(result)
- {
- case DONGLE_QUECTEL:
- DEBUG_INFO("Quectel 4G modem be found\n");
- break;
- case DONGLE_THALES:
- DEBUG_INFO("Thelas 4G modem be found\n");
- break;
- case DONGLE_UBLOX:
- DEBUG_INFO("Ublox 4G modem be found\n");
- break;
- default:
- DEBUG_WARN("No 4G modem be found\n");
- break;
- }
- return result;
- }
- //==========================================
- // Check ppp interface status
- //==========================================
- int isPppUp(void)
- {
- int result = FAIL;
- FILE *fp;
- char cmd[256];
- char buf[512];
- char tmp[512];
- strcpy(cmd, "ifconfig ppp0");;
- fp = popen(cmd, "r");
- if(fp != NULL)
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if(strstr(buf, "ppp") > 0)
- {
- result = PASS;
- }
- if(strstr(buf, "addr:") > 0)
- {
- sscanf(buf, "%*s%s", tmp);
- substr((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, tmp, strspn(tmp, "addr:"), (strlen(tmp) - strspn(tmp, "addr:")));
- }
- }
- }
- pclose(fp);
- return result;
- }
- //==========================================
- // Check dongle info read status
- //==========================================
- int isReadInfo(int connectStep)
- {
- int result = PASS;
- int uart;
- char rx[512];
- char tmp[512];
- char Length;
- if((uart = openPort(portName[Dongle.Model])) != FAIL)
- {
- switch(Dongle.Model)
- {
- case DONGLE_QUECTEL:
- default:
- //==============================
- // Set don't echo command
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "ate0\r", rx);
- if(Length > 0)
- {
- if(strstr(rx, "ERROR"))
- {
- result = FAIL;
- }
- else
- {}
- }
- else
- result = FAIL;
- if(connectStep == CONNECT_STEP_READ_MODULE_INFO)
- {
- //==============================
- // Read Manufacturer
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "at+gmi\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, ARRAY_SIZE(tmp));
- memcpy(tmp, rx, strcspn(rx,"OK"));
- memset(rx, 0, ARRAY_SIZE(rx));
- trim_s(tmp,Length);
- if(strstr(tmp, "atgmi") != NULL)
- {
- substr(tmp, tmp, 5, strlen(tmp)-5);
- }
- memset(Dongle.MANUFACTURER, 0x00, ARRAY_SIZE(Dongle.MANUFACTURER));
- strncpy(Dongle.MANUFACTURER, tmp, strlen(tmp));
- }
- else
- result = FAIL;
- //==============================
- // Read Model
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "at+gmm\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, ARRAY_SIZE(tmp));
- memcpy(tmp, rx, strcspn(rx,"OK"));
- memset(rx, 0, ARRAY_SIZE(rx));
- trim_s(tmp,Length);
- if(strstr(tmp, "atgmm") != NULL)
- {
- substr(tmp, tmp, 5, strlen(tmp)-5);
- }
- memset(Dongle.MODELNAME, 0x00, ARRAY_SIZE(Dongle.MODELNAME));
- strncpy(Dongle.MODELNAME, tmp, strlen(tmp));
- }
- else
- result = FAIL;
- //==============================
- // Read Revision
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "at+gmr\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, ARRAY_SIZE(tmp));
- memcpy(tmp, rx, strcspn(rx, "OK"));
- memset(rx, 0, ARRAY_SIZE(rx));
- trim_s(tmp,Length);
- if(strstr(tmp, "atgmr") != NULL)
- {
- substr(tmp, tmp, 5, strlen(tmp)-5);
- }
- memset(Dongle.REVISION, 0x00, ARRAY_SIZE(Dongle.REVISION));
- strncpy(Dongle.REVISION, tmp, strlen(tmp));
- }
- else
- result = FAIL;
- //==============================
- // Read IMEI
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "at+gsn\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, ARRAY_SIZE(tmp));
- memcpy(tmp, rx, strcspn(rx, "OK"));
- memset(rx, 0, ARRAY_SIZE(rx));
- trim_s(tmp,Length);
- if(strstr(tmp, "atgsn") != NULL)
- {
- substr(tmp, tmp, 5, strlen(tmp)-5);
- }
- memset(Dongle.IMEI, 0x00, ARRAY_SIZE(Dongle.IMEI));
- strncpy(Dongle.IMEI, tmp, strlen(tmp));
- }
- else
- result = FAIL;
- }
- else if(connectStep == CONNECT_STEP_CONNECTED)
- {
- //==============================
- // Read CSQ
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "at+csq\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, ARRAY_SIZE(tmp));
- memcpy(tmp, rx, strcspn(rx,","));
- strncpy(rx, tmp + strcspn(tmp,":")+1, 10);
- Dongle.CSQ = atoi(rx);
- }
- else
- result = FAIL;
- //==============================
- // Read Mode
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "at+qnwinfo\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, sizeof tmp);
- if(sscanf(rx, "%*[^:]:%[^,],%[^,],%[^,],%d", Dongle.act, Dongle.operator, Dongle.band, &Dongle.channel) == 4)
- {
- //strncpy(tmp, &rx[strcspn(rx,":")+2], strcspn(rx,",")-(strcspn(rx,":")+2));
- if((strstr(Dongle.act, "GPRS") != NULL) || (strstr(Dongle.act, "EDGE") != NULL))
- Dongle.MODE = NET_MODE_GMS_GPRS;
- else if(strstr(Dongle.act, "WCDMA") != NULL)
- Dongle.MODE = NET_MODE_WCDMA;
- else if(strstr(Dongle.act, "GSM") != NULL)
- Dongle.MODE = NET_MODE_GSM_WCDMA;
- else if(strstr(Dongle.act, "TDSCDMA") != NULL)
- Dongle.MODE = NET_MODE_TD_SCDMA;
- else if(strstr(Dongle.act, "CDMA") != NULL)
- Dongle.MODE = NET_MODE_CDMA;
- else if(strstr(Dongle.act, "LTE") != NULL)
- Dongle.MODE = NET_MODE_LTE;
- else if((strstr(Dongle.act, "HSDPA") != NULL) || (strstr(Dongle.act, "HSUPA") != NULL) || (strstr(Dongle.act, "HSPA+") != NULL))
- Dongle.MODE = NET_MODE_HSPA;
- else
- Dongle.MODE = NET_MODE_UNKNOWN;
- }
- else
- {
- DEBUG_WARN("at+qnwinfo response can not parse.\n");
- }
- }
- else
- result = FAIL;
- //==============================
- // Read data counter
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "at+qgdcnt?\r", rx);
- if(Length > 0)
- {
- if(sscanf(rx, "%*[^:]:%lld,%lld", &Dongle.bytesTx, &Dongle.bytesRx) != 2)
- {
- DEBUG_WARN("at+qgdcnt response can not parse.\n");
- }
- }
- else
- result = FAIL;
- }
- break;
- case DONGLE_THALES:
- //==============================
- // Set don't echo command
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "ate0\r", rx);
- if(Length > 0)
- {
- if(strstr(rx, "ERROR"))
- {
- result = FAIL;
- }
- else
- {}
- }
- else
- result = FAIL;
- if(connectStep == CONNECT_STEP_READ_MODULE_INFO)
- {
- //==============================
- // Read Manufacturer
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "at+cgmi\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, ARRAY_SIZE(tmp));
- memcpy(tmp, rx, strcspn(rx,"OK"));
- memset(rx, 0, ARRAY_SIZE(rx));
- memset(Dongle.MANUFACTURER, 0x00, ARRAY_SIZE(Dongle.MANUFACTURER));
- strncpy(Dongle.MANUFACTURER, tmp+2, 9);
- }
- else
- result = FAIL;
- //==============================
- // Read Model
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "at+cgmm\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, ARRAY_SIZE(tmp));
- memcpy(tmp, &rx[strcspn(rx,"PLS")], strcspn(rx,"OK"));
- memset(rx, 0, ARRAY_SIZE(rx));
- memset(Dongle.MODELNAME, 0x00, ARRAY_SIZE(Dongle.MODELNAME));
- strncpy(Dongle.MODELNAME , tmp, 8);
- }
- else
- result = FAIL;
- //==============================
- // Read Revision
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "at+cgmr\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, ARRAY_SIZE(tmp));
- memcpy(tmp, &rx[strcspn(rx, "REVISION")+9], strcspn(rx, "OK"));
- memset(rx, 0, ARRAY_SIZE(rx));
- memset(Dongle.REVISION, 0x00, ARRAY_SIZE(Dongle.REVISION));
- strncpy(Dongle.REVISION, tmp, 6);
- }
- else
- result = FAIL;
- //==============================
- // Read IMEI
- //==============================
- Length = at_command(uart, "at+cgsn\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, ARRAY_SIZE(tmp));
- memcpy(tmp, rx, strcspn(rx, "OK"));
- memset(rx, 0, ARRAY_SIZE(rx));
- trim_s(tmp,Length);
- memset(Dongle.IMEI, 0x00, ARRAY_SIZE(Dongle.IMEI));
- strncpy(Dongle.IMEI, tmp, strlen(tmp));
- }
- else
- result = FAIL;
- }
- else if(connectStep == CONNECT_STEP_CONNECTED)
- {
- //==============================
- // Read CSQ
- //==============================
- Length = at_command(uart, "at+csq\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, ARRAY_SIZE(tmp));
- memcpy(tmp, rx, strcspn(rx,","));
- strncpy(rx, tmp + strcspn(tmp,":")+1, 10);
- Dongle.CSQ = atoi(rx);
- }
- else
- result = FAIL;
- //==============================
- // Read Mode
- //==============================
- memset(rx, 0x00, ARRAY_SIZE(rx));
- Length = at_command(uart, "AT^SMONI\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, sizeof tmp);
- if(strstr(rx, "2G,") != NULL)
- {
- DEBUG_INFO("2G network\n");
- if(sscanf(rx, "%*[^:]:%*[^,],%d,%*[^,],%*[^,],%[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%*[^,],%[^,],", &Dongle.channel, Dongle.operator, Dongle.act) == 2)
- {
- if(strstr(Dongle.act, "G") != NULL)
- Dongle.MODE = NET_MODE_GMS_GPRS;
- else if(strstr(Dongle.act, "E") != NULL)
- Dongle.MODE = NET_MODE_GMS_GPRS;
- else
- Dongle.MODE = NET_MODE_CDMA;
- }
- else
- {
- DEBUG_WARN("AT^SMONI response can not parse.\n");
- }
- }
- else if(strstr(rx, "3G,") != NULL)
- {
- if(sscanf(rx, "%*[^:]:%*[^,],%d,%*[^,],%*[^,],%*[^,],%*[^,],%[^,]", &Dongle.channel, Dongle.operator) == 2)
- {
- Dongle.MODE = NET_MODE_GSM_WCDMA;
- memset(Dongle.band, 0x00, ARRAY_SIZE(Dongle.band));
- memset(Dongle.act, 0x00, ARRAY_SIZE(Dongle.act));
- }
- else
- {
- DEBUG_WARN("AT^SMONI response can not parse.\n");
- }
- }
- else if(strstr(rx, "4G,") != NULL)
- {
- if(sscanf(rx, "%*[^:]:%*[^,],%d,%[^,],%*[^,],%*[^,],%[^,],%*[^,],%[^,],", &Dongle.channel, Dongle.band, Dongle.act, Dongle.operator) == 4)
- {
- if(strstr(Dongle.act, "FDD") != NULL)
- Dongle.MODE = NET_MODE_LTE;
- else if(strstr(Dongle.act, "TDD") != NULL)
- Dongle.MODE = NET_MODE_TD_SCDMA;
- else
- Dongle.MODE = NET_MODE_UNKNOWN;
- }
- else
- {
- DEBUG_WARN("AT^SMONI response can not parse.\n");
- }
- }
- else
- {
- DEBUG_WARN("AT^SMONI response can not parse.\n");
- }
- }
- else
- result = FAIL;
- }
- break;
- }
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
- #endif
- result = FAIL;
- }
- close(uart);
- return result;
- }
- //==========================================
- // Read sim card information
- //==========================================
- int isReadSimInfo(void)
- {
- int result = PASS;
- int uart;
- char rx[512];
- char tmp[512];
- char Length;
- if((uart = openPort(portName[Dongle.Model])) != FAIL)
- {
- switch(Dongle.Model)
- {
- case DONGLE_QUECTEL:
- default:
- //==============================
- // Set don't echo command
- //==============================
- Length = at_command(uart, "ate0\r", rx);
- if(Length > 0)
- {
- if(strstr(rx, "ERROR"))
- {
- result = FAIL;
- }
- else
- {}
- }
- else
- result = FAIL;
- //==============================
- // Read IMSI
- //==============================
- Length = at_command(uart, "at+cimi\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, sizeof tmp);
- if(strstr(rx, "ERROR"))
- {
- memset(Dongle.IMSI, 0, sizeof Dongle.IMSI);
- result = FAIL;
- }
- else
- {
- memcpy(tmp, rx, strcspn(rx, "OK"));
- trim_s(tmp, Length);
- memset(rx, 0, sizeof rx);
- if(strstr(tmp, "atcimi") != NULL)
- {
- substr(tmp, tmp, 6, strlen(tmp)-6);
- }
- memset(Dongle.IMSI, 0x00, ARRAY_SIZE(Dongle.IMSI));
- strncpy(Dongle.IMSI, tmp, strlen(tmp));
- }
- }
- else
- result = FAIL;
- //==============================
- // Read CCID
- //==============================
- Length = at_command(uart, "at+qccid\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, sizeof tmp);
- if(strstr(rx, "ERROR"))
- {
- memset(Dongle.ICCID, 0, sizeof Dongle.ICCID);
- result = FAIL;
- }
- else
- {
- memcpy(tmp, rx, strcspn(rx, "OK"));
- memset(rx, 0, sizeof rx);
- memset(Dongle.ICCID, 0x00, ARRAY_SIZE(Dongle.ICCID));
- strncpy(Dongle.ICCID, tmp + strcspn(tmp, ":") + 2, 20);
- }
- }
- else
- result = FAIL;
- break;
- case DONGLE_THALES:
- //==============================
- // Set don't echo command
- //==============================
- Length = at_command(uart, "ate0\r", rx);
- if(Length > 0)
- {
- if(strstr(rx, "ERROR"))
- {
- result = FAIL;
- }
- else
- {}
- }
- else
- result = FAIL;
- //==============================
- // Read IMSI
- //==============================
- Length = at_command(uart, "at+cimi\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, sizeof tmp);
- if(strstr(rx, "ERROR"))
- {
- memset(Dongle.IMSI, 0, sizeof Dongle.IMSI);
- result = FAIL;
- }
- else
- {
- memcpy(tmp, rx, strcspn(rx,"OK"));
- memset(rx, 0, sizeof rx);
- trim_s(tmp,Length);
- if(strstr(tmp, "atcimi") != NULL)
- {
- substr(tmp, tmp, 6, strlen(tmp)-6);
- }
- memset(Dongle.IMSI, 0x00, ARRAY_SIZE(Dongle.IMSI));
- strncpy(Dongle.IMSI, tmp, strlen(tmp));
- }
- }
- else
- result = FAIL;
- //==============================
- // Read CCID
- //==============================
- Length = at_command(uart, "at+ccid\r", rx);
- if(Length > 0)
- {
- memset(tmp, 0, sizeof tmp);
- if(strstr(rx, "ERROR"))
- {
- memset(Dongle.ICCID, 0, sizeof Dongle.ICCID);
- result = FAIL;
- }
- else
- {
- memcpy(tmp, rx, strcspn(rx, "OK"));
- memset(rx, 0, sizeof rx);
- memset(Dongle.ICCID, 0x00, ARRAY_SIZE(Dongle.ICCID));
- strncpy(Dongle.ICCID, tmp + strcspn(tmp, ":") + 2, 20);
- }
- }
- else
- result = FAIL;
- break;
- }
- }
- else
- {
- DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
- result = FAIL;
- }
- close(uart);
- return result;
- }
- //==========================================
- // Read signal information
- //==========================================
- int CheckSignalRssi(void)
- {
- int result = PASS;
- int uart;
- char rx[512];
- char tmp[512];
- char Lenght;
- if((uart = openPort(portName[Dongle.Model])) != FAIL)
- {
- switch (Dongle.Model)
- {
- case DONGLE_THALES:
- //==============================
- // Set don't echo command
- //==============================
- Lenght = at_command(uart, "ate0\r", rx);
- if(Lenght > 0)
- {
- if(strstr(rx, "ERROR"))
- {
- result = FAIL;
- }
- else
- {}
- }
- else
- result = FAIL;
- //==============================
- // Read CSQ
- //==============================
- Lenght = at_command(uart, "at+csq\r", rx);
- if(Lenght > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rx, strcspn(rx, ","));
- strncpy(rx, tmp + strcspn(tmp, ":") + 1, 10);
- Dongle.CSQ = atoi(rx);
- }
- else
- result = FAIL;
- break;
- case DONGLE_QUECTEL:
- default:
- //==============================
- // Set don't echo command
- //==============================
- Lenght = at_command(uart, "ate0\r", rx);
- if(Lenght > 0)
- {
- if(strstr(rx, "ERROR"))
- {
- result = FAIL;
- }
- else
- {}
- }
- else
- result = FAIL;
- //==============================
- // Read CSQ
- //==============================
- Lenght = at_command(uart, "at+csq\r", rx);
- if(Lenght > 0)
- {
- memset(tmp, 0, sizeof tmp);
- memcpy(tmp, rx, strcspn(rx, ","));
- strncpy(rx, tmp + strcspn(tmp, ":") + 1, 10);
- Dongle.CSQ = atoi(rx);
- }
- else
- result = FAIL;
- break;
- }
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
- #endif
- result = FAIL;
- }
- close(uart);
- return result;
- }
- //==========================================
- // Init 4G dongle configuration
- //==========================================
- int Load4gConfiguration()
- {
- int result = FAIL;
- unsigned char CopyTmp[1024];
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn) > 0)
- {
- system("cat /dev/null > /root/ppp/auto-apn.conf");
- system("chmod 600 /root/ppp/auto-apn.conf");
- system("cat /dev/null > /etc/ppp/pap-secrets");
- system("chmod 600 /etc/ppp/pap-secrets");
- system("cat /dev/null > /etc/ppp/chap-secrets");
- system("chmod 600 /etc/ppp/chap-secrets");
- system("cat /dev/null > /etc/ppp/auth");
- system("chmod 600 /etc/ppp/auth");
- memset(CopyTmp,0,sizeof(CopyTmp));
- sprintf((char*)CopyTmp,"echo APN=\"%s\" > /root/ppp/auto-apn.conf",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn);
- system((char*)CopyTmp);
- memset(CopyTmp,0,sizeof(CopyTmp));
- sprintf((char*)CopyTmp,"echo ACCOUNT=\"%s\" >> /root/ppp/auto-apn.conf",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
- system((char*)CopyTmp);
- memset(CopyTmp,0,sizeof(CopyTmp));
- sprintf((char*)CopyTmp,"echo PASSWORD=\"%s\" >> /root/ppp/auto-apn.conf",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
- system((char*)CopyTmp);
- memset(CopyTmp,0,sizeof(CopyTmp));
- sprintf((char*)CopyTmp,"echo %s > /etc/ppp/auth",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
- system((char*)CopyTmp);
- sprintf((char*)CopyTmp,"echo %s >> /etc/ppp/auth",ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
- system((char*)CopyTmp);
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId)>0)
- {
- memset(CopyTmp,0,sizeof(CopyTmp));
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd)>0)
- sprintf((char*)CopyTmp,"echo \"%s * %s \" > /etc/ppp/pap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
- else
- sprintf((char*)CopyTmp,"echo \"%s * \\<Your\\ Password\\> \" > /etc/ppp/pap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
- system((char*)CopyTmp);
- memset(CopyTmp,0,sizeof(CopyTmp));
- if(strlen((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd)>0)
- sprintf((char*)CopyTmp,"echo \"%s * %s \" > /etc/ppp/chap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd);
- else
- sprintf((char*)CopyTmp,"echo \"%s * \\<Your\\ Password\\> \" > /etc/ppp/chap-secrets", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId);
- system((char*)CopyTmp);
- }
- result = PASS;
- }
- return result;
- }
- //==========================================
- // Check internet access status
- //==========================================
- int isReachableInternet(void)
- {
- int result = FAIL;
- FILE *fp;
- char cmd[512];
- char buf[512];
- char tmp[256];
- char isPppGetGw = FALSE;
- for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
- {
- sprintf(cmd, "ping -c 1 -w 3 -s 8 -I ppp0 %s", 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", tmp);
- if(strstr(buf,"100%") != NULL)
- {}
- else
- {
- result = PASS;
- break;
- }
- //DEBUG_INFO("%s",buf);
- //DEBUG_INFO("%s\n",tmp);
- }
- }
- }
- pclose(fp);
- }
-
- // Check ppp0 gateway
- if(result == TRUE)
- {
- fp = popen("ip route", "r");
- if(fp == NULL)
- result = FAIL;
- else
- {
- while(fgets(buf, sizeof(buf), fp) != NULL)
- {
- if((strstr(buf, "default") != NULL) && (strstr(buf, "ppp0") != NULL))
- {
- isPppGetGw = TRUE;
- break;
- }
- if(strstr(buf, "ppp0") != NULL)
- {
- break;
- }
- }
- if((isPppGetGw == FALSE) && (strstr(buf, "ppp0") != NULL))
- {
- sscanf(buf, "%s", tmp);
- DEBUG_INFO("ppp0 does not get default route gateway force assign to %s\n", tmp);
- sprintf(cmd,"route add default gw %s ppp0 &", tmp);
- system(cmd);
- }
- }
- pclose(fp);
- memset(buf, 0x00, sizeof(buf));
- }
- return result;
- }
- //==========================================
- // Unbind USB port
- //==========================================
- int isModuleUnbind(void)
- {
- int result = FAIL;
- FILE *fp;
- char cmd[256];
- strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/unbind");
- fp = popen(cmd, "r");
- if(fp == NULL)
- {
- result = FAIL;
- }
- else
- {
- result = PASS;
- #ifdef SystemLogMessage
- DEBUG_INFO("Unbind USB for dongle.\n");
- #endif
- }
- return result;
- }
- //==========================================
- // Bind USB port
- //==========================================
- int isModuleBind(void)
- {
- int result = FAIL;
- FILE *fp;
- char cmd[256];
- strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/bind");
- fp = popen(cmd, "r");
- if(fp == NULL)
- {
- result = FAIL;
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_INFO("Bind USB for dongle.\n");
- #endif
- result = PASS;
- }
- return result;
- }
- //==========================================
- // Dongle reset process
- //==========================================
- int rstModule(void)
- {
- int result = PASS;
- int uart;
- char rx[512];
- if((uart = openPort(portName[Dongle.Model])) != FAIL)
- {
- //==============================
- // Reset module
- //==============================
- switch(Dongle.Model)
- {
- case DONGLE_QUECTEL:
- default:
- if(at_command(uart, "at+cfun=1,1\r", rx) <= 0)
- {
- result = FAIL;
- }
-
- DEBUG_INFO("Dongle hardware reset...\n");
- break;
- case DONGLE_THALES:
- if(at_command(uart, "at+cfun=1,1\r", rx) <= 0)
- {
- result = FAIL;
- }
-
- DEBUG_INFO("Dongle hardware reset...\n");
- break;
- }
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("%s open fail.\n", portName[Dongle.Model]);
- #endif
- result = FAIL;
- }
- close(uart);
- sleep(40);
- return result;
- }
- //==========================================
- // AT command send/receive
- //==========================================
- int at_command(int uart, char* cmd, char* rx)
- {
- int len;
- //sleep(2); //required to make flush work, for some reason
- //DEBUG_INFO("cmd : %s \n", cmd);
- tcflush(uart,TCIOFLUSH);
- if(write(uart, cmd, strlen(cmd)) >= strlen(cmd))
- {
- usleep(500000);
- len = read(uart, rx, 512);
- //DEBUG_INFO("rx : %s \n", rx);
- }
- else
- {
- #ifdef SystemLogMessage
- DEBUG_ERROR("AT command %s response fail.\n", cmd);
- #endif
- }
- return len;
- }
- //==========================================
- // Dongle communication port open
- //==========================================
- int openPort(char *tty)
- {
- int uart = open(tty, O_RDWR | O_NOCTTY | O_NDELAY);
- if(uart!=FAIL)
- {
- if((set_interface_attribs(uart, B115200, 0) != PASS) || (set_blocking(uart, 0) != PASS))
- uart = FAIL;
- }
- return uart;
- }
- //==========================================
- // Port parameter set
- //==========================================
- int set_interface_attribs (int fd, int speed, int parity)
- {
- int result = FAIL;
- struct termios tty;
- memset (&tty, 0, sizeof tty);
- if (tcgetattr (fd, &tty) == 0)
- {
- cfsetospeed (&tty, speed);
- cfsetispeed (&tty, speed);
- tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; // 8-bit chars
- // disable IGNBRK for mismatched speed tests; otherwise receive break
- // as \000 chars
- tty.c_iflag &= ~IGNBRK; // disable break processing
- tty.c_lflag = 0; // no signaling chars, no echo,
- // no canonical processing
- tty.c_oflag = 0; // no remapping, no delays
- tty.c_cc[VMIN] = 0; // read doesn't block
- tty.c_cc[VTIME] = 10; // 1 seconds read timeout
- tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
- tty.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
- // enable reading
- tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
- tty.c_cflag |= parity;
- tty.c_cflag &= ~CSTOPB;
- tty.c_cflag &= ~CRTSCTS;
- if (tcsetattr (fd, TCSANOW, &tty) == 0)
- result = PASS;
- }
- return result;
- }
- int set_blocking (int fd, int should_block)
- {
- int result = FAIL;
- struct termios tty;
- memset (&tty, 0, sizeof tty);
- if (tcgetattr (fd, &tty) == 0)
- {
- tty.c_cc[VMIN] = should_block ? 1 : 0;
- tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
- if (tcsetattr (fd, TCSANOW, &tty) == 0)
- result = PASS;
- }
- return result;
- }
- //==========================================
- // Common routine
- //==========================================
- void trim_s(char *s, unsigned char len)
- {
- for(unsigned char i = 0 ; i < len; i++)
- {
- if (!((s[i]>='a') && (s[i]<='z')) && !((s[i]>='A') && (s[i]<='Z'))&& !((s[i]>='0') && (s[i]<='9')))
- {
- s[i] = s[i + 1];
- strncpy(s + i, s + i + 1, len);
- i -= 1;
- len -= 1;
- }
- }
- s[len + 1] = '\0';
- }
- void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
- {
- strncpy(dest, src + start, cnt);
- dest[cnt] = 0;
- }
- int csq2rssi(int csq)
- {
- int result = 0;
- switch(csq)
- {
- case 0 ... 31:
- result = -113+(2*csq);
- break;
- case 100 ... 191:
- result = -116 + (csq-102);
- break;
- case 99:
- case 199:
- default:
- result = csq;
- break;
- }
- return result;
- }
- void configNetworkMode(uint8_t actReq)
- {
- FILE *fpOrg = fopen("/root/ppp/ppp-on-dialer-org", "r");
- FILE *fpDst = fopen("/root/ppp/ppp-on-dialer", "w+");
- char str[512]={0};
- if(Dongle.Model == DONGLE_QUECTEL)
- {
- while (fgets(str, ARRAY_SIZE(str), fpOrg) != NULL)
- {
- fprintf(fpDst,"%s", str);
- if(strstr(str, "ATE1") != NULL)
- {
- fprintf(fpDst, " OK \"AT+QCFG=\\\"nwscanmode\\\",%d,1\" \\\n", actReq);
- }
- memset(str, 0x00, ARRAY_SIZE(str));
- }
- }
- else if(Dongle.Model == DONGLE_THALES)
- {
- while (fgets(str, ARRAY_SIZE(str), fpOrg) != NULL)
- {
- fprintf(fpDst,"%s", str);
- if(strstr(str, "ATE1") != NULL)
- {
- switch(actReq)
- {
- case NETWORK_MODE_GSM:
- fprintf(fpDst, " OK \"AT\\^SCFG=\\\"Radio\\/Band\\/2G\\\",\\\"0000000f\\\"\" \\\n");
- fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
- fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/3G\\\",\\\"0\\\"\" \\\n");
- fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
- fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/4G\\\",\\\"0\\\",\\\"0\\\"\" \\\n");
- break;
- case NETWORK_MODE_UMTS:
- fprintf(fpDst, " OK \"AT\\^SCFG=\\\"Radio\\/Band\\/2G\\\",\\\"0\\\"\" \\\n");
- fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
- fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/3G\\\",\\\"000400bf\\\"\" \\\n");
- fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
- fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/4G\\\",\\\"0\\\",\\\"0\\\"\" \\\n");
- break;
- case NETWORK_MODE_LTE:
- fprintf(fpDst, " OK \"AT\\^SCFG=\\\"Radio\\/Band\\/2G\\\",\\\"0\\\"\" \\\n");
- fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
- fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/3G\\\",\\\"0\\\"\" \\\n");
- fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
- fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/4G\\\",\\\"0a0e18df\\\",\\\"00000002000001a0\\\"\" \\\n");
- break;
- case NETWORK_MODE_AUTO:
- fprintf(fpDst, " OK \"AT\\^SCFG=\\\"Radio\\/Band\\/2G\\\",\\\"0000000f\\\"\" \\\n");
- fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
- fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/3G\\\",\\\"000400bf\\\"\" \\\n");
- fprintf(fpDst, " OK \\\\d\\\\d\\\\d \\\n");
- fprintf(fpDst, " \"\" \"AT\\^SCFG=\\\"Radio\\/Band\\/4G\\\",\\\"0a0e18df\\\",\\\"00000002000001a0\\\"\" \\\n");
- default:
- break;
- }
- }
- memset(str, 0x00, ARRAY_SIZE(str));
- }
- }
- fclose(fpOrg);
- fclose(fpDst);
- }
- //==========================================
- // Main process
- //==========================================
- int main(void)
- {
- unsigned char Previous_Module_Mode = 0xff;
- int connectStep=0;
- int preConnectStep=-1;
- int delayInterval = DefaultInterval;
- //==========================================
- //Initialization share memory
- //==========================================
- if(InitShareMemory() == FAIL)
- {
- DEBUG_ERROR("InitShareMemory NG\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
- }
- sleep(5);
- return 0;
- }
-
- DEBUG_INFO("========================================\n");
- DEBUG_INFO("Latest Firmware Version : [ %s ].\n", Version_And_Date[0]);
- DEBUG_INFO("Latest Upgrade Date : [ %s ].\n", Version_And_Date[1]);
-
- for(;;)
- {
- if(Previous_Module_Mode != ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled)
- {
- Previous_Module_Mode = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled;
- DEBUG_INFO("========================================\n");
- DEBUG_INFO("4G module : [ %s ]\n", (ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled?"Enable":"Disable"));
- }
- if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled)
- {
- // Enable 4G
- switch(connectStep)
- {
- case CONNECT_STEP_READ_MODULE_INFO:
- if(preConnectStep != connectStep)
- {
- preConnectStep = connectStep;
- DEBUG_INFO("----------------------------------------\n");
- DEBUG_INFO("Into CONNECT_STEP_READ_MODULE_INFO.\n");
- }
- if(isReadInfo(connectStep) == PASS)
- {
- if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, Dongle.MODELNAME) != 0)
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName));
- memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, Dongle.MODELNAME, sizeof Dongle.MODELNAME);
- if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, Dongle.REVISION) != 0)
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer));
- memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, Dongle.REVISION, sizeof Dongle.REVISION);
- if(strcmp((char*)ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, Dongle.REVISION) != 0)
- memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev));
- memcpy(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, Dongle.REVISION, sizeof Dongle.REVISION);
- if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, Dongle.IMEI) != 0)
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei));
- memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, Dongle.IMEI, sizeof Dongle.IMEI);
- DEBUG_INFO("Device MANUFACTURER: %s\n", Dongle.MANUFACTURER);
- DEBUG_INFO("Device MODEL: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
- DEBUG_INFO("Device REVISION: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
- DEBUG_INFO("Device IMEI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
- Dongle.cnt_ReadSimInfoFail = 0;
- connectStep = CONNECT_STEP_READ_SIM_INFO;
- delayInterval = DefaultInterval;
- }
- else
- {
- DEBUG_ERROR("Device info read error fail: %d\n", Dongle.cnt_ReadInfoFail);
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
- Dongle.MODE = NET_MODE_NO_SERVICE;
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
- memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
- Dongle.cnt_ReadInfoFail++;
- if(Dongle.cnt_ReadInfoFail > 3)
- {
- system("killall 4GDetection");
- system("killall pppd");
- sleep(2);
- Dongle.cnt_ReadInfoFail = 0;
- rstModule();
- connectStep = CONNECT_STEP_CHECK_MODULE;
- }
- delayInterval = CheckModemInfoInterval;
- }
- break;
- case CONNECT_STEP_READ_SIM_INFO:
- if(preConnectStep != connectStep)
- {
- preConnectStep = connectStep;
- DEBUG_INFO("----------------------------------------\n");
- DEBUG_INFO("Into CONNECT_STEP_READ_SIM_INFO.\n");
- }
- if(isReadSimInfo() == PASS)
- {
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 1;
- if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID) != 0)
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid));
- memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, Dongle.ICCID, sizeof Dongle.ICCID);
- if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI) != 0)
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi));
- memcpy(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, Dongle.IMSI, sizeof Dongle.IMSI);
- DEBUG_INFO("Device IMSI: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
- DEBUG_INFO("Device ICCID: %.20s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
- DEBUG_INFO("TelcomSimStatus: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus);
- Dongle.cnt_ReadSimInfoFail = 0;
- connectStep = CONNECT_STEP_CONNECTING;
- delayInterval = DefaultInterval;
- }
- else
- {
- DEBUG_ERROR("SIM card info read error fail: %d\n", Dongle.cnt_ReadSimInfoFail);
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
- Dongle.cnt_ReadSimInfoFail ++;
- if(Dongle.cnt_ReadSimInfoFail > 3)
- {
- system("killall 4GDetection");
- system("killall pppd");
- sleep(2);
- Dongle.cnt_ReadSimInfoFail = 0;
- rstModule();
- connectStep = CONNECT_STEP_CHECK_MODULE;
- }
- delayInterval = CheckSimInterval;
- }
- break;
- case CONNECT_STEP_CONNECTING:
- if(preConnectStep != connectStep)
- {
- preConnectStep = connectStep;
- DEBUG_INFO("----------------------------------------\n");
- DEBUG_INFO("Into CONNECT_STEP_CONNECTING.\n");
- }
- if(isPppUp() == PASS)
- {
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 1;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 0;
- DEBUG_INFO("PPP IP: %s\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
- DEBUG_INFO("Telecom network connection: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn);
- DEBUG_INFO("Apn connnection status: %s\n", (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == 0)? "Connected":"Disconnected");
- Dongle.cnt_pppFail = 0;
- connectStep = CONNECT_STEP_CONNECTED;
- delayInterval = DefaultInterval;
- }
- else
- {
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
- if(Load4gConfiguration() == FAIL)
- {
- DEBUG_ERROR("4G configuration value NG.\n");
- if(ShmStatusCodeData!=NULL)
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail=1;
- }
- }
- else
- {
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0 , sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
- DEBUG_INFO("Apn connnection status: %s\n", (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi == 0)? "Connected":"Disconnected");
- Dongle.cnt_pppFail++;
- if(Dongle.cnt_pppFail > 3)
- {
- system("killall 4GDetection");
- system("killall pppd");
- sleep(2);
- Dongle.cnt_pppFail = 0;
- rstModule();
- connectStep = CONNECT_STEP_CHECK_MODULE;
- }
- system("killall 4GDetection");
- system("killall pppd");
- sleep(2);
- /*
- * TODO:
- * 1. Act priority check
- */
- configNetworkMode(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkType);
- if(Dongle.Model == DONGLE_QUECTEL)
- {
- system("/root/ppp/4GDetection /dev/ttyUSB3 &");
- printf("4GDetection for primary device.\n");
- }
- else if(Dongle.Model == DONGLE_THALES)
- {
- system("/root/ppp/4GDetection /dev/ttyACM0 &");
- printf("4GDetection for second device.\n");
- }
- else
- {}
- }
- delayInterval = CheckConnectionInterval;
- }
- break;
- case CONNECT_STEP_CONNECTED:
- if(preConnectStep != connectStep)
- {
- preConnectStep = connectStep;
- DEBUG_INFO("----------------------------------------\n");
- DEBUG_INFO("Into CONNECT_STEP_CONNECTED.\n");
- }
- if((isReadInfo(connectStep) == PASS) && (isReachableInternet() == PASS))
- {
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
- Dongle.cnt_InternetFail = 0;
- DEBUG_INFO("Dongle internet valid result: Pass\n");
- delayInterval = CheckInternetInterval;
- }
- else
- {
- DEBUG_INFO("Dongle internet valid result: Fail %d time\n", Dongle.cnt_InternetFail);
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
- Dongle.cnt_InternetFail++;
- if((Dongle.cnt_InternetFail > 3) && !ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
- {
- system("killall 4GDetection");
- system("killall pppd");
- sleep(2);
- Dongle.cnt_InternetFail = 0;
- rstModule();
- connectStep = CONNECT_STEP_CHECK_MODULE;
- }
- delayInterval = DisconnInterval;
- }
- if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi != csq2rssi(Dongle.CSQ))
- {
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = csq2rssi(Dongle.CSQ);
- DEBUG_INFO("RSSI: %d dBm (CSQ:%d)\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi, Dongle.CSQ);
- }
- if(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode != Dongle.MODE)
- {
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = Dongle.MODE;
- DEBUG_INFO("Device MODE: %d\n", ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode);
- DEBUG_INFO("Device Act: %s\n", Dongle.act);
- DEBUG_INFO("Device Band: %s\n", Dongle.band);
- DEBUG_INFO("Device Channel: %d\n", Dongle.channel);
- }
- if(Dongle.Model == DONGLE_QUECTEL)
- {
- DEBUG_INFO("TX counter: %d bytes\n", Dongle.bytesTx);
- DEBUG_INFO("RX counter: %d bytes\n", Dongle.bytesRx);
- DEBUG_INFO("Delta counter: %d bytes\n", (Dongle.bytesTx+Dongle.bytesRx)-Dongle.lastCounter);
- Dongle.lastCounter = (Dongle.bytesTx+Dongle.bytesRx);
- }
- DEBUG_INFO("++++++++++++++++++++++++++++++++++++++++\n");
- break;
- default:
- case CONNECT_STEP_CHECK_MODULE:
- if(preConnectStep != connectStep)
- {
- preConnectStep = connectStep;
- DEBUG_INFO("----------------------------------------\n");
- DEBUG_INFO("Into CONNECT_STEP_CHECK_MODULE.\n");
- }
- if(((Dongle.Model = Check4GModem()) != FAIL))
- {
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
- ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
- Dongle.cnt_SearchModuleFail = 0;
- connectStep = CONNECT_STEP_READ_MODULE_INFO;
- delayInterval = DefaultInterval;
- }
- else
- {
- DEBUG_ERROR("Device search error fail: %d\n", Dongle.cnt_SearchModuleFail);
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 1;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi = 1;
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
- memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
- Dongle.cnt_SearchModuleFail++;
- if(Dongle.cnt_SearchModuleFail > 3)
- {
- DEBUG_ERROR("4G Module was broken.\n");
- if(isModuleUnbind() == PASS)
- {
- isModuleBind();
- }
- ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 1;
- Dongle.cnt_SearchModuleFail = 0;
- }
- delayInterval = CheckModemInterval;
- }
- break;
- }
- }
- else
- {
- // Disable 4G
- ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = 0;
- ShmStatusCodeData->InfoCode.InfoEvents.bits.ApnDisconnectVia4Gi= 0;
- ShmStatusCodeData->AlarmCode.AlarmEvents.bits.Telecom4GModuleCommFail = 0;
- ShmStatusCodeData->FaultCode.FaultEvents.bits.Telecom4GModuleBroken = 0;
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomNetworkConn = 0;
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi = 0;
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus = 0;
- ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode = 0;
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimIccid);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer);
- memset(ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev, 0, sizeof ShmSysConfigAndInfo->SysInfo.TelcomModemFwRev);
- memset(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName, 0, sizeof ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModelName);
- if(system("pidof -s 4GDetection > /dev/null") == 0)
- {
- DEBUG_INFO("4GDetection running, stop it.\n");
- system("killall 4GDetection");
- }
- if(system("pidof -s pppd > /dev/null") == 0)
- {
- DEBUG_INFO("pppd running, stop it.\n");
- system("killall pppd");
- }
- if(isPppUp() == PASS)
- {
- system("ifconfig ppp0 down");
- }
- connectStep = CONNECT_STEP_CHECK_MODULE;
- delayInterval = DefaultInterval;
- }
- sleep(delayInterval);
- }
- return 0;
- }
|