S&B Volcano vaporizer remote control with Pi Pico W
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * http.c
  3. *
  4. * https://github.com/krzmaz/pico-w-webserver-example
  5. * https://github.com/lwip-tcpip/lwip/blob/master/contrib/examples/httpd/genfiles_example/genfiles_example.c
  6. * https://www.nongnu.org/lwip/2_1_x/group__httpd.html
  7. *
  8. * Copyright (c) 2023 Thomas Buck (thomas@xythobuz.de)
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * See <http://www.gnu.org/licenses/>.
  21. */
  22. #include "lwip/apps/httpd.h"
  23. #include "lwip/apps/fs.h"
  24. #include "ff.h"
  25. #include <string.h>
  26. #include "config.h"
  27. #include "log.h"
  28. #include "debug_disk.h"
  29. #include "http.h"
  30. static char *fs_log_write_buf = NULL;
  31. static size_t fs_log_write_count = 0;
  32. static size_t fs_log_write_len = 0;
  33. void http_init(void) {
  34. httpd_init();
  35. }
  36. static void fs_log_write(const void *b, size_t l) {
  37. if ((fs_log_write_count + l) > fs_log_write_len) {
  38. l = fs_log_write_len - fs_log_write_count;
  39. }
  40. memcpy(fs_log_write_buf + fs_log_write_count, b, l);
  41. fs_log_write_count += l;
  42. }
  43. int fs_open_custom(struct fs_file *file, const char *name) {
  44. debug("'%s'", name);
  45. if (strcmp(name, "/log.json") == 0) {
  46. size_t log_len = rb_len(log_get());
  47. char *log = malloc(log_len);
  48. if (!log) {
  49. debug("error: not enough memory %d", log_len);
  50. return 0;
  51. }
  52. fs_log_write_buf = log;
  53. fs_log_write_count = 0;
  54. fs_log_write_len = log_len;
  55. rb_dump(log_get(), fs_log_write, 0);
  56. memset(file, 0, sizeof(struct fs_file));
  57. file->pextension = "/log.json";
  58. file->data = log;
  59. file->len = log_len;
  60. file->index = file->len;
  61. file->flags = FS_FILE_FLAGS_HEADER_PERSISTENT;
  62. return 1;
  63. }
  64. // TODO only do this when not mounted via USB
  65. debug_disk_mount();
  66. FIL f;
  67. FRESULT r = f_open (&f, name, FA_READ);
  68. if (r == FR_OK) {
  69. FSIZE_t len = f_size(&f);
  70. char *data = malloc(len);
  71. if (!data) {
  72. debug("error: not enough memory %ld", len);
  73. f_close(&f);
  74. debug_disk_unmount();
  75. return 0;
  76. }
  77. UINT read_count = 0;
  78. r = f_read(&f, data, len, &read_count);
  79. if ((r != FR_OK) || (read_count != len)) {
  80. debug("invalid read: %d %d %ld", r, read_count, len);
  81. }
  82. memset(file, 0, sizeof(struct fs_file));
  83. file->pextension = NULL;
  84. file->data = data;
  85. file->len = len;
  86. file->index = file->len;
  87. file->flags = FS_FILE_FLAGS_HEADER_PERSISTENT;
  88. f_close(&f);
  89. debug_disk_unmount();
  90. return 1;
  91. }
  92. debug_disk_unmount();
  93. return 0;
  94. }
  95. void fs_close_custom(struct fs_file *file) {
  96. debug("len=%d", file->len);
  97. if (file && file->data) {
  98. free((void *)file->data);
  99. file->data = NULL;
  100. }
  101. }