Commit 3336ea3b authored by Larry Sachs's avatar Larry Sachs Committed by Rick Bell

Update OBT with Random PIN APIs.

Updates the OBT to use the new Random PIN APIs.
This OBT is now functionally equivalent with
obtmain.c

Tested with smart_home_server_linux.c

Change-Id: Iedbd85a2747145bd6e2170166196c6addba4b086
Signed-off-by: Larry Sachs's avatarLarry Sachs <larry.j.sachs@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/29428Tested-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 dae92dd6
......@@ -19,8 +19,6 @@ import org.iotivity.OCStorage;
import org.iotivity.OCUuidUtil;
import org.iotivity.OCUuid;
import java_onboarding_tool.UnownedDeviceHandler;
public class ObtMain {
/* user input Scanner */
......
package java_oc_onboarding_tool;
import org.iotivity.*;
import org.iotivity.oc.*;
public class GenerateRandomPinHandler implements OCObtDeviceStatusHandler {
@Override
public void handler(OCUuid uuid, int status) {
if (status >= 0) {
System.out.println("\nSuccessfully generated a Random PIN on device " + OCUuidUtil.uuidToString(uuid));
} else {
System.out.println("\nERROR generating a Random PIN on device " + OCUuidUtil.uuidToString(uuid));
}
}
}
......@@ -26,6 +26,8 @@ public class ObtMain {
private static UnownedDeviceHandler unownedDeviceHandler = new UnownedDeviceHandler();
private static OwnedDeviceHandler ownedDeviceHandler = new OwnedDeviceHandler();
private static JustWorksHandler justWorksHandler = new JustWorksHandler();
private static GenerateRandomPinHandler generateRandomPinHandler = new GenerateRandomPinHandler();
private static OtmRandomPinHandler otmRandomPinHandler = new OtmRandomPinHandler();
private static ProvisionCredentialsHandler provisionCredentialsHandler = new ProvisionCredentialsHandler();
private static ResetDeviceHandler resetDeviceHandler = new ResetDeviceHandler();
private static ProvisionAce2Handler provisionAce2Handler = new ProvisionAce2Handler();
......@@ -40,6 +42,7 @@ public class ObtMain {
quit = true;
System.out.println("Calling platform shutdown.");
obtPlatform.systemShutdown();
obt.shutdown();
scanner.close();
mainThread.interrupt();
}
......@@ -55,11 +58,13 @@ public class ObtMain {
menu.append("[1] Discover un-owned devices\n");
menu.append("[2] Discover owned devices\n");
menu.append("------------------------------------------------\n");
menu.append("[3] Take ownership of device (Just-works)\n");
menu.append("[4] Provision pair-wise credentials\n");
menu.append("[5] Provision ACE2\n");
menu.append("[3] Just-Works Ownership Transfer Method\n");
menu.append("[4] Request Random PIN from device for OTM\n");
menu.append("[5] Random PIN Ownership Transfer Method\n");
menu.append("[6] Provision pair-wise credentials\n");
menu.append("[7] Provision ACE2\n");
menu.append("------------------------------------------------\n");
menu.append("[6] RESET device\n");
menu.append("[8] RESET device\n");
menu.append("------------------------------------------------\n");
menu.append("[9] Exit\n");
menu.append("################################################\n");
......@@ -80,7 +85,7 @@ public class ObtMain {
}
}
private static void takeOwnershipOfDevice() {
private static void otmJustWorks() {
if (unownedDevices.isEmpty()) {
System.out.println("\nPlease Re-discover Unowned devices");
return;
......@@ -118,6 +123,83 @@ public class ObtMain {
unownedDevices.remove(uds[userInput]);
}
private static void requestRandomPin() {
if (unownedDevices.isEmpty()) {
System.out.println("\nPlease Re-discover Unowned devices");
return;
}
int i = 0;
StringBuilder unownedDevicesMenu = new StringBuilder();
unownedDevicesMenu.append("\nUnowned Devices:\n");
OCUuid[] uds = unownedDevices.toArray(new OCUuid[unownedDevices.size()]);
for (OCUuid ud : uds) {
unownedDevicesMenu.append("[" + i + "]: " + OCUuidUtil.uuidToString(ud) + "\n");
i++;
}
unownedDevicesMenu.append("\n\nSelect device: ");
System.out.print(unownedDevicesMenu);
int userInput = scanner.nextInt();
if (userInput < 0 || userInput >= i) {
System.out.println("ERROR: Invalid selection");
return;
}
int ret = obt.requestRandomPin(uds[userInput], generateRandomPinHandler);
if (ret >= 0) {
System.out.println("\nSuccessfully issued request to generate a random pin");
} else {
System.out.println("\nERROR issuing request to generate a random pin");
}
}
private static void otmRandomPin() {
if (unownedDevices.isEmpty()) {
System.out.println("\nPlease Re-discover Unowned devices");
return;
}
int i = 0;
StringBuilder unownedDevicesMenu = new StringBuilder();
unownedDevicesMenu.append("\nUnowned Devices:\n");
OCUuid[] uds = unownedDevices.toArray(new OCUuid[unownedDevices.size()]);
for (OCUuid ud : uds) {
unownedDevicesMenu.append("[" + i + "]: " + OCUuidUtil.uuidToString(ud) + "\n");
i++;
}
unownedDevicesMenu.append("\n\nSelect device: ");
System.out.print(unownedDevicesMenu);
int userInput = scanner.nextInt();
if (userInput < 0 || userInput >= i) {
System.out.println("ERROR: Invalid selection");
return;
}
System.out.print("Enter Random PIN: ");
String pin = scanner.next();
// max string length for pin is 24 characters
if (pin.length() > 24) {
pin = pin.substring(0, 24);
}
int ret = obt.performRandomPinOtm(uds[userInput], pin, otmRandomPinHandler);
if (ret >= 0) {
System.out.println("\nSuccessfully issued request to perform Random PIN OTM");
} else {
System.out.println("\nERROR issuing request to perform Random PIN OTM");
}
/*
* Having issued an OTM request, remove this item from the unowned
* device list
*/
unownedDevices.remove(uds[userInput]);
}
private static void provisionCredentials() {
if (ownedDevices.isEmpty()) {
System.out.println("\n\nPlease Re-Discover Owned devices");
......@@ -178,7 +260,7 @@ public class ObtMain {
}
if (i == 0) {
System.out.println("\nNo devices to provision.. Please Re-Discover Owned devices.");
System.out.println("\nNo devices to provision... Please Re-Discover Owned devices.");
return;
}
......@@ -245,10 +327,10 @@ public class ObtMain {
System.out.print("Have resource href? [0-No, 1-Yes]: ");
int c = scanner.nextInt();
if (c == 1) {
System.out.println("Enter resource href (eg. /a/light): ");
System.out.print("Enter resource href (eg. /a/light): ");
String href;
// max string length in C is 64 characters
// removing then nul character that is 63
// max string length in C is 64 characters including
// the nul character, so useable lenght is 63
href = scanner.next();
if (href.length() > 63) {
href = href.substring(0, 63);
......@@ -344,8 +426,7 @@ public class ObtMain {
j++;
}
} else if (c < 0 || c > 7) {
System.out.println(
"\nWARNING: Invalid number of interfaces.." + " skipping interface selection");
System.out.println("\nWARNING: Invalid number of interfaces... skipping interface selection");
}
i++;
}
......@@ -453,15 +534,21 @@ public class ObtMain {
discoverOwnedDevices();
break;
case 3:
takeOwnershipOfDevice();
otmJustWorks();
break;
case 4:
provisionCredentials();
requestRandomPin();
break;
case 5:
provisionAce2();
otmRandomPin();
break;
case 6:
provisionCredentials();
break;
case 7:
provisionAce2();
break;
case 8:
resetDevice();
break;
case 9:
......@@ -473,6 +560,7 @@ public class ObtMain {
}
obtPlatform.systemShutdown();
obt.shutdown();
System.exit(0);
}
}
package java_oc_onboarding_tool;
import org.iotivity.*;
import org.iotivity.oc.*;
public class OtmRandomPinHandler implements OCObtDeviceStatusHandler {
@Override
public void handler(OCUuid uuid, int status) {
if (status >= 0) {
System.out.println("\nSuccessfully performed OTM on device " + OCUuidUtil.uuidToString(uuid));
} else {
System.out.println("\nERROR performing ownership transfer on device " + OCUuidUtil.uuidToString(uuid));
}
}
}
......@@ -46,6 +46,7 @@ public class MyInitHandler implements OCMainInitHandler {
light1 = new Light("Alice's Light");
light2 = new Light("Bob's Light");
OcUtils.setRandomPinHandler(new RandomPinHandler());
return ret;
}
......
package java_oc_simple_server;
import org.iotivity.OCRandomPinHandler;
public class RandomPinHandler implements OCRandomPinHandler {
@Override
public void handler(String pin) {
System.out.println("Inside the RandomPinHandler");
System.out.println("\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
System.out.println("Random PIN: " + pin + "\n");
System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
}
}
......@@ -8,6 +8,10 @@ public class OcObt {
OCObt.init();
}
public void shutdown() {
OCObt.shutdown();
}
public int discoverUnownedDevices(OCObtDiscoveryHandler unownedDeviceHandler) {
return OCObt.discoverUnownedDevices(unownedDeviceHandler);
}
......@@ -16,8 +20,16 @@ public class OcObt {
return OCObt.discoverOwnedDevices(ownedDeviceHandler);
}
public int performJustWorksOtm(OCUuid uuid, OCObtDeviceStatusHandler justWorksHandler) {
return OCObt.performJustWorksOtm(uuid, justWorksHandler);
public int performJustWorksOtm(OCUuid uuid, OCObtDeviceStatusHandler otmJustWorksHandler) {
return OCObt.performJustWorksOtm(uuid, otmJustWorksHandler);
}
public int requestRandomPin(OCUuid uuid, OCObtDeviceStatusHandler generateRandomPinHandler) {
return OCObt.requestRandomPin(uuid, generateRandomPinHandler);
}
public int performRandomPinOtm(OCUuid uuid, String pin, OCObtDeviceStatusHandler otmRandomPinHandler) {
return OCObt.performRandomPinOtm(uuid, pin, pin.length(), otmRandomPinHandler);
}
public int provisionPairwiseCredentials(OCUuid uuid1, OCUuid uuid2,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment