#pragma once #include "http/request.hpp" #include "network_connection.hpp" #include #include #include #include constexpr auto MAX_NETWORK_MODULE_RESPONSE_LENGTH = 128U; constexpr auto REQUEST_DATA_LENGTH_BUF_SIZE = 16U; constexpr auto REQUEST_METHOD_STR_MAX_LENGTH = 10U; constexpr auto REQUEST_PATH_MAX_LENGTH = 64U; constexpr auto REQUEST_HTTP_VERSION_MAX_LENGTH = 10U; constexpr auto WIFI_CONNECT_COMMAND_BASE_LENGTH = 7U; constexpr auto CREATE_TCP_SERVER_COMMAND_BASE_LENGTH = 8U; constexpr auto CLOSE_CONNECTION_COMMAND_BASE_LENGTH = 51U; constexpr auto SEND_RESPONSE_COMMAND_BASE_LENGTH = 51U; constexpr auto ASCII_TO_CHAR = 48U; constexpr auto TIMEOUT_SHORT = 1500U; constexpr auto TIMEOUT_MEDIUM = 4000U; constexpr auto TIMEOUT_LONG = 10000U; const char RESPONSE_HTTP_VERSION[] PROGMEM = { "HTTP/1.1" }; constexpr auto RESPONSE_STATUS_CODE_LENGTH = 3U; constexpr auto NUMBER_BASE = 10U; enum WifiMode { Station = 1, SoftAP = 2, SoftAPAndStation = 3 }; enum WiFiModuleResponseStatus { OK, FAIL, ERROR, TIMEOUT }; struct WiFiModuleOptions { uint8_t receive_pin; uint8_t transmit_pin; }; class WiFiModule { public: explicit WiFiModule(const WiFiModuleOptions &options) noexcept; void begin(size_t baudrate) noexcept; auto get_available() noexcept -> int; void reset() noexcept; /** * Connects to a wifi network. * * @param ssid The service set identifier of a wifi network. * @param password The wifi network password. * * @returns Whether or not it succeeded. */ auto connect_to_wifi(const char *ssid, const char *password) noexcept -> bool; void set_wifi_mode(WifiMode wifi_mode) noexcept; void set_multiple_connections_enabled(bool is_enabled) noexcept; void set_echo_enabled(bool is_enabled) noexcept; void create_tcp_server(size_t port) noexcept; /** * Tests the connection to the wifi module. * * @returns Whether or not the test succeeded. */ auto test() noexcept -> bool; /** * Gets local IP address of the wifi module. * * @param local_ip_out Local IP output buffer. * * @returns A pointer to the local IP output buffer. */ auto get_local_ip(char *local_ip_out) noexcept -> const char *; /** * Gets the MAC address of the wifi module. * * @param mac_address_out MAC address output buffer. * * @returns A pointer to the MAC address output buffer. */ auto get_mac_address(char *mac_address_out) noexcept -> const char *; /** * Reads a incoming HTTP request. * * @returns A request. Has a connection with a ID of -1 if reading the request failed. */ auto read_incoming_request() noexcept -> HTTPRequest; auto close_connection(NetworkConnection &connection) noexcept -> bool; auto send_response( const NetworkConnection &connection, size_t status_code, const char **headers, size_t headers_cnt, const char *body ) noexcept -> bool; private: SoftwareSerial _serial; /** * Sends a command to the wifi module. * * @param command A command without the "AT+" in the beginning. * * @returns Whether or not it succeeded. */ auto _send_serial(const char *command) noexcept -> bool; auto _read_connection_id() noexcept -> int8_t; auto _read_request_data_length() noexcept -> size_t; auto _read_request_method(size_t &request_data_length) noexcept -> HTTPRequestMethod; /** * Reads from the wifi module until it responds with a status. * * @param timeout Timeout in milliseconds. * @param response_out Response output buffer. * * @returns The response status. * */ auto _read(uint64_t timeout, char *response_out) noexcept -> WiFiModuleResponseStatus; void _read_to(char *buffer_out, size_t length, char stop_char, uint64_t timeout) noexcept; void _read_bytes(char *buffer_out, size_t length, uint64_t timeout) noexcept; auto _read_byte() noexcept -> char; };