Skip to content

Commit

Permalink
Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
vshymanskyy committed Dec 16, 2016
1 parent 87298ea commit 7b99fd6
Showing 1 changed file with 32 additions and 48 deletions.
80 changes: 32 additions & 48 deletions TinyGsmClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ class GsmClient : public Client
* Basic functions
*/
bool begin() {
return init();
}

bool init() {
if (!autoBaud()) {
return false;
}
Expand All @@ -218,7 +222,7 @@ class GsmClient : public Client

bool autoBaud(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT("");
sendAT(GF(""));
if (waitResponse(200) == 1) {
delay(100);
return true;
Expand Down Expand Up @@ -254,10 +258,6 @@ class GsmClient : public Client
*/

bool restart() {
return resetSoft();
}

bool resetSoft() {
if (!autoBaud()) {
return false;
}
Expand All @@ -270,27 +270,7 @@ class GsmClient : public Client
return false;
}
delay(3000);
return begin();
}

// Reboot the module by setting the specified pin LOW, then HIGH.
// (The pin should be connected to a P-MOSFET)
bool resetHard(int pwrPin) {
powerOff(pwrPin);
delay(100);
return powerOn(pwrPin);
}

void powerOff(int pwrPin) {
pinMode(pwrPin, OUTPUT);
digitalWrite(pwrPin, LOW);
}

bool powerOn(int pwrPin) {
pinMode(pwrPin, OUTPUT);
digitalWrite(pwrPin, HIGH);
delay(3000);
return begin();
return init();
}

/*
Expand All @@ -304,7 +284,7 @@ class GsmClient : public Client

String getSimCCID() {
sendAT(GF("+ICCID"));
if (waitResponse(GF(GSM_NL "+ICCID: ")) != 1) {
if (waitResponse(GF(GSM_NL "+ICCID:")) != 1) {
return "";
}
String res = stream.readStringUntil('\n');
Expand All @@ -316,7 +296,7 @@ class GsmClient : public Client
SimStatus getSimStatus(unsigned long timeout = 10000L) {
for (unsigned long start = millis(); millis() - start < timeout; ) {
sendAT(GF("+CPIN?"));
if (waitResponse(GF(GSM_NL "+CPIN: ")) != 1) {
if (waitResponse(GF(GSM_NL "+CPIN:")) != 1) {
delay(1000);
continue;
}
Expand All @@ -334,20 +314,21 @@ class GsmClient : public Client

RegStatus getRegistrationStatus() {
sendAT(GF("+CREG?"));
if (waitResponse(GF(GSM_NL "+CREG: 0,")) != 1) {
if (waitResponse(GF(GSM_NL "+CREG:")) != 1) {
return REG_UNKNOWN;
}
streamSkipUntil(','); // Skip format (0)
int status = stream.readStringUntil('\n').toInt();
waitResponse();
return (RegStatus)status;
}

String getOperator() {
sendAT(GF("+COPS?"));
if (waitResponse(GF(GSM_NL "+COPS: ")) != 1) {
if (waitResponse(GF(GSM_NL "+COPS:")) != 1) {
return "";
}
stream.readStringUntil('"'); // Skip mode and format
streamSkipUntil('"'); // Skip mode and format
String res = stream.readStringUntil('"');
waitResponse();
return res;
Expand Down Expand Up @@ -493,9 +474,8 @@ class GsmClient : public Client
if (waitResponse(GF(GSM_NL "DATA ACCEPT:")) != 1) {
return -1;
}
stream.readStringUntil(',');
String data = stream.readStringUntil('\n');
return data.toInt();
streamSkipUntil(','); // Skip mux
return stream.readStringUntil('\n').toInt();
}

size_t modemRead(size_t size, uint8_t mux) {
Expand All @@ -510,20 +490,20 @@ class GsmClient : public Client
return 0;
}
#endif
stream.readStringUntil(','); // Skip mode 2/3
stream.readStringUntil(','); // Skip mux
streamSkipUntil(','); // Skip mode 2/3
streamSkipUntil(','); // Skip mux
size_t len = stream.readStringUntil(',').toInt();
sockets[mux]->sock_available = stream.readStringUntil('\n').toInt();

for (size_t i=0; i<len; i++) {
#ifdef GSM_USE_HEX
while (stream.available() < 2) { delay(1); }
while (stream.available() < 2) {}
char buf[4] = { 0, };
buf[0] = stream.read();
buf[1] = stream.read();
char c = strtol(buf, NULL, 16);
#else
while (stream.available() < 1) { delay(1); }
while (!stream.available()) {}
char c = stream.read();
#endif
sockets[mux]->rx.put(c);
Expand All @@ -535,16 +515,11 @@ class GsmClient : public Client
size_t modemGetAvailable(uint8_t mux) {
sendAT(GF("+CIPRXGET=4,"), mux);
size_t result = 0;
for (byte i = 0; i < 2; i++) {
int res = waitResponse(GF("+CIPRXGET:"), GFP(GSM_OK), GFP(GSM_ERROR));
if (res == 1) {
stream.readStringUntil(','); // Skip mode 4
stream.readStringUntil(','); // Skip mux
result = stream.readStringUntil('\n').toInt();
} else if (res == 2) {
} else {
return result;
}
if (waitResponse(GF("+CIPRXGET:")) == 1) {
streamSkipUntil(','); // Skip mode 4
streamSkipUntil(','); // Skip mux
result = stream.readStringUntil('\n').toInt();
waitResponse();
}
if (!result) {
sockets[mux]->sock_connected = modemGetConnected(mux);
Expand Down Expand Up @@ -573,6 +548,15 @@ class GsmClient : public Client

int streamRead() { return stream.read(); }

bool streamSkipUntil(char c) { //TODO: timeout
while (true) {
while (!stream.available()) {}
if (stream.read() == c)
return true;
}
return false;
}

template<typename... Args>
void sendAT(Args... cmd) {
streamWrite("AT", cmd..., GSM_NL);
Expand Down

0 comments on commit 7b99fd6

Please sign in to comment.