FactoryConfig.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. SysConfig.AthInterface.WifiMode = 0;
  146. SysConfig.TelecomInterface.TelcomEnabled = 0;
  147. strcpy((char *) SysConfig.AthInterface.WifiSsid, "");
  148. strcpy((char *) SysConfig.AthInterface.WifiPassword, "");
  149. SysConfig.AthInterface.WifiRssi = 0;
  150. SysConfig.AthInterface.WifiDhcpServer = 0;
  151. SysConfig.AthInterface.WifiDhcpClient = 0;
  152. strcpy((char *) SysConfig.AthInterface.WifiMacAddress, "");
  153. strcpy((char *) SysConfig.AthInterface.WifiIpAddress, "");
  154. strcpy((char *) SysConfig.AthInterface.WifiSubmaskAddress, "");
  155. strcpy((char *) SysConfig.AthInterface.WifiGatewayAddress, "");
  156. SysConfig.AthInterface.WifiNetworkConn = 0;
  157. strcpy((char *) SysConfig.TelecomInterface.TelcomModelName, "");
  158. strcpy((char *) SysConfig.TelecomInterface.TelcomSoftwareVer, "");
  159. //strcpy((char *) SysConfig.TelecomInterface.TelcomApn, "Internet");
  160. strcpy((char *) SysConfig.TelecomInterface.TelcomApn, "");
  161. SysConfig.TelecomInterface.TelcomRssi = 0;
  162. strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapId, " ");
  163. strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  164. strcpy((char *) SysConfig.TelecomInterface.TelcomModemImei, "");
  165. strcpy((char *) SysConfig.TelecomInterface.TelcomSimImsi, "");
  166. strcpy((char *) SysConfig.TelecomInterface.TelcomSimIccid, "");
  167. SysConfig.TelecomInterface.TelcomSimStatus = 0;
  168. SysConfig.TelecomInterface.TelcomModemMode = 0;
  169. strcpy((char *) SysConfig.TelecomInterface.TelcomIpAddress, "");
  170. SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  171. strcpy((char *)SysConfig.chargePointVendor, "Phihong Technology");
  172. //********** Backend **********//
  173. SysConfig.BackendConnTimeout = 300; //300 seconds
  174. SysConfig.OfflinePolicy = 2;
  175. SysConfig.OfflineMaxChargeEnergy = 0;
  176. SysConfig.OfflineMaxChargeDuration = 0;
  177. strcpy((char *) SysConfig.OcppServerURL, "");
  178. strcpy((char *) SysConfig.ChargeBoxId, "");
  179. SysConfig.LedInfo.Intensity = 2;
  180. //copy default configuration to pointer
  181. memcpy(ptr,&SysConfig,sizeof(struct SysConfigData));
  182. //calculate CRC
  183. Chk=0;
  184. for(i=0;i<(MtdBlockSize-4);i++)
  185. {
  186. Chk+=*(ptr+i);
  187. }
  188. memcpy( ptr+MtdBlockSize-4,&Chk,4);
  189. /*
  190. * Parameter process
  191. */
  192. if (argc > 1)
  193. {
  194. char *arg = argv[1];
  195. switch (arg[0])
  196. {
  197. case '-':
  198. switch (arg[1])
  199. {
  200. case 'a':
  201. outType |= OUTPUT_FILE;
  202. outType |= OUTPUT_FLASH;
  203. break;
  204. case 'f':
  205. outType |= OUTPUT_FILE;
  206. break;
  207. case 'm':
  208. outType |= OUTPUT_FLASH;
  209. break;
  210. default:
  211. helpOutput();
  212. break;
  213. }
  214. break;
  215. default:
  216. helpOutput();
  217. break;
  218. }
  219. }
  220. else
  221. {
  222. helpOutput();
  223. }
  224. /*
  225. * Configuration bin file generate
  226. */
  227. if((outType&OUTPUT_FILE)>0)
  228. {
  229. fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR | O_CREAT);
  230. if (fd < 0)
  231. {
  232. StoreLogMsg("[FactoryConfig]main: open /mnt/FactoryDefaultConfig.bin NG");
  233. free(ptr);
  234. return 0;
  235. }
  236. wrd=write(fd, ptr, MtdBlockSize);
  237. close(fd);
  238. if(wrd<MtdBlockSize)
  239. {
  240. StoreLogMsg("write /mnt/FactoryDefaultConfig.bin NG\r\n");
  241. free(ptr);
  242. return 0;
  243. }
  244. StoreLogMsg("FactoryConfig write to file in /mnt OK.\r\n");
  245. }
  246. /*
  247. * Flash memory write
  248. */
  249. if((outType&OUTPUT_FLASH)>0)
  250. {
  251. // Save factory default setting value to flash factory default setting block
  252. fd = open("/dev/mtdblock12", O_RDWR);
  253. if (fd < 0)
  254. {
  255. StoreLogMsg("open /dev/mtdblock12 NG\r\n");
  256. free(ptr);
  257. return 0;
  258. }
  259. wrd=write(fd, ptr, MtdBlockSize);
  260. close(fd);
  261. if(wrd<MtdBlockSize)
  262. {
  263. StoreLogMsg("write /dev/mtdblock12 NG\r\n");
  264. free(ptr);
  265. return 0;
  266. }
  267. // Save factory default setting value to flash backup setting block
  268. fd = open("/dev/mtdblock11", O_RDWR);
  269. if (fd < 0)
  270. {
  271. StoreLogMsg("open /dev/mtdblock11 NG\r\n");
  272. free(ptr);
  273. return 0;
  274. }
  275. wrd=write(fd, ptr, MtdBlockSize);
  276. close(fd);
  277. if(wrd<MtdBlockSize)
  278. {
  279. StoreLogMsg("write /dev/mtdblock11 NG\r\n");
  280. free(ptr);
  281. return 0;
  282. }
  283. // Save factory default setting value to flash setting block
  284. fd = open("/dev/mtdblock10", O_RDWR);
  285. if (fd < 0)
  286. {
  287. StoreLogMsg("open /dev/mtdblock10 NG\r\n");
  288. free(ptr);
  289. return 0;
  290. }
  291. wrd=write(fd, ptr, MtdBlockSize);
  292. close(fd);
  293. if(wrd<MtdBlockSize)
  294. {
  295. StoreLogMsg("write /dev/mtdblock10 NG\r\n");
  296. free(ptr);
  297. return 0;
  298. }
  299. StoreLogMsg("FactoryConfig write to flash OK\r\n");
  300. }
  301. free(ptr);
  302. return FAIL;
  303. }