X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=benchmarks%2Fdrivers%2FCpp%2FLifxLightBulb%2FLifxLightBulb.cpp;h=667d765bf27d2a44e725d39dc49259b0b41085f5;hb=ffea2cb68e5a2e1bac041fe2279620eba8d227f8;hp=669548ffcb744a6a4bc7816007db674a480bd31c;hpb=47345575aebb8df39274ddf83d9cfbde7fd5ad83;p=iot2.git diff --git a/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp b/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp index 669548f..667d765 100644 --- a/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp +++ b/benchmarks/drivers/Cpp/LifxLightBulb/LifxLightBulb.cpp @@ -11,6 +11,25 @@ using namespace std; +// External functions to create, destroy and initialize this class object +extern "C" void* createLifxLightBulb(void** params) { + // Arguments: IoTSet* _devAddress, string macAddress + return new LifxLightBulb((IoTSet*) params[0], *((string*) params[1])); +} + + +extern "C" void destroyLifxLightBulb(void* t) { + LifxLightBulb* llb = (LifxLightBulb*) t; + delete llb; +} + + +extern "C" void initLifxLightBulb(void* t) { + LifxLightBulb* llb = (LifxLightBulb*) t; + llb->init(); +} + + // Constructor LifxLightBulb::LifxLightBulb() { // LB1 macAddress: d0:73:d5:12:8e:30 @@ -28,7 +47,6 @@ LifxLightBulb::LifxLightBulb() { 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]; @@ -36,11 +54,11 @@ LifxLightBulb::LifxLightBulb() { tmpMacByte[1] = tmpMacAddress[i+1]; bulbMacAddress[i/2] = (char) strtol(tmpMacByte, NULL, 16); } - //IoTRMIUtil::printBytes(bulbMacAddress, 8, false); } -LifxLightBulb::LifxLightBulb(IoTSet _devAddress, string macAddress) { +// Driver constructor always gets a pointer to device address, trailed by class arguments of generic type +LifxLightBulb::LifxLightBulb(IoTSet* _devAddress, string macAddress) { // Initialize macAddress char tmpMacAddress[16]; @@ -53,12 +71,11 @@ LifxLightBulb::LifxLightBulb(IoTSet _devAddress, string macAd tmpMacByte[1] = tmpMacAddress[i+1]; bulbMacAddress[i/2] = (char) strtol(tmpMacByte, NULL, 16); } - cout << "MAC address is set. Value: "; - IoTRMIUtil::printBytes(bulbMacAddress, 8, false); + //cout << "MAC address is set. Value: "; // Initialize device address lb_addresses = _devAddress; - cout << "Device address is set! " << endl; + //cout << "Device address is set! " << endl; } @@ -70,6 +87,16 @@ LifxLightBulb::~LifxLightBulb() { delete communicationSocket; communicationSocket = NULL; } + for(void* dev : *lb_addresses) { + IoTDeviceAddress* dv = (IoTDeviceAddress*) dev; + delete dv; + dv = NULL; + } + if (lb_addresses != NULL) { + + delete lb_addresses; + lb_addresses = NULL; + } } @@ -80,16 +107,16 @@ void LifxLightBulb::init() { if (didAlreadyInit.exchange(true)) return; - unordered_set::const_iterator itr = lb_addresses.begin(); - IoTDeviceAddress* deviceAddress = *itr; - cout << "Address: " << deviceAddress->getAddress() << endl; + unordered_set::const_iterator itr = lb_addresses->begin(); + IoTDeviceAddress* deviceAddress = (IoTDeviceAddress*) *itr; + //cout << "Address: " << deviceAddress->getAddress() << endl; // Create IoTUDP socket communicationSocket = new IoTUDP(deviceAddress); - cout << "Host address: " << communicationSocket->getHostAddress() << endl; - cout << "Source port: " << communicationSocket->getSourcePort() << endl; - cout << "Destination port: " << communicationSocket->getDestinationPort() << endl << endl; + //cout << "Host address: " << communicationSocket->getHostAddress() << endl; + //cout << "Source port: " << communicationSocket->getSourcePort() << endl; + //cout << "Destination port: " << communicationSocket->getDestinationPort() << endl << endl; // Launch the worker function in a separate thread. // NOTE: "this" pointer is passed into the detached thread because it does not belong @@ -98,7 +125,7 @@ void LifxLightBulb::init() { thread th1 (&LifxLightBulb::workerFunction, this, this); th1.detach(); - cout << "Initialized LifxLightBulb!" << endl; + //cout << "Initialized LifxLightBulb!" << endl; } @@ -293,22 +320,21 @@ void LifxLightBulb::receivedPacket(char* packetData) { recHeader.setFromBytes(headerBytes); // load the payload bytes (strip away the header) - //char payloadBytes[recHeader.getSize()]; char* payloadBytes = new char[recHeader.getSize()]; for (int i = 36; i < recHeader.getSize(); i++) { payloadBytes[i - 36] = packetData[i]; } int type = recHeader.getType(); - cout << "Received: " << type << endl; + //cout << "Received: " << type << endl; DeviceStateService* dat = NULL; switch (type) { case 3: dat = parseDeviceStateServiceMessage(payloadBytes); - cout << "Service: " << dat->getService(); - cout << "Port : " << dat->getPort(); + //cout << "Service: " << dat->getService(); + //cout << "Port : " << dat->getPort(); // Avoid memory leak - delete this object delete dat; break; @@ -327,7 +353,8 @@ void LifxLightBulb::receivedPacket(char* packetData) { break; default: - cout << "unknown packet Type" << endl; + break; + //cout << "unknown packet Type" << endl; } // Avoid memory leaks delete payloadBytes; @@ -350,7 +377,6 @@ void LifxLightBulb::workerFunction(LifxLightBulb* llb) { // Need timeout on receives since we are not sure if a packet will be available // for processing so don't block waiting llb->communicationSocket->setTimeOut(50000); // In milliseconds - llb->turnOff(); int64_t lastSentGetBulbVersionRequest = 0; // time last request sent @@ -363,7 +389,7 @@ void LifxLightBulb::workerFunction(LifxLightBulb* llb) { int64_t currentTime = (int64_t) time(NULL); if ((currentTime - lastSentGetBulbVersionRequest) > llb->GET_BULB_VERSION_RESEND_WAIT_SECONDS) { // Get the bulb version so we know what type of bulb this is. - cout << "Sending version packet! " << endl; + //cout << "Sending version packet! " << endl; llb->sendGetVersionPacket(); lastSentGetBulbVersionRequest = currentTime; } @@ -763,10 +789,8 @@ void LifxLightBulb::sendSetLightPowerPacket(int level, long duration) { cerr << "Invalid parameter values" << endl; exit(1); } - char packetBytes[42]; - LifxHeader header; header.setSize(42); header.setTagged(false); @@ -1122,76 +1146,3 @@ void LifxLightBulb::handleLightStateMessageReceived(char* payloadData) { delete color; } - -// Functions for the main function -void onOff(LifxLightBulb *llb) { - - for (int i = 0; i < 5; i++) { - llb->turnOff(); - cout << "Turning off!" << endl; - this_thread::sleep_for (chrono::milliseconds(1000)); - llb->turnOn(); - cout << "Turning on!" << endl; - this_thread::sleep_for (chrono::milliseconds(1000)); - } -} - - -void adjustTemp(LifxLightBulb *llb) { - - for (int i = 2500; i < 9000; i += 100) { - cout << "Adjusting Temp: " << i << endl; - llb->setTemperature(i); - this_thread::sleep_for (chrono::milliseconds(100)); - } - cout << "Adjusted temperature to 9000!" << endl; - for (int i = 9000; i > 2500; i -= 100) { - cout << "Adjusting Temp: " << i << endl; - llb->setTemperature(i); - this_thread::sleep_for (chrono::milliseconds(100)); - } - cout << "Adjusted temperature to 2500!" << endl; -} - - -void adjustBright(LifxLightBulb *llb) { - for (int i = 100; i > 0; i -= 10) { - cout << "Adjusting Brightness: " << i << endl; - llb->setColor(llb->getHue(), llb->getSaturation(), i); - this_thread::sleep_for (chrono::milliseconds(100)); - } - cout << "Adjusted brightness to 0!" << endl; - for (int i = 0; i < 100; i += 10) { - cout << "Adjusting Brightness: " << i << endl; - llb->setColor(llb->getHue(), llb->getSaturation(), i); - this_thread::sleep_for (chrono::milliseconds(100)); - } - cout << "Adjusting brightness to 100!" << endl; -} - - -int main(int argc, char *argv[]) -{ - string macAddress = "D073D5128E300000"; - //string macAddress = "D073D50241DA0000"; - string devIPAddress = "192.168.2.126"; - //string devIPAddress = "192.168.2.232"; - //IoTDeviceAddress devAddress(devIPAddress, 12345, 56700, false, false); - IoTDeviceAddress* devAddress = new IoTDeviceAddress(devIPAddress, 12345, 56700, false, false); - unordered_set myset = { devAddress }; - - IoTSet setDevAddress(myset); - LifxLightBulb *llb = new LifxLightBulb(setDevAddress, macAddress); - cout << "Generated LifxLightBulb object!" << endl; - llb->init(); - llb->turnOn(); - onOff(llb); - adjustTemp(llb); - adjustBright(llb); -// llb->turnOff(); - - delete devAddress; - delete llb; - - return 0; -}