Module_PrimaryComm.c 7.6 KB

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