- else {
- for(i=0; i<n; i++) { // Look for identical block in known blocks
- if(memcmp(tmpBlocks[i], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) { // Block is not full of 00
- for(j=0; j<max_blocks; j++) {
- if(Blocks[j][ALLOC] == 1 && !memcmp(tmpBlocks[i], Blocks[j], 16)) {
- // Found an identical block
- for(ind=i-1,ind2=j-1; ind >= 0; ind--,ind2--) {
- if(ind2 < 0)
- ind2 = max_blocks;
- if(!Blocks[ind2][ALLOC]) { // Block ind2 not already found
- // Dbprintf("Tmp %d -> Block %d", ind, ind2);
- memcpy(Blocks[ind2], tmpBlocks[ind], 16);
- Blocks[ind2][ALLOC] = 1;
- num_blocks++;
- if(num_blocks == max_blocks) goto end;
- }
- }
- for(ind=i+1,ind2=j+1; ind < n; ind++,ind2++) {
- if(ind2 > max_blocks)
- ind2 = 0;
- if(!Blocks[ind2][ALLOC]) { // Block ind2 not already found
- // Dbprintf("Tmp %d -> Block %d", ind, ind2);
- memcpy(Blocks[ind2], tmpBlocks[ind], 16);
- Blocks[ind2][ALLOC] = 1;
- num_blocks++;
- if(num_blocks == max_blocks) goto end;
- }
+
+ Dbprintf("(dbg) got %d blocks (%d/%d found) (%d tries, %d errors)", n, found_blocks, (max_blocks == 0 ? found_blocks : max_blocks), tries, errors);
+ for (i = 0; i < n; ++i)
+ {
+ print_result("got consecutive blocks", tmp_blocks[i], 16);
+ }
+
+ i = 0;
+ if(!found_0_1) {
+ while (i < n - 1) {
+ if (IsBlock0PCF7931(tmp_blocks[i]) && IsBlock1PCF7931(tmp_blocks[i+1])) {
+ found_0_1 = 1;
+ memcpy(memory_blocks[0], tmp_blocks[i], 16);
+ memcpy(memory_blocks[1], tmp_blocks[i+1], 16);
+ memory_blocks[0][ALLOC] = memory_blocks[1][ALLOC] = 1;
+ // block 1 tells how many blocks are going to be sent
+ max_blocks = MAX((memory_blocks[1][14] & 0x7f), memory_blocks[1][15]) + 1;
+ found_blocks = 2;
+
+ Dbprintf("Found blocks 0 and 1. PCF is transmitting %d blocks.", max_blocks);
+
+ // handle the following blocks
+ for (j = i + 2; j < n; ++j) {
+ memcpy(memory_blocks[found_blocks], tmp_blocks[j], 16);
+ memory_blocks[found_blocks][ALLOC] = 1;
+ ++found_blocks;
+ }
+ break;
+ }
+ ++i;
+ }
+ } else {
+ // Trying to re-order blocks
+ // Look for identical block in memory blocks
+ while (i < n-1) {
+ // skip all zeroes blocks
+ if (memcmp(tmp_blocks[i], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) {
+ for (j = 1; j < max_blocks - 1; ++j) {
+ if (!memcmp(tmp_blocks[i], memory_blocks[j], 16) && !memory_blocks[j+1][ALLOC]) {
+ memcpy(memory_blocks[j+1], tmp_blocks[i+1], 16);
+ memory_blocks[j+1][ALLOC] = 1;
+ if (++found_blocks >= max_blocks) goto end;
+ }
+ }
+ }
+ if (memcmp(tmp_blocks[i+1], "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 16)) {
+ for (j = 0; j < max_blocks; ++j) {
+ if (!memcmp(tmp_blocks[i+1], memory_blocks[j], 16) && !memory_blocks[(j == 0 ? max_blocks : j) -1][ALLOC]) {
+ if (j == 0) {
+ memcpy(memory_blocks[max_blocks - 1], tmp_blocks[i], 16);
+ memory_blocks[max_blocks - 1][ALLOC] = 1;
+ } else {
+ memcpy(memory_blocks[j-1], tmp_blocks[i], 16);
+ memory_blocks[j-1][ALLOC] = 1;