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]) {
40     std::copy(d, d + kLength, data());
41   }
ByteArray(std::array<uint8_t,kLength> a)42   ByteArray(std::array<uint8_t, kLength> a) : bytes(std::move(a)) {}
43 
44   std::array<uint8_t, kLength> bytes = {};
45 
data()46   uint8_t* data() override {
47     return bytes.data();
48   }
49 
data()50   const uint8_t* data() const override {
51     return bytes.data();
52   }
53 
54   // operators
55   bool operator<(const ByteArray& rhs) const {
56     return bytes < rhs.bytes;
57   }
58   bool operator==(const ByteArray& rhs) const {
59     return bytes == rhs.bytes;
60   }
61   bool operator>(const ByteArray& rhs) const {
62     return (rhs < *this);
63   }
64   bool operator<=(const ByteArray& rhs) const {
65     return !(*this > rhs);
66   }
67   bool operator>=(const ByteArray& rhs) const {
68     return !(*this < rhs);
69   }
70   bool operator!=(const ByteArray& rhs) const {
71     return !(*this == rhs);
72   }
73 
74   // storage::Serializable methods
ToString()75   std::string ToString() const override {
76     return common::ToHexString(bytes.begin(), bytes.end());
77   }
FromString(const std::string & from)78   static std::optional<ByteArray<kLength>> FromString(const std::string& from) {
79     if (from.length() != (kLength * 2)) {
80       return std::nullopt;
81     }
82     auto vec = common::FromHexString(from);
83     if (!vec) {
84       return std::nullopt;
85     }
86     ByteArray<kLength> byte_array = {};
87     std::move(vec->data(), vec->data() + vec->size(), byte_array.data());
88     return byte_array;
89   }
ToLegacyConfigString()90   std::string ToLegacyConfigString() const override {
91     return ToString();
92   }
FromLegacyConfigString(const std::string & from)93   static std::optional<ByteArray<kLength>> FromLegacyConfigString(const std::string& from) {
94     return FromString(from);
95   }
96 };
97 
98 }  // namespace common
99 }  // namespace bluetooth