Module_Wifi.c 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762
  1. /*
  2. * Module_Wifi.c
  3. *
  4. * Created on: 2020-01-14
  5. * Update on: 2020-07-23
  6. * Author: Jerry Wang, Eason Yang, Folus Wen
  7. * Version: D0.07
  8. */
  9. #include <sys/time.h>
  10. #include <sys/timeb.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <sys/types.h>
  14. #include <sys/ioctl.h>
  15. #include <sys/socket.h>
  16. #include <sys/ipc.h>
  17. #include <sys/shm.h>
  18. #include <sys/shm.h>
  19. #include <sys/mman.h>
  20. #include <linux/wireless.h>
  21. #include <arpa/inet.h>
  22. #include <netinet/in.h>
  23. #include <unistd.h>
  24. #include <stdarg.h>
  25. #include <stdio.h> /*標準輸入輸出定義*/
  26. #include <stdlib.h> /*標準函數庫定義*/
  27. #include <unistd.h> /*Unix 標準函數定義*/
  28. #include <fcntl.h> /*檔控制定義*/
  29. #include <termios.h> /*PPSIX 終端控制定義*/
  30. #include <errno.h> /*錯誤號定義*/
  31. #include <errno.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <ctype.h>
  35. #include <ifaddrs.h>
  36. #include <sqlite3.h>
  37. #include "define.h"
  38. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  39. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  40. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  41. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  42. #define PASS 1
  43. #define FAIL -1
  44. #define ON 1
  45. #define OFF 0
  46. #define true 1
  47. #define false 0
  48. #define IP_TYPE_DHCP 0
  49. #define IP_TYPE_STATIC 1
  50. #define AUTH_TYPE_NONE 0
  51. #define AUTH_TYPE_WEP 1
  52. #define AUTH_TYPE_WPAPSK 2
  53. #define AUTH_TYPE_WPA2PSK 3
  54. #define WIFI_MODE_DISABLE 0
  55. #define WIFI_MODE_STA 1
  56. #define WIFI_MODE_AP 2
  57. #define WIFI_MODE_ADHOC 3
  58. #define STA_STATE_INIT 0
  59. #define STA_STATE_DEVICE_DETECT 1
  60. #define STA_STATE_AP_CONNECTED 2
  61. #define STA_STATE_INTERNET_CHECKING 3
  62. #define AP_STATE_INIT 0
  63. #define AP_STATE_DEVICE_DETECT 1
  64. #define AP_STATE_AP_START 2
  65. #define AP_STATE_SERVER_LISTEN 3
  66. #define LISTEN_PORT_UDP 54088
  67. #define LISTEN_PORT_TCP 54089
  68. #define CONNECTION_LIMIT 1
  69. #define MtdBlockSize 0x600000
  70. #define REGISTER 0x01
  71. #define CLEAR_REGISTER 0x02
  72. #define UBLOX
  73. //#define MT7601U
  74. #define DB_FILE "/Storage/ChargeLog/puk_pin_list.db"
  75. enum WIFI_COMMAND
  76. {
  77. WIFI_CMD_LOGIN_SIGNIN = 0x01,
  78. WIFI_CMD_LOGIN_AUTH_PUK = 0x02,
  79. WIFI_CMD_LOGIN_GET_USER_ID = 0x03,
  80. WIFI_CMD_PROTOCOL_VERSION = 0x04,
  81. WIFI_CMD_GET_MODEL_NAME = 0x0d,
  82. WIFI_CMD_GET_SERIAL_NUMBER = 0x0e,
  83. WIFI_CMD_GET_SYSTEM_TIME = 0x0f,
  84. WIFI_CMD_GET_WHITE_CARD = 0x12,
  85. WIFI_CMD_GET_SYS_ALARM_CODE = 0x15,
  86. WIFI_CMD_GET_SYS_ID = 0x16,
  87. WIFI_CMD_GET_RATING_CURRENT = 0x17,
  88. WIFI_CMD_GET_VER_RFS = 0x1e,
  89. WIFI_CMD_GET_VER_PRIMARY_CSU = 0x1f,
  90. WIFI_CMD_GET_VER_LCM = 0x20,
  91. WIFI_CMD_GET_PSU_COUNT = 0x21,
  92. WIFI_CMD_GET_VER_PSU = 0x22,
  93. WIFI_CMD_GET_VER_AUX_POWER = 0x23,
  94. WIFI_CMD_GET_VER_FAN = 0x24,
  95. WIFI_CMD_GET_VER_RELAY = 0x25,
  96. WIFI_CMD_GET_VER_TELECOMM = 0x26,
  97. WIFI_CMD_GET_EVSE_CONFIG = 0x30,
  98. WIFI_CMD_GET_GUN_STS = 0x32,
  99. WIFI_CMD_GET_GUN_USER = 0x33,
  100. WIFI_CMD_GET_SCHEDULE = 0x36,
  101. WIFI_CMD_GET_NETWORK_STS = 0x41,
  102. WIFI_CMD_GET_ETH_DHCP = 0x42,
  103. WIFI_CMD_GET_ETH_MAC = 0x44,
  104. WIFI_CMD_GET_ETH_IP = 0x45,
  105. WIFI_CMD_GET_ETH_SUBMASK = 0x47,
  106. WIFI_CMD_GET_ETH_GATEWAY = 0x49,
  107. WIFI_CMD_GET_WIFI_MODE = 0x54,
  108. WIFI_CMD_GET_WIFI_SSID = 0x56,
  109. WIFI_CMD_GET_WIFI_PASSWD = 0x58,
  110. WIFI_CMD_GET_WIFI_RSSI = 0x5a,
  111. WIFI_CMD_GET_WIFI_DHCP = 0x5b,
  112. WIFI_CMD_GET_WIFI_MAC = 0x5d,
  113. WIFI_CMD_GET_WIFI_IP = 0x5e,
  114. WIFI_CMD_GET_WIFI_NETMASK = 0x60,
  115. WIFI_CMD_GET_WIFI_GATEWAY = 0x62,
  116. WIFI_CMD_GET_TELE_APN = 0x6b,
  117. WIFI_CMD_GET_TELE_RSSI = 0x6d,
  118. WIFI_CMD_GET_TELE_CHAP_ID = 0x6e,
  119. WIFI_CMD_GET_TELE_CHAP_PASSWD = 0x70,
  120. WIFI_CMD_GET_TELE_IMEI = 0x72,
  121. WIFI_CMD_GET_TELE_IMSI = 0x73,
  122. WIFI_CMD_GET_TELE_SIM_STS = 0x74,
  123. WIFI_CMD_GET_TELE_MODE = 0x75,
  124. WIFI_CMD_GET_TELE_IP = 0x76,
  125. WIFI_CMD_SET_SYSTEM_TIME = 0x10,
  126. WIFI_CMD_SET_WHITE_CARD = 0x13,
  127. WIFI_CMD_SET_FACTORY_RESET = 0x14,
  128. WIFI_CMD_SET_EVSE_CONFIG = 0x31,
  129. WIFI_CMD_SET_CHARGING_START = 0x34,
  130. WIFI_CMD_SET_CHARGING_STOP = 0x35,
  131. WIFI_CMD_SET_SCHEDULE = 0x37,
  132. WIFI_CMD_SET_ETH_CONFIG = 0x43,
  133. WIFI_CMD_SET_ETH_IP = 0x46,
  134. WIFI_CMD_SET_ETH_SUBMASK = 0x48,
  135. WIFI_CMD_SET_ETH_GATEWAY = 0x4a,
  136. WIFI_CMD_SET_WIFI_MODE = 0x55,
  137. WIFI_CMD_SET_WIFI_SSID = 0x57,
  138. WIFI_CMD_SET_WIFI_PASSWD = 0x59,
  139. WIFI_CMD_SET_WIFI_DHCP = 0x5c,
  140. WIFI_CMD_SET_WIFI_IP = 0x5f,
  141. WIFI_CMD_SET_WIFI_NETMASK = 0x61,
  142. WIFI_CMD_SET_WIFI_GATEWAY = 0x63,
  143. WIFI_CMD_SET_TELE_APN = 0x6c,
  144. WIFI_CMD_SET_TELE_CHAP_ID = 0x6f,
  145. WIFI_CMD_SET_TELE_CHAP_PASSWD = 0x71,
  146. WIFI_CMD_SET_TELE_IP = 0x77,
  147. WIFI_CMD_SET_CONFIRM = 0x81,
  148. WIFI_CMD_SET_DISCARD = 0x82,
  149. WIFI_CMD_UNKNOW = 0xfe
  150. };
  151. enum LOGIN_ROLE
  152. {
  153. ROLE_UNKNOWN = 0x00,
  154. ROLE_MANAGER = 0x01,
  155. ROLE_USER = 0x02
  156. };
  157. struct SYS_FLAG
  158. {
  159. uint8_t isPkill;
  160. }Sys_Flag;
  161. struct interface_info
  162. {
  163. int ipType;
  164. int authType;
  165. int rssi;
  166. int cnt_InternetFail;
  167. char *currentInterface;
  168. char currentSSID[256];
  169. char currentPasswd[256];
  170. char currentMAC[24];
  171. char currentIPAddr[24];
  172. char currentNetmask[24];
  173. char currentGateway[24];
  174. char staticIPAddr[24];
  175. }Wifi;
  176. struct Display_Request
  177. {
  178. unsigned char isShowed_IF_info:1;
  179. unsigned char isShowed_IP_info:1;
  180. }dispReq;
  181. struct auth_info
  182. {
  183. int auth_type;
  184. unsigned char ssid[256];
  185. unsigned char passwd[256];
  186. }Wifi_A;
  187. struct WIFI_LOGIN_INFO
  188. {
  189. uint8_t puk[8];
  190. uint8_t loginRole;
  191. uint8_t loginId[32];
  192. }wifi_login_info;
  193. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  194. struct StatusCodeData *ShmStatusCodeData;
  195. struct OCPP16Data *ShmOCPP16Data;
  196. struct PsuData *ShmPsuData;
  197. struct SysConfigData SysConfigTemporarily;
  198. int isFindInterface();
  199. int getInterfaceInfo();
  200. int isReachableInternet();
  201. int getLinkQuality();
  202. char* getTimeString(void);
  203. int setWPAconf();
  204. int restartWPA();
  205. void refreshUSB();
  206. void getParameters();
  207. int Wifi_module_sts = STA_STATE_INIT;
  208. int isSetWPA_OK = false;
  209. int cnt_pingDNS_Fail;
  210. int cnt_getAP_Fail;
  211. int cnt_getIP_Fail;
  212. sqlite3 *localDb;
  213. void trim(char *s);
  214. int mystrcmp(char *p1,char *p2);
  215. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  216. void split(char **arr, char *str, const char *del);
  217. int isValidCheckSum(uint8_t *message);
  218. char *Support_InterfaceSTA[2] = {"mlan0", "wlan0"};
  219. char *Support_InterfaceAP[1] = {"uap0"};
  220. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  221. int protocol_Version [] = {0,6,0};
  222. int StoreLogMsg(const char *fmt, ...)
  223. {
  224. char Buf[4096+256];
  225. char buffer[4096];
  226. time_t CurrentTime;
  227. struct tm *tm;
  228. va_list args;
  229. va_start(args, fmt);
  230. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  231. va_end(args);
  232. memset(Buf,0,sizeof(Buf));
  233. CurrentTime = time(NULL);
  234. tm=localtime(&CurrentTime);
  235. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]Wifi_SystemLog",
  236. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  237. buffer,
  238. tm->tm_year+1900,tm->tm_mon+1);
  239. #ifdef SystemLogMessage
  240. system(Buf);
  241. #endif
  242. #ifdef ConsloePrintLog
  243. 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);
  244. #endif
  245. return rc;
  246. }
  247. int DiffTimeb(struct timeb ST, struct timeb ET)
  248. {
  249. //return milli-second
  250. unsigned int StartTime,StopTime;
  251. StartTime=(unsigned int)ST.time;
  252. StopTime=(unsigned int)ET.time;
  253. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  254. }
  255. //==========================================
  256. // Init all share memory
  257. //==========================================
  258. int InitShareMemory()
  259. {
  260. int result = PASS;
  261. int MeterSMId;
  262. //creat ShmSysConfigAndInfo
  263. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  264. {
  265. #ifdef SystemLogMessage
  266. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  267. #endif
  268. result = FAIL;
  269. }
  270. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  271. {
  272. #ifdef SystemLogMessage
  273. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  274. #endif
  275. result = FAIL;
  276. }
  277. else
  278. {}
  279. //creat ShmStatusCodeData
  280. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  281. {
  282. #ifdef SystemLogMessage
  283. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  284. #endif
  285. result = FAIL;
  286. }
  287. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  288. {
  289. #ifdef SystemLogMessage
  290. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  291. #endif
  292. result = FAIL;
  293. }
  294. else
  295. {}
  296. //creat ShmOCPP16Data
  297. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), 0777)) < 0)
  298. {
  299. #ifdef SystemLogMessage
  300. DEBUG_ERROR("shmget ShmOCPP16Data NG");
  301. #endif
  302. result = FAIL;
  303. }
  304. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  305. {
  306. #ifdef SystemLogMessage
  307. DEBUG_ERROR("shmat ShmOCPP16Data NG");
  308. #endif
  309. result = FAIL;
  310. }
  311. else
  312. {}
  313. //creat ShmPsuData
  314. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), 0777)) < 0)
  315. {
  316. #ifdef SystemLogMessage
  317. DEBUG_ERROR("shmget ShmPsuData NG \n");
  318. #endif
  319. result = FAIL;
  320. }
  321. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  322. {
  323. #ifdef SystemLogMessage
  324. DEBUG_ERROR("shmat ShmPsuData NG \n");
  325. #endif
  326. result = FAIL;
  327. }
  328. memset(ShmPsuData,0,sizeof(struct PsuData));
  329. return result;
  330. }
  331. //==========================================
  332. // Get parameters from shared memory
  333. //==========================================
  334. void getParameters(void)
  335. {
  336. DEBUG_INFO("============= Configuration ===============\r\n");
  337. strcpy((char*)Wifi_A.ssid, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  338. DEBUG_INFO("Wifi SSID: %s\n", Wifi_A.ssid);
  339. strcpy((char*)Wifi_A.passwd, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  340. DEBUG_INFO("Wifi Password: %s\n", Wifi_A.passwd);
  341. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress != NULL)
  342. strcpy((char*)Wifi.staticIPAddr, (const char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  343. DEBUG_INFO("Wifi Static IP: %s\n", Wifi.staticIPAddr);
  344. Wifi.ipType = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient;
  345. DEBUG_INFO("DHCP client flag: %d\n", Wifi.ipType);
  346. DEBUG_INFO("===========================================\r\n");
  347. }
  348. //==========================================
  349. // Refresh USB interface
  350. //==========================================
  351. void refreshUSB(void)
  352. {
  353. FILE *fp;
  354. char cmd[256];
  355. // Get IP address & net mask
  356. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/unbind");
  357. fp = popen(cmd, "r");
  358. sleep(2);
  359. pclose(fp);
  360. strcpy(cmd, "echo '1-1'> /sys/bus/usb/drivers/usb/bind");
  361. fp = popen(cmd, "r");
  362. sleep(5);
  363. pclose(fp);
  364. }
  365. //==========================================
  366. // Check wifi interface status
  367. //==========================================
  368. int isFindInterface()
  369. {
  370. int result = FAIL;
  371. struct ifaddrs *ifaddr, *ifa;
  372. //refreshUSB();
  373. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == WIFI_MODE_AP)
  374. {
  375. if (getifaddrs(&ifaddr) != FAIL)
  376. {
  377. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  378. {
  379. if (ifa->ifa_addr == NULL ||
  380. ifa->ifa_addr->sa_family != AF_PACKET) continue;
  381. for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceAP);idx++)
  382. {
  383. if(mystrcmp(ifa->ifa_name, Support_InterfaceAP[idx]) == PASS)
  384. {
  385. Wifi.currentInterface = ifa->ifa_name;
  386. result = PASS;
  387. }
  388. }
  389. }
  390. freeifaddrs(ifaddr);
  391. freeifaddrs(ifa);
  392. }
  393. }
  394. else
  395. {
  396. if (getifaddrs(&ifaddr) != FAIL)
  397. {
  398. for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next)
  399. {
  400. if (ifa->ifa_addr == NULL ||
  401. ifa->ifa_addr->sa_family != AF_PACKET) continue;
  402. for(int idx=0;idx<ARRAY_SIZE(Support_InterfaceSTA);idx++)
  403. {
  404. if(mystrcmp(ifa->ifa_name, Support_InterfaceSTA[idx]) == PASS)
  405. {
  406. Wifi.currentInterface = ifa->ifa_name;
  407. result = PASS;
  408. }
  409. }
  410. }
  411. freeifaddrs(ifaddr);
  412. freeifaddrs(ifa);
  413. }
  414. }
  415. return result;
  416. }
  417. //==========================================
  418. // Get wifi interface info
  419. //==========================================
  420. int getInterfaceInfo()
  421. {
  422. int result = PASS;
  423. FILE *fp;
  424. char cmd[256];
  425. char buf[512];
  426. char tmp[512];
  427. // Get IP address & net mask
  428. strcpy(cmd, "ifconfig ");
  429. strcat(cmd, Wifi.currentInterface);
  430. fp = popen(cmd, "r");
  431. if(fp == NULL)
  432. result = FAIL;
  433. else
  434. {
  435. while(fgets(buf, sizeof(buf), fp) != NULL)
  436. {
  437. if(strstr(buf, "HWaddr") > 0)
  438. {
  439. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  440. strcpy(Wifi.currentMAC, tmp);
  441. }
  442. if(strstr(buf, "inet addr:") > 0)
  443. {
  444. sscanf(buf, "%*s%s", tmp);
  445. substr(Wifi.currentIPAddr, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  446. sscanf(buf, "%*s%*s%*s%s", tmp);
  447. substr(Wifi.currentNetmask, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  448. }
  449. }
  450. }
  451. pclose(fp);
  452. // Get gateway
  453. fp = popen("ip route", "r");
  454. if(fp == NULL)
  455. result = FAIL;
  456. else
  457. {
  458. while(fgets(buf, sizeof(buf), fp) != NULL)
  459. {
  460. if(strncmp(buf, "default", strlen("default")) == 0)
  461. break;
  462. }
  463. sscanf(buf, "%*s%*s%s", tmp);
  464. substr(Wifi.currentGateway, tmp, 0, strlen(tmp));
  465. }
  466. pclose(fp);
  467. if(!dispReq.isShowed_IF_info)
  468. {
  469. DEBUG_INFO("MAC: %s\n", Wifi.currentMAC);
  470. dispReq.isShowed_IF_info = true;
  471. }
  472. if(!dispReq.isShowed_IP_info)
  473. {
  474. DEBUG_INFO("IP address: %s\n", Wifi.currentIPAddr);
  475. DEBUG_INFO("Net mask: %s\n", Wifi.currentNetmask);
  476. DEBUG_INFO("Default gateway: %s\n", Wifi.currentGateway);
  477. dispReq.isShowed_IP_info = true;
  478. }
  479. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, (const char*)Wifi.currentIPAddr);
  480. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, (const char*)Wifi.currentNetmask);
  481. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, (const char*)Wifi.currentGateway);
  482. strcpy((char*)ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, (const char*)Wifi.currentMAC);
  483. return result;
  484. }
  485. //==========================================
  486. // Check if module connect to wifi AP
  487. //==========================================
  488. int isConnectAP()
  489. {
  490. int result = PASS;
  491. FILE *fp;
  492. char cmd[256];
  493. char buf[512];
  494. // Get IP address & net mask
  495. strcpy(cmd, "iwconfig ");
  496. strcat(cmd, Wifi.currentInterface);
  497. fp = popen(cmd, "r");
  498. if(fp == NULL)
  499. result = FAIL;
  500. else
  501. {
  502. while(fgets(buf, sizeof(buf), fp) != NULL)
  503. {
  504. if(strstr(buf, "Not-Associated") > 0)
  505. {
  506. result = FAIL;
  507. DEBUG_INFO("AP check: Not-Associated.\n");
  508. }
  509. }
  510. }
  511. pclose(fp);
  512. return result;
  513. }
  514. //==========================================
  515. // Check internet access status
  516. //==========================================
  517. int isReachableInternet()
  518. {
  519. int result = FAIL;
  520. FILE *fp;
  521. char cmd[256];
  522. char buf[512];
  523. //char tmp[512];
  524. if(ShmOCPP16Data->OcppConnStatus == PASS)
  525. {
  526. result = PASS;
  527. }
  528. else
  529. {
  530. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  531. {
  532. sprintf(cmd, "ping -c 1 -w 3 -I %s %s", Wifi.currentInterface, valid_Internet[idx]);
  533. fp = popen(cmd, "r");
  534. if(fp != NULL)
  535. {
  536. while(fgets(buf, sizeof(buf), fp) != NULL)
  537. {
  538. if(strstr(buf, "transmitted") > 0)
  539. {
  540. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  541. if(strstr(buf,"100%") != NULL)
  542. {
  543. }
  544. else
  545. {
  546. result = PASS;
  547. }
  548. //DEBUG_INFO("%s",buf);
  549. //DEBUG_INFO("%s\n",tmp);
  550. }
  551. }
  552. }
  553. pclose(fp);
  554. }
  555. }
  556. return result;
  557. }
  558. //==========================================
  559. // Check module soft AP is start up
  560. //==========================================
  561. int isStartUpAP()
  562. {
  563. int result = FAIL;
  564. FILE *fp;
  565. char cmd[256];
  566. char buf[512];
  567. // Get IP address & net mask
  568. strcpy(cmd, "/root/uaputl sys_cfg_bss_status");
  569. fp = popen(cmd, "r");
  570. if(fp != NULL)
  571. {
  572. while(fgets(buf, sizeof(buf), fp) != NULL)
  573. {
  574. if(strstr(buf, "BSS status = started") > 0)
  575. {
  576. result = PASS;
  577. DEBUG_INFO("AP already start up.\n");
  578. }
  579. }
  580. }
  581. pclose(fp);
  582. return result;
  583. }
  584. //==========================================
  585. // Get wifi connection quality
  586. //==========================================
  587. int getLinkQuality()
  588. {
  589. int result = FAIL;
  590. FILE *fp;
  591. char cmd[256];
  592. char buf[512];
  593. char tmp[512];
  594. strcpy(cmd, "iwconfig ");
  595. strcat(cmd, Wifi.currentInterface);
  596. fp = popen(cmd, "r");
  597. if(fp == NULL)
  598. result = FAIL;
  599. else
  600. {
  601. while(fgets(buf, sizeof(buf), fp) != NULL)
  602. {
  603. if(strstr(buf, "Signal level") > 0)
  604. {
  605. sscanf(buf, "%*s%*s%*s%s", tmp);
  606. substr(tmp, tmp, strspn(tmp, "Signal level="), strlen(buf)-strspn(tmp, "Signal level="));
  607. Wifi.rssi = atoi(tmp);
  608. result = PASS;
  609. }
  610. }
  611. }
  612. pclose(fp);
  613. return result;
  614. }
  615. //==========================================
  616. // WPA parameter set
  617. //==========================================
  618. int setWPAconf()
  619. {
  620. int result = FAIL;
  621. char cmdBuf[512];
  622. FILE *pFile;
  623. char buffer[500]="ctrl_interface=/var/run/wpa_supplicant\nupdate_config=1\n";
  624. // Stop dhcp client or server
  625. sprintf(cmdBuf, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  626. system(cmdBuf);
  627. sprintf(cmdBuf, "pgrep -f \"udhcpd\" | xargs kill");
  628. system(cmdBuf);
  629. // Clear wifi configuration file
  630. system("cat /dev/null > /etc/wpa.conf");
  631. system("cat /dev/null > /etc/hostapd.conf");
  632. switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
  633. {
  634. case WIFI_MODE_STA:
  635. // Fill out configuration value depend on share memory setting
  636. if(strlen((const char*)Wifi_A.passwd)>0)
  637. {
  638. if(strlen((const char*)Wifi_A.passwd)>=8)
  639. {
  640. strcat(buffer, "network={\n ssid=\"");
  641. strcat(buffer, (char*)Wifi_A.ssid);
  642. strcat(buffer, "\"\n key_mgmt=WPA-EAP WPA-PSK \n");
  643. strcat(buffer, " psk=\"");
  644. strcat(buffer, (char*)Wifi_A.passwd);
  645. strcat(buffer, "\"\n}\n\n");
  646. }
  647. if(strlen((const char*)Wifi_A.passwd)==5 || strlen((const char*)Wifi_A.passwd)==13)
  648. {
  649. strcat(buffer, "network={\n ssid=\"");
  650. strcat(buffer, (char*)Wifi_A.ssid);
  651. strcat(buffer, "\"\n key_mgmt=NONE\n");
  652. strcat(buffer, " wep_key0=\"");
  653. strcat(buffer, (char*)Wifi_A.passwd);
  654. strcat(buffer, "\"\n auth_alg=OPEN SHARED\n");
  655. strcat(buffer, "\n}\n\n");
  656. }
  657. strcat(buffer, "network={\n ssid=\"");
  658. strcat(buffer, (char*)Wifi_A.ssid);
  659. strcat(buffer, "\"\n key_mgmt=NONE");
  660. strcat(buffer, "\n}");
  661. }
  662. else
  663. {
  664. strcat(buffer, "network={\n ssid=\"");
  665. strcat(buffer, (char*)Wifi_A.ssid);
  666. strcat(buffer, "\"\n key_mgmt=NONE");
  667. strcat(buffer, "\n}");
  668. }
  669. // Configuration value write to /etc/wpa.conf
  670. pFile = fopen("/etc/wpa.conf","w");
  671. fwrite(buffer,strlen(buffer), 1, pFile);
  672. fclose(pFile);
  673. // Restart wpa_supplicant
  674. sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
  675. system(cmdBuf);
  676. #ifdef UBLOX
  677. sprintf(cmdBuf, "mlanutl mlan0 hostcmd /root/u-blox_tools/ed_mac_ctrl_V1_8801.conf ed_mac_ctrl");
  678. system(cmdBuf);
  679. sprintf(cmdBuf, "mlanutl mlan0 regrdwr 2 0x70");
  680. system(cmdBuf);
  681. #endif
  682. sprintf(cmdBuf, "/root/wpa_supplicant -i %s -c /etc/wpa.conf -B", Wifi.currentInterface);
  683. system(cmdBuf);
  684. // Stop wifi AP
  685. sprintf(cmdBuf, "pgrep -f \"hostapd\" | xargs kill");
  686. system(cmdBuf);
  687. sleep(5);
  688. // Wifi IP set by DHCP client or static
  689. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
  690. {
  691. sprintf(cmdBuf, "/sbin/udhcpc -i %s -x hostname:CSU3_%s -s /root/dhcp_script/wifi.script > /dev/null &", Wifi.currentInterface, ShmSysConfigAndInfo->SysConfig.SystemId);
  692. system(cmdBuf);
  693. }
  694. else
  695. {
  696. memset(cmdBuf, 0, ARRAY_SIZE(cmdBuf));
  697. sprintf(cmdBuf,"/sbin/ifconfig %s %s netmask %s",
  698. Wifi.currentInterface,
  699. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress,
  700. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  701. system(cmdBuf);
  702. memset(cmdBuf, 0, ARRAY_SIZE(cmdBuf));
  703. sprintf(cmdBuf,"route add default gw %s %s ",
  704. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress,
  705. Wifi.currentInterface);
  706. system(cmdBuf);
  707. DEBUG_INFO("Setting %s static IP to %s submask to %s gateway to %s...\n",
  708. Wifi.currentInterface,
  709. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress,
  710. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress,
  711. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  712. }
  713. break;
  714. case WIFI_MODE_AP:
  715. // Stop dhcp client or server
  716. sprintf(cmdBuf, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  717. system(cmdBuf);
  718. sprintf(cmdBuf, "pgrep -f \"udhcpd\" | xargs kill");
  719. system(cmdBuf);
  720. // Restart wpa_supplicant
  721. sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
  722. system(cmdBuf);
  723. sprintf(cmdBuf, "pgrep -f \"hostapd\" | xargs kill");
  724. system(cmdBuf);
  725. sprintf(cmdBuf, "echo interface=uap0 >> /etc/hostapd.conf");
  726. system(cmdBuf);
  727. sprintf(cmdBuf, "echo driver=nl80211 >> /etc/hostapd.conf");
  728. system(cmdBuf);
  729. sprintf(cmdBuf, "echo ssid=%s%s >> /etc/hostapd.conf", ShmSysConfigAndInfo->SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.SerialNumber);
  730. system(cmdBuf);
  731. sprintf(cmdBuf, "echo channel=6 >> /etc/hostapd.conf");
  732. system(cmdBuf);
  733. sprintf(cmdBuf, "echo wpa=2 >> /etc/hostapd.conf");
  734. system(cmdBuf);
  735. sprintf(cmdBuf, "echo wpa_passphrase=%s%s >> /etc/hostapd.conf", ShmSysConfigAndInfo->SysConfig.SerialNumber, ShmSysConfigAndInfo->SysConfig.ModelName);
  736. system(cmdBuf);
  737. sprintf(cmdBuf, "echo wpa_key_mgmt=WPA-PSK >> /etc/hostapd.conf");
  738. system(cmdBuf);
  739. sprintf(cmdBuf, "hostapd /etc/hostapd.conf -B");
  740. system(cmdBuf);
  741. sleep(5);
  742. // Set wifi AP ip address
  743. sprintf(cmdBuf, "ifconfig %s 192.168.10.10", Wifi.currentInterface);
  744. system(cmdBuf);
  745. DEBUG_INFO("Setting %s static IP to 192.168.10.10...\n", Wifi.currentInterface);
  746. // Start DHCP server
  747. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpServer==0)
  748. {
  749. sprintf(cmdBuf, "/usr/sbin/udhcpd /etc/udhcpd.conf > /dev/null &");
  750. system(cmdBuf);
  751. }
  752. break;
  753. case WIFI_MODE_ADHOC:
  754. break;
  755. case WIFI_MODE_DISABLE:
  756. default:
  757. break;
  758. }
  759. result = PASS;
  760. return result;
  761. }
  762. //==========================================
  763. // UDP socket server routine
  764. //==========================================
  765. int split2int(char *dest, char *src, const char *flag, int base)
  766. {
  767. uint8_t result = 0;
  768. char *s = strtok(src, flag);
  769. while(s != NULL)
  770. {
  771. *dest++ = (base==10?atoi(s):strtol(s, NULL, 16));
  772. s = strtok(NULL, flag);
  773. result++;
  774. }
  775. return result;
  776. }
  777. uint8_t parseIP(uint8_t *ip)
  778. {
  779. uint8_t result = FAIL;
  780. const int8_t flag_dot[1] = {'.'};
  781. int8_t buf[16];
  782. memcpy(buf, ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, ARRAY_SIZE(buf));
  783. DEBUG_INFO("GET IP WifiIpAddress : %s...\r\n",ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  784. if(split2int((char*)ip , (char*)buf, (char*)flag_dot, 10)==4)
  785. result = PASS;
  786. return result;
  787. }
  788. uint8_t parseMAC(uint8_t *mac)
  789. {
  790. uint8_t result = FAIL;
  791. const int8_t flag_colon[1] = {':'};
  792. int8_t buf[18];
  793. memcpy(buf, ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, ARRAY_SIZE(buf));
  794. if(split2int((char*)mac , (char*)buf, (char*)flag_colon, 16)==6)
  795. result = PASS;
  796. return result;
  797. }
  798. int udpSocketServerStart(void)
  799. {
  800. int sockFd;
  801. struct sockaddr_in servaddr;
  802. struct sockaddr_in peeraddr;
  803. socklen_t peerlen = sizeof(peeraddr);
  804. uint8_t inputBuffer[2048] = {};
  805. uint8_t outBuffer[2048] = {};
  806. int16_t read_size;
  807. int16_t tx_size;
  808. uint8_t broadcastCmd[6] = {0xff, 0xff, 0x00, 0x02, 0x00, 0x02};
  809. uint8_t validResult;
  810. uint8_t chksum;
  811. uint8_t ip[4];
  812. uint8_t mac[6];
  813. memset(&servaddr, 0, sizeof(servaddr));
  814. servaddr.sin_family = AF_INET;
  815. servaddr.sin_port = htons(LISTEN_PORT_UDP);
  816. servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  817. if ((sockFd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
  818. return FAIL;
  819. if (bind(sockFd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
  820. return FAIL;
  821. for(;;)
  822. {
  823. if((read_size = recvfrom(sockFd, inputBuffer, sizeof(inputBuffer), 0, (struct sockaddr *)&peeraddr, &peerlen)) > 0)
  824. {
  825. DEBUG_INFO("Revieve from: %s:%d\r\n", inet_ntoa(peeraddr.sin_addr), htons(peeraddr.sin_port));
  826. DEBUG_INFO("read_size: %d\r\n",read_size);
  827. if(read_size>=6)
  828. {
  829. validResult = PASS;
  830. for(uint8_t idx=0;idx<6;idx++)
  831. {
  832. if(inputBuffer[idx] != broadcastCmd[idx])
  833. {
  834. validResult = FAIL;
  835. break;
  836. }
  837. }
  838. if(validResult)
  839. {
  840. memset(outBuffer, 0x00, ARRAY_SIZE(outBuffer));
  841. tx_size = 41;
  842. outBuffer[0] = 0xff;
  843. outBuffer[1] = 0xff;
  844. outBuffer[2] = (0x25 << 0x08) & 0xff;
  845. outBuffer[3] = 0x25 & 0xff;
  846. outBuffer[4] = 0x00;
  847. if(parseIP(ip)==PASS)
  848. {
  849. outBuffer[5] = ip[0];
  850. outBuffer[6] = ip[1];
  851. outBuffer[7] = ip[2];
  852. outBuffer[8] = ip[3];
  853. }
  854. else
  855. {
  856. outBuffer[5] = 0x00;
  857. outBuffer[6] = 0x00;
  858. outBuffer[7] = 0x00;
  859. outBuffer[8] = 0x00;
  860. }
  861. for(uint8_t count = 5; count < 9 ;count++)
  862. DEBUG_INFO("outBuffer[%d] : %X...\r\n",count, outBuffer[count]);
  863. if(parseMAC(mac)==PASS)
  864. {
  865. outBuffer[9] = mac[0];
  866. outBuffer[10] = mac[1];
  867. outBuffer[11] = mac[2];
  868. outBuffer[12] = mac[3];
  869. outBuffer[13] = mac[4];
  870. outBuffer[14] = mac[5];
  871. }
  872. else
  873. {
  874. outBuffer[9] = 0x00;
  875. outBuffer[10] = 0x00;
  876. outBuffer[11] = 0x00;
  877. outBuffer[12] = 0x00;
  878. outBuffer[13] = 0x00;
  879. outBuffer[14] = 0x00;
  880. }
  881. for(uint8_t count = 9; count < 15 ;count++)
  882. DEBUG_INFO("outBuffer[%d] : %X...\r\n",count, outBuffer[count]);
  883. memcpy(&outBuffer[15], ShmSysConfigAndInfo->SysConfig.ModelName, 0x0e);
  884. memcpy(&outBuffer[29], ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x0b);
  885. chksum = 0x00;
  886. for(uint8_t idx=0;idx<36;idx++)
  887. {
  888. chksum ^= outBuffer[idx];
  889. }
  890. outBuffer[40] = chksum;
  891. sendto(sockFd, outBuffer, tx_size, 0, (struct sockaddr *)&peeraddr, peerlen);
  892. }
  893. }
  894. }
  895. }
  896. return FAIL;
  897. }
  898. //===============================================
  899. // SQLite3 related routine
  900. //===============================================
  901. int DB_Open(sqlite3 *db)
  902. {
  903. int result = PASS;
  904. char* errMsg = NULL;
  905. char* createSql="CREATE TABLE IF NOT EXISTS list("
  906. "idx integer primary key AUTOINCREMENT, "
  907. "list_type text, "
  908. "code text, "
  909. "user_id text, "
  910. "isReg text"
  911. ");";
  912. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  913. if(sqlite3_open(DB_FILE, &db))
  914. {
  915. result = FAIL;
  916. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  917. sqlite3_close(db);
  918. }
  919. else
  920. {
  921. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  922. if (sqlite3_exec(db, createSql, 0, 0, &errMsg) != SQLITE_OK)
  923. {
  924. result = FAIL;
  925. DEBUG_INFO( "Create puk/pin list table error message: %s\n", errMsg);
  926. }
  927. else
  928. {
  929. DEBUG_INFO( "Opened puk/pin list table successfully\n");
  930. }
  931. sqlite3_close(db);
  932. }
  933. return result;
  934. }
  935. uint8_t isPukReg(sqlite3 *db)
  936. {
  937. uint8_t result = false;
  938. char* errMsg = NULL;
  939. char sqlStr[1024];
  940. char **rs;
  941. int rows, cols;
  942. sprintf(sqlStr, "select * from list where list_type='puk';");
  943. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  944. if(sqlite3_open(DB_FILE, &db))
  945. {
  946. result = FAIL;
  947. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  948. sqlite3_close(db);
  949. }
  950. else
  951. {
  952. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  953. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  954. if(rows>0)
  955. result = true;
  956. sqlite3_free_table(rs);
  957. sqlite3_close(db);
  958. }
  959. return result;
  960. }
  961. uint8_t PukReg(sqlite3 *db)
  962. {
  963. uint8_t result = false;
  964. char* errMsg = NULL;
  965. char sqlStr[1024];
  966. sprintf(sqlStr, "insert into list(list_type, code, user_id, isReg) "
  967. "values('puk', '%s', '%s', '1');",
  968. wifi_login_info.puk,
  969. wifi_login_info.loginId);
  970. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  971. if(sqlite3_open(DB_FILE, &db))
  972. {
  973. result = FAIL;
  974. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  975. sqlite3_close(db);
  976. }
  977. else
  978. {
  979. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  980. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  981. {
  982. result = FAIL;
  983. DEBUG_INFO( "Insert puk/pin list error message: %s\n", errMsg);
  984. }
  985. else
  986. {
  987. DEBUG_INFO( "Insert puk/pin list successfully\n");
  988. }
  989. sqlite3_close(db);
  990. }
  991. return result;
  992. }
  993. uint8_t isPinGen(sqlite3 *db)
  994. {
  995. uint8_t result = false;
  996. char* errMsg = NULL;
  997. char sqlStr[1024];
  998. char **rs;
  999. int rows, cols;
  1000. sprintf(sqlStr, "select * from list where list_type='pin';");
  1001. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  1002. if(sqlite3_open(DB_FILE, &db))
  1003. {
  1004. result = FAIL;
  1005. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  1006. sqlite3_close(db);
  1007. }
  1008. else
  1009. {
  1010. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  1011. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1012. if(rows>0)
  1013. result = true;
  1014. sqlite3_free_table(rs);
  1015. sqlite3_close(db);
  1016. }
  1017. return result;
  1018. }
  1019. uint8_t PinGen(sqlite3 *db)
  1020. {
  1021. uint8_t result = false;
  1022. char* errMsg = NULL;
  1023. char sqlStr[1024];
  1024. srand(time(NULL));
  1025. if(sqlite3_open(DB_FILE, &db))
  1026. {
  1027. result = FAIL;
  1028. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  1029. sqlite3_close(db);
  1030. }
  1031. else
  1032. {
  1033. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  1034. sprintf(sqlStr, "delete from list where list_type='pin';");
  1035. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  1036. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1037. {
  1038. result = FAIL;
  1039. DEBUG_INFO( "Delete puk/pin list error message: %s\n", errMsg);
  1040. }
  1041. else
  1042. {
  1043. for(int idxList=0;idxList<20;idxList++)
  1044. {
  1045. sprintf(sqlStr, "insert into list(list_type, code, user_id, isReg) "
  1046. "values('pin', '%06X', '', '0');", (rand() % 0xffffff));
  1047. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  1048. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  1049. {
  1050. result = FAIL;
  1051. DEBUG_INFO( "Insert puk/pin list error message: %s\n", errMsg);
  1052. }
  1053. else
  1054. {
  1055. DEBUG_INFO( "Insert puk/pin list successfully\n");
  1056. }
  1057. }
  1058. }
  1059. sqlite3_close(db);
  1060. }
  1061. return result;
  1062. }
  1063. uint8_t isValidPin(sqlite3 *db, uint8_t *pinCode)
  1064. {
  1065. uint8_t result = false;
  1066. char* errMsg = NULL;
  1067. char sqlStr[1024];
  1068. char **rs;
  1069. int rows, cols;
  1070. sprintf(sqlStr, "select * from list where list_type='pin' and code='%s';", pinCode);
  1071. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  1072. if(sqlite3_open(DB_FILE, &db))
  1073. {
  1074. result = FAIL;
  1075. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  1076. sqlite3_close(db);
  1077. }
  1078. else
  1079. {
  1080. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  1081. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1082. if(rows>0)
  1083. result = true;
  1084. sqlite3_free_table(rs);
  1085. sqlite3_close(db);
  1086. }
  1087. return result;
  1088. }
  1089. uint8_t loginValid(sqlite3 *db)
  1090. {
  1091. uint8_t result = ROLE_UNKNOWN;
  1092. char* errMsg = NULL;
  1093. char sqlStr[1024];
  1094. char **rs;
  1095. int rows, cols;
  1096. sprintf(sqlStr, "select * from list where user_id='%s';", wifi_login_info.loginId);
  1097. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  1098. DEBUG_INFO("Login id: %s\r\n", wifi_login_info.loginId);
  1099. if(sqlite3_open(DB_FILE, &db))
  1100. {
  1101. result = FAIL;
  1102. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  1103. sqlite3_close(db);
  1104. }
  1105. else
  1106. {
  1107. DEBUG_INFO( "Local puk/pin list database open successfully.\r\n");
  1108. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  1109. for(int idxRow=0;idxRow<rows;idxRow++)
  1110. {
  1111. if(strcmp(rs[(idxRow*cols)+1], "puk") == 0)
  1112. {
  1113. result = ROLE_MANAGER;
  1114. DEBUG_INFO("Login role: Manager\r\n");
  1115. }
  1116. else if(strcmp(rs[(idxRow*cols)+1], "pin") == 0)
  1117. {
  1118. result = ROLE_USER;
  1119. DEBUG_INFO("Login role: User\r\n");
  1120. }
  1121. }
  1122. sqlite3_free_table(rs);
  1123. sqlite3_close(db);
  1124. }
  1125. return result;
  1126. }
  1127. //==========================================
  1128. // TCP socket server routine
  1129. //==========================================
  1130. uint32_t stm32crc(uint32_t *ptr, uint32_t len)
  1131. {
  1132. uint32_t xbit, data;
  1133. uint32_t crc32 = 0xFFFFFFFF;
  1134. const uint32_t polynomial = 0x04c11db7;
  1135. for(uint8_t i = 0; i < len; i ++)
  1136. {
  1137. xbit = 1 << 31;
  1138. data = ptr[i];
  1139. for (uint32_t bits = 0; bits < 32; bits++)
  1140. {
  1141. if (crc32 & 0x80000000)
  1142. {
  1143. crc32 <<= 1;
  1144. crc32 ^= polynomial;
  1145. }
  1146. else
  1147. crc32 <<= 1;
  1148. if (data & xbit)
  1149. crc32 ^= polynomial;
  1150. xbit >>= 1;
  1151. }
  1152. }
  1153. return crc32;
  1154. }
  1155. int isValidCheckSum(uint8_t *message)
  1156. {
  1157. uint8_t chksum=0;
  1158. for(int idx=0;idx<(((message[2]<<8) | message[3])>1024?1024:(((message[2]<<8) | message[3])-1));idx++)
  1159. {
  1160. chksum ^= message[4+idx];
  1161. }
  1162. return ((chksum == message[3+(((message[2]<<8) | message[3])>1024?1024:((message[2]<<8) | message[3]))]) ? PASS : FAIL);
  1163. }
  1164. int chksumCal(uint8_t *message)
  1165. {
  1166. uint8_t chksum=0;
  1167. for(int idx = 0;idx<(((message[2]<<8) | message[3])>1024?1024:(((message[2]<<8) | message[3])-1));idx++)
  1168. {
  1169. chksum ^= message[4+idx];
  1170. }
  1171. return chksum & 0xff;
  1172. }
  1173. int StoreUsrConfigData(struct SysConfigData *UsrData)
  1174. {
  1175. int result = PASS;
  1176. int fd,wrd;
  1177. unsigned int i,Chk;
  1178. unsigned char *ptr, *BufTmp;
  1179. Chk=0;
  1180. ptr=(unsigned char *)UsrData;
  1181. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  1182. {
  1183. memset(BufTmp,0,MtdBlockSize);
  1184. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  1185. for(i=0;i<MtdBlockSize-4;i++)
  1186. Chk+=*(BufTmp+i);
  1187. memcpy( BufTmp+MtdBlockSize-4,&Chk,4);
  1188. fd = open("/dev/mtdblock10", O_RDWR);
  1189. if (fd>0)
  1190. {
  1191. wrd=write(fd, BufTmp, MtdBlockSize);
  1192. close(fd);
  1193. if(wrd>=MtdBlockSize)
  1194. {
  1195. fd = open("/dev/mtdblock11", O_RDWR);
  1196. if (fd>0)
  1197. {
  1198. wrd=write(fd, BufTmp, MtdBlockSize);
  1199. close(fd);
  1200. if(wrd<MtdBlockSize)
  1201. {
  1202. DEBUG_ERROR("write /dev/mtdblock11(backup) NG\r\n");
  1203. result = FAIL;
  1204. }
  1205. }
  1206. else
  1207. {
  1208. DEBUG_ERROR("open /dev/mtdblock11(backup) NG\r\n");
  1209. result = FAIL;
  1210. }
  1211. }
  1212. else
  1213. {
  1214. DEBUG_ERROR("write /dev/mtdblock10 NG\r\n");
  1215. result = FAIL;
  1216. }
  1217. }
  1218. else
  1219. {
  1220. DEBUG_ERROR("open /dev/mtdblock10 NG\r\n");
  1221. result = FAIL;
  1222. }
  1223. }
  1224. else
  1225. {
  1226. DEBUG_ERROR("alloc BlockSize NG\r\n");
  1227. result = FAIL;
  1228. }
  1229. if(BufTmp!=NULL)
  1230. free(BufTmp);
  1231. return result;
  1232. }
  1233. void getConnectorInfo(uint8_t gun_index, uint8_t *outBuffer)
  1234. {
  1235. uint8_t idxAC = 0;
  1236. uint8_t idxCCS = 0;
  1237. uint8_t idxCHAdeMO = 0;
  1238. uint8_t idxGBT = 0;
  1239. uint8_t AC_GUN_IDX = 0;
  1240. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1241. {
  1242. if(gun_index == 2)
  1243. {
  1244. AC_GUN_IDX = 0;
  1245. }
  1246. else if(gun_index == 1)
  1247. {
  1248. AC_GUN_IDX = 1;
  1249. }
  1250. else if(gun_index == 0)
  1251. {
  1252. AC_GUN_IDX = 2;
  1253. }
  1254. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1255. {
  1256. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1257. {
  1258. case '1' ... '6':
  1259. idxAC++;
  1260. break;
  1261. }
  1262. }
  1263. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1264. {
  1265. case '1' ... '6':
  1266. // AC
  1267. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus;
  1268. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>8)&0xff;
  1269. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>0)&0xff;
  1270. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>8)&0xff;
  1271. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>0)&0xff;
  1272. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>8)&0xff;
  1273. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>0)&0xff;
  1274. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>8)&0xff;
  1275. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>0)&0xff;
  1276. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>8)&0xff;
  1277. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>0)&0xff;
  1278. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>8)&0xff;
  1279. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>0)&0xff;
  1280. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].EvBatterySoc)>>0)&0xff;
  1281. break;
  1282. default:
  1283. break;
  1284. }
  1285. }
  1286. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1287. {
  1288. for(uint8_t idx=0;idx<gun_index;idx++)
  1289. {
  1290. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1291. {
  1292. case '1' ... '6':
  1293. idxAC++;
  1294. break;
  1295. case 'J':
  1296. idxCHAdeMO++;
  1297. break;
  1298. case 'U':
  1299. case 'E':
  1300. idxCCS++;
  1301. break;
  1302. case 'G':
  1303. idxGBT++;
  1304. break;
  1305. }
  1306. }
  1307. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1308. {
  1309. case '1' ... '6':
  1310. // AC
  1311. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus;
  1312. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>8)&0xff;
  1313. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingVoltage*10)>>0)&0xff;
  1314. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>8)&0xff;
  1315. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingCurrent*10)>>0)&0xff;
  1316. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>8)&0xff;
  1317. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargingPower*10)>>0)&0xff;
  1318. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>8)&0xff;
  1319. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedEnergy*10)>>0)&0xff;
  1320. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>8)&0xff;
  1321. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].PresentChargedDuration)>>0)&0xff;
  1322. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>8)&0xff;
  1323. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].RemainChargingDuration)>>0)&0xff;
  1324. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].EvBatterySoc)>>0)&0xff;
  1325. break;
  1326. case 'J':
  1327. // CHAdeMO
  1328. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus;
  1329. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingVoltage*10)>>8)&0xff;
  1330. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingVoltage*10)>>0)&0xff;
  1331. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingCurrent*10)>>8)&0xff;
  1332. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingCurrent*10)>>0)&0xff;
  1333. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingPower*10)>>8)&0xff;
  1334. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargingPower*10)>>0)&0xff;
  1335. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedEnergy*10)>>8)&0xff;
  1336. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedEnergy*10)>>0)&0xff;
  1337. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedDuration)>>8)&0xff;
  1338. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].PresentChargedDuration)>>0)&0xff;
  1339. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].RemainChargingDuration)>>8)&0xff;
  1340. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].RemainChargingDuration)>>0)&0xff;
  1341. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].EvBatterySoc)>>0)&0xff;
  1342. break;
  1343. case 'U':
  1344. case 'E':
  1345. // CCS
  1346. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus;
  1347. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingVoltage*10)>>8)&0xff;
  1348. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingVoltage*10)>>0)&0xff;
  1349. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingCurrent*10)>>8)&0xff;
  1350. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingCurrent*10)>>0)&0xff;
  1351. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingPower*10)>>8)&0xff;
  1352. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargingPower*10)>>0)&0xff;
  1353. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedEnergy*10)>>8)&0xff;
  1354. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedEnergy*10)>>0)&0xff;
  1355. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedDuration)>>8)&0xff;
  1356. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].PresentChargedDuration)>>0)&0xff;
  1357. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].RemainChargingDuration)>>8)&0xff;
  1358. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].RemainChargingDuration)>>0)&0xff;
  1359. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].EvBatterySoc)>>0)&0xff;
  1360. break;
  1361. case 'G':
  1362. // GBT
  1363. outBuffer[6] = ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus;
  1364. outBuffer[7] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingVoltage*10)>>8)&0xff;
  1365. outBuffer[8] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingVoltage*10)>>0)&0xff;
  1366. outBuffer[9] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingCurrent*10)>>8)&0xff;
  1367. outBuffer[10] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingCurrent*10)>>0)&0xff;
  1368. outBuffer[11] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingPower*10)>>8)&0xff;
  1369. outBuffer[12] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargingPower*10)>>0)&0xff;
  1370. outBuffer[13] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedEnergy*10)>>8)&0xff;
  1371. outBuffer[14] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedEnergy*10)>>0)&0xff;
  1372. outBuffer[15] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedDuration)>>8)&0xff;
  1373. outBuffer[16] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].PresentChargedDuration)>>0)&0xff;
  1374. outBuffer[17] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].RemainChargingDuration)>>8)&0xff;
  1375. outBuffer[18] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].RemainChargingDuration)>>0)&0xff;
  1376. outBuffer[19] = ((uint16_t)(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].EvBatterySoc)>>0)&0xff;
  1377. break;
  1378. default:
  1379. break;
  1380. }
  1381. }
  1382. }
  1383. void getConnectorSchedule(uint8_t gun_index, uint8_t *outBuffer)
  1384. {
  1385. uint8_t idxAC = 0;
  1386. uint8_t idxCCS = 0;
  1387. uint8_t idxCHAdeMO = 0;
  1388. uint8_t idxGBT = 0;
  1389. uint8_t AC_GUN_IDX = 0;
  1390. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1391. {
  1392. if(gun_index == 2)
  1393. {
  1394. AC_GUN_IDX = 0;
  1395. }
  1396. else if(gun_index == 1)
  1397. {
  1398. AC_GUN_IDX = 1;
  1399. }
  1400. else if(gun_index == 0)
  1401. {
  1402. AC_GUN_IDX = 2;
  1403. }
  1404. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1405. {
  1406. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1407. {
  1408. case '1' ... '6':
  1409. idxAC++;
  1410. break;
  1411. }
  1412. }
  1413. }
  1414. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1415. {
  1416. for(uint8_t idx=0;idx<gun_index;idx++)
  1417. {
  1418. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1419. {
  1420. case '1' ... '6':
  1421. idxAC++;
  1422. break;
  1423. case 'J':
  1424. idxCHAdeMO++;
  1425. break;
  1426. case 'U':
  1427. case 'E':
  1428. idxCCS++;
  1429. break;
  1430. case 'G':
  1431. idxGBT++;
  1432. break;
  1433. }
  1434. }
  1435. }
  1436. /*
  1437. * TODO:
  1438. * 1. Connector schedule get
  1439. */
  1440. }
  1441. uint8_t startConnectorCharging(uint8_t gun_index)
  1442. {
  1443. uint8_t idxAC = 0;
  1444. uint8_t idxCCS = 0;
  1445. uint8_t idxCHAdeMO = 0;
  1446. uint8_t idxGBT = 0;
  1447. uint8_t result = OFF;
  1448. uint8_t AC_GUN_IDX = 0;
  1449. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1450. {
  1451. if(gun_index == 2)
  1452. {
  1453. AC_GUN_IDX = 0;
  1454. }
  1455. else if(gun_index == 1)
  1456. {
  1457. AC_GUN_IDX = 1;
  1458. }
  1459. else if(gun_index == 0)
  1460. {
  1461. AC_GUN_IDX = 2;
  1462. }
  1463. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1464. {
  1465. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1466. {
  1467. case '1' ... '6':
  1468. idxAC++;
  1469. break;
  1470. }
  1471. }
  1472. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1473. {
  1474. case '1' ... '6':
  1475. // AC
  1476. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)
  1477. {
  1478. ShmOCPP16Data->CsMsg.bits[AC_GUN_IDX].RemoteStartTransactionReq = ON;
  1479. result = ON;
  1480. }
  1481. break;
  1482. default:
  1483. break;
  1484. }
  1485. return result;
  1486. }
  1487. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1488. {
  1489. for(uint8_t idx=0;idx<gun_index;idx++)
  1490. {
  1491. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1492. {
  1493. case '1' ... '6':
  1494. idxAC++;
  1495. break;
  1496. case 'J':
  1497. idxCHAdeMO++;
  1498. break;
  1499. case 'U':
  1500. case 'E':
  1501. idxCCS++;
  1502. break;
  1503. case 'G':
  1504. idxGBT++;
  1505. break;
  1506. }
  1507. }
  1508. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1509. {
  1510. case '1' ... '6':
  1511. // AC
  1512. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)
  1513. {
  1514. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1515. DEBUG_INFO("AC : %d...\r\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1516. result = ON;
  1517. }
  1518. break;
  1519. case 'J':
  1520. // CHAdeMO
  1521. if(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus == SYS_MODE_IDLE)
  1522. {
  1523. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1524. DEBUG_INFO("CHAdeMO : %d...\r\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1525. result = ON;
  1526. }
  1527. break;
  1528. case 'U':
  1529. case 'E':
  1530. // CCS
  1531. if(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus == SYS_MODE_IDLE)
  1532. {
  1533. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1534. DEBUG_INFO("CCS : %d...\r\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1535. result = ON;
  1536. }
  1537. break;
  1538. case 'G':
  1539. // GBT
  1540. if(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus == SYS_MODE_IDLE)
  1541. {
  1542. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = ON;
  1543. DEBUG_INFO("GBT : %d...\r\n", ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq);
  1544. result = ON;
  1545. }
  1546. break;
  1547. default:
  1548. break;
  1549. }
  1550. }
  1551. return result;
  1552. }
  1553. uint8_t stopConnectorCharging(uint8_t gun_index)
  1554. {
  1555. uint8_t idxAC = 0;
  1556. uint8_t idxCCS = 0;
  1557. uint8_t idxCHAdeMO = 0;
  1558. uint8_t idxGBT = 0;
  1559. uint8_t result = OFF;
  1560. uint8_t AC_GUN_IDX = 0;
  1561. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1562. {
  1563. if(gun_index == 2)
  1564. {
  1565. AC_GUN_IDX = 0;
  1566. }
  1567. else if(gun_index == 1)
  1568. {
  1569. AC_GUN_IDX = 1;
  1570. }
  1571. else if(gun_index == 0)
  1572. {
  1573. AC_GUN_IDX = 2;
  1574. }
  1575. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1576. {
  1577. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1578. {
  1579. case '1' ... '6':
  1580. idxAC++;
  1581. break;
  1582. }
  1583. }
  1584. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1585. {
  1586. case '1' ... '6':
  1587. // AC
  1588. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus != SYS_MODE_IDLE)
  1589. {
  1590. ShmOCPP16Data->CsMsg.bits[AC_GUN_IDX].RemoteStopTransactionReq = ON;
  1591. result = ON;
  1592. }
  1593. break;
  1594. default:
  1595. break;
  1596. }
  1597. return result;
  1598. }
  1599. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1600. {
  1601. for(uint8_t idx=0;idx<gun_index;idx++)
  1602. {
  1603. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1604. {
  1605. case '1' ... '6':
  1606. idxAC++;
  1607. break;
  1608. case 'J':
  1609. idxCHAdeMO++;
  1610. break;
  1611. case 'U':
  1612. case 'E':
  1613. idxCCS++;
  1614. break;
  1615. case 'G':
  1616. idxGBT++;
  1617. break;
  1618. }
  1619. }
  1620. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1621. {
  1622. case '1' ... '6':
  1623. // AC
  1624. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus != SYS_MODE_IDLE)
  1625. {
  1626. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1627. result = ON;
  1628. }
  1629. break;
  1630. case 'J':
  1631. // CHAdeMO
  1632. if(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus != SYS_MODE_IDLE)
  1633. {
  1634. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1635. result = ON;
  1636. }
  1637. break;
  1638. case 'U':
  1639. case 'E':
  1640. // CCS
  1641. if(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus != SYS_MODE_IDLE)
  1642. {
  1643. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1644. result = ON;
  1645. }
  1646. break;
  1647. case 'G':
  1648. // GBT
  1649. if(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus != SYS_MODE_IDLE)
  1650. {
  1651. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = ON;
  1652. result = ON;
  1653. }
  1654. break;
  1655. default:
  1656. break;
  1657. }
  1658. }
  1659. return result;
  1660. }
  1661. void setConnectorSchedule(uint8_t gun_index, uint8_t *inputBuffer)
  1662. {
  1663. uint8_t idxAC = 0;
  1664. uint8_t idxCCS = 0;
  1665. uint8_t idxCHAdeMO = 0;
  1666. uint8_t idxGBT = 0;
  1667. uint8_t AC_GUN_IDX = 0;
  1668. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1669. {
  1670. if(gun_index == 2)
  1671. {
  1672. AC_GUN_IDX = 0;
  1673. }
  1674. else if(gun_index == 1)
  1675. {
  1676. AC_GUN_IDX = 1;
  1677. }
  1678. else if(gun_index == 0)
  1679. {
  1680. AC_GUN_IDX = 2;
  1681. }
  1682. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1683. {
  1684. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1685. {
  1686. case '1' ... '6':
  1687. idxAC++;
  1688. break;
  1689. }
  1690. }
  1691. }
  1692. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1693. {
  1694. for(uint8_t idx=0;idx<gun_index;idx++)
  1695. {
  1696. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1697. {
  1698. case '1' ... '6':
  1699. idxAC++;
  1700. break;
  1701. case 'J':
  1702. idxCHAdeMO++;
  1703. break;
  1704. case 'U':
  1705. case 'E':
  1706. idxCCS++;
  1707. break;
  1708. case 'G':
  1709. idxGBT++;
  1710. break;
  1711. }
  1712. }
  1713. }
  1714. /*
  1715. * TODO:
  1716. * 1. Connector schedule configuration
  1717. */
  1718. }
  1719. void getConnectorUser(uint8_t gun_index, uint8_t *outBuffer)
  1720. {
  1721. uint8_t idxAC = 0;
  1722. uint8_t idxCCS = 0;
  1723. uint8_t idxCHAdeMO = 0;
  1724. uint8_t idxGBT = 0;
  1725. uint8_t AC_GUN_IDX = 0;
  1726. if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'A')
  1727. {
  1728. if(gun_index == 2)
  1729. {
  1730. AC_GUN_IDX = 0;
  1731. }
  1732. else if(gun_index == 1)
  1733. {
  1734. AC_GUN_IDX = 1;
  1735. }
  1736. else if(gun_index == 0)
  1737. {
  1738. AC_GUN_IDX = 2;
  1739. }
  1740. for(uint8_t idx=0;idx<AC_GUN_IDX;idx++)
  1741. {
  1742. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-idx])
  1743. {
  1744. case '1' ... '6':
  1745. idxAC++;
  1746. break;
  1747. }
  1748. }
  1749. switch(ShmSysConfigAndInfo->SysConfig.ModelName[9-AC_GUN_IDX])
  1750. {
  1751. case '1' ... '6':
  1752. // AC
  1753. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  1754. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId));
  1755. break;
  1756. default:
  1757. break;
  1758. }
  1759. }
  1760. else if(ShmSysConfigAndInfo->SysConfig.ModelName[0] == 'D')
  1761. {
  1762. for(uint8_t idx=0;idx<gun_index;idx++)
  1763. {
  1764. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1765. {
  1766. case '1' ... '6':
  1767. idxAC++;
  1768. break;
  1769. case 'J':
  1770. idxCHAdeMO++;
  1771. break;
  1772. case 'U':
  1773. case 'E':
  1774. idxCCS++;
  1775. break;
  1776. case 'G':
  1777. idxGBT++;
  1778. break;
  1779. }
  1780. }
  1781. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+gun_index])
  1782. {
  1783. case '1' ... '6':
  1784. // AC
  1785. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  1786. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AcChargingData[idxAC].StartUserId));
  1787. break;
  1788. case 'J':
  1789. // CHAdeMO
  1790. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  1791. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.ChademoChargingData[idxCHAdeMO].StartUserId));
  1792. break;
  1793. case 'U':
  1794. case 'E':
  1795. // CCS
  1796. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  1797. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.CcsChargingData[idxCCS].StartUserId));
  1798. break;
  1799. case 'G':
  1800. // GBT
  1801. outBuffer[6] = ((ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].SystemStatus == SYS_MODE_IDLE)?0x01:0x00);
  1802. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].StartUserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.GbChargingData[idxGBT].StartUserId));
  1803. break;
  1804. default:
  1805. break;
  1806. }
  1807. }
  1808. }
  1809. int tcpSocketServerStart(void)
  1810. {
  1811. int sockFd = 0;
  1812. int clientSockFd = 0;
  1813. uint8_t inputBuffer[2048] = {};
  1814. uint8_t outBuffer[2048] = {};
  1815. int16_t read_size;
  1816. int16_t tx_size;
  1817. uint32_t randomChk;
  1818. struct sockaddr_in serverInfo, clientInfo;
  1819. socklen_t addrlen = sizeof(clientInfo);
  1820. uint8_t cmdBuf[512];
  1821. time_t rawtime;
  1822. struct tm ts;
  1823. sockFd = socket(AF_INET , SOCK_STREAM , 0);
  1824. if(sockFd == -1)
  1825. {
  1826. DEBUG_ERROR("InitSocketServer NG\n");
  1827. sleep(5);
  1828. return FAIL;
  1829. }
  1830. bzero(&serverInfo,sizeof(serverInfo));
  1831. serverInfo.sin_family = PF_INET;
  1832. serverInfo.sin_addr.s_addr = htonl(INADDR_ANY);
  1833. serverInfo.sin_port = htons(LISTEN_PORT_TCP);
  1834. bind(sockFd, (struct sockaddr *)&serverInfo, sizeof(serverInfo));
  1835. listen(sockFd, CONNECTION_LIMIT);
  1836. // Main loop
  1837. for(;;)
  1838. {
  1839. clientSockFd = accept(sockFd, (struct sockaddr*) &clientInfo, &addrlen);
  1840. DEBUG_INFO("Client connect in.\r\n");
  1841. DEBUG_INFO("clientSockFd : %d...\r\n", clientSockFd);
  1842. // Copy ShmSysConfigAndInfo to SysConfigTemporarily
  1843. memcpy(&SysConfigTemporarily, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  1844. while((read_size = recv(clientSockFd, inputBuffer, sizeof(inputBuffer), 0)) > 0)
  1845. {
  1846. if(isValidCheckSum(inputBuffer) && (randomChk != ((inputBuffer[4]<<24) | (inputBuffer[5]<<16) | (inputBuffer[6]<<8) | (inputBuffer[7]<<0))))
  1847. {
  1848. randomChk = ((inputBuffer[4]<<24) | (inputBuffer[5]<<16) | (inputBuffer[6]<<8) | (inputBuffer[7]<<0));
  1849. memset(outBuffer, 0x00, sizeof(outBuffer));
  1850. switch(inputBuffer[8])
  1851. {
  1852. case WIFI_CMD_LOGIN_SIGNIN:
  1853. memset(&wifi_login_info.loginId[0], 0x00, ARRAY_SIZE(wifi_login_info.loginId));
  1854. memcpy((char*)&wifi_login_info.loginId[0], (char*)&inputBuffer[9], (((inputBuffer[2] << 8)| inputBuffer[3])-6));
  1855. wifi_login_info.loginRole = loginValid(localDb);
  1856. DEBUG_INFO("Login user id: %s\r\n", wifi_login_info.loginId);
  1857. DEBUG_INFO("User id: %d\r\n", wifi_login_info.loginRole);
  1858. tx_size = 7;
  1859. outBuffer[0] = 0xff;
  1860. outBuffer[1] = 0xff;
  1861. outBuffer[2] = 0x00;
  1862. outBuffer[3] = 0x03;
  1863. outBuffer[4] = WIFI_CMD_LOGIN_SIGNIN;
  1864. switch(wifi_login_info.loginRole)
  1865. {
  1866. case ROLE_MANAGER:
  1867. case ROLE_USER:
  1868. // Login success
  1869. outBuffer[5] = 0x02;
  1870. break;
  1871. case ROLE_UNKNOWN:
  1872. default:
  1873. if(!isPukReg(localDb))
  1874. {
  1875. // Login fail, PUK never be registered
  1876. outBuffer[5] = 0x01;
  1877. }
  1878. else
  1879. {
  1880. // Login fail
  1881. outBuffer[5] = 0x00;
  1882. }
  1883. break;
  1884. }
  1885. outBuffer[6] = chksumCal(outBuffer);
  1886. break;
  1887. case WIFI_CMD_LOGIN_AUTH_PUK:
  1888. if(!isPukReg(localDb))
  1889. {
  1890. // PUK never be registered
  1891. if((wifi_login_info.puk[0] == inputBuffer[9]) &&
  1892. (wifi_login_info.puk[1] == inputBuffer[10]) &&
  1893. (wifi_login_info.puk[2] == inputBuffer[11]) &&
  1894. (wifi_login_info.puk[3] == inputBuffer[12]) &&
  1895. (wifi_login_info.puk[4] == inputBuffer[13]) &&
  1896. (wifi_login_info.puk[5] == inputBuffer[14]) &&
  1897. (wifi_login_info.puk[6] == inputBuffer[15]) &&
  1898. (wifi_login_info.puk[7] == inputBuffer[16]))
  1899. {
  1900. // PUK valid success
  1901. if(PukReg(localDb))
  1902. {
  1903. // PUK registered success
  1904. tx_size = 7 + strlen((char*)wifi_login_info.loginId);
  1905. outBuffer[0] = 0xff;
  1906. outBuffer[1] = 0xff;
  1907. outBuffer[2] = ((0x03 + strlen((char*)wifi_login_info.loginId))>>8)&0xff;
  1908. outBuffer[3] = (0x03 + strlen((char*)wifi_login_info.loginId)) & 0xff;
  1909. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  1910. outBuffer[5] = 0x01;
  1911. memcpy(&outBuffer[6], wifi_login_info.loginId, strlen((char*)wifi_login_info.loginId));
  1912. outBuffer[6+strlen((char*)wifi_login_info.loginId)] = chksumCal(outBuffer);
  1913. }
  1914. else
  1915. {
  1916. // PUK registered fail
  1917. tx_size = 7;
  1918. outBuffer[0] = 0xff;
  1919. outBuffer[1] = 0xff;
  1920. outBuffer[2] = 0x00;
  1921. outBuffer[3] = 0x03;
  1922. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  1923. outBuffer[5] = 0x00;
  1924. outBuffer[6] = chksumCal(outBuffer);
  1925. }
  1926. }
  1927. else
  1928. {
  1929. // PUK valid fail
  1930. tx_size = 7;
  1931. outBuffer[0] = 0xff;
  1932. outBuffer[1] = 0xff;
  1933. outBuffer[2] = 0x00;
  1934. outBuffer[3] = 0x03;
  1935. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  1936. outBuffer[5] = 0x00;
  1937. outBuffer[6] = chksumCal(outBuffer);
  1938. }
  1939. }
  1940. else
  1941. {
  1942. // PUK already be registered
  1943. tx_size = 7;
  1944. outBuffer[0] = 0xff;
  1945. outBuffer[1] = 0xff;
  1946. outBuffer[2] = 0x00;
  1947. outBuffer[3] = 0x03;
  1948. outBuffer[4] = WIFI_CMD_LOGIN_AUTH_PUK;
  1949. outBuffer[5] = 0x02;
  1950. outBuffer[6] = chksumCal(outBuffer);
  1951. }
  1952. break;
  1953. case WIFI_CMD_LOGIN_GET_USER_ID:
  1954. if(wifi_login_info.loginRole != ROLE_UNKNOWN)
  1955. {
  1956. // Login role is Manager or User
  1957. tx_size = 7 + strlen((char*)wifi_login_info.loginId);
  1958. outBuffer[0] = 0xff;
  1959. outBuffer[1] = 0xff;
  1960. outBuffer[2] = ((0x03 + strlen((char*)wifi_login_info.loginId))>>8)&0xff;
  1961. outBuffer[3] = (0x03 + strlen((char*)wifi_login_info.loginId)) & 0xff;
  1962. outBuffer[4] = WIFI_CMD_LOGIN_GET_USER_ID;
  1963. outBuffer[5] = 0x01;
  1964. memcpy(&outBuffer[6], wifi_login_info.loginId, strlen((char*)wifi_login_info.loginId));
  1965. outBuffer[6+strlen((char*)wifi_login_info.loginId)] = chksumCal(outBuffer);
  1966. }
  1967. else
  1968. {
  1969. // Login role is Unknown
  1970. tx_size = 7;
  1971. outBuffer[0] = 0xff;
  1972. outBuffer[1] = 0xff;
  1973. outBuffer[2] = 0x00;
  1974. outBuffer[3] = 0x03;
  1975. outBuffer[4] = WIFI_CMD_LOGIN_GET_USER_ID;
  1976. outBuffer[5] = 0x00;
  1977. outBuffer[6] = chksumCal(outBuffer);
  1978. }
  1979. break;
  1980. case WIFI_CMD_PROTOCOL_VERSION:
  1981. tx_size = 12;
  1982. outBuffer[0] = 0xff;
  1983. outBuffer[1] = 0xff;
  1984. outBuffer[2] = 0x00;
  1985. outBuffer[3] = 0x08;
  1986. outBuffer[4] = WIFI_CMD_PROTOCOL_VERSION;
  1987. outBuffer[5] = (protocol_Version[0] >> 0x00) & 0xff;
  1988. outBuffer[6] = (protocol_Version[0] >> 0x08) & 0xff;
  1989. outBuffer[7] = (protocol_Version[1] >> 0x00) & 0xff;
  1990. outBuffer[8] = (protocol_Version[1] >> 0x08) & 0xff;
  1991. outBuffer[9] = (protocol_Version[2] >> 0x00) & 0xff;
  1992. outBuffer[10] = (protocol_Version[2] >> 0x08) & 0xff;
  1993. outBuffer[11] = chksumCal(outBuffer);
  1994. break;
  1995. case WIFI_CMD_GET_MODEL_NAME:
  1996. tx_size = 70;
  1997. outBuffer[0] = 0xff;
  1998. outBuffer[1] = 0xff;
  1999. outBuffer[2] = 0x00;
  2000. outBuffer[3] = 0x42;
  2001. outBuffer[4] = WIFI_CMD_GET_MODEL_NAME;
  2002. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.ModelName, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  2003. outBuffer[69] = chksumCal(outBuffer);
  2004. break;
  2005. case WIFI_CMD_GET_SERIAL_NUMBER:
  2006. tx_size = 70;
  2007. outBuffer[0] = 0xff;
  2008. outBuffer[1] = 0xff;
  2009. outBuffer[2] = 0x00;
  2010. outBuffer[3] = 0x42;
  2011. outBuffer[4] = WIFI_CMD_GET_SERIAL_NUMBER;
  2012. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  2013. outBuffer[69] = chksumCal(outBuffer);
  2014. break;
  2015. case WIFI_CMD_GET_SYSTEM_TIME:
  2016. tx_size = 10;
  2017. outBuffer[0] = 0xff;
  2018. outBuffer[1] = 0xff;
  2019. outBuffer[2] = 0x00;
  2020. outBuffer[3] = 0x06;
  2021. outBuffer[4] = WIFI_CMD_GET_SYSTEM_TIME;
  2022. outBuffer[5] = (time(NULL)>>24)&0xff;
  2023. outBuffer[6] = (time(NULL)>>16)&0xff;
  2024. outBuffer[7] = (time(NULL)>>8)&0xff;
  2025. outBuffer[8] = (time(NULL)>>0)&0xff;
  2026. outBuffer[9] = chksumCal(outBuffer);
  2027. break;
  2028. case WIFI_CMD_GET_WHITE_CARD:
  2029. tx_size = 40;
  2030. outBuffer[0] = 0xff;
  2031. outBuffer[1] = 0xff;
  2032. outBuffer[2] = 0x00;
  2033. outBuffer[3] = 0x24;
  2034. outBuffer[4] = WIFI_CMD_GET_WHITE_CARD;
  2035. outBuffer[5] = inputBuffer[9];
  2036. outBuffer[6] = ((strlen((char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[inputBuffer[9]])>0)?0x00:0x01);
  2037. memcpy(&outBuffer[7], ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[inputBuffer[9]], ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[inputBuffer[9]]));
  2038. outBuffer[39] = chksumCal(outBuffer);
  2039. break;
  2040. case WIFI_CMD_GET_SYS_ALARM_CODE:
  2041. tx_size = 70;
  2042. outBuffer[0] = 0xff;
  2043. outBuffer[1] = 0xff;
  2044. outBuffer[2] = 0x00;
  2045. outBuffer[3] = 0x42;
  2046. outBuffer[4] = WIFI_CMD_GET_SYS_ALARM_CODE;
  2047. memcpy(&outBuffer[5], ShmStatusCodeData->FaultCode.FaultEvents.FaultVal, ARRAY_SIZE(ShmStatusCodeData->FaultCode.FaultEvents.FaultVal));
  2048. memcpy(&outBuffer[13], ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal, ARRAY_SIZE(ShmStatusCodeData->AlarmCode.AlarmEvents.AlarmVal));
  2049. memcpy(&outBuffer[29], ShmStatusCodeData->InfoCode.InfoEvents.InfoVal, ARRAY_SIZE(ShmStatusCodeData->InfoCode.InfoEvents.InfoVal));
  2050. outBuffer[69] = chksumCal(outBuffer);
  2051. break;
  2052. case WIFI_CMD_GET_SYS_ID:
  2053. tx_size = 70;
  2054. outBuffer[0] = 0xff;
  2055. outBuffer[1] = 0xff;
  2056. outBuffer[2] = 0x00;
  2057. outBuffer[3] = 0x42;
  2058. outBuffer[4] = WIFI_CMD_GET_SYS_ID;
  2059. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.SystemId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  2060. outBuffer[69] = chksumCal(outBuffer);
  2061. break;
  2062. case WIFI_CMD_GET_RATING_CURRENT:
  2063. tx_size = 10;
  2064. outBuffer[0] = 0xff;
  2065. outBuffer[1] = 0xff;
  2066. outBuffer[2] = 0x00;
  2067. outBuffer[3] = 0x06;
  2068. outBuffer[4] = WIFI_CMD_GET_RATING_CURRENT;
  2069. outBuffer[5] = (ShmSysConfigAndInfo->SysConfig.RatingCurrent>>0x08) & 0xff;
  2070. outBuffer[6] = (ShmSysConfigAndInfo->SysConfig.RatingCurrent>>0x00) & 0xff;
  2071. outBuffer[7] = (ShmSysConfigAndInfo->SysConfig.AcRatingCurrent>>0x08) & 0xff;
  2072. outBuffer[8] = (ShmSysConfigAndInfo->SysConfig.AcRatingCurrent>>0x00) & 0xff;
  2073. outBuffer[9] = chksumCal(outBuffer);
  2074. break;
  2075. case WIFI_CMD_GET_VER_RFS:
  2076. tx_size = 38;
  2077. outBuffer[0] = 0xff;
  2078. outBuffer[1] = 0xff;
  2079. outBuffer[2] = 0x00;
  2080. outBuffer[3] = 0x22;
  2081. outBuffer[4] = WIFI_CMD_GET_VER_RFS;
  2082. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev));
  2083. outBuffer[37] = chksumCal(outBuffer);
  2084. break;
  2085. case WIFI_CMD_GET_VER_PRIMARY_CSU:
  2086. tx_size = 38;
  2087. outBuffer[0] = 0xff;
  2088. outBuffer[1] = 0xff;
  2089. outBuffer[2] = 0x00;
  2090. outBuffer[3] = 0x22;
  2091. outBuffer[4] = WIFI_CMD_GET_VER_PRIMARY_CSU;
  2092. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev));
  2093. outBuffer[37] = chksumCal(outBuffer);
  2094. break;
  2095. case WIFI_CMD_GET_VER_LCM:
  2096. tx_size = 38;
  2097. outBuffer[0] = 0xff;
  2098. outBuffer[1] = 0xff;
  2099. outBuffer[2] = 0x00;
  2100. outBuffer[3] = 0x22;
  2101. outBuffer[4] = WIFI_CMD_GET_VER_LCM;
  2102. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.LcmHwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.LcmHwRev));
  2103. outBuffer[37] = chksumCal(outBuffer);
  2104. break;
  2105. case WIFI_CMD_GET_PSU_COUNT:
  2106. tx_size = 7;
  2107. outBuffer[0] = 0xff;
  2108. outBuffer[1] = 0xff;
  2109. outBuffer[2] = 0x00;
  2110. outBuffer[3] = 0x03;
  2111. outBuffer[4] = WIFI_CMD_GET_PSU_COUNT;
  2112. outBuffer[5] = ShmPsuData->SystemPresentPsuQuantity;
  2113. outBuffer[6] = chksumCal(outBuffer);
  2114. break;
  2115. case WIFI_CMD_GET_VER_PSU:
  2116. tx_size = 39;
  2117. outBuffer[0] = 0xff;
  2118. outBuffer[1] = 0xff;
  2119. outBuffer[2] = 0x00;
  2120. outBuffer[3] = 0x23;
  2121. outBuffer[4] = WIFI_CMD_GET_VER_PSU;
  2122. outBuffer[5] = inputBuffer[9];
  2123. memcpy(&outBuffer[6], ShmPsuData->PsuVersion[inputBuffer[9]].FwPrimaryVersion, ARRAY_SIZE(ShmPsuData->PsuVersion[inputBuffer[9]].FwPrimaryVersion));
  2124. memcpy(&outBuffer[22], ShmPsuData->PsuVersion[inputBuffer[9]].FwSecondVersion, ARRAY_SIZE(ShmPsuData->PsuVersion[inputBuffer[9]].FwSecondVersion));
  2125. outBuffer[38] = chksumCal(outBuffer);
  2126. break;
  2127. case WIFI_CMD_GET_VER_AUX_POWER:
  2128. tx_size = 38;
  2129. outBuffer[0] = 0xff;
  2130. outBuffer[1] = 0xff;
  2131. outBuffer[2] = 0x00;
  2132. outBuffer[3] = 0x22;
  2133. outBuffer[4] = WIFI_CMD_GET_VER_AUX_POWER;
  2134. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.AuxPwrFwRev));
  2135. outBuffer[37] = chksumCal(outBuffer);
  2136. break;
  2137. case WIFI_CMD_GET_VER_FAN:
  2138. tx_size = 38;
  2139. outBuffer[0] = 0xff;
  2140. outBuffer[1] = 0xff;
  2141. outBuffer[2] = 0x00;
  2142. outBuffer[3] = 0x22;
  2143. outBuffer[4] = WIFI_CMD_GET_VER_FAN;
  2144. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.FanModuleFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.FanModuleFwRev));
  2145. outBuffer[37] = chksumCal(outBuffer);
  2146. break;
  2147. case WIFI_CMD_GET_VER_RELAY:
  2148. tx_size = 38;
  2149. outBuffer[0] = 0xff;
  2150. outBuffer[1] = 0xff;
  2151. outBuffer[2] = 0x00;
  2152. outBuffer[3] = 0x22;
  2153. outBuffer[4] = WIFI_CMD_GET_VER_RELAY;
  2154. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysInfo.RelayModuleFwRev));
  2155. outBuffer[37] = chksumCal(outBuffer);
  2156. break;
  2157. case WIFI_CMD_GET_VER_TELECOMM:
  2158. tx_size = 38;
  2159. outBuffer[0] = 0xff;
  2160. outBuffer[1] = 0xff;
  2161. outBuffer[2] = 0x00;
  2162. outBuffer[3] = 0x22;
  2163. outBuffer[4] = WIFI_CMD_GET_VER_TELECOMM;
  2164. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSoftwareVer));
  2165. outBuffer[37] = chksumCal(outBuffer);
  2166. break;
  2167. case WIFI_CMD_GET_EVSE_CONFIG:
  2168. tx_size = 16;
  2169. outBuffer[0] = 0xff;
  2170. outBuffer[1] = 0xff;
  2171. outBuffer[2] = 0x00;
  2172. outBuffer[3] = 0x0C;
  2173. outBuffer[4] = WIFI_CMD_GET_EVSE_CONFIG;
  2174. outBuffer[5] = (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy>>0x08) & 0xff;
  2175. outBuffer[6] = (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy>>0x00) & 0xff;
  2176. outBuffer[7] = (ShmSysConfigAndInfo->SysConfig.MaxChargingPower>>0x08) & 0xff;
  2177. outBuffer[8] = (ShmSysConfigAndInfo->SysConfig.MaxChargingPower>>0x00) & 0xff;
  2178. outBuffer[9] = (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent>>0x08) & 0xff;
  2179. outBuffer[10] = (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent>>0x00) & 0xff;
  2180. outBuffer[11] = (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration>>0x08) & 0xff;
  2181. outBuffer[12] = (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration>>0x00) & 0xff;
  2182. outBuffer[13] = (ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent>>0x08) & 0xff;
  2183. outBuffer[14] = (ShmSysConfigAndInfo->SysConfig.AcMaxChargingCurrent>>0x00) & 0xff;
  2184. outBuffer[15] = chksumCal(outBuffer);
  2185. break;
  2186. case WIFI_CMD_GET_GUN_STS:
  2187. tx_size = 21;
  2188. outBuffer[0] = 0xff;
  2189. outBuffer[1] = 0xff;
  2190. outBuffer[2] = 0x00;
  2191. outBuffer[3] = 0x11;
  2192. outBuffer[4] = WIFI_CMD_GET_GUN_STS;
  2193. outBuffer[5] = inputBuffer[9];
  2194. getConnectorInfo(inputBuffer[9], outBuffer);
  2195. outBuffer[20] = chksumCal(outBuffer);
  2196. break;
  2197. case WIFI_CMD_GET_GUN_USER:
  2198. tx_size = 40;
  2199. outBuffer[0] = 0xff;
  2200. outBuffer[1] = 0xff;
  2201. outBuffer[2] = 0x00;
  2202. outBuffer[3] = 0x24;
  2203. outBuffer[4] = WIFI_CMD_GET_GUN_USER;
  2204. outBuffer[5] = inputBuffer[9];
  2205. getConnectorUser(inputBuffer[9], outBuffer);
  2206. outBuffer[39] = chksumCal(outBuffer);
  2207. break;
  2208. case WIFI_CMD_GET_SCHEDULE:
  2209. tx_size = 10;
  2210. outBuffer[0] = 0xff;
  2211. outBuffer[1] = 0xff;
  2212. outBuffer[2] = 0x00;
  2213. outBuffer[3] = 0x06;
  2214. outBuffer[4] = WIFI_CMD_GET_SCHEDULE;
  2215. outBuffer[5] = inputBuffer[9];
  2216. getConnectorSchedule(inputBuffer[9], outBuffer);
  2217. outBuffer[9] = chksumCal(outBuffer);
  2218. break;
  2219. case WIFI_CMD_GET_NETWORK_STS:
  2220. tx_size = 7;
  2221. outBuffer[0] = 0xff;
  2222. outBuffer[1] = 0xff;
  2223. outBuffer[2] = 0x00;
  2224. outBuffer[3] = 0x03;
  2225. outBuffer[4] = WIFI_CMD_GET_NETWORK_STS;
  2226. outBuffer[5] = ShmSysConfigAndInfo->SysInfo.InternetConn;
  2227. outBuffer[6] = chksumCal(outBuffer);
  2228. break;
  2229. case WIFI_CMD_GET_ETH_DHCP:
  2230. tx_size = 7;
  2231. outBuffer[0] = 0xff;
  2232. outBuffer[1] = 0xff;
  2233. outBuffer[2] = 0x00;
  2234. outBuffer[3] = 0x03;
  2235. outBuffer[4] = WIFI_CMD_GET_ETH_DHCP;
  2236. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient^1;
  2237. outBuffer[6] = chksumCal(outBuffer);
  2238. break;
  2239. case WIFI_CMD_GET_ETH_MAC:
  2240. tx_size = 24;
  2241. outBuffer[0] = 0xff;
  2242. outBuffer[1] = 0xff;
  2243. outBuffer[2] = 0x00;
  2244. outBuffer[3] = 0x14;
  2245. outBuffer[4] = WIFI_CMD_GET_ETH_MAC;
  2246. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress));
  2247. outBuffer[23] = chksumCal(outBuffer);
  2248. break;
  2249. case WIFI_CMD_GET_ETH_IP:
  2250. tx_size = 22;
  2251. outBuffer[0] = 0xff;
  2252. outBuffer[1] = 0xff;
  2253. outBuffer[2] = 0x00;
  2254. outBuffer[3] = 0x12;
  2255. outBuffer[4] = WIFI_CMD_GET_ETH_IP;
  2256. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress));
  2257. outBuffer[21] = chksumCal(outBuffer);
  2258. break;
  2259. case WIFI_CMD_GET_ETH_SUBMASK:
  2260. tx_size = 22;
  2261. outBuffer[0] = 0xff;
  2262. outBuffer[1] = 0xff;
  2263. outBuffer[2] = 0x00;
  2264. outBuffer[3] = 0x12;
  2265. outBuffer[4] = WIFI_CMD_GET_ETH_SUBMASK;
  2266. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress));
  2267. outBuffer[21] = chksumCal(outBuffer);
  2268. break;
  2269. case WIFI_CMD_GET_ETH_GATEWAY:
  2270. tx_size = 22;
  2271. outBuffer[0] = 0xff;
  2272. outBuffer[1] = 0xff;
  2273. outBuffer[2] = 0x00;
  2274. outBuffer[3] = 0x12;
  2275. outBuffer[4] = WIFI_CMD_GET_ETH_GATEWAY;
  2276. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress));
  2277. outBuffer[21] = chksumCal(outBuffer);
  2278. break;
  2279. case WIFI_CMD_GET_WIFI_MODE:
  2280. tx_size = 7;
  2281. outBuffer[0] = 0xff;
  2282. outBuffer[1] = 0xff;
  2283. outBuffer[2] = 0x00;
  2284. outBuffer[3] = 0x03;
  2285. outBuffer[4] = WIFI_CMD_GET_WIFI_MODE;
  2286. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode;
  2287. outBuffer[6] = chksumCal(outBuffer);
  2288. break;
  2289. case WIFI_CMD_GET_WIFI_SSID:
  2290. tx_size = 261;
  2291. outBuffer[0] = 0xff;
  2292. outBuffer[1] = 0xff;
  2293. outBuffer[2] = 0x01;
  2294. outBuffer[3] = 0x01;
  2295. outBuffer[4] = WIFI_CMD_GET_WIFI_SSID;
  2296. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid));
  2297. outBuffer[260] = chksumCal(outBuffer);
  2298. break;
  2299. case WIFI_CMD_GET_WIFI_PASSWD:
  2300. tx_size = 261;
  2301. outBuffer[0] = 0xff;
  2302. outBuffer[1] = 0xff;
  2303. outBuffer[2] = 0x01;
  2304. outBuffer[3] = 0x01;
  2305. outBuffer[4] = WIFI_CMD_GET_WIFI_PASSWD;
  2306. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword));
  2307. outBuffer[260] = chksumCal(outBuffer);
  2308. break;
  2309. case WIFI_CMD_GET_WIFI_RSSI:
  2310. tx_size = 7;
  2311. outBuffer[0] = 0xff;
  2312. outBuffer[1] = 0xff;
  2313. outBuffer[2] = 0x00;
  2314. outBuffer[3] = 0x03;
  2315. outBuffer[4] = WIFI_CMD_GET_WIFI_RSSI;
  2316. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi+100;
  2317. outBuffer[6] = chksumCal(outBuffer);
  2318. break;
  2319. case WIFI_CMD_GET_WIFI_DHCP:
  2320. tx_size = 7;
  2321. outBuffer[0] = 0xff;
  2322. outBuffer[1] = 0xff;
  2323. outBuffer[2] = 0x00;
  2324. outBuffer[3] = 0x03;
  2325. outBuffer[4] = WIFI_CMD_GET_WIFI_DHCP;
  2326. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient^1;
  2327. outBuffer[6] = chksumCal(outBuffer);
  2328. break;
  2329. case WIFI_CMD_GET_WIFI_MAC:
  2330. tx_size = 24;
  2331. outBuffer[0] = 0xff;
  2332. outBuffer[1] = 0xff;
  2333. outBuffer[2] = 0x00;
  2334. outBuffer[3] = 0x14;
  2335. outBuffer[4] = WIFI_CMD_GET_WIFI_MAC;
  2336. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress));
  2337. outBuffer[23] = chksumCal(outBuffer);
  2338. break;
  2339. case WIFI_CMD_GET_WIFI_IP:
  2340. tx_size = 22;
  2341. outBuffer[0] = 0xff;
  2342. outBuffer[1] = 0xff;
  2343. outBuffer[2] = 0x00;
  2344. outBuffer[3] = 0x12;
  2345. outBuffer[4] = WIFI_CMD_GET_WIFI_IP;
  2346. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress));
  2347. outBuffer[21] = chksumCal(outBuffer);
  2348. break;
  2349. case WIFI_CMD_GET_WIFI_NETMASK:
  2350. tx_size = 22;
  2351. outBuffer[0] = 0xff;
  2352. outBuffer[1] = 0xff;
  2353. outBuffer[2] = 0x00;
  2354. outBuffer[3] = 0x12;
  2355. outBuffer[4] = WIFI_CMD_GET_WIFI_NETMASK;
  2356. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress));
  2357. outBuffer[21] = chksumCal(outBuffer);
  2358. break;
  2359. case WIFI_CMD_GET_WIFI_GATEWAY:
  2360. tx_size = 22;
  2361. outBuffer[0] = 0xff;
  2362. outBuffer[1] = 0xff;
  2363. outBuffer[2] = 0x00;
  2364. outBuffer[3] = 0x12;
  2365. outBuffer[4] = WIFI_CMD_GET_WIFI_GATEWAY;
  2366. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress));
  2367. outBuffer[21] = chksumCal(outBuffer);
  2368. break;
  2369. case WIFI_CMD_GET_TELE_APN:
  2370. tx_size = 262;
  2371. outBuffer[0] = 0xff;
  2372. outBuffer[1] = 0xff;
  2373. outBuffer[2] = 0x01;
  2374. outBuffer[3] = 0x02;
  2375. outBuffer[4] = WIFI_CMD_GET_TELE_APN;
  2376. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomApn));
  2377. outBuffer[261] = chksumCal(outBuffer);
  2378. break;
  2379. case WIFI_CMD_GET_TELE_RSSI:
  2380. tx_size = 7;
  2381. outBuffer[0] = 0xff;
  2382. outBuffer[1] = 0xff;
  2383. outBuffer[2] = 0x00;
  2384. outBuffer[3] = 0x03;
  2385. outBuffer[4] = WIFI_CMD_GET_TELE_RSSI;
  2386. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomRssi+100;
  2387. outBuffer[6] = chksumCal(outBuffer);
  2388. break;
  2389. case WIFI_CMD_GET_TELE_CHAP_ID:
  2390. tx_size = 262;
  2391. outBuffer[0] = 0xff;
  2392. outBuffer[1] = 0xff;
  2393. outBuffer[2] = 0x01;
  2394. outBuffer[3] = 0x02;
  2395. outBuffer[4] = WIFI_CMD_GET_TELE_CHAP_ID;
  2396. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapId));
  2397. outBuffer[261] = chksumCal(outBuffer);
  2398. break;
  2399. case WIFI_CMD_GET_TELE_CHAP_PASSWD:
  2400. tx_size = 262;
  2401. outBuffer[0] = 0xff;
  2402. outBuffer[1] = 0xff;
  2403. outBuffer[2] = 0x01;
  2404. outBuffer[3] = 0x02;
  2405. outBuffer[4] = WIFI_CMD_GET_TELE_CHAP_PASSWD;
  2406. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomChapPapPwd));
  2407. outBuffer[261] = chksumCal(outBuffer);
  2408. break;
  2409. case WIFI_CMD_GET_TELE_IMEI:
  2410. tx_size = 22;
  2411. outBuffer[0] = 0xff;
  2412. outBuffer[1] = 0xff;
  2413. outBuffer[2] = 0x00;
  2414. outBuffer[3] = 0x12;
  2415. outBuffer[4] = WIFI_CMD_GET_TELE_IMEI;
  2416. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemImei));
  2417. outBuffer[21] = chksumCal(outBuffer);
  2418. break;
  2419. case WIFI_CMD_GET_TELE_IMSI:
  2420. tx_size = 22;
  2421. outBuffer[0] = 0xff;
  2422. outBuffer[1] = 0xff;
  2423. outBuffer[2] = 0x00;
  2424. outBuffer[3] = 0x12;
  2425. outBuffer[4] = WIFI_CMD_GET_TELE_IMSI;
  2426. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimImsi));
  2427. outBuffer[21] = chksumCal(outBuffer);
  2428. break;
  2429. case WIFI_CMD_GET_TELE_SIM_STS:
  2430. tx_size = 7;
  2431. outBuffer[0] = 0xff;
  2432. outBuffer[1] = 0xff;
  2433. outBuffer[2] = 0x00;
  2434. outBuffer[3] = 0x03;
  2435. outBuffer[4] = WIFI_CMD_GET_TELE_SIM_STS;
  2436. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomSimStatus;
  2437. outBuffer[6] = chksumCal(outBuffer);
  2438. break;
  2439. case WIFI_CMD_GET_TELE_MODE:
  2440. tx_size = 7;
  2441. outBuffer[0] = 0xff;
  2442. outBuffer[1] = 0xff;
  2443. outBuffer[2] = 0x00;
  2444. outBuffer[3] = 0x03;
  2445. outBuffer[4] = WIFI_CMD_GET_TELE_MODE;
  2446. outBuffer[5] = ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomModemMode;
  2447. outBuffer[6] = chksumCal(outBuffer);
  2448. break;
  2449. case WIFI_CMD_GET_TELE_IP:
  2450. tx_size = 22;
  2451. outBuffer[0] = 0xff;
  2452. outBuffer[1] = 0xff;
  2453. outBuffer[2] = 0x00;
  2454. outBuffer[3] = 0x12;
  2455. outBuffer[4] = WIFI_CMD_GET_TELE_IP;
  2456. memcpy(&outBuffer[5], ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomIpAddress));
  2457. outBuffer[21] = chksumCal(outBuffer);
  2458. break;
  2459. case WIFI_CMD_SET_SYSTEM_TIME:
  2460. rawtime = (inputBuffer[9]<<0) | (inputBuffer[10]<<8) | (inputBuffer[11]<<16) |(inputBuffer[12]<<24);
  2461. ts = *localtime(&rawtime);
  2462. sprintf((char*)cmdBuf, "date -u -s \"%04d-%02d-%02d %02d:%02d:%02d\"", (ts.tm_year+1900),
  2463. (ts.tm_mon+1),
  2464. ts.tm_mday,
  2465. ts.tm_hour,
  2466. ts.tm_min,
  2467. ts.tm_sec);
  2468. system((char*)cmdBuf);
  2469. system("hwclock -w -u");
  2470. system("hwclock -s");
  2471. tx_size = 7;
  2472. outBuffer[0] = 0xff;
  2473. outBuffer[1] = 0xff;
  2474. outBuffer[2] = 0x00;
  2475. outBuffer[3] = 0x03;
  2476. outBuffer[4] = WIFI_CMD_SET_SYSTEM_TIME;
  2477. outBuffer[5] = 0x01;
  2478. outBuffer[6] = chksumCal(outBuffer);
  2479. break;
  2480. case WIFI_CMD_SET_WHITE_CARD:
  2481. if((0<=inputBuffer[9]) && (inputBuffer[9]<10))
  2482. {
  2483. if(inputBuffer[10] == REGISTER)
  2484. {
  2485. // Register white card
  2486. memcpy(SysConfigTemporarily.LocalWhiteCard[inputBuffer[9]], &inputBuffer[11],(((inputBuffer[2] << 8) | inputBuffer[3])-8));
  2487. }
  2488. else if (inputBuffer[10] == CLEAR_REGISTER)
  2489. {
  2490. // Clear register white card
  2491. memset(SysConfigTemporarily.LocalWhiteCard[inputBuffer[9]], 0x00, ARRAY_SIZE(SysConfigTemporarily.LocalWhiteCard[inputBuffer[9]]));
  2492. }
  2493. outBuffer[6] = 0x01;
  2494. }
  2495. else
  2496. {
  2497. outBuffer[6] = 0x00;
  2498. }
  2499. tx_size = 8;
  2500. outBuffer[0] = 0xff;
  2501. outBuffer[1] = 0xff;
  2502. outBuffer[2] = 0x00;
  2503. outBuffer[3] = 0x04;
  2504. outBuffer[4] = WIFI_CMD_SET_WHITE_CARD;
  2505. outBuffer[5] = inputBuffer[9];
  2506. outBuffer[7] = chksumCal(outBuffer);
  2507. break;
  2508. case WIFI_CMD_SET_FACTORY_RESET:
  2509. ShmSysConfigAndInfo->SysInfo.FactoryConfiguration = ON;
  2510. tx_size = 7;
  2511. outBuffer[0] = 0xff;
  2512. outBuffer[1] = 0xff;
  2513. outBuffer[2] = 0x00;
  2514. outBuffer[3] = 0x03;
  2515. outBuffer[4] = WIFI_CMD_SET_FACTORY_RESET;
  2516. outBuffer[5] = 0x01;
  2517. outBuffer[6] = chksumCal(outBuffer);
  2518. break;
  2519. case WIFI_CMD_SET_EVSE_CONFIG:
  2520. if((inputBuffer[9]>>4)&0x01)
  2521. {
  2522. SysConfigTemporarily.AcMaxChargingCurrent = (inputBuffer[18]) | (inputBuffer[19]<<8);
  2523. }
  2524. if((inputBuffer[9]>>3)&0x01)
  2525. {
  2526. SysConfigTemporarily.MaxChargingEnergy = (inputBuffer[10]) | (inputBuffer[11]<<8);
  2527. }
  2528. if((inputBuffer[9]>>2)&0x01)
  2529. {
  2530. SysConfigTemporarily.MaxChargingPower = (inputBuffer[12]) | (inputBuffer[13]<<8);
  2531. }
  2532. if((inputBuffer[9]>>1)&0x01)
  2533. {
  2534. SysConfigTemporarily.MaxChargingCurrent = (inputBuffer[14]) | (inputBuffer[15]<<8);
  2535. }
  2536. if(inputBuffer[9]&0x01)
  2537. {
  2538. SysConfigTemporarily.MaxChargingDuration = (inputBuffer[16]) | (inputBuffer[17]<<8);
  2539. }
  2540. tx_size = 7;
  2541. outBuffer[0] = 0xff;
  2542. outBuffer[1] = 0xff;
  2543. outBuffer[2] = 0x00;
  2544. outBuffer[3] = 0x03;
  2545. outBuffer[4] = WIFI_CMD_SET_EVSE_CONFIG;
  2546. outBuffer[5] = 0x01;
  2547. outBuffer[6] = chksumCal(outBuffer);
  2548. break;
  2549. case WIFI_CMD_SET_CHARGING_START:
  2550. tx_size = 7;
  2551. outBuffer[0] = 0xff;
  2552. outBuffer[1] = 0xff;
  2553. outBuffer[2] = 0x00;
  2554. outBuffer[3] = 0x03;
  2555. outBuffer[4] = WIFI_CMD_SET_CHARGING_START;
  2556. outBuffer[5] = (startConnectorCharging(inputBuffer[9])?0x01:0x00);
  2557. outBuffer[6] = chksumCal(outBuffer);
  2558. break;
  2559. case WIFI_CMD_SET_CHARGING_STOP:
  2560. tx_size = 7;
  2561. outBuffer[0] = 0xff;
  2562. outBuffer[1] = 0xff;
  2563. outBuffer[2] = 0x00;
  2564. outBuffer[3] = 0x03;
  2565. outBuffer[4] = WIFI_CMD_SET_CHARGING_STOP;
  2566. outBuffer[5] = (stopConnectorCharging(inputBuffer[9])?0x01:0x00);
  2567. outBuffer[6] = chksumCal(outBuffer);
  2568. break;
  2569. case WIFI_CMD_SET_SCHEDULE:
  2570. setConnectorSchedule(inputBuffer[9], inputBuffer);
  2571. tx_size = 7;
  2572. outBuffer[0] = 0xff;
  2573. outBuffer[1] = 0xff;
  2574. outBuffer[2] = 0x00;
  2575. outBuffer[3] = 0x03;
  2576. outBuffer[4] = WIFI_CMD_SET_SCHEDULE;
  2577. outBuffer[5] = 0x01;
  2578. outBuffer[6] = chksumCal(outBuffer);
  2579. break;
  2580. case WIFI_CMD_SET_ETH_CONFIG:
  2581. SysConfigTemporarily.Eth0Interface.EthDhcpClient = inputBuffer[9]^1;
  2582. tx_size = 7;
  2583. outBuffer[0] = 0xff;
  2584. outBuffer[1] = 0xff;
  2585. outBuffer[2] = 0x00;
  2586. outBuffer[3] = 0x03;
  2587. outBuffer[4] = WIFI_CMD_SET_ETH_CONFIG;
  2588. outBuffer[5] = 0x01;
  2589. outBuffer[6] = chksumCal(outBuffer);
  2590. break;
  2591. case WIFI_CMD_SET_ETH_IP:
  2592. memset(SysConfigTemporarily.Eth0Interface.EthIpAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.Eth0Interface.EthIpAddress));
  2593. memcpy(SysConfigTemporarily.Eth0Interface.EthIpAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2594. tx_size = 7;
  2595. outBuffer[0] = 0xff;
  2596. outBuffer[1] = 0xff;
  2597. outBuffer[2] = 0x00;
  2598. outBuffer[3] = 0x03;
  2599. outBuffer[4] = WIFI_CMD_SET_ETH_IP;
  2600. outBuffer[5] = 0x01;
  2601. outBuffer[6] = chksumCal(outBuffer);
  2602. break;
  2603. case WIFI_CMD_SET_ETH_SUBMASK:
  2604. memset(SysConfigTemporarily.Eth0Interface.EthSubmaskAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.Eth0Interface.EthSubmaskAddress));
  2605. memcpy(SysConfigTemporarily.Eth0Interface.EthSubmaskAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2606. tx_size = 7;
  2607. outBuffer[0] = 0xff;
  2608. outBuffer[1] = 0xff;
  2609. outBuffer[2] = 0x00;
  2610. outBuffer[3] = 0x03;
  2611. outBuffer[4] = WIFI_CMD_SET_ETH_SUBMASK;
  2612. outBuffer[5] = 0x01;
  2613. outBuffer[6] = chksumCal(outBuffer);
  2614. break;
  2615. case WIFI_CMD_SET_ETH_GATEWAY:
  2616. memset(SysConfigTemporarily.Eth0Interface.EthGatewayAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.Eth0Interface.EthGatewayAddress));
  2617. memcpy(SysConfigTemporarily.Eth0Interface.EthGatewayAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2618. tx_size = 7;
  2619. outBuffer[0] = 0xff;
  2620. outBuffer[1] = 0xff;
  2621. outBuffer[2] = 0x00;
  2622. outBuffer[3] = 0x03;
  2623. outBuffer[4] = WIFI_CMD_SET_ETH_GATEWAY;
  2624. outBuffer[5] = 0x01;
  2625. outBuffer[6] = chksumCal(outBuffer);
  2626. break;
  2627. case WIFI_CMD_SET_WIFI_MODE:
  2628. SysConfigTemporarily.AthInterface.WifiMode = inputBuffer[9];
  2629. tx_size = 7;
  2630. outBuffer[0] = 0xff;
  2631. outBuffer[1] = 0xff;
  2632. outBuffer[2] = 0x00;
  2633. outBuffer[3] = 0x03;
  2634. outBuffer[4] = WIFI_CMD_SET_WIFI_MODE;
  2635. outBuffer[5] = 0x01;
  2636. outBuffer[6] = chksumCal(outBuffer);
  2637. break;
  2638. case WIFI_CMD_SET_WIFI_SSID:
  2639. memset(SysConfigTemporarily.AthInterface.WifiSsid, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiSsid));
  2640. memcpy(SysConfigTemporarily.AthInterface.WifiSsid, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2641. tx_size = 7;
  2642. outBuffer[0] = 0xff;
  2643. outBuffer[1] = 0xff;
  2644. outBuffer[2] = 0x00;
  2645. outBuffer[3] = 0x03;
  2646. outBuffer[4] = WIFI_CMD_SET_WIFI_SSID;
  2647. outBuffer[5] = 0x01;
  2648. outBuffer[6] = chksumCal(outBuffer);
  2649. break;
  2650. case WIFI_CMD_SET_WIFI_PASSWD:
  2651. memset(SysConfigTemporarily.AthInterface.WifiPassword, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiPassword));
  2652. memcpy(SysConfigTemporarily.AthInterface.WifiPassword, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2653. tx_size = 7;
  2654. outBuffer[0] = 0xff;
  2655. outBuffer[1] = 0xff;
  2656. outBuffer[2] = 0x00;
  2657. outBuffer[3] = 0x03;
  2658. outBuffer[4] = WIFI_CMD_SET_WIFI_PASSWD;
  2659. outBuffer[5] = 0x01;
  2660. outBuffer[6] = chksumCal(outBuffer);
  2661. break;
  2662. case WIFI_CMD_SET_WIFI_DHCP:
  2663. SysConfigTemporarily.AthInterface.WifiDhcpClient = inputBuffer[9]^1;
  2664. tx_size = 7;
  2665. outBuffer[0] = 0xff;
  2666. outBuffer[1] = 0xff;
  2667. outBuffer[2] = 0x00;
  2668. outBuffer[3] = 0x03;
  2669. outBuffer[4] = WIFI_CMD_SET_WIFI_DHCP;
  2670. outBuffer[5] = 0x01;
  2671. outBuffer[6] = chksumCal(outBuffer);
  2672. break;
  2673. case WIFI_CMD_SET_WIFI_IP:
  2674. memset(SysConfigTemporarily.AthInterface.WifiIpAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiIpAddress));
  2675. memcpy(SysConfigTemporarily.AthInterface.WifiIpAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2676. tx_size = 7;
  2677. outBuffer[0] = 0xff;
  2678. outBuffer[1] = 0xff;
  2679. outBuffer[2] = 0x00;
  2680. outBuffer[3] = 0x03;
  2681. outBuffer[4] = WIFI_CMD_SET_WIFI_IP;
  2682. outBuffer[5] = 0x01;
  2683. outBuffer[6] = chksumCal(outBuffer);
  2684. break;
  2685. case WIFI_CMD_SET_WIFI_NETMASK:
  2686. memset(SysConfigTemporarily.AthInterface.WifiSubmaskAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiSubmaskAddress));
  2687. memcpy(SysConfigTemporarily.AthInterface.WifiSubmaskAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2688. tx_size = 7;
  2689. outBuffer[0] = 0xff;
  2690. outBuffer[1] = 0xff;
  2691. outBuffer[2] = 0x00;
  2692. outBuffer[3] = 0x03;
  2693. outBuffer[4] = WIFI_CMD_SET_WIFI_NETMASK;
  2694. outBuffer[5] = 0x01;
  2695. outBuffer[6] = chksumCal(outBuffer);
  2696. break;
  2697. case WIFI_CMD_SET_WIFI_GATEWAY:
  2698. memset(SysConfigTemporarily.AthInterface.WifiGatewayAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.AthInterface.WifiGatewayAddress));
  2699. memcpy(SysConfigTemporarily.AthInterface.WifiGatewayAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2700. tx_size = 7;
  2701. outBuffer[0] = 0xff;
  2702. outBuffer[1] = 0xff;
  2703. outBuffer[2] = 0x00;
  2704. outBuffer[3] = 0x03;
  2705. outBuffer[4] = WIFI_CMD_SET_WIFI_GATEWAY;
  2706. outBuffer[5] = 0x01;
  2707. outBuffer[6] = chksumCal(outBuffer);
  2708. break;
  2709. case WIFI_CMD_SET_TELE_APN:
  2710. memset(SysConfigTemporarily.TelecomInterface.TelcomApn, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomApn));
  2711. memcpy(SysConfigTemporarily.TelecomInterface.TelcomApn, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2712. tx_size = 7;
  2713. outBuffer[0] = 0xff;
  2714. outBuffer[1] = 0xff;
  2715. outBuffer[2] = 0x00;
  2716. outBuffer[3] = 0x03;
  2717. outBuffer[4] = WIFI_CMD_SET_TELE_APN;
  2718. outBuffer[5] = 0x01;
  2719. outBuffer[6] = chksumCal(outBuffer);
  2720. break;
  2721. case WIFI_CMD_SET_TELE_CHAP_ID:
  2722. memset(SysConfigTemporarily.TelecomInterface.TelcomChapPapId, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomChapPapId));
  2723. memcpy(SysConfigTemporarily.TelecomInterface.TelcomChapPapId, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2724. tx_size = 7;
  2725. outBuffer[0] = 0xff;
  2726. outBuffer[1] = 0xff;
  2727. outBuffer[2] = 0x00;
  2728. outBuffer[3] = 0x03;
  2729. outBuffer[4] = WIFI_CMD_SET_TELE_CHAP_ID;
  2730. outBuffer[5] = 0x01;
  2731. outBuffer[6] = chksumCal(outBuffer);
  2732. break;
  2733. case WIFI_CMD_SET_TELE_CHAP_PASSWD:
  2734. memset(SysConfigTemporarily.TelecomInterface.TelcomChapPapPwd, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomChapPapPwd));
  2735. memcpy(SysConfigTemporarily.TelecomInterface.TelcomChapPapPwd, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2736. tx_size = 7;
  2737. outBuffer[0] = 0xff;
  2738. outBuffer[1] = 0xff;
  2739. outBuffer[2] = 0x00;
  2740. outBuffer[3] = 0x03;
  2741. outBuffer[4] = WIFI_CMD_SET_TELE_CHAP_PASSWD;
  2742. outBuffer[5] = 0x01;
  2743. outBuffer[6] = chksumCal(outBuffer);
  2744. break;
  2745. case WIFI_CMD_SET_TELE_IP:
  2746. memset(SysConfigTemporarily.TelecomInterface.TelcomIpAddress, 0x00, ARRAY_SIZE(SysConfigTemporarily.TelecomInterface.TelcomIpAddress));
  2747. memcpy(SysConfigTemporarily.TelecomInterface.TelcomIpAddress, &inputBuffer[9], (((inputBuffer[2] << 8) | inputBuffer[3])-6));
  2748. tx_size = 7;
  2749. outBuffer[0] = 0xff;
  2750. outBuffer[1] = 0xff;
  2751. outBuffer[2] = 0x00;
  2752. outBuffer[3] = 0x03;
  2753. outBuffer[4] = WIFI_CMD_SET_TELE_IP;
  2754. outBuffer[5] = 0x01;
  2755. outBuffer[6] = chksumCal(outBuffer);
  2756. break;
  2757. case WIFI_CMD_SET_CONFIRM:
  2758. // Copy SysConfigTemporarily to ShmSysConfigAndInfo->SysConfig
  2759. memcpy(&ShmSysConfigAndInfo->SysConfig,&SysConfigTemporarily, sizeof(struct SysConfigData));
  2760. tx_size = 7;
  2761. outBuffer[0] = 0xff;
  2762. outBuffer[1] = 0xff;
  2763. outBuffer[2] = 0x00;
  2764. outBuffer[3] = 0x03;
  2765. outBuffer[4] = WIFI_CMD_SET_CONFIRM;
  2766. outBuffer[5] = (StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig)!=0x01?0x00:0x01);
  2767. outBuffer[6] = chksumCal(outBuffer);
  2768. Sys_Flag.isPkill = ON;
  2769. break;
  2770. case WIFI_CMD_SET_DISCARD:
  2771. // Copy ShmSysConfigAndInfo to SysConfigTemporarily
  2772. memcpy(&SysConfigTemporarily, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2773. tx_size = 7;
  2774. outBuffer[0] = 0xff;
  2775. outBuffer[1] = 0xff;
  2776. outBuffer[2] = 0x00;
  2777. outBuffer[3] = 0x03;
  2778. outBuffer[4] = WIFI_CMD_SET_DISCARD;
  2779. outBuffer[5] = 0x01;
  2780. outBuffer[6] = chksumCal(outBuffer);
  2781. break;
  2782. default:
  2783. tx_size = 6;
  2784. outBuffer[0] = 0xff;
  2785. outBuffer[1] = 0xff;
  2786. outBuffer[2] = 0x00;
  2787. outBuffer[3] = 0x02;
  2788. outBuffer[4] = WIFI_CMD_UNKNOW;
  2789. outBuffer[5] = chksumCal(outBuffer);
  2790. break;
  2791. }
  2792. }
  2793. else
  2794. {
  2795. tx_size = 6;
  2796. outBuffer[0] = 0xff;
  2797. outBuffer[1] = 0xff;
  2798. outBuffer[2] = 0x00;
  2799. outBuffer[3] = 0x02;
  2800. outBuffer[4] = inputBuffer[8];
  2801. outBuffer[5] = chksumCal(outBuffer);
  2802. }
  2803. send(clientSockFd, outBuffer, tx_size, 0);
  2804. }
  2805. if(read_size == 0)
  2806. {
  2807. DEBUG_INFO("Client disconnected.\r\n");
  2808. wifi_login_info.loginRole = ROLE_UNKNOWN;
  2809. memset(&wifi_login_info.loginId[0], 0x00, ARRAY_SIZE(wifi_login_info.loginId));
  2810. close(clientSockFd);
  2811. if(Sys_Flag.isPkill == ON)
  2812. {
  2813. Sys_Flag.isPkill = OFF;
  2814. shutdown(sockFd, SHUT_RDWR);
  2815. close(sockFd);
  2816. DEBUG_INFO("pKill Module_Wifi after set confirm...\r\n");
  2817. system("pkill Module_Wifi");
  2818. }
  2819. // Copy ShmSysConfigAndInfo to SysConfigTemporarily
  2820. memcpy(&SysConfigTemporarily, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  2821. fflush(stdout);
  2822. }
  2823. else if(read_size == -1)
  2824. {
  2825. DEBUG_ERROR("Socket recv failed.\r\n");
  2826. }
  2827. sleep(1);
  2828. }
  2829. return FAIL;
  2830. }
  2831. //==========================================
  2832. // Common routine
  2833. //==========================================
  2834. char* getTimeString(void)
  2835. {
  2836. char *result=malloc(21);
  2837. time_t timep;
  2838. struct tm *p;
  2839. time(&timep);
  2840. p=gmtime(&timep);
  2841. 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);
  2842. return result;
  2843. }
  2844. void trim(char *s)
  2845. {
  2846. int i=0, j, k, l=0;
  2847. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  2848. i++;
  2849. j = strlen(s)-1;
  2850. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  2851. j--;
  2852. if(i==0 && j==strlen(s)-1) { }
  2853. else if(i==0) s[j+1] = '\0';
  2854. else {
  2855. for(k=i; k<=j; k++) s[l++] = s[k];
  2856. s[l] = '\0';
  2857. }
  2858. }
  2859. int mystrcmp(char *p1,char *p2)
  2860. {
  2861. while(*p1==*p2)
  2862. {
  2863. if(*p1=='\0' || *p2=='\0')
  2864. break;
  2865. p1++;
  2866. p2++;
  2867. }
  2868. if(*p1=='\0' && *p2=='\0')
  2869. return(PASS);
  2870. else
  2871. return(FAIL);
  2872. }
  2873. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  2874. {
  2875. strncpy(dest, src + start, cnt);
  2876. dest[cnt] = 0;
  2877. }
  2878. void split(char **arr, char *str, const char *del)
  2879. {
  2880. char *s = strtok(str, del);
  2881. while(s != NULL)
  2882. {
  2883. *arr++ = s;
  2884. s = strtok(NULL, del);
  2885. }
  2886. }
  2887. int checkIP(void)
  2888. {
  2889. // DHCP or static setting
  2890. int isGetIP = FAIL;
  2891. FILE *fp;
  2892. char buf[512];
  2893. char tmp[512];
  2894. char cmd[512];
  2895. if(Wifi.ipType == IP_TYPE_DHCP) {
  2896. // Get IP address & net mask
  2897. strcpy(cmd, "ifconfig ");
  2898. strcat(cmd, Wifi.currentInterface);
  2899. fp = popen(cmd, "r");
  2900. if(fp == NULL)
  2901. isGetIP = FAIL;
  2902. else
  2903. {
  2904. while(fgets(buf, sizeof(buf), fp) != NULL)
  2905. {
  2906. if(strstr(buf, "addr:") > 0)
  2907. {
  2908. if(strstr(buf, "192.168.1.10") > 0)
  2909. {
  2910. DEBUG_INFO("IP cannot be same as default value of Ethernet...\n");
  2911. }
  2912. else
  2913. {
  2914. isGetIP = PASS;
  2915. cnt_getIP_Fail = 0;
  2916. }
  2917. sscanf(buf, "%*s%s", tmp);
  2918. substr(Wifi.currentIPAddr, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  2919. sscanf(buf, "%*s%*s%*s%s", tmp);
  2920. substr(Wifi.currentNetmask, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  2921. }else {
  2922. }
  2923. }
  2924. }
  2925. if(isGetIP == FAIL && cnt_getIP_Fail<3) {
  2926. sprintf(cmd, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  2927. system(cmd);
  2928. if(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiDhcpClient==0)
  2929. {
  2930. sprintf(cmd, "/sbin/udhcpc -i %s -s /root/dhcp_script/wifi.script > /dev/null &", Wifi.currentInterface);
  2931. system(cmd);
  2932. }
  2933. DEBUG_INFO("Sending DHCP request...\n");
  2934. cnt_getIP_Fail++;
  2935. sleep(5);
  2936. }
  2937. pclose(fp);
  2938. }else {
  2939. strcpy(cmd, "ifconfig ");
  2940. strcat(cmd, Wifi.currentInterface);
  2941. strcat(cmd, " ");
  2942. strcat(cmd, Wifi.staticIPAddr);
  2943. fp = popen(cmd, "r");
  2944. DEBUG_INFO("Setting static IP to %s ...\n", Wifi.staticIPAddr);
  2945. pclose(fp);
  2946. isGetIP = PASS;
  2947. }
  2948. cnt_getIP_Fail = 0;
  2949. return isGetIP;
  2950. }
  2951. int restartWPA(void)
  2952. {
  2953. int result = FAIL;
  2954. FILE *fp;
  2955. char buf[512];
  2956. char cmd[50];
  2957. // Get IP address & net mask
  2958. strcpy(cmd, "wpa_cli -i ");
  2959. strcat(cmd, Wifi.currentInterface);
  2960. strcat(cmd, " reconfigure");
  2961. fp = popen(cmd, "r");
  2962. DEBUG_INFO("WPA reconfigure %s ...\n", Wifi.currentInterface);
  2963. if(fp == NULL)
  2964. result = FAIL;
  2965. else
  2966. {
  2967. while(fgets(buf, sizeof(buf), fp) != NULL)
  2968. {
  2969. if(strstr(buf, "OK") > 0)
  2970. {
  2971. DEBUG_INFO("reconfigure OK.\n");
  2972. result = PASS;
  2973. }else {
  2974. DEBUG_INFO("reconfigure failed.\n");
  2975. result = FAIL;
  2976. }
  2977. }
  2978. }
  2979. pclose(fp);
  2980. if(result == PASS) {
  2981. strcpy(cmd, "wpa_cli -i ");
  2982. strcat(cmd, Wifi.currentInterface);
  2983. strcat(cmd, " reconfigure");
  2984. fp = popen(cmd, "r");
  2985. DEBUG_INFO("WPA reconnect %s ...\n", Wifi.currentInterface);
  2986. if(fp == NULL)
  2987. result = FAIL;
  2988. else{
  2989. while(fgets(buf, sizeof(buf), fp) != NULL)
  2990. {
  2991. if(strstr(buf, "OK") > 0){
  2992. DEBUG_INFO("reconnect OK.\n");
  2993. result = PASS;
  2994. }else {
  2995. DEBUG_INFO("reconnect failed.\n");
  2996. result = FAIL;
  2997. }
  2998. }
  2999. }
  3000. pclose(fp);
  3001. }
  3002. return result;
  3003. }
  3004. void proc_sta()
  3005. {
  3006. switch(Wifi_module_sts)
  3007. {
  3008. case STA_STATE_INIT:
  3009. dispReq.isShowed_IF_info = false;
  3010. // get info from shared memory
  3011. getParameters();
  3012. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3013. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  3014. isSetWPA_OK = false;
  3015. // check interface
  3016. if((strlen((const char*)Wifi_A.ssid)>0) && (isFindInterface() == PASS))
  3017. {
  3018. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  3019. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  3020. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
  3021. DEBUG_INFO("=================[State 1]===================\n");
  3022. }
  3023. else
  3024. {
  3025. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
  3026. DEBUG_ERROR("Wifi support interface valid result: Fail\n");
  3027. sleep(30);
  3028. }
  3029. break;
  3030. case STA_STATE_DEVICE_DETECT:
  3031. dispReq.isShowed_IP_info = false;
  3032. if(!isSetWPA_OK && (setWPAconf() == PASS))
  3033. {
  3034. isSetWPA_OK = true;
  3035. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
  3036. sleep(5);
  3037. }
  3038. else if((isSetWPA_OK == true) && (isConnectAP() == PASS))
  3039. {
  3040. Wifi_module_sts = STA_STATE_AP_CONNECTED;
  3041. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  3042. DEBUG_INFO("=================[State 2]===================\n");
  3043. }
  3044. else
  3045. {
  3046. cnt_getAP_Fail++;
  3047. DEBUG_INFO("Fail to connect to the AP %d times...\n",cnt_getAP_Fail);
  3048. if(cnt_getAP_Fail>=3)
  3049. {
  3050. Wifi_module_sts = STA_STATE_INIT;
  3051. cnt_getAP_Fail = 0;
  3052. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
  3053. DEBUG_INFO("=================[State 0]===================\n");
  3054. }
  3055. else
  3056. {
  3057. sleep(10);
  3058. }
  3059. }
  3060. break;
  3061. case STA_STATE_AP_CONNECTED:
  3062. getInterfaceInfo();
  3063. getLinkQuality();
  3064. DEBUG_INFO("Wifi quality: %d dBm\n", Wifi.rssi);
  3065. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = Wifi.rssi;
  3066. if(isReachableInternet() == PASS)
  3067. {
  3068. DEBUG_INFO("Wifi internet valid result: Pass\n");
  3069. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=0;
  3070. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=1;
  3071. cnt_pingDNS_Fail = 0;
  3072. sleep(30);
  3073. }
  3074. else
  3075. {
  3076. cnt_pingDNS_Fail++;
  3077. DEBUG_INFO("Wifi internet valid result: Fail %d time\n", cnt_pingDNS_Fail);
  3078. sleep(5);
  3079. }
  3080. if(cnt_pingDNS_Fail >= 3)
  3081. {
  3082. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3083. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3084. cnt_pingDNS_Fail = 0;
  3085. DEBUG_INFO("Ping DNS failed...");
  3086. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  3087. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  3088. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
  3089. if(isFindInterface() == PASS)
  3090. {
  3091. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  3092. getParameters();
  3093. isSetWPA_OK = false;
  3094. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  3095. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  3096. DEBUG_INFO("=================[State 1]===================\n");
  3097. }
  3098. else
  3099. {
  3100. DEBUG_INFO("Wifi support interface valid result: Fail\n");
  3101. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  3102. Wifi_module_sts = STA_STATE_INIT;
  3103. DEBUG_INFO("=================[State 0]===================\n");
  3104. }
  3105. }
  3106. break;
  3107. default:
  3108. Wifi_module_sts = STA_STATE_INIT;
  3109. break;
  3110. }
  3111. }
  3112. void proc_ap()
  3113. {
  3114. switch(Wifi_module_sts)
  3115. {
  3116. case AP_STATE_INIT:
  3117. // get info from shared memory
  3118. getParameters();
  3119. isSetWPA_OK = false;
  3120. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3121. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  3122. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3123. // check interface
  3124. if(isFindInterface() == PASS)
  3125. {
  3126. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  3127. Wifi_module_sts = AP_STATE_DEVICE_DETECT;
  3128. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
  3129. DEBUG_INFO("=================[State 1]===================\n");
  3130. }
  3131. else
  3132. {
  3133. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=1;
  3134. DEBUG_ERROR("Wifi support interface valid result: Fail\n");
  3135. sleep(30);
  3136. }
  3137. break;
  3138. case AP_STATE_DEVICE_DETECT:
  3139. dispReq.isShowed_IP_info = false;
  3140. if(!isSetWPA_OK && (setWPAconf() == PASS))
  3141. {
  3142. isSetWPA_OK = true;
  3143. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
  3144. sleep(5);
  3145. }
  3146. else if(isSetWPA_OK && (isStartUpAP() == PASS))
  3147. {
  3148. Wifi_module_sts = AP_STATE_AP_START;
  3149. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3150. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  3151. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3152. DEBUG_INFO("=================[State 2]===================\n");
  3153. }
  3154. else
  3155. {
  3156. cnt_getAP_Fail++;
  3157. DEBUG_INFO("Fail to start up AP %d times...\n",cnt_getAP_Fail);
  3158. if(cnt_getAP_Fail>=3)
  3159. {
  3160. Wifi_module_sts = AP_STATE_INIT;
  3161. cnt_getAP_Fail = 0;
  3162. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=1;
  3163. DEBUG_INFO("=================[State 0]===================\n");
  3164. }
  3165. else
  3166. {
  3167. sleep(10);
  3168. }
  3169. }
  3170. break;
  3171. case AP_STATE_AP_START:
  3172. getInterfaceInfo();
  3173. if(isStartUpAP() == PASS)
  3174. {
  3175. DEBUG_INFO("Wifi AP start up valid result: Pass\n");
  3176. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3177. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=0;
  3178. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3179. cnt_pingDNS_Fail = 0;
  3180. sleep(30);
  3181. }
  3182. else
  3183. {
  3184. cnt_pingDNS_Fail++;
  3185. DEBUG_INFO("Wifi AP start up valid result: Fail %d time\n", cnt_pingDNS_Fail);
  3186. sleep(5);
  3187. }
  3188. if(cnt_pingDNS_Fail >= 3)
  3189. {
  3190. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3191. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  3192. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3193. cnt_pingDNS_Fail = 0;
  3194. DEBUG_INFO("Wifi AP start up status failed...");
  3195. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  3196. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  3197. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMacAddress);
  3198. if(isFindInterface() == PASS)
  3199. {
  3200. DEBUG_INFO("Wifi interface: %s\n", Wifi.currentInterface);
  3201. isSetWPA_OK = false;
  3202. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  3203. Wifi_module_sts = STA_STATE_DEVICE_DETECT;
  3204. DEBUG_INFO("=================[State 1]===================\n");
  3205. }
  3206. else
  3207. {
  3208. DEBUG_INFO("Wifi support interface valid result: Fail\n");
  3209. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiRssi = 0;
  3210. Wifi_module_sts = STA_STATE_INIT;
  3211. DEBUG_INFO("=================[State 0]===================\n");
  3212. }
  3213. }
  3214. break;
  3215. default:
  3216. Wifi_module_sts = AP_STATE_INIT;
  3217. break;
  3218. }
  3219. }
  3220. void proc_killConnection()
  3221. {
  3222. char cmdBuf[512];
  3223. // Initialization flags when network is disable mode
  3224. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi=1;
  3225. ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi=1;
  3226. ShmSysConfigAndInfo->SysConfig.AthInterface.WifiNetworkConn=0;
  3227. ShmStatusCodeData->FaultCode.FaultEvents.bits.WiFiModuleBroken=0;
  3228. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.WiFiModuleCommFail=0;
  3229. // Stop dhcp client or server
  3230. sprintf(cmdBuf, "pgrep -f \"udhcpc -i %s\" | xargs kill", Wifi.currentInterface);
  3231. system(cmdBuf);
  3232. sprintf(cmdBuf, "pgrep -f \"udhcpd\" | xargs kill");
  3233. system(cmdBuf);
  3234. // Restart wpa_supplicant
  3235. sprintf(cmdBuf, "pgrep -f \"wpa_supplicant\" | xargs kill");
  3236. system(cmdBuf);
  3237. // Stop wifi AP
  3238. sprintf(cmdBuf, "pgrep -f \"hostapd\" | xargs kill");
  3239. system(cmdBuf);
  3240. // Clean share memory when network is disable mode
  3241. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiIpAddress);
  3242. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSubmaskAddress);
  3243. memset(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress, 0, sizeof ShmSysConfigAndInfo->SysConfig.AthInterface.WifiGatewayAddress);
  3244. }
  3245. //==========================================
  3246. // Main loop
  3247. //==========================================
  3248. int main(void)
  3249. {
  3250. pid_t pid;
  3251. if(InitShareMemory() == FAIL)
  3252. {
  3253. DEBUG_ERROR("InitShareMemory NG\n");
  3254. if(ShmStatusCodeData!=NULL)
  3255. {
  3256. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=ON;
  3257. }
  3258. sleep(5);
  3259. return 0;
  3260. }
  3261. // Local database initial
  3262. if(DB_Open(localDb) != PASS)
  3263. {
  3264. DEBUG_ERROR("Puk pin list local db initial fail.\n");
  3265. }
  3266. // Wifi login info initial & PUK generate
  3267. wifi_login_info.loginRole = ROLE_UNKNOWN;
  3268. memset(&wifi_login_info.loginId[0], 0x00, ARRAY_SIZE(wifi_login_info.loginId));
  3269. memset(&wifi_login_info.puk[0], 0x00, ARRAY_SIZE(wifi_login_info.puk));
  3270. sprintf((char*)wifi_login_info.puk, "%08X", ~stm32crc((uint32_t*)&ShmSysConfigAndInfo->SysConfig.SystemId[0], (ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)>>2)));
  3271. DEBUG_INFO("System ID: %s\r\n", ShmSysConfigAndInfo->SysConfig.SystemId);
  3272. DEBUG_INFO("EVSE PUK: %s\r\n", wifi_login_info.puk);
  3273. // UDP socket server start
  3274. pid = fork();
  3275. if(pid == 0)
  3276. {
  3277. if(udpSocketServerStart() == FAIL)
  3278. {
  3279. DEBUG_ERROR("UDP socket server down.\r\n");
  3280. return 0;
  3281. }
  3282. }
  3283. // TCP socket server start
  3284. pid = fork();
  3285. if(pid == 0)
  3286. {
  3287. if(tcpSocketServerStart() == FAIL)
  3288. {
  3289. DEBUG_ERROR("TCP socket server down.\r\n");
  3290. return 0;
  3291. }
  3292. }
  3293. //=============================================
  3294. // Install WIFI module driver
  3295. //=============================================
  3296. #ifdef UBLOX
  3297. system("insmod /lib/modules/mlan.ko");
  3298. system("insmod /lib/modules/usb8801.ko");
  3299. sleep(5);
  3300. system("ifconfig mlan0 up");
  3301. system("ifconfig uap0 up");
  3302. #endif
  3303. #ifdef MT7601U
  3304. system("insmod /lib/modules/mt7601u.ko");
  3305. sleep(5);
  3306. system("ifconfig wlan0 up");
  3307. #endif
  3308. DEBUG_INFO("=================[State 0]===================\n");
  3309. for(;;)
  3310. {
  3311. switch(ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode)
  3312. {
  3313. case WIFI_MODE_STA:
  3314. proc_sta();
  3315. break;
  3316. case WIFI_MODE_AP:
  3317. proc_ap();
  3318. break;
  3319. case WIFI_MODE_ADHOC:
  3320. break;
  3321. case WIFI_MODE_DISABLE:
  3322. default:
  3323. proc_killConnection();
  3324. sleep(30);
  3325. break;
  3326. }
  3327. }
  3328. return 0;
  3329. }