Module_Wifi.c 115 KB

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