Module_FactoryConfig.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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/wireless.h>
  13. #include <arpa/inet.h>
  14. #include <netinet/in.h>
  15. #include <unistd.h>
  16. #include <stdarg.h>
  17. #include <stdio.h> /*標準輸入輸出定義*/
  18. #include <stdlib.h> /*標準函數庫定義*/
  19. #include <unistd.h> /*Unix 標準函數定義*/
  20. #include <fcntl.h> /*檔控制定義*/
  21. #include <termios.h> /*PPSIX 終端控制定義*/
  22. #include <errno.h> /*錯誤號定義*/
  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. #define DEBUG_INFO(format, args...) StoreLogMsg("[%s:%d][%s][Info] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  30. #define DEBUG_WARN(format, args...) StoreLogMsg("[%s:%d][%s][Warn] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  31. #define DEBUG_ERROR(format, args...) StoreLogMsg("[%s:%d][%s][Error] "format, __FILE__, __LINE__, __FUNCTION__, ##args)
  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. struct SysConfigAndInfo *ShmSysConfigAndInfo;
  40. struct StatusCodeData *ShmStatusCodeData;
  41. struct FanModuleData *ShmFanModuleData;
  42. void trim(char *s);
  43. int mystrcmp(char *p1,char *p2);
  44. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
  45. void split(char **arr, char *str, const char *del);
  46. int StoreLogMsg(const char *fmt, ...)
  47. {
  48. char Buf[4096+256];
  49. char buffer[4096];
  50. time_t CurrentTime;
  51. struct tm *tm;
  52. struct timeval tv;
  53. va_list args;
  54. va_start(args, fmt);
  55. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  56. va_end(args);
  57. memset(Buf,0,sizeof(Buf));
  58. CurrentTime = time(NULL);
  59. tm=localtime(&CurrentTime);
  60. gettimeofday(&tv, NULL); // get microseconds, 10^-6
  61. if((ShmSysConfigAndInfo->SysConfig.ModelName != NULL) && (ShmSysConfigAndInfo->SysConfig.SerialNumber != NULL) && (strlen((char*)ShmSysConfigAndInfo->SysConfig.ModelName) >= 14))
  62. {
  63. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]-%s\" >> /Storage/SystemLog/[%04d.%02d]%s_%s_SystemLog",
  64. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  65. buffer,
  66. tm->tm_year+1900,tm->tm_mon+1,
  67. ShmSysConfigAndInfo->SysConfig.ModelName,
  68. ShmSysConfigAndInfo->SysConfig.SerialNumber);
  69. }
  70. else
  71. {
  72. sprintf(Buf,"echo -n \"[%04d.%02d.%02d %02d:%02d:%02d.%06ld]-%s\" >> /Storage/SystemLog/[%04d.%02d]SystemLog",
  73. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec,
  74. buffer,
  75. tm->tm_year+1900,tm->tm_mon+1);
  76. }
  77. #ifdef SystemLogMessage
  78. system(Buf);
  79. #endif
  80. #ifdef ConsloePrintLog
  81. printf("[%04d.%02d.%02d %02d:%02d:%02d.%06ld] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,tv.tv_usec, buffer);
  82. #endif
  83. return rc;
  84. }
  85. int DiffTimeb(struct timeb ST, struct timeb ET)
  86. {
  87. //return milli-second
  88. unsigned int StartTime,StopTime;
  89. StartTime=(unsigned int)ST.time;
  90. StopTime=(unsigned int)ET.time;
  91. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  92. }
  93. //=================================
  94. // Common routine
  95. //=================================
  96. void trim(char *s)
  97. {
  98. int i=0, j, k, l=0;
  99. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  100. i++;
  101. j = strlen(s)-1;
  102. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  103. j--;
  104. if(i==0 && j==strlen(s)-1) { }
  105. else if(i==0) s[j+1] = '\0';
  106. else {
  107. for(k=i; k<=j; k++) s[l++] = s[k];
  108. s[l] = '\0';
  109. }
  110. }
  111. int mystrcmp(char *p1,char *p2)
  112. {
  113. while(*p1==*p2)
  114. {
  115. if(*p1=='\0' || *p2=='\0')
  116. break;
  117. p1++;
  118. p2++;
  119. }
  120. if(*p1=='\0' && *p2=='\0')
  121. return(PASS);
  122. else
  123. return(FAIL);
  124. }
  125. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  126. {
  127. strncpy(dest, src + start, cnt);
  128. dest[cnt] = 0;
  129. }
  130. void split(char **arr, char *str, const char *del)
  131. {
  132. char *s = strtok(str, del);
  133. while(s != NULL)
  134. {
  135. *arr++ = s;
  136. s = strtok(NULL, del);
  137. }
  138. }
  139. int runShellCmd(const char*cmd)
  140. {
  141. int result = FAIL;
  142. char buf[256];
  143. FILE *fp;
  144. fp = popen(cmd, "r");
  145. if(fp != NULL)
  146. {
  147. while(fgets(buf, sizeof(buf), fp) != NULL)
  148. {
  149. DEBUG_INFO("%s\n", buf);
  150. }
  151. result = PASS;
  152. }
  153. pclose(fp);
  154. return result;
  155. }
  156. //==========================================
  157. // Init all share memory
  158. //==========================================
  159. int InitShareMemory()
  160. {
  161. int result = PASS;
  162. int MeterSMId;
  163. //creat ShmSysConfigAndInfo
  164. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  165. {
  166. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  167. result = FAIL;
  168. }
  169. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  170. {
  171. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  172. result = FAIL;
  173. }
  174. else
  175. {}
  176. //creat ShmStatusCodeData
  177. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  178. {
  179. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  180. result = FAIL;
  181. }
  182. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  183. {
  184. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  185. result = FAIL;
  186. }
  187. else
  188. {}
  189. return result;
  190. }
  191. void helpOutput(void)
  192. {
  193. printf("Usage: Module_FactoryConfig [OPTION]...\n\n");
  194. printf("Generate factory default configuration value\n\n");
  195. printf("OPTION:\n");
  196. printf(" -a Write to file(/mnt) & flash\n");
  197. printf(" -f Write to file(/mnt)\n");
  198. printf(" -m Write to flash\n");
  199. }
  200. //================================================
  201. // Main process
  202. //================================================
  203. int main(int argc, char *argv[])
  204. {
  205. unsigned char outType=0;
  206. unsigned int i,Chk,MtdBlockSize=0x600000;
  207. unsigned char *ptr;
  208. int fd,wrd;
  209. ptr=malloc(MtdBlockSize);
  210. if(ptr==NULL)
  211. {
  212. #ifdef SystemLogMessage
  213. StoreLogMsg("[FactoryConfig]main: malloc for SysConfigData NG");
  214. #endif
  215. return 0;
  216. }
  217. memset(ptr,0,MtdBlockSize);
  218. memset(&SysConfig,0,sizeof(struct SysConfigData));
  219. /*
  220. * TODO: Set factory default configuration
  221. */
  222. // System configuration
  223. time_t t = time(NULL);
  224. struct tm tm = *localtime(&t);
  225. // Initial Share Memory
  226. if(InitShareMemory() == FAIL)
  227. {
  228. DEBUG_ERROR("InitShareMemory NG\n");
  229. strcpy((char*)SysConfig.ModelName, "");
  230. strcpy((char*)SysConfig.SerialNumber, "");
  231. sleep(5);
  232. }
  233. else
  234. {
  235. memcpy((char*)SysConfig.ModelName, ShmSysConfigAndInfo->SysConfig.ModelName, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  236. memcpy((char*)SysConfig.SerialNumber, ShmSysConfigAndInfo->SysConfig.SerialNumber, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  237. DEBUG_INFO("InitShareMemory OK.\n");
  238. }
  239. sprintf((char*)SysConfig.SystemId, "%s%s", SysConfig.ModelName, SysConfig.SerialNumber);
  240. sprintf((char*)SysConfig.SystemDateTime, "%d-%d-%d %d:%d:%d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
  241. SysConfig.AuthorisationMode = 0; // 0: enable, 1: disable
  242. SysConfig.DefaultLanguage = 0; // 0:English 1:Big5 2: GB 3: JN 4: Français 5: Italiano 6: Español 7: Deutsch 8: Nederland 9: Norsk 10: Suomalainen 11: Svenska 12: Pусский 13: ไทย
  243. SysConfig.RfidCardNumEndian = 1; // 0: Little endian 1: Big endian
  244. // Charging configuration
  245. SysConfig.MaxChargingEnergy = 0; // 0: No limit Other: 1~65536 KWH
  246. SysConfig.MaxChargingPower = 0; // 0: No limit Other: 1~65536 KW
  247. SysConfig.MaxChargingCurrent = 0; // 0: Rating value Other: 1~Rating A
  248. SysConfig.MaxChargingDuration = 0; // 0: No limit Other: 1~65536 Minute
  249. SysConfig.PhaseLossPolicy = 0; // 0: Charging 1: Stop charging
  250. SysConfig.AcPhaseCount = 1; // 1: One phase 3: Three phase
  251. // Network configuration
  252. strcpy((char*)SysConfig.FtpServer, "");
  253. SysConfig.Eth0Interface.EthDhcpClient = 0;
  254. strcpy((char*)SysConfig.Eth0Interface.EthIpAddress, "192.168.1.10");
  255. strcpy((char*)SysConfig.Eth0Interface.EthSubmaskAddress, "255.255.255.0");
  256. strcpy((char*)SysConfig.Eth0Interface.EthGatewayAddress, "192.168.1.254");
  257. SysConfig.Eth1Interface.EthDhcpClient = 0;
  258. strcpy((char*)SysConfig.Eth1Interface.EthIpAddress, "192.168.0.10");
  259. strcpy((char*)SysConfig.Eth1Interface.EthSubmaskAddress, "255.255.255.0");
  260. strcpy((char*)SysConfig.Eth1Interface.EthGatewayAddress, "192.168.0.254");
  261. SysConfig.AthInterface.WifiMode = 1; // 0: Disable 1: Infrastructure client 2: Infrastructure server 3: Ad-Hoc
  262. strcpy((char*)SysConfig.AthInterface.WifiSsid, "ChargeLab-EVC");
  263. strcpy((char*)SysConfig.AthInterface.WifiPassword, "evc-pwd-default-21");
  264. SysConfig.AthInterface.WifiRssi = 0; // Wifi rssi value
  265. SysConfig.AthInterface.WifiDhcpServer = 0; // 0: Enable 1: Disable
  266. SysConfig.AthInterface.WifiDhcpClient = 0; // 0: Enable 1: Disable
  267. strcpy((char*)SysConfig.TelecomInterface.TelcomApn, "m2minternet.apn");
  268. SysConfig.TelecomInterface.TelcomEnabled = 1; // 0: disable, 1: enable
  269. SysConfig.TelecomInterface.TelcomSimStatus = 0; // SIM card status
  270. SysConfig.TelecomInterface.TelcomModemMode = 0; // 0: No services 1: CDMA 2: GSM/GPRS 3: WCDMA 4: GSM/WCDMA 5: TD_SCDMA 6: Unknown
  271. // Backend configuration
  272. strcpy((char*)SysConfig.OcppServerURL, "");
  273. sprintf((char*)SysConfig.ChargeBoxId, "%s%s", SysConfig.ModelName, SysConfig.SerialNumber);
  274. SysConfig.BackendConnTimeout=300; // 300 seconds
  275. SysConfig.OfflinePolicy = 0; // 0: local list, 1: Phihong RFID tag, 2: free charging, 3: no charging
  276. SysConfig.OfflineMaxChargeEnergy = 0; // 0: Same as MaxChargeEnergy Other: 1~65535KWH
  277. SysConfig.OfflineMaxChargeDuration = 0; // 0: Same as MaxChargeDuration Other: 1~65535 minutes
  278. //SysConfig.isReqFirstUpgrade = 1; // 0: Skip first upgrade, 1: Process first upgrade
  279. // Customization configuration item
  280. if(SysConfig.ModelName[12] == 'A')
  281. {
  282. strcpy((char*)SysConfig.OcppServerURL, "wss://ocpp.io");
  283. sprintf((char*)SysConfig.ChargeBoxId, "%s%s", SysConfig.ModelName, SysConfig.SerialNumber);
  284. }
  285. else
  286. {
  287. }
  288. // Copy default configuration to pointer
  289. memcpy(ptr,&SysConfig,sizeof(struct SysConfigData));
  290. // Calculate CRC
  291. Chk=0;
  292. for(i=0;i<(MtdBlockSize-4);i++)
  293. {
  294. Chk+=*(ptr+i);
  295. }
  296. memcpy(ptr+MtdBlockSize-4, &Chk, 4);
  297. /*
  298. * Parameter process
  299. */
  300. if(argc>1)
  301. {
  302. char *arg = argv[1];
  303. switch(arg[0])
  304. {
  305. case '-':
  306. switch(arg[1])
  307. {
  308. case 'a':
  309. outType |= OUTPUT_FILE;
  310. outType |= OUTPUT_FLASH;
  311. break;
  312. case 'f':
  313. outType |= OUTPUT_FILE;
  314. break;
  315. case 'm':
  316. outType |= OUTPUT_FLASH;
  317. break;
  318. default:
  319. helpOutput();
  320. break;
  321. }
  322. break;
  323. default:
  324. helpOutput();
  325. break;
  326. }
  327. }
  328. else
  329. {
  330. helpOutput();
  331. }
  332. /*
  333. * Configuration bin file generate
  334. */
  335. // Save factory default setting value to file
  336. fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR|O_CREAT);
  337. if (fd < 0)
  338. {
  339. DEBUG_ERROR("open /mnt/FactoryDefaultConfig.bin NG\n");
  340. free(ptr);
  341. return 0;
  342. }
  343. wrd=write(fd, ptr, MtdBlockSize);
  344. close(fd);
  345. if(wrd<MtdBlockSize)
  346. {
  347. DEBUG_ERROR("write /mnt/FactoryDefaultConfig.bin NG\n");
  348. free(ptr);
  349. return 0;
  350. }
  351. DEBUG_INFO("FactoryConfig write to file in /mnt OK.\n");
  352. /*
  353. * Flash memory write
  354. */
  355. if((outType&OUTPUT_FLASH)>0)
  356. {
  357. DEBUG_INFO("Erase /dev/mtd10.\n");
  358. runShellCmd("flash_erase /dev/mtd10 0 12");
  359. DEBUG_INFO("Write /dev/mtd10.\n");
  360. runShellCmd("nandwrite -p /dev/mtd10 /mnt/FactoryDefaultConfig.bin");
  361. DEBUG_INFO("Erase /dev/mtd11.\n");
  362. runShellCmd("flash_erase /dev/mtd11 0 12");
  363. DEBUG_INFO("Write /dev/mtd11.\n");
  364. runShellCmd("nandwrite -p /dev/mtd11 /mnt/FactoryDefaultConfig.bin");
  365. DEBUG_INFO("Erase /dev/mtd12.\n");
  366. runShellCmd("flash_erase /dev/mtd12 0 12");
  367. DEBUG_INFO("Write /dev/mtd12.\n");
  368. runShellCmd("nandwrite -p /dev/mtd12 /mnt/FactoryDefaultConfig.bin");
  369. system("rm -f /mnt/FactoryDefaultConfig.bin");
  370. DEBUG_INFO("FactoryConfig write to flash OK\n");
  371. }
  372. free(ptr);
  373. return PASS;
  374. }