Skip to content

Commit

Permalink
Checking a whitelist in a BLE message parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Magalex2x14 committed Apr 27, 2020
1 parent e63d001 commit f2d8a2a
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions custom_components/mitemp_bt/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def decrypt_payload(encrypted_payload, key, nonce):
return plaindata


def parse_raw_message(data, aeskeyslist, report_unknown=False):
def parse_raw_message(data, aeskeyslist, whitelist, report_unknown=False):
"""Parse the raw data."""
if data is None:
return None
Expand All @@ -231,6 +231,10 @@ def parse_raw_message(data, aeskeyslist, report_unknown=False):
source_mac_reversed = data[adv_index - 7:adv_index - 1]
if xiaomi_mac_reversed != source_mac_reversed:
return None
# check for MAC presence in whitelist, if needed
if whitelist:
if xiaomi_mac_reversed not in whitelist:
return None
# extract RSSI byte
(rssi,) = struct.unpack("<b", data[msg_length - 1:msg_length])
#strange positive RSSI workaround
Expand Down Expand Up @@ -434,7 +438,7 @@ def lpacket(mac, packet=None):
whitelist.append(mac)
for i, mac in enumerate(whitelist):
whitelist[i] = bytes.fromhex(reverse_mac(mac.replace(":", "")).lower())
_LOGGER.debug("%s whitelist item(s) loaded.", len(aeskeys))
_LOGGER.debug("%s whitelist item(s) loaded.", len(whitelist))
lpacket.cntr = {}
sleep(1)

Expand Down Expand Up @@ -491,7 +495,7 @@ def calc_update_state(
error = err
return success, error

def discover_ble_devices(config, aeskeyslist):
def discover_ble_devices(config, aeskeyslist, whitelist):
"""Discover Bluetooth LE devices."""
nonlocal firstrun
if firstrun:
Expand Down Expand Up @@ -520,7 +524,7 @@ def discover_ble_devices(config, aeskeyslist):
scanner.start(config) # minimum delay between HCIdumps
report_unknown = config[CONF_REPORT_UNKNOWN]
for msg in hcidump_raw:
data = parse_raw_message(msg, aeskeyslist, report_unknown)
data = parse_raw_message(msg, aeskeyslist, whitelist, report_unknown)
if data and "mac" in data:
# ignore duplicated message
packet = data["packet"]
Expand Down Expand Up @@ -704,7 +708,7 @@ def update_ble(now):
period = config[CONF_PERIOD]
_LOGGER.debug("update_ble called")
try:
discover_ble_devices(config, aeskeys)
discover_ble_devices(config, aeskeys, whitelist)
except RuntimeError as error:
_LOGGER.error("Error during Bluetooth LE scan: %s", error)
track_point_in_utc_time(
Expand Down

0 comments on commit f2d8a2a

Please sign in to comment.