FactoryConfig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. void CustomChange(char *custom)
  87. {
  88. if (strcmp(custom, "C0") == EQUAL)
  89. {
  90. SysConfig.AuthorisationMode = AUTH_MODE_DISABLE;
  91. SysConfig.isRFID = 0;
  92. }
  93. }
  94. /**************************************************************************************/
  95. /************This task will create Factory default confgiuration file *****************/
  96. /***********and store it into mtdblock 10,11,12 ****************/
  97. /**************************************************************************************/
  98. int main(int argc,char *argv[])
  99. {
  100. unsigned char outType=0;
  101. unsigned int i,Chk; //MtdBlockSize = 0x600000;
  102. unsigned int MtdBlockSize = 0x300000;
  103. unsigned char *ptr;
  104. int fd,wrd;
  105. ptr=malloc(MtdBlockSize);
  106. if(ptr==NULL)
  107. {
  108. #ifdef SystemLogMessage
  109. StoreLogMsg("[FactoryConfig]main: malloc for SysConfigData NG");
  110. #endif
  111. return 0;
  112. }
  113. memset(ptr, 0, MtdBlockSize);
  114. memset(&SysConfig, 0, sizeof(struct SysConfigData));
  115. /*
  116. * TODO: Set factory default configuration
  117. */
  118. //********** System **********// udhcpc -i eth1 -s ./dhcp_script/eth1.script
  119. //
  120. if (argc == 4)
  121. {
  122. strcpy((char *)SysConfig.ModelName, argv[2]);
  123. strcpy((char *)SysConfig.SerialNumber, argv[3]);
  124. }
  125. else
  126. {
  127. strcpy((char *)SysConfig.ModelName, "DSYE301E00D2PH");
  128. strcpy((char *)SysConfig.SerialNumber, "NeedSetupSN");
  129. }
  130. memset(SysConfig.SystemId, 0x00, sizeof(SysConfig.SystemId));
  131. strcat((char *)SysConfig.SystemId, (char *)SysConfig.ModelName);
  132. strcat((char *)SysConfig.SystemId, (char *)SysConfig.SerialNumber);
  133. strcpy((char *)SysConfig.SystemDateTime, "");
  134. SysConfig.AuthorisationMode = AUTH_MODE_ENABLE;
  135. SysConfig.DefaultLanguage = 0;
  136. SysConfig.RfidCardNumEndian = 0;
  137. SysConfig.AcPlugInTimes = 0;
  138. SysConfig.GbPlugInTimes = 0;
  139. SysConfig.Ccs1PlugInTime = 0;
  140. SysConfig.Ccs2PlugInTimes = 0;
  141. SysConfig.ChademoPlugInTimes = 0;
  142. SysConfig.BillingData.isBilling = 0;
  143. SysConfig.isAPP = 1;
  144. SysConfig.isQRCode = 1;
  145. SysConfig.isRFID = 1;
  146. //********** Charging **********//
  147. SysConfig.MaxChargingEnergy = 0;
  148. //SysConfig.MaxChargingCurrent = 200; // 最大可輸出電流 (整樁)
  149. SysConfig.MaxChargingDuration = 0;
  150. SysConfig.AcMaxChargingCurrent = 0;
  151. SysConfig.PhaseLossPolicy = 0;
  152. for(unsigned char i = 0; i < 10; i++)
  153. strcpy((char *)SysConfig.LocalWhiteCard, "");
  154. strcpy((char *)SysConfig.UserId, "");
  155. //********** Network **********//
  156. strcpy((char *)SysConfig.FtpServer, "");
  157. SysConfig.Eth0Interface.EthDhcpClient = 0;
  158. strcpy((char *) SysConfig.Eth0Interface.EthIpAddress, "192.168.1.10");
  159. strcpy((char *) SysConfig.Eth0Interface.EthSubmaskAddress, "255.255.255.0");
  160. strcpy((char *) SysConfig.Eth0Interface.EthGatewayAddress, "192.168.1.254");
  161. SysConfig.Eth1Interface.EthDhcpClient = 0;
  162. strcpy((char *) SysConfig.Eth1Interface.EthIpAddress, "192.168.0.10");
  163. strcpy((char *) SysConfig.Eth1Interface.EthSubmaskAddress, "255.255.255.0");
  164. strcpy((char *) SysConfig.Eth1Interface.EthGatewayAddress, "192.168.0.254");
  165. // if(SysConfig.ModelName[10] == 'W' || SysConfig.ModelName[10] == 'D')
  166. // SysConfig.AthInterface.WifiMode = 2;
  167. // else
  168. // SysConfig.AthInterface.WifiMode = 0;
  169. SysConfig.AthInterface.WifiMode = 0;
  170. SysConfig.TelecomInterface.TelcomEnabled = 0;
  171. strcpy((char *) SysConfig.AthInterface.WifiSsid, "");
  172. strcpy((char *) SysConfig.AthInterface.WifiPassword, "");
  173. SysConfig.AthInterface.WifiRssi = 0;
  174. SysConfig.AthInterface.WifiDhcpServer = 0;
  175. SysConfig.AthInterface.WifiDhcpClient = 0;
  176. strcpy((char *) SysConfig.AthInterface.WifiMacAddress, "");
  177. strcpy((char *) SysConfig.AthInterface.WifiIpAddress, "");
  178. strcpy((char *) SysConfig.AthInterface.WifiSubmaskAddress, "");
  179. strcpy((char *) SysConfig.AthInterface.WifiGatewayAddress, "");
  180. SysConfig.AthInterface.WifiNetworkConn = 0;
  181. strcpy((char *) SysConfig.TelecomInterface.TelcomModelName, "");
  182. strcpy((char *) SysConfig.TelecomInterface.TelcomSoftwareVer, "");
  183. //strcpy((char *) SysConfig.TelecomInterface.TelcomApn, "Internet");
  184. strcpy((char *) SysConfig.TelecomInterface.TelcomApn, "");
  185. SysConfig.TelecomInterface.TelcomRssi = 0;
  186. strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapId, " ");
  187. strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  188. strcpy((char *) SysConfig.TelecomInterface.TelcomModemImei, "");
  189. strcpy((char *) SysConfig.TelecomInterface.TelcomSimImsi, "");
  190. strcpy((char *) SysConfig.TelecomInterface.TelcomSimIccid, "");
  191. SysConfig.TelecomInterface.TelcomSimStatus = 0;
  192. SysConfig.TelecomInterface.TelcomModemMode = 0;
  193. strcpy((char *) SysConfig.TelecomInterface.TelcomIpAddress, "");
  194. SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  195. strcpy((char *)SysConfig.chargePointVendor, "Phihong Technology");
  196. //********** Backend **********//
  197. SysConfig.BackendConnTimeout = 300; //300 seconds
  198. SysConfig.OfflinePolicy = 2;
  199. SysConfig.OfflineMaxChargeEnergy = 0;
  200. SysConfig.OfflineMaxChargeDuration = 0;
  201. strcpy((char *) SysConfig.OcppServerURL, "");
  202. strcpy((char *) SysConfig.ChargeBoxId, "");
  203. SysConfig.LedInfo.Intensity = 2;
  204. // ********** 客製化 ********** //
  205. char _buf[3] = {0};
  206. memcpy(_buf, &SysConfig.ModelName[12], 2);
  207. CustomChange(_buf);
  208. //copy default configuration to pointer
  209. memcpy(ptr,&SysConfig,sizeof(struct SysConfigData));
  210. //calculate CRC
  211. Chk=0;
  212. // for(i=0;i<(MtdBlockSize-4);i++)
  213. // {
  214. // Chk+=*(ptr+i);
  215. // }
  216. for(i = ARRAY_SIZE(SysConfig.CsuBootLoadFwRev); i < (MtdBlockSize-4); i++)
  217. {
  218. Chk+=*(ptr+i);
  219. }
  220. memcpy(ptr+MtdBlockSize-4,&Chk,4);
  221. /*
  222. * Parameter process
  223. */
  224. if (argc > 1)
  225. {
  226. char *arg = argv[1];
  227. switch (arg[0])
  228. {
  229. case '-':
  230. switch (arg[1])
  231. {
  232. case 'a':
  233. outType |= OUTPUT_FILE;
  234. outType |= OUTPUT_FLASH;
  235. break;
  236. case 'f':
  237. outType |= OUTPUT_FILE;
  238. break;
  239. case 'm':
  240. outType |= OUTPUT_FLASH;
  241. break;
  242. default:
  243. helpOutput();
  244. break;
  245. }
  246. break;
  247. default:
  248. helpOutput();
  249. break;
  250. }
  251. }
  252. else
  253. {
  254. helpOutput();
  255. }
  256. /*
  257. * Configuration bin file generate
  258. */
  259. if((outType&OUTPUT_FILE) > 0)
  260. {
  261. //fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR | O_CREAT);
  262. fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR | O_CREAT | O_TRUNC);
  263. if (fd < 0)
  264. {
  265. StoreLogMsg("[FactoryConfig]main: open /mnt/FactoryDefaultConfig.bin NG");
  266. free(ptr);
  267. return 0;
  268. }
  269. wrd=write(fd, ptr, MtdBlockSize);
  270. close(fd);
  271. if(wrd<MtdBlockSize)
  272. {
  273. StoreLogMsg("write /mnt/FactoryDefaultConfig.bin NG\r\n");
  274. free(ptr);
  275. return 0;
  276. }
  277. StoreLogMsg("FactoryConfig write to file in /mnt OK.\r\n");
  278. /* * Flash memory write */
  279. if((outType&OUTPUT_FLASH)>0)
  280. {
  281. StoreLogMsg("Erase /dev/mtd10.\n");
  282. runShellCmd("flash_erase /dev/mtd10 0 0");
  283. StoreLogMsg("Write /dev/mtd10.\n");
  284. runShellCmd("nandwrite -p /dev/mtd10 /mnt/FactoryDefaultConfig.bin");
  285. StoreLogMsg("Erase /dev/mtd11.\n");
  286. runShellCmd("flash_erase /dev/mtd11 0 0");
  287. StoreLogMsg("Write /dev/mtd11.\n");
  288. runShellCmd("nandwrite -p /dev/mtd11 /mnt/FactoryDefaultConfig.bin");
  289. StoreLogMsg("Erase /dev/mtd12.\n");
  290. runShellCmd("flash_erase /dev/mtd12 0 0");
  291. StoreLogMsg("Write /dev/mtd12.\n");
  292. runShellCmd("nandwrite -p /dev/mtd12 /mnt/FactoryDefaultConfig.bin");
  293. system("rm -f /mnt/FactoryDefaultConfig.bin");
  294. StoreLogMsg("FactoryConfig write to flash OK\n");
  295. }
  296. free(ptr);
  297. }
  298. /*
  299. * Flash memory write
  300. */
  301. if((outType&OUTPUT_FLASH)>0)
  302. {
  303. // Save factory default setting value to flash factory default setting block
  304. fd = open("/dev/mtdblock12", O_RDWR);
  305. if (fd < 0)
  306. {
  307. StoreLogMsg("open /dev/mtdblock12 NG\r\n");
  308. free(ptr);
  309. return 0;
  310. }
  311. wrd=write(fd, ptr, MtdBlockSize);
  312. close(fd);
  313. if(wrd<MtdBlockSize)
  314. {
  315. StoreLogMsg("write /dev/mtdblock12 NG\r\n");
  316. free(ptr);
  317. return 0;
  318. }
  319. // Save factory default setting value to flash backup setting block
  320. fd = open("/dev/mtdblock11", O_RDWR);
  321. if (fd < 0)
  322. {
  323. StoreLogMsg("open /dev/mtdblock11 NG\r\n");
  324. free(ptr);
  325. return 0;
  326. }
  327. wrd=write(fd, ptr, MtdBlockSize);
  328. close(fd);
  329. if(wrd<MtdBlockSize)
  330. {
  331. StoreLogMsg("write /dev/mtdblock11 NG\r\n");
  332. free(ptr);
  333. return 0;
  334. }
  335. // Save factory default setting value to flash setting block
  336. fd = open("/dev/mtdblock10", O_RDWR);
  337. if (fd < 0)
  338. {
  339. StoreLogMsg("open /dev/mtdblock10 NG\r\n");
  340. free(ptr);
  341. return 0;
  342. }
  343. wrd=write(fd, ptr, MtdBlockSize);
  344. close(fd);
  345. if(wrd<MtdBlockSize)
  346. {
  347. StoreLogMsg("write /dev/mtdblock10 NG\r\n");
  348. free(ptr);
  349. return 0;
  350. }
  351. StoreLogMsg("FactoryConfig write to flash OK\r\n");
  352. }
  353. free(ptr);
  354. return FAIL;
  355. }