void GSwifi::escape (const char *cmd, uint8_t timeout_second) { GSLOG_PRINT(P("e> ")); resetResponse(GSCOMMANDMODE_NONE); serial_->write( ESCAPE ); serial_->print(cmd); // without ln GSLOG_PRINTLN(cmd); }
void GSwifi::clear () { joined_ = false; listening_ = false; limited_ap_ = false; resetResponse(GSCOMMANDMODE_NONE); gs_mode_ = GSMODE_COMMAND; ring_clear(_buf_cmd); memset(ipaddr_, 0, sizeof(ipaddr_)); for (uint8_t i=0; i<16; i++) { TIMER_STOP( timers_[i] ); } }
void GSwifi::writeCert() { // Binary format, store in memory command( "AT+TCERTADD=cacert,0,753,1", GSCOMMANDMODE_NORMAL ); resetResponse(GSCOMMANDMODE_NORMAL); // ESCAPE 'W' is written in pgm too (thus +2) for (uint16_t i=0; i<753 + 2; i++) { uint8_t read = pgm_read_byte_near(der + i); serial_->write( read ); Serial.print( read, HEX ); } setBusy(true); waitResponse(GS_TIMEOUT); }
void GSwifi::command (const char *cmd, GSCOMMANDMODE res, uint8_t timeout_second) { // GSLOG_PRINT(P("F: 0x")); GSLOG_PRINTLN2( freeMemory(), HEX ); GSLOG_PRINT(P("c> ")); resetResponse(res); serial_->println(cmd); GSLOG_PRINTLN(cmd); if (timeout_second == GS_TIMEOUT_NOWAIT) { return; } setBusy(true); waitResponse(timeout_second); }
void Ant::readPacket() { // reset previous response if (getResponse().isAvailable() || getResponse().isError()) { // discard previous packet and start over resetResponse(); } while (available()) { b = read(); // checksum includes all bytes including the sync _checksumTotal ^= b; switch(_pos) { case 0: if (b == ANT_START_BYTE) { _pos++; } break; case 1: // length msb getResponse().setLength(b); _pos++; break; case 2: getResponse().setMsgId(b); _pos++; break; default: // starts at fourth byte if (_pos > ANT_MAX_MSG_DATA_SIZE) { // exceed max size. should never occur getResponse().setErrorCode(PACKET_EXCEEDS_BYTE_ARRAY_LENGTH); return; } // check if we're at the end of the packet if (_pos == (getResponse().getLength() + 3)) { // verify checksum // if the last byte is the checksum // then XOR it with itself should be 0 if (_checksumTotal == 0) { getResponse().setChecksum(b); getResponse().setAvailable(true); getResponse().setErrorCode(NO_ERROR); } else { // checksum failed getResponse().setErrorCode(CHECKSUM_FAILURE); } // reset state vars _pos = 0; return; } else { // add to packet array, starting with the fourth byte of the msgId getResponse().getFrameData()[_pos - ANT_MSG_FRONT_OVERHEAD] = b; _pos++; } } } }
void readPacket() { // reset previous response if ((_response_Available == true) || (_response_ERROR > 0)) { // discard previous packet and start over resetResponse(); } while (uart_available()) { b = uart_getc(); if (_pos > 0 && b == START_BYTE && ATAP == 2) { // new packet start before previous packeted completed -- discard previous packet and start over _response_ERROR = UNEXPECTED_START_BYTE; uart_putc(_response_ERROR); return; } if (_pos > 0 && b == ESCAPE) { if (uart_available()) { b = uart_getc(); b = 0x20 ^ b; } else { // escape byte. next byte will be _escape = true; continue; } } if (_escape == true) { b = 0x20 ^ b; _escape = false; } // checksum includes all bytes starting with api id if (_pos >= API_ID_INDEX) { _checksumTotal+= b; } switch(_pos) { case 0: if (b == START_BYTE) { _pos++; } break; case 1: // length msb _response_MsbLength = b; _pos++; break; case 2: // length lsb _response_LsbLength = b; _pos++; break; case 3: _response_ApiId = b; _pos++; break; default: // starts at fifth byte if (_pos > MAX_FRAME_DATA_SIZE) { // exceed max size. should never occur _response_ERROR = PACKET_EXCEEDS_BYTE_ARRAY_LENGTH; uart_putc(_response_ERROR); return; } // check if we're at the end of the packet // packet length does not include start, length, or checksum bytes, so add 3 if (_pos == (getPacketLength() + 3)) { // verify checksum if ((_checksumTotal & 0xff) == 0xff) { _response_Checksum = b; _response_Available = true; //no check //uart_puts_P("OK\n");/////////////////////////// //DDRA = 0x02;//////////////////////////////////// //PORTA = 0x02;////////////////////////// _response_ERROR = NO_ERROR; } else { // checksum failed _response_ERROR = CHECKSUM_FAILURE; uart_putc(_response_ERROR); } // minus 4 because we start after start,msb,lsb,api and up to but not including checksum // e.g. if frame was one byte, _pos=4 would be the byte, pos=5 is the checksum, where end stop reading _response_FrameLength = (_pos - 4); // reset state vars _pos = 0; _checksumTotal = 0; return; } else { // add to packet array, starting with the fourth byte of the apiFrame _response_FrameData[_pos - 4] = b; _pos++; } } } }