stream_server.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Copyright (C) 2020-2023 Oxan van Leeuwen
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include "esphome/core/component.h"
  18. #include "esphome/components/socket/socket.h"
  19. #include "esphome/components/uart/uart.h"
  20. #ifdef USE_BINARY_SENSOR
  21. #include "esphome/components/binary_sensor/binary_sensor.h"
  22. #endif
  23. #include <memory>
  24. #include <string>
  25. #include <vector>
  26. class StreamServerComponent : public esphome::Component {
  27. public:
  28. StreamServerComponent() = default;
  29. explicit StreamServerComponent(esphome::uart::UARTComponent *stream) : stream_{stream} {}
  30. void set_uart_parent(esphome::uart::UARTComponent *parent) { this->stream_ = parent; }
  31. void set_buffer_size(size_t size) { this->buf_size_ = size; }
  32. #ifdef USE_BINARY_SENSOR
  33. void set_connected_sensor(esphome::binary_sensor::BinarySensor *connected) { this->connected_sensor_ = connected; }
  34. #endif
  35. void setup() override;
  36. void loop() override;
  37. void dump_config() override;
  38. void on_shutdown() override;
  39. float get_setup_priority() const override { return esphome::setup_priority::AFTER_WIFI; }
  40. void set_port(uint16_t port) { this->port_ = port; }
  41. protected:
  42. void publish_sensor();
  43. void accept();
  44. void cleanup();
  45. void read();
  46. void flush();
  47. void write();
  48. size_t buf_index(size_t pos) { return pos & (this->buf_size_ - 1); }
  49. /// Return the number of consecutive elements that are ahead of @p pos in memory.
  50. size_t buf_ahead(size_t pos) { return (pos | (this->buf_size_ - 1)) - pos + 1; }
  51. struct Client {
  52. Client(std::unique_ptr<esphome::socket::Socket> socket, std::string identifier, size_t position);
  53. std::unique_ptr<esphome::socket::Socket> socket{nullptr};
  54. std::string identifier{};
  55. bool disconnected{false};
  56. size_t position{0};
  57. };
  58. esphome::uart::UARTComponent *stream_{nullptr};
  59. uint16_t port_;
  60. size_t buf_size_;
  61. #ifdef USE_BINARY_SENSOR
  62. esphome::binary_sensor::BinarySensor *connected_sensor_;
  63. #endif
  64. std::unique_ptr<uint8_t[]> buf_{};
  65. size_t buf_head_{0};
  66. size_t buf_tail_{0};
  67. std::unique_ptr<esphome::socket::Socket> socket_{};
  68. std::vector<Client> clients_{};
  69. };