MeshUtils.java
4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* Copyright (C) 2015 The Telink Bluetooth Light Project
*
*/
package com.telink.util;
import java.security.SecureRandom;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public final class MeshUtils {
public static final int GROUP_ADDRESS_MIN = 0x8001;
public static final int GROUP_ADDRESS_MAX = 0x80FF;
public static final int DEVICE_ADDRESS_MIN = 0x0001;
public static final int DEVICE_ADDRESS_MAX = 0x00FF;
public static final String CHARS = "123456789aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ+-*/<>/?!@#$%^&;'[]{}|,.";
private static final MeshAddressComparator MESH_ADDRESS_COMPARATOR = new MeshAddressComparator();
private static int GroupAddress = GROUP_ADDRESS_MIN;
private static int DeviceAddress = DEVICE_ADDRESS_MIN;
private static SecureRandom rng;
private MeshUtils() {
}
public static byte[] generateRandom(int length) {
byte[] data = new byte[length];
synchronized (MeshUtils.class) {
if (rng == null) {
rng = new SecureRandom();
}
}
rng.nextBytes(data);
return data;
}
public static byte[] generateChars(int length) {
int charLen = CHARS.length() - 1;
int charAt;
byte[] data = new byte[length];
for (int i = 0; i < length; i++) {
charAt = (int) Math.round(Math.random() * charLen);
data[i] = (byte) CHARS.charAt(charAt);
}
return data;
}
synchronized public static int allocGroupAddress(List<Integer> allocAddress) {
if (allocAddress == null || allocAddress.isEmpty()) {
GroupAddress = GROUP_ADDRESS_MIN;
if ((GroupAddress + 1) > GROUP_ADDRESS_MAX)
return -1;
return GroupAddress++;
}
int count = allocAddress.size();
if (count > (GROUP_ADDRESS_MAX - GROUP_ADDRESS_MIN))
return -1;
Collections.sort(allocAddress, MESH_ADDRESS_COMPARATOR);
Integer last = allocAddress.get(count - 1);
if ((last + 1) <= GROUP_ADDRESS_MAX)
return last + 1;
Integer prev = null;
Integer next;
int i = 0;
while (i < count) {
if (prev == null) {
prev = allocAddress.get(i);
i = 1;
continue;
}
next = allocAddress.get(i);
if ((prev + 1) != next && prev != GROUP_ADDRESS_MAX) {
return prev + 1;
}
if ((i + 1) >= count) {
if (next >= GROUP_ADDRESS_MAX) {
GroupAddress = GROUP_ADDRESS_MIN;
return GroupAddress++;
} else {
return next + 1;
}
}
prev = next;
i++;
}
return -1;
}
synchronized public static int allocDeviceAddress(List<Integer> allocAddress) {
if (allocAddress == null || allocAddress.isEmpty()) {
DeviceAddress = DEVICE_ADDRESS_MIN;
if ((DeviceAddress + 1) > DEVICE_ADDRESS_MAX)
return -1;
return DeviceAddress++;
}
int count = allocAddress.size();
if (count > (DEVICE_ADDRESS_MAX - DEVICE_ADDRESS_MIN))
return -1;
Collections.sort(allocAddress, MESH_ADDRESS_COMPARATOR);
Integer last = allocAddress.get(count - 1);
if ((last + 1) <= DEVICE_ADDRESS_MAX)
return last + 1;
Integer prev = null;
Integer next;
int i = 0;
while (i < count) {
if (prev == null) {
prev = allocAddress.get(i);
i = 1;
continue;
}
next = allocAddress.get(i);
if ((prev + 1) != next && prev != DEVICE_ADDRESS_MAX) {
return prev + 1;
}
if ((i + 1) >= count) {
if (next >= DEVICE_ADDRESS_MAX) {
DeviceAddress = DEVICE_ADDRESS_MIN;
return DeviceAddress++;
} else {
return next + 1;
}
}
prev = next;
i++;
}
return -1;
}
private static class MeshAddressComparator implements Comparator<Integer> {
@Override
public int compare(Integer lhs, Integer rhs) {
if (lhs > rhs)
return 1;
if (lhs < rhs)
return -1;
return 0;
}
}
}