From: rtrimana Date: Wed, 4 Apr 2018 18:39:36 +0000 (-0700) Subject: Adding more directory structure to generated files by compiler, also script to copy... X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=c237c320de0460665d2adf13fc24034398a2d9c9;p=iot2.git Adding more directory structure to generated files by compiler, also script to copy files directly to the appropriate paths; also adding files for tutorial. --- diff --git a/benchmarks/interfaces/CameraCallback.java b/benchmarks/interfaces/CameraCallback.java index 5d5377d..3069957 100644 --- a/benchmarks/interfaces/CameraCallback.java +++ b/benchmarks/interfaces/CameraCallback.java @@ -1,6 +1,8 @@ package iotcode.interfaces; +import java.util.List; +import java.util.ArrayList; + public interface CameraCallback { public void newCameraFrameAvailable(byte latestFrame[], long timeStamp); - //public void newCameraFrameAvailable(CameraSmart _camera); } diff --git a/benchmarks/interfaces/Room.java b/benchmarks/interfaces/Room.java index c7388fd..e9d99de 100644 --- a/benchmarks/interfaces/Room.java +++ b/benchmarks/interfaces/Room.java @@ -1,5 +1,8 @@ package iotcode.interfaces; +import java.util.List; +import java.util.ArrayList; + public interface Room { public int getRoomID(); } diff --git a/iotjava/Makefile b/iotjava/Makefile index c68c986..7cd1f7a 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -86,7 +86,8 @@ run-compiler-smartlight: cp ../localconfig/iotpolicy/AmcrestCamera/*.req $(BIN_DIR)/iotpolicy/ cp ../localconfig/iotpolicy/Room/*.pol $(BIN_DIR)/iotpolicy/ cp ../localconfig/iotpolicy/Room/*.req $(BIN_DIR)/iotpolicy/ - cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler -cont SmartLightsController lifxlightbulb.pol smartlightsbulb.req -drv LifxLightBulb amcrestcamera.pol smartlightscam.req -drv AmcrestCamera motiondetection.pol motiondetection.req -drv AmcrestCamera room.pol roomsmart.req -drv LabRoom -java Java + #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler -cont SmartLightsController lifxlightbulb.pol smartlightsbulb.req -drv LifxLightBulb amcrestcamera.pol smartlightscam.req -drv AmcrestCamera motiondetection.pol motiondetection.req -drv AmcrestCamera room.pol roomsmart.req -drv LabRoom -java Java + cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler -cont SmartLightsController lifxlightbulb.pol smartlightsbulb.req -drv LifxLightBulb amcrestcamera.pol smartlightscam.req -drv AmcrestCamera motiondetection.pol motiondetection.req -drv AmcrestCamera room.pol roomsmart.req -drv LabRoom -cplus Cplus PHONY += run-compiler-lifx run-compiler-lifx: diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 79e838c..027ea07 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -73,6 +73,8 @@ public class IoTCompiler { private final static String OUTPUT_DIRECTORY = "output_files"; private final static String INTERFACES_DIRECTORY = "interfaces"; private final static String VIRTUALS_DIRECTORY = "virtuals"; + private final static String DRIVERS_DIRECTORY = "drivers"; + private final static String CONTROLLER_DIRECTORY = "controller"; private final static String CODE_PREFIX = "iotcode"; private final static String INTERFACE_PACKAGE = "iotcode.interfaces"; @@ -1174,10 +1176,10 @@ public class IoTCompiler { // Check if this interface is a callback class if(isCallbackClass(intface)) { packageClass = CODE_PREFIX + "." + driverClass; - path = createDirectories(dir + "/" + subdir, driverClass); + path = createDirectories(dir + "/" + subdir + "/" + DRIVERS_DIRECTORY, driverClass); } else { packageClass = controllerClass; - path = createDirectories(dir + "/" + subdir, controllerClass); + path = createDirectories(dir + "/" + subdir + "/" + CONTROLLER_DIRECTORY, controllerClass); } FileWriter fw = new FileWriter(path + "/" + newStubClass + ".java"); pw = new PrintWriter(new BufferedWriter(fw)); @@ -2304,10 +2306,10 @@ public class IoTCompiler { // Check if this interface is a callback class if(isCallbackClass(intface)) { packageClass = controllerClass; - path = createDirectories(dir + "/" + subdir, controllerClass); + path = createDirectories(dir + "/" + subdir + "/" + CONTROLLER_DIRECTORY, controllerClass); } else { packageClass = CODE_PREFIX + "." + driverClass; - path = createDirectories(dir + "/" + subdir, driverClass); + path = createDirectories(dir + "/" + subdir + "/" + DRIVERS_DIRECTORY, driverClass); } FileWriter fw = new FileWriter(path + "/" + newSkelClass + ".java"); pw = new PrintWriter(new BufferedWriter(fw)); @@ -3217,9 +3219,9 @@ public class IoTCompiler { String newStubClass = newIntface + "_Stub"; // Check if this interface is a callback class if(isCallbackClass(intface)) - path = createDirectories(dir + "/" + subdir, driverClass); + path = createDirectories(dir + "/" + subdir + "/" + DRIVERS_DIRECTORY, driverClass); else - path = createDirectories(dir + "/" + subdir, controllerClass); + path = createDirectories(dir + "/" + subdir + "/" + CONTROLLER_DIRECTORY, controllerClass); FileWriter fw = new FileWriter(path + "/" + newStubClass + ".hpp"); pw = new PrintWriter(new BufferedWriter(fw)); // Write file headers @@ -3300,9 +3302,9 @@ public class IoTCompiler { String newStubClass = newIntface + "_Stub"; // Check if this interface is a callback class if(isCallbackClass(intface)) - path = createDirectories(dir + "/" + subdir, driverClass); + path = createDirectories(dir + "/" + subdir + "/" + DRIVERS_DIRECTORY, driverClass); else - path = createDirectories(dir + "/" + subdir, controllerClass); + path = createDirectories(dir + "/" + subdir + "/" + CONTROLLER_DIRECTORY, controllerClass); FileWriter fw = new FileWriter(path + "/" + newStubClass + ".cpp"); pw = new PrintWriter(new BufferedWriter(fw)); // Write file headers @@ -4323,9 +4325,9 @@ public class IoTCompiler { String driverClass = drvList.get(i); // Check if this interface is a callback class if(isCallbackClass(intface)) - path = createDirectories(dir + "/" + subdir, controllerClass); + path = createDirectories(dir + "/" + subdir + "/" + CONTROLLER_DIRECTORY, controllerClass); else - path = createDirectories(dir + "/" + subdir, driverClass); + path = createDirectories(dir + "/" + subdir + "/" + DRIVERS_DIRECTORY, driverClass); FileWriter fw = new FileWriter(path + "/" + newSkelClass + ".hpp"); pw = new PrintWriter(new BufferedWriter(fw)); // Write file headers @@ -4419,9 +4421,9 @@ public class IoTCompiler { String driverClass = drvList.get(i); // Check if this interface is a callback class if(isCallbackClass(intface)) - path = createDirectories(dir + "/" + subdir, controllerClass); + path = createDirectories(dir + "/" + subdir + "/" + CONTROLLER_DIRECTORY, controllerClass); else - path = createDirectories(dir + "/" + subdir, driverClass); + path = createDirectories(dir + "/" + subdir + "/" + DRIVERS_DIRECTORY, driverClass); FileWriter fw = new FileWriter(path + "/" + newSkelClass + ".cpp"); pw = new PrintWriter(new BufferedWriter(fw)); // Write file headers diff --git a/others/lede-gui/.gradle/3.3/taskArtifacts/fileHashes.bin b/others/lede-gui/.gradle/3.3/taskArtifacts/fileHashes.bin index b3625fe..14472c9 100644 Binary files a/others/lede-gui/.gradle/3.3/taskArtifacts/fileHashes.bin and b/others/lede-gui/.gradle/3.3/taskArtifacts/fileHashes.bin differ diff --git a/others/lede-gui/.gradle/3.3/taskArtifacts/fileSnapshots.bin b/others/lede-gui/.gradle/3.3/taskArtifacts/fileSnapshots.bin index 15c118b..c27ddb0 100644 Binary files a/others/lede-gui/.gradle/3.3/taskArtifacts/fileSnapshots.bin and b/others/lede-gui/.gradle/3.3/taskArtifacts/fileSnapshots.bin differ diff --git a/others/lede-gui/.gradle/3.3/taskArtifacts/taskArtifacts.bin b/others/lede-gui/.gradle/3.3/taskArtifacts/taskArtifacts.bin index 331ee73..f1a6c2c 100644 Binary files a/others/lede-gui/.gradle/3.3/taskArtifacts/taskArtifacts.bin and b/others/lede-gui/.gradle/3.3/taskArtifacts/taskArtifacts.bin differ diff --git a/others/lede-gui/.gradle/3.3/taskArtifacts/taskArtifacts.lock b/others/lede-gui/.gradle/3.3/taskArtifacts/taskArtifacts.lock index bc35d02..9c87ff1 100644 Binary files a/others/lede-gui/.gradle/3.3/taskArtifacts/taskArtifacts.lock and b/others/lede-gui/.gradle/3.3/taskArtifacts/taskArtifacts.lock differ diff --git a/others/lede-gui/.gradle/3.3/tasks/_compileDebugJavaWithJavac/localClassSetAnalysis/localClassSetAnalysis.lock b/others/lede-gui/.gradle/3.3/tasks/_compileDebugJavaWithJavac/localClassSetAnalysis/localClassSetAnalysis.lock index 3ede7c9..737c618 100644 Binary files a/others/lede-gui/.gradle/3.3/tasks/_compileDebugJavaWithJavac/localClassSetAnalysis/localClassSetAnalysis.lock and b/others/lede-gui/.gradle/3.3/tasks/_compileDebugJavaWithJavac/localClassSetAnalysis/localClassSetAnalysis.lock differ diff --git a/others/lede-gui/.gradle/3.3/tasks/_compileDebugJavaWithJavac/localJarClasspathSnapshot/localJarClasspathSnapshot.lock b/others/lede-gui/.gradle/3.3/tasks/_compileDebugJavaWithJavac/localJarClasspathSnapshot/localJarClasspathSnapshot.lock index 24c9e46..560430c 100644 Binary files a/others/lede-gui/.gradle/3.3/tasks/_compileDebugJavaWithJavac/localJarClasspathSnapshot/localJarClasspathSnapshot.lock and b/others/lede-gui/.gradle/3.3/tasks/_compileDebugJavaWithJavac/localJarClasspathSnapshot/localJarClasspathSnapshot.lock differ diff --git a/others/lede-gui/.idea/misc.xml b/others/lede-gui/.idea/misc.xml index 7e15d9d..b0a270f 100644 --- a/others/lede-gui/.idea/misc.xml +++ b/others/lede-gui/.idea/misc.xml @@ -27,7 +27,7 @@ - + diff --git a/others/lede-gui/.idea/workspace.xml b/others/lede-gui/.idea/workspace.xml index ce9efb8..e8ea71d 100644 --- a/others/lede-gui/.idea/workspace.xml +++ b/others/lede-gui/.idea/workspace.xml @@ -47,45 +47,69 @@ - - - - - + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + - - + + + + + - - + + - - - - - + + + - + - + @@ -95,7 +119,7 @@ - + @@ -114,25 +138,28 @@ - - - + + + - - - - - + + + + + + + + @@ -1227,7 +1254,7 @@ @@ -1256,11 +1283,11 @@ - + + - @@ -1427,12 +1454,12 @@ - + - - + + @@ -2047,7 +2074,7 @@ - + @@ -2056,13 +2083,14 @@ - - + + + - + @@ -2077,7 +2105,6 @@ - @@ -2141,14 +2168,14 @@ - - - + + + @@ -2187,17 +2214,6 @@ - - - - - - - - - - - @@ -2207,17 +2223,6 @@ - - - - - - - - - - - @@ -2248,78 +2253,78 @@ - - - - + - - - + + - - - - + + + + - - + + + + + - + - - + + + - + - - + + + + + - - - - + - - + + + + + - - - - - - - + + + + - + - - + + - - - + - - - + + + + + @@ -2332,50 +2337,76 @@ - - - + + + + + + + + + + + + + + + + - - + + + + + - - - - + + + + + + + - - - - + + + + + + + - - - - - - - + + + + + + + + - - - - + + + + + + + \ No newline at end of file diff --git a/others/lede-gui/lede-gui.iml b/others/lede-gui/lede-gui.iml index 288756d..6e6cf73 100644 --- a/others/lede-gui/lede-gui.iml +++ b/others/lede-gui/lede-gui.iml @@ -62,13 +62,6 @@ - - - - - - - @@ -76,14 +69,37 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/others/script/deploy.sh b/others/script/deploy.sh new file mode 100755 index 0000000..9fe8143 --- /dev/null +++ b/others/script/deploy.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +function usage { + echo -e "\nVigilia Application Deployment" + echo -e "\nUsage: $(basename $0) [OPTION] [PARAMETER]" + echo -e "\nOPTION:" + echo -e "\t-java\n" + echo -e "\t-cpp\n" +} + +case "$1" in +-java ) + cp ../interfaces/* ../../../../benchmarks/interfaces/ + cp interfaces/* ../../../../benchmarks/interfaces/ + echo -e "Transferred interface files..." + cp -r controller/* ../../../../benchmarks/Java/ + echo -e "Transferred controller files..." + cp -r drivers/* ../../../../benchmarks/drivers/Java/ + echo -e "Transferred driver files..." + ;; +-cpp ) + cp ../virtuals/* ../../../../benchmarks/virtuals/ + cp virtuals/* ../../../../benchmarks/virtuals/ + echo -e "Transferred interface files..." + cp -r controller/* ../../../../benchmarks/Cpp/ + echo -e "Transferred controller files..." + cp -r drivers/* ../../../../benchmarks/drivers/Cpp/ + echo -e "Transferred driver files..." + ;; +* ) + usage + ;; +esac diff --git a/others/tutorial/benchmarks/application/SmartLights/Makefile b/others/tutorial/benchmarks/application/SmartLights/Makefile new file mode 100644 index 0000000..ce4bbf5 --- /dev/null +++ b/others/tutorial/benchmarks/application/SmartLights/Makefile @@ -0,0 +1,31 @@ +BASE = ../../.. + +include $(BASE)/common.mk + +JFLAGS = -d $(BIN_DIR) -cp $(BIN_DIR):$(CHECKERJARS):. +JARFLAGS = cf +JARFLAGS_SMARTLIGHTS = cf SmartLights.jar + +# checker option +# +CHECKER_OPT = -processor iotchecker.IoTJavaChecker -AprintErrorStack + +ASTUBS = -Astubs=../../../checker/astubs/ + +all: smartlights + +check-all: check-smartlights + +PHONY += smartlights +smartlights: + $(JAVAC) $(JFLAGS) *.java + cp SmartLights.config $(BIN_DIR)/SmartLights + cd $(BIN_DIR)/SmartLights; $(JAR) $(JARFLAGS) SmartLights.jar ../SmartLights/SmartLights*.class ../SmartLights/LightBulb*.class ../iotcode/SmartLightBulb/*.class ../iotcode/interfaces/LightBulb*.class; mkdir tmp; mv SmartLights.class ./tmp; rm -rf *.class; mv ./tmp/* ./; rm -rf ./tmp + +PHONY += check-smartlights +check-smartlights: + $(JAVAC) $(JFLAGS) $(CHECKER_OPT) $(ASTUBS) *.java + cp SmartLights.config $(BIN_DIR)/SmartLights + cd $(BIN_DIR)/SmartLights; $(JAR) $(JARFLAGS) SmartLights.jar ../SmartLights/SmartLights*.class ../SmartLights/LightBulb*.class ../iotcode/SmartLightBulb/*.class ../iotcode/interfaces/LightBulb*.class; mkdir tmp; mv SmartLights.class ./tmp; rm -rf *.class; mv ./tmp/* ./; rm -rf ./tmp + +.PHONY: $(PHONY) diff --git a/others/tutorial/benchmarks/application/SmartLights/SmartLights.config b/others/tutorial/benchmarks/application/SmartLights/SmartLights.config new file mode 100644 index 0000000..28a4f80 --- /dev/null +++ b/others/tutorial/benchmarks/application/SmartLights/SmartLights.config @@ -0,0 +1,7 @@ +ADDITIONAL_ZIP_FILE=No + +# Language +LANGUAGE=Java + +# Tomoyo +ADDITIONAL_MAC_POLICY=Yes diff --git a/others/tutorial/benchmarks/application/SmartLights/SmartLights.java b/others/tutorial/benchmarks/application/SmartLights/SmartLights.java new file mode 100644 index 0000000..2a4eca0 --- /dev/null +++ b/others/tutorial/benchmarks/application/SmartLights/SmartLights.java @@ -0,0 +1,52 @@ +package SmartLights; + +import iotruntime.slave.IoTSet; +import iotruntime.slave.IoTRelation; +import iotruntime.slave.IoTDeviceAddress; +import iotruntime.IoTUDP; + +import java.io.IOException; +import java.net.*; +import iotcode.interfaces.LightBulbTest; +import iotcode.annotation.*; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; +import java.util.Map; + +import java.rmi.*; +import java.rmi.server.UnicastRemoteObject; + +public class Lifxtest { + + @config private IoTSet lightbulbs; + @config private IoTSet rooms; + @config private IoTRelation rooms2lightbulbs; + + public void init() throws InterruptedException { + + // Iterate over RoomSmart set + for(RoomSmart rm : rooms.values()) { + + System.out.println("Get room: " + rm.getRoomID()); + // Iterate over LightBulbTest set + for(LightBulbTest lb : rooms2lightbulbs.get(rm)) { + + lb.init(); + Thread.sleep(1000); + + for (int i = 0; i < 5; i++) { + lb.turnOff(); + System.out.println("Turning off!"); + Thread.sleep(1000); + lb.turnOn(); + System.out.println("Turning on!"); + Thread.sleep(1000); + } + + lb.turnOff(); + } + } + } +} diff --git a/others/tutorial/benchmarks/application/SmartLights/SmartLights.tomoyo b/others/tutorial/benchmarks/application/SmartLights/SmartLights.tomoyo new file mode 100644 index 0000000..974b883 --- /dev/null +++ b/others/tutorial/benchmarks/application/SmartLights/SmartLights.tomoyo @@ -0,0 +1,3 @@ +file create /tmp/imageio\*.tmp 0600 +file read/write/unlink /tmp/imageio\*.tmp + diff --git a/others/tutorial/benchmarks/drivers/HomeRoom/HomeRoom.config b/others/tutorial/benchmarks/drivers/HomeRoom/HomeRoom.config new file mode 100644 index 0000000..ec9c5d4 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/HomeRoom/HomeRoom.config @@ -0,0 +1,10 @@ +# Skeleton/original interface +INTERFACE_CLASS=Room +# Stub +INTERFACE_STUB_CLASS=RoomSmart + +# Language +LANGUAGE=Java + +# Tomoyo +ADDITIONAL_MAC_POLICY=No diff --git a/others/tutorial/benchmarks/drivers/HomeRoom/HomeRoom.java b/others/tutorial/benchmarks/drivers/HomeRoom/HomeRoom.java new file mode 100644 index 0000000..84e7833 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/HomeRoom/HomeRoom.java @@ -0,0 +1,14 @@ +package iotcode.HomeRoom; + +import iotcode.interfaces.Room; + +public class HomeRoom implements Room { + + public HomeRoom() { + + } + + public int getRoomID() { + return 0; + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/BulbColor.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/BulbColor.java new file mode 100644 index 0000000..e069e74 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/BulbColor.java @@ -0,0 +1,67 @@ +package iotcode.LifxLightBulb; + +import java.security.InvalidParameterException; + +public class BulbColor { + + private int hue; + private int saturation; + private int brightness; + private int kelvin; + + public BulbColor(int _hue, int _saturation, int _brightness, int _kelvin) { + + if ((hue > 65535) || (hue < 0)) { + throw new InvalidParameterException("BulbColor: Invalid parameter value for _hue (0-65535)"); + } + + if ((saturation > 65535) || (saturation < 0)) { + throw new InvalidParameterException("BulbColor: Invalid parameter value for _saturation (0-65535)"); + } + + if ((brightness > 65535) || (brightness < 0)) { + throw new InvalidParameterException("BulbColor: Invalid parameter value for _brightness (0-65535)"); + } + + if ((kelvin > 65535) || (kelvin < 0)) { + throw new InvalidParameterException("BulbColor: Invalid parameter value for _kelvin (0-65535)"); + } + + hue = _hue; + saturation = _saturation; + brightness = _brightness; + kelvin = _kelvin; + } + + public BulbColor(byte[] data) { + hue = ((data[1] & 0xFF) << 8); + hue |= (data[0] & 0xFF); + + saturation = ((data[3] & 0xFF) << 8); + saturation |= (data[2] & 0xFF); + + brightness = ((data[5] & 0xFF) << 8); + brightness |= (data[4] & 0xFF); + + kelvin = ((data[7] & 0xFF) << 8); + kelvin |= (data[6] & 0xFF); + } + + public int getHue() { + return hue; + } + + public int getSaturation() { + return saturation; + } + + public int getBrightness() { + return brightness; + } + + public int getKelvin() { + return kelvin; + } +} + + diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateGroup.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateGroup.java new file mode 100644 index 0000000..9ed6c91 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateGroup.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateGroup { + byte[] group = new byte[16]; + final String label; + final long updatedAt; + + public DeviceStateGroup(byte[] _location, String _label, long _updatedAt) { + group = _location; + label = _label; + updatedAt = _updatedAt; + } + + public byte[] getGroup() { + return group; + } + + public String getLabel() { + return label; + } + + public long getUpdatedAt() { + return updatedAt; + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateHostFirmware.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateHostFirmware.java new file mode 100644 index 0000000..4447ca0 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateHostFirmware.java @@ -0,0 +1,21 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateHostFirmware { + // time of build in nanosecond accuracy + // after some tests + final long build; + final long version; // firmware version + + public DeviceStateHostFirmware(long _build, long _version) { + build = _build; + version = _version; + } + + public long getBuild() { + return build; + } + + public long getVersion() { + return version; + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateHostInfo.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateHostInfo.java new file mode 100644 index 0000000..a10213d --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateHostInfo.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateHostInfo { + final long signal; + final long tx; + final long rx; + + public DeviceStateHostInfo(long _signal, long _tx, long _rx) { + signal = _signal; + tx = _tx; + rx = _rx; + } + + public long getSignal() { + return signal; + } + + public long getTx() { + return tx; + } + + public long getRx() { + return rx; + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateInfo.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateInfo.java new file mode 100644 index 0000000..6f8e397 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateInfo.java @@ -0,0 +1,26 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateInfo { + // all values are in nanoseconds + private final long time; + private final long upTime; + private final long downTime; + + public DeviceStateInfo(long _time, long _upTime, long _downTime) { + time = _time; + upTime = _upTime; + downTime = _downTime; + } + + public long getTime() { + return time; + } + + public long getUpTime() { + return upTime; + } + + public long getDownTime() { + return downTime; + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateLocation.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateLocation.java new file mode 100644 index 0000000..e5d3e08 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateLocation.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateLocation { + byte[] location = new byte[16]; + final String label; + final long updatedAt; + + public DeviceStateLocation(byte[] _location, String _label, long _updatedAt) { + location = _location; + label = _label; + updatedAt = _updatedAt; + } + + public byte[] getLocation() { + return location; + } + + public String getLabel() { + return label; + } + + public long getUpdatedAt() { + return updatedAt; + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateService.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateService.java new file mode 100644 index 0000000..22f34ac --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateService.java @@ -0,0 +1,19 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateService { + private final int service; + private final long port; + + public DeviceStateService(int _service, long _port) { + service = _service; + port = _port; + } + + public int getService() { + return service; + } + + public long getPort() { + return port; + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateVersion.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateVersion.java new file mode 100644 index 0000000..f2f0200 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateVersion.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateVersion { + final long vender; + final long product; + final long version; + + public DeviceStateVersion(long _vender, long _product, long _version) { + vender = _vender; + product = _product; + version = _version; + } + + public long getVender() { + return vender; + } + + public long getProduct() { + return product; + } + + public long getVersion() { + return version; + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateWifiFirmware.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateWifiFirmware.java new file mode 100644 index 0000000..50bafe9 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateWifiFirmware.java @@ -0,0 +1,21 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateWifiFirmware { + // time of build in nanosecond accuracy + // after some tests + final long build; + final long version; // firmware version + + public DeviceStateWifiFirmware(long _build, long _version) { + build = _build; + version = _version; + } + + public long getBuild() { + return build; + } + + public long getVersion() { + return version; + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateWifiInfo.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateWifiInfo.java new file mode 100644 index 0000000..b2d8e02 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/DeviceStateWifiInfo.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class DeviceStateWifiInfo { + final long signal; + final long tx; + final long rx; + + public DeviceStateWifiInfo(long _signal, long _tx, long _rx) { + signal = _signal; + tx = _tx; + rx = _rx; + } + + public long getSignal() { + return signal; + } + + public long getTx() { + return tx; + } + + public long getRx() { + return rx; + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/LifxHeader.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/LifxHeader.java new file mode 100644 index 0000000..70d314f --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/LifxHeader.java @@ -0,0 +1,263 @@ +package iotcode.LifxLightBulb; + +import java.security.InvalidParameterException; + +public class LifxHeader { + // Frame Variables + private int size; + private int origin; + private boolean tagged; + private boolean addressable; + private int protocol; + private long source; + + //Frame Adress Variables + private byte[] macAddress = new byte[8]; + private boolean ack_required; + private boolean res_required; + private int sequence; + + //Protocol Header + private int type; + + public LifxHeader() { + // needed values as per spec + origin = 0; + addressable = true; + protocol = 1024; + } + + public void setSize(int _size) { + if (_size < 0) { + throw new InvalidParameterException("Header: size cannot be less than 0"); + } else if (_size > 65535) { + throw new InvalidParameterException("Header: size to large"); + } + size = _size; + } + + public void setOrigin(int _origin) { + if (_origin < 0) { + throw new InvalidParameterException("Header: origin cannot be less than 0"); + } else if (_origin > 3) { + throw new InvalidParameterException("Header: origin to large"); + } + + origin = _origin; + } + + public void setTagged(boolean _tagged) { + tagged = _tagged; + } + + public void setAddressable(boolean _addressable) { + addressable = _addressable; + } + + public void setProtocol(int _protocol) { + if (_protocol < 0) { + throw new InvalidParameterException("Header: protocol cannot be less than 0"); + } else if (_protocol > 4095) { + throw new InvalidParameterException("Header: protocol to large"); + } + + protocol = _protocol; + } + + public void setSource(long _source) { + if (_source < 0) { + throw new InvalidParameterException("Header: source cannot be less than 0"); + } else if (_source > (long)4294967295l) { + throw new InvalidParameterException("Header: source to large"); + } + source = _source; + } + + public void setSequence(int _sequence) { + if (_sequence < 0) { + throw new InvalidParameterException("Header: sequence cannot be less than 0"); + } else if (_sequence > 255) { + throw new InvalidParameterException("Header: sequence to large"); + } + sequence = _sequence; + } + + public void setType(int _type) { + if (_type < 0) { + throw new InvalidParameterException("Header: type cannot be less than 0"); + } else if (_type > 65535) { + throw new InvalidParameterException("Header: type to large"); + } + type = _type; + } + + public void setAck_required(boolean _ack_required) { + ack_required = _ack_required; + } + + public void setRes_required(boolean _res_required) { + res_required = _res_required; + } + + public void setMacAddress(byte[] _macAddress) { + macAddress = _macAddress; + } + + public int getSize() { + return size; + } + + public int getOrigin() { + return origin; + } + + public boolean getTagged() { + return tagged; + } + + public boolean getAddressable() { + return addressable; + } + + public int getProtocol() { + return protocol; + } + + public long getSource() { + return source; + } + + public int getSequence() { + return sequence; + } + + public int getType() { + return type; + } + + public byte[] getMacAddress() { + return macAddress; + } + + public boolean getAck_required() { + return ack_required; + } + + public boolean getRes_required() { + return res_required; + } + + public byte[] getHeaderBytes() { + byte[] headerBytes = new byte[36]; + headerBytes[0] = (byte)(size & 0xFF); + headerBytes[1] = (byte)((size >> 8) & 0xFF); + + + headerBytes[2] = (byte)(protocol & 0xFF); + headerBytes[3] = (byte)((protocol >> 8) & 0x0F); + + headerBytes[3] |= (byte)((origin & 0x03) << 6); + + if (tagged) { + headerBytes[3] |= (1 << 5); + } + + if (addressable) { + headerBytes[3] |= (1 << 4); + } + + headerBytes[4] = (byte)((source >> 0) & 0xFF); + headerBytes[5] = (byte)((source >> 8) & 0xFF); + headerBytes[6] = (byte)((source >> 16) & 0xFF); + headerBytes[7] = (byte)((source >> 24) & 0xFF); + + + // fix in a bit + headerBytes[8] = macAddress[0]; + headerBytes[9] = macAddress[1]; + headerBytes[10] = macAddress[2]; + headerBytes[11] = macAddress[3]; + headerBytes[12] = macAddress[4]; + headerBytes[13] = macAddress[5]; + headerBytes[14] = macAddress[6]; + headerBytes[15] = macAddress[7]; + + // Reserved and set to 0 + // headerBytes[16] = 0; + // headerBytes[17] = 0; + // headerBytes[18] = 0; + // headerBytes[19] = 0; + // headerBytes[20] = 0; + // headerBytes[21] = 0; + + if (ack_required) { + headerBytes[22] = (1 << 1); + } + + if (res_required) { + headerBytes[22] |= (1); + } + + headerBytes[23] = (byte)(sequence & 0xFF); + + // Reserved and set to 0 + //headerBytes[24] = 0; + //headerBytes[25] = 0; + //headerBytes[26] = 0; + //headerBytes[27] = 0; + //headerBytes[28] = 0; + //headerBytes[29] = 0; + //headerBytes[30] = 0; + //headerBytes[31] = 0; + + headerBytes[32] = (byte)((type >> 0) & 0xFF); + headerBytes[33] = (byte)((type >> 8) & 0xFF); + + // Reserved and set to 0 + //headerBytes[34] = 0; + //headerBytes[35] = 0; + + return headerBytes; + } + + public void setFromBytes(byte[] dataBytes) { + if (dataBytes.length != 36) { + throw new InvalidParameterException("Header: invalid number of bytes"); + } + + size = dataBytes[0] & 0xFF; + size |= ((dataBytes[1] & 0xFF) << 8); + size &= 0xFFFF; + + origin = (dataBytes[3] >> 6) & 0x03; + tagged = ((dataBytes[3] >> 5) & 0x01) == 1; + addressable = ((dataBytes[3] >> 4) & 0x01) == 1; + + + protocol = (dataBytes[3] & 0x0F) << 8; + protocol |= dataBytes[2]; + protocol &= 0x0FFF; + + source = (dataBytes[7] & 0xFFl) << 24; + source |= ((dataBytes[6] & 0xFFl) << 16); + source |= ((dataBytes[5] & 0xFFl) << 8); + source |= ((dataBytes[4] & 0xFFl)); + + macAddress[0] = dataBytes[8]; + macAddress[1] = dataBytes[9]; + macAddress[2] = dataBytes[10]; + macAddress[3] = dataBytes[11]; + macAddress[4] = dataBytes[12]; + macAddress[5] = dataBytes[13]; + macAddress[6] = dataBytes[14]; + macAddress[7] = dataBytes[15]; + + ack_required = (dataBytes[22] & 0x02) == 0x02; + res_required = (dataBytes[22] & 0x01) == 0x01; + + sequence = (dataBytes[23] & 0xFF); + + type = ((dataBytes[33] & 0xFF) << 8); + type |= (dataBytes[32] & 0xFF); + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/LightBulb_Skeleton.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/LightBulb_Skeleton.java new file mode 100644 index 0000000..3b808fa --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/LightBulb_Skeleton.java @@ -0,0 +1,520 @@ +package iotcode.LifxLightBulb; + +import java.io.IOException; +import java.util.List; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Map; +import java.util.HashMap; +import java.util.concurrent.atomic.AtomicBoolean; +import iotrmi.Java.IoTRMIComm; +import iotrmi.Java.IoTRMICommClient; +import iotrmi.Java.IoTRMICommServer; +import iotrmi.Java.IoTRMIUtil; + +import iotcode.interfaces.LightBulb; + +public class LightBulb_Skeleton implements LightBulb { + + private LightBulb mainObj; + private int objectId = 1; + // Communications and synchronizations + private IoTRMIComm rmiComm; + private AtomicBoolean didAlreadyInitWaitInvoke; + private AtomicBoolean methodReceived; + private byte[] methodBytes = null; + // Permissions + private static Integer[] object1Permission = { 2, 10, 1, 3, 11, 8, 12, 7, 13, 9, 6, 16, 17, 4, 0, 14, 15, 5 }; + private static List set1Allowed; + + + public LightBulb_Skeleton(LightBulb _mainObj, int _portSend, int _portRecv) throws Exception { + mainObj = _mainObj; + rmiComm = new IoTRMICommServer(_portSend, _portRecv); + set1Allowed = new ArrayList(Arrays.asList(object1Permission)); + IoTRMIUtil.mapSkel.put(_mainObj, this); + IoTRMIUtil.mapSkelId.put(_mainObj, objectId); + didAlreadyInitWaitInvoke = new AtomicBoolean(false); + methodReceived = new AtomicBoolean(false); + rmiComm.registerSkeleton(objectId, methodReceived); + Thread thread1 = new Thread() { + public void run() { + try { + ___waitRequestInvokeMethod(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + }; + thread1.start(); + } + + public LightBulb_Skeleton(LightBulb _mainObj, IoTRMIComm _rmiComm, int _objectId) throws Exception { + mainObj = _mainObj; + rmiComm = _rmiComm; + objectId = _objectId; + set1Allowed = new ArrayList(Arrays.asList(object1Permission)); + didAlreadyInitWaitInvoke = new AtomicBoolean(false); + methodReceived = new AtomicBoolean(false); + rmiComm.registerSkeleton(objectId, methodReceived); + } + + public boolean didAlreadyInitWaitInvoke() { + return didAlreadyInitWaitInvoke.get(); + } + + public void init() { + mainObj.init(); + } + + public void turnOff() { + mainObj.turnOff(); + } + + public void turnOn() { + mainObj.turnOn(); + } + + public boolean getState() { + return mainObj.getState(); + } + + public void setColor(double _hue, double _saturation, double _brightness) { + mainObj.setColor(_hue, _saturation, _brightness); + } + + public void setTemperature(int _temperature) { + mainObj.setTemperature(_temperature); + } + + public double getBrightness() { + return mainObj.getBrightness(); + } + + public double getHue() { + return mainObj.getHue(); + } + + public double getSaturation() { + return mainObj.getSaturation(); + } + + public int getTemperature() { + return mainObj.getTemperature(); + } + + public double getBrightnessRangeLowerBound() { + return mainObj.getBrightnessRangeLowerBound(); + } + + public double getBrightnessRangeUpperBound() { + return mainObj.getBrightnessRangeUpperBound(); + } + + public double getHueRangeLowerBound() { + return mainObj.getHueRangeLowerBound(); + } + + public double getHueRangeUpperBound() { + return mainObj.getHueRangeUpperBound(); + } + + public double getSaturationRangeLowerBound() { + return mainObj.getSaturationRangeLowerBound(); + } + + public double getSaturationRangeUpperBound() { + return mainObj.getSaturationRangeUpperBound(); + } + + public int getTemperatureRangeLowerBound() { + return mainObj.getTemperatureRangeLowerBound(); + } + + public int getTemperatureRangeUpperBound() { + return mainObj.getTemperatureRangeUpperBound(); + } + + public void ___init() { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + init(); + } + + public void ___turnOff() { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + turnOff(); + } + + public void ___turnOn() { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + turnOn(); + } + + public void ___getState() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getState(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___setColor() { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { double.class, double.class, double.class }, new Class[] { null, null, null }, localMethodBytes); + setColor((double) paramObj[0], (double) paramObj[1], (double) paramObj[2]); + } + + public void ___setTemperature() { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { int.class }, new Class[] { null }, localMethodBytes); + setTemperature((int) paramObj[0]); + } + + public void ___getBrightness() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getBrightness(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___getHue() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getHue(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___getSaturation() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getSaturation(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___getTemperature() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getTemperature(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___getBrightnessRangeLowerBound() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getBrightnessRangeLowerBound(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___getBrightnessRangeUpperBound() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getBrightnessRangeUpperBound(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___getHueRangeLowerBound() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getHueRangeLowerBound(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___getHueRangeUpperBound() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getHueRangeUpperBound(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___getSaturationRangeLowerBound() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getSaturationRangeLowerBound(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___getSaturationRangeUpperBound() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getSaturationRangeUpperBound(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___getTemperatureRangeLowerBound() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getTemperatureRangeLowerBound(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___getTemperatureRangeUpperBound() throws IOException { + byte[] localMethodBytes = methodBytes; + rmiComm.setGetMethodBytes(); + Object[] paramObj = rmiComm.getMethodParams(new Class[] { }, new Class[] { }, localMethodBytes); + Object retObj = getTemperatureRangeUpperBound(); + rmiComm.sendReturnObj(retObj, localMethodBytes); + } + + public void ___waitRequestInvokeMethod() throws IOException { + didAlreadyInitWaitInvoke.compareAndSet(false, true); + while (true) { + if (!methodReceived.get()) { + continue; + } + methodBytes = rmiComm.getMethodBytes(); + methodReceived.set(false); + int _objectId = IoTRMIComm.getObjectId(methodBytes); + int methodId = IoTRMIComm.getMethodId(methodBytes); + if (_objectId == objectId) { + if (!set1Allowed.contains(methodId)) { + throw new Error("Object with object Id: " + _objectId + " is not allowed to access method: " + methodId); + } + } + else { + continue; + } + switch (methodId) { + case 0: + new Thread() { + public void run() { + try { + ___init(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 1: + new Thread() { + public void run() { + try { + ___turnOff(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 2: + new Thread() { + public void run() { + try { + ___turnOn(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 3: + new Thread() { + public void run() { + try { + ___getState(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 4: + new Thread() { + public void run() { + try { + ___setColor(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 5: + new Thread() { + public void run() { + try { + ___setTemperature(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 6: + new Thread() { + public void run() { + try { + ___getBrightness(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 7: + new Thread() { + public void run() { + try { + ___getHue(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 8: + new Thread() { + public void run() { + try { + ___getSaturation(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 9: + new Thread() { + public void run() { + try { + ___getTemperature(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 10: + new Thread() { + public void run() { + try { + ___getBrightnessRangeLowerBound(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 11: + new Thread() { + public void run() { + try { + ___getBrightnessRangeUpperBound(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 12: + new Thread() { + public void run() { + try { + ___getHueRangeLowerBound(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 13: + new Thread() { + public void run() { + try { + ___getHueRangeUpperBound(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 14: + new Thread() { + public void run() { + try { + ___getSaturationRangeLowerBound(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 15: + new Thread() { + public void run() { + try { + ___getSaturationRangeUpperBound(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 16: + new Thread() { + public void run() { + try { + ___getTemperatureRangeLowerBound(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + case 17: + new Thread() { + public void run() { + try { + ___getTemperatureRangeUpperBound(); + } + catch (Exception ex) { + ex.printStackTrace(); + } + } + }.start(); + break; + default: + throw new Error("Method Id " + methodId + " not recognized!"); + } + } + } + +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/LightState.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/LightState.java new file mode 100644 index 0000000..6d8a1f7 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/LightState.java @@ -0,0 +1,25 @@ +package iotcode.LifxLightBulb; + +public class LightState { + private final BulbColor color; + private final int power; + private final String label; + + public LightState(BulbColor _color, int _power, String _label) { + color = _color; + power = _power; + label = _label; + } + + public BulbColor getColor() { + return color; + } + + public int getPower() { + return power; + } + + public String getLabel() { + return label; + } +} diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/SmartLightBulb.config b/others/tutorial/benchmarks/drivers/SmartLightBulb/SmartLightBulb.config new file mode 100644 index 0000000..f18d3d8 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/SmartLightBulb.config @@ -0,0 +1,22 @@ +# Skeleton/original interface +INTERFACE_CLASS=LightBulb +# Stub +INTERFACE_STUB_CLASS=LightBulbTest + +# For C++ instrumentation +FIELD_NUMBER=1 +FIELD_0=lb_addresses +FIELD_CLASS_0=IoTDeviceAddress +FIELD_TYPE_0=IoTSet + +# Language +LANGUAGE=Java +#LANGUAGE=C++ +# Language (fine-grained flags) +#LANGUAGE_LifxLightBulbLB1=C++ +#LANGUAGE_LifxLightBulbLB1=Java +#LANGUAGE_LifxLightBulbLB2=C++ +#LANGUAGE_LifxLightBulbLB2=Java + +# Tomoyo +ADDITIONAL_MAC_POLICY=No diff --git a/others/tutorial/benchmarks/drivers/SmartLightBulb/SmartLightBulb.java b/others/tutorial/benchmarks/drivers/SmartLightBulb/SmartLightBulb.java new file mode 100644 index 0000000..4551e25 --- /dev/null +++ b/others/tutorial/benchmarks/drivers/SmartLightBulb/SmartLightBulb.java @@ -0,0 +1,1228 @@ +package iotcode.SmartLightBulb; + +// Standard Java Packages +import java.io.*; +import java.net.*; +import java.util.concurrent.Semaphore; +import java.security.InvalidParameterException; +import java.util.Date; +import java.util.Iterator; +import java.util.concurrent.atomic.AtomicBoolean; + +import java.util.Arrays; +import java.util.Set; +import java.util.HashSet; + +// IoT Packages +import iotcode.annotation.*; +import iotcode.interfaces.LightBulb; +import iotruntime.IoTUDP; +import iotruntime.slave.IoTDeviceAddress; +import iotruntime.slave.IoTSet; + +// String to byte conversion +import javax.xml.bind.DatatypeConverter; + +public class SmartLightBulb implements LightBulb { + + /******************************************************************************************************************************************* + ** + ** Constants + ** + *******************************************************************************************************************************************/ + public static final long GET_BULB_VERSION_RESEND_WAIT_SECONDS = 10; + + + + /******************************************************************************************************************************************* + ** + ** Variables + ** + *******************************************************************************************************************************************/ + private IoTUDP communicationSockect; + private byte[] bulbMacAddress = new byte[8]; + static Semaphore socketMutex = new Semaphore(1); + static boolean sendSocketFlag = false; + private long lastSentGetBulbVersionRequest = 0; // time last request sent + + // Current Bulb Values + private int currentHue = 0; + private int currentSaturation = 0; + private int currentBrightness = 65535; + private int currentTemperature = 9000; + private boolean bulbIsOn = false; + + + + private AtomicBoolean didAlreadyInit = new AtomicBoolean(false); + + private AtomicBoolean didGetBulbVersion = new AtomicBoolean(false); + static Semaphore settingBulbColorMutex = new Semaphore(1); + static Semaphore settingBulbTempuraturerMutex = new Semaphore(1); + static Semaphore bulbStateMutex = new Semaphore(1); + + // color and temperature ranges for the bulbs + private int hueLowerBound = 0; + private int hueUpperBound = 0; + private int saturationLowerBound = 0; + private int saturationUpperBound = 0; + private int brightnessLowerBound = 0; + private int brightnessUpperBound = 0; + private int temperatureLowerBound = 2500; + private int temperatureUpperBound = 9000; + + + + // Check if a state change was requested, used to poll the bulb for if the bulb did + // preform the requested state change + private boolean stateDidChange = false; + + /******************************************************************************************************************************************* + ** + ** Threads + ** + *******************************************************************************************************************************************/ + + // Main worker thread will do the receive loop + Thread workerThread = null; + + /******************************************************************************************************************************************* + ** + ** IoT Sets and Relations + ** + *******************************************************************************************************************************************/ + + // IoTSet of Device Addresses. + // Will be filled with only 1 address. + @config private IoTSet lb_addresses; + + /** + * Used for testing only + */ + /*public SmartLightBulb(IoTUDP udp, byte[] macAddress) { + communicationSockect = udp; + bulbMacAddress = macAddress; + }*/ + + public SmartLightBulb(IoTSet _lb_addresses, String macAddress) { + this(macAddress); + lb_addresses = _lb_addresses; + } + + public SmartLightBulb(String macAddress) { + communicationSockect = null; + + // Set the Mac Address to a default value + // Probably not needed for anything + /*bulbMacAdd[0] = (byte)0x00; + bulbMacAdd[1] = (byte)0x00; + bulbMacAdd[2] = (byte)0x00; + bulbMacAdd[3] = (byte)0x00; + bulbMacAdd[4] = (byte)0x00; + bulbMacAdd[5] = (byte)0x00; + bulbMacAdd[6] = (byte)0x00; + bulbMacAdd[7] = (byte)0x00;*/ + + bulbMacAddress = DatatypeConverter.parseHexBinary(macAddress); + } + + + + /******************************************************************************************************************************************* + ** Sending + ** Device Messages + ** + *******************************************************************************************************************************************/ + private void sendGetServicePacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(true); + header.setMacAddress(bulbMacAddress); + header.setSource(0); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(2); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetHostInfoPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(12); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetHostFirmwarePacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(14); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetWifiInfoPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(16); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetWifiFirmwarePacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(18); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetPowerPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(20); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendSetPowerPacket(int level) { + // Currently only 0 and 65535 are supported + // This is a fix for now + if ((level != 65535) && (level != 0)) { + throw new InvalidParameterException("Invalid parameter values"); + } + + if ((level > 65535) || (level < 0)) { + throw new InvalidParameterException("Invalid parameter values"); + } + + byte[] packetBytes = new byte[38]; + + LifxHeader header = new LifxHeader(); + header.setSize(38); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(21); + byte[] headerBytes = header.getHeaderBytes(); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + packetBytes[36] = (byte)(level & 0xFF); + packetBytes[37] = (byte)((level >> 8) & 0xFF); + + sendPacket(packetBytes); + } + + private void sendGetLabelPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(23); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendSetLabelPacket(String label) { + // Currently only 0 and 65535 are supported + // This is a fix for now + if (label.length() != 32) { + throw new InvalidParameterException("Invalid parameter values, label must be 32 bytes long"); + } + + byte[] packetBytes = new byte[68]; + + LifxHeader header = new LifxHeader(); + header.setSize(68); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(24); + byte[] headerBytes = header.getHeaderBytes(); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + for (int i = 0; i < 32; i++) { + packetBytes[i + 36] = label.getBytes()[i]; + } + + sendPacket(packetBytes); + } + + private void sendGetVersionPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(32); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetInfoPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(34); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetLocationPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(34); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendGetGroupPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(51); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + + /******************************************************************************************************************************************* + ** Sending + ** Light Messages + ** + *******************************************************************************************************************************************/ + private void sendGetLightStatePacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(101); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendSetLightColorPacket(BulbColor bulbColor, long duration) { + + if ((duration > 4294967295l) || (duration < 0)) { + throw new InvalidParameterException("Invalid parameter value, duration out of range (0 - 4294967295)"); + } + + byte[] packetBytes = new byte[49]; + + LifxHeader header = new LifxHeader(); + header.setSize(49); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(102); + byte[] headerBytes = header.getHeaderBytes(); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + // 1 reserved packet + packetBytes[37] = (byte)(bulbColor.getHue() & 0xFF); + packetBytes[38] = (byte)((bulbColor.getHue() >> 8) & 0xFF); + + packetBytes[39] = (byte)(bulbColor.getSaturation() & 0xFF); + packetBytes[40] = (byte)((bulbColor.getSaturation() >> 8) & 0xFF); + + packetBytes[41] = (byte)(bulbColor.getBrightness() & 0xFF); + packetBytes[42] = (byte)((bulbColor.getBrightness() >> 8) & 0xFF); + + packetBytes[43] = (byte)(bulbColor.getKelvin() & 0xFF); + packetBytes[44] = (byte)((bulbColor.getKelvin() >> 8) & 0xFF); + + packetBytes[45] = (byte)((duration >> 0) & 0xFF); + packetBytes[46] = (byte)((duration >> 8) & 0xFF); + packetBytes[47] = (byte)((duration >> 16) & 0xFF); + packetBytes[48] = (byte)((duration >> 24) & 0xFF); + + sendPacket(packetBytes); + } + + private void sendGetLightPowerPacket() { + LifxHeader header = new LifxHeader(); + header.setSize(36); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(116); + + byte[] dataBytes = header.getHeaderBytes(); + sendPacket(dataBytes); + } + + private void sendSetLightPowerPacket(int level, long duration) { + + if ((level > 65535) || (duration > 4294967295l) + || (level < 0) || (duration < 0)) { + throw new InvalidParameterException("Invalid parameter values"); + } + + byte[] packetBytes = new byte[42]; + + + LifxHeader header = new LifxHeader(); + header.setSize(42); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(117); + byte[] headerBytes = header.getHeaderBytes(); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + packetBytes[36] = (byte)(level & 0xFF); + packetBytes[37] = (byte)((level >> 8) & 0xFF); + + packetBytes[38] = (byte)((duration >> 0) & 0xFF); + packetBytes[39] = (byte)((duration >> 8) & 0xFF); + packetBytes[40] = (byte)((duration >> 16) & 0xFF); + packetBytes[41] = (byte)((duration >> 24) & 0xFF); + + System.out.println(Arrays.toString(packetBytes)); + + sendPacket(packetBytes); + } + + private void sendEchoRequestPacket(byte[] data) { + // Currently only 0 and 65535 are supported + // This is a fix for now + if (data.length != 64) { + throw new InvalidParameterException("Invalid parameter values, must have 64 bytes"); + } + + byte[] packetBytes = new byte[100]; + + LifxHeader header = new LifxHeader(); + header.setSize(100); + header.setTagged(false); + header.setMacAddress(bulbMacAddress); + header.setSource(10); // randomly picked + header.setAck_required(false); + header.setRes_required(false); + header.setSequence(0); + header.setType(58); + byte[] headerBytes = header.getHeaderBytes(); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + for (int i = 0; i < 64; i++) { + packetBytes[i + 36] = data[i]; + } + + sendPacket(packetBytes); + } + + + /******************************************************************************************************************************************* + ** Receiving + ** Device Messages + ** + *******************************************************************************************************************************************/ + private DeviceStateService parseDeviceStateServiceMessage(LifxHeader header, byte[] payloadData) { + int service = payloadData[0]; + long port = ((payloadData[3] & 0xFF) << 24); + port |= ((payloadData[2] & 0xFF) << 16); + port |= ((payloadData[1] & 0xFF) << 8); + port |= (payloadData[0] & 0xFF); + + return new DeviceStateService(service, port); + } + + private DeviceStateHostInfo parseDeviceStateHostInfoMessage(LifxHeader header, byte[] payloadData) { + long signal = ((payloadData[3] & 0xFF) << 24); + signal |= ((payloadData[2] & 0xFF) << 16); + signal |= ((payloadData[1] & 0xFF) << 8); + signal |= (payloadData[0] & 0xFF); + + long tx = ((payloadData[7] & 0xFF) << 24); + tx |= ((payloadData[6] & 0xFF) << 16); + tx |= ((payloadData[5] & 0xFF) << 8); + tx |= (payloadData[4] & 0xFF); + + long rx = ((payloadData[11] & 0xFF) << 24); + rx |= ((payloadData[10] & 0xFF) << 16); + rx |= ((payloadData[9] & 0xFF) << 8); + rx |= (payloadData[8] & 0xFF); + + return new DeviceStateHostInfo(signal, tx, rx); + } + + private DeviceStateHostFirmware parseDeviceStateHostFirmwareMessage(LifxHeader header, byte[] payloadData) { + long build = 0; + for (int i = 0; i < 8; i++) { + build += ((long) payloadData[i] & 0xffL) << (8 * i); + } + + // 8 reserved bytes + + long version = ((payloadData[19] & 0xFF) << 24); + version |= ((payloadData[18] & 0xFF) << 16); + version |= ((payloadData[17] & 0xFF) << 8); + version |= (payloadData[16] & 0xFF); + + return new DeviceStateHostFirmware(build, version); + } + + private DeviceStateWifiInfo parseDeviceStateWifiInfoMessage(LifxHeader header, byte[] payloadData) { + long signal = ((payloadData[3] & 0xFF) << 24); + signal |= ((payloadData[2] & 0xFF) << 16); + signal |= ((payloadData[1] & 0xFF) << 8); + signal |= (payloadData[0] & 0xFF); + + long tx = ((payloadData[7] & 0xFF) << 24); + tx |= ((payloadData[6] & 0xFF) << 16); + tx |= ((payloadData[5] & 0xFF) << 8); + tx |= (payloadData[4] & 0xFF); + + long rx = ((payloadData[11] & 0xFF) << 24); + rx |= ((payloadData[10] & 0xFF) << 16); + rx |= ((payloadData[9] & 0xFF) << 8); + rx |= (payloadData[8] & 0xFF); + + return new DeviceStateWifiInfo(signal, tx, rx); + } + + private DeviceStateWifiFirmware parseDeviceStateWifiFirmwareMessage(LifxHeader header, byte[] payloadData) { + long build = 0; + for (int i = 0; i < 8; i++) { + build += ((long) payloadData[i] & 0xffL) << (8 * i); + } + + // 8 reserved bytes + + long version = ((payloadData[19] & 0xFF) << 24); + version |= ((payloadData[18] & 0xFF) << 16); + version |= ((payloadData[17] & 0xFF) << 8); + version |= (payloadData[16] & 0xFF); + + return new DeviceStateWifiFirmware(build, version); + } + + private int parseStatePowerMessage(LifxHeader header, byte[] payloadData) { + int level = ((payloadData[1] & 0xFF) << 8); + level |= (payloadData[0] & 0xFF); + return level; + } + + private String parseStateLabelMessage(LifxHeader header, byte[] payloadData) { + return new String(payloadData); + } + + + private DeviceStateVersion parseDeviceStateVersionMessage(LifxHeader header, byte[] payloadData) { + long vender = ((payloadData[3] & 0xFF) << 24); + vender |= ((payloadData[2] & 0xFF) << 16); + vender |= ((payloadData[1] & 0xFF) << 8); + vender |= (payloadData[0] & 0xFF); + + long product = ((payloadData[7] & 0xFF) << 24); + product |= ((payloadData[6] & 0xFF) << 16); + product |= ((payloadData[5] & 0xFF) << 8); + product |= (payloadData[4] & 0xFF); + + long version = ((payloadData[11] & 0xFF) << 24); + version |= ((payloadData[10] & 0xFF) << 16); + version |= ((payloadData[9] & 0xFF) << 8); + version |= (payloadData[8] & 0xFF); + + return new DeviceStateVersion(vender, product, version); + } + + private DeviceStateInfo parseDeviceStateInfoMessage(LifxHeader header, byte[] payloadData) { + long time = 0; + long upTime = 0; + long downTime = 0; + for (int i = 0; i < 8; i++) { + time += ((long) payloadData[i] & 0xffL) << (8 * i); + upTime += ((long) payloadData[i + 8] & 0xffL) << (8 * i); + downTime += ((long) payloadData[i + 16] & 0xffL) << (8 * i); + } + + return new DeviceStateInfo(time, upTime, downTime); + } + + private DeviceStateLocation parseDeviceStateLocationMessage(LifxHeader header, byte[] payloadData) { + byte[] location = new byte[16]; + for (int i = 0; i < 16; i++) { + location[i] = payloadData[i]; + } + + byte[] labelBytes = new byte[32]; + for (int i = 0; i < 32; i++) { + labelBytes[i] = payloadData[i + 16]; + } + + long updatedAt = 0; + for (int i = 0; i < 8; i++) { + updatedAt += ((long) payloadData[48] & 0xffL) << (8 * i); + } + + return new DeviceStateLocation(location, new String(labelBytes), updatedAt); + } + + private DeviceStateGroup parseDeviceStateGroupMessage(LifxHeader header, byte[] payloadData) { + byte[] group = new byte[16]; + for (int i = 0; i < 16; i++) { + group[i] = payloadData[i]; + } + + byte[] labelBytes = new byte[32]; + for (int i = 0; i < 32; i++) { + labelBytes[i] = payloadData[i + 16]; + } + + long updatedAt = 0; + for (int i = 0; i < 8; i++) { + updatedAt += ((long) payloadData[48] & 0xffL) << (8 * i); + } + + return new DeviceStateGroup(group, new String(labelBytes), updatedAt); + } + + private byte[] parseDeviceEchoResponseMessage(LifxHeader header, byte[] payloadData) { + return payloadData; + } + + /******************************************************************************************************************************************* + ** Receiving + ** Light Messages + ** + *******************************************************************************************************************************************/ + private LightState parseLightStateMessage(LifxHeader header, byte[] payloadData) { + + byte[] colorData = new byte[8]; + for (int i = 0; i < 8; i++) { + colorData[i] = payloadData[i]; + } + BulbColor color = new BulbColor(colorData); + + int power = ((payloadData[11] & 0xFF) << 8); + power |= (payloadData[10] & 0xFF); + + String label = new String(payloadData); + + byte[] labelArray = new byte[32]; + for (int i = 0; i < 32; i++) { + labelArray[i] = payloadData[12 + i]; + } + + return new LightState(color, power, label); + } + + private int parseLightStatePowerMessage(LifxHeader header, byte[] payloadData) { + int level = ((payloadData[1] & 0xFF) << 8); + level |= (payloadData[0] & 0xFF); + return level; + } + + + /******************************************************************************************************************************************* + ** + ** Private Handlers + ** + *******************************************************************************************************************************************/ + private void handleStateVersionMessageRecieved(LifxHeader header, byte[] payloadData) { + + DeviceStateVersion deviceState = parseDeviceStateVersionMessage(header, payloadData); + int productNumber = (int)deviceState.getProduct(); + + boolean isColor = false; + + if (productNumber == 1) {// Original 1000 + isColor = true; + } else if (productNumber == 3) {//Color 650 + isColor = true; + } else if (productNumber == 10) {// White 800 (Low Voltage) + isColor = false; + } else if (productNumber == 11) {// White 800 (High Voltage) + isColor = false; + } else if (productNumber == 18) {// White 900 BR30 (Low Voltage) + isColor = false; + } else if (productNumber == 20) {// Color 1000 BR30 + isColor = true; + } else if (productNumber == 22) {// Color 1000 + isColor = true; + } + + if (isColor) { + hueLowerBound = 0; + hueUpperBound = 65535; + saturationLowerBound = 0; + saturationUpperBound = 65535; + brightnessLowerBound = 0; + brightnessUpperBound = 65535; + temperatureLowerBound = 2500; + temperatureUpperBound = 9000; + } else { + hueLowerBound = 0; + hueUpperBound = 0; + saturationLowerBound = 0; + saturationUpperBound = 0; + brightnessLowerBound = 0; + brightnessUpperBound = 65535;// still can dim bulb + temperatureLowerBound = 2500; + temperatureUpperBound = 9000; + } + + didGetBulbVersion.set(true); + + } + + private void handleLightStateMessageRecieved(LifxHeader header, byte[] payloadData) { + LightState lightState = parseLightStateMessage(header, payloadData); + + BulbColor color = lightState.getColor(); + int power = lightState.getPower(); + + boolean bulbWrongColor = false; + bulbWrongColor = bulbWrongColor || (color.getHue() != currentHue); + bulbWrongColor = bulbWrongColor || (color.getSaturation() != currentSaturation); + bulbWrongColor = bulbWrongColor || (color.getBrightness() != currentBrightness); + bulbWrongColor = bulbWrongColor || (color.getKelvin() != currentTemperature); + + + // gets set to true if any of the below if statements are taken + stateDidChange = false; + + if (bulbWrongColor) { + BulbColor newColor = new BulbColor(currentHue, currentSaturation, currentBrightness, currentTemperature); + sendSetLightColorPacket(newColor, 250); + // System.out.println("Failed Check 1"); + } + + try { + bulbStateMutex.acquire(); + } catch (Exception e) { + e.printStackTrace(); + } + boolean bulbIsOnTmp = bulbIsOn; + bulbStateMutex.release(); + + if ((!bulbIsOnTmp) && (power != 0)) { + turnOff(); + // System.out.println("Failed Check 2: " + Integer.toString(power)); + + } + + if (bulbIsOnTmp && (power < 65530)) { + turnOn(); + // System.out.println("Failed Check 3: " + Integer.toString(power)); + + } + } + + /******************************************************************************************************************************************* + ** + ** Light Bulb Interface Methods + ** + *******************************************************************************************************************************************/ + public double getHue() { + double tmp = 0; + try { + settingBulbColorMutex.acquire(); + tmp = ((double)currentHue / 65535.0) * 360.0; + } catch (Exception e) { + e.printStackTrace(); + } + settingBulbColorMutex.release(); + + + return tmp; + } + + public double getSaturation() { + double tmp = 0; + try { + settingBulbColorMutex.acquire(); + tmp = ((double)currentSaturation / 65535.0) * 360.0; + } catch (Exception e) { + e.printStackTrace(); + } + settingBulbColorMutex.release(); + + + return tmp; + } + + public double getBrightness() { + double tmp = 0; + try { + settingBulbColorMutex.acquire(); + tmp = ((double)currentBrightness / 65535.0) * 360.0; + } catch (Exception e) { + e.printStackTrace(); + } + settingBulbColorMutex.release(); + + return tmp; + } + + public int getTemperature() { + + int tmp = 0; + try { + settingBulbTempuraturerMutex.acquire(); + tmp = currentTemperature; + } catch (Exception e) { + e.printStackTrace(); + } + settingBulbTempuraturerMutex.release(); + + return tmp; + } + + public double getHueRangeLowerBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)hueLowerBound / 65535.0) * 360.0; + } + + public double getHueRangeUpperBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)hueUpperBound / 65535.0) * 360.0; + } + + public double getSaturationRangeLowerBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)saturationLowerBound / 65535.0) * 100.0; + } + + public double getSaturationRangeUpperBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)saturationUpperBound / 65535.0) * 100.0; + } + + public double getBrightnessRangeLowerBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)brightnessLowerBound / 65535.0) * 100.0; + } + + public double getBrightnessRangeUpperBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return ((double)brightnessUpperBound / 65535.0) * 100.0; + } + + public int getTemperatureRangeLowerBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return temperatureLowerBound; + } + + public int getTemperatureRangeUpperBound() { + if (!didGetBulbVersion.get()) { + return -1; + } + return temperatureUpperBound; + } + + public void setTemperature(int _temperature) { + + try { + settingBulbTempuraturerMutex.acquire(); + } catch (Exception e) { + e.printStackTrace(); + } + + BulbColor newColor = new BulbColor(currentHue, currentSaturation, currentBrightness, _temperature); + sendSetLightColorPacket(newColor, 250); + + currentTemperature = _temperature; + stateDidChange = true; + + settingBulbTempuraturerMutex.release(); + } + + public void setColor(double _hue, double _saturation, double _brightness) { + + try { + settingBulbColorMutex.acquire(); + } catch (Exception e) { + e.printStackTrace(); + } + + + _hue /= 360.0; + _saturation /= 100.0; + _brightness /= 100.0; + + + int newHue = (int)(_hue * 65535.0); + int newSaturation = (int)(_saturation * 65535.0); + int newBrightness = (int)(_brightness * 65535.0); + + BulbColor newColor = new BulbColor(newHue, newSaturation, newBrightness, currentTemperature); + sendSetLightColorPacket(newColor, 250); + + currentHue = newHue; + currentSaturation = newSaturation; + currentBrightness = newBrightness; + stateDidChange = true; + + settingBulbColorMutex.release(); + } + + + public void turnOff() { + + try { + bulbStateMutex.acquire(); + bulbIsOn = false; + sendSetLightPowerPacket(0, 0); + stateDidChange = true; + } catch (Exception e) { + e.printStackTrace(); + } + + bulbStateMutex.release(); + } + + public void turnOn() { + try { + bulbStateMutex.acquire(); + bulbIsOn = true; + sendSetLightPowerPacket(65535, 0); + stateDidChange = true; + + } catch (Exception e) { + e.printStackTrace(); + } + + + bulbStateMutex.release(); + } + + public boolean getState() { + + boolean tmp = false; + try { + bulbStateMutex.acquire(); + tmp = bulbIsOn; + } catch (Exception e) { + e.printStackTrace(); + } + + bulbStateMutex.release(); + + return tmp; + } + + + /******************************************************************************************************************************************* + ** + ** Communication Helpers + ** + *******************************************************************************************************************************************/ + private void recievedPacket(byte[] packetData) { + + byte[] headerBytes = new byte[36]; + for (int i = 0; i < 36; i++) { + headerBytes[i] = packetData[i]; + } + + LifxHeader recHeader = new LifxHeader(); + recHeader.setFromBytes(headerBytes); + + // load the payload bytes (strip away the header) + byte[] payloadBytes = new byte[recHeader.getSize()]; + for (int i = 36; i < recHeader.getSize(); i++) { + payloadBytes[i - 36] = packetData[i]; + } + + System.out.println("Received: " + Integer.toString(recHeader.getType())); + + switch (recHeader.getType()) { + case 3: + DeviceStateService dat = parseDeviceStateServiceMessage(recHeader, payloadBytes); + // System.out.println("Service: " + Integer.toString(dat.getService())); + // System.out.println("Port : " + Long.toString(dat.getPort())); + break; + + + case 33: + handleStateVersionMessageRecieved(recHeader, payloadBytes); + break; + + case 35: + parseDeviceStateInfoMessage(recHeader, payloadBytes); + break; + + + case 107: + handleLightStateMessageRecieved(recHeader, payloadBytes); + break; + + default: + // System.out.println("unknown packet Type"); + } + + } + + private void sendPacket(byte[] packetData) { + // System.out.println("About to send"); + sendSocketFlag = true; + + try { + socketMutex.acquire(); + } catch (InterruptedException e) { + System.out.println("mutex Error"); + } + + try { + communicationSockect.sendData(packetData); + + } catch (IOException e) { + System.out.println("Socket Send Error"); + } + + sendSocketFlag = false; + socketMutex.release(); + } + + + /** + * Worker function which runs the while loop for receiving data from the bulb. + * Is blocking + */ + private void workerFunction() { + LifxHeader h = new LifxHeader(); + + try { + // Need timeout on receives since we are not sure if a packet will be available + // for processing so don't block waiting + communicationSockect.setSoTimeout(50); + } catch (IOException e) { + } + + // Start the bulb in the off state + turnOff(); + + while (true) { + + // Check if we got the bulb version yet + // could have requested it but message could have gotten lost (UDP) + if (!didGetBulbVersion.get()) { + long currentTime = (new Date().getTime()) / 1000; + if ((currentTime - lastSentGetBulbVersionRequest) > GET_BULB_VERSION_RESEND_WAIT_SECONDS) { + // Get the bulb version so we know what type of bulb this is. + sendGetVersionPacket(); + lastSentGetBulbVersionRequest = currentTime; + } + } + + // Communication resource is busy so try again later + if (sendSocketFlag) { + continue; + } + + try { + socketMutex.acquire(); + } catch (InterruptedException e) { + } + + byte[] dat = null; + try { + dat = communicationSockect.recieveData(1024); + } catch (java.net.SocketTimeoutException e) { + // Timeout occurred + + } catch (IOException e) { + // Problem but might be able to recover?? + e.printStackTrace(); + + } + + // Never forget to release! + socketMutex.release(); + + // A packed arrived + if (dat != null) { + recievedPacket(dat); + } + + // If a state change occurred then request the bulb state to ensure that the + // bulb did indeed change its state to the correct state + if (stateDidChange) { + sendGetLightStatePacket(); + } + + // Wait a bit as to not tie up system resources + try { + Thread.sleep(100); + } catch (Exception e) { + + } + + + } + } + + + public void init() { + + if (didAlreadyInit.compareAndSet(false, true) == false) { + return; // already init + } + + try { + // Get the bulb address from the IoTSet + Iterator itr = lb_addresses.iterator(); + IoTDeviceAddress deviceAddress = (IoTDeviceAddress)itr.next(); + + System.out.println("Address: " + deviceAddress.getCompleteAddress()); + + // Create the communication channel + communicationSockect = new IoTUDP(deviceAddress); + + } catch (IOException e) { + e.printStackTrace(); + } + + // Launch the worker function in a separate thread. + workerThread = new Thread(new Runnable() { + public void run() { + workerFunction(); + } + }); + workerThread.start(); + + } + + + /* TODO: Uncomment this part to do light bulb test + public static void main(String[] args) throws Exception { + + System.out.println("Executing main function!"); + IoTDeviceAddress iotDevAdd = new IoTDeviceAddress(args[0], 12345, 56700, false, false); + Set set = new HashSet(); + set.add(iotDevAdd); + IoTSet iotset = new IoTSet(set); + SmartLightBulb lb = new SmartLightBulb(iotset, "D073D5128E300000"); + try { + lb.init(); + Thread.sleep(1000); + for (int i = 0; i < 2; i++) { + lb.turnOff(); + System.out.println("Turning off!"); + Thread.sleep(1000); + lb.turnOn(); + System.out.println("Turning on!"); + Thread.sleep(1000); + } + lb.turnOff(); + } catch(Exception ex) { + ex.printStackTrace(); + } + }*/ +} diff --git a/others/tutorial/iotpolicy/HomeRoom/room.pol b/others/tutorial/iotpolicy/HomeRoom/room.pol new file mode 100644 index 0000000..17f3bbd --- /dev/null +++ b/others/tutorial/iotpolicy/HomeRoom/room.pol @@ -0,0 +1,11 @@ +public interface Room { + + public int getRoomID(); + + capability Basic { + description = "Get room ID"; + method = "getRoomID()"; + } +} + + diff --git a/others/tutorial/iotpolicy/HomeRoom/roomsmart.req b/others/tutorial/iotpolicy/HomeRoom/roomsmart.req new file mode 100644 index 0000000..71de3c9 --- /dev/null +++ b/others/tutorial/iotpolicy/HomeRoom/roomsmart.req @@ -0,0 +1,3 @@ + +requires Room with Basic as interface RoomSmart; + diff --git a/others/tutorial/iotpolicy/SmartLightBulb/smartlightbulb.pol b/others/tutorial/iotpolicy/SmartLightBulb/smartlightbulb.pol new file mode 100644 index 0000000..1aa759e --- /dev/null +++ b/others/tutorial/iotpolicy/SmartLightBulb/smartlightbulb.pol @@ -0,0 +1,62 @@ +public interface LightBulb { + + public void init(); + public void turnOff(); + public void turnOn(); + public boolean getState(); + public void setColor(double _hue, double _saturation, double _brightness); + public void setTemperature(int _temperature); + public double getBrightness(); + public double getHue(); + public double getSaturation(); + public int getTemperature(); + public double getBrightnessRangeLowerBound(); + public double getBrightnessRangeUpperBound(); + public double getHueRangeLowerBound(); + public double getHueRangeUpperBound(); + public double getSaturationRangeLowerBound(); + public double getSaturationRangeUpperBound(); + public int getTemperatureRangeLowerBound(); + public int getTemperatureRangeUpperBound(); + + capability Initialize { + description = "Initialize object"; + method = "init()"; + } + + capability Power { + description = "Turn on/off"; + method = "turnOff()"; + method = "turnOn()"; + method = "getState()"; + } + + capability SetColorProperties { + description = "Get color properties"; + method = "setColor(double _hue, double _saturation, double _brightness)"; + method = "setTemperature(int _temperature)"; + } + + capability GetColorProperties { + description = "Set color properties"; + method = "getBrightness()"; + method = "getHue()"; + method = "getSaturation()"; + method = "getTemperature()"; + } + + capability GetColorPropertiesRange { + description = "Get upper and lower bounds of color properties"; + method = "setColor(double _hue, double _saturation, double _brightness)"; + method = "getBrightnessRangeLowerBound()"; + method = "getBrightnessRangeUpperBound()"; + method = "getHueRangeLowerBound()"; + method = "getHueRangeUpperBound()"; + method = "getSaturationRangeLowerBound()"; + method = "getSaturationRangeUpperBound()"; + method = "getTemperatureRangeLowerBound()"; + method = "getTemperatureRangeUpperBound()"; + } +} + + diff --git a/others/tutorial/iotpolicy/SmartLightBulb/smartlightsbulb.req b/others/tutorial/iotpolicy/SmartLightBulb/smartlightsbulb.req new file mode 100644 index 0000000..f7344b0 --- /dev/null +++ b/others/tutorial/iotpolicy/SmartLightBulb/smartlightsbulb.req @@ -0,0 +1,3 @@ + +requires LightBulb with Initialize, Power, SetColorProperties, GetColorProperties, GetColorPropertiesRange as interface LightBulbTest; +