From b4fed7e71b30f19f9d4ed845e513120e02dc9e81 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Tue, 10 Jan 2017 14:50:09 -0800 Subject: [PATCH] Moving Java drivers; Creating iotruntime socket connections for C++; First version of LifxLightBulb driver in C++ --- benchmarks/Makefile | 2 +- .../drivers/Cpp/LifxLightBulb/BulbColor.hpp | 82 ++ .../Cpp/LifxLightBulb/DeviceStateGroup.hpp | 41 + .../LifxLightBulb/DeviceStateHostFirmware.hpp | 35 + .../Cpp/LifxLightBulb/DeviceStateHostInfo.hpp | 39 + .../Cpp/LifxLightBulb/DeviceStateInfo.hpp | 39 + .../Cpp/LifxLightBulb/DeviceStateLocation.hpp | 40 + .../LifxLightBulb/DeviceStateLocation.hpp~ | 40 + .../Cpp/LifxLightBulb/DeviceStateService.hpp | 32 + .../Cpp/LifxLightBulb/DeviceStateVersion.hpp | 39 + .../LifxLightBulb/DeviceStateWifiFirmware.hpp | 34 + .../Cpp/LifxLightBulb/DeviceStateWifiInfo.hpp | 39 + .../drivers/Cpp/LifxLightBulb/LifxHeader.hpp | 287 ++++ .../LifxLightBulb/LifxLightBulb.config | 0 .../Cpp/LifxLightBulb/LifxLightBulb.cpp | 25 + .../Cpp/LifxLightBulb/LifxLightBulb.hpp | 1191 +++++++++++++++++ .../drivers/Cpp/LifxLightBulb/LightState.hpp | 41 + benchmarks/drivers/Cpp/Makefile | 14 + .../AmcrestCamera/AmcrestCamera.config | 0 .../AmcrestCamera/AmcrestCamera.java | 0 .../CameraSmartCallback_CallbackStub.java | 0 .../AmcrestCamera/Camera_Skeleton.java | 0 .../{ => Java}/AudioRoom/AudioRoom.config | 0 .../{ => Java}/AudioRoom/AudioRoom.java | 0 .../{ => Java}/AudioRoom/Room_Skeleton.java | 0 .../{ => Java}/EspAlarm/Alarm_Skeleton.java | 0 .../{ => Java}/EspAlarm/EspAlarm.config | 0 .../drivers/{ => Java}/EspAlarm/EspAlarm.java | 0 .../EspSprinkler/EspSprinkler.config | 0 .../{ => Java}/EspSprinkler/EspSprinkler.java | 0 .../EspSprinkler/Sprinkler_Skeleton.java | 0 .../GPSGatewaySmartCallback_CallbackStub.java | 0 .../GPSPhoneGateway/GPSGateway_Skeleton.java | 0 .../GPSPhoneGateway/GPSPhoneGateway.config | 0 .../GPSPhoneGateway/GPSPhoneGateway.java | 0 .../{ => Java}/GPSPhoneGateway/PhoneInfo.java | 0 .../GPSPhoneGateway/PhoneInfoInterface.java | 0 .../{ => Java}/GreenLawn/GreenLawn.config | 0 .../{ => Java}/GreenLawn/GreenLawn.java | 0 .../{ => Java}/GreenLawn/Lawn_Skeleton.java | 0 .../{ => Java}/HomeRoom/HomeRoom.config | 0 .../drivers/{ => Java}/HomeRoom/HomeRoom.java | 0 .../{ => Java}/HomeRoom/Room_Skeleton.java | 0 .../drivers/{ => Java}/IHome/IHome.config | 0 .../drivers/{ => Java}/IHome/IHome.java | 0 .../SpeakerSmartCallback_CallbackStub.java | 0 .../{ => Java}/IHome/Speaker_Skeleton.java | 0 .../drivers/{ => Java}/LabRoom/LabRoom.config | 0 .../drivers/{ => Java}/LabRoom/LabRoom.java | 0 .../{ => Java}/LabRoom/Room_Skeleton.java | 0 .../{ => Java}/LifxLightBulb/BulbColor.java | 0 .../LifxLightBulb/DeviceStateGroup.java | 0 .../DeviceStateHostFirmware.java | 0 .../LifxLightBulb/DeviceStateHostInfo.java | 0 .../LifxLightBulb/DeviceStateInfo.java | 0 .../LifxLightBulb/DeviceStateLocation.java | 0 .../LifxLightBulb/DeviceStateService.java | 0 .../LifxLightBulb/DeviceStateVersion.java | 0 .../DeviceStateWifiFirmware.java | 0 .../LifxLightBulb/DeviceStateWifiInfo.java | 0 .../{ => Java}/LifxLightBulb/LifxHeader.java | 0 .../Java/LifxLightBulb/LifxLightBulb.config | 6 + .../LifxLightBulb/LifxLightBulb.java | 80 +- .../LifxLightBulb/LightBulb_Skeleton.java | 0 .../{ => Java}/LifxLightBulb/LightState.java | 0 benchmarks/drivers/{ => Java}/Makefile | 2 +- .../MotionSensor/MotionSensor.config | 0 .../{ => Java}/MotionSensor/MotionSensor.java | 0 ...hingsSensorSmartCallback_CallbackStub.java | 0 .../SmartthingsSensor_Skeleton.java | 0 .../MultipurposeSensor.config | 0 .../MultipurposeSensor.java | 0 ...hingsSensorSmartCallback_CallbackStub.java | 0 .../SmartthingsSensor_Skeleton.java | 0 ...stureSensorSmartCallback_CallbackStub.java | 0 .../SpruceSensor/MoistureSensor_Skeleton.java | 0 .../SpruceSensor/SpruceSensor.config | 0 .../{ => Java}/SpruceSensor/SpruceSensor.java | 0 ...hingsSensorSmartCallback_CallbackStub.java | 0 .../SmartthingsSensor_Skeleton.java | 0 .../WaterLeakSensor/WaterLeakSensor.config | 0 .../WaterLeakSensor/WaterLeakSensor.java | 0 ...therGatewaySmartCallback_CallbackStub.java | 0 .../WeatherGateway_Skeleton.java | 0 .../WeatherPhoneGateway/WeatherInfo.java | 0 .../WeatherInfoInterface.java | 0 .../WeatherPhoneGateway.config | 0 .../WeatherPhoneGateway.java | 0 benchmarks/virtuals/LightBulb.hpp | 29 + benchmarks/virtuals/LightBulbSmart.hpp | 33 + iotjava/Makefile | 3 +- iotjava/iotruntime/cpp/IoTAddress.hpp | 59 + iotjava/iotruntime/cpp/IoTDeviceAddress.hpp | 91 ++ iotjava/iotruntime/cpp/IoTUDP.hpp | 68 + .../cpp/setrelation/IoTRelation.hpp | 129 ++ iotjava/iotruntime/cpp/setrelation/IoTSet.hpp | 111 ++ .../iotruntime/cpp/setrelation/Iterator.hpp | 20 + iotjava/iotruntime/cpp/socket/Socket.cpp | 383 ++++++ iotjava/iotruntime/cpp/socket/Socket.hpp | 340 +++++ .../iotruntime/slave/IoTDeviceAddress.java | 3 +- iotjava/iotruntime/slave/IoTSet.java | 1 + 101 files changed, 3345 insertions(+), 33 deletions(-) create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/BulbColor.hpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateGroup.hpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateHostFirmware.hpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateHostInfo.hpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateInfo.hpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateLocation.hpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateLocation.hpp~ create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateService.hpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateVersion.hpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateWifiFirmware.hpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateWifiInfo.hpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/LifxHeader.hpp rename benchmarks/drivers/{ => Cpp}/LifxLightBulb/LifxLightBulb.config (100%) create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.hpp create mode 100644 benchmarks/drivers/Cpp/LifxLightBulb/LightState.hpp create mode 100644 benchmarks/drivers/Cpp/Makefile rename benchmarks/drivers/{ => Java}/AmcrestCamera/AmcrestCamera.config (100%) rename benchmarks/drivers/{ => Java}/AmcrestCamera/AmcrestCamera.java (100%) rename benchmarks/drivers/{ => Java}/AmcrestCamera/CameraSmartCallback_CallbackStub.java (100%) rename benchmarks/drivers/{ => Java}/AmcrestCamera/Camera_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/AudioRoom/AudioRoom.config (100%) rename benchmarks/drivers/{ => Java}/AudioRoom/AudioRoom.java (100%) rename benchmarks/drivers/{ => Java}/AudioRoom/Room_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/EspAlarm/Alarm_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/EspAlarm/EspAlarm.config (100%) rename benchmarks/drivers/{ => Java}/EspAlarm/EspAlarm.java (100%) rename benchmarks/drivers/{ => Java}/EspSprinkler/EspSprinkler.config (100%) rename benchmarks/drivers/{ => Java}/EspSprinkler/EspSprinkler.java (100%) rename benchmarks/drivers/{ => Java}/EspSprinkler/Sprinkler_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/GPSPhoneGateway/GPSGatewaySmartCallback_CallbackStub.java (100%) rename benchmarks/drivers/{ => Java}/GPSPhoneGateway/GPSGateway_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/GPSPhoneGateway/GPSPhoneGateway.config (100%) rename benchmarks/drivers/{ => Java}/GPSPhoneGateway/GPSPhoneGateway.java (100%) rename benchmarks/drivers/{ => Java}/GPSPhoneGateway/PhoneInfo.java (100%) rename benchmarks/drivers/{ => Java}/GPSPhoneGateway/PhoneInfoInterface.java (100%) rename benchmarks/drivers/{ => Java}/GreenLawn/GreenLawn.config (100%) rename benchmarks/drivers/{ => Java}/GreenLawn/GreenLawn.java (100%) rename benchmarks/drivers/{ => Java}/GreenLawn/Lawn_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/HomeRoom/HomeRoom.config (100%) rename benchmarks/drivers/{ => Java}/HomeRoom/HomeRoom.java (100%) rename benchmarks/drivers/{ => Java}/HomeRoom/Room_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/IHome/IHome.config (100%) rename benchmarks/drivers/{ => Java}/IHome/IHome.java (100%) rename benchmarks/drivers/{ => Java}/IHome/SpeakerSmartCallback_CallbackStub.java (100%) rename benchmarks/drivers/{ => Java}/IHome/Speaker_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/LabRoom/LabRoom.config (100%) rename benchmarks/drivers/{ => Java}/LabRoom/LabRoom.java (100%) rename benchmarks/drivers/{ => Java}/LabRoom/Room_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/BulbColor.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/DeviceStateGroup.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/DeviceStateHostFirmware.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/DeviceStateHostInfo.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/DeviceStateInfo.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/DeviceStateLocation.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/DeviceStateService.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/DeviceStateVersion.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/DeviceStateWifiFirmware.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/DeviceStateWifiInfo.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/LifxHeader.java (100%) create mode 100644 benchmarks/drivers/Java/LifxLightBulb/LifxLightBulb.config rename benchmarks/drivers/{ => Java}/LifxLightBulb/LifxLightBulb.java (90%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/LightBulb_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/LifxLightBulb/LightState.java (100%) rename benchmarks/drivers/{ => Java}/Makefile (99%) rename benchmarks/drivers/{ => Java}/MotionSensor/MotionSensor.config (100%) rename benchmarks/drivers/{ => Java}/MotionSensor/MotionSensor.java (100%) rename benchmarks/drivers/{ => Java}/MotionSensor/SmartthingsSensorSmartCallback_CallbackStub.java (100%) rename benchmarks/drivers/{ => Java}/MotionSensor/SmartthingsSensor_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/MultipurposeSensor/MultipurposeSensor.config (100%) rename benchmarks/drivers/{ => Java}/MultipurposeSensor/MultipurposeSensor.java (100%) rename benchmarks/drivers/{ => Java}/MultipurposeSensor/SmartthingsSensorSmartCallback_CallbackStub.java (100%) rename benchmarks/drivers/{ => Java}/MultipurposeSensor/SmartthingsSensor_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/SpruceSensor/MoistureSensorSmartCallback_CallbackStub.java (100%) rename benchmarks/drivers/{ => Java}/SpruceSensor/MoistureSensor_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/SpruceSensor/SpruceSensor.config (100%) rename benchmarks/drivers/{ => Java}/SpruceSensor/SpruceSensor.java (100%) rename benchmarks/drivers/{ => Java}/WaterLeakSensor/SmartthingsSensorSmartCallback_CallbackStub.java (100%) rename benchmarks/drivers/{ => Java}/WaterLeakSensor/SmartthingsSensor_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/WaterLeakSensor/WaterLeakSensor.config (100%) rename benchmarks/drivers/{ => Java}/WaterLeakSensor/WaterLeakSensor.java (100%) rename benchmarks/drivers/{ => Java}/WeatherPhoneGateway/WeatherGatewaySmartCallback_CallbackStub.java (100%) rename benchmarks/drivers/{ => Java}/WeatherPhoneGateway/WeatherGateway_Skeleton.java (100%) rename benchmarks/drivers/{ => Java}/WeatherPhoneGateway/WeatherInfo.java (100%) rename benchmarks/drivers/{ => Java}/WeatherPhoneGateway/WeatherInfoInterface.java (100%) rename benchmarks/drivers/{ => Java}/WeatherPhoneGateway/WeatherPhoneGateway.config (100%) rename benchmarks/drivers/{ => Java}/WeatherPhoneGateway/WeatherPhoneGateway.java (100%) create mode 100644 benchmarks/virtuals/LightBulb.hpp create mode 100644 benchmarks/virtuals/LightBulbSmart.hpp create mode 100644 iotjava/iotruntime/cpp/IoTAddress.hpp create mode 100644 iotjava/iotruntime/cpp/IoTDeviceAddress.hpp create mode 100644 iotjava/iotruntime/cpp/IoTUDP.hpp create mode 100644 iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp create mode 100644 iotjava/iotruntime/cpp/setrelation/IoTSet.hpp create mode 100644 iotjava/iotruntime/cpp/setrelation/Iterator.hpp create mode 100644 iotjava/iotruntime/cpp/socket/Socket.cpp create mode 100644 iotjava/iotruntime/cpp/socket/Socket.hpp diff --git a/benchmarks/Makefile b/benchmarks/Makefile index 5ed4629..74fb11a 100644 --- a/benchmarks/Makefile +++ b/benchmarks/Makefile @@ -16,7 +16,7 @@ annotation: PHONY += drivers drivers: - $(MAKE) -C drivers + $(MAKE) -C drivers/Java PHONY += Lifxtest Lifxtest: diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/BulbColor.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/BulbColor.hpp new file mode 100644 index 0000000..726d21c --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/BulbColor.hpp @@ -0,0 +1,82 @@ +#ifndef _BULBCOLOR_HPP__ +#define _BULBCOLOR_HPP__ +#include + +class BulbColor { + + private: + int hue; + int saturation; + int brightness; + int kelvin; + + public: + + BulbColor(int _hue, int _saturation, int _brightness, int _kelvin) { + + if ((_hue > 65535) || (_hue < 0)) { + cerr << "BulbColor: Invalid parameter value for _hue (0-65535)" << endl; + exit(1); + } + + if ((_saturation > 65535) || (_saturation < 0)) { + cerr << "BulbColor: Invalid parameter value for _saturation (0-65535)" << endl; + exit(1); + } + + if ((_brightness > 65535) || (_brightness < 0)) { + cerr << "BulbColor: Invalid parameter value for _brightness (0-65535)" << endl; + exit(1); + } + + if ((_kelvin > 65535) || (_kelvin < 0)) { + cerr << "BulbColor: Invalid parameter value for _kelvin (0-65535)" << endl; + exit(1); + } + + hue = _hue; + saturation = _saturation; + brightness = _brightness; + kelvin = _kelvin; + } + + + BulbColor(char* 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); + } + + + ~BulbColor() { + } + + + int getHue() { + return hue; + } + + + int getSaturation() { + return saturation; + } + + + int getBrightness() { + return brightness; + } + + + int getKelvin() { + return kelvin; + } +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateGroup.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateGroup.hpp new file mode 100644 index 0000000..b3e86e7 --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateGroup.hpp @@ -0,0 +1,41 @@ +#ifndef _DEVICESTATEGROUP_HPP__ +#define _DEVICESTATEGROUP_HPP__ +#include +#include + +class DeviceStateGroup { + + private: + char group[16]; + string label; + int64_t updatedAt; + + public: + + DeviceStateGroup(char _location[16], string _label, int64_t _updatedAt) { + + strcpy(group, _location); + label = _label; + updatedAt = _updatedAt; + } + + + ~DeviceStateGroup() { + } + + + char* getGroup() { + return group; + } + + + string getLabel() { + return label; + } + + + int64_t getUpdatedAt() { + return updatedAt; + } +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateHostFirmware.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateHostFirmware.hpp new file mode 100644 index 0000000..495f628 --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateHostFirmware.hpp @@ -0,0 +1,35 @@ +#ifndef _DEVICESTATEHOSTFIRMWARE_HPP__ +#define _DEVICESTATEHOSTFIRMWARE_HPP__ +#include + +class DeviceStateHostFirmware { + // time of build in nanosecond accuracy + // after some tests + private: + int64_t build; + int64_t version; // firmware version + + public: + + DeviceStateHostFirmware(int64_t _build, int64_t _version) { + + build = _build; + version = _version; + } + + + ~DeviceStateHostFirmware() { + } + + + int64_t getBuild() { + return build; + } + + + int64_t getVersion() { + return version; + } + +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateHostInfo.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateHostInfo.hpp new file mode 100644 index 0000000..702433f --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateHostInfo.hpp @@ -0,0 +1,39 @@ +#ifndef _DEVICESTATEHOSTINFO_HPP__ +#define _DEVICESTATEHOSTINFO_HPP__ +#include + +class DeviceStateHostInfo { + private: + int64_t signal; + int64_t tx; + int64_t rx; + + public: + + DeviceStateHostInfo(int64_t _signal, int64_t _tx, int64_t _rx) { + + signal = _signal; + tx = _tx; + rx = _rx; + } + + + ~DeviceStateHostInfo() { + } + + + int64_t getSignal() { + return signal; + } + + + int64_t getTx() { + return tx; + } + + + int64_t getRx() { + return rx; + } +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateInfo.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateInfo.hpp new file mode 100644 index 0000000..66551dd --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateInfo.hpp @@ -0,0 +1,39 @@ +#ifndef _DEVICESTATEINFO_HPP__ +#define _DEVICESTATEINFO_HPP__ +#include + +class DeviceStateInfo { + private: + int64_t time; + int64_t upTime; + int64_t downTime; + + public: + + DeviceStateInfo(int64_t _time, int64_t _upTime, int64_t _downTime) { + + time = _time; + upTime = _upTime; + downTime = _downTime; + } + + + ~DeviceStateInfo() { + } + + + int64_t getTime() { + return time; + } + + + int64_t getUpTime() { + return upTime; + } + + + int64_t getDownTime() { + return downTime; + } +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateLocation.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateLocation.hpp new file mode 100644 index 0000000..aed44b7 --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateLocation.hpp @@ -0,0 +1,40 @@ +#ifndef _DEVICESTATELOCATION_HPP__ +#define _DEVICESTATELOCATION_HPP__ +#include +#include + +class DeviceStateLocation { + private: + char location[16]; + string label; + int64_t updatedAt; + + public: + + DeviceStateLocation(char _location[16], string _label, long _updatedAt) { + + strcpy(location, _location); + label = _label; + updatedAt = _updatedAt; + } + + + ~DeviceStateLocation() { + } + + + char* getLocation() { + return location; + } + + + string getLabel() { + return label; + } + + + int64_t getUpdatedAt() { + return updatedAt; + } +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateLocation.hpp~ b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateLocation.hpp~ new file mode 100644 index 0000000..b898319 --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateLocation.hpp~ @@ -0,0 +1,40 @@ +#ifndef _DEVICESTATELOCATION_HPP__ +#define _DEVICESTATELOCATION_HPP__ +#include +#include + +class DeviceStateLocation { + private: + char location[16]; + string label; + int64_t updatedAt; + + public: + + DeviceStateLocation(char _location[16], String _label, long _updatedAt) { + + strcpy(location, _location); + label = _label; + updatedAt = _updatedAt; + } + + + ~DeviceStateLocation() { + } + + + char* getLocation() { + return location; + } + + + string getLabel() { + return label; + } + + + int64_t getUpdatedAt() { + return updatedAt; + } +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateService.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateService.hpp new file mode 100644 index 0000000..2fee53c --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateService.hpp @@ -0,0 +1,32 @@ +#ifndef _DEVICESTATESERVICE_HPP__ +#define _DEVICESTATESERVICE_HPP__ +#include + +class DeviceStateService { + private: + int service; + int64_t port; + + public: + + DeviceStateService(int _service, long _port) { + + service = _service; + port = _port; + } + + + ~DeviceStateService() { + } + + + int getService() { + return service; + } + + + int64_t getPort() { + return port; + } +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateVersion.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateVersion.hpp new file mode 100644 index 0000000..4a138d6 --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateVersion.hpp @@ -0,0 +1,39 @@ +#ifndef _DEVICESTATEVERSION_HPP__ +#define _DEVICESTATEVERSION_HPP__ +#include + +class DeviceStateVersion { + private: + int64_t vender; + int64_t product; + int64_t version; + + public: + + DeviceStateVersion(int64_t _vender, int64_t _product, int64_t _version) { + + vender = _vender; + product = _product; + version = _version; + } + + + ~DeviceStateVersion() { + } + + + int64_t getVender() { + return vender; + } + + + int64_t getProduct() { + return product; + } + + + int64_t getVersion() { + return version; + } +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateWifiFirmware.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateWifiFirmware.hpp new file mode 100644 index 0000000..36ff4f3 --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateWifiFirmware.hpp @@ -0,0 +1,34 @@ +#ifndef _DEVICESTATEWIFIFIRMWARE_HPP__ +#define _DEVICESTATEWIFIFIRMWARE_HPP__ +#include + +class DeviceStateWifiFirmware { + // time of build in nanosecond accuracy + // after some tests + private: + int64_t build; + int64_t version; // firmware version + + public: + + DeviceStateWifiFirmware(int64_t _build, int64_t _version) { + + build = _build; + version = _version; + } + + + ~DeviceStateWifiFirmware() { + } + + + int64_t getBuild() { + return build; + } + + + int64_t getVersion() { + return version; + } +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateWifiInfo.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateWifiInfo.hpp new file mode 100644 index 0000000..500b5d1 --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/DeviceStateWifiInfo.hpp @@ -0,0 +1,39 @@ +#ifndef _DEVICESTATEWIFIINFO_HPP__ +#define _DEVICESTATEWIFIINFO_HPP__ +#include + +class DeviceStateWifiInfo { + private: + int64_t signal; + int64_t tx; + int64_t rx; + + public: + + DeviceStateWifiInfo(int64_t _signal, int64_t _tx, int64_t _rx) { + + signal = _signal; + tx = _tx; + rx = _rx; + } + + + ~DeviceStateWifiInfo() { + } + + + int64_t getSignal() { + return signal; + } + + + int64_t getTx() { + return tx; + } + + + int64_t getRx() { + return rx; + } +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/LifxHeader.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/LifxHeader.hpp new file mode 100644 index 0000000..d750281 --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/LifxHeader.hpp @@ -0,0 +1,287 @@ +#ifndef _LIFXHEADER_HPP__ +#define _LIFXHEADER_HPP__ +#include + +class LifxHeader { + + private: + // Frame variables + int size; + int origin; + bool tagged; + bool addressable; + int protocol; + int64_t source; + + // Frame address variables + char macAddress[8]; + bool ack_required; + bool res_required; + int sequence; + + // Protocol header + int type; + + public: + + LifxHeader() { + origin = 0; + addressable = true; + protocol =1024; + } + + + ~LifxHeader() { + } + + + void setSize(int _size) { + if (_size < 0) { + cerr << "Header: size cannot be less than 0" << endl; + exit(1); + } else if (_size > 65535) { + cerr << "Header: size too large" << endl; + exit(1); + } + size = _size; + } + + + void setOrigin(int _origin) { + if (_origin < 0) { + cerr << "Header: origin cannot be less than 0" << endl; + exit(1); + } else if (_origin > 3) { + cerr << "Header: origin too large" << endl; + exit(1); + } + origin = _origin; + } + + + void setTagged(bool _tagged) { + tagged = _tagged; + } + + + void setAddressable(bool _addressable) { + addressable = _addressable; + } + + + void setProtocol(int _protocol) { + if (_protocol < 0) { + cerr << "Header: protocol cannot be less than 0" << endl; + exit(1); + } else if (_protocol > 4095) { + cerr << "Header: protocol too large" << endl; + exit(1); + } + protocol = _protocol; + } + + + void setSource(int64_t _source) { + if (_source < 0) { + cerr << "Header: source cannot be less than 0" << endl; + exit(1); + } else if (_source > 4294967295) { + cerr << "Header: source too large" << endl; + exit(1); + } + source = _source; + } + + + void setSequence(int _sequence) { + if (_sequence < 0) { + cerr << "Header: sequence cannot be less than 0" << endl; + exit(1); + } else if (_sequence > 255) { + cerr << "Header: sequence too large" << endl; + exit(1); + } + sequence = _sequence; + } + + + void setType(int _type) { + if (_type < 0) { + cerr << "Header: type cannot be less than 0" << endl; + exit(1); + } else if (_type > 65535) { + cerr << "Header: type too large" << endl; + exit(1); + } + type = _type; + } + + + void setAck_required(bool _ack_required) { + ack_required = _ack_required; + } + + + void setRes_required(bool _res_required) { + res_required = _res_required; + } + + + void setMacAddress(char _macAddress[8]) { + strcpy(macAddress, _macAddress); + } + + + int getSize() { + return size; + } + + + int getOrigin() { + return origin; + } + + + bool getTagged() { + return tagged; + } + + + bool getAddressable() { + return addressable; + } + + + int getProtocol() { + return protocol; + } + + + int64_t getSource() { + return source; + } + + + int64_t getSequence() { + return sequence; + } + + + int getType() { + return type; + } + + + char* getHeaderBytes(char headerBytes[36]) { + + //char headerBytes[36]; + headerBytes[0] = (char)(size & 0xFF); + headerBytes[1] = (char)((size >> 8) & 0xFF); + + + headerBytes[2] = (char)(protocol & 0xFF); + headerBytes[3] = (char)((protocol >> 8) & 0x0F); + + headerBytes[3] |= (char)((origin & 0x03) << 6); + + if (tagged) { + headerBytes[3] |= (1 << 5); + } + + if (addressable) { + headerBytes[3] |= (1 << 4); + } + + headerBytes[4] = (char)((source >> 0) & 0xFF); + headerBytes[5] = (char)((source >> 8) & 0xFF); + headerBytes[6] = (char)((source >> 16) & 0xFF); + headerBytes[7] = (char)((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] = (char)(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] = (char)((type >> 0) & 0xFF); + headerBytes[33] = (char)((type >> 8) & 0xFF); + + // Reserved and set to 0 + headerBytes[34] = 0; + headerBytes[35] = 0; + + return headerBytes; + } + + + void setFromBytes(char dataBytes[36]) { + + 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); + } +}; +#endif diff --git a/benchmarks/drivers/LifxLightBulb/LifxLightBulb.config b/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.config similarity index 100% rename from benchmarks/drivers/LifxLightBulb/LifxLightBulb.config rename to benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.config diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp b/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp new file mode 100644 index 0000000..21b209c --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp @@ -0,0 +1,25 @@ +#include +#include +#include "LifxLightBulb.hpp" +#include "IoTSet.hpp" +#include "IoTDeviceAddress.hpp" + +using namespace std; + +int main(int argc, char *argv[]) +{ + string macAddress = "D073D5128E300000"; + string devIPAddress = "192.168.2.126"; + IoTDeviceAddress devAddress(devIPAddress, 12345, 56700, false, false); + unordered_set myset = { devAddress }; + + IoTSet setDevAddress(myset); + LifxLightBulb *llb = new LifxLightBulb(setDevAddress, macAddress); + llb->init(); + + cout << "Generated LifxLightBulb object!" << endl; + + delete llb; + + return 0; +} diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.hpp new file mode 100644 index 0000000..a70a982 --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.hpp @@ -0,0 +1,1191 @@ +#ifndef _LIFXLIGHTBULB_HPP__ +#define _LIFXLIGHTBULB_HPP__ +#include +#include +#include +#include +#include + +#include +#include + +#include "LightBulb.hpp" +#include "Socket.hpp" +#include "IoTRMIUtil.hpp" +#include "IoTSet.hpp" +#include "IoTUDP.hpp" +#include "IoTDeviceAddress.hpp" +#include "Iterator.hpp" + +// Helper classes for LifxLightBulb +#include "LifxHeader.hpp" +#include "BulbColor.hpp" +#include "DeviceStateGroup.hpp" +#include "DeviceStateHostFirmware.hpp" +#include "DeviceStateHostInfo.hpp" +#include "DeviceStateInfo.hpp" +#include "DeviceStateLocation.hpp" +#include "DeviceStateService.hpp" +#include "DeviceStateVersion.hpp" +#include "DeviceStateWifiFirmware.hpp" +#include "DeviceStateWifiInfo.hpp" +#include "LightState.hpp" + + +using namespace std; + +// Driver LifxLightBulb +// Implemented based on LightBulb virtual class (interface) + +//std::atomic +std::atomic didAlreadyInit(false); +std::atomic didGetBulbVersion(false); + +class LifxLightBulb //: public LightBulb +{ + private: + // Constants + const static int64_t GET_BULB_VERSION_RESEND_WAIT_SECONDS = 10; + + // Variables + IoTUDP *communicationSocket; + char bulbMacAddress[8]; + //TODO: + //static Semaphore socketMutex = new Semaphore(1); + bool sendSocketFlag = false; + int64_t lastSentGetBulbVersionRequest = 0; // time last request sent + + // Current Bulb Values + int currentHue = 0; + int currentSaturation = 0; + int currentBrightness = 65535; + int currentTemperature = 9000; + bool bulbIsOn = false; + + //std::atomic + atomic didAlreadyInit; + atomic didGetBulbVersion; + + // Mutex locks + mutex socketMutex; + mutex settingBulbColorMutex; + mutex settingBulbTemperatureMutex; + mutex bulbStateMutex; + + // color and temperature ranges for the bulbs + int hueLowerBound = 0; + int hueUpperBound = 0; + int saturationLowerBound = 0; + int saturationUpperBound = 0; + int brightnessLowerBound = 0; + int brightnessUpperBound = 0; + int temperatureLowerBound = 2500; + 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 + bool stateDidChange = false; + + // Device address + IoTSet lb_addresses; + + public: + + // Constructor + LifxLightBulb() { + // LB1 macAddress: d0:73:d5:12:8e:30 + // LB1 macAddress: d0:73:d5:02:41:da + string macAddress = "D073D5128E300000"; // bulbMacAddress: [-48, 115, -43, 18, -114, 48, 0, 0] + //string macAddress = "D073D50241DA0000"; // bulbMacAddress: [-48, 115, -43, 2, 65, -38, 0, 0] + /*bulbMacAddress[0] = 0xD0; + bulbMacAddress[1] = 0x73; + bulbMacAddress[2] = 0xD5; + bulbMacAddress[3] = 0x02; + bulbMacAddress[4] = 0x41; + bulbMacAddress[5] = 0xDA; + bulbMacAddress[6] = 0x00; + bulbMacAddress[7] = 0x00;*/ + + char tmpMacAddress[16]; + strcpy(tmpMacAddress, macAddress.c_str()); + //test[0] = (char) strtol(strTest.c_str(), NULL, 16); + for(int i=0; i<16; i=i+2) { + // Take 2 digits and then convert + char tmpMacByte[2]; + tmpMacByte[0] = tmpMacAddress[i]; + tmpMacByte[1] = tmpMacAddress[i+1]; + bulbMacAddress[i/2] = (char) strtol(tmpMacByte, NULL, 16); + } + //IoTRMIUtil::printBytes(bulbMacAddress, 8, false); + } + + + LifxLightBulb(IoTSet _devAddress, string macAddress) { + + // Initialize macAddress + char tmpMacAddress[16]; + strcpy(tmpMacAddress, macAddress.c_str()); + //test[0] = (char) strtol(strTest.c_str(), NULL, 16); + for(int i=0; i<16; i=i+2) { + // Take 2 digits and then convert + char tmpMacByte[2]; + tmpMacByte[0] = tmpMacAddress[i]; + tmpMacByte[1] = tmpMacAddress[i+1]; + bulbMacAddress[i/2] = (char) strtol(tmpMacByte, NULL, 16); + } + cout << "MAC address is set. Value: "; + IoTRMIUtil::printBytes(bulbMacAddress, 8, false); + + // Initialize device address + lb_addresses = _devAddress; + cout << "Device address is set! " << endl; + } + + + ~LifxLightBulb() { + + // Clean up + if (communicationSocket != NULL) { + + delete communicationSocket; + communicationSocket = NULL; + } + } + + + // Initialize the lightbulb + void init() { + + if (didAlreadyInit.exchange(true)) + return; + + unordered_set::const_iterator itr = lb_addresses.begin(); + IoTDeviceAddress deviceAddress = *itr; + cout << "Address: " << deviceAddress.getAddress() << endl; + + // Create IoTUDP socket + communicationSocket = new IoTUDP(deviceAddress); + + // Launch the worker function in a separate thread. + thread th1 (&LifxLightBulb::workerFunction, this); + th1.join(); + } + + + void turnOff() { + + lock_guard guard(bulbStateMutex); + bulbIsOn = false; + sendSetLightPowerPacket(0, 0); + stateDidChange = true; + cout << "Turning off lightbulb!" << endl; + } + + + void turnOn() { + + lock_guard guard(bulbStateMutex); + bulbIsOn = true; + sendSetLightPowerPacket(65535, 0); + stateDidChange = true; + } + + + double getHue() { + double tmp = 0; + settingBulbColorMutex.lock(); + tmp = ((double)currentHue / 65535.0) * 360.0; + settingBulbColorMutex.unlock(); + + return tmp; + } + + + double getSaturation() { + double tmp = 0; + settingBulbColorMutex.lock(); + tmp = ((double)currentSaturation / 65535.0) * 360.0; + settingBulbColorMutex.unlock(); + + return tmp; + } + + + double getBrightness() { + double tmp = 0; + settingBulbColorMutex.lock(); + tmp = ((double)currentBrightness / 65535.0) * 360.0; + settingBulbColorMutex.unlock(); + + return tmp; + } + + + int getTemperature() { + + int tmp = 0; + settingBulbTemperatureMutex.lock(); + tmp = currentTemperature; + settingBulbTemperatureMutex.unlock(); + + return tmp; + } + + + double getHueRangeLowerBound() { + if (!didGetBulbVersion) { + return -1; + } + return ((double)hueLowerBound / 65535.0) * 360.0; + } + + + double getHueRangeUpperBound() { + if (!didGetBulbVersion) { + return -1; + } + return ((double)hueUpperBound / 65535.0) * 360.0; + } + + + double getSaturationRangeLowerBound() { + if (!didGetBulbVersion) { + return -1; + } + return ((double)saturationLowerBound / 65535.0) * 100.0; + } + + + double getSaturationRangeUpperBound() { + if (!didGetBulbVersion) { + return -1; + } + return ((double)saturationUpperBound / 65535.0) * 100.0; + } + + + double getBrightnessRangeLowerBound() { + if (!didGetBulbVersion) { + return -1; + } + return ((double)brightnessLowerBound / 65535.0) * 100.0; + } + + + double getBrightnessRangeUpperBound() { + if (!didGetBulbVersion) { + return -1; + } + return ((double)brightnessUpperBound / 65535.0) * 100.0; + } + + + int getTemperatureRangeLowerBound() { + if (!didGetBulbVersion) { + return -1; + } + return temperatureLowerBound; + } + + + int getTemperatureRangeUpperBound() { + if (!didGetBulbVersion) { + return -1; + } + return temperatureUpperBound; + } + + + void setTemperature(int _temperature) { + + settingBulbTemperatureMutex.lock(); + + BulbColor* newColor = new BulbColor(currentHue, currentSaturation, currentBrightness, _temperature); + sendSetLightColorPacket(newColor, 250); + + currentTemperature = _temperature; + stateDidChange = true; + + settingBulbTemperatureMutex.unlock(); + } + + + void setColor(double _hue, double _saturation, double _brightness) { + + settingBulbColorMutex.lock(); + + _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.unlock(); + } + + + bool getState() { + + bool tmp = false; + + bulbStateMutex.lock(); + tmp = bulbIsOn; + bulbStateMutex.unlock(); + + return tmp; + } + + private: + // Private functions + + // Communication helpers + void receivedPacket(char* packetData) { + + char headerBytes[36]; + for (int i = 0; i < 36; i++) { + headerBytes[i] = packetData[i]; + } + + LifxHeader recHeader; + recHeader.setFromBytes(headerBytes); + + // load the payload bytes (strip away the header) + char payloadBytes[recHeader.getSize()]; + for (int i = 36; i < recHeader.getSize(); i++) { + payloadBytes[i - 36] = packetData[i]; + } + + int type = recHeader.getType(); + cout << "Received: " << type; + + DeviceStateService* dat = NULL; + switch (type) { + + case 3: + dat = parseDeviceStateServiceMessage(payloadBytes); + cout << "Service: " << dat->getService(); + cout << "Port : " << dat->getPort(); + // Avoid memory leak - delete this object + delete dat; + break; + + case 33: + handleStateVersionMessageReceived(payloadBytes); + break; + + case 35: + parseDeviceStateInfoMessage(payloadBytes); + break; + + + case 107: + handleLightStateMessageReceived(payloadBytes); + break; + + default: + cout << "unknown packet Type" << endl; + } + + } + + + void sendPacket(char* packetData, int len) { + //cout << "sendPacket: About to send" << endl; + lock_guard guard(socketMutex); + sendSocketFlag = true; + communicationSocket->sendData(packetData, len); + sendSocketFlag = false; + } + + + // Worker function which runs the while loop for receiving data from the bulb. + // Is blocking. + void workerFunction() { + LifxHeader h; + // Need timeout on receives since we are not sure if a packet will be available + // for processing so don't block waiting + // TODO: Check if we can do this here! + //communicationSocket.setSoTimeout(50); + + turnOff(); + + while (true) { + + // Check if we got the bulb version yet + // could have requested it but message could have gotten lost (UDP) + if (!didGetBulbVersion) { + int64_t currentTime = (int64_t) time(NULL); + 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; + } + + /*socketMutex.lock(); + + char dat[1024]; + dat = communicationSocket.receiveData(1024); + + // Never forget to release! + socketMutex.unlock(); + + // A packed arrived + if (dat != null) { + receivedPacket(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 + this_thread::sleep_for (chrono::milliseconds(100)); + } catch (Exception e) { + + }*/ + + + } + } + + + // Sending + // Device Messages + void sendGetServicePacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendGetHostInfoPacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendGetHostFirmwarePacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendGetWifiInfoPacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendGetWifiFirmwarePacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendGetPowerPacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendSetPowerPacket(int level) { + // Currently only 0 and 65535 are supported + // This is a fix for now + if ((level != 65535) && (level != 0)) { + cerr << "Invalid parameter values" << endl; + exit(1); + } + + if ((level > 65535) || (level < 0)) { + cerr << "Invalid parameter values" << endl; + exit(1); + } + + char packetBytes[38]; + + LifxHeader header; + 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); + char headerBytes[36]; + header.getHeaderBytes(headerBytes); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + packetBytes[36] = (char)(level & 0xFF); + packetBytes[37] = (char)((level >> 8) & 0xFF); + + sendPacket(packetBytes, 38); + } + + + void sendGetLabelPacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendSetLabelPacket(string label) { + // Currently only 0 and 65535 are supported + // This is a fix for now + if (label.length() != 32) { + cerr << "Invalid parameter values, label must be 32 bytes long" << endl; + exit(1); + } + + char packetBytes[68]; + + LifxHeader header; + 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); + char headerBytes[36]; + header.getHeaderBytes(headerBytes); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + for (int i = 0; i < 32; i++) { + packetBytes[i + 36] = label.c_str()[i]; + } + + sendPacket(packetBytes, 68); + } + + + void sendGetVersionPacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendGetInfoPacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendGetLocationPacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendGetGroupPacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + // Sending + // Light Messages + void sendGetLightStatePacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendSetLightColorPacket(BulbColor* bulbColor, long duration) { + + if ((duration > 4294967295l) || (duration < 0)) { + cerr << "Invalid parameter value, duration out of range (0 - 4294967295)" << endl; + exit(1); + } + + char packetBytes[49]; + + LifxHeader header; + 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); + char headerBytes[36]; + header.getHeaderBytes(headerBytes); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + // 1 reserved packet + packetBytes[37] = (char)(bulbColor->getHue() & 0xFF); + packetBytes[38] = (char)((bulbColor->getHue() >> 8) & 0xFF); + + packetBytes[39] = (char)(bulbColor->getSaturation() & 0xFF); + packetBytes[40] = (char)((bulbColor->getSaturation() >> 8) & 0xFF); + + packetBytes[41] = (char)(bulbColor->getBrightness() & 0xFF); + packetBytes[42] = (char)((bulbColor->getBrightness() >> 8) & 0xFF); + + packetBytes[43] = (char)(bulbColor->getKelvin() & 0xFF); + packetBytes[44] = (char)((bulbColor->getKelvin() >> 8) & 0xFF); + + packetBytes[45] = (char)((duration >> 0) & 0xFF); + packetBytes[46] = (char)((duration >> 8) & 0xFF); + packetBytes[47] = (char)((duration >> 16) & 0xFF); + packetBytes[48] = (char)((duration >> 24) & 0xFF); + + sendPacket(packetBytes, 49); + // Avoid memory leak - delete object + delete bulbColor; + } + + + void sendGetLightPowerPacket() { + LifxHeader header; + 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); + + char dataBytes[36]; + header.getHeaderBytes(dataBytes); + + sendPacket(dataBytes, 36); + } + + + void sendSetLightPowerPacket(int level, long duration) { + + if ((level > 65535) || (duration > 4294967295l) + || (level < 0) || (duration < 0)) { + cerr << "Invalid parameter values" << endl; + exit(1); + } + + char packetBytes[42]; + + + LifxHeader header; + 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); + char headerBytes[36]; + header.getHeaderBytes(headerBytes); + + for (int i = 0; i < 36; i++) { + packetBytes[i] = headerBytes[i]; + } + + packetBytes[36] = (char)(level & 0xFF); + packetBytes[37] = (char)((level >> 8) & 0xFF); + + packetBytes[38] = (char)((duration >> 0) & 0xFF); + packetBytes[39] = (char)((duration >> 8) & 0xFF); + packetBytes[40] = (char)((duration >> 16) & 0xFF); + packetBytes[41] = (char)((duration >> 24) & 0xFF); + + sendPacket(packetBytes, 42); + } + + + void sendEchoRequestPacket(char data[64]) { + + char packetBytes[100]; + + LifxHeader header; + 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); + char headerBytes[36]; + header.getHeaderBytes(headerBytes); + + 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, 100); + } + + + // Receiving + // Device Messages + DeviceStateService* parseDeviceStateServiceMessage(char* payloadData) { + int service = payloadData[0]; + int64_t port = ((payloadData[3] & 0xFF) << 24); + port |= ((payloadData[2] & 0xFF) << 16); + port |= ((payloadData[1] & 0xFF) << 8); + port |= (payloadData[0] & 0xFF); + + return new DeviceStateService(service, port); + } + + + DeviceStateHostInfo* parseDeviceStateHostInfoMessage(char* 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); + } + + + DeviceStateHostFirmware* parseDeviceStateHostFirmwareMessage(char* payloadData) { + long build = 0; + for (int i = 0; i < 8; i++) { + build += ((int64_t) payloadData[i] & 0xffL) << (8 * i); + } + + // 8 reserved bytes + + int64_t version = ((payloadData[19] & 0xFF) << 24); + version |= ((payloadData[18] & 0xFF) << 16); + version |= ((payloadData[17] & 0xFF) << 8); + version |= (payloadData[16] & 0xFF); + + return new DeviceStateHostFirmware(build, version); + } + + + DeviceStateWifiInfo* parseDeviceStateWifiInfoMessage(char* payloadData) { + int64_t signal = ((payloadData[3] & 0xFF) << 24); + signal |= ((payloadData[2] & 0xFF) << 16); + signal |= ((payloadData[1] & 0xFF) << 8); + signal |= (payloadData[0] & 0xFF); + + int64_t tx = ((payloadData[7] & 0xFF) << 24); + tx |= ((payloadData[6] & 0xFF) << 16); + tx |= ((payloadData[5] & 0xFF) << 8); + tx |= (payloadData[4] & 0xFF); + + int64_t 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); + } + + + DeviceStateWifiFirmware* parseDeviceStateWifiFirmwareMessage(char* payloadData) { + long build = 0; + for (int i = 0; i < 8; i++) { + build += ((int64_t) payloadData[i] & 0xffL) << (8 * i); + } + + // 8 reserved bytes + + int64_t version = ((payloadData[19] & 0xFF) << 24); + version |= ((payloadData[18] & 0xFF) << 16); + version |= ((payloadData[17] & 0xFF) << 8); + version |= (payloadData[16] & 0xFF); + + return new DeviceStateWifiFirmware(build, version); + } + + + int parseStatePowerMessage(char* payloadData) { + int level = ((payloadData[1] & 0xFF) << 8); + level |= (payloadData[0] & 0xFF); + return level; + } + + + DeviceStateVersion* parseDeviceStateVersionMessage(char* payloadData) { + int64_t vender = ((payloadData[3] & 0xFF) << 24); + vender |= ((payloadData[2] & 0xFF) << 16); + vender |= ((payloadData[1] & 0xFF) << 8); + vender |= (payloadData[0] & 0xFF); + + int64_t product = ((payloadData[7] & 0xFF) << 24); + product |= ((payloadData[6] & 0xFF) << 16); + product |= ((payloadData[5] & 0xFF) << 8); + product |= (payloadData[4] & 0xFF); + + int64_t 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); + } + + + DeviceStateInfo* parseDeviceStateInfoMessage(char* payloadData) { + int64_t time = 0; + int64_t upTime = 0; + int64_t downTime = 0; + for (int i = 0; i < 8; i++) { + time += ((int64_t) payloadData[i] & 0xffL) << (8 * i); + upTime += ((int64_t) payloadData[i + 8] & 0xffL) << (8 * i); + downTime += ((int64_t) payloadData[i + 16] & 0xffL) << (8 * i); + } + + return new DeviceStateInfo(time, upTime, downTime); + } + + + DeviceStateLocation* parseDeviceStateLocationMessage(char* payloadData) { + char location[16]; + for (int i = 0; i < 16; i++) { + location[i] = payloadData[i]; + } + + char labelBytes[32]; + for (int i = 0; i < 32; i++) { + labelBytes[i] = payloadData[i + 16]; + } + + int64_t updatedAt = 0; + for (int i = 0; i < 8; i++) { + updatedAt += ((int64_t) payloadData[48] & 0xffL) << (8 * i); + } + + string str(labelBytes); + return new DeviceStateLocation(location, str, updatedAt); + } + + + DeviceStateGroup* parseDeviceStateGroupMessage(char* payloadData) { + char group[16]; + for (int i = 0; i < 16; i++) { + group[i] = payloadData[i]; + } + + char labelBytes[32]; + for (int i = 0; i < 32; i++) { + labelBytes[i] = payloadData[i + 16]; + } + + int64_t updatedAt = 0; + for (int i = 0; i < 8; i++) { + updatedAt += ((int64_t) payloadData[48] & 0xffL) << (8 * i); + } + + string str(labelBytes); + return new DeviceStateGroup(group, str, updatedAt); + } + + + // Receiving + // Light Messages + LightState* parseLightStateMessage(char* payloadData) { + + char colorData[8]; + for (int i = 0; i < 8; i++) { + colorData[i] = payloadData[i]; + } + BulbColor color(colorData); + + int power = ((payloadData[11] & 0xFF) << 8); + power |= (payloadData[10] & 0xFF); + + string label(payloadData); + + char labelArray[32]; + for (int i = 0; i < 32; i++) { + labelArray[i] = payloadData[12 + i]; + } + + return new LightState(&color, power, label); + } + + + int parseLightStatePowerMessage(char* payloadData) { + int level = ((payloadData[1] & 0xFF) << 8); + level |= (payloadData[0] & 0xFF); + return level; + } + + + // Private Handlers + void handleStateVersionMessageReceived(char* payloadData) { + + DeviceStateVersion* deviceState = parseDeviceStateVersionMessage(payloadData); + int productNumber = (int)deviceState->getProduct(); + + bool 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.exchange(true); + // Avoid memory leak - delete this object + delete deviceState; + } + + + void handleLightStateMessageReceived(char* payloadData) { + LightState* lightState = parseLightStateMessage(payloadData); + + BulbColor* color = lightState->getColor(); + int power = lightState->getPower(); + + bool 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"); + } + + bulbStateMutex.lock(); + bool bulbIsOnTmp = bulbIsOn; + bulbStateMutex.unlock(); + + 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)); + + } + // Avoid memory leak - delete object + delete lightState; + delete color; + } + +}; +#endif diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/LightState.hpp b/benchmarks/drivers/Cpp/LifxLightBulb/LightState.hpp new file mode 100644 index 0000000..3745c8a --- /dev/null +++ b/benchmarks/drivers/Cpp/LifxLightBulb/LightState.hpp @@ -0,0 +1,41 @@ +#ifndef _LIGHTSTATE_HPP__ +#define _LIGHTSTATE_HPP__ +#include + +#include "BulbColor.hpp" + +class LightState { + private: + BulbColor* color; + int power; + string label; + + public: + + LightState(BulbColor* _color, int _power, string _label) { + + color = _color; + power = _power; + label = _label; + } + + + ~LightState() { + } + + + BulbColor* getColor() { + return color; + } + + + int getPower() { + return power; + } + + + string getLabel() { + return label; + } +}; +#endif diff --git a/benchmarks/drivers/Cpp/Makefile b/benchmarks/drivers/Cpp/Makefile new file mode 100644 index 0000000..a14df94 --- /dev/null +++ b/benchmarks/drivers/Cpp/Makefile @@ -0,0 +1,14 @@ +BASE = ../../.. + +include $(BASE)/common.mk + +all: light + +# Compile +# +PHONY += light +light: + cd LifxLightBulb/; $(G++) ./LifxLightBulb.cpp $(BASE)/../iotjava/iotruntime/cpp/socket/Socket.cpp -o ../$(BIN_DIR)/iotcode/LifxLightBulb/LifxLightBulb.o --std=c++11 -pthread -pg -I$(BASE)/../iotjava/iotruntime/cpp/ -I$(BASE)/../iotjava/iotruntime/cpp/socket/ -I$(BASE)/../iotjava/iotruntime/cpp/setrelation/ -I$(BASE)/../iotjava/iotrmi/C++/ -I$(BASE)/../benchmarks/virtuals/ + cp LifxLightBulb/LifxLightBulb.config $(BIN_DIR)/iotcode/LifxLightBulb + +.PHONY: $(PHONY) diff --git a/benchmarks/drivers/AmcrestCamera/AmcrestCamera.config b/benchmarks/drivers/Java/AmcrestCamera/AmcrestCamera.config similarity index 100% rename from benchmarks/drivers/AmcrestCamera/AmcrestCamera.config rename to benchmarks/drivers/Java/AmcrestCamera/AmcrestCamera.config diff --git a/benchmarks/drivers/AmcrestCamera/AmcrestCamera.java b/benchmarks/drivers/Java/AmcrestCamera/AmcrestCamera.java similarity index 100% rename from benchmarks/drivers/AmcrestCamera/AmcrestCamera.java rename to benchmarks/drivers/Java/AmcrestCamera/AmcrestCamera.java diff --git a/benchmarks/drivers/AmcrestCamera/CameraSmartCallback_CallbackStub.java b/benchmarks/drivers/Java/AmcrestCamera/CameraSmartCallback_CallbackStub.java similarity index 100% rename from benchmarks/drivers/AmcrestCamera/CameraSmartCallback_CallbackStub.java rename to benchmarks/drivers/Java/AmcrestCamera/CameraSmartCallback_CallbackStub.java diff --git a/benchmarks/drivers/AmcrestCamera/Camera_Skeleton.java b/benchmarks/drivers/Java/AmcrestCamera/Camera_Skeleton.java similarity index 100% rename from benchmarks/drivers/AmcrestCamera/Camera_Skeleton.java rename to benchmarks/drivers/Java/AmcrestCamera/Camera_Skeleton.java diff --git a/benchmarks/drivers/AudioRoom/AudioRoom.config b/benchmarks/drivers/Java/AudioRoom/AudioRoom.config similarity index 100% rename from benchmarks/drivers/AudioRoom/AudioRoom.config rename to benchmarks/drivers/Java/AudioRoom/AudioRoom.config diff --git a/benchmarks/drivers/AudioRoom/AudioRoom.java b/benchmarks/drivers/Java/AudioRoom/AudioRoom.java similarity index 100% rename from benchmarks/drivers/AudioRoom/AudioRoom.java rename to benchmarks/drivers/Java/AudioRoom/AudioRoom.java diff --git a/benchmarks/drivers/AudioRoom/Room_Skeleton.java b/benchmarks/drivers/Java/AudioRoom/Room_Skeleton.java similarity index 100% rename from benchmarks/drivers/AudioRoom/Room_Skeleton.java rename to benchmarks/drivers/Java/AudioRoom/Room_Skeleton.java diff --git a/benchmarks/drivers/EspAlarm/Alarm_Skeleton.java b/benchmarks/drivers/Java/EspAlarm/Alarm_Skeleton.java similarity index 100% rename from benchmarks/drivers/EspAlarm/Alarm_Skeleton.java rename to benchmarks/drivers/Java/EspAlarm/Alarm_Skeleton.java diff --git a/benchmarks/drivers/EspAlarm/EspAlarm.config b/benchmarks/drivers/Java/EspAlarm/EspAlarm.config similarity index 100% rename from benchmarks/drivers/EspAlarm/EspAlarm.config rename to benchmarks/drivers/Java/EspAlarm/EspAlarm.config diff --git a/benchmarks/drivers/EspAlarm/EspAlarm.java b/benchmarks/drivers/Java/EspAlarm/EspAlarm.java similarity index 100% rename from benchmarks/drivers/EspAlarm/EspAlarm.java rename to benchmarks/drivers/Java/EspAlarm/EspAlarm.java diff --git a/benchmarks/drivers/EspSprinkler/EspSprinkler.config b/benchmarks/drivers/Java/EspSprinkler/EspSprinkler.config similarity index 100% rename from benchmarks/drivers/EspSprinkler/EspSprinkler.config rename to benchmarks/drivers/Java/EspSprinkler/EspSprinkler.config diff --git a/benchmarks/drivers/EspSprinkler/EspSprinkler.java b/benchmarks/drivers/Java/EspSprinkler/EspSprinkler.java similarity index 100% rename from benchmarks/drivers/EspSprinkler/EspSprinkler.java rename to benchmarks/drivers/Java/EspSprinkler/EspSprinkler.java diff --git a/benchmarks/drivers/EspSprinkler/Sprinkler_Skeleton.java b/benchmarks/drivers/Java/EspSprinkler/Sprinkler_Skeleton.java similarity index 100% rename from benchmarks/drivers/EspSprinkler/Sprinkler_Skeleton.java rename to benchmarks/drivers/Java/EspSprinkler/Sprinkler_Skeleton.java diff --git a/benchmarks/drivers/GPSPhoneGateway/GPSGatewaySmartCallback_CallbackStub.java b/benchmarks/drivers/Java/GPSPhoneGateway/GPSGatewaySmartCallback_CallbackStub.java similarity index 100% rename from benchmarks/drivers/GPSPhoneGateway/GPSGatewaySmartCallback_CallbackStub.java rename to benchmarks/drivers/Java/GPSPhoneGateway/GPSGatewaySmartCallback_CallbackStub.java diff --git a/benchmarks/drivers/GPSPhoneGateway/GPSGateway_Skeleton.java b/benchmarks/drivers/Java/GPSPhoneGateway/GPSGateway_Skeleton.java similarity index 100% rename from benchmarks/drivers/GPSPhoneGateway/GPSGateway_Skeleton.java rename to benchmarks/drivers/Java/GPSPhoneGateway/GPSGateway_Skeleton.java diff --git a/benchmarks/drivers/GPSPhoneGateway/GPSPhoneGateway.config b/benchmarks/drivers/Java/GPSPhoneGateway/GPSPhoneGateway.config similarity index 100% rename from benchmarks/drivers/GPSPhoneGateway/GPSPhoneGateway.config rename to benchmarks/drivers/Java/GPSPhoneGateway/GPSPhoneGateway.config diff --git a/benchmarks/drivers/GPSPhoneGateway/GPSPhoneGateway.java b/benchmarks/drivers/Java/GPSPhoneGateway/GPSPhoneGateway.java similarity index 100% rename from benchmarks/drivers/GPSPhoneGateway/GPSPhoneGateway.java rename to benchmarks/drivers/Java/GPSPhoneGateway/GPSPhoneGateway.java diff --git a/benchmarks/drivers/GPSPhoneGateway/PhoneInfo.java b/benchmarks/drivers/Java/GPSPhoneGateway/PhoneInfo.java similarity index 100% rename from benchmarks/drivers/GPSPhoneGateway/PhoneInfo.java rename to benchmarks/drivers/Java/GPSPhoneGateway/PhoneInfo.java diff --git a/benchmarks/drivers/GPSPhoneGateway/PhoneInfoInterface.java b/benchmarks/drivers/Java/GPSPhoneGateway/PhoneInfoInterface.java similarity index 100% rename from benchmarks/drivers/GPSPhoneGateway/PhoneInfoInterface.java rename to benchmarks/drivers/Java/GPSPhoneGateway/PhoneInfoInterface.java diff --git a/benchmarks/drivers/GreenLawn/GreenLawn.config b/benchmarks/drivers/Java/GreenLawn/GreenLawn.config similarity index 100% rename from benchmarks/drivers/GreenLawn/GreenLawn.config rename to benchmarks/drivers/Java/GreenLawn/GreenLawn.config diff --git a/benchmarks/drivers/GreenLawn/GreenLawn.java b/benchmarks/drivers/Java/GreenLawn/GreenLawn.java similarity index 100% rename from benchmarks/drivers/GreenLawn/GreenLawn.java rename to benchmarks/drivers/Java/GreenLawn/GreenLawn.java diff --git a/benchmarks/drivers/GreenLawn/Lawn_Skeleton.java b/benchmarks/drivers/Java/GreenLawn/Lawn_Skeleton.java similarity index 100% rename from benchmarks/drivers/GreenLawn/Lawn_Skeleton.java rename to benchmarks/drivers/Java/GreenLawn/Lawn_Skeleton.java diff --git a/benchmarks/drivers/HomeRoom/HomeRoom.config b/benchmarks/drivers/Java/HomeRoom/HomeRoom.config similarity index 100% rename from benchmarks/drivers/HomeRoom/HomeRoom.config rename to benchmarks/drivers/Java/HomeRoom/HomeRoom.config diff --git a/benchmarks/drivers/HomeRoom/HomeRoom.java b/benchmarks/drivers/Java/HomeRoom/HomeRoom.java similarity index 100% rename from benchmarks/drivers/HomeRoom/HomeRoom.java rename to benchmarks/drivers/Java/HomeRoom/HomeRoom.java diff --git a/benchmarks/drivers/HomeRoom/Room_Skeleton.java b/benchmarks/drivers/Java/HomeRoom/Room_Skeleton.java similarity index 100% rename from benchmarks/drivers/HomeRoom/Room_Skeleton.java rename to benchmarks/drivers/Java/HomeRoom/Room_Skeleton.java diff --git a/benchmarks/drivers/IHome/IHome.config b/benchmarks/drivers/Java/IHome/IHome.config similarity index 100% rename from benchmarks/drivers/IHome/IHome.config rename to benchmarks/drivers/Java/IHome/IHome.config diff --git a/benchmarks/drivers/IHome/IHome.java b/benchmarks/drivers/Java/IHome/IHome.java similarity index 100% rename from benchmarks/drivers/IHome/IHome.java rename to benchmarks/drivers/Java/IHome/IHome.java diff --git a/benchmarks/drivers/IHome/SpeakerSmartCallback_CallbackStub.java b/benchmarks/drivers/Java/IHome/SpeakerSmartCallback_CallbackStub.java similarity index 100% rename from benchmarks/drivers/IHome/SpeakerSmartCallback_CallbackStub.java rename to benchmarks/drivers/Java/IHome/SpeakerSmartCallback_CallbackStub.java diff --git a/benchmarks/drivers/IHome/Speaker_Skeleton.java b/benchmarks/drivers/Java/IHome/Speaker_Skeleton.java similarity index 100% rename from benchmarks/drivers/IHome/Speaker_Skeleton.java rename to benchmarks/drivers/Java/IHome/Speaker_Skeleton.java diff --git a/benchmarks/drivers/LabRoom/LabRoom.config b/benchmarks/drivers/Java/LabRoom/LabRoom.config similarity index 100% rename from benchmarks/drivers/LabRoom/LabRoom.config rename to benchmarks/drivers/Java/LabRoom/LabRoom.config diff --git a/benchmarks/drivers/LabRoom/LabRoom.java b/benchmarks/drivers/Java/LabRoom/LabRoom.java similarity index 100% rename from benchmarks/drivers/LabRoom/LabRoom.java rename to benchmarks/drivers/Java/LabRoom/LabRoom.java diff --git a/benchmarks/drivers/LabRoom/Room_Skeleton.java b/benchmarks/drivers/Java/LabRoom/Room_Skeleton.java similarity index 100% rename from benchmarks/drivers/LabRoom/Room_Skeleton.java rename to benchmarks/drivers/Java/LabRoom/Room_Skeleton.java diff --git a/benchmarks/drivers/LifxLightBulb/BulbColor.java b/benchmarks/drivers/Java/LifxLightBulb/BulbColor.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/BulbColor.java rename to benchmarks/drivers/Java/LifxLightBulb/BulbColor.java diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateGroup.java b/benchmarks/drivers/Java/LifxLightBulb/DeviceStateGroup.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/DeviceStateGroup.java rename to benchmarks/drivers/Java/LifxLightBulb/DeviceStateGroup.java diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateHostFirmware.java b/benchmarks/drivers/Java/LifxLightBulb/DeviceStateHostFirmware.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/DeviceStateHostFirmware.java rename to benchmarks/drivers/Java/LifxLightBulb/DeviceStateHostFirmware.java diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateHostInfo.java b/benchmarks/drivers/Java/LifxLightBulb/DeviceStateHostInfo.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/DeviceStateHostInfo.java rename to benchmarks/drivers/Java/LifxLightBulb/DeviceStateHostInfo.java diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateInfo.java b/benchmarks/drivers/Java/LifxLightBulb/DeviceStateInfo.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/DeviceStateInfo.java rename to benchmarks/drivers/Java/LifxLightBulb/DeviceStateInfo.java diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateLocation.java b/benchmarks/drivers/Java/LifxLightBulb/DeviceStateLocation.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/DeviceStateLocation.java rename to benchmarks/drivers/Java/LifxLightBulb/DeviceStateLocation.java diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateService.java b/benchmarks/drivers/Java/LifxLightBulb/DeviceStateService.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/DeviceStateService.java rename to benchmarks/drivers/Java/LifxLightBulb/DeviceStateService.java diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateVersion.java b/benchmarks/drivers/Java/LifxLightBulb/DeviceStateVersion.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/DeviceStateVersion.java rename to benchmarks/drivers/Java/LifxLightBulb/DeviceStateVersion.java diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateWifiFirmware.java b/benchmarks/drivers/Java/LifxLightBulb/DeviceStateWifiFirmware.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/DeviceStateWifiFirmware.java rename to benchmarks/drivers/Java/LifxLightBulb/DeviceStateWifiFirmware.java diff --git a/benchmarks/drivers/LifxLightBulb/DeviceStateWifiInfo.java b/benchmarks/drivers/Java/LifxLightBulb/DeviceStateWifiInfo.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/DeviceStateWifiInfo.java rename to benchmarks/drivers/Java/LifxLightBulb/DeviceStateWifiInfo.java diff --git a/benchmarks/drivers/LifxLightBulb/LifxHeader.java b/benchmarks/drivers/Java/LifxLightBulb/LifxHeader.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/LifxHeader.java rename to benchmarks/drivers/Java/LifxLightBulb/LifxHeader.java diff --git a/benchmarks/drivers/Java/LifxLightBulb/LifxLightBulb.config b/benchmarks/drivers/Java/LifxLightBulb/LifxLightBulb.config new file mode 100644 index 0000000..fa88933 --- /dev/null +++ b/benchmarks/drivers/Java/LifxLightBulb/LifxLightBulb.config @@ -0,0 +1,6 @@ +# Skeleton/original interface +INTERFACE_CLASS=LightBulb +# Stub +#INTERFACE_STUB_CLASS=LightBulbTest +INTERFACE_STUB_CLASS=LightBulbSmart + diff --git a/benchmarks/drivers/LifxLightBulb/LifxLightBulb.java b/benchmarks/drivers/Java/LifxLightBulb/LifxLightBulb.java similarity index 90% rename from benchmarks/drivers/LifxLightBulb/LifxLightBulb.java rename to benchmarks/drivers/Java/LifxLightBulb/LifxLightBulb.java index cbc0ca9..c131bfa 100644 --- a/benchmarks/drivers/LifxLightBulb/LifxLightBulb.java +++ b/benchmarks/drivers/Java/LifxLightBulb/LifxLightBulb.java @@ -9,6 +9,10 @@ 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; @@ -39,7 +43,7 @@ public class LifxLightBulb implements LightBulb { private byte[] bulbMacAddress = new byte[8]; static Semaphore socketMutex = new Semaphore(1); static boolean sendSocketFlag = false; - private long lastSentGetBulbVersionRequest = 0; // time last request sent + private long lastSentGetBulbVersionRequest = 0; // time last request sent // Current Bulb Values private int currentHue = 0; @@ -95,11 +99,16 @@ public class LifxLightBulb implements LightBulb { /** * Used for testing only */ - public LifxLightBulb(IoTUDP udp, byte[] macAddress) { + /*public LifxLightBulb(IoTUDP udp, byte[] macAddress) { communicationSockect = udp; bulbMacAddress = macAddress; } + public LifxLightBulb(IoTSet _lb_addresses, String macAddress) { + this(macAddress); + lb_addresses = _lb_addresses; + }*/ + public LifxLightBulb(String macAddress) { communicationSockect = null; @@ -129,7 +138,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(true); header.setMacAddress(bulbMacAddress); - header.setSource(0); // randomly picked + header.setSource(0); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -144,7 +153,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -159,7 +168,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -174,7 +183,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -189,7 +198,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -204,7 +213,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -231,7 +240,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(38); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -253,7 +262,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -276,7 +285,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(68); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -299,7 +308,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -314,7 +323,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -329,7 +338,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -344,7 +353,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -365,7 +374,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -387,7 +396,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(49); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -424,7 +433,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(36); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -448,7 +457,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(42); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -467,6 +476,8 @@ public class LifxLightBulb implements LightBulb { packetBytes[40] = (byte)((duration >> 16) & 0xFF); packetBytes[41] = (byte)((duration >> 24) & 0xFF); + System.out.println(Arrays.toString(packetBytes)); + sendPacket(packetBytes); } @@ -483,7 +494,7 @@ public class LifxLightBulb implements LightBulb { header.setSize(100); header.setTagged(false); header.setMacAddress(bulbMacAddress); - header.setSource(10); // randomly picked + header.setSource(10); // randomly picked header.setAck_required(false); header.setRes_required(false); header.setSequence(0); @@ -717,19 +728,19 @@ public class LifxLightBulb implements LightBulb { boolean isColor = false; - if (productNumber == 1) { // Original 1000 + if (productNumber == 1) {// Original 1000 isColor = true; - } else if (productNumber == 3) { //Color 650 + } else if (productNumber == 3) {//Color 650 isColor = true; - } else if (productNumber == 10) { // White 800 (Low Voltage) + } else if (productNumber == 10) {// White 800 (Low Voltage) isColor = false; - } else if (productNumber == 11) { // White 800 (High Voltage) + } else if (productNumber == 11) {// White 800 (High Voltage) isColor = false; - } else if (productNumber == 18) { // White 900 BR30 (Low Voltage) + } else if (productNumber == 18) {// White 900 BR30 (Low Voltage) isColor = false; - } else if (productNumber == 20) { // Color 1000 BR30 + } else if (productNumber == 20) {// Color 1000 BR30 isColor = true; - } else if (productNumber == 22) { // Color 1000 + } else if (productNumber == 22) {// Color 1000 isColor = true; } @@ -748,7 +759,7 @@ public class LifxLightBulb implements LightBulb { saturationLowerBound = 0; saturationUpperBound = 0; brightnessLowerBound = 0; - brightnessUpperBound = 65535; // still can dim bulb + brightnessUpperBound = 65535;// still can dim bulb temperatureLowerBound = 2500; temperatureUpperBound = 9000; } @@ -1097,7 +1108,6 @@ public class LifxLightBulb implements LightBulb { // Start the bulb in the off state turnOff(); - while (true) { // Check if we got the bulb version yet @@ -1188,6 +1198,18 @@ public class LifxLightBulb implements LightBulb { workerThread.start(); } + + + /*public static void main(String[] args) throws Exception { + + System.out.println("Executing main function!"); + IoTDeviceAddress iotDevAdd = new IoTDeviceAddress("192.168.2.126", 12345, 56700, false, false); + Set set = new HashSet(); + set.add(iotDevAdd); + IoTSet iotset = new IoTSet(set); + LifxLightBulb lb = new LifxLightBulb(iotset, "D073D5128E300000"); + lb.init(); + }*/ } diff --git a/benchmarks/drivers/LifxLightBulb/LightBulb_Skeleton.java b/benchmarks/drivers/Java/LifxLightBulb/LightBulb_Skeleton.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/LightBulb_Skeleton.java rename to benchmarks/drivers/Java/LifxLightBulb/LightBulb_Skeleton.java diff --git a/benchmarks/drivers/LifxLightBulb/LightState.java b/benchmarks/drivers/Java/LifxLightBulb/LightState.java similarity index 100% rename from benchmarks/drivers/LifxLightBulb/LightState.java rename to benchmarks/drivers/Java/LifxLightBulb/LightState.java diff --git a/benchmarks/drivers/Makefile b/benchmarks/drivers/Java/Makefile similarity index 99% rename from benchmarks/drivers/Makefile rename to benchmarks/drivers/Java/Makefile index b842e02..cd5d083 100644 --- a/benchmarks/drivers/Makefile +++ b/benchmarks/drivers/Java/Makefile @@ -1,4 +1,4 @@ -BASE = ../.. +BASE = ../../.. include $(BASE)/common.mk diff --git a/benchmarks/drivers/MotionSensor/MotionSensor.config b/benchmarks/drivers/Java/MotionSensor/MotionSensor.config similarity index 100% rename from benchmarks/drivers/MotionSensor/MotionSensor.config rename to benchmarks/drivers/Java/MotionSensor/MotionSensor.config diff --git a/benchmarks/drivers/MotionSensor/MotionSensor.java b/benchmarks/drivers/Java/MotionSensor/MotionSensor.java similarity index 100% rename from benchmarks/drivers/MotionSensor/MotionSensor.java rename to benchmarks/drivers/Java/MotionSensor/MotionSensor.java diff --git a/benchmarks/drivers/MotionSensor/SmartthingsSensorSmartCallback_CallbackStub.java b/benchmarks/drivers/Java/MotionSensor/SmartthingsSensorSmartCallback_CallbackStub.java similarity index 100% rename from benchmarks/drivers/MotionSensor/SmartthingsSensorSmartCallback_CallbackStub.java rename to benchmarks/drivers/Java/MotionSensor/SmartthingsSensorSmartCallback_CallbackStub.java diff --git a/benchmarks/drivers/MotionSensor/SmartthingsSensor_Skeleton.java b/benchmarks/drivers/Java/MotionSensor/SmartthingsSensor_Skeleton.java similarity index 100% rename from benchmarks/drivers/MotionSensor/SmartthingsSensor_Skeleton.java rename to benchmarks/drivers/Java/MotionSensor/SmartthingsSensor_Skeleton.java diff --git a/benchmarks/drivers/MultipurposeSensor/MultipurposeSensor.config b/benchmarks/drivers/Java/MultipurposeSensor/MultipurposeSensor.config similarity index 100% rename from benchmarks/drivers/MultipurposeSensor/MultipurposeSensor.config rename to benchmarks/drivers/Java/MultipurposeSensor/MultipurposeSensor.config diff --git a/benchmarks/drivers/MultipurposeSensor/MultipurposeSensor.java b/benchmarks/drivers/Java/MultipurposeSensor/MultipurposeSensor.java similarity index 100% rename from benchmarks/drivers/MultipurposeSensor/MultipurposeSensor.java rename to benchmarks/drivers/Java/MultipurposeSensor/MultipurposeSensor.java diff --git a/benchmarks/drivers/MultipurposeSensor/SmartthingsSensorSmartCallback_CallbackStub.java b/benchmarks/drivers/Java/MultipurposeSensor/SmartthingsSensorSmartCallback_CallbackStub.java similarity index 100% rename from benchmarks/drivers/MultipurposeSensor/SmartthingsSensorSmartCallback_CallbackStub.java rename to benchmarks/drivers/Java/MultipurposeSensor/SmartthingsSensorSmartCallback_CallbackStub.java diff --git a/benchmarks/drivers/MultipurposeSensor/SmartthingsSensor_Skeleton.java b/benchmarks/drivers/Java/MultipurposeSensor/SmartthingsSensor_Skeleton.java similarity index 100% rename from benchmarks/drivers/MultipurposeSensor/SmartthingsSensor_Skeleton.java rename to benchmarks/drivers/Java/MultipurposeSensor/SmartthingsSensor_Skeleton.java diff --git a/benchmarks/drivers/SpruceSensor/MoistureSensorSmartCallback_CallbackStub.java b/benchmarks/drivers/Java/SpruceSensor/MoistureSensorSmartCallback_CallbackStub.java similarity index 100% rename from benchmarks/drivers/SpruceSensor/MoistureSensorSmartCallback_CallbackStub.java rename to benchmarks/drivers/Java/SpruceSensor/MoistureSensorSmartCallback_CallbackStub.java diff --git a/benchmarks/drivers/SpruceSensor/MoistureSensor_Skeleton.java b/benchmarks/drivers/Java/SpruceSensor/MoistureSensor_Skeleton.java similarity index 100% rename from benchmarks/drivers/SpruceSensor/MoistureSensor_Skeleton.java rename to benchmarks/drivers/Java/SpruceSensor/MoistureSensor_Skeleton.java diff --git a/benchmarks/drivers/SpruceSensor/SpruceSensor.config b/benchmarks/drivers/Java/SpruceSensor/SpruceSensor.config similarity index 100% rename from benchmarks/drivers/SpruceSensor/SpruceSensor.config rename to benchmarks/drivers/Java/SpruceSensor/SpruceSensor.config diff --git a/benchmarks/drivers/SpruceSensor/SpruceSensor.java b/benchmarks/drivers/Java/SpruceSensor/SpruceSensor.java similarity index 100% rename from benchmarks/drivers/SpruceSensor/SpruceSensor.java rename to benchmarks/drivers/Java/SpruceSensor/SpruceSensor.java diff --git a/benchmarks/drivers/WaterLeakSensor/SmartthingsSensorSmartCallback_CallbackStub.java b/benchmarks/drivers/Java/WaterLeakSensor/SmartthingsSensorSmartCallback_CallbackStub.java similarity index 100% rename from benchmarks/drivers/WaterLeakSensor/SmartthingsSensorSmartCallback_CallbackStub.java rename to benchmarks/drivers/Java/WaterLeakSensor/SmartthingsSensorSmartCallback_CallbackStub.java diff --git a/benchmarks/drivers/WaterLeakSensor/SmartthingsSensor_Skeleton.java b/benchmarks/drivers/Java/WaterLeakSensor/SmartthingsSensor_Skeleton.java similarity index 100% rename from benchmarks/drivers/WaterLeakSensor/SmartthingsSensor_Skeleton.java rename to benchmarks/drivers/Java/WaterLeakSensor/SmartthingsSensor_Skeleton.java diff --git a/benchmarks/drivers/WaterLeakSensor/WaterLeakSensor.config b/benchmarks/drivers/Java/WaterLeakSensor/WaterLeakSensor.config similarity index 100% rename from benchmarks/drivers/WaterLeakSensor/WaterLeakSensor.config rename to benchmarks/drivers/Java/WaterLeakSensor/WaterLeakSensor.config diff --git a/benchmarks/drivers/WaterLeakSensor/WaterLeakSensor.java b/benchmarks/drivers/Java/WaterLeakSensor/WaterLeakSensor.java similarity index 100% rename from benchmarks/drivers/WaterLeakSensor/WaterLeakSensor.java rename to benchmarks/drivers/Java/WaterLeakSensor/WaterLeakSensor.java diff --git a/benchmarks/drivers/WeatherPhoneGateway/WeatherGatewaySmartCallback_CallbackStub.java b/benchmarks/drivers/Java/WeatherPhoneGateway/WeatherGatewaySmartCallback_CallbackStub.java similarity index 100% rename from benchmarks/drivers/WeatherPhoneGateway/WeatherGatewaySmartCallback_CallbackStub.java rename to benchmarks/drivers/Java/WeatherPhoneGateway/WeatherGatewaySmartCallback_CallbackStub.java diff --git a/benchmarks/drivers/WeatherPhoneGateway/WeatherGateway_Skeleton.java b/benchmarks/drivers/Java/WeatherPhoneGateway/WeatherGateway_Skeleton.java similarity index 100% rename from benchmarks/drivers/WeatherPhoneGateway/WeatherGateway_Skeleton.java rename to benchmarks/drivers/Java/WeatherPhoneGateway/WeatherGateway_Skeleton.java diff --git a/benchmarks/drivers/WeatherPhoneGateway/WeatherInfo.java b/benchmarks/drivers/Java/WeatherPhoneGateway/WeatherInfo.java similarity index 100% rename from benchmarks/drivers/WeatherPhoneGateway/WeatherInfo.java rename to benchmarks/drivers/Java/WeatherPhoneGateway/WeatherInfo.java diff --git a/benchmarks/drivers/WeatherPhoneGateway/WeatherInfoInterface.java b/benchmarks/drivers/Java/WeatherPhoneGateway/WeatherInfoInterface.java similarity index 100% rename from benchmarks/drivers/WeatherPhoneGateway/WeatherInfoInterface.java rename to benchmarks/drivers/Java/WeatherPhoneGateway/WeatherInfoInterface.java diff --git a/benchmarks/drivers/WeatherPhoneGateway/WeatherPhoneGateway.config b/benchmarks/drivers/Java/WeatherPhoneGateway/WeatherPhoneGateway.config similarity index 100% rename from benchmarks/drivers/WeatherPhoneGateway/WeatherPhoneGateway.config rename to benchmarks/drivers/Java/WeatherPhoneGateway/WeatherPhoneGateway.config diff --git a/benchmarks/drivers/WeatherPhoneGateway/WeatherPhoneGateway.java b/benchmarks/drivers/Java/WeatherPhoneGateway/WeatherPhoneGateway.java similarity index 100% rename from benchmarks/drivers/WeatherPhoneGateway/WeatherPhoneGateway.java rename to benchmarks/drivers/Java/WeatherPhoneGateway/WeatherPhoneGateway.java diff --git a/benchmarks/virtuals/LightBulb.hpp b/benchmarks/virtuals/LightBulb.hpp new file mode 100644 index 0000000..62a8f96 --- /dev/null +++ b/benchmarks/virtuals/LightBulb.hpp @@ -0,0 +1,29 @@ +#ifndef _LIGHTBULB_HPP__ +#define _LIGHTBULB_HPP__ +#include + +using namespace std; + +class LightBulb +{ + public: + virtual void init() = 0; + virtual void turnOff() = 0; + virtual void turnOn() = 0; + virtual bool getState() = 0; + virtual void setColor(double _hue, double _saturation, double _brightness) = 0; + virtual void setTemperature(int _temperature) = 0; + virtual double getBrightness() = 0; + virtual double getHue() = 0; + virtual double getSaturation() = 0; + virtual int getTemperature() = 0; + virtual double getBrightnessRangeLowerBound() = 0; + virtual double getBrightnessRangeUpperBound() = 0; + virtual double getHueRangeLowerBound() = 0; + virtual double getHueRangeUpperBound() = 0; + virtual double getSaturationRangeLowerBound() = 0; + virtual double getSaturationRangeUpperBound() = 0; + virtual int getTemperatureRangeLowerBound() = 0; + virtual int getTemperatureRangeUpperBound() = 0; +}; +#endif diff --git a/benchmarks/virtuals/LightBulbSmart.hpp b/benchmarks/virtuals/LightBulbSmart.hpp new file mode 100644 index 0000000..5666280 --- /dev/null +++ b/benchmarks/virtuals/LightBulbSmart.hpp @@ -0,0 +1,33 @@ +#ifndef _LIGHTBULBSMART_HPP__ +#define _LIGHTBULBSMART_HPP__ +#include +#include +#include +#include "IoTRMICall.hpp" +#include "IoTRMIObject.hpp" + +using namespace std; + +class LightBulbSmart +{ + public: + virtual void turnOn() = 0; + virtual double getBrightnessRangeLowerBound() = 0; + virtual void turnOff() = 0; + virtual bool getState() = 0; + virtual double getBrightnessRangeUpperBound() = 0; + virtual double getSaturation() = 0; + virtual double getHueRangeLowerBound() = 0; + virtual double getHue() = 0; + virtual double getHueRangeUpperBound() = 0; + virtual int getTemperature() = 0; + virtual double getBrightness() = 0; + virtual int getTemperatureRangeLowerBound() = 0; + virtual int getTemperatureRangeUpperBound() = 0; + virtual void setColor(double _hue, double _saturation, double _brightness) = 0; + virtual void init() = 0; + virtual double getSaturationRangeLowerBound() = 0; + virtual double getSaturationRangeUpperBound() = 0; + virtual void setTemperature(int _temperature) = 0; +}; +#endif diff --git a/iotjava/Makefile b/iotjava/Makefile index 9a5fb1f..e346a15 100644 --- a/iotjava/Makefile +++ b/iotjava/Makefile @@ -37,7 +37,8 @@ PHONY += run-compiler-lifx run-compiler-lifx: cp ../localconfig/iotpolicy/LifxLightBulb/*.pol $(BIN_DIR)/iotpolicy/ cp ../localconfig/iotpolicy/LifxLightBulb/*.req $(BIN_DIR)/iotpolicy/ - cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler lifxlightbulb.pol smartlightsbulb.req -java Java + #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler lifxlightbulb.pol smartlightsbulb.req -java Java + cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler lifxlightbulb.pol smartlightsbulb.req -cplus C++ PHONY += run-compiler-room run-compiler-room: diff --git a/iotjava/iotruntime/cpp/IoTAddress.hpp b/iotjava/iotruntime/cpp/IoTAddress.hpp new file mode 100644 index 0000000..5d57ede --- /dev/null +++ b/iotjava/iotruntime/cpp/IoTAddress.hpp @@ -0,0 +1,59 @@ +#ifndef _IOTADDRESS_HPP__ +#define _IOTADDRESS_HPP__ +#include + +using namespace std; + + +// IoTAddress class for iotruntime +// Implemented based on IoTAddress.java that is used to wrap address +// +// @author Rahmadi Trimananda +// @version 1.0 +// @since 2017-01-09 + +class IoTAddress +{ + public: + + // Constructor + IoTAddress(string _sAddress) { + + inetAddress = _sAddress; + } + + + // Constructor + IoTAddress() { + } + + + ~IoTAddress() { + } + + + string getAddress() { + + return inetAddress; + } + + + string getURL(string strURLComplete) { + + return "http://" + inetAddress + strURLComplete; + } + + + // Custom hasher for IoTAddress / IoTDeviceAddress iterator + size_t hash(IoTAddress const& devAddress) const { + + std::hash hashVal; + return hashVal(inetAddress); + } + + + // IoTAddress class properties + protected: + string inetAddress; +}; +#endif diff --git a/iotjava/iotruntime/cpp/IoTDeviceAddress.hpp b/iotjava/iotruntime/cpp/IoTDeviceAddress.hpp new file mode 100644 index 0000000..da6a695 --- /dev/null +++ b/iotjava/iotruntime/cpp/IoTDeviceAddress.hpp @@ -0,0 +1,91 @@ +#ifndef _IOTDEVICEADDRESS_HPP__ +#define _IOTDEVICEADDRESS_HPP__ +#include + +#include "IoTAddress.hpp" + +using namespace std; + +// IoTDeviceAddress class for iotruntime +// Implemented based on IoTDeviceAddress.java that is used to wrap device address +// +// @author Rahmadi Trimananda +// @version 1.0 +// @since 2017-01-09 + + +class IoTDeviceAddress : public IoTAddress +{ + + public: + + // Constructor + IoTDeviceAddress(string _sAddress, int _iSrcPort, int _iDstPort, bool _isSrcPortWildCard, bool _isDstPortWildCard) : IoTAddress(_sAddress) { + + iSrcPort = _iSrcPort; + iDstPort = _iDstPort; + isSrcPortWildCard = _isSrcPortWildCard; + isDstPortWildCard = _isDstPortWildCard; + } + + + // Constructor + IoTDeviceAddress() { + } + + + ~IoTDeviceAddress() { + } + + + // Getter methods + int getSourcePortNumber() { + + return iSrcPort; + } + + + int getDestinationPortNumber() { + + return iDstPort; + } + + + bool getIsSrcPortWildcard() { + + return isSrcPortWildCard; + } + + + bool getIsDstPortWildcard() { + + return isDstPortWildCard; + } + + + // Setter methods + void setSrcPort(int port) { + + if (isDstPortWildCard) { + iDstPort = port; + } + } + + + void setDstPort(int port) { + + if (isSrcPortWildCard) { + iSrcPort = port; + } + } + + + // IoTDeviceAddress class properties + private: + int iSrcPort; + int iDstPort; + + bool isSrcPortWildCard; + bool isDstPortWildCard; +}; +#endif diff --git a/iotjava/iotruntime/cpp/IoTUDP.hpp b/iotjava/iotruntime/cpp/IoTUDP.hpp new file mode 100644 index 0000000..ccdb5d9 --- /dev/null +++ b/iotjava/iotruntime/cpp/IoTUDP.hpp @@ -0,0 +1,68 @@ +#ifndef _IOTUDP_HPP__ +#define _IOTUDP_HPP__ +#include + +#include "IoTDeviceAddress.hpp" + +using namespace std; + +// IoTUDP class for iotruntime +// Implemented based on IoTUDP.java that is used to wrap communication socket for UDP +// +// @author Rahmadi Trimananda +// @version 1.0 +// @since 2017-01-09 + +class IoTUDP +{ + // IoTUDP class properties + private: + string strHostAddress; + int iSrcPort; + int iDstPort; + UDPSocket *socket; + bool didClose; + + public: + + // Constructor + IoTUDP(IoTDeviceAddress iotDevAdd) { + + strHostAddress = iotDevAdd.getAddress(); + iSrcPort = iotDevAdd.getSourcePortNumber(); + iDstPort = iotDevAdd.getDestinationPortNumber(); + + socket = new UDPSocket(iSrcPort); + if (socket == NULL) { + perror("IoTUDP: UDP socket isn't initialized!"); + } + didClose = false; + } + + + ~IoTUDP() { + // Clean up + if (socket != NULL) { + + delete socket; + socket = NULL; + } + } + + + // Send data packet + void sendData(const void* buffer, int bufferLen) { + + unsigned short destinationPort = (unsigned short) iDstPort; + socket->sendTo(buffer, bufferLen, strHostAddress, destinationPort); + } + + + // Receive data packet + int receiveData(void* buffer, int iMaxDataLength) { + + unsigned short destinationPort = (unsigned short) iDstPort; + socket->recvFrom(buffer, iMaxDataLength, strHostAddress, destinationPort); + } +}; +#endif diff --git a/iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp b/iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp new file mode 100644 index 0000000..c2315f9 --- /dev/null +++ b/iotjava/iotruntime/cpp/setrelation/IoTRelation.hpp @@ -0,0 +1,129 @@ +#ifndef _IOTRELATION_HPP__ +#define _IOTRELATION_HPP__ +#include +#include +#include + +using namespace std; + +/** This is the IoTRelation implementation for C++ + * + * @author Rahmadi Trimananda + * @version 1.0 + * @since 2016-09-06 + */ +template +class IoTRelation { + private: + unordered_multimap rel; + public: + IoTRelation(); + IoTRelation(unordered_multimap const& s); + ~IoTRelation(); + public: + typename unordered_multimap::const_iterator find(const K& k); // Find the object based on key + bool empty(); // Test is empty? + typename unordered_multimap::const_iterator begin(); // Iterator + typename unordered_multimap::const_iterator end(); // Iterator + std::pair::const_iterator, + typename unordered_multimap::const_iterator> + equal_range(const K& k); // Equal range iterator + int size(); // Set size + unordered_multimap values(); // Return set contents +}; + + +/** + * Default constructor + */ +template +IoTRelation::IoTRelation() { + +} + + +/** + * Useful constructor + */ +template +IoTRelation::IoTRelation(const unordered_multimap& r) { + + rel = r; +} + + +/** + * Default destructor + */ +template +IoTRelation::~IoTRelation() { + + //free(&set); +} + + +/** + * Find the object k in the set + */ +template +typename unordered_multimap::const_iterator IoTRelation::find(const K& k) { + + return rel.find(k); +} + + +/** + * Return the "begin" iterator + */ +template +typename unordered_multimap::const_iterator IoTRelation::begin() { + + return rel.begin(); +} + + +/** + * Return the "end" iterator + */ +template +typename unordered_multimap::const_iterator IoTRelation::end() { + + return rel.end(); +} + + +/** + * Return the "equal_range" iterator + */ +template +std::pair::const_iterator, + typename unordered_multimap::const_iterator> + IoTRelation::equal_range(const K& k) { + + return rel.equal_range(k); +} + + +/** + * Return the size of the set + */ +template +int IoTRelation::size() { + + return rel.size(); +} + + +/** + * Return a new copy of the set + */ +template +unordered_multimap IoTRelation::values() { + + return new unordered_multimap(rel); +} +#endif + + + + diff --git a/iotjava/iotruntime/cpp/setrelation/IoTSet.hpp b/iotjava/iotruntime/cpp/setrelation/IoTSet.hpp new file mode 100644 index 0000000..f64595f --- /dev/null +++ b/iotjava/iotruntime/cpp/setrelation/IoTSet.hpp @@ -0,0 +1,111 @@ +#ifndef _IOTSET_HPP__ +#define _IOTSET_HPP__ +#include +#include +#include + +using namespace std; + +/** This is the IoTSet implementation for C++ + * + * @author Rahmadi Trimananda + * @version 1.0 + * @since 2016-09-06 + */ +template +class IoTSet { + private: + unordered_set set; + public: + IoTSet(); + IoTSet(unordered_set const& s); + ~IoTSet(); + public: + typename unordered_set::const_iterator find(const T& k); // Find the object + bool empty(); // Test is empty? + typename unordered_set::const_iterator begin(); // Iterator + typename unordered_set::const_iterator end(); // Iterator + int size(); // Set size + unordered_set values(); // Return set contents +}; + + +/** + * Default constructor + */ +template +IoTSet::IoTSet() { + +} + + +/** + * Useful constructor + */ +template +IoTSet::IoTSet(const unordered_set& s) { + + set = s; +} + + +/** + * Default destructor + */ +template +IoTSet::~IoTSet() { + + //free(&set); +} + + +/** + * Find the object k in the set + */ +template +typename unordered_set::const_iterator IoTSet::find(const T& k) { + + return set.find(k); +} + + +/** + * Return the "begin" iterator + */ +template +typename unordered_set::const_iterator IoTSet::begin() { + + return set.begin(); +} + + +/** + * Return the "end" iterator + */ +template +typename unordered_set::const_iterator IoTSet::end() { + + return set.end(); +} + + +/** + * Return the size of the set + */ +template +int IoTSet::size() { + + return set.size(); +} + + +/** + * Return a new copy of the set + */ +template +unordered_set IoTSet::values() { + + return new unordered_set(set); +} +#endif + diff --git a/iotjava/iotruntime/cpp/setrelation/Iterator.hpp b/iotjava/iotruntime/cpp/setrelation/Iterator.hpp new file mode 100644 index 0000000..9cc4416 --- /dev/null +++ b/iotjava/iotruntime/cpp/setrelation/Iterator.hpp @@ -0,0 +1,20 @@ +#ifndef _ITERATOR_HPP__ +#define _ITERATOR_HPP__ + +namespace std +{ + template<> struct hash + { + size_t operator()(IoTDeviceAddress const& devAddress) const + { + return devAddress.hash(devAddress); + } + }; +} + + +bool operator==(const IoTDeviceAddress& lhs, const IoTDeviceAddress& rhs) { + return lhs.hash(lhs) == rhs.hash(rhs); +} + +#endif diff --git a/iotjava/iotruntime/cpp/socket/Socket.cpp b/iotjava/iotruntime/cpp/socket/Socket.cpp new file mode 100644 index 0000000..ceac82b --- /dev/null +++ b/iotjava/iotruntime/cpp/socket/Socket.cpp @@ -0,0 +1,383 @@ +/* + * C++ sockets on Unix and Windows + * Copyright (C) 2002 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +// http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/ + +#include "Socket.hpp" + +#ifdef WIN32 + #include // For socket(), connect(), send(), and recv() + typedef int socklen_t; + typedef char raw_type; // Type used for raw data on this platform +#else + #include // For data types + #include // For socket(), connect(), send(), and recv() + #include // For gethostbyname() + #include // For inet_addr() + #include // For close() + #include // For sockaddr_in + typedef void raw_type; // Type used for raw data on this platform +#endif + +#include // For errno +#include // For memset + +using namespace std; + +#ifdef WIN32 +static bool initialized = false; +#endif + +// SocketException Code + +SocketException::SocketException(const string &message, bool inclSysMsg) + throw() : userMessage(message) { + if (inclSysMsg) { + userMessage.append(": "); + userMessage.append(strerror(errno)); + } +} + +SocketException::~SocketException() throw() { +} + +const char *SocketException::what() const throw() { + return userMessage.c_str(); +} + +// Function to fill in address structure given an address and port +static void fillAddr(const string &address, unsigned short port, + sockaddr_in &addr) { + memset(&addr, 0, sizeof(addr)); // Zero out address structure + addr.sin_family = AF_INET; // Internet address + + hostent *host; // Resolve name + if ((host = gethostbyname(address.c_str())) == NULL) { + // strerror() will not work for gethostbyname() and hstrerror() + // is supposedly obsolete + throw SocketException("Failed to resolve name (gethostbyname())"); + } + addr.sin_addr.s_addr = *((unsigned long *) host->h_addr_list[0]); + + addr.sin_port = htons(port); // Assign port in network byte order +} + +// Socket Code + +Socket::Socket(int type, int protocol) throw(SocketException) { + #ifdef WIN32 + if (!initialized) { + WORD wVersionRequested; + WSADATA wsaData; + + wVersionRequested = MAKEWORD(2, 0); // Request WinSock v2.0 + if (WSAStartup(wVersionRequested, &wsaData) != 0) { // Load WinSock DLL + throw SocketException("Unable to load WinSock DLL"); + } + initialized = true; + } + #endif + + // Make a new socket + if ((sockDesc = socket(PF_INET, type, protocol)) < 0) { + throw SocketException("Socket creation failed (socket())", true); + } +} + +Socket::Socket(int sockDesc) { + this->sockDesc = sockDesc; +} + +Socket::~Socket() { + #ifdef WIN32 + ::closesocket(sockDesc); + #else + ::close(sockDesc); + #endif + sockDesc = -1; +} + +string Socket::getLocalAddress() throw(SocketException) { + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { + throw SocketException("Fetch of local address failed (getsockname())", true); + } + return inet_ntoa(addr.sin_addr); +} + +unsigned short Socket::getLocalPort() throw(SocketException) { + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getsockname(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { + throw SocketException("Fetch of local port failed (getsockname())", true); + } + return ntohs(addr.sin_port); +} + +void Socket::setLocalPort(unsigned short localPort) throw(SocketException) { + // Bind the socket to its port + sockaddr_in localAddr; + memset(&localAddr, 0, sizeof(localAddr)); + localAddr.sin_family = AF_INET; + localAddr.sin_addr.s_addr = htonl(INADDR_ANY); + localAddr.sin_port = htons(localPort); + + if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) { + throw SocketException("Set of local port failed (bind())", true); + } +} + +void Socket::setLocalAddressAndPort(const string &localAddress, + unsigned short localPort) throw(SocketException) { + // Get the address of the requested host + sockaddr_in localAddr; + fillAddr(localAddress, localPort, localAddr); + + if (bind(sockDesc, (sockaddr *) &localAddr, sizeof(sockaddr_in)) < 0) { + throw SocketException("Set of local address and port failed (bind())", true); + } +} + +void Socket::cleanUp() throw(SocketException) { + #ifdef WIN32 + if (WSACleanup() != 0) { + throw SocketException("WSACleanup() failed"); + } + #endif +} + +unsigned short Socket::resolveService(const string &service, + const string &protocol) { + struct servent *serv; /* Structure containing service information */ + + if ((serv = getservbyname(service.c_str(), protocol.c_str())) == NULL) + return atoi(service.c_str()); /* Service is port number */ + else + return ntohs(serv->s_port); /* Found port (network byte order) by name */ +} + +// CommunicatingSocket Code + +CommunicatingSocket::CommunicatingSocket(int type, int protocol) + throw(SocketException) : Socket(type, protocol) { +} + +CommunicatingSocket::CommunicatingSocket(int newConnSD) : Socket(newConnSD) { +} + +void CommunicatingSocket::connect(const string &foreignAddress, + unsigned short foreignPort) throw(SocketException) { + // Get the address of the requested host + sockaddr_in destAddr; + fillAddr(foreignAddress, foreignPort, destAddr); + + // Try to connect to the given port + if (::connect(sockDesc, (sockaddr *) &destAddr, sizeof(destAddr)) < 0) { + throw SocketException("Connect failed (connect())", true); + } +} + +void CommunicatingSocket::send(const void *buffer, int bufferLen) + throw(SocketException) { + if (::send(sockDesc, (raw_type *) buffer, bufferLen, 0) < 0) { + throw SocketException("Send failed (send())", true); + } +} + +int CommunicatingSocket::recv(void *buffer, int bufferLen) + throw(SocketException) { + int rtn; + if ((rtn = ::recv(sockDesc, (raw_type *) buffer, bufferLen, 0)) < 0) { + throw SocketException("Received failed (recv())", true); + } + + return rtn; +} + +string CommunicatingSocket::getForeignAddress() + throw(SocketException) { + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getpeername(sockDesc, (sockaddr *) &addr,(socklen_t *) &addr_len) < 0) { + throw SocketException("Fetch of foreign address failed (getpeername())", true); + } + return inet_ntoa(addr.sin_addr); +} + +unsigned short CommunicatingSocket::getForeignPort() throw(SocketException) { + sockaddr_in addr; + unsigned int addr_len = sizeof(addr); + + if (getpeername(sockDesc, (sockaddr *) &addr, (socklen_t *) &addr_len) < 0) { + throw SocketException("Fetch of foreign port failed (getpeername())", true); + } + return ntohs(addr.sin_port); +} + +// TCPSocket Code + +TCPSocket::TCPSocket() + throw(SocketException) : CommunicatingSocket(SOCK_STREAM, + IPPROTO_TCP) { +} + +TCPSocket::TCPSocket(const string &foreignAddress, unsigned short foreignPort) + throw(SocketException) : CommunicatingSocket(SOCK_STREAM, IPPROTO_TCP) { + connect(foreignAddress, foreignPort); +} + +TCPSocket::TCPSocket(int newConnSD) : CommunicatingSocket(newConnSD) { +} + +// TCPServerSocket Code + +TCPServerSocket::TCPServerSocket(unsigned short localPort, int queueLen) + throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) { + setLocalPort(localPort); + setListen(queueLen); +} + +TCPServerSocket::TCPServerSocket(const string &localAddress, + unsigned short localPort, int queueLen) + throw(SocketException) : Socket(SOCK_STREAM, IPPROTO_TCP) { + setLocalAddressAndPort(localAddress, localPort); + setListen(queueLen); +} + +TCPSocket *TCPServerSocket::accept() throw(SocketException) { + int newConnSD; + if ((newConnSD = ::accept(sockDesc, NULL, 0)) < 0) { + throw SocketException("Accept failed (accept())", true); + } + + return new TCPSocket(newConnSD); +} + +void TCPServerSocket::setListen(int queueLen) throw(SocketException) { + if (listen(sockDesc, queueLen) < 0) { + throw SocketException("Set listening socket failed (listen())", true); + } +} + +// UDPSocket Code + +UDPSocket::UDPSocket() throw(SocketException) : CommunicatingSocket(SOCK_DGRAM, + IPPROTO_UDP) { + setBroadcast(); +} + +UDPSocket::UDPSocket(unsigned short localPort) throw(SocketException) : + CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) { + setLocalPort(localPort); + setBroadcast(); +} + +UDPSocket::UDPSocket(const string &localAddress, unsigned short localPort) + throw(SocketException) : CommunicatingSocket(SOCK_DGRAM, IPPROTO_UDP) { + setLocalAddressAndPort(localAddress, localPort); + setBroadcast(); +} + +void UDPSocket::setBroadcast() { + // If this fails, we'll hear about it when we try to send. This will allow + // system that cannot broadcast to continue if they don't plan to broadcast + int broadcastPermission = 1; + setsockopt(sockDesc, SOL_SOCKET, SO_BROADCAST, + (raw_type *) &broadcastPermission, sizeof(broadcastPermission)); +} + +void UDPSocket::disconnect() throw(SocketException) { + sockaddr_in nullAddr; + memset(&nullAddr, 0, sizeof(nullAddr)); + nullAddr.sin_family = AF_UNSPEC; + + // Try to disconnect + if (::connect(sockDesc, (sockaddr *) &nullAddr, sizeof(nullAddr)) < 0) { + #ifdef WIN32 + if (errno != WSAEAFNOSUPPORT) { + #else + if (errno != EAFNOSUPPORT) { + #endif + throw SocketException("Disconnect failed (connect())", true); + } + } +} + +void UDPSocket::sendTo(const void *buffer, int bufferLen, + const string &foreignAddress, unsigned short foreignPort) + throw(SocketException) { + sockaddr_in destAddr; + fillAddr(foreignAddress, foreignPort, destAddr); + + // Write out the whole buffer as a single message. + if (sendto(sockDesc, (raw_type *) buffer, bufferLen, 0, + (sockaddr *) &destAddr, sizeof(destAddr)) != bufferLen) { + throw SocketException("Send failed (sendto())", true); + } +} + +int UDPSocket::recvFrom(void *buffer, int bufferLen, string &sourceAddress, + unsigned short &sourcePort) throw(SocketException) { + sockaddr_in clntAddr; + socklen_t addrLen = sizeof(clntAddr); + int rtn; + if ((rtn = recvfrom(sockDesc, (raw_type *) buffer, bufferLen, 0, + (sockaddr *) &clntAddr, (socklen_t *) &addrLen)) < 0) { + throw SocketException("Receive failed (recvfrom())", true); + } + sourceAddress = inet_ntoa(clntAddr.sin_addr); + sourcePort = ntohs(clntAddr.sin_port); + + return rtn; +} + +void UDPSocket::setMulticastTTL(unsigned char multicastTTL) throw(SocketException) { + if (setsockopt(sockDesc, IPPROTO_IP, IP_MULTICAST_TTL, + (raw_type *) &multicastTTL, sizeof(multicastTTL)) < 0) { + throw SocketException("Multicast TTL set failed (setsockopt())", true); + } +} + +void UDPSocket::joinGroup(const string &multicastGroup) throw(SocketException) { + struct ip_mreq multicastRequest; + + multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str()); + multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY); + if (setsockopt(sockDesc, IPPROTO_IP, IP_ADD_MEMBERSHIP, + (raw_type *) &multicastRequest, + sizeof(multicastRequest)) < 0) { + throw SocketException("Multicast group join failed (setsockopt())", true); + } +} + +void UDPSocket::leaveGroup(const string &multicastGroup) throw(SocketException) { + struct ip_mreq multicastRequest; + + multicastRequest.imr_multiaddr.s_addr = inet_addr(multicastGroup.c_str()); + multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY); + if (setsockopt(sockDesc, IPPROTO_IP, IP_DROP_MEMBERSHIP, + (raw_type *) &multicastRequest, + sizeof(multicastRequest)) < 0) { + throw SocketException("Multicast group leave failed (setsockopt())", true); + } +} diff --git a/iotjava/iotruntime/cpp/socket/Socket.hpp b/iotjava/iotruntime/cpp/socket/Socket.hpp new file mode 100644 index 0000000..9da627a --- /dev/null +++ b/iotjava/iotruntime/cpp/socket/Socket.hpp @@ -0,0 +1,340 @@ +/* + * C++ sockets on Unix and Windows + * Copyright (C) 2002 + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +// http://cs.ecs.baylor.edu/~donahoo/practical/CSockets/practical/ +#ifndef __SOCKET_ +#define __SOCKET_ + +#include // For string +#include // For exception class + +using namespace std; + +/** + * Signals a problem with the execution of a socket call. + */ +class SocketException : public exception { +public: + /** + * Construct a SocketException with a explanatory message. + * @param message explanatory message + * @param incSysMsg true if system message (from strerror(errno)) + * should be postfixed to the user provided message + */ + SocketException(const string &message, bool inclSysMsg = false) throw(); + + /** + * Provided just to guarantee that no exceptions are thrown. + */ + ~SocketException() throw(); + + /** + * Get the exception message + * @return exception message + */ + const char *what() const throw(); + +private: + string userMessage; // Exception message +}; + +/** + * Base class representing basic communication endpoint + */ +class Socket { +public: + /** + * Close and deallocate this socket + */ + ~Socket(); + + /** + * Get the local address + * @return local address of socket + * @exception SocketException thrown if fetch fails + */ + string getLocalAddress() throw(SocketException); + + /** + * Get the local port + * @return local port of socket + * @exception SocketException thrown if fetch fails + */ + unsigned short getLocalPort() throw(SocketException); + + /** + * Set the local port to the specified port and the local address + * to any interface + * @param localPort local port + * @exception SocketException thrown if setting local port fails + */ + void setLocalPort(unsigned short localPort) throw(SocketException); + + /** + * Set the local port to the specified port and the local address + * to the specified address. If you omit the port, a random port + * will be selected. + * @param localAddress local address + * @param localPort local port + * @exception SocketException thrown if setting local port or address fails + */ + void setLocalAddressAndPort(const string &localAddress, + unsigned short localPort = 0) throw(SocketException); + + /** + * If WinSock, unload the WinSock DLLs; otherwise do nothing. We ignore + * this in our sample client code but include it in the library for + * completeness. If you are running on Windows and you are concerned + * about DLL resource consumption, call this after you are done with all + * Socket instances. If you execute this on Windows while some instance of + * Socket exists, you are toast. For portability of client code, this is + * an empty function on non-Windows platforms so you can always include it. + * @param buffer buffer to receive the data + * @param bufferLen maximum number of bytes to read into buffer + * @return number of bytes read, 0 for EOF, and -1 for error + * @exception SocketException thrown WinSock clean up fails + */ + static void cleanUp() throw(SocketException); + + /** + * Resolve the specified service for the specified protocol to the + * corresponding port number in host byte order + * @param service service to resolve (e.g., "http") + * @param protocol protocol of service to resolve. Default is "tcp". + */ + static unsigned short resolveService(const string &service, + const string &protocol = "tcp"); + +private: + // Prevent the user from trying to use value semantics on this object + Socket(const Socket &sock); + void operator=(const Socket &sock); + +protected: + int sockDesc; // Socket descriptor + Socket(int type, int protocol) throw(SocketException); + Socket(int sockDesc); +}; + +/** + * Socket which is able to connect, send, and receive + */ +class CommunicatingSocket : public Socket { +public: + /** + * Establish a socket connection with the given foreign + * address and port + * @param foreignAddress foreign address (IP address or name) + * @param foreignPort foreign port + * @exception SocketException thrown if unable to establish connection + */ + void connect(const string &foreignAddress, unsigned short foreignPort) + throw(SocketException); + + /** + * Write the given buffer to this socket. Call connect() before + * calling send() + * @param buffer buffer to be written + * @param bufferLen number of bytes from buffer to be written + * @exception SocketException thrown if unable to send data + */ + void send(const void *buffer, int bufferLen) throw(SocketException); + + /** + * Read into the given buffer up to bufferLen bytes data from this + * socket. Call connect() before calling recv() + * @param buffer buffer to receive the data + * @param bufferLen maximum number of bytes to read into buffer + * @return number of bytes read, 0 for EOF, and -1 for error + * @exception SocketException thrown if unable to receive data + */ + int recv(void *buffer, int bufferLen) throw(SocketException); + + /** + * Get the foreign address. Call connect() before calling recv() + * @return foreign address + * @exception SocketException thrown if unable to fetch foreign address + */ + string getForeignAddress() throw(SocketException); + + /** + * Get the foreign port. Call connect() before calling recv() + * @return foreign port + * @exception SocketException thrown if unable to fetch foreign port + */ + unsigned short getForeignPort() throw(SocketException); + +protected: + CommunicatingSocket(int type, int protocol) throw(SocketException); + CommunicatingSocket(int newConnSD); +}; + +/** + * TCP socket for communication with other TCP sockets + */ +class TCPSocket : public CommunicatingSocket { +public: + /** + * Construct a TCP socket with no connection + * @exception SocketException thrown if unable to create TCP socket + */ + TCPSocket() throw(SocketException); + + /** + * Construct a TCP socket with a connection to the given foreign address + * and port + * @param foreignAddress foreign address (IP address or name) + * @param foreignPort foreign port + * @exception SocketException thrown if unable to create TCP socket + */ + TCPSocket(const string &foreignAddress, unsigned short foreignPort) + throw(SocketException); + +private: + // Access for TCPServerSocket::accept() connection creation + friend class TCPServerSocket; + TCPSocket(int newConnSD); +}; + +/** + * TCP socket class for servers + */ +class TCPServerSocket : public Socket { +public: + /** + * Construct a TCP socket for use with a server, accepting connections + * on the specified port on any interface + * @param localPort local port of server socket, a value of zero will + * give a system-assigned unused port + * @param queueLen maximum queue length for outstanding + * connection requests (default 5) + * @exception SocketException thrown if unable to create TCP server socket + */ + TCPServerSocket(unsigned short localPort, int queueLen = 5) + throw(SocketException); + + /** + * Construct a TCP socket for use with a server, accepting connections + * on the specified port on the interface specified by the given address + * @param localAddress local interface (address) of server socket + * @param localPort local port of server socket + * @param queueLen maximum queue length for outstanding + * connection requests (default 5) + * @exception SocketException thrown if unable to create TCP server socket + */ + TCPServerSocket(const string &localAddress, unsigned short localPort, + int queueLen = 5) throw(SocketException); + + /** + * Blocks until a new connection is established on this socket or error + * @return new connection socket + * @exception SocketException thrown if attempt to accept a new connection fails + */ + TCPSocket *accept() throw(SocketException); + +private: + void setListen(int queueLen) throw(SocketException); +}; + +/** + * UDP socket class + */ +class UDPSocket : public CommunicatingSocket { +public: + /** + * Construct a UDP socket + * @exception SocketException thrown if unable to create UDP socket + */ + UDPSocket() throw(SocketException); + + /** + * Construct a UDP socket with the given local port + * @param localPort local port + * @exception SocketException thrown if unable to create UDP socket + */ + UDPSocket(unsigned short localPort) throw(SocketException); + + /** + * Construct a UDP socket with the given local port and address + * @param localAddress local address + * @param localPort local port + * @exception SocketException thrown if unable to create UDP socket + */ + UDPSocket(const string &localAddress, unsigned short localPort) + throw(SocketException); + + /** + * Unset foreign address and port + * @return true if disassociation is successful + * @exception SocketException thrown if unable to disconnect UDP socket + */ + void disconnect() throw(SocketException); + + /** + * Send the given buffer as a UDP datagram to the + * specified address/port + * @param buffer buffer to be written + * @param bufferLen number of bytes to write + * @param foreignAddress address (IP address or name) to send to + * @param foreignPort port number to send to + * @return true if send is successful + * @exception SocketException thrown if unable to send datagram + */ + void sendTo(const void *buffer, int bufferLen, const string &foreignAddress, + unsigned short foreignPort) throw(SocketException); + + /** + * Read read up to bufferLen bytes data from this socket. The given buffer + * is where the data will be placed + * @param buffer buffer to receive data + * @param bufferLen maximum number of bytes to receive + * @param sourceAddress address of datagram source + * @param sourcePort port of data source + * @return number of bytes received and -1 for error + * @exception SocketException thrown if unable to receive datagram + */ + int recvFrom(void *buffer, int bufferLen, string &sourceAddress, + unsigned short &sourcePort) throw(SocketException); + + /** + * Set the multicast TTL + * @param multicastTTL multicast TTL + * @exception SocketException thrown if unable to set TTL + */ + void setMulticastTTL(unsigned char multicastTTL) throw(SocketException); + + /** + * Join the specified multicast group + * @param multicastGroup multicast group address to join + * @exception SocketException thrown if unable to join group + */ + void joinGroup(const string &multicastGroup) throw(SocketException); + + /** + * Leave the specified multicast group + * @param multicastGroup multicast group address to leave + * @exception SocketException thrown if unable to leave group + */ + void leaveGroup(const string &multicastGroup) throw(SocketException); + +private: + void setBroadcast(); +}; + +#endif diff --git a/iotjava/iotruntime/slave/IoTDeviceAddress.java b/iotjava/iotruntime/slave/IoTDeviceAddress.java index 2917e98..1b4bc10 100644 --- a/iotjava/iotruntime/slave/IoTDeviceAddress.java +++ b/iotjava/iotruntime/slave/IoTDeviceAddress.java @@ -36,7 +36,8 @@ public class IoTDeviceAddress extends IoTAddress { * @param _isSrcPortWildCard Is this source port a wild card (=can change port number)? * @param _isDstPortWildCard Is this destination port a wild card (=can change port number)? */ - protected IoTDeviceAddress(String _sAddress, int _iSrcPort, int _iDstPort, boolean _isSrcPortWildCard, + protected IoTDeviceAddress(String _sAddress, int _iSrcPort, int _iDstPort, boolean _isSrcPortWildCard, + //public IoTDeviceAddress(String _sAddress, int _iSrcPort, int _iDstPort, boolean _isSrcPortWildCard, boolean _isDstPortWildCard) throws UnknownHostException { super(_sAddress); diff --git a/iotjava/iotruntime/slave/IoTSet.java b/iotjava/iotruntime/slave/IoTSet.java index 5330e8e..1676fa5 100644 --- a/iotjava/iotruntime/slave/IoTSet.java +++ b/iotjava/iotruntime/slave/IoTSet.java @@ -30,6 +30,7 @@ public final class IoTSet { * Class constructor (pass the reference to this immutable wrapper) */ protected IoTSet(Set s) { + //public IoTSet(Set s) { set = s; } -- 2.34.1