-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwireless.cpp
152 lines (117 loc) · 3.35 KB
/
wireless.cpp
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
/*
* Navinaut-AIS
* Code: https://github.com/andrejanowicz/Navinaut-AIS
* Shop: https://navinaut-ais.de/
*
* Author: Andre Janowicz, <[email protected]>
* License: GPLV2
*
*/
#include <Arduino.h>
#include "wireless.h"
#include "defs.h"
#include <WiFiClient.h>
#include <WiFi.h>
#include "LED.h"
#include "List.h"
const char* FRIENDLY_NAME = "NAVINAUT-AIS-";
const char* PASSWORD = "NAVINAUT";
IPAddress Ip(192, 168, 15, 1);
IPAddress NMask(255, 255, 255, 0);
const char* BROADCAST_ADDR = "192.168.15.255";
const int UDP_PORT = 2000;
const int TCP_PORT = 2222;
char unique_friendly_name[22];
bool AP_started = false;
const size_t MAX_CLIENTS = 2;
uint8_t wireless_has_clients;
using tWiFiClientPtr = std::shared_ptr<WiFiClient>;
LinkedList<tWiFiClientPtr> clients;
WiFiUDP udp;
WiFiClient client;
WiFiServer server(TCP_PORT, MAX_CLIENTS);
void set_friendly_name() {
uint64_t chipid = ESP.getEfuseMac();
strcpy(unique_friendly_name, FRIENDLY_NAME);
sprintf(unique_friendly_name, "%s%04X", FRIENDLY_NAME, (uint16_t)(chipid >> 32));
}
void OnWiFiEvent(WiFiEvent_t event, WiFiEventInfo_t info){
switch (event) {
case WiFiEvent_t::ARDUINO_EVENT_WIFI_READY:
DEBUG.print("soft AP started.\t");
AP_started = true;
break;
case WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_START:
break;
case WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_STACONNECTED:
wireless_has_clients = WiFi.softAPgetStationNum();
DEBUG.println("soft AP client connected.\t");
break;
case WiFiEvent_t::ARDUINO_EVENT_WIFI_AP_STADISCONNECTED:
DEBUG.println("soft AP client disconnected.\t");
wireless_has_clients = WiFi.softAPgetStationNum();
break;
default:
break;
}
}
void AddClient(WiFiClient &client) {
DEBUG.print("new client:\t");
DEBUG.println(client.remoteIP());
clients.push_back(tWiFiClientPtr(new WiFiClient(client)));
}
void StopClient(LinkedList<tWiFiClientPtr>::iterator &it) {
DEBUG.println("client disconnected.");
(*it)->stop();
it = clients.erase(it);
}
void WiFi_handle() {
WiFiClient client = server.available(); // listen for incoming clients
if ( client ) AddClient(client);
for (auto it = clients.begin(); it != clients.end(); it++) {
if ( (*it) != NULL ) {
if ( !(*it)->connected() ) {
StopClient(it);
}else{
if ( (*it)->available() ) {
char c = (*it)->read();
if ( c == 0x03 ) StopClient(it);
}
}
}else{
it = clients.erase(it); // Should have been erased by StopClient
}
}
}
void UDP_send_NMEA(char* message) {
udp.beginPacket(BROADCAST_ADDR, UDP_PORT);
udp.write((uint8_t*)message, strlen(message));
udp.endPacket();
}
void TCP_IP_send_NMEA(char* message) {
for (auto it = clients.begin() ; it != clients.end(); it++) {
if ( (*it) != NULL && (*it)->connected() ) {
(*it)->print(message);
}
}
}
void WiFi_setup() {
WiFi.onEvent(OnWiFiEvent);
WiFi.mode(WIFI_AP);
WiFi.softAP(unique_friendly_name, PASSWORD);
while (!AP_started) {
LED_PWR_error_flash();
}
WiFi.softAPConfig(Ip, INADDR_NONE, NMask);
IPAddress myIP = WiFi.softAPIP();
DEBUG.print("\nUDP:\t");
DEBUG.print(myIP);
DEBUG.print(":");
DEBUG.println(UDP_PORT);
DEBUG.print("TCP/IP:\t");
DEBUG.print(myIP);
DEBUG.print(":");
DEBUG.print(TCP_PORT);
DEBUG.print("\t");
server.begin();
}