Changing a few things to pointer; Testing Lifxtest benchmark without using runtime...
[iot2.git] / benchmarks / drivers / Cpp / LifxLightBulb / LifxLightBulb.hpp
1 #ifndef _LIFXLIGHTBULB_HPP__
2 #define _LIFXLIGHTBULB_HPP__
3 #include <iostream>
4 #include <atomic>
5 #include <mutex>
6 #include <thread>
7 #include <chrono>
8
9 #include <string.h>
10 #include <time.h>
11
12 #include "LightBulb.hpp"
13 #include "Socket.hpp"
14 #include "IoTRMIUtil.hpp"
15 #include "IoTSet.hpp"
16 #include "IoTUDP.hpp"
17 #include "IoTDeviceAddress.hpp"
18 #include "Iterator.hpp"
19
20 // Helper classes for LifxLightBulb
21 #include "LifxHeader.hpp"
22 #include "BulbColor.hpp"
23 #include "DeviceStateGroup.hpp"
24 #include "DeviceStateHostFirmware.hpp"
25 #include "DeviceStateHostInfo.hpp"
26 #include "DeviceStateInfo.hpp"
27 #include "DeviceStateLocation.hpp"
28 #include "DeviceStateService.hpp"
29 #include "DeviceStateVersion.hpp"
30 #include "DeviceStateWifiFirmware.hpp"
31 #include "DeviceStateWifiInfo.hpp"
32 #include "LightState.hpp"
33
34
35 using namespace std;
36
37 // Driver LifxLightBulb
38 // Implemented based on LightBulb virtual class (interface)
39
40 //std::atomic
41 std::atomic<bool> didAlreadyInit(false);
42 std::atomic<bool> didGetBulbVersion(false);
43
44 class LifxLightBulb : public LightBulb
45 {
46         private:
47                 // Constants
48                 const static int64_t GET_BULB_VERSION_RESEND_WAIT_SECONDS = 10;
49
50                 // Variables
51                 IoTUDP *communicationSocket;
52                 char bulbMacAddress[8];
53                 //TODO:
54                 //static Semaphore socketMutex = new Semaphore(1);
55                 bool sendSocketFlag = false;
56
57                 // Current Bulb Values
58                 int currentHue = 0;
59                 int currentSaturation = 0;
60                 int currentBrightness = 65535;
61                 int currentTemperature = 9000;
62                 bool bulbIsOn = false;
63
64                 //std::atomic
65                 atomic<bool> didAlreadyInit;
66                 atomic<bool> didGetBulbVersion;
67
68                 // Mutex locks
69                 mutex socketMutex;
70                 mutex settingBulbColorMutex;
71                 mutex settingBulbTemperatureMutex;
72                 mutex bulbStateMutex;
73
74                 // color and temperature ranges for the bulbs
75                 int hueLowerBound = 0;
76                 int hueUpperBound = 0;
77                 int saturationLowerBound = 0;
78                 int saturationUpperBound = 0;
79                 int brightnessLowerBound = 0;
80                 int brightnessUpperBound = 0;
81                 int temperatureLowerBound = 2500;
82                 int temperatureUpperBound = 9000;
83
84                 // Check if a state change was requested, used to poll the bulb for if the bulb did
85                 // preform the requested state change
86                 bool stateDidChange = false;
87
88                 // Device address
89                 IoTSet<IoTDeviceAddress*>* lb_addresses;
90
91         public:
92
93                 // Constructor
94                 LifxLightBulb();
95                 LifxLightBulb(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress);
96                 ~LifxLightBulb();
97                 // Initialize the lightbulb
98                 void init();
99                 void turnOff();
100                 void turnOn();
101                 double getHue();
102                 double getSaturation();
103                 double getBrightness();
104                 int getTemperature();
105                 double getHueRangeLowerBound();
106                 double getHueRangeUpperBound();
107                 double getSaturationRangeLowerBound();
108                 double getSaturationRangeUpperBound();
109                 double getBrightnessRangeLowerBound();
110                 double getBrightnessRangeUpperBound();
111                 int getTemperatureRangeLowerBound();
112                 int getTemperatureRangeUpperBound();
113                 void setTemperature(int _temperature);
114                 void setColor(double _hue, double _saturation, double _brightness);
115                 bool getState();
116
117         private:
118                 // Private functions
119                 // Communication helpers
120                 void receivedPacket(char* packetData);
121                 void sendPacket(char* packetData, int len);
122                 // Worker function which runs the while loop for receiving data from the bulb.
123                 // Is blocking.
124                 void workerFunction(LifxLightBulb* llb);
125                 //  Sending
126                 //  Device Messages
127                 void sendGetServicePacket();
128                 void sendGetHostInfoPacket();
129                 void sendGetHostFirmwarePacket();
130                 void sendGetWifiInfoPacket();
131                 void sendGetWifiFirmwarePacket();
132                 void sendGetPowerPacket();
133                 void sendSetPowerPacket(int level);
134                 void sendGetLabelPacket();
135                 void sendSetLabelPacket(string label);
136                 void sendGetVersionPacket();
137                 void sendGetInfoPacket();
138                 void sendGetLocationPacket();
139                 void sendGetGroupPacket();
140                 //  Sending
141                 //  Light Messages
142                 void sendGetLightStatePacket();
143                 void sendSetLightColorPacket(BulbColor* bulbColor, long duration);
144                 void sendGetLightPowerPacket();
145                 void sendSetLightPowerPacket(int level, long duration);
146                 void sendEchoRequestPacket(char data[64]);
147                 // Receiving
148                 // Device Messages
149                 DeviceStateService* parseDeviceStateServiceMessage(char* payloadData);
150                 DeviceStateHostInfo* parseDeviceStateHostInfoMessage(char* payloadData);
151                 DeviceStateHostFirmware* parseDeviceStateHostFirmwareMessage(char* payloadData);
152                 DeviceStateWifiInfo* parseDeviceStateWifiInfoMessage(char* payloadData);
153                 DeviceStateWifiFirmware* parseDeviceStateWifiFirmwareMessage(char* payloadData);
154                 int parseStatePowerMessage(char* payloadData);
155                 DeviceStateVersion* parseDeviceStateVersionMessage(char* payloadData);
156                 DeviceStateInfo* parseDeviceStateInfoMessage(char* payloadData);
157                 DeviceStateLocation* parseDeviceStateLocationMessage(char* payloadData);
158                 DeviceStateGroup* parseDeviceStateGroupMessage(char* payloadData);
159                 // Receiving
160                 // Light Messages
161                 LightState* parseLightStateMessage(char* payloadData);
162                 int parseLightStatePowerMessage(char* payloadData);
163                 // Private Handlers
164                 void handleStateVersionMessageReceived(char* payloadData);
165                 void handleLightStateMessageReceived(char* payloadData);
166 };
167 #endif