From f42c56f19c84153baa93380c4dfb73420e784dd4 Mon Sep 17 00:00:00 2001 From: Aarno Aukia Date: Thu, 7 Nov 2013 11:14:15 +0100 Subject: [PATCH] fix 2nd address scanning bug, add some comments code merge from arska/libmbus without debug --- mbus/mbus-protocol-aux.c | 57 ++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/mbus/mbus-protocol-aux.c b/mbus/mbus-protocol-aux.c index 32e775f..d78e187 100755 --- a/mbus/mbus-protocol-aux.c +++ b/mbus/mbus-protocol-aux.c @@ -2414,6 +2414,7 @@ mbus_scan_2nd_address_range(mbus_handle * handle, int pos, char *addr_mask) if (mask[pos] == 'f' || mask[pos] == 'F') { + // mask[pos] is a wildcard -> enumerate all 0..9 at this position i_start = 0; i_end = 9; } @@ -2421,44 +2422,50 @@ mbus_scan_2nd_address_range(mbus_handle * handle, int pos, char *addr_mask) { if (pos < 15) { + // mask[pos] is not a wildcard -> don't iterate, recursively check pos+1 mbus_scan_2nd_address_range(handle, pos+1, mask); } else { + // .. except if we're at the last pos (==15) and this isn't a wildcard we still need to send the probe i_start = (int)(mask[pos] - '0'); i_end = (int)(mask[pos] - '0'); } } - for (i = 0; i <= 9; i++) + // skip the scanning if we're returning from the (pos < 15) case above + if (mask[pos] == 'f' || mask[pos] == 'F' || pos == 15) { - mask[pos] = '0'+i; - - if (handle->scan_progress) - handle->scan_progress(handle,mask); - - probe_ret = mbus_probe_secondary_address(handle, mask, matching_mask); - - if (probe_ret == MBUS_PROBE_SINGLE) + for (i = i_start; i <= i_end; i++) { - if (!handle->found_event) + mask[pos] = '0'+i; + + if (handle->scan_progress) + handle->scan_progress(handle,mask); + + probe_ret = mbus_probe_secondary_address(handle, mask, matching_mask); + + if (probe_ret == MBUS_PROBE_SINGLE) { - printf("Found a device on secondary address %s [using address mask %s]\n", matching_mask, mask); + if (!handle->found_event) + { + printf("Found a device on secondary address %s [using address mask %s]\n", matching_mask, mask); + } + } + else if (probe_ret == MBUS_PROBE_COLLISION) + { + // collision, more than one device matching, restrict the search mask further + mbus_scan_2nd_address_range(handle, pos+1, mask); + } + else if (probe_ret == MBUS_PROBE_NOTHING) + { + // nothing... move on to next address mask + } + else // MBUS_PROBE_ERROR + { + MBUS_ERROR("%s: Failed to probe secondary address [%s].\n", __PRETTY_FUNCTION__, mask); + return -1; } - } - else if (probe_ret == MBUS_PROBE_COLLISION) - { - // collision, more than one device matching, restrict the search mask further - mbus_scan_2nd_address_range(handle, pos+1, mask); - } - else if (probe_ret == MBUS_PROBE_NOTHING) - { - // nothing... move on to next address mask - } - else // MBUS_PROBE_ERROR - { - MBUS_ERROR("%s: Failed to probe secondary address [%s].\n", __PRETTY_FUNCTION__, mask); - return -1; } }