main.c 147 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907
  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. // SysConfig in flash is empty (0xffffffff)
  729. if((strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName)) ||
  730. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber)) ||
  731. (strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId)) ||
  732. (ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff))
  733. {
  734. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName))
  735. {
  736. memset(ShmSysConfigAndInfo->SysConfig.ModelName, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  737. }
  738. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber))
  739. {
  740. memset(ShmSysConfigAndInfo->SysConfig.SerialNumber, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  741. }
  742. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) > ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId))
  743. {
  744. memset(ShmSysConfigAndInfo->SysConfig.SystemId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SystemId));
  745. }
  746. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient == 0xff)
  747. {
  748. DEBUG_INFO("Ethernet dhcp config is null.\n");
  749. }
  750. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) == 0x00)
  751. {
  752. DEBUG_INFO("Model name over length.\n");
  753. }
  754. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SerialNumber) == 0x00)
  755. {
  756. DEBUG_INFO("Model serial number over length.\n");
  757. }
  758. if(strlen((char*)ShmSysConfigAndInfo->SysConfig.SystemId) == 0x00)
  759. {
  760. DEBUG_INFO("System id over length.\n");
  761. }
  762. system("cd /root;./Module_FactoryConfig -m");
  763. sleep(3);
  764. system("/usr/bin/run_evse_restart.sh");
  765. }
  766. DEBUG_INFO("Load SysConfigData OK\n");
  767. return PASS;
  768. }
  769. int StoreUsrConfigData(struct SysConfigData *UsrData)
  770. {
  771. int result = PASS;
  772. int fd,wrd;
  773. unsigned int i,Chk;
  774. unsigned char *ptr, *BufTmp;
  775. Chk=0;
  776. ptr=(unsigned char *)UsrData;
  777. if((BufTmp=malloc(MtdBlockSize))!=NULL)
  778. {
  779. memset(BufTmp,0,MtdBlockSize);
  780. memcpy(BufTmp,ptr,sizeof(struct SysConfigData));
  781. for(i=0;i<MtdBlockSize-4;i++)
  782. Chk+=*(BufTmp+i);
  783. memcpy(BufTmp+MtdBlockSize-4, &Chk, 4);
  784. // Output configuration to file.
  785. fd = open("/mnt/EvseConfig.bin", O_RDWR|O_CREAT);
  786. if (fd < 0)
  787. {
  788. DEBUG_ERROR("open /mnt/EvseConfig.bin NG\n");
  789. free(BufTmp);
  790. return 0;
  791. }
  792. wrd=write(fd, BufTmp, MtdBlockSize);
  793. close(fd);
  794. if(wrd<MtdBlockSize)
  795. {
  796. DEBUG_ERROR("write /mnt/EvseConfig.bin NG\n");
  797. free(BufTmp);
  798. return 0;
  799. }
  800. DEBUG_INFO("EvseConfig write to file in /mnt OK.\n");
  801. DEBUG_INFO("Erase /dev/mtd10.\n");
  802. runShellCmd("flash_erase /dev/mtd10 0 12");
  803. DEBUG_INFO("Write /dev/mtd10.\n");
  804. runShellCmd("nandwrite -p /dev/mtd10 /mnt/EvseConfig.bin");
  805. DEBUG_INFO("Erase /dev/mtd11.\n");
  806. runShellCmd("flash_erase /dev/mtd11 0 12");
  807. DEBUG_INFO("Write /dev/mtd11.\n");
  808. runShellCmd("nandwrite -p /dev/mtd11 /mnt/EvseConfig.bin");
  809. system("rm -f /mnt/EvseConfig.bin");
  810. DEBUG_INFO("EvseConfig write to flash OK\n");
  811. }
  812. else
  813. {
  814. DEBUG_ERROR("alloc BlockSize NG\r\n");
  815. result = FAIL;
  816. }
  817. if(BufTmp!=NULL)
  818. free(BufTmp);
  819. return result;
  820. }
  821. void InitEthernet()
  822. {
  823. pid_t pid;
  824. uint8_t cnt_pingDNS_Fail;
  825. char tmpbuf[256];
  826. //unsigned int address;
  827. //Init Eth0 for internet
  828. memset(tmpbuf,0,256);
  829. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  830. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  831. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  832. DEBUG_INFO("eth0 config as ip: %s, netmask: %s\n", ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  833. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  834. system(tmpbuf);
  835. memset(tmpbuf,0,256);
  836. sprintf(tmpbuf,"route add default gw %s eth0 &",
  837. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  838. system(tmpbuf);
  839. system("/sbin/ifconfig eth0:1 192.168.201.201 netmask 255.255.255.248 up &");
  840. system("ifconfig lo up &");
  841. system("/sbin/ethtool -s eth0 speed 10 duplex full autoneg off");
  842. if(isInterfaceUp("eth1")==PASS)
  843. {
  844. //Init Eth1 for administrator tool
  845. memset(tmpbuf,0,256);
  846. sprintf(tmpbuf,"/sbin/ifconfig eth1 %s netmask %s up &",
  847. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthIpAddress,
  848. ShmSysConfigAndInfo->SysConfig.Eth1Interface.EthSubmaskAddress);
  849. system(tmpbuf);
  850. }
  851. //Run DHCP client if enabled
  852. system("killall udhcpc");
  853. system("rm -rf /etc/resolv.conf");
  854. system("echo nameserver 8.8.8.8 >> /etc/resolv.conf"); //Google DNS server
  855. system("echo nameserver 180.76.76.76 >> /etc/resolv.conf"); //Baidu DNS server
  856. if(ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0)
  857. {
  858. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  859. system(tmpbuf);
  860. }
  861. // Upgrade system id to /etc/hostname
  862. sprintf(tmpbuf, "echo %s > /etc/hostname", ShmSysConfigAndInfo->SysConfig.SystemId);
  863. system(tmpbuf);
  864. //check internet status
  865. pid = fork();
  866. if(pid == 0)
  867. {
  868. for(;;)
  869. {
  870. if(isRouteFail())
  871. {
  872. DEBUG_ERROR("eth0 not in route, restart eth0.\n");
  873. system("/sbin/ifconfig eth0 down;/sbin/ifconfig eth0 up");
  874. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  875. {
  876. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  877. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  878. system(tmpbuf);
  879. }
  880. else
  881. {
  882. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  883. memset(tmpbuf,0,256);
  884. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  885. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  886. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  887. system(tmpbuf);
  888. memset(tmpbuf,0,256);
  889. sprintf(tmpbuf,"route add default gw %s eth0 &",
  890. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  891. system(tmpbuf);
  892. }
  893. }
  894. if(isReachableInternet() == PASS)
  895. {
  896. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=OFF;
  897. cnt_pingDNS_Fail = 0;
  898. }
  899. else
  900. {
  901. if(cnt_pingDNS_Fail >= 3)
  902. {
  903. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
  904. if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
  905. {
  906. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  907. sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
  908. system(tmpbuf);
  909. }
  910. else
  911. {
  912. system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
  913. memset(tmpbuf,0,256);
  914. sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
  915. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
  916. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
  917. system(tmpbuf);
  918. memset(tmpbuf,0,256);
  919. sprintf(tmpbuf,"route add default gw %s eth0 &",
  920. ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
  921. system(tmpbuf);
  922. }
  923. cnt_pingDNS_Fail = 0;
  924. }
  925. else
  926. {
  927. cnt_pingDNS_Fail++;
  928. }
  929. }
  930. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet &&
  931. ((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi) &&
  932. ((ShmSysConfigAndInfo->SysConfig.TelecomInterface.TelcomEnabled == 0) || ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi) &&
  933. (ShmOCPP16Data->OcppConnStatus != PASS))
  934. {
  935. ShmSysConfigAndInfo->SysInfo.InternetConn = OFF;
  936. }
  937. else
  938. {
  939. ShmSysConfigAndInfo->SysInfo.InternetConn = ON;
  940. }
  941. //============================================================
  942. // Priority for internet 0 : First / 1 : Second / 2: Third
  943. //============================================================
  944. if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet)
  945. {
  946. system("/sbin/ifmetric eth0 0");
  947. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  948. {
  949. system("/sbin/ifmetric mlan0 1");
  950. }
  951. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  952. {
  953. system("/sbin/ifmetric ppp0 2");
  954. }
  955. }
  956. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi)
  957. {
  958. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  959. {
  960. system("/sbin/ifmetric eth0 1");
  961. system("/sbin/ifmetric mlan0 0");
  962. }
  963. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  964. {
  965. system("/sbin/ifmetric ppp0 2");
  966. }
  967. }
  968. else if(!ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi)
  969. {
  970. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  971. {
  972. system("/sbin/ifmetric mlan0 2");
  973. }
  974. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  975. {
  976. system("/sbin/ifmetric eth0 1");
  977. system("/sbin/ifmetric ppp0 0");
  978. }
  979. }
  980. sleep(5);
  981. }
  982. }
  983. DEBUG_INFO("Initial Ethernet OK\n");
  984. }
  985. int SpawnTask()
  986. {
  987. system ("pkill Module_4g");
  988. system ("pkill Module_Wifi");
  989. system ("pkill Module_EventLogging");
  990. system ("pkill OcppBackend");
  991. system ("pkill Module_AlarmDetect");
  992. system ("pkill Module_InternalComm");
  993. system ("pkill Module_Speaker");
  994. system ("pkill Module_ProduceUtils");
  995. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  996. {
  997. system("/root/Module_4g &");
  998. }
  999. else if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1000. {
  1001. system("/root/Module_Wifi &");
  1002. }
  1003. system("/root/Module_EventLogging &");
  1004. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  1005. {
  1006. system("/root/OcppBackend &");
  1007. }
  1008. else
  1009. {
  1010. DEBUG_INFO("OCPP URL is empty, need to create a configuration table !!!\n");
  1011. system("/root/OcppBackend &");
  1012. }
  1013. system ("/root/Module_AlarmDetect &");
  1014. system ("/root/Module_InternalComm &");
  1015. system ("/root/Module_Speaker &");
  1016. system ("/root/Module_ProduceUtils &");
  1017. return PASS;
  1018. }
  1019. int Initialization()
  1020. {
  1021. int result = PASS;
  1022. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  1023. InitGPIO();
  1024. InitEthernet();
  1025. if(DB_Open(localDb) != PASS)
  1026. {
  1027. DEBUG_ERROR("Local database initial fail.\n");
  1028. result = FAIL;
  1029. }
  1030. for(int gun_index=0;gun_index< AC_QUANTITY;gun_index++)
  1031. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  1032. if((rfidFd = InitRfidPort()) == FAIL)
  1033. {
  1034. DEBUG_ERROR("RFID port initial fail.\n");
  1035. result = FAIL;
  1036. }
  1037. if((wtdFd = InitWatchDog()) == FAIL)
  1038. {
  1039. DEBUG_ERROR("Watchdog initial fail.\n");
  1040. result = FAIL;
  1041. }
  1042. if(result == PASS)
  1043. DEBUG_INFO("Initialization OK.\n");
  1044. else
  1045. DEBUG_INFO("Initialization Fail.\n");
  1046. return result;
  1047. }
  1048. //=====================================================
  1049. // Common routine
  1050. //=====================================================
  1051. char* getSystemModeName(unsigned char mode)
  1052. {
  1053. char* result;
  1054. switch(mode)
  1055. {
  1056. case SYS_MODE_BOOTING:
  1057. result = "booting";
  1058. break;
  1059. case SYS_MODE_IDLE:
  1060. result = "idle";
  1061. break;
  1062. case SYS_MODE_AUTHORIZING:
  1063. result = "authorizing";
  1064. break;
  1065. case SYS_MODE_PREPARING:
  1066. result = "preparing";
  1067. break;
  1068. case SYS_MODE_CHARGING:
  1069. result = "charging";
  1070. break;
  1071. case SYS_MODE_TERMINATING:
  1072. result = "terminating";
  1073. break;
  1074. case SYS_MODE_COMPLETE:
  1075. result = "complete";
  1076. break;
  1077. case SYS_MODE_ALARM:
  1078. result = "alarm";
  1079. break;
  1080. case SYS_MODE_FAULT:
  1081. result = "fault";
  1082. break;
  1083. case SYS_MODE_MAINTAIN:
  1084. result = "maintain";
  1085. break;
  1086. case SYS_MODE_RESERVATION:
  1087. result = "reservation";
  1088. break;
  1089. case SYS_MODE_BOOKING:
  1090. result = "booking";
  1091. break;
  1092. case SYS_MODE_DEBUG:
  1093. result = "debug";
  1094. break;
  1095. case SYS_MODE_UPDATE:
  1096. result = "upgrade";
  1097. break;
  1098. default:
  1099. result = "unknown";
  1100. break;
  1101. }
  1102. return result;
  1103. }
  1104. void setChargerMode(unsigned char gun_index, unsigned char mode)
  1105. {
  1106. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  1107. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus = mode;
  1108. 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));
  1109. }
  1110. unsigned char isMode(unsigned char gun_index, unsigned char mode)
  1111. {
  1112. return ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == mode)?YES:NO);
  1113. }
  1114. unsigned char isModeChange(unsigned char gun_index)
  1115. {
  1116. unsigned char result = NO;
  1117. if(!isMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus))
  1118. {
  1119. result = YES;
  1120. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus;
  1121. }
  1122. return result;
  1123. }
  1124. //===============================================
  1125. // Get firmware version
  1126. //===============================================
  1127. void get_firmware_version(unsigned char gun_index)
  1128. {
  1129. FILE *fp;
  1130. char cmd[512];
  1131. char buf[512];
  1132. // Get CSU hardware version
  1133. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuHwRev, "REV.XXXXXXX");
  1134. // Get CSU boot loader version
  1135. memcpy(ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev, ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.CsuBootLoadFwRev));
  1136. // Get CSU kernel version
  1137. sprintf(cmd, "/bin/uname -r");
  1138. fp = popen(cmd, "r");
  1139. if(fp == NULL)
  1140. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, "Unknown version");
  1141. else
  1142. {
  1143. while(fgets(buf, sizeof(buf), fp) != NULL)
  1144. {
  1145. memcpy(ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev, buf, strlen(buf)-1);
  1146. }
  1147. }
  1148. // Get MCU firmware version
  1149. strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1150. // Get CSU root file system version
  1151. sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "V0.52.00.0000.00");
  1152. // Get AC connector type from model name
  1153. for(uint8_t idx=0;idx<3;idx++)
  1154. {
  1155. switch(ShmSysConfigAndInfo->SysConfig.ModelName[7+idx])
  1156. {
  1157. case '1':
  1158. // J1772 Plug
  1159. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '4';
  1160. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '4';
  1161. break;
  1162. case '2':
  1163. // J1772 Socket
  1164. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '1';
  1165. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '1';
  1166. break;
  1167. case '3':
  1168. // CE Plug
  1169. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '5';
  1170. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '5';
  1171. break;
  1172. case '4':
  1173. // CE Socket
  1174. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '2';
  1175. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '2';
  1176. break;
  1177. case '5':
  1178. // GB Plug
  1179. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '6';
  1180. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '6';
  1181. break;
  1182. case '6':
  1183. // GB Socket
  1184. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[6] = '3';
  1185. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[6] = '3';
  1186. break;
  1187. }
  1188. }
  1189. // Get network option from model name
  1190. switch(ShmSysConfigAndInfo->SysConfig.ModelName[10])
  1191. {
  1192. case 'B':
  1193. case 'U':
  1194. //Blue tooth
  1195. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '3';
  1196. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '3';
  1197. break;
  1198. case 'W':
  1199. // WIFI
  1200. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '1';
  1201. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '1';
  1202. break;
  1203. case 'T':
  1204. // 3G/4G
  1205. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '2';
  1206. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '2';
  1207. break;
  1208. case 'D':
  1209. // WIFI + 4G
  1210. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '5';
  1211. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '5';
  1212. break;
  1213. default:
  1214. // LAN
  1215. ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[9] = '0';
  1216. ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[9] = '0';
  1217. break;
  1218. }
  1219. // Get rating power from model name
  1220. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1221. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[10], &ShmSysConfigAndInfo->SysConfig.ModelName[4], 0x03);
  1222. // Get vender code from model name
  1223. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1224. memcpy(&ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev[14], &ShmSysConfigAndInfo->SysConfig.ModelName[12], 0x02);
  1225. DEBUG_INFO("========================================\n");
  1226. DEBUG_INFO("Model: %s\n", ShmSysConfigAndInfo->SysConfig.ModelName);
  1227. DEBUG_INFO("CSU hardware version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuHwRev);
  1228. DEBUG_INFO("CSU boot loader version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuBootLoadFwRev);
  1229. DEBUG_INFO("CSU kernel version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuKernelFwRev);
  1230. DEBUG_INFO("CSU root file system version: %s\n", ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  1231. DEBUG_INFO("CSU MCU-%2d firmware version: %s\n", gun_index, ShmCharger->gun_info[gun_index].ver.Version_FW);
  1232. DEBUG_INFO("========================================\n");
  1233. }
  1234. //===============================================
  1235. // Upgrade firmware
  1236. //===============================================
  1237. int upgrade_check()
  1238. {
  1239. int result = PASS;
  1240. int fd;
  1241. DIR *dir;
  1242. struct dirent *file;
  1243. char cmd[512];
  1244. long int MaxLen=48*1024*1024;
  1245. if ((dir = opendir ("/mnt")) != NULL)
  1246. {
  1247. /* print all the files and directories within directory */
  1248. while ((file = readdir (dir)) != NULL)
  1249. {
  1250. if(strlen(file->d_name)>2)
  1251. {
  1252. memset(&ShmCharger->fwUpgradeInfo, 0xFF, sizeof(Fw_Upgrade_Info));
  1253. DEBUG_INFO("New firmware file: %s\n", file->d_name);
  1254. sprintf(ShmCharger->fwUpgradeInfo.location, "/mnt/%s", file->d_name);
  1255. if((fd=open(ShmCharger->fwUpgradeInfo.location, O_RDONLY)) >= 0)
  1256. {
  1257. unsigned char *ptr = malloc(MaxLen); //-48 is take out the header
  1258. memset(ptr, 0xFF, MaxLen); //-48 is take out the header
  1259. read(fd, ptr, MaxLen);
  1260. close(fd);
  1261. ShmCharger->fwUpgradeInfo.fwType = ((ptr[0x13]<<0) | (ptr[0x12]<<8) | (ptr[0x11]<<16) | (ptr[0x10]<<24));
  1262. substr(ShmCharger->fwUpgradeInfo.modelName, (char *)ptr, 0, 0x10);
  1263. DEBUG_INFO("New firmware type: %X\n", ShmCharger->fwUpgradeInfo.fwType);
  1264. DEBUG_INFO("New firmware model name: %s, %s\n", ShmCharger->fwUpgradeInfo.modelName, ShmSysConfigAndInfo->SysConfig.ModelName);
  1265. if((strcmp(ShmCharger->fwUpgradeInfo.modelName, (char*)ShmSysConfigAndInfo->SysConfig.ModelName)==0) && (ShmCharger->fwUpgradeInfo.fwType>0))
  1266. {
  1267. switch(ShmCharger->fwUpgradeInfo.fwType)
  1268. {
  1269. case CSU_BOOTLOADER:
  1270. case CSU_KERNEL_CONFIGURATION:
  1271. case CSU_KERNEL_IMAGE:
  1272. case CSU_ROOT_FILE_SYSTEM:
  1273. case CSU_USER_CONFIGURATION:
  1274. case CSU_PRIMARY_CONTROLLER:
  1275. if(Upgrade_Flash(ShmCharger->fwUpgradeInfo.fwType, ShmCharger->fwUpgradeInfo.location, ShmCharger->fwUpgradeInfo.modelName) != PASS)
  1276. {
  1277. result = FAIL;
  1278. }
  1279. else
  1280. {
  1281. if(ShmCharger->fwUpgradeInfo.fwType == CSU_USER_CONFIGURATION)
  1282. {
  1283. DEBUG_INFO("Restore model name & serial number.\n");
  1284. memcpy(&SysConfigOrg, &ShmSysConfigAndInfo->SysConfig, sizeof(struct SysConfigData));
  1285. if(LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1286. {
  1287. DEBUG_INFO("Re-load configuration fail.\n");
  1288. result = FAIL;
  1289. }
  1290. else
  1291. {
  1292. memcpy(&ShmSysConfigAndInfo->SysConfig.ModelName, &SysConfigOrg.ModelName, ARRAY_SIZE(SysConfigOrg.ModelName));
  1293. memcpy(&ShmSysConfigAndInfo->SysConfig.SerialNumber, &SysConfigOrg.SerialNumber, ARRAY_SIZE(SysConfigOrg.SerialNumber));
  1294. memcpy(&ShmSysConfigAndInfo->SysConfig.SystemId, &SysConfigOrg.SystemId, ARRAY_SIZE(SysConfigOrg.SystemId));
  1295. if(StoreUsrConfigData(&ShmSysConfigAndInfo->SysConfig) != PASS)
  1296. {
  1297. DEBUG_INFO("Re-write configuration fail.\n");
  1298. result = FAIL;
  1299. }
  1300. else
  1301. DEBUG_INFO("Re-write configuration OK.\n");
  1302. }
  1303. }
  1304. }
  1305. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1306. system(cmd);
  1307. break;
  1308. case AC_WALLMOUNT_CONTROLLER:
  1309. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  1310. ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq = ON;
  1311. break;
  1312. default:
  1313. result = FAIL;
  1314. DEBUG_WARN("Image file is unknown type.\n");
  1315. sprintf(cmd, "yes|rm %s", ShmCharger->fwUpgradeInfo.location);
  1316. system(cmd);
  1317. break;
  1318. }
  1319. }
  1320. else
  1321. {
  1322. result = FAIL;
  1323. DEBUG_ERROR("Model name and Firmware type error.\n");
  1324. }
  1325. free(ptr);
  1326. }
  1327. else
  1328. {
  1329. result = FAIL;
  1330. DEBUG_ERROR("New firmware open error.\n");
  1331. }
  1332. }
  1333. else
  1334. {
  1335. if(strlen(file->d_name) >= 3)
  1336. {
  1337. result = FAIL;
  1338. DEBUG_ERROR("File name error.\n");
  1339. }
  1340. else
  1341. {
  1342. DEBUG_ERROR("Searching file.\n");
  1343. }
  1344. }
  1345. }
  1346. closedir (dir);
  1347. }
  1348. else
  1349. {
  1350. result = FAIL;
  1351. DEBUG_ERROR("/mnt does not valid.\n");
  1352. }
  1353. return result;
  1354. }
  1355. //===============================================
  1356. // Check RFID is match with start user
  1357. //===============================================
  1358. int isMatchStartUser(unsigned char gun_index)
  1359. {
  1360. uint8_t tmpUser[32];
  1361. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  1362. {
  1363. // Big endian
  1364. switch(rfid.snType)
  1365. {
  1366. case RFID_SN_TYPE_6BYTE:
  1367. 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]);
  1368. break;
  1369. case RFID_SN_TYPE_7BYTE:
  1370. 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]);
  1371. break;
  1372. case RFID_SN_TYPE_10BYTE:
  1373. 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]);
  1374. break;
  1375. case RFID_SN_TYPE_4BYTE:
  1376. default:
  1377. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  1378. break;
  1379. }
  1380. }
  1381. else
  1382. {
  1383. // Little endian
  1384. switch(rfid.snType)
  1385. {
  1386. case RFID_SN_TYPE_6BYTE:
  1387. 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]);
  1388. break;
  1389. case RFID_SN_TYPE_7BYTE:
  1390. 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]);
  1391. break;
  1392. case RFID_SN_TYPE_10BYTE:
  1393. 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]);
  1394. break;
  1395. case RFID_SN_TYPE_4BYTE:
  1396. default:
  1397. sprintf((char*)tmpUser, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  1398. break;
  1399. }
  1400. }
  1401. return ((strcmp((char*)tmpUser, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId)==0)?YES:NO);
  1402. }
  1403. //===============================================
  1404. // Read RFID Serial Number
  1405. //===============================================
  1406. int GetCardSerialNumber()
  1407. {
  1408. int isSuccess = FAIL;
  1409. int module_type = MODULE_EWT;
  1410. if(getRequestCardSN(rfidFd,module_type,&rfid))
  1411. {
  1412. if(rfid.cardType == ISO14443A)
  1413. {
  1414. if(rfid.snType == RFID_SN_TYPE_4BYTE)
  1415. {
  1416. isSuccess = PASS;
  1417. }
  1418. else if (rfid.snType == RFID_SN_TYPE_7BYTE)
  1419. {
  1420. isSuccess = PASS;
  1421. }
  1422. sethaltCard(rfidFd,module_type);
  1423. }
  1424. else if(rfid.cardType == IS014443B)
  1425. {
  1426. isSuccess = PASS;
  1427. }
  1428. else if(rfid.cardType == FELICA)
  1429. {
  1430. isSuccess = PASS;
  1431. }
  1432. else
  1433. {}
  1434. }
  1435. else
  1436. {}
  1437. return isSuccess;
  1438. }
  1439. //===============================================
  1440. // Set led motion
  1441. //===============================================
  1442. void setLedMotion(unsigned char gun_index,unsigned char led_mode)
  1443. {
  1444. switch(led_mode)
  1445. {
  1446. case LED_ACTION_INIT:
  1447. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INIT;
  1448. break;
  1449. case LED_ACTION_IDLE:
  1450. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_IDLE;
  1451. break;
  1452. case LED_ACTION_AUTHED:
  1453. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_AUTHED;
  1454. break;
  1455. case LED_ACTION_CONNECTED:
  1456. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CONNECTED;
  1457. break;
  1458. case LED_ACTION_CHARGING:
  1459. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_CHARGING;
  1460. break;
  1461. case LED_ACTION_STOP:
  1462. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_STOP;
  1463. break;
  1464. case LED_ACTION_ALARM:
  1465. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALARM;
  1466. break;
  1467. case LED_ACTION_MAINTAIN:
  1468. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_MAINTAIN;
  1469. break;
  1470. case LED_ACTION_RFID_PASS:
  1471. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_PASS;
  1472. break;
  1473. case LED_ACTION_RFID_FAIL:
  1474. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_RFID_FAIL;
  1475. break;
  1476. case LED_ACTION_BLE_CONNECT:
  1477. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_CONNECT;
  1478. break;
  1479. case LED_ACTION_BLE_DISABLE:
  1480. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_BLE_DISABLE;
  1481. break;
  1482. case LED_ACTION_DEBUG:
  1483. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_DEBUG;
  1484. break;
  1485. case LED_ACTION_ALL_OFF:
  1486. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_ALL_OFF;
  1487. break;
  1488. case LED_RELAY_ON:
  1489. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_ON;
  1490. break;
  1491. case LED_RELAY_OFF:
  1492. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_RELAY_OFF;
  1493. break;
  1494. case LED_ACTION_HANDSHAKE_FAIL:
  1495. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_HANDSHAKE_FAIL;
  1496. break;
  1497. case LED_ACTION_INTERNET_DISCONNECT:
  1498. ShmCharger->gun_info[gun_index].primaryMcuLed.mode = LED_ACTION_INTERNET_DISCONNECT;
  1499. break;
  1500. }
  1501. }
  1502. //===============================================
  1503. // Relay on/off request set
  1504. //===============================================
  1505. void setRelay(unsigned char gun_index,unsigned char isOn)
  1506. {
  1507. if(isOn == ON)
  1508. {
  1509. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == OFF)
  1510. {
  1511. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = ON;
  1512. DEBUG_INFO("Gun-%d Output relay sts: ON \n",gun_index);
  1513. }
  1514. }
  1515. else
  1516. {
  1517. if(ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON)
  1518. {
  1519. ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest = OFF;
  1520. DEBUG_INFO("Gun-%d Output relay sts: OFF \n",gun_index);
  1521. }
  1522. }
  1523. }
  1524. //===============================================
  1525. // Relay on/off request get
  1526. //===============================================
  1527. int getRelay(unsigned char gun_index)
  1528. {
  1529. return ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest;
  1530. }
  1531. //===============================================
  1532. // Set speaker on/off request
  1533. //===============================================
  1534. void setSpeaker(unsigned char isOn, unsigned char speaker_mode)
  1535. {
  1536. if(isOn == ON)
  1537. {
  1538. ShmCharger->isSpeakerOn = ON;
  1539. ShmCharger->speaker_type = speaker_mode;
  1540. }
  1541. }
  1542. //===============================================
  1543. // Initialization RFID communication port
  1544. //===============================================
  1545. int InitRfidPort()
  1546. {
  1547. int uartO2 = open(rfidPortName, O_RDWR);
  1548. struct termios tios;
  1549. if (uartO2 != FAIL)
  1550. {
  1551. ioctl (uartO2, TCGETS, &tios);
  1552. tios.c_cflag = B19200 | CS8 | CLOCAL | CREAD;
  1553. tios.c_lflag = 0;
  1554. tios.c_iflag = 0;
  1555. tios.c_oflag = 0;
  1556. tios.c_cc[VMIN] = 0;
  1557. tios.c_cc[VTIME] = (unsigned char) 1;
  1558. tios.c_lflag = 0;
  1559. tcflush(uartO2, TCIFLUSH);
  1560. ioctl(uartO2, TCSETS, &tios);
  1561. }
  1562. if (uartO2 < 0)
  1563. {
  1564. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.RfidModuleCommFail = 1;
  1565. }
  1566. return uartO2;
  1567. }
  1568. //===============================================
  1569. // Check internet access status
  1570. //===============================================
  1571. int isReachableInternet()
  1572. {
  1573. int result = FAIL;
  1574. FILE *fp;
  1575. char cmd[256];
  1576. char buf[512];
  1577. char tmp[512];
  1578. // Get ip address & net mask
  1579. strcpy(cmd, "ifconfig eth0");
  1580. fp = popen(cmd, "r");
  1581. if(fp != NULL)
  1582. {
  1583. while(fgets(buf, sizeof(buf), fp) != NULL)
  1584. {
  1585. if (strstr(buf, "inet addr:") > 0)
  1586. {
  1587. sscanf(buf, "%*s%s", tmp);
  1588. substr(tmp, tmp, strspn(tmp, "addr:"), strlen(buf)-strspn(tmp, "addr:"));
  1589. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress) != 0)
  1590. {
  1591. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress, tmp);
  1592. }
  1593. sscanf(buf, "%*s%*s%*s%s", tmp);
  1594. substr(tmp, tmp, strspn(tmp, "Mask:"), strlen(buf)-strspn(tmp, "Mask:"));
  1595. if (strcmp(tmp, (char *)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress) != 0)
  1596. {
  1597. strcpy((char *) ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress, tmp);
  1598. }
  1599. }
  1600. }
  1601. }
  1602. pclose(fp);
  1603. memset(buf, 0x00, sizeof(buf));
  1604. // Get gateway
  1605. fp = popen("ip route", "r");
  1606. if(fp == NULL)
  1607. result = FAIL;
  1608. else
  1609. {
  1610. while(fgets(buf, sizeof(buf), fp) != NULL)
  1611. {
  1612. if((strstr(buf, "default") != NULL) && (strstr(buf, "eth0") != NULL))
  1613. break;
  1614. }
  1615. if(strstr(buf, "default") != NULL)
  1616. {
  1617. sscanf(buf, "%*s%*s%s", tmp);
  1618. substr((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress, tmp, 0, strlen(tmp));
  1619. }
  1620. }
  1621. pclose(fp);
  1622. memset(buf, 0x00, sizeof(buf));
  1623. for(int idx=0;idx<ARRAY_SIZE(valid_Internet);idx++)
  1624. {
  1625. sprintf(cmd, "ping -c 1 -w 3 -I eth0 %s", valid_Internet[idx]);
  1626. fp = popen(cmd, "r");
  1627. if(fp != NULL)
  1628. {
  1629. while(fgets(buf, sizeof(buf), fp) != NULL)
  1630. {
  1631. if(strstr(buf, "transmitted") > 0)
  1632. {
  1633. //sscanf(buf, "%*s%*s%*s%*s%*s%*s%s", tmp);
  1634. if(strstr(buf,"100%") != NULL)
  1635. {
  1636. }
  1637. else
  1638. {
  1639. result = PASS;
  1640. }
  1641. //DEBUG_INFO("%s",buf);
  1642. //DEBUG_INFO("%s\n",tmp);
  1643. }
  1644. }
  1645. }
  1646. pclose(fp);
  1647. }
  1648. return result;
  1649. }
  1650. //===============================================
  1651. // Check route for eth0
  1652. //===============================================
  1653. int isRouteFail()
  1654. {
  1655. int result = YES;
  1656. FILE *fp;
  1657. char buf[512];
  1658. fp = popen("route -n", "r");
  1659. if(fp != NULL)
  1660. {
  1661. while(fgets(buf, sizeof(buf), fp) != NULL)
  1662. {
  1663. if(strstr(buf, "eth0") != NULL)
  1664. result = NO;
  1665. }
  1666. }
  1667. pclose(fp);
  1668. return result;
  1669. }
  1670. //===============================================
  1671. // Check reservation date is expired
  1672. //===============================================
  1673. int isReservationExpired(unsigned char gun_index)
  1674. {
  1675. int result = NO;
  1676. struct tm expiredDate;
  1677. struct timeb expiredTime;
  1678. if(sscanf((char*)ShmOCPP16Data->ReserveNow[gun_index].ExpiryDate, "%4d-%2d-%2dT%2d:%2d:%2d", &expiredDate.tm_year, &expiredDate.tm_mon, &expiredDate.tm_mday, &expiredDate.tm_hour, &expiredDate.tm_min, &expiredDate.tm_sec) == 6)
  1679. {
  1680. expiredDate.tm_year -= 1900;
  1681. expiredDate.tm_mon -= 1;
  1682. expiredTime.time = mktime(&expiredDate);
  1683. if(DiffTimebWithNow(expiredTime)>=0)
  1684. {
  1685. result = YES;
  1686. }
  1687. }
  1688. else
  1689. {
  1690. DEBUG_WARN("Expired date parsing error.\n");
  1691. DEBUG_INFO("Date expired.\n");
  1692. result = YES;
  1693. }
  1694. return result;
  1695. }
  1696. //===============================================
  1697. // Check charging profile related date routine
  1698. //===============================================
  1699. int isProfileValid(uint8_t gun_index)
  1700. {
  1701. int result = NO;
  1702. struct tm tmFrom, tmTo;
  1703. struct timeb tbFrom, tbTo;
  1704. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom, "%4d-%2d-%2dT%2d:%2d:%2d", &tmFrom.tm_year, &tmFrom.tm_mon, &tmFrom.tm_mday, &tmFrom.tm_hour, &tmFrom.tm_min, &tmFrom.tm_sec) == 6) &&
  1705. (sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo, "%4d-%2d-%2dT%2d:%2d:%2d", &tmTo.tm_year, &tmTo.tm_mon, &tmTo.tm_mday, &tmTo.tm_hour, &tmTo.tm_min, &tmTo.tm_sec) == 6))
  1706. {
  1707. tmFrom.tm_year -= 1900;
  1708. tmFrom.tm_mon -= 1;
  1709. tbFrom.time = mktime(&tmFrom);
  1710. tmTo.tm_year -= 1900;
  1711. tmTo.tm_mon -= 1;
  1712. tbTo.time = mktime(&tmTo);
  1713. DEBUG_INFO("Valid from compare Now: %d\n", DiffTimebWithNow(tbFrom));
  1714. DEBUG_INFO("Valid to compare Now: %d\n", DiffTimebWithNow(tbTo));
  1715. if((DiffTimebWithNow(tbFrom)>=0) && (DiffTimebWithNow(tbTo)<=0))
  1716. {
  1717. result = YES;
  1718. }
  1719. }
  1720. else
  1721. {
  1722. DEBUG_WARN("ValidFrom or ValidTo date parsing error.\n");
  1723. }
  1724. return result;
  1725. }
  1726. int getScheduleStart(int gun_index)
  1727. {
  1728. int result = -1;
  1729. struct tm tmScheduleStart;;
  1730. struct timeb tbScheduleStart;
  1731. if((sscanf((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule, "%4d-%2d-%2dT%2d:%2d:%2d", &tmScheduleStart.tm_year, &tmScheduleStart.tm_mon, &tmScheduleStart.tm_mday, &tmScheduleStart.tm_hour, &tmScheduleStart.tm_min, &tmScheduleStart.tm_sec) == 6))
  1732. {
  1733. tmScheduleStart.tm_year -= 1900;
  1734. tmScheduleStart.tm_mon -= 1;
  1735. tbScheduleStart.time = mktime(&tmScheduleStart);
  1736. tbScheduleStart.millitm = 0;
  1737. result = DiffTimebWithNow(tbScheduleStart)/1000;
  1738. //DEBUG_INFO("Schedule start compare Now(seconds): %d\n", result);
  1739. }
  1740. else
  1741. {
  1742. DEBUG_WARN("Schedule start date parsing error.\n");
  1743. }
  1744. return result;
  1745. }
  1746. int getStartSinceToday()
  1747. {
  1748. int result = -1;
  1749. time_t t;
  1750. struct tm *tmStartToday;
  1751. struct timeb tbStartToday;
  1752. t=time(NULL);
  1753. tmStartToday=localtime(&t);
  1754. tmStartToday->tm_hour = 0;
  1755. tmStartToday->tm_min = 0;
  1756. tmStartToday->tm_sec = 0;
  1757. tbStartToday.time = mktime(tmStartToday);
  1758. result = DiffTimebWithNow(tbStartToday)/1000;
  1759. //DEBUG_INFO("Start today compare Now(seconds): %d\n", result);
  1760. return result;
  1761. }
  1762. int getStartSinceWeek()
  1763. {
  1764. int result = -1;
  1765. time_t t;
  1766. struct tm *tmStartWeek;
  1767. struct timeb tbStartWeek;
  1768. t=time(NULL);
  1769. tmStartWeek=localtime(&t);
  1770. t-=86400*tmStartWeek->tm_wday;
  1771. tmStartWeek=localtime(&t);
  1772. tmStartWeek->tm_hour = 0;
  1773. tmStartWeek->tm_min = 0;
  1774. tmStartWeek->tm_sec = 0;
  1775. tbStartWeek.time = mktime(tmStartWeek);
  1776. result = DiffTimebWithNow(tbStartWeek)/1000;
  1777. //DEBUG_INFO("Start week compare Now(seconds): %d\n", result);
  1778. return result;
  1779. }
  1780. //===============================================
  1781. // Valid from local white list
  1782. //===============================================
  1783. int isValidLocalWhiteCard()
  1784. {
  1785. uint8_t result = FAIL;
  1786. for(uint8_t idx=0;idx<ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.LocalWhiteCard);idx++)
  1787. {
  1788. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmSysConfigAndInfo->SysConfig.LocalWhiteCard[idx]) == 0)
  1789. {
  1790. result = PASS;
  1791. }
  1792. }
  1793. return result;
  1794. }
  1795. //==========================================
  1796. // Get Ethernet MAC address
  1797. //==========================================
  1798. int getEth0MacAddress()
  1799. {
  1800. int result = PASS;
  1801. FILE *fp;
  1802. char cmd[256];
  1803. char buf[512];
  1804. char tmp[512];
  1805. strcpy(cmd, "ifconfig eth0");
  1806. fp = popen(cmd, "r");
  1807. if(fp != NULL)
  1808. {
  1809. while(fgets(buf, sizeof(buf), fp) != NULL)
  1810. {
  1811. if(strstr(buf, "eth0") > 0)
  1812. {
  1813. result = PASS;
  1814. }
  1815. if(strstr(buf, "eth0 Link encap:Ethernet HWaddr") > 0)
  1816. {
  1817. sscanf(buf, "%*s%*s%*s%*s%s", tmp);
  1818. strcpy((char*)ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthMacAddress, tmp);
  1819. }
  1820. }
  1821. }
  1822. pclose(fp);
  1823. return result;
  1824. }
  1825. //==========================================
  1826. // Check task processing
  1827. //==========================================
  1828. void checkTask()
  1829. {
  1830. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'T') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1831. {
  1832. if(system("pidof -s Module_4g > /dev/null") != 0)
  1833. {
  1834. DEBUG_INFO("Module_4g not running, restart it.\n");
  1835. system("/root/Module_4g &");
  1836. }
  1837. }
  1838. if((ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'W') || (ShmSysConfigAndInfo->SysConfig.ModelName[10] == 'D'))
  1839. {
  1840. if(system("pidof -s Module_Wifi > /dev/null") != 0)
  1841. {
  1842. DEBUG_INFO("Module_Wifi not running, restart it.\n");
  1843. system("/root/Module_Wifi &");
  1844. }
  1845. }
  1846. if(system("pidof -s Module_EventLogging > /dev/null") != 0)
  1847. {
  1848. DEBUG_INFO("Module_EventLogging not running, restart it.\n");
  1849. system("/root/Module_EventLogging &");
  1850. }
  1851. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") != 0)
  1852. {
  1853. if((time((time_t*)NULL) - ShmOCPP16Data->procDogTime) > 180)
  1854. {
  1855. DEBUG_WARN("OcppBackend watch dog timeout task restart.\n");
  1856. ShmOCPP16Data->procDogTime = time((time_t*)NULL);
  1857. system("pkill OcppBackend");
  1858. }
  1859. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1860. {
  1861. DEBUG_INFO("OcppBackend not running, restart it.\n");
  1862. system("/root/OcppBackend &");
  1863. }
  1864. }
  1865. if(system("pidof -s Module_AlarmDetect > /dev/null") != 0)
  1866. {
  1867. DEBUG_INFO("Module_AlarmDetect not running, restart it.\n");
  1868. system("/root/Module_AlarmDetect &");
  1869. }
  1870. if(system("pidof -s Module_InternalComm > /dev/null") != 0)
  1871. {
  1872. DEBUG_INFO("Module_InternalComm not running, restart it.\n");
  1873. system("/root/Module_InternalComm &");
  1874. }
  1875. if(system("pidof -s Module_Speaker > /dev/null") != 0)
  1876. {
  1877. DEBUG_INFO("Module_Speaker not running, restart it.\n");
  1878. system("/root/Module_Speaker &");
  1879. }
  1880. if(system("pidof -s Module_ProduceUtils > /dev/null") != 0)
  1881. {
  1882. DEBUG_INFO("Module_ProduceUtils not running, restart it.\n");
  1883. system ("/root/Module_ProduceUtils &");
  1884. }
  1885. }
  1886. //===============================================
  1887. // Check Connection Timeout
  1888. //===============================================
  1889. void checkConnectionTimeout()
  1890. {
  1891. if(system("pidof -s OcppBackend > /dev/null") != 0)
  1892. {
  1893. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1894. //DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
  1895. }
  1896. else
  1897. {
  1898. if(strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData,"") != 0)
  1899. {
  1900. ShmCharger->timeoutSpec.Setting_Timeout_Spec = atoi((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  1901. if(ShmCharger->timeoutSpec.Setting_Timeout_Spec <= 0)
  1902. {
  1903. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1904. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
  1905. }
  1906. else
  1907. {
  1908. ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
  1909. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
  1910. }
  1911. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
  1912. }
  1913. else
  1914. {
  1915. ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
  1916. //DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Table is blank...\n.");
  1917. }
  1918. //DEBUG_INFO("Present timeout spec : %d \n", ShmCharger->timeoutSpec.Present_Timeout_Spec);
  1919. }
  1920. }
  1921. //===============================================
  1922. // Set Light Internsity from ocpp
  1923. //===============================================
  1924. void setLightIntensity(unsigned char gun_index)
  1925. {
  1926. switch(atoi((char*)ShmOCPP16Data->ConfigurationTable.CoreProfile[LightIntensity].ItemData))
  1927. {
  1928. case 0 ... 15:
  1929. if(ShmCharger->gun_info[gun_index].LightIntensityLevel != 0x00)
  1930. {
  1931. // 12 sector change to level 0
  1932. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0x00;
  1933. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0x00;
  1934. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0x00;
  1935. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0x00;
  1936. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0x00;
  1937. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0x00;
  1938. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0x00;
  1939. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0x00;
  1940. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0x00;
  1941. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0x00;
  1942. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0x00;
  1943. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0x00;
  1944. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0x00;
  1945. ShmCharger->gun_info[gun_index].isSetLedBrightness = ON;
  1946. DEBUG_INFO("Light intensity level : [%X] == Level 0.\r\n", ShmCharger->gun_info[gun_index].LightIntensityLevel);
  1947. }
  1948. break;
  1949. case 16 ... 35:
  1950. if(ShmCharger->gun_info[gun_index].LightIntensityLevel != 0x11)
  1951. {
  1952. // 12 sector change to level 1
  1953. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0x11;
  1954. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0x11;
  1955. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0x11;
  1956. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0x11;
  1957. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0x11;
  1958. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0x11;
  1959. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0x11;
  1960. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0x11;
  1961. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0x11;
  1962. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0x11;
  1963. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0x11;
  1964. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0x11;
  1965. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0x11;
  1966. ShmCharger->gun_info[gun_index].isSetLedBrightness = ON;
  1967. DEBUG_INFO("Light intensity level : [%X] == Level 1.\r\n", ShmCharger->gun_info[gun_index].LightIntensityLevel);
  1968. }
  1969. break;
  1970. case 36 ... 75:
  1971. if(ShmCharger->gun_info[gun_index].LightIntensityLevel != 0x22)
  1972. {
  1973. // 12 sector change to level 2
  1974. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0x22;
  1975. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0x22;
  1976. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0x22;
  1977. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0x22;
  1978. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0x22;
  1979. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0x22;
  1980. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0x22;
  1981. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0x22;
  1982. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0x22;
  1983. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0x22;
  1984. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0x22;
  1985. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0x22;
  1986. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0x22;
  1987. ShmCharger->gun_info[gun_index].isSetLedBrightness = ON;
  1988. DEBUG_INFO("Light intensity level : [%X] == Level 2.\r\n", ShmCharger->gun_info[gun_index].LightIntensityLevel);
  1989. }
  1990. break;
  1991. case 76 ... 100:
  1992. if(ShmCharger->gun_info[gun_index].LightIntensityLevel != 0x33)
  1993. {
  1994. // 12 sector change to level 3
  1995. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0x33;
  1996. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0x33;
  1997. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0x33;
  1998. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0x33;
  1999. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0x33;
  2000. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0x33;
  2001. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0x33;
  2002. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0x33;
  2003. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0x33;
  2004. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0x33;
  2005. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0x33;
  2006. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0x33;
  2007. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0x33;
  2008. ShmCharger->gun_info[gun_index].isSetLedBrightness = ON;
  2009. DEBUG_INFO("Light intensity level : [%X] == Level 3.\r\n", ShmCharger->gun_info[gun_index].LightIntensityLevel);
  2010. }
  2011. break;
  2012. default:
  2013. if(ShmCharger->gun_info[gun_index].LightIntensityLevel != 2)
  2014. {
  2015. // 12 sector change to level 2
  2016. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0x22;
  2017. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0x22;
  2018. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0x22;
  2019. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0x22;
  2020. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0x22;
  2021. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0x22;
  2022. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0x22;
  2023. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0x22;
  2024. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0x22;
  2025. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0x22;
  2026. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0x22;
  2027. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0x22;
  2028. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0x22;
  2029. ShmCharger->gun_info[gun_index].isSetLedBrightness = ON;
  2030. DEBUG_INFO("Default light intensity level : [%X] == Level 2.\r\n", ShmCharger->gun_info[gun_index].LightIntensityLevel);
  2031. }
  2032. break;
  2033. }
  2034. }
  2035. void initialLightIntensity(unsigned char gun_index)
  2036. {
  2037. ShmCharger->gun_info[gun_index].setLedBrightness.sector_1 = 0xff;
  2038. ShmCharger->gun_info[gun_index].setLedBrightness.sector_2 = 0xff;
  2039. ShmCharger->gun_info[gun_index].setLedBrightness.sector_3 = 0xff;
  2040. ShmCharger->gun_info[gun_index].setLedBrightness.sector_4 = 0xff;
  2041. ShmCharger->gun_info[gun_index].setLedBrightness.sector_5 = 0xff;
  2042. ShmCharger->gun_info[gun_index].setLedBrightness.sector_6 = 0xff;
  2043. ShmCharger->gun_info[gun_index].setLedBrightness.sector_7 = 0xff;
  2044. ShmCharger->gun_info[gun_index].setLedBrightness.sector_8 = 0xff;
  2045. ShmCharger->gun_info[gun_index].setLedBrightness.sector_9 = 0xff;
  2046. ShmCharger->gun_info[gun_index].setLedBrightness.sector_10 = 0xff;
  2047. ShmCharger->gun_info[gun_index].setLedBrightness.sector_11 = 0xff;
  2048. ShmCharger->gun_info[gun_index].setLedBrightness.sector_12 = 0xff;
  2049. ShmCharger->gun_info[gun_index].LightIntensityLevel = 0xff;
  2050. DEBUG_INFO("Initial light intensity gun_index %d to : [%X] .\r\n", gun_index, ShmCharger->gun_info[0].LightIntensityLevel);
  2051. }
  2052. //===============================================
  2053. // Main process
  2054. //===============================================
  2055. int main(void)
  2056. {
  2057. //Create all share memory
  2058. if(CreatShareMemory()==0)
  2059. {
  2060. DEBUG_ERROR("CreatShareMemory NG\n");
  2061. if(ShmStatusCodeData!=NULL)
  2062. {
  2063. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory=1;
  2064. }
  2065. sleep(5);
  2066. system("reboot -f");
  2067. sleep(5);
  2068. system("reboot -f");
  2069. }
  2070. else
  2071. {
  2072. DEBUG_INFO("CreatShareMemory OK\n");
  2073. }
  2074. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2075. {
  2076. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus = 0xff;
  2077. ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch = 0xff;
  2078. ShmCharger->gun_info[gun_index].mcuResetRequest.isMcuResetRequest = ON;
  2079. ShmCharger->gun_info[gun_index].isInitialPass = NO;
  2080. ShmCharger->gun_info[gun_index].isSetBreatheLedTiming = OFF;
  2081. ShmCharger->gun_info[gun_index].isSetLedBrightness = OFF;
  2082. initialLightIntensity(gun_index);
  2083. }
  2084. // Main loop
  2085. for(;;)
  2086. {
  2087. //==========================================
  2088. // Synchronize share memory from OCPP struct
  2089. //==========================================
  2090. ShmSysConfigAndInfo->SysInfo.OcppConnStatus = ShmOCPP16Data->OcppConnStatus;
  2091. //==========================================
  2092. // Ethernet MAC address
  2093. //==========================================
  2094. getEth0MacAddress();
  2095. //==========================================
  2096. // Check task processing
  2097. //==========================================
  2098. checkTask();
  2099. //==========================================
  2100. // Check connection timeout specification
  2101. //==========================================
  2102. checkConnectionTimeout();
  2103. //==========================================
  2104. // Something need run in Idle mode
  2105. //==========================================
  2106. if(((ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus==SYS_MODE_ALARM)) &&
  2107. (AC_QUANTITY>1?((ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[1].SystemStatus==SYS_MODE_ALARM)):true))
  2108. {
  2109. //======================================
  2110. // Check restore factory setting request
  2111. //======================================
  2112. if(ShmSysConfigAndInfo->SysInfo.FactoryConfiguration)
  2113. {
  2114. // Set led to initial
  2115. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2116. {
  2117. setLedMotion(gun_index,LED_ACTION_INIT);
  2118. }
  2119. system("cd /root;./Module_FactoryConfig -m");
  2120. system("rm -f /Storage/OCPP/OCPPConfiguration");
  2121. system("sync");
  2122. sleep(5);
  2123. system("reboot -f");
  2124. sleep(5);
  2125. system("reboot -f");
  2126. }
  2127. //======================================
  2128. // Check upgrade firmware request
  2129. //======================================
  2130. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate ||
  2131. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq)
  2132. {
  2133. ShmCharger->isUpdateSuccess = NO;
  2134. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2135. {
  2136. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  2137. setChargerMode(gun_index, SYS_MODE_UPDATE);
  2138. }
  2139. }
  2140. }
  2141. //==========================================
  2142. // Check remote reset request
  2143. //==========================================
  2144. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  2145. {
  2146. if((!isMode(0, SYS_MODE_CHARGING) && !isMode(0, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)) &&
  2147. (AC_QUANTITY>1?(!isMode(1, SYS_MODE_CHARGING) && !isMode(1, SYS_MODE_TERMINATING) && !isMode(0, SYS_MODE_COMPLETE)):true))
  2148. {
  2149. ShmOCPP16Data->MsMsg.bits.ResetReq = OFF;
  2150. sprintf((char*)ShmOCPP16Data->Reset.ResponseStatus, "Accepted");
  2151. ShmOCPP16Data->MsMsg.bits.ResetConf = ON;
  2152. DEBUG_INFO("%s reset request by OCPP.\n", ShmOCPP16Data->Reset.Type);
  2153. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2154. setChargerMode(gun_index, SYS_MODE_BOOTING);
  2155. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard") == 0)
  2156. {
  2157. system("sync");
  2158. sleep(5);
  2159. system("reboot -f");
  2160. sleep(5);
  2161. system("reboot -f");
  2162. }
  2163. else
  2164. {
  2165. sleep(5);
  2166. close(wtdFd);
  2167. system("/usr/bin/run_evse_restart.sh");
  2168. }
  2169. }
  2170. }
  2171. //==========================================
  2172. // Check RFID
  2173. //==========================================
  2174. if(!ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq)
  2175. {
  2176. if(GetCardSerialNumber()!= FAIL)
  2177. {
  2178. ShmCharger->gun_info[ShmCharger->gun_selectd].rfidReq = ON;
  2179. }
  2180. }
  2181. //==========================================
  2182. // Connector loop
  2183. //==========================================
  2184. for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
  2185. {
  2186. // Synchronize current rating value from MCU
  2187. ShmSysConfigAndInfo->SysConfig.RatingCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  2188. // Assign connector location index for OCPP
  2189. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].Index = gun_index;
  2190. // Synchronize present charging power
  2191. if(ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON)
  2192. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = (ShmSysConfigAndInfo->SysInfo.InputVoltageR* ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent)/1000;
  2193. else
  2194. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingPower = 0;
  2195. // The system switch to Booting mode
  2196. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == 0XFF)
  2197. {
  2198. setChargerMode(gun_index, SYS_MODE_BOOTING);
  2199. }
  2200. // Set light intensity
  2201. setLightIntensity(gun_index);
  2202. // Check initialization "PASS" or "FAIL"
  2203. if(ShmCharger->gun_info[gun_index].isInitialPass == YES)
  2204. {
  2205. // Alarm event check
  2206. if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0))
  2207. {
  2208. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
  2209. {
  2210. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE)
  2211. {
  2212. }
  2213. else
  2214. {
  2215. setChargerMode(gun_index, SYS_MODE_ALARM);
  2216. }
  2217. }
  2218. }
  2219. else
  2220. {
  2221. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM)
  2222. {
  2223. }
  2224. }
  2225. }
  2226. // Reservation request check
  2227. if(ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq)
  2228. {
  2229. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowReq = OFF;
  2230. if(isMode(gun_index, SYS_MODE_IDLE) && !isReservationExpired(gun_index))
  2231. {
  2232. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = ShmOCPP16Data->ReserveNow[gun_index].ReservationId;
  2233. setChargerMode(gun_index, SYS_MODE_RESERVATION);
  2234. }
  2235. DEBUG_INFO("Reservation request on connector-%d.\n", gun_index);
  2236. ShmOCPP16Data->CsMsg.bits[gun_index].ReserveNowConf = ON;
  2237. }
  2238. // Change availability check
  2239. if(ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq)
  2240. {
  2241. if(strcmp((char*)ShmOCPP16Data->ChangeAvailability[gun_index].Type, "Operative") == 0)
  2242. {
  2243. DB_Update_Operactive(localDb, gun_index, true);
  2244. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2245. }
  2246. else
  2247. {
  2248. DB_Update_Operactive(localDb, gun_index, false);
  2249. ShmCharger->gun_info[gun_index].isOperactive = DB_Get_Operactive(localDb, gun_index);
  2250. }
  2251. ShmOCPP16Data->CsMsg.bits[gun_index].ChangeAvailabilityReq = OFF;
  2252. }
  2253. if(ShmCharger->gun_info[gun_index].isOperactive)
  2254. {
  2255. if(isMode(gun_index, SYS_MODE_MAINTAIN))
  2256. {
  2257. setChargerMode(gun_index, SYS_MODE_IDLE);
  2258. }
  2259. }
  2260. else
  2261. {
  2262. if(isMode(gun_index, SYS_MODE_IDLE))
  2263. {
  2264. setChargerMode(gun_index, SYS_MODE_MAINTAIN);
  2265. }
  2266. }
  2267. // Unlock Connector signal check
  2268. if(ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq == ON)
  2269. {
  2270. ShmOCPP16Data->CsMsg.bits[gun_index].UnlockConnectorReq = OFF;
  2271. sprintf((char*)ShmOCPP16Data->UnlockConnector[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].ResponseStatus, "NotSupported");
  2272. ShmOCPP16Data->CsMsg.bits[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].UnlockConnectorConf = ON;
  2273. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = ON;
  2274. }
  2275. // Connector process
  2276. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
  2277. {
  2278. case SYS_MODE_BOOTING:
  2279. if(isModeChange(gun_index))
  2280. {
  2281. setLedMotion(gun_index,LED_ACTION_INIT);
  2282. //CSU Initialization & task spawn
  2283. if((Initialization() != PASS) ||
  2284. (SpawnTask() != PASS))
  2285. {
  2286. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
  2287. }
  2288. }
  2289. if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
  2290. ShmCharger->gun_info[gun_index].mcuFlag.isSetModelNamePass &&
  2291. ShmCharger->gun_info[gun_index].mcuFlag.isSetSerialNumberPass)
  2292. {
  2293. ShmCharger->gun_selectd = 0;
  2294. // Firmware version
  2295. get_firmware_version(gun_index);
  2296. // OCPP BootNotification info set
  2297. memcpy((char*)ShmOCPP16Data->OcppServerURL, (char*)ShmSysConfigAndInfo->SysConfig.OcppServerURL, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.OcppServerURL));
  2298. memcpy((char*)ShmOCPP16Data->ChargeBoxId, (char*)ShmSysConfigAndInfo->SysConfig.ChargeBoxId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ChargeBoxId));
  2299. sprintf((char*)ShmOCPP16Data->BootNotification.CpFwVersion, (char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev);
  2300. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterSerialNumber, "N/A");
  2301. sprintf((char*)ShmOCPP16Data->BootNotification.CpMeterType, "AC");
  2302. DEBUG_INFO("==========================================\n");
  2303. DEBUG_INFO("System ID: %s\n",ShmSysConfigAndInfo->SysConfig.SystemId);
  2304. DEBUG_INFO("==========================================\n");
  2305. DEBUG_INFO("=== OCPP BootNotification information ====\n");
  2306. DEBUG_INFO("OcppServerURL: %s\n",ShmSysConfigAndInfo->SysConfig.OcppServerURL);
  2307. DEBUG_INFO("ChargeBoxId: %s\n",ShmSysConfigAndInfo->SysConfig.ChargeBoxId);
  2308. DEBUG_INFO("ChargePointVendor: %s\n",ShmSysConfigAndInfo->SysConfig.chargePointVendor);
  2309. DEBUG_INFO("CpFwVersion: %s\n",ShmOCPP16Data->BootNotification.CpFwVersion);
  2310. DEBUG_INFO("CpMeterSerialNumber: %s\n",ShmOCPP16Data->BootNotification.CpMeterSerialNumber);
  2311. DEBUG_INFO("CpMeterType: %s\n",ShmOCPP16Data->BootNotification.CpMeterType);
  2312. DEBUG_INFO("==========================================\n");
  2313. DEBUG_INFO("========== Set Wifi information ==========\n");
  2314. DEBUG_INFO("Wifi mode: %d\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode);
  2315. DEBUG_INFO("Wifi SSID: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiSsid);
  2316. DEBUG_INFO("Wifi password: %s\n", ShmSysConfigAndInfo->SysConfig.AthInterface.WifiPassword);
  2317. DEBUG_INFO("==========================================\n");
  2318. // Flag for initialization system success
  2319. ShmCharger->gun_info[gun_index].isInitialPass = YES;
  2320. // Set max current to rating current
  2321. ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  2322. // Default Ethernet / Wifi / 4G to 1:disconnected
  2323. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet = ON;
  2324. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi = ON;
  2325. ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectVia4Gi = ON;
  2326. // If Web Server OPCC URL is empty kill Module_OcppBackend
  2327. if(strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)
  2328. {
  2329. DEBUG_INFO("URL is empty kill Module_OcppBackend...\n");
  2330. system ("pkill OcppBackend");
  2331. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  2332. }
  2333. // If rotate switch equal zero, the system needs to change Debug mode
  2334. if(ShmCharger->gun_info[gun_index].primaryMcuState.rotatory_switch == 0)
  2335. setChargerMode(gun_index, SYS_MODE_DEBUG);
  2336. else
  2337. setChargerMode(gun_index, SYS_MODE_IDLE);
  2338. }
  2339. break;
  2340. case SYS_MODE_IDLE:
  2341. if(isModeChange(gun_index))
  2342. {
  2343. setLedMotion(gun_index,LED_ACTION_IDLE);
  2344. setRelay(gun_index,OFF);
  2345. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  2346. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2347. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  2348. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  2349. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  2350. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  2351. ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode = 0x00;
  2352. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = 0;
  2353. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2354. ShmCharger->gun_info[gun_index].targetCurrent = 0xFF;
  2355. 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;
  2356. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  2357. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].ReservationId = -1;
  2358. // Response StopTransactionConf
  2359. if(ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf == ON)
  2360. {
  2361. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionConf = OFF;
  2362. }
  2363. }
  2364. if(ShmOCPP16Data->OcppConnStatus == ON)
  2365. {
  2366. setLedMotion(gun_index,LED_ACTION_IDLE);
  2367. }
  2368. else
  2369. {
  2370. setLedMotion(gun_index,LED_ACTION_INTERNET_DISCONNECT);
  2371. }
  2372. if(((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)) ||
  2373. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C)) ||
  2374. (ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  2375. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON) ||
  2376. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == ON) ||
  2377. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart == ON))
  2378. {
  2379. // Clean User id & Card Number
  2380. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2381. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  2382. {
  2383. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  2384. DEBUG_INFO("Start Method : RFID...\n");
  2385. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2386. {
  2387. // Big endian
  2388. switch(rfid.snType)
  2389. {
  2390. case RFID_SN_TYPE_6BYTE:
  2391. 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]);
  2392. break;
  2393. case RFID_SN_TYPE_7BYTE:
  2394. 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]);
  2395. break;
  2396. case RFID_SN_TYPE_10BYTE:
  2397. 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]);
  2398. break;
  2399. case RFID_SN_TYPE_4BYTE:
  2400. default:
  2401. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2402. break;
  2403. }
  2404. }
  2405. else
  2406. {
  2407. // Little endian
  2408. switch(rfid.snType)
  2409. {
  2410. case RFID_SN_TYPE_6BYTE:
  2411. 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]);
  2412. break;
  2413. case RFID_SN_TYPE_7BYTE:
  2414. 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]);
  2415. break;
  2416. case RFID_SN_TYPE_10BYTE:
  2417. 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]);
  2418. break;
  2419. case RFID_SN_TYPE_4BYTE:
  2420. default:
  2421. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2422. break;
  2423. }
  2424. }
  2425. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2426. }
  2427. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq)
  2428. {
  2429. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  2430. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  2431. DEBUG_INFO("Start Method : BACKEND...\n");
  2432. }
  2433. else if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart == ON)
  2434. {
  2435. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BLE;
  2436. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmCharger->gun_info[gun_index].bleLoginCentralId.id, ARRAY_SIZE(ShmCharger->gun_info[gun_index].bleLoginCentralId.id));
  2437. DEBUG_INFO("Start Method : BLE...\n");
  2438. }
  2439. else
  2440. {
  2441. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_FREE;
  2442. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  2443. DEBUG_INFO("Start Method : FREE...\n");
  2444. }
  2445. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2446. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  2447. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStart = OFF;
  2448. ShmCharger->gun_info[gun_index].isGunPlugged = NO;
  2449. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStart = OFF;
  2450. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  2451. }
  2452. else
  2453. {}
  2454. break;
  2455. case SYS_MODE_AUTHORIZING:
  2456. if(isModeChange(gun_index))
  2457. {
  2458. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  2459. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID)
  2460. {
  2461. memset(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, 0x00, ARRAY_SIZE(ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status));
  2462. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  2463. {
  2464. case AUTH_MODE_ENABLE:
  2465. if(ShmOCPP16Data->OcppConnStatus)
  2466. {
  2467. // On line
  2468. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2469. }
  2470. else
  2471. {
  2472. // Off line
  2473. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  2474. {
  2475. case OFF_POLICY_LOCALLIST:
  2476. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2477. break;
  2478. case OFF_POLICY_PH_RFID:
  2479. break;
  2480. case OFF_POLICY_FREE:
  2481. break;
  2482. case OFF_POLICY_NOCHARGE:
  2483. default:
  2484. break;
  2485. }
  2486. }
  2487. break;
  2488. case AUTH_MODE_DISABLE:
  2489. default:
  2490. break;
  2491. }
  2492. }
  2493. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2494. }
  2495. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
  2496. {
  2497. // Authorization timeout process.
  2498. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2499. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  2500. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  2501. sleep(3);
  2502. setChargerMode(gun_index, SYS_MODE_IDLE);
  2503. DEBUG_WARN("Authorize timeout !!!\n");
  2504. }
  2505. else
  2506. {
  2507. switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod)
  2508. {
  2509. case START_METHOD_RFID:
  2510. if((ShmOCPP16Data->SpMsg.bits.AuthorizeConf) ||
  2511. (!ShmOCPP16Data->OcppConnStatus && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
  2512. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)))
  2513. {
  2514. DEBUG_INFO("ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status: %s \n", ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status);
  2515. if((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) ||
  2516. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)) ||
  2517. (!ShmOCPP16Data->OcppConnStatus && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)))
  2518. {
  2519. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2520. DEBUG_INFO("Authorize pass.\n");
  2521. setSpeaker(ON,SPEAKER_SHORT);
  2522. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  2523. sleep(3);
  2524. setChargerMode(gun_index, SYS_MODE_PREPARING);
  2525. }
  2526. else
  2527. {
  2528. DEBUG_INFO("Authorize fail.\n");
  2529. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  2530. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  2531. sleep(3);
  2532. setChargerMode(gun_index, SYS_MODE_IDLE);
  2533. }
  2534. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2535. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  2536. }
  2537. break;
  2538. case START_METHOD_BACKEND:
  2539. case START_METHOD_BLE:
  2540. case START_METHOD_FREE:
  2541. default:
  2542. setSpeaker(ON,SPEAKER_SHORT);
  2543. setChargerMode(gun_index, SYS_MODE_PREPARING);
  2544. break;
  2545. }
  2546. }
  2547. break;
  2548. case SYS_MODE_PREPARING:
  2549. if(isModeChange(gun_index))
  2550. {
  2551. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2552. setRelay(gun_index, ON);
  2553. setLedMotion(gun_index,LED_ACTION_CONNECTED);
  2554. }
  2555. // If control pilot detect Bx, skip watch dog time out.
  2556. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B)
  2557. {
  2558. ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
  2559. ShmCharger->gun_info[gun_index].isGunPlugged = YES;
  2560. }
  2561. // Unplug charging gun to Idle mode
  2562. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].isGunPlugged == YES))
  2563. {
  2564. DEBUG_INFO("Charging gun is plugged before.\n");
  2565. setChargerMode(gun_index, SYS_MODE_IDLE);
  2566. }
  2567. // Use RFID card to stop handshaking
  2568. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index))
  2569. {
  2570. DEBUG_INFO("Use RFID card to stop handshaking.\n");
  2571. setSpeaker(ON,SPEAKER_SHORT);
  2572. setChargerMode(gun_index, SYS_MODE_IDLE);
  2573. }
  2574. else
  2575. {
  2576. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2577. }
  2578. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
  2579. {
  2580. setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
  2581. setSpeaker(ON,SPEAKER_INTERVAL_LONG);
  2582. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+3000))
  2583. {
  2584. DEBUG_INFO("Handshaking timeout...\n");
  2585. setChargerMode(gun_index, SYS_MODE_IDLE);
  2586. }
  2587. }
  2588. else if((ShmCharger->gun_info[gun_index].primaryMcuState.relay_state == ON))
  2589. {
  2590. setSpeaker(ON,SPEAKER_SHORT);
  2591. ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor = OFF;
  2592. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy = 0;
  2593. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartDateTime);
  2594. ShmCharger->gun_info[gun_index].powerConsumption.power_consumption_at_start = ShmCharger->gun_info[gun_index].powerConsumption.power_consumption;
  2595. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  2596. memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2597. memcpy((char*)ShmOCPP16Data->StartTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StartTransaction[gun_index].IdTag));
  2598. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionReq = ON;
  2599. setChargerMode(gun_index, SYS_MODE_CHARGING);
  2600. }
  2601. break;
  2602. case SYS_MODE_CHARGING:
  2603. if(isModeChange(gun_index))
  2604. {
  2605. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2606. ftime(&startChargingTime[gun_index]);
  2607. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2608. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  2609. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  2610. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  2611. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = OFF;
  2612. ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId = 0;
  2613. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  2614. }
  2615. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  2616. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  2617. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON)||
  2618. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState != CP_STATE_C) ||
  2619. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  2620. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON) ||
  2621. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  2622. {
  2623. if(ShmCharger->gun_info[gun_index].rfidReq)
  2624. {
  2625. DEBUG_INFO("Certified in charging mode... \n");
  2626. // If RFID SN different with start user, it need to authorize ID
  2627. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  2628. {
  2629. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ShmOCPP16Data->SpMsg.bits.AuthorizeReq)
  2630. {
  2631. // Request authorization
  2632. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  2633. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  2634. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  2635. {
  2636. // Big endian
  2637. switch(rfid.snType)
  2638. {
  2639. case RFID_SN_TYPE_6BYTE:
  2640. 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]);
  2641. break;
  2642. case RFID_SN_TYPE_7BYTE:
  2643. 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]);
  2644. break;
  2645. case RFID_SN_TYPE_10BYTE:
  2646. 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]);
  2647. break;
  2648. case RFID_SN_TYPE_4BYTE:
  2649. default:
  2650. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  2651. break;
  2652. }
  2653. }
  2654. else
  2655. {
  2656. // Little endian
  2657. switch(rfid.snType)
  2658. {
  2659. case RFID_SN_TYPE_6BYTE:
  2660. 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]);
  2661. break;
  2662. case RFID_SN_TYPE_7BYTE:
  2663. 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]);
  2664. break;
  2665. case RFID_SN_TYPE_10BYTE:
  2666. 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]);
  2667. break;
  2668. case RFID_SN_TYPE_4BYTE:
  2669. default:
  2670. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  2671. break;
  2672. }
  2673. }
  2674. DEBUG_INFO("End request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  2675. DEBUG_INFO("Start method : %d\n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  2676. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  2677. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  2678. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  2679. {
  2680. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  2681. {
  2682. case AUTH_MODE_ENABLE:
  2683. if(ShmOCPP16Data->OcppConnStatus)
  2684. {
  2685. // On line
  2686. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2687. }
  2688. else
  2689. {
  2690. // Off line
  2691. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  2692. {
  2693. case OFF_POLICY_LOCALLIST:
  2694. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  2695. break;
  2696. case OFF_POLICY_PH_RFID:
  2697. break;
  2698. case OFF_POLICY_FREE:
  2699. break;
  2700. case OFF_POLICY_NOCHARGE:
  2701. default:
  2702. break;
  2703. }
  2704. }
  2705. break;
  2706. case AUTH_MODE_DISABLE:
  2707. default:
  2708. break;
  2709. }
  2710. }
  2711. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  2712. {
  2713. DEBUG_INFO("Should be remote stop charger... \n");
  2714. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2715. }
  2716. }
  2717. else
  2718. {
  2719. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf ||
  2720. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2721. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)) ||
  2722. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  2723. {
  2724. if(((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) &&
  2725. (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag)==0))||
  2726. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2727. (!ShmOCPP16Data->OcppConnStatus && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  2728. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  2729. {
  2730. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  2731. }
  2732. else
  2733. {
  2734. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  2735. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = OFF;
  2736. DEBUG_INFO("Authorize fail.\n");
  2737. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  2738. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  2739. sleep(3);
  2740. }
  2741. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  2742. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  2743. }
  2744. }
  2745. }
  2746. // If Authorize is accpted or Rfid card match with start User, The system should terminating to the end
  2747. if(ShmCharger->gun_info[gun_index].isAuthPassEnd || isMatchStartUser(gun_index))
  2748. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2749. }
  2750. else
  2751. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2752. }
  2753. else
  2754. {
  2755. setLedMotion(gun_index,LED_ACTION_CHARGING);
  2756. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  2757. ftime(&endChargingTime[gun_index]);
  2758. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2759. 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;
  2760. // Response StartTransactionConf
  2761. if(ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf == ON)
  2762. {
  2763. ShmOCPP16Data->CpMsg.bits[gun_index].StartTransactionConf = OFF;
  2764. }
  2765. // Charging profile preparation
  2766. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE)
  2767. {
  2768. if(!ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf)
  2769. {
  2770. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq = ON;
  2771. ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
  2772. }
  2773. else
  2774. {
  2775. ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileConf = OFF;
  2776. }
  2777. }
  2778. // Checking profile id > 0 and current time is between charging profile validFrom & validTo
  2779. if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId > 0) &&
  2780. (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON) &&
  2781. (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
  2782. {
  2783. // Debug information
  2784. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2785. {
  2786. DEBUG_INFO("Profile ID found: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
  2787. DEBUG_INFO("Valid from: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom);
  2788. DEBUG_INFO("Valid to: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo);
  2789. DEBUG_INFO("Start schedule: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.StartSchedule);
  2790. DEBUG_INFO("Profile kind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind);
  2791. DEBUG_INFO("RecurrencyKind: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind);
  2792. DEBUG_INFO("Profile purpose: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose);
  2793. DEBUG_INFO("Transaction ID: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId);
  2794. DEBUG_INFO("ChargingRateUnit: %s\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingRateUnit);
  2795. }
  2796. // Checking profile kind
  2797. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Absolute") == PASS))
  2798. {
  2799. // Absolute profile
  2800. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2801. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2802. {
  2803. // Checking limitation
  2804. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2805. {
  2806. if((getScheduleStart(gun_index) > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2807. (((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)))
  2808. {
  2809. 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);
  2810. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2811. }
  2812. else
  2813. break;
  2814. }
  2815. }
  2816. }
  2817. else if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileKind, "Relative") == PASS))
  2818. {
  2819. // Relative profile
  2820. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2821. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2822. {
  2823. // Checking limitation
  2824. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2825. {
  2826. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2827. (((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)))
  2828. {
  2829. 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);
  2830. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2831. }
  2832. else
  2833. break;
  2834. }
  2835. }
  2836. }
  2837. else
  2838. {
  2839. // Recurring profile
  2840. if(((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == PASS) && (ShmOCPP16Data->SmartChargingProfile[gun_index].TransactionId == ShmOCPP16Data->StartTransaction[gun_index].ResponseTransactionId)) ||
  2841. (mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfilePurpose, "TxProfile") == FAIL))
  2842. {
  2843. if((mystrcmp((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].RecurrencyKind, "Daily") == PASS))
  2844. {
  2845. // Checking limitation
  2846. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2847. {
  2848. if((getStartSinceToday() > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2849. (((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)))
  2850. {
  2851. 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);
  2852. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2853. }
  2854. else
  2855. break;
  2856. }
  2857. }
  2858. else
  2859. {
  2860. // Checking limitation
  2861. for(uint8_t idx_period=0;idx_period<ARRAY_SIZE(ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod);idx_period++)
  2862. {
  2863. if((getStartSinceWeek() > ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingSchedule.ChargingSchedulePeriod[idx_period].StartPeriod) &&
  2864. (((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)))
  2865. {
  2866. 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);
  2867. //DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent on period[%d]: %d\n", gun_index, idx_period, ShmCharger->gun_info[gun_index].targetCurrent);
  2868. }
  2869. else
  2870. break;
  2871. }
  2872. }
  2873. }
  2874. }
  2875. }
  2876. else if((ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId == 0) && (ShmOCPP16Data->CSUMsg.bits[gun_index].ChargingProfileReq != ON))
  2877. {
  2878. ShmCharger->gun_info[gun_index].targetCurrent = ShmCharger->gun_info[gun_index].primaryMcuState.rating_current;
  2879. }
  2880. // Determine max charging current to MCU
  2881. if(ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent == 0)
  2882. {
  2883. 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);
  2884. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2885. {
  2886. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  2887. {
  2888. 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);
  2889. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2890. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  2891. }
  2892. else
  2893. {}
  2894. }
  2895. }
  2896. else
  2897. {
  2898. ShmCharger->gun_info[gun_index].targetCurrent = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
  2899. if(ShmCharger->gun_info[gun_index].targetCurrent != ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current)
  2900. {
  2901. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
  2902. {
  2903. 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);
  2904. ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
  2905. ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
  2906. }
  2907. else
  2908. {}
  2909. }
  2910. }
  2911. // Debug information
  2912. if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_LOGPPRINTOUT]) > TIMEOUT_SPEC_LOGPPRINTOUT)
  2913. {
  2914. DEBUG_INFO("=======================================================================\n");
  2915. DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
  2916. 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);
  2917. DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
  2918. DEBUG_INFO("=======================================================================\n");
  2919. ftime(&startTime[gun_index][TMR_IDX_LOGPPRINTOUT]);
  2920. }
  2921. if(ShmOCPP16Data->OcppConnStatus)
  2922. {
  2923. // On-line max condition check
  2924. if((ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60)))
  2925. {
  2926. setRelay(gun_index, OFF);
  2927. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  2928. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2929. 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));
  2930. }
  2931. else if((ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy)))
  2932. {
  2933. setRelay(gun_index, OFF);
  2934. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  2935. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2936. 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));
  2937. }
  2938. else
  2939. {
  2940. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  2941. setRelay(gun_index, ON);
  2942. }
  2943. }
  2944. else
  2945. {
  2946. // Off-line max condition check
  2947. if((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  2948. ((ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_ENABLE) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy != OFF_POLICY_NOCHARGE)))
  2949. {
  2950. 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))))
  2951. {
  2952. setRelay(gun_index, OFF);
  2953. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  2954. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2955. 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));
  2956. }
  2957. 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))))
  2958. {
  2959. setRelay(gun_index, OFF);
  2960. ShmCharger->gun_info[gun_index].isChargerStopByCondition = YES;
  2961. setChargerMode(gun_index, SYS_MODE_TERMINATING);
  2962. 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));
  2963. }
  2964. else
  2965. {
  2966. ShmCharger->gun_info[gun_index].isChargerStopByCondition = NO;
  2967. setRelay(gun_index, ON);
  2968. }
  2969. }
  2970. else
  2971. {
  2972. setRelay(gun_index, OFF);
  2973. DEBUG_INFO("Connector-%d can not charging in off line\n", gun_index);
  2974. }
  2975. }
  2976. }
  2977. break;
  2978. case SYS_MODE_TERMINATING:
  2979. if(isModeChange(gun_index))
  2980. {
  2981. if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
  2982. {
  2983. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
  2984. }
  2985. getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
  2986. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  2987. sleep(3);
  2988. }
  2989. // End authorize pass
  2990. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  2991. (ShmCharger->gun_info[gun_index].isAuthPassEnd) ||
  2992. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON) ||
  2993. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON) ||
  2994. ((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0)) ||
  2995. (ShmOCPP16Data->MsMsg.bits.ResetReq) ||
  2996. (ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON) ||
  2997. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop == ON))
  2998. {
  2999. if((ShmCharger->gun_info[gun_index].legacyRequest.isLegacyRequest == ON) || (ShmCharger->gun_info[gun_index].isChargerStopByCondition == YES))
  3000. {
  3001. if(((ShmCharger->gun_info[gun_index].rfidReq == ON) && isMatchStartUser(gun_index)) ||
  3002. (ShmCharger->gun_info[gun_index].isAuthPassEnd))
  3003. {
  3004. DEBUG_INFO("Authorize pass.\n");
  3005. setSpeaker(ON,SPEAKER_SHORT);
  3006. setLedMotion(gun_index,LED_ACTION_RFID_PASS);
  3007. sleep(3);
  3008. }
  3009. else
  3010. {
  3011. /*
  3012. // If stop reason is "Bluetooth" / "RemoteStop" / "Reset" / "Unplug" should be set speaker on
  3013. if(ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop == ON)
  3014. DEBUG_INFO("Bluetooth to stop charging... \n");
  3015. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq == ON)
  3016. DEBUG_INFO("Remote to stop charging... \n");
  3017. else if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (strcmp((char *)ShmOCPP16Data->ConfigurationTable.CoreProfile[StopTransactionOnEVSideDisconnect].ItemData, "TRUE") == 0))
  3018. DEBUG_INFO("Unplug to stop charging... \n");
  3019. else if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3020. DEBUG_INFO("Reset request to stop charging... \n");
  3021. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3022. DEBUG_INFO("Unlock connector request to stop charging... \n");
  3023. setSpeaker(ON,SPEAKER_SHORT);
  3024. */
  3025. }
  3026. }
  3027. else
  3028. {}
  3029. setRelay(gun_index, OFF);
  3030. setLedMotion(gun_index, LED_ACTION_STOP);
  3031. // If relay is off, the system should change to complete mode
  3032. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  3033. {
  3034. setChargerMode(gun_index, SYS_MODE_COMPLETE);
  3035. }
  3036. }
  3037. else
  3038. {
  3039. // If the charger stops charging, the led should be stoped blink
  3040. if(!ShmCharger->gun_info[gun_index].primaryMcuState.relay_state)
  3041. {
  3042. setLedMotion(gun_index, LED_ACTION_STOP);
  3043. }
  3044. if(ShmCharger->gun_info[gun_index].rfidReq)
  3045. {
  3046. DEBUG_INFO("Certified in terminating mode... \n");
  3047. // If RFID SN different with start user, it need to authorize ID
  3048. if((ShmCharger->gun_info[gun_index].rfidReq == ON) && !isMatchStartUser(gun_index))
  3049. {
  3050. if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH) && !ShmOCPP16Data->SpMsg.bits.AuthorizeReq)
  3051. {
  3052. // Request authorization
  3053. ftime(&startTime[gun_index][TMR_IDX_AUTH]);
  3054. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3055. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3056. {
  3057. // Big endian
  3058. switch(rfid.snType)
  3059. {
  3060. case RFID_SN_TYPE_6BYTE:
  3061. 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]);
  3062. break;
  3063. case RFID_SN_TYPE_7BYTE:
  3064. 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]);
  3065. break;
  3066. case RFID_SN_TYPE_10BYTE:
  3067. 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]);
  3068. break;
  3069. case RFID_SN_TYPE_4BYTE:
  3070. default:
  3071. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3072. break;
  3073. }
  3074. }
  3075. else
  3076. {
  3077. // Little endian
  3078. switch(rfid.snType)
  3079. {
  3080. case RFID_SN_TYPE_6BYTE:
  3081. 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]);
  3082. break;
  3083. case RFID_SN_TYPE_7BYTE:
  3084. 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]);
  3085. break;
  3086. case RFID_SN_TYPE_10BYTE:
  3087. 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]);
  3088. break;
  3089. case RFID_SN_TYPE_4BYTE:
  3090. default:
  3091. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3092. break;
  3093. }
  3094. }
  3095. DEBUG_INFO("End request User Id : %s... \n", ShmSysConfigAndInfo->SysConfig.UserId);
  3096. DEBUG_INFO("Start method : %d... \n ", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod);
  3097. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_RFID) ||
  3098. //(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND) ||
  3099. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BLE))
  3100. {
  3101. switch(ShmSysConfigAndInfo->SysConfig.AuthorisationMode)
  3102. {
  3103. case AUTH_MODE_ENABLE:
  3104. if(ShmOCPP16Data->OcppConnStatus)
  3105. {
  3106. // On line
  3107. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  3108. }
  3109. else
  3110. {
  3111. // Off line
  3112. switch(ShmSysConfigAndInfo->SysConfig.OfflinePolicy)
  3113. {
  3114. case OFF_POLICY_LOCALLIST:
  3115. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = ON;
  3116. break;
  3117. case OFF_POLICY_PH_RFID:
  3118. break;
  3119. case OFF_POLICY_FREE:
  3120. break;
  3121. case OFF_POLICY_NOCHARGE:
  3122. default:
  3123. break;
  3124. }
  3125. }
  3126. break;
  3127. case AUTH_MODE_DISABLE:
  3128. default:
  3129. break;
  3130. }
  3131. }
  3132. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND)
  3133. {
  3134. DEBUG_INFO("Should be remote stop charger... \n");
  3135. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3136. }
  3137. }
  3138. else
  3139. {
  3140. if(ShmOCPP16Data->SpMsg.bits.AuthorizeConf ||
  3141. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3142. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (strcmp((char *)&ShmSysConfigAndInfo->SysConfig.OcppServerURL,"") == 0)) ||
  3143. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3144. {
  3145. if(((strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.Status, "Accepted")==0) &&
  3146. (strcmp((char*)ShmOCPP16Data->Authorize.ResponseIdTagInfo.ParentIdTag, (char*)ShmOCPP16Data->StartTransaction[gun_index].ResponseIdTagInfo.ParentIdTag)==0)) ||
  3147. (ShmSysConfigAndInfo->SysConfig.AuthorisationMode == AUTH_MODE_DISABLE) ||
  3148. (!ShmOCPP16Data->OcppConnStatus && (isValidLocalWhiteCard() == PASS) && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST)) ||
  3149. (!ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE)))
  3150. {
  3151. ShmCharger->gun_info[gun_index].isAuthPassEnd = ON;
  3152. }
  3153. else
  3154. {
  3155. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3156. ShmOCPP16Data->SpMsg.bits.AuthorizeReq = OFF;
  3157. DEBUG_INFO("Authorize fail... \n");
  3158. setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
  3159. setLedMotion(gun_index,LED_ACTION_RFID_FAIL);
  3160. sleep(3);
  3161. }
  3162. startTime[gun_index][TMR_IDX_AUTH].time -= TIMEOUT_SPEC_AUTH;
  3163. ShmOCPP16Data->SpMsg.bits.AuthorizeConf = OFF;
  3164. }
  3165. }
  3166. }
  3167. }
  3168. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) &&
  3169. (ShmCharger->gun_info[gun_index].rfidReq != ON) &&
  3170. (ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop != ON) &&
  3171. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq != ON) &&
  3172. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop != ON) &&
  3173. !ShmOCPP16Data->MsMsg.bits.ResetReq &&
  3174. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration >= (ShmSysConfigAndInfo->SysConfig.MaxChargingDuration*60))) &&
  3175. !(ShmOCPP16Data->OcppConnStatus && (ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy > 0) && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy >= ((float)ShmSysConfigAndInfo->SysConfig.MaxChargingEnergy))) &&
  3176. !(!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)))) &&
  3177. !(!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)))))
  3178. {
  3179. setChargerMode(gun_index, SYS_MODE_CHARGING);
  3180. }
  3181. }
  3182. break;
  3183. case SYS_MODE_COMPLETE:
  3184. if(isModeChange(gun_index))
  3185. {
  3186. setLedMotion(gun_index, LED_ACTION_STOP);
  3187. sleep(3);
  3188. }
  3189. memset(ShmOCPP16Data->StopTransaction[gun_index].IdTag, 0x00, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3190. if(ShmOCPP16Data->MsMsg.bits.ResetReq)
  3191. {
  3192. if(strcmp((char*)ShmOCPP16Data->Reset.Type, "Hard")==0)
  3193. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "HardReset");
  3194. else
  3195. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "SoftReset");
  3196. }
  3197. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq)
  3198. {
  3199. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Remote");
  3200. }
  3201. else if(ShmCharger->gun_info[gun_index].rfidReq || ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop)
  3202. {
  3203. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Local");
  3204. if(!isMatchStartUser(gun_index))
  3205. {
  3206. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3207. }
  3208. else
  3209. {
  3210. memcpy((char*)ShmOCPP16Data->StopTransaction[gun_index].IdTag, (char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ARRAY_SIZE(ShmOCPP16Data->StopTransaction[gun_index].IdTag));
  3211. }
  3212. DEBUG_INFO("Gun-%d [ IdTag ] : %s \n", gun_index, ShmOCPP16Data->StopTransaction[gun_index].IdTag);
  3213. }
  3214. else if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
  3215. {
  3216. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EVDisconnected");
  3217. }
  3218. else if(ShmCharger->gun_info[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].isUnlockerConnetor == ON)
  3219. {
  3220. sprintf((char*)ShmOCPP16Data->StopTransaction[ShmOCPP16Data->UnlockConnector[gun_index].ConnectorId-1].StopReason, "UnlockCommand");
  3221. }
  3222. else if(ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode & ALARM_EMERGENCY_STOP)
  3223. {
  3224. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "EmergencyStop");
  3225. }
  3226. else
  3227. {
  3228. sprintf((char*)ShmOCPP16Data->StopTransaction[gun_index].StopReason, "Other");
  3229. }
  3230. DEBUG_INFO("Gun-%d : StopReason [ %s ]...\n.",gun_index,ShmOCPP16Data->StopTransaction[gun_index].StopReason);
  3231. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption = (ShmCharger->gun_info[gun_index].powerConsumption.power_consumption/100.0);
  3232. ShmOCPP16Data->CpMsg.bits[gun_index].StopTransactionReq = ON;
  3233. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3234. ShmCharger->gun_info[gun_index].isAuthPassEnd = OFF;
  3235. ShmCharger->gun_info[gun_index].bleConfigData.isRequestStop = OFF;
  3236. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStopTransactionReq = OFF;
  3237. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].schedule.isTriggerStop = OFF;
  3238. DB_Insert_Record(localDb, gun_index);
  3239. setChargerMode(gun_index, SYS_MODE_IDLE);
  3240. break;
  3241. case SYS_MODE_ALARM:
  3242. setLedMotion(gun_index,LED_ACTION_ALARM);
  3243. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  3244. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  3245. {
  3246. }
  3247. else
  3248. {
  3249. setRelay(gun_index, OFF);
  3250. }
  3251. if((ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode == 0))
  3252. {
  3253. if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_CHARGING) ||
  3254. (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus == SYS_MODE_TERMINATING))
  3255. {
  3256. setChargerMode(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus);
  3257. }
  3258. else
  3259. {
  3260. setChargerMode(gun_index, SYS_MODE_IDLE);
  3261. }
  3262. }
  3263. break;
  3264. case SYS_MODE_FAULT:
  3265. if(isModeChange(gun_index))
  3266. {}
  3267. break;
  3268. case SYS_MODE_MAINTAIN:
  3269. if(isModeChange(gun_index))
  3270. {
  3271. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3272. }
  3273. break;
  3274. case SYS_MODE_UPDATE:
  3275. if(isModeChange(gun_index))
  3276. {
  3277. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3278. }
  3279. //======================================
  3280. // Check local upgrade firmware request
  3281. //======================================
  3282. if(ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  3283. {
  3284. DEBUG_INFO("Firmware local upgrading...\n");
  3285. ShmSysConfigAndInfo->SysInfo.FirmwareUpdate = OFF;
  3286. int value = upgrade_check();
  3287. DEBUG_INFO("Local update Value: %s... \n",((value == PASS)?"Pass": "Fail"));
  3288. if(value == PASS)
  3289. {
  3290. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3291. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3292. ShmCharger->isUpdateSuccess = YES;
  3293. DEBUG_INFO("Local update success...\n");
  3294. }
  3295. else
  3296. {
  3297. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3298. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3299. ShmCharger->isUpdateSuccess = NO;
  3300. DEBUG_INFO("Local update unsuccess...\n");
  3301. }
  3302. }
  3303. else if(ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq)
  3304. {
  3305. //======================================
  3306. // Check remote upgrade firmware request
  3307. //======================================
  3308. if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "DownloadFailed")==0)
  3309. {
  3310. DEBUG_INFO("Firmware remote upgraded fail...\n");
  3311. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3312. ShmCharger->isUpdateSuccess = NO;
  3313. }
  3314. else if(strcmp((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Downloaded")==0)
  3315. {
  3316. DEBUG_INFO("Firmware remote upgrading...\n");
  3317. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installing");
  3318. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3319. ShmOCPP16Data->MsMsg.bits.UpdateFirmwareReq = OFF;
  3320. int result = upgrade_check();
  3321. DEBUG_INFO("Remote update Result: %s... \n",((result == PASS)?"Pass": "Fail"));
  3322. if(result == PASS)
  3323. {
  3324. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == ON)
  3325. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = OFF;
  3326. ShmCharger->isUpdateSuccess = YES;
  3327. DEBUG_INFO("Remote update success...\n");
  3328. }
  3329. else
  3330. {
  3331. if(ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail == OFF)
  3332. ShmStatusCodeData->InfoCode.InfoEvents.bits.CsuFimrwareUpdateFail = ON;
  3333. ShmCharger->isUpdateSuccess = NO;
  3334. DEBUG_INFO("Remote update unsuccess...\n");
  3335. }
  3336. }
  3337. else
  3338. {}
  3339. }
  3340. else
  3341. {
  3342. //======================================
  3343. // Upgrade complete reboot system
  3344. //======================================
  3345. if(!ShmCharger->gun_info[gun_index].mcuFlag.isMcuUpgradeReq && ((AC_QUANTITY>1)?!ShmCharger->gun_info[gun_index^1].mcuFlag.isMcuUpgradeReq:YES))
  3346. {
  3347. if(ShmCharger->isUpdateSuccess == YES)
  3348. {
  3349. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "Installed");
  3350. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3351. DEBUG_WARN("Firmware upgrade success.\n");
  3352. sleep(5);
  3353. DEBUG_INFO("Firmware upgraded, reboot...\n");
  3354. system("reboot -f");
  3355. sleep(5);
  3356. system("reboot -f");
  3357. }
  3358. else
  3359. {
  3360. sprintf((char*)ShmOCPP16Data->FirmwareStatusNotification.Status, "InstallationFailed");
  3361. ShmOCPP16Data->SpMsg.bits.FirmwareStatusNotificationReq = ON;
  3362. DEBUG_WARN("Firmware upgrade fail.\n");
  3363. sleep(5);
  3364. system("rm -rvf /mnt/* ");
  3365. close(wtdFd);
  3366. system("/usr/bin/run_evse_restart.sh");
  3367. }
  3368. }
  3369. }
  3370. break;
  3371. case SYS_MODE_RESERVATION:
  3372. if(isModeChange(gun_index))
  3373. {
  3374. setLedMotion(gun_index,LED_ACTION_MAINTAIN);
  3375. }
  3376. if(isReservationExpired(gun_index))
  3377. {
  3378. DEBUG_INFO("Reservation: Time's up...\n");
  3379. setChargerMode(gun_index, SYS_MODE_IDLE);
  3380. }
  3381. else if(ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq)
  3382. {
  3383. DEBUG_INFO("Reservation: Cancel reservation...\n");
  3384. setChargerMode(gun_index, SYS_MODE_IDLE);
  3385. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationReq = OFF;
  3386. ShmOCPP16Data->CsMsg.bits[gun_index].CancelReservationConf = ON;
  3387. }
  3388. else
  3389. {
  3390. // Check is there RFID or back end request start
  3391. if((ShmCharger->gun_info[gun_index].rfidReq == ON) ||
  3392. (ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq == ON))
  3393. {
  3394. if((ShmCharger->gun_info[gun_index].rfidReq == ON))
  3395. {
  3396. memset(ShmSysConfigAndInfo->SysConfig.UserId, 0x00, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
  3397. if(ShmSysConfigAndInfo->SysConfig.RfidCardNumEndian)
  3398. {
  3399. // Big endian
  3400. switch(rfid.snType)
  3401. {
  3402. case RFID_SN_TYPE_6BYTE:
  3403. 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]);
  3404. break;
  3405. case RFID_SN_TYPE_7BYTE:
  3406. 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]);
  3407. break;
  3408. case RFID_SN_TYPE_10BYTE:
  3409. 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]);
  3410. break;
  3411. case RFID_SN_TYPE_4BYTE:
  3412. default:
  3413. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[0], rfid.currentCard[1], rfid.currentCard[2], rfid.currentCard[3]);
  3414. break;
  3415. }
  3416. }
  3417. else
  3418. {
  3419. // Little endian
  3420. switch(rfid.snType)
  3421. {
  3422. case RFID_SN_TYPE_6BYTE:
  3423. 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]);
  3424. break;
  3425. case RFID_SN_TYPE_7BYTE:
  3426. 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]);
  3427. break;
  3428. case RFID_SN_TYPE_10BYTE:
  3429. 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]);
  3430. break;
  3431. case RFID_SN_TYPE_4BYTE:
  3432. default:
  3433. sprintf((char*)ShmSysConfigAndInfo->SysConfig.UserId, "%02X%02X%02X%02X", rfid.currentCard[3], rfid.currentCard[2], rfid.currentCard[1], rfid.currentCard[0]);
  3434. break;
  3435. }
  3436. }
  3437. if(strcmp((char*)ShmSysConfigAndInfo->SysConfig.UserId, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  3438. {
  3439. DEBUG_INFO("Start Method in reservation : RFID...\n");
  3440. DEBUG_INFO("Start request User Id : %s\n", ShmSysConfigAndInfo->SysConfig.UserId);
  3441. DEBUG_INFO("Reservation User Id : %s\n", ShmOCPP16Data->ReserveNow[gun_index].IdTag);
  3442. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_RFID;
  3443. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3444. }
  3445. ShmCharger->gun_info[gun_index].rfidReq = OFF;
  3446. }
  3447. else if(ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq)
  3448. {
  3449. if(strcmp((char*)ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, (char*)ShmOCPP16Data->ReserveNow[gun_index].IdTag) == 0)
  3450. {
  3451. DEBUG_INFO("Start Method in reservation: BACKEND...\n");
  3452. ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod = START_METHOD_BACKEND;
  3453. memcpy(ShmSysConfigAndInfo->SysConfig.UserId, ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag, ARRAY_SIZE(ShmOCPP16Data->RemoteStartTransaction[gun_index].IdTag));
  3454. setChargerMode(gun_index, SYS_MODE_AUTHORIZING);
  3455. }
  3456. ShmOCPP16Data->CsMsg.bits[gun_index].RemoteStartTransactionReq = OFF;
  3457. }
  3458. }
  3459. }
  3460. break;
  3461. case SYS_MODE_BOOKING:
  3462. if(isModeChange(gun_index))
  3463. {}
  3464. break;
  3465. case SYS_MODE_DEBUG:
  3466. if(isModeChange(gun_index))
  3467. {
  3468. setLedMotion(gun_index,LED_ACTION_DEBUG);
  3469. }
  3470. break;
  3471. }
  3472. }
  3473. // System watch dog reset
  3474. write(wtdFd, "a", 1);
  3475. usleep(50000);
  3476. }
  3477. return FAIL;
  3478. }