main.c 163 KB

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