main.c 154 KB

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