summaryrefslogtreecommitdiff
path: root/minion/src/wifi_module.cpp
blob: 7c3e5bc9c977673274e440037443fd9075507b86 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#include "wifi_module.hpp"

#include "util.hpp"

#include <Arduino.h>
#include <string.h>

WiFiModule::WiFiModule(const WiFiModuleOptions &options) noexcept
	: _serial(options.receive_pin, options.transmit_pin)
{
}

void WiFiModule::begin(size_t baudrate) noexcept
{
	_serial.begin(baudrate);
}

auto WiFiModule::get_available() noexcept -> int
{
	return _serial.available();
}

void WiFiModule::reset() noexcept
{
	_send_serial("AT+RST");

	char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";

	const auto response_status = _read(2000U, response);

	Serial.println(
		response_status == WiFiModuleResponseStatus::OK ? "Reset wifi module"
														: "Failed to reset wifi module"
	);
}

auto WiFiModule::connect_to_wifi(const char *ssid, const char *password) noexcept -> bool
{
	const char cmd[] = "AT+CWJAP";

	auto command_length = strlen(cmd) + strlen(ssid) + strlen(password) + 6U + 1U;

	auto *command = util::malloc<char>(command_length);

	if (command == nullptr)
	{
		Serial.println("Memory allocation failed for creating full connection command");
		return false;
	}

	snprintf(command, command_length, "%s=\"%s\",\"%s\"", cmd, ssid, password);

	const auto send_success = _send_serial(command);

	if (!send_success)
	{
		Serial.println("Failed to send connection command");
		return false;
	}

	free(command);

	char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";

	const auto response_status = _read(20000U, response);

	return response_status == WiFiModuleResponseStatus::OK;
}

void WiFiModule::set_wifi_mode(WifiMode wifi_mode) noexcept
{
	const char cmd[] = "AT+CWMODE_CUR";

	const auto command_length = strlen(cmd) + 2U + 1U;

	auto *command = util::malloc<char>(command_length);

	snprintf(command, command_length, "%s=%u", cmd, static_cast<int>(wifi_mode));

	_send_serial(command);

	free(command);

	char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";

	_read(TIMEOUT_SHORT, response);
}

void WiFiModule::set_multiple_connections_enabled(bool is_enabled) noexcept
{
	const char cmd[] = "AT+CIPMUX";

	auto command_length = strlen(cmd) + 2U + 1U;

	auto *command = util::malloc<char>(command_length);

	snprintf(command, command_length, "%s=%u", cmd, is_enabled ? 1U : 0U);

	_send_serial(command);

	free(command);

	char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";

	_read(TIMEOUT_SHORT, response);

	// Serial.println(response);
}

void WiFiModule::set_echo_enabled(bool is_enabled) noexcept
{
	const char cmd[] = "ATE";

	auto command_length = strlen(cmd) + 1U + 1U;

	auto *command = util::malloc<char>(command_length);

	snprintf(command, command_length, "%s%u", cmd, is_enabled ? 1U : 0U);

	_send_serial(command);

	free(command);

	char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";

	const auto response_status = _read(1500U, response);

	Serial.print(
		response_status == WiFiModuleResponseStatus::OK ? "Turned " : "Failed to turn "
	);
	Serial.print(is_enabled ? "on" : "off");
	Serial.println(" AT commands echo");
}

void WiFiModule::create_tcp_server(size_t port) noexcept
{
	const auto *cmd = "AT+CIPSERVER";

	auto command_length = strlen(cmd) + 7U + 1U;

	auto *command = util::malloc<char>(command_length);

	snprintf(command, command_length, "%s=1,%u", cmd, port);

	_send_serial(command);

	free(command);

	char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";

	_read(TIMEOUT_MEDIUM, response);

	// Serial.println(response);
}

auto WiFiModule::test() noexcept -> bool
{
	const auto send_success = _send_serial("AT");

	if (!send_success)
	{
		return false;
	}

	char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";

	const auto response_status = _read(5000U, response);

	if (response_status == WiFiModuleResponseStatus::TIMEOUT)
	{
		return false;
	}

	return strcmp(response, "") != 0;
}

auto WiFiModule::get_local_ip(char *local_ip_out) noexcept -> const char *
{
	const auto *command = "AT+CIFSR";

	const auto send_success = _send_serial(command);

	if (!send_success)
	{
		return local_ip_out;
	}

	auto *buf = util::malloc<char>(strlen(local_ip_out));

	if (buf == nullptr)
	{
		strcpy(local_ip_out, "Memory allocation failure");
		return local_ip_out;
	}

	strcpy(buf, "");

	const auto response_status = _read(10000U, buf);

	if (response_status != WiFiModuleResponseStatus::OK)
	{
		free(buf);

		sprintf(
			local_ip_out,
			"Response status was not OK. Was %d",
			static_cast<int>(response_status)
		);

		return local_ip_out;
	}

	const auto local_ip_end = strstr(buf, "\r\n+CIFSR:STAMAC");

	if (local_ip_end == nullptr)
	{
		free(buf);
		strcpy(local_ip_out, "Response parsing error");
		return local_ip_out;
	}

	const auto staip_title_length = 16U; // The length of '+CIFSR:STAIP,"'

	util::substr(buf + staip_title_length, local_ip_end, local_ip_out);

	free(buf);

	return local_ip_out;
}

auto WiFiModule::read_incoming_request() noexcept -> HTTPRequest *
{
	char request_prefix[] = "+IPD,";

	if (get_available() == 0 || !_serial.find(request_prefix))
	{
		return nullptr;
	}

	const auto min_available_bytes = 5;

	// Wait for the data buffer a bit
	while (get_available() < min_available_bytes)
	{
	}

	const auto connection_id = _read_connection_id();

	auto request_data_length = _read_request_data_length();

	const auto request_method = _read_request_method(request_data_length);

	// Read request path
	char request_path[REQUEST_PATH_MAX_LENGTH] = "";
	_read_to(&request_path[0], sizeof(request_path) - 1U, ' ', TIMEOUT_MEDIUM);

	request_data_length -= strlen(request_path) + 1U;

	// Read request HTTP version
	char http_version[REQUEST_HTTP_VERSION_MAX_LENGTH] = "";
	_read_to(&http_version[0], sizeof(http_version) - 1U, '\r', TIMEOUT_MEDIUM);

	request_data_length -= strlen(http_version) + 1U;

	// Skip the newline
	while (get_available() == 0)
	{
	}
	_serial.read();
	request_data_length -= 1U;

	auto request_data = util::malloc<char>(request_data_length + 1U);

	if (request_data == nullptr)
	{
		return nullptr;
	}

	strcpy(request_data, "");

	_read_bytes(request_data, request_data_length, TIMEOUT_LONG);

	return new HTTPRequest(
		connection_id,
		request_method,
		http_version,
		request_path,
		request_data_length,
		request_data
	);
}

auto WiFiModule::close_connection(size_t connection_id) noexcept -> bool
{
	const auto *cmd = "AT+CIPCLOSE";

	auto command_length = strlen(cmd) + 50U + 1U;

	auto *command = util::malloc<char>(command_length);

	if (command == nullptr)
	{
		Serial.println("Memory allocation failed for creating close connection command");
		return false;
	}

	snprintf(command, command_length, "%s=%u", cmd, connection_id);

	_send_serial(command);

	free(command);

	char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";

	const auto response_status = _read(4000U, response);

	if (response_status == WiFiModuleResponseStatus::OK)
	{
		Serial.print("Closed connection to ");
		Serial.println(connection_id);
	}
	else
	{
		Serial.print("Failed to close connection to ");
		Serial.println(connection_id);
		return false;
	}

	return true;
}

auto WiFiModule::send_response(size_t connection_id, const char *data) noexcept -> bool
{
	const auto *cmd = "AT+CIPSEND";

	const auto data_length = strlen(data);

	auto command_length = strlen(cmd) + 50U + data_length + 1U;

	auto *command = util::malloc<char>(command_length);

	if (command == nullptr)
	{
		Serial.println("Memory allocation failed for creating close connection command");
		return false;
	}

	snprintf(command, command_length, "%s=%u,%u", cmd, connection_id, data_length);

	_send_serial(command);

	free(command);

	char response[MAX_NETWORK_MODULE_RESPONSE_LENGTH] = "";

	_read(TIMEOUT_MEDIUM, response);

	_serial.print(data);

	strcpy(response, "");

	_read(TIMEOUT_MEDIUM, response);

	return true;
}

auto WiFiModule::_send_serial(const char *command) noexcept -> bool
{
	auto full_command_length = strlen(command) + 2U + 1U;

	auto *full_command = util::malloc<char>(full_command_length);

	if (full_command == nullptr)
	{
		return false;
	}

	snprintf(full_command, full_command_length, "%s\r\n", command);

	_serial.print(full_command);

	// Serial.print("Sent command: ");
	// Serial.println(full_command);

	free(full_command);

	return true;
}

size_t WiFiModule::_read_connection_id() noexcept
{
	const auto connection_id = _serial.read() - ASCII_TO_CHAR;

	// Skip the comma
	_serial.read();

	return connection_id;
}

size_t WiFiModule::_read_request_data_length() noexcept
{
	char data_length_buf[REQUEST_DATA_LENGTH_BUF_SIZE] = "";

	_read_to(&data_length_buf[0], sizeof(data_length_buf) - 1U, ':', TIMEOUT_SHORT);

	return static_cast<size_t>(strtoul(data_length_buf, nullptr, 10));
}

HTTPRequestMethod WiFiModule::_read_request_method(size_t &request_data_length) noexcept
{
	char request_method_buf[REQUEST_METHOD_STR_MAX_LENGTH] = "";

	_read_to(
		&request_method_buf[0],
		sizeof(request_method_buf) - 1U,
		' ',
		TIMEOUT_MEDIUM
	);

	const auto request_method = str_to_http_request_method(request_method_buf);

	request_data_length -= strlen(request_method_buf) + 1U;

	return request_method;
}

auto WiFiModule::_read(uint64_t timeout, char *response_out) noexcept
	-> WiFiModuleResponseStatus
{
	const auto start_time = millis();

	bool has_end = false;
	auto status = WiFiModuleResponseStatus::TIMEOUT;

	while (!has_end && (start_time + timeout) > millis())
	{
		while (_serial.available() != 0)
		{
			char character = _read_byte();

			strncat(response_out, &character, 1U);

			if (util::str_ends_with(response_out, "OK"))
			{
				status = WiFiModuleResponseStatus::OK;
				has_end = true;
			}

			if (util::str_ends_with(response_out, "ERROR"))
			{
				status = WiFiModuleResponseStatus::ERROR;
				has_end = true;
			}

			if (util::str_ends_with(response_out, "FAIL"))
			{
				status = WiFiModuleResponseStatus::FAIL;
				has_end = true;
			}
		}
	}

	return status;
}

void WiFiModule::_read_to(
	char *buffer_out,
	size_t length, // NOLINT(bugprone-easily-swappable-parameters)
	char stop_char,
	uint64_t timeout
) noexcept
{
	auto position = 0U;
	const auto start_time = millis();

	while (position < length && (start_time + timeout) > millis())
	{
		if (get_available() == 0)
		{
			continue;
		}

		auto character = _read_byte();

		if (character == stop_char)
		{
			break;
		}

		buffer_out[position++] = character;
	}

	buffer_out[position] = '\0';
}

// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
void WiFiModule::_read_bytes(char *buffer_out, size_t length, uint64_t timeout) noexcept
{
	const auto original_length = length;

	auto position = 0U;
	const auto start_time = millis();

	while (position != length && (start_time + timeout) > millis())
	{
		if (get_available() == 0)
		{
			continue;
		}

		char character = _read_byte();

		buffer_out[position++] = character;

		if (character == '\n' && position != original_length)
		{
			length -= 2U;
		}
	}

	buffer_out[position] = '\0';
}

auto WiFiModule::_read_byte() noexcept -> char
{
	return static_cast<char>(_serial.read());
}