OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include <bluetooth/bluetooth.h> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/string_number_conversions.h" | |
13 #include "base/string_util.h" | |
14 | |
15 namespace { | |
16 static const char* kCommonUuidPostfix = "-0000-1000-8000-00805f9b34fb"; | |
17 static const char* kCommonUuidPrefix = "0000"; | |
18 static const int kUuidSize = 36; | |
19 } // namespace | |
20 | |
21 namespace chromeos { | |
22 namespace bluetooth_utils { | |
23 | |
24 bool str2ba(const std::string& in_address, bdaddr_t* out_address) { | |
25 if (!out_address) | |
26 return false; | |
27 | |
28 memset(out_address, 0, sizeof(*out_address)); | |
29 | |
30 if (in_address.size() != 17) | |
31 return false; | |
32 | |
33 std::string numbers_only; | |
34 for (int i = 0; i < 6; ++i) { | |
35 numbers_only += in_address.substr(i * 3, 2); | |
36 } | |
37 | |
38 std::vector<uint8> address_bytes; | |
39 if (base::HexStringToBytes(numbers_only, &address_bytes)) { | |
40 if (address_bytes.size() == 6) { | |
41 for (int i = 0; i < 6; ++i) { | |
42 out_address->b[5 - i] = address_bytes[i]; | |
43 } | |
44 return true; | |
45 } | |
46 } | |
47 | |
48 return false; | |
49 } | |
50 | |
51 std::string CanonicalUuid(std::string uuid) { | |
52 if (uuid.empty()) | |
53 return ""; | |
54 | |
55 if (uuid.size() < 11 && uuid.find("0x") == 0) | |
56 uuid = uuid.substr(2); | |
57 | |
58 if (!(uuid.size() == 4 || uuid.size() == 8 || uuid.size() == 36)) | |
59 return ""; | |
60 | |
61 if (uuid.size() == 4 || uuid.size() == 8) { | |
62 for (size_t i = 0; i < uuid.size(); ++i) { | |
63 if (!IsHexDigit(uuid[i])) | |
64 return ""; | |
65 } | |
66 | |
67 if (uuid.size() == 4) | |
68 return kCommonUuidPrefix + uuid + kCommonUuidPostfix; | |
69 | |
70 return uuid + kCommonUuidPostfix; | |
71 } | |
72 | |
73 std::string uuid_result(uuid); | |
74 for (int i = 0; i < kUuidSize; ++i) { | |
75 if (i == 8 || i == 13 || i == 18 || i == 23) { | |
76 if (uuid[i] != '-') | |
77 return ""; | |
78 } else { | |
79 if (!IsHexDigit(uuid[i])) | |
80 return ""; | |
81 uuid_result[i] = tolower(uuid[i]); | |
82 } | |
83 } | |
84 return uuid_result; | |
85 } | |
86 | |
87 } // namespace bluetooth_utils | |
88 } // namespace chromeos | |
OLD | NEW |