Skip to content
Snippets Groups Projects
Verified Commit 858467ad authored by Zoe Michaela Dietmar Pfister's avatar Zoe Michaela Dietmar Pfister :gay_pride_flag:
Browse files

WIP: NetworkManager

parent f5e24b13
No related branches found
No related tags found
2 merge requests!39Merge Develop into Main,!23NTP Refactor into dev
//
// 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
//
// 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);
}
//
// 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
//
// 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
//
// 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
//
// 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
//
// 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
......@@ -37,4 +37,4 @@ private:
};
#endif //HOST_CENTRAL_MAST_TIMEMANAGER_H
#endif //HOST_CENTRAL_MAST_TIMEMANAGER_H
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment