webinterface.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. static WebServer server(80);
  12. /*
  13. serve files from ROMFS
  14. */
  15. class ROMFS_Handler : public RequestHandler
  16. {
  17. bool canHandle(HTTPMethod method, String uri) {
  18. if (uri == "/") {
  19. uri = "/index.html";
  20. }
  21. uri = "web" + uri;
  22. if (ROMFS::exists(uri.c_str())) {
  23. return true;
  24. }
  25. return false;
  26. }
  27. bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) {
  28. if (requestUri == "/") {
  29. requestUri = "/index.html";
  30. }
  31. String uri = "web" + requestUri;
  32. Serial.printf("handle: '%s'\n", requestUri.c_str());
  33. // work out content type
  34. const char *content_type = "text/html";
  35. const struct {
  36. const char *extension;
  37. const char *content_type;
  38. } extensions[] = {
  39. { ".js", "text/javascript" },
  40. { ".jpg", "image/jpeg" },
  41. { ".css", "text/css" },
  42. };
  43. for (const auto &e : extensions) {
  44. if (uri.endsWith(e.extension)) {
  45. content_type = e.content_type;
  46. break;
  47. }
  48. }
  49. auto *f = ROMFS::find_stream(uri.c_str());
  50. if (f != nullptr) {
  51. server.sendHeader("Content-Encoding", "gzip");
  52. server.streamFile(*f, content_type);
  53. delete f;
  54. return true;
  55. }
  56. return false;
  57. }
  58. } ROMFS_Handler;
  59. /*
  60. init web server
  61. */
  62. void WebInterface::init(void)
  63. {
  64. Serial.printf("WAP start %s %s\n", g.wifi_ssid, g.wifi_password);
  65. WiFi.softAP(g.wifi_ssid, g.wifi_password);
  66. IPAddress myIP = WiFi.softAPIP();
  67. server.addHandler( &ROMFS_Handler );
  68. /*handling uploading firmware file */
  69. server.on("/update", HTTP_POST, []() {
  70. server.sendHeader("Connection", "close");
  71. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  72. ESP.restart();
  73. }, [this]() {
  74. HTTPUpload& upload = server.upload();
  75. if (upload.status == UPLOAD_FILE_START) {
  76. Serial.printf("Update: %s\n", upload.filename.c_str());
  77. lead_len = 0;
  78. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  79. Update.printError(Serial);
  80. }
  81. } else if (upload.status == UPLOAD_FILE_WRITE) {
  82. /* flashing firmware to ESP*/
  83. if (lead_len < sizeof(lead_bytes)) {
  84. uint32_t n = sizeof(lead_bytes)-lead_len;
  85. if (n > upload.currentSize) {
  86. n = upload.currentSize;
  87. }
  88. memcpy(&lead_bytes[lead_len], upload.buf, n);
  89. lead_len += n;
  90. }
  91. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  92. Update.printError(Serial);
  93. }
  94. } else if (upload.status == UPLOAD_FILE_END) {
  95. // write extra bytes to force flush of the buffer before we check signature
  96. uint32_t extra = SPI_FLASH_SEC_SIZE+1;
  97. while (extra--) {
  98. uint8_t ff = 0xff;
  99. Update.write(&ff, 1);
  100. }
  101. if (!CheckFirmware::check_OTA_next(lead_bytes, lead_len) && g.lock_level > 0) {
  102. Serial.printf("failed firmware check\n");
  103. } else if (Update.end(true)) {
  104. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  105. } else {
  106. Update.printError(Serial);
  107. }
  108. }
  109. });
  110. Serial.printf("WAP started\n");
  111. server.begin();
  112. }
  113. void WebInterface::update()
  114. {
  115. if (!initialised) {
  116. init();
  117. initialised = true;
  118. }
  119. server.handleClient();
  120. }