Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 1 | /* |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 2 | * |
| 3 | * Copyright 2019 The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0; |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 17 | */ |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 18 | #pragma once |
| 19 | |
| 20 | #include <memory> |
| 21 | #include <vector> |
| 22 | |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 23 | #include "hci/address_with_type.h" |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 24 | #include "hci/hci_layer.h" |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 25 | #include "hci/hci_packets.h" |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 26 | #include "hci/security_interface.h" |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 27 | |
| 28 | namespace bluetooth { |
| 29 | namespace security { |
| 30 | namespace channel { |
| 31 | |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 32 | /** |
| 33 | * Interface for listening to the channel for SMP commands. |
| 34 | */ |
| 35 | class ISecurityManagerChannelListener { |
| 36 | public: |
| 37 | virtual ~ISecurityManagerChannelListener() = default; |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 38 | virtual void OnHciEventReceived(hci::EventPacketView packet) = 0; |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | /** |
| 42 | * Channel for consolidating traffic and making the transport agnostic. |
| 43 | */ |
| 44 | class SecurityManagerChannel { |
| 45 | public: |
| 46 | explicit SecurityManagerChannel(os::Handler* handler, hci::HciLayer* hci_layer) |
| 47 | : listener_(nullptr), |
| 48 | hci_security_interface_(hci_layer->GetSecurityInterface( |
| 49 | common::Bind(&SecurityManagerChannel::OnHciEventReceived, common::Unretained(this)), handler)), |
| 50 | handler_(handler) {} |
| 51 | ~SecurityManagerChannel() { |
| 52 | delete listener_; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Send a given SMP command over the SecurityManagerChannel |
| 57 | * |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 58 | * @param command smp command to send |
| 59 | */ |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 60 | void SendCommand(std::unique_ptr<hci::SecurityCommandBuilder> command); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 61 | |
| 62 | /** |
| 63 | * Sets the listener to listen for channel events |
| 64 | * |
| 65 | * @param listener the caller interested in events |
| 66 | */ |
| 67 | void SetChannelListener(ISecurityManagerChannelListener* listener) { |
| 68 | listener_ = listener; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Called when an incoming HCI event happens |
| 73 | * |
| 74 | * @param event_packet |
| 75 | */ |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 76 | void OnHciEventReceived(hci::EventPacketView packet); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
| 79 | * Called when an HCI command is completed |
| 80 | * |
| 81 | * @param on_complete |
| 82 | */ |
Martin Brabham | 80854c2 | 2019-11-12 14:52:42 -0800 | [diff] [blame^] | 83 | void OnCommandComplete(hci::CommandCompleteView packet); |
Martin Brabham | 605d6f1 | 2019-03-29 12:02:30 -0700 | [diff] [blame] | 84 | |
| 85 | private: |
| 86 | ISecurityManagerChannelListener* listener_; |
| 87 | hci::SecurityInterface* hci_security_interface_; |
| 88 | os::Handler* handler_; |
| 89 | }; |
| 90 | |
| 91 | } // namespace channel |
| 92 | } // namespace security |
| 93 | } // namespace bluetooth |