diff --git a/host/host_central_mast/lib/NetworkConnectionManager/GPRSCredentials.h b/host/host_central_mast/lib/NetworkConnectionManager/GPRSCredentials.h new file mode 100644 index 0000000000000000000000000000000000000000..8cc838eef9ed62a1b8014561f696c8a016ee1052 --- /dev/null +++ b/host/host_central_mast/lib/NetworkConnectionManager/GPRSCredentials.h @@ -0,0 +1,15 @@ +// +// Created by zoe on 12/20/22. +// + +#ifndef HOST_CENTRAL_MAST_GPRSCREDENTIALS_H +#define HOST_CENTRAL_MAST_GPRSCREDENTIALS_H + +#include <string> + +struct GPRSCredentials { + std::string apn; + std::string user; + std::string password; +}; +#endif //HOST_CENTRAL_MAST_GPRSCREDENTIALS_H diff --git a/host/host_central_mast/lib/NetworkConnectionManager/LTEConnection.cpp b/host/host_central_mast/lib/NetworkConnectionManager/LTEConnection.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c509bf072ddfa235fab4debdacee361fa8ff096b --- /dev/null +++ b/host/host_central_mast/lib/NetworkConnectionManager/LTEConnection.cpp @@ -0,0 +1,101 @@ +// +// Created by zoe on 12/20/22. +// + +#include "LTEConnection.h" +#include "ModemFunctionalityLevel.h" +#include "LTEConnectionException.hpp" + +#include <utility> + +std::string LTEConnection::connect(const std::string &host, const std::string &port) { + return std::__cxx11::string(); +} + +LTEConnection::LTEConnection(TinyGsm &client, GPRSCredentials credentials) : client(client), + credentials( + std::move( + credentials)) {} + + +void LTEConnection::restartModem() { + // Restart takes quite some time + // To skip it, call init() instead of restart() + esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Restarting modem...\n"); + if (!client.restart()) { + esp_log_write(ESP_LOG_WARN, TAG_GSM.c_str(), + "Failed to restart modem, attempting to continue without restarting\n"); + } +} + +void LTEConnection::initModem() { + esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Initializing modem...\n"); + if (!client.init()) { + esp_log_write(ESP_LOG_WARN, TAG_GSM.c_str(), + "Failed to initialize modem, attempting to continue by restarting\n"); + // might not be the cleanest design + restartModem(); + } + setModemFunctionalityLevel(MINIMAL); +} + +void +LTEConnection::setModemFunctionalityLevel( + ModemFunctionalityLevel functionalityLevel) {// This sets the level of functionality of the modem. Full functionality is where the highest +// level of power is drawn. Minimum functionality (default) is where the lowest level of power is drawn. +// https://m2msupport.net/m2msupport/atcfun-set-phone-functionality/ + client.sendAT(("+CFUN=" + std::to_string(ModemFunctionalityLevel::MINIMAL) + " ").c_str()); + if (client.waitResponse(10000L) != 1) { + DBG(" +CFUN=0 false "); + } +} + +void LTEConnection::disableGPS() { + // Set SIM7000G GPIO4 LOW ,turn off GPS power + // CMD:AT+SGPIO=0,4,1,0 + // Only in version 20200415 is there a function to control GPS power + client.sendAT("+SGPIO=0,4,1,0"); + if (client.waitResponse(10000L) != 1) { + DBG(" SGPIO=0,4,1,0 false "); + } + client.disableGPS(); +} + +void LTEConnection::enableGPS() { + // Set SIM7000G GPIO4 LOW ,turn on GPS power + // CMD:AT+SGPIO=0,4,1,1 + // Only in version 20200415 is there a function to control GPS power + client.sendAT("+SGPIO=0,4,1,1"); + if (client.waitResponse(10000L) != 1) { + DBG(" SGPIO=0,4,1,1 false "); + } + client.enableGPS(); +} + +void LTEConnection::setNetworkMode(NetworkMode networkMode) { + + String res; + res = client.setNetworkMode(networkMode); + if (res != "1") { + DBG("setNetworkMode false "); + throw LTEConnectionException("Failed to set network mode"); + } + delay(200); +} + +void LTEConnection::setPreferredMode(Mode mode) { + /*AT+CBANDCFG=<mode>,<band>[,<band>…] + * <mode> "CAT-M" "NB-IOT" + * <band> The value of <band> must is in the band list of getting from AT+CBANDCFG=? + * For example, my SIM card carrier "NB-iot" supports B8. I will configure +CBANDCFG= "Nb-iot ",8 + */ + client.sendAT("+CBANDCFG=\"CAT-M\",8 "); + if (client.waitResponse(10000L) != 1) { + DBG(" +CBANDCFG=\"NB-IOT\" "); + } + delay(200); + +} + + + diff --git a/host/host_central_mast/lib/NetworkConnectionManager/LTEConnection.h b/host/host_central_mast/lib/NetworkConnectionManager/LTEConnection.h new file mode 100644 index 0000000000000000000000000000000000000000..a02298769e52234bd458a1bcc013fed08f6bed4d --- /dev/null +++ b/host/host_central_mast/lib/NetworkConnectionManager/LTEConnection.h @@ -0,0 +1,47 @@ +// +// Created by zoe on 12/20/22. +// + +#ifndef HOST_CENTRAL_MAST_LTECONNECTION_H +#define HOST_CENTRAL_MAST_LTECONNECTION_H + +#define TINY_GSM_MODEM_SIM7000 + +#include <TinyGsmClient.h> +#include "GPRSCredentials.h" +#include "StreamDebugger.h" +#include "ModemFunctionalityLevel.h" +#include "NetworkMode.h" +#include "Mode.h" + +static const std::string TAG_GSM = "GSM"; + +class LTEConnection { +private: + TinyGsm &client; + const GPRSCredentials credentials; + + +public: + LTEConnection(TinyGsm &client, GPRSCredentials credentials); + + + std::string connect(const std::string &host, const std::string &port); + + void enableGPS(); + + void disableGPS(); + + void initModem(); + + void restartModem(); + + void setNetworkMode(NetworkMode networkMode = NetworkMode::LTE_ONLY); + + void setPreferredMode(Mode mode = Mode::CAT_M); + + void setModemFunctionalityLevel(ModemFunctionalityLevel functionalityLevel = MINIMAL); +}; + + +#endif //HOST_CENTRAL_MAST_LTECONNECTION_H diff --git a/host/host_central_mast/lib/NetworkConnectionManager/LTEConnectionException.hpp b/host/host_central_mast/lib/NetworkConnectionManager/LTEConnectionException.hpp new file mode 100644 index 0000000000000000000000000000000000000000..1c9067bdbab1a9721b1d41df2b0bbb3aef47bd66 --- /dev/null +++ b/host/host_central_mast/lib/NetworkConnectionManager/LTEConnectionException.hpp @@ -0,0 +1,23 @@ +// +// Created by zoe on 12/20/22. +// + +#ifndef HOST_CENTRAL_MAST_LTECONNECTIONEXCEPTION_HPP +#define HOST_CENTRAL_MAST_LTECONNECTIONEXCEPTION_HPP + +#include <exception> +#include <string> + +class LTEConnectionException : public std::exception { +public: + explicit LTEConnectionException(const std::string &message) : message(message) {} + + const char *what() const noexcept override { + return message.c_str(); + } + +private: + std::string message; +}; + +#endif //HOST_CENTRAL_MAST_LTECONNECTIONEXCEPTION_HPP diff --git a/host/host_central_mast/lib/NetworkConnectionManager/Mode.h b/host/host_central_mast/lib/NetworkConnectionManager/Mode.h new file mode 100644 index 0000000000000000000000000000000000000000..bb248b7920ad62aca446830742948f6fe8de2a95 --- /dev/null +++ b/host/host_central_mast/lib/NetworkConnectionManager/Mode.h @@ -0,0 +1,14 @@ +// +// Created by zoe on 12/20/22. +// + +#ifndef HOST_CENTRAL_MAST_MODE_H +#define HOST_CENTRAL_MAST_MODE_H + +enum Mode { + CAT_M = 1, + NB_IOT = 2, + CAT_M_AND_NB_IOT = 3 +}; + +#endif //HOST_CENTRAL_MAST_MODE_H diff --git a/host/host_central_mast/lib/NetworkConnectionManager/ModemFunctionalityLevel.h b/host/host_central_mast/lib/NetworkConnectionManager/ModemFunctionalityLevel.h new file mode 100644 index 0000000000000000000000000000000000000000..35a458033a388af7a8d7fe3a37649b5ffbc458e7 --- /dev/null +++ b/host/host_central_mast/lib/NetworkConnectionManager/ModemFunctionalityLevel.h @@ -0,0 +1,14 @@ +// +// Created by zoe on 12/20/22. +// + +#ifndef HOST_CENTRAL_MAST_MODEMFUNCTIONALITYLEVEL_H +#define HOST_CENTRAL_MAST_MODEMFUNCTIONALITYLEVEL_H + +enum ModemFunctionalityLevel { + MINIMAL = 0, + FULL = 1, + DISABLE = 4, +}; + +#endif //HOST_CENTRAL_MAST_MODEMFUNCTIONALITYLEVEL_H diff --git a/host/host_central_mast/lib/NetworkConnectionManager/NetworkMode.h b/host/host_central_mast/lib/NetworkConnectionManager/NetworkMode.h new file mode 100644 index 0000000000000000000000000000000000000000..daafc19e6b94bcb6d6a818cb905606f559a23e04 --- /dev/null +++ b/host/host_central_mast/lib/NetworkConnectionManager/NetworkMode.h @@ -0,0 +1,15 @@ +// +// Created by zoe on 12/20/22. +// + +#ifndef HOST_CENTRAL_MAST_NETWORKMODE_H +#define HOST_CENTRAL_MAST_NETWORKMODE_H + +enum NetworkMode { + AUTOMATIC = 2, + GSM_ONLY = 13, + LTE_ONLY = 38, + GSM_AND_LTE_ONLY = 51 +}; + +#endif //HOST_CENTRAL_MAST_NETWORKMODE_H diff --git a/host/host_central_mast/lib/TimeManager/TimeManager.h b/host/host_central_mast/lib/TimeManager/TimeManager.h index 3079b9fc9f936154cc8205849f70b93647080baa..44b552bdf9acaae00ca5d74480454cd5fd559641 100644 --- a/host/host_central_mast/lib/TimeManager/TimeManager.h +++ b/host/host_central_mast/lib/TimeManager/TimeManager.h @@ -37,4 +37,4 @@ private: }; -#endif //HOST_CENTRAL_MAST_TIMEMANAGER_H +#endif //HOST_CENTRAL_MAST_TIMEMANAGER_H \ No newline at end of file