Skip to content

Commit a5c38d1

Browse files
committed
Fix issue with peek function
1 parent a363b19 commit a5c38d1

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

WiFi/WiFiClient.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,22 @@ int WiFiClient::connect(IPAddress ip, uint16_t port) {
3535
{
3636
ServerDrv::startClient(uint32_t(ip), port, _sock);
3737
WiFiClass::_state[_sock] = _sock;
38-
while(!connected());
38+
39+
unsigned long start = millis();
40+
41+
// wait 4 second for the connection to close
42+
while (!connected() && millis() - start < 10000)
43+
delay(1);
44+
45+
if (!connected())
46+
{
47+
Serial.println("timeout on client connection");
48+
return 0;
49+
}
50+
else
51+
Serial.println("CONNECTED");
3952
}else{
53+
Serial.println("No Socket available");
4054
return 0;
4155
}
4256
return 1;
@@ -94,8 +108,12 @@ int WiFiClient::read(uint8_t* buf, size_t size) {
94108
}
95109

96110
int WiFiClient::peek() {
97-
//TODO to be implemented
98-
return 0;
111+
uint8_t b;
112+
if (!available())
113+
return -1;
114+
115+
ServerDrv::getData(_sock, &b, 1);
116+
return b;
99117
}
100118

101119
void WiFiClient::flush() {
@@ -112,10 +130,11 @@ void WiFiClient::stop() {
112130

113131
unsigned long start = millis();
114132

133+
115134
// wait a second for the connection to close
116135
while (status() != CLOSED && millis() - start < 1000)
117136
delay(1);
118-
137+
Serial.print("Stop client! Status:");Serial.println(status(),10);
119138
_sock = 255;
120139
}
121140

@@ -125,7 +144,12 @@ uint8_t WiFiClient::connected() {
125144
return 0;
126145
} else {
127146
uint8_t s = status();
128-
147+
/*
148+
if (s== SYN_SENT) Serial.print("*");
149+
else{
150+
Serial.print("Status:"); Serial.println(s,10);
151+
}
152+
*/
129153
return !(s == LISTEN || s == CLOSED || s == FIN_WAIT_1 ||
130154
s == FIN_WAIT_2 || s == TIME_WAIT ||
131155
s == SYN_SENT || s== SYN_RCVD ||

WiFi/utility/server_drv.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,13 @@ uint8_t ServerDrv::availData(uint8_t sock)
146146
return false;
147147
}
148148

149-
bool ServerDrv::getData(uint8_t sock, uint8_t *data)
149+
bool ServerDrv::getData(uint8_t sock, uint8_t *data, uint8_t peek)
150150
{
151151
WAIT_FOR_SLAVE_SELECT();
152152
// Send Command
153-
SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_1);
154-
SpiDrv::sendParam(&sock, sizeof(sock), LAST_PARAM);
153+
SpiDrv::sendCmd(GET_DATA_TCP_CMD, PARAM_NUMS_2);
154+
SpiDrv::sendParam(&sock, sizeof(sock));
155+
SpiDrv::sendParam(peek, LAST_PARAM);
155156

156157
//Wait the reply elaboration
157158
SpiDrv::waitForSlaveReady();

WiFi/utility/server_drv.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class ServerDrv
1818

1919
static uint8_t getClientState(uint8_t sock);
2020

21-
static bool getData(uint8_t sock, uint8_t *data);
21+
static bool getData(uint8_t sock, uint8_t *data, uint8_t peek = 0);
2222

2323
static bool getDataBuf(uint8_t sock, uint8_t *data, uint16_t *len);
2424

0 commit comments

Comments
 (0)