Example #1
0
/* This function is invoked whenever new data is received. Such new data 
  includes both the actual message sent from as well as all control messages,
  such as "CSQ:"

  Currently, this is the function in which you may read out the actual (SBDRB) 
  message and perform appropriate tasks.
*/
void rxInterruptLoop(const void *_serial, const void *_sbd) {
  Serial *serial = (Serial *)_serial;
  IridiumSBD2 *sbd = (IridiumSBD2 *)_sbd;

  if (millis_time() - sbd->getStartTime() > 1000UL * sbd->getDuration()) {
    // Last message received timeout, and need to reset state
    // drop message
    return;
  }

  bool terminated = false; /* This is used to check if the current set of recvd
                              bytes is the end of a full control message, which
                              will trigger subsequent control actions.
                            */
  while (serial->readable()) { // serial == nss (main.cpp)
    char c = (char) serial->getc();
    sbd->console(c);
    char *prompt = sbd->getPrompt();
    char *terminator = sbd->getTerminator();

    if (prompt) {
      int matchPromptPos = sbd->getMatchPromptPos();
      switch(sbd->getPromptState()) {
        case 0: // matching prompt
          if (c == prompt[matchPromptPos]) {
            matchPromptPos++;
            sbd->setMatchPromptPos(matchPromptPos);
            if (prompt[matchPromptPos] == '\0') {
               sbd->setPromptState(1);
            }
          } else {
            matchPromptPos = c == prompt[0] ? 1 : 0; /* try to match prompt, 
                                                        if current char matches, 
                                                        then move on to next char 
                                                        to match
                                                      */
            sbd->setMatchPromptPos(matchPromptPos);
         }
         break;

        case 1: // gathering reponse from end of prompt to first
          int responseSize = sbd->getResponseSize();
          if (responseSize > 0) {
            if (c == '\r' || responseSize < 2) { 
               sbd->setPromptState(2);
            } else {
               (sbd->getRxBuffer())->insert(c); 
               // rxBuffer (only put in actual response,
               // no prompt/terminator in buffer
               responseSize--;
               sbd->setResponseSize(responseSize);
            }
          }
          break;
      }
    }
    
    if (sbd->getCompletionNum() == processSBDRBResponsNum) {
      (sbd->getRxBuffer())->insert(c);
      messageBuffer.insert(c);
    }

    // If there is no prompt, then just keep trying to match the terminator 
    // until either all terminator characters are
    // matched (return true), or no more serial to read (return false)
    int matchTerminatorPos = sbd->getMatchTerminatorPos();
    if (terminator) {
      if (c == terminator[matchTerminatorPos]) {
        matchTerminatorPos++;
        sbd->setMatchTerminatorPos(matchTerminatorPos);
        if (terminator[matchTerminatorPos] == '\0') {
          terminated = true;
        }
      } else {
        matchTerminatorPos = c == terminator[0] ? 1 : 0;
        sbd->setMatchTerminatorPos(matchTerminatorPos);
      }
    }
  } // while (serial.available())

  if (sbd->checkCompletion(terminated) && sbd->getCompletionNum() == processSBDRBResponsNum) {
    char c;
    int nBytes = sbd->getResponseSize() - 4; /* -4 because we are currently 
                                                seeing the four bytes missing
                                                on most cases, though not always
                                                consistent.
                                              */
    /* NOTE: THIS IS WHERE YOU MAY DECIDE WHAT ACTIONS/TASKS TO ENQUE FOR 
             RECEIVING DIFFERENT TYPES OF MESSAGES FROM SBDRB */
    if (nBytes > 0) {
        printf("I RECEIVED A MESSAGE :) \n");
    }
    for (int i = 0; i < nBytes; i++) {
      if (messageBuffer.pop(c)){
        diagSerial.printf("%c",c);
      }
    }
    diagSerial.printf("\r\n");
    messageBuffer.clear();
  }

  return;
}
Example #2
0
unsigned char get_command(Serial cmdLink) {
    while (! cmdLink.readable());  
    return cmdLink.getByte();
}