main.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*===========================================================================
  2. Combined Charging System (CCS): SECC
  3. main.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 <sys/socket.h>
  21. #include <netinet/in.h>
  22. #include <sys/time.h>
  23. #include <sys/timeb.h>
  24. #include <math.h>//for pow
  25. #include <unistd.h>
  26. #include "define.h"
  27. #include "main.h"
  28. #define Debug
  29. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  30. struct StatusCodeData *ShmStatusCodeData;
  31. struct CcsData *ShmCcsData;
  32. struct InternalComm *ShmInternalComm;
  33. #ifdef SystemLogMessage
  34. int StoreLogMsg(unsigned char *DataString);
  35. #endif
  36. void System_Init();
  37. void CreateShareMemory_Init();
  38. void GPIO_Init();
  39. void PWM_Init();
  40. void Confinguration_Init();
  41. int DiffTimeb(struct timeb ST, struct timeb ET);
  42. int CreatShareMemory();
  43. int LoadSysConfigAndInfo(struct SysConfigData *ptr);
  44. int SpawnTask();
  45. int StoreUsrConfigData(struct SysConfigData *UsrData);
  46. /*===========================================================================
  47. FUNCTION: main
  48. DESCRIPTION:
  49. 1. This main routine will be executed by the "rcS" when linux is booting up.
  50. /opt/ti-processor-sdk-linux-am335x-evm-04.02.00.09/EVSE/rootfs/etc/init.d/rcS
  51. PRE-CONDITION:
  52. INPUT:
  53. OUTPUT:
  54. GLOBAL VARIABLES:
  55. =============================================================================*/
  56. int main(int argc, char *argv[])
  57. {
  58. //int Rtn = 0;
  59. //unsigned int StartTime;
  60. //struct timeb StartChargingTime, CurrentChargingTime, ChargingLoopTime;
  61. //float ChargingPower, ChargingEnergy;
  62. //CSU Initialization
  63. System_Init();
  64. //task spawn
  65. //SpawnTask();
  66. /**************************************************************/
  67. /************** main Loop********************************/
  68. /*****************************************************************/
  69. while(1)
  70. {
  71. sleep(5);
  72. }//main while
  73. }
  74. /*===========================================================================
  75. FUNCTION: System_Init
  76. DESCRIPTION:
  77. PRE-CONDITION:
  78. 1. Pin Assignment Configuration
  79. board-support/linux-4.9.59+gitAUTOINC+a75d8e9305-ga75d8e9305/
  80. arch/arm/boot/dts/[CCS]am335x-evm.dts
  81. INPUT:
  82. OUTPUT:
  83. GLOBAL VARIABLES:
  84. =============================================================================*/
  85. void System_Init()
  86. {
  87. // ---------- Hardware Init ----------
  88. //Init GPIO
  89. GPIO_Init();
  90. //Init ecap0
  91. PWM_Init();
  92. // ---------- Software Init ----------
  93. //Create all share memory
  94. CreateShareMemory_Init();
  95. //Init System Confinguration
  96. Confinguration_Init();
  97. #ifdef SystemLogMessage
  98. StoreLogMsg("--------------------------");
  99. StoreLogMsg("[main] System_Init(): DONE");
  100. #endif
  101. }
  102. /*===========================================================================
  103. FUNCTION: CreateShareMemory_Init
  104. DESCRIPTION:
  105. PRE-CONDITION:
  106. INPUT:
  107. OUTPUT:
  108. GLOBAL VARIABLES:
  109. =============================================================================*/
  110. void CreateShareMemory_Init()
  111. {
  112. if(CreatShareMemory() == 0)
  113. {
  114. #ifdef SystemLogMessage
  115. StoreLogMsg("[main] CreateShareMemory_Init(): NG");
  116. #endif
  117. if(ShmStatusCodeData != NULL)
  118. {
  119. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  120. }
  121. sleep(5);
  122. system("reboot -f");
  123. sleep(5);
  124. system("reboot -f");
  125. }
  126. else
  127. {
  128. #ifdef SystemLogMessage
  129. StoreLogMsg("[main] CreateShareMemory_Init(): DONE");
  130. #endif
  131. }
  132. }
  133. /**************************************************************************************/
  134. /**************************Create all share memory *********************************/
  135. /**************************************************************************************/
  136. /*===========================================================================
  137. FUNCTION: CreatShareMemory
  138. DESCRIPTION:
  139. There are 4 share memory created here, which are listed as below.
  140. 1) ShmSysConfigAndInfo
  141. 2) ShmStatusCodeData
  142. 3) ShmCcsData
  143. 4) ShmInternalComm
  144. PRE-CONDITION:
  145. INPUT:
  146. OUTPUT:
  147. GLOBAL VARIABLES:
  148. =============================================================================*/
  149. int CreatShareMemory()
  150. {
  151. int MeterSMId;
  152. //creat ShmSysConfigAndInfo
  153. if((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), IPC_CREAT | 0777)) < 0)
  154. {
  155. #ifdef SystemLogMessage
  156. StoreLogMsg("[main]CreatShareMemory:shmget ShmSysConfigAndInfo NG");
  157. #endif
  158. return 0;
  159. }
  160. else if((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  161. {
  162. #ifdef SystemLogMessage
  163. StoreLogMsg("[main]CreatShareMemory:shmat ShmSysConfigAndInfo NG");
  164. #endif
  165. return 0;
  166. }
  167. memset(ShmSysConfigAndInfo, 0, sizeof(struct SysConfigAndInfo));
  168. //creat ShmStatusCodeData
  169. if((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), IPC_CREAT | 0777)) < 0)
  170. {
  171. #ifdef SystemLogMessage
  172. StoreLogMsg("[main]CreatShareMemory:shmget ShmStatusCodeData NG");
  173. #endif
  174. return 0;
  175. }
  176. else if((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  177. {
  178. #ifdef SystemLogMessage
  179. StoreLogMsg("[main]CreatShareMemory:shmat ShmStatusCodeData NG");
  180. #endif
  181. return 0;
  182. }
  183. memset(ShmStatusCodeData, 0, sizeof(struct StatusCodeData));
  184. //creat ShmCcsData
  185. if((MeterSMId = shmget(ShmCcsCommKey, sizeof(struct CcsData), IPC_CREAT | 0777)) < 0)
  186. {
  187. #ifdef SystemLogMessage
  188. StoreLogMsg("[main]CreatShareMemory:shmget ShmCcsData NG");
  189. #endif
  190. return 0;
  191. }
  192. else if((ShmCcsData = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  193. {
  194. #ifdef SystemLogMessage
  195. StoreLogMsg("[main]CreatShareMemory:shmat ShmCcsData NG");
  196. #endif
  197. return 0;
  198. }
  199. memset(ShmCcsData, 0, sizeof(struct CcsData));
  200. //creat ShmInternalComm
  201. if((MeterSMId = shmget(ShmInternalCommKey, sizeof(struct InternalComm), IPC_CREAT | 0777)) < 0)
  202. {
  203. #ifdef SystemLogMessage
  204. StoreLogMsg("[main]CreatShareMemory:shmget ShmInternalComm NG");
  205. #endif
  206. return 0;
  207. }
  208. else if((ShmInternalComm = shmat(MeterSMId, NULL, 0)) == (void *)-1)
  209. {
  210. #ifdef SystemLogMessage
  211. StoreLogMsg("[main]CreatShareMemory:shmat ShmInternalComm NG");
  212. #endif
  213. return 0;
  214. }
  215. return 1;
  216. }
  217. /*===========================================================================
  218. FUNCTION: GPIO_Init
  219. DESCRIPTION:
  220. 1. GPIO (input) x 1
  221. * gpio2_0(gpio64): QCA7000 interrupt
  222. 2. GPIO (output) x 4
  223. * gpio2_25 (gpio89): reserved (default = 0)
  224. * gpio2_23 (gpio87): reserved (default = 0)
  225. * gpio2_24 (gpio88): power reset QCA7000 (default = 0)
  226. * gpio2_22 (gpio86): Pilot Status E control (0V) (default = 0)
  227. PRE-CONDITION:
  228. INPUT:
  229. OUTPUT:
  230. GLOBAL VARIABLES:
  231. =============================================================================*/
  232. void GPIO_Init()
  233. {
  234. /*****************0~3, 4 bank, bank x 32+ num*********************/
  235. /*****************************************************************/
  236. /*************** INPUT PIN ***************************************/
  237. /*****************************************************************/
  238. /*GPIO2_0: QCA7000 interrupt */
  239. system("echo 64 > /sys/class/gpio/export");
  240. system("echo \"in\" > /sys/class/gpio/gpio64/direction");
  241. /***************************************************************/
  242. /*************** OUTPUT PIN ************************************/
  243. /***************************************************************/
  244. //IO port for Communication Board.(reserved for emergency shutdown PSU, i.e. PSKILL)
  245. /*GPIO2_25: AM_IO_2, H: ?; L: ?*/
  246. system("echo 89 > /sys/class/gpio/export");
  247. system("echo \"out\" > /sys/class/gpio/gpio89/direction");
  248. system("echo 0 > /sys/class/gpio/gpio89/value");
  249. //IO port for Communication Board.(reserved for emergency shutdown PSU, i.e. PSKILL)
  250. /*GPIO2_23: AM_IO_1, H: ?; L: ?*/
  251. system("echo 87 > /sys/class/gpio/export");
  252. system("echo \"out\" > /sys/class/gpio/gpio87/direction");
  253. system("echo 0 > /sys/class/gpio/gpio87/value");
  254. //Power Reset QCA7000
  255. /*GPIO2_24: AM_QCA_PWR_RST, H: reset, L: noraml*/
  256. system("echo 88 > /sys/class/gpio/export");
  257. system("echo \"out\" > /sys/class/gpio/gpio88/direction");
  258. system("echo 0 > /sys/class/gpio/gpio88/value");
  259. //Pilot Status E control (0V)
  260. /*GPIO2_22: Pilot_state E, H: state E, L: normal*/
  261. system("echo 86 > /sys/class/gpio/export");
  262. system("echo \"out\" > /sys/class/gpio/gpio86/direction");
  263. system("echo 0 > /sys/class/gpio/gpio86/value");
  264. #ifdef SystemLogMessage
  265. StoreLogMsg("[main] GPIO_Init(): DONE");
  266. #endif
  267. }
  268. /*===========================================================================
  269. FUNCTION: PWM_Init
  270. DESCRIPTION:
  271. - Initialize the PWM channel by the following configuration.
  272. * Frequency = 1KHz
  273. * Duty = 100%
  274. - The default voltage should be as below.
  275. * 12V: unplugged
  276. * 9V: plugged
  277. - Check Mechanism (to-do)
  278. * Read CP Voltage and PWM by AIN3:
  279. // cat /sys/bus/iio/devices/iio\\:device0/in_voltage3_raw;
  280. PRE-CONDITION:
  281. INPUT:
  282. OUTPUT:
  283. - PWM = 100% at 1KHz
  284. GLOBAL VARIABLES:
  285. =============================================================================*/
  286. void PWM_Init()
  287. {
  288. //unsigned char str[256];
  289. //init ecap0
  290. //memset(str, 0, sizeof(str));
  291. system("echo 0 > /sys/class/pwm/pwmchip0/export");
  292. system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/period");// nano seconds =>1k Hz
  293. system("echo 1000000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle"); //default 100%
  294. system("echo 1 > /sys/class/pwm/pwmchip0/pwm0/enable");
  295. //system("echo 1 > /sys /class/pwm/pwmchip0/pwm0/polarity");
  296. //PWM_Init_Check(); //Checking if the PWM analog signal is well-initialized
  297. //as 1KHz,100%, and 12V/9V.
  298. #ifdef SystemLogMessage
  299. StoreLogMsg("[main] PWM_Init(): DONE");
  300. #endif
  301. }
  302. /*===========================================================================
  303. FUNCTION:
  304. DESCRIPTION:
  305. PRE-CONDITION:
  306. INPUT:
  307. OUTPUT:
  308. GLOBAL VARIABLES:
  309. =============================================================================*/
  310. void Confinguration_Init()
  311. {
  312. //Load System Confinguration
  313. LoadSysConfigAndInfo(&ShmSysConfigAndInfo->SysConfig);
  314. #ifdef SystemLogMessage
  315. StoreLogMsg("[main] Confinguration_Init(): DONE");
  316. #endif
  317. }
  318. /*===========================================================================
  319. FUNCTION:
  320. DESCRIPTION:
  321. PRE-CONDITION:
  322. INPUT:
  323. OUTPUT:
  324. GLOBAL VARIABLES:
  325. =============================================================================*/
  326. #ifdef SystemLogMessage
  327. int StoreLogMsg(unsigned char *DataString)
  328. {
  329. unsigned char Buf[256];
  330. time_t CurrentTime;
  331. struct tm *tm;
  332. struct timeval tv;
  333. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  334. //Get uptime in nano-second precision
  335. //struct timespec elapsed_from_boot;
  336. //clock_gettime(CLOCK_BOOTTIME, &elapsed_from_boot);
  337. //Need GLIBC_2.17
  338. memset(Buf, 0, sizeof(Buf));
  339. CurrentTime = time(NULL);
  340. tm = localtime(&CurrentTime);
  341. sprintf(Buf, "echo \"[%04d.%02d.%02d-%02d:%02d:%02d.%06d] @ %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  342. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec,
  343. DataString,
  344. tm->tm_year + 1900, tm->tm_mon + 1);
  345. system(Buf);
  346. #ifdef Debug
  347. printf("[%04d.%02d.%02d-%02d:%02d:%02d.%06d] @ %s \n",
  348. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, tv.tv_usec,
  349. DataString);
  350. #endif
  351. }
  352. #endif
  353. /*===========================================================================
  354. FUNCTION: DiffTimeb
  355. DESCRIPTION:
  356. Caculating the time difference in ms precision (milli-second).
  357. PRE-CONDITION:
  358. INPUT:
  359. OUTPUT:
  360. GLOBAL VARIABLES:
  361. =============================================================================*/
  362. int DiffTimeb(struct timeb ST, struct timeb ET)
  363. {
  364. //return milli-second
  365. unsigned int StartTime, StopTime;
  366. StartTime = (unsigned int)ST.time;
  367. StopTime = (unsigned int)ET.time;
  368. return (StopTime - StartTime) * 1000 + ET.millitm - ST.millitm;
  369. }
  370. /**************************************************************************************/
  371. /****************Following functions are CSU initialization***************************/
  372. /**************************************************************************************/
  373. /*===========================================================================
  374. FUNCTION:
  375. DESCRIPTION:
  376. PRE-CONDITION:
  377. INPUT:
  378. OUTPUT:
  379. GLOBAL VARIABLES:
  380. =============================================================================*/
  381. int SpawnTask()
  382. {
  383. system("/root/EventLogging &");
  384. system("/root/CsuComm &");
  385. system("/root/EvComm &");
  386. }
  387. /*===========================================================================
  388. FUNCTION:
  389. DESCRIPTION:
  390. PRE-CONDITION:
  391. INPUT:
  392. OUTPUT:
  393. GLOBAL VARIABLES:
  394. =============================================================================*/
  395. int StoreUsrConfigData(struct SysConfigData *UsrData)
  396. {
  397. int fd, wrd;
  398. unsigned int i, Chk;
  399. unsigned char *ptr;
  400. Chk = 0;
  401. ptr = (unsigned char *)UsrData;
  402. for(i = 0; i < sizeof(struct SysConfigData) - 4; i++)
  403. {
  404. Chk += *(ptr + i);
  405. }
  406. UsrData->Checksum = Chk;
  407. fd = open("/dev/mtdblock10", O_RDWR);
  408. if(fd < 0)
  409. {
  410. #ifdef SystemLogMessage
  411. StoreLogMsg("[main]StoreUsrConfigData: open /dev/mtdblock10 NG");
  412. #endif
  413. return 0;
  414. }
  415. wrd = write(fd, UsrData, sizeof(struct SysConfigData));
  416. close(fd);
  417. if(wrd != (sizeof(struct SysConfigData)))
  418. {
  419. #ifdef SystemLogMessage
  420. StoreLogMsg("[main]StoreUsrConfigData: write /dev/mtdblock10 NG");
  421. #endif
  422. return 0;
  423. }
  424. fd = open("/dev/mtdblock11", O_RDWR);
  425. if(fd < 0)
  426. {
  427. #ifdef SystemLogMessage
  428. StoreLogMsg("[main]StoreUsrConfigData: open /dev/mtdblock11(backup) NG");
  429. #endif
  430. return 0;
  431. }
  432. wrd = write(fd, UsrData, sizeof(struct SysConfigData));
  433. close(fd);
  434. if(wrd != (sizeof(struct SysConfigData)))
  435. {
  436. #ifdef SystemLogMessage
  437. StoreLogMsg("[main]StoreUsrConfigData: write /dev/mtdblock11(backup) NG");
  438. #endif
  439. return 0;
  440. }
  441. return 1;
  442. }
  443. /*===========================================================================
  444. FUNCTION: LoadSysConfigAndInfo
  445. DESCRIPTION:
  446. PRE-CONDITION:
  447. INPUT:
  448. OUTPUT:
  449. GLOBAL VARIABLES:
  450. =============================================================================*/
  451. int LoadSysConfigAndInfo(struct SysConfigData *ptr)
  452. {
  453. int fd, wrd;
  454. struct SysConfigData *buf;
  455. unsigned char *PtrBuf;
  456. unsigned int ChkSum, ChkSumOrg;
  457. if((buf = malloc(sizeof(struct SysConfigData))) == NULL)
  458. {
  459. #ifdef SystemLogMessage
  460. StoreLogMsg("[main] LoadSysConfigAndInfo(): malloc buffer NG,rebooting..");
  461. #endif
  462. if(ShmStatusCodeData != NULL)
  463. {
  464. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  465. }
  466. sleep(5);
  467. system("reboot -f");
  468. sleep(5);
  469. system("reboot -f");
  470. }
  471. memset(buf, 0, sizeof(struct SysConfigData));
  472. fd = open("/dev/mtdblock10", O_RDWR);
  473. if(fd < 0)
  474. {
  475. free(buf);
  476. #ifdef SystemLogMessage
  477. StoreLogMsg("[main] LoadSysConfigAndInfo():open mtdblock10 NG,rebooting..");
  478. #endif
  479. if(ShmStatusCodeData != NULL)
  480. {
  481. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  482. }
  483. sleep(5);
  484. system("reboot -f");
  485. sleep(5);
  486. system("reboot -f");
  487. }
  488. wrd = read(fd, buf, sizeof(struct SysConfigData));
  489. close(fd);
  490. if(wrd != (sizeof(struct SysConfigData)))
  491. {
  492. free(buf);
  493. #ifdef SystemLogMessage
  494. StoreLogMsg("[main] LoadSysConfigAndInfo(): read SysConfigData data NG,rebooting..");
  495. #endif
  496. if(ShmStatusCodeData != NULL)
  497. {
  498. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  499. }
  500. sleep(5);
  501. system("reboot -f");
  502. sleep(5);
  503. system("reboot -f");
  504. }
  505. PtrBuf = (unsigned char *)buf;
  506. ChkSum = 0;
  507. for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  508. {
  509. ChkSum += PtrBuf[wrd];
  510. }
  511. ChkSumOrg = buf->Checksum;
  512. if(ChkSum != ChkSumOrg)
  513. {
  514. #ifdef SystemLogMessage
  515. StoreLogMsg("[main] LoadSysConfigAndInfo(): Primary SysConfigData checksum NG, read backup");
  516. #endif
  517. fd = open("/dev/mtdblock11", O_RDWR);
  518. if(fd < 0)
  519. {
  520. free(buf);
  521. #ifdef SystemLogMessage
  522. StoreLogMsg("[main] LoadSysConfigAndInfo(): open mtdblock11 (backup) NG,rebooting..");
  523. #endif
  524. if(ShmStatusCodeData != NULL)
  525. {
  526. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  527. }
  528. sleep(5);
  529. system("reboot -f");
  530. sleep(5);
  531. system("reboot -f");
  532. }
  533. memset(buf, 0, sizeof(struct SysConfigData));
  534. wrd = read(fd, buf, sizeof(struct SysConfigData));
  535. close(fd);
  536. if(wrd != sizeof(struct SysConfigData))
  537. {
  538. free(buf);
  539. #ifdef SystemLogMessage
  540. StoreLogMsg("[main] LoadSysConfigAndInfo(): read backup SysConfigData data NG,rebooting..");
  541. #endif
  542. if(ShmStatusCodeData != NULL)
  543. {
  544. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  545. }
  546. sleep(5);
  547. system("reboot -f");
  548. sleep(5);
  549. system("reboot -f");
  550. }
  551. PtrBuf = (unsigned char *)buf;
  552. ChkSum = 0;
  553. for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  554. {
  555. ChkSum += PtrBuf[wrd];
  556. }
  557. ChkSumOrg = buf->Checksum;
  558. if(ChkSum != ChkSumOrg)
  559. {
  560. #ifdef SystemLogMessage
  561. StoreLogMsg("[main] LoadSysConfigAndInfo(): backup SysConfigData checksum NG, read Factory default");
  562. #endif
  563. fd = open("/dev/mtdblock12", O_RDWR);
  564. if(fd < 0)
  565. {
  566. free(buf);
  567. #ifdef SystemLogMessage
  568. StoreLogMsg("[main] LoadSysConfigAndInfo(): open mtdblock12 (Factory default) NG,rebooting..");
  569. #endif
  570. if(ShmStatusCodeData != NULL)
  571. {
  572. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  573. }
  574. sleep(5);
  575. system("reboot -f");
  576. sleep(5);
  577. system("reboot -f");
  578. }
  579. memset(buf, 0, sizeof(struct SysConfigData));
  580. wrd = read(fd, buf, sizeof(struct SysConfigData));
  581. close(fd);
  582. if(wrd != sizeof(struct SysConfigData))
  583. {
  584. free(buf);
  585. #ifdef SystemLogMessage
  586. StoreLogMsg("[main] LoadSysConfigAndInfo(): read factory default SysConfigData data NG,rebooting..");
  587. #endif
  588. if(ShmStatusCodeData != NULL)
  589. {
  590. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  591. }
  592. sleep(5);
  593. system("reboot -f");
  594. sleep(5);
  595. system("reboot -f");
  596. }
  597. PtrBuf = (unsigned char *)buf;
  598. ChkSum = 0;
  599. for(wrd = 0; wrd < (sizeof(struct SysConfigData) - 4); wrd++)
  600. {
  601. ChkSum += PtrBuf[wrd];
  602. }
  603. ChkSumOrg = buf->Checksum;
  604. if(ChkSum != ChkSumOrg)
  605. {
  606. #ifdef SystemLogMessage
  607. StoreLogMsg("[main] LoadSysConfigAndInfo(): factory default SysConfigData checksum NG, restore factory default");
  608. #endif
  609. goto DefaultShm;
  610. }
  611. }
  612. }
  613. //load OK
  614. memcpy((struct SysConfigData *)ptr, (struct SysConfigData *)buf, sizeof(struct SysConfigData));
  615. free(buf);
  616. #ifdef SystemLogMessage
  617. StoreLogMsg("[main]LoadSysConfigAndInfo(): Load SysConfigData OK");
  618. #endif
  619. return 1;
  620. DefaultShm:
  621. system("cd /root;./FactoryConfig");
  622. system("sync");
  623. sleep(5);
  624. system("reboot -f");
  625. sleep(5);
  626. system("reboot -f");
  627. }