Module_PrimaryComm.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. #include <sys/time.h>
  2. #include <sys/timeb.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <sys/types.h>
  6. #include <sys/ioctl.h>
  7. #include <sys/socket.h>
  8. #include <sys/ipc.h>
  9. #include <sys/shm.h>
  10. #include <sys/shm.h>
  11. #include <sys/mman.h>
  12. #include <linux/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h> /*標準輸入輸出定義*/
  18. #include <stdlib.h> /*標準函數庫定義*/
  19. #include <unistd.h> /*Unix 標準函數定義*/
  20. #include <fcntl.h> /*檔控制定義*/
  21. #include <termios.h> /*PPSIX 終端控制定義*/
  22. #include <errno.h> /*錯誤號定義*/
  23. #include <errno.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include <math.h>
  29. #include "../../define.h"
  30. #include "PrimaryComm.h"
  31. #include <stdbool.h>
  32. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  33. #define PASS 1
  34. #define FAIL -1
  35. #define YES 1
  36. #define NO 0
  37. #define COMM_FAIL_COUNT 10
  38. typedef unsigned char byte;
  39. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  40. struct StatusCodeData *ShmStatusCodeData;
  41. struct PrimaryMcuData *ShmPrimaryMcuData;
  42. void trim(char *s);
  43. int mystrcmp(char *p1,char *p2);
  44. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  45. void split(char **arr, char *str, const char *del);
  46. int Uart1Fd;
  47. char *priPortName = "/dev/ttyS1";
  48. Ver ver;
  49. Gpio_in gpio_in;
  50. Rtc rtc;
  51. struct timeval _flash_time;
  52. byte _OutputDrv = 0;
  53. byte _acStatus = 0;
  54. byte _acChkCount = 0;
  55. int _CommFailCount = 0;
  56. void PRINTF_FUNC(char *string, ...);
  57. int StoreLogMsg(const char *fmt, ...);
  58. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  59. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  60. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  61. int StoreLogMsg(const char *fmt, ...)
  62. {
  63. char Buf[4096+256];
  64. char buffer[4096];
  65. va_list args;
  66. struct timeb SeqEndTime;
  67. struct tm *tm;
  68. va_start(args, fmt);
  69. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  70. va_end(args);
  71. memset(Buf,0,sizeof(Buf));
  72. ftime(&SeqEndTime);
  73. SeqEndTime.time = time(NULL);
  74. tm=localtime(&SeqEndTime.time);
  75. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  76. {
  77. sprintf(Buf,"%02d:%02d:%02d:%03d - %s",
  78. tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm, buffer);
  79. printf("%s \n", Buf);
  80. }
  81. else
  82. {
  83. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  84. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  85. buffer,
  86. tm->tm_year+1900,tm->tm_mon+1);
  87. system(Buf);
  88. }
  89. return rc;
  90. }
  91. int DiffTimeb(struct timeb ST, struct timeb ET)
  92. {
  93. //return milli-second
  94. unsigned int StartTime,StopTime;
  95. StartTime=(unsigned int)ST.time;
  96. StopTime=(unsigned int)ET.time;
  97. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  98. }
  99. void PRINTF_FUNC(char *string, ...)
  100. {
  101. va_list args;
  102. char buffer[4096];
  103. va_start(args, string);
  104. vsnprintf(buffer, sizeof(buffer), string, args);
  105. va_end(args);
  106. DEBUG_INFO("%s \n", buffer);
  107. }
  108. //=================================
  109. // Common routine
  110. //=================================
  111. char* getTimeString(void)
  112. {
  113. char *result=malloc(21);
  114. time_t timep;
  115. struct tm *p;
  116. time(&timep);
  117. p=gmtime(&timep);
  118. sprintf(result, "[%04d-%02d-%02d %02d:%02d:%02d]", (1900+p->tm_year), (1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_hour, p->tm_sec);
  119. return result;
  120. }
  121. void trim(char *s)
  122. {
  123. int i=0, j, k, l=0;
  124. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  125. i++;
  126. j = strlen(s)-1;
  127. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  128. j--;
  129. if(i==0 && j==strlen(s)-1) { }
  130. else if(i==0) s[j+1] = '\0';
  131. else {
  132. for(k=i; k<=j; k++) s[l++] = s[k];
  133. s[l] = '\0';
  134. }
  135. }
  136. int mystrcmp(char *p1,char *p2)
  137. {
  138. while(*p1==*p2)
  139. {
  140. if(*p1=='\0' || *p2=='\0')
  141. break;
  142. p1++;
  143. p2++;
  144. }
  145. if(*p1=='\0' && *p2=='\0')
  146. return(PASS);
  147. else
  148. return(FAIL);
  149. }
  150. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  151. {
  152. strncpy(dest, src + start, cnt);
  153. dest[cnt] = 0;
  154. }
  155. void split(char **arr, char *str, const char *del)
  156. {
  157. char *s = strtok(str, del);
  158. while(s != NULL)
  159. {
  160. *arr++ = s;
  161. s = strtok(NULL, del);
  162. }
  163. }
  164. //==========================================
  165. // Init all share memory
  166. //==========================================
  167. int InitShareMemory()
  168. {
  169. int result = PASS;
  170. int MeterSMId;
  171. //creat ShmSysConfigAndInfo
  172. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  173. {
  174. #ifdef SystemLogMessage
  175. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  176. #endif
  177. result = FAIL;
  178. }
  179. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  180. {
  181. #ifdef SystemLogMessage
  182. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  183. #endif
  184. result = FAIL;
  185. }
  186. //creat ShmStatusCodeData
  187. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  188. {
  189. #ifdef SystemLogMessage
  190. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  191. #endif
  192. result = FAIL;
  193. }
  194. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  195. {
  196. #ifdef SystemLogMessage
  197. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  198. #endif
  199. result = FAIL;
  200. }
  201. //creat ShmStatusCodeData
  202. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
  203. {
  204. #ifdef SystemLogMessage
  205. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  206. #endif
  207. result = FAIL;
  208. }
  209. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  210. {
  211. #ifdef ShmPrimaryMcuData
  212. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  213. #endif
  214. result = FAIL;
  215. }
  216. return result;
  217. }
  218. //================================================
  219. // Function
  220. //================================================
  221. void GetFwAndHwVersion()
  222. {
  223. if(Query_FW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
  224. {
  225. PRINTF_FUNC("s1 = %s \n", ver.Version_FW);
  226. strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
  227. strcpy((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ver.Version_FW);
  228. }
  229. else
  230. {
  231. _CommFailCount++;
  232. }
  233. if (Query_HW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
  234. {
  235. PRINTF_FUNC("s2 = %s \n", ver.Version_HW);
  236. }
  237. }
  238. void GetInputGpioStatus()
  239. {
  240. //PRINTF_FUNC("GetInputGpioStatus \n");
  241. if (Query_Gpio_Input(Uart1Fd, Addr.IoExtend, &gpio_in) == PASS)
  242. {
  243. if (_acStatus != gpio_in.AC_Connector)
  244. {
  245. if (_acChkCount >= 3)
  246. {
  247. _acStatus = gpio_in.AC_Connector;
  248. // DO360 AC_Connector Status is the inverse of DS's
  249. if(gpio_in.AC_Connector)
  250. {
  251. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = 0;
  252. ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = 0;
  253. }
  254. else
  255. {
  256. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = 1;
  257. ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = 1;
  258. }
  259. PRINTF_FUNC("Ac Contactor Status %s", ShmPrimaryMcuData->InputDet.bits.AcContactorDetec > 0 ? "On" : "Off");
  260. }
  261. else
  262. _acChkCount++;
  263. }
  264. else
  265. _acChkCount = 0;
  266. ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  267. ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
  268. // DO360 Door Status is the inverse of DS's
  269. ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open > 0 ? 0 : 1;
  270. // Bypass door open
  271. //ShmPrimaryMcuData->InputDet.bits.DoorOpen = 0;
  272. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  273. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  274. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = gpio_in.Emergency_Btn;
  275. //PRINTF_FUNC("left = %d \n", ShmPrimaryMcuData->InputDet.bits.Button1);
  276. //PRINTF_FUNC("right = %d \n", ShmPrimaryMcuData->InputDet.bits.Button2);
  277. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.AcContactorStatus = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  278. //if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == YES)
  279. // DEBUG_ERROR("AC Mainbreaker occur. \n");
  280. }
  281. }
  282. void SetOutputGpio(byte flash)
  283. {
  284. Gpio_out gpio;
  285. if(_OutputDrv != flash)
  286. {
  287. gpio.Button_LED[0] = (flash & 0x01) > 0 ? 1 : 0;
  288. gpio.Button_LED[1] = (flash & 0x02) > 0 ? 1 : 0;
  289. gpio.System_LED[0] = (flash & 0x04) > 0 ? 1 : 0;
  290. gpio.System_LED[1] = (flash & 0x08) > 0 ? 1 : 0;
  291. gpio.System_LED[2] = (flash & 0x10) > 0 ? 1 : 0;
  292. gpio.System_LED[3] = (flash & 0x20) > 0 ? 1 : 0;
  293. gpio.AC_Connector = (flash & 0x40) > 0 ? 1 : 0;
  294. gpio.AC_Breaker = (flash & 0x80) > 0 ? 1 : 0;
  295. if (Config_Gpio_Output(Uart1Fd, Addr.IoExtend, &gpio) == PASS)
  296. {
  297. _OutputDrv = flash;
  298. }
  299. }
  300. else
  301. {
  302. // do nothing when no change
  303. }
  304. }
  305. void SetRtcData()
  306. {
  307. struct timeb csuTime;
  308. struct tm *tmCSU;
  309. ftime(&csuTime);
  310. tmCSU = localtime(&csuTime.time);
  311. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  312. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  313. // tmCSU->tm_sec);
  314. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  315. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  316. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  317. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  318. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  319. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  320. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  321. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  322. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  323. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  324. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  325. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  326. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  327. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  328. if (Config_Rtc_Data(Uart1Fd, Addr.IoExtend, &rtc) == PASS)
  329. {
  330. //PRINTF_FUNC("SetRtc sucessfully. \n");
  331. }
  332. else
  333. {
  334. //PRINTF_FUNC("SetRtc fail. \n");
  335. }
  336. }
  337. //================================================
  338. // Main process
  339. //================================================
  340. int InitComPort()
  341. {
  342. int fd;
  343. struct termios tios;
  344. fd = open(priPortName, O_RDWR);
  345. if(fd<=0)
  346. {
  347. #ifdef SystemLogMessage
  348. DEBUG_ERROR("open 407 Communication port NG \n");
  349. #endif
  350. return -1;
  351. }
  352. ioctl (fd, TCGETS, &tios);
  353. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  354. tios.c_lflag = 0;
  355. tios.c_iflag = 0;
  356. tios.c_oflag = 0;
  357. tios.c_cc[VMIN]=0;
  358. tios.c_cc[VTIME]=(unsigned char)1;
  359. tios.c_lflag=0;
  360. tcflush(fd, TCIFLUSH);
  361. ioctl (fd, TCSETS, &tios);
  362. return fd;
  363. }
  364. unsigned long GetTimeoutValue(struct timeval _sour_time)
  365. {
  366. struct timeval _end_time;
  367. gettimeofday(&_end_time, NULL);
  368. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  369. }
  370. int main(void)
  371. {
  372. if(InitShareMemory() == FAIL)
  373. {
  374. #ifdef SystemLogMessage
  375. DEBUG_ERROR("InitShareMemory NG\n");
  376. #endif
  377. if(ShmStatusCodeData!=NULL)
  378. {
  379. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  380. }
  381. sleep(5);
  382. return 0;
  383. }
  384. Uart1Fd = InitComPort();
  385. PRINTF_FUNC("407 Port id = %d \n", Uart1Fd);
  386. if(Uart1Fd < 0)
  387. {
  388. #ifdef SystemLogMessage
  389. DEBUG_ERROR("InitComPort (Uart1 : AM3352 - STM32) NG");
  390. #endif
  391. if (ShmStatusCodeData != NULL)
  392. {
  393. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  394. }
  395. sleep(5);
  396. return 0;
  397. }
  398. SetRtcData();
  399. gettimeofday(&_flash_time, NULL);
  400. // update ac contact status
  401. //_acStatus = ShmSysConfigAndInfo->SysInfo.AcContactorStatus;
  402. _acStatus = ShmSysConfigAndInfo->SysInfo.AcContactorStatus > 0 ? 0 : 1;
  403. // set wrong value, it will update at the first time
  404. _OutputDrv = ~ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0];
  405. for(;;)
  406. {
  407. if(!ShmSysConfigAndInfo->SysInfo.FirmwareUpdate)
  408. {
  409. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  410. // 模組更新 FW 後,需重新做
  411. if(ShmPrimaryMcuData->SelfTest_Comp != PASS)
  412. {
  413. memset(ShmPrimaryMcuData->version, 0x00, 16);
  414. GetFwAndHwVersion();
  415. sleep(1);
  416. if(strlen((char *)ShmPrimaryMcuData->version) != 0)
  417. {
  418. ShmPrimaryMcuData->SelfTest_Comp = YES;
  419. }
  420. if(_CommFailCount >= COMM_FAIL_COUNT)
  421. {
  422. PRINTF_FUNC("Primary MCU Communication Fail: %d", _CommFailCount);
  423. _CommFailCount = 0;
  424. }
  425. }
  426. else
  427. {
  428. GetInputGpioStatus();
  429. //PRINTF_FUNC("Input Status: %02X %02X", ShmPrimaryMcuData->InputDet.InputDetValue[1], ShmPrimaryMcuData->InputDet.InputDetValue[0]);
  430. SetOutputGpio(ShmPrimaryMcuData->OutputDrv.OutputDrvValue[0]);
  431. }
  432. }
  433. usleep(100000);
  434. }
  435. return FAIL;
  436. }