Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 1 | # Copyright 2021-2022 Google LLC |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | # ----------------------------------------------------------------------------- |
| 16 | # Imports |
| 17 | # ----------------------------------------------------------------------------- |
| 18 | import asyncio |
| 19 | import sys |
| 20 | import os |
| 21 | import random |
| 22 | import logging |
| 23 | |
| 24 | from bumble.device import Device, Connection |
| 25 | from bumble.transport import open_transport_or_link |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 26 | from bumble.gatt import Service, Characteristic |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 27 | |
| 28 | |
| 29 | # ----------------------------------------------------------------------------- |
| 30 | class Listener(Device.Listener, Connection.Listener): |
| 31 | def __init__(self, device): |
| 32 | self.device = device |
| 33 | |
| 34 | def on_connection(self, connection): |
| 35 | print(f'=== Connected to {connection}') |
| 36 | connection.listener = self |
| 37 | |
| 38 | def on_disconnection(self, reason): |
| 39 | print(f'### Disconnected, reason={reason}') |
| 40 | |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 41 | def on_characteristic_subscription( |
| 42 | self, connection, characteristic, notify_enabled, indicate_enabled |
| 43 | ): |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 44 | print( |
Gilles Boccon-Gibod | c2959da | 2022-12-10 09:29:51 -0800 | [diff] [blame] | 45 | f'$$$ Characteristic subscription for handle {characteristic.handle} ' |
| 46 | f'from {connection}: ' |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 47 | f'notify {"enabled" if notify_enabled else "disabled"}, ' |
| 48 | f'indicate {"enabled" if indicate_enabled else "disabled"}' |
| 49 | ) |
| 50 | |
| 51 | |
| 52 | # ----------------------------------------------------------------------------- |
Gilles Boccon-Gibod | ce9004f | 2022-06-02 11:44:01 -0700 | [diff] [blame] | 53 | # Alternative way to listen for subscriptions |
| 54 | # ----------------------------------------------------------------------------- |
| 55 | def on_my_characteristic_subscription(peer, enabled): |
| 56 | print(f'### My characteristic from {peer}: {"enabled" if enabled else "disabled"}') |
| 57 | |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 58 | |
Gilles Boccon-Gibod | ce9004f | 2022-06-02 11:44:01 -0700 | [diff] [blame] | 59 | # ----------------------------------------------------------------------------- |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 60 | async def main(): |
| 61 | if len(sys.argv) < 3: |
Gilles Boccon-Gibod | ce9004f | 2022-06-02 11:44:01 -0700 | [diff] [blame] | 62 | print('Usage: run_notifier.py <device-config> <transport-spec>') |
| 63 | print('example: run_notifier.py device1.json usb:0') |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 64 | return |
| 65 | |
| 66 | print('<<< connecting to HCI...') |
| 67 | async with await open_transport_or_link(sys.argv[2]) as (hci_source, hci_sink): |
| 68 | print('<<< connected') |
| 69 | |
| 70 | # Create a device to manage the host |
| 71 | device = Device.from_config_file_with_hci(sys.argv[1], hci_source, hci_sink) |
| 72 | device.listener = Listener(device) |
| 73 | |
| 74 | # Add a few entries to the device's GATT server |
| 75 | characteristic1 = Characteristic( |
| 76 | '486F64C6-4B5F-4B3B-8AFF-EDE134A8446A', |
Alan Rosenthal | fb49a87 | 2023-03-30 16:14:53 -0400 | [diff] [blame] | 77 | Characteristic.Properties.READ | Characteristic.Properties.NOTIFY, |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 78 | Characteristic.READABLE, |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 79 | bytes([0x40]), |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 80 | ) |
| 81 | characteristic2 = Characteristic( |
| 82 | '8EBDEBAE-0017-418E-8D3B-3A3809492165', |
Alan Rosenthal | fb49a87 | 2023-03-30 16:14:53 -0400 | [diff] [blame] | 83 | Characteristic.Properties.READ | Characteristic.Properties.INDICATE, |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 84 | Characteristic.READABLE, |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 85 | bytes([0x41]), |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 86 | ) |
| 87 | characteristic3 = Characteristic( |
| 88 | '8EBDEBAE-0017-418E-8D3B-3A3809492165', |
Alan Rosenthal | fb49a87 | 2023-03-30 16:14:53 -0400 | [diff] [blame] | 89 | Characteristic.Properties.READ |
| 90 | | Characteristic.Properties.NOTIFY |
| 91 | | Characteristic.Properties.INDICATE, |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 92 | Characteristic.READABLE, |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 93 | bytes([0x42]), |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 94 | ) |
Gilles Boccon-Gibod | ce9004f | 2022-06-02 11:44:01 -0700 | [diff] [blame] | 95 | characteristic3.on('subscription', on_my_characteristic_subscription) |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 96 | custom_service = Service( |
| 97 | '50DB505C-8AC4-4738-8448-3B1D9CC09CC5', |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 98 | [characteristic1, characteristic2, characteristic3], |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 99 | ) |
| 100 | device.add_services([custom_service]) |
| 101 | |
| 102 | # Debug print |
| 103 | for attribute in device.gatt_server.attributes: |
| 104 | print(attribute) |
| 105 | |
| 106 | # Get things going |
| 107 | await device.power_on() |
| 108 | |
| 109 | # Connect to a peer |
| 110 | if len(sys.argv) > 3: |
| 111 | target_address = sys.argv[3] |
| 112 | print(f'=== Connecting to {target_address}...') |
| 113 | await device.connect(target_address) |
| 114 | else: |
| 115 | await device.start_advertising(auto_restart=True) |
| 116 | |
| 117 | while True: |
| 118 | await asyncio.sleep(3.0) |
| 119 | characteristic1.value = bytes([random.randint(0, 255)]) |
| 120 | await device.notify_subscribers(characteristic1) |
| 121 | characteristic2.value = bytes([random.randint(0, 255)]) |
| 122 | await device.indicate_subscribers(characteristic2) |
| 123 | characteristic3.value = bytes([random.randint(0, 255)]) |
| 124 | await device.notify_subscribers(characteristic3) |
| 125 | await device.indicate_subscribers(characteristic3) |
| 126 | |
| 127 | |
| 128 | # ----------------------------------------------------------------------------- |
Gilles Boccon-Gibod | 135df0d | 2022-12-10 08:53:51 -0800 | [diff] [blame] | 129 | logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper()) |
Gilles Boccon-Gibod | 6ac91f7 | 2022-05-16 19:42:31 -0700 | [diff] [blame] | 130 | asyncio.run(main()) |