FactoryConfig.c 9.1 KB

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