Skip to content

Commit

Permalink
SocketWrapper MbedClient debugging readSocket
Browse files Browse the repository at this point in the history
  • Loading branch information
JAndrassy committed Jul 2, 2024
1 parent 2ece915 commit a5fc13f
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions libraries/SocketWrapper/src/MbedClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,31 @@ void arduino::MbedClient::readSocket() {
int ret = NSAPI_ERROR_WOULD_BLOCK;
do {
mutex->lock();
if (sock != nullptr && rxBuffer.availableForStore() == 0) {
if (sock == nullptr) {
mutex->unlock();
goto cleanup;
}
if (rxBuffer.availableForStore() == 0) {
mutex->unlock();
yield();
continue;
} else if (sock == nullptr) {
goto cleanup;
}
ret = sock->recv(data, rxBuffer.availableForStore());
if (ret < 0 && ret != NSAPI_ERROR_WOULD_BLOCK) {
mutex->unlock();
goto cleanup;
}
if (ret == NSAPI_ERROR_WOULD_BLOCK || ret == 0) {
yield();
mutex->unlock();
continue;
yield();
break;
}
for (int i = 0; i < ret; i++) {
rxBuffer.store_char(data[i]);
}
mutex->unlock();
_status = true;
} while (ret == NSAPI_ERROR_WOULD_BLOCK || ret > 0);
} while (true);
}
cleanup:
_status = false;
Expand Down Expand Up @@ -98,6 +101,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
}

if (static_cast<TCPSocket *>(sock)->open(getNetwork()) != NSAPI_ERROR_OK) {
_status = false;
return 0;
}

Expand All @@ -117,6 +121,7 @@ int arduino::MbedClient::connect(SocketAddress socketAddress) {
configureSocket(sock);
_status = true;
} else {
sock->close();
_status = false;
}

Expand Down Expand Up @@ -148,6 +153,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
}

if (static_cast<TLSSocket *>(sock)->open(getNetwork()) != NSAPI_ERROR_OK) {
_status = false;
return 0;
}

Expand Down Expand Up @@ -179,6 +185,7 @@ int arduino::MbedClient::connectSSL(SocketAddress socketAddress) {
configureSocket(sock);
_status = true;
} else {
sock->close();
_status = false;
}

Expand Down Expand Up @@ -209,12 +216,14 @@ size_t arduino::MbedClient::write(uint8_t c) {
}

size_t arduino::MbedClient::write(const uint8_t *buf, size_t size) {
if (sock == nullptr)
if (_status == false)
return 0;

mutex->lock();
sock->set_timeout(_timeout);
int ret = sock->send(buf, size);
sock->set_blocking(false);
mutex->unlock();
return ret >= 0 ? ret : 0;
}

Expand All @@ -224,7 +233,7 @@ int arduino::MbedClient::available() {
}

int arduino::MbedClient::read() {
if (sock == nullptr)
if (sock == nullptr || mutex == nullptr)
return -1;
mutex->lock();
if (!available()) {
Expand All @@ -238,7 +247,7 @@ int arduino::MbedClient::read() {
}

int arduino::MbedClient::read(uint8_t *data, size_t len) {
if (sock == nullptr)
if (sock == nullptr || mutex == nullptr)
return 0;
mutex->lock();
int avail = available();
Expand Down

0 comments on commit a5fc13f

Please sign in to comment.