Skip to content
Snippets Groups Projects

NTP Refactor into dev

Merged Zoe Pfister requested to merge 38-ntp-server-redundancy into develop
14 files
+ 1016
448
Compare changes
  • Side-by-side
  • Inline
Files
14
//
// Created by zoe on 12/20/22.
//
#include "ConnectionManager.h"
#include "LTEConnectionException.hpp"
#include "ModemFunctionalityLevel.h"
#include <utility>
ConnectionManager::ConnectionManager(TinyGsm &modem, GPRSCredentials credentials, ModemPins modemPins)
: modem(modem), credentials(std::move(credentials)), modemPins(modemPins) {}
void ConnectionManager::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 (!modem.restart()) {
esp_log_write(ESP_LOG_WARN, TAG_GSM.c_str(),
"Failed to restart modem, attempting to continue without restarting\n");
}
}
void ConnectionManager::initModem() {
esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "Initializing modem...\n");
if (!modem.init()) {
esp_log_write(ESP_LOG_WARN, TAG_GSM.c_str(), "Failed to initialize modem, attempting to continue...\n");
}
setModemFunctionalityLevel(MINIMAL);
}
void ConnectionManager::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/
modem.sendAT(("+CFUN=" + std::to_string(functionalityLevel) + " ").c_str());
if (modem.waitResponse(10000L) != 1) {
DBG(" +CFUN=0 false ");
}
}
void ConnectionManager::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
modem.sendAT("+SGPIO=0,4,1,0");
if (modem.waitResponse(10000L) != 1) {
DBG(" SGPIO=0,4,1,0 false ");
}
modem.disableGPS();
}
void ConnectionManager::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
modem.sendAT("+SGPIO=0,4,1,1");
if (modem.waitResponse(10000L) != 1) {
DBG(" SGPIO=0,4,1,1 false ");
}
modem.enableGPS();
}
void ConnectionManager::setNetworkMode(NetworkMode networkMode) {
String res;
res = modem.setNetworkMode(networkMode);
if (res != "1") {
DBG("setNetworkMode false ");
throw LTEConnectionException("Failed to set network mode");
}
}
void ConnectionManager::setPreferredMode(Mode mode) {
String res;
res = modem.setPreferredMode(mode);
if (res != "1") {
DBG("setPreferredMode false ");
return;
}
}
void ConnectionManager::setBand(BandMode bandMode, int band) {
/*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
*/
bandMode == BandMode::BAND_CAT_M ? modem.sendAT(("+CBANDCFG=\"CAT-M\"," + std::to_string(band) + " ").c_str())
: modem.sendAT(("+CBANDCFG=\"NB-IOT\"," + std::to_string(band) + " ").c_str());
if (modem.waitResponse(10000L) != 1) {
DBG(" +CBANDCFG=\"NB-IOT\" ");
}
}
bool ConnectionManager::gprsConnect() {
return modem.gprsConnect(credentials.apn.c_str(), credentials.user.c_str(), credentials.password.c_str());
}
bool ConnectionManager::isNetworkConnected() {
return modem.isNetworkConnected();
}
std::string ConnectionManager::connect(int port, RequestInformation requestInformation) {
TinyGsmClient client{modem, 0};
if (!client.connect(requestInformation.host.c_str(), port)) {
throw LTEConnectionException("Failed to connect to host");
}
String request = requestInformation.buildRequest();
client.print(request);
String line;
// print response
while (client.connected()) {
line += client.readStringUntil('\n');
if (line == "\r") {
esp_log_write(ESP_LOG_DEBUG, TAG_GSM.c_str(), "headers received\n");
break;
}
}
client.stop();
return line.c_str();
}
void ConnectionManager::modemPowerOn() {
pinMode(modemPins.pwr, OUTPUT);
digitalWrite(modemPins.pwr, LOW);
delay(1000);
digitalWrite(modemPins.pwr, HIGH);
}
void ConnectionManager::modemPowerOff() {
modem.sendAT("+CPOWD=1");
if (modem.waitResponse(10000L) != 1) {
esp_log_write(ESP_LOG_WARN, TAG_GSM.c_str(), "Failed to power off modem\n");
}
modem.poweroff();
pinMode(modemPins.pwr, OUTPUT);
digitalWrite(modemPins.pwr, LOW);
delay(1500);
digitalWrite(modemPins.pwr, HIGH);
}
void ConnectionManager::unlockSimCard() {
// Unlock your SIM card with a PIN if needed
if (modem.getSimStatus() != 3) {
modem.simUnlock(credentials.password.c_str());
}
}
bool ConnectionManager::waitForNetwork() {
if (!modem.waitForNetwork(600000L, true)) {
delay(10000);
return false;
}
return true;
}
bool ConnectionManager::gprsDisconnect() {
return modem.gprsDisconnect();
}
Loading