Module_FactoryConfig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. va_list args;
  53. va_start(args, fmt);
  54. int rc = vsnprintf(buffer, sizeof(buffer), fmt, args);
  55. va_end(args);
  56. memset(Buf,0,sizeof(Buf));
  57. CurrentTime = time(NULL);
  58. tm=localtime(&CurrentTime);
  59. sprintf(Buf,"echo \"[%04d.%02d.%02d %02d:%02d:%02d] - %s\" >> /Storage/SystemLog/%04d-%02d_%s_%s_SystemLog",
  60. tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec,
  61. buffer,
  62. tm->tm_year+1900,tm->tm_mon+1,
  63. SysConfig.ModelName,
  64. SysConfig.SerialNumber);
  65. #ifdef SystemLogMessage
  66. system(Buf);
  67. #endif
  68. printf("[%04d.%02d.%02d %02d:%02d:%02d] - %s", tm->tm_year+1900,tm->tm_mon+1,tm->tm_mday,tm->tm_hour,tm->tm_min,tm->tm_sec, buffer);
  69. return rc;
  70. }
  71. int DiffTimeb(struct timeb ST, struct timeb ET)
  72. {
  73. //return milli-second
  74. unsigned int StartTime,StopTime;
  75. StartTime=(unsigned int)ST.time;
  76. StopTime=(unsigned int)ET.time;
  77. return (StopTime-StartTime)*1000+ET.millitm-ST.millitm;
  78. }
  79. //=================================
  80. // Common routine
  81. //=================================
  82. void trim(char *s)
  83. {
  84. int i=0, j, k, l=0;
  85. while((s[i]==' ')||(s[i]=='\t')||(s[i]=='\n'))
  86. i++;
  87. j = strlen(s)-1;
  88. while((s[j]==' ')||(s[j]=='\t')||(s[j]=='\n'))
  89. j--;
  90. if(i==0 && j==strlen(s)-1) { }
  91. else if(i==0) s[j+1] = '\0';
  92. else {
  93. for(k=i; k<=j; k++) s[l++] = s[k];
  94. s[l] = '\0';
  95. }
  96. }
  97. int mystrcmp(char *p1,char *p2)
  98. {
  99. while(*p1==*p2)
  100. {
  101. if(*p1=='\0' || *p2=='\0')
  102. break;
  103. p1++;
  104. p2++;
  105. }
  106. if(*p1=='\0' && *p2=='\0')
  107. return(PASS);
  108. else
  109. return(FAIL);
  110. }
  111. void substr(char *dest, const char* src, unsigned int start, unsigned int cnt)
  112. {
  113. strncpy(dest, src + start, cnt);
  114. dest[cnt] = 0;
  115. }
  116. void split(char **arr, char *str, const char *del)
  117. {
  118. char *s = strtok(str, del);
  119. while(s != NULL)
  120. {
  121. *arr++ = s;
  122. s = strtok(NULL, del);
  123. }
  124. }
  125. //==========================================
  126. // Init all share memory
  127. //==========================================
  128. int InitShareMemory()
  129. {
  130. int result = PASS;
  131. int MeterSMId;
  132. //creat ShmSysConfigAndInfo
  133. if ((MeterSMId = shmget(ShmSysConfigAndInfoKey, sizeof(struct SysConfigAndInfo), 0777)) < 0)
  134. {
  135. DEBUG_ERROR("shmget ShmSysConfigAndInfo NG\n");
  136. result = FAIL;
  137. }
  138. else if ((ShmSysConfigAndInfo = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  139. {
  140. DEBUG_ERROR("shmat ShmSysConfigAndInfo NG\n");
  141. result = FAIL;
  142. }
  143. else
  144. {}
  145. //creat ShmStatusCodeData
  146. if ((MeterSMId = shmget(ShmStatusCodeKey, sizeof(struct StatusCodeData), 0777)) < 0)
  147. {
  148. DEBUG_ERROR("shmget ShmStatusCodeData NG\n");
  149. result = FAIL;
  150. }
  151. else if ((ShmStatusCodeData = shmat(MeterSMId, NULL, 0)) == (void *) -1)
  152. {
  153. DEBUG_ERROR("shmat ShmStatusCodeData NG\n");
  154. result = FAIL;
  155. }
  156. else
  157. {}
  158. return result;
  159. }
  160. void helpOutput(void)
  161. {
  162. printf("Usage: Module_FactoryConfig [OPTION]...\r\n\r\n");
  163. printf("Generate factory default configuration value\r\n\r\n");
  164. printf("OPTION:\r\n");
  165. printf(" -a Write to file(/mnt) & flash\r\n");
  166. printf(" -f Write to file(/mnt)\r\n");
  167. printf(" -m Write to flash\r\n");
  168. }
  169. //================================================
  170. // Main process
  171. //================================================
  172. int main(int argc, char *argv[])
  173. {
  174. unsigned char outType=0;
  175. unsigned int i,Chk,MtdBlockSize=0x600000;
  176. unsigned char *ptr;
  177. int fd,wrd;
  178. ptr=malloc(MtdBlockSize);
  179. if(ptr==NULL)
  180. {
  181. #ifdef SystemLogMessage
  182. StoreLogMsg("[FactoryConfig]main: malloc for SysConfigData NG");
  183. #endif
  184. return 0;
  185. }
  186. memset(ptr,0,MtdBlockSize);
  187. memset(&SysConfig,0,sizeof(struct SysConfigData));
  188. /*
  189. * TODO: Set factory default configuration
  190. */
  191. // System configuration
  192. time_t t = time(NULL);
  193. struct tm tm = *localtime(&t);
  194. // Initial Share Memory
  195. if(InitShareMemory() == FAIL)
  196. {
  197. DEBUG_ERROR("InitShareMemory NG\n");
  198. strcpy((char*)SysConfig.ModelName,"");
  199. strcpy((char*)SysConfig.SerialNumber,"");
  200. sleep(5);
  201. }
  202. else
  203. {
  204. memcpy((char*)SysConfig.ModelName,ShmSysConfigAndInfo->SysConfig.ModelName,ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.ModelName));
  205. memcpy((char*)SysConfig.SerialNumber,ShmSysConfigAndInfo->SysConfig.SerialNumber,ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.SerialNumber));
  206. DEBUG_INFO("InitShareMemory OK.\r\n");
  207. }
  208. sprintf((char*)SysConfig.SystemId, "%s%s", SysConfig.ModelName, SysConfig.SerialNumber);
  209. 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);
  210. SysConfig.AuthorisationMode = 0; // 0: enable, 1: disable
  211. 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: ไทย
  212. SysConfig.RfidCardNumEndian = 0; // 0: Little endian 1: Big endian
  213. // Charging configuration
  214. SysConfig.MaxChargingEnergy = 0; // 0: No limit Other: 1~65536 KWH
  215. SysConfig.MaxChargingPower = 0; // 0: No limit Other: 1~65536 KW
  216. SysConfig.MaxChargingCurrent = 0; // 0: Rating value Other: 1~Rating A
  217. SysConfig.MaxChargingDuration = 0; // 0: No limit Other: 1~65536 Minute
  218. SysConfig.PhaseLossPolicy = 0; // 0: Charging 1: Stop charging
  219. SysConfig.AcPhaseCount = 1; // 1: One phase 3: Three phase
  220. // Network configuration
  221. strcpy((char*)SysConfig.FtpServer, "");
  222. SysConfig.Eth0Interface.EthDhcpClient = 0;
  223. strcpy((char*)SysConfig.Eth0Interface.EthIpAddress, "192.168.1.10");
  224. strcpy((char*)SysConfig.Eth0Interface.EthSubmaskAddress, "255.255.255.0");
  225. strcpy((char*)SysConfig.Eth0Interface.EthGatewayAddress, "192.168.1.254");
  226. SysConfig.Eth1Interface.EthDhcpClient = 0;
  227. strcpy((char*)SysConfig.Eth1Interface.EthIpAddress, "192.168.0.10");
  228. strcpy((char*)SysConfig.Eth1Interface.EthSubmaskAddress, "255.255.255.0");
  229. strcpy((char*)SysConfig.Eth1Interface.EthGatewayAddress, "192.168.0.254");
  230. SysConfig.AthInterface.WifiMode = 2; // 0: Disable 1: Infrastructure client 2: Infrastructure server 3: Ad-Hoc
  231. SysConfig.AthInterface.WifiRssi = 0; // Wifi rssi value
  232. SysConfig.AthInterface.WifiDhcpServer = 0; // 0: Enable 1: Disable
  233. SysConfig.AthInterface.WifiDhcpClient = 0; // 0: Enable 1: Disable
  234. strcpy((char*)SysConfig.TelecomInterface.TelcomApn, "");
  235. SysConfig.TelecomInterface.TelcomSimStatus = 0; // SIM card status
  236. SysConfig.TelecomInterface.TelcomModemMode = 0; //0: No services 1: CDMA 2: GSM/GPRS 3: WCDMA 4: GSM/WCDMA 5: TD_SCDMA 6: Unknown
  237. // Backend configuration
  238. SysConfig.BackendConnTimeout=300; // 300 seconds
  239. SysConfig.OfflinePolicy = 2; // 0: local list, 1: Phihong RFID tag, 2: free charging, 3: no charging
  240. SysConfig.OfflineMaxChargeEnergy = 0; // 0: Same as MaxChargeEnergy Other: 1~65535KWH
  241. SysConfig.OfflineMaxChargeDuration = 0; // 0: Same as MaxChargeDuration Other: 1~65535 minutes
  242. strcpy((char*)SysConfig.OcppServerURL, "");
  243. sprintf((char*)SysConfig.ChargeBoxId, "%s%s", SysConfig.ModelName, SysConfig.SerialNumber);
  244. // Copy default configuration to pointer
  245. memcpy(ptr,&SysConfig,sizeof(struct SysConfigData));
  246. // Calculate CRC
  247. Chk=0;
  248. for(i=0;i<(MtdBlockSize-4);i++)
  249. {
  250. Chk+=*(ptr+i);
  251. }
  252. memcpy( ptr+MtdBlockSize-4,&Chk,4);
  253. /*
  254. * Parameter process
  255. */
  256. if(argc>1)
  257. {
  258. char *arg = argv[1];
  259. switch(arg[0])
  260. {
  261. case '-':
  262. switch(arg[1])
  263. {
  264. case 'a':
  265. outType |= OUTPUT_FILE;
  266. outType |= OUTPUT_FLASH;
  267. break;
  268. case 'f':
  269. outType |= OUTPUT_FILE;
  270. break;
  271. case 'm':
  272. outType |= OUTPUT_FLASH;
  273. break;
  274. default:
  275. helpOutput();
  276. break;
  277. }
  278. break;
  279. default:
  280. helpOutput();
  281. break;
  282. }
  283. }
  284. else
  285. {
  286. helpOutput();
  287. }
  288. /*
  289. * Configuration bin file generate
  290. */
  291. if((outType&OUTPUT_FILE)>0)
  292. {
  293. // Save factory default setting value to file
  294. fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR|O_CREAT);
  295. if (fd < 0)
  296. {
  297. DEBUG_ERROR("open /mnt/FactoryDefaultConfig.bin NG\r\n");
  298. free(ptr);
  299. return 0;
  300. }
  301. wrd=write(fd, ptr, MtdBlockSize);
  302. close(fd);
  303. if(wrd<MtdBlockSize)
  304. {
  305. DEBUG_ERROR("write /mnt/FactoryDefaultConfig.bin NG\r\n");
  306. free(ptr);
  307. return 0;
  308. }
  309. DEBUG_INFO("FactoryConfig write to file in /mnt OK.\r\n");
  310. }
  311. /*
  312. * Flash memory write
  313. */
  314. if((outType&OUTPUT_FLASH)>0)
  315. {
  316. // Save factory default setting value to flash factory default setting block
  317. fd = open("/dev/mtdblock12", O_RDWR);
  318. if (fd < 0)
  319. {
  320. DEBUG_ERROR("open /dev/mtdblock12 NG\r\n");
  321. free(ptr);
  322. return 0;
  323. }
  324. wrd=write(fd, ptr, MtdBlockSize);
  325. close(fd);
  326. if(wrd<MtdBlockSize)
  327. {
  328. DEBUG_ERROR("write /dev/mtdblock12 NG\r\n");
  329. free(ptr);
  330. return 0;
  331. }
  332. // Save factory default setting value to flash backup setting block
  333. fd = open("/dev/mtdblock11", O_RDWR);
  334. if (fd < 0)
  335. {
  336. DEBUG_ERROR("open /dev/mtdblock11 NG\r\n");
  337. free(ptr);
  338. return 0;
  339. }
  340. wrd=write(fd, ptr, MtdBlockSize);
  341. close(fd);
  342. if(wrd<MtdBlockSize)
  343. {
  344. DEBUG_ERROR("write /dev/mtdblock11 NG\r\n");
  345. free(ptr);
  346. return 0;
  347. }
  348. // Save factory default setting value to flash setting block
  349. fd = open("/dev/mtdblock10", O_RDWR);
  350. if (fd < 0)
  351. {
  352. DEBUG_ERROR("open /dev/mtdblock10 NG\r\n");
  353. free(ptr);
  354. return 0;
  355. }
  356. wrd=write(fd, ptr, MtdBlockSize);
  357. close(fd);
  358. if(wrd<MtdBlockSize)
  359. {
  360. DEBUG_ERROR("write /dev/mtdblock10 NG\r\n");
  361. free(ptr);
  362. return 0;
  363. }
  364. DEBUG_INFO("FactoryConfig write to flash OK\r\n");
  365. }
  366. free(ptr);
  367. return FAIL;
  368. }