@@ -2134,6 +2134,8 @@ - (void)decodePacketsInBufferAsync;
21342134 rfidPacketBufferInHexString=[[NSString alloc ] init ];
21352135
21362136 int datalen; // data length given on the RFID packet
2137+ int sequenceNumber=0 ;
2138+
21372139 while (self.bleDevice ) // packet decoding will continue as long as there is a connected device instance
21382140 {
21392141 @autoreleasepool {
@@ -2145,6 +2147,30 @@ - (void)decodePacketsInBufferAsync;
21452147 if ([packet isKindOfClass: [NSNull class ]]) {
21462148 continue ;
21472149 }
2150+
2151+ if (packet.direction ==Uplink && packet.deviceId ==RFID) {
2152+
2153+ NSLog (@" [decodePacketsInBufferAsync] Current sequence number: %d " , sequenceNumber);
2154+
2155+ // validate checksum of packet
2156+ if (!packet.isCRCPassed ) {
2157+ NSLog (@" [decodePacketsInBufferAsync] Checksum verification failed. Discarding data in buffer" );
2158+ [rfidPacketBuffer setLength: 0 ];
2159+ continue ;
2160+ }
2161+
2162+ if ([rfidPacketBuffer length ] == 0 )
2163+ sequenceNumber=packet.Reserve ;
2164+ else {
2165+ if (packet.Reserve != (sequenceNumber+1 )) {
2166+ NSLog (@" [decodePacketsInBufferAsync] Packet out-of-order based on sequence number. Discarding data in buffer" );
2167+ [rfidPacketBuffer setLength: 0 ];
2168+ continue ;
2169+ }
2170+ else
2171+ sequenceNumber++;
2172+ }
2173+ }
21482174 }
21492175 else
21502176 continue ;
@@ -2353,7 +2379,7 @@ - (void)decodePacketsInBufferAsync;
23532379 // start decode taq-response message
23542380 // length of data field (in bytes) = ((pkt_len – 3) * 4) – ((flags >> 6) & 3)
23552381 datalen=(((((Byte *)[rfidPacketBuffer bytes ])[ptr+4 ] + (((((Byte *)[rfidPacketBuffer bytes ])[ptr+5 ] << 8 ) & 0xFF00 )))-3 ) * 4 ) - ((((Byte *)[rfidPacketBuffer bytes ])[ptr+1 ] >> 6 ) & 3 );
2356- tag.DATALength =datalen;
2382+ tag.DATALength =datalen / 2 ;
23572383
23582384 /*
23592385 if (tag.DATA1Length > 0 && (((tag.DATA1Length + tag.DATA2Length) * 2) == datalen))
@@ -2384,7 +2410,9 @@ - (void)decodePacketsInBufferAsync;
23842410
23852411 tag.ACKTimeout =(((Byte *)[rfidPacketBuffer bytes ])[ptr+1 ] & 0x04 ) >> 2 ;
23862412
2387- if ((((Byte *)[rfidPacketBuffer bytes ])[ptr+1 ] & 0x02 ) >> 1 && tag.BackScatterError == 0xFF && !tag.CRCError && !tag.ACKTimeout ) {
2413+ // if access error occurred and nothg of the following: tag backscatter error, ack time out, crc error indicated a fault,
2414+ // read error code form the data field
2415+ if ((((Byte *)[rfidPacketBuffer bytes ])[ptr+1 ] & 0x01 ) && tag.BackScatterError == 0xFF && !tag.CRCError && !tag.ACKTimeout ) {
23882416 tag.AccessError =((Byte *)[rfidPacketBuffer bytes ])[ptr+20 ];
23892417 }
23902418 else
@@ -2587,7 +2615,8 @@ - (void)decodePacketsInBufferAsync;
25872615 tag.PC =((((Byte *)[rfidPacketBuffer bytes ])[ptr] << 8 ) & 0xFF00 )+ ((Byte *)[rfidPacketBuffer bytes ])[ptr+1 ];
25882616
25892617 // for the case where we reaches to the end of the BLE packet but not the RFID response packet, where there will be partial packet to be returned from the next packet. The partial tag data will be combined with the next packet being returned.
2590- if ((ptr + (2 + ((tag.PC >> 11 ) * 2 ) + 1 )) > ([rfidPacketBuffer length ])) {
2618+ // 8100 (two bytes) + 8 bytes RFID packet header + payload length being calcuated ont he header
2619+ if ((10 + datalen) > [rfidPacketBuffer length ]) {
25912620 // stop decoding and wait for the partial tag data to be appended in the next packet arrival
25922621 NSLog (@" [decodePacketsInBufferAsync] partial tag data being returned. Wait for next rfid response packet for complete tag data." );
25932622 break ;
0 commit comments