FactoryConfig.c 12 KB

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