]> git.basschouten.com Git - openhab-addons.git/blob
977172eaebae3bb1552b83bc76d77e6cfb45554a
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.bluetooth.bluegiga.internal.eir;
14
15 import java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 /**
23  * This class processes the Extended Inquiry Response data used in the BLE advertisement frame
24  *
25  * @author Chris Jackson - Initial contribution
26  *
27  */
28 @NonNullByDefault
29 public class EirPacket {
30     private Map<EirDataType, Object> records = new HashMap<>();
31
32     public EirPacket(int @Nullable [] data) {
33         if (data == null || data.length == 0) {
34             return;
35         }
36
37         for (int cnt = 0; cnt < data.length;) {
38             if (data[cnt] == 0) {
39                 break;
40             }
41
42             int[] rawRecord = Arrays.copyOfRange(data, cnt + 1, cnt + data[cnt] + 1);
43             EirRecord record = new EirRecord(rawRecord);
44
45             cnt += data[cnt] + 1;
46
47             records.put(record.getType(), record.getRecord());
48         }
49     }
50
51     /**
52      * Returns a map of all records decoded in the packet
53      *
54      * @return {@link Map} of {@link EirDataType} to {@link Object}
55      */
56     public Map<EirDataType, Object> getRecords() {
57         return records;
58     }
59
60     /**
61      * Returns the specified record decoded in the packet or null if the record is not found
62      *
63      * @param recordType the requested {@link EirDataType}
64      * @return {@link Map} of to {@link Object}
65      */
66     public @Nullable Object getRecord(EirDataType recordType) {
67         return records.get(recordType);
68     }
69
70     @Override
71     public String toString() {
72         final StringBuilder builder = new StringBuilder();
73         builder.append("EirPacket [records=");
74         builder.append(records);
75         builder.append(']');
76         return builder.toString();
77     }
78 }