From f7e9656026303f95a8eb62db365eb697f0f79922 Mon Sep 17 00:00:00 2001
From: Zoe Pfister <zoe.pfister@uibk.ac.at>
Date: Thu, 12 Jan 2023 15:12:29 +0100
Subject: [PATCH] WIP: ConnectionManager updates

---
 .../ConnectionManager.cpp                     | 31 +++++++------------
 .../ConnectionManager.h                       |  4 +--
 .../GPRSCredentials.h                         |  7 +++++
 3 files changed, 20 insertions(+), 22 deletions(-)

diff --git a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp
index 2b1d106..73c526d 100644
--- a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp
+++ b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp
@@ -8,8 +8,8 @@
 
 #include <utility>
 
-ConnectionManager::ConnectionManager(TinyGsm &client, GPRSCredentials credentials)
-    : modem(client), credentials(std::move(credentials)) {}
+ConnectionManager::ConnectionManager(TinyGsm &modem, GPRSCredentials credentials)
+    : modem(modem), credentials(std::move(credentials)) {}
 
 void ConnectionManager::restartModem() {
     // Restart takes quite some time
@@ -24,10 +24,7 @@ void ConnectionManager::restartModem() {
 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 by restarting\n");
-        // might not be the cleanest design
-        restartModem();
+        esp_log_write(ESP_LOG_WARN, TAG_GSM.c_str(), "Failed to initialize modem, attempting to continue...\n");
     }
     setModemFunctionalityLevel(MINIMAL);
 }
@@ -37,7 +34,7 @@ void ConnectionManager::setModemFunctionalityLevel(
                                                   // 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(ModemFunctionalityLevel::MINIMAL) + " ").c_str());
+    modem.sendAT(("+CFUN=" + std::to_string(functionalityLevel) + " ").c_str());
     if (modem.waitResponse(10000L) != 1) {
         DBG(" +CFUN=0  false ");
     }
@@ -73,20 +70,15 @@ void ConnectionManager::setNetworkMode(NetworkMode networkMode) {
         DBG("setNetworkMode  false ");
         throw LTEConnectionException("Failed to set network mode");
     }
-    delay(200);
 }
 
 void ConnectionManager::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
-     */
-    modem.sendAT("+CBANDCFG=\"CAT-M\",8 ");
-    if (modem.waitResponse(10000L) != 1) {
-        DBG(" +CBANDCFG=\"NB-IOT\" ");
+    String res;
+    res = modem.setPreferredMode(mode);
+    if (res != "1") {
+        DBG("setPreferredMode  false ");
+        return;
     }
-    delay(200);
 }
 void ConnectionManager::setBand(BandMode bandMode, int band) {
     /*AT+CBANDCFG=<mode>,<band>[,<band>…]
@@ -101,7 +93,6 @@ void ConnectionManager::setBand(BandMode bandMode, int band) {
     if (modem.waitResponse(10000L) != 1) {
         DBG(" +CBANDCFG=\"NB-IOT\" ");
     }
-    delay(200);
 }
 bool ConnectionManager::gprsConnect() {
     return modem.gprsConnect(credentials.apn.c_str(), credentials.user.c_str(), credentials.password.c_str());
@@ -109,9 +100,9 @@ bool ConnectionManager::gprsConnect() {
 bool ConnectionManager::isNetworkConnected() {
     return modem.isNetworkConnected();
 }
-std::string ConnectionManager::connect(const std::string &host, int port, RequestInformation requestInformation) {
+std::string ConnectionManager::connect(int port, RequestInformation requestInformation) {
     TinyGsmClient client{modem};
-    if (!client.connect(host.c_str(), port)) {
+    if (!client.connect(requestInformation.host.c_str(), port)) {
         throw LTEConnectionException("Failed to connect to host");
     }
 
diff --git a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h
index ea475a4..7d1b8c0 100644
--- a/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h
+++ b/host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.h
@@ -23,9 +23,9 @@ class ConnectionManager {
     const GPRSCredentials credentials;
 
   public:
-    ConnectionManager(TinyGsm &client, GPRSCredentials credentials);
+    ConnectionManager(TinyGsm &modem, GPRSCredentials credentials);
 
-    std::string connect(const std::string &host, int port, RequestInformation requestInformation);
+    std::string connect(int port, RequestInformation requestInformation);
 
     void enableGPS();
 
diff --git a/host/host_central_mast/lib/NetworkConnectionManager/GPRSCredentials.h b/host/host_central_mast/lib/NetworkConnectionManager/GPRSCredentials.h
index 8cc838e..1029c27 100644
--- a/host/host_central_mast/lib/NetworkConnectionManager/GPRSCredentials.h
+++ b/host/host_central_mast/lib/NetworkConnectionManager/GPRSCredentials.h
@@ -6,10 +6,17 @@
 #define HOST_CENTRAL_MAST_GPRSCREDENTIALS_H
 
 #include <string>
+#include <utility>
 
 struct GPRSCredentials {
     std::string apn;
     std::string user;
     std::string password;
+
+    GPRSCredentials(std::string apn, std::string user, std::string password) {
+        this->apn = std::move(apn);
+        this->user = std::move(user);
+        this->password = std::move(password);
+    }
 };
 #endif //HOST_CENTRAL_MAST_GPRSCREDENTIALS_H
-- 
GitLab