-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Bose manufacturer data implementation (issue #18)
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import hashlib | ||
|
||
|
||
def unextract_manufacturer_id(manufacturer_id: int, manufacturer_data: bytes) -> bytes: | ||
manufacturer_id_bytes = bytes([manufacturer_id & 0xFF, (manufacturer_id >> 8) & 0xFF]) | ||
return manufacturer_id_bytes + manufacturer_data | ||
|
||
|
||
def mac_hash(mac: bytes, data: bytes) -> bytes: | ||
new_data = data[:4] + mac + data[9:] | ||
hash_data = new_data.hex().upper().encode("utf-8") | ||
return hashlib.sha256(hash_data).digest() | ||
|
||
|
||
def is_bose_device(mac: bytes, data: bytes) -> bool: | ||
partial_mac_hash = data[4:9] | ||
return partial_mac_hash in mac_hash(mac, data) # possibly ok implementation? | ||
# return partial_mac_hash.hex() in mac_hash(mac, data).hex() # official implementation via .hex() | ||
|
||
|
||
# from bluez dbus | ||
mac = bytes([0x4C, 0x87, 0x5D, 0x53, 0xF2, 0xCF]) | ||
manufacturer_id = 2305 | ||
manufacturer_data = bytes([113, 18, 177, 27, 65, 149, 232, 52, 213, 174, 45, 195, 82]) | ||
|
||
# reconstruct full manufacturer data | ||
# bose doesn't use manufacturer id correctly | ||
# data = bytes([1, 9, 113, 18, 177, 27, 65, 149, 232, 52, 213, 174, 45, 195, 82]) | ||
data = unextract_manufacturer_id(manufacturer_id, manufacturer_data) | ||
|
||
print(is_bose_device(mac, data)) |