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