FactoryConfig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/mman.h>
  11. #include <linux/can.h>
  12. #include <linux/can/raw.h>
  13. #include <linux/wireless.h>
  14. #include <arpa/inet.h>
  15. #include <netinet/in.h>
  16. #include <unistd.h>
  17. #include <stdarg.h>
  18. #include <stdio.h> /*標準輸入輸出定義*/
  19. #include <stdlib.h> /*標準函數庫定義*/
  20. #include <unistd.h> /*Unix 標準函數定義*/
  21. #include <fcntl.h> /*檔控制定義*/
  22. #include <termios.h> /*PPSIX 終端控制定義*/
  23. #include <errno.h> /*錯誤號定義*/
  24. #include <string.h>
  25. #include <time.h>
  26. #include <ctype.h>
  27. #include <ifaddrs.h>
  28. #include "../../define.h"
  29. #include "Config.h"
  30. #define Debug
  31. #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
  32. #define PASS 1
  33. #define FAIL -1
  34. #define OUTPUT_FLASH 0x01
  35. #define OUTPUT_FILE 0x02
  36. #define EQUAL 0
  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. va_list args;
  44. struct timeb SeqEndTime;
  45. struct tm *tm;
  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. ftime(&SeqEndTime);
  51. SeqEndTime.time = time(NULL);
  52. tm=localtime(&SeqEndTime.time);
  53. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d:%03d - %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,SeqEndTime.millitm,
  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. int runShellCmd(const char*cmd)
  70. {
  71. int result = FAIL;
  72. char buf[256];
  73. FILE *fp;
  74. fp = popen(cmd, "r");
  75. if(fp != NULL)
  76. {
  77. while(fgets(buf, sizeof(buf), fp) != NULL)
  78. {
  79. StoreLogMsg("%s\n", buf);
  80. }
  81. result = PASS;
  82. }
  83. pclose(fp);
  84. return result;
  85. }
  86. /**************************************************************************************/
  87. /************This task will create Factory default confgiuration file *****************/
  88. /***********and store it into mtdblock 10,11,12 ****************/
  89. /**************************************************************************************/
  90. int main(int argc,char *argv[])
  91. {
  92. unsigned char outType=0;
  93. unsigned int i,Chk; //MtdBlockSize = 0x600000;
  94. unsigned int MtdBlockSize = 0x300000;
  95. unsigned char *ptr;
  96. int fd,wrd;
  97. ptr=malloc(MtdBlockSize);
  98. if(ptr==NULL)
  99. {
  100. #ifdef SystemLogMessage
  101. StoreLogMsg("[FactoryConfig]main: malloc for SysConfigData NG");
  102. #endif
  103. return 0;
  104. }
  105. memset(ptr, 0, MtdBlockSize);
  106. memset(&SysConfig, 0, sizeof(struct SysConfigData));
  107. /*
  108. * TODO: Set factory default configuration
  109. */
  110. //********** System **********// udhcpc -i eth1 -s ./dhcp_script/eth1.script
  111. //
  112. if (argc == 4)
  113. {
  114. strcpy((char *)SysConfig.ModelName, argv[2]);
  115. strcpy((char *)SysConfig.SerialNumber, argv[3]);
  116. }
  117. else
  118. {
  119. strcpy((char *)SysConfig.ModelName, "DSYE301E00D2PH");
  120. strcpy((char *)SysConfig.SerialNumber, "NeedSetupSN");
  121. }
  122. memset(SysConfig.SystemId, 0x00, sizeof(SysConfig.SystemId));
  123. strcat((char *)SysConfig.SystemId, (char *)SysConfig.ModelName);
  124. strcat((char *)SysConfig.SystemId, (char *)SysConfig.SerialNumber);
  125. strcpy((char *)SysConfig.SystemDateTime, "");
  126. SysConfig.AuthorisationMode = AUTH_MODE_ENABLE;
  127. SysConfig.DefaultLanguage = 0;
  128. SysConfig.RfidCardNumEndian = 0;
  129. SysConfig.AcPlugInTimes = 0;
  130. SysConfig.GbPlugInTimes = 0;
  131. SysConfig.Ccs1PlugInTime = 0;
  132. SysConfig.Ccs2PlugInTimes = 0;
  133. SysConfig.ChademoPlugInTimes = 0;
  134. SysConfig.BillingData.isBilling = 0;
  135. SysConfig.isAPP = 1;
  136. SysConfig.isQRCode = 1;
  137. SysConfig.isRFID = 1;
  138. //********** Charging **********//
  139. SysConfig.MaxChargingEnergy = 0;
  140. //SysConfig.MaxChargingCurrent = 200; // 最大可輸出電流 (整樁)
  141. SysConfig.MaxChargingDuration = 0;
  142. SysConfig.AcMaxChargingCurrent = 0;
  143. SysConfig.PhaseLossPolicy = 0;
  144. for(unsigned char i = 0; i < 10; i++)
  145. strcpy((char *)SysConfig.LocalWhiteCard, "");
  146. strcpy((char *)SysConfig.UserId, "");
  147. //********** Network **********//
  148. strcpy((char *)SysConfig.FtpServer, "");
  149. SysConfig.Eth0Interface.EthDhcpClient = 0;
  150. strcpy((char *) SysConfig.Eth0Interface.EthIpAddress, "192.168.1.10");
  151. strcpy((char *) SysConfig.Eth0Interface.EthSubmaskAddress, "255.255.255.0");
  152. strcpy((char *) SysConfig.Eth0Interface.EthGatewayAddress, "192.168.1.254");
  153. SysConfig.Eth1Interface.EthDhcpClient = 0;
  154. strcpy((char *) SysConfig.Eth1Interface.EthIpAddress, "192.168.0.10");
  155. strcpy((char *) SysConfig.Eth1Interface.EthSubmaskAddress, "255.255.255.0");
  156. strcpy((char *) SysConfig.Eth1Interface.EthGatewayAddress, "192.168.0.254");
  157. // if(SysConfig.ModelName[10] == 'W' || SysConfig.ModelName[10] == 'D')
  158. // SysConfig.AthInterface.WifiMode = 2;
  159. // else
  160. // SysConfig.AthInterface.WifiMode = 0;
  161. SysConfig.AthInterface.WifiMode = 0;
  162. SysConfig.TelecomInterface.TelcomEnabled = 0;
  163. strcpy((char *) SysConfig.AthInterface.WifiSsid, "");
  164. strcpy((char *) SysConfig.AthInterface.WifiPassword, "");
  165. SysConfig.AthInterface.WifiRssi = 0;
  166. SysConfig.AthInterface.WifiDhcpServer = 0;
  167. SysConfig.AthInterface.WifiDhcpClient = 0;
  168. strcpy((char *) SysConfig.AthInterface.WifiMacAddress, "");
  169. strcpy((char *) SysConfig.AthInterface.WifiIpAddress, "");
  170. strcpy((char *) SysConfig.AthInterface.WifiSubmaskAddress, "");
  171. strcpy((char *) SysConfig.AthInterface.WifiGatewayAddress, "");
  172. SysConfig.AthInterface.WifiNetworkConn = 0;
  173. strcpy((char *) SysConfig.TelecomInterface.TelcomModelName, "");
  174. strcpy((char *) SysConfig.TelecomInterface.TelcomSoftwareVer, "");
  175. //strcpy((char *) SysConfig.TelecomInterface.TelcomApn, "Internet");
  176. strcpy((char *) SysConfig.TelecomInterface.TelcomApn, "");
  177. SysConfig.TelecomInterface.TelcomRssi = 0;
  178. strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapId, " ");
  179. strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  180. strcpy((char *) SysConfig.TelecomInterface.TelcomModemImei, "");
  181. strcpy((char *) SysConfig.TelecomInterface.TelcomSimImsi, "");
  182. strcpy((char *) SysConfig.TelecomInterface.TelcomSimIccid, "");
  183. SysConfig.TelecomInterface.TelcomSimStatus = 0;
  184. SysConfig.TelecomInterface.TelcomModemMode = 0;
  185. strcpy((char *) SysConfig.TelecomInterface.TelcomIpAddress, "");
  186. SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  187. strcpy((char *)SysConfig.chargePointVendor, "Phihong Technology");
  188. //********** Backend **********//
  189. SysConfig.BackendConnTimeout = 300; //300 seconds
  190. SysConfig.OfflinePolicy = 2;
  191. SysConfig.OfflineMaxChargeEnergy = 0;
  192. SysConfig.OfflineMaxChargeDuration = 0;
  193. strcpy((char *) SysConfig.OcppServerURL, "");
  194. strcpy((char *) SysConfig.ChargeBoxId, "");
  195. SysConfig.LedInfo.Intensity = 2;
  196. //copy default configuration to pointer
  197. memcpy(ptr,&SysConfig,sizeof(struct SysConfigData));
  198. //calculate CRC
  199. Chk=0;
  200. // for(i=0;i<(MtdBlockSize-4);i++)
  201. // {
  202. // Chk+=*(ptr+i);
  203. // }
  204. for(i = ARRAY_SIZE(SysConfig.CsuBootLoadFwRev); i < (MtdBlockSize-4); i++)
  205. {
  206. Chk+=*(ptr+i);
  207. }
  208. memcpy(ptr+MtdBlockSize-4,&Chk,4);
  209. /*
  210. * Parameter process
  211. */
  212. if (argc > 1)
  213. {
  214. char *arg = argv[1];
  215. switch (arg[0])
  216. {
  217. case '-':
  218. switch (arg[1])
  219. {
  220. case 'a':
  221. outType |= OUTPUT_FILE;
  222. outType |= OUTPUT_FLASH;
  223. break;
  224. case 'f':
  225. outType |= OUTPUT_FILE;
  226. break;
  227. case 'm':
  228. outType |= OUTPUT_FLASH;
  229. break;
  230. default:
  231. helpOutput();
  232. break;
  233. }
  234. break;
  235. default:
  236. helpOutput();
  237. break;
  238. }
  239. }
  240. else
  241. {
  242. helpOutput();
  243. }
  244. /*
  245. * Configuration bin file generate
  246. */
  247. if((outType&OUTPUT_FILE) > 0)
  248. {
  249. //fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR | O_CREAT);
  250. fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR | O_CREAT | O_TRUNC);
  251. if (fd < 0)
  252. {
  253. StoreLogMsg("[FactoryConfig]main: open /mnt/FactoryDefaultConfig.bin NG");
  254. free(ptr);
  255. return 0;
  256. }
  257. wrd=write(fd, ptr, MtdBlockSize);
  258. close(fd);
  259. if(wrd<MtdBlockSize)
  260. {
  261. StoreLogMsg("write /mnt/FactoryDefaultConfig.bin NG\r\n");
  262. free(ptr);
  263. return 0;
  264. }
  265. StoreLogMsg("FactoryConfig write to file in /mnt OK.\r\n");
  266. /* * Flash memory write */
  267. if((outType&OUTPUT_FLASH)>0)
  268. {
  269. StoreLogMsg("Erase /dev/mtd10.\n");
  270. runShellCmd("flash_erase /dev/mtd10 0 0");
  271. StoreLogMsg("Write /dev/mtd10.\n");
  272. runShellCmd("nandwrite -p /dev/mtd10 /mnt/FactoryDefaultConfig.bin");
  273. StoreLogMsg("Erase /dev/mtd11.\n");
  274. runShellCmd("flash_erase /dev/mtd11 0 0");
  275. StoreLogMsg("Write /dev/mtd11.\n");
  276. runShellCmd("nandwrite -p /dev/mtd11 /mnt/FactoryDefaultConfig.bin");
  277. StoreLogMsg("Erase /dev/mtd12.\n");
  278. runShellCmd("flash_erase /dev/mtd12 0 0");
  279. StoreLogMsg("Write /dev/mtd12.\n");
  280. runShellCmd("nandwrite -p /dev/mtd12 /mnt/FactoryDefaultConfig.bin");
  281. system("rm -f /mnt/FactoryDefaultConfig.bin");
  282. StoreLogMsg("FactoryConfig write to flash OK\n");
  283. }
  284. free(ptr);
  285. }
  286. /*
  287. * Flash memory write
  288. */
  289. if((outType&OUTPUT_FLASH)>0)
  290. {
  291. // Save factory default setting value to flash factory default setting block
  292. fd = open("/dev/mtdblock12", O_RDWR);
  293. if (fd < 0)
  294. {
  295. StoreLogMsg("open /dev/mtdblock12 NG\r\n");
  296. free(ptr);
  297. return 0;
  298. }
  299. wrd=write(fd, ptr, MtdBlockSize);
  300. close(fd);
  301. if(wrd<MtdBlockSize)
  302. {
  303. StoreLogMsg("write /dev/mtdblock12 NG\r\n");
  304. free(ptr);
  305. return 0;
  306. }
  307. // Save factory default setting value to flash backup setting block
  308. fd = open("/dev/mtdblock11", O_RDWR);
  309. if (fd < 0)
  310. {
  311. StoreLogMsg("open /dev/mtdblock11 NG\r\n");
  312. free(ptr);
  313. return 0;
  314. }
  315. wrd=write(fd, ptr, MtdBlockSize);
  316. close(fd);
  317. if(wrd<MtdBlockSize)
  318. {
  319. StoreLogMsg("write /dev/mtdblock11 NG\r\n");
  320. free(ptr);
  321. return 0;
  322. }
  323. // Save factory default setting value to flash setting block
  324. fd = open("/dev/mtdblock10", O_RDWR);
  325. if (fd < 0)
  326. {
  327. StoreLogMsg("open /dev/mtdblock10 NG\r\n");
  328. free(ptr);
  329. return 0;
  330. }
  331. wrd=write(fd, ptr, MtdBlockSize);
  332. close(fd);
  333. if(wrd<MtdBlockSize)
  334. {
  335. StoreLogMsg("write /dev/mtdblock10 NG\r\n");
  336. free(ptr);
  337. return 0;
  338. }
  339. StoreLogMsg("FactoryConfig write to flash OK\r\n");
  340. }
  341. free(ptr);
  342. return FAIL;
  343. }