CsuComm.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554
  1. /*===========================================================================
  2. Combined Charging System (CCS): SECC
  3. CsuComm.c
  4. initiated by Vern, Joseph
  5. (since 2019/07/19)
  6. =============================================================================*/
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <linux/termios.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include <stdlib.h>
  15. #include <sys/ipc.h>
  16. #include <sys/shm.h>
  17. #include <sys/mman.h>
  18. #include <linux/sockios.h>
  19. #include <linux/socket.h>
  20. #include <linux/can.h>
  21. #include <linux/can/raw.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <sys/time.h>
  25. #include <sys/timeb.h>
  26. #include <math.h>//for pow
  27. #include <net/if.h>
  28. #include <unistd.h>
  29. #include "define.h"
  30. #include "CsuComm.h"
  31. #include "SeccComm.h"
  32. #include "exi_engine/api/api.h"
  33. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  34. struct StatusCodeData *ShmStatusCodeData;
  35. struct CcsData *ShmCcsData;
  36. struct InternalComm *ShmInternalComm;
  37. struct InternalCommAC *ShmInternalCommAC;
  38. pid_t PID_CAN_Rx_Task;
  39. pid_t PID_CsuComm_Error_Monitor_Task;
  40. int FD_CAN_Socket;
  41. unsigned char buf_log_csucomm[SIZE_OF_LOG_BUFFER];
  42. unsigned char buf_log_csucomm_fork1[SIZE_OF_LOG_BUFFER];
  43. unsigned char buf_log_csucomm_fork2[SIZE_OF_LOG_BUFFER];
  44. /*===========================================================================
  45. FUNCTION: GetSysTime()
  46. DESCRIPTION:
  47. PRE-CONDITION:
  48. INPUT:
  49. OUTPUT:
  50. GLOBAL VARIABLES:
  51. =============================================================================*/
  52. void PrintTimeStamp()
  53. {
  54. //static time_t CurrentTime;
  55. //static struct tm *tm;
  56. static struct timeval tv;
  57. //CurrentTime = time(NULL);
  58. //tm = localtime(&CurrentTime);
  59. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  60. //printf("[%02d:%02d:%02d.%06d]",
  61. // tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec);
  62. DEBUG_PRINTF_CSUCOMM_DETAIL("[%05d.%06d]", tv.tv_sec, tv.tv_usec);
  63. }
  64. /*===========================================================================
  65. FUNCTION: Check_V2G_Flow_Status
  66. DESCRIPTION:
  67. PRE-CONDITION:
  68. INPUT:
  69. OUTPUT:
  70. GLOBAL VARIABLES:
  71. =============================================================================*/
  72. unsigned char Check_V2G_Flow_Status()
  73. {
  74. unsigned char result = 0;
  75. switch (ShmCcsData->CommProtocol)
  76. {
  77. case V2GT_MSG_PROTOCOL_DIN70121: //0
  78. {
  79. result = ShmCcsData->V2GMessage_DIN70121.PresentMsgFlowStatus;
  80. break;
  81. }
  82. case V2GT_MSG_PROTOCOL_ISO15118_2014: //1
  83. {
  84. result = ShmCcsData->V2GMessage_ISO15118_2014.PresentMsgFlowStatus;
  85. break;
  86. }
  87. case V2GT_MSG_PROTOCOL_ISO15118_2018: //2
  88. {
  89. result = ShmCcsData->V2GMessage_ISO15118_2018.PresentMsgFlowStatus;
  90. break;
  91. }
  92. default:
  93. break;
  94. }
  95. return result;
  96. }
  97. /*===========================================================================
  98. FUNCTION: StoreLogMsg
  99. DESCRIPTION:
  100. PRE-CONDITION:
  101. INPUT:
  102. OUTPUT:
  103. GLOBAL VARIABLES:
  104. =============================================================================*/
  105. #if SAVE_SYS_LOG_MSG_CSUCOMM_SWITCH == ENABLE
  106. int StoreLogMsg(unsigned char *DataString)
  107. {
  108. static unsigned char Buf[256*2];
  109. static time_t CurrentTime;
  110. static struct tm *tm;
  111. static struct timeval tv;
  112. memset(Buf, 0, sizeof(Buf));
  113. CurrentTime = time(NULL);
  114. tm = localtime(&CurrentTime);
  115. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  116. sprintf(Buf, "echo \"[%04d%02d%02d: %02d:%02d:%02d.%06d][CsuComm][%d][%02d]%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  117. tm->tm_year + 1900,
  118. tm->tm_mon + 1,
  119. tm->tm_mday,
  120. tm->tm_hour,
  121. tm->tm_min,
  122. tm->tm_sec,
  123. tv.tv_usec,
  124. EVCOMM_SYS_INFO.CpState,
  125. Check_V2G_Flow_Status(),
  126. DataString,
  127. tm->tm_year + 1900,
  128. tm->tm_mon + 1);
  129. system(Buf);
  130. DEBUG_PRINTF_CSUCOMM_SYSTEM_LOG("[%02d:%02d:%02d.%06d][CsuComm][%d][%02d]%s \n",
  131. tm->tm_hour,
  132. tm->tm_min,
  133. tm->tm_sec,
  134. tv.tv_usec,
  135. EVCOMM_SYS_INFO.CpState,
  136. Check_V2G_Flow_Status(),
  137. DataString);
  138. //Reset the buf_log_csucomm Buffer, i.e. DataString
  139. memset(buf_log_csucomm, 0, SIZE_OF_LOG_BUFFER);
  140. }
  141. #endif
  142. /*===========================================================================
  143. FUNCTION: Array_Check_All_Zero
  144. DESCRIPTION:
  145. PRE-CONDITION:
  146. INPUT:
  147. OUTPUT:
  148. result:
  149. (1) TRUE: all zero
  150. (2) FALSE: not all zero
  151. GLOBAL VARIABLES:
  152. =============================================================================*/
  153. int Array_Check_All_Zero(unsigned char *ptr, int size)
  154. {
  155. int result = TRUE;
  156. int i = 0;
  157. for (i = 0; i < size; i++)
  158. {
  159. if (ptr[i] != 0)
  160. {
  161. result = FALSE;
  162. break;
  163. }
  164. }
  165. return result;
  166. }
  167. /*===========================================================================
  168. FUNCTION: Array_Compare_Identity
  169. DESCRIPTION:
  170. PRE-CONDITION:
  171. INPUT:
  172. OUTPUT:
  173. result = FALSE (not identical)
  174. result = TRUE (identical)
  175. GLOBAL VARIABLES:
  176. =============================================================================*/
  177. int Array_Compare_Identity(unsigned char *ptrA, unsigned char *ptrB, int size)
  178. {
  179. int result = TRUE;
  180. int i = 0;
  181. for (i = 0; i < size; i++)
  182. {
  183. if (ptrA[i] != ptrB[i])
  184. {
  185. result = FALSE;
  186. #if 0
  187. sprintf(buf_log_evcomm,
  188. "[Array_Compare_Identity]%02X%02X%02X%02X%02X%02X,%02X%02X%02X%02X%02X%02X(%d)\n",
  189. ptrA[0], ptrA[1], ptrA[2], ptrA[3], ptrA[4], ptrA[5],
  190. ptrB[0], ptrB[1], ptrB[2], ptrB[3], ptrB[4], ptrB[5],
  191. result);
  192. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_evcomm);
  193. #endif
  194. break;
  195. }
  196. }
  197. return result;
  198. }
  199. /*===========================================================================
  200. FUNCTION: CAN_Tx_MSG
  201. DESCRIPTION:
  202. PRE-CONDITION:
  203. INPUT:
  204. OUTPUT:
  205. GLOBAL VARIABLES:
  206. =============================================================================*/
  207. int CAN_Tx_MSG(int Fd, unsigned int MsgId, unsigned char SlaveAddress, unsigned char DataLength, unsigned char *SendData)
  208. {
  209. struct can_frame frame;
  210. unsigned int tmp = 0;
  211. int nbytes = 0;
  212. int i = 0;
  213. //Protection: To avoid unexpected length for CAN bus payload.
  214. if (DataLength > 8)
  215. {
  216. DataLength = 8;
  217. }
  218. memset(&frame, 0, sizeof(struct can_frame));
  219. frame.can_id = 0x80000000 | CAN_SEND_DIRECTION | MsgId | SlaveAddress; //0x80000000: extension ID format
  220. frame.can_dlc = DataLength;
  221. memcpy(frame.data, SendData, DataLength);
  222. nbytes = write(Fd, &frame, sizeof(struct can_frame));
  223. #if 0
  224. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][CAN_Tx_MSG] <%X> ", frame.can_id);
  225. for (i = 0; i < frame.can_dlc; i++)
  226. {
  227. DEBUG_PRINTF_CSUCOMM_DETAIL("%02X ", frame.data[i]);
  228. }
  229. DEBUG_PRINTF_CSUCOMM_DETAIL("(%d Bytes)\n", frame.can_dlc);
  230. #endif
  231. return nbytes;
  232. }
  233. /*===========================================================================
  234. FUNCTION: Sniffer_Candump
  235. DESCRIPTION:
  236. PRE-CONDITION:
  237. INPUT:
  238. OUTPUT:
  239. 0: accept
  240. -1: invalid
  241. GLOBAL VARIABLES:
  242. =============================================================================*/
  243. int Sniffer_Candump(char cmd)
  244. {
  245. #if (CANDUMP_PACKETS_SNIFFER_SWITCH == ENABLE)
  246. if (cmd == ENABLE)
  247. {
  248. SAVE_SYS_LOG_MSG_CSUCOMM("[candump]init");
  249. system("cd /mnt/;rm -rf candump/");
  250. system("cd /mnt/;mkdir candump");
  251. SAVE_SYS_LOG_MSG_CSUCOMM("[candump]on");
  252. system("cd /mnt/candump;candump -l can0 &");
  253. return 0;
  254. }
  255. else if (cmd == DISABLE)
  256. {
  257. SAVE_SYS_LOG_MSG_CSUCOMM("[candump]off");
  258. system("killall candump");
  259. SAVE_SYS_LOG_MSG_CSUCOMM("[candump]save");
  260. system("cd /;cp -rfv /mnt/candump /Storage/SystemLog/");
  261. return 0;
  262. }
  263. else
  264. {
  265. sprintf(buf_log_csucomm, "[candump]unexpected cmd(%d)", cmd);
  266. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  267. return -1;
  268. }
  269. #endif
  270. }
  271. /*===========================================================================
  272. FUNCTION: Checksum_Generator
  273. DESCRIPTION:
  274. PRE-CONDITION:
  275. INPUT:
  276. OUTPUT:
  277. GLOBAL VARIABLES:
  278. =============================================================================*/
  279. unsigned char Checksum_Generator(unsigned int StartAdress, unsigned int length, unsigned char Data[])
  280. {
  281. unsigned char checksum = 0x00;
  282. for(unsigned int i = 0; i < length; i++)
  283. {
  284. //DEBUG_INFO("value = %x \n", Data[StartAdress + i]);
  285. checksum ^= Data[StartAdress + i];
  286. //DEBUG_INFO("checksum = %x \n", checksum);
  287. }
  288. return checksum;
  289. }
  290. /*===========================================================================
  291. FUNCTION: CRC32_Generator
  292. DESCRIPTION:
  293. PRE-CONDITION:
  294. INPUT:
  295. OUTPUT:
  296. GLOBAL VARIABLES:
  297. =============================================================================*/
  298. unsigned int CRC32_Generator(unsigned char *data, unsigned int length)
  299. {
  300. unsigned char i;
  301. unsigned int cnt = 0;
  302. unsigned int crc = 0xffffffff; // Initial value
  303. while(length--)
  304. {
  305. if(cnt > 33 && cnt < 48)
  306. {
  307. data++;
  308. }
  309. else
  310. {
  311. crc ^= *data++; // crc ^= *data; data++;
  312. for (i = 0; i < 8; ++i)
  313. {
  314. if (crc & 1)
  315. {
  316. crc = (crc >> 1) ^ 0xEDB88320;// 0xEDB88320= reverse 0x04C11DB7
  317. }
  318. else
  319. {
  320. crc = (crc >> 1);
  321. }
  322. }
  323. }
  324. cnt++;
  325. }
  326. return ~crc;
  327. }
  328. /*===========================================================================
  329. FUNCTION: Get_GPIO_Value
  330. DESCRIPTION:
  331. PRE-CONDITION:
  332. INPUT:
  333. OUTPUT:
  334. GLOBAL VARIABLES:
  335. =============================================================================*/
  336. int Get_GPIO_Value(unsigned int gpio)
  337. {
  338. int fd;
  339. char buf[32];
  340. char ch;
  341. snprintf(buf, sizeof(buf), GPIO_SYS_DIR"/gpio%d/value", gpio);
  342. fd = open(buf, O_RDONLY);
  343. if (fd < 0)
  344. {
  345. perror("gpio/get-value");
  346. return fd;
  347. }
  348. read(fd, &ch, 1);
  349. close(fd);
  350. if (ch != '0')
  351. {
  352. return 1;
  353. }
  354. else
  355. {
  356. return 0;
  357. }
  358. }
  359. /*===========================================================================
  360. FUNCTION: DiffTimeb
  361. DESCRIPTION:
  362. 1. Data structure of timeb.h
  363. The <sys/timeb.h> header defines the timeb structure that includes at least the following members:
  364. - time_t time the seconds portion of the current time
  365. - unsigned short millitm the milliseconds portion of the current time
  366. - short timezone the local timezone in minutes west of Greenwich
  367. - short dstflag TRUE if Daylight Savings Time is in effect
  368. PRE-CONDITION:
  369. INPUT:
  370. OUTPUT:
  371. GLOBAL VARIABLES:
  372. =============================================================================*/
  373. double DiffTimeb(struct timeb ST, struct timeb ET)
  374. {
  375. //return milli-second
  376. double StartTime, EndTime;
  377. double t_diff;
  378. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  379. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  380. t_diff = EndTime - StartTime;
  381. //printf("%.02lf - %.02lf = %.02lf\n", EndTime, StartTime, t_diff);
  382. if (t_diff < 0)
  383. {
  384. #if 0
  385. if (t_diff < -1000) //1000ms
  386. {
  387. sprintf(buf_log_csucomm,
  388. "[Warning]StartTime(%.02lf) > EndTime(%.02lf), d(%.02lf)",
  389. StartTime,
  390. EndTime,
  391. t_diff);
  392. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  393. }
  394. #endif
  395. return -1;
  396. }
  397. return t_diff;
  398. }
  399. /*===========================================================================
  400. FUNCTION: DiffTimeb_CsuComm_fork1
  401. DESCRIPTION:
  402. 1. fork1
  403. PRE-CONDITION:
  404. INPUT:
  405. OUTPUT:
  406. GLOBAL VARIABLES:
  407. =============================================================================*/
  408. double DiffTimeb_CsuComm_fork1(struct timeb ST, struct timeb ET)
  409. {
  410. //return milli-second
  411. double StartTime, EndTime;
  412. double t_diff;
  413. StartTime = ((double)ST.time)*1000 + (double)ST.millitm;
  414. EndTime = ((double)ET.time)*1000 + (double)ET.millitm;
  415. t_diff = EndTime - StartTime;
  416. if (t_diff < 0)
  417. {
  418. #if 0
  419. if (t_diff < -1000) //1000ms
  420. {
  421. sprintf(buf_log_csucomm_fork1,
  422. "[fork1][Warning]StartTime(%.02lf) > EndTime(%.02lf), d(%.02lf)",
  423. StartTime,
  424. EndTime,
  425. t_diff);
  426. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm_fork1);
  427. }
  428. #endif
  429. return -1;
  430. }
  431. return t_diff;
  432. }
  433. /*===========================================================================
  434. FUNCTION: ShareMemory_Init
  435. DESCRIPTION
  436. Initialize all share memories.
  437. PRE-CONDITION:
  438. INPUT:
  439. OUTPUT:
  440. GLOBAL VARIABLES:
  441. =============================================================================*/
  442. int ShareMemory_Init()
  443. {
  444. int MeterSMId;
  445. //[1/5] create ShmSysConfigAndInfo
  446. if((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  447. {
  448. SAVE_SYS_LOG_MSG_CSUCOMM("ShareMemory_Init:shmget ShmSysConfigAndInfo NG");
  449. return 0;
  450. }
  451. else if((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  452. {
  453. SAVE_SYS_LOG_MSG_CSUCOMM("ShareMemory_Init:shmat ShmSysConfigAndInfo NG");
  454. return 0;
  455. }
  456. //[2/5] create ShmStatusCodeData
  457. if((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  458. {
  459. SAVE_SYS_LOG_MSG_CSUCOMM("ShareMemory_Init:shmget ShmStatusCodeData NG");
  460. return 0;
  461. }
  462. else if((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  463. {
  464. SAVE_SYS_LOG_MSG_CSUCOMM("ShareMemory_Init:shmat ShmStatusCodeData NG");
  465. return 0;
  466. }
  467. //[3/5] create ShmCcsData
  468. if((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), 0777)) < 0)
  469. {
  470. SAVE_SYS_LOG_MSG_CSUCOMM("ShareMemory_Init:shmget ShmCcsData NG");
  471. return 0;
  472. }
  473. else if((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  474. {
  475. SAVE_SYS_LOG_MSG_CSUCOMM("ShareMemory_Init:shmat ShmCcsData NG");
  476. return 0;
  477. }
  478. //[4/5] create ShmInternalComm
  479. if((MeterSMId = shmget(ShmInternalCommKey, sizeof(struct InternalComm), IPC_CREAT | 0777)) < 0)
  480. {
  481. SAVE_SYS_LOG_MSG_CSUCOMM("ShareMemory_Init:shmget ShmInternalComm NG");
  482. return 0;
  483. }
  484. else if((ShmInternalComm = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  485. {
  486. SAVE_SYS_LOG_MSG_CSUCOMM("ShareMemory_Init:shmat ShmInternalComm NG");
  487. return 0;
  488. }
  489. //memset(ShmInternalComm, 0, sizeof(struct InternalComm));
  490. //[5/5] create ShmInternalCommAC
  491. if((MeterSMId = shmget(ShmInternalCommACKey, sizeof(struct InternalCommAC), IPC_CREAT | 0777)) < 0)
  492. {
  493. SAVE_SYS_LOG_MSG_CSUCOMM("ShareMemory_Init:shmget ShmInternalCommAC NG");
  494. return 0;
  495. }
  496. else if((ShmInternalCommAC = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  497. {
  498. SAVE_SYS_LOG_MSG_CSUCOMM("ShareMemory_Init:shmat ShmInternalCommAC NG");
  499. return 0;
  500. }
  501. //memset(ShmInternalCommAC, 0, sizeof(struct InternalCommAC));
  502. return 1;
  503. }
  504. /*===========================================================================
  505. FUNCTION: CANBus_Init
  506. DESCRIPTION:
  507. PRE-CONDITION:
  508. INPUT:
  509. OUTPUT:
  510. GLOBAL VARIABLES:
  511. =============================================================================*/
  512. int CANBus_Init()
  513. {
  514. int s0, nbytes;
  515. struct timeval tv;
  516. struct ifreq ifr0;
  517. struct sockaddr_can addr0;
  518. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  519. system("/sbin/ip link set can0 up");
  520. s0 = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  521. if (s0 < 0) //added by Joseph (not full implemented)
  522. {
  523. SAVE_SYS_LOG_MSG_CSUCOMM("[ERROR] Fail on initializing CAN Bus socket");
  524. }
  525. tv.tv_sec = 0;
  526. tv.tv_usec = 10000;
  527. if(setsockopt(s0, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
  528. {
  529. SAVE_SYS_LOG_MSG_CSUCOMM("CANBus_Init:Set SO_RCVTIMEO NG");
  530. }
  531. nbytes = 40960;
  532. if(setsockopt(s0, SOL_SOCKET, SO_RCVBUF, &nbytes, sizeof(int)) < 0)
  533. {
  534. SAVE_SYS_LOG_MSG_CSUCOMM("CANBus_Init:Set SO_RCVBUF NG");
  535. }
  536. nbytes = 40960;
  537. if(setsockopt(s0, SOL_SOCKET, SO_SNDBUF, &nbytes, sizeof(int)) < 0)
  538. {
  539. SAVE_SYS_LOG_MSG_CSUCOMM("CANBus_Init:Set SO_SNDBUF NG");
  540. }
  541. strcpy(ifr0.ifr_name, "can0" );
  542. ioctl(s0, SIOCGIFINDEX, &ifr0); /* ifr.ifr_ifindex gets filled with that device's index */
  543. addr0.can_family = AF_CAN;
  544. addr0.can_ifindex = ifr0.ifr_ifindex;
  545. bind(s0, (struct sockaddr *)&addr0, sizeof(addr0));
  546. return s0;
  547. }
  548. /*===========================================================================
  549. FUNCTION: Update_EVSE_INFO_to_SHMs
  550. DESCRIPTION:
  551. Updating all corresponding content in each Share Memory.
  552. 1. I_now
  553. 2. V_now
  554. 3. P_now
  555. 4. I_max
  556. 5. V_max
  557. 6. P_max
  558. PRE-CONDITION:
  559. INPUT:
  560. 1. ShmInternalComm
  561. OUTPUT:
  562. 1. ShmCcsData
  563. * PreChargeResponse
  564. * CurrentDemandResponse
  565. * WeldingDetectionResponse
  566. 2. ShmSysConfigAndInfo
  567. GLOBAL VARIABLES:
  568. =============================================================================*/
  569. void Update_EVSE_INFO_to_SHMs()
  570. {
  571. int I_now = 0; //0.1A
  572. int V_now = 0; //0.1V
  573. int P_now = 0; //0.1KW
  574. int I_max = 0; //0.1A
  575. int V_max = 0; //0.1V
  576. int P_max = 0; //0.1KW
  577. //[CAUTION] Re-initializing process is necessary. (to-be implemented)
  578. //----------[1/7] Permission --------
  579. if (ShmInternalComm->ChargingPermission_new != ShmInternalComm->ChargingPermission)
  580. {
  581. sprintf(buf_log_csucomm, "Permission: %d >> %d",
  582. ShmInternalComm->ChargingPermission,
  583. ShmInternalComm->ChargingPermission_new
  584. );
  585. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  586. ShmInternalComm->ChargingPermission_pre = ShmInternalComm->ChargingPermission;
  587. ShmInternalComm->ChargingPermission = ShmInternalComm->ChargingPermission_new;
  588. }
  589. // -------- [2/7] I_now --------
  590. I_now = ShmInternalComm->PresentChargingCurrent;
  591. EVCOMM_SYS_INFO.PresentChargingCurrent = I_now / 10.0; //1A
  592. if (ShmInternalComm->PresentChargingCurrent_pre != ShmInternalComm->PresentChargingCurrent)
  593. {
  594. //if (abs(ShmInternalComm->PresentChargingCurrent_pre - ShmInternalComm->PresentChargingCurrent) > 10) //10/;1A
  595. //{
  596. //memset(buf_log_csucomm, 0, sizeof(buf_log_csucomm));
  597. sprintf(buf_log_csucomm, "I_now(EVSE): %d >> %d (0.1A, DEC)",
  598. ShmInternalComm->PresentChargingCurrent_pre,
  599. ShmInternalComm->PresentChargingCurrent
  600. );
  601. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  602. //}
  603. ShmInternalComm->PresentChargingCurrent_pre = ShmInternalComm->PresentChargingCurrent;
  604. }
  605. // -------- [3/7] V_now --------
  606. V_now = ShmInternalComm->PresentChargingVoltage;
  607. EVCOMM_SYS_INFO.PresentChargingVoltage = V_now / 10.0; //1V
  608. if (ShmInternalComm->PresentChargingVoltage_pre != ShmInternalComm->PresentChargingVoltage)
  609. {
  610. if (abs(ShmInternalComm->PresentChargingVoltage_pre - ShmInternalComm->PresentChargingVoltage) > 10) //10:1V
  611. {
  612. unsigned char state = 0;
  613. state = Check_V2G_Flow_Status();
  614. if(state == CableCheckRequest || state == CableCheckResponse || //37, 38
  615. state == PreChargeRequest || state == PreChargeResponse || //39, 40
  616. state == CurrentDemandRequest || state == CurrentDemandResponse) //45, 46
  617. {
  618. if ((ShmInternalComm->PresentChargingVoltage <= 600) || //600:60V
  619. (ShmInternalComm->PresentChargingVoltage >= 1500) //1500:150V
  620. )
  621. {
  622. sprintf(buf_log_csucomm, "V_now(EVSE): %d >> %d (0.1V, DEC)",
  623. ShmInternalComm->PresentChargingVoltage_pre,
  624. ShmInternalComm->PresentChargingVoltage
  625. );
  626. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  627. }
  628. }
  629. else
  630. {
  631. sprintf(buf_log_csucomm, "V_now(EVSE): %d >> %d (0.1V, DEC)",
  632. ShmInternalComm->PresentChargingVoltage_pre,
  633. ShmInternalComm->PresentChargingVoltage
  634. );
  635. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  636. }
  637. }
  638. ShmInternalComm->PresentChargingVoltage_pre = ShmInternalComm->PresentChargingVoltage;
  639. }
  640. // -------- [4/7] P_now --------
  641. ShmInternalComm->PresentChargingPower = (int)(((I_now/10) * (V_now/10)) / 100); //0.1KW
  642. P_now = ShmInternalComm->PresentChargingPower;
  643. EVCOMM_SYS_INFO.PresentChargingPower = P_now * 10.0; //1KW
  644. if (ShmInternalComm->PresentChargingPower_pre != ShmInternalComm->PresentChargingPower)
  645. {
  646. sprintf(buf_log_csucomm, "P_now(EVSE): %d >> %d (0.1KW, DEC)",
  647. ShmInternalComm->PresentChargingPower_pre,
  648. ShmInternalComm->PresentChargingPower);
  649. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  650. ShmInternalComm->PresentChargingPower_pre = ShmInternalComm->PresentChargingPower;
  651. }
  652. // -------- [5/7] I_max --------
  653. I_max = ShmInternalComm->AvailableChargingCurrent;
  654. EVCOMM_SYS_INFO.AvailableChargingCurrent = I_max / 10.0; //1A
  655. if (ShmInternalComm->AvailableChargingCurrent_pre != ShmInternalComm->AvailableChargingCurrent)
  656. {
  657. sprintf(buf_log_csucomm, "I_max(EVSE): %d >> %d (0.1A, DEC)",
  658. ShmInternalComm->AvailableChargingCurrent_pre,
  659. ShmInternalComm->AvailableChargingCurrent);
  660. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  661. ShmInternalComm->AvailableChargingCurrent_pre = ShmInternalComm->AvailableChargingCurrent;
  662. }
  663. // -------- [6/7] V_max --------
  664. V_max = ShmInternalComm->MaximumChargingVoltage;
  665. EVCOMM_SYS_INFO.MaximumChargingVoltage = V_max / 10.0; //1V
  666. if (ShmInternalComm->MaximumChargingVoltage_pre != ShmInternalComm->MaximumChargingVoltage)
  667. {
  668. sprintf(buf_log_csucomm, "V_max(EVSE): %d >> %d (0.1V, DEC)",
  669. ShmInternalComm->MaximumChargingVoltage_pre,
  670. ShmInternalComm->MaximumChargingVoltage
  671. );
  672. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  673. ShmInternalComm->MaximumChargingVoltage_pre = ShmInternalComm->MaximumChargingVoltage;
  674. }
  675. // -------- [7/7] P_max --------
  676. P_max = ShmInternalComm->AvailableChargingPower;
  677. EVCOMM_SYS_INFO.AvailableChargingPower = P_max / 10.0; //1KW
  678. if (ShmInternalComm->AvailableChargingPower_pre != ShmInternalComm->AvailableChargingPower)
  679. {
  680. sprintf(buf_log_csucomm, "P_max(EVSE): %d >> %d (0.1KW, DEC)",
  681. ShmInternalComm->AvailableChargingPower_pre,
  682. ShmInternalComm->AvailableChargingPower);
  683. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  684. ShmInternalComm->AvailableChargingPower_pre = ShmInternalComm->AvailableChargingPower;
  685. }
  686. }
  687. /*===========================================================================
  688. FUNCTION: Proc_EVBoardStatusRes
  689. DESCRIPTION:
  690. PRE-CONDITION:
  691. INPUT:
  692. OUTPUT:
  693. GLOBAL VARIABLES:
  694. =============================================================================*/
  695. int Proc_EVBoardStatusRes(int Fd)
  696. {
  697. int nbytes;
  698. unsigned char Buffer[8];
  699. memset(Buffer, 0, sizeof(Buffer));
  700. Buffer[0] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].ConnectorPlugIn;
  701. Buffer[1] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].CpState;
  702. /*
  703. if(strlen(ShmStatusCodeData->PresentStatusCode[0]) > 0)
  704. {
  705. memcpy(Buffer + 2, ShmStatusCodeData->PresentStatusCode[0], 6);
  706. }
  707. */
  708. Buffer[2] = ShmStatusCodeData->PresentStatusCode[0][0];
  709. Buffer[3] = ShmStatusCodeData->PresentStatusCode[0][1];
  710. Buffer[4] = ShmStatusCodeData->PresentStatusCode[0][2];
  711. Buffer[5] = ShmStatusCodeData->PresentStatusCode[0][3];
  712. Buffer[6] = ShmStatusCodeData->PresentStatusCode[0][4];
  713. Buffer[7] = ShmStatusCodeData->PresentStatusCode[0][5];
  714. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_EV_BOARD_STATUS, ShmInternalComm->SlaveAddress, 8, Buffer);
  715. CSUCOMMDC_TASK_FLAG.Send_EVBoardStatus = FALSE;
  716. return nbytes;
  717. }
  718. /*===========================================================================
  719. FUNCTION: Proc_AddressAssignRes
  720. DESCRIPTION:
  721. PRE-CONDITION:
  722. INPUT:
  723. OUTPUT:
  724. GLOBAL VARIABLES:
  725. =============================================================================*/
  726. void Proc_AddressAssignRes(int Fd)
  727. {
  728. }
  729. /*===========================================================================
  730. FUNCTION: Proc_AddressAssignReq
  731. DESCRIPTION:
  732. PRE-CONDITION:
  733. INPUT:
  734. OUTPUT:
  735. GLOBAL VARIABLES:
  736. =============================================================================*/
  737. void Proc_AddressAssignReq(struct can_frame *frame)
  738. {
  739. //[CAUTION] There should not be such message ID here.
  740. // A warning alarm should be recorded.
  741. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][Proc_AddressAssignReq] Unexpected message of address assignment from CSU.\n");
  742. ShmInternalComm->SlaveAddress = frame->can_id & 0x000000FF;
  743. CSUCOMMDC_TASK_FLAG.Got_AssignedAddress = TRUE;
  744. }
  745. /*===========================================================================
  746. FUNCTION: Proc_GetFirmwareVersionRes
  747. DESCRIPTION:
  748. PRE-CONDITION:
  749. INPUT:
  750. OUTPUT:
  751. GLOBAL VARIABLES:
  752. =============================================================================*/
  753. int Proc_GetFirmwareVersionRes(int Fd)
  754. {
  755. int i = 0, j = 0, nbytes = 0;
  756. unsigned char str[FIRMWARE_VERSION_LENGTH + 1];
  757. unsigned char buf[FIRMWARE_VERSION_LENGTH];
  758. memset(buf, 0, sizeof(buf));
  759. strcpy(str, FIRMWARE_VERSION);
  760. for(i = 0; i < FIRMWARE_VERSION_LENGTH + 3; i++)
  761. {
  762. if (str[i] != '_')
  763. {
  764. buf[j] = str[i];
  765. j++;
  766. }
  767. }
  768. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_GET_FW_VERSION, ShmInternalComm->SlaveAddress, FIRMWARE_VERSION_LENGTH, buf);
  769. CSUCOMMDC_TASK_FLAG.Got_FWVersionReq = FALSE;
  770. return nbytes;
  771. }
  772. /*===========================================================================
  773. FUNCTION: Proc_GetFirmwareVersionReq
  774. DESCRIPTION:
  775. PRE-CONDITION:
  776. INPUT:
  777. OUTPUT:
  778. GLOBAL VARIABLES:
  779. =============================================================================*/
  780. void Proc_GetFirmwareVersionReq(struct can_frame *frame)
  781. {
  782. CSUCOMMDC_TASK_FLAG.Got_FWVersionReq = TRUE;
  783. }
  784. /*===========================================================================
  785. FUNCTION: Proc_HardwareVersionRes
  786. DESCRIPTION:
  787. PRE-CONDITION:
  788. INPUT:
  789. OUTPUT:
  790. GLOBAL VARIABLES:
  791. =============================================================================*/
  792. int Proc_HardwareVersionRes(int Fd)
  793. {
  794. int i = 0, nbytes = 0;
  795. unsigned char str[HARDWARE_VERSION_LENGTH + 1];
  796. unsigned char buf[HARDWARE_VERSION_LENGTH];
  797. memset(buf, 0, sizeof(buf));
  798. strcpy(str, HARDWARE_VERSION);
  799. for(i = 0; i < HARDWARE_VERSION_LENGTH; i++)
  800. {
  801. buf[i] = str[i];
  802. }
  803. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_GET_HW_VERSION, ShmInternalComm->SlaveAddress, HARDWARE_VERSION_LENGTH, buf);
  804. CSUCOMMDC_TASK_FLAG.Got_HWVersionReq = FALSE;
  805. return nbytes;
  806. }
  807. /*===========================================================================
  808. FUNCTION: Proc_HardwareVersionReq
  809. DESCRIPTION:
  810. PRE-CONDITION:
  811. INPUT:
  812. OUTPUT:
  813. GLOBAL VARIABLES:
  814. =============================================================================*/
  815. void Proc_HardwareVersionReq(struct can_frame *frame)
  816. {
  817. CSUCOMMDC_TASK_FLAG.Got_HWVersionReq = TRUE;
  818. }
  819. /*===========================================================================
  820. FUNCTION: Proc_GetMiscellaneousInfoRes
  821. DESCRIPTION:
  822. PRE-CONDITION:
  823. INPUT:
  824. OUTPUT:
  825. GLOBAL VARIABLES:
  826. =============================================================================*/
  827. int Proc_GetMiscellaneousInfoRes(int Fd)
  828. {
  829. //DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][Proc_GetMiscellaneousInfoRes] To-be implemented.\n");
  830. int nbytes;
  831. unsigned char Buffer[8];
  832. unsigned short TmpValue;
  833. memset(Buffer, 0, sizeof(Buffer));
  834. Buffer[0] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].ConnectorLocked;
  835. TmpValue = ShmSysConfigAndInfo->SysInfo.CcsConnectorTemp;
  836. memcpy(Buffer + 1, &TmpValue, 2);
  837. Buffer[3] = (unsigned char)(ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].CpVoltage * 10);
  838. Buffer[4] = ShmSysConfigAndInfo->SysInfo.CcsChargingData[0].CpState;
  839. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_GET_MISC_INFO, ShmInternalComm->SlaveAddress, 7, Buffer);
  840. CSUCOMMDC_TASK_FLAG.Got_MiscellaneousInfoReq = FALSE;
  841. return nbytes;
  842. }
  843. /*===========================================================================
  844. FUNCTION: Proc_GetMiscellaneousInfoReq
  845. DESCRIPTION:
  846. PRE-CONDITION:
  847. INPUT:
  848. OUTPUT:
  849. GLOBAL VARIABLES:
  850. =============================================================================*/
  851. void Proc_GetMiscellaneousInfoReq(struct can_frame *frame)
  852. {
  853. CSUCOMMDC_TASK_FLAG.Got_MiscellaneousInfoReq = TRUE;
  854. }
  855. /*===========================================================================
  856. FUNCTION: Proc_ChargingPermissionRes
  857. DESCRIPTION:
  858. PRE-CONDITION:
  859. INPUT:
  860. OUTPUT:
  861. GLOBAL VARIABLES:
  862. =============================================================================*/
  863. int Proc_ChargingPermissionRes(int Fd)
  864. {
  865. //Save into ShmCcsData
  866. //[1/2] Updating Parameters
  867. Update_EVSE_INFO_to_SHMs();
  868. //[2/2] Check if SECC is in the End_Process or Initial stage
  869. if ((EVCOMM_SYS_INFO.End_Process_inused == TRUE) ||
  870. (EVCOMM_SYS_INFO.QCA7K_SetKeyDone == FALSE))
  871. {
  872. if (ShmInternalComm->ChargingPermission == TRUE)
  873. {
  874. //Update_ShmStatusCode(); //[To-Do] to be implemented
  875. //CCS_SECC_Not_Ready_For_Charging (023891)
  876. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  877. ShmStatusCodeData->PresentStatusCode[0][1] = 2;
  878. ShmStatusCodeData->PresentStatusCode[0][2] = 3;
  879. ShmStatusCodeData->PresentStatusCode[0][3] = 8;
  880. ShmStatusCodeData->PresentStatusCode[0][4] = 9;
  881. ShmStatusCodeData->PresentStatusCode[0][5] = 1;
  882. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  883. //This Stop command will be delivered to CSU once SECC receives
  884. //one permission as TRUE command.
  885. }
  886. }
  887. CSUCOMMDC_TASK_FLAG.Got_ChargingPermission = FALSE;
  888. }
  889. /*===========================================================================
  890. FUNCTION: Proc_ChargingPermissionReq
  891. DESCRIPTION:
  892. PRE-CONDITION:
  893. INPUT:
  894. OUTPUT:
  895. GLOBAL VARIABLES:
  896. =============================================================================*/
  897. void Proc_ChargingPermissionReq(struct can_frame *frame)
  898. {
  899. ShmInternalComm->ChargingPermission_new = frame->data[0];
  900. ShmInternalComm->AvailableChargingPower = (unsigned int) ((frame->data[2] << 8) | frame->data[1]);
  901. ShmInternalComm->AvailableChargingCurrent = (unsigned int) ((frame->data[4] << 8) | frame->data[3]);
  902. ShmInternalComm->MaximumChargingVoltage = (unsigned int) ((frame->data[6] << 8) | frame->data[5]);
  903. ShmInternalComm->MaximumAllowedChargingTime = frame->data[7]; //0: unlimited, 0x01: 1 minutes ~ 0xFF: 255 minutes, resolution: 1 minute
  904. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][CAN_Rx]\n\
  905. \t - AvailableChargingPower = (%d, %02X, %02X)\n\
  906. \t - AvailableChargingCurrent = (%d, %02X, %02X)\n\
  907. \t - MaximumChargingVoltage = (%d, %02X, %02X)\n",
  908. ShmInternalComm->AvailableChargingPower, frame->data[2], frame->data[1],
  909. ShmInternalComm->AvailableChargingCurrent, frame->data[4], frame->data[3],
  910. ShmInternalComm->MaximumChargingVoltage, frame->data[6], frame->data[5]
  911. );
  912. //Update Flag
  913. CSUCOMMDC_TASK_FLAG.Got_ChargingPermission = TRUE;
  914. }
  915. /*===========================================================================
  916. FUNCTION: Proc_EVSEOutputStatusUpdateRes
  917. DESCRIPTION:
  918. PRE-CONDITION:
  919. INPUT:
  920. OUTPUT:
  921. GLOBAL VARIABLES:
  922. =============================================================================*/
  923. int Proc_EVSEOutputStatusUpdateRes(int Fd)
  924. {
  925. Update_EVSE_INFO_to_SHMs();
  926. //Update Flag
  927. CSUCOMMDC_TASK_FLAG.Got_EVSEOutputStatus = FALSE;
  928. }
  929. /*===========================================================================
  930. FUNCTION: Proc_EVSEOutputStatusUpdateReq
  931. DESCRIPTION:
  932. PRE-CONDITION:
  933. INPUT:
  934. OUTPUT:
  935. GLOBAL VARIABLES:
  936. =============================================================================*/
  937. void Proc_EVSEOutputStatusUpdateReq(struct can_frame *frame)
  938. {
  939. if(ShmInternalComm->SlaveAddress == 1)
  940. {
  941. ShmInternalComm->PresentChargingVoltage = ((unsigned int)frame->data[1] << 8 | frame->data[0]);
  942. ShmInternalComm->PresentChargingCurrent = ((unsigned int)frame->data[3] << 8 | frame->data[2]);
  943. }
  944. else
  945. {
  946. ShmInternalComm->PresentChargingVoltage = ((unsigned int)frame->data[5] << 8 | frame->data[4]);
  947. ShmInternalComm->PresentChargingCurrent = ((unsigned int)frame->data[7] << 8 | frame->data[6]);
  948. }
  949. //Update Flag
  950. CSUCOMMDC_TASK_FLAG.Got_EVSEOutputStatus = TRUE;
  951. }
  952. /*===========================================================================
  953. FUNCTION: Proc_EVSECapacityUpdateRes
  954. DESCRIPTION:
  955. PRE-CONDITION:
  956. INPUT:
  957. OUTPUT:
  958. GLOBAL VARIABLES:
  959. =============================================================================*/
  960. int Proc_EVSECapacityUpdateRes(int Fd)
  961. {
  962. Update_EVSE_INFO_to_SHMs();
  963. CSUCOMMDC_TASK_FLAG.Got_EnergyCapacity = FALSE;
  964. }
  965. /*===========================================================================
  966. FUNCTION: Proc_EVSECapacityUpdateReq
  967. DESCRIPTION:
  968. PRE-CONDITION:
  969. INPUT:
  970. OUTPUT:
  971. GLOBAL VARIABLES:
  972. =============================================================================*/
  973. void Proc_EVSECapacityUpdateReq(struct can_frame *frame)
  974. {
  975. if(ShmInternalComm->SlaveAddress == 1)
  976. {
  977. ShmInternalComm->AvailableChargingPower = ((unsigned int)frame->data[1] << 8 | frame->data[0]);
  978. ShmInternalComm->AvailableChargingCurrent = ((unsigned int)frame->data[3] << 8 | frame->data[2]);
  979. }
  980. else
  981. {
  982. ShmInternalComm->AvailableChargingPower = ((unsigned int)frame->data[5] << 8 | frame->data[4]);
  983. ShmInternalComm->AvailableChargingCurrent = ((unsigned int)frame->data[7] << 8 | frame->data[6]);
  984. }
  985. //Update Flag
  986. CSUCOMMDC_TASK_FLAG.Got_EnergyCapacity = TRUE;
  987. }
  988. /*===========================================================================
  989. FUNCTION: Proc_GetEVTargetRes
  990. DESCRIPTION:
  991. #define CAN_CMD_GET_EV_TARGET_INFO 0x00000900
  992. PRE-CONDITION:
  993. INPUT:
  994. OUTPUT:
  995. GLOBAL VARIABLES:
  996. =============================================================================*/
  997. int Proc_GetEVTargetRes(int Fd)
  998. {
  999. //DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][Proc_GetEVTargetRes] To-be implemented.\n");
  1000. int nbytes;
  1001. unsigned char Buffer[8];
  1002. unsigned short TmpValue;
  1003. static struct ChargingInfoData *sys;
  1004. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1005. memset(Buffer, 0, sizeof(Buffer));
  1006. Buffer[0] = Check_V2G_Flow_Status();
  1007. //[1/4] SOC
  1008. if (sys->EvBatterySoc_pre != sys->EvBatterySoc)
  1009. {
  1010. //memset(buf_log_csucomm, 0, sizeof(buf_log_csucomm));
  1011. sprintf(buf_log_csucomm, "SOC = %d >> %d",
  1012. sys->EvBatterySoc_pre,
  1013. sys->EvBatterySoc
  1014. );
  1015. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1016. sys->EvBatterySoc_pre = sys->EvBatterySoc;
  1017. }
  1018. Buffer[1] = sys->EvBatterySoc;
  1019. //printf("[CsuComm] V_ev_target = %.02f\n", sys->EvBatterytargetVoltage);
  1020. //[2/4] EV Target Voltage
  1021. if (sys->EvBatterytargetVoltage_pre != sys->EvBatterytargetVoltage)
  1022. {
  1023. //memset(buf_log_csucomm, 0, sizeof(buf_log_csucomm));
  1024. sprintf(buf_log_csucomm, "V_target = %.02f >> %.02f (1V, DEC)",
  1025. sys->EvBatterytargetVoltage_pre,
  1026. sys->EvBatterytargetVoltage
  1027. );
  1028. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1029. sys->EvBatterytargetVoltage_pre = sys->EvBatterytargetVoltage;
  1030. }
  1031. TmpValue = (unsigned short) (sys->EvBatterytargetVoltage * 10);
  1032. memcpy(Buffer + 2, &TmpValue, 2);
  1033. //[3/4] EV Target Current
  1034. if (sys->EvBatterytargetCurrent_pre != sys->EvBatterytargetCurrent)
  1035. {
  1036. //memset(buf_log_csucomm, 0, sizeof(buf_log_csucomm));
  1037. sprintf(buf_log_csucomm, "I_target = %.02f >> %.02f (1A, DEC)",
  1038. sys->EvBatterytargetCurrent_pre,
  1039. sys->EvBatterytargetCurrent
  1040. );
  1041. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1042. sys->EvBatterytargetCurrent_pre = sys->EvBatterytargetCurrent;
  1043. }
  1044. TmpValue = (unsigned short) (sys->EvBatterytargetCurrent * 10);
  1045. memcpy(Buffer + 4, &TmpValue, 2);
  1046. //[4/4] Remaining Time
  1047. TmpValue = (unsigned short) sys->RemainChargingDuration;
  1048. memcpy(Buffer + 6, &TmpValue, 2);
  1049. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_GET_EV_TARGET_INFO, ShmInternalComm->SlaveAddress, 8, Buffer);
  1050. CSUCOMMDC_TASK_FLAG.Got_EVTargetReq = FALSE;
  1051. return nbytes;
  1052. }
  1053. /*===========================================================================
  1054. FUNCTION: Proc_GetEVTargetReq
  1055. DESCRIPTION:
  1056. PRE-CONDITION:
  1057. INPUT:
  1058. OUTPUT:
  1059. GLOBAL VARIABLES:
  1060. =============================================================================*/
  1061. void Proc_GetEVTargetReq(struct can_frame *frame)
  1062. {
  1063. CSUCOMMDC_TASK_FLAG.Got_EVTargetReq = TRUE;
  1064. }
  1065. /*===========================================================================
  1066. FUNCTION: Proc_GetEVBatteryInfoRes
  1067. DESCRIPTION:
  1068. PRE-CONDITION:
  1069. INPUT:
  1070. OUTPUT:
  1071. GLOBAL VARIABLES:
  1072. =============================================================================*/
  1073. int Proc_GetEVBatteryInfoRes(int Fd)
  1074. {
  1075. int nbytes;
  1076. unsigned char Buffer[8];
  1077. unsigned short TmpValue;
  1078. memset(Buffer, 0, sizeof(Buffer));
  1079. //[To-Do] Adding the consideration of ISO15118_2018
  1080. if((ShmCcsData->CommProtocol == V2GT_MSG_PROTOCOL_ISO15118_2014) &&
  1081. (ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.RequestedEnergyTransferMode <= 1))
  1082. {
  1083. Buffer[0] = 1;//AC
  1084. }
  1085. else
  1086. {
  1087. Buffer[0] = 0;//DC
  1088. }
  1089. if(ShmCcsData->CommProtocol == V2GT_MSG_PROTOCOL_DIN70121)
  1090. {
  1091. //DIN70121
  1092. TmpValue = DIN70121PhyValDecode(ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVEnergyCapacity) * 10;
  1093. memcpy(Buffer + 1, &TmpValue, 2);
  1094. TmpValue = DIN70121PhyValDecode(ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVMaximumVoltageLimit) * 10;
  1095. memcpy(Buffer + 3, &TmpValue, 2);
  1096. TmpValue = DIN70121PhyValDecode(ShmCcsData->V2GMessage_DIN70121.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVMaximumCurrentLimit) * 10;
  1097. memcpy(Buffer + 5, &TmpValue, 2);
  1098. }
  1099. else if(ShmCcsData->CommProtocol == V2GT_MSG_PROTOCOL_ISO15118_2014)
  1100. {
  1101. //ISO15118_2014
  1102. if(Buffer[0] == 0)
  1103. {
  1104. //DC
  1105. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVEnergyCapacity) * 10;
  1106. memcpy(Buffer + 1, &TmpValue, 2);
  1107. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVMaximumVoltageLimit) * 10;
  1108. memcpy(Buffer + 3, &TmpValue, 2);
  1109. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.DC_EVChargeParameter.EVMaximumCurrentLimit) * 10;
  1110. memcpy(Buffer + 5, &TmpValue, 2);
  1111. }
  1112. else
  1113. {
  1114. //AC
  1115. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.AC_EVChargeParameter.EAmount) * 10;
  1116. memcpy(Buffer + 1, &TmpValue, 2);
  1117. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.AC_EVChargeParameter.EVMaxVoltage) * 10;
  1118. memcpy(Buffer + 3, &TmpValue, 2);
  1119. TmpValue = ISO151182014PhyValDecode(ShmCcsData->V2GMessage_ISO15118_2014.ChargeParameterDiscoveryRequest.AC_EVChargeParameter.EVMaxCurrent) * 10;
  1120. memcpy(Buffer + 5, &TmpValue, 2);
  1121. }
  1122. }
  1123. else if(ShmCcsData->CommProtocol == V2GT_MSG_PROTOCOL_ISO15118_2018)
  1124. {
  1125. //to be implemented
  1126. }
  1127. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_GET_EV_BATTERY_INFO, ShmInternalComm->SlaveAddress, 7, Buffer);
  1128. CSUCOMMDC_TASK_FLAG.Got_EVBatteryInfoReq = FALSE;
  1129. return nbytes;
  1130. }
  1131. /*===========================================================================
  1132. FUNCTION: Proc_GetEVBatteryInfoReq
  1133. DESCRIPTION:
  1134. PRE-CONDITION:
  1135. INPUT:
  1136. OUTPUT:
  1137. GLOBAL VARIABLES:
  1138. =============================================================================*/
  1139. void Proc_GetEVBatteryInfoReq()
  1140. {
  1141. CSUCOMMDC_TASK_FLAG.Got_EVBatteryInfoReq = TRUE;
  1142. }
  1143. /*===========================================================================
  1144. FUNCTION: Proc_IsolationStatusAnnounceRes
  1145. DESCRIPTION:
  1146. - ShmInternalComm->IsolationStatus:
  1147. GFD_Invalid 0 //ongoing
  1148. GFD_Valid 1
  1149. GFD_Warning 2
  1150. GFD_Fault 3
  1151. GFD_No_IMD 4 //for ISO 15118
  1152. PRE-CONDITION:
  1153. INPUT:
  1154. OUTPUT:
  1155. GLOBAL VARIABLES:
  1156. =============================================================================*/
  1157. int Proc_IsolationStatusAnnounceRes(int Fd)
  1158. {
  1159. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][Proc_IsolationStatusAnnounceRes] %d (DEC, 0:ongoing, 1:valid, 2:warning/fault)\n",
  1160. ShmInternalComm->IsolationStatus);
  1161. if (ShmInternalComm->IsolationStatus_pre != ShmInternalComm->IsolationStatus)
  1162. {
  1163. //memset(buf_log_csucomm, 0, sizeof(buf_log_csucomm));
  1164. sprintf(buf_log_csucomm, "Isolation: %d >> %d",
  1165. ShmInternalComm->IsolationStatus_pre,
  1166. ShmInternalComm->IsolationStatus);
  1167. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1168. ShmInternalComm->IsolationStatus_pre = ShmInternalComm->IsolationStatus;
  1169. }
  1170. //DC_EVSEStatus.EVSEIsolationStatus = dinisolationLevelType_Invalid; //0 (default)
  1171. // dinisolationLevelType_Invalid = 0,
  1172. // dinisolationLevelType_Valid = 1, (default)
  1173. // dinisolationLevelType_Warning = 2,
  1174. // dinisolationLevelType_Fault = 3
  1175. CSUCOMMDC_TASK_FLAG.Got_IsolationStatus = FALSE;
  1176. }
  1177. /*===========================================================================
  1178. FUNCTION: Proc_IsolationStatusAnnounceReq
  1179. DESCRIPTION:
  1180. - BYTE[0]:
  1181. GFD_Invalid 0 //ongoing
  1182. GFD_Valid 1
  1183. GFD_Warning 2
  1184. GFD_Fault 3
  1185. GFD_No_IMD 4 //for ISO 15118
  1186. PRE-CONDITION:
  1187. INPUT:
  1188. OUTPUT:
  1189. GLOBAL VARIABLES:
  1190. =============================================================================*/
  1191. void Proc_IsolationStatusAnnounceReq(struct can_frame *frame)
  1192. {
  1193. unsigned char gfd_rx = 0;
  1194. gfd_rx = (unsigned char) frame->data[0];
  1195. if (gfd_rx >= GFD_Invalid && gfd_rx <= GFD_No_IMD)
  1196. {
  1197. //Transfer Parameter
  1198. if (gfd_rx == GFD_Warning)
  1199. {
  1200. gfd_rx = GFD_Fault;
  1201. }
  1202. else if (gfd_rx == GFD_Fault)
  1203. {
  1204. gfd_rx = GFD_Warning;
  1205. }
  1206. else
  1207. {
  1208. //no modification.
  1209. }
  1210. ShmInternalComm->IsolationStatus = gfd_rx;
  1211. EVCOMM_SYS_INFO.IsolationStatus = ShmInternalComm->IsolationStatus;
  1212. CSUCOMMDC_TASK_FLAG.Got_IsolationStatus = TRUE;
  1213. }
  1214. else
  1215. {
  1216. sprintf(buf_log_csucomm,
  1217. "[WARNING]unexpected isolation status(%d)",
  1218. frame->data[0]);
  1219. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1220. }
  1221. }
  1222. /*===========================================================================
  1223. FUNCTION: Proc_CCSConnectorTypeSetRes
  1224. DESCRIPTION:
  1225. PRE-CONDITION:
  1226. INPUT:
  1227. OUTPUT:
  1228. GLOBAL VARIABLES:
  1229. =============================================================================*/
  1230. int Proc_CCSConnectorTypeSetRes(int Fd)
  1231. {
  1232. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][Proc_CCSConnectorTypeSetRes] To-be implemented. (checking for PP or not)\n");
  1233. CSUCOMMDC_TASK_FLAG.Got_CCSConnectorReq = FALSE;
  1234. }
  1235. /*===========================================================================
  1236. FUNCTION: Proc_CCSConnectorTypeSetReq
  1237. DESCRIPTION:
  1238. PRE-CONDITION:
  1239. INPUT:
  1240. OUTPUT:
  1241. GLOBAL VARIABLES:
  1242. =============================================================================*/
  1243. void Proc_CCSConnectorTypeSetReq(struct can_frame *frame)
  1244. {
  1245. ShmInternalComm->CCSConnectorType = (unsigned char) frame->data[0];
  1246. CSUCOMMDC_TASK_FLAG.Got_CCSConnectorReq = TRUE;
  1247. }
  1248. /*===========================================================================
  1249. FUNCTION: Proc_RTCSetRes
  1250. DESCRIPTION:
  1251. PRE-CONDITION:
  1252. INPUT:
  1253. OUTPUT:
  1254. GLOBAL VARIABLES:
  1255. =============================================================================*/
  1256. int Proc_RTCSetRes(int Fd)
  1257. {
  1258. int nbytes;
  1259. unsigned char Buffer[1];
  1260. memset(Buffer, 0, sizeof(Buffer));
  1261. if (ShmInternalComm->RTC != 0)
  1262. {
  1263. //[CAUTION] updating RTC time only in IDLE mode
  1264. //ShmInternalComm->RTC = 1575461829;
  1265. sprintf(buf_log_csucomm, "[Proc_RTCSetRes] Got RTC Message: %lu (DEC)", ShmInternalComm->RTC);
  1266. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1267. system("date");
  1268. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_RTCSetRes] RTC is updating...");
  1269. #if 1
  1270. time_t rtc_new;
  1271. rtc_new = ShmInternalComm->RTC; //Epoch time
  1272. stime(&rtc_new);
  1273. ftime(&ShmInternalComm->Start_Time);
  1274. ftime(&ShmInternalComm->End_Time);
  1275. ftime(&ShmInternalComm->CAN_Rx_Timer_Start);
  1276. ftime(&ShmInternalComm->CAN_Rx_Timer_End);
  1277. #else //verified ok
  1278. char buf[64];
  1279. sprintf(buf, "date +%%s -s @%u", ShmInternalComm->RTC);
  1280. system(buf);
  1281. ftime(&ShmInternalComm->Start_Time);
  1282. ftime(&ShmInternalComm->End_Time);
  1283. ftime(&ShmInternalComm->CAN_Rx_Timer_Start);
  1284. ftime(&ShmInternalComm->CAN_Rx_Timer_End);
  1285. #endif
  1286. system("date");
  1287. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_RTCSetRes] RTC is updated.\n");
  1288. Buffer[0] = 1; //Accept
  1289. }
  1290. else
  1291. {
  1292. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_RTCSetRes] Reject.\n");
  1293. Buffer[0] = 0; //Reject
  1294. }
  1295. CSUCOMMDC_TASK_FLAG.Got_RTC = FALSE;
  1296. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_RTC_INFO, ShmInternalComm->SlaveAddress, 1, Buffer);
  1297. return nbytes;
  1298. }
  1299. /*===========================================================================
  1300. FUNCTION: Proc_RTCSetReq
  1301. DESCRIPTION:
  1302. PRE-CONDITION:
  1303. INPUT:
  1304. 1. Epoch time
  1305. OUTPUT:
  1306. GLOBAL VARIABLES:
  1307. =============================================================================*/
  1308. void Proc_RTCSetReq(struct can_frame *frame)
  1309. {
  1310. //STEP 1: If the RTC time from CSU is older than CCS default time, ignore it.
  1311. //(The RTC time of CSU might not be updated.)
  1312. long int rtc_tmp = 0;
  1313. memcpy(&rtc_tmp, &frame->data[0], 4); //Epoch time
  1314. if (rtc_tmp < RTC_DEFAULT_TIME)
  1315. {
  1316. sprintf(buf_log_csucomm,
  1317. "[WARNING] RTC time from CSU(%lu) is older than CCS(%lu): ignored",
  1318. rtc_tmp,
  1319. RTC_DEFAULT_TIME);
  1320. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1321. return;
  1322. }
  1323. //STEP 2: Checking System State and Update RTC
  1324. if (Check_V2G_Flow_Status() == IDLE &&
  1325. ShmInternalComm->DownloadImageInfo.active == FALSE &&
  1326. EVCOMM_SYS_INFO.End_Process_inused == FALSE)
  1327. {
  1328. #if 1
  1329. //4-Byte EPOCH TIME: CAN: BYTE_TIME[0] BYTE_TIME[1] BYTE_TIME[2] BYTE_TIME[3]
  1330. memcpy(&ShmInternalComm->RTC, &frame->data[0], 4); //Epoch time
  1331. #else
  1332. //4-Byte EPOCH TIME: CAN: BYTE_TIME[3] BYTE_TIME[2] BYTE_TIME[1] BYTE_TIME[0]
  1333. unsigned char rtc[4], tmp;
  1334. memcpy(&rtc[0], &frame->data[0], 4);
  1335. tmp = rtc[0];
  1336. rtc[0] = rtc[3];
  1337. rtc[3] = tmp;
  1338. tmp = rtc[1];
  1339. rtc[1] = rtc[2];
  1340. rtc[2] = tmp;
  1341. memcpy(&ShmInternalComm->RTC, &rtc[0], 4); //Epoch time
  1342. #endif
  1343. }
  1344. else
  1345. {
  1346. ShmInternalComm->RTC = 0;
  1347. }
  1348. //printf("\n%X %X %X %X (%X)\n", frame->data[0], frame->data[1], frame->data[2], frame->data[3], ShmInternalComm->RTC);
  1349. CSUCOMMDC_TASK_FLAG.Got_RTC = TRUE;
  1350. }
  1351. /*===========================================================================
  1352. FUNCTION: Proc_EVSEPrechargeInfoUpdateRes
  1353. DESCRIPTION:
  1354. PRE-CONDITION:
  1355. INPUT:
  1356. OUTPUT:
  1357. GLOBAL VARIABLES:
  1358. =============================================================================*/
  1359. int Proc_EVSEPrechargeInfoUpdateRes(int Fd)
  1360. {
  1361. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][Proc_EVSEPrechargeInfoUpdateRes]\n");
  1362. int nbytes;
  1363. unsigned char Buffer[8];
  1364. unsigned short TmpValue;
  1365. memset(Buffer, 0, sizeof(Buffer));
  1366. //Buffer[0] = TRUE;
  1367. Buffer[0] = ShmInternalComm->EVSEPrechargeStatus;
  1368. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_EVSE_PRECHARGE_INFO, ShmInternalComm->SlaveAddress, 1, Buffer);
  1369. CSUCOMMDC_TASK_FLAG.Got_EVSE_Precharge_Info = FALSE;
  1370. return nbytes;
  1371. }
  1372. /*===========================================================================
  1373. FUNCTION: Proc_EVSEPrechargeInfoUpdateReq
  1374. DESCRIPTION:
  1375. PRE-CONDITION:
  1376. INPUT:
  1377. OUTPUT:
  1378. GLOBAL VARIABLES:
  1379. =============================================================================*/
  1380. void Proc_EVSEPrechargeInfoUpdateReq(struct can_frame *frame)
  1381. {
  1382. static struct ChargingInfoData *sys;
  1383. sys = &ShmSysConfigAndInfo->SysInfo.CcsChargingData[0];
  1384. ShmInternalComm->EVSEPrechargeStatus = frame->data[0];
  1385. sys->EVSEPrechargeStatus = ShmInternalComm->EVSEPrechargeStatus;
  1386. if (sys->EVSEPrechargeStatus_pre != sys->EVSEPrechargeStatus)
  1387. {
  1388. //memset(buf_log_csucomm, 0, sizeof(buf_log_csucomm));
  1389. sprintf(buf_log_csucomm, "Precharge Status: %d >> %d",
  1390. sys->EVSEPrechargeStatus_pre,
  1391. sys->EVSEPrechargeStatus
  1392. );
  1393. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1394. sys->EVSEPrechargeStatus_pre = sys->EVSEPrechargeStatus;
  1395. }
  1396. CSUCOMMDC_TASK_FLAG.Got_EVSE_Precharge_Info = TRUE;
  1397. }
  1398. /*===========================================================================
  1399. FUNCTION: Proc_EVCCIDRes
  1400. DESCRIPTION:
  1401. PRE-CONDITION:
  1402. INPUT:
  1403. OUTPUT:
  1404. GLOBAL VARIABLES:
  1405. =============================================================================*/
  1406. int Proc_EVCCIDRes(int Fd)
  1407. {
  1408. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][Proc_EVCCIDRes]\n");
  1409. int i = 0;
  1410. int nbytes = 0;
  1411. unsigned char Buffer[8];
  1412. /*+++ 20200707, vern, reduce 8 digital MAC from BMW i3 +++*/
  1413. memset(Buffer, 0, sizeof(Buffer));
  1414. if(EVCOMM_SYS_INFO.EVCCID_length>=8)
  1415. {
  1416. EVCOMM_SYS_INFO.EVCCID_length=8;
  1417. if((EVCOMM_SYS_INFO.EVCCID[0]==0)&&(EVCOMM_SYS_INFO.EVCCID[1]==0))
  1418. memcpy(EVCOMM_SYS_INFO.EVCCID,EVCOMM_SYS_INFO.EVCCID+2,6);
  1419. EVCOMM_SYS_INFO.EVCCID_length=6;
  1420. }
  1421. memcpy(Buffer,EVCOMM_SYS_INFO.EVCCID,EVCOMM_SYS_INFO.EVCCID_length);
  1422. /*--- 20200707, vern, reduce 8 digital MAC from BMW i3 ---*/
  1423. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_EVCCID_REQUEST, ShmInternalComm->SlaveAddress, EVCOMM_SYS_INFO.EVCCID_length, Buffer);
  1424. CSUCOMMDC_TASK_FLAG.Got_EVCCID_Req = FALSE;
  1425. return nbytes;
  1426. }
  1427. /*===========================================================================
  1428. FUNCTION: Proc_EVCCIDReq
  1429. DESCRIPTION:
  1430. PRE-CONDITION:
  1431. INPUT:
  1432. OUTPUT:
  1433. GLOBAL VARIABLES:
  1434. =============================================================================*/
  1435. void Proc_EVCCIDReq(struct can_frame *frame)
  1436. {
  1437. CSUCOMMDC_TASK_FLAG.Got_EVCCID_Req = TRUE;
  1438. }
  1439. /*===========================================================================
  1440. FUNCTION: Proc_EVStopRes
  1441. DESCRIPTION:
  1442. PRE-CONDITION:
  1443. INPUT:
  1444. OUTPUT:
  1445. GLOBAL VARIABLES:
  1446. =============================================================================*/
  1447. int Proc_EVStopRes(int Fd)
  1448. {
  1449. int nbytes;
  1450. unsigned char Buffer[8];
  1451. memset(Buffer, 0, sizeof(Buffer));
  1452. if (CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency == TRUE)
  1453. {
  1454. Buffer[0] = EV_EMERGENCY_STOP; //2
  1455. }
  1456. else
  1457. {
  1458. Buffer[0] = EV_NORMAL_STOP; //1
  1459. }
  1460. /*
  1461. if(strlen(ShmStatusCodeData->PresentStatusCode[0]) > 0)
  1462. {
  1463. memcpy(Buffer + 2, ShmStatusCodeData->PresentStatusCode[0], 6);
  1464. }
  1465. */
  1466. Buffer[1] = ShmStatusCodeData->PresentStatusCode[0][0];
  1467. Buffer[2] = ShmStatusCodeData->PresentStatusCode[0][1];
  1468. Buffer[3] = ShmStatusCodeData->PresentStatusCode[0][2];
  1469. Buffer[4] = ShmStatusCodeData->PresentStatusCode[0][3];
  1470. Buffer[5] = ShmStatusCodeData->PresentStatusCode[0][4];
  1471. Buffer[6] = ShmStatusCodeData->PresentStatusCode[0][5];
  1472. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_EV_STOP_EVENT, ShmInternalComm->SlaveAddress, 7, Buffer);
  1473. //system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"); //for test only
  1474. //system("echo 0 > /sys/class/gpio/gpio89/value"); //for test
  1475. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_EVStopRes] Sending STOP Command to CSU");
  1476. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = FALSE;
  1477. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = FALSE;
  1478. return nbytes;
  1479. }
  1480. /*===========================================================================
  1481. FUNCTION: Proc_EVSEStopRes
  1482. DESCRIPTION:
  1483. PRE-CONDITION:
  1484. INPUT:
  1485. OUTPUT:
  1486. GLOBAL VARIABLES:
  1487. =============================================================================*/
  1488. int Proc_EVSEStopRes(int Fd)
  1489. {
  1490. if (EVCOMM_SYS_INFO.DC_EVSEStatus != EVSE_Shutdown &&
  1491. EVCOMM_SYS_INFO.DC_EVSEStatus != EVSE_EmergencyShutdown)
  1492. {
  1493. //The above considtion will let this debug message to print once
  1494. //even though the CSU sends several continuous stop commands.
  1495. sprintf(buf_log_csucomm,
  1496. "[Proc_EVSEStopRes] CSU Requests for STOP (Stop by EVSE: %d)",
  1497. ShmInternalComm->EVSEStopChargingReq.type);
  1498. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1499. //Once the stop command is received by CCS, this CAN message cannot
  1500. //resume it back to normal state unless the End_Process is completed.
  1501. if (ShmInternalComm->EVSEStopChargingReq.type == 1)
  1502. {
  1503. EVCOMM_SYS_INFO.DC_EVSEStatus = EVSE_Shutdown;
  1504. }
  1505. else if (ShmInternalComm->EVSEStopChargingReq.type == 2)
  1506. {
  1507. EVCOMM_SYS_INFO.DC_EVSEStatus = EVSE_EmergencyShutdown;
  1508. }
  1509. else
  1510. {
  1511. EVCOMM_SYS_INFO.DC_EVSEStatus = EVSE_Ready;
  1512. }
  1513. }
  1514. CSUCOMMDC_TASK_FLAG.Got_EVSEStopReq = FALSE;
  1515. }
  1516. /*===========================================================================
  1517. FUNCTION: Proc_EVSEStopReq
  1518. DESCRIPTION:
  1519. PRE-CONDITION:
  1520. INPUT:
  1521. OUTPUT:
  1522. GLOBAL VARIABLES:
  1523. =============================================================================*/
  1524. void Proc_EVSEStopReq(struct can_frame *frame)
  1525. {
  1526. ShmInternalComm->EVSEStopChargingReq.type = frame->data[0];
  1527. memcpy(&ShmInternalComm->EVSEStopChargingReq.alarmcode, &frame->data[1], 6);
  1528. CSUCOMMDC_TASK_FLAG.Got_EVSEStopReq = TRUE;
  1529. }
  1530. /*===========================================================================
  1531. FUNCTION: Check_DwnldImgParameter
  1532. DESCRIPTION:
  1533. PRE-CONDITION:
  1534. INPUT:
  1535. OUTPUT:
  1536. 1. Parameter Validation (0: invalid, 1: valid)
  1537. 2. final_canmsg_size
  1538. 3. total_can_packets
  1539. GLOBAL VARIABLES:
  1540. =============================================================================*/
  1541. int Check_DwnldImgParameter()
  1542. {
  1543. int image_size = 0; //unit: byte
  1544. int total_can_packets = 0;
  1545. unsigned char block_quantity = 0;
  1546. unsigned char block_size_KByte = 0; //unit: Kbytes
  1547. unsigned char final_canmsg_size = 0; //1~7
  1548. image_size = ShmInternalComm->DownloadImageInfo.image_size;
  1549. block_quantity = ShmInternalComm->DownloadImageInfo.block_quantity;
  1550. block_size_KByte = ShmInternalComm->DownloadImageInfo.block_size_KByte;
  1551. //Step 1: Caculate final_canmsg_size and total_can_packets
  1552. final_canmsg_size = image_size % 8; //8: max of CAN bus payload
  1553. total_can_packets = image_size / 8;
  1554. if (final_canmsg_size != 0)
  1555. {
  1556. total_can_packets += 1;
  1557. }
  1558. ShmInternalComm->DownloadImageInfo.final_canmsg_size = final_canmsg_size;
  1559. ShmInternalComm->DownloadImageInfo.total_can_packets = total_can_packets;
  1560. DEBUG_PRINTF_CSUCOMM_DETAIL("[Check_DwnldImgParameter]\n");
  1561. DEBUG_PRINTF_CSUCOMM_DETAIL("\t - final_canmsg_size = %d (DEC)\n\
  1562. \t - total_can_packets = %d (DEC)\n\n",
  1563. ShmInternalComm->DownloadImageInfo.final_canmsg_size,
  1564. ShmInternalComm->DownloadImageInfo.total_can_packets
  1565. );
  1566. //Step 2: Check Parameter Validation
  1567. //if ((block_quantity * block_size_KByte * 1024) > image_size)
  1568. //[CAUTION] To increase type ID, please modify this condition.
  1569. if (ShmInternalComm->DownloadImageInfo.type <= IMAGE_TYPE_USER_CONFIGURATION)
  1570. {
  1571. return TRUE;
  1572. }
  1573. else
  1574. {
  1575. sprintf(buf_log_csucomm,
  1576. "[Check_DwnldImgParameter] type(%d) is not allowed, yet.",
  1577. ShmInternalComm->DownloadImageInfo.type
  1578. );
  1579. SAVE_SYS_LOG_MSG_CSUCOMM("buf_log_csucomm");
  1580. return FALSE;
  1581. }
  1582. }
  1583. /*===========================================================================
  1584. FUNCTION: Proc_DownloadImageRes
  1585. DESCRIPTION:
  1586. PRE-CONDITION:
  1587. INPUT:
  1588. OUTPUT:
  1589. GLOBAL VARIABLES:
  1590. =============================================================================*/
  1591. int Proc_DownloadImageRes(int Fd)
  1592. {
  1593. int nbytes;
  1594. unsigned char Buffer[1];
  1595. memset(Buffer, 0, sizeof(Buffer));
  1596. if (ShmInternalComm->DownloadImageInfo.active == TRUE)
  1597. {
  1598. //[VITAL] Closing unneccesary task to supoort rapid CAN data receving..
  1599. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_DownloadImageRes] closing SeccComm...");
  1600. system("killall SeccComm");
  1601. system("rm -f /Storage/tmp");
  1602. system("touch /Storage/tmp");
  1603. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
  1604. {
  1605. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_DownloadImageRes]sync...");
  1606. system("sync");
  1607. }
  1608. #endif
  1609. Buffer[0] = 1; //Accept
  1610. }
  1611. else
  1612. {
  1613. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_DownloadImageRes] Fail");
  1614. Buffer[0] = 0; //Reject
  1615. //[To-be-implemented][TBD] Sending alarm code or not?
  1616. }
  1617. DEBUG_PRINTF_CSUCOMM_DETAIL("[Proc_DownloadImageRes] %d (DEC)\n", Buffer[0]);
  1618. CSUCOMMDC_TASK_FLAG.Got_DownloadImageReq = FALSE;
  1619. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_DOWNLOAD_REQUEST, ShmInternalComm->SlaveAddress, 1, Buffer);
  1620. return nbytes;
  1621. }
  1622. /*===========================================================================
  1623. FUNCTION: Proc_DownloadImageReq
  1624. DESCRIPTION:
  1625. PRE-CONDITION:
  1626. INPUT:
  1627. OUTPUT:
  1628. GLOBAL VARIABLES:
  1629. =============================================================================*/
  1630. void Proc_DownloadImageReq(struct can_frame *frame)
  1631. {
  1632. if (Check_V2G_Flow_Status() == IDLE)
  1633. {
  1634. ShmInternalComm->DownloadImageInfo.type = frame->data[0];
  1635. memcpy(&ShmInternalComm->DownloadImageInfo.image_size, &frame->data[1], 4);
  1636. ShmInternalComm->DownloadImageInfo.image_size_received = 0;
  1637. ShmInternalComm->DownloadImageInfo.block_quantity = frame->data[5];
  1638. ShmInternalComm->DownloadImageInfo.block_sequence_number = 0;
  1639. ShmInternalComm->DownloadImageInfo.block_size_KByte = frame->data[6];
  1640. ShmInternalComm->DownloadImageInfo.block_sequence_number = 0;
  1641. ShmInternalComm->DownloadImageInfo.block_checksum = 0;
  1642. ShmInternalComm->DownloadImageInfo.total_can_packets = 0;
  1643. ShmInternalComm->DownloadImageInfo.image_rx_cnt = 0;
  1644. ShmInternalComm->DownloadImageInfo.downlaod_percentage = 0;
  1645. ShmInternalComm->DownloadImageInfo.downlaod_percentage_pre = 0;
  1646. ShmInternalComm->DownloadImageInfo.active = FALSE;
  1647. if (ShmInternalComm->DownloadImageInfo.image_size == 0)
  1648. {
  1649. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_DownloadImageReq][Warning] image_size cannot be 0\n");
  1650. }
  1651. else if (ShmInternalComm->DownloadImageInfo.block_quantity == 0)
  1652. {
  1653. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_DownloadImageReq][Warning] block_quantity cannot be 0\n");
  1654. }
  1655. else if (ShmInternalComm->DownloadImageInfo.block_size_KByte == 0)
  1656. {
  1657. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_DownloadImageReq][Warning] block_size_KByte cannot be 0\n");
  1658. }
  1659. else if (Check_DwnldImgParameter() == FALSE)
  1660. {
  1661. sprintf(buf_log_csucomm, "[Proc_DownloadImageReq][Warning] nonlogical image_size(%d bytes, DEC), block_quantity(%d, DEC), block_size_KByte(%d, DEC)\n",
  1662. ShmInternalComm->DownloadImageInfo.image_size,
  1663. ShmInternalComm->DownloadImageInfo.block_quantity,
  1664. ShmInternalComm->DownloadImageInfo.block_size_KByte
  1665. );
  1666. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1667. }
  1668. else
  1669. {
  1670. ShmInternalComm->DownloadImageInfo.active = TRUE;
  1671. }
  1672. }
  1673. else
  1674. {
  1675. ShmInternalComm->DownloadImageInfo.active = FALSE;
  1676. sprintf(buf_log_csucomm,
  1677. "[Proc_BlockTransferStartReq] PresentMsgFlowStatus (%d, DEC) is not in IDLE\n",
  1678. Check_V2G_Flow_Status()
  1679. );
  1680. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1681. }
  1682. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_DownloadImageReq] Got Request from CSU\n");
  1683. sprintf(buf_log_csucomm, "\t - type = %d (DEC)\n\
  1684. \t - image size = %d (DEC, bytes)\n\
  1685. \t - block quantity = %d (DEC)\n\
  1686. \t - block size = %d (DEC, KBytes)\n\n",
  1687. ShmInternalComm->DownloadImageInfo.type,
  1688. ShmInternalComm->DownloadImageInfo.image_size,
  1689. ShmInternalComm->DownloadImageInfo.block_quantity,
  1690. ShmInternalComm->DownloadImageInfo.block_size_KByte
  1691. );
  1692. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1693. CSUCOMMDC_TASK_FLAG.Got_DownloadImageReq = TRUE;
  1694. }
  1695. /*===========================================================================
  1696. FUNCTION: Proc_BlockTransferStartRes
  1697. DESCRIPTION:
  1698. PRE-CONDITION:
  1699. INPUT:
  1700. OUTPUT:
  1701. GLOBAL VARIABLES:
  1702. =============================================================================*/
  1703. int Proc_BlockTransferStartRes(int Fd)
  1704. {
  1705. int nbytes;
  1706. unsigned char Buffer[1];
  1707. memset(Buffer, 0, sizeof(Buffer));
  1708. if (ShmInternalComm->DownloadImageInfo.active == TRUE)
  1709. {
  1710. Buffer[0] = 1; //Accept
  1711. }
  1712. else
  1713. {
  1714. fclose(ShmInternalComm->DownloadImageInfo.file);
  1715. Buffer[0] = 0; //Reject
  1716. //[To-be-implemented][TBD] Sending alarm code or not?
  1717. }
  1718. DEBUG_PRINTF_CSUCOMM_DETAIL("[Proc_BlockTransferStartRes] %d (DEC)\n", Buffer[0]);
  1719. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_START_BLOCK_TRANSFER, ShmInternalComm->SlaveAddress, 1, Buffer);
  1720. CSUCOMMDC_TASK_FLAG.Got_BlockTransferStartReq = FALSE;
  1721. return nbytes;
  1722. }
  1723. /*===========================================================================
  1724. FUNCTION: Proc_BlockTransferStartReq
  1725. DESCRIPTION:
  1726. PRE-CONDITION:
  1727. INPUT:
  1728. OUTPUT:
  1729. GLOBAL VARIABLES:
  1730. =============================================================================*/
  1731. void Proc_BlockTransferStartReq(struct can_frame *frame)
  1732. {
  1733. if (Check_V2G_Flow_Status() == IDLE)
  1734. {
  1735. ShmInternalComm->DownloadImageInfo.block_sequence_number += 1; //1~255
  1736. //Check the valid range (1 ~ 255)
  1737. if (frame->data[0] >= 1 &&
  1738. frame->data[0] <= 255 &&
  1739. ShmInternalComm->DownloadImageInfo.block_sequence_number == frame->data[0] &&
  1740. ShmInternalComm->DownloadImageInfo.block_sequence_number >= 1 &&
  1741. ShmInternalComm->DownloadImageInfo.block_sequence_number <= 255
  1742. )
  1743. {
  1744. ShmInternalComm->DownloadImageInfo.active = TRUE;
  1745. }
  1746. else
  1747. {
  1748. sprintf(buf_log_csucomm, "[Proc_BlockTransferStartReq][Warning] Unexpected block_sequence_number (%d, %d, DEC)\n",
  1749. frame->data[0],
  1750. ShmInternalComm->DownloadImageInfo.block_sequence_number
  1751. );
  1752. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1753. ShmInternalComm->DownloadImageInfo.active = FALSE;
  1754. }
  1755. ShmInternalComm->DownloadImageInfo.block_checksum = frame->data[1];
  1756. }
  1757. else
  1758. {
  1759. ShmInternalComm->DownloadImageInfo.active = FALSE;
  1760. sprintf(buf_log_csucomm,
  1761. "[Proc_BlockTransferStartReq] PresentMsgFlowStatus (%d, DEC) is not in IDLE\n",
  1762. Check_V2G_Flow_Status()
  1763. );
  1764. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1765. }
  1766. DEBUG_PRINTF_CSUCOMM_DETAIL("\n[Proc_BlockTransferStartReq] Got Request from CSU\n");
  1767. sprintf(buf_log_csucomm, "\t - block_sequence_number = %d (DEC)\n\
  1768. \t - block_checksum = %02X (HEX, bytes)\n\n",
  1769. ShmInternalComm->DownloadImageInfo.block_sequence_number,
  1770. ShmInternalComm->DownloadImageInfo.block_checksum
  1771. );
  1772. DEBUG_PRINTF_CSUCOMM_DETAIL(buf_log_csucomm);
  1773. CSUCOMMDC_TASK_FLAG.Got_BlockTransferStartReq = TRUE;
  1774. }
  1775. /*===========================================================================
  1776. FUNCTION: Proc_DataTransferRes
  1777. DESCRIPTION:
  1778. PRE-CONDITION:
  1779. INPUT:
  1780. OUTPUT:
  1781. GLOBAL VARIABLES:
  1782. =============================================================================*/
  1783. int Proc_DataTransferRes(int Fd)
  1784. {
  1785. //DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][Proc_DataTransferRes]\n");
  1786. //[To-be-implemented] Check Block Checksum, if fail, we should use this response to ask CSU to resend this block again.
  1787. CSUCOMMDC_TASK_FLAG.Got_DataTransferReq = FALSE;
  1788. }
  1789. /*===========================================================================
  1790. FUNCTION: Proc_DataTransferReq
  1791. DESCRIPTION:
  1792. PRE-CONDITION:
  1793. 1. The time interval between each CAN data should be 3ms at least to
  1794. avoid data loss.
  1795. INPUT:
  1796. OUTPUT:
  1797. GLOBAL VARIABLES:
  1798. =============================================================================*/
  1799. void Proc_DataTransferReq(struct can_frame *frame)
  1800. {
  1801. if (Check_V2G_Flow_Status() == IDLE &&
  1802. ShmInternalComm->DownloadImageInfo.active == 1)
  1803. {
  1804. if ((ShmInternalComm->DownloadImageInfo.image_rx_cnt + frame->can_dlc) <= ShmInternalComm->DownloadImageInfo.image_size)
  1805. {
  1806. //Saving Data
  1807. FILE *file;
  1808. file = fopen("/Storage/tmp", "ab+");
  1809. int rlen = 0;
  1810. DEBUG_PRINTF_CSUCOMM_DETAIL("[Proc_DataTransferReq] saving data... (%d bytes, DEC)\n", frame->can_dlc);
  1811. //rlen = fwrite(&frame->data[0], sizeof(unsigned char), frame->can_dlc, ShmInternalComm->DownloadImageInfo.file);
  1812. rlen = fwrite(&frame->data[0], sizeof(unsigned char), frame->can_dlc, file);
  1813. if (rlen == frame->can_dlc)
  1814. {
  1815. fflush(ShmInternalComm->DownloadImageInfo.file);
  1816. DEBUG_PRINTF_CSUCOMM_DETAIL("[Proc_DataTransferReq] fwrite: OK (rlen = %d)\n", rlen);
  1817. ShmInternalComm->DownloadImageInfo.image_rx_cnt += frame->can_dlc;
  1818. }
  1819. else
  1820. {
  1821. DEBUG_PRINTF_CSUCOMM_DETAIL("[Proc_DataTransferReq] fwrite: FAIL (rlen = %d)\n", rlen);
  1822. ShmInternalComm->DownloadImageInfo.active = FALSE;
  1823. }
  1824. fflush(file);
  1825. fclose(file);
  1826. DEBUG_PRINTF_CSUCOMM_DETAIL("\n", ShmInternalComm->DownloadImageInfo.image_rx_cnt);
  1827. //Caculating the Download Percentage
  1828. ShmInternalComm->DownloadImageInfo.downlaod_percentage = (ShmInternalComm->DownloadImageInfo.image_rx_cnt*100)/ShmInternalComm->DownloadImageInfo.image_size;
  1829. if (ShmInternalComm->DownloadImageInfo.downlaod_percentage != ShmInternalComm->DownloadImageInfo.downlaod_percentage_pre)
  1830. {
  1831. sprintf(buf_log_csucomm,
  1832. "[Proc_DataTransferReq] rx: %d\% (%d of %d bytes)\n", //showing the downlaod percentage
  1833. ShmInternalComm->DownloadImageInfo.downlaod_percentage,
  1834. ShmInternalComm->DownloadImageInfo.image_rx_cnt,
  1835. ShmInternalComm->DownloadImageInfo.image_size
  1836. );
  1837. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1838. ShmInternalComm->DownloadImageInfo.downlaod_percentage_pre = ShmInternalComm->DownloadImageInfo.downlaod_percentage;
  1839. }
  1840. }
  1841. else
  1842. {
  1843. ShmInternalComm->DownloadImageInfo.active = FALSE;
  1844. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_BlockTransferStartReq] unexpected extra packets\n");
  1845. }
  1846. }
  1847. else
  1848. {
  1849. ShmInternalComm->DownloadImageInfo.active = FALSE;
  1850. sprintf(buf_log_csucomm, "[Proc_BlockTransferStartReq] PresentMsgFlowStatus (%d, DEC) \
  1851. is not in IDLE or active flag (%d, DEC) is canceled\n",
  1852. Check_V2G_Flow_Status(),
  1853. ShmInternalComm->DownloadImageInfo.active
  1854. );
  1855. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1856. //Sending Stop Request to CSU to abort Data Transfer
  1857. //CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  1858. //[To-be-implemented] Adding corresponding alarm code
  1859. }
  1860. CSUCOMMDC_TASK_FLAG.Got_DataTransferReq = TRUE;
  1861. }
  1862. /*===========================================================================
  1863. FUNCTION: Proc_DownloadFinishRes
  1864. DESCRIPTION:
  1865. PRE-CONDITION:
  1866. INPUT:
  1867. OUTPUT:
  1868. GLOBAL VARIABLES:
  1869. =============================================================================*/
  1870. int Proc_DownloadFinishRes(int Fd)
  1871. {
  1872. int nbytes;
  1873. unsigned char Buffer[1];
  1874. memset(Buffer, 0, sizeof(Buffer));
  1875. //Step 1: Protection for receiving Firmware Update Req in non-IDLE mode
  1876. if (Check_V2G_Flow_Status() != IDLE)
  1877. {
  1878. if (CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused == FALSE)
  1879. {
  1880. SAVE_SYS_LOG_MSG_CSUCOMM("[Warning]FW update Req: ignored (only for idle mode)");
  1881. Buffer[0] = FAIL;
  1882. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_DOWNLOAD_FINISH, ShmInternalComm->SlaveAddress, 1, Buffer);
  1883. if (nbytes > 0)
  1884. {
  1885. sprintf(buf_log_csucomm, "[Proc_DownloadFinishRes]Tx:ok(%d)", Buffer[0]);
  1886. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1887. }
  1888. else
  1889. {
  1890. sprintf(buf_log_csucomm, "[Proc_DownloadFinishRes]Tx:fail(%d)", nbytes);
  1891. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1892. }
  1893. return FAIL;
  1894. }
  1895. else if (CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused == TRUE)
  1896. {
  1897. SAVE_SYS_LOG_MSG_CSUCOMM("[Warning]FW update Req: ignored (in used, already)");
  1898. //not sending response message, since it stands for end of update process.
  1899. return FAIL;
  1900. }
  1901. else
  1902. {
  1903. sprintf(buf_log_csucomm,
  1904. "[Warning]unexpected FW_Update_Task_inused value(%d)",
  1905. CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused
  1906. );
  1907. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1908. return FAIL;
  1909. }
  1910. }
  1911. //Step 2: Execute the FW update task
  1912. if (CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused == FALSE)
  1913. {
  1914. CSUCOMMDC_TASK_FLAG.FW_Update_Task_inused = TRUE;
  1915. //[VITAL] Closing unneccesary task to supoort rapid CAN data receving..
  1916. SAVE_SYS_LOG_MSG_CSUCOMM("[Proc_DownloadFinishRes] closing SeccComm...");
  1917. system("killall SeccComm");
  1918. system("cd /root;./FWUpdate -w &");
  1919. }
  1920. //Step 3:Sending Response message with result once FW update is done.
  1921. if (CSUCOMMDC_TASK_FLAG.FW_Update_Done == TRUE)
  1922. {
  1923. if (CSUCOMMDC_TASK_FLAG.FW_Update_result == PASS)
  1924. {
  1925. Buffer[0] = PASS;
  1926. }
  1927. else
  1928. {
  1929. Buffer[0] = FAIL;
  1930. }
  1931. nbytes = CAN_Tx_MSG(Fd, CAN_CMD_DOWNLOAD_FINISH, ShmInternalComm->SlaveAddress, 1, Buffer);
  1932. if (nbytes > 0)
  1933. {
  1934. sprintf(buf_log_csucomm, "[Proc_DownloadFinishRes]Tx:ok(%d)", Buffer[0]);
  1935. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1936. }
  1937. else
  1938. {
  1939. sprintf(buf_log_csucomm, "[Proc_DownloadFinishRes]Tx:fail(%d)", nbytes);
  1940. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  1941. }
  1942. sleep(2);
  1943. //system("killall FWUpdate");
  1944. CSUCOMMDC_TASK_FLAG.Got_DownloadFinishReq = FALSE;
  1945. return nbytes;
  1946. }
  1947. }
  1948. /*===========================================================================
  1949. FUNCTION: Proc_DownloadFinishReq
  1950. DESCRIPTION:
  1951. PRE-CONDITION:
  1952. INPUT:
  1953. OUTPUT:
  1954. GLOBAL VARIABLES:
  1955. =============================================================================*/
  1956. void Proc_DownloadFinishReq(struct can_frame *frame)
  1957. {
  1958. CSUCOMMDC_TASK_FLAG.Got_DownloadFinishReq = TRUE;
  1959. }
  1960. /*===========================================================================
  1961. FUNCTION: PRINT_CAN_FRAME
  1962. DESCRIPTION:
  1963. PRE-CONDITION:
  1964. INPUT:
  1965. OUTPUT:
  1966. GLOBAL VARIABLES:
  1967. =============================================================================*/
  1968. void PRINT_CAN_FRAME(struct can_frame *frame)
  1969. {
  1970. if (frame->can_dlc != 0)
  1971. {
  1972. int i = 0;
  1973. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm] Got CAN Message: \n\
  1974. \t - Length: %d (Bytes, DEC)\n\
  1975. \t - ID: %08X\n\
  1976. \t - Payload: ",
  1977. frame->can_dlc,
  1978. frame->can_id
  1979. );
  1980. for (i = 0; i< frame->can_dlc; i++)
  1981. {
  1982. DEBUG_PRINTF_CSUCOMM_DETAIL("%02X ", frame->data[i]);
  1983. }
  1984. DEBUG_PRINTF_CSUCOMM_DETAIL("\n");
  1985. }
  1986. }
  1987. /*===========================================================================
  1988. FUNCTION: CANRxTimeoutDetector
  1989. DESCRIPTION:
  1990. 1. fork1
  1991. PRE-CONDITION:
  1992. INPUT:
  1993. OUTPUT:
  1994. GLOBAL VARIABLES:
  1995. =============================================================================*/
  1996. int CANRxTimeoutHandler()
  1997. {
  1998. #if (CAN_RX_TIMEOUT_MECHANISM == ENABLE)
  1999. //#if 1
  2000. if (CSUCOMMDC_TASK_FLAG.matched == TRUE &&
  2001. CSUCOMMDC_TASK_FLAG.CAN_Rx_Timeout == FALSE)
  2002. {
  2003. long int time_diff = 0;
  2004. ftime(&ShmInternalComm->CAN_Rx_Timer_End);
  2005. time_diff = DiffTimeb_CsuComm_fork1(ShmInternalComm->CAN_Rx_Timer_Start, ShmInternalComm->CAN_Rx_Timer_End);
  2006. if(time_diff >= CSUCOMM_CAN_RX_TIMEOUT) //5 sec
  2007. {
  2008. sprintf(buf_log_csucomm_fork1,
  2009. "[fork1][Rx]CAN timeout(%ld of %dms) => reboot",
  2010. time_diff,
  2011. CSUCOMM_CAN_RX_TIMEOUT
  2012. );
  2013. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm_fork1);
  2014. //Reboot the CCS Board
  2015. //Update_ShmStatusCode();
  2016. //CCS module communication fail (012248)
  2017. ShmStatusCodeData->PresentStatusCode[0][0] = 0;
  2018. ShmStatusCodeData->PresentStatusCode[0][1] = 1;
  2019. ShmStatusCodeData->PresentStatusCode[0][2] = 2;
  2020. ShmStatusCodeData->PresentStatusCode[0][3] = 2;
  2021. ShmStatusCodeData->PresentStatusCode[0][4] = 4;
  2022. ShmStatusCodeData->PresentStatusCode[0][5] = 8;
  2023. CSUCOMMDC_TASK_FLAG.EV_Stop_Type_Emergency = TRUE;
  2024. CSUCOMMDC_TASK_FLAG.Send_EVStopReq = TRUE;
  2025. CSUCOMMDC_TASK_FLAG.CAN_Rx_Timeout = TRUE;
  2026. Reboot_Process();
  2027. }
  2028. }
  2029. #endif
  2030. }
  2031. /*===========================================================================
  2032. FUNCTION: FWUpdateRebootDetector
  2033. DESCRIPTION:
  2034. 1. fork2
  2035. PRE-CONDITION:
  2036. INPUT:
  2037. OUTPUT:
  2038. GLOBAL VARIABLES:
  2039. =============================================================================*/
  2040. int FWUpdateRebootHandler()
  2041. {
  2042. if (CSUCOMMDC_TASK_FLAG.FW_Update_Done == TRUE &&
  2043. CSUCOMMDC_TASK_FLAG.Got_DownloadFinishReq == FALSE &&
  2044. CSUCOMMDC_TASK_FLAG.FW_Update_Reboot_inused == FALSE) //wait for CAN Res msg to be sent.
  2045. {
  2046. CSUCOMMDC_TASK_FLAG.FW_Update_Reboot_inused = TRUE;
  2047. sprintf(buf_log_csucomm_fork1,
  2048. "[fork1]FW Update Done(%d) => End_Process",
  2049. CSUCOMMDC_TASK_FLAG.Got_DownloadFinishReq);
  2050. SAVE_SYS_LOG_MSG_EVCOMM(buf_log_csucomm_fork1);
  2051. Reboot_Process();
  2052. }
  2053. }
  2054. /*===========================================================================
  2055. FUNCTION: Error_Monitor_CsuComm
  2056. DESCRIPTION:
  2057. 1. fork1
  2058. PRE-CONDITION:
  2059. INPUT:
  2060. OUTPUT:
  2061. GLOBAL VARIABLES:
  2062. =============================================================================*/
  2063. void Error_Monitor_CsuComm()
  2064. {
  2065. pid_t tmp = 0;
  2066. if(PID_CsuComm_Error_Monitor_Task == 0)
  2067. {
  2068. tmp = fork(); //fork1
  2069. if(tmp > 0)
  2070. {
  2071. PID_CsuComm_Error_Monitor_Task = tmp;
  2072. sprintf(buf_log_csucomm_fork1,
  2073. "[fork2][Error_Monitor]created(%d)",
  2074. PID_CsuComm_Error_Monitor_Task);
  2075. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm_fork1);
  2076. #if 0
  2077. unsigned char buf[64];
  2078. memset(buf, 0, sizeof(buf));
  2079. sprintf(buf, "renice -20 -p %d", tmp);
  2080. system(buf);
  2081. #endif
  2082. return;
  2083. }
  2084. }
  2085. while(1)
  2086. {
  2087. //CAN comm. Timeout Detector
  2088. CANRxTimeoutHandler();
  2089. //Check for FW Update Reboot Request
  2090. FWUpdateRebootHandler();
  2091. }
  2092. }
  2093. /*===========================================================================
  2094. FUNCTION: CAN_Rx
  2095. DESCRIPTION:
  2096. 1. fork2
  2097. 2. Receiving Task (indivitual process)
  2098. PRE-CONDITION:
  2099. INPUT:
  2100. OUTPUT:
  2101. GLOBAL VARIABLES:
  2102. =============================================================================*/
  2103. void CAN_Rx(int fd)
  2104. {
  2105. pid_t tmp = 0;
  2106. struct can_frame frame;
  2107. int nbytes;
  2108. if(PID_CAN_Rx_Task == 0)
  2109. {
  2110. tmp = fork(); //fork2
  2111. if(tmp > 0)
  2112. {
  2113. PID_CAN_Rx_Task = tmp;
  2114. sprintf(buf_log_csucomm_fork2,
  2115. "[fork2][Error_Monitor]created(%d)",
  2116. PID_CAN_Rx_Task);
  2117. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm_fork2);
  2118. #if 0
  2119. unsigned char buf[64];
  2120. memset(buf, 0, sizeof(buf));
  2121. sprintf(buf, "renice -20 -p %d", tmp);
  2122. system(buf);
  2123. #endif
  2124. return;
  2125. }
  2126. }
  2127. ftime(&ShmInternalComm->CAN_Rx_Timer_Start); //ID is matched
  2128. while(1) //Process after Matched
  2129. {
  2130. memset(&frame, 0, sizeof(struct can_frame));
  2131. nbytes = read(fd, &frame, sizeof(struct can_frame));
  2132. //For Test (Print Rx Messages)
  2133. //PRINT_CAN_FRAME(&frame);
  2134. //Filtering for legal CAN Rx Message
  2135. if((frame.can_id == 0) || //Null CAN ID
  2136. ((frame.can_id & 0x08000000) == 1) || //Direction
  2137. (((frame.can_id & 0x000000FF) != ShmInternalComm->SlaveAddress) && ((frame.can_id & 0x000000FF) != 0))) //allow its SlaveAddress and Broadcasting Packetes (ID = 0x00)
  2138. {
  2139. continue;
  2140. }
  2141. //--------------------- Legal CAN Rx Message Zone ----------------------
  2142. //CAN comm. Timeout Detector
  2143. ftime(&ShmInternalComm->CAN_Rx_Timer_Start);
  2144. //Extracting and Parsing Legal CAN Message ID (1-Byte)
  2145. switch(frame.can_id & 0x0000FF00)
  2146. {
  2147. case CAN_CMD_ADDRESS_ASSIGN: //0x00000200
  2148. {
  2149. Proc_AddressAssignReq(&frame);
  2150. break;
  2151. }
  2152. case CAN_CMD_GET_FW_VERSION: //0x00000400
  2153. {
  2154. Proc_GetFirmwareVersionReq(&frame);
  2155. break;
  2156. }
  2157. case CAN_CMD_GET_HW_VERSION: //0x00000500
  2158. {
  2159. Proc_HardwareVersionReq(&frame);
  2160. break;
  2161. }
  2162. case CAN_CMD_CHARGING_PERMISSION: //0x00000600
  2163. {
  2164. Proc_ChargingPermissionReq(&frame);
  2165. break;
  2166. }
  2167. case CAN_CMD_EVSE_OUTPUT_STATUS_ANNOUNCEMENT: //0x00000700
  2168. {
  2169. Proc_EVSEOutputStatusUpdateReq(&frame);
  2170. break;
  2171. }
  2172. case CAN_CMD_EVSE_CAPACITY_ANNOUNCEMENT: //0x00000800
  2173. {
  2174. Proc_EVSECapacityUpdateReq(&frame);
  2175. break;
  2176. }
  2177. case CAN_CMD_GET_EV_TARGET_INFO: //0x00000900
  2178. {
  2179. Proc_GetEVTargetReq(&frame);
  2180. break;
  2181. }
  2182. case CAN_CMD_GET_EV_BATTERY_INFO: //0x00000A00
  2183. {
  2184. Proc_GetEVBatteryInfoReq(&frame);
  2185. break;
  2186. }
  2187. case CAN_CMD_EVSE_STOP_EVENT: //0x00000C00
  2188. {
  2189. Proc_EVSEStopReq(&frame);
  2190. break;
  2191. }
  2192. case CAN_CMD_GET_MISC_INFO: //0x00000D00
  2193. {
  2194. Proc_GetMiscellaneousInfoReq(&frame);
  2195. break;
  2196. }
  2197. case CAN_CMD_DOWNLOAD_REQUEST: //0x00000E00
  2198. {
  2199. //Proc_DownloadImageReq(&frame);
  2200. break;
  2201. }
  2202. case CAN_CMD_START_BLOCK_TRANSFER: //0x00000F00
  2203. {
  2204. //Proc_BlockTransferStartReq(&frame);
  2205. break;
  2206. }
  2207. case CAN_CMD_DATA_TRANSFER: //0x00001000
  2208. {
  2209. //Proc_DataTransferReq(&frame);
  2210. break;
  2211. }
  2212. case CAN_CMD_DOWNLOAD_FINISH: //0x00001100
  2213. {
  2214. Proc_DownloadFinishReq(&frame);
  2215. break;
  2216. }
  2217. case CAN_CMD_ISOLATION_STATUS: //0x00001200
  2218. {
  2219. Proc_IsolationStatusAnnounceReq(&frame);
  2220. break;
  2221. }
  2222. case CAN_CMD_CCS_CONNECTOR_INFO: //0x00001300
  2223. {
  2224. Proc_CCSConnectorTypeSetReq(&frame);
  2225. break;
  2226. }
  2227. case CAN_CMD_RTC_INFO: //0x00001400
  2228. {
  2229. Proc_RTCSetReq(&frame);
  2230. break;
  2231. }
  2232. case CAN_CMD_EVSE_PRECHARGE_INFO: //0x00001500
  2233. {
  2234. Proc_EVSEPrechargeInfoUpdateReq(&frame);
  2235. break;
  2236. }
  2237. case CAN_CMD_EVCCID_REQUEST:
  2238. {
  2239. Proc_EVCCIDReq(&frame);
  2240. break;
  2241. }
  2242. default:
  2243. {
  2244. sprintf(buf_log_csucomm_fork2,
  2245. "[fork2][ERROR]Unexpected CAN Command ID: %d (DEC)",
  2246. (frame.can_id & 0x0000FF00));
  2247. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm_fork2);
  2248. break;
  2249. }
  2250. }
  2251. }
  2252. }
  2253. /*===========================================================================
  2254. FUNCTION: Eth0_PortSetting
  2255. DESCRIPTION:
  2256. PRE-CONDITION:
  2257. INPUT:
  2258. OUTPUT:
  2259. GLOBAL VARIABLES:
  2260. =============================================================================*/
  2261. int Eth0_PortSetting_Add(unsigned char addr)
  2262. {
  2263. SAVE_SYS_LOG_MSG_CSUCOMM("[Eth0_PortSetting_Add]start");
  2264. unsigned char cmd[128];
  2265. if ((addr >= 1) && (addr <= 20))
  2266. {
  2267. addr = 20 + addr;
  2268. //Step 0: shutdown eth0
  2269. #if 0
  2270. SAVE_SYS_LOG_MSG_CSUCOMM("[Eth0_PortSetting][eth0:0]down");
  2271. system("/sbin/ifconfig eth0:0 down");
  2272. sleep(1);
  2273. #endif
  2274. //Step 1: IP setting
  2275. sprintf(buf_log_csucomm,
  2276. "[Eth0_PortSetting_Add][eth0:0]IP >> 192.168.0.%d(up))",
  2277. addr);
  2278. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2279. sprintf(cmd, "/sbin/ifconfig eth0:0 192.168.0.%d netmask 255.255.255.0 up", addr);
  2280. system(cmd);
  2281. memset(cmd, 0, sizeof(cmd));
  2282. sleep(1);
  2283. sprintf(buf_log_csucomm,
  2284. "[Eth0_PortSetting_Add][eth0:0]done",
  2285. addr);
  2286. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2287. return PASS;
  2288. }
  2289. else
  2290. {
  2291. sprintf(buf_log_csucomm,
  2292. "[Eth0_PortSetting_Add][Error]addr(%d) is out of range(1~20)",
  2293. addr);
  2294. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2295. return FAIL;
  2296. }
  2297. }
  2298. /*===========================================================================
  2299. FUNCTION: Reboot_Process
  2300. DESCRIPTION:
  2301. 1. fork1
  2302. PRE-CONDITION:
  2303. INPUT:
  2304. OUTPUT:
  2305. GLOBAL VARIABLES:
  2306. =============================================================================*/
  2307. int Reboot_Process()
  2308. {
  2309. if (CSUCOMMDC_TASK_FLAG.Reboot_Process_inused == FALSE)
  2310. {
  2311. SAVE_SYS_LOG_MSG_CSUCOMM("[Reboot_Process]entered");
  2312. CSUCOMMDC_TASK_FLAG.Reboot_Process_inused = TRUE;
  2313. #if (FIRMWARE_VERSION_COMPILE_SETTING_RELEASE_MODE == DISABLE)
  2314. {
  2315. SAVE_SYS_LOG_MSG_CSUCOMM("[Reboot_Process]sync...");
  2316. system("sync");
  2317. }
  2318. #endif
  2319. system("sleep 2");
  2320. system("cd /root;./reboot.sh");
  2321. }
  2322. }
  2323. /*===========================================================================
  2324. FUNCTION: CsuCommAC_GetCcsFirmwareVersion
  2325. DESCRIPTION:
  2326. PRE-CONDITION:
  2327. INPUT:
  2328. OUTPUT:
  2329. GLOBAL VARIABLES:
  2330. =============================================================================*/
  2331. int CsuCommAC_GetCcsFirmwareVersion(unsigned char *buf)
  2332. {
  2333. int i = 0, j = 0, err = PASS, leng = 0;
  2334. unsigned char str[FIRMWARE_VERSION_LENGTH + 1];
  2335. memset(buf, 0, sizeof(buf));
  2336. strcpy(str, FIRMWARE_VERSION);
  2337. leng = FIRMWARE_VERSION_LENGTH + 3;
  2338. if (sizeof(buf) < leng)
  2339. {
  2340. SAVE_SYS_LOG_MSG_CSUCOMM("[WARNING][CsuCommAC_GetCcsFirmwareVersion]ver size: too long");
  2341. leng = sizeof(buf);
  2342. }
  2343. for(i = 0; i < leng; i++)
  2344. {
  2345. if (str[i] != '_')
  2346. {
  2347. buf[j] = str[i];
  2348. j++;
  2349. }
  2350. }
  2351. return err;
  2352. }
  2353. /*===========================================================================
  2354. FUNCTION: CsuCommAC_Init
  2355. DESCRIPTION:
  2356. 1. Set CCS FW Version
  2357. PRE-CONDITION:
  2358. 1. Share Memory should be initialized in advance.
  2359. INPUT:
  2360. OUTPUT:
  2361. GLOBAL VARIABLES:
  2362. =============================================================================*/
  2363. int CsuCommAC_Init()
  2364. {
  2365. int err = PASS;
  2366. CsuCommAC_GetCcsFirmwareVersion(CSUCOMMAC_SHM.CCSLibRev);
  2367. memset(CSUCOMMAC_SHM.EVCCID, 0, sizeof(CSUCOMMAC_SHM.EVCCID));
  2368. CSUCOMMAC_SHM.PresentMsgFlowStatus = IDLE;
  2369. CSUCOMMAC_SHM.ConnectorLockerStatus = FALSE;
  2370. CSUCOMMAC_SHM.ConnectorTemperature1 = 0;
  2371. CSUCOMMAC_SHM.ConnectorTemperature2 = 0;
  2372. CSUCOMMAC_SHM.TotalBatteryCapacity = 0;
  2373. CSUCOMMAC_SHM.BatteryMaximumVoltage = 0;
  2374. CSUCOMMAC_SHM.BatteryMaximumCurrent = 0;
  2375. CSUCOMMAC_SHM.CCSAlarmStatusCode = 0;
  2376. CSUCOMMAC_SHM.PaymentOption = 0;
  2377. CSUCOMMAC_SHM.EVSEMaxCurrent = 0;
  2378. CSUCOMMAC_SHM.EVSEMinCurrent = 0;
  2379. CSUCOMMAC_SHM.EVOperation = 0;
  2380. CSUCOMMAC_SHM.EVChargeProgress = 0;
  2381. CSUCOMMAC_SHM.CpSetPWMDuty = 0;
  2382. CSUCOMMAC_SHM.CpSetStateE = 0;
  2383. CSUCOMMAC_SHM.CcsHeartBeat = 0;
  2384. return err;
  2385. }
  2386. /*===========================================================================
  2387. FUNCTION: CsuCommAC_HeartBeat_Handler
  2388. DESCRIPTION:
  2389. PRE-CONDITION:
  2390. INPUT:
  2391. OUTPUT:
  2392. GLOBAL VARIABLES:
  2393. =============================================================================*/
  2394. int CsuCommAC_HeartBeat_Handler()
  2395. {
  2396. static unsigned int tmp = 0;
  2397. if (tmp == 0)
  2398. {
  2399. tmp = time(NULL);
  2400. }
  2401. else
  2402. {
  2403. if ((time(NULL) - tmp) >= 1) //1 sec
  2404. {
  2405. CSUCOMMAC_SHM.CcsHeartBeat++;
  2406. tmp = 0;
  2407. //sprintf(buf_log_csucomm, "[CcsHeartBeat]%d", CSUCOMMAC_SHM.CcsHeartBeat);
  2408. //SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2409. if (CSUCOMMAC_SHM.CcsHeartBeat >= 60) //timeout: 60 sec
  2410. {
  2411. sprintf(buf_log_csucomm,
  2412. "[WARNING]CSU Comm: Fail(%d)",
  2413. CSUCOMMAC_SHM.CcsHeartBeat);
  2414. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2415. //[To-Do] Reboot or Send Error Code (TBD)
  2416. }
  2417. }
  2418. }
  2419. }
  2420. /*===========================================================================
  2421. FUNCTION: CsuCommAC_SHM_Rx_Update
  2422. DESCRIPTION:
  2423. PRE-CONDITION:
  2424. INPUT:
  2425. OUTPUT:
  2426. GLOBAL VARIABLES:
  2427. =============================================================================*/
  2428. int CsuCommAC_SHM_Rx_Update()
  2429. {
  2430. //[CAUTION] Re-initializing process is necessary. (to-be implemented)
  2431. //----------[1] Permission --------
  2432. ShmInternalComm->ChargingPermission_new = (unsigned char) CSUCOMMAC_SHM.ChargingPermission;
  2433. if (ShmInternalComm->ChargingPermission_new != ShmInternalComm->ChargingPermission)
  2434. {
  2435. sprintf(buf_log_csucomm, "Permission: %d >> %d",
  2436. ShmInternalComm->ChargingPermission,
  2437. ShmInternalComm->ChargingPermission_new
  2438. );
  2439. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2440. ShmInternalComm->ChargingPermission_pre = ShmInternalComm->ChargingPermission;
  2441. ShmInternalComm->ChargingPermission = ShmInternalComm->ChargingPermission_new;
  2442. }
  2443. // -------- [2] CpPresentPWMDuty --------
  2444. ShmInternalComm->AC_CpPresentPWMDuty = CSUCOMMAC_SHM.CpPresentPWMDuty; //unit: 1 sec.
  2445. if (ShmInternalComm->AC_CpPresentPWMDuty_pre != ShmInternalComm->AC_CpPresentPWMDuty)
  2446. {
  2447. sprintf(buf_log_csucomm, "CP Duty(real): %d >> %d (1\%)",
  2448. ShmInternalComm->AC_CpPresentPWMDuty_pre,
  2449. ShmInternalComm->AC_CpPresentPWMDuty
  2450. );
  2451. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2452. ShmInternalComm->AC_CpPresentPWMDuty_pre = ShmInternalComm->AC_CpPresentPWMDuty;
  2453. }
  2454. // -------- [3] CpPresentState --------
  2455. ShmInternalComm->AC_CpPresentState = CSUCOMMAC_SHM.CpPresentState; //unit: 1 sec.
  2456. if (ShmInternalComm->AC_CpPresentState_pre != ShmInternalComm->AC_CpPresentState)
  2457. {
  2458. sprintf(buf_log_csucomm, "CP State: %d >> %d",
  2459. ShmInternalComm->AC_CpPresentState_pre,
  2460. ShmInternalComm->AC_CpPresentState
  2461. );
  2462. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2463. ShmInternalComm->AC_CpPresentState_pre = ShmInternalComm->AC_CpPresentState;
  2464. }
  2465. // -------- [4] ChargingRemainTime --------
  2466. ShmInternalComm->AC_ChargingRemainTime = CSUCOMMAC_SHM.ChargingRemainTime; //unit: 1 sec.
  2467. if (ShmInternalComm->AC_ChargingRemainTime_pre != ShmInternalComm->AC_ChargingRemainTime)
  2468. {
  2469. sprintf(buf_log_csucomm, "ChargingRemainTime: %d >> %d (1 sec)",
  2470. ShmInternalComm->AC_ChargingRemainTime_pre,
  2471. ShmInternalComm->AC_ChargingRemainTime
  2472. );
  2473. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2474. ShmInternalComm->AC_ChargingRemainTime_pre = ShmInternalComm->AC_ChargingRemainTime;
  2475. }
  2476. // -------- [5] CSUAlarmStatusCode --------
  2477. ShmInternalComm->AC_CSUAlarmStatusCode = CSUCOMMAC_SHM.CSUAlarmStatusCode;
  2478. if (ShmInternalComm->AC_CSUAlarmStatusCode_pre != ShmInternalComm->AC_CSUAlarmStatusCode)
  2479. {
  2480. sprintf(buf_log_csucomm, "CSUAlarmStatusCode: %X >> %X (1V)",
  2481. ShmInternalComm->AC_CSUAlarmStatusCode_pre,
  2482. ShmInternalComm->AC_CSUAlarmStatusCode
  2483. );
  2484. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2485. ShmInternalComm->AC_CSUAlarmStatusCode_pre = ShmInternalComm->AC_CSUAlarmStatusCode;
  2486. }
  2487. // -------- [6] MeterReadingValue --------
  2488. ShmInternalComm->AC_MeterReadingValue = CSUCOMMAC_SHM.MeterReadingValue; //unit: 1WH
  2489. if (ShmInternalComm->AC_MeterReadingValue_pre != ShmInternalComm->AC_MeterReadingValue)
  2490. {
  2491. sprintf(buf_log_csucomm, "MeterReadingValue(Power): %.02f >> %.02f (1V)",
  2492. ShmInternalComm->AC_MeterReadingValue_pre,
  2493. ShmInternalComm->AC_MeterReadingValue
  2494. );
  2495. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2496. ShmInternalComm->AC_MeterReadingValue_pre = ShmInternalComm->AC_MeterReadingValue;
  2497. }
  2498. // -------- [7] CpPositiveVoltage --------
  2499. ShmInternalComm->AC_CpPositiveVoltage = CSUCOMMAC_SHM.CpPositiveVoltage; //unit: 1V
  2500. #if 0 //the modification message will be logged via Check_CP_State_Update_fork1() in SeccComm task.
  2501. if (ShmInternalComm->AC_CpPositiveVoltage_pre != ShmInternalComm->AC_CpPositiveVoltage)
  2502. {
  2503. sprintf(buf_log_csucomm, "CP_v_p: %.02f >> %.02f (1V)",
  2504. ShmInternalComm->AC_CpPositiveVoltage_pre,
  2505. ShmInternalComm->AC_CpPositiveVoltage
  2506. );
  2507. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2508. ShmInternalComm->AC_CpPositiveVoltage_pre = ShmInternalComm->AC_CpPositiveVoltage;
  2509. }
  2510. #endif
  2511. // -------- [8] CpNegativeVoltage --------
  2512. ShmInternalComm->AC_CpNegativeVoltage = CSUCOMMAC_SHM.CpNegativeVoltage; //unit: 1V
  2513. if (ShmInternalComm->AC_CpNegativeVoltage_pre != ShmInternalComm->AC_CpNegativeVoltage)
  2514. {
  2515. sprintf(buf_log_csucomm, "CP_v_n: %.02f >> %.02f (1V)",
  2516. ShmInternalComm->AC_CpNegativeVoltage_pre,
  2517. ShmInternalComm->AC_CpNegativeVoltage
  2518. );
  2519. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2520. ShmInternalComm->AC_CpNegativeVoltage_pre = ShmInternalComm->AC_CpNegativeVoltage;
  2521. }
  2522. // -------- [9] I_now: EVSEPresentCurrent[3] --------
  2523. memcpy(ShmInternalComm->AC_EVSEPresentCurrent, CSUCOMMAC_SHM.EVSEPresentCurrent, 3); //unit: 1A
  2524. EVCOMM_SYS_INFO.PresentChargingCurrent = ShmInternalComm->AC_EVSEPresentCurrent[0]; //unit: 1A (using phase L)
  2525. ShmInternalComm->PresentChargingCurrent = (int)(ShmInternalComm->AC_EVSEPresentCurrent[0] * 10); //unit: 0.1A
  2526. if ((ShmInternalComm->AC_EVSEPresentCurrent[0] != ShmInternalComm->AC_EVSEPresentCurrent[0]) ||
  2527. (ShmInternalComm->AC_EVSEPresentCurrent[1] != ShmInternalComm->AC_EVSEPresentCurrent[1]) ||
  2528. (ShmInternalComm->AC_EVSEPresentCurrent[2] != ShmInternalComm->AC_EVSEPresentCurrent[2])
  2529. )
  2530. {
  2531. if ((abs(ShmInternalComm->AC_EVSEPresentCurrent_pre[0] - ShmInternalComm->AC_EVSEPresentCurrent[0]) > 10) || //10/;1A
  2532. (abs(ShmInternalComm->AC_EVSEPresentCurrent_pre[1] - ShmInternalComm->AC_EVSEPresentCurrent[1]) > 10) || //10/;1A
  2533. (abs(ShmInternalComm->AC_EVSEPresentCurrent_pre[2] - ShmInternalComm->AC_EVSEPresentCurrent[2]) > 10) //10/;1A
  2534. )
  2535. {
  2536. sprintf(buf_log_csucomm, "I_now(EVSE): (%.02f,%.02f,%.02f) >> (%.02f,%.02f,%.02f) (1A)",
  2537. ShmInternalComm->AC_EVSEPresentCurrent_pre[0],
  2538. ShmInternalComm->AC_EVSEPresentCurrent_pre[1],
  2539. ShmInternalComm->AC_EVSEPresentCurrent_pre[2],
  2540. ShmInternalComm->AC_EVSEPresentCurrent[0],
  2541. ShmInternalComm->AC_EVSEPresentCurrent[1],
  2542. ShmInternalComm->AC_EVSEPresentCurrent[2]
  2543. );
  2544. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2545. }
  2546. ShmInternalComm->PresentChargingCurrent_pre = ShmInternalComm->PresentChargingCurrent;
  2547. memcpy(ShmInternalComm->AC_EVSEPresentCurrent_pre, ShmInternalComm->AC_EVSEPresentCurrent, 3);
  2548. }
  2549. //-------- [10] OutputRelayStatus --------
  2550. ShmInternalComm->AC_OutputRelayStatus = CSUCOMMAC_SHM.OutputRelayStatus; //bool
  2551. if (ShmInternalComm->AC_OutputRelayStatus_pre != ShmInternalComm->AC_OutputRelayStatus)
  2552. {
  2553. sprintf(buf_log_csucomm, "RelayON: %d >> %d",
  2554. ShmInternalComm->AC_OutputRelayStatus_pre,
  2555. ShmInternalComm->AC_OutputRelayStatus
  2556. );
  2557. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2558. ShmInternalComm->AC_OutputRelayStatus_pre = ShmInternalComm->AC_OutputRelayStatus;
  2559. }
  2560. // -------- [11] GridVoltage (EVSENominalVoltage) --------
  2561. EVCOMM_SYS_INFO.AC_EVSENominalVoltage = CSUCOMMAC_SHM.GridVoltage[0]; //unit: 1V
  2562. memcpy(ShmInternalComm->AC_GridVoltage, CSUCOMMAC_SHM.GridVoltage, 3); //unit: 1V
  2563. if ((ShmInternalComm->AC_GridVoltage_pre[0] != ShmInternalComm->AC_GridVoltage[0]) ||
  2564. (ShmInternalComm->AC_GridVoltage_pre[1] != ShmInternalComm->AC_GridVoltage[1]) ||
  2565. (ShmInternalComm->AC_GridVoltage_pre[2] != ShmInternalComm->AC_GridVoltage[2])
  2566. )
  2567. {
  2568. sprintf(buf_log_csucomm, "V_grid: (%.02f,%.02f,%.02f) >> (%.02f,%.02f,%.02f) (1V)",
  2569. ShmInternalComm->AC_GridVoltage_pre[0],
  2570. ShmInternalComm->AC_GridVoltage_pre[1],
  2571. ShmInternalComm->AC_GridVoltage_pre[2],
  2572. ShmInternalComm->AC_GridVoltage[0],
  2573. ShmInternalComm->AC_GridVoltage[1],
  2574. ShmInternalComm->AC_GridVoltage[2]
  2575. );
  2576. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2577. memcpy(ShmInternalComm->AC_GridVoltage_pre, ShmInternalComm->AC_GridVoltage, 3);
  2578. }
  2579. // -------- [12] V_now --------
  2580. if (ShmInternalComm->AC_OutputRelayStatus == ENABLE)
  2581. {
  2582. EVCOMM_SYS_INFO.PresentChargingVoltage = ShmInternalComm->AC_GridVoltage[0]; //unit: 1V (using phase 1)
  2583. ShmInternalComm->PresentChargingVoltage = (int)(ShmInternalComm->AC_GridVoltage[0] * 10); //unit: 0.1V (using phase 1)
  2584. }
  2585. else
  2586. {
  2587. EVCOMM_SYS_INFO.PresentChargingVoltage = 0;
  2588. ShmInternalComm->PresentChargingVoltage = 0;
  2589. }
  2590. if (ShmInternalComm->PresentChargingVoltage_pre != ShmInternalComm->PresentChargingVoltage)
  2591. {
  2592. if (abs(ShmInternalComm->PresentChargingVoltage_pre - ShmInternalComm->PresentChargingVoltage) > 10) //10:1V
  2593. {
  2594. sprintf(buf_log_csucomm, "V_now(EVSE): %d >> %d (0.1V, DEC)",
  2595. ShmInternalComm->PresentChargingVoltage_pre,
  2596. ShmInternalComm->PresentChargingVoltage
  2597. );
  2598. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2599. }
  2600. ShmInternalComm->PresentChargingVoltage_pre = ShmInternalComm->PresentChargingVoltage;
  2601. }
  2602. // -------- [13] P_now --------
  2603. EVCOMM_SYS_INFO.PresentChargingPower = (EVCOMM_SYS_INFO.PresentChargingVoltage * EVCOMM_SYS_INFO.PresentChargingCurrent)/1000; //1KW
  2604. ShmInternalComm->PresentChargingPower = (int)(EVCOMM_SYS_INFO.PresentChargingPower * 10); //0.1KW
  2605. if (ShmInternalComm->PresentChargingPower_pre != ShmInternalComm->PresentChargingPower)
  2606. {
  2607. sprintf(buf_log_csucomm, "P_now(EVSE): %d >> %d (0.1KW)",
  2608. ShmInternalComm->PresentChargingPower_pre,
  2609. ShmInternalComm->PresentChargingPower);
  2610. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2611. ShmInternalComm->PresentChargingPower_pre = ShmInternalComm->PresentChargingPower;
  2612. }
  2613. // -------- [14] I_max (EVSE)--------
  2614. EVCOMM_SYS_INFO.AvailableChargingCurrent = CSUCOMMAC_SHM.EVSEMaxCurrent; //unit: 1A
  2615. ShmInternalComm->AvailableChargingCurrent = (int)(CSUCOMMAC_SHM.EVSEMaxCurrent *10); //unit: 0.1A
  2616. if (ShmInternalComm->AvailableChargingCurrent_pre != ShmInternalComm->AvailableChargingCurrent)
  2617. {
  2618. sprintf(buf_log_csucomm, "I_max(EVSE): %d >> %d (0.1A)",
  2619. ShmInternalComm->AvailableChargingCurrent_pre,
  2620. ShmInternalComm->AvailableChargingCurrent);
  2621. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2622. ShmInternalComm->AvailableChargingCurrent_pre = ShmInternalComm->AvailableChargingCurrent;
  2623. }
  2624. // -------- [15] I_min (EVSE)--------
  2625. ShmInternalComm->AC_EVSEMinCurrent = CSUCOMMAC_SHM.EVSEMinCurrent; //unit: 1A
  2626. if (ShmInternalComm->AC_EVSEMinCurrent_pre != ShmInternalComm->AC_EVSEMinCurrent)
  2627. {
  2628. sprintf(buf_log_csucomm, "I_min(EVSE): %.02f >> %.02f (1A)",
  2629. ShmInternalComm->AC_EVSEMinCurrent_pre,
  2630. ShmInternalComm->AC_EVSEMinCurrent);
  2631. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2632. ShmInternalComm->AC_EVSEMinCurrent_pre = ShmInternalComm->AC_EVSEMinCurrent;
  2633. }
  2634. // -------- [16] V_max --------
  2635. EVCOMM_SYS_INFO.MaximumChargingVoltage = CSUCOMMAC_SHM.GridVoltage[0]; //unit: 1V (using phase 1)
  2636. ShmInternalComm->MaximumChargingVoltage = (int)(CSUCOMMAC_SHM.GridVoltage[0] * 10); //unit: 0.1V (using phase 1)
  2637. if (ShmInternalComm->MaximumChargingVoltage_pre != ShmInternalComm->MaximumChargingVoltage)
  2638. {
  2639. sprintf(buf_log_csucomm, "V_max(EVSE): %d >> %d (0.1V, DEC)",
  2640. ShmInternalComm->MaximumChargingVoltage_pre,
  2641. ShmInternalComm->MaximumChargingVoltage
  2642. );
  2643. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2644. ShmInternalComm->MaximumChargingVoltage_pre = ShmInternalComm->MaximumChargingVoltage;
  2645. }
  2646. // -------- [17] P_max --------
  2647. ShmInternalComm->AC_AvailableChargingPower = CSUCOMMAC_SHM.AvailableChargingPower; //unit: 1KW
  2648. EVCOMM_SYS_INFO.AvailableChargingPower = ShmInternalComm->AC_AvailableChargingPower; //1KW
  2649. ShmInternalComm->AvailableChargingPower = (int)(ShmInternalComm->AC_AvailableChargingPower * 10); //unit: 0.1KW
  2650. if (ShmInternalComm->AvailableChargingPower_pre != ShmInternalComm->AvailableChargingPower)
  2651. {
  2652. sprintf(buf_log_csucomm, "P_max(EVSE): %d >> %d (0.1KW)",
  2653. ShmInternalComm->AvailableChargingPower_pre,
  2654. ShmInternalComm->AvailableChargingPower);
  2655. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2656. ShmInternalComm->AvailableChargingPower_pre = ShmInternalComm->AvailableChargingPower;
  2657. }
  2658. // -------- [18] EVSEID[40] --------
  2659. memcpy(ShmInternalComm->AC_EVSEID, CSUCOMMAC_SHM.EVSEID, 40);
  2660. if (Array_Compare_Identity(ShmInternalComm->AC_EVSEID, CSUCOMMAC_SHM.EVSEID, 40) == FALSE)
  2661. {
  2662. sprintf(buf_log_csucomm, "EVSEID: [%02X %02X %02X %02X %02X %02X...] >> [%02X %02X %02X %02X %02X %02X...]",
  2663. ShmInternalComm->AC_EVSEID_pre[0],
  2664. ShmInternalComm->AC_EVSEID_pre[1],
  2665. ShmInternalComm->AC_EVSEID_pre[2],
  2666. ShmInternalComm->AC_EVSEID_pre[3],
  2667. ShmInternalComm->AC_EVSEID_pre[4],
  2668. ShmInternalComm->AC_EVSEID_pre[5],
  2669. ShmInternalComm->AC_EVSEID[0],
  2670. ShmInternalComm->AC_EVSEID[1],
  2671. ShmInternalComm->AC_EVSEID[2],
  2672. ShmInternalComm->AC_EVSEID[3],
  2673. ShmInternalComm->AC_EVSEID[4],
  2674. ShmInternalComm->AC_EVSEID[5]
  2675. );
  2676. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2677. memcpy(ShmInternalComm->AC_EVSEID_pre, ShmInternalComm->AC_EVSEID_pre, 40);
  2678. }
  2679. // -------- [19] MeterID[32] --------
  2680. memcpy(ShmInternalComm->AC_MeterID, CSUCOMMAC_SHM.MeterID, 32);
  2681. if (Array_Compare_Identity(ShmInternalComm->AC_MeterID, CSUCOMMAC_SHM.MeterID, 32) == FALSE)
  2682. {
  2683. sprintf(buf_log_csucomm, "MeterID: [%02X %02X %02X %02X %02X %02X...] >> [%02X %02X %02X %02X %02X %02X...]",
  2684. ShmInternalComm->AC_MeterID_pre[0],
  2685. ShmInternalComm->AC_MeterID_pre[1],
  2686. ShmInternalComm->AC_MeterID_pre[2],
  2687. ShmInternalComm->AC_MeterID_pre[3],
  2688. ShmInternalComm->AC_MeterID_pre[4],
  2689. ShmInternalComm->AC_MeterID_pre[5],
  2690. ShmInternalComm->AC_MeterID[0],
  2691. ShmInternalComm->AC_MeterID[1],
  2692. ShmInternalComm->AC_MeterID[2],
  2693. ShmInternalComm->AC_MeterID[3],
  2694. ShmInternalComm->AC_MeterID[4],
  2695. ShmInternalComm->AC_MeterID[5]
  2696. );
  2697. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2698. memcpy(ShmInternalComm->AC_MeterID_pre, ShmInternalComm->AC_MeterID_pre, 32);
  2699. }
  2700. // -------- [20] EVSEModelName[32] --------
  2701. memcpy(ShmInternalComm->AC_EVSEModelName, CSUCOMMAC_SHM.EVSEModelName, 32);
  2702. if (Array_Compare_Identity(ShmInternalComm->AC_EVSEModelName, CSUCOMMAC_SHM.EVSEModelName, 32) == FALSE)
  2703. {
  2704. sprintf(buf_log_csucomm, "EVSEModelName: [%02X %02X %02X %02X %02X %02X...] >> [%02X %02X %02X %02X %02X %02X...]",
  2705. ShmInternalComm->AC_EVSEModelName_pre[0],
  2706. ShmInternalComm->AC_EVSEModelName_pre[1],
  2707. ShmInternalComm->AC_EVSEModelName_pre[2],
  2708. ShmInternalComm->AC_EVSEModelName_pre[3],
  2709. ShmInternalComm->AC_EVSEModelName_pre[4],
  2710. ShmInternalComm->AC_EVSEModelName_pre[5],
  2711. ShmInternalComm->AC_EVSEModelName[0],
  2712. ShmInternalComm->AC_EVSEModelName[1],
  2713. ShmInternalComm->AC_EVSEModelName[2],
  2714. ShmInternalComm->AC_EVSEModelName[3],
  2715. ShmInternalComm->AC_EVSEModelName[4],
  2716. ShmInternalComm->AC_EVSEModelName[5]
  2717. );
  2718. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2719. memcpy(ShmInternalComm->AC_EVSEModelName_pre, ShmInternalComm->AC_EVSEModelName_pre, 32);
  2720. }
  2721. // -------- [21] BatteryChargeType --------
  2722. ShmInternalComm->AC_BatteryChargeType = CSUCOMMAC_SHM.BatteryChargeType;
  2723. if (ShmInternalComm->AC_BatteryChargeType_pre != ShmInternalComm->AC_BatteryChargeType)
  2724. {
  2725. sprintf(buf_log_csucomm, "BatteryChargeType: %d >> %d",
  2726. ShmInternalComm->AC_BatteryChargeType_pre,
  2727. ShmInternalComm->AC_BatteryChargeType
  2728. );
  2729. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2730. ShmInternalComm->AC_BatteryChargeType_pre = ShmInternalComm->AC_BatteryChargeType;
  2731. }
  2732. // -------- [21] RcdStatus --------
  2733. ShmInternalComm->AC_RcdStatus = CSUCOMMAC_SHM.RcdStatus;
  2734. if (ShmInternalComm->AC_RcdStatus_pre != ShmInternalComm->AC_RcdStatus)
  2735. {
  2736. sprintf(buf_log_csucomm, "RCDStatus: %d >> %d",
  2737. ShmInternalComm->AC_RcdStatus_pre,
  2738. ShmInternalComm->AC_RcdStatus
  2739. );
  2740. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2741. ShmInternalComm->AC_RcdStatus_pre = ShmInternalComm->AC_RcdStatus;
  2742. }
  2743. // -------- [22] EVSENotification --------
  2744. ShmInternalComm->AC_EVSENotification = CSUCOMMAC_SHM.EVSENotification;
  2745. if (ShmInternalComm->AC_EVSENotification_pre != ShmInternalComm->AC_EVSENotification)
  2746. {
  2747. sprintf(buf_log_csucomm, "AC_EVSENotification: %d >> %d",
  2748. ShmInternalComm->AC_EVSENotification_pre,
  2749. ShmInternalComm->AC_EVSENotification
  2750. );
  2751. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2752. ShmInternalComm->AC_EVSENotification_pre = ShmInternalComm->AC_EVSENotification;
  2753. }
  2754. }
  2755. /*===========================================================================
  2756. FUNCTION: CsuCommAC_Error_Monitor
  2757. DESCRIPTION:
  2758. 1. fork1
  2759. PRE-CONDITION:
  2760. INPUT:
  2761. OUTPUT:
  2762. GLOBAL VARIABLES:
  2763. =============================================================================*/
  2764. void CsuCommAC_Error_Monitor()
  2765. {
  2766. pid_t tmp = 0;
  2767. if(PID_CsuComm_Error_Monitor_Task == 0)
  2768. {
  2769. tmp = fork(); //fork1
  2770. if(tmp > 0)
  2771. {
  2772. PID_CsuComm_Error_Monitor_Task = tmp;
  2773. sprintf(buf_log_csucomm_fork1,
  2774. "[fork2][Error_Monitor]created(%d)",
  2775. PID_CsuComm_Error_Monitor_Task);
  2776. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm_fork1);
  2777. #if 0
  2778. unsigned char buf[64];
  2779. memset(buf, 0, sizeof(buf));
  2780. sprintf(buf, "renice -20 -p %d", tmp);
  2781. system(buf);
  2782. #endif
  2783. return;
  2784. }
  2785. }
  2786. while(1)
  2787. {
  2788. CsuCommAC_HeartBeat_Handler(); //timeout: 60s
  2789. }
  2790. }
  2791. /*===========================================================================
  2792. FUNCTION: CsuCommAC_Proc
  2793. DESCRIPTION:
  2794. PRE-CONDITION:
  2795. 1. Share Memory should be initialized in advance.
  2796. INPUT:
  2797. OUTPUT:
  2798. GLOBAL VARIABLES:
  2799. =============================================================================*/
  2800. int CsuCommAC_Proc()
  2801. {
  2802. CsuCommAC_Init();
  2803. CsuCommAC_Error_Monitor(); //fork1
  2804. while(1)
  2805. {
  2806. CsuCommAC_SHM_Rx_Update();
  2807. }
  2808. }
  2809. void CheckID()
  2810. {
  2811. unsigned char BoardId;
  2812. unsigned char buf[64];
  2813. BoardId=(Get_GPIO_Value(GPIO_2_23_AM_IO_1) + 1) & 0x000000FF;//auto detect GPIO
  2814. if(ShmInternalComm->SlaveAddress == BoardId)
  2815. return;
  2816. memset(buf,0,sizeof(buf));
  2817. sprintf(buf,"SlaveAddress from %d to %d\n",ShmInternalComm->SlaveAddress,BoardId);
  2818. ShmInternalComm->SlaveAddress = BoardId;
  2819. SAVE_SYS_LOG_MSG_CSUCOMM(buf);
  2820. CSUCOMMDC_TASK_FLAG.matched = TRUE;
  2821. //Changing the eth0 to 192.168.1.20 + ID
  2822. #if (DYNAMIC_ETH0_IP_MECHANISM == ENABLE)
  2823. Eth0_PortSetting_Add(ShmInternalComm->SlaveAddress);
  2824. #endif
  2825. }
  2826. /*===========================================================================
  2827. FUNCTION: main
  2828. DESCRIPTION:
  2829. PRE-CONDITION:
  2830. INPUT:
  2831. OUTPUT:
  2832. GLOBAL VARIABLES:
  2833. 1. PID_CAN_Rx_Task
  2834. =============================================================================*/
  2835. int main(int argc, char *argv[])
  2836. {
  2837. // ======== [STEP 1/5] Initialize Share Memory ========
  2838. if(ShareMemory_Init() == 0)
  2839. {
  2840. SAVE_SYS_LOG_MSG_CSUCOMM("[main]ShareMemory_Init NG");
  2841. if(ShmStatusCodeData != NULL)
  2842. {
  2843. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  2844. }
  2845. sleep(5);
  2846. return 0;
  2847. }
  2848. #if ((CCS_ENERGY_TRANSFER_MODE == MODE_AC_SINGLE_PHASE_CORE) || (CCS_ENERGY_TRANSFER_MODE == MODE_AC_THREE_PHASE_CORE))
  2849. {
  2850. sprintf(buf_log_csucomm,
  2851. "========= CCS: AC (%d)=========",
  2852. CCS_ENERGY_TRANSFER_MODE
  2853. );
  2854. SAVE_SYS_LOG_MSG_CSUCOMM(buf_log_csucomm);
  2855. CsuCommAC_Proc();
  2856. while (1)
  2857. {
  2858. //null
  2859. }
  2860. }
  2861. #endif
  2862. int FD_CAN_Socket; //Socket File Descriptor
  2863. struct can_frame frame;
  2864. unsigned int value_random;
  2865. unsigned char can_tx_payload[8];
  2866. // ======== [STEP 2/5] Initialize CAN BUS ========
  2867. FD_CAN_Socket = CANBus_Init();
  2868. ShmInternalComm->FD_CAN_Socket = FD_CAN_Socket;
  2869. PID_CAN_Rx_Task = 0;
  2870. ShmInternalComm->SlaveAddress = 0xFF; //unmatched
  2871. CSUCOMMDC_TASK_FLAG.matched = FALSE;
  2872. //Sniffering CAN packets
  2873. Sniffer_Candump(ENABLE);
  2874. Error_Monitor_CsuComm(); //fork1
  2875. #if 0 //disable ID assignmemt
  2876. ftime(&ShmInternalComm->Start_Time);
  2877. srandom(ShmInternalComm->Start_Time.millitm);
  2878. value_random = random();
  2879. value_random &= 0xFFFFFF00;
  2880. value_random |= 0x000000CC; //adding a specific header for identification of CCS.
  2881. // ======== [STEP 3/5] Request for A Slave Address ========
  2882. while(ShmInternalComm->SlaveAddress == 0xFF)
  2883. {
  2884. ftime(&ShmInternalComm->Start_Time);
  2885. // srandom(ShmInternalComm->Start_Time.millitm);
  2886. // value_random = random();
  2887. // value_random &= 0xFFFFFF00;
  2888. // value_random |= 0x000000CC; //adding a specific header for identification of CCS.
  2889. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][main] value_random = %08X (4 Bytes, HEX)\n", value_random);
  2890. memset(can_tx_payload, 0, sizeof(can_tx_payload));
  2891. memcpy(can_tx_payload, &value_random, sizeof(int));
  2892. //for test
  2893. #if (CAN_RAMDOM_MATCHING_ID_MECHANISM == DISABLE)
  2894. value_random = 0xC3B2A1CC;
  2895. memset(can_tx_payload, 0, sizeof(can_tx_payload));
  2896. memcpy(can_tx_payload, &value_random, sizeof(int));
  2897. //can_tx_payload[0] = 0xA1;
  2898. //can_tx_payload[1] = 0xB2;
  2899. //can_tx_payload[2] = 0xC3;
  2900. //can_tx_payload[3] = 0xD4;
  2901. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][main] replaced random number = %02X %02X %02X %02X (4 Bytes, HEX)\n",
  2902. can_tx_payload[0],
  2903. can_tx_payload[1],
  2904. can_tx_payload[2],
  2905. can_tx_payload[3],
  2906. can_tx_payload[4]);
  2907. #endif
  2908. can_tx_payload[4] = Get_GPIO_Value(GPIO_2_23_AM_IO_1) + 1; //auto detect GPIO
  2909. //can_tx_payload[4] = 0x01; //(to be implemented: auto detect GPIO)
  2910. //0x01:connector 1
  2911. //0x02:connector 2
  2912. CAN_Tx_MSG(FD_CAN_Socket, CAN_CMD_ADDRESS_REQUEST, ShmInternalComm->SlaveAddress, 5, can_tx_payload);
  2913. ftime(&ShmInternalComm->End_Time);
  2914. //Here is the process after sending the CAN message (t = 0 to t = 100ms)
  2915. while(DiffTimeb(ShmInternalComm->Start_Time, ShmInternalComm->End_Time) < 1000)//resend interval: 1000ms
  2916. {
  2917. unsigned int value_random_return = 0; //4 Bytes
  2918. memset(&frame, 0, sizeof(struct can_frame));
  2919. read(FD_CAN_Socket, &frame, sizeof(struct can_frame));
  2920. ftime(&ShmInternalComm->End_Time);
  2921. PRINT_CAN_FRAME(&frame);
  2922. if( (frame.can_id == 0) || //all zero
  2923. (frame.can_id & 0x08000000) || //to avoid incoreect DIRECTION bit
  2924. ((frame.can_id & 0x0000FF00) != CAN_CMD_ADDRESS_ASSIGN) || //the 1st command ID from Master should be 0x02
  2925. (frame.can_dlc != 5)) //payload length should be only 5 bytes.
  2926. {
  2927. continue;
  2928. }
  2929. memcpy(&value_random_return, frame.data, sizeof(int)); //only copy the previous 4 bytes
  2930. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][main] ori = %X, return = %X (HEX)\n", value_random, value_random_return);
  2931. if(value_random_return == value_random)
  2932. {
  2933. //Getting a legal Slave Address, successfully.
  2934. ShmInternalComm->SlaveAddress = frame.can_id & 0x000000FF;
  2935. CSUCOMMDC_TASK_FLAG.matched = TRUE;
  2936. #if (SAVE_SYS_LOG_MSG_CSUCOMM_SWITCH == ENABLE)
  2937. {
  2938. SAVE_SYS_LOG_MSG_CSUCOMM("Matched");
  2939. }
  2940. #endif
  2941. DEBUG_PRINTF_CSUCOMM_DETAIL("[CsuComm][main] ShmInternalComm->SlaveAddres = %02X (1 Byte, HEX)\t(Matched NOW.)\n", ShmInternalComm->SlaveAddress);
  2942. //Changing the eth0 to 192.168.1.20 + ID
  2943. #if (DYNAMIC_ETH0_IP_MECHANISM == ENABLE)
  2944. Eth0_PortSetting_Add(ShmInternalComm->SlaveAddress);
  2945. #endif
  2946. break;
  2947. }
  2948. }
  2949. }
  2950. #else
  2951. sleep(5);
  2952. CheckID();
  2953. #endif
  2954. // ======== [STEP 4/5] CAN RX ========
  2955. CAN_Rx(FD_CAN_Socket); //precondition: matched already
  2956. //Here is a indivitual process/task which is dealing with all CAN message and commands.
  2957. // ======== [STEP 5/5] CAN TX ========
  2958. //main loop
  2959. ftime(&ShmInternalComm->Start_Time);
  2960. while(1)
  2961. {
  2962. /*
  2963. //CAN_CMD_ADDRESS_REQUEST 0x00000100
  2964. if (CSUCOMMDC_TASK_FLAG.matched == TRUE)
  2965. {
  2966. //Proc_Match(FD_CAN_Socket);
  2967. //CSUCOMMDC_TASK_FLAG.matched = FALSE;
  2968. }
  2969. //CAN_CMD_ADDRESS_ASSIGN 0x00000200
  2970. if (CSUCOMMDC_TASK_FLAG.Got_AssignedAddress == TRUE)
  2971. {
  2972. //Proc_AddressAssignRes(FD_CAN_Socket);
  2973. CSUCOMMDC_TASK_FLAG.Got_AssignedAddress = FALSE;
  2974. }
  2975. */
  2976. //======================================================================
  2977. // CAN_CMD_EV_BOARD_STATUS 0x00000300
  2978. ftime(&ShmInternalComm->End_Time);
  2979. if(DiffTimeb(ShmInternalComm->Start_Time, ShmInternalComm->End_Time) >= 1000) //ori = 1000ms
  2980. {
  2981. //Interval = 1000ms
  2982. CSUCOMMDC_TASK_FLAG.Send_EVBoardStatus = TRUE;
  2983. CheckID();
  2984. }
  2985. if (CSUCOMMDC_TASK_FLAG.Send_EVBoardStatus == TRUE)
  2986. {
  2987. //Tx Interval = 1000ms
  2988. Proc_EVBoardStatusRes(FD_CAN_Socket);
  2989. ftime(&ShmInternalComm->Start_Time);
  2990. }
  2991. // CAN_CMD_GET_FW_VERSION 0x00000400
  2992. if(CSUCOMMDC_TASK_FLAG.Got_FWVersionReq == TRUE)
  2993. {
  2994. Proc_GetFirmwareVersionRes(FD_CAN_Socket);
  2995. }
  2996. // CAN_CMD_GET_HW_VERSION 0x00000500
  2997. if(CSUCOMMDC_TASK_FLAG.Got_HWVersionReq == TRUE)
  2998. {
  2999. Proc_HardwareVersionRes(FD_CAN_Socket);
  3000. }
  3001. //CAN_CMD_CHARGING_PERMISSION 0x00000600
  3002. if (CSUCOMMDC_TASK_FLAG.Got_ChargingPermission == TRUE)
  3003. {
  3004. Proc_ChargingPermissionRes(FD_CAN_Socket); //need real-time response
  3005. }
  3006. // CAN_CMD_EVSE_OUTPUT_STATUS_ANNOUNCEMENT 0x00000700
  3007. if(CSUCOMMDC_TASK_FLAG.Got_EVSEOutputStatus == TRUE)
  3008. {
  3009. Proc_EVSEOutputStatusUpdateRes(FD_CAN_Socket); //need real-time response
  3010. }
  3011. // CAN_CMD_EVSE_CAPACITY_ANNOUNCEMENT 0x00000800
  3012. if (CSUCOMMDC_TASK_FLAG.Got_EnergyCapacity == TRUE)
  3013. {
  3014. Proc_EVSECapacityUpdateRes(FD_CAN_Socket); //need real-time response
  3015. }
  3016. // CAN_CMD_GET_EV_TARGET_INFO 0x00000900
  3017. if(CSUCOMMDC_TASK_FLAG.Got_EVTargetReq == TRUE)
  3018. {
  3019. Proc_GetEVTargetRes(FD_CAN_Socket);
  3020. }
  3021. // CAN_CMD_GET_EV_BATTERY_INFO 0x00000A00
  3022. if(CSUCOMMDC_TASK_FLAG.Got_EVBatteryInfoReq == TRUE)
  3023. {
  3024. Proc_GetEVBatteryInfoRes(FD_CAN_Socket);
  3025. }
  3026. //#define CAN_CMD_EV_STOP_EVENT 0x00000B00
  3027. if (CSUCOMMDC_TASK_FLAG.Send_EVStopReq == TRUE) //The Request is from SeccComm
  3028. {
  3029. Proc_EVStopRes(FD_CAN_Socket);
  3030. }
  3031. //#define CAN_CMD_EVSE_STOP_EVENT 0x00000C00
  3032. if (CSUCOMMDC_TASK_FLAG.Got_EVSEStopReq == TRUE)
  3033. {
  3034. Proc_EVSEStopRes(FD_CAN_Socket);
  3035. }
  3036. //#define CAN_CMD_GET_MISC_INFO 0x00000D00
  3037. if(CSUCOMMDC_TASK_FLAG.Got_MiscellaneousInfoReq == TRUE)
  3038. {
  3039. Proc_GetMiscellaneousInfoRes(FD_CAN_Socket);
  3040. }
  3041. //#define CAN_CMD_DOWNLOAD_REQUEST 0x00000E00
  3042. if (CSUCOMMDC_TASK_FLAG.Got_DownloadImageReq == TRUE)
  3043. {
  3044. Proc_DownloadImageRes(FD_CAN_Socket);
  3045. }
  3046. //#define CAN_CMD_START_BLOCK_TRANSFER 0x00000F00
  3047. if (CSUCOMMDC_TASK_FLAG.Got_BlockTransferStartReq == TRUE)
  3048. {
  3049. Proc_BlockTransferStartRes(FD_CAN_Socket);
  3050. }
  3051. // CAN_CMD_DATA_TRANSFER 0x00001000
  3052. if(CSUCOMMDC_TASK_FLAG.Got_DataTransferReq == TRUE)
  3053. {
  3054. Proc_DataTransferRes(FD_CAN_Socket);
  3055. }
  3056. //CAN_CMD_DOWNLOAD_FINISH 0x00001100
  3057. if (CSUCOMMDC_TASK_FLAG.Got_DownloadFinishReq == TRUE)
  3058. {
  3059. Proc_DownloadFinishRes(FD_CAN_Socket);
  3060. }
  3061. // CAN_CMD_ISOLATION_STATUS 0x00001200
  3062. if (CSUCOMMDC_TASK_FLAG.Got_IsolationStatus == TRUE)
  3063. {
  3064. Proc_IsolationStatusAnnounceRes(FD_CAN_Socket);
  3065. }
  3066. // CAN_CMD_CCS_CONNECTOR_INFO 0x00001300
  3067. if (CSUCOMMDC_TASK_FLAG.Got_CCSConnectorReq == TRUE)
  3068. {
  3069. Proc_CCSConnectorTypeSetRes(FD_CAN_Socket); //to-do
  3070. }
  3071. // CAN_CMD_RTC_INFO 0x00001400
  3072. if (CSUCOMMDC_TASK_FLAG.Got_RTC == TRUE)
  3073. {
  3074. Proc_RTCSetRes(FD_CAN_Socket); //to-do
  3075. }
  3076. //CAN_CMD_EVSE_PRECHARGE_INFO //0x00001500
  3077. if (CSUCOMMDC_TASK_FLAG.Got_EVSE_Precharge_Info == TRUE)
  3078. {
  3079. Proc_EVSEPrechargeInfoUpdateRes(FD_CAN_Socket);
  3080. }
  3081. //CAN_CMD_EVCCID_REQUEST //0x00001600
  3082. if (CSUCOMMDC_TASK_FLAG.Got_EVCCID_Req == TRUE)
  3083. {
  3084. Proc_EVCCIDRes(FD_CAN_Socket);
  3085. }
  3086. }
  3087. EndProcess:
  3088. if(PID_CAN_Rx_Task > 0)
  3089. {
  3090. char Buf[32];
  3091. memset(Buf, 0, 32);
  3092. sprintf(Buf, "kill %d", PID_CAN_Rx_Task);
  3093. system(Buf);
  3094. }
  3095. close(FD_CAN_Socket);
  3096. system("/sbin/ip link set can0 down");
  3097. system("/sbin/ip link set can0 type can bitrate 500000 restart-ms 100");
  3098. system("/sbin/ip link set can0 up");
  3099. system("/sbin/ip link set can0 down");
  3100. system("killall CsuComm");
  3101. }