1""" 2 Copyright (c) 2024, The OpenThread Authors. 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions are met: 7 1. Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 2. Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in the 11 documentation and/or other materials provided with the distribution. 12 3. Neither the name of the copyright holder nor the 13 names of its contributors may be used to endorse or promote products 14 derived from this software without specific prior written permission. 15 16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 20 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 POSSIBILITY OF SUCH DAMAGE. 27""" 28 29import asyncio 30import argparse 31from os import path 32import logging 33 34from ble.ble_connection_constants import BBTC_SERVICE_UUID, BBTC_TX_CHAR_UUID, \ 35 BBTC_RX_CHAR_UUID, SERVER_COMMON_NAME 36from ble.ble_stream import BleStream 37from ble.udp_stream import UdpStream 38from ble.ble_stream_secure import BleStreamSecure 39from ble import ble_scanner 40from cli.cli import CLI 41from dataset.dataset import ThreadDataset 42from cli.command import CommandResult 43from utils import select_device_by_user_input 44 45 46async def main(): 47 logging.basicConfig(level=logging.WARNING) 48 49 parser = argparse.ArgumentParser(description='Device parameters') 50 parser.add_argument('--debug', help='Enable debug logs', action='store_true') 51 parser.add_argument('--cert_path', help='Path to certificate chain and key', action='store', default='auth') 52 group = parser.add_mutually_exclusive_group() 53 group.add_argument('--mac', type=str, help='Device MAC address', action='store') 54 group.add_argument('--name', type=str, help='Device name', action='store') 55 group.add_argument('--scan', help='Scan all available devices', action='store_true') 56 group.add_argument('--simulation', help='Connect to simulation node id', action='store') 57 args = parser.parse_args() 58 59 if args.debug: 60 logging.getLogger('ble_stream').setLevel(logging.DEBUG) 61 logging.getLogger('ble_stream_secure').setLevel(logging.DEBUG) 62 63 device = await get_device_by_args(args) 64 65 ble_sstream = None 66 67 if not (device is None): 68 print(f'Connecting to {device}') 69 ble_sstream = BleStreamSecure(device) 70 ble_sstream.load_cert( 71 certfile=path.join(args.cert_path, 'commissioner_cert.pem'), 72 keyfile=path.join(args.cert_path, 'commissioner_key.pem'), 73 cafile=path.join(args.cert_path, 'ca_cert.pem'), 74 ) 75 76 print('Setting up secure channel...') 77 await ble_sstream.do_handshake(hostname=SERVER_COMMON_NAME) 78 print('Done') 79 80 ds = ThreadDataset() 81 cli = CLI(ds, ble_sstream) 82 loop = asyncio.get_running_loop() 83 print('Enter \'help\' to see available commands' ' or \'exit\' to exit the application.') 84 while True: 85 user_input = await loop.run_in_executor(None, lambda: input('> ')) 86 if user_input.lower() == 'exit': 87 print('Disconnecting...') 88 break 89 try: 90 result: CommandResult = await cli.evaluate_input(user_input) 91 if result: 92 result.pretty_print() 93 except Exception as e: 94 print(e) 95 96 97async def get_device_by_args(args): 98 device = None 99 if args.mac: 100 device = await ble_scanner.find_first_by_mac(args.mac) 101 device = await BleStream.create(device.address, BBTC_SERVICE_UUID, BBTC_TX_CHAR_UUID, BBTC_RX_CHAR_UUID) 102 elif args.name: 103 device = await ble_scanner.find_first_by_name(args.name) 104 device = await BleStream.create(device.address, BBTC_SERVICE_UUID, BBTC_TX_CHAR_UUID, BBTC_RX_CHAR_UUID) 105 elif args.scan: 106 tcat_devices = await ble_scanner.scan_tcat_devices() 107 device = select_device_by_user_input(tcat_devices) 108 device = await BleStream.create(device.address, BBTC_SERVICE_UUID, BBTC_TX_CHAR_UUID, BBTC_RX_CHAR_UUID) 109 elif args.simulation: 110 device = UdpStream("127.0.0.1", int(args.simulation)) 111 112 return device 113 114 115if __name__ == '__main__': 116 try: 117 asyncio.run(main()) 118 except asyncio.CancelledError: 119 pass # device disconnected 120