FactoryConfig.c 9.2 KB

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