diff --git a/examples/ModemTest/ModemTest.ino b/examples/ModemTest/ModemTest.ino index 5d35408..09dd253 100644 --- a/examples/ModemTest/ModemTest.ino +++ b/examples/ModemTest/ModemTest.ino @@ -23,8 +23,8 @@ const uint8_t GSM_UART_RX = 41; const uint8_t GSM_UART_TX = 42; const uint16_t GSM_UART_BUFFER_SIZE = 1024; const char* GSM_APN = "internet"; -const char* GSM_USER = nullptr; -const char* GSM_PASS = nullptr; +const char* GSM_USER = ""; +const char* GSM_PASS = ""; // TCP configuration const char* TCP_SERVER = "api.ipify.org"; diff --git a/src/SIM7600Common.h b/src/SIM7600Common.h index d32d462..59057c3 100644 --- a/src/SIM7600Common.h +++ b/src/SIM7600Common.h @@ -117,6 +117,7 @@ namespace SIM7600 { X(EmptyLine) \ X(GPSNotFixed) \ X(InvalidAntennaVoltage) \ + X(InvalidAPN) \ X(InvalidBuffer) \ X(InvalidCallback) \ X(InvalidClientName) \ diff --git a/src/SIM7600Modem.cpp b/src/SIM7600Modem.cpp index d545c3f..0d3293a 100644 --- a/src/SIM7600Modem.cpp +++ b/src/SIM7600Modem.cpp @@ -813,17 +813,41 @@ bool Modem::isCurrentlyRegisteredOnNetwork() const { return (_registered_on_netw RegStatus Modem::getCurrentRegistrationStatus() const { return _current_reg_status; } Status Modem::configureAPN(const char* apn, const char* user, const char* password) { + if (apn == nullptr) { + SIM7600_LOGE(tag, "APN cannot be null"); + return Status::InvalidAPN; + } + + const char* user_str; + const char* pass_str; + + if (user == nullptr) { + user_str = nullptr; + } else if (strlen(user) == 0) { + user_str = nullptr; + } else { + user_str = user; + } + + if (password == nullptr) { + pass_str = nullptr; + } else if (strlen(password) == 0) { + pass_str = nullptr; + } else { + pass_str = password; + } + SIM7600_LOGI(tag, "Configuring APN: %s, USER: %s, PASS: %s", apn, - user ? user : "-empty-", - password ? password : "-empty-"); + (user_str != nullptr) ? user_str : "-empty-", + (pass_str != nullptr) ? pass_str : "-empty-"); Status status; // Set authentication if needed - if (user != nullptr && password != nullptr) { - status = sendATCmd("AT+CGAUTH=1,1,\"%s\",\"%s\"", password, user); + if (user_str != nullptr && pass_str != nullptr) { + status = sendATCmd("AT+CGAUTH=1,1,\"%s\",\"%s\"", pass_str, user_str); if (status != Status::Success) return status; status = waitForResponse(AT_OK); diff --git a/src/SIM7600Modem.h b/src/SIM7600Modem.h index f0b448c..177fa6c 100644 --- a/src/SIM7600Modem.h +++ b/src/SIM7600Modem.h @@ -434,9 +434,9 @@ class Modem { /** * @brief Configure the APN settings for GPRS connection. - * @param apn Access Point Name. - * @param user Username for APN, if required. - * @param password Password for APN, if required. + * @param apn Access Point Name. Cannot be nullptr or an empty string. + * @param user Username for APN, if required. Can be nullptr or an empty string. + * @param password Password for APN, if required. Can be nullptr or an empty string. * @return Status::Success on success, error code otherwise. */ Status configureAPN(const char* apn, const char* user = nullptr, const char* password = nullptr);