FactoryConfig.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. /**************************************************************************************/
  33. /************This task will create Factory default confgiuration file *****************/
  34. /***********and store it into mtdblock 10,11,12 ****************/
  35. /**************************************************************************************/
  36. int main(int argc,char *argv[])
  37. {
  38. struct SysConfigData SysConfig;
  39. unsigned int i,Chk;
  40. unsigned char *ptr;
  41. int fd,wrd;
  42. ptr=malloc(sizeof(struct SysConfigData));
  43. if(ptr==NULL)
  44. {
  45. #ifdef SystemLogMessage
  46. StoreLogMsg("[FactoryConfig]main: malloc for SysConfigData NG");
  47. #endif
  48. return 0;
  49. }
  50. memset(ptr,0,sizeof(struct SysConfigData));
  51. memset(&SysConfig,0,sizeof(struct SysConfigData));
  52. //Set default configuration
  53. strcpy((char *)SysConfig.Eth0Interface.EthIpAddress, "192.168.1.10");
  54. strcpy((char *)SysConfig.Eth0Interface.EthSubmaskAddress,"255.255.255.0");
  55. strcpy((char *)SysConfig.Eth0Interface.EthGatewayAddress,"192.168.1.254");
  56. strcpy((char *)SysConfig.Eth1Interface.EthIpAddress,"192.168.0.10");
  57. strcpy((char *)SysConfig.Eth1Interface.EthSubmaskAddress,"255.255.255.0");
  58. strcpy((char *)SysConfig.Eth1Interface.EthGatewayAddress,"192.168.0.254");
  59. SysConfig.BackendConnTimeout=300; //300 seconds
  60. //copy default configuration to pointer
  61. memcpy(ptr,&SysConfig,sizeof(struct SysConfigData));
  62. //calculate CRC
  63. Chk=0;
  64. for(i=0;i<(sizeof(struct SysConfigData)-4);i++)
  65. {
  66. Chk+=*(ptr+i);
  67. }
  68. SysConfig.Checksum=Chk;
  69. fd = open("/mnt/FactoryDefaultConfig.bin", O_RDWR|O_CREAT);
  70. if (fd < 0)
  71. {
  72. #ifdef SystemLogMessage
  73. StoreLogMsg("[FactoryConfig]main: open /mnt/FactoryDefaultConfig.bin NG");
  74. #endif
  75. free(ptr);
  76. return 0;
  77. }
  78. wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
  79. close(fd);
  80. if(wrd!=(sizeof(struct SysConfigData)))
  81. {
  82. #ifdef SystemLogMessage
  83. StoreLogMsg("[FactoryConfig]main: write /mnt/FactoryDefaultConfig.bin NG");
  84. #endif
  85. free(ptr);
  86. return 0;
  87. }
  88. fd = open("/dev/mtdblock12", O_RDWR);
  89. if (fd < 0)
  90. {
  91. #ifdef SystemLogMessage
  92. StoreLogMsg("[FactoryConfig]main: open /dev/mtdblock12 NG");
  93. #endif
  94. free(ptr);
  95. return 0;
  96. }
  97. wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
  98. close(fd);
  99. if(wrd!=(sizeof(struct SysConfigData)))
  100. {
  101. #ifdef SystemLogMessage
  102. StoreLogMsg("[FactoryConfig]main: write /dev/mtdblock12 NG");
  103. #endif
  104. free(ptr);
  105. return 0;
  106. }
  107. fd = open("/dev/mtdblock11", O_RDWR);
  108. if (fd < 0)
  109. {
  110. #ifdef SystemLogMessage
  111. StoreLogMsg("[FactoryConfig]main: open /dev/mtdblock11 NG");
  112. #endif
  113. free(ptr);
  114. return 0;
  115. }
  116. wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
  117. close(fd);
  118. if(wrd!=(sizeof(struct SysConfigData)))
  119. {
  120. #ifdef SystemLogMessage
  121. StoreLogMsg("[FactoryConfig]main: write /dev/mtdblock11 NG");
  122. #endif
  123. free(ptr);
  124. return 0;
  125. }
  126. fd = open("/dev/mtdblock10", O_RDWR);
  127. if (fd < 0)
  128. {
  129. #ifdef SystemLogMessage
  130. StoreLogMsg("[FactoryConfig]main: open /dev/mtdblock10 NG");
  131. #endif
  132. free(ptr);
  133. return 0;
  134. }
  135. wrd=write(fd, &SysConfig, sizeof(struct SysConfigData));
  136. close(fd);
  137. if(wrd!=(sizeof(struct SysConfigData)))
  138. {
  139. #ifdef SystemLogMessage
  140. StoreLogMsg("[FactoryConfig]main: write /dev/mtdblock10 NG");
  141. #endif
  142. free(ptr);
  143. return 0;
  144. }
  145. free(ptr);
  146. #ifdef SystemLogMessage
  147. StoreLogMsg("[FactoryConfig]main: FactoryConfig OK");
  148. #endif
  149. }