Skip to content

Commit 198cbdc

Browse files
committed
Use +UUSORD URC in GSMServer to determine GSMServer::available
Also use AT+USORD to check if child socket is still alive
1 parent a47762d commit 198cbdc

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

src/GSMServer.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ enum {
2929
SERVER_STATE_WAIT_CLOSE_SOCKET
3030
};
3131

32-
3332
GSMServer::GSMServer(uint16_t port, bool synch) :
3433
_port(port),
3534
_synch(synch),
@@ -39,6 +38,7 @@ GSMServer::GSMServer(uint16_t port, bool synch) :
3938
for (int i = 0; i < MAX_CHILD_SOCKETS; i++) {
4039
_childSockets[i].socket = -1;
4140
_childSockets[i].accepted = false;
41+
_childSockets[i].available = 0;
4242
}
4343

4444
MODEM.addUrcHandler(this);
@@ -150,14 +150,21 @@ GSMClient GSMServer::available(bool synch)
150150
// no new accepted sockets, search for one with data to be read
151151
for (int i = 0; i < MAX_CHILD_SOCKETS; i++) {
152152
if (_childSockets[i].socket != -1) {
153-
GSMClient client(_childSockets[i].socket, true);
153+
// check if socket is still alive
154+
MODEM.sendf("AT+USORD=%d,0", _childSockets[i].socket);
155+
if (MODEM.waitForResponse(10000) != 1) {
156+
// closed
157+
_childSockets[i].socket = -1;
158+
_childSockets[i].accepted = false;
159+
_childSockets[i].available = 0;
160+
161+
continue;
162+
}
154163

155-
if (client.available()) {
164+
if (_childSockets[i].available) {
165+
_childSockets[i].available = 0;
156166
socket = _childSockets[i].socket;
157167
break;
158-
} else if (!client.connected()) {
159-
_childSockets[i].socket = -1;
160-
_childSockets[i].accepted = false;
161168
}
162169
}
163170
}
@@ -185,6 +192,8 @@ size_t GSMServer::write(const uint8_t *buf, size_t sz)
185192
{
186193
size_t written = 0;
187194

195+
MODEM.poll();
196+
188197
if (_socket != -1) {
189198
for (int i = 0; i < MAX_CHILD_SOCKETS; i++) {
190199
if (_childSockets[i].socket != -1) {
@@ -223,6 +232,7 @@ void GSMServer::handleUrc(const String& urc)
223232
if (_childSockets[i].socket == -1) {
224233
_childSockets[i].socket = socket;
225234
_childSockets[i].accepted = true;
235+
_childSockets[i].available = 0;
226236

227237
break;
228238
}
@@ -234,6 +244,20 @@ void GSMServer::handleUrc(const String& urc)
234244
if (_childSockets[i].socket == socket) {
235245
_childSockets[i].socket = -1;
236246
_childSockets[i].accepted = false;
247+
_childSockets[i].available = 0;
248+
249+
break;
250+
}
251+
}
252+
} else if (urc.startsWith("+UUSORD: ")) {
253+
int socket = urc.charAt(9) - '0';
254+
255+
for (int i = 0; i < MAX_CHILD_SOCKETS; i++) {
256+
if (_childSockets[i].socket == socket) {
257+
int commaIndex = urc.indexOf(',');
258+
if (commaIndex != -1) {
259+
_childSockets[i].available = urc.substring(commaIndex + 1).toInt();
260+
}
237261

238262
break;
239263
}

src/GSMServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class GSMServer : public Server, public ModemUrcHandler {
103103
struct {
104104
int socket;
105105
bool accepted;
106+
int available;
106107
} _childSockets[MAX_CHILD_SOCKETS];
107108
};
108109

0 commit comments

Comments
 (0)