main.c 164 KB

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