• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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  *
17  */
18 #pragma once
19 
20 #include <memory>
21 #include <vector>
22 
23 #include "hci/address_with_type.h"
24 #include "hci/hci_layer.h"
25 #include "hci/hci_packets.h"
26 #include "hci/security_interface.h"
27 
28 namespace bluetooth {
29 namespace security {
30 namespace channel {
31 
32 /**
33  * Interface for listening to the channel for SMP commands.
34  */
35 class ISecurityManagerChannelListener {
36  public:
37   virtual ~ISecurityManagerChannelListener() = default;
38   virtual void OnHciEventReceived(hci::EventPacketView packet) = 0;
39 };
40 
41 /**
42  * Channel for consolidating traffic and making the transport agnostic.
43  */
44 class SecurityManagerChannel {
45  public:
SecurityManagerChannel(os::Handler * handler,hci::HciLayer * hci_layer)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 
52   /**
53    * Send a given SMP command over the SecurityManagerChannel
54    *
55    * @param command smp command to send
56    */
57   void SendCommand(std::unique_ptr<hci::SecurityCommandBuilder> command);
58 
59   /**
60    * Sets the listener to listen for channel events
61    *
62    * @param listener the caller interested in events
63    */
SetChannelListener(ISecurityManagerChannelListener * listener)64   void SetChannelListener(ISecurityManagerChannelListener* listener) {
65     listener_ = listener;
66   }
67 
68   /**
69    * Called when an incoming HCI event happens
70    *
71    * @param event_packet
72    */
73   void OnHciEventReceived(hci::EventPacketView packet);
74 
75   /**
76    * Called when an HCI command is completed
77    *
78    * @param on_complete
79    */
80   void OnCommandComplete(hci::CommandCompleteView packet);
81 
82  private:
83   ISecurityManagerChannelListener* listener_;
84   hci::SecurityInterface* hci_security_interface_;
85   os::Handler* handler_;
86 };
87 
88 }  // namespace channel
89 }  // namespace security
90 }  // namespace bluetooth
91