#include <iostream>
#include "IoTDeviceAddress.h"
-#include "application.h"
using namespace std;
private:
//UDPSocket *socket;
UDP socket;
- uint8_t* strHostAddress;
+ IPAddress strHostAddress;
int iSrcPort;
int iDstPort;
bool didClose;
iDstPort = iotDevAdd->getDestinationPortNumber();
timeOut = 0;
- //socket = new UDPSocket(iSrcPort);
socket.begin(iSrcPort);
didClose = false;
}
}
- uint8_t* getHostAddress() {
+ IPAddress getHostAddress() {
return strHostAddress;
}
// Send data packet
void sendData(const char* buffer, int bufferLen) {
- //void sendData(const void* buffer, int bufferLen) {
+
unsigned short destinationPort = (unsigned short) iDstPort;
- IPAddress ipFromBytes = strHostAddress;
- socket.sendPacket(buffer, bufferLen, ipFromBytes, destinationPort);
- //socket->sendTo(buffer, bufferLen, strHostAddress, destinationPort);
+ IPAddress ipFromBytes(strHostAddress);
+ socket.sendPacket(buffer, bufferLen, strHostAddress, destinationPort);
}
// Receive data packet
int receiveData(char* buffer, int iMaxDataLength) {
- //int receiveData(void* buffer, int iMaxDataLength) {
- //unsigned short destinationPort = (unsigned short) iDstPort;
+
return socket.receivePacket(buffer, iMaxDataLength);
- //return socket->recvFrom(buffer, iMaxDataLength, strHostAddress, destinationPort);
}
};
#endif
#include "LifxLightBulb.h"
#include "IoTSet.h"
#include "IoTDeviceAddress.h"
+#include "application.h"
using namespace std;
-/* TODO: Uncomment this to do normal C++ (g++)
-int main(int argc, char *argv[])
-{
- string macAddress1 = "D073D5128E300000";
- //string macAddress1 = "D073D50241DA0000";
- string devIPAddress1 = "192.168.1.126";
- //string devIPAddress1 = "192.168.1.232";
- LifxLightBulb *llb1 = new LifxLightBulb(devIPAddress1, macAddress1, 12345);
-
- llb1->init();
- for(int i=0; i < 1; i++) {
- llb1->turnOff();
- this_thread::sleep_for (chrono::milliseconds(1000));
- cout << "Turn off" << endl;
- llb1->turnOn();
- this_thread::sleep_for (chrono::milliseconds(1000));
- cout << "Turn on" << endl;
- }
-
-
- //delete setDevAddress1;
- delete llb1;
-
- return 0;
-}*/
-
-// Constructor
+// Default constructor
LifxLightBulb::LifxLightBulb() {
- // LB1 macAddress: d0:73:d5:12:8e:30
- // LB2 macAddress: d0:73:d5:02:41:da
- string macAddress = "D073D5128E300000";
- //string macAddress = "D073D50241DA0000";
- /*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());
- 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);
- }
+
}
// Constructor
-LifxLightBulb::LifxLightBulb(uint8_t* ipAddress, string macAddress, int srcPort) {
-
- // 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: ";
+LifxLightBulb::LifxLightBulb(IPAddress ipAddress, char* macAddress, int srcPort) {
+ memcpy(bulbMacAddress, macAddress, 8);
+
// Initialize device address
// Port 56700 is the default port for Lifx
IoTDeviceAddress* devAddress = new IoTDeviceAddress(ipAddress, srcPort, 56700, false, false);
myset1->insert(devAddress);
IoTSet<void*>* setDevAddress = new IoTSet<void*>(myset1);
lb_addresses = setDevAddress;
- //cout << "Device address is set! " << endl;
-
-}
-
-
-// Driver constructor always gets a pointer to device address, trailed by class arguments of generic type
-LifxLightBulb::LifxLightBulb(IoTSet<void*>* _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: ";
-
- // Initialize device address
- lb_addresses = _devAddress;
- //cout << "Device address is set! " << endl;
}
// Initialize the lightbulb
void LifxLightBulb::init() {
- //if (didAlreadyInit.exchange(true))
if (didAlreadyInit)
return;
// Set to true if not yet
unordered_set<void*>::const_iterator itr = lb_addresses->begin();
IoTDeviceAddress* deviceAddress = (IoTDeviceAddress*) *itr;
- //cout << "Address: " << deviceAddress->getAddress() << endl;
// Create IoTUDP socket
communicationSocket = new IoTUDP(deviceAddress);
// the correct references to stuff
thread th1 (&LifxLightBulb::workerFunction, this, this);
th1.detach();
-
- //cout << "Initialized LifxLightBulb!" << endl;
}
}
// Wait a bit as to not tie up system resources
- this_thread::sleep_for (chrono::milliseconds(100));
+ //this_thread::sleep_for (chrono::milliseconds(100));
+ delay(100);
//cout << endl << "Sleep and wake up!" << endl;
}
}
// Driver LifxLightBulb
// Implemented based on LightBulb virtual class (interface)
-//std::atomic
-//std::atomic<bool> didAlreadyInit(false);
-//std::atomic<bool> didGetBulbVersion(false);
-
class LifxLightBulb : public LightBulb
{
private:
IoTUDP *communicationSocket;
char bulbMacAddress[8];
//TODO:
- //static Semaphore socketMutex = new Semaphore(1);
bool sendSocketFlag = false;
// Current Bulb Values
int currentTemperature = 9000;
bool bulbIsOn = false;
- //std::atomic
- //atomic<bool> didAlreadyInit;
- bool didAlreadyInit;
- //atomic<bool> didGetBulbVersion;
- bool didGetBulbVersion;
+ bool didAlreadyInit = false;
+ bool didGetBulbVersion = false;
// Mutex locks
mutex socketMutex;
// Constructor
LifxLightBulb();
- LifxLightBulb(uint8_t* ipAddress, string macAddress, int srcPort);
- //LifxLightBulb(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress);
- LifxLightBulb(IoTSet<void*>* _devAddress, string macAddress);
+ LifxLightBulb(IPAddress ipAddress, char* macAddress, int srcPort);
~LifxLightBulb();
// Initialize the lightbulb
void init();