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