summaryrefslogtreecommitdiff
path: root/minion/src/wifi_module.hpp
blob: 5de898ef16882666d517630b2a912b40ca420d09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once

#include <SoftwareSerial.h>
#include <stddef.h>
#include <stdint.h>

constexpr auto MAX_NETWORK_MODULE_RESPONSE_LENGTH = 128U;

constexpr auto ASCII_TO_CHAR = 48U;

constexpr auto TIMEOUT_SHORT = 1500U;
constexpr auto TIMEOUT_LONG = 4000U;

enum WifiMode
{
	Station = 1,
	SoftAP = 2,
	SoftAPAndStation = 3
};

enum ResponseStatus
{
	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(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 *;

	auto has_incoming_request() noexcept -> bool;

	/**
	 * Reads a incoming HTTP request.
	 *
	 * @param raw_request_out Raw request output buffer.
	 *
	 * @returns The connection ID.
	 */
	auto read_incoming_request(char *raw_request_out) noexcept -> size_t;

	auto close_connection(size_t connection_id) noexcept -> bool;

	auto send(size_t connection_id, const char *data) 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;

	/**
	 * 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 -> ResponseStatus;

	auto _read_byte() noexcept -> char;
};