Perfecting IoTSlave for C++; Found one issue with SSH/Java process execution in which...
[iot2.git] / benchmarks / drivers / Cpp / LifxLightBulb / LifxLightBulb.cpp
index af7b89292551c224159b9b63f26404cf3d762321..07076db9f9002d0711505b89e8ade62f17c12c21 100644 (file)
 
 using namespace std;
 
-// External creator/destroyer
-/*extern "C" LifxLightBulb* create(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress) {
-       return new LifxLightBulb(_devAddress, macAddress);
+
+// External functions to create, destroy and initialize this class object
+extern "C" void* createLifxLightBulb(void** params) {
+       // Arguments: IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress
+       return new LifxLightBulb((IoTSet<void*>*) 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();
 }
 
-extern "C" void destroy(LifxLightBulb* t) {
-       delete t;
-}*/
 
 // Constructor
 LifxLightBulb::LifxLightBulb() { 
@@ -48,7 +59,8 @@ LifxLightBulb::LifxLightBulb() {
 }
 
 
-LifxLightBulb::LifxLightBulb(IoTSet<IoTDeviceAddress*>* _devAddress, string macAddress) {
+// 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];
@@ -61,12 +73,15 @@ LifxLightBulb::LifxLightBulb(IoTSet<IoTDeviceAddress*>* _devAddress, string macA
                tmpMacByte[1] = tmpMacAddress[i+1];
                bulbMacAddress[i/2] = (char) strtol(tmpMacByte, NULL, 16);
        }
-       cout << "MAC address is set. Value: ";
+       //cout << "MAC address is set. Value: ";
        IoTRMIUtil::printBytes(bulbMacAddress, 8, false);
+       // Logging
+       log.open("LifxLightBulb_cpp.log");
+       log << "MAC address is " << macAddress << endl;
 
        // Initialize device address
        lb_addresses = _devAddress;
-       cout << "Device address is set! " << endl;
+       //cout << "Device address is set! " << endl;
 }
 
 
@@ -78,9 +93,10 @@ LifxLightBulb::~LifxLightBulb() {
                delete communicationSocket;
                communicationSocket = NULL;             
        }
-       for(IoTDeviceAddress* dev : *lb_addresses) {
-               delete dev;
-               dev = NULL;
+       for(void* dev : *lb_addresses) {
+               IoTDeviceAddress* dv = (IoTDeviceAddress*) dev;
+               delete dv;
+               dv = NULL;
        }
        if (lb_addresses != NULL) {
 
@@ -97,16 +113,20 @@ void LifxLightBulb::init() {
        if (didAlreadyInit.exchange(true))
                return;
 
-       unordered_set<IoTDeviceAddress*>::const_iterator itr = lb_addresses->begin();
-       IoTDeviceAddress* deviceAddress = *itr;
-       cout << "Address: " << deviceAddress->getAddress() << endl;
+       unordered_set<void*>::const_iterator itr = lb_addresses->begin();
+       IoTDeviceAddress* deviceAddress = (IoTDeviceAddress*) *itr;
+       //cout << "Address: " << deviceAddress->getAddress() << endl;
+       log << "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;
+       log << "Host address: " << communicationSocket->getHostAddress() << endl;
+       log << "Source port: " << communicationSocket->getSourcePort() << endl;
+       log << "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
@@ -115,7 +135,9 @@ void LifxLightBulb::init() {
        thread th1 (&LifxLightBulb::workerFunction, this, this);
        th1.detach();
 
-       cout << "Initialized LifxLightBulb!" << endl;
+       //cout << "Initialized LifxLightBulb!" << endl;
+       log << "Initialized LifxLightBulb!" << endl;
+       log.close();
 }
 
 
@@ -317,15 +339,15 @@ void LifxLightBulb::receivedPacket(char* packetData) {
        }
 
        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;
@@ -344,7 +366,8 @@ void LifxLightBulb::receivedPacket(char* packetData) {
                        break;
 
                default:
-                       cout << "unknown packet Type" << endl;
+                       break;
+                       //cout << "unknown packet Type" << endl;
        }
        // Avoid memory leaks
        delete payloadBytes;
@@ -380,7 +403,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;
                        }
@@ -1145,10 +1168,10 @@ void onOff(LifxLightBulb *llb) {
 
        for (int i = 0; i < 2; i++) {
                llb->turnOff();
-               cout << "Turning off!" << endl;
+               //cout << "Turning off!" << endl;
                this_thread::sleep_for (chrono::milliseconds(1000));
                llb->turnOn();
-               cout << "Turning on!" << endl;
+               //cout << "Turning on!" << endl;
                this_thread::sleep_for (chrono::milliseconds(1000));
        }
 }
@@ -1157,33 +1180,33 @@ void onOff(LifxLightBulb *llb) {
 void adjustTemp(LifxLightBulb *llb) {
 
        for (int i = 2500; i < 9000; i += 100) {
-               cout << "Adjusting Temp: " << i << endl;
+               //cout << "Adjusting Temp: " << i << endl;
                llb->setTemperature(i);
                this_thread::sleep_for (chrono::milliseconds(100));
        }
-       cout << "Adjusted temperature to 9000!" << endl;
+       //cout << "Adjusted temperature to 9000!" << endl;
        for (int i = 9000; i > 2500; i -= 100) {
-               cout << "Adjusting Temp: " << i << endl;
+               //cout << "Adjusting Temp: " << i << endl;
                llb->setTemperature(i);
                this_thread::sleep_for (chrono::milliseconds(100));
        }
-       cout << "Adjusted temperature to 2500!" << endl;
+       //cout << "Adjusted temperature to 2500!" << endl;
 }
 
 
 void adjustBright(LifxLightBulb *llb) {
        for (int i = 100; i > 0; i -= 10) {
-               cout << "Adjusting Brightness: " << i << endl;
+               //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;
+       //cout << "Adjusted brightness to 0!" << endl;
        for (int i = 0; i < 100; i += 10) {
-               cout << "Adjusting Brightness: " << i << endl;
+               //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;
+       //cout << "Adjusting brightness to 100!" << endl;
 }