Skip to content

Commit 6169ad3

Browse files
authored
Merge pull request #14 from sandeepmistry/socket-urc-available
Use socket URC's to determine available socket data
2 parents 92dd9fe + 198cbdc commit 6169ad3

File tree

7 files changed

+89
-32
lines changed

7 files changed

+89
-32
lines changed

src/GSMClient.cpp

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ GSMClient::GSMClient(int socket, bool synch) :
4545
_port(0),
4646
_ssl(false),
4747
_writeSync(true),
48-
_peek(-1)
48+
_peek(-1),
49+
_available(0)
4950
{
50-
MODEM.addUrcHandler(this);
51+
MODEM.addUrcHandler(this);
5152
}
5253

5354
GSMClient::~GSMClient()
@@ -363,6 +364,9 @@ int GSMClient::read(uint8_t *buf, size_t size)
363364
buf[i] = (n1 << 4) | n2;
364365
}
365366

367+
_available = 0;
368+
MODEM.poll();
369+
366370
return size;
367371
}
368372

@@ -395,24 +399,9 @@ int GSMClient::available()
395399
return 0;
396400
}
397401

398-
String response;
399-
400-
MODEM.sendf("AT+USORD=%d,0", _socket, 0);
401-
if (MODEM.waitForResponse(10000, &response) == 1) {
402-
if (response.startsWith("+USORD: ")) {
403-
int commaIndex = response.indexOf(',');
404-
405-
if (commaIndex != -1) {
406-
response.remove(0, commaIndex + 1);
407-
408-
return response.toInt();
409-
}
410-
}
411-
} else {
412-
_socket = -1;
413-
}
402+
MODEM.poll();
414403

415-
return 0;
404+
return _available;
416405
}
417406

418407
int GSMClient::peek()
@@ -448,14 +437,23 @@ void GSMClient::handleUrc(const String& urc)
448437
if (socket == _socket) {
449438
// this socket closed
450439
_socket = -1;
440+
_available = 0;
451441
}
452-
} else if (urc.startsWith("+UUSORD: ") && urc.endsWith(",4294967295")) {
453-
// SSL disconnect
442+
} else if (urc.startsWith("+UUSORD: ")) {
454443
int socket = urc.charAt(9) - '0';
455444

456445
if (socket == _socket) {
457-
// this socket closed
458-
_socket = -1;
446+
if (urc.endsWith(",4294967295")) {
447+
// SSL disconnect
448+
// this socket closed
449+
_socket = -1;
450+
_available = 0;
451+
} else {
452+
int commaIndex = urc.indexOf(',');
453+
if (commaIndex != -1) {
454+
_available = urc.substring(commaIndex + 1).toInt();
455+
}
456+
}
459457
}
460458
}
461459
}

src/GSMClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
#include "Modem.h"
2626

27-
2827
class GSMClient : public Client, public ModemUrcHandler {
2928

3029
public:
@@ -146,6 +145,7 @@ class GSMClient : public Client, public ModemUrcHandler {
146145
bool _writeSync;
147146
String _response;
148147
int _peek;
148+
int _available;
149149
};
150150

151151
#endif

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

src/GSMUdp.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
GSMUDP::GSMUDP() :
2525
_socket(-1),
26+
_packetReceived(false),
2627
_txIp((uint32_t)0),
2728
_txHost(NULL),
2829
_txPort(0),
@@ -32,6 +33,12 @@ GSMUDP::GSMUDP() :
3233
_rxSize(0),
3334
_rxIndex(0)
3435
{
36+
MODEM.addUrcHandler(this);
37+
}
38+
39+
GSMUDP::~GSMUDP()
40+
{
41+
MODEM.removeUrcHandler(this);
3542
}
3643

3744
uint8_t GSMUDP::begin(uint16_t port)
@@ -161,6 +168,13 @@ size_t GSMUDP::write(const uint8_t *buffer, size_t size)
161168

162169
int GSMUDP::parsePacket()
163170
{
171+
MODEM.poll();
172+
173+
if (!_packetReceived) {
174+
return 0;
175+
}
176+
_packetReceived = false;
177+
164178
String response;
165179

166180
MODEM.sendf("AT+USORF=%d,%d", _socket, sizeof(_rxBuffer));
@@ -218,6 +232,8 @@ int GSMUDP::parsePacket()
218232
_rxBuffer[i] = (n1 << 4) | n2;
219233
}
220234

235+
MODEM.poll();
236+
221237
return _rxSize;
222238
}
223239

@@ -274,3 +290,14 @@ uint16_t GSMUDP::remotePort()
274290
{
275291
return _rxPort;
276292
}
293+
294+
void GSMUDP::handleUrc(const String& urc)
295+
{
296+
if (urc.startsWith("+UUSORF: ")) {
297+
int socket = urc.charAt(9) - '0';
298+
299+
if (socket == _socket) {
300+
_packetReceived = true;
301+
}
302+
}
303+
}

src/GSMUdp.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@
2222

2323
#include <Udp.h>
2424

25-
class GSMUDP : public UDP {
25+
#include "Modem.h"
26+
27+
class GSMUDP : public UDP, public ModemUrcHandler {
2628

2729
public:
2830
GSMUDP(); // Constructor
31+
virtual ~GSMUDP();
32+
2933
virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use
3034
virtual void stop(); // Finish with the UDP socket
3135

@@ -69,8 +73,11 @@ class GSMUDP : public UDP {
6973
// Return the port of the host who sent the current incoming packet
7074
virtual uint16_t remotePort();
7175

76+
virtual void handleUrc(const String& urc);
77+
7278
private:
7379
int _socket;
80+
bool _packetReceived;
7481

7582
IPAddress _txIp;
7683
const char* _txHost;

src/Modem.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ModemClass {
7676
String _buffer;
7777
String* _responseDataStorage;
7878

79-
#define MAX_URC_HANDLERS 10
79+
#define MAX_URC_HANDLERS 10 // 7 sockets + GPRS + GSMLocation + GSMVoiceCall
8080
static bool _debug;
8181
static ModemUrcHandler* _urcHandlers[MAX_URC_HANDLERS];
8282
};

0 commit comments

Comments
 (0)