FactoryConfig.c 5.7 KB

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