int RawSocketDevice::receivePackets(RawPacketVector& packetVec, int timeout, int& failedRecv) { if (!isOpened()) { LOG_ERROR("Device is not open"); return 0; } long curSec, curNsec; clockGetTime(curSec, curNsec); int packetCount = 0; failedRecv = 0; long timeoutSec = curSec + timeout; while (curSec < timeoutSec) { RawPacket* rawPacket = new RawPacket(); if (receivePacket(*rawPacket, true, timeoutSec-curSec) == RecvSuccess) { packetVec.pushBack(rawPacket); packetCount++; } else { failedRecv++; delete rawPacket; } clockGetTime(curSec, curNsec); } return packetCount; }
//------------------------------------------------------------------------------ unsigned char timerExpired(Timer *t) { uint32_t time = clockGetTime(); if(!t->active) { return 0; } if(time > t->start) { uint32_t diff = (time - t->start); if(diff > t->interval) { t->active = 0; return 1; } else { return 0; } } else { if(((~t->start) + time + 1) >= t->interval) { t->active = 0; return 1; } else { return 0; } } }
/* * init all programs component */ void init() { initDrivers(); //set init values cyclicBufferInit(&gReadBuffer); cyclicBufferInit(&gWriteBuffer); gLastClockReading = clockGetTime(); gInterval = INTERVAL_INIT_VAL; gCounter = COUNTER_INIT_VAL; segmentsSetNumber(gCounter); }
/* * Checks if the colck has moved forward (modolo max UINT32) for gInterval, * and updates the couter and the 7-segments device. */ void checkClock() { UINT32 currentClock = clockGetTime(); //check if the clock started another cycle or not if ( //no clock overlap ((currentClock > gLastClockReading) && (currentClock - gLastClockReading >= gInterval)) || //clock overlaps ((gLastClockReading > currentClock) && (currentClock + (MAX_UINT32 - gLastClockReading) >= gInterval)) ) { updateCounter(); gLastClockReading = currentClock; } }
//------------------------------------------------------------------------------ void timerRestart(Timer *t) { t->active = 1; t->start = clockGetTime(); }
//------------------------------------------------------------------------------ void timerSet(Timer *t, uint32_t interval) { t->active = 1; t->start = clockGetTime(); t->interval = interval; }