main.c 143 KB

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