Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Sensor System
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Institut für Informatik
QE Research Group
FORTE
Sensor System
Merge requests
!23
NTP Refactor into dev
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
NTP Refactor into dev
38-ntp-server-redundancy
into
develop
Overview
0
Commits
6
Pipelines
0
Changes
14
Merged
Zoe Pfister
requested to merge
38-ntp-server-redundancy
into
develop
2 years ago
Overview
0
Commits
6
Pipelines
0
Changes
14
Expand
Closes
#38 (closed)
0
0
Merge request reports
Compare
develop
develop (base)
and
latest version
latest version
67602608
6 commits,
2 years ago
14 files
+
1016
−
448
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
14
Search (e.g. *.vue) (Ctrl+P)
host/host_central_mast/lib/NetworkConnectionManager/ConnectionManager.cpp
0 → 100644
+
159
−
0
Options
//
// 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