• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# -----------------------------------------------------------------------------
18import asyncio
19import sys
20import os
21import logging
22import struct
23
24from bumble.core import AdvertisingData
25from bumble.device import Device
26from bumble.transport import open_transport_or_link
27from bumble.profiles.device_information_service import DeviceInformationService
28
29
30# -----------------------------------------------------------------------------
31async def main():
32    if len(sys.argv) != 3:
33        print('Usage: python device_info_server.py <device-config> <transport-spec>')
34        print('example: python device_info_server.py device1.json usb:0')
35        return
36
37    async with await open_transport_or_link(sys.argv[2]) as (hci_source, hci_sink):
38        device = Device.from_config_file_with_hci(sys.argv[1], hci_source, hci_sink)
39
40        # Add a Device Information Service to the GATT sever
41        device_information_service = DeviceInformationService(
42            manufacturer_name='ACME',
43            model_number='AB-102',
44            serial_number='7654321',
45            hardware_revision='1.1.3',
46            software_revision='2.5.6',
47            system_id=(0x123456, 0x8877665544),
48        )
49        device.add_service(device_information_service)
50
51        # Set the advertising data
52        device.advertising_data = bytes(
53            AdvertisingData(
54                [
55                    (
56                        AdvertisingData.COMPLETE_LOCAL_NAME,
57                        bytes('Bumble Device', 'utf-8'),
58                    ),
59                    (AdvertisingData.APPEARANCE, struct.pack('<H', 0x0340)),
60                ]
61            )
62        )
63
64        # Go!
65        await device.power_on()
66        await device.start_advertising(auto_restart=True)
67        await hci_source.wait_for_termination()
68
69
70# -----------------------------------------------------------------------------
71logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper())
72asyncio.run(main())
73