/* * 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 #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 , int>::type = 0> std::optional 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(*raw_value); } // Must be defined in bluetooth namespace template , int>::type = 0> std::optional FromLegacyConfigString(const std::string& str) { auto raw_value = common::Int64FromString(str); if (!raw_value) { return std::nullopt; } if (*raw_value < static_cast(hci::AddressType::PUBLIC_DEVICE_ADDRESS) || *raw_value > static_cast(hci::AddressType::RANDOM_IDENTITY_ADDRESS)) { return std::nullopt; } return static_cast(*raw_value); } // Must be defined in bluetooth namespace template , int>::type = 0> std::optional FromLegacyConfigString(const std::string& str) { auto raw_value = common::Int64FromString(str); if (!raw_value) { return std::nullopt; } if (*raw_value < static_cast(hci::KeyType::COMBINATION) || *raw_value > static_cast(hci::KeyType::AUTHENTICATED_P256)) { return std::nullopt; } return static_cast(*raw_value); } // Must be defined in bluetooth namespace template , int>::type = 0> std::optional FromLegacyConfigString(const std::string& str) { auto raw_value = common::Int64FromString(str); if (!raw_value) { return std::nullopt; } if (*raw_value < static_cast(hci::LegacyScanMode::BT_SCAN_MODE_NONE) || *raw_value > static_cast(hci::LegacyScanMode::BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)) { return std::nullopt; } return static_cast(*raw_value); } } // namespace bluetooth