FactoryConfig.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. struct SysConfigData SysConfig;
  39. int StoreLogMsg(const char *fmt, ...);
  40. int StoreLogMsg(const char *fmt, ...)
  41. {
  42. char Buf[4096+256];
  43. char buffer[4096];
  44. va_list args;
  45. struct timeb SeqEndTime;
  46. struct tm *tm;
  47. va_start(args, fmt);
  48. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  49. va_end(args);
  50. memset(Buf,0,sizeof(Buf));
  51. ftime(&SeqEndTime);
  52. SeqEndTime.time = time(NULL);
  53. tm=localtime(&SeqEndTime.time);
  54. sprintf(Buf,"echo \"%04d-%02d-%02d %02d:%02d:%02d.%03d - %s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  55. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,SeqEndTime.millitm,
  56. buffer,
  57. tm->tm_year+1900,tm->tm_mon+1);
  58. system(Buf);
  59. return rc;
  60. }
  61. void helpOutput(void)
  62. {
  63. printf("Usage: Module_FactoryConfig [OPTION]...\r\n\r\n");
  64. printf("Generate factory default configuration value\r\n\r\n");
  65. printf("OPTION:\r\n");
  66. printf(" -a Write to file(/mnt) & flash\r\n");
  67. printf(" -f Write to file(/mnt)\r\n");
  68. printf(" -m Write to flash\r\n");
  69. }
  70. /**************************************************************************************/
  71. /************This task will create Factory default confgiuration file *****************/
  72. /************and store it into mtdblock 10,11,12 ****************/
  73. /**************************************************************************************/
  74. int main(int argc,char *argv[])
  75. {
  76. unsigned char outType=0;
  77. unsigned int i,Chk, MtdBlockSize=0x600000;
  78. unsigned char *ptr;
  79. int fd,wrd;
  80. ptr=malloc(MtdBlockSize);
  81. if(ptr==NULL)
  82. {
  83. #ifdef SystemLogMessage
  84. StoreLogMsg("[FactoryConfig]main: malloc for SysConfigData NG");
  85. #endif
  86. return 0;
  87. }
  88. memset(ptr, 0, MtdBlockSize);
  89. memset(&SysConfig, 0, sizeof(struct SysConfigData));
  90. /*
  91. * TODO: Set factory default configuration
  92. */
  93. //********** System **********// udhcpc -i eth1 -s ./dhcp_script/eth1.script
  94. //
  95. strcpy((char *)SysConfig.ModelName, "DWWU301U00W1PH");
  96. strcpy((char *)SysConfig.SerialNumber, "NeedSetupSN");
  97. memset(SysConfig.SystemId, 0x00, sizeof(SysConfig.SystemId));
  98. char Dash = '-';
  99. strcat((char *)SysConfig.SystemId, (char *)SysConfig.ModelName);
  100. strncat((char *)SysConfig.SystemId, &Dash, 1);
  101. strcat((char *)SysConfig.SystemId, (char *)SysConfig.SerialNumber);
  102. strcpy((char *)SysConfig.SystemDateTime, "");
  103. SysConfig.AuthorisationMode = AUTH_MODE_ENABLE;
  104. SysConfig.DefaultLanguage = 0;
  105. SysConfig.RfidCardNumEndian = 0;
  106. SysConfig.AcPlugInTimes = 0;
  107. SysConfig.GbPlugInTimes = 0;
  108. SysConfig.Ccs1PlugInTime = 0;
  109. SysConfig.Ccs2PlugInTimes = 0;
  110. SysConfig.ChademoPlugInTimes = 0;
  111. SysConfig.BillingData.isBilling = 0;
  112. SysConfig.isAPP = 1;
  113. SysConfig.isQRCode = 1;
  114. SysConfig.isRFID = 1;
  115. //********** Charging **********//
  116. SysConfig.MaxChargingEnergy = 0;
  117. SysConfig.MaxChargingCurrent = 60; // 最大可輸出電流 (整樁)
  118. SysConfig.MaxChargingDuration = 0;
  119. SysConfig.AcMaxChargingCurrent = 0;
  120. SysConfig.PhaseLossPolicy = LOSS_POLICY_STOP;
  121. for(unsigned char i = 0; i < 10; i++)
  122. strcpy((char *)SysConfig.LocalWhiteCard, "");
  123. strcpy((char *)SysConfig.UserId, "");
  124. //********** Network **********//
  125. strcpy((char *)SysConfig.FtpServer, "");
  126. SysConfig.Eth0Interface.EthDhcpClient = 0;
  127. strcpy((char *) SysConfig.Eth0Interface.EthIpAddress, "192.168.1.10");
  128. strcpy((char *) SysConfig.Eth0Interface.EthSubmaskAddress, "255.255.255.0");
  129. strcpy((char *) SysConfig.Eth0Interface.EthGatewayAddress, "192.168.1.254");
  130. SysConfig.Eth1Interface.EthDhcpClient = 0;
  131. strcpy((char *) SysConfig.Eth1Interface.EthIpAddress, "192.168.0.10");
  132. strcpy((char *) SysConfig.Eth1Interface.EthSubmaskAddress, "255.255.255.0");
  133. strcpy((char *) SysConfig.Eth1Interface.EthGatewayAddress, "192.168.0.254");
  134. if(SysConfig.ModelName[10] == 'W')
  135. SysConfig.AthInterface.WifiMode = _SYS_WIFI_MODE_AP;
  136. else
  137. SysConfig.AthInterface.WifiMode = _SYS_WIFI_MODE_DISABLE;
  138. strcpy((char *) SysConfig.AthInterface.WifiSsid, "");
  139. strcpy((char *) SysConfig.AthInterface.WifiPassword, "");
  140. SysConfig.AthInterface.WifiRssi = 0;
  141. SysConfig.AthInterface.WifiDhcpServer = 0;
  142. SysConfig.AthInterface.WifiDhcpClient = 0;
  143. strcpy((char *) SysConfig.AthInterface.WifiMacAddress, "");
  144. strcpy((char *) SysConfig.AthInterface.WifiIpAddress, "");
  145. strcpy((char *) SysConfig.AthInterface.WifiSubmaskAddress, "");
  146. strcpy((char *) SysConfig.AthInterface.WifiGatewayAddress, "");
  147. SysConfig.AthInterface.WifiNetworkConn = 0;
  148. strcpy((char *) SysConfig.TelecomInterface.TelcomModelName, "");
  149. strcpy((char *) SysConfig.TelecomInterface.TelcomSoftwareVer, "");
  150. //strcpy((char *) SysConfig.TelecomInterface.TelcomApn, "Internet");
  151. strcpy((char *) SysConfig.TelecomInterface.TelcomApn, "");
  152. SysConfig.TelecomInterface.TelcomRssi = 0;
  153. strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapId, " ");
  154. strcpy((char *) SysConfig.TelecomInterface.TelcomChapPapPwd, " ");
  155. strcpy((char *) SysConfig.TelecomInterface.TelcomModemImei, "");
  156. strcpy((char *) SysConfig.TelecomInterface.TelcomSimImsi, "");
  157. strcpy((char *) SysConfig.TelecomInterface.TelcomSimIccid, "");
  158. SysConfig.TelecomInterface.TelcomSimStatus = 0;
  159. SysConfig.TelecomInterface.TelcomModemMode = 0;
  160. strcpy((char *) SysConfig.TelecomInterface.TelcomIpAddress, "");
  161. SysConfig.TelecomInterface.TelcomNetworkConn = 0;
  162. strcpy((char *)SysConfig.chargePointVendor, "Phihong Technology");
  163. //********** Backend **********//
  164. SysConfig.BackendConnTimeout = 300; //300 seconds
  165. SysConfig.OfflinePolicy = OFF_POLICY_FREE;
  166. SysConfig.OfflineMaxChargeEnergy = 0;
  167. SysConfig.OfflineMaxChargeDuration = 0;
  168. //strcpy((char *) SysConfig.OcppServerURL, "ws://test.evsocket.phihong.com.cn:2012/");
  169. //strcpy((char *) SysConfig.ChargeBoxId, "DemoDC");
  170. strcpy((char *) SysConfig.OcppServerURL, "");
  171. strcpy((char *) SysConfig.ChargeBoxId, "");
  172. SysConfig.LedInfo.Intensity = 2;
  173. //copy default configuration to pointer
  174. memcpy(ptr,&SysConfig,sizeof(struct SysConfigData));
  175. //calculate CRC
  176. Chk=0;
  177. for(i=0;i<(MtdBlockSize-4);i++)
  178. {
  179. Chk+=*(ptr+i);
  180. }
  181. memcpy( ptr+MtdBlockSize-4,&Chk,4);
  182. /*
  183. * Parameter process
  184. */
  185. if (argc > 1)
  186. {
  187. char *arg = argv[1];
  188. switch (arg[0])
  189. {
  190. case '-':
  191. switch (arg[1])
  192. {
  193. case 'a':
  194. outType |= OUTPUT_FILE;
  195. outType |= OUTPUT_FLASH;
  196. break;
  197. case 'f':
  198. outType |= OUTPUT_FILE;
  199. break;
  200. case 'm':
  201. outType |= OUTPUT_FLASH;
  202. break;
  203. default:
  204. helpOutput();
  205. break;
  206. }
  207. break;
  208. default:
  209. helpOutput();
  210. break;
  211. }
  212. }
  213. else
  214. {
  215. helpOutput();
  216. }
  217. /*
  218. * Configuration bin file generate
  219. */
  220. if((outType&OUTPUT_FILE)>0)
  221. {
  222. fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR | O_CREAT);
  223. if (fd < 0)
  224. {
  225. StoreLogMsg("[FactoryConfig]main: open /mnt/FactoryDefaultConfig.bin NG");
  226. free(ptr);
  227. return 0;
  228. }
  229. wrd=write(fd, ptr, MtdBlockSize);
  230. close(fd);
  231. if(wrd<MtdBlockSize)
  232. {
  233. StoreLogMsg("write /mnt/FactoryDefaultConfig.bin NG\r\n");
  234. free(ptr);
  235. return 0;
  236. }
  237. StoreLogMsg("FactoryConfig write to file in /mnt OK.\r\n");
  238. }
  239. /*
  240. * Flash memory write
  241. */
  242. if((outType&OUTPUT_FLASH)>0)
  243. {
  244. // Save factory default setting value to flash factory default setting block
  245. fd = open("/dev/mtdblock12", O_RDWR);
  246. if (fd < 0)
  247. {
  248. StoreLogMsg("open /dev/mtdblock12 NG\r\n");
  249. free(ptr);
  250. return 0;
  251. }
  252. wrd=write(fd, ptr, MtdBlockSize);
  253. close(fd);
  254. if(wrd<MtdBlockSize)
  255. {
  256. StoreLogMsg("write /dev/mtdblock12 NG\r\n");
  257. free(ptr);
  258. return 0;
  259. }
  260. // Save factory default setting value to flash backup setting block
  261. fd = open("/dev/mtdblock11", O_RDWR);
  262. if (fd < 0)
  263. {
  264. StoreLogMsg("open /dev/mtdblock11 NG\r\n");
  265. free(ptr);
  266. return 0;
  267. }
  268. wrd=write(fd, ptr, MtdBlockSize);
  269. close(fd);
  270. if(wrd<MtdBlockSize)
  271. {
  272. StoreLogMsg("write /dev/mtdblock11 NG\r\n");
  273. free(ptr);
  274. return 0;
  275. }
  276. // Save factory default setting value to flash setting block
  277. fd = open("/dev/mtdblock10", O_RDWR);
  278. if (fd < 0)
  279. {
  280. StoreLogMsg("open /dev/mtdblock10 NG\r\n");
  281. free(ptr);
  282. return 0;
  283. }
  284. wrd=write(fd, ptr, MtdBlockSize);
  285. close(fd);
  286. if(wrd<MtdBlockSize)
  287. {
  288. StoreLogMsg("write /dev/mtdblock10 NG\r\n");
  289. free(ptr);
  290. return 0;
  291. }
  292. StoreLogMsg("FactoryConfig write to flash OK\r\n");
  293. }
  294. free(ptr);
  295. return FAIL;
  296. }