• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright (C) 2015 Google, Inc.
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 #include "service/common/bluetooth/util/address_helper.h"
18 
19 #include <cstdlib>
20 
21 #include <base/logging.h>
22 #include <base/strings/string_split.h>
23 
24 namespace util {
25 
IsAddressValid(const std::string & address)26 bool IsAddressValid(const std::string& address) {
27   bt_bdaddr_t addr;
28   return BdAddrFromString(address, &addr);
29 }
30 
BdAddrFromString(const std::string & address,bt_bdaddr_t * out_addr)31 bool BdAddrFromString(const std::string& address, bt_bdaddr_t* out_addr) {
32   CHECK(out_addr);
33 
34   if (address.length() != 17) return false;
35 
36   std::vector<std::string> byte_tokens = base::SplitString(
37       address, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
38 
39   if (byte_tokens.size() != 6) return false;
40 
41   for (int i = 0; i < 6; i++) {
42     const auto& token = byte_tokens[i];
43 
44     if (token.length() != 2) return false;
45 
46     char* temp = nullptr;
47     out_addr->address[i] = strtol(token.c_str(), &temp, 16);
48     if (*temp != '\0') return false;
49   }
50 
51   return true;
52 }
53 
54 }  // namespace util
55