mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-12-23 05:55:49 +00:00
Add HCI Cast Lib & PyYaml
HCI Cast yet to be tested / debugged PyYaml to ingest secrets file
This commit is contained in:
parent
d758ed6f97
commit
69d2e7e2a1
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -153,3 +153,6 @@ cython_debug/
|
||||||
|
|
||||||
# VSCode
|
# VSCode
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
|
# Secrets file
|
||||||
|
.secrets.yml
|
||||||
|
|
73
hci.py
Normal file
73
hci.py
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
import base64
|
||||||
|
import subprocess
|
||||||
|
from time import sleep
|
||||||
|
from struct import pack
|
||||||
|
|
||||||
|
|
||||||
|
class HCIBroadcaster:
|
||||||
|
def __init__(self, b64key):
|
||||||
|
self.key = base64.b64decode(b64key)
|
||||||
|
|
||||||
|
def _advertisement_template():
|
||||||
|
adv = ""
|
||||||
|
adv += "1e" # length (30)
|
||||||
|
adv += "ff" # manufacturer specific data
|
||||||
|
adv += "4c00" # company ID (Apple)
|
||||||
|
adv += "1219" # offline finding type and length
|
||||||
|
adv += "00" # state
|
||||||
|
for _ in range(22): # key[6:28]
|
||||||
|
adv += "00"
|
||||||
|
adv += "00" # first two bits of key[0]
|
||||||
|
adv += "00" # hint
|
||||||
|
return bytearray.fromhex(adv)
|
||||||
|
|
||||||
|
def _bytes_to_strarray(self, bytes_, with_prefix=False):
|
||||||
|
if with_prefix:
|
||||||
|
return [hex(b) for b in bytes_]
|
||||||
|
else:
|
||||||
|
return [format(b, "x") for b in bytes_]
|
||||||
|
|
||||||
|
def _run_hci_cmd(self, cmd, hci="hci0", wait=1):
|
||||||
|
cmd_ = ["hcitool", "-i", hci, "cmd"]
|
||||||
|
cmd_ += cmd
|
||||||
|
print(cmd_)
|
||||||
|
subprocess.run(cmd_)
|
||||||
|
if wait > 0:
|
||||||
|
sleep(wait)
|
||||||
|
|
||||||
|
def start_advertising(self, interval_ms=2000):
|
||||||
|
key = self.key
|
||||||
|
addr = bytearray(key[:6])
|
||||||
|
addr[0] |= 0b11000000
|
||||||
|
|
||||||
|
adv = self._advertisement_template()
|
||||||
|
adv[7:29] = key[6:28]
|
||||||
|
adv[29] = key[0] >> 6
|
||||||
|
|
||||||
|
print(f"key ({len(key):2}) {key.hex()}")
|
||||||
|
print(f"address ({len(addr):2}) {addr.hex()}")
|
||||||
|
print(f"payload ({len(adv):2}) {adv.hex()}")
|
||||||
|
|
||||||
|
# Set BLE address
|
||||||
|
self._run_hci_cmd(
|
||||||
|
["0x3f", "0x001"] + self._bytes_to_strarray(addr, with_prefix=True)[::-1]
|
||||||
|
)
|
||||||
|
subprocess.run(["systemctl", "restart", "bluetooth"])
|
||||||
|
sleep(1)
|
||||||
|
|
||||||
|
# Set BLE advertisement payload
|
||||||
|
self._run_hci_cmd(
|
||||||
|
["0x08", "0x0008"] + [format(len(adv), "x")] + self._bytes_to_strarray(adv)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Set BLE advertising mode
|
||||||
|
interval_enc = pack("<h", interval_ms)
|
||||||
|
hci_set_adv_params = ["0x08", "0x0006"]
|
||||||
|
hci_set_adv_params += self._bytes_to_strarray(interval_enc)
|
||||||
|
hci_set_adv_params += self._bytes_to_strarray(interval_enc)
|
||||||
|
hci_set_adv_params += ["03", "00", "00", "00", "00", "00", "00", "00", "00"]
|
||||||
|
hci_set_adv_params += ["07", "00"]
|
||||||
|
self._run_hci_cmd(hci_set_adv_params)
|
||||||
|
|
||||||
|
# Start BLE advertising
|
||||||
|
self._run_hci_cmd(["0x08", "0x000a"] + ["01"], wait=0)
|
17
main.py
17
main.py
|
@ -1,7 +1,9 @@
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
import yaml
|
||||||
import smbus2
|
import smbus2
|
||||||
import si7201
|
import si7201
|
||||||
import tmp006
|
import tmp006
|
||||||
|
import hci
|
||||||
|
|
||||||
bus = smbus2.SMBus(1) # set up I2C bus 1
|
bus = smbus2.SMBus(1) # set up I2C bus 1
|
||||||
|
|
||||||
|
@ -20,7 +22,16 @@ print(irtempsensor.manID) # read the manufacturer ID and print
|
||||||
print(irtempsensor.devID) # read the device ID and print
|
print(irtempsensor.devID) # read the device ID and print
|
||||||
print(irtempsensor.temperature) # read the temperature and print
|
print(irtempsensor.temperature) # read the temperature and print
|
||||||
|
|
||||||
print("========= TMP006 Test Loop =========")
|
print("========= Testing HCI Cast =========")
|
||||||
|
with open(".secrets.yml", "r") as secrets:
|
||||||
|
try:
|
||||||
|
secrets = yaml.safe_load(secrets)
|
||||||
|
key = secrets["key"] # Get Base64 encoded device public key from secrets file
|
||||||
|
except yaml.YAMLError as exc:
|
||||||
|
print(exc)
|
||||||
|
|
||||||
|
btcast = hci.HCIBroadcaster(key) # set up HCI Broadcaster
|
||||||
|
btcast.start_advertising(5000) # start advertising with interval of 5 seconds
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
print(irtempsensor.temperature) # read the temperature and print
|
btcast.start_advertising(5000) # start advertising with interval of 5 seconds
|
||||||
sleep(1) # wait for 1 second
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
colorzero==2.0
|
colorzero==2.0
|
||||||
gpiozero==1.6.2
|
gpiozero==1.6.2
|
||||||
|
PyYAML==6.0
|
||||||
smbus2==0.4.1
|
smbus2==0.4.1
|
||||||
|
|
Loading…
Reference in a new issue