Cleaning up drivers/Cpp, Cpp/Lifxtest, virtuals, and iotrmi/C++ (revisiting the C...
[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
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<void*>* lb_addresses;    // IoTSet<IoTDeviceAddress*>* lb_addresses
90
91                 // Logging
92                 ofstream log;
93         public:
94
95                 // Constructor
96                 LifxLightBulb();
97                 //LifxLightBulb(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress);
98                 LifxLightBulb(IoTSet<void*>* _devAddress, string macAddress);
99                 ~LifxLightBulb();
100                 // Initialize the lightbulb
101                 void init();
102                 void turnOff();
103                 void turnOn();
104                 double getHue();
105                 double getSaturation();
106                 double getBrightness();
107                 int getTemperature();
108                 double getHueRangeLowerBound();
109                 double getHueRangeUpperBound();
110                 double getSaturationRangeLowerBound();
111                 double getSaturationRangeUpperBound();
112                 double getBrightnessRangeLowerBound();
113                 double getBrightnessRangeUpperBound();
114                 int getTemperatureRangeLowerBound();
115                 int getTemperatureRangeUpperBound();
116                 void setTemperature(int _temperature);
117                 void setColor(double _hue, double _saturation, double _brightness);
118                 bool getState();
119
120         private:
121                 // Private functions
122                 // Communication helpers
123                 void receivedPacket(char* packetData);
124                 void sendPacket(char* packetData, int len);
125                 // Worker function which runs the while loop for receiving data from the bulb.
126                 // Is blocking.
127                 void workerFunction(LifxLightBulb* llb);
128                 //  Sending
129                 //  Device Messages
130                 void sendGetServicePacket();
131                 void sendGetHostInfoPacket();
132                 void sendGetHostFirmwarePacket();
133                 void sendGetWifiInfoPacket();
134                 void sendGetWifiFirmwarePacket();
135                 void sendGetPowerPacket();
136                 void sendSetPowerPacket(int level);
137                 void sendGetLabelPacket();
138                 void sendSetLabelPacket(string label);
139                 void sendGetVersionPacket();
140                 void sendGetInfoPacket();
141                 void sendGetLocationPacket();
142                 void sendGetGroupPacket();
143                 //  Sending
144                 //  Light Messages
145                 void sendGetLightStatePacket();
146                 void sendSetLightColorPacket(BulbColor* bulbColor, long duration);
147                 void sendGetLightPowerPacket();
148                 void sendSetLightPowerPacket(int level, long duration);
149                 void sendEchoRequestPacket(char data[64]);
150                 // Receiving
151                 // Device Messages
152                 DeviceStateService* parseDeviceStateServiceMessage(char* payloadData);
153                 DeviceStateHostInfo* parseDeviceStateHostInfoMessage(char* payloadData);
154                 DeviceStateHostFirmware* parseDeviceStateHostFirmwareMessage(char* payloadData);
155                 DeviceStateWifiInfo* parseDeviceStateWifiInfoMessage(char* payloadData);
156                 DeviceStateWifiFirmware* parseDeviceStateWifiFirmwareMessage(char* payloadData);
157                 int parseStatePowerMessage(char* payloadData);
158                 DeviceStateVersion* parseDeviceStateVersionMessage(char* payloadData);
159                 DeviceStateInfo* parseDeviceStateInfoMessage(char* payloadData);
160                 DeviceStateLocation* parseDeviceStateLocationMessage(char* payloadData);
161                 DeviceStateGroup* parseDeviceStateGroupMessage(char* payloadData);
162                 // Receiving
163                 // Light Messages
164                 LightState* parseLightStateMessage(char* payloadData);
165                 int parseLightStatePowerMessage(char* payloadData);
166                 // Private Handlers
167                 void handleStateVersionMessageReceived(char* payloadData);
168                 void handleLightStateMessageReceived(char* payloadData);
169 };
170
171 #endif