main.c 159 KB

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