Skip to content

Commit e1df697

Browse files
committed
WiFiServer - added end(), begin(port) and ctor without parameters
1 parent 12c33f1 commit e1df697

File tree

6 files changed

+54
-3
lines changed

6 files changed

+54
-3
lines changed

docs/api.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -2713,12 +2713,12 @@ Tells the server to begin listening for incoming connections.
27132713

27142714
```
27152715
server.begin()
2716-
2716+
server.begin(port)
27172717
```
27182718

27192719
#### Parameters
27202720

2721-
- None
2721+
- port (optional): the port to listen on (int)
27222722

27232723
#### Returns
27242724

src/WiFiServer.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ extern "C" {
2929
#include "WiFiClient.h"
3030
#include "WiFiServer.h"
3131

32+
WiFiServer::WiFiServer() :
33+
_sock(NO_SOCKET_AVAIL),
34+
_lastSock(NO_SOCKET_AVAIL)
35+
{
36+
_port = 80;
37+
}
38+
3239
WiFiServer::WiFiServer(uint16_t port) :
3340
_sock(NO_SOCKET_AVAIL),
3441
_lastSock(NO_SOCKET_AVAIL)
@@ -38,13 +45,30 @@ WiFiServer::WiFiServer(uint16_t port) :
3845

3946
void WiFiServer::begin()
4047
{
48+
end();
4149
_sock = ServerDrv::getSocket();
4250
if (_sock != NO_SOCKET_AVAIL)
4351
{
4452
ServerDrv::startServer(_port, _sock);
4553
}
4654
}
4755

56+
void WiFiServer::begin(uint16_t port)
57+
{
58+
end();
59+
_port = port;
60+
begin();
61+
}
62+
63+
void WiFiServer::end()
64+
{
65+
if (_sock != NO_SOCKET_AVAIL) {
66+
ServerDrv::stopServer(_sock);
67+
_sock = NO_SOCKET_AVAIL;
68+
_lastSock = NO_SOCKET_AVAIL;
69+
}
70+
}
71+
4872
WiFiClient WiFiServer::available(byte* status)
4973
{
5074
int sock = NO_SOCKET_AVAIL;

src/WiFiServer.h

+3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ class WiFiServer : public Server {
3636
uint16_t _port;
3737
void* pcb;
3838
public:
39+
WiFiServer();
3940
WiFiServer(uint16_t);
4041
WiFiClient available(uint8_t* status = NULL);
4142
void begin();
43+
void begin(uint16_t port);
44+
void end();
4245
virtual size_t write(uint8_t);
4346
virtual size_t write(const uint8_t *buf, size_t size);
4447
uint8_t status();

src/utility/server_drv.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,28 @@ void ServerDrv::startServer(uint32_t ipAddress, uint16_t port, uint8_t sock, uin
8484
SpiDrv::spiSlaveDeselect();
8585
}
8686

87+
void ServerDrv::stopServer(uint8_t sock)
88+
{
89+
WAIT_FOR_SLAVE_SELECT();
90+
// Send Command
91+
SpiDrv::sendCmd(STOP_SERVER_TCP_CMD, PARAM_NUMS_1);
92+
SpiDrv::sendParam(&sock, 1, LAST_PARAM);
93+
94+
SpiDrv::spiSlaveDeselect();
95+
//Wait the reply elaboration
96+
SpiDrv::waitForSlaveReady();
97+
SpiDrv::spiSlaveSelect();
98+
99+
// Wait for reply
100+
uint8_t _data = 0;
101+
uint8_t _dataLen = 0;
102+
if (!SpiDrv::waitResponseCmd(STOP_SERVER_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
103+
{
104+
WARN("error waitResponse");
105+
}
106+
SpiDrv::spiSlaveDeselect();
107+
}
108+
87109
// Start server TCP on port specified
88110
void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode)
89111
{

src/utility/server_drv.h

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class ServerDrv
3535

3636
static void startServer(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
3737

38+
static void stopServer(uint8_t sock);
39+
3840
static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
3941

4042
static void startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);

src/utility/wifi_spi.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ enum {
8282
GET_HOST_BY_NAME_CMD= 0x35,
8383
START_SCAN_NETWORKS = 0x36,
8484
GET_FW_VERSION_CMD = 0x37,
85-
// GET_TEST_CMD = 0x38,
85+
STOP_SERVER_TCP_CMD = 0x38,
8686
SEND_DATA_UDP_CMD = 0x39,
8787
GET_REMOTE_DATA_CMD = 0x3A,
8888
GET_TIME_CMD = 0x3B,

0 commit comments

Comments
 (0)