Skip to content

Commit 208b75d

Browse files
committed
thread: adjust uri constants access
- Created CoAPURI class to contain the URIs that can be used in CoAP transmissions. - Updated other files that used the old constants from CoAPTxRequestPacket. -Removed old constants from CoAPTxRequestPacket. Signed-off-by: Héctor González <hector.gonzalez@digi.com>
1 parent 9ff6a9b commit 208b75d

File tree

6 files changed

+98
-62
lines changed

6 files changed

+98
-62
lines changed

examples/communication/coap/SendCoAPDataSample/src/com/digi/xbee/api/sendcoapdata/MainApp.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020

2121
import com.digi.xbee.api.ThreadDevice;
2222
import com.digi.xbee.api.exceptions.XBeeException;
23+
import com.digi.xbee.api.models.CoAPURI;
2324
import com.digi.xbee.api.models.HTTPMethodEnum;
24-
import com.digi.xbee.api.packet.thread.CoAPTxRequestPacket;
2525
import com.digi.xbee.api.utils.HexUtils;
2626

2727
/**
@@ -46,7 +46,7 @@ public class MainApp {
4646

4747
private static final HTTPMethodEnum METHOD = HTTPMethodEnum.PUT;
4848

49-
private static final String URI = CoAPTxRequestPacket.URI_DATA_TRANSMISSION;
49+
private static final String URI = CoAPURI.URI_DATA_TRANSMISSION;
5050

5151
private static final String DATA_TO_SEND = "Hello XBee!";
5252

library/src/main/java/com/digi/xbee/api/ThreadDevice.java

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.digi.xbee.api.exceptions.XBeeDeviceException;
2727
import com.digi.xbee.api.exceptions.XBeeException;
2828
import com.digi.xbee.api.models.AssociationIndicationStatus;
29+
import com.digi.xbee.api.models.CoAPURI;
2930
import com.digi.xbee.api.models.HTTPMethodEnum;
3031
import com.digi.xbee.api.models.IPProtocol;
3132
import com.digi.xbee.api.models.RemoteATCommandOptions;
@@ -346,13 +347,13 @@ protected AssociationIndicationStatus getAssociationIndicationStatus()
346347
* valid URI string and must meet the following criteria. There
347348
* are built-in CoAP URIs:
348349
* <ul>
349-
* <li><b>CoAPTxRequestPacket.URI_DATA_TRANSMISSION:</b> "XB/TX"
350+
* <li><b>CoAPURI.URI_DATA_TRANSMISSION:</b> "XB/TX"
350351
* for data transmissions (HTTP method must be set to PUT)</li>
351-
* <li><b>CoAPTxRequestPacket.URI_AT_COMMAND:</b> "XB/AT" for
352+
* <li><b>CoAPURI.URI_AT_COMMAND:</b> "XB/AT" for
352353
* AT Command operation (HTTP method must be set to PUT or GET).
353354
* After the URI, an AT command needs to be specified, for example:
354-
* CoAPTxRequestPacket.URI_AT_COMMAND + "/NI"</li>
355-
* <li><b>CoAPTxRequestPacket.URI_IO_SAMPLING:</b> "XB/IO" for
355+
* CoAPURI.URI_AT_COMMAND + "/NI"</li>
356+
* <li><b>CoAPURI.URI_IO_SAMPLING:</b> "XB/IO" for
356357
* IO operation (HTTP method must be set to POST)</li>
357358
* </ul>
358359
* @param method HTTP method used for the transmission.
@@ -379,7 +380,7 @@ protected AssociationIndicationStatus getAssociationIndicationStatus()
379380
*/
380381
public byte[] sendCoAPData(Inet6Address ipv6Address, String uri,
381382
HTTPMethodEnum method, byte[] data) throws TimeoutException, IllegalArgumentException, XBeeException {
382-
boolean applyChanges = uri.startsWith(CoAPTxRequestPacket.URI_AT_COMMAND);
383+
boolean applyChanges = uri.startsWith(CoAPURI.URI_AT_COMMAND);
383384
return sendCoAPData(ipv6Address, uri, method, applyChanges, data, false);
384385
}
385386

@@ -402,13 +403,13 @@ public byte[] sendCoAPData(Inet6Address ipv6Address, String uri,
402403
* valid URI string and must meet the following criteria. There
403404
* are built-in CoAP URIs:
404405
* <ul>
405-
* <li><b>CoAPTxRequestPacket.URI_DATA_TRANSMISSION:</b> "XB/TX"
406+
* <li><b>CoAPURI.URI_DATA_TRANSMISSION:</b> "XB/TX"
406407
* for data transmissions (HTTP method must be set to PUT)</li>
407-
* <li><b>CoAPTxRequestPacket.URI_AT_COMMAND:</b> "XB/AT" for
408+
* <li><b>CoAPURI.URI_AT_COMMAND:</b> "XB/AT" for
408409
* AT Command operation (HTTP method must be set to PUT or GET).
409410
* After the URI, an AT command needs to be specified, for example:
410-
* CoAPTxRequestPacket.URI_AT_COMMAND + "/NI"</li>
411-
* <li><b>CoAPTxRequestPacket.URI_IO_SAMPLING:</b> "XB/IO" for
411+
* CoAPURI.URI_AT_COMMAND + "/NI"</li>
412+
* <li><b>CoAPURI.URI_IO_SAMPLING:</b> "XB/IO" for
412413
* IO operation (HTTP method must be set to POST)</li>
413414
* </ul>
414415
* @param method HTTP method used for the transmission.
@@ -455,13 +456,13 @@ public byte[] sendCoAPData(Inet6Address ipv6Address, String uri, HTTPMethodEnum
455456
* valid URI string and must meet the following criteria. There
456457
* are built-in CoAP URIs:
457458
* <ul>
458-
* <li><b>CoAPTxRequestPacket.URI_DATA_TRANSMISSION:</b> "XB/TX"
459+
* <li><b>CoAPURI.URI_DATA_TRANSMISSION:</b> "XB/TX"
459460
* for data transmissions (PUT)</li>
460-
* <li><b>CoAPTxRequestPacket.URI_AT_COMMAND:</b> "XB/AT" for
461+
* <li><b>CoAPURI.URI_AT_COMMAND:</b> "XB/AT" for
461462
* AT Command operation (PUT or GET). After the URI, an AT command
462463
* needs to be specified, for example:
463-
* CoAPTxRequestPacket.URI_AT_COMMAND + "/NI"</li>
464-
* <li><b>CoAPTxRequestPacket.URI_IO_SAMPLING:</b> "XB/IO" for
464+
* CoAPURI.URI_AT_COMMAND + "/NI"</li>
465+
* <li><b>CoAPURI.URI_IO_SAMPLING:</b> "XB/IO" for
465466
* IO operation (POST)</li>
466467
* </ul>
467468
* @param method HTTP method used for the transmission.
@@ -486,7 +487,7 @@ public byte[] sendCoAPData(Inet6Address ipv6Address, String uri, HTTPMethodEnum
486487
*/
487488
public void sendCoAPDataAsync(Inet6Address ipv6Address, String uri,
488489
HTTPMethodEnum method, byte[] data) throws TimeoutException, IllegalArgumentException, XBeeException {
489-
boolean applyChanges = uri.startsWith(CoAPTxRequestPacket.URI_AT_COMMAND);
490+
boolean applyChanges = uri.startsWith(CoAPURI.URI_AT_COMMAND);
490491
sendCoAPData(ipv6Address, uri, method, applyChanges, data, true);
491492
}
492493

@@ -505,13 +506,13 @@ public void sendCoAPDataAsync(Inet6Address ipv6Address, String uri,
505506
* valid URI string and must meet the following criteria. There
506507
* are built-in CoAP URIs:
507508
* <ul>
508-
* <li><b>CoAPTxRequestPacket.URI_DATA_TRANSMISSION:</b> "XB/TX"
509+
* <li><b>CoAPURI.URI_DATA_TRANSMISSION:</b> "XB/TX"
509510
* for data transmissions (PUT)</li>
510-
* <li><b>CoAPTxRequestPacket.URI_AT_COMMAND:</b> "XB/AT" for
511+
* <li><b>CoAPURI.URI_AT_COMMAND:</b> "XB/AT" for
511512
* AT Command operation (PUT or GET). After the URI, an AT command
512513
* needs to be specified, for example:
513-
* CoAPTxRequestPacket.URI_AT_COMMAND + "/NI"</li>
514-
* <li><b>CoAPTxRequestPacket.URI_IO_SAMPLING:</b> "XB/IO" for
514+
* CoAPURI.URI_AT_COMMAND + "/NI"</li>
515+
* <li><b>CoAPURI.URI_IO_SAMPLING:</b> "XB/IO" for
515516
* IO operation (POST)</li>
516517
* </ul>
517518
* @param method HTTP method used for the transmission.
@@ -570,13 +571,13 @@ public void sendCoAPDataAsync(Inet6Address ipv6Address, String uri, HTTPMethodEn
570571
* valid URI string and must meet the following criteria. There
571572
* are built-in CoAP URIs:
572573
* <ul>
573-
* <li><b>CoAPTxRequestPacket.URI_DATA_TRANSMISSION:</b> "XB/TX"
574+
* <li><b>CoAPURI.URI_DATA_TRANSMISSION:</b> "XB/TX"
574575
* for data transmissions (PUT)</li>
575-
* <li><b>CoAPTxRequestPacket.URI_AT_COMMAND:</b> "XB/AT" for
576+
* <li><b>CoAPURI.URI_AT_COMMAND:</b> "XB/AT" for
576577
* AT Command operation (PUT or GET). After the URI, an AT command
577578
* needs to be specified, for example:
578-
* CoAPTxRequestPacket.URI_AT_COMMAND + "/NI"</li>
579-
* <li><b>CoAPTxRequestPacket.URI_IO_SAMPLING:</b> "XB/IO" for
579+
* CoAPURI.URI_AT_COMMAND + "/NI"</li>
580+
* <li><b>CoAPURI.URI_IO_SAMPLING:</b> "XB/IO" for
580581
* IO operation (POST)</li>
581582
* </ul>
582583
* @param method HTTP method used for the transmission.
@@ -617,8 +618,8 @@ private byte[] sendCoAPData(Inet6Address ipv6Address, String uri, HTTPMethodEnum
617618
throw new NullPointerException("HTTP method cannot be null");
618619

619620
// If AT command uri is used but no AT command is specified throw an error.
620-
if (uri.startsWith(CoAPTxRequestPacket.URI_AT_COMMAND)
621-
&& uri.length() < CoAPTxRequestPacket.URI_AT_COMMAND.length() + 3)
621+
if (uri.startsWith(CoAPURI.URI_AT_COMMAND)
622+
&& uri.length() < CoAPURI.URI_AT_COMMAND.length() + 3)
622623
throw new IllegalArgumentException("AT command URI must contain an AT command.");
623624

624625
// Check if device is remote.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Copyright 2017, Digi International Inc.
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public
5+
* License, v. 2.0. If a copy of the MPL was not distributed with this
6+
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
7+
*
8+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15+
*/
16+
package com.digi.xbee.api.models;
17+
18+
/**
19+
* Class containing the different constants that can be used as Uniform Resource
20+
* Identifier (URI) when working with CoAP transmissions.
21+
*
22+
* @since 1.2.1
23+
*/
24+
public class CoAPURI {
25+
26+
/**
27+
* URI for data transmissions (PUT).
28+
*/
29+
public static final String URI_DATA_TRANSMISSION = "XB/TX";
30+
31+
/**
32+
* URI for AT Command operation (PUT or GET).
33+
*/
34+
public static final String URI_AT_COMMAND = "XB/AT";
35+
36+
/**
37+
* URI for IO operation (POST).
38+
*/
39+
public static final String URI_IO_SAMPLING = "XB/IO";
40+
}

library/src/main/java/com/digi/xbee/api/packet/thread/CoAPTxRequestPacket.java

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.slf4j.Logger;
2626
import org.slf4j.LoggerFactory;
2727

28+
import com.digi.xbee.api.models.CoAPURI;
2829
import com.digi.xbee.api.models.HTTPMethodEnum;
2930
import com.digi.xbee.api.models.RemoteATCommandOptions;
3031
import com.digi.xbee.api.packet.APIFrameType;
@@ -45,9 +46,10 @@
4546
* <p>The method indicates the HTTP function used for the transmission. It is
4647
* specified by the {@link HTTPMethodEnum} enumerator.</p>
4748
*
48-
* <p>The URI field is a string that must be {@value #URI_DATA_TRANSMISSION} for
49-
* data transmission (PUT), {@value #URI_AT_COMMAND} for AT Command operations
50-
* (PUT or GET) or {@value #URI_IO_SAMPLING} for IO operation (POST).</p>
49+
* <p>The URI field is a string that must be {@value CoAPURI#URI_DATA_TRANSMISSION}
50+
* for data transmission (PUT), {@value CoAPURI#URI_AT_COMMAND} for AT Command
51+
* operations (PUT or GET) or {@value CoAPURI#URI_IO_SAMPLING} for IO
52+
* operation (POST).</p>
5153
*
5254
* <p>The packet also includes an optional payload. For data transmission, it
5355
* should contain the data to send; for AT Command operations, empty to query
@@ -61,15 +63,6 @@
6163
public class CoAPTxRequestPacket extends XBeeAPIPacket {
6264

6365
// Constants.
64-
/** URI for data transmissions (PUT). */
65-
public static final String URI_DATA_TRANSMISSION = "XB/TX";
66-
67-
/** URI for AT Command operation (PUT or GET). */
68-
public static final String URI_AT_COMMAND = "XB/AT";
69-
70-
/** URI for IO operation (POST). */
71-
public static final String URI_IO_SAMPLING = "XB/IO";
72-
7366
private static final int MIN_API_PAYLOAD_LENGTH = 26; /* 1 (Frame type) + 1 (frame ID) + 1 (transmit options) +
7467
1 (RESTful method) + 16 (dest address) + 1 (URI length) + 5 (URI) */
7568

@@ -176,8 +169,8 @@ public static CoAPTxRequestPacket createPacket(byte[] payload) {
176169
*
177170
* @throws IllegalArgumentException if {@code frameID < 0} or
178171
* if {@code frameID > 255} or
179-
* if {@code uri contains }{@value #URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE} or {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_APPLY_CHANGES}
180-
* if {@code uri does not contain }{@value #URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE}.
172+
* if {@code uri contains }{@value CoAPURI#URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE} or {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_APPLY_CHANGES}
173+
* if {@code uri does not contain }{@value CoAPURI#URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE}.
181174
* @throws NullPointerException if {@code method == null} or
182175
* if {@code destAddress == null} or
183176
* if {@code uri == null}.
@@ -192,8 +185,8 @@ public CoAPTxRequestPacket(int frameID, int transmitOptions, HTTPMethodEnum meth
192185

193186
if (frameID < 0 || frameID > 255)
194187
throw new IllegalArgumentException(ERROR_FRAME_ID_ILLEGAL);
195-
if ((uri != null && !uri.contains(URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE)
196-
|| (uri != null && uri.contains(URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE && transmitOptions != RemoteATCommandOptions.OPTION_APPLY_CHANGES))
188+
if ((uri != null && !uri.contains(CoAPURI.URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE)
189+
|| (uri != null && uri.contains(CoAPURI.URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE && transmitOptions != RemoteATCommandOptions.OPTION_APPLY_CHANGES))
197190
throw new IllegalArgumentException(ERROR_OPTIONS_INVALID);
198191
if (method == null)
199192
throw new NullPointerException(ERROR_METHOD_NULL);
@@ -253,17 +246,17 @@ public boolean isBroadcast() {
253246
/**
254247
* Sets the transmit options bifield.
255248
*
256-
* @param options The transmit options bitfield.
249+
* @param transmitOptions The transmit options bitfield.
257250
*
258-
* @throws IllegalArgumentException if {@code uri contains }{@value #URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE} or {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_APPLY_CHANGES}
259-
* if {@code uri does not contain }{@value #URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE}.
251+
* @throws IllegalArgumentException if {@code uri contains }{@value CoAPURI#URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE} or {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_APPLY_CHANGES}
252+
* if {@code uri does not contain }{@value CoAPURI#URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE}.
260253
*
261254
* @see #getTransmitOptions()
262255
* @see com.digi.xbee.api.models.RemoteATCommandOptions
263256
*/
264257
public void setTransmitOptions(int transmitOptions) {
265-
if ((uri != null && !uri.contains(URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE)
266-
|| (uri != null && uri.contains(URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE && transmitOptions != RemoteATCommandOptions.OPTION_APPLY_CHANGES))
258+
if ((uri != null && !uri.contains(CoAPURI.URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE)
259+
|| (uri != null && uri.contains(CoAPURI.URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE && transmitOptions != RemoteATCommandOptions.OPTION_APPLY_CHANGES))
267260
throw new IllegalArgumentException(ERROR_OPTIONS_INVALID);
268261

269262
this.transmitOptions = transmitOptions;

0 commit comments

Comments
 (0)