Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2778,12 +2778,12 @@ Tells the server to begin listening for incoming connections.

```
server.begin()

server.begin(port)
```

#### Parameters

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

#### Returns

Expand Down
24 changes: 24 additions & 0 deletions src/WiFiServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ extern "C" {
#include "WiFiClient.h"
#include "WiFiServer.h"

WiFiServer::WiFiServer() :
_sock(NO_SOCKET_AVAIL),
_lastSock(NO_SOCKET_AVAIL)
{
_port = 80;
}

WiFiServer::WiFiServer(uint16_t port) :
_sock(NO_SOCKET_AVAIL),
_lastSock(NO_SOCKET_AVAIL)
Expand All @@ -38,13 +45,30 @@ WiFiServer::WiFiServer(uint16_t port) :

void WiFiServer::begin()
{
end();
_sock = ServerDrv::getSocket();
if (_sock != NO_SOCKET_AVAIL)
{
ServerDrv::startServer(_port, _sock);
}
}

void WiFiServer::begin(uint16_t port)
{
end();
_port = port;
begin();
}

void WiFiServer::end()
{
if (_sock != NO_SOCKET_AVAIL) {
ServerDrv::stopServer(_sock);
_sock = NO_SOCKET_AVAIL;
_lastSock = NO_SOCKET_AVAIL;
}
}

WiFiClient WiFiServer::available(byte* status)
{
int sock = NO_SOCKET_AVAIL;
Expand Down
3 changes: 3 additions & 0 deletions src/WiFiServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ class WiFiServer : public Server {
uint16_t _port;
void* pcb;
public:
WiFiServer();
WiFiServer(uint16_t);
WiFiClient available(uint8_t* status = NULL);
WiFiClient accept();
void begin();
void begin(uint16_t port);
void end();
virtual size_t write(uint8_t);
virtual size_t write(const uint8_t *buf, size_t size);
uint8_t status();
Expand Down
22 changes: 22 additions & 0 deletions src/utility/server_drv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,28 @@ void ServerDrv::startServer(uint32_t ipAddress, uint16_t port, uint8_t sock, uin
SpiDrv::spiSlaveDeselect();
}

void ServerDrv::stopServer(uint8_t sock)
{
WAIT_FOR_SLAVE_SELECT();
// Send Command
SpiDrv::sendCmd(STOP_SERVER_TCP_CMD, PARAM_NUMS_1);
SpiDrv::sendParam(&sock, 1, LAST_PARAM);

SpiDrv::spiSlaveDeselect();
//Wait the reply elaboration
SpiDrv::waitForSlaveReady();
SpiDrv::spiSlaveSelect();

// Wait for reply
uint8_t _data = 0;
uint8_t _dataLen = 0;
if (!SpiDrv::waitResponseCmd(STOP_SERVER_TCP_CMD, PARAM_NUMS_1, &_data, &_dataLen))
{
WARN("error waitResponse");
}
SpiDrv::spiSlaveDeselect();
}

// Start server TCP on port specified
void ServerDrv::startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode)
{
Expand Down
2 changes: 2 additions & 0 deletions src/utility/server_drv.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ class ServerDrv

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

static void stopServer(uint8_t sock);

static void startClient(uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);

static void startClient(const char* host, uint8_t host_len, uint32_t ipAddress, uint16_t port, uint8_t sock, uint8_t protMode=TCP_MODE);
Expand Down
2 changes: 1 addition & 1 deletion src/utility/wifi_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ enum {
GET_HOST_BY_NAME_CMD = 0x35,
START_SCAN_NETWORKS = 0x36,
GET_FW_VERSION_CMD = 0x37,
// GET_TEST_CMD = 0x38,
STOP_SERVER_TCP_CMD = 0x38,
SEND_DATA_UDP_CMD = 0x39,
GET_REMOTE_DATA_CMD = 0x3A,
GET_TIME_CMD = 0x3B,
Expand Down