Myles Watson | 0ead597 | 2019-04-01 13:21:25 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright 2019 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 | #include "address.h" |
| 20 | |
| 21 | #include <stdint.h> |
| 22 | #include <algorithm> |
| 23 | #include <sstream> |
| 24 | #include <vector> |
| 25 | |
| 26 | namespace bluetooth { |
| 27 | namespace common { |
| 28 | |
| 29 | static_assert(sizeof(Address) == 6, "Address must be 6 bytes long!"); |
| 30 | |
| 31 | const Address Address::kAny{{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}}; |
| 32 | const Address Address::kEmpty{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; |
| 33 | |
| 34 | Address::Address(const uint8_t (&addr)[6]) { |
| 35 | std::copy(addr, addr + kLength, address); |
| 36 | }; |
| 37 | |
| 38 | std::string Address::ToString() const { |
| 39 | char buffer[] = "00:00:00:00:00:00"; |
| 40 | std::snprintf(&buffer[0], sizeof(buffer), |
| 41 | "%02x:%02x:%02x:%02x:%02x:%02x", address[5], address[4], address[3], address[2], address[1], address[0]); |
| 42 | std::string str(buffer); |
| 43 | return str; |
| 44 | } |
| 45 | |
| 46 | bool Address::FromString(const std::string& from, Address& to) { |
| 47 | Address new_addr; |
| 48 | if (from.length() != 17) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | std::istringstream stream(from); |
| 53 | std::string token; |
| 54 | int index = 0; |
| 55 | while (getline(stream, token, ':')) { |
| 56 | if (index >= 6) { |
| 57 | return false; |
| 58 | } |
| 59 | |
| 60 | if (token.length() != 2) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | char* temp = nullptr; |
| 65 | new_addr.address[5 - index] = strtol(token.c_str(), &temp, 16); |
| 66 | if (*temp != '\0') { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | index++; |
| 71 | } |
| 72 | |
| 73 | if (index != 6) { |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | to = new_addr; |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | size_t Address::FromOctets(const uint8_t* from) { |
| 82 | std::copy(from, from + kLength, address); |
| 83 | return kLength; |
| 84 | }; |
| 85 | |
| 86 | bool Address::IsValidAddress(const std::string& address) { |
| 87 | Address tmp; |
| 88 | return Address::FromString(address, tmp); |
| 89 | } |
| 90 | |
| 91 | } // namespace common |
| 92 | } // namespace bluetooth |