summaryrefslogtreecommitdiff
path: root/system/gd/neighbor/scan.cc
blob: 9508725f2e256b362a06839ba3092a9d6db46368 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#define LOG_TAG "bt_gd_neigh"

#include "neighbor/scan.h"

#include <memory>

#include "hci/hci_layer.h"
#include "hci/hci_packets.h"
#include "module.h"
#include "os/handler.h"
#include "os/log.h"

namespace bluetooth {
namespace neighbor {

struct ScanModule::impl {
  impl(ScanModule& module);

  void SetInquiryScan(bool enabled);
  bool IsInquiryEnabled() const;

  void SetPageScan(bool enabled);
  bool IsPageEnabled() const;

  void Start();
  void Stop();

 private:
  ScanModule& module_;

  bool inquiry_scan_enabled_;
  bool page_scan_enabled_;

  void WriteScanEnable();
  void ReadScanEnable(hci::ScanEnable);

  void OnCommandComplete(hci::CommandCompleteView status);

  hci::HciLayer* hci_layer_;
  os::Handler* handler_;
};

const ModuleFactory neighbor::ScanModule::Factory = ModuleFactory([]() { return new neighbor::ScanModule(); });

neighbor::ScanModule::impl::impl(neighbor::ScanModule& module)
    : module_(module), inquiry_scan_enabled_(false), page_scan_enabled_(false) {}

void neighbor::ScanModule::impl::OnCommandComplete(hci::CommandCompleteView view) {
  switch (view.GetCommandOpCode()) {
    case hci::OpCode::READ_SCAN_ENABLE: {
      auto packet = hci::ReadScanEnableCompleteView::Create(view);
      ASSERT(packet.IsValid());
      ASSERT(packet.GetStatus() == hci::ErrorCode::SUCCESS);
      ReadScanEnable(packet.GetScanEnable());
    } break;

    case hci::OpCode::WRITE_SCAN_ENABLE: {
      auto packet = hci::WriteScanEnableCompleteView::Create(view);
      ASSERT(packet.IsValid());
      ASSERT(packet.GetStatus() == hci::ErrorCode::SUCCESS);
    } break;

    default:
      LOG_ERROR("Unhandled command %s", hci::OpCodeText(view.GetCommandOpCode()).c_str());
      break;
  }
}

void neighbor::ScanModule::impl::WriteScanEnable() {
  hci::ScanEnable scan_enable;

  if (inquiry_scan_enabled_ && !page_scan_enabled_) {
    scan_enable = hci::ScanEnable::INQUIRY_SCAN_ONLY;
  } else if (!inquiry_scan_enabled_ && page_scan_enabled_) {
    scan_enable = hci::ScanEnable::PAGE_SCAN_ONLY;
  } else if (inquiry_scan_enabled_ && page_scan_enabled_) {
    scan_enable = hci::ScanEnable::INQUIRY_AND_PAGE_SCAN;
  } else {
    scan_enable = hci::ScanEnable::NO_SCANS;
  }

  {
    std::unique_ptr<hci::WriteScanEnableBuilder> packet = hci::WriteScanEnableBuilder::Create(scan_enable);
    hci_layer_->EnqueueCommand(std::move(packet), handler_->BindOnceOn(this, &impl::OnCommandComplete));
  }

  {
    std::unique_ptr<hci::ReadScanEnableBuilder> packet = hci::ReadScanEnableBuilder::Create();
    hci_layer_->EnqueueCommand(std::move(packet), handler_->BindOnceOn(this, &impl::OnCommandComplete));
  }
}

void neighbor::ScanModule::impl::ReadScanEnable(hci::ScanEnable scan_enable) {
  switch (scan_enable) {
    case hci::ScanEnable::INQUIRY_SCAN_ONLY:
      inquiry_scan_enabled_ = true;
      page_scan_enabled_ = false;
      break;

    case hci::ScanEnable::PAGE_SCAN_ONLY:
      inquiry_scan_enabled_ = false;
      page_scan_enabled_ = true;
      break;

    case hci::ScanEnable::INQUIRY_AND_PAGE_SCAN:
      inquiry_scan_enabled_ = true;
      page_scan_enabled_ = true;
      break;

    default:
      inquiry_scan_enabled_ = false;
      page_scan_enabled_ = false;
      break;
  }
}

void neighbor::ScanModule::impl::SetInquiryScan(bool enabled) {
  inquiry_scan_enabled_ = enabled;
  WriteScanEnable();
}

void neighbor::ScanModule::impl::SetPageScan(bool enabled) {
  page_scan_enabled_ = enabled;
  WriteScanEnable();
}

bool neighbor::ScanModule::impl::IsInquiryEnabled() const {
  return inquiry_scan_enabled_;
}

bool neighbor::ScanModule::impl::IsPageEnabled() const {
  return page_scan_enabled_;
}

void neighbor::ScanModule::impl::Start() {
  hci_layer_ = module_.GetDependency<hci::HciLayer>();
  handler_ = module_.GetHandler();

  std::unique_ptr<hci::ReadScanEnableBuilder> packet = hci::ReadScanEnableBuilder::Create();
  hci_layer_->EnqueueCommand(std::move(packet), handler_->BindOnceOn(this, &impl::OnCommandComplete));
}

void neighbor::ScanModule::impl::Stop() {
  LOG_INFO("inquiry scan enabled:%d page scan enabled:%d", inquiry_scan_enabled_, page_scan_enabled_);
}

neighbor::ScanModule::ScanModule() : pimpl_(std::make_unique<impl>(*this)) {}

neighbor::ScanModule::~ScanModule() {
  pimpl_.reset();
}

void neighbor::ScanModule::SetInquiryScan() {
  pimpl_->SetInquiryScan(true);
}

void neighbor::ScanModule::ClearInquiryScan() {
  pimpl_->SetInquiryScan(false);
}

void neighbor::ScanModule::SetPageScan() {
  pimpl_->SetPageScan(true);
}

void neighbor::ScanModule::ClearPageScan() {
  pimpl_->SetPageScan(false);
}

bool neighbor::ScanModule::IsInquiryEnabled() const {
  return pimpl_->IsInquiryEnabled();
}

bool neighbor::ScanModule::IsPageEnabled() const {
  return pimpl_->IsPageEnabled();
}

void neighbor::ScanModule::ListDependencies(ModuleList* list) const {
  list->add<hci::HciLayer>();
}

void neighbor::ScanModule::Start() {
  pimpl_->Start();
}

void neighbor::ScanModule::Stop() {
  pimpl_->Stop();
}

}  // namespace neighbor
}  // namespace bluetooth