From a20afc81a74593184585f14113ec09d293e50786 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 22 Jul 2020 14:48:19 +0200 Subject: [PATCH 1/2] Workaround: Calling attachGPRS multiple times is not handled well by the MRKGSM stack. But this is exactly what happens when the first call to ping fails. Consequently the attachGPRS part has been moved one state up so it will be only called once. --- src/Arduino_GSMConnectionHandler.cpp | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/Arduino_GSMConnectionHandler.cpp b/src/Arduino_GSMConnectionHandler.cpp index 951038bd..7d374db9 100644 --- a/src/Arduino_GSMConnectionHandler.cpp +++ b/src/Arduino_GSMConnectionHandler.cpp @@ -62,6 +62,15 @@ NetworkConnectionState GSMConnectionHandler::update_handleInit() { Debug.print(DBG_INFO, "SIM card ok"); _gsm.setTimeout(GSM_TIMEOUT); + + GSM3_NetworkStatus_t const network_status = _gprs.attachGPRS(_apn, _login, _pass, true); + Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", network_status); + if (network_status == GSM3_NetworkStatus_t::ERROR) + { + Debug.print(DBG_ERROR, "GPRS attach failed"); + Debug.print(DBG_ERROR, "Make sure the antenna is connected and reset your board."); + return NetworkConnectionState::ERROR; + } return NetworkConnectionState::CONNECTING; } else @@ -73,14 +82,6 @@ NetworkConnectionState GSMConnectionHandler::update_handleInit() NetworkConnectionState GSMConnectionHandler::update_handleConnecting() { - GSM3_NetworkStatus_t const network_status = _gprs.attachGPRS(_apn, _login, _pass, true); - Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", network_status); - if (network_status == GSM3_NetworkStatus_t::ERROR) - { - Debug.print(DBG_ERROR, "GPRS attach failed"); - Debug.print(DBG_ERROR, "Make sure the antenna is connected and reset your board."); - return NetworkConnectionState::ERROR; - } Debug.print(DBG_INFO, "Sending PING to outer space..."); int const ping_result = _gprs.ping("time.arduino.cc"); Debug.print(DBG_INFO, "GPRS.ping(): %d", ping_result); From 21825b4ffe3ccaa553198dc204b61bf92ddd6a87 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 22 Jul 2020 14:50:04 +0200 Subject: [PATCH 2/2] Restructuring control flow for better readability --- src/Arduino_GSMConnectionHandler.cpp | 29 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Arduino_GSMConnectionHandler.cpp b/src/Arduino_GSMConnectionHandler.cpp index 7d374db9..629699b6 100644 --- a/src/Arduino_GSMConnectionHandler.cpp +++ b/src/Arduino_GSMConnectionHandler.cpp @@ -58,26 +58,25 @@ unsigned long GSMConnectionHandler::getTime() NetworkConnectionState GSMConnectionHandler::update_handleInit() { - if (_gsm.begin(_pin) == GSM_READY) + if (_gsm.begin(_pin) != GSM_READY) { - Debug.print(DBG_INFO, "SIM card ok"); - _gsm.setTimeout(GSM_TIMEOUT); - - GSM3_NetworkStatus_t const network_status = _gprs.attachGPRS(_apn, _login, _pass, true); - Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", network_status); - if (network_status == GSM3_NetworkStatus_t::ERROR) - { - Debug.print(DBG_ERROR, "GPRS attach failed"); - Debug.print(DBG_ERROR, "Make sure the antenna is connected and reset your board."); - return NetworkConnectionState::ERROR; - } - return NetworkConnectionState::CONNECTING; + Debug.print(DBG_ERROR, "SIM not present or wrong PIN"); + return NetworkConnectionState::ERROR; } - else + + Debug.print(DBG_INFO, "SIM card ok"); + _gsm.setTimeout(GSM_TIMEOUT); + + GSM3_NetworkStatus_t const network_status = _gprs.attachGPRS(_apn, _login, _pass, true); + Debug.print(DBG_DEBUG, "GPRS.attachGPRS(): %d", network_status); + if (network_status == GSM3_NetworkStatus_t::ERROR) { - Debug.print(DBG_ERROR, "SIM not present or wrong PIN"); + Debug.print(DBG_ERROR, "GPRS attach failed"); + Debug.print(DBG_ERROR, "Make sure the antenna is connected and reset your board."); return NetworkConnectionState::ERROR; } + + return NetworkConnectionState::CONNECTING; } NetworkConnectionState GSMConnectionHandler::update_handleConnecting()