FactoryConfig.c 11 KB

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