]> git.basschouten.com Git - openhab-addons.git/blob
bfce15f8014186481d5428a497830f55fed0147e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.lifx.internal.fields;
14
15 import java.io.ByteArrayInputStream;
16 import java.io.IOException;
17 import java.nio.ByteBuffer;
18 import java.util.Arrays;
19 import java.util.LinkedList;
20 import java.util.List;
21 import java.util.Objects;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * @author Tim Buckley
31  * @author Karel Goderis
32  */
33 @NonNullByDefault
34 public class MACAddress {
35
36     public static final MACAddress BROADCAST_ADDRESS = new MACAddress("000000000000", true);
37
38     private final Logger logger = LoggerFactory.getLogger(MACAddress.class);
39
40     private ByteBuffer bytes;
41     private String hex = "";
42
43     public ByteBuffer getBytes() {
44         return bytes;
45     }
46
47     public String getHex() {
48         return hex;
49     }
50
51     public MACAddress(ByteBuffer bytes) {
52         this.bytes = bytes;
53
54         createHex();
55     }
56
57     public MACAddress(String string, boolean isHex) {
58         if (!isHex) {
59             this.bytes = ByteBuffer.wrap(string.getBytes());
60             createHex();
61         } else {
62             this.bytes = ByteBuffer.wrap(parseHexBinary(string));
63
64             try {
65                 formatHex(string, 2, ":");
66             } catch (IOException e) {
67                 logger.error("An exception occurred while formatting an HEX string : '{}'", e.getMessage());
68             }
69         }
70     }
71
72     private byte[] parseHexBinary(String s) {
73         int len = s.length();
74         byte[] data = new byte[len / 2];
75         for (int i = 0; i < len; i += 2) {
76             data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
77         }
78         return data;
79     }
80
81     public MACAddress() {
82         this(ByteBuffer.allocate(6));
83     }
84
85     private void createHex() {
86         bytes.rewind();
87
88         List<String> byteStrings = new LinkedList<>();
89         while (bytes.hasRemaining()) {
90             byteStrings.add(String.format("%02X", bytes.get()));
91         }
92
93         hex = StringUtils.join(byteStrings, ':');
94
95         bytes.rewind();
96     }
97
98     public String getAsLabel() {
99         bytes.rewind();
100
101         StringBuilder hex = new StringBuilder();
102         while (bytes.hasRemaining()) {
103             hex.append(String.format("%02X", bytes.get()));
104         }
105
106         bytes.rewind();
107
108         return hex.toString();
109     }
110
111     private void formatHex(String original, int length, String separator) throws IOException {
112         ByteArrayInputStream bis = new ByteArrayInputStream(original.getBytes());
113         byte[] buffer = new byte[length];
114         String result = "";
115         while (bis.read(buffer) > 0) {
116             for (byte b : buffer) {
117                 result += (char) b;
118             }
119             Arrays.fill(buffer, (byte) 0);
120             result += separator;
121         }
122
123         hex = StringUtils.left(result, result.length() - 1);
124     }
125
126     @Override
127     public int hashCode() {
128         int hash = 7;
129         hash = 97 * hash + Objects.hashCode(this.hex);
130         return hash;
131     }
132
133     @Override
134     public boolean equals(@Nullable Object obj) {
135         if (obj == null) {
136             return false;
137         }
138
139         if (getClass() != obj.getClass()) {
140             return false;
141         }
142
143         final MACAddress other = (MACAddress) obj;
144         if (!this.hex.equalsIgnoreCase(other.hex)) {
145             return false;
146         }
147
148         return true;
149     }
150 }