-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathNetwork.cpp
More file actions
84 lines (66 loc) · 2.52 KB
/
Copy pathNetwork.cpp
File metadata and controls
84 lines (66 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "Network.h"
#include "Hardware.h"
#include "MainUnit.h"
#include "Rtc.h"
#include "Config.h"
#include "Logging.h"
namespace network {
#define CURRENT_LOGGER "network"
timestamp_t _poweredOnTime;
namespace details {
void print(SIM808_NETWORK_REGISTRATION_STATE state, SIM808SignalQualityReport &report) {
#define CURRENT_LOGGER_FUNCTION "print"
NOTICE_FORMAT("%d, [%d %ddBm]", state, report.rssi, report.attenuation);
}
}
void powerOn() {
hardware::sim808::networkPowerOn();
_poweredOnTime = rtc::getTime();
}
void powerOff() {
hardware::sim808::networkPowerOff();
_poweredOnTime = 0;
}
SIM808_NETWORK_REGISTRATION_STATE waitForRegistered(uint32_t timeout, bool relativeToPowerOnTime = true) {
#define CURRENT_LOGGER_FUNCTION "waitForRegistered"
NOTICE;
SIM808_NETWORK_REGISTRATION_STATE currentStatus;
SIM808SignalQualityReport report;
uint8_t noReliableNetwork = 0;
if (relativeToPowerOnTime) timeout -= (rtc::getTime() - _poweredOnTime) * 1000;
currentStatus = hardware::sim808::device.getNetworkRegistrationStatus();
report = hardware::sim808::device.getSignalQuality();
do {
if (isAvailable(currentStatus)) break;
details::print(currentStatus, report);
if (report.rssi < NETWORK_NO_NETWORK_QUALITY_THRESHOLD) noReliableNetwork++;
else noReliableNetwork = 0;
if (noReliableNetwork > NETWORK_NO_NETWORK_TRIES) {
NOTICE_MSG("No reliable signal");
break; //after a while, no network really means no network. Bailing out
}
mainunit::deepSleep(NETWORK_INTERMEDIATE_TIMEOUT_MS / 1000);
timeout -= NETWORK_INTERMEDIATE_TIMEOUT_MS;
currentStatus = hardware::sim808::device.getNetworkRegistrationStatus();
report = hardware::sim808::device.getSignalQuality();
} while (timeout > 1);
details::print(currentStatus, report);
return currentStatus;
}
bool isAvailable(SIM808_NETWORK_REGISTRATION_STATE state) {
return static_cast<int8_t>(state) &
(static_cast<int8_t>(SIM808_NETWORK_REGISTRATION_STATE::REGISTERED) | static_cast<int8_t>(SIM808_NETWORK_REGISTRATION_STATE::ROAMING))
!= 0;
}
#if BACKUP_ENABLE_NETWORK
bool enableGprs() {
return hardware::sim808::device.enableGprs(config::main::value.network.apn);
}
#endif
bool sendSms(const char * msg) {
#define CURRENT_LOGGER_FUNCTION "sendSms"
const char * phoneNumber = config::main::value.contactPhone;
NOTICE_FORMAT("%s, %s", phoneNumber, msg);
return hardware::sim808::device.sendSms(phoneNumber, msg);
}
}