1# Copyright 2021-2023 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# -----------------------------------------------------------------------------
18from bumble.hci import (
19    hci_vendor_command_op_code,
20    HCI_Command,
21    STATUS_SPEC,
22)
23
24
25# -----------------------------------------------------------------------------
26# Constants
27# -----------------------------------------------------------------------------
28
29# Zephyr RTOS Vendor Specific Commands and Events.
30# Only a subset of the commands are implemented here currently.
31#
32# pylint: disable-next=line-too-long
33# See https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/bluetooth/hci_vs.h
34HCI_WRITE_TX_POWER_LEVEL_COMMAND = hci_vendor_command_op_code(0x000E)
35HCI_READ_TX_POWER_LEVEL_COMMAND = hci_vendor_command_op_code(0x000F)
36
37HCI_Command.register_commands(globals())
38
39
40# -----------------------------------------------------------------------------
41class TX_Power_Level_Command:
42    '''
43    Base class for read and write TX power level HCI commands
44    '''
45
46    TX_POWER_HANDLE_TYPE_ADV = 0x00
47    TX_POWER_HANDLE_TYPE_SCAN = 0x01
48    TX_POWER_HANDLE_TYPE_CONN = 0x02
49
50
51# -----------------------------------------------------------------------------
52@HCI_Command.command(
53    fields=[('handle_type', 1), ('connection_handle', 2), ('tx_power_level', -1)],
54    return_parameters_fields=[
55        ('status', STATUS_SPEC),
56        ('handle_type', 1),
57        ('connection_handle', 2),
58        ('selected_tx_power_level', -1),
59    ],
60)
61class HCI_Write_Tx_Power_Level_Command(HCI_Command, TX_Power_Level_Command):
62    '''
63    Write TX power level. See BT_HCI_OP_VS_WRITE_TX_POWER_LEVEL in
64    https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/bluetooth/hci_vs.h
65
66    Power level is in dB. Connection handle for TX_POWER_HANDLE_TYPE_ADV and
67    TX_POWER_HANDLE_TYPE_SCAN should be zero.
68    '''
69
70
71# -----------------------------------------------------------------------------
72@HCI_Command.command(
73    fields=[('handle_type', 1), ('connection_handle', 2)],
74    return_parameters_fields=[
75        ('status', STATUS_SPEC),
76        ('handle_type', 1),
77        ('connection_handle', 2),
78        ('tx_power_level', -1),
79    ],
80)
81class HCI_Read_Tx_Power_Level_Command(HCI_Command, TX_Power_Level_Command):
82    '''
83    Read TX power level. See BT_HCI_OP_VS_READ_TX_POWER_LEVEL in
84    https://github.com/zephyrproject-rtos/zephyr/blob/main/include/zephyr/bluetooth/hci_vs.h
85
86    Power level is in dB. Connection handle for TX_POWER_HANDLE_TYPE_ADV and
87    TX_POWER_HANDLE_TYPE_SCAN should be zero.
88    '''
89