main.c 155 KB

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