Module_Wifi.c 123 KB

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