summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/bluetooth/bluetooth_utils.cc
blob: 608845fcf352ebecd00a532cb56a601156733460 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h"

#include <vector>

#include <bluetooth/bluetooth.h>

#include "base/logging.h"
#include "base/string_number_conversions.h"

namespace chromeos {
namespace bluetooth_utils {

bool str2ba(const std::string& in_address, bdaddr_t* out_address) {
  if (!out_address)
    return false;

  memset(out_address, 0, sizeof(*out_address));

  if (in_address.size() != 17)
    return false;

  std::string numbers_only;
  for (int i = 0; i < 6; ++i) {
    numbers_only += in_address.substr(i * 3, 2);
  }

  std::vector<uint8> address_bytes;
  if (base::HexStringToBytes(numbers_only, &address_bytes)) {
    if (address_bytes.size() == 6) {
      for (int i = 0; i < 6; ++i) {
        out_address->b[5 - i] = address_bytes[i];
      }
      return true;
    }
  }

  return false;
}

}  // namespace bluetooth_utils
}  // namespace chromeos