Skip to content

Commit 6dd86b3

Browse files
committed
Cosmetic items, plus two nice fixes courtesy of John Malsbury: (1) Fixed a bug where device seemed to be always asleep unless explicitly using sleep line, and (2) a race condition might cause RX data to be missed.
1 parent e0ad7e5 commit 6dd86b3

File tree

1 file changed

+13
-11
lines changed

1 file changed

+13
-11
lines changed

IridiumSBD.cpp

+13-11
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ bool IridiumSBD::waitForATResponse(char *response, int responseSize, const char
442442
bool done = false;
443443
int matchPromptPos = 0; // Matches chars in prompt
444444
int matchTerminatorPos = 0; // Matches chars in terminator
445-
// 0=prompt not seen, 1=prompt seen, gathering response, 2=response gathered, looking for terminator
446-
int promptState = prompt ? 0 : 2;
445+
enum {LOOKING_FOR_PROMPT, GATHERING_RESPONSE, LOOKING_FOR_TERMINATOR}
446+
int promptState = prompt ? LOOKING_FOR_PROMPT : LOOKING_FOR_TERMINATOR;
447447
console(F("<< "));
448448
for (unsigned long start=millis(); millis() - start < 1000UL * atTimeout;)
449449
{
@@ -457,12 +457,12 @@ bool IridiumSBD::waitForATResponse(char *response, int responseSize, const char
457457
if (prompt)
458458
switch(promptState)
459459
{
460-
case 0: // matching prompt
460+
case LOOKING_FOR_PROMPT:
461461
if (c == prompt[matchPromptPos])
462462
{
463463
++matchPromptPos;
464464
if (prompt[matchPromptPos] == '\0')
465-
promptState = 1;
465+
promptState = GATHERING_RESPONSE;
466466
}
467467

468468
else
@@ -471,12 +471,12 @@ bool IridiumSBD::waitForATResponse(char *response, int responseSize, const char
471471
}
472472

473473
break;
474-
case 1: // gathering reponse from end of prompt to first \r
474+
case GATHERING_RESPONSE: // gathering reponse from end of prompt to first \r
475475
if (response)
476476
{
477477
if (c == '\r' || responseSize < 2)
478478
{
479-
promptState = 2;
479+
promptState = LOOKING_FOR_TERMINATOR;
480480
}
481481
else
482482
{
@@ -610,14 +610,16 @@ int IridiumSBD::doSBDRB(uint8_t *rxBuffer, size_t *prxBufferSize)
610610
console(F("[Binary size:"));
611611
console(size);
612612
console(F("]"));
613-
for (int i=0; i<size; ++i)
613+
614+
for (uint16_t bytesRead = 0; bytesRead < size;)
614615
{
615616
if (cancelled())
616617
return ISBD_CANCELLED;
617618

618619
if (stream.available())
619620
{
620621
uint8_t c = stream.read();
622+
bytesRead++;
621623
if (rxBuffer && prxBufferSize)
622624
if (*prxBufferSize > 0)
623625
{
@@ -661,14 +663,16 @@ void IridiumSBD::power(bool on)
661663
{
662664
static unsigned long lastPowerOnTime = 0UL;
663665

666+
this->asleep = !on;
667+
664668
if (this->sleepPin == -1)
665669
return;
666670

671+
pinMode(this->sleepPin, OUTPUT);
672+
667673
if (on)
668674
{
669675
dbg(F("Powering on RockBLOCK...!\r\n"));
670-
this->asleep = false;
671-
pinMode(this->sleepPin, OUTPUT);
672676
digitalWrite(this->sleepPin, HIGH); // HIGH = awake
673677
lastPowerOnTime = millis();
674678
}
@@ -682,8 +686,6 @@ void IridiumSBD::power(bool on)
682686
delay(elapsed);
683687

684688
dbg(F("Powering off RockBLOCK...!\r\n"));
685-
this->asleep = true;
686-
pinMode(this->sleepPin, OUTPUT);
687689
digitalWrite(this->sleepPin, LOW); // LOW = asleep
688690
}
689691
}

0 commit comments

Comments
 (0)