Commit d1f8602f authored by Larry Sachs's avatar Larry Sachs Committed by Rick Bell

Add classes for cbor encoder and representation.

Also adds OcUtils for remaining static methods in OCMain.
Includes sample apps modified to use the new Java classes.

Change-Id: I4d69e3608967d573509488209acfa3e80b751024
Signed-off-by: Larry Sachs's avatarLarry Sachs <larry.j.sachs@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/29427Tested-by: default avatarIoTivity Jenkins <jenkins-daemon@iotivity.org>
Reviewed-by: George Nash's avatarGeorge Nash <george.nash@intel.com>
Reviewed-by: default avatarRick Bell <richard.s.bell@intel.com>
parent e4e8073e
......@@ -10,9 +10,8 @@ public class OwnedDeviceHandler implements OCObtDiscoveryHandler {
String deviceId = OCUuidUtil.uuidToString(uuid);
System.out.println("\nDiscovered owned device: " + deviceId + " at:");
while (endpoints != null) {
String[] endpointStr = new String[1];
OCMain.endpointToString(endpoints, endpointStr);
System.out.println(endpointStr[0]);
String endpointStr = OcUtils.endpointToString(endpoints);
System.out.println(endpointStr);
endpoints = endpoints.getNext();
}
......
......@@ -10,9 +10,8 @@ public class UnownedDeviceHandler implements OCObtDiscoveryHandler {
String deviceId = OCUuidUtil.uuidToString(uuid);
System.out.println("\nDiscovered unowned device: " + deviceId + " at:");
while (endpoints != null) {
String[] endpointStr = new String[1];
OCMain.endpointToString(endpoints, endpointStr);
System.out.println(endpointStr[0]);
String endpointStr = OcUtils.endpointToString(endpoints);
System.out.println(endpointStr);
endpoints = endpoints.getNext();
}
......
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class GetLightCollectionResponseHandler implements OCResponseHandler {
......@@ -24,7 +25,7 @@ public class GetLightCollectionResponseHandler implements OCResponseHandler {
System.out.println("\tKey " + link.getName() + " value " + link.getValue().getString());
break;
case OC_REP_STRING_ARRAY: {
String[] strings = OCMain.ocArrayToStringArray(link.getValue().getArray());
String[] strings = OcUtils.ocArrayToStringArray(link.getValue().getArray());
StringBuilder msg = new StringBuilder("[");
for (String s : strings) {
msg.append(" " + s);
......@@ -71,30 +72,30 @@ public class GetLightCollectionResponseHandler implements OCResponseHandler {
}
PostLightCollectionResponseHandler responseHandler = new PostLightCollectionResponseHandler(collection);
if (OCMain.initPost(collection.getServerUri(), collection.getServerEndpoint(), "if=oic.if.b", responseHandler,
if (OcUtils.initPost(collection.getServerUri(), collection.getServerEndpoint(), "if=oic.if.b", responseHandler,
OCQos.LOW_QOS)) {
CborEncoder links = OCMain.repBeginLinksArray();
OcCborEncoder links = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.LINKS_ARRAY);
CborEncoder link = OCMain.repObjectArrayBeginItem(links);
OCMain.repSetTextString(link, "href", "/light/1");
CborEncoder light = OCMain.repOpenObject(link, "rep");
OCMain.repSetLong(light, "power", 10);
OCMain.repSetBoolean(light, "state", true);
OCMain.repCloseObject(link, light);
OCMain.repObjectArrayEndItem(links, link);
OcCborEncoder link = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ARRAY_ITEM, links);
link.setTextString("href", "/light/1");
OcCborEncoder light = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.OBJECT, link, "rep");
light.setLong("power", 10);
light.setBoolean("state", true);
light.done();
link.done();
link = OCMain.repObjectArrayBeginItem(links);
OCMain.repSetTextString(link, "href", "/light/2");
light = OCMain.repOpenObject(link, "rep");
OCMain.repSetLong(light, "power", 20);
OCMain.repSetBoolean(light, "state", true);
OCMain.repCloseObject(link, light);
OCMain.repObjectArrayEndItem(links, link);
link = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ARRAY_ITEM, links);
link.setTextString("href", "/light/2");
light = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.OBJECT, link, "rep");
light.setLong("power", 20);
light.setBoolean("state", false);
light.done();
link.done();
OCMain.repEndLinksArray();
links.done();
if (OCMain.doPost()) {
if (OcUtils.doPost()) {
System.out.println("\tSent POST request");
} else {
System.out.println("\tCould not send POST request");
......
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class GetLightResponseHandler implements OCResponseHandler {
......@@ -35,13 +36,14 @@ public class GetLightResponseHandler implements OCResponseHandler {
}
PutLightResponseHandler putLight = new PutLightResponseHandler(light);
if (OCMain.initPut(light.getServerUri(), light.getServerEndpoint(), null, putLight, OCQos.LOW_QOS)) {
CborEncoder root = OCMain.repBeginRootObject();
OCMain.repSetBoolean(root, "state", true);
OCMain.repSetLong(root, "power", 15);
OCMain.repEndRootObject();
if (OcUtils.initPut(light.getServerUri(), light.getServerEndpoint(), null, putLight, OCQos.LOW_QOS)) {
if (OCMain.doPut()) {
OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
root.setBoolean("state", true);
root.setLong("power", 15);
root.done();
if (OcUtils.doPut()) {
System.out.println("\tSent PUT request");
} else {
System.out.println("\tCould not send PUT request");
......
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class GetLinkedLightResponseHandler implements OCResponseHandler {
......@@ -35,13 +36,14 @@ public class GetLinkedLightResponseHandler implements OCResponseHandler {
}
PostLinkedLightResponseHandler putLight = new PostLinkedLightResponseHandler(light);
if (OCMain.initPut(light.getServerUri(), light.getServerEndpoint(), null, putLight, OCQos.LOW_QOS)) {
CborEncoder root = OCMain.repBeginRootObject();
OCMain.repSetBoolean(root, "state", true);
OCMain.repSetLong(root, "power", light.getPower() + 1);
OCMain.repEndRootObject();
if (OcUtils.initPut(light.getServerUri(), light.getServerEndpoint(), null, putLight, OCQos.LOW_QOS)) {
if (OCMain.doPut()) {
OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
root.setBoolean("state", !light.getState());
root.setLong("power", light.getPower() + 1);
root.done();
if (OcUtils.doPut()) {
System.out.println("\tSent PUT request");
} else {
System.out.println("\tCould not send PUT request");
......
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class GetLinksResponseHandler implements OCResponseHandler {
......@@ -29,12 +30,12 @@ public class GetLinksResponseHandler implements OCResponseHandler {
light.setServerEndpoint(server.getServerEndpoint());
light.setServerUri(link.getValue().getString());
GetLinkedLightResponseHandler responseHandler = new GetLinkedLightResponseHandler(light);
OCMain.doGet(light.getServerUri(), light.getServerEndpoint(), null, responseHandler,
OcUtils.doGet(light.getServerUri(), light.getServerEndpoint(), null, responseHandler,
OCQos.LOW_QOS);
}
break;
case OC_REP_STRING_ARRAY:
String[] strings = OCMain.ocArrayToStringArray(link.getValue().getArray());
String[] strings = OcUtils.ocArrayToStringArray(link.getValue().getArray());
StringBuilder msg = new StringBuilder("[");
for (String s : strings) {
msg.append(" " + s);
......@@ -76,6 +77,6 @@ public class GetLinksResponseHandler implements OCResponseHandler {
}
GetLightCollectionResponseHandler responseHandler = new GetLightCollectionResponseHandler(server);
OCMain.doGet(server.getServerUri(), server.getServerEndpoint(), "if=oic.if.b", responseHandler, OCQos.LOW_QOS);
OcUtils.doGet(server.getServerUri(), server.getServerEndpoint(), "if=oic.if.b", responseHandler, OCQos.LOW_QOS);
}
}
......@@ -3,6 +3,7 @@ package java_oc_simple_client;
import java.util.Arrays;
import org.iotivity.*;
import org.iotivity.oc.*;
public class MyDiscoveryHandler implements OCDiscoveryHandler {
......@@ -95,9 +96,8 @@ public class MyDiscoveryHandler implements OCDiscoveryHandler {
msg.append("\tResource " + server.getServerUri() + " hosted at endpoint(s):\n");
OCEndpoint ep = endpoint;
while (ep != null) {
String[] endpointStr = new String[1];
OCMain.endpointToString(ep, endpointStr);
msg.append("\t\tendpoint: " + endpointStr[0] + "\n");
String endpointStr = OcUtils.endpointToString(ep);
msg.append("\t\tendpoint: " + endpointStr + "\n");
msg.append("\t\t\tendpoint.device " + ep.getDevice() + "\n");
msg.append("\t\t\tendpoint.flags " + ep.getFlags() + "\n");
msg.append("\t\t\tendpoint.interfaceIndex " + ep.getInterfaceIndex() + "\n");
......@@ -108,18 +108,18 @@ public class MyDiscoveryHandler implements OCDiscoveryHandler {
if (server instanceof Light) {
GetLightResponseHandler responseHandler = new GetLightResponseHandler((Light) server);
OCMain.doGet(server.getServerUri(), server.getServerEndpoint(), null, responseHandler,
OcUtils.doGet(server.getServerUri(), server.getServerEndpoint(), null, responseHandler,
OCQos.LOW_QOS);
} else {
GetLinksResponseHandler responseHandler = new GetLinksResponseHandler(server);
OCMain.doGet(server.getServerUri(), server.getServerEndpoint(), "if=oic.if.ll", responseHandler,
OcUtils.doGet(server.getServerUri(), server.getServerEndpoint(), "if=oic.if.ll", responseHandler,
OCQos.LOW_QOS);
}
return OCDiscoveryFlags.OC_STOP_DISCOVERY;
}
}
OCMain.freeServerEndpoints(endpoint);
OcUtils.freeServerEndpoints(endpoint);
return OCDiscoveryFlags.OC_CONTINUE_DISCOVERY;
}
}
......@@ -34,7 +34,7 @@ public class MyInitHandler implements OCMainInitHandler {
public void requestEntry() {
System.out.println("inside MyInitHandler.requestEntry()");
MyDiscoveryHandler discoveryHandler = new MyDiscoveryHandler();
OCMain.doIPDiscovery("core.light", discoveryHandler);
OCMain.doIPDiscovery("oic.wk.col", discoveryHandler);
OcUtils.doIPDiscovery("core.light", discoveryHandler);
OcUtils.doIPDiscovery("oic.wk.col", discoveryHandler);
}
}
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class ObserveLightCollectionResponseHandler implements OCResponseHandler {
......@@ -24,7 +25,7 @@ public class ObserveLightCollectionResponseHandler implements OCResponseHandler
System.out.println("\tKey " + link.getName() + " value " + link.getValue().getString());
break;
case OC_REP_STRING_ARRAY: {
String[] strings = OCMain.ocArrayToStringArray(link.getValue().getArray());
String[] strings = OcUtils.ocArrayToStringArray(link.getValue().getArray());
StringBuilder msg = new StringBuilder("[");
for (String s : strings) {
msg.append(" " + s);
......
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class Post2LightResponseHandler implements OCResponseHandler {
......@@ -23,9 +24,9 @@ public class Post2LightResponseHandler implements OCResponseHandler {
}
ObserveLightResponseHandler observerLight = new ObserveLightResponseHandler(light);
OCMain.doObserve(light.getServerUri(), light.getServerEndpoint(), null, observerLight, OCQos.LOW_QOS);
OcUtils.doObserve(light.getServerUri(), light.getServerEndpoint(), null, observerLight, OCQos.LOW_QOS);
StopObserveTriggerHandler stopObserve = new StopObserveTriggerHandler(light);
OCMain.setDelayedHandler(stopObserve, 30);
OcUtils.setDelayedHandler(stopObserve, 30);
System.out.println("Sent OBSERVE request");
}
}
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class PostLightCollectionResponseHandler implements OCResponseHandler {
......@@ -23,10 +24,10 @@ public class PostLightCollectionResponseHandler implements OCResponseHandler {
}
ObserveLightCollectionResponseHandler responseHandler = new ObserveLightCollectionResponseHandler(collection);
OCMain.doGet(collection.getServerUri(), collection.getServerEndpoint(), "if=oic.if.b", responseHandler,
OcUtils.doGet(collection.getServerUri(), collection.getServerEndpoint(), "if=oic.if.b", responseHandler,
OCQos.LOW_QOS);
StopObserveCollectionTriggerHandler stopObserve = new StopObserveCollectionTriggerHandler(collection);
OCMain.setDelayedHandler(stopObserve, 30);
OcUtils.setDelayedHandler(stopObserve, 30);
System.out.println("Sent OBSERVE request");
}
}
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class PostLightResponseHandler implements OCResponseHandler {
......@@ -23,13 +24,14 @@ public class PostLightResponseHandler implements OCResponseHandler {
}
Post2LightResponseHandler postLight = new Post2LightResponseHandler(light);
if (OCMain.initPost(light.getServerUri(), light.getServerEndpoint(), null, postLight, OCQos.LOW_QOS)) {
CborEncoder root = OCMain.repBeginRootObject();
OCMain.repSetBoolean(root, "state", true);
OCMain.repSetLong(root, "power", 55);
OCMain.repEndRootObject();
if (OcUtils.initPost(light.getServerUri(), light.getServerEndpoint(), null, postLight, OCQos.LOW_QOS)) {
if (OCMain.doPost()) {
OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
root.setBoolean("state", true);
root.setLong("power", 55);
root.done();
if (OcUtils.doPost()) {
System.out.println("\tSent POST2 request");
} else {
System.out.println("\tCould not send POST2 request");
......
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class PostLinkedLightResponseHandler implements OCResponseHandler {
......@@ -23,9 +24,9 @@ public class PostLinkedLightResponseHandler implements OCResponseHandler {
}
ObserveLightResponseHandler observerLight = new ObserveLightResponseHandler(light);
OCMain.doObserve(light.getServerUri(), light.getServerEndpoint(), null, observerLight, OCQos.LOW_QOS);
OcUtils.doObserve(light.getServerUri(), light.getServerEndpoint(), null, observerLight, OCQos.LOW_QOS);
StopObserveTriggerHandler stopObserve = new StopObserveTriggerHandler(light);
OCMain.setDelayedHandler(stopObserve, 30);
OcUtils.setDelayedHandler(stopObserve, 30);
System.out.println("Sent OBSERVE request");
}
}
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class PutLightResponseHandler implements OCResponseHandler {
......@@ -21,13 +22,14 @@ public class PutLightResponseHandler implements OCResponseHandler {
}
PostLightResponseHandler postLight = new PostLightResponseHandler(light);
if (OCMain.initPost(light.getServerUri(), light.getServerEndpoint(), null, postLight, OCQos.LOW_QOS)) {
CborEncoder root = OCMain.repBeginRootObject();
OCMain.repSetBoolean(root, "state", false);
OCMain.repSetLong(root, "power", 105);
OCMain.repEndRootObject();
if (OcUtils.initPost(light.getServerUri(), light.getServerEndpoint(), null, postLight, OCQos.LOW_QOS)) {
if (OCMain.doPost()) {
OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
root.setBoolean("state", false);
root.setLong("power", 105);
root.done();
if (OcUtils.doPost()) {
System.out.println("\tSent POST request");
} else {
System.out.println("\tCould not send POST request");
......
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class StopObserveCollectionTriggerHandler implements OCTriggerHandler {
......@@ -15,7 +16,7 @@ public class StopObserveCollectionTriggerHandler implements OCTriggerHandler {
System.out.println("-------------------------------------------------------");
System.out.println("Stopping OBSERVE of Collection " + collection.getServerUri());
System.out.println("-------------------------------------------------------");
OCMain.stopObserve(collection.getServerUri(), collection.getServerEndpoint());
OcUtils.stopObserve(collection.getServerUri(), collection.getServerEndpoint());
return OCEventCallbackResult.OC_EVENT_DONE;
}
}
package java_oc_simple_client;
import org.iotivity.*;
import org.iotivity.oc.*;
public class StopObserveTriggerHandler implements OCTriggerHandler {
......@@ -15,7 +16,7 @@ public class StopObserveTriggerHandler implements OCTriggerHandler {
System.out.println("-------------------------------------------------------");
System.out.println("Stopping OBSERVE of " + light.getServerUri());
System.out.println("-------------------------------------------------------");
OCMain.stopObserve(light.getServerUri(), light.getServerEndpoint());
OcUtils.stopObserve(light.getServerUri(), light.getServerEndpoint());
return OCEventCallbackResult.OC_EVENT_DONE;
}
}
package java_oc_simple_server;
import org.iotivity.*;
import org.iotivity.oc.*;
public class GetCounter implements OCRequestHandler {
......@@ -16,19 +17,19 @@ public class GetCounter implements OCRequestHandler {
counter.setCounter(counter.getCounter() + 1);
System.out.println("GET COUNTER:");
CborEncoder root = OCMain.repBeginRootObject();
OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
switch (interfaces) {
case OCInterfaceMask.BASELINE:
OCMain.processBaselineInterface(request.getResource());
root.processBaselineInterface(request.getResource());
/* fall through */
case OCInterfaceMask.R:
OCMain.repSetLong(root, "count", counter.getCounter());
OCMain.repSetTextString(root, "name", counter.getName());
root.setLong("count", counter.getCounter());
root.setTextString("name", counter.getName());
break;
default:
break;
}
OCMain.repEndRootObject();
OCMain.sendResponse(request, OCStatus.OC_STATUS_OK);
root.done();
OcUtils.sendResponse(request, OCStatus.OC_STATUS_OK);
}
}
package java_oc_simple_server;
import org.iotivity.*;
import org.iotivity.oc.*;
public class GetLight implements OCRequestHandler {
......@@ -14,22 +15,23 @@ public class GetLight implements OCRequestHandler {
public void handler(OCRequest request, int interfaces) {
System.out.println("Inside the GetLight RequestHandler");
light.setState(!light.getState());
light.setPower(light.getPower() + 1);
System.out.println("GET LIGHT:");
CborEncoder root = OCMain.repBeginRootObject();
OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
switch (interfaces) {
case OCInterfaceMask.BASELINE:
OCMain.processBaselineInterface(request.getResource());
root.processBaselineInterface(request.getResource());
/* fall through */
case OCInterfaceMask.RW:
OCMain.repSetBoolean(root, "state", light.getState());
OCMain.repSetLong(root, "power", light.getPower());
OCMain.repSetTextString(root, "name", light.getName());
root.setBoolean("state", light.getState());
root.setLong("power", light.getPower());
root.setTextString("name", light.getName());
break;
default:
break;
}
OCMain.repEndRootObject();
OCMain.sendResponse(request, OCStatus.OC_STATUS_OK);
root.done();
OcUtils.sendResponse(request, OCStatus.OC_STATUS_OK);
}
}
package java_oc_simple_server;
import org.iotivity.*;
import org.iotivity.oc.*;
public class PostCounter implements OCRequestHandler {
......@@ -30,16 +31,16 @@ public class PostCounter implements OCRequestHandler {
break;
default:
System.out.println("NOT YET HANDLED VALUE");
OCMain.sendResponse(request, OCStatus.OC_STATUS_BAD_REQUEST);
OcUtils.sendResponse(request, OCStatus.OC_STATUS_BAD_REQUEST);
}
System.out.println("-----------------------------------------------------");
rep = rep.getNext();
}
CborEncoder root = OCMain.repBeginRootObject();
OCMain.repSetLong(root, "count", counter.getCounter());
OCMain.repEndRootObject();
OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
root.setLong("count", counter.getCounter());
root.done();
OCMain.sendResponse(request, OCStatus.OC_STATUS_CHANGED);
OcUtils.sendResponse(request, OCStatus.OC_STATUS_CHANGED);
}
}
package java_oc_simple_server;
import org.iotivity.*;
import org.iotivity.oc.*;
public class PostLight implements OCRequestHandler {
......@@ -34,17 +35,17 @@ public class PostLight implements OCRequestHandler {
break;
default:
System.out.println("NOT YET HANDLED VALUE");
OCMain.sendResponse(request, OCStatus.OC_STATUS_BAD_REQUEST);
OcUtils.sendResponse(request, OCStatus.OC_STATUS_BAD_REQUEST);
}
System.out.println("-----------------------------------------------------");
rep = rep.getNext();
}
CborEncoder root = OCMain.repBeginRootObject();
OCMain.repSetBoolean(root, "state", light.getState());
OCMain.repSetLong(root, "power", light.getPower());
OCMain.repEndRootObject();
OcCborEncoder root = OcCborEncoder.createOcCborEncoder(OcCborEncoder.EncoderType.ROOT);
root.setBoolean("state", light.getState());
root.setLong("power", light.getPower());
root.done();
OCMain.sendResponse(request, OCStatus.OC_STATUS_CHANGED);
OcUtils.sendResponse(request, OCStatus.OC_STATUS_CHANGED);
}
}
package org.iotivity.oc;
import org.iotivity.*;
public class OcCborEncoder {
private boolean isDone;
private boolean hasKey;
private EncoderType encoderType;
private OcCborEncoder parentEncoder;
private CborEncoder nativeCborEncoder;
public enum EncoderType {
ROOT, LINKS_ARRAY, OBJECT, ARRAY, ARRAY_ITEM
}
// ctors are private, use a factory ctor
private OcCborEncoder(EncoderType type, CborEncoder nativeEncoder) {
this(type, null, false, nativeEncoder);
}
private OcCborEncoder(EncoderType type, OcCborEncoder parent, boolean key, CborEncoder nativeEncoder) {
encoderType = type;
parentEncoder = parent;
hasKey = key;
nativeCborEncoder = nativeEncoder;
}
static public OcCborEncoder createOcCborEncoder(EncoderType type) {
if (type == null) {
throw new IllegalArgumentException("EncoderType cannot be null");
}
CborEncoder nativeEncoder;
switch (type) {