main.c 153 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906
  1. #include "define.h"
  2. #include "main.h"
  3. //==========================
  4. // System basic sample constant
  5. //==========================
  6. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  7. #define PASS 1
  8. #define FAIL -1
  9. #define YES 1
  10. #define NO 0
  11. #define ON 1
  12. #define OFF 0
  13. #define BUFFER_SIZE 128
  14. //==========================
  15. // Timer interval
  16. //==========================
  17. #define TMR_IDX_HANDSHAKING 0
  18. #define TMR_IDX_AUTH 1
  19. #define TMR_IDX_LOGPPRINTOUT 2
  20. #define TMR_IDX_PROFILE_PREPARE 3
  21. #define TMR_IDX_BS_HLC_HANDSHAKE 4
  22. #define TMR_IDX_CCS_HEARTBEAT_COUNT_RESET 5
  23. #define TMR_IDX_6 6
  24. #define TMR_IDX_7 7
  25. #define TMR_IDX_8 8
  26. #define TMR_IDX_9 9
  27. #define TIMEOUT_SPEC_HANDSHAKING 180000
  28. #define TIMEOUT_SPEC_AUTH 15000
  29. #define TIMEOUT_SPEC_HANDSHAKING_LED 185000
  30. #define TIMEOUT_SPEC_LOGPPRINTOUT 30000
  31. #define TIMEOUT_SPEC_PROFILE_PREPARE 5000
  32. #define TIMEOUT_SPEC_BS_HLC_HANDSHAKE 2000
  33. #define TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET 10000
  34. #define TIMEOUT_SPEC_CCS_HANDSHAKE 120000
  35. #define MtdBlockSize 0x600000
  36. #define DB_FILE "/Storage/ChargeLog/localCgargingRecord.db"
  37. #define AC_INPUT_PHASE 3
  38. //==========================
  39. // Declare method
  40. //==========================
  41. void trim(char *s);
  42. int mystrcmp(char *p1,char *p2);
  43. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  44. void split(char **arr, char *str, const char *del);
  45. int isReachableInternet();
  46. int InitRfidPort(void);
  47. int GetCardSerialNumber();
  48. void setLedMotion(unsigned char gun_index,unsigned char led_mode);
  49. void setRequest(unsigned char gun_index,unsigned char isOn);
  50. void setSpeaker(unsigned char isOn, unsigned char speaker_mode);
  51. //==========================
  52. // Declare RFID module type
  53. //==========================
  54. #define MODULE_EWT 0
  55. int wtdFd = -1;
  56. int rfidFd = -1;
  57. char* rfidPortName = "/dev/ttyS2";
  58. RFID rfid;
  59. char *valid_Internet[2] = {"8.8.8.8", "180.76.76.76"};
  60. //==========================
  61. // Declare share memory
  62. //==========================
  63. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  64. struct StatusCodeData *ShmStatusCodeData;
  65. struct PsuData *ShmPsuData;
  66. struct CHAdeMOData *ShmCHAdeMOData;
  67. struct CcsData *ShmCcsData;
  68. struct PrimaryMcuData *ShmPrimaryMcuData;
  69. struct FanModuleData *ShmFanModuleData;
  70. struct RelayModuleData *ShmRelayModuleData;
  71. struct OCPP16Data *ShmOCPP16Data;
  72. struct Charger *ShmCharger;
  73. struct timeb startTime[AC_QUANTITY][10];
  74. struct timeb startChargingTime[AC_QUANTITY];
  75. struct timeb endChargingTime[AC_QUANTITY];
  76. sqlite3 *localDb;
  77. struct SysConfigData SysConfigOrg;
  78. //=================================
  79. // Common routine
  80. //=================================
  81. void trim(char *s)
  82. {
  83. int i=0, j, k, l=0;
  84. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  85. i++;
  86. j = strlen(s)-1;
  87. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  88. j--;
  89. if(i==0 && j==strlen(s)-1) { }
  90. else if(i==0) s[j+1] = '\0';
  91. else {
  92. for(k=i; k<=j; k++) s[l++] = s[k];
  93. s[l] = '\0';
  94. }
  95. }
  96. int mystrcmp(char *p1,char *p2)
  97. {
  98. while(*p1==*p2)
  99. {
  100. if(*p1=='\0' || *p2=='\0')
  101. break;
  102. p1++;
  103. p2++;
  104. }
  105. if(*p1=='\0' && *p2=='\0')
  106. return(PASS);
  107. else
  108. return(FAIL);
  109. }
  110. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  111. {
  112. strncpy(dest, src + start, cnt);
  113. dest[cnt] = 0;
  114. }
  115. void split(char **arr, char *str, const char *del)
  116. {
  117. char *s = strtok(str, del);
  118. while(s != NULL)
  119. {
  120. *arr++ = s;
  121. s = strtok(NULL, del);
  122. }
  123. }
  124. int StoreLogMsg(const char *fmt, ...)
  125. {
  126. char Buf[4096+256];
  127. char buffer[4096];
  128. time_t CurrentTime;
  129. struct tm *tm;
  130. va_list args;
  131. va_start(args, fmt);
  132. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  133. va_end(args);
  134. memset(Buf,0,sizeof(Buf));
  135. CurrentTime = time(NULL);
  136. tm=localtime(&CurrentTime);
  137. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  138. {
  139. sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]%s_%s_SystemLog",
  140. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  141. buffer,
  142. tm->tm_year+1900,tm->tm_mon+1,
  143. ShmSysConfigAndInfo->SysConfig.ModelName,
  144. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  145. }
  146. else
  147. {
  148. sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  149. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  150. buffer,
  151. tm->tm_year+1900,tm->tm_mon+1);
  152. }
  153. #ifdef SystemLogMessage
  154. system(Buf);
  155. #endif
  156. #ifdef ConsloePrintLog
  157. 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);
  158. #endif
  159. return rc;
  160. }
  161. long long DiffTimebWithNow(struct timeb ST)
  162. {
  163. //return milli-second
  164. struct timeb ET;
  165. long long StartTime,StopTime;
  166. ftime(&ET);
  167. StartTime=(long long)ST.time;
  168. StopTime=(long long)ET.time;
  169. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  170. }
  171. long long DiffTimeb(struct timeb ST, struct timeb ET)
  172. {
  173. //return milli-second
  174. long long StartTime,StopTime;
  175. StartTime=(long long)ST.time;
  176. StopTime=(long long)ET.time;
  177. return ((StopTime-StartTime)*1000) + (ET.millitm-ST.millitm);
  178. }
  179. void getDateTimeString(char* result)
  180. {
  181. time_t CurrentTime;
  182. struct tm *tm;
  183. CurrentTime = time(NULL);
  184. tm=localtime(&CurrentTime);
  185. sprintf(result, "%04d.%02d.%02d %02d:%02d:%02d", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec);
  186. }
  187. //======================================================
  188. // Check interface status
  189. //======================================================
  190. int isInterfaceUp(const char *interface)
  191. {
  192. int result = FAIL;
  193. FILE *fp;
  194. char cmd[256];
  195. char buf[512];
  196. strcpy(cmd, "ifconfig");;
  197. fp = popen(cmd, "r");
  198. if(fp != NULL)
  199. {
  200. while(fgets(buf, sizeof(buf), fp) != NULL)
  201. {
  202. if(strstr(buf, interface) > 0)
  203. {
  204. result = PASS;
  205. }
  206. }
  207. }
  208. pclose(fp);
  209. return result;
  210. }
  211. //======================================================
  212. // Create all share memory
  213. //======================================================
  214. int CreatShareMemory()
  215. {
  216. int result = PASS;
  217. int MeterSMId;
  218. //creat ShmSysConfigAndInfo
  219. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  220. {
  221. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\r\n");
  222. result = FAIL;
  223. }
  224. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  225. {
  226. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\r\n");
  227. result = FAIL;
  228. }
  229. memset(ShmSysConfigAndInfo,0,sizeof(struct SysConfigAndInfo));
  230. //creat ShmStatusCodeData
  231. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  232. {
  233. DEBUG_ERROR("shmget ShmStatusCodeData NG\r\n");
  234. result = FAIL;
  235. }
  236. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  237. {
  238. DEBUG_ERROR("shmat ShmStatusCodeData NG\r\n");
  239. result = FAIL;
  240. }
  241. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  242. //creat ShmPsuData
  243. if ((MeterSMId = shmget(ShmPsuKey, sizeof(struct PsuData), IPC_CREAT | 0777)) < 0)
  244. {
  245. DEBUG_ERROR("shmget ShmPsuData NG\r\n");
  246. result = FAIL;
  247. }
  248. else if ((ShmPsuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  249. {
  250. DEBUG_ERROR("shmat ShmPsuData NG\r\n");
  251. result = FAIL;
  252. }
  253. memset(ShmPsuData,0,sizeof(struct PsuData));
  254. //creat ShmCHAdeMOData
  255. if ((MeterSMId = shmget(ShmCHAdeMOCommKey, sizeof(struct CHAdeMOData), IPC_CREAT | 0777)) < 0)
  256. {
  257. DEBUG_ERROR("shmget ShmCHAdeMOData NG1\r\n");
  258. result = FAIL;
  259. }
  260. else if ((ShmCHAdeMOData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  261. {
  262. DEBUG_ERROR("shmat ShmCHAdeMOData NG2\r\n");
  263. result = FAIL;
  264. }
  265. memset(ShmCHAdeMOData,0,sizeof(struct CHAdeMOData));
  266. //creat ShmCcsData
  267. if ((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  268. {
  269. DEBUG_ERROR("shmget ShmCcsData NG\r\n");
  270. result = FAIL;
  271. }
  272. else if ((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  273. {
  274. DEBUG_ERROR("shmat ShmCcsData NG\r\n");
  275. result = FAIL;
  276. }
  277. memset(ShmCcsData,0,sizeof(struct CcsData));
  278. //creat ShmPrimaryMcuData
  279. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), IPC_CREAT | 0777)) < 0)
  280. {
  281. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\r\n");
  282. result = FAIL;
  283. }
  284. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  285. {
  286. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\r\n");
  287. result = FAIL;
  288. }
  289. memset(ShmPrimaryMcuData,0,sizeof(struct PrimaryMcuData));
  290. /*
  291. //creat ShmFanModuleData
  292. if ((MeterSMId = shmget(ShmFanBdKey, sizeof(struct FanModuleData), IPC_CREAT | 0777)) < 0)
  293. {
  294. DEBUG_ERROR("shmget ShmFanModuleData NG\r\n");
  295. result = FAIL;
  296. }
  297. else if ((ShmFanModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  298. {
  299. DEBUG_ERROR("shmat ShmFanModuleData NG\r\n");
  300. result = FAIL;
  301. }
  302. memset(ShmFanModuleData,0,sizeof(struct FanModuleData));
  303. //creat ShmRelayModuleData
  304. if ((MeterSMId = shmget(ShmRelayBdKey, sizeof(struct RelayModuleData), IPC_CREAT | 0777)) < 0)
  305. {
  306. DEBUG_ERROR("shmget ShmRelayModuleData NG\r\n");
  307. result = FAIL;
  308. }
  309. else if ((ShmRelayModuleData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  310. {
  311. DEBUG_ERROR("shmat ShmRelayModuleData NG\r\n");
  312. result = FAIL;
  313. }
  314. memset(ShmRelayModuleData,0,sizeof(struct RelayModuleData));*/
  315. //creat ShmOCPP16Data
  316. if ((MeterSMId = shmget(ShmOcppModuleKey, sizeof(struct OCPP16Data), IPC_CREAT | 0777)) < 0)
  317. {
  318. DEBUG_ERROR("shmget ShmOCPP16Data NG\r\n");
  319. result = FAIL;
  320. }
  321. else if ((ShmOCPP16Data = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  322. {
  323. DEBUG_ERROR("shmat ShmOCPP16Data NG\r\n");
  324. result = FAIL;
  325. }
  326. memset(ShmOCPP16Data,0,sizeof(struct OCPP16Data));
  327. //creat ShmCharger
  328. if ((MeterSMId = shmget(ShmChargerKey, sizeof(struct Charger), IPC_CREAT | 0777)) < 0)
  329. {
  330. DEBUG_ERROR("shmget ShmCharger NG\r\n");
  331. result = FAIL;
  332. }
  333. else if ((ShmCharger = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  334. {
  335. DEBUG_ERROR("shmat ShmCharger NG\r\n");
  336. result = FAIL;
  337. }
  338. memset(ShmCharger,0,sizeof(struct Charger));
  339. return result;
  340. }
  341. //===============================================
  342. // SQLite3 related routine
  343. //===============================================
  344. int DB_Open(sqlite3 *db)
  345. {
  346. int result = PASS;
  347. char* errMsg = NULL;
  348. char* createRecordSql="CREATE TABLE IF NOT EXISTS charging_record("
  349. "idx integer primary key AUTOINCREMENT, "
  350. "reservationId text, "
  351. "transactionId text, "
  352. "startMethod text, "
  353. "userId text, "
  354. "dateTimeStart text, "
  355. "dateTimeStop text,"
  356. "socStart text, "
  357. "socStop text, "
  358. "chargeEnergy text, "
  359. "stopReason text"
  360. ");";
  361. char* createCfgSql="CREATE TABLE IF NOT EXISTS `config` ( "
  362. "`idx` INTEGER PRIMARY KEY AUTOINCREMENT, "
  363. "`item` TEXT NOT NULL, "
  364. "`connector` INTEGER NOT NULL, "
  365. "`val` TEXT NOT NULL, unique(item,connector) on conflict replace);";
  366. //sqlite3_config(SQLITE_CONFIG_URI, 1);
  367. if(sqlite3_open(DB_FILE, &db))
  368. {
  369. result = FAIL;
  370. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  371. sqlite3_close(db);
  372. }
  373. else
  374. {
  375. DEBUG_INFO( "Local charging record database open successfully.\r\n");
  376. if (sqlite3_exec(db, createRecordSql, 0, 0, &errMsg) != SQLITE_OK)
  377. {
  378. result = FAIL;
  379. DEBUG_INFO( "Create local charging record table error message: %s\n", errMsg);
  380. }
  381. else
  382. {
  383. DEBUG_INFO( "Opened local charging record table successfully\n");
  384. }
  385. if (sqlite3_exec(db, createCfgSql, 0, 0, &errMsg) != SQLITE_OK)
  386. {
  387. result = FAIL;
  388. DEBUG_INFO( "Create local config table error message: %s\n", errMsg);
  389. }
  390. else
  391. {
  392. DEBUG_INFO( "Opened local config table successfully\n");
  393. }
  394. sqlite3_close(db);
  395. }
  396. return result;
  397. }
  398. int DB_Insert_Record(sqlite3 *db, int gun_index)
  399. {
  400. int result = PASS;
  401. char* errMsg = NULL;
  402. char insertSql[1024];
  403. sprintf(insertSql, "insert into charging_record(reservationId, transactionId, startMethod, userId, dateTimeStart, dateTimeStop, socStart, socStop, chargeEnergy, stopReason) "
  404. "values('%d', '%d', '%d', '%s', '%s', '%s', '%d', '%d', '%f', '%s');",
  405. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId,
  406. ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId,
  407. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod,
  408. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId,
  409. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime,
  410. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime,
  411. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  412. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].EvBatterySoc,
  413. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy,
  414. ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  415. if(sqlite3_open("/Storage/ChargeLog/localCgargingRecord.db", &db))
  416. {
  417. result = FAIL;
  418. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  419. sqlite3_close(db);
  420. }
  421. else
  422. {
  423. DEBUG_INFO( "Local charging record database open successfully.\r\n");
  424. if (sqlite3_exec(db, insertSql, 0, 0, &errMsg) != SQLITE_OK)
  425. {
  426. result = FAIL;
  427. DEBUG_INFO( "Insert local charging record error message: %s\n", errMsg);
  428. }
  429. else
  430. {
  431. DEBUG_INFO( "Insert local charging record successfully\n");
  432. }
  433. sqlite3_close(db);
  434. }
  435. return result;
  436. }
  437. int DB_Update_Operactive(sqlite3 *db, uint8_t gun_index, uint8_t isOperactive)
  438. {
  439. uint8_t result = false;
  440. char* errMsg = NULL;
  441. char sqlStr[1024];
  442. srand(time(NULL));
  443. if(sqlite3_open(DB_FILE, &db))
  444. {
  445. result = FAIL;
  446. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  447. sqlite3_close(db);
  448. }
  449. else
  450. {
  451. DEBUG_INFO( "Local charging record database open successfully.\r\n");
  452. sprintf(sqlStr, "insert or replace into config (item, connector, val) values('isOperactive', %d, %d);", gun_index, isOperactive);
  453. DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  454. if (sqlite3_exec(db, sqlStr, 0, 0, &errMsg) != SQLITE_OK)
  455. {
  456. result = FAIL;
  457. DEBUG_INFO( "update config error message: %s\n", errMsg);
  458. }
  459. else
  460. {
  461. DEBUG_INFO("update connector-%d config item isOperactive to %d\r\n", gun_index, isOperactive);
  462. }
  463. sqlite3_close(db);
  464. }
  465. return result;
  466. }
  467. int DB_Get_Operactive(sqlite3 *db, uint8_t gun_index)
  468. {
  469. uint8_t result = true;
  470. char* errMsg = NULL;
  471. char sqlStr[1024];
  472. char **rs;
  473. int rows, cols;
  474. sprintf(sqlStr, "select * from config where item='isOperactive' and connector=%d;", gun_index);
  475. //DEBUG_INFO("sqlStr= %s\r\n", sqlStr);
  476. if(sqlite3_open(DB_FILE, &db))
  477. {
  478. result = FAIL;
  479. DEBUG_INFO( "Can't open database: %s\r\n", sqlite3_errmsg(db));
  480. sqlite3_close(db);
  481. }
  482. else
  483. {
  484. DEBUG_INFO( "Local config query database open successfully.\r\n");
  485. sqlite3_get_table(db, sqlStr, &rs, &rows, &cols, &errMsg);
  486. if(rows>0)
  487. {
  488. for(int idxRow=1;idxRow<=rows;idxRow++)
  489. {
  490. if(strcmp(rs[(idxRow*cols)+3], "0") == 0)
  491. {
  492. result = false;
  493. }
  494. DEBUG_INFO("Query connector-%d isOperactive: %s\r\n", gun_index, rs[(idxRow*cols)+3]);
  495. }
  496. }
  497. else
  498. {
  499. DEBUG_INFO("Query connector-%d fail, set default value to operactive.\r\n", gun_index);
  500. }
  501. sqlite3_free_table(rs);
  502. sqlite3_close(db);
  503. }
  504. return result;
  505. }
  506. //======================================================
  507. // Peripheral initial
  508. //======================================================
  509. int InitWatchDog()
  510. {
  511. int fd;
  512. int timeout = 180;
  513. system("/usr/bin/fuser -k /dev/watchdog");
  514. sleep(1);
  515. system("echo V > /dev/watchdog");
  516. sleep(1);
  517. fd=open("/dev/watchdog", O_RDWR);
  518. if(fd<=0)
  519. {
  520. DEBUG_ERROR("System watch dog initial fail.\r\n");
  521. }
  522. ioctl(fd, _IOWR('W', 6, int), &timeout);
  523. return fd;
  524. }
  525. void InitGPIO()
  526. {
  527. /*****************0~3, 4 bank, bank x 32+ num*********************/
  528. /***************************************************************/
  529. /*************** INPUT PIN ***************************************/
  530. /***************************************************************/
  531. /***************************************************************/
  532. /*************** OUTPUT PIN ************************************/
  533. /***************************************************************/
  534. /*MCU request:GPIO3_20 => H:ON; L:OFF*/
  535. system("echo 116 > /sys/class/gpio/export");
  536. system("echo \"out\" > /sys/class/gpio/gpio116/direction");
  537. system("echo 0 > /sys/class/gpio/gpio116/value");
  538. /*Rfid:GPIO0_19 => Reset_PING H:ON; L:OFF*/
  539. system("echo 19 > /sys/class/gpio/export");
  540. system("echo \"out\" > /sys/class/gpio/gpio19/direction");
  541. system("echo 1 > /sys/class/gpio/gpio19/value");
  542. /*Speaker:GPIO2_1 => H:ON; L:OFF*/
  543. system("echo 65 > /sys/class/gpio/export");
  544. system("echo \"out\" > /sys/class/gpio/gpio65/direction");
  545. system("echo 0 > /sys/class/gpio/gpio65/value");
  546. /*4G/Wifi RST:GPIO3_18 => H:ON; L:OFF*/
  547. system("echo 114 > /sys/class/gpio/export");
  548. system("echo \"out\" > /sys/class/gpio/gpio114/direction");
  549. system("echo 1 > /sys/class/gpio/gpio114/value");
  550. sleep(3);
  551. system("echo 0 > /sys/class/gpio/gpio114/value");
  552. DEBUG_INFO("Initial GPIO OK\r\n");
  553. }
  554. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  555. {
  556. int fd,wrd;
  557. unsigned char *buf;
  558. unsigned int ChkSum,ChkSumOrg;
  559. if((buf=malloc(MtdBlockSize))==NULL)
  560. {
  561. DEBUG_ERROR("malloc buffer NG,rebooting..\r\n");
  562. if(ShmStatusCodeData!=NULL)
  563. {
  564. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  565. }
  566. sleep(5);
  567. system("reboot -f");
  568. sleep(5);
  569. system("reboot -f");
  570. }
  571. memset(buf, 0, MtdBlockSize);
  572. //================================================
  573. // Load configuration from mtdblock10
  574. //================================================
  575. fd = open("/dev/mtdblock10", O_RDWR);
  576. if (fd < 0)
  577. {
  578. free(buf);
  579. DEBUG_ERROR("open mtdblock10 NG,rebooting..\r\n");
  580. if(ShmStatusCodeData!=NULL)
  581. {
  582. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  583. }
  584. sleep(5);
  585. system("reboot -f");
  586. sleep(5);
  587. system("reboot -f");
  588. }
  589. wrd=read(fd, buf, MtdBlockSize);
  590. close(fd);
  591. if(wrd<MtdBlockSize)
  592. {
  593. free(buf);
  594. DEBUG_ERROR("read SysConfigData data NG,rebooting..\r\n");
  595. if(ShmStatusCodeData!=NULL)
  596. {
  597. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  598. }
  599. sleep(5);
  600. system("reboot -f");
  601. sleep(5);
  602. system("reboot -f");
  603. }
  604. ChkSum=0;
  605. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  606. {
  607. ChkSum+=buf[wrd];
  608. }
  609. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  610. //================================================
  611. // Load configuration from mtdblock11
  612. //================================================
  613. if(ChkSum!=ChkSumOrg)
  614. {
  615. DEBUG_ERROR("Primary SysConfigData checksum NG, read backup\r\n");
  616. fd = open("/dev/mtdblock11", O_RDWR);
  617. if (fd < 0)
  618. {
  619. free(buf);
  620. DEBUG_ERROR("open mtdblock11 (backup) NG,rebooting..\r\n");
  621. if(ShmStatusCodeData!=NULL)
  622. {
  623. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  624. }
  625. sleep(5);
  626. system("reboot -f");
  627. sleep(5);
  628. system("reboot -f");
  629. }
  630. memset(buf, 0, MtdBlockSize);
  631. wrd=read(fd, buf,MtdBlockSize);
  632. close(fd);
  633. if(wrd<MtdBlockSize)
  634. {
  635. free(buf);
  636. DEBUG_ERROR("read backup SysConfigData data NG,rebooting..\r\n");
  637. if(ShmStatusCodeData!=NULL)
  638. {
  639. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  640. }
  641. sleep(5);
  642. system("reboot -f");
  643. sleep(5);
  644. system("reboot -f");
  645. }
  646. ChkSum=0;
  647. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  648. {
  649. ChkSum+=buf[wrd];
  650. }
  651. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  652. //================================================
  653. // Load configuration from mtdblock12 (Factory default)
  654. //================================================
  655. if(ChkSum!=ChkSumOrg)
  656. {
  657. DEBUG_WARN("backup SysConfigData checksum NG, read Factory default\r\n");
  658. fd = open("/dev/mtdblock12", O_RDWR);
  659. if (fd < 0)
  660. {
  661. free(buf);
  662. DEBUG_ERROR("open mtdblock12 (Factory default) NG,rebooting..\r\n");
  663. if(ShmStatusCodeData!=NULL)
  664. {
  665. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  666. }
  667. sleep(5);
  668. system("reboot -f");
  669. sleep(5);
  670. system("reboot -f");
  671. }
  672. memset(buf, 0, MtdBlockSize);
  673. wrd=read(fd, buf,MtdBlockSize);
  674. close(fd);
  675. if(wrd<MtdBlockSize)
  676. {
  677. free(buf);
  678. DEBUG_ERROR("read factory default SysConfigData data NG,rebooting..\r\n");
  679. if(ShmStatusCodeData!=NULL)
  680. {
  681. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed=1;
  682. }
  683. sleep(5);
  684. system("reboot -f");
  685. sleep(5);
  686. system("reboot -f");
  687. }
  688. ChkSum=0;
  689. for(wrd=0;wrd<MtdBlockSize-4;wrd++)
  690. {
  691. ChkSum+=buf[wrd];
  692. }
  693. memcpy(&ChkSumOrg,buf+(MtdBlockSize-4),sizeof(ChkSumOrg));
  694. if(ChkSum!=ChkSumOrg)
  695. {
  696. DEBUG_WARN("factory default SysConfigData checksum NG, restore factory default\r\n");
  697. free(buf);
  698. system("cd /root;./Module_FactoryConfig -m");
  699. system("sync");
  700. sleep(5);
  701. system("reboot -f");
  702. sleep(5);
  703. system("reboot -f");
  704. return FAIL;
  705. }
  706. }
  707. }
  708. //load OK
  709. memcpy((struct SysConfigData *)ptr,buf,sizeof(struct SysConfigData));
  710. free(buf);
  711. DEBUG_INFO("Load SysConfigData OK\r\n");
  712. return PASS;
  713. }
  714. int StoreUsrConfigData(struct SysConfigData *UsrData)
  715. {
  716. int result = PASS;
  717. int fd,wrd;
  718. unsigned int i,Chk;
  719. unsigned char *ptr, *BufTmp;
  720. Chk=0;
  721. ptr=(unsigned char *)UsrData;
  722. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  723. {
  724. memset(BufTmp,0,MtdBlockSize);
  725. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  726. for(i=0;i<MtdBlockSize-4;i++)
  727. Chk+=*(BufTmp+i);
  728. memcpy( BufTmp+MtdBlockSize-4,&Chk,4);
  729. fd = open("/dev/mtdblock10", O_RDWR);
  730. if (fd>0)
  731. {
  732. wrd=write(fd, BufTmp, MtdBlockSize);
  733. close(fd);
  734. if(wrd>=MtdBlockSize)
  735. {
  736. fd = open("/dev/mtdblock11", O_RDWR);
  737. if (fd>0)
  738. {
  739. wrd=write(fd, BufTmp, MtdBlockSize);
  740. close(fd);
  741. if(wrd<MtdBlockSize)
  742. {
  743. DEBUG_ERROR("write /dev/mtdblock11(backup) NG\r\n");
  744. result = FAIL;
  745. }
  746. }
  747. else
  748. {
  749. DEBUG_ERROR("open /dev/mtdblock11(backup) NG\r\n");
  750. result = FAIL;
  751. }
  752. }
  753. else
  754. {
  755. DEBUG_ERROR("write /dev/mtdblock10 NG\r\n");
  756. result = FAIL;
  757. }
  758. }
  759. else
  760. {
  761. DEBUG_ERROR("open /dev/mtdblock10 NG\r\n");
  762. result = FAIL;
  763. }
  764. }
  765. else
  766. {
  767. DEBUG_ERROR("alloc BlockSize NG\r\n");
  768. result = FAIL;
  769. }
  770. if(BufTmp!=NULL)
  771. free(BufTmp);
  772. return result;
  773. }
  774. void InitEthernet()
  775. {
  776. pid_t pid;
  777. uint8_t cnt_pingDNS_Fail;
  778. char tmpbuf[256];
  779. //unsigned int address;
  780. //Init Eth0 for internet
  781. if(isInterfaceUp("eth0")==PASS)
  782. {
  783. memset(tmpbuf,0,256);
  784. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up",
  785. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  786. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  787. system(tmpbuf);
  788. memset(tmpbuf,0,256);
  789. sprintf(tmpbuf,"route add default gw %s eth0 ",
  790. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  791. system(tmpbuf);
  792. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up");
  793. system("ifconfig lo up");
  794. }
  795. if(isInterfaceUp("eth1")==PASS)
  796. {
  797. //Init Eth1 for administrator tool
  798. memset(tmpbuf,0,256);
  799. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up",
  800. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  801. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  802. system(tmpbuf);
  803. }
  804. //Run DHCP client if enabled
  805. system("killall udhcpc");
  806. system("rm -rf /etc/resolv.conf");
  807. system("echo nameserver 8.8.8.8 > /etc/resolv.conf"); //Google DNS server
  808. system("echo nameserver 180.76.76.76 > /etc/resolv.conf"); //Baidu DNS server
  809. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  810. {
  811. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  812. system(tmpbuf);
  813. }
  814. // Upgrade system id to /etc/hostname
  815. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  816. system(tmpbuf);
  817. //check internet status
  818. pid = fork();
  819. if(pid == 0)
  820. {
  821. for(;;)
  822. {
  823. if(isReachableInternet() == PASS)
  824. {
  825. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  826. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  827. cnt_pingDNS_Fail = 0;
  828. }
  829. else
  830. {
  831. if(++cnt_pingDNS_Fail > 3)
  832. {
  833. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  834. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  835. }
  836. }
  837. sleep(5);
  838. }
  839. }
  840. DEBUG_INFO("Initial Ethernet OK\r\n");
  841. }
  842. int SpawnTask()
  843. {
  844. system ("pkill Module_4g");
  845. system ("pkill Module_Wifi");
  846. system ("pkill Module_EventLogging");
  847. system ("pkill OcppBackend");
  848. system ("pkill Module_AlarmDetect");
  849. system ("pkill Module_InternalComm");
  850. system ("pkill Module_Speaker");
  851. system ("pkill Module_ProduceUtils");
  852. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  853. {
  854. system("/root/Module_4g &");
  855. }
  856. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  857. {
  858. system("/root/Module_Wifi &");
  859. }
  860. system("/root/Module_EventLogging &");
  861. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  862. {
  863. system("/root/OcppBackend &");
  864. }
  865. else
  866. {
  867. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\r\n");
  868. system("/root/OcppBackend &");
  869. }
  870. system ("/root/Module_AlarmDetect &");
  871. system ("/root/Module_InternalComm &");
  872. system ("/root/Module_Speaker &");
  873. system ("/root/Module_ProduceUtils &");
  874. return PASS;
  875. }
  876. int Initialization()
  877. {
  878. int result = PASS;
  879. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  880. InitGPIO();
  881. InitEthernet();
  882. if(DB_Open(localDb) != PASS)
  883. result = FAIL;
  884. for(int gun_index=0;gun_index< AC_QUANTITY;gun_index++)
  885. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  886. rfidFd = InitRfidPort();
  887. wtdFd = InitWatchDog();
  888. if((wtdFd < 0) || (rfidFd < 0))
  889. result = FAIL;
  890. if(result == PASS)
  891. DEBUG_INFO("Initialization OK.\r\n");
  892. else
  893. DEBUG_INFO("Initialization Fail.\r\n");
  894. return result;
  895. }
  896. //=====================================================
  897. // Common routine
  898. //=====================================================
  899. char* getSystemModeName(unsigned char mode)
  900. {
  901. char* result;
  902. switch(mode)
  903. {
  904. case SYS_MODE_BOOTING:
  905. result = "booting";
  906. break;
  907. case SYS_MODE_IDLE:
  908. result = "idle";
  909. break;
  910. case SYS_MODE_AUTHORIZING:
  911. result = "authorizing";
  912. break;
  913. case SYS_MODE_PREPARING:
  914. result = "preparing";
  915. break;
  916. case SYS_MODE_CHARGING:
  917. result = "charging";
  918. break;
  919. case SYS_MODE_TERMINATING:
  920. result = "terminating";
  921. break;
  922. case SYS_MODE_COMPLETE:
  923. result = "complete";
  924. break;
  925. case SYS_MODE_ALARM:
  926. result = "alarm";
  927. break;
  928. case SYS_MODE_FAULT:
  929. result = "fault";
  930. break;
  931. case SYS_MODE_MAINTAIN:
  932. result = "maintain";
  933. break;
  934. case SYS_MODE_RESERVATION:
  935. result = "reservation";
  936. break;
  937. case SYS_MODE_BOOKING:
  938. result = "booking";
  939. break;
  940. case SYS_MODE_DEBUG:
  941. result = "debug";
  942. break;
  943. case SYS_MODE_UPDATE:
  944. result = "upgrade";
  945. break;
  946. default:
  947. result = "unknown";
  948. break;
  949. }
  950. return result;
  951. }
  952. void setChargerMode(unsigned char gun_index, unsigned char mode)
  953. {
  954. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  955. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  956. DEBUG_INFO("Gun-%02d mode switch from %s to %s\r\n", gun_index, getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus), getSystemModeName(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus));
  957. }
  958. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  959. {
  960. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  961. }
  962. unsigned char isModeChange(unsigned char gun_index)
  963. {
  964. unsigned char result = NO;
  965. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  966. {
  967. result = YES;
  968. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  969. }
  970. return result;
  971. }
  972. //===============================================
  973. // Get firmware version
  974. //===============================================
  975. void get_firmware_version(unsigned char gun_index)
  976. {
  977. FILE *fp;
  978. char cmd[512];
  979. char buf[512];
  980. // Get CSU hardware version
  981. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  982. // Get CSU boot loader version
  983. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  984. // Get CSU kernel version
  985. sprintf(cmd, "/bin/uname -r");
  986. fp = popen(cmd, "r");
  987. if(fp == NULL)
  988. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  989. else
  990. {
  991. while(fgets(buf, sizeof(buf), fp) != NULL)
  992. {
  993. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  994. }
  995. }
  996. // Get MCU firmware version
  997. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  998. // Get CSU root file system version
  999. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "D0.19.00.0000.00");
  1000. // Get AC connector type from model name
  1001. for(uint8_t idx=0;idx<3;idx++)
  1002. {
  1003. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1004. {
  1005. case '1':
  1006. // J1772 Plug
  1007. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  1008. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  1009. break;
  1010. case '2':
  1011. // J1772 Socket
  1012. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  1013. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  1014. break;
  1015. case '3':
  1016. // CE Plug
  1017. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  1018. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  1019. break;
  1020. case '4':
  1021. // CE Socket
  1022. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  1023. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  1024. break;
  1025. case '5':
  1026. // GB Plug
  1027. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  1028. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  1029. break;
  1030. case '6':
  1031. // GB Socket
  1032. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  1033. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  1034. break;
  1035. }
  1036. }
  1037. // Get network option from model name
  1038. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1039. {
  1040. case 'B':
  1041. case 'U':
  1042. //Blue tooth
  1043. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1044. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  1045. break;
  1046. case 'W':
  1047. // WIFI
  1048. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1049. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  1050. break;
  1051. case 'T':
  1052. // 3G/4G
  1053. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1054. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  1055. break;
  1056. default:
  1057. // LAN
  1058. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1059. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  1060. break;
  1061. }
  1062. // Get rating power from model name
  1063. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1064. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1065. // Get vender code from model name
  1066. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1067. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1068. DEBUG_INFO("====================================================================\r\n");
  1069. DEBUG_INFO("Model: %s\r\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  1070. DEBUG_INFO("CSU hardware version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  1071. DEBUG_INFO("CSU boot loader version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  1072. DEBUG_INFO("CSU kernel version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  1073. DEBUG_INFO("CSU root file system version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  1074. DEBUG_INFO("CSU MCU-%2d firmware version: %s\r\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1075. DEBUG_INFO("====================================================================\r\n");
  1076. }
  1077. //===============================================
  1078. // Upgrade firmware
  1079. //===============================================
  1080. int upgrade_check()
  1081. {
  1082. int result = PASS;
  1083. int fd;
  1084. DIR *dir;
  1085. struct dirent *file;
  1086. char cmd[512];
  1087. long int MaxLen=48*1024*1024;
  1088. if ((dir = opendir ("/mnt")) != NULL)
  1089. {
  1090. /* print all the files and directories within directory */
  1091. while ((file = readdir (dir)) != NULL)
  1092. {
  1093. if(strlen(file->d_name)>2)
  1094. {
  1095. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  1096. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  1097. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  1098. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  1099. {
  1100. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  1101. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  1102. read(fd, ptr, MaxLen);
  1103. close(fd);
  1104. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  1105. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  1106. DEBUG_INFO("New firmware type: %X\r\n", ShmCharger->fwUpgradeInfo.fwType);
  1107. DEBUG_INFO("New firmware model name: %s, %s\r\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  1108. if((strcmp(ShmCharger->fwUpgradeInfo.modelName, (char*)ShmSysConfigAndInfo->SysConfig.ModelName)==0) && (ShmCharger->fwUpgradeInfo.fwType>0))
  1109. {
  1110. switch(ShmCharger->fwUpgradeInfo.fwType)
  1111. {
  1112. case CSU_BOOTLOADER:
  1113. case CSU_KERNEL_CONFIGURATION:
  1114. case CSU_KERNEL_IMAGE:
  1115. case CSU_ROOT_FILE_SYSTEM:
  1116. case CSU_USER_CONFIGURATION:
  1117. case CSU_PRIMARY_CONTROLLER:
  1118. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  1119. {
  1120. result = FAIL;
  1121. }
  1122. else
  1123. {
  1124. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  1125. {
  1126. DEBUG_INFO("Restore model name & serial number.\r\n");
  1127. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  1128. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1129. {
  1130. DEBUG_INFO("Re-load configuration fail.\r\n");
  1131. result = FAIL;
  1132. }
  1133. else
  1134. {
  1135. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  1136. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  1137. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  1138. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1139. {
  1140. DEBUG_INFO("Re-write configuration fail.\r\n");
  1141. result = FAIL;
  1142. }
  1143. else
  1144. DEBUG_INFO("Re-write configuration OK.\r\n");
  1145. }
  1146. }
  1147. }
  1148. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1149. system(cmd);
  1150. break;
  1151. case AC_WALLMOUNT_CONTROLLER:
  1152. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1153. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  1154. break;
  1155. default:
  1156. result = FAIL;
  1157. DEBUG_WARN("Image file is unknown type.\r\n");
  1158. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1159. system(cmd);
  1160. break;
  1161. }
  1162. }
  1163. else
  1164. {
  1165. result = FAIL;
  1166. DEBUG_ERROR("Model name and Firmware type error.\r\n");
  1167. }
  1168. free(ptr);
  1169. }
  1170. else
  1171. {
  1172. result = FAIL;
  1173. DEBUG_ERROR("New firmware open error.\r\n");
  1174. }
  1175. }
  1176. else
  1177. {
  1178. if(strlen(file->d_name) >= 3)
  1179. {
  1180. result = FAIL;
  1181. DEBUG_ERROR("File name error.\r\n");
  1182. }
  1183. else
  1184. {
  1185. DEBUG_ERROR("Searching file.\r\n");
  1186. }
  1187. }
  1188. }
  1189. closedir (dir);
  1190. }
  1191. else
  1192. {
  1193. result = FAIL;
  1194. DEBUG_ERROR("/mnt does not valid.\r\n");
  1195. }
  1196. return result;
  1197. }
  1198. //===============================================
  1199. // Check RFID is match with start user
  1200. //===============================================
  1201. int isMatchStartUser(unsigned char gun_index)
  1202. {
  1203. uint8_t tmpUser[32];
  1204. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  1205. {
  1206. // Big endian
  1207. switch(rfid.snType)
  1208. {
  1209. case RFID_SN_TYPE_6BYTE:
  1210. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  1211. break;
  1212. case RFID_SN_TYPE_7BYTE:
  1213. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  1214. break;
  1215. case RFID_SN_TYPE_10BYTE:
  1216. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  1217. break;
  1218. case RFID_SN_TYPE_4BYTE:
  1219. default:
  1220. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  1221. break;
  1222. }
  1223. }
  1224. else
  1225. {
  1226. // Little endian
  1227. switch(rfid.snType)
  1228. {
  1229. case RFID_SN_TYPE_6BYTE:
  1230. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  1231. break;
  1232. case RFID_SN_TYPE_7BYTE:
  1233. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  1234. break;
  1235. case RFID_SN_TYPE_10BYTE:
  1236. sprintf((char*)tmpUser, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  1237. break;
  1238. case RFID_SN_TYPE_4BYTE:
  1239. default:
  1240. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  1241. break;
  1242. }
  1243. }
  1244. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  1245. }
  1246. //===============================================
  1247. // Read RFID Serial Number
  1248. //===============================================
  1249. int GetCardSerialNumber()
  1250. {
  1251. int isSuccess = FAIL;
  1252. int module_type = MODULE_EWT;
  1253. if(getRequestCardSN(rfidFd,module_type,&rfid))
  1254. {
  1255. if(rfid.cardType == ISO14443A)
  1256. {
  1257. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  1258. {
  1259. isSuccess = PASS;
  1260. }
  1261. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  1262. {
  1263. isSuccess = PASS;
  1264. }
  1265. sethaltCard(rfidFd,module_type);
  1266. }
  1267. else if(rfid.cardType == IS014443B)
  1268. {
  1269. isSuccess = PASS;
  1270. }
  1271. else if(rfid.cardType == FELICA)
  1272. {
  1273. isSuccess = PASS;
  1274. }
  1275. else
  1276. {}
  1277. }
  1278. else
  1279. {}
  1280. return isSuccess;
  1281. }
  1282. //===============================================
  1283. // Set led motion
  1284. //===============================================
  1285. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  1286. {
  1287. switch(led_mode)
  1288. {
  1289. case LED_ACTION_INIT:
  1290. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  1291. break;
  1292. case LED_ACTION_IDLE:
  1293. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  1294. break;
  1295. case LED_ACTION_AUTHED:
  1296. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  1297. break;
  1298. case LED_ACTION_CONNECTED:
  1299. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  1300. break;
  1301. case LED_ACTION_CHARGING:
  1302. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  1303. break;
  1304. case LED_ACTION_STOP:
  1305. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  1306. break;
  1307. case LED_ACTION_ALARM:
  1308. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  1309. break;
  1310. case LED_ACTION_MAINTAIN:
  1311. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  1312. break;
  1313. case LED_ACTION_RFID_PASS:
  1314. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  1315. break;
  1316. case LED_ACTION_RFID_FAIL:
  1317. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  1318. break;
  1319. case LED_ACTION_BLE_CONNECT:
  1320. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  1321. break;
  1322. case LED_ACTION_BLE_DISABLE:
  1323. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  1324. break;
  1325. case LED_ACTION_DEBUG:
  1326. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  1327. break;
  1328. case LED_ACTION_ALL_OFF:
  1329. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  1330. break;
  1331. case LED_RELAY_ON:
  1332. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  1333. break;
  1334. case LED_RELAY_OFF:
  1335. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  1336. break;
  1337. case LED_ACTION_HANDSHAKE_FAIL:
  1338. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  1339. break;
  1340. case LED_ACTION_INTERNET_DISCONNECT:
  1341. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  1342. break;
  1343. }
  1344. }
  1345. //===============================================
  1346. // Request on/off set
  1347. //===============================================
  1348. void setRequest(unsigned char gun_index,unsigned char isOn)
  1349. {
  1350. if(isOn == ON)
  1351. {
  1352. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  1353. {
  1354. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  1355. DEBUG_INFO("Gun-%d Output relay sts: ON \r\n",gun_index);
  1356. }
  1357. }
  1358. else
  1359. {
  1360. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  1361. {
  1362. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  1363. DEBUG_INFO("Gun-%d Output relay sts: OFF \r\n",gun_index);
  1364. }
  1365. }
  1366. }
  1367. //===============================================
  1368. // Request on/off get
  1369. //===============================================
  1370. int getRequest(unsigned char gun_index)
  1371. {
  1372. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  1373. }
  1374. //===============================================
  1375. // Relay on/off set
  1376. //===============================================
  1377. void setRelay(unsigned char gun_index,unsigned char isOn)
  1378. {
  1379. if(isOn == ON)
  1380. {
  1381. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  1382. {
  1383. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  1384. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  1385. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  1386. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  1387. DEBUG_INFO("Gun-%d Output relay sts: ON \r\n",gun_index);
  1388. }
  1389. }
  1390. else
  1391. {
  1392. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  1393. {
  1394. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  1395. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  1396. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  1397. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  1398. DEBUG_INFO("Gun-%d Output relay sts: OFF \r\n",gun_index);
  1399. }
  1400. }
  1401. }
  1402. //===============================================
  1403. // Relay on/off get
  1404. //===============================================
  1405. int getRelay(unsigned char gun_index)
  1406. {
  1407. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  1408. }
  1409. //===============================================
  1410. // Set speaker on/off request
  1411. //===============================================
  1412. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  1413. {
  1414. if(isOn == ON)
  1415. {
  1416. ShmCharger->isSpeakerOn = ON;
  1417. ShmCharger->speaker_type = speaker_mode;
  1418. }
  1419. }
  1420. //===============================================
  1421. // Initialization RFID communication port
  1422. //===============================================
  1423. int InitRfidPort()
  1424. {
  1425. int uartO2 = open(rfidPortName, O_RDWR);
  1426. struct termios tios;
  1427. if (uartO2 != FAIL)
  1428. {
  1429. ioctl (uartO2, TCGETS, &tios);
  1430. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1431. tios.c_lflag = 0;
  1432. tios.c_iflag = 0;
  1433. tios.c_oflag = 0;
  1434. tios.c_cc[VMIN] = 0;
  1435. tios.c_cc[VTIME] = (unsigned char) 1;
  1436. tios.c_lflag = 0;
  1437. tcflush(uartO2, TCIFLUSH);
  1438. ioctl(uartO2, TCSETS, &tios);
  1439. }
  1440. if (uartO2 < 0)
  1441. {
  1442. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1443. }
  1444. return uartO2;
  1445. }
  1446. //===============================================
  1447. // Check internet access status
  1448. //===============================================
  1449. int isReachableInternet()
  1450. {
  1451. int result = FAIL;
  1452. FILE *fp;
  1453. char cmd[256];
  1454. char buf[512];
  1455. //char tmp[512];
  1456. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1457. {
  1458. strcpy(cmd, "ping -c 1 -w 3 ");
  1459. strcat(cmd, valid_Internet[idx]);
  1460. fp = popen(cmd, "r");
  1461. if(fp != NULL)
  1462. {
  1463. while(fgets(buf, sizeof(buf), fp) != NULL)
  1464. {
  1465. if(strstr(buf, "transmitted") > 0)
  1466. {
  1467. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1468. if(strstr(buf,"100%") != NULL)
  1469. {
  1470. }
  1471. else
  1472. {
  1473. result = PASS;
  1474. }
  1475. //DEBUG_INFO("%s",buf);
  1476. //DEBUG_INFO("%s\n",tmp);
  1477. }
  1478. }
  1479. }
  1480. pclose(fp);
  1481. }
  1482. return result;
  1483. }
  1484. //===============================================
  1485. // Check reservation date is expired
  1486. //===============================================
  1487. int isReservationExpired(unsigned char gun_index)
  1488. {
  1489. int result = NO;
  1490. struct tm expiredDate;
  1491. struct timeb expiredTime;
  1492. if(sscanf((char*)ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate, "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year, &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour, &expiredDate.tm_min, &expiredDate.tm_sec) == 6)
  1493. {
  1494. expiredDate.tm_year -= 1900;
  1495. expiredDate.tm_mon -= 1;
  1496. expiredTime.time = mktime(&expiredDate);
  1497. if(DiffTimebWithNow(expiredTime)>=0)
  1498. {
  1499. result = YES;
  1500. }
  1501. }
  1502. else
  1503. {
  1504. DEBUG_WARN("Expired date parsing error.\r\n");
  1505. DEBUG_INFO("Date expired.\r\n");
  1506. result = YES;
  1507. }
  1508. return result;
  1509. }
  1510. //===============================================
  1511. // Check charging profile related date routine
  1512. //===============================================
  1513. int isProfileValid(uint8_t gun_index)
  1514. {
  1515. int result = NO;
  1516. struct tm tmFrom, tmTo;
  1517. struct timeb tbFrom, tbTo;
  1518. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom, "%4d-%2d-%2dT%2d:%2d:%2d", &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6) &&
  1519. (sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo, "%4d-%2d-%2dT%2d:%2d:%2d", &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
  1520. {
  1521. tmFrom.tm_year -= 1900;
  1522. tmFrom.tm_mon -= 1;
  1523. tbFrom.time = mktime(&tmFrom);
  1524. tmTo.tm_year -= 1900;
  1525. tmTo.tm_mon -= 1;
  1526. tbTo.time = mktime(&tmTo);
  1527. DEBUG_INFO("Valid from compare Now: %d\r\n", DiffTimebWithNow(tbFrom));
  1528. DEBUG_INFO("Valid to compare Now: %d\r\n", DiffTimebWithNow(tbTo));
  1529. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  1530. {
  1531. result = YES;
  1532. }
  1533. }
  1534. else
  1535. {
  1536. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\r\n");
  1537. }
  1538. return result;
  1539. }
  1540. int getScheduleStart(int gun_index)
  1541. {
  1542. int result = -1;
  1543. struct tm tmScheduleStart;;
  1544. struct timeb tbScheduleStart;
  1545. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule, "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday, &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6))
  1546. {
  1547. tmScheduleStart.tm_year -= 1900;
  1548. tmScheduleStart.tm_mon -= 1;
  1549. tbScheduleStart.time = mktime(&tmScheduleStart);
  1550. tbScheduleStart.millitm = 0;
  1551. result = DiffTimebWithNow(tbScheduleStart)/1000;
  1552. DEBUG_INFO("Schedule start compare Now(seconds): %d\r\n", result);
  1553. }
  1554. else
  1555. {
  1556. DEBUG_WARN("Schedule start date parsing error.\r\n");
  1557. }
  1558. return result;
  1559. }
  1560. int getStartSinceToday()
  1561. {
  1562. int result = -1;
  1563. time_t t;
  1564. struct tm *tmStartToday;
  1565. struct timeb tbStartToday;
  1566. t=time(NULL);
  1567. tmStartToday=localtime(&t);
  1568. tmStartToday->tm_hour = 0;
  1569. tmStartToday->tm_min = 0;
  1570. tmStartToday->tm_sec = 0;
  1571. tbStartToday.time = mktime(tmStartToday);
  1572. result = DiffTimebWithNow(tbStartToday)/1000;
  1573. DEBUG_INFO("Start today compare Now(seconds): %d\r\n", result);
  1574. return result;
  1575. }
  1576. int getStartSinceWeek()
  1577. {
  1578. int result = -1;
  1579. time_t t;
  1580. struct tm *tmStartWeek;
  1581. struct timeb tbStartWeek;
  1582. t=time(NULL);
  1583. tmStartWeek=localtime(&t);
  1584. t-=86400*tmStartWeek->tm_wday;
  1585. tmStartWeek=localtime(&t);
  1586. tmStartWeek->tm_hour = 0;
  1587. tmStartWeek->tm_min = 0;
  1588. tmStartWeek->tm_sec = 0;
  1589. tbStartWeek.time = mktime(tmStartWeek);
  1590. result = DiffTimebWithNow(tbStartWeek)/1000;
  1591. DEBUG_INFO("Start week compare Now(seconds): %d\r\n", result);
  1592. return result;
  1593. }
  1594. //===============================================
  1595. // Valid from local white list
  1596. //===============================================
  1597. int isValidLocalWhiteCard()
  1598. {
  1599. uint8_t result = FAIL;
  1600. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  1601. {
  1602. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  1603. {
  1604. result = PASS;
  1605. }
  1606. }
  1607. return result;
  1608. }
  1609. //==========================================
  1610. // Get Ethernet MAC address
  1611. //==========================================
  1612. int getEth0MacAddress()
  1613. {
  1614. int result = PASS;
  1615. FILE *fp;
  1616. char cmd[256];
  1617. char buf[512];
  1618. char tmp[512];
  1619. strcpy(cmd, "ifconfig");;
  1620. fp = popen(cmd, "r");
  1621. if(fp != NULL)
  1622. {
  1623. while(fgets(buf, sizeof(buf), fp) != NULL)
  1624. {
  1625. if(strstr(buf, "eth0") > 0)
  1626. {
  1627. result = PASS;
  1628. }
  1629. if(strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
  1630. {
  1631. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  1632. strcpy((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
  1633. }
  1634. }
  1635. }
  1636. pclose(fp);
  1637. return result;
  1638. }
  1639. //==========================================
  1640. // Check task processing
  1641. //==========================================
  1642. void checkTask()
  1643. {
  1644. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1645. {
  1646. if(system("pidof -s Module_4g > /dev/null") != 0)
  1647. {
  1648. DEBUG_INFO("Module_4g not running, restart it.\r\n");
  1649. system("/root/Module_4g &");
  1650. }
  1651. }
  1652. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1653. {
  1654. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  1655. {
  1656. DEBUG_INFO("Module_Wifi not running, restart it.\r\n");
  1657. system("/root/Module_Wifi &");
  1658. }
  1659. }
  1660. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  1661. {
  1662. DEBUG_INFO("Module_EventLogging not running, restart it.\r\n");
  1663. system("/root/Module_EventLogging &");
  1664. }
  1665. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1666. {
  1667. DEBUG_INFO("OcppBackend not running, restart it.\r\n");
  1668. system("/root/OcppBackend &");
  1669. }
  1670. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  1671. {
  1672. DEBUG_INFO("Module_AlarmDetect not running, restart it.\r\n");
  1673. system("/root/Module_AlarmDetect &");
  1674. }
  1675. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  1676. {
  1677. DEBUG_INFO("Module_InternalComm not running, restart it.\r\n");
  1678. system("/root/Module_InternalComm &");
  1679. }
  1680. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  1681. {
  1682. DEBUG_INFO("Module_Speaker not running, restart it.\r\n");
  1683. system("/root/Module_Speaker &");
  1684. }
  1685. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  1686. {
  1687. DEBUG_INFO("Module_ProduceUtils not running, restart it.\r\n");
  1688. system ("/root/Module_ProduceUtils &");
  1689. }
  1690. }
  1691. //===============================================
  1692. // Check Connection Timeout
  1693. //===============================================
  1694. void checkConnectionTimeout()
  1695. {
  1696. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1697. {
  1698. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1699. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \r\n", TIMEOUT_SPEC_HANDSHAKING/1000);
  1700. }
  1701. else
  1702. {
  1703. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  1704. {
  1705. ShmCharger->timeoutSpec.Setting_Timeout_Spec = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  1706. if(ShmCharger->timeoutSpec.Setting_Timeout_Spec <= 0)
  1707. {
  1708. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1709. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\r\n.");
  1710. }
  1711. else
  1712. {
  1713. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  1714. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\r\n.");
  1715. }
  1716. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \r\n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  1717. }
  1718. else
  1719. {
  1720. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1721. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Table is blank...\r\n.");
  1722. }
  1723. //DEBUG_INFO("Present timeout spec : %d \r\n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  1724. }
  1725. }
  1726. //===============================================
  1727. // Main process
  1728. //===============================================
  1729. int main(void)
  1730. {
  1731. //Create all share memory
  1732. if(CreatShareMemory()==0)
  1733. {
  1734. DEBUG_ERROR("CreatShareMemory NG\r\n");
  1735. if(ShmStatusCodeData!=NULL)
  1736. {
  1737. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1738. }
  1739. sleep(5);
  1740. system("reboot -f");
  1741. sleep(5);
  1742. system("reboot -f");
  1743. }
  1744. else
  1745. {
  1746. DEBUG_INFO("CreatShareMemory OK\r\n");
  1747. }
  1748. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1749. {
  1750. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  1751. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  1752. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  1753. ShmCharger->gun_info[gun_index].isInitialPass = NO;
  1754. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  1755. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  1756. }
  1757. // Main loop
  1758. for(;;)
  1759. {
  1760. //==========================================
  1761. // Synchronize share memory from OCPP struct
  1762. //==========================================
  1763. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ShmOCPP16Data->OcppConnStatus;
  1764. //==========================================
  1765. // Ethernet MAC address
  1766. //==========================================
  1767. getEth0MacAddress();
  1768. //==========================================
  1769. // Check task processing
  1770. //==========================================
  1771. checkTask();
  1772. //==========================================
  1773. // Check connection timeout specification
  1774. //==========================================
  1775. checkConnectionTimeout();
  1776. //==========================================
  1777. // Something need run in Idle mode
  1778. //==========================================
  1779. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM)) &&
  1780. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM)):true))
  1781. {
  1782. //======================================
  1783. // Check restore factory setting request
  1784. //======================================
  1785. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  1786. {
  1787. // Set led to initial
  1788. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1789. {
  1790. setLedMotion(gun_index,LED_ACTION_INIT);
  1791. }
  1792. system("cd /root;./Module_FactoryConfig -m");
  1793. system("sync");
  1794. sleep(5);
  1795. system("reboot -f");
  1796. sleep(5);
  1797. system("reboot -f");
  1798. }
  1799. //======================================
  1800. // Check upgrade firmware request
  1801. //======================================
  1802. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  1803. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq)
  1804. {
  1805. ShmCharger->isUpdateSuccess = NO;
  1806. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1807. {
  1808. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  1809. setChargerMode(gun_index, SYS_MODE_UPDATE);
  1810. }
  1811. }
  1812. }
  1813. //==========================================
  1814. // Check remote reset request
  1815. //==========================================
  1816. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  1817. {
  1818. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  1819. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):true))
  1820. {
  1821. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  1822. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  1823. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  1824. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  1825. {
  1826. system("sync");
  1827. sleep(5);
  1828. system("reboot -f");
  1829. sleep(5);
  1830. system("reboot -f");
  1831. }
  1832. else
  1833. {
  1834. sleep(5);
  1835. close(wtdFd);
  1836. system("/usr/bin/run_evse_restart.sh");
  1837. }
  1838. }
  1839. }
  1840. //==========================================
  1841. // Check RFID
  1842. //==========================================
  1843. if(!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq)
  1844. {
  1845. if(GetCardSerialNumber()!= FAIL)
  1846. {
  1847. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  1848. }
  1849. }
  1850. //==========================================
  1851. // Connector loop
  1852. //==========================================
  1853. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1854. {
  1855. // Synchronize current rating value from MCU
  1856. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1857. // Assign connector location index for OCPP
  1858. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  1859. // Synchronize present charging power
  1860. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  1861. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  1862. else
  1863. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  1864. // The system switch to Booting mode
  1865. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == 0XFF)
  1866. {
  1867. setChargerMode(gun_index, SYS_MODE_BOOTING);
  1868. }
  1869. // Check initialization "PASS" or "FAIL"
  1870. if(ShmCharger->gun_info[gun_index].isInitialPass == YES)
  1871. {
  1872. // Alarm event check
  1873. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  1874. {
  1875. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  1876. {
  1877. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE)
  1878. {
  1879. }
  1880. else
  1881. {
  1882. setChargerMode(gun_index, SYS_MODE_ALARM);
  1883. }
  1884. }
  1885. }
  1886. else
  1887. {
  1888. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM)
  1889. {
  1890. }
  1891. }
  1892. }
  1893. // Reservation request check
  1894. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  1895. {
  1896. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  1897. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  1898. {
  1899. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  1900. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  1901. }
  1902. DEBUG_INFO("Reservation request on connector-%d.\r\n", gun_index);
  1903. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  1904. }
  1905. // Change availability check
  1906. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  1907. {
  1908. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  1909. {
  1910. DB_Update_Operactive(localDb, gun_index, true);
  1911. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  1912. }
  1913. else
  1914. {
  1915. DB_Update_Operactive(localDb, gun_index, false);
  1916. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  1917. }
  1918. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  1919. }
  1920. if(ShmCharger->gun_info[gun_index].isOperactive)
  1921. {
  1922. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  1923. {
  1924. setChargerMode(gun_index, SYS_MODE_IDLE);
  1925. }
  1926. }
  1927. else
  1928. {
  1929. if(isMode(gun_index, SYS_MODE_IDLE))
  1930. {
  1931. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  1932. }
  1933. }
  1934. // Unlock Connector signal check
  1935. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  1936. {
  1937. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  1938. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  1939. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  1940. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  1941. }
  1942. // Connector process
  1943. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  1944. {
  1945. case SYS_MODE_BOOTING:
  1946. if(isModeChange(gun_index))
  1947. {
  1948. setLedMotion(gun_index,LED_ACTION_INIT);
  1949. //CSU Initialization & task spawn
  1950. if((Initialization() != PASS) ||
  1951. (SpawnTask() != PASS))
  1952. {
  1953. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  1954. }
  1955. }
  1956. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  1957. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  1958. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  1959. {
  1960. ShmCharger->gun_selectd = 0;
  1961. // Firmware version
  1962. get_firmware_version(gun_index);
  1963. // OCPP BootNotification info set
  1964. memcpy((char*)ShmOCPP16Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  1965. memcpy((char*)ShmOCPP16Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1966. sprintf((char*)ShmOCPP16Data->BootNotification.CpFwVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  1967. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
  1968. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "AC");
  1969. DEBUG_INFO("==========================================\r\n");
  1970. DEBUG_INFO("System ID: %s\r\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  1971. DEBUG_INFO("==========================================\r\n");
  1972. DEBUG_INFO("========== OCPP BootNotification information ==========\r\n");
  1973. DEBUG_INFO("OCPP OcppServerURL: %s\r\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  1974. DEBUG_INFO("OCPP ChargeBoxId: %s\r\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1975. DEBUG_INFO("OCPP ChargePointVendor: %s\r\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  1976. DEBUG_INFO("OCPP CpFwVersion: %s\r\n",ShmOCPP16Data->BootNotification.CpFwVersion);
  1977. DEBUG_INFO("OCPP CpMeterSerialNumber: %s\r\n",ShmOCPP16Data->BootNotification.CpMeterSerialNumber);
  1978. DEBUG_INFO("OCPP CpMeterType: %s\r\n",ShmOCPP16Data->BootNotification.CpMeterType);
  1979. DEBUG_INFO("=======================================================\r\n");
  1980. DEBUG_INFO("========== Set Wifi information ==========\r\n");
  1981. DEBUG_INFO("Wifi mode: %d\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  1982. DEBUG_INFO("Wifi SSID: %s\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  1983. DEBUG_INFO("Wifi password: %s\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  1984. DEBUG_INFO("==========================================\r\n");
  1985. // Flag for initialization system success
  1986. ShmCharger->gun_info[gun_index].isInitialPass = YES;
  1987. // Set max current to rating current
  1988. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1989. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float)ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1990. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  1991. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1992. // If rotate switch equal zero, the system needs to change Debug mode
  1993. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  1994. setChargerMode(gun_index, SYS_MODE_DEBUG);
  1995. else
  1996. setChargerMode(gun_index, SYS_MODE_IDLE);
  1997. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = AC_INPUT_PHASE;
  1998. }
  1999. break;
  2000. case SYS_MODE_IDLE:
  2001. if(isModeChange(gun_index))
  2002. {
  2003. setLedMotion(gun_index,LED_ACTION_IDLE);
  2004. setRelay(gun_index, OFF);
  2005. setRequest(gun_index,OFF);
  2006. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  2007. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2008. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  2009. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  2010. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  2011. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  2012. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  2013. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  2014. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2015. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  2016. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = (ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent > 0) ? ((ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent<ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].primaryMcuState.rating_current) : ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  2017. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  2018. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  2019. // Response StopTransactionConf
  2020. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf == ON)
  2021. {
  2022. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = OFF;
  2023. }
  2024. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5;
  2025. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  2026. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  2027. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2028. }
  2029. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  2030. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  2031. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  2032. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  2033. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == ON))
  2034. {
  2035. // Clean User id & Card Number
  2036. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2037. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  2038. {
  2039. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  2040. DEBUG_INFO("Start Method : RFID...\r\n");
  2041. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2042. {
  2043. // Big endian
  2044. switch(rfid.snType)
  2045. {
  2046. case RFID_SN_TYPE_6BYTE:
  2047. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  2048. break;
  2049. case RFID_SN_TYPE_7BYTE:
  2050. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  2051. break;
  2052. case RFID_SN_TYPE_10BYTE:
  2053. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  2054. break;
  2055. case RFID_SN_TYPE_4BYTE:
  2056. default:
  2057. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2058. break;
  2059. }
  2060. }
  2061. else
  2062. {
  2063. // Little endian
  2064. switch(rfid.snType)
  2065. {
  2066. case RFID_SN_TYPE_6BYTE:
  2067. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2068. break;
  2069. case RFID_SN_TYPE_7BYTE:
  2070. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2071. break;
  2072. case RFID_SN_TYPE_10BYTE:
  2073. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2074. break;
  2075. case RFID_SN_TYPE_4BYTE:
  2076. default:
  2077. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2078. break;
  2079. }
  2080. }
  2081. DEBUG_INFO("Start request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2082. }
  2083. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq)
  2084. {
  2085. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  2086. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  2087. DEBUG_INFO("Start Method : BACKEND...\r\n");
  2088. }
  2089. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  2090. {
  2091. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  2092. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  2093. DEBUG_INFO("Start Method : BLE...\r\n");
  2094. }
  2095. else
  2096. {
  2097. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  2098. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  2099. DEBUG_INFO("Start Method : FREE..\r\n");
  2100. }
  2101. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2102. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  2103. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  2104. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  2105. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  2106. }
  2107. else
  2108. {}
  2109. break;
  2110. case SYS_MODE_AUTHORIZING:
  2111. if(isModeChange(gun_index))
  2112. {
  2113. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  2114. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  2115. {
  2116. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  2117. {
  2118. case AUTH_MODE_ENABLE:
  2119. if(ShmOCPP16Data->OcppConnStatus)
  2120. {
  2121. // On line
  2122. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2123. }
  2124. else
  2125. {
  2126. // Off line
  2127. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  2128. {
  2129. case OFF_POLICY_LOCALLIST:
  2130. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2131. break;
  2132. case OFF_POLICY_PH_RFID:
  2133. break;
  2134. case OFF_POLICY_FREE:
  2135. break;
  2136. case OFF_POLICY_NOCHARGE:
  2137. default:
  2138. break;
  2139. }
  2140. }
  2141. break;
  2142. case AUTH_MODE_DISABLE:
  2143. default:
  2144. break;
  2145. }
  2146. }
  2147. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2148. }
  2149. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  2150. {
  2151. // Authorization timeout process.
  2152. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2153. setChargerMode(gun_index, SYS_MODE_IDLE);
  2154. DEBUG_WARN("Authorize timeout !!!\r\n");
  2155. }
  2156. else
  2157. {
  2158. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  2159. {
  2160. case START_METHOD_RFID:
  2161. if((ShmOCPP16Data->SpMsg.bits.AuthorizeConf) ||
  2162. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  2163. (!ShmOCPP16Data->OcppConnStatus && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  2164. (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))
  2165. {
  2166. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
  2167. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  2168. (!ShmOCPP16Data->OcppConnStatus && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  2169. {
  2170. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2171. DEBUG_INFO("Authorize pass.\r\n");
  2172. setSpeaker(ON,SPEAKER_SHORT);
  2173. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  2174. sleep(3);
  2175. setChargerMode(gun_index, SYS_MODE_PREPARING);
  2176. }
  2177. else
  2178. {
  2179. DEBUG_INFO("Authorize fail.\r\n");
  2180. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  2181. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  2182. sleep(3);
  2183. setChargerMode(gun_index, SYS_MODE_IDLE);
  2184. }
  2185. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2186. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  2187. }
  2188. break;
  2189. case START_METHOD_BACKEND:
  2190. case START_METHOD_BLE:
  2191. case START_METHOD_FREE:
  2192. default:
  2193. setSpeaker(ON,SPEAKER_SHORT);
  2194. setChargerMode(gun_index, SYS_MODE_PREPARING);
  2195. break;
  2196. }
  2197. }
  2198. break;
  2199. case SYS_MODE_PREPARING:
  2200. if(isModeChange(gun_index))
  2201. {
  2202. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2203. setLedMotion(gun_index,LED_ACTION_AUTHED);
  2204. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5;
  2205. }
  2206. // If control pilot detect Bx, skip watch dog time out.
  2207. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) ||
  2208. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  2209. {
  2210. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2211. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  2212. switch(ShmCharger->gun_info[gun_index].ccsHandshakeState)
  2213. {
  2214. case HANDSHAKE_DUTY_5:
  2215. setRequest(gun_index, ON);
  2216. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  2217. DEBUG_INFO("Set Request On.\r\n");
  2218. // Set CCS 5% PWM duty
  2219. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  2220. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK;
  2221. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2222. break;
  2223. case HANDSHAKE_DUTY_5_CHECK:
  2224. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  2225. {
  2226. //2 secs timeout
  2227. ftime(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  2228. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CCS;
  2229. //Let CCS task start to negotiate
  2230. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = ON;
  2231. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 0;
  2232. }
  2233. break;
  2234. case HANDSHAKE_CCS:
  2235. //CCS handshake timeout
  2236. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  2237. {
  2238. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  2239. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 1;
  2240. DEBUG_INFO("BS/HLC 2 secs handshake timeout.\r\n");
  2241. }
  2242. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  2243. {
  2244. DEBUG_INFO("Wait CCS give up negotiagting.\r\n");
  2245. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_E;
  2246. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2247. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  2248. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  2249. }
  2250. #ifdef CCS_SIMULATION_DATA
  2251. ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus = 1;
  2252. #endif //CCS_SIMULATION_DATA
  2253. //CCS status check
  2254. if(ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus > 0)
  2255. {
  2256. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
  2257. DEBUG_INFO("Enter HLC Mode charging.\r\n");
  2258. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
  2259. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2260. }
  2261. // Use RFID card to stop handshaking
  2262. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  2263. {
  2264. DEBUG_INFO("Use RFID card to stop handshaking.\r\n");
  2265. setSpeaker(ON,SPEAKER_SHORT);
  2266. setChargerMode(gun_index, SYS_MODE_IDLE);
  2267. }
  2268. else
  2269. {
  2270. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2271. }
  2272. break;
  2273. case HANDSHAKE_CP_STATE_E:
  2274. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  2275. {
  2276. DEBUG_INFO("Change to CP STATE E for 4 secs.\r\n");
  2277. //CP STATE E for 4 secs
  2278. sleep(4);
  2279. //restore normal CP PWM duty
  2280. // Determine max charging current to MCU
  2281. DEBUG_INFO("Determine max charging current to MCU.\r\n");
  2282. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  2283. {
  2284. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  2285. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2286. {
  2287. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  2288. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2289. }
  2290. }
  2291. else
  2292. {
  2293. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2294. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2295. {
  2296. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2297. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2298. }
  2299. }
  2300. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2301. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
  2302. }
  2303. break;
  2304. case HANDSHAKE_SET_MAX_CURRENT:
  2305. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  2306. {
  2307. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_BS_MODE;
  2308. DEBUG_INFO("Enter BS Mode charging.\r\n");
  2309. }
  2310. break;
  2311. case HANDSHAKE_BS_MODE:
  2312. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  2313. setRelay(gun_index,ON);
  2314. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  2315. {
  2316. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  2317. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
  2318. {
  2319. DEBUG_INFO("Handshaking timeout...\r\n");
  2320. setChargerMode(gun_index, SYS_MODE_IDLE);
  2321. }
  2322. }
  2323. else if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON))
  2324. {
  2325. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  2326. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2327. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  2328. ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
  2329. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
  2330. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2331. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  2332. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = ON;
  2333. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2334. }
  2335. break;
  2336. case HANDSHAKE_HLC_MODE:
  2337. #ifdef CCS_SIMULATION_DATA
  2338. ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress = HLC_START_MODE;
  2339. #endif //CCS_SIMULATION_DATA
  2340. if(ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE)
  2341. {
  2342. setRelay(gun_index,ON);
  2343. ShmCharger->gun_info[gun_index].isCCSStartTransation = ON;
  2344. }
  2345. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) &&
  2346. (ShmCharger->gun_info[gun_index].isCCSStartTransation == ON))
  2347. {
  2348. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  2349. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2350. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  2351. ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
  2352. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
  2353. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2354. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  2355. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = ON;
  2356. ShmCharger->gun_info[gun_index].isCCSStartTransation = OFF;
  2357. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2358. ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  2359. }
  2360. //120 sec timeout
  2361. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
  2362. {
  2363. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  2364. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5000)
  2365. {
  2366. DEBUG_INFO("CCS 120 secs handshake timeout.\r\n");
  2367. //Cancel CCS task negotiating
  2368. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  2369. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 1;
  2370. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  2371. #ifdef CCS_SIMULATION_DATA
  2372. ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty = CCS_PWM_DUTY_100;
  2373. #endif //CCS_SIMULATION_DATA
  2374. }
  2375. }
  2376. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100)
  2377. {
  2378. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  2379. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2380. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  2381. setChargerMode(gun_index, SYS_MODE_IDLE);
  2382. }
  2383. break;
  2384. default:
  2385. break;
  2386. }
  2387. }
  2388. // Unplug charging gun to Idle mode
  2389. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  2390. {
  2391. DEBUG_INFO("Charging gun is plugged before.\r\n");
  2392. //setChargerMode(gun_index, SYS_MODE_IDLE);
  2393. //Cancel CCS task negotiating
  2394. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  2395. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 1;
  2396. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  2397. //#ifdef CCS_SIMULATION_DATA
  2398. ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty = CCS_PWM_DUTY_100;
  2399. //#endif //CCS_SIMULATION_DATA
  2400. }
  2401. // Use RFID card to stop handshaking
  2402. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  2403. {
  2404. DEBUG_INFO("Use RFID card to stop handshaking.\r\n");
  2405. setSpeaker(ON,SPEAKER_SHORT);
  2406. //setChargerMode(gun_index, SYS_MODE_IDLE);
  2407. //Cancel CCS task negotiating
  2408. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  2409. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 1;
  2410. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  2411. #ifdef CCS_SIMULATION_DATA
  2412. ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty = CCS_PWM_DUTY_100;
  2413. #endif //CCS_SIMULATION_DATA
  2414. }
  2415. else
  2416. {
  2417. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2418. }
  2419. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5)
  2420. {
  2421. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  2422. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2423. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  2424. setChargerMode(gun_index, SYS_MODE_IDLE);
  2425. }
  2426. break;
  2427. case SYS_MODE_CHARGING:
  2428. if(isModeChange(gun_index))
  2429. {
  2430. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2431. ftime(&startChargingTime[gun_index]);
  2432. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2433. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  2434. }
  2435. switch(ShmCharger->gun_info[gun_index].chargingMode)
  2436. {
  2437. case CHARGING_MODE_BS:
  2438. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  2439. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  2440. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON)||
  2441. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  2442. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  2443. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON))
  2444. {
  2445. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2446. }
  2447. else
  2448. {
  2449. setLedMotion(gun_index,LED_ACTION_CHARGING);
  2450. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
  2451. ftime(&endChargingTime[gun_index]);
  2452. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2453. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = ((float)(ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start))/100;
  2454. // Response StartTransactionConf
  2455. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == ON)
  2456. {
  2457. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = OFF;
  2458. }
  2459. // Charging profile preparation
  2460. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  2461. {
  2462. if(!ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf)
  2463. {
  2464. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = ON;
  2465. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  2466. }
  2467. else
  2468. {
  2469. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = OFF;
  2470. }
  2471. }
  2472. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  2473. if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId>0) &&
  2474. (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON) &&
  2475. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2476. {
  2477. // Debug information
  2478. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2479. {
  2480. DEBUG_INFO("Profile ID found: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2481. DEBUG_INFO("Valid from: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2482. DEBUG_INFO("Valid to: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2483. DEBUG_INFO("Start schedule: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2484. DEBUG_INFO("Profile kind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2485. DEBUG_INFO("RecurrencyKind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2486. DEBUG_INFO("Profile purpose: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2487. DEBUG_INFO("Transaction ID: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2488. DEBUG_INFO("ChargingRateUnit: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2489. }
  2490. // Checking profile kind
  2491. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2492. {
  2493. // Absolute profile
  2494. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2495. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2496. {
  2497. // Checking limitation
  2498. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2499. {
  2500. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2501. (((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod == 0) && (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit != 0)) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0)))
  2502. {
  2503. ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit,"W")==PASS?(uint16_t)(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/220):(uint16_t)(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit));
  2504. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2505. }
  2506. else
  2507. break;
  2508. }
  2509. }
  2510. }
  2511. }
  2512. // Determine max charging current to MCU
  2513. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  2514. {
  2515. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  2516. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2517. {
  2518. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  2519. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2520. }
  2521. }
  2522. else
  2523. {
  2524. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2525. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2526. {
  2527. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2528. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2529. }
  2530. }
  2531. // Debug information
  2532. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2533. {
  2534. DEBUG_INFO("=======================================================================\r\n");
  2535. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \r\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  2536. DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  2537. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\r\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  2538. DEBUG_INFO("=======================================================================\r\n");
  2539. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2540. }
  2541. if(ShmOCPP16Data->OcppConnStatus)
  2542. {
  2543. // On-line max condition check
  2544. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  2545. {
  2546. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2547. DEBUG_INFO("Connector-%d charging duration(%d) already over max duration(%d) in second.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  2548. }
  2549. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  2550. {
  2551. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2552. DEBUG_INFO("Connector-%d charging energy(%.2f) already over max energy(%.2f) in KWH.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy));
  2553. }
  2554. else
  2555. {
  2556. setRelay(gun_index, ON);
  2557. }
  2558. }
  2559. else
  2560. {
  2561. // Off-line max condition check
  2562. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2563. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  2564. {
  2565. if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))))
  2566. {
  2567. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2568. DEBUG_INFO("Connector-%d charging duration(%d) already over off-line max duration(%d) in second.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  2569. }
  2570. else if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
  2571. {
  2572. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2573. DEBUG_INFO("Connector-%d charging energy(%.2f) already over off-line max energy(%.2f) in KWH.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy));
  2574. }
  2575. else
  2576. {
  2577. setRelay(gun_index, ON);
  2578. }
  2579. }
  2580. else
  2581. {
  2582. setRelay(gun_index, OFF);
  2583. setRequest(gun_index, OFF);
  2584. DEBUG_INFO("Connector-%d can not charging in off line\r\n", gun_index);
  2585. }
  2586. }
  2587. }
  2588. break;
  2589. case CHARGING_MODE_HLC:
  2590. //if time up, clear CCS MSG count
  2591. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET)
  2592. {
  2593. ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  2594. if(ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat > 0)
  2595. {
  2596. ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat = 0;
  2597. }
  2598. else
  2599. {
  2600. DEBUG_INFO("CCS could not get MSG from car.\r\n");
  2601. //setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2602. //setRelay(gun_index, OFF);
  2603. #ifdef CCS_SIMULATION_DATA
  2604. //ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress = HLC_STOP_MODE;
  2605. #endif //CCS_SIMULATION_DATA
  2606. }
  2607. }
  2608. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  2609. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  2610. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON)||
  2611. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  2612. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  2613. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON) ||
  2614. (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE) ||
  2615. (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE) ||
  2616. (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE))
  2617. {
  2618. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2619. setRelay(gun_index, OFF);
  2620. }
  2621. else
  2622. {
  2623. setLedMotion(gun_index,LED_ACTION_CHARGING);
  2624. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
  2625. ftime(&endChargingTime[gun_index]);
  2626. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2627. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = ((float)(ShmCharger->gun_info[gun_index].powerConsumption.power_consumption - ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start))/100;
  2628. // Response StartTransactionConf
  2629. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == ON)
  2630. {
  2631. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = OFF;
  2632. }
  2633. // Charging profile preparation
  2634. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  2635. {
  2636. if(!ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf)
  2637. {
  2638. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = ON;
  2639. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  2640. }
  2641. else
  2642. {
  2643. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = OFF;
  2644. }
  2645. }
  2646. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  2647. if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId>0) &&
  2648. (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON) &&
  2649. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2650. {
  2651. // Debug information
  2652. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2653. {
  2654. DEBUG_INFO("Profile ID found: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2655. DEBUG_INFO("Valid from: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2656. DEBUG_INFO("Valid to: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2657. DEBUG_INFO("Start schedule: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2658. DEBUG_INFO("Profile kind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2659. DEBUG_INFO("RecurrencyKind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2660. DEBUG_INFO("Profile purpose: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2661. DEBUG_INFO("Transaction ID: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2662. DEBUG_INFO("ChargingRateUnit: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2663. }
  2664. // Checking profile kind
  2665. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2666. {
  2667. // Absolute profile
  2668. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2669. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2670. {
  2671. // Checking limitation
  2672. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2673. {
  2674. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2675. (((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod == 0) && (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit != 0)) || (ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod > 0)))
  2676. {
  2677. ShmCharger->gun_info[gun_index].targetCurrent = (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit,"W")==PASS?ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit/220:ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].Limit);
  2678. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2679. }
  2680. else
  2681. break;
  2682. }
  2683. }
  2684. }
  2685. }
  2686. // Determine max charging current to MCU
  2687. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  2688. {
  2689. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  2690. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2691. {
  2692. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  2693. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2694. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float)((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
  2695. }
  2696. }
  2697. else
  2698. {
  2699. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2700. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2701. {
  2702. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2703. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2704. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float)((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2705. }
  2706. }
  2707. // Debug information
  2708. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2709. {
  2710. DEBUG_INFO("=======================================================================\r\n");
  2711. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \r\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  2712. DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\r\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
  2713. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\r\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  2714. DEBUG_INFO("=======================================================================\r\n");
  2715. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2716. }
  2717. if(ShmOCPP16Data->OcppConnStatus)
  2718. {
  2719. // On-line max condition check
  2720. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  2721. {
  2722. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2723. setRelay(gun_index, OFF);
  2724. DEBUG_INFO("Connector-%d charging duration(%d) already over max duration(%d) in second.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  2725. }
  2726. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  2727. {
  2728. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2729. setRelay(gun_index, OFF);
  2730. DEBUG_INFO("Connector-%d charging energy(%.2f) already over max energy(%.2f) in KWH.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy));
  2731. }
  2732. else
  2733. {
  2734. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  2735. setRelay(gun_index, ON);
  2736. }
  2737. }
  2738. else
  2739. {
  2740. // Off-line max condition check
  2741. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2742. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  2743. {
  2744. if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))))
  2745. {
  2746. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2747. setRelay(gun_index, OFF);
  2748. DEBUG_INFO("Connector-%d charging duration(%d) already over off-line max duration(%d) in second.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration, (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60));
  2749. }
  2750. else if(((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))))
  2751. {
  2752. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2753. setRelay(gun_index, OFF);
  2754. DEBUG_INFO("Connector-%d charging energy(%.2f) already over off-line max energy(%.2f) in KWH.\r\n", gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy, ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy));
  2755. }
  2756. else
  2757. {
  2758. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  2759. setRelay(gun_index, ON);
  2760. }
  2761. }
  2762. else
  2763. {
  2764. setRelay(gun_index, OFF);
  2765. DEBUG_INFO("Connector-%d can not charging in off line\r\n", gun_index);
  2766. }
  2767. }
  2768. }
  2769. break;
  2770. default:
  2771. break;
  2772. }
  2773. break;
  2774. case SYS_MODE_TERMINATING:
  2775. if(isModeChange(gun_index))
  2776. {
  2777. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  2778. {
  2779. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2780. }
  2781. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  2782. {
  2783. setLedMotion(gun_index, LED_ACTION_STOP);
  2784. }
  2785. else
  2786. {}
  2787. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  2788. }
  2789. switch(ShmCharger->gun_info[gun_index].chargingMode)
  2790. {
  2791. case CHARGING_MODE_BS:
  2792. // If RFID SN different with start user, it need to authorize ID
  2793. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  2794. {
  2795. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ShmOCPP16Data->SpMsg.bits.AuthorizeReq)
  2796. {
  2797. // Request authorization
  2798. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  2799. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2800. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2801. {
  2802. // Big endian
  2803. switch(rfid.snType)
  2804. {
  2805. case RFID_SN_TYPE_6BYTE:
  2806. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  2807. break;
  2808. case RFID_SN_TYPE_7BYTE:
  2809. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  2810. break;
  2811. case RFID_SN_TYPE_10BYTE:
  2812. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  2813. break;
  2814. case RFID_SN_TYPE_4BYTE:
  2815. default:
  2816. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2817. break;
  2818. }
  2819. }
  2820. else
  2821. {
  2822. // Little endian
  2823. switch(rfid.snType)
  2824. {
  2825. case RFID_SN_TYPE_6BYTE:
  2826. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2827. break;
  2828. case RFID_SN_TYPE_7BYTE:
  2829. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2830. break;
  2831. case RFID_SN_TYPE_10BYTE:
  2832. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2833. break;
  2834. case RFID_SN_TYPE_4BYTE:
  2835. default:
  2836. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2837. break;
  2838. }
  2839. }
  2840. DEBUG_INFO("End request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2841. DEBUG_INFO("Start method : %d...\r\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  2842. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  2843. {
  2844. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  2845. {
  2846. case AUTH_MODE_ENABLE:
  2847. if(ShmOCPP16Data->OcppConnStatus)
  2848. {
  2849. // On line
  2850. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2851. }
  2852. else
  2853. {
  2854. // Off line
  2855. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  2856. {
  2857. case OFF_POLICY_LOCALLIST:
  2858. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2859. break;
  2860. case OFF_POLICY_PH_RFID:
  2861. break;
  2862. case OFF_POLICY_FREE:
  2863. break;
  2864. case OFF_POLICY_NOCHARGE:
  2865. default:
  2866. break;
  2867. }
  2868. }
  2869. break;
  2870. case AUTH_MODE_DISABLE:
  2871. default:
  2872. break;
  2873. }
  2874. }
  2875. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  2876. {
  2877. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2878. DEBUG_INFO("Need to match with start method : START_METHOD_BACKEND...\r\n");
  2879. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2880. }
  2881. else if (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE)
  2882. {
  2883. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2884. DEBUG_INFO("Need to match with start method : START_METHOD_BLE...\r\n");
  2885. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2886. }
  2887. else
  2888. {}
  2889. }
  2890. else
  2891. {
  2892. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf ||
  2893. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2894. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  2895. {
  2896. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
  2897. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2898. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  2899. {
  2900. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  2901. }
  2902. else
  2903. {
  2904. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2905. DEBUG_INFO("Authorize fail.\r\n");
  2906. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  2907. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  2908. sleep(3);
  2909. }
  2910. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  2911. }
  2912. }
  2913. }
  2914. // End authorize pass
  2915. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  2916. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  2917. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  2918. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON) ||
  2919. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)) ||
  2920. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  2921. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON))
  2922. {
  2923. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  2924. {
  2925. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  2926. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  2927. {
  2928. DEBUG_INFO("Authorize pass.\r\n");
  2929. setSpeaker(ON,SPEAKER_SHORT);
  2930. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  2931. sleep(3);
  2932. }
  2933. }
  2934. else
  2935. {}
  2936. setRelay(gun_index, OFF);
  2937. setRequest(gun_index, OFF);
  2938. setLedMotion(gun_index, LED_ACTION_STOP);
  2939. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  2940. {
  2941. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  2942. }
  2943. }
  2944. else
  2945. {
  2946. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  2947. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  2948. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  2949. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != ON) &&
  2950. !ShmOCPP16Data->MsMsg.bits.ResetReq &&
  2951. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  2952. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  2953. !(!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))) &&
  2954. !(!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))))
  2955. {
  2956. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2957. }
  2958. }
  2959. break;
  2960. case CHARGING_MODE_HLC:
  2961. // If RFID SN different with start user, it need to authorize ID
  2962. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  2963. {
  2964. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ShmOCPP16Data->SpMsg.bits.AuthorizeReq)
  2965. {
  2966. // Request authorization
  2967. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  2968. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2969. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2970. {
  2971. // Big endian
  2972. switch(rfid.snType)
  2973. {
  2974. case RFID_SN_TYPE_6BYTE:
  2975. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  2976. break;
  2977. case RFID_SN_TYPE_7BYTE:
  2978. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  2979. break;
  2980. case RFID_SN_TYPE_10BYTE:
  2981. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  2982. break;
  2983. case RFID_SN_TYPE_4BYTE:
  2984. default:
  2985. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2986. break;
  2987. }
  2988. }
  2989. else
  2990. {
  2991. // Little endian
  2992. switch(rfid.snType)
  2993. {
  2994. case RFID_SN_TYPE_6BYTE:
  2995. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2996. break;
  2997. case RFID_SN_TYPE_7BYTE:
  2998. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2999. break;
  3000. case RFID_SN_TYPE_10BYTE:
  3001. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3002. break;
  3003. case RFID_SN_TYPE_4BYTE:
  3004. default:
  3005. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3006. break;
  3007. }
  3008. }
  3009. DEBUG_INFO("End request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3010. DEBUG_INFO("Start method : %d...\r\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  3011. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  3012. {
  3013. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3014. {
  3015. case AUTH_MODE_ENABLE:
  3016. if(ShmOCPP16Data->OcppConnStatus)
  3017. {
  3018. // On line
  3019. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  3020. }
  3021. else
  3022. {
  3023. // Off line
  3024. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3025. {
  3026. case OFF_POLICY_LOCALLIST:
  3027. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  3028. break;
  3029. case OFF_POLICY_PH_RFID:
  3030. break;
  3031. case OFF_POLICY_FREE:
  3032. break;
  3033. case OFF_POLICY_NOCHARGE:
  3034. default:
  3035. break;
  3036. }
  3037. }
  3038. break;
  3039. case AUTH_MODE_DISABLE:
  3040. default:
  3041. break;
  3042. }
  3043. }
  3044. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  3045. {
  3046. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3047. DEBUG_INFO("Need to match with start method : START_METHOD_BACKEND...\r\n");
  3048. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3049. }
  3050. else if (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE)
  3051. {
  3052. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3053. DEBUG_INFO("Need to match with start method : START_METHOD_BLE...\r\n");
  3054. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3055. }
  3056. else
  3057. {}
  3058. }
  3059. else
  3060. {
  3061. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf ||
  3062. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3063. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3064. {
  3065. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
  3066. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3067. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3068. {
  3069. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  3070. }
  3071. else
  3072. {
  3073. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3074. DEBUG_INFO("Authorize fail.\r\n");
  3075. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3076. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3077. sleep(3);
  3078. }
  3079. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  3080. }
  3081. }
  3082. }
  3083. // End authorize pass
  3084. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  3085. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  3086. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  3087. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON) ||
  3088. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)) ||
  3089. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  3090. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON) ||
  3091. (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE))
  3092. {
  3093. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  3094. {
  3095. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  3096. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  3097. {
  3098. DEBUG_INFO("Authorize pass.\r\n");
  3099. setSpeaker(ON,SPEAKER_SHORT);
  3100. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3101. sleep(3);
  3102. }
  3103. }
  3104. else
  3105. {}
  3106. setRelay(gun_index, OFF);
  3107. setLedMotion(gun_index, LED_ACTION_STOP);
  3108. #ifdef CCS_SIMULATION_DATA
  3109. //for CCS testing
  3110. setRequest(gun_index, OFF);
  3111. #endif //CCS_SIMULATION_DATA
  3112. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  3113. {
  3114. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  3115. //Cancel CCS task negotiating
  3116. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  3117. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 1;
  3118. setRelay(gun_index,OFF);
  3119. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  3120. #ifdef CCS_SIMULATION_DATA
  3121. ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty = CCS_PWM_DUTY_100;
  3122. #endif //CCS_SIMULATION_DATA
  3123. }
  3124. }
  3125. else
  3126. {
  3127. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  3128. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  3129. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  3130. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != ON) &&
  3131. !ShmOCPP16Data->MsMsg.bits.ResetReq &&
  3132. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  3133. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  3134. !(!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeDuration*60)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))) &&
  3135. !(!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE) && ((ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy > 0) ? (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.OfflineMaxChargeEnergy)) : (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))) &&
  3136. (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress != HLC_STOP_MODE))
  3137. {
  3138. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3139. }
  3140. }
  3141. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100)
  3142. {
  3143. DEBUG_INFO("Set PWM duty 100% go to SYS_MODE_TERMINATING.\r\n");
  3144. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  3145. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3146. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  3147. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  3148. }
  3149. break;
  3150. default:
  3151. break;
  3152. }
  3153. break;
  3154. case SYS_MODE_COMPLETE:
  3155. if(isModeChange(gun_index))
  3156. {
  3157. setLedMotion(gun_index, LED_ACTION_STOP);
  3158. }
  3159. switch(ShmCharger->gun_info[gun_index].chargingMode)
  3160. {
  3161. case CHARGING_MODE_BS:
  3162. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3163. {
  3164. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  3165. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  3166. else
  3167. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  3168. }
  3169. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  3170. {
  3171. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  3172. }
  3173. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3174. {
  3175. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  3176. }
  3177. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3178. {
  3179. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  3180. }
  3181. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3182. {
  3183. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  3184. }
  3185. else
  3186. {
  3187. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  3188. }
  3189. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\r\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3190. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3191. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
  3192. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3193. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3194. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  3195. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  3196. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  3197. DB_Insert_Record(localDb, gun_index);
  3198. setChargerMode(gun_index, SYS_MODE_IDLE);
  3199. break;
  3200. case CHARGING_MODE_HLC:
  3201. setRelay(gun_index, OFF);
  3202. setRequest(gun_index, OFF);
  3203. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3204. {
  3205. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  3206. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  3207. else
  3208. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  3209. }
  3210. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  3211. {
  3212. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  3213. }
  3214. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3215. {
  3216. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  3217. }
  3218. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3219. {
  3220. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  3221. }
  3222. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3223. {
  3224. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  3225. }
  3226. else
  3227. {
  3228. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  3229. }
  3230. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\r\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3231. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3232. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100);
  3233. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3234. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3235. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  3236. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  3237. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  3238. DB_Insert_Record(localDb, gun_index);
  3239. setChargerMode(gun_index, SYS_MODE_IDLE);
  3240. break;
  3241. default:
  3242. break;
  3243. }
  3244. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  3245. break;
  3246. case SYS_MODE_ALARM:
  3247. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  3248. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  3249. {
  3250. }
  3251. else
  3252. {
  3253. setRelay(gun_index, OFF);
  3254. setRequest(gun_index, OFF);
  3255. }
  3256. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  3257. {
  3258. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  3259. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  3260. {
  3261. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  3262. }
  3263. else
  3264. {
  3265. setChargerMode(gun_index, SYS_MODE_IDLE);
  3266. }
  3267. }
  3268. setLedMotion(gun_index,LED_ACTION_ALARM);
  3269. break;
  3270. case SYS_MODE_FAULT:
  3271. if(isModeChange(gun_index))
  3272. {}
  3273. break;
  3274. case SYS_MODE_MAINTAIN:
  3275. if(isModeChange(gun_index))
  3276. {
  3277. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3278. }
  3279. break;
  3280. case SYS_MODE_UPDATE:
  3281. if(isModeChange(gun_index))
  3282. {
  3283. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3284. }
  3285. //======================================
  3286. // Check local upgrade firmware request
  3287. //======================================
  3288. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  3289. {
  3290. DEBUG_INFO("Firmware local upgrading...\r\n");
  3291. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  3292. int value = upgrade_check();
  3293. DEBUG_INFO("Local update Value: %d...\r\n",value);
  3294. if(value == PASS)
  3295. {
  3296. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3297. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3298. ShmCharger->isUpdateSuccess = YES;
  3299. DEBUG_INFO("Local update success...\r\n");
  3300. }
  3301. else
  3302. {
  3303. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3304. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3305. ShmCharger->isUpdateSuccess = NO;
  3306. DEBUG_INFO("Local update unsuccess...\r\n");
  3307. }
  3308. }
  3309. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq)
  3310. {
  3311. //======================================
  3312. // Check remote upgrade firmware request
  3313. //======================================
  3314. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  3315. {
  3316. DEBUG_INFO("Firmware remote upgraded fail...\r\n");
  3317. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3318. ShmCharger->isUpdateSuccess = NO;
  3319. }
  3320. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  3321. {
  3322. DEBUG_INFO("Firmware remote upgrading...\r\n");
  3323. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3324. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3325. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3326. int result = upgrade_check();
  3327. DEBUG_INFO("Remote update Result: %d...\r\n",result);
  3328. if(result == PASS)
  3329. {
  3330. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3331. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3332. ShmCharger->isUpdateSuccess = YES;
  3333. DEBUG_INFO("Remote update success...\r\n");
  3334. }
  3335. else
  3336. {
  3337. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3338. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3339. ShmCharger->isUpdateSuccess = NO;
  3340. DEBUG_INFO("Remote update unsuccess...\r\n");
  3341. }
  3342. }
  3343. else
  3344. {}
  3345. }
  3346. else
  3347. {
  3348. //======================================
  3349. // Upgrade complete reboot system
  3350. //======================================
  3351. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  3352. {
  3353. if(ShmCharger->isUpdateSuccess == YES)
  3354. {
  3355. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3356. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3357. DEBUG_WARN("Firmware upgrade success.\r\n");
  3358. sleep(5);
  3359. DEBUG_INFO("Firmware upgraded, reboot...\r\n");
  3360. system("reboot -f");
  3361. sleep(5);
  3362. system("reboot -f");
  3363. }
  3364. else
  3365. {
  3366. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  3367. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3368. DEBUG_WARN("Firmware upgrade fail.\r\n");
  3369. close(wtdFd);
  3370. system("/usr/bin/run_evse_restart.sh");
  3371. }
  3372. }
  3373. }
  3374. break;
  3375. case SYS_MODE_RESERVATION:
  3376. if(isModeChange(gun_index))
  3377. {
  3378. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3379. }
  3380. if(isReservationExpired(gun_index))
  3381. {
  3382. DEBUG_INFO("Reservation: Time's up...\r\n");
  3383. setChargerMode(gun_index, SYS_MODE_IDLE);
  3384. }
  3385. else if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq)
  3386. {
  3387. DEBUG_INFO("Reservation: Cancel reservation...\r\n");
  3388. setChargerMode(gun_index, SYS_MODE_IDLE);
  3389. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = OFF;
  3390. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  3391. }
  3392. else
  3393. {
  3394. // Check is there RFID or back end request start
  3395. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3396. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == ON))
  3397. {
  3398. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  3399. {
  3400. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3401. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3402. {
  3403. // Big endian
  3404. switch(rfid.snType)
  3405. {
  3406. case RFID_SN_TYPE_6BYTE:
  3407. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5]);
  3408. break;
  3409. case RFID_SN_TYPE_7BYTE:
  3410. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6]);
  3411. break;
  3412. case RFID_SN_TYPE_10BYTE:
  3413. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3], rfid.currentCard[4], rfid.currentCard[5], rfid.currentCard[6], rfid.currentCard[7], rfid.currentCard[8], rfid.currentCard[9]);
  3414. break;
  3415. case RFID_SN_TYPE_4BYTE:
  3416. default:
  3417. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3418. break;
  3419. }
  3420. }
  3421. else
  3422. {
  3423. // Little endian
  3424. switch(rfid.snType)
  3425. {
  3426. case RFID_SN_TYPE_6BYTE:
  3427. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X", rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3428. break;
  3429. case RFID_SN_TYPE_7BYTE:
  3430. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3431. break;
  3432. case RFID_SN_TYPE_10BYTE:
  3433. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X", rfid.currentCard[9], rfid.currentCard[8], rfid.currentCard[7], rfid.currentCard[6], rfid.currentCard[5], rfid.currentCard[4], rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3434. break;
  3435. case RFID_SN_TYPE_4BYTE:
  3436. default:
  3437. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3438. break;
  3439. }
  3440. }
  3441. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  3442. {
  3443. DEBUG_INFO("Start Method in reservation : RFID...\r\n");
  3444. DEBUG_INFO("Start request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3445. DEBUG_INFO("Reservation User Id : %s\r\n", ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  3446. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  3447. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3448. }
  3449. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3450. }
  3451. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq)
  3452. {
  3453. if(strcmp((char*)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  3454. {
  3455. DEBUG_INFO("Start Method in reservation: BACKEND...\r\n");
  3456. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  3457. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  3458. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3459. }
  3460. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  3461. }
  3462. }
  3463. }
  3464. break;
  3465. case SYS_MODE_BOOKING:
  3466. if(isModeChange(gun_index))
  3467. {}
  3468. break;
  3469. case SYS_MODE_DEBUG:
  3470. if(isModeChange(gun_index))
  3471. {
  3472. setLedMotion(gun_index,LED_ACTION_DEBUG);
  3473. }
  3474. break;
  3475. }
  3476. }
  3477. // System watch dog reset
  3478. write(wtdFd, "a", 1);
  3479. usleep(50000);
  3480. }
  3481. return FAIL;
  3482. }