Advertisement
thebys

BLE LOCK open device ESP32

Aug 31st, 2020 (edited)
2,523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. import ubluetooth
  2. import ubinascii
  3. import uos
  4. import time
  5. from ucryptolib import aes
  6. from micropython import const
  7.  
  8. _IRQ_SCAN_RESULT = const(5)
  9. _IRQ_SCAN_DONE = const(6)
  10. _IRQ_PERIPHERAL_CONNECT = const(7)
  11. _IRQ_PERIPHERAL_DISCONNECT = const(8)
  12. _IRQ_GATTC_SERVICE_RESULT = const(9)
  13. _IRQ_GATTC_SERVICE_DONE = const(10)
  14. _IRQ_GATTC_CHARACTERISTIC_RESULT = const(11)
  15. _IRQ_GATTC_CHARACTERISTIC_DONE = const(12)
  16. _IRQ_GATTC_DESCRIPTOR_RESULT = const(13)
  17. _IRQ_GATTC_DESCRIPTOR_DONE = const(14)
  18. _IRQ_GATTC_READ_RESULT = const(15)
  19. _IRQ_GATTC_READ_DONE = const(16)
  20. _IRQ_GATTC_WRITE_DONE = const(17)
  21. _IRQ_GATTC_NOTIFY = const(18)
  22. _IRQ_GATTC_INDICATE = const(19)
  23. _IRQ_GATTS_INDICATE_DONE = const(20)
  24.  
  25. mac = None
  26. connected = False
  27.  
  28.  
  29. def on_scan_result(data):
  30.  
  31.     global mac
  32.     addr_type, addr, adv_type, rssi, adv_data = data
  33.     mac = addr
  34.     print('type:{} addr:{} rssi:{} data:{}'.format(
  35.         addr_type, ubinascii.hexlify(addr), rssi, ubinascii.hexlify(adv_data)))
  36.  
  37.  
  38. def on_connection():
  39.  
  40.     global connected
  41.     print("CONNECTED!")
  42.     ble.gattc_discover_services(0)
  43.     ble.gattc_discover_characteristics(0, 9, 65535)
  44.     ble.gattc_discover_descriptors(0, 9, 65535)
  45.     connected = True
  46.  
  47.  
  48. def blelock_get_token():
  49.  
  50.     key = ubinascii.unhexlify('3A60432A5C01211F291E0F4E0C132825') #Known key supposted to work.
  51.     req_token_data = ubinascii.unhexlify('060101012D1A683D48271A18316E471A') #Instruction supposted to get token.
  52.     cipher = aes(key, 1)  # MODE_ECB = 1
  53.    
  54.     global mac
  55.     print('Connecting to address:', mac)
  56.     ble.gap_connect(1, mac)
  57.     while not(connected):
  58.         time.sleep(1)
  59.         print('…')
  60.     # print(req_token_data)
  61.     # encrypted = cipher.encrypt(req_token_data)
  62.     # print(encrypted)
  63.     # ble.gattc_write(0, 11, b'\x01\x00')    #subscribe to notifications?
  64.     # ble.gattc_write(0, 14, encrypted)      #send token request? Listen for response, use it to unlock.
  65.     print('All done for now.')
  66.  
  67.  
  68. def bt_irq(event, data):
  69.  
  70.     if event == _IRQ_SCAN_RESULT:
  71.         on_scan_result(data)
  72.  
  73.     elif event == _IRQ_SCAN_DONE:
  74.         print("Scan complete.")
  75.  
  76.     elif event == _IRQ_PERIPHERAL_CONNECT:
  77.         on_connection()
  78.  
  79.     elif event == _IRQ_PERIPHERAL_DISCONNECT:
  80.         print("DISCONNECTED!")
  81.  
  82.     elif event == _IRQ_GATTC_SERVICE_RESULT:
  83.         conn_handle, start_handle, end_handle, uuid = data
  84.         print('S:  ', data)
  85.  
  86.     elif event == _IRQ_GATTC_SERVICE_DONE:
  87.         conn_handle, status = data
  88.  
  89.     elif event == _IRQ_GATTC_CHARACTERISTIC_DONE:
  90.         conn_handle, status = data
  91.  
  92.     elif event == _IRQ_GATTC_DESCRIPTOR_DONE:
  93.         conn_handle, status = data
  94.  
  95.     elif event == _IRQ_GATTC_CHARACTERISTIC_RESULT:
  96.         conn_handle, def_handle, value_handle, properties, uuid = data
  97.         print('CH: ', data)
  98.  
  99.     elif event == _IRQ_GATTC_DESCRIPTOR_RESULT:
  100.         conn_handle, dsc_handle, uuid = data
  101.         print('Ds: ', data)
  102.  
  103.     elif event == _IRQ_GATTC_READ_RESULT:
  104.         conn_handle, value_handle, char_data = data
  105.         print('Dat:', data)
  106.  
  107.     elif event == _IRQ_GATTC_READ_DONE:
  108.         conn_handle, value_handle, status = data
  109.  
  110.     elif event == _IRQ_GATTC_WRITE_DONE:
  111.         conn_handle, value_handle, status = data
  112.     else:
  113.         print("Unhandled event!")
  114.         print(event)
  115.  
  116.  
  117. ble = ubluetooth.BLE()
  118. ble.irq(handler=bt_irq)
  119. ble.active(True)
  120. ble.gap_scan(10000, 30000, 30000)
  121. time.sleep(10)
  122. blelock_get_token()
  123. # blelock_unlock() #TODO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement