FactoryConfig.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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/can.h>
  13. #include <linux/can/raw.h>
  14. #include <linux/wireless.h>
  15. #include <arpa/inet.h>
  16. #include <netinet/in.h>
  17. #include <unistd.h>
  18. #include <stdarg.h>
  19. #include <stdio.h> /*標準輸入輸出定義*/
  20. #include <stdlib.h> /*標準函數庫定義*/
  21. #include <unistd.h> /*Unix 標準函數定義*/
  22. #include <fcntl.h> /*檔控制定義*/
  23. #include <termios.h> /*PPSIX 終端控制定義*/
  24. #include <errno.h> /*錯誤號定義*/
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <time.h>
  28. #include <ctype.h>
  29. #include <ifaddrs.h>
  30. #include "../../define.h"
  31. #define Debug
  32. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  33. #define PASS 1
  34. #define FAIL -1
  35. #define OUTPUT_FLASH 0x01
  36. #define OUTPUT_FILE 0x02
  37. struct SysConfigData SysConfig;
  38. int StoreLogMsg(const char *fmt, ...);
  39. int StoreLogMsg(const char *fmt, ...)
  40. {
  41. char Buf[4096+256];
  42. char buffer[4096];
  43. time_t CurrentTime;
  44. struct tm *tm;
  45. va_list args;
  46. va_start(args, fmt);
  47. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  48. va_end(args);
  49. memset(Buf,0,sizeof(Buf));
  50. CurrentTime = time(NULL);
  51. tm=localtime(&CurrentTime);
  52. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  53. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  54. buffer,
  55. tm->tm_year+1900,tm->tm_mon+1);
  56. system(Buf);
  57. return rc;
  58. }
  59. void helpOutput(void)
  60. {
  61. printf("Usage: Module_FactoryConfig [OPTION]...\r\n\r\n");
  62. printf("Generate factory default configuration value\r\n\r\n");
  63. printf("OPTION:\r\n");
  64. printf(" -a Write to file(/mnt) & flash\r\n");
  65. printf(" -f Write to file(/mnt)\r\n");
  66. printf(" -m Write to flash\r\n");
  67. }
  68. /**************************************************************************************/
  69. /************This task will create Factory default confgiuration file *****************/
  70. /***********and store it into mtdblock 10,11,12 ****************/
  71. /**************************************************************************************/
  72. int main(int argc,char *argv[])
  73. {
  74. unsigned char outType = 0;
  75. unsigned int i,Chk, MtdBlockSize = 0x600000;
  76. unsigned char *ptr;
  77. int fd, wrd;
  78. ptr = malloc(MtdBlockSize);
  79. if(ptr == NULL)
  80. {
  81. #ifdef SystemLogMessage
  82. StoreLogMsg("[FactoryConfig]main: malloc for SysConfigData NG");
  83. #endif
  84. return 0;
  85. }
  86. memset(ptr, 0, MtdBlockSize);
  87. memset(&SysConfig, 0, sizeof(struct SysConfigData));
  88. /*
  89. * TODO: Set factory default configuration
  90. */
  91. //********** System **********//
  92. strcpy((char *)SysConfig.ModelName, "DSYE122J0ET2PH");
  93. strcpy((char *)SysConfig.SerialNumber, "");
  94. strcpy((char *)SysConfig.SystemId, "");
  95. strcpy((char *)SysConfig.SystemDateTime, "");
  96. SysConfig.AuthorisationMode = 0;
  97. SysConfig.DefaultLanguage = 0;
  98. SysConfig.RfidCardNumEndian = 0;
  99. SysConfig.AcPlugInTimes = 0;
  100. SysConfig.GbPlugInTimes = 0;
  101. SysConfig.Ccs1PlugInTime = 0;
  102. SysConfig.Ccs2PlugInTimes = 0;
  103. SysConfig.ChademoPlugInTimes = 0;
  104. //********** Charging **********//
  105. SysConfig.MaxChargingEnergy = 0;
  106. SysConfig.MaxChargingPower = 0;
  107. SysConfig.MaxChargingCurrent = 0;
  108. SysConfig.MaxChargingDuration = 0;
  109. SysConfig.PhaseLossPolicy = 0;
  110. for(unsigned char i = 0; i < 10; i++)
  111. strcpy((char *)SysConfig.LocalWhiteCard, "");
  112. strcpy((char *)SysConfig.UserId, "");
  113. //********** Network **********//
  114. strcpy((char *)SysConfig.FtpServer, "");
  115. SysConfig.Eth0Interface.EthDhcpClient = 0;
  116. strcpy((char *) SysConfig.Eth0Interface.EthMacAddress, "");
  117. strcpy((char *) SysConfig.Eth0Interface.EthIpAddress, "192.168.1.10");
  118. strcpy((char *) SysConfig.Eth0Interface.EthSubmaskAddress, "255.255.255.0");
  119. strcpy((char *) SysConfig.Eth0Interface.EthGatewayAddress, "192.168.1.254");
  120. SysConfig.Eth1Interface.EthDhcpClient = 0;
  121. strcpy((char *) SysConfig.Eth1Interface.EthMacAddress, "");
  122. strcpy((char *) SysConfig.Eth1Interface.EthIpAddress, "192.168.0.10");
  123. strcpy((char *) SysConfig.Eth1Interface.EthSubmaskAddress, "255.255.255.0");
  124. strcpy((char *) SysConfig.Eth1Interface.EthGatewayAddress, "192.168.0.254");
  125. SysConfig.AthInterface.WifiMode = 0;
  126. strcpy((char *) SysConfig.AthInterface.WifiSsid, "");
  127. strcpy((char *) SysConfig.AthInterface.WifiPassword, "");
  128. SysConfig.AthInterface.WifiRssi = 0;
  129. SysConfig.AthInterface.WifiDhcpServer = 0;
  130. SysConfig.AthInterface.WifiDhcpClient = 0;
  131. strcpy((char *) SysConfig.AthInterface.WifiMacAddress, "");
  132. strcpy((char *) SysConfig.AthInterface.WifiIpAddress, "");
  133. strcpy((char *) SysConfig.AthInterface.WifiSubmaskAddress, "");
  134. strcpy((char *) SysConfig.AthInterface.WifiGatewayAddress, "");
  135. SysConfig.AthInterface.WifiNetworkConn = 0;
  136. strcpy((char *) SysConfig.TelecomInterface.TelcomModelName, "");
  137. strcpy((char *) SysConfig.TelecomInterface.TelcomSoftwareVer, "");
  138. strcpy((char *) SysConfig.TelecomInterface.TelcomApn, "Internet");
  139. SysConfig.TelecomInterface.TelcomRssi = 0;
  140. strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapId, "");
  141. strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapPwd, "");
  142. strcpy((char *) SysConfig.TelecomInterface.TelcomModemImei, "");
  143. strcpy((char *) SysConfig.TelecomInterface.TelcomSimImsi, "");
  144. strcpy((char *) SysConfig.TelecomInterface.TelcomSimIccid, "");
  145. SysConfig.TelecomInterface.TelcomSimStatus = 0;
  146. SysConfig.TelecomInterface.TelcomModemMode = 0;
  147. strcpy((char *) SysConfig.TelecomInterface.TelcomIpAddress, "");
  148. SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  149. //********** Backend **********//
  150. SysConfig.BackendConnTimeout=300; //300 seconds
  151. SysConfig.OfflinePolicy = 0;
  152. SysConfig.OfflineMaxChargeEnergy = 0;
  153. SysConfig.OfflineMaxChargeDuration = 0;
  154. strcpy((char *) SysConfig.OcppServerURL, "ws://127.0.0.1");
  155. strcpy((char *) SysConfig.ChargeBoxId, "");
  156. //copy default configuration to pointer
  157. memcpy(ptr, &SysConfig, sizeof(struct SysConfigData));
  158. //calculate CRC
  159. Chk=0;
  160. for(i=0;i<(MtdBlockSize-4);i++)
  161. {
  162. Chk += *(ptr+i);
  163. }
  164. memcpy( ptr + MtdBlockSize-4, &Chk, 4);
  165. /*
  166. * Parameter process
  167. */
  168. if (argc > 1)
  169. {
  170. char *arg = argv[1];
  171. switch (arg[0])
  172. {
  173. case '-':
  174. switch (arg[1])
  175. {
  176. case 'a':
  177. outType |= OUTPUT_FILE;
  178. outType |= OUTPUT_FLASH;
  179. break;
  180. case 'f':
  181. outType |= OUTPUT_FILE;
  182. break;
  183. case 'm':
  184. outType |= OUTPUT_FLASH;
  185. break;
  186. default:
  187. helpOutput();
  188. break;
  189. }
  190. break;
  191. default:
  192. helpOutput();
  193. break;
  194. }
  195. }
  196. else
  197. {
  198. helpOutput();
  199. }
  200. /*
  201. * Configuration bin file generate
  202. */
  203. if((outType&OUTPUT_FILE) > 0)
  204. {
  205. fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR | O_CREAT);
  206. if (fd < 0)
  207. {
  208. StoreLogMsg("[FactoryConfig]main: open /mnt/FactoryDefaultConfig.bin NG");
  209. free(ptr);
  210. return 0;
  211. }
  212. wrd=write(fd, ptr, MtdBlockSize);
  213. close(fd);
  214. if(wrd < MtdBlockSize)
  215. {
  216. StoreLogMsg("write /mnt/FactoryDefaultConfig.bin NG\r\n");
  217. free(ptr);
  218. return 0;
  219. }
  220. StoreLogMsg("FactoryConfig write to file in /mnt OK.\r\n");
  221. }
  222. /*
  223. * Flash memory write
  224. */
  225. if((outType&OUTPUT_FLASH) > 0)
  226. {
  227. // Save factory default setting value to flash factory default setting block
  228. fd = open("/dev/mtdblock12", O_RDWR);
  229. if (fd < 0)
  230. {
  231. StoreLogMsg("open /dev/mtdblock12 NG\r\n");
  232. free(ptr);
  233. return 0;
  234. }
  235. wrd=write(fd, ptr, MtdBlockSize);
  236. close(fd);
  237. if(wrd < MtdBlockSize)
  238. {
  239. StoreLogMsg("write /dev/mtdblock12 NG\r\n");
  240. free(ptr);
  241. return 0;
  242. }
  243. // Save factory default setting value to flash backup setting block
  244. fd = open("/dev/mtdblock11", O_RDWR);
  245. if (fd < 0)
  246. {
  247. StoreLogMsg("open /dev/mtdblock11 NG\r\n");
  248. free(ptr);
  249. return 0;
  250. }
  251. wrd=write(fd, ptr, MtdBlockSize);
  252. close(fd);
  253. if(wrd < MtdBlockSize)
  254. {
  255. StoreLogMsg("write /dev/mtdblock11 NG\r\n");
  256. free(ptr);
  257. return 0;
  258. }
  259. // Save factory default setting value to flash setting block
  260. fd = open("/dev/mtdblock10", O_RDWR);
  261. if (fd < 0)
  262. {
  263. StoreLogMsg("open /dev/mtdblock10 NG\r\n");
  264. free(ptr);
  265. return 0;
  266. }
  267. wrd=write(fd, ptr, MtdBlockSize);
  268. close(fd);
  269. if(wrd < MtdBlockSize)
  270. {
  271. StoreLogMsg("write /dev/mtdblock10 NG\r\n");
  272. free(ptr);
  273. return 0;
  274. }
  275. StoreLogMsg("FactoryConfig write to flash OK\r\n");
  276. }
  277. free(ptr);
  278. return FAIL;
  279. }