Skip to content

Commit e383a11

Browse files
committed
Add Server and UDP and fix WiFi.hostByName
1 parent f6d4843 commit e383a11

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+682
-166
lines changed

libraries/WiFi/src/WiFi.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "WiFiGeneric.h"
3535

3636
#include "WiFiClient.h"
37+
#include "WiFiServer.h"
38+
#include "WiFiUdp.h"
3739

3840
class WiFiClass : public WiFiGenericClass, public WiFiSTAClass, public WiFiScanClass, public WiFiAPClass
3941
{
@@ -55,6 +57,8 @@ class WiFiClass : public WiFiGenericClass, public WiFiSTAClass, public WiFiScanC
5557
public:
5658
void printDiag(Print& dest);
5759
friend class WiFiClient;
60+
friend class WiFiServer;
61+
friend class WiFiUDP;
5862
};
5963

6064
extern WiFiClass WiFi;

libraries/WiFi/src/WiFiGeneric.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,21 @@ bool WiFiGenericClass::enableAP(bool enable)
304304
// ------------------------------------------------ Generic Network function ---------------------------------------------
305305
// -----------------------------------------------------------------------------------------------------------------------
306306

307-
void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg);
307+
static bool _dns_busy = false;
308+
309+
/**
310+
* DNS callback
311+
* @param name
312+
* @param ipaddr
313+
* @param callback_arg
314+
*/
315+
static void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
316+
{
317+
if(ipaddr) {
318+
(*reinterpret_cast<IPAddress*>(callback_arg)) = ipaddr->u_addr.ip4.addr;
319+
}
320+
_dns_busy = false;
321+
}
308322

309323
/**
310324
* Resolve the given hostname to an IP address.
@@ -313,36 +327,24 @@ void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *ca
313327
* @return 1 if aIPAddrString was successfully converted to an IP address,
314328
* else error code
315329
*/
316-
static bool _dns_busy = false;
317-
318330
int WiFiGenericClass::hostByName(const char* aHostname, IPAddress& aResult)
319331
{
320332
ip_addr_t addr;
321333
aResult = static_cast<uint32_t>(0);
334+
335+
_dns_busy = true;
322336
err_t err = dns_gethostbyname(aHostname, &addr, &wifi_dns_found_callback, &aResult);
323-
_dns_busy = err == ERR_INPROGRESS;
324-
while(_dns_busy);
325-
if(err == ERR_INPROGRESS && aResult) {
326-
//found by search
327-
} else if(err == ERR_OK && addr.u_addr.ip4.addr) {
337+
if(err == ERR_OK && addr.u_addr.ip4.addr) {
328338
aResult = addr.u_addr.ip4.addr;
339+
_dns_busy = false;
340+
} else if(err == ERR_INPROGRESS) {
341+
while(_dns_busy){
342+
delay(1);
343+
}
329344
} else {
345+
_dns_busy = false;
330346
return 0;
331347
}
332348
return 1;
333349
}
334350

335-
/**
336-
* DNS callback
337-
* @param name
338-
* @param ipaddr
339-
* @param callback_arg
340-
*/
341-
void wifi_dns_found_callback(const char *name, const ip_addr_t *ipaddr, void *callback_arg)
342-
{
343-
if(ipaddr) {
344-
(*reinterpret_cast<IPAddress*>(callback_arg)) = ipaddr->u_addr.ip4.addr;
345-
}
346-
_dns_busy = false;
347-
}
348-

libraries/WiFi/src/WiFiServer.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Server.cpp - Server class for Raspberry Pi
3+
Copyright (c) 2016 Hristo Gochkov All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
#include "WiFiServer.h"
20+
#include <lwip/sockets.h>
21+
#include <lwip/netdb.h>
22+
23+
#undef write
24+
25+
int WiFiServer::setTimeout(uint32_t seconds){
26+
struct timeval tv;
27+
tv.tv_sec = seconds;
28+
tv.tv_usec = 0;
29+
if(setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(struct timeval)) < 0)
30+
return -1;
31+
return setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(struct timeval));
32+
}
33+
34+
size_t WiFiServer::write(const uint8_t *data, size_t len){
35+
return 0;
36+
}
37+
38+
void WiFiServer::stopAll(){}
39+
40+
WiFiClient WiFiServer::available(){
41+
if(!_listening)
42+
return WiFiClient();
43+
struct sockaddr_in _client;
44+
int cs = sizeof(struct sockaddr_in);
45+
int client_sock = accept(sockfd, (struct sockaddr *)&_client, (socklen_t*)&cs);
46+
if(client_sock >= 0){
47+
int val = 1;
48+
if(setsockopt(client_sock, SOL_SOCKET, SO_KEEPALIVE, (char*)&val, sizeof(int)) == ESP_OK)
49+
return WiFiClient(client_sock);
50+
}
51+
return WiFiClient();
52+
}
53+
54+
void WiFiServer::begin(){
55+
if(_listening)
56+
return;
57+
struct sockaddr_in server;
58+
sockfd = socket(AF_INET , SOCK_STREAM, 0);
59+
if (sockfd < 0)
60+
return;
61+
server.sin_family = AF_INET;
62+
server.sin_addr.s_addr = INADDR_ANY;
63+
server.sin_port = htons(_port);
64+
if(bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
65+
return;
66+
if(listen(sockfd , _max_clients) < 0)
67+
return;
68+
fcntl(sockfd, F_SETFL, O_NONBLOCK);
69+
_listening = true;
70+
}
71+
72+
void WiFiServer::end(){
73+
close(sockfd);
74+
sockfd = -1;
75+
_listening = false;
76+
}
77+

libraries/WiFi/src/WiFiServer.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Server.h - Server class for Raspberry Pi
3+
Copyright (c) 2016 Hristo Gochkov All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
#ifndef _WIFISERVER_H_
20+
#define _WIFISERVER_H_
21+
22+
#include "Arduino.h"
23+
#include "Server.h"
24+
#include "WiFiClient.h"
25+
26+
class WiFiServer : public Server {
27+
private:
28+
int sockfd;
29+
uint16_t _port;
30+
uint8_t _max_clients;
31+
bool _listening;
32+
33+
public:
34+
void listenOnLocalhost(){}
35+
36+
WiFiServer(uint16_t port=80, uint8_t max_clients=4):sockfd(-1),_port(port),_max_clients(max_clients),_listening(false){}
37+
~WiFiServer(){ end();}
38+
WiFiClient available();
39+
WiFiClient accept(){return available();}
40+
void begin();
41+
size_t write(const uint8_t *data, size_t len);
42+
size_t write(uint8_t data){
43+
return write(&data, 1);
44+
}
45+
using Print::write;
46+
47+
void end();
48+
operator bool(){return _listening;}
49+
int setTimeout(uint32_t seconds);
50+
void stopAll();
51+
};
52+
53+
#endif /* _WIFISERVER_H_ */

0 commit comments

Comments
 (0)