Commit 3d014341 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: Ib5a9e4972b18dd48c6bbc56182d88f9dfd69eaff
Signed-off-by: Larry Sachs's avatarLarry Sachs <larry.j.sachs@intel.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/29409Tested-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 a62b00f8
...@@ -13,6 +13,8 @@ public class MyInitHandler implements OCMainInitHandler { ...@@ -13,6 +13,8 @@ public class MyInitHandler implements OCMainInitHandler {
Light.state = false; Light.state = false;
Counter.name = "John's Counter"; Counter.name = "John's Counter";
Counter.count = 0; Counter.count = 0;
OCMain.setRandomPinHandler(new RandomPinHandler());
return ret; return ret;
} }
......
package java_lite_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");
}
}
package java_onboarding_tool;
import org.iotivity.OCObtDeviceStatusHandler;
import org.iotivity.OCUuidUtil;
import org.iotivity.OCUuid;
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));
}
}
}
...@@ -38,6 +38,8 @@ public class ObtMain { ...@@ -38,6 +38,8 @@ public class ObtMain {
private static UnownedDeviceHandler unownedDeviceHandler = new UnownedDeviceHandler(); private static UnownedDeviceHandler unownedDeviceHandler = new UnownedDeviceHandler();
private static OwnedDeviceHandler ownedDeviceHandler = new OwnedDeviceHandler(); private static OwnedDeviceHandler ownedDeviceHandler = new OwnedDeviceHandler();
private static JustWorksHandler justWorksHandler = new JustWorksHandler(); 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 ProvisionCredentialsHandler provisionCredentialsHandler = new ProvisionCredentialsHandler();
private static ResetDeviceHandler resetDeviceHandler = new ResetDeviceHandler(); private static ResetDeviceHandler resetDeviceHandler = new ResetDeviceHandler();
private static ProvisionAce2Handler provisionAce2Handler = new ProvisionAce2Handler(); private static ProvisionAce2Handler provisionAce2Handler = new ProvisionAce2Handler();
...@@ -47,15 +49,15 @@ public class ObtMain { ...@@ -47,15 +49,15 @@ public class ObtMain {
static private Thread shutdownHook = new Thread() { static private Thread shutdownHook = new Thread() {
public void run() { public void run() {
quit = true; quit = true;
System.out.println("Calling main_shutdown."); System.out.println("Calling mainShutdown.");
OCMain.mainShutdown(); OCMain.mainShutdown();
OCObt.shutdown();
scanner.close(); scanner.close();
mainThread.interrupt(); mainThread.interrupt();
} }
}; };
public static void displayMenu() public static void displayMenu() {
{
StringBuilder menu = new StringBuilder(); StringBuilder menu = new StringBuilder();
menu.append("\n################################################\n"); menu.append("\n################################################\n");
menu.append("OCF 2.0 Onboarding Tool\n"); menu.append("OCF 2.0 Onboarding Tool\n");
...@@ -65,11 +67,13 @@ public class ObtMain { ...@@ -65,11 +67,13 @@ public class ObtMain {
menu.append("[1] Discover un-owned devices\n"); menu.append("[1] Discover un-owned devices\n");
menu.append("[2] Discover owned devices\n"); menu.append("[2] Discover owned devices\n");
menu.append("------------------------------------------------\n"); menu.append("------------------------------------------------\n");
menu.append("[3] Take ownership of device (Just-works)\n"); menu.append("[3] Just-Works Ownership Transfer Method\n");
menu.append("[4] Provision pair-wise credentials\n"); menu.append("[4] Request Random PIN from device for OTM\n");
menu.append("[5] Provision ACE2\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("------------------------------------------------\n");
menu.append("[6] RESET device\n"); menu.append("[8] RESET device\n");
menu.append("------------------------------------------------\n"); menu.append("------------------------------------------------\n");
menu.append("[9] Exit\n"); menu.append("[9] Exit\n");
menu.append("################################################\n"); menu.append("################################################\n");
...@@ -77,22 +81,20 @@ public class ObtMain { ...@@ -77,22 +81,20 @@ public class ObtMain {
System.out.print(menu); System.out.print(menu);
} }
private static void discoverUnownedDevices() private static void discoverUnownedDevices() {
{
System.out.println("Discovering un-owned devices"); System.out.println("Discovering un-owned devices");
if ( 0 > OCObt.discoverUnownedDevices(unownedDeviceHandler)) { if (0 > OCObt.discoverUnownedDevices(unownedDeviceHandler)) {
System.err.println("ERROR discovering un-owned Devices."); System.err.println("ERROR discovering un-owned Devices.");
} }
} }
private static void discoverOwnedDevices() private static void discoverOwnedDevices() {
{
if (0 > OCObt.discoverOwnedDevices(ownedDeviceHandler)) { if (0 > OCObt.discoverOwnedDevices(ownedDeviceHandler)) {
System.err.println("ERROR discovering owned Devices."); System.err.println("ERROR discovering owned Devices.");
} }
} }
private static void takeOwnershipOfDevice() { private static void otmJustWorks() {
if (unownedDevices.isEmpty()) { if (unownedDevices.isEmpty()) {
System.out.println("\nPlease Re-discover Unowned devices"); System.out.println("\nPlease Re-discover Unowned devices");
return; return;
...@@ -103,8 +105,8 @@ public class ObtMain { ...@@ -103,8 +105,8 @@ public class ObtMain {
StringBuilder unownedDevicesMenu = new StringBuilder(); StringBuilder unownedDevicesMenu = new StringBuilder();
unownedDevicesMenu.append("\nUnowned Devices:\n"); unownedDevicesMenu.append("\nUnowned Devices:\n");
OCUuid[] uds = unownedDevices.toArray(new OCUuid[unownedDevices.size()]); OCUuid[] uds = unownedDevices.toArray(new OCUuid[unownedDevices.size()]);
for(OCUuid ud : uds) { for (OCUuid ud : uds) {
unownedDevicesMenu.append("[" + i + "]: " + OCUuidUtil.uuidToString(ud) +"\n"); unownedDevicesMenu.append("[" + i + "]: " + OCUuidUtil.uuidToString(ud) + "\n");
i++; i++;
} }
unownedDevicesMenu.append("\n\nSelect device: "); unownedDevicesMenu.append("\n\nSelect device: ");
...@@ -123,13 +125,92 @@ public class ObtMain { ...@@ -123,13 +125,92 @@ public class ObtMain {
System.out.println("\nERROR issuing request to perform ownership transfer"); System.out.println("\nERROR issuing request to perform ownership transfer");
} }
/* Having issued an OTM request, remove this item from the unowned device list /*
* Having issued an OTM request, remove this item from the unowned
* device list
*/
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 = OCObt.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 = OCObt.performRandomPinOtm(uds[userInput], pin, pin.length(), 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]); unownedDevices.remove(uds[userInput]);
} }
private static void provisionCredentials() { private static void provisionCredentials() {
if(ownedDevices.isEmpty()) { if (ownedDevices.isEmpty()) {
System.out.println("\n\nPlease Re-Discover Owned devices"); System.out.println("\n\nPlease Re-Discover Owned devices");
return; return;
} }
...@@ -139,7 +220,7 @@ public class ObtMain { ...@@ -139,7 +220,7 @@ public class ObtMain {
StringBuilder ownedDevicesMenu = new StringBuilder(); StringBuilder ownedDevicesMenu = new StringBuilder();
ownedDevicesMenu.append("\nMy Devices:\n"); ownedDevicesMenu.append("\nMy Devices:\n");
OCUuid[] ods = ownedDevices.toArray(new OCUuid[ownedDevices.size()]); OCUuid[] ods = ownedDevices.toArray(new OCUuid[ownedDevices.size()]);
for(OCUuid od : ods) { for (OCUuid od : ods) {
ownedDevicesMenu.append("[" + i + "]: " + OCUuidUtil.uuidToString(od) + "\n"); ownedDevicesMenu.append("[" + i + "]: " + OCUuidUtil.uuidToString(od) + "\n");
i++; i++;
} }
...@@ -167,12 +248,12 @@ public class ObtMain { ...@@ -167,12 +248,12 @@ public class ObtMain {
} }
public static void provisionAce2() { public static void provisionAce2() {
if(ownedDevices.isEmpty()) { if (ownedDevices.isEmpty()) {
System.out.println("\n\nPlease Re-Discover Owned devices"); System.out.println("\n\nPlease Re-Discover Owned devices");
return; return;
} }
String[] connTypes = new String[]{ "anon-clear", "auth-crypt" }; String[] connTypes = new String[] { "anon-clear", "auth-crypt" };
int num_resources = 0; int num_resources = 0;
System.out.println("\nProvision ACL2\nMy Devices:"); System.out.println("\nProvision ACL2\nMy Devices:");
...@@ -182,14 +263,13 @@ public class ObtMain { ...@@ -182,14 +263,13 @@ public class ObtMain {
StringBuilder ownedDevicesMenu = new StringBuilder(); StringBuilder ownedDevicesMenu = new StringBuilder();
ownedDevicesMenu.append("\nMy Devices:\n"); ownedDevicesMenu.append("\nMy Devices:\n");
OCUuid[] ods = ownedDevices.toArray(new OCUuid[ownedDevices.size()]); OCUuid[] ods = ownedDevices.toArray(new OCUuid[ownedDevices.size()]);
for(OCUuid od : ods) { for (OCUuid od : ods) {
ownedDevicesMenu.append("[" + i + "]: " + OCUuidUtil.uuidToString(od) + "\n"); ownedDevicesMenu.append("[" + i + "]: " + OCUuidUtil.uuidToString(od) + "\n");
i++; i++;
} }
if (i == 0) { 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; return;
} }
...@@ -206,7 +286,7 @@ public class ObtMain { ...@@ -206,7 +286,7 @@ public class ObtMain {
subjectsMenu.append("[0]: " + connTypes[0] + "\n"); subjectsMenu.append("[0]: " + connTypes[0] + "\n");
subjectsMenu.append("[1]: " + connTypes[1] + "\n"); subjectsMenu.append("[1]: " + connTypes[1] + "\n");
i = 0; i = 0;
for(OCUuid od : ods) { for (OCUuid od : ods) {
subjectsMenu.append("[" + (i + 2) + "]: " + OCUuidUtil.uuidToString(od) + "\n"); subjectsMenu.append("[" + (i + 2) + "]: " + OCUuidUtil.uuidToString(od) + "\n");
i++; i++;
} }
...@@ -257,10 +337,10 @@ public class ObtMain { ...@@ -257,10 +337,10 @@ public class ObtMain {
System.out.print("Have resource href? [0-No, 1-Yes]: "); System.out.print("Have resource href? [0-No, 1-Yes]: ");
int c = scanner.nextInt(); int c = scanner.nextInt();
if (c == 1) { if (c == 1) {
System.out.println("Enter resource href (eg. /a/light): "); System.out.print("Enter resource href (eg. /a/light): ");
String href; String href;
// max string length in C is 64 characters // max string length in C is 64 characters including
// removing then nul character that is 63 // the nul character, so useable lenght is 63
href = scanner.next(); href = scanner.next();
if (href.length() > 63) { if (href.length() > 63) {
href = href.substring(0, 63); href = href.substring(0, 63);
...@@ -356,8 +436,7 @@ public class ObtMain { ...@@ -356,8 +436,7 @@ public class ObtMain {
j++; j++;
} }
} else if (c < 0 || c > 7) { } else if (c < 0 || c > 7) {
System.out.println("\nWARNING: Invalid number of interfaces.." System.out.println("\nWARNING: Invalid number of interfaces... skipping interface selection");
+ " skipping interface selection");
} }
i++; i++;
} }
...@@ -398,7 +477,7 @@ public class ObtMain { ...@@ -398,7 +477,7 @@ public class ObtMain {
} }
public static void resetDevice() { public static void resetDevice() {
if(ownedDevices.isEmpty()) { if (ownedDevices.isEmpty()) {
System.out.println("\n\nPlease Re-Discover Owned devices"); System.out.println("\n\nPlease Re-Discover Owned devices");
return; return;
} }
...@@ -408,7 +487,7 @@ public class ObtMain { ...@@ -408,7 +487,7 @@ public class ObtMain {
StringBuilder ownedDevicesMenu = new StringBuilder(); StringBuilder ownedDevicesMenu = new StringBuilder();
ownedDevicesMenu.append("\nMy Devices:\n"); ownedDevicesMenu.append("\nMy Devices:\n");
OCUuid[] ods = ownedDevices.toArray(new OCUuid[ownedDevices.size()]); OCUuid[] ods = ownedDevices.toArray(new OCUuid[ownedDevices.size()]);
for(OCUuid od : ods) { for (OCUuid od : ods) {
ownedDevicesMenu.append("[" + i + "]: " + OCUuidUtil.uuidToString(od) + "\n"); ownedDevicesMenu.append("[" + i + "]: " + OCUuidUtil.uuidToString(od) + "\n");
i++; i++;
} }
...@@ -429,15 +508,14 @@ public class ObtMain { ...@@ -429,15 +508,14 @@ public class ObtMain {
} }
} }
public static void main(String[] args) public static void main(String[] args) {
{
quit = false; quit = false;
mainThread = Thread.currentThread(); mainThread = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(shutdownHook); Runtime.getRuntime().addShutdownHook(shutdownHook);
String creds_path = "./onboarding_tool_creds/"; String creds_path = "./onboarding_tool_creds/";
java.io.File directory = new java.io.File(creds_path); java.io.File directory = new java.io.File(creds_path);
if (! directory.exists()) { if (!directory.exists()) {
directory.mkdir(); directory.mkdir();
} }
System.out.println("Storage Config PATH : " + directory.getPath()); System.out.println("Storage Config PATH : " + directory.getPath());
...@@ -456,11 +534,11 @@ public class ObtMain { ...@@ -456,11 +534,11 @@ public class ObtMain {
int userInput = 0; int userInput = 0;
try { try {
userInput = scanner.nextInt(); userInput = scanner.nextInt();
} catch(InputMismatchException e) { } catch (InputMismatchException e) {
System.out.println("Invalid Input."); System.out.println("Invalid Input.");
userInput = 0; userInput = 0;
} }
switch(userInput) { switch (userInput) {
case 0: case 0:
continue; continue;
case 1: case 1:
...@@ -470,15 +548,21 @@ public class ObtMain { ...@@ -470,15 +548,21 @@ public class ObtMain {
discoverOwnedDevices(); discoverOwnedDevices();
break; break;
case 3: case 3:
takeOwnershipOfDevice(); otmJustWorks();
break; break;
case 4: case 4:
provisionCredentials(); requestRandomPin();
break; break;
case 5: case 5:
provisionAce2(); otmRandomPin();
break; break;
case 6: case 6:
provisionCredentials();
break;
case 7:
provisionAce2();
break;
case 8:
resetDevice(); resetDevice();
break; break;
case 9: case 9:
...@@ -489,6 +573,7 @@ public class ObtMain { ...@@ -489,6 +573,7 @@ public class ObtMain {
} }
} }
OCMain.mainShutdown(); OCMain.mainShutdown();
OCObt.shutdown();
System.exit(0); System.exit(0);
} }
} }
package java_onboarding_tool;
import org.iotivity.OCObtDeviceStatusHandler;
import org.iotivity.OCUuidUtil;
import org.iotivity.OCUuid;
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));
}
}
}
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