main.c 129 KB

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