Skip to content

Commit ea8a118

Browse files
committed
Adding Client::peek() in Ethernet library (issue #349).
1 parent 76641d1 commit ea8a118

File tree

6 files changed

+30
-4
lines changed

6 files changed

+30
-4
lines changed

Diff for: libraries/Ethernet/Client.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ int Client::read() {
8383
return b;
8484
}
8585

86+
int Client::peek() {
87+
uint8_t b;
88+
if (!available())
89+
return -1;
90+
::peek(_sock, &b);
91+
return b;
92+
}
93+
8694
void Client::flush() {
8795
while (available())
8896
read();

Diff for: libraries/Ethernet/Client.h

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Client : public Stream {
1717
virtual void write(const uint8_t *buf, size_t size);
1818
virtual int available();
1919
virtual int read();
20+
virtual int peek();
2021
virtual void flush();
2122
void stop();
2223
uint8_t connected();

Diff for: libraries/Ethernet/utility/socket.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,19 @@ uint16_t recv(SOCKET s, uint8_t *buf, uint16_t len)
158158
}
159159

160160

161+
/**
162+
* @brief Returns the first byte in the receive queue (no checking)
163+
*
164+
* @return
165+
*/
166+
uint16_t peek(SOCKET s, uint8_t *buf)
167+
{
168+
W5100.recv_data_processing(s, buf, 1, 1);
169+
170+
return 1;
171+
}
172+
173+
161174
/**
162175
* @brief This function is an application I/F function which is used to send the data for other then TCP mode.
163176
* Unlike TCP transmission, The peer's destination address and the port is needed.

Diff for: libraries/Ethernet/utility/socket.h

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ extern void disconnect(SOCKET s); // disconnect the connection
1010
extern uint8_t listen(SOCKET s); // Establish TCP connection (Passive connection)
1111
extern uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len); // Send data (TCP)
1212
extern uint16_t recv(SOCKET s, uint8_t * buf, uint16_t len); // Receive data (TCP)
13+
extern uint16_t peek(SOCKET s, uint8_t *buf);
1314
extern uint16_t sendto(SOCKET s, const uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port); // Send data (UDP/IP RAW)
1415
extern uint16_t recvfrom(SOCKET s, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port); // Receive data (UDP/IP RAW)
1516

Diff for: libraries/Ethernet/utility/w5100.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,16 @@ void W5100Class::send_data_processing(SOCKET s, uint8_t *data, uint16_t len)
8888
}
8989

9090

91-
void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len)
91+
void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek)
9292
{
9393
uint16_t ptr;
9494
ptr = readSnRX_RD(s);
9595
read_data(s, (uint8_t *)ptr, data, len);
96-
ptr += len;
97-
writeSnRX_RD(s, ptr);
96+
if (!peek)
97+
{
98+
ptr += len;
99+
writeSnRX_RD(s, ptr);
100+
}
98101
}
99102

100103
void W5100Class::read_data(SOCKET s, volatile uint8_t *src, volatile uint8_t *dst, uint16_t len)

Diff for: libraries/Ethernet/utility/w5100.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class W5100Class {
155155
* and after copy the data from receive buffer update the Rx write pointer register.
156156
* User should read upper byte first and lower byte later to get proper value.
157157
*/
158-
void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len);
158+
void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek = 0);
159159

160160
inline void setGatewayIp(uint8_t *_addr);
161161
inline void getGatewayIp(uint8_t *_addr);

0 commit comments

Comments
 (0)