Skip to content

WifiS3 Wificlient issues #309

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 27, 2024
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
76 changes: 42 additions & 34 deletions libraries/WiFiS3/src/WiFiClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using namespace std;

/* -------------------------------------------------------------------------- */
WiFiClient::WiFiClient() : _sock(-1), destroy_at_distructor(true), rx_buffer(nullptr) {
WiFiClient::WiFiClient() : _sock(-1), destroy_at_distructor(true), rx_buffer(nullptr) {
rx_buffer = std::shared_ptr<FifoBuffer<uint8_t,RX_BUFFER_DIM>>(new FifoBuffer<uint8_t,RX_BUFFER_DIM>());
}
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
WiFiClient::WiFiClient(int s) : _sock(s), destroy_at_distructor(false), rx_buffer(nullptr) {
rx_buffer = std::shared_ptr<FifoBuffer<uint8_t,RX_BUFFER_DIM>>(new FifoBuffer<uint8_t,RX_BUFFER_DIM>());
Expand All @@ -18,23 +18,29 @@ WiFiClient::WiFiClient(int s) : _sock(s), destroy_at_distructor(false), rx_buffe
WiFiClient::~WiFiClient() { }
/* -------------------------------------------------------------------------- */

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
WiFiClient::WiFiClient(const WiFiClient& c) {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
_sock = c._sock;
rx_buffer = c.rx_buffer;
}

/* -------------------------------------------------------------------------- */
void WiFiClient::getSocket() {
/* -------------------------------------------------------------------------- */
if(_sock >= 0 && !connected()) {
// if sock >= 0 -> it means we were connected, but something happened and we need
// to reset this socket in order to be able to connect again
stop();
}

if(_sock == -1) {
string res = "";
modem.begin();
if(modem.write(string(PROMPT(_BEGINCLIENT)),res, "%s" , CMD(_BEGINCLIENT))) {
_sock = atoi(res.c_str());
}
}
}
}

/* -------------------------------------------------------------------------- */
Expand All @@ -45,7 +51,7 @@ int WiFiClient::connect(IPAddress ip, uint16_t port){

/* -------------------------------------------------------------------------- */
int WiFiClient::connect(const char *host, uint16_t port){
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
getSocket();
if(_sock >= 0) {
string res = "";
Expand All @@ -55,17 +61,18 @@ int WiFiClient::connect(const char *host, uint16_t port){
return 1;
}
} else {
if(modem.write(string(PROMPT(_CLIENTCONNECTNAME)),res, "%s%d,%s,%d\r\n" , CMD_WRITE(_CLIENTCONNECTNAME), _sock, host,port)) {
return 1;
}
if(modem.write(string(PROMPT(_CLIENTCONNECTNAME)),res, "%s%d,%s,%d\r\n" , CMD_WRITE(_CLIENTCONNECTNAME), _sock, host,port)) {
return 1;
}
}
}

return 0;
}

/* -------------------------------------------------------------------------- */
size_t WiFiClient::write(uint8_t b){
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
return write(&b, 1);
}

Expand All @@ -79,15 +86,14 @@ size_t WiFiClient::write(const uint8_t *buf, size_t size){
if(modem.passthrough(buf,size)) {
return size;
}

}
return 0;

}

/* -------------------------------------------------------------------------- */
int WiFiClient::available() {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int rv = 0;
if(_sock >= 0 && rx_buffer != nullptr) {
if(rx_buffer->available() > 0) {
Expand All @@ -109,17 +115,17 @@ int WiFiClient::available() {

/* -------------------------------------------------------------------------- */
int WiFiClient::_read() {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int rv = -1;
if(_sock >= 0 && rx_buffer != nullptr) {
string res = "";
uint32_t size = rx_buffer->freePositions() - 1;
modem.begin();

/* important - it works one shot */
modem.avoid_trim_results();
modem.read_using_size();

if(modem.write(string(PROMPT(_CLIENTRECEIVE)),res, "%s%d,%d\r\n" , CMD_WRITE(_CLIENTRECEIVE), _sock, size)) {
if(res.size() > 0) {
for(int i = 0, rv = 0; i < size && i < res.size(); i++) {
Expand All @@ -133,11 +139,11 @@ int WiFiClient::_read() {
}
}
return rv;
}
}

/* -------------------------------------------------------------------------- */
void WiFiClient::read_if_needed(size_t s) {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
if(rx_buffer != nullptr) {
if((size_t)rx_buffer->available() < s) {
_read();
Expand All @@ -147,12 +153,12 @@ void WiFiClient::read_if_needed(size_t s) {

/* -------------------------------------------------------------------------- */
int WiFiClient::read() {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
uint8_t b;
if(read(&b, 1) == 1) {
return b;
}
return -1;
return -1;
}

/* -------------------------------------------------------------------------- */
Expand All @@ -173,27 +179,27 @@ int WiFiClient::read(uint8_t *buf, size_t size) {
}
}
}
return rv;
return rv;
}

/* -------------------------------------------------------------------------- */
int WiFiClient::peek() {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int rv = -1;
if(_sock >= 0) {
string res = "";
modem.begin();
if(modem.write(string(PROMPT(_PEEK)),res, "%s%d\r\n" , CMD_WRITE(_PEEK), _sock)) {
rv = atoi(res.c_str());
}
}
}
return rv;
}


/* -------------------------------------------------------------------------- */
void WiFiClient::flush() {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
if(_sock >= 0) {
string res = "";
modem.begin();
Expand All @@ -203,18 +209,20 @@ void WiFiClient::flush() {

/* -------------------------------------------------------------------------- */
void WiFiClient::stop() {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
if(_sock >= 0) {
string res = "";
modem.begin();
modem.write(string(PROMPT(_CLIENTCLOSE)),res, "%s%d\r\n" , CMD_WRITE(_CLIENTCLOSE), _sock);
_sock = -1;
}

rx_buffer->clear();
}

/* -------------------------------------------------------------------------- */
uint8_t WiFiClient::connected() {
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
uint8_t rv = 0;
if(this->available() > 0) {
return 1;
Expand All @@ -224,16 +232,16 @@ uint8_t WiFiClient::connected() {
modem.begin();
if(modem.write(string(PROMPT(_CLIENTCONNECTED)),res, "%s%d\r\n" , CMD_WRITE(_CLIENTCONNECTED), _sock)) {
rv = atoi(res.c_str());
}
}
}

return rv;
}

/* -------------------------------------------------------------------------- */
bool WiFiClient::operator==(const WiFiClient& whs)
{
/* -------------------------------------------------------------------------- */
return _sock == whs._sock;
bool WiFiClient::operator==(const WiFiClient& whs) {
/* -------------------------------------------------------------------------- */
return _sock == whs._sock;
}

/* -------------------------------------------------------------------------- */
Expand All @@ -246,22 +254,22 @@ IPAddress WiFiClient::remoteIP() {
if(modem.write(string(PROMPT(_REMOTEIP)),res, "%s%d\r\n" , CMD_WRITE(_REMOTEIP), _sock)) {
ip.fromString(res.c_str());
return ip;
}
}
}
return IPAddress(0,0,0,0);
}

/* -------------------------------------------------------------------------- */
uint16_t WiFiClient::remotePort(){
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
uint16_t rv = 0;
if(_sock >= 0) {
string res = "";
modem.begin();
if(modem.write(string(PROMPT(_REMOTEPORT)),res, "%s%d\r\n" , CMD_WRITE(_REMOTEPORT), _sock)) {
rv = atoi(res.c_str());
return rv;
}
}
}
return rv;
return rv;
}
3 changes: 1 addition & 2 deletions libraries/WiFiS3/src/WiFiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class WiFiClient : public Client {
}

friend class WiFiServer;

using Print::write;

protected:
Expand All @@ -77,7 +77,6 @@ class WiFiClient : public Client {
std::shared_ptr<FifoBuffer<uint8_t,RX_BUFFER_DIM>> rx_buffer;
int _read();
void read_if_needed(size_t s);
void clear_buffer();
bool destroy_at_distructor;


Expand Down
Loading
Loading