Skip to content

Commit

Permalink
thread: adjust uri constants access
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
hgonzaleDigi committed Aug 30, 2017
1 parent 9ff6a9b commit 208b75d
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@

import com.digi.xbee.api.ThreadDevice;
import com.digi.xbee.api.exceptions.XBeeException;
import com.digi.xbee.api.models.CoAPURI;
import com.digi.xbee.api.models.HTTPMethodEnum;
import com.digi.xbee.api.packet.thread.CoAPTxRequestPacket;
import com.digi.xbee.api.utils.HexUtils;

/**
Expand All @@ -46,7 +46,7 @@ public class MainApp {

private static final HTTPMethodEnum METHOD = HTTPMethodEnum.PUT;

private static final String URI = CoAPTxRequestPacket.URI_DATA_TRANSMISSION;
private static final String URI = CoAPURI.URI_DATA_TRANSMISSION;

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

Expand Down
49 changes: 25 additions & 24 deletions library/src/main/java/com/digi/xbee/api/ThreadDevice.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.digi.xbee.api.exceptions.XBeeDeviceException;
import com.digi.xbee.api.exceptions.XBeeException;
import com.digi.xbee.api.models.AssociationIndicationStatus;
import com.digi.xbee.api.models.CoAPURI;
import com.digi.xbee.api.models.HTTPMethodEnum;
import com.digi.xbee.api.models.IPProtocol;
import com.digi.xbee.api.models.RemoteATCommandOptions;
Expand Down Expand Up @@ -346,13 +347,13 @@ protected AssociationIndicationStatus getAssociationIndicationStatus()
* valid URI string and must meet the following criteria. There
* are built-in CoAP URIs:
* <ul>
* <li><b>CoAPTxRequestPacket.URI_DATA_TRANSMISSION:</b> "XB/TX"
* <li><b>CoAPURI.URI_DATA_TRANSMISSION:</b> "XB/TX"
* for data transmissions (HTTP method must be set to PUT)</li>
* <li><b>CoAPTxRequestPacket.URI_AT_COMMAND:</b> "XB/AT" for
* <li><b>CoAPURI.URI_AT_COMMAND:</b> "XB/AT" for
* AT Command operation (HTTP method must be set to PUT or GET).
* After the URI, an AT command needs to be specified, for example:
* CoAPTxRequestPacket.URI_AT_COMMAND + "/NI"</li>
* <li><b>CoAPTxRequestPacket.URI_IO_SAMPLING:</b> "XB/IO" for
* CoAPURI.URI_AT_COMMAND + "/NI"</li>
* <li><b>CoAPURI.URI_IO_SAMPLING:</b> "XB/IO" for
* IO operation (HTTP method must be set to POST)</li>
* </ul>
* @param method HTTP method used for the transmission.
Expand All @@ -379,7 +380,7 @@ protected AssociationIndicationStatus getAssociationIndicationStatus()
*/
public byte[] sendCoAPData(Inet6Address ipv6Address, String uri,
HTTPMethodEnum method, byte[] data) throws TimeoutException, IllegalArgumentException, XBeeException {
boolean applyChanges = uri.startsWith(CoAPTxRequestPacket.URI_AT_COMMAND);
boolean applyChanges = uri.startsWith(CoAPURI.URI_AT_COMMAND);
return sendCoAPData(ipv6Address, uri, method, applyChanges, data, false);
}

Expand All @@ -402,13 +403,13 @@ public byte[] sendCoAPData(Inet6Address ipv6Address, String uri,
* valid URI string and must meet the following criteria. There
* are built-in CoAP URIs:
* <ul>
* <li><b>CoAPTxRequestPacket.URI_DATA_TRANSMISSION:</b> "XB/TX"
* <li><b>CoAPURI.URI_DATA_TRANSMISSION:</b> "XB/TX"
* for data transmissions (HTTP method must be set to PUT)</li>
* <li><b>CoAPTxRequestPacket.URI_AT_COMMAND:</b> "XB/AT" for
* <li><b>CoAPURI.URI_AT_COMMAND:</b> "XB/AT" for
* AT Command operation (HTTP method must be set to PUT or GET).
* After the URI, an AT command needs to be specified, for example:
* CoAPTxRequestPacket.URI_AT_COMMAND + "/NI"</li>
* <li><b>CoAPTxRequestPacket.URI_IO_SAMPLING:</b> "XB/IO" for
* CoAPURI.URI_AT_COMMAND + "/NI"</li>
* <li><b>CoAPURI.URI_IO_SAMPLING:</b> "XB/IO" for
* IO operation (HTTP method must be set to POST)</li>
* </ul>
* @param method HTTP method used for the transmission.
Expand Down Expand Up @@ -455,13 +456,13 @@ public byte[] sendCoAPData(Inet6Address ipv6Address, String uri, HTTPMethodEnum
* valid URI string and must meet the following criteria. There
* are built-in CoAP URIs:
* <ul>
* <li><b>CoAPTxRequestPacket.URI_DATA_TRANSMISSION:</b> "XB/TX"
* <li><b>CoAPURI.URI_DATA_TRANSMISSION:</b> "XB/TX"
* for data transmissions (PUT)</li>
* <li><b>CoAPTxRequestPacket.URI_AT_COMMAND:</b> "XB/AT" for
* <li><b>CoAPURI.URI_AT_COMMAND:</b> "XB/AT" for
* AT Command operation (PUT or GET). After the URI, an AT command
* needs to be specified, for example:
* CoAPTxRequestPacket.URI_AT_COMMAND + "/NI"</li>
* <li><b>CoAPTxRequestPacket.URI_IO_SAMPLING:</b> "XB/IO" for
* CoAPURI.URI_AT_COMMAND + "/NI"</li>
* <li><b>CoAPURI.URI_IO_SAMPLING:</b> "XB/IO" for
* IO operation (POST)</li>
* </ul>
* @param method HTTP method used for the transmission.
Expand All @@ -486,7 +487,7 @@ public byte[] sendCoAPData(Inet6Address ipv6Address, String uri, HTTPMethodEnum
*/
public void sendCoAPDataAsync(Inet6Address ipv6Address, String uri,
HTTPMethodEnum method, byte[] data) throws TimeoutException, IllegalArgumentException, XBeeException {
boolean applyChanges = uri.startsWith(CoAPTxRequestPacket.URI_AT_COMMAND);
boolean applyChanges = uri.startsWith(CoAPURI.URI_AT_COMMAND);
sendCoAPData(ipv6Address, uri, method, applyChanges, data, true);
}

Expand All @@ -505,13 +506,13 @@ public void sendCoAPDataAsync(Inet6Address ipv6Address, String uri,
* valid URI string and must meet the following criteria. There
* are built-in CoAP URIs:
* <ul>
* <li><b>CoAPTxRequestPacket.URI_DATA_TRANSMISSION:</b> "XB/TX"
* <li><b>CoAPURI.URI_DATA_TRANSMISSION:</b> "XB/TX"
* for data transmissions (PUT)</li>
* <li><b>CoAPTxRequestPacket.URI_AT_COMMAND:</b> "XB/AT" for
* <li><b>CoAPURI.URI_AT_COMMAND:</b> "XB/AT" for
* AT Command operation (PUT or GET). After the URI, an AT command
* needs to be specified, for example:
* CoAPTxRequestPacket.URI_AT_COMMAND + "/NI"</li>
* <li><b>CoAPTxRequestPacket.URI_IO_SAMPLING:</b> "XB/IO" for
* CoAPURI.URI_AT_COMMAND + "/NI"</li>
* <li><b>CoAPURI.URI_IO_SAMPLING:</b> "XB/IO" for
* IO operation (POST)</li>
* </ul>
* @param method HTTP method used for the transmission.
Expand Down Expand Up @@ -570,13 +571,13 @@ public void sendCoAPDataAsync(Inet6Address ipv6Address, String uri, HTTPMethodEn
* valid URI string and must meet the following criteria. There
* are built-in CoAP URIs:
* <ul>
* <li><b>CoAPTxRequestPacket.URI_DATA_TRANSMISSION:</b> "XB/TX"
* <li><b>CoAPURI.URI_DATA_TRANSMISSION:</b> "XB/TX"
* for data transmissions (PUT)</li>
* <li><b>CoAPTxRequestPacket.URI_AT_COMMAND:</b> "XB/AT" for
* <li><b>CoAPURI.URI_AT_COMMAND:</b> "XB/AT" for
* AT Command operation (PUT or GET). After the URI, an AT command
* needs to be specified, for example:
* CoAPTxRequestPacket.URI_AT_COMMAND + "/NI"</li>
* <li><b>CoAPTxRequestPacket.URI_IO_SAMPLING:</b> "XB/IO" for
* CoAPURI.URI_AT_COMMAND + "/NI"</li>
* <li><b>CoAPURI.URI_IO_SAMPLING:</b> "XB/IO" for
* IO operation (POST)</li>
* </ul>
* @param method HTTP method used for the transmission.
Expand Down Expand Up @@ -617,8 +618,8 @@ private byte[] sendCoAPData(Inet6Address ipv6Address, String uri, HTTPMethodEnum
throw new NullPointerException("HTTP method cannot be null");

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

// Check if device is remote.
Expand Down
40 changes: 40 additions & 0 deletions library/src/main/java/com/digi/xbee/api/models/CoAPURI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2017, Digi International Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package com.digi.xbee.api.models;

/**
* Class containing the different constants that can be used as Uniform Resource
* Identifier (URI) when working with CoAP transmissions.
*
* @since 1.2.1
*/
public class CoAPURI {

/**
* URI for data transmissions (PUT).
*/
public static final String URI_DATA_TRANSMISSION = "XB/TX";

/**
* URI for AT Command operation (PUT or GET).
*/
public static final String URI_AT_COMMAND = "XB/AT";

/**
* URI for IO operation (POST).
*/
public static final String URI_IO_SAMPLING = "XB/IO";
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

// Constants.
/** URI for data transmissions (PUT). */
public static final String URI_DATA_TRANSMISSION = "XB/TX";

/** URI for AT Command operation (PUT or GET). */
public static final String URI_AT_COMMAND = "XB/AT";

/** URI for IO operation (POST). */
public static final String URI_IO_SAMPLING = "XB/IO";

private static final int MIN_API_PAYLOAD_LENGTH = 26; /* 1 (Frame type) + 1 (frame ID) + 1 (transmit options) +
1 (RESTful method) + 16 (dest address) + 1 (URI length) + 5 (URI) */

Expand Down Expand Up @@ -176,8 +169,8 @@ public static CoAPTxRequestPacket createPacket(byte[] payload) {
*
* @throws IllegalArgumentException if {@code frameID < 0} or
* if {@code frameID > 255} or
* if {@code uri contains }{@value #URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE} or {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_APPLY_CHANGES}
* if {@code uri does not contain }{@value #URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE}.
* if {@code uri contains }{@value CoAPURI#URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE} or {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_APPLY_CHANGES}
* if {@code uri does not contain }{@value CoAPURI#URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE}.
* @throws NullPointerException if {@code method == null} or
* if {@code destAddress == null} or
* if {@code uri == null}.
Expand All @@ -192,8 +185,8 @@ public CoAPTxRequestPacket(int frameID, int transmitOptions, HTTPMethodEnum meth

if (frameID < 0 || frameID > 255)
throw new IllegalArgumentException(ERROR_FRAME_ID_ILLEGAL);
if ((uri != null && !uri.contains(URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE)
|| (uri != null && uri.contains(URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE && transmitOptions != RemoteATCommandOptions.OPTION_APPLY_CHANGES))
if ((uri != null && !uri.contains(CoAPURI.URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE)
|| (uri != null && uri.contains(CoAPURI.URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE && transmitOptions != RemoteATCommandOptions.OPTION_APPLY_CHANGES))
throw new IllegalArgumentException(ERROR_OPTIONS_INVALID);
if (method == null)
throw new NullPointerException(ERROR_METHOD_NULL);
Expand Down Expand Up @@ -253,17 +246,17 @@ public boolean isBroadcast() {
/**
* Sets the transmit options bifield.
*
* @param options The transmit options bitfield.
* @param transmitOptions The transmit options bitfield.
*
* @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}
* if {@code uri does not contain }{@value #URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE}.
* @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}
* if {@code uri does not contain }{@value CoAPURI#URI_AT_COMMAND} and {@code transmitOptions != }{@value RemoteATCommandOptions#OPTION_NONE}.
*
* @see #getTransmitOptions()
* @see com.digi.xbee.api.models.RemoteATCommandOptions
*/
public void setTransmitOptions(int transmitOptions) {
if ((uri != null && !uri.contains(URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE)
|| (uri != null && uri.contains(URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE && transmitOptions != RemoteATCommandOptions.OPTION_APPLY_CHANGES))
if ((uri != null && !uri.contains(CoAPURI.URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE)
|| (uri != null && uri.contains(CoAPURI.URI_AT_COMMAND) && transmitOptions != RemoteATCommandOptions.OPTION_NONE && transmitOptions != RemoteATCommandOptions.OPTION_APPLY_CHANGES))
throw new IllegalArgumentException(ERROR_OPTIONS_INVALID);

this.transmitOptions = transmitOptions;
Expand Down
Loading

0 comments on commit 208b75d

Please sign in to comment.