summaryrefslogtreecommitdiff
path: root/core/java
diff options
context:
space:
mode:
authorHugo Benichi <hugobenichi@google.com>2017-11-08 06:30:04 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2017-11-08 06:30:04 +0000
commit925349ac8f2faa65cc4f8d2d84938aed54b95cda (patch)
treed8325f4d04074f56537e380bd44729edebf467bd /core/java
parentee56b4a65c4fcaf512137a8c97410cee34661630 (diff)
parent59c8e423a871227b7280fc849743182679c180fe (diff)
Merge "Define MacAddress class"
Diffstat (limited to 'core/java')
-rw-r--r--core/java/android/net/MacAddress.java62
1 files changed, 62 insertions, 0 deletions
diff --git a/core/java/android/net/MacAddress.java b/core/java/android/net/MacAddress.java
new file mode 100644
index 000000000000..e76d17d0535b
--- /dev/null
+++ b/core/java/android/net/MacAddress.java
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.net;
+
+import com.android.internal.annotations.VisibleForTesting;
+
+import java.util.Arrays;
+
+/**
+ * @hide
+ */
+public final class MacAddress {
+
+ // TODO: add isLocallyAssigned().
+ // TODO: add getRandomAddress() factory method.
+
+ private static final int ETHER_ADDR_LEN = 6;
+ private static final byte FF = (byte) 0xff;
+ @VisibleForTesting
+ static final byte[] ETHER_ADDR_BROADCAST = { FF, FF, FF, FF, FF, FF };
+
+ public enum MacAddressType {
+ UNICAST,
+ MULTICAST,
+ BROADCAST;
+ }
+
+ /** Return true if the given byte array is not null and has the length of a mac address. */
+ public static boolean isMacAddress(byte[] addr) {
+ return addr != null && addr.length == ETHER_ADDR_LEN;
+ }
+
+ /**
+ * Return the MacAddressType of the mac address represented by the given byte array,
+ * or null if the given byte array does not represent an mac address. */
+ public static MacAddressType macAddressType(byte[] addr) {
+ if (!isMacAddress(addr)) {
+ return null;
+ }
+ if (Arrays.equals(addr, ETHER_ADDR_BROADCAST)) {
+ return MacAddressType.BROADCAST;
+ }
+ if ((addr[0] & 0x01) == 1) {
+ return MacAddressType.MULTICAST;
+ }
+ return MacAddressType.UNICAST;
+ }
+}