main.c 153 KB

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