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 #pragma once
17 
18 #include "hci/address_with_type.h"
19 
20 #include "stack/include/bt_types.h"
21 
22 namespace bluetooth {
23 
ToRawAddress(const hci::Address & address)24 inline RawAddress ToRawAddress(const hci::Address& address) {
25   RawAddress ret;
26   ret.address[0] = address.address[5];
27   ret.address[1] = address.address[4];
28   ret.address[2] = address.address[3];
29   ret.address[3] = address.address[2];
30   ret.address[4] = address.address[1];
31   ret.address[5] = address.address[0];
32   return ret;
33 }
34 
ToGdAddress(const RawAddress & address)35 inline hci::Address ToGdAddress(const RawAddress& address) {
36   hci::Address ret;
37   ret.address[0] = address.address[5];
38   ret.address[1] = address.address[4];
39   ret.address[2] = address.address[3];
40   ret.address[3] = address.address[2];
41   ret.address[4] = address.address[1];
42   ret.address[5] = address.address[0];
43   return ret;
44 }
45 
ToAddressWithType(const RawAddress & legacy_address,tBLE_ADDR_TYPE legacy_type)46 inline hci::AddressWithType ToAddressWithType(const RawAddress& legacy_address,
47                                        tBLE_ADDR_TYPE legacy_type) {
48   hci::Address address = ToGdAddress(legacy_address);
49 
50   hci::AddressType type;
51   if (legacy_type == BLE_ADDR_PUBLIC)
52     type = hci::AddressType::PUBLIC_DEVICE_ADDRESS;
53   else if (legacy_type == BLE_ADDR_RANDOM)
54     type = hci::AddressType::RANDOM_DEVICE_ADDRESS;
55   else if (legacy_type == BLE_ADDR_PUBLIC_ID)
56     type = hci::AddressType::PUBLIC_IDENTITY_ADDRESS;
57   else if (legacy_type == BLE_ADDR_RANDOM_ID)
58     type = hci::AddressType::RANDOM_IDENTITY_ADDRESS;
59   else {
60     LOG_ALWAYS_FATAL("Bad address type %02x", legacy_type);
61     return hci::AddressWithType{address,
62                                 hci::AddressType::PUBLIC_DEVICE_ADDRESS};
63   }
64 
65   return hci::AddressWithType{address, type};
66 }
67 }  // namespace bluetooth
68