aboutsummaryrefslogtreecommitdiff
path: root/gd/hci/enum_helper.h
blob: adbc3c71e1e8fdc37e9c8b4039098244ccfa7220 (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
/*
 * Copyright 2020 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.
 */
#pragma once

#include <type_traits>

#include "common/strings.h"
#include "hci/hci_packets.h"

// Define new enums or parsers for existing enums
namespace bluetooth {
namespace hci {

// Must be 0b00, 0b01, 0b10, and 0b11 as this is a bit mask
enum DeviceType { UNKNOWN = 0, BR_EDR = 1, LE = 2, DUAL = 3 };

// Scan mode from legacy stack, which is different from hci::ScanEnable
enum LegacyScanMode { BT_SCAN_MODE_NONE = 0, BT_SCAN_MODE_CONNECTABLE = 1, BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE = 2 };

}  // namespace hci

// Must be defined in bluetooth namespace
template <typename T, typename std::enable_if<std::is_same_v<T, hci::DeviceType>, int>::type = 0>
std::optional<hci::DeviceType> FromLegacyConfigString(const std::string& str) {
  auto raw_value = common::Int64FromString(str);
  if (!raw_value) {
    return std::nullopt;
  }
  if (*raw_value < hci::DeviceType::UNKNOWN || *raw_value > hci::DeviceType::DUAL) {
    return std::nullopt;
  }
  return static_cast<hci::DeviceType>(*raw_value);
}

// Must be defined in bluetooth namespace
template <typename T, typename std::enable_if<std::is_same_v<T, hci::AddressType>, int>::type = 0>
std::optional<hci::AddressType> FromLegacyConfigString(const std::string& str) {
  auto raw_value = common::Int64FromString(str);
  if (!raw_value) {
    return std::nullopt;
  }
  if (*raw_value < static_cast<int64_t>(hci::AddressType::PUBLIC_DEVICE_ADDRESS) ||
      *raw_value > static_cast<int64_t>(hci::AddressType::RANDOM_IDENTITY_ADDRESS)) {
    return std::nullopt;
  }
  return static_cast<hci::AddressType>(*raw_value);
}

// Must be defined in bluetooth namespace
template <typename T, typename std::enable_if<std::is_same_v<T, hci::KeyType>, int>::type = 0>
std::optional<hci::KeyType> FromLegacyConfigString(const std::string& str) {
  auto raw_value = common::Int64FromString(str);
  if (!raw_value) {
    return std::nullopt;
  }
  if (*raw_value < static_cast<int64_t>(hci::KeyType::COMBINATION) ||
      *raw_value > static_cast<int64_t>(hci::KeyType::AUTHENTICATED_P256)) {
    return std::nullopt;
  }
  return static_cast<hci::KeyType>(*raw_value);
}

// Must be defined in bluetooth namespace
template <typename T, typename std::enable_if<std::is_same_v<T, hci::LegacyScanMode>, int>::type = 0>
std::optional<hci::LegacyScanMode> FromLegacyConfigString(const std::string& str) {
  auto raw_value = common::Int64FromString(str);
  if (!raw_value) {
    return std::nullopt;
  }
  if (*raw_value < static_cast<int64_t>(hci::LegacyScanMode::BT_SCAN_MODE_NONE) ||
      *raw_value > static_cast<int64_t>(hci::LegacyScanMode::BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)) {
    return std::nullopt;
  }
  return static_cast<hci::LegacyScanMode>(*raw_value);
}

}  // namespace bluetooth