webinterface.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. server.sendHeader("Connection", "close");
  89. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  90. ESP.restart();
  91. }, [this]() {
  92. HTTPUpload& upload = server.upload();
  93. if (upload.status == UPLOAD_FILE_START) {
  94. Serial.printf("Update: %s\n", upload.filename.c_str());
  95. lead_len = 0;
  96. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  97. Update.printError(Serial);
  98. }
  99. } else if (upload.status == UPLOAD_FILE_WRITE) {
  100. /* flashing firmware to ESP*/
  101. if (lead_len < sizeof(lead_bytes)) {
  102. uint32_t n = sizeof(lead_bytes)-lead_len;
  103. if (n > upload.currentSize) {
  104. n = upload.currentSize;
  105. }
  106. memcpy(&lead_bytes[lead_len], upload.buf, n);
  107. lead_len += n;
  108. }
  109. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  110. Update.printError(Serial);
  111. }
  112. } else if (upload.status == UPLOAD_FILE_END) {
  113. // write extra bytes to force flush of the buffer before we check signature
  114. uint32_t extra = SPI_FLASH_SEC_SIZE+1;
  115. while (extra--) {
  116. uint8_t ff = 0xff;
  117. Update.write(&ff, 1);
  118. }
  119. if (!CheckFirmware::check_OTA_next(lead_bytes, lead_len)) {
  120. Serial.printf("failed firmware check\n");
  121. } else if (Update.end(true)) {
  122. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  123. } else {
  124. Update.printError(Serial);
  125. }
  126. }
  127. });
  128. Serial.printf("WAP started\n");
  129. server.begin();
  130. }
  131. void WebInterface::update()
  132. {
  133. if (!initialised) {
  134. init();
  135. initialised = true;
  136. }
  137. server.handleClient();
  138. }