Myles Watson | 6c25287 | 2018-11-21 16:24:52 -0800 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright 2018 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 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include <string> |
| 22 | |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 23 | namespace bluetooth { |
| 24 | namespace types { |
| 25 | |
Myles Watson | 6c25287 | 2018-11-21 16:24:52 -0800 | [diff] [blame] | 26 | /** Bluetooth Class of Device */ |
| 27 | class ClassOfDevice final { |
| 28 | public: |
| 29 | static constexpr unsigned int kLength = 3; |
| 30 | |
| 31 | uint8_t cod[kLength]; |
| 32 | |
| 33 | ClassOfDevice() = default; |
| 34 | ClassOfDevice(const uint8_t (&class_of_device)[kLength]); |
| 35 | |
| 36 | bool operator==(const ClassOfDevice& rhs) const { |
| 37 | return (std::memcmp(cod, rhs.cod, sizeof(cod)) == 0); |
| 38 | } |
| 39 | |
| 40 | std::string ToString() const; |
| 41 | |
| 42 | // Converts |string| to ClassOfDevice and places it in |to|. If |from| does |
| 43 | // not represent a Class of Device, |to| is not modified and this function |
| 44 | // returns false. Otherwise, it returns true. |
| 45 | static bool FromString(const std::string& from, ClassOfDevice& to); |
| 46 | |
| 47 | // Copies |from| raw Class of Device octets to the local object. |
| 48 | // Returns the number of copied octets (always ClassOfDevice::kLength) |
| 49 | size_t FromOctets(const uint8_t* from); |
| 50 | |
| 51 | static bool IsValid(const std::string& class_of_device); |
| 52 | }; |
| 53 | |
| 54 | inline std::ostream& operator<<(std::ostream& os, const ClassOfDevice& c) { |
| 55 | os << c.ToString(); |
| 56 | return os; |
| 57 | } |
Chienyuan | db55f31 | 2019-10-31 14:01:28 +0800 | [diff] [blame] | 58 | |
| 59 | } // namespace types |
| 60 | } // namespace bluetooth |
| 61 | |
| 62 | using ::bluetooth::types::ClassOfDevice; // TODO, remove |