main.c 154 KB

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