webinterface.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "webinterface.h"
  2. #include <WiFi.h>
  3. #include <WiFiClient.h>
  4. #include <WebServer.h>
  5. #include <WiFiAP.h>
  6. #include <ESPmDNS.h>
  7. #include <Update.h>
  8. #include "parameters.h"
  9. #include "romfs.h"
  10. #include "check_firmware.h"
  11. #include "status.h"
  12. static WebServer server(80);
  13. /*
  14. serve files from ROMFS
  15. */
  16. class ROMFS_Handler : public RequestHandler
  17. {
  18. bool canHandle(HTTPMethod method, String uri) {
  19. if (uri == "/") {
  20. uri = "/index.html";
  21. }
  22. uri = "web" + uri;
  23. if (ROMFS::exists(uri.c_str())) {
  24. return true;
  25. }
  26. return false;
  27. }
  28. bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) {
  29. if (requestUri == "/") {
  30. requestUri = "/index.html";
  31. }
  32. String uri = "web" + requestUri;
  33. Serial.printf("handle: '%s'\n", requestUri.c_str());
  34. // work out content type
  35. const char *content_type = "text/html";
  36. const struct {
  37. const char *extension;
  38. const char *content_type;
  39. } extensions[] = {
  40. { ".js", "text/javascript" },
  41. { ".jpg", "image/jpeg" },
  42. { ".png", "image/png" },
  43. { ".css", "text/css" },
  44. };
  45. for (const auto &e : extensions) {
  46. if (uri.endsWith(e.extension)) {
  47. content_type = e.content_type;
  48. break;
  49. }
  50. }
  51. auto *f = ROMFS::find_stream(uri.c_str());
  52. if (f != nullptr) {
  53. server.sendHeader("Content-Encoding", "gzip");
  54. server.streamFile(*f, content_type);
  55. delete f;
  56. return true;
  57. }
  58. return false;
  59. }
  60. } ROMFS_Handler;
  61. /*
  62. serve files from ROMFS
  63. */
  64. class AJAX_Handler : public RequestHandler
  65. {
  66. bool canHandle(HTTPMethod method, String uri) {
  67. return uri == "/ajax/status.json";
  68. }
  69. bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) {
  70. if (requestUri != "/ajax/status.json") {
  71. return false;
  72. }
  73. server.send(200, "application/json", status_json());
  74. return true;
  75. }
  76. } AJAX_Handler;
  77. /*
  78. init web server
  79. */
  80. void WebInterface::init(void)
  81. {
  82. Serial.printf("WAP start %s %s\n", g.wifi_ssid, g.wifi_password);
  83. IPAddress myIP = WiFi.softAPIP();
  84. server.addHandler( &AJAX_Handler );
  85. server.addHandler( &ROMFS_Handler );
  86. /*handling uploading firmware file */
  87. server.on("/update", HTTP_POST, []() {
  88. if (Update.hasError()) {
  89. server.sendHeader("Connection", "close");
  90. server.send(500, "text/plain","FAIL");
  91. Serial.printf("Update Failed: Update function has errors\n");
  92. delay(5000);
  93. } else {
  94. server.sendHeader("Connection", "close");
  95. server.send(200, "text/plain","OK");
  96. Serial.printf("Update Success: \nRebooting...\n");
  97. delay(1000);
  98. ESP.restart();
  99. }
  100. }, [this]() {
  101. HTTPUpload& upload = server.upload();
  102. static const esp_partition_t* partition_new_firmware = esp_ota_get_next_update_partition(NULL); //get OTA partion to which we will write new firmware file;
  103. if (upload.status == UPLOAD_FILE_START) {
  104. Serial.printf("Update: %s\n", upload.filename.c_str());
  105. lead_len = 0;
  106. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  107. Update.printError(Serial);
  108. }
  109. } else if (upload.status == UPLOAD_FILE_WRITE) {
  110. /* flashing firmware to ESP*/
  111. if (lead_len < sizeof(lead_bytes)) {
  112. uint32_t n = sizeof(lead_bytes)-lead_len;
  113. if (n > upload.currentSize) {
  114. n = upload.currentSize;
  115. }
  116. memcpy(&lead_bytes[lead_len], upload.buf, n);
  117. lead_len += n;
  118. }
  119. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  120. Update.printError(Serial);
  121. }
  122. } else if (upload.status == UPLOAD_FILE_END) {
  123. // write extra bytes to force flush of the buffer before we check signature
  124. uint32_t extra = SPI_FLASH_SEC_SIZE+1;
  125. while (extra--) {
  126. uint8_t ff = 0xff;
  127. Update.write(&ff, 1);
  128. }
  129. if (!CheckFirmware::check_OTA_next(partition_new_firmware, lead_bytes, lead_len)) {
  130. Serial.printf("Update Failed: firmware checks have errors\n");
  131. server.sendHeader("Connection", "close");
  132. server.send(500, "text/plain","FAIL");
  133. delay(5000);
  134. } else if (Update.end(true)) {
  135. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  136. server.sendHeader("Connection", "close");
  137. server.send(200, "text/plain","OK");
  138. } else {
  139. Update.printError(Serial);
  140. Serial.printf("Update Failed: Update.end function has errors\n");
  141. server.sendHeader("Connection", "close");
  142. server.send(500, "text/plain","FAIL");
  143. delay(5000);
  144. }
  145. }
  146. });
  147. Serial.printf("WAP started\n");
  148. server.begin();
  149. }
  150. void WebInterface::update()
  151. {
  152. if (!initialised) {
  153. init();
  154. initialised = true;
  155. }
  156. server.handleClient();
  157. }