|
| 1 | +# Copyright (c) 2020, Digi International Inc. |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +# SOFTWARE. |
| 20 | + |
| 21 | +# Change these two variables to your device's address and address type. |
| 22 | +# The address and address type can be discovered using ble.gap_scan(). |
| 23 | + |
| 24 | +import binascii |
| 25 | +import time |
| 26 | + |
| 27 | +from digi import ble |
| 28 | + |
| 29 | +REMOTE_ADDRESS = "f2:bc:3c:06:01:0a" |
| 30 | +address_type = ble.ADDR_TYPE_PUBLIC |
| 31 | + |
| 32 | +# Put address into bytes (without colons) |
| 33 | +address = binascii.unhexlify(REMOTE_ADDRESS.replace(':', '')) |
| 34 | + |
| 35 | + |
| 36 | +# Present the passkey to the user |
| 37 | +def display_cb(passkey): |
| 38 | + print("The passkey is {}".format(passkey)) |
| 39 | + |
| 40 | + |
| 41 | +# Solicit the passkey from the user. |
| 42 | +# NOTE: `ble.passkey_enter()` does not need to be called from the |
| 43 | +# callback, this is done for simplicity here, but blocking the |
| 44 | +# callback for user activity in a real application may not be |
| 45 | +# desirable. |
| 46 | +def request_cb(): |
| 47 | + print("The passkey is being requested") |
| 48 | + passkey = input("Passkey: ") |
| 49 | + passkey = int(passkey) |
| 50 | + ble.passkey_enter(passkey) |
| 51 | + |
| 52 | + |
| 53 | +# Ask the user to confirm the passkey |
| 54 | +# NOTE: As above `passkey_confirm` need not be called from the |
| 55 | +# callback. |
| 56 | +def confirm_cb(passkey): |
| 57 | + print("The passkey is {}".format(passkey)) |
| 58 | + yn = input("Is this correct (y/n): ") |
| 59 | + yn = yn.strip().lower() |
| 60 | + ble.passkey_confirm(yn[0] == 'y') |
| 61 | + |
| 62 | + |
| 63 | +# Called when the `secure` operation completes |
| 64 | +def secure_cb(code): |
| 65 | + if code == 0: |
| 66 | + print("Secured") |
| 67 | + else: |
| 68 | + print("Pairing failed with error 0x{:x}".format(code)) |
| 69 | + |
| 70 | + |
| 71 | +def main(): |
| 72 | + # io_callbacks must be called before `ble.config` to enable |
| 73 | + # Require MITM. |
| 74 | + ble.io_callbacks(display_cb=display_cb, |
| 75 | + request_cb=request_cb, |
| 76 | + confirm_cb=confirm_cb) |
| 77 | + ble.config(security=ble.PAIRING_REQUIRE_MITM) |
| 78 | + # Comment the line above, and uncomment the line below to use |
| 79 | + # bonding. Once bonded the pairing activity will no longer be |
| 80 | + # necessary on each connection as long as the keys are retained by |
| 81 | + # the devices. |
| 82 | + # ble.config(security=ble.PAIRING_REQUIRE_MITM | ble.PAIRING_REQUIRE_BONDING) |
| 83 | + |
| 84 | + |
| 85 | + print("Connecting") |
| 86 | + conn = ble.gap_connect(address_type, address) |
| 87 | + print("Connected") |
| 88 | + |
| 89 | + # The delay is not necessary, just here to easily observe the |
| 90 | + # secured vs unsecured state. |
| 91 | + print("Wait for a bit before securing") |
| 92 | + time.sleep(5) |
| 93 | + |
| 94 | + print("Securing") |
| 95 | + conn.secure(secure_cb) |
| 96 | + |
| 97 | + print("Sleep forever") |
| 98 | + while True: |
| 99 | + time.sleep(1) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == "__main__": |
| 103 | + main() |
0 commit comments