Skip to content

Commit

Permalink
Merge pull request #962 from spark/feature/electron_g350_tcp_fix
Browse files Browse the repository at this point in the history
Electron G350 TCP Client fix for #896
  • Loading branch information
m-mcgowan committed Apr 14, 2016
2 parents 38a5e80 + 8a14816 commit a6f1d97
Showing 1 changed file with 99 additions and 93 deletions.
192 changes: 99 additions & 93 deletions hal/src/electron/modem/mdm_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ std::recursive_mutex mdm_mutex;
//! registration done check helper (no need to poll further)
#define REG_DONE(r) ((r == REG_HOME) || (r == REG_ROAMING) || (r == REG_DENIED))
//! helper to make sure that lock unlock pair is always balanced
#define LOCK() std::lock_guard<std::recursive_mutex> __mdm_guard(mdm_mutex);
#define LOCK() std::lock_guard<std::recursive_mutex> __mdm_guard(mdm_mutex);
//! helper to make sure that lock unlock pair is always balanced
#define UNLOCK()

Expand Down Expand Up @@ -1668,19 +1668,19 @@ int MDMParser::socketSend(int socket, const char * buf, int len)
blk = cnt;
bool ok = false;
{
LOCK();
if (ISSOCKET(socket)) {
sendFormated("AT+USOWR=%d,%d\r\n",_sockets[socket].handle,blk);
if (RESP_PROMPT == waitFinalResp()) {
HAL_Delay_Milliseconds(50);
send(buf, blk);
if (RESP_OK == waitFinalResp())
ok = true;
}
}
UNLOCK();
LOCK();
if (ISSOCKET(socket)) {
sendFormated("AT+USOWR=%d,%d\r\n",_sockets[socket].handle,blk);
if (RESP_PROMPT == waitFinalResp()) {
HAL_Delay_Milliseconds(50);
send(buf, blk);
if (RESP_OK == waitFinalResp())
ok = true;
}
}
UNLOCK();
}
if (!ok)
if (!ok)
return MDM_SOCKET_ERROR;
buf += blk;
cnt -= blk;
Expand All @@ -1698,17 +1698,17 @@ int MDMParser::socketSendTo(int socket, MDM_IP ip, int port, const char * buf, i
blk = cnt;
bool ok = false;
{
LOCK();
if (ISSOCKET(socket)) {
sendFormated("AT+USOST=%d,\"" IPSTR "\",%d,%d\r\n",_sockets[socket].handle,IPNUM(ip),port,blk);
if (RESP_PROMPT == waitFinalResp()) {
HAL_Delay_Milliseconds(50);
send(buf, blk);
if (RESP_OK == waitFinalResp())
ok = true;
}
}
UNLOCK();
LOCK();
if (ISSOCKET(socket)) {
sendFormated("AT+USOST=%d,\"" IPSTR "\",%d,%d\r\n",_sockets[socket].handle,IPNUM(ip),port,blk);
if (RESP_PROMPT == waitFinalResp()) {
HAL_Delay_Milliseconds(50);
send(buf, blk);
if (RESP_OK == waitFinalResp())
ok = true;
}
}
UNLOCK();
}
if (!ok)
return MDM_SOCKET_ERROR;
Expand All @@ -1722,10 +1722,10 @@ int MDMParser::socketReadable(int socket)
{
int pending = MDM_SOCKET_ERROR;
if (_cancel_all_operations)
return MDM_SOCKET_ERROR;
return MDM_SOCKET_ERROR;
LOCK();
if (ISSOCKET(socket) && _sockets[socket].connected) {
//DEBUG_D("socketReadable(%d)\r\n", socket);
//DEBUG_D("socketReadable(%d)\r\n", socket);
// allow to receive unsolicited commands
waitFinalResp(NULL, NULL, 0);
if (_sockets[socket].connected)
Expand Down Expand Up @@ -1761,57 +1761,63 @@ int MDMParser::socketRecv(int socket, char* buf, int len)
*/
system_tick_t start = HAL_Timer_Get_Milli_Seconds();
while (len) {
// DEBUG_D("socketRecv: LEN: %d\r\n", len);
int blk = MAX_SIZE; // still need space for headers and unsolicited commands
if (len < blk) blk = len;
bool ok = false;
{
LOCK();
if (ISSOCKET(socket)) {
if (_sockets[socket].connected) {
int available = socketReadable(socket);
if (available<0) {
ok = false;
}
else
{
if (blk > available) // only read up to the amount available. When 0,
blk = available;// skip reading and check timeout.
if (blk > 0) {
DEBUG_D("socketRecv: _cbUSORD\r\n");
sendFormated("AT+USORD=%d,%d\r\n",_sockets[socket].handle, blk);
USORDparam param;
param.buf = buf;
if (RESP_OK == waitFinalResp(_cbUSORD, &param)) {
blk = param.len;
_sockets[socket].pending -= blk;
len -= blk;
cnt += blk;
buf += blk;
ok = true;
}
} else if (!TIMEOUT(start, _sockets[socket].timeout_ms)) {
// DEBUG_D("socketRecv: WAIT FOR URCs\r\n");
ok = (WAIT == waitFinalResp(NULL,NULL,0)); // wait for URCs
} else {
// DEBUG_D("socketRecv: TIMEOUT\r\n");
len = 0;
ok = true;
}
}
} else {
// DEBUG_D("socketRecv: SOCKET NOT CONNECTED\r\n");
len = 0;
ok = true;
}
}
UNLOCK();
LOCK();
if (ISSOCKET(socket)) {
if (_sockets[socket].connected) {
int available = socketReadable(socket);
if (available<0) {
// DEBUG_D("socketRecv: SOCKET CLOSED or NO AVAIL DATA\r\n");
// Socket may have been closed remotely during read, or no more data to read.
// Zero the `len` to break out of the while(len), and set `ok` to true so
// we return the `cnt` recv'd up until the socket was closed.
len = 0;
ok = true;
}
else
{
if (blk > available) // only read up to the amount available. When 0,
blk = available;// skip reading and check timeout.
if (blk > 0) {
DEBUG_D("socketRecv: _cbUSORD\r\n");
sendFormated("AT+USORD=%d,%d\r\n",_sockets[socket].handle, blk);
USORDparam param;
param.buf = buf;
if (RESP_OK == waitFinalResp(_cbUSORD, &param)) {
blk = param.len;
_sockets[socket].pending -= blk;
len -= blk;
cnt += blk;
buf += blk;
ok = true;
}
} else if (!TIMEOUT(start, _sockets[socket].timeout_ms)) {
// DEBUG_D("socketRecv: WAIT FOR URCs\r\n");
ok = (WAIT == waitFinalResp(NULL,NULL,0)); // wait for URCs
} else {
// DEBUG_D("socketRecv: TIMEOUT\r\n");
len = 0;
ok = true;
}
}
} else {
// DEBUG_D("socketRecv: SOCKET NOT CONNECTED\r\n");
len = 0;
ok = true;
}
}
UNLOCK();
}
if (!ok) {
if (!ok) {
// DEBUG_D("socketRecv: ERROR\r\n");
return MDM_SOCKET_ERROR;
}
}
//DEBUG_D("socketRecv: %d \"%*s\"\r\n", cnt, cnt, buf-cnt);
// DEBUG_D("socketRecv: %d \"%*s\"\r\n", cnt, cnt, buf-cnt);
return cnt;
}

Expand Down Expand Up @@ -1846,31 +1852,31 @@ int MDMParser::socketRecvFrom(int socket, MDM_IP* ip, int* port, char* buf, int
if (len < blk) blk = len;
bool ok = false;
{
LOCK();
if (ISSOCKET(socket)) {
if (blk > 0) {
sendFormated("AT+USORF=%d,%d\r\n",_sockets[socket].handle, blk);
USORFparam param;
param.buf = buf;
if (RESP_OK == waitFinalResp(_cbUSORF, &param)) {
*ip = param.ip;
*port = param.port;
blk = param.len;
_sockets[socket].pending -= blk;
len -= blk;
cnt += blk;
buf += blk;
len = 0; // done
ok = true;
}
} else if (!TIMEOUT(start, _sockets[socket].timeout_ms)) {
ok = (WAIT == waitFinalResp(NULL,NULL,0)); // wait for URCs
} else {
len = 0; // no more data and socket closed or timed-out
ok = true;
}
}
UNLOCK();
LOCK();
if (ISSOCKET(socket)) {
if (blk > 0) {
sendFormated("AT+USORF=%d,%d\r\n",_sockets[socket].handle, blk);
USORFparam param;
param.buf = buf;
if (RESP_OK == waitFinalResp(_cbUSORF, &param)) {
*ip = param.ip;
*port = param.port;
blk = param.len;
_sockets[socket].pending -= blk;
len -= blk;
cnt += blk;
buf += blk;
len = 0; // done
ok = true;
}
} else if (!TIMEOUT(start, _sockets[socket].timeout_ms)) {
ok = (WAIT == waitFinalResp(NULL,NULL,0)); // wait for URCs
} else {
len = 0; // no more data and socket closed or timed-out
ok = true;
}
}
UNLOCK();
}
if (!ok) {
DEBUG_D("socketRecv: ERROR\r\n");
Expand Down

0 comments on commit a6f1d97

Please sign in to comment.