adafruit / Adafruit-Fingerprint-Sensor-Library

Arduino library for interfacing to the fingerprint sensor in the Adafruit shop
http://www.adafruit.com/products/751
Other
417 stars 340 forks source link

Problem with getStructuredPacket when receiving large payloads (> 62 bytes) #137

Open filipecustodio opened 1 month ago

filipecustodio commented 1 month ago

When using the getStructuredPacket() utility function to receive a payload over 62 bytes, a bug will be triggered where the result is always FINGERPRINT_BADPACKET.

The end condition of the function is (lines 606 - 611):

  if ((idx - 8) == packet->length) {

ifdef FINGERPRINT_DEBUG

    Serial.println(" OK ");

endif

    return FINGERPRINT_OK;
  }

Which should terminate the read when "length" bytes have been read. However, "length" is not the length of the payload, as it contains 2 extra bytes for the checksum. The correct end condition should be: (idx-8) == packet->length - 2, and before exiting it should read the two additional bytes and validate the checksum. As it is now, the error condition in line 615 is always triggered if the data being received is >= 62 bytes.

A possible solution would be:

if ((idx-8) == (packet->length - 2) { // Read the checksum int8_t hiChecksum = mySerial->read(); int8_t loCheckxum = mySerial->read(); // Validate the checksum of the array packet->data[] if ( (((int16_t) hiChecksum) << 8) | (int16_t loChecksum) != calcChecksum ) return FINGERPRINT_BADPACKET; return FINGERPRINT_OK; }