-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
IP.cpp
202 lines (179 loc) · 8.47 KB
/
IP.cpp
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "IP.h"
/* IP Class */
IP::IP(DTE &dte, GPRS &gprs) {
this->dte = &dte;
this->gprs = &gprs;
bearerProfile[0] = (struct BearerProfile){1, (struct ConnStatus){3, ""}, (struct ConnParam){"", "", "", "", "", 2}};
bearerProfile[1] = (struct BearerProfile){2, (struct ConnStatus){3, ""}, (struct ConnParam){"", "", "", "", "", 2}};
bearerProfile[2] = (struct BearerProfile){3, (struct ConnStatus){3, ""}, (struct ConnParam){"", "", "", "", "", 2}};
}
bool IP::atBearerSettings(unsigned char cmdType, unsigned char cid, const char paramTag[], const char paramValue[]) {
char buffer[20 + strlen(paramTag) + strlen(paramValue)]; // "AT+SAPBR=X,X,\"{paramTag}\",\"{paramValue}\"\r"
struct BearerProfile bearerProfile;
if (cmdType <= 5 && cmdType != 3) {
const __FlashStringHelper *command = F("AT+SAPBR=%d,%d\r");
sprintf_P(buffer, (const char *)command, cmdType, cid);
} else if (cmdType == 3) {
const __FlashStringHelper *command = F("AT+SAPBR=%d,%d,\"%s\",\"%s\"\r");
sprintf_P(buffer, (const char *)command, cmdType, cid, paramTag, paramValue);
} else
return false;
const __FlashStringHelper *response = F("+SAPBR:");
bearerProfile = this->bearerProfile[cid - 1];
dte->clearReceivedBuffer();
if (!dte->ATCommand(buffer)) return false;
if (cmdType == 2) {
if (!dte->ATResponseContain(response)) return false;
char *pointer = strstr_P(dte->getResponse(), (const char *)response) + strlen_P((const char *)response);
char *str = strtok(pointer, ",\"");
for (unsigned char i = 0; i < 3 && str != NULL; i++) {
if (i == 0) bearerProfile.cid = str[0] - '0';
if (i == 1) bearerProfile.connStatus.status = str[0] - '0';
if (i == 2) strcpy(bearerProfile.connStatus.ip, str);
str = strtok(NULL, ",\"");
}
if (!dte->ATResponseOk()) return false;
this->bearerProfile[cid - 1] = bearerProfile;
} else if (cmdType == 4) {
if (!dte->ATResponseContain(response)) return false;
for (unsigned char i = 0; i < 6; i++) {
if (!dte->ATResponse()) return false;
if (dte->isResponseContain(F("CONTYPE: "))) {
const __FlashStringHelper *response = F("CONTYPE: ");
char *pointer = strstr_P(dte->getResponse(), (const char *)response) + strlen_P((const char *)response);
char *str = strtok(pointer, ",\"");
strcpy(bearerProfile.connParam.contype, str);
} else if (dte->isResponseContain(F("APN: "))) {
const __FlashStringHelper *response = F("APN: ");
char *pointer = strstr_P(dte->getResponse(), (const char *)response) + strlen_P((const char *)response);
char *str = strtok(pointer, ",\"");
strcpy(bearerProfile.connParam.apn, str);
} else if (dte->isResponseContain(F("USER: "))) {
const __FlashStringHelper *response = F("USER: ");
char *pointer = strstr_P(dte->getResponse(), (const char *)response) + strlen_P((const char *)response);
char *str = strtok(pointer, ",\"");
strcpy(bearerProfile.connParam.user, str);
} else if (dte->isResponseContain(F("PWD: "))) {
const __FlashStringHelper *response = F("PWD: ");
char *pointer = strstr_P(dte->getResponse(), (const char *)response) + strlen_P((const char *)response);
char *str = strtok(pointer, ",\"");
strcpy(bearerProfile.connParam.pwd, str);
} else if (dte->isResponseContain(F("PHONENUM: "))) {
const __FlashStringHelper *response = F("PHONENUM: ");
char *pointer = strstr_P(dte->getResponse(), (const char *)response) + strlen_P((const char *)response);
char *str = strtok(pointer, ",\"");
strcpy(bearerProfile.connParam.phonenum, str);
} else if (dte->isResponseContain(F("RATE: "))) {
const __FlashStringHelper *response = F("RATE: ");
char *pointer = strstr_P(dte->getResponse(), (const char *)response) + strlen_P((const char *)response);
char *str = strtok(pointer, ",\"");
bearerProfile.connParam.rate = str[0] - '0';
}
}
if (!dte->ATResponseOk()) return false;
this->bearerProfile[cid - 1] = bearerProfile;
} else if (!dte->ATResponseOk(10000))
return false;
return true;
}
bool IP::atBearerSettings(unsigned char cmdType, unsigned char cid, const __FlashStringHelper *paramTag, const char paramValue[]) {
char buffer[strlen_P((const char *)paramTag) + 1];
strcpy_P(buffer, (const char *)paramTag);
return atBearerSettings(cmdType, cid, buffer, paramValue);
}
bool IP::atBearerSettings(unsigned char cmdType, unsigned char cid, const char paramTag[], const __FlashStringHelper *paramValue) {
char buffer[strlen_P((const char *)paramValue) + 1];
strcpy_P(buffer, (const char *)paramValue);
return atBearerSettings(cmdType, cid, paramTag, buffer);
}
bool IP::atBearerSettings(unsigned char cmdType, unsigned char cid, const __FlashStringHelper *paramTag, const __FlashStringHelper *paramValue) {
char buffer[strlen_P((const char *)paramValue) + 1];
strcpy_P(buffer, (const char *)paramValue);
return atBearerSettings(cmdType, cid, paramTag, buffer);
}
void IP::setConnectionParamGprs(const char apn[], const char user[], const char pwd[], unsigned char cid) {
struct ConnParam connParam = this->bearerProfile[cid - 1].connParam;
bool change = false;
if (strcmp_P(connParam.contype, (const char *)F("GPRS")) != 0) {
atBearerSettings(3, cid, F("CONTYPE"), F("GPRS"));
change = true;
}
if (strcmp_P(connParam.apn, apn) != 0) {
atBearerSettings(3, cid, F("APN"), apn);
change = true;
}
if (strcmp_P(connParam.user, user) != 0) {
atBearerSettings(3, cid, F("USER"), user);
change = true;
}
if (strcmp_P(connParam.pwd, pwd) != 0) {
atBearerSettings(3, cid, F("PWD"), pwd);
change = true;
}
if (change) {
atBearerSettings(5, 1);
getConnectionParam(cid);
}
}
void IP::setConnectionParamGprs(const __FlashStringHelper *apn, const char user[], const char pwd[], unsigned char cid) {
char buffer[strlen_P((const char *)apn) + 1];
strcpy_P(buffer, (const char *)apn);
setConnectionParamGprs(buffer, user, pwd, cid);
}
void IP::setConnectionParamGprs(const char apn[], const __FlashStringHelper *user, const char pwd[], unsigned char cid) {
char buffer[strlen_P((const char *)user) + 1];
strcpy_P(buffer, (const char *)user);
setConnectionParamGprs(apn, buffer, pwd, cid);
}
void IP::setConnectionParamGprs(const __FlashStringHelper *apn, const __FlashStringHelper *user, const char pwd[], unsigned char cid) {
char buffer[strlen_P((const char *)user) + 1];
strcpy_P(buffer, (const char *)user);
setConnectionParamGprs(apn, buffer, pwd, cid);
}
void IP::setConnectionParamGprs(const char apn[], const char user[], const __FlashStringHelper *pwd, unsigned char cid) {
char buffer[strlen_P((const char *)pwd) + 1];
strcpy_P(buffer, (const char *)pwd);
setConnectionParamGprs(apn, user, buffer, cid);
}
void IP::setConnectionParamGprs(const __FlashStringHelper *apn, const char user[], const __FlashStringHelper *pwd, unsigned char cid) {
char buffer[strlen_P((const char *)pwd) + 1];
strcpy_P(buffer, (const char *)pwd);
setConnectionParamGprs(apn, user, buffer, cid);
}
void IP::setConnectionParamGprs(const char apn[], const __FlashStringHelper *user, const __FlashStringHelper *pwd, unsigned char cid) {
char buffer[strlen_P((const char *)pwd) + 1];
strcpy_P(buffer, (const char *)pwd);
setConnectionParamGprs(apn, user, buffer, cid);
}
void IP::setConnectionParamGprs(const __FlashStringHelper *apn, const __FlashStringHelper *user, const __FlashStringHelper *pwd, unsigned char cid) {
char buffer[strlen_P((const char *)pwd) + 1];
strcpy_P(buffer, (const char *)pwd);
setConnectionParamGprs(apn, user, buffer, cid);
}
struct ConnStatus IP::getConnectionStatus(unsigned char cid) {
atBearerSettings(2, cid);
return bearerProfile[cid - 1].connStatus;
}
struct ConnParam IP::getConnectionParam(unsigned char cid) {
atBearerSettings(4, cid);
return bearerProfile[cid - 1].connParam;
}
bool IP::openConnection(unsigned char cid) {
struct ConnStatus connStatus = getConnectionStatus(cid);
if (connStatus.status == 3) {
if (!gprs->isAttached()) return false;
if (!atBearerSettings(1, cid)) return false;
connStatus = getConnectionStatus(cid);
}
if (connStatus.status >= 2) return false;
return true;
}
bool IP::closeConnection(unsigned char cid) {
struct ConnStatus connStatus = getConnectionStatus(cid);
if (connStatus.status == 1) {
if (!atBearerSettings(0, cid)) return false;
connStatus = getConnectionStatus(cid);
}
if (connStatus.status <= 1) return false;
return true;
}