1 /* 2 * Copyright 2020 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <algorithm> 20 #include <array> 21 #include <cstdint> 22 #include <initializer_list> 23 #include <optional> 24 #include <string> 25 26 #include "common/strings.h" 27 #include "packet/custom_field_fixed_size_interface.h" 28 #include "storage/serializable.h" 29 30 namespace bluetooth { 31 namespace common { 32 33 template <std::size_t LENGTH> 34 class ByteArray : public packet::CustomFieldFixedSizeInterface<ByteArray<LENGTH>>, 35 public storage::Serializable<ByteArray<LENGTH>> { 36 public: 37 static constexpr size_t kLength = LENGTH; 38 ByteArray() = default; ByteArray(const uint8_t (& d)[kLength])39 ByteArray(const uint8_t (&d)[kLength]) { std::copy(d, d + kLength, data()); } ByteArray(std::array<uint8_t,kLength> a)40 ByteArray(std::array<uint8_t, kLength> a) : bytes(std::move(a)) {} 41 42 std::array<uint8_t, kLength> bytes = {}; 43 data()44 uint8_t* data() override { return bytes.data(); } 45 data()46 const uint8_t* data() const override { return bytes.data(); } 47 48 // operators 49 bool operator<(const ByteArray& rhs) const { return bytes < rhs.bytes; } 50 bool operator==(const ByteArray& rhs) const { return bytes == rhs.bytes; } 51 bool operator>(const ByteArray& rhs) const { return rhs < *this; } 52 bool operator<=(const ByteArray& rhs) const { return !(*this > rhs); } 53 bool operator>=(const ByteArray& rhs) const { return !(*this < rhs); } 54 bool operator!=(const ByteArray& rhs) const { return !(*this == rhs); } 55 56 // storage::Serializable methods ToString()57 std::string ToString() const override { return common::ToHexString(bytes.begin(), bytes.end()); } FromString(const std::string & from)58 static std::optional<ByteArray<kLength>> FromString(const std::string& from) { 59 if (from.length() != (kLength * 2)) { 60 return std::nullopt; 61 } 62 auto vec = common::FromHexString(from); 63 if (!vec) { 64 return std::nullopt; 65 } 66 ByteArray<kLength> byte_array = {}; 67 std::move(vec->data(), vec->data() + vec->size(), byte_array.data()); 68 return byte_array; 69 } ToLegacyConfigString()70 std::string ToLegacyConfigString() const override { return ToString(); } FromLegacyConfigString(const std::string & from)71 static std::optional<ByteArray<kLength>> FromLegacyConfigString(const std::string& from) { 72 return FromString(from); 73 } 74 }; 75 76 } // namespace common 77 } // namespace bluetooth 78