FactoryConfig.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*===========================================================================
  2. Combined Charging System (CCS): SECC
  3. FactoryConfig.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 "FactoryConfig.h"
  28. #ifdef SystemLogMessage
  29. int StoreLogMsg(unsigned char *DataString)
  30. {
  31. unsigned char Buf[256];
  32. time_t CurrentTime;
  33. struct tm *tm;
  34. memset(Buf, 0, sizeof(Buf));
  35. CurrentTime = time(NULL);
  36. tm = localtime(&CurrentTime);
  37. sprintf(Buf, "echo \"%04d.%02d.%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  38. tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
  39. DataString,
  40. tm->tm_year + 1900, tm->tm_mon + 1);
  41. system(Buf);
  42. #ifdef Debug
  43. printf("%s \n", DataString);
  44. #endif
  45. }
  46. #endif
  47. /**************************************************************************************/
  48. /************This task will create Factory default confgiuration file *****************/
  49. /***********and store it into mtdblock 10,11,12 ****************/
  50. /**************************************************************************************/
  51. int main(int argc, char *argv[])
  52. {
  53. struct SysConfigData SysConfig;
  54. unsigned int i, Chk;
  55. unsigned char *ptr;
  56. int fd, wrd;
  57. ptr = malloc(sizeof(struct SysConfigData));
  58. if(ptr == NULL)
  59. {
  60. #ifdef SystemLogMessage
  61. StoreLogMsg("[FactoryConfig]main: malloc for SysConfigData NG");
  62. #endif
  63. return 0;
  64. }
  65. memset(ptr, 0, sizeof(struct SysConfigData));
  66. memset(&SysConfig, 0, sizeof(struct SysConfigData));
  67. //Set default configuration
  68. strcpy(SysConfig.Eth0Interface.EthIpAddress, "192.168.0.10");
  69. strcpy(SysConfig.Eth0Interface.EthSubmaskAddress, "255.255.255.0");
  70. strcpy(SysConfig.Eth0Interface.EthGatewayAddress, "192.168.0.254");
  71. strcpy(SysConfig.Eth1Interface.EthIpAddress, "192.168.1.10");
  72. strcpy(SysConfig.Eth1Interface.EthSubmaskAddress, "255.255.255.0");
  73. strcpy(SysConfig.Eth1Interface.EthGatewayAddress, "192.168.1.254");
  74. SysConfig.BackendConnTimeout = 300; //300 seconds
  75. //copy default configuration to pointer
  76. memcpy(ptr, &SysConfig, sizeof(struct SysConfigData));
  77. //calculate CRC
  78. Chk = 0;
  79. for(i = 0; i < (sizeof(struct SysConfigData) - 4); i++)
  80. {
  81. Chk += *(ptr + i);
  82. }
  83. SysConfig.Checksum = Chk;
  84. fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR | O_CREAT);
  85. if(fd < 0)
  86. {
  87. #ifdef SystemLogMessage
  88. StoreLogMsg("[FactoryConfig]main: open /mnt/FactoryDefaultConfig.bin NG");
  89. #endif
  90. free(ptr);
  91. return 0;
  92. }
  93. wrd = write(fd, &SysConfig, sizeof(struct SysConfigData));
  94. close(fd);
  95. if(wrd != (sizeof(struct SysConfigData)))
  96. {
  97. #ifdef SystemLogMessage
  98. StoreLogMsg("[FactoryConfig]main: write /mnt/FactoryDefaultConfig.bin NG");
  99. #endif
  100. free(ptr);
  101. return 0;
  102. }
  103. fd = open("/dev/mtdblock12", O_RDWR);
  104. if(fd < 0)
  105. {
  106. #ifdef SystemLogMessage
  107. StoreLogMsg("[FactoryConfig]main: open /dev/mtdblock12 NG");
  108. #endif
  109. free(ptr);
  110. return 0;
  111. }
  112. wrd = write(fd, &SysConfig, sizeof(struct SysConfigData));
  113. close(fd);
  114. if(wrd != (sizeof(struct SysConfigData)))
  115. {
  116. #ifdef SystemLogMessage
  117. StoreLogMsg("[FactoryConfig]main: write /dev/mtdblock12 NG");
  118. #endif
  119. free(ptr);
  120. return 0;
  121. }
  122. fd = open("/dev/mtdblock11", O_RDWR);
  123. if(fd < 0)
  124. {
  125. #ifdef SystemLogMessage
  126. StoreLogMsg("[FactoryConfig]main: open /dev/mtdblock11 NG");
  127. #endif
  128. free(ptr);
  129. return 0;
  130. }
  131. wrd = write(fd, &SysConfig, sizeof(struct SysConfigData));
  132. close(fd);
  133. if(wrd != (sizeof(struct SysConfigData)))
  134. {
  135. #ifdef SystemLogMessage
  136. StoreLogMsg("[FactoryConfig]main: write /dev/mtdblock11 NG");
  137. #endif
  138. free(ptr);
  139. return 0;
  140. }
  141. fd = open("/dev/mtdblock10", O_RDWR);
  142. if(fd < 0)
  143. {
  144. #ifdef SystemLogMessage
  145. StoreLogMsg("[FactoryConfig]main: open /dev/mtdblock10 NG");
  146. #endif
  147. free(ptr);
  148. return 0;
  149. }
  150. wrd = write(fd, &SysConfig, sizeof(struct SysConfigData));
  151. close(fd);
  152. if(wrd != (sizeof(struct SysConfigData)))
  153. {
  154. #ifdef SystemLogMessage
  155. StoreLogMsg("[FactoryConfig]main: write /dev/mtdblock10 NG");
  156. #endif
  157. free(ptr);
  158. return 0;
  159. }
  160. free(ptr);
  161. #ifdef SystemLogMessage
  162. StoreLogMsg("[FactoryConfig]main: FactoryConfig OK");
  163. #endif
  164. }