webinterface.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. { ".css", "text/css" },
  43. };
  44. for (const auto &e : extensions) {
  45. if (uri.endsWith(e.extension)) {
  46. content_type = e.content_type;
  47. break;
  48. }
  49. }
  50. auto *f = ROMFS::find_stream(uri.c_str());
  51. if (f != nullptr) {
  52. server.sendHeader("Content-Encoding", "gzip");
  53. server.streamFile(*f, content_type);
  54. delete f;
  55. return true;
  56. }
  57. return false;
  58. }
  59. } ROMFS_Handler;
  60. /*
  61. serve files from ROMFS
  62. */
  63. class AJAX_Handler : public RequestHandler
  64. {
  65. bool canHandle(HTTPMethod method, String uri) {
  66. return uri == "/ajax/status.json";
  67. }
  68. bool handle(WebServer& server, HTTPMethod requestMethod, String requestUri) {
  69. if (requestUri != "/ajax/status.json") {
  70. return false;
  71. }
  72. server.send(200, "application/json", status_json());
  73. return true;
  74. }
  75. } AJAX_Handler;
  76. /*
  77. init web server
  78. */
  79. void WebInterface::init(void)
  80. {
  81. Serial.printf("WAP start %s %s\n", g.wifi_ssid, g.wifi_password);
  82. IPAddress myIP = WiFi.softAPIP();
  83. server.addHandler( &AJAX_Handler );
  84. server.addHandler( &ROMFS_Handler );
  85. /*handling uploading firmware file */
  86. server.on("/update", HTTP_POST, []() {
  87. server.sendHeader("Connection", "close");
  88. server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
  89. ESP.restart();
  90. }, [this]() {
  91. HTTPUpload& upload = server.upload();
  92. if (upload.status == UPLOAD_FILE_START) {
  93. Serial.printf("Update: %s\n", upload.filename.c_str());
  94. lead_len = 0;
  95. if (!Update.begin(UPDATE_SIZE_UNKNOWN)) { //start with max available size
  96. Update.printError(Serial);
  97. }
  98. } else if (upload.status == UPLOAD_FILE_WRITE) {
  99. /* flashing firmware to ESP*/
  100. if (lead_len < sizeof(lead_bytes)) {
  101. uint32_t n = sizeof(lead_bytes)-lead_len;
  102. if (n > upload.currentSize) {
  103. n = upload.currentSize;
  104. }
  105. memcpy(&lead_bytes[lead_len], upload.buf, n);
  106. lead_len += n;
  107. }
  108. if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
  109. Update.printError(Serial);
  110. }
  111. } else if (upload.status == UPLOAD_FILE_END) {
  112. // write extra bytes to force flush of the buffer before we check signature
  113. uint32_t extra = SPI_FLASH_SEC_SIZE+1;
  114. while (extra--) {
  115. uint8_t ff = 0xff;
  116. Update.write(&ff, 1);
  117. }
  118. if (!CheckFirmware::check_OTA_next(lead_bytes, lead_len)) {
  119. Serial.printf("failed firmware check\n");
  120. } else if (Update.end(true)) {
  121. Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
  122. } else {
  123. Update.printError(Serial);
  124. }
  125. }
  126. });
  127. Serial.printf("WAP started\n");
  128. server.begin();
  129. }
  130. void WebInterface::update()
  131. {
  132. if (!initialised) {
  133. init();
  134. initialised = true;
  135. }
  136. server.handleClient();
  137. }