-
Notifications
You must be signed in to change notification settings - Fork 66
/
ThingerTinyGSM.h
142 lines (116 loc) · 4.25 KB
/
ThingerTinyGSM.h
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// The MIT License (MIT)
//
// Copyright (c) 2017 THINK BIG LABS SL
// Author: alvarolb@gmail.com (Alvaro Luis Bustamante)
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef THINGER_TINY_GSM_H
#define THINGER_TINY_GSM_H
#include "ThingerClient.h"
#define NETWORK_REGISTER_TIMEOUT 15000
#define NETWORK_GPRS_TIMEOUT 15000
class ThingerTinyGSM : public ThingerClient {
public:
ThingerTinyGSM(const char* user, const char* device, const char* device_credential, Stream& serial) :
ThingerClient(client_, user, device, device_credential),
modem_(serial),
client_(modem_)
{
}
~ThingerTinyGSM(){
}
protected:
virtual bool network_connected(){
return modem_.isNetworkConnected() && modem_.isGprsConnected();
}
virtual bool connect_network(){
if(apn_==nullptr) {
THINGER_DEBUG("NETWORK", "Cannot connect without setting the APN!")
return false;
}
if(module_reset_) module_reset_();
if(!modem_.isNetworkConnected()){
THINGER_DEBUG("NETWORK", "Initializing Modem...")
if(!modem_.restart()){
THINGER_DEBUG("NETWORK", "Cannot init modem! Is it connected?")
return false;
}else{
THINGER_DEBUG_VALUE("NETWORK", "Modem Info: ", modem_.getModemInfo());
// Unlock your SIM card with a PIN if needed
if(pin_!=nullptr && modem_.getSimStatus() != 3){
THINGER_DEBUG_VALUE("NETWORK", "Unlocking SIM... ", pin_)
if(!modem_.simUnlock(pin_)){
THINGER_DEBUG("NETWORK", "Cannot unlock SIM!")
return false;
}
}
THINGER_DEBUG("NETWORK", "Waiting for Network...")
if(!modem_.waitForNetwork(NETWORK_REGISTER_TIMEOUT)) {
THINGER_DEBUG("NETWORK", "Cannot connect network!")
return false;
}
THINGER_DEBUG("NETWORK", "Network Connected!")
}
}
if(!modem_.isGprsConnected()){
THINGER_DEBUG("NETWORK", "Connecting to APN...")
if (!modem_.gprsConnect(apn_, user_, password_)) {
THINGER_DEBUG("NETWORK", "Cannot connect to APN!")
return false;
}
}
return true;
}
#ifdef _DISABLE_TLS_
virtual bool secure_connection(){
return false;
}
#endif
public:
TinyGsm& getTinyGsm(){
return modem_;
}
TinyGsmClient& getTinyGsmClient(){
return client_;
}
void setAPN(const char* APN, const char* user=nullptr, const char* password=nullptr){
apn_ = APN;
user_ = user;
password_ = password;
}
void setPIN(const char* pin){
pin_ = pin;
}
void setModuleReset(std::function<void()> module_reset){
module_reset_ = module_reset;
}
private:
const char* apn_ = nullptr;
const char* user_ = nullptr;
const char* password_ = nullptr;
const char* pin_ = nullptr;
TinyGsm modem_;
#ifdef _DISABLE_TLS_
TinyGsmClient client_;
#else
TinyGsmClientSecure client_;
#endif
std::function<void()> module_reset_;
};
#endif