Module_PrimaryComm.c 8.4 KB

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