main.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. #include "cfg.h"
  5. #include "fs.h"
  6. #include <stdio.h>
  7. #include "md5.h"
  8. #include "led.h"
  9. #include "rtthread.h"
  10. #include "dfs_posix.h"
  11. //#include "channel_display.h"
  12. //#include "data_js.h"
  13. //#include "display_data.h"
  14. //#include "index_css.h"
  15. #include "index_html.h"
  16. //#include "index_js.h"
  17. //#include "operation.h"
  18. //#include "petite-vue.min.h"
  19. //#include "sys_para.h"
  20. //#include "ws.h"
  21. //https://embedded-rd.blog.csdn.net/article/details/109643188
  22. #define CHAR(b) (((b)<=9)?((b)+0x30):((b)-10+0x61));
  23. static void byte2char(char *chr, uint8_t b)
  24. {
  25. chr[0] = CHAR(b>>4);
  26. chr[1] = CHAR(b&0x0f);
  27. }
  28. //char * test_txt ="sadasdasjkdjalkdfnlakdfnjo'ajdfrlkasdjflkjasdlkfjasdlkfjlasdkjfmjsdlkmfasdlkfmsldfmsdlf;asldfkjsdl;fjslkjf;lskdfjlas;jfasdlkflkjsf";
  29. #define BASE_PATH "/nor/web"
  30. #define API_PATH "/nor/web/api"
  31. #define CSS_PATH "/nor/web/css"
  32. static void check_path()
  33. {
  34. DIR *dir_base = opendir(BASE_PATH);
  35. DIR *dir_api = opendir(API_PATH);
  36. DIR *dir_css = opendir(CSS_PATH);
  37. if(!dir_base)
  38. {
  39. mkdir(BASE_PATH,0777);
  40. }
  41. if(!dir_api)
  42. {
  43. mkdir(API_PATH,0777);
  44. }
  45. if(!dir_css)
  46. {
  47. mkdir(CSS_PATH,0777);
  48. }
  49. closedir(dir_base);
  50. closedir(dir_api);
  51. closedir(dir_css);
  52. }
  53. static void check_file(const char *nor_filename,const char *dst)
  54. {
  55. if(!nor_filename || !dst)
  56. return;
  57. md5_ctx_t ctx;
  58. md5_init(&ctx);
  59. uint8_t nor_tmp[16] = {0};
  60. char nor_m1[32]={0};
  61. char str_m1[32]={0};
  62. uint8_t str_tmp[16] = {0};
  63. char buff[1024] = {0};
  64. int fd = open(nor_filename,O_CREAT | O_RDWR,0777);
  65. if(fd <= 0)
  66. {
  67. return;
  68. }
  69. int read_len = 0;
  70. while((read_len = read(fd,buff,1024)) != 0)
  71. {
  72. md5_update(&ctx,(uint8_t*)&buff,read_len);
  73. }
  74. md5_final(&ctx, nor_tmp);
  75. for (int i=0; i<16; i++)
  76. byte2char(&nor_m1[i*2], nor_tmp[i]);
  77. //str
  78. md5_init(&ctx);
  79. uint32_t lenth = strlen(dst);
  80. uint32_t count = 1000;
  81. char *start = (char *)dst;
  82. do{
  83. if(lenth <= 1000)
  84. {
  85. memcpy(buff,start,lenth);
  86. md5_update(&ctx,(uint8_t*)&buff,lenth);
  87. lenth -= lenth;
  88. }else
  89. {
  90. memcpy(buff,start,1000);
  91. md5_update(&ctx,(uint8_t*)&buff,1000);
  92. lenth -= 1000;
  93. start += 1000;
  94. }
  95. }while(lenth);
  96. md5_final(&ctx, nor_tmp);
  97. for (int i=0; i<16; i++)
  98. byte2char(&str_m1[i*2], nor_tmp[i]);
  99. if(memcmp(nor_m1,str_m1,sizeof(nor_m1)))
  100. {
  101. //write filed to nor
  102. if (ftruncate(fd, 0) == -1) {
  103. close(fd);
  104. return;
  105. }
  106. lseek(fd,0,SEEK_SET);
  107. lenth = strlen(dst);
  108. char *start = (char *)dst;
  109. do{
  110. if(lenth <= 1000)
  111. {
  112. memcpy(buff,start,lenth);
  113. write(fd,buff,lenth);
  114. lenth-=lenth;
  115. }else
  116. {
  117. memcpy(buff,start,1000);
  118. lenth -= 1000;
  119. start += 1000;
  120. write(fd,buff,1000);
  121. }
  122. }while(lenth);
  123. }
  124. close(fd);
  125. fd = 0;
  126. }
  127. int main(void)
  128. {
  129. fs_init();
  130. check_path();
  131. // check_file("/nor/web/api/channel_display.js",p_channel_display);
  132. // check_file("/nor/web/api/data.js",p_data_js);
  133. // check_file("/nor/web/api/display_data.js",p_display_data);
  134. // check_file("/nor/web/api/index.js",p_index_js);
  135. // check_file("/nor/web/api/operation.js",p_operation);
  136. // check_file("/nor/web/api/petite-vue.min.js",p_petite_vue);
  137. // check_file("/nor/web/api/sys_paras.js",p_sys_para);
  138. // check_file("/nor/web/api/ws.js",p_ws);
  139. //
  140. //
  141. // check_file("/nor/web/css/index.css",p_css_index);
  142. check_file("/nor/web/index.html",p_index_html);
  143. led_init();
  144. led_set(0);
  145. while (1) {
  146. sleep(2);
  147. }
  148. }