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() -> None:
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_transport:
38        device = Device.from_config_file_with_hci(
39            sys.argv[1], hci_transport.source, hci_transport.sink
40        )
41
42        # Add a Device Information Service to the GATT sever
43        device_information_service = DeviceInformationService(
44            manufacturer_name='ACME',
45            model_number='AB-102',
46            serial_number='7654321',
47            hardware_revision='1.1.3',
48            software_revision='2.5.6',
49            system_id=(0x123456, 0x8877665544),
50        )
51        device.add_service(device_information_service)
52
53        # Set the advertising data
54        device.advertising_data = bytes(
55            AdvertisingData(
56                [
57                    (
58                        AdvertisingData.COMPLETE_LOCAL_NAME,
59                        bytes('Bumble Device', 'utf-8'),
60                    ),
61                    (AdvertisingData.APPEARANCE, struct.pack('<H', 0x0340)),
62                ]
63            )
64        )
65
66        # Go!
67        await device.power_on()
68        await device.start_advertising(auto_restart=True)
69        await hci_transport.source.wait_for_termination()
70
71
72# -----------------------------------------------------------------------------
73logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'DEBUG').upper())
74asyncio.run(main())
75