FactoryConfig.c 11 KB

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