|
18 | 18 |
|
19 | 19 | import java.net.SocketException; |
20 | 20 | import java.net.UnknownHostException; |
| 21 | +import java.nio.charset.StandardCharsets; |
21 | 22 | import java.util.LinkedHashMap; |
22 | 23 | import java.util.List; |
23 | 24 | import java.util.Locale; |
@@ -155,6 +156,27 @@ public boolean isStarted() { |
155 | 156 | return statusClient != null; |
156 | 157 | } |
157 | 158 |
|
| 159 | + private byte[] readOptionBytes(Option option) { |
| 160 | + try { |
| 161 | + return option.encode(); |
| 162 | + } catch (RuntimeException e) { |
| 163 | + logger.debug("{}: Failed to encode option value", thingName, e); |
| 164 | + return EMPTY_BYTE; |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + private String readStringOption(Option option) { |
| 169 | + return new String(readOptionBytes(option), StandardCharsets.UTF_8); |
| 170 | + } |
| 171 | + |
| 172 | + private int readIntegerOption(Option option) { |
| 173 | + int value = 0; |
| 174 | + for (byte b : readOptionBytes(option)) { |
| 175 | + value = (value << 8) | (b & 0xFF); |
| 176 | + } |
| 177 | + return value; |
| 178 | + } |
| 179 | + |
158 | 180 | /** |
159 | 181 | * Process an inbound Response (or mapped Request): decode CoAP options. handle discovery result or status updates |
160 | 182 | * |
@@ -182,7 +204,7 @@ public void processResponse(@Nullable Response response) { |
182 | 204 | // We can't identify device by IP, so we need to check the CoAP header's Global Device ID |
183 | 205 | for (Option opt : options) { |
184 | 206 | if (opt.getNumber() == COIOT_OPTION_GLOBAL_DEVID) { |
185 | | - String devid = opt.getStringValue(); |
| 207 | + String devid = readStringOption(opt); |
186 | 208 | if (devid.contains("#") && profile.device.mac != null) { |
187 | 209 | // Format: <device type>#<mac address>#<coap version> |
188 | 210 | String macid = substringBetween(devid, "#", "#"); |
@@ -226,14 +248,14 @@ public void processResponse(@Nullable Response response) { |
226 | 248 | for (Option opt : options) { |
227 | 249 | switch (opt.getNumber()) { |
228 | 250 | case OptionNumberRegistry.URI_PATH: |
229 | | - uri = COLOIT_URI_BASE + opt.getStringValue(); |
| 251 | + uri = COLOIT_URI_BASE + readStringOption(opt); |
230 | 252 | break; |
231 | 253 | case OptionNumberRegistry.URI_HOST: // ignore |
232 | 254 | break; |
233 | 255 | case OptionNumberRegistry.CONTENT_FORMAT: // ignore |
234 | 256 | break; |
235 | 257 | case COIOT_OPTION_GLOBAL_DEVID: |
236 | | - devId = opt.getStringValue(); |
| 258 | + devId = readStringOption(opt); |
237 | 259 | String sVersion = substringAfterLast(devId, "#"); |
238 | 260 | int iVersion; |
239 | 261 | try { |
@@ -267,11 +289,11 @@ public void processResponse(@Nullable Response response) { |
267 | 289 | case COIOT_OPTION_STATUS_VALIDITY: |
268 | 290 | break; |
269 | 291 | case COIOT_OPTION_STATUS_SERIAL: |
270 | | - serial = opt.getIntegerValue(); |
| 292 | + serial = readIntegerOption(opt); |
271 | 293 | break; |
272 | 294 | default: |
273 | 295 | logger.debug("{} ({}): CoAP option {} with value {} skipped", thingName, devId, opt.getNumber(), |
274 | | - opt.getValue()); |
| 296 | + opt.toValueString()); |
275 | 297 | } |
276 | 298 | } |
277 | 299 |
|
|
0 commit comments