Module_PrimaryComm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  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. typedef unsigned char byte;
  38. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  39. struct StatusCodeData *ShmStatusCodeData;
  40. struct PrimaryMcuData *ShmPrimaryMcuData;
  41. void trim(char *s);
  42. int mystrcmp(char *p1,char *p2);
  43. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  44. void split(char **arr, char *str, const char *del);
  45. int Uart1Fd;
  46. char *priPortName = "/dev/ttyS1";
  47. Ver ver;
  48. Gpio_in gpio_in;
  49. Rtc rtc;
  50. struct timeval _flash_time;
  51. byte flash = NO;
  52. void PRINTF_FUNC(char *string, ...);
  53. int StoreLogMsg(const char *fmt, ...);
  54. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  55. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  56. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  57. int StoreLogMsg(const char *fmt, ...)
  58. {
  59. char Buf[4096+256];
  60. char buffer[4096];
  61. va_list args;
  62. struct timeb SeqEndTime;
  63. struct tm *tm;
  64. va_start(args, fmt);
  65. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  66. va_end(args);
  67. memset(Buf,0,sizeof(Buf));
  68. ftime(&SeqEndTime);
  69. SeqEndTime.time = time(NULL);
  70. tm=localtime(&SeqEndTime.time);
  71. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d.%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  72. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  73. buffer,
  74. tm->tm_year+1900,tm->tm_mon+1);
  75. system(Buf);
  76. return rc;
  77. }
  78. int DiffTimeb(struct timeb ST, struct timeb ET)
  79. {
  80. //return milli-second
  81. unsigned int StartTime,StopTime;
  82. StartTime=(unsigned int)ST.time;
  83. StopTime=(unsigned int)ET.time;
  84. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  85. }
  86. void PRINTF_FUNC(char *string, ...)
  87. {
  88. va_list args;
  89. char buffer[4096];
  90. va_start(args, string);
  91. vsnprintf(buffer, sizeof(buffer), string, args);
  92. va_end(args);
  93. if (ShmSysConfigAndInfo->SysConfig.SwitchDebugFlag == YES)
  94. printf("%s \n", buffer);
  95. else
  96. DEBUG_INFO("%s \n", buffer);
  97. }
  98. //=================================
  99. // Common routine
  100. //=================================
  101. char* getTimeString(void)
  102. {
  103. char *result=malloc(21);
  104. time_t timep;
  105. struct tm *p;
  106. time(&timep);
  107. p=gmtime(&timep);
  108. 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);
  109. return result;
  110. }
  111. void trim(char *s)
  112. {
  113. int i=0, j, k, l=0;
  114. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  115. i++;
  116. j = strlen(s)-1;
  117. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  118. j--;
  119. if(i==0 && j==strlen(s)-1) { }
  120. else if(i==0) s[j+1] = '\0';
  121. else {
  122. for(k=i; k<=j; k++) s[l++] = s[k];
  123. s[l] = '\0';
  124. }
  125. }
  126. int mystrcmp(char *p1,char *p2)
  127. {
  128. while(*p1==*p2)
  129. {
  130. if(*p1=='\0' || *p2=='\0')
  131. break;
  132. p1++;
  133. p2++;
  134. }
  135. if(*p1=='\0' && *p2=='\0')
  136. return(PASS);
  137. else
  138. return(FAIL);
  139. }
  140. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  141. {
  142. strncpy(dest, src + start, cnt);
  143. dest[cnt] = 0;
  144. }
  145. void split(char **arr, char *str, const char *del)
  146. {
  147. char *s = strtok(str, del);
  148. while(s != NULL)
  149. {
  150. *arr++ = s;
  151. s = strtok(NULL, del);
  152. }
  153. }
  154. //==========================================
  155. // Init all share memory
  156. //==========================================
  157. int InitShareMemory()
  158. {
  159. int result = PASS;
  160. int MeterSMId;
  161. //creat ShmSysConfigAndInfo
  162. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  163. {
  164. #ifdef SystemLogMessage
  165. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  166. #endif
  167. result = FAIL;
  168. }
  169. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  170. {
  171. #ifdef SystemLogMessage
  172. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  173. #endif
  174. result = FAIL;
  175. }
  176. //creat ShmStatusCodeData
  177. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  178. {
  179. #ifdef SystemLogMessage
  180. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  181. #endif
  182. result = FAIL;
  183. }
  184. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  185. {
  186. #ifdef SystemLogMessage
  187. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  188. #endif
  189. result = FAIL;
  190. }
  191. //creat ShmStatusCodeData
  192. if ((MeterSMId = shmget(ShmPrimaryMcuKey, sizeof(struct PrimaryMcuData), 0777)) < 0)
  193. {
  194. #ifdef SystemLogMessage
  195. DEBUG_ERROR("shmget ShmPrimaryMcuData NG\n");
  196. #endif
  197. result = FAIL;
  198. }
  199. else if ((ShmPrimaryMcuData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  200. {
  201. #ifdef ShmPrimaryMcuData
  202. DEBUG_ERROR("shmat ShmPrimaryMcuData NG\n");
  203. #endif
  204. result = FAIL;
  205. }
  206. return result;
  207. }
  208. //================================================
  209. // Function
  210. //================================================
  211. void GetFwAndHwVersion()
  212. {
  213. if(Query_FW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
  214. {
  215. PRINTF_FUNC("s1 = %s \n", ver.Version_FW);
  216. strcpy((char *)ShmPrimaryMcuData->version, ver.Version_FW);
  217. strcpy((char *) ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ver.Version_FW);
  218. }
  219. if (Query_HW_Ver(Uart1Fd, Addr.IoExtend, &ver) == PASS)
  220. PRINTF_FUNC("s2 = %s \n", ver.Version_HW);
  221. }
  222. void GetInputGpioStatus()
  223. {
  224. //PRINTF_FUNC("GetInputGpioStatus \n");
  225. if (Query_Gpio_Input(Uart1Fd, Addr.IoExtend, &gpio_in) == PASS)
  226. {
  227. ShmSysConfigAndInfo->SysInfo.AcContactorStatus = ShmPrimaryMcuData->InputDet.bits.AcContactorDetec = gpio_in.AC_Connector;
  228. ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec = gpio_in.AC_MainBreaker;
  229. ShmPrimaryMcuData->InputDet.bits.SpdDetec = gpio_in.SPD;
  230. ShmPrimaryMcuData->InputDet.bits.DoorOpen = gpio_in.Door_Open;
  231. ShmPrimaryMcuData->InputDet.bits.Button1 = gpio_in.Button[0];
  232. ShmPrimaryMcuData->InputDet.bits.Button2 = gpio_in.Button[1];
  233. ShmPrimaryMcuData->InputDet.bits.EmergencyButton = gpio_in.Emergency_Btn;
  234. //PRINTF_FUNC("left = %d \n", ShmPrimaryMcuData->InputDet.bits.Button1);
  235. //PRINTF_FUNC("right = %d \n", ShmPrimaryMcuData->InputDet.bits.Button2);
  236. //PRINTF_FUNC("ShmSysConfigAndInfo->SysInfo.AcContactorStatus = %d \n", ShmSysConfigAndInfo->SysInfo.AcContactorStatus);
  237. if (ShmPrimaryMcuData->InputDet.bits.AcMainBreakerDetec == YES)
  238. DEBUG_ERROR("AC Mainbreaker occur. \n");
  239. }
  240. }
  241. void SetOutputGpio(byte flash)
  242. {
  243. Gpio_out gpio;
  244. gpio.Button_LED[0] = flash;
  245. gpio.Button_LED[1] = flash;
  246. gpio.System_LED[0] = 0x00;
  247. gpio.System_LED[1] = 0x00;
  248. gpio.System_LED[2] = 0x00;
  249. gpio.System_LED[3] = 0x00;
  250. gpio.AC_Connector = 0x00;
  251. gpio.AC_Breaker = 0x00;
  252. if (Config_Gpio_Output(Uart1Fd, Addr.IoExtend, &gpio) == PASS)
  253. {
  254. //PRINTF_FUNC("SetOutputGpio sucessfully. %d \n", flash);
  255. }
  256. else
  257. {
  258. //PRINTF_FUNC("SetOutputGpio fail. \n");
  259. }
  260. }
  261. void SetRtcData()
  262. {
  263. struct timeb csuTime;
  264. struct tm *tmCSU;
  265. ftime(&csuTime);
  266. tmCSU = localtime(&csuTime.time);
  267. // PRINTF_FUNC("Time : %04d-%02d-%02d %02d:%02d:%02d \n", tmCSU->tm_year + 1900,
  268. // tmCSU->tm_mon + 1, tmCSU->tm_mday, tmCSU->tm_hour, tmCSU->tm_min,
  269. // tmCSU->tm_sec);
  270. rtc.RtcData[0] = '0' + (tmCSU->tm_year + 1900) / 1000 % 10;
  271. rtc.RtcData[1] = '0' + (tmCSU->tm_year + 1900) / 100 % 10;
  272. rtc.RtcData[2] = '0' + (tmCSU->tm_year + 1900) / 10 % 10;
  273. rtc.RtcData[3] = '0' + (tmCSU->tm_year + 1900) / 1 % 10;
  274. rtc.RtcData[4] = '0' + (tmCSU->tm_mon + 1) / 10 % 10;
  275. rtc.RtcData[5] = '0' + (tmCSU->tm_mon + 1) / 1 % 10;
  276. rtc.RtcData[6] = '0' + (tmCSU->tm_mday) / 10 % 10;
  277. rtc.RtcData[7] = '0' + (tmCSU->tm_mday) / 1 % 10;
  278. rtc.RtcData[8] = '0' + (tmCSU->tm_hour) / 10 % 10;
  279. rtc.RtcData[9] = '0' + (tmCSU->tm_hour) / 1 % 10;
  280. rtc.RtcData[10] = '0' + (tmCSU->tm_min) / 10 % 10;
  281. rtc.RtcData[11] = '0' + (tmCSU->tm_min) / 1 % 10;
  282. rtc.RtcData[12] = '0' + (tmCSU->tm_sec) / 10 % 10;
  283. rtc.RtcData[13] = '0' + (tmCSU->tm_sec) / 1 % 10;
  284. if (Config_Rtc_Data(Uart1Fd, Addr.IoExtend, &rtc) == PASS)
  285. {
  286. //PRINTF_FUNC("SetRtc sucessfully. \n");
  287. }
  288. else
  289. {
  290. //PRINTF_FUNC("SetRtc fail. \n");
  291. }
  292. }
  293. //================================================
  294. // Main process
  295. //================================================
  296. int InitComPort()
  297. {
  298. int fd;
  299. struct termios tios;
  300. fd = open(priPortName, O_RDWR);
  301. if(fd<=0)
  302. {
  303. #ifdef SystemLogMessage
  304. DEBUG_ERROR("open 407 Communication port NG \n");
  305. #endif
  306. return -1;
  307. }
  308. ioctl (fd, TCGETS, &tios);
  309. tios.c_cflag = B115200| CS8 | CLOCAL | CREAD;
  310. tios.c_lflag = 0;
  311. tios.c_iflag = 0;
  312. tios.c_oflag = 0;
  313. tios.c_cc[VMIN]=0;
  314. tios.c_cc[VTIME]=(unsigned char)1;
  315. tios.c_lflag=0;
  316. tcflush(fd, TCIFLUSH);
  317. ioctl (fd, TCSETS, &tios);
  318. return fd;
  319. }
  320. unsigned long GetTimeoutValue(struct timeval _sour_time)
  321. {
  322. struct timeval _end_time;
  323. gettimeofday(&_end_time, NULL);
  324. return 1000000 * (_end_time.tv_sec - _sour_time.tv_sec) + _end_time.tv_usec - _sour_time.tv_usec;
  325. }
  326. int main(void)
  327. {
  328. if(InitShareMemory() == FAIL)
  329. {
  330. #ifdef SystemLogMessage
  331. DEBUG_ERROR("InitShareMemory NG\n");
  332. #endif
  333. if(ShmStatusCodeData!=NULL)
  334. {
  335. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.FailToCreateShareMemory = 1;
  336. }
  337. sleep(5);
  338. return 0;
  339. }
  340. Uart1Fd = InitComPort();
  341. PRINTF_FUNC("407 Port id = %d \n", Uart1Fd);
  342. if(Uart1Fd < 0)
  343. {
  344. #ifdef SystemLogMessage
  345. DEBUG_ERROR("InitComPort (Uart1 : AM3352 - STM32) NG");
  346. #endif
  347. if (ShmStatusCodeData != NULL)
  348. {
  349. ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = 1;
  350. }
  351. sleep(5);
  352. return 0;
  353. }
  354. SetRtcData();
  355. gettimeofday(&_flash_time, NULL);
  356. for(;;)
  357. {
  358. if (strcmp((char *)ShmSysConfigAndInfo->SysInfo.LcmHwRev, " ") == 0x00)
  359. {
  360. if ((GetTimeoutValue(_flash_time) / 1000) > 1000)
  361. {
  362. if (flash == NO)
  363. flash = YES;
  364. else
  365. flash = NO;
  366. SetOutputGpio(flash);
  367. gettimeofday(&_flash_time, NULL);
  368. }
  369. }
  370. else
  371. {
  372. if ((GetTimeoutValue(_flash_time) / 1000) > 5000)
  373. {
  374. flash = YES;
  375. SetOutputGpio(flash);
  376. gettimeofday(&_flash_time, NULL);
  377. }
  378. }
  379. // 程序開始之前~ 必須先確定 FW 版本與硬體版本,確認後!!~ 該模組才算是真正的 Initial Comp.
  380. // 模組更新 FW 後,需重新做
  381. if(ShmPrimaryMcuData->SelfTest_Comp != PASS)
  382. {
  383. PRINTF_FUNC("(407) Get Fw and Hw Ver. \n");
  384. GetFwAndHwVersion();
  385. sleep(1);
  386. ShmPrimaryMcuData->SelfTest_Comp = PASS;
  387. }
  388. else
  389. {
  390. GetInputGpioStatus();
  391. }
  392. usleep(100000);
  393. }
  394. return FAIL;
  395. }