• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 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 
17 package com.android.libraries.testing.deviceshadower.internal.utils;
18 
19 import android.bluetooth.BluetoothAdapter;
20 
21 import com.android.internal.annotations.VisibleForTesting;
22 
23 import java.util.Locale;
24 
25 import javax.annotation.concurrent.GuardedBy;
26 
27 /**
28  * A class which generates and converts valid Bluetooth MAC addresses.
29  */
30 public class MacAddressGenerator {
31 
32     @GuardedBy("MacAddressGenerator.class")
33     private static MacAddressGenerator sInstance = new MacAddressGenerator();
34 
35     @VisibleForTesting
setInstanceForTest(MacAddressGenerator generator)36     public static synchronized void setInstanceForTest(MacAddressGenerator generator) {
37         sInstance = generator;
38     }
39 
get()40     public static synchronized MacAddressGenerator get() {
41         return sInstance;
42     }
43 
44     private long mLastAddress = 0x0L;
45 
MacAddressGenerator()46     private MacAddressGenerator() {
47     }
48 
generateMacAddress()49     public String generateMacAddress() {
50         byte[] bytes = generateMacAddressBytes();
51         return convertByteMacAddress(bytes);
52     }
53 
generateMacAddressBytes()54     public byte[] generateMacAddressBytes() {
55         long addr = mLastAddress++;
56         byte[] bytes = new byte[6];
57         for (int i = 5; i >= 0; i--) {
58             bytes[i] = (byte) (addr & 0xFF);
59             addr = addr >> 8;
60         }
61         return bytes;
62     }
63 
convertStringMacAddress(String address)64     public static byte[] convertStringMacAddress(String address) {
65         if (!BluetoothAdapter.checkBluetoothAddress(address)) {
66             throw new IllegalArgumentException("Not a valid bluetooth mac hex string: " + address);
67         }
68         byte[] bytes = new byte[6];
69         String[] macValues = address.split(":");
70         for (int i = 0; i < bytes.length; i++) {
71             bytes[i] = Integer.decode("0x" + macValues[i]).byteValue();
72         }
73         return bytes;
74     }
75 
convertByteMacAddress(byte[] address)76     public static String convertByteMacAddress(byte[] address) {
77         if (address == null || address.length != 6) {
78             throw new IllegalArgumentException("Bluetooth address must have 6 bytes");
79         }
80         return String.format(Locale.US, "%02X:%02X:%02X:%02X:%02X:%02X",
81                 address[0], address[1], address[2], address[3], address[4], address[5]);
82     }
83 }
84