int16_t ESP8266Class::ping(char * server) { char params[strlen(server) + 3]; sprintf(params, "\"%s\"", server); // Send AT+Ping=<server> sendCommand(ESP8266_PING, ESP8266_CMD_SETUP, params); // Example responses: // * Good response: +12\r\n\r\nOK\r\n // * Timeout response: +timeout\r\n\r\nERROR\r\n // * Error response (unreachable): ERROR\r\n\r\n int16_t rsp = readForResponses(RESPONSE_OK, RESPONSE_ERROR, COMMAND_PING_TIMEOUT); if (rsp > 0) { char * p = searchBuffer("+"); p += 1; // Move p forward 1 space char * q = strchr(p, '\r'); // Find the first \r if (q == NULL) return ESP8266_RSP_UNKNOWN; char tempRsp[10]; strncpy(tempRsp, p, q - p); return atoi(tempRsp); } else { if (searchBuffer("timeout") != NULL) return 0; } return rsp; }
int MG2639_Cell::readWaitForResponses(const char * goodRsp, const char * failRsp, unsigned int timeout) { unsigned long timeIn = millis(); // Timestamp coming into function unsigned int received = 0; // received keeps track of number of chars read clearBuffer(); // Clear the class receive buffer (rxBuffer) while (timeIn + timeout > millis()) // While we haven't timed out { if (dataAvailable()) // If data is available on UART RX { // Increment received count & read byte to buffer received += readByteToBuffer(); if (searchBuffer(goodRsp)) return received; // If we've received [goodRsp], return received if (searchBuffer(failRsp)) // If we've received [failRsp] return ERROR_FAIL_RESPONSE; // return FAIL response error code } } if (received > 0) // If we received any characters return ERROR_UNKNOWN_RESPONSE; // Return unkown response error code else // If we haven't received any characters return ERROR_TIMEOUT; // Return the timeout error code }
int16_t ESP8266Class::readForResponses(const char * pass, const char * fail, unsigned int timeout) { unsigned long timeIn = millis(); // Timestamp coming into function unsigned int received = 0; // received keeps track of number of chars read clearBuffer(); // Clear the class receive buffer (esp8266RxBuffer) while (timeIn + timeout > millis()) // While we haven't timed out { if (_serial->available()) // If data is available on UART RX { received += readByteToBuffer(); if (searchBuffer(pass)) // Search the buffer for goodRsp return received; // Return how number of chars read if (searchBuffer(fail)) return ESP8266_RSP_FAIL; } } if (received > 0) // If we received any characters return ESP8266_RSP_UNKNOWN; // Return unkown response error code else // If we haven't received any characters return ESP8266_RSP_TIMEOUT; // Return the timeout error code }
int16_t ESP8266Class::tcpConnect(uint8_t linkID, const char * destination, uint16_t port, uint16_t keepAlive) { print("AT"); print(ESP8266_TCP_CONNECT); print('='); print(linkID); print(','); print("\"TCP\","); print("\""); print(destination); print("\","); print(port); if (keepAlive > 0) { print(','); // keepAlive is in units of 500 milliseconds. // Max is 7200 * 500 = 3600000 ms = 60 minutes. print(keepAlive / 500); } print("\r\n"); // Example good: CONNECT\r\n\r\nOK\r\n // Example bad: DNS Fail\r\n\r\nERROR\r\n // Example meh: ALREADY CONNECTED\r\n\r\nERROR\r\n int16_t rsp = readForResponses(RESPONSE_OK, RESPONSE_ERROR, CLIENT_CONNECT_TIMEOUT); if (rsp < 0) { // We may see "ERROR", but be "ALREADY CONNECTED". // Search for "ALREADY", and return success if we see it. char * p = searchBuffer("ALREADY"); if (p != NULL) return 2; // Otherwise the connection failed. Return the error code: return rsp; } // Return 1 on successful (new) connection return 1; }
int16_t ESP8266Class::updateStatus() { sendCommand(ESP8266_TCP_STATUS); // Send AT+CIPSTATUS\r\n // Example response: (connected as client) // STATUS:3\r\n // +CIPSTATUS:0,"TCP","93.184.216.34",80,0\r\n\r\nOK\r\n // - or - (clients connected to ESP8266 server) // STATUS:3\r\n // +CIPSTATUS:0,"TCP","192.168.0.100",54723,1\r\n // +CIPSTATUS:1,"TCP","192.168.0.101",54724,1\r\n\r\nOK\r\n int16_t rsp = readForResponse(RESPONSE_OK, COMMAND_RESPONSE_TIMEOUT); if (rsp > 0) { char * p = searchBuffer("STATUS:"); if (p == NULL) return ESP8266_RSP_UNKNOWN; p += 7;//strlen("STATUS:"); _status.stat = (esp8266_connect_status)(*p - 48); for (int i=0; i<ESP8266_MAX_SOCK_NUM; i++) { p = strstr(p, "+CIPSTATUS:"); if (p == NULL) { // Didn't find any IPSTATUS'. Set linkID to 255. for (int j=i; j<ESP8266_MAX_SOCK_NUM; j++) _status.ipstatus[j].linkID = 255; return rsp; } else { p += 11;//strlen("+CIPSTATUS:"); // Find linkID: uint8_t linkId = *p - 48; if (linkId >= ESP8266_MAX_SOCK_NUM) return rsp; _status.ipstatus[linkId].linkID = linkId; // Find type (p pointing at linkID): p += 3; // Move p to either "T" or "U" if (*p == 'T') _status.ipstatus[linkId].type = ESP8266_TCP; else if (*p == 'U') _status.ipstatus[linkId].type = ESP8266_UDP; else _status.ipstatus[linkId].type = ESP8266_TYPE_UNDEFINED; // Find remoteIP (p pointing at first letter or type): p += 6; // Move p to first digit of first octet. for (uint8_t j = 0; j < 4; j++) { char tempOctet[4]; memset(tempOctet, 0, 4); // Clear tempOctet size_t octetLength = strspn(p, "0123456789"); // Find length of numerical string: strncpy(tempOctet, p, octetLength); // Copy string to temp char array: _status.ipstatus[linkId].remoteIP[j] = atoi(tempOctet); // Move the temp char into IP Address octet p += (octetLength + 1); // Increment p to next octet } // Find port (p pointing at ',' between IP and port: p += 1; // Move p to first digit of port char tempPort[6]; memset(tempPort, 0, 6); size_t portLen = strspn(p, "0123456789"); // Find length of numerical string: strncpy(tempPort, p, portLen); _status.ipstatus[linkId].port = atoi(tempPort); p += portLen + 1; // Find tetype (p pointing at tetype) if (*p == '0') _status.ipstatus[linkId].tetype = ESP8266_CLIENT; else if (*p == '1') _status.ipstatus[linkId].tetype = ESP8266_SERVER; } } } return rsp; }
static void performSearch( char *fn ) { int io; int retries; // number of times we read the pipe and got nothing int red; int sav; size_t size; unsigned frag; char *locn; unsigned saveRecs; char **currs; char **nexts; char *probe; struct stat buf; MatchCount = 0; if( strcmp( fn, "@@" ) == 0 ) { fn = "stdin"; io = STDIN_FILENO; setmode( io, O_BINARY ); } else { if( FileMode != 0 ) { if( stat( fn, &buf ) != -1 ) { if( (buf.st_mode & FileMode) != 0 ) { return; } } } io = open( fn, O_RDONLY | O_BINARY ); if( io == -1 ) { Warning( "Unable to open", fn ); return; } } FName = fn; retries = 0; // number of times we read the pipe and got nothing if( PrtAll ) { printFileName(); } PrtFn = TRUE; red = readFile( io, Buff, BSize ); if( red == -1 ) { // Warning( "Error reading file", fn ); // removed for 0 length files if( io != STDIN_FILENO ) { close( io ); } return; } // look for two '\n's with no preceeding '\r' probe = (char *)memchr( Buff, '\n', red ); // look for a newline if( ( probe == Buff || ( probe != NULL && probe[-1] != '\r' ) ) && red > 1 ) { probe = (char *)memchr( probe+1, '\n', red - ( probe+1 - Buff ) ); if( probe != NULL && probe[-1] != '\n' ) { OutMode( O_TEXT ); // we'll assume file has no \r's in it } else { OutMode( O_BINARY ); // assume file has \r\n pairs } } else { OutMode( O_BINARY ); } Recs = 1; locn = Buff; size = BSize; for(;;) { retries = 0; // we actually got something sav = red; // preserve old value Buff[ sav + 0 ] = '\0'; Buff[ sav + 1 ] = '\n'; // put this here as a sentinel // (required by searchBuffer) while( red != 0 && Buff[red-1] != '\n' ) { // break on record boundary --red; } if( red == 0 ) red = sav; // if we did not find NL restore red currs = SrchStrings; saveRecs = Recs; // reset line number for each search string while( *currs ) { Recs = saveRecs; strcpy( Buff + sav + 2, *currs ); searchBuffer( *currs, red ); // - search the buffer if( ExitStatus && QuitFirst ) { nexts = currs; for( ;; ) { nexts[ 0 ] = nexts[ 1 ]; if( nexts[ 0 ] == NULL ) break; ++nexts; } if( *SrchStrings ) { ExitStatus = 0; } } else { ++currs; } } if( ExitStatus ) break; frag = (unsigned)(sav - red); if( frag != 0) { memcpy( Buff, &Buff[red], frag ); } locn = &Buff[frag]; size = BSize - frag; red = readFile( io, locn, size ); // read a big hunk of the file if( red == -1 ) { // if nothing read if( frag == 0 ) break; // - and no fragment quit red = 0; // - did not read anything } // endif red += frag; // include size of piece moved } if( io != STDIN_FILENO ) { close( io ); } if( PrtCount && (MatchCount != 0) ) { printf( "Lines: %d\r\n", MatchCount ); } }
CELL * p_find(CELL * params) { char * key; char * str; ssize_t found; CELL * next; CELL * keyCell; CELL * funcCell; size_t size; long options = -1; size_t offset = 0; UINT * resultIdxSave; keyCell = evaluateExpression(params); params = getEvalDefault(params->next, &next); if(keyCell->type == CELL_STRING && next->type == CELL_STRING) { key = (char *)keyCell->contents; str = (char *)next->contents; size = next->aux - 1; if(params != nilCell) { if(params->next != nilCell) getInteger(params->next, (UINT*)&offset); if(offset > size) offset = size; params = evaluateExpression(params); if(!isNil(params)) getIntegerExt(params, (UINT *)&options, FALSE); } if(options == -1) found = searchBuffer(str + offset, size - offset, key, keyCell->aux - 1, TRUE); else found = searchBufferRegex(str, (int)offset, key, (int)size, options, NULL) - offset; if(found < 0) return(nilCell); } else { /* list mode with optional functor */ if(!isList(next->type)) return(nilCell); next = (CELL *)next->contents; found = 0; if(params != nilCell) funcCell = evaluateExpression(params); else funcCell = NULL; /* do regex when first arg is string and option# is present */ if(funcCell && isNumber(funcCell->type) && keyCell->type == CELL_STRING) { getIntegerExt(funcCell, (UINT*)&options, FALSE); key = (char *)keyCell->contents; while(next != nilCell) { if(next->type == CELL_STRING) { if(searchBufferRegex((char *)next->contents, 0, key, next->aux - 1 , options, NULL) != -1) break; } found++; next = next->next; } if(next == nilCell) return(nilCell); else return(stuffInteger(found)); } resultIdxSave = resultStackIdx; while(next != nilCell) { if(compareFunc(keyCell, next, funcCell) == 0) { if(funcCell) { deleteList((CELL*)sysSymbol[0]->contents); sysSymbol[0]->contents = (UINT)copyCell(next); } break; } found++; next = next->next; cleanupResults(resultIdxSave); } if(next == nilCell) return(nilCell); } return(stuffInteger(found + offset)); }