main.c 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951
  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 -n \"[%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 -n \"[%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. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  826. cnt_pingDNS_Fail = 0;
  827. }
  828. else
  829. {
  830. if(++cnt_pingDNS_Fail > 3)
  831. {
  832. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  833. }
  834. }
  835. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet &&
  836. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi &&
  837. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  838. {
  839. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  840. }
  841. else
  842. {
  843. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  844. }
  845. sleep(5);
  846. }
  847. }
  848. DEBUG_INFO("Initial Ethernet OK\r\n");
  849. }
  850. int SpawnTask()
  851. {
  852. system ("pkill Module_4g");
  853. system ("pkill Module_Wifi");
  854. system ("pkill Module_EventLogging");
  855. system ("pkill OcppBackend");
  856. system ("pkill Module_AlarmDetect");
  857. system ("pkill Module_InternalComm");
  858. system ("pkill Module_Speaker");
  859. system ("pkill Module_ProduceUtils");
  860. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  861. {
  862. system("/root/Module_4g &");
  863. }
  864. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  865. {
  866. system("/root/Module_Wifi &");
  867. }
  868. system("/root/Module_EventLogging &");
  869. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  870. {
  871. system("/root/OcppBackend &");
  872. }
  873. else
  874. {
  875. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\r\n");
  876. system("/root/OcppBackend &");
  877. }
  878. system ("/root/Module_AlarmDetect &");
  879. system ("/root/Module_InternalComm &");
  880. system ("/root/Module_Speaker &");
  881. system ("/root/Module_ProduceUtils &");
  882. return PASS;
  883. }
  884. int Initialization()
  885. {
  886. int result = PASS;
  887. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  888. InitGPIO();
  889. InitEthernet();
  890. if(DB_Open(localDb) != PASS)
  891. result = FAIL;
  892. for(int gun_index=0;gun_index< AC_QUANTITY;gun_index++)
  893. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  894. rfidFd = InitRfidPort();
  895. wtdFd = InitWatchDog();
  896. if((wtdFd < 0) || (rfidFd < 0))
  897. result = FAIL;
  898. if(result == PASS)
  899. DEBUG_INFO("Initialization OK.\r\n");
  900. else
  901. DEBUG_INFO("Initialization Fail.\r\n");
  902. return result;
  903. }
  904. //=====================================================
  905. // Common routine
  906. //=====================================================
  907. char* getSystemModeName(unsigned char mode)
  908. {
  909. char* result;
  910. switch(mode)
  911. {
  912. case SYS_MODE_BOOTING:
  913. result = "booting";
  914. break;
  915. case SYS_MODE_IDLE:
  916. result = "idle";
  917. break;
  918. case SYS_MODE_AUTHORIZING:
  919. result = "authorizing";
  920. break;
  921. case SYS_MODE_PREPARING:
  922. result = "preparing";
  923. break;
  924. case SYS_MODE_CHARGING:
  925. result = "charging";
  926. break;
  927. case SYS_MODE_TERMINATING:
  928. result = "terminating";
  929. break;
  930. case SYS_MODE_COMPLETE:
  931. result = "complete";
  932. break;
  933. case SYS_MODE_ALARM:
  934. result = "alarm";
  935. break;
  936. case SYS_MODE_FAULT:
  937. result = "fault";
  938. break;
  939. case SYS_MODE_MAINTAIN:
  940. result = "maintain";
  941. break;
  942. case SYS_MODE_RESERVATION:
  943. result = "reservation";
  944. break;
  945. case SYS_MODE_BOOKING:
  946. result = "booking";
  947. break;
  948. case SYS_MODE_DEBUG:
  949. result = "debug";
  950. break;
  951. case SYS_MODE_UPDATE:
  952. result = "upgrade";
  953. break;
  954. default:
  955. result = "unknown";
  956. break;
  957. }
  958. return result;
  959. }
  960. void setChargerMode(unsigned char gun_index, unsigned char mode)
  961. {
  962. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  963. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  964. 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));
  965. }
  966. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  967. {
  968. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  969. }
  970. unsigned char isModeChange(unsigned char gun_index)
  971. {
  972. unsigned char result = NO;
  973. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  974. {
  975. result = YES;
  976. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  977. }
  978. return result;
  979. }
  980. //===============================================
  981. // Get firmware version
  982. //===============================================
  983. void get_firmware_version(unsigned char gun_index)
  984. {
  985. FILE *fp;
  986. char cmd[512];
  987. char buf[512];
  988. // Get CSU hardware version
  989. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  990. // Get CSU boot loader version
  991. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  992. // Get CSU kernel version
  993. sprintf(cmd, "/bin/uname -r");
  994. fp = popen(cmd, "r");
  995. if(fp == NULL)
  996. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  997. else
  998. {
  999. while(fgets(buf, sizeof(buf), fp) != NULL)
  1000. {
  1001. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf);
  1002. }
  1003. }
  1004. // Get MCU firmware version
  1005. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1006. // Get CSU root file system version
  1007. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "D0.23.00.0000.00");
  1008. // Get AC connector type from model name
  1009. for(uint8_t idx=0;idx<3;idx++)
  1010. {
  1011. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1012. {
  1013. case '1':
  1014. // J1772 Plug
  1015. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  1016. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  1017. break;
  1018. case '2':
  1019. // J1772 Socket
  1020. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  1021. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  1022. break;
  1023. case '3':
  1024. // CE Plug
  1025. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  1026. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  1027. break;
  1028. case '4':
  1029. // CE Socket
  1030. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  1031. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  1032. break;
  1033. case '5':
  1034. // GB Plug
  1035. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  1036. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  1037. break;
  1038. case '6':
  1039. // GB Socket
  1040. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  1041. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  1042. break;
  1043. }
  1044. }
  1045. // Get network option from model name
  1046. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1047. {
  1048. case 'B':
  1049. case 'U':
  1050. //Blue tooth
  1051. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1052. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  1053. break;
  1054. case 'W':
  1055. // WIFI
  1056. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1057. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  1058. break;
  1059. case 'T':
  1060. // 3G/4G
  1061. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1062. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  1063. break;
  1064. default:
  1065. // LAN
  1066. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1067. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  1068. break;
  1069. }
  1070. // Get rating power from model name
  1071. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1072. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1073. // Get vender code from model name
  1074. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1075. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1076. DEBUG_INFO("====================================================================\r\n");
  1077. DEBUG_INFO("Model: %s\r\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  1078. DEBUG_INFO("CSU hardware version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  1079. DEBUG_INFO("CSU boot loader version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  1080. DEBUG_INFO("CSU kernel version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  1081. DEBUG_INFO("CSU root file system version: %s\r\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  1082. DEBUG_INFO("CSU MCU-%2d firmware version: %s\r\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1083. DEBUG_INFO("====================================================================\r\n");
  1084. }
  1085. //===============================================
  1086. // Upgrade firmware
  1087. //===============================================
  1088. int upgrade_check()
  1089. {
  1090. int result = PASS;
  1091. int fd;
  1092. DIR *dir;
  1093. struct dirent *file;
  1094. char cmd[512];
  1095. long int MaxLen=48*1024*1024;
  1096. if ((dir = opendir ("/mnt")) != NULL)
  1097. {
  1098. /* print all the files and directories within directory */
  1099. while ((file = readdir (dir)) != NULL)
  1100. {
  1101. if(strlen(file->d_name)>2)
  1102. {
  1103. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  1104. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  1105. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  1106. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  1107. {
  1108. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  1109. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  1110. read(fd, ptr, MaxLen);
  1111. close(fd);
  1112. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  1113. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  1114. DEBUG_INFO("New firmware type: %X\r\n", ShmCharger->fwUpgradeInfo.fwType);
  1115. DEBUG_INFO("New firmware model name: %s, %s\r\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  1116. if((strcmp(ShmCharger->fwUpgradeInfo.modelName, (char*)ShmSysConfigAndInfo->SysConfig.ModelName)==0) && (ShmCharger->fwUpgradeInfo.fwType>0))
  1117. {
  1118. switch(ShmCharger->fwUpgradeInfo.fwType)
  1119. {
  1120. case CSU_BOOTLOADER:
  1121. case CSU_KERNEL_CONFIGURATION:
  1122. case CSU_KERNEL_IMAGE:
  1123. case CSU_ROOT_FILE_SYSTEM:
  1124. case CSU_USER_CONFIGURATION:
  1125. case CSU_PRIMARY_CONTROLLER:
  1126. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  1127. {
  1128. result = FAIL;
  1129. }
  1130. else
  1131. {
  1132. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  1133. {
  1134. DEBUG_INFO("Restore model name & serial number.\r\n");
  1135. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  1136. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1137. {
  1138. DEBUG_INFO("Re-load configuration fail.\r\n");
  1139. result = FAIL;
  1140. }
  1141. else
  1142. {
  1143. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  1144. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  1145. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  1146. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1147. {
  1148. DEBUG_INFO("Re-write configuration fail.\r\n");
  1149. result = FAIL;
  1150. }
  1151. else
  1152. DEBUG_INFO("Re-write configuration OK.\r\n");
  1153. }
  1154. }
  1155. }
  1156. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1157. system(cmd);
  1158. break;
  1159. case AC_WALLMOUNT_CONTROLLER:
  1160. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1161. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  1162. break;
  1163. default:
  1164. result = FAIL;
  1165. DEBUG_WARN("Image file is unknown type.\r\n");
  1166. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1167. system(cmd);
  1168. break;
  1169. }
  1170. }
  1171. else
  1172. {
  1173. result = FAIL;
  1174. DEBUG_ERROR("Model name and Firmware type error.\r\n");
  1175. }
  1176. free(ptr);
  1177. }
  1178. else
  1179. {
  1180. result = FAIL;
  1181. DEBUG_ERROR("New firmware open error.\r\n");
  1182. }
  1183. }
  1184. else
  1185. {
  1186. if(strlen(file->d_name) >= 3)
  1187. {
  1188. result = FAIL;
  1189. DEBUG_ERROR("File name error.\r\n");
  1190. }
  1191. else
  1192. {
  1193. DEBUG_ERROR("Searching file.\r\n");
  1194. }
  1195. }
  1196. }
  1197. closedir (dir);
  1198. }
  1199. else
  1200. {
  1201. result = FAIL;
  1202. DEBUG_ERROR("/mnt does not valid.\r\n");
  1203. }
  1204. return result;
  1205. }
  1206. //===============================================
  1207. // Check RFID is match with start user
  1208. //===============================================
  1209. int isMatchStartUser(unsigned char gun_index)
  1210. {
  1211. uint8_t tmpUser[32];
  1212. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  1213. {
  1214. // Big endian
  1215. switch(rfid.snType)
  1216. {
  1217. case RFID_SN_TYPE_6BYTE:
  1218. 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]);
  1219. break;
  1220. case RFID_SN_TYPE_7BYTE:
  1221. 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]);
  1222. break;
  1223. case RFID_SN_TYPE_10BYTE:
  1224. 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]);
  1225. break;
  1226. case RFID_SN_TYPE_4BYTE:
  1227. default:
  1228. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  1229. break;
  1230. }
  1231. }
  1232. else
  1233. {
  1234. // Little endian
  1235. switch(rfid.snType)
  1236. {
  1237. case RFID_SN_TYPE_6BYTE:
  1238. 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]);
  1239. break;
  1240. case RFID_SN_TYPE_7BYTE:
  1241. 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]);
  1242. break;
  1243. case RFID_SN_TYPE_10BYTE:
  1244. 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]);
  1245. break;
  1246. case RFID_SN_TYPE_4BYTE:
  1247. default:
  1248. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  1249. break;
  1250. }
  1251. }
  1252. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  1253. }
  1254. //===============================================
  1255. // Read RFID Serial Number
  1256. //===============================================
  1257. int GetCardSerialNumber()
  1258. {
  1259. int isSuccess = FAIL;
  1260. int module_type = MODULE_EWT;
  1261. if(getRequestCardSN(rfidFd,module_type,&rfid))
  1262. {
  1263. if(rfid.cardType == ISO14443A)
  1264. {
  1265. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  1266. {
  1267. isSuccess = PASS;
  1268. }
  1269. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  1270. {
  1271. isSuccess = PASS;
  1272. }
  1273. sethaltCard(rfidFd,module_type);
  1274. }
  1275. else if(rfid.cardType == IS014443B)
  1276. {
  1277. isSuccess = PASS;
  1278. }
  1279. else if(rfid.cardType == FELICA)
  1280. {
  1281. isSuccess = PASS;
  1282. }
  1283. else
  1284. {}
  1285. }
  1286. else
  1287. {}
  1288. return isSuccess;
  1289. }
  1290. //===============================================
  1291. // Set led motion
  1292. //===============================================
  1293. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  1294. {
  1295. switch(led_mode)
  1296. {
  1297. case LED_ACTION_INIT:
  1298. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  1299. break;
  1300. case LED_ACTION_IDLE:
  1301. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  1302. break;
  1303. case LED_ACTION_AUTHED:
  1304. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  1305. break;
  1306. case LED_ACTION_CONNECTED:
  1307. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  1308. break;
  1309. case LED_ACTION_CHARGING:
  1310. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  1311. break;
  1312. case LED_ACTION_STOP:
  1313. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  1314. break;
  1315. case LED_ACTION_ALARM:
  1316. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  1317. break;
  1318. case LED_ACTION_MAINTAIN:
  1319. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  1320. break;
  1321. case LED_ACTION_RFID_PASS:
  1322. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  1323. break;
  1324. case LED_ACTION_RFID_FAIL:
  1325. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  1326. break;
  1327. case LED_ACTION_BLE_CONNECT:
  1328. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  1329. break;
  1330. case LED_ACTION_BLE_DISABLE:
  1331. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  1332. break;
  1333. case LED_ACTION_DEBUG:
  1334. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  1335. break;
  1336. case LED_ACTION_ALL_OFF:
  1337. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  1338. break;
  1339. case LED_RELAY_ON:
  1340. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  1341. break;
  1342. case LED_RELAY_OFF:
  1343. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  1344. break;
  1345. case LED_ACTION_HANDSHAKE_FAIL:
  1346. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  1347. break;
  1348. case LED_ACTION_INTERNET_DISCONNECT:
  1349. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  1350. break;
  1351. }
  1352. }
  1353. //===============================================
  1354. // Request on/off set
  1355. //===============================================
  1356. void setRequest(unsigned char gun_index,unsigned char isOn)
  1357. {
  1358. if(isOn == ON)
  1359. {
  1360. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  1361. {
  1362. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  1363. DEBUG_INFO("Gun-%d Output relay sts: ON \r\n",gun_index);
  1364. }
  1365. }
  1366. else
  1367. {
  1368. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  1369. {
  1370. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  1371. DEBUG_INFO("Gun-%d Output relay sts: OFF \r\n",gun_index);
  1372. }
  1373. }
  1374. }
  1375. //===============================================
  1376. // Request on/off get
  1377. //===============================================
  1378. int getRequest(unsigned char gun_index)
  1379. {
  1380. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  1381. }
  1382. //===============================================
  1383. // Relay on/off set
  1384. //===============================================
  1385. void setRelay(unsigned char gun_index,unsigned char isOn)
  1386. {
  1387. if(isOn == ON)
  1388. {
  1389. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == OFF)
  1390. {
  1391. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = ON;
  1392. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0x01;
  1393. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0x01;
  1394. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0x01;
  1395. DEBUG_INFO("Gun-%d Output relay sts: ON \r\n",gun_index);
  1396. }
  1397. }
  1398. else
  1399. {
  1400. if(ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn == ON)
  1401. {
  1402. ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn = OFF;
  1403. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][0] = 0;
  1404. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][1] = 0;
  1405. ShmCharger->gun_info[gun_index].primaryMcuState.relayState.relay_status[0][2] = 0;
  1406. DEBUG_INFO("Gun-%d Output relay sts: OFF \r\n",gun_index);
  1407. }
  1408. }
  1409. }
  1410. //===============================================
  1411. // Relay on/off get
  1412. //===============================================
  1413. int getRelay(unsigned char gun_index)
  1414. {
  1415. return ShmCharger->gun_info[gun_index].legacyRequest.isRelayOn;
  1416. }
  1417. //===============================================
  1418. // Set speaker on/off request
  1419. //===============================================
  1420. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  1421. {
  1422. if(isOn == ON)
  1423. {
  1424. ShmCharger->isSpeakerOn = ON;
  1425. ShmCharger->speaker_type = speaker_mode;
  1426. }
  1427. }
  1428. //===============================================
  1429. // Initialization RFID communication port
  1430. //===============================================
  1431. int InitRfidPort()
  1432. {
  1433. int uartO2 = open(rfidPortName, O_RDWR);
  1434. struct termios tios;
  1435. if (uartO2 != FAIL)
  1436. {
  1437. ioctl (uartO2, TCGETS, &tios);
  1438. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1439. tios.c_lflag = 0;
  1440. tios.c_iflag = 0;
  1441. tios.c_oflag = 0;
  1442. tios.c_cc[VMIN] = 0;
  1443. tios.c_cc[VTIME] = (unsigned char) 1;
  1444. tios.c_lflag = 0;
  1445. tcflush(uartO2, TCIFLUSH);
  1446. ioctl(uartO2, TCSETS, &tios);
  1447. }
  1448. if (uartO2 < 0)
  1449. {
  1450. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1451. }
  1452. return uartO2;
  1453. }
  1454. //===============================================
  1455. // Check internet access status
  1456. //===============================================
  1457. int isReachableInternet()
  1458. {
  1459. int result = FAIL;
  1460. FILE *fp;
  1461. char cmd[256];
  1462. char buf[512];
  1463. //char tmp[512];
  1464. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1465. {
  1466. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1467. fp = popen(cmd, "r");
  1468. if(fp != NULL)
  1469. {
  1470. while(fgets(buf, sizeof(buf), fp) != NULL)
  1471. {
  1472. if(strstr(buf, "transmitted") > 0)
  1473. {
  1474. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1475. if(strstr(buf,"100%") != NULL)
  1476. {
  1477. }
  1478. else
  1479. {
  1480. result = PASS;
  1481. }
  1482. //DEBUG_INFO("%s",buf);
  1483. //DEBUG_INFO("%s\n",tmp);
  1484. }
  1485. }
  1486. }
  1487. pclose(fp);
  1488. }
  1489. return result;
  1490. }
  1491. //===============================================
  1492. // Check reservation date is expired
  1493. //===============================================
  1494. int isReservationExpired(unsigned char gun_index)
  1495. {
  1496. int result = NO;
  1497. struct tm expiredDate;
  1498. struct timeb expiredTime;
  1499. 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)
  1500. {
  1501. expiredDate.tm_year -= 1900;
  1502. expiredDate.tm_mon -= 1;
  1503. expiredTime.time = mktime(&expiredDate);
  1504. if(DiffTimebWithNow(expiredTime)>=0)
  1505. {
  1506. result = YES;
  1507. }
  1508. }
  1509. else
  1510. {
  1511. DEBUG_WARN("Expired date parsing error.\r\n");
  1512. DEBUG_INFO("Date expired.\r\n");
  1513. result = YES;
  1514. }
  1515. return result;
  1516. }
  1517. //===============================================
  1518. // Check charging profile related date routine
  1519. //===============================================
  1520. int isProfileValid(uint8_t gun_index)
  1521. {
  1522. int result = NO;
  1523. struct tm tmFrom, tmTo;
  1524. struct timeb tbFrom, tbTo;
  1525. 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) &&
  1526. (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))
  1527. {
  1528. tmFrom.tm_year -= 1900;
  1529. tmFrom.tm_mon -= 1;
  1530. tbFrom.time = mktime(&tmFrom);
  1531. tmTo.tm_year -= 1900;
  1532. tmTo.tm_mon -= 1;
  1533. tbTo.time = mktime(&tmTo);
  1534. DEBUG_INFO("Valid from compare Now: %d\r\n", DiffTimebWithNow(tbFrom));
  1535. DEBUG_INFO("Valid to compare Now: %d\r\n", DiffTimebWithNow(tbTo));
  1536. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  1537. {
  1538. result = YES;
  1539. }
  1540. }
  1541. else
  1542. {
  1543. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\r\n");
  1544. }
  1545. return result;
  1546. }
  1547. int getScheduleStart(int gun_index)
  1548. {
  1549. int result = -1;
  1550. struct tm tmScheduleStart;;
  1551. struct timeb tbScheduleStart;
  1552. 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))
  1553. {
  1554. tmScheduleStart.tm_year -= 1900;
  1555. tmScheduleStart.tm_mon -= 1;
  1556. tbScheduleStart.time = mktime(&tmScheduleStart);
  1557. tbScheduleStart.millitm = 0;
  1558. result = DiffTimebWithNow(tbScheduleStart)/1000;
  1559. DEBUG_INFO("Schedule start compare Now(seconds): %d\r\n", result);
  1560. }
  1561. else
  1562. {
  1563. DEBUG_WARN("Schedule start date parsing error.\r\n");
  1564. }
  1565. return result;
  1566. }
  1567. int getStartSinceToday()
  1568. {
  1569. int result = -1;
  1570. time_t t;
  1571. struct tm *tmStartToday;
  1572. struct timeb tbStartToday;
  1573. t=time(NULL);
  1574. tmStartToday=localtime(&t);
  1575. tmStartToday->tm_hour = 0;
  1576. tmStartToday->tm_min = 0;
  1577. tmStartToday->tm_sec = 0;
  1578. tbStartToday.time = mktime(tmStartToday);
  1579. result = DiffTimebWithNow(tbStartToday)/1000;
  1580. DEBUG_INFO("Start today compare Now(seconds): %d\r\n", result);
  1581. return result;
  1582. }
  1583. int getStartSinceWeek()
  1584. {
  1585. int result = -1;
  1586. time_t t;
  1587. struct tm *tmStartWeek;
  1588. struct timeb tbStartWeek;
  1589. t=time(NULL);
  1590. tmStartWeek=localtime(&t);
  1591. t-=86400*tmStartWeek->tm_wday;
  1592. tmStartWeek=localtime(&t);
  1593. tmStartWeek->tm_hour = 0;
  1594. tmStartWeek->tm_min = 0;
  1595. tmStartWeek->tm_sec = 0;
  1596. tbStartWeek.time = mktime(tmStartWeek);
  1597. result = DiffTimebWithNow(tbStartWeek)/1000;
  1598. DEBUG_INFO("Start week compare Now(seconds): %d\r\n", result);
  1599. return result;
  1600. }
  1601. //===============================================
  1602. // Valid from local white list
  1603. //===============================================
  1604. int isValidLocalWhiteCard()
  1605. {
  1606. uint8_t result = FAIL;
  1607. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  1608. {
  1609. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  1610. {
  1611. result = PASS;
  1612. }
  1613. }
  1614. return result;
  1615. }
  1616. //==========================================
  1617. // Get Ethernet MAC address
  1618. //==========================================
  1619. int getEth0MacAddress()
  1620. {
  1621. int result = PASS;
  1622. FILE *fp;
  1623. char cmd[256];
  1624. char buf[512];
  1625. char tmp[512];
  1626. strcpy(cmd, "ifconfig");;
  1627. fp = popen(cmd, "r");
  1628. if(fp != NULL)
  1629. {
  1630. while(fgets(buf, sizeof(buf), fp) != NULL)
  1631. {
  1632. if(strstr(buf, "eth0") > 0)
  1633. {
  1634. result = PASS;
  1635. }
  1636. if(strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
  1637. {
  1638. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  1639. strcpy((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
  1640. }
  1641. }
  1642. }
  1643. pclose(fp);
  1644. return result;
  1645. }
  1646. //==========================================
  1647. // Check task processing
  1648. //==========================================
  1649. void checkTask()
  1650. {
  1651. if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T')
  1652. {
  1653. if(system("pidof -s Module_4g > /dev/null") != 0)
  1654. {
  1655. DEBUG_INFO("Module_4g not running, restart it.\r\n");
  1656. system("/root/Module_4g &");
  1657. }
  1658. }
  1659. else if(ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W')
  1660. {
  1661. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  1662. {
  1663. DEBUG_INFO("Module_Wifi not running, restart it.\r\n");
  1664. system("/root/Module_Wifi &");
  1665. }
  1666. }
  1667. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  1668. {
  1669. DEBUG_INFO("Module_EventLogging not running, restart it.\r\n");
  1670. system("/root/Module_EventLogging &");
  1671. }
  1672. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1673. {
  1674. DEBUG_INFO("OcppBackend not running, restart it.\r\n");
  1675. system("/root/OcppBackend &");
  1676. }
  1677. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  1678. {
  1679. DEBUG_INFO("Module_AlarmDetect not running, restart it.\r\n");
  1680. system("/root/Module_AlarmDetect &");
  1681. }
  1682. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  1683. {
  1684. DEBUG_INFO("Module_InternalComm not running, restart it.\r\n");
  1685. system("/root/Module_InternalComm &");
  1686. }
  1687. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  1688. {
  1689. DEBUG_INFO("Module_Speaker not running, restart it.\r\n");
  1690. system("/root/Module_Speaker &");
  1691. }
  1692. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  1693. {
  1694. DEBUG_INFO("Module_ProduceUtils not running, restart it.\r\n");
  1695. system ("/root/Module_ProduceUtils &");
  1696. }
  1697. }
  1698. //===============================================
  1699. // Check Connection Timeout
  1700. //===============================================
  1701. void checkConnectionTimeout()
  1702. {
  1703. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1704. {
  1705. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1706. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \r\n", TIMEOUT_SPEC_HANDSHAKING/1000);
  1707. }
  1708. else
  1709. {
  1710. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  1711. {
  1712. ShmCharger->timeoutSpec.Setting_Timeout_Spec = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  1713. if(ShmCharger->timeoutSpec.Setting_Timeout_Spec <= 0)
  1714. {
  1715. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1716. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\r\n.");
  1717. }
  1718. else
  1719. {
  1720. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  1721. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\r\n.");
  1722. }
  1723. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \r\n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  1724. }
  1725. else
  1726. {
  1727. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1728. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Table is blank...\r\n.");
  1729. }
  1730. //DEBUG_INFO("Present timeout spec : %d \r\n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  1731. }
  1732. }
  1733. //===============================================
  1734. // Main process
  1735. //===============================================
  1736. int main(void)
  1737. {
  1738. //Create all share memory
  1739. if(CreatShareMemory()==0)
  1740. {
  1741. DEBUG_ERROR("CreatShareMemory NG\r\n");
  1742. if(ShmStatusCodeData!=NULL)
  1743. {
  1744. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  1745. }
  1746. sleep(5);
  1747. system("reboot -f");
  1748. sleep(5);
  1749. system("reboot -f");
  1750. }
  1751. else
  1752. {
  1753. DEBUG_INFO("CreatShareMemory OK\r\n");
  1754. }
  1755. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1756. {
  1757. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  1758. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  1759. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  1760. ShmCharger->gun_info[gun_index].isInitialPass = NO;
  1761. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  1762. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  1763. }
  1764. // Main loop
  1765. for(;;)
  1766. {
  1767. //==========================================
  1768. // Synchronize share memory from OCPP struct
  1769. //==========================================
  1770. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ShmOCPP16Data->OcppConnStatus;
  1771. //==========================================
  1772. // Ethernet MAC address
  1773. //==========================================
  1774. getEth0MacAddress();
  1775. //==========================================
  1776. // Check task processing
  1777. //==========================================
  1778. checkTask();
  1779. //==========================================
  1780. // Check connection timeout specification
  1781. //==========================================
  1782. checkConnectionTimeout();
  1783. //==========================================
  1784. // Something need run in Idle mode
  1785. //==========================================
  1786. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM)) &&
  1787. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM)):true))
  1788. {
  1789. //======================================
  1790. // Check restore factory setting request
  1791. //======================================
  1792. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  1793. {
  1794. // Set led to initial
  1795. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1796. {
  1797. setLedMotion(gun_index,LED_ACTION_INIT);
  1798. }
  1799. system("cd /root;./Module_FactoryConfig -m");
  1800. system("sync");
  1801. sleep(5);
  1802. system("reboot -f");
  1803. sleep(5);
  1804. system("reboot -f");
  1805. }
  1806. //======================================
  1807. // Check upgrade firmware request
  1808. //======================================
  1809. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  1810. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq)
  1811. {
  1812. ShmCharger->isUpdateSuccess = NO;
  1813. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1814. {
  1815. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  1816. setChargerMode(gun_index, SYS_MODE_UPDATE);
  1817. }
  1818. }
  1819. }
  1820. //==========================================
  1821. // Check remote reset request
  1822. //==========================================
  1823. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  1824. {
  1825. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  1826. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):true))
  1827. {
  1828. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  1829. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  1830. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  1831. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  1832. {
  1833. system("sync");
  1834. sleep(5);
  1835. system("reboot -f");
  1836. sleep(5);
  1837. system("reboot -f");
  1838. }
  1839. else
  1840. {
  1841. sleep(5);
  1842. close(wtdFd);
  1843. system("/usr/bin/run_evse_restart.sh");
  1844. }
  1845. }
  1846. }
  1847. //==========================================
  1848. // Check RFID
  1849. //==========================================
  1850. if(!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq)
  1851. {
  1852. if(GetCardSerialNumber()!= FAIL)
  1853. {
  1854. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  1855. }
  1856. }
  1857. //==========================================
  1858. // Connector loop
  1859. //==========================================
  1860. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1861. {
  1862. // Synchronize current rating value from MCU
  1863. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1864. // Assign connector location index for OCPP
  1865. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  1866. // Synchronize present charging power
  1867. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  1868. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  1869. else
  1870. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  1871. // The system switch to Booting mode
  1872. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == 0XFF)
  1873. {
  1874. setChargerMode(gun_index, SYS_MODE_BOOTING);
  1875. }
  1876. // Check initialization "PASS" or "FAIL"
  1877. if(ShmCharger->gun_info[gun_index].isInitialPass == YES)
  1878. {
  1879. // Alarm event check
  1880. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  1881. {
  1882. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  1883. {
  1884. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE)
  1885. {
  1886. }
  1887. else
  1888. {
  1889. setChargerMode(gun_index, SYS_MODE_ALARM);
  1890. }
  1891. }
  1892. }
  1893. else
  1894. {
  1895. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM)
  1896. {
  1897. }
  1898. }
  1899. }
  1900. // Reservation request check
  1901. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  1902. {
  1903. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  1904. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  1905. {
  1906. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  1907. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  1908. }
  1909. DEBUG_INFO("Reservation request on connector-%d.\r\n", gun_index);
  1910. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  1911. }
  1912. // Change availability check
  1913. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  1914. {
  1915. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  1916. {
  1917. DB_Update_Operactive(localDb, gun_index, true);
  1918. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  1919. }
  1920. else
  1921. {
  1922. DB_Update_Operactive(localDb, gun_index, false);
  1923. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  1924. }
  1925. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  1926. }
  1927. if(ShmCharger->gun_info[gun_index].isOperactive)
  1928. {
  1929. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  1930. {
  1931. setChargerMode(gun_index, SYS_MODE_IDLE);
  1932. }
  1933. }
  1934. else
  1935. {
  1936. if(isMode(gun_index, SYS_MODE_IDLE))
  1937. {
  1938. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  1939. }
  1940. }
  1941. // Unlock Connector signal check
  1942. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  1943. {
  1944. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  1945. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  1946. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  1947. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  1948. }
  1949. // Connector process
  1950. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  1951. {
  1952. case SYS_MODE_BOOTING:
  1953. if(isModeChange(gun_index))
  1954. {
  1955. setLedMotion(gun_index,LED_ACTION_INIT);
  1956. //CSU Initialization & task spawn
  1957. if((Initialization() != PASS) ||
  1958. (SpawnTask() != PASS))
  1959. {
  1960. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  1961. }
  1962. }
  1963. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  1964. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  1965. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  1966. {
  1967. ShmCharger->gun_selectd = 0;
  1968. // Firmware version
  1969. get_firmware_version(gun_index);
  1970. // OCPP BootNotification info set
  1971. memcpy((char*)ShmOCPP16Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  1972. memcpy((char*)ShmOCPP16Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  1973. sprintf((char*)ShmOCPP16Data->BootNotification.CpFwVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  1974. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
  1975. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "AC");
  1976. DEBUG_INFO("==========================================\r\n");
  1977. DEBUG_INFO("System ID: %s\r\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  1978. DEBUG_INFO("==========================================\r\n");
  1979. DEBUG_INFO("========== OCPP BootNotification information ==========\r\n");
  1980. DEBUG_INFO("OCPP OcppServerURL: %s\r\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  1981. DEBUG_INFO("OCPP ChargeBoxId: %s\r\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  1982. DEBUG_INFO("OCPP ChargePointVendor: %s\r\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  1983. DEBUG_INFO("OCPP CpFwVersion: %s\r\n",ShmOCPP16Data->BootNotification.CpFwVersion);
  1984. DEBUG_INFO("OCPP CpMeterSerialNumber: %s\r\n",ShmOCPP16Data->BootNotification.CpMeterSerialNumber);
  1985. DEBUG_INFO("OCPP CpMeterType: %s\r\n",ShmOCPP16Data->BootNotification.CpMeterType);
  1986. DEBUG_INFO("=======================================================\r\n");
  1987. DEBUG_INFO("========== Set Wifi information ==========\r\n");
  1988. DEBUG_INFO("Wifi mode: %d\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  1989. DEBUG_INFO("Wifi SSID: %s\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  1990. DEBUG_INFO("Wifi password: %s\r\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  1991. DEBUG_INFO("==========================================\r\n");
  1992. // Flag for initialization system success
  1993. ShmCharger->gun_info[gun_index].isInitialPass = YES;
  1994. // Set max current to rating current
  1995. //ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1996. ShmCharger->gun_info[gun_index].acCcsInfo.EVSEMaxCurrent = (float)ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  1997. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  1998. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  1999. // If rotate switch equal zero, the system needs to change Debug mode
  2000. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  2001. setChargerMode(gun_index, SYS_MODE_DEBUG);
  2002. else
  2003. setChargerMode(gun_index, SYS_MODE_IDLE);
  2004. ShmSysConfigAndInfo->SysConfig.AcPhaseCount = AC_INPUT_PHASE;
  2005. }
  2006. break;
  2007. case SYS_MODE_IDLE:
  2008. if(isModeChange(gun_index))
  2009. {
  2010. setLedMotion(gun_index,LED_ACTION_IDLE);
  2011. setRelay(gun_index, OFF);
  2012. setRequest(gun_index,OFF);
  2013. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  2014. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2015. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  2016. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  2017. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  2018. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  2019. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  2020. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  2021. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2022. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  2023. //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;
  2024. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  2025. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  2026. // Response StopTransactionConf
  2027. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf == ON)
  2028. {
  2029. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = OFF;
  2030. }
  2031. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5;
  2032. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  2033. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  2034. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2035. }
  2036. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  2037. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  2038. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  2039. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  2040. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == ON))
  2041. {
  2042. // Clean User id & Card Number
  2043. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2044. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  2045. {
  2046. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  2047. DEBUG_INFO("Start Method : RFID...\r\n");
  2048. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2049. {
  2050. // Big endian
  2051. switch(rfid.snType)
  2052. {
  2053. case RFID_SN_TYPE_6BYTE:
  2054. 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]);
  2055. break;
  2056. case RFID_SN_TYPE_7BYTE:
  2057. 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]);
  2058. break;
  2059. case RFID_SN_TYPE_10BYTE:
  2060. 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]);
  2061. break;
  2062. case RFID_SN_TYPE_4BYTE:
  2063. default:
  2064. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2065. break;
  2066. }
  2067. }
  2068. else
  2069. {
  2070. // Little endian
  2071. switch(rfid.snType)
  2072. {
  2073. case RFID_SN_TYPE_6BYTE:
  2074. 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]);
  2075. break;
  2076. case RFID_SN_TYPE_7BYTE:
  2077. 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]);
  2078. break;
  2079. case RFID_SN_TYPE_10BYTE:
  2080. 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]);
  2081. break;
  2082. case RFID_SN_TYPE_4BYTE:
  2083. default:
  2084. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2085. break;
  2086. }
  2087. }
  2088. DEBUG_INFO("Start request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2089. }
  2090. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq)
  2091. {
  2092. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  2093. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  2094. DEBUG_INFO("Start Method : BACKEND...\r\n");
  2095. }
  2096. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  2097. {
  2098. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  2099. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  2100. DEBUG_INFO("Start Method : BLE...\r\n");
  2101. }
  2102. else
  2103. {
  2104. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  2105. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  2106. DEBUG_INFO("Start Method : FREE..\r\n");
  2107. }
  2108. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2109. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  2110. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  2111. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  2112. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  2113. }
  2114. else
  2115. {}
  2116. break;
  2117. case SYS_MODE_AUTHORIZING:
  2118. if(isModeChange(gun_index))
  2119. {
  2120. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  2121. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  2122. {
  2123. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  2124. {
  2125. case AUTH_MODE_ENABLE:
  2126. if(ShmOCPP16Data->OcppConnStatus)
  2127. {
  2128. // On line
  2129. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2130. }
  2131. else
  2132. {
  2133. // Off line
  2134. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  2135. {
  2136. case OFF_POLICY_LOCALLIST:
  2137. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2138. break;
  2139. case OFF_POLICY_PH_RFID:
  2140. break;
  2141. case OFF_POLICY_FREE:
  2142. break;
  2143. case OFF_POLICY_NOCHARGE:
  2144. default:
  2145. break;
  2146. }
  2147. }
  2148. break;
  2149. case AUTH_MODE_DISABLE:
  2150. default:
  2151. break;
  2152. }
  2153. }
  2154. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2155. }
  2156. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  2157. {
  2158. // Authorization timeout process.
  2159. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2160. setChargerMode(gun_index, SYS_MODE_IDLE);
  2161. DEBUG_WARN("Authorize timeout !!!\r\n");
  2162. }
  2163. else
  2164. {
  2165. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  2166. {
  2167. case START_METHOD_RFID:
  2168. if((ShmOCPP16Data->SpMsg.bits.AuthorizeConf) ||
  2169. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  2170. (!ShmOCPP16Data->OcppConnStatus && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  2171. (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))
  2172. {
  2173. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
  2174. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  2175. (!ShmOCPP16Data->OcppConnStatus && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  2176. {
  2177. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2178. DEBUG_INFO("Authorize pass.\r\n");
  2179. setSpeaker(ON,SPEAKER_SHORT);
  2180. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  2181. sleep(3);
  2182. setChargerMode(gun_index, SYS_MODE_PREPARING);
  2183. }
  2184. else
  2185. {
  2186. DEBUG_INFO("Authorize fail.\r\n");
  2187. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  2188. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  2189. sleep(3);
  2190. setChargerMode(gun_index, SYS_MODE_IDLE);
  2191. }
  2192. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2193. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  2194. }
  2195. break;
  2196. case START_METHOD_BACKEND:
  2197. case START_METHOD_BLE:
  2198. case START_METHOD_FREE:
  2199. default:
  2200. setSpeaker(ON,SPEAKER_SHORT);
  2201. setChargerMode(gun_index, SYS_MODE_PREPARING);
  2202. break;
  2203. }
  2204. }
  2205. break;
  2206. case SYS_MODE_PREPARING:
  2207. if(isModeChange(gun_index))
  2208. {
  2209. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2210. setLedMotion(gun_index,LED_ACTION_AUTHED);
  2211. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5;
  2212. }
  2213. // If control pilot detect Bx, skip watch dog time out.
  2214. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) ||
  2215. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C))
  2216. {
  2217. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2218. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  2219. switch(ShmCharger->gun_info[gun_index].ccsHandshakeState)
  2220. {
  2221. case HANDSHAKE_DUTY_5:
  2222. setRequest(gun_index, ON);
  2223. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  2224. DEBUG_INFO("Set Request On.\r\n");
  2225. // Set CCS 5% PWM duty
  2226. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_5;
  2227. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_DUTY_5_CHECK;
  2228. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2229. break;
  2230. case HANDSHAKE_DUTY_5_CHECK:
  2231. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  2232. {
  2233. //2 secs timeout
  2234. ftime(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
  2235. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CCS;
  2236. //Let CCS task start to negotiate
  2237. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = ON;
  2238. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 0;
  2239. }
  2240. break;
  2241. case HANDSHAKE_CCS:
  2242. //CCS handshake timeout
  2243. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
  2244. {
  2245. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  2246. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 1;
  2247. DEBUG_INFO("BS/HLC 2 secs handshake timeout.\r\n");
  2248. }
  2249. if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
  2250. {
  2251. DEBUG_INFO("Wait CCS give up negotiagting.\r\n");
  2252. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_CP_STATE_E;
  2253. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2254. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CP_STATE_E;
  2255. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  2256. }
  2257. #ifdef CCS_SIMULATION_DATA
  2258. ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus = 1;
  2259. #endif //CCS_SIMULATION_DATA
  2260. //CCS status check
  2261. if(ShmCharger->gun_info[gun_index].acCcsInfo.PresentMsgFlowStatus > 0)
  2262. {
  2263. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
  2264. DEBUG_INFO("Enter HLC Mode charging.\r\n");
  2265. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
  2266. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2267. }
  2268. // Use RFID card to stop handshaking
  2269. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  2270. {
  2271. DEBUG_INFO("Use RFID card to stop handshaking.\r\n");
  2272. setSpeaker(ON,SPEAKER_SHORT);
  2273. setChargerMode(gun_index, SYS_MODE_IDLE);
  2274. }
  2275. else
  2276. {
  2277. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2278. }
  2279. break;
  2280. case HANDSHAKE_CP_STATE_E:
  2281. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  2282. {
  2283. DEBUG_INFO("Change to CP STATE E for 4 secs.\r\n");
  2284. //CP STATE E for 4 secs
  2285. sleep(4);
  2286. //restore normal CP PWM duty
  2287. // Determine max charging current to MCU
  2288. DEBUG_INFO("Determine max charging current to MCU.\r\n");
  2289. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  2290. {
  2291. 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);
  2292. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2293. {
  2294. 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);
  2295. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2296. }
  2297. }
  2298. else
  2299. {
  2300. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2301. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2302. {
  2303. 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);
  2304. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2305. }
  2306. }
  2307. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2308. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
  2309. }
  2310. break;
  2311. case HANDSHAKE_SET_MAX_CURRENT:
  2312. if(ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF)
  2313. {
  2314. ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_BS_MODE;
  2315. DEBUG_INFO("Enter BS Mode charging.\r\n");
  2316. }
  2317. break;
  2318. case HANDSHAKE_BS_MODE:
  2319. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  2320. setRelay(gun_index,ON);
  2321. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON))
  2322. {
  2323. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  2324. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2325. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  2326. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  2327. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  2328. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  2329. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  2330. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100);
  2331. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100);
  2332. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100);
  2333. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100);
  2334. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2335. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  2336. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = ON;
  2337. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2338. }
  2339. break;
  2340. case HANDSHAKE_HLC_MODE:
  2341. #ifdef CCS_SIMULATION_DATA
  2342. ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress = HLC_START_MODE;
  2343. #endif //CCS_SIMULATION_DATA
  2344. if(ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_START_MODE)
  2345. {
  2346. setRelay(gun_index,ON);
  2347. ShmCharger->gun_info[gun_index].isCCSStartTransation = ON;
  2348. }
  2349. if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON) &&
  2350. (ShmCharger->gun_info[gun_index].isCCSStartTransation == ON))
  2351. {
  2352. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  2353. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2354. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  2355. ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption;
  2356. ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption;
  2357. ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption;
  2358. ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption;
  2359. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100);
  2360. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100);
  2361. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100);
  2362. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100);
  2363. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2364. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  2365. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = ON;
  2366. ShmCharger->gun_info[gun_index].isCCSStartTransation = OFF;
  2367. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2368. ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  2369. }
  2370. //120 sec timeout
  2371. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
  2372. {
  2373. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  2374. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5000)
  2375. {
  2376. DEBUG_INFO("CCS 120 secs handshake timeout.\r\n");
  2377. //Cancel CCS task negotiating
  2378. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  2379. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 1;
  2380. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  2381. #ifdef CCS_SIMULATION_DATA
  2382. ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty = CCS_PWM_DUTY_100;
  2383. #endif //CCS_SIMULATION_DATA
  2384. }
  2385. }
  2386. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100)
  2387. {
  2388. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  2389. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2390. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  2391. setChargerMode(gun_index, SYS_MODE_IDLE);
  2392. }
  2393. break;
  2394. default:
  2395. break;
  2396. }
  2397. }
  2398. // Unplug charging gun to Idle mode
  2399. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  2400. {
  2401. DEBUG_INFO("Charging gun is plugged before.\r\n");
  2402. //setChargerMode(gun_index, SYS_MODE_IDLE);
  2403. //Cancel CCS task negotiating
  2404. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  2405. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 1;
  2406. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  2407. #ifdef CCS_SIMULATION_DATA
  2408. ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty = CCS_PWM_DUTY_100;
  2409. #endif //CCS_SIMULATION_DATA
  2410. }
  2411. // Use RFID card to stop handshaking
  2412. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  2413. {
  2414. DEBUG_INFO("Use RFID card to stop handshaking.\r\n");
  2415. setSpeaker(ON,SPEAKER_SHORT);
  2416. //setChargerMode(gun_index, SYS_MODE_IDLE);
  2417. //Cancel CCS task negotiating
  2418. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  2419. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 1;
  2420. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  2421. #ifdef CCS_SIMULATION_DATA
  2422. ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty = CCS_PWM_DUTY_100;
  2423. #endif //CCS_SIMULATION_DATA
  2424. }
  2425. else
  2426. {
  2427. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2428. }
  2429. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  2430. {
  2431. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  2432. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
  2433. {
  2434. DEBUG_INFO("Handshaking timeout...\r\n");
  2435. //Cancel CCS task negotiating
  2436. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  2437. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 1;
  2438. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  2439. #ifdef CCS_SIMULATION_DATA
  2440. ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty = CCS_PWM_DUTY_100;
  2441. #endif //CCS_SIMULATION_DATA
  2442. }
  2443. }
  2444. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5)
  2445. {
  2446. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  2447. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  2448. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  2449. setChargerMode(gun_index, SYS_MODE_IDLE);
  2450. }
  2451. break;
  2452. case SYS_MODE_CHARGING:
  2453. if(isModeChange(gun_index))
  2454. {
  2455. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2456. ftime(&startChargingTime[gun_index]);
  2457. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2458. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  2459. }
  2460. switch(ShmCharger->gun_info[gun_index].chargingMode)
  2461. {
  2462. case CHARGING_MODE_BS:
  2463. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  2464. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  2465. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON)||
  2466. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  2467. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  2468. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON))
  2469. {
  2470. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2471. }
  2472. else
  2473. {
  2474. setLedMotion(gun_index,LED_ACTION_CHARGING);
  2475. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100);
  2476. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100);
  2477. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100);
  2478. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100);
  2479. ftime(&endChargingTime[gun_index]);
  2480. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2481. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = ((float)(ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption - ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start))/100;
  2482. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy[0] = ((float)(ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start))/100;
  2483. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy[1] = ((float)(ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start))/100;
  2484. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy[2] = ((float)(ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start))/100;
  2485. // Response StartTransactionConf
  2486. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == ON)
  2487. {
  2488. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = OFF;
  2489. }
  2490. // Charging profile preparation
  2491. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  2492. {
  2493. if(!ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf)
  2494. {
  2495. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = ON;
  2496. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  2497. }
  2498. else
  2499. {
  2500. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = OFF;
  2501. }
  2502. }
  2503. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  2504. if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId>0) &&
  2505. (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON) &&
  2506. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2507. {
  2508. // Debug information
  2509. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2510. {
  2511. DEBUG_INFO("Profile ID found: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2512. DEBUG_INFO("Valid from: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2513. DEBUG_INFO("Valid to: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2514. DEBUG_INFO("Start schedule: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2515. DEBUG_INFO("Profile kind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2516. DEBUG_INFO("RecurrencyKind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2517. DEBUG_INFO("Profile purpose: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2518. DEBUG_INFO("Transaction ID: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2519. DEBUG_INFO("ChargingRateUnit: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2520. }
  2521. // Checking profile kind
  2522. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2523. {
  2524. // Absolute profile
  2525. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2526. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2527. {
  2528. // Checking limitation
  2529. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2530. {
  2531. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2532. (((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)))
  2533. {
  2534. 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));
  2535. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2536. }
  2537. else
  2538. break;
  2539. }
  2540. }
  2541. }
  2542. }
  2543. // Determine max charging current to MCU
  2544. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  2545. {
  2546. 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);
  2547. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2548. {
  2549. 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);
  2550. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2551. }
  2552. }
  2553. else
  2554. {
  2555. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2556. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2557. {
  2558. 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);
  2559. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2560. }
  2561. }
  2562. // Debug information
  2563. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2564. {
  2565. DEBUG_INFO("=======================================================================\r\n");
  2566. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \r\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  2567. 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);
  2568. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\r\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  2569. DEBUG_INFO("=======================================================================\r\n");
  2570. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2571. }
  2572. if(ShmOCPP16Data->OcppConnStatus)
  2573. {
  2574. // On-line max condition check
  2575. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  2576. {
  2577. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2578. 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));
  2579. }
  2580. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  2581. {
  2582. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2583. 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));
  2584. }
  2585. else
  2586. {
  2587. setRelay(gun_index, ON);
  2588. }
  2589. }
  2590. else
  2591. {
  2592. // Off-line max condition check
  2593. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2594. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  2595. {
  2596. 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))))
  2597. {
  2598. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2599. 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));
  2600. }
  2601. 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))))
  2602. {
  2603. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2604. 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));
  2605. }
  2606. else
  2607. {
  2608. setRelay(gun_index, ON);
  2609. }
  2610. }
  2611. else
  2612. {
  2613. setRelay(gun_index, OFF);
  2614. setRequest(gun_index, OFF);
  2615. DEBUG_INFO("Connector-%d can not charging in off line\r\n", gun_index);
  2616. }
  2617. }
  2618. }
  2619. break;
  2620. case CHARGING_MODE_HLC:
  2621. //if time up, clear CCS MSG count
  2622. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET)
  2623. {
  2624. ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
  2625. if(ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat > 0)
  2626. {
  2627. ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat = 0;
  2628. }
  2629. else
  2630. {
  2631. DEBUG_INFO("CCS could not get MSG from car.\r\n");
  2632. //setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2633. //setRelay(gun_index, OFF);
  2634. #ifdef CCS_SIMULATION_DATA
  2635. //ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress = HLC_STOP_MODE;
  2636. #endif //CCS_SIMULATION_DATA
  2637. }
  2638. }
  2639. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  2640. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  2641. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON)||
  2642. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  2643. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  2644. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON) ||
  2645. (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE) ||
  2646. (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_RENEGOTIATE_MODE) ||
  2647. (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STANDBY_MODE))
  2648. {
  2649. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2650. setRelay(gun_index, OFF);
  2651. }
  2652. else
  2653. {
  2654. setLedMotion(gun_index,LED_ACTION_CHARGING);
  2655. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100);
  2656. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100);
  2657. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100);
  2658. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100);
  2659. ftime(&endChargingTime[gun_index]);
  2660. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2661. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = ((float)(ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption - ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption_at_start))/100;
  2662. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy[0] = ((float)(ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption_at_start))/100;
  2663. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy[1] = ((float)(ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption_at_start))/100;
  2664. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy[2] = ((float)(ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption - ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption_at_start))/100;
  2665. // Response StartTransactionConf
  2666. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == ON)
  2667. {
  2668. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = OFF;
  2669. }
  2670. // Charging profile preparation
  2671. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  2672. {
  2673. if(!ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf)
  2674. {
  2675. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = ON;
  2676. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  2677. }
  2678. else
  2679. {
  2680. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = OFF;
  2681. }
  2682. }
  2683. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  2684. if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId>0) &&
  2685. (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON) &&
  2686. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2687. {
  2688. // Debug information
  2689. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2690. {
  2691. DEBUG_INFO("Profile ID found: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2692. DEBUG_INFO("Valid from: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2693. DEBUG_INFO("Valid to: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2694. DEBUG_INFO("Start schedule: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2695. DEBUG_INFO("Profile kind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2696. DEBUG_INFO("RecurrencyKind: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2697. DEBUG_INFO("Profile purpose: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2698. DEBUG_INFO("Transaction ID: %d\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2699. DEBUG_INFO("ChargingRateUnit: %s\r\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2700. }
  2701. // Checking profile kind
  2702. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2703. {
  2704. // Absolute profile
  2705. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2706. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2707. {
  2708. // Checking limitation
  2709. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2710. {
  2711. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2712. (((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)))
  2713. {
  2714. 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);
  2715. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\r\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2716. }
  2717. else
  2718. break;
  2719. }
  2720. }
  2721. }
  2722. }
  2723. // Determine max charging current to MCU
  2724. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  2725. {
  2726. 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);
  2727. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2728. {
  2729. //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);
  2730. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2731. 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);
  2732. }
  2733. }
  2734. else
  2735. {
  2736. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2737. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2738. {
  2739. //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);
  2740. //ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2741. 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);
  2742. }
  2743. }
  2744. // Debug information
  2745. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2746. {
  2747. DEBUG_INFO("=======================================================================\r\n");
  2748. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \r\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  2749. 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);
  2750. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\r\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  2751. DEBUG_INFO("=======================================================================\r\n");
  2752. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2753. }
  2754. if(ShmOCPP16Data->OcppConnStatus)
  2755. {
  2756. // On-line max condition check
  2757. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  2758. {
  2759. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2760. setRelay(gun_index, OFF);
  2761. 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));
  2762. }
  2763. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  2764. {
  2765. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2766. setRelay(gun_index, OFF);
  2767. 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));
  2768. }
  2769. else
  2770. {
  2771. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  2772. setRelay(gun_index, ON);
  2773. }
  2774. }
  2775. else
  2776. {
  2777. // Off-line max condition check
  2778. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2779. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  2780. {
  2781. 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))))
  2782. {
  2783. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2784. setRelay(gun_index, OFF);
  2785. 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));
  2786. }
  2787. 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))))
  2788. {
  2789. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2790. setRelay(gun_index, OFF);
  2791. 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));
  2792. }
  2793. else
  2794. {
  2795. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)
  2796. setRelay(gun_index, ON);
  2797. }
  2798. }
  2799. else
  2800. {
  2801. setRelay(gun_index, OFF);
  2802. DEBUG_INFO("Connector-%d can not charging in off line\r\n", gun_index);
  2803. }
  2804. }
  2805. }
  2806. break;
  2807. default:
  2808. break;
  2809. }
  2810. break;
  2811. case SYS_MODE_TERMINATING:
  2812. if(isModeChange(gun_index))
  2813. {
  2814. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  2815. {
  2816. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2817. }
  2818. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  2819. {
  2820. setLedMotion(gun_index, LED_ACTION_STOP);
  2821. }
  2822. else
  2823. {}
  2824. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  2825. }
  2826. switch(ShmCharger->gun_info[gun_index].chargingMode)
  2827. {
  2828. case CHARGING_MODE_BS:
  2829. // If RFID SN different with start user, it need to authorize ID
  2830. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  2831. {
  2832. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ShmOCPP16Data->SpMsg.bits.AuthorizeReq)
  2833. {
  2834. // Request authorization
  2835. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  2836. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2837. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2838. {
  2839. // Big endian
  2840. switch(rfid.snType)
  2841. {
  2842. case RFID_SN_TYPE_6BYTE:
  2843. 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]);
  2844. break;
  2845. case RFID_SN_TYPE_7BYTE:
  2846. 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]);
  2847. break;
  2848. case RFID_SN_TYPE_10BYTE:
  2849. 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]);
  2850. break;
  2851. case RFID_SN_TYPE_4BYTE:
  2852. default:
  2853. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2854. break;
  2855. }
  2856. }
  2857. else
  2858. {
  2859. // Little endian
  2860. switch(rfid.snType)
  2861. {
  2862. case RFID_SN_TYPE_6BYTE:
  2863. 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]);
  2864. break;
  2865. case RFID_SN_TYPE_7BYTE:
  2866. 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]);
  2867. break;
  2868. case RFID_SN_TYPE_10BYTE:
  2869. 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]);
  2870. break;
  2871. case RFID_SN_TYPE_4BYTE:
  2872. default:
  2873. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2874. break;
  2875. }
  2876. }
  2877. DEBUG_INFO("End request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2878. DEBUG_INFO("Start method : %d...\r\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  2879. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  2880. {
  2881. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  2882. {
  2883. case AUTH_MODE_ENABLE:
  2884. if(ShmOCPP16Data->OcppConnStatus)
  2885. {
  2886. // On line
  2887. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2888. }
  2889. else
  2890. {
  2891. // Off line
  2892. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  2893. {
  2894. case OFF_POLICY_LOCALLIST:
  2895. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2896. break;
  2897. case OFF_POLICY_PH_RFID:
  2898. break;
  2899. case OFF_POLICY_FREE:
  2900. break;
  2901. case OFF_POLICY_NOCHARGE:
  2902. default:
  2903. break;
  2904. }
  2905. }
  2906. break;
  2907. case AUTH_MODE_DISABLE:
  2908. default:
  2909. break;
  2910. }
  2911. }
  2912. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  2913. {
  2914. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2915. DEBUG_INFO("Need to match with start method : START_METHOD_BACKEND...\r\n");
  2916. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2917. }
  2918. else if (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE)
  2919. {
  2920. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2921. DEBUG_INFO("Need to match with start method : START_METHOD_BLE...\r\n");
  2922. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2923. }
  2924. else
  2925. {}
  2926. }
  2927. else
  2928. {
  2929. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf ||
  2930. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2931. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  2932. {
  2933. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
  2934. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2935. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  2936. {
  2937. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  2938. }
  2939. else
  2940. {
  2941. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2942. DEBUG_INFO("Authorize fail.\r\n");
  2943. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  2944. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  2945. sleep(3);
  2946. }
  2947. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  2948. }
  2949. }
  2950. }
  2951. // End authorize pass
  2952. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  2953. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  2954. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  2955. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON) ||
  2956. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)) ||
  2957. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  2958. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON))
  2959. {
  2960. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  2961. {
  2962. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  2963. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  2964. {
  2965. DEBUG_INFO("Authorize pass.\r\n");
  2966. setSpeaker(ON,SPEAKER_SHORT);
  2967. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  2968. sleep(3);
  2969. }
  2970. }
  2971. else
  2972. {}
  2973. setRelay(gun_index, OFF);
  2974. setRequest(gun_index, OFF);
  2975. setLedMotion(gun_index, LED_ACTION_STOP);
  2976. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  2977. {
  2978. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  2979. }
  2980. }
  2981. else
  2982. {
  2983. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  2984. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  2985. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  2986. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != ON) &&
  2987. !ShmOCPP16Data->MsMsg.bits.ResetReq &&
  2988. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  2989. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  2990. !(!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)))) &&
  2991. !(!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)))))
  2992. {
  2993. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2994. }
  2995. }
  2996. break;
  2997. case CHARGING_MODE_HLC:
  2998. // If RFID SN different with start user, it need to authorize ID
  2999. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  3000. {
  3001. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ShmOCPP16Data->SpMsg.bits.AuthorizeReq)
  3002. {
  3003. // Request authorization
  3004. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3005. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3006. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3007. {
  3008. // Big endian
  3009. switch(rfid.snType)
  3010. {
  3011. case RFID_SN_TYPE_6BYTE:
  3012. 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]);
  3013. break;
  3014. case RFID_SN_TYPE_7BYTE:
  3015. 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]);
  3016. break;
  3017. case RFID_SN_TYPE_10BYTE:
  3018. 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]);
  3019. break;
  3020. case RFID_SN_TYPE_4BYTE:
  3021. default:
  3022. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3023. break;
  3024. }
  3025. }
  3026. else
  3027. {
  3028. // Little endian
  3029. switch(rfid.snType)
  3030. {
  3031. case RFID_SN_TYPE_6BYTE:
  3032. 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]);
  3033. break;
  3034. case RFID_SN_TYPE_7BYTE:
  3035. 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]);
  3036. break;
  3037. case RFID_SN_TYPE_10BYTE:
  3038. 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]);
  3039. break;
  3040. case RFID_SN_TYPE_4BYTE:
  3041. default:
  3042. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3043. break;
  3044. }
  3045. }
  3046. DEBUG_INFO("End request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3047. DEBUG_INFO("Start method : %d...\r\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  3048. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  3049. {
  3050. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3051. {
  3052. case AUTH_MODE_ENABLE:
  3053. if(ShmOCPP16Data->OcppConnStatus)
  3054. {
  3055. // On line
  3056. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  3057. }
  3058. else
  3059. {
  3060. // Off line
  3061. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3062. {
  3063. case OFF_POLICY_LOCALLIST:
  3064. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  3065. break;
  3066. case OFF_POLICY_PH_RFID:
  3067. break;
  3068. case OFF_POLICY_FREE:
  3069. break;
  3070. case OFF_POLICY_NOCHARGE:
  3071. default:
  3072. break;
  3073. }
  3074. }
  3075. break;
  3076. case AUTH_MODE_DISABLE:
  3077. default:
  3078. break;
  3079. }
  3080. }
  3081. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  3082. {
  3083. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3084. DEBUG_INFO("Need to match with start method : START_METHOD_BACKEND...\r\n");
  3085. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3086. }
  3087. else if (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE)
  3088. {
  3089. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3090. DEBUG_INFO("Need to match with start method : START_METHOD_BLE...\r\n");
  3091. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3092. }
  3093. else
  3094. {}
  3095. }
  3096. else
  3097. {
  3098. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf ||
  3099. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3100. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3101. {
  3102. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
  3103. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3104. (!ShmOCPP16Data->OcppConnStatus&&(ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3105. {
  3106. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  3107. }
  3108. else
  3109. {
  3110. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3111. DEBUG_INFO("Authorize fail.\r\n");
  3112. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3113. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3114. sleep(3);
  3115. }
  3116. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  3117. }
  3118. }
  3119. }
  3120. // End authorize pass
  3121. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  3122. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  3123. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  3124. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON) ||
  3125. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)) ||
  3126. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  3127. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON) ||
  3128. (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress == HLC_STOP_MODE))
  3129. {
  3130. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  3131. {
  3132. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  3133. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  3134. {
  3135. DEBUG_INFO("Authorize pass.\r\n");
  3136. setSpeaker(ON,SPEAKER_SHORT);
  3137. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3138. sleep(3);
  3139. }
  3140. }
  3141. else
  3142. {}
  3143. setRelay(gun_index, OFF);
  3144. setLedMotion(gun_index, LED_ACTION_STOP);
  3145. #ifdef CCS_SIMULATION_DATA
  3146. //for CCS testing
  3147. setRequest(gun_index, OFF);
  3148. #endif //CCS_SIMULATION_DATA
  3149. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  3150. {
  3151. //setChargerMode(gun_index, SYS_MODE_COMPLETE);
  3152. //Cancel CCS task negotiating
  3153. ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
  3154. ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = 1;
  3155. setRelay(gun_index,OFF);
  3156. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = ON;
  3157. #ifdef CCS_SIMULATION_DATA
  3158. ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty = CCS_PWM_DUTY_100;
  3159. #endif //CCS_SIMULATION_DATA
  3160. }
  3161. }
  3162. else
  3163. {
  3164. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  3165. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  3166. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  3167. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != ON) &&
  3168. !ShmOCPP16Data->MsMsg.bits.ResetReq &&
  3169. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  3170. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  3171. !(!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)))) &&
  3172. !(!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)))) &&
  3173. (ShmCharger->gun_info[gun_index].acCcsInfo.EVChargeProgress != HLC_STOP_MODE))
  3174. {
  3175. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3176. }
  3177. }
  3178. if((ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty == ON) && ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty == CCS_PWM_DUTY_100)
  3179. {
  3180. DEBUG_INFO("Set PWM duty 100% go to SYS_MODE_TERMINATING.\r\n");
  3181. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = CCS_PWM_DUTY_100;
  3182. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = ON;
  3183. ShmCharger->gun_info[gun_index].isCCSWaitChangeDuty = OFF;
  3184. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  3185. }
  3186. break;
  3187. default:
  3188. break;
  3189. }
  3190. break;
  3191. case SYS_MODE_COMPLETE:
  3192. if(isModeChange(gun_index))
  3193. {
  3194. setLedMotion(gun_index, LED_ACTION_STOP);
  3195. }
  3196. switch(ShmCharger->gun_info[gun_index].chargingMode)
  3197. {
  3198. case CHARGING_MODE_BS:
  3199. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3200. {
  3201. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  3202. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  3203. else
  3204. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  3205. }
  3206. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  3207. {
  3208. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  3209. }
  3210. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3211. {
  3212. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  3213. }
  3214. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3215. {
  3216. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  3217. }
  3218. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3219. {
  3220. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  3221. }
  3222. else
  3223. {
  3224. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  3225. }
  3226. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\r\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3227. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3228. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100);
  3229. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[0] = (ShmCharger->gun_info[gun_index].powerConsumption[0].power_consumption/100);
  3230. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[1] = (ShmCharger->gun_info[gun_index].powerConsumption[1].power_consumption/100);
  3231. //ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/100);
  3232. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3233. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3234. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  3235. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  3236. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  3237. DB_Insert_Record(localDb, gun_index);
  3238. setChargerMode(gun_index, SYS_MODE_IDLE);
  3239. break;
  3240. case CHARGING_MODE_HLC:
  3241. setRelay(gun_index, OFF);
  3242. setRequest(gun_index, OFF);
  3243. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3244. {
  3245. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  3246. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  3247. else
  3248. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  3249. }
  3250. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  3251. {
  3252. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  3253. }
  3254. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3255. {
  3256. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  3257. }
  3258. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3259. {
  3260. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  3261. }
  3262. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3263. {
  3264. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  3265. }
  3266. else
  3267. {
  3268. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  3269. }
  3270. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\r\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3271. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3272. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumptionTotal.power_consumption/100);
  3273. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3274. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3275. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  3276. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  3277. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  3278. DB_Insert_Record(localDb, gun_index);
  3279. setChargerMode(gun_index, SYS_MODE_IDLE);
  3280. break;
  3281. default:
  3282. break;
  3283. }
  3284. ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_BS;
  3285. break;
  3286. case SYS_MODE_ALARM:
  3287. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  3288. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  3289. {
  3290. }
  3291. else
  3292. {
  3293. setRelay(gun_index, OFF);
  3294. setRequest(gun_index, OFF);
  3295. }
  3296. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  3297. {
  3298. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  3299. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  3300. {
  3301. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  3302. }
  3303. else
  3304. {
  3305. setChargerMode(gun_index, SYS_MODE_IDLE);
  3306. }
  3307. }
  3308. setLedMotion(gun_index,LED_ACTION_ALARM);
  3309. break;
  3310. case SYS_MODE_FAULT:
  3311. if(isModeChange(gun_index))
  3312. {}
  3313. break;
  3314. case SYS_MODE_MAINTAIN:
  3315. if(isModeChange(gun_index))
  3316. {
  3317. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3318. }
  3319. break;
  3320. case SYS_MODE_UPDATE:
  3321. if(isModeChange(gun_index))
  3322. {
  3323. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3324. }
  3325. //======================================
  3326. // Check local upgrade firmware request
  3327. //======================================
  3328. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  3329. {
  3330. DEBUG_INFO("Firmware local upgrading...\r\n");
  3331. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  3332. int value = upgrade_check();
  3333. DEBUG_INFO("Local update Value: %d...\r\n",value);
  3334. if(value == PASS)
  3335. {
  3336. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3337. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3338. ShmCharger->isUpdateSuccess = YES;
  3339. DEBUG_INFO("Local update success...\r\n");
  3340. }
  3341. else
  3342. {
  3343. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3344. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3345. ShmCharger->isUpdateSuccess = NO;
  3346. DEBUG_INFO("Local update unsuccess...\r\n");
  3347. }
  3348. }
  3349. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq)
  3350. {
  3351. //======================================
  3352. // Check remote upgrade firmware request
  3353. //======================================
  3354. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  3355. {
  3356. DEBUG_INFO("Firmware remote upgraded fail...\r\n");
  3357. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3358. ShmCharger->isUpdateSuccess = NO;
  3359. }
  3360. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  3361. {
  3362. DEBUG_INFO("Firmware remote upgrading...\r\n");
  3363. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3364. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3365. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3366. int result = upgrade_check();
  3367. DEBUG_INFO("Remote update Result: %d...\r\n",result);
  3368. if(result == PASS)
  3369. {
  3370. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3371. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3372. ShmCharger->isUpdateSuccess = YES;
  3373. DEBUG_INFO("Remote update success...\r\n");
  3374. }
  3375. else
  3376. {
  3377. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3378. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3379. ShmCharger->isUpdateSuccess = NO;
  3380. DEBUG_INFO("Remote update unsuccess...\r\n");
  3381. }
  3382. }
  3383. else
  3384. {}
  3385. }
  3386. else
  3387. {
  3388. //======================================
  3389. // Upgrade complete reboot system
  3390. //======================================
  3391. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  3392. {
  3393. if(ShmCharger->isUpdateSuccess == YES)
  3394. {
  3395. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3396. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3397. DEBUG_WARN("Firmware upgrade success.\r\n");
  3398. sleep(5);
  3399. DEBUG_INFO("Firmware upgraded, reboot...\r\n");
  3400. system("reboot -f");
  3401. sleep(5);
  3402. system("reboot -f");
  3403. }
  3404. else
  3405. {
  3406. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  3407. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3408. DEBUG_WARN("Firmware upgrade fail.\r\n");
  3409. close(wtdFd);
  3410. system("/usr/bin/run_evse_restart.sh");
  3411. }
  3412. }
  3413. }
  3414. break;
  3415. case SYS_MODE_RESERVATION:
  3416. if(isModeChange(gun_index))
  3417. {
  3418. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3419. }
  3420. if(isReservationExpired(gun_index))
  3421. {
  3422. DEBUG_INFO("Reservation: Time's up...\r\n");
  3423. setChargerMode(gun_index, SYS_MODE_IDLE);
  3424. }
  3425. else if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq)
  3426. {
  3427. DEBUG_INFO("Reservation: Cancel reservation...\r\n");
  3428. setChargerMode(gun_index, SYS_MODE_IDLE);
  3429. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = OFF;
  3430. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  3431. }
  3432. else
  3433. {
  3434. // Check is there RFID or back end request start
  3435. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3436. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == ON))
  3437. {
  3438. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  3439. {
  3440. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3441. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3442. {
  3443. // Big endian
  3444. switch(rfid.snType)
  3445. {
  3446. case RFID_SN_TYPE_6BYTE:
  3447. 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]);
  3448. break;
  3449. case RFID_SN_TYPE_7BYTE:
  3450. 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]);
  3451. break;
  3452. case RFID_SN_TYPE_10BYTE:
  3453. 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]);
  3454. break;
  3455. case RFID_SN_TYPE_4BYTE:
  3456. default:
  3457. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3458. break;
  3459. }
  3460. }
  3461. else
  3462. {
  3463. // Little endian
  3464. switch(rfid.snType)
  3465. {
  3466. case RFID_SN_TYPE_6BYTE:
  3467. 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]);
  3468. break;
  3469. case RFID_SN_TYPE_7BYTE:
  3470. 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]);
  3471. break;
  3472. case RFID_SN_TYPE_10BYTE:
  3473. 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]);
  3474. break;
  3475. case RFID_SN_TYPE_4BYTE:
  3476. default:
  3477. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3478. break;
  3479. }
  3480. }
  3481. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  3482. {
  3483. DEBUG_INFO("Start Method in reservation : RFID...\r\n");
  3484. DEBUG_INFO("Start request User Id : %s\r\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3485. DEBUG_INFO("Reservation User Id : %s\r\n", ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  3486. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  3487. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3488. }
  3489. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3490. }
  3491. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq)
  3492. {
  3493. if(strcmp((char*)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  3494. {
  3495. DEBUG_INFO("Start Method in reservation: BACKEND...\r\n");
  3496. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  3497. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  3498. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3499. }
  3500. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  3501. }
  3502. }
  3503. }
  3504. break;
  3505. case SYS_MODE_BOOKING:
  3506. if(isModeChange(gun_index))
  3507. {}
  3508. break;
  3509. case SYS_MODE_DEBUG:
  3510. if(isModeChange(gun_index))
  3511. {
  3512. setLedMotion(gun_index,LED_ACTION_DEBUG);
  3513. }
  3514. break;
  3515. }
  3516. }
  3517. // System watch dog reset
  3518. write(wtdFd, "a", 1);
  3519. usleep(50000);
  3520. }
  3521. return FAIL;
  3522. }