stream_server.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifdef USE_BINARY_SENSOR
  24. #include "esphome/components/sensor/sensor.h"
  25. #endif
  26. #include <memory>
  27. #include <string>
  28. #include <vector>
  29. class StreamServerComponent : public esphome::Component {
  30. public:
  31. StreamServerComponent() = default;
  32. explicit StreamServerComponent(esphome::uart::UARTComponent *stream) : stream_{stream} {}
  33. void set_uart_parent(esphome::uart::UARTComponent *parent) { this->stream_ = parent; }
  34. void set_buffer_size(size_t size) { this->buf_size_ = size; }
  35. #ifdef USE_BINARY_SENSOR
  36. void set_connected_sensor(esphome::binary_sensor::BinarySensor *connected) { this->connected_sensor_ = connected; }
  37. #endif
  38. #ifdef USE_SENSOR
  39. void set_connection_count_sensor(esphome::sensor::Sensor *connection_count) { this->connection_count_sensor_ = connection_count; }
  40. #endif
  41. void setup() override;
  42. void loop() override;
  43. void dump_config() override;
  44. void on_shutdown() override;
  45. float get_setup_priority() const override { return esphome::setup_priority::AFTER_WIFI; }
  46. void set_port(uint16_t port) { this->port_ = port; }
  47. protected:
  48. void publish_sensor();
  49. void accept();
  50. void cleanup();
  51. void read();
  52. void flush();
  53. void write();
  54. size_t buf_index(size_t pos) { return pos & (this->buf_size_ - 1); }
  55. /// Return the number of consecutive elements that are ahead of @p pos in memory.
  56. size_t buf_ahead(size_t pos) { return (pos | (this->buf_size_ - 1)) - pos + 1; }
  57. struct Client {
  58. Client(std::unique_ptr<esphome::socket::Socket> socket, std::string identifier, size_t position);
  59. std::unique_ptr<esphome::socket::Socket> socket{nullptr};
  60. std::string identifier{};
  61. bool disconnected{false};
  62. size_t position{0};
  63. };
  64. esphome::uart::UARTComponent *stream_{nullptr};
  65. uint16_t port_;
  66. size_t buf_size_;
  67. #ifdef USE_BINARY_SENSOR
  68. esphome::binary_sensor::BinarySensor *connected_sensor_;
  69. #endif
  70. #ifdef USE_SENSOR
  71. esphome::sensor::Sensor *connection_count_sensor_;
  72. #endif
  73. std::unique_ptr<uint8_t[]> buf_{};
  74. size_t buf_head_{0};
  75. size_t buf_tail_{0};
  76. std::unique_ptr<esphome::socket::Socket> socket_{};
  77. std::vector<Client> clients_{};
  78. };